blob: 69d2d11a4d585f961565ef21b6949f36ea9ae5d4 [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/Image.h>
44#include <renderengine/RenderEngine.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
170 : mCurrentTextureBuffer->graphicBuffer();
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);
209 if (mImages[item->mSlot] == nullptr || mImages[item->mSlot]->graphicBuffer() == nullptr ||
210 mImages[item->mSlot]->graphicBuffer()->getId() != item->mGraphicBuffer->getId()) {
211 mImages[item->mSlot] = std::make_shared<Image>(item->mGraphicBuffer, mRE);
212 }
Chia-I Wuf1405182017-11-27 11:29:21 -0800213 }
214
215 return NO_ERROR;
216}
217
Chia-I Wuf1405182017-11-27 11:29:21 -0800218status_t BufferLayerConsumer::updateAndReleaseLocked(const BufferItem& item,
Alec Mouri56e538f2019-01-14 15:22:01 -0800219 PendingRelease* pendingRelease) {
Chia-I Wuf1405182017-11-27 11:29:21 -0800220 status_t err = NO_ERROR;
221
222 int slot = item.mSlot;
223
Chia-I Wuf1405182017-11-27 11:29:21 -0800224 BLC_LOGV("updateAndRelease: (slot=%d buf=%p) -> (slot=%d buf=%p)", mCurrentTexture,
Alec Mourib5c4f352019-02-19 19:46:38 -0800225 (mCurrentTextureBuffer != nullptr && mCurrentTextureBuffer->graphicBuffer() != nullptr)
226 ? mCurrentTextureBuffer->graphicBuffer()->handle
227 : 0,
228 slot, mSlots[slot].mGraphicBuffer->handle);
Chia-I Wuf1405182017-11-27 11:29:21 -0800229
230 // Hang onto the pointer so that it isn't freed in the call to
231 // releaseBufferLocked() if we're in shared buffer mode and both buffers are
232 // the same.
Alec Mourid7b3a8b2019-03-21 11:44:18 -0700233
234 std::shared_ptr<Image> nextTextureBuffer;
235 {
236 std::lock_guard<std::mutex> lock(mImagesMutex);
237 nextTextureBuffer = mImages[slot];
238 }
Chia-I Wuf1405182017-11-27 11:29:21 -0800239
240 // release old buffer
241 if (mCurrentTexture != BufferQueue::INVALID_BUFFER_SLOT) {
242 if (pendingRelease == nullptr) {
Alec Mourib5c4f352019-02-19 19:46:38 -0800243 status_t status =
244 releaseBufferLocked(mCurrentTexture, mCurrentTextureBuffer->graphicBuffer());
Chia-I Wuf1405182017-11-27 11:29:21 -0800245 if (status < NO_ERROR) {
246 BLC_LOGE("updateAndRelease: failed to release buffer: %s (%d)", strerror(-status),
247 status);
248 err = status;
249 // keep going, with error raised [?]
250 }
251 } else {
252 pendingRelease->currentTexture = mCurrentTexture;
Alec Mourib5c4f352019-02-19 19:46:38 -0800253 pendingRelease->graphicBuffer = mCurrentTextureBuffer->graphicBuffer();
Chia-I Wuf1405182017-11-27 11:29:21 -0800254 pendingRelease->isPending = true;
255 }
256 }
257
258 // Update the BufferLayerConsumer state.
259 mCurrentTexture = slot;
Alec Mouri39801c02018-10-10 10:44:47 -0700260 mCurrentTextureBuffer = nextTextureBuffer;
Chia-I Wuf1405182017-11-27 11:29:21 -0800261 mCurrentCrop = item.mCrop;
262 mCurrentTransform = item.mTransform;
263 mCurrentScalingMode = item.mScalingMode;
264 mCurrentTimestamp = item.mTimestamp;
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700265 mCurrentDataSpace = static_cast<ui::Dataspace>(item.mDataSpace);
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -0700266 mCurrentHdrMetadata = item.mHdrMetadata;
Chia-I Wuf1405182017-11-27 11:29:21 -0800267 mCurrentFence = item.mFence;
268 mCurrentFenceTime = item.mFenceTime;
269 mCurrentFrameNumber = item.mFrameNumber;
Chia-I Wu67dcc692017-11-27 14:51:06 -0800270 mCurrentTransformToDisplayInverse = item.mTransformToDisplayInverse;
271 mCurrentSurfaceDamage = item.mSurfaceDamage;
Chia-I Wu5c6e4632018-01-11 08:54:38 -0800272 mCurrentApi = item.mApi;
Chia-I Wuf1405182017-11-27 11:29:21 -0800273
274 computeCurrentTransformMatrixLocked();
275
276 return err;
277}
278
Chia-I Wuf1405182017-11-27 11:29:21 -0800279void BufferLayerConsumer::getTransformMatrix(float mtx[16]) {
280 Mutex::Autolock lock(mMutex);
281 memcpy(mtx, mCurrentTransformMatrix, sizeof(mCurrentTransformMatrix));
282}
283
284void BufferLayerConsumer::setFilteringEnabled(bool enabled) {
285 Mutex::Autolock lock(mMutex);
286 if (mAbandoned) {
287 BLC_LOGE("setFilteringEnabled: BufferLayerConsumer is abandoned!");
288 return;
289 }
290 bool needsRecompute = mFilteringEnabled != enabled;
291 mFilteringEnabled = enabled;
292
Alec Mouri39801c02018-10-10 10:44:47 -0700293 if (needsRecompute && mCurrentTextureBuffer == nullptr) {
294 BLC_LOGD("setFilteringEnabled called with mCurrentTextureBuffer == nullptr");
Chia-I Wuf1405182017-11-27 11:29:21 -0800295 }
296
Alec Mouri39801c02018-10-10 10:44:47 -0700297 if (needsRecompute && mCurrentTextureBuffer != nullptr) {
Chia-I Wuf1405182017-11-27 11:29:21 -0800298 computeCurrentTransformMatrixLocked();
299 }
300}
301
302void BufferLayerConsumer::computeCurrentTransformMatrixLocked() {
303 BLC_LOGV("computeCurrentTransformMatrixLocked");
Alec Mourib5c4f352019-02-19 19:46:38 -0800304 if (mCurrentTextureBuffer == nullptr || mCurrentTextureBuffer->graphicBuffer() == nullptr) {
Chia-I Wuf1405182017-11-27 11:29:21 -0800305 BLC_LOGD("computeCurrentTransformMatrixLocked: "
Alec Mouri39801c02018-10-10 10:44:47 -0700306 "mCurrentTextureBuffer is nullptr");
Chia-I Wuf1405182017-11-27 11:29:21 -0800307 }
Alec Mourib5c4f352019-02-19 19:46:38 -0800308 GLConsumer::computeTransformMatrix(mCurrentTransformMatrix,
309 mCurrentTextureBuffer == nullptr
310 ? nullptr
311 : mCurrentTextureBuffer->graphicBuffer(),
Alec Mouri2ee0dda2019-01-30 16:44:43 -0800312 getCurrentCropLocked(), mCurrentTransform,
313 mFilteringEnabled);
Chia-I Wuf1405182017-11-27 11:29:21 -0800314}
315
Chia-I Wuf1405182017-11-27 11:29:21 -0800316nsecs_t BufferLayerConsumer::getTimestamp() {
317 BLC_LOGV("getTimestamp");
318 Mutex::Autolock lock(mMutex);
319 return mCurrentTimestamp;
320}
321
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700322ui::Dataspace BufferLayerConsumer::getCurrentDataSpace() {
Chia-I Wuf1405182017-11-27 11:29:21 -0800323 BLC_LOGV("getCurrentDataSpace");
324 Mutex::Autolock lock(mMutex);
325 return mCurrentDataSpace;
326}
327
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -0700328const HdrMetadata& BufferLayerConsumer::getCurrentHdrMetadata() const {
329 BLC_LOGV("getCurrentHdrMetadata");
330 Mutex::Autolock lock(mMutex);
331 return mCurrentHdrMetadata;
332}
333
Chia-I Wuf1405182017-11-27 11:29:21 -0800334uint64_t BufferLayerConsumer::getFrameNumber() {
335 BLC_LOGV("getFrameNumber");
336 Mutex::Autolock lock(mMutex);
337 return mCurrentFrameNumber;
338}
339
Chia-I Wu67dcc692017-11-27 14:51:06 -0800340bool BufferLayerConsumer::getTransformToDisplayInverse() const {
341 Mutex::Autolock lock(mMutex);
342 return mCurrentTransformToDisplayInverse;
343}
344
345const Region& BufferLayerConsumer::getSurfaceDamage() const {
346 return mCurrentSurfaceDamage;
347}
348
Steven Thomas44685cb2019-07-23 16:19:31 -0700349void BufferLayerConsumer::mergeSurfaceDamage(const Region& damage) {
350 if (damage.bounds() == Rect::INVALID_RECT ||
351 mCurrentSurfaceDamage.bounds() == Rect::INVALID_RECT) {
352 mCurrentSurfaceDamage = Region::INVALID_REGION;
353 } else {
354 mCurrentSurfaceDamage |= damage;
355 }
356}
357
Chia-I Wu5c6e4632018-01-11 08:54:38 -0800358int BufferLayerConsumer::getCurrentApi() const {
359 Mutex::Autolock lock(mMutex);
360 return mCurrentApi;
361}
362
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000363sp<GraphicBuffer> BufferLayerConsumer::getCurrentBuffer(int* outSlot, sp<Fence>* outFence) const {
Chia-I Wuf1405182017-11-27 11:29:21 -0800364 Mutex::Autolock lock(mMutex);
365
366 if (outSlot != nullptr) {
367 *outSlot = mCurrentTexture;
368 }
369
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000370 if (outFence != nullptr) {
371 *outFence = mCurrentFence;
372 }
373
Alec Mourib5c4f352019-02-19 19:46:38 -0800374 return mCurrentTextureBuffer == nullptr ? nullptr : mCurrentTextureBuffer->graphicBuffer();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000375}
376
Chia-I Wuf1405182017-11-27 11:29:21 -0800377Rect BufferLayerConsumer::getCurrentCrop() const {
378 Mutex::Autolock lock(mMutex);
Alec Mouri2ee0dda2019-01-30 16:44:43 -0800379 return getCurrentCropLocked();
380}
381
382Rect BufferLayerConsumer::getCurrentCropLocked() const {
Ana Krulec65d87802020-04-24 11:06:46 -0700383 uint32_t width = mDefaultWidth;
384 uint32_t height = mDefaultHeight;
385 // If the buffer comes with a rotated bit for 90 (or 270) degrees, switch width/height in order
386 // to scale and crop correctly.
387 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
388 width = mDefaultHeight;
389 height = mDefaultWidth;
390 }
391
Chia-I Wuf1405182017-11-27 11:29:21 -0800392 return (mCurrentScalingMode == NATIVE_WINDOW_SCALING_MODE_SCALE_CROP)
Ana Krulec65d87802020-04-24 11:06:46 -0700393 ? GLConsumer::scaleDownCrop(mCurrentCrop, width, height)
Chia-I Wuf1405182017-11-27 11:29:21 -0800394 : mCurrentCrop;
395}
396
397uint32_t BufferLayerConsumer::getCurrentTransform() const {
398 Mutex::Autolock lock(mMutex);
399 return mCurrentTransform;
400}
401
402uint32_t BufferLayerConsumer::getCurrentScalingMode() const {
403 Mutex::Autolock lock(mMutex);
404 return mCurrentScalingMode;
405}
406
407sp<Fence> BufferLayerConsumer::getCurrentFence() const {
408 Mutex::Autolock lock(mMutex);
409 return mCurrentFence;
410}
411
412std::shared_ptr<FenceTime> BufferLayerConsumer::getCurrentFenceTime() const {
413 Mutex::Autolock lock(mMutex);
414 return mCurrentFenceTime;
415}
416
Chia-I Wuf1405182017-11-27 11:29:21 -0800417void BufferLayerConsumer::freeBufferLocked(int slotIndex) {
418 BLC_LOGV("freeBufferLocked: slotIndex=%d", slotIndex);
Alec Mourid7b3a8b2019-03-21 11:44:18 -0700419 std::lock_guard<std::mutex> lock(mImagesMutex);
Chia-I Wuf1405182017-11-27 11:29:21 -0800420 if (slotIndex == mCurrentTexture) {
421 mCurrentTexture = BufferQueue::INVALID_BUFFER_SLOT;
422 }
Alec Mourib5c4f352019-02-19 19:46:38 -0800423 mImages[slotIndex] = nullptr;
Chia-I Wuf1405182017-11-27 11:29:21 -0800424 ConsumerBase::freeBufferLocked(slotIndex);
425}
426
Chia-I Wuc75c44d2017-11-27 14:32:57 -0800427void BufferLayerConsumer::onDisconnect() {
chaviw1a4dba42020-05-27 15:16:15 -0700428 Mutex::Autolock lock(mMutex);
429
430 if (mAbandoned) {
431 // Nothing to do if we're already abandoned.
432 return;
433 }
434
chaviwc65e8642020-03-18 15:35:48 -0700435 mLayer->onDisconnect();
Chia-I Wuc75c44d2017-11-27 14:32:57 -0800436}
437
Chia-I Wufd257f82017-11-27 14:51:06 -0800438void BufferLayerConsumer::onSidebandStreamChanged() {
439 FrameAvailableListener* unsafeFrameAvailableListener = nullptr;
440 {
441 Mutex::Autolock lock(mFrameAvailableMutex);
442 unsafeFrameAvailableListener = mFrameAvailableListener.unsafe_get();
443 }
444 sp<ContentsChangedListener> listener;
445 { // scope for the lock
446 Mutex::Autolock lock(mMutex);
447 ALOG_ASSERT(unsafeFrameAvailableListener == mContentsChangedListener.unsafe_get());
448 listener = mContentsChangedListener.promote();
449 }
450
Peiyong Lin566a3b42018-01-09 18:22:43 -0800451 if (listener != nullptr) {
Chia-I Wufd257f82017-11-27 14:51:06 -0800452 listener->onSidebandStreamChanged();
453 }
454}
455
Alec Mouri485e4c32019-04-30 18:24:05 -0700456void BufferLayerConsumer::onBufferAvailable(const BufferItem& item) {
457 if (item.mGraphicBuffer != nullptr && item.mSlot != BufferQueue::INVALID_BUFFER_SLOT) {
458 std::lock_guard<std::mutex> lock(mImagesMutex);
459 const std::shared_ptr<Image>& oldImage = mImages[item.mSlot];
460 if (oldImage == nullptr || oldImage->graphicBuffer() == nullptr ||
461 oldImage->graphicBuffer()->getId() != item.mGraphicBuffer->getId()) {
462 mImages[item.mSlot] = std::make_shared<Image>(item.mGraphicBuffer, mRE);
Alec Mourid7b3a8b2019-03-21 11:44:18 -0700463 }
Alec Mourid7b3a8b2019-03-21 11:44:18 -0700464 }
465}
466
Chia-I Wuc75c44d2017-11-27 14:32:57 -0800467void BufferLayerConsumer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
468 FrameEventHistoryDelta* outDelta) {
chaviw1a4dba42020-05-27 15:16:15 -0700469 Mutex::Autolock lock(mMutex);
470
471 if (mAbandoned) {
472 // Nothing to do if we're already abandoned.
473 return;
474 }
475
chaviwc65e8642020-03-18 15:35:48 -0700476 mLayer->addAndGetFrameTimestamps(newTimestamps, outDelta);
Chia-I Wuc75c44d2017-11-27 14:32:57 -0800477}
478
Chia-I Wuf1405182017-11-27 11:29:21 -0800479void BufferLayerConsumer::abandonLocked() {
480 BLC_LOGV("abandonLocked");
Alec Mourib5c4f352019-02-19 19:46:38 -0800481 mCurrentTextureBuffer = nullptr;
482 for (int i = 0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) {
Alec Mourid7b3a8b2019-03-21 11:44:18 -0700483 std::lock_guard<std::mutex> lock(mImagesMutex);
Alec Mourib5c4f352019-02-19 19:46:38 -0800484 mImages[i] = nullptr;
485 }
Chia-I Wuf1405182017-11-27 11:29:21 -0800486 ConsumerBase::abandonLocked();
487}
488
489status_t BufferLayerConsumer::setConsumerUsageBits(uint64_t usage) {
490 return ConsumerBase::setConsumerUsageBits(usage | DEFAULT_USAGE_FLAGS);
491}
492
493void BufferLayerConsumer::dumpLocked(String8& result, const char* prefix) const {
494 result.appendFormat("%smTexName=%d mCurrentTexture=%d\n"
495 "%smCurrentCrop=[%d,%d,%d,%d] mCurrentTransform=%#x\n",
496 prefix, mTexName, mCurrentTexture, prefix, mCurrentCrop.left,
497 mCurrentCrop.top, mCurrentCrop.right, mCurrentCrop.bottom,
498 mCurrentTransform);
499
500 ConsumerBase::dumpLocked(result, prefix);
501}
502
Alec Mouri16a99402019-07-29 16:37:30 -0700503BufferLayerConsumer::Image::Image(const sp<GraphicBuffer>& graphicBuffer,
504 renderengine::RenderEngine& engine)
505 : mGraphicBuffer(graphicBuffer), mRE(engine) {
Peiyong Lin637f4ac2020-10-22 00:39:39 -0700506 if (graphicBuffer != nullptr && (graphicBuffer->getUsage() & GRALLOC_USAGE_PROTECTED)) {
507 return;
508 }
Alec Mouri16a99402019-07-29 16:37:30 -0700509 mRE.cacheExternalTextureBuffer(mGraphicBuffer);
510}
511
Alec Mourib5c4f352019-02-19 19:46:38 -0800512BufferLayerConsumer::Image::~Image() {
513 if (mGraphicBuffer != nullptr) {
Alec Mourif1907332019-03-07 13:05:26 -0800514 ALOGV("Destroying buffer: %" PRId64, mGraphicBuffer->getId());
Alec Mourib5c4f352019-02-19 19:46:38 -0800515 mRE.unbindExternalTextureBuffer(mGraphicBuffer->getId());
516 }
517}
Chia-I Wuf1405182017-11-27 11:29:21 -0800518}; // namespace android
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800519
520// TODO(b/129481165): remove the #pragma below and fix conversion issues
521#pragma clang diagnostic pop // ignored "-Wconversion"