blob: dfd032671dbc288672df18f843dc902fe5bfedee [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 Wuf1405182017-11-27 11:29:21 -0800161 BufferItem item;
162
163 // Acquire the next buffer.
164 // In asynchronous mode the list is guaranteed to be one buffer
165 // deep, while in synchronous mode we use the oldest buffer.
Chia-I Wu9f2db772017-11-30 21:06:50 -0800166 status_t err = acquireBufferLocked(&item, computeExpectedPresent(dispSync), maxFrameNumber);
Chia-I Wuf1405182017-11-27 11:29:21 -0800167 if (err != NO_ERROR) {
168 if (err == BufferQueue::NO_BUFFER_AVAILABLE) {
Chia-I Wuf1405182017-11-27 11:29:21 -0800169 err = NO_ERROR;
Chia-I Wuda5c7302017-11-27 14:51:06 -0800170 } else if (err == BufferQueue::PRESENT_LATER) {
171 // return the error, without logging
Chia-I Wuf1405182017-11-27 11:29:21 -0800172 } else {
173 BLC_LOGE("updateTexImage: acquire failed: %s (%d)", strerror(-err), err);
174 }
175 return err;
176 }
177
Chia-I Wuda5c7302017-11-27 14:51:06 -0800178 if (autoRefresh) {
179 *autoRefresh = item.mAutoRefresh;
180 }
181
182 if (queuedBuffer) {
183 *queuedBuffer = item.mQueuedBuffer;
184 }
185
186 // We call the rejecter here, in case the caller has a reason to
187 // not accept this buffer. This is used by SurfaceFlinger to
188 // reject buffers which have the wrong size
189 int slot = item.mSlot;
190 if (rejecter && rejecter->reject(mSlots[slot].mGraphicBuffer, item)) {
191 releaseBufferLocked(slot, mSlots[slot].mGraphicBuffer);
192 return BUFFER_REJECTED;
193 }
194
Chia-I Wuf1405182017-11-27 11:29:21 -0800195 // Release the previous buffer.
Alec Mouri86770e52018-09-24 22:40:58 +0000196 err = updateAndReleaseLocked(item, &mPendingRelease, releaseFence);
Chia-I Wuf1405182017-11-27 11:29:21 -0800197 if (err != NO_ERROR) {
Chia-I Wuf1405182017-11-27 11:29:21 -0800198 return err;
199 }
200
Alec Mouri3221a892018-09-28 20:45:06 +0000201 if (!mRE.useNativeFenceSync()) {
Chia-I Wuda5c7302017-11-27 14:51:06 -0800202 // Bind the new buffer to the GL texture.
203 //
204 // Older devices require the "implicit" synchronization provided
205 // by glEGLImageTargetTexture2DOES, which this method calls. Newer
206 // devices will either call this in Layer::onDraw, or (if it's not
207 // a GL-composited layer) not at all.
208 err = bindTextureImageLocked();
209 }
210
211 return err;
212}
213
Chia-I Wu0cb75ac2017-11-27 15:56:04 -0800214status_t BufferLayerConsumer::bindTextureImage() {
215 Mutex::Autolock lock(mMutex);
216 return bindTextureImageLocked();
217}
218
Chia-I Wuda5c7302017-11-27 14:51:06 -0800219void BufferLayerConsumer::setReleaseFence(const sp<Fence>& fence) {
220 if (!fence->isValid()) {
221 return;
222 }
223
224 auto slot = mPendingRelease.isPending ? mPendingRelease.currentTexture : mCurrentTexture;
225 if (slot == BufferQueue::INVALID_BUFFER_SLOT) {
226 return;
227 }
228
Alec Mouri39801c02018-10-10 10:44:47 -0700229 auto buffer = mPendingRelease.isPending ? mPendingRelease.graphicBuffer : mCurrentTextureBuffer;
Chia-I Wuda5c7302017-11-27 14:51:06 -0800230 auto err = addReleaseFence(slot, buffer, fence);
231 if (err != OK) {
232 BLC_LOGE("setReleaseFence: failed to add the fence: %s (%d)", strerror(-err), err);
233 }
234}
235
236bool BufferLayerConsumer::releasePendingBuffer() {
237 if (!mPendingRelease.isPending) {
238 BLC_LOGV("Pending buffer already released");
239 return false;
240 }
241 BLC_LOGV("Releasing pending buffer");
242 Mutex::Autolock lock(mMutex);
243 status_t result =
244 releaseBufferLocked(mPendingRelease.currentTexture, mPendingRelease.graphicBuffer);
245 if (result < NO_ERROR) {
246 BLC_LOGE("releasePendingBuffer failed: %s (%d)", strerror(-result), result);
247 }
248 mPendingRelease = PendingRelease();
249 return true;
Chia-I Wuf1405182017-11-27 11:29:21 -0800250}
251
Chia-I Wu0cb75ac2017-11-27 15:56:04 -0800252sp<Fence> BufferLayerConsumer::getPrevFinalReleaseFence() const {
253 Mutex::Autolock lock(mMutex);
254 return ConsumerBase::mPrevFinalReleaseFence;
255}
256
Chia-I Wuf1405182017-11-27 11:29:21 -0800257status_t BufferLayerConsumer::acquireBufferLocked(BufferItem* item, nsecs_t presentWhen,
258 uint64_t maxFrameNumber) {
259 status_t err = ConsumerBase::acquireBufferLocked(item, presentWhen, maxFrameNumber);
260 if (err != NO_ERROR) {
261 return err;
262 }
263
264 // If item->mGraphicBuffer is not null, this buffer has not been acquired
Alec Mouri39801c02018-10-10 10:44:47 -0700265 // before.
Peiyong Lin566a3b42018-01-09 18:22:43 -0800266 if (item->mGraphicBuffer != nullptr) {
Alec Mouri39801c02018-10-10 10:44:47 -0700267 mImages[item->mSlot] = nullptr;
Chia-I Wuf1405182017-11-27 11:29:21 -0800268 }
269
270 return NO_ERROR;
271}
272
Chia-I Wuf1405182017-11-27 11:29:21 -0800273status_t BufferLayerConsumer::updateAndReleaseLocked(const BufferItem& item,
Alec Mouri86770e52018-09-24 22:40:58 +0000274 PendingRelease* pendingRelease,
275 const sp<Fence>& releaseFence) {
Chia-I Wuf1405182017-11-27 11:29:21 -0800276 status_t err = NO_ERROR;
277
278 int slot = item.mSlot;
279
Chia-I Wuf1405182017-11-27 11:29:21 -0800280 // Do whatever sync ops we need to do before releasing the old slot.
281 if (slot != mCurrentTexture) {
Alec Mouri86770e52018-09-24 22:40:58 +0000282 err = syncForReleaseLocked(releaseFence);
Chia-I Wuf1405182017-11-27 11:29:21 -0800283 if (err != NO_ERROR) {
284 // Release the buffer we just acquired. It's not safe to
285 // release the old buffer, so instead we just drop the new frame.
286 // As we are still under lock since acquireBuffer, it is safe to
287 // release by slot.
Chia-I Wu6aff69b2017-11-27 14:08:48 -0800288 releaseBufferLocked(slot, mSlots[slot].mGraphicBuffer);
Chia-I Wuf1405182017-11-27 11:29:21 -0800289 return err;
290 }
291 }
292
293 BLC_LOGV("updateAndRelease: (slot=%d buf=%p) -> (slot=%d buf=%p)", mCurrentTexture,
Alec Mouri39801c02018-10-10 10:44:47 -0700294 mCurrentTextureBuffer != nullptr ? mCurrentTextureBuffer->handle : 0, slot,
295 mSlots[slot].mGraphicBuffer->handle);
Chia-I Wuf1405182017-11-27 11:29:21 -0800296
297 // Hang onto the pointer so that it isn't freed in the call to
298 // releaseBufferLocked() if we're in shared buffer mode and both buffers are
299 // the same.
Alec Mouri39801c02018-10-10 10:44:47 -0700300 sp<GraphicBuffer> nextTextureBuffer = mSlots[slot].mGraphicBuffer;
Chia-I Wuf1405182017-11-27 11:29:21 -0800301
302 // release old buffer
303 if (mCurrentTexture != BufferQueue::INVALID_BUFFER_SLOT) {
304 if (pendingRelease == nullptr) {
Alec Mouri39801c02018-10-10 10:44:47 -0700305 status_t status = releaseBufferLocked(mCurrentTexture, mCurrentTextureBuffer);
Chia-I Wuf1405182017-11-27 11:29:21 -0800306 if (status < NO_ERROR) {
307 BLC_LOGE("updateAndRelease: failed to release buffer: %s (%d)", strerror(-status),
308 status);
309 err = status;
310 // keep going, with error raised [?]
311 }
312 } else {
313 pendingRelease->currentTexture = mCurrentTexture;
Alec Mouri39801c02018-10-10 10:44:47 -0700314 pendingRelease->graphicBuffer = mCurrentTextureBuffer;
Chia-I Wuf1405182017-11-27 11:29:21 -0800315 pendingRelease->isPending = true;
316 }
317 }
318
319 // Update the BufferLayerConsumer state.
320 mCurrentTexture = slot;
Alec Mouri39801c02018-10-10 10:44:47 -0700321 mCurrentTextureBuffer = nextTextureBuffer;
322 mCurrentTextureImageFreed = nullptr;
Chia-I Wuf1405182017-11-27 11:29:21 -0800323 mCurrentCrop = item.mCrop;
324 mCurrentTransform = item.mTransform;
325 mCurrentScalingMode = item.mScalingMode;
326 mCurrentTimestamp = item.mTimestamp;
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700327 mCurrentDataSpace = static_cast<ui::Dataspace>(item.mDataSpace);
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -0700328 mCurrentHdrMetadata = item.mHdrMetadata;
Chia-I Wuf1405182017-11-27 11:29:21 -0800329 mCurrentFence = item.mFence;
330 mCurrentFenceTime = item.mFenceTime;
331 mCurrentFrameNumber = item.mFrameNumber;
Chia-I Wu67dcc692017-11-27 14:51:06 -0800332 mCurrentTransformToDisplayInverse = item.mTransformToDisplayInverse;
333 mCurrentSurfaceDamage = item.mSurfaceDamage;
Chia-I Wu5c6e4632018-01-11 08:54:38 -0800334 mCurrentApi = item.mApi;
Chia-I Wuf1405182017-11-27 11:29:21 -0800335
336 computeCurrentTransformMatrixLocked();
337
338 return err;
339}
340
341status_t BufferLayerConsumer::bindTextureImageLocked() {
Dan Stoza84d619e2018-03-28 17:07:36 -0700342 ATRACE_CALL();
Alec Mouri39801c02018-10-10 10:44:47 -0700343
Chia-I Wu9f2db772017-11-30 21:06:50 -0800344 mRE.checkErrors();
Chia-I Wuf1405182017-11-27 11:29:21 -0800345
Alec Mouri39801c02018-10-10 10:44:47 -0700346 // It is possible for the current slot's buffer to be freed before a new one
347 // is bound. In that scenario we still want to bind the image.
348 if (mCurrentTexture == BufferQueue::INVALID_BUFFER_SLOT && mCurrentTextureBuffer == nullptr) {
Chia-I Wuf1405182017-11-27 11:29:21 -0800349 BLC_LOGE("bindTextureImage: no currently-bound texture");
Lloyd Pique144e1162017-12-20 16:44:52 -0800350 mRE.bindExternalTextureImage(mTexName, *mRE.createImage());
Chia-I Wuf1405182017-11-27 11:29:21 -0800351 return NO_INIT;
352 }
353
Alec Mouri39801c02018-10-10 10:44:47 -0700354 renderengine::Image* imageToRender;
355
356 // mCurrentTextureImageFreed is non-null iff mCurrentTexture ==
357 // BufferQueue::INVALID_BUFFER_SLOT, so we can omit that check.
358 if (mCurrentTextureImageFreed) {
359 imageToRender = mCurrentTextureImageFreed.get();
360 } else if (mImages[mCurrentTexture]) {
361 imageToRender = mImages[mCurrentTexture].get();
362 } else {
363 std::unique_ptr<renderengine::Image> image = mRE.createImage();
364 bool success = image->setNativeWindowBuffer(mCurrentTextureBuffer->getNativeBuffer(),
365 mCurrentTextureBuffer->getUsage() &
366 GRALLOC_USAGE_PROTECTED);
367 if (!success) {
368 BLC_LOGE("bindTextureImage: Failed to create image. size=%ux%u st=%u usage=%#" PRIx64
369 " fmt=%d",
370 mCurrentTextureBuffer->getWidth(), mCurrentTextureBuffer->getHeight(),
371 mCurrentTextureBuffer->getStride(), mCurrentTextureBuffer->getUsage(),
372 mCurrentTextureBuffer->getPixelFormat());
373 BLC_LOGW("bindTextureImage: can't create image on slot=%d", mCurrentTexture);
374 mRE.bindExternalTextureImage(mTexName, *image);
375 return UNKNOWN_ERROR;
376 }
377 imageToRender = image.get();
378 // Cache the image here so that we can reuse it.
379 mImages[mCurrentTexture] = std::move(image);
Chia-I Wuf1405182017-11-27 11:29:21 -0800380 }
Chia-I Wu6748db42017-12-01 10:53:53 -0800381
Alec Mouri39801c02018-10-10 10:44:47 -0700382 mRE.bindExternalTextureImage(mTexName, *imageToRender);
Chia-I Wuf1405182017-11-27 11:29:21 -0800383
Chia-I Wuf1405182017-11-27 11:29:21 -0800384 // Wait for the new buffer to be ready.
Chia-I Wu3498e3c2017-12-01 10:19:38 -0800385 return doFenceWaitLocked();
Chia-I Wuf1405182017-11-27 11:29:21 -0800386}
387
Alec Mouri86770e52018-09-24 22:40:58 +0000388status_t BufferLayerConsumer::syncForReleaseLocked(const sp<Fence>& releaseFence) {
Chia-I Wuf1405182017-11-27 11:29:21 -0800389 BLC_LOGV("syncForReleaseLocked");
390
391 if (mCurrentTexture != BufferQueue::INVALID_BUFFER_SLOT) {
Alec Mouri86770e52018-09-24 22:40:58 +0000392 if (mRE.useNativeFenceSync() && releaseFence != Fence::NO_FENCE) {
393 // TODO(alecmouri): fail further upstream if the fence is invalid
394 if (!releaseFence->isValid()) {
Chia-I Wu3498e3c2017-12-01 10:19:38 -0800395 BLC_LOGE("syncForReleaseLocked: failed to flush RenderEngine");
Chia-I Wuf1405182017-11-27 11:29:21 -0800396 return UNKNOWN_ERROR;
397 }
Alec Mouri86770e52018-09-24 22:40:58 +0000398 status_t err =
Alec Mouri39801c02018-10-10 10:44:47 -0700399 addReleaseFenceLocked(mCurrentTexture, mCurrentTextureBuffer, releaseFence);
Chia-I Wuf1405182017-11-27 11:29:21 -0800400 if (err != OK) {
401 BLC_LOGE("syncForReleaseLocked: error adding release fence: "
402 "%s (%d)",
403 strerror(-err), err);
404 return err;
405 }
Chia-I Wuf1405182017-11-27 11:29:21 -0800406 }
407 }
408
409 return OK;
410}
411
Chia-I Wuf1405182017-11-27 11:29:21 -0800412void BufferLayerConsumer::getTransformMatrix(float mtx[16]) {
413 Mutex::Autolock lock(mMutex);
414 memcpy(mtx, mCurrentTransformMatrix, sizeof(mCurrentTransformMatrix));
415}
416
417void BufferLayerConsumer::setFilteringEnabled(bool enabled) {
418 Mutex::Autolock lock(mMutex);
419 if (mAbandoned) {
420 BLC_LOGE("setFilteringEnabled: BufferLayerConsumer is abandoned!");
421 return;
422 }
423 bool needsRecompute = mFilteringEnabled != enabled;
424 mFilteringEnabled = enabled;
425
Alec Mouri39801c02018-10-10 10:44:47 -0700426 if (needsRecompute && mCurrentTextureBuffer == nullptr) {
427 BLC_LOGD("setFilteringEnabled called with mCurrentTextureBuffer == nullptr");
Chia-I Wuf1405182017-11-27 11:29:21 -0800428 }
429
Alec Mouri39801c02018-10-10 10:44:47 -0700430 if (needsRecompute && mCurrentTextureBuffer != nullptr) {
Chia-I Wuf1405182017-11-27 11:29:21 -0800431 computeCurrentTransformMatrixLocked();
432 }
433}
434
435void BufferLayerConsumer::computeCurrentTransformMatrixLocked() {
436 BLC_LOGV("computeCurrentTransformMatrixLocked");
Alec Mouri39801c02018-10-10 10:44:47 -0700437 if (mCurrentTextureBuffer == nullptr) {
Chia-I Wuf1405182017-11-27 11:29:21 -0800438 BLC_LOGD("computeCurrentTransformMatrixLocked: "
Alec Mouri39801c02018-10-10 10:44:47 -0700439 "mCurrentTextureBuffer is nullptr");
Chia-I Wuf1405182017-11-27 11:29:21 -0800440 }
Alec Mouri39801c02018-10-10 10:44:47 -0700441 GLConsumer::computeTransformMatrix(mCurrentTransformMatrix, mCurrentTextureBuffer, mCurrentCrop,
Krzysztof Kosiński7108c312018-06-27 19:12:54 -0700442 mCurrentTransform, mFilteringEnabled);
Chia-I Wuf1405182017-11-27 11:29:21 -0800443}
444
Chia-I Wuf1405182017-11-27 11:29:21 -0800445nsecs_t BufferLayerConsumer::getTimestamp() {
446 BLC_LOGV("getTimestamp");
447 Mutex::Autolock lock(mMutex);
448 return mCurrentTimestamp;
449}
450
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700451ui::Dataspace BufferLayerConsumer::getCurrentDataSpace() {
Chia-I Wuf1405182017-11-27 11:29:21 -0800452 BLC_LOGV("getCurrentDataSpace");
453 Mutex::Autolock lock(mMutex);
454 return mCurrentDataSpace;
455}
456
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -0700457const HdrMetadata& BufferLayerConsumer::getCurrentHdrMetadata() const {
458 BLC_LOGV("getCurrentHdrMetadata");
459 Mutex::Autolock lock(mMutex);
460 return mCurrentHdrMetadata;
461}
462
Chia-I Wuf1405182017-11-27 11:29:21 -0800463uint64_t BufferLayerConsumer::getFrameNumber() {
464 BLC_LOGV("getFrameNumber");
465 Mutex::Autolock lock(mMutex);
466 return mCurrentFrameNumber;
467}
468
Chia-I Wu67dcc692017-11-27 14:51:06 -0800469bool BufferLayerConsumer::getTransformToDisplayInverse() const {
470 Mutex::Autolock lock(mMutex);
471 return mCurrentTransformToDisplayInverse;
472}
473
474const Region& BufferLayerConsumer::getSurfaceDamage() const {
475 return mCurrentSurfaceDamage;
476}
477
Chia-I Wu5c6e4632018-01-11 08:54:38 -0800478int BufferLayerConsumer::getCurrentApi() const {
479 Mutex::Autolock lock(mMutex);
480 return mCurrentApi;
481}
482
Chia-I Wuf1405182017-11-27 11:29:21 -0800483sp<GraphicBuffer> BufferLayerConsumer::getCurrentBuffer(int* outSlot) const {
484 Mutex::Autolock lock(mMutex);
485
486 if (outSlot != nullptr) {
487 *outSlot = mCurrentTexture;
488 }
489
Alec Mouri39801c02018-10-10 10:44:47 -0700490 return mCurrentTextureBuffer;
Chia-I Wuf1405182017-11-27 11:29:21 -0800491}
492
493Rect BufferLayerConsumer::getCurrentCrop() const {
494 Mutex::Autolock lock(mMutex);
495 return (mCurrentScalingMode == NATIVE_WINDOW_SCALING_MODE_SCALE_CROP)
Chia-I Wue1e11872017-12-01 09:21:59 -0800496 ? GLConsumer::scaleDownCrop(mCurrentCrop, mDefaultWidth, mDefaultHeight)
Chia-I Wuf1405182017-11-27 11:29:21 -0800497 : mCurrentCrop;
498}
499
500uint32_t BufferLayerConsumer::getCurrentTransform() const {
501 Mutex::Autolock lock(mMutex);
502 return mCurrentTransform;
503}
504
505uint32_t BufferLayerConsumer::getCurrentScalingMode() const {
506 Mutex::Autolock lock(mMutex);
507 return mCurrentScalingMode;
508}
509
510sp<Fence> BufferLayerConsumer::getCurrentFence() const {
511 Mutex::Autolock lock(mMutex);
512 return mCurrentFence;
513}
514
515std::shared_ptr<FenceTime> BufferLayerConsumer::getCurrentFenceTime() const {
516 Mutex::Autolock lock(mMutex);
517 return mCurrentFenceTime;
518}
519
Chia-I Wu3498e3c2017-12-01 10:19:38 -0800520status_t BufferLayerConsumer::doFenceWaitLocked() const {
Chia-I Wuf1405182017-11-27 11:29:21 -0800521 if (mCurrentFence->isValid()) {
Lloyd Pique63f9dbf2018-06-21 13:13:07 -0700522 if (mRE.useWaitSync()) {
Chia-I Wu3498e3c2017-12-01 10:19:38 -0800523 base::unique_fd fenceFd(mCurrentFence->dup());
Chia-I Wuf1405182017-11-27 11:29:21 -0800524 if (fenceFd == -1) {
Chia-I Wu3498e3c2017-12-01 10:19:38 -0800525 BLC_LOGE("doFenceWait: error dup'ing fence fd: %d", errno);
Chia-I Wuf1405182017-11-27 11:29:21 -0800526 return -errno;
527 }
Chia-I Wu3498e3c2017-12-01 10:19:38 -0800528 if (!mRE.waitFence(std::move(fenceFd))) {
529 BLC_LOGE("doFenceWait: failed to wait on fence fd");
Chia-I Wuf1405182017-11-27 11:29:21 -0800530 return UNKNOWN_ERROR;
531 }
532 } else {
Chia-I Wu3498e3c2017-12-01 10:19:38 -0800533 status_t err = mCurrentFence->waitForever("BufferLayerConsumer::doFenceWaitLocked");
Chia-I Wuf1405182017-11-27 11:29:21 -0800534 if (err != NO_ERROR) {
Chia-I Wu3498e3c2017-12-01 10:19:38 -0800535 BLC_LOGE("doFenceWait: error waiting for fence: %d", err);
Chia-I Wuf1405182017-11-27 11:29:21 -0800536 return err;
537 }
538 }
539 }
540
541 return NO_ERROR;
542}
543
544void BufferLayerConsumer::freeBufferLocked(int slotIndex) {
545 BLC_LOGV("freeBufferLocked: slotIndex=%d", slotIndex);
546 if (slotIndex == mCurrentTexture) {
547 mCurrentTexture = BufferQueue::INVALID_BUFFER_SLOT;
Alec Mouri39801c02018-10-10 10:44:47 -0700548 mCurrentTextureImageFreed = std::move(mImages[slotIndex]);
549 } else {
550 mImages[slotIndex] = nullptr;
Chia-I Wuf1405182017-11-27 11:29:21 -0800551 }
Chia-I Wuf1405182017-11-27 11:29:21 -0800552 ConsumerBase::freeBufferLocked(slotIndex);
553}
554
Chia-I Wuc75c44d2017-11-27 14:32:57 -0800555void BufferLayerConsumer::onDisconnect() {
556 sp<Layer> l = mLayer.promote();
557 if (l.get()) {
558 l->onDisconnect();
559 }
560}
561
Chia-I Wufd257f82017-11-27 14:51:06 -0800562void BufferLayerConsumer::onSidebandStreamChanged() {
563 FrameAvailableListener* unsafeFrameAvailableListener = nullptr;
564 {
565 Mutex::Autolock lock(mFrameAvailableMutex);
566 unsafeFrameAvailableListener = mFrameAvailableListener.unsafe_get();
567 }
568 sp<ContentsChangedListener> listener;
569 { // scope for the lock
570 Mutex::Autolock lock(mMutex);
571 ALOG_ASSERT(unsafeFrameAvailableListener == mContentsChangedListener.unsafe_get());
572 listener = mContentsChangedListener.promote();
573 }
574
Peiyong Lin566a3b42018-01-09 18:22:43 -0800575 if (listener != nullptr) {
Chia-I Wufd257f82017-11-27 14:51:06 -0800576 listener->onSidebandStreamChanged();
577 }
578}
579
Chia-I Wuc75c44d2017-11-27 14:32:57 -0800580void BufferLayerConsumer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
581 FrameEventHistoryDelta* outDelta) {
582 sp<Layer> l = mLayer.promote();
583 if (l.get()) {
584 l->addAndGetFrameTimestamps(newTimestamps, outDelta);
585 }
586}
587
Chia-I Wuf1405182017-11-27 11:29:21 -0800588void BufferLayerConsumer::abandonLocked() {
589 BLC_LOGV("abandonLocked");
Alec Mouri39801c02018-10-10 10:44:47 -0700590 mCurrentTextureBuffer.clear();
Chia-I Wuf1405182017-11-27 11:29:21 -0800591 ConsumerBase::abandonLocked();
592}
593
594status_t BufferLayerConsumer::setConsumerUsageBits(uint64_t usage) {
595 return ConsumerBase::setConsumerUsageBits(usage | DEFAULT_USAGE_FLAGS);
596}
597
598void BufferLayerConsumer::dumpLocked(String8& result, const char* prefix) const {
599 result.appendFormat("%smTexName=%d mCurrentTexture=%d\n"
600 "%smCurrentCrop=[%d,%d,%d,%d] mCurrentTransform=%#x\n",
601 prefix, mTexName, mCurrentTexture, prefix, mCurrentCrop.left,
602 mCurrentCrop.top, mCurrentCrop.right, mCurrentCrop.bottom,
603 mCurrentTransform);
604
605 ConsumerBase::dumpLocked(result, prefix);
606}
607
Chia-I Wuf1405182017-11-27 11:29:21 -0800608}; // namespace android