blob: 916576af20d509d44f37984a6512689f7cf216a5 [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 Sodmandc5e0622018-01-05 23:10:57 -0800163 CompositionInfo& compositionInfo = getBE().compositionInfo;
164
David Sodman0cf8f8d2017-12-20 18:19:45 -0800165 if (CC_UNLIKELY(mActiveBuffer == 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
Lloyd Pique144e1162017-12-20 16:44:52 -0800203 auto& engine(mFlinger->getRenderEngine());
David Sodman0c69cad2017-08-21 12:12:51 -0700204
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 Sodman0cf8f8d2017-12-20 18:19:45 -0800243 mTexture.setDimensions(mActiveBuffer->getWidth(), mActiveBuffer->getHeight());
David Sodman0c69cad2017-08-21 12:12:51 -0700244 mTexture.setFiltering(useFiltering);
245 mTexture.setMatrix(textureMatrix);
David Sodmandc5e0622018-01-05 23:10:57 -0800246 compositionInfo.re.texture = mTexture;
David Sodman0c69cad2017-08-21 12:12:51 -0700247
248 engine.setupLayerTexturing(mTexture);
249 } else {
250 engine.setupLayerBlackedOut();
251 }
252 drawWithOpenGL(renderArea, useIdentityTransform);
253 engine.disableTexturing();
254}
255
David Sodmandc5e0622018-01-05 23:10:57 -0800256void BufferLayer::drawNow(const RenderArea& renderArea, bool useIdentityTransform) const {
257 CompositionInfo& compositionInfo = getBE().compositionInfo;
258 auto& engine(mFlinger->getRenderEngine());
259
260 draw(renderArea, useIdentityTransform);
261
262 engine.setupLayerTexturing(compositionInfo.re.texture);
263 engine.setupLayerBlending(compositionInfo.re.preMultipliedAlpha, compositionInfo.re.opaque,
264 false, compositionInfo.re.color);
265 engine.setSourceDataSpace(compositionInfo.hwc.dataspace);
266 engine.setSourceY410BT2020(compositionInfo.re.Y410BT2020);
267 engine.drawMesh(getBE().getMesh());
268 engine.disableBlending();
269 engine.disableTexturing();
270 engine.setSourceY410BT2020(false);
271}
272
David Sodmaneb085e02017-10-05 18:49:04 -0700273void BufferLayer::onLayerDisplayed(const sp<Fence>& releaseFence) {
Chia-I Wub28c6742017-12-27 10:59:54 -0800274 mConsumer->setReleaseFence(releaseFence);
David Sodmaneb085e02017-10-05 18:49:04 -0700275}
David Sodmaneb085e02017-10-05 18:49:04 -0700276
277void BufferLayer::abandon() {
Chia-I Wub28c6742017-12-27 10:59:54 -0800278 mConsumer->abandon();
David Sodmaneb085e02017-10-05 18:49:04 -0700279}
280
281bool BufferLayer::shouldPresentNow(const DispSync& dispSync) const {
282 if (mSidebandStreamChanged || mAutoRefresh) {
283 return true;
284 }
285
286 Mutex::Autolock lock(mQueueItemLock);
287 if (mQueueItems.empty()) {
288 return false;
289 }
290 auto timestamp = mQueueItems[0].mTimestamp;
Chia-I Wub28c6742017-12-27 10:59:54 -0800291 nsecs_t expectedPresent = mConsumer->computeExpectedPresent(dispSync);
David Sodmaneb085e02017-10-05 18:49:04 -0700292
293 // Ignore timestamps more than a second in the future
294 bool isPlausible = timestamp < (expectedPresent + s2ns(1));
295 ALOGW_IF(!isPlausible,
296 "[%s] Timestamp %" PRId64 " seems implausible "
297 "relative to expectedPresent %" PRId64,
298 mName.string(), timestamp, expectedPresent);
299
300 bool isDue = timestamp < expectedPresent;
301 return isDue || !isPlausible;
302}
303
304void BufferLayer::setTransformHint(uint32_t orientation) const {
Chia-I Wub28c6742017-12-27 10:59:54 -0800305 mConsumer->setTransformHint(orientation);
David Sodmaneb085e02017-10-05 18:49:04 -0700306}
307
David Sodman0c69cad2017-08-21 12:12:51 -0700308bool BufferLayer::onPreComposition(nsecs_t refreshStartTime) {
309 if (mBufferLatched) {
310 Mutex::Autolock lock(mFrameEventHistoryMutex);
David Sodman0cf8f8d2017-12-20 18:19:45 -0800311 mFrameEventHistory.addPreComposition(mCurrentFrameNumber, refreshStartTime);
David Sodman0c69cad2017-08-21 12:12:51 -0700312 }
313 mRefreshPending = false;
David Sodman0cf8f8d2017-12-20 18:19:45 -0800314 return mQueuedFrames > 0 || mSidebandStreamChanged || mAutoRefresh;
David Sodman0c69cad2017-08-21 12:12:51 -0700315}
David Sodmaneb085e02017-10-05 18:49:04 -0700316bool BufferLayer::onPostComposition(const std::shared_ptr<FenceTime>& glDoneFence,
317 const std::shared_ptr<FenceTime>& presentFence,
318 const CompositorTiming& compositorTiming) {
319 // mFrameLatencyNeeded is true when a new frame was latched for the
320 // composition.
321 if (!mFrameLatencyNeeded) return false;
322
323 // Update mFrameEventHistory.
324 {
325 Mutex::Autolock lock(mFrameEventHistoryMutex);
David Sodman0cf8f8d2017-12-20 18:19:45 -0800326 mFrameEventHistory.addPostComposition(mCurrentFrameNumber, glDoneFence, presentFence,
327 compositorTiming);
David Sodmaneb085e02017-10-05 18:49:04 -0700328 }
329
330 // Update mFrameTracker.
Chia-I Wub28c6742017-12-27 10:59:54 -0800331 nsecs_t desiredPresentTime = mConsumer->getTimestamp();
David Sodmaneb085e02017-10-05 18:49:04 -0700332 mFrameTracker.setDesiredPresentTime(desiredPresentTime);
333
Chia-I Wub28c6742017-12-27 10:59:54 -0800334 std::shared_ptr<FenceTime> frameReadyFence = mConsumer->getCurrentFenceTime();
David Sodmaneb085e02017-10-05 18:49:04 -0700335 if (frameReadyFence->isValid()) {
336 mFrameTracker.setFrameReadyFence(std::move(frameReadyFence));
337 } else {
338 // There was no fence for this frame, so assume that it was ready
339 // to be presented at the desired present time.
340 mFrameTracker.setFrameReadyTime(desiredPresentTime);
341 }
342
343 if (presentFence->isValid()) {
344 mFrameTracker.setActualPresentFence(std::shared_ptr<FenceTime>(presentFence));
345 } else {
346 // The HWC doesn't support present fences, so use the refresh
347 // timestamp instead.
348 mFrameTracker.setActualPresentTime(
349 mFlinger->getHwComposer().getRefreshTimestamp(HWC_DISPLAY_PRIMARY));
350 }
351
352 mFrameTracker.advanceFrame();
353 mFrameLatencyNeeded = false;
354 return true;
355}
356
357std::vector<OccupancyTracker::Segment> BufferLayer::getOccupancyHistory(bool forceFlush) {
358 std::vector<OccupancyTracker::Segment> history;
Chia-I Wub28c6742017-12-27 10:59:54 -0800359 status_t result = mConsumer->getOccupancyHistory(forceFlush, &history);
David Sodmaneb085e02017-10-05 18:49:04 -0700360 if (result != NO_ERROR) {
361 ALOGW("[%s] Failed to obtain occupancy history (%d)", mName.string(), result);
362 return {};
363 }
364 return history;
365}
366
367bool BufferLayer::getTransformToDisplayInverse() const {
Chia-I Wub28c6742017-12-27 10:59:54 -0800368 return mConsumer->getTransformToDisplayInverse();
David Sodmaneb085e02017-10-05 18:49:04 -0700369}
David Sodman0c69cad2017-08-21 12:12:51 -0700370
David Sodman0c69cad2017-08-21 12:12:51 -0700371void BufferLayer::releasePendingBuffer(nsecs_t dequeueReadyTime) {
Chia-I Wub28c6742017-12-27 10:59:54 -0800372 if (!mConsumer->releasePendingBuffer()) {
David Sodman0c69cad2017-08-21 12:12:51 -0700373 return;
374 }
375
David Sodman0cf8f8d2017-12-20 18:19:45 -0800376 auto releaseFenceTime = std::make_shared<FenceTime>(mConsumer->getPrevFinalReleaseFence());
David Sodman0c69cad2017-08-21 12:12:51 -0700377 mReleaseTimeline.updateSignalTimes();
378 mReleaseTimeline.push(releaseFenceTime);
379
380 Mutex::Autolock lock(mFrameEventHistoryMutex);
381 if (mPreviousFrameNumber != 0) {
382 mFrameEventHistory.addRelease(mPreviousFrameNumber, dequeueReadyTime,
383 std::move(releaseFenceTime));
384 }
385}
David Sodman0c69cad2017-08-21 12:12:51 -0700386
387Region BufferLayer::latchBuffer(bool& recomputeVisibleRegions, nsecs_t latchTime) {
388 ATRACE_CALL();
389
390 if (android_atomic_acquire_cas(true, false, &mSidebandStreamChanged) == 0) {
391 // mSidebandStreamChanged was true
Chia-I Wub28c6742017-12-27 10:59:54 -0800392 mSidebandStream = mConsumer->getSidebandStream();
David Sodman386c22e2017-11-09 16:34:46 -0800393 // replicated in LayerBE until FE/BE is ready to be synchronized
David Sodman0cc69182017-11-17 12:12:07 -0800394 getBE().compositionInfo.hwc.sidebandStream = mSidebandStream;
Peiyong Lin566a3b42018-01-09 18:22:43 -0800395 if (getBE().compositionInfo.hwc.sidebandStream != nullptr) {
David Sodman0c69cad2017-08-21 12:12:51 -0700396 setTransactionFlags(eTransactionNeeded);
397 mFlinger->setTransactionFlags(eTraversalNeeded);
398 }
399 recomputeVisibleRegions = true;
400
401 const State& s(getDrawingState());
402 return getTransform().transform(Region(Rect(s.active.w, s.active.h)));
403 }
404
405 Region outDirtyRegion;
406 if (mQueuedFrames <= 0 && !mAutoRefresh) {
407 return outDirtyRegion;
408 }
409
410 // if we've already called updateTexImage() without going through
411 // a composition step, we have to skip this layer at this point
412 // because we cannot call updateTeximage() without a corresponding
413 // compositionComplete() call.
414 // we'll trigger an update in onPreComposition().
415 if (mRefreshPending) {
416 return outDirtyRegion;
417 }
418
419 // If the head buffer's acquire fence hasn't signaled yet, return and
420 // try again later
421 if (!headFenceHasSignaled()) {
422 mFlinger->signalLayerUpdate();
423 return outDirtyRegion;
424 }
425
426 // Capture the old state of the layer for comparisons later
427 const State& s(getDrawingState());
428 const bool oldOpacity = isOpaque(s);
David Sodman0cf8f8d2017-12-20 18:19:45 -0800429 sp<GraphicBuffer> oldBuffer = mActiveBuffer;
David Sodman0c69cad2017-08-21 12:12:51 -0700430
431 if (!allTransactionsSignaled()) {
432 mFlinger->signalLayerUpdate();
433 return outDirtyRegion;
434 }
435
436 // This boolean is used to make sure that SurfaceFlinger's shadow copy
437 // of the buffer queue isn't modified when the buffer queue is returning
438 // BufferItem's that weren't actually queued. This can happen in shared
439 // buffer mode.
440 bool queuedBuffer = false;
441 LayerRejecter r(mDrawingState, getCurrentState(), recomputeVisibleRegions,
David Sodman0cf8f8d2017-12-20 18:19:45 -0800442 getProducerStickyTransform() != 0, mName.string(), mOverrideScalingMode,
443 mFreezeGeometryUpdates);
444 status_t updateResult = mConsumer->updateTexImage(&r, mFlinger->mPrimaryDispSync, &mAutoRefresh,
445 &queuedBuffer, mLastFrameNumberReceived);
David Sodman0c69cad2017-08-21 12:12:51 -0700446 if (updateResult == BufferQueue::PRESENT_LATER) {
447 // Producer doesn't want buffer to be displayed yet. Signal a
448 // layer update so we check again at the next opportunity.
449 mFlinger->signalLayerUpdate();
450 return outDirtyRegion;
Chia-I Wu0cb75ac2017-11-27 15:56:04 -0800451 } else if (updateResult == BufferLayerConsumer::BUFFER_REJECTED) {
David Sodman0c69cad2017-08-21 12:12:51 -0700452 // If the buffer has been rejected, remove it from the shadow queue
453 // and return early
454 if (queuedBuffer) {
455 Mutex::Autolock lock(mQueueItemLock);
456 mQueueItems.removeAt(0);
457 android_atomic_dec(&mQueuedFrames);
458 }
459 return outDirtyRegion;
460 } else if (updateResult != NO_ERROR || mUpdateTexImageFailed) {
461 // This can occur if something goes wrong when trying to create the
462 // EGLImage for this buffer. If this happens, the buffer has already
463 // been released, so we need to clean up the queue and bug out
464 // early.
465 if (queuedBuffer) {
466 Mutex::Autolock lock(mQueueItemLock);
467 mQueueItems.clear();
468 android_atomic_and(0, &mQueuedFrames);
469 }
470
471 // Once we have hit this state, the shadow queue may no longer
472 // correctly reflect the incoming BufferQueue's contents, so even if
473 // updateTexImage starts working, the only safe course of action is
474 // to continue to ignore updates.
475 mUpdateTexImageFailed = true;
476
477 return outDirtyRegion;
478 }
479
480 if (queuedBuffer) {
481 // Autolock scope
Chia-I Wub28c6742017-12-27 10:59:54 -0800482 auto currentFrameNumber = mConsumer->getFrameNumber();
David Sodman0c69cad2017-08-21 12:12:51 -0700483
484 Mutex::Autolock lock(mQueueItemLock);
485
486 // Remove any stale buffers that have been dropped during
487 // updateTexImage
488 while (mQueueItems[0].mFrameNumber != currentFrameNumber) {
489 mQueueItems.removeAt(0);
490 android_atomic_dec(&mQueuedFrames);
491 }
492
493 mQueueItems.removeAt(0);
494 }
495
496 // Decrement the queued-frames count. Signal another event if we
497 // have more frames pending.
David Sodman0cf8f8d2017-12-20 18:19:45 -0800498 if ((queuedBuffer && android_atomic_dec(&mQueuedFrames) > 1) || mAutoRefresh) {
David Sodman0c69cad2017-08-21 12:12:51 -0700499 mFlinger->signalLayerUpdate();
500 }
501
502 // update the active buffer
David Sodman0cf8f8d2017-12-20 18:19:45 -0800503 mActiveBuffer = mConsumer->getCurrentBuffer(&mActiveBufferSlot);
504 getBE().compositionInfo.mBuffer = mActiveBuffer;
505 getBE().compositionInfo.mBufferSlot = mActiveBufferSlot;
506
507 if (mActiveBuffer == nullptr) {
David Sodman0c69cad2017-08-21 12:12:51 -0700508 // this can only happen if the very first buffer was rejected.
509 return outDirtyRegion;
510 }
511
512 mBufferLatched = true;
513 mPreviousFrameNumber = mCurrentFrameNumber;
Chia-I Wub28c6742017-12-27 10:59:54 -0800514 mCurrentFrameNumber = mConsumer->getFrameNumber();
David Sodman0c69cad2017-08-21 12:12:51 -0700515
516 {
517 Mutex::Autolock lock(mFrameEventHistoryMutex);
518 mFrameEventHistory.addLatch(mCurrentFrameNumber, latchTime);
David Sodman0c69cad2017-08-21 12:12:51 -0700519 }
520
521 mRefreshPending = true;
522 mFrameLatencyNeeded = true;
Peiyong Lin566a3b42018-01-09 18:22:43 -0800523 if (oldBuffer == nullptr) {
David Sodman0c69cad2017-08-21 12:12:51 -0700524 // the first time we receive a buffer, we need to trigger a
525 // geometry invalidation.
526 recomputeVisibleRegions = true;
527 }
528
Chia-I Wub28c6742017-12-27 10:59:54 -0800529 setDataSpace(mConsumer->getCurrentDataSpace());
David Sodman0c69cad2017-08-21 12:12:51 -0700530
Chia-I Wub28c6742017-12-27 10:59:54 -0800531 Rect crop(mConsumer->getCurrentCrop());
532 const uint32_t transform(mConsumer->getCurrentTransform());
533 const uint32_t scalingMode(mConsumer->getCurrentScalingMode());
David Sodman0cf8f8d2017-12-20 18:19:45 -0800534 if ((crop != mCurrentCrop) || (transform != mCurrentTransform) ||
David Sodman0c69cad2017-08-21 12:12:51 -0700535 (scalingMode != mCurrentScalingMode)) {
536 mCurrentCrop = crop;
537 mCurrentTransform = transform;
538 mCurrentScalingMode = scalingMode;
539 recomputeVisibleRegions = true;
540 }
541
Peiyong Lin566a3b42018-01-09 18:22:43 -0800542 if (oldBuffer != nullptr) {
David Sodman0cf8f8d2017-12-20 18:19:45 -0800543 uint32_t bufWidth = mActiveBuffer->getWidth();
544 uint32_t bufHeight = mActiveBuffer->getHeight();
545 if (bufWidth != uint32_t(oldBuffer->width) || bufHeight != uint32_t(oldBuffer->height)) {
David Sodman0c69cad2017-08-21 12:12:51 -0700546 recomputeVisibleRegions = true;
547 }
548 }
549
David Sodman0cf8f8d2017-12-20 18:19:45 -0800550 mCurrentOpacity = getOpacityForFormat(mActiveBuffer->format);
David Sodman0c69cad2017-08-21 12:12:51 -0700551 if (oldOpacity != isOpaque(s)) {
552 recomputeVisibleRegions = true;
553 }
554
555 // Remove any sync points corresponding to the buffer which was just
556 // latched
557 {
558 Mutex::Autolock lock(mLocalSyncPointMutex);
559 auto point = mLocalSyncPoints.begin();
560 while (point != mLocalSyncPoints.end()) {
561 if (!(*point)->frameIsAvailable() || !(*point)->transactionIsApplied()) {
562 // This sync point must have been added since we started
563 // latching. Don't drop it yet.
564 ++point;
565 continue;
566 }
567
568 if ((*point)->getFrameNumber() <= mCurrentFrameNumber) {
569 point = mLocalSyncPoints.erase(point);
570 } else {
571 ++point;
572 }
573 }
574 }
575
576 // FIXME: postedRegion should be dirty & bounds
577 Region dirtyRegion(Rect(s.active.w, s.active.h));
578
579 // transform the dirty region to window-manager space
580 outDirtyRegion = (getTransform().transform(dirtyRegion));
581
582 return outDirtyRegion;
583}
584
David Sodmaneb085e02017-10-05 18:49:04 -0700585void BufferLayer::setDefaultBufferSize(uint32_t w, uint32_t h) {
Chia-I Wub28c6742017-12-27 10:59:54 -0800586 mConsumer->setDefaultBufferSize(w, h);
David Sodmaneb085e02017-10-05 18:49:04 -0700587}
588
David Sodman0c69cad2017-08-21 12:12:51 -0700589void BufferLayer::setPerFrameData(const sp<const DisplayDevice>& displayDevice) {
590 // Apply this display's projection's viewport to the visible region
591 // before giving it to the HWC HAL.
592 const Transform& tr = displayDevice->getTransform();
593 const auto& viewport = displayDevice->getViewport();
594 Region visible = tr.transform(visibleRegion.intersect(viewport));
595 auto hwcId = displayDevice->getHwcDisplayId();
David Sodman0c69cad2017-08-21 12:12:51 -0700596
David Sodman4b7c4bc2017-11-17 12:13:59 -0800597 getBE().compositionInfo.hwc.visibleRegion = visible;
598 getBE().compositionInfo.hwc.surfaceDamage = surfaceDamageRegion;
David Sodman0c69cad2017-08-21 12:12:51 -0700599
600 // Sideband layers
David Sodman0cc69182017-11-17 12:12:07 -0800601 if (getBE().compositionInfo.hwc.sidebandStream.get()) {
David Sodman0c69cad2017-08-21 12:12:51 -0700602 setCompositionType(hwcId, HWC2::Composition::Sideband);
David Sodman4b7c4bc2017-11-17 12:13:59 -0800603 getBE().compositionInfo.compositionType = HWC2::Composition::Sideband;
David Sodman0c69cad2017-08-21 12:12:51 -0700604 return;
605 }
606
David Sodman0c69cad2017-08-21 12:12:51 -0700607 // Device or Cursor layers
608 if (mPotentialCursor) {
609 ALOGV("[%s] Requesting Cursor composition", mName.string());
610 setCompositionType(hwcId, HWC2::Composition::Cursor);
611 } else {
612 ALOGV("[%s] Requesting Device composition", mName.string());
613 setCompositionType(hwcId, HWC2::Composition::Device);
614 }
615
David Sodman4b7c4bc2017-11-17 12:13:59 -0800616 getBE().compositionInfo.hwc.dataspace = mDrawingState.dataSpace;
617 getBE().compositionInfo.hwc.hdrMetadata = mConsumer->getCurrentHdrMetadata();
David Sodman0c69cad2017-08-21 12:12:51 -0700618
Chia-I Wub28c6742017-12-27 10:59:54 -0800619 auto acquireFence = mConsumer->getCurrentFence();
David Sodman4b7c4bc2017-11-17 12:13:59 -0800620 getBE().compositionInfo.mBufferSlot = mActiveBufferSlot;
621 getBE().compositionInfo.mBuffer = mActiveBuffer;
622 getBE().compositionInfo.hwc.fence = acquireFence;
David Sodman0c69cad2017-08-21 12:12:51 -0700623}
624
David Sodman41fdfc92017-11-06 16:09:56 -0800625bool BufferLayer::isOpaque(const Layer::State& s) const {
David Sodman0c69cad2017-08-21 12:12:51 -0700626 // if we don't have a buffer or sidebandStream yet, we're translucent regardless of the
627 // layer's opaque flag.
David Sodman0cf8f8d2017-12-20 18:19:45 -0800628 if ((getBE().compositionInfo.hwc.sidebandStream == nullptr) && (mActiveBuffer == nullptr)) {
David Sodman0c69cad2017-08-21 12:12:51 -0700629 return false;
630 }
631
632 // if the layer has the opaque flag, then we're always opaque,
633 // otherwise we use the current buffer's format.
634 return ((s.flags & layer_state_t::eLayerOpaque) != 0) || mCurrentOpacity;
635}
636
637void BufferLayer::onFirstRef() {
638 // Creates a custom BufferQueue for SurfaceFlingerConsumer to use
639 sp<IGraphicBufferProducer> producer;
640 sp<IGraphicBufferConsumer> consumer;
641 BufferQueue::createBufferQueue(&producer, &consumer, true);
642 mProducer = new MonitoredProducer(producer, mFlinger, this);
David Sodman0cf8f8d2017-12-20 18:19:45 -0800643 mConsumer = new BufferLayerConsumer(consumer, mFlinger->getRenderEngine(), mTextureName, this);
Chia-I Wub28c6742017-12-27 10:59:54 -0800644 mConsumer->setConsumerUsageBits(getEffectiveUsage(0));
645 mConsumer->setContentsChangedListener(this);
646 mConsumer->setName(mName);
David Sodman0c69cad2017-08-21 12:12:51 -0700647
648 if (mFlinger->isLayerTripleBufferingDisabled()) {
649 mProducer->setMaxDequeuedBufferCount(2);
650 }
651
652 const sp<const DisplayDevice> hw(mFlinger->getDefaultDisplayDevice());
653 updateTransformHint(hw);
654}
655
656// ---------------------------------------------------------------------------
657// Interface implementation for SurfaceFlingerConsumer::ContentsChangedListener
658// ---------------------------------------------------------------------------
659
660void BufferLayer::onFrameAvailable(const BufferItem& item) {
661 // Add this buffer from our internal queue tracker
662 { // Autolock scope
663 Mutex::Autolock lock(mQueueItemLock);
Lloyd Pique4dccc412018-01-22 17:21:36 -0800664 mFlinger->mInterceptor->saveBufferUpdate(this, item.mGraphicBuffer->getWidth(),
665 item.mGraphicBuffer->getHeight(),
666 item.mFrameNumber);
David Sodman0c69cad2017-08-21 12:12:51 -0700667 // Reset the frame number tracker when we receive the first buffer after
668 // a frame number reset
669 if (item.mFrameNumber == 1) {
670 mLastFrameNumberReceived = 0;
671 }
672
673 // Ensure that callbacks are handled in order
674 while (item.mFrameNumber != mLastFrameNumberReceived + 1) {
David Sodman0cf8f8d2017-12-20 18:19:45 -0800675 status_t result = mQueueItemCondition.waitRelative(mQueueItemLock, ms2ns(500));
David Sodman0c69cad2017-08-21 12:12:51 -0700676 if (result != NO_ERROR) {
677 ALOGE("[%s] Timed out waiting on callback", mName.string());
678 }
679 }
680
681 mQueueItems.push_back(item);
682 android_atomic_inc(&mQueuedFrames);
683
684 // Wake up any pending callbacks
685 mLastFrameNumberReceived = item.mFrameNumber;
686 mQueueItemCondition.broadcast();
687 }
688
689 mFlinger->signalLayerUpdate();
690}
691
692void BufferLayer::onFrameReplaced(const BufferItem& item) {
693 { // Autolock scope
694 Mutex::Autolock lock(mQueueItemLock);
695
696 // Ensure that callbacks are handled in order
697 while (item.mFrameNumber != mLastFrameNumberReceived + 1) {
David Sodman0cf8f8d2017-12-20 18:19:45 -0800698 status_t result = mQueueItemCondition.waitRelative(mQueueItemLock, 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 if (mQueueItems.empty()) {
705 ALOGE("Can't replace a frame on an empty queue");
706 return;
707 }
708 mQueueItems.editItemAt(mQueueItems.size() - 1) = item;
709
710 // Wake up any pending callbacks
711 mLastFrameNumberReceived = item.mFrameNumber;
712 mQueueItemCondition.broadcast();
713 }
714}
715
716void BufferLayer::onSidebandStreamChanged() {
717 if (android_atomic_release_cas(false, true, &mSidebandStreamChanged) == 0) {
718 // mSidebandStreamChanged was false
719 mFlinger->signalLayerUpdate();
720 }
721}
722
723bool BufferLayer::needsFiltering(const RenderArea& renderArea) const {
724 return mNeedsFiltering || renderArea.needsFiltering();
725}
726
727// As documented in libhardware header, formats in the range
728// 0x100 - 0x1FF are specific to the HAL implementation, and
729// are known to have no alpha channel
730// TODO: move definition for device-specific range into
731// hardware.h, instead of using hard-coded values here.
732#define HARDWARE_IS_DEVICE_FORMAT(f) ((f) >= 0x100 && (f) <= 0x1FF)
733
734bool BufferLayer::getOpacityForFormat(uint32_t format) {
735 if (HARDWARE_IS_DEVICE_FORMAT(format)) {
736 return true;
737 }
738 switch (format) {
739 case HAL_PIXEL_FORMAT_RGBA_8888:
740 case HAL_PIXEL_FORMAT_BGRA_8888:
741 case HAL_PIXEL_FORMAT_RGBA_FP16:
742 case HAL_PIXEL_FORMAT_RGBA_1010102:
743 return false;
744 }
745 // in all other case, we have no blending (also for unknown formats)
746 return true;
747}
748
David Sodman41fdfc92017-11-06 16:09:56 -0800749void BufferLayer::drawWithOpenGL(const RenderArea& renderArea, bool useIdentityTransform) const {
David Sodman0c69cad2017-08-21 12:12:51 -0700750 const State& s(getDrawingState());
751
David Sodman9eeae692017-11-02 10:53:32 -0700752 computeGeometry(renderArea, getBE().mMesh, useIdentityTransform);
David Sodman0c69cad2017-08-21 12:12:51 -0700753
754 /*
755 * NOTE: the way we compute the texture coordinates here produces
756 * different results than when we take the HWC path -- in the later case
757 * the "source crop" is rounded to texel boundaries.
758 * This can produce significantly different results when the texture
759 * is scaled by a large amount.
760 *
761 * The GL code below is more logical (imho), and the difference with
762 * HWC is due to a limitation of the HWC API to integers -- a question
763 * is suspend is whether we should ignore this problem or revert to
764 * GL composition when a buffer scaling is applied (maybe with some
765 * minimal value)? Or, we could make GL behave like HWC -- but this feel
766 * like more of a hack.
767 */
Dan Stoza80d61162017-12-20 15:57:52 -0800768 const Rect bounds{computeBounds()}; // Rounds from FloatRect
David Sodman0c69cad2017-08-21 12:12:51 -0700769
770 Transform t = getTransform();
Dan Stoza80d61162017-12-20 15:57:52 -0800771 Rect win = bounds;
David Sodman0c69cad2017-08-21 12:12:51 -0700772 if (!s.finalCrop.isEmpty()) {
773 win = t.transform(win);
774 if (!win.intersect(s.finalCrop, &win)) {
775 win.clear();
776 }
777 win = t.inverse().transform(win);
Dan Stoza80d61162017-12-20 15:57:52 -0800778 if (!win.intersect(bounds, &win)) {
David Sodman0c69cad2017-08-21 12:12:51 -0700779 win.clear();
780 }
781 }
782
783 float left = float(win.left) / float(s.active.w);
784 float top = float(win.top) / float(s.active.h);
785 float right = float(win.right) / float(s.active.w);
786 float bottom = float(win.bottom) / float(s.active.h);
787
788 // TODO: we probably want to generate the texture coords with the mesh
789 // here we assume that we only have 4 vertices
David Sodman9eeae692017-11-02 10:53:32 -0700790 Mesh::VertexArray<vec2> texCoords(getBE().mMesh.getTexCoordArray<vec2>());
David Sodman0c69cad2017-08-21 12:12:51 -0700791 texCoords[0] = vec2(left, 1.0f - top);
792 texCoords[1] = vec2(left, 1.0f - bottom);
793 texCoords[2] = vec2(right, 1.0f - bottom);
794 texCoords[3] = vec2(right, 1.0f - top);
795
Bo Hu6ce838d2018-03-27 17:31:10 +0000796 getBE().compositionInfo.re.preMultipliedAlpha = mPremultipliedAlpha;
797 getBE().compositionInfo.re.opaque = isOpaque(s);
798 getBE().compositionInfo.re.disableTexture = false;
799 getBE().compositionInfo.re.color = getColor();
800 getBE().compositionInfo.hwc.dataspace = mCurrentState.dataSpace;
Chia-I Wu5c6e4632018-01-11 08:54:38 -0800801
Chia-I Wu8d2651e2018-01-24 12:18:49 -0800802 if (mCurrentState.dataSpace == HAL_DATASPACE_BT2020_ITU_PQ &&
Chia-I Wu5c6e4632018-01-11 08:54:38 -0800803 mConsumer->getCurrentApi() == NATIVE_WINDOW_API_MEDIA &&
804 getBE().compositionInfo.mBuffer->getPixelFormat() == HAL_PIXEL_FORMAT_RGBA_1010102) {
Bo Hu6ce838d2018-03-27 17:31:10 +0000805 getBE().compositionInfo.re.Y410BT2020 = true;
Chia-I Wu5c6e4632018-01-11 08:54:38 -0800806 }
David Sodman0c69cad2017-08-21 12:12:51 -0700807}
808
809uint32_t BufferLayer::getProducerStickyTransform() const {
810 int producerStickyTransform = 0;
811 int ret = mProducer->query(NATIVE_WINDOW_STICKY_TRANSFORM, &producerStickyTransform);
812 if (ret != OK) {
813 ALOGW("%s: Error %s (%d) while querying window sticky transform.", __FUNCTION__,
814 strerror(-ret), ret);
815 return 0;
816 }
817 return static_cast<uint32_t>(producerStickyTransform);
818}
819
820bool BufferLayer::latchUnsignaledBuffers() {
821 static bool propertyLoaded = false;
822 static bool latch = false;
823 static std::mutex mutex;
824 std::lock_guard<std::mutex> lock(mutex);
825 if (!propertyLoaded) {
826 char value[PROPERTY_VALUE_MAX] = {};
827 property_get("debug.sf.latch_unsignaled", value, "0");
828 latch = atoi(value);
829 propertyLoaded = true;
830 }
831 return latch;
832}
833
834uint64_t BufferLayer::getHeadFrameNumber() const {
835 Mutex::Autolock lock(mQueueItemLock);
836 if (!mQueueItems.empty()) {
837 return mQueueItems[0].mFrameNumber;
838 } else {
839 return mCurrentFrameNumber;
840 }
841}
842
843bool BufferLayer::headFenceHasSignaled() const {
David Sodman0c69cad2017-08-21 12:12:51 -0700844 if (latchUnsignaledBuffers()) {
845 return true;
846 }
847
848 Mutex::Autolock lock(mQueueItemLock);
849 if (mQueueItems.empty()) {
850 return true;
851 }
852 if (mQueueItems[0].mIsDroppable) {
853 // Even though this buffer's fence may not have signaled yet, it could
854 // be replaced by another buffer before it has a chance to, which means
855 // that it's possible to get into a situation where a buffer is never
856 // able to be latched. To avoid this, grab this buffer anyway.
857 return true;
858 }
David Sodman0cf8f8d2017-12-20 18:19:45 -0800859 return mQueueItems[0].mFenceTime->getSignalTime() != Fence::SIGNAL_TIME_PENDING;
David Sodman0c69cad2017-08-21 12:12:51 -0700860}
861
862uint32_t BufferLayer::getEffectiveScalingMode() const {
863 if (mOverrideScalingMode >= 0) {
864 return mOverrideScalingMode;
865 }
866 return mCurrentScalingMode;
867}
868
869// ----------------------------------------------------------------------------
870// transaction
871// ----------------------------------------------------------------------------
872
873void BufferLayer::notifyAvailableFrames() {
874 auto headFrameNumber = getHeadFrameNumber();
875 bool headFenceSignaled = headFenceHasSignaled();
876 Mutex::Autolock lock(mLocalSyncPointMutex);
877 for (auto& point : mLocalSyncPoints) {
878 if (headFrameNumber >= point->getFrameNumber() && headFenceSignaled) {
879 point->setFrameAvailable();
880 }
881 }
882}
883
884sp<IGraphicBufferProducer> BufferLayer::getProducer() const {
885 return mProducer;
886}
887
888// ---------------------------------------------------------------------------
889// h/w composer set-up
890// ---------------------------------------------------------------------------
891
892bool BufferLayer::allTransactionsSignaled() {
893 auto headFrameNumber = getHeadFrameNumber();
894 bool matchingFramesFound = false;
895 bool allTransactionsApplied = true;
896 Mutex::Autolock lock(mLocalSyncPointMutex);
897
898 for (auto& point : mLocalSyncPoints) {
899 if (point->getFrameNumber() > headFrameNumber) {
900 break;
901 }
902 matchingFramesFound = true;
903
904 if (!point->frameIsAvailable()) {
905 // We haven't notified the remote layer that the frame for
906 // this point is available yet. Notify it now, and then
907 // abort this attempt to latch.
908 point->setFrameAvailable();
909 allTransactionsApplied = false;
910 break;
911 }
912
913 allTransactionsApplied = allTransactionsApplied && point->transactionIsApplied();
914 }
915 return !matchingFramesFound || allTransactionsApplied;
916}
917
918} // namespace android
919
920#if defined(__gl_h_)
921#error "don't include gl/gl.h in this file"
922#endif
923
924#if defined(__gl2_h_)
925#error "don't include gl2/gl2.h in this file"
926#endif