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 | |
Eino-Ville Talvala | eb0d129 | 2013-02-28 11:01:32 -0800 | [diff] [blame] | 33 | CpuConsumer::CpuConsumer(uint32_t maxLockedBuffers, bool synchronousMode) : |
Jamie Gennis | 72f096f | 2012-08-27 18:48:37 -0700 | [diff] [blame] | 34 | ConsumerBase(new BufferQueue(true) ), |
Eino-Ville Talvala | e41b318 | 2012-04-16 17:54:33 -0700 | [diff] [blame] | 35 | mMaxLockedBuffers(maxLockedBuffers), |
| 36 | mCurrentLockedBuffers(0) |
| 37 | { |
Eino-Ville Talvala | 042ecee | 2013-02-28 18:23:24 -0800 | [diff] [blame] | 38 | // Create tracking entries for locked buffers |
| 39 | mAcquiredBuffers.insertAt(0, maxLockedBuffers); |
Eino-Ville Talvala | e41b318 | 2012-04-16 17:54:33 -0700 | [diff] [blame] | 40 | |
Eino-Ville Talvala | eb0d129 | 2013-02-28 11:01:32 -0800 | [diff] [blame] | 41 | mBufferQueue->setSynchronousMode(synchronousMode); |
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 | |
| 58 | status_t CpuConsumer::lockNextBuffer(LockedBuffer *nativeBuffer) { |
| 59 | status_t err; |
| 60 | |
| 61 | if (!nativeBuffer) return BAD_VALUE; |
| 62 | if (mCurrentLockedBuffers == mMaxLockedBuffers) { |
| 63 | return INVALID_OPERATION; |
| 64 | } |
| 65 | |
| 66 | BufferQueue::BufferItem b; |
| 67 | |
| 68 | Mutex::Autolock _l(mMutex); |
| 69 | |
Eino-Ville Talvala | f57e754 | 2012-08-20 15:44:40 -0700 | [diff] [blame] | 70 | err = acquireBufferLocked(&b); |
Eino-Ville Talvala | e41b318 | 2012-04-16 17:54:33 -0700 | [diff] [blame] | 71 | if (err != OK) { |
| 72 | if (err == BufferQueue::NO_BUFFER_AVAILABLE) { |
| 73 | return BAD_VALUE; |
| 74 | } else { |
| 75 | CC_LOGE("Error acquiring buffer: %s (%d)", strerror(err), err); |
| 76 | return err; |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | int buf = b.mBuf; |
| 81 | |
Jesse Hall | b42b1ac | 2012-06-28 14:27:53 -0700 | [diff] [blame] | 82 | if (b.mFence.get()) { |
Mathias Agopian | ea74d3b | 2013-05-16 18:03:22 -0700 | [diff] [blame] | 83 | err = b.mFence->waitForever("CpuConsumer::lockNextBuffer"); |
Jesse Hall | b42b1ac | 2012-06-28 14:27:53 -0700 | [diff] [blame] | 84 | if (err != OK) { |
| 85 | CC_LOGE("Failed to wait for fence of acquired buffer: %s (%d)", |
| 86 | strerror(-err), err); |
| 87 | return err; |
| 88 | } |
| 89 | } |
| 90 | |
Eino-Ville Talvala | 64d8b19 | 2013-02-28 14:08:34 -0800 | [diff] [blame] | 91 | void *bufferPointer = NULL; |
Eino-Ville Talvala | c43946b | 2013-05-04 18:07:43 -0700 | [diff] [blame] | 92 | android_ycbcr ycbcr = android_ycbcr(); |
Eino-Ville Talvala | e41b318 | 2012-04-16 17:54:33 -0700 | [diff] [blame] | 93 | |
Eino-Ville Talvala | c43946b | 2013-05-04 18:07:43 -0700 | [diff] [blame] | 94 | if (mSlots[buf].mGraphicBuffer->getPixelFormat() == |
| 95 | HAL_PIXEL_FORMAT_YCbCr_420_888) { |
| 96 | err = mSlots[buf].mGraphicBuffer->lockYCbCr( |
| 97 | GraphicBuffer::USAGE_SW_READ_OFTEN, |
| 98 | b.mCrop, |
| 99 | &ycbcr); |
| 100 | |
| 101 | if (err != OK) { |
| 102 | CC_LOGE("Unable to lock YCbCr buffer for CPU reading: %s (%d)", |
| 103 | strerror(-err), err); |
| 104 | return err; |
| 105 | } |
| 106 | bufferPointer = ycbcr.y; |
| 107 | } else { |
| 108 | err = mSlots[buf].mGraphicBuffer->lock( |
| 109 | GraphicBuffer::USAGE_SW_READ_OFTEN, |
| 110 | b.mCrop, |
| 111 | &bufferPointer); |
| 112 | |
| 113 | if (err != OK) { |
| 114 | CC_LOGE("Unable to lock buffer for CPU reading: %s (%d)", |
| 115 | strerror(-err), err); |
| 116 | return err; |
| 117 | } |
Eino-Ville Talvala | e41b318 | 2012-04-16 17:54:33 -0700 | [diff] [blame] | 118 | } |
Eino-Ville Talvala | c43946b | 2013-05-04 18:07:43 -0700 | [diff] [blame] | 119 | |
Eino-Ville Talvala | 042ecee | 2013-02-28 18:23:24 -0800 | [diff] [blame] | 120 | size_t lockedIdx = 0; |
| 121 | for (; lockedIdx < mMaxLockedBuffers; lockedIdx++) { |
| 122 | if (mAcquiredBuffers[lockedIdx].mSlot == |
| 123 | BufferQueue::INVALID_BUFFER_SLOT) { |
| 124 | break; |
| 125 | } |
| 126 | } |
| 127 | assert(lockedIdx < mMaxLockedBuffers); |
| 128 | |
| 129 | AcquiredBuffer &ab = mAcquiredBuffers.editItemAt(lockedIdx); |
| 130 | ab.mSlot = buf; |
| 131 | ab.mBufferPointer = bufferPointer; |
| 132 | ab.mGraphicBuffer = mSlots[buf].mGraphicBuffer; |
Eino-Ville Talvala | e41b318 | 2012-04-16 17:54:33 -0700 | [diff] [blame] | 133 | |
Eino-Ville Talvala | 64d8b19 | 2013-02-28 14:08:34 -0800 | [diff] [blame] | 134 | nativeBuffer->data = |
| 135 | reinterpret_cast<uint8_t*>(bufferPointer); |
Eino-Ville Talvala | f57e754 | 2012-08-20 15:44:40 -0700 | [diff] [blame] | 136 | nativeBuffer->width = mSlots[buf].mGraphicBuffer->getWidth(); |
| 137 | nativeBuffer->height = mSlots[buf].mGraphicBuffer->getHeight(); |
| 138 | nativeBuffer->format = mSlots[buf].mGraphicBuffer->getPixelFormat(); |
Eino-Ville Talvala | c43946b | 2013-05-04 18:07:43 -0700 | [diff] [blame] | 139 | nativeBuffer->stride = (ycbcr.y != NULL) ? |
| 140 | ycbcr.ystride : |
| 141 | mSlots[buf].mGraphicBuffer->getStride(); |
Eino-Ville Talvala | e41b318 | 2012-04-16 17:54:33 -0700 | [diff] [blame] | 142 | |
| 143 | nativeBuffer->crop = b.mCrop; |
| 144 | nativeBuffer->transform = b.mTransform; |
| 145 | nativeBuffer->scalingMode = b.mScalingMode; |
| 146 | nativeBuffer->timestamp = b.mTimestamp; |
| 147 | nativeBuffer->frameNumber = b.mFrameNumber; |
| 148 | |
Eino-Ville Talvala | c43946b | 2013-05-04 18:07:43 -0700 | [diff] [blame] | 149 | nativeBuffer->dataCb = reinterpret_cast<uint8_t*>(ycbcr.cb); |
| 150 | nativeBuffer->dataCr = reinterpret_cast<uint8_t*>(ycbcr.cr); |
| 151 | nativeBuffer->chromaStride = ycbcr.cstride; |
| 152 | nativeBuffer->chromaStep = ycbcr.chroma_step; |
| 153 | |
Eino-Ville Talvala | e41b318 | 2012-04-16 17:54:33 -0700 | [diff] [blame] | 154 | mCurrentLockedBuffers++; |
| 155 | |
| 156 | return OK; |
| 157 | } |
| 158 | |
| 159 | status_t CpuConsumer::unlockBuffer(const LockedBuffer &nativeBuffer) { |
| 160 | Mutex::Autolock _l(mMutex); |
Eino-Ville Talvala | 042ecee | 2013-02-28 18:23:24 -0800 | [diff] [blame] | 161 | size_t lockedIdx = 0; |
Eino-Ville Talvala | e41b318 | 2012-04-16 17:54:33 -0700 | [diff] [blame] | 162 | status_t err; |
| 163 | |
| 164 | void *bufPtr = reinterpret_cast<void *>(nativeBuffer.data); |
Eino-Ville Talvala | 042ecee | 2013-02-28 18:23:24 -0800 | [diff] [blame] | 165 | for (; lockedIdx < mMaxLockedBuffers; lockedIdx++) { |
| 166 | if (bufPtr == mAcquiredBuffers[lockedIdx].mBufferPointer) break; |
Eino-Ville Talvala | e41b318 | 2012-04-16 17:54:33 -0700 | [diff] [blame] | 167 | } |
Eino-Ville Talvala | 042ecee | 2013-02-28 18:23:24 -0800 | [diff] [blame] | 168 | if (lockedIdx == mMaxLockedBuffers) { |
Eino-Ville Talvala | e41b318 | 2012-04-16 17:54:33 -0700 | [diff] [blame] | 169 | CC_LOGE("%s: Can't find buffer to free", __FUNCTION__); |
| 170 | return BAD_VALUE; |
| 171 | } |
| 172 | |
Eino-Ville Talvala | 042ecee | 2013-02-28 18:23:24 -0800 | [diff] [blame] | 173 | return releaseAcquiredBufferLocked(lockedIdx); |
| 174 | } |
| 175 | |
| 176 | status_t CpuConsumer::releaseAcquiredBufferLocked(int lockedIdx) { |
| 177 | status_t err; |
| 178 | |
| 179 | err = mAcquiredBuffers[lockedIdx].mGraphicBuffer->unlock(); |
Eino-Ville Talvala | e41b318 | 2012-04-16 17:54:33 -0700 | [diff] [blame] | 180 | if (err != OK) { |
Eino-Ville Talvala | 042ecee | 2013-02-28 18:23:24 -0800 | [diff] [blame] | 181 | CC_LOGE("%s: Unable to unlock graphic buffer %d", __FUNCTION__, |
| 182 | lockedIdx); |
Eino-Ville Talvala | e41b318 | 2012-04-16 17:54:33 -0700 | [diff] [blame] | 183 | return err; |
| 184 | } |
Eino-Ville Talvala | 042ecee | 2013-02-28 18:23:24 -0800 | [diff] [blame] | 185 | int buf = mAcquiredBuffers[lockedIdx].mSlot; |
| 186 | |
| 187 | // release the buffer if it hasn't already been freed by the BufferQueue. |
| 188 | // This can happen, for example, when the producer of this buffer |
| 189 | // disconnected after this buffer was acquired. |
| 190 | if (CC_LIKELY(mAcquiredBuffers[lockedIdx].mGraphicBuffer == |
| 191 | mSlots[buf].mGraphicBuffer)) { |
Lajos Molnar | c5d7b7d | 2013-05-03 14:50:50 -0700 | [diff] [blame^] | 192 | releaseBufferLocked( |
| 193 | buf, mAcquiredBuffers[lockedIdx].mGraphicBuffer, |
| 194 | EGL_NO_DISPLAY, EGL_NO_SYNC_KHR); |
Eino-Ville Talvala | 042ecee | 2013-02-28 18:23:24 -0800 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | AcquiredBuffer &ab = mAcquiredBuffers.editItemAt(lockedIdx); |
| 198 | ab.mSlot = BufferQueue::INVALID_BUFFER_SLOT; |
| 199 | ab.mBufferPointer = NULL; |
| 200 | ab.mGraphicBuffer.clear(); |
Eino-Ville Talvala | e41b318 | 2012-04-16 17:54:33 -0700 | [diff] [blame] | 201 | |
| 202 | mCurrentLockedBuffers--; |
Eino-Ville Talvala | e41b318 | 2012-04-16 17:54:33 -0700 | [diff] [blame] | 203 | return OK; |
| 204 | } |
| 205 | |
Eino-Ville Talvala | f57e754 | 2012-08-20 15:44:40 -0700 | [diff] [blame] | 206 | void CpuConsumer::freeBufferLocked(int slotIndex) { |
Eino-Ville Talvala | f57e754 | 2012-08-20 15:44:40 -0700 | [diff] [blame] | 207 | ConsumerBase::freeBufferLocked(slotIndex); |
Eino-Ville Talvala | e41b318 | 2012-04-16 17:54:33 -0700 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | } // namespace android |