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 |
| 20 | #include <utils/Log.h> |
| 21 | |
| 22 | #include <gui/CpuConsumer.h> |
| 23 | |
| 24 | #define CC_LOGV(x, ...) ALOGV("[%s] "x, mName.string(), ##__VA_ARGS__) |
| 25 | #define CC_LOGD(x, ...) ALOGD("[%s] "x, mName.string(), ##__VA_ARGS__) |
| 26 | #define CC_LOGI(x, ...) ALOGI("[%s] "x, mName.string(), ##__VA_ARGS__) |
| 27 | #define CC_LOGW(x, ...) ALOGW("[%s] "x, mName.string(), ##__VA_ARGS__) |
| 28 | #define CC_LOGE(x, ...) ALOGE("[%s] "x, mName.string(), ##__VA_ARGS__) |
| 29 | |
| 30 | namespace android { |
| 31 | |
Eino-Ville Talvala | eb0d129 | 2013-02-28 11:01:32 -0800 | [diff] [blame] | 32 | CpuConsumer::CpuConsumer(uint32_t maxLockedBuffers, bool synchronousMode) : |
Jamie Gennis | 72f096f | 2012-08-27 18:48:37 -0700 | [diff] [blame] | 33 | ConsumerBase(new BufferQueue(true) ), |
Eino-Ville Talvala | e41b318 | 2012-04-16 17:54:33 -0700 | [diff] [blame] | 34 | mMaxLockedBuffers(maxLockedBuffers), |
| 35 | mCurrentLockedBuffers(0) |
| 36 | { |
Eino-Ville Talvala | 64d8b19 | 2013-02-28 14:08:34 -0800 | [diff] [blame] | 37 | for (size_t i=0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) { |
| 38 | mLockedSlots[i].mBufferPointer = NULL; |
Eino-Ville Talvala | e41b318 | 2012-04-16 17:54:33 -0700 | [diff] [blame] | 39 | } |
| 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 | 64d8b19 | 2013-02-28 14:08:34 -0800 | [diff] [blame] | 47 | status_t err; |
| 48 | for (size_t i=0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) { |
| 49 | if (mLockedSlots[i].mBufferPointer != NULL) { |
| 50 | mLockedSlots[i].mBufferPointer = NULL; |
| 51 | err = mLockedSlots[i].mGraphicBuffer->unlock(); |
| 52 | mLockedSlots[i].mGraphicBuffer.clear(); |
| 53 | if (err != OK) { |
| 54 | CC_LOGE("%s: Unable to unlock graphic buffer %d", __FUNCTION__, |
| 55 | i); |
| 56 | } |
| 57 | |
| 58 | } |
| 59 | } |
Eino-Ville Talvala | e232fdc | 2012-08-21 13:37:35 -0700 | [diff] [blame] | 60 | } |
| 61 | |
Eino-Ville Talvala | e41b318 | 2012-04-16 17:54:33 -0700 | [diff] [blame] | 62 | void CpuConsumer::setName(const String8& name) { |
| 63 | Mutex::Autolock _l(mMutex); |
| 64 | mName = name; |
| 65 | mBufferQueue->setConsumerName(name); |
| 66 | } |
| 67 | |
| 68 | status_t CpuConsumer::lockNextBuffer(LockedBuffer *nativeBuffer) { |
| 69 | status_t err; |
| 70 | |
| 71 | if (!nativeBuffer) return BAD_VALUE; |
| 72 | if (mCurrentLockedBuffers == mMaxLockedBuffers) { |
| 73 | return INVALID_OPERATION; |
| 74 | } |
| 75 | |
| 76 | BufferQueue::BufferItem b; |
| 77 | |
| 78 | Mutex::Autolock _l(mMutex); |
| 79 | |
Eino-Ville Talvala | f57e754 | 2012-08-20 15:44:40 -0700 | [diff] [blame] | 80 | err = acquireBufferLocked(&b); |
Eino-Ville Talvala | e41b318 | 2012-04-16 17:54:33 -0700 | [diff] [blame] | 81 | if (err != OK) { |
| 82 | if (err == BufferQueue::NO_BUFFER_AVAILABLE) { |
| 83 | return BAD_VALUE; |
| 84 | } else { |
| 85 | CC_LOGE("Error acquiring buffer: %s (%d)", strerror(err), err); |
| 86 | return err; |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | int buf = b.mBuf; |
| 91 | |
Jesse Hall | b42b1ac | 2012-06-28 14:27:53 -0700 | [diff] [blame] | 92 | if (b.mFence.get()) { |
Jesse Hall | ba607d5 | 2012-10-01 14:05:20 -0700 | [diff] [blame] | 93 | err = b.mFence->waitForever(1000, "CpuConsumer::lockNextBuffer"); |
Jesse Hall | b42b1ac | 2012-06-28 14:27:53 -0700 | [diff] [blame] | 94 | if (err != OK) { |
| 95 | CC_LOGE("Failed to wait for fence of acquired buffer: %s (%d)", |
| 96 | strerror(-err), err); |
| 97 | return err; |
| 98 | } |
| 99 | } |
| 100 | |
Eino-Ville Talvala | 64d8b19 | 2013-02-28 14:08:34 -0800 | [diff] [blame] | 101 | void *bufferPointer = NULL; |
Eino-Ville Talvala | f57e754 | 2012-08-20 15:44:40 -0700 | [diff] [blame] | 102 | err = mSlots[buf].mGraphicBuffer->lock( |
Eino-Ville Talvala | e41b318 | 2012-04-16 17:54:33 -0700 | [diff] [blame] | 103 | GraphicBuffer::USAGE_SW_READ_OFTEN, |
| 104 | b.mCrop, |
Eino-Ville Talvala | 64d8b19 | 2013-02-28 14:08:34 -0800 | [diff] [blame] | 105 | &bufferPointer); |
Eino-Ville Talvala | e41b318 | 2012-04-16 17:54:33 -0700 | [diff] [blame] | 106 | |
Eino-Ville Talvala | 64d8b19 | 2013-02-28 14:08:34 -0800 | [diff] [blame] | 107 | if (bufferPointer != NULL && err != OK) { |
Eino-Ville Talvala | e41b318 | 2012-04-16 17:54:33 -0700 | [diff] [blame] | 108 | CC_LOGE("Unable to lock buffer for CPU reading: %s (%d)", strerror(-err), |
| 109 | err); |
| 110 | return err; |
| 111 | } |
Eino-Ville Talvala | 64d8b19 | 2013-02-28 14:08:34 -0800 | [diff] [blame] | 112 | mLockedSlots[buf].mBufferPointer = bufferPointer; |
| 113 | mLockedSlots[buf].mGraphicBuffer = mSlots[buf].mGraphicBuffer; |
Eino-Ville Talvala | e41b318 | 2012-04-16 17:54:33 -0700 | [diff] [blame] | 114 | |
Eino-Ville Talvala | 64d8b19 | 2013-02-28 14:08:34 -0800 | [diff] [blame] | 115 | nativeBuffer->data = |
| 116 | reinterpret_cast<uint8_t*>(bufferPointer); |
Eino-Ville Talvala | f57e754 | 2012-08-20 15:44:40 -0700 | [diff] [blame] | 117 | nativeBuffer->width = mSlots[buf].mGraphicBuffer->getWidth(); |
| 118 | nativeBuffer->height = mSlots[buf].mGraphicBuffer->getHeight(); |
| 119 | nativeBuffer->format = mSlots[buf].mGraphicBuffer->getPixelFormat(); |
| 120 | nativeBuffer->stride = mSlots[buf].mGraphicBuffer->getStride(); |
Eino-Ville Talvala | e41b318 | 2012-04-16 17:54:33 -0700 | [diff] [blame] | 121 | |
| 122 | nativeBuffer->crop = b.mCrop; |
| 123 | nativeBuffer->transform = b.mTransform; |
| 124 | nativeBuffer->scalingMode = b.mScalingMode; |
| 125 | nativeBuffer->timestamp = b.mTimestamp; |
| 126 | nativeBuffer->frameNumber = b.mFrameNumber; |
| 127 | |
| 128 | mCurrentLockedBuffers++; |
| 129 | |
| 130 | return OK; |
| 131 | } |
| 132 | |
| 133 | status_t CpuConsumer::unlockBuffer(const LockedBuffer &nativeBuffer) { |
| 134 | Mutex::Autolock _l(mMutex); |
Eino-Ville Talvala | f57e754 | 2012-08-20 15:44:40 -0700 | [diff] [blame] | 135 | int slotIndex = 0; |
Eino-Ville Talvala | e41b318 | 2012-04-16 17:54:33 -0700 | [diff] [blame] | 136 | status_t err; |
| 137 | |
| 138 | void *bufPtr = reinterpret_cast<void *>(nativeBuffer.data); |
Eino-Ville Talvala | f57e754 | 2012-08-20 15:44:40 -0700 | [diff] [blame] | 139 | for (; slotIndex < BufferQueue::NUM_BUFFER_SLOTS; slotIndex++) { |
Eino-Ville Talvala | 64d8b19 | 2013-02-28 14:08:34 -0800 | [diff] [blame] | 140 | if (bufPtr == mLockedSlots[slotIndex].mBufferPointer) break; |
Eino-Ville Talvala | e41b318 | 2012-04-16 17:54:33 -0700 | [diff] [blame] | 141 | } |
Eino-Ville Talvala | f57e754 | 2012-08-20 15:44:40 -0700 | [diff] [blame] | 142 | if (slotIndex == BufferQueue::NUM_BUFFER_SLOTS) { |
Eino-Ville Talvala | e41b318 | 2012-04-16 17:54:33 -0700 | [diff] [blame] | 143 | CC_LOGE("%s: Can't find buffer to free", __FUNCTION__); |
| 144 | return BAD_VALUE; |
| 145 | } |
| 146 | |
Eino-Ville Talvala | 64d8b19 | 2013-02-28 14:08:34 -0800 | [diff] [blame] | 147 | mLockedSlots[slotIndex].mBufferPointer = NULL; |
| 148 | err = mLockedSlots[slotIndex].mGraphicBuffer->unlock(); |
| 149 | mLockedSlots[slotIndex].mGraphicBuffer.clear(); |
Eino-Ville Talvala | e41b318 | 2012-04-16 17:54:33 -0700 | [diff] [blame] | 150 | if (err != OK) { |
Eino-Ville Talvala | f57e754 | 2012-08-20 15:44:40 -0700 | [diff] [blame] | 151 | CC_LOGE("%s: Unable to unlock graphic buffer %d", __FUNCTION__, slotIndex); |
Eino-Ville Talvala | e41b318 | 2012-04-16 17:54:33 -0700 | [diff] [blame] | 152 | return err; |
| 153 | } |
Jamie Gennis | b272541 | 2012-09-05 20:09:05 -0700 | [diff] [blame] | 154 | releaseBufferLocked(slotIndex, EGL_NO_DISPLAY, EGL_NO_SYNC_KHR); |
Eino-Ville Talvala | e41b318 | 2012-04-16 17:54:33 -0700 | [diff] [blame] | 155 | |
| 156 | mCurrentLockedBuffers--; |
| 157 | |
| 158 | return OK; |
| 159 | } |
| 160 | |
Eino-Ville Talvala | f57e754 | 2012-08-20 15:44:40 -0700 | [diff] [blame] | 161 | void CpuConsumer::freeBufferLocked(int slotIndex) { |
Eino-Ville Talvala | f57e754 | 2012-08-20 15:44:40 -0700 | [diff] [blame] | 162 | ConsumerBase::freeBufferLocked(slotIndex); |
Eino-Ville Talvala | e41b318 | 2012-04-16 17:54:33 -0700 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | } // namespace android |