blob: e50a9095fe3822f7c4515c8b3f37538aa067df82 [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"
Ana Krulecfefcb582018-08-07 14:22:37 -070028#include "Scheduler/DispSync.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 }
156
Alec Mouri3221a892018-09-28 20:45:06 +0000157 if (!mRE.useNativeFenceSync()) {
Chia-I Wuda5c7302017-11-27 14:51:06 -0800158 // Bind the new buffer to the GL texture.
159 //
160 // Older devices require the "implicit" synchronization provided
161 // by glEGLImageTargetTexture2DOES, which this method calls. Newer
162 // devices will either call this in Layer::onDraw, or (if it's not
163 // a GL-composited layer) not at all.
164 err = bindTextureImageLocked();
165 }
166
167 return err;
168}
169
Chia-I Wu0cb75ac2017-11-27 15:56:04 -0800170status_t BufferLayerConsumer::bindTextureImage() {
171 Mutex::Autolock lock(mMutex);
172 return bindTextureImageLocked();
173}
174
Chia-I Wuda5c7302017-11-27 14:51:06 -0800175void BufferLayerConsumer::setReleaseFence(const sp<Fence>& fence) {
176 if (!fence->isValid()) {
177 return;
178 }
179
180 auto slot = mPendingRelease.isPending ? mPendingRelease.currentTexture : mCurrentTexture;
181 if (slot == BufferQueue::INVALID_BUFFER_SLOT) {
182 return;
183 }
184
Alec Mourib5c4f352019-02-19 19:46:38 -0800185 auto buffer = mPendingRelease.isPending ? mPendingRelease.graphicBuffer
186 : mCurrentTextureBuffer->graphicBuffer();
Chia-I Wuda5c7302017-11-27 14:51:06 -0800187 auto err = addReleaseFence(slot, buffer, fence);
188 if (err != OK) {
189 BLC_LOGE("setReleaseFence: failed to add the fence: %s (%d)", strerror(-err), err);
190 }
191}
192
193bool BufferLayerConsumer::releasePendingBuffer() {
194 if (!mPendingRelease.isPending) {
195 BLC_LOGV("Pending buffer already released");
196 return false;
197 }
198 BLC_LOGV("Releasing pending buffer");
199 Mutex::Autolock lock(mMutex);
200 status_t result =
201 releaseBufferLocked(mPendingRelease.currentTexture, mPendingRelease.graphicBuffer);
202 if (result < NO_ERROR) {
203 BLC_LOGE("releasePendingBuffer failed: %s (%d)", strerror(-result), result);
204 }
205 mPendingRelease = PendingRelease();
206 return true;
Chia-I Wuf1405182017-11-27 11:29:21 -0800207}
208
Chia-I Wu0cb75ac2017-11-27 15:56:04 -0800209sp<Fence> BufferLayerConsumer::getPrevFinalReleaseFence() const {
210 Mutex::Autolock lock(mMutex);
211 return ConsumerBase::mPrevFinalReleaseFence;
212}
213
Chia-I Wuf1405182017-11-27 11:29:21 -0800214status_t BufferLayerConsumer::acquireBufferLocked(BufferItem* item, nsecs_t presentWhen,
215 uint64_t maxFrameNumber) {
216 status_t err = ConsumerBase::acquireBufferLocked(item, presentWhen, maxFrameNumber);
217 if (err != NO_ERROR) {
218 return err;
219 }
220
221 // If item->mGraphicBuffer is not null, this buffer has not been acquired
Alec Mourib5c4f352019-02-19 19:46:38 -0800222 // before, so we need to clean up old references.
Peiyong Lin566a3b42018-01-09 18:22:43 -0800223 if (item->mGraphicBuffer != nullptr) {
Alec Mourid7b3a8b2019-03-21 11:44:18 -0700224 std::lock_guard<std::mutex> lock(mImagesMutex);
225 if (mImages[item->mSlot] == nullptr || mImages[item->mSlot]->graphicBuffer() == nullptr ||
226 mImages[item->mSlot]->graphicBuffer()->getId() != item->mGraphicBuffer->getId()) {
227 mImages[item->mSlot] = std::make_shared<Image>(item->mGraphicBuffer, mRE);
228 }
Chia-I Wuf1405182017-11-27 11:29:21 -0800229 }
230
231 return NO_ERROR;
232}
233
Chia-I Wuf1405182017-11-27 11:29:21 -0800234status_t BufferLayerConsumer::updateAndReleaseLocked(const BufferItem& item,
Alec Mouri56e538f2019-01-14 15:22:01 -0800235 PendingRelease* pendingRelease) {
Chia-I Wuf1405182017-11-27 11:29:21 -0800236 status_t err = NO_ERROR;
237
238 int slot = item.mSlot;
239
Chia-I Wuf1405182017-11-27 11:29:21 -0800240 BLC_LOGV("updateAndRelease: (slot=%d buf=%p) -> (slot=%d buf=%p)", mCurrentTexture,
Alec Mourib5c4f352019-02-19 19:46:38 -0800241 (mCurrentTextureBuffer != nullptr && mCurrentTextureBuffer->graphicBuffer() != nullptr)
242 ? mCurrentTextureBuffer->graphicBuffer()->handle
243 : 0,
244 slot, mSlots[slot].mGraphicBuffer->handle);
Chia-I Wuf1405182017-11-27 11:29:21 -0800245
246 // Hang onto the pointer so that it isn't freed in the call to
247 // releaseBufferLocked() if we're in shared buffer mode and both buffers are
248 // the same.
Alec Mourid7b3a8b2019-03-21 11:44:18 -0700249
250 std::shared_ptr<Image> nextTextureBuffer;
251 {
252 std::lock_guard<std::mutex> lock(mImagesMutex);
253 nextTextureBuffer = mImages[slot];
254 }
Chia-I Wuf1405182017-11-27 11:29:21 -0800255
256 // release old buffer
257 if (mCurrentTexture != BufferQueue::INVALID_BUFFER_SLOT) {
258 if (pendingRelease == nullptr) {
Alec Mourib5c4f352019-02-19 19:46:38 -0800259 status_t status =
260 releaseBufferLocked(mCurrentTexture, mCurrentTextureBuffer->graphicBuffer());
Chia-I Wuf1405182017-11-27 11:29:21 -0800261 if (status < NO_ERROR) {
262 BLC_LOGE("updateAndRelease: failed to release buffer: %s (%d)", strerror(-status),
263 status);
264 err = status;
265 // keep going, with error raised [?]
266 }
267 } else {
268 pendingRelease->currentTexture = mCurrentTexture;
Alec Mourib5c4f352019-02-19 19:46:38 -0800269 pendingRelease->graphicBuffer = mCurrentTextureBuffer->graphicBuffer();
Chia-I Wuf1405182017-11-27 11:29:21 -0800270 pendingRelease->isPending = true;
271 }
272 }
273
274 // Update the BufferLayerConsumer state.
275 mCurrentTexture = slot;
Alec Mouri39801c02018-10-10 10:44:47 -0700276 mCurrentTextureBuffer = nextTextureBuffer;
Chia-I Wuf1405182017-11-27 11:29:21 -0800277 mCurrentCrop = item.mCrop;
278 mCurrentTransform = item.mTransform;
279 mCurrentScalingMode = item.mScalingMode;
280 mCurrentTimestamp = item.mTimestamp;
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700281 mCurrentDataSpace = static_cast<ui::Dataspace>(item.mDataSpace);
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -0700282 mCurrentHdrMetadata = item.mHdrMetadata;
Chia-I Wuf1405182017-11-27 11:29:21 -0800283 mCurrentFence = item.mFence;
284 mCurrentFenceTime = item.mFenceTime;
285 mCurrentFrameNumber = item.mFrameNumber;
Chia-I Wu67dcc692017-11-27 14:51:06 -0800286 mCurrentTransformToDisplayInverse = item.mTransformToDisplayInverse;
287 mCurrentSurfaceDamage = item.mSurfaceDamage;
Chia-I Wu5c6e4632018-01-11 08:54:38 -0800288 mCurrentApi = item.mApi;
Chia-I Wuf1405182017-11-27 11:29:21 -0800289
290 computeCurrentTransformMatrixLocked();
291
292 return err;
293}
294
295status_t BufferLayerConsumer::bindTextureImageLocked() {
Dan Stoza84d619e2018-03-28 17:07:36 -0700296 ATRACE_CALL();
Alec Mouri39801c02018-10-10 10:44:47 -0700297
Alec Mourib5c4f352019-02-19 19:46:38 -0800298 if (mCurrentTextureBuffer != nullptr && mCurrentTextureBuffer->graphicBuffer() != nullptr) {
299 return mRE.bindExternalTextureBuffer(mTexName, mCurrentTextureBuffer->graphicBuffer(),
300 mCurrentFence);
301 }
302
303 return NO_INIT;
Chia-I Wuf1405182017-11-27 11:29:21 -0800304}
305
Chia-I Wuf1405182017-11-27 11:29:21 -0800306void BufferLayerConsumer::getTransformMatrix(float mtx[16]) {
307 Mutex::Autolock lock(mMutex);
308 memcpy(mtx, mCurrentTransformMatrix, sizeof(mCurrentTransformMatrix));
309}
310
311void BufferLayerConsumer::setFilteringEnabled(bool enabled) {
312 Mutex::Autolock lock(mMutex);
313 if (mAbandoned) {
314 BLC_LOGE("setFilteringEnabled: BufferLayerConsumer is abandoned!");
315 return;
316 }
317 bool needsRecompute = mFilteringEnabled != enabled;
318 mFilteringEnabled = enabled;
319
Alec Mouri39801c02018-10-10 10:44:47 -0700320 if (needsRecompute && mCurrentTextureBuffer == nullptr) {
321 BLC_LOGD("setFilteringEnabled called with mCurrentTextureBuffer == nullptr");
Chia-I Wuf1405182017-11-27 11:29:21 -0800322 }
323
Alec Mouri39801c02018-10-10 10:44:47 -0700324 if (needsRecompute && mCurrentTextureBuffer != nullptr) {
Chia-I Wuf1405182017-11-27 11:29:21 -0800325 computeCurrentTransformMatrixLocked();
326 }
327}
328
329void BufferLayerConsumer::computeCurrentTransformMatrixLocked() {
330 BLC_LOGV("computeCurrentTransformMatrixLocked");
Alec Mourib5c4f352019-02-19 19:46:38 -0800331 if (mCurrentTextureBuffer == nullptr || mCurrentTextureBuffer->graphicBuffer() == nullptr) {
Chia-I Wuf1405182017-11-27 11:29:21 -0800332 BLC_LOGD("computeCurrentTransformMatrixLocked: "
Alec Mouri39801c02018-10-10 10:44:47 -0700333 "mCurrentTextureBuffer is nullptr");
Chia-I Wuf1405182017-11-27 11:29:21 -0800334 }
Alec Mourib5c4f352019-02-19 19:46:38 -0800335 GLConsumer::computeTransformMatrix(mCurrentTransformMatrix,
336 mCurrentTextureBuffer == nullptr
337 ? nullptr
338 : mCurrentTextureBuffer->graphicBuffer(),
Alec Mouri2ee0dda2019-01-30 16:44:43 -0800339 getCurrentCropLocked(), mCurrentTransform,
340 mFilteringEnabled);
Chia-I Wuf1405182017-11-27 11:29:21 -0800341}
342
Chia-I Wuf1405182017-11-27 11:29:21 -0800343nsecs_t BufferLayerConsumer::getTimestamp() {
344 BLC_LOGV("getTimestamp");
345 Mutex::Autolock lock(mMutex);
346 return mCurrentTimestamp;
347}
348
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700349ui::Dataspace BufferLayerConsumer::getCurrentDataSpace() {
Chia-I Wuf1405182017-11-27 11:29:21 -0800350 BLC_LOGV("getCurrentDataSpace");
351 Mutex::Autolock lock(mMutex);
352 return mCurrentDataSpace;
353}
354
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -0700355const HdrMetadata& BufferLayerConsumer::getCurrentHdrMetadata() const {
356 BLC_LOGV("getCurrentHdrMetadata");
357 Mutex::Autolock lock(mMutex);
358 return mCurrentHdrMetadata;
359}
360
Chia-I Wuf1405182017-11-27 11:29:21 -0800361uint64_t BufferLayerConsumer::getFrameNumber() {
362 BLC_LOGV("getFrameNumber");
363 Mutex::Autolock lock(mMutex);
364 return mCurrentFrameNumber;
365}
366
Chia-I Wu67dcc692017-11-27 14:51:06 -0800367bool BufferLayerConsumer::getTransformToDisplayInverse() const {
368 Mutex::Autolock lock(mMutex);
369 return mCurrentTransformToDisplayInverse;
370}
371
372const Region& BufferLayerConsumer::getSurfaceDamage() const {
373 return mCurrentSurfaceDamage;
374}
375
Steven Thomas44685cb2019-07-23 16:19:31 -0700376void BufferLayerConsumer::mergeSurfaceDamage(const Region& damage) {
377 if (damage.bounds() == Rect::INVALID_RECT ||
378 mCurrentSurfaceDamage.bounds() == Rect::INVALID_RECT) {
379 mCurrentSurfaceDamage = Region::INVALID_REGION;
380 } else {
381 mCurrentSurfaceDamage |= damage;
382 }
383}
384
Chia-I Wu5c6e4632018-01-11 08:54:38 -0800385int BufferLayerConsumer::getCurrentApi() const {
386 Mutex::Autolock lock(mMutex);
387 return mCurrentApi;
388}
389
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000390sp<GraphicBuffer> BufferLayerConsumer::getCurrentBuffer(int* outSlot, sp<Fence>* outFence) const {
Chia-I Wuf1405182017-11-27 11:29:21 -0800391 Mutex::Autolock lock(mMutex);
392
393 if (outSlot != nullptr) {
394 *outSlot = mCurrentTexture;
395 }
396
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000397 if (outFence != nullptr) {
398 *outFence = mCurrentFence;
399 }
400
Alec Mourib5c4f352019-02-19 19:46:38 -0800401 return mCurrentTextureBuffer == nullptr ? nullptr : mCurrentTextureBuffer->graphicBuffer();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000402}
403
Chia-I Wuf1405182017-11-27 11:29:21 -0800404Rect BufferLayerConsumer::getCurrentCrop() const {
405 Mutex::Autolock lock(mMutex);
Alec Mouri2ee0dda2019-01-30 16:44:43 -0800406 return getCurrentCropLocked();
407}
408
409Rect BufferLayerConsumer::getCurrentCropLocked() const {
Chia-I Wuf1405182017-11-27 11:29:21 -0800410 return (mCurrentScalingMode == NATIVE_WINDOW_SCALING_MODE_SCALE_CROP)
Chia-I Wue1e11872017-12-01 09:21:59 -0800411 ? GLConsumer::scaleDownCrop(mCurrentCrop, mDefaultWidth, mDefaultHeight)
Chia-I Wuf1405182017-11-27 11:29:21 -0800412 : mCurrentCrop;
413}
414
415uint32_t BufferLayerConsumer::getCurrentTransform() const {
416 Mutex::Autolock lock(mMutex);
417 return mCurrentTransform;
418}
419
420uint32_t BufferLayerConsumer::getCurrentScalingMode() const {
421 Mutex::Autolock lock(mMutex);
422 return mCurrentScalingMode;
423}
424
425sp<Fence> BufferLayerConsumer::getCurrentFence() const {
426 Mutex::Autolock lock(mMutex);
427 return mCurrentFence;
428}
429
430std::shared_ptr<FenceTime> BufferLayerConsumer::getCurrentFenceTime() const {
431 Mutex::Autolock lock(mMutex);
432 return mCurrentFenceTime;
433}
434
Chia-I Wuf1405182017-11-27 11:29:21 -0800435void BufferLayerConsumer::freeBufferLocked(int slotIndex) {
436 BLC_LOGV("freeBufferLocked: slotIndex=%d", slotIndex);
Alec Mourid7b3a8b2019-03-21 11:44:18 -0700437 std::lock_guard<std::mutex> lock(mImagesMutex);
Chia-I Wuf1405182017-11-27 11:29:21 -0800438 if (slotIndex == mCurrentTexture) {
439 mCurrentTexture = BufferQueue::INVALID_BUFFER_SLOT;
440 }
Alec Mourib5c4f352019-02-19 19:46:38 -0800441 mImages[slotIndex] = nullptr;
Chia-I Wuf1405182017-11-27 11:29:21 -0800442 ConsumerBase::freeBufferLocked(slotIndex);
443}
444
Chia-I Wuc75c44d2017-11-27 14:32:57 -0800445void BufferLayerConsumer::onDisconnect() {
chaviwc65e8642020-03-18 15:35:48 -0700446 mLayer->onDisconnect();
Chia-I Wuc75c44d2017-11-27 14:32:57 -0800447}
448
Chia-I Wufd257f82017-11-27 14:51:06 -0800449void BufferLayerConsumer::onSidebandStreamChanged() {
450 FrameAvailableListener* unsafeFrameAvailableListener = nullptr;
451 {
452 Mutex::Autolock lock(mFrameAvailableMutex);
453 unsafeFrameAvailableListener = mFrameAvailableListener.unsafe_get();
454 }
455 sp<ContentsChangedListener> listener;
456 { // scope for the lock
457 Mutex::Autolock lock(mMutex);
458 ALOG_ASSERT(unsafeFrameAvailableListener == mContentsChangedListener.unsafe_get());
459 listener = mContentsChangedListener.promote();
460 }
461
Peiyong Lin566a3b42018-01-09 18:22:43 -0800462 if (listener != nullptr) {
Chia-I Wufd257f82017-11-27 14:51:06 -0800463 listener->onSidebandStreamChanged();
464 }
465}
466
Alec Mouri485e4c32019-04-30 18:24:05 -0700467void BufferLayerConsumer::onBufferAvailable(const BufferItem& item) {
468 if (item.mGraphicBuffer != nullptr && item.mSlot != BufferQueue::INVALID_BUFFER_SLOT) {
469 std::lock_guard<std::mutex> lock(mImagesMutex);
470 const std::shared_ptr<Image>& oldImage = mImages[item.mSlot];
471 if (oldImage == nullptr || oldImage->graphicBuffer() == nullptr ||
472 oldImage->graphicBuffer()->getId() != item.mGraphicBuffer->getId()) {
473 mImages[item.mSlot] = std::make_shared<Image>(item.mGraphicBuffer, mRE);
Alec Mourid7b3a8b2019-03-21 11:44:18 -0700474 }
Alec Mourid7b3a8b2019-03-21 11:44:18 -0700475 }
476}
477
Chia-I Wuc75c44d2017-11-27 14:32:57 -0800478void BufferLayerConsumer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
479 FrameEventHistoryDelta* outDelta) {
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}
506
Alec Mouri16a99402019-07-29 16:37:30 -0700507BufferLayerConsumer::Image::Image(const sp<GraphicBuffer>& graphicBuffer,
508 renderengine::RenderEngine& engine)
509 : mGraphicBuffer(graphicBuffer), mRE(engine) {
510 mRE.cacheExternalTextureBuffer(mGraphicBuffer);
511}
512
Alec Mourib5c4f352019-02-19 19:46:38 -0800513BufferLayerConsumer::Image::~Image() {
514 if (mGraphicBuffer != nullptr) {
Alec Mourif1907332019-03-07 13:05:26 -0800515 ALOGV("Destroying buffer: %" PRId64, mGraphicBuffer->getId());
Alec Mourib5c4f352019-02-19 19:46:38 -0800516 mRE.unbindExternalTextureBuffer(mGraphicBuffer->getId());
517 }
518}
Chia-I Wuf1405182017-11-27 11:29:21 -0800519}; // namespace android
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800520
521// TODO(b/129481165): remove the #pragma below and fix conversion issues
522#pragma clang diagnostic pop // ignored "-Wconversion"