blob: 3cb7e9cdb846564c167edb971c6ab59e28acded3 [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
chaviw98318de2021-05-19 16:45:23 -050061using gui::WindowInfo;
62
John Reckac09e452021-04-07 16:35:37 -040063static constexpr float defaultMaxLuminance = 1000.0;
Peiyong Lin1a70eca2019-11-15 09:33:33 -080064
Lloyd Pique42ab75e2018-09-12 20:46:03 -070065BufferLayer::BufferLayer(const LayerCreationArgs& args)
Lloyd Piquefeb73d72018-12-04 17:23:44 -080066 : Layer(args),
chaviwb4c6e582019-08-16 14:35:07 -070067 mTextureName(args.textureName),
Lloyd Piquede196652020-01-22 17:29:58 -080068 mCompositionState{mFlinger->getCompositionEngine().createLayerFECompositionState()} {
Dominik Laskowski87a07e42019-10-10 20:38:02 -070069 ALOGV("Creating Layer %s", getDebugName());
David Sodman0c69cad2017-08-21 12:12:51 -070070
Lloyd Pique42ab75e2018-09-12 20:46:03 -070071 mPremultipliedAlpha = !(args.flags & ISurfaceComposerClient::eNonPremultiplied);
David Sodman0c69cad2017-08-21 12:12:51 -070072
Lloyd Pique42ab75e2018-09-12 20:46:03 -070073 mPotentialCursor = args.flags & ISurfaceComposerClient::eCursorWindow;
74 mProtectedByApp = args.flags & ISurfaceComposerClient::eProtectedByApp;
David Sodman0c69cad2017-08-21 12:12:51 -070075}
76
77BufferLayer::~BufferLayer() {
chaviwb4c6e582019-08-16 14:35:07 -070078 if (!isClone()) {
79 // The original layer and the clone layer share the same texture. Therefore, only one of
80 // the layers, in this case the original layer, needs to handle the deletion. The original
81 // layer and the clone should be removed at the same time so there shouldn't be any issue
82 // with the clone layer trying to use the deleted texture.
83 mFlinger->deleteTextureAsync(mTextureName);
84 }
Yiwei Zhang1a88c402019-11-18 10:43:58 -080085 const int32_t layerId = getSequence();
86 mFlinger->mTimeStats->onDestroy(layerId);
87 mFlinger->mFrameTracer->onDestroy(layerId);
David Sodman0c69cad2017-08-21 12:12:51 -070088}
89
David Sodmaneb085e02017-10-05 18:49:04 -070090void BufferLayer::useSurfaceDamage() {
91 if (mFlinger->mForceFullDamage) {
92 surfaceDamageRegion = Region::INVALID_REGION;
93 } else {
chaviw4244e032019-09-04 11:27:49 -070094 surfaceDamageRegion = mBufferInfo.mSurfaceDamage;
David Sodmaneb085e02017-10-05 18:49:04 -070095 }
96}
97
98void BufferLayer::useEmptyDamage() {
99 surfaceDamageRegion.clear();
100}
101
Marissa Wallfd668622018-05-10 10:21:13 -0700102bool BufferLayer::isOpaque(const Layer::State& s) const {
103 // if we don't have a buffer or sidebandStream yet, we're translucent regardless of the
104 // layer's opaque flag.
chaviwd62d3062019-09-04 14:48:02 -0700105 if ((mSidebandStream == nullptr) && (mBufferInfo.mBuffer == nullptr)) {
Marissa Wallfd668622018-05-10 10:21:13 -0700106 return false;
107 }
108
109 // if the layer has the opaque flag, then we're always opaque,
110 // otherwise we use the current buffer's format.
111 return ((s.flags & layer_state_t::eLayerOpaque) != 0) || getOpacityForFormat(getPixelFormat());
David Sodman0c69cad2017-08-21 12:12:51 -0700112}
113
114bool BufferLayer::isVisible() const {
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -0700115 return !isHiddenByPolicy() && getAlpha() > 0.0f &&
chaviwd62d3062019-09-04 14:48:02 -0700116 (mBufferInfo.mBuffer != nullptr || mSidebandStream != nullptr);
David Sodman0c69cad2017-08-21 12:12:51 -0700117}
118
119bool BufferLayer::isFixedSize() const {
120 return getEffectiveScalingMode() != NATIVE_WINDOW_SCALING_MODE_FREEZE;
121}
122
Lloyd Piquea83776c2019-01-29 18:42:32 -0800123bool BufferLayer::usesSourceCrop() const {
124 return true;
125}
126
David Sodman0c69cad2017-08-21 12:12:51 -0700127static constexpr mat4 inverseOrientation(uint32_t transform) {
David Sodman41fdfc92017-11-06 16:09:56 -0800128 const mat4 flipH(-1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1);
129 const mat4 flipV(1, 0, 0, 0, 0, -1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1);
130 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 -0700131 mat4 tr;
132
133 if (transform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
134 tr = tr * rot90;
135 }
136 if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_H) {
137 tr = tr * flipH;
138 }
139 if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_V) {
140 tr = tr * flipV;
141 }
142 return inverse(tr);
143}
144
Vishnu Nair9b079a22020-01-21 14:36:08 -0800145std::optional<compositionengine::LayerFE::LayerSettings> BufferLayer::prepareClientComposition(
Lloyd Piquef16688f2019-02-19 17:47:57 -0800146 compositionengine::LayerFE::ClientCompositionTargetSettings& targetSettings) {
David Sodman0c69cad2017-08-21 12:12:51 -0700147 ATRACE_CALL();
Lloyd Piquef16688f2019-02-19 17:47:57 -0800148
Vishnu Nair9b079a22020-01-21 14:36:08 -0800149 std::optional<compositionengine::LayerFE::LayerSettings> result =
150 Layer::prepareClientComposition(targetSettings);
Lloyd Piquef16688f2019-02-19 17:47:57 -0800151 if (!result) {
152 return result;
153 }
154
chaviwd62d3062019-09-04 14:48:02 -0700155 if (CC_UNLIKELY(mBufferInfo.mBuffer == 0)) {
David Sodman0c69cad2017-08-21 12:12:51 -0700156 // the texture has not been created yet, this Layer has
157 // in fact never been drawn into. This happens frequently with
158 // SurfaceView because the WindowManager can't know when the client
159 // has drawn the first time.
160
161 // If there is nothing under us, we paint the screen in black, otherwise
162 // we just skip this update.
163
164 // figure out if there is something below us
165 Region under;
166 bool finished = false;
167 mFlinger->mDrawingState.traverseInZOrder([&](Layer* layer) {
168 if (finished || layer == static_cast<BufferLayer const*>(this)) {
169 finished = true;
170 return;
171 }
Lloyd Piquea2468662019-03-07 21:31:06 -0800172
173 under.orSelf(layer->getScreenBounds());
David Sodman0c69cad2017-08-21 12:12:51 -0700174 });
175 // if not everything below us is covered, we plug the holes!
Lloyd Piquef16688f2019-02-19 17:47:57 -0800176 Region holes(targetSettings.clip.subtract(under));
David Sodman0c69cad2017-08-21 12:12:51 -0700177 if (!holes.isEmpty()) {
Lloyd Piquef16688f2019-02-19 17:47:57 -0800178 targetSettings.clearRegion.orSelf(holes);
David Sodman0c69cad2017-08-21 12:12:51 -0700179 }
Tianhua Sundcff00c2020-12-29 07:20:55 -0500180
181 if (mSidebandStream != nullptr) {
182 // For surfaceview of tv sideband, there is no activeBuffer
183 // in bufferqueue, we need return LayerSettings.
184 return result;
185 } else {
186 return std::nullopt;
187 }
David Sodman0c69cad2017-08-21 12:12:51 -0700188 }
Ady Abraham7a60afb2021-03-29 13:20:55 -0700189 const bool blackOutLayer = (isProtected() && !targetSettings.supportsProtectedContent) ||
yuhui.zhanga102ff92021-04-09 14:45:51 +0800190 ((isSecure() || isProtected()) && !targetSettings.isSecure);
Ady Abraham7a60afb2021-03-29 13:20:55 -0700191 const bool bufferCanBeUsedAsHwTexture =
Alec Mouria90a5702021-04-16 16:36:21 +0000192 mBufferInfo.mBuffer->getBuffer()->getUsage() & GraphicBuffer::USAGE_HW_TEXTURE;
Lloyd Piquede196652020-01-22 17:29:58 -0800193 compositionengine::LayerFE::LayerSettings& layer = *result;
Ady Abraham7a60afb2021-03-29 13:20:55 -0700194 if (blackOutLayer || !bufferCanBeUsedAsHwTexture) {
195 ALOGE_IF(!bufferCanBeUsedAsHwTexture, "%s is blacked out as buffer is not gpu readable",
196 mName.c_str());
Vishnu Nairb87d94f2020-02-13 09:17:36 -0800197 prepareClearClientComposition(layer, true /* blackout */);
198 return layer;
David Sodman0c69cad2017-08-21 12:12:51 -0700199 }
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000200
Vishnu Nairb87d94f2020-02-13 09:17:36 -0800201 const State& s(getDrawingState());
202 layer.source.buffer.buffer = mBufferInfo.mBuffer;
203 layer.source.buffer.isOpaque = isOpaque(s);
204 layer.source.buffer.fence = mBufferInfo.mFence;
205 layer.source.buffer.textureName = mTextureName;
206 layer.source.buffer.usePremultipliedAlpha = getPremultipledAlpha();
207 layer.source.buffer.isY410BT2020 = isHdrY410();
208 bool hasSmpte2086 = mBufferInfo.mHdrMetadata.validTypes & HdrMetadata::SMPTE2086;
209 bool hasCta861_3 = mBufferInfo.mHdrMetadata.validTypes & HdrMetadata::CTA861_3;
John Reckac09e452021-04-07 16:35:37 -0400210 float maxLuminance = 0.f;
211 if (hasSmpte2086 && hasCta861_3) {
212 maxLuminance = std::min(mBufferInfo.mHdrMetadata.smpte2086.maxLuminance,
213 mBufferInfo.mHdrMetadata.cta8613.maxContentLightLevel);
214 } else if (hasSmpte2086) {
215 maxLuminance = mBufferInfo.mHdrMetadata.smpte2086.maxLuminance;
216 } else if (hasCta861_3) {
217 maxLuminance = mBufferInfo.mHdrMetadata.cta8613.maxContentLightLevel;
218 } else {
219 switch (layer.sourceDataspace & HAL_DATASPACE_TRANSFER_MASK) {
220 case HAL_DATASPACE_TRANSFER_ST2084:
221 case HAL_DATASPACE_TRANSFER_HLG:
222 // Behavior-match previous releases for HDR content
223 maxLuminance = defaultMaxLuminance;
224 break;
225 }
226 }
227 layer.source.buffer.maxLuminanceNits = maxLuminance;
Vishnu Nairb87d94f2020-02-13 09:17:36 -0800228 layer.frameNumber = mCurrentFrameNumber;
Alec Mouria90a5702021-04-16 16:36:21 +0000229 layer.bufferId = mBufferInfo.mBuffer ? mBufferInfo.mBuffer->getBuffer()->getId() : 0;
Vishnu Nairb87d94f2020-02-13 09:17:36 -0800230
Vishnu Naire7f79c52020-10-29 14:45:03 -0700231 const bool useFiltering =
232 targetSettings.needsFiltering || mNeedsFiltering || bufferNeedsFiltering();
Vishnu Nairb87d94f2020-02-13 09:17:36 -0800233
234 // Query the texture matrix given our current filtering mode.
235 float textureMatrix[16];
236 getDrawingTransformMatrix(useFiltering, textureMatrix);
237
238 if (getTransformToDisplayInverse()) {
239 /*
240 * the code below applies the primary display's inverse transform to
241 * the texture transform
242 */
243 uint32_t transform = DisplayDevice::getPrimaryDisplayRotationFlags();
244 mat4 tr = inverseOrientation(transform);
245
246 /**
247 * TODO(b/36727915): This is basically a hack.
248 *
249 * Ensure that regardless of the parent transformation,
250 * this buffer is always transformed from native display
251 * orientation to display orientation. For example, in the case
252 * of a camera where the buffer remains in native orientation,
253 * we want the pixels to always be upright.
254 */
255 sp<Layer> p = mDrawingParent.promote();
256 if (p != nullptr) {
257 const auto parentTransform = p->getTransform();
258 tr = tr * inverseOrientation(parentTransform.getOrientation());
259 }
260
261 // and finally apply it to the original texture matrix
262 const mat4 texTransform(mat4(static_cast<const float*>(textureMatrix)) * tr);
263 memcpy(textureMatrix, texTransform.asArray(), sizeof(textureMatrix));
264 }
265
266 const Rect win{getBounds()};
267 float bufferWidth = getBufferSize(s).getWidth();
268 float bufferHeight = getBufferSize(s).getHeight();
269
270 // BufferStateLayers can have a "buffer size" of [0, 0, -1, -1] when no display frame has
271 // been set and there is no parent layer bounds. In that case, the scale is meaningless so
272 // ignore them.
273 if (!getBufferSize(s).isValid()) {
274 bufferWidth = float(win.right) - float(win.left);
275 bufferHeight = float(win.bottom) - float(win.top);
276 }
277
278 const float scaleHeight = (float(win.bottom) - float(win.top)) / bufferHeight;
279 const float scaleWidth = (float(win.right) - float(win.left)) / bufferWidth;
280 const float translateY = float(win.top) / bufferHeight;
281 const float translateX = float(win.left) / bufferWidth;
282
283 // Flip y-coordinates because GLConsumer expects OpenGL convention.
284 mat4 tr = mat4::translate(vec4(.5, .5, 0, 1)) * mat4::scale(vec4(1, -1, 1, 1)) *
285 mat4::translate(vec4(-.5, -.5, 0, 1)) *
286 mat4::translate(vec4(translateX, translateY, 0, 1)) *
287 mat4::scale(vec4(scaleWidth, scaleHeight, 1.0, 1.0));
288
289 layer.source.buffer.useTextureFiltering = useFiltering;
290 layer.source.buffer.textureTransform = mat4(static_cast<const float*>(textureMatrix)) * tr;
291
Vishnu Nair9b079a22020-01-21 14:36:08 -0800292 return layer;
David Sodman0c69cad2017-08-21 12:12:51 -0700293}
294
Marissa Wallfd668622018-05-10 10:21:13 -0700295bool BufferLayer::isHdrY410() const {
296 // pixel format is HDR Y410 masquerading as RGBA_1010102
chaviw4244e032019-09-04 11:27:49 -0700297 return (mBufferInfo.mDataspace == ui::Dataspace::BT2020_ITU_PQ &&
298 mBufferInfo.mApi == NATIVE_WINDOW_API_MEDIA &&
chaviwdebadb82020-03-26 14:57:24 -0700299 mBufferInfo.mPixelFormat == HAL_PIXEL_FORMAT_RGBA_1010102);
David Sodmaneb085e02017-10-05 18:49:04 -0700300}
301
Lloyd Piquede196652020-01-22 17:29:58 -0800302sp<compositionengine::LayerFE> BufferLayer::getCompositionEngineLayerFE() const {
303 return asLayerFE();
304}
305
306compositionengine::LayerFECompositionState* BufferLayer::editCompositionState() {
307 return mCompositionState.get();
308}
309
310const compositionengine::LayerFECompositionState* BufferLayer::getCompositionState() const {
311 return mCompositionState.get();
312}
313
314void BufferLayer::preparePerFrameCompositionState() {
315 Layer::preparePerFrameCompositionState();
David Sodman0c69cad2017-08-21 12:12:51 -0700316
317 // Sideband layers
Lloyd Piquede196652020-01-22 17:29:58 -0800318 auto* compositionState = editCompositionState();
319 if (compositionState->sidebandStream.get()) {
320 compositionState->compositionType = Hwc2::IComposerClient::Composition::SIDEBAND;
Robert Carra6bb2bc2020-04-08 11:03:23 -0700321 return;
David Sodman15094112018-10-11 09:39:37 -0700322 } else {
Lloyd Piquef5275482019-01-29 18:42:42 -0800323 // Normal buffer layers
Lloyd Piquede196652020-01-22 17:29:58 -0800324 compositionState->hdrMetadata = mBufferInfo.mHdrMetadata;
325 compositionState->compositionType = mPotentialCursor
Lloyd Piquef5275482019-01-29 18:42:42 -0800326 ? Hwc2::IComposerClient::Composition::CURSOR
327 : Hwc2::IComposerClient::Composition::DEVICE;
David Sodman0c69cad2017-08-21 12:12:51 -0700328 }
Robert Carra6bb2bc2020-04-08 11:03:23 -0700329
Alec Mouria90a5702021-04-16 16:36:21 +0000330 compositionState->buffer = mBufferInfo.mBuffer->getBuffer();
Robert Carra6bb2bc2020-04-08 11:03:23 -0700331 compositionState->bufferSlot = (mBufferInfo.mBufferSlot == BufferQueue::INVALID_BUFFER_SLOT)
332 ? 0
333 : mBufferInfo.mBufferSlot;
334 compositionState->acquireFence = mBufferInfo.mFence;
David Sodman0c69cad2017-08-21 12:12:51 -0700335}
336
Marissa Wallfd668622018-05-10 10:21:13 -0700337bool BufferLayer::onPreComposition(nsecs_t refreshStartTime) {
chaviwd62d3062019-09-04 14:48:02 -0700338 if (mBufferInfo.mBuffer != nullptr) {
Marissa Wallfd668622018-05-10 10:21:13 -0700339 Mutex::Autolock lock(mFrameEventHistoryMutex);
340 mFrameEventHistory.addPreComposition(mCurrentFrameNumber, refreshStartTime);
David Sodman0c69cad2017-08-21 12:12:51 -0700341 }
Marissa Wallfd668622018-05-10 10:21:13 -0700342 mRefreshPending = false;
343 return hasReadyFrame();
David Sodman0c69cad2017-08-21 12:12:51 -0700344}
Ady Abraham8b9e6122021-01-26 19:11:45 -0800345namespace {
346TimeStats::SetFrameRateVote frameRateToSetFrameRateVotePayload(Layer::FrameRate frameRate) {
347 using FrameRateCompatibility = TimeStats::SetFrameRateVote::FrameRateCompatibility;
348 using Seamlessness = TimeStats::SetFrameRateVote::Seamlessness;
349 const auto frameRateCompatibility = [frameRate] {
350 switch (frameRate.type) {
351 case Layer::FrameRateCompatibility::Default:
352 return FrameRateCompatibility::Default;
353 case Layer::FrameRateCompatibility::ExactOrMultiple:
354 return FrameRateCompatibility::ExactOrMultiple;
355 default:
356 return FrameRateCompatibility::Undefined;
357 }
358 }();
359
360 const auto seamlessness = [frameRate] {
361 switch (frameRate.seamlessness) {
362 case scheduler::Seamlessness::OnlySeamless:
363 return Seamlessness::ShouldBeSeamless;
364 case scheduler::Seamlessness::SeamedAndSeamless:
365 return Seamlessness::NotRequired;
366 default:
367 return Seamlessness::Undefined;
368 }
369 }();
370
371 return TimeStats::SetFrameRateVote{.frameRate = frameRate.rate.getValue(),
372 .frameRateCompatibility = frameRateCompatibility,
373 .seamlessness = seamlessness};
374}
375} // namespace
David Sodman0c69cad2017-08-21 12:12:51 -0700376
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700377bool BufferLayer::onPostComposition(const DisplayDevice* display,
Dominik Laskowski075d3172018-05-24 15:50:06 -0700378 const std::shared_ptr<FenceTime>& glDoneFence,
Marissa Wallfd668622018-05-10 10:21:13 -0700379 const std::shared_ptr<FenceTime>& presentFence,
380 const CompositorTiming& compositorTiming) {
381 // mFrameLatencyNeeded is true when a new frame was latched for the
382 // composition.
chaviw74b03172019-08-19 11:09:03 -0700383 if (!mBufferInfo.mFrameLatencyNeeded) return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700384
385 // Update mFrameEventHistory.
Dan Stoza436ccf32018-06-21 12:10:12 -0700386 {
Marissa Wallfd668622018-05-10 10:21:13 -0700387 Mutex::Autolock lock(mFrameEventHistoryMutex);
388 mFrameEventHistory.addPostComposition(mCurrentFrameNumber, glDoneFence, presentFence,
389 compositorTiming);
Valerie Hau93d5a5e2020-02-13 14:50:01 -0800390 finalizeFrameEventHistory(glDoneFence, compositorTiming);
David Sodman0c69cad2017-08-21 12:12:51 -0700391 }
392
Marissa Wallfd668622018-05-10 10:21:13 -0700393 // Update mFrameTracker.
chaviw4244e032019-09-04 11:27:49 -0700394 nsecs_t desiredPresentTime = mBufferInfo.mDesiredPresentTime;
Marissa Wallfd668622018-05-10 10:21:13 -0700395 mFrameTracker.setDesiredPresentTime(desiredPresentTime);
396
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800397 const int32_t layerId = getSequence();
398 mFlinger->mTimeStats->setDesiredTime(layerId, mCurrentFrameNumber, desiredPresentTime);
Marissa Wallfd668622018-05-10 10:21:13 -0700399
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700400 const auto outputLayer = findOutputLayerForDisplay(display);
Adithya Srinivasanb69e0762019-11-11 18:39:53 -0800401 if (outputLayer && outputLayer->requiresClientComposition()) {
402 nsecs_t clientCompositionTimestamp = outputLayer->getState().clientCompositionTimestamp;
403 mFlinger->mFrameTracer->traceTimestamp(layerId, getCurrentBufferId(), mCurrentFrameNumber,
404 clientCompositionTimestamp,
405 FrameTracer::FrameEvent::FALLBACK_COMPOSITION);
Adithya Srinivasanb6a2fa12021-03-13 00:23:09 +0000406 // Update the SurfaceFrames in the drawing state
407 if (mDrawingState.bufferSurfaceFrameTX) {
408 mDrawingState.bufferSurfaceFrameTX->setGpuComposition();
409 }
410 for (auto& [token, surfaceFrame] : mDrawingState.bufferlessSurfaceFramesTX) {
411 surfaceFrame->setGpuComposition();
412 }
Adithya Srinivasanb69e0762019-11-11 18:39:53 -0800413 }
414
chaviw4244e032019-09-04 11:27:49 -0700415 std::shared_ptr<FenceTime> frameReadyFence = mBufferInfo.mFenceTime;
Marissa Wallfd668622018-05-10 10:21:13 -0700416 if (frameReadyFence->isValid()) {
417 mFrameTracker.setFrameReadyFence(std::move(frameReadyFence));
418 } else {
419 // There was no fence for this frame, so assume that it was ready
420 // to be presented at the desired present time.
421 mFrameTracker.setFrameReadyTime(desiredPresentTime);
Dominik Laskowski45de9bd2018-06-11 17:44:10 -0700422 }
Marissa Wallfd668622018-05-10 10:21:13 -0700423
Alec Mouri7d436ec2021-01-27 20:40:50 -0800424 const Fps refreshRate = mFlinger->mRefreshRateConfigs->getCurrentRefreshRate().getFps();
425 const std::optional<Fps> renderRate = mFlinger->mScheduler->getFrameRateOverride(getOwnerUid());
Marissa Wallfd668622018-05-10 10:21:13 -0700426 if (presentFence->isValid()) {
Alec Mouri7d436ec2021-01-27 20:40:50 -0800427 mFlinger->mTimeStats->setPresentFence(layerId, mCurrentFrameNumber, presentFence,
Ady Abraham8b9e6122021-01-26 19:11:45 -0800428 refreshRate, renderRate,
429 frameRateToSetFrameRateVotePayload(
Adithya Srinivasan58069dc2021-06-04 20:37:02 +0000430 mDrawingState.frameRate),
431 getGameMode());
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800432 mFlinger->mFrameTracer->traceFence(layerId, getCurrentBufferId(), mCurrentFrameNumber,
Mikael Pessa90092f42019-08-26 17:22:04 -0700433 presentFence, FrameTracer::FrameEvent::PRESENT_FENCE);
Marissa Wallfd668622018-05-10 10:21:13 -0700434 mFrameTracker.setActualPresentFence(std::shared_ptr<FenceTime>(presentFence));
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700435 } else if (!display) {
436 // Do nothing.
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200437 } else if (const auto displayId = PhysicalDisplayId::tryCast(display->getId());
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700438 displayId && mFlinger->getHwComposer().isConnected(*displayId)) {
Marissa Wallfd668622018-05-10 10:21:13 -0700439 // The HWC doesn't support present fences, so use the refresh
440 // timestamp instead.
Marin Shalamanov045b7002021-01-07 16:56:24 +0100441 const nsecs_t actualPresentTime = display->getRefreshTimestamp();
Alec Mouri7d436ec2021-01-27 20:40:50 -0800442 mFlinger->mTimeStats->setPresentTime(layerId, mCurrentFrameNumber, actualPresentTime,
Ady Abraham8b9e6122021-01-26 19:11:45 -0800443 refreshRate, renderRate,
444 frameRateToSetFrameRateVotePayload(
Adithya Srinivasan58069dc2021-06-04 20:37:02 +0000445 mDrawingState.frameRate),
446 getGameMode());
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800447 mFlinger->mFrameTracer->traceTimestamp(layerId, getCurrentBufferId(), mCurrentFrameNumber,
Mikael Pessa90092f42019-08-26 17:22:04 -0700448 actualPresentTime,
449 FrameTracer::FrameEvent::PRESENT_FENCE);
Marissa Wallfd668622018-05-10 10:21:13 -0700450 mFrameTracker.setActualPresentTime(actualPresentTime);
451 }
452
453 mFrameTracker.advanceFrame();
chaviw74b03172019-08-19 11:09:03 -0700454 mBufferInfo.mFrameLatencyNeeded = false;
Marissa Wallfd668622018-05-10 10:21:13 -0700455 return true;
David Sodman0c69cad2017-08-21 12:12:51 -0700456}
457
chaviwdebadb82020-03-26 14:57:24 -0700458void BufferLayer::gatherBufferInfo() {
459 mBufferInfo.mPixelFormat =
Alec Mouria90a5702021-04-16 16:36:21 +0000460 !mBufferInfo.mBuffer ? PIXEL_FORMAT_NONE : mBufferInfo.mBuffer->getBuffer()->format;
chaviwdebadb82020-03-26 14:57:24 -0700461 mBufferInfo.mFrameLatencyNeeded = true;
462}
463
Ady Abraham63a3e592021-01-06 10:47:15 -0800464bool BufferLayer::shouldPresentNow(nsecs_t expectedPresentTime) const {
465 // If this is not a valid vsync for the layer's uid, return and try again later
466 const bool isVsyncValidForUid =
467 mFlinger->mScheduler->isVsyncValid(expectedPresentTime, mOwnerUid);
468 if (!isVsyncValidForUid) {
469 ATRACE_NAME("!isVsyncValidForUid");
470 return false;
471 }
472
473 // AutoRefresh layers and sideband streams should always be presented
474 if (getSidebandStreamChanged() || getAutoRefresh()) {
475 return true;
476 }
477
Ady Abraham63a3e592021-01-06 10:47:15 -0800478 // If this layer doesn't have a frame is shouldn't be presented
479 if (!hasFrameUpdate()) {
480 return false;
481 }
482
483 // Defer to the derived class to decide whether the next buffer is due for
484 // presentation.
485 return isBufferDue(expectedPresentTime);
486}
487
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700488bool BufferLayer::latchBuffer(bool& recomputeVisibleRegions, nsecs_t latchTime,
489 nsecs_t expectedPresentTime) {
Marissa Wallfd668622018-05-10 10:21:13 -0700490 ATRACE_CALL();
David Sodman0c69cad2017-08-21 12:12:51 -0700491
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800492 bool refreshRequired = latchSidebandStream(recomputeVisibleRegions);
David Sodman0c69cad2017-08-21 12:12:51 -0700493
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800494 if (refreshRequired) {
495 return refreshRequired;
David Sodman0c69cad2017-08-21 12:12:51 -0700496 }
497
Marissa Wallfd668622018-05-10 10:21:13 -0700498 if (!hasReadyFrame()) {
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800499 return false;
David Sodman0c69cad2017-08-21 12:12:51 -0700500 }
David Sodman0c69cad2017-08-21 12:12:51 -0700501
Marissa Wallfd668622018-05-10 10:21:13 -0700502 // if we've already called updateTexImage() without going through
503 // a composition step, we have to skip this layer at this point
504 // because we cannot call updateTeximage() without a corresponding
505 // compositionComplete() call.
506 // we'll trigger an update in onPreComposition().
507 if (mRefreshPending) {
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800508 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700509 }
510
511 // If the head buffer's acquire fence hasn't signaled yet, return and
512 // try again later
513 if (!fenceHasSignaled()) {
Ady Abraham09bd3922019-04-08 10:44:56 -0700514 ATRACE_NAME("!fenceHasSignaled()");
David Sodman0c69cad2017-08-21 12:12:51 -0700515 mFlinger->signalLayerUpdate();
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800516 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700517 }
518
519 // Capture the old state of the layer for comparisons later
520 const State& s(getDrawingState());
521 const bool oldOpacity = isOpaque(s);
chaviwd62d3062019-09-04 14:48:02 -0700522
523 BufferInfo oldBufferInfo = mBufferInfo;
Marissa Wallfd668622018-05-10 10:21:13 -0700524
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700525 status_t err = updateTexImage(recomputeVisibleRegions, latchTime, expectedPresentTime);
Marissa Wallfd668622018-05-10 10:21:13 -0700526 if (err != NO_ERROR) {
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800527 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700528 }
529
530 err = updateActiveBuffer();
531 if (err != NO_ERROR) {
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800532 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700533 }
534
Marissa Wallfd668622018-05-10 10:21:13 -0700535 err = updateFrameNumber(latchTime);
536 if (err != NO_ERROR) {
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800537 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700538 }
539
chaviw4244e032019-09-04 11:27:49 -0700540 gatherBufferInfo();
541
Marissa Wallfd668622018-05-10 10:21:13 -0700542 mRefreshPending = true;
chaviwd62d3062019-09-04 14:48:02 -0700543 if (oldBufferInfo.mBuffer == nullptr) {
Marissa Wallfd668622018-05-10 10:21:13 -0700544 // the first time we receive a buffer, we need to trigger a
545 // geometry invalidation.
546 recomputeVisibleRegions = true;
547 }
548
chaviw4244e032019-09-04 11:27:49 -0700549 if ((mBufferInfo.mCrop != oldBufferInfo.mCrop) ||
550 (mBufferInfo.mTransform != oldBufferInfo.mTransform) ||
551 (mBufferInfo.mScaleMode != oldBufferInfo.mScaleMode) ||
552 (mBufferInfo.mTransformToDisplayInverse != oldBufferInfo.mTransformToDisplayInverse)) {
Marissa Wallfd668622018-05-10 10:21:13 -0700553 recomputeVisibleRegions = true;
554 }
555
chaviwd62d3062019-09-04 14:48:02 -0700556 if (oldBufferInfo.mBuffer != nullptr) {
Alec Mouria90a5702021-04-16 16:36:21 +0000557 uint32_t bufWidth = mBufferInfo.mBuffer->getBuffer()->getWidth();
558 uint32_t bufHeight = mBufferInfo.mBuffer->getBuffer()->getHeight();
559 if (bufWidth != uint32_t(oldBufferInfo.mBuffer->getBuffer()->width) ||
560 bufHeight != uint32_t(oldBufferInfo.mBuffer->getBuffer()->height)) {
Marissa Wallfd668622018-05-10 10:21:13 -0700561 recomputeVisibleRegions = true;
562 }
563 }
564
565 if (oldOpacity != isOpaque(s)) {
566 recomputeVisibleRegions = true;
567 }
568
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800569 return true;
Marissa Wallfd668622018-05-10 10:21:13 -0700570}
571
Marissa Wallfd668622018-05-10 10:21:13 -0700572bool BufferLayer::hasReadyFrame() const {
Marissa Wall024a1912018-08-13 13:55:35 -0700573 return hasFrameUpdate() || getSidebandStreamChanged() || getAutoRefresh();
Marissa Wallfd668622018-05-10 10:21:13 -0700574}
575
576uint32_t BufferLayer::getEffectiveScalingMode() const {
chaviw4244e032019-09-04 11:27:49 -0700577 return mBufferInfo.mScaleMode;
Marissa Wallfd668622018-05-10 10:21:13 -0700578}
579
580bool BufferLayer::isProtected() const {
Alec Mouria90a5702021-04-16 16:36:21 +0000581 return (mBufferInfo.mBuffer != nullptr) &&
582 (mBufferInfo.mBuffer->getBuffer()->getUsage() & GRALLOC_USAGE_PROTECTED);
Marissa Wallfd668622018-05-10 10:21:13 -0700583}
584
David Sodman0c69cad2017-08-21 12:12:51 -0700585// As documented in libhardware header, formats in the range
586// 0x100 - 0x1FF are specific to the HAL implementation, and
587// are known to have no alpha channel
588// TODO: move definition for device-specific range into
589// hardware.h, instead of using hard-coded values here.
590#define HARDWARE_IS_DEVICE_FORMAT(f) ((f) >= 0x100 && (f) <= 0x1FF)
591
592bool BufferLayer::getOpacityForFormat(uint32_t format) {
593 if (HARDWARE_IS_DEVICE_FORMAT(format)) {
594 return true;
595 }
596 switch (format) {
597 case HAL_PIXEL_FORMAT_RGBA_8888:
598 case HAL_PIXEL_FORMAT_BGRA_8888:
599 case HAL_PIXEL_FORMAT_RGBA_FP16:
600 case HAL_PIXEL_FORMAT_RGBA_1010102:
601 return false;
602 }
603 // in all other case, we have no blending (also for unknown formats)
604 return true;
605}
606
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700607bool BufferLayer::needsFiltering(const DisplayDevice* display) const {
608 const auto outputLayer = findOutputLayerForDisplay(display);
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800609 if (outputLayer == nullptr) {
Lloyd Piquef16688f2019-02-19 17:47:57 -0800610 return false;
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800611 }
612
Lloyd Piquef16688f2019-02-19 17:47:57 -0800613 // We need filtering if the sourceCrop rectangle size does not match the
614 // displayframe rectangle size (not a 1:1 render)
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800615 const auto& compositionState = outputLayer->getState();
616 const auto displayFrame = compositionState.displayFrame;
617 const auto sourceCrop = compositionState.sourceCrop;
Lloyd Piquef16688f2019-02-19 17:47:57 -0800618 return sourceCrop.getHeight() != displayFrame.getHeight() ||
Peiyong Linc2020ca2019-01-10 11:36:12 -0800619 sourceCrop.getWidth() != displayFrame.getWidth();
Chia-I Wu692e0832018-06-05 15:46:58 -0700620}
621
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700622bool BufferLayer::needsFilteringForScreenshots(const DisplayDevice* display,
Alec Mouri5a6d8572020-03-23 23:56:15 -0700623 const ui::Transform& inverseParentTransform) const {
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700624 const auto outputLayer = findOutputLayerForDisplay(display);
Alec Mouri5a6d8572020-03-23 23:56:15 -0700625 if (outputLayer == nullptr) {
626 return false;
627 }
628
629 // We need filtering if the sourceCrop rectangle size does not match the
630 // viewport rectangle size (not a 1:1 render)
631 const auto& compositionState = outputLayer->getState();
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700632 const ui::Transform& displayTransform = display->getTransform();
Alec Mouri5a6d8572020-03-23 23:56:15 -0700633 const ui::Transform inverseTransform = inverseParentTransform * displayTransform.inverse();
634 // Undo the transformation of the displayFrame so that we're back into
635 // layer-stack space.
636 const Rect frame = inverseTransform.transform(compositionState.displayFrame);
637 const FloatRect sourceCrop = compositionState.sourceCrop;
638
639 int32_t frameHeight = frame.getHeight();
640 int32_t frameWidth = frame.getWidth();
641 // If the display transform had a rotational component then undo the
642 // rotation so that the orientation matches the source crop.
643 if (displayTransform.getOrientation() & ui::Transform::ROT_90) {
644 std::swap(frameHeight, frameWidth);
645 }
646 return sourceCrop.getHeight() != frameHeight || sourceCrop.getWidth() != frameWidth;
647}
648
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700649uint64_t BufferLayer::getHeadFrameNumber(nsecs_t expectedPresentTime) const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800650 if (hasFrameUpdate()) {
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700651 return getFrameNumber(expectedPresentTime);
David Sodman0c69cad2017-08-21 12:12:51 -0700652 } else {
653 return mCurrentFrameNumber;
654 }
655}
656
Vishnu Nair60356342018-11-13 13:00:45 -0800657Rect BufferLayer::getBufferSize(const State& s) const {
658 // If we have a sideband stream, or we are scaling the buffer then return the layer size since
659 // we cannot determine the buffer size.
660 if ((s.sidebandStream != nullptr) ||
661 (getEffectiveScalingMode() != NATIVE_WINDOW_SCALING_MODE_FREEZE)) {
662 return Rect(getActiveWidth(s), getActiveHeight(s));
663 }
664
chaviwd62d3062019-09-04 14:48:02 -0700665 if (mBufferInfo.mBuffer == nullptr) {
Vishnu Nair60356342018-11-13 13:00:45 -0800666 return Rect::INVALID_RECT;
667 }
668
Alec Mouria90a5702021-04-16 16:36:21 +0000669 uint32_t bufWidth = mBufferInfo.mBuffer->getBuffer()->getWidth();
670 uint32_t bufHeight = mBufferInfo.mBuffer->getBuffer()->getHeight();
Vishnu Nair60356342018-11-13 13:00:45 -0800671
672 // Undo any transformations on the buffer and return the result.
chaviw4244e032019-09-04 11:27:49 -0700673 if (mBufferInfo.mTransform & ui::Transform::ROT_90) {
Vishnu Nair60356342018-11-13 13:00:45 -0800674 std::swap(bufWidth, bufHeight);
675 }
676
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800677 if (getTransformToDisplayInverse()) {
Dominik Laskowski718f9602019-11-09 20:01:35 -0800678 uint32_t invTransform = DisplayDevice::getPrimaryDisplayRotationFlags();
Vishnu Nair60356342018-11-13 13:00:45 -0800679 if (invTransform & ui::Transform::ROT_90) {
680 std::swap(bufWidth, bufHeight);
681 }
682 }
683
684 return Rect(bufWidth, bufHeight);
685}
686
Vishnu Nair4351ad52019-02-11 14:13:02 -0800687FloatRect BufferLayer::computeSourceBounds(const FloatRect& parentBounds) const {
688 const State& s(getDrawingState());
689
690 // If we have a sideband stream, or we are scaling the buffer then return the layer size since
691 // we cannot determine the buffer size.
692 if ((s.sidebandStream != nullptr) ||
693 (getEffectiveScalingMode() != NATIVE_WINDOW_SCALING_MODE_FREEZE)) {
694 return FloatRect(0, 0, getActiveWidth(s), getActiveHeight(s));
695 }
696
chaviwd62d3062019-09-04 14:48:02 -0700697 if (mBufferInfo.mBuffer == nullptr) {
Vishnu Nair4351ad52019-02-11 14:13:02 -0800698 return parentBounds;
699 }
700
Alec Mouria90a5702021-04-16 16:36:21 +0000701 uint32_t bufWidth = mBufferInfo.mBuffer->getBuffer()->getWidth();
702 uint32_t bufHeight = mBufferInfo.mBuffer->getBuffer()->getHeight();
Vishnu Nair4351ad52019-02-11 14:13:02 -0800703
704 // Undo any transformations on the buffer and return the result.
chaviw4244e032019-09-04 11:27:49 -0700705 if (mBufferInfo.mTransform & ui::Transform::ROT_90) {
Vishnu Nair4351ad52019-02-11 14:13:02 -0800706 std::swap(bufWidth, bufHeight);
707 }
708
709 if (getTransformToDisplayInverse()) {
Dominik Laskowski718f9602019-11-09 20:01:35 -0800710 uint32_t invTransform = DisplayDevice::getPrimaryDisplayRotationFlags();
Vishnu Nair4351ad52019-02-11 14:13:02 -0800711 if (invTransform & ui::Transform::ROT_90) {
712 std::swap(bufWidth, bufHeight);
713 }
714 }
715
716 return FloatRect(0, 0, bufWidth, bufHeight);
717}
718
chaviw49a108c2019-08-12 11:23:06 -0700719void BufferLayer::latchAndReleaseBuffer() {
720 mRefreshPending = false;
721 if (hasReadyFrame()) {
722 bool ignored = false;
723 latchBuffer(ignored, systemTime(), 0 /* expectedPresentTime */);
724 }
725 releasePendingBuffer(systemTime());
726}
727
chaviw4244e032019-09-04 11:27:49 -0700728PixelFormat BufferLayer::getPixelFormat() const {
729 return mBufferInfo.mPixelFormat;
730}
731
732bool BufferLayer::getTransformToDisplayInverse() const {
733 return mBufferInfo.mTransformToDisplayInverse;
734}
735
736Rect BufferLayer::getBufferCrop() const {
737 // this is the crop rectangle that applies to the buffer
738 // itself (as opposed to the window)
739 if (!mBufferInfo.mCrop.isEmpty()) {
740 // if the buffer crop is defined, we use that
741 return mBufferInfo.mCrop;
chaviwd62d3062019-09-04 14:48:02 -0700742 } else if (mBufferInfo.mBuffer != nullptr) {
chaviw4244e032019-09-04 11:27:49 -0700743 // otherwise we use the whole buffer
Alec Mouria90a5702021-04-16 16:36:21 +0000744 return mBufferInfo.mBuffer->getBuffer()->getBounds();
chaviw4244e032019-09-04 11:27:49 -0700745 } else {
746 // if we don't have a buffer yet, we use an empty/invalid crop
747 return Rect();
748 }
749}
750
751uint32_t BufferLayer::getBufferTransform() const {
752 return mBufferInfo.mTransform;
753}
754
755ui::Dataspace BufferLayer::getDataSpace() const {
756 return mBufferInfo.mDataspace;
757}
758
759ui::Dataspace BufferLayer::translateDataspace(ui::Dataspace dataspace) {
760 ui::Dataspace updatedDataspace = dataspace;
761 // translate legacy dataspaces to modern dataspaces
762 switch (dataspace) {
763 case ui::Dataspace::SRGB:
764 updatedDataspace = ui::Dataspace::V0_SRGB;
765 break;
766 case ui::Dataspace::SRGB_LINEAR:
767 updatedDataspace = ui::Dataspace::V0_SRGB_LINEAR;
768 break;
769 case ui::Dataspace::JFIF:
770 updatedDataspace = ui::Dataspace::V0_JFIF;
771 break;
772 case ui::Dataspace::BT601_625:
773 updatedDataspace = ui::Dataspace::V0_BT601_625;
774 break;
775 case ui::Dataspace::BT601_525:
776 updatedDataspace = ui::Dataspace::V0_BT601_525;
777 break;
778 case ui::Dataspace::BT709:
779 updatedDataspace = ui::Dataspace::V0_BT709;
780 break;
781 default:
782 break;
783 }
784
785 return updatedDataspace;
786}
787
chaviwd62d3062019-09-04 14:48:02 -0700788sp<GraphicBuffer> BufferLayer::getBuffer() const {
Alec Mouria90a5702021-04-16 16:36:21 +0000789 return mBufferInfo.mBuffer ? mBufferInfo.mBuffer->getBuffer() : nullptr;
chaviwd62d3062019-09-04 14:48:02 -0700790}
791
chaviwf83ce182019-09-12 14:43:08 -0700792void BufferLayer::getDrawingTransformMatrix(bool filteringEnabled, float outMatrix[16]) {
Alec Mouria90a5702021-04-16 16:36:21 +0000793 GLConsumer::computeTransformMatrix(outMatrix,
794 mBufferInfo.mBuffer ? mBufferInfo.mBuffer->getBuffer()
795 : nullptr,
796 mBufferInfo.mCrop, mBufferInfo.mTransform, filteringEnabled);
chaviwf83ce182019-09-12 14:43:08 -0700797}
798
chaviwb4c6e582019-08-16 14:35:07 -0700799void BufferLayer::setInitialValuesForClone(const sp<Layer>& clonedFrom) {
800 Layer::setInitialValuesForClone(clonedFrom);
801
802 sp<BufferLayer> bufferClonedFrom = static_cast<BufferLayer*>(clonedFrom.get());
803 mPremultipliedAlpha = bufferClonedFrom->mPremultipliedAlpha;
804 mPotentialCursor = bufferClonedFrom->mPotentialCursor;
805 mProtectedByApp = bufferClonedFrom->mProtectedByApp;
chaviw74b03172019-08-19 11:09:03 -0700806
807 updateCloneBufferInfo();
808}
809
810void BufferLayer::updateCloneBufferInfo() {
811 if (!isClone() || !isClonedFromAlive()) {
812 return;
813 }
814
815 sp<BufferLayer> clonedFrom = static_cast<BufferLayer*>(getClonedFrom().get());
816 mBufferInfo = clonedFrom->mBufferInfo;
817 mSidebandStream = clonedFrom->mSidebandStream;
818 surfaceDamageRegion = clonedFrom->surfaceDamageRegion;
819 mCurrentFrameNumber = clonedFrom->mCurrentFrameNumber.load();
820 mPreviousFrameNumber = clonedFrom->mPreviousFrameNumber;
821
822 // After buffer info is updated, the drawingState from the real layer needs to be copied into
823 // the cloned. This is because some properties of drawingState can change when latchBuffer is
chaviwaf87b3e2019-10-01 16:59:28 -0700824 // called. However, copying the drawingState would also overwrite the cloned layer's relatives
825 // and touchableRegionCrop. Therefore, temporarily store the relatives so they can be set in
826 // the cloned drawingState again.
chaviw74b03172019-08-19 11:09:03 -0700827 wp<Layer> tmpZOrderRelativeOf = mDrawingState.zOrderRelativeOf;
828 SortedVector<wp<Layer>> tmpZOrderRelatives = mDrawingState.zOrderRelatives;
chaviwaf87b3e2019-10-01 16:59:28 -0700829 wp<Layer> tmpTouchableRegionCrop = mDrawingState.touchableRegionCrop;
chaviw98318de2021-05-19 16:45:23 -0500830 WindowInfo tmpInputInfo = mDrawingState.inputInfo;
chaviwaf87b3e2019-10-01 16:59:28 -0700831
chaviw74b03172019-08-19 11:09:03 -0700832 mDrawingState = clonedFrom->mDrawingState;
chaviwaf87b3e2019-10-01 16:59:28 -0700833
834 mDrawingState.touchableRegionCrop = tmpTouchableRegionCrop;
chaviw74b03172019-08-19 11:09:03 -0700835 mDrawingState.zOrderRelativeOf = tmpZOrderRelativeOf;
836 mDrawingState.zOrderRelatives = tmpZOrderRelatives;
chaviwaf87b3e2019-10-01 16:59:28 -0700837 mDrawingState.inputInfo = tmpInputInfo;
chaviwb4c6e582019-08-16 14:35:07 -0700838}
839
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700840void BufferLayer::setTransformHint(ui::Transform::RotationFlags displayTransformHint) {
Vishnu Nair6213bd92020-05-08 17:42:25 -0700841 mTransformHint = getFixedTransformHint();
842 if (mTransformHint == ui::Transform::ROT_INVALID) {
843 mTransformHint = displayTransformHint;
844 }
845}
846
Vishnu Naire7f79c52020-10-29 14:45:03 -0700847bool BufferLayer::bufferNeedsFiltering() const {
848 return isFixedSize();
849}
850
David Sodman0c69cad2017-08-21 12:12:51 -0700851} // namespace android
852
853#if defined(__gl_h_)
854#error "don't include gl/gl.h in this file"
855#endif
856
857#if defined(__gl2_h_)
858#error "don't include gl2/gl2.h in this file"
859#endif
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800860
861// TODO(b/129481165): remove the #pragma below and fix conversion issues
862#pragma clang diagnostic pop // ignored "-Wconversion"