Eino-Ville Talvala | e41b318 | 2012-04-16 17:54:33 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | //#define LOG_NDEBUG 0 |
| 18 | #define LOG_TAG "CpuConsumer" |
| 19 | #define ATRACE_TAG ATRACE_TAG_GRAPHICS |
Eino-Ville Talvala | e41b318 | 2012-04-16 17:54:33 -0700 | [diff] [blame] | 20 | |
Eino-Ville Talvala | 042ecee | 2013-02-28 18:23:24 -0800 | [diff] [blame] | 21 | #include <cutils/compiler.h> |
| 22 | #include <utils/Log.h> |
Eino-Ville Talvala | e41b318 | 2012-04-16 17:54:33 -0700 | [diff] [blame] | 23 | #include <gui/CpuConsumer.h> |
| 24 | |
| 25 | #define CC_LOGV(x, ...) ALOGV("[%s] "x, mName.string(), ##__VA_ARGS__) |
| 26 | #define CC_LOGD(x, ...) ALOGD("[%s] "x, mName.string(), ##__VA_ARGS__) |
| 27 | #define CC_LOGI(x, ...) ALOGI("[%s] "x, mName.string(), ##__VA_ARGS__) |
| 28 | #define CC_LOGW(x, ...) ALOGW("[%s] "x, mName.string(), ##__VA_ARGS__) |
| 29 | #define CC_LOGE(x, ...) ALOGE("[%s] "x, mName.string(), ##__VA_ARGS__) |
| 30 | |
| 31 | namespace android { |
| 32 | |
Mathias Agopian | 8f938a5 | 2013-07-12 22:06:26 -0700 | [diff] [blame] | 33 | CpuConsumer::CpuConsumer(const sp<BufferQueue>& bq, |
Mathias Agopian | 595264f | 2013-07-16 22:56:09 -0700 | [diff] [blame^] | 34 | uint32_t maxLockedBuffers, bool controlledByApp) : |
| 35 | ConsumerBase(bq, controlledByApp), |
Eino-Ville Talvala | e41b318 | 2012-04-16 17:54:33 -0700 | [diff] [blame] | 36 | mMaxLockedBuffers(maxLockedBuffers), |
| 37 | mCurrentLockedBuffers(0) |
| 38 | { |
Eino-Ville Talvala | 042ecee | 2013-02-28 18:23:24 -0800 | [diff] [blame] | 39 | // Create tracking entries for locked buffers |
| 40 | mAcquiredBuffers.insertAt(0, maxLockedBuffers); |
Eino-Ville Talvala | e41b318 | 2012-04-16 17:54:33 -0700 | [diff] [blame] | 41 | |
Eino-Ville Talvala | f57e754 | 2012-08-20 15:44:40 -0700 | [diff] [blame] | 42 | mBufferQueue->setConsumerUsageBits(GRALLOC_USAGE_SW_READ_OFTEN); |
Jamie Gennis | 72f096f | 2012-08-27 18:48:37 -0700 | [diff] [blame] | 43 | mBufferQueue->setMaxAcquiredBufferCount(maxLockedBuffers); |
Eino-Ville Talvala | e41b318 | 2012-04-16 17:54:33 -0700 | [diff] [blame] | 44 | } |
| 45 | |
Eino-Ville Talvala | e232fdc | 2012-08-21 13:37:35 -0700 | [diff] [blame] | 46 | CpuConsumer::~CpuConsumer() { |
Eino-Ville Talvala | 042ecee | 2013-02-28 18:23:24 -0800 | [diff] [blame] | 47 | // ConsumerBase destructor does all the work. |
Eino-Ville Talvala | e232fdc | 2012-08-21 13:37:35 -0700 | [diff] [blame] | 48 | } |
| 49 | |
Eino-Ville Talvala | 042ecee | 2013-02-28 18:23:24 -0800 | [diff] [blame] | 50 | |
| 51 | |
Eino-Ville Talvala | e41b318 | 2012-04-16 17:54:33 -0700 | [diff] [blame] | 52 | void CpuConsumer::setName(const String8& name) { |
| 53 | Mutex::Autolock _l(mMutex); |
| 54 | mName = name; |
| 55 | mBufferQueue->setConsumerName(name); |
| 56 | } |
| 57 | |
Zhijun He | b4b6370 | 2013-06-06 11:59:21 -0700 | [diff] [blame] | 58 | status_t CpuConsumer::setDefaultBufferSize(uint32_t width, uint32_t height) |
| 59 | { |
| 60 | Mutex::Autolock _l(mMutex); |
| 61 | return mBufferQueue->setDefaultBufferSize(width, height); |
| 62 | } |
| 63 | |
| 64 | status_t CpuConsumer::setDefaultBufferFormat(uint32_t defaultFormat) |
| 65 | { |
| 66 | Mutex::Autolock _l(mMutex); |
| 67 | return mBufferQueue->setDefaultBufferFormat(defaultFormat); |
| 68 | } |
| 69 | |
Eino-Ville Talvala | e41b318 | 2012-04-16 17:54:33 -0700 | [diff] [blame] | 70 | status_t CpuConsumer::lockNextBuffer(LockedBuffer *nativeBuffer) { |
| 71 | status_t err; |
| 72 | |
| 73 | if (!nativeBuffer) return BAD_VALUE; |
| 74 | if (mCurrentLockedBuffers == mMaxLockedBuffers) { |
| 75 | return INVALID_OPERATION; |
| 76 | } |
| 77 | |
| 78 | BufferQueue::BufferItem b; |
| 79 | |
| 80 | Mutex::Autolock _l(mMutex); |
| 81 | |
Andy McFadden | 1585c4d | 2013-06-28 13:52:40 -0700 | [diff] [blame] | 82 | err = acquireBufferLocked(&b, 0); |
Eino-Ville Talvala | e41b318 | 2012-04-16 17:54:33 -0700 | [diff] [blame] | 83 | if (err != OK) { |
| 84 | if (err == BufferQueue::NO_BUFFER_AVAILABLE) { |
| 85 | return BAD_VALUE; |
| 86 | } else { |
| 87 | CC_LOGE("Error acquiring buffer: %s (%d)", strerror(err), err); |
| 88 | return err; |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | int buf = b.mBuf; |
| 93 | |
Jesse Hall | b42b1ac | 2012-06-28 14:27:53 -0700 | [diff] [blame] | 94 | if (b.mFence.get()) { |
Mathias Agopian | ea74d3b | 2013-05-16 18:03:22 -0700 | [diff] [blame] | 95 | err = b.mFence->waitForever("CpuConsumer::lockNextBuffer"); |
Jesse Hall | b42b1ac | 2012-06-28 14:27:53 -0700 | [diff] [blame] | 96 | if (err != OK) { |
| 97 | CC_LOGE("Failed to wait for fence of acquired buffer: %s (%d)", |
| 98 | strerror(-err), err); |
| 99 | return err; |
| 100 | } |
| 101 | } |
| 102 | |
Eino-Ville Talvala | 64d8b19 | 2013-02-28 14:08:34 -0800 | [diff] [blame] | 103 | void *bufferPointer = NULL; |
Eino-Ville Talvala | c43946b | 2013-05-04 18:07:43 -0700 | [diff] [blame] | 104 | android_ycbcr ycbcr = android_ycbcr(); |
Eino-Ville Talvala | e41b318 | 2012-04-16 17:54:33 -0700 | [diff] [blame] | 105 | |
Eino-Ville Talvala | c43946b | 2013-05-04 18:07:43 -0700 | [diff] [blame] | 106 | if (mSlots[buf].mGraphicBuffer->getPixelFormat() == |
| 107 | HAL_PIXEL_FORMAT_YCbCr_420_888) { |
| 108 | err = mSlots[buf].mGraphicBuffer->lockYCbCr( |
| 109 | GraphicBuffer::USAGE_SW_READ_OFTEN, |
| 110 | b.mCrop, |
| 111 | &ycbcr); |
| 112 | |
| 113 | if (err != OK) { |
| 114 | CC_LOGE("Unable to lock YCbCr buffer for CPU reading: %s (%d)", |
| 115 | strerror(-err), err); |
| 116 | return err; |
| 117 | } |
| 118 | bufferPointer = ycbcr.y; |
| 119 | } else { |
| 120 | err = mSlots[buf].mGraphicBuffer->lock( |
| 121 | GraphicBuffer::USAGE_SW_READ_OFTEN, |
| 122 | b.mCrop, |
| 123 | &bufferPointer); |
| 124 | |
| 125 | if (err != OK) { |
| 126 | CC_LOGE("Unable to lock buffer for CPU reading: %s (%d)", |
| 127 | strerror(-err), err); |
| 128 | return err; |
| 129 | } |
Eino-Ville Talvala | e41b318 | 2012-04-16 17:54:33 -0700 | [diff] [blame] | 130 | } |
Eino-Ville Talvala | c43946b | 2013-05-04 18:07:43 -0700 | [diff] [blame] | 131 | |
Eino-Ville Talvala | 042ecee | 2013-02-28 18:23:24 -0800 | [diff] [blame] | 132 | size_t lockedIdx = 0; |
| 133 | for (; lockedIdx < mMaxLockedBuffers; lockedIdx++) { |
| 134 | if (mAcquiredBuffers[lockedIdx].mSlot == |
| 135 | BufferQueue::INVALID_BUFFER_SLOT) { |
| 136 | break; |
| 137 | } |
| 138 | } |
| 139 | assert(lockedIdx < mMaxLockedBuffers); |
| 140 | |
| 141 | AcquiredBuffer &ab = mAcquiredBuffers.editItemAt(lockedIdx); |
| 142 | ab.mSlot = buf; |
| 143 | ab.mBufferPointer = bufferPointer; |
| 144 | ab.mGraphicBuffer = mSlots[buf].mGraphicBuffer; |
Eino-Ville Talvala | e41b318 | 2012-04-16 17:54:33 -0700 | [diff] [blame] | 145 | |
Eino-Ville Talvala | 64d8b19 | 2013-02-28 14:08:34 -0800 | [diff] [blame] | 146 | nativeBuffer->data = |
| 147 | reinterpret_cast<uint8_t*>(bufferPointer); |
Eino-Ville Talvala | f57e754 | 2012-08-20 15:44:40 -0700 | [diff] [blame] | 148 | nativeBuffer->width = mSlots[buf].mGraphicBuffer->getWidth(); |
| 149 | nativeBuffer->height = mSlots[buf].mGraphicBuffer->getHeight(); |
| 150 | nativeBuffer->format = mSlots[buf].mGraphicBuffer->getPixelFormat(); |
Eino-Ville Talvala | c43946b | 2013-05-04 18:07:43 -0700 | [diff] [blame] | 151 | nativeBuffer->stride = (ycbcr.y != NULL) ? |
| 152 | ycbcr.ystride : |
| 153 | mSlots[buf].mGraphicBuffer->getStride(); |
Eino-Ville Talvala | e41b318 | 2012-04-16 17:54:33 -0700 | [diff] [blame] | 154 | |
| 155 | nativeBuffer->crop = b.mCrop; |
| 156 | nativeBuffer->transform = b.mTransform; |
| 157 | nativeBuffer->scalingMode = b.mScalingMode; |
| 158 | nativeBuffer->timestamp = b.mTimestamp; |
| 159 | nativeBuffer->frameNumber = b.mFrameNumber; |
| 160 | |
Eino-Ville Talvala | c43946b | 2013-05-04 18:07:43 -0700 | [diff] [blame] | 161 | nativeBuffer->dataCb = reinterpret_cast<uint8_t*>(ycbcr.cb); |
| 162 | nativeBuffer->dataCr = reinterpret_cast<uint8_t*>(ycbcr.cr); |
| 163 | nativeBuffer->chromaStride = ycbcr.cstride; |
| 164 | nativeBuffer->chromaStep = ycbcr.chroma_step; |
| 165 | |
Eino-Ville Talvala | e41b318 | 2012-04-16 17:54:33 -0700 | [diff] [blame] | 166 | mCurrentLockedBuffers++; |
| 167 | |
| 168 | return OK; |
| 169 | } |
| 170 | |
| 171 | status_t CpuConsumer::unlockBuffer(const LockedBuffer &nativeBuffer) { |
| 172 | Mutex::Autolock _l(mMutex); |
Eino-Ville Talvala | 042ecee | 2013-02-28 18:23:24 -0800 | [diff] [blame] | 173 | size_t lockedIdx = 0; |
Eino-Ville Talvala | e41b318 | 2012-04-16 17:54:33 -0700 | [diff] [blame] | 174 | status_t err; |
| 175 | |
| 176 | void *bufPtr = reinterpret_cast<void *>(nativeBuffer.data); |
Eino-Ville Talvala | 042ecee | 2013-02-28 18:23:24 -0800 | [diff] [blame] | 177 | for (; lockedIdx < mMaxLockedBuffers; lockedIdx++) { |
| 178 | if (bufPtr == mAcquiredBuffers[lockedIdx].mBufferPointer) break; |
Eino-Ville Talvala | e41b318 | 2012-04-16 17:54:33 -0700 | [diff] [blame] | 179 | } |
Eino-Ville Talvala | 042ecee | 2013-02-28 18:23:24 -0800 | [diff] [blame] | 180 | if (lockedIdx == mMaxLockedBuffers) { |
Eino-Ville Talvala | e41b318 | 2012-04-16 17:54:33 -0700 | [diff] [blame] | 181 | CC_LOGE("%s: Can't find buffer to free", __FUNCTION__); |
| 182 | return BAD_VALUE; |
| 183 | } |
| 184 | |
Eino-Ville Talvala | 042ecee | 2013-02-28 18:23:24 -0800 | [diff] [blame] | 185 | return releaseAcquiredBufferLocked(lockedIdx); |
| 186 | } |
| 187 | |
| 188 | status_t CpuConsumer::releaseAcquiredBufferLocked(int lockedIdx) { |
| 189 | status_t err; |
| 190 | |
| 191 | err = mAcquiredBuffers[lockedIdx].mGraphicBuffer->unlock(); |
Eino-Ville Talvala | e41b318 | 2012-04-16 17:54:33 -0700 | [diff] [blame] | 192 | if (err != OK) { |
Eino-Ville Talvala | 042ecee | 2013-02-28 18:23:24 -0800 | [diff] [blame] | 193 | CC_LOGE("%s: Unable to unlock graphic buffer %d", __FUNCTION__, |
| 194 | lockedIdx); |
Eino-Ville Talvala | e41b318 | 2012-04-16 17:54:33 -0700 | [diff] [blame] | 195 | return err; |
| 196 | } |
Eino-Ville Talvala | 042ecee | 2013-02-28 18:23:24 -0800 | [diff] [blame] | 197 | int buf = mAcquiredBuffers[lockedIdx].mSlot; |
| 198 | |
| 199 | // release the buffer if it hasn't already been freed by the BufferQueue. |
| 200 | // This can happen, for example, when the producer of this buffer |
| 201 | // disconnected after this buffer was acquired. |
| 202 | if (CC_LIKELY(mAcquiredBuffers[lockedIdx].mGraphicBuffer == |
| 203 | mSlots[buf].mGraphicBuffer)) { |
Lajos Molnar | c5d7b7d | 2013-05-03 14:50:50 -0700 | [diff] [blame] | 204 | releaseBufferLocked( |
| 205 | buf, mAcquiredBuffers[lockedIdx].mGraphicBuffer, |
| 206 | EGL_NO_DISPLAY, EGL_NO_SYNC_KHR); |
Eino-Ville Talvala | 042ecee | 2013-02-28 18:23:24 -0800 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | AcquiredBuffer &ab = mAcquiredBuffers.editItemAt(lockedIdx); |
| 210 | ab.mSlot = BufferQueue::INVALID_BUFFER_SLOT; |
| 211 | ab.mBufferPointer = NULL; |
| 212 | ab.mGraphicBuffer.clear(); |
Eino-Ville Talvala | e41b318 | 2012-04-16 17:54:33 -0700 | [diff] [blame] | 213 | |
| 214 | mCurrentLockedBuffers--; |
Eino-Ville Talvala | e41b318 | 2012-04-16 17:54:33 -0700 | [diff] [blame] | 215 | return OK; |
| 216 | } |
| 217 | |
Eino-Ville Talvala | f57e754 | 2012-08-20 15:44:40 -0700 | [diff] [blame] | 218 | void CpuConsumer::freeBufferLocked(int slotIndex) { |
Eino-Ville Talvala | f57e754 | 2012-08-20 15:44:40 -0700 | [diff] [blame] | 219 | ConsumerBase::freeBufferLocked(slotIndex); |
Eino-Ville Talvala | e41b318 | 2012-04-16 17:54:33 -0700 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | } // namespace android |