| 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 | db89edc | 2013-08-02 01:40:18 -0700 | [diff] [blame] | 33 | CpuConsumer::CpuConsumer(const sp<IGraphicBufferConsumer>& 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 |  | 
| Mathias Agopian | db89edc | 2013-08-02 01:40:18 -0700 | [diff] [blame] | 42 | mConsumer->setConsumerUsageBits(GRALLOC_USAGE_SW_READ_OFTEN); | 
|  | 43 | mConsumer->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; | 
| Mathias Agopian | db89edc | 2013-08-02 01:40:18 -0700 | [diff] [blame] | 55 | mConsumer->setConsumerName(name); | 
| Eino-Ville Talvala | e41b318 | 2012-04-16 17:54:33 -0700 | [diff] [blame] | 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); | 
| Mathias Agopian | db89edc | 2013-08-02 01:40:18 -0700 | [diff] [blame] | 61 | return mConsumer->setDefaultBufferSize(width, height); | 
| Zhijun He | b4b6370 | 2013-06-06 11:59:21 -0700 | [diff] [blame] | 62 | } | 
|  | 63 |  | 
|  | 64 | status_t CpuConsumer::setDefaultBufferFormat(uint32_t defaultFormat) | 
|  | 65 | { | 
|  | 66 | Mutex::Autolock _l(mMutex); | 
| Mathias Agopian | db89edc | 2013-08-02 01:40:18 -0700 | [diff] [blame] | 67 | return mConsumer->setDefaultBufferFormat(defaultFormat); | 
| Zhijun He | b4b6370 | 2013-06-06 11:59:21 -0700 | [diff] [blame] | 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) { | 
| Igor Murashkin | a5b7513 | 2013-08-14 18:49:12 -0700 | [diff] [blame] | 75 | CC_LOGW("Max buffers have been locked (%d), cannot lock anymore.", | 
|  | 76 | mMaxLockedBuffers); | 
|  | 77 | return NOT_ENOUGH_DATA; | 
| Eino-Ville Talvala | e41b318 | 2012-04-16 17:54:33 -0700 | [diff] [blame] | 78 | } | 
|  | 79 |  | 
|  | 80 | BufferQueue::BufferItem b; | 
|  | 81 |  | 
|  | 82 | Mutex::Autolock _l(mMutex); | 
|  | 83 |  | 
| Andy McFadden | 1585c4d | 2013-06-28 13:52:40 -0700 | [diff] [blame] | 84 | err = acquireBufferLocked(&b, 0); | 
| Eino-Ville Talvala | e41b318 | 2012-04-16 17:54:33 -0700 | [diff] [blame] | 85 | if (err != OK) { | 
|  | 86 | if (err == BufferQueue::NO_BUFFER_AVAILABLE) { | 
|  | 87 | return BAD_VALUE; | 
|  | 88 | } else { | 
|  | 89 | CC_LOGE("Error acquiring buffer: %s (%d)", strerror(err), err); | 
|  | 90 | return err; | 
|  | 91 | } | 
|  | 92 | } | 
|  | 93 |  | 
|  | 94 | int buf = b.mBuf; | 
|  | 95 |  | 
| Eino-Ville Talvala | 64d8b19 | 2013-02-28 14:08:34 -0800 | [diff] [blame] | 96 | void *bufferPointer = NULL; | 
| Eino-Ville Talvala | c43946b | 2013-05-04 18:07:43 -0700 | [diff] [blame] | 97 | android_ycbcr ycbcr = android_ycbcr(); | 
| Eino-Ville Talvala | e41b318 | 2012-04-16 17:54:33 -0700 | [diff] [blame] | 98 |  | 
| Riley Andrews | d53e052 | 2014-08-18 16:57:11 -0700 | [diff] [blame] | 99 | if (b.mFence.get()) { | 
|  | 100 | if (mSlots[buf].mGraphicBuffer->getPixelFormat() == | 
|  | 101 | HAL_PIXEL_FORMAT_YCbCr_420_888) { | 
|  | 102 | err = mSlots[buf].mGraphicBuffer->lockAsyncYCbCr( | 
|  | 103 | GraphicBuffer::USAGE_SW_READ_OFTEN, | 
|  | 104 | b.mCrop, | 
|  | 105 | &ycbcr, | 
|  | 106 | b.mFence->dup()); | 
| Eino-Ville Talvala | c43946b | 2013-05-04 18:07:43 -0700 | [diff] [blame] | 107 |  | 
| Riley Andrews | d53e052 | 2014-08-18 16:57:11 -0700 | [diff] [blame] | 108 | if (err != OK) { | 
|  | 109 | CC_LOGE("Unable to lock YCbCr buffer for CPU reading: %s (%d)", | 
|  | 110 | strerror(-err), err); | 
|  | 111 | return err; | 
|  | 112 | } | 
|  | 113 | bufferPointer = ycbcr.y; | 
|  | 114 | } else { | 
|  | 115 | err = mSlots[buf].mGraphicBuffer->lockAsync( | 
|  | 116 | GraphicBuffer::USAGE_SW_READ_OFTEN, | 
|  | 117 | b.mCrop, | 
|  | 118 | &bufferPointer, | 
|  | 119 | b.mFence->dup()); | 
|  | 120 |  | 
|  | 121 | if (err != OK) { | 
|  | 122 | CC_LOGE("Unable to lock buffer for CPU reading: %s (%d)", | 
|  | 123 | strerror(-err), err); | 
|  | 124 | return err; | 
|  | 125 | } | 
| Eino-Ville Talvala | c43946b | 2013-05-04 18:07:43 -0700 | [diff] [blame] | 126 | } | 
| Eino-Ville Talvala | c43946b | 2013-05-04 18:07:43 -0700 | [diff] [blame] | 127 | } else { | 
| Riley Andrews | d53e052 | 2014-08-18 16:57:11 -0700 | [diff] [blame] | 128 | if (mSlots[buf].mGraphicBuffer->getPixelFormat() == | 
|  | 129 | HAL_PIXEL_FORMAT_YCbCr_420_888) { | 
|  | 130 | err = mSlots[buf].mGraphicBuffer->lockYCbCr( | 
|  | 131 | GraphicBuffer::USAGE_SW_READ_OFTEN, | 
|  | 132 | b.mCrop, | 
|  | 133 | &ycbcr); | 
| Eino-Ville Talvala | c43946b | 2013-05-04 18:07:43 -0700 | [diff] [blame] | 134 |  | 
| Riley Andrews | d53e052 | 2014-08-18 16:57:11 -0700 | [diff] [blame] | 135 | if (err != OK) { | 
|  | 136 | CC_LOGE("Unable to lock YCbCr buffer for CPU reading: %s (%d)", | 
|  | 137 | strerror(-err), err); | 
|  | 138 | return err; | 
|  | 139 | } | 
|  | 140 | bufferPointer = ycbcr.y; | 
|  | 141 | } else { | 
|  | 142 | err = mSlots[buf].mGraphicBuffer->lock( | 
|  | 143 | GraphicBuffer::USAGE_SW_READ_OFTEN, | 
|  | 144 | b.mCrop, | 
|  | 145 | &bufferPointer); | 
|  | 146 |  | 
|  | 147 | if (err != OK) { | 
|  | 148 | CC_LOGE("Unable to lock buffer for CPU reading: %s (%d)", | 
|  | 149 | strerror(-err), err); | 
|  | 150 | return err; | 
|  | 151 | } | 
| Eino-Ville Talvala | c43946b | 2013-05-04 18:07:43 -0700 | [diff] [blame] | 152 | } | 
| Eino-Ville Talvala | e41b318 | 2012-04-16 17:54:33 -0700 | [diff] [blame] | 153 | } | 
| Eino-Ville Talvala | c43946b | 2013-05-04 18:07:43 -0700 | [diff] [blame] | 154 |  | 
| Eino-Ville Talvala | 042ecee | 2013-02-28 18:23:24 -0800 | [diff] [blame] | 155 | size_t lockedIdx = 0; | 
|  | 156 | for (; lockedIdx < mMaxLockedBuffers; lockedIdx++) { | 
|  | 157 | if (mAcquiredBuffers[lockedIdx].mSlot == | 
|  | 158 | BufferQueue::INVALID_BUFFER_SLOT) { | 
|  | 159 | break; | 
|  | 160 | } | 
|  | 161 | } | 
|  | 162 | assert(lockedIdx < mMaxLockedBuffers); | 
|  | 163 |  | 
|  | 164 | AcquiredBuffer &ab = mAcquiredBuffers.editItemAt(lockedIdx); | 
|  | 165 | ab.mSlot = buf; | 
|  | 166 | ab.mBufferPointer = bufferPointer; | 
|  | 167 | ab.mGraphicBuffer = mSlots[buf].mGraphicBuffer; | 
| Eino-Ville Talvala | e41b318 | 2012-04-16 17:54:33 -0700 | [diff] [blame] | 168 |  | 
| Eino-Ville Talvala | 64d8b19 | 2013-02-28 14:08:34 -0800 | [diff] [blame] | 169 | nativeBuffer->data   = | 
|  | 170 | reinterpret_cast<uint8_t*>(bufferPointer); | 
| Eino-Ville Talvala | f57e754 | 2012-08-20 15:44:40 -0700 | [diff] [blame] | 171 | nativeBuffer->width  = mSlots[buf].mGraphicBuffer->getWidth(); | 
|  | 172 | nativeBuffer->height = mSlots[buf].mGraphicBuffer->getHeight(); | 
|  | 173 | nativeBuffer->format = mSlots[buf].mGraphicBuffer->getPixelFormat(); | 
| Eino-Ville Talvala | c43946b | 2013-05-04 18:07:43 -0700 | [diff] [blame] | 174 | nativeBuffer->stride = (ycbcr.y != NULL) ? | 
|  | 175 | ycbcr.ystride : | 
|  | 176 | mSlots[buf].mGraphicBuffer->getStride(); | 
| Eino-Ville Talvala | e41b318 | 2012-04-16 17:54:33 -0700 | [diff] [blame] | 177 |  | 
|  | 178 | nativeBuffer->crop        = b.mCrop; | 
|  | 179 | nativeBuffer->transform   = b.mTransform; | 
|  | 180 | nativeBuffer->scalingMode = b.mScalingMode; | 
|  | 181 | nativeBuffer->timestamp   = b.mTimestamp; | 
|  | 182 | nativeBuffer->frameNumber = b.mFrameNumber; | 
|  | 183 |  | 
| Eino-Ville Talvala | c43946b | 2013-05-04 18:07:43 -0700 | [diff] [blame] | 184 | nativeBuffer->dataCb       = reinterpret_cast<uint8_t*>(ycbcr.cb); | 
|  | 185 | nativeBuffer->dataCr       = reinterpret_cast<uint8_t*>(ycbcr.cr); | 
|  | 186 | nativeBuffer->chromaStride = ycbcr.cstride; | 
|  | 187 | nativeBuffer->chromaStep   = ycbcr.chroma_step; | 
|  | 188 |  | 
| Eino-Ville Talvala | e41b318 | 2012-04-16 17:54:33 -0700 | [diff] [blame] | 189 | mCurrentLockedBuffers++; | 
|  | 190 |  | 
|  | 191 | return OK; | 
|  | 192 | } | 
|  | 193 |  | 
|  | 194 | status_t CpuConsumer::unlockBuffer(const LockedBuffer &nativeBuffer) { | 
|  | 195 | Mutex::Autolock _l(mMutex); | 
| Eino-Ville Talvala | 042ecee | 2013-02-28 18:23:24 -0800 | [diff] [blame] | 196 | size_t lockedIdx = 0; | 
| Eino-Ville Talvala | e41b318 | 2012-04-16 17:54:33 -0700 | [diff] [blame] | 197 | status_t err; | 
|  | 198 |  | 
|  | 199 | void *bufPtr = reinterpret_cast<void *>(nativeBuffer.data); | 
| Eino-Ville Talvala | 042ecee | 2013-02-28 18:23:24 -0800 | [diff] [blame] | 200 | for (; lockedIdx < mMaxLockedBuffers; lockedIdx++) { | 
|  | 201 | if (bufPtr == mAcquiredBuffers[lockedIdx].mBufferPointer) break; | 
| Eino-Ville Talvala | e41b318 | 2012-04-16 17:54:33 -0700 | [diff] [blame] | 202 | } | 
| Eino-Ville Talvala | 042ecee | 2013-02-28 18:23:24 -0800 | [diff] [blame] | 203 | if (lockedIdx == mMaxLockedBuffers) { | 
| Eino-Ville Talvala | e41b318 | 2012-04-16 17:54:33 -0700 | [diff] [blame] | 204 | CC_LOGE("%s: Can't find buffer to free", __FUNCTION__); | 
|  | 205 | return BAD_VALUE; | 
|  | 206 | } | 
|  | 207 |  | 
| Eino-Ville Talvala | 042ecee | 2013-02-28 18:23:24 -0800 | [diff] [blame] | 208 | return releaseAcquiredBufferLocked(lockedIdx); | 
|  | 209 | } | 
|  | 210 |  | 
|  | 211 | status_t CpuConsumer::releaseAcquiredBufferLocked(int lockedIdx) { | 
|  | 212 | status_t err; | 
| Riley Andrews | d53e052 | 2014-08-18 16:57:11 -0700 | [diff] [blame] | 213 | int fd = -1; | 
| Eino-Ville Talvala | 042ecee | 2013-02-28 18:23:24 -0800 | [diff] [blame] | 214 |  | 
| Riley Andrews | d53e052 | 2014-08-18 16:57:11 -0700 | [diff] [blame] | 215 | err = mAcquiredBuffers[lockedIdx].mGraphicBuffer->unlockAsync(&fd); | 
| Eino-Ville Talvala | e41b318 | 2012-04-16 17:54:33 -0700 | [diff] [blame] | 216 | if (err != OK) { | 
| Eino-Ville Talvala | 042ecee | 2013-02-28 18:23:24 -0800 | [diff] [blame] | 217 | CC_LOGE("%s: Unable to unlock graphic buffer %d", __FUNCTION__, | 
|  | 218 | lockedIdx); | 
| Eino-Ville Talvala | e41b318 | 2012-04-16 17:54:33 -0700 | [diff] [blame] | 219 | return err; | 
|  | 220 | } | 
| Eino-Ville Talvala | 042ecee | 2013-02-28 18:23:24 -0800 | [diff] [blame] | 221 | int buf = mAcquiredBuffers[lockedIdx].mSlot; | 
| Riley Andrews | d53e052 | 2014-08-18 16:57:11 -0700 | [diff] [blame] | 222 | if (CC_LIKELY(fd != -1)) { | 
|  | 223 | sp<Fence> fence(new Fence(fd)); | 
|  | 224 | addReleaseFenceLocked( | 
|  | 225 | mAcquiredBuffers[lockedIdx].mSlot, | 
|  | 226 | mSlots[buf].mGraphicBuffer, | 
|  | 227 | fence); | 
|  | 228 | } | 
| Eino-Ville Talvala | 042ecee | 2013-02-28 18:23:24 -0800 | [diff] [blame] | 229 |  | 
|  | 230 | // release the buffer if it hasn't already been freed by the BufferQueue. | 
|  | 231 | // This can happen, for example, when the producer of this buffer | 
|  | 232 | // disconnected after this buffer was acquired. | 
|  | 233 | if (CC_LIKELY(mAcquiredBuffers[lockedIdx].mGraphicBuffer == | 
|  | 234 | mSlots[buf].mGraphicBuffer)) { | 
| Lajos Molnar | c5d7b7d | 2013-05-03 14:50:50 -0700 | [diff] [blame] | 235 | releaseBufferLocked( | 
|  | 236 | buf, mAcquiredBuffers[lockedIdx].mGraphicBuffer, | 
|  | 237 | EGL_NO_DISPLAY, EGL_NO_SYNC_KHR); | 
| Eino-Ville Talvala | 042ecee | 2013-02-28 18:23:24 -0800 | [diff] [blame] | 238 | } | 
|  | 239 |  | 
|  | 240 | AcquiredBuffer &ab = mAcquiredBuffers.editItemAt(lockedIdx); | 
|  | 241 | ab.mSlot = BufferQueue::INVALID_BUFFER_SLOT; | 
|  | 242 | ab.mBufferPointer = NULL; | 
|  | 243 | ab.mGraphicBuffer.clear(); | 
| Eino-Ville Talvala | e41b318 | 2012-04-16 17:54:33 -0700 | [diff] [blame] | 244 |  | 
|  | 245 | mCurrentLockedBuffers--; | 
| Eino-Ville Talvala | e41b318 | 2012-04-16 17:54:33 -0700 | [diff] [blame] | 246 | return OK; | 
|  | 247 | } | 
|  | 248 |  | 
| Eino-Ville Talvala | f57e754 | 2012-08-20 15:44:40 -0700 | [diff] [blame] | 249 | void CpuConsumer::freeBufferLocked(int slotIndex) { | 
| Eino-Ville Talvala | f57e754 | 2012-08-20 15:44:40 -0700 | [diff] [blame] | 250 | ConsumerBase::freeBufferLocked(slotIndex); | 
| Eino-Ville Talvala | e41b318 | 2012-04-16 17:54:33 -0700 | [diff] [blame] | 251 | } | 
|  | 252 |  | 
|  | 253 | } // namespace android |