blob: a5169ba7a4bf2eed3cb496c400fe9e549bad30b7 [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 Sodman0cf8f8d2017-12-20 18:19:45 -0800101 const sp<GraphicBuffer>& buffer(mActiveBuffer);
102 return (buffer != 0) && (buffer->getUsage() & GRALLOC_USAGE_PROTECTED);
David Sodman0c69cad2017-08-21 12:12:51 -0700103}
104
105bool BufferLayer::isVisible() const {
106 return !(isHiddenByPolicy()) && getAlpha() > 0.0f &&
David Sodman0cf8f8d2017-12-20 18:19:45 -0800107 (mActiveBuffer != nullptr || getBE().compositionInfo.hwc.sidebandStream != nullptr);
David Sodman0c69cad2017-08-21 12:12:51 -0700108}
109
110bool BufferLayer::isFixedSize() const {
111 return getEffectiveScalingMode() != NATIVE_WINDOW_SCALING_MODE_FREEZE;
112}
113
114status_t BufferLayer::setBuffers(uint32_t w, uint32_t h, PixelFormat format, uint32_t flags) {
115 uint32_t const maxSurfaceDims =
116 min(mFlinger->getMaxTextureSize(), mFlinger->getMaxViewportDims());
117
118 // never allow a surface larger than what our underlying GL implementation
119 // can handle.
120 if ((uint32_t(w) > maxSurfaceDims) || (uint32_t(h) > maxSurfaceDims)) {
121 ALOGE("dimensions too large %u x %u", uint32_t(w), uint32_t(h));
122 return BAD_VALUE;
123 }
124
125 mFormat = format;
126
127 mPotentialCursor = (flags & ISurfaceComposerClient::eCursorWindow) ? true : false;
128 mProtectedByApp = (flags & ISurfaceComposerClient::eProtectedByApp) ? true : false;
129 mCurrentOpacity = getOpacityForFormat(format);
130
Chia-I Wub28c6742017-12-27 10:59:54 -0800131 mConsumer->setDefaultBufferSize(w, h);
132 mConsumer->setDefaultBufferFormat(format);
133 mConsumer->setConsumerUsageBits(getEffectiveUsage(0));
David Sodman0c69cad2017-08-21 12:12:51 -0700134
135 return NO_ERROR;
136}
137
138static constexpr mat4 inverseOrientation(uint32_t transform) {
David Sodman41fdfc92017-11-06 16:09:56 -0800139 const mat4 flipH(-1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1);
140 const mat4 flipV(1, 0, 0, 0, 0, -1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1);
141 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 -0700142 mat4 tr;
143
144 if (transform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
145 tr = tr * rot90;
146 }
147 if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_H) {
148 tr = tr * flipH;
149 }
150 if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_V) {
151 tr = tr * flipV;
152 }
153 return inverse(tr);
154}
155
156/*
157 * onDraw will draw the current layer onto the presentable buffer
158 */
159void BufferLayer::onDraw(const RenderArea& renderArea, const Region& clip,
160 bool useIdentityTransform) const {
161 ATRACE_CALL();
162
David Sodman0cf8f8d2017-12-20 18:19:45 -0800163 if (CC_UNLIKELY(mActiveBuffer == 0)) {
David Sodman0c69cad2017-08-21 12:12:51 -0700164 // the texture has not been created yet, this Layer has
165 // in fact never been drawn into. This happens frequently with
166 // SurfaceView because the WindowManager can't know when the client
167 // has drawn the first time.
168
169 // If there is nothing under us, we paint the screen in black, otherwise
170 // we just skip this update.
171
172 // figure out if there is something below us
173 Region under;
174 bool finished = false;
175 mFlinger->mDrawingState.traverseInZOrder([&](Layer* layer) {
176 if (finished || layer == static_cast<BufferLayer const*>(this)) {
177 finished = true;
178 return;
179 }
180 under.orSelf(renderArea.getTransform().transform(layer->visibleRegion));
181 });
182 // if not everything below us is covered, we plug the holes!
183 Region holes(clip.subtract(under));
184 if (!holes.isEmpty()) {
185 clearWithOpenGL(renderArea, 0, 0, 0, 1);
186 }
187 return;
188 }
189
190 // Bind the current buffer to the GL texture, and wait for it to be
191 // ready for us to draw into.
Chia-I Wub28c6742017-12-27 10:59:54 -0800192 status_t err = mConsumer->bindTextureImage();
David Sodman0c69cad2017-08-21 12:12:51 -0700193 if (err != NO_ERROR) {
194 ALOGW("onDraw: bindTextureImage failed (err=%d)", err);
195 // Go ahead and draw the buffer anyway; no matter what we do the screen
196 // is probably going to have something visibly wrong.
197 }
198
199 bool blackOutLayer = isProtected() || (isSecure() && !renderArea.isSecure());
200
Lloyd Pique144e1162017-12-20 16:44:52 -0800201 auto& engine(mFlinger->getRenderEngine());
David Sodman0c69cad2017-08-21 12:12:51 -0700202
203 if (!blackOutLayer) {
204 // TODO: we could be more subtle with isFixedSize()
205 const bool useFiltering = getFiltering() || needsFiltering(renderArea) || isFixedSize();
206
207 // Query the texture matrix given our current filtering mode.
208 float textureMatrix[16];
Chia-I Wub28c6742017-12-27 10:59:54 -0800209 mConsumer->setFilteringEnabled(useFiltering);
210 mConsumer->getTransformMatrix(textureMatrix);
David Sodman0c69cad2017-08-21 12:12:51 -0700211
212 if (getTransformToDisplayInverse()) {
213 /*
214 * the code below applies the primary display's inverse transform to
215 * the texture transform
216 */
217 uint32_t transform = DisplayDevice::getPrimaryDisplayOrientationTransform();
218 mat4 tr = inverseOrientation(transform);
219
220 /**
221 * TODO(b/36727915): This is basically a hack.
222 *
223 * Ensure that regardless of the parent transformation,
224 * this buffer is always transformed from native display
225 * orientation to display orientation. For example, in the case
226 * of a camera where the buffer remains in native orientation,
227 * we want the pixels to always be upright.
228 */
229 sp<Layer> p = mDrawingParent.promote();
230 if (p != nullptr) {
231 const auto parentTransform = p->getTransform();
232 tr = tr * inverseOrientation(parentTransform.getOrientation());
233 }
234
235 // and finally apply it to the original texture matrix
236 const mat4 texTransform(mat4(static_cast<const float*>(textureMatrix)) * tr);
237 memcpy(textureMatrix, texTransform.asArray(), sizeof(textureMatrix));
238 }
239
240 // Set things up for texturing.
David Sodman0cf8f8d2017-12-20 18:19:45 -0800241 mTexture.setDimensions(mActiveBuffer->getWidth(), mActiveBuffer->getHeight());
David Sodman0c69cad2017-08-21 12:12:51 -0700242 mTexture.setFiltering(useFiltering);
243 mTexture.setMatrix(textureMatrix);
244
245 engine.setupLayerTexturing(mTexture);
246 } else {
247 engine.setupLayerBlackedOut();
248 }
249 drawWithOpenGL(renderArea, useIdentityTransform);
250 engine.disableTexturing();
251}
252
David Sodmaneb085e02017-10-05 18:49:04 -0700253void BufferLayer::onLayerDisplayed(const sp<Fence>& releaseFence) {
Chia-I Wub28c6742017-12-27 10:59:54 -0800254 mConsumer->setReleaseFence(releaseFence);
David Sodmaneb085e02017-10-05 18:49:04 -0700255}
David Sodmaneb085e02017-10-05 18:49:04 -0700256
257void BufferLayer::abandon() {
Chia-I Wub28c6742017-12-27 10:59:54 -0800258 mConsumer->abandon();
David Sodmaneb085e02017-10-05 18:49:04 -0700259}
260
261bool BufferLayer::shouldPresentNow(const DispSync& dispSync) const {
262 if (mSidebandStreamChanged || mAutoRefresh) {
263 return true;
264 }
265
266 Mutex::Autolock lock(mQueueItemLock);
267 if (mQueueItems.empty()) {
268 return false;
269 }
270 auto timestamp = mQueueItems[0].mTimestamp;
Chia-I Wub28c6742017-12-27 10:59:54 -0800271 nsecs_t expectedPresent = mConsumer->computeExpectedPresent(dispSync);
David Sodmaneb085e02017-10-05 18:49:04 -0700272
273 // Ignore timestamps more than a second in the future
274 bool isPlausible = timestamp < (expectedPresent + s2ns(1));
275 ALOGW_IF(!isPlausible,
276 "[%s] Timestamp %" PRId64 " seems implausible "
277 "relative to expectedPresent %" PRId64,
278 mName.string(), timestamp, expectedPresent);
279
280 bool isDue = timestamp < expectedPresent;
281 return isDue || !isPlausible;
282}
283
284void BufferLayer::setTransformHint(uint32_t orientation) const {
Chia-I Wub28c6742017-12-27 10:59:54 -0800285 mConsumer->setTransformHint(orientation);
David Sodmaneb085e02017-10-05 18:49:04 -0700286}
287
David Sodman0c69cad2017-08-21 12:12:51 -0700288bool BufferLayer::onPreComposition(nsecs_t refreshStartTime) {
289 if (mBufferLatched) {
290 Mutex::Autolock lock(mFrameEventHistoryMutex);
David Sodman0cf8f8d2017-12-20 18:19:45 -0800291 mFrameEventHistory.addPreComposition(mCurrentFrameNumber, refreshStartTime);
David Sodman0c69cad2017-08-21 12:12:51 -0700292 }
293 mRefreshPending = false;
David Sodman0cf8f8d2017-12-20 18:19:45 -0800294 return mQueuedFrames > 0 || mSidebandStreamChanged || mAutoRefresh;
David Sodman0c69cad2017-08-21 12:12:51 -0700295}
David Sodmaneb085e02017-10-05 18:49:04 -0700296bool BufferLayer::onPostComposition(const std::shared_ptr<FenceTime>& glDoneFence,
297 const std::shared_ptr<FenceTime>& presentFence,
298 const CompositorTiming& compositorTiming) {
299 // mFrameLatencyNeeded is true when a new frame was latched for the
300 // composition.
301 if (!mFrameLatencyNeeded) return false;
302
303 // Update mFrameEventHistory.
304 {
305 Mutex::Autolock lock(mFrameEventHistoryMutex);
David Sodman0cf8f8d2017-12-20 18:19:45 -0800306 mFrameEventHistory.addPostComposition(mCurrentFrameNumber, glDoneFence, presentFence,
307 compositorTiming);
David Sodmaneb085e02017-10-05 18:49:04 -0700308 }
309
310 // Update mFrameTracker.
Chia-I Wub28c6742017-12-27 10:59:54 -0800311 nsecs_t desiredPresentTime = mConsumer->getTimestamp();
David Sodmaneb085e02017-10-05 18:49:04 -0700312 mFrameTracker.setDesiredPresentTime(desiredPresentTime);
313
Chia-I Wub28c6742017-12-27 10:59:54 -0800314 std::shared_ptr<FenceTime> frameReadyFence = mConsumer->getCurrentFenceTime();
David Sodmaneb085e02017-10-05 18:49:04 -0700315 if (frameReadyFence->isValid()) {
316 mFrameTracker.setFrameReadyFence(std::move(frameReadyFence));
317 } else {
318 // There was no fence for this frame, so assume that it was ready
319 // to be presented at the desired present time.
320 mFrameTracker.setFrameReadyTime(desiredPresentTime);
321 }
322
323 if (presentFence->isValid()) {
324 mFrameTracker.setActualPresentFence(std::shared_ptr<FenceTime>(presentFence));
325 } else {
326 // The HWC doesn't support present fences, so use the refresh
327 // timestamp instead.
328 mFrameTracker.setActualPresentTime(
329 mFlinger->getHwComposer().getRefreshTimestamp(HWC_DISPLAY_PRIMARY));
330 }
331
332 mFrameTracker.advanceFrame();
333 mFrameLatencyNeeded = false;
334 return true;
335}
336
337std::vector<OccupancyTracker::Segment> BufferLayer::getOccupancyHistory(bool forceFlush) {
338 std::vector<OccupancyTracker::Segment> history;
Chia-I Wub28c6742017-12-27 10:59:54 -0800339 status_t result = mConsumer->getOccupancyHistory(forceFlush, &history);
David Sodmaneb085e02017-10-05 18:49:04 -0700340 if (result != NO_ERROR) {
341 ALOGW("[%s] Failed to obtain occupancy history (%d)", mName.string(), result);
342 return {};
343 }
344 return history;
345}
346
347bool BufferLayer::getTransformToDisplayInverse() const {
Chia-I Wub28c6742017-12-27 10:59:54 -0800348 return mConsumer->getTransformToDisplayInverse();
David Sodmaneb085e02017-10-05 18:49:04 -0700349}
David Sodman0c69cad2017-08-21 12:12:51 -0700350
David Sodman0c69cad2017-08-21 12:12:51 -0700351void BufferLayer::releasePendingBuffer(nsecs_t dequeueReadyTime) {
Chia-I Wub28c6742017-12-27 10:59:54 -0800352 if (!mConsumer->releasePendingBuffer()) {
David Sodman0c69cad2017-08-21 12:12:51 -0700353 return;
354 }
355
David Sodman0cf8f8d2017-12-20 18:19:45 -0800356 auto releaseFenceTime = std::make_shared<FenceTime>(mConsumer->getPrevFinalReleaseFence());
David Sodman0c69cad2017-08-21 12:12:51 -0700357 mReleaseTimeline.updateSignalTimes();
358 mReleaseTimeline.push(releaseFenceTime);
359
360 Mutex::Autolock lock(mFrameEventHistoryMutex);
361 if (mPreviousFrameNumber != 0) {
362 mFrameEventHistory.addRelease(mPreviousFrameNumber, dequeueReadyTime,
363 std::move(releaseFenceTime));
364 }
365}
David Sodman0c69cad2017-08-21 12:12:51 -0700366
367Region BufferLayer::latchBuffer(bool& recomputeVisibleRegions, nsecs_t latchTime) {
368 ATRACE_CALL();
369
370 if (android_atomic_acquire_cas(true, false, &mSidebandStreamChanged) == 0) {
371 // mSidebandStreamChanged was true
Chia-I Wub28c6742017-12-27 10:59:54 -0800372 mSidebandStream = mConsumer->getSidebandStream();
David Sodman386c22e2017-11-09 16:34:46 -0800373 // replicated in LayerBE until FE/BE is ready to be synchronized
David Sodman0cc69182017-11-17 12:12:07 -0800374 getBE().compositionInfo.hwc.sidebandStream = mSidebandStream;
Peiyong Lin566a3b42018-01-09 18:22:43 -0800375 if (getBE().compositionInfo.hwc.sidebandStream != nullptr) {
David Sodman0c69cad2017-08-21 12:12:51 -0700376 setTransactionFlags(eTransactionNeeded);
377 mFlinger->setTransactionFlags(eTraversalNeeded);
378 }
379 recomputeVisibleRegions = true;
380
381 const State& s(getDrawingState());
382 return getTransform().transform(Region(Rect(s.active.w, s.active.h)));
383 }
384
385 Region outDirtyRegion;
386 if (mQueuedFrames <= 0 && !mAutoRefresh) {
387 return outDirtyRegion;
388 }
389
390 // if we've already called updateTexImage() without going through
391 // a composition step, we have to skip this layer at this point
392 // because we cannot call updateTeximage() without a corresponding
393 // compositionComplete() call.
394 // we'll trigger an update in onPreComposition().
395 if (mRefreshPending) {
396 return outDirtyRegion;
397 }
398
399 // If the head buffer's acquire fence hasn't signaled yet, return and
400 // try again later
401 if (!headFenceHasSignaled()) {
402 mFlinger->signalLayerUpdate();
403 return outDirtyRegion;
404 }
405
406 // Capture the old state of the layer for comparisons later
407 const State& s(getDrawingState());
408 const bool oldOpacity = isOpaque(s);
David Sodman0cf8f8d2017-12-20 18:19:45 -0800409 sp<GraphicBuffer> oldBuffer = mActiveBuffer;
David Sodman0c69cad2017-08-21 12:12:51 -0700410
411 if (!allTransactionsSignaled()) {
412 mFlinger->signalLayerUpdate();
413 return outDirtyRegion;
414 }
415
416 // This boolean is used to make sure that SurfaceFlinger's shadow copy
417 // of the buffer queue isn't modified when the buffer queue is returning
418 // BufferItem's that weren't actually queued. This can happen in shared
419 // buffer mode.
420 bool queuedBuffer = false;
421 LayerRejecter r(mDrawingState, getCurrentState(), recomputeVisibleRegions,
David Sodman0cf8f8d2017-12-20 18:19:45 -0800422 getProducerStickyTransform() != 0, mName.string(), mOverrideScalingMode,
423 mFreezeGeometryUpdates);
424 status_t updateResult = mConsumer->updateTexImage(&r, mFlinger->mPrimaryDispSync, &mAutoRefresh,
425 &queuedBuffer, mLastFrameNumberReceived);
David Sodman0c69cad2017-08-21 12:12:51 -0700426 if (updateResult == BufferQueue::PRESENT_LATER) {
427 // Producer doesn't want buffer to be displayed yet. Signal a
428 // layer update so we check again at the next opportunity.
429 mFlinger->signalLayerUpdate();
430 return outDirtyRegion;
Chia-I Wu0cb75ac2017-11-27 15:56:04 -0800431 } else if (updateResult == BufferLayerConsumer::BUFFER_REJECTED) {
David Sodman0c69cad2017-08-21 12:12:51 -0700432 // If the buffer has been rejected, remove it from the shadow queue
433 // and return early
434 if (queuedBuffer) {
435 Mutex::Autolock lock(mQueueItemLock);
436 mQueueItems.removeAt(0);
437 android_atomic_dec(&mQueuedFrames);
438 }
439 return outDirtyRegion;
440 } else if (updateResult != NO_ERROR || mUpdateTexImageFailed) {
441 // This can occur if something goes wrong when trying to create the
442 // EGLImage for this buffer. If this happens, the buffer has already
443 // been released, so we need to clean up the queue and bug out
444 // early.
445 if (queuedBuffer) {
446 Mutex::Autolock lock(mQueueItemLock);
447 mQueueItems.clear();
448 android_atomic_and(0, &mQueuedFrames);
449 }
450
451 // Once we have hit this state, the shadow queue may no longer
452 // correctly reflect the incoming BufferQueue's contents, so even if
453 // updateTexImage starts working, the only safe course of action is
454 // to continue to ignore updates.
455 mUpdateTexImageFailed = true;
456
457 return outDirtyRegion;
458 }
459
460 if (queuedBuffer) {
461 // Autolock scope
Chia-I Wub28c6742017-12-27 10:59:54 -0800462 auto currentFrameNumber = mConsumer->getFrameNumber();
David Sodman0c69cad2017-08-21 12:12:51 -0700463
464 Mutex::Autolock lock(mQueueItemLock);
465
466 // Remove any stale buffers that have been dropped during
467 // updateTexImage
468 while (mQueueItems[0].mFrameNumber != currentFrameNumber) {
469 mQueueItems.removeAt(0);
470 android_atomic_dec(&mQueuedFrames);
471 }
472
473 mQueueItems.removeAt(0);
474 }
475
476 // Decrement the queued-frames count. Signal another event if we
477 // have more frames pending.
David Sodman0cf8f8d2017-12-20 18:19:45 -0800478 if ((queuedBuffer && android_atomic_dec(&mQueuedFrames) > 1) || mAutoRefresh) {
David Sodman0c69cad2017-08-21 12:12:51 -0700479 mFlinger->signalLayerUpdate();
480 }
481
482 // update the active buffer
David Sodman0cf8f8d2017-12-20 18:19:45 -0800483 mActiveBuffer = mConsumer->getCurrentBuffer(&mActiveBufferSlot);
484 getBE().compositionInfo.mBuffer = mActiveBuffer;
485 getBE().compositionInfo.mBufferSlot = mActiveBufferSlot;
486
487 if (mActiveBuffer == nullptr) {
David Sodman0c69cad2017-08-21 12:12:51 -0700488 // this can only happen if the very first buffer was rejected.
489 return outDirtyRegion;
490 }
491
492 mBufferLatched = true;
493 mPreviousFrameNumber = mCurrentFrameNumber;
Chia-I Wub28c6742017-12-27 10:59:54 -0800494 mCurrentFrameNumber = mConsumer->getFrameNumber();
David Sodman0c69cad2017-08-21 12:12:51 -0700495
496 {
497 Mutex::Autolock lock(mFrameEventHistoryMutex);
498 mFrameEventHistory.addLatch(mCurrentFrameNumber, latchTime);
David Sodman0c69cad2017-08-21 12:12:51 -0700499 }
500
501 mRefreshPending = true;
502 mFrameLatencyNeeded = true;
Peiyong Lin566a3b42018-01-09 18:22:43 -0800503 if (oldBuffer == nullptr) {
David Sodman0c69cad2017-08-21 12:12:51 -0700504 // the first time we receive a buffer, we need to trigger a
505 // geometry invalidation.
506 recomputeVisibleRegions = true;
507 }
508
Chia-I Wub28c6742017-12-27 10:59:54 -0800509 setDataSpace(mConsumer->getCurrentDataSpace());
David Sodman0c69cad2017-08-21 12:12:51 -0700510
Chia-I Wub28c6742017-12-27 10:59:54 -0800511 Rect crop(mConsumer->getCurrentCrop());
512 const uint32_t transform(mConsumer->getCurrentTransform());
513 const uint32_t scalingMode(mConsumer->getCurrentScalingMode());
David Sodman0cf8f8d2017-12-20 18:19:45 -0800514 if ((crop != mCurrentCrop) || (transform != mCurrentTransform) ||
David Sodman0c69cad2017-08-21 12:12:51 -0700515 (scalingMode != mCurrentScalingMode)) {
516 mCurrentCrop = crop;
517 mCurrentTransform = transform;
518 mCurrentScalingMode = scalingMode;
519 recomputeVisibleRegions = true;
520 }
521
Peiyong Lin566a3b42018-01-09 18:22:43 -0800522 if (oldBuffer != nullptr) {
David Sodman0cf8f8d2017-12-20 18:19:45 -0800523 uint32_t bufWidth = mActiveBuffer->getWidth();
524 uint32_t bufHeight = mActiveBuffer->getHeight();
525 if (bufWidth != uint32_t(oldBuffer->width) || bufHeight != uint32_t(oldBuffer->height)) {
David Sodman0c69cad2017-08-21 12:12:51 -0700526 recomputeVisibleRegions = true;
527 }
528 }
529
David Sodman0cf8f8d2017-12-20 18:19:45 -0800530 mCurrentOpacity = getOpacityForFormat(mActiveBuffer->format);
David Sodman0c69cad2017-08-21 12:12:51 -0700531 if (oldOpacity != isOpaque(s)) {
532 recomputeVisibleRegions = true;
533 }
534
535 // Remove any sync points corresponding to the buffer which was just
536 // latched
537 {
538 Mutex::Autolock lock(mLocalSyncPointMutex);
539 auto point = mLocalSyncPoints.begin();
540 while (point != mLocalSyncPoints.end()) {
541 if (!(*point)->frameIsAvailable() || !(*point)->transactionIsApplied()) {
542 // This sync point must have been added since we started
543 // latching. Don't drop it yet.
544 ++point;
545 continue;
546 }
547
548 if ((*point)->getFrameNumber() <= mCurrentFrameNumber) {
549 point = mLocalSyncPoints.erase(point);
550 } else {
551 ++point;
552 }
553 }
554 }
555
556 // FIXME: postedRegion should be dirty & bounds
557 Region dirtyRegion(Rect(s.active.w, s.active.h));
558
559 // transform the dirty region to window-manager space
560 outDirtyRegion = (getTransform().transform(dirtyRegion));
561
562 return outDirtyRegion;
563}
564
David Sodmaneb085e02017-10-05 18:49:04 -0700565void BufferLayer::setDefaultBufferSize(uint32_t w, uint32_t h) {
Chia-I Wub28c6742017-12-27 10:59:54 -0800566 mConsumer->setDefaultBufferSize(w, h);
David Sodmaneb085e02017-10-05 18:49:04 -0700567}
568
David Sodman0c69cad2017-08-21 12:12:51 -0700569void BufferLayer::setPerFrameData(const sp<const DisplayDevice>& displayDevice) {
570 // Apply this display's projection's viewport to the visible region
571 // before giving it to the HWC HAL.
572 const Transform& tr = displayDevice->getTransform();
573 const auto& viewport = displayDevice->getViewport();
574 Region visible = tr.transform(visibleRegion.intersect(viewport));
575 auto hwcId = displayDevice->getHwcDisplayId();
David Sodman6f65f3e2017-11-03 14:28:09 -0700576 auto& hwcInfo = getBE().mHwcLayers[hwcId];
David Sodman0c69cad2017-08-21 12:12:51 -0700577 auto& hwcLayer = hwcInfo.layer;
David Sodman5d89c1d2017-12-14 15:54:51 -0800578 auto error = (*hwcLayer)->setVisibleRegion(visible);
David Sodman0c69cad2017-08-21 12:12:51 -0700579 if (error != HWC2::Error::None) {
580 ALOGE("[%s] Failed to set visible region: %s (%d)", mName.string(),
581 to_string(error).c_str(), static_cast<int32_t>(error));
582 visible.dump(LOG_TAG);
583 }
584
David Sodman5d89c1d2017-12-14 15:54:51 -0800585 error = (*hwcLayer)->setSurfaceDamage(surfaceDamageRegion);
David Sodman0c69cad2017-08-21 12:12:51 -0700586 if (error != HWC2::Error::None) {
587 ALOGE("[%s] Failed to set surface damage: %s (%d)", mName.string(),
588 to_string(error).c_str(), static_cast<int32_t>(error));
589 surfaceDamageRegion.dump(LOG_TAG);
590 }
591
592 // Sideband layers
David Sodman0cc69182017-11-17 12:12:07 -0800593 if (getBE().compositionInfo.hwc.sidebandStream.get()) {
David Sodman0c69cad2017-08-21 12:12:51 -0700594 setCompositionType(hwcId, HWC2::Composition::Sideband);
595 ALOGV("[%s] Requesting Sideband composition", mName.string());
David Sodman5d89c1d2017-12-14 15:54:51 -0800596 error = (*hwcLayer)->setSidebandStream(getBE().compositionInfo.hwc.sidebandStream->handle());
David Sodman0c69cad2017-08-21 12:12:51 -0700597 if (error != HWC2::Error::None) {
598 ALOGE("[%s] Failed to set sideband stream %p: %s (%d)", mName.string(),
David Sodman0cc69182017-11-17 12:12:07 -0800599 getBE().compositionInfo.hwc.sidebandStream->handle(), to_string(error).c_str(),
David Sodman9eeae692017-11-02 10:53:32 -0700600 static_cast<int32_t>(error));
David Sodman0c69cad2017-08-21 12:12:51 -0700601 }
602 return;
603 }
604
David Sodman0c69cad2017-08-21 12:12:51 -0700605 // Device or Cursor layers
606 if (mPotentialCursor) {
607 ALOGV("[%s] Requesting Cursor composition", mName.string());
608 setCompositionType(hwcId, HWC2::Composition::Cursor);
609 } else {
610 ALOGV("[%s] Requesting Device composition", mName.string());
611 setCompositionType(hwcId, HWC2::Composition::Device);
612 }
613
Peiyong Lin13170c82018-01-22 18:55:51 -0800614 ALOGV("setPerFrameData: dataspace = %d", mDrawingState.dataSpace);
David Sodman5d89c1d2017-12-14 15:54:51 -0800615 error = (*hwcLayer)->setDataspace(mDrawingState.dataSpace);
David Sodman0c69cad2017-08-21 12:12:51 -0700616 if (error != HWC2::Error::None) {
Peiyong Lin13170c82018-01-22 18:55:51 -0800617 ALOGE("[%s] Failed to set dataspace %d: %s (%d)", mName.string(), mDrawingState.dataSpace,
David Sodman0c69cad2017-08-21 12:12:51 -0700618 to_string(error).c_str(), static_cast<int32_t>(error));
619 }
620
Courtney Goeltzenleuchterf9c98e52018-02-12 07:23:17 -0700621 const HdrMetadata& metadata = mConsumer->getCurrentHdrMetadata();
David Sodman5d89c1d2017-12-14 15:54:51 -0800622 error = (*hwcLayer)->setHdrMetadata(metadata);
Courtney Goeltzenleuchter301bb302018-03-12 11:12:42 -0600623 if (error != HWC2::Error::None && error != HWC2::Error::Unsupported) {
Courtney Goeltzenleuchterf9c98e52018-02-12 07:23:17 -0700624 ALOGE("[%s] Failed to set hdrMetadata: %s (%d)", mName.string(),
625 to_string(error).c_str(), static_cast<int32_t>(error));
626 }
627
David Sodman0c69cad2017-08-21 12:12:51 -0700628 uint32_t hwcSlot = 0;
629 sp<GraphicBuffer> hwcBuffer;
David Sodman0cf8f8d2017-12-20 18:19:45 -0800630 getBE().mHwcLayers[hwcId].bufferCache.getHwcBuffer(mActiveBufferSlot, mActiveBuffer, &hwcSlot,
631 &hwcBuffer);
David Sodman0c69cad2017-08-21 12:12:51 -0700632
Chia-I Wub28c6742017-12-27 10:59:54 -0800633 auto acquireFence = mConsumer->getCurrentFence();
David Sodman5d89c1d2017-12-14 15:54:51 -0800634 error = (*hwcLayer)->setBuffer(hwcSlot, hwcBuffer, acquireFence);
David Sodman0c69cad2017-08-21 12:12:51 -0700635 if (error != HWC2::Error::None) {
David Sodman9eeae692017-11-02 10:53:32 -0700636 ALOGE("[%s] Failed to set buffer %p: %s (%d)", mName.string(),
David Sodman0cc69182017-11-17 12:12:07 -0800637 getBE().compositionInfo.mBuffer->handle, to_string(error).c_str(),
David Sodman9eeae692017-11-02 10:53:32 -0700638 static_cast<int32_t>(error));
David Sodman0c69cad2017-08-21 12:12:51 -0700639 }
640}
641
David Sodman41fdfc92017-11-06 16:09:56 -0800642bool BufferLayer::isOpaque(const Layer::State& s) const {
David Sodman0c69cad2017-08-21 12:12:51 -0700643 // if we don't have a buffer or sidebandStream yet, we're translucent regardless of the
644 // layer's opaque flag.
David Sodman0cf8f8d2017-12-20 18:19:45 -0800645 if ((getBE().compositionInfo.hwc.sidebandStream == nullptr) && (mActiveBuffer == nullptr)) {
David Sodman0c69cad2017-08-21 12:12:51 -0700646 return false;
647 }
648
649 // if the layer has the opaque flag, then we're always opaque,
650 // otherwise we use the current buffer's format.
651 return ((s.flags & layer_state_t::eLayerOpaque) != 0) || mCurrentOpacity;
652}
653
654void BufferLayer::onFirstRef() {
655 // Creates a custom BufferQueue for SurfaceFlingerConsumer to use
656 sp<IGraphicBufferProducer> producer;
657 sp<IGraphicBufferConsumer> consumer;
658 BufferQueue::createBufferQueue(&producer, &consumer, true);
659 mProducer = new MonitoredProducer(producer, mFlinger, this);
David Sodman0cf8f8d2017-12-20 18:19:45 -0800660 mConsumer = new BufferLayerConsumer(consumer, mFlinger->getRenderEngine(), mTextureName, this);
Chia-I Wub28c6742017-12-27 10:59:54 -0800661 mConsumer->setConsumerUsageBits(getEffectiveUsage(0));
662 mConsumer->setContentsChangedListener(this);
663 mConsumer->setName(mName);
David Sodman0c69cad2017-08-21 12:12:51 -0700664
665 if (mFlinger->isLayerTripleBufferingDisabled()) {
666 mProducer->setMaxDequeuedBufferCount(2);
667 }
668
669 const sp<const DisplayDevice> hw(mFlinger->getDefaultDisplayDevice());
670 updateTransformHint(hw);
671}
672
673// ---------------------------------------------------------------------------
674// Interface implementation for SurfaceFlingerConsumer::ContentsChangedListener
675// ---------------------------------------------------------------------------
676
677void BufferLayer::onFrameAvailable(const BufferItem& item) {
678 // Add this buffer from our internal queue tracker
679 { // Autolock scope
680 Mutex::Autolock lock(mQueueItemLock);
681 mFlinger->mInterceptor.saveBufferUpdate(this, item.mGraphicBuffer->getWidth(),
682 item.mGraphicBuffer->getHeight(),
683 item.mFrameNumber);
684 // Reset the frame number tracker when we receive the first buffer after
685 // a frame number reset
686 if (item.mFrameNumber == 1) {
687 mLastFrameNumberReceived = 0;
688 }
689
690 // Ensure that callbacks are handled in order
691 while (item.mFrameNumber != mLastFrameNumberReceived + 1) {
David Sodman0cf8f8d2017-12-20 18:19:45 -0800692 status_t result = mQueueItemCondition.waitRelative(mQueueItemLock, ms2ns(500));
David Sodman0c69cad2017-08-21 12:12:51 -0700693 if (result != NO_ERROR) {
694 ALOGE("[%s] Timed out waiting on callback", mName.string());
695 }
696 }
697
698 mQueueItems.push_back(item);
699 android_atomic_inc(&mQueuedFrames);
700
701 // Wake up any pending callbacks
702 mLastFrameNumberReceived = item.mFrameNumber;
703 mQueueItemCondition.broadcast();
704 }
705
706 mFlinger->signalLayerUpdate();
707}
708
709void BufferLayer::onFrameReplaced(const BufferItem& item) {
710 { // Autolock scope
711 Mutex::Autolock lock(mQueueItemLock);
712
713 // Ensure that callbacks are handled in order
714 while (item.mFrameNumber != mLastFrameNumberReceived + 1) {
David Sodman0cf8f8d2017-12-20 18:19:45 -0800715 status_t result = mQueueItemCondition.waitRelative(mQueueItemLock, ms2ns(500));
David Sodman0c69cad2017-08-21 12:12:51 -0700716 if (result != NO_ERROR) {
717 ALOGE("[%s] Timed out waiting on callback", mName.string());
718 }
719 }
720
721 if (mQueueItems.empty()) {
722 ALOGE("Can't replace a frame on an empty queue");
723 return;
724 }
725 mQueueItems.editItemAt(mQueueItems.size() - 1) = item;
726
727 // Wake up any pending callbacks
728 mLastFrameNumberReceived = item.mFrameNumber;
729 mQueueItemCondition.broadcast();
730 }
731}
732
733void BufferLayer::onSidebandStreamChanged() {
734 if (android_atomic_release_cas(false, true, &mSidebandStreamChanged) == 0) {
735 // mSidebandStreamChanged was false
736 mFlinger->signalLayerUpdate();
737 }
738}
739
740bool BufferLayer::needsFiltering(const RenderArea& renderArea) const {
741 return mNeedsFiltering || renderArea.needsFiltering();
742}
743
744// As documented in libhardware header, formats in the range
745// 0x100 - 0x1FF are specific to the HAL implementation, and
746// are known to have no alpha channel
747// TODO: move definition for device-specific range into
748// hardware.h, instead of using hard-coded values here.
749#define HARDWARE_IS_DEVICE_FORMAT(f) ((f) >= 0x100 && (f) <= 0x1FF)
750
751bool BufferLayer::getOpacityForFormat(uint32_t format) {
752 if (HARDWARE_IS_DEVICE_FORMAT(format)) {
753 return true;
754 }
755 switch (format) {
756 case HAL_PIXEL_FORMAT_RGBA_8888:
757 case HAL_PIXEL_FORMAT_BGRA_8888:
758 case HAL_PIXEL_FORMAT_RGBA_FP16:
759 case HAL_PIXEL_FORMAT_RGBA_1010102:
760 return false;
761 }
762 // in all other case, we have no blending (also for unknown formats)
763 return true;
764}
765
David Sodman41fdfc92017-11-06 16:09:56 -0800766void BufferLayer::drawWithOpenGL(const RenderArea& renderArea, bool useIdentityTransform) const {
David Sodman0c69cad2017-08-21 12:12:51 -0700767 const State& s(getDrawingState());
768
David Sodman9eeae692017-11-02 10:53:32 -0700769 computeGeometry(renderArea, getBE().mMesh, useIdentityTransform);
David Sodman0c69cad2017-08-21 12:12:51 -0700770
771 /*
772 * NOTE: the way we compute the texture coordinates here produces
773 * different results than when we take the HWC path -- in the later case
774 * the "source crop" is rounded to texel boundaries.
775 * This can produce significantly different results when the texture
776 * is scaled by a large amount.
777 *
778 * The GL code below is more logical (imho), and the difference with
779 * HWC is due to a limitation of the HWC API to integers -- a question
780 * is suspend is whether we should ignore this problem or revert to
781 * GL composition when a buffer scaling is applied (maybe with some
782 * minimal value)? Or, we could make GL behave like HWC -- but this feel
783 * like more of a hack.
784 */
Dan Stoza80d61162017-12-20 15:57:52 -0800785 const Rect bounds{computeBounds()}; // Rounds from FloatRect
David Sodman0c69cad2017-08-21 12:12:51 -0700786
787 Transform t = getTransform();
Dan Stoza80d61162017-12-20 15:57:52 -0800788 Rect win = bounds;
David Sodman0c69cad2017-08-21 12:12:51 -0700789 if (!s.finalCrop.isEmpty()) {
790 win = t.transform(win);
791 if (!win.intersect(s.finalCrop, &win)) {
792 win.clear();
793 }
794 win = t.inverse().transform(win);
Dan Stoza80d61162017-12-20 15:57:52 -0800795 if (!win.intersect(bounds, &win)) {
David Sodman0c69cad2017-08-21 12:12:51 -0700796 win.clear();
797 }
798 }
799
800 float left = float(win.left) / float(s.active.w);
801 float top = float(win.top) / float(s.active.h);
802 float right = float(win.right) / float(s.active.w);
803 float bottom = float(win.bottom) / float(s.active.h);
804
805 // TODO: we probably want to generate the texture coords with the mesh
806 // here we assume that we only have 4 vertices
David Sodman9eeae692017-11-02 10:53:32 -0700807 Mesh::VertexArray<vec2> texCoords(getBE().mMesh.getTexCoordArray<vec2>());
David Sodman0c69cad2017-08-21 12:12:51 -0700808 texCoords[0] = vec2(left, 1.0f - top);
809 texCoords[1] = vec2(left, 1.0f - bottom);
810 texCoords[2] = vec2(right, 1.0f - bottom);
811 texCoords[3] = vec2(right, 1.0f - top);
812
Lloyd Pique144e1162017-12-20 16:44:52 -0800813 auto& engine(mFlinger->getRenderEngine());
David Sodman0c69cad2017-08-21 12:12:51 -0700814 engine.setupLayerBlending(mPremultipliedAlpha, isOpaque(s), false /* disableTexture */,
815 getColor());
David Sodman0c69cad2017-08-21 12:12:51 -0700816 engine.setSourceDataSpace(mCurrentState.dataSpace);
Chia-I Wu5c6e4632018-01-11 08:54:38 -0800817
Chia-I Wu8d2651e2018-01-24 12:18:49 -0800818 if (mCurrentState.dataSpace == HAL_DATASPACE_BT2020_ITU_PQ &&
Chia-I Wu5c6e4632018-01-11 08:54:38 -0800819 mConsumer->getCurrentApi() == NATIVE_WINDOW_API_MEDIA &&
820 getBE().compositionInfo.mBuffer->getPixelFormat() == HAL_PIXEL_FORMAT_RGBA_1010102) {
821 engine.setSourceY410BT2020(true);
822 }
823
David Sodman9eeae692017-11-02 10:53:32 -0700824 engine.drawMesh(getBE().mMesh);
David Sodman0c69cad2017-08-21 12:12:51 -0700825 engine.disableBlending();
Chia-I Wu5c6e4632018-01-11 08:54:38 -0800826
827 engine.setSourceY410BT2020(false);
David Sodman0c69cad2017-08-21 12:12:51 -0700828}
829
830uint32_t BufferLayer::getProducerStickyTransform() const {
831 int producerStickyTransform = 0;
832 int ret = mProducer->query(NATIVE_WINDOW_STICKY_TRANSFORM, &producerStickyTransform);
833 if (ret != OK) {
834 ALOGW("%s: Error %s (%d) while querying window sticky transform.", __FUNCTION__,
835 strerror(-ret), ret);
836 return 0;
837 }
838 return static_cast<uint32_t>(producerStickyTransform);
839}
840
841bool BufferLayer::latchUnsignaledBuffers() {
842 static bool propertyLoaded = false;
843 static bool latch = false;
844 static std::mutex mutex;
845 std::lock_guard<std::mutex> lock(mutex);
846 if (!propertyLoaded) {
847 char value[PROPERTY_VALUE_MAX] = {};
848 property_get("debug.sf.latch_unsignaled", value, "0");
849 latch = atoi(value);
850 propertyLoaded = true;
851 }
852 return latch;
853}
854
855uint64_t BufferLayer::getHeadFrameNumber() const {
856 Mutex::Autolock lock(mQueueItemLock);
857 if (!mQueueItems.empty()) {
858 return mQueueItems[0].mFrameNumber;
859 } else {
860 return mCurrentFrameNumber;
861 }
862}
863
864bool BufferLayer::headFenceHasSignaled() const {
David Sodman0c69cad2017-08-21 12:12:51 -0700865 if (latchUnsignaledBuffers()) {
866 return true;
867 }
868
869 Mutex::Autolock lock(mQueueItemLock);
870 if (mQueueItems.empty()) {
871 return true;
872 }
873 if (mQueueItems[0].mIsDroppable) {
874 // Even though this buffer's fence may not have signaled yet, it could
875 // be replaced by another buffer before it has a chance to, which means
876 // that it's possible to get into a situation where a buffer is never
877 // able to be latched. To avoid this, grab this buffer anyway.
878 return true;
879 }
David Sodman0cf8f8d2017-12-20 18:19:45 -0800880 return mQueueItems[0].mFenceTime->getSignalTime() != Fence::SIGNAL_TIME_PENDING;
David Sodman0c69cad2017-08-21 12:12:51 -0700881}
882
883uint32_t BufferLayer::getEffectiveScalingMode() const {
884 if (mOverrideScalingMode >= 0) {
885 return mOverrideScalingMode;
886 }
887 return mCurrentScalingMode;
888}
889
890// ----------------------------------------------------------------------------
891// transaction
892// ----------------------------------------------------------------------------
893
894void BufferLayer::notifyAvailableFrames() {
895 auto headFrameNumber = getHeadFrameNumber();
896 bool headFenceSignaled = headFenceHasSignaled();
897 Mutex::Autolock lock(mLocalSyncPointMutex);
898 for (auto& point : mLocalSyncPoints) {
899 if (headFrameNumber >= point->getFrameNumber() && headFenceSignaled) {
900 point->setFrameAvailable();
901 }
902 }
903}
904
905sp<IGraphicBufferProducer> BufferLayer::getProducer() const {
906 return mProducer;
907}
908
909// ---------------------------------------------------------------------------
910// h/w composer set-up
911// ---------------------------------------------------------------------------
912
913bool BufferLayer::allTransactionsSignaled() {
914 auto headFrameNumber = getHeadFrameNumber();
915 bool matchingFramesFound = false;
916 bool allTransactionsApplied = true;
917 Mutex::Autolock lock(mLocalSyncPointMutex);
918
919 for (auto& point : mLocalSyncPoints) {
920 if (point->getFrameNumber() > headFrameNumber) {
921 break;
922 }
923 matchingFramesFound = true;
924
925 if (!point->frameIsAvailable()) {
926 // We haven't notified the remote layer that the frame for
927 // this point is available yet. Notify it now, and then
928 // abort this attempt to latch.
929 point->setFrameAvailable();
930 allTransactionsApplied = false;
931 break;
932 }
933
934 allTransactionsApplied = allTransactionsApplied && point->transactionIsApplied();
935 }
936 return !matchingFramesFound || allTransactionsApplied;
937}
938
939} // namespace android
940
941#if defined(__gl_h_)
942#error "don't include gl/gl.h in this file"
943#endif
944
945#if defined(__gl2_h_)
946#error "don't include gl2/gl2.h in this file"
947#endif