blob: b939752be5b403f2add59c3cb15058a7fc9cc76b [file] [log] [blame]
Marissa Wall61c58622018-07-18 10:12:20 -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
17//#define LOG_NDEBUG 0
18#undef LOG_TAG
19#define LOG_TAG "BufferStateLayer"
20#define ATRACE_TAG ATRACE_TAG_GRAPHICS
21
Lloyd Pique9755fb72019-03-26 14:44:40 -070022#include "BufferStateLayer.h"
23
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080024#include <limits>
Marissa Wall61c58622018-07-18 10:12:20 -070025
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +000026#include <FrameTimeline/FrameTimeline.h>
Vishnu Naira72f6c02022-07-01 05:33:08 +000027#include <compositionengine/CompositionEngine.h>
Marissa Wall947d34e2019-03-29 14:03:53 -070028#include <gui/BufferQueue.h>
Marissa Wall61c58622018-07-18 10:12:20 -070029#include <private/gui/SyncFeatures.h>
Peiyong Lincbc184f2018-08-22 13:24:10 -070030#include <renderengine/Image.h>
Robert Carr38d25002021-06-11 14:30:09 -070031#include "TunnelModeEnabledReporter.h"
Marissa Wall61c58622018-07-18 10:12:20 -070032
Chavi Weingartend00e0f72022-07-14 15:59:20 +000033#include <gui/TraceUtils.h>
Vishnu Nairfa247b12020-02-11 08:58:26 -080034#include "EffectLayer.h"
Adithya Srinivasanb238cd52021-02-04 17:54:05 +000035#include "FrameTracer/FrameTracer.h"
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080036#include "TimeStats/TimeStats.h"
Valerie Hau0bc09152018-12-20 07:42:47 -080037
Robert Carrb6450492022-06-23 11:17:16 -070038#define EARLY_RELEASE_ENABLED false
39
Vishnu Naira72f6c02022-07-01 05:33:08 +000040#include <compositionengine/LayerFECompositionState.h>
41#include <compositionengine/OutputLayer.h>
42#include <compositionengine/impl/OutputLayerCompositionState.h>
43#include <cutils/compiler.h>
44#include <cutils/native_handle.h>
45#include <cutils/properties.h>
46#include <gui/BufferItem.h>
47#include <gui/BufferQueue.h>
48#include <gui/GLConsumer.h>
49#include <gui/LayerDebugInfo.h>
50#include <gui/Surface.h>
51#include <renderengine/RenderEngine.h>
52#include <ui/DebugUtils.h>
53#include <utils/Errors.h>
54#include <utils/Log.h>
55#include <utils/NativeHandle.h>
56#include <utils/StopWatch.h>
57#include <utils/Trace.h>
58
59#include <cmath>
60#include <cstdlib>
61#include <mutex>
62#include <sstream>
63
64#include "Colorizer.h"
65#include "DisplayDevice.h"
66#include "FrameTracer/FrameTracer.h"
67#include "TimeStats/TimeStats.h"
68
Marissa Wall61c58622018-07-18 10:12:20 -070069namespace android {
70
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +000071using PresentState = frametimeline::SurfaceFrame::PresentState;
Vishnu Naira72f6c02022-07-01 05:33:08 +000072using gui::WindowInfo;
Vishnu Naira72f6c02022-07-01 05:33:08 +000073namespace {
74static constexpr float defaultMaxLuminance = 1000.0;
Vishnu Nair397a0e32022-07-26 00:01:48 +000075
76constexpr mat4 inverseOrientation(uint32_t transform) {
77 const mat4 flipH(-1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1);
78 const mat4 flipV(1, 0, 0, 0, 0, -1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1);
79 const mat4 rot90(0, 1, 0, 0, -1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1);
80 mat4 tr;
81
82 if (transform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
83 tr = tr * rot90;
84 }
85 if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_H) {
86 tr = tr * flipH;
87 }
88 if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_V) {
89 tr = tr * flipV;
90 }
91 return inverse(tr);
92}
93
94bool assignTransform(ui::Transform* dst, ui::Transform& from) {
95 if (*dst == from) {
96 return false;
97 }
98 *dst = from;
99 return true;
100}
101
102TimeStats::SetFrameRateVote frameRateToSetFrameRateVotePayload(Layer::FrameRate frameRate) {
103 using FrameRateCompatibility = TimeStats::SetFrameRateVote::FrameRateCompatibility;
104 using Seamlessness = TimeStats::SetFrameRateVote::Seamlessness;
105 const auto frameRateCompatibility = [frameRate] {
106 switch (frameRate.type) {
107 case Layer::FrameRateCompatibility::Default:
108 return FrameRateCompatibility::Default;
109 case Layer::FrameRateCompatibility::ExactOrMultiple:
110 return FrameRateCompatibility::ExactOrMultiple;
111 default:
112 return FrameRateCompatibility::Undefined;
113 }
114 }();
115
116 const auto seamlessness = [frameRate] {
117 switch (frameRate.seamlessness) {
118 case scheduler::Seamlessness::OnlySeamless:
119 return Seamlessness::ShouldBeSeamless;
120 case scheduler::Seamlessness::SeamedAndSeamless:
121 return Seamlessness::NotRequired;
122 default:
123 return Seamlessness::Undefined;
124 }
125 }();
126
127 return TimeStats::SetFrameRateVote{.frameRate = frameRate.rate.getValue(),
128 .frameRateCompatibility = frameRateCompatibility,
129 .seamlessness = seamlessness};
130}
Vishnu Naira72f6c02022-07-01 05:33:08 +0000131} // namespace
132
Marissa Wall947d34e2019-03-29 14:03:53 -0700133BufferStateLayer::BufferStateLayer(const LayerCreationArgs& args)
Vishnu Naira72f6c02022-07-01 05:33:08 +0000134 : Layer(args),
135 mTextureName(args.textureName),
136 mCompositionState{mFlinger->getCompositionEngine().createLayerFECompositionState()},
Ady Abrahamd11bade2022-08-01 16:18:03 -0700137 mHwcSlotGenerator(sp<HwcSlotGenerator>::make()) {
Vishnu Naira72f6c02022-07-01 05:33:08 +0000138 ALOGV("Creating Layer %s", getDebugName());
139
140 mPremultipliedAlpha = !(args.flags & ISurfaceComposerClient::eNonPremultiplied);
Vishnu Naira72f6c02022-07-01 05:33:08 +0000141 mPotentialCursor = args.flags & ISurfaceComposerClient::eCursorWindow;
142 mProtectedByApp = args.flags & ISurfaceComposerClient::eProtectedByApp;
Robert Carr6a160312021-05-17 12:08:20 -0700143 mDrawingState.dataspace = ui::Dataspace::V0_SRGB;
Vishnu Nair60356342018-11-13 13:00:45 -0800144}
Marissa Wall61c58622018-07-18 10:12:20 -0700145
Alec Mouri4545a8a2019-08-08 20:05:32 -0700146BufferStateLayer::~BufferStateLayer() {
chaviwb4c6e582019-08-16 14:35:07 -0700147 // The original layer and the clone layer share the same texture and buffer. Therefore, only
148 // one of the layers, in this case the original layer, needs to handle the deletion. The
149 // original layer and the clone should be removed at the same time so there shouldn't be any
150 // issue with the clone layer trying to use the texture.
Vishnu Nair3bb11d02021-11-26 09:24:11 -0800151 if (mBufferInfo.mBuffer != nullptr) {
Alec Mouria90a5702021-04-16 16:36:21 +0000152 callReleaseBufferCallback(mDrawingState.releaseBufferListener,
Vishnu Nair4ba0c2e2021-06-24 11:27:17 -0700153 mBufferInfo.mBuffer->getBuffer(), mBufferInfo.mFrameNumber,
chaviw69058fb2021-09-27 09:37:30 -0500154 mBufferInfo.mFence,
Ady Abraham899dcdb2021-06-15 16:56:21 -0700155 mFlinger->getMaxAcquiredBufferCountForCurrentRefreshRate(
156 mOwnerUid));
Alec Mouri4545a8a2019-08-08 20:05:32 -0700157 }
Vishnu Naira72f6c02022-07-01 05:33:08 +0000158 if (!isClone()) {
159 // The original layer and the clone layer share the same texture. Therefore, only one of
160 // the layers, in this case the original layer, needs to handle the deletion. The original
161 // layer and the clone should be removed at the same time so there shouldn't be any issue
162 // with the clone layer trying to use the deleted texture.
163 mFlinger->deleteTextureAsync(mTextureName);
164 }
165 const int32_t layerId = getSequence();
166 mFlinger->mTimeStats->onDestroy(layerId);
167 mFlinger->mFrameTracer->onDestroy(layerId);
Alec Mouri4545a8a2019-08-08 20:05:32 -0700168}
169
Vishnu Nair397a0e32022-07-26 00:01:48 +0000170void BufferStateLayer::callReleaseBufferCallback(const sp<ITransactionCompletedListener>& listener,
171 const sp<GraphicBuffer>& buffer,
172 uint64_t framenumber,
173 const sp<Fence>& releaseFence,
174 uint32_t currentMaxAcquiredBufferCount) {
175 if (!listener) {
176 return;
177 }
178 ATRACE_FORMAT_INSTANT("callReleaseBufferCallback %s - %" PRIu64, getDebugName(), framenumber);
179 listener->onReleaseBuffer({buffer->getId(), framenumber},
180 releaseFence ? releaseFence : Fence::NO_FENCE,
181 currentMaxAcquiredBufferCount);
182}
183
Marissa Wall61c58622018-07-18 10:12:20 -0700184// -----------------------------------------------------------------------
185// Interface implementation for Layer
186// -----------------------------------------------------------------------
Dominik Laskowskib17c6212022-05-09 09:36:19 -0700187void BufferStateLayer::onLayerDisplayed(ftl::SharedFuture<FenceResult> futureFenceResult) {
Robert Carrc2f84132022-03-09 16:26:43 -0800188 // If we are displayed on multiple displays in a single composition cycle then we would
189 // need to do careful tracking to enable the use of the mLastClientCompositionFence.
190 // For example we can only use it if all the displays are client comp, and we need
191 // to merge all the client comp fences. We could do this, but for now we just
192 // disable the optimization when a layer is composed on multiple displays.
Robert Carr05da0082022-05-25 23:29:34 -0700193 if (mClearClientCompositionFenceOnLayerDisplayed) {
Robert Carrccab4242021-09-28 16:53:03 -0700194 mLastClientCompositionFence = nullptr;
Robert Carrc2f84132022-03-09 16:26:43 -0800195 } else {
Robert Carr05da0082022-05-25 23:29:34 -0700196 mClearClientCompositionFenceOnLayerDisplayed = true;
Robert Carrccab4242021-09-28 16:53:03 -0700197 }
198
Marissa Wall5a68a772018-12-22 17:43:42 -0800199 // The previous release fence notifies the client that SurfaceFlinger is done with the previous
200 // buffer that was presented on this layer. The first transaction that came in this frame that
201 // replaced the previous buffer on this layer needs this release fence, because the fence will
202 // let the client know when that previous buffer is removed from the screen.
203 //
204 // Every other transaction on this layer does not need a release fence because no other
205 // Transactions that were set on this layer this frame are going to have their preceeding buffer
206 // removed from the display this frame.
207 //
208 // For example, if we have 3 transactions this frame. The first transaction doesn't contain a
209 // buffer so it doesn't need a previous release fence because the layer still needs the previous
210 // buffer. The second transaction contains a buffer so it needs a previous release fence because
211 // the previous buffer will be released this frame. The third transaction also contains a
212 // buffer. It replaces the buffer in the second transaction. The buffer in the second
213 // transaction will now no longer be presented so it is released immediately and the third
214 // transaction doesn't need a previous release fence.
Robert Carr8d958532020-11-10 14:09:16 -0800215 sp<CallbackHandle> ch;
Marissa Wall5a68a772018-12-22 17:43:42 -0800216 for (auto& handle : mDrawingState.callbackHandles) {
chaviw0b06a8d2021-08-06 11:49:08 -0500217 if (handle->releasePreviousBuffer &&
218 mDrawingState.releaseBufferEndpoint == handle->listener) {
Robert Carr8d958532020-11-10 14:09:16 -0800219 ch = handle;
Marissa Wall5a68a772018-12-22 17:43:42 -0800220 break;
221 }
222 }
Valerie Haubf784642020-01-29 07:25:23 -0800223
Mikael Pessa2e1608f2019-07-19 11:25:35 -0700224 // Prevent tracing the same release multiple times.
225 if (mPreviousFrameNumber != mPreviousReleasedFrameNumber) {
Mikael Pessa2e1608f2019-07-19 11:25:35 -0700226 mPreviousReleasedFrameNumber = mPreviousFrameNumber;
227 }
Sally Qi59a9f502021-10-12 18:53:23 +0000228
229 if (ch != nullptr) {
230 ch->previousReleaseCallbackId = mPreviousReleaseCallbackId;
Dominik Laskowskibb448ce2022-05-07 15:52:55 -0700231 ch->previousReleaseFences.emplace_back(std::move(futureFenceResult));
Sally Qi59a9f502021-10-12 18:53:23 +0000232 ch->name = mName;
233 }
Marissa Wall61c58622018-07-18 10:12:20 -0700234}
235
Jorim Jaggi9c03b502020-11-24 23:51:31 +0100236void BufferStateLayer::onSurfaceFrameCreated(
237 const std::shared_ptr<frametimeline::SurfaceFrame>& surfaceFrame) {
Adithya Srinivasand17c7da2021-03-05 20:43:32 +0000238 while (mPendingJankClassifications.size() >= kPendingClassificationMaxSurfaceFrames) {
239 // Too many SurfaceFrames pending classification. The front of the deque is probably not
240 // tracked by FrameTimeline and will never be presented. This will only result in a memory
241 // leak.
242 ALOGW("Removing the front of pending jank deque from layer - %s to prevent memory leak",
243 mName.c_str());
Adithya Srinivasan785addd2021-03-09 00:38:00 +0000244 std::string miniDump = mPendingJankClassifications.front()->miniDump();
245 ALOGD("Head SurfaceFrame mini dump\n%s", miniDump.c_str());
Adithya Srinivasand17c7da2021-03-05 20:43:32 +0000246 mPendingJankClassifications.pop_front();
247 }
Jorim Jaggi9c03b502020-11-24 23:51:31 +0100248 mPendingJankClassifications.emplace_back(surfaceFrame);
249}
250
Valerie Haubf784642020-01-29 07:25:23 -0800251void BufferStateLayer::releasePendingBuffer(nsecs_t dequeueReadyTime) {
Valerie Hau32cdc1f2019-10-21 14:45:54 -0700252 for (const auto& handle : mDrawingState.callbackHandles) {
253 handle->transformHint = mTransformHint;
Valerie Hau871d6352020-01-29 08:44:02 -0800254 handle->dequeueReadyTime = dequeueReadyTime;
Ady Abraham899dcdb2021-06-15 16:56:21 -0700255 handle->currentMaxAcquiredBufferCount =
256 mFlinger->getMaxAcquiredBufferCountForCurrentRefreshRate(mOwnerUid);
Chavi Weingartend00e0f72022-07-14 15:59:20 +0000257 ATRACE_FORMAT_INSTANT("releasePendingBuffer %s - %" PRIu64, getDebugName(),
258 handle->previousReleaseCallbackId.framenumber);
Valerie Hau32cdc1f2019-10-21 14:45:54 -0700259 }
260
Vishnu Nair1506b182021-02-22 14:35:15 -0800261 for (auto& handle : mDrawingState.callbackHandles) {
chaviw0b06a8d2021-08-06 11:49:08 -0500262 if (handle->releasePreviousBuffer &&
263 mDrawingState.releaseBufferEndpoint == handle->listener) {
Vishnu Nair4ba0c2e2021-06-24 11:27:17 -0700264 handle->previousReleaseCallbackId = mPreviousReleaseCallbackId;
Vishnu Nair1506b182021-02-22 14:35:15 -0800265 break;
266 }
267 }
268
Jorim Jaggi9c03b502020-11-24 23:51:31 +0100269 std::vector<JankData> jankData;
270 jankData.reserve(mPendingJankClassifications.size());
271 while (!mPendingJankClassifications.empty()
272 && mPendingJankClassifications.front()->getJankType()) {
273 std::shared_ptr<frametimeline::SurfaceFrame> surfaceFrame =
274 mPendingJankClassifications.front();
275 mPendingJankClassifications.pop_front();
276 jankData.emplace_back(
277 JankData(surfaceFrame->getToken(), surfaceFrame->getJankType().value()));
278 }
279
Robert Carr3d1047b2021-09-20 18:22:32 -0700280 mFlinger->getTransactionCallbackInvoker().addCallbackHandles(
Jorim Jaggi9c03b502020-11-24 23:51:31 +0100281 mDrawingState.callbackHandles, jankData);
Marissa Wall5a68a772018-12-22 17:43:42 -0800282
Sally Qi59a9f502021-10-12 18:53:23 +0000283 sp<Fence> releaseFence = Fence::NO_FENCE;
284 for (auto& handle : mDrawingState.callbackHandles) {
285 if (handle->releasePreviousBuffer &&
286 mDrawingState.releaseBufferEndpoint == handle->listener) {
287 releaseFence =
288 handle->previousReleaseFence ? handle->previousReleaseFence : Fence::NO_FENCE;
289 break;
290 }
291 }
292
Marissa Wall5a68a772018-12-22 17:43:42 -0800293 mDrawingState.callbackHandles = {};
Marissa Wall61c58622018-07-18 10:12:20 -0700294}
295
Marissa Walle2ffb422018-10-12 11:33:52 -0700296bool BufferStateLayer::willPresentCurrentTransaction() const {
297 // Returns true if the most recent Transaction applied to CurrentState will be presented.
Robert Carr321e83c2019-08-19 15:49:30 -0700298 return (getSidebandStreamChanged() || getAutoRefresh() ||
Robert Carr6a160312021-05-17 12:08:20 -0700299 (mDrawingState.modified &&
300 (mDrawingState.buffer != nullptr || mDrawingState.bgColorLayer != nullptr)));
Marissa Wall61c58622018-07-18 10:12:20 -0700301}
302
Marissa Wall61c58622018-07-18 10:12:20 -0700303bool BufferStateLayer::setTransform(uint32_t transform) {
Robert Carr6a160312021-05-17 12:08:20 -0700304 if (mDrawingState.bufferTransform == transform) return false;
305 mDrawingState.bufferTransform = transform;
306 mDrawingState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700307 setTransactionFlags(eTransactionNeeded);
308 return true;
309}
310
311bool BufferStateLayer::setTransformToDisplayInverse(bool transformToDisplayInverse) {
Robert Carr6a160312021-05-17 12:08:20 -0700312 if (mDrawingState.transformToDisplayInverse == transformToDisplayInverse) return false;
313 mDrawingState.sequence++;
314 mDrawingState.transformToDisplayInverse = transformToDisplayInverse;
315 mDrawingState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700316 setTransactionFlags(eTransactionNeeded);
317 return true;
318}
319
chaviwf3f40fe2021-04-27 15:54:02 -0500320bool BufferStateLayer::setBufferCrop(const Rect& bufferCrop) {
Robert Carr6a160312021-05-17 12:08:20 -0700321 if (mDrawingState.bufferCrop == bufferCrop) return false;
chaviwf3f40fe2021-04-27 15:54:02 -0500322
Robert Carr6a160312021-05-17 12:08:20 -0700323 mDrawingState.sequence++;
324 mDrawingState.bufferCrop = bufferCrop;
chaviwf3f40fe2021-04-27 15:54:02 -0500325
Robert Carr6a160312021-05-17 12:08:20 -0700326 mDrawingState.modified = true;
chaviwf3f40fe2021-04-27 15:54:02 -0500327 setTransactionFlags(eTransactionNeeded);
328 return true;
329}
330
Vishnu Nair6bdec7d2021-05-10 15:01:13 -0700331bool BufferStateLayer::setDestinationFrame(const Rect& destinationFrame) {
Robert Carr6a160312021-05-17 12:08:20 -0700332 if (mDrawingState.destinationFrame == destinationFrame) return false;
Vishnu Nair6bdec7d2021-05-10 15:01:13 -0700333
Robert Carr6a160312021-05-17 12:08:20 -0700334 mDrawingState.sequence++;
335 mDrawingState.destinationFrame = destinationFrame;
Vishnu Nair6bdec7d2021-05-10 15:01:13 -0700336
Robert Carr6a160312021-05-17 12:08:20 -0700337 mDrawingState.modified = true;
Vishnu Nair6bdec7d2021-05-10 15:01:13 -0700338 setTransactionFlags(eTransactionNeeded);
339 return true;
340}
341
342// Translate destination frame into scale and position. If a destination frame is not set, use the
343// provided scale and position
Robert Carr6a160312021-05-17 12:08:20 -0700344bool BufferStateLayer::updateGeometry() {
Vishnu Naird2aaab12022-02-10 14:49:09 -0800345 if ((mDrawingState.flags & layer_state_t::eIgnoreDestinationFrame) ||
346 mDrawingState.destinationFrame.isEmpty()) {
Vishnu Nair6bdec7d2021-05-10 15:01:13 -0700347 // If destination frame is not set, use the requested transform set via
348 // BufferStateLayer::setPosition and BufferStateLayer::setMatrix.
Robert Carr6a160312021-05-17 12:08:20 -0700349 return assignTransform(&mDrawingState.transform, mRequestedTransform);
Vishnu Nair6bdec7d2021-05-10 15:01:13 -0700350 }
351
Robert Carr6a160312021-05-17 12:08:20 -0700352 Rect destRect = mDrawingState.destinationFrame;
Vishnu Nair6bdec7d2021-05-10 15:01:13 -0700353 int32_t destW = destRect.width();
354 int32_t destH = destRect.height();
355 if (destRect.left < 0) {
356 destRect.left = 0;
357 destRect.right = destW;
358 }
359 if (destRect.top < 0) {
360 destRect.top = 0;
361 destRect.bottom = destH;
362 }
363
Robert Carr6a160312021-05-17 12:08:20 -0700364 if (!mDrawingState.buffer) {
Vishnu Nair6bdec7d2021-05-10 15:01:13 -0700365 ui::Transform t;
366 t.set(destRect.left, destRect.top);
Robert Carr6a160312021-05-17 12:08:20 -0700367 return assignTransform(&mDrawingState.transform, t);
Vishnu Nair6bdec7d2021-05-10 15:01:13 -0700368 }
369
Vishnu Nairdbbe3852022-01-12 20:22:11 -0800370 uint32_t bufferWidth = mDrawingState.buffer->getWidth();
371 uint32_t bufferHeight = mDrawingState.buffer->getHeight();
Vishnu Nair6bdec7d2021-05-10 15:01:13 -0700372 // Undo any transformations on the buffer.
Robert Carr6a160312021-05-17 12:08:20 -0700373 if (mDrawingState.bufferTransform & ui::Transform::ROT_90) {
Vishnu Nair6bdec7d2021-05-10 15:01:13 -0700374 std::swap(bufferWidth, bufferHeight);
375 }
376 uint32_t invTransform = DisplayDevice::getPrimaryDisplayRotationFlags();
Robert Carr6a160312021-05-17 12:08:20 -0700377 if (mDrawingState.transformToDisplayInverse) {
Vishnu Nair6bdec7d2021-05-10 15:01:13 -0700378 if (invTransform & ui::Transform::ROT_90) {
379 std::swap(bufferWidth, bufferHeight);
380 }
381 }
382
383 float sx = destW / static_cast<float>(bufferWidth);
384 float sy = destH / static_cast<float>(bufferHeight);
385 ui::Transform t;
386 t.set(sx, 0, 0, sy);
387 t.set(destRect.left, destRect.top);
Robert Carr6a160312021-05-17 12:08:20 -0700388 return assignTransform(&mDrawingState.transform, t);
Vishnu Nair6bdec7d2021-05-10 15:01:13 -0700389}
390
Robert Carrde6d7b42022-01-07 18:23:06 -0800391bool BufferStateLayer::setMatrix(const layer_state_t::matrix22_t& matrix) {
Vishnu Nair6bdec7d2021-05-10 15:01:13 -0700392 if (mRequestedTransform.dsdx() == matrix.dsdx && mRequestedTransform.dtdy() == matrix.dtdy &&
393 mRequestedTransform.dtdx() == matrix.dtdx && mRequestedTransform.dsdy() == matrix.dsdy) {
Marissa Wall861616d2018-10-22 12:52:23 -0700394 return false;
395 }
396
Vishnu Nair6bdec7d2021-05-10 15:01:13 -0700397 mRequestedTransform.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
chaviw9a93ea62021-03-11 16:44:42 -0600398
Robert Carr6a160312021-05-17 12:08:20 -0700399 mDrawingState.sequence++;
400 mDrawingState.modified = true;
chaviw9a93ea62021-03-11 16:44:42 -0600401 setTransactionFlags(eTransactionNeeded);
Chavi Weingartena5aedbd2021-04-09 13:37:33 +0000402
403 return true;
404}
405
406bool BufferStateLayer::setPosition(float x, float y) {
Vishnu Nair6bdec7d2021-05-10 15:01:13 -0700407 if (mRequestedTransform.tx() == x && mRequestedTransform.ty() == y) {
Chavi Weingartena5aedbd2021-04-09 13:37:33 +0000408 return false;
409 }
410
Vishnu Nair6bdec7d2021-05-10 15:01:13 -0700411 mRequestedTransform.set(x, y);
Chavi Weingartena5aedbd2021-04-09 13:37:33 +0000412
Robert Carr6a160312021-05-17 12:08:20 -0700413 mDrawingState.sequence++;
414 mDrawingState.modified = true;
Chavi Weingartena5aedbd2021-04-09 13:37:33 +0000415 setTransactionFlags(eTransactionNeeded);
416
Marissa Wall861616d2018-10-22 12:52:23 -0700417 return true;
418}
419
Vishnu Nairdbbe3852022-01-12 20:22:11 -0800420bool BufferStateLayer::setBuffer(std::shared_ptr<renderengine::ExternalTexture>& buffer,
421 const BufferData& bufferData, nsecs_t postTime,
Alec Mouria90a5702021-04-16 16:36:21 +0000422 nsecs_t desiredPresentTime, bool isAutoTimestamp,
chaviwba4320c2021-09-15 15:20:53 -0500423 std::optional<nsecs_t> dequeueTime,
424 const FrameTimelineInfo& info) {
Alex Chaucf6b4b42021-12-07 10:48:52 +0000425 ATRACE_CALL();
Robert Carr0c1966e2020-10-19 12:12:08 -0700426
chaviwba4320c2021-09-15 15:20:53 -0500427 if (!buffer) {
428 return false;
429 }
430
431 const bool frameNumberChanged =
432 bufferData.flags.test(BufferData::BufferDataChange::frameNumberChanged);
433 const uint64_t frameNumber =
434 frameNumberChanged ? bufferData.frameNumber : mDrawingState.frameNumber + 1;
435
Robert Carr6a160312021-05-17 12:08:20 -0700436 if (mDrawingState.buffer) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700437 mReleasePreviousBuffer = true;
Vishnu Nairdbbe3852022-01-12 20:22:11 -0800438 if (!mBufferInfo.mBuffer ||
439 (!mDrawingState.buffer->hasSameBuffer(*mBufferInfo.mBuffer) ||
440 mDrawingState.frameNumber != mBufferInfo.mFrameNumber)) {
Robert Carr6a160312021-05-17 12:08:20 -0700441 // If mDrawingState has a buffer, and we are about to update again
Robert Carr7121caf2020-12-15 13:07:32 -0800442 // before swapping to drawing state, then the first buffer will be
Vishnu Nair1506b182021-02-22 14:35:15 -0800443 // dropped and we should decrement the pending buffer count and
444 // call any release buffer callbacks if set.
Robert Carr6a160312021-05-17 12:08:20 -0700445 callReleaseBufferCallback(mDrawingState.releaseBufferListener,
Vishnu Nair4ba0c2e2021-06-24 11:27:17 -0700446 mDrawingState.buffer->getBuffer(), mDrawingState.frameNumber,
chaviw69058fb2021-09-27 09:37:30 -0500447 mDrawingState.acquireFence,
Ady Abraham899dcdb2021-06-15 16:56:21 -0700448 mFlinger->getMaxAcquiredBufferCountForCurrentRefreshRate(
449 mOwnerUid));
Robert Carr7121caf2020-12-15 13:07:32 -0800450 decrementPendingBufferCount();
Robert Carr6a160312021-05-17 12:08:20 -0700451 if (mDrawingState.bufferSurfaceFrameTX != nullptr &&
452 mDrawingState.bufferSurfaceFrameTX->getPresentState() != PresentState::Presented) {
453 addSurfaceFrameDroppedForBuffer(mDrawingState.bufferSurfaceFrameTX);
454 mDrawingState.bufferSurfaceFrameTX.reset();
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +0000455 }
Robert Carrb6450492022-06-23 11:17:16 -0700456 } else if (EARLY_RELEASE_ENABLED && mLastClientCompositionFence != nullptr) {
Robert Carrccab4242021-09-28 16:53:03 -0700457 callReleaseBufferCallback(mDrawingState.releaseBufferListener,
458 mDrawingState.buffer->getBuffer(), mDrawingState.frameNumber,
chaviw69058fb2021-09-27 09:37:30 -0500459 mLastClientCompositionFence,
Robert Carrccab4242021-09-28 16:53:03 -0700460 mFlinger->getMaxAcquiredBufferCountForCurrentRefreshRate(
chaviw69058fb2021-09-27 09:37:30 -0500461 mOwnerUid));
Robert Carrccab4242021-09-28 16:53:03 -0700462 mLastClientCompositionFence = nullptr;
Robert Carr7121caf2020-12-15 13:07:32 -0800463 }
Marissa Wallfda30bb2018-10-12 11:34:28 -0700464 }
Robert Carr6a160312021-05-17 12:08:20 -0700465
466 mDrawingState.frameNumber = frameNumber;
chaviwba4320c2021-09-15 15:20:53 -0500467 mDrawingState.releaseBufferListener = bufferData.releaseBufferListener;
Vishnu Nairdbbe3852022-01-12 20:22:11 -0800468 mDrawingState.buffer = std::move(buffer);
chaviwba4320c2021-09-15 15:20:53 -0500469 mDrawingState.clientCacheId = bufferData.cachedBuffer;
470
471 mDrawingState.acquireFence = bufferData.flags.test(BufferData::BufferDataChange::fenceChanged)
472 ? bufferData.acquireFence
473 : Fence::NO_FENCE;
474 mDrawingState.acquireFenceTime = std::make_unique<FenceTime>(mDrawingState.acquireFence);
Ady Abraham461296a2022-01-21 11:11:31 -0800475 if (mDrawingState.acquireFenceTime->getSignalTime() == Fence::SIGNAL_TIME_PENDING) {
476 // We latched this buffer unsiganled, so we need to pass the acquire fence
477 // on the callback instead of just the acquire time, since it's unknown at
478 // this point.
479 mCallbackHandleAcquireTimeOrFence = mDrawingState.acquireFence;
480 } else {
481 mCallbackHandleAcquireTimeOrFence = mDrawingState.acquireFenceTime->getSignalTime();
482 }
chaviwba4320c2021-09-15 15:20:53 -0500483
Robert Carr6a160312021-05-17 12:08:20 -0700484 mDrawingState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700485 setTransactionFlags(eTransactionNeeded);
Ady Abraham09bd3922019-04-08 10:44:56 -0700486
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800487 const int32_t layerId = getSequence();
Robert Carr6a160312021-05-17 12:08:20 -0700488 mFlinger->mTimeStats->setPostTime(layerId, mDrawingState.frameNumber, getName().c_str(),
Adithya Srinivasan58069dc2021-06-04 20:37:02 +0000489 mOwnerUid, postTime, getGameMode());
Robert Carr6a160312021-05-17 12:08:20 -0700490 mDrawingState.desiredPresentTime = desiredPresentTime;
491 mDrawingState.isAutoTimestamp = isAutoTimestamp;
Ady Abraham09bd3922019-04-08 10:44:56 -0700492
Ady Abrahamb7f15562021-03-15 18:34:08 -0700493 const nsecs_t presentTime = [&] {
494 if (!isAutoTimestamp) return desiredPresentTime;
495
496 const auto prediction =
497 mFlinger->mFrameTimeline->getTokenManager()->getPredictionsForToken(info.vsyncId);
498 if (prediction.has_value()) return prediction->presentTime;
499
500 return static_cast<nsecs_t>(0);
501 }();
Dominik Laskowski068173d2021-08-11 17:22:59 -0700502
503 using LayerUpdateType = scheduler::LayerHistory::LayerUpdateType;
504 mFlinger->mScheduler->recordLayerHistory(this, presentTime, LayerUpdateType::Buffer);
Ady Abraham09bd3922019-04-08 10:44:56 -0700505
Adithya Srinivasan891004e2021-02-12 20:20:47 +0000506 setFrameTimelineVsyncForBufferTransaction(info, postTime);
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +0000507
Vishnu Nair26c49762022-01-18 22:58:52 +0000508 if (dequeueTime && *dequeueTime != 0) {
509 const uint64_t bufferId = mDrawingState.buffer->getId();
Adithya Srinivasanb238cd52021-02-04 17:54:05 +0000510 mFlinger->mFrameTracer->traceNewLayer(layerId, getName().c_str());
511 mFlinger->mFrameTracer->traceTimestamp(layerId, bufferId, frameNumber, *dequeueTime,
512 FrameTracer::FrameEvent::DEQUEUE);
513 mFlinger->mFrameTracer->traceTimestamp(layerId, bufferId, frameNumber, postTime,
514 FrameTracer::FrameEvent::QUEUE);
515 }
Chavi Weingartena5aedbd2021-04-09 13:37:33 +0000516
chaviwba4320c2021-09-15 15:20:53 -0500517 mDrawingState.releaseBufferEndpoint = bufferData.releaseBufferEndpoint;
Marissa Wall61c58622018-07-18 10:12:20 -0700518 return true;
519}
520
521bool BufferStateLayer::setDataspace(ui::Dataspace dataspace) {
Vishnu Nairbc4ee5c2022-08-16 03:19:37 +0000522 mDrawingState.dataspaceRequested = true;
Robert Carr6a160312021-05-17 12:08:20 -0700523 if (mDrawingState.dataspace == dataspace) return false;
524 mDrawingState.dataspace = dataspace;
525 mDrawingState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700526 setTransactionFlags(eTransactionNeeded);
527 return true;
528}
529
530bool BufferStateLayer::setHdrMetadata(const HdrMetadata& hdrMetadata) {
Robert Carr6a160312021-05-17 12:08:20 -0700531 if (mDrawingState.hdrMetadata == hdrMetadata) return false;
532 mDrawingState.hdrMetadata = hdrMetadata;
533 mDrawingState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700534 setTransactionFlags(eTransactionNeeded);
535 return true;
536}
537
538bool BufferStateLayer::setSurfaceDamageRegion(const Region& surfaceDamage) {
Robert Carr6a160312021-05-17 12:08:20 -0700539 mDrawingState.surfaceDamageRegion = surfaceDamage;
540 mDrawingState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700541 setTransactionFlags(eTransactionNeeded);
542 return true;
543}
544
545bool BufferStateLayer::setApi(int32_t api) {
Robert Carr6a160312021-05-17 12:08:20 -0700546 if (mDrawingState.api == api) return false;
547 mDrawingState.api = api;
548 mDrawingState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700549 setTransactionFlags(eTransactionNeeded);
550 return true;
551}
552
553bool BufferStateLayer::setSidebandStream(const sp<NativeHandle>& sidebandStream) {
Robert Carr6a160312021-05-17 12:08:20 -0700554 if (mDrawingState.sidebandStream == sidebandStream) return false;
Robert Carr3e2a2992021-06-11 13:42:55 -0700555
556 if (mDrawingState.sidebandStream != nullptr && sidebandStream == nullptr) {
557 mFlinger->mTunnelModeEnabledReporter->decrementTunnelModeCount();
558 } else if (sidebandStream != nullptr) {
559 mFlinger->mTunnelModeEnabledReporter->incrementTunnelModeCount();
560 }
561
Robert Carr6a160312021-05-17 12:08:20 -0700562 mDrawingState.sidebandStream = sidebandStream;
563 mDrawingState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700564 setTransactionFlags(eTransactionNeeded);
Marissa Wall61c58622018-07-18 10:12:20 -0700565 if (!mSidebandStreamChanged.exchange(true)) {
566 // mSidebandStreamChanged was false
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -0700567 mFlinger->onLayerUpdate();
Marissa Wall61c58622018-07-18 10:12:20 -0700568 }
569 return true;
570}
571
Marissa Walle2ffb422018-10-12 11:33:52 -0700572bool BufferStateLayer::setTransactionCompletedListeners(
Jiakai Zhanga5505cb2021-11-09 11:46:30 +0000573 const std::vector<sp<CallbackHandle>>& handles) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700574 // If there is no handle, we will not send a callback so reset mReleasePreviousBuffer and return
Jiakai Zhanga5505cb2021-11-09 11:46:30 +0000575 if (handles.empty()) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700576 mReleasePreviousBuffer = false;
Marissa Walle2ffb422018-10-12 11:33:52 -0700577 return false;
578 }
579
580 const bool willPresent = willPresentCurrentTransaction();
581
582 for (const auto& handle : handles) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700583 // If this transaction set a buffer on this layer, release its previous buffer
584 handle->releasePreviousBuffer = mReleasePreviousBuffer;
585
Marissa Walle2ffb422018-10-12 11:33:52 -0700586 // If this layer will be presented in this frame
587 if (willPresent) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700588 // If this transaction set an acquire fence on this layer, set its acquire time
Ady Abraham461296a2022-01-21 11:11:31 -0800589 handle->acquireTimeOrFence = mCallbackHandleAcquireTimeOrFence;
Robert Carr6a160312021-05-17 12:08:20 -0700590 handle->frameNumber = mDrawingState.frameNumber;
Marissa Wallfda30bb2018-10-12 11:34:28 -0700591
Marissa Walle2ffb422018-10-12 11:33:52 -0700592 // Store so latched time and release fence can be set
Robert Carr6a160312021-05-17 12:08:20 -0700593 mDrawingState.callbackHandles.push_back(handle);
Marissa Walle2ffb422018-10-12 11:33:52 -0700594
Jiakai Zhanga5505cb2021-11-09 11:46:30 +0000595 } else { // If this layer will NOT need to be relatched and presented this frame
Marissa Walle2ffb422018-10-12 11:33:52 -0700596 // Notify the transaction completed thread this handle is done
Jiakai Zhanga5505cb2021-11-09 11:46:30 +0000597 mFlinger->getTransactionCallbackInvoker().registerUnpresentedCallbackHandle(handle);
Marissa Walle2ffb422018-10-12 11:33:52 -0700598 }
599 }
600
Marissa Wallfda30bb2018-10-12 11:34:28 -0700601 mReleasePreviousBuffer = false;
Ady Abraham461296a2022-01-21 11:11:31 -0800602 mCallbackHandleAcquireTimeOrFence = -1;
Marissa Wallfda30bb2018-10-12 11:34:28 -0700603
Marissa Walle2ffb422018-10-12 11:33:52 -0700604 return willPresent;
605}
606
rnleeed20fa42021-08-10 18:00:03 -0700607Rect BufferStateLayer::getBufferSize(const State& /*s*/) const {
Marissa Wall861616d2018-10-22 12:52:23 -0700608 // for buffer state layers we use the display frame size as the buffer size.
Marissa Wall61c58622018-07-18 10:12:20 -0700609
chaviw7e72caf2020-12-02 16:50:43 -0800610 if (mBufferInfo.mBuffer == nullptr) {
611 return Rect::INVALID_RECT;
612 }
613
Vishnu Nairdbbe3852022-01-12 20:22:11 -0800614 uint32_t bufWidth = mBufferInfo.mBuffer->getWidth();
615 uint32_t bufHeight = mBufferInfo.mBuffer->getHeight();
Vishnu Nair6bdec7d2021-05-10 15:01:13 -0700616
617 // Undo any transformations on the buffer and return the result.
618 if (mBufferInfo.mTransform & ui::Transform::ROT_90) {
619 std::swap(bufWidth, bufHeight);
620 }
621
622 if (getTransformToDisplayInverse()) {
623 uint32_t invTransform = DisplayDevice::getPrimaryDisplayRotationFlags();
624 if (invTransform & ui::Transform::ROT_90) {
625 std::swap(bufWidth, bufHeight);
Marissa Wall861616d2018-10-22 12:52:23 -0700626 }
627 }
628
rnleeed20fa42021-08-10 18:00:03 -0700629 return Rect(0, 0, static_cast<int32_t>(bufWidth), static_cast<int32_t>(bufHeight));
Marissa Wall61c58622018-07-18 10:12:20 -0700630}
Vishnu Nair4351ad52019-02-11 14:13:02 -0800631
632FloatRect BufferStateLayer::computeSourceBounds(const FloatRect& parentBounds) const {
Vishnu Nair6bdec7d2021-05-10 15:01:13 -0700633 if (mBufferInfo.mBuffer == nullptr) {
634 return parentBounds;
Vishnu Nair4351ad52019-02-11 14:13:02 -0800635 }
636
Vishnu Nair6bdec7d2021-05-10 15:01:13 -0700637 return getBufferSize(getDrawingState()).toFloatRect();
Vishnu Nair4351ad52019-02-11 14:13:02 -0800638}
639
Marissa Wall61c58622018-07-18 10:12:20 -0700640// -----------------------------------------------------------------------
Marissa Wall61c58622018-07-18 10:12:20 -0700641bool BufferStateLayer::fenceHasSignaled() const {
ramindani4d48f902021-09-20 21:07:45 +0000642 if (SurfaceFlinger::enableLatchUnsignaledConfig != LatchUnsignaledConfig::Disabled) {
Huihong Luo86c80e32021-06-16 15:41:07 -0700643 return true;
644 }
645
Alec Mouri91f6df32020-01-30 08:48:58 -0800646 const bool fenceSignaled =
647 getDrawingState().acquireFence->getStatus() == Fence::Status::Signaled;
648 if (!fenceSignaled) {
649 mFlinger->mTimeStats->incrementLatchSkipped(getSequence(),
650 TimeStats::LatchSkipReason::LateAcquire);
651 }
652
653 return fenceSignaled;
Marissa Wall61c58622018-07-18 10:12:20 -0700654}
655
Valerie Hau871d6352020-01-29 08:44:02 -0800656bool BufferStateLayer::onPreComposition(nsecs_t refreshStartTime) {
657 for (const auto& handle : mDrawingState.callbackHandles) {
658 handle->refreshStartTime = refreshStartTime;
659 }
Vishnu Naira72f6c02022-07-01 05:33:08 +0000660 return hasReadyFrame();
Valerie Hau871d6352020-01-29 08:44:02 -0800661}
662
Vishnu Naircf26a0a2020-11-13 12:56:20 -0800663void BufferStateLayer::setAutoRefresh(bool autoRefresh) {
Vishnu Nair86653e92021-11-03 17:19:36 -0700664 mDrawingState.autoRefresh = autoRefresh;
Marissa Wall61c58622018-07-18 10:12:20 -0700665}
666
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800667bool BufferStateLayer::latchSidebandStream(bool& recomputeVisibleRegions) {
baocheng suna663c2b2021-05-13 18:51:28 +0800668 // We need to update the sideband stream if the layer has both a buffer and a sideband stream.
baocheng sun9691b9c2021-08-03 19:27:06 +0800669 editCompositionState()->sidebandStreamHasFrame = hasFrameUpdate() && mSidebandStream.get();
baocheng suna663c2b2021-05-13 18:51:28 +0800670
baocheng sun9691b9c2021-08-03 19:27:06 +0800671 if (mSidebandStreamChanged.exchange(false)) {
Marissa Wall61c58622018-07-18 10:12:20 -0700672 const State& s(getDrawingState());
673 // mSidebandStreamChanged was true
Lloyd Pique0b785d82018-12-04 17:25:27 -0800674 mSidebandStream = s.sidebandStream;
Lloyd Piquede196652020-01-22 17:29:58 -0800675 editCompositionState()->sidebandStream = mSidebandStream;
Lloyd Pique0b785d82018-12-04 17:25:27 -0800676 if (mSidebandStream != nullptr) {
Marissa Wall61c58622018-07-18 10:12:20 -0700677 setTransactionFlags(eTransactionNeeded);
678 mFlinger->setTransactionFlags(eTraversalNeeded);
679 }
680 recomputeVisibleRegions = true;
681
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800682 return true;
Marissa Wall61c58622018-07-18 10:12:20 -0700683 }
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800684 return false;
Marissa Wall61c58622018-07-18 10:12:20 -0700685}
686
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800687bool BufferStateLayer::hasFrameUpdate() const {
Robert Carr6a160312021-05-17 12:08:20 -0700688 const State& c(getDrawingState());
Robert Carr315f3c72021-06-24 21:58:09 -0700689 return (mDrawingStateModified || mDrawingState.modified) && (c.buffer != nullptr || c.bgColorLayer != nullptr);
Marissa Wall61c58622018-07-18 10:12:20 -0700690}
691
Vishnu Nair397a0e32022-07-26 00:01:48 +0000692void BufferStateLayer::updateTexImage(nsecs_t latchTime) {
Marissa Wall61c58622018-07-18 10:12:20 -0700693 const State& s(getDrawingState());
694
695 if (!s.buffer) {
Valerie Hauaa194562019-02-05 16:21:38 -0800696 if (s.bgColorLayer) {
697 for (auto& handle : mDrawingState.callbackHandles) {
698 handle->latchTime = latchTime;
699 }
700 }
Vishnu Nair397a0e32022-07-26 00:01:48 +0000701 return;
Marissa Wall61c58622018-07-18 10:12:20 -0700702 }
703
Marissa Wall5a68a772018-12-22 17:43:42 -0800704 for (auto& handle : mDrawingState.callbackHandles) {
Vishnu Nair935590e2021-02-10 13:05:52 -0800705 if (handle->frameNumber == mDrawingState.frameNumber) {
706 handle->latchTime = latchTime;
707 }
Marissa Wall5a68a772018-12-22 17:43:42 -0800708 }
Marissa Walle2ffb422018-10-12 11:33:52 -0700709
Vishnu Nairea0de002020-11-17 17:42:37 -0800710 const int32_t layerId = getSequence();
Vishnu Nairdbbe3852022-01-12 20:22:11 -0800711 const uint64_t bufferId = mDrawingState.buffer->getId();
Adithya Srinivasanb238cd52021-02-04 17:54:05 +0000712 const uint64_t frameNumber = mDrawingState.frameNumber;
713 const auto acquireFence = std::make_shared<FenceTime>(mDrawingState.acquireFence);
714 mFlinger->mTimeStats->setAcquireFence(layerId, frameNumber, acquireFence);
715 mFlinger->mTimeStats->setLatchTime(layerId, frameNumber, latchTime);
716
717 mFlinger->mFrameTracer->traceFence(layerId, bufferId, frameNumber, acquireFence,
718 FrameTracer::FrameEvent::ACQUIRE_FENCE);
719 mFlinger->mFrameTracer->traceTimestamp(layerId, bufferId, frameNumber, latchTime,
720 FrameTracer::FrameEvent::LATCH);
Marissa Wall61c58622018-07-18 10:12:20 -0700721
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +0000722 auto& bufferSurfaceFrame = mDrawingState.bufferSurfaceFrameTX;
723 if (bufferSurfaceFrame != nullptr &&
724 bufferSurfaceFrame->getPresentState() != PresentState::Presented) {
725 // Update only if the bufferSurfaceFrame wasn't already presented. A Presented
726 // bufferSurfaceFrame could be seen here if a pending state was applied successfully and we
727 // are processing the next state.
728 addSurfaceFramePresentedForBuffer(bufferSurfaceFrame,
Ady Abraham6c1b7ac2021-03-31 16:56:03 -0700729 mDrawingState.acquireFenceTime->getSignalTime(),
730 latchTime);
Robert Carr6a160312021-05-17 12:08:20 -0700731 mDrawingState.bufferSurfaceFrameTX.reset();
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +0000732 }
733
Vishnu Nairfc46c1e2021-04-21 08:31:32 -0700734 std::deque<sp<CallbackHandle>> remainingHandles;
735 mFlinger->getTransactionCallbackInvoker()
Robert Carr3d1047b2021-09-20 18:22:32 -0700736 .addOnCommitCallbackHandles(mDrawingState.callbackHandles, remainingHandles);
Vishnu Nairfc46c1e2021-04-21 08:31:32 -0700737 mDrawingState.callbackHandles = remainingHandles;
738
Robert Carr6a160312021-05-17 12:08:20 -0700739 mDrawingStateModified = false;
Marissa Wall61c58622018-07-18 10:12:20 -0700740}
741
Vishnu Nair397a0e32022-07-26 00:01:48 +0000742void BufferStateLayer::gatherBufferInfo() {
743 if (!mBufferInfo.mBuffer || !mDrawingState.buffer->hasSameBuffer(*mBufferInfo.mBuffer)) {
chaviwdf3c5e82021-01-07 13:00:37 -0800744 decrementPendingBufferCount();
745 }
Marissa Wall61c58622018-07-18 10:12:20 -0700746
Vishnu Nair4ba0c2e2021-06-24 11:27:17 -0700747 mPreviousReleaseCallbackId = {getCurrentBufferId(), mBufferInfo.mFrameNumber};
Vishnu Nair397a0e32022-07-26 00:01:48 +0000748 mBufferInfo.mBuffer = mDrawingState.buffer;
749 mBufferInfo.mFence = mDrawingState.acquireFence;
750 mBufferInfo.mFrameNumber = mDrawingState.frameNumber;
Vishnu Naira72f6c02022-07-01 05:33:08 +0000751 mBufferInfo.mPixelFormat =
752 !mBufferInfo.mBuffer ? PIXEL_FORMAT_NONE : mBufferInfo.mBuffer->getPixelFormat();
753 mBufferInfo.mFrameLatencyNeeded = true;
Vishnu Nair397a0e32022-07-26 00:01:48 +0000754 mBufferInfo.mDesiredPresentTime = mDrawingState.desiredPresentTime;
755 mBufferInfo.mFenceTime = std::make_shared<FenceTime>(mDrawingState.acquireFence);
756 mBufferInfo.mFence = mDrawingState.acquireFence;
757 mBufferInfo.mTransform = mDrawingState.bufferTransform;
Robert Carr167bdde2021-07-28 11:26:51 -0700758 auto lastDataspace = mBufferInfo.mDataspace;
Vishnu Nair397a0e32022-07-26 00:01:48 +0000759 mBufferInfo.mDataspace = translateDataspace(mDrawingState.dataspace);
Robert Carr167bdde2021-07-28 11:26:51 -0700760 if (lastDataspace != mBufferInfo.mDataspace) {
761 mFlinger->mSomeDataspaceChanged = true;
762 }
Vishnu Nair397a0e32022-07-26 00:01:48 +0000763 mBufferInfo.mCrop = computeBufferCrop(mDrawingState);
chaviw4244e032019-09-04 11:27:49 -0700764 mBufferInfo.mScaleMode = NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW;
Vishnu Nair397a0e32022-07-26 00:01:48 +0000765 mBufferInfo.mSurfaceDamage = mDrawingState.surfaceDamageRegion;
766 mBufferInfo.mHdrMetadata = mDrawingState.hdrMetadata;
767 mBufferInfo.mApi = mDrawingState.api;
768 mBufferInfo.mTransformToDisplayInverse = mDrawingState.transformToDisplayInverse;
769 mBufferInfo.mBufferSlot = mHwcSlotGenerator->getHwcCacheSlot(mDrawingState.clientCacheId);
Robert Carr916b0362020-10-06 13:53:03 -0700770}
771
Vishnu Nair5cc9ac02021-04-19 13:23:38 -0700772Rect BufferStateLayer::computeBufferCrop(const State& s) {
chaviwf3f40fe2021-04-27 15:54:02 -0500773 if (s.buffer && !s.bufferCrop.isEmpty()) {
774 Rect bufferCrop;
Vishnu Nairdbbe3852022-01-12 20:22:11 -0800775 s.buffer->getBounds().intersect(s.bufferCrop, &bufferCrop);
chaviwf3f40fe2021-04-27 15:54:02 -0500776 return bufferCrop;
777 } else if (s.buffer) {
Vishnu Nairdbbe3852022-01-12 20:22:11 -0800778 return s.buffer->getBounds();
chaviwf3f40fe2021-04-27 15:54:02 -0500779 } else {
780 return s.bufferCrop;
chaviw4244e032019-09-04 11:27:49 -0700781 }
chaviw4244e032019-09-04 11:27:49 -0700782}
783
chaviwb4c6e582019-08-16 14:35:07 -0700784sp<Layer> BufferStateLayer::createClone() {
Vishnu Nair7fb9e5a2021-11-08 12:44:05 -0800785 LayerCreationArgs args(mFlinger.get(), nullptr, mName + " (Mirror)", 0, LayerMetadata());
chaviwb4c6e582019-08-16 14:35:07 -0700786 args.textureName = mTextureName;
Lloyd Pique1c3a5eb2019-10-03 13:07:08 -0700787 sp<BufferStateLayer> layer = mFlinger->getFactory().createBufferStateLayer(args);
chaviwb4c6e582019-08-16 14:35:07 -0700788 layer->mHwcSlotGenerator = mHwcSlotGenerator;
Ady Abrahamd11bade2022-08-01 16:18:03 -0700789 layer->setInitialValuesForClone(sp<Layer>::fromExisting(this));
chaviwb4c6e582019-08-16 14:35:07 -0700790 return layer;
791}
Valerie Hau92bf5482020-02-10 09:49:08 -0800792
Vishnu Naire7f79c52020-10-29 14:45:03 -0700793bool BufferStateLayer::bufferNeedsFiltering() const {
794 const State& s(getDrawingState());
795 if (!s.buffer) {
796 return false;
797 }
798
Vishnu Nairdbbe3852022-01-12 20:22:11 -0800799 int32_t bufferWidth = static_cast<int32_t>(s.buffer->getWidth());
800 int32_t bufferHeight = static_cast<int32_t>(s.buffer->getHeight());
Vishnu Naire7f79c52020-10-29 14:45:03 -0700801
802 // Undo any transformations on the buffer and return the result.
chaviw766c9c52021-02-10 17:36:47 -0800803 if (s.bufferTransform & ui::Transform::ROT_90) {
Vishnu Naire7f79c52020-10-29 14:45:03 -0700804 std::swap(bufferWidth, bufferHeight);
805 }
806
807 if (s.transformToDisplayInverse) {
808 uint32_t invTransform = DisplayDevice::getPrimaryDisplayRotationFlags();
809 if (invTransform & ui::Transform::ROT_90) {
810 std::swap(bufferWidth, bufferHeight);
811 }
812 }
813
814 const Rect layerSize{getBounds()};
Yiwei Zhang22ca2c42022-07-07 07:00:16 +0000815 int32_t layerWidth = layerSize.getWidth();
816 int32_t layerHeight = layerSize.getHeight();
817
818 // Align the layer orientation with the buffer before comparism
819 if (mTransformHint & ui::Transform::ROT_90) {
820 std::swap(layerWidth, layerHeight);
821 }
822
823 return layerWidth != bufferWidth || layerHeight != bufferHeight;
Vishnu Naire7f79c52020-10-29 14:45:03 -0700824}
Robert Carr7121caf2020-12-15 13:07:32 -0800825
Robert Carr7121caf2020-12-15 13:07:32 -0800826void BufferStateLayer::decrementPendingBufferCount() {
Vishnu Nair8eda69e2021-02-26 10:42:10 -0800827 int32_t pendingBuffers = --mPendingBufferTransactions;
828 tracePendingBufferCount(pendingBuffers);
Robert Carr7121caf2020-12-15 13:07:32 -0800829}
830
Vishnu Nair8eda69e2021-02-26 10:42:10 -0800831void BufferStateLayer::tracePendingBufferCount(int32_t pendingBuffers) {
832 ATRACE_INT(mBlastTransactionName.c_str(), pendingBuffers);
Robert Carr7121caf2020-12-15 13:07:32 -0800833}
834
Robert Carr7121caf2020-12-15 13:07:32 -0800835
chaviw39d01472021-04-08 14:26:24 -0500836/*
837 * We don't want to send the layer's transform to input, but rather the
838 * parent's transform. This is because BufferStateLayer's transform is
839 * information about how the buffer is placed on screen. The parent's
840 * transform makes more sense to send since it's information about how the
841 * layer is placed on screen. This transform is used by input to determine
842 * how to go from screen space back to window space.
843 */
844ui::Transform BufferStateLayer::getInputTransform() const {
Vishnu Nairbc4ee5c2022-08-16 03:19:37 +0000845 if (!hasBufferOrSidebandStream()) {
846 return getTransform();
847 }
Rob Carrc6d2d2b2021-10-25 16:51:49 +0000848 sp<Layer> parent = mDrawingParent.promote();
chaviw39d01472021-04-08 14:26:24 -0500849 if (parent == nullptr) {
850 return ui::Transform();
851 }
852
853 return parent->getTransform();
854}
855
856/**
857 * Similar to getInputTransform, we need to update the bounds to include the transform.
858 * This is because bounds for BSL doesn't include buffer transform, where the input assumes
859 * that's already included.
860 */
861Rect BufferStateLayer::getInputBounds() const {
Vishnu Nairbc4ee5c2022-08-16 03:19:37 +0000862 if (!hasBufferOrSidebandStream()) {
863 return getCroppedBufferSize(getDrawingState());
864 }
865
chaviw39d01472021-04-08 14:26:24 -0500866 Rect bufferBounds = getCroppedBufferSize(getDrawingState());
867 if (mDrawingState.transform.getType() == ui::Transform::IDENTITY || !bufferBounds.isValid()) {
868 return bufferBounds;
869 }
870 return mDrawingState.transform.transform(bufferBounds);
871}
872
Ady Abraham9dada822022-02-03 10:26:59 -0800873bool BufferStateLayer::simpleBufferUpdate(const layer_state_t& s) const {
874 const uint64_t requiredFlags = layer_state_t::eBufferChanged;
875
876 const uint64_t deniedFlags = layer_state_t::eProducerDisconnect | layer_state_t::eLayerChanged |
877 layer_state_t::eRelativeLayerChanged | layer_state_t::eTransparentRegionChanged |
878 layer_state_t::eFlagsChanged | layer_state_t::eBlurRegionsChanged |
879 layer_state_t::eLayerStackChanged | layer_state_t::eAutoRefreshChanged |
880 layer_state_t::eReparent;
881
882 const uint64_t allowedFlags = layer_state_t::eHasListenerCallbacksChanged |
883 layer_state_t::eFrameRateSelectionPriority | layer_state_t::eFrameRateChanged |
884 layer_state_t::eSurfaceDamageRegionChanged | layer_state_t::eApiChanged |
885 layer_state_t::eMetadataChanged | layer_state_t::eDropInputModeChanged |
886 layer_state_t::eInputInfoChanged;
887
888 if ((s.what & requiredFlags) != requiredFlags) {
889 ALOGV("%s: false [missing required flags 0x%" PRIx64 "]", __func__,
890 (s.what | requiredFlags) & ~s.what);
891 return false;
892 }
893
894 if (s.what & deniedFlags) {
895 ALOGV("%s: false [has denied flags 0x%" PRIx64 "]", __func__, s.what & deniedFlags);
896 return false;
897 }
898
899 if (s.what & allowedFlags) {
900 ALOGV("%s: [has allowed flags 0x%" PRIx64 "]", __func__, s.what & allowedFlags);
901 }
902
903 if (s.what & layer_state_t::ePositionChanged) {
904 if (mRequestedTransform.tx() != s.x || mRequestedTransform.ty() != s.y) {
905 ALOGV("%s: false [ePositionChanged changed]", __func__);
906 return false;
907 }
908 }
909
910 if (s.what & layer_state_t::eAlphaChanged) {
911 if (mDrawingState.color.a != s.alpha) {
912 ALOGV("%s: false [eAlphaChanged changed]", __func__);
913 return false;
914 }
915 }
916
917 if (s.what & layer_state_t::eColorTransformChanged) {
918 if (mDrawingState.colorTransform != s.colorTransform) {
919 ALOGV("%s: false [eColorTransformChanged changed]", __func__);
920 return false;
921 }
922 }
923
924 if (s.what & layer_state_t::eBackgroundColorChanged) {
925 if (mDrawingState.bgColorLayer || s.bgColorAlpha != 0) {
926 ALOGV("%s: false [eBackgroundColorChanged changed]", __func__);
927 return false;
928 }
929 }
930
931 if (s.what & layer_state_t::eMatrixChanged) {
932 if (mRequestedTransform.dsdx() != s.matrix.dsdx ||
933 mRequestedTransform.dtdy() != s.matrix.dtdy ||
934 mRequestedTransform.dtdx() != s.matrix.dtdx ||
935 mRequestedTransform.dsdy() != s.matrix.dsdy) {
936 ALOGV("%s: false [eMatrixChanged changed]", __func__);
937 return false;
938 }
939 }
940
941 if (s.what & layer_state_t::eCornerRadiusChanged) {
942 if (mDrawingState.cornerRadius != s.cornerRadius) {
943 ALOGV("%s: false [eCornerRadiusChanged changed]", __func__);
944 return false;
945 }
946 }
947
948 if (s.what & layer_state_t::eBackgroundBlurRadiusChanged) {
949 if (mDrawingState.backgroundBlurRadius != static_cast<int>(s.backgroundBlurRadius)) {
950 ALOGV("%s: false [eBackgroundBlurRadiusChanged changed]", __func__);
951 return false;
952 }
953 }
954
955 if (s.what & layer_state_t::eTransformChanged) {
956 if (mDrawingState.bufferTransform != s.transform) {
957 ALOGV("%s: false [eTransformChanged changed]", __func__);
958 return false;
959 }
960 }
961
962 if (s.what & layer_state_t::eTransformToDisplayInverseChanged) {
963 if (mDrawingState.transformToDisplayInverse != s.transformToDisplayInverse) {
964 ALOGV("%s: false [eTransformToDisplayInverseChanged changed]", __func__);
965 return false;
966 }
967 }
968
969 if (s.what & layer_state_t::eCropChanged) {
970 if (mDrawingState.crop != s.crop) {
971 ALOGV("%s: false [eCropChanged changed]", __func__);
972 return false;
973 }
974 }
975
976 if (s.what & layer_state_t::eDataspaceChanged) {
977 if (mDrawingState.dataspace != s.dataspace) {
978 ALOGV("%s: false [eDataspaceChanged changed]", __func__);
979 return false;
980 }
981 }
982
983 if (s.what & layer_state_t::eHdrMetadataChanged) {
984 if (mDrawingState.hdrMetadata != s.hdrMetadata) {
985 ALOGV("%s: false [eHdrMetadataChanged changed]", __func__);
986 return false;
987 }
988 }
989
990 if (s.what & layer_state_t::eSidebandStreamChanged) {
991 if (mDrawingState.sidebandStream != s.sidebandStream) {
992 ALOGV("%s: false [eSidebandStreamChanged changed]", __func__);
993 return false;
994 }
995 }
996
997 if (s.what & layer_state_t::eColorSpaceAgnosticChanged) {
998 if (mDrawingState.colorSpaceAgnostic != s.colorSpaceAgnostic) {
999 ALOGV("%s: false [eColorSpaceAgnosticChanged changed]", __func__);
1000 return false;
1001 }
1002 }
1003
1004 if (s.what & layer_state_t::eShadowRadiusChanged) {
1005 if (mDrawingState.shadowRadius != s.shadowRadius) {
1006 ALOGV("%s: false [eShadowRadiusChanged changed]", __func__);
1007 return false;
1008 }
1009 }
1010
1011 if (s.what & layer_state_t::eFixedTransformHintChanged) {
1012 if (mDrawingState.fixedTransformHint != s.fixedTransformHint) {
1013 ALOGV("%s: false [eFixedTransformHintChanged changed]", __func__);
1014 return false;
1015 }
1016 }
1017
1018 if (s.what & layer_state_t::eTrustedOverlayChanged) {
1019 if (mDrawingState.isTrustedOverlay != s.isTrustedOverlay) {
1020 ALOGV("%s: false [eTrustedOverlayChanged changed]", __func__);
1021 return false;
1022 }
1023 }
1024
1025 if (s.what & layer_state_t::eStretchChanged) {
1026 StretchEffect temp = s.stretchEffect;
1027 temp.sanitize();
1028 if (mDrawingState.stretchEffect != temp) {
1029 ALOGV("%s: false [eStretchChanged changed]", __func__);
1030 return false;
1031 }
1032 }
1033
1034 if (s.what & layer_state_t::eBufferCropChanged) {
1035 if (mDrawingState.bufferCrop != s.bufferCrop) {
1036 ALOGV("%s: false [eBufferCropChanged changed]", __func__);
1037 return false;
1038 }
1039 }
1040
1041 if (s.what & layer_state_t::eDestinationFrameChanged) {
1042 if (mDrawingState.destinationFrame != s.destinationFrame) {
1043 ALOGV("%s: false [eDestinationFrameChanged changed]", __func__);
1044 return false;
1045 }
1046 }
1047
Sally Qi81d95e62022-03-21 19:41:33 -07001048 if (s.what & layer_state_t::eDimmingEnabledChanged) {
1049 if (mDrawingState.dimmingEnabled != s.dimmingEnabled) {
1050 ALOGV("%s: false [eDimmingEnabledChanged changed]", __func__);
1051 return false;
1052 }
1053 }
1054
Ady Abraham9dada822022-02-03 10:26:59 -08001055 ALOGV("%s: true", __func__);
1056 return true;
1057}
1058
Vishnu Naira72f6c02022-07-01 05:33:08 +00001059void BufferStateLayer::useSurfaceDamage() {
1060 if (mFlinger->mForceFullDamage) {
1061 surfaceDamageRegion = Region::INVALID_REGION;
1062 } else {
1063 surfaceDamageRegion = mBufferInfo.mSurfaceDamage;
1064 }
1065}
1066
1067void BufferStateLayer::useEmptyDamage() {
1068 surfaceDamageRegion.clear();
1069}
1070
1071bool BufferStateLayer::isOpaque(const Layer::State& s) const {
1072 // if we don't have a buffer or sidebandStream yet, we're translucent regardless of the
1073 // layer's opaque flag.
Vishnu Nairbc4ee5c2022-08-16 03:19:37 +00001074 if (!hasSomethingToDraw()) {
Vishnu Naira72f6c02022-07-01 05:33:08 +00001075 return false;
1076 }
1077
Vishnu Nairbc4ee5c2022-08-16 03:19:37 +00001078 // if the layer has the opaque flag, then we're always opaque
1079 if ((s.flags & layer_state_t::eLayerOpaque) == layer_state_t::eLayerOpaque) {
1080 return true;
1081 }
1082
1083 // If the buffer has no alpha channel, then we are opaque
1084 if (hasBufferOrSidebandStream() && isOpaqueFormat(getPixelFormat())) {
1085 return true;
1086 }
1087
1088 // Lastly consider the layer opaque if drawing a color with alpha == 1.0
1089 return fillsColor() && getAlpha() == 1.0_hf;
Vishnu Naira72f6c02022-07-01 05:33:08 +00001090}
1091
1092bool BufferStateLayer::canReceiveInput() const {
1093 return !isHiddenByPolicy() && (mBufferInfo.mBuffer == nullptr || getAlpha() > 0.0f);
1094}
1095
1096bool BufferStateLayer::isVisible() const {
Vishnu Nairbc4ee5c2022-08-16 03:19:37 +00001097 if (!hasSomethingToDraw()) {
1098 return false;
1099 }
1100
1101 if (isHiddenByPolicy()) {
1102 return false;
1103 }
1104
1105 return getAlpha() > 0.0f || hasBlur();
Vishnu Naira72f6c02022-07-01 05:33:08 +00001106}
1107
Vishnu Naira72f6c02022-07-01 05:33:08 +00001108std::optional<compositionengine::LayerFE::LayerSettings> BufferStateLayer::prepareClientComposition(
Patrick Williams16d8b2c2022-08-08 17:29:05 +00001109 compositionengine::LayerFE::ClientCompositionTargetSettings& targetSettings) const {
1110 std::optional<compositionengine::LayerFE::LayerSettings> layerSettings =
1111 prepareClientCompositionInternal(targetSettings);
1112 // Nothing to render.
1113 if (!layerSettings) {
1114 return {};
1115 }
1116
1117 // HWC requests to clear this layer.
1118 if (targetSettings.clearContent) {
1119 prepareClearClientComposition(*layerSettings, false /* blackout */);
1120 return *layerSettings;
1121 }
1122
1123 // set the shadow for the layer if needed
1124 prepareShadowClientComposition(*layerSettings, targetSettings.viewport);
1125
1126 return *layerSettings;
1127}
1128
1129std::optional<compositionengine::LayerFE::LayerSettings>
1130BufferStateLayer::prepareClientCompositionInternal(
1131 compositionengine::LayerFE::ClientCompositionTargetSettings& targetSettings) const {
Vishnu Naira72f6c02022-07-01 05:33:08 +00001132 ATRACE_CALL();
1133
1134 std::optional<compositionengine::LayerFE::LayerSettings> result =
1135 Layer::prepareClientComposition(targetSettings);
1136 if (!result) {
1137 return result;
1138 }
1139
Vishnu Nairbc4ee5c2022-08-16 03:19:37 +00001140 if (hasEffect()) {
1141 prepareEffectsClientComposition(*result, targetSettings);
1142 return result;
1143 }
1144
Vishnu Naira72f6c02022-07-01 05:33:08 +00001145 if (CC_UNLIKELY(mBufferInfo.mBuffer == 0) && mSidebandStream != nullptr) {
1146 // For surfaceview of tv sideband, there is no activeBuffer
1147 // in bufferqueue, we need return LayerSettings.
1148 return result;
1149 }
1150 const bool blackOutLayer = (isProtected() && !targetSettings.supportsProtectedContent) ||
1151 ((isSecure() || isProtected()) && !targetSettings.isSecure);
1152 const bool bufferCanBeUsedAsHwTexture =
1153 mBufferInfo.mBuffer->getUsage() & GraphicBuffer::USAGE_HW_TEXTURE;
1154 compositionengine::LayerFE::LayerSettings& layer = *result;
1155 if (blackOutLayer || !bufferCanBeUsedAsHwTexture) {
1156 ALOGE_IF(!bufferCanBeUsedAsHwTexture, "%s is blacked out as buffer is not gpu readable",
1157 mName.c_str());
1158 prepareClearClientComposition(layer, true /* blackout */);
1159 return layer;
1160 }
1161
1162 const State& s(getDrawingState());
1163 layer.source.buffer.buffer = mBufferInfo.mBuffer;
1164 layer.source.buffer.isOpaque = isOpaque(s);
1165 layer.source.buffer.fence = mBufferInfo.mFence;
1166 layer.source.buffer.textureName = mTextureName;
1167 layer.source.buffer.usePremultipliedAlpha = getPremultipledAlpha();
1168 layer.source.buffer.isY410BT2020 = isHdrY410();
1169 bool hasSmpte2086 = mBufferInfo.mHdrMetadata.validTypes & HdrMetadata::SMPTE2086;
1170 bool hasCta861_3 = mBufferInfo.mHdrMetadata.validTypes & HdrMetadata::CTA861_3;
1171 float maxLuminance = 0.f;
1172 if (hasSmpte2086 && hasCta861_3) {
1173 maxLuminance = std::min(mBufferInfo.mHdrMetadata.smpte2086.maxLuminance,
1174 mBufferInfo.mHdrMetadata.cta8613.maxContentLightLevel);
1175 } else if (hasSmpte2086) {
1176 maxLuminance = mBufferInfo.mHdrMetadata.smpte2086.maxLuminance;
1177 } else if (hasCta861_3) {
1178 maxLuminance = mBufferInfo.mHdrMetadata.cta8613.maxContentLightLevel;
1179 } else {
1180 switch (layer.sourceDataspace & HAL_DATASPACE_TRANSFER_MASK) {
1181 case HAL_DATASPACE_TRANSFER_ST2084:
1182 case HAL_DATASPACE_TRANSFER_HLG:
1183 // Behavior-match previous releases for HDR content
1184 maxLuminance = defaultMaxLuminance;
1185 break;
1186 }
1187 }
1188 layer.source.buffer.maxLuminanceNits = maxLuminance;
1189 layer.frameNumber = mCurrentFrameNumber;
1190 layer.bufferId = mBufferInfo.mBuffer ? mBufferInfo.mBuffer->getId() : 0;
1191
1192 const bool useFiltering =
1193 targetSettings.needsFiltering || mNeedsFiltering || bufferNeedsFiltering();
1194
1195 // Query the texture matrix given our current filtering mode.
1196 float textureMatrix[16];
1197 getDrawingTransformMatrix(useFiltering, textureMatrix);
1198
1199 if (getTransformToDisplayInverse()) {
1200 /*
1201 * the code below applies the primary display's inverse transform to
1202 * the texture transform
1203 */
1204 uint32_t transform = DisplayDevice::getPrimaryDisplayRotationFlags();
1205 mat4 tr = inverseOrientation(transform);
1206
1207 /**
1208 * TODO(b/36727915): This is basically a hack.
1209 *
1210 * Ensure that regardless of the parent transformation,
1211 * this buffer is always transformed from native display
1212 * orientation to display orientation. For example, in the case
1213 * of a camera where the buffer remains in native orientation,
1214 * we want the pixels to always be upright.
1215 */
1216 sp<Layer> p = mDrawingParent.promote();
1217 if (p != nullptr) {
1218 const auto parentTransform = p->getTransform();
1219 tr = tr * inverseOrientation(parentTransform.getOrientation());
1220 }
1221
1222 // and finally apply it to the original texture matrix
1223 const mat4 texTransform(mat4(static_cast<const float*>(textureMatrix)) * tr);
1224 memcpy(textureMatrix, texTransform.asArray(), sizeof(textureMatrix));
1225 }
1226
1227 const Rect win{getBounds()};
1228 float bufferWidth = getBufferSize(s).getWidth();
1229 float bufferHeight = getBufferSize(s).getHeight();
1230
1231 // BufferStateLayers can have a "buffer size" of [0, 0, -1, -1] when no display frame has
1232 // been set and there is no parent layer bounds. In that case, the scale is meaningless so
1233 // ignore them.
1234 if (!getBufferSize(s).isValid()) {
1235 bufferWidth = float(win.right) - float(win.left);
1236 bufferHeight = float(win.bottom) - float(win.top);
1237 }
1238
1239 const float scaleHeight = (float(win.bottom) - float(win.top)) / bufferHeight;
1240 const float scaleWidth = (float(win.right) - float(win.left)) / bufferWidth;
1241 const float translateY = float(win.top) / bufferHeight;
1242 const float translateX = float(win.left) / bufferWidth;
1243
1244 // Flip y-coordinates because GLConsumer expects OpenGL convention.
1245 mat4 tr = mat4::translate(vec4(.5f, .5f, 0.f, 1.f)) * mat4::scale(vec4(1.f, -1.f, 1.f, 1.f)) *
1246 mat4::translate(vec4(-.5f, -.5f, 0.f, 1.f)) *
1247 mat4::translate(vec4(translateX, translateY, 0.f, 1.f)) *
1248 mat4::scale(vec4(scaleWidth, scaleHeight, 1.0f, 1.0f));
1249
1250 layer.source.buffer.useTextureFiltering = useFiltering;
1251 layer.source.buffer.textureTransform = mat4(static_cast<const float*>(textureMatrix)) * tr;
1252
1253 return layer;
1254}
1255
Vishnu Nairbc4ee5c2022-08-16 03:19:37 +00001256void BufferStateLayer::prepareEffectsClientComposition(
1257 compositionengine::LayerFE::LayerSettings& layerSettings,
1258 compositionengine::LayerFE::ClientCompositionTargetSettings& targetSettings) const {
1259 // If fill bounds are occluded or the fill color is invalid skip the fill settings.
1260 if (targetSettings.realContentIsVisible && fillsColor()) {
1261 // Set color for color fill settings.
1262 layerSettings.source.solidColor = getColor().rgb;
1263 } else if (hasBlur() || drawShadows()) {
1264 layerSettings.skipContentDraw = true;
1265 }
1266}
1267
Vishnu Naira72f6c02022-07-01 05:33:08 +00001268bool BufferStateLayer::isHdrY410() const {
1269 // pixel format is HDR Y410 masquerading as RGBA_1010102
1270 return (mBufferInfo.mDataspace == ui::Dataspace::BT2020_ITU_PQ &&
1271 mBufferInfo.mApi == NATIVE_WINDOW_API_MEDIA &&
1272 mBufferInfo.mPixelFormat == HAL_PIXEL_FORMAT_RGBA_1010102);
1273}
1274
1275sp<compositionengine::LayerFE> BufferStateLayer::getCompositionEngineLayerFE() const {
Vishnu Nairbc4ee5c2022-08-16 03:19:37 +00001276 // There's no need to get a CE Layer if the layer isn't going to draw anything.
1277 if (hasSomethingToDraw()) {
1278 return asLayerFE();
1279 } else {
1280 return nullptr;
1281 }
Vishnu Naira72f6c02022-07-01 05:33:08 +00001282}
1283
1284compositionengine::LayerFECompositionState* BufferStateLayer::editCompositionState() {
1285 return mCompositionState.get();
1286}
1287
1288const compositionengine::LayerFECompositionState* BufferStateLayer::getCompositionState() const {
1289 return mCompositionState.get();
1290}
1291
1292void BufferStateLayer::preparePerFrameCompositionState() {
1293 Layer::preparePerFrameCompositionState();
Vishnu Nairbc4ee5c2022-08-16 03:19:37 +00001294 if (hasBufferOrSidebandStream()) {
1295 preparePerFrameBufferCompositionState();
1296 } else {
1297 preparePerFrameEffectsCompositionState();
1298 }
1299}
Vishnu Naira72f6c02022-07-01 05:33:08 +00001300
Vishnu Nairbc4ee5c2022-08-16 03:19:37 +00001301void BufferStateLayer::preparePerFrameBufferCompositionState() {
Vishnu Naira72f6c02022-07-01 05:33:08 +00001302 // Sideband layers
1303 auto* compositionState = editCompositionState();
1304 if (compositionState->sidebandStream.get() && !compositionState->sidebandStreamHasFrame) {
1305 compositionState->compositionType =
1306 aidl::android::hardware::graphics::composer3::Composition::SIDEBAND;
1307 return;
1308 } else if ((mDrawingState.flags & layer_state_t::eLayerIsDisplayDecoration) != 0) {
1309 compositionState->compositionType =
1310 aidl::android::hardware::graphics::composer3::Composition::DISPLAY_DECORATION;
1311 } else {
1312 // Normal buffer layers
1313 compositionState->hdrMetadata = mBufferInfo.mHdrMetadata;
1314 compositionState->compositionType = mPotentialCursor
1315 ? aidl::android::hardware::graphics::composer3::Composition::CURSOR
1316 : aidl::android::hardware::graphics::composer3::Composition::DEVICE;
1317 }
1318
1319 compositionState->buffer = getBuffer();
1320 compositionState->bufferSlot = (mBufferInfo.mBufferSlot == BufferQueue::INVALID_BUFFER_SLOT)
1321 ? 0
1322 : mBufferInfo.mBufferSlot;
1323 compositionState->acquireFence = mBufferInfo.mFence;
1324 compositionState->frameNumber = mBufferInfo.mFrameNumber;
1325 compositionState->sidebandStreamHasFrame = false;
1326}
1327
Vishnu Nairbc4ee5c2022-08-16 03:19:37 +00001328void BufferStateLayer::preparePerFrameEffectsCompositionState() {
1329 auto* compositionState = editCompositionState();
1330 compositionState->color = getColor();
1331 compositionState->compositionType =
1332 aidl::android::hardware::graphics::composer3::Composition::SOLID_COLOR;
1333}
1334
Vishnu Naira72f6c02022-07-01 05:33:08 +00001335void BufferStateLayer::onPostComposition(const DisplayDevice* display,
1336 const std::shared_ptr<FenceTime>& glDoneFence,
1337 const std::shared_ptr<FenceTime>& presentFence,
1338 const CompositorTiming& compositorTiming) {
1339 // mFrameLatencyNeeded is true when a new frame was latched for the
1340 // composition.
1341 if (!mBufferInfo.mFrameLatencyNeeded) return;
1342
Vishnu Nair397a0e32022-07-26 00:01:48 +00001343 for (const auto& handle : mDrawingState.callbackHandles) {
1344 handle->gpuCompositionDoneFence = glDoneFence;
1345 handle->compositorTiming = compositorTiming;
1346 }
Vishnu Naira72f6c02022-07-01 05:33:08 +00001347
1348 // Update mFrameTracker.
1349 nsecs_t desiredPresentTime = mBufferInfo.mDesiredPresentTime;
1350 mFrameTracker.setDesiredPresentTime(desiredPresentTime);
1351
1352 const int32_t layerId = getSequence();
1353 mFlinger->mTimeStats->setDesiredTime(layerId, mCurrentFrameNumber, desiredPresentTime);
1354
1355 const auto outputLayer = findOutputLayerForDisplay(display);
1356 if (outputLayer && outputLayer->requiresClientComposition()) {
1357 nsecs_t clientCompositionTimestamp = outputLayer->getState().clientCompositionTimestamp;
1358 mFlinger->mFrameTracer->traceTimestamp(layerId, getCurrentBufferId(), mCurrentFrameNumber,
1359 clientCompositionTimestamp,
1360 FrameTracer::FrameEvent::FALLBACK_COMPOSITION);
1361 // Update the SurfaceFrames in the drawing state
1362 if (mDrawingState.bufferSurfaceFrameTX) {
1363 mDrawingState.bufferSurfaceFrameTX->setGpuComposition();
1364 }
1365 for (auto& [token, surfaceFrame] : mDrawingState.bufferlessSurfaceFramesTX) {
1366 surfaceFrame->setGpuComposition();
1367 }
1368 }
1369
1370 std::shared_ptr<FenceTime> frameReadyFence = mBufferInfo.mFenceTime;
1371 if (frameReadyFence->isValid()) {
1372 mFrameTracker.setFrameReadyFence(std::move(frameReadyFence));
1373 } else {
1374 // There was no fence for this frame, so assume that it was ready
1375 // to be presented at the desired present time.
1376 mFrameTracker.setFrameReadyTime(desiredPresentTime);
1377 }
1378
1379 if (display) {
1380 const Fps refreshRate = display->refreshRateConfigs().getActiveMode()->getFps();
1381 const std::optional<Fps> renderRate =
1382 mFlinger->mScheduler->getFrameRateOverride(getOwnerUid());
1383
1384 const auto vote = frameRateToSetFrameRateVotePayload(mDrawingState.frameRate);
1385 const auto gameMode = getGameMode();
1386
1387 if (presentFence->isValid()) {
1388 mFlinger->mTimeStats->setPresentFence(layerId, mCurrentFrameNumber, presentFence,
1389 refreshRate, renderRate, vote, gameMode);
1390 mFlinger->mFrameTracer->traceFence(layerId, getCurrentBufferId(), mCurrentFrameNumber,
1391 presentFence,
1392 FrameTracer::FrameEvent::PRESENT_FENCE);
1393 mFrameTracker.setActualPresentFence(std::shared_ptr<FenceTime>(presentFence));
1394 } else if (const auto displayId = PhysicalDisplayId::tryCast(display->getId());
1395 displayId && mFlinger->getHwComposer().isConnected(*displayId)) {
1396 // The HWC doesn't support present fences, so use the refresh
1397 // timestamp instead.
1398 const nsecs_t actualPresentTime = display->getRefreshTimestamp();
1399 mFlinger->mTimeStats->setPresentTime(layerId, mCurrentFrameNumber, actualPresentTime,
1400 refreshRate, renderRate, vote, gameMode);
1401 mFlinger->mFrameTracer->traceTimestamp(layerId, getCurrentBufferId(),
1402 mCurrentFrameNumber, actualPresentTime,
1403 FrameTracer::FrameEvent::PRESENT_FENCE);
1404 mFrameTracker.setActualPresentTime(actualPresentTime);
1405 }
1406 }
1407
1408 mFrameTracker.advanceFrame();
1409 mBufferInfo.mFrameLatencyNeeded = false;
1410}
1411
Vishnu Nair397a0e32022-07-26 00:01:48 +00001412bool BufferStateLayer::latchBuffer(bool& recomputeVisibleRegions, nsecs_t latchTime) {
Vishnu Naira72f6c02022-07-01 05:33:08 +00001413 ATRACE_FORMAT_INSTANT("latchBuffer %s - %" PRIu64, getDebugName(),
1414 getDrawingState().frameNumber);
1415
1416 bool refreshRequired = latchSidebandStream(recomputeVisibleRegions);
1417
1418 if (refreshRequired) {
1419 return refreshRequired;
1420 }
1421
1422 // If the head buffer's acquire fence hasn't signaled yet, return and
1423 // try again later
1424 if (!fenceHasSignaled()) {
1425 ATRACE_NAME("!fenceHasSignaled()");
1426 mFlinger->onLayerUpdate();
1427 return false;
1428 }
1429
Vishnu Nair397a0e32022-07-26 00:01:48 +00001430 updateTexImage(latchTime);
1431 if (mDrawingState.buffer == nullptr) {
1432 return false;
1433 }
1434
Vishnu Naira72f6c02022-07-01 05:33:08 +00001435 // Capture the old state of the layer for comparisons later
Vishnu Naira72f6c02022-07-01 05:33:08 +00001436 BufferInfo oldBufferInfo = mBufferInfo;
Vishnu Nair397a0e32022-07-26 00:01:48 +00001437 const bool oldOpacity = isOpaque(mDrawingState);
1438 mPreviousFrameNumber = mCurrentFrameNumber;
1439 mCurrentFrameNumber = mDrawingState.frameNumber;
Vishnu Naira72f6c02022-07-01 05:33:08 +00001440 gatherBufferInfo();
1441
1442 if (oldBufferInfo.mBuffer == nullptr) {
1443 // the first time we receive a buffer, we need to trigger a
1444 // geometry invalidation.
1445 recomputeVisibleRegions = true;
1446 }
1447
1448 if ((mBufferInfo.mCrop != oldBufferInfo.mCrop) ||
1449 (mBufferInfo.mTransform != oldBufferInfo.mTransform) ||
1450 (mBufferInfo.mScaleMode != oldBufferInfo.mScaleMode) ||
1451 (mBufferInfo.mTransformToDisplayInverse != oldBufferInfo.mTransformToDisplayInverse)) {
1452 recomputeVisibleRegions = true;
1453 }
1454
1455 if (oldBufferInfo.mBuffer != nullptr) {
1456 uint32_t bufWidth = mBufferInfo.mBuffer->getWidth();
1457 uint32_t bufHeight = mBufferInfo.mBuffer->getHeight();
1458 if (bufWidth != oldBufferInfo.mBuffer->getWidth() ||
1459 bufHeight != oldBufferInfo.mBuffer->getHeight()) {
1460 recomputeVisibleRegions = true;
1461 }
1462 }
1463
Vishnu Nair397a0e32022-07-26 00:01:48 +00001464 if (oldOpacity != isOpaque(mDrawingState)) {
Vishnu Naira72f6c02022-07-01 05:33:08 +00001465 recomputeVisibleRegions = true;
1466 }
1467
1468 return true;
1469}
1470
1471bool BufferStateLayer::hasReadyFrame() const {
1472 return hasFrameUpdate() || getSidebandStreamChanged() || getAutoRefresh();
1473}
1474
1475bool BufferStateLayer::isProtected() const {
1476 return (mBufferInfo.mBuffer != nullptr) &&
1477 (mBufferInfo.mBuffer->getUsage() & GRALLOC_USAGE_PROTECTED);
1478}
1479
1480// As documented in libhardware header, formats in the range
1481// 0x100 - 0x1FF are specific to the HAL implementation, and
1482// are known to have no alpha channel
1483// TODO: move definition for device-specific range into
1484// hardware.h, instead of using hard-coded values here.
1485#define HARDWARE_IS_DEVICE_FORMAT(f) ((f) >= 0x100 && (f) <= 0x1FF)
1486
Vishnu Nairbc4ee5c2022-08-16 03:19:37 +00001487bool BufferStateLayer::isOpaqueFormat(PixelFormat format) {
Vishnu Naira72f6c02022-07-01 05:33:08 +00001488 if (HARDWARE_IS_DEVICE_FORMAT(format)) {
1489 return true;
1490 }
1491 switch (format) {
1492 case PIXEL_FORMAT_RGBA_8888:
1493 case PIXEL_FORMAT_BGRA_8888:
1494 case PIXEL_FORMAT_RGBA_FP16:
1495 case PIXEL_FORMAT_RGBA_1010102:
1496 case PIXEL_FORMAT_R_8:
1497 return false;
1498 }
1499 // in all other case, we have no blending (also for unknown formats)
1500 return true;
1501}
1502
1503bool BufferStateLayer::needsFiltering(const DisplayDevice* display) const {
Vishnu Nairbc4ee5c2022-08-16 03:19:37 +00001504 if (!hasBufferOrSidebandStream()) {
1505 return false;
1506 }
Vishnu Naira72f6c02022-07-01 05:33:08 +00001507 const auto outputLayer = findOutputLayerForDisplay(display);
1508 if (outputLayer == nullptr) {
1509 return false;
1510 }
1511
1512 // We need filtering if the sourceCrop rectangle size does not match the
1513 // displayframe rectangle size (not a 1:1 render)
1514 const auto& compositionState = outputLayer->getState();
1515 const auto displayFrame = compositionState.displayFrame;
1516 const auto sourceCrop = compositionState.sourceCrop;
1517 return sourceCrop.getHeight() != displayFrame.getHeight() ||
1518 sourceCrop.getWidth() != displayFrame.getWidth();
1519}
1520
1521bool BufferStateLayer::needsFilteringForScreenshots(
1522 const DisplayDevice* display, const ui::Transform& inverseParentTransform) const {
Vishnu Nairbc4ee5c2022-08-16 03:19:37 +00001523 if (!hasBufferOrSidebandStream()) {
1524 return false;
1525 }
Vishnu Naira72f6c02022-07-01 05:33:08 +00001526 const auto outputLayer = findOutputLayerForDisplay(display);
1527 if (outputLayer == nullptr) {
1528 return false;
1529 }
1530
1531 // We need filtering if the sourceCrop rectangle size does not match the
1532 // viewport rectangle size (not a 1:1 render)
1533 const auto& compositionState = outputLayer->getState();
1534 const ui::Transform& displayTransform = display->getTransform();
1535 const ui::Transform inverseTransform = inverseParentTransform * displayTransform.inverse();
1536 // Undo the transformation of the displayFrame so that we're back into
1537 // layer-stack space.
1538 const Rect frame = inverseTransform.transform(compositionState.displayFrame);
1539 const FloatRect sourceCrop = compositionState.sourceCrop;
1540
1541 int32_t frameHeight = frame.getHeight();
1542 int32_t frameWidth = frame.getWidth();
1543 // If the display transform had a rotational component then undo the
1544 // rotation so that the orientation matches the source crop.
1545 if (displayTransform.getOrientation() & ui::Transform::ROT_90) {
1546 std::swap(frameHeight, frameWidth);
1547 }
1548 return sourceCrop.getHeight() != frameHeight || sourceCrop.getWidth() != frameWidth;
1549}
1550
1551void BufferStateLayer::latchAndReleaseBuffer() {
1552 if (hasReadyFrame()) {
1553 bool ignored = false;
Vishnu Nair397a0e32022-07-26 00:01:48 +00001554 latchBuffer(ignored, systemTime());
Vishnu Naira72f6c02022-07-01 05:33:08 +00001555 }
1556 releasePendingBuffer(systemTime());
1557}
1558
1559PixelFormat BufferStateLayer::getPixelFormat() const {
1560 return mBufferInfo.mPixelFormat;
1561}
1562
1563bool BufferStateLayer::getTransformToDisplayInverse() const {
1564 return mBufferInfo.mTransformToDisplayInverse;
1565}
1566
1567Rect BufferStateLayer::getBufferCrop() const {
1568 // this is the crop rectangle that applies to the buffer
1569 // itself (as opposed to the window)
1570 if (!mBufferInfo.mCrop.isEmpty()) {
1571 // if the buffer crop is defined, we use that
1572 return mBufferInfo.mCrop;
1573 } else if (mBufferInfo.mBuffer != nullptr) {
1574 // otherwise we use the whole buffer
1575 return mBufferInfo.mBuffer->getBounds();
1576 } else {
1577 // if we don't have a buffer yet, we use an empty/invalid crop
1578 return Rect();
1579 }
1580}
1581
1582uint32_t BufferStateLayer::getBufferTransform() const {
1583 return mBufferInfo.mTransform;
1584}
1585
1586ui::Dataspace BufferStateLayer::getDataSpace() const {
Vishnu Nairbc4ee5c2022-08-16 03:19:37 +00001587 return mDrawingState.dataspaceRequested ? getRequestedDataSpace() : ui::Dataspace::UNKNOWN;
1588}
1589
1590ui::Dataspace BufferStateLayer::getRequestedDataSpace() const {
1591 return hasBufferOrSidebandStream() ? mBufferInfo.mDataspace : mDrawingState.dataspace;
Vishnu Naira72f6c02022-07-01 05:33:08 +00001592}
1593
1594ui::Dataspace BufferStateLayer::translateDataspace(ui::Dataspace dataspace) {
1595 ui::Dataspace updatedDataspace = dataspace;
1596 // translate legacy dataspaces to modern dataspaces
1597 switch (dataspace) {
1598 case ui::Dataspace::SRGB:
1599 updatedDataspace = ui::Dataspace::V0_SRGB;
1600 break;
1601 case ui::Dataspace::SRGB_LINEAR:
1602 updatedDataspace = ui::Dataspace::V0_SRGB_LINEAR;
1603 break;
1604 case ui::Dataspace::JFIF:
1605 updatedDataspace = ui::Dataspace::V0_JFIF;
1606 break;
1607 case ui::Dataspace::BT601_625:
1608 updatedDataspace = ui::Dataspace::V0_BT601_625;
1609 break;
1610 case ui::Dataspace::BT601_525:
1611 updatedDataspace = ui::Dataspace::V0_BT601_525;
1612 break;
1613 case ui::Dataspace::BT709:
1614 updatedDataspace = ui::Dataspace::V0_BT709;
1615 break;
1616 default:
1617 break;
1618 }
1619
1620 return updatedDataspace;
1621}
1622
1623sp<GraphicBuffer> BufferStateLayer::getBuffer() const {
1624 return mBufferInfo.mBuffer ? mBufferInfo.mBuffer->getBuffer() : nullptr;
1625}
1626
Patrick Williams16d8b2c2022-08-08 17:29:05 +00001627void BufferStateLayer::getDrawingTransformMatrix(bool filteringEnabled, float outMatrix[16]) const {
Vishnu Naira72f6c02022-07-01 05:33:08 +00001628 sp<GraphicBuffer> buffer = getBuffer();
1629 if (!buffer) {
1630 ALOGE("Buffer should not be null!");
1631 return;
1632 }
1633 GLConsumer::computeTransformMatrix(outMatrix, buffer->getWidth(), buffer->getHeight(),
1634 buffer->getPixelFormat(), mBufferInfo.mCrop,
1635 mBufferInfo.mTransform, filteringEnabled);
1636}
1637
1638void BufferStateLayer::setInitialValuesForClone(const sp<Layer>& clonedFrom) {
1639 Layer::setInitialValuesForClone(clonedFrom);
1640
Ady Abrahamd11bade2022-08-01 16:18:03 -07001641 sp<BufferStateLayer> bufferClonedFrom =
1642 sp<BufferStateLayer>::fromExisting(static_cast<BufferStateLayer*>(clonedFrom.get()));
Vishnu Naira72f6c02022-07-01 05:33:08 +00001643 mPremultipliedAlpha = bufferClonedFrom->mPremultipliedAlpha;
1644 mPotentialCursor = bufferClonedFrom->mPotentialCursor;
1645 mProtectedByApp = bufferClonedFrom->mProtectedByApp;
1646
1647 updateCloneBufferInfo();
1648}
1649
1650void BufferStateLayer::updateCloneBufferInfo() {
1651 if (!isClone() || !isClonedFromAlive()) {
1652 return;
1653 }
1654
Ady Abrahamd11bade2022-08-01 16:18:03 -07001655 sp<BufferStateLayer> clonedFrom = sp<BufferStateLayer>::fromExisting(
1656 static_cast<BufferStateLayer*>(getClonedFrom().get()));
Vishnu Naira72f6c02022-07-01 05:33:08 +00001657 mBufferInfo = clonedFrom->mBufferInfo;
1658 mSidebandStream = clonedFrom->mSidebandStream;
1659 surfaceDamageRegion = clonedFrom->surfaceDamageRegion;
1660 mCurrentFrameNumber = clonedFrom->mCurrentFrameNumber.load();
1661 mPreviousFrameNumber = clonedFrom->mPreviousFrameNumber;
1662
1663 // After buffer info is updated, the drawingState from the real layer needs to be copied into
1664 // the cloned. This is because some properties of drawingState can change when latchBuffer is
1665 // called. However, copying the drawingState would also overwrite the cloned layer's relatives
1666 // and touchableRegionCrop. Therefore, temporarily store the relatives so they can be set in
1667 // the cloned drawingState again.
1668 wp<Layer> tmpZOrderRelativeOf = mDrawingState.zOrderRelativeOf;
1669 SortedVector<wp<Layer>> tmpZOrderRelatives = mDrawingState.zOrderRelatives;
1670 wp<Layer> tmpTouchableRegionCrop = mDrawingState.touchableRegionCrop;
1671 WindowInfo tmpInputInfo = mDrawingState.inputInfo;
1672
1673 cloneDrawingState(clonedFrom.get());
1674
1675 mDrawingState.touchableRegionCrop = tmpTouchableRegionCrop;
1676 mDrawingState.zOrderRelativeOf = tmpZOrderRelativeOf;
1677 mDrawingState.zOrderRelatives = tmpZOrderRelatives;
1678 mDrawingState.inputInfo = tmpInputInfo;
1679}
1680
1681void BufferStateLayer::setTransformHint(ui::Transform::RotationFlags displayTransformHint) {
1682 mTransformHint = getFixedTransformHint();
1683 if (mTransformHint == ui::Transform::ROT_INVALID) {
1684 mTransformHint = displayTransformHint;
1685 }
1686}
1687
1688const std::shared_ptr<renderengine::ExternalTexture>& BufferStateLayer::getExternalTexture() const {
1689 return mBufferInfo.mBuffer;
1690}
1691
Vishnu Nairbc4ee5c2022-08-16 03:19:37 +00001692bool BufferStateLayer::setColor(const half3& color) {
1693 if (mDrawingState.color.r == color.r && mDrawingState.color.g == color.g &&
1694 mDrawingState.color.b == color.b) {
1695 return false;
1696 }
1697
1698 mDrawingState.sequence++;
1699 mDrawingState.color.r = color.r;
1700 mDrawingState.color.g = color.g;
1701 mDrawingState.color.b = color.b;
1702 mDrawingState.modified = true;
1703 setTransactionFlags(eTransactionNeeded);
1704 return true;
1705}
1706
1707bool BufferStateLayer::fillsColor() const {
1708 return !hasBufferOrSidebandStream() && mDrawingState.color.r >= 0.0_hf &&
1709 mDrawingState.color.g >= 0.0_hf && mDrawingState.color.b >= 0.0_hf;
1710}
1711
1712bool BufferStateLayer::hasBlur() const {
1713 return getBackgroundBlurRadius() > 0 || getDrawingState().blurRegions.size() > 0;
1714}
1715
Marissa Wall61c58622018-07-18 10:12:20 -07001716} // namespace android