blob: a9bafef8b611841606a79cac7d1198bc274735be [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:
Romain Guyff415142016-12-13 16:51:25 -080067 case HAL_PIXEL_FORMAT_RGBA_FP16:
Lajos Molnar6a26be72015-01-22 16:37:37 -080068 case HAL_PIXEL_FORMAT_RGB_888:
69 case HAL_PIXEL_FORMAT_RGB_565:
70 case HAL_PIXEL_FORMAT_BGRA_8888:
Lajos Molnar6a26be72015-01-22 16:37:37 -080071 case HAL_PIXEL_FORMAT_Y8:
72 case HAL_PIXEL_FORMAT_Y16:
Eino-Ville Talvala5b75a512015-02-19 16:10:43 -080073 case HAL_PIXEL_FORMAT_RAW16:
Lajos Molnar6a26be72015-01-22 16:37:37 -080074 case HAL_PIXEL_FORMAT_RAW10:
75 case HAL_PIXEL_FORMAT_RAW_OPAQUE:
76 case HAL_PIXEL_FORMAT_BLOB:
77 case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED:
78 return false;
79
80 case HAL_PIXEL_FORMAT_YV12:
81 case HAL_PIXEL_FORMAT_YCbCr_420_888:
82 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
83 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
84 case HAL_PIXEL_FORMAT_YCbCr_422_I:
85 default:
86 return true;
87 }
88}
89
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -070090status_t CpuConsumer::lockNextBuffer(LockedBuffer *nativeBuffer) {
91 status_t err;
92
93 if (!nativeBuffer) return BAD_VALUE;
94 if (mCurrentLockedBuffers == mMaxLockedBuffers) {
Dan Stozad723bd72014-11-18 10:24:03 -080095 CC_LOGW("Max buffers have been locked (%zd), cannot lock anymore.",
Igor Murashkina5b75132013-08-14 18:49:12 -070096 mMaxLockedBuffers);
97 return NOT_ENOUGH_DATA;
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -070098 }
99
Dan Stozadd264162015-03-12 13:58:47 -0700100 BufferItem b;
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700101
102 Mutex::Autolock _l(mMutex);
103
Andy McFadden1585c4d2013-06-28 13:52:40 -0700104 err = acquireBufferLocked(&b, 0);
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700105 if (err != OK) {
106 if (err == BufferQueue::NO_BUFFER_AVAILABLE) {
107 return BAD_VALUE;
108 } else {
109 CC_LOGE("Error acquiring buffer: %s (%d)", strerror(err), err);
110 return err;
111 }
112 }
113
Pablo Ceballos47650f42015-08-04 16:38:17 -0700114 int slot = b.mSlot;
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700115
Eino-Ville Talvala64d8b192013-02-28 14:08:34 -0800116 void *bufferPointer = NULL;
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700117 android_ycbcr ycbcr = android_ycbcr();
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700118
Pablo Ceballos47650f42015-08-04 16:38:17 -0700119 PixelFormat format = mSlots[slot].mGraphicBuffer->getPixelFormat();
Lajos Molnar6a26be72015-01-22 16:37:37 -0800120 PixelFormat flexFormat = format;
121 if (isPossiblyYUV(format)) {
122 if (b.mFence.get()) {
Pablo Ceballos47650f42015-08-04 16:38:17 -0700123 err = mSlots[slot].mGraphicBuffer->lockAsyncYCbCr(
Riley Andrewsd53e0522014-08-18 16:57:11 -0700124 GraphicBuffer::USAGE_SW_READ_OFTEN,
125 b.mCrop,
126 &ycbcr,
127 b.mFence->dup());
Riley Andrewsd53e0522014-08-18 16:57:11 -0700128 } else {
Pablo Ceballos47650f42015-08-04 16:38:17 -0700129 err = mSlots[slot].mGraphicBuffer->lockYCbCr(
Lajos Molnar6a26be72015-01-22 16:37:37 -0800130 GraphicBuffer::USAGE_SW_READ_OFTEN,
131 b.mCrop,
132 &ycbcr);
133 }
134 if (err == OK) {
135 bufferPointer = ycbcr.y;
136 flexFormat = HAL_PIXEL_FORMAT_YCbCr_420_888;
137 if (format != HAL_PIXEL_FORMAT_YCbCr_420_888) {
138 CC_LOGV("locking buffer of format %#x as flex YUV", format);
139 }
140 } else if (format == HAL_PIXEL_FORMAT_YCbCr_420_888) {
141 CC_LOGE("Unable to lock YCbCr buffer for CPU reading: %s (%d)",
142 strerror(-err), err);
143 return err;
144 }
145 }
146
147 if (bufferPointer == NULL) { // not flexible YUV
148 if (b.mFence.get()) {
Pablo Ceballos47650f42015-08-04 16:38:17 -0700149 err = mSlots[slot].mGraphicBuffer->lockAsync(
Riley Andrewsd53e0522014-08-18 16:57:11 -0700150 GraphicBuffer::USAGE_SW_READ_OFTEN,
151 b.mCrop,
152 &bufferPointer,
153 b.mFence->dup());
Riley Andrewsd53e0522014-08-18 16:57:11 -0700154 } else {
Pablo Ceballos47650f42015-08-04 16:38:17 -0700155 err = mSlots[slot].mGraphicBuffer->lock(
Riley Andrewsd53e0522014-08-18 16:57:11 -0700156 GraphicBuffer::USAGE_SW_READ_OFTEN,
157 b.mCrop,
158 &bufferPointer);
Lajos Molnar6a26be72015-01-22 16:37:37 -0800159 }
160 if (err != OK) {
161 CC_LOGE("Unable to lock buffer for CPU reading: %s (%d)",
162 strerror(-err), err);
163 return err;
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700164 }
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700165 }
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700166
Eino-Ville Talvala042ecee2013-02-28 18:23:24 -0800167 size_t lockedIdx = 0;
Dan Stozad723bd72014-11-18 10:24:03 -0800168 for (; lockedIdx < static_cast<size_t>(mMaxLockedBuffers); lockedIdx++) {
Eino-Ville Talvala042ecee2013-02-28 18:23:24 -0800169 if (mAcquiredBuffers[lockedIdx].mSlot ==
170 BufferQueue::INVALID_BUFFER_SLOT) {
171 break;
172 }
173 }
174 assert(lockedIdx < mMaxLockedBuffers);
175
176 AcquiredBuffer &ab = mAcquiredBuffers.editItemAt(lockedIdx);
Pablo Ceballos47650f42015-08-04 16:38:17 -0700177 ab.mSlot = slot;
Eino-Ville Talvala042ecee2013-02-28 18:23:24 -0800178 ab.mBufferPointer = bufferPointer;
Pablo Ceballos47650f42015-08-04 16:38:17 -0700179 ab.mGraphicBuffer = mSlots[slot].mGraphicBuffer;
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700180
Eino-Ville Talvala64d8b192013-02-28 14:08:34 -0800181 nativeBuffer->data =
182 reinterpret_cast<uint8_t*>(bufferPointer);
Pablo Ceballos47650f42015-08-04 16:38:17 -0700183 nativeBuffer->width = mSlots[slot].mGraphicBuffer->getWidth();
184 nativeBuffer->height = mSlots[slot].mGraphicBuffer->getHeight();
Lajos Molnar6a26be72015-01-22 16:37:37 -0800185 nativeBuffer->format = format;
186 nativeBuffer->flexFormat = flexFormat;
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700187 nativeBuffer->stride = (ycbcr.y != NULL) ?
Dan Stozad723bd72014-11-18 10:24:03 -0800188 static_cast<uint32_t>(ycbcr.ystride) :
Pablo Ceballos47650f42015-08-04 16:38:17 -0700189 mSlots[slot].mGraphicBuffer->getStride();
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700190
191 nativeBuffer->crop = b.mCrop;
192 nativeBuffer->transform = b.mTransform;
193 nativeBuffer->scalingMode = b.mScalingMode;
194 nativeBuffer->timestamp = b.mTimestamp;
Eino-Ville Talvala5b75a512015-02-19 16:10:43 -0800195 nativeBuffer->dataSpace = b.mDataSpace;
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700196 nativeBuffer->frameNumber = b.mFrameNumber;
197
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700198 nativeBuffer->dataCb = reinterpret_cast<uint8_t*>(ycbcr.cb);
199 nativeBuffer->dataCr = reinterpret_cast<uint8_t*>(ycbcr.cr);
Dan Stozad723bd72014-11-18 10:24:03 -0800200 nativeBuffer->chromaStride = static_cast<uint32_t>(ycbcr.cstride);
201 nativeBuffer->chromaStep = static_cast<uint32_t>(ycbcr.chroma_step);
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700202
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700203 mCurrentLockedBuffers++;
204
205 return OK;
206}
207
208status_t CpuConsumer::unlockBuffer(const LockedBuffer &nativeBuffer) {
209 Mutex::Autolock _l(mMutex);
Eino-Ville Talvala042ecee2013-02-28 18:23:24 -0800210 size_t lockedIdx = 0;
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700211
212 void *bufPtr = reinterpret_cast<void *>(nativeBuffer.data);
Dan Stozad723bd72014-11-18 10:24:03 -0800213 for (; lockedIdx < static_cast<size_t>(mMaxLockedBuffers); lockedIdx++) {
Eino-Ville Talvala042ecee2013-02-28 18:23:24 -0800214 if (bufPtr == mAcquiredBuffers[lockedIdx].mBufferPointer) break;
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700215 }
Eino-Ville Talvala042ecee2013-02-28 18:23:24 -0800216 if (lockedIdx == mMaxLockedBuffers) {
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700217 CC_LOGE("%s: Can't find buffer to free", __FUNCTION__);
218 return BAD_VALUE;
219 }
220
Eino-Ville Talvala042ecee2013-02-28 18:23:24 -0800221 return releaseAcquiredBufferLocked(lockedIdx);
222}
223
Dan Stozad723bd72014-11-18 10:24:03 -0800224status_t CpuConsumer::releaseAcquiredBufferLocked(size_t lockedIdx) {
Eino-Ville Talvala042ecee2013-02-28 18:23:24 -0800225 status_t err;
Riley Andrewsd53e0522014-08-18 16:57:11 -0700226 int fd = -1;
Eino-Ville Talvala042ecee2013-02-28 18:23:24 -0800227
Riley Andrewsd53e0522014-08-18 16:57:11 -0700228 err = mAcquiredBuffers[lockedIdx].mGraphicBuffer->unlockAsync(&fd);
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700229 if (err != OK) {
Dan Stozad723bd72014-11-18 10:24:03 -0800230 CC_LOGE("%s: Unable to unlock graphic buffer %zd", __FUNCTION__,
Eino-Ville Talvala042ecee2013-02-28 18:23:24 -0800231 lockedIdx);
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700232 return err;
233 }
Eino-Ville Talvala042ecee2013-02-28 18:23:24 -0800234 int buf = mAcquiredBuffers[lockedIdx].mSlot;
Riley Andrewsd53e0522014-08-18 16:57:11 -0700235 if (CC_LIKELY(fd != -1)) {
236 sp<Fence> fence(new Fence(fd));
237 addReleaseFenceLocked(
238 mAcquiredBuffers[lockedIdx].mSlot,
239 mSlots[buf].mGraphicBuffer,
240 fence);
241 }
Eino-Ville Talvala042ecee2013-02-28 18:23:24 -0800242
243 // release the buffer if it hasn't already been freed by the BufferQueue.
244 // This can happen, for example, when the producer of this buffer
245 // disconnected after this buffer was acquired.
246 if (CC_LIKELY(mAcquiredBuffers[lockedIdx].mGraphicBuffer ==
247 mSlots[buf].mGraphicBuffer)) {
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700248 releaseBufferLocked(
249 buf, mAcquiredBuffers[lockedIdx].mGraphicBuffer,
250 EGL_NO_DISPLAY, EGL_NO_SYNC_KHR);
Eino-Ville Talvala042ecee2013-02-28 18:23:24 -0800251 }
252
253 AcquiredBuffer &ab = mAcquiredBuffers.editItemAt(lockedIdx);
254 ab.mSlot = BufferQueue::INVALID_BUFFER_SLOT;
255 ab.mBufferPointer = NULL;
256 ab.mGraphicBuffer.clear();
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700257
258 mCurrentLockedBuffers--;
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700259 return OK;
260}
261
Eino-Ville Talvalaf57e7542012-08-20 15:44:40 -0700262void CpuConsumer::freeBufferLocked(int slotIndex) {
Eino-Ville Talvalaf57e7542012-08-20 15:44:40 -0700263 ConsumerBase::freeBufferLocked(slotIndex);
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700264}
265
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700266} // namespace android