blob: 7ed3d0f8933d8affb9505ed4f60ff50ba1f82075 [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);
55 mName = name;
Mathias Agopiandb89edc2013-08-02 01:40:18 -070056 mConsumer->setConsumerName(name);
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -070057}
58
Lajos Molnar6a26be72015-01-22 16:37:37 -080059static bool isPossiblyYUV(PixelFormat format) {
Lajos Molnarb3ca72c2015-01-29 10:55:21 -080060 switch (static_cast<int>(format)) {
Lajos Molnar6a26be72015-01-22 16:37:37 -080061 case HAL_PIXEL_FORMAT_RGBA_8888:
62 case HAL_PIXEL_FORMAT_RGBX_8888:
63 case HAL_PIXEL_FORMAT_RGB_888:
64 case HAL_PIXEL_FORMAT_RGB_565:
65 case HAL_PIXEL_FORMAT_BGRA_8888:
Lajos Molnar6a26be72015-01-22 16:37:37 -080066 case HAL_PIXEL_FORMAT_Y8:
67 case HAL_PIXEL_FORMAT_Y16:
Eino-Ville Talvala5b75a512015-02-19 16:10:43 -080068 case HAL_PIXEL_FORMAT_RAW16:
Lajos Molnar6a26be72015-01-22 16:37:37 -080069 case HAL_PIXEL_FORMAT_RAW10:
70 case HAL_PIXEL_FORMAT_RAW_OPAQUE:
71 case HAL_PIXEL_FORMAT_BLOB:
72 case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED:
73 return false;
74
75 case HAL_PIXEL_FORMAT_YV12:
76 case HAL_PIXEL_FORMAT_YCbCr_420_888:
77 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
78 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
79 case HAL_PIXEL_FORMAT_YCbCr_422_I:
80 default:
81 return true;
82 }
83}
84
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -070085status_t CpuConsumer::lockNextBuffer(LockedBuffer *nativeBuffer) {
86 status_t err;
87
88 if (!nativeBuffer) return BAD_VALUE;
89 if (mCurrentLockedBuffers == mMaxLockedBuffers) {
Dan Stozad723bd72014-11-18 10:24:03 -080090 CC_LOGW("Max buffers have been locked (%zd), cannot lock anymore.",
Igor Murashkina5b75132013-08-14 18:49:12 -070091 mMaxLockedBuffers);
92 return NOT_ENOUGH_DATA;
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -070093 }
94
Dan Stozadd264162015-03-12 13:58:47 -070095 BufferItem b;
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -070096
97 Mutex::Autolock _l(mMutex);
98
Andy McFadden1585c4d2013-06-28 13:52:40 -070099 err = acquireBufferLocked(&b, 0);
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700100 if (err != OK) {
101 if (err == BufferQueue::NO_BUFFER_AVAILABLE) {
102 return BAD_VALUE;
103 } else {
104 CC_LOGE("Error acquiring buffer: %s (%d)", strerror(err), err);
105 return err;
106 }
107 }
108
Pablo Ceballos47650f42015-08-04 16:38:17 -0700109 int slot = b.mSlot;
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700110
Eino-Ville Talvala64d8b192013-02-28 14:08:34 -0800111 void *bufferPointer = NULL;
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700112 android_ycbcr ycbcr = android_ycbcr();
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700113
Pablo Ceballos47650f42015-08-04 16:38:17 -0700114 PixelFormat format = mSlots[slot].mGraphicBuffer->getPixelFormat();
Lajos Molnar6a26be72015-01-22 16:37:37 -0800115 PixelFormat flexFormat = format;
116 if (isPossiblyYUV(format)) {
117 if (b.mFence.get()) {
Pablo Ceballos47650f42015-08-04 16:38:17 -0700118 err = mSlots[slot].mGraphicBuffer->lockAsyncYCbCr(
Riley Andrewsd53e0522014-08-18 16:57:11 -0700119 GraphicBuffer::USAGE_SW_READ_OFTEN,
120 b.mCrop,
121 &ycbcr,
122 b.mFence->dup());
Riley Andrewsd53e0522014-08-18 16:57:11 -0700123 } else {
Pablo Ceballos47650f42015-08-04 16:38:17 -0700124 err = mSlots[slot].mGraphicBuffer->lockYCbCr(
Lajos Molnar6a26be72015-01-22 16:37:37 -0800125 GraphicBuffer::USAGE_SW_READ_OFTEN,
126 b.mCrop,
127 &ycbcr);
128 }
129 if (err == OK) {
130 bufferPointer = ycbcr.y;
131 flexFormat = HAL_PIXEL_FORMAT_YCbCr_420_888;
132 if (format != HAL_PIXEL_FORMAT_YCbCr_420_888) {
133 CC_LOGV("locking buffer of format %#x as flex YUV", format);
134 }
135 } else if (format == HAL_PIXEL_FORMAT_YCbCr_420_888) {
136 CC_LOGE("Unable to lock YCbCr buffer for CPU reading: %s (%d)",
137 strerror(-err), err);
138 return err;
139 }
140 }
141
142 if (bufferPointer == NULL) { // not flexible YUV
143 if (b.mFence.get()) {
Pablo Ceballos47650f42015-08-04 16:38:17 -0700144 err = mSlots[slot].mGraphicBuffer->lockAsync(
Riley Andrewsd53e0522014-08-18 16:57:11 -0700145 GraphicBuffer::USAGE_SW_READ_OFTEN,
146 b.mCrop,
147 &bufferPointer,
148 b.mFence->dup());
Riley Andrewsd53e0522014-08-18 16:57:11 -0700149 } else {
Pablo Ceballos47650f42015-08-04 16:38:17 -0700150 err = mSlots[slot].mGraphicBuffer->lock(
Riley Andrewsd53e0522014-08-18 16:57:11 -0700151 GraphicBuffer::USAGE_SW_READ_OFTEN,
152 b.mCrop,
153 &bufferPointer);
Lajos Molnar6a26be72015-01-22 16:37:37 -0800154 }
155 if (err != OK) {
156 CC_LOGE("Unable to lock buffer for CPU reading: %s (%d)",
157 strerror(-err), err);
158 return err;
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700159 }
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700160 }
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700161
Eino-Ville Talvala042ecee2013-02-28 18:23:24 -0800162 size_t lockedIdx = 0;
Dan Stozad723bd72014-11-18 10:24:03 -0800163 for (; lockedIdx < static_cast<size_t>(mMaxLockedBuffers); lockedIdx++) {
Eino-Ville Talvala042ecee2013-02-28 18:23:24 -0800164 if (mAcquiredBuffers[lockedIdx].mSlot ==
165 BufferQueue::INVALID_BUFFER_SLOT) {
166 break;
167 }
168 }
169 assert(lockedIdx < mMaxLockedBuffers);
170
171 AcquiredBuffer &ab = mAcquiredBuffers.editItemAt(lockedIdx);
Pablo Ceballos47650f42015-08-04 16:38:17 -0700172 ab.mSlot = slot;
Eino-Ville Talvala042ecee2013-02-28 18:23:24 -0800173 ab.mBufferPointer = bufferPointer;
Pablo Ceballos47650f42015-08-04 16:38:17 -0700174 ab.mGraphicBuffer = mSlots[slot].mGraphicBuffer;
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700175
Eino-Ville Talvala64d8b192013-02-28 14:08:34 -0800176 nativeBuffer->data =
177 reinterpret_cast<uint8_t*>(bufferPointer);
Pablo Ceballos47650f42015-08-04 16:38:17 -0700178 nativeBuffer->width = mSlots[slot].mGraphicBuffer->getWidth();
179 nativeBuffer->height = mSlots[slot].mGraphicBuffer->getHeight();
Lajos Molnar6a26be72015-01-22 16:37:37 -0800180 nativeBuffer->format = format;
181 nativeBuffer->flexFormat = flexFormat;
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700182 nativeBuffer->stride = (ycbcr.y != NULL) ?
Dan Stozad723bd72014-11-18 10:24:03 -0800183 static_cast<uint32_t>(ycbcr.ystride) :
Pablo Ceballos47650f42015-08-04 16:38:17 -0700184 mSlots[slot].mGraphicBuffer->getStride();
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700185
186 nativeBuffer->crop = b.mCrop;
187 nativeBuffer->transform = b.mTransform;
188 nativeBuffer->scalingMode = b.mScalingMode;
189 nativeBuffer->timestamp = b.mTimestamp;
Eino-Ville Talvala5b75a512015-02-19 16:10:43 -0800190 nativeBuffer->dataSpace = b.mDataSpace;
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700191 nativeBuffer->frameNumber = b.mFrameNumber;
192
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700193 nativeBuffer->dataCb = reinterpret_cast<uint8_t*>(ycbcr.cb);
194 nativeBuffer->dataCr = reinterpret_cast<uint8_t*>(ycbcr.cr);
Dan Stozad723bd72014-11-18 10:24:03 -0800195 nativeBuffer->chromaStride = static_cast<uint32_t>(ycbcr.cstride);
196 nativeBuffer->chromaStep = static_cast<uint32_t>(ycbcr.chroma_step);
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700197
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700198 mCurrentLockedBuffers++;
199
200 return OK;
201}
202
203status_t CpuConsumer::unlockBuffer(const LockedBuffer &nativeBuffer) {
204 Mutex::Autolock _l(mMutex);
Eino-Ville Talvala042ecee2013-02-28 18:23:24 -0800205 size_t lockedIdx = 0;
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700206
207 void *bufPtr = reinterpret_cast<void *>(nativeBuffer.data);
Dan Stozad723bd72014-11-18 10:24:03 -0800208 for (; lockedIdx < static_cast<size_t>(mMaxLockedBuffers); lockedIdx++) {
Eino-Ville Talvala042ecee2013-02-28 18:23:24 -0800209 if (bufPtr == mAcquiredBuffers[lockedIdx].mBufferPointer) break;
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700210 }
Eino-Ville Talvala042ecee2013-02-28 18:23:24 -0800211 if (lockedIdx == mMaxLockedBuffers) {
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700212 CC_LOGE("%s: Can't find buffer to free", __FUNCTION__);
213 return BAD_VALUE;
214 }
215
Eino-Ville Talvala042ecee2013-02-28 18:23:24 -0800216 return releaseAcquiredBufferLocked(lockedIdx);
217}
218
Dan Stozad723bd72014-11-18 10:24:03 -0800219status_t CpuConsumer::releaseAcquiredBufferLocked(size_t lockedIdx) {
Eino-Ville Talvala042ecee2013-02-28 18:23:24 -0800220 status_t err;
Riley Andrewsd53e0522014-08-18 16:57:11 -0700221 int fd = -1;
Eino-Ville Talvala042ecee2013-02-28 18:23:24 -0800222
Riley Andrewsd53e0522014-08-18 16:57:11 -0700223 err = mAcquiredBuffers[lockedIdx].mGraphicBuffer->unlockAsync(&fd);
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700224 if (err != OK) {
Dan Stozad723bd72014-11-18 10:24:03 -0800225 CC_LOGE("%s: Unable to unlock graphic buffer %zd", __FUNCTION__,
Eino-Ville Talvala042ecee2013-02-28 18:23:24 -0800226 lockedIdx);
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700227 return err;
228 }
Eino-Ville Talvala042ecee2013-02-28 18:23:24 -0800229 int buf = mAcquiredBuffers[lockedIdx].mSlot;
Riley Andrewsd53e0522014-08-18 16:57:11 -0700230 if (CC_LIKELY(fd != -1)) {
231 sp<Fence> fence(new Fence(fd));
232 addReleaseFenceLocked(
233 mAcquiredBuffers[lockedIdx].mSlot,
234 mSlots[buf].mGraphicBuffer,
235 fence);
236 }
Eino-Ville Talvala042ecee2013-02-28 18:23:24 -0800237
238 // release the buffer if it hasn't already been freed by the BufferQueue.
239 // This can happen, for example, when the producer of this buffer
240 // disconnected after this buffer was acquired.
241 if (CC_LIKELY(mAcquiredBuffers[lockedIdx].mGraphicBuffer ==
242 mSlots[buf].mGraphicBuffer)) {
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700243 releaseBufferLocked(
244 buf, mAcquiredBuffers[lockedIdx].mGraphicBuffer,
245 EGL_NO_DISPLAY, EGL_NO_SYNC_KHR);
Eino-Ville Talvala042ecee2013-02-28 18:23:24 -0800246 }
247
248 AcquiredBuffer &ab = mAcquiredBuffers.editItemAt(lockedIdx);
249 ab.mSlot = BufferQueue::INVALID_BUFFER_SLOT;
250 ab.mBufferPointer = NULL;
251 ab.mGraphicBuffer.clear();
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700252
253 mCurrentLockedBuffers--;
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700254 return OK;
255}
256
Eino-Ville Talvalaf57e7542012-08-20 15:44:40 -0700257void CpuConsumer::freeBufferLocked(int slotIndex) {
Eino-Ville Talvalaf57e7542012-08-20 15:44:40 -0700258 ConsumerBase::freeBufferLocked(slotIndex);
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700259}
260
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700261} // namespace android