blob: 4c73b6edda3263e7249c3698205abcf99cb20631 [file] [log] [blame]
David Sodman0c69cad2017-08-21 12:12:51 -07001/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -080017// TODO(b/129481165): remove the #pragma below and fix conversion issues
18#pragma clang diagnostic push
19#pragma clang diagnostic ignored "-Wconversion"
20
David Sodman0c69cad2017-08-21 12:12:51 -070021//#define LOG_NDEBUG 0
22#undef LOG_TAG
23#define LOG_TAG "BufferLayer"
24#define ATRACE_TAG ATRACE_TAG_GRAPHICS
25
Alec Mourie60041e2019-06-14 18:59:51 -070026#include "BufferLayer.h"
Lloyd Piquefeb73d72018-12-04 17:23:44 -080027
28#include <compositionengine/CompositionEngine.h>
Lloyd Piquef5275482019-01-29 18:42:42 -080029#include <compositionengine/LayerFECompositionState.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080030#include <compositionengine/OutputLayer.h>
31#include <compositionengine/impl/OutputLayerCompositionState.h>
Lloyd Piquefeb73d72018-12-04 17:23:44 -080032#include <cutils/compiler.h>
33#include <cutils/native_handle.h>
34#include <cutils/properties.h>
35#include <gui/BufferItem.h>
36#include <gui/BufferQueue.h>
chaviwf83ce182019-09-12 14:43:08 -070037#include <gui/GLConsumer.h>
Lloyd Piquefeb73d72018-12-04 17:23:44 -080038#include <gui/LayerDebugInfo.h>
39#include <gui/Surface.h>
40#include <renderengine/RenderEngine.h>
41#include <ui/DebugUtils.h>
42#include <utils/Errors.h>
43#include <utils/Log.h>
44#include <utils/NativeHandle.h>
45#include <utils/StopWatch.h>
46#include <utils/Trace.h>
47
Alec Mourie60041e2019-06-14 18:59:51 -070048#include <cmath>
49#include <cstdlib>
50#include <mutex>
51#include <sstream>
52
David Sodman0c69cad2017-08-21 12:12:51 -070053#include "Colorizer.h"
54#include "DisplayDevice.h"
Mikael Pessa90092f42019-08-26 17:22:04 -070055#include "FrameTracer/FrameTracer.h"
David Sodman0c69cad2017-08-21 12:12:51 -070056#include "LayerRejecter.h"
Yiwei Zhang7e666a52018-11-15 13:33:42 -080057#include "TimeStats/TimeStats.h"
58
David Sodman0c69cad2017-08-21 12:12:51 -070059namespace android {
60
Peiyong Lin1a70eca2019-11-15 09:33:33 -080061static constexpr float defaultMaxMasteringLuminance = 1000.0;
62static constexpr float defaultMaxContentLuminance = 1000.0;
63
Lloyd Pique42ab75e2018-09-12 20:46:03 -070064BufferLayer::BufferLayer(const LayerCreationArgs& args)
Lloyd Piquefeb73d72018-12-04 17:23:44 -080065 : Layer(args),
chaviwb4c6e582019-08-16 14:35:07 -070066 mTextureName(args.textureName),
Lloyd Piquede196652020-01-22 17:29:58 -080067 mCompositionState{mFlinger->getCompositionEngine().createLayerFECompositionState()} {
Dominik Laskowski87a07e42019-10-10 20:38:02 -070068 ALOGV("Creating Layer %s", getDebugName());
David Sodman0c69cad2017-08-21 12:12:51 -070069
Lloyd Pique42ab75e2018-09-12 20:46:03 -070070 mPremultipliedAlpha = !(args.flags & ISurfaceComposerClient::eNonPremultiplied);
David Sodman0c69cad2017-08-21 12:12:51 -070071
Lloyd Pique42ab75e2018-09-12 20:46:03 -070072 mPotentialCursor = args.flags & ISurfaceComposerClient::eCursorWindow;
73 mProtectedByApp = args.flags & ISurfaceComposerClient::eProtectedByApp;
David Sodman0c69cad2017-08-21 12:12:51 -070074}
75
76BufferLayer::~BufferLayer() {
chaviwb4c6e582019-08-16 14:35:07 -070077 if (!isClone()) {
78 // The original layer and the clone layer share the same texture. Therefore, only one of
79 // the layers, in this case the original layer, needs to handle the deletion. The original
80 // layer and the clone should be removed at the same time so there shouldn't be any issue
81 // with the clone layer trying to use the deleted texture.
82 mFlinger->deleteTextureAsync(mTextureName);
83 }
Yiwei Zhang1a88c402019-11-18 10:43:58 -080084 const int32_t layerId = getSequence();
85 mFlinger->mTimeStats->onDestroy(layerId);
86 mFlinger->mFrameTracer->onDestroy(layerId);
David Sodman0c69cad2017-08-21 12:12:51 -070087}
88
David Sodmaneb085e02017-10-05 18:49:04 -070089void BufferLayer::useSurfaceDamage() {
90 if (mFlinger->mForceFullDamage) {
91 surfaceDamageRegion = Region::INVALID_REGION;
92 } else {
chaviw4244e032019-09-04 11:27:49 -070093 surfaceDamageRegion = mBufferInfo.mSurfaceDamage;
David Sodmaneb085e02017-10-05 18:49:04 -070094 }
95}
96
97void BufferLayer::useEmptyDamage() {
98 surfaceDamageRegion.clear();
99}
100
Marissa Wallfd668622018-05-10 10:21:13 -0700101bool BufferLayer::isOpaque(const Layer::State& s) const {
102 // if we don't have a buffer or sidebandStream yet, we're translucent regardless of the
103 // layer's opaque flag.
chaviwd62d3062019-09-04 14:48:02 -0700104 if ((mSidebandStream == nullptr) && (mBufferInfo.mBuffer == nullptr)) {
Marissa Wallfd668622018-05-10 10:21:13 -0700105 return false;
106 }
107
108 // if the layer has the opaque flag, then we're always opaque,
109 // otherwise we use the current buffer's format.
110 return ((s.flags & layer_state_t::eLayerOpaque) != 0) || getOpacityForFormat(getPixelFormat());
David Sodman0c69cad2017-08-21 12:12:51 -0700111}
112
113bool BufferLayer::isVisible() const {
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -0700114 return !isHiddenByPolicy() && getAlpha() > 0.0f &&
chaviwd62d3062019-09-04 14:48:02 -0700115 (mBufferInfo.mBuffer != nullptr || mSidebandStream != nullptr);
David Sodman0c69cad2017-08-21 12:12:51 -0700116}
117
118bool BufferLayer::isFixedSize() const {
119 return getEffectiveScalingMode() != NATIVE_WINDOW_SCALING_MODE_FREEZE;
120}
121
Lloyd Piquea83776c2019-01-29 18:42:32 -0800122bool BufferLayer::usesSourceCrop() const {
123 return true;
124}
125
David Sodman0c69cad2017-08-21 12:12:51 -0700126static constexpr mat4 inverseOrientation(uint32_t transform) {
David Sodman41fdfc92017-11-06 16:09:56 -0800127 const mat4 flipH(-1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1);
128 const mat4 flipV(1, 0, 0, 0, 0, -1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1);
129 const mat4 rot90(0, 1, 0, 0, -1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1);
David Sodman0c69cad2017-08-21 12:12:51 -0700130 mat4 tr;
131
132 if (transform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
133 tr = tr * rot90;
134 }
135 if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_H) {
136 tr = tr * flipH;
137 }
138 if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_V) {
139 tr = tr * flipV;
140 }
141 return inverse(tr);
142}
143
Vishnu Nair9b079a22020-01-21 14:36:08 -0800144std::optional<compositionengine::LayerFE::LayerSettings> BufferLayer::prepareClientComposition(
Lloyd Piquef16688f2019-02-19 17:47:57 -0800145 compositionengine::LayerFE::ClientCompositionTargetSettings& targetSettings) {
David Sodman0c69cad2017-08-21 12:12:51 -0700146 ATRACE_CALL();
Lloyd Piquef16688f2019-02-19 17:47:57 -0800147
Vishnu Nair9b079a22020-01-21 14:36:08 -0800148 std::optional<compositionengine::LayerFE::LayerSettings> result =
149 Layer::prepareClientComposition(targetSettings);
Lloyd Piquef16688f2019-02-19 17:47:57 -0800150 if (!result) {
151 return result;
152 }
153
chaviwd62d3062019-09-04 14:48:02 -0700154 if (CC_UNLIKELY(mBufferInfo.mBuffer == 0)) {
David Sodman0c69cad2017-08-21 12:12:51 -0700155 // the texture has not been created yet, this Layer has
156 // in fact never been drawn into. This happens frequently with
157 // SurfaceView because the WindowManager can't know when the client
158 // has drawn the first time.
159
160 // If there is nothing under us, we paint the screen in black, otherwise
161 // we just skip this update.
162
163 // figure out if there is something below us
164 Region under;
165 bool finished = false;
166 mFlinger->mDrawingState.traverseInZOrder([&](Layer* layer) {
167 if (finished || layer == static_cast<BufferLayer const*>(this)) {
168 finished = true;
169 return;
170 }
Lloyd Piquea2468662019-03-07 21:31:06 -0800171
172 under.orSelf(layer->getScreenBounds());
David Sodman0c69cad2017-08-21 12:12:51 -0700173 });
174 // if not everything below us is covered, we plug the holes!
Lloyd Piquef16688f2019-02-19 17:47:57 -0800175 Region holes(targetSettings.clip.subtract(under));
David Sodman0c69cad2017-08-21 12:12:51 -0700176 if (!holes.isEmpty()) {
Lloyd Piquef16688f2019-02-19 17:47:57 -0800177 targetSettings.clearRegion.orSelf(holes);
David Sodman0c69cad2017-08-21 12:12:51 -0700178 }
Tianhua Sundcff00c2020-12-29 07:20:55 -0500179
180 if (mSidebandStream != nullptr) {
181 // For surfaceview of tv sideband, there is no activeBuffer
182 // in bufferqueue, we need return LayerSettings.
183 return result;
184 } else {
185 return std::nullopt;
186 }
David Sodman0c69cad2017-08-21 12:12:51 -0700187 }
Ady Abraham7a60afb2021-03-29 13:20:55 -0700188 const bool blackOutLayer = (isProtected() && !targetSettings.supportsProtectedContent) ||
yuhui.zhanga102ff92021-04-09 14:45:51 +0800189 ((isSecure() || isProtected()) && !targetSettings.isSecure);
Ady Abraham7a60afb2021-03-29 13:20:55 -0700190 const bool bufferCanBeUsedAsHwTexture =
Alec Mouria90a5702021-04-16 16:36:21 +0000191 mBufferInfo.mBuffer->getBuffer()->getUsage() & GraphicBuffer::USAGE_HW_TEXTURE;
Lloyd Piquede196652020-01-22 17:29:58 -0800192 compositionengine::LayerFE::LayerSettings& layer = *result;
Ady Abraham7a60afb2021-03-29 13:20:55 -0700193 if (blackOutLayer || !bufferCanBeUsedAsHwTexture) {
194 ALOGE_IF(!bufferCanBeUsedAsHwTexture, "%s is blacked out as buffer is not gpu readable",
195 mName.c_str());
Vishnu Nairb87d94f2020-02-13 09:17:36 -0800196 prepareClearClientComposition(layer, true /* blackout */);
197 return layer;
David Sodman0c69cad2017-08-21 12:12:51 -0700198 }
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000199
Vishnu Nairb87d94f2020-02-13 09:17:36 -0800200 const State& s(getDrawingState());
201 layer.source.buffer.buffer = mBufferInfo.mBuffer;
202 layer.source.buffer.isOpaque = isOpaque(s);
203 layer.source.buffer.fence = mBufferInfo.mFence;
204 layer.source.buffer.textureName = mTextureName;
205 layer.source.buffer.usePremultipliedAlpha = getPremultipledAlpha();
206 layer.source.buffer.isY410BT2020 = isHdrY410();
207 bool hasSmpte2086 = mBufferInfo.mHdrMetadata.validTypes & HdrMetadata::SMPTE2086;
208 bool hasCta861_3 = mBufferInfo.mHdrMetadata.validTypes & HdrMetadata::CTA861_3;
209 layer.source.buffer.maxMasteringLuminance = hasSmpte2086
210 ? mBufferInfo.mHdrMetadata.smpte2086.maxLuminance
211 : defaultMaxMasteringLuminance;
212 layer.source.buffer.maxContentLuminance = hasCta861_3
213 ? mBufferInfo.mHdrMetadata.cta8613.maxContentLightLevel
214 : defaultMaxContentLuminance;
215 layer.frameNumber = mCurrentFrameNumber;
Alec Mouria90a5702021-04-16 16:36:21 +0000216 layer.bufferId = mBufferInfo.mBuffer ? mBufferInfo.mBuffer->getBuffer()->getId() : 0;
Vishnu Nairb87d94f2020-02-13 09:17:36 -0800217
Vishnu Naire7f79c52020-10-29 14:45:03 -0700218 const bool useFiltering =
219 targetSettings.needsFiltering || mNeedsFiltering || bufferNeedsFiltering();
Vishnu Nairb87d94f2020-02-13 09:17:36 -0800220
221 // Query the texture matrix given our current filtering mode.
222 float textureMatrix[16];
223 getDrawingTransformMatrix(useFiltering, textureMatrix);
224
225 if (getTransformToDisplayInverse()) {
226 /*
227 * the code below applies the primary display's inverse transform to
228 * the texture transform
229 */
230 uint32_t transform = DisplayDevice::getPrimaryDisplayRotationFlags();
231 mat4 tr = inverseOrientation(transform);
232
233 /**
234 * TODO(b/36727915): This is basically a hack.
235 *
236 * Ensure that regardless of the parent transformation,
237 * this buffer is always transformed from native display
238 * orientation to display orientation. For example, in the case
239 * of a camera where the buffer remains in native orientation,
240 * we want the pixels to always be upright.
241 */
242 sp<Layer> p = mDrawingParent.promote();
243 if (p != nullptr) {
244 const auto parentTransform = p->getTransform();
245 tr = tr * inverseOrientation(parentTransform.getOrientation());
246 }
247
248 // and finally apply it to the original texture matrix
249 const mat4 texTransform(mat4(static_cast<const float*>(textureMatrix)) * tr);
250 memcpy(textureMatrix, texTransform.asArray(), sizeof(textureMatrix));
251 }
252
253 const Rect win{getBounds()};
254 float bufferWidth = getBufferSize(s).getWidth();
255 float bufferHeight = getBufferSize(s).getHeight();
256
257 // BufferStateLayers can have a "buffer size" of [0, 0, -1, -1] when no display frame has
258 // been set and there is no parent layer bounds. In that case, the scale is meaningless so
259 // ignore them.
260 if (!getBufferSize(s).isValid()) {
261 bufferWidth = float(win.right) - float(win.left);
262 bufferHeight = float(win.bottom) - float(win.top);
263 }
264
265 const float scaleHeight = (float(win.bottom) - float(win.top)) / bufferHeight;
266 const float scaleWidth = (float(win.right) - float(win.left)) / bufferWidth;
267 const float translateY = float(win.top) / bufferHeight;
268 const float translateX = float(win.left) / bufferWidth;
269
270 // Flip y-coordinates because GLConsumer expects OpenGL convention.
271 mat4 tr = mat4::translate(vec4(.5, .5, 0, 1)) * mat4::scale(vec4(1, -1, 1, 1)) *
272 mat4::translate(vec4(-.5, -.5, 0, 1)) *
273 mat4::translate(vec4(translateX, translateY, 0, 1)) *
274 mat4::scale(vec4(scaleWidth, scaleHeight, 1.0, 1.0));
275
276 layer.source.buffer.useTextureFiltering = useFiltering;
277 layer.source.buffer.textureTransform = mat4(static_cast<const float*>(textureMatrix)) * tr;
278
Vishnu Nair9b079a22020-01-21 14:36:08 -0800279 return layer;
David Sodman0c69cad2017-08-21 12:12:51 -0700280}
281
Marissa Wallfd668622018-05-10 10:21:13 -0700282bool BufferLayer::isHdrY410() const {
283 // pixel format is HDR Y410 masquerading as RGBA_1010102
chaviw4244e032019-09-04 11:27:49 -0700284 return (mBufferInfo.mDataspace == ui::Dataspace::BT2020_ITU_PQ &&
285 mBufferInfo.mApi == NATIVE_WINDOW_API_MEDIA &&
chaviwdebadb82020-03-26 14:57:24 -0700286 mBufferInfo.mPixelFormat == HAL_PIXEL_FORMAT_RGBA_1010102);
David Sodmaneb085e02017-10-05 18:49:04 -0700287}
288
Lloyd Piquede196652020-01-22 17:29:58 -0800289sp<compositionengine::LayerFE> BufferLayer::getCompositionEngineLayerFE() const {
290 return asLayerFE();
291}
292
293compositionengine::LayerFECompositionState* BufferLayer::editCompositionState() {
294 return mCompositionState.get();
295}
296
297const compositionengine::LayerFECompositionState* BufferLayer::getCompositionState() const {
298 return mCompositionState.get();
299}
300
301void BufferLayer::preparePerFrameCompositionState() {
302 Layer::preparePerFrameCompositionState();
David Sodman0c69cad2017-08-21 12:12:51 -0700303
304 // Sideband layers
Lloyd Piquede196652020-01-22 17:29:58 -0800305 auto* compositionState = editCompositionState();
306 if (compositionState->sidebandStream.get()) {
307 compositionState->compositionType = Hwc2::IComposerClient::Composition::SIDEBAND;
Robert Carra6bb2bc2020-04-08 11:03:23 -0700308 return;
David Sodman15094112018-10-11 09:39:37 -0700309 } else {
Lloyd Piquef5275482019-01-29 18:42:42 -0800310 // Normal buffer layers
Lloyd Piquede196652020-01-22 17:29:58 -0800311 compositionState->hdrMetadata = mBufferInfo.mHdrMetadata;
312 compositionState->compositionType = mPotentialCursor
Lloyd Piquef5275482019-01-29 18:42:42 -0800313 ? Hwc2::IComposerClient::Composition::CURSOR
314 : Hwc2::IComposerClient::Composition::DEVICE;
David Sodman0c69cad2017-08-21 12:12:51 -0700315 }
Robert Carra6bb2bc2020-04-08 11:03:23 -0700316
Alec Mouria90a5702021-04-16 16:36:21 +0000317 compositionState->buffer = mBufferInfo.mBuffer->getBuffer();
Robert Carra6bb2bc2020-04-08 11:03:23 -0700318 compositionState->bufferSlot = (mBufferInfo.mBufferSlot == BufferQueue::INVALID_BUFFER_SLOT)
319 ? 0
320 : mBufferInfo.mBufferSlot;
321 compositionState->acquireFence = mBufferInfo.mFence;
David Sodman0c69cad2017-08-21 12:12:51 -0700322}
323
Marissa Wallfd668622018-05-10 10:21:13 -0700324bool BufferLayer::onPreComposition(nsecs_t refreshStartTime) {
chaviwd62d3062019-09-04 14:48:02 -0700325 if (mBufferInfo.mBuffer != nullptr) {
Marissa Wallfd668622018-05-10 10:21:13 -0700326 Mutex::Autolock lock(mFrameEventHistoryMutex);
327 mFrameEventHistory.addPreComposition(mCurrentFrameNumber, refreshStartTime);
David Sodman0c69cad2017-08-21 12:12:51 -0700328 }
Marissa Wallfd668622018-05-10 10:21:13 -0700329 mRefreshPending = false;
330 return hasReadyFrame();
David Sodman0c69cad2017-08-21 12:12:51 -0700331}
Ady Abraham8b9e6122021-01-26 19:11:45 -0800332namespace {
333TimeStats::SetFrameRateVote frameRateToSetFrameRateVotePayload(Layer::FrameRate frameRate) {
334 using FrameRateCompatibility = TimeStats::SetFrameRateVote::FrameRateCompatibility;
335 using Seamlessness = TimeStats::SetFrameRateVote::Seamlessness;
336 const auto frameRateCompatibility = [frameRate] {
337 switch (frameRate.type) {
338 case Layer::FrameRateCompatibility::Default:
339 return FrameRateCompatibility::Default;
340 case Layer::FrameRateCompatibility::ExactOrMultiple:
341 return FrameRateCompatibility::ExactOrMultiple;
342 default:
343 return FrameRateCompatibility::Undefined;
344 }
345 }();
346
347 const auto seamlessness = [frameRate] {
348 switch (frameRate.seamlessness) {
349 case scheduler::Seamlessness::OnlySeamless:
350 return Seamlessness::ShouldBeSeamless;
351 case scheduler::Seamlessness::SeamedAndSeamless:
352 return Seamlessness::NotRequired;
353 default:
354 return Seamlessness::Undefined;
355 }
356 }();
357
358 return TimeStats::SetFrameRateVote{.frameRate = frameRate.rate.getValue(),
359 .frameRateCompatibility = frameRateCompatibility,
360 .seamlessness = seamlessness};
361}
362} // namespace
David Sodman0c69cad2017-08-21 12:12:51 -0700363
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700364bool BufferLayer::onPostComposition(const DisplayDevice* display,
Dominik Laskowski075d3172018-05-24 15:50:06 -0700365 const std::shared_ptr<FenceTime>& glDoneFence,
Marissa Wallfd668622018-05-10 10:21:13 -0700366 const std::shared_ptr<FenceTime>& presentFence,
367 const CompositorTiming& compositorTiming) {
368 // mFrameLatencyNeeded is true when a new frame was latched for the
369 // composition.
chaviw74b03172019-08-19 11:09:03 -0700370 if (!mBufferInfo.mFrameLatencyNeeded) return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700371
372 // Update mFrameEventHistory.
Dan Stoza436ccf32018-06-21 12:10:12 -0700373 {
Marissa Wallfd668622018-05-10 10:21:13 -0700374 Mutex::Autolock lock(mFrameEventHistoryMutex);
375 mFrameEventHistory.addPostComposition(mCurrentFrameNumber, glDoneFence, presentFence,
376 compositorTiming);
Valerie Hau93d5a5e2020-02-13 14:50:01 -0800377 finalizeFrameEventHistory(glDoneFence, compositorTiming);
David Sodman0c69cad2017-08-21 12:12:51 -0700378 }
379
Marissa Wallfd668622018-05-10 10:21:13 -0700380 // Update mFrameTracker.
chaviw4244e032019-09-04 11:27:49 -0700381 nsecs_t desiredPresentTime = mBufferInfo.mDesiredPresentTime;
Marissa Wallfd668622018-05-10 10:21:13 -0700382 mFrameTracker.setDesiredPresentTime(desiredPresentTime);
383
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800384 const int32_t layerId = getSequence();
385 mFlinger->mTimeStats->setDesiredTime(layerId, mCurrentFrameNumber, desiredPresentTime);
Marissa Wallfd668622018-05-10 10:21:13 -0700386
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700387 const auto outputLayer = findOutputLayerForDisplay(display);
Adithya Srinivasanb69e0762019-11-11 18:39:53 -0800388 if (outputLayer && outputLayer->requiresClientComposition()) {
389 nsecs_t clientCompositionTimestamp = outputLayer->getState().clientCompositionTimestamp;
390 mFlinger->mFrameTracer->traceTimestamp(layerId, getCurrentBufferId(), mCurrentFrameNumber,
391 clientCompositionTimestamp,
392 FrameTracer::FrameEvent::FALLBACK_COMPOSITION);
Adithya Srinivasanb6a2fa12021-03-13 00:23:09 +0000393 // Update the SurfaceFrames in the drawing state
394 if (mDrawingState.bufferSurfaceFrameTX) {
395 mDrawingState.bufferSurfaceFrameTX->setGpuComposition();
396 }
397 for (auto& [token, surfaceFrame] : mDrawingState.bufferlessSurfaceFramesTX) {
398 surfaceFrame->setGpuComposition();
399 }
Adithya Srinivasanb69e0762019-11-11 18:39:53 -0800400 }
401
chaviw4244e032019-09-04 11:27:49 -0700402 std::shared_ptr<FenceTime> frameReadyFence = mBufferInfo.mFenceTime;
Marissa Wallfd668622018-05-10 10:21:13 -0700403 if (frameReadyFence->isValid()) {
404 mFrameTracker.setFrameReadyFence(std::move(frameReadyFence));
405 } else {
406 // There was no fence for this frame, so assume that it was ready
407 // to be presented at the desired present time.
408 mFrameTracker.setFrameReadyTime(desiredPresentTime);
Dominik Laskowski45de9bd2018-06-11 17:44:10 -0700409 }
Marissa Wallfd668622018-05-10 10:21:13 -0700410
Alec Mouri7d436ec2021-01-27 20:40:50 -0800411 const Fps refreshRate = mFlinger->mRefreshRateConfigs->getCurrentRefreshRate().getFps();
412 const std::optional<Fps> renderRate = mFlinger->mScheduler->getFrameRateOverride(getOwnerUid());
Marissa Wallfd668622018-05-10 10:21:13 -0700413 if (presentFence->isValid()) {
Alec Mouri7d436ec2021-01-27 20:40:50 -0800414 mFlinger->mTimeStats->setPresentFence(layerId, mCurrentFrameNumber, presentFence,
Ady Abraham8b9e6122021-01-26 19:11:45 -0800415 refreshRate, renderRate,
416 frameRateToSetFrameRateVotePayload(
417 mDrawingState.frameRate));
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800418 mFlinger->mFrameTracer->traceFence(layerId, getCurrentBufferId(), mCurrentFrameNumber,
Mikael Pessa90092f42019-08-26 17:22:04 -0700419 presentFence, FrameTracer::FrameEvent::PRESENT_FENCE);
Marissa Wallfd668622018-05-10 10:21:13 -0700420 mFrameTracker.setActualPresentFence(std::shared_ptr<FenceTime>(presentFence));
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700421 } else if (!display) {
422 // Do nothing.
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200423 } else if (const auto displayId = PhysicalDisplayId::tryCast(display->getId());
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700424 displayId && mFlinger->getHwComposer().isConnected(*displayId)) {
Marissa Wallfd668622018-05-10 10:21:13 -0700425 // The HWC doesn't support present fences, so use the refresh
426 // timestamp instead.
Marin Shalamanov045b7002021-01-07 16:56:24 +0100427 const nsecs_t actualPresentTime = display->getRefreshTimestamp();
Alec Mouri7d436ec2021-01-27 20:40:50 -0800428 mFlinger->mTimeStats->setPresentTime(layerId, mCurrentFrameNumber, actualPresentTime,
Ady Abraham8b9e6122021-01-26 19:11:45 -0800429 refreshRate, renderRate,
430 frameRateToSetFrameRateVotePayload(
431 mDrawingState.frameRate));
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800432 mFlinger->mFrameTracer->traceTimestamp(layerId, getCurrentBufferId(), mCurrentFrameNumber,
Mikael Pessa90092f42019-08-26 17:22:04 -0700433 actualPresentTime,
434 FrameTracer::FrameEvent::PRESENT_FENCE);
Marissa Wallfd668622018-05-10 10:21:13 -0700435 mFrameTracker.setActualPresentTime(actualPresentTime);
436 }
437
438 mFrameTracker.advanceFrame();
chaviw74b03172019-08-19 11:09:03 -0700439 mBufferInfo.mFrameLatencyNeeded = false;
Marissa Wallfd668622018-05-10 10:21:13 -0700440 return true;
David Sodman0c69cad2017-08-21 12:12:51 -0700441}
442
chaviwdebadb82020-03-26 14:57:24 -0700443void BufferLayer::gatherBufferInfo() {
444 mBufferInfo.mPixelFormat =
Alec Mouria90a5702021-04-16 16:36:21 +0000445 !mBufferInfo.mBuffer ? PIXEL_FORMAT_NONE : mBufferInfo.mBuffer->getBuffer()->format;
chaviwdebadb82020-03-26 14:57:24 -0700446 mBufferInfo.mFrameLatencyNeeded = true;
447}
448
Ady Abraham63a3e592021-01-06 10:47:15 -0800449bool BufferLayer::shouldPresentNow(nsecs_t expectedPresentTime) const {
450 // If this is not a valid vsync for the layer's uid, return and try again later
451 const bool isVsyncValidForUid =
452 mFlinger->mScheduler->isVsyncValid(expectedPresentTime, mOwnerUid);
453 if (!isVsyncValidForUid) {
454 ATRACE_NAME("!isVsyncValidForUid");
455 return false;
456 }
457
458 // AutoRefresh layers and sideband streams should always be presented
459 if (getSidebandStreamChanged() || getAutoRefresh()) {
460 return true;
461 }
462
Ady Abraham63a3e592021-01-06 10:47:15 -0800463 // If this layer doesn't have a frame is shouldn't be presented
464 if (!hasFrameUpdate()) {
465 return false;
466 }
467
468 // Defer to the derived class to decide whether the next buffer is due for
469 // presentation.
470 return isBufferDue(expectedPresentTime);
471}
472
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700473bool BufferLayer::latchBuffer(bool& recomputeVisibleRegions, nsecs_t latchTime,
474 nsecs_t expectedPresentTime) {
Marissa Wallfd668622018-05-10 10:21:13 -0700475 ATRACE_CALL();
David Sodman0c69cad2017-08-21 12:12:51 -0700476
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800477 bool refreshRequired = latchSidebandStream(recomputeVisibleRegions);
David Sodman0c69cad2017-08-21 12:12:51 -0700478
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800479 if (refreshRequired) {
480 return refreshRequired;
David Sodman0c69cad2017-08-21 12:12:51 -0700481 }
482
Marissa Wallfd668622018-05-10 10:21:13 -0700483 if (!hasReadyFrame()) {
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800484 return false;
David Sodman0c69cad2017-08-21 12:12:51 -0700485 }
David Sodman0c69cad2017-08-21 12:12:51 -0700486
Marissa Wallfd668622018-05-10 10:21:13 -0700487 // if we've already called updateTexImage() without going through
488 // a composition step, we have to skip this layer at this point
489 // because we cannot call updateTeximage() without a corresponding
490 // compositionComplete() call.
491 // we'll trigger an update in onPreComposition().
492 if (mRefreshPending) {
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800493 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700494 }
495
496 // If the head buffer's acquire fence hasn't signaled yet, return and
497 // try again later
498 if (!fenceHasSignaled()) {
Ady Abraham09bd3922019-04-08 10:44:56 -0700499 ATRACE_NAME("!fenceHasSignaled()");
David Sodman0c69cad2017-08-21 12:12:51 -0700500 mFlinger->signalLayerUpdate();
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800501 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700502 }
503
504 // Capture the old state of the layer for comparisons later
505 const State& s(getDrawingState());
506 const bool oldOpacity = isOpaque(s);
chaviwd62d3062019-09-04 14:48:02 -0700507
508 BufferInfo oldBufferInfo = mBufferInfo;
Marissa Wallfd668622018-05-10 10:21:13 -0700509
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700510 status_t err = updateTexImage(recomputeVisibleRegions, latchTime, expectedPresentTime);
Marissa Wallfd668622018-05-10 10:21:13 -0700511 if (err != NO_ERROR) {
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800512 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700513 }
514
515 err = updateActiveBuffer();
516 if (err != NO_ERROR) {
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800517 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700518 }
519
Marissa Wallfd668622018-05-10 10:21:13 -0700520 err = updateFrameNumber(latchTime);
521 if (err != NO_ERROR) {
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800522 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700523 }
524
chaviw4244e032019-09-04 11:27:49 -0700525 gatherBufferInfo();
526
Marissa Wallfd668622018-05-10 10:21:13 -0700527 mRefreshPending = true;
chaviwd62d3062019-09-04 14:48:02 -0700528 if (oldBufferInfo.mBuffer == nullptr) {
Marissa Wallfd668622018-05-10 10:21:13 -0700529 // the first time we receive a buffer, we need to trigger a
530 // geometry invalidation.
531 recomputeVisibleRegions = true;
532 }
533
chaviw4244e032019-09-04 11:27:49 -0700534 if ((mBufferInfo.mCrop != oldBufferInfo.mCrop) ||
535 (mBufferInfo.mTransform != oldBufferInfo.mTransform) ||
536 (mBufferInfo.mScaleMode != oldBufferInfo.mScaleMode) ||
537 (mBufferInfo.mTransformToDisplayInverse != oldBufferInfo.mTransformToDisplayInverse)) {
Marissa Wallfd668622018-05-10 10:21:13 -0700538 recomputeVisibleRegions = true;
539 }
540
chaviwd62d3062019-09-04 14:48:02 -0700541 if (oldBufferInfo.mBuffer != nullptr) {
Alec Mouria90a5702021-04-16 16:36:21 +0000542 uint32_t bufWidth = mBufferInfo.mBuffer->getBuffer()->getWidth();
543 uint32_t bufHeight = mBufferInfo.mBuffer->getBuffer()->getHeight();
544 if (bufWidth != uint32_t(oldBufferInfo.mBuffer->getBuffer()->width) ||
545 bufHeight != uint32_t(oldBufferInfo.mBuffer->getBuffer()->height)) {
Marissa Wallfd668622018-05-10 10:21:13 -0700546 recomputeVisibleRegions = true;
547 }
548 }
549
550 if (oldOpacity != isOpaque(s)) {
551 recomputeVisibleRegions = true;
552 }
553
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800554 return true;
Marissa Wallfd668622018-05-10 10:21:13 -0700555}
556
Marissa Wallfd668622018-05-10 10:21:13 -0700557bool BufferLayer::hasReadyFrame() const {
Marissa Wall024a1912018-08-13 13:55:35 -0700558 return hasFrameUpdate() || getSidebandStreamChanged() || getAutoRefresh();
Marissa Wallfd668622018-05-10 10:21:13 -0700559}
560
561uint32_t BufferLayer::getEffectiveScalingMode() const {
chaviw4244e032019-09-04 11:27:49 -0700562 return mBufferInfo.mScaleMode;
Marissa Wallfd668622018-05-10 10:21:13 -0700563}
564
565bool BufferLayer::isProtected() const {
Alec Mouria90a5702021-04-16 16:36:21 +0000566 return (mBufferInfo.mBuffer != nullptr) &&
567 (mBufferInfo.mBuffer->getBuffer()->getUsage() & GRALLOC_USAGE_PROTECTED);
Marissa Wallfd668622018-05-10 10:21:13 -0700568}
569
David Sodman0c69cad2017-08-21 12:12:51 -0700570// As documented in libhardware header, formats in the range
571// 0x100 - 0x1FF are specific to the HAL implementation, and
572// are known to have no alpha channel
573// TODO: move definition for device-specific range into
574// hardware.h, instead of using hard-coded values here.
575#define HARDWARE_IS_DEVICE_FORMAT(f) ((f) >= 0x100 && (f) <= 0x1FF)
576
577bool BufferLayer::getOpacityForFormat(uint32_t format) {
578 if (HARDWARE_IS_DEVICE_FORMAT(format)) {
579 return true;
580 }
581 switch (format) {
582 case HAL_PIXEL_FORMAT_RGBA_8888:
583 case HAL_PIXEL_FORMAT_BGRA_8888:
584 case HAL_PIXEL_FORMAT_RGBA_FP16:
585 case HAL_PIXEL_FORMAT_RGBA_1010102:
586 return false;
587 }
588 // in all other case, we have no blending (also for unknown formats)
589 return true;
590}
591
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700592bool BufferLayer::needsFiltering(const DisplayDevice* display) const {
593 const auto outputLayer = findOutputLayerForDisplay(display);
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800594 if (outputLayer == nullptr) {
Lloyd Piquef16688f2019-02-19 17:47:57 -0800595 return false;
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800596 }
597
Lloyd Piquef16688f2019-02-19 17:47:57 -0800598 // We need filtering if the sourceCrop rectangle size does not match the
599 // displayframe rectangle size (not a 1:1 render)
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800600 const auto& compositionState = outputLayer->getState();
601 const auto displayFrame = compositionState.displayFrame;
602 const auto sourceCrop = compositionState.sourceCrop;
Lloyd Piquef16688f2019-02-19 17:47:57 -0800603 return sourceCrop.getHeight() != displayFrame.getHeight() ||
Peiyong Linc2020ca2019-01-10 11:36:12 -0800604 sourceCrop.getWidth() != displayFrame.getWidth();
Chia-I Wu692e0832018-06-05 15:46:58 -0700605}
606
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700607bool BufferLayer::needsFilteringForScreenshots(const DisplayDevice* display,
Alec Mouri5a6d8572020-03-23 23:56:15 -0700608 const ui::Transform& inverseParentTransform) const {
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700609 const auto outputLayer = findOutputLayerForDisplay(display);
Alec Mouri5a6d8572020-03-23 23:56:15 -0700610 if (outputLayer == nullptr) {
611 return false;
612 }
613
614 // We need filtering if the sourceCrop rectangle size does not match the
615 // viewport rectangle size (not a 1:1 render)
616 const auto& compositionState = outputLayer->getState();
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700617 const ui::Transform& displayTransform = display->getTransform();
Alec Mouri5a6d8572020-03-23 23:56:15 -0700618 const ui::Transform inverseTransform = inverseParentTransform * displayTransform.inverse();
619 // Undo the transformation of the displayFrame so that we're back into
620 // layer-stack space.
621 const Rect frame = inverseTransform.transform(compositionState.displayFrame);
622 const FloatRect sourceCrop = compositionState.sourceCrop;
623
624 int32_t frameHeight = frame.getHeight();
625 int32_t frameWidth = frame.getWidth();
626 // If the display transform had a rotational component then undo the
627 // rotation so that the orientation matches the source crop.
628 if (displayTransform.getOrientation() & ui::Transform::ROT_90) {
629 std::swap(frameHeight, frameWidth);
630 }
631 return sourceCrop.getHeight() != frameHeight || sourceCrop.getWidth() != frameWidth;
632}
633
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700634uint64_t BufferLayer::getHeadFrameNumber(nsecs_t expectedPresentTime) const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800635 if (hasFrameUpdate()) {
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700636 return getFrameNumber(expectedPresentTime);
David Sodman0c69cad2017-08-21 12:12:51 -0700637 } else {
638 return mCurrentFrameNumber;
639 }
640}
641
Vishnu Nair60356342018-11-13 13:00:45 -0800642Rect BufferLayer::getBufferSize(const State& s) const {
643 // If we have a sideband stream, or we are scaling the buffer then return the layer size since
644 // we cannot determine the buffer size.
645 if ((s.sidebandStream != nullptr) ||
646 (getEffectiveScalingMode() != NATIVE_WINDOW_SCALING_MODE_FREEZE)) {
647 return Rect(getActiveWidth(s), getActiveHeight(s));
648 }
649
chaviwd62d3062019-09-04 14:48:02 -0700650 if (mBufferInfo.mBuffer == nullptr) {
Vishnu Nair60356342018-11-13 13:00:45 -0800651 return Rect::INVALID_RECT;
652 }
653
Alec Mouria90a5702021-04-16 16:36:21 +0000654 uint32_t bufWidth = mBufferInfo.mBuffer->getBuffer()->getWidth();
655 uint32_t bufHeight = mBufferInfo.mBuffer->getBuffer()->getHeight();
Vishnu Nair60356342018-11-13 13:00:45 -0800656
657 // Undo any transformations on the buffer and return the result.
chaviw4244e032019-09-04 11:27:49 -0700658 if (mBufferInfo.mTransform & ui::Transform::ROT_90) {
Vishnu Nair60356342018-11-13 13:00:45 -0800659 std::swap(bufWidth, bufHeight);
660 }
661
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800662 if (getTransformToDisplayInverse()) {
Dominik Laskowski718f9602019-11-09 20:01:35 -0800663 uint32_t invTransform = DisplayDevice::getPrimaryDisplayRotationFlags();
Vishnu Nair60356342018-11-13 13:00:45 -0800664 if (invTransform & ui::Transform::ROT_90) {
665 std::swap(bufWidth, bufHeight);
666 }
667 }
668
669 return Rect(bufWidth, bufHeight);
670}
671
Vishnu Nair4351ad52019-02-11 14:13:02 -0800672FloatRect BufferLayer::computeSourceBounds(const FloatRect& parentBounds) const {
673 const State& s(getDrawingState());
674
675 // If we have a sideband stream, or we are scaling the buffer then return the layer size since
676 // we cannot determine the buffer size.
677 if ((s.sidebandStream != nullptr) ||
678 (getEffectiveScalingMode() != NATIVE_WINDOW_SCALING_MODE_FREEZE)) {
679 return FloatRect(0, 0, getActiveWidth(s), getActiveHeight(s));
680 }
681
chaviwd62d3062019-09-04 14:48:02 -0700682 if (mBufferInfo.mBuffer == nullptr) {
Vishnu Nair4351ad52019-02-11 14:13:02 -0800683 return parentBounds;
684 }
685
Alec Mouria90a5702021-04-16 16:36:21 +0000686 uint32_t bufWidth = mBufferInfo.mBuffer->getBuffer()->getWidth();
687 uint32_t bufHeight = mBufferInfo.mBuffer->getBuffer()->getHeight();
Vishnu Nair4351ad52019-02-11 14:13:02 -0800688
689 // Undo any transformations on the buffer and return the result.
chaviw4244e032019-09-04 11:27:49 -0700690 if (mBufferInfo.mTransform & ui::Transform::ROT_90) {
Vishnu Nair4351ad52019-02-11 14:13:02 -0800691 std::swap(bufWidth, bufHeight);
692 }
693
694 if (getTransformToDisplayInverse()) {
Dominik Laskowski718f9602019-11-09 20:01:35 -0800695 uint32_t invTransform = DisplayDevice::getPrimaryDisplayRotationFlags();
Vishnu Nair4351ad52019-02-11 14:13:02 -0800696 if (invTransform & ui::Transform::ROT_90) {
697 std::swap(bufWidth, bufHeight);
698 }
699 }
700
701 return FloatRect(0, 0, bufWidth, bufHeight);
702}
703
chaviw49a108c2019-08-12 11:23:06 -0700704void BufferLayer::latchAndReleaseBuffer() {
705 mRefreshPending = false;
706 if (hasReadyFrame()) {
707 bool ignored = false;
708 latchBuffer(ignored, systemTime(), 0 /* expectedPresentTime */);
709 }
710 releasePendingBuffer(systemTime());
711}
712
chaviw4244e032019-09-04 11:27:49 -0700713PixelFormat BufferLayer::getPixelFormat() const {
714 return mBufferInfo.mPixelFormat;
715}
716
717bool BufferLayer::getTransformToDisplayInverse() const {
718 return mBufferInfo.mTransformToDisplayInverse;
719}
720
721Rect BufferLayer::getBufferCrop() const {
722 // this is the crop rectangle that applies to the buffer
723 // itself (as opposed to the window)
724 if (!mBufferInfo.mCrop.isEmpty()) {
725 // if the buffer crop is defined, we use that
726 return mBufferInfo.mCrop;
chaviwd62d3062019-09-04 14:48:02 -0700727 } else if (mBufferInfo.mBuffer != nullptr) {
chaviw4244e032019-09-04 11:27:49 -0700728 // otherwise we use the whole buffer
Alec Mouria90a5702021-04-16 16:36:21 +0000729 return mBufferInfo.mBuffer->getBuffer()->getBounds();
chaviw4244e032019-09-04 11:27:49 -0700730 } else {
731 // if we don't have a buffer yet, we use an empty/invalid crop
732 return Rect();
733 }
734}
735
736uint32_t BufferLayer::getBufferTransform() const {
737 return mBufferInfo.mTransform;
738}
739
740ui::Dataspace BufferLayer::getDataSpace() const {
741 return mBufferInfo.mDataspace;
742}
743
744ui::Dataspace BufferLayer::translateDataspace(ui::Dataspace dataspace) {
745 ui::Dataspace updatedDataspace = dataspace;
746 // translate legacy dataspaces to modern dataspaces
747 switch (dataspace) {
748 case ui::Dataspace::SRGB:
749 updatedDataspace = ui::Dataspace::V0_SRGB;
750 break;
751 case ui::Dataspace::SRGB_LINEAR:
752 updatedDataspace = ui::Dataspace::V0_SRGB_LINEAR;
753 break;
754 case ui::Dataspace::JFIF:
755 updatedDataspace = ui::Dataspace::V0_JFIF;
756 break;
757 case ui::Dataspace::BT601_625:
758 updatedDataspace = ui::Dataspace::V0_BT601_625;
759 break;
760 case ui::Dataspace::BT601_525:
761 updatedDataspace = ui::Dataspace::V0_BT601_525;
762 break;
763 case ui::Dataspace::BT709:
764 updatedDataspace = ui::Dataspace::V0_BT709;
765 break;
766 default:
767 break;
768 }
769
770 return updatedDataspace;
771}
772
chaviwd62d3062019-09-04 14:48:02 -0700773sp<GraphicBuffer> BufferLayer::getBuffer() const {
Alec Mouria90a5702021-04-16 16:36:21 +0000774 return mBufferInfo.mBuffer ? mBufferInfo.mBuffer->getBuffer() : nullptr;
chaviwd62d3062019-09-04 14:48:02 -0700775}
776
chaviwf83ce182019-09-12 14:43:08 -0700777void BufferLayer::getDrawingTransformMatrix(bool filteringEnabled, float outMatrix[16]) {
Alec Mouria90a5702021-04-16 16:36:21 +0000778 GLConsumer::computeTransformMatrix(outMatrix,
779 mBufferInfo.mBuffer ? mBufferInfo.mBuffer->getBuffer()
780 : nullptr,
781 mBufferInfo.mCrop, mBufferInfo.mTransform, filteringEnabled);
chaviwf83ce182019-09-12 14:43:08 -0700782}
783
chaviwb4c6e582019-08-16 14:35:07 -0700784void BufferLayer::setInitialValuesForClone(const sp<Layer>& clonedFrom) {
785 Layer::setInitialValuesForClone(clonedFrom);
786
787 sp<BufferLayer> bufferClonedFrom = static_cast<BufferLayer*>(clonedFrom.get());
788 mPremultipliedAlpha = bufferClonedFrom->mPremultipliedAlpha;
789 mPotentialCursor = bufferClonedFrom->mPotentialCursor;
790 mProtectedByApp = bufferClonedFrom->mProtectedByApp;
chaviw74b03172019-08-19 11:09:03 -0700791
792 updateCloneBufferInfo();
793}
794
795void BufferLayer::updateCloneBufferInfo() {
796 if (!isClone() || !isClonedFromAlive()) {
797 return;
798 }
799
800 sp<BufferLayer> clonedFrom = static_cast<BufferLayer*>(getClonedFrom().get());
801 mBufferInfo = clonedFrom->mBufferInfo;
802 mSidebandStream = clonedFrom->mSidebandStream;
803 surfaceDamageRegion = clonedFrom->surfaceDamageRegion;
804 mCurrentFrameNumber = clonedFrom->mCurrentFrameNumber.load();
805 mPreviousFrameNumber = clonedFrom->mPreviousFrameNumber;
806
807 // After buffer info is updated, the drawingState from the real layer needs to be copied into
808 // the cloned. This is because some properties of drawingState can change when latchBuffer is
chaviwaf87b3e2019-10-01 16:59:28 -0700809 // called. However, copying the drawingState would also overwrite the cloned layer's relatives
810 // and touchableRegionCrop. Therefore, temporarily store the relatives so they can be set in
811 // the cloned drawingState again.
chaviw74b03172019-08-19 11:09:03 -0700812 wp<Layer> tmpZOrderRelativeOf = mDrawingState.zOrderRelativeOf;
813 SortedVector<wp<Layer>> tmpZOrderRelatives = mDrawingState.zOrderRelatives;
chaviwaf87b3e2019-10-01 16:59:28 -0700814 wp<Layer> tmpTouchableRegionCrop = mDrawingState.touchableRegionCrop;
815 InputWindowInfo tmpInputInfo = mDrawingState.inputInfo;
816
chaviw74b03172019-08-19 11:09:03 -0700817 mDrawingState = clonedFrom->mDrawingState;
chaviwaf87b3e2019-10-01 16:59:28 -0700818
819 mDrawingState.touchableRegionCrop = tmpTouchableRegionCrop;
chaviw74b03172019-08-19 11:09:03 -0700820 mDrawingState.zOrderRelativeOf = tmpZOrderRelativeOf;
821 mDrawingState.zOrderRelatives = tmpZOrderRelatives;
chaviwaf87b3e2019-10-01 16:59:28 -0700822 mDrawingState.inputInfo = tmpInputInfo;
chaviwb4c6e582019-08-16 14:35:07 -0700823}
824
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700825void BufferLayer::setTransformHint(ui::Transform::RotationFlags displayTransformHint) {
Vishnu Nair6213bd92020-05-08 17:42:25 -0700826 mTransformHint = getFixedTransformHint();
827 if (mTransformHint == ui::Transform::ROT_INVALID) {
828 mTransformHint = displayTransformHint;
829 }
830}
831
Vishnu Naire7f79c52020-10-29 14:45:03 -0700832bool BufferLayer::bufferNeedsFiltering() const {
833 return isFixedSize();
834}
835
David Sodman0c69cad2017-08-21 12:12:51 -0700836} // namespace android
837
838#if defined(__gl_h_)
839#error "don't include gl/gl.h in this file"
840#endif
841
842#if defined(__gl2_h_)
843#error "don't include gl2/gl2.h in this file"
844#endif
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800845
846// TODO(b/129481165): remove the #pragma below and fix conversion issues
847#pragma clang diagnostic pop // ignored "-Wconversion"