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_RGB_888:
68 case HAL_PIXEL_FORMAT_RGB_565:
69 case HAL_PIXEL_FORMAT_BGRA_8888:
70 case HAL_PIXEL_FORMAT_Y8:
71 case HAL_PIXEL_FORMAT_Y16:
72 case HAL_PIXEL_FORMAT_RAW16:
73 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
lockNextBuffer(LockedBuffer * nativeBuffer)89 status_t CpuConsumer::lockNextBuffer(LockedBuffer *nativeBuffer) {
90 status_t err;
91
92 if (!nativeBuffer) return BAD_VALUE;
93 if (mCurrentLockedBuffers == mMaxLockedBuffers) {
94 CC_LOGW("Max buffers have been locked (%zd), cannot lock anymore.",
95 mMaxLockedBuffers);
96 return NOT_ENOUGH_DATA;
97 }
98
99 BufferItem b;
100
101 Mutex::Autolock _l(mMutex);
102
103 err = acquireBufferLocked(&b, 0);
104 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
113 int slot = b.mSlot;
114
115 void *bufferPointer = NULL;
116 android_ycbcr ycbcr = android_ycbcr();
117
118 PixelFormat format = mSlots[slot].mGraphicBuffer->getPixelFormat();
119 PixelFormat flexFormat = format;
120 if (isPossiblyYUV(format)) {
121 if (b.mFence.get()) {
122 err = mSlots[slot].mGraphicBuffer->lockAsyncYCbCr(
123 GraphicBuffer::USAGE_SW_READ_OFTEN,
124 b.mCrop,
125 &ycbcr,
126 b.mFence->dup());
127 } else {
128 err = mSlots[slot].mGraphicBuffer->lockYCbCr(
129 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()) {
148 err = mSlots[slot].mGraphicBuffer->lockAsync(
149 GraphicBuffer::USAGE_SW_READ_OFTEN,
150 b.mCrop,
151 &bufferPointer,
152 b.mFence->dup());
153 } else {
154 err = mSlots[slot].mGraphicBuffer->lock(
155 GraphicBuffer::USAGE_SW_READ_OFTEN,
156 b.mCrop,
157 &bufferPointer);
158 }
159 if (err != OK) {
160 CC_LOGE("Unable to lock buffer for CPU reading: %s (%d)",
161 strerror(-err), err);
162 return err;
163 }
164 }
165
166 size_t lockedIdx = 0;
167 for (; lockedIdx < static_cast<size_t>(mMaxLockedBuffers); lockedIdx++) {
168 if (mAcquiredBuffers[lockedIdx].mSlot ==
169 BufferQueue::INVALID_BUFFER_SLOT) {
170 break;
171 }
172 }
173 assert(lockedIdx < mMaxLockedBuffers);
174
175 AcquiredBuffer &ab = mAcquiredBuffers.editItemAt(lockedIdx);
176 ab.mSlot = slot;
177 ab.mBufferPointer = bufferPointer;
178 ab.mGraphicBuffer = mSlots[slot].mGraphicBuffer;
179
180 nativeBuffer->data =
181 reinterpret_cast<uint8_t*>(bufferPointer);
182 nativeBuffer->width = mSlots[slot].mGraphicBuffer->getWidth();
183 nativeBuffer->height = mSlots[slot].mGraphicBuffer->getHeight();
184 nativeBuffer->format = format;
185 nativeBuffer->flexFormat = flexFormat;
186 nativeBuffer->stride = (ycbcr.y != NULL) ?
187 static_cast<uint32_t>(ycbcr.ystride) :
188 mSlots[slot].mGraphicBuffer->getStride();
189
190 nativeBuffer->crop = b.mCrop;
191 nativeBuffer->transform = b.mTransform;
192 nativeBuffer->scalingMode = b.mScalingMode;
193 nativeBuffer->timestamp = b.mTimestamp;
194 nativeBuffer->dataSpace = b.mDataSpace;
195 nativeBuffer->frameNumber = b.mFrameNumber;
196
197 nativeBuffer->dataCb = reinterpret_cast<uint8_t*>(ycbcr.cb);
198 nativeBuffer->dataCr = reinterpret_cast<uint8_t*>(ycbcr.cr);
199 nativeBuffer->chromaStride = static_cast<uint32_t>(ycbcr.cstride);
200 nativeBuffer->chromaStep = static_cast<uint32_t>(ycbcr.chroma_step);
201
202 mCurrentLockedBuffers++;
203
204 return OK;
205 }
206
unlockBuffer(const LockedBuffer & nativeBuffer)207 status_t CpuConsumer::unlockBuffer(const LockedBuffer &nativeBuffer) {
208 Mutex::Autolock _l(mMutex);
209 size_t lockedIdx = 0;
210
211 void *bufPtr = reinterpret_cast<void *>(nativeBuffer.data);
212 for (; lockedIdx < static_cast<size_t>(mMaxLockedBuffers); lockedIdx++) {
213 if (bufPtr == mAcquiredBuffers[lockedIdx].mBufferPointer) break;
214 }
215 if (lockedIdx == mMaxLockedBuffers) {
216 CC_LOGE("%s: Can't find buffer to free", __FUNCTION__);
217 return BAD_VALUE;
218 }
219
220 return releaseAcquiredBufferLocked(lockedIdx);
221 }
222
releaseAcquiredBufferLocked(size_t lockedIdx)223 status_t CpuConsumer::releaseAcquiredBufferLocked(size_t lockedIdx) {
224 status_t err;
225 int fd = -1;
226
227 err = mAcquiredBuffers[lockedIdx].mGraphicBuffer->unlockAsync(&fd);
228 if (err != OK) {
229 CC_LOGE("%s: Unable to unlock graphic buffer %zd", __FUNCTION__,
230 lockedIdx);
231 return err;
232 }
233 int buf = mAcquiredBuffers[lockedIdx].mSlot;
234 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 }
241
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)) {
247 releaseBufferLocked(
248 buf, mAcquiredBuffers[lockedIdx].mGraphicBuffer,
249 EGL_NO_DISPLAY, EGL_NO_SYNC_KHR);
250 }
251
252 AcquiredBuffer &ab = mAcquiredBuffers.editItemAt(lockedIdx);
253 ab.mSlot = BufferQueue::INVALID_BUFFER_SLOT;
254 ab.mBufferPointer = NULL;
255 ab.mGraphicBuffer.clear();
256
257 mCurrentLockedBuffers--;
258 return OK;
259 }
260
freeBufferLocked(int slotIndex)261 void CpuConsumer::freeBufferLocked(int slotIndex) {
262 ConsumerBase::freeBufferLocked(slotIndex);
263 }
264
265 } // namespace android
266