michael@0: /* michael@0: * Copyright (C) 2012 The Android Open Source Project michael@0: * Copyright (C) 2013 Mozilla Foundation michael@0: * michael@0: * Licensed under the Apache License, Version 2.0 (the "License"); michael@0: * you may not use this file except in compliance with the License. michael@0: * You may obtain a copy of the License at michael@0: * michael@0: * http://www.apache.org/licenses/LICENSE-2.0 michael@0: * michael@0: * Unless required by applicable law or agreed to in writing, software michael@0: * distributed under the License is distributed on an "AS IS" BASIS, michael@0: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. michael@0: * See the License for the specific language governing permissions and michael@0: * limitations under the License. michael@0: */ michael@0: michael@0: #define LOG_TAG "GonkBufferQueue" michael@0: #define ATRACE_TAG ATRACE_TAG_GRAPHICS michael@0: #define LOG_NDEBUG 0 michael@0: michael@0: #define GL_GLEXT_PROTOTYPES michael@0: #define EGL_EGLEXT_PROTOTYPES michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: #include "mozilla/layers/GrallocTextureClient.h" michael@0: #include "mozilla/layers/ImageBridgeChild.h" michael@0: #include "GonkBufferQueueKK.h" michael@0: michael@0: // Macros for including the GonkBufferQueue name in log messages michael@0: #define ST_LOGV(...) __android_log_print(ANDROID_LOG_VERBOSE, LOG_TAG, __VA_ARGS__) michael@0: #define ST_LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__) michael@0: #define ST_LOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__) michael@0: #define ST_LOGW(...) __android_log_print(ANDROID_LOG_WARN, LOG_TAG, __VA_ARGS__) michael@0: #define ST_LOGE(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__) michael@0: michael@0: #define ATRACE_BUFFER_INDEX(index) michael@0: michael@0: using namespace mozilla; michael@0: using namespace mozilla::gfx; michael@0: using namespace mozilla::layers; michael@0: michael@0: namespace android { michael@0: michael@0: // Get an ID that's unique within this process. michael@0: static int32_t createProcessUniqueId() { michael@0: static volatile int32_t globalCounter = 0; michael@0: return android_atomic_inc(&globalCounter); michael@0: } michael@0: michael@0: static const char* scalingModeName(int scalingMode) { michael@0: switch (scalingMode) { michael@0: case NATIVE_WINDOW_SCALING_MODE_FREEZE: return "FREEZE"; michael@0: case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW: return "SCALE_TO_WINDOW"; michael@0: case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP: return "SCALE_CROP"; michael@0: default: return "Unknown"; michael@0: } michael@0: } michael@0: michael@0: class nsProxyReleaseTask : public Task michael@0: { michael@0: public: michael@0: nsProxyReleaseTask(TextureClient* aClient) michael@0: : mTextureClient(aClient) { michael@0: } michael@0: michael@0: virtual void Run() MOZ_OVERRIDE michael@0: { michael@0: mTextureClient = nullptr; michael@0: } michael@0: michael@0: private: michael@0: mozilla::RefPtr mTextureClient; michael@0: }; michael@0: michael@0: GonkBufferQueue::GonkBufferQueue(bool allowSynchronousMode, michael@0: const sp& allocator) : michael@0: mDefaultWidth(1), michael@0: mDefaultHeight(1), michael@0: mMaxAcquiredBufferCount(1), michael@0: mDefaultMaxBufferCount(2), michael@0: mOverrideMaxBufferCount(0), michael@0: // mSynchronousMode(true), // GonkBufferQueue always works in sync mode. michael@0: mConsumerControlledByApp(false), michael@0: mDequeueBufferCannotBlock(false), michael@0: mUseAsyncBuffer(true), michael@0: mConnectedApi(NO_CONNECTED_API), michael@0: mAbandoned(false), michael@0: mFrameCounter(0), michael@0: mBufferHasBeenQueued(false), michael@0: mDefaultBufferFormat(PIXEL_FORMAT_RGBA_8888), michael@0: mConsumerUsageBits(0), michael@0: mTransformHint(0) michael@0: { michael@0: // Choose a name using the PID and a process-unique ID. michael@0: mConsumerName = String8::format("unnamed-%d-%d", getpid(), createProcessUniqueId()); michael@0: michael@0: ST_LOGV("GonkBufferQueue"); michael@0: } michael@0: michael@0: GonkBufferQueue::~GonkBufferQueue() { michael@0: ST_LOGV("~GonkBufferQueue"); michael@0: } michael@0: michael@0: status_t GonkBufferQueue::setDefaultMaxBufferCountLocked(int count) { michael@0: if (count < 2 || count > NUM_BUFFER_SLOTS) michael@0: return BAD_VALUE; michael@0: michael@0: mDefaultMaxBufferCount = count; michael@0: mDequeueCondition.broadcast(); michael@0: michael@0: return NO_ERROR; michael@0: } michael@0: michael@0: void GonkBufferQueue::setConsumerName(const String8& name) { michael@0: Mutex::Autolock lock(mMutex); michael@0: mConsumerName = name; michael@0: } michael@0: michael@0: status_t GonkBufferQueue::setDefaultBufferFormat(uint32_t defaultFormat) { michael@0: Mutex::Autolock lock(mMutex); michael@0: mDefaultBufferFormat = defaultFormat; michael@0: return NO_ERROR; michael@0: } michael@0: michael@0: status_t GonkBufferQueue::setConsumerUsageBits(uint32_t usage) { michael@0: Mutex::Autolock lock(mMutex); michael@0: mConsumerUsageBits = usage; michael@0: return NO_ERROR; michael@0: } michael@0: michael@0: status_t GonkBufferQueue::setTransformHint(uint32_t hint) { michael@0: ST_LOGV("setTransformHint: %02x", hint); michael@0: Mutex::Autolock lock(mMutex); michael@0: mTransformHint = hint; michael@0: return NO_ERROR; michael@0: } michael@0: michael@0: TemporaryRef michael@0: GonkBufferQueue::getTextureClientFromBuffer(ANativeWindowBuffer* buffer) michael@0: { michael@0: Mutex::Autolock _l(mMutex); michael@0: if (buffer == NULL) { michael@0: ST_LOGE("getSlotFromBufferLocked: encountered NULL buffer"); michael@0: return nullptr; michael@0: } michael@0: michael@0: for (int i = 0; i < NUM_BUFFER_SLOTS; i++) { michael@0: if (mSlots[i].mGraphicBuffer != NULL && mSlots[i].mGraphicBuffer->handle == buffer->handle) { michael@0: return mSlots[i].mTextureClient; michael@0: } michael@0: } michael@0: ST_LOGE("getSlotFromBufferLocked: unknown buffer: %p", buffer->handle); michael@0: return nullptr; michael@0: } michael@0: michael@0: int GonkBufferQueue::getSlotFromTextureClientLocked( michael@0: TextureClient* client) const michael@0: { michael@0: if (client == NULL) { michael@0: ST_LOGE("getSlotFromBufferLocked: encountered NULL buffer"); michael@0: return BAD_VALUE; michael@0: } michael@0: michael@0: for (int i = 0; i < NUM_BUFFER_SLOTS; i++) { michael@0: if (mSlots[i].mTextureClient == client) { michael@0: return i; michael@0: } michael@0: } michael@0: ST_LOGE("getSlotFromBufferLocked: unknown TextureClient: %p", client); michael@0: return BAD_VALUE; michael@0: } michael@0: michael@0: status_t GonkBufferQueue::setBufferCount(int bufferCount) { michael@0: ST_LOGV("setBufferCount: count=%d", bufferCount); michael@0: michael@0: sp listener; michael@0: { michael@0: Mutex::Autolock lock(mMutex); michael@0: michael@0: if (mAbandoned) { michael@0: ST_LOGE("setBufferCount: GonkBufferQueue has been abandoned!"); michael@0: return NO_INIT; michael@0: } michael@0: if (bufferCount > NUM_BUFFER_SLOTS) { michael@0: ST_LOGE("setBufferCount: bufferCount too large (max %d)", michael@0: NUM_BUFFER_SLOTS); michael@0: return BAD_VALUE; michael@0: } michael@0: michael@0: // Error out if the user has dequeued buffers michael@0: for (int i=0 ; ionBuffersReleased(); michael@0: } michael@0: michael@0: return NO_ERROR; michael@0: } michael@0: michael@0: int GonkBufferQueue::query(int what, int* outValue) michael@0: { michael@0: ATRACE_CALL(); michael@0: Mutex::Autolock lock(mMutex); michael@0: michael@0: if (mAbandoned) { michael@0: ST_LOGE("query: GonkBufferQueue has been abandoned!"); michael@0: return NO_INIT; michael@0: } michael@0: michael@0: int value; michael@0: switch (what) { michael@0: case NATIVE_WINDOW_WIDTH: michael@0: value = mDefaultWidth; michael@0: break; michael@0: case NATIVE_WINDOW_HEIGHT: michael@0: value = mDefaultHeight; michael@0: break; michael@0: case NATIVE_WINDOW_FORMAT: michael@0: value = mDefaultBufferFormat; michael@0: break; michael@0: case NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS: michael@0: value = getMinUndequeuedBufferCount(false); michael@0: break; michael@0: case NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND: michael@0: value = (mQueue.size() >= 2); michael@0: break; michael@0: case NATIVE_WINDOW_CONSUMER_USAGE_BITS: michael@0: value = mConsumerUsageBits; michael@0: break; michael@0: default: michael@0: return BAD_VALUE; michael@0: } michael@0: outValue[0] = value; michael@0: return NO_ERROR; michael@0: } michael@0: michael@0: status_t GonkBufferQueue::requestBuffer(int slot, sp* buf) { michael@0: ATRACE_CALL(); michael@0: ST_LOGV("requestBuffer: slot=%d", slot); michael@0: Mutex::Autolock lock(mMutex); michael@0: if (mAbandoned) { michael@0: ST_LOGE("requestBuffer: GonkBufferQueue has been abandoned!"); michael@0: return NO_INIT; michael@0: } michael@0: if (slot < 0 || slot >= NUM_BUFFER_SLOTS) { michael@0: ST_LOGE("requestBuffer: slot index out of range [0, %d]: %d", michael@0: NUM_BUFFER_SLOTS, slot); michael@0: return BAD_VALUE; michael@0: } else if (mSlots[slot].mBufferState != BufferSlot::DEQUEUED) { michael@0: ST_LOGE("requestBuffer: slot %d is not owned by the client (state=%d)", michael@0: slot, mSlots[slot].mBufferState); michael@0: return BAD_VALUE; michael@0: } michael@0: mSlots[slot].mRequestBufferCalled = true; michael@0: *buf = mSlots[slot].mGraphicBuffer; michael@0: return NO_ERROR; michael@0: } michael@0: michael@0: status_t GonkBufferQueue::dequeueBuffer(int *outBuf, sp* outFence, bool async, michael@0: uint32_t w, uint32_t h, uint32_t format, uint32_t usage) { michael@0: ATRACE_CALL(); michael@0: ST_LOGV("dequeueBuffer: w=%d h=%d fmt=%#x usage=%#x", w, h, format, usage); michael@0: michael@0: if ((w && !h) || (!w && h)) { michael@0: ST_LOGE("dequeueBuffer: invalid size: w=%u, h=%u", w, h); michael@0: return BAD_VALUE; michael@0: } michael@0: michael@0: status_t returnFlags(OK); michael@0: int buf = INVALID_BUFFER_SLOT; michael@0: michael@0: { // Scope for the lock michael@0: Mutex::Autolock lock(mMutex); michael@0: michael@0: if (format == 0) { michael@0: format = mDefaultBufferFormat; michael@0: } michael@0: // turn on usage bits the consumer requested michael@0: usage |= mConsumerUsageBits; michael@0: michael@0: int found = -1; michael@0: bool tryAgain = true; michael@0: while (tryAgain) { michael@0: if (mAbandoned) { michael@0: ST_LOGE("dequeueBuffer: GonkBufferQueue has been abandoned!"); michael@0: return NO_INIT; michael@0: } michael@0: michael@0: const int maxBufferCount = getMaxBufferCountLocked(async); michael@0: if (async && mOverrideMaxBufferCount) { michael@0: // FIXME: some drivers are manually setting the buffer-count (which they michael@0: // shouldn't), so we do this extra test here to handle that case. michael@0: // This is TEMPORARY, until we get this fixed. michael@0: if (mOverrideMaxBufferCount < maxBufferCount) { michael@0: ST_LOGE("dequeueBuffer: async mode is invalid with buffercount override"); michael@0: return BAD_VALUE; michael@0: } michael@0: } michael@0: michael@0: // Free up any buffers that are in slots beyond the max buffer michael@0: // count. michael@0: //for (int i = maxBufferCount; i < NUM_BUFFER_SLOTS; i++) { michael@0: // assert(mSlots[i].mBufferState == BufferSlot::FREE); michael@0: // if (mSlots[i].mGraphicBuffer != NULL) { michael@0: // freeBufferLocked(i); michael@0: // returnFlags |= IGraphicBufferProducer::RELEASE_ALL_BUFFERS; michael@0: // } michael@0: //} michael@0: michael@0: // look for a free buffer to give to the client michael@0: found = INVALID_BUFFER_SLOT; michael@0: int dequeuedCount = 0; michael@0: int acquiredCount = 0; michael@0: for (int i = 0; i < maxBufferCount; i++) { michael@0: const int state = mSlots[i].mBufferState; michael@0: switch (state) { michael@0: case BufferSlot::DEQUEUED: michael@0: dequeuedCount++; michael@0: break; michael@0: case BufferSlot::ACQUIRED: michael@0: acquiredCount++; michael@0: break; michael@0: case BufferSlot::FREE: michael@0: /* We return the oldest of the free buffers to avoid michael@0: * stalling the producer if possible. This is because michael@0: * the consumer may still have pending reads of the michael@0: * buffers in flight. michael@0: */ michael@0: if ((found < 0) || michael@0: mSlots[i].mFrameNumber < mSlots[found].mFrameNumber) { michael@0: found = i; michael@0: } michael@0: break; michael@0: } michael@0: } michael@0: michael@0: // clients are not allowed to dequeue more than one buffer michael@0: // if they didn't set a buffer count. michael@0: if (!mOverrideMaxBufferCount && dequeuedCount) { michael@0: ST_LOGE("dequeueBuffer: can't dequeue multiple buffers without " michael@0: "setting the buffer count"); michael@0: return -EINVAL; michael@0: } michael@0: michael@0: // See whether a buffer has been queued since the last michael@0: // setBufferCount so we know whether to perform the min undequeued michael@0: // buffers check below. michael@0: if (mBufferHasBeenQueued) { michael@0: // make sure the client is not trying to dequeue more buffers michael@0: // than allowed. michael@0: const int newUndequeuedCount = maxBufferCount - (dequeuedCount+1); michael@0: const int minUndequeuedCount = getMinUndequeuedBufferCount(async); michael@0: if (newUndequeuedCount < minUndequeuedCount) { michael@0: ST_LOGE("dequeueBuffer: min undequeued buffer count (%d) " michael@0: "exceeded (dequeued=%d undequeudCount=%d)", michael@0: minUndequeuedCount, dequeuedCount, michael@0: newUndequeuedCount); michael@0: return -EBUSY; michael@0: } michael@0: } michael@0: michael@0: // If no buffer is found, wait for a buffer to be released or for michael@0: // the max buffer count to change. michael@0: tryAgain = found == INVALID_BUFFER_SLOT; michael@0: if (tryAgain) { michael@0: // return an error if we're in "cannot block" mode (producer and consumer michael@0: // are controlled by the application) -- however, the consumer is allowed michael@0: // to acquire briefly an extra buffer (which could cause us to have to wait here) michael@0: // and that's okay because we know the wait will be brief (it happens michael@0: // if we dequeue a buffer while the consumer has acquired one but not released michael@0: // the old one yet -- for e.g.: see GLConsumer::updateTexImage()). michael@0: if (mDequeueBufferCannotBlock && (acquiredCount <= mMaxAcquiredBufferCount)) { michael@0: ST_LOGE("dequeueBuffer: would block! returning an error instead."); michael@0: return WOULD_BLOCK; michael@0: } michael@0: mDequeueCondition.wait(mMutex); michael@0: } michael@0: } michael@0: michael@0: michael@0: if (found == INVALID_BUFFER_SLOT) { michael@0: // This should not happen. michael@0: ST_LOGE("dequeueBuffer: no available buffer slots"); michael@0: return -EBUSY; michael@0: } michael@0: michael@0: buf = found; michael@0: *outBuf = found; michael@0: michael@0: const bool useDefaultSize = !w && !h; michael@0: if (useDefaultSize) { michael@0: // use the default size michael@0: w = mDefaultWidth; michael@0: h = mDefaultHeight; michael@0: } michael@0: michael@0: mSlots[buf].mBufferState = BufferSlot::DEQUEUED; michael@0: michael@0: const sp& buffer(mSlots[buf].mGraphicBuffer); michael@0: if ((buffer == NULL) || michael@0: (uint32_t(buffer->width) != w) || michael@0: (uint32_t(buffer->height) != h) || michael@0: (uint32_t(buffer->format) != format) || michael@0: ((uint32_t(buffer->usage) & usage) != usage)) michael@0: { michael@0: mSlots[buf].mAcquireCalled = false; michael@0: mSlots[buf].mGraphicBuffer = NULL; michael@0: mSlots[buf].mRequestBufferCalled = false; michael@0: mSlots[buf].mFence = Fence::NO_FENCE; michael@0: if (mSlots[buf].mTextureClient) { michael@0: mSlots[buf].mTextureClient->ClearRecycleCallback(); michael@0: // release TextureClient in ImageBridge thread michael@0: nsProxyReleaseTask* task = new nsProxyReleaseTask(mSlots[buf].mTextureClient); michael@0: mSlots[buf].mTextureClient = NULL; michael@0: ImageBridgeChild::GetSingleton()->GetMessageLoop()->PostTask(FROM_HERE, task); michael@0: } michael@0: returnFlags |= IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION; michael@0: } michael@0: michael@0: michael@0: if (CC_UNLIKELY(mSlots[buf].mFence == NULL)) { michael@0: ST_LOGE("dequeueBuffer: about to return a NULL fence from mSlot. " michael@0: "buf=%d, w=%d, h=%d, format=%d", michael@0: buf, buffer->width, buffer->height, buffer->format); michael@0: } michael@0: *outFence = mSlots[buf].mFence; michael@0: mSlots[buf].mFence = Fence::NO_FENCE; michael@0: } // end lock scope michael@0: michael@0: sp graphicBuffer; michael@0: if (returnFlags & IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION) { michael@0: RefPtr textureClient = michael@0: new GrallocTextureClientOGL(ImageBridgeChild::GetSingleton(), michael@0: gfx::SurfaceFormat::UNKNOWN, michael@0: gfx::BackendType::NONE, michael@0: TEXTURE_DEALLOCATE_CLIENT); michael@0: usage |= GraphicBuffer::USAGE_HW_TEXTURE; michael@0: bool result = textureClient->AllocateGralloc(IntSize(w, h), format, usage); michael@0: sp graphicBuffer = textureClient->GetGraphicBuffer(); michael@0: if (!result || !graphicBuffer.get()) { michael@0: ST_LOGE("dequeueBuffer: failed to alloc gralloc buffer"); michael@0: return -ENOMEM; michael@0: } michael@0: michael@0: { // Scope for the lock michael@0: Mutex::Autolock lock(mMutex); michael@0: michael@0: if (mAbandoned) { michael@0: ST_LOGE("dequeueBuffer: SurfaceTexture has been abandoned!"); michael@0: return NO_INIT; michael@0: } michael@0: michael@0: mSlots[buf].mGraphicBuffer = graphicBuffer; michael@0: mSlots[buf].mTextureClient = textureClient; michael@0: ST_LOGD("dequeueBuffer: returning slot=%d buf=%p ", buf, michael@0: mSlots[buf].mGraphicBuffer->handle); michael@0: michael@0: } michael@0: michael@0: } michael@0: michael@0: ST_LOGV("dequeueBuffer: returning slot=%d/%llu buf=%p flags=%#x", *outBuf, michael@0: mSlots[*outBuf].mFrameNumber, michael@0: mSlots[*outBuf].mGraphicBuffer->handle, returnFlags); michael@0: michael@0: return returnFlags; michael@0: } michael@0: michael@0: status_t GonkBufferQueue::queueBuffer(int buf, michael@0: const QueueBufferInput& input, QueueBufferOutput* output) { michael@0: ATRACE_CALL(); michael@0: michael@0: Rect crop; michael@0: uint32_t transform; michael@0: int scalingMode; michael@0: int64_t timestamp; michael@0: bool isAutoTimestamp; michael@0: bool async; michael@0: sp fence; michael@0: michael@0: input.deflate(×tamp, &isAutoTimestamp, &crop, &scalingMode, &transform, michael@0: &async, &fence); michael@0: michael@0: if (fence == NULL) { michael@0: ST_LOGE("queueBuffer: fence is NULL"); michael@0: return BAD_VALUE; michael@0: } michael@0: michael@0: ST_LOGV("queueBuffer: slot=%d time=%#llx crop=[%d,%d,%d,%d] tr=%#x " michael@0: "scale=%s", michael@0: buf, timestamp, crop.left, crop.top, crop.right, crop.bottom, michael@0: transform, scalingModeName(scalingMode)); michael@0: michael@0: switch (scalingMode) { michael@0: case NATIVE_WINDOW_SCALING_MODE_FREEZE: michael@0: case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW: michael@0: case NATIVE_WINDOW_SCALING_MODE_SCALE_CROP: michael@0: case NATIVE_WINDOW_SCALING_MODE_NO_SCALE_CROP: michael@0: break; michael@0: default: michael@0: ST_LOGE("unknown scaling mode: %d", scalingMode); michael@0: return -EINVAL; michael@0: } michael@0: michael@0: sp listener; michael@0: michael@0: { // scope for the lock michael@0: Mutex::Autolock lock(mMutex); michael@0: michael@0: if (mAbandoned) { michael@0: ST_LOGE("queueBuffer: GonkBufferQueue has been abandoned!"); michael@0: return NO_INIT; michael@0: } michael@0: michael@0: const int maxBufferCount = getMaxBufferCountLocked(async); michael@0: if (async && mOverrideMaxBufferCount) { michael@0: // FIXME: some drivers are manually setting the buffer-count (which they michael@0: // shouldn't), so we do this extra test here to handle that case. michael@0: // This is TEMPORARY, until we get this fixed. michael@0: if (mOverrideMaxBufferCount < maxBufferCount) { michael@0: ST_LOGE("queueBuffer: async mode is invalid with buffercount override"); michael@0: return BAD_VALUE; michael@0: } michael@0: } michael@0: if (buf < 0 || buf >= maxBufferCount) { michael@0: ST_LOGE("queueBuffer: slot index out of range [0, %d]: %d", michael@0: maxBufferCount, buf); michael@0: return -EINVAL; michael@0: } else if (mSlots[buf].mBufferState != BufferSlot::DEQUEUED) { michael@0: ST_LOGE("queueBuffer: slot %d is not owned by the client " michael@0: "(state=%d)", buf, mSlots[buf].mBufferState); michael@0: return -EINVAL; michael@0: } else if (!mSlots[buf].mRequestBufferCalled) { michael@0: ST_LOGE("queueBuffer: slot %d was enqueued without requesting a " michael@0: "buffer", buf); michael@0: return -EINVAL; michael@0: } michael@0: michael@0: ST_LOGV("queueBuffer: slot=%d/%llu time=%#llx crop=[%d,%d,%d,%d] " michael@0: "tr=%#x scale=%s", michael@0: buf, mFrameCounter + 1, timestamp, michael@0: crop.left, crop.top, crop.right, crop.bottom, michael@0: transform, scalingModeName(scalingMode)); michael@0: michael@0: const sp& graphicBuffer(mSlots[buf].mGraphicBuffer); michael@0: Rect bufferRect(graphicBuffer->getWidth(), graphicBuffer->getHeight()); michael@0: Rect croppedCrop; michael@0: crop.intersect(bufferRect, &croppedCrop); michael@0: if (croppedCrop != crop) { michael@0: ST_LOGE("queueBuffer: crop rect is not contained within the " michael@0: "buffer in slot %d", buf); michael@0: return -EINVAL; michael@0: } michael@0: michael@0: mSlots[buf].mFence = fence; michael@0: mSlots[buf].mBufferState = BufferSlot::QUEUED; michael@0: mFrameCounter++; michael@0: mSlots[buf].mFrameNumber = mFrameCounter; michael@0: michael@0: BufferItem item; michael@0: item.mAcquireCalled = mSlots[buf].mAcquireCalled; michael@0: item.mGraphicBuffer = mSlots[buf].mGraphicBuffer; michael@0: item.mCrop = crop; michael@0: item.mTransform = transform & ~NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY; michael@0: item.mTransformToDisplayInverse = bool(transform & NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY); michael@0: item.mScalingMode = scalingMode; michael@0: item.mTimestamp = timestamp; michael@0: item.mIsAutoTimestamp = isAutoTimestamp; michael@0: item.mFrameNumber = mFrameCounter; michael@0: item.mBuf = buf; michael@0: item.mFence = fence; michael@0: item.mIsDroppable = mDequeueBufferCannotBlock || async; michael@0: michael@0: if (mQueue.empty()) { michael@0: // when the queue is empty, we can ignore "mDequeueBufferCannotBlock", and michael@0: // simply queue this buffer. michael@0: mQueue.push_back(item); michael@0: listener = mConsumerListener; michael@0: } else { michael@0: // when the queue is not empty, we need to look at the front buffer michael@0: // state and see if we need to replace it. michael@0: Fifo::iterator front(mQueue.begin()); michael@0: if (front->mIsDroppable) { michael@0: // buffer slot currently queued is marked free if still tracked michael@0: if (stillTracking(front)) { michael@0: mSlots[front->mBuf].mBufferState = BufferSlot::FREE; michael@0: // reset the frame number of the freed buffer so that it is the first in michael@0: // line to be dequeued again. michael@0: mSlots[front->mBuf].mFrameNumber = 0; michael@0: } michael@0: // and we record the new buffer in the queued list michael@0: *front = item; michael@0: } else { michael@0: mQueue.push_back(item); michael@0: listener = mConsumerListener; michael@0: } michael@0: } michael@0: michael@0: mBufferHasBeenQueued = true; michael@0: mDequeueCondition.broadcast(); michael@0: michael@0: output->inflate(mDefaultWidth, mDefaultHeight, mTransformHint, michael@0: mQueue.size()); michael@0: michael@0: } // scope for the lock michael@0: michael@0: // call back without lock held michael@0: if (listener != 0) { michael@0: listener->onFrameAvailable(); michael@0: } michael@0: return NO_ERROR; michael@0: } michael@0: michael@0: void GonkBufferQueue::cancelBuffer(int buf, const sp& fence) { michael@0: ATRACE_CALL(); michael@0: ST_LOGV("cancelBuffer: slot=%d", buf); michael@0: Mutex::Autolock lock(mMutex); michael@0: michael@0: if (mAbandoned) { michael@0: ST_LOGW("cancelBuffer: GonkBufferQueue has been abandoned!"); michael@0: return; michael@0: } michael@0: michael@0: if (buf < 0 || buf >= NUM_BUFFER_SLOTS) { michael@0: ST_LOGE("cancelBuffer: slot index out of range [0, %d]: %d", michael@0: NUM_BUFFER_SLOTS, buf); michael@0: return; michael@0: } else if (mSlots[buf].mBufferState != BufferSlot::DEQUEUED) { michael@0: ST_LOGE("cancelBuffer: slot %d is not owned by the client (state=%d)", michael@0: buf, mSlots[buf].mBufferState); michael@0: return; michael@0: } else if (fence == NULL) { michael@0: ST_LOGE("cancelBuffer: fence is NULL"); michael@0: return; michael@0: } michael@0: mSlots[buf].mBufferState = BufferSlot::FREE; michael@0: mSlots[buf].mFrameNumber = 0; michael@0: mSlots[buf].mFence = fence; michael@0: mDequeueCondition.broadcast(); michael@0: } michael@0: michael@0: michael@0: status_t GonkBufferQueue::connect(const sp& token, michael@0: int api, bool producerControlledByApp, QueueBufferOutput* output) { michael@0: ATRACE_CALL(); michael@0: ST_LOGV("connect: api=%d producerControlledByApp=%s", api, michael@0: producerControlledByApp ? "true" : "false"); michael@0: Mutex::Autolock lock(mMutex); michael@0: michael@0: retry: michael@0: if (mAbandoned) { michael@0: ST_LOGE("connect: GonkBufferQueue has been abandoned!"); michael@0: return NO_INIT; michael@0: } michael@0: michael@0: if (mConsumerListener == NULL) { michael@0: ST_LOGE("connect: GonkBufferQueue has no consumer!"); michael@0: return NO_INIT; michael@0: } michael@0: michael@0: if (mConnectedApi != NO_CONNECTED_API) { michael@0: ST_LOGE("connect: already connected (cur=%d, req=%d)", michael@0: mConnectedApi, api); michael@0: return -EINVAL; michael@0: } michael@0: michael@0: // If we disconnect and reconnect quickly, we can be in a state where our slots are michael@0: // empty but we have many buffers in the queue. This can cause us to run out of michael@0: // memory if we outrun the consumer. Wait here if it looks like we have too many michael@0: // buffers queued up. michael@0: int maxBufferCount = getMaxBufferCountLocked(false); // worst-case, i.e. largest value michael@0: if (mQueue.size() > (size_t) maxBufferCount) { michael@0: // TODO: make this bound tighter? michael@0: ST_LOGV("queue size is %d, waiting", mQueue.size()); michael@0: mDequeueCondition.wait(mMutex); michael@0: goto retry; michael@0: } michael@0: michael@0: int err = NO_ERROR; michael@0: switch (api) { michael@0: case NATIVE_WINDOW_API_EGL: michael@0: case NATIVE_WINDOW_API_CPU: michael@0: case NATIVE_WINDOW_API_MEDIA: michael@0: case NATIVE_WINDOW_API_CAMERA: michael@0: mConnectedApi = api; michael@0: output->inflate(mDefaultWidth, mDefaultHeight, mTransformHint, mQueue.size()); michael@0: michael@0: // set-up a death notification so that we can disconnect michael@0: // automatically when/if the remote producer dies. michael@0: if (token != NULL && token->remoteBinder() != NULL) { michael@0: status_t err = token->linkToDeath(static_cast(this)); michael@0: if (err == NO_ERROR) { michael@0: mConnectedProducerToken = token; michael@0: } else { michael@0: ALOGE("linkToDeath failed: %s (%d)", strerror(-err), err); michael@0: } michael@0: } michael@0: break; michael@0: default: michael@0: err = -EINVAL; michael@0: break; michael@0: } michael@0: michael@0: mBufferHasBeenQueued = false; michael@0: mDequeueBufferCannotBlock = mConsumerControlledByApp && producerControlledByApp; michael@0: michael@0: return err; michael@0: } michael@0: michael@0: void GonkBufferQueue::binderDied(const wp& who) { michael@0: // If we're here, it means that a producer we were connected to died. michael@0: // We're GUARANTEED that we still are connected to it because it has no other way michael@0: // to get disconnected -- or -- we wouldn't be here because we're removing this michael@0: // callback upon disconnect. Therefore, it's okay to read mConnectedApi without michael@0: // synchronization here. michael@0: int api = mConnectedApi; michael@0: this->disconnect(api); michael@0: } michael@0: michael@0: status_t GonkBufferQueue::disconnect(int api) { michael@0: ATRACE_CALL(); michael@0: ST_LOGV("disconnect: api=%d", api); michael@0: michael@0: int err = NO_ERROR; michael@0: sp listener; michael@0: michael@0: { // Scope for the lock michael@0: Mutex::Autolock lock(mMutex); michael@0: michael@0: if (mAbandoned) { michael@0: // it is not really an error to disconnect after the surface michael@0: // has been abandoned, it should just be a no-op. michael@0: return NO_ERROR; michael@0: } michael@0: michael@0: switch (api) { michael@0: case NATIVE_WINDOW_API_EGL: michael@0: case NATIVE_WINDOW_API_CPU: michael@0: case NATIVE_WINDOW_API_MEDIA: michael@0: case NATIVE_WINDOW_API_CAMERA: michael@0: if (mConnectedApi == api) { michael@0: freeAllBuffersLocked(); michael@0: mConnectedApi = NO_CONNECTED_API; michael@0: mDequeueCondition.broadcast(); michael@0: listener = mConsumerListener; michael@0: } else { michael@0: ST_LOGE("disconnect: connected to another api (cur=%d, req=%d)", michael@0: mConnectedApi, api); michael@0: err = -EINVAL; michael@0: } michael@0: break; michael@0: default: michael@0: ST_LOGE("disconnect: unknown API %d", api); michael@0: err = -EINVAL; michael@0: break; michael@0: } michael@0: } michael@0: michael@0: if (listener != NULL) { michael@0: listener->onBuffersReleased(); michael@0: } michael@0: michael@0: return err; michael@0: } michael@0: michael@0: void GonkBufferQueue::dump(String8& result, const char* prefix) const { michael@0: Mutex::Autolock _l(mMutex); michael@0: michael@0: String8 fifo; michael@0: int fifoSize = 0; michael@0: Fifo::const_iterator i(mQueue.begin()); michael@0: while (i != mQueue.end()) { michael@0: fifo.appendFormat("%02d:%p crop=[%d,%d,%d,%d], " michael@0: "xform=0x%02x, time=%#llx, scale=%s\n", michael@0: i->mBuf, i->mGraphicBuffer.get(), michael@0: i->mCrop.left, i->mCrop.top, i->mCrop.right, michael@0: i->mCrop.bottom, i->mTransform, i->mTimestamp, michael@0: scalingModeName(i->mScalingMode) michael@0: ); michael@0: i++; michael@0: fifoSize++; michael@0: } michael@0: michael@0: michael@0: result.appendFormat( michael@0: "%s-BufferQueue mMaxAcquiredBufferCount=%d, mDequeueBufferCannotBlock=%d, default-size=[%dx%d], " michael@0: "default-format=%d, transform-hint=%02x, FIFO(%d)={%s}\n", michael@0: prefix, mMaxAcquiredBufferCount, mDequeueBufferCannotBlock, mDefaultWidth, michael@0: mDefaultHeight, mDefaultBufferFormat, mTransformHint, michael@0: fifoSize, fifo.string()); michael@0: michael@0: struct { michael@0: const char * operator()(int state) const { michael@0: switch (state) { michael@0: case BufferSlot::DEQUEUED: return "DEQUEUED"; michael@0: case BufferSlot::QUEUED: return "QUEUED"; michael@0: case BufferSlot::FREE: return "FREE"; michael@0: case BufferSlot::ACQUIRED: return "ACQUIRED"; michael@0: default: return "Unknown"; michael@0: } michael@0: } michael@0: } stateName; michael@0: michael@0: // just trim the free buffers to not spam the dump michael@0: int maxBufferCount = 0; michael@0: for (int i=NUM_BUFFER_SLOTS-1 ; i>=0 ; i--) { michael@0: const BufferSlot& slot(mSlots[i]); michael@0: if ((slot.mBufferState != BufferSlot::FREE) || (slot.mGraphicBuffer != NULL)) { michael@0: maxBufferCount = i+1; michael@0: break; michael@0: } michael@0: } michael@0: michael@0: for (int i=0 ; i& buf(slot.mGraphicBuffer); michael@0: result.appendFormat( michael@0: "%s%s[%02d:%p] state=%-8s", michael@0: prefix, (slot.mBufferState == BufferSlot::ACQUIRED)?">":" ", i, buf.get(), michael@0: stateName(slot.mBufferState) michael@0: ); michael@0: michael@0: if (buf != NULL) { michael@0: result.appendFormat( michael@0: ", %p [%4ux%4u:%4u,%3X]", michael@0: buf->handle, buf->width, buf->height, buf->stride, michael@0: buf->format); michael@0: } michael@0: result.append("\n"); michael@0: } michael@0: } michael@0: michael@0: void GonkBufferQueue::freeAllBuffersLocked() michael@0: { michael@0: ALOGW_IF(!mQueue.isEmpty(), michael@0: "freeAllBuffersLocked called but mQueue is not empty"); michael@0: mQueue.clear(); michael@0: mBufferHasBeenQueued = false; michael@0: for (int i = 0; i < NUM_BUFFER_SLOTS; i++) { michael@0: mSlots[i].mGraphicBuffer = 0; michael@0: if (mSlots[i].mTextureClient) { michael@0: mSlots[i].mTextureClient->ClearRecycleCallback(); michael@0: // release TextureClient in ImageBridge thread michael@0: nsProxyReleaseTask* task = new nsProxyReleaseTask(mSlots[i].mTextureClient); michael@0: mSlots[i].mTextureClient = NULL; michael@0: ImageBridgeChild::GetSingleton()->GetMessageLoop()->PostTask(FROM_HERE, task); michael@0: } michael@0: if (mSlots[i].mBufferState == BufferSlot::ACQUIRED) { michael@0: mSlots[i].mNeedsCleanupOnRelease = true; michael@0: } michael@0: mSlots[i].mBufferState = BufferSlot::FREE; michael@0: mSlots[i].mFrameNumber = 0; michael@0: mSlots[i].mAcquireCalled = false; michael@0: // destroy fence as GonkBufferQueue now takes ownership michael@0: mSlots[i].mFence = Fence::NO_FENCE; michael@0: } michael@0: } michael@0: michael@0: status_t GonkBufferQueue::acquireBuffer(BufferItem *buffer, nsecs_t expectedPresent) { michael@0: ATRACE_CALL(); michael@0: Mutex::Autolock _l(mMutex); michael@0: michael@0: // Check that the consumer doesn't currently have the maximum number of michael@0: // buffers acquired. We allow the max buffer count to be exceeded by one michael@0: // buffer, so that the consumer can successfully set up the newly acquired michael@0: // buffer before releasing the old one. michael@0: int numAcquiredBuffers = 0; michael@0: for (int i = 0; i < NUM_BUFFER_SLOTS; i++) { michael@0: if (mSlots[i].mBufferState == BufferSlot::ACQUIRED) { michael@0: numAcquiredBuffers++; michael@0: } michael@0: } michael@0: if (numAcquiredBuffers >= mMaxAcquiredBufferCount+1) { michael@0: ST_LOGE("acquireBuffer: max acquired buffer count reached: %d (max=%d)", michael@0: numAcquiredBuffers, mMaxAcquiredBufferCount); michael@0: return INVALID_OPERATION; michael@0: } michael@0: michael@0: // check if queue is empty michael@0: // In asynchronous mode the list is guaranteed to be one buffer michael@0: // deep, while in synchronous mode we use the oldest buffer. michael@0: if (mQueue.empty()) { michael@0: return NO_BUFFER_AVAILABLE; michael@0: } michael@0: michael@0: Fifo::iterator front(mQueue.begin()); michael@0: michael@0: // If expectedPresent is specified, we may not want to return a buffer yet. michael@0: // If it's specified and there's more than one buffer queued, we may michael@0: // want to drop a buffer. michael@0: if (expectedPresent != 0) { michael@0: const int MAX_REASONABLE_NSEC = 1000000000ULL; // 1 second michael@0: michael@0: // The "expectedPresent" argument indicates when the buffer is expected michael@0: // to be presented on-screen. If the buffer's desired-present time michael@0: // is earlier (less) than expectedPresent, meaning it'll be displayed michael@0: // on time or possibly late if we show it ASAP, we acquire and return michael@0: // it. If we don't want to display it until after the expectedPresent michael@0: // time, we return PRESENT_LATER without acquiring it. michael@0: // michael@0: // To be safe, we don't defer acquisition if expectedPresent is michael@0: // more than one second in the future beyond the desired present time michael@0: // (i.e. we'd be holding the buffer for a long time). michael@0: // michael@0: // NOTE: code assumes monotonic time values from the system clock are michael@0: // positive. michael@0: michael@0: // Start by checking to see if we can drop frames. We skip this check michael@0: // if the timestamps are being auto-generated by Surface -- if the michael@0: // app isn't generating timestamps explicitly, they probably don't michael@0: // want frames to be discarded based on them. michael@0: while (mQueue.size() > 1 && !mQueue[0].mIsAutoTimestamp) { michael@0: // If entry[1] is timely, drop entry[0] (and repeat). We apply michael@0: // an additional criteria here: we only drop the earlier buffer if michael@0: // our desiredPresent falls within +/- 1 second of the expected michael@0: // present. Otherwise, bogus desiredPresent times (e.g. 0 or michael@0: // a small relative timestamp), which normally mean "ignore the michael@0: // timestamp and acquire immediately", would cause us to drop michael@0: // frames. michael@0: // michael@0: // We may want to add an additional criteria: don't drop the michael@0: // earlier buffer if entry[1]'s fence hasn't signaled yet. michael@0: // michael@0: // (Vector front is [0], back is [size()-1]) michael@0: const BufferItem& bi(mQueue[1]); michael@0: nsecs_t desiredPresent = bi.mTimestamp; michael@0: if (desiredPresent < expectedPresent - MAX_REASONABLE_NSEC || michael@0: desiredPresent > expectedPresent) { michael@0: // This buffer is set to display in the near future, or michael@0: // desiredPresent is garbage. Either way we don't want to michael@0: // drop the previous buffer just to get this on screen sooner. michael@0: ST_LOGV("pts nodrop: des=%lld expect=%lld (%lld) now=%lld", michael@0: desiredPresent, expectedPresent, desiredPresent - expectedPresent, michael@0: systemTime(CLOCK_MONOTONIC)); michael@0: break; michael@0: } michael@0: ST_LOGV("pts drop: queue1des=%lld expect=%lld size=%d", michael@0: desiredPresent, expectedPresent, mQueue.size()); michael@0: if (stillTracking(front)) { michael@0: // front buffer is still in mSlots, so mark the slot as free michael@0: mSlots[front->mBuf].mBufferState = BufferSlot::FREE; michael@0: } michael@0: mQueue.erase(front); michael@0: front = mQueue.begin(); michael@0: } michael@0: michael@0: // See if the front buffer is due. michael@0: nsecs_t desiredPresent = front->mTimestamp; michael@0: if (desiredPresent > expectedPresent && michael@0: desiredPresent < expectedPresent + MAX_REASONABLE_NSEC) { michael@0: ST_LOGV("pts defer: des=%lld expect=%lld (%lld) now=%lld", michael@0: desiredPresent, expectedPresent, desiredPresent - expectedPresent, michael@0: systemTime(CLOCK_MONOTONIC)); michael@0: return PRESENT_LATER; michael@0: } michael@0: michael@0: ST_LOGV("pts accept: des=%lld expect=%lld (%lld) now=%lld", michael@0: desiredPresent, expectedPresent, desiredPresent - expectedPresent, michael@0: systemTime(CLOCK_MONOTONIC)); michael@0: } michael@0: michael@0: int buf = front->mBuf; michael@0: buffer->mGraphicBuffer = mSlots[buf].mGraphicBuffer; michael@0: buffer->mFrameNumber = mSlots[buf].mFrameNumber; michael@0: buffer->mBuf = buf; michael@0: buffer->mFence = mSlots[buf].mFence; michael@0: ATRACE_BUFFER_INDEX(buf); michael@0: michael@0: ST_LOGV("acquireBuffer: acquiring { slot=%d/%llu, buffer=%p }", michael@0: front->mBuf, front->mFrameNumber, michael@0: front->mGraphicBuffer->handle); michael@0: // if front buffer still being tracked update slot state michael@0: if (stillTracking(front)) { michael@0: mSlots[buf].mAcquireCalled = true; michael@0: mSlots[buf].mNeedsCleanupOnRelease = false; michael@0: mSlots[buf].mBufferState = BufferSlot::ACQUIRED; michael@0: mSlots[buf].mFence = Fence::NO_FENCE; michael@0: } michael@0: michael@0: // If the buffer has previously been acquired by the consumer, set michael@0: // mGraphicBuffer to NULL to avoid unnecessarily remapping this michael@0: // buffer on the consumer side. michael@0: //if (buffer->mAcquireCalled) { michael@0: // buffer->mGraphicBuffer = NULL; michael@0: //} michael@0: michael@0: mQueue.erase(front); michael@0: mDequeueCondition.broadcast(); michael@0: michael@0: return NO_ERROR; michael@0: } michael@0: michael@0: status_t GonkBufferQueue::releaseBuffer(int buf, uint64_t frameNumber, const sp& fence) { michael@0: ATRACE_CALL(); michael@0: michael@0: if (buf == INVALID_BUFFER_SLOT || fence == NULL) { michael@0: return BAD_VALUE; michael@0: } michael@0: michael@0: Mutex::Autolock _l(mMutex); michael@0: michael@0: // If the frame number has changed because buffer has been reallocated, michael@0: // we can ignore this releaseBuffer for the old buffer. michael@0: //if (frameNumber != mSlots[buf].mFrameNumber) { michael@0: // return STALE_BUFFER_SLOT; michael@0: //} michael@0: michael@0: michael@0: // Internal state consistency checks: michael@0: // Make sure this buffers hasn't been queued while we were owning it (acquired) michael@0: Fifo::iterator front(mQueue.begin()); michael@0: Fifo::const_iterator const end(mQueue.end()); michael@0: while (front != end) { michael@0: if (front->mBuf == buf) { michael@0: LOG_ALWAYS_FATAL("[%s] received new buffer(#%lld) on slot #%d that has not yet been " michael@0: "acquired", mConsumerName.string(), frameNumber, buf); michael@0: break; // never reached michael@0: } michael@0: front++; michael@0: } michael@0: michael@0: // The buffer can now only be released if its in the acquired state michael@0: if (mSlots[buf].mBufferState == BufferSlot::ACQUIRED) { michael@0: mSlots[buf].mFence = fence; michael@0: mSlots[buf].mBufferState = BufferSlot::FREE; michael@0: } else if (mSlots[buf].mNeedsCleanupOnRelease) { michael@0: ST_LOGV("releasing a stale buf %d its state was %d", buf, mSlots[buf].mBufferState); michael@0: mSlots[buf].mNeedsCleanupOnRelease = false; michael@0: return STALE_BUFFER_SLOT; michael@0: } else { michael@0: ST_LOGE("attempted to release buf %d but its state was %d", buf, mSlots[buf].mBufferState); michael@0: return -EINVAL; michael@0: } michael@0: michael@0: mDequeueCondition.broadcast(); michael@0: return NO_ERROR; michael@0: } michael@0: michael@0: status_t GonkBufferQueue::consumerConnect(const sp& consumerListener, michael@0: bool controlledByApp) { michael@0: ST_LOGV("consumerConnect controlledByApp=%s", michael@0: controlledByApp ? "true" : "false"); michael@0: Mutex::Autolock lock(mMutex); michael@0: michael@0: if (mAbandoned) { michael@0: ST_LOGE("consumerConnect: GonkBufferQueue has been abandoned!"); michael@0: return NO_INIT; michael@0: } michael@0: if (consumerListener == NULL) { michael@0: ST_LOGE("consumerConnect: consumerListener may not be NULL"); michael@0: return BAD_VALUE; michael@0: } michael@0: michael@0: mConsumerListener = consumerListener; michael@0: mConsumerControlledByApp = controlledByApp; michael@0: michael@0: return NO_ERROR; michael@0: } michael@0: michael@0: status_t GonkBufferQueue::consumerDisconnect() { michael@0: ST_LOGV("consumerDisconnect"); michael@0: Mutex::Autolock lock(mMutex); michael@0: michael@0: if (mConsumerListener == NULL) { michael@0: ST_LOGE("consumerDisconnect: No consumer is connected!"); michael@0: return -EINVAL; michael@0: } michael@0: michael@0: mAbandoned = true; michael@0: mConsumerListener = NULL; michael@0: mQueue.clear(); michael@0: freeAllBuffersLocked(); michael@0: mDequeueCondition.broadcast(); michael@0: return NO_ERROR; michael@0: } michael@0: michael@0: status_t GonkBufferQueue::getReleasedBuffers(uint32_t* slotMask) { michael@0: ST_LOGV("getReleasedBuffers"); michael@0: Mutex::Autolock lock(mMutex); michael@0: michael@0: if (mAbandoned) { michael@0: ST_LOGE("getReleasedBuffers: GonkBufferQueue has been abandoned!"); michael@0: return NO_INIT; michael@0: } michael@0: michael@0: uint32_t mask = 0; michael@0: for (int i = 0; i < NUM_BUFFER_SLOTS; i++) { michael@0: if (!mSlots[i].mAcquireCalled) { michael@0: mask |= 1 << i; michael@0: } michael@0: } michael@0: michael@0: // Remove buffers in flight (on the queue) from the mask where acquire has michael@0: // been called, as the consumer will not receive the buffer address, so michael@0: // it should not free these slots. michael@0: Fifo::iterator front(mQueue.begin()); michael@0: while (front != mQueue.end()) { michael@0: if (front->mAcquireCalled) michael@0: mask &= ~(1 << front->mBuf); michael@0: front++; michael@0: } michael@0: michael@0: *slotMask = mask; michael@0: michael@0: ST_LOGV("getReleasedBuffers: returning mask %#x", mask); michael@0: return NO_ERROR; michael@0: } michael@0: michael@0: status_t GonkBufferQueue::setDefaultBufferSize(uint32_t w, uint32_t h) { michael@0: ST_LOGV("setDefaultBufferSize: w=%d, h=%d", w, h); michael@0: if (!w || !h) { michael@0: ST_LOGE("setDefaultBufferSize: dimensions cannot be 0 (w=%d, h=%d)", michael@0: w, h); michael@0: return BAD_VALUE; michael@0: } michael@0: michael@0: Mutex::Autolock lock(mMutex); michael@0: mDefaultWidth = w; michael@0: mDefaultHeight = h; michael@0: return NO_ERROR; michael@0: } michael@0: michael@0: status_t GonkBufferQueue::setDefaultMaxBufferCount(int bufferCount) { michael@0: ATRACE_CALL(); michael@0: Mutex::Autolock lock(mMutex); michael@0: return setDefaultMaxBufferCountLocked(bufferCount); michael@0: } michael@0: michael@0: status_t GonkBufferQueue::disableAsyncBuffer() { michael@0: ATRACE_CALL(); michael@0: Mutex::Autolock lock(mMutex); michael@0: if (mConsumerListener != NULL) { michael@0: ST_LOGE("disableAsyncBuffer: consumer already connected!"); michael@0: return INVALID_OPERATION; michael@0: } michael@0: mUseAsyncBuffer = false; michael@0: return NO_ERROR; michael@0: } michael@0: michael@0: status_t GonkBufferQueue::setMaxAcquiredBufferCount(int maxAcquiredBuffers) { michael@0: ATRACE_CALL(); michael@0: Mutex::Autolock lock(mMutex); michael@0: if (maxAcquiredBuffers < 1 || maxAcquiredBuffers > MAX_MAX_ACQUIRED_BUFFERS) { michael@0: ST_LOGE("setMaxAcquiredBufferCount: invalid count specified: %d", michael@0: maxAcquiredBuffers); michael@0: return BAD_VALUE; michael@0: } michael@0: if (mConnectedApi != NO_CONNECTED_API) { michael@0: return INVALID_OPERATION; michael@0: } michael@0: mMaxAcquiredBufferCount = maxAcquiredBuffers; michael@0: return NO_ERROR; michael@0: } michael@0: michael@0: int GonkBufferQueue::getMinUndequeuedBufferCount(bool async) const { michael@0: // if dequeueBuffer is allowed to error out, we don't have to michael@0: // add an extra buffer. michael@0: if (!mUseAsyncBuffer) michael@0: return mMaxAcquiredBufferCount; michael@0: michael@0: // we're in async mode, or we want to prevent the app to michael@0: // deadlock itself, we throw-in an extra buffer to guarantee it. michael@0: if (mDequeueBufferCannotBlock || async) michael@0: return mMaxAcquiredBufferCount + 1; michael@0: michael@0: return mMaxAcquiredBufferCount; michael@0: } michael@0: michael@0: int GonkBufferQueue::getMinMaxBufferCountLocked(bool async) const { michael@0: return getMinUndequeuedBufferCount(async) + 1; michael@0: } michael@0: michael@0: int GonkBufferQueue::getMaxBufferCountLocked(bool async) const { michael@0: int minMaxBufferCount = getMinMaxBufferCountLocked(async); michael@0: michael@0: int maxBufferCount = mDefaultMaxBufferCount; michael@0: if (maxBufferCount < minMaxBufferCount) { michael@0: maxBufferCount = minMaxBufferCount; michael@0: } michael@0: if (mOverrideMaxBufferCount != 0) { michael@0: assert(mOverrideMaxBufferCount >= minMaxBufferCount); michael@0: maxBufferCount = mOverrideMaxBufferCount; michael@0: } michael@0: michael@0: // Any buffers that are dequeued by the producer or sitting in the queue michael@0: // waiting to be consumed need to have their slots preserved. Such michael@0: // buffers will temporarily keep the max buffer count up until the slots michael@0: // no longer need to be preserved. michael@0: for (int i = maxBufferCount; i < NUM_BUFFER_SLOTS; i++) { michael@0: BufferSlot::BufferState state = mSlots[i].mBufferState; michael@0: if (state == BufferSlot::QUEUED || state == BufferSlot::DEQUEUED) { michael@0: maxBufferCount = i + 1; michael@0: } michael@0: } michael@0: michael@0: return maxBufferCount; michael@0: } michael@0: michael@0: bool GonkBufferQueue::stillTracking(const BufferItem *item) const { michael@0: const BufferSlot &slot = mSlots[item->mBuf]; michael@0: michael@0: ST_LOGV("stillTracking?: item: { slot=%d/%llu, buffer=%p }, " michael@0: "slot: { slot=%d/%llu, buffer=%p }", michael@0: item->mBuf, item->mFrameNumber, michael@0: (item->mGraphicBuffer.get() ? item->mGraphicBuffer->handle : 0), michael@0: item->mBuf, slot.mFrameNumber, michael@0: (slot.mGraphicBuffer.get() ? slot.mGraphicBuffer->handle : 0)); michael@0: michael@0: // Compare item with its original buffer slot. We can check the slot michael@0: // as the buffer would not be moved to a different slot by the producer. michael@0: return (slot.mGraphicBuffer != NULL && michael@0: item->mGraphicBuffer->handle == slot.mGraphicBuffer->handle); michael@0: } michael@0: michael@0: GonkBufferQueue::ProxyConsumerListener::ProxyConsumerListener( michael@0: const wp& consumerListener): michael@0: mConsumerListener(consumerListener) {} michael@0: michael@0: GonkBufferQueue::ProxyConsumerListener::~ProxyConsumerListener() {} michael@0: michael@0: void GonkBufferQueue::ProxyConsumerListener::onFrameAvailable() { michael@0: sp listener(mConsumerListener.promote()); michael@0: if (listener != NULL) { michael@0: listener->onFrameAvailable(); michael@0: } michael@0: } michael@0: michael@0: void GonkBufferQueue::ProxyConsumerListener::onBuffersReleased() { michael@0: sp listener(mConsumerListener.promote()); michael@0: if (listener != NULL) { michael@0: listener->onBuffersReleased(); michael@0: } michael@0: } michael@0: michael@0: }; // namespace android