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

Commit 84aefcf0 authored by Treehugger Robot's avatar Treehugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Use Layer IDs instead of handles in BufferCountTracker" into main

parents 054ce5cc cd391bc3
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -28,7 +28,7 @@ LayerHandle::LayerHandle(const sp<android::SurfaceFlinger>& flinger,

LayerHandle::~LayerHandle() {
    if (mFlinger) {
        mFlinger->onHandleDestroyed(this, mLayer, mLayerId);
        mFlinger->onHandleDestroyed(mLayer, mLayerId);
    }
}

+5 −5
Original line number Diff line number Diff line
@@ -4723,6 +4723,7 @@ status_t SurfaceFlinger::setTransactionState(
    for (auto& state : states) {
        resolvedStates.emplace_back(std::move(state));
        auto& resolvedState = resolvedStates.back();
        resolvedState.layerId = LayerHandle::getLayerId(resolvedState.state.surface);
        if (resolvedState.state.hasBufferChanges() && resolvedState.state.hasValidBuffer() &&
            resolvedState.state.surface) {
            sp<Layer> layer = LayerHandle::getLayer(resolvedState.state.surface);
@@ -4734,9 +4735,8 @@ status_t SurfaceFlinger::setTransactionState(
            if (resolvedState.externalTexture) {
                resolvedState.state.bufferData->buffer = resolvedState.externalTexture->getBuffer();
            }
            mBufferCountTracker.increment(resolvedState.state.surface->localBinder());
            mBufferCountTracker.increment(resolvedState.layerId);
        }
        resolvedState.layerId = LayerHandle::getLayerId(resolvedState.state.surface);
        if (resolvedState.state.what & layer_state_t::eReparent) {
            resolvedState.parentId =
                    getLayerIdFromSurfaceControl(resolvedState.state.parentSurfaceControlForChild);
@@ -5179,7 +5179,7 @@ status_t SurfaceFlinger::createLayer(LayerCreationArgs& args, gui::CreateSurface
            std::atomic<int32_t>* pendingBufferCounter = layer->getPendingBufferCounter();
            if (pendingBufferCounter) {
                std::string counterName = layer->getPendingBufferCounterName();
                mBufferCountTracker.add(outResult.handle->localBinder(), counterName,
                mBufferCountTracker.add(LayerHandle::getLayerId(outResult.handle), counterName,
                                        pendingBufferCounter);
            }
        } break;
@@ -5227,7 +5227,7 @@ status_t SurfaceFlinger::createEffectLayer(const LayerCreationArgs& args, sp<IBi
    return NO_ERROR;
}

void SurfaceFlinger::onHandleDestroyed(BBinder* handle, sp<Layer>& layer, uint32_t layerId) {
void SurfaceFlinger::onHandleDestroyed(sp<Layer>& layer, uint32_t layerId) {
    {
        // Used to remove stalled transactions which uses an internal lock.
        ftl::FakeGuard guard(kMainThreadContext);
@@ -5240,7 +5240,7 @@ void SurfaceFlinger::onHandleDestroyed(BBinder* handle, sp<Layer>& layer, uint32

    Mutex::Autolock stateLock(mStateLock);
    layer->onHandleDestroyed();
    mBufferCountTracker.remove(handle);
    mBufferCountTracker.remove(layerId);
    layer.clear();
    setTransactionFlags(eTransactionFlushNeeded | eTransactionNeeded);
}
+11 −11
Original line number Diff line number Diff line
@@ -296,7 +296,7 @@ public:
    // Called when all clients have released all their references to
    // this layer. The layer may still be kept alive by its parents but
    // the client can no longer modify this layer directly.
    void onHandleDestroyed(BBinder* handle, sp<Layer>& layer, uint32_t layerId);
    void onHandleDestroyed(sp<Layer>& layer, uint32_t layerId);

    TransactionCallbackInvoker& getTransactionCallbackInvoker() {
        return mTransactionCallbackInvoker;
@@ -433,32 +433,32 @@ private:
    // This is done to avoid lock contention with the main thread.
    class BufferCountTracker {
    public:
        void increment(BBinder* layerHandle) {
        void increment(uint32_t layerId) {
            std::lock_guard<std::mutex> lock(mLock);
            auto it = mCounterByLayerHandle.find(layerHandle);
            if (it != mCounterByLayerHandle.end()) {
            auto it = mCounterByLayerId.find(layerId);
            if (it != mCounterByLayerId.end()) {
                auto [name, pendingBuffers] = it->second;
                int32_t count = ++(*pendingBuffers);
                SFTRACE_INT(name.c_str(), count);
            } else {
                ALOGW("Handle not found! %p", layerHandle);
                ALOGW("Layer ID not found! %d", layerId);
            }
        }

        void add(BBinder* layerHandle, const std::string& name, std::atomic<int32_t>* counter) {
        void add(uint32_t layerId, const std::string& name, std::atomic<int32_t>* counter) {
            std::lock_guard<std::mutex> lock(mLock);
            mCounterByLayerHandle[layerHandle] = std::make_pair(name, counter);
            mCounterByLayerId[layerId] = std::make_pair(name, counter);
        }

        void remove(BBinder* layerHandle) {
        void remove(uint32_t layerId) {
            std::lock_guard<std::mutex> lock(mLock);
            mCounterByLayerHandle.erase(layerHandle);
            mCounterByLayerId.erase(layerId);
        }

    private:
        std::mutex mLock;
        std::unordered_map<BBinder*, std::pair<std::string, std::atomic<int32_t>*>>
                mCounterByLayerHandle GUARDED_BY(mLock);
        std::unordered_map<uint32_t, std::pair<std::string, std::atomic<int32_t>*>>
                mCounterByLayerId GUARDED_BY(mLock);
    };

    enum class BootStage {
+1 −1
Original line number Diff line number Diff line
@@ -637,7 +637,7 @@ public:
    void destroyAllLayerHandles() {
        ftl::FakeGuard guard(kMainThreadContext);
        for (auto [layerId, legacyLayer] : mFlinger->mLegacyLayers) {
            mFlinger->onHandleDestroyed(nullptr, legacyLayer, layerId);
            mFlinger->onHandleDestroyed(legacyLayer, layerId);
        }
    }