blob: b6764d02d2f18c0d4087e30fd8050a0ac1b4eb0d [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 }
Lloyd Pique688abd42019-02-15 15:42:24 -0800188 bool blackOutLayer = (isProtected() && !targetSettings.supportsProtectedContent) ||
Lloyd Piquef16688f2019-02-19 17:47:57 -0800189 (isSecure() && !targetSettings.isSecure);
Lloyd Piquede196652020-01-22 17:29:58 -0800190 compositionengine::LayerFE::LayerSettings& layer = *result;
Vishnu Nairb87d94f2020-02-13 09:17:36 -0800191 if (blackOutLayer) {
192 prepareClearClientComposition(layer, true /* blackout */);
193 return layer;
David Sodman0c69cad2017-08-21 12:12:51 -0700194 }
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000195
Vishnu Nairb87d94f2020-02-13 09:17:36 -0800196 const State& s(getDrawingState());
197 layer.source.buffer.buffer = mBufferInfo.mBuffer;
198 layer.source.buffer.isOpaque = isOpaque(s);
199 layer.source.buffer.fence = mBufferInfo.mFence;
200 layer.source.buffer.textureName = mTextureName;
201 layer.source.buffer.usePremultipliedAlpha = getPremultipledAlpha();
202 layer.source.buffer.isY410BT2020 = isHdrY410();
203 bool hasSmpte2086 = mBufferInfo.mHdrMetadata.validTypes & HdrMetadata::SMPTE2086;
204 bool hasCta861_3 = mBufferInfo.mHdrMetadata.validTypes & HdrMetadata::CTA861_3;
205 layer.source.buffer.maxMasteringLuminance = hasSmpte2086
206 ? mBufferInfo.mHdrMetadata.smpte2086.maxLuminance
207 : defaultMaxMasteringLuminance;
208 layer.source.buffer.maxContentLuminance = hasCta861_3
209 ? mBufferInfo.mHdrMetadata.cta8613.maxContentLightLevel
210 : defaultMaxContentLuminance;
211 layer.frameNumber = mCurrentFrameNumber;
212 layer.bufferId = mBufferInfo.mBuffer ? mBufferInfo.mBuffer->getId() : 0;
213
Vishnu Naire7f79c52020-10-29 14:45:03 -0700214 const bool useFiltering =
215 targetSettings.needsFiltering || mNeedsFiltering || bufferNeedsFiltering();
Vishnu Nairb87d94f2020-02-13 09:17:36 -0800216
217 // Query the texture matrix given our current filtering mode.
218 float textureMatrix[16];
219 getDrawingTransformMatrix(useFiltering, textureMatrix);
220
221 if (getTransformToDisplayInverse()) {
222 /*
223 * the code below applies the primary display's inverse transform to
224 * the texture transform
225 */
226 uint32_t transform = DisplayDevice::getPrimaryDisplayRotationFlags();
227 mat4 tr = inverseOrientation(transform);
228
229 /**
230 * TODO(b/36727915): This is basically a hack.
231 *
232 * Ensure that regardless of the parent transformation,
233 * this buffer is always transformed from native display
234 * orientation to display orientation. For example, in the case
235 * of a camera where the buffer remains in native orientation,
236 * we want the pixels to always be upright.
237 */
238 sp<Layer> p = mDrawingParent.promote();
239 if (p != nullptr) {
240 const auto parentTransform = p->getTransform();
241 tr = tr * inverseOrientation(parentTransform.getOrientation());
242 }
243
244 // and finally apply it to the original texture matrix
245 const mat4 texTransform(mat4(static_cast<const float*>(textureMatrix)) * tr);
246 memcpy(textureMatrix, texTransform.asArray(), sizeof(textureMatrix));
247 }
248
249 const Rect win{getBounds()};
250 float bufferWidth = getBufferSize(s).getWidth();
251 float bufferHeight = getBufferSize(s).getHeight();
252
253 // BufferStateLayers can have a "buffer size" of [0, 0, -1, -1] when no display frame has
254 // been set and there is no parent layer bounds. In that case, the scale is meaningless so
255 // ignore them.
256 if (!getBufferSize(s).isValid()) {
257 bufferWidth = float(win.right) - float(win.left);
258 bufferHeight = float(win.bottom) - float(win.top);
259 }
260
261 const float scaleHeight = (float(win.bottom) - float(win.top)) / bufferHeight;
262 const float scaleWidth = (float(win.right) - float(win.left)) / bufferWidth;
263 const float translateY = float(win.top) / bufferHeight;
264 const float translateX = float(win.left) / bufferWidth;
265
266 // Flip y-coordinates because GLConsumer expects OpenGL convention.
267 mat4 tr = mat4::translate(vec4(.5, .5, 0, 1)) * mat4::scale(vec4(1, -1, 1, 1)) *
268 mat4::translate(vec4(-.5, -.5, 0, 1)) *
269 mat4::translate(vec4(translateX, translateY, 0, 1)) *
270 mat4::scale(vec4(scaleWidth, scaleHeight, 1.0, 1.0));
271
272 layer.source.buffer.useTextureFiltering = useFiltering;
273 layer.source.buffer.textureTransform = mat4(static_cast<const float*>(textureMatrix)) * tr;
274
Vishnu Nair9b079a22020-01-21 14:36:08 -0800275 return layer;
David Sodman0c69cad2017-08-21 12:12:51 -0700276}
277
Marissa Wallfd668622018-05-10 10:21:13 -0700278bool BufferLayer::isHdrY410() const {
279 // pixel format is HDR Y410 masquerading as RGBA_1010102
chaviw4244e032019-09-04 11:27:49 -0700280 return (mBufferInfo.mDataspace == ui::Dataspace::BT2020_ITU_PQ &&
281 mBufferInfo.mApi == NATIVE_WINDOW_API_MEDIA &&
chaviwdebadb82020-03-26 14:57:24 -0700282 mBufferInfo.mPixelFormat == HAL_PIXEL_FORMAT_RGBA_1010102);
David Sodmaneb085e02017-10-05 18:49:04 -0700283}
284
Lloyd Piquede196652020-01-22 17:29:58 -0800285sp<compositionengine::LayerFE> BufferLayer::getCompositionEngineLayerFE() const {
286 return asLayerFE();
287}
288
289compositionengine::LayerFECompositionState* BufferLayer::editCompositionState() {
290 return mCompositionState.get();
291}
292
293const compositionengine::LayerFECompositionState* BufferLayer::getCompositionState() const {
294 return mCompositionState.get();
295}
296
297void BufferLayer::preparePerFrameCompositionState() {
298 Layer::preparePerFrameCompositionState();
David Sodman0c69cad2017-08-21 12:12:51 -0700299
300 // Sideband layers
Lloyd Piquede196652020-01-22 17:29:58 -0800301 auto* compositionState = editCompositionState();
302 if (compositionState->sidebandStream.get()) {
303 compositionState->compositionType = Hwc2::IComposerClient::Composition::SIDEBAND;
Robert Carra6bb2bc2020-04-08 11:03:23 -0700304 return;
David Sodman15094112018-10-11 09:39:37 -0700305 } else {
Lloyd Piquef5275482019-01-29 18:42:42 -0800306 // Normal buffer layers
Lloyd Piquede196652020-01-22 17:29:58 -0800307 compositionState->hdrMetadata = mBufferInfo.mHdrMetadata;
308 compositionState->compositionType = mPotentialCursor
Lloyd Piquef5275482019-01-29 18:42:42 -0800309 ? Hwc2::IComposerClient::Composition::CURSOR
310 : Hwc2::IComposerClient::Composition::DEVICE;
David Sodman0c69cad2017-08-21 12:12:51 -0700311 }
Robert Carra6bb2bc2020-04-08 11:03:23 -0700312
313 compositionState->buffer = mBufferInfo.mBuffer;
314 compositionState->bufferSlot = (mBufferInfo.mBufferSlot == BufferQueue::INVALID_BUFFER_SLOT)
315 ? 0
316 : mBufferInfo.mBufferSlot;
317 compositionState->acquireFence = mBufferInfo.mFence;
David Sodman0c69cad2017-08-21 12:12:51 -0700318}
319
Marissa Wallfd668622018-05-10 10:21:13 -0700320bool BufferLayer::onPreComposition(nsecs_t refreshStartTime) {
chaviwd62d3062019-09-04 14:48:02 -0700321 if (mBufferInfo.mBuffer != nullptr) {
Marissa Wallfd668622018-05-10 10:21:13 -0700322 Mutex::Autolock lock(mFrameEventHistoryMutex);
323 mFrameEventHistory.addPreComposition(mCurrentFrameNumber, refreshStartTime);
David Sodman0c69cad2017-08-21 12:12:51 -0700324 }
Marissa Wallfd668622018-05-10 10:21:13 -0700325 mRefreshPending = false;
326 return hasReadyFrame();
David Sodman0c69cad2017-08-21 12:12:51 -0700327}
Ady Abraham8b9e6122021-01-26 19:11:45 -0800328namespace {
329TimeStats::SetFrameRateVote frameRateToSetFrameRateVotePayload(Layer::FrameRate frameRate) {
330 using FrameRateCompatibility = TimeStats::SetFrameRateVote::FrameRateCompatibility;
331 using Seamlessness = TimeStats::SetFrameRateVote::Seamlessness;
332 const auto frameRateCompatibility = [frameRate] {
333 switch (frameRate.type) {
334 case Layer::FrameRateCompatibility::Default:
335 return FrameRateCompatibility::Default;
336 case Layer::FrameRateCompatibility::ExactOrMultiple:
337 return FrameRateCompatibility::ExactOrMultiple;
338 default:
339 return FrameRateCompatibility::Undefined;
340 }
341 }();
342
343 const auto seamlessness = [frameRate] {
344 switch (frameRate.seamlessness) {
345 case scheduler::Seamlessness::OnlySeamless:
346 return Seamlessness::ShouldBeSeamless;
347 case scheduler::Seamlessness::SeamedAndSeamless:
348 return Seamlessness::NotRequired;
349 default:
350 return Seamlessness::Undefined;
351 }
352 }();
353
354 return TimeStats::SetFrameRateVote{.frameRate = frameRate.rate.getValue(),
355 .frameRateCompatibility = frameRateCompatibility,
356 .seamlessness = seamlessness};
357}
358} // namespace
David Sodman0c69cad2017-08-21 12:12:51 -0700359
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700360bool BufferLayer::onPostComposition(const DisplayDevice* display,
Dominik Laskowski075d3172018-05-24 15:50:06 -0700361 const std::shared_ptr<FenceTime>& glDoneFence,
Marissa Wallfd668622018-05-10 10:21:13 -0700362 const std::shared_ptr<FenceTime>& presentFence,
363 const CompositorTiming& compositorTiming) {
364 // mFrameLatencyNeeded is true when a new frame was latched for the
365 // composition.
chaviw74b03172019-08-19 11:09:03 -0700366 if (!mBufferInfo.mFrameLatencyNeeded) return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700367
368 // Update mFrameEventHistory.
Dan Stoza436ccf32018-06-21 12:10:12 -0700369 {
Marissa Wallfd668622018-05-10 10:21:13 -0700370 Mutex::Autolock lock(mFrameEventHistoryMutex);
371 mFrameEventHistory.addPostComposition(mCurrentFrameNumber, glDoneFence, presentFence,
372 compositorTiming);
Valerie Hau93d5a5e2020-02-13 14:50:01 -0800373 finalizeFrameEventHistory(glDoneFence, compositorTiming);
David Sodman0c69cad2017-08-21 12:12:51 -0700374 }
375
Marissa Wallfd668622018-05-10 10:21:13 -0700376 // Update mFrameTracker.
chaviw4244e032019-09-04 11:27:49 -0700377 nsecs_t desiredPresentTime = mBufferInfo.mDesiredPresentTime;
Marissa Wallfd668622018-05-10 10:21:13 -0700378 mFrameTracker.setDesiredPresentTime(desiredPresentTime);
379
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800380 const int32_t layerId = getSequence();
381 mFlinger->mTimeStats->setDesiredTime(layerId, mCurrentFrameNumber, desiredPresentTime);
Marissa Wallfd668622018-05-10 10:21:13 -0700382
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700383 const auto outputLayer = findOutputLayerForDisplay(display);
Adithya Srinivasanb69e0762019-11-11 18:39:53 -0800384 if (outputLayer && outputLayer->requiresClientComposition()) {
385 nsecs_t clientCompositionTimestamp = outputLayer->getState().clientCompositionTimestamp;
386 mFlinger->mFrameTracer->traceTimestamp(layerId, getCurrentBufferId(), mCurrentFrameNumber,
387 clientCompositionTimestamp,
388 FrameTracer::FrameEvent::FALLBACK_COMPOSITION);
Adithya Srinivasanb6a2fa12021-03-13 00:23:09 +0000389 // Update the SurfaceFrames in the drawing state
390 if (mDrawingState.bufferSurfaceFrameTX) {
391 mDrawingState.bufferSurfaceFrameTX->setGpuComposition();
392 }
393 for (auto& [token, surfaceFrame] : mDrawingState.bufferlessSurfaceFramesTX) {
394 surfaceFrame->setGpuComposition();
395 }
Adithya Srinivasanb69e0762019-11-11 18:39:53 -0800396 }
397
chaviw4244e032019-09-04 11:27:49 -0700398 std::shared_ptr<FenceTime> frameReadyFence = mBufferInfo.mFenceTime;
Marissa Wallfd668622018-05-10 10:21:13 -0700399 if (frameReadyFence->isValid()) {
400 mFrameTracker.setFrameReadyFence(std::move(frameReadyFence));
401 } else {
402 // There was no fence for this frame, so assume that it was ready
403 // to be presented at the desired present time.
404 mFrameTracker.setFrameReadyTime(desiredPresentTime);
Dominik Laskowski45de9bd2018-06-11 17:44:10 -0700405 }
Marissa Wallfd668622018-05-10 10:21:13 -0700406
Alec Mouri7d436ec2021-01-27 20:40:50 -0800407 const Fps refreshRate = mFlinger->mRefreshRateConfigs->getCurrentRefreshRate().getFps();
408 const std::optional<Fps> renderRate = mFlinger->mScheduler->getFrameRateOverride(getOwnerUid());
Marissa Wallfd668622018-05-10 10:21:13 -0700409 if (presentFence->isValid()) {
Alec Mouri7d436ec2021-01-27 20:40:50 -0800410 mFlinger->mTimeStats->setPresentFence(layerId, mCurrentFrameNumber, presentFence,
Ady Abraham8b9e6122021-01-26 19:11:45 -0800411 refreshRate, renderRate,
412 frameRateToSetFrameRateVotePayload(
413 mDrawingState.frameRate));
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800414 mFlinger->mFrameTracer->traceFence(layerId, getCurrentBufferId(), mCurrentFrameNumber,
Mikael Pessa90092f42019-08-26 17:22:04 -0700415 presentFence, FrameTracer::FrameEvent::PRESENT_FENCE);
Marissa Wallfd668622018-05-10 10:21:13 -0700416 mFrameTracker.setActualPresentFence(std::shared_ptr<FenceTime>(presentFence));
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700417 } else if (!display) {
418 // Do nothing.
Marin Shalamanov0f10d0d2020-08-06 20:04:06 +0200419 } else if (const auto displayId = PhysicalDisplayId::tryCast(display->getId());
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700420 displayId && mFlinger->getHwComposer().isConnected(*displayId)) {
Marissa Wallfd668622018-05-10 10:21:13 -0700421 // The HWC doesn't support present fences, so use the refresh
422 // timestamp instead.
Marin Shalamanov045b7002021-01-07 16:56:24 +0100423 const nsecs_t actualPresentTime = display->getRefreshTimestamp();
Alec Mouri7d436ec2021-01-27 20:40:50 -0800424 mFlinger->mTimeStats->setPresentTime(layerId, mCurrentFrameNumber, actualPresentTime,
Ady Abraham8b9e6122021-01-26 19:11:45 -0800425 refreshRate, renderRate,
426 frameRateToSetFrameRateVotePayload(
427 mDrawingState.frameRate));
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800428 mFlinger->mFrameTracer->traceTimestamp(layerId, getCurrentBufferId(), mCurrentFrameNumber,
Mikael Pessa90092f42019-08-26 17:22:04 -0700429 actualPresentTime,
430 FrameTracer::FrameEvent::PRESENT_FENCE);
Marissa Wallfd668622018-05-10 10:21:13 -0700431 mFrameTracker.setActualPresentTime(actualPresentTime);
432 }
433
434 mFrameTracker.advanceFrame();
chaviw74b03172019-08-19 11:09:03 -0700435 mBufferInfo.mFrameLatencyNeeded = false;
Marissa Wallfd668622018-05-10 10:21:13 -0700436 return true;
David Sodman0c69cad2017-08-21 12:12:51 -0700437}
438
chaviwdebadb82020-03-26 14:57:24 -0700439void BufferLayer::gatherBufferInfo() {
440 mBufferInfo.mPixelFormat =
441 !mBufferInfo.mBuffer ? PIXEL_FORMAT_NONE : mBufferInfo.mBuffer->format;
442 mBufferInfo.mFrameLatencyNeeded = true;
443}
444
Ady Abraham63a3e592021-01-06 10:47:15 -0800445bool BufferLayer::shouldPresentNow(nsecs_t expectedPresentTime) const {
446 // If this is not a valid vsync for the layer's uid, return and try again later
447 const bool isVsyncValidForUid =
448 mFlinger->mScheduler->isVsyncValid(expectedPresentTime, mOwnerUid);
449 if (!isVsyncValidForUid) {
450 ATRACE_NAME("!isVsyncValidForUid");
451 return false;
452 }
453
454 // AutoRefresh layers and sideband streams should always be presented
455 if (getSidebandStreamChanged() || getAutoRefresh()) {
456 return true;
457 }
458
Ady Abraham63a3e592021-01-06 10:47:15 -0800459 // If this layer doesn't have a frame is shouldn't be presented
460 if (!hasFrameUpdate()) {
461 return false;
462 }
463
464 // Defer to the derived class to decide whether the next buffer is due for
465 // presentation.
466 return isBufferDue(expectedPresentTime);
467}
468
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700469bool BufferLayer::latchBuffer(bool& recomputeVisibleRegions, nsecs_t latchTime,
470 nsecs_t expectedPresentTime) {
Marissa Wallfd668622018-05-10 10:21:13 -0700471 ATRACE_CALL();
David Sodman0c69cad2017-08-21 12:12:51 -0700472
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800473 bool refreshRequired = latchSidebandStream(recomputeVisibleRegions);
David Sodman0c69cad2017-08-21 12:12:51 -0700474
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800475 if (refreshRequired) {
476 return refreshRequired;
David Sodman0c69cad2017-08-21 12:12:51 -0700477 }
478
Marissa Wallfd668622018-05-10 10:21:13 -0700479 if (!hasReadyFrame()) {
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800480 return false;
David Sodman0c69cad2017-08-21 12:12:51 -0700481 }
David Sodman0c69cad2017-08-21 12:12:51 -0700482
Marissa Wallfd668622018-05-10 10:21:13 -0700483 // if we've already called updateTexImage() without going through
484 // a composition step, we have to skip this layer at this point
485 // because we cannot call updateTeximage() without a corresponding
486 // compositionComplete() call.
487 // we'll trigger an update in onPreComposition().
488 if (mRefreshPending) {
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800489 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700490 }
491
492 // If the head buffer's acquire fence hasn't signaled yet, return and
493 // try again later
494 if (!fenceHasSignaled()) {
Ady Abraham09bd3922019-04-08 10:44:56 -0700495 ATRACE_NAME("!fenceHasSignaled()");
David Sodman0c69cad2017-08-21 12:12:51 -0700496 mFlinger->signalLayerUpdate();
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800497 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700498 }
499
500 // Capture the old state of the layer for comparisons later
501 const State& s(getDrawingState());
502 const bool oldOpacity = isOpaque(s);
chaviwd62d3062019-09-04 14:48:02 -0700503
504 BufferInfo oldBufferInfo = mBufferInfo;
Marissa Wallfd668622018-05-10 10:21:13 -0700505
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700506 if (!allTransactionsSignaled(expectedPresentTime)) {
Marissa Wallebb486e2019-05-15 14:08:08 -0700507 mFlinger->setTransactionFlags(eTraversalNeeded);
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800508 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700509 }
510
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700511 status_t err = updateTexImage(recomputeVisibleRegions, latchTime, expectedPresentTime);
Marissa Wallfd668622018-05-10 10:21:13 -0700512 if (err != NO_ERROR) {
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800513 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700514 }
515
516 err = updateActiveBuffer();
517 if (err != NO_ERROR) {
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800518 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700519 }
520
Marissa Wallfd668622018-05-10 10:21:13 -0700521 err = updateFrameNumber(latchTime);
522 if (err != NO_ERROR) {
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800523 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700524 }
525
chaviw4244e032019-09-04 11:27:49 -0700526 gatherBufferInfo();
527
Marissa Wallfd668622018-05-10 10:21:13 -0700528 mRefreshPending = true;
chaviwd62d3062019-09-04 14:48:02 -0700529 if (oldBufferInfo.mBuffer == nullptr) {
Marissa Wallfd668622018-05-10 10:21:13 -0700530 // the first time we receive a buffer, we need to trigger a
531 // geometry invalidation.
532 recomputeVisibleRegions = true;
533 }
534
chaviw4244e032019-09-04 11:27:49 -0700535 if ((mBufferInfo.mCrop != oldBufferInfo.mCrop) ||
536 (mBufferInfo.mTransform != oldBufferInfo.mTransform) ||
537 (mBufferInfo.mScaleMode != oldBufferInfo.mScaleMode) ||
538 (mBufferInfo.mTransformToDisplayInverse != oldBufferInfo.mTransformToDisplayInverse)) {
Marissa Wallfd668622018-05-10 10:21:13 -0700539 recomputeVisibleRegions = true;
540 }
541
chaviwd62d3062019-09-04 14:48:02 -0700542 if (oldBufferInfo.mBuffer != nullptr) {
543 uint32_t bufWidth = mBufferInfo.mBuffer->getWidth();
544 uint32_t bufHeight = mBufferInfo.mBuffer->getHeight();
545 if (bufWidth != uint32_t(oldBufferInfo.mBuffer->width) ||
546 bufHeight != uint32_t(oldBufferInfo.mBuffer->height)) {
Marissa Wallfd668622018-05-10 10:21:13 -0700547 recomputeVisibleRegions = true;
548 }
549 }
550
551 if (oldOpacity != isOpaque(s)) {
552 recomputeVisibleRegions = true;
553 }
554
555 // Remove any sync points corresponding to the buffer which was just
556 // latched
557 {
558 Mutex::Autolock lock(mLocalSyncPointMutex);
559 auto point = mLocalSyncPoints.begin();
560 while (point != mLocalSyncPoints.end()) {
561 if (!(*point)->frameIsAvailable() || !(*point)->transactionIsApplied()) {
562 // This sync point must have been added since we started
563 // latching. Don't drop it yet.
564 ++point;
565 continue;
566 }
567
568 if ((*point)->getFrameNumber() <= mCurrentFrameNumber) {
Alec Mourie60041e2019-06-14 18:59:51 -0700569 std::stringstream ss;
570 ss << "Dropping sync point " << (*point)->getFrameNumber();
571 ATRACE_NAME(ss.str().c_str());
Marissa Wallfd668622018-05-10 10:21:13 -0700572 point = mLocalSyncPoints.erase(point);
573 } else {
574 ++point;
575 }
576 }
577 }
578
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800579 return true;
Marissa Wallfd668622018-05-10 10:21:13 -0700580}
581
582// transaction
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700583void BufferLayer::notifyAvailableFrames(nsecs_t expectedPresentTime) {
584 const auto headFrameNumber = getHeadFrameNumber(expectedPresentTime);
Ady Abrahamcd1580c2019-04-29 15:40:03 -0700585 const bool headFenceSignaled = fenceHasSignaled();
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700586 const bool presentTimeIsCurrent = framePresentTimeIsCurrent(expectedPresentTime);
Marissa Wallfd668622018-05-10 10:21:13 -0700587 Mutex::Autolock lock(mLocalSyncPointMutex);
588 for (auto& point : mLocalSyncPoints) {
Ady Abrahamcd1580c2019-04-29 15:40:03 -0700589 if (headFrameNumber >= point->getFrameNumber() && headFenceSignaled &&
590 presentTimeIsCurrent) {
Marissa Wallfd668622018-05-10 10:21:13 -0700591 point->setFrameAvailable();
chaviw43cb3cb2019-05-31 15:23:41 -0700592 sp<Layer> requestedSyncLayer = point->getRequestedSyncLayer();
593 if (requestedSyncLayer) {
594 // Need to update the transaction flag to ensure the layer's pending transaction
595 // gets applied.
596 requestedSyncLayer->setTransactionFlags(eTransactionNeeded);
597 }
Marissa Wallfd668622018-05-10 10:21:13 -0700598 }
David Sodman0c69cad2017-08-21 12:12:51 -0700599 }
600}
601
Marissa Wallfd668622018-05-10 10:21:13 -0700602bool BufferLayer::hasReadyFrame() const {
Marissa Wall024a1912018-08-13 13:55:35 -0700603 return hasFrameUpdate() || getSidebandStreamChanged() || getAutoRefresh();
Marissa Wallfd668622018-05-10 10:21:13 -0700604}
605
606uint32_t BufferLayer::getEffectiveScalingMode() const {
chaviw4244e032019-09-04 11:27:49 -0700607 return mBufferInfo.mScaleMode;
Marissa Wallfd668622018-05-10 10:21:13 -0700608}
609
610bool BufferLayer::isProtected() const {
chaviwd62d3062019-09-04 14:48:02 -0700611 const sp<GraphicBuffer>& buffer(mBufferInfo.mBuffer);
Marissa Wallfd668622018-05-10 10:21:13 -0700612 return (buffer != 0) && (buffer->getUsage() & GRALLOC_USAGE_PROTECTED);
613}
614
Marissa Wallfd668622018-05-10 10:21:13 -0700615// h/w composer set-up
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700616bool BufferLayer::allTransactionsSignaled(nsecs_t expectedPresentTime) {
617 const auto headFrameNumber = getHeadFrameNumber(expectedPresentTime);
Marissa Wallfd668622018-05-10 10:21:13 -0700618 bool matchingFramesFound = false;
619 bool allTransactionsApplied = true;
620 Mutex::Autolock lock(mLocalSyncPointMutex);
621
622 for (auto& point : mLocalSyncPoints) {
623 if (point->getFrameNumber() > headFrameNumber) {
624 break;
625 }
626 matchingFramesFound = true;
627
628 if (!point->frameIsAvailable()) {
629 // We haven't notified the remote layer that the frame for
630 // this point is available yet. Notify it now, and then
631 // abort this attempt to latch.
632 point->setFrameAvailable();
633 allTransactionsApplied = false;
634 break;
635 }
636
637 allTransactionsApplied = allTransactionsApplied && point->transactionIsApplied();
638 }
639 return !matchingFramesFound || allTransactionsApplied;
David Sodman0c69cad2017-08-21 12:12:51 -0700640}
641
642// As documented in libhardware header, formats in the range
643// 0x100 - 0x1FF are specific to the HAL implementation, and
644// are known to have no alpha channel
645// TODO: move definition for device-specific range into
646// hardware.h, instead of using hard-coded values here.
647#define HARDWARE_IS_DEVICE_FORMAT(f) ((f) >= 0x100 && (f) <= 0x1FF)
648
649bool BufferLayer::getOpacityForFormat(uint32_t format) {
650 if (HARDWARE_IS_DEVICE_FORMAT(format)) {
651 return true;
652 }
653 switch (format) {
654 case HAL_PIXEL_FORMAT_RGBA_8888:
655 case HAL_PIXEL_FORMAT_BGRA_8888:
656 case HAL_PIXEL_FORMAT_RGBA_FP16:
657 case HAL_PIXEL_FORMAT_RGBA_1010102:
658 return false;
659 }
660 // in all other case, we have no blending (also for unknown formats)
661 return true;
662}
663
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700664bool BufferLayer::needsFiltering(const DisplayDevice* display) const {
665 const auto outputLayer = findOutputLayerForDisplay(display);
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800666 if (outputLayer == nullptr) {
Lloyd Piquef16688f2019-02-19 17:47:57 -0800667 return false;
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800668 }
669
Lloyd Piquef16688f2019-02-19 17:47:57 -0800670 // We need filtering if the sourceCrop rectangle size does not match the
671 // displayframe rectangle size (not a 1:1 render)
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800672 const auto& compositionState = outputLayer->getState();
673 const auto displayFrame = compositionState.displayFrame;
674 const auto sourceCrop = compositionState.sourceCrop;
Lloyd Piquef16688f2019-02-19 17:47:57 -0800675 return sourceCrop.getHeight() != displayFrame.getHeight() ||
Peiyong Linc2020ca2019-01-10 11:36:12 -0800676 sourceCrop.getWidth() != displayFrame.getWidth();
Chia-I Wu692e0832018-06-05 15:46:58 -0700677}
678
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700679bool BufferLayer::needsFilteringForScreenshots(const DisplayDevice* display,
Alec Mouri5a6d8572020-03-23 23:56:15 -0700680 const ui::Transform& inverseParentTransform) const {
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700681 const auto outputLayer = findOutputLayerForDisplay(display);
Alec Mouri5a6d8572020-03-23 23:56:15 -0700682 if (outputLayer == nullptr) {
683 return false;
684 }
685
686 // We need filtering if the sourceCrop rectangle size does not match the
687 // viewport rectangle size (not a 1:1 render)
688 const auto& compositionState = outputLayer->getState();
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700689 const ui::Transform& displayTransform = display->getTransform();
Alec Mouri5a6d8572020-03-23 23:56:15 -0700690 const ui::Transform inverseTransform = inverseParentTransform * displayTransform.inverse();
691 // Undo the transformation of the displayFrame so that we're back into
692 // layer-stack space.
693 const Rect frame = inverseTransform.transform(compositionState.displayFrame);
694 const FloatRect sourceCrop = compositionState.sourceCrop;
695
696 int32_t frameHeight = frame.getHeight();
697 int32_t frameWidth = frame.getWidth();
698 // If the display transform had a rotational component then undo the
699 // rotation so that the orientation matches the source crop.
700 if (displayTransform.getOrientation() & ui::Transform::ROT_90) {
701 std::swap(frameHeight, frameWidth);
702 }
703 return sourceCrop.getHeight() != frameHeight || sourceCrop.getWidth() != frameWidth;
704}
705
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700706uint64_t BufferLayer::getHeadFrameNumber(nsecs_t expectedPresentTime) const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800707 if (hasFrameUpdate()) {
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700708 return getFrameNumber(expectedPresentTime);
David Sodman0c69cad2017-08-21 12:12:51 -0700709 } else {
710 return mCurrentFrameNumber;
711 }
712}
713
Vishnu Nair60356342018-11-13 13:00:45 -0800714Rect BufferLayer::getBufferSize(const State& s) const {
715 // If we have a sideband stream, or we are scaling the buffer then return the layer size since
716 // we cannot determine the buffer size.
717 if ((s.sidebandStream != nullptr) ||
718 (getEffectiveScalingMode() != NATIVE_WINDOW_SCALING_MODE_FREEZE)) {
719 return Rect(getActiveWidth(s), getActiveHeight(s));
720 }
721
chaviwd62d3062019-09-04 14:48:02 -0700722 if (mBufferInfo.mBuffer == nullptr) {
Vishnu Nair60356342018-11-13 13:00:45 -0800723 return Rect::INVALID_RECT;
724 }
725
chaviwd62d3062019-09-04 14:48:02 -0700726 uint32_t bufWidth = mBufferInfo.mBuffer->getWidth();
727 uint32_t bufHeight = mBufferInfo.mBuffer->getHeight();
Vishnu Nair60356342018-11-13 13:00:45 -0800728
729 // Undo any transformations on the buffer and return the result.
chaviw4244e032019-09-04 11:27:49 -0700730 if (mBufferInfo.mTransform & ui::Transform::ROT_90) {
Vishnu Nair60356342018-11-13 13:00:45 -0800731 std::swap(bufWidth, bufHeight);
732 }
733
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800734 if (getTransformToDisplayInverse()) {
Dominik Laskowski718f9602019-11-09 20:01:35 -0800735 uint32_t invTransform = DisplayDevice::getPrimaryDisplayRotationFlags();
Vishnu Nair60356342018-11-13 13:00:45 -0800736 if (invTransform & ui::Transform::ROT_90) {
737 std::swap(bufWidth, bufHeight);
738 }
739 }
740
741 return Rect(bufWidth, bufHeight);
742}
743
Vishnu Nair4351ad52019-02-11 14:13:02 -0800744FloatRect BufferLayer::computeSourceBounds(const FloatRect& parentBounds) const {
745 const State& s(getDrawingState());
746
747 // If we have a sideband stream, or we are scaling the buffer then return the layer size since
748 // we cannot determine the buffer size.
749 if ((s.sidebandStream != nullptr) ||
750 (getEffectiveScalingMode() != NATIVE_WINDOW_SCALING_MODE_FREEZE)) {
751 return FloatRect(0, 0, getActiveWidth(s), getActiveHeight(s));
752 }
753
chaviwd62d3062019-09-04 14:48:02 -0700754 if (mBufferInfo.mBuffer == nullptr) {
Vishnu Nair4351ad52019-02-11 14:13:02 -0800755 return parentBounds;
756 }
757
chaviwd62d3062019-09-04 14:48:02 -0700758 uint32_t bufWidth = mBufferInfo.mBuffer->getWidth();
759 uint32_t bufHeight = mBufferInfo.mBuffer->getHeight();
Vishnu Nair4351ad52019-02-11 14:13:02 -0800760
761 // Undo any transformations on the buffer and return the result.
chaviw4244e032019-09-04 11:27:49 -0700762 if (mBufferInfo.mTransform & ui::Transform::ROT_90) {
Vishnu Nair4351ad52019-02-11 14:13:02 -0800763 std::swap(bufWidth, bufHeight);
764 }
765
766 if (getTransformToDisplayInverse()) {
Dominik Laskowski718f9602019-11-09 20:01:35 -0800767 uint32_t invTransform = DisplayDevice::getPrimaryDisplayRotationFlags();
Vishnu Nair4351ad52019-02-11 14:13:02 -0800768 if (invTransform & ui::Transform::ROT_90) {
769 std::swap(bufWidth, bufHeight);
770 }
771 }
772
773 return FloatRect(0, 0, bufWidth, bufHeight);
774}
775
chaviw49a108c2019-08-12 11:23:06 -0700776void BufferLayer::latchAndReleaseBuffer() {
777 mRefreshPending = false;
778 if (hasReadyFrame()) {
779 bool ignored = false;
780 latchBuffer(ignored, systemTime(), 0 /* expectedPresentTime */);
781 }
782 releasePendingBuffer(systemTime());
783}
784
chaviw4244e032019-09-04 11:27:49 -0700785PixelFormat BufferLayer::getPixelFormat() const {
786 return mBufferInfo.mPixelFormat;
787}
788
789bool BufferLayer::getTransformToDisplayInverse() const {
790 return mBufferInfo.mTransformToDisplayInverse;
791}
792
793Rect BufferLayer::getBufferCrop() const {
794 // this is the crop rectangle that applies to the buffer
795 // itself (as opposed to the window)
796 if (!mBufferInfo.mCrop.isEmpty()) {
797 // if the buffer crop is defined, we use that
798 return mBufferInfo.mCrop;
chaviwd62d3062019-09-04 14:48:02 -0700799 } else if (mBufferInfo.mBuffer != nullptr) {
chaviw4244e032019-09-04 11:27:49 -0700800 // otherwise we use the whole buffer
chaviwd62d3062019-09-04 14:48:02 -0700801 return mBufferInfo.mBuffer->getBounds();
chaviw4244e032019-09-04 11:27:49 -0700802 } else {
803 // if we don't have a buffer yet, we use an empty/invalid crop
804 return Rect();
805 }
806}
807
808uint32_t BufferLayer::getBufferTransform() const {
809 return mBufferInfo.mTransform;
810}
811
812ui::Dataspace BufferLayer::getDataSpace() const {
813 return mBufferInfo.mDataspace;
814}
815
816ui::Dataspace BufferLayer::translateDataspace(ui::Dataspace dataspace) {
817 ui::Dataspace updatedDataspace = dataspace;
818 // translate legacy dataspaces to modern dataspaces
819 switch (dataspace) {
820 case ui::Dataspace::SRGB:
821 updatedDataspace = ui::Dataspace::V0_SRGB;
822 break;
823 case ui::Dataspace::SRGB_LINEAR:
824 updatedDataspace = ui::Dataspace::V0_SRGB_LINEAR;
825 break;
826 case ui::Dataspace::JFIF:
827 updatedDataspace = ui::Dataspace::V0_JFIF;
828 break;
829 case ui::Dataspace::BT601_625:
830 updatedDataspace = ui::Dataspace::V0_BT601_625;
831 break;
832 case ui::Dataspace::BT601_525:
833 updatedDataspace = ui::Dataspace::V0_BT601_525;
834 break;
835 case ui::Dataspace::BT709:
836 updatedDataspace = ui::Dataspace::V0_BT709;
837 break;
838 default:
839 break;
840 }
841
842 return updatedDataspace;
843}
844
chaviwd62d3062019-09-04 14:48:02 -0700845sp<GraphicBuffer> BufferLayer::getBuffer() const {
846 return mBufferInfo.mBuffer;
847}
848
chaviwf83ce182019-09-12 14:43:08 -0700849void BufferLayer::getDrawingTransformMatrix(bool filteringEnabled, float outMatrix[16]) {
850 GLConsumer::computeTransformMatrix(outMatrix, mBufferInfo.mBuffer, mBufferInfo.mCrop,
851 mBufferInfo.mTransform, filteringEnabled);
852}
853
chaviwb4c6e582019-08-16 14:35:07 -0700854void BufferLayer::setInitialValuesForClone(const sp<Layer>& clonedFrom) {
855 Layer::setInitialValuesForClone(clonedFrom);
856
857 sp<BufferLayer> bufferClonedFrom = static_cast<BufferLayer*>(clonedFrom.get());
858 mPremultipliedAlpha = bufferClonedFrom->mPremultipliedAlpha;
859 mPotentialCursor = bufferClonedFrom->mPotentialCursor;
860 mProtectedByApp = bufferClonedFrom->mProtectedByApp;
chaviw74b03172019-08-19 11:09:03 -0700861
862 updateCloneBufferInfo();
863}
864
865void BufferLayer::updateCloneBufferInfo() {
866 if (!isClone() || !isClonedFromAlive()) {
867 return;
868 }
869
870 sp<BufferLayer> clonedFrom = static_cast<BufferLayer*>(getClonedFrom().get());
871 mBufferInfo = clonedFrom->mBufferInfo;
872 mSidebandStream = clonedFrom->mSidebandStream;
873 surfaceDamageRegion = clonedFrom->surfaceDamageRegion;
874 mCurrentFrameNumber = clonedFrom->mCurrentFrameNumber.load();
875 mPreviousFrameNumber = clonedFrom->mPreviousFrameNumber;
876
877 // After buffer info is updated, the drawingState from the real layer needs to be copied into
878 // the cloned. This is because some properties of drawingState can change when latchBuffer is
chaviwaf87b3e2019-10-01 16:59:28 -0700879 // called. However, copying the drawingState would also overwrite the cloned layer's relatives
880 // and touchableRegionCrop. Therefore, temporarily store the relatives so they can be set in
881 // the cloned drawingState again.
chaviw74b03172019-08-19 11:09:03 -0700882 wp<Layer> tmpZOrderRelativeOf = mDrawingState.zOrderRelativeOf;
883 SortedVector<wp<Layer>> tmpZOrderRelatives = mDrawingState.zOrderRelatives;
chaviwaf87b3e2019-10-01 16:59:28 -0700884 wp<Layer> tmpTouchableRegionCrop = mDrawingState.touchableRegionCrop;
885 InputWindowInfo tmpInputInfo = mDrawingState.inputInfo;
886
chaviw74b03172019-08-19 11:09:03 -0700887 mDrawingState = clonedFrom->mDrawingState;
chaviwaf87b3e2019-10-01 16:59:28 -0700888
889 mDrawingState.touchableRegionCrop = tmpTouchableRegionCrop;
chaviw74b03172019-08-19 11:09:03 -0700890 mDrawingState.zOrderRelativeOf = tmpZOrderRelativeOf;
891 mDrawingState.zOrderRelatives = tmpZOrderRelatives;
chaviwaf87b3e2019-10-01 16:59:28 -0700892 mDrawingState.inputInfo = tmpInputInfo;
chaviwb4c6e582019-08-16 14:35:07 -0700893}
894
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700895void BufferLayer::setTransformHint(ui::Transform::RotationFlags displayTransformHint) {
Vishnu Nair6213bd92020-05-08 17:42:25 -0700896 mTransformHint = getFixedTransformHint();
897 if (mTransformHint == ui::Transform::ROT_INVALID) {
898 mTransformHint = displayTransformHint;
899 }
900}
901
Vishnu Naire7f79c52020-10-29 14:45:03 -0700902bool BufferLayer::bufferNeedsFiltering() const {
903 return isFixedSize();
904}
905
David Sodman0c69cad2017-08-21 12:12:51 -0700906} // namespace android
907
908#if defined(__gl_h_)
909#error "don't include gl/gl.h in this file"
910#endif
911
912#if defined(__gl2_h_)
913#error "don't include gl2/gl2.h in this file"
914#endif
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800915
916// TODO(b/129481165): remove the #pragma below and fix conversion issues
917#pragma clang diagnostic pop // ignored "-Wconversion"