blob: cacad52410d9ac8ee3df8e901de5b251419dcb6d [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
John Reckac09e452021-04-07 16:35:37 -040061static constexpr float defaultMaxLuminance = 1000.0;
Peiyong Lin1a70eca2019-11-15 09:33:33 -080062
Lloyd Pique42ab75e2018-09-12 20:46:03 -070063BufferLayer::BufferLayer(const LayerCreationArgs& args)
Lloyd Piquefeb73d72018-12-04 17:23:44 -080064 : Layer(args),
chaviwb4c6e582019-08-16 14:35:07 -070065 mTextureName(args.textureName),
Lloyd Piquede196652020-01-22 17:29:58 -080066 mCompositionState{mFlinger->getCompositionEngine().createLayerFECompositionState()} {
Dominik Laskowski87a07e42019-10-10 20:38:02 -070067 ALOGV("Creating Layer %s", getDebugName());
David Sodman0c69cad2017-08-21 12:12:51 -070068
Lloyd Pique42ab75e2018-09-12 20:46:03 -070069 mPremultipliedAlpha = !(args.flags & ISurfaceComposerClient::eNonPremultiplied);
David Sodman0c69cad2017-08-21 12:12:51 -070070
Lloyd Pique42ab75e2018-09-12 20:46:03 -070071 mPotentialCursor = args.flags & ISurfaceComposerClient::eCursorWindow;
72 mProtectedByApp = args.flags & ISurfaceComposerClient::eProtectedByApp;
David Sodman0c69cad2017-08-21 12:12:51 -070073}
74
75BufferLayer::~BufferLayer() {
chaviwb4c6e582019-08-16 14:35:07 -070076 if (!isClone()) {
77 // The original layer and the clone layer share the same texture. Therefore, only one of
78 // the layers, in this case the original layer, needs to handle the deletion. The original
79 // layer and the clone should be removed at the same time so there shouldn't be any issue
80 // with the clone layer trying to use the deleted texture.
81 mFlinger->deleteTextureAsync(mTextureName);
82 }
Yiwei Zhang1a88c402019-11-18 10:43:58 -080083 const int32_t layerId = getSequence();
84 mFlinger->mTimeStats->onDestroy(layerId);
85 mFlinger->mFrameTracer->onDestroy(layerId);
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 {
chaviw4244e032019-09-04 11:27:49 -070092 surfaceDamageRegion = mBufferInfo.mSurfaceDamage;
David Sodmaneb085e02017-10-05 18:49:04 -070093 }
94}
95
96void BufferLayer::useEmptyDamage() {
97 surfaceDamageRegion.clear();
98}
99
Marissa Wallfd668622018-05-10 10:21:13 -0700100bool BufferLayer::isOpaque(const Layer::State& s) const {
101 // if we don't have a buffer or sidebandStream yet, we're translucent regardless of the
102 // layer's opaque flag.
chaviwd62d3062019-09-04 14:48:02 -0700103 if ((mSidebandStream == nullptr) && (mBufferInfo.mBuffer == nullptr)) {
Marissa Wallfd668622018-05-10 10:21:13 -0700104 return false;
105 }
106
107 // if the layer has the opaque flag, then we're always opaque,
108 // otherwise we use the current buffer's format.
109 return ((s.flags & layer_state_t::eLayerOpaque) != 0) || getOpacityForFormat(getPixelFormat());
David Sodman0c69cad2017-08-21 12:12:51 -0700110}
111
112bool BufferLayer::isVisible() const {
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -0700113 return !isHiddenByPolicy() && getAlpha() > 0.0f &&
chaviwd62d3062019-09-04 14:48:02 -0700114 (mBufferInfo.mBuffer != nullptr || mSidebandStream != nullptr);
David Sodman0c69cad2017-08-21 12:12:51 -0700115}
116
117bool BufferLayer::isFixedSize() const {
118 return getEffectiveScalingMode() != NATIVE_WINDOW_SCALING_MODE_FREEZE;
119}
120
Lloyd Piquea83776c2019-01-29 18:42:32 -0800121bool BufferLayer::usesSourceCrop() const {
122 return true;
123}
124
David Sodman0c69cad2017-08-21 12:12:51 -0700125static constexpr mat4 inverseOrientation(uint32_t transform) {
David Sodman41fdfc92017-11-06 16:09:56 -0800126 const mat4 flipH(-1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1);
127 const mat4 flipV(1, 0, 0, 0, 0, -1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1);
128 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 -0700129 mat4 tr;
130
131 if (transform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
132 tr = tr * rot90;
133 }
134 if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_H) {
135 tr = tr * flipH;
136 }
137 if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_V) {
138 tr = tr * flipV;
139 }
140 return inverse(tr);
141}
142
Vishnu Nair9b079a22020-01-21 14:36:08 -0800143std::optional<compositionengine::LayerFE::LayerSettings> BufferLayer::prepareClientComposition(
Lloyd Piquef16688f2019-02-19 17:47:57 -0800144 compositionengine::LayerFE::ClientCompositionTargetSettings& targetSettings) {
David Sodman0c69cad2017-08-21 12:12:51 -0700145 ATRACE_CALL();
Lloyd Piquef16688f2019-02-19 17:47:57 -0800146
Vishnu Nair9b079a22020-01-21 14:36:08 -0800147 std::optional<compositionengine::LayerFE::LayerSettings> result =
148 Layer::prepareClientComposition(targetSettings);
Lloyd Piquef16688f2019-02-19 17:47:57 -0800149 if (!result) {
150 return result;
151 }
152
chaviwd62d3062019-09-04 14:48:02 -0700153 if (CC_UNLIKELY(mBufferInfo.mBuffer == 0)) {
David Sodman0c69cad2017-08-21 12:12:51 -0700154 // the texture has not been created yet, this Layer has
155 // in fact never been drawn into. This happens frequently with
156 // SurfaceView because the WindowManager can't know when the client
157 // has drawn the first time.
158
159 // If there is nothing under us, we paint the screen in black, otherwise
160 // we just skip this update.
161
162 // figure out if there is something below us
163 Region under;
164 bool finished = false;
165 mFlinger->mDrawingState.traverseInZOrder([&](Layer* layer) {
166 if (finished || layer == static_cast<BufferLayer const*>(this)) {
167 finished = true;
168 return;
169 }
Lloyd Piquea2468662019-03-07 21:31:06 -0800170
171 under.orSelf(layer->getScreenBounds());
David Sodman0c69cad2017-08-21 12:12:51 -0700172 });
173 // if not everything below us is covered, we plug the holes!
Lloyd Piquef16688f2019-02-19 17:47:57 -0800174 Region holes(targetSettings.clip.subtract(under));
David Sodman0c69cad2017-08-21 12:12:51 -0700175 if (!holes.isEmpty()) {
Lloyd Piquef16688f2019-02-19 17:47:57 -0800176 targetSettings.clearRegion.orSelf(holes);
David Sodman0c69cad2017-08-21 12:12:51 -0700177 }
Tianhua Sundcff00c2020-12-29 07:20:55 -0500178
179 if (mSidebandStream != nullptr) {
180 // For surfaceview of tv sideband, there is no activeBuffer
181 // in bufferqueue, we need return LayerSettings.
182 return result;
183 } else {
184 return std::nullopt;
185 }
David Sodman0c69cad2017-08-21 12:12:51 -0700186 }
Ady Abraham7a60afb2021-03-29 13:20:55 -0700187 const bool blackOutLayer = (isProtected() && !targetSettings.supportsProtectedContent) ||
yuhui.zhanga102ff92021-04-09 14:45:51 +0800188 ((isSecure() || isProtected()) && !targetSettings.isSecure);
Ady Abraham7a60afb2021-03-29 13:20:55 -0700189 const bool bufferCanBeUsedAsHwTexture =
Alec Mouria90a5702021-04-16 16:36:21 +0000190 mBufferInfo.mBuffer->getBuffer()->getUsage() & GraphicBuffer::USAGE_HW_TEXTURE;
Lloyd Piquede196652020-01-22 17:29:58 -0800191 compositionengine::LayerFE::LayerSettings& layer = *result;
Ady Abraham7a60afb2021-03-29 13:20:55 -0700192 if (blackOutLayer || !bufferCanBeUsedAsHwTexture) {
193 ALOGE_IF(!bufferCanBeUsedAsHwTexture, "%s is blacked out as buffer is not gpu readable",
194 mName.c_str());
Vishnu Nairb87d94f2020-02-13 09:17:36 -0800195 prepareClearClientComposition(layer, true /* blackout */);
196 return layer;
David Sodman0c69cad2017-08-21 12:12:51 -0700197 }
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000198
Vishnu Nairb87d94f2020-02-13 09:17:36 -0800199 const State& s(getDrawingState());
200 layer.source.buffer.buffer = mBufferInfo.mBuffer;
201 layer.source.buffer.isOpaque = isOpaque(s);
202 layer.source.buffer.fence = mBufferInfo.mFence;
203 layer.source.buffer.textureName = mTextureName;
204 layer.source.buffer.usePremultipliedAlpha = getPremultipledAlpha();
205 layer.source.buffer.isY410BT2020 = isHdrY410();
206 bool hasSmpte2086 = mBufferInfo.mHdrMetadata.validTypes & HdrMetadata::SMPTE2086;
207 bool hasCta861_3 = mBufferInfo.mHdrMetadata.validTypes & HdrMetadata::CTA861_3;
John Reckac09e452021-04-07 16:35:37 -0400208 float maxLuminance = 0.f;
209 if (hasSmpte2086 && hasCta861_3) {
210 maxLuminance = std::min(mBufferInfo.mHdrMetadata.smpte2086.maxLuminance,
211 mBufferInfo.mHdrMetadata.cta8613.maxContentLightLevel);
212 } else if (hasSmpte2086) {
213 maxLuminance = mBufferInfo.mHdrMetadata.smpte2086.maxLuminance;
214 } else if (hasCta861_3) {
215 maxLuminance = mBufferInfo.mHdrMetadata.cta8613.maxContentLightLevel;
216 } else {
217 switch (layer.sourceDataspace & HAL_DATASPACE_TRANSFER_MASK) {
218 case HAL_DATASPACE_TRANSFER_ST2084:
219 case HAL_DATASPACE_TRANSFER_HLG:
220 // Behavior-match previous releases for HDR content
221 maxLuminance = defaultMaxLuminance;
222 break;
223 }
224 }
225 layer.source.buffer.maxLuminanceNits = maxLuminance;
Vishnu Nairb87d94f2020-02-13 09:17:36 -0800226 layer.frameNumber = mCurrentFrameNumber;
Alec Mouria90a5702021-04-16 16:36:21 +0000227 layer.bufferId = mBufferInfo.mBuffer ? mBufferInfo.mBuffer->getBuffer()->getId() : 0;
Vishnu Nairb87d94f2020-02-13 09:17:36 -0800228
Vishnu Naire7f79c52020-10-29 14:45:03 -0700229 const bool useFiltering =
230 targetSettings.needsFiltering || mNeedsFiltering || bufferNeedsFiltering();
Vishnu Nairb87d94f2020-02-13 09:17:36 -0800231
232 // Query the texture matrix given our current filtering mode.
233 float textureMatrix[16];
234 getDrawingTransformMatrix(useFiltering, textureMatrix);
235
236 if (getTransformToDisplayInverse()) {
237 /*
238 * the code below applies the primary display's inverse transform to
239 * the texture transform
240 */
241 uint32_t transform = DisplayDevice::getPrimaryDisplayRotationFlags();
242 mat4 tr = inverseOrientation(transform);
243
244 /**
245 * TODO(b/36727915): This is basically a hack.
246 *
247 * Ensure that regardless of the parent transformation,
248 * this buffer is always transformed from native display
249 * orientation to display orientation. For example, in the case
250 * of a camera where the buffer remains in native orientation,
251 * we want the pixels to always be upright.
252 */
253 sp<Layer> p = mDrawingParent.promote();
254 if (p != nullptr) {
255 const auto parentTransform = p->getTransform();
256 tr = tr * inverseOrientation(parentTransform.getOrientation());
257 }
258
259 // and finally apply it to the original texture matrix
260 const mat4 texTransform(mat4(static_cast<const float*>(textureMatrix)) * tr);
261 memcpy(textureMatrix, texTransform.asArray(), sizeof(textureMatrix));
262 }
263
264 const Rect win{getBounds()};
265 float bufferWidth = getBufferSize(s).getWidth();
266 float bufferHeight = getBufferSize(s).getHeight();
267
268 // BufferStateLayers can have a "buffer size" of [0, 0, -1, -1] when no display frame has
269 // been set and there is no parent layer bounds. In that case, the scale is meaningless so
270 // ignore them.
271 if (!getBufferSize(s).isValid()) {
272 bufferWidth = float(win.right) - float(win.left);
273 bufferHeight = float(win.bottom) - float(win.top);
274 }
275
276 const float scaleHeight = (float(win.bottom) - float(win.top)) / bufferHeight;
277 const float scaleWidth = (float(win.right) - float(win.left)) / bufferWidth;
278 const float translateY = float(win.top) / bufferHeight;
279 const float translateX = float(win.left) / bufferWidth;
280
281 // Flip y-coordinates because GLConsumer expects OpenGL convention.
282 mat4 tr = mat4::translate(vec4(.5, .5, 0, 1)) * mat4::scale(vec4(1, -1, 1, 1)) *
283 mat4::translate(vec4(-.5, -.5, 0, 1)) *
284 mat4::translate(vec4(translateX, translateY, 0, 1)) *
285 mat4::scale(vec4(scaleWidth, scaleHeight, 1.0, 1.0));
286
287 layer.source.buffer.useTextureFiltering = useFiltering;
288 layer.source.buffer.textureTransform = mat4(static_cast<const float*>(textureMatrix)) * tr;
289
Vishnu Nair9b079a22020-01-21 14:36:08 -0800290 return layer;
David Sodman0c69cad2017-08-21 12:12:51 -0700291}
292
Marissa Wallfd668622018-05-10 10:21:13 -0700293bool BufferLayer::isHdrY410() const {
294 // pixel format is HDR Y410 masquerading as RGBA_1010102
chaviw4244e032019-09-04 11:27:49 -0700295 return (mBufferInfo.mDataspace == ui::Dataspace::BT2020_ITU_PQ &&
296 mBufferInfo.mApi == NATIVE_WINDOW_API_MEDIA &&
chaviwdebadb82020-03-26 14:57:24 -0700297 mBufferInfo.mPixelFormat == HAL_PIXEL_FORMAT_RGBA_1010102);
David Sodmaneb085e02017-10-05 18:49:04 -0700298}
299
Lloyd Piquede196652020-01-22 17:29:58 -0800300sp<compositionengine::LayerFE> BufferLayer::getCompositionEngineLayerFE() const {
301 return asLayerFE();
302}
303
304compositionengine::LayerFECompositionState* BufferLayer::editCompositionState() {
305 return mCompositionState.get();
306}
307
308const compositionengine::LayerFECompositionState* BufferLayer::getCompositionState() const {
309 return mCompositionState.get();
310}
311
312void BufferLayer::preparePerFrameCompositionState() {
313 Layer::preparePerFrameCompositionState();
David Sodman0c69cad2017-08-21 12:12:51 -0700314
315 // Sideband layers
Lloyd Piquede196652020-01-22 17:29:58 -0800316 auto* compositionState = editCompositionState();
317 if (compositionState->sidebandStream.get()) {
318 compositionState->compositionType = Hwc2::IComposerClient::Composition::SIDEBAND;
Robert Carra6bb2bc2020-04-08 11:03:23 -0700319 return;
David Sodman15094112018-10-11 09:39:37 -0700320 } else {
Lloyd Piquef5275482019-01-29 18:42:42 -0800321 // Normal buffer layers
Lloyd Piquede196652020-01-22 17:29:58 -0800322 compositionState->hdrMetadata = mBufferInfo.mHdrMetadata;
323 compositionState->compositionType = mPotentialCursor
Lloyd Piquef5275482019-01-29 18:42:42 -0800324 ? Hwc2::IComposerClient::Composition::CURSOR
325 : Hwc2::IComposerClient::Composition::DEVICE;
David Sodman0c69cad2017-08-21 12:12:51 -0700326 }
Robert Carra6bb2bc2020-04-08 11:03:23 -0700327
Alec Mouria90a5702021-04-16 16:36:21 +0000328 compositionState->buffer = mBufferInfo.mBuffer->getBuffer();
Robert Carra6bb2bc2020-04-08 11:03:23 -0700329 compositionState->bufferSlot = (mBufferInfo.mBufferSlot == BufferQueue::INVALID_BUFFER_SLOT)
330 ? 0
331 : mBufferInfo.mBufferSlot;
332 compositionState->acquireFence = mBufferInfo.mFence;
David Sodman0c69cad2017-08-21 12:12:51 -0700333}
334
Marissa Wallfd668622018-05-10 10:21:13 -0700335bool BufferLayer::onPreComposition(nsecs_t refreshStartTime) {
chaviwd62d3062019-09-04 14:48:02 -0700336 if (mBufferInfo.mBuffer != nullptr) {
Marissa Wallfd668622018-05-10 10:21:13 -0700337 Mutex::Autolock lock(mFrameEventHistoryMutex);
338 mFrameEventHistory.addPreComposition(mCurrentFrameNumber, refreshStartTime);
David Sodman0c69cad2017-08-21 12:12:51 -0700339 }
Marissa Wallfd668622018-05-10 10:21:13 -0700340 mRefreshPending = false;
341 return hasReadyFrame();
David Sodman0c69cad2017-08-21 12:12:51 -0700342}
Ady Abraham8b9e6122021-01-26 19:11:45 -0800343namespace {
344TimeStats::SetFrameRateVote frameRateToSetFrameRateVotePayload(Layer::FrameRate frameRate) {
345 using FrameRateCompatibility = TimeStats::SetFrameRateVote::FrameRateCompatibility;
346 using Seamlessness = TimeStats::SetFrameRateVote::Seamlessness;
347 const auto frameRateCompatibility = [frameRate] {
348 switch (frameRate.type) {
349 case Layer::FrameRateCompatibility::Default:
350 return FrameRateCompatibility::Default;
351 case Layer::FrameRateCompatibility::ExactOrMultiple:
352 return FrameRateCompatibility::ExactOrMultiple;
353 default:
354 return FrameRateCompatibility::Undefined;
355 }
356 }();
357
358 const auto seamlessness = [frameRate] {
359 switch (frameRate.seamlessness) {
360 case scheduler::Seamlessness::OnlySeamless:
361 return Seamlessness::ShouldBeSeamless;
362 case scheduler::Seamlessness::SeamedAndSeamless:
363 return Seamlessness::NotRequired;
364 default:
365 return Seamlessness::Undefined;
366 }
367 }();
368
369 return TimeStats::SetFrameRateVote{.frameRate = frameRate.rate.getValue(),
370 .frameRateCompatibility = frameRateCompatibility,
371 .seamlessness = seamlessness};
372}
373} // namespace
David Sodman0c69cad2017-08-21 12:12:51 -0700374
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700375bool BufferLayer::onPostComposition(const DisplayDevice* display,
Dominik Laskowski075d3172018-05-24 15:50:06 -0700376 const std::shared_ptr<FenceTime>& glDoneFence,
Marissa Wallfd668622018-05-10 10:21:13 -0700377 const std::shared_ptr<FenceTime>& presentFence,
378 const CompositorTiming& compositorTiming) {
379 // mFrameLatencyNeeded is true when a new frame was latched for the
380 // composition.
chaviw74b03172019-08-19 11:09:03 -0700381 if (!mBufferInfo.mFrameLatencyNeeded) return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700382
383 // Update mFrameEventHistory.
Dan Stoza436ccf32018-06-21 12:10:12 -0700384 {
Marissa Wallfd668622018-05-10 10:21:13 -0700385 Mutex::Autolock lock(mFrameEventHistoryMutex);
386 mFrameEventHistory.addPostComposition(mCurrentFrameNumber, glDoneFence, presentFence,
387 compositorTiming);
Valerie Hau93d5a5e2020-02-13 14:50:01 -0800388 finalizeFrameEventHistory(glDoneFence, compositorTiming);
David Sodman0c69cad2017-08-21 12:12:51 -0700389 }
390
Marissa Wallfd668622018-05-10 10:21:13 -0700391 // Update mFrameTracker.
chaviw4244e032019-09-04 11:27:49 -0700392 nsecs_t desiredPresentTime = mBufferInfo.mDesiredPresentTime;
Marissa Wallfd668622018-05-10 10:21:13 -0700393 mFrameTracker.setDesiredPresentTime(desiredPresentTime);
394
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800395 const int32_t layerId = getSequence();
396 mFlinger->mTimeStats->setDesiredTime(layerId, mCurrentFrameNumber, desiredPresentTime);
Marissa Wallfd668622018-05-10 10:21:13 -0700397
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700398 const auto outputLayer = findOutputLayerForDisplay(display);
Adithya Srinivasanb69e0762019-11-11 18:39:53 -0800399 if (outputLayer && outputLayer->requiresClientComposition()) {
400 nsecs_t clientCompositionTimestamp = outputLayer->getState().clientCompositionTimestamp;
401 mFlinger->mFrameTracer->traceTimestamp(layerId, getCurrentBufferId(), mCurrentFrameNumber,
402 clientCompositionTimestamp,
403 FrameTracer::FrameEvent::FALLBACK_COMPOSITION);
Adithya Srinivasanb6a2fa12021-03-13 00:23:09 +0000404 // Update the SurfaceFrames in the drawing state
405 if (mDrawingState.bufferSurfaceFrameTX) {
406 mDrawingState.bufferSurfaceFrameTX->setGpuComposition();
407 }
408 for (auto& [token, surfaceFrame] : mDrawingState.bufferlessSurfaceFramesTX) {
409 surfaceFrame->setGpuComposition();
410 }
Adithya Srinivasanb69e0762019-11-11 18:39:53 -0800411 }
412
chaviw4244e032019-09-04 11:27:49 -0700413 std::shared_ptr<FenceTime> frameReadyFence = mBufferInfo.mFenceTime;
Marissa Wallfd668622018-05-10 10:21:13 -0700414 if (frameReadyFence->isValid()) {
415 mFrameTracker.setFrameReadyFence(std::move(frameReadyFence));
416 } else {
417 // There was no fence for this frame, so assume that it was ready
418 // to be presented at the desired present time.
419 mFrameTracker.setFrameReadyTime(desiredPresentTime);
Dominik Laskowski45de9bd2018-06-11 17:44:10 -0700420 }
Marissa Wallfd668622018-05-10 10:21:13 -0700421
Alec Mouri7d436ec2021-01-27 20:40:50 -0800422 const Fps refreshRate = mFlinger->mRefreshRateConfigs->getCurrentRefreshRate().getFps();
423 const std::optional<Fps> renderRate = mFlinger->mScheduler->getFrameRateOverride(getOwnerUid());
Marissa Wallfd668622018-05-10 10:21:13 -0700424 if (presentFence->isValid()) {
Alec Mouri7d436ec2021-01-27 20:40:50 -0800425 mFlinger->mTimeStats->setPresentFence(layerId, mCurrentFrameNumber, presentFence,
Ady Abraham8b9e6122021-01-26 19:11:45 -0800426 refreshRate, renderRate,
427 frameRateToSetFrameRateVotePayload(
428 mDrawingState.frameRate));
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800429 mFlinger->mFrameTracer->traceFence(layerId, getCurrentBufferId(), mCurrentFrameNumber,
Mikael Pessa90092f42019-08-26 17:22:04 -0700430 presentFence, FrameTracer::FrameEvent::PRESENT_FENCE);
Marissa Wallfd668622018-05-10 10:21:13 -0700431 mFrameTracker.setActualPresentFence(std::shared_ptr<FenceTime>(presentFence));
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700432 } else if (!display) {
433 // Do nothing.
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200434 } else if (const auto displayId = PhysicalDisplayId::tryCast(display->getId());
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700435 displayId && mFlinger->getHwComposer().isConnected(*displayId)) {
Marissa Wallfd668622018-05-10 10:21:13 -0700436 // The HWC doesn't support present fences, so use the refresh
437 // timestamp instead.
Marin Shalamanov045b7002021-01-07 16:56:24 +0100438 const nsecs_t actualPresentTime = display->getRefreshTimestamp();
Alec Mouri7d436ec2021-01-27 20:40:50 -0800439 mFlinger->mTimeStats->setPresentTime(layerId, mCurrentFrameNumber, actualPresentTime,
Ady Abraham8b9e6122021-01-26 19:11:45 -0800440 refreshRate, renderRate,
441 frameRateToSetFrameRateVotePayload(
442 mDrawingState.frameRate));
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800443 mFlinger->mFrameTracer->traceTimestamp(layerId, getCurrentBufferId(), mCurrentFrameNumber,
Mikael Pessa90092f42019-08-26 17:22:04 -0700444 actualPresentTime,
445 FrameTracer::FrameEvent::PRESENT_FENCE);
Marissa Wallfd668622018-05-10 10:21:13 -0700446 mFrameTracker.setActualPresentTime(actualPresentTime);
447 }
448
449 mFrameTracker.advanceFrame();
chaviw74b03172019-08-19 11:09:03 -0700450 mBufferInfo.mFrameLatencyNeeded = false;
Marissa Wallfd668622018-05-10 10:21:13 -0700451 return true;
David Sodman0c69cad2017-08-21 12:12:51 -0700452}
453
chaviwdebadb82020-03-26 14:57:24 -0700454void BufferLayer::gatherBufferInfo() {
455 mBufferInfo.mPixelFormat =
Alec Mouria90a5702021-04-16 16:36:21 +0000456 !mBufferInfo.mBuffer ? PIXEL_FORMAT_NONE : mBufferInfo.mBuffer->getBuffer()->format;
chaviwdebadb82020-03-26 14:57:24 -0700457 mBufferInfo.mFrameLatencyNeeded = true;
458}
459
Ady Abraham63a3e592021-01-06 10:47:15 -0800460bool BufferLayer::shouldPresentNow(nsecs_t expectedPresentTime) const {
461 // If this is not a valid vsync for the layer's uid, return and try again later
462 const bool isVsyncValidForUid =
463 mFlinger->mScheduler->isVsyncValid(expectedPresentTime, mOwnerUid);
464 if (!isVsyncValidForUid) {
465 ATRACE_NAME("!isVsyncValidForUid");
466 return false;
467 }
468
469 // AutoRefresh layers and sideband streams should always be presented
470 if (getSidebandStreamChanged() || getAutoRefresh()) {
471 return true;
472 }
473
Ady Abraham63a3e592021-01-06 10:47:15 -0800474 // If this layer doesn't have a frame is shouldn't be presented
475 if (!hasFrameUpdate()) {
476 return false;
477 }
478
479 // Defer to the derived class to decide whether the next buffer is due for
480 // presentation.
481 return isBufferDue(expectedPresentTime);
482}
483
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700484bool BufferLayer::latchBuffer(bool& recomputeVisibleRegions, nsecs_t latchTime,
485 nsecs_t expectedPresentTime) {
Marissa Wallfd668622018-05-10 10:21:13 -0700486 ATRACE_CALL();
David Sodman0c69cad2017-08-21 12:12:51 -0700487
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800488 bool refreshRequired = latchSidebandStream(recomputeVisibleRegions);
David Sodman0c69cad2017-08-21 12:12:51 -0700489
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800490 if (refreshRequired) {
491 return refreshRequired;
David Sodman0c69cad2017-08-21 12:12:51 -0700492 }
493
Marissa Wallfd668622018-05-10 10:21:13 -0700494 if (!hasReadyFrame()) {
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800495 return false;
David Sodman0c69cad2017-08-21 12:12:51 -0700496 }
David Sodman0c69cad2017-08-21 12:12:51 -0700497
Marissa Wallfd668622018-05-10 10:21:13 -0700498 // if we've already called updateTexImage() without going through
499 // a composition step, we have to skip this layer at this point
500 // because we cannot call updateTeximage() without a corresponding
501 // compositionComplete() call.
502 // we'll trigger an update in onPreComposition().
503 if (mRefreshPending) {
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800504 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700505 }
506
507 // If the head buffer's acquire fence hasn't signaled yet, return and
508 // try again later
509 if (!fenceHasSignaled()) {
Ady Abraham09bd3922019-04-08 10:44:56 -0700510 ATRACE_NAME("!fenceHasSignaled()");
David Sodman0c69cad2017-08-21 12:12:51 -0700511 mFlinger->signalLayerUpdate();
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800512 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700513 }
514
515 // Capture the old state of the layer for comparisons later
516 const State& s(getDrawingState());
517 const bool oldOpacity = isOpaque(s);
chaviwd62d3062019-09-04 14:48:02 -0700518
519 BufferInfo oldBufferInfo = mBufferInfo;
Marissa Wallfd668622018-05-10 10:21:13 -0700520
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700521 status_t err = updateTexImage(recomputeVisibleRegions, latchTime, expectedPresentTime);
Marissa Wallfd668622018-05-10 10:21:13 -0700522 if (err != NO_ERROR) {
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800523 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700524 }
525
526 err = updateActiveBuffer();
527 if (err != NO_ERROR) {
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800528 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700529 }
530
Marissa Wallfd668622018-05-10 10:21:13 -0700531 err = updateFrameNumber(latchTime);
532 if (err != NO_ERROR) {
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800533 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700534 }
535
chaviw4244e032019-09-04 11:27:49 -0700536 gatherBufferInfo();
537
Marissa Wallfd668622018-05-10 10:21:13 -0700538 mRefreshPending = true;
chaviwd62d3062019-09-04 14:48:02 -0700539 if (oldBufferInfo.mBuffer == nullptr) {
Marissa Wallfd668622018-05-10 10:21:13 -0700540 // the first time we receive a buffer, we need to trigger a
541 // geometry invalidation.
542 recomputeVisibleRegions = true;
543 }
544
chaviw4244e032019-09-04 11:27:49 -0700545 if ((mBufferInfo.mCrop != oldBufferInfo.mCrop) ||
546 (mBufferInfo.mTransform != oldBufferInfo.mTransform) ||
547 (mBufferInfo.mScaleMode != oldBufferInfo.mScaleMode) ||
548 (mBufferInfo.mTransformToDisplayInverse != oldBufferInfo.mTransformToDisplayInverse)) {
Marissa Wallfd668622018-05-10 10:21:13 -0700549 recomputeVisibleRegions = true;
550 }
551
chaviwd62d3062019-09-04 14:48:02 -0700552 if (oldBufferInfo.mBuffer != nullptr) {
Alec Mouria90a5702021-04-16 16:36:21 +0000553 uint32_t bufWidth = mBufferInfo.mBuffer->getBuffer()->getWidth();
554 uint32_t bufHeight = mBufferInfo.mBuffer->getBuffer()->getHeight();
555 if (bufWidth != uint32_t(oldBufferInfo.mBuffer->getBuffer()->width) ||
556 bufHeight != uint32_t(oldBufferInfo.mBuffer->getBuffer()->height)) {
Marissa Wallfd668622018-05-10 10:21:13 -0700557 recomputeVisibleRegions = true;
558 }
559 }
560
561 if (oldOpacity != isOpaque(s)) {
562 recomputeVisibleRegions = true;
563 }
564
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800565 return true;
Marissa Wallfd668622018-05-10 10:21:13 -0700566}
567
Marissa Wallfd668622018-05-10 10:21:13 -0700568bool BufferLayer::hasReadyFrame() const {
Marissa Wall024a1912018-08-13 13:55:35 -0700569 return hasFrameUpdate() || getSidebandStreamChanged() || getAutoRefresh();
Marissa Wallfd668622018-05-10 10:21:13 -0700570}
571
572uint32_t BufferLayer::getEffectiveScalingMode() const {
chaviw4244e032019-09-04 11:27:49 -0700573 return mBufferInfo.mScaleMode;
Marissa Wallfd668622018-05-10 10:21:13 -0700574}
575
576bool BufferLayer::isProtected() const {
Alec Mouria90a5702021-04-16 16:36:21 +0000577 return (mBufferInfo.mBuffer != nullptr) &&
578 (mBufferInfo.mBuffer->getBuffer()->getUsage() & GRALLOC_USAGE_PROTECTED);
Marissa Wallfd668622018-05-10 10:21:13 -0700579}
580
David Sodman0c69cad2017-08-21 12:12:51 -0700581// As documented in libhardware header, formats in the range
582// 0x100 - 0x1FF are specific to the HAL implementation, and
583// are known to have no alpha channel
584// TODO: move definition for device-specific range into
585// hardware.h, instead of using hard-coded values here.
586#define HARDWARE_IS_DEVICE_FORMAT(f) ((f) >= 0x100 && (f) <= 0x1FF)
587
588bool BufferLayer::getOpacityForFormat(uint32_t format) {
589 if (HARDWARE_IS_DEVICE_FORMAT(format)) {
590 return true;
591 }
592 switch (format) {
593 case HAL_PIXEL_FORMAT_RGBA_8888:
594 case HAL_PIXEL_FORMAT_BGRA_8888:
595 case HAL_PIXEL_FORMAT_RGBA_FP16:
596 case HAL_PIXEL_FORMAT_RGBA_1010102:
597 return false;
598 }
599 // in all other case, we have no blending (also for unknown formats)
600 return true;
601}
602
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700603bool BufferLayer::needsFiltering(const DisplayDevice* display) const {
604 const auto outputLayer = findOutputLayerForDisplay(display);
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800605 if (outputLayer == nullptr) {
Lloyd Piquef16688f2019-02-19 17:47:57 -0800606 return false;
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800607 }
608
Lloyd Piquef16688f2019-02-19 17:47:57 -0800609 // We need filtering if the sourceCrop rectangle size does not match the
610 // displayframe rectangle size (not a 1:1 render)
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800611 const auto& compositionState = outputLayer->getState();
612 const auto displayFrame = compositionState.displayFrame;
613 const auto sourceCrop = compositionState.sourceCrop;
Lloyd Piquef16688f2019-02-19 17:47:57 -0800614 return sourceCrop.getHeight() != displayFrame.getHeight() ||
Peiyong Linc2020ca2019-01-10 11:36:12 -0800615 sourceCrop.getWidth() != displayFrame.getWidth();
Chia-I Wu692e0832018-06-05 15:46:58 -0700616}
617
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700618bool BufferLayer::needsFilteringForScreenshots(const DisplayDevice* display,
Alec Mouri5a6d8572020-03-23 23:56:15 -0700619 const ui::Transform& inverseParentTransform) const {
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700620 const auto outputLayer = findOutputLayerForDisplay(display);
Alec Mouri5a6d8572020-03-23 23:56:15 -0700621 if (outputLayer == nullptr) {
622 return false;
623 }
624
625 // We need filtering if the sourceCrop rectangle size does not match the
626 // viewport rectangle size (not a 1:1 render)
627 const auto& compositionState = outputLayer->getState();
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700628 const ui::Transform& displayTransform = display->getTransform();
Alec Mouri5a6d8572020-03-23 23:56:15 -0700629 const ui::Transform inverseTransform = inverseParentTransform * displayTransform.inverse();
630 // Undo the transformation of the displayFrame so that we're back into
631 // layer-stack space.
632 const Rect frame = inverseTransform.transform(compositionState.displayFrame);
633 const FloatRect sourceCrop = compositionState.sourceCrop;
634
635 int32_t frameHeight = frame.getHeight();
636 int32_t frameWidth = frame.getWidth();
637 // If the display transform had a rotational component then undo the
638 // rotation so that the orientation matches the source crop.
639 if (displayTransform.getOrientation() & ui::Transform::ROT_90) {
640 std::swap(frameHeight, frameWidth);
641 }
642 return sourceCrop.getHeight() != frameHeight || sourceCrop.getWidth() != frameWidth;
643}
644
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700645uint64_t BufferLayer::getHeadFrameNumber(nsecs_t expectedPresentTime) const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800646 if (hasFrameUpdate()) {
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700647 return getFrameNumber(expectedPresentTime);
David Sodman0c69cad2017-08-21 12:12:51 -0700648 } else {
649 return mCurrentFrameNumber;
650 }
651}
652
Vishnu Nair60356342018-11-13 13:00:45 -0800653Rect BufferLayer::getBufferSize(const State& s) const {
654 // If we have a sideband stream, or we are scaling the buffer then return the layer size since
655 // we cannot determine the buffer size.
656 if ((s.sidebandStream != nullptr) ||
657 (getEffectiveScalingMode() != NATIVE_WINDOW_SCALING_MODE_FREEZE)) {
658 return Rect(getActiveWidth(s), getActiveHeight(s));
659 }
660
chaviwd62d3062019-09-04 14:48:02 -0700661 if (mBufferInfo.mBuffer == nullptr) {
Vishnu Nair60356342018-11-13 13:00:45 -0800662 return Rect::INVALID_RECT;
663 }
664
Alec Mouria90a5702021-04-16 16:36:21 +0000665 uint32_t bufWidth = mBufferInfo.mBuffer->getBuffer()->getWidth();
666 uint32_t bufHeight = mBufferInfo.mBuffer->getBuffer()->getHeight();
Vishnu Nair60356342018-11-13 13:00:45 -0800667
668 // Undo any transformations on the buffer and return the result.
chaviw4244e032019-09-04 11:27:49 -0700669 if (mBufferInfo.mTransform & ui::Transform::ROT_90) {
Vishnu Nair60356342018-11-13 13:00:45 -0800670 std::swap(bufWidth, bufHeight);
671 }
672
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800673 if (getTransformToDisplayInverse()) {
Dominik Laskowski718f9602019-11-09 20:01:35 -0800674 uint32_t invTransform = DisplayDevice::getPrimaryDisplayRotationFlags();
Vishnu Nair60356342018-11-13 13:00:45 -0800675 if (invTransform & ui::Transform::ROT_90) {
676 std::swap(bufWidth, bufHeight);
677 }
678 }
679
680 return Rect(bufWidth, bufHeight);
681}
682
Vishnu Nair4351ad52019-02-11 14:13:02 -0800683FloatRect BufferLayer::computeSourceBounds(const FloatRect& parentBounds) const {
684 const State& s(getDrawingState());
685
686 // If we have a sideband stream, or we are scaling the buffer then return the layer size since
687 // we cannot determine the buffer size.
688 if ((s.sidebandStream != nullptr) ||
689 (getEffectiveScalingMode() != NATIVE_WINDOW_SCALING_MODE_FREEZE)) {
690 return FloatRect(0, 0, getActiveWidth(s), getActiveHeight(s));
691 }
692
chaviwd62d3062019-09-04 14:48:02 -0700693 if (mBufferInfo.mBuffer == nullptr) {
Vishnu Nair4351ad52019-02-11 14:13:02 -0800694 return parentBounds;
695 }
696
Alec Mouria90a5702021-04-16 16:36:21 +0000697 uint32_t bufWidth = mBufferInfo.mBuffer->getBuffer()->getWidth();
698 uint32_t bufHeight = mBufferInfo.mBuffer->getBuffer()->getHeight();
Vishnu Nair4351ad52019-02-11 14:13:02 -0800699
700 // Undo any transformations on the buffer and return the result.
chaviw4244e032019-09-04 11:27:49 -0700701 if (mBufferInfo.mTransform & ui::Transform::ROT_90) {
Vishnu Nair4351ad52019-02-11 14:13:02 -0800702 std::swap(bufWidth, bufHeight);
703 }
704
705 if (getTransformToDisplayInverse()) {
Dominik Laskowski718f9602019-11-09 20:01:35 -0800706 uint32_t invTransform = DisplayDevice::getPrimaryDisplayRotationFlags();
Vishnu Nair4351ad52019-02-11 14:13:02 -0800707 if (invTransform & ui::Transform::ROT_90) {
708 std::swap(bufWidth, bufHeight);
709 }
710 }
711
712 return FloatRect(0, 0, bufWidth, bufHeight);
713}
714
chaviw49a108c2019-08-12 11:23:06 -0700715void BufferLayer::latchAndReleaseBuffer() {
716 mRefreshPending = false;
717 if (hasReadyFrame()) {
718 bool ignored = false;
719 latchBuffer(ignored, systemTime(), 0 /* expectedPresentTime */);
720 }
721 releasePendingBuffer(systemTime());
722}
723
chaviw4244e032019-09-04 11:27:49 -0700724PixelFormat BufferLayer::getPixelFormat() const {
725 return mBufferInfo.mPixelFormat;
726}
727
728bool BufferLayer::getTransformToDisplayInverse() const {
729 return mBufferInfo.mTransformToDisplayInverse;
730}
731
732Rect BufferLayer::getBufferCrop() const {
733 // this is the crop rectangle that applies to the buffer
734 // itself (as opposed to the window)
735 if (!mBufferInfo.mCrop.isEmpty()) {
736 // if the buffer crop is defined, we use that
737 return mBufferInfo.mCrop;
chaviwd62d3062019-09-04 14:48:02 -0700738 } else if (mBufferInfo.mBuffer != nullptr) {
chaviw4244e032019-09-04 11:27:49 -0700739 // otherwise we use the whole buffer
Alec Mouria90a5702021-04-16 16:36:21 +0000740 return mBufferInfo.mBuffer->getBuffer()->getBounds();
chaviw4244e032019-09-04 11:27:49 -0700741 } else {
742 // if we don't have a buffer yet, we use an empty/invalid crop
743 return Rect();
744 }
745}
746
747uint32_t BufferLayer::getBufferTransform() const {
748 return mBufferInfo.mTransform;
749}
750
751ui::Dataspace BufferLayer::getDataSpace() const {
752 return mBufferInfo.mDataspace;
753}
754
755ui::Dataspace BufferLayer::translateDataspace(ui::Dataspace dataspace) {
756 ui::Dataspace updatedDataspace = dataspace;
757 // translate legacy dataspaces to modern dataspaces
758 switch (dataspace) {
759 case ui::Dataspace::SRGB:
760 updatedDataspace = ui::Dataspace::V0_SRGB;
761 break;
762 case ui::Dataspace::SRGB_LINEAR:
763 updatedDataspace = ui::Dataspace::V0_SRGB_LINEAR;
764 break;
765 case ui::Dataspace::JFIF:
766 updatedDataspace = ui::Dataspace::V0_JFIF;
767 break;
768 case ui::Dataspace::BT601_625:
769 updatedDataspace = ui::Dataspace::V0_BT601_625;
770 break;
771 case ui::Dataspace::BT601_525:
772 updatedDataspace = ui::Dataspace::V0_BT601_525;
773 break;
774 case ui::Dataspace::BT709:
775 updatedDataspace = ui::Dataspace::V0_BT709;
776 break;
777 default:
778 break;
779 }
780
781 return updatedDataspace;
782}
783
chaviwd62d3062019-09-04 14:48:02 -0700784sp<GraphicBuffer> BufferLayer::getBuffer() const {
Alec Mouria90a5702021-04-16 16:36:21 +0000785 return mBufferInfo.mBuffer ? mBufferInfo.mBuffer->getBuffer() : nullptr;
chaviwd62d3062019-09-04 14:48:02 -0700786}
787
chaviwf83ce182019-09-12 14:43:08 -0700788void BufferLayer::getDrawingTransformMatrix(bool filteringEnabled, float outMatrix[16]) {
Alec Mouria90a5702021-04-16 16:36:21 +0000789 GLConsumer::computeTransformMatrix(outMatrix,
790 mBufferInfo.mBuffer ? mBufferInfo.mBuffer->getBuffer()
791 : nullptr,
792 mBufferInfo.mCrop, mBufferInfo.mTransform, filteringEnabled);
chaviwf83ce182019-09-12 14:43:08 -0700793}
794
chaviwb4c6e582019-08-16 14:35:07 -0700795void BufferLayer::setInitialValuesForClone(const sp<Layer>& clonedFrom) {
796 Layer::setInitialValuesForClone(clonedFrom);
797
798 sp<BufferLayer> bufferClonedFrom = static_cast<BufferLayer*>(clonedFrom.get());
799 mPremultipliedAlpha = bufferClonedFrom->mPremultipliedAlpha;
800 mPotentialCursor = bufferClonedFrom->mPotentialCursor;
801 mProtectedByApp = bufferClonedFrom->mProtectedByApp;
chaviw74b03172019-08-19 11:09:03 -0700802
803 updateCloneBufferInfo();
804}
805
806void BufferLayer::updateCloneBufferInfo() {
807 if (!isClone() || !isClonedFromAlive()) {
808 return;
809 }
810
811 sp<BufferLayer> clonedFrom = static_cast<BufferLayer*>(getClonedFrom().get());
812 mBufferInfo = clonedFrom->mBufferInfo;
813 mSidebandStream = clonedFrom->mSidebandStream;
814 surfaceDamageRegion = clonedFrom->surfaceDamageRegion;
815 mCurrentFrameNumber = clonedFrom->mCurrentFrameNumber.load();
816 mPreviousFrameNumber = clonedFrom->mPreviousFrameNumber;
817
818 // After buffer info is updated, the drawingState from the real layer needs to be copied into
819 // the cloned. This is because some properties of drawingState can change when latchBuffer is
chaviwaf87b3e2019-10-01 16:59:28 -0700820 // called. However, copying the drawingState would also overwrite the cloned layer's relatives
821 // and touchableRegionCrop. Therefore, temporarily store the relatives so they can be set in
822 // the cloned drawingState again.
chaviw74b03172019-08-19 11:09:03 -0700823 wp<Layer> tmpZOrderRelativeOf = mDrawingState.zOrderRelativeOf;
824 SortedVector<wp<Layer>> tmpZOrderRelatives = mDrawingState.zOrderRelatives;
chaviwaf87b3e2019-10-01 16:59:28 -0700825 wp<Layer> tmpTouchableRegionCrop = mDrawingState.touchableRegionCrop;
826 InputWindowInfo tmpInputInfo = mDrawingState.inputInfo;
827
chaviw74b03172019-08-19 11:09:03 -0700828 mDrawingState = clonedFrom->mDrawingState;
chaviwaf87b3e2019-10-01 16:59:28 -0700829
830 mDrawingState.touchableRegionCrop = tmpTouchableRegionCrop;
chaviw74b03172019-08-19 11:09:03 -0700831 mDrawingState.zOrderRelativeOf = tmpZOrderRelativeOf;
832 mDrawingState.zOrderRelatives = tmpZOrderRelatives;
chaviwaf87b3e2019-10-01 16:59:28 -0700833 mDrawingState.inputInfo = tmpInputInfo;
chaviwb4c6e582019-08-16 14:35:07 -0700834}
835
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700836void BufferLayer::setTransformHint(ui::Transform::RotationFlags displayTransformHint) {
Vishnu Nair6213bd92020-05-08 17:42:25 -0700837 mTransformHint = getFixedTransformHint();
838 if (mTransformHint == ui::Transform::ROT_INVALID) {
839 mTransformHint = displayTransformHint;
840 }
841}
842
Vishnu Naire7f79c52020-10-29 14:45:03 -0700843bool BufferLayer::bufferNeedsFiltering() const {
844 return isFixedSize();
845}
846
David Sodman0c69cad2017-08-21 12:12:51 -0700847} // namespace android
848
849#if defined(__gl_h_)
850#error "don't include gl/gl.h in this file"
851#endif
852
853#if defined(__gl2_h_)
854#error "don't include gl2/gl2.h in this file"
855#endif
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800856
857// TODO(b/129481165): remove the #pragma below and fix conversion issues
858#pragma clang diagnostic pop // ignored "-Wconversion"