blob: 414814a6f4265fc5333f66c3e734938e6570d40b [file] [log] [blame]
Chia-I Wuf1405182017-11-27 11:29:21 -08001/*
2 * Copyright (C) 2010 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#undef LOG_TAG
18#define LOG_TAG "BufferLayerConsumer"
19#define ATRACE_TAG ATRACE_TAG_GRAPHICS
20//#define LOG_NDEBUG 0
21
22#include "BufferLayerConsumer.h"
Chia-I Wuc75c44d2017-11-27 14:32:57 -080023#include "Layer.h"
Ana Krulecfefcb582018-08-07 14:22:37 -070024#include "Scheduler/DispSync.h"
Chia-I Wuc75c44d2017-11-27 14:32:57 -080025
Chia-I Wuf1405182017-11-27 11:29:21 -080026#include <inttypes.h>
27
Chia-I Wuf1405182017-11-27 11:29:21 -080028#include <cutils/compiler.h>
29
30#include <hardware/hardware.h>
31
32#include <math/mat4.h>
33
34#include <gui/BufferItem.h>
35#include <gui/GLConsumer.h>
36#include <gui/ISurfaceComposer.h>
37#include <gui/SurfaceComposerClient.h>
Chia-I Wuf1405182017-11-27 11:29:21 -080038#include <private/gui/ComposerService.h>
Peiyong Lincbc184f2018-08-22 13:24:10 -070039#include <renderengine/Image.h>
40#include <renderengine/RenderEngine.h>
Chia-I Wuf1405182017-11-27 11:29:21 -080041#include <utils/Log.h>
42#include <utils/String8.h>
43#include <utils/Trace.h>
44
Chia-I Wuf1405182017-11-27 11:29:21 -080045namespace android {
46
47// Macros for including the BufferLayerConsumer name in log messages
48#define BLC_LOGV(x, ...) ALOGV("[%s] " x, mName.string(), ##__VA_ARGS__)
49#define BLC_LOGD(x, ...) ALOGD("[%s] " x, mName.string(), ##__VA_ARGS__)
50//#define BLC_LOGI(x, ...) ALOGI("[%s] " x, mName.string(), ##__VA_ARGS__)
51#define BLC_LOGW(x, ...) ALOGW("[%s] " x, mName.string(), ##__VA_ARGS__)
52#define BLC_LOGE(x, ...) ALOGE("[%s] " x, mName.string(), ##__VA_ARGS__)
53
Chia-I Wuf1405182017-11-27 11:29:21 -080054static const mat4 mtxIdentity;
55
Lloyd Pique144e1162017-12-20 16:44:52 -080056BufferLayerConsumer::BufferLayerConsumer(const sp<IGraphicBufferConsumer>& bq,
Peiyong Lin833074a2018-08-28 11:53:54 -070057 renderengine::RenderEngine& engine, uint32_t tex,
58 Layer* layer)
Chia-I Wubd854bf2017-11-27 13:41:26 -080059 : ConsumerBase(bq, false),
Chia-I Wuf1405182017-11-27 11:29:21 -080060 mCurrentCrop(Rect::EMPTY_RECT),
61 mCurrentTransform(0),
62 mCurrentScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE),
63 mCurrentFence(Fence::NO_FENCE),
64 mCurrentTimestamp(0),
Peiyong Lin34beb7a2018-03-28 11:57:12 -070065 mCurrentDataSpace(ui::Dataspace::UNKNOWN),
Chia-I Wuf1405182017-11-27 11:29:21 -080066 mCurrentFrameNumber(0),
Chia-I Wu67dcc692017-11-27 14:51:06 -080067 mCurrentTransformToDisplayInverse(false),
68 mCurrentSurfaceDamage(),
Chia-I Wu5c6e4632018-01-11 08:54:38 -080069 mCurrentApi(0),
Chia-I Wuf1405182017-11-27 11:29:21 -080070 mDefaultWidth(1),
71 mDefaultHeight(1),
72 mFilteringEnabled(true),
Chia-I Wu9f2db772017-11-30 21:06:50 -080073 mRE(engine),
Chia-I Wuf1405182017-11-27 11:29:21 -080074 mTexName(tex),
Chia-I Wuc75c44d2017-11-27 14:32:57 -080075 mLayer(layer),
Chia-I Wuc91077c2017-11-27 13:32:04 -080076 mCurrentTexture(BufferQueue::INVALID_BUFFER_SLOT) {
Chia-I Wuf1405182017-11-27 11:29:21 -080077 BLC_LOGV("BufferLayerConsumer");
78
79 memcpy(mCurrentTransformMatrix, mtxIdentity.asArray(), sizeof(mCurrentTransformMatrix));
80
81 mConsumer->setConsumerUsageBits(DEFAULT_USAGE_FLAGS);
82}
83
84status_t BufferLayerConsumer::setDefaultBufferSize(uint32_t w, uint32_t h) {
85 Mutex::Autolock lock(mMutex);
86 if (mAbandoned) {
87 BLC_LOGE("setDefaultBufferSize: BufferLayerConsumer is abandoned!");
88 return NO_INIT;
89 }
90 mDefaultWidth = w;
91 mDefaultHeight = h;
92 return mConsumer->setDefaultBufferSize(w, h);
93}
94
Chia-I Wufd257f82017-11-27 14:51:06 -080095void BufferLayerConsumer::setContentsChangedListener(const wp<ContentsChangedListener>& listener) {
96 setFrameAvailableListener(listener);
97 Mutex::Autolock lock(mMutex);
98 mContentsChangedListener = listener;
99}
100
Ana Krulec010d2192018-10-08 06:29:54 -0700101status_t BufferLayerConsumer::updateTexImage(BufferRejecter* rejecter, nsecs_t expectedPresentTime,
Chia-I Wuda5c7302017-11-27 14:51:06 -0800102 bool* autoRefresh, bool* queuedBuffer,
Alec Mouri56e538f2019-01-14 15:22:01 -0800103 uint64_t maxFrameNumber) {
Chia-I Wuf1405182017-11-27 11:29:21 -0800104 ATRACE_CALL();
105 BLC_LOGV("updateTexImage");
106 Mutex::Autolock lock(mMutex);
107
108 if (mAbandoned) {
109 BLC_LOGE("updateTexImage: BufferLayerConsumer is abandoned!");
110 return NO_INIT;
111 }
112
Chia-I Wuf1405182017-11-27 11:29:21 -0800113 BufferItem item;
114
115 // Acquire the next buffer.
116 // In asynchronous mode the list is guaranteed to be one buffer
117 // deep, while in synchronous mode we use the oldest buffer.
Ana Krulec010d2192018-10-08 06:29:54 -0700118 status_t err = acquireBufferLocked(&item, expectedPresentTime, maxFrameNumber);
Chia-I Wuf1405182017-11-27 11:29:21 -0800119 if (err != NO_ERROR) {
120 if (err == BufferQueue::NO_BUFFER_AVAILABLE) {
Chia-I Wuf1405182017-11-27 11:29:21 -0800121 err = NO_ERROR;
Chia-I Wuda5c7302017-11-27 14:51:06 -0800122 } else if (err == BufferQueue::PRESENT_LATER) {
123 // return the error, without logging
Chia-I Wuf1405182017-11-27 11:29:21 -0800124 } else {
125 BLC_LOGE("updateTexImage: acquire failed: %s (%d)", strerror(-err), err);
126 }
127 return err;
128 }
129
Chia-I Wuda5c7302017-11-27 14:51:06 -0800130 if (autoRefresh) {
131 *autoRefresh = item.mAutoRefresh;
132 }
133
134 if (queuedBuffer) {
135 *queuedBuffer = item.mQueuedBuffer;
136 }
137
138 // We call the rejecter here, in case the caller has a reason to
139 // not accept this buffer. This is used by SurfaceFlinger to
140 // reject buffers which have the wrong size
141 int slot = item.mSlot;
142 if (rejecter && rejecter->reject(mSlots[slot].mGraphicBuffer, item)) {
143 releaseBufferLocked(slot, mSlots[slot].mGraphicBuffer);
144 return BUFFER_REJECTED;
145 }
146
Chia-I Wuf1405182017-11-27 11:29:21 -0800147 // Release the previous buffer.
Alec Mouri56e538f2019-01-14 15:22:01 -0800148 err = updateAndReleaseLocked(item, &mPendingRelease);
Chia-I Wuf1405182017-11-27 11:29:21 -0800149 if (err != NO_ERROR) {
Chia-I Wuf1405182017-11-27 11:29:21 -0800150 return err;
151 }
152
Alec Mouri3221a892018-09-28 20:45:06 +0000153 if (!mRE.useNativeFenceSync()) {
Chia-I Wuda5c7302017-11-27 14:51:06 -0800154 // Bind the new buffer to the GL texture.
155 //
156 // Older devices require the "implicit" synchronization provided
157 // by glEGLImageTargetTexture2DOES, which this method calls. Newer
158 // devices will either call this in Layer::onDraw, or (if it's not
159 // a GL-composited layer) not at all.
160 err = bindTextureImageLocked();
161 }
162
163 return err;
164}
165
Chia-I Wu0cb75ac2017-11-27 15:56:04 -0800166status_t BufferLayerConsumer::bindTextureImage() {
167 Mutex::Autolock lock(mMutex);
168 return bindTextureImageLocked();
169}
170
Chia-I Wuda5c7302017-11-27 14:51:06 -0800171void BufferLayerConsumer::setReleaseFence(const sp<Fence>& fence) {
172 if (!fence->isValid()) {
173 return;
174 }
175
176 auto slot = mPendingRelease.isPending ? mPendingRelease.currentTexture : mCurrentTexture;
177 if (slot == BufferQueue::INVALID_BUFFER_SLOT) {
178 return;
179 }
180
Alec Mourib5c4f352019-02-19 19:46:38 -0800181 auto buffer = mPendingRelease.isPending ? mPendingRelease.graphicBuffer
182 : mCurrentTextureBuffer->graphicBuffer();
Chia-I Wuda5c7302017-11-27 14:51:06 -0800183 auto err = addReleaseFence(slot, buffer, fence);
184 if (err != OK) {
185 BLC_LOGE("setReleaseFence: failed to add the fence: %s (%d)", strerror(-err), err);
186 }
187}
188
189bool BufferLayerConsumer::releasePendingBuffer() {
190 if (!mPendingRelease.isPending) {
191 BLC_LOGV("Pending buffer already released");
192 return false;
193 }
194 BLC_LOGV("Releasing pending buffer");
195 Mutex::Autolock lock(mMutex);
196 status_t result =
197 releaseBufferLocked(mPendingRelease.currentTexture, mPendingRelease.graphicBuffer);
198 if (result < NO_ERROR) {
199 BLC_LOGE("releasePendingBuffer failed: %s (%d)", strerror(-result), result);
200 }
201 mPendingRelease = PendingRelease();
202 return true;
Chia-I Wuf1405182017-11-27 11:29:21 -0800203}
204
Chia-I Wu0cb75ac2017-11-27 15:56:04 -0800205sp<Fence> BufferLayerConsumer::getPrevFinalReleaseFence() const {
206 Mutex::Autolock lock(mMutex);
207 return ConsumerBase::mPrevFinalReleaseFence;
208}
209
Chia-I Wuf1405182017-11-27 11:29:21 -0800210status_t BufferLayerConsumer::acquireBufferLocked(BufferItem* item, nsecs_t presentWhen,
211 uint64_t maxFrameNumber) {
212 status_t err = ConsumerBase::acquireBufferLocked(item, presentWhen, maxFrameNumber);
213 if (err != NO_ERROR) {
214 return err;
215 }
216
217 // If item->mGraphicBuffer is not null, this buffer has not been acquired
Alec Mourib5c4f352019-02-19 19:46:38 -0800218 // before, so we need to clean up old references.
Peiyong Lin566a3b42018-01-09 18:22:43 -0800219 if (item->mGraphicBuffer != nullptr) {
Alec Mourid7b3a8b2019-03-21 11:44:18 -0700220 std::lock_guard<std::mutex> lock(mImagesMutex);
221 if (mImages[item->mSlot] == nullptr || mImages[item->mSlot]->graphicBuffer() == nullptr ||
222 mImages[item->mSlot]->graphicBuffer()->getId() != item->mGraphicBuffer->getId()) {
223 mImages[item->mSlot] = std::make_shared<Image>(item->mGraphicBuffer, mRE);
224 }
Chia-I Wuf1405182017-11-27 11:29:21 -0800225 }
226
227 return NO_ERROR;
228}
229
Chia-I Wuf1405182017-11-27 11:29:21 -0800230status_t BufferLayerConsumer::updateAndReleaseLocked(const BufferItem& item,
Alec Mouri56e538f2019-01-14 15:22:01 -0800231 PendingRelease* pendingRelease) {
Chia-I Wuf1405182017-11-27 11:29:21 -0800232 status_t err = NO_ERROR;
233
234 int slot = item.mSlot;
235
Chia-I Wuf1405182017-11-27 11:29:21 -0800236 BLC_LOGV("updateAndRelease: (slot=%d buf=%p) -> (slot=%d buf=%p)", mCurrentTexture,
Alec Mourib5c4f352019-02-19 19:46:38 -0800237 (mCurrentTextureBuffer != nullptr && mCurrentTextureBuffer->graphicBuffer() != nullptr)
238 ? mCurrentTextureBuffer->graphicBuffer()->handle
239 : 0,
240 slot, mSlots[slot].mGraphicBuffer->handle);
Chia-I Wuf1405182017-11-27 11:29:21 -0800241
242 // Hang onto the pointer so that it isn't freed in the call to
243 // releaseBufferLocked() if we're in shared buffer mode and both buffers are
244 // the same.
Alec Mourid7b3a8b2019-03-21 11:44:18 -0700245
246 std::shared_ptr<Image> nextTextureBuffer;
247 {
248 std::lock_guard<std::mutex> lock(mImagesMutex);
249 nextTextureBuffer = mImages[slot];
250 }
Chia-I Wuf1405182017-11-27 11:29:21 -0800251
252 // release old buffer
253 if (mCurrentTexture != BufferQueue::INVALID_BUFFER_SLOT) {
254 if (pendingRelease == nullptr) {
Alec Mourib5c4f352019-02-19 19:46:38 -0800255 status_t status =
256 releaseBufferLocked(mCurrentTexture, mCurrentTextureBuffer->graphicBuffer());
Chia-I Wuf1405182017-11-27 11:29:21 -0800257 if (status < NO_ERROR) {
258 BLC_LOGE("updateAndRelease: failed to release buffer: %s (%d)", strerror(-status),
259 status);
260 err = status;
261 // keep going, with error raised [?]
262 }
263 } else {
264 pendingRelease->currentTexture = mCurrentTexture;
Alec Mourib5c4f352019-02-19 19:46:38 -0800265 pendingRelease->graphicBuffer = mCurrentTextureBuffer->graphicBuffer();
Chia-I Wuf1405182017-11-27 11:29:21 -0800266 pendingRelease->isPending = true;
267 }
268 }
269
270 // Update the BufferLayerConsumer state.
271 mCurrentTexture = slot;
Alec Mouri39801c02018-10-10 10:44:47 -0700272 mCurrentTextureBuffer = nextTextureBuffer;
Chia-I Wuf1405182017-11-27 11:29:21 -0800273 mCurrentCrop = item.mCrop;
274 mCurrentTransform = item.mTransform;
275 mCurrentScalingMode = item.mScalingMode;
276 mCurrentTimestamp = item.mTimestamp;
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700277 mCurrentDataSpace = static_cast<ui::Dataspace>(item.mDataSpace);
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -0700278 mCurrentHdrMetadata = item.mHdrMetadata;
Chia-I Wuf1405182017-11-27 11:29:21 -0800279 mCurrentFence = item.mFence;
280 mCurrentFenceTime = item.mFenceTime;
281 mCurrentFrameNumber = item.mFrameNumber;
Chia-I Wu67dcc692017-11-27 14:51:06 -0800282 mCurrentTransformToDisplayInverse = item.mTransformToDisplayInverse;
283 mCurrentSurfaceDamage = item.mSurfaceDamage;
Chia-I Wu5c6e4632018-01-11 08:54:38 -0800284 mCurrentApi = item.mApi;
Chia-I Wuf1405182017-11-27 11:29:21 -0800285
286 computeCurrentTransformMatrixLocked();
287
288 return err;
289}
290
291status_t BufferLayerConsumer::bindTextureImageLocked() {
Dan Stoza84d619e2018-03-28 17:07:36 -0700292 ATRACE_CALL();
Alec Mouri39801c02018-10-10 10:44:47 -0700293
Alec Mourib5c4f352019-02-19 19:46:38 -0800294 if (mCurrentTextureBuffer != nullptr && mCurrentTextureBuffer->graphicBuffer() != nullptr) {
295 return mRE.bindExternalTextureBuffer(mTexName, mCurrentTextureBuffer->graphicBuffer(),
296 mCurrentFence);
297 }
298
299 return NO_INIT;
Chia-I Wuf1405182017-11-27 11:29:21 -0800300}
301
Chia-I Wuf1405182017-11-27 11:29:21 -0800302void BufferLayerConsumer::getTransformMatrix(float mtx[16]) {
303 Mutex::Autolock lock(mMutex);
304 memcpy(mtx, mCurrentTransformMatrix, sizeof(mCurrentTransformMatrix));
305}
306
307void BufferLayerConsumer::setFilteringEnabled(bool enabled) {
308 Mutex::Autolock lock(mMutex);
309 if (mAbandoned) {
310 BLC_LOGE("setFilteringEnabled: BufferLayerConsumer is abandoned!");
311 return;
312 }
313 bool needsRecompute = mFilteringEnabled != enabled;
314 mFilteringEnabled = enabled;
315
Alec Mouri39801c02018-10-10 10:44:47 -0700316 if (needsRecompute && mCurrentTextureBuffer == nullptr) {
317 BLC_LOGD("setFilteringEnabled called with mCurrentTextureBuffer == nullptr");
Chia-I Wuf1405182017-11-27 11:29:21 -0800318 }
319
Alec Mouri39801c02018-10-10 10:44:47 -0700320 if (needsRecompute && mCurrentTextureBuffer != nullptr) {
Chia-I Wuf1405182017-11-27 11:29:21 -0800321 computeCurrentTransformMatrixLocked();
322 }
323}
324
325void BufferLayerConsumer::computeCurrentTransformMatrixLocked() {
326 BLC_LOGV("computeCurrentTransformMatrixLocked");
Alec Mourib5c4f352019-02-19 19:46:38 -0800327 if (mCurrentTextureBuffer == nullptr || mCurrentTextureBuffer->graphicBuffer() == nullptr) {
Chia-I Wuf1405182017-11-27 11:29:21 -0800328 BLC_LOGD("computeCurrentTransformMatrixLocked: "
Alec Mouri39801c02018-10-10 10:44:47 -0700329 "mCurrentTextureBuffer is nullptr");
Chia-I Wuf1405182017-11-27 11:29:21 -0800330 }
Alec Mourib5c4f352019-02-19 19:46:38 -0800331 GLConsumer::computeTransformMatrix(mCurrentTransformMatrix,
332 mCurrentTextureBuffer == nullptr
333 ? nullptr
334 : mCurrentTextureBuffer->graphicBuffer(),
Alec Mouri2ee0dda2019-01-30 16:44:43 -0800335 getCurrentCropLocked(), mCurrentTransform,
336 mFilteringEnabled);
Chia-I Wuf1405182017-11-27 11:29:21 -0800337}
338
Chia-I Wuf1405182017-11-27 11:29:21 -0800339nsecs_t BufferLayerConsumer::getTimestamp() {
340 BLC_LOGV("getTimestamp");
341 Mutex::Autolock lock(mMutex);
342 return mCurrentTimestamp;
343}
344
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700345ui::Dataspace BufferLayerConsumer::getCurrentDataSpace() {
Chia-I Wuf1405182017-11-27 11:29:21 -0800346 BLC_LOGV("getCurrentDataSpace");
347 Mutex::Autolock lock(mMutex);
348 return mCurrentDataSpace;
349}
350
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -0700351const HdrMetadata& BufferLayerConsumer::getCurrentHdrMetadata() const {
352 BLC_LOGV("getCurrentHdrMetadata");
353 Mutex::Autolock lock(mMutex);
354 return mCurrentHdrMetadata;
355}
356
Chia-I Wuf1405182017-11-27 11:29:21 -0800357uint64_t BufferLayerConsumer::getFrameNumber() {
358 BLC_LOGV("getFrameNumber");
359 Mutex::Autolock lock(mMutex);
360 return mCurrentFrameNumber;
361}
362
Chia-I Wu67dcc692017-11-27 14:51:06 -0800363bool BufferLayerConsumer::getTransformToDisplayInverse() const {
364 Mutex::Autolock lock(mMutex);
365 return mCurrentTransformToDisplayInverse;
366}
367
368const Region& BufferLayerConsumer::getSurfaceDamage() const {
369 return mCurrentSurfaceDamage;
370}
371
Steven Thomas44685cb2019-07-23 16:19:31 -0700372void BufferLayerConsumer::mergeSurfaceDamage(const Region& damage) {
373 if (damage.bounds() == Rect::INVALID_RECT ||
374 mCurrentSurfaceDamage.bounds() == Rect::INVALID_RECT) {
375 mCurrentSurfaceDamage = Region::INVALID_REGION;
376 } else {
377 mCurrentSurfaceDamage |= damage;
378 }
379}
380
Chia-I Wu5c6e4632018-01-11 08:54:38 -0800381int BufferLayerConsumer::getCurrentApi() const {
382 Mutex::Autolock lock(mMutex);
383 return mCurrentApi;
384}
385
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000386sp<GraphicBuffer> BufferLayerConsumer::getCurrentBuffer(int* outSlot, sp<Fence>* outFence) const {
Chia-I Wuf1405182017-11-27 11:29:21 -0800387 Mutex::Autolock lock(mMutex);
388
389 if (outSlot != nullptr) {
390 *outSlot = mCurrentTexture;
391 }
392
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000393 if (outFence != nullptr) {
394 *outFence = mCurrentFence;
395 }
396
Alec Mourib5c4f352019-02-19 19:46:38 -0800397 return mCurrentTextureBuffer == nullptr ? nullptr : mCurrentTextureBuffer->graphicBuffer();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000398}
399
Chia-I Wuf1405182017-11-27 11:29:21 -0800400Rect BufferLayerConsumer::getCurrentCrop() const {
401 Mutex::Autolock lock(mMutex);
Alec Mouri2ee0dda2019-01-30 16:44:43 -0800402 return getCurrentCropLocked();
403}
404
405Rect BufferLayerConsumer::getCurrentCropLocked() const {
Chia-I Wuf1405182017-11-27 11:29:21 -0800406 return (mCurrentScalingMode == NATIVE_WINDOW_SCALING_MODE_SCALE_CROP)
Chia-I Wue1e11872017-12-01 09:21:59 -0800407 ? GLConsumer::scaleDownCrop(mCurrentCrop, mDefaultWidth, mDefaultHeight)
Chia-I Wuf1405182017-11-27 11:29:21 -0800408 : mCurrentCrop;
409}
410
411uint32_t BufferLayerConsumer::getCurrentTransform() const {
412 Mutex::Autolock lock(mMutex);
413 return mCurrentTransform;
414}
415
416uint32_t BufferLayerConsumer::getCurrentScalingMode() const {
417 Mutex::Autolock lock(mMutex);
418 return mCurrentScalingMode;
419}
420
421sp<Fence> BufferLayerConsumer::getCurrentFence() const {
422 Mutex::Autolock lock(mMutex);
423 return mCurrentFence;
424}
425
426std::shared_ptr<FenceTime> BufferLayerConsumer::getCurrentFenceTime() const {
427 Mutex::Autolock lock(mMutex);
428 return mCurrentFenceTime;
429}
430
Chia-I Wu3498e3c2017-12-01 10:19:38 -0800431status_t BufferLayerConsumer::doFenceWaitLocked() const {
Chia-I Wuf1405182017-11-27 11:29:21 -0800432 if (mCurrentFence->isValid()) {
Lloyd Pique63f9dbf2018-06-21 13:13:07 -0700433 if (mRE.useWaitSync()) {
Chia-I Wu3498e3c2017-12-01 10:19:38 -0800434 base::unique_fd fenceFd(mCurrentFence->dup());
Chia-I Wuf1405182017-11-27 11:29:21 -0800435 if (fenceFd == -1) {
Chia-I Wu3498e3c2017-12-01 10:19:38 -0800436 BLC_LOGE("doFenceWait: error dup'ing fence fd: %d", errno);
Chia-I Wuf1405182017-11-27 11:29:21 -0800437 return -errno;
438 }
Chia-I Wu3498e3c2017-12-01 10:19:38 -0800439 if (!mRE.waitFence(std::move(fenceFd))) {
440 BLC_LOGE("doFenceWait: failed to wait on fence fd");
Chia-I Wuf1405182017-11-27 11:29:21 -0800441 return UNKNOWN_ERROR;
442 }
443 } else {
Chia-I Wu3498e3c2017-12-01 10:19:38 -0800444 status_t err = mCurrentFence->waitForever("BufferLayerConsumer::doFenceWaitLocked");
Chia-I Wuf1405182017-11-27 11:29:21 -0800445 if (err != NO_ERROR) {
Chia-I Wu3498e3c2017-12-01 10:19:38 -0800446 BLC_LOGE("doFenceWait: error waiting for fence: %d", err);
Chia-I Wuf1405182017-11-27 11:29:21 -0800447 return err;
448 }
449 }
450 }
451
452 return NO_ERROR;
453}
454
455void BufferLayerConsumer::freeBufferLocked(int slotIndex) {
456 BLC_LOGV("freeBufferLocked: slotIndex=%d", slotIndex);
Alec Mourid7b3a8b2019-03-21 11:44:18 -0700457 std::lock_guard<std::mutex> lock(mImagesMutex);
Chia-I Wuf1405182017-11-27 11:29:21 -0800458 if (slotIndex == mCurrentTexture) {
459 mCurrentTexture = BufferQueue::INVALID_BUFFER_SLOT;
460 }
Alec Mourib5c4f352019-02-19 19:46:38 -0800461 mImages[slotIndex] = nullptr;
Chia-I Wuf1405182017-11-27 11:29:21 -0800462 ConsumerBase::freeBufferLocked(slotIndex);
463}
464
Chia-I Wuc75c44d2017-11-27 14:32:57 -0800465void BufferLayerConsumer::onDisconnect() {
466 sp<Layer> l = mLayer.promote();
467 if (l.get()) {
468 l->onDisconnect();
469 }
470}
471
Chia-I Wufd257f82017-11-27 14:51:06 -0800472void BufferLayerConsumer::onSidebandStreamChanged() {
473 FrameAvailableListener* unsafeFrameAvailableListener = nullptr;
474 {
475 Mutex::Autolock lock(mFrameAvailableMutex);
476 unsafeFrameAvailableListener = mFrameAvailableListener.unsafe_get();
477 }
478 sp<ContentsChangedListener> listener;
479 { // scope for the lock
480 Mutex::Autolock lock(mMutex);
481 ALOG_ASSERT(unsafeFrameAvailableListener == mContentsChangedListener.unsafe_get());
482 listener = mContentsChangedListener.promote();
483 }
484
Peiyong Lin566a3b42018-01-09 18:22:43 -0800485 if (listener != nullptr) {
Chia-I Wufd257f82017-11-27 14:51:06 -0800486 listener->onSidebandStreamChanged();
487 }
488}
489
Alec Mouri485e4c32019-04-30 18:24:05 -0700490void BufferLayerConsumer::onBufferAvailable(const BufferItem& item) {
491 if (item.mGraphicBuffer != nullptr && item.mSlot != BufferQueue::INVALID_BUFFER_SLOT) {
492 std::lock_guard<std::mutex> lock(mImagesMutex);
493 const std::shared_ptr<Image>& oldImage = mImages[item.mSlot];
494 if (oldImage == nullptr || oldImage->graphicBuffer() == nullptr ||
495 oldImage->graphicBuffer()->getId() != item.mGraphicBuffer->getId()) {
496 mImages[item.mSlot] = std::make_shared<Image>(item.mGraphicBuffer, mRE);
Alec Mourid7b3a8b2019-03-21 11:44:18 -0700497 }
Alec Mourid7b3a8b2019-03-21 11:44:18 -0700498 }
499}
500
Chia-I Wuc75c44d2017-11-27 14:32:57 -0800501void BufferLayerConsumer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
502 FrameEventHistoryDelta* outDelta) {
503 sp<Layer> l = mLayer.promote();
504 if (l.get()) {
505 l->addAndGetFrameTimestamps(newTimestamps, outDelta);
506 }
507}
508
Chia-I Wuf1405182017-11-27 11:29:21 -0800509void BufferLayerConsumer::abandonLocked() {
510 BLC_LOGV("abandonLocked");
Alec Mourib5c4f352019-02-19 19:46:38 -0800511 mCurrentTextureBuffer = nullptr;
512 for (int i = 0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) {
Alec Mourid7b3a8b2019-03-21 11:44:18 -0700513 std::lock_guard<std::mutex> lock(mImagesMutex);
Alec Mourib5c4f352019-02-19 19:46:38 -0800514 mImages[i] = nullptr;
515 }
Chia-I Wuf1405182017-11-27 11:29:21 -0800516 ConsumerBase::abandonLocked();
517}
518
519status_t BufferLayerConsumer::setConsumerUsageBits(uint64_t usage) {
520 return ConsumerBase::setConsumerUsageBits(usage | DEFAULT_USAGE_FLAGS);
521}
522
523void BufferLayerConsumer::dumpLocked(String8& result, const char* prefix) const {
524 result.appendFormat("%smTexName=%d mCurrentTexture=%d\n"
525 "%smCurrentCrop=[%d,%d,%d,%d] mCurrentTransform=%#x\n",
526 prefix, mTexName, mCurrentTexture, prefix, mCurrentCrop.left,
527 mCurrentCrop.top, mCurrentCrop.right, mCurrentCrop.bottom,
528 mCurrentTransform);
529
530 ConsumerBase::dumpLocked(result, prefix);
531}
532
Alec Mouri16a99402019-07-29 16:37:30 -0700533BufferLayerConsumer::Image::Image(const sp<GraphicBuffer>& graphicBuffer,
534 renderengine::RenderEngine& engine)
535 : mGraphicBuffer(graphicBuffer), mRE(engine) {
536 mRE.cacheExternalTextureBuffer(mGraphicBuffer);
537}
538
Alec Mourib5c4f352019-02-19 19:46:38 -0800539BufferLayerConsumer::Image::~Image() {
540 if (mGraphicBuffer != nullptr) {
Alec Mourif1907332019-03-07 13:05:26 -0800541 ALOGV("Destroying buffer: %" PRId64, mGraphicBuffer->getId());
Alec Mourib5c4f352019-02-19 19:46:38 -0800542 mRE.unbindExternalTextureBuffer(mGraphicBuffer->getId());
543 }
544}
Chia-I Wuf1405182017-11-27 11:29:21 -0800545}; // namespace android