blob: 87333d0ffd19fd3e5d42e328779ed2cf23423f80 [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"
Lloyd Pique144e1162017-12-20 16:44:52 -080026#include "RenderEngine/Image.h"
Chia-I Wu9f2db772017-11-30 21:06:50 -080027#include "RenderEngine/RenderEngine.h"
Chia-I Wuc75c44d2017-11-27 14:32:57 -080028
Chia-I Wuf1405182017-11-27 11:29:21 -080029#include <inttypes.h>
30
Chia-I Wuf1405182017-11-27 11:29:21 -080031#include <cutils/compiler.h>
32
33#include <hardware/hardware.h>
34
35#include <math/mat4.h>
36
37#include <gui/BufferItem.h>
38#include <gui/GLConsumer.h>
39#include <gui/ISurfaceComposer.h>
40#include <gui/SurfaceComposerClient.h>
41
42#include <private/gui/ComposerService.h>
43#include <private/gui/SyncFeatures.h>
44
45#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,
61 RE::RenderEngine& engine, uint32_t tex, Layer* layer)
Chia-I Wubd854bf2017-11-27 13:41:26 -080062 : ConsumerBase(bq, false),
Chia-I Wuf1405182017-11-27 11:29:21 -080063 mCurrentCrop(Rect::EMPTY_RECT),
64 mCurrentTransform(0),
65 mCurrentScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE),
66 mCurrentFence(Fence::NO_FENCE),
67 mCurrentTimestamp(0),
Peiyong Lin34beb7a2018-03-28 11:57:12 -070068 mCurrentDataSpace(ui::Dataspace::UNKNOWN),
Chia-I Wuf1405182017-11-27 11:29:21 -080069 mCurrentFrameNumber(0),
Chia-I Wu67dcc692017-11-27 14:51:06 -080070 mCurrentTransformToDisplayInverse(false),
71 mCurrentSurfaceDamage(),
Chia-I Wu5c6e4632018-01-11 08:54:38 -080072 mCurrentApi(0),
Chia-I Wuf1405182017-11-27 11:29:21 -080073 mDefaultWidth(1),
74 mDefaultHeight(1),
75 mFilteringEnabled(true),
Chia-I Wu9f2db772017-11-30 21:06:50 -080076 mRE(engine),
Chia-I Wuf1405182017-11-27 11:29:21 -080077 mTexName(tex),
Chia-I Wuc75c44d2017-11-27 14:32:57 -080078 mLayer(layer),
Chia-I Wuc91077c2017-11-27 13:32:04 -080079 mCurrentTexture(BufferQueue::INVALID_BUFFER_SLOT) {
Chia-I Wuf1405182017-11-27 11:29:21 -080080 BLC_LOGV("BufferLayerConsumer");
81
82 memcpy(mCurrentTransformMatrix, mtxIdentity.asArray(), sizeof(mCurrentTransformMatrix));
83
84 mConsumer->setConsumerUsageBits(DEFAULT_USAGE_FLAGS);
85}
86
87status_t BufferLayerConsumer::setDefaultBufferSize(uint32_t w, uint32_t h) {
88 Mutex::Autolock lock(mMutex);
89 if (mAbandoned) {
90 BLC_LOGE("setDefaultBufferSize: BufferLayerConsumer is abandoned!");
91 return NO_INIT;
92 }
93 mDefaultWidth = w;
94 mDefaultHeight = h;
95 return mConsumer->setDefaultBufferSize(w, h);
96}
97
Chia-I Wufd257f82017-11-27 14:51:06 -080098void BufferLayerConsumer::setContentsChangedListener(const wp<ContentsChangedListener>& listener) {
99 setFrameAvailableListener(listener);
100 Mutex::Autolock lock(mMutex);
101 mContentsChangedListener = listener;
102}
103
Chia-I Wuda5c7302017-11-27 14:51:06 -0800104// We need to determine the time when a buffer acquired now will be
105// displayed. This can be calculated:
106// time when previous buffer's actual-present fence was signaled
107// + current display refresh rate * HWC latency
108// + a little extra padding
109//
110// Buffer producers are expected to set their desired presentation time
111// based on choreographer time stamps, which (coming from vsync events)
112// will be slightly later then the actual-present timing. If we get a
113// desired-present time that is unintentionally a hair after the next
114// vsync, we'll hold the frame when we really want to display it. We
115// need to take the offset between actual-present and reported-vsync
116// into account.
117//
118// If the system is configured without a DispSync phase offset for the app,
119// we also want to throw in a bit of padding to avoid edge cases where we
120// just barely miss. We want to do it here, not in every app. A major
121// source of trouble is the app's use of the display's ideal refresh time
122// (via Display.getRefreshRate()), which could be off of the actual refresh
123// by a few percent, with the error multiplied by the number of frames
124// between now and when the buffer should be displayed.
125//
126// If the refresh reported to the app has a phase offset, we shouldn't need
127// to tweak anything here.
128nsecs_t BufferLayerConsumer::computeExpectedPresent(const DispSync& dispSync) {
129 // The HWC doesn't currently have a way to report additional latency.
130 // Assume that whatever we submit now will appear right after the flip.
131 // For a smart panel this might be 1. This is expressed in frames,
132 // rather than time, because we expect to have a constant frame delay
133 // regardless of the refresh rate.
134 const uint32_t hwcLatency = 0;
135
136 // Ask DispSync when the next refresh will be (CLOCK_MONOTONIC).
137 const nsecs_t nextRefresh = dispSync.computeNextRefresh(hwcLatency);
138
139 // The DispSync time is already adjusted for the difference between
140 // vsync and reported-vsync (SurfaceFlinger::dispSyncPresentTimeOffset), so
141 // we don't need to factor that in here. Pad a little to avoid
142 // weird effects if apps might be requesting times right on the edge.
143 nsecs_t extraPadding = 0;
144 if (SurfaceFlinger::vsyncPhaseOffsetNs == 0) {
145 extraPadding = 1000000; // 1ms (6% of 60Hz)
146 }
147
148 return nextRefresh + extraPadding;
149}
150
151status_t BufferLayerConsumer::updateTexImage(BufferRejecter* rejecter, const DispSync& dispSync,
152 bool* autoRefresh, bool* queuedBuffer,
153 uint64_t maxFrameNumber) {
Chia-I Wuf1405182017-11-27 11:29:21 -0800154 ATRACE_CALL();
155 BLC_LOGV("updateTexImage");
156 Mutex::Autolock lock(mMutex);
157
158 if (mAbandoned) {
159 BLC_LOGE("updateTexImage: BufferLayerConsumer is abandoned!");
160 return NO_INIT;
161 }
162
Chia-I Wu9f2db772017-11-30 21:06:50 -0800163 // Make sure RenderEngine is current
164 if (!mRE.isCurrent()) {
165 BLC_LOGE("updateTexImage: RenderEngine is not current");
166 return INVALID_OPERATION;
Chia-I Wuf1405182017-11-27 11:29:21 -0800167 }
168
169 BufferItem item;
170
171 // Acquire the next buffer.
172 // In asynchronous mode the list is guaranteed to be one buffer
173 // deep, while in synchronous mode we use the oldest buffer.
Chia-I Wu9f2db772017-11-30 21:06:50 -0800174 status_t err = acquireBufferLocked(&item, computeExpectedPresent(dispSync), maxFrameNumber);
Chia-I Wuf1405182017-11-27 11:29:21 -0800175 if (err != NO_ERROR) {
176 if (err == BufferQueue::NO_BUFFER_AVAILABLE) {
Chia-I Wuf1405182017-11-27 11:29:21 -0800177 err = NO_ERROR;
Chia-I Wuda5c7302017-11-27 14:51:06 -0800178 } else if (err == BufferQueue::PRESENT_LATER) {
179 // return the error, without logging
Chia-I Wuf1405182017-11-27 11:29:21 -0800180 } else {
181 BLC_LOGE("updateTexImage: acquire failed: %s (%d)", strerror(-err), err);
182 }
183 return err;
184 }
185
Chia-I Wuda5c7302017-11-27 14:51:06 -0800186 if (autoRefresh) {
187 *autoRefresh = item.mAutoRefresh;
188 }
189
190 if (queuedBuffer) {
191 *queuedBuffer = item.mQueuedBuffer;
192 }
193
194 // We call the rejecter here, in case the caller has a reason to
195 // not accept this buffer. This is used by SurfaceFlinger to
196 // reject buffers which have the wrong size
197 int slot = item.mSlot;
198 if (rejecter && rejecter->reject(mSlots[slot].mGraphicBuffer, item)) {
199 releaseBufferLocked(slot, mSlots[slot].mGraphicBuffer);
200 return BUFFER_REJECTED;
201 }
202
Chia-I Wuf1405182017-11-27 11:29:21 -0800203 // Release the previous buffer.
Chia-I Wuda5c7302017-11-27 14:51:06 -0800204 err = updateAndReleaseLocked(item, &mPendingRelease);
Chia-I Wuf1405182017-11-27 11:29:21 -0800205 if (err != NO_ERROR) {
Chia-I Wuf1405182017-11-27 11:29:21 -0800206 return err;
207 }
208
Chia-I Wuda5c7302017-11-27 14:51:06 -0800209 if (!SyncFeatures::getInstance().useNativeFenceSync()) {
210 // Bind the new buffer to the GL texture.
211 //
212 // Older devices require the "implicit" synchronization provided
213 // by glEGLImageTargetTexture2DOES, which this method calls. Newer
214 // devices will either call this in Layer::onDraw, or (if it's not
215 // a GL-composited layer) not at all.
216 err = bindTextureImageLocked();
217 }
218
219 return err;
220}
221
Chia-I Wu0cb75ac2017-11-27 15:56:04 -0800222status_t BufferLayerConsumer::bindTextureImage() {
223 Mutex::Autolock lock(mMutex);
224 return bindTextureImageLocked();
225}
226
Chia-I Wuda5c7302017-11-27 14:51:06 -0800227void BufferLayerConsumer::setReleaseFence(const sp<Fence>& fence) {
228 if (!fence->isValid()) {
229 return;
230 }
231
232 auto slot = mPendingRelease.isPending ? mPendingRelease.currentTexture : mCurrentTexture;
233 if (slot == BufferQueue::INVALID_BUFFER_SLOT) {
234 return;
235 }
236
237 auto buffer = mPendingRelease.isPending ? mPendingRelease.graphicBuffer
238 : mCurrentTextureImage->graphicBuffer();
239 auto err = addReleaseFence(slot, buffer, fence);
240 if (err != OK) {
241 BLC_LOGE("setReleaseFence: failed to add the fence: %s (%d)", strerror(-err), err);
242 }
243}
244
245bool BufferLayerConsumer::releasePendingBuffer() {
246 if (!mPendingRelease.isPending) {
247 BLC_LOGV("Pending buffer already released");
248 return false;
249 }
250 BLC_LOGV("Releasing pending buffer");
251 Mutex::Autolock lock(mMutex);
252 status_t result =
253 releaseBufferLocked(mPendingRelease.currentTexture, mPendingRelease.graphicBuffer);
254 if (result < NO_ERROR) {
255 BLC_LOGE("releasePendingBuffer failed: %s (%d)", strerror(-result), result);
256 }
257 mPendingRelease = PendingRelease();
258 return true;
Chia-I Wuf1405182017-11-27 11:29:21 -0800259}
260
Chia-I Wu0cb75ac2017-11-27 15:56:04 -0800261sp<Fence> BufferLayerConsumer::getPrevFinalReleaseFence() const {
262 Mutex::Autolock lock(mMutex);
263 return ConsumerBase::mPrevFinalReleaseFence;
264}
265
Chia-I Wuf1405182017-11-27 11:29:21 -0800266status_t BufferLayerConsumer::acquireBufferLocked(BufferItem* item, nsecs_t presentWhen,
267 uint64_t maxFrameNumber) {
268 status_t err = ConsumerBase::acquireBufferLocked(item, presentWhen, maxFrameNumber);
269 if (err != NO_ERROR) {
270 return err;
271 }
272
273 // If item->mGraphicBuffer is not null, this buffer has not been acquired
274 // before, so any prior EglImage created is using a stale buffer. This
275 // replaces any old EglImage with a new one (using the new buffer).
Peiyong Lin566a3b42018-01-09 18:22:43 -0800276 if (item->mGraphicBuffer != nullptr) {
Chia-I Wu6748db42017-12-01 10:53:53 -0800277 mImages[item->mSlot] = new Image(item->mGraphicBuffer, mRE);
Chia-I Wuf1405182017-11-27 11:29:21 -0800278 }
279
280 return NO_ERROR;
281}
282
Chia-I Wu6748db42017-12-01 10:53:53 -0800283bool BufferLayerConsumer::canUseImageCrop(const Rect& crop) const {
284 // If the crop rect is not at the origin, we can't set the crop on the
285 // EGLImage because that's not allowed by the EGL_ANDROID_image_crop
286 // extension. In the future we can add a layered extension that
287 // removes this restriction if there is hardware that can support it.
288 return mRE.supportsImageCrop() && crop.left == 0 && crop.top == 0;
289}
290
Chia-I Wuf1405182017-11-27 11:29:21 -0800291status_t BufferLayerConsumer::updateAndReleaseLocked(const BufferItem& item,
292 PendingRelease* pendingRelease) {
293 status_t err = NO_ERROR;
294
295 int slot = item.mSlot;
296
Chia-I Wuf1405182017-11-27 11:29:21 -0800297 // Do whatever sync ops we need to do before releasing the old slot.
298 if (slot != mCurrentTexture) {
Chia-I Wu3498e3c2017-12-01 10:19:38 -0800299 err = syncForReleaseLocked();
Chia-I Wuf1405182017-11-27 11:29:21 -0800300 if (err != NO_ERROR) {
301 // Release the buffer we just acquired. It's not safe to
302 // release the old buffer, so instead we just drop the new frame.
303 // As we are still under lock since acquireBuffer, it is safe to
304 // release by slot.
Chia-I Wu6aff69b2017-11-27 14:08:48 -0800305 releaseBufferLocked(slot, mSlots[slot].mGraphicBuffer);
Chia-I Wuf1405182017-11-27 11:29:21 -0800306 return err;
307 }
308 }
309
310 BLC_LOGV("updateAndRelease: (slot=%d buf=%p) -> (slot=%d buf=%p)", mCurrentTexture,
Peiyong Lin566a3b42018-01-09 18:22:43 -0800311 mCurrentTextureImage != nullptr ? mCurrentTextureImage->graphicBufferHandle() : 0,
312 slot, mSlots[slot].mGraphicBuffer->handle);
Chia-I Wuf1405182017-11-27 11:29:21 -0800313
314 // Hang onto the pointer so that it isn't freed in the call to
315 // releaseBufferLocked() if we're in shared buffer mode and both buffers are
316 // the same.
Chia-I Wu6748db42017-12-01 10:53:53 -0800317 sp<Image> nextTextureImage = mImages[slot];
Chia-I Wuf1405182017-11-27 11:29:21 -0800318
319 // release old buffer
320 if (mCurrentTexture != BufferQueue::INVALID_BUFFER_SLOT) {
321 if (pendingRelease == nullptr) {
322 status_t status =
Chia-I Wu6aff69b2017-11-27 14:08:48 -0800323 releaseBufferLocked(mCurrentTexture, mCurrentTextureImage->graphicBuffer());
Chia-I Wuf1405182017-11-27 11:29:21 -0800324 if (status < NO_ERROR) {
325 BLC_LOGE("updateAndRelease: failed to release buffer: %s (%d)", strerror(-status),
326 status);
327 err = status;
328 // keep going, with error raised [?]
329 }
330 } else {
331 pendingRelease->currentTexture = mCurrentTexture;
332 pendingRelease->graphicBuffer = mCurrentTextureImage->graphicBuffer();
Chia-I Wuf1405182017-11-27 11:29:21 -0800333 pendingRelease->isPending = true;
334 }
335 }
336
337 // Update the BufferLayerConsumer state.
338 mCurrentTexture = slot;
339 mCurrentTextureImage = nextTextureImage;
340 mCurrentCrop = item.mCrop;
341 mCurrentTransform = item.mTransform;
342 mCurrentScalingMode = item.mScalingMode;
343 mCurrentTimestamp = item.mTimestamp;
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700344 mCurrentDataSpace = static_cast<ui::Dataspace>(item.mDataSpace);
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -0700345 mCurrentHdrMetadata = item.mHdrMetadata;
Chia-I Wuf1405182017-11-27 11:29:21 -0800346 mCurrentFence = item.mFence;
347 mCurrentFenceTime = item.mFenceTime;
348 mCurrentFrameNumber = item.mFrameNumber;
Chia-I Wu67dcc692017-11-27 14:51:06 -0800349 mCurrentTransformToDisplayInverse = item.mTransformToDisplayInverse;
350 mCurrentSurfaceDamage = item.mSurfaceDamage;
Chia-I Wu5c6e4632018-01-11 08:54:38 -0800351 mCurrentApi = item.mApi;
Chia-I Wuf1405182017-11-27 11:29:21 -0800352
353 computeCurrentTransformMatrixLocked();
354
355 return err;
356}
357
358status_t BufferLayerConsumer::bindTextureImageLocked() {
Dan Stoza2713c302018-03-28 17:07:36 -0700359 ATRACE_CALL();
Chia-I Wu9f2db772017-11-30 21:06:50 -0800360 mRE.checkErrors();
Chia-I Wuf1405182017-11-27 11:29:21 -0800361
Peiyong Lin566a3b42018-01-09 18:22:43 -0800362 if (mCurrentTexture == BufferQueue::INVALID_BUFFER_SLOT && mCurrentTextureImage == nullptr) {
Chia-I Wuf1405182017-11-27 11:29:21 -0800363 BLC_LOGE("bindTextureImage: no currently-bound texture");
Lloyd Pique144e1162017-12-20 16:44:52 -0800364 mRE.bindExternalTextureImage(mTexName, *mRE.createImage());
Chia-I Wuf1405182017-11-27 11:29:21 -0800365 return NO_INIT;
366 }
367
Chia-I Wu6748db42017-12-01 10:53:53 -0800368 const Rect& imageCrop = canUseImageCrop(mCurrentCrop) ? mCurrentCrop : Rect::EMPTY_RECT;
369 status_t err = mCurrentTextureImage->createIfNeeded(imageCrop);
Chia-I Wuf1405182017-11-27 11:29:21 -0800370 if (err != NO_ERROR) {
Chia-I Wu6748db42017-12-01 10:53:53 -0800371 BLC_LOGW("bindTextureImage: can't create image on slot=%d", mCurrentTexture);
Lloyd Pique144e1162017-12-20 16:44:52 -0800372 mRE.bindExternalTextureImage(mTexName, *mRE.createImage());
Chia-I Wuf1405182017-11-27 11:29:21 -0800373 return UNKNOWN_ERROR;
374 }
Chia-I Wu6748db42017-12-01 10:53:53 -0800375
376 mRE.bindExternalTextureImage(mTexName, mCurrentTextureImage->image());
Chia-I Wuf1405182017-11-27 11:29:21 -0800377
Chia-I Wuf1405182017-11-27 11:29:21 -0800378 // Wait for the new buffer to be ready.
Chia-I Wu3498e3c2017-12-01 10:19:38 -0800379 return doFenceWaitLocked();
Chia-I Wuf1405182017-11-27 11:29:21 -0800380}
381
Chia-I Wu3498e3c2017-12-01 10:19:38 -0800382status_t BufferLayerConsumer::syncForReleaseLocked() {
Chia-I Wuf1405182017-11-27 11:29:21 -0800383 BLC_LOGV("syncForReleaseLocked");
384
385 if (mCurrentTexture != BufferQueue::INVALID_BUFFER_SLOT) {
386 if (SyncFeatures::getInstance().useNativeFenceSync()) {
Chia-I Wudf1badd2017-12-27 11:10:20 -0800387 base::unique_fd fenceFd = mRE.flush();
Chia-I Wu3498e3c2017-12-01 10:19:38 -0800388 if (fenceFd == -1) {
389 BLC_LOGE("syncForReleaseLocked: failed to flush RenderEngine");
Chia-I Wuf1405182017-11-27 11:29:21 -0800390 return UNKNOWN_ERROR;
391 }
Chia-I Wudf1badd2017-12-27 11:10:20 -0800392 sp<Fence> fence(new Fence(std::move(fenceFd)));
Chia-I Wuf1405182017-11-27 11:29:21 -0800393 status_t err = addReleaseFenceLocked(mCurrentTexture,
394 mCurrentTextureImage->graphicBuffer(), fence);
395 if (err != OK) {
396 BLC_LOGE("syncForReleaseLocked: error adding release fence: "
397 "%s (%d)",
398 strerror(-err), err);
399 return err;
400 }
Chia-I Wuf1405182017-11-27 11:29:21 -0800401 }
402 }
403
404 return OK;
405}
406
Chia-I Wuf1405182017-11-27 11:29:21 -0800407void BufferLayerConsumer::getTransformMatrix(float mtx[16]) {
408 Mutex::Autolock lock(mMutex);
409 memcpy(mtx, mCurrentTransformMatrix, sizeof(mCurrentTransformMatrix));
410}
411
412void BufferLayerConsumer::setFilteringEnabled(bool enabled) {
413 Mutex::Autolock lock(mMutex);
414 if (mAbandoned) {
415 BLC_LOGE("setFilteringEnabled: BufferLayerConsumer is abandoned!");
416 return;
417 }
418 bool needsRecompute = mFilteringEnabled != enabled;
419 mFilteringEnabled = enabled;
420
Peiyong Lin566a3b42018-01-09 18:22:43 -0800421 if (needsRecompute && mCurrentTextureImage == nullptr) {
422 BLC_LOGD("setFilteringEnabled called with mCurrentTextureImage == nullptr");
Chia-I Wuf1405182017-11-27 11:29:21 -0800423 }
424
Peiyong Lin566a3b42018-01-09 18:22:43 -0800425 if (needsRecompute && mCurrentTextureImage != nullptr) {
Chia-I Wuf1405182017-11-27 11:29:21 -0800426 computeCurrentTransformMatrixLocked();
427 }
428}
429
430void BufferLayerConsumer::computeCurrentTransformMatrixLocked() {
431 BLC_LOGV("computeCurrentTransformMatrixLocked");
432 sp<GraphicBuffer> buf =
433 (mCurrentTextureImage == nullptr) ? nullptr : mCurrentTextureImage->graphicBuffer();
434 if (buf == nullptr) {
435 BLC_LOGD("computeCurrentTransformMatrixLocked: "
Peiyong Lin566a3b42018-01-09 18:22:43 -0800436 "mCurrentTextureImage is nullptr");
Chia-I Wuf1405182017-11-27 11:29:21 -0800437 }
Chia-I Wu6748db42017-12-01 10:53:53 -0800438 const Rect& cropRect = canUseImageCrop(mCurrentCrop) ? Rect::EMPTY_RECT : mCurrentCrop;
439 GLConsumer::computeTransformMatrix(mCurrentTransformMatrix, buf, cropRect, mCurrentTransform,
440 mFilteringEnabled);
Chia-I Wuf1405182017-11-27 11:29:21 -0800441}
442
Chia-I Wuf1405182017-11-27 11:29:21 -0800443nsecs_t BufferLayerConsumer::getTimestamp() {
444 BLC_LOGV("getTimestamp");
445 Mutex::Autolock lock(mMutex);
446 return mCurrentTimestamp;
447}
448
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700449ui::Dataspace BufferLayerConsumer::getCurrentDataSpace() {
Chia-I Wuf1405182017-11-27 11:29:21 -0800450 BLC_LOGV("getCurrentDataSpace");
451 Mutex::Autolock lock(mMutex);
452 return mCurrentDataSpace;
453}
454
Courtney Goeltzenleuchter9bad0d72017-12-19 12:34:34 -0700455const HdrMetadata& BufferLayerConsumer::getCurrentHdrMetadata() const {
456 BLC_LOGV("getCurrentHdrMetadata");
457 Mutex::Autolock lock(mMutex);
458 return mCurrentHdrMetadata;
459}
460
Chia-I Wuf1405182017-11-27 11:29:21 -0800461uint64_t BufferLayerConsumer::getFrameNumber() {
462 BLC_LOGV("getFrameNumber");
463 Mutex::Autolock lock(mMutex);
464 return mCurrentFrameNumber;
465}
466
Chia-I Wu67dcc692017-11-27 14:51:06 -0800467bool BufferLayerConsumer::getTransformToDisplayInverse() const {
468 Mutex::Autolock lock(mMutex);
469 return mCurrentTransformToDisplayInverse;
470}
471
472const Region& BufferLayerConsumer::getSurfaceDamage() const {
473 return mCurrentSurfaceDamage;
474}
475
Chia-I Wu5c6e4632018-01-11 08:54:38 -0800476int BufferLayerConsumer::getCurrentApi() const {
477 Mutex::Autolock lock(mMutex);
478 return mCurrentApi;
479}
480
Chia-I Wuf1405182017-11-27 11:29:21 -0800481sp<GraphicBuffer> BufferLayerConsumer::getCurrentBuffer(int* outSlot) const {
482 Mutex::Autolock lock(mMutex);
483
484 if (outSlot != nullptr) {
485 *outSlot = mCurrentTexture;
486 }
487
Peiyong Lin566a3b42018-01-09 18:22:43 -0800488 return (mCurrentTextureImage == nullptr) ? nullptr : mCurrentTextureImage->graphicBuffer();
Chia-I Wuf1405182017-11-27 11:29:21 -0800489}
490
491Rect BufferLayerConsumer::getCurrentCrop() const {
492 Mutex::Autolock lock(mMutex);
493 return (mCurrentScalingMode == NATIVE_WINDOW_SCALING_MODE_SCALE_CROP)
Chia-I Wue1e11872017-12-01 09:21:59 -0800494 ? GLConsumer::scaleDownCrop(mCurrentCrop, mDefaultWidth, mDefaultHeight)
Chia-I Wuf1405182017-11-27 11:29:21 -0800495 : mCurrentCrop;
496}
497
498uint32_t BufferLayerConsumer::getCurrentTransform() const {
499 Mutex::Autolock lock(mMutex);
500 return mCurrentTransform;
501}
502
503uint32_t BufferLayerConsumer::getCurrentScalingMode() const {
504 Mutex::Autolock lock(mMutex);
505 return mCurrentScalingMode;
506}
507
508sp<Fence> BufferLayerConsumer::getCurrentFence() const {
509 Mutex::Autolock lock(mMutex);
510 return mCurrentFence;
511}
512
513std::shared_ptr<FenceTime> BufferLayerConsumer::getCurrentFenceTime() const {
514 Mutex::Autolock lock(mMutex);
515 return mCurrentFenceTime;
516}
517
Chia-I Wu3498e3c2017-12-01 10:19:38 -0800518status_t BufferLayerConsumer::doFenceWaitLocked() const {
Chia-I Wu9f2db772017-11-30 21:06:50 -0800519 if (!mRE.isCurrent()) {
Chia-I Wu3498e3c2017-12-01 10:19:38 -0800520 BLC_LOGE("doFenceWait: RenderEngine is not current");
Chia-I Wuf1405182017-11-27 11:29:21 -0800521 return INVALID_OPERATION;
522 }
523
524 if (mCurrentFence->isValid()) {
525 if (SyncFeatures::getInstance().useWaitSync()) {
Chia-I Wu3498e3c2017-12-01 10:19:38 -0800526 base::unique_fd fenceFd(mCurrentFence->dup());
Chia-I Wuf1405182017-11-27 11:29:21 -0800527 if (fenceFd == -1) {
Chia-I Wu3498e3c2017-12-01 10:19:38 -0800528 BLC_LOGE("doFenceWait: error dup'ing fence fd: %d", errno);
Chia-I Wuf1405182017-11-27 11:29:21 -0800529 return -errno;
530 }
Chia-I Wu3498e3c2017-12-01 10:19:38 -0800531 if (!mRE.waitFence(std::move(fenceFd))) {
532 BLC_LOGE("doFenceWait: failed to wait on fence fd");
Chia-I Wuf1405182017-11-27 11:29:21 -0800533 return UNKNOWN_ERROR;
534 }
535 } else {
Chia-I Wu3498e3c2017-12-01 10:19:38 -0800536 status_t err = mCurrentFence->waitForever("BufferLayerConsumer::doFenceWaitLocked");
Chia-I Wuf1405182017-11-27 11:29:21 -0800537 if (err != NO_ERROR) {
Chia-I Wu3498e3c2017-12-01 10:19:38 -0800538 BLC_LOGE("doFenceWait: error waiting for fence: %d", err);
Chia-I Wuf1405182017-11-27 11:29:21 -0800539 return err;
540 }
541 }
542 }
543
544 return NO_ERROR;
545}
546
547void BufferLayerConsumer::freeBufferLocked(int slotIndex) {
548 BLC_LOGV("freeBufferLocked: slotIndex=%d", slotIndex);
549 if (slotIndex == mCurrentTexture) {
550 mCurrentTexture = BufferQueue::INVALID_BUFFER_SLOT;
551 }
Chia-I Wu6748db42017-12-01 10:53:53 -0800552 mImages[slotIndex].clear();
Chia-I Wuf1405182017-11-27 11:29:21 -0800553 ConsumerBase::freeBufferLocked(slotIndex);
554}
555
Chia-I Wuc75c44d2017-11-27 14:32:57 -0800556void BufferLayerConsumer::onDisconnect() {
557 sp<Layer> l = mLayer.promote();
558 if (l.get()) {
559 l->onDisconnect();
560 }
561}
562
Chia-I Wufd257f82017-11-27 14:51:06 -0800563void BufferLayerConsumer::onSidebandStreamChanged() {
564 FrameAvailableListener* unsafeFrameAvailableListener = nullptr;
565 {
566 Mutex::Autolock lock(mFrameAvailableMutex);
567 unsafeFrameAvailableListener = mFrameAvailableListener.unsafe_get();
568 }
569 sp<ContentsChangedListener> listener;
570 { // scope for the lock
571 Mutex::Autolock lock(mMutex);
572 ALOG_ASSERT(unsafeFrameAvailableListener == mContentsChangedListener.unsafe_get());
573 listener = mContentsChangedListener.promote();
574 }
575
Peiyong Lin566a3b42018-01-09 18:22:43 -0800576 if (listener != nullptr) {
Chia-I Wufd257f82017-11-27 14:51:06 -0800577 listener->onSidebandStreamChanged();
578 }
579}
580
Chia-I Wuc75c44d2017-11-27 14:32:57 -0800581void BufferLayerConsumer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
582 FrameEventHistoryDelta* outDelta) {
583 sp<Layer> l = mLayer.promote();
584 if (l.get()) {
585 l->addAndGetFrameTimestamps(newTimestamps, outDelta);
586 }
587}
588
Chia-I Wuf1405182017-11-27 11:29:21 -0800589void BufferLayerConsumer::abandonLocked() {
590 BLC_LOGV("abandonLocked");
591 mCurrentTextureImage.clear();
592 ConsumerBase::abandonLocked();
593}
594
595status_t BufferLayerConsumer::setConsumerUsageBits(uint64_t usage) {
596 return ConsumerBase::setConsumerUsageBits(usage | DEFAULT_USAGE_FLAGS);
597}
598
599void BufferLayerConsumer::dumpLocked(String8& result, const char* prefix) const {
600 result.appendFormat("%smTexName=%d mCurrentTexture=%d\n"
601 "%smCurrentCrop=[%d,%d,%d,%d] mCurrentTransform=%#x\n",
602 prefix, mTexName, mCurrentTexture, prefix, mCurrentCrop.left,
603 mCurrentCrop.top, mCurrentCrop.right, mCurrentCrop.bottom,
604 mCurrentTransform);
605
606 ConsumerBase::dumpLocked(result, prefix);
607}
608
Lloyd Pique144e1162017-12-20 16:44:52 -0800609BufferLayerConsumer::Image::Image(sp<GraphicBuffer> graphicBuffer, RE::RenderEngine& engine)
Chia-I Wuf1405182017-11-27 11:29:21 -0800610 : mGraphicBuffer(graphicBuffer),
Lloyd Pique144e1162017-12-20 16:44:52 -0800611 mImage{engine.createImage()},
Chia-I Wu6748db42017-12-01 10:53:53 -0800612 mCreated(false),
613 mCropWidth(0),
614 mCropHeight(0) {}
Chia-I Wuf1405182017-11-27 11:29:21 -0800615
Lloyd Pique144e1162017-12-20 16:44:52 -0800616BufferLayerConsumer::Image::~Image() = default;
617
Chia-I Wu6748db42017-12-01 10:53:53 -0800618status_t BufferLayerConsumer::Image::createIfNeeded(const Rect& imageCrop) {
619 const int32_t cropWidth = imageCrop.width();
620 const int32_t cropHeight = imageCrop.height();
621 if (mCreated && mCropWidth == cropWidth && mCropHeight == cropHeight) {
622 return OK;
Chia-I Wuf1405182017-11-27 11:29:21 -0800623 }
624
Lloyd Pique144e1162017-12-20 16:44:52 -0800625 mCreated = mImage->setNativeWindowBuffer(mGraphicBuffer->getNativeBuffer(),
626 mGraphicBuffer->getUsage() & GRALLOC_USAGE_PROTECTED,
627 cropWidth, cropHeight);
Chia-I Wu6748db42017-12-01 10:53:53 -0800628 if (mCreated) {
629 mCropWidth = cropWidth;
630 mCropHeight = cropHeight;
631 } else {
632 mCropWidth = 0;
633 mCropHeight = 0;
Chia-I Wuf1405182017-11-27 11:29:21 -0800634
Chia-I Wuf1405182017-11-27 11:29:21 -0800635 const sp<GraphicBuffer>& buffer = mGraphicBuffer;
636 ALOGE("Failed to create image. size=%ux%u st=%u usage=%#" PRIx64 " fmt=%d",
637 buffer->getWidth(), buffer->getHeight(), buffer->getStride(), buffer->getUsage(),
638 buffer->getPixelFormat());
Chia-I Wuf1405182017-11-27 11:29:21 -0800639 }
640
Chia-I Wu6748db42017-12-01 10:53:53 -0800641 return mCreated ? OK : UNKNOWN_ERROR;
Chia-I Wuf1405182017-11-27 11:29:21 -0800642}
643
644}; // namespace android