blob: 8f5c9c740b5b338581acf32e289c13caded6587f [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"
23
Chia-I Wuda5c7302017-11-27 14:51:06 -080024#include "DispSync.h"
Chia-I Wuc75c44d2017-11-27 14:32:57 -080025#include "Layer.h"
Chia-I Wu9f2db772017-11-30 21:06:50 -080026#include "RenderEngine/RenderEngine.h"
Chia-I Wuc75c44d2017-11-27 14:32:57 -080027
Chia-I Wuf1405182017-11-27 11:29:21 -080028#include <inttypes.h>
29
Chia-I Wuf1405182017-11-27 11:29:21 -080030#include <cutils/compiler.h>
31
32#include <hardware/hardware.h>
33
34#include <math/mat4.h>
35
36#include <gui/BufferItem.h>
37#include <gui/GLConsumer.h>
38#include <gui/ISurfaceComposer.h>
39#include <gui/SurfaceComposerClient.h>
40
41#include <private/gui/ComposerService.h>
42#include <private/gui/SyncFeatures.h>
43
44#include <utils/Log.h>
45#include <utils/String8.h>
46#include <utils/Trace.h>
47
Chia-I Wuf1405182017-11-27 11:29:21 -080048namespace android {
49
50// Macros for including the BufferLayerConsumer name in log messages
51#define BLC_LOGV(x, ...) ALOGV("[%s] " x, mName.string(), ##__VA_ARGS__)
52#define BLC_LOGD(x, ...) ALOGD("[%s] " x, mName.string(), ##__VA_ARGS__)
53//#define BLC_LOGI(x, ...) ALOGI("[%s] " x, mName.string(), ##__VA_ARGS__)
54#define BLC_LOGW(x, ...) ALOGW("[%s] " x, mName.string(), ##__VA_ARGS__)
55#define BLC_LOGE(x, ...) ALOGE("[%s] " x, mName.string(), ##__VA_ARGS__)
56
Chia-I Wuf1405182017-11-27 11:29:21 -080057static const mat4 mtxIdentity;
58
Chia-I Wu9f2db772017-11-30 21:06:50 -080059BufferLayerConsumer::BufferLayerConsumer(const sp<IGraphicBufferConsumer>& bq, RenderEngine& engine,
60 uint32_t tex, Layer* layer)
Chia-I Wubd854bf2017-11-27 13:41:26 -080061 : ConsumerBase(bq, false),
Chia-I Wuf1405182017-11-27 11:29:21 -080062 mCurrentCrop(Rect::EMPTY_RECT),
63 mCurrentTransform(0),
64 mCurrentScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE),
65 mCurrentFence(Fence::NO_FENCE),
66 mCurrentTimestamp(0),
67 mCurrentDataSpace(HAL_DATASPACE_UNKNOWN),
68 mCurrentFrameNumber(0),
Chia-I Wu67dcc692017-11-27 14:51:06 -080069 mCurrentTransformToDisplayInverse(false),
70 mCurrentSurfaceDamage(),
Chia-I Wuf1405182017-11-27 11:29:21 -080071 mDefaultWidth(1),
72 mDefaultHeight(1),
73 mFilteringEnabled(true),
Chia-I Wu9f2db772017-11-30 21:06:50 -080074 mRE(engine),
Chia-I Wuf1405182017-11-27 11:29:21 -080075 mTexName(tex),
Chia-I Wuc75c44d2017-11-27 14:32:57 -080076 mLayer(layer),
Chia-I Wuc91077c2017-11-27 13:32:04 -080077 mCurrentTexture(BufferQueue::INVALID_BUFFER_SLOT) {
Chia-I Wuf1405182017-11-27 11:29:21 -080078 BLC_LOGV("BufferLayerConsumer");
79
80 memcpy(mCurrentTransformMatrix, mtxIdentity.asArray(), sizeof(mCurrentTransformMatrix));
81
82 mConsumer->setConsumerUsageBits(DEFAULT_USAGE_FLAGS);
83}
84
85status_t BufferLayerConsumer::setDefaultBufferSize(uint32_t w, uint32_t h) {
86 Mutex::Autolock lock(mMutex);
87 if (mAbandoned) {
88 BLC_LOGE("setDefaultBufferSize: BufferLayerConsumer is abandoned!");
89 return NO_INIT;
90 }
91 mDefaultWidth = w;
92 mDefaultHeight = h;
93 return mConsumer->setDefaultBufferSize(w, h);
94}
95
Chia-I Wufd257f82017-11-27 14:51:06 -080096void BufferLayerConsumer::setContentsChangedListener(const wp<ContentsChangedListener>& listener) {
97 setFrameAvailableListener(listener);
98 Mutex::Autolock lock(mMutex);
99 mContentsChangedListener = listener;
100}
101
Chia-I Wuda5c7302017-11-27 14:51:06 -0800102// We need to determine the time when a buffer acquired now will be
103// displayed. This can be calculated:
104// time when previous buffer's actual-present fence was signaled
105// + current display refresh rate * HWC latency
106// + a little extra padding
107//
108// Buffer producers are expected to set their desired presentation time
109// based on choreographer time stamps, which (coming from vsync events)
110// will be slightly later then the actual-present timing. If we get a
111// desired-present time that is unintentionally a hair after the next
112// vsync, we'll hold the frame when we really want to display it. We
113// need to take the offset between actual-present and reported-vsync
114// into account.
115//
116// If the system is configured without a DispSync phase offset for the app,
117// we also want to throw in a bit of padding to avoid edge cases where we
118// just barely miss. We want to do it here, not in every app. A major
119// source of trouble is the app's use of the display's ideal refresh time
120// (via Display.getRefreshRate()), which could be off of the actual refresh
121// by a few percent, with the error multiplied by the number of frames
122// between now and when the buffer should be displayed.
123//
124// If the refresh reported to the app has a phase offset, we shouldn't need
125// to tweak anything here.
126nsecs_t BufferLayerConsumer::computeExpectedPresent(const DispSync& dispSync) {
127 // The HWC doesn't currently have a way to report additional latency.
128 // Assume that whatever we submit now will appear right after the flip.
129 // For a smart panel this might be 1. This is expressed in frames,
130 // rather than time, because we expect to have a constant frame delay
131 // regardless of the refresh rate.
132 const uint32_t hwcLatency = 0;
133
134 // Ask DispSync when the next refresh will be (CLOCK_MONOTONIC).
135 const nsecs_t nextRefresh = dispSync.computeNextRefresh(hwcLatency);
136
137 // The DispSync time is already adjusted for the difference between
138 // vsync and reported-vsync (SurfaceFlinger::dispSyncPresentTimeOffset), so
139 // we don't need to factor that in here. Pad a little to avoid
140 // weird effects if apps might be requesting times right on the edge.
141 nsecs_t extraPadding = 0;
142 if (SurfaceFlinger::vsyncPhaseOffsetNs == 0) {
143 extraPadding = 1000000; // 1ms (6% of 60Hz)
144 }
145
146 return nextRefresh + extraPadding;
147}
148
149status_t BufferLayerConsumer::updateTexImage(BufferRejecter* rejecter, const DispSync& dispSync,
150 bool* autoRefresh, bool* queuedBuffer,
151 uint64_t maxFrameNumber) {
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.
Chia-I Wuda5c7302017-11-27 14:51:06 -0800202 err = updateAndReleaseLocked(item, &mPendingRelease);
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
Chia-I Wuda5c7302017-11-27 14:51:06 -0800207 if (!SyncFeatures::getInstance().useNativeFenceSync()) {
208 // 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
235 auto buffer = mPendingRelease.isPending ? mPendingRelease.graphicBuffer
236 : mCurrentTextureImage->graphicBuffer();
237 auto err = addReleaseFence(slot, buffer, fence);
238 if (err != OK) {
239 BLC_LOGE("setReleaseFence: failed to add the fence: %s (%d)", strerror(-err), err);
240 }
241}
242
243bool BufferLayerConsumer::releasePendingBuffer() {
244 if (!mPendingRelease.isPending) {
245 BLC_LOGV("Pending buffer already released");
246 return false;
247 }
248 BLC_LOGV("Releasing pending buffer");
249 Mutex::Autolock lock(mMutex);
250 status_t result =
251 releaseBufferLocked(mPendingRelease.currentTexture, mPendingRelease.graphicBuffer);
252 if (result < NO_ERROR) {
253 BLC_LOGE("releasePendingBuffer failed: %s (%d)", strerror(-result), result);
254 }
255 mPendingRelease = PendingRelease();
256 return true;
Chia-I Wuf1405182017-11-27 11:29:21 -0800257}
258
Chia-I Wu0cb75ac2017-11-27 15:56:04 -0800259sp<Fence> BufferLayerConsumer::getPrevFinalReleaseFence() const {
260 Mutex::Autolock lock(mMutex);
261 return ConsumerBase::mPrevFinalReleaseFence;
262}
263
Chia-I Wuf1405182017-11-27 11:29:21 -0800264status_t BufferLayerConsumer::acquireBufferLocked(BufferItem* item, nsecs_t presentWhen,
265 uint64_t maxFrameNumber) {
266 status_t err = ConsumerBase::acquireBufferLocked(item, presentWhen, maxFrameNumber);
267 if (err != NO_ERROR) {
268 return err;
269 }
270
271 // If item->mGraphicBuffer is not null, this buffer has not been acquired
272 // before, so any prior EglImage created is using a stale buffer. This
273 // replaces any old EglImage with a new one (using the new buffer).
Peiyong Lin566a3b42018-01-09 18:22:43 -0800274 if (item->mGraphicBuffer != nullptr) {
Chia-I Wu6748db42017-12-01 10:53:53 -0800275 mImages[item->mSlot] = new Image(item->mGraphicBuffer, mRE);
Chia-I Wuf1405182017-11-27 11:29:21 -0800276 }
277
278 return NO_ERROR;
279}
280
Chia-I Wu6748db42017-12-01 10:53:53 -0800281bool BufferLayerConsumer::canUseImageCrop(const Rect& crop) const {
282 // If the crop rect is not at the origin, we can't set the crop on the
283 // EGLImage because that's not allowed by the EGL_ANDROID_image_crop
284 // extension. In the future we can add a layered extension that
285 // removes this restriction if there is hardware that can support it.
286 return mRE.supportsImageCrop() && crop.left == 0 && crop.top == 0;
287}
288
Chia-I Wuf1405182017-11-27 11:29:21 -0800289status_t BufferLayerConsumer::updateAndReleaseLocked(const BufferItem& item,
290 PendingRelease* pendingRelease) {
291 status_t err = NO_ERROR;
292
293 int slot = item.mSlot;
294
Chia-I Wuf1405182017-11-27 11:29:21 -0800295 // Do whatever sync ops we need to do before releasing the old slot.
296 if (slot != mCurrentTexture) {
Chia-I Wu3498e3c2017-12-01 10:19:38 -0800297 err = syncForReleaseLocked();
Chia-I Wuf1405182017-11-27 11:29:21 -0800298 if (err != NO_ERROR) {
299 // Release the buffer we just acquired. It's not safe to
300 // release the old buffer, so instead we just drop the new frame.
301 // As we are still under lock since acquireBuffer, it is safe to
302 // release by slot.
Chia-I Wu6aff69b2017-11-27 14:08:48 -0800303 releaseBufferLocked(slot, mSlots[slot].mGraphicBuffer);
Chia-I Wuf1405182017-11-27 11:29:21 -0800304 return err;
305 }
306 }
307
308 BLC_LOGV("updateAndRelease: (slot=%d buf=%p) -> (slot=%d buf=%p)", mCurrentTexture,
Peiyong Lin566a3b42018-01-09 18:22:43 -0800309 mCurrentTextureImage != nullptr ? mCurrentTextureImage->graphicBufferHandle() : 0,
310 slot, mSlots[slot].mGraphicBuffer->handle);
Chia-I Wuf1405182017-11-27 11:29:21 -0800311
312 // Hang onto the pointer so that it isn't freed in the call to
313 // releaseBufferLocked() if we're in shared buffer mode and both buffers are
314 // the same.
Chia-I Wu6748db42017-12-01 10:53:53 -0800315 sp<Image> nextTextureImage = mImages[slot];
Chia-I Wuf1405182017-11-27 11:29:21 -0800316
317 // release old buffer
318 if (mCurrentTexture != BufferQueue::INVALID_BUFFER_SLOT) {
319 if (pendingRelease == nullptr) {
320 status_t status =
Chia-I Wu6aff69b2017-11-27 14:08:48 -0800321 releaseBufferLocked(mCurrentTexture, mCurrentTextureImage->graphicBuffer());
Chia-I Wuf1405182017-11-27 11:29:21 -0800322 if (status < NO_ERROR) {
323 BLC_LOGE("updateAndRelease: failed to release buffer: %s (%d)", strerror(-status),
324 status);
325 err = status;
326 // keep going, with error raised [?]
327 }
328 } else {
329 pendingRelease->currentTexture = mCurrentTexture;
330 pendingRelease->graphicBuffer = mCurrentTextureImage->graphicBuffer();
Chia-I Wuf1405182017-11-27 11:29:21 -0800331 pendingRelease->isPending = true;
332 }
333 }
334
335 // Update the BufferLayerConsumer state.
336 mCurrentTexture = slot;
337 mCurrentTextureImage = nextTextureImage;
338 mCurrentCrop = item.mCrop;
339 mCurrentTransform = item.mTransform;
340 mCurrentScalingMode = item.mScalingMode;
341 mCurrentTimestamp = item.mTimestamp;
342 mCurrentDataSpace = item.mDataSpace;
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -0700343 mCurrentHdrMetadata = item.mHdrMetadata;
Chia-I Wuf1405182017-11-27 11:29:21 -0800344 mCurrentFence = item.mFence;
345 mCurrentFenceTime = item.mFenceTime;
346 mCurrentFrameNumber = item.mFrameNumber;
Chia-I Wu67dcc692017-11-27 14:51:06 -0800347 mCurrentTransformToDisplayInverse = item.mTransformToDisplayInverse;
348 mCurrentSurfaceDamage = item.mSurfaceDamage;
Chia-I Wuf1405182017-11-27 11:29:21 -0800349
350 computeCurrentTransformMatrixLocked();
351
352 return err;
353}
354
355status_t BufferLayerConsumer::bindTextureImageLocked() {
Chia-I Wu9f2db772017-11-30 21:06:50 -0800356 mRE.checkErrors();
Chia-I Wuf1405182017-11-27 11:29:21 -0800357
Peiyong Lin566a3b42018-01-09 18:22:43 -0800358 if (mCurrentTexture == BufferQueue::INVALID_BUFFER_SLOT && mCurrentTextureImage == nullptr) {
Chia-I Wuf1405182017-11-27 11:29:21 -0800359 BLC_LOGE("bindTextureImage: no currently-bound texture");
Chia-I Wu6748db42017-12-01 10:53:53 -0800360 mRE.bindExternalTextureImage(mTexName, RE::Image(mRE));
Chia-I Wuf1405182017-11-27 11:29:21 -0800361 return NO_INIT;
362 }
363
Chia-I Wu6748db42017-12-01 10:53:53 -0800364 const Rect& imageCrop = canUseImageCrop(mCurrentCrop) ? mCurrentCrop : Rect::EMPTY_RECT;
365 status_t err = mCurrentTextureImage->createIfNeeded(imageCrop);
Chia-I Wuf1405182017-11-27 11:29:21 -0800366 if (err != NO_ERROR) {
Chia-I Wu6748db42017-12-01 10:53:53 -0800367 BLC_LOGW("bindTextureImage: can't create image on slot=%d", mCurrentTexture);
368 mRE.bindExternalTextureImage(mTexName, RE::Image(mRE));
Chia-I Wuf1405182017-11-27 11:29:21 -0800369 return UNKNOWN_ERROR;
370 }
Chia-I Wu6748db42017-12-01 10:53:53 -0800371
372 mRE.bindExternalTextureImage(mTexName, mCurrentTextureImage->image());
Chia-I Wuf1405182017-11-27 11:29:21 -0800373
Chia-I Wuf1405182017-11-27 11:29:21 -0800374 // Wait for the new buffer to be ready.
Chia-I Wu3498e3c2017-12-01 10:19:38 -0800375 return doFenceWaitLocked();
Chia-I Wuf1405182017-11-27 11:29:21 -0800376}
377
Chia-I Wu3498e3c2017-12-01 10:19:38 -0800378status_t BufferLayerConsumer::syncForReleaseLocked() {
Chia-I Wuf1405182017-11-27 11:29:21 -0800379 BLC_LOGV("syncForReleaseLocked");
380
381 if (mCurrentTexture != BufferQueue::INVALID_BUFFER_SLOT) {
382 if (SyncFeatures::getInstance().useNativeFenceSync()) {
Chia-I Wudf1badd2017-12-27 11:10:20 -0800383 base::unique_fd fenceFd = mRE.flush();
Chia-I Wu3498e3c2017-12-01 10:19:38 -0800384 if (fenceFd == -1) {
385 BLC_LOGE("syncForReleaseLocked: failed to flush RenderEngine");
Chia-I Wuf1405182017-11-27 11:29:21 -0800386 return UNKNOWN_ERROR;
387 }
Chia-I Wudf1badd2017-12-27 11:10:20 -0800388 sp<Fence> fence(new Fence(std::move(fenceFd)));
Chia-I Wuf1405182017-11-27 11:29:21 -0800389 status_t err = addReleaseFenceLocked(mCurrentTexture,
390 mCurrentTextureImage->graphicBuffer(), fence);
391 if (err != OK) {
392 BLC_LOGE("syncForReleaseLocked: error adding release fence: "
393 "%s (%d)",
394 strerror(-err), err);
395 return err;
396 }
Chia-I Wuf1405182017-11-27 11:29:21 -0800397 }
398 }
399
400 return OK;
401}
402
Chia-I Wuf1405182017-11-27 11:29:21 -0800403void BufferLayerConsumer::getTransformMatrix(float mtx[16]) {
404 Mutex::Autolock lock(mMutex);
405 memcpy(mtx, mCurrentTransformMatrix, sizeof(mCurrentTransformMatrix));
406}
407
408void BufferLayerConsumer::setFilteringEnabled(bool enabled) {
409 Mutex::Autolock lock(mMutex);
410 if (mAbandoned) {
411 BLC_LOGE("setFilteringEnabled: BufferLayerConsumer is abandoned!");
412 return;
413 }
414 bool needsRecompute = mFilteringEnabled != enabled;
415 mFilteringEnabled = enabled;
416
Peiyong Lin566a3b42018-01-09 18:22:43 -0800417 if (needsRecompute && mCurrentTextureImage == nullptr) {
418 BLC_LOGD("setFilteringEnabled called with mCurrentTextureImage == nullptr");
Chia-I Wuf1405182017-11-27 11:29:21 -0800419 }
420
Peiyong Lin566a3b42018-01-09 18:22:43 -0800421 if (needsRecompute && mCurrentTextureImage != nullptr) {
Chia-I Wuf1405182017-11-27 11:29:21 -0800422 computeCurrentTransformMatrixLocked();
423 }
424}
425
426void BufferLayerConsumer::computeCurrentTransformMatrixLocked() {
427 BLC_LOGV("computeCurrentTransformMatrixLocked");
428 sp<GraphicBuffer> buf =
429 (mCurrentTextureImage == nullptr) ? nullptr : mCurrentTextureImage->graphicBuffer();
430 if (buf == nullptr) {
431 BLC_LOGD("computeCurrentTransformMatrixLocked: "
Peiyong Lin566a3b42018-01-09 18:22:43 -0800432 "mCurrentTextureImage is nullptr");
Chia-I Wuf1405182017-11-27 11:29:21 -0800433 }
Chia-I Wu6748db42017-12-01 10:53:53 -0800434 const Rect& cropRect = canUseImageCrop(mCurrentCrop) ? Rect::EMPTY_RECT : mCurrentCrop;
435 GLConsumer::computeTransformMatrix(mCurrentTransformMatrix, buf, cropRect, mCurrentTransform,
436 mFilteringEnabled);
Chia-I Wuf1405182017-11-27 11:29:21 -0800437}
438
Chia-I Wuf1405182017-11-27 11:29:21 -0800439nsecs_t BufferLayerConsumer::getTimestamp() {
440 BLC_LOGV("getTimestamp");
441 Mutex::Autolock lock(mMutex);
442 return mCurrentTimestamp;
443}
444
445android_dataspace BufferLayerConsumer::getCurrentDataSpace() {
446 BLC_LOGV("getCurrentDataSpace");
447 Mutex::Autolock lock(mMutex);
448 return mCurrentDataSpace;
449}
450
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -0700451const HdrMetadata& BufferLayerConsumer::getCurrentHdrMetadata() const {
452 BLC_LOGV("getCurrentHdrMetadata");
453 Mutex::Autolock lock(mMutex);
454 return mCurrentHdrMetadata;
455}
456
Chia-I Wuf1405182017-11-27 11:29:21 -0800457uint64_t BufferLayerConsumer::getFrameNumber() {
458 BLC_LOGV("getFrameNumber");
459 Mutex::Autolock lock(mMutex);
460 return mCurrentFrameNumber;
461}
462
Chia-I Wu67dcc692017-11-27 14:51:06 -0800463bool BufferLayerConsumer::getTransformToDisplayInverse() const {
464 Mutex::Autolock lock(mMutex);
465 return mCurrentTransformToDisplayInverse;
466}
467
468const Region& BufferLayerConsumer::getSurfaceDamage() const {
469 return mCurrentSurfaceDamage;
470}
471
Chia-I Wuf1405182017-11-27 11:29:21 -0800472sp<GraphicBuffer> BufferLayerConsumer::getCurrentBuffer(int* outSlot) const {
473 Mutex::Autolock lock(mMutex);
474
475 if (outSlot != nullptr) {
476 *outSlot = mCurrentTexture;
477 }
478
Peiyong Lin566a3b42018-01-09 18:22:43 -0800479 return (mCurrentTextureImage == nullptr) ? nullptr : mCurrentTextureImage->graphicBuffer();
Chia-I Wuf1405182017-11-27 11:29:21 -0800480}
481
482Rect BufferLayerConsumer::getCurrentCrop() const {
483 Mutex::Autolock lock(mMutex);
484 return (mCurrentScalingMode == NATIVE_WINDOW_SCALING_MODE_SCALE_CROP)
Chia-I Wue1e11872017-12-01 09:21:59 -0800485 ? GLConsumer::scaleDownCrop(mCurrentCrop, mDefaultWidth, mDefaultHeight)
Chia-I Wuf1405182017-11-27 11:29:21 -0800486 : mCurrentCrop;
487}
488
489uint32_t BufferLayerConsumer::getCurrentTransform() const {
490 Mutex::Autolock lock(mMutex);
491 return mCurrentTransform;
492}
493
494uint32_t BufferLayerConsumer::getCurrentScalingMode() const {
495 Mutex::Autolock lock(mMutex);
496 return mCurrentScalingMode;
497}
498
499sp<Fence> BufferLayerConsumer::getCurrentFence() const {
500 Mutex::Autolock lock(mMutex);
501 return mCurrentFence;
502}
503
504std::shared_ptr<FenceTime> BufferLayerConsumer::getCurrentFenceTime() const {
505 Mutex::Autolock lock(mMutex);
506 return mCurrentFenceTime;
507}
508
Chia-I Wu3498e3c2017-12-01 10:19:38 -0800509status_t BufferLayerConsumer::doFenceWaitLocked() const {
Chia-I Wu9f2db772017-11-30 21:06:50 -0800510 if (!mRE.isCurrent()) {
Chia-I Wu3498e3c2017-12-01 10:19:38 -0800511 BLC_LOGE("doFenceWait: RenderEngine is not current");
Chia-I Wuf1405182017-11-27 11:29:21 -0800512 return INVALID_OPERATION;
513 }
514
515 if (mCurrentFence->isValid()) {
516 if (SyncFeatures::getInstance().useWaitSync()) {
Chia-I Wu3498e3c2017-12-01 10:19:38 -0800517 base::unique_fd fenceFd(mCurrentFence->dup());
Chia-I Wuf1405182017-11-27 11:29:21 -0800518 if (fenceFd == -1) {
Chia-I Wu3498e3c2017-12-01 10:19:38 -0800519 BLC_LOGE("doFenceWait: error dup'ing fence fd: %d", errno);
Chia-I Wuf1405182017-11-27 11:29:21 -0800520 return -errno;
521 }
Chia-I Wu3498e3c2017-12-01 10:19:38 -0800522 if (!mRE.waitFence(std::move(fenceFd))) {
523 BLC_LOGE("doFenceWait: failed to wait on fence fd");
Chia-I Wuf1405182017-11-27 11:29:21 -0800524 return UNKNOWN_ERROR;
525 }
526 } else {
Chia-I Wu3498e3c2017-12-01 10:19:38 -0800527 status_t err = mCurrentFence->waitForever("BufferLayerConsumer::doFenceWaitLocked");
Chia-I Wuf1405182017-11-27 11:29:21 -0800528 if (err != NO_ERROR) {
Chia-I Wu3498e3c2017-12-01 10:19:38 -0800529 BLC_LOGE("doFenceWait: error waiting for fence: %d", err);
Chia-I Wuf1405182017-11-27 11:29:21 -0800530 return err;
531 }
532 }
533 }
534
535 return NO_ERROR;
536}
537
538void BufferLayerConsumer::freeBufferLocked(int slotIndex) {
539 BLC_LOGV("freeBufferLocked: slotIndex=%d", slotIndex);
540 if (slotIndex == mCurrentTexture) {
541 mCurrentTexture = BufferQueue::INVALID_BUFFER_SLOT;
542 }
Chia-I Wu6748db42017-12-01 10:53:53 -0800543 mImages[slotIndex].clear();
Chia-I Wuf1405182017-11-27 11:29:21 -0800544 ConsumerBase::freeBufferLocked(slotIndex);
545}
546
Chia-I Wuc75c44d2017-11-27 14:32:57 -0800547void BufferLayerConsumer::onDisconnect() {
548 sp<Layer> l = mLayer.promote();
549 if (l.get()) {
550 l->onDisconnect();
551 }
552}
553
Chia-I Wufd257f82017-11-27 14:51:06 -0800554void BufferLayerConsumer::onSidebandStreamChanged() {
555 FrameAvailableListener* unsafeFrameAvailableListener = nullptr;
556 {
557 Mutex::Autolock lock(mFrameAvailableMutex);
558 unsafeFrameAvailableListener = mFrameAvailableListener.unsafe_get();
559 }
560 sp<ContentsChangedListener> listener;
561 { // scope for the lock
562 Mutex::Autolock lock(mMutex);
563 ALOG_ASSERT(unsafeFrameAvailableListener == mContentsChangedListener.unsafe_get());
564 listener = mContentsChangedListener.promote();
565 }
566
Peiyong Lin566a3b42018-01-09 18:22:43 -0800567 if (listener != nullptr) {
Chia-I Wufd257f82017-11-27 14:51:06 -0800568 listener->onSidebandStreamChanged();
569 }
570}
571
Chia-I Wuc75c44d2017-11-27 14:32:57 -0800572void BufferLayerConsumer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
573 FrameEventHistoryDelta* outDelta) {
574 sp<Layer> l = mLayer.promote();
575 if (l.get()) {
576 l->addAndGetFrameTimestamps(newTimestamps, outDelta);
577 }
578}
579
Chia-I Wuf1405182017-11-27 11:29:21 -0800580void BufferLayerConsumer::abandonLocked() {
581 BLC_LOGV("abandonLocked");
582 mCurrentTextureImage.clear();
583 ConsumerBase::abandonLocked();
584}
585
586status_t BufferLayerConsumer::setConsumerUsageBits(uint64_t usage) {
587 return ConsumerBase::setConsumerUsageBits(usage | DEFAULT_USAGE_FLAGS);
588}
589
590void BufferLayerConsumer::dumpLocked(String8& result, const char* prefix) const {
591 result.appendFormat("%smTexName=%d mCurrentTexture=%d\n"
592 "%smCurrentCrop=[%d,%d,%d,%d] mCurrentTransform=%#x\n",
593 prefix, mTexName, mCurrentTexture, prefix, mCurrentCrop.left,
594 mCurrentCrop.top, mCurrentCrop.right, mCurrentCrop.bottom,
595 mCurrentTransform);
596
597 ConsumerBase::dumpLocked(result, prefix);
598}
599
Chia-I Wu6748db42017-12-01 10:53:53 -0800600BufferLayerConsumer::Image::Image(sp<GraphicBuffer> graphicBuffer, const RenderEngine& engine)
Chia-I Wuf1405182017-11-27 11:29:21 -0800601 : mGraphicBuffer(graphicBuffer),
Chia-I Wu6748db42017-12-01 10:53:53 -0800602 mImage{engine},
603 mCreated(false),
604 mCropWidth(0),
605 mCropHeight(0) {}
Chia-I Wuf1405182017-11-27 11:29:21 -0800606
Chia-I Wu6748db42017-12-01 10:53:53 -0800607status_t BufferLayerConsumer::Image::createIfNeeded(const Rect& imageCrop) {
608 const int32_t cropWidth = imageCrop.width();
609 const int32_t cropHeight = imageCrop.height();
610 if (mCreated && mCropWidth == cropWidth && mCropHeight == cropHeight) {
611 return OK;
Chia-I Wuf1405182017-11-27 11:29:21 -0800612 }
613
Chia-I Wu6748db42017-12-01 10:53:53 -0800614 mCreated = mImage.setNativeWindowBuffer(mGraphicBuffer->getNativeBuffer(),
615 mGraphicBuffer->getUsage() & GRALLOC_USAGE_PROTECTED,
616 cropWidth, cropHeight);
617 if (mCreated) {
618 mCropWidth = cropWidth;
619 mCropHeight = cropHeight;
620 } else {
621 mCropWidth = 0;
622 mCropHeight = 0;
Chia-I Wuf1405182017-11-27 11:29:21 -0800623
Chia-I Wuf1405182017-11-27 11:29:21 -0800624 const sp<GraphicBuffer>& buffer = mGraphicBuffer;
625 ALOGE("Failed to create image. size=%ux%u st=%u usage=%#" PRIx64 " fmt=%d",
626 buffer->getWidth(), buffer->getHeight(), buffer->getStride(), buffer->getUsage(),
627 buffer->getPixelFormat());
Chia-I Wuf1405182017-11-27 11:29:21 -0800628 }
629
Chia-I Wu6748db42017-12-01 10:53:53 -0800630 return mCreated ? OK : UNKNOWN_ERROR;
Chia-I Wuf1405182017-11-27 11:29:21 -0800631}
632
633}; // namespace android