Loading include/gui/BufferQueue.h +74 −21 Original line number Diff line number Diff line Loading @@ -42,18 +42,57 @@ public: enum { NO_CONNECTED_API = 0 }; enum { INVALID_BUFFER_SLOT = -1 }; struct FrameAvailableListener : public virtual RefBase { // onFrameAvailable() is called from queueBuffer() each time an // additional frame becomes available for consumption. This means that // frames that are queued while in asynchronous mode only trigger the // callback if no previous frames are pending. Frames queued while in // synchronous mode always trigger the callback. // ConsumerListener is the interface through which the BufferQueue notifies // the consumer of events that the consumer may wish to react to. Because // the consumer will generally have a mutex that is locked during calls from // teh consumer to the BufferQueue, these calls from the BufferQueue to the // consumer *MUST* be called only when the BufferQueue mutex is NOT locked. struct ConsumerListener : public virtual RefBase { // onFrameAvailable is called from queueBuffer each time an additional // frame becomes available for consumption. This means that frames that // are queued while in asynchronous mode only trigger the callback if no // previous frames are pending. Frames queued while in synchronous mode // always trigger the callback. // // This is called without any lock held and can be called concurrently // by multiple threads. virtual void onFrameAvailable() = 0; // onBuffersReleased is called to notify the buffer consumer that the // BufferQueue has released its references to one or more GraphicBuffers // contained in its slots. The buffer consumer should then call // BufferQueue::getReleasedBuffers to retrieve the list of buffers // // This is called without any lock held and can be called concurrently // by multiple threads. virtual void onBuffersReleased() = 0; }; // ProxyConsumerListener is a ConsumerListener implementation that keeps a weak // reference to the actual consumer object. It forwards all calls to that // consumer object so long as it exists. // // This class exists to avoid having a circular reference between the // BufferQueue object and the consumer object. The reason this can't be a weak // reference in the BufferQueue class is because we're planning to expose the // consumer side of a BufferQueue as a binder interface, which doesn't support // weak references. class ProxyConsumerListener : public BufferQueue::ConsumerListener { public: ProxyConsumerListener(const wp<BufferQueue::ConsumerListener>& consumerListener); virtual ~ProxyConsumerListener(); virtual void onFrameAvailable(); virtual void onBuffersReleased(); private: // mConsumerListener is a weak reference to the ConsumerListener. This is // the raison d'etre of ProxyConsumerListener. wp<BufferQueue::ConsumerListener> mConsumerListener; }; // BufferQueue manages a pool of gralloc memory slots to be used // by producers and consumers. // allowSynchronousMode specifies whether or not synchronous mode can be Loading Loading @@ -168,21 +207,39 @@ public: // The following public functions is the consumer facing interface // acquire consumes a buffer by transferring its ownership to a consumer. // buffer contains the GraphicBuffer and its corresponding information. // buffer.mGraphicsBuffer will be NULL when the buffer has been already // acquired by the consumer. status_t acquire(BufferItem *buffer); // acquireBuffer attempts to acquire ownership of the next pending buffer in // the BufferQueue. If no buffer is pending then it returns -EINVAL. If a // buffer is successfully acquired, the information about the buffer is // returned in BufferItem. If the buffer returned had previously been // acquired then the BufferItem::mGraphicBuffer field of buffer is set to // NULL and it is assumed that the consumer still holds a reference to the // buffer. status_t acquireBuffer(BufferItem *buffer); // releaseBuffer releases a buffer slot from the consumer back to the // BufferQueue pending a fence sync. // // Note that the dependencies on EGL will be removed once we switch to using // the Android HW Sync HAL. status_t releaseBuffer(int buf, EGLDisplay display, EGLSyncKHR fence); // consumerConnect connects a consumer to the BufferQueue. Only one // consumer may be connected, and when that consumer disconnects the // BufferQueue is placed into the "abandoned" state, causing most // interactions with the BufferQueue by the producer to fail. status_t consumerConnect(const sp<ConsumerListener>& consumer); // consumerDisconnect disconnects a consumer from the BufferQueue. All // buffers will be freed. // buffers will be freed and the BufferQueue is placed in the "abandoned" // state, causing most interactions with the BufferQueue by the producer to // fail. status_t consumerDisconnect(); // getReleasedBuffers sets the value pointed to by slotMask to a bit mask // indicating which buffer slots the have been released by the BufferQueue // but have not yet been released by the consumer. status_t getReleasedBuffers(uint32_t* slotMask); // setDefaultBufferSize is used to set the size of buffers returned by // requestBuffers when a with and height of zero is requested. status_t setDefaultBufferSize(uint32_t w, uint32_t h); Loading @@ -199,10 +256,6 @@ public: // setConsumerName sets the name used in logging void setConsumerName(const String8& name); // setFrameAvailableListener sets the listener object that will be notified // when a new frame becomes available. void setFrameAvailableListener(const sp<FrameAvailableListener>& listener); // setDefaultBufferFormat allows the BufferQueue to create // GraphicBuffers of a defaultFormat if no format is specified // in dequeueBuffer Loading Loading @@ -384,10 +437,10 @@ private: // allocate new GraphicBuffer objects. sp<IGraphicBufferAlloc> mGraphicBufferAlloc; // mFrameAvailableListener is the listener object that will be called when a // new frame becomes available. If it is not NULL it will be called from // queueBuffer. sp<FrameAvailableListener> mFrameAvailableListener; // mConsumerListener is used to notify the connected consumer of // asynchronous events that it may wish to react to. It is initially set // to NULL and is written by consumerConnect and consumerDisconnect. sp<ConsumerListener> mConsumerListener; // mSynchronousMode whether we're in synchronous mode or not bool mSynchronousMode; Loading include/gui/SurfaceTexture.h +43 −11 Original line number Diff line number Diff line Loading @@ -39,12 +39,20 @@ namespace android { class String8; class SurfaceTexture : public virtual RefBase { class SurfaceTexture : public virtual RefBase, protected BufferQueue::ConsumerListener { public: // This typedef allows external code to continue referencing // SurfaceTexture::FrameAvailableListener during refactoring typedef BufferQueue::FrameAvailableListener FrameAvailableListener; struct FrameAvailableListener : public virtual RefBase { // onFrameAvailable() is called each time an additional frame becomes // available for consumption. This means that frames that are queued // while in asynchronous mode only trigger the callback if no previous // frames are pending. Frames queued while in synchronous mode always // trigger the callback. // // This is called without any lock held and can be called concurrently // by multiple threads. virtual void onFrameAvailable() = 0; }; // SurfaceTexture constructs a new SurfaceTexture object. tex indicates the // name of the OpenGL ES texture to which images are to be streamed. This Loading Loading @@ -175,6 +183,12 @@ public: protected: // Implementation of the BufferQueue::ConsumerListener interface. These // calls are used to notify the SurfaceTexture of asynchronous events in the // BufferQueue. virtual void onFrameAvailable(); virtual void onBuffersReleased(); static bool isExternalFormat(uint32_t format); private: Loading @@ -183,6 +197,13 @@ private: EGLImageKHR createImage(EGLDisplay dpy, const sp<GraphicBuffer>& graphicBuffer); // freeBufferLocked frees up the given buffer slot. If the slot has been // initialized this will release the reference to the GraphicBuffer in that // slot and destroy the EGLImage in that slot. Otherwise it has no effect. // // This method must be called with mMutex locked. void freeBufferLocked(int slotIndex); // computeCurrentTransformMatrix computes the transform matrix for the // current texture. It uses mCurrentTransform and the current GraphicBuffer // to compute this matrix and stores it in mCurrentTransformMatrix. Loading Loading @@ -234,8 +255,8 @@ private: // browser's tile cache exceeds. const GLenum mTexTarget; // SurfaceTexture maintains EGL information about GraphicBuffers that corresponds // directly with BufferQueue's buffers // EGLSlot contains the information and object references that // SurfaceTexture maintains about a BufferQueue buffer slot. struct EGLSlot { EGLSlot() : mEglImage(EGL_NO_IMAGE_KHR), Loading @@ -258,6 +279,13 @@ private: EGLSyncKHR mFence; }; // mEGLSlots stores the buffers that have been allocated by the BufferQueue // for each buffer slot. It is initialized to null pointers, and gets // filled in with the result of BufferQueue::acquire when the // client dequeues a buffer from a // slot that has not yet been used. The buffer allocated to a slot will also // be replaced if the requested buffer usage or geometry differs from that // of the buffer allocated to a slot. EGLSlot mEGLSlots[BufferQueue::NUM_BUFFER_SLOTS]; // mAbandoned indicates that the BufferQueue will no longer be used to Loading @@ -271,10 +299,10 @@ private: // It can be set by the setName method. String8 mName; // mMutex is the mutex used to prevent concurrent access to the member // variables of SurfaceTexture objects. It must be locked whenever the // member variables are accessed. mutable Mutex mMutex; // mFrameAvailableListener is the listener object that will be called when a // new frame becomes available. If it is not NULL it will be called from // queueBuffer. sp<FrameAvailableListener> mFrameAvailableListener; // mCurrentTexture is the buffer slot index of the buffer that is currently // bound to the OpenGL texture. It is initialized to INVALID_BUFFER_SLOT, Loading @@ -288,6 +316,10 @@ private: // if none is supplied sp<BufferQueue> mBufferQueue; // mMutex is the mutex used to prevent concurrent access to the member // variables of SurfaceTexture objects. It must be locked whenever the // member variables are accessed. mutable Mutex mMutex; }; // ---------------------------------------------------------------------------- Loading libs/gui/BufferQueue.cpp +113 −45 Original line number Diff line number Diff line Loading @@ -15,8 +15,8 @@ */ #define LOG_TAG "BufferQueue" //#define LOG_NDEBUG 0 #define ATRACE_TAG ATRACE_TAG_GRAPHICS //#define LOG_NDEBUG 0 #define GL_GLEXT_PROTOTYPES #define EGL_EGLEXT_PROTOTYPES Loading Loading @@ -146,13 +146,6 @@ void BufferQueue::setConsumerName(const String8& name) { mConsumerName = name; } void BufferQueue::setFrameAvailableListener( const sp<FrameAvailableListener>& listener) { ST_LOGV("setFrameAvailableListener"); Mutex::Autolock lock(mMutex); mFrameAvailableListener = listener; } status_t BufferQueue::setDefaultBufferFormat(uint32_t defaultFormat) { Mutex::Autolock lock(mMutex); mDefaultBufferFormat = defaultFormat; Loading Loading @@ -531,7 +524,7 @@ status_t BufferQueue::queueBuffer(int buf, int64_t timestamp, ST_LOGV("queueBuffer: slot=%d time=%lld", buf, timestamp); sp<FrameAvailableListener> listener; sp<ConsumerListener> listener; { // scope for the lock Mutex::Autolock lock(mMutex); Loading Loading @@ -559,7 +552,7 @@ status_t BufferQueue::queueBuffer(int buf, int64_t timestamp, // Synchronous mode always signals that an additional frame should // be consumed. listener = mFrameAvailableListener; listener = mConsumerListener; } else { // In asynchronous mode we only keep the most recent buffer. if (mQueue.empty()) { Loading @@ -568,7 +561,7 @@ status_t BufferQueue::queueBuffer(int buf, int64_t timestamp, // Asynchronous mode only signals that a frame should be // consumed if no previous frame was pending. If a frame were // pending then the consumer would have already been notified. listener = mFrameAvailableListener; listener = mConsumerListener; } else { Fifo::iterator front(mQueue.begin()); // buffer currently queued is freed Loading Loading @@ -682,6 +675,11 @@ status_t BufferQueue::connect(int api, return NO_INIT; } if (mConsumerListener == NULL) { ST_LOGE("connect: BufferQueue has no consumer!"); return NO_INIT; } int err = NO_ERROR; switch (api) { case NATIVE_WINDOW_API_EGL: Loading Loading @@ -712,6 +710,11 @@ status_t BufferQueue::connect(int api, status_t BufferQueue::disconnect(int api) { ATRACE_CALL(); ST_LOGV("disconnect: api=%d", api); int err = NO_ERROR; sp<ConsumerListener> listener; { // Scope for the lock Mutex::Autolock lock(mMutex); if (mAbandoned) { Loading @@ -720,7 +723,6 @@ status_t BufferQueue::disconnect(int api) { return NO_ERROR; } int err = NO_ERROR; switch (api) { case NATIVE_WINDOW_API_EGL: case NATIVE_WINDOW_API_CPU: Loading @@ -733,6 +735,7 @@ status_t BufferQueue::disconnect(int api) { mNextScalingMode = NATIVE_WINDOW_SCALING_MODE_FREEZE; mNextTransform = 0; mDequeueCondition.broadcast(); listener = mConsumerListener; } else { ST_LOGE("disconnect: connected to another api (cur=%d, req=%d)", mConnectedApi, api); Loading @@ -744,6 +747,12 @@ status_t BufferQueue::disconnect(int api) { err = -EINVAL; break; } } if (listener != NULL) { listener->onBuffersReleased(); } return err; } Loading Loading @@ -841,7 +850,7 @@ void BufferQueue::freeAllBuffersLocked() { } } status_t BufferQueue::acquire(BufferItem *buffer) { status_t BufferQueue::acquireBuffer(BufferItem *buffer) { ATRACE_CALL(); Mutex::Autolock _l(mMutex); // check if queue is empty Loading @@ -855,8 +864,7 @@ status_t BufferQueue::acquire(BufferItem *buffer) { if (mSlots[buf].mAcquireCalled) { buffer->mGraphicBuffer = NULL; } else { } else { buffer->mGraphicBuffer = mSlots[buf].mGraphicBuffer; } buffer->mCrop = mSlots[buf].mCrop; Loading @@ -872,8 +880,7 @@ status_t BufferQueue::acquire(BufferItem *buffer) { mDequeueCondition.broadcast(); ATRACE_INT(mConsumerName.string(), mQueue.size()); } else { } else { // should be a better return code? return -EINVAL; } Loading Loading @@ -907,17 +914,58 @@ status_t BufferQueue::releaseBuffer(int buf, EGLDisplay display, return OK; } status_t BufferQueue::consumerConnect(const sp<ConsumerListener>& consumerListener) { ST_LOGV("consumerConnect"); Mutex::Autolock lock(mMutex); if (mAbandoned) { ST_LOGE("consumerConnect: BufferQueue has been abandoned!"); return NO_INIT; } mConsumerListener = consumerListener; return OK; } status_t BufferQueue::consumerDisconnect() { ST_LOGV("consumerDisconnect"); Mutex::Autolock lock(mMutex); mAbandoned = true; if (mConsumerListener == NULL) { ST_LOGE("consumerDisconnect: No consumer is connected!"); return -EINVAL; } mAbandoned = true; mConsumerListener = NULL; mQueue.clear(); freeAllBuffersLocked(); mDequeueCondition.broadcast(); return OK; } status_t BufferQueue::getReleasedBuffers(uint32_t* slotMask) { ST_LOGV("getReleasedBuffers"); Mutex::Autolock lock(mMutex); if (mAbandoned) { ST_LOGE("getReleasedBuffers: BufferQueue has been abandoned!"); return NO_INIT; } uint32_t mask = 0; for (int i = 0; i < NUM_BUFFER_SLOTS; i++) { if (!mSlots[i].mAcquireCalled) { mask |= 1 << i; } } *slotMask = mask; ST_LOGV("getReleasedBuffers: returning mask %#x", mask); return NO_ERROR; } status_t BufferQueue::setDefaultBufferSize(uint32_t w, uint32_t h) { ST_LOGV("setDefaultBufferSize: w=%d, h=%d", w, h); Loading Loading @@ -982,4 +1030,24 @@ status_t BufferQueue::drainQueueAndFreeBuffersLocked() { return err; } BufferQueue::ProxyConsumerListener::ProxyConsumerListener( const wp<BufferQueue::ConsumerListener>& consumerListener): mConsumerListener(consumerListener) {} BufferQueue::ProxyConsumerListener::~ProxyConsumerListener() {} void BufferQueue::ProxyConsumerListener::onFrameAvailable() { sp<BufferQueue::ConsumerListener> listener(mConsumerListener.promote()); if (listener != NULL) { listener->onFrameAvailable(); } } void BufferQueue::ProxyConsumerListener::onBuffersReleased() { sp<BufferQueue::ConsumerListener> listener(mConsumerListener.promote()); if (listener != NULL) { listener->onBuffersReleased(); } } }; // namespace android libs/gui/SurfaceTexture.cpp +86 −23 Original line number Diff line number Diff line Loading @@ -122,17 +122,32 @@ SurfaceTexture::SurfaceTexture(GLuint tex, bool allowSynchronousMode, mName = String8::format("unnamed-%d-%d", getpid(), createProcessUniqueId()); ST_LOGV("SurfaceTexture"); if (bufferQueue == 0) { ST_LOGV("Creating a new BufferQueue"); mBufferQueue = new BufferQueue(allowSynchronousMode); } else { mBufferQueue = bufferQueue; } mBufferQueue->setConsumerName(mName); memcpy(mCurrentTransformMatrix, mtxIdentity, sizeof(mCurrentTransformMatrix)); // Note that we can't create an sp<...>(this) in a ctor that will not keep a // reference once the ctor ends, as that would cause the refcount of 'this' // dropping to 0 at the end of the ctor. Since all we need is a wp<...> // that's what we create. wp<BufferQueue::ConsumerListener> listener; sp<BufferQueue::ConsumerListener> proxy; listener = static_cast<BufferQueue::ConsumerListener*>(this); proxy = new BufferQueue::ProxyConsumerListener(listener); status_t err = mBufferQueue->consumerConnect(proxy); if (err != NO_ERROR) { ST_LOGE("SurfaceTexture: error connecting to BufferQueue: %s (%d)", strerror(-err), err); } else { mBufferQueue->setConsumerName(mName); } } SurfaceTexture::~SurfaceTexture() { Loading Loading @@ -167,7 +182,7 @@ status_t SurfaceTexture::updateTexImage() { // In asynchronous mode the list is guaranteed to be one buffer // deep, while in synchronous mode we use the oldest buffer. if (mBufferQueue->acquire(&item) == NO_ERROR) { if (mBufferQueue->acquireBuffer(&item) == NO_ERROR) { int buf = item.mBuf; // This buffer was newly allocated, so we need to clean up on our side if (item.mGraphicBuffer != NULL) { Loading Loading @@ -394,7 +409,7 @@ void SurfaceTexture::setFrameAvailableListener( const sp<FrameAvailableListener>& listener) { ST_LOGV("setFrameAvailableListener"); Mutex::Autolock lock(mMutex); mBufferQueue->setFrameAvailableListener(listener); mFrameAvailableListener = listener; } EGLImageKHR SurfaceTexture::createImage(EGLDisplay dpy, Loading Loading @@ -438,24 +453,36 @@ bool SurfaceTexture::isSynchronousMode() const { return mBufferQueue->isSynchronousMode(); } void SurfaceTexture::freeBufferLocked(int slotIndex) { ST_LOGV("freeBufferLocked: slotIndex=%d", slotIndex); mEGLSlots[slotIndex].mGraphicBuffer = 0; if (mEGLSlots[slotIndex].mEglImage != EGL_NO_IMAGE_KHR) { EGLImageKHR img = mEGLSlots[slotIndex].mEglImage; if (img != EGL_NO_IMAGE_KHR) { eglDestroyImageKHR(mEGLSlots[slotIndex].mEglDisplay, img); } mEGLSlots[slotIndex].mEglImage = EGL_NO_IMAGE_KHR; mEGLSlots[slotIndex].mEglDisplay = EGL_NO_DISPLAY; } } void SurfaceTexture::abandon() { ST_LOGV("abandon"); Mutex::Autolock lock(mMutex); if (!mAbandoned) { mAbandoned = true; mCurrentTextureBuf.clear(); // destroy all egl buffers for (int i =0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) { mEGLSlots[i].mGraphicBuffer = 0; if (mEGLSlots[i].mEglImage != EGL_NO_IMAGE_KHR) { eglDestroyImageKHR(mEGLSlots[i].mEglDisplay, mEGLSlots[i].mEglImage); mEGLSlots[i].mEglImage = EGL_NO_IMAGE_KHR; mEGLSlots[i].mEglDisplay = EGL_NO_DISPLAY; } freeBufferLocked(i); } // disconnect from the BufferQueue mBufferQueue->consumerDisconnect(); mBufferQueue.clear(); } } void SurfaceTexture::setName(const String8& name) { Loading Loading @@ -505,6 +532,40 @@ status_t SurfaceTexture::connect(int api, return mBufferQueue->connect(api, outWidth, outHeight, outTransform); } void SurfaceTexture::onFrameAvailable() { ST_LOGV("onFrameAvailable"); sp<FrameAvailableListener> listener; { // scope for the lock Mutex::Autolock lock(mMutex); listener = mFrameAvailableListener; } if (listener != NULL) { ST_LOGV("actually calling onFrameAvailable"); listener->onFrameAvailable(); } } void SurfaceTexture::onBuffersReleased() { ST_LOGV("onBuffersReleased"); Mutex::Autolock lock(mMutex); if (mAbandoned) { // Nothing to do if we're already abandoned. return; } uint32_t mask = 0; mBufferQueue->getReleasedBuffers(&mask); for (int i = 0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) { if (mask & (1 << i)) { freeBufferLocked(i); } } } void SurfaceTexture::dump(String8& result) const { char buffer[1024]; Loading @@ -515,20 +576,22 @@ void SurfaceTexture::dump(String8& result, const char* prefix, char* buffer, size_t SIZE) const { Mutex::Autolock _l(mMutex); snprintf(buffer, SIZE, "%smTexName=%d\n", prefix, mTexName); snprintf(buffer, SIZE, "%smTexName=%d, mAbandoned=%d\n", prefix, mTexName, int(mAbandoned)); result.append(buffer); snprintf(buffer, SIZE, "%snext : {crop=[%d,%d,%d,%d], transform=0x%02x, current=%d}\n" ,prefix, mCurrentCrop.left, "%snext : {crop=[%d,%d,%d,%d], transform=0x%02x, current=%d}\n", prefix, mCurrentCrop.left, mCurrentCrop.top, mCurrentCrop.right, mCurrentCrop.bottom, mCurrentTransform, mCurrentTexture ); result.append(buffer); if (!mAbandoned) { mBufferQueue->dump(result, prefix, buffer, SIZE); } } static void mtxMul(float out[16], const float a[16], const float b[16]) { out[0] = a[0]*b[0] + a[4]*b[1] + a[8]*b[2] + a[12]*b[3]; Loading libs/gui/tests/SurfaceTextureClient_test.cpp +19 −1 Original line number Diff line number Diff line Loading @@ -14,10 +14,14 @@ * limitations under the License. */ #define LOG_TAG "SurfaceTextureClient_test" //#define LOG_NDEBUG 0 #include <EGL/egl.h> #include <gtest/gtest.h> #include <gui/SurfaceTextureClient.h> #include <utils/threads.h> #include <utils/Log.h> #include <utils/Thread.h> namespace android { Loading @@ -30,6 +34,11 @@ protected: } virtual void SetUp() { const ::testing::TestInfo* const testInfo = ::testing::UnitTest::GetInstance()->current_test_info(); ALOGV("Begin test: %s.%s", testInfo->test_case_name(), testInfo->name()); mST = new SurfaceTexture(123); mSTC = new SurfaceTextureClient(mST); mANW = mSTC; Loading Loading @@ -76,6 +85,11 @@ protected: eglDestroyContext(mEglDisplay, mEglContext); eglDestroySurface(mEglDisplay, mEglSurface); eglTerminate(mEglDisplay); const ::testing::TestInfo* const testInfo = ::testing::UnitTest::GetInstance()->current_test_info(); ALOGV("End test: %s.%s", testInfo->test_case_name(), testInfo->name()); } virtual EGLint const* getConfigAttribs() { Loading Loading @@ -147,6 +161,10 @@ TEST_F(SurfaceTextureClientTest, EglCreateWindowSurfaceSucceeds) { EXPECT_NE(EGL_NO_SURFACE, eglSurface); EXPECT_EQ(EGL_SUCCESS, eglGetError()); if (eglSurface != EGL_NO_SURFACE) { eglDestroySurface(dpy, eglSurface); } eglTerminate(dpy); } Loading Loading
include/gui/BufferQueue.h +74 −21 Original line number Diff line number Diff line Loading @@ -42,18 +42,57 @@ public: enum { NO_CONNECTED_API = 0 }; enum { INVALID_BUFFER_SLOT = -1 }; struct FrameAvailableListener : public virtual RefBase { // onFrameAvailable() is called from queueBuffer() each time an // additional frame becomes available for consumption. This means that // frames that are queued while in asynchronous mode only trigger the // callback if no previous frames are pending. Frames queued while in // synchronous mode always trigger the callback. // ConsumerListener is the interface through which the BufferQueue notifies // the consumer of events that the consumer may wish to react to. Because // the consumer will generally have a mutex that is locked during calls from // teh consumer to the BufferQueue, these calls from the BufferQueue to the // consumer *MUST* be called only when the BufferQueue mutex is NOT locked. struct ConsumerListener : public virtual RefBase { // onFrameAvailable is called from queueBuffer each time an additional // frame becomes available for consumption. This means that frames that // are queued while in asynchronous mode only trigger the callback if no // previous frames are pending. Frames queued while in synchronous mode // always trigger the callback. // // This is called without any lock held and can be called concurrently // by multiple threads. virtual void onFrameAvailable() = 0; // onBuffersReleased is called to notify the buffer consumer that the // BufferQueue has released its references to one or more GraphicBuffers // contained in its slots. The buffer consumer should then call // BufferQueue::getReleasedBuffers to retrieve the list of buffers // // This is called without any lock held and can be called concurrently // by multiple threads. virtual void onBuffersReleased() = 0; }; // ProxyConsumerListener is a ConsumerListener implementation that keeps a weak // reference to the actual consumer object. It forwards all calls to that // consumer object so long as it exists. // // This class exists to avoid having a circular reference between the // BufferQueue object and the consumer object. The reason this can't be a weak // reference in the BufferQueue class is because we're planning to expose the // consumer side of a BufferQueue as a binder interface, which doesn't support // weak references. class ProxyConsumerListener : public BufferQueue::ConsumerListener { public: ProxyConsumerListener(const wp<BufferQueue::ConsumerListener>& consumerListener); virtual ~ProxyConsumerListener(); virtual void onFrameAvailable(); virtual void onBuffersReleased(); private: // mConsumerListener is a weak reference to the ConsumerListener. This is // the raison d'etre of ProxyConsumerListener. wp<BufferQueue::ConsumerListener> mConsumerListener; }; // BufferQueue manages a pool of gralloc memory slots to be used // by producers and consumers. // allowSynchronousMode specifies whether or not synchronous mode can be Loading Loading @@ -168,21 +207,39 @@ public: // The following public functions is the consumer facing interface // acquire consumes a buffer by transferring its ownership to a consumer. // buffer contains the GraphicBuffer and its corresponding information. // buffer.mGraphicsBuffer will be NULL when the buffer has been already // acquired by the consumer. status_t acquire(BufferItem *buffer); // acquireBuffer attempts to acquire ownership of the next pending buffer in // the BufferQueue. If no buffer is pending then it returns -EINVAL. If a // buffer is successfully acquired, the information about the buffer is // returned in BufferItem. If the buffer returned had previously been // acquired then the BufferItem::mGraphicBuffer field of buffer is set to // NULL and it is assumed that the consumer still holds a reference to the // buffer. status_t acquireBuffer(BufferItem *buffer); // releaseBuffer releases a buffer slot from the consumer back to the // BufferQueue pending a fence sync. // // Note that the dependencies on EGL will be removed once we switch to using // the Android HW Sync HAL. status_t releaseBuffer(int buf, EGLDisplay display, EGLSyncKHR fence); // consumerConnect connects a consumer to the BufferQueue. Only one // consumer may be connected, and when that consumer disconnects the // BufferQueue is placed into the "abandoned" state, causing most // interactions with the BufferQueue by the producer to fail. status_t consumerConnect(const sp<ConsumerListener>& consumer); // consumerDisconnect disconnects a consumer from the BufferQueue. All // buffers will be freed. // buffers will be freed and the BufferQueue is placed in the "abandoned" // state, causing most interactions with the BufferQueue by the producer to // fail. status_t consumerDisconnect(); // getReleasedBuffers sets the value pointed to by slotMask to a bit mask // indicating which buffer slots the have been released by the BufferQueue // but have not yet been released by the consumer. status_t getReleasedBuffers(uint32_t* slotMask); // setDefaultBufferSize is used to set the size of buffers returned by // requestBuffers when a with and height of zero is requested. status_t setDefaultBufferSize(uint32_t w, uint32_t h); Loading @@ -199,10 +256,6 @@ public: // setConsumerName sets the name used in logging void setConsumerName(const String8& name); // setFrameAvailableListener sets the listener object that will be notified // when a new frame becomes available. void setFrameAvailableListener(const sp<FrameAvailableListener>& listener); // setDefaultBufferFormat allows the BufferQueue to create // GraphicBuffers of a defaultFormat if no format is specified // in dequeueBuffer Loading Loading @@ -384,10 +437,10 @@ private: // allocate new GraphicBuffer objects. sp<IGraphicBufferAlloc> mGraphicBufferAlloc; // mFrameAvailableListener is the listener object that will be called when a // new frame becomes available. If it is not NULL it will be called from // queueBuffer. sp<FrameAvailableListener> mFrameAvailableListener; // mConsumerListener is used to notify the connected consumer of // asynchronous events that it may wish to react to. It is initially set // to NULL and is written by consumerConnect and consumerDisconnect. sp<ConsumerListener> mConsumerListener; // mSynchronousMode whether we're in synchronous mode or not bool mSynchronousMode; Loading
include/gui/SurfaceTexture.h +43 −11 Original line number Diff line number Diff line Loading @@ -39,12 +39,20 @@ namespace android { class String8; class SurfaceTexture : public virtual RefBase { class SurfaceTexture : public virtual RefBase, protected BufferQueue::ConsumerListener { public: // This typedef allows external code to continue referencing // SurfaceTexture::FrameAvailableListener during refactoring typedef BufferQueue::FrameAvailableListener FrameAvailableListener; struct FrameAvailableListener : public virtual RefBase { // onFrameAvailable() is called each time an additional frame becomes // available for consumption. This means that frames that are queued // while in asynchronous mode only trigger the callback if no previous // frames are pending. Frames queued while in synchronous mode always // trigger the callback. // // This is called without any lock held and can be called concurrently // by multiple threads. virtual void onFrameAvailable() = 0; }; // SurfaceTexture constructs a new SurfaceTexture object. tex indicates the // name of the OpenGL ES texture to which images are to be streamed. This Loading Loading @@ -175,6 +183,12 @@ public: protected: // Implementation of the BufferQueue::ConsumerListener interface. These // calls are used to notify the SurfaceTexture of asynchronous events in the // BufferQueue. virtual void onFrameAvailable(); virtual void onBuffersReleased(); static bool isExternalFormat(uint32_t format); private: Loading @@ -183,6 +197,13 @@ private: EGLImageKHR createImage(EGLDisplay dpy, const sp<GraphicBuffer>& graphicBuffer); // freeBufferLocked frees up the given buffer slot. If the slot has been // initialized this will release the reference to the GraphicBuffer in that // slot and destroy the EGLImage in that slot. Otherwise it has no effect. // // This method must be called with mMutex locked. void freeBufferLocked(int slotIndex); // computeCurrentTransformMatrix computes the transform matrix for the // current texture. It uses mCurrentTransform and the current GraphicBuffer // to compute this matrix and stores it in mCurrentTransformMatrix. Loading Loading @@ -234,8 +255,8 @@ private: // browser's tile cache exceeds. const GLenum mTexTarget; // SurfaceTexture maintains EGL information about GraphicBuffers that corresponds // directly with BufferQueue's buffers // EGLSlot contains the information and object references that // SurfaceTexture maintains about a BufferQueue buffer slot. struct EGLSlot { EGLSlot() : mEglImage(EGL_NO_IMAGE_KHR), Loading @@ -258,6 +279,13 @@ private: EGLSyncKHR mFence; }; // mEGLSlots stores the buffers that have been allocated by the BufferQueue // for each buffer slot. It is initialized to null pointers, and gets // filled in with the result of BufferQueue::acquire when the // client dequeues a buffer from a // slot that has not yet been used. The buffer allocated to a slot will also // be replaced if the requested buffer usage or geometry differs from that // of the buffer allocated to a slot. EGLSlot mEGLSlots[BufferQueue::NUM_BUFFER_SLOTS]; // mAbandoned indicates that the BufferQueue will no longer be used to Loading @@ -271,10 +299,10 @@ private: // It can be set by the setName method. String8 mName; // mMutex is the mutex used to prevent concurrent access to the member // variables of SurfaceTexture objects. It must be locked whenever the // member variables are accessed. mutable Mutex mMutex; // mFrameAvailableListener is the listener object that will be called when a // new frame becomes available. If it is not NULL it will be called from // queueBuffer. sp<FrameAvailableListener> mFrameAvailableListener; // mCurrentTexture is the buffer slot index of the buffer that is currently // bound to the OpenGL texture. It is initialized to INVALID_BUFFER_SLOT, Loading @@ -288,6 +316,10 @@ private: // if none is supplied sp<BufferQueue> mBufferQueue; // mMutex is the mutex used to prevent concurrent access to the member // variables of SurfaceTexture objects. It must be locked whenever the // member variables are accessed. mutable Mutex mMutex; }; // ---------------------------------------------------------------------------- Loading
libs/gui/BufferQueue.cpp +113 −45 Original line number Diff line number Diff line Loading @@ -15,8 +15,8 @@ */ #define LOG_TAG "BufferQueue" //#define LOG_NDEBUG 0 #define ATRACE_TAG ATRACE_TAG_GRAPHICS //#define LOG_NDEBUG 0 #define GL_GLEXT_PROTOTYPES #define EGL_EGLEXT_PROTOTYPES Loading Loading @@ -146,13 +146,6 @@ void BufferQueue::setConsumerName(const String8& name) { mConsumerName = name; } void BufferQueue::setFrameAvailableListener( const sp<FrameAvailableListener>& listener) { ST_LOGV("setFrameAvailableListener"); Mutex::Autolock lock(mMutex); mFrameAvailableListener = listener; } status_t BufferQueue::setDefaultBufferFormat(uint32_t defaultFormat) { Mutex::Autolock lock(mMutex); mDefaultBufferFormat = defaultFormat; Loading Loading @@ -531,7 +524,7 @@ status_t BufferQueue::queueBuffer(int buf, int64_t timestamp, ST_LOGV("queueBuffer: slot=%d time=%lld", buf, timestamp); sp<FrameAvailableListener> listener; sp<ConsumerListener> listener; { // scope for the lock Mutex::Autolock lock(mMutex); Loading Loading @@ -559,7 +552,7 @@ status_t BufferQueue::queueBuffer(int buf, int64_t timestamp, // Synchronous mode always signals that an additional frame should // be consumed. listener = mFrameAvailableListener; listener = mConsumerListener; } else { // In asynchronous mode we only keep the most recent buffer. if (mQueue.empty()) { Loading @@ -568,7 +561,7 @@ status_t BufferQueue::queueBuffer(int buf, int64_t timestamp, // Asynchronous mode only signals that a frame should be // consumed if no previous frame was pending. If a frame were // pending then the consumer would have already been notified. listener = mFrameAvailableListener; listener = mConsumerListener; } else { Fifo::iterator front(mQueue.begin()); // buffer currently queued is freed Loading Loading @@ -682,6 +675,11 @@ status_t BufferQueue::connect(int api, return NO_INIT; } if (mConsumerListener == NULL) { ST_LOGE("connect: BufferQueue has no consumer!"); return NO_INIT; } int err = NO_ERROR; switch (api) { case NATIVE_WINDOW_API_EGL: Loading Loading @@ -712,6 +710,11 @@ status_t BufferQueue::connect(int api, status_t BufferQueue::disconnect(int api) { ATRACE_CALL(); ST_LOGV("disconnect: api=%d", api); int err = NO_ERROR; sp<ConsumerListener> listener; { // Scope for the lock Mutex::Autolock lock(mMutex); if (mAbandoned) { Loading @@ -720,7 +723,6 @@ status_t BufferQueue::disconnect(int api) { return NO_ERROR; } int err = NO_ERROR; switch (api) { case NATIVE_WINDOW_API_EGL: case NATIVE_WINDOW_API_CPU: Loading @@ -733,6 +735,7 @@ status_t BufferQueue::disconnect(int api) { mNextScalingMode = NATIVE_WINDOW_SCALING_MODE_FREEZE; mNextTransform = 0; mDequeueCondition.broadcast(); listener = mConsumerListener; } else { ST_LOGE("disconnect: connected to another api (cur=%d, req=%d)", mConnectedApi, api); Loading @@ -744,6 +747,12 @@ status_t BufferQueue::disconnect(int api) { err = -EINVAL; break; } } if (listener != NULL) { listener->onBuffersReleased(); } return err; } Loading Loading @@ -841,7 +850,7 @@ void BufferQueue::freeAllBuffersLocked() { } } status_t BufferQueue::acquire(BufferItem *buffer) { status_t BufferQueue::acquireBuffer(BufferItem *buffer) { ATRACE_CALL(); Mutex::Autolock _l(mMutex); // check if queue is empty Loading @@ -855,8 +864,7 @@ status_t BufferQueue::acquire(BufferItem *buffer) { if (mSlots[buf].mAcquireCalled) { buffer->mGraphicBuffer = NULL; } else { } else { buffer->mGraphicBuffer = mSlots[buf].mGraphicBuffer; } buffer->mCrop = mSlots[buf].mCrop; Loading @@ -872,8 +880,7 @@ status_t BufferQueue::acquire(BufferItem *buffer) { mDequeueCondition.broadcast(); ATRACE_INT(mConsumerName.string(), mQueue.size()); } else { } else { // should be a better return code? return -EINVAL; } Loading Loading @@ -907,17 +914,58 @@ status_t BufferQueue::releaseBuffer(int buf, EGLDisplay display, return OK; } status_t BufferQueue::consumerConnect(const sp<ConsumerListener>& consumerListener) { ST_LOGV("consumerConnect"); Mutex::Autolock lock(mMutex); if (mAbandoned) { ST_LOGE("consumerConnect: BufferQueue has been abandoned!"); return NO_INIT; } mConsumerListener = consumerListener; return OK; } status_t BufferQueue::consumerDisconnect() { ST_LOGV("consumerDisconnect"); Mutex::Autolock lock(mMutex); mAbandoned = true; if (mConsumerListener == NULL) { ST_LOGE("consumerDisconnect: No consumer is connected!"); return -EINVAL; } mAbandoned = true; mConsumerListener = NULL; mQueue.clear(); freeAllBuffersLocked(); mDequeueCondition.broadcast(); return OK; } status_t BufferQueue::getReleasedBuffers(uint32_t* slotMask) { ST_LOGV("getReleasedBuffers"); Mutex::Autolock lock(mMutex); if (mAbandoned) { ST_LOGE("getReleasedBuffers: BufferQueue has been abandoned!"); return NO_INIT; } uint32_t mask = 0; for (int i = 0; i < NUM_BUFFER_SLOTS; i++) { if (!mSlots[i].mAcquireCalled) { mask |= 1 << i; } } *slotMask = mask; ST_LOGV("getReleasedBuffers: returning mask %#x", mask); return NO_ERROR; } status_t BufferQueue::setDefaultBufferSize(uint32_t w, uint32_t h) { ST_LOGV("setDefaultBufferSize: w=%d, h=%d", w, h); Loading Loading @@ -982,4 +1030,24 @@ status_t BufferQueue::drainQueueAndFreeBuffersLocked() { return err; } BufferQueue::ProxyConsumerListener::ProxyConsumerListener( const wp<BufferQueue::ConsumerListener>& consumerListener): mConsumerListener(consumerListener) {} BufferQueue::ProxyConsumerListener::~ProxyConsumerListener() {} void BufferQueue::ProxyConsumerListener::onFrameAvailable() { sp<BufferQueue::ConsumerListener> listener(mConsumerListener.promote()); if (listener != NULL) { listener->onFrameAvailable(); } } void BufferQueue::ProxyConsumerListener::onBuffersReleased() { sp<BufferQueue::ConsumerListener> listener(mConsumerListener.promote()); if (listener != NULL) { listener->onBuffersReleased(); } } }; // namespace android
libs/gui/SurfaceTexture.cpp +86 −23 Original line number Diff line number Diff line Loading @@ -122,17 +122,32 @@ SurfaceTexture::SurfaceTexture(GLuint tex, bool allowSynchronousMode, mName = String8::format("unnamed-%d-%d", getpid(), createProcessUniqueId()); ST_LOGV("SurfaceTexture"); if (bufferQueue == 0) { ST_LOGV("Creating a new BufferQueue"); mBufferQueue = new BufferQueue(allowSynchronousMode); } else { mBufferQueue = bufferQueue; } mBufferQueue->setConsumerName(mName); memcpy(mCurrentTransformMatrix, mtxIdentity, sizeof(mCurrentTransformMatrix)); // Note that we can't create an sp<...>(this) in a ctor that will not keep a // reference once the ctor ends, as that would cause the refcount of 'this' // dropping to 0 at the end of the ctor. Since all we need is a wp<...> // that's what we create. wp<BufferQueue::ConsumerListener> listener; sp<BufferQueue::ConsumerListener> proxy; listener = static_cast<BufferQueue::ConsumerListener*>(this); proxy = new BufferQueue::ProxyConsumerListener(listener); status_t err = mBufferQueue->consumerConnect(proxy); if (err != NO_ERROR) { ST_LOGE("SurfaceTexture: error connecting to BufferQueue: %s (%d)", strerror(-err), err); } else { mBufferQueue->setConsumerName(mName); } } SurfaceTexture::~SurfaceTexture() { Loading Loading @@ -167,7 +182,7 @@ status_t SurfaceTexture::updateTexImage() { // In asynchronous mode the list is guaranteed to be one buffer // deep, while in synchronous mode we use the oldest buffer. if (mBufferQueue->acquire(&item) == NO_ERROR) { if (mBufferQueue->acquireBuffer(&item) == NO_ERROR) { int buf = item.mBuf; // This buffer was newly allocated, so we need to clean up on our side if (item.mGraphicBuffer != NULL) { Loading Loading @@ -394,7 +409,7 @@ void SurfaceTexture::setFrameAvailableListener( const sp<FrameAvailableListener>& listener) { ST_LOGV("setFrameAvailableListener"); Mutex::Autolock lock(mMutex); mBufferQueue->setFrameAvailableListener(listener); mFrameAvailableListener = listener; } EGLImageKHR SurfaceTexture::createImage(EGLDisplay dpy, Loading Loading @@ -438,24 +453,36 @@ bool SurfaceTexture::isSynchronousMode() const { return mBufferQueue->isSynchronousMode(); } void SurfaceTexture::freeBufferLocked(int slotIndex) { ST_LOGV("freeBufferLocked: slotIndex=%d", slotIndex); mEGLSlots[slotIndex].mGraphicBuffer = 0; if (mEGLSlots[slotIndex].mEglImage != EGL_NO_IMAGE_KHR) { EGLImageKHR img = mEGLSlots[slotIndex].mEglImage; if (img != EGL_NO_IMAGE_KHR) { eglDestroyImageKHR(mEGLSlots[slotIndex].mEglDisplay, img); } mEGLSlots[slotIndex].mEglImage = EGL_NO_IMAGE_KHR; mEGLSlots[slotIndex].mEglDisplay = EGL_NO_DISPLAY; } } void SurfaceTexture::abandon() { ST_LOGV("abandon"); Mutex::Autolock lock(mMutex); if (!mAbandoned) { mAbandoned = true; mCurrentTextureBuf.clear(); // destroy all egl buffers for (int i =0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) { mEGLSlots[i].mGraphicBuffer = 0; if (mEGLSlots[i].mEglImage != EGL_NO_IMAGE_KHR) { eglDestroyImageKHR(mEGLSlots[i].mEglDisplay, mEGLSlots[i].mEglImage); mEGLSlots[i].mEglImage = EGL_NO_IMAGE_KHR; mEGLSlots[i].mEglDisplay = EGL_NO_DISPLAY; } freeBufferLocked(i); } // disconnect from the BufferQueue mBufferQueue->consumerDisconnect(); mBufferQueue.clear(); } } void SurfaceTexture::setName(const String8& name) { Loading Loading @@ -505,6 +532,40 @@ status_t SurfaceTexture::connect(int api, return mBufferQueue->connect(api, outWidth, outHeight, outTransform); } void SurfaceTexture::onFrameAvailable() { ST_LOGV("onFrameAvailable"); sp<FrameAvailableListener> listener; { // scope for the lock Mutex::Autolock lock(mMutex); listener = mFrameAvailableListener; } if (listener != NULL) { ST_LOGV("actually calling onFrameAvailable"); listener->onFrameAvailable(); } } void SurfaceTexture::onBuffersReleased() { ST_LOGV("onBuffersReleased"); Mutex::Autolock lock(mMutex); if (mAbandoned) { // Nothing to do if we're already abandoned. return; } uint32_t mask = 0; mBufferQueue->getReleasedBuffers(&mask); for (int i = 0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) { if (mask & (1 << i)) { freeBufferLocked(i); } } } void SurfaceTexture::dump(String8& result) const { char buffer[1024]; Loading @@ -515,20 +576,22 @@ void SurfaceTexture::dump(String8& result, const char* prefix, char* buffer, size_t SIZE) const { Mutex::Autolock _l(mMutex); snprintf(buffer, SIZE, "%smTexName=%d\n", prefix, mTexName); snprintf(buffer, SIZE, "%smTexName=%d, mAbandoned=%d\n", prefix, mTexName, int(mAbandoned)); result.append(buffer); snprintf(buffer, SIZE, "%snext : {crop=[%d,%d,%d,%d], transform=0x%02x, current=%d}\n" ,prefix, mCurrentCrop.left, "%snext : {crop=[%d,%d,%d,%d], transform=0x%02x, current=%d}\n", prefix, mCurrentCrop.left, mCurrentCrop.top, mCurrentCrop.right, mCurrentCrop.bottom, mCurrentTransform, mCurrentTexture ); result.append(buffer); if (!mAbandoned) { mBufferQueue->dump(result, prefix, buffer, SIZE); } } static void mtxMul(float out[16], const float a[16], const float b[16]) { out[0] = a[0]*b[0] + a[4]*b[1] + a[8]*b[2] + a[12]*b[3]; Loading
libs/gui/tests/SurfaceTextureClient_test.cpp +19 −1 Original line number Diff line number Diff line Loading @@ -14,10 +14,14 @@ * limitations under the License. */ #define LOG_TAG "SurfaceTextureClient_test" //#define LOG_NDEBUG 0 #include <EGL/egl.h> #include <gtest/gtest.h> #include <gui/SurfaceTextureClient.h> #include <utils/threads.h> #include <utils/Log.h> #include <utils/Thread.h> namespace android { Loading @@ -30,6 +34,11 @@ protected: } virtual void SetUp() { const ::testing::TestInfo* const testInfo = ::testing::UnitTest::GetInstance()->current_test_info(); ALOGV("Begin test: %s.%s", testInfo->test_case_name(), testInfo->name()); mST = new SurfaceTexture(123); mSTC = new SurfaceTextureClient(mST); mANW = mSTC; Loading Loading @@ -76,6 +85,11 @@ protected: eglDestroyContext(mEglDisplay, mEglContext); eglDestroySurface(mEglDisplay, mEglSurface); eglTerminate(mEglDisplay); const ::testing::TestInfo* const testInfo = ::testing::UnitTest::GetInstance()->current_test_info(); ALOGV("End test: %s.%s", testInfo->test_case_name(), testInfo->name()); } virtual EGLint const* getConfigAttribs() { Loading Loading @@ -147,6 +161,10 @@ TEST_F(SurfaceTextureClientTest, EglCreateWindowSurfaceSucceeds) { EXPECT_NE(EGL_NO_SURFACE, eglSurface); EXPECT_EQ(EGL_SUCCESS, eglGetError()); if (eglSurface != EGL_NO_SURFACE) { eglDestroySurface(dpy, eglSurface); } eglTerminate(dpy); } Loading