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
21 #include <cutils/compiler.h>
22 #include <utils/Log.h>
23 #include <gui/BufferItem.h>
24 #include <gui/CpuConsumer.h>
25
26 #define CC_LOGV(x, ...) ALOGV("[%s] " x, mName.string(), ##__VA_ARGS__)
27 //#define CC_LOGD(x, ...) ALOGD("[%s] " x, mName.string(), ##__VA_ARGS__)
28 //#define CC_LOGI(x, ...) ALOGI("[%s] " x, mName.string(), ##__VA_ARGS__)
29 #define CC_LOGW(x, ...) ALOGW("[%s] " x, mName.string(), ##__VA_ARGS__)
30 #define CC_LOGE(x, ...) ALOGE("[%s] " x, mName.string(), ##__VA_ARGS__)
31
32 namespace android {
33
CpuConsumer(const sp<IGraphicBufferConsumer> & bq,size_t maxLockedBuffers,bool controlledByApp)34 CpuConsumer::CpuConsumer(const sp<IGraphicBufferConsumer>& bq,
35 size_t maxLockedBuffers, bool controlledByApp) :
36 ConsumerBase(bq, controlledByApp),
37 mMaxLockedBuffers(maxLockedBuffers),
38 mCurrentLockedBuffers(0)
39 {
40 // Create tracking entries for locked buffers
41 mAcquiredBuffers.insertAt(0, maxLockedBuffers);
42
43 mConsumer->setConsumerUsageBits(GRALLOC_USAGE_SW_READ_OFTEN);
44 mConsumer->setMaxAcquiredBufferCount(static_cast<int32_t>(maxLockedBuffers));
45 }
46
~CpuConsumer()47 CpuConsumer::~CpuConsumer() {
48 // ConsumerBase destructor does all the work.
49 }
50
51
52
setName(const String8 & name)53 void CpuConsumer::setName(const String8& name) {
54 Mutex::Autolock _l(mMutex);
55 if (mAbandoned) {
56 CC_LOGE("setName: CpuConsumer is abandoned!");
57 return;
58 }
59 mName = name;
60 mConsumer->setConsumerName(name);
61 }
62
isPossiblyYUV(PixelFormat format)63 static bool isPossiblyYUV(PixelFormat format) {
64 switch (static_cast<int>(format)) {
65 case HAL_PIXEL_FORMAT_RGBA_8888:
66 case HAL_PIXEL_FORMAT_RGBX_8888:
67 case HAL_PIXEL_FORMAT_RGBA_FP16:
68 case HAL_PIXEL_FORMAT_RGBA_1010102:
69 case HAL_PIXEL_FORMAT_RGB_888:
70 case HAL_PIXEL_FORMAT_RGB_565:
71 case HAL_PIXEL_FORMAT_BGRA_8888:
72 case HAL_PIXEL_FORMAT_Y8:
73 case HAL_PIXEL_FORMAT_Y16:
74 case HAL_PIXEL_FORMAT_RAW16:
75 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
lockNextBuffer(LockedBuffer * nativeBuffer)91 status_t CpuConsumer::lockNextBuffer(LockedBuffer *nativeBuffer) {
92 status_t err;
93
94 if (!nativeBuffer) return BAD_VALUE;
95 if (mCurrentLockedBuffers == mMaxLockedBuffers) {
96 CC_LOGW("Max buffers have been locked (%zd), cannot lock anymore.",
97 mMaxLockedBuffers);
98 return NOT_ENOUGH_DATA;
99 }
100
101 BufferItem b;
102
103 Mutex::Autolock _l(mMutex);
104
105 err = acquireBufferLocked(&b, 0);
106 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
115 int slot = b.mSlot;
116
117 void *bufferPointer = NULL;
118 android_ycbcr ycbcr = android_ycbcr();
119
120 PixelFormat format = mSlots[slot].mGraphicBuffer->getPixelFormat();
121 PixelFormat flexFormat = format;
122 if (isPossiblyYUV(format)) {
123 if (b.mFence.get()) {
124 err = mSlots[slot].mGraphicBuffer->lockAsyncYCbCr(
125 GraphicBuffer::USAGE_SW_READ_OFTEN,
126 b.mCrop,
127 &ycbcr,
128 b.mFence->dup());
129 } else {
130 err = mSlots[slot].mGraphicBuffer->lockYCbCr(
131 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()) {
150 err = mSlots[slot].mGraphicBuffer->lockAsync(
151 GraphicBuffer::USAGE_SW_READ_OFTEN,
152 b.mCrop,
153 &bufferPointer,
154 b.mFence->dup());
155 } else {
156 err = mSlots[slot].mGraphicBuffer->lock(
157 GraphicBuffer::USAGE_SW_READ_OFTEN,
158 b.mCrop,
159 &bufferPointer);
160 }
161 if (err != OK) {
162 CC_LOGE("Unable to lock buffer for CPU reading: %s (%d)",
163 strerror(-err), err);
164 return err;
165 }
166 }
167
168 size_t lockedIdx = 0;
169 for (; lockedIdx < static_cast<size_t>(mMaxLockedBuffers); lockedIdx++) {
170 if (mAcquiredBuffers[lockedIdx].mSlot ==
171 BufferQueue::INVALID_BUFFER_SLOT) {
172 break;
173 }
174 }
175 assert(lockedIdx < mMaxLockedBuffers);
176
177 AcquiredBuffer &ab = mAcquiredBuffers.editItemAt(lockedIdx);
178 ab.mSlot = slot;
179 ab.mBufferPointer = bufferPointer;
180 ab.mGraphicBuffer = mSlots[slot].mGraphicBuffer;
181
182 nativeBuffer->data =
183 reinterpret_cast<uint8_t*>(bufferPointer);
184 nativeBuffer->width = mSlots[slot].mGraphicBuffer->getWidth();
185 nativeBuffer->height = mSlots[slot].mGraphicBuffer->getHeight();
186 nativeBuffer->format = format;
187 nativeBuffer->flexFormat = flexFormat;
188 nativeBuffer->stride = (ycbcr.y != NULL) ?
189 static_cast<uint32_t>(ycbcr.ystride) :
190 mSlots[slot].mGraphicBuffer->getStride();
191
192 nativeBuffer->crop = b.mCrop;
193 nativeBuffer->transform = b.mTransform;
194 nativeBuffer->scalingMode = b.mScalingMode;
195 nativeBuffer->timestamp = b.mTimestamp;
196 nativeBuffer->dataSpace = b.mDataSpace;
197 nativeBuffer->frameNumber = b.mFrameNumber;
198
199 nativeBuffer->dataCb = reinterpret_cast<uint8_t*>(ycbcr.cb);
200 nativeBuffer->dataCr = reinterpret_cast<uint8_t*>(ycbcr.cr);
201 nativeBuffer->chromaStride = static_cast<uint32_t>(ycbcr.cstride);
202 nativeBuffer->chromaStep = static_cast<uint32_t>(ycbcr.chroma_step);
203
204 mCurrentLockedBuffers++;
205
206 return OK;
207 }
208
unlockBuffer(const LockedBuffer & nativeBuffer)209 status_t CpuConsumer::unlockBuffer(const LockedBuffer &nativeBuffer) {
210 Mutex::Autolock _l(mMutex);
211 size_t lockedIdx = 0;
212
213 void *bufPtr = reinterpret_cast<void *>(nativeBuffer.data);
214 for (; lockedIdx < static_cast<size_t>(mMaxLockedBuffers); lockedIdx++) {
215 if (bufPtr == mAcquiredBuffers[lockedIdx].mBufferPointer) break;
216 }
217 if (lockedIdx == mMaxLockedBuffers) {
218 CC_LOGE("%s: Can't find buffer to free", __FUNCTION__);
219 return BAD_VALUE;
220 }
221
222 return releaseAcquiredBufferLocked(lockedIdx);
223 }
224
releaseAcquiredBufferLocked(size_t lockedIdx)225 status_t CpuConsumer::releaseAcquiredBufferLocked(size_t lockedIdx) {
226 status_t err;
227 int fd = -1;
228
229 err = mAcquiredBuffers[lockedIdx].mGraphicBuffer->unlockAsync(&fd);
230 if (err != OK) {
231 CC_LOGE("%s: Unable to unlock graphic buffer %zd", __FUNCTION__,
232 lockedIdx);
233 return err;
234 }
235 int buf = mAcquiredBuffers[lockedIdx].mSlot;
236 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 }
243
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)) {
249 releaseBufferLocked(
250 buf, mAcquiredBuffers[lockedIdx].mGraphicBuffer,
251 EGL_NO_DISPLAY, EGL_NO_SYNC_KHR);
252 }
253
254 AcquiredBuffer &ab = mAcquiredBuffers.editItemAt(lockedIdx);
255 ab.mSlot = BufferQueue::INVALID_BUFFER_SLOT;
256 ab.mBufferPointer = NULL;
257 ab.mGraphicBuffer.clear();
258
259 mCurrentLockedBuffers--;
260 return OK;
261 }
262
freeBufferLocked(int slotIndex)263 void CpuConsumer::freeBufferLocked(int slotIndex) {
264 ConsumerBase::freeBufferLocked(slotIndex);
265 }
266
267 } // namespace android
268