blob: 839316021ea33c9a401a71cb451b6df18fa82356 [file] [log] [blame]
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -07001/*
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"
Dan Stozad723bd72014-11-18 10:24:03 -080019//#define ATRACE_TAG ATRACE_TAG_GRAPHICS
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -070020
Eino-Ville Talvala042ecee2013-02-28 18:23:24 -080021#include <cutils/compiler.h>
22#include <utils/Log.h>
Dan Stozadd264162015-03-12 13:58:47 -070023#include <gui/BufferItem.h>
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -070024#include <gui/CpuConsumer.h>
25
Lajos Molnarb3ca72c2015-01-29 10:55:21 -080026#define CC_LOGV(x, ...) ALOGV("[%s] " x, mName.string(), ##__VA_ARGS__)
Dan Stozad723bd72014-11-18 10:24:03 -080027//#define CC_LOGD(x, ...) ALOGD("[%s] " x, mName.string(), ##__VA_ARGS__)
28//#define CC_LOGI(x, ...) ALOGI("[%s] " x, mName.string(), ##__VA_ARGS__)
Dan Albert8b491252014-09-08 18:53:39 -070029#define CC_LOGW(x, ...) ALOGW("[%s] " x, mName.string(), ##__VA_ARGS__)
30#define CC_LOGE(x, ...) ALOGE("[%s] " x, mName.string(), ##__VA_ARGS__)
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -070031
32namespace android {
33
Mathias Agopiandb89edc2013-08-02 01:40:18 -070034CpuConsumer::CpuConsumer(const sp<IGraphicBufferConsumer>& bq,
Dan Stozad723bd72014-11-18 10:24:03 -080035 size_t maxLockedBuffers, bool controlledByApp) :
Mathias Agopian595264f2013-07-16 22:56:09 -070036 ConsumerBase(bq, controlledByApp),
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -070037 mMaxLockedBuffers(maxLockedBuffers),
38 mCurrentLockedBuffers(0)
39{
Eino-Ville Talvala042ecee2013-02-28 18:23:24 -080040 // Create tracking entries for locked buffers
41 mAcquiredBuffers.insertAt(0, maxLockedBuffers);
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -070042
Mathias Agopiandb89edc2013-08-02 01:40:18 -070043 mConsumer->setConsumerUsageBits(GRALLOC_USAGE_SW_READ_OFTEN);
Dan Stozad723bd72014-11-18 10:24:03 -080044 mConsumer->setMaxAcquiredBufferCount(static_cast<int32_t>(maxLockedBuffers));
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -070045}
46
Eino-Ville Talvalae232fdc2012-08-21 13:37:35 -070047CpuConsumer::~CpuConsumer() {
Eino-Ville Talvala042ecee2013-02-28 18:23:24 -080048 // ConsumerBase destructor does all the work.
Eino-Ville Talvalae232fdc2012-08-21 13:37:35 -070049}
50
Eino-Ville Talvala042ecee2013-02-28 18:23:24 -080051
52
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -070053void CpuConsumer::setName(const String8& name) {
54 Mutex::Autolock _l(mMutex);
Pablo Ceballos65d9f6d2016-05-04 13:59:35 -070055 if (mAbandoned) {
56 CC_LOGE("setName: CpuConsumer is abandoned!");
57 return;
58 }
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -070059 mName = name;
Mathias Agopiandb89edc2013-08-02 01:40:18 -070060 mConsumer->setConsumerName(name);
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -070061}
62
Lajos Molnar6a26be72015-01-22 16:37:37 -080063static bool isPossiblyYUV(PixelFormat format) {
Lajos Molnarb3ca72c2015-01-29 10:55:21 -080064 switch (static_cast<int>(format)) {
Lajos Molnar6a26be72015-01-22 16:37:37 -080065 case HAL_PIXEL_FORMAT_RGBA_8888:
66 case HAL_PIXEL_FORMAT_RGBX_8888:
67 case HAL_PIXEL_FORMAT_RGB_888:
68 case HAL_PIXEL_FORMAT_RGB_565:
69 case HAL_PIXEL_FORMAT_BGRA_8888:
Lajos Molnar6a26be72015-01-22 16:37:37 -080070 case HAL_PIXEL_FORMAT_Y8:
71 case HAL_PIXEL_FORMAT_Y16:
Eino-Ville Talvala5b75a512015-02-19 16:10:43 -080072 case HAL_PIXEL_FORMAT_RAW16:
Lajos Molnar6a26be72015-01-22 16:37:37 -080073 case HAL_PIXEL_FORMAT_RAW10:
74 case HAL_PIXEL_FORMAT_RAW_OPAQUE:
75 case HAL_PIXEL_FORMAT_BLOB:
76 case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED:
77 return false;
78
79 case HAL_PIXEL_FORMAT_YV12:
80 case HAL_PIXEL_FORMAT_YCbCr_420_888:
81 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
82 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
83 case HAL_PIXEL_FORMAT_YCbCr_422_I:
84 default:
85 return true;
86 }
87}
88
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -070089status_t CpuConsumer::lockNextBuffer(LockedBuffer *nativeBuffer) {
90 status_t err;
91
92 if (!nativeBuffer) return BAD_VALUE;
93 if (mCurrentLockedBuffers == mMaxLockedBuffers) {
Dan Stozad723bd72014-11-18 10:24:03 -080094 CC_LOGW("Max buffers have been locked (%zd), cannot lock anymore.",
Igor Murashkina5b75132013-08-14 18:49:12 -070095 mMaxLockedBuffers);
96 return NOT_ENOUGH_DATA;
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -070097 }
98
Dan Stozadd264162015-03-12 13:58:47 -070099 BufferItem b;
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700100
101 Mutex::Autolock _l(mMutex);
102
Andy McFadden1585c4d2013-06-28 13:52:40 -0700103 err = acquireBufferLocked(&b, 0);
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700104 if (err != OK) {
105 if (err == BufferQueue::NO_BUFFER_AVAILABLE) {
106 return BAD_VALUE;
107 } else {
108 CC_LOGE("Error acquiring buffer: %s (%d)", strerror(err), err);
109 return err;
110 }
111 }
112
Pablo Ceballos47650f42015-08-04 16:38:17 -0700113 int slot = b.mSlot;
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700114
Eino-Ville Talvala64d8b192013-02-28 14:08:34 -0800115 void *bufferPointer = NULL;
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700116 android_ycbcr ycbcr = android_ycbcr();
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700117
Pablo Ceballos47650f42015-08-04 16:38:17 -0700118 PixelFormat format = mSlots[slot].mGraphicBuffer->getPixelFormat();
Lajos Molnar6a26be72015-01-22 16:37:37 -0800119 PixelFormat flexFormat = format;
120 if (isPossiblyYUV(format)) {
121 if (b.mFence.get()) {
Pablo Ceballos47650f42015-08-04 16:38:17 -0700122 err = mSlots[slot].mGraphicBuffer->lockAsyncYCbCr(
Riley Andrewsd53e0522014-08-18 16:57:11 -0700123 GraphicBuffer::USAGE_SW_READ_OFTEN,
124 b.mCrop,
125 &ycbcr,
126 b.mFence->dup());
Riley Andrewsd53e0522014-08-18 16:57:11 -0700127 } else {
Pablo Ceballos47650f42015-08-04 16:38:17 -0700128 err = mSlots[slot].mGraphicBuffer->lockYCbCr(
Lajos Molnar6a26be72015-01-22 16:37:37 -0800129 GraphicBuffer::USAGE_SW_READ_OFTEN,
130 b.mCrop,
131 &ycbcr);
132 }
133 if (err == OK) {
134 bufferPointer = ycbcr.y;
135 flexFormat = HAL_PIXEL_FORMAT_YCbCr_420_888;
136 if (format != HAL_PIXEL_FORMAT_YCbCr_420_888) {
137 CC_LOGV("locking buffer of format %#x as flex YUV", format);
138 }
139 } else if (format == HAL_PIXEL_FORMAT_YCbCr_420_888) {
140 CC_LOGE("Unable to lock YCbCr buffer for CPU reading: %s (%d)",
141 strerror(-err), err);
142 return err;
143 }
144 }
145
146 if (bufferPointer == NULL) { // not flexible YUV
147 if (b.mFence.get()) {
Pablo Ceballos47650f42015-08-04 16:38:17 -0700148 err = mSlots[slot].mGraphicBuffer->lockAsync(
Riley Andrewsd53e0522014-08-18 16:57:11 -0700149 GraphicBuffer::USAGE_SW_READ_OFTEN,
150 b.mCrop,
151 &bufferPointer,
152 b.mFence->dup());
Riley Andrewsd53e0522014-08-18 16:57:11 -0700153 } else {
Pablo Ceballos47650f42015-08-04 16:38:17 -0700154 err = mSlots[slot].mGraphicBuffer->lock(
Riley Andrewsd53e0522014-08-18 16:57:11 -0700155 GraphicBuffer::USAGE_SW_READ_OFTEN,
156 b.mCrop,
157 &bufferPointer);
Lajos Molnar6a26be72015-01-22 16:37:37 -0800158 }
159 if (err != OK) {
160 CC_LOGE("Unable to lock buffer for CPU reading: %s (%d)",
161 strerror(-err), err);
162 return err;
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700163 }
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700164 }
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700165
Eino-Ville Talvala042ecee2013-02-28 18:23:24 -0800166 size_t lockedIdx = 0;
Dan Stozad723bd72014-11-18 10:24:03 -0800167 for (; lockedIdx < static_cast<size_t>(mMaxLockedBuffers); lockedIdx++) {
Eino-Ville Talvala042ecee2013-02-28 18:23:24 -0800168 if (mAcquiredBuffers[lockedIdx].mSlot ==
169 BufferQueue::INVALID_BUFFER_SLOT) {
170 break;
171 }
172 }
173 assert(lockedIdx < mMaxLockedBuffers);
174
175 AcquiredBuffer &ab = mAcquiredBuffers.editItemAt(lockedIdx);
Pablo Ceballos47650f42015-08-04 16:38:17 -0700176 ab.mSlot = slot;
Eino-Ville Talvala042ecee2013-02-28 18:23:24 -0800177 ab.mBufferPointer = bufferPointer;
Pablo Ceballos47650f42015-08-04 16:38:17 -0700178 ab.mGraphicBuffer = mSlots[slot].mGraphicBuffer;
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700179
Eino-Ville Talvala64d8b192013-02-28 14:08:34 -0800180 nativeBuffer->data =
181 reinterpret_cast<uint8_t*>(bufferPointer);
Pablo Ceballos47650f42015-08-04 16:38:17 -0700182 nativeBuffer->width = mSlots[slot].mGraphicBuffer->getWidth();
183 nativeBuffer->height = mSlots[slot].mGraphicBuffer->getHeight();
Lajos Molnar6a26be72015-01-22 16:37:37 -0800184 nativeBuffer->format = format;
185 nativeBuffer->flexFormat = flexFormat;
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700186 nativeBuffer->stride = (ycbcr.y != NULL) ?
Dan Stozad723bd72014-11-18 10:24:03 -0800187 static_cast<uint32_t>(ycbcr.ystride) :
Pablo Ceballos47650f42015-08-04 16:38:17 -0700188 mSlots[slot].mGraphicBuffer->getStride();
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700189
190 nativeBuffer->crop = b.mCrop;
191 nativeBuffer->transform = b.mTransform;
192 nativeBuffer->scalingMode = b.mScalingMode;
193 nativeBuffer->timestamp = b.mTimestamp;
Eino-Ville Talvala5b75a512015-02-19 16:10:43 -0800194 nativeBuffer->dataSpace = b.mDataSpace;
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700195 nativeBuffer->frameNumber = b.mFrameNumber;
196
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700197 nativeBuffer->dataCb = reinterpret_cast<uint8_t*>(ycbcr.cb);
198 nativeBuffer->dataCr = reinterpret_cast<uint8_t*>(ycbcr.cr);
Dan Stozad723bd72014-11-18 10:24:03 -0800199 nativeBuffer->chromaStride = static_cast<uint32_t>(ycbcr.cstride);
200 nativeBuffer->chromaStep = static_cast<uint32_t>(ycbcr.chroma_step);
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700201
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700202 mCurrentLockedBuffers++;
203
204 return OK;
205}
206
207status_t CpuConsumer::unlockBuffer(const LockedBuffer &nativeBuffer) {
208 Mutex::Autolock _l(mMutex);
Eino-Ville Talvala042ecee2013-02-28 18:23:24 -0800209 size_t lockedIdx = 0;
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700210
211 void *bufPtr = reinterpret_cast<void *>(nativeBuffer.data);
Dan Stozad723bd72014-11-18 10:24:03 -0800212 for (; lockedIdx < static_cast<size_t>(mMaxLockedBuffers); lockedIdx++) {
Eino-Ville Talvala042ecee2013-02-28 18:23:24 -0800213 if (bufPtr == mAcquiredBuffers[lockedIdx].mBufferPointer) break;
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700214 }
Eino-Ville Talvala042ecee2013-02-28 18:23:24 -0800215 if (lockedIdx == mMaxLockedBuffers) {
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700216 CC_LOGE("%s: Can't find buffer to free", __FUNCTION__);
217 return BAD_VALUE;
218 }
219
Eino-Ville Talvala042ecee2013-02-28 18:23:24 -0800220 return releaseAcquiredBufferLocked(lockedIdx);
221}
222
Dan Stozad723bd72014-11-18 10:24:03 -0800223status_t CpuConsumer::releaseAcquiredBufferLocked(size_t lockedIdx) {
Eino-Ville Talvala042ecee2013-02-28 18:23:24 -0800224 status_t err;
Riley Andrewsd53e0522014-08-18 16:57:11 -0700225 int fd = -1;
Eino-Ville Talvala042ecee2013-02-28 18:23:24 -0800226
Riley Andrewsd53e0522014-08-18 16:57:11 -0700227 err = mAcquiredBuffers[lockedIdx].mGraphicBuffer->unlockAsync(&fd);
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700228 if (err != OK) {
Dan Stozad723bd72014-11-18 10:24:03 -0800229 CC_LOGE("%s: Unable to unlock graphic buffer %zd", __FUNCTION__,
Eino-Ville Talvala042ecee2013-02-28 18:23:24 -0800230 lockedIdx);
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700231 return err;
232 }
Eino-Ville Talvala042ecee2013-02-28 18:23:24 -0800233 int buf = mAcquiredBuffers[lockedIdx].mSlot;
Riley Andrewsd53e0522014-08-18 16:57:11 -0700234 if (CC_LIKELY(fd != -1)) {
235 sp<Fence> fence(new Fence(fd));
236 addReleaseFenceLocked(
237 mAcquiredBuffers[lockedIdx].mSlot,
238 mSlots[buf].mGraphicBuffer,
239 fence);
240 }
Eino-Ville Talvala042ecee2013-02-28 18:23:24 -0800241
242 // release the buffer if it hasn't already been freed by the BufferQueue.
243 // This can happen, for example, when the producer of this buffer
244 // disconnected after this buffer was acquired.
245 if (CC_LIKELY(mAcquiredBuffers[lockedIdx].mGraphicBuffer ==
246 mSlots[buf].mGraphicBuffer)) {
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700247 releaseBufferLocked(
248 buf, mAcquiredBuffers[lockedIdx].mGraphicBuffer,
249 EGL_NO_DISPLAY, EGL_NO_SYNC_KHR);
Eino-Ville Talvala042ecee2013-02-28 18:23:24 -0800250 }
251
252 AcquiredBuffer &ab = mAcquiredBuffers.editItemAt(lockedIdx);
253 ab.mSlot = BufferQueue::INVALID_BUFFER_SLOT;
254 ab.mBufferPointer = NULL;
255 ab.mGraphicBuffer.clear();
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700256
257 mCurrentLockedBuffers--;
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700258 return OK;
259}
260
Eino-Ville Talvalaf57e7542012-08-20 15:44:40 -0700261void CpuConsumer::freeBufferLocked(int slotIndex) {
Eino-Ville Talvalaf57e7542012-08-20 15:44:40 -0700262 ConsumerBase::freeBufferLocked(slotIndex);
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700263}
264
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700265} // namespace android