blob: 96b22478ab3d73e63edd280a469f03943936b8aa [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>
Chia-I Wuf1405182017-11-27 11:29:21 -080044#include <utils/Log.h>
45#include <utils/String8.h>
46#include <utils/Trace.h>
47
Chia-I Wuf1405182017-11-27 11:29:21 -080048namespace android {
49
50// Macros for including the BufferLayerConsumer name in log messages
51#define BLC_LOGV(x, ...) ALOGV("[%s] " x, mName.string(), ##__VA_ARGS__)
52#define BLC_LOGD(x, ...) ALOGD("[%s] " x, mName.string(), ##__VA_ARGS__)
53//#define BLC_LOGI(x, ...) ALOGI("[%s] " x, mName.string(), ##__VA_ARGS__)
54#define BLC_LOGW(x, ...) ALOGW("[%s] " x, mName.string(), ##__VA_ARGS__)
55#define BLC_LOGE(x, ...) ALOGE("[%s] " x, mName.string(), ##__VA_ARGS__)
56
Chia-I Wuf1405182017-11-27 11:29:21 -080057static const mat4 mtxIdentity;
58
Lloyd Pique144e1162017-12-20 16:44:52 -080059BufferLayerConsumer::BufferLayerConsumer(const sp<IGraphicBufferConsumer>& bq,
Peiyong Lin833074a2018-08-28 11:53:54 -070060 renderengine::RenderEngine& engine, uint32_t tex,
61 Layer* layer)
Chia-I Wubd854bf2017-11-27 13:41:26 -080062 : ConsumerBase(bq, false),
Chia-I Wuf1405182017-11-27 11:29:21 -080063 mCurrentCrop(Rect::EMPTY_RECT),
64 mCurrentTransform(0),
65 mCurrentScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE),
66 mCurrentFence(Fence::NO_FENCE),
67 mCurrentTimestamp(0),
Peiyong Lin34beb7a2018-03-28 11:57:12 -070068 mCurrentDataSpace(ui::Dataspace::UNKNOWN),
Chia-I Wuf1405182017-11-27 11:29:21 -080069 mCurrentFrameNumber(0),
Chia-I Wu67dcc692017-11-27 14:51:06 -080070 mCurrentTransformToDisplayInverse(false),
71 mCurrentSurfaceDamage(),
Chia-I Wu5c6e4632018-01-11 08:54:38 -080072 mCurrentApi(0),
Chia-I Wuf1405182017-11-27 11:29:21 -080073 mDefaultWidth(1),
74 mDefaultHeight(1),
75 mFilteringEnabled(true),
Chia-I Wu9f2db772017-11-30 21:06:50 -080076 mRE(engine),
Chia-I Wuf1405182017-11-27 11:29:21 -080077 mTexName(tex),
Chia-I Wuc75c44d2017-11-27 14:32:57 -080078 mLayer(layer),
Chia-I Wuc91077c2017-11-27 13:32:04 -080079 mCurrentTexture(BufferQueue::INVALID_BUFFER_SLOT) {
Chia-I Wuf1405182017-11-27 11:29:21 -080080 BLC_LOGV("BufferLayerConsumer");
81
82 memcpy(mCurrentTransformMatrix, mtxIdentity.asArray(), sizeof(mCurrentTransformMatrix));
83
84 mConsumer->setConsumerUsageBits(DEFAULT_USAGE_FLAGS);
85}
86
87status_t BufferLayerConsumer::setDefaultBufferSize(uint32_t w, uint32_t h) {
88 Mutex::Autolock lock(mMutex);
89 if (mAbandoned) {
90 BLC_LOGE("setDefaultBufferSize: BufferLayerConsumer is abandoned!");
91 return NO_INIT;
92 }
93 mDefaultWidth = w;
94 mDefaultHeight = h;
95 return mConsumer->setDefaultBufferSize(w, h);
96}
97
Chia-I Wufd257f82017-11-27 14:51:06 -080098void BufferLayerConsumer::setContentsChangedListener(const wp<ContentsChangedListener>& listener) {
99 setFrameAvailableListener(listener);
100 Mutex::Autolock lock(mMutex);
101 mContentsChangedListener = listener;
102}
103
Ana Krulec010d2192018-10-08 06:29:54 -0700104status_t BufferLayerConsumer::updateTexImage(BufferRejecter* rejecter, nsecs_t expectedPresentTime,
Chia-I Wuda5c7302017-11-27 14:51:06 -0800105 bool* autoRefresh, bool* queuedBuffer,
Alec Mouri56e538f2019-01-14 15:22:01 -0800106 uint64_t maxFrameNumber) {
Chia-I Wuf1405182017-11-27 11:29:21 -0800107 ATRACE_CALL();
108 BLC_LOGV("updateTexImage");
109 Mutex::Autolock lock(mMutex);
110
111 if (mAbandoned) {
112 BLC_LOGE("updateTexImage: BufferLayerConsumer is abandoned!");
113 return NO_INIT;
114 }
115
Chia-I Wuf1405182017-11-27 11:29:21 -0800116 BufferItem item;
117
118 // Acquire the next buffer.
119 // In asynchronous mode the list is guaranteed to be one buffer
120 // deep, while in synchronous mode we use the oldest buffer.
Ana Krulec010d2192018-10-08 06:29:54 -0700121 status_t err = acquireBufferLocked(&item, expectedPresentTime, maxFrameNumber);
Chia-I Wuf1405182017-11-27 11:29:21 -0800122 if (err != NO_ERROR) {
123 if (err == BufferQueue::NO_BUFFER_AVAILABLE) {
Chia-I Wuf1405182017-11-27 11:29:21 -0800124 err = NO_ERROR;
Chia-I Wuda5c7302017-11-27 14:51:06 -0800125 } else if (err == BufferQueue::PRESENT_LATER) {
126 // return the error, without logging
Chia-I Wuf1405182017-11-27 11:29:21 -0800127 } else {
128 BLC_LOGE("updateTexImage: acquire failed: %s (%d)", strerror(-err), err);
129 }
130 return err;
131 }
132
Chia-I Wuda5c7302017-11-27 14:51:06 -0800133 if (autoRefresh) {
134 *autoRefresh = item.mAutoRefresh;
135 }
136
137 if (queuedBuffer) {
138 *queuedBuffer = item.mQueuedBuffer;
139 }
140
141 // We call the rejecter here, in case the caller has a reason to
142 // not accept this buffer. This is used by SurfaceFlinger to
143 // reject buffers which have the wrong size
144 int slot = item.mSlot;
145 if (rejecter && rejecter->reject(mSlots[slot].mGraphicBuffer, item)) {
146 releaseBufferLocked(slot, mSlots[slot].mGraphicBuffer);
147 return BUFFER_REJECTED;
148 }
149
Chia-I Wuf1405182017-11-27 11:29:21 -0800150 // Release the previous buffer.
Alec Mouri56e538f2019-01-14 15:22:01 -0800151 err = updateAndReleaseLocked(item, &mPendingRelease);
Chia-I Wuf1405182017-11-27 11:29:21 -0800152 if (err != NO_ERROR) {
Chia-I Wuf1405182017-11-27 11:29:21 -0800153 return err;
154 }
Chia-I Wuda5c7302017-11-27 14:51:06 -0800155 return err;
156}
157
158void BufferLayerConsumer::setReleaseFence(const sp<Fence>& fence) {
159 if (!fence->isValid()) {
160 return;
161 }
162
163 auto slot = mPendingRelease.isPending ? mPendingRelease.currentTexture : mCurrentTexture;
164 if (slot == BufferQueue::INVALID_BUFFER_SLOT) {
165 return;
166 }
167
Alec Mourib5c4f352019-02-19 19:46:38 -0800168 auto buffer = mPendingRelease.isPending ? mPendingRelease.graphicBuffer
Alec Mouria90a5702021-04-16 16:36:21 +0000169 : mCurrentTextureBuffer->getBuffer();
Chia-I Wuda5c7302017-11-27 14:51:06 -0800170 auto err = addReleaseFence(slot, buffer, fence);
171 if (err != OK) {
172 BLC_LOGE("setReleaseFence: failed to add the fence: %s (%d)", strerror(-err), err);
173 }
174}
175
176bool BufferLayerConsumer::releasePendingBuffer() {
177 if (!mPendingRelease.isPending) {
178 BLC_LOGV("Pending buffer already released");
179 return false;
180 }
181 BLC_LOGV("Releasing pending buffer");
182 Mutex::Autolock lock(mMutex);
183 status_t result =
184 releaseBufferLocked(mPendingRelease.currentTexture, mPendingRelease.graphicBuffer);
185 if (result < NO_ERROR) {
186 BLC_LOGE("releasePendingBuffer failed: %s (%d)", strerror(-result), result);
187 }
188 mPendingRelease = PendingRelease();
189 return true;
Chia-I Wuf1405182017-11-27 11:29:21 -0800190}
191
Chia-I Wu0cb75ac2017-11-27 15:56:04 -0800192sp<Fence> BufferLayerConsumer::getPrevFinalReleaseFence() const {
193 Mutex::Autolock lock(mMutex);
194 return ConsumerBase::mPrevFinalReleaseFence;
195}
196
Chia-I Wuf1405182017-11-27 11:29:21 -0800197status_t BufferLayerConsumer::acquireBufferLocked(BufferItem* item, nsecs_t presentWhen,
198 uint64_t maxFrameNumber) {
199 status_t err = ConsumerBase::acquireBufferLocked(item, presentWhen, maxFrameNumber);
200 if (err != NO_ERROR) {
201 return err;
202 }
203
204 // If item->mGraphicBuffer is not null, this buffer has not been acquired
Alec Mourib5c4f352019-02-19 19:46:38 -0800205 // before, so we need to clean up old references.
Peiyong Lin566a3b42018-01-09 18:22:43 -0800206 if (item->mGraphicBuffer != nullptr) {
Alec Mourid7b3a8b2019-03-21 11:44:18 -0700207 std::lock_guard<std::mutex> lock(mImagesMutex);
Alec Mouria90a5702021-04-16 16:36:21 +0000208 if (mImages[item->mSlot] == nullptr || mImages[item->mSlot]->getBuffer() == nullptr ||
209 mImages[item->mSlot]->getBuffer()->getId() != item->mGraphicBuffer->getId()) {
210 mImages[item->mSlot] = std::make_shared<
211 renderengine::ExternalTexture>(item->mGraphicBuffer, mRE,
212 renderengine::ExternalTexture::Usage::READABLE);
Alec Mourid7b3a8b2019-03-21 11:44:18 -0700213 }
Chia-I Wuf1405182017-11-27 11:29:21 -0800214 }
215
216 return NO_ERROR;
217}
218
Chia-I Wuf1405182017-11-27 11:29:21 -0800219status_t BufferLayerConsumer::updateAndReleaseLocked(const BufferItem& item,
Alec Mouri56e538f2019-01-14 15:22:01 -0800220 PendingRelease* pendingRelease) {
Chia-I Wuf1405182017-11-27 11:29:21 -0800221 status_t err = NO_ERROR;
222
223 int slot = item.mSlot;
224
Chia-I Wuf1405182017-11-27 11:29:21 -0800225 BLC_LOGV("updateAndRelease: (slot=%d buf=%p) -> (slot=%d buf=%p)", mCurrentTexture,
Alec Mouria90a5702021-04-16 16:36:21 +0000226 (mCurrentTextureBuffer != nullptr && mCurrentTextureBuffer->getBuffer() != nullptr)
227 ? mCurrentTextureBuffer->getBuffer()->handle
Alec Mourib5c4f352019-02-19 19:46:38 -0800228 : 0,
229 slot, mSlots[slot].mGraphicBuffer->handle);
Chia-I Wuf1405182017-11-27 11:29:21 -0800230
231 // Hang onto the pointer so that it isn't freed in the call to
232 // releaseBufferLocked() if we're in shared buffer mode and both buffers are
233 // the same.
Alec Mourid7b3a8b2019-03-21 11:44:18 -0700234
Alec Mouria90a5702021-04-16 16:36:21 +0000235 std::shared_ptr<renderengine::ExternalTexture> nextTextureBuffer;
Alec Mourid7b3a8b2019-03-21 11:44:18 -0700236 {
237 std::lock_guard<std::mutex> lock(mImagesMutex);
238 nextTextureBuffer = mImages[slot];
239 }
Chia-I Wuf1405182017-11-27 11:29:21 -0800240
241 // release old buffer
242 if (mCurrentTexture != BufferQueue::INVALID_BUFFER_SLOT) {
243 if (pendingRelease == nullptr) {
Alec Mourib5c4f352019-02-19 19:46:38 -0800244 status_t status =
Alec Mouria90a5702021-04-16 16:36:21 +0000245 releaseBufferLocked(mCurrentTexture, mCurrentTextureBuffer->getBuffer());
Chia-I Wuf1405182017-11-27 11:29:21 -0800246 if (status < NO_ERROR) {
247 BLC_LOGE("updateAndRelease: failed to release buffer: %s (%d)", strerror(-status),
248 status);
249 err = status;
250 // keep going, with error raised [?]
251 }
252 } else {
253 pendingRelease->currentTexture = mCurrentTexture;
Alec Mouria90a5702021-04-16 16:36:21 +0000254 pendingRelease->graphicBuffer = mCurrentTextureBuffer->getBuffer();
Chia-I Wuf1405182017-11-27 11:29:21 -0800255 pendingRelease->isPending = true;
256 }
257 }
258
259 // Update the BufferLayerConsumer state.
260 mCurrentTexture = slot;
Alec Mouri39801c02018-10-10 10:44:47 -0700261 mCurrentTextureBuffer = nextTextureBuffer;
Chia-I Wuf1405182017-11-27 11:29:21 -0800262 mCurrentCrop = item.mCrop;
263 mCurrentTransform = item.mTransform;
264 mCurrentScalingMode = item.mScalingMode;
265 mCurrentTimestamp = item.mTimestamp;
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700266 mCurrentDataSpace = static_cast<ui::Dataspace>(item.mDataSpace);
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -0700267 mCurrentHdrMetadata = item.mHdrMetadata;
Chia-I Wuf1405182017-11-27 11:29:21 -0800268 mCurrentFence = item.mFence;
269 mCurrentFenceTime = item.mFenceTime;
270 mCurrentFrameNumber = item.mFrameNumber;
Chia-I Wu67dcc692017-11-27 14:51:06 -0800271 mCurrentTransformToDisplayInverse = item.mTransformToDisplayInverse;
272 mCurrentSurfaceDamage = item.mSurfaceDamage;
Chia-I Wu5c6e4632018-01-11 08:54:38 -0800273 mCurrentApi = item.mApi;
Chia-I Wuf1405182017-11-27 11:29:21 -0800274
275 computeCurrentTransformMatrixLocked();
276
277 return err;
278}
279
Chia-I Wuf1405182017-11-27 11:29:21 -0800280void BufferLayerConsumer::getTransformMatrix(float mtx[16]) {
281 Mutex::Autolock lock(mMutex);
282 memcpy(mtx, mCurrentTransformMatrix, sizeof(mCurrentTransformMatrix));
283}
284
285void BufferLayerConsumer::setFilteringEnabled(bool enabled) {
286 Mutex::Autolock lock(mMutex);
287 if (mAbandoned) {
288 BLC_LOGE("setFilteringEnabled: BufferLayerConsumer is abandoned!");
289 return;
290 }
291 bool needsRecompute = mFilteringEnabled != enabled;
292 mFilteringEnabled = enabled;
293
Alec Mouri39801c02018-10-10 10:44:47 -0700294 if (needsRecompute && mCurrentTextureBuffer == nullptr) {
295 BLC_LOGD("setFilteringEnabled called with mCurrentTextureBuffer == nullptr");
Chia-I Wuf1405182017-11-27 11:29:21 -0800296 }
297
Alec Mouri39801c02018-10-10 10:44:47 -0700298 if (needsRecompute && mCurrentTextureBuffer != nullptr) {
Chia-I Wuf1405182017-11-27 11:29:21 -0800299 computeCurrentTransformMatrixLocked();
300 }
301}
302
303void BufferLayerConsumer::computeCurrentTransformMatrixLocked() {
304 BLC_LOGV("computeCurrentTransformMatrixLocked");
Alec Mouria90a5702021-04-16 16:36:21 +0000305 if (mCurrentTextureBuffer == nullptr || mCurrentTextureBuffer->getBuffer() == nullptr) {
Chia-I Wuf1405182017-11-27 11:29:21 -0800306 BLC_LOGD("computeCurrentTransformMatrixLocked: "
Alec Mouri39801c02018-10-10 10:44:47 -0700307 "mCurrentTextureBuffer is nullptr");
Chia-I Wuf1405182017-11-27 11:29:21 -0800308 }
Alec Mourib5c4f352019-02-19 19:46:38 -0800309 GLConsumer::computeTransformMatrix(mCurrentTransformMatrix,
310 mCurrentTextureBuffer == nullptr
311 ? nullptr
Alec Mouria90a5702021-04-16 16:36:21 +0000312 : mCurrentTextureBuffer->getBuffer(),
Alec Mouri2ee0dda2019-01-30 16:44:43 -0800313 getCurrentCropLocked(), mCurrentTransform,
314 mFilteringEnabled);
Chia-I Wuf1405182017-11-27 11:29:21 -0800315}
316
Chia-I Wuf1405182017-11-27 11:29:21 -0800317nsecs_t BufferLayerConsumer::getTimestamp() {
318 BLC_LOGV("getTimestamp");
319 Mutex::Autolock lock(mMutex);
320 return mCurrentTimestamp;
321}
322
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700323ui::Dataspace BufferLayerConsumer::getCurrentDataSpace() {
Chia-I Wuf1405182017-11-27 11:29:21 -0800324 BLC_LOGV("getCurrentDataSpace");
325 Mutex::Autolock lock(mMutex);
326 return mCurrentDataSpace;
327}
328
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -0700329const HdrMetadata& BufferLayerConsumer::getCurrentHdrMetadata() const {
330 BLC_LOGV("getCurrentHdrMetadata");
331 Mutex::Autolock lock(mMutex);
332 return mCurrentHdrMetadata;
333}
334
Chia-I Wuf1405182017-11-27 11:29:21 -0800335uint64_t BufferLayerConsumer::getFrameNumber() {
336 BLC_LOGV("getFrameNumber");
337 Mutex::Autolock lock(mMutex);
338 return mCurrentFrameNumber;
339}
340
Chia-I Wu67dcc692017-11-27 14:51:06 -0800341bool BufferLayerConsumer::getTransformToDisplayInverse() const {
342 Mutex::Autolock lock(mMutex);
343 return mCurrentTransformToDisplayInverse;
344}
345
346const Region& BufferLayerConsumer::getSurfaceDamage() const {
347 return mCurrentSurfaceDamage;
348}
349
Steven Thomas44685cb2019-07-23 16:19:31 -0700350void BufferLayerConsumer::mergeSurfaceDamage(const Region& damage) {
351 if (damage.bounds() == Rect::INVALID_RECT ||
352 mCurrentSurfaceDamage.bounds() == Rect::INVALID_RECT) {
353 mCurrentSurfaceDamage = Region::INVALID_REGION;
354 } else {
355 mCurrentSurfaceDamage |= damage;
356 }
357}
358
Chia-I Wu5c6e4632018-01-11 08:54:38 -0800359int BufferLayerConsumer::getCurrentApi() const {
360 Mutex::Autolock lock(mMutex);
361 return mCurrentApi;
362}
363
Alec Mouria90a5702021-04-16 16:36:21 +0000364std::shared_ptr<renderengine::ExternalTexture> BufferLayerConsumer::getCurrentBuffer(
365 int* outSlot, sp<Fence>* outFence) const {
Chia-I Wuf1405182017-11-27 11:29:21 -0800366 Mutex::Autolock lock(mMutex);
367
368 if (outSlot != nullptr) {
369 *outSlot = mCurrentTexture;
370 }
371
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000372 if (outFence != nullptr) {
373 *outFence = mCurrentFence;
374 }
375
Alec Mouria90a5702021-04-16 16:36:21 +0000376 return mCurrentTextureBuffer == nullptr ? nullptr : mCurrentTextureBuffer;
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000377}
378
Chia-I Wuf1405182017-11-27 11:29:21 -0800379Rect BufferLayerConsumer::getCurrentCrop() const {
380 Mutex::Autolock lock(mMutex);
Alec Mouri2ee0dda2019-01-30 16:44:43 -0800381 return getCurrentCropLocked();
382}
383
384Rect BufferLayerConsumer::getCurrentCropLocked() const {
Ana Krulec65d87802020-04-24 11:06:46 -0700385 uint32_t width = mDefaultWidth;
386 uint32_t height = mDefaultHeight;
387 // If the buffer comes with a rotated bit for 90 (or 270) degrees, switch width/height in order
388 // to scale and crop correctly.
389 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
390 width = mDefaultHeight;
391 height = mDefaultWidth;
392 }
393
Chia-I Wuf1405182017-11-27 11:29:21 -0800394 return (mCurrentScalingMode == NATIVE_WINDOW_SCALING_MODE_SCALE_CROP)
Ana Krulec65d87802020-04-24 11:06:46 -0700395 ? GLConsumer::scaleDownCrop(mCurrentCrop, width, height)
Chia-I Wuf1405182017-11-27 11:29:21 -0800396 : mCurrentCrop;
397}
398
399uint32_t BufferLayerConsumer::getCurrentTransform() const {
400 Mutex::Autolock lock(mMutex);
401 return mCurrentTransform;
402}
403
404uint32_t BufferLayerConsumer::getCurrentScalingMode() const {
405 Mutex::Autolock lock(mMutex);
406 return mCurrentScalingMode;
407}
408
409sp<Fence> BufferLayerConsumer::getCurrentFence() const {
410 Mutex::Autolock lock(mMutex);
411 return mCurrentFence;
412}
413
414std::shared_ptr<FenceTime> BufferLayerConsumer::getCurrentFenceTime() const {
415 Mutex::Autolock lock(mMutex);
416 return mCurrentFenceTime;
417}
418
Chia-I Wuf1405182017-11-27 11:29:21 -0800419void BufferLayerConsumer::freeBufferLocked(int slotIndex) {
420 BLC_LOGV("freeBufferLocked: slotIndex=%d", slotIndex);
Alec Mourid7b3a8b2019-03-21 11:44:18 -0700421 std::lock_guard<std::mutex> lock(mImagesMutex);
Chia-I Wuf1405182017-11-27 11:29:21 -0800422 if (slotIndex == mCurrentTexture) {
423 mCurrentTexture = BufferQueue::INVALID_BUFFER_SLOT;
424 }
Alec Mourib5c4f352019-02-19 19:46:38 -0800425 mImages[slotIndex] = nullptr;
Chia-I Wuf1405182017-11-27 11:29:21 -0800426 ConsumerBase::freeBufferLocked(slotIndex);
427}
428
Chia-I Wuc75c44d2017-11-27 14:32:57 -0800429void BufferLayerConsumer::onDisconnect() {
chaviw1a4dba42020-05-27 15:16:15 -0700430 Mutex::Autolock lock(mMutex);
431
432 if (mAbandoned) {
433 // Nothing to do if we're already abandoned.
434 return;
435 }
436
chaviwc65e8642020-03-18 15:35:48 -0700437 mLayer->onDisconnect();
Chia-I Wuc75c44d2017-11-27 14:32:57 -0800438}
439
Chia-I Wufd257f82017-11-27 14:51:06 -0800440void BufferLayerConsumer::onSidebandStreamChanged() {
441 FrameAvailableListener* unsafeFrameAvailableListener = nullptr;
442 {
443 Mutex::Autolock lock(mFrameAvailableMutex);
444 unsafeFrameAvailableListener = mFrameAvailableListener.unsafe_get();
445 }
446 sp<ContentsChangedListener> listener;
447 { // scope for the lock
448 Mutex::Autolock lock(mMutex);
449 ALOG_ASSERT(unsafeFrameAvailableListener == mContentsChangedListener.unsafe_get());
450 listener = mContentsChangedListener.promote();
451 }
452
Peiyong Lin566a3b42018-01-09 18:22:43 -0800453 if (listener != nullptr) {
Chia-I Wufd257f82017-11-27 14:51:06 -0800454 listener->onSidebandStreamChanged();
455 }
456}
457
Alec Mouri485e4c32019-04-30 18:24:05 -0700458void BufferLayerConsumer::onBufferAvailable(const BufferItem& item) {
459 if (item.mGraphicBuffer != nullptr && item.mSlot != BufferQueue::INVALID_BUFFER_SLOT) {
460 std::lock_guard<std::mutex> lock(mImagesMutex);
Alec Mouria90a5702021-04-16 16:36:21 +0000461 const std::shared_ptr<renderengine::ExternalTexture>& oldImage = mImages[item.mSlot];
462 if (oldImage == nullptr || oldImage->getBuffer() == nullptr ||
463 oldImage->getBuffer()->getId() != item.mGraphicBuffer->getId()) {
464 mImages[item.mSlot] = std::make_shared<
465 renderengine::ExternalTexture>(item.mGraphicBuffer, mRE,
466 renderengine::ExternalTexture::Usage::READABLE);
Alec Mourid7b3a8b2019-03-21 11:44:18 -0700467 }
Alec Mourid7b3a8b2019-03-21 11:44:18 -0700468 }
469}
470
Chia-I Wuc75c44d2017-11-27 14:32:57 -0800471void BufferLayerConsumer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
472 FrameEventHistoryDelta* outDelta) {
chaviw1a4dba42020-05-27 15:16:15 -0700473 Mutex::Autolock lock(mMutex);
474
475 if (mAbandoned) {
476 // Nothing to do if we're already abandoned.
477 return;
478 }
479
chaviwc65e8642020-03-18 15:35:48 -0700480 mLayer->addAndGetFrameTimestamps(newTimestamps, outDelta);
Chia-I Wuc75c44d2017-11-27 14:32:57 -0800481}
482
Chia-I Wuf1405182017-11-27 11:29:21 -0800483void BufferLayerConsumer::abandonLocked() {
484 BLC_LOGV("abandonLocked");
Alec Mourib5c4f352019-02-19 19:46:38 -0800485 mCurrentTextureBuffer = nullptr;
486 for (int i = 0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) {
Alec Mourid7b3a8b2019-03-21 11:44:18 -0700487 std::lock_guard<std::mutex> lock(mImagesMutex);
Alec Mourib5c4f352019-02-19 19:46:38 -0800488 mImages[i] = nullptr;
489 }
Chia-I Wuf1405182017-11-27 11:29:21 -0800490 ConsumerBase::abandonLocked();
491}
492
493status_t BufferLayerConsumer::setConsumerUsageBits(uint64_t usage) {
494 return ConsumerBase::setConsumerUsageBits(usage | DEFAULT_USAGE_FLAGS);
495}
496
497void BufferLayerConsumer::dumpLocked(String8& result, const char* prefix) const {
498 result.appendFormat("%smTexName=%d mCurrentTexture=%d\n"
499 "%smCurrentCrop=[%d,%d,%d,%d] mCurrentTransform=%#x\n",
500 prefix, mTexName, mCurrentTexture, prefix, mCurrentCrop.left,
501 mCurrentCrop.top, mCurrentCrop.right, mCurrentCrop.bottom,
502 mCurrentTransform);
503
504 ConsumerBase::dumpLocked(result, prefix);
505}
Chia-I Wuf1405182017-11-27 11:29:21 -0800506}; // namespace android
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800507
508// TODO(b/129481165): remove the #pragma below and fix conversion issues
509#pragma clang diagnostic pop // ignored "-Wconversion"