• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <gui/CpuConsumer.h>
22 
23 #include <gui/BufferItem.h>
24 #include <utils/Log.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 
findAcquiredBufferLocked(uintptr_t id) const47 size_t CpuConsumer::findAcquiredBufferLocked(uintptr_t id) const {
48     for (size_t i = 0; i < mMaxLockedBuffers; i++) {
49         const auto& ab = mAcquiredBuffers[i];
50         // note that this finds AcquiredBuffer::kUnusedId as well
51         if (ab.mLockedBufferId == id) {
52             return i;
53         }
54     }
55     return mMaxLockedBuffers; // an invalid index
56 }
57 
getLockedBufferId(const CpuConsumer::LockedBuffer & buffer)58 static uintptr_t getLockedBufferId(const CpuConsumer::LockedBuffer& buffer) {
59     return reinterpret_cast<uintptr_t>(buffer.data);
60 }
61 
isPossiblyYUV(PixelFormat format)62 static bool isPossiblyYUV(PixelFormat format) {
63     switch (static_cast<int>(format)) {
64         case HAL_PIXEL_FORMAT_RGBA_8888:
65         case HAL_PIXEL_FORMAT_RGBX_8888:
66         case HAL_PIXEL_FORMAT_RGBA_FP16:
67         case HAL_PIXEL_FORMAT_RGBA_1010102:
68         case HAL_PIXEL_FORMAT_RGB_888:
69         case HAL_PIXEL_FORMAT_RGB_565:
70         case HAL_PIXEL_FORMAT_BGRA_8888:
71         case HAL_PIXEL_FORMAT_Y8:
72         case HAL_PIXEL_FORMAT_Y16:
73         case HAL_PIXEL_FORMAT_RAW16:
74         case HAL_PIXEL_FORMAT_RAW12:
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 
lockBufferItem(const BufferItem & item,LockedBuffer * outBuffer) const91 status_t CpuConsumer::lockBufferItem(const BufferItem& item, LockedBuffer* outBuffer) const {
92     android_ycbcr ycbcr = android_ycbcr();
93 
94     PixelFormat format = item.mGraphicBuffer->getPixelFormat();
95     PixelFormat flexFormat = format;
96     if (isPossiblyYUV(format)) {
97         int fenceFd = item.mFence.get() ? item.mFence->dup() : -1;
98         status_t err = item.mGraphicBuffer->lockAsyncYCbCr(GraphicBuffer::USAGE_SW_READ_OFTEN,
99                                                            item.mCrop, &ycbcr, fenceFd);
100         if (err == OK) {
101             flexFormat = HAL_PIXEL_FORMAT_YCbCr_420_888;
102             if (format != HAL_PIXEL_FORMAT_YCbCr_420_888) {
103                 CC_LOGV("locking buffer of format %#x as flex YUV", format);
104             }
105         } else if (format == HAL_PIXEL_FORMAT_YCbCr_420_888) {
106             CC_LOGE("Unable to lock YCbCr buffer for CPU reading: %s (%d)", strerror(-err), err);
107             return err;
108         }
109     }
110 
111     if (ycbcr.y != nullptr) {
112         outBuffer->data = reinterpret_cast<uint8_t*>(ycbcr.y);
113         outBuffer->stride = static_cast<uint32_t>(ycbcr.ystride);
114         outBuffer->dataCb = reinterpret_cast<uint8_t*>(ycbcr.cb);
115         outBuffer->dataCr = reinterpret_cast<uint8_t*>(ycbcr.cr);
116         outBuffer->chromaStride = static_cast<uint32_t>(ycbcr.cstride);
117         outBuffer->chromaStep = static_cast<uint32_t>(ycbcr.chroma_step);
118     } else {
119         // not flexible YUV; try lockAsync
120         void* bufferPointer = nullptr;
121         int fenceFd = item.mFence.get() ? item.mFence->dup() : -1;
122         status_t err = item.mGraphicBuffer->lockAsync(GraphicBuffer::USAGE_SW_READ_OFTEN,
123                                                       item.mCrop, &bufferPointer, fenceFd);
124         if (err != OK) {
125             CC_LOGE("Unable to lock buffer for CPU reading: %s (%d)", strerror(-err), err);
126             return err;
127         }
128 
129         outBuffer->data = reinterpret_cast<uint8_t*>(bufferPointer);
130         outBuffer->stride = item.mGraphicBuffer->getStride();
131         outBuffer->dataCb = nullptr;
132         outBuffer->dataCr = nullptr;
133         outBuffer->chromaStride = 0;
134         outBuffer->chromaStep = 0;
135     }
136 
137     outBuffer->width = item.mGraphicBuffer->getWidth();
138     outBuffer->height = item.mGraphicBuffer->getHeight();
139     outBuffer->format = format;
140     outBuffer->flexFormat = flexFormat;
141 
142     outBuffer->crop = item.mCrop;
143     outBuffer->transform = item.mTransform;
144     outBuffer->scalingMode = item.mScalingMode;
145     outBuffer->timestamp = item.mTimestamp;
146     outBuffer->dataSpace = item.mDataSpace;
147     outBuffer->frameNumber = item.mFrameNumber;
148 
149     return OK;
150 }
151 
lockNextBuffer(LockedBuffer * nativeBuffer)152 status_t CpuConsumer::lockNextBuffer(LockedBuffer *nativeBuffer) {
153     status_t err;
154 
155     if (!nativeBuffer) return BAD_VALUE;
156 
157     Mutex::Autolock _l(mMutex);
158 
159     if (mCurrentLockedBuffers == mMaxLockedBuffers) {
160         CC_LOGW("Max buffers have been locked (%zd), cannot lock anymore.",
161                 mMaxLockedBuffers);
162         return NOT_ENOUGH_DATA;
163     }
164 
165     BufferItem b;
166     err = acquireBufferLocked(&b, 0);
167     if (err != OK) {
168         if (err == BufferQueue::NO_BUFFER_AVAILABLE) {
169             return BAD_VALUE;
170         } else {
171             CC_LOGE("Error acquiring buffer: %s (%d)", strerror(err), err);
172             return err;
173         }
174     }
175 
176     if (b.mGraphicBuffer == nullptr) {
177         b.mGraphicBuffer = mSlots[b.mSlot].mGraphicBuffer;
178     }
179 
180     err = lockBufferItem(b, nativeBuffer);
181     if (err != OK) {
182         return err;
183     }
184 
185     // find an unused AcquiredBuffer
186     size_t lockedIdx = findAcquiredBufferLocked(AcquiredBuffer::kUnusedId);
187     ALOG_ASSERT(lockedIdx < mMaxLockedBuffers);
188     AcquiredBuffer& ab = mAcquiredBuffers.editItemAt(lockedIdx);
189 
190     ab.mSlot = b.mSlot;
191     ab.mGraphicBuffer = b.mGraphicBuffer;
192     ab.mLockedBufferId = getLockedBufferId(*nativeBuffer);
193 
194     mCurrentLockedBuffers++;
195 
196     return OK;
197 }
198 
unlockBuffer(const LockedBuffer & nativeBuffer)199 status_t CpuConsumer::unlockBuffer(const LockedBuffer &nativeBuffer) {
200     Mutex::Autolock _l(mMutex);
201 
202     uintptr_t id = getLockedBufferId(nativeBuffer);
203     size_t lockedIdx =
204         (id != AcquiredBuffer::kUnusedId) ? findAcquiredBufferLocked(id) : mMaxLockedBuffers;
205     if (lockedIdx == mMaxLockedBuffers) {
206         CC_LOGE("%s: Can't find buffer to free", __FUNCTION__);
207         return BAD_VALUE;
208     }
209 
210     AcquiredBuffer& ab = mAcquiredBuffers.editItemAt(lockedIdx);
211 
212     int fenceFd = -1;
213     status_t err = ab.mGraphicBuffer->unlockAsync(&fenceFd);
214     if (err != OK) {
215         CC_LOGE("%s: Unable to unlock graphic buffer %zd", __FUNCTION__,
216                 lockedIdx);
217         return err;
218     }
219 
220     sp<Fence> fence(fenceFd >= 0 ? new Fence(fenceFd) : Fence::NO_FENCE);
221     addReleaseFenceLocked(ab.mSlot, ab.mGraphicBuffer, fence);
222     releaseBufferLocked(ab.mSlot, ab.mGraphicBuffer);
223 
224     ab.reset();
225 
226     mCurrentLockedBuffers--;
227 
228     return OK;
229 }
230 
231 } // namespace android
232