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 | e41b318 | 2012-04-16 17:54:33 -0700 | [diff] [blame] | 32 | CpuConsumer::CpuConsumer(uint32_t maxLockedBuffers) : |
Eino-Ville Talvala | f57e754 | 2012-08-20 15:44:40 -0700 | [diff] [blame^] | 33 | ConsumerBase(new BufferQueue(true, maxLockedBuffers) ), |
Eino-Ville Talvala | e41b318 | 2012-04-16 17:54:33 -0700 | [diff] [blame] | 34 | mMaxLockedBuffers(maxLockedBuffers), |
| 35 | mCurrentLockedBuffers(0) |
| 36 | { |
Eino-Ville Talvala | e41b318 | 2012-04-16 17:54:33 -0700 | [diff] [blame] | 37 | |
| 38 | for (int i = 0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) { |
| 39 | mBufferPointers[i] = NULL; |
| 40 | } |
| 41 | |
Eino-Ville Talvala | f57e754 | 2012-08-20 15:44:40 -0700 | [diff] [blame^] | 42 | mBufferQueue->setSynchronousMode(true); |
| 43 | mBufferQueue->setConsumerUsageBits(GRALLOC_USAGE_SW_READ_OFTEN); |
Eino-Ville Talvala | e41b318 | 2012-04-16 17:54:33 -0700 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | void CpuConsumer::setName(const String8& name) { |
| 47 | Mutex::Autolock _l(mMutex); |
| 48 | mName = name; |
| 49 | mBufferQueue->setConsumerName(name); |
| 50 | } |
| 51 | |
| 52 | status_t CpuConsumer::lockNextBuffer(LockedBuffer *nativeBuffer) { |
| 53 | status_t err; |
| 54 | |
| 55 | if (!nativeBuffer) return BAD_VALUE; |
| 56 | if (mCurrentLockedBuffers == mMaxLockedBuffers) { |
| 57 | return INVALID_OPERATION; |
| 58 | } |
| 59 | |
| 60 | BufferQueue::BufferItem b; |
| 61 | |
| 62 | Mutex::Autolock _l(mMutex); |
| 63 | |
Eino-Ville Talvala | f57e754 | 2012-08-20 15:44:40 -0700 | [diff] [blame^] | 64 | err = acquireBufferLocked(&b); |
Eino-Ville Talvala | e41b318 | 2012-04-16 17:54:33 -0700 | [diff] [blame] | 65 | if (err != OK) { |
| 66 | if (err == BufferQueue::NO_BUFFER_AVAILABLE) { |
| 67 | return BAD_VALUE; |
| 68 | } else { |
| 69 | CC_LOGE("Error acquiring buffer: %s (%d)", strerror(err), err); |
| 70 | return err; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | int buf = b.mBuf; |
| 75 | |
Jesse Hall | b42b1ac | 2012-06-28 14:27:53 -0700 | [diff] [blame] | 76 | if (b.mFence.get()) { |
| 77 | err = b.mFence->wait(Fence::TIMEOUT_NEVER); |
| 78 | if (err != OK) { |
| 79 | CC_LOGE("Failed to wait for fence of acquired buffer: %s (%d)", |
| 80 | strerror(-err), err); |
| 81 | return err; |
| 82 | } |
| 83 | } |
| 84 | |
Eino-Ville Talvala | f57e754 | 2012-08-20 15:44:40 -0700 | [diff] [blame^] | 85 | err = mSlots[buf].mGraphicBuffer->lock( |
Eino-Ville Talvala | e41b318 | 2012-04-16 17:54:33 -0700 | [diff] [blame] | 86 | GraphicBuffer::USAGE_SW_READ_OFTEN, |
| 87 | b.mCrop, |
| 88 | &mBufferPointers[buf]); |
| 89 | |
| 90 | if (mBufferPointers[buf] != NULL && err != OK) { |
| 91 | CC_LOGE("Unable to lock buffer for CPU reading: %s (%d)", strerror(-err), |
| 92 | err); |
| 93 | return err; |
| 94 | } |
| 95 | |
| 96 | nativeBuffer->data = reinterpret_cast<uint8_t*>(mBufferPointers[buf]); |
Eino-Ville Talvala | f57e754 | 2012-08-20 15:44:40 -0700 | [diff] [blame^] | 97 | nativeBuffer->width = mSlots[buf].mGraphicBuffer->getWidth(); |
| 98 | nativeBuffer->height = mSlots[buf].mGraphicBuffer->getHeight(); |
| 99 | nativeBuffer->format = mSlots[buf].mGraphicBuffer->getPixelFormat(); |
| 100 | nativeBuffer->stride = mSlots[buf].mGraphicBuffer->getStride(); |
Eino-Ville Talvala | e41b318 | 2012-04-16 17:54:33 -0700 | [diff] [blame] | 101 | |
| 102 | nativeBuffer->crop = b.mCrop; |
| 103 | nativeBuffer->transform = b.mTransform; |
| 104 | nativeBuffer->scalingMode = b.mScalingMode; |
| 105 | nativeBuffer->timestamp = b.mTimestamp; |
| 106 | nativeBuffer->frameNumber = b.mFrameNumber; |
| 107 | |
| 108 | mCurrentLockedBuffers++; |
| 109 | |
| 110 | return OK; |
| 111 | } |
| 112 | |
| 113 | status_t CpuConsumer::unlockBuffer(const LockedBuffer &nativeBuffer) { |
| 114 | Mutex::Autolock _l(mMutex); |
Eino-Ville Talvala | f57e754 | 2012-08-20 15:44:40 -0700 | [diff] [blame^] | 115 | int slotIndex = 0; |
Eino-Ville Talvala | e41b318 | 2012-04-16 17:54:33 -0700 | [diff] [blame] | 116 | status_t err; |
| 117 | |
| 118 | void *bufPtr = reinterpret_cast<void *>(nativeBuffer.data); |
Eino-Ville Talvala | f57e754 | 2012-08-20 15:44:40 -0700 | [diff] [blame^] | 119 | for (; slotIndex < BufferQueue::NUM_BUFFER_SLOTS; slotIndex++) { |
| 120 | if (bufPtr == mBufferPointers[slotIndex]) break; |
Eino-Ville Talvala | e41b318 | 2012-04-16 17:54:33 -0700 | [diff] [blame] | 121 | } |
Eino-Ville Talvala | f57e754 | 2012-08-20 15:44:40 -0700 | [diff] [blame^] | 122 | if (slotIndex == BufferQueue::NUM_BUFFER_SLOTS) { |
Eino-Ville Talvala | e41b318 | 2012-04-16 17:54:33 -0700 | [diff] [blame] | 123 | CC_LOGE("%s: Can't find buffer to free", __FUNCTION__); |
| 124 | return BAD_VALUE; |
| 125 | } |
| 126 | |
Eino-Ville Talvala | f57e754 | 2012-08-20 15:44:40 -0700 | [diff] [blame^] | 127 | mBufferPointers[slotIndex] = NULL; |
| 128 | err = mSlots[slotIndex].mGraphicBuffer->unlock(); |
Eino-Ville Talvala | e41b318 | 2012-04-16 17:54:33 -0700 | [diff] [blame] | 129 | if (err != OK) { |
Eino-Ville Talvala | f57e754 | 2012-08-20 15:44:40 -0700 | [diff] [blame^] | 130 | CC_LOGE("%s: Unable to unlock graphic buffer %d", __FUNCTION__, slotIndex); |
Eino-Ville Talvala | e41b318 | 2012-04-16 17:54:33 -0700 | [diff] [blame] | 131 | return err; |
| 132 | } |
Eino-Ville Talvala | f57e754 | 2012-08-20 15:44:40 -0700 | [diff] [blame^] | 133 | releaseBufferLocked(slotIndex, EGL_NO_DISPLAY, EGL_NO_SYNC_KHR, Fence::NO_FENCE); |
Eino-Ville Talvala | e41b318 | 2012-04-16 17:54:33 -0700 | [diff] [blame] | 134 | |
| 135 | mCurrentLockedBuffers--; |
| 136 | |
| 137 | return OK; |
| 138 | } |
| 139 | |
Eino-Ville Talvala | f57e754 | 2012-08-20 15:44:40 -0700 | [diff] [blame^] | 140 | void CpuConsumer::freeBufferLocked(int slotIndex) { |
| 141 | if (mBufferPointers[slotIndex] != NULL) { |
| 142 | status_t err; |
| 143 | CC_LOGW("Buffer %d freed while locked by consumer", slotIndex); |
| 144 | mBufferPointers[slotIndex] = NULL; |
| 145 | err = mSlots[slotIndex].mGraphicBuffer->unlock(); |
Eino-Ville Talvala | e41b318 | 2012-04-16 17:54:33 -0700 | [diff] [blame] | 146 | if (err != OK) { |
Eino-Ville Talvala | f57e754 | 2012-08-20 15:44:40 -0700 | [diff] [blame^] | 147 | CC_LOGE("%s: Unable to unlock graphic buffer %d", __FUNCTION__, |
| 148 | slotIndex); |
Eino-Ville Talvala | e41b318 | 2012-04-16 17:54:33 -0700 | [diff] [blame] | 149 | } |
| 150 | mCurrentLockedBuffers--; |
| 151 | } |
Eino-Ville Talvala | f57e754 | 2012-08-20 15:44:40 -0700 | [diff] [blame^] | 152 | ConsumerBase::freeBufferLocked(slotIndex); |
Eino-Ville Talvala | e41b318 | 2012-04-16 17:54:33 -0700 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | } // namespace android |