blob: 9ae45fc4cb9f5a5de7777ddb69af1c3f11ff3ca0 [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
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -080017// TODO(b/129481165): remove the #pragma below and fix conversion issues
18#pragma clang diagnostic push
19#pragma clang diagnostic ignored "-Wconversion"
20
Chia-I Wuf1405182017-11-27 11:29:21 -080021#undef LOG_TAG
22#define LOG_TAG "BufferLayerConsumer"
23#define ATRACE_TAG ATRACE_TAG_GRAPHICS
24//#define LOG_NDEBUG 0
25
26#include "BufferLayerConsumer.h"
Chia-I Wuc75c44d2017-11-27 14:32:57 -080027#include "Layer.h"
Ady Abraham8cb21882020-08-26 18:22:05 -070028#include "Scheduler/VsyncController.h"
Chia-I Wuc75c44d2017-11-27 14:32:57 -080029
Chia-I Wuf1405182017-11-27 11:29:21 -080030#include <inttypes.h>
31
Chia-I Wuf1405182017-11-27 11:29:21 -080032#include <cutils/compiler.h>
33
34#include <hardware/hardware.h>
35
36#include <math/mat4.h>
37
38#include <gui/BufferItem.h>
39#include <gui/GLConsumer.h>
40#include <gui/ISurfaceComposer.h>
41#include <gui/SurfaceComposerClient.h>
Chia-I Wuf1405182017-11-27 11:29:21 -080042#include <private/gui/ComposerService.h>
Peiyong Lincbc184f2018-08-22 13:24:10 -070043#include <renderengine/RenderEngine.h>
Vishnu Nairdbbe3852022-01-12 20:22:11 -080044#include <renderengine/impl/ExternalTexture.h>
Chia-I Wuf1405182017-11-27 11:29:21 -080045#include <utils/Log.h>
46#include <utils/String8.h>
47#include <utils/Trace.h>
48
Chia-I Wuf1405182017-11-27 11:29:21 -080049namespace android {
50
51// Macros for including the BufferLayerConsumer name in log messages
52#define BLC_LOGV(x, ...) ALOGV("[%s] " x, mName.string(), ##__VA_ARGS__)
53#define BLC_LOGD(x, ...) ALOGD("[%s] " x, mName.string(), ##__VA_ARGS__)
54//#define BLC_LOGI(x, ...) ALOGI("[%s] " x, mName.string(), ##__VA_ARGS__)
55#define BLC_LOGW(x, ...) ALOGW("[%s] " x, mName.string(), ##__VA_ARGS__)
56#define BLC_LOGE(x, ...) ALOGE("[%s] " x, mName.string(), ##__VA_ARGS__)
57
Chia-I Wuf1405182017-11-27 11:29:21 -080058static const mat4 mtxIdentity;
59
Lloyd Pique144e1162017-12-20 16:44:52 -080060BufferLayerConsumer::BufferLayerConsumer(const sp<IGraphicBufferConsumer>& bq,
Peiyong Lin833074a2018-08-28 11:53:54 -070061 renderengine::RenderEngine& engine, uint32_t tex,
62 Layer* layer)
Chia-I Wubd854bf2017-11-27 13:41:26 -080063 : ConsumerBase(bq, false),
Chia-I Wuf1405182017-11-27 11:29:21 -080064 mCurrentCrop(Rect::EMPTY_RECT),
65 mCurrentTransform(0),
66 mCurrentScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE),
67 mCurrentFence(Fence::NO_FENCE),
68 mCurrentTimestamp(0),
Peiyong Lin34beb7a2018-03-28 11:57:12 -070069 mCurrentDataSpace(ui::Dataspace::UNKNOWN),
Chia-I Wuf1405182017-11-27 11:29:21 -080070 mCurrentFrameNumber(0),
Chia-I Wu67dcc692017-11-27 14:51:06 -080071 mCurrentTransformToDisplayInverse(false),
72 mCurrentSurfaceDamage(),
Chia-I Wu5c6e4632018-01-11 08:54:38 -080073 mCurrentApi(0),
Chia-I Wuf1405182017-11-27 11:29:21 -080074 mDefaultWidth(1),
75 mDefaultHeight(1),
76 mFilteringEnabled(true),
Chia-I Wu9f2db772017-11-30 21:06:50 -080077 mRE(engine),
Chia-I Wuf1405182017-11-27 11:29:21 -080078 mTexName(tex),
Chia-I Wuc75c44d2017-11-27 14:32:57 -080079 mLayer(layer),
Chia-I Wuc91077c2017-11-27 13:32:04 -080080 mCurrentTexture(BufferQueue::INVALID_BUFFER_SLOT) {
Chia-I Wuf1405182017-11-27 11:29:21 -080081 BLC_LOGV("BufferLayerConsumer");
82
83 memcpy(mCurrentTransformMatrix, mtxIdentity.asArray(), sizeof(mCurrentTransformMatrix));
84
85 mConsumer->setConsumerUsageBits(DEFAULT_USAGE_FLAGS);
86}
87
88status_t BufferLayerConsumer::setDefaultBufferSize(uint32_t w, uint32_t h) {
89 Mutex::Autolock lock(mMutex);
90 if (mAbandoned) {
91 BLC_LOGE("setDefaultBufferSize: BufferLayerConsumer is abandoned!");
92 return NO_INIT;
93 }
94 mDefaultWidth = w;
95 mDefaultHeight = h;
96 return mConsumer->setDefaultBufferSize(w, h);
97}
98
Chia-I Wufd257f82017-11-27 14:51:06 -080099void BufferLayerConsumer::setContentsChangedListener(const wp<ContentsChangedListener>& listener) {
100 setFrameAvailableListener(listener);
101 Mutex::Autolock lock(mMutex);
102 mContentsChangedListener = listener;
103}
104
Ana Krulec010d2192018-10-08 06:29:54 -0700105status_t BufferLayerConsumer::updateTexImage(BufferRejecter* rejecter, nsecs_t expectedPresentTime,
Chia-I Wuda5c7302017-11-27 14:51:06 -0800106 bool* autoRefresh, bool* queuedBuffer,
Alec Mouri56e538f2019-01-14 15:22:01 -0800107 uint64_t maxFrameNumber) {
Chia-I Wuf1405182017-11-27 11:29:21 -0800108 ATRACE_CALL();
109 BLC_LOGV("updateTexImage");
110 Mutex::Autolock lock(mMutex);
111
112 if (mAbandoned) {
113 BLC_LOGE("updateTexImage: BufferLayerConsumer is abandoned!");
114 return NO_INIT;
115 }
116
Chia-I Wuf1405182017-11-27 11:29:21 -0800117 BufferItem item;
118
119 // Acquire the next buffer.
120 // In asynchronous mode the list is guaranteed to be one buffer
121 // deep, while in synchronous mode we use the oldest buffer.
Ana Krulec010d2192018-10-08 06:29:54 -0700122 status_t err = acquireBufferLocked(&item, expectedPresentTime, maxFrameNumber);
Chia-I Wuf1405182017-11-27 11:29:21 -0800123 if (err != NO_ERROR) {
124 if (err == BufferQueue::NO_BUFFER_AVAILABLE) {
Chia-I Wuf1405182017-11-27 11:29:21 -0800125 err = NO_ERROR;
Chia-I Wuda5c7302017-11-27 14:51:06 -0800126 } else if (err == BufferQueue::PRESENT_LATER) {
127 // return the error, without logging
Chia-I Wuf1405182017-11-27 11:29:21 -0800128 } else {
129 BLC_LOGE("updateTexImage: acquire failed: %s (%d)", strerror(-err), err);
130 }
131 return err;
132 }
133
Chia-I Wuda5c7302017-11-27 14:51:06 -0800134 if (autoRefresh) {
135 *autoRefresh = item.mAutoRefresh;
136 }
137
138 if (queuedBuffer) {
139 *queuedBuffer = item.mQueuedBuffer;
140 }
141
142 // We call the rejecter here, in case the caller has a reason to
143 // not accept this buffer. This is used by SurfaceFlinger to
144 // reject buffers which have the wrong size
145 int slot = item.mSlot;
146 if (rejecter && rejecter->reject(mSlots[slot].mGraphicBuffer, item)) {
147 releaseBufferLocked(slot, mSlots[slot].mGraphicBuffer);
148 return BUFFER_REJECTED;
149 }
150
Chia-I Wuf1405182017-11-27 11:29:21 -0800151 // Release the previous buffer.
Alec Mouri56e538f2019-01-14 15:22:01 -0800152 err = updateAndReleaseLocked(item, &mPendingRelease);
Chia-I Wuf1405182017-11-27 11:29:21 -0800153 if (err != NO_ERROR) {
Chia-I Wuf1405182017-11-27 11:29:21 -0800154 return err;
155 }
Chia-I Wuda5c7302017-11-27 14:51:06 -0800156 return err;
157}
158
159void BufferLayerConsumer::setReleaseFence(const sp<Fence>& fence) {
160 if (!fence->isValid()) {
161 return;
162 }
163
164 auto slot = mPendingRelease.isPending ? mPendingRelease.currentTexture : mCurrentTexture;
165 if (slot == BufferQueue::INVALID_BUFFER_SLOT) {
166 return;
167 }
168
Alec Mourib5c4f352019-02-19 19:46:38 -0800169 auto buffer = mPendingRelease.isPending ? mPendingRelease.graphicBuffer
Alec Mouria90a5702021-04-16 16:36:21 +0000170 : mCurrentTextureBuffer->getBuffer();
Chia-I Wuda5c7302017-11-27 14:51:06 -0800171 auto err = addReleaseFence(slot, buffer, fence);
172 if (err != OK) {
173 BLC_LOGE("setReleaseFence: failed to add the fence: %s (%d)", strerror(-err), err);
174 }
175}
176
177bool BufferLayerConsumer::releasePendingBuffer() {
178 if (!mPendingRelease.isPending) {
179 BLC_LOGV("Pending buffer already released");
180 return false;
181 }
182 BLC_LOGV("Releasing pending buffer");
183 Mutex::Autolock lock(mMutex);
184 status_t result =
185 releaseBufferLocked(mPendingRelease.currentTexture, mPendingRelease.graphicBuffer);
186 if (result < NO_ERROR) {
187 BLC_LOGE("releasePendingBuffer failed: %s (%d)", strerror(-result), result);
188 }
189 mPendingRelease = PendingRelease();
190 return true;
Chia-I Wuf1405182017-11-27 11:29:21 -0800191}
192
Chia-I Wu0cb75ac2017-11-27 15:56:04 -0800193sp<Fence> BufferLayerConsumer::getPrevFinalReleaseFence() const {
194 Mutex::Autolock lock(mMutex);
195 return ConsumerBase::mPrevFinalReleaseFence;
196}
197
Chia-I Wuf1405182017-11-27 11:29:21 -0800198status_t BufferLayerConsumer::acquireBufferLocked(BufferItem* item, nsecs_t presentWhen,
199 uint64_t maxFrameNumber) {
200 status_t err = ConsumerBase::acquireBufferLocked(item, presentWhen, maxFrameNumber);
201 if (err != NO_ERROR) {
202 return err;
203 }
204
205 // If item->mGraphicBuffer is not null, this buffer has not been acquired
Alec Mourib5c4f352019-02-19 19:46:38 -0800206 // before, so we need to clean up old references.
Peiyong Lin566a3b42018-01-09 18:22:43 -0800207 if (item->mGraphicBuffer != nullptr) {
Alec Mourid7b3a8b2019-03-21 11:44:18 -0700208 std::lock_guard<std::mutex> lock(mImagesMutex);
Alec Mouria90a5702021-04-16 16:36:21 +0000209 if (mImages[item->mSlot] == nullptr || mImages[item->mSlot]->getBuffer() == nullptr ||
210 mImages[item->mSlot]->getBuffer()->getId() != item->mGraphicBuffer->getId()) {
211 mImages[item->mSlot] = std::make_shared<
Vishnu Nairdbbe3852022-01-12 20:22:11 -0800212 renderengine::impl::ExternalTexture>(item->mGraphicBuffer, mRE,
213 renderengine::impl::ExternalTexture::
214 Usage::READABLE);
Alec Mourid7b3a8b2019-03-21 11:44:18 -0700215 }
Chia-I Wuf1405182017-11-27 11:29:21 -0800216 }
217
218 return NO_ERROR;
219}
220
Chia-I Wuf1405182017-11-27 11:29:21 -0800221status_t BufferLayerConsumer::updateAndReleaseLocked(const BufferItem& item,
Alec Mouri56e538f2019-01-14 15:22:01 -0800222 PendingRelease* pendingRelease) {
Chia-I Wuf1405182017-11-27 11:29:21 -0800223 status_t err = NO_ERROR;
224
225 int slot = item.mSlot;
226
Chia-I Wuf1405182017-11-27 11:29:21 -0800227 BLC_LOGV("updateAndRelease: (slot=%d buf=%p) -> (slot=%d buf=%p)", mCurrentTexture,
Alec Mouria90a5702021-04-16 16:36:21 +0000228 (mCurrentTextureBuffer != nullptr && mCurrentTextureBuffer->getBuffer() != nullptr)
229 ? mCurrentTextureBuffer->getBuffer()->handle
Alec Mourib5c4f352019-02-19 19:46:38 -0800230 : 0,
231 slot, mSlots[slot].mGraphicBuffer->handle);
Chia-I Wuf1405182017-11-27 11:29:21 -0800232
233 // Hang onto the pointer so that it isn't freed in the call to
234 // releaseBufferLocked() if we're in shared buffer mode and both buffers are
235 // the same.
Alec Mourid7b3a8b2019-03-21 11:44:18 -0700236
Alec Mouria90a5702021-04-16 16:36:21 +0000237 std::shared_ptr<renderengine::ExternalTexture> nextTextureBuffer;
Alec Mourid7b3a8b2019-03-21 11:44:18 -0700238 {
239 std::lock_guard<std::mutex> lock(mImagesMutex);
240 nextTextureBuffer = mImages[slot];
241 }
Chia-I Wuf1405182017-11-27 11:29:21 -0800242
243 // release old buffer
244 if (mCurrentTexture != BufferQueue::INVALID_BUFFER_SLOT) {
245 if (pendingRelease == nullptr) {
Alec Mourib5c4f352019-02-19 19:46:38 -0800246 status_t status =
Alec Mouria90a5702021-04-16 16:36:21 +0000247 releaseBufferLocked(mCurrentTexture, mCurrentTextureBuffer->getBuffer());
Chia-I Wuf1405182017-11-27 11:29:21 -0800248 if (status < NO_ERROR) {
249 BLC_LOGE("updateAndRelease: failed to release buffer: %s (%d)", strerror(-status),
250 status);
251 err = status;
252 // keep going, with error raised [?]
253 }
254 } else {
255 pendingRelease->currentTexture = mCurrentTexture;
Alec Mouria90a5702021-04-16 16:36:21 +0000256 pendingRelease->graphicBuffer = mCurrentTextureBuffer->getBuffer();
Chia-I Wuf1405182017-11-27 11:29:21 -0800257 pendingRelease->isPending = true;
258 }
259 }
260
261 // Update the BufferLayerConsumer state.
262 mCurrentTexture = slot;
Alec Mouri39801c02018-10-10 10:44:47 -0700263 mCurrentTextureBuffer = nextTextureBuffer;
Chia-I Wuf1405182017-11-27 11:29:21 -0800264 mCurrentCrop = item.mCrop;
265 mCurrentTransform = item.mTransform;
266 mCurrentScalingMode = item.mScalingMode;
267 mCurrentTimestamp = item.mTimestamp;
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700268 mCurrentDataSpace = static_cast<ui::Dataspace>(item.mDataSpace);
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -0700269 mCurrentHdrMetadata = item.mHdrMetadata;
Chia-I Wuf1405182017-11-27 11:29:21 -0800270 mCurrentFence = item.mFence;
271 mCurrentFenceTime = item.mFenceTime;
272 mCurrentFrameNumber = item.mFrameNumber;
Chia-I Wu67dcc692017-11-27 14:51:06 -0800273 mCurrentTransformToDisplayInverse = item.mTransformToDisplayInverse;
274 mCurrentSurfaceDamage = item.mSurfaceDamage;
Chia-I Wu5c6e4632018-01-11 08:54:38 -0800275 mCurrentApi = item.mApi;
Chia-I Wuf1405182017-11-27 11:29:21 -0800276
277 computeCurrentTransformMatrixLocked();
278
279 return err;
280}
281
Chia-I Wuf1405182017-11-27 11:29:21 -0800282void BufferLayerConsumer::getTransformMatrix(float mtx[16]) {
283 Mutex::Autolock lock(mMutex);
284 memcpy(mtx, mCurrentTransformMatrix, sizeof(mCurrentTransformMatrix));
285}
286
287void BufferLayerConsumer::setFilteringEnabled(bool enabled) {
288 Mutex::Autolock lock(mMutex);
289 if (mAbandoned) {
290 BLC_LOGE("setFilteringEnabled: BufferLayerConsumer is abandoned!");
291 return;
292 }
293 bool needsRecompute = mFilteringEnabled != enabled;
294 mFilteringEnabled = enabled;
295
Alec Mouri39801c02018-10-10 10:44:47 -0700296 if (needsRecompute && mCurrentTextureBuffer == nullptr) {
297 BLC_LOGD("setFilteringEnabled called with mCurrentTextureBuffer == nullptr");
Chia-I Wuf1405182017-11-27 11:29:21 -0800298 }
299
Alec Mouri39801c02018-10-10 10:44:47 -0700300 if (needsRecompute && mCurrentTextureBuffer != nullptr) {
Chia-I Wuf1405182017-11-27 11:29:21 -0800301 computeCurrentTransformMatrixLocked();
302 }
303}
304
305void BufferLayerConsumer::computeCurrentTransformMatrixLocked() {
306 BLC_LOGV("computeCurrentTransformMatrixLocked");
Alec Mouria90a5702021-04-16 16:36:21 +0000307 if (mCurrentTextureBuffer == nullptr || mCurrentTextureBuffer->getBuffer() == nullptr) {
Chia-I Wuf1405182017-11-27 11:29:21 -0800308 BLC_LOGD("computeCurrentTransformMatrixLocked: "
Alec Mouri39801c02018-10-10 10:44:47 -0700309 "mCurrentTextureBuffer is nullptr");
Chia-I Wuf1405182017-11-27 11:29:21 -0800310 }
Alec Mourib5c4f352019-02-19 19:46:38 -0800311 GLConsumer::computeTransformMatrix(mCurrentTransformMatrix,
312 mCurrentTextureBuffer == nullptr
313 ? nullptr
Alec Mouria90a5702021-04-16 16:36:21 +0000314 : mCurrentTextureBuffer->getBuffer(),
Alec Mouri2ee0dda2019-01-30 16:44:43 -0800315 getCurrentCropLocked(), mCurrentTransform,
316 mFilteringEnabled);
Chia-I Wuf1405182017-11-27 11:29:21 -0800317}
318
Chia-I Wuf1405182017-11-27 11:29:21 -0800319nsecs_t BufferLayerConsumer::getTimestamp() {
320 BLC_LOGV("getTimestamp");
321 Mutex::Autolock lock(mMutex);
322 return mCurrentTimestamp;
323}
324
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700325ui::Dataspace BufferLayerConsumer::getCurrentDataSpace() {
Chia-I Wuf1405182017-11-27 11:29:21 -0800326 BLC_LOGV("getCurrentDataSpace");
327 Mutex::Autolock lock(mMutex);
328 return mCurrentDataSpace;
329}
330
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -0700331const HdrMetadata& BufferLayerConsumer::getCurrentHdrMetadata() const {
332 BLC_LOGV("getCurrentHdrMetadata");
333 Mutex::Autolock lock(mMutex);
334 return mCurrentHdrMetadata;
335}
336
Chia-I Wuf1405182017-11-27 11:29:21 -0800337uint64_t BufferLayerConsumer::getFrameNumber() {
338 BLC_LOGV("getFrameNumber");
339 Mutex::Autolock lock(mMutex);
340 return mCurrentFrameNumber;
341}
342
Chia-I Wu67dcc692017-11-27 14:51:06 -0800343bool BufferLayerConsumer::getTransformToDisplayInverse() const {
344 Mutex::Autolock lock(mMutex);
345 return mCurrentTransformToDisplayInverse;
346}
347
348const Region& BufferLayerConsumer::getSurfaceDamage() const {
349 return mCurrentSurfaceDamage;
350}
351
Steven Thomas44685cb2019-07-23 16:19:31 -0700352void BufferLayerConsumer::mergeSurfaceDamage(const Region& damage) {
353 if (damage.bounds() == Rect::INVALID_RECT ||
354 mCurrentSurfaceDamage.bounds() == Rect::INVALID_RECT) {
355 mCurrentSurfaceDamage = Region::INVALID_REGION;
356 } else {
357 mCurrentSurfaceDamage |= damage;
358 }
359}
360
Chia-I Wu5c6e4632018-01-11 08:54:38 -0800361int BufferLayerConsumer::getCurrentApi() const {
362 Mutex::Autolock lock(mMutex);
363 return mCurrentApi;
364}
365
Alec Mouria90a5702021-04-16 16:36:21 +0000366std::shared_ptr<renderengine::ExternalTexture> BufferLayerConsumer::getCurrentBuffer(
367 int* outSlot, sp<Fence>* outFence) const {
Chia-I Wuf1405182017-11-27 11:29:21 -0800368 Mutex::Autolock lock(mMutex);
369
370 if (outSlot != nullptr) {
371 *outSlot = mCurrentTexture;
372 }
373
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000374 if (outFence != nullptr) {
375 *outFence = mCurrentFence;
376 }
377
Alec Mouria90a5702021-04-16 16:36:21 +0000378 return mCurrentTextureBuffer == nullptr ? nullptr : mCurrentTextureBuffer;
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000379}
380
Chia-I Wuf1405182017-11-27 11:29:21 -0800381Rect BufferLayerConsumer::getCurrentCrop() const {
382 Mutex::Autolock lock(mMutex);
Alec Mouri2ee0dda2019-01-30 16:44:43 -0800383 return getCurrentCropLocked();
384}
385
386Rect BufferLayerConsumer::getCurrentCropLocked() const {
Ana Krulec65d87802020-04-24 11:06:46 -0700387 uint32_t width = mDefaultWidth;
388 uint32_t height = mDefaultHeight;
389 // If the buffer comes with a rotated bit for 90 (or 270) degrees, switch width/height in order
390 // to scale and crop correctly.
391 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
392 width = mDefaultHeight;
393 height = mDefaultWidth;
394 }
395
Chia-I Wuf1405182017-11-27 11:29:21 -0800396 return (mCurrentScalingMode == NATIVE_WINDOW_SCALING_MODE_SCALE_CROP)
Ana Krulec65d87802020-04-24 11:06:46 -0700397 ? GLConsumer::scaleDownCrop(mCurrentCrop, width, height)
Chia-I Wuf1405182017-11-27 11:29:21 -0800398 : mCurrentCrop;
399}
400
401uint32_t BufferLayerConsumer::getCurrentTransform() const {
402 Mutex::Autolock lock(mMutex);
403 return mCurrentTransform;
404}
405
406uint32_t BufferLayerConsumer::getCurrentScalingMode() const {
407 Mutex::Autolock lock(mMutex);
408 return mCurrentScalingMode;
409}
410
411sp<Fence> BufferLayerConsumer::getCurrentFence() const {
412 Mutex::Autolock lock(mMutex);
413 return mCurrentFence;
414}
415
416std::shared_ptr<FenceTime> BufferLayerConsumer::getCurrentFenceTime() const {
417 Mutex::Autolock lock(mMutex);
418 return mCurrentFenceTime;
419}
420
Chia-I Wuf1405182017-11-27 11:29:21 -0800421void BufferLayerConsumer::freeBufferLocked(int slotIndex) {
422 BLC_LOGV("freeBufferLocked: slotIndex=%d", slotIndex);
Alec Mourid7b3a8b2019-03-21 11:44:18 -0700423 std::lock_guard<std::mutex> lock(mImagesMutex);
Chia-I Wuf1405182017-11-27 11:29:21 -0800424 if (slotIndex == mCurrentTexture) {
425 mCurrentTexture = BufferQueue::INVALID_BUFFER_SLOT;
426 }
Alec Mourib5c4f352019-02-19 19:46:38 -0800427 mImages[slotIndex] = nullptr;
Chia-I Wuf1405182017-11-27 11:29:21 -0800428 ConsumerBase::freeBufferLocked(slotIndex);
429}
430
Chia-I Wuc75c44d2017-11-27 14:32:57 -0800431void BufferLayerConsumer::onDisconnect() {
chaviw1a4dba42020-05-27 15:16:15 -0700432 Mutex::Autolock lock(mMutex);
433
434 if (mAbandoned) {
435 // Nothing to do if we're already abandoned.
436 return;
437 }
438
chaviwc65e8642020-03-18 15:35:48 -0700439 mLayer->onDisconnect();
Chia-I Wuc75c44d2017-11-27 14:32:57 -0800440}
441
Chia-I Wufd257f82017-11-27 14:51:06 -0800442void BufferLayerConsumer::onSidebandStreamChanged() {
Dominik Laskowskib4ba8f52021-09-27 18:20:58 -0700443 [[maybe_unused]] FrameAvailableListener* unsafeFrameAvailableListener = nullptr;
Chia-I Wufd257f82017-11-27 14:51:06 -0800444 {
445 Mutex::Autolock lock(mFrameAvailableMutex);
446 unsafeFrameAvailableListener = mFrameAvailableListener.unsafe_get();
447 }
448 sp<ContentsChangedListener> listener;
449 { // scope for the lock
450 Mutex::Autolock lock(mMutex);
451 ALOG_ASSERT(unsafeFrameAvailableListener == mContentsChangedListener.unsafe_get());
452 listener = mContentsChangedListener.promote();
453 }
454
Peiyong Lin566a3b42018-01-09 18:22:43 -0800455 if (listener != nullptr) {
Chia-I Wufd257f82017-11-27 14:51:06 -0800456 listener->onSidebandStreamChanged();
457 }
458}
459
Alec Mouri485e4c32019-04-30 18:24:05 -0700460void BufferLayerConsumer::onBufferAvailable(const BufferItem& item) {
461 if (item.mGraphicBuffer != nullptr && item.mSlot != BufferQueue::INVALID_BUFFER_SLOT) {
462 std::lock_guard<std::mutex> lock(mImagesMutex);
Alec Mouria90a5702021-04-16 16:36:21 +0000463 const std::shared_ptr<renderengine::ExternalTexture>& oldImage = mImages[item.mSlot];
464 if (oldImage == nullptr || oldImage->getBuffer() == nullptr ||
465 oldImage->getBuffer()->getId() != item.mGraphicBuffer->getId()) {
466 mImages[item.mSlot] = std::make_shared<
Vishnu Nairdbbe3852022-01-12 20:22:11 -0800467 renderengine::impl::ExternalTexture>(item.mGraphicBuffer, mRE,
468 renderengine::impl::ExternalTexture::
469 Usage::READABLE);
Alec Mourid7b3a8b2019-03-21 11:44:18 -0700470 }
Alec Mourid7b3a8b2019-03-21 11:44:18 -0700471 }
472}
473
Chia-I Wuc75c44d2017-11-27 14:32:57 -0800474void BufferLayerConsumer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
475 FrameEventHistoryDelta* outDelta) {
chaviw1a4dba42020-05-27 15:16:15 -0700476 Mutex::Autolock lock(mMutex);
477
478 if (mAbandoned) {
479 // Nothing to do if we're already abandoned.
480 return;
481 }
482
chaviwc65e8642020-03-18 15:35:48 -0700483 mLayer->addAndGetFrameTimestamps(newTimestamps, outDelta);
Chia-I Wuc75c44d2017-11-27 14:32:57 -0800484}
485
Chia-I Wuf1405182017-11-27 11:29:21 -0800486void BufferLayerConsumer::abandonLocked() {
487 BLC_LOGV("abandonLocked");
Alec Mourib5c4f352019-02-19 19:46:38 -0800488 mCurrentTextureBuffer = nullptr;
489 for (int i = 0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) {
Alec Mourid7b3a8b2019-03-21 11:44:18 -0700490 std::lock_guard<std::mutex> lock(mImagesMutex);
Alec Mourib5c4f352019-02-19 19:46:38 -0800491 mImages[i] = nullptr;
492 }
Chia-I Wuf1405182017-11-27 11:29:21 -0800493 ConsumerBase::abandonLocked();
494}
495
496status_t BufferLayerConsumer::setConsumerUsageBits(uint64_t usage) {
497 return ConsumerBase::setConsumerUsageBits(usage | DEFAULT_USAGE_FLAGS);
498}
499
500void BufferLayerConsumer::dumpLocked(String8& result, const char* prefix) const {
501 result.appendFormat("%smTexName=%d mCurrentTexture=%d\n"
502 "%smCurrentCrop=[%d,%d,%d,%d] mCurrentTransform=%#x\n",
503 prefix, mTexName, mCurrentTexture, prefix, mCurrentCrop.left,
504 mCurrentCrop.top, mCurrentCrop.right, mCurrentCrop.bottom,
505 mCurrentTransform);
506
507 ConsumerBase::dumpLocked(result, prefix);
508}
Chia-I Wuf1405182017-11-27 11:29:21 -0800509}; // namespace android
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800510
511// TODO(b/129481165): remove the #pragma below and fix conversion issues
512#pragma clang diagnostic pop // ignored "-Wconversion"