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

Commit 9fdb7e0c authored by Dan Stoza's avatar Dan Stoza
Browse files

SF: Add GL texture pool

Adds a pool of GL texture names.

Prior to this change, Layer creation was forced to run on the SF main
thread because it would need to call into RenderEngine to generate a new
texture name.

By creating a pool of pre-generated texture names, this operation no
longer needs to run on the main thread, which unblocks the rest of the
system during operations such as fingerprint unlock.

Bug: 110477323
Test: SurfaceFlinger_test + manual: examine systrace and observe that
      layer creation no longer blocks on access to the main thread

Change-Id: I9d68874d6c6f704c8884676454e84d916cd86507
Merged-In: I9d68874d6c6f704c8884676454e84d916cd86507
parent 63a2d825
Loading
Loading
Loading
Loading
+7 −3
Original line number Diff line number Diff line
@@ -63,7 +63,7 @@ BufferLayer::BufferLayer(SurfaceFlinger* flinger, const sp<Client>& client, cons
        mRefreshPending(false) {
    ALOGV("Creating Layer %s", name.string());

    mFlinger->getRenderEngine().genTextures(1, &mTextureName);
    mTextureName = mFlinger->getNewTexture();
    mTexture.init(Texture::TEXTURE_EXTERNAL, mTextureName);

    if (flags & ISurfaceComposerClient::eNonPremultiplied) mPremultipliedAlpha = false;
@@ -706,8 +706,12 @@ void BufferLayer::onFirstRef() {
    sp<IGraphicBufferConsumer> consumer;
    BufferQueue::createBufferQueue(&producer, &consumer, true);
    mProducer = new MonitoredProducer(producer, mFlinger, this);
    mConsumer = new BufferLayerConsumer(consumer,
            mFlinger->getRenderEngine(), mTextureName, this);
    {
        // Grab the SF state lock during this since it's the only safe way to access RenderEngine
        Mutex::Autolock lock(mFlinger->mStateLock);
        mConsumer = new BufferLayerConsumer(consumer, mFlinger->getRenderEngine(), mTextureName,
                                            this);
    }
    mConsumer->setConsumerUsageBits(getEffectiveUsage(0));
    mConsumer->setContentsChangedListener(this);
    mConsumer->setName(mName);
+2 −42
Original line number Diff line number Diff line
@@ -170,48 +170,8 @@ status_t Client::createSurface(
        }
    }

    /*
     * createSurface must be called from the GL thread so that it can
     * have access to the GL context.
     */
    class MessageCreateLayer : public MessageBase {
        SurfaceFlinger* flinger;
        Client* client;
        sp<IBinder>* handle;
        sp<IGraphicBufferProducer>* gbp;
        status_t result;
        const String8& name;
        uint32_t w, h;
        PixelFormat format;
        uint32_t flags;
        sp<Layer>* parent;
        int32_t windowType;
        int32_t ownerUid;
    public:
        MessageCreateLayer(SurfaceFlinger* flinger,
                const String8& name, Client* client,
                uint32_t w, uint32_t h, PixelFormat format, uint32_t flags,
                sp<IBinder>* handle, int32_t windowType, int32_t ownerUid,
                sp<IGraphicBufferProducer>* gbp,
                sp<Layer>* parent)
            : flinger(flinger), client(client),
              handle(handle), gbp(gbp), result(NO_ERROR),
              name(name), w(w), h(h), format(format), flags(flags),
              parent(parent), windowType(windowType), ownerUid(ownerUid) {
        }
        status_t getResult() const { return result; }
        virtual bool handler() {
            result = flinger->createLayer(name, client, w, h, format, flags,
                    windowType, ownerUid, handle, gbp, parent);
            return true;
        }
    };

    sp<MessageBase> msg = new MessageCreateLayer(mFlinger.get(),
            name, this, w, h, format, flags, handle,
            windowType, ownerUid, gbp, &parent);
    mFlinger->postMessageSync(msg);
    return static_cast<MessageCreateLayer*>( msg.get() )->getResult();
    return mFlinger->createLayer(name, this, w, h, format, flags, windowType,
                                 ownerUid, handle, gbp, &parent);
}

status_t Client::destroySurface(const sp<IBinder>& handle) {
+2 −0
Original line number Diff line number Diff line
@@ -35,6 +35,8 @@ public:
    bool isVisible() const override;

    void setPerFrameData(const sp<const DisplayDevice>& displayDevice) override;

    bool isCreatedFromMainThread() const override { return true; }
};

} // namespace android
+15 −6
Original line number Diff line number Diff line
@@ -130,18 +130,27 @@ Layer::Layer(SurfaceFlinger* flinger, const sp<Client>& client, const String8& n
    // drawing state & current state are identical
    mDrawingState = mCurrentState;

    const auto& hwc = flinger->getHwComposer();
    const auto& activeConfig = hwc.getActiveConfig(HWC_DISPLAY_PRIMARY);
    nsecs_t displayPeriod = activeConfig->getVsyncPeriod();
    mFrameTracker.setDisplayRefreshPeriod(displayPeriod);

    CompositorTiming compositorTiming;
    flinger->getCompositorTiming(&compositorTiming);
    mFrameEventHistory.initializeCompositorTiming(compositorTiming);

}

void Layer::onFirstRef() {}
void Layer::onFirstRef() NO_THREAD_SAFETY_ANALYSIS {
    if (!isCreatedFromMainThread()) {
        // Grab the SF state lock during this since it's the only way to safely access HWC
        mFlinger->mStateLock.lock();
    }

    const auto& hwc = mFlinger->getHwComposer();
    const auto& activeConfig = hwc.getActiveConfig(HWC_DISPLAY_PRIMARY);
    nsecs_t displayPeriod = activeConfig->getVsyncPeriod();
    mFrameTracker.setDisplayRefreshPeriod(displayPeriod);

    if (!isCreatedFromMainThread()) {
        mFlinger->mStateLock.unlock();
    }
}

Layer::~Layer() {
    sp<Client> c(mClientRef.promote());
+5 −0
Original line number Diff line number Diff line
@@ -362,6 +362,11 @@ public:
     */
    virtual bool isFixedSize() const { return true; }

    // Most layers aren't created from the main thread, and therefore need to
    // grab the SF state lock to access HWC, but ContainerLayer does, so we need
    // to avoid grabbing the lock again to avoid deadlock
    virtual bool isCreatedFromMainThread() const { return false; }


    bool isPendingRemoval() const { return mPendingRemoval; }

Loading