blob: be9bce0795330f004ad5dfb09c0de3488d69b4b4 [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
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -080017// TODO(b/129481165): remove the #pragma below and fix conversion issues
18#pragma clang diagnostic push
19#pragma clang diagnostic ignored "-Wconversion"
20
David Sodman0c69cad2017-08-21 12:12:51 -070021//#define LOG_NDEBUG 0
22#undef LOG_TAG
23#define LOG_TAG "BufferLayer"
24#define ATRACE_TAG ATRACE_TAG_GRAPHICS
25
Alec Mourie60041e2019-06-14 18:59:51 -070026#include "BufferLayer.h"
Lloyd Piquefeb73d72018-12-04 17:23:44 -080027
28#include <compositionengine/CompositionEngine.h>
Lloyd Piquef5275482019-01-29 18:42:42 -080029#include <compositionengine/LayerFECompositionState.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080030#include <compositionengine/OutputLayer.h>
31#include <compositionengine/impl/OutputLayerCompositionState.h>
Lloyd Piquefeb73d72018-12-04 17:23:44 -080032#include <cutils/compiler.h>
33#include <cutils/native_handle.h>
34#include <cutils/properties.h>
35#include <gui/BufferItem.h>
36#include <gui/BufferQueue.h>
chaviwf83ce182019-09-12 14:43:08 -070037#include <gui/GLConsumer.h>
Lloyd Piquefeb73d72018-12-04 17:23:44 -080038#include <gui/LayerDebugInfo.h>
39#include <gui/Surface.h>
40#include <renderengine/RenderEngine.h>
41#include <ui/DebugUtils.h>
42#include <utils/Errors.h>
43#include <utils/Log.h>
44#include <utils/NativeHandle.h>
45#include <utils/StopWatch.h>
46#include <utils/Trace.h>
47
Alec Mourie60041e2019-06-14 18:59:51 -070048#include <cmath>
49#include <cstdlib>
50#include <mutex>
51#include <sstream>
52
David Sodman0c69cad2017-08-21 12:12:51 -070053#include "Colorizer.h"
54#include "DisplayDevice.h"
Mikael Pessa90092f42019-08-26 17:22:04 -070055#include "FrameTracer/FrameTracer.h"
David Sodman0c69cad2017-08-21 12:12:51 -070056#include "LayerRejecter.h"
Yiwei Zhang7e666a52018-11-15 13:33:42 -080057#include "TimeStats/TimeStats.h"
58
David Sodman0c69cad2017-08-21 12:12:51 -070059namespace android {
60
Peiyong Lin1a70eca2019-11-15 09:33:33 -080061static constexpr float defaultMaxMasteringLuminance = 1000.0;
62static constexpr float defaultMaxContentLuminance = 1000.0;
63
Lloyd Pique42ab75e2018-09-12 20:46:03 -070064BufferLayer::BufferLayer(const LayerCreationArgs& args)
Lloyd Piquefeb73d72018-12-04 17:23:44 -080065 : Layer(args),
chaviwb4c6e582019-08-16 14:35:07 -070066 mTextureName(args.textureName),
Lloyd Piquede196652020-01-22 17:29:58 -080067 mCompositionState{mFlinger->getCompositionEngine().createLayerFECompositionState()} {
Dominik Laskowski87a07e42019-10-10 20:38:02 -070068 ALOGV("Creating Layer %s", getDebugName());
David Sodman0c69cad2017-08-21 12:12:51 -070069
Lloyd Pique42ab75e2018-09-12 20:46:03 -070070 mPremultipliedAlpha = !(args.flags & ISurfaceComposerClient::eNonPremultiplied);
David Sodman0c69cad2017-08-21 12:12:51 -070071
Lloyd Pique42ab75e2018-09-12 20:46:03 -070072 mPotentialCursor = args.flags & ISurfaceComposerClient::eCursorWindow;
73 mProtectedByApp = args.flags & ISurfaceComposerClient::eProtectedByApp;
David Sodman0c69cad2017-08-21 12:12:51 -070074}
75
76BufferLayer::~BufferLayer() {
chaviwb4c6e582019-08-16 14:35:07 -070077 if (!isClone()) {
78 // The original layer and the clone layer share the same texture. Therefore, only one of
79 // the layers, in this case the original layer, needs to handle the deletion. The original
80 // layer and the clone should be removed at the same time so there shouldn't be any issue
81 // with the clone layer trying to use the deleted texture.
82 mFlinger->deleteTextureAsync(mTextureName);
83 }
Yiwei Zhang1a88c402019-11-18 10:43:58 -080084 const int32_t layerId = getSequence();
85 mFlinger->mTimeStats->onDestroy(layerId);
86 mFlinger->mFrameTracer->onDestroy(layerId);
David Sodman0c69cad2017-08-21 12:12:51 -070087}
88
David Sodmaneb085e02017-10-05 18:49:04 -070089void BufferLayer::useSurfaceDamage() {
90 if (mFlinger->mForceFullDamage) {
91 surfaceDamageRegion = Region::INVALID_REGION;
92 } else {
chaviw4244e032019-09-04 11:27:49 -070093 surfaceDamageRegion = mBufferInfo.mSurfaceDamage;
David Sodmaneb085e02017-10-05 18:49:04 -070094 }
95}
96
97void BufferLayer::useEmptyDamage() {
98 surfaceDamageRegion.clear();
99}
100
Marissa Wallfd668622018-05-10 10:21:13 -0700101bool BufferLayer::isOpaque(const Layer::State& s) const {
102 // if we don't have a buffer or sidebandStream yet, we're translucent regardless of the
103 // layer's opaque flag.
chaviwd62d3062019-09-04 14:48:02 -0700104 if ((mSidebandStream == nullptr) && (mBufferInfo.mBuffer == nullptr)) {
Marissa Wallfd668622018-05-10 10:21:13 -0700105 return false;
106 }
107
108 // if the layer has the opaque flag, then we're always opaque,
109 // otherwise we use the current buffer's format.
110 return ((s.flags & layer_state_t::eLayerOpaque) != 0) || getOpacityForFormat(getPixelFormat());
David Sodman0c69cad2017-08-21 12:12:51 -0700111}
112
113bool BufferLayer::isVisible() const {
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -0700114 return !isHiddenByPolicy() && getAlpha() > 0.0f &&
chaviwd62d3062019-09-04 14:48:02 -0700115 (mBufferInfo.mBuffer != nullptr || mSidebandStream != nullptr);
David Sodman0c69cad2017-08-21 12:12:51 -0700116}
117
118bool BufferLayer::isFixedSize() const {
119 return getEffectiveScalingMode() != NATIVE_WINDOW_SCALING_MODE_FREEZE;
120}
121
Lloyd Piquea83776c2019-01-29 18:42:32 -0800122bool BufferLayer::usesSourceCrop() const {
123 return true;
124}
125
David Sodman0c69cad2017-08-21 12:12:51 -0700126static constexpr mat4 inverseOrientation(uint32_t transform) {
David Sodman41fdfc92017-11-06 16:09:56 -0800127 const mat4 flipH(-1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1);
128 const mat4 flipV(1, 0, 0, 0, 0, -1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1);
129 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 -0700130 mat4 tr;
131
132 if (transform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
133 tr = tr * rot90;
134 }
135 if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_H) {
136 tr = tr * flipH;
137 }
138 if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_V) {
139 tr = tr * flipV;
140 }
141 return inverse(tr);
142}
143
Vishnu Nair9b079a22020-01-21 14:36:08 -0800144std::optional<compositionengine::LayerFE::LayerSettings> BufferLayer::prepareClientComposition(
Lloyd Piquef16688f2019-02-19 17:47:57 -0800145 compositionengine::LayerFE::ClientCompositionTargetSettings& targetSettings) {
David Sodman0c69cad2017-08-21 12:12:51 -0700146 ATRACE_CALL();
Lloyd Piquef16688f2019-02-19 17:47:57 -0800147
Vishnu Nair9b079a22020-01-21 14:36:08 -0800148 std::optional<compositionengine::LayerFE::LayerSettings> result =
149 Layer::prepareClientComposition(targetSettings);
Lloyd Piquef16688f2019-02-19 17:47:57 -0800150 if (!result) {
151 return result;
152 }
153
chaviwd62d3062019-09-04 14:48:02 -0700154 if (CC_UNLIKELY(mBufferInfo.mBuffer == 0)) {
David Sodman0c69cad2017-08-21 12:12:51 -0700155 // the texture has not been created yet, this Layer has
156 // in fact never been drawn into. This happens frequently with
157 // SurfaceView because the WindowManager can't know when the client
158 // has drawn the first time.
159
160 // If there is nothing under us, we paint the screen in black, otherwise
161 // we just skip this update.
162
163 // figure out if there is something below us
164 Region under;
165 bool finished = false;
166 mFlinger->mDrawingState.traverseInZOrder([&](Layer* layer) {
167 if (finished || layer == static_cast<BufferLayer const*>(this)) {
168 finished = true;
169 return;
170 }
Lloyd Piquea2468662019-03-07 21:31:06 -0800171
172 under.orSelf(layer->getScreenBounds());
David Sodman0c69cad2017-08-21 12:12:51 -0700173 });
174 // if not everything below us is covered, we plug the holes!
Lloyd Piquef16688f2019-02-19 17:47:57 -0800175 Region holes(targetSettings.clip.subtract(under));
David Sodman0c69cad2017-08-21 12:12:51 -0700176 if (!holes.isEmpty()) {
Lloyd Piquef16688f2019-02-19 17:47:57 -0800177 targetSettings.clearRegion.orSelf(holes);
David Sodman0c69cad2017-08-21 12:12:51 -0700178 }
Tianhua Sundcff00c2020-12-29 07:20:55 -0500179
180 if (mSidebandStream != nullptr) {
181 // For surfaceview of tv sideband, there is no activeBuffer
182 // in bufferqueue, we need return LayerSettings.
183 return result;
184 } else {
185 return std::nullopt;
186 }
David Sodman0c69cad2017-08-21 12:12:51 -0700187 }
Ady Abraham7a60afb2021-03-29 13:20:55 -0700188 const bool blackOutLayer = (isProtected() && !targetSettings.supportsProtectedContent) ||
Lloyd Piquef16688f2019-02-19 17:47:57 -0800189 (isSecure() && !targetSettings.isSecure);
Ady Abraham7a60afb2021-03-29 13:20:55 -0700190 const bool bufferCanBeUsedAsHwTexture =
Alec Mouri617752f2021-04-15 16:27:01 +0000191 mBufferInfo.mBuffer->getUsage() & GraphicBuffer::USAGE_HW_TEXTURE;
Lloyd Piquede196652020-01-22 17:29:58 -0800192 compositionengine::LayerFE::LayerSettings& layer = *result;
Ady Abraham7a60afb2021-03-29 13:20:55 -0700193 if (blackOutLayer || !bufferCanBeUsedAsHwTexture) {
194 ALOGE_IF(!bufferCanBeUsedAsHwTexture, "%s is blacked out as buffer is not gpu readable",
195 mName.c_str());
Vishnu Nairb87d94f2020-02-13 09:17:36 -0800196 prepareClearClientComposition(layer, true /* blackout */);
197 return layer;
David Sodman0c69cad2017-08-21 12:12:51 -0700198 }
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000199
Vishnu Nairb87d94f2020-02-13 09:17:36 -0800200 const State& s(getDrawingState());
201 layer.source.buffer.buffer = mBufferInfo.mBuffer;
202 layer.source.buffer.isOpaque = isOpaque(s);
203 layer.source.buffer.fence = mBufferInfo.mFence;
204 layer.source.buffer.textureName = mTextureName;
205 layer.source.buffer.usePremultipliedAlpha = getPremultipledAlpha();
206 layer.source.buffer.isY410BT2020 = isHdrY410();
207 bool hasSmpte2086 = mBufferInfo.mHdrMetadata.validTypes & HdrMetadata::SMPTE2086;
208 bool hasCta861_3 = mBufferInfo.mHdrMetadata.validTypes & HdrMetadata::CTA861_3;
209 layer.source.buffer.maxMasteringLuminance = hasSmpte2086
210 ? mBufferInfo.mHdrMetadata.smpte2086.maxLuminance
211 : defaultMaxMasteringLuminance;
212 layer.source.buffer.maxContentLuminance = hasCta861_3
213 ? mBufferInfo.mHdrMetadata.cta8613.maxContentLightLevel
214 : defaultMaxContentLuminance;
215 layer.frameNumber = mCurrentFrameNumber;
Alec Mouri617752f2021-04-15 16:27:01 +0000216 layer.bufferId = mBufferInfo.mBuffer ? mBufferInfo.mBuffer->getId() : 0;
Vishnu Nairb87d94f2020-02-13 09:17:36 -0800217
Vishnu Naire7f79c52020-10-29 14:45:03 -0700218 const bool useFiltering =
219 targetSettings.needsFiltering || mNeedsFiltering || bufferNeedsFiltering();
Vishnu Nairb87d94f2020-02-13 09:17:36 -0800220
221 // Query the texture matrix given our current filtering mode.
222 float textureMatrix[16];
223 getDrawingTransformMatrix(useFiltering, textureMatrix);
224
225 if (getTransformToDisplayInverse()) {
226 /*
227 * the code below applies the primary display's inverse transform to
228 * the texture transform
229 */
230 uint32_t transform = DisplayDevice::getPrimaryDisplayRotationFlags();
231 mat4 tr = inverseOrientation(transform);
232
233 /**
234 * TODO(b/36727915): This is basically a hack.
235 *
236 * Ensure that regardless of the parent transformation,
237 * this buffer is always transformed from native display
238 * orientation to display orientation. For example, in the case
239 * of a camera where the buffer remains in native orientation,
240 * we want the pixels to always be upright.
241 */
242 sp<Layer> p = mDrawingParent.promote();
243 if (p != nullptr) {
244 const auto parentTransform = p->getTransform();
245 tr = tr * inverseOrientation(parentTransform.getOrientation());
246 }
247
248 // and finally apply it to the original texture matrix
249 const mat4 texTransform(mat4(static_cast<const float*>(textureMatrix)) * tr);
250 memcpy(textureMatrix, texTransform.asArray(), sizeof(textureMatrix));
251 }
252
253 const Rect win{getBounds()};
254 float bufferWidth = getBufferSize(s).getWidth();
255 float bufferHeight = getBufferSize(s).getHeight();
256
257 // BufferStateLayers can have a "buffer size" of [0, 0, -1, -1] when no display frame has
258 // been set and there is no parent layer bounds. In that case, the scale is meaningless so
259 // ignore them.
260 if (!getBufferSize(s).isValid()) {
261 bufferWidth = float(win.right) - float(win.left);
262 bufferHeight = float(win.bottom) - float(win.top);
263 }
264
265 const float scaleHeight = (float(win.bottom) - float(win.top)) / bufferHeight;
266 const float scaleWidth = (float(win.right) - float(win.left)) / bufferWidth;
267 const float translateY = float(win.top) / bufferHeight;
268 const float translateX = float(win.left) / bufferWidth;
269
270 // Flip y-coordinates because GLConsumer expects OpenGL convention.
271 mat4 tr = mat4::translate(vec4(.5, .5, 0, 1)) * mat4::scale(vec4(1, -1, 1, 1)) *
272 mat4::translate(vec4(-.5, -.5, 0, 1)) *
273 mat4::translate(vec4(translateX, translateY, 0, 1)) *
274 mat4::scale(vec4(scaleWidth, scaleHeight, 1.0, 1.0));
275
276 layer.source.buffer.useTextureFiltering = useFiltering;
277 layer.source.buffer.textureTransform = mat4(static_cast<const float*>(textureMatrix)) * tr;
278
Vishnu Nair9b079a22020-01-21 14:36:08 -0800279 return layer;
David Sodman0c69cad2017-08-21 12:12:51 -0700280}
281
Marissa Wallfd668622018-05-10 10:21:13 -0700282bool BufferLayer::isHdrY410() const {
283 // pixel format is HDR Y410 masquerading as RGBA_1010102
chaviw4244e032019-09-04 11:27:49 -0700284 return (mBufferInfo.mDataspace == ui::Dataspace::BT2020_ITU_PQ &&
285 mBufferInfo.mApi == NATIVE_WINDOW_API_MEDIA &&
chaviwdebadb82020-03-26 14:57:24 -0700286 mBufferInfo.mPixelFormat == HAL_PIXEL_FORMAT_RGBA_1010102);
David Sodmaneb085e02017-10-05 18:49:04 -0700287}
288
Lloyd Piquede196652020-01-22 17:29:58 -0800289sp<compositionengine::LayerFE> BufferLayer::getCompositionEngineLayerFE() const {
290 return asLayerFE();
291}
292
293compositionengine::LayerFECompositionState* BufferLayer::editCompositionState() {
294 return mCompositionState.get();
295}
296
297const compositionengine::LayerFECompositionState* BufferLayer::getCompositionState() const {
298 return mCompositionState.get();
299}
300
301void BufferLayer::preparePerFrameCompositionState() {
302 Layer::preparePerFrameCompositionState();
David Sodman0c69cad2017-08-21 12:12:51 -0700303
304 // Sideband layers
Lloyd Piquede196652020-01-22 17:29:58 -0800305 auto* compositionState = editCompositionState();
306 if (compositionState->sidebandStream.get()) {
307 compositionState->compositionType = Hwc2::IComposerClient::Composition::SIDEBAND;
Robert Carra6bb2bc2020-04-08 11:03:23 -0700308 return;
David Sodman15094112018-10-11 09:39:37 -0700309 } else {
Lloyd Piquef5275482019-01-29 18:42:42 -0800310 // Normal buffer layers
Lloyd Piquede196652020-01-22 17:29:58 -0800311 compositionState->hdrMetadata = mBufferInfo.mHdrMetadata;
312 compositionState->compositionType = mPotentialCursor
Lloyd Piquef5275482019-01-29 18:42:42 -0800313 ? Hwc2::IComposerClient::Composition::CURSOR
314 : Hwc2::IComposerClient::Composition::DEVICE;
David Sodman0c69cad2017-08-21 12:12:51 -0700315 }
Robert Carra6bb2bc2020-04-08 11:03:23 -0700316
Alec Mouri617752f2021-04-15 16:27:01 +0000317 compositionState->buffer = mBufferInfo.mBuffer;
Robert Carra6bb2bc2020-04-08 11:03:23 -0700318 compositionState->bufferSlot = (mBufferInfo.mBufferSlot == BufferQueue::INVALID_BUFFER_SLOT)
319 ? 0
320 : mBufferInfo.mBufferSlot;
321 compositionState->acquireFence = mBufferInfo.mFence;
David Sodman0c69cad2017-08-21 12:12:51 -0700322}
323
Marissa Wallfd668622018-05-10 10:21:13 -0700324bool BufferLayer::onPreComposition(nsecs_t refreshStartTime) {
chaviwd62d3062019-09-04 14:48:02 -0700325 if (mBufferInfo.mBuffer != nullptr) {
Marissa Wallfd668622018-05-10 10:21:13 -0700326 Mutex::Autolock lock(mFrameEventHistoryMutex);
327 mFrameEventHistory.addPreComposition(mCurrentFrameNumber, refreshStartTime);
David Sodman0c69cad2017-08-21 12:12:51 -0700328 }
Marissa Wallfd668622018-05-10 10:21:13 -0700329 mRefreshPending = false;
330 return hasReadyFrame();
David Sodman0c69cad2017-08-21 12:12:51 -0700331}
Ady Abraham8b9e6122021-01-26 19:11:45 -0800332namespace {
333TimeStats::SetFrameRateVote frameRateToSetFrameRateVotePayload(Layer::FrameRate frameRate) {
334 using FrameRateCompatibility = TimeStats::SetFrameRateVote::FrameRateCompatibility;
335 using Seamlessness = TimeStats::SetFrameRateVote::Seamlessness;
336 const auto frameRateCompatibility = [frameRate] {
337 switch (frameRate.type) {
338 case Layer::FrameRateCompatibility::Default:
339 return FrameRateCompatibility::Default;
340 case Layer::FrameRateCompatibility::ExactOrMultiple:
341 return FrameRateCompatibility::ExactOrMultiple;
342 default:
343 return FrameRateCompatibility::Undefined;
344 }
345 }();
346
347 const auto seamlessness = [frameRate] {
348 switch (frameRate.seamlessness) {
349 case scheduler::Seamlessness::OnlySeamless:
350 return Seamlessness::ShouldBeSeamless;
351 case scheduler::Seamlessness::SeamedAndSeamless:
352 return Seamlessness::NotRequired;
353 default:
354 return Seamlessness::Undefined;
355 }
356 }();
357
358 return TimeStats::SetFrameRateVote{.frameRate = frameRate.rate.getValue(),
359 .frameRateCompatibility = frameRateCompatibility,
360 .seamlessness = seamlessness};
361}
362} // namespace
David Sodman0c69cad2017-08-21 12:12:51 -0700363
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700364bool BufferLayer::onPostComposition(const DisplayDevice* display,
Dominik Laskowski075d3172018-05-24 15:50:06 -0700365 const std::shared_ptr<FenceTime>& glDoneFence,
Marissa Wallfd668622018-05-10 10:21:13 -0700366 const std::shared_ptr<FenceTime>& presentFence,
367 const CompositorTiming& compositorTiming) {
368 // mFrameLatencyNeeded is true when a new frame was latched for the
369 // composition.
chaviw74b03172019-08-19 11:09:03 -0700370 if (!mBufferInfo.mFrameLatencyNeeded) return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700371
372 // Update mFrameEventHistory.
Dan Stoza436ccf32018-06-21 12:10:12 -0700373 {
Marissa Wallfd668622018-05-10 10:21:13 -0700374 Mutex::Autolock lock(mFrameEventHistoryMutex);
375 mFrameEventHistory.addPostComposition(mCurrentFrameNumber, glDoneFence, presentFence,
376 compositorTiming);
Valerie Hau93d5a5e2020-02-13 14:50:01 -0800377 finalizeFrameEventHistory(glDoneFence, compositorTiming);
David Sodman0c69cad2017-08-21 12:12:51 -0700378 }
379
Marissa Wallfd668622018-05-10 10:21:13 -0700380 // Update mFrameTracker.
chaviw4244e032019-09-04 11:27:49 -0700381 nsecs_t desiredPresentTime = mBufferInfo.mDesiredPresentTime;
Marissa Wallfd668622018-05-10 10:21:13 -0700382 mFrameTracker.setDesiredPresentTime(desiredPresentTime);
383
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800384 const int32_t layerId = getSequence();
385 mFlinger->mTimeStats->setDesiredTime(layerId, mCurrentFrameNumber, desiredPresentTime);
Marissa Wallfd668622018-05-10 10:21:13 -0700386
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700387 const auto outputLayer = findOutputLayerForDisplay(display);
Adithya Srinivasanb69e0762019-11-11 18:39:53 -0800388 if (outputLayer && outputLayer->requiresClientComposition()) {
389 nsecs_t clientCompositionTimestamp = outputLayer->getState().clientCompositionTimestamp;
390 mFlinger->mFrameTracer->traceTimestamp(layerId, getCurrentBufferId(), mCurrentFrameNumber,
391 clientCompositionTimestamp,
392 FrameTracer::FrameEvent::FALLBACK_COMPOSITION);
Adithya Srinivasanb6a2fa12021-03-13 00:23:09 +0000393 // Update the SurfaceFrames in the drawing state
394 if (mDrawingState.bufferSurfaceFrameTX) {
395 mDrawingState.bufferSurfaceFrameTX->setGpuComposition();
396 }
397 for (auto& [token, surfaceFrame] : mDrawingState.bufferlessSurfaceFramesTX) {
398 surfaceFrame->setGpuComposition();
399 }
Adithya Srinivasanb69e0762019-11-11 18:39:53 -0800400 }
401
chaviw4244e032019-09-04 11:27:49 -0700402 std::shared_ptr<FenceTime> frameReadyFence = mBufferInfo.mFenceTime;
Marissa Wallfd668622018-05-10 10:21:13 -0700403 if (frameReadyFence->isValid()) {
404 mFrameTracker.setFrameReadyFence(std::move(frameReadyFence));
405 } else {
406 // There was no fence for this frame, so assume that it was ready
407 // to be presented at the desired present time.
408 mFrameTracker.setFrameReadyTime(desiredPresentTime);
Dominik Laskowski45de9bd2018-06-11 17:44:10 -0700409 }
Marissa Wallfd668622018-05-10 10:21:13 -0700410
Alec Mouri7d436ec2021-01-27 20:40:50 -0800411 const Fps refreshRate = mFlinger->mRefreshRateConfigs->getCurrentRefreshRate().getFps();
412 const std::optional<Fps> renderRate = mFlinger->mScheduler->getFrameRateOverride(getOwnerUid());
Marissa Wallfd668622018-05-10 10:21:13 -0700413 if (presentFence->isValid()) {
Alec Mouri7d436ec2021-01-27 20:40:50 -0800414 mFlinger->mTimeStats->setPresentFence(layerId, mCurrentFrameNumber, presentFence,
Ady Abraham8b9e6122021-01-26 19:11:45 -0800415 refreshRate, renderRate,
416 frameRateToSetFrameRateVotePayload(
417 mDrawingState.frameRate));
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800418 mFlinger->mFrameTracer->traceFence(layerId, getCurrentBufferId(), mCurrentFrameNumber,
Mikael Pessa90092f42019-08-26 17:22:04 -0700419 presentFence, FrameTracer::FrameEvent::PRESENT_FENCE);
Marissa Wallfd668622018-05-10 10:21:13 -0700420 mFrameTracker.setActualPresentFence(std::shared_ptr<FenceTime>(presentFence));
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700421 } else if (!display) {
422 // Do nothing.
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200423 } else if (const auto displayId = PhysicalDisplayId::tryCast(display->getId());
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700424 displayId && mFlinger->getHwComposer().isConnected(*displayId)) {
Marissa Wallfd668622018-05-10 10:21:13 -0700425 // The HWC doesn't support present fences, so use the refresh
426 // timestamp instead.
Marin Shalamanov045b7002021-01-07 16:56:24 +0100427 const nsecs_t actualPresentTime = display->getRefreshTimestamp();
Alec Mouri7d436ec2021-01-27 20:40:50 -0800428 mFlinger->mTimeStats->setPresentTime(layerId, mCurrentFrameNumber, actualPresentTime,
Ady Abraham8b9e6122021-01-26 19:11:45 -0800429 refreshRate, renderRate,
430 frameRateToSetFrameRateVotePayload(
431 mDrawingState.frameRate));
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800432 mFlinger->mFrameTracer->traceTimestamp(layerId, getCurrentBufferId(), mCurrentFrameNumber,
Mikael Pessa90092f42019-08-26 17:22:04 -0700433 actualPresentTime,
434 FrameTracer::FrameEvent::PRESENT_FENCE);
Marissa Wallfd668622018-05-10 10:21:13 -0700435 mFrameTracker.setActualPresentTime(actualPresentTime);
436 }
437
438 mFrameTracker.advanceFrame();
chaviw74b03172019-08-19 11:09:03 -0700439 mBufferInfo.mFrameLatencyNeeded = false;
Marissa Wallfd668622018-05-10 10:21:13 -0700440 return true;
David Sodman0c69cad2017-08-21 12:12:51 -0700441}
442
chaviwdebadb82020-03-26 14:57:24 -0700443void BufferLayer::gatherBufferInfo() {
444 mBufferInfo.mPixelFormat =
Alec Mouri617752f2021-04-15 16:27:01 +0000445 !mBufferInfo.mBuffer ? PIXEL_FORMAT_NONE : mBufferInfo.mBuffer->format;
chaviwdebadb82020-03-26 14:57:24 -0700446 mBufferInfo.mFrameLatencyNeeded = true;
447}
448
Ady Abraham63a3e592021-01-06 10:47:15 -0800449bool BufferLayer::shouldPresentNow(nsecs_t expectedPresentTime) const {
450 // If this is not a valid vsync for the layer's uid, return and try again later
451 const bool isVsyncValidForUid =
452 mFlinger->mScheduler->isVsyncValid(expectedPresentTime, mOwnerUid);
453 if (!isVsyncValidForUid) {
454 ATRACE_NAME("!isVsyncValidForUid");
455 return false;
456 }
457
458 // AutoRefresh layers and sideband streams should always be presented
459 if (getSidebandStreamChanged() || getAutoRefresh()) {
460 return true;
461 }
462
Ady Abraham63a3e592021-01-06 10:47:15 -0800463 // If this layer doesn't have a frame is shouldn't be presented
464 if (!hasFrameUpdate()) {
465 return false;
466 }
467
468 // Defer to the derived class to decide whether the next buffer is due for
469 // presentation.
470 return isBufferDue(expectedPresentTime);
471}
472
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700473bool BufferLayer::latchBuffer(bool& recomputeVisibleRegions, nsecs_t latchTime,
474 nsecs_t expectedPresentTime) {
Marissa Wallfd668622018-05-10 10:21:13 -0700475 ATRACE_CALL();
David Sodman0c69cad2017-08-21 12:12:51 -0700476
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800477 bool refreshRequired = latchSidebandStream(recomputeVisibleRegions);
David Sodman0c69cad2017-08-21 12:12:51 -0700478
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800479 if (refreshRequired) {
480 return refreshRequired;
David Sodman0c69cad2017-08-21 12:12:51 -0700481 }
482
Marissa Wallfd668622018-05-10 10:21:13 -0700483 if (!hasReadyFrame()) {
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800484 return false;
David Sodman0c69cad2017-08-21 12:12:51 -0700485 }
David Sodman0c69cad2017-08-21 12:12:51 -0700486
Marissa Wallfd668622018-05-10 10:21:13 -0700487 // if we've already called updateTexImage() without going through
488 // a composition step, we have to skip this layer at this point
489 // because we cannot call updateTeximage() without a corresponding
490 // compositionComplete() call.
491 // we'll trigger an update in onPreComposition().
492 if (mRefreshPending) {
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800493 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700494 }
495
496 // If the head buffer's acquire fence hasn't signaled yet, return and
497 // try again later
498 if (!fenceHasSignaled()) {
Ady Abraham09bd3922019-04-08 10:44:56 -0700499 ATRACE_NAME("!fenceHasSignaled()");
David Sodman0c69cad2017-08-21 12:12:51 -0700500 mFlinger->signalLayerUpdate();
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800501 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700502 }
503
504 // Capture the old state of the layer for comparisons later
505 const State& s(getDrawingState());
506 const bool oldOpacity = isOpaque(s);
chaviwd62d3062019-09-04 14:48:02 -0700507
508 BufferInfo oldBufferInfo = mBufferInfo;
Marissa Wallfd668622018-05-10 10:21:13 -0700509
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700510 if (!allTransactionsSignaled(expectedPresentTime)) {
Marissa Wallebb486e2019-05-15 14:08:08 -0700511 mFlinger->setTransactionFlags(eTraversalNeeded);
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800512 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700513 }
514
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700515 status_t err = updateTexImage(recomputeVisibleRegions, latchTime, expectedPresentTime);
Marissa Wallfd668622018-05-10 10:21:13 -0700516 if (err != NO_ERROR) {
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800517 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700518 }
519
520 err = updateActiveBuffer();
521 if (err != NO_ERROR) {
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800522 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700523 }
524
Marissa Wallfd668622018-05-10 10:21:13 -0700525 err = updateFrameNumber(latchTime);
526 if (err != NO_ERROR) {
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800527 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700528 }
529
chaviw4244e032019-09-04 11:27:49 -0700530 gatherBufferInfo();
531
Marissa Wallfd668622018-05-10 10:21:13 -0700532 mRefreshPending = true;
chaviwd62d3062019-09-04 14:48:02 -0700533 if (oldBufferInfo.mBuffer == nullptr) {
Marissa Wallfd668622018-05-10 10:21:13 -0700534 // the first time we receive a buffer, we need to trigger a
535 // geometry invalidation.
536 recomputeVisibleRegions = true;
537 }
538
chaviw4244e032019-09-04 11:27:49 -0700539 if ((mBufferInfo.mCrop != oldBufferInfo.mCrop) ||
540 (mBufferInfo.mTransform != oldBufferInfo.mTransform) ||
541 (mBufferInfo.mScaleMode != oldBufferInfo.mScaleMode) ||
542 (mBufferInfo.mTransformToDisplayInverse != oldBufferInfo.mTransformToDisplayInverse)) {
Marissa Wallfd668622018-05-10 10:21:13 -0700543 recomputeVisibleRegions = true;
544 }
545
chaviwd62d3062019-09-04 14:48:02 -0700546 if (oldBufferInfo.mBuffer != nullptr) {
Alec Mouri617752f2021-04-15 16:27:01 +0000547 uint32_t bufWidth = mBufferInfo.mBuffer->getWidth();
548 uint32_t bufHeight = mBufferInfo.mBuffer->getHeight();
549 if (bufWidth != uint32_t(oldBufferInfo.mBuffer->width) ||
550 bufHeight != uint32_t(oldBufferInfo.mBuffer->height)) {
Marissa Wallfd668622018-05-10 10:21:13 -0700551 recomputeVisibleRegions = true;
552 }
553 }
554
555 if (oldOpacity != isOpaque(s)) {
556 recomputeVisibleRegions = true;
557 }
558
559 // Remove any sync points corresponding to the buffer which was just
560 // latched
561 {
562 Mutex::Autolock lock(mLocalSyncPointMutex);
563 auto point = mLocalSyncPoints.begin();
564 while (point != mLocalSyncPoints.end()) {
565 if (!(*point)->frameIsAvailable() || !(*point)->transactionIsApplied()) {
566 // This sync point must have been added since we started
567 // latching. Don't drop it yet.
568 ++point;
569 continue;
570 }
571
572 if ((*point)->getFrameNumber() <= mCurrentFrameNumber) {
Alec Mourie60041e2019-06-14 18:59:51 -0700573 std::stringstream ss;
574 ss << "Dropping sync point " << (*point)->getFrameNumber();
575 ATRACE_NAME(ss.str().c_str());
Marissa Wallfd668622018-05-10 10:21:13 -0700576 point = mLocalSyncPoints.erase(point);
577 } else {
578 ++point;
579 }
580 }
581 }
582
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800583 return true;
Marissa Wallfd668622018-05-10 10:21:13 -0700584}
585
586// transaction
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700587void BufferLayer::notifyAvailableFrames(nsecs_t expectedPresentTime) {
588 const auto headFrameNumber = getHeadFrameNumber(expectedPresentTime);
Ady Abrahamcd1580c2019-04-29 15:40:03 -0700589 const bool headFenceSignaled = fenceHasSignaled();
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700590 const bool presentTimeIsCurrent = framePresentTimeIsCurrent(expectedPresentTime);
Marissa Wallfd668622018-05-10 10:21:13 -0700591 Mutex::Autolock lock(mLocalSyncPointMutex);
592 for (auto& point : mLocalSyncPoints) {
Ady Abrahamcd1580c2019-04-29 15:40:03 -0700593 if (headFrameNumber >= point->getFrameNumber() && headFenceSignaled &&
594 presentTimeIsCurrent) {
Marissa Wallfd668622018-05-10 10:21:13 -0700595 point->setFrameAvailable();
chaviw43cb3cb2019-05-31 15:23:41 -0700596 sp<Layer> requestedSyncLayer = point->getRequestedSyncLayer();
597 if (requestedSyncLayer) {
598 // Need to update the transaction flag to ensure the layer's pending transaction
599 // gets applied.
600 requestedSyncLayer->setTransactionFlags(eTransactionNeeded);
601 }
Marissa Wallfd668622018-05-10 10:21:13 -0700602 }
David Sodman0c69cad2017-08-21 12:12:51 -0700603 }
604}
605
Marissa Wallfd668622018-05-10 10:21:13 -0700606bool BufferLayer::hasReadyFrame() const {
Marissa Wall024a1912018-08-13 13:55:35 -0700607 return hasFrameUpdate() || getSidebandStreamChanged() || getAutoRefresh();
Marissa Wallfd668622018-05-10 10:21:13 -0700608}
609
610uint32_t BufferLayer::getEffectiveScalingMode() const {
chaviw4244e032019-09-04 11:27:49 -0700611 return mBufferInfo.mScaleMode;
Marissa Wallfd668622018-05-10 10:21:13 -0700612}
613
614bool BufferLayer::isProtected() const {
Alec Mouri617752f2021-04-15 16:27:01 +0000615 const sp<GraphicBuffer>& buffer(mBufferInfo.mBuffer);
616 return (buffer != 0) && (buffer->getUsage() & GRALLOC_USAGE_PROTECTED);
Marissa Wallfd668622018-05-10 10:21:13 -0700617}
618
Marissa Wallfd668622018-05-10 10:21:13 -0700619// h/w composer set-up
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700620bool BufferLayer::allTransactionsSignaled(nsecs_t expectedPresentTime) {
621 const auto headFrameNumber = getHeadFrameNumber(expectedPresentTime);
Marissa Wallfd668622018-05-10 10:21:13 -0700622 bool matchingFramesFound = false;
623 bool allTransactionsApplied = true;
624 Mutex::Autolock lock(mLocalSyncPointMutex);
625
626 for (auto& point : mLocalSyncPoints) {
627 if (point->getFrameNumber() > headFrameNumber) {
628 break;
629 }
630 matchingFramesFound = true;
631
632 if (!point->frameIsAvailable()) {
633 // We haven't notified the remote layer that the frame for
634 // this point is available yet. Notify it now, and then
635 // abort this attempt to latch.
636 point->setFrameAvailable();
637 allTransactionsApplied = false;
638 break;
639 }
640
641 allTransactionsApplied = allTransactionsApplied && point->transactionIsApplied();
642 }
643 return !matchingFramesFound || allTransactionsApplied;
David Sodman0c69cad2017-08-21 12:12:51 -0700644}
645
646// As documented in libhardware header, formats in the range
647// 0x100 - 0x1FF are specific to the HAL implementation, and
648// are known to have no alpha channel
649// TODO: move definition for device-specific range into
650// hardware.h, instead of using hard-coded values here.
651#define HARDWARE_IS_DEVICE_FORMAT(f) ((f) >= 0x100 && (f) <= 0x1FF)
652
653bool BufferLayer::getOpacityForFormat(uint32_t format) {
654 if (HARDWARE_IS_DEVICE_FORMAT(format)) {
655 return true;
656 }
657 switch (format) {
658 case HAL_PIXEL_FORMAT_RGBA_8888:
659 case HAL_PIXEL_FORMAT_BGRA_8888:
660 case HAL_PIXEL_FORMAT_RGBA_FP16:
661 case HAL_PIXEL_FORMAT_RGBA_1010102:
662 return false;
663 }
664 // in all other case, we have no blending (also for unknown formats)
665 return true;
666}
667
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700668bool BufferLayer::needsFiltering(const DisplayDevice* display) const {
669 const auto outputLayer = findOutputLayerForDisplay(display);
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800670 if (outputLayer == nullptr) {
Lloyd Piquef16688f2019-02-19 17:47:57 -0800671 return false;
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800672 }
673
Lloyd Piquef16688f2019-02-19 17:47:57 -0800674 // We need filtering if the sourceCrop rectangle size does not match the
675 // displayframe rectangle size (not a 1:1 render)
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800676 const auto& compositionState = outputLayer->getState();
677 const auto displayFrame = compositionState.displayFrame;
678 const auto sourceCrop = compositionState.sourceCrop;
Lloyd Piquef16688f2019-02-19 17:47:57 -0800679 return sourceCrop.getHeight() != displayFrame.getHeight() ||
Peiyong Linc2020ca2019-01-10 11:36:12 -0800680 sourceCrop.getWidth() != displayFrame.getWidth();
Chia-I Wu692e0832018-06-05 15:46:58 -0700681}
682
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700683bool BufferLayer::needsFilteringForScreenshots(const DisplayDevice* display,
Alec Mouri5a6d8572020-03-23 23:56:15 -0700684 const ui::Transform& inverseParentTransform) const {
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700685 const auto outputLayer = findOutputLayerForDisplay(display);
Alec Mouri5a6d8572020-03-23 23:56:15 -0700686 if (outputLayer == nullptr) {
687 return false;
688 }
689
690 // We need filtering if the sourceCrop rectangle size does not match the
691 // viewport rectangle size (not a 1:1 render)
692 const auto& compositionState = outputLayer->getState();
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700693 const ui::Transform& displayTransform = display->getTransform();
Alec Mouri5a6d8572020-03-23 23:56:15 -0700694 const ui::Transform inverseTransform = inverseParentTransform * displayTransform.inverse();
695 // Undo the transformation of the displayFrame so that we're back into
696 // layer-stack space.
697 const Rect frame = inverseTransform.transform(compositionState.displayFrame);
698 const FloatRect sourceCrop = compositionState.sourceCrop;
699
700 int32_t frameHeight = frame.getHeight();
701 int32_t frameWidth = frame.getWidth();
702 // If the display transform had a rotational component then undo the
703 // rotation so that the orientation matches the source crop.
704 if (displayTransform.getOrientation() & ui::Transform::ROT_90) {
705 std::swap(frameHeight, frameWidth);
706 }
707 return sourceCrop.getHeight() != frameHeight || sourceCrop.getWidth() != frameWidth;
708}
709
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700710uint64_t BufferLayer::getHeadFrameNumber(nsecs_t expectedPresentTime) const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800711 if (hasFrameUpdate()) {
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700712 return getFrameNumber(expectedPresentTime);
David Sodman0c69cad2017-08-21 12:12:51 -0700713 } else {
714 return mCurrentFrameNumber;
715 }
716}
717
Vishnu Nair60356342018-11-13 13:00:45 -0800718Rect BufferLayer::getBufferSize(const State& s) const {
719 // If we have a sideband stream, or we are scaling the buffer then return the layer size since
720 // we cannot determine the buffer size.
721 if ((s.sidebandStream != nullptr) ||
722 (getEffectiveScalingMode() != NATIVE_WINDOW_SCALING_MODE_FREEZE)) {
723 return Rect(getActiveWidth(s), getActiveHeight(s));
724 }
725
chaviwd62d3062019-09-04 14:48:02 -0700726 if (mBufferInfo.mBuffer == nullptr) {
Vishnu Nair60356342018-11-13 13:00:45 -0800727 return Rect::INVALID_RECT;
728 }
729
Alec Mouri617752f2021-04-15 16:27:01 +0000730 uint32_t bufWidth = mBufferInfo.mBuffer->getWidth();
731 uint32_t bufHeight = mBufferInfo.mBuffer->getHeight();
Vishnu Nair60356342018-11-13 13:00:45 -0800732
733 // Undo any transformations on the buffer and return the result.
chaviw4244e032019-09-04 11:27:49 -0700734 if (mBufferInfo.mTransform & ui::Transform::ROT_90) {
Vishnu Nair60356342018-11-13 13:00:45 -0800735 std::swap(bufWidth, bufHeight);
736 }
737
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800738 if (getTransformToDisplayInverse()) {
Dominik Laskowski718f9602019-11-09 20:01:35 -0800739 uint32_t invTransform = DisplayDevice::getPrimaryDisplayRotationFlags();
Vishnu Nair60356342018-11-13 13:00:45 -0800740 if (invTransform & ui::Transform::ROT_90) {
741 std::swap(bufWidth, bufHeight);
742 }
743 }
744
745 return Rect(bufWidth, bufHeight);
746}
747
Vishnu Nair4351ad52019-02-11 14:13:02 -0800748FloatRect BufferLayer::computeSourceBounds(const FloatRect& parentBounds) const {
749 const State& s(getDrawingState());
750
751 // If we have a sideband stream, or we are scaling the buffer then return the layer size since
752 // we cannot determine the buffer size.
753 if ((s.sidebandStream != nullptr) ||
754 (getEffectiveScalingMode() != NATIVE_WINDOW_SCALING_MODE_FREEZE)) {
755 return FloatRect(0, 0, getActiveWidth(s), getActiveHeight(s));
756 }
757
chaviwd62d3062019-09-04 14:48:02 -0700758 if (mBufferInfo.mBuffer == nullptr) {
Vishnu Nair4351ad52019-02-11 14:13:02 -0800759 return parentBounds;
760 }
761
Alec Mouri617752f2021-04-15 16:27:01 +0000762 uint32_t bufWidth = mBufferInfo.mBuffer->getWidth();
763 uint32_t bufHeight = mBufferInfo.mBuffer->getHeight();
Vishnu Nair4351ad52019-02-11 14:13:02 -0800764
765 // Undo any transformations on the buffer and return the result.
chaviw4244e032019-09-04 11:27:49 -0700766 if (mBufferInfo.mTransform & ui::Transform::ROT_90) {
Vishnu Nair4351ad52019-02-11 14:13:02 -0800767 std::swap(bufWidth, bufHeight);
768 }
769
770 if (getTransformToDisplayInverse()) {
Dominik Laskowski718f9602019-11-09 20:01:35 -0800771 uint32_t invTransform = DisplayDevice::getPrimaryDisplayRotationFlags();
Vishnu Nair4351ad52019-02-11 14:13:02 -0800772 if (invTransform & ui::Transform::ROT_90) {
773 std::swap(bufWidth, bufHeight);
774 }
775 }
776
777 return FloatRect(0, 0, bufWidth, bufHeight);
778}
779
chaviw49a108c2019-08-12 11:23:06 -0700780void BufferLayer::latchAndReleaseBuffer() {
781 mRefreshPending = false;
782 if (hasReadyFrame()) {
783 bool ignored = false;
784 latchBuffer(ignored, systemTime(), 0 /* expectedPresentTime */);
785 }
786 releasePendingBuffer(systemTime());
787}
788
chaviw4244e032019-09-04 11:27:49 -0700789PixelFormat BufferLayer::getPixelFormat() const {
790 return mBufferInfo.mPixelFormat;
791}
792
793bool BufferLayer::getTransformToDisplayInverse() const {
794 return mBufferInfo.mTransformToDisplayInverse;
795}
796
797Rect BufferLayer::getBufferCrop() const {
798 // this is the crop rectangle that applies to the buffer
799 // itself (as opposed to the window)
800 if (!mBufferInfo.mCrop.isEmpty()) {
801 // if the buffer crop is defined, we use that
802 return mBufferInfo.mCrop;
chaviwd62d3062019-09-04 14:48:02 -0700803 } else if (mBufferInfo.mBuffer != nullptr) {
chaviw4244e032019-09-04 11:27:49 -0700804 // otherwise we use the whole buffer
Alec Mouri617752f2021-04-15 16:27:01 +0000805 return mBufferInfo.mBuffer->getBounds();
chaviw4244e032019-09-04 11:27:49 -0700806 } else {
807 // if we don't have a buffer yet, we use an empty/invalid crop
808 return Rect();
809 }
810}
811
812uint32_t BufferLayer::getBufferTransform() const {
813 return mBufferInfo.mTransform;
814}
815
816ui::Dataspace BufferLayer::getDataSpace() const {
817 return mBufferInfo.mDataspace;
818}
819
820ui::Dataspace BufferLayer::translateDataspace(ui::Dataspace dataspace) {
821 ui::Dataspace updatedDataspace = dataspace;
822 // translate legacy dataspaces to modern dataspaces
823 switch (dataspace) {
824 case ui::Dataspace::SRGB:
825 updatedDataspace = ui::Dataspace::V0_SRGB;
826 break;
827 case ui::Dataspace::SRGB_LINEAR:
828 updatedDataspace = ui::Dataspace::V0_SRGB_LINEAR;
829 break;
830 case ui::Dataspace::JFIF:
831 updatedDataspace = ui::Dataspace::V0_JFIF;
832 break;
833 case ui::Dataspace::BT601_625:
834 updatedDataspace = ui::Dataspace::V0_BT601_625;
835 break;
836 case ui::Dataspace::BT601_525:
837 updatedDataspace = ui::Dataspace::V0_BT601_525;
838 break;
839 case ui::Dataspace::BT709:
840 updatedDataspace = ui::Dataspace::V0_BT709;
841 break;
842 default:
843 break;
844 }
845
846 return updatedDataspace;
847}
848
chaviwd62d3062019-09-04 14:48:02 -0700849sp<GraphicBuffer> BufferLayer::getBuffer() const {
Alec Mouri617752f2021-04-15 16:27:01 +0000850 return mBufferInfo.mBuffer;
chaviwd62d3062019-09-04 14:48:02 -0700851}
852
chaviwf83ce182019-09-12 14:43:08 -0700853void BufferLayer::getDrawingTransformMatrix(bool filteringEnabled, float outMatrix[16]) {
Alec Mouri617752f2021-04-15 16:27:01 +0000854 GLConsumer::computeTransformMatrix(outMatrix, mBufferInfo.mBuffer, mBufferInfo.mCrop,
855 mBufferInfo.mTransform, filteringEnabled);
chaviwf83ce182019-09-12 14:43:08 -0700856}
857
chaviwb4c6e582019-08-16 14:35:07 -0700858void BufferLayer::setInitialValuesForClone(const sp<Layer>& clonedFrom) {
859 Layer::setInitialValuesForClone(clonedFrom);
860
861 sp<BufferLayer> bufferClonedFrom = static_cast<BufferLayer*>(clonedFrom.get());
862 mPremultipliedAlpha = bufferClonedFrom->mPremultipliedAlpha;
863 mPotentialCursor = bufferClonedFrom->mPotentialCursor;
864 mProtectedByApp = bufferClonedFrom->mProtectedByApp;
chaviw74b03172019-08-19 11:09:03 -0700865
866 updateCloneBufferInfo();
867}
868
869void BufferLayer::updateCloneBufferInfo() {
870 if (!isClone() || !isClonedFromAlive()) {
871 return;
872 }
873
874 sp<BufferLayer> clonedFrom = static_cast<BufferLayer*>(getClonedFrom().get());
875 mBufferInfo = clonedFrom->mBufferInfo;
876 mSidebandStream = clonedFrom->mSidebandStream;
877 surfaceDamageRegion = clonedFrom->surfaceDamageRegion;
878 mCurrentFrameNumber = clonedFrom->mCurrentFrameNumber.load();
879 mPreviousFrameNumber = clonedFrom->mPreviousFrameNumber;
880
881 // After buffer info is updated, the drawingState from the real layer needs to be copied into
882 // the cloned. This is because some properties of drawingState can change when latchBuffer is
chaviwaf87b3e2019-10-01 16:59:28 -0700883 // called. However, copying the drawingState would also overwrite the cloned layer's relatives
884 // and touchableRegionCrop. Therefore, temporarily store the relatives so they can be set in
885 // the cloned drawingState again.
chaviw74b03172019-08-19 11:09:03 -0700886 wp<Layer> tmpZOrderRelativeOf = mDrawingState.zOrderRelativeOf;
887 SortedVector<wp<Layer>> tmpZOrderRelatives = mDrawingState.zOrderRelatives;
chaviwaf87b3e2019-10-01 16:59:28 -0700888 wp<Layer> tmpTouchableRegionCrop = mDrawingState.touchableRegionCrop;
889 InputWindowInfo tmpInputInfo = mDrawingState.inputInfo;
890
chaviw74b03172019-08-19 11:09:03 -0700891 mDrawingState = clonedFrom->mDrawingState;
chaviwaf87b3e2019-10-01 16:59:28 -0700892
893 mDrawingState.touchableRegionCrop = tmpTouchableRegionCrop;
chaviw74b03172019-08-19 11:09:03 -0700894 mDrawingState.zOrderRelativeOf = tmpZOrderRelativeOf;
895 mDrawingState.zOrderRelatives = tmpZOrderRelatives;
chaviwaf87b3e2019-10-01 16:59:28 -0700896 mDrawingState.inputInfo = tmpInputInfo;
chaviwb4c6e582019-08-16 14:35:07 -0700897}
898
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700899void BufferLayer::setTransformHint(ui::Transform::RotationFlags displayTransformHint) {
Vishnu Nair6213bd92020-05-08 17:42:25 -0700900 mTransformHint = getFixedTransformHint();
901 if (mTransformHint == ui::Transform::ROT_INVALID) {
902 mTransformHint = displayTransformHint;
903 }
904}
905
Vishnu Naire7f79c52020-10-29 14:45:03 -0700906bool BufferLayer::bufferNeedsFiltering() const {
907 return isFixedSize();
908}
909
David Sodman0c69cad2017-08-21 12:12:51 -0700910} // namespace android
911
912#if defined(__gl_h_)
913#error "don't include gl/gl.h in this file"
914#endif
915
916#if defined(__gl2_h_)
917#error "don't include gl2/gl2.h in this file"
918#endif
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800919
920// TODO(b/129481165): remove the #pragma below and fix conversion issues
921#pragma clang diagnostic pop // ignored "-Wconversion"