blob: 6561707bcc7cb28f4aa63324db11a9bb688661dd [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 }
Lloyd Piquef16688f2019-02-19 17:47:57 -0800179 return std::nullopt;
David Sodman0c69cad2017-08-21 12:12:51 -0700180 }
Lloyd Pique688abd42019-02-15 15:42:24 -0800181 bool blackOutLayer = (isProtected() && !targetSettings.supportsProtectedContent) ||
Lloyd Piquef16688f2019-02-19 17:47:57 -0800182 (isSecure() && !targetSettings.isSecure);
Lloyd Piquede196652020-01-22 17:29:58 -0800183 compositionengine::LayerFE::LayerSettings& layer = *result;
Vishnu Nairb87d94f2020-02-13 09:17:36 -0800184 if (blackOutLayer) {
185 prepareClearClientComposition(layer, true /* blackout */);
186 return layer;
David Sodman0c69cad2017-08-21 12:12:51 -0700187 }
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000188
Vishnu Nairb87d94f2020-02-13 09:17:36 -0800189 const State& s(getDrawingState());
190 layer.source.buffer.buffer = mBufferInfo.mBuffer;
191 layer.source.buffer.isOpaque = isOpaque(s);
192 layer.source.buffer.fence = mBufferInfo.mFence;
193 layer.source.buffer.textureName = mTextureName;
194 layer.source.buffer.usePremultipliedAlpha = getPremultipledAlpha();
195 layer.source.buffer.isY410BT2020 = isHdrY410();
196 bool hasSmpte2086 = mBufferInfo.mHdrMetadata.validTypes & HdrMetadata::SMPTE2086;
197 bool hasCta861_3 = mBufferInfo.mHdrMetadata.validTypes & HdrMetadata::CTA861_3;
198 layer.source.buffer.maxMasteringLuminance = hasSmpte2086
199 ? mBufferInfo.mHdrMetadata.smpte2086.maxLuminance
200 : defaultMaxMasteringLuminance;
201 layer.source.buffer.maxContentLuminance = hasCta861_3
202 ? mBufferInfo.mHdrMetadata.cta8613.maxContentLightLevel
203 : defaultMaxContentLuminance;
204 layer.frameNumber = mCurrentFrameNumber;
205 layer.bufferId = mBufferInfo.mBuffer ? mBufferInfo.mBuffer->getId() : 0;
206
Vishnu Naire7f79c52020-10-29 14:45:03 -0700207 const bool useFiltering =
208 targetSettings.needsFiltering || mNeedsFiltering || bufferNeedsFiltering();
Vishnu Nairb87d94f2020-02-13 09:17:36 -0800209
210 // Query the texture matrix given our current filtering mode.
211 float textureMatrix[16];
212 getDrawingTransformMatrix(useFiltering, textureMatrix);
213
214 if (getTransformToDisplayInverse()) {
215 /*
216 * the code below applies the primary display's inverse transform to
217 * the texture transform
218 */
219 uint32_t transform = DisplayDevice::getPrimaryDisplayRotationFlags();
220 mat4 tr = inverseOrientation(transform);
221
222 /**
223 * TODO(b/36727915): This is basically a hack.
224 *
225 * Ensure that regardless of the parent transformation,
226 * this buffer is always transformed from native display
227 * orientation to display orientation. For example, in the case
228 * of a camera where the buffer remains in native orientation,
229 * we want the pixels to always be upright.
230 */
231 sp<Layer> p = mDrawingParent.promote();
232 if (p != nullptr) {
233 const auto parentTransform = p->getTransform();
234 tr = tr * inverseOrientation(parentTransform.getOrientation());
235 }
236
237 // and finally apply it to the original texture matrix
238 const mat4 texTransform(mat4(static_cast<const float*>(textureMatrix)) * tr);
239 memcpy(textureMatrix, texTransform.asArray(), sizeof(textureMatrix));
240 }
241
242 const Rect win{getBounds()};
243 float bufferWidth = getBufferSize(s).getWidth();
244 float bufferHeight = getBufferSize(s).getHeight();
245
246 // BufferStateLayers can have a "buffer size" of [0, 0, -1, -1] when no display frame has
247 // been set and there is no parent layer bounds. In that case, the scale is meaningless so
248 // ignore them.
249 if (!getBufferSize(s).isValid()) {
250 bufferWidth = float(win.right) - float(win.left);
251 bufferHeight = float(win.bottom) - float(win.top);
252 }
253
254 const float scaleHeight = (float(win.bottom) - float(win.top)) / bufferHeight;
255 const float scaleWidth = (float(win.right) - float(win.left)) / bufferWidth;
256 const float translateY = float(win.top) / bufferHeight;
257 const float translateX = float(win.left) / bufferWidth;
258
259 // Flip y-coordinates because GLConsumer expects OpenGL convention.
260 mat4 tr = mat4::translate(vec4(.5, .5, 0, 1)) * mat4::scale(vec4(1, -1, 1, 1)) *
261 mat4::translate(vec4(-.5, -.5, 0, 1)) *
262 mat4::translate(vec4(translateX, translateY, 0, 1)) *
263 mat4::scale(vec4(scaleWidth, scaleHeight, 1.0, 1.0));
264
265 layer.source.buffer.useTextureFiltering = useFiltering;
266 layer.source.buffer.textureTransform = mat4(static_cast<const float*>(textureMatrix)) * tr;
267
Vishnu Nair9b079a22020-01-21 14:36:08 -0800268 return layer;
David Sodman0c69cad2017-08-21 12:12:51 -0700269}
270
Marissa Wallfd668622018-05-10 10:21:13 -0700271bool BufferLayer::isHdrY410() const {
272 // pixel format is HDR Y410 masquerading as RGBA_1010102
chaviw4244e032019-09-04 11:27:49 -0700273 return (mBufferInfo.mDataspace == ui::Dataspace::BT2020_ITU_PQ &&
274 mBufferInfo.mApi == NATIVE_WINDOW_API_MEDIA &&
chaviwdebadb82020-03-26 14:57:24 -0700275 mBufferInfo.mPixelFormat == HAL_PIXEL_FORMAT_RGBA_1010102);
David Sodmaneb085e02017-10-05 18:49:04 -0700276}
277
Lloyd Piquede196652020-01-22 17:29:58 -0800278sp<compositionengine::LayerFE> BufferLayer::getCompositionEngineLayerFE() const {
279 return asLayerFE();
280}
281
282compositionengine::LayerFECompositionState* BufferLayer::editCompositionState() {
283 return mCompositionState.get();
284}
285
286const compositionengine::LayerFECompositionState* BufferLayer::getCompositionState() const {
287 return mCompositionState.get();
288}
289
290void BufferLayer::preparePerFrameCompositionState() {
291 Layer::preparePerFrameCompositionState();
David Sodman0c69cad2017-08-21 12:12:51 -0700292
293 // Sideband layers
Lloyd Piquede196652020-01-22 17:29:58 -0800294 auto* compositionState = editCompositionState();
295 if (compositionState->sidebandStream.get()) {
296 compositionState->compositionType = Hwc2::IComposerClient::Composition::SIDEBAND;
Robert Carra6bb2bc2020-04-08 11:03:23 -0700297 return;
David Sodman15094112018-10-11 09:39:37 -0700298 } else {
Lloyd Piquef5275482019-01-29 18:42:42 -0800299 // Normal buffer layers
Lloyd Piquede196652020-01-22 17:29:58 -0800300 compositionState->hdrMetadata = mBufferInfo.mHdrMetadata;
301 compositionState->compositionType = mPotentialCursor
Lloyd Piquef5275482019-01-29 18:42:42 -0800302 ? Hwc2::IComposerClient::Composition::CURSOR
303 : Hwc2::IComposerClient::Composition::DEVICE;
David Sodman0c69cad2017-08-21 12:12:51 -0700304 }
Robert Carra6bb2bc2020-04-08 11:03:23 -0700305
306 compositionState->buffer = mBufferInfo.mBuffer;
307 compositionState->bufferSlot = (mBufferInfo.mBufferSlot == BufferQueue::INVALID_BUFFER_SLOT)
308 ? 0
309 : mBufferInfo.mBufferSlot;
310 compositionState->acquireFence = mBufferInfo.mFence;
David Sodman0c69cad2017-08-21 12:12:51 -0700311}
312
Marissa Wallfd668622018-05-10 10:21:13 -0700313bool BufferLayer::onPreComposition(nsecs_t refreshStartTime) {
chaviwd62d3062019-09-04 14:48:02 -0700314 if (mBufferInfo.mBuffer != nullptr) {
Marissa Wallfd668622018-05-10 10:21:13 -0700315 Mutex::Autolock lock(mFrameEventHistoryMutex);
316 mFrameEventHistory.addPreComposition(mCurrentFrameNumber, refreshStartTime);
David Sodman0c69cad2017-08-21 12:12:51 -0700317 }
Marissa Wallfd668622018-05-10 10:21:13 -0700318 mRefreshPending = false;
319 return hasReadyFrame();
David Sodman0c69cad2017-08-21 12:12:51 -0700320}
321
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700322bool BufferLayer::onPostComposition(const DisplayDevice* display,
Dominik Laskowski075d3172018-05-24 15:50:06 -0700323 const std::shared_ptr<FenceTime>& glDoneFence,
Marissa Wallfd668622018-05-10 10:21:13 -0700324 const std::shared_ptr<FenceTime>& presentFence,
325 const CompositorTiming& compositorTiming) {
326 // mFrameLatencyNeeded is true when a new frame was latched for the
327 // composition.
chaviw74b03172019-08-19 11:09:03 -0700328 if (!mBufferInfo.mFrameLatencyNeeded) return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700329
330 // Update mFrameEventHistory.
Dan Stoza436ccf32018-06-21 12:10:12 -0700331 {
Marissa Wallfd668622018-05-10 10:21:13 -0700332 Mutex::Autolock lock(mFrameEventHistoryMutex);
333 mFrameEventHistory.addPostComposition(mCurrentFrameNumber, glDoneFence, presentFence,
334 compositorTiming);
Valerie Hau93d5a5e2020-02-13 14:50:01 -0800335 finalizeFrameEventHistory(glDoneFence, compositorTiming);
David Sodman0c69cad2017-08-21 12:12:51 -0700336 }
337
Marissa Wallfd668622018-05-10 10:21:13 -0700338 // Update mFrameTracker.
chaviw4244e032019-09-04 11:27:49 -0700339 nsecs_t desiredPresentTime = mBufferInfo.mDesiredPresentTime;
Marissa Wallfd668622018-05-10 10:21:13 -0700340 mFrameTracker.setDesiredPresentTime(desiredPresentTime);
341
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800342 const int32_t layerId = getSequence();
343 mFlinger->mTimeStats->setDesiredTime(layerId, mCurrentFrameNumber, desiredPresentTime);
Marissa Wallfd668622018-05-10 10:21:13 -0700344
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700345 const auto outputLayer = findOutputLayerForDisplay(display);
Adithya Srinivasanb69e0762019-11-11 18:39:53 -0800346 if (outputLayer && outputLayer->requiresClientComposition()) {
347 nsecs_t clientCompositionTimestamp = outputLayer->getState().clientCompositionTimestamp;
348 mFlinger->mFrameTracer->traceTimestamp(layerId, getCurrentBufferId(), mCurrentFrameNumber,
349 clientCompositionTimestamp,
350 FrameTracer::FrameEvent::FALLBACK_COMPOSITION);
351 }
352
chaviw4244e032019-09-04 11:27:49 -0700353 std::shared_ptr<FenceTime> frameReadyFence = mBufferInfo.mFenceTime;
Marissa Wallfd668622018-05-10 10:21:13 -0700354 if (frameReadyFence->isValid()) {
355 mFrameTracker.setFrameReadyFence(std::move(frameReadyFence));
356 } else {
357 // There was no fence for this frame, so assume that it was ready
358 // to be presented at the desired present time.
359 mFrameTracker.setFrameReadyTime(desiredPresentTime);
Dominik Laskowski45de9bd2018-06-11 17:44:10 -0700360 }
Marissa Wallfd668622018-05-10 10:21:13 -0700361
362 if (presentFence->isValid()) {
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800363 mFlinger->mTimeStats->setPresentFence(layerId, mCurrentFrameNumber, presentFence);
364 mFlinger->mFrameTracer->traceFence(layerId, getCurrentBufferId(), mCurrentFrameNumber,
Mikael Pessa90092f42019-08-26 17:22:04 -0700365 presentFence, FrameTracer::FrameEvent::PRESENT_FENCE);
Marissa Wallfd668622018-05-10 10:21:13 -0700366 mFrameTracker.setActualPresentFence(std::shared_ptr<FenceTime>(presentFence));
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700367 } else if (!display) {
368 // Do nothing.
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200369 } else if (const auto displayId = PhysicalDisplayId::tryCast(display->getId());
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700370 displayId && mFlinger->getHwComposer().isConnected(*displayId)) {
Marissa Wallfd668622018-05-10 10:21:13 -0700371 // The HWC doesn't support present fences, so use the refresh
372 // timestamp instead.
Dominik Laskowski075d3172018-05-24 15:50:06 -0700373 const nsecs_t actualPresentTime = mFlinger->getHwComposer().getRefreshTimestamp(*displayId);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800374 mFlinger->mTimeStats->setPresentTime(layerId, mCurrentFrameNumber, actualPresentTime);
375 mFlinger->mFrameTracer->traceTimestamp(layerId, getCurrentBufferId(), mCurrentFrameNumber,
Mikael Pessa90092f42019-08-26 17:22:04 -0700376 actualPresentTime,
377 FrameTracer::FrameEvent::PRESENT_FENCE);
Marissa Wallfd668622018-05-10 10:21:13 -0700378 mFrameTracker.setActualPresentTime(actualPresentTime);
379 }
380
381 mFrameTracker.advanceFrame();
chaviw74b03172019-08-19 11:09:03 -0700382 mBufferInfo.mFrameLatencyNeeded = false;
Marissa Wallfd668622018-05-10 10:21:13 -0700383 return true;
David Sodman0c69cad2017-08-21 12:12:51 -0700384}
385
chaviwdebadb82020-03-26 14:57:24 -0700386void BufferLayer::gatherBufferInfo() {
387 mBufferInfo.mPixelFormat =
388 !mBufferInfo.mBuffer ? PIXEL_FORMAT_NONE : mBufferInfo.mBuffer->format;
389 mBufferInfo.mFrameLatencyNeeded = true;
390}
391
Ady Abrahamce4adf12020-12-15 18:45:12 -0800392bool BufferLayer::frameIsEarly(nsecs_t expectedPresentTime) const {
393 // TODO(b/169901895): kEarlyLatchVsyncThreshold should be based on the
394 // vsync period. We can do this change as soon as ag/13100772 is merged.
395 constexpr static std::chrono::nanoseconds kEarlyLatchVsyncThreshold = 5ms;
396
397 const auto presentTime = nextPredictedPresentTime();
398 if (std::abs(presentTime - expectedPresentTime) >= kEarlyLatchMaxThreshold.count()) {
399 return false;
400 }
401
402 return presentTime >= expectedPresentTime &&
403 presentTime - expectedPresentTime >= kEarlyLatchVsyncThreshold.count();
404}
405
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700406bool BufferLayer::latchBuffer(bool& recomputeVisibleRegions, nsecs_t latchTime,
407 nsecs_t expectedPresentTime) {
Marissa Wallfd668622018-05-10 10:21:13 -0700408 ATRACE_CALL();
David Sodman0c69cad2017-08-21 12:12:51 -0700409
Ady Abraham0bb6a472020-10-12 10:22:13 -0700410 // If this is not a valid vsync for the layer's uid, return and try again later
411 const bool isVsyncValidForUid =
412 mFlinger->mScheduler->isVsyncValid(expectedPresentTime, mOwnerUid);
413 if (!isVsyncValidForUid) {
414 ATRACE_NAME("!isVsyncValidForUid");
415 mFlinger->setTransactionFlags(eTraversalNeeded);
416 return false;
417 }
418
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800419 bool refreshRequired = latchSidebandStream(recomputeVisibleRegions);
David Sodman0c69cad2017-08-21 12:12:51 -0700420
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800421 if (refreshRequired) {
422 return refreshRequired;
David Sodman0c69cad2017-08-21 12:12:51 -0700423 }
424
Marissa Wallfd668622018-05-10 10:21:13 -0700425 if (!hasReadyFrame()) {
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800426 return false;
David Sodman0c69cad2017-08-21 12:12:51 -0700427 }
David Sodman0c69cad2017-08-21 12:12:51 -0700428
Marissa Wallfd668622018-05-10 10:21:13 -0700429 // if we've already called updateTexImage() without going through
430 // a composition step, we have to skip this layer at this point
431 // because we cannot call updateTeximage() without a corresponding
432 // compositionComplete() call.
433 // we'll trigger an update in onPreComposition().
434 if (mRefreshPending) {
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800435 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700436 }
437
Ady Abrahamce4adf12020-12-15 18:45:12 -0800438 if (frameIsEarly(expectedPresentTime)) {
439 ATRACE_NAME("frameIsEarly()");
440 mFlinger->signalLayerUpdate();
441 return false;
442 }
443
Marissa Wallfd668622018-05-10 10:21:13 -0700444 // If the head buffer's acquire fence hasn't signaled yet, return and
445 // try again later
446 if (!fenceHasSignaled()) {
Ady Abraham09bd3922019-04-08 10:44:56 -0700447 ATRACE_NAME("!fenceHasSignaled()");
David Sodman0c69cad2017-08-21 12:12:51 -0700448 mFlinger->signalLayerUpdate();
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800449 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700450 }
451
452 // Capture the old state of the layer for comparisons later
453 const State& s(getDrawingState());
454 const bool oldOpacity = isOpaque(s);
chaviwd62d3062019-09-04 14:48:02 -0700455
456 BufferInfo oldBufferInfo = mBufferInfo;
Marissa Wallfd668622018-05-10 10:21:13 -0700457
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700458 if (!allTransactionsSignaled(expectedPresentTime)) {
Marissa Wallebb486e2019-05-15 14:08:08 -0700459 mFlinger->setTransactionFlags(eTraversalNeeded);
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800460 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700461 }
462
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700463 status_t err = updateTexImage(recomputeVisibleRegions, latchTime, expectedPresentTime);
Marissa Wallfd668622018-05-10 10:21:13 -0700464 if (err != NO_ERROR) {
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800465 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700466 }
467
468 err = updateActiveBuffer();
469 if (err != NO_ERROR) {
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800470 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700471 }
472
Marissa Wallfd668622018-05-10 10:21:13 -0700473 err = updateFrameNumber(latchTime);
474 if (err != NO_ERROR) {
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800475 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700476 }
477
chaviw4244e032019-09-04 11:27:49 -0700478 gatherBufferInfo();
479
Marissa Wallfd668622018-05-10 10:21:13 -0700480 mRefreshPending = true;
chaviwd62d3062019-09-04 14:48:02 -0700481 if (oldBufferInfo.mBuffer == nullptr) {
Marissa Wallfd668622018-05-10 10:21:13 -0700482 // the first time we receive a buffer, we need to trigger a
483 // geometry invalidation.
484 recomputeVisibleRegions = true;
485 }
486
chaviw4244e032019-09-04 11:27:49 -0700487 if ((mBufferInfo.mCrop != oldBufferInfo.mCrop) ||
488 (mBufferInfo.mTransform != oldBufferInfo.mTransform) ||
489 (mBufferInfo.mScaleMode != oldBufferInfo.mScaleMode) ||
490 (mBufferInfo.mTransformToDisplayInverse != oldBufferInfo.mTransformToDisplayInverse)) {
Marissa Wallfd668622018-05-10 10:21:13 -0700491 recomputeVisibleRegions = true;
492 }
493
chaviwd62d3062019-09-04 14:48:02 -0700494 if (oldBufferInfo.mBuffer != nullptr) {
495 uint32_t bufWidth = mBufferInfo.mBuffer->getWidth();
496 uint32_t bufHeight = mBufferInfo.mBuffer->getHeight();
497 if (bufWidth != uint32_t(oldBufferInfo.mBuffer->width) ||
498 bufHeight != uint32_t(oldBufferInfo.mBuffer->height)) {
Marissa Wallfd668622018-05-10 10:21:13 -0700499 recomputeVisibleRegions = true;
500 }
501 }
502
503 if (oldOpacity != isOpaque(s)) {
504 recomputeVisibleRegions = true;
505 }
506
507 // Remove any sync points corresponding to the buffer which was just
508 // latched
509 {
510 Mutex::Autolock lock(mLocalSyncPointMutex);
511 auto point = mLocalSyncPoints.begin();
512 while (point != mLocalSyncPoints.end()) {
513 if (!(*point)->frameIsAvailable() || !(*point)->transactionIsApplied()) {
514 // This sync point must have been added since we started
515 // latching. Don't drop it yet.
516 ++point;
517 continue;
518 }
519
520 if ((*point)->getFrameNumber() <= mCurrentFrameNumber) {
Alec Mourie60041e2019-06-14 18:59:51 -0700521 std::stringstream ss;
522 ss << "Dropping sync point " << (*point)->getFrameNumber();
523 ATRACE_NAME(ss.str().c_str());
Marissa Wallfd668622018-05-10 10:21:13 -0700524 point = mLocalSyncPoints.erase(point);
525 } else {
526 ++point;
527 }
528 }
529 }
530
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800531 return true;
Marissa Wallfd668622018-05-10 10:21:13 -0700532}
533
534// transaction
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700535void BufferLayer::notifyAvailableFrames(nsecs_t expectedPresentTime) {
536 const auto headFrameNumber = getHeadFrameNumber(expectedPresentTime);
Ady Abrahamcd1580c2019-04-29 15:40:03 -0700537 const bool headFenceSignaled = fenceHasSignaled();
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700538 const bool presentTimeIsCurrent = framePresentTimeIsCurrent(expectedPresentTime);
Marissa Wallfd668622018-05-10 10:21:13 -0700539 Mutex::Autolock lock(mLocalSyncPointMutex);
540 for (auto& point : mLocalSyncPoints) {
Ady Abrahamcd1580c2019-04-29 15:40:03 -0700541 if (headFrameNumber >= point->getFrameNumber() && headFenceSignaled &&
542 presentTimeIsCurrent) {
Marissa Wallfd668622018-05-10 10:21:13 -0700543 point->setFrameAvailable();
chaviw43cb3cb2019-05-31 15:23:41 -0700544 sp<Layer> requestedSyncLayer = point->getRequestedSyncLayer();
545 if (requestedSyncLayer) {
546 // Need to update the transaction flag to ensure the layer's pending transaction
547 // gets applied.
548 requestedSyncLayer->setTransactionFlags(eTransactionNeeded);
549 }
Marissa Wallfd668622018-05-10 10:21:13 -0700550 }
David Sodman0c69cad2017-08-21 12:12:51 -0700551 }
552}
553
Marissa Wallfd668622018-05-10 10:21:13 -0700554bool BufferLayer::hasReadyFrame() const {
Marissa Wall024a1912018-08-13 13:55:35 -0700555 return hasFrameUpdate() || getSidebandStreamChanged() || getAutoRefresh();
Marissa Wallfd668622018-05-10 10:21:13 -0700556}
557
558uint32_t BufferLayer::getEffectiveScalingMode() const {
chaviw4244e032019-09-04 11:27:49 -0700559 return mBufferInfo.mScaleMode;
Marissa Wallfd668622018-05-10 10:21:13 -0700560}
561
562bool BufferLayer::isProtected() const {
chaviwd62d3062019-09-04 14:48:02 -0700563 const sp<GraphicBuffer>& buffer(mBufferInfo.mBuffer);
Marissa Wallfd668622018-05-10 10:21:13 -0700564 return (buffer != 0) && (buffer->getUsage() & GRALLOC_USAGE_PROTECTED);
565}
566
Marissa Wallfd668622018-05-10 10:21:13 -0700567// h/w composer set-up
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700568bool BufferLayer::allTransactionsSignaled(nsecs_t expectedPresentTime) {
569 const auto headFrameNumber = getHeadFrameNumber(expectedPresentTime);
Marissa Wallfd668622018-05-10 10:21:13 -0700570 bool matchingFramesFound = false;
571 bool allTransactionsApplied = true;
572 Mutex::Autolock lock(mLocalSyncPointMutex);
573
574 for (auto& point : mLocalSyncPoints) {
575 if (point->getFrameNumber() > headFrameNumber) {
576 break;
577 }
578 matchingFramesFound = true;
579
580 if (!point->frameIsAvailable()) {
581 // We haven't notified the remote layer that the frame for
582 // this point is available yet. Notify it now, and then
583 // abort this attempt to latch.
584 point->setFrameAvailable();
585 allTransactionsApplied = false;
586 break;
587 }
588
589 allTransactionsApplied = allTransactionsApplied && point->transactionIsApplied();
590 }
591 return !matchingFramesFound || allTransactionsApplied;
David Sodman0c69cad2017-08-21 12:12:51 -0700592}
593
594// As documented in libhardware header, formats in the range
595// 0x100 - 0x1FF are specific to the HAL implementation, and
596// are known to have no alpha channel
597// TODO: move definition for device-specific range into
598// hardware.h, instead of using hard-coded values here.
599#define HARDWARE_IS_DEVICE_FORMAT(f) ((f) >= 0x100 && (f) <= 0x1FF)
600
601bool BufferLayer::getOpacityForFormat(uint32_t format) {
602 if (HARDWARE_IS_DEVICE_FORMAT(format)) {
603 return true;
604 }
605 switch (format) {
606 case HAL_PIXEL_FORMAT_RGBA_8888:
607 case HAL_PIXEL_FORMAT_BGRA_8888:
608 case HAL_PIXEL_FORMAT_RGBA_FP16:
609 case HAL_PIXEL_FORMAT_RGBA_1010102:
610 return false;
611 }
612 // in all other case, we have no blending (also for unknown formats)
613 return true;
614}
615
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700616bool BufferLayer::needsFiltering(const DisplayDevice* display) const {
617 const auto outputLayer = findOutputLayerForDisplay(display);
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800618 if (outputLayer == nullptr) {
Lloyd Piquef16688f2019-02-19 17:47:57 -0800619 return false;
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800620 }
621
Lloyd Piquef16688f2019-02-19 17:47:57 -0800622 // We need filtering if the sourceCrop rectangle size does not match the
623 // displayframe rectangle size (not a 1:1 render)
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800624 const auto& compositionState = outputLayer->getState();
625 const auto displayFrame = compositionState.displayFrame;
626 const auto sourceCrop = compositionState.sourceCrop;
Lloyd Piquef16688f2019-02-19 17:47:57 -0800627 return sourceCrop.getHeight() != displayFrame.getHeight() ||
Peiyong Linc2020ca2019-01-10 11:36:12 -0800628 sourceCrop.getWidth() != displayFrame.getWidth();
Chia-I Wu692e0832018-06-05 15:46:58 -0700629}
630
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700631bool BufferLayer::needsFilteringForScreenshots(const DisplayDevice* display,
Alec Mouri5a6d8572020-03-23 23:56:15 -0700632 const ui::Transform& inverseParentTransform) const {
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700633 const auto outputLayer = findOutputLayerForDisplay(display);
Alec Mouri5a6d8572020-03-23 23:56:15 -0700634 if (outputLayer == nullptr) {
635 return false;
636 }
637
638 // We need filtering if the sourceCrop rectangle size does not match the
639 // viewport rectangle size (not a 1:1 render)
640 const auto& compositionState = outputLayer->getState();
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700641 const ui::Transform& displayTransform = display->getTransform();
Alec Mouri5a6d8572020-03-23 23:56:15 -0700642 const ui::Transform inverseTransform = inverseParentTransform * displayTransform.inverse();
643 // Undo the transformation of the displayFrame so that we're back into
644 // layer-stack space.
645 const Rect frame = inverseTransform.transform(compositionState.displayFrame);
646 const FloatRect sourceCrop = compositionState.sourceCrop;
647
648 int32_t frameHeight = frame.getHeight();
649 int32_t frameWidth = frame.getWidth();
650 // If the display transform had a rotational component then undo the
651 // rotation so that the orientation matches the source crop.
652 if (displayTransform.getOrientation() & ui::Transform::ROT_90) {
653 std::swap(frameHeight, frameWidth);
654 }
655 return sourceCrop.getHeight() != frameHeight || sourceCrop.getWidth() != frameWidth;
656}
657
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700658uint64_t BufferLayer::getHeadFrameNumber(nsecs_t expectedPresentTime) const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800659 if (hasFrameUpdate()) {
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700660 return getFrameNumber(expectedPresentTime);
David Sodman0c69cad2017-08-21 12:12:51 -0700661 } else {
662 return mCurrentFrameNumber;
663 }
664}
665
Vishnu Nair60356342018-11-13 13:00:45 -0800666Rect BufferLayer::getBufferSize(const State& s) const {
667 // If we have a sideband stream, or we are scaling the buffer then return the layer size since
668 // we cannot determine the buffer size.
669 if ((s.sidebandStream != nullptr) ||
670 (getEffectiveScalingMode() != NATIVE_WINDOW_SCALING_MODE_FREEZE)) {
671 return Rect(getActiveWidth(s), getActiveHeight(s));
672 }
673
chaviwd62d3062019-09-04 14:48:02 -0700674 if (mBufferInfo.mBuffer == nullptr) {
Vishnu Nair60356342018-11-13 13:00:45 -0800675 return Rect::INVALID_RECT;
676 }
677
chaviwd62d3062019-09-04 14:48:02 -0700678 uint32_t bufWidth = mBufferInfo.mBuffer->getWidth();
679 uint32_t bufHeight = mBufferInfo.mBuffer->getHeight();
Vishnu Nair60356342018-11-13 13:00:45 -0800680
681 // Undo any transformations on the buffer and return the result.
chaviw4244e032019-09-04 11:27:49 -0700682 if (mBufferInfo.mTransform & ui::Transform::ROT_90) {
Vishnu Nair60356342018-11-13 13:00:45 -0800683 std::swap(bufWidth, bufHeight);
684 }
685
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800686 if (getTransformToDisplayInverse()) {
Dominik Laskowski718f9602019-11-09 20:01:35 -0800687 uint32_t invTransform = DisplayDevice::getPrimaryDisplayRotationFlags();
Vishnu Nair60356342018-11-13 13:00:45 -0800688 if (invTransform & ui::Transform::ROT_90) {
689 std::swap(bufWidth, bufHeight);
690 }
691 }
692
693 return Rect(bufWidth, bufHeight);
694}
695
Vishnu Nair4351ad52019-02-11 14:13:02 -0800696FloatRect BufferLayer::computeSourceBounds(const FloatRect& parentBounds) const {
697 const State& s(getDrawingState());
698
699 // If we have a sideband stream, or we are scaling the buffer then return the layer size since
700 // we cannot determine the buffer size.
701 if ((s.sidebandStream != nullptr) ||
702 (getEffectiveScalingMode() != NATIVE_WINDOW_SCALING_MODE_FREEZE)) {
703 return FloatRect(0, 0, getActiveWidth(s), getActiveHeight(s));
704 }
705
chaviwd62d3062019-09-04 14:48:02 -0700706 if (mBufferInfo.mBuffer == nullptr) {
Vishnu Nair4351ad52019-02-11 14:13:02 -0800707 return parentBounds;
708 }
709
chaviwd62d3062019-09-04 14:48:02 -0700710 uint32_t bufWidth = mBufferInfo.mBuffer->getWidth();
711 uint32_t bufHeight = mBufferInfo.mBuffer->getHeight();
Vishnu Nair4351ad52019-02-11 14:13:02 -0800712
713 // Undo any transformations on the buffer and return the result.
chaviw4244e032019-09-04 11:27:49 -0700714 if (mBufferInfo.mTransform & ui::Transform::ROT_90) {
Vishnu Nair4351ad52019-02-11 14:13:02 -0800715 std::swap(bufWidth, bufHeight);
716 }
717
718 if (getTransformToDisplayInverse()) {
Dominik Laskowski718f9602019-11-09 20:01:35 -0800719 uint32_t invTransform = DisplayDevice::getPrimaryDisplayRotationFlags();
Vishnu Nair4351ad52019-02-11 14:13:02 -0800720 if (invTransform & ui::Transform::ROT_90) {
721 std::swap(bufWidth, bufHeight);
722 }
723 }
724
725 return FloatRect(0, 0, bufWidth, bufHeight);
726}
727
chaviw49a108c2019-08-12 11:23:06 -0700728void BufferLayer::latchAndReleaseBuffer() {
729 mRefreshPending = false;
730 if (hasReadyFrame()) {
731 bool ignored = false;
732 latchBuffer(ignored, systemTime(), 0 /* expectedPresentTime */);
733 }
734 releasePendingBuffer(systemTime());
735}
736
chaviw4244e032019-09-04 11:27:49 -0700737PixelFormat BufferLayer::getPixelFormat() const {
738 return mBufferInfo.mPixelFormat;
739}
740
741bool BufferLayer::getTransformToDisplayInverse() const {
742 return mBufferInfo.mTransformToDisplayInverse;
743}
744
745Rect BufferLayer::getBufferCrop() const {
746 // this is the crop rectangle that applies to the buffer
747 // itself (as opposed to the window)
748 if (!mBufferInfo.mCrop.isEmpty()) {
749 // if the buffer crop is defined, we use that
750 return mBufferInfo.mCrop;
chaviwd62d3062019-09-04 14:48:02 -0700751 } else if (mBufferInfo.mBuffer != nullptr) {
chaviw4244e032019-09-04 11:27:49 -0700752 // otherwise we use the whole buffer
chaviwd62d3062019-09-04 14:48:02 -0700753 return mBufferInfo.mBuffer->getBounds();
chaviw4244e032019-09-04 11:27:49 -0700754 } else {
755 // if we don't have a buffer yet, we use an empty/invalid crop
756 return Rect();
757 }
758}
759
760uint32_t BufferLayer::getBufferTransform() const {
761 return mBufferInfo.mTransform;
762}
763
764ui::Dataspace BufferLayer::getDataSpace() const {
765 return mBufferInfo.mDataspace;
766}
767
768ui::Dataspace BufferLayer::translateDataspace(ui::Dataspace dataspace) {
769 ui::Dataspace updatedDataspace = dataspace;
770 // translate legacy dataspaces to modern dataspaces
771 switch (dataspace) {
772 case ui::Dataspace::SRGB:
773 updatedDataspace = ui::Dataspace::V0_SRGB;
774 break;
775 case ui::Dataspace::SRGB_LINEAR:
776 updatedDataspace = ui::Dataspace::V0_SRGB_LINEAR;
777 break;
778 case ui::Dataspace::JFIF:
779 updatedDataspace = ui::Dataspace::V0_JFIF;
780 break;
781 case ui::Dataspace::BT601_625:
782 updatedDataspace = ui::Dataspace::V0_BT601_625;
783 break;
784 case ui::Dataspace::BT601_525:
785 updatedDataspace = ui::Dataspace::V0_BT601_525;
786 break;
787 case ui::Dataspace::BT709:
788 updatedDataspace = ui::Dataspace::V0_BT709;
789 break;
790 default:
791 break;
792 }
793
794 return updatedDataspace;
795}
796
chaviwd62d3062019-09-04 14:48:02 -0700797sp<GraphicBuffer> BufferLayer::getBuffer() const {
798 return mBufferInfo.mBuffer;
799}
800
chaviwf83ce182019-09-12 14:43:08 -0700801void BufferLayer::getDrawingTransformMatrix(bool filteringEnabled, float outMatrix[16]) {
802 GLConsumer::computeTransformMatrix(outMatrix, mBufferInfo.mBuffer, mBufferInfo.mCrop,
803 mBufferInfo.mTransform, filteringEnabled);
804}
805
chaviwb4c6e582019-08-16 14:35:07 -0700806void BufferLayer::setInitialValuesForClone(const sp<Layer>& clonedFrom) {
807 Layer::setInitialValuesForClone(clonedFrom);
808
809 sp<BufferLayer> bufferClonedFrom = static_cast<BufferLayer*>(clonedFrom.get());
810 mPremultipliedAlpha = bufferClonedFrom->mPremultipliedAlpha;
811 mPotentialCursor = bufferClonedFrom->mPotentialCursor;
812 mProtectedByApp = bufferClonedFrom->mProtectedByApp;
chaviw74b03172019-08-19 11:09:03 -0700813
814 updateCloneBufferInfo();
815}
816
817void BufferLayer::updateCloneBufferInfo() {
818 if (!isClone() || !isClonedFromAlive()) {
819 return;
820 }
821
822 sp<BufferLayer> clonedFrom = static_cast<BufferLayer*>(getClonedFrom().get());
823 mBufferInfo = clonedFrom->mBufferInfo;
824 mSidebandStream = clonedFrom->mSidebandStream;
825 surfaceDamageRegion = clonedFrom->surfaceDamageRegion;
826 mCurrentFrameNumber = clonedFrom->mCurrentFrameNumber.load();
827 mPreviousFrameNumber = clonedFrom->mPreviousFrameNumber;
828
829 // After buffer info is updated, the drawingState from the real layer needs to be copied into
830 // the cloned. This is because some properties of drawingState can change when latchBuffer is
chaviwaf87b3e2019-10-01 16:59:28 -0700831 // called. However, copying the drawingState would also overwrite the cloned layer's relatives
832 // and touchableRegionCrop. Therefore, temporarily store the relatives so they can be set in
833 // the cloned drawingState again.
chaviw74b03172019-08-19 11:09:03 -0700834 wp<Layer> tmpZOrderRelativeOf = mDrawingState.zOrderRelativeOf;
835 SortedVector<wp<Layer>> tmpZOrderRelatives = mDrawingState.zOrderRelatives;
chaviwaf87b3e2019-10-01 16:59:28 -0700836 wp<Layer> tmpTouchableRegionCrop = mDrawingState.touchableRegionCrop;
837 InputWindowInfo tmpInputInfo = mDrawingState.inputInfo;
838
chaviw74b03172019-08-19 11:09:03 -0700839 mDrawingState = clonedFrom->mDrawingState;
chaviwaf87b3e2019-10-01 16:59:28 -0700840
841 mDrawingState.touchableRegionCrop = tmpTouchableRegionCrop;
chaviw74b03172019-08-19 11:09:03 -0700842 mDrawingState.zOrderRelativeOf = tmpZOrderRelativeOf;
843 mDrawingState.zOrderRelatives = tmpZOrderRelatives;
chaviwaf87b3e2019-10-01 16:59:28 -0700844 mDrawingState.inputInfo = tmpInputInfo;
chaviwb4c6e582019-08-16 14:35:07 -0700845}
846
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700847void BufferLayer::setTransformHint(ui::Transform::RotationFlags displayTransformHint) {
Vishnu Nair6213bd92020-05-08 17:42:25 -0700848 mTransformHint = getFixedTransformHint();
849 if (mTransformHint == ui::Transform::ROT_INVALID) {
850 mTransformHint = displayTransformHint;
851 }
852}
853
Vishnu Naire7f79c52020-10-29 14:45:03 -0700854bool BufferLayer::bufferNeedsFiltering() const {
855 return isFixedSize();
856}
857
David Sodman0c69cad2017-08-21 12:12:51 -0700858} // namespace android
859
860#if defined(__gl_h_)
861#error "don't include gl/gl.h in this file"
862#endif
863
864#if defined(__gl2_h_)
865#error "don't include gl2/gl2.h in this file"
866#endif
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800867
868// TODO(b/129481165): remove the #pragma below and fix conversion issues
869#pragma clang diagnostic pop // ignored "-Wconversion"