blob: f37f0abaca0003539c374a54cd5588f7b15658e5 [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),
David Sodmaneb085e02017-10-05 18:49:04 -070056 mSurfaceFlingerConsumer(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() {
78 sp<Client> c(mClientRef.promote());
79 if (c != 0) {
80 c->detachLayer(this);
81 }
82
83 for (auto& point : mRemoteSyncPoints) {
84 point->setTransactionApplied();
85 }
86 for (auto& point : mLocalSyncPoints) {
87 point->setFrameAvailable();
88 }
89 mFlinger->deleteTextureAsync(mTextureName);
90
David Sodman0c69cad2017-08-21 12:12:51 -070091 if (!mHwcLayers.empty()) {
92 ALOGE("Found stale hardware composer layers when destroying "
93 "surface flinger layer %s",
94 mName.string());
95 destroyAllHwcLayers();
96 }
David Sodman0c69cad2017-08-21 12:12:51 -070097}
98
David Sodmaneb085e02017-10-05 18:49:04 -070099void BufferLayer::useSurfaceDamage() {
100 if (mFlinger->mForceFullDamage) {
101 surfaceDamageRegion = Region::INVALID_REGION;
102 } else {
103 surfaceDamageRegion = mSurfaceFlingerConsumer->getSurfaceDamage();
104 }
105}
106
107void BufferLayer::useEmptyDamage() {
108 surfaceDamageRegion.clear();
109}
110
David Sodman41fdfc92017-11-06 16:09:56 -0800111bool BufferLayer::isProtected() const {
David Sodman0c69cad2017-08-21 12:12:51 -0700112 const sp<GraphicBuffer>& activeBuffer(mActiveBuffer);
David Sodman41fdfc92017-11-06 16:09:56 -0800113 return (activeBuffer != 0) && (activeBuffer->getUsage() & GRALLOC_USAGE_PROTECTED);
David Sodman0c69cad2017-08-21 12:12:51 -0700114}
115
116bool BufferLayer::isVisible() const {
117 return !(isHiddenByPolicy()) && getAlpha() > 0.0f &&
David Sodman386c22e2017-11-09 16:34:46 -0800118 (mActiveBuffer != NULL || getBE().mSidebandStream != NULL);
David Sodman0c69cad2017-08-21 12:12:51 -0700119}
120
121bool BufferLayer::isFixedSize() const {
122 return getEffectiveScalingMode() != NATIVE_WINDOW_SCALING_MODE_FREEZE;
123}
124
125status_t BufferLayer::setBuffers(uint32_t w, uint32_t h, PixelFormat format, uint32_t flags) {
126 uint32_t const maxSurfaceDims =
127 min(mFlinger->getMaxTextureSize(), mFlinger->getMaxViewportDims());
128
129 // never allow a surface larger than what our underlying GL implementation
130 // can handle.
131 if ((uint32_t(w) > maxSurfaceDims) || (uint32_t(h) > maxSurfaceDims)) {
132 ALOGE("dimensions too large %u x %u", uint32_t(w), uint32_t(h));
133 return BAD_VALUE;
134 }
135
136 mFormat = format;
137
138 mPotentialCursor = (flags & ISurfaceComposerClient::eCursorWindow) ? true : false;
139 mProtectedByApp = (flags & ISurfaceComposerClient::eProtectedByApp) ? true : false;
140 mCurrentOpacity = getOpacityForFormat(format);
141
142 mSurfaceFlingerConsumer->setDefaultBufferSize(w, h);
143 mSurfaceFlingerConsumer->setDefaultBufferFormat(format);
144 mSurfaceFlingerConsumer->setConsumerUsageBits(getEffectiveUsage(0));
145
146 return NO_ERROR;
147}
148
149static constexpr mat4 inverseOrientation(uint32_t transform) {
David Sodman41fdfc92017-11-06 16:09:56 -0800150 const mat4 flipH(-1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1);
151 const mat4 flipV(1, 0, 0, 0, 0, -1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1);
152 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 -0700153 mat4 tr;
154
155 if (transform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
156 tr = tr * rot90;
157 }
158 if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_H) {
159 tr = tr * flipH;
160 }
161 if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_V) {
162 tr = tr * flipV;
163 }
164 return inverse(tr);
165}
166
167/*
168 * onDraw will draw the current layer onto the presentable buffer
169 */
170void BufferLayer::onDraw(const RenderArea& renderArea, const Region& clip,
171 bool useIdentityTransform) const {
172 ATRACE_CALL();
173
174 if (CC_UNLIKELY(mActiveBuffer == 0)) {
175 // the texture has not been created yet, this Layer has
176 // in fact never been drawn into. This happens frequently with
177 // SurfaceView because the WindowManager can't know when the client
178 // has drawn the first time.
179
180 // If there is nothing under us, we paint the screen in black, otherwise
181 // we just skip this update.
182
183 // figure out if there is something below us
184 Region under;
185 bool finished = false;
186 mFlinger->mDrawingState.traverseInZOrder([&](Layer* layer) {
187 if (finished || layer == static_cast<BufferLayer const*>(this)) {
188 finished = true;
189 return;
190 }
191 under.orSelf(renderArea.getTransform().transform(layer->visibleRegion));
192 });
193 // if not everything below us is covered, we plug the holes!
194 Region holes(clip.subtract(under));
195 if (!holes.isEmpty()) {
196 clearWithOpenGL(renderArea, 0, 0, 0, 1);
197 }
198 return;
199 }
200
201 // Bind the current buffer to the GL texture, and wait for it to be
202 // ready for us to draw into.
203 status_t err = mSurfaceFlingerConsumer->bindTextureImage();
204 if (err != NO_ERROR) {
205 ALOGW("onDraw: bindTextureImage failed (err=%d)", err);
206 // Go ahead and draw the buffer anyway; no matter what we do the screen
207 // is probably going to have something visibly wrong.
208 }
209
210 bool blackOutLayer = isProtected() || (isSecure() && !renderArea.isSecure());
211
212 RenderEngine& engine(mFlinger->getRenderEngine());
213
214 if (!blackOutLayer) {
215 // TODO: we could be more subtle with isFixedSize()
216 const bool useFiltering = getFiltering() || needsFiltering(renderArea) || isFixedSize();
217
218 // Query the texture matrix given our current filtering mode.
219 float textureMatrix[16];
220 mSurfaceFlingerConsumer->setFilteringEnabled(useFiltering);
221 mSurfaceFlingerConsumer->getTransformMatrix(textureMatrix);
222
223 if (getTransformToDisplayInverse()) {
224 /*
225 * the code below applies the primary display's inverse transform to
226 * the texture transform
227 */
228 uint32_t transform = DisplayDevice::getPrimaryDisplayOrientationTransform();
229 mat4 tr = inverseOrientation(transform);
230
231 /**
232 * TODO(b/36727915): This is basically a hack.
233 *
234 * Ensure that regardless of the parent transformation,
235 * this buffer is always transformed from native display
236 * orientation to display orientation. For example, in the case
237 * of a camera where the buffer remains in native orientation,
238 * we want the pixels to always be upright.
239 */
240 sp<Layer> p = mDrawingParent.promote();
241 if (p != nullptr) {
242 const auto parentTransform = p->getTransform();
243 tr = tr * inverseOrientation(parentTransform.getOrientation());
244 }
245
246 // and finally apply it to the original texture matrix
247 const mat4 texTransform(mat4(static_cast<const float*>(textureMatrix)) * tr);
248 memcpy(textureMatrix, texTransform.asArray(), sizeof(textureMatrix));
249 }
250
251 // Set things up for texturing.
David Sodman9eeae692017-11-02 10:53:32 -0700252 mTexture.setDimensions(mActiveBuffer->getWidth(),
253 mActiveBuffer->getHeight());
David Sodman0c69cad2017-08-21 12:12:51 -0700254 mTexture.setFiltering(useFiltering);
255 mTexture.setMatrix(textureMatrix);
256
257 engine.setupLayerTexturing(mTexture);
258 } else {
259 engine.setupLayerBlackedOut();
260 }
261 drawWithOpenGL(renderArea, useIdentityTransform);
262 engine.disableTexturing();
263}
264
David Sodmaneb085e02017-10-05 18:49:04 -0700265void BufferLayer::onLayerDisplayed(const sp<Fence>& releaseFence) {
David Sodmaneb085e02017-10-05 18:49:04 -0700266 mSurfaceFlingerConsumer->setReleaseFence(releaseFence);
267}
David Sodmaneb085e02017-10-05 18:49:04 -0700268
269void BufferLayer::abandon() {
270 mSurfaceFlingerConsumer->abandon();
271}
272
273bool BufferLayer::shouldPresentNow(const DispSync& dispSync) const {
274 if (mSidebandStreamChanged || mAutoRefresh) {
275 return true;
276 }
277
278 Mutex::Autolock lock(mQueueItemLock);
279 if (mQueueItems.empty()) {
280 return false;
281 }
282 auto timestamp = mQueueItems[0].mTimestamp;
283 nsecs_t expectedPresent = mSurfaceFlingerConsumer->computeExpectedPresent(dispSync);
284
285 // Ignore timestamps more than a second in the future
286 bool isPlausible = timestamp < (expectedPresent + s2ns(1));
287 ALOGW_IF(!isPlausible,
288 "[%s] Timestamp %" PRId64 " seems implausible "
289 "relative to expectedPresent %" PRId64,
290 mName.string(), timestamp, expectedPresent);
291
292 bool isDue = timestamp < expectedPresent;
293 return isDue || !isPlausible;
294}
295
296void BufferLayer::setTransformHint(uint32_t orientation) const {
297 mSurfaceFlingerConsumer->setTransformHint(orientation);
298}
299
David Sodman0c69cad2017-08-21 12:12:51 -0700300bool BufferLayer::onPreComposition(nsecs_t refreshStartTime) {
301 if (mBufferLatched) {
302 Mutex::Autolock lock(mFrameEventHistoryMutex);
David Sodman9eeae692017-11-02 10:53:32 -0700303 mFrameEventHistory.addPreComposition(mCurrentFrameNumber,
304 refreshStartTime);
David Sodman0c69cad2017-08-21 12:12:51 -0700305 }
306 mRefreshPending = false;
David Sodman9eeae692017-11-02 10:53:32 -0700307 return mQueuedFrames > 0 || mSidebandStreamChanged ||
308 mAutoRefresh;
David Sodman0c69cad2017-08-21 12:12:51 -0700309}
David Sodmaneb085e02017-10-05 18:49:04 -0700310bool BufferLayer::onPostComposition(const std::shared_ptr<FenceTime>& glDoneFence,
311 const std::shared_ptr<FenceTime>& presentFence,
312 const CompositorTiming& compositorTiming) {
313 // mFrameLatencyNeeded is true when a new frame was latched for the
314 // composition.
315 if (!mFrameLatencyNeeded) return false;
316
317 // Update mFrameEventHistory.
318 {
319 Mutex::Autolock lock(mFrameEventHistoryMutex);
David Sodman9eeae692017-11-02 10:53:32 -0700320 mFrameEventHistory.addPostComposition(mCurrentFrameNumber, glDoneFence,
321 presentFence, compositorTiming);
David Sodmaneb085e02017-10-05 18:49:04 -0700322 }
323
324 // Update mFrameTracker.
325 nsecs_t desiredPresentTime = mSurfaceFlingerConsumer->getTimestamp();
326 mFrameTracker.setDesiredPresentTime(desiredPresentTime);
327
328 std::shared_ptr<FenceTime> frameReadyFence = mSurfaceFlingerConsumer->getCurrentFenceTime();
329 if (frameReadyFence->isValid()) {
330 mFrameTracker.setFrameReadyFence(std::move(frameReadyFence));
331 } else {
332 // There was no fence for this frame, so assume that it was ready
333 // to be presented at the desired present time.
334 mFrameTracker.setFrameReadyTime(desiredPresentTime);
335 }
336
337 if (presentFence->isValid()) {
338 mFrameTracker.setActualPresentFence(std::shared_ptr<FenceTime>(presentFence));
339 } else {
340 // The HWC doesn't support present fences, so use the refresh
341 // timestamp instead.
342 mFrameTracker.setActualPresentTime(
343 mFlinger->getHwComposer().getRefreshTimestamp(HWC_DISPLAY_PRIMARY));
344 }
345
346 mFrameTracker.advanceFrame();
347 mFrameLatencyNeeded = false;
348 return true;
349}
350
351std::vector<OccupancyTracker::Segment> BufferLayer::getOccupancyHistory(bool forceFlush) {
352 std::vector<OccupancyTracker::Segment> history;
353 status_t result = mSurfaceFlingerConsumer->getOccupancyHistory(forceFlush, &history);
354 if (result != NO_ERROR) {
355 ALOGW("[%s] Failed to obtain occupancy history (%d)", mName.string(), result);
356 return {};
357 }
358 return history;
359}
360
361bool BufferLayer::getTransformToDisplayInverse() const {
362 return mSurfaceFlingerConsumer->getTransformToDisplayInverse();
363}
David Sodman0c69cad2017-08-21 12:12:51 -0700364
David Sodman0c69cad2017-08-21 12:12:51 -0700365void BufferLayer::releasePendingBuffer(nsecs_t dequeueReadyTime) {
366 if (!mSurfaceFlingerConsumer->releasePendingBuffer()) {
367 return;
368 }
369
370 auto releaseFenceTime =
371 std::make_shared<FenceTime>(mSurfaceFlingerConsumer->getPrevFinalReleaseFence());
372 mReleaseTimeline.updateSignalTimes();
373 mReleaseTimeline.push(releaseFenceTime);
374
375 Mutex::Autolock lock(mFrameEventHistoryMutex);
376 if (mPreviousFrameNumber != 0) {
377 mFrameEventHistory.addRelease(mPreviousFrameNumber, dequeueReadyTime,
378 std::move(releaseFenceTime));
379 }
380}
David Sodman0c69cad2017-08-21 12:12:51 -0700381
382Region BufferLayer::latchBuffer(bool& recomputeVisibleRegions, nsecs_t latchTime) {
383 ATRACE_CALL();
384
385 if (android_atomic_acquire_cas(true, false, &mSidebandStreamChanged) == 0) {
386 // mSidebandStreamChanged was true
387 mSidebandStream = mSurfaceFlingerConsumer->getSidebandStream();
David Sodman386c22e2017-11-09 16:34:46 -0800388 // replicated in LayerBE until FE/BE is ready to be synchronized
389 getBE().mSidebandStream = mSidebandStream;
390 if (getBE().mSidebandStream != NULL) {
David Sodman0c69cad2017-08-21 12:12:51 -0700391 setTransactionFlags(eTransactionNeeded);
392 mFlinger->setTransactionFlags(eTraversalNeeded);
393 }
394 recomputeVisibleRegions = true;
395
396 const State& s(getDrawingState());
397 return getTransform().transform(Region(Rect(s.active.w, s.active.h)));
398 }
399
400 Region outDirtyRegion;
401 if (mQueuedFrames <= 0 && !mAutoRefresh) {
402 return outDirtyRegion;
403 }
404
405 // if we've already called updateTexImage() without going through
406 // a composition step, we have to skip this layer at this point
407 // because we cannot call updateTeximage() without a corresponding
408 // compositionComplete() call.
409 // we'll trigger an update in onPreComposition().
410 if (mRefreshPending) {
411 return outDirtyRegion;
412 }
413
414 // If the head buffer's acquire fence hasn't signaled yet, return and
415 // try again later
416 if (!headFenceHasSignaled()) {
417 mFlinger->signalLayerUpdate();
418 return outDirtyRegion;
419 }
420
421 // Capture the old state of the layer for comparisons later
422 const State& s(getDrawingState());
423 const bool oldOpacity = isOpaque(s);
424 sp<GraphicBuffer> oldActiveBuffer = mActiveBuffer;
425
426 if (!allTransactionsSignaled()) {
427 mFlinger->signalLayerUpdate();
428 return outDirtyRegion;
429 }
430
431 // This boolean is used to make sure that SurfaceFlinger's shadow copy
432 // of the buffer queue isn't modified when the buffer queue is returning
433 // BufferItem's that weren't actually queued. This can happen in shared
434 // buffer mode.
435 bool queuedBuffer = false;
436 LayerRejecter r(mDrawingState, getCurrentState(), recomputeVisibleRegions,
David Sodman9eeae692017-11-02 10:53:32 -0700437 getProducerStickyTransform() != 0, mName.string(),
438 mOverrideScalingMode, mFreezeGeometryUpdates);
David Sodman0c69cad2017-08-21 12:12:51 -0700439 status_t updateResult =
David Sodman9eeae692017-11-02 10:53:32 -0700440 mSurfaceFlingerConsumer->updateTexImage(&r, mFlinger->mPrimaryDispSync,
441 &mAutoRefresh, &queuedBuffer,
442 mLastFrameNumberReceived);
David Sodman0c69cad2017-08-21 12:12:51 -0700443 if (updateResult == BufferQueue::PRESENT_LATER) {
444 // Producer doesn't want buffer to be displayed yet. Signal a
445 // layer update so we check again at the next opportunity.
446 mFlinger->signalLayerUpdate();
447 return outDirtyRegion;
448 } else if (updateResult == SurfaceFlingerConsumer::BUFFER_REJECTED) {
449 // If the buffer has been rejected, remove it from the shadow queue
450 // and return early
451 if (queuedBuffer) {
452 Mutex::Autolock lock(mQueueItemLock);
453 mQueueItems.removeAt(0);
454 android_atomic_dec(&mQueuedFrames);
455 }
456 return outDirtyRegion;
457 } else if (updateResult != NO_ERROR || mUpdateTexImageFailed) {
458 // This can occur if something goes wrong when trying to create the
459 // EGLImage for this buffer. If this happens, the buffer has already
460 // been released, so we need to clean up the queue and bug out
461 // early.
462 if (queuedBuffer) {
463 Mutex::Autolock lock(mQueueItemLock);
464 mQueueItems.clear();
465 android_atomic_and(0, &mQueuedFrames);
466 }
467
468 // Once we have hit this state, the shadow queue may no longer
469 // correctly reflect the incoming BufferQueue's contents, so even if
470 // updateTexImage starts working, the only safe course of action is
471 // to continue to ignore updates.
472 mUpdateTexImageFailed = true;
473
474 return outDirtyRegion;
475 }
476
477 if (queuedBuffer) {
478 // Autolock scope
479 auto currentFrameNumber = mSurfaceFlingerConsumer->getFrameNumber();
480
481 Mutex::Autolock lock(mQueueItemLock);
482
483 // Remove any stale buffers that have been dropped during
484 // updateTexImage
485 while (mQueueItems[0].mFrameNumber != currentFrameNumber) {
486 mQueueItems.removeAt(0);
487 android_atomic_dec(&mQueuedFrames);
488 }
489
490 mQueueItems.removeAt(0);
491 }
492
493 // Decrement the queued-frames count. Signal another event if we
494 // have more frames pending.
David Sodman9eeae692017-11-02 10:53:32 -0700495 if ((queuedBuffer && android_atomic_dec(&mQueuedFrames) > 1) ||
496 mAutoRefresh) {
David Sodman0c69cad2017-08-21 12:12:51 -0700497 mFlinger->signalLayerUpdate();
498 }
499
500 // update the active buffer
David Sodman9eeae692017-11-02 10:53:32 -0700501 mActiveBuffer =
502 mSurfaceFlingerConsumer->getCurrentBuffer(&mActiveBufferSlot);
David Sodman0c69cad2017-08-21 12:12:51 -0700503 if (mActiveBuffer == NULL) {
504 // this can only happen if the very first buffer was rejected.
505 return outDirtyRegion;
506 }
507
508 mBufferLatched = true;
509 mPreviousFrameNumber = mCurrentFrameNumber;
510 mCurrentFrameNumber = mSurfaceFlingerConsumer->getFrameNumber();
511
512 {
513 Mutex::Autolock lock(mFrameEventHistoryMutex);
514 mFrameEventHistory.addLatch(mCurrentFrameNumber, latchTime);
David Sodman0c69cad2017-08-21 12:12:51 -0700515 }
516
517 mRefreshPending = true;
518 mFrameLatencyNeeded = true;
519 if (oldActiveBuffer == NULL) {
520 // the first time we receive a buffer, we need to trigger a
521 // geometry invalidation.
522 recomputeVisibleRegions = true;
523 }
524
525 setDataSpace(mSurfaceFlingerConsumer->getCurrentDataSpace());
526
527 Rect crop(mSurfaceFlingerConsumer->getCurrentCrop());
528 const uint32_t transform(mSurfaceFlingerConsumer->getCurrentTransform());
529 const uint32_t scalingMode(mSurfaceFlingerConsumer->getCurrentScalingMode());
David Sodman9eeae692017-11-02 10:53:32 -0700530 if ((crop != mCurrentCrop) ||
531 (transform != mCurrentTransform) ||
David Sodman0c69cad2017-08-21 12:12:51 -0700532 (scalingMode != mCurrentScalingMode)) {
533 mCurrentCrop = crop;
534 mCurrentTransform = transform;
535 mCurrentScalingMode = scalingMode;
536 recomputeVisibleRegions = true;
537 }
538
539 if (oldActiveBuffer != NULL) {
540 uint32_t bufWidth = mActiveBuffer->getWidth();
541 uint32_t bufHeight = mActiveBuffer->getHeight();
542 if (bufWidth != uint32_t(oldActiveBuffer->width) ||
543 bufHeight != uint32_t(oldActiveBuffer->height)) {
544 recomputeVisibleRegions = true;
545 }
546 }
547
548 mCurrentOpacity = getOpacityForFormat(mActiveBuffer->format);
549 if (oldOpacity != isOpaque(s)) {
550 recomputeVisibleRegions = true;
551 }
552
553 // Remove any sync points corresponding to the buffer which was just
554 // latched
555 {
556 Mutex::Autolock lock(mLocalSyncPointMutex);
557 auto point = mLocalSyncPoints.begin();
558 while (point != mLocalSyncPoints.end()) {
559 if (!(*point)->frameIsAvailable() || !(*point)->transactionIsApplied()) {
560 // This sync point must have been added since we started
561 // latching. Don't drop it yet.
562 ++point;
563 continue;
564 }
565
566 if ((*point)->getFrameNumber() <= mCurrentFrameNumber) {
567 point = mLocalSyncPoints.erase(point);
568 } else {
569 ++point;
570 }
571 }
572 }
573
574 // FIXME: postedRegion should be dirty & bounds
575 Region dirtyRegion(Rect(s.active.w, s.active.h));
576
577 // transform the dirty region to window-manager space
578 outDirtyRegion = (getTransform().transform(dirtyRegion));
579
580 return outDirtyRegion;
581}
582
David Sodmaneb085e02017-10-05 18:49:04 -0700583void BufferLayer::setDefaultBufferSize(uint32_t w, uint32_t h) {
584 mSurfaceFlingerConsumer->setDefaultBufferSize(w, h);
585}
586
David Sodman0c69cad2017-08-21 12:12:51 -0700587void BufferLayer::setPerFrameData(const sp<const DisplayDevice>& displayDevice) {
588 // Apply this display's projection's viewport to the visible region
589 // before giving it to the HWC HAL.
590 const Transform& tr = displayDevice->getTransform();
591 const auto& viewport = displayDevice->getViewport();
592 Region visible = tr.transform(visibleRegion.intersect(viewport));
593 auto hwcId = displayDevice->getHwcDisplayId();
594 auto& hwcInfo = mHwcLayers[hwcId];
595 auto& hwcLayer = hwcInfo.layer;
596 auto error = hwcLayer->setVisibleRegion(visible);
597 if (error != HWC2::Error::None) {
598 ALOGE("[%s] Failed to set visible region: %s (%d)", mName.string(),
599 to_string(error).c_str(), static_cast<int32_t>(error));
600 visible.dump(LOG_TAG);
601 }
602
603 error = hwcLayer->setSurfaceDamage(surfaceDamageRegion);
604 if (error != HWC2::Error::None) {
605 ALOGE("[%s] Failed to set surface damage: %s (%d)", mName.string(),
606 to_string(error).c_str(), static_cast<int32_t>(error));
607 surfaceDamageRegion.dump(LOG_TAG);
608 }
609
610 // Sideband layers
David Sodman386c22e2017-11-09 16:34:46 -0800611 if (getBE().mSidebandStream.get()) {
David Sodman0c69cad2017-08-21 12:12:51 -0700612 setCompositionType(hwcId, HWC2::Composition::Sideband);
613 ALOGV("[%s] Requesting Sideband composition", mName.string());
David Sodman386c22e2017-11-09 16:34:46 -0800614 error = hwcLayer->setSidebandStream(getBE().mSidebandStream->handle());
David Sodman0c69cad2017-08-21 12:12:51 -0700615 if (error != HWC2::Error::None) {
616 ALOGE("[%s] Failed to set sideband stream %p: %s (%d)", mName.string(),
David Sodman386c22e2017-11-09 16:34:46 -0800617 getBE().mSidebandStream->handle(), to_string(error).c_str(),
David Sodman9eeae692017-11-02 10:53:32 -0700618 static_cast<int32_t>(error));
David Sodman0c69cad2017-08-21 12:12:51 -0700619 }
620 return;
621 }
622
David Sodman0c69cad2017-08-21 12:12:51 -0700623 // Device or Cursor layers
624 if (mPotentialCursor) {
625 ALOGV("[%s] Requesting Cursor composition", mName.string());
626 setCompositionType(hwcId, HWC2::Composition::Cursor);
627 } else {
628 ALOGV("[%s] Requesting Device composition", mName.string());
629 setCompositionType(hwcId, HWC2::Composition::Device);
630 }
631
632 ALOGV("setPerFrameData: dataspace = %d", mCurrentState.dataSpace);
633 error = hwcLayer->setDataspace(mCurrentState.dataSpace);
634 if (error != HWC2::Error::None) {
635 ALOGE("[%s] Failed to set dataspace %d: %s (%d)", mName.string(), mCurrentState.dataSpace,
636 to_string(error).c_str(), static_cast<int32_t>(error));
637 }
638
639 uint32_t hwcSlot = 0;
640 sp<GraphicBuffer> hwcBuffer;
David Sodman9eeae692017-11-02 10:53:32 -0700641 hwcInfo.bufferCache.getHwcBuffer(mActiveBufferSlot,
642 mActiveBuffer, &hwcSlot, &hwcBuffer);
David Sodman0c69cad2017-08-21 12:12:51 -0700643
644 auto acquireFence = mSurfaceFlingerConsumer->getCurrentFence();
645 error = hwcLayer->setBuffer(hwcSlot, hwcBuffer, acquireFence);
646 if (error != HWC2::Error::None) {
David Sodman9eeae692017-11-02 10:53:32 -0700647 ALOGE("[%s] Failed to set buffer %p: %s (%d)", mName.string(),
648 mActiveBuffer->handle, to_string(error).c_str(),
649 static_cast<int32_t>(error));
David Sodman0c69cad2017-08-21 12:12:51 -0700650 }
651}
652
David Sodman41fdfc92017-11-06 16:09:56 -0800653bool BufferLayer::isOpaque(const Layer::State& s) const {
David Sodman0c69cad2017-08-21 12:12:51 -0700654 // if we don't have a buffer or sidebandStream yet, we're translucent regardless of the
655 // layer's opaque flag.
David Sodman386c22e2017-11-09 16:34:46 -0800656 if ((getBE().mSidebandStream == nullptr) && (mActiveBuffer == nullptr)) {
David Sodman0c69cad2017-08-21 12:12:51 -0700657 return false;
658 }
659
660 // if the layer has the opaque flag, then we're always opaque,
661 // otherwise we use the current buffer's format.
662 return ((s.flags & layer_state_t::eLayerOpaque) != 0) || mCurrentOpacity;
663}
664
665void BufferLayer::onFirstRef() {
666 // Creates a custom BufferQueue for SurfaceFlingerConsumer to use
667 sp<IGraphicBufferProducer> producer;
668 sp<IGraphicBufferConsumer> consumer;
669 BufferQueue::createBufferQueue(&producer, &consumer, true);
670 mProducer = new MonitoredProducer(producer, mFlinger, this);
671 mSurfaceFlingerConsumer = new SurfaceFlingerConsumer(consumer, mTextureName, this);
672 mSurfaceFlingerConsumer->setConsumerUsageBits(getEffectiveUsage(0));
673 mSurfaceFlingerConsumer->setContentsChangedListener(this);
674 mSurfaceFlingerConsumer->setName(mName);
675
676 if (mFlinger->isLayerTripleBufferingDisabled()) {
677 mProducer->setMaxDequeuedBufferCount(2);
678 }
679
680 const sp<const DisplayDevice> hw(mFlinger->getDefaultDisplayDevice());
681 updateTransformHint(hw);
682}
683
684// ---------------------------------------------------------------------------
685// Interface implementation for SurfaceFlingerConsumer::ContentsChangedListener
686// ---------------------------------------------------------------------------
687
688void BufferLayer::onFrameAvailable(const BufferItem& item) {
689 // Add this buffer from our internal queue tracker
690 { // Autolock scope
691 Mutex::Autolock lock(mQueueItemLock);
692 mFlinger->mInterceptor.saveBufferUpdate(this, item.mGraphicBuffer->getWidth(),
693 item.mGraphicBuffer->getHeight(),
694 item.mFrameNumber);
695 // Reset the frame number tracker when we receive the first buffer after
696 // a frame number reset
697 if (item.mFrameNumber == 1) {
698 mLastFrameNumberReceived = 0;
699 }
700
701 // Ensure that callbacks are handled in order
702 while (item.mFrameNumber != mLastFrameNumberReceived + 1) {
David Sodman9eeae692017-11-02 10:53:32 -0700703 status_t result = mQueueItemCondition.waitRelative(mQueueItemLock,
704 ms2ns(500));
David Sodman0c69cad2017-08-21 12:12:51 -0700705 if (result != NO_ERROR) {
706 ALOGE("[%s] Timed out waiting on callback", mName.string());
707 }
708 }
709
710 mQueueItems.push_back(item);
711 android_atomic_inc(&mQueuedFrames);
712
713 // Wake up any pending callbacks
714 mLastFrameNumberReceived = item.mFrameNumber;
715 mQueueItemCondition.broadcast();
716 }
717
718 mFlinger->signalLayerUpdate();
719}
720
721void BufferLayer::onFrameReplaced(const BufferItem& item) {
722 { // Autolock scope
723 Mutex::Autolock lock(mQueueItemLock);
724
725 // Ensure that callbacks are handled in order
726 while (item.mFrameNumber != mLastFrameNumberReceived + 1) {
David Sodman9eeae692017-11-02 10:53:32 -0700727 status_t result = mQueueItemCondition.waitRelative(mQueueItemLock,
728 ms2ns(500));
David Sodman0c69cad2017-08-21 12:12:51 -0700729 if (result != NO_ERROR) {
730 ALOGE("[%s] Timed out waiting on callback", mName.string());
731 }
732 }
733
734 if (mQueueItems.empty()) {
735 ALOGE("Can't replace a frame on an empty queue");
736 return;
737 }
738 mQueueItems.editItemAt(mQueueItems.size() - 1) = item;
739
740 // Wake up any pending callbacks
741 mLastFrameNumberReceived = item.mFrameNumber;
742 mQueueItemCondition.broadcast();
743 }
744}
745
746void BufferLayer::onSidebandStreamChanged() {
747 if (android_atomic_release_cas(false, true, &mSidebandStreamChanged) == 0) {
748 // mSidebandStreamChanged was false
749 mFlinger->signalLayerUpdate();
750 }
751}
752
753bool BufferLayer::needsFiltering(const RenderArea& renderArea) const {
754 return mNeedsFiltering || renderArea.needsFiltering();
755}
756
757// As documented in libhardware header, formats in the range
758// 0x100 - 0x1FF are specific to the HAL implementation, and
759// are known to have no alpha channel
760// TODO: move definition for device-specific range into
761// hardware.h, instead of using hard-coded values here.
762#define HARDWARE_IS_DEVICE_FORMAT(f) ((f) >= 0x100 && (f) <= 0x1FF)
763
764bool BufferLayer::getOpacityForFormat(uint32_t format) {
765 if (HARDWARE_IS_DEVICE_FORMAT(format)) {
766 return true;
767 }
768 switch (format) {
769 case HAL_PIXEL_FORMAT_RGBA_8888:
770 case HAL_PIXEL_FORMAT_BGRA_8888:
771 case HAL_PIXEL_FORMAT_RGBA_FP16:
772 case HAL_PIXEL_FORMAT_RGBA_1010102:
773 return false;
774 }
775 // in all other case, we have no blending (also for unknown formats)
776 return true;
777}
778
David Sodman41fdfc92017-11-06 16:09:56 -0800779void BufferLayer::drawWithOpenGL(const RenderArea& renderArea, bool useIdentityTransform) const {
David Sodman0c69cad2017-08-21 12:12:51 -0700780 const State& s(getDrawingState());
781
David Sodman9eeae692017-11-02 10:53:32 -0700782 computeGeometry(renderArea, getBE().mMesh, useIdentityTransform);
David Sodman0c69cad2017-08-21 12:12:51 -0700783
784 /*
785 * NOTE: the way we compute the texture coordinates here produces
786 * different results than when we take the HWC path -- in the later case
787 * the "source crop" is rounded to texel boundaries.
788 * This can produce significantly different results when the texture
789 * is scaled by a large amount.
790 *
791 * The GL code below is more logical (imho), and the difference with
792 * HWC is due to a limitation of the HWC API to integers -- a question
793 * is suspend is whether we should ignore this problem or revert to
794 * GL composition when a buffer scaling is applied (maybe with some
795 * minimal value)? Or, we could make GL behave like HWC -- but this feel
796 * like more of a hack.
797 */
798 Rect win(computeBounds());
799
800 Transform t = getTransform();
801 if (!s.finalCrop.isEmpty()) {
802 win = t.transform(win);
803 if (!win.intersect(s.finalCrop, &win)) {
804 win.clear();
805 }
806 win = t.inverse().transform(win);
807 if (!win.intersect(computeBounds(), &win)) {
808 win.clear();
809 }
810 }
811
812 float left = float(win.left) / float(s.active.w);
813 float top = float(win.top) / float(s.active.h);
814 float right = float(win.right) / float(s.active.w);
815 float bottom = float(win.bottom) / float(s.active.h);
816
817 // TODO: we probably want to generate the texture coords with the mesh
818 // here we assume that we only have 4 vertices
David Sodman9eeae692017-11-02 10:53:32 -0700819 Mesh::VertexArray<vec2> texCoords(getBE().mMesh.getTexCoordArray<vec2>());
David Sodman0c69cad2017-08-21 12:12:51 -0700820 texCoords[0] = vec2(left, 1.0f - top);
821 texCoords[1] = vec2(left, 1.0f - bottom);
822 texCoords[2] = vec2(right, 1.0f - bottom);
823 texCoords[3] = vec2(right, 1.0f - top);
824
825 RenderEngine& engine(mFlinger->getRenderEngine());
826 engine.setupLayerBlending(mPremultipliedAlpha, isOpaque(s), false /* disableTexture */,
827 getColor());
David Sodman0c69cad2017-08-21 12:12:51 -0700828 engine.setSourceDataSpace(mCurrentState.dataSpace);
David Sodman9eeae692017-11-02 10:53:32 -0700829 engine.drawMesh(getBE().mMesh);
David Sodman0c69cad2017-08-21 12:12:51 -0700830 engine.disableBlending();
831}
832
833uint32_t BufferLayer::getProducerStickyTransform() const {
834 int producerStickyTransform = 0;
835 int ret = mProducer->query(NATIVE_WINDOW_STICKY_TRANSFORM, &producerStickyTransform);
836 if (ret != OK) {
837 ALOGW("%s: Error %s (%d) while querying window sticky transform.", __FUNCTION__,
838 strerror(-ret), ret);
839 return 0;
840 }
841 return static_cast<uint32_t>(producerStickyTransform);
842}
843
844bool BufferLayer::latchUnsignaledBuffers() {
845 static bool propertyLoaded = false;
846 static bool latch = false;
847 static std::mutex mutex;
848 std::lock_guard<std::mutex> lock(mutex);
849 if (!propertyLoaded) {
850 char value[PROPERTY_VALUE_MAX] = {};
851 property_get("debug.sf.latch_unsignaled", value, "0");
852 latch = atoi(value);
853 propertyLoaded = true;
854 }
855 return latch;
856}
857
858uint64_t BufferLayer::getHeadFrameNumber() const {
859 Mutex::Autolock lock(mQueueItemLock);
860 if (!mQueueItems.empty()) {
861 return mQueueItems[0].mFrameNumber;
862 } else {
863 return mCurrentFrameNumber;
864 }
865}
866
867bool BufferLayer::headFenceHasSignaled() const {
David Sodman0c69cad2017-08-21 12:12:51 -0700868 if (latchUnsignaledBuffers()) {
869 return true;
870 }
871
872 Mutex::Autolock lock(mQueueItemLock);
873 if (mQueueItems.empty()) {
874 return true;
875 }
876 if (mQueueItems[0].mIsDroppable) {
877 // Even though this buffer's fence may not have signaled yet, it could
878 // be replaced by another buffer before it has a chance to, which means
879 // that it's possible to get into a situation where a buffer is never
880 // able to be latched. To avoid this, grab this buffer anyway.
881 return true;
882 }
David Sodman9eeae692017-11-02 10:53:32 -0700883 return mQueueItems[0].mFenceTime->getSignalTime() !=
884 Fence::SIGNAL_TIME_PENDING;
David Sodman0c69cad2017-08-21 12:12:51 -0700885}
886
887uint32_t BufferLayer::getEffectiveScalingMode() const {
888 if (mOverrideScalingMode >= 0) {
889 return mOverrideScalingMode;
890 }
891 return mCurrentScalingMode;
892}
893
894// ----------------------------------------------------------------------------
895// transaction
896// ----------------------------------------------------------------------------
897
898void BufferLayer::notifyAvailableFrames() {
899 auto headFrameNumber = getHeadFrameNumber();
900 bool headFenceSignaled = headFenceHasSignaled();
901 Mutex::Autolock lock(mLocalSyncPointMutex);
902 for (auto& point : mLocalSyncPoints) {
903 if (headFrameNumber >= point->getFrameNumber() && headFenceSignaled) {
904 point->setFrameAvailable();
905 }
906 }
907}
908
909sp<IGraphicBufferProducer> BufferLayer::getProducer() const {
910 return mProducer;
911}
912
913// ---------------------------------------------------------------------------
914// h/w composer set-up
915// ---------------------------------------------------------------------------
916
917bool BufferLayer::allTransactionsSignaled() {
918 auto headFrameNumber = getHeadFrameNumber();
919 bool matchingFramesFound = false;
920 bool allTransactionsApplied = true;
921 Mutex::Autolock lock(mLocalSyncPointMutex);
922
923 for (auto& point : mLocalSyncPoints) {
924 if (point->getFrameNumber() > headFrameNumber) {
925 break;
926 }
927 matchingFramesFound = true;
928
929 if (!point->frameIsAvailable()) {
930 // We haven't notified the remote layer that the frame for
931 // this point is available yet. Notify it now, and then
932 // abort this attempt to latch.
933 point->setFrameAvailable();
934 allTransactionsApplied = false;
935 break;
936 }
937
938 allTransactionsApplied = allTransactionsApplied && point->transactionIsApplied();
939 }
940 return !matchingFramesFound || allTransactionsApplied;
941}
942
943} // namespace android
944
945#if defined(__gl_h_)
946#error "don't include gl/gl.h in this file"
947#endif
948
949#if defined(__gl2_h_)
950#error "don't include gl2/gl2.h in this file"
951#endif