blob: 8bd98b7df4c8f4400f202718f4bc332c141990df [file] [log] [blame]
Chia-I Wuf1405182017-11-27 11:29:21 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#undef LOG_TAG
18#define LOG_TAG "BufferLayerConsumer"
19#define ATRACE_TAG ATRACE_TAG_GRAPHICS
20//#define LOG_NDEBUG 0
21
22#include "BufferLayerConsumer.h"
Chia-I Wuc75c44d2017-11-27 14:32:57 -080023#include "Layer.h"
Ana Krulecfefcb582018-08-07 14:22:37 -070024#include "Scheduler/DispSync.h"
Chia-I Wuc75c44d2017-11-27 14:32:57 -080025
Chia-I Wuf1405182017-11-27 11:29:21 -080026#include <inttypes.h>
27
Chia-I Wuf1405182017-11-27 11:29:21 -080028#include <cutils/compiler.h>
29
30#include <hardware/hardware.h>
31
32#include <math/mat4.h>
33
34#include <gui/BufferItem.h>
35#include <gui/GLConsumer.h>
36#include <gui/ISurfaceComposer.h>
37#include <gui/SurfaceComposerClient.h>
Chia-I Wuf1405182017-11-27 11:29:21 -080038#include <private/gui/ComposerService.h>
Peiyong Lincbc184f2018-08-22 13:24:10 -070039#include <renderengine/Image.h>
40#include <renderengine/RenderEngine.h>
Chia-I Wuf1405182017-11-27 11:29:21 -080041#include <utils/Log.h>
42#include <utils/String8.h>
43#include <utils/Trace.h>
44
Chia-I Wuf1405182017-11-27 11:29:21 -080045namespace android {
46
47// Macros for including the BufferLayerConsumer name in log messages
48#define BLC_LOGV(x, ...) ALOGV("[%s] " x, mName.string(), ##__VA_ARGS__)
49#define BLC_LOGD(x, ...) ALOGD("[%s] " x, mName.string(), ##__VA_ARGS__)
50//#define BLC_LOGI(x, ...) ALOGI("[%s] " x, mName.string(), ##__VA_ARGS__)
51#define BLC_LOGW(x, ...) ALOGW("[%s] " x, mName.string(), ##__VA_ARGS__)
52#define BLC_LOGE(x, ...) ALOGE("[%s] " x, mName.string(), ##__VA_ARGS__)
53
Chia-I Wuf1405182017-11-27 11:29:21 -080054static const mat4 mtxIdentity;
55
Lloyd Pique144e1162017-12-20 16:44:52 -080056BufferLayerConsumer::BufferLayerConsumer(const sp<IGraphicBufferConsumer>& bq,
Peiyong Lin833074a2018-08-28 11:53:54 -070057 renderengine::RenderEngine& engine, uint32_t tex,
58 Layer* layer)
Chia-I Wubd854bf2017-11-27 13:41:26 -080059 : ConsumerBase(bq, false),
Chia-I Wuf1405182017-11-27 11:29:21 -080060 mCurrentCrop(Rect::EMPTY_RECT),
61 mCurrentTransform(0),
62 mCurrentScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE),
63 mCurrentFence(Fence::NO_FENCE),
64 mCurrentTimestamp(0),
Peiyong Lin34beb7a2018-03-28 11:57:12 -070065 mCurrentDataSpace(ui::Dataspace::UNKNOWN),
Chia-I Wuf1405182017-11-27 11:29:21 -080066 mCurrentFrameNumber(0),
Chia-I Wu67dcc692017-11-27 14:51:06 -080067 mCurrentTransformToDisplayInverse(false),
68 mCurrentSurfaceDamage(),
Chia-I Wu5c6e4632018-01-11 08:54:38 -080069 mCurrentApi(0),
Chia-I Wuf1405182017-11-27 11:29:21 -080070 mDefaultWidth(1),
71 mDefaultHeight(1),
72 mFilteringEnabled(true),
Chia-I Wu9f2db772017-11-30 21:06:50 -080073 mRE(engine),
Chia-I Wuf1405182017-11-27 11:29:21 -080074 mTexName(tex),
Chia-I Wuc75c44d2017-11-27 14:32:57 -080075 mLayer(layer),
Chia-I Wuc91077c2017-11-27 13:32:04 -080076 mCurrentTexture(BufferQueue::INVALID_BUFFER_SLOT) {
Chia-I Wuf1405182017-11-27 11:29:21 -080077 BLC_LOGV("BufferLayerConsumer");
78
79 memcpy(mCurrentTransformMatrix, mtxIdentity.asArray(), sizeof(mCurrentTransformMatrix));
80
81 mConsumer->setConsumerUsageBits(DEFAULT_USAGE_FLAGS);
82}
83
84status_t BufferLayerConsumer::setDefaultBufferSize(uint32_t w, uint32_t h) {
85 Mutex::Autolock lock(mMutex);
86 if (mAbandoned) {
87 BLC_LOGE("setDefaultBufferSize: BufferLayerConsumer is abandoned!");
88 return NO_INIT;
89 }
90 mDefaultWidth = w;
91 mDefaultHeight = h;
92 return mConsumer->setDefaultBufferSize(w, h);
93}
94
Chia-I Wufd257f82017-11-27 14:51:06 -080095void BufferLayerConsumer::setContentsChangedListener(const wp<ContentsChangedListener>& listener) {
96 setFrameAvailableListener(listener);
97 Mutex::Autolock lock(mMutex);
98 mContentsChangedListener = listener;
99}
100
Chia-I Wuda5c7302017-11-27 14:51:06 -0800101// We need to determine the time when a buffer acquired now will be
102// displayed. This can be calculated:
103// time when previous buffer's actual-present fence was signaled
104// + current display refresh rate * HWC latency
105// + a little extra padding
106//
107// Buffer producers are expected to set their desired presentation time
108// based on choreographer time stamps, which (coming from vsync events)
109// will be slightly later then the actual-present timing. If we get a
110// desired-present time that is unintentionally a hair after the next
111// vsync, we'll hold the frame when we really want to display it. We
112// need to take the offset between actual-present and reported-vsync
113// into account.
114//
115// If the system is configured without a DispSync phase offset for the app,
116// we also want to throw in a bit of padding to avoid edge cases where we
117// just barely miss. We want to do it here, not in every app. A major
118// source of trouble is the app's use of the display's ideal refresh time
119// (via Display.getRefreshRate()), which could be off of the actual refresh
120// by a few percent, with the error multiplied by the number of frames
121// between now and when the buffer should be displayed.
122//
123// If the refresh reported to the app has a phase offset, we shouldn't need
124// to tweak anything here.
125nsecs_t BufferLayerConsumer::computeExpectedPresent(const DispSync& dispSync) {
126 // The HWC doesn't currently have a way to report additional latency.
127 // Assume that whatever we submit now will appear right after the flip.
128 // For a smart panel this might be 1. This is expressed in frames,
129 // rather than time, because we expect to have a constant frame delay
130 // regardless of the refresh rate.
131 const uint32_t hwcLatency = 0;
132
133 // Ask DispSync when the next refresh will be (CLOCK_MONOTONIC).
134 const nsecs_t nextRefresh = dispSync.computeNextRefresh(hwcLatency);
135
136 // The DispSync time is already adjusted for the difference between
137 // vsync and reported-vsync (SurfaceFlinger::dispSyncPresentTimeOffset), so
138 // we don't need to factor that in here. Pad a little to avoid
139 // weird effects if apps might be requesting times right on the edge.
140 nsecs_t extraPadding = 0;
141 if (SurfaceFlinger::vsyncPhaseOffsetNs == 0) {
142 extraPadding = 1000000; // 1ms (6% of 60Hz)
143 }
144
145 return nextRefresh + extraPadding;
146}
147
148status_t BufferLayerConsumer::updateTexImage(BufferRejecter* rejecter, const DispSync& dispSync,
149 bool* autoRefresh, bool* queuedBuffer,
Alec Mouri86770e52018-09-24 22:40:58 +0000150 uint64_t maxFrameNumber,
151 const sp<Fence>& releaseFence) {
Chia-I Wuf1405182017-11-27 11:29:21 -0800152 ATRACE_CALL();
153 BLC_LOGV("updateTexImage");
154 Mutex::Autolock lock(mMutex);
155
156 if (mAbandoned) {
157 BLC_LOGE("updateTexImage: BufferLayerConsumer is abandoned!");
158 return NO_INIT;
159 }
160
Chia-I Wu9f2db772017-11-30 21:06:50 -0800161 // Make sure RenderEngine is current
162 if (!mRE.isCurrent()) {
163 BLC_LOGE("updateTexImage: RenderEngine is not current");
164 return INVALID_OPERATION;
Chia-I Wuf1405182017-11-27 11:29:21 -0800165 }
166
167 BufferItem item;
168
169 // Acquire the next buffer.
170 // In asynchronous mode the list is guaranteed to be one buffer
171 // deep, while in synchronous mode we use the oldest buffer.
Chia-I Wu9f2db772017-11-30 21:06:50 -0800172 status_t err = acquireBufferLocked(&item, computeExpectedPresent(dispSync), maxFrameNumber);
Chia-I Wuf1405182017-11-27 11:29:21 -0800173 if (err != NO_ERROR) {
174 if (err == BufferQueue::NO_BUFFER_AVAILABLE) {
Chia-I Wuf1405182017-11-27 11:29:21 -0800175 err = NO_ERROR;
Chia-I Wuda5c7302017-11-27 14:51:06 -0800176 } else if (err == BufferQueue::PRESENT_LATER) {
177 // return the error, without logging
Chia-I Wuf1405182017-11-27 11:29:21 -0800178 } else {
179 BLC_LOGE("updateTexImage: acquire failed: %s (%d)", strerror(-err), err);
180 }
181 return err;
182 }
183
Chia-I Wuda5c7302017-11-27 14:51:06 -0800184 if (autoRefresh) {
185 *autoRefresh = item.mAutoRefresh;
186 }
187
188 if (queuedBuffer) {
189 *queuedBuffer = item.mQueuedBuffer;
190 }
191
192 // We call the rejecter here, in case the caller has a reason to
193 // not accept this buffer. This is used by SurfaceFlinger to
194 // reject buffers which have the wrong size
195 int slot = item.mSlot;
196 if (rejecter && rejecter->reject(mSlots[slot].mGraphicBuffer, item)) {
197 releaseBufferLocked(slot, mSlots[slot].mGraphicBuffer);
198 return BUFFER_REJECTED;
199 }
200
Chia-I Wuf1405182017-11-27 11:29:21 -0800201 // Release the previous buffer.
Alec Mouri86770e52018-09-24 22:40:58 +0000202 err = updateAndReleaseLocked(item, &mPendingRelease, releaseFence);
Chia-I Wuf1405182017-11-27 11:29:21 -0800203 if (err != NO_ERROR) {
Chia-I Wuf1405182017-11-27 11:29:21 -0800204 return err;
205 }
206
Alec Mouri3221a892018-09-28 20:45:06 +0000207 if (!mRE.useNativeFenceSync()) {
Chia-I Wuda5c7302017-11-27 14:51:06 -0800208 // Bind the new buffer to the GL texture.
209 //
210 // Older devices require the "implicit" synchronization provided
211 // by glEGLImageTargetTexture2DOES, which this method calls. Newer
212 // devices will either call this in Layer::onDraw, or (if it's not
213 // a GL-composited layer) not at all.
214 err = bindTextureImageLocked();
215 }
216
217 return err;
218}
219
Chia-I Wu0cb75ac2017-11-27 15:56:04 -0800220status_t BufferLayerConsumer::bindTextureImage() {
221 Mutex::Autolock lock(mMutex);
222 return bindTextureImageLocked();
223}
224
Chia-I Wuda5c7302017-11-27 14:51:06 -0800225void BufferLayerConsumer::setReleaseFence(const sp<Fence>& fence) {
226 if (!fence->isValid()) {
227 return;
228 }
229
230 auto slot = mPendingRelease.isPending ? mPendingRelease.currentTexture : mCurrentTexture;
231 if (slot == BufferQueue::INVALID_BUFFER_SLOT) {
232 return;
233 }
234
Alec Mouri39801c02018-10-10 10:44:47 -0700235 auto buffer = mPendingRelease.isPending ? mPendingRelease.graphicBuffer : mCurrentTextureBuffer;
Chia-I Wuda5c7302017-11-27 14:51:06 -0800236 auto err = addReleaseFence(slot, buffer, fence);
237 if (err != OK) {
238 BLC_LOGE("setReleaseFence: failed to add the fence: %s (%d)", strerror(-err), err);
239 }
240}
241
242bool BufferLayerConsumer::releasePendingBuffer() {
243 if (!mPendingRelease.isPending) {
244 BLC_LOGV("Pending buffer already released");
245 return false;
246 }
247 BLC_LOGV("Releasing pending buffer");
248 Mutex::Autolock lock(mMutex);
249 status_t result =
250 releaseBufferLocked(mPendingRelease.currentTexture, mPendingRelease.graphicBuffer);
251 if (result < NO_ERROR) {
252 BLC_LOGE("releasePendingBuffer failed: %s (%d)", strerror(-result), result);
253 }
254 mPendingRelease = PendingRelease();
255 return true;
Chia-I Wuf1405182017-11-27 11:29:21 -0800256}
257
Chia-I Wu0cb75ac2017-11-27 15:56:04 -0800258sp<Fence> BufferLayerConsumer::getPrevFinalReleaseFence() const {
259 Mutex::Autolock lock(mMutex);
260 return ConsumerBase::mPrevFinalReleaseFence;
261}
262
Chia-I Wuf1405182017-11-27 11:29:21 -0800263status_t BufferLayerConsumer::acquireBufferLocked(BufferItem* item, nsecs_t presentWhen,
264 uint64_t maxFrameNumber) {
265 status_t err = ConsumerBase::acquireBufferLocked(item, presentWhen, maxFrameNumber);
266 if (err != NO_ERROR) {
267 return err;
268 }
269
270 // If item->mGraphicBuffer is not null, this buffer has not been acquired
Alec Mouri39801c02018-10-10 10:44:47 -0700271 // before.
Peiyong Lin566a3b42018-01-09 18:22:43 -0800272 if (item->mGraphicBuffer != nullptr) {
Alec Mouri39801c02018-10-10 10:44:47 -0700273 mImages[item->mSlot] = nullptr;
Chia-I Wuf1405182017-11-27 11:29:21 -0800274 }
275
276 return NO_ERROR;
277}
278
Chia-I Wuf1405182017-11-27 11:29:21 -0800279status_t BufferLayerConsumer::updateAndReleaseLocked(const BufferItem& item,
Alec Mouri86770e52018-09-24 22:40:58 +0000280 PendingRelease* pendingRelease,
281 const sp<Fence>& releaseFence) {
Chia-I Wuf1405182017-11-27 11:29:21 -0800282 status_t err = NO_ERROR;
283
284 int slot = item.mSlot;
285
Chia-I Wuf1405182017-11-27 11:29:21 -0800286 // Do whatever sync ops we need to do before releasing the old slot.
287 if (slot != mCurrentTexture) {
Alec Mouri86770e52018-09-24 22:40:58 +0000288 err = syncForReleaseLocked(releaseFence);
Chia-I Wuf1405182017-11-27 11:29:21 -0800289 if (err != NO_ERROR) {
290 // Release the buffer we just acquired. It's not safe to
291 // release the old buffer, so instead we just drop the new frame.
292 // As we are still under lock since acquireBuffer, it is safe to
293 // release by slot.
Chia-I Wu6aff69b2017-11-27 14:08:48 -0800294 releaseBufferLocked(slot, mSlots[slot].mGraphicBuffer);
Chia-I Wuf1405182017-11-27 11:29:21 -0800295 return err;
296 }
297 }
298
299 BLC_LOGV("updateAndRelease: (slot=%d buf=%p) -> (slot=%d buf=%p)", mCurrentTexture,
Alec Mouri39801c02018-10-10 10:44:47 -0700300 mCurrentTextureBuffer != nullptr ? mCurrentTextureBuffer->handle : 0, slot,
301 mSlots[slot].mGraphicBuffer->handle);
Chia-I Wuf1405182017-11-27 11:29:21 -0800302
303 // Hang onto the pointer so that it isn't freed in the call to
304 // releaseBufferLocked() if we're in shared buffer mode and both buffers are
305 // the same.
Alec Mouri39801c02018-10-10 10:44:47 -0700306 sp<GraphicBuffer> nextTextureBuffer = mSlots[slot].mGraphicBuffer;
Chia-I Wuf1405182017-11-27 11:29:21 -0800307
308 // release old buffer
309 if (mCurrentTexture != BufferQueue::INVALID_BUFFER_SLOT) {
310 if (pendingRelease == nullptr) {
Alec Mouri39801c02018-10-10 10:44:47 -0700311 status_t status = releaseBufferLocked(mCurrentTexture, mCurrentTextureBuffer);
Chia-I Wuf1405182017-11-27 11:29:21 -0800312 if (status < NO_ERROR) {
313 BLC_LOGE("updateAndRelease: failed to release buffer: %s (%d)", strerror(-status),
314 status);
315 err = status;
316 // keep going, with error raised [?]
317 }
318 } else {
319 pendingRelease->currentTexture = mCurrentTexture;
Alec Mouri39801c02018-10-10 10:44:47 -0700320 pendingRelease->graphicBuffer = mCurrentTextureBuffer;
Chia-I Wuf1405182017-11-27 11:29:21 -0800321 pendingRelease->isPending = true;
322 }
323 }
324
325 // Update the BufferLayerConsumer state.
326 mCurrentTexture = slot;
Alec Mouri39801c02018-10-10 10:44:47 -0700327 mCurrentTextureBuffer = nextTextureBuffer;
328 mCurrentTextureImageFreed = nullptr;
Chia-I Wuf1405182017-11-27 11:29:21 -0800329 mCurrentCrop = item.mCrop;
330 mCurrentTransform = item.mTransform;
331 mCurrentScalingMode = item.mScalingMode;
332 mCurrentTimestamp = item.mTimestamp;
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700333 mCurrentDataSpace = static_cast<ui::Dataspace>(item.mDataSpace);
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -0700334 mCurrentHdrMetadata = item.mHdrMetadata;
Chia-I Wuf1405182017-11-27 11:29:21 -0800335 mCurrentFence = item.mFence;
336 mCurrentFenceTime = item.mFenceTime;
337 mCurrentFrameNumber = item.mFrameNumber;
Chia-I Wu67dcc692017-11-27 14:51:06 -0800338 mCurrentTransformToDisplayInverse = item.mTransformToDisplayInverse;
339 mCurrentSurfaceDamage = item.mSurfaceDamage;
Chia-I Wu5c6e4632018-01-11 08:54:38 -0800340 mCurrentApi = item.mApi;
Chia-I Wuf1405182017-11-27 11:29:21 -0800341
342 computeCurrentTransformMatrixLocked();
343
344 return err;
345}
346
347status_t BufferLayerConsumer::bindTextureImageLocked() {
Dan Stoza84d619e2018-03-28 17:07:36 -0700348 ATRACE_CALL();
Alec Mouri39801c02018-10-10 10:44:47 -0700349
Chia-I Wu9f2db772017-11-30 21:06:50 -0800350 mRE.checkErrors();
Chia-I Wuf1405182017-11-27 11:29:21 -0800351
Alec Mouri39801c02018-10-10 10:44:47 -0700352 // It is possible for the current slot's buffer to be freed before a new one
353 // is bound. In that scenario we still want to bind the image.
354 if (mCurrentTexture == BufferQueue::INVALID_BUFFER_SLOT && mCurrentTextureBuffer == nullptr) {
Chia-I Wuf1405182017-11-27 11:29:21 -0800355 BLC_LOGE("bindTextureImage: no currently-bound texture");
Lloyd Pique144e1162017-12-20 16:44:52 -0800356 mRE.bindExternalTextureImage(mTexName, *mRE.createImage());
Chia-I Wuf1405182017-11-27 11:29:21 -0800357 return NO_INIT;
358 }
359
Alec Mouri39801c02018-10-10 10:44:47 -0700360 renderengine::Image* imageToRender;
361
362 // mCurrentTextureImageFreed is non-null iff mCurrentTexture ==
363 // BufferQueue::INVALID_BUFFER_SLOT, so we can omit that check.
364 if (mCurrentTextureImageFreed) {
365 imageToRender = mCurrentTextureImageFreed.get();
366 } else if (mImages[mCurrentTexture]) {
367 imageToRender = mImages[mCurrentTexture].get();
368 } else {
369 std::unique_ptr<renderengine::Image> image = mRE.createImage();
370 bool success = image->setNativeWindowBuffer(mCurrentTextureBuffer->getNativeBuffer(),
371 mCurrentTextureBuffer->getUsage() &
372 GRALLOC_USAGE_PROTECTED);
373 if (!success) {
374 BLC_LOGE("bindTextureImage: Failed to create image. size=%ux%u st=%u usage=%#" PRIx64
375 " fmt=%d",
376 mCurrentTextureBuffer->getWidth(), mCurrentTextureBuffer->getHeight(),
377 mCurrentTextureBuffer->getStride(), mCurrentTextureBuffer->getUsage(),
378 mCurrentTextureBuffer->getPixelFormat());
379 BLC_LOGW("bindTextureImage: can't create image on slot=%d", mCurrentTexture);
380 mRE.bindExternalTextureImage(mTexName, *image);
381 return UNKNOWN_ERROR;
382 }
383 imageToRender = image.get();
384 // Cache the image here so that we can reuse it.
385 mImages[mCurrentTexture] = std::move(image);
Chia-I Wuf1405182017-11-27 11:29:21 -0800386 }
Chia-I Wu6748db42017-12-01 10:53:53 -0800387
Alec Mouri39801c02018-10-10 10:44:47 -0700388 mRE.bindExternalTextureImage(mTexName, *imageToRender);
Chia-I Wuf1405182017-11-27 11:29:21 -0800389
Chia-I Wuf1405182017-11-27 11:29:21 -0800390 // Wait for the new buffer to be ready.
Chia-I Wu3498e3c2017-12-01 10:19:38 -0800391 return doFenceWaitLocked();
Chia-I Wuf1405182017-11-27 11:29:21 -0800392}
393
Alec Mouri86770e52018-09-24 22:40:58 +0000394status_t BufferLayerConsumer::syncForReleaseLocked(const sp<Fence>& releaseFence) {
Chia-I Wuf1405182017-11-27 11:29:21 -0800395 BLC_LOGV("syncForReleaseLocked");
396
397 if (mCurrentTexture != BufferQueue::INVALID_BUFFER_SLOT) {
Alec Mouri86770e52018-09-24 22:40:58 +0000398 if (mRE.useNativeFenceSync() && releaseFence != Fence::NO_FENCE) {
399 // TODO(alecmouri): fail further upstream if the fence is invalid
400 if (!releaseFence->isValid()) {
Chia-I Wu3498e3c2017-12-01 10:19:38 -0800401 BLC_LOGE("syncForReleaseLocked: failed to flush RenderEngine");
Chia-I Wuf1405182017-11-27 11:29:21 -0800402 return UNKNOWN_ERROR;
403 }
Alec Mouri86770e52018-09-24 22:40:58 +0000404 status_t err =
Alec Mouri39801c02018-10-10 10:44:47 -0700405 addReleaseFenceLocked(mCurrentTexture, mCurrentTextureBuffer, releaseFence);
Chia-I Wuf1405182017-11-27 11:29:21 -0800406 if (err != OK) {
407 BLC_LOGE("syncForReleaseLocked: error adding release fence: "
408 "%s (%d)",
409 strerror(-err), err);
410 return err;
411 }
Chia-I Wuf1405182017-11-27 11:29:21 -0800412 }
413 }
414
415 return OK;
416}
417
Chia-I Wuf1405182017-11-27 11:29:21 -0800418void BufferLayerConsumer::getTransformMatrix(float mtx[16]) {
419 Mutex::Autolock lock(mMutex);
420 memcpy(mtx, mCurrentTransformMatrix, sizeof(mCurrentTransformMatrix));
421}
422
423void BufferLayerConsumer::setFilteringEnabled(bool enabled) {
424 Mutex::Autolock lock(mMutex);
425 if (mAbandoned) {
426 BLC_LOGE("setFilteringEnabled: BufferLayerConsumer is abandoned!");
427 return;
428 }
429 bool needsRecompute = mFilteringEnabled != enabled;
430 mFilteringEnabled = enabled;
431
Alec Mouri39801c02018-10-10 10:44:47 -0700432 if (needsRecompute && mCurrentTextureBuffer == nullptr) {
433 BLC_LOGD("setFilteringEnabled called with mCurrentTextureBuffer == nullptr");
Chia-I Wuf1405182017-11-27 11:29:21 -0800434 }
435
Alec Mouri39801c02018-10-10 10:44:47 -0700436 if (needsRecompute && mCurrentTextureBuffer != nullptr) {
Chia-I Wuf1405182017-11-27 11:29:21 -0800437 computeCurrentTransformMatrixLocked();
438 }
439}
440
441void BufferLayerConsumer::computeCurrentTransformMatrixLocked() {
442 BLC_LOGV("computeCurrentTransformMatrixLocked");
Alec Mouri39801c02018-10-10 10:44:47 -0700443 if (mCurrentTextureBuffer == nullptr) {
Chia-I Wuf1405182017-11-27 11:29:21 -0800444 BLC_LOGD("computeCurrentTransformMatrixLocked: "
Alec Mouri39801c02018-10-10 10:44:47 -0700445 "mCurrentTextureBuffer is nullptr");
Chia-I Wuf1405182017-11-27 11:29:21 -0800446 }
Alec Mouri39801c02018-10-10 10:44:47 -0700447 GLConsumer::computeTransformMatrix(mCurrentTransformMatrix, mCurrentTextureBuffer, mCurrentCrop,
Krzysztof Kosiński7108c312018-06-27 19:12:54 -0700448 mCurrentTransform, mFilteringEnabled);
Chia-I Wuf1405182017-11-27 11:29:21 -0800449}
450
Chia-I Wuf1405182017-11-27 11:29:21 -0800451nsecs_t BufferLayerConsumer::getTimestamp() {
452 BLC_LOGV("getTimestamp");
453 Mutex::Autolock lock(mMutex);
454 return mCurrentTimestamp;
455}
456
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700457ui::Dataspace BufferLayerConsumer::getCurrentDataSpace() {
Chia-I Wuf1405182017-11-27 11:29:21 -0800458 BLC_LOGV("getCurrentDataSpace");
459 Mutex::Autolock lock(mMutex);
460 return mCurrentDataSpace;
461}
462
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -0700463const HdrMetadata& BufferLayerConsumer::getCurrentHdrMetadata() const {
464 BLC_LOGV("getCurrentHdrMetadata");
465 Mutex::Autolock lock(mMutex);
466 return mCurrentHdrMetadata;
467}
468
Chia-I Wuf1405182017-11-27 11:29:21 -0800469uint64_t BufferLayerConsumer::getFrameNumber() {
470 BLC_LOGV("getFrameNumber");
471 Mutex::Autolock lock(mMutex);
472 return mCurrentFrameNumber;
473}
474
Chia-I Wu67dcc692017-11-27 14:51:06 -0800475bool BufferLayerConsumer::getTransformToDisplayInverse() const {
476 Mutex::Autolock lock(mMutex);
477 return mCurrentTransformToDisplayInverse;
478}
479
480const Region& BufferLayerConsumer::getSurfaceDamage() const {
481 return mCurrentSurfaceDamage;
482}
483
Chia-I Wu5c6e4632018-01-11 08:54:38 -0800484int BufferLayerConsumer::getCurrentApi() const {
485 Mutex::Autolock lock(mMutex);
486 return mCurrentApi;
487}
488
Chia-I Wuf1405182017-11-27 11:29:21 -0800489sp<GraphicBuffer> BufferLayerConsumer::getCurrentBuffer(int* outSlot) const {
490 Mutex::Autolock lock(mMutex);
491
492 if (outSlot != nullptr) {
493 *outSlot = mCurrentTexture;
494 }
495
Alec Mouri39801c02018-10-10 10:44:47 -0700496 return mCurrentTextureBuffer;
Chia-I Wuf1405182017-11-27 11:29:21 -0800497}
498
499Rect BufferLayerConsumer::getCurrentCrop() const {
500 Mutex::Autolock lock(mMutex);
501 return (mCurrentScalingMode == NATIVE_WINDOW_SCALING_MODE_SCALE_CROP)
Chia-I Wue1e11872017-12-01 09:21:59 -0800502 ? GLConsumer::scaleDownCrop(mCurrentCrop, mDefaultWidth, mDefaultHeight)
Chia-I Wuf1405182017-11-27 11:29:21 -0800503 : mCurrentCrop;
504}
505
506uint32_t BufferLayerConsumer::getCurrentTransform() const {
507 Mutex::Autolock lock(mMutex);
508 return mCurrentTransform;
509}
510
511uint32_t BufferLayerConsumer::getCurrentScalingMode() const {
512 Mutex::Autolock lock(mMutex);
513 return mCurrentScalingMode;
514}
515
516sp<Fence> BufferLayerConsumer::getCurrentFence() const {
517 Mutex::Autolock lock(mMutex);
518 return mCurrentFence;
519}
520
521std::shared_ptr<FenceTime> BufferLayerConsumer::getCurrentFenceTime() const {
522 Mutex::Autolock lock(mMutex);
523 return mCurrentFenceTime;
524}
525
Chia-I Wu3498e3c2017-12-01 10:19:38 -0800526status_t BufferLayerConsumer::doFenceWaitLocked() const {
Chia-I Wu9f2db772017-11-30 21:06:50 -0800527 if (!mRE.isCurrent()) {
Chia-I Wu3498e3c2017-12-01 10:19:38 -0800528 BLC_LOGE("doFenceWait: RenderEngine is not current");
Chia-I Wuf1405182017-11-27 11:29:21 -0800529 return INVALID_OPERATION;
530 }
Chia-I Wuf1405182017-11-27 11:29:21 -0800531 if (mCurrentFence->isValid()) {
Lloyd Pique63f9dbf2018-06-21 13:13:07 -0700532 if (mRE.useWaitSync()) {
Chia-I Wu3498e3c2017-12-01 10:19:38 -0800533 base::unique_fd fenceFd(mCurrentFence->dup());
Chia-I Wuf1405182017-11-27 11:29:21 -0800534 if (fenceFd == -1) {
Chia-I Wu3498e3c2017-12-01 10:19:38 -0800535 BLC_LOGE("doFenceWait: error dup'ing fence fd: %d", errno);
Chia-I Wuf1405182017-11-27 11:29:21 -0800536 return -errno;
537 }
Chia-I Wu3498e3c2017-12-01 10:19:38 -0800538 if (!mRE.waitFence(std::move(fenceFd))) {
539 BLC_LOGE("doFenceWait: failed to wait on fence fd");
Chia-I Wuf1405182017-11-27 11:29:21 -0800540 return UNKNOWN_ERROR;
541 }
542 } else {
Chia-I Wu3498e3c2017-12-01 10:19:38 -0800543 status_t err = mCurrentFence->waitForever("BufferLayerConsumer::doFenceWaitLocked");
Chia-I Wuf1405182017-11-27 11:29:21 -0800544 if (err != NO_ERROR) {
Chia-I Wu3498e3c2017-12-01 10:19:38 -0800545 BLC_LOGE("doFenceWait: error waiting for fence: %d", err);
Chia-I Wuf1405182017-11-27 11:29:21 -0800546 return err;
547 }
548 }
549 }
550
551 return NO_ERROR;
552}
553
554void BufferLayerConsumer::freeBufferLocked(int slotIndex) {
555 BLC_LOGV("freeBufferLocked: slotIndex=%d", slotIndex);
556 if (slotIndex == mCurrentTexture) {
557 mCurrentTexture = BufferQueue::INVALID_BUFFER_SLOT;
Alec Mouri39801c02018-10-10 10:44:47 -0700558 mCurrentTextureImageFreed = std::move(mImages[slotIndex]);
559 } else {
560 mImages[slotIndex] = nullptr;
Chia-I Wuf1405182017-11-27 11:29:21 -0800561 }
Chia-I Wuf1405182017-11-27 11:29:21 -0800562 ConsumerBase::freeBufferLocked(slotIndex);
563}
564
Chia-I Wuc75c44d2017-11-27 14:32:57 -0800565void BufferLayerConsumer::onDisconnect() {
566 sp<Layer> l = mLayer.promote();
567 if (l.get()) {
568 l->onDisconnect();
569 }
570}
571
Chia-I Wufd257f82017-11-27 14:51:06 -0800572void BufferLayerConsumer::onSidebandStreamChanged() {
573 FrameAvailableListener* unsafeFrameAvailableListener = nullptr;
574 {
575 Mutex::Autolock lock(mFrameAvailableMutex);
576 unsafeFrameAvailableListener = mFrameAvailableListener.unsafe_get();
577 }
578 sp<ContentsChangedListener> listener;
579 { // scope for the lock
580 Mutex::Autolock lock(mMutex);
581 ALOG_ASSERT(unsafeFrameAvailableListener == mContentsChangedListener.unsafe_get());
582 listener = mContentsChangedListener.promote();
583 }
584
Peiyong Lin566a3b42018-01-09 18:22:43 -0800585 if (listener != nullptr) {
Chia-I Wufd257f82017-11-27 14:51:06 -0800586 listener->onSidebandStreamChanged();
587 }
588}
589
Chia-I Wuc75c44d2017-11-27 14:32:57 -0800590void BufferLayerConsumer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
591 FrameEventHistoryDelta* outDelta) {
592 sp<Layer> l = mLayer.promote();
593 if (l.get()) {
594 l->addAndGetFrameTimestamps(newTimestamps, outDelta);
595 }
596}
597
Chia-I Wuf1405182017-11-27 11:29:21 -0800598void BufferLayerConsumer::abandonLocked() {
599 BLC_LOGV("abandonLocked");
Alec Mouri39801c02018-10-10 10:44:47 -0700600 mCurrentTextureBuffer.clear();
Chia-I Wuf1405182017-11-27 11:29:21 -0800601 ConsumerBase::abandonLocked();
602}
603
604status_t BufferLayerConsumer::setConsumerUsageBits(uint64_t usage) {
605 return ConsumerBase::setConsumerUsageBits(usage | DEFAULT_USAGE_FLAGS);
606}
607
608void BufferLayerConsumer::dumpLocked(String8& result, const char* prefix) const {
609 result.appendFormat("%smTexName=%d mCurrentTexture=%d\n"
610 "%smCurrentCrop=[%d,%d,%d,%d] mCurrentTransform=%#x\n",
611 prefix, mTexName, mCurrentTexture, prefix, mCurrentCrop.left,
612 mCurrentCrop.top, mCurrentCrop.right, mCurrentCrop.bottom,
613 mCurrentTransform);
614
615 ConsumerBase::dumpLocked(result, prefix);
616}
617
Chia-I Wuf1405182017-11-27 11:29:21 -0800618}; // namespace android