blob: 054364997ec0991fefaa1aa6bda3a4181db228dd [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
25#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__)
30
31namespace android {
32
Eino-Ville Talvalaeb0d1292013-02-28 11:01:32 -080033CpuConsumer::CpuConsumer(uint32_t maxLockedBuffers, bool synchronousMode) :
Jamie Gennis72f096f2012-08-27 18:48:37 -070034 ConsumerBase(new BufferQueue(true) ),
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -070035 mMaxLockedBuffers(maxLockedBuffers),
36 mCurrentLockedBuffers(0)
37{
Eino-Ville Talvala042ecee2013-02-28 18:23:24 -080038 // Create tracking entries for locked buffers
39 mAcquiredBuffers.insertAt(0, maxLockedBuffers);
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -070040
Eino-Ville Talvalaeb0d1292013-02-28 11:01:32 -080041 mBufferQueue->setSynchronousMode(synchronousMode);
Eino-Ville Talvalaf57e7542012-08-20 15:44:40 -070042 mBufferQueue->setConsumerUsageBits(GRALLOC_USAGE_SW_READ_OFTEN);
Jamie Gennis72f096f2012-08-27 18:48:37 -070043 mBufferQueue->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;
55 mBufferQueue->setConsumerName(name);
56}
57
58status_t CpuConsumer::lockNextBuffer(LockedBuffer *nativeBuffer) {
59 status_t err;
60
61 if (!nativeBuffer) return BAD_VALUE;
62 if (mCurrentLockedBuffers == mMaxLockedBuffers) {
63 return INVALID_OPERATION;
64 }
65
66 BufferQueue::BufferItem b;
67
68 Mutex::Autolock _l(mMutex);
69
Eino-Ville Talvalaf57e7542012-08-20 15:44:40 -070070 err = acquireBufferLocked(&b);
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -070071 if (err != OK) {
72 if (err == BufferQueue::NO_BUFFER_AVAILABLE) {
73 return BAD_VALUE;
74 } else {
75 CC_LOGE("Error acquiring buffer: %s (%d)", strerror(err), err);
76 return err;
77 }
78 }
79
80 int buf = b.mBuf;
81
Jesse Hallb42b1ac2012-06-28 14:27:53 -070082 if (b.mFence.get()) {
Mathias Agopianea74d3b2013-05-16 18:03:22 -070083 err = b.mFence->waitForever("CpuConsumer::lockNextBuffer");
Jesse Hallb42b1ac2012-06-28 14:27:53 -070084 if (err != OK) {
85 CC_LOGE("Failed to wait for fence of acquired buffer: %s (%d)",
86 strerror(-err), err);
87 return err;
88 }
89 }
90
Eino-Ville Talvala64d8b192013-02-28 14:08:34 -080091 void *bufferPointer = NULL;
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -070092 android_ycbcr ycbcr = android_ycbcr();
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -070093
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -070094 if (mSlots[buf].mGraphicBuffer->getPixelFormat() ==
95 HAL_PIXEL_FORMAT_YCbCr_420_888) {
96 err = mSlots[buf].mGraphicBuffer->lockYCbCr(
97 GraphicBuffer::USAGE_SW_READ_OFTEN,
98 b.mCrop,
99 &ycbcr);
100
101 if (err != OK) {
102 CC_LOGE("Unable to lock YCbCr buffer for CPU reading: %s (%d)",
103 strerror(-err), err);
104 return err;
105 }
106 bufferPointer = ycbcr.y;
107 } else {
108 err = mSlots[buf].mGraphicBuffer->lock(
109 GraphicBuffer::USAGE_SW_READ_OFTEN,
110 b.mCrop,
111 &bufferPointer);
112
113 if (err != OK) {
114 CC_LOGE("Unable to lock buffer for CPU reading: %s (%d)",
115 strerror(-err), err);
116 return err;
117 }
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700118 }
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700119
Eino-Ville Talvala042ecee2013-02-28 18:23:24 -0800120 size_t lockedIdx = 0;
121 for (; lockedIdx < mMaxLockedBuffers; lockedIdx++) {
122 if (mAcquiredBuffers[lockedIdx].mSlot ==
123 BufferQueue::INVALID_BUFFER_SLOT) {
124 break;
125 }
126 }
127 assert(lockedIdx < mMaxLockedBuffers);
128
129 AcquiredBuffer &ab = mAcquiredBuffers.editItemAt(lockedIdx);
130 ab.mSlot = buf;
131 ab.mBufferPointer = bufferPointer;
132 ab.mGraphicBuffer = mSlots[buf].mGraphicBuffer;
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700133
Eino-Ville Talvala64d8b192013-02-28 14:08:34 -0800134 nativeBuffer->data =
135 reinterpret_cast<uint8_t*>(bufferPointer);
Eino-Ville Talvalaf57e7542012-08-20 15:44:40 -0700136 nativeBuffer->width = mSlots[buf].mGraphicBuffer->getWidth();
137 nativeBuffer->height = mSlots[buf].mGraphicBuffer->getHeight();
138 nativeBuffer->format = mSlots[buf].mGraphicBuffer->getPixelFormat();
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700139 nativeBuffer->stride = (ycbcr.y != NULL) ?
140 ycbcr.ystride :
141 mSlots[buf].mGraphicBuffer->getStride();
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700142
143 nativeBuffer->crop = b.mCrop;
144 nativeBuffer->transform = b.mTransform;
145 nativeBuffer->scalingMode = b.mScalingMode;
146 nativeBuffer->timestamp = b.mTimestamp;
147 nativeBuffer->frameNumber = b.mFrameNumber;
148
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700149 nativeBuffer->dataCb = reinterpret_cast<uint8_t*>(ycbcr.cb);
150 nativeBuffer->dataCr = reinterpret_cast<uint8_t*>(ycbcr.cr);
151 nativeBuffer->chromaStride = ycbcr.cstride;
152 nativeBuffer->chromaStep = ycbcr.chroma_step;
153
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700154 mCurrentLockedBuffers++;
155
156 return OK;
157}
158
159status_t CpuConsumer::unlockBuffer(const LockedBuffer &nativeBuffer) {
160 Mutex::Autolock _l(mMutex);
Eino-Ville Talvala042ecee2013-02-28 18:23:24 -0800161 size_t lockedIdx = 0;
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700162 status_t err;
163
164 void *bufPtr = reinterpret_cast<void *>(nativeBuffer.data);
Eino-Ville Talvala042ecee2013-02-28 18:23:24 -0800165 for (; lockedIdx < mMaxLockedBuffers; lockedIdx++) {
166 if (bufPtr == mAcquiredBuffers[lockedIdx].mBufferPointer) break;
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700167 }
Eino-Ville Talvala042ecee2013-02-28 18:23:24 -0800168 if (lockedIdx == mMaxLockedBuffers) {
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700169 CC_LOGE("%s: Can't find buffer to free", __FUNCTION__);
170 return BAD_VALUE;
171 }
172
Eino-Ville Talvala042ecee2013-02-28 18:23:24 -0800173 return releaseAcquiredBufferLocked(lockedIdx);
174}
175
176status_t CpuConsumer::releaseAcquiredBufferLocked(int lockedIdx) {
177 status_t err;
178
179 err = mAcquiredBuffers[lockedIdx].mGraphicBuffer->unlock();
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700180 if (err != OK) {
Eino-Ville Talvala042ecee2013-02-28 18:23:24 -0800181 CC_LOGE("%s: Unable to unlock graphic buffer %d", __FUNCTION__,
182 lockedIdx);
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700183 return err;
184 }
Eino-Ville Talvala042ecee2013-02-28 18:23:24 -0800185 int buf = mAcquiredBuffers[lockedIdx].mSlot;
186
187 // release the buffer if it hasn't already been freed by the BufferQueue.
188 // This can happen, for example, when the producer of this buffer
189 // disconnected after this buffer was acquired.
190 if (CC_LIKELY(mAcquiredBuffers[lockedIdx].mGraphicBuffer ==
191 mSlots[buf].mGraphicBuffer)) {
192 releaseBufferLocked(buf, EGL_NO_DISPLAY, EGL_NO_SYNC_KHR);
193 }
194
195 AcquiredBuffer &ab = mAcquiredBuffers.editItemAt(lockedIdx);
196 ab.mSlot = BufferQueue::INVALID_BUFFER_SLOT;
197 ab.mBufferPointer = NULL;
198 ab.mGraphicBuffer.clear();
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700199
200 mCurrentLockedBuffers--;
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700201 return OK;
202}
203
Eino-Ville Talvalaf57e7542012-08-20 15:44:40 -0700204void CpuConsumer::freeBufferLocked(int slotIndex) {
Eino-Ville Talvalaf57e7542012-08-20 15:44:40 -0700205 ConsumerBase::freeBufferLocked(slotIndex);
Eino-Ville Talvalae41b3182012-04-16 17:54:33 -0700206}
207
208} // namespace android