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: michael@0: #include "mozilla/layers/GrallocTextureClient.h" michael@0: #include "mozilla/layers/ImageBridgeChild.h" michael@0: michael@0: #include "GonkBufferQueueJB.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: mAllowSynchronousMode(allowSynchronousMode), 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: bool GonkBufferQueue::isSynchronousMode() const { michael@0: Mutex::Autolock lock(mMutex); michael@0: return mSynchronousMode; 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: 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: int maxBufferCount = getMaxBufferCountLocked(); 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: 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 = getMinUndequeuedBufferCountLocked(); michael@0: break; michael@0: case NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND: michael@0: value = (mQueue.size() >= 2); 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: 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: int maxBufferCount = getMaxBufferCountLocked(); michael@0: if (slot < 0 || maxBufferCount <= slot) { michael@0: ST_LOGE("requestBuffer: slot index out of range [0, %d]: %d", michael@0: maxBufferCount, slot); michael@0: return BAD_VALUE; michael@0: } else if (mSlots[slot].mBufferState != BufferSlot::DEQUEUED) { michael@0: // XXX: I vaguely recall there was some reason this can be valid, but michael@0: // for the life of me I can't recall under what circumstances that's michael@0: // the case. 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, michael@0: uint32_t w, uint32_t h, uint32_t format, uint32_t usage) { 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: int dequeuedCount = 0; 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(); 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: dequeuedCount = 0; michael@0: for (int i = 0; i < maxBufferCount; i++) { michael@0: const int state = mSlots[i].mBufferState; michael@0: if (state == BufferSlot::DEQUEUED) { michael@0: dequeuedCount++; michael@0: } michael@0: michael@0: if (state == 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: } 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 = getMinUndequeuedBufferCountLocked(); 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: 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: *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: //mSlots[*outBuf].mGraphicBuffer = graphicBuffer; michael@0: } michael@0: } michael@0: michael@0: ST_LOGV("dequeueBuffer: returning slot=%d buf=%p flags=%#x", *outBuf, michael@0: mSlots[*outBuf].mGraphicBuffer->handle, returnFlags); michael@0: michael@0: return returnFlags; michael@0: } michael@0: michael@0: status_t GonkBufferQueue::setSynchronousMode(bool enabled) { michael@0: return NO_ERROR; michael@0: } michael@0: michael@0: status_t GonkBufferQueue::queueBuffer(int buf, michael@0: const QueueBufferInput& input, QueueBufferOutput* output) { michael@0: michael@0: Rect crop; michael@0: uint32_t transform; michael@0: int scalingMode; michael@0: int64_t timestamp; michael@0: sp fence; michael@0: michael@0: input.deflate(×tamp, &crop, &scalingMode, &transform, &fence); michael@0: michael@0: #if ANDROID_VERSION >= 18 michael@0: if (fence == NULL) { michael@0: ST_LOGE("queueBuffer: fence is NULL"); michael@0: return BAD_VALUE; michael@0: } michael@0: #endif 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: sp listener; michael@0: michael@0: { // scope for the lock michael@0: Mutex::Autolock lock(mMutex); michael@0: if (mAbandoned) { michael@0: ST_LOGE("queueBuffer: GonkBufferQueue has been abandoned!"); michael@0: return NO_INIT; michael@0: } michael@0: int maxBufferCount = getMaxBufferCountLocked(); 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: 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: if (mSynchronousMode) { michael@0: // In synchronous mode we queue all buffers in a FIFO. michael@0: mQueue.push_back(buf); michael@0: michael@0: // Synchronous mode always signals that an additional frame should michael@0: // be consumed. michael@0: listener = mConsumerListener; michael@0: } else { michael@0: // In asynchronous mode we only keep the most recent buffer. michael@0: if (mQueue.empty()) { michael@0: mQueue.push_back(buf); michael@0: michael@0: // Asynchronous mode only signals that a frame should be michael@0: // consumed if no previous frame was pending. If a frame were michael@0: // pending then the consumer would have already been notified. michael@0: listener = mConsumerListener; michael@0: } else { michael@0: Fifo::iterator front(mQueue.begin()); michael@0: // buffer currently queued is freed michael@0: mSlots[*front].mBufferState = BufferSlot::FREE; michael@0: // and we record the new buffer index in the queued list michael@0: *front = buf; michael@0: } michael@0: } michael@0: michael@0: mSlots[buf].mTimestamp = timestamp; michael@0: mSlots[buf].mCrop = crop; michael@0: mSlots[buf].mTransform = transform; michael@0: mSlots[buf].mFence = fence; 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: break; michael@0: default: michael@0: ST_LOGE("unknown scaling mode: %d (ignoring)", scalingMode); michael@0: scalingMode = mSlots[buf].mScalingMode; michael@0: break; michael@0: } michael@0: michael@0: mSlots[buf].mBufferState = BufferSlot::QUEUED; michael@0: mSlots[buf].mScalingMode = scalingMode; michael@0: mFrameCounter++; michael@0: mSlots[buf].mFrameNumber = mFrameCounter; 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: } // 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: #if ANDROID_VERSION == 17 michael@0: void GonkBufferQueue::cancelBuffer(int buf, sp fence) { michael@0: #else michael@0: void GonkBufferQueue::cancelBuffer(int buf, const sp& fence) { michael@0: #endif michael@0: 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: int maxBufferCount = getMaxBufferCountLocked(); michael@0: if (buf < 0 || buf >= maxBufferCount) { michael@0: ST_LOGE("cancelBuffer: slot index out of range [0, %d]: %d", michael@0: maxBufferCount, 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: #if ANDROID_VERSION >= 18 michael@0: } else if (fence == NULL) { michael@0: ST_LOGE("cancelBuffer: fence is NULL"); michael@0: return; michael@0: #endif 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: status_t GonkBufferQueue::connect(int api, QueueBufferOutput* output) { michael@0: ST_LOGV("connect: api=%d", api); michael@0: Mutex::Autolock lock(mMutex); michael@0: 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: 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: if (mConnectedApi != NO_CONNECTED_API) { michael@0: ST_LOGE("connect: already connected (cur=%d, req=%d)", michael@0: mConnectedApi, api); michael@0: err = -EINVAL; michael@0: } else { michael@0: mConnectedApi = api; michael@0: output->inflate(mDefaultWidth, mDefaultHeight, mTransformHint, michael@0: mQueue.size()); 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: michael@0: return err; michael@0: } michael@0: michael@0: status_t GonkBufferQueue::disconnect(int api) { 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 michael@0: { michael@0: char buffer[1024]; michael@0: GonkBufferQueue::dump(result, "", buffer, 1024); michael@0: } michael@0: michael@0: void GonkBufferQueue::dump(String8& result, const char* prefix, michael@0: char* buffer, size_t SIZE) const michael@0: { 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: snprintf(buffer, SIZE, "%02d ", *i++); michael@0: fifoSize++; michael@0: fifo.append(buffer); michael@0: } michael@0: michael@0: int maxBufferCount = getMaxBufferCountLocked(); michael@0: michael@0: snprintf(buffer, SIZE, michael@0: "%s-BufferQueue maxBufferCount=%d, mSynchronousMode=%d, default-size=[%dx%d], " michael@0: "default-format=%d, transform-hint=%02x, FIFO(%d)={%s}\n", michael@0: prefix, maxBufferCount, mSynchronousMode, mDefaultWidth, michael@0: mDefaultHeight, mDefaultBufferFormat, mTransformHint, michael@0: fifoSize, fifo.string()); michael@0: result.append(buffer); michael@0: 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: for (int i=0 ; i":" ", i, michael@0: stateName(slot.mBufferState), michael@0: slot.mCrop.left, slot.mCrop.top, slot.mCrop.right, michael@0: slot.mCrop.bottom, slot.mTransform, slot.mTimestamp, michael@0: scalingModeName(slot.mScalingMode) michael@0: ); michael@0: result.append(buffer); michael@0: michael@0: const sp& buf(slot.mGraphicBuffer); michael@0: if (buf != NULL) { michael@0: snprintf(buffer, SIZE, michael@0: ", %p [%4ux%4u:%4u,%3X]", michael@0: buf->handle, buf->width, buf->height, buf->stride, michael@0: buf->format); michael@0: result.append(buffer); 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) { 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: Fifo::iterator front(mQueue.begin()); michael@0: int buf = *front; michael@0: michael@0: // In android, when the buffer is aquired by BufferConsumer, michael@0: // BufferQueue releases a reference to the buffer and michael@0: // it's ownership moves to the BufferConsumer. michael@0: // In b2g, GonkBufferQueue continues to have a buffer ownership. michael@0: // It is necessary to free buffer via ImageBridgeChild. michael@0: michael@0: //if (mSlots[buf].mAcquireCalled) { michael@0: // buffer->mGraphicBuffer = NULL; michael@0: //} else { michael@0: // buffer->mGraphicBuffer = mSlots[buf].mGraphicBuffer; michael@0: //} michael@0: buffer->mGraphicBuffer = mSlots[buf].mGraphicBuffer; michael@0: buffer->mCrop = mSlots[buf].mCrop; michael@0: buffer->mTransform = mSlots[buf].mTransform; michael@0: buffer->mScalingMode = mSlots[buf].mScalingMode; michael@0: buffer->mFrameNumber = mSlots[buf].mFrameNumber; michael@0: buffer->mTimestamp = mSlots[buf].mTimestamp; michael@0: buffer->mBuf = buf; michael@0: buffer->mFence = mSlots[buf].mFence; michael@0: 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: mQueue.erase(front); michael@0: mDequeueCondition.broadcast(); michael@0: } else { michael@0: return NO_BUFFER_AVAILABLE; michael@0: } michael@0: michael@0: return NO_ERROR; michael@0: } michael@0: michael@0: status_t GonkBufferQueue::releaseBuffer(int buf, const sp& fence) { michael@0: Mutex::Autolock _l(mMutex); michael@0: michael@0: #if ANDROID_VERSION == 17 michael@0: if (buf == INVALID_BUFFER_SLOT) { michael@0: #else michael@0: if (buf == INVALID_BUFFER_SLOT || fence == NULL) { michael@0: #endif michael@0: return BAD_VALUE; michael@0: } michael@0: michael@0: mSlots[buf].mFence = fence; 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].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: ST_LOGV("consumerConnect"); 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: 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: *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: { 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: Mutex::Autolock lock(mMutex); michael@0: return setDefaultMaxBufferCountLocked(bufferCount); michael@0: } michael@0: michael@0: status_t GonkBufferQueue::setMaxAcquiredBufferCount(int maxAcquiredBuffers) { 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::getMinMaxBufferCountLocked() const { michael@0: return getMinUndequeuedBufferCountLocked() + 1; michael@0: } michael@0: michael@0: int GonkBufferQueue::getMinUndequeuedBufferCountLocked() const { michael@0: return mSynchronousMode ? mMaxAcquiredBufferCount : michael@0: mMaxAcquiredBufferCount + 1; michael@0: } michael@0: michael@0: int GonkBufferQueue::getMaxBufferCountLocked() const { michael@0: int minMaxBufferCount = getMinMaxBufferCountLocked(); 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: 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