blob: e55e108558d4f88849b3ab7c2a3abb83e5c1af12 [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"
19#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>
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -070023#include <gui/CpuConsumer.h>
24
Dan Albert8b491252014-09-08 18:53:39 -070025#define CC_LOGV(x, ...) ALOGV("[%s] " x, mName.string(), ##__VA_ARGS__)
26#define CC_LOGD(x, ...) ALOGD("[%s] " x, mName.string(), ##__VA_ARGS__)
27#define CC_LOGI(x, ...) ALOGI("[%s] " x, mName.string(), ##__VA_ARGS__)
28#define CC_LOGW(x, ...) ALOGW("[%s] " x, mName.string(), ##__VA_ARGS__)
29#define CC_LOGE(x, ...) ALOGE("[%s] " x, mName.string(), ##__VA_ARGS__)
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -070030
31namespace android {
32
Mathias Agopiandb89edc2013-08-02 01:40:18 -070033CpuConsumer::CpuConsumer(const sp<IGraphicBufferConsumer>& bq,
Mathias Agopian595264f2013-07-16 22:56:09 -070034 uint32_t maxLockedBuffers, bool controlledByApp) :
35 ConsumerBase(bq, controlledByApp),
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -070036 mMaxLockedBuffers(maxLockedBuffers),
37 mCurrentLockedBuffers(0)
38{
Eino-Ville Talvala042ecee2013-02-28 18:23:24 -080039 // Create tracking entries for locked buffers
40 mAcquiredBuffers.insertAt(0, maxLockedBuffers);
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -070041
Mathias Agopiandb89edc2013-08-02 01:40:18 -070042 mConsumer->setConsumerUsageBits(GRALLOC_USAGE_SW_READ_OFTEN);
43 mConsumer->setMaxAcquiredBufferCount(maxLockedBuffers);
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -070044}
45
Eino-Ville Talvalae232fdc2012-08-21 13:37:35 -070046CpuConsumer::~CpuConsumer() {
Eino-Ville Talvala042ecee2013-02-28 18:23:24 -080047 // ConsumerBase destructor does all the work.
Eino-Ville Talvalae232fdc2012-08-21 13:37:35 -070048}
49
Eino-Ville Talvala042ecee2013-02-28 18:23:24 -080050
51
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -070052void CpuConsumer::setName(const String8& name) {
53 Mutex::Autolock _l(mMutex);
54 mName = name;
Mathias Agopiandb89edc2013-08-02 01:40:18 -070055 mConsumer->setConsumerName(name);
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -070056}
57
Zhijun Heb4b63702013-06-06 11:59:21 -070058status_t CpuConsumer::setDefaultBufferSize(uint32_t width, uint32_t height)
59{
60 Mutex::Autolock _l(mMutex);
Mathias Agopiandb89edc2013-08-02 01:40:18 -070061 return mConsumer->setDefaultBufferSize(width, height);
Zhijun Heb4b63702013-06-06 11:59:21 -070062}
63
64status_t CpuConsumer::setDefaultBufferFormat(uint32_t defaultFormat)
65{
66 Mutex::Autolock _l(mMutex);
Mathias Agopiandb89edc2013-08-02 01:40:18 -070067 return mConsumer->setDefaultBufferFormat(defaultFormat);
Zhijun Heb4b63702013-06-06 11:59:21 -070068}
69
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -070070status_t CpuConsumer::lockNextBuffer(LockedBuffer *nativeBuffer) {
71 status_t err;
72
73 if (!nativeBuffer) return BAD_VALUE;
74 if (mCurrentLockedBuffers == mMaxLockedBuffers) {
Igor Murashkina5b75132013-08-14 18:49:12 -070075 CC_LOGW("Max buffers have been locked (%d), cannot lock anymore.",
76 mMaxLockedBuffers);
77 return NOT_ENOUGH_DATA;
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -070078 }
79
80 BufferQueue::BufferItem b;
81
82 Mutex::Autolock _l(mMutex);
83
Andy McFadden1585c4d2013-06-28 13:52:40 -070084 err = acquireBufferLocked(&b, 0);
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -070085 if (err != OK) {
86 if (err == BufferQueue::NO_BUFFER_AVAILABLE) {
87 return BAD_VALUE;
88 } else {
89 CC_LOGE("Error acquiring buffer: %s (%d)", strerror(err), err);
90 return err;
91 }
92 }
93
94 int buf = b.mBuf;
95
Eino-Ville Talvala64d8b192013-02-28 14:08:34 -080096 void *bufferPointer = NULL;
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -070097 android_ycbcr ycbcr = android_ycbcr();
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -070098
Riley Andrewsd53e0522014-08-18 16:57:11 -070099 if (b.mFence.get()) {
100 if (mSlots[buf].mGraphicBuffer->getPixelFormat() ==
101 HAL_PIXEL_FORMAT_YCbCr_420_888) {
102 err = mSlots[buf].mGraphicBuffer->lockAsyncYCbCr(
103 GraphicBuffer::USAGE_SW_READ_OFTEN,
104 b.mCrop,
105 &ycbcr,
106 b.mFence->dup());
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700107
Riley Andrewsd53e0522014-08-18 16:57:11 -0700108 if (err != OK) {
109 CC_LOGE("Unable to lock YCbCr buffer for CPU reading: %s (%d)",
110 strerror(-err), err);
111 return err;
112 }
113 bufferPointer = ycbcr.y;
114 } else {
115 err = mSlots[buf].mGraphicBuffer->lockAsync(
116 GraphicBuffer::USAGE_SW_READ_OFTEN,
117 b.mCrop,
118 &bufferPointer,
119 b.mFence->dup());
120
121 if (err != OK) {
122 CC_LOGE("Unable to lock buffer for CPU reading: %s (%d)",
123 strerror(-err), err);
124 return err;
125 }
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700126 }
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700127 } else {
Riley Andrewsd53e0522014-08-18 16:57:11 -0700128 if (mSlots[buf].mGraphicBuffer->getPixelFormat() ==
129 HAL_PIXEL_FORMAT_YCbCr_420_888) {
130 err = mSlots[buf].mGraphicBuffer->lockYCbCr(
131 GraphicBuffer::USAGE_SW_READ_OFTEN,
132 b.mCrop,
133 &ycbcr);
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700134
Riley Andrewsd53e0522014-08-18 16:57:11 -0700135 if (err != OK) {
136 CC_LOGE("Unable to lock YCbCr buffer for CPU reading: %s (%d)",
137 strerror(-err), err);
138 return err;
139 }
140 bufferPointer = ycbcr.y;
141 } else {
142 err = mSlots[buf].mGraphicBuffer->lock(
143 GraphicBuffer::USAGE_SW_READ_OFTEN,
144 b.mCrop,
145 &bufferPointer);
146
147 if (err != OK) {
148 CC_LOGE("Unable to lock buffer for CPU reading: %s (%d)",
149 strerror(-err), err);
150 return err;
151 }
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700152 }
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700153 }
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700154
Eino-Ville Talvala042ecee2013-02-28 18:23:24 -0800155 size_t lockedIdx = 0;
156 for (; lockedIdx < mMaxLockedBuffers; lockedIdx++) {
157 if (mAcquiredBuffers[lockedIdx].mSlot ==
158 BufferQueue::INVALID_BUFFER_SLOT) {
159 break;
160 }
161 }
162 assert(lockedIdx < mMaxLockedBuffers);
163
164 AcquiredBuffer &ab = mAcquiredBuffers.editItemAt(lockedIdx);
165 ab.mSlot = buf;
166 ab.mBufferPointer = bufferPointer;
167 ab.mGraphicBuffer = mSlots[buf].mGraphicBuffer;
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700168
Eino-Ville Talvala64d8b192013-02-28 14:08:34 -0800169 nativeBuffer->data =
170 reinterpret_cast<uint8_t*>(bufferPointer);
Eino-Ville Talvalaf57e7542012-08-20 15:44:40 -0700171 nativeBuffer->width = mSlots[buf].mGraphicBuffer->getWidth();
172 nativeBuffer->height = mSlots[buf].mGraphicBuffer->getHeight();
173 nativeBuffer->format = mSlots[buf].mGraphicBuffer->getPixelFormat();
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700174 nativeBuffer->stride = (ycbcr.y != NULL) ?
175 ycbcr.ystride :
176 mSlots[buf].mGraphicBuffer->getStride();
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700177
178 nativeBuffer->crop = b.mCrop;
179 nativeBuffer->transform = b.mTransform;
180 nativeBuffer->scalingMode = b.mScalingMode;
181 nativeBuffer->timestamp = b.mTimestamp;
182 nativeBuffer->frameNumber = b.mFrameNumber;
183
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700184 nativeBuffer->dataCb = reinterpret_cast<uint8_t*>(ycbcr.cb);
185 nativeBuffer->dataCr = reinterpret_cast<uint8_t*>(ycbcr.cr);
186 nativeBuffer->chromaStride = ycbcr.cstride;
187 nativeBuffer->chromaStep = ycbcr.chroma_step;
188
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700189 mCurrentLockedBuffers++;
190
191 return OK;
192}
193
194status_t CpuConsumer::unlockBuffer(const LockedBuffer &nativeBuffer) {
195 Mutex::Autolock _l(mMutex);
Eino-Ville Talvala042ecee2013-02-28 18:23:24 -0800196 size_t lockedIdx = 0;
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700197 status_t err;
198
199 void *bufPtr = reinterpret_cast<void *>(nativeBuffer.data);
Eino-Ville Talvala042ecee2013-02-28 18:23:24 -0800200 for (; lockedIdx < mMaxLockedBuffers; lockedIdx++) {
201 if (bufPtr == mAcquiredBuffers[lockedIdx].mBufferPointer) break;
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700202 }
Eino-Ville Talvala042ecee2013-02-28 18:23:24 -0800203 if (lockedIdx == mMaxLockedBuffers) {
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700204 CC_LOGE("%s: Can't find buffer to free", __FUNCTION__);
205 return BAD_VALUE;
206 }
207
Eino-Ville Talvala042ecee2013-02-28 18:23:24 -0800208 return releaseAcquiredBufferLocked(lockedIdx);
209}
210
211status_t CpuConsumer::releaseAcquiredBufferLocked(int lockedIdx) {
212 status_t err;
Riley Andrewsd53e0522014-08-18 16:57:11 -0700213 int fd = -1;
Eino-Ville Talvala042ecee2013-02-28 18:23:24 -0800214
Riley Andrewsd53e0522014-08-18 16:57:11 -0700215 err = mAcquiredBuffers[lockedIdx].mGraphicBuffer->unlockAsync(&fd);
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700216 if (err != OK) {
Eino-Ville Talvala042ecee2013-02-28 18:23:24 -0800217 CC_LOGE("%s: Unable to unlock graphic buffer %d", __FUNCTION__,
218 lockedIdx);
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700219 return err;
220 }
Eino-Ville Talvala042ecee2013-02-28 18:23:24 -0800221 int buf = mAcquiredBuffers[lockedIdx].mSlot;
Riley Andrewsd53e0522014-08-18 16:57:11 -0700222 if (CC_LIKELY(fd != -1)) {
223 sp<Fence> fence(new Fence(fd));
224 addReleaseFenceLocked(
225 mAcquiredBuffers[lockedIdx].mSlot,
226 mSlots[buf].mGraphicBuffer,
227 fence);
228 }
Eino-Ville Talvala042ecee2013-02-28 18:23:24 -0800229
230 // release the buffer if it hasn't already been freed by the BufferQueue.
231 // This can happen, for example, when the producer of this buffer
232 // disconnected after this buffer was acquired.
233 if (CC_LIKELY(mAcquiredBuffers[lockedIdx].mGraphicBuffer ==
234 mSlots[buf].mGraphicBuffer)) {
Lajos Molnarc5d7b7d2013-05-03 14:50:50 -0700235 releaseBufferLocked(
236 buf, mAcquiredBuffers[lockedIdx].mGraphicBuffer,
237 EGL_NO_DISPLAY, EGL_NO_SYNC_KHR);
Eino-Ville Talvala042ecee2013-02-28 18:23:24 -0800238 }
239
240 AcquiredBuffer &ab = mAcquiredBuffers.editItemAt(lockedIdx);
241 ab.mSlot = BufferQueue::INVALID_BUFFER_SLOT;
242 ab.mBufferPointer = NULL;
243 ab.mGraphicBuffer.clear();
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700244
245 mCurrentLockedBuffers--;
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700246 return OK;
247}
248
Eino-Ville Talvalaf57e7542012-08-20 15:44:40 -0700249void CpuConsumer::freeBufferLocked(int slotIndex) {
Eino-Ville Talvalaf57e7542012-08-20 15:44:40 -0700250 ConsumerBase::freeBufferLocked(slotIndex);
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700251}
252
253} // namespace android