blob: 4acf618a635fe3b8bc490a2e6b1da4698a7dc671 [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:
68 case HAL_PIXEL_FORMAT_RGBX_FP16:
Lajos Molnar6a26be72015-01-22 16:37:37 -080069 case HAL_PIXEL_FORMAT_RGB_888:
70 case HAL_PIXEL_FORMAT_RGB_565:
71 case HAL_PIXEL_FORMAT_BGRA_8888:
Lajos Molnar6a26be72015-01-22 16:37:37 -080072 case HAL_PIXEL_FORMAT_Y8:
73 case HAL_PIXEL_FORMAT_Y16:
Eino-Ville Talvala5b75a512015-02-19 16:10:43 -080074 case HAL_PIXEL_FORMAT_RAW16:
Lajos Molnar6a26be72015-01-22 16:37:37 -080075 case HAL_PIXEL_FORMAT_RAW10:
76 case HAL_PIXEL_FORMAT_RAW_OPAQUE:
77 case HAL_PIXEL_FORMAT_BLOB:
78 case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED:
79 return false;
80
81 case HAL_PIXEL_FORMAT_YV12:
82 case HAL_PIXEL_FORMAT_YCbCr_420_888:
83 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
84 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
85 case HAL_PIXEL_FORMAT_YCbCr_422_I:
86 default:
87 return true;
88 }
89}
90
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -070091status_t CpuConsumer::lockNextBuffer(LockedBuffer *nativeBuffer) {
92 status_t err;
93
94 if (!nativeBuffer) return BAD_VALUE;
95 if (mCurrentLockedBuffers == mMaxLockedBuffers) {
Dan Stozad723bd72014-11-18 10:24:03 -080096 CC_LOGW("Max buffers have been locked (%zd), cannot lock anymore.",
Igor Murashkina5b75132013-08-14 18:49:12 -070097 mMaxLockedBuffers);
98 return NOT_ENOUGH_DATA;
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -070099 }
100
Dan Stozadd264162015-03-12 13:58:47 -0700101 BufferItem b;
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700102
103 Mutex::Autolock _l(mMutex);
104
Andy McFadden1585c4d2013-06-28 13:52:40 -0700105 err = acquireBufferLocked(&b, 0);
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700106 if (err != OK) {
107 if (err == BufferQueue::NO_BUFFER_AVAILABLE) {
108 return BAD_VALUE;
109 } else {
110 CC_LOGE("Error acquiring buffer: %s (%d)", strerror(err), err);
111 return err;
112 }
113 }
114
Pablo Ceballos47650f42015-08-04 16:38:17 -0700115 int slot = b.mSlot;
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700116
Eino-Ville Talvala64d8b192013-02-28 14:08:34 -0800117 void *bufferPointer = NULL;
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700118 android_ycbcr ycbcr = android_ycbcr();
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700119
Pablo Ceballos47650f42015-08-04 16:38:17 -0700120 PixelFormat format = mSlots[slot].mGraphicBuffer->getPixelFormat();
Lajos Molnar6a26be72015-01-22 16:37:37 -0800121 PixelFormat flexFormat = format;
122 if (isPossiblyYUV(format)) {
123 if (b.mFence.get()) {
Pablo Ceballos47650f42015-08-04 16:38:17 -0700124 err = mSlots[slot].mGraphicBuffer->lockAsyncYCbCr(
Riley Andrewsd53e0522014-08-18 16:57:11 -0700125 GraphicBuffer::USAGE_SW_READ_OFTEN,
126 b.mCrop,
127 &ycbcr,
128 b.mFence->dup());
Riley Andrewsd53e0522014-08-18 16:57:11 -0700129 } else {
Pablo Ceballos47650f42015-08-04 16:38:17 -0700130 err = mSlots[slot].mGraphicBuffer->lockYCbCr(
Lajos Molnar6a26be72015-01-22 16:37:37 -0800131 GraphicBuffer::USAGE_SW_READ_OFTEN,
132 b.mCrop,
133 &ycbcr);
134 }
135 if (err == OK) {
136 bufferPointer = ycbcr.y;
137 flexFormat = HAL_PIXEL_FORMAT_YCbCr_420_888;
138 if (format != HAL_PIXEL_FORMAT_YCbCr_420_888) {
139 CC_LOGV("locking buffer of format %#x as flex YUV", format);
140 }
141 } else if (format == HAL_PIXEL_FORMAT_YCbCr_420_888) {
142 CC_LOGE("Unable to lock YCbCr buffer for CPU reading: %s (%d)",
143 strerror(-err), err);
144 return err;
145 }
146 }
147
148 if (bufferPointer == NULL) { // not flexible YUV
149 if (b.mFence.get()) {
Pablo Ceballos47650f42015-08-04 16:38:17 -0700150 err = mSlots[slot].mGraphicBuffer->lockAsync(
Riley Andrewsd53e0522014-08-18 16:57:11 -0700151 GraphicBuffer::USAGE_SW_READ_OFTEN,
152 b.mCrop,
153 &bufferPointer,
154 b.mFence->dup());
Riley Andrewsd53e0522014-08-18 16:57:11 -0700155 } else {
Pablo Ceballos47650f42015-08-04 16:38:17 -0700156 err = mSlots[slot].mGraphicBuffer->lock(
Riley Andrewsd53e0522014-08-18 16:57:11 -0700157 GraphicBuffer::USAGE_SW_READ_OFTEN,
158 b.mCrop,
159 &bufferPointer);
Lajos Molnar6a26be72015-01-22 16:37:37 -0800160 }
161 if (err != OK) {
162 CC_LOGE("Unable to lock buffer for CPU reading: %s (%d)",
163 strerror(-err), err);
164 return err;
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700165 }
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700166 }
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700167
Eino-Ville Talvala042ecee2013-02-28 18:23:24 -0800168 size_t lockedIdx = 0;
Dan Stozad723bd72014-11-18 10:24:03 -0800169 for (; lockedIdx < static_cast<size_t>(mMaxLockedBuffers); lockedIdx++) {
Eino-Ville Talvala042ecee2013-02-28 18:23:24 -0800170 if (mAcquiredBuffers[lockedIdx].mSlot ==
171 BufferQueue::INVALID_BUFFER_SLOT) {
172 break;
173 }
174 }
175 assert(lockedIdx < mMaxLockedBuffers);
176
177 AcquiredBuffer &ab = mAcquiredBuffers.editItemAt(lockedIdx);
Pablo Ceballos47650f42015-08-04 16:38:17 -0700178 ab.mSlot = slot;
Eino-Ville Talvala042ecee2013-02-28 18:23:24 -0800179 ab.mBufferPointer = bufferPointer;
Pablo Ceballos47650f42015-08-04 16:38:17 -0700180 ab.mGraphicBuffer = mSlots[slot].mGraphicBuffer;
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700181
Eino-Ville Talvala64d8b192013-02-28 14:08:34 -0800182 nativeBuffer->data =
183 reinterpret_cast<uint8_t*>(bufferPointer);
Pablo Ceballos47650f42015-08-04 16:38:17 -0700184 nativeBuffer->width = mSlots[slot].mGraphicBuffer->getWidth();
185 nativeBuffer->height = mSlots[slot].mGraphicBuffer->getHeight();
Lajos Molnar6a26be72015-01-22 16:37:37 -0800186 nativeBuffer->format = format;
187 nativeBuffer->flexFormat = flexFormat;
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700188 nativeBuffer->stride = (ycbcr.y != NULL) ?
Dan Stozad723bd72014-11-18 10:24:03 -0800189 static_cast<uint32_t>(ycbcr.ystride) :
Pablo Ceballos47650f42015-08-04 16:38:17 -0700190 mSlots[slot].mGraphicBuffer->getStride();
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700191
192 nativeBuffer->crop = b.mCrop;
193 nativeBuffer->transform = b.mTransform;
194 nativeBuffer->scalingMode = b.mScalingMode;
195 nativeBuffer->timestamp = b.mTimestamp;
Eino-Ville Talvala5b75a512015-02-19 16:10:43 -0800196 nativeBuffer->dataSpace = b.mDataSpace;
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700197 nativeBuffer->frameNumber = b.mFrameNumber;
198
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700199 nativeBuffer->dataCb = reinterpret_cast<uint8_t*>(ycbcr.cb);
200 nativeBuffer->dataCr = reinterpret_cast<uint8_t*>(ycbcr.cr);
Dan Stozad723bd72014-11-18 10:24:03 -0800201 nativeBuffer->chromaStride = static_cast<uint32_t>(ycbcr.cstride);
202 nativeBuffer->chromaStep = static_cast<uint32_t>(ycbcr.chroma_step);
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700203
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700204 mCurrentLockedBuffers++;
205
206 return OK;
207}
208
209status_t CpuConsumer::unlockBuffer(const LockedBuffer &nativeBuffer) {
210 Mutex::Autolock _l(mMutex);
Eino-Ville Talvala042ecee2013-02-28 18:23:24 -0800211 size_t lockedIdx = 0;
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700212
213 void *bufPtr = reinterpret_cast<void *>(nativeBuffer.data);
Dan Stozad723bd72014-11-18 10:24:03 -0800214 for (; lockedIdx < static_cast<size_t>(mMaxLockedBuffers); lockedIdx++) {
Eino-Ville Talvala042ecee2013-02-28 18:23:24 -0800215 if (bufPtr == mAcquiredBuffers[lockedIdx].mBufferPointer) break;
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700216 }
Eino-Ville Talvala042ecee2013-02-28 18:23:24 -0800217 if (lockedIdx == mMaxLockedBuffers) {
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700218 CC_LOGE("%s: Can't find buffer to free", __FUNCTION__);
219 return BAD_VALUE;
220 }
221
Eino-Ville Talvala042ecee2013-02-28 18:23:24 -0800222 return releaseAcquiredBufferLocked(lockedIdx);
223}
224
Dan Stozad723bd72014-11-18 10:24:03 -0800225status_t CpuConsumer::releaseAcquiredBufferLocked(size_t lockedIdx) {
Eino-Ville Talvala042ecee2013-02-28 18:23:24 -0800226 status_t err;
Riley Andrewsd53e0522014-08-18 16:57:11 -0700227 int fd = -1;
Eino-Ville Talvala042ecee2013-02-28 18:23:24 -0800228
Riley Andrewsd53e0522014-08-18 16:57:11 -0700229 err = mAcquiredBuffers[lockedIdx].mGraphicBuffer->unlockAsync(&fd);
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700230 if (err != OK) {
Dan Stozad723bd72014-11-18 10:24:03 -0800231 CC_LOGE("%s: Unable to unlock graphic buffer %zd", __FUNCTION__,
Eino-Ville Talvala042ecee2013-02-28 18:23:24 -0800232 lockedIdx);
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700233 return err;
234 }
Eino-Ville Talvala042ecee2013-02-28 18:23:24 -0800235 int buf = mAcquiredBuffers[lockedIdx].mSlot;
Riley Andrewsd53e0522014-08-18 16:57:11 -0700236 if (CC_LIKELY(fd != -1)) {
237 sp<Fence> fence(new Fence(fd));
238 addReleaseFenceLocked(
239 mAcquiredBuffers[lockedIdx].mSlot,
240 mSlots[buf].mGraphicBuffer,
241 fence);
242 }
Eino-Ville Talvala042ecee2013-02-28 18:23:24 -0800243
244 // release the buffer if it hasn't already been freed by the BufferQueue.
245 // This can happen, for example, when the producer of this buffer
246 // disconnected after this buffer was acquired.
247 if (CC_LIKELY(mAcquiredBuffers[lockedIdx].mGraphicBuffer ==
248 mSlots[buf].mGraphicBuffer)) {
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700249 releaseBufferLocked(
250 buf, mAcquiredBuffers[lockedIdx].mGraphicBuffer,
251 EGL_NO_DISPLAY, EGL_NO_SYNC_KHR);
Eino-Ville Talvala042ecee2013-02-28 18:23:24 -0800252 }
253
254 AcquiredBuffer &ab = mAcquiredBuffers.editItemAt(lockedIdx);
255 ab.mSlot = BufferQueue::INVALID_BUFFER_SLOT;
256 ab.mBufferPointer = NULL;
257 ab.mGraphicBuffer.clear();
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700258
259 mCurrentLockedBuffers--;
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700260 return OK;
261}
262
Eino-Ville Talvalaf57e7542012-08-20 15:44:40 -0700263void CpuConsumer::freeBufferLocked(int slotIndex) {
Eino-Ville Talvalaf57e7542012-08-20 15:44:40 -0700264 ConsumerBase::freeBufferLocked(slotIndex);
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700265}
266
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700267} // namespace android