blob: 23779be73ac261a899d7f686883ae61e0e076968 [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(
Adithya Srinivasan58069dc2021-06-04 20:37:02 +0000428 mDrawingState.frameRate),
429 getGameMode());
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800430 mFlinger->mFrameTracer->traceFence(layerId, getCurrentBufferId(), mCurrentFrameNumber,
Mikael Pessa90092f42019-08-26 17:22:04 -0700431 presentFence, FrameTracer::FrameEvent::PRESENT_FENCE);
Marissa Wallfd668622018-05-10 10:21:13 -0700432 mFrameTracker.setActualPresentFence(std::shared_ptr<FenceTime>(presentFence));
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700433 } else if (!display) {
434 // Do nothing.
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200435 } else if (const auto displayId = PhysicalDisplayId::tryCast(display->getId());
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700436 displayId && mFlinger->getHwComposer().isConnected(*displayId)) {
Marissa Wallfd668622018-05-10 10:21:13 -0700437 // The HWC doesn't support present fences, so use the refresh
438 // timestamp instead.
Marin Shalamanov045b7002021-01-07 16:56:24 +0100439 const nsecs_t actualPresentTime = display->getRefreshTimestamp();
Alec Mouri7d436ec2021-01-27 20:40:50 -0800440 mFlinger->mTimeStats->setPresentTime(layerId, mCurrentFrameNumber, actualPresentTime,
Ady Abraham8b9e6122021-01-26 19:11:45 -0800441 refreshRate, renderRate,
442 frameRateToSetFrameRateVotePayload(
Adithya Srinivasan58069dc2021-06-04 20:37:02 +0000443 mDrawingState.frameRate),
444 getGameMode());
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800445 mFlinger->mFrameTracer->traceTimestamp(layerId, getCurrentBufferId(), mCurrentFrameNumber,
Mikael Pessa90092f42019-08-26 17:22:04 -0700446 actualPresentTime,
447 FrameTracer::FrameEvent::PRESENT_FENCE);
Marissa Wallfd668622018-05-10 10:21:13 -0700448 mFrameTracker.setActualPresentTime(actualPresentTime);
449 }
450
451 mFrameTracker.advanceFrame();
chaviw74b03172019-08-19 11:09:03 -0700452 mBufferInfo.mFrameLatencyNeeded = false;
Marissa Wallfd668622018-05-10 10:21:13 -0700453 return true;
David Sodman0c69cad2017-08-21 12:12:51 -0700454}
455
chaviwdebadb82020-03-26 14:57:24 -0700456void BufferLayer::gatherBufferInfo() {
457 mBufferInfo.mPixelFormat =
Alec Mouria90a5702021-04-16 16:36:21 +0000458 !mBufferInfo.mBuffer ? PIXEL_FORMAT_NONE : mBufferInfo.mBuffer->getBuffer()->format;
chaviwdebadb82020-03-26 14:57:24 -0700459 mBufferInfo.mFrameLatencyNeeded = true;
460}
461
Ady Abraham63a3e592021-01-06 10:47:15 -0800462bool BufferLayer::shouldPresentNow(nsecs_t expectedPresentTime) const {
463 // If this is not a valid vsync for the layer's uid, return and try again later
464 const bool isVsyncValidForUid =
465 mFlinger->mScheduler->isVsyncValid(expectedPresentTime, mOwnerUid);
466 if (!isVsyncValidForUid) {
467 ATRACE_NAME("!isVsyncValidForUid");
468 return false;
469 }
470
471 // AutoRefresh layers and sideband streams should always be presented
472 if (getSidebandStreamChanged() || getAutoRefresh()) {
473 return true;
474 }
475
Ady Abraham63a3e592021-01-06 10:47:15 -0800476 // If this layer doesn't have a frame is shouldn't be presented
477 if (!hasFrameUpdate()) {
478 return false;
479 }
480
481 // Defer to the derived class to decide whether the next buffer is due for
482 // presentation.
483 return isBufferDue(expectedPresentTime);
484}
485
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700486bool BufferLayer::latchBuffer(bool& recomputeVisibleRegions, nsecs_t latchTime,
487 nsecs_t expectedPresentTime) {
Marissa Wallfd668622018-05-10 10:21:13 -0700488 ATRACE_CALL();
David Sodman0c69cad2017-08-21 12:12:51 -0700489
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800490 bool refreshRequired = latchSidebandStream(recomputeVisibleRegions);
David Sodman0c69cad2017-08-21 12:12:51 -0700491
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800492 if (refreshRequired) {
493 return refreshRequired;
David Sodman0c69cad2017-08-21 12:12:51 -0700494 }
495
Marissa Wallfd668622018-05-10 10:21:13 -0700496 if (!hasReadyFrame()) {
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800497 return false;
David Sodman0c69cad2017-08-21 12:12:51 -0700498 }
David Sodman0c69cad2017-08-21 12:12:51 -0700499
Marissa Wallfd668622018-05-10 10:21:13 -0700500 // if we've already called updateTexImage() without going through
501 // a composition step, we have to skip this layer at this point
502 // because we cannot call updateTeximage() without a corresponding
503 // compositionComplete() call.
504 // we'll trigger an update in onPreComposition().
505 if (mRefreshPending) {
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800506 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700507 }
508
509 // If the head buffer's acquire fence hasn't signaled yet, return and
510 // try again later
511 if (!fenceHasSignaled()) {
Ady Abraham09bd3922019-04-08 10:44:56 -0700512 ATRACE_NAME("!fenceHasSignaled()");
David Sodman0c69cad2017-08-21 12:12:51 -0700513 mFlinger->signalLayerUpdate();
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800514 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700515 }
516
517 // Capture the old state of the layer for comparisons later
518 const State& s(getDrawingState());
519 const bool oldOpacity = isOpaque(s);
chaviwd62d3062019-09-04 14:48:02 -0700520
521 BufferInfo oldBufferInfo = mBufferInfo;
Marissa Wallfd668622018-05-10 10:21:13 -0700522
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700523 status_t err = updateTexImage(recomputeVisibleRegions, latchTime, expectedPresentTime);
Marissa Wallfd668622018-05-10 10:21:13 -0700524 if (err != NO_ERROR) {
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800525 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700526 }
527
528 err = updateActiveBuffer();
529 if (err != NO_ERROR) {
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800530 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700531 }
532
Marissa Wallfd668622018-05-10 10:21:13 -0700533 err = updateFrameNumber(latchTime);
534 if (err != NO_ERROR) {
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800535 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700536 }
537
chaviw4244e032019-09-04 11:27:49 -0700538 gatherBufferInfo();
539
Marissa Wallfd668622018-05-10 10:21:13 -0700540 mRefreshPending = true;
chaviwd62d3062019-09-04 14:48:02 -0700541 if (oldBufferInfo.mBuffer == nullptr) {
Marissa Wallfd668622018-05-10 10:21:13 -0700542 // the first time we receive a buffer, we need to trigger a
543 // geometry invalidation.
544 recomputeVisibleRegions = true;
545 }
546
chaviw4244e032019-09-04 11:27:49 -0700547 if ((mBufferInfo.mCrop != oldBufferInfo.mCrop) ||
548 (mBufferInfo.mTransform != oldBufferInfo.mTransform) ||
549 (mBufferInfo.mScaleMode != oldBufferInfo.mScaleMode) ||
550 (mBufferInfo.mTransformToDisplayInverse != oldBufferInfo.mTransformToDisplayInverse)) {
Marissa Wallfd668622018-05-10 10:21:13 -0700551 recomputeVisibleRegions = true;
552 }
553
chaviwd62d3062019-09-04 14:48:02 -0700554 if (oldBufferInfo.mBuffer != nullptr) {
Alec Mouria90a5702021-04-16 16:36:21 +0000555 uint32_t bufWidth = mBufferInfo.mBuffer->getBuffer()->getWidth();
556 uint32_t bufHeight = mBufferInfo.mBuffer->getBuffer()->getHeight();
557 if (bufWidth != uint32_t(oldBufferInfo.mBuffer->getBuffer()->width) ||
558 bufHeight != uint32_t(oldBufferInfo.mBuffer->getBuffer()->height)) {
Marissa Wallfd668622018-05-10 10:21:13 -0700559 recomputeVisibleRegions = true;
560 }
561 }
562
563 if (oldOpacity != isOpaque(s)) {
564 recomputeVisibleRegions = true;
565 }
566
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800567 return true;
Marissa Wallfd668622018-05-10 10:21:13 -0700568}
569
Marissa Wallfd668622018-05-10 10:21:13 -0700570bool BufferLayer::hasReadyFrame() const {
Marissa Wall024a1912018-08-13 13:55:35 -0700571 return hasFrameUpdate() || getSidebandStreamChanged() || getAutoRefresh();
Marissa Wallfd668622018-05-10 10:21:13 -0700572}
573
574uint32_t BufferLayer::getEffectiveScalingMode() const {
chaviw4244e032019-09-04 11:27:49 -0700575 return mBufferInfo.mScaleMode;
Marissa Wallfd668622018-05-10 10:21:13 -0700576}
577
578bool BufferLayer::isProtected() const {
Alec Mouria90a5702021-04-16 16:36:21 +0000579 return (mBufferInfo.mBuffer != nullptr) &&
580 (mBufferInfo.mBuffer->getBuffer()->getUsage() & GRALLOC_USAGE_PROTECTED);
Marissa Wallfd668622018-05-10 10:21:13 -0700581}
582
David Sodman0c69cad2017-08-21 12:12:51 -0700583// As documented in libhardware header, formats in the range
584// 0x100 - 0x1FF are specific to the HAL implementation, and
585// are known to have no alpha channel
586// TODO: move definition for device-specific range into
587// hardware.h, instead of using hard-coded values here.
588#define HARDWARE_IS_DEVICE_FORMAT(f) ((f) >= 0x100 && (f) <= 0x1FF)
589
590bool BufferLayer::getOpacityForFormat(uint32_t format) {
591 if (HARDWARE_IS_DEVICE_FORMAT(format)) {
592 return true;
593 }
594 switch (format) {
595 case HAL_PIXEL_FORMAT_RGBA_8888:
596 case HAL_PIXEL_FORMAT_BGRA_8888:
597 case HAL_PIXEL_FORMAT_RGBA_FP16:
598 case HAL_PIXEL_FORMAT_RGBA_1010102:
599 return false;
600 }
601 // in all other case, we have no blending (also for unknown formats)
602 return true;
603}
604
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700605bool BufferLayer::needsFiltering(const DisplayDevice* display) const {
606 const auto outputLayer = findOutputLayerForDisplay(display);
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800607 if (outputLayer == nullptr) {
Lloyd Piquef16688f2019-02-19 17:47:57 -0800608 return false;
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800609 }
610
Lloyd Piquef16688f2019-02-19 17:47:57 -0800611 // We need filtering if the sourceCrop rectangle size does not match the
612 // displayframe rectangle size (not a 1:1 render)
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800613 const auto& compositionState = outputLayer->getState();
614 const auto displayFrame = compositionState.displayFrame;
615 const auto sourceCrop = compositionState.sourceCrop;
Lloyd Piquef16688f2019-02-19 17:47:57 -0800616 return sourceCrop.getHeight() != displayFrame.getHeight() ||
Peiyong Linc2020ca2019-01-10 11:36:12 -0800617 sourceCrop.getWidth() != displayFrame.getWidth();
Chia-I Wu692e0832018-06-05 15:46:58 -0700618}
619
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700620bool BufferLayer::needsFilteringForScreenshots(const DisplayDevice* display,
Alec Mouri5a6d8572020-03-23 23:56:15 -0700621 const ui::Transform& inverseParentTransform) const {
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700622 const auto outputLayer = findOutputLayerForDisplay(display);
Alec Mouri5a6d8572020-03-23 23:56:15 -0700623 if (outputLayer == nullptr) {
624 return false;
625 }
626
627 // We need filtering if the sourceCrop rectangle size does not match the
628 // viewport rectangle size (not a 1:1 render)
629 const auto& compositionState = outputLayer->getState();
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700630 const ui::Transform& displayTransform = display->getTransform();
Alec Mouri5a6d8572020-03-23 23:56:15 -0700631 const ui::Transform inverseTransform = inverseParentTransform * displayTransform.inverse();
632 // Undo the transformation of the displayFrame so that we're back into
633 // layer-stack space.
634 const Rect frame = inverseTransform.transform(compositionState.displayFrame);
635 const FloatRect sourceCrop = compositionState.sourceCrop;
636
637 int32_t frameHeight = frame.getHeight();
638 int32_t frameWidth = frame.getWidth();
639 // If the display transform had a rotational component then undo the
640 // rotation so that the orientation matches the source crop.
641 if (displayTransform.getOrientation() & ui::Transform::ROT_90) {
642 std::swap(frameHeight, frameWidth);
643 }
644 return sourceCrop.getHeight() != frameHeight || sourceCrop.getWidth() != frameWidth;
645}
646
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700647uint64_t BufferLayer::getHeadFrameNumber(nsecs_t expectedPresentTime) const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800648 if (hasFrameUpdate()) {
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700649 return getFrameNumber(expectedPresentTime);
David Sodman0c69cad2017-08-21 12:12:51 -0700650 } else {
651 return mCurrentFrameNumber;
652 }
653}
654
Vishnu Nair60356342018-11-13 13:00:45 -0800655Rect BufferLayer::getBufferSize(const State& s) const {
656 // If we have a sideband stream, or we are scaling the buffer then return the layer size since
657 // we cannot determine the buffer size.
658 if ((s.sidebandStream != nullptr) ||
659 (getEffectiveScalingMode() != NATIVE_WINDOW_SCALING_MODE_FREEZE)) {
660 return Rect(getActiveWidth(s), getActiveHeight(s));
661 }
662
chaviwd62d3062019-09-04 14:48:02 -0700663 if (mBufferInfo.mBuffer == nullptr) {
Vishnu Nair60356342018-11-13 13:00:45 -0800664 return Rect::INVALID_RECT;
665 }
666
Alec Mouria90a5702021-04-16 16:36:21 +0000667 uint32_t bufWidth = mBufferInfo.mBuffer->getBuffer()->getWidth();
668 uint32_t bufHeight = mBufferInfo.mBuffer->getBuffer()->getHeight();
Vishnu Nair60356342018-11-13 13:00:45 -0800669
670 // Undo any transformations on the buffer and return the result.
chaviw4244e032019-09-04 11:27:49 -0700671 if (mBufferInfo.mTransform & ui::Transform::ROT_90) {
Vishnu Nair60356342018-11-13 13:00:45 -0800672 std::swap(bufWidth, bufHeight);
673 }
674
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800675 if (getTransformToDisplayInverse()) {
Dominik Laskowski718f9602019-11-09 20:01:35 -0800676 uint32_t invTransform = DisplayDevice::getPrimaryDisplayRotationFlags();
Vishnu Nair60356342018-11-13 13:00:45 -0800677 if (invTransform & ui::Transform::ROT_90) {
678 std::swap(bufWidth, bufHeight);
679 }
680 }
681
682 return Rect(bufWidth, bufHeight);
683}
684
Vishnu Nair4351ad52019-02-11 14:13:02 -0800685FloatRect BufferLayer::computeSourceBounds(const FloatRect& parentBounds) const {
686 const State& s(getDrawingState());
687
688 // If we have a sideband stream, or we are scaling the buffer then return the layer size since
689 // we cannot determine the buffer size.
690 if ((s.sidebandStream != nullptr) ||
691 (getEffectiveScalingMode() != NATIVE_WINDOW_SCALING_MODE_FREEZE)) {
692 return FloatRect(0, 0, getActiveWidth(s), getActiveHeight(s));
693 }
694
chaviwd62d3062019-09-04 14:48:02 -0700695 if (mBufferInfo.mBuffer == nullptr) {
Vishnu Nair4351ad52019-02-11 14:13:02 -0800696 return parentBounds;
697 }
698
Alec Mouria90a5702021-04-16 16:36:21 +0000699 uint32_t bufWidth = mBufferInfo.mBuffer->getBuffer()->getWidth();
700 uint32_t bufHeight = mBufferInfo.mBuffer->getBuffer()->getHeight();
Vishnu Nair4351ad52019-02-11 14:13:02 -0800701
702 // Undo any transformations on the buffer and return the result.
chaviw4244e032019-09-04 11:27:49 -0700703 if (mBufferInfo.mTransform & ui::Transform::ROT_90) {
Vishnu Nair4351ad52019-02-11 14:13:02 -0800704 std::swap(bufWidth, bufHeight);
705 }
706
707 if (getTransformToDisplayInverse()) {
Dominik Laskowski718f9602019-11-09 20:01:35 -0800708 uint32_t invTransform = DisplayDevice::getPrimaryDisplayRotationFlags();
Vishnu Nair4351ad52019-02-11 14:13:02 -0800709 if (invTransform & ui::Transform::ROT_90) {
710 std::swap(bufWidth, bufHeight);
711 }
712 }
713
714 return FloatRect(0, 0, bufWidth, bufHeight);
715}
716
chaviw49a108c2019-08-12 11:23:06 -0700717void BufferLayer::latchAndReleaseBuffer() {
718 mRefreshPending = false;
719 if (hasReadyFrame()) {
720 bool ignored = false;
721 latchBuffer(ignored, systemTime(), 0 /* expectedPresentTime */);
722 }
723 releasePendingBuffer(systemTime());
724}
725
chaviw4244e032019-09-04 11:27:49 -0700726PixelFormat BufferLayer::getPixelFormat() const {
727 return mBufferInfo.mPixelFormat;
728}
729
730bool BufferLayer::getTransformToDisplayInverse() const {
731 return mBufferInfo.mTransformToDisplayInverse;
732}
733
734Rect BufferLayer::getBufferCrop() const {
735 // this is the crop rectangle that applies to the buffer
736 // itself (as opposed to the window)
737 if (!mBufferInfo.mCrop.isEmpty()) {
738 // if the buffer crop is defined, we use that
739 return mBufferInfo.mCrop;
chaviwd62d3062019-09-04 14:48:02 -0700740 } else if (mBufferInfo.mBuffer != nullptr) {
chaviw4244e032019-09-04 11:27:49 -0700741 // otherwise we use the whole buffer
Alec Mouria90a5702021-04-16 16:36:21 +0000742 return mBufferInfo.mBuffer->getBuffer()->getBounds();
chaviw4244e032019-09-04 11:27:49 -0700743 } else {
744 // if we don't have a buffer yet, we use an empty/invalid crop
745 return Rect();
746 }
747}
748
749uint32_t BufferLayer::getBufferTransform() const {
750 return mBufferInfo.mTransform;
751}
752
753ui::Dataspace BufferLayer::getDataSpace() const {
754 return mBufferInfo.mDataspace;
755}
756
757ui::Dataspace BufferLayer::translateDataspace(ui::Dataspace dataspace) {
758 ui::Dataspace updatedDataspace = dataspace;
759 // translate legacy dataspaces to modern dataspaces
760 switch (dataspace) {
761 case ui::Dataspace::SRGB:
762 updatedDataspace = ui::Dataspace::V0_SRGB;
763 break;
764 case ui::Dataspace::SRGB_LINEAR:
765 updatedDataspace = ui::Dataspace::V0_SRGB_LINEAR;
766 break;
767 case ui::Dataspace::JFIF:
768 updatedDataspace = ui::Dataspace::V0_JFIF;
769 break;
770 case ui::Dataspace::BT601_625:
771 updatedDataspace = ui::Dataspace::V0_BT601_625;
772 break;
773 case ui::Dataspace::BT601_525:
774 updatedDataspace = ui::Dataspace::V0_BT601_525;
775 break;
776 case ui::Dataspace::BT709:
777 updatedDataspace = ui::Dataspace::V0_BT709;
778 break;
779 default:
780 break;
781 }
782
783 return updatedDataspace;
784}
785
chaviwd62d3062019-09-04 14:48:02 -0700786sp<GraphicBuffer> BufferLayer::getBuffer() const {
Alec Mouria90a5702021-04-16 16:36:21 +0000787 return mBufferInfo.mBuffer ? mBufferInfo.mBuffer->getBuffer() : nullptr;
chaviwd62d3062019-09-04 14:48:02 -0700788}
789
chaviwf83ce182019-09-12 14:43:08 -0700790void BufferLayer::getDrawingTransformMatrix(bool filteringEnabled, float outMatrix[16]) {
Alec Mouria90a5702021-04-16 16:36:21 +0000791 GLConsumer::computeTransformMatrix(outMatrix,
792 mBufferInfo.mBuffer ? mBufferInfo.mBuffer->getBuffer()
793 : nullptr,
794 mBufferInfo.mCrop, mBufferInfo.mTransform, filteringEnabled);
chaviwf83ce182019-09-12 14:43:08 -0700795}
796
chaviwb4c6e582019-08-16 14:35:07 -0700797void BufferLayer::setInitialValuesForClone(const sp<Layer>& clonedFrom) {
798 Layer::setInitialValuesForClone(clonedFrom);
799
800 sp<BufferLayer> bufferClonedFrom = static_cast<BufferLayer*>(clonedFrom.get());
801 mPremultipliedAlpha = bufferClonedFrom->mPremultipliedAlpha;
802 mPotentialCursor = bufferClonedFrom->mPotentialCursor;
803 mProtectedByApp = bufferClonedFrom->mProtectedByApp;
chaviw74b03172019-08-19 11:09:03 -0700804
805 updateCloneBufferInfo();
806}
807
808void BufferLayer::updateCloneBufferInfo() {
809 if (!isClone() || !isClonedFromAlive()) {
810 return;
811 }
812
813 sp<BufferLayer> clonedFrom = static_cast<BufferLayer*>(getClonedFrom().get());
814 mBufferInfo = clonedFrom->mBufferInfo;
815 mSidebandStream = clonedFrom->mSidebandStream;
816 surfaceDamageRegion = clonedFrom->surfaceDamageRegion;
817 mCurrentFrameNumber = clonedFrom->mCurrentFrameNumber.load();
818 mPreviousFrameNumber = clonedFrom->mPreviousFrameNumber;
819
820 // After buffer info is updated, the drawingState from the real layer needs to be copied into
821 // the cloned. This is because some properties of drawingState can change when latchBuffer is
chaviwaf87b3e2019-10-01 16:59:28 -0700822 // called. However, copying the drawingState would also overwrite the cloned layer's relatives
823 // and touchableRegionCrop. Therefore, temporarily store the relatives so they can be set in
824 // the cloned drawingState again.
chaviw74b03172019-08-19 11:09:03 -0700825 wp<Layer> tmpZOrderRelativeOf = mDrawingState.zOrderRelativeOf;
826 SortedVector<wp<Layer>> tmpZOrderRelatives = mDrawingState.zOrderRelatives;
chaviwaf87b3e2019-10-01 16:59:28 -0700827 wp<Layer> tmpTouchableRegionCrop = mDrawingState.touchableRegionCrop;
828 InputWindowInfo tmpInputInfo = mDrawingState.inputInfo;
829
chaviw74b03172019-08-19 11:09:03 -0700830 mDrawingState = clonedFrom->mDrawingState;
chaviwaf87b3e2019-10-01 16:59:28 -0700831
832 mDrawingState.touchableRegionCrop = tmpTouchableRegionCrop;
chaviw74b03172019-08-19 11:09:03 -0700833 mDrawingState.zOrderRelativeOf = tmpZOrderRelativeOf;
834 mDrawingState.zOrderRelatives = tmpZOrderRelatives;
chaviwaf87b3e2019-10-01 16:59:28 -0700835 mDrawingState.inputInfo = tmpInputInfo;
chaviwb4c6e582019-08-16 14:35:07 -0700836}
837
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700838void BufferLayer::setTransformHint(ui::Transform::RotationFlags displayTransformHint) {
Vishnu Nair6213bd92020-05-08 17:42:25 -0700839 mTransformHint = getFixedTransformHint();
840 if (mTransformHint == ui::Transform::ROT_INVALID) {
841 mTransformHint = displayTransformHint;
842 }
843}
844
Vishnu Naire7f79c52020-10-29 14:45:03 -0700845bool BufferLayer::bufferNeedsFiltering() const {
846 return isFixedSize();
847}
848
David Sodman0c69cad2017-08-21 12:12:51 -0700849} // namespace android
850
851#if defined(__gl_h_)
852#error "don't include gl/gl.h in this file"
853#endif
854
855#if defined(__gl2_h_)
856#error "don't include gl2/gl2.h in this file"
857#endif
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800858
859// TODO(b/129481165): remove the #pragma below and fix conversion issues
860#pragma clang diagnostic pop // ignored "-Wconversion"