blob: 4d4c9fcc3dc0beed3e228f41ed2d6fa2a31098a9 [file] [log] [blame]
David Sodman0c69cad2017-08-21 12:12:51 -07001/*
2 * Copyright (C) 2017 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//#define LOG_NDEBUG 0
18#undef LOG_TAG
19#define LOG_TAG "BufferLayer"
20#define ATRACE_TAG ATRACE_TAG_GRAPHICS
21
22#include "BufferLayer.h"
23#include "Colorizer.h"
24#include "DisplayDevice.h"
25#include "LayerRejecter.h"
26#include "clz.h"
27
28#include "RenderEngine/RenderEngine.h"
29
30#include <gui/BufferItem.h>
31#include <gui/BufferQueue.h>
32#include <gui/LayerDebugInfo.h>
33#include <gui/Surface.h>
34
35#include <ui/DebugUtils.h>
36
37#include <utils/Errors.h>
38#include <utils/Log.h>
39#include <utils/NativeHandle.h>
40#include <utils/StopWatch.h>
41#include <utils/Trace.h>
42
43#include <cutils/compiler.h>
44#include <cutils/native_handle.h>
45#include <cutils/properties.h>
46
47#include <math.h>
48#include <stdlib.h>
49#include <mutex>
50
51namespace android {
52
53BufferLayer::BufferLayer(SurfaceFlinger* flinger, const sp<Client>& client, const String8& name,
54 uint32_t w, uint32_t h, uint32_t flags)
55 : Layer(flinger, client, name, w, h, flags),
Chia-I Wub28c6742017-12-27 10:59:54 -080056 mConsumer(nullptr),
Ivan Lozanoeb13f9e2017-11-09 12:39:31 -080057 mTextureName(UINT32_MAX),
David Sodman0c69cad2017-08-21 12:12:51 -070058 mFormat(PIXEL_FORMAT_NONE),
59 mCurrentScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE),
60 mBufferLatched(false),
61 mPreviousFrameNumber(0),
62 mUpdateTexImageFailed(false),
63 mRefreshPending(false) {
David Sodman0c69cad2017-08-21 12:12:51 -070064 ALOGV("Creating Layer %s", name.string());
David Sodman0c69cad2017-08-21 12:12:51 -070065
66 mFlinger->getRenderEngine().genTextures(1, &mTextureName);
67 mTexture.init(Texture::TEXTURE_EXTERNAL, mTextureName);
68
69 if (flags & ISurfaceComposerClient::eNonPremultiplied) mPremultipliedAlpha = false;
70
71 mCurrentState.requested = mCurrentState.active;
72
73 // drawing state & current state are identical
74 mDrawingState = mCurrentState;
75}
76
77BufferLayer::~BufferLayer() {
David Sodman0c69cad2017-08-21 12:12:51 -070078 mFlinger->deleteTextureAsync(mTextureName);
79
David Sodman6f65f3e2017-11-03 14:28:09 -070080 if (!getBE().mHwcLayers.empty()) {
David Sodman0c69cad2017-08-21 12:12:51 -070081 ALOGE("Found stale hardware composer layers when destroying "
82 "surface flinger layer %s",
83 mName.string());
84 destroyAllHwcLayers();
85 }
David Sodman0c69cad2017-08-21 12:12:51 -070086}
87
David Sodmaneb085e02017-10-05 18:49:04 -070088void BufferLayer::useSurfaceDamage() {
89 if (mFlinger->mForceFullDamage) {
90 surfaceDamageRegion = Region::INVALID_REGION;
91 } else {
Chia-I Wub28c6742017-12-27 10:59:54 -080092 surfaceDamageRegion = mConsumer->getSurfaceDamage();
David Sodmaneb085e02017-10-05 18:49:04 -070093 }
94}
95
96void BufferLayer::useEmptyDamage() {
97 surfaceDamageRegion.clear();
98}
99
David Sodman41fdfc92017-11-06 16:09:56 -0800100bool BufferLayer::isProtected() const {
David Sodman0cc69182017-11-17 12:12:07 -0800101 const sp<GraphicBuffer>& buffer(getBE().compositionInfo.mBuffer);
David Sodman5b4cffc2017-11-23 13:20:29 -0800102 return (buffer != 0) &&
103 (buffer->getUsage() & GRALLOC_USAGE_PROTECTED);
David Sodman0c69cad2017-08-21 12:12:51 -0700104}
105
106bool BufferLayer::isVisible() const {
107 return !(isHiddenByPolicy()) && getAlpha() > 0.0f &&
Peiyong Lin566a3b42018-01-09 18:22:43 -0800108 (getBE().compositionInfo.mBuffer != nullptr ||
109 getBE().compositionInfo.hwc.sidebandStream != nullptr);
David Sodman0c69cad2017-08-21 12:12:51 -0700110}
111
112bool BufferLayer::isFixedSize() const {
113 return getEffectiveScalingMode() != NATIVE_WINDOW_SCALING_MODE_FREEZE;
114}
115
116status_t BufferLayer::setBuffers(uint32_t w, uint32_t h, PixelFormat format, uint32_t flags) {
117 uint32_t const maxSurfaceDims =
118 min(mFlinger->getMaxTextureSize(), mFlinger->getMaxViewportDims());
119
120 // never allow a surface larger than what our underlying GL implementation
121 // can handle.
122 if ((uint32_t(w) > maxSurfaceDims) || (uint32_t(h) > maxSurfaceDims)) {
123 ALOGE("dimensions too large %u x %u", uint32_t(w), uint32_t(h));
124 return BAD_VALUE;
125 }
126
127 mFormat = format;
128
129 mPotentialCursor = (flags & ISurfaceComposerClient::eCursorWindow) ? true : false;
130 mProtectedByApp = (flags & ISurfaceComposerClient::eProtectedByApp) ? true : false;
131 mCurrentOpacity = getOpacityForFormat(format);
132
Chia-I Wub28c6742017-12-27 10:59:54 -0800133 mConsumer->setDefaultBufferSize(w, h);
134 mConsumer->setDefaultBufferFormat(format);
135 mConsumer->setConsumerUsageBits(getEffectiveUsage(0));
David Sodman0c69cad2017-08-21 12:12:51 -0700136
137 return NO_ERROR;
138}
139
140static constexpr mat4 inverseOrientation(uint32_t transform) {
David Sodman41fdfc92017-11-06 16:09:56 -0800141 const mat4 flipH(-1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1);
142 const mat4 flipV(1, 0, 0, 0, 0, -1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1);
143 const mat4 rot90(0, 1, 0, 0, -1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1);
David Sodman0c69cad2017-08-21 12:12:51 -0700144 mat4 tr;
145
146 if (transform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
147 tr = tr * rot90;
148 }
149 if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_H) {
150 tr = tr * flipH;
151 }
152 if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_V) {
153 tr = tr * flipV;
154 }
155 return inverse(tr);
156}
157
158/*
159 * onDraw will draw the current layer onto the presentable buffer
160 */
161void BufferLayer::onDraw(const RenderArea& renderArea, const Region& clip,
162 bool useIdentityTransform) const {
163 ATRACE_CALL();
164
David Sodman0cc69182017-11-17 12:12:07 -0800165 if (CC_UNLIKELY(getBE().compositionInfo.mBuffer == 0)) {
David Sodman0c69cad2017-08-21 12:12:51 -0700166 // the texture has not been created yet, this Layer has
167 // in fact never been drawn into. This happens frequently with
168 // SurfaceView because the WindowManager can't know when the client
169 // has drawn the first time.
170
171 // If there is nothing under us, we paint the screen in black, otherwise
172 // we just skip this update.
173
174 // figure out if there is something below us
175 Region under;
176 bool finished = false;
177 mFlinger->mDrawingState.traverseInZOrder([&](Layer* layer) {
178 if (finished || layer == static_cast<BufferLayer const*>(this)) {
179 finished = true;
180 return;
181 }
182 under.orSelf(renderArea.getTransform().transform(layer->visibleRegion));
183 });
184 // if not everything below us is covered, we plug the holes!
185 Region holes(clip.subtract(under));
186 if (!holes.isEmpty()) {
187 clearWithOpenGL(renderArea, 0, 0, 0, 1);
188 }
189 return;
190 }
191
192 // Bind the current buffer to the GL texture, and wait for it to be
193 // ready for us to draw into.
Chia-I Wub28c6742017-12-27 10:59:54 -0800194 status_t err = mConsumer->bindTextureImage();
David Sodman0c69cad2017-08-21 12:12:51 -0700195 if (err != NO_ERROR) {
196 ALOGW("onDraw: bindTextureImage failed (err=%d)", err);
197 // Go ahead and draw the buffer anyway; no matter what we do the screen
198 // is probably going to have something visibly wrong.
199 }
200
201 bool blackOutLayer = isProtected() || (isSecure() && !renderArea.isSecure());
202
203 RenderEngine& engine(mFlinger->getRenderEngine());
204
205 if (!blackOutLayer) {
206 // TODO: we could be more subtle with isFixedSize()
207 const bool useFiltering = getFiltering() || needsFiltering(renderArea) || isFixedSize();
208
209 // Query the texture matrix given our current filtering mode.
210 float textureMatrix[16];
Chia-I Wub28c6742017-12-27 10:59:54 -0800211 mConsumer->setFilteringEnabled(useFiltering);
212 mConsumer->getTransformMatrix(textureMatrix);
David Sodman0c69cad2017-08-21 12:12:51 -0700213
214 if (getTransformToDisplayInverse()) {
215 /*
216 * the code below applies the primary display's inverse transform to
217 * the texture transform
218 */
219 uint32_t transform = DisplayDevice::getPrimaryDisplayOrientationTransform();
220 mat4 tr = inverseOrientation(transform);
221
222 /**
223 * TODO(b/36727915): This is basically a hack.
224 *
225 * Ensure that regardless of the parent transformation,
226 * this buffer is always transformed from native display
227 * orientation to display orientation. For example, in the case
228 * of a camera where the buffer remains in native orientation,
229 * we want the pixels to always be upright.
230 */
231 sp<Layer> p = mDrawingParent.promote();
232 if (p != nullptr) {
233 const auto parentTransform = p->getTransform();
234 tr = tr * inverseOrientation(parentTransform.getOrientation());
235 }
236
237 // and finally apply it to the original texture matrix
238 const mat4 texTransform(mat4(static_cast<const float*>(textureMatrix)) * tr);
239 memcpy(textureMatrix, texTransform.asArray(), sizeof(textureMatrix));
240 }
241
242 // Set things up for texturing.
David Sodman0cc69182017-11-17 12:12:07 -0800243 mTexture.setDimensions(getBE().compositionInfo.mBuffer->getWidth(),
244 getBE().compositionInfo.mBuffer->getHeight());
David Sodman0c69cad2017-08-21 12:12:51 -0700245 mTexture.setFiltering(useFiltering);
246 mTexture.setMatrix(textureMatrix);
247
248 engine.setupLayerTexturing(mTexture);
249 } else {
250 engine.setupLayerBlackedOut();
251 }
252 drawWithOpenGL(renderArea, useIdentityTransform);
253 engine.disableTexturing();
254}
255
David Sodmaneb085e02017-10-05 18:49:04 -0700256void BufferLayer::onLayerDisplayed(const sp<Fence>& releaseFence) {
Chia-I Wub28c6742017-12-27 10:59:54 -0800257 mConsumer->setReleaseFence(releaseFence);
David Sodmaneb085e02017-10-05 18:49:04 -0700258}
David Sodmaneb085e02017-10-05 18:49:04 -0700259
260void BufferLayer::abandon() {
Chia-I Wub28c6742017-12-27 10:59:54 -0800261 mConsumer->abandon();
David Sodmaneb085e02017-10-05 18:49:04 -0700262}
263
264bool BufferLayer::shouldPresentNow(const DispSync& dispSync) const {
265 if (mSidebandStreamChanged || mAutoRefresh) {
266 return true;
267 }
268
269 Mutex::Autolock lock(mQueueItemLock);
270 if (mQueueItems.empty()) {
271 return false;
272 }
273 auto timestamp = mQueueItems[0].mTimestamp;
Chia-I Wub28c6742017-12-27 10:59:54 -0800274 nsecs_t expectedPresent = mConsumer->computeExpectedPresent(dispSync);
David Sodmaneb085e02017-10-05 18:49:04 -0700275
276 // Ignore timestamps more than a second in the future
277 bool isPlausible = timestamp < (expectedPresent + s2ns(1));
278 ALOGW_IF(!isPlausible,
279 "[%s] Timestamp %" PRId64 " seems implausible "
280 "relative to expectedPresent %" PRId64,
281 mName.string(), timestamp, expectedPresent);
282
283 bool isDue = timestamp < expectedPresent;
284 return isDue || !isPlausible;
285}
286
287void BufferLayer::setTransformHint(uint32_t orientation) const {
Chia-I Wub28c6742017-12-27 10:59:54 -0800288 mConsumer->setTransformHint(orientation);
David Sodmaneb085e02017-10-05 18:49:04 -0700289}
290
David Sodman0c69cad2017-08-21 12:12:51 -0700291bool BufferLayer::onPreComposition(nsecs_t refreshStartTime) {
292 if (mBufferLatched) {
293 Mutex::Autolock lock(mFrameEventHistoryMutex);
David Sodman9eeae692017-11-02 10:53:32 -0700294 mFrameEventHistory.addPreComposition(mCurrentFrameNumber,
295 refreshStartTime);
David Sodman0c69cad2017-08-21 12:12:51 -0700296 }
297 mRefreshPending = false;
David Sodman9eeae692017-11-02 10:53:32 -0700298 return mQueuedFrames > 0 || mSidebandStreamChanged ||
299 mAutoRefresh;
David Sodman0c69cad2017-08-21 12:12:51 -0700300}
David Sodmaneb085e02017-10-05 18:49:04 -0700301bool BufferLayer::onPostComposition(const std::shared_ptr<FenceTime>& glDoneFence,
302 const std::shared_ptr<FenceTime>& presentFence,
303 const CompositorTiming& compositorTiming) {
304 // mFrameLatencyNeeded is true when a new frame was latched for the
305 // composition.
306 if (!mFrameLatencyNeeded) return false;
307
308 // Update mFrameEventHistory.
309 {
310 Mutex::Autolock lock(mFrameEventHistoryMutex);
David Sodman9eeae692017-11-02 10:53:32 -0700311 mFrameEventHistory.addPostComposition(mCurrentFrameNumber, glDoneFence,
312 presentFence, compositorTiming);
David Sodmaneb085e02017-10-05 18:49:04 -0700313 }
314
315 // Update mFrameTracker.
Chia-I Wub28c6742017-12-27 10:59:54 -0800316 nsecs_t desiredPresentTime = mConsumer->getTimestamp();
David Sodmaneb085e02017-10-05 18:49:04 -0700317 mFrameTracker.setDesiredPresentTime(desiredPresentTime);
318
Chia-I Wub28c6742017-12-27 10:59:54 -0800319 std::shared_ptr<FenceTime> frameReadyFence = mConsumer->getCurrentFenceTime();
David Sodmaneb085e02017-10-05 18:49:04 -0700320 if (frameReadyFence->isValid()) {
321 mFrameTracker.setFrameReadyFence(std::move(frameReadyFence));
322 } else {
323 // There was no fence for this frame, so assume that it was ready
324 // to be presented at the desired present time.
325 mFrameTracker.setFrameReadyTime(desiredPresentTime);
326 }
327
328 if (presentFence->isValid()) {
329 mFrameTracker.setActualPresentFence(std::shared_ptr<FenceTime>(presentFence));
330 } else {
331 // The HWC doesn't support present fences, so use the refresh
332 // timestamp instead.
333 mFrameTracker.setActualPresentTime(
334 mFlinger->getHwComposer().getRefreshTimestamp(HWC_DISPLAY_PRIMARY));
335 }
336
337 mFrameTracker.advanceFrame();
338 mFrameLatencyNeeded = false;
339 return true;
340}
341
342std::vector<OccupancyTracker::Segment> BufferLayer::getOccupancyHistory(bool forceFlush) {
343 std::vector<OccupancyTracker::Segment> history;
Chia-I Wub28c6742017-12-27 10:59:54 -0800344 status_t result = mConsumer->getOccupancyHistory(forceFlush, &history);
David Sodmaneb085e02017-10-05 18:49:04 -0700345 if (result != NO_ERROR) {
346 ALOGW("[%s] Failed to obtain occupancy history (%d)", mName.string(), result);
347 return {};
348 }
349 return history;
350}
351
352bool BufferLayer::getTransformToDisplayInverse() const {
Chia-I Wub28c6742017-12-27 10:59:54 -0800353 return mConsumer->getTransformToDisplayInverse();
David Sodmaneb085e02017-10-05 18:49:04 -0700354}
David Sodman0c69cad2017-08-21 12:12:51 -0700355
David Sodman0c69cad2017-08-21 12:12:51 -0700356void BufferLayer::releasePendingBuffer(nsecs_t dequeueReadyTime) {
Chia-I Wub28c6742017-12-27 10:59:54 -0800357 if (!mConsumer->releasePendingBuffer()) {
David Sodman0c69cad2017-08-21 12:12:51 -0700358 return;
359 }
360
361 auto releaseFenceTime =
Chia-I Wub28c6742017-12-27 10:59:54 -0800362 std::make_shared<FenceTime>(mConsumer->getPrevFinalReleaseFence());
David Sodman0c69cad2017-08-21 12:12:51 -0700363 mReleaseTimeline.updateSignalTimes();
364 mReleaseTimeline.push(releaseFenceTime);
365
366 Mutex::Autolock lock(mFrameEventHistoryMutex);
367 if (mPreviousFrameNumber != 0) {
368 mFrameEventHistory.addRelease(mPreviousFrameNumber, dequeueReadyTime,
369 std::move(releaseFenceTime));
370 }
371}
David Sodman0c69cad2017-08-21 12:12:51 -0700372
373Region BufferLayer::latchBuffer(bool& recomputeVisibleRegions, nsecs_t latchTime) {
374 ATRACE_CALL();
375
376 if (android_atomic_acquire_cas(true, false, &mSidebandStreamChanged) == 0) {
377 // mSidebandStreamChanged was true
Chia-I Wub28c6742017-12-27 10:59:54 -0800378 mSidebandStream = mConsumer->getSidebandStream();
David Sodman386c22e2017-11-09 16:34:46 -0800379 // replicated in LayerBE until FE/BE is ready to be synchronized
David Sodman0cc69182017-11-17 12:12:07 -0800380 getBE().compositionInfo.hwc.sidebandStream = mSidebandStream;
Peiyong Lin566a3b42018-01-09 18:22:43 -0800381 if (getBE().compositionInfo.hwc.sidebandStream != nullptr) {
David Sodman0c69cad2017-08-21 12:12:51 -0700382 setTransactionFlags(eTransactionNeeded);
383 mFlinger->setTransactionFlags(eTraversalNeeded);
384 }
385 recomputeVisibleRegions = true;
386
387 const State& s(getDrawingState());
388 return getTransform().transform(Region(Rect(s.active.w, s.active.h)));
389 }
390
391 Region outDirtyRegion;
392 if (mQueuedFrames <= 0 && !mAutoRefresh) {
393 return outDirtyRegion;
394 }
395
396 // if we've already called updateTexImage() without going through
397 // a composition step, we have to skip this layer at this point
398 // because we cannot call updateTeximage() without a corresponding
399 // compositionComplete() call.
400 // we'll trigger an update in onPreComposition().
401 if (mRefreshPending) {
402 return outDirtyRegion;
403 }
404
405 // If the head buffer's acquire fence hasn't signaled yet, return and
406 // try again later
407 if (!headFenceHasSignaled()) {
408 mFlinger->signalLayerUpdate();
409 return outDirtyRegion;
410 }
411
412 // Capture the old state of the layer for comparisons later
413 const State& s(getDrawingState());
414 const bool oldOpacity = isOpaque(s);
David Sodman0cc69182017-11-17 12:12:07 -0800415 sp<GraphicBuffer> oldBuffer = getBE().compositionInfo.mBuffer;
David Sodman0c69cad2017-08-21 12:12:51 -0700416
417 if (!allTransactionsSignaled()) {
418 mFlinger->signalLayerUpdate();
419 return outDirtyRegion;
420 }
421
422 // This boolean is used to make sure that SurfaceFlinger's shadow copy
423 // of the buffer queue isn't modified when the buffer queue is returning
424 // BufferItem's that weren't actually queued. This can happen in shared
425 // buffer mode.
426 bool queuedBuffer = false;
427 LayerRejecter r(mDrawingState, getCurrentState(), recomputeVisibleRegions,
David Sodman9eeae692017-11-02 10:53:32 -0700428 getProducerStickyTransform() != 0, mName.string(),
429 mOverrideScalingMode, mFreezeGeometryUpdates);
David Sodman0c69cad2017-08-21 12:12:51 -0700430 status_t updateResult =
Chia-I Wub28c6742017-12-27 10:59:54 -0800431 mConsumer->updateTexImage(&r, mFlinger->mPrimaryDispSync,
David Sodman9eeae692017-11-02 10:53:32 -0700432 &mAutoRefresh, &queuedBuffer,
433 mLastFrameNumberReceived);
David Sodman0c69cad2017-08-21 12:12:51 -0700434 if (updateResult == BufferQueue::PRESENT_LATER) {
435 // Producer doesn't want buffer to be displayed yet. Signal a
436 // layer update so we check again at the next opportunity.
437 mFlinger->signalLayerUpdate();
438 return outDirtyRegion;
Chia-I Wu0cb75ac2017-11-27 15:56:04 -0800439 } else if (updateResult == BufferLayerConsumer::BUFFER_REJECTED) {
David Sodman0c69cad2017-08-21 12:12:51 -0700440 // If the buffer has been rejected, remove it from the shadow queue
441 // and return early
442 if (queuedBuffer) {
443 Mutex::Autolock lock(mQueueItemLock);
444 mQueueItems.removeAt(0);
445 android_atomic_dec(&mQueuedFrames);
446 }
447 return outDirtyRegion;
448 } else if (updateResult != NO_ERROR || mUpdateTexImageFailed) {
449 // This can occur if something goes wrong when trying to create the
450 // EGLImage for this buffer. If this happens, the buffer has already
451 // been released, so we need to clean up the queue and bug out
452 // early.
453 if (queuedBuffer) {
454 Mutex::Autolock lock(mQueueItemLock);
455 mQueueItems.clear();
456 android_atomic_and(0, &mQueuedFrames);
457 }
458
459 // Once we have hit this state, the shadow queue may no longer
460 // correctly reflect the incoming BufferQueue's contents, so even if
461 // updateTexImage starts working, the only safe course of action is
462 // to continue to ignore updates.
463 mUpdateTexImageFailed = true;
464
465 return outDirtyRegion;
466 }
467
468 if (queuedBuffer) {
469 // Autolock scope
Chia-I Wub28c6742017-12-27 10:59:54 -0800470 auto currentFrameNumber = mConsumer->getFrameNumber();
David Sodman0c69cad2017-08-21 12:12:51 -0700471
472 Mutex::Autolock lock(mQueueItemLock);
473
474 // Remove any stale buffers that have been dropped during
475 // updateTexImage
476 while (mQueueItems[0].mFrameNumber != currentFrameNumber) {
477 mQueueItems.removeAt(0);
478 android_atomic_dec(&mQueuedFrames);
479 }
480
481 mQueueItems.removeAt(0);
482 }
483
484 // Decrement the queued-frames count. Signal another event if we
485 // have more frames pending.
David Sodman9eeae692017-11-02 10:53:32 -0700486 if ((queuedBuffer && android_atomic_dec(&mQueuedFrames) > 1) ||
487 mAutoRefresh) {
David Sodman0c69cad2017-08-21 12:12:51 -0700488 mFlinger->signalLayerUpdate();
489 }
490
491 // update the active buffer
David Sodman0cc69182017-11-17 12:12:07 -0800492 getBE().compositionInfo.mBuffer =
Chia-I Wub28c6742017-12-27 10:59:54 -0800493 mConsumer->getCurrentBuffer(&getBE().compositionInfo.mBufferSlot);
David Sodman5b4cffc2017-11-23 13:20:29 -0800494 // replicated in LayerBE until FE/BE is ready to be synchronized
David Sodman0cc69182017-11-17 12:12:07 -0800495 mActiveBuffer = getBE().compositionInfo.mBuffer;
Peiyong Lin566a3b42018-01-09 18:22:43 -0800496 if (getBE().compositionInfo.mBuffer == nullptr) {
David Sodman0c69cad2017-08-21 12:12:51 -0700497 // this can only happen if the very first buffer was rejected.
498 return outDirtyRegion;
499 }
500
501 mBufferLatched = true;
502 mPreviousFrameNumber = mCurrentFrameNumber;
Chia-I Wub28c6742017-12-27 10:59:54 -0800503 mCurrentFrameNumber = mConsumer->getFrameNumber();
David Sodman0c69cad2017-08-21 12:12:51 -0700504
505 {
506 Mutex::Autolock lock(mFrameEventHistoryMutex);
507 mFrameEventHistory.addLatch(mCurrentFrameNumber, latchTime);
David Sodman0c69cad2017-08-21 12:12:51 -0700508 }
509
510 mRefreshPending = true;
511 mFrameLatencyNeeded = true;
Peiyong Lin566a3b42018-01-09 18:22:43 -0800512 if (oldBuffer == nullptr) {
David Sodman0c69cad2017-08-21 12:12:51 -0700513 // the first time we receive a buffer, we need to trigger a
514 // geometry invalidation.
515 recomputeVisibleRegions = true;
516 }
517
Chia-I Wub28c6742017-12-27 10:59:54 -0800518 setDataSpace(mConsumer->getCurrentDataSpace());
David Sodman0c69cad2017-08-21 12:12:51 -0700519
Chia-I Wub28c6742017-12-27 10:59:54 -0800520 Rect crop(mConsumer->getCurrentCrop());
521 const uint32_t transform(mConsumer->getCurrentTransform());
522 const uint32_t scalingMode(mConsumer->getCurrentScalingMode());
David Sodman9eeae692017-11-02 10:53:32 -0700523 if ((crop != mCurrentCrop) ||
524 (transform != mCurrentTransform) ||
David Sodman0c69cad2017-08-21 12:12:51 -0700525 (scalingMode != mCurrentScalingMode)) {
526 mCurrentCrop = crop;
527 mCurrentTransform = transform;
528 mCurrentScalingMode = scalingMode;
529 recomputeVisibleRegions = true;
530 }
531
Peiyong Lin566a3b42018-01-09 18:22:43 -0800532 if (oldBuffer != nullptr) {
David Sodman0cc69182017-11-17 12:12:07 -0800533 uint32_t bufWidth = getBE().compositionInfo.mBuffer->getWidth();
534 uint32_t bufHeight = getBE().compositionInfo.mBuffer->getHeight();
David Sodman5b4cffc2017-11-23 13:20:29 -0800535 if (bufWidth != uint32_t(oldBuffer->width) ||
536 bufHeight != uint32_t(oldBuffer->height)) {
David Sodman0c69cad2017-08-21 12:12:51 -0700537 recomputeVisibleRegions = true;
538 }
539 }
540
David Sodman0cc69182017-11-17 12:12:07 -0800541 mCurrentOpacity = getOpacityForFormat(getBE().compositionInfo.mBuffer->format);
David Sodman0c69cad2017-08-21 12:12:51 -0700542 if (oldOpacity != isOpaque(s)) {
543 recomputeVisibleRegions = true;
544 }
545
546 // Remove any sync points corresponding to the buffer which was just
547 // latched
548 {
549 Mutex::Autolock lock(mLocalSyncPointMutex);
550 auto point = mLocalSyncPoints.begin();
551 while (point != mLocalSyncPoints.end()) {
552 if (!(*point)->frameIsAvailable() || !(*point)->transactionIsApplied()) {
553 // This sync point must have been added since we started
554 // latching. Don't drop it yet.
555 ++point;
556 continue;
557 }
558
559 if ((*point)->getFrameNumber() <= mCurrentFrameNumber) {
560 point = mLocalSyncPoints.erase(point);
561 } else {
562 ++point;
563 }
564 }
565 }
566
567 // FIXME: postedRegion should be dirty & bounds
568 Region dirtyRegion(Rect(s.active.w, s.active.h));
569
570 // transform the dirty region to window-manager space
571 outDirtyRegion = (getTransform().transform(dirtyRegion));
572
573 return outDirtyRegion;
574}
575
David Sodmaneb085e02017-10-05 18:49:04 -0700576void BufferLayer::setDefaultBufferSize(uint32_t w, uint32_t h) {
Chia-I Wub28c6742017-12-27 10:59:54 -0800577 mConsumer->setDefaultBufferSize(w, h);
David Sodmaneb085e02017-10-05 18:49:04 -0700578}
579
David Sodman0c69cad2017-08-21 12:12:51 -0700580void BufferLayer::setPerFrameData(const sp<const DisplayDevice>& displayDevice) {
581 // Apply this display's projection's viewport to the visible region
582 // before giving it to the HWC HAL.
583 const Transform& tr = displayDevice->getTransform();
584 const auto& viewport = displayDevice->getViewport();
585 Region visible = tr.transform(visibleRegion.intersect(viewport));
586 auto hwcId = displayDevice->getHwcDisplayId();
David Sodman6f65f3e2017-11-03 14:28:09 -0700587 auto& hwcInfo = getBE().mHwcLayers[hwcId];
David Sodman0c69cad2017-08-21 12:12:51 -0700588 auto& hwcLayer = hwcInfo.layer;
589 auto error = hwcLayer->setVisibleRegion(visible);
590 if (error != HWC2::Error::None) {
591 ALOGE("[%s] Failed to set visible region: %s (%d)", mName.string(),
592 to_string(error).c_str(), static_cast<int32_t>(error));
593 visible.dump(LOG_TAG);
594 }
595
596 error = hwcLayer->setSurfaceDamage(surfaceDamageRegion);
597 if (error != HWC2::Error::None) {
598 ALOGE("[%s] Failed to set surface damage: %s (%d)", mName.string(),
599 to_string(error).c_str(), static_cast<int32_t>(error));
600 surfaceDamageRegion.dump(LOG_TAG);
601 }
602
603 // Sideband layers
David Sodman0cc69182017-11-17 12:12:07 -0800604 if (getBE().compositionInfo.hwc.sidebandStream.get()) {
David Sodman0c69cad2017-08-21 12:12:51 -0700605 setCompositionType(hwcId, HWC2::Composition::Sideband);
606 ALOGV("[%s] Requesting Sideband composition", mName.string());
David Sodman0cc69182017-11-17 12:12:07 -0800607 error = hwcLayer->setSidebandStream(getBE().compositionInfo.hwc.sidebandStream->handle());
David Sodman0c69cad2017-08-21 12:12:51 -0700608 if (error != HWC2::Error::None) {
609 ALOGE("[%s] Failed to set sideband stream %p: %s (%d)", mName.string(),
David Sodman0cc69182017-11-17 12:12:07 -0800610 getBE().compositionInfo.hwc.sidebandStream->handle(), to_string(error).c_str(),
David Sodman9eeae692017-11-02 10:53:32 -0700611 static_cast<int32_t>(error));
David Sodman0c69cad2017-08-21 12:12:51 -0700612 }
613 return;
614 }
615
David Sodman0c69cad2017-08-21 12:12:51 -0700616 // Device or Cursor layers
617 if (mPotentialCursor) {
618 ALOGV("[%s] Requesting Cursor composition", mName.string());
619 setCompositionType(hwcId, HWC2::Composition::Cursor);
620 } else {
621 ALOGV("[%s] Requesting Device composition", mName.string());
622 setCompositionType(hwcId, HWC2::Composition::Device);
623 }
624
Peiyong Lin13170c82018-01-22 18:55:51 -0800625 ALOGV("setPerFrameData: dataspace = %d", mDrawingState.dataSpace);
626 error = hwcLayer->setDataspace(mDrawingState.dataSpace);
David Sodman0c69cad2017-08-21 12:12:51 -0700627 if (error != HWC2::Error::None) {
Peiyong Lin13170c82018-01-22 18:55:51 -0800628 ALOGE("[%s] Failed to set dataspace %d: %s (%d)", mName.string(), mDrawingState.dataSpace,
David Sodman0c69cad2017-08-21 12:12:51 -0700629 to_string(error).c_str(), static_cast<int32_t>(error));
630 }
631
632 uint32_t hwcSlot = 0;
633 sp<GraphicBuffer> hwcBuffer;
David Sodman0cc69182017-11-17 12:12:07 -0800634 hwcInfo.bufferCache.getHwcBuffer(getBE().compositionInfo.mBufferSlot,
635 getBE().compositionInfo.mBuffer, &hwcSlot, &hwcBuffer);
David Sodman0c69cad2017-08-21 12:12:51 -0700636
Chia-I Wub28c6742017-12-27 10:59:54 -0800637 auto acquireFence = mConsumer->getCurrentFence();
David Sodman0c69cad2017-08-21 12:12:51 -0700638 error = hwcLayer->setBuffer(hwcSlot, hwcBuffer, acquireFence);
639 if (error != HWC2::Error::None) {
David Sodman9eeae692017-11-02 10:53:32 -0700640 ALOGE("[%s] Failed to set buffer %p: %s (%d)", mName.string(),
David Sodman0cc69182017-11-17 12:12:07 -0800641 getBE().compositionInfo.mBuffer->handle, to_string(error).c_str(),
David Sodman9eeae692017-11-02 10:53:32 -0700642 static_cast<int32_t>(error));
David Sodman0c69cad2017-08-21 12:12:51 -0700643 }
644}
645
David Sodman41fdfc92017-11-06 16:09:56 -0800646bool BufferLayer::isOpaque(const Layer::State& s) const {
David Sodman0c69cad2017-08-21 12:12:51 -0700647 // if we don't have a buffer or sidebandStream yet, we're translucent regardless of the
648 // layer's opaque flag.
David Sodman0cc69182017-11-17 12:12:07 -0800649 if ((getBE().compositionInfo.hwc.sidebandStream == nullptr) && (getBE().compositionInfo.mBuffer == nullptr)) {
David Sodman0c69cad2017-08-21 12:12:51 -0700650 return false;
651 }
652
653 // if the layer has the opaque flag, then we're always opaque,
654 // otherwise we use the current buffer's format.
655 return ((s.flags & layer_state_t::eLayerOpaque) != 0) || mCurrentOpacity;
656}
657
658void BufferLayer::onFirstRef() {
659 // Creates a custom BufferQueue for SurfaceFlingerConsumer to use
660 sp<IGraphicBufferProducer> producer;
661 sp<IGraphicBufferConsumer> consumer;
662 BufferQueue::createBufferQueue(&producer, &consumer, true);
663 mProducer = new MonitoredProducer(producer, mFlinger, this);
Chia-I Wub28c6742017-12-27 10:59:54 -0800664 mConsumer = new BufferLayerConsumer(consumer,
Chia-I Wu9f2db772017-11-30 21:06:50 -0800665 mFlinger->getRenderEngine(), mTextureName, this);
Chia-I Wub28c6742017-12-27 10:59:54 -0800666 mConsumer->setConsumerUsageBits(getEffectiveUsage(0));
667 mConsumer->setContentsChangedListener(this);
668 mConsumer->setName(mName);
David Sodman0c69cad2017-08-21 12:12:51 -0700669
670 if (mFlinger->isLayerTripleBufferingDisabled()) {
671 mProducer->setMaxDequeuedBufferCount(2);
672 }
673
674 const sp<const DisplayDevice> hw(mFlinger->getDefaultDisplayDevice());
675 updateTransformHint(hw);
676}
677
678// ---------------------------------------------------------------------------
679// Interface implementation for SurfaceFlingerConsumer::ContentsChangedListener
680// ---------------------------------------------------------------------------
681
682void BufferLayer::onFrameAvailable(const BufferItem& item) {
683 // Add this buffer from our internal queue tracker
684 { // Autolock scope
685 Mutex::Autolock lock(mQueueItemLock);
686 mFlinger->mInterceptor.saveBufferUpdate(this, item.mGraphicBuffer->getWidth(),
687 item.mGraphicBuffer->getHeight(),
688 item.mFrameNumber);
689 // Reset the frame number tracker when we receive the first buffer after
690 // a frame number reset
691 if (item.mFrameNumber == 1) {
692 mLastFrameNumberReceived = 0;
693 }
694
695 // Ensure that callbacks are handled in order
696 while (item.mFrameNumber != mLastFrameNumberReceived + 1) {
David Sodman9eeae692017-11-02 10:53:32 -0700697 status_t result = mQueueItemCondition.waitRelative(mQueueItemLock,
698 ms2ns(500));
David Sodman0c69cad2017-08-21 12:12:51 -0700699 if (result != NO_ERROR) {
700 ALOGE("[%s] Timed out waiting on callback", mName.string());
701 }
702 }
703
704 mQueueItems.push_back(item);
705 android_atomic_inc(&mQueuedFrames);
706
707 // Wake up any pending callbacks
708 mLastFrameNumberReceived = item.mFrameNumber;
709 mQueueItemCondition.broadcast();
710 }
711
712 mFlinger->signalLayerUpdate();
713}
714
715void BufferLayer::onFrameReplaced(const BufferItem& item) {
716 { // Autolock scope
717 Mutex::Autolock lock(mQueueItemLock);
718
719 // Ensure that callbacks are handled in order
720 while (item.mFrameNumber != mLastFrameNumberReceived + 1) {
David Sodman9eeae692017-11-02 10:53:32 -0700721 status_t result = mQueueItemCondition.waitRelative(mQueueItemLock,
722 ms2ns(500));
David Sodman0c69cad2017-08-21 12:12:51 -0700723 if (result != NO_ERROR) {
724 ALOGE("[%s] Timed out waiting on callback", mName.string());
725 }
726 }
727
728 if (mQueueItems.empty()) {
729 ALOGE("Can't replace a frame on an empty queue");
730 return;
731 }
732 mQueueItems.editItemAt(mQueueItems.size() - 1) = item;
733
734 // Wake up any pending callbacks
735 mLastFrameNumberReceived = item.mFrameNumber;
736 mQueueItemCondition.broadcast();
737 }
738}
739
740void BufferLayer::onSidebandStreamChanged() {
741 if (android_atomic_release_cas(false, true, &mSidebandStreamChanged) == 0) {
742 // mSidebandStreamChanged was false
743 mFlinger->signalLayerUpdate();
744 }
745}
746
747bool BufferLayer::needsFiltering(const RenderArea& renderArea) const {
748 return mNeedsFiltering || renderArea.needsFiltering();
749}
750
751// As documented in libhardware header, formats in the range
752// 0x100 - 0x1FF are specific to the HAL implementation, and
753// are known to have no alpha channel
754// TODO: move definition for device-specific range into
755// hardware.h, instead of using hard-coded values here.
756#define HARDWARE_IS_DEVICE_FORMAT(f) ((f) >= 0x100 && (f) <= 0x1FF)
757
758bool BufferLayer::getOpacityForFormat(uint32_t format) {
759 if (HARDWARE_IS_DEVICE_FORMAT(format)) {
760 return true;
761 }
762 switch (format) {
763 case HAL_PIXEL_FORMAT_RGBA_8888:
764 case HAL_PIXEL_FORMAT_BGRA_8888:
765 case HAL_PIXEL_FORMAT_RGBA_FP16:
766 case HAL_PIXEL_FORMAT_RGBA_1010102:
767 return false;
768 }
769 // in all other case, we have no blending (also for unknown formats)
770 return true;
771}
772
David Sodman41fdfc92017-11-06 16:09:56 -0800773void BufferLayer::drawWithOpenGL(const RenderArea& renderArea, bool useIdentityTransform) const {
David Sodman0c69cad2017-08-21 12:12:51 -0700774 const State& s(getDrawingState());
775
David Sodman9eeae692017-11-02 10:53:32 -0700776 computeGeometry(renderArea, getBE().mMesh, useIdentityTransform);
David Sodman0c69cad2017-08-21 12:12:51 -0700777
778 /*
779 * NOTE: the way we compute the texture coordinates here produces
780 * different results than when we take the HWC path -- in the later case
781 * the "source crop" is rounded to texel boundaries.
782 * This can produce significantly different results when the texture
783 * is scaled by a large amount.
784 *
785 * The GL code below is more logical (imho), and the difference with
786 * HWC is due to a limitation of the HWC API to integers -- a question
787 * is suspend is whether we should ignore this problem or revert to
788 * GL composition when a buffer scaling is applied (maybe with some
789 * minimal value)? Or, we could make GL behave like HWC -- but this feel
790 * like more of a hack.
791 */
Dan Stoza80d61162017-12-20 15:57:52 -0800792 const Rect bounds{computeBounds()}; // Rounds from FloatRect
David Sodman0c69cad2017-08-21 12:12:51 -0700793
794 Transform t = getTransform();
Dan Stoza80d61162017-12-20 15:57:52 -0800795 Rect win = bounds;
David Sodman0c69cad2017-08-21 12:12:51 -0700796 if (!s.finalCrop.isEmpty()) {
797 win = t.transform(win);
798 if (!win.intersect(s.finalCrop, &win)) {
799 win.clear();
800 }
801 win = t.inverse().transform(win);
Dan Stoza80d61162017-12-20 15:57:52 -0800802 if (!win.intersect(bounds, &win)) {
David Sodman0c69cad2017-08-21 12:12:51 -0700803 win.clear();
804 }
805 }
806
807 float left = float(win.left) / float(s.active.w);
808 float top = float(win.top) / float(s.active.h);
809 float right = float(win.right) / float(s.active.w);
810 float bottom = float(win.bottom) / float(s.active.h);
811
812 // TODO: we probably want to generate the texture coords with the mesh
813 // here we assume that we only have 4 vertices
David Sodman9eeae692017-11-02 10:53:32 -0700814 Mesh::VertexArray<vec2> texCoords(getBE().mMesh.getTexCoordArray<vec2>());
David Sodman0c69cad2017-08-21 12:12:51 -0700815 texCoords[0] = vec2(left, 1.0f - top);
816 texCoords[1] = vec2(left, 1.0f - bottom);
817 texCoords[2] = vec2(right, 1.0f - bottom);
818 texCoords[3] = vec2(right, 1.0f - top);
819
820 RenderEngine& engine(mFlinger->getRenderEngine());
821 engine.setupLayerBlending(mPremultipliedAlpha, isOpaque(s), false /* disableTexture */,
822 getColor());
David Sodman0c69cad2017-08-21 12:12:51 -0700823 engine.setSourceDataSpace(mCurrentState.dataSpace);
Chia-I Wu5c6e4632018-01-11 08:54:38 -0800824
Chia-I Wu8d2651e2018-01-24 12:18:49 -0800825 if (mCurrentState.dataSpace == HAL_DATASPACE_BT2020_ITU_PQ &&
Chia-I Wu5c6e4632018-01-11 08:54:38 -0800826 mConsumer->getCurrentApi() == NATIVE_WINDOW_API_MEDIA &&
827 getBE().compositionInfo.mBuffer->getPixelFormat() == HAL_PIXEL_FORMAT_RGBA_1010102) {
828 engine.setSourceY410BT2020(true);
829 }
830
David Sodman9eeae692017-11-02 10:53:32 -0700831 engine.drawMesh(getBE().mMesh);
David Sodman0c69cad2017-08-21 12:12:51 -0700832 engine.disableBlending();
Chia-I Wu5c6e4632018-01-11 08:54:38 -0800833
834 engine.setSourceY410BT2020(false);
David Sodman0c69cad2017-08-21 12:12:51 -0700835}
836
837uint32_t BufferLayer::getProducerStickyTransform() const {
838 int producerStickyTransform = 0;
839 int ret = mProducer->query(NATIVE_WINDOW_STICKY_TRANSFORM, &producerStickyTransform);
840 if (ret != OK) {
841 ALOGW("%s: Error %s (%d) while querying window sticky transform.", __FUNCTION__,
842 strerror(-ret), ret);
843 return 0;
844 }
845 return static_cast<uint32_t>(producerStickyTransform);
846}
847
848bool BufferLayer::latchUnsignaledBuffers() {
849 static bool propertyLoaded = false;
850 static bool latch = false;
851 static std::mutex mutex;
852 std::lock_guard<std::mutex> lock(mutex);
853 if (!propertyLoaded) {
854 char value[PROPERTY_VALUE_MAX] = {};
855 property_get("debug.sf.latch_unsignaled", value, "0");
856 latch = atoi(value);
857 propertyLoaded = true;
858 }
859 return latch;
860}
861
862uint64_t BufferLayer::getHeadFrameNumber() const {
863 Mutex::Autolock lock(mQueueItemLock);
864 if (!mQueueItems.empty()) {
865 return mQueueItems[0].mFrameNumber;
866 } else {
867 return mCurrentFrameNumber;
868 }
869}
870
871bool BufferLayer::headFenceHasSignaled() const {
David Sodman0c69cad2017-08-21 12:12:51 -0700872 if (latchUnsignaledBuffers()) {
873 return true;
874 }
875
876 Mutex::Autolock lock(mQueueItemLock);
877 if (mQueueItems.empty()) {
878 return true;
879 }
880 if (mQueueItems[0].mIsDroppable) {
881 // Even though this buffer's fence may not have signaled yet, it could
882 // be replaced by another buffer before it has a chance to, which means
883 // that it's possible to get into a situation where a buffer is never
884 // able to be latched. To avoid this, grab this buffer anyway.
885 return true;
886 }
David Sodman9eeae692017-11-02 10:53:32 -0700887 return mQueueItems[0].mFenceTime->getSignalTime() !=
888 Fence::SIGNAL_TIME_PENDING;
David Sodman0c69cad2017-08-21 12:12:51 -0700889}
890
891uint32_t BufferLayer::getEffectiveScalingMode() const {
892 if (mOverrideScalingMode >= 0) {
893 return mOverrideScalingMode;
894 }
895 return mCurrentScalingMode;
896}
897
898// ----------------------------------------------------------------------------
899// transaction
900// ----------------------------------------------------------------------------
901
902void BufferLayer::notifyAvailableFrames() {
903 auto headFrameNumber = getHeadFrameNumber();
904 bool headFenceSignaled = headFenceHasSignaled();
905 Mutex::Autolock lock(mLocalSyncPointMutex);
906 for (auto& point : mLocalSyncPoints) {
907 if (headFrameNumber >= point->getFrameNumber() && headFenceSignaled) {
908 point->setFrameAvailable();
909 }
910 }
911}
912
913sp<IGraphicBufferProducer> BufferLayer::getProducer() const {
914 return mProducer;
915}
916
917// ---------------------------------------------------------------------------
918// h/w composer set-up
919// ---------------------------------------------------------------------------
920
921bool BufferLayer::allTransactionsSignaled() {
922 auto headFrameNumber = getHeadFrameNumber();
923 bool matchingFramesFound = false;
924 bool allTransactionsApplied = true;
925 Mutex::Autolock lock(mLocalSyncPointMutex);
926
927 for (auto& point : mLocalSyncPoints) {
928 if (point->getFrameNumber() > headFrameNumber) {
929 break;
930 }
931 matchingFramesFound = true;
932
933 if (!point->frameIsAvailable()) {
934 // We haven't notified the remote layer that the frame for
935 // this point is available yet. Notify it now, and then
936 // abort this attempt to latch.
937 point->setFrameAvailable();
938 allTransactionsApplied = false;
939 break;
940 }
941
942 allTransactionsApplied = allTransactionsApplied && point->transactionIsApplied();
943 }
944 return !matchingFramesFound || allTransactionsApplied;
945}
946
947} // namespace android
948
949#if defined(__gl_h_)
950#error "don't include gl/gl.h in this file"
951#endif
952
953#if defined(__gl2_h_)
954#error "don't include gl2/gl2.h in this file"
955#endif