blob: 8f1aef06f27dd54c4e61c3bf36dc54048b375dae [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) ||
Lloyd Piquef16688f2019-02-19 17:47:57 -0800189 (isSecure() && !targetSettings.isSecure);
Ady Abraham7a60afb2021-03-29 13:20:55 -0700190 const bool bufferCanBeUsedAsHwTexture =
191 mBufferInfo.mBuffer->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;
216 layer.bufferId = mBufferInfo.mBuffer ? mBufferInfo.mBuffer->getId() : 0;
217
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
317 compositionState->buffer = mBufferInfo.mBuffer;
318 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}
332
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700333bool BufferLayer::onPostComposition(const DisplayDevice* display,
Dominik Laskowski075d3172018-05-24 15:50:06 -0700334 const std::shared_ptr<FenceTime>& glDoneFence,
Marissa Wallfd668622018-05-10 10:21:13 -0700335 const std::shared_ptr<FenceTime>& presentFence,
336 const CompositorTiming& compositorTiming) {
337 // mFrameLatencyNeeded is true when a new frame was latched for the
338 // composition.
chaviw74b03172019-08-19 11:09:03 -0700339 if (!mBufferInfo.mFrameLatencyNeeded) return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700340
341 // Update mFrameEventHistory.
Dan Stoza436ccf32018-06-21 12:10:12 -0700342 {
Marissa Wallfd668622018-05-10 10:21:13 -0700343 Mutex::Autolock lock(mFrameEventHistoryMutex);
344 mFrameEventHistory.addPostComposition(mCurrentFrameNumber, glDoneFence, presentFence,
345 compositorTiming);
Valerie Hau93d5a5e2020-02-13 14:50:01 -0800346 finalizeFrameEventHistory(glDoneFence, compositorTiming);
David Sodman0c69cad2017-08-21 12:12:51 -0700347 }
348
Marissa Wallfd668622018-05-10 10:21:13 -0700349 // Update mFrameTracker.
chaviw4244e032019-09-04 11:27:49 -0700350 nsecs_t desiredPresentTime = mBufferInfo.mDesiredPresentTime;
Marissa Wallfd668622018-05-10 10:21:13 -0700351 mFrameTracker.setDesiredPresentTime(desiredPresentTime);
352
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800353 const int32_t layerId = getSequence();
354 mFlinger->mTimeStats->setDesiredTime(layerId, mCurrentFrameNumber, desiredPresentTime);
Marissa Wallfd668622018-05-10 10:21:13 -0700355
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700356 const auto outputLayer = findOutputLayerForDisplay(display);
Adithya Srinivasanb69e0762019-11-11 18:39:53 -0800357 if (outputLayer && outputLayer->requiresClientComposition()) {
358 nsecs_t clientCompositionTimestamp = outputLayer->getState().clientCompositionTimestamp;
359 mFlinger->mFrameTracer->traceTimestamp(layerId, getCurrentBufferId(), mCurrentFrameNumber,
360 clientCompositionTimestamp,
361 FrameTracer::FrameEvent::FALLBACK_COMPOSITION);
Adithya Srinivasanb6a2fa12021-03-13 00:23:09 +0000362 // Update the SurfaceFrames in the drawing state
363 if (mDrawingState.bufferSurfaceFrameTX) {
364 mDrawingState.bufferSurfaceFrameTX->setGpuComposition();
365 }
366 for (auto& [token, surfaceFrame] : mDrawingState.bufferlessSurfaceFramesTX) {
367 surfaceFrame->setGpuComposition();
368 }
Adithya Srinivasanb69e0762019-11-11 18:39:53 -0800369 }
370
chaviw4244e032019-09-04 11:27:49 -0700371 std::shared_ptr<FenceTime> frameReadyFence = mBufferInfo.mFenceTime;
Marissa Wallfd668622018-05-10 10:21:13 -0700372 if (frameReadyFence->isValid()) {
373 mFrameTracker.setFrameReadyFence(std::move(frameReadyFence));
374 } else {
375 // There was no fence for this frame, so assume that it was ready
376 // to be presented at the desired present time.
377 mFrameTracker.setFrameReadyTime(desiredPresentTime);
Dominik Laskowski45de9bd2018-06-11 17:44:10 -0700378 }
Marissa Wallfd668622018-05-10 10:21:13 -0700379
Alec Mouri7d436ec2021-01-27 20:40:50 -0800380 const Fps refreshRate = mFlinger->mRefreshRateConfigs->getCurrentRefreshRate().getFps();
381 const std::optional<Fps> renderRate = mFlinger->mScheduler->getFrameRateOverride(getOwnerUid());
Marissa Wallfd668622018-05-10 10:21:13 -0700382 if (presentFence->isValid()) {
Alec Mouri7d436ec2021-01-27 20:40:50 -0800383 mFlinger->mTimeStats->setPresentFence(layerId, mCurrentFrameNumber, presentFence,
384 refreshRate, renderRate);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800385 mFlinger->mFrameTracer->traceFence(layerId, getCurrentBufferId(), mCurrentFrameNumber,
Mikael Pessa90092f42019-08-26 17:22:04 -0700386 presentFence, FrameTracer::FrameEvent::PRESENT_FENCE);
Marissa Wallfd668622018-05-10 10:21:13 -0700387 mFrameTracker.setActualPresentFence(std::shared_ptr<FenceTime>(presentFence));
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700388 } else if (!display) {
389 // Do nothing.
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200390 } else if (const auto displayId = PhysicalDisplayId::tryCast(display->getId());
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700391 displayId && mFlinger->getHwComposer().isConnected(*displayId)) {
Marissa Wallfd668622018-05-10 10:21:13 -0700392 // The HWC doesn't support present fences, so use the refresh
393 // timestamp instead.
Marin Shalamanov045b7002021-01-07 16:56:24 +0100394 const nsecs_t actualPresentTime = display->getRefreshTimestamp();
Alec Mouri7d436ec2021-01-27 20:40:50 -0800395 mFlinger->mTimeStats->setPresentTime(layerId, mCurrentFrameNumber, actualPresentTime,
396 refreshRate, renderRate);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800397 mFlinger->mFrameTracer->traceTimestamp(layerId, getCurrentBufferId(), mCurrentFrameNumber,
Mikael Pessa90092f42019-08-26 17:22:04 -0700398 actualPresentTime,
399 FrameTracer::FrameEvent::PRESENT_FENCE);
Marissa Wallfd668622018-05-10 10:21:13 -0700400 mFrameTracker.setActualPresentTime(actualPresentTime);
401 }
402
403 mFrameTracker.advanceFrame();
chaviw74b03172019-08-19 11:09:03 -0700404 mBufferInfo.mFrameLatencyNeeded = false;
Marissa Wallfd668622018-05-10 10:21:13 -0700405 return true;
David Sodman0c69cad2017-08-21 12:12:51 -0700406}
407
chaviwdebadb82020-03-26 14:57:24 -0700408void BufferLayer::gatherBufferInfo() {
409 mBufferInfo.mPixelFormat =
410 !mBufferInfo.mBuffer ? PIXEL_FORMAT_NONE : mBufferInfo.mBuffer->format;
411 mBufferInfo.mFrameLatencyNeeded = true;
412}
413
Ady Abraham63a3e592021-01-06 10:47:15 -0800414bool BufferLayer::shouldPresentNow(nsecs_t expectedPresentTime) const {
415 // If this is not a valid vsync for the layer's uid, return and try again later
416 const bool isVsyncValidForUid =
417 mFlinger->mScheduler->isVsyncValid(expectedPresentTime, mOwnerUid);
418 if (!isVsyncValidForUid) {
419 ATRACE_NAME("!isVsyncValidForUid");
420 return false;
421 }
422
423 // AutoRefresh layers and sideband streams should always be presented
424 if (getSidebandStreamChanged() || getAutoRefresh()) {
425 return true;
426 }
427
Ady Abraham63a3e592021-01-06 10:47:15 -0800428 // If this layer doesn't have a frame is shouldn't be presented
429 if (!hasFrameUpdate()) {
430 return false;
431 }
432
433 // Defer to the derived class to decide whether the next buffer is due for
434 // presentation.
435 return isBufferDue(expectedPresentTime);
436}
437
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700438bool BufferLayer::latchBuffer(bool& recomputeVisibleRegions, nsecs_t latchTime,
439 nsecs_t expectedPresentTime) {
Marissa Wallfd668622018-05-10 10:21:13 -0700440 ATRACE_CALL();
David Sodman0c69cad2017-08-21 12:12:51 -0700441
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800442 bool refreshRequired = latchSidebandStream(recomputeVisibleRegions);
David Sodman0c69cad2017-08-21 12:12:51 -0700443
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800444 if (refreshRequired) {
445 return refreshRequired;
David Sodman0c69cad2017-08-21 12:12:51 -0700446 }
447
Marissa Wallfd668622018-05-10 10:21:13 -0700448 if (!hasReadyFrame()) {
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800449 return false;
David Sodman0c69cad2017-08-21 12:12:51 -0700450 }
David Sodman0c69cad2017-08-21 12:12:51 -0700451
Marissa Wallfd668622018-05-10 10:21:13 -0700452 // if we've already called updateTexImage() without going through
453 // a composition step, we have to skip this layer at this point
454 // because we cannot call updateTeximage() without a corresponding
455 // compositionComplete() call.
456 // we'll trigger an update in onPreComposition().
457 if (mRefreshPending) {
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800458 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700459 }
460
461 // If the head buffer's acquire fence hasn't signaled yet, return and
462 // try again later
463 if (!fenceHasSignaled()) {
Ady Abraham09bd3922019-04-08 10:44:56 -0700464 ATRACE_NAME("!fenceHasSignaled()");
David Sodman0c69cad2017-08-21 12:12:51 -0700465 mFlinger->signalLayerUpdate();
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800466 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700467 }
468
469 // Capture the old state of the layer for comparisons later
470 const State& s(getDrawingState());
471 const bool oldOpacity = isOpaque(s);
chaviwd62d3062019-09-04 14:48:02 -0700472
473 BufferInfo oldBufferInfo = mBufferInfo;
Marissa Wallfd668622018-05-10 10:21:13 -0700474
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700475 if (!allTransactionsSignaled(expectedPresentTime)) {
Marissa Wallebb486e2019-05-15 14:08:08 -0700476 mFlinger->setTransactionFlags(eTraversalNeeded);
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800477 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700478 }
479
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700480 status_t err = updateTexImage(recomputeVisibleRegions, latchTime, expectedPresentTime);
Marissa Wallfd668622018-05-10 10:21:13 -0700481 if (err != NO_ERROR) {
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800482 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700483 }
484
485 err = updateActiveBuffer();
486 if (err != NO_ERROR) {
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800487 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700488 }
489
Marissa Wallfd668622018-05-10 10:21:13 -0700490 err = updateFrameNumber(latchTime);
491 if (err != NO_ERROR) {
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800492 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700493 }
494
chaviw4244e032019-09-04 11:27:49 -0700495 gatherBufferInfo();
496
Marissa Wallfd668622018-05-10 10:21:13 -0700497 mRefreshPending = true;
chaviwd62d3062019-09-04 14:48:02 -0700498 if (oldBufferInfo.mBuffer == nullptr) {
Marissa Wallfd668622018-05-10 10:21:13 -0700499 // the first time we receive a buffer, we need to trigger a
500 // geometry invalidation.
501 recomputeVisibleRegions = true;
502 }
503
chaviw4244e032019-09-04 11:27:49 -0700504 if ((mBufferInfo.mCrop != oldBufferInfo.mCrop) ||
505 (mBufferInfo.mTransform != oldBufferInfo.mTransform) ||
506 (mBufferInfo.mScaleMode != oldBufferInfo.mScaleMode) ||
507 (mBufferInfo.mTransformToDisplayInverse != oldBufferInfo.mTransformToDisplayInverse)) {
Marissa Wallfd668622018-05-10 10:21:13 -0700508 recomputeVisibleRegions = true;
509 }
510
chaviwd62d3062019-09-04 14:48:02 -0700511 if (oldBufferInfo.mBuffer != nullptr) {
512 uint32_t bufWidth = mBufferInfo.mBuffer->getWidth();
513 uint32_t bufHeight = mBufferInfo.mBuffer->getHeight();
514 if (bufWidth != uint32_t(oldBufferInfo.mBuffer->width) ||
515 bufHeight != uint32_t(oldBufferInfo.mBuffer->height)) {
Marissa Wallfd668622018-05-10 10:21:13 -0700516 recomputeVisibleRegions = true;
517 }
518 }
519
520 if (oldOpacity != isOpaque(s)) {
521 recomputeVisibleRegions = true;
522 }
523
524 // Remove any sync points corresponding to the buffer which was just
525 // latched
526 {
527 Mutex::Autolock lock(mLocalSyncPointMutex);
528 auto point = mLocalSyncPoints.begin();
529 while (point != mLocalSyncPoints.end()) {
530 if (!(*point)->frameIsAvailable() || !(*point)->transactionIsApplied()) {
531 // This sync point must have been added since we started
532 // latching. Don't drop it yet.
533 ++point;
534 continue;
535 }
536
537 if ((*point)->getFrameNumber() <= mCurrentFrameNumber) {
Alec Mourie60041e2019-06-14 18:59:51 -0700538 std::stringstream ss;
539 ss << "Dropping sync point " << (*point)->getFrameNumber();
540 ATRACE_NAME(ss.str().c_str());
Marissa Wallfd668622018-05-10 10:21:13 -0700541 point = mLocalSyncPoints.erase(point);
542 } else {
543 ++point;
544 }
545 }
546 }
547
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800548 return true;
Marissa Wallfd668622018-05-10 10:21:13 -0700549}
550
551// transaction
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700552void BufferLayer::notifyAvailableFrames(nsecs_t expectedPresentTime) {
553 const auto headFrameNumber = getHeadFrameNumber(expectedPresentTime);
Ady Abrahamcd1580c2019-04-29 15:40:03 -0700554 const bool headFenceSignaled = fenceHasSignaled();
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700555 const bool presentTimeIsCurrent = framePresentTimeIsCurrent(expectedPresentTime);
Marissa Wallfd668622018-05-10 10:21:13 -0700556 Mutex::Autolock lock(mLocalSyncPointMutex);
557 for (auto& point : mLocalSyncPoints) {
Ady Abrahamcd1580c2019-04-29 15:40:03 -0700558 if (headFrameNumber >= point->getFrameNumber() && headFenceSignaled &&
559 presentTimeIsCurrent) {
Marissa Wallfd668622018-05-10 10:21:13 -0700560 point->setFrameAvailable();
chaviw43cb3cb2019-05-31 15:23:41 -0700561 sp<Layer> requestedSyncLayer = point->getRequestedSyncLayer();
562 if (requestedSyncLayer) {
563 // Need to update the transaction flag to ensure the layer's pending transaction
564 // gets applied.
565 requestedSyncLayer->setTransactionFlags(eTransactionNeeded);
566 }
Marissa Wallfd668622018-05-10 10:21:13 -0700567 }
David Sodman0c69cad2017-08-21 12:12:51 -0700568 }
569}
570
Marissa Wallfd668622018-05-10 10:21:13 -0700571bool BufferLayer::hasReadyFrame() const {
Marissa Wall024a1912018-08-13 13:55:35 -0700572 return hasFrameUpdate() || getSidebandStreamChanged() || getAutoRefresh();
Marissa Wallfd668622018-05-10 10:21:13 -0700573}
574
575uint32_t BufferLayer::getEffectiveScalingMode() const {
chaviw4244e032019-09-04 11:27:49 -0700576 return mBufferInfo.mScaleMode;
Marissa Wallfd668622018-05-10 10:21:13 -0700577}
578
579bool BufferLayer::isProtected() const {
chaviwd62d3062019-09-04 14:48:02 -0700580 const sp<GraphicBuffer>& buffer(mBufferInfo.mBuffer);
Marissa Wallfd668622018-05-10 10:21:13 -0700581 return (buffer != 0) && (buffer->getUsage() & GRALLOC_USAGE_PROTECTED);
582}
583
Marissa Wallfd668622018-05-10 10:21:13 -0700584// h/w composer set-up
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700585bool BufferLayer::allTransactionsSignaled(nsecs_t expectedPresentTime) {
586 const auto headFrameNumber = getHeadFrameNumber(expectedPresentTime);
Marissa Wallfd668622018-05-10 10:21:13 -0700587 bool matchingFramesFound = false;
588 bool allTransactionsApplied = true;
589 Mutex::Autolock lock(mLocalSyncPointMutex);
590
591 for (auto& point : mLocalSyncPoints) {
592 if (point->getFrameNumber() > headFrameNumber) {
593 break;
594 }
595 matchingFramesFound = true;
596
597 if (!point->frameIsAvailable()) {
598 // We haven't notified the remote layer that the frame for
599 // this point is available yet. Notify it now, and then
600 // abort this attempt to latch.
601 point->setFrameAvailable();
602 allTransactionsApplied = false;
603 break;
604 }
605
606 allTransactionsApplied = allTransactionsApplied && point->transactionIsApplied();
607 }
608 return !matchingFramesFound || allTransactionsApplied;
David Sodman0c69cad2017-08-21 12:12:51 -0700609}
610
611// As documented in libhardware header, formats in the range
612// 0x100 - 0x1FF are specific to the HAL implementation, and
613// are known to have no alpha channel
614// TODO: move definition for device-specific range into
615// hardware.h, instead of using hard-coded values here.
616#define HARDWARE_IS_DEVICE_FORMAT(f) ((f) >= 0x100 && (f) <= 0x1FF)
617
618bool BufferLayer::getOpacityForFormat(uint32_t format) {
619 if (HARDWARE_IS_DEVICE_FORMAT(format)) {
620 return true;
621 }
622 switch (format) {
623 case HAL_PIXEL_FORMAT_RGBA_8888:
624 case HAL_PIXEL_FORMAT_BGRA_8888:
625 case HAL_PIXEL_FORMAT_RGBA_FP16:
626 case HAL_PIXEL_FORMAT_RGBA_1010102:
627 return false;
628 }
629 // in all other case, we have no blending (also for unknown formats)
630 return true;
631}
632
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700633bool BufferLayer::needsFiltering(const DisplayDevice* display) const {
634 const auto outputLayer = findOutputLayerForDisplay(display);
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800635 if (outputLayer == nullptr) {
Lloyd Piquef16688f2019-02-19 17:47:57 -0800636 return false;
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800637 }
638
Lloyd Piquef16688f2019-02-19 17:47:57 -0800639 // We need filtering if the sourceCrop rectangle size does not match the
640 // displayframe rectangle size (not a 1:1 render)
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800641 const auto& compositionState = outputLayer->getState();
642 const auto displayFrame = compositionState.displayFrame;
643 const auto sourceCrop = compositionState.sourceCrop;
Lloyd Piquef16688f2019-02-19 17:47:57 -0800644 return sourceCrop.getHeight() != displayFrame.getHeight() ||
Peiyong Linc2020ca2019-01-10 11:36:12 -0800645 sourceCrop.getWidth() != displayFrame.getWidth();
Chia-I Wu692e0832018-06-05 15:46:58 -0700646}
647
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700648bool BufferLayer::needsFilteringForScreenshots(const DisplayDevice* display,
Alec Mouri5a6d8572020-03-23 23:56:15 -0700649 const ui::Transform& inverseParentTransform) const {
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700650 const auto outputLayer = findOutputLayerForDisplay(display);
Alec Mouri5a6d8572020-03-23 23:56:15 -0700651 if (outputLayer == nullptr) {
652 return false;
653 }
654
655 // We need filtering if the sourceCrop rectangle size does not match the
656 // viewport rectangle size (not a 1:1 render)
657 const auto& compositionState = outputLayer->getState();
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700658 const ui::Transform& displayTransform = display->getTransform();
Alec Mouri5a6d8572020-03-23 23:56:15 -0700659 const ui::Transform inverseTransform = inverseParentTransform * displayTransform.inverse();
660 // Undo the transformation of the displayFrame so that we're back into
661 // layer-stack space.
662 const Rect frame = inverseTransform.transform(compositionState.displayFrame);
663 const FloatRect sourceCrop = compositionState.sourceCrop;
664
665 int32_t frameHeight = frame.getHeight();
666 int32_t frameWidth = frame.getWidth();
667 // If the display transform had a rotational component then undo the
668 // rotation so that the orientation matches the source crop.
669 if (displayTransform.getOrientation() & ui::Transform::ROT_90) {
670 std::swap(frameHeight, frameWidth);
671 }
672 return sourceCrop.getHeight() != frameHeight || sourceCrop.getWidth() != frameWidth;
673}
674
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700675uint64_t BufferLayer::getHeadFrameNumber(nsecs_t expectedPresentTime) const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800676 if (hasFrameUpdate()) {
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700677 return getFrameNumber(expectedPresentTime);
David Sodman0c69cad2017-08-21 12:12:51 -0700678 } else {
679 return mCurrentFrameNumber;
680 }
681}
682
Vishnu Nair60356342018-11-13 13:00:45 -0800683Rect BufferLayer::getBufferSize(const State& s) const {
684 // If we have a sideband stream, or we are scaling the buffer then return the layer size since
685 // we cannot determine the buffer size.
686 if ((s.sidebandStream != nullptr) ||
687 (getEffectiveScalingMode() != NATIVE_WINDOW_SCALING_MODE_FREEZE)) {
688 return Rect(getActiveWidth(s), getActiveHeight(s));
689 }
690
chaviwd62d3062019-09-04 14:48:02 -0700691 if (mBufferInfo.mBuffer == nullptr) {
Vishnu Nair60356342018-11-13 13:00:45 -0800692 return Rect::INVALID_RECT;
693 }
694
chaviwd62d3062019-09-04 14:48:02 -0700695 uint32_t bufWidth = mBufferInfo.mBuffer->getWidth();
696 uint32_t bufHeight = mBufferInfo.mBuffer->getHeight();
Vishnu Nair60356342018-11-13 13:00:45 -0800697
698 // Undo any transformations on the buffer and return the result.
chaviw4244e032019-09-04 11:27:49 -0700699 if (mBufferInfo.mTransform & ui::Transform::ROT_90) {
Vishnu Nair60356342018-11-13 13:00:45 -0800700 std::swap(bufWidth, bufHeight);
701 }
702
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800703 if (getTransformToDisplayInverse()) {
Dominik Laskowski718f9602019-11-09 20:01:35 -0800704 uint32_t invTransform = DisplayDevice::getPrimaryDisplayRotationFlags();
Vishnu Nair60356342018-11-13 13:00:45 -0800705 if (invTransform & ui::Transform::ROT_90) {
706 std::swap(bufWidth, bufHeight);
707 }
708 }
709
710 return Rect(bufWidth, bufHeight);
711}
712
Vishnu Nair4351ad52019-02-11 14:13:02 -0800713FloatRect BufferLayer::computeSourceBounds(const FloatRect& parentBounds) const {
714 const State& s(getDrawingState());
715
716 // If we have a sideband stream, or we are scaling the buffer then return the layer size since
717 // we cannot determine the buffer size.
718 if ((s.sidebandStream != nullptr) ||
719 (getEffectiveScalingMode() != NATIVE_WINDOW_SCALING_MODE_FREEZE)) {
720 return FloatRect(0, 0, getActiveWidth(s), getActiveHeight(s));
721 }
722
chaviwd62d3062019-09-04 14:48:02 -0700723 if (mBufferInfo.mBuffer == nullptr) {
Vishnu Nair4351ad52019-02-11 14:13:02 -0800724 return parentBounds;
725 }
726
chaviwd62d3062019-09-04 14:48:02 -0700727 uint32_t bufWidth = mBufferInfo.mBuffer->getWidth();
728 uint32_t bufHeight = mBufferInfo.mBuffer->getHeight();
Vishnu Nair4351ad52019-02-11 14:13:02 -0800729
730 // Undo any transformations on the buffer and return the result.
chaviw4244e032019-09-04 11:27:49 -0700731 if (mBufferInfo.mTransform & ui::Transform::ROT_90) {
Vishnu Nair4351ad52019-02-11 14:13:02 -0800732 std::swap(bufWidth, bufHeight);
733 }
734
735 if (getTransformToDisplayInverse()) {
Dominik Laskowski718f9602019-11-09 20:01:35 -0800736 uint32_t invTransform = DisplayDevice::getPrimaryDisplayRotationFlags();
Vishnu Nair4351ad52019-02-11 14:13:02 -0800737 if (invTransform & ui::Transform::ROT_90) {
738 std::swap(bufWidth, bufHeight);
739 }
740 }
741
742 return FloatRect(0, 0, bufWidth, bufHeight);
743}
744
chaviw49a108c2019-08-12 11:23:06 -0700745void BufferLayer::latchAndReleaseBuffer() {
746 mRefreshPending = false;
747 if (hasReadyFrame()) {
748 bool ignored = false;
749 latchBuffer(ignored, systemTime(), 0 /* expectedPresentTime */);
750 }
751 releasePendingBuffer(systemTime());
752}
753
chaviw4244e032019-09-04 11:27:49 -0700754PixelFormat BufferLayer::getPixelFormat() const {
755 return mBufferInfo.mPixelFormat;
756}
757
758bool BufferLayer::getTransformToDisplayInverse() const {
759 return mBufferInfo.mTransformToDisplayInverse;
760}
761
762Rect BufferLayer::getBufferCrop() const {
763 // this is the crop rectangle that applies to the buffer
764 // itself (as opposed to the window)
765 if (!mBufferInfo.mCrop.isEmpty()) {
766 // if the buffer crop is defined, we use that
767 return mBufferInfo.mCrop;
chaviwd62d3062019-09-04 14:48:02 -0700768 } else if (mBufferInfo.mBuffer != nullptr) {
chaviw4244e032019-09-04 11:27:49 -0700769 // otherwise we use the whole buffer
chaviwd62d3062019-09-04 14:48:02 -0700770 return mBufferInfo.mBuffer->getBounds();
chaviw4244e032019-09-04 11:27:49 -0700771 } else {
772 // if we don't have a buffer yet, we use an empty/invalid crop
773 return Rect();
774 }
775}
776
777uint32_t BufferLayer::getBufferTransform() const {
778 return mBufferInfo.mTransform;
779}
780
781ui::Dataspace BufferLayer::getDataSpace() const {
782 return mBufferInfo.mDataspace;
783}
784
785ui::Dataspace BufferLayer::translateDataspace(ui::Dataspace dataspace) {
786 ui::Dataspace updatedDataspace = dataspace;
787 // translate legacy dataspaces to modern dataspaces
788 switch (dataspace) {
789 case ui::Dataspace::SRGB:
790 updatedDataspace = ui::Dataspace::V0_SRGB;
791 break;
792 case ui::Dataspace::SRGB_LINEAR:
793 updatedDataspace = ui::Dataspace::V0_SRGB_LINEAR;
794 break;
795 case ui::Dataspace::JFIF:
796 updatedDataspace = ui::Dataspace::V0_JFIF;
797 break;
798 case ui::Dataspace::BT601_625:
799 updatedDataspace = ui::Dataspace::V0_BT601_625;
800 break;
801 case ui::Dataspace::BT601_525:
802 updatedDataspace = ui::Dataspace::V0_BT601_525;
803 break;
804 case ui::Dataspace::BT709:
805 updatedDataspace = ui::Dataspace::V0_BT709;
806 break;
807 default:
808 break;
809 }
810
811 return updatedDataspace;
812}
813
chaviwd62d3062019-09-04 14:48:02 -0700814sp<GraphicBuffer> BufferLayer::getBuffer() const {
815 return mBufferInfo.mBuffer;
816}
817
chaviwf83ce182019-09-12 14:43:08 -0700818void BufferLayer::getDrawingTransformMatrix(bool filteringEnabled, float outMatrix[16]) {
819 GLConsumer::computeTransformMatrix(outMatrix, mBufferInfo.mBuffer, mBufferInfo.mCrop,
820 mBufferInfo.mTransform, filteringEnabled);
821}
822
chaviwb4c6e582019-08-16 14:35:07 -0700823void BufferLayer::setInitialValuesForClone(const sp<Layer>& clonedFrom) {
824 Layer::setInitialValuesForClone(clonedFrom);
825
826 sp<BufferLayer> bufferClonedFrom = static_cast<BufferLayer*>(clonedFrom.get());
827 mPremultipliedAlpha = bufferClonedFrom->mPremultipliedAlpha;
828 mPotentialCursor = bufferClonedFrom->mPotentialCursor;
829 mProtectedByApp = bufferClonedFrom->mProtectedByApp;
chaviw74b03172019-08-19 11:09:03 -0700830
831 updateCloneBufferInfo();
832}
833
834void BufferLayer::updateCloneBufferInfo() {
835 if (!isClone() || !isClonedFromAlive()) {
836 return;
837 }
838
839 sp<BufferLayer> clonedFrom = static_cast<BufferLayer*>(getClonedFrom().get());
840 mBufferInfo = clonedFrom->mBufferInfo;
841 mSidebandStream = clonedFrom->mSidebandStream;
842 surfaceDamageRegion = clonedFrom->surfaceDamageRegion;
843 mCurrentFrameNumber = clonedFrom->mCurrentFrameNumber.load();
844 mPreviousFrameNumber = clonedFrom->mPreviousFrameNumber;
845
846 // After buffer info is updated, the drawingState from the real layer needs to be copied into
847 // the cloned. This is because some properties of drawingState can change when latchBuffer is
chaviwaf87b3e2019-10-01 16:59:28 -0700848 // called. However, copying the drawingState would also overwrite the cloned layer's relatives
849 // and touchableRegionCrop. Therefore, temporarily store the relatives so they can be set in
850 // the cloned drawingState again.
chaviw74b03172019-08-19 11:09:03 -0700851 wp<Layer> tmpZOrderRelativeOf = mDrawingState.zOrderRelativeOf;
852 SortedVector<wp<Layer>> tmpZOrderRelatives = mDrawingState.zOrderRelatives;
chaviwaf87b3e2019-10-01 16:59:28 -0700853 wp<Layer> tmpTouchableRegionCrop = mDrawingState.touchableRegionCrop;
854 InputWindowInfo tmpInputInfo = mDrawingState.inputInfo;
855
chaviw74b03172019-08-19 11:09:03 -0700856 mDrawingState = clonedFrom->mDrawingState;
chaviwaf87b3e2019-10-01 16:59:28 -0700857
858 mDrawingState.touchableRegionCrop = tmpTouchableRegionCrop;
chaviw74b03172019-08-19 11:09:03 -0700859 mDrawingState.zOrderRelativeOf = tmpZOrderRelativeOf;
860 mDrawingState.zOrderRelatives = tmpZOrderRelatives;
chaviwaf87b3e2019-10-01 16:59:28 -0700861 mDrawingState.inputInfo = tmpInputInfo;
chaviwb4c6e582019-08-16 14:35:07 -0700862}
863
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700864void BufferLayer::setTransformHint(ui::Transform::RotationFlags displayTransformHint) {
Vishnu Nair6213bd92020-05-08 17:42:25 -0700865 mTransformHint = getFixedTransformHint();
866 if (mTransformHint == ui::Transform::ROT_INVALID) {
867 mTransformHint = displayTransformHint;
868 }
869}
870
Vishnu Naire7f79c52020-10-29 14:45:03 -0700871bool BufferLayer::bufferNeedsFiltering() const {
872 return isFixedSize();
873}
874
David Sodman0c69cad2017-08-21 12:12:51 -0700875} // namespace android
876
877#if defined(__gl_h_)
878#error "don't include gl/gl.h in this file"
879#endif
880
881#if defined(__gl2_h_)
882#error "don't include gl2/gl2.h in this file"
883#endif
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800884
885// TODO(b/129481165): remove the #pragma below and fix conversion issues
886#pragma clang diagnostic pop // ignored "-Wconversion"