Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 1cd9d30e authored by Chavi Weingarten's avatar Chavi Weingarten Committed by Android (Google) Code Review
Browse files

Merge "Use unique_ptr for proto parser to handle destruction."

parents 5b769bda 7ba019b2
Loading
Loading
Loading
Loading
+5 −6
Original line number Diff line number Diff line
@@ -56,11 +56,11 @@ bool LayerStats::isEnabled() {
}

void LayerStats::traverseLayerTreeStatsLocked(
        const std::vector<const LayerProtoParser::Layer*>& layerTree,
        std::vector<std::unique_ptr<LayerProtoParser::Layer>> layerTree,
        const LayerProtoParser::LayerGlobal* layerGlobal) {
    for (auto layer : layerTree) {
    for (std::unique_ptr<LayerProtoParser::Layer>& layer : layerTree) {
        if (!layer) continue;
        traverseLayerTreeStatsLocked(layer->children, layerGlobal);
        traverseLayerTreeStatsLocked(std::move(layer->children), layerGlobal);
        std::string key =
                base::StringPrintf("%s,%s,%s,%s,%s,%s,%s,%s,%s",
                                   destinationLocation(layer->hwcFrame.left,
@@ -71,7 +71,7 @@ void LayerStats::traverseLayerTreeStatsLocked(
                                                   layerGlobal->resolution[0], true),
                                   destinationSize(layer->hwcFrame.bottom - layer->hwcFrame.top,
                                                   layerGlobal->resolution[1], false),
                                   layer->type.c_str(), scaleRatioWH(layer).c_str(),
                                   layer->type.c_str(), scaleRatioWH(layer.get()).c_str(),
                                   layerTransform(layer->hwcTransform), layer->pixelFormat.c_str(),
                                   layer->dataspace.c_str());
        mLayerStatsMap[key]++;
@@ -83,8 +83,7 @@ void LayerStats::logLayerStats(const LayersProto& layersProto) {
    auto layerGlobal = LayerProtoParser::generateLayerGlobalInfo(layersProto);
    auto layerTree = LayerProtoParser::generateLayerTree(layersProto);
    std::lock_guard<std::mutex> lock(mMutex);
    traverseLayerTreeStatsLocked(layerTree, &layerGlobal);
    LayerProtoParser::destroyLayerTree(layerTree);
    traverseLayerTreeStatsLocked(std::move(layerTree), &layerGlobal);
}

void LayerStats::dump(String8& result) {
+3 −2
Original line number Diff line number Diff line
@@ -37,7 +37,8 @@ public:

private:
    // Traverse layer tree to get all visible layers' stats
    void traverseLayerTreeStatsLocked(const std::vector<const LayerProtoParser::Layer*>& layerTree,
    void traverseLayerTreeStatsLocked(
        std::vector<std::unique_ptr<LayerProtoParser::Layer>> layerTree,
        const LayerProtoParser::LayerGlobal* layerGlobal);
    // Convert layer's top-left position into 8x8 percentage of the display
    static const char* destinationLocation(int32_t location, int32_t range, bool isHorizontal);
+1 −1
Original line number Diff line number Diff line
@@ -4062,7 +4062,7 @@ void SurfaceFlinger::dumpAllLocked(const Vector<String16>& args, size_t& index,

    LayersProto layersProto = dumpProtoInfo(LayerVector::StateSet::Current);
    auto layerTree = LayerProtoParser::generateLayerTree(layersProto);
    result.append(LayerProtoParser::layersToString(layerTree).c_str());
    result.append(LayerProtoParser::layersToString(std::move(layerTree)).c_str());

    /*
     * Dump Display state
+33 −39
Original line number Diff line number Diff line
@@ -13,7 +13,6 @@
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <android-base/stringprintf.h>
#include <layerproto/LayerProtoParser.h>
#include <ui/DebugUtils.h>
@@ -24,7 +23,7 @@ using android::base::StringPrintf;
namespace android {
namespace surfaceflinger {

bool sortLayers(const LayerProtoParser::Layer* lhs, const LayerProtoParser::Layer* rhs) {
bool sortLayers(LayerProtoParser::Layer* lhs, const LayerProtoParser::Layer* rhs) {
    uint32_t ls = lhs->layerStack;
    uint32_t rs = rhs->layerStack;
    if (ls != rs) return ls < rs;
@@ -38,33 +37,30 @@ bool sortLayers(const LayerProtoParser::Layer* lhs, const LayerProtoParser::Laye
    return lhs->id < rhs->id;
}

bool sortLayerUniquePtrs(const std::unique_ptr<LayerProtoParser::Layer>& lhs,
                   const std::unique_ptr<LayerProtoParser::Layer>& rhs) {
    return sortLayers(lhs.get(), rhs.get());
}

const LayerProtoParser::LayerGlobal LayerProtoParser::generateLayerGlobalInfo(
        const LayersProto& layersProto) {
    return {{layersProto.resolution().w(), layersProto.resolution().h()}};
}

void LayerProtoParser::destroyLayerTree(
        const std::vector<const LayerProtoParser::Layer*>& layerTree) {
    for (auto layer : layerTree) {
        destroyLayerTree(layer->children);
        delete layer;
    }
}

std::vector<const LayerProtoParser::Layer*> LayerProtoParser::generateLayerTree(
std::vector<std::unique_ptr<LayerProtoParser::Layer>> LayerProtoParser::generateLayerTree(
        const LayersProto& layersProto) {
    auto layerMap = generateMap(layersProto);
    std::unordered_map<int32_t, LayerProtoParser::Layer*> layerMap = generateMap(layersProto);
    std::vector<std::unique_ptr<LayerProtoParser::Layer>> layers;

    std::vector<const Layer*> layers;
    std::for_each(layerMap.begin(), layerMap.end(),
                  [&](const std::pair<const int32_t, Layer*>& ref) {
                      if (ref.second->parent == nullptr) {
                          // only save top level layers
                          layers.push_back(ref.second);
    for (std::pair<int32_t, Layer*> kv : layerMap) {
        if (kv.second->parent == nullptr) {
            // Make unique_ptr for top level layers since they are not children. This ensures there
            // will only be one unique_ptr made for each layer.
            layers.push_back(std::unique_ptr<Layer>(kv.second));
        }
    }
                  });

    std::sort(layers.begin(), layers.end(), sortLayers);
    std::sort(layers.begin(), layers.end(), sortLayerUniquePtrs);
    return layers;
}

@@ -181,63 +177,61 @@ void LayerProtoParser::updateChildrenAndRelative(const LayerProto& layerProto,

    for (int i = 0; i < layerProto.children_size(); i++) {
        if (layerMap.count(layerProto.children(i)) > 0) {
            auto childLayer = layerMap[layerProto.children(i)];
            currLayer->children.push_back(childLayer);
            // Only make unique_ptrs for children since they are guaranteed to be unique, only one
            // parent per child. This ensures there will only be one unique_ptr made for each layer.
            currLayer->children.push_back(std::unique_ptr<Layer>(layerMap[layerProto.children(i)]));
        }
    }

    for (int i = 0; i < layerProto.relatives_size(); i++) {
        if (layerMap.count(layerProto.relatives(i)) > 0) {
            auto relativeLayer = layerMap[layerProto.relatives(i)];
            currLayer->relatives.push_back(relativeLayer);
            currLayer->relatives.push_back(layerMap[layerProto.relatives(i)]);
        }
    }

    if (layerProto.has_parent()) {
        if (layerMap.count(layerProto.parent()) > 0) {
            auto parentLayer = layerMap[layerProto.parent()];
            currLayer->parent = parentLayer;
            currLayer->parent = layerMap[layerProto.parent()];
        }
    }

    if (layerProto.has_z_order_relative_of()) {
        if (layerMap.count(layerProto.z_order_relative_of()) > 0) {
            auto relativeLayer = layerMap[layerProto.z_order_relative_of()];
            currLayer->zOrderRelativeOf = relativeLayer;
            currLayer->zOrderRelativeOf = layerMap[layerProto.z_order_relative_of()];
        }
    }
}

std::string LayerProtoParser::layersToString(
        const std::vector<const LayerProtoParser::Layer*> layers) {
        std::vector<std::unique_ptr<LayerProtoParser::Layer>> layers) {
    std::string result;
    for (const LayerProtoParser::Layer* layer : layers) {
    for (std::unique_ptr<LayerProtoParser::Layer>& layer : layers) {
        if (layer->zOrderRelativeOf != nullptr) {
            continue;
        }
        result.append(layerToString(layer).c_str());
        result.append(layerToString(layer.get()).c_str());
    }

    return result;
}

std::string LayerProtoParser::layerToString(const LayerProtoParser::Layer* layer) {
std::string LayerProtoParser::layerToString(LayerProtoParser::Layer* layer) {
    std::string result;

    std::vector<const Layer*> traverse(layer->relatives);
    for (const LayerProtoParser::Layer* child : layer->children) {
    std::vector<Layer*> traverse(layer->relatives);
    for (std::unique_ptr<LayerProtoParser::Layer>& child : layer->children) {
        if (child->zOrderRelativeOf != nullptr) {
            continue;
        }

        traverse.push_back(child);
        traverse.push_back(child.get());
    }

    std::sort(traverse.begin(), traverse.end(), sortLayers);

    size_t i = 0;
    for (; i < traverse.size(); i++) {
        const auto& relative = traverse[i];
        auto& relative = traverse[i];
        if (relative->z >= 0) {
            break;
        }
@@ -246,7 +240,7 @@ std::string LayerProtoParser::layerToString(const LayerProtoParser::Layer* layer
    result.append(layer->to_string().c_str());
    result.append("\n");
    for (; i < traverse.size(); i++) {
        const auto& relative = traverse[i];
        auto& relative = traverse[i];
        result.append(layerToString(relative).c_str());
    }

+6 −6
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@

#include <math/vec4.h>

#include <memory>
#include <unordered_map>
#include <vector>

@@ -79,8 +80,8 @@ public:
    public:
        int32_t id;
        std::string name;
        std::vector<const Layer*> children;
        std::vector<const Layer*> relatives;
        std::vector<std::unique_ptr<Layer>> children;
        std::vector<Layer*> relatives;
        std::string type;
        LayerProtoParser::Region transparentRegion;
        LayerProtoParser::Region visibleRegion;
@@ -119,9 +120,8 @@ public:
    };

    static const LayerGlobal generateLayerGlobalInfo(const LayersProto& layersProto);
    static std::vector<const Layer*> generateLayerTree(const LayersProto& layersProto);
    static void destroyLayerTree(const std::vector<const LayerProtoParser::Layer*>& layerTree);
    static std::string layersToString(const std::vector<const LayerProtoParser::Layer*> layers);
    static std::vector<std::unique_ptr<Layer>> generateLayerTree(const LayersProto& layersProto);
    static std::string layersToString(std::vector<std::unique_ptr<LayerProtoParser::Layer>> layers);

private:
    static std::unordered_map<int32_t, Layer*> generateMap(const LayersProto& layersProto);
@@ -135,7 +135,7 @@ private:
    static void updateChildrenAndRelative(const LayerProto& layerProto,
                                          std::unordered_map<int32_t, Layer*>& layerMap);

    static std::string layerToString(const LayerProtoParser::Layer* layer);
    static std::string layerToString(LayerProtoParser::Layer* layer);
};

} // namespace surfaceflinger
Loading