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

Commit 1926052e authored by Ady Abraham's avatar Ady Abraham
Browse files

libnativedisplay: Introduce SurfaceTextureListener

This is a tiny refactoting needed for the next CL.

Bug: 281695725
Test: manual using a test app
Change-Id: I0635325717073b528dadb712a843aabb4efddd3b
parent ab85f59f
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -200,6 +200,7 @@ aconfig_declarations {

cc_aconfig_library {
    name: "libguiflags",
    host_supported: true,
    vendor_available: true,
    aconfig_declarations: "libgui_flags",
}
+2 −0
Original line number Diff line number Diff line
@@ -73,6 +73,8 @@ cc_library_shared {
        "libGLESv2",
    ],

    static_libs: ["libguiflags"],

    export_header_lib_headers: ["jni_headers"],

    header_libs: [
+34 −0
Original line number Diff line number Diff line
@@ -290,6 +290,18 @@ public:
     */
    void releaseConsumerOwnership();

    /**
     * Interface for SurfaceTexture callback(s).
     */
    struct SurfaceTextureListener : public RefBase {
        virtual void onFrameAvailable(const BufferItem& item) = 0;
    };

    /**
     * setSurfaceTextureListener registers a SurfaceTextureListener.
     */
    void setSurfaceTextureListener(const sp<SurfaceTextureListener>&);

protected:
    /**
     * abandonLocked overrides the ConsumerBase method to clear
@@ -465,8 +477,30 @@ protected:
     */
    ImageConsumer mImageConsumer;

    /**
     * mSurfaceTextureListener holds the registered SurfaceTextureListener.
     * Note that SurfaceTexture holds the lister with an sp<>, which means that the listener
     * must only hold a wp<> to SurfaceTexture and not an sp<>.
     */
    sp<SurfaceTextureListener> mSurfaceTextureListener;

    friend class ImageConsumer;
    friend class EGLConsumer;

private:
    // Proxy listener to avoid having SurfaceTexture directly implement FrameAvailableListener as it
    // is extending ConsumerBase which also implements FrameAvailableListener.
    class FrameAvailableListenerProxy : public ConsumerBase::FrameAvailableListener {
    public:
        FrameAvailableListenerProxy(const wp<SurfaceTextureListener>& listener)
              : mSurfaceTextureListener(listener) {}

    private:
        void onFrameAvailable(const BufferItem& item) override;

        const wp<SurfaceTextureListener> mSurfaceTextureListener;
    };
    sp<FrameAvailableListenerProxy> mFrameAvailableListenerProxy;
};

// ----------------------------------------------------------------------------
+24 −0
Original line number Diff line number Diff line
@@ -23,6 +23,8 @@
#include <system/window.h>
#include <utils/Trace.h>

#include <com_android_graphics_libgui_flags.h>

namespace android {

// Macros for including the SurfaceTexture name in log messages
@@ -491,4 +493,26 @@ sp<GraphicBuffer> SurfaceTexture::dequeueBuffer(int* outSlotid, android_dataspac
    return buffer;
}

void SurfaceTexture::setSurfaceTextureListener(
        const sp<android::SurfaceTexture::SurfaceTextureListener>& listener) {
    SFT_LOGV("setSurfaceTextureListener");

    Mutex::Autolock _l(mMutex);
    mSurfaceTextureListener = listener;
    if (mSurfaceTextureListener != nullptr) {
        mFrameAvailableListenerProxy =
                sp<FrameAvailableListenerProxy>::make(mSurfaceTextureListener);
        setFrameAvailableListener(mFrameAvailableListenerProxy);
    } else {
        mFrameAvailableListenerProxy.clear();
    }
}

void SurfaceTexture::FrameAvailableListenerProxy::onFrameAvailable(const BufferItem& item) {
    const auto listener = mSurfaceTextureListener.promote();
    if (listener) {
        listener->onFrameAvailable(item);
    }
}

} // namespace android