blob: 51a54455d122d70c5e407a5c64be60204cd90f23 [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
Chavi Weingartena5aedbd2021-04-09 13:37:33 +0000303Rect BufferStateLayer::getCrop(const Layer::State& s) const {
304 return s.crop;
Marissa Wall61c58622018-07-18 10:12:20 -0700305}
306
307bool BufferStateLayer::setTransform(uint32_t transform) {
Robert Carr6a160312021-05-17 12:08:20 -0700308 if (mDrawingState.bufferTransform == transform) return false;
309 mDrawingState.bufferTransform = transform;
310 mDrawingState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700311 setTransactionFlags(eTransactionNeeded);
312 return true;
313}
314
315bool BufferStateLayer::setTransformToDisplayInverse(bool transformToDisplayInverse) {
Robert Carr6a160312021-05-17 12:08:20 -0700316 if (mDrawingState.transformToDisplayInverse == transformToDisplayInverse) return false;
317 mDrawingState.sequence++;
318 mDrawingState.transformToDisplayInverse = transformToDisplayInverse;
319 mDrawingState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700320 setTransactionFlags(eTransactionNeeded);
321 return true;
322}
323
324bool BufferStateLayer::setCrop(const Rect& crop) {
Robert Carr6a160312021-05-17 12:08:20 -0700325 if (mDrawingState.crop == crop) return false;
326 mDrawingState.sequence++;
327 mDrawingState.crop = crop;
Marissa Wall290ad082019-03-06 13:23:47 -0800328
Robert Carr6a160312021-05-17 12:08:20 -0700329 mDrawingState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700330 setTransactionFlags(eTransactionNeeded);
331 return true;
332}
333
chaviwf3f40fe2021-04-27 15:54:02 -0500334bool BufferStateLayer::setBufferCrop(const Rect& bufferCrop) {
Robert Carr6a160312021-05-17 12:08:20 -0700335 if (mDrawingState.bufferCrop == bufferCrop) return false;
chaviwf3f40fe2021-04-27 15:54:02 -0500336
Robert Carr6a160312021-05-17 12:08:20 -0700337 mDrawingState.sequence++;
338 mDrawingState.bufferCrop = bufferCrop;
chaviwf3f40fe2021-04-27 15:54:02 -0500339
Robert Carr6a160312021-05-17 12:08:20 -0700340 mDrawingState.modified = true;
chaviwf3f40fe2021-04-27 15:54:02 -0500341 setTransactionFlags(eTransactionNeeded);
342 return true;
343}
344
Vishnu Nair6bdec7d2021-05-10 15:01:13 -0700345bool BufferStateLayer::setDestinationFrame(const Rect& destinationFrame) {
Robert Carr6a160312021-05-17 12:08:20 -0700346 if (mDrawingState.destinationFrame == destinationFrame) return false;
Vishnu Nair6bdec7d2021-05-10 15:01:13 -0700347
Robert Carr6a160312021-05-17 12:08:20 -0700348 mDrawingState.sequence++;
349 mDrawingState.destinationFrame = destinationFrame;
Vishnu Nair6bdec7d2021-05-10 15:01:13 -0700350
Robert Carr6a160312021-05-17 12:08:20 -0700351 mDrawingState.modified = true;
Vishnu Nair6bdec7d2021-05-10 15:01:13 -0700352 setTransactionFlags(eTransactionNeeded);
353 return true;
354}
355
356// Translate destination frame into scale and position. If a destination frame is not set, use the
357// provided scale and position
Robert Carr6a160312021-05-17 12:08:20 -0700358bool BufferStateLayer::updateGeometry() {
Vishnu Naird2aaab12022-02-10 14:49:09 -0800359 if ((mDrawingState.flags & layer_state_t::eIgnoreDestinationFrame) ||
360 mDrawingState.destinationFrame.isEmpty()) {
Vishnu Nair6bdec7d2021-05-10 15:01:13 -0700361 // If destination frame is not set, use the requested transform set via
362 // BufferStateLayer::setPosition and BufferStateLayer::setMatrix.
Robert Carr6a160312021-05-17 12:08:20 -0700363 return assignTransform(&mDrawingState.transform, mRequestedTransform);
Vishnu Nair6bdec7d2021-05-10 15:01:13 -0700364 }
365
Robert Carr6a160312021-05-17 12:08:20 -0700366 Rect destRect = mDrawingState.destinationFrame;
Vishnu Nair6bdec7d2021-05-10 15:01:13 -0700367 int32_t destW = destRect.width();
368 int32_t destH = destRect.height();
369 if (destRect.left < 0) {
370 destRect.left = 0;
371 destRect.right = destW;
372 }
373 if (destRect.top < 0) {
374 destRect.top = 0;
375 destRect.bottom = destH;
376 }
377
Robert Carr6a160312021-05-17 12:08:20 -0700378 if (!mDrawingState.buffer) {
Vishnu Nair6bdec7d2021-05-10 15:01:13 -0700379 ui::Transform t;
380 t.set(destRect.left, destRect.top);
Robert Carr6a160312021-05-17 12:08:20 -0700381 return assignTransform(&mDrawingState.transform, t);
Vishnu Nair6bdec7d2021-05-10 15:01:13 -0700382 }
383
Vishnu Nairdbbe3852022-01-12 20:22:11 -0800384 uint32_t bufferWidth = mDrawingState.buffer->getWidth();
385 uint32_t bufferHeight = mDrawingState.buffer->getHeight();
Vishnu Nair6bdec7d2021-05-10 15:01:13 -0700386 // Undo any transformations on the buffer.
Robert Carr6a160312021-05-17 12:08:20 -0700387 if (mDrawingState.bufferTransform & ui::Transform::ROT_90) {
Vishnu Nair6bdec7d2021-05-10 15:01:13 -0700388 std::swap(bufferWidth, bufferHeight);
389 }
390 uint32_t invTransform = DisplayDevice::getPrimaryDisplayRotationFlags();
Robert Carr6a160312021-05-17 12:08:20 -0700391 if (mDrawingState.transformToDisplayInverse) {
Vishnu Nair6bdec7d2021-05-10 15:01:13 -0700392 if (invTransform & ui::Transform::ROT_90) {
393 std::swap(bufferWidth, bufferHeight);
394 }
395 }
396
397 float sx = destW / static_cast<float>(bufferWidth);
398 float sy = destH / static_cast<float>(bufferHeight);
399 ui::Transform t;
400 t.set(sx, 0, 0, sy);
401 t.set(destRect.left, destRect.top);
Robert Carr6a160312021-05-17 12:08:20 -0700402 return assignTransform(&mDrawingState.transform, t);
Vishnu Nair6bdec7d2021-05-10 15:01:13 -0700403}
404
Robert Carrde6d7b42022-01-07 18:23:06 -0800405bool BufferStateLayer::setMatrix(const layer_state_t::matrix22_t& matrix) {
Vishnu Nair6bdec7d2021-05-10 15:01:13 -0700406 if (mRequestedTransform.dsdx() == matrix.dsdx && mRequestedTransform.dtdy() == matrix.dtdy &&
407 mRequestedTransform.dtdx() == matrix.dtdx && mRequestedTransform.dsdy() == matrix.dsdy) {
Marissa Wall861616d2018-10-22 12:52:23 -0700408 return false;
409 }
410
Chavi Weingartena5aedbd2021-04-09 13:37:33 +0000411 ui::Transform t;
412 t.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
413
Vishnu Nair6bdec7d2021-05-10 15:01:13 -0700414 mRequestedTransform.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
chaviw9a93ea62021-03-11 16:44:42 -0600415
Robert Carr6a160312021-05-17 12:08:20 -0700416 mDrawingState.sequence++;
417 mDrawingState.modified = true;
chaviw9a93ea62021-03-11 16:44:42 -0600418 setTransactionFlags(eTransactionNeeded);
Chavi Weingartena5aedbd2021-04-09 13:37:33 +0000419
420 return true;
421}
422
423bool BufferStateLayer::setPosition(float x, float y) {
Vishnu Nair6bdec7d2021-05-10 15:01:13 -0700424 if (mRequestedTransform.tx() == x && mRequestedTransform.ty() == y) {
Chavi Weingartena5aedbd2021-04-09 13:37:33 +0000425 return false;
426 }
427
Vishnu Nair6bdec7d2021-05-10 15:01:13 -0700428 mRequestedTransform.set(x, y);
Chavi Weingartena5aedbd2021-04-09 13:37:33 +0000429
Robert Carr6a160312021-05-17 12:08:20 -0700430 mDrawingState.sequence++;
431 mDrawingState.modified = true;
Chavi Weingartena5aedbd2021-04-09 13:37:33 +0000432 setTransactionFlags(eTransactionNeeded);
433
Marissa Wall861616d2018-10-22 12:52:23 -0700434 return true;
435}
436
Vishnu Nairdbbe3852022-01-12 20:22:11 -0800437bool BufferStateLayer::setBuffer(std::shared_ptr<renderengine::ExternalTexture>& buffer,
438 const BufferData& bufferData, nsecs_t postTime,
Alec Mouria90a5702021-04-16 16:36:21 +0000439 nsecs_t desiredPresentTime, bool isAutoTimestamp,
chaviwba4320c2021-09-15 15:20:53 -0500440 std::optional<nsecs_t> dequeueTime,
441 const FrameTimelineInfo& info) {
Alex Chaucf6b4b42021-12-07 10:48:52 +0000442 ATRACE_CALL();
Robert Carr0c1966e2020-10-19 12:12:08 -0700443
chaviwba4320c2021-09-15 15:20:53 -0500444 if (!buffer) {
445 return false;
446 }
447
448 const bool frameNumberChanged =
449 bufferData.flags.test(BufferData::BufferDataChange::frameNumberChanged);
450 const uint64_t frameNumber =
451 frameNumberChanged ? bufferData.frameNumber : mDrawingState.frameNumber + 1;
452
Robert Carr6a160312021-05-17 12:08:20 -0700453 if (mDrawingState.buffer) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700454 mReleasePreviousBuffer = true;
Vishnu Nairdbbe3852022-01-12 20:22:11 -0800455 if (!mBufferInfo.mBuffer ||
456 (!mDrawingState.buffer->hasSameBuffer(*mBufferInfo.mBuffer) ||
457 mDrawingState.frameNumber != mBufferInfo.mFrameNumber)) {
Robert Carr6a160312021-05-17 12:08:20 -0700458 // If mDrawingState has a buffer, and we are about to update again
Robert Carr7121caf2020-12-15 13:07:32 -0800459 // before swapping to drawing state, then the first buffer will be
Vishnu Nair1506b182021-02-22 14:35:15 -0800460 // dropped and we should decrement the pending buffer count and
461 // call any release buffer callbacks if set.
Robert Carr6a160312021-05-17 12:08:20 -0700462 callReleaseBufferCallback(mDrawingState.releaseBufferListener,
Vishnu Nair4ba0c2e2021-06-24 11:27:17 -0700463 mDrawingState.buffer->getBuffer(), mDrawingState.frameNumber,
chaviw69058fb2021-09-27 09:37:30 -0500464 mDrawingState.acquireFence,
Ady Abraham899dcdb2021-06-15 16:56:21 -0700465 mFlinger->getMaxAcquiredBufferCountForCurrentRefreshRate(
466 mOwnerUid));
Robert Carr7121caf2020-12-15 13:07:32 -0800467 decrementPendingBufferCount();
Robert Carr6a160312021-05-17 12:08:20 -0700468 if (mDrawingState.bufferSurfaceFrameTX != nullptr &&
469 mDrawingState.bufferSurfaceFrameTX->getPresentState() != PresentState::Presented) {
470 addSurfaceFrameDroppedForBuffer(mDrawingState.bufferSurfaceFrameTX);
471 mDrawingState.bufferSurfaceFrameTX.reset();
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +0000472 }
Robert Carrb6450492022-06-23 11:17:16 -0700473 } else if (EARLY_RELEASE_ENABLED && mLastClientCompositionFence != nullptr) {
Robert Carrccab4242021-09-28 16:53:03 -0700474 callReleaseBufferCallback(mDrawingState.releaseBufferListener,
475 mDrawingState.buffer->getBuffer(), mDrawingState.frameNumber,
chaviw69058fb2021-09-27 09:37:30 -0500476 mLastClientCompositionFence,
Robert Carrccab4242021-09-28 16:53:03 -0700477 mFlinger->getMaxAcquiredBufferCountForCurrentRefreshRate(
chaviw69058fb2021-09-27 09:37:30 -0500478 mOwnerUid));
Robert Carrccab4242021-09-28 16:53:03 -0700479 mLastClientCompositionFence = nullptr;
Robert Carr7121caf2020-12-15 13:07:32 -0800480 }
Marissa Wallfda30bb2018-10-12 11:34:28 -0700481 }
Robert Carr6a160312021-05-17 12:08:20 -0700482
483 mDrawingState.frameNumber = frameNumber;
chaviwba4320c2021-09-15 15:20:53 -0500484 mDrawingState.releaseBufferListener = bufferData.releaseBufferListener;
Vishnu Nairdbbe3852022-01-12 20:22:11 -0800485 mDrawingState.buffer = std::move(buffer);
chaviwba4320c2021-09-15 15:20:53 -0500486 mDrawingState.clientCacheId = bufferData.cachedBuffer;
487
488 mDrawingState.acquireFence = bufferData.flags.test(BufferData::BufferDataChange::fenceChanged)
489 ? bufferData.acquireFence
490 : Fence::NO_FENCE;
491 mDrawingState.acquireFenceTime = std::make_unique<FenceTime>(mDrawingState.acquireFence);
Ady Abraham461296a2022-01-21 11:11:31 -0800492 if (mDrawingState.acquireFenceTime->getSignalTime() == Fence::SIGNAL_TIME_PENDING) {
493 // We latched this buffer unsiganled, so we need to pass the acquire fence
494 // on the callback instead of just the acquire time, since it's unknown at
495 // this point.
496 mCallbackHandleAcquireTimeOrFence = mDrawingState.acquireFence;
497 } else {
498 mCallbackHandleAcquireTimeOrFence = mDrawingState.acquireFenceTime->getSignalTime();
499 }
chaviwba4320c2021-09-15 15:20:53 -0500500
Robert Carr6a160312021-05-17 12:08:20 -0700501 mDrawingState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700502 setTransactionFlags(eTransactionNeeded);
Ady Abraham09bd3922019-04-08 10:44:56 -0700503
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800504 const int32_t layerId = getSequence();
Robert Carr6a160312021-05-17 12:08:20 -0700505 mFlinger->mTimeStats->setPostTime(layerId, mDrawingState.frameNumber, getName().c_str(),
Adithya Srinivasan58069dc2021-06-04 20:37:02 +0000506 mOwnerUid, postTime, getGameMode());
Robert Carr6a160312021-05-17 12:08:20 -0700507 mDrawingState.desiredPresentTime = desiredPresentTime;
508 mDrawingState.isAutoTimestamp = isAutoTimestamp;
Ady Abraham09bd3922019-04-08 10:44:56 -0700509
Ady Abrahamb7f15562021-03-15 18:34:08 -0700510 const nsecs_t presentTime = [&] {
511 if (!isAutoTimestamp) return desiredPresentTime;
512
513 const auto prediction =
514 mFlinger->mFrameTimeline->getTokenManager()->getPredictionsForToken(info.vsyncId);
515 if (prediction.has_value()) return prediction->presentTime;
516
517 return static_cast<nsecs_t>(0);
518 }();
Dominik Laskowski068173d2021-08-11 17:22:59 -0700519
520 using LayerUpdateType = scheduler::LayerHistory::LayerUpdateType;
521 mFlinger->mScheduler->recordLayerHistory(this, presentTime, LayerUpdateType::Buffer);
Ady Abraham09bd3922019-04-08 10:44:56 -0700522
Adithya Srinivasan891004e2021-02-12 20:20:47 +0000523 setFrameTimelineVsyncForBufferTransaction(info, postTime);
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +0000524
Vishnu Nair26c49762022-01-18 22:58:52 +0000525 if (dequeueTime && *dequeueTime != 0) {
526 const uint64_t bufferId = mDrawingState.buffer->getId();
Adithya Srinivasanb238cd52021-02-04 17:54:05 +0000527 mFlinger->mFrameTracer->traceNewLayer(layerId, getName().c_str());
528 mFlinger->mFrameTracer->traceTimestamp(layerId, bufferId, frameNumber, *dequeueTime,
529 FrameTracer::FrameEvent::DEQUEUE);
530 mFlinger->mFrameTracer->traceTimestamp(layerId, bufferId, frameNumber, postTime,
531 FrameTracer::FrameEvent::QUEUE);
532 }
Chavi Weingartena5aedbd2021-04-09 13:37:33 +0000533
Vishnu Nairdbbe3852022-01-12 20:22:11 -0800534 mDrawingState.width = mDrawingState.buffer->getWidth();
535 mDrawingState.height = mDrawingState.buffer->getHeight();
chaviwba4320c2021-09-15 15:20:53 -0500536 mDrawingState.releaseBufferEndpoint = bufferData.releaseBufferEndpoint;
Marissa Wall61c58622018-07-18 10:12:20 -0700537 return true;
538}
539
540bool BufferStateLayer::setDataspace(ui::Dataspace dataspace) {
Robert Carr6a160312021-05-17 12:08:20 -0700541 if (mDrawingState.dataspace == dataspace) return false;
542 mDrawingState.dataspace = dataspace;
543 mDrawingState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700544 setTransactionFlags(eTransactionNeeded);
545 return true;
546}
547
548bool BufferStateLayer::setHdrMetadata(const HdrMetadata& hdrMetadata) {
Robert Carr6a160312021-05-17 12:08:20 -0700549 if (mDrawingState.hdrMetadata == hdrMetadata) return false;
550 mDrawingState.hdrMetadata = hdrMetadata;
551 mDrawingState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700552 setTransactionFlags(eTransactionNeeded);
553 return true;
554}
555
556bool BufferStateLayer::setSurfaceDamageRegion(const Region& surfaceDamage) {
Robert Carr6a160312021-05-17 12:08:20 -0700557 mDrawingState.surfaceDamageRegion = surfaceDamage;
558 mDrawingState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700559 setTransactionFlags(eTransactionNeeded);
560 return true;
561}
562
563bool BufferStateLayer::setApi(int32_t api) {
Robert Carr6a160312021-05-17 12:08:20 -0700564 if (mDrawingState.api == api) return false;
565 mDrawingState.api = api;
566 mDrawingState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700567 setTransactionFlags(eTransactionNeeded);
568 return true;
569}
570
571bool BufferStateLayer::setSidebandStream(const sp<NativeHandle>& sidebandStream) {
Robert Carr6a160312021-05-17 12:08:20 -0700572 if (mDrawingState.sidebandStream == sidebandStream) return false;
Robert Carr3e2a2992021-06-11 13:42:55 -0700573
574 if (mDrawingState.sidebandStream != nullptr && sidebandStream == nullptr) {
575 mFlinger->mTunnelModeEnabledReporter->decrementTunnelModeCount();
576 } else if (sidebandStream != nullptr) {
577 mFlinger->mTunnelModeEnabledReporter->incrementTunnelModeCount();
578 }
579
Robert Carr6a160312021-05-17 12:08:20 -0700580 mDrawingState.sidebandStream = sidebandStream;
581 mDrawingState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700582 setTransactionFlags(eTransactionNeeded);
Marissa Wall61c58622018-07-18 10:12:20 -0700583 if (!mSidebandStreamChanged.exchange(true)) {
584 // mSidebandStreamChanged was false
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -0700585 mFlinger->onLayerUpdate();
Marissa Wall61c58622018-07-18 10:12:20 -0700586 }
587 return true;
588}
589
Marissa Walle2ffb422018-10-12 11:33:52 -0700590bool BufferStateLayer::setTransactionCompletedListeners(
Jiakai Zhanga5505cb2021-11-09 11:46:30 +0000591 const std::vector<sp<CallbackHandle>>& handles) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700592 // If there is no handle, we will not send a callback so reset mReleasePreviousBuffer and return
Jiakai Zhanga5505cb2021-11-09 11:46:30 +0000593 if (handles.empty()) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700594 mReleasePreviousBuffer = false;
Marissa Walle2ffb422018-10-12 11:33:52 -0700595 return false;
596 }
597
598 const bool willPresent = willPresentCurrentTransaction();
599
600 for (const auto& handle : handles) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700601 // If this transaction set a buffer on this layer, release its previous buffer
602 handle->releasePreviousBuffer = mReleasePreviousBuffer;
603
Marissa Walle2ffb422018-10-12 11:33:52 -0700604 // If this layer will be presented in this frame
605 if (willPresent) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700606 // If this transaction set an acquire fence on this layer, set its acquire time
Ady Abraham461296a2022-01-21 11:11:31 -0800607 handle->acquireTimeOrFence = mCallbackHandleAcquireTimeOrFence;
Robert Carr6a160312021-05-17 12:08:20 -0700608 handle->frameNumber = mDrawingState.frameNumber;
Marissa Wallfda30bb2018-10-12 11:34:28 -0700609
Marissa Walle2ffb422018-10-12 11:33:52 -0700610 // Store so latched time and release fence can be set
Robert Carr6a160312021-05-17 12:08:20 -0700611 mDrawingState.callbackHandles.push_back(handle);
Marissa Walle2ffb422018-10-12 11:33:52 -0700612
Jiakai Zhanga5505cb2021-11-09 11:46:30 +0000613 } else { // If this layer will NOT need to be relatched and presented this frame
Marissa Walle2ffb422018-10-12 11:33:52 -0700614 // Notify the transaction completed thread this handle is done
Jiakai Zhanga5505cb2021-11-09 11:46:30 +0000615 mFlinger->getTransactionCallbackInvoker().registerUnpresentedCallbackHandle(handle);
Marissa Walle2ffb422018-10-12 11:33:52 -0700616 }
617 }
618
Marissa Wallfda30bb2018-10-12 11:34:28 -0700619 mReleasePreviousBuffer = false;
Ady Abraham461296a2022-01-21 11:11:31 -0800620 mCallbackHandleAcquireTimeOrFence = -1;
Marissa Wallfda30bb2018-10-12 11:34:28 -0700621
Marissa Walle2ffb422018-10-12 11:33:52 -0700622 return willPresent;
623}
624
Marissa Wall61c58622018-07-18 10:12:20 -0700625bool BufferStateLayer::setTransparentRegionHint(const Region& transparent) {
Vishnu Nair27e3ed52021-07-08 18:24:25 -0700626 mDrawingState.sequence++;
Robert Carr6a160312021-05-17 12:08:20 -0700627 mDrawingState.transparentRegionHint = transparent;
628 mDrawingState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700629 setTransactionFlags(eTransactionNeeded);
630 return true;
631}
632
rnleeed20fa42021-08-10 18:00:03 -0700633Rect BufferStateLayer::getBufferSize(const State& /*s*/) const {
Marissa Wall861616d2018-10-22 12:52:23 -0700634 // for buffer state layers we use the display frame size as the buffer size.
Marissa Wall61c58622018-07-18 10:12:20 -0700635
chaviw7e72caf2020-12-02 16:50:43 -0800636 if (mBufferInfo.mBuffer == nullptr) {
637 return Rect::INVALID_RECT;
638 }
639
Vishnu Nairdbbe3852022-01-12 20:22:11 -0800640 uint32_t bufWidth = mBufferInfo.mBuffer->getWidth();
641 uint32_t bufHeight = mBufferInfo.mBuffer->getHeight();
Vishnu Nair6bdec7d2021-05-10 15:01:13 -0700642
643 // Undo any transformations on the buffer and return the result.
644 if (mBufferInfo.mTransform & ui::Transform::ROT_90) {
645 std::swap(bufWidth, bufHeight);
646 }
647
648 if (getTransformToDisplayInverse()) {
649 uint32_t invTransform = DisplayDevice::getPrimaryDisplayRotationFlags();
650 if (invTransform & ui::Transform::ROT_90) {
651 std::swap(bufWidth, bufHeight);
Marissa Wall861616d2018-10-22 12:52:23 -0700652 }
653 }
654
rnleeed20fa42021-08-10 18:00:03 -0700655 return Rect(0, 0, static_cast<int32_t>(bufWidth), static_cast<int32_t>(bufHeight));
Marissa Wall61c58622018-07-18 10:12:20 -0700656}
Vishnu Nair4351ad52019-02-11 14:13:02 -0800657
658FloatRect BufferStateLayer::computeSourceBounds(const FloatRect& parentBounds) const {
Vishnu Nair6bdec7d2021-05-10 15:01:13 -0700659 if (mBufferInfo.mBuffer == nullptr) {
660 return parentBounds;
Vishnu Nair4351ad52019-02-11 14:13:02 -0800661 }
662
Vishnu Nair6bdec7d2021-05-10 15:01:13 -0700663 return getBufferSize(getDrawingState()).toFloatRect();
Vishnu Nair4351ad52019-02-11 14:13:02 -0800664}
665
Marissa Wall61c58622018-07-18 10:12:20 -0700666// -----------------------------------------------------------------------
Marissa Wall61c58622018-07-18 10:12:20 -0700667bool BufferStateLayer::fenceHasSignaled() const {
ramindani4d48f902021-09-20 21:07:45 +0000668 if (SurfaceFlinger::enableLatchUnsignaledConfig != LatchUnsignaledConfig::Disabled) {
Huihong Luo86c80e32021-06-16 15:41:07 -0700669 return true;
670 }
671
Alec Mouri91f6df32020-01-30 08:48:58 -0800672 const bool fenceSignaled =
673 getDrawingState().acquireFence->getStatus() == Fence::Status::Signaled;
674 if (!fenceSignaled) {
675 mFlinger->mTimeStats->incrementLatchSkipped(getSequence(),
676 TimeStats::LatchSkipReason::LateAcquire);
677 }
678
679 return fenceSignaled;
Marissa Wall61c58622018-07-18 10:12:20 -0700680}
681
Valerie Hau871d6352020-01-29 08:44:02 -0800682bool BufferStateLayer::onPreComposition(nsecs_t refreshStartTime) {
683 for (const auto& handle : mDrawingState.callbackHandles) {
684 handle->refreshStartTime = refreshStartTime;
685 }
Vishnu Naira72f6c02022-07-01 05:33:08 +0000686 return hasReadyFrame();
Valerie Hau871d6352020-01-29 08:44:02 -0800687}
688
Vishnu Naircf26a0a2020-11-13 12:56:20 -0800689void BufferStateLayer::setAutoRefresh(bool autoRefresh) {
Vishnu Nair86653e92021-11-03 17:19:36 -0700690 mDrawingState.autoRefresh = autoRefresh;
Marissa Wall61c58622018-07-18 10:12:20 -0700691}
692
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800693bool BufferStateLayer::latchSidebandStream(bool& recomputeVisibleRegions) {
baocheng suna663c2b2021-05-13 18:51:28 +0800694 // 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 +0800695 editCompositionState()->sidebandStreamHasFrame = hasFrameUpdate() && mSidebandStream.get();
baocheng suna663c2b2021-05-13 18:51:28 +0800696
baocheng sun9691b9c2021-08-03 19:27:06 +0800697 if (mSidebandStreamChanged.exchange(false)) {
Marissa Wall61c58622018-07-18 10:12:20 -0700698 const State& s(getDrawingState());
699 // mSidebandStreamChanged was true
Lloyd Pique0b785d82018-12-04 17:25:27 -0800700 mSidebandStream = s.sidebandStream;
Lloyd Piquede196652020-01-22 17:29:58 -0800701 editCompositionState()->sidebandStream = mSidebandStream;
Lloyd Pique0b785d82018-12-04 17:25:27 -0800702 if (mSidebandStream != nullptr) {
Marissa Wall61c58622018-07-18 10:12:20 -0700703 setTransactionFlags(eTransactionNeeded);
704 mFlinger->setTransactionFlags(eTraversalNeeded);
705 }
706 recomputeVisibleRegions = true;
707
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800708 return true;
Marissa Wall61c58622018-07-18 10:12:20 -0700709 }
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800710 return false;
Marissa Wall61c58622018-07-18 10:12:20 -0700711}
712
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800713bool BufferStateLayer::hasFrameUpdate() const {
Robert Carr6a160312021-05-17 12:08:20 -0700714 const State& c(getDrawingState());
Robert Carr315f3c72021-06-24 21:58:09 -0700715 return (mDrawingStateModified || mDrawingState.modified) && (c.buffer != nullptr || c.bgColorLayer != nullptr);
Marissa Wall61c58622018-07-18 10:12:20 -0700716}
717
Vishnu Nair397a0e32022-07-26 00:01:48 +0000718void BufferStateLayer::updateTexImage(nsecs_t latchTime) {
Marissa Wall61c58622018-07-18 10:12:20 -0700719 const State& s(getDrawingState());
720
721 if (!s.buffer) {
Valerie Hauaa194562019-02-05 16:21:38 -0800722 if (s.bgColorLayer) {
723 for (auto& handle : mDrawingState.callbackHandles) {
724 handle->latchTime = latchTime;
725 }
726 }
Vishnu Nair397a0e32022-07-26 00:01:48 +0000727 return;
Marissa Wall61c58622018-07-18 10:12:20 -0700728 }
729
Marissa Wall5a68a772018-12-22 17:43:42 -0800730 for (auto& handle : mDrawingState.callbackHandles) {
Vishnu Nair935590e2021-02-10 13:05:52 -0800731 if (handle->frameNumber == mDrawingState.frameNumber) {
732 handle->latchTime = latchTime;
733 }
Marissa Wall5a68a772018-12-22 17:43:42 -0800734 }
Marissa Walle2ffb422018-10-12 11:33:52 -0700735
Vishnu Nairea0de002020-11-17 17:42:37 -0800736 const int32_t layerId = getSequence();
Vishnu Nairdbbe3852022-01-12 20:22:11 -0800737 const uint64_t bufferId = mDrawingState.buffer->getId();
Adithya Srinivasanb238cd52021-02-04 17:54:05 +0000738 const uint64_t frameNumber = mDrawingState.frameNumber;
739 const auto acquireFence = std::make_shared<FenceTime>(mDrawingState.acquireFence);
740 mFlinger->mTimeStats->setAcquireFence(layerId, frameNumber, acquireFence);
741 mFlinger->mTimeStats->setLatchTime(layerId, frameNumber, latchTime);
742
743 mFlinger->mFrameTracer->traceFence(layerId, bufferId, frameNumber, acquireFence,
744 FrameTracer::FrameEvent::ACQUIRE_FENCE);
745 mFlinger->mFrameTracer->traceTimestamp(layerId, bufferId, frameNumber, latchTime,
746 FrameTracer::FrameEvent::LATCH);
Marissa Wall61c58622018-07-18 10:12:20 -0700747
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +0000748 auto& bufferSurfaceFrame = mDrawingState.bufferSurfaceFrameTX;
749 if (bufferSurfaceFrame != nullptr &&
750 bufferSurfaceFrame->getPresentState() != PresentState::Presented) {
751 // Update only if the bufferSurfaceFrame wasn't already presented. A Presented
752 // bufferSurfaceFrame could be seen here if a pending state was applied successfully and we
753 // are processing the next state.
754 addSurfaceFramePresentedForBuffer(bufferSurfaceFrame,
Ady Abraham6c1b7ac2021-03-31 16:56:03 -0700755 mDrawingState.acquireFenceTime->getSignalTime(),
756 latchTime);
Robert Carr6a160312021-05-17 12:08:20 -0700757 mDrawingState.bufferSurfaceFrameTX.reset();
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +0000758 }
759
Vishnu Nairfc46c1e2021-04-21 08:31:32 -0700760 std::deque<sp<CallbackHandle>> remainingHandles;
761 mFlinger->getTransactionCallbackInvoker()
Robert Carr3d1047b2021-09-20 18:22:32 -0700762 .addOnCommitCallbackHandles(mDrawingState.callbackHandles, remainingHandles);
Vishnu Nairfc46c1e2021-04-21 08:31:32 -0700763 mDrawingState.callbackHandles = remainingHandles;
764
Robert Carr6a160312021-05-17 12:08:20 -0700765 mDrawingStateModified = false;
Marissa Wall61c58622018-07-18 10:12:20 -0700766}
767
Vishnu Nair397a0e32022-07-26 00:01:48 +0000768void BufferStateLayer::gatherBufferInfo() {
769 if (!mBufferInfo.mBuffer || !mDrawingState.buffer->hasSameBuffer(*mBufferInfo.mBuffer)) {
chaviwdf3c5e82021-01-07 13:00:37 -0800770 decrementPendingBufferCount();
771 }
Marissa Wall61c58622018-07-18 10:12:20 -0700772
Vishnu Nair4ba0c2e2021-06-24 11:27:17 -0700773 mPreviousReleaseCallbackId = {getCurrentBufferId(), mBufferInfo.mFrameNumber};
Vishnu Nair397a0e32022-07-26 00:01:48 +0000774 mBufferInfo.mBuffer = mDrawingState.buffer;
775 mBufferInfo.mFence = mDrawingState.acquireFence;
776 mBufferInfo.mFrameNumber = mDrawingState.frameNumber;
Vishnu Naira72f6c02022-07-01 05:33:08 +0000777 mBufferInfo.mPixelFormat =
778 !mBufferInfo.mBuffer ? PIXEL_FORMAT_NONE : mBufferInfo.mBuffer->getPixelFormat();
779 mBufferInfo.mFrameLatencyNeeded = true;
Vishnu Nair397a0e32022-07-26 00:01:48 +0000780 mBufferInfo.mDesiredPresentTime = mDrawingState.desiredPresentTime;
781 mBufferInfo.mFenceTime = std::make_shared<FenceTime>(mDrawingState.acquireFence);
782 mBufferInfo.mFence = mDrawingState.acquireFence;
783 mBufferInfo.mTransform = mDrawingState.bufferTransform;
Robert Carr167bdde2021-07-28 11:26:51 -0700784 auto lastDataspace = mBufferInfo.mDataspace;
Vishnu Nair397a0e32022-07-26 00:01:48 +0000785 mBufferInfo.mDataspace = translateDataspace(mDrawingState.dataspace);
Robert Carr167bdde2021-07-28 11:26:51 -0700786 if (lastDataspace != mBufferInfo.mDataspace) {
787 mFlinger->mSomeDataspaceChanged = true;
788 }
Vishnu Nair397a0e32022-07-26 00:01:48 +0000789 mBufferInfo.mCrop = computeBufferCrop(mDrawingState);
chaviw4244e032019-09-04 11:27:49 -0700790 mBufferInfo.mScaleMode = NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW;
Vishnu Nair397a0e32022-07-26 00:01:48 +0000791 mBufferInfo.mSurfaceDamage = mDrawingState.surfaceDamageRegion;
792 mBufferInfo.mHdrMetadata = mDrawingState.hdrMetadata;
793 mBufferInfo.mApi = mDrawingState.api;
794 mBufferInfo.mTransformToDisplayInverse = mDrawingState.transformToDisplayInverse;
795 mBufferInfo.mBufferSlot = mHwcSlotGenerator->getHwcCacheSlot(mDrawingState.clientCacheId);
Robert Carr916b0362020-10-06 13:53:03 -0700796}
797
Vishnu Nair5cc9ac02021-04-19 13:23:38 -0700798Rect BufferStateLayer::computeBufferCrop(const State& s) {
chaviwf3f40fe2021-04-27 15:54:02 -0500799 if (s.buffer && !s.bufferCrop.isEmpty()) {
800 Rect bufferCrop;
Vishnu Nairdbbe3852022-01-12 20:22:11 -0800801 s.buffer->getBounds().intersect(s.bufferCrop, &bufferCrop);
chaviwf3f40fe2021-04-27 15:54:02 -0500802 return bufferCrop;
803 } else if (s.buffer) {
Vishnu Nairdbbe3852022-01-12 20:22:11 -0800804 return s.buffer->getBounds();
chaviwf3f40fe2021-04-27 15:54:02 -0500805 } else {
806 return s.bufferCrop;
chaviw4244e032019-09-04 11:27:49 -0700807 }
chaviw4244e032019-09-04 11:27:49 -0700808}
809
chaviwb4c6e582019-08-16 14:35:07 -0700810sp<Layer> BufferStateLayer::createClone() {
Vishnu Nair7fb9e5a2021-11-08 12:44:05 -0800811 LayerCreationArgs args(mFlinger.get(), nullptr, mName + " (Mirror)", 0, LayerMetadata());
chaviwb4c6e582019-08-16 14:35:07 -0700812 args.textureName = mTextureName;
Lloyd Pique1c3a5eb2019-10-03 13:07:08 -0700813 sp<BufferStateLayer> layer = mFlinger->getFactory().createBufferStateLayer(args);
chaviwb4c6e582019-08-16 14:35:07 -0700814 layer->mHwcSlotGenerator = mHwcSlotGenerator;
Ady Abrahamd11bade2022-08-01 16:18:03 -0700815 layer->setInitialValuesForClone(sp<Layer>::fromExisting(this));
chaviwb4c6e582019-08-16 14:35:07 -0700816 return layer;
817}
Valerie Hau92bf5482020-02-10 09:49:08 -0800818
Vishnu Naire7f79c52020-10-29 14:45:03 -0700819bool BufferStateLayer::bufferNeedsFiltering() const {
820 const State& s(getDrawingState());
821 if (!s.buffer) {
822 return false;
823 }
824
Vishnu Nairdbbe3852022-01-12 20:22:11 -0800825 int32_t bufferWidth = static_cast<int32_t>(s.buffer->getWidth());
826 int32_t bufferHeight = static_cast<int32_t>(s.buffer->getHeight());
Vishnu Naire7f79c52020-10-29 14:45:03 -0700827
828 // Undo any transformations on the buffer and return the result.
chaviw766c9c52021-02-10 17:36:47 -0800829 if (s.bufferTransform & ui::Transform::ROT_90) {
Vishnu Naire7f79c52020-10-29 14:45:03 -0700830 std::swap(bufferWidth, bufferHeight);
831 }
832
833 if (s.transformToDisplayInverse) {
834 uint32_t invTransform = DisplayDevice::getPrimaryDisplayRotationFlags();
835 if (invTransform & ui::Transform::ROT_90) {
836 std::swap(bufferWidth, bufferHeight);
837 }
838 }
839
840 const Rect layerSize{getBounds()};
Yiwei Zhang22ca2c42022-07-07 07:00:16 +0000841 int32_t layerWidth = layerSize.getWidth();
842 int32_t layerHeight = layerSize.getHeight();
843
844 // Align the layer orientation with the buffer before comparism
845 if (mTransformHint & ui::Transform::ROT_90) {
846 std::swap(layerWidth, layerHeight);
847 }
848
849 return layerWidth != bufferWidth || layerHeight != bufferHeight;
Vishnu Naire7f79c52020-10-29 14:45:03 -0700850}
Robert Carr7121caf2020-12-15 13:07:32 -0800851
Robert Carr7121caf2020-12-15 13:07:32 -0800852void BufferStateLayer::decrementPendingBufferCount() {
Vishnu Nair8eda69e2021-02-26 10:42:10 -0800853 int32_t pendingBuffers = --mPendingBufferTransactions;
854 tracePendingBufferCount(pendingBuffers);
Robert Carr7121caf2020-12-15 13:07:32 -0800855}
856
Vishnu Nair8eda69e2021-02-26 10:42:10 -0800857void BufferStateLayer::tracePendingBufferCount(int32_t pendingBuffers) {
858 ATRACE_INT(mBlastTransactionName.c_str(), pendingBuffers);
Robert Carr7121caf2020-12-15 13:07:32 -0800859}
860
Robert Carr7121caf2020-12-15 13:07:32 -0800861
chaviw39d01472021-04-08 14:26:24 -0500862/*
863 * We don't want to send the layer's transform to input, but rather the
864 * parent's transform. This is because BufferStateLayer's transform is
865 * information about how the buffer is placed on screen. The parent's
866 * transform makes more sense to send since it's information about how the
867 * layer is placed on screen. This transform is used by input to determine
868 * how to go from screen space back to window space.
869 */
870ui::Transform BufferStateLayer::getInputTransform() const {
Rob Carrc6d2d2b2021-10-25 16:51:49 +0000871 sp<Layer> parent = mDrawingParent.promote();
chaviw39d01472021-04-08 14:26:24 -0500872 if (parent == nullptr) {
873 return ui::Transform();
874 }
875
876 return parent->getTransform();
877}
878
879/**
880 * Similar to getInputTransform, we need to update the bounds to include the transform.
881 * This is because bounds for BSL doesn't include buffer transform, where the input assumes
882 * that's already included.
883 */
884Rect BufferStateLayer::getInputBounds() const {
885 Rect bufferBounds = getCroppedBufferSize(getDrawingState());
886 if (mDrawingState.transform.getType() == ui::Transform::IDENTITY || !bufferBounds.isValid()) {
887 return bufferBounds;
888 }
889 return mDrawingState.transform.transform(bufferBounds);
890}
891
Ady Abraham9dada822022-02-03 10:26:59 -0800892bool BufferStateLayer::simpleBufferUpdate(const layer_state_t& s) const {
893 const uint64_t requiredFlags = layer_state_t::eBufferChanged;
894
895 const uint64_t deniedFlags = layer_state_t::eProducerDisconnect | layer_state_t::eLayerChanged |
896 layer_state_t::eRelativeLayerChanged | layer_state_t::eTransparentRegionChanged |
897 layer_state_t::eFlagsChanged | layer_state_t::eBlurRegionsChanged |
898 layer_state_t::eLayerStackChanged | layer_state_t::eAutoRefreshChanged |
899 layer_state_t::eReparent;
900
901 const uint64_t allowedFlags = layer_state_t::eHasListenerCallbacksChanged |
902 layer_state_t::eFrameRateSelectionPriority | layer_state_t::eFrameRateChanged |
903 layer_state_t::eSurfaceDamageRegionChanged | layer_state_t::eApiChanged |
904 layer_state_t::eMetadataChanged | layer_state_t::eDropInputModeChanged |
905 layer_state_t::eInputInfoChanged;
906
907 if ((s.what & requiredFlags) != requiredFlags) {
908 ALOGV("%s: false [missing required flags 0x%" PRIx64 "]", __func__,
909 (s.what | requiredFlags) & ~s.what);
910 return false;
911 }
912
913 if (s.what & deniedFlags) {
914 ALOGV("%s: false [has denied flags 0x%" PRIx64 "]", __func__, s.what & deniedFlags);
915 return false;
916 }
917
918 if (s.what & allowedFlags) {
919 ALOGV("%s: [has allowed flags 0x%" PRIx64 "]", __func__, s.what & allowedFlags);
920 }
921
922 if (s.what & layer_state_t::ePositionChanged) {
923 if (mRequestedTransform.tx() != s.x || mRequestedTransform.ty() != s.y) {
924 ALOGV("%s: false [ePositionChanged changed]", __func__);
925 return false;
926 }
927 }
928
929 if (s.what & layer_state_t::eAlphaChanged) {
930 if (mDrawingState.color.a != s.alpha) {
931 ALOGV("%s: false [eAlphaChanged changed]", __func__);
932 return false;
933 }
934 }
935
936 if (s.what & layer_state_t::eColorTransformChanged) {
937 if (mDrawingState.colorTransform != s.colorTransform) {
938 ALOGV("%s: false [eColorTransformChanged changed]", __func__);
939 return false;
940 }
941 }
942
943 if (s.what & layer_state_t::eBackgroundColorChanged) {
944 if (mDrawingState.bgColorLayer || s.bgColorAlpha != 0) {
945 ALOGV("%s: false [eBackgroundColorChanged changed]", __func__);
946 return false;
947 }
948 }
949
950 if (s.what & layer_state_t::eMatrixChanged) {
951 if (mRequestedTransform.dsdx() != s.matrix.dsdx ||
952 mRequestedTransform.dtdy() != s.matrix.dtdy ||
953 mRequestedTransform.dtdx() != s.matrix.dtdx ||
954 mRequestedTransform.dsdy() != s.matrix.dsdy) {
955 ALOGV("%s: false [eMatrixChanged changed]", __func__);
956 return false;
957 }
958 }
959
960 if (s.what & layer_state_t::eCornerRadiusChanged) {
961 if (mDrawingState.cornerRadius != s.cornerRadius) {
962 ALOGV("%s: false [eCornerRadiusChanged changed]", __func__);
963 return false;
964 }
965 }
966
967 if (s.what & layer_state_t::eBackgroundBlurRadiusChanged) {
968 if (mDrawingState.backgroundBlurRadius != static_cast<int>(s.backgroundBlurRadius)) {
969 ALOGV("%s: false [eBackgroundBlurRadiusChanged changed]", __func__);
970 return false;
971 }
972 }
973
974 if (s.what & layer_state_t::eTransformChanged) {
975 if (mDrawingState.bufferTransform != s.transform) {
976 ALOGV("%s: false [eTransformChanged changed]", __func__);
977 return false;
978 }
979 }
980
981 if (s.what & layer_state_t::eTransformToDisplayInverseChanged) {
982 if (mDrawingState.transformToDisplayInverse != s.transformToDisplayInverse) {
983 ALOGV("%s: false [eTransformToDisplayInverseChanged changed]", __func__);
984 return false;
985 }
986 }
987
988 if (s.what & layer_state_t::eCropChanged) {
989 if (mDrawingState.crop != s.crop) {
990 ALOGV("%s: false [eCropChanged changed]", __func__);
991 return false;
992 }
993 }
994
995 if (s.what & layer_state_t::eDataspaceChanged) {
996 if (mDrawingState.dataspace != s.dataspace) {
997 ALOGV("%s: false [eDataspaceChanged changed]", __func__);
998 return false;
999 }
1000 }
1001
1002 if (s.what & layer_state_t::eHdrMetadataChanged) {
1003 if (mDrawingState.hdrMetadata != s.hdrMetadata) {
1004 ALOGV("%s: false [eHdrMetadataChanged changed]", __func__);
1005 return false;
1006 }
1007 }
1008
1009 if (s.what & layer_state_t::eSidebandStreamChanged) {
1010 if (mDrawingState.sidebandStream != s.sidebandStream) {
1011 ALOGV("%s: false [eSidebandStreamChanged changed]", __func__);
1012 return false;
1013 }
1014 }
1015
1016 if (s.what & layer_state_t::eColorSpaceAgnosticChanged) {
1017 if (mDrawingState.colorSpaceAgnostic != s.colorSpaceAgnostic) {
1018 ALOGV("%s: false [eColorSpaceAgnosticChanged changed]", __func__);
1019 return false;
1020 }
1021 }
1022
1023 if (s.what & layer_state_t::eShadowRadiusChanged) {
1024 if (mDrawingState.shadowRadius != s.shadowRadius) {
1025 ALOGV("%s: false [eShadowRadiusChanged changed]", __func__);
1026 return false;
1027 }
1028 }
1029
1030 if (s.what & layer_state_t::eFixedTransformHintChanged) {
1031 if (mDrawingState.fixedTransformHint != s.fixedTransformHint) {
1032 ALOGV("%s: false [eFixedTransformHintChanged changed]", __func__);
1033 return false;
1034 }
1035 }
1036
1037 if (s.what & layer_state_t::eTrustedOverlayChanged) {
1038 if (mDrawingState.isTrustedOverlay != s.isTrustedOverlay) {
1039 ALOGV("%s: false [eTrustedOverlayChanged changed]", __func__);
1040 return false;
1041 }
1042 }
1043
1044 if (s.what & layer_state_t::eStretchChanged) {
1045 StretchEffect temp = s.stretchEffect;
1046 temp.sanitize();
1047 if (mDrawingState.stretchEffect != temp) {
1048 ALOGV("%s: false [eStretchChanged changed]", __func__);
1049 return false;
1050 }
1051 }
1052
1053 if (s.what & layer_state_t::eBufferCropChanged) {
1054 if (mDrawingState.bufferCrop != s.bufferCrop) {
1055 ALOGV("%s: false [eBufferCropChanged changed]", __func__);
1056 return false;
1057 }
1058 }
1059
1060 if (s.what & layer_state_t::eDestinationFrameChanged) {
1061 if (mDrawingState.destinationFrame != s.destinationFrame) {
1062 ALOGV("%s: false [eDestinationFrameChanged changed]", __func__);
1063 return false;
1064 }
1065 }
1066
Sally Qi81d95e62022-03-21 19:41:33 -07001067 if (s.what & layer_state_t::eDimmingEnabledChanged) {
1068 if (mDrawingState.dimmingEnabled != s.dimmingEnabled) {
1069 ALOGV("%s: false [eDimmingEnabledChanged changed]", __func__);
1070 return false;
1071 }
1072 }
1073
Ady Abraham9dada822022-02-03 10:26:59 -08001074 ALOGV("%s: true", __func__);
1075 return true;
1076}
1077
Vishnu Naira72f6c02022-07-01 05:33:08 +00001078void BufferStateLayer::useSurfaceDamage() {
1079 if (mFlinger->mForceFullDamage) {
1080 surfaceDamageRegion = Region::INVALID_REGION;
1081 } else {
1082 surfaceDamageRegion = mBufferInfo.mSurfaceDamage;
1083 }
1084}
1085
1086void BufferStateLayer::useEmptyDamage() {
1087 surfaceDamageRegion.clear();
1088}
1089
1090bool BufferStateLayer::isOpaque(const Layer::State& s) const {
1091 // if we don't have a buffer or sidebandStream yet, we're translucent regardless of the
1092 // layer's opaque flag.
1093 if ((mSidebandStream == nullptr) && (mBufferInfo.mBuffer == nullptr)) {
1094 return false;
1095 }
1096
1097 // if the layer has the opaque flag, then we're always opaque,
1098 // otherwise we use the current buffer's format.
1099 return ((s.flags & layer_state_t::eLayerOpaque) != 0) || getOpacityForFormat(getPixelFormat());
1100}
1101
1102bool BufferStateLayer::canReceiveInput() const {
1103 return !isHiddenByPolicy() && (mBufferInfo.mBuffer == nullptr || getAlpha() > 0.0f);
1104}
1105
1106bool BufferStateLayer::isVisible() const {
1107 return !isHiddenByPolicy() && getAlpha() > 0.0f &&
1108 (mBufferInfo.mBuffer != nullptr || mSidebandStream != nullptr);
1109}
1110
Vishnu Naira72f6c02022-07-01 05:33:08 +00001111std::optional<compositionengine::LayerFE::LayerSettings> BufferStateLayer::prepareClientComposition(
1112 compositionengine::LayerFE::ClientCompositionTargetSettings& targetSettings) {
1113 ATRACE_CALL();
1114
1115 std::optional<compositionengine::LayerFE::LayerSettings> result =
1116 Layer::prepareClientComposition(targetSettings);
1117 if (!result) {
1118 return result;
1119 }
1120
1121 if (CC_UNLIKELY(mBufferInfo.mBuffer == 0) && mSidebandStream != nullptr) {
1122 // For surfaceview of tv sideband, there is no activeBuffer
1123 // in bufferqueue, we need return LayerSettings.
1124 return result;
1125 }
1126 const bool blackOutLayer = (isProtected() && !targetSettings.supportsProtectedContent) ||
1127 ((isSecure() || isProtected()) && !targetSettings.isSecure);
1128 const bool bufferCanBeUsedAsHwTexture =
1129 mBufferInfo.mBuffer->getUsage() & GraphicBuffer::USAGE_HW_TEXTURE;
1130 compositionengine::LayerFE::LayerSettings& layer = *result;
1131 if (blackOutLayer || !bufferCanBeUsedAsHwTexture) {
1132 ALOGE_IF(!bufferCanBeUsedAsHwTexture, "%s is blacked out as buffer is not gpu readable",
1133 mName.c_str());
1134 prepareClearClientComposition(layer, true /* blackout */);
1135 return layer;
1136 }
1137
1138 const State& s(getDrawingState());
1139 layer.source.buffer.buffer = mBufferInfo.mBuffer;
1140 layer.source.buffer.isOpaque = isOpaque(s);
1141 layer.source.buffer.fence = mBufferInfo.mFence;
1142 layer.source.buffer.textureName = mTextureName;
1143 layer.source.buffer.usePremultipliedAlpha = getPremultipledAlpha();
1144 layer.source.buffer.isY410BT2020 = isHdrY410();
1145 bool hasSmpte2086 = mBufferInfo.mHdrMetadata.validTypes & HdrMetadata::SMPTE2086;
1146 bool hasCta861_3 = mBufferInfo.mHdrMetadata.validTypes & HdrMetadata::CTA861_3;
1147 float maxLuminance = 0.f;
1148 if (hasSmpte2086 && hasCta861_3) {
1149 maxLuminance = std::min(mBufferInfo.mHdrMetadata.smpte2086.maxLuminance,
1150 mBufferInfo.mHdrMetadata.cta8613.maxContentLightLevel);
1151 } else if (hasSmpte2086) {
1152 maxLuminance = mBufferInfo.mHdrMetadata.smpte2086.maxLuminance;
1153 } else if (hasCta861_3) {
1154 maxLuminance = mBufferInfo.mHdrMetadata.cta8613.maxContentLightLevel;
1155 } else {
1156 switch (layer.sourceDataspace & HAL_DATASPACE_TRANSFER_MASK) {
1157 case HAL_DATASPACE_TRANSFER_ST2084:
1158 case HAL_DATASPACE_TRANSFER_HLG:
1159 // Behavior-match previous releases for HDR content
1160 maxLuminance = defaultMaxLuminance;
1161 break;
1162 }
1163 }
1164 layer.source.buffer.maxLuminanceNits = maxLuminance;
1165 layer.frameNumber = mCurrentFrameNumber;
1166 layer.bufferId = mBufferInfo.mBuffer ? mBufferInfo.mBuffer->getId() : 0;
1167
1168 const bool useFiltering =
1169 targetSettings.needsFiltering || mNeedsFiltering || bufferNeedsFiltering();
1170
1171 // Query the texture matrix given our current filtering mode.
1172 float textureMatrix[16];
1173 getDrawingTransformMatrix(useFiltering, textureMatrix);
1174
1175 if (getTransformToDisplayInverse()) {
1176 /*
1177 * the code below applies the primary display's inverse transform to
1178 * the texture transform
1179 */
1180 uint32_t transform = DisplayDevice::getPrimaryDisplayRotationFlags();
1181 mat4 tr = inverseOrientation(transform);
1182
1183 /**
1184 * TODO(b/36727915): This is basically a hack.
1185 *
1186 * Ensure that regardless of the parent transformation,
1187 * this buffer is always transformed from native display
1188 * orientation to display orientation. For example, in the case
1189 * of a camera where the buffer remains in native orientation,
1190 * we want the pixels to always be upright.
1191 */
1192 sp<Layer> p = mDrawingParent.promote();
1193 if (p != nullptr) {
1194 const auto parentTransform = p->getTransform();
1195 tr = tr * inverseOrientation(parentTransform.getOrientation());
1196 }
1197
1198 // and finally apply it to the original texture matrix
1199 const mat4 texTransform(mat4(static_cast<const float*>(textureMatrix)) * tr);
1200 memcpy(textureMatrix, texTransform.asArray(), sizeof(textureMatrix));
1201 }
1202
1203 const Rect win{getBounds()};
1204 float bufferWidth = getBufferSize(s).getWidth();
1205 float bufferHeight = getBufferSize(s).getHeight();
1206
1207 // BufferStateLayers can have a "buffer size" of [0, 0, -1, -1] when no display frame has
1208 // been set and there is no parent layer bounds. In that case, the scale is meaningless so
1209 // ignore them.
1210 if (!getBufferSize(s).isValid()) {
1211 bufferWidth = float(win.right) - float(win.left);
1212 bufferHeight = float(win.bottom) - float(win.top);
1213 }
1214
1215 const float scaleHeight = (float(win.bottom) - float(win.top)) / bufferHeight;
1216 const float scaleWidth = (float(win.right) - float(win.left)) / bufferWidth;
1217 const float translateY = float(win.top) / bufferHeight;
1218 const float translateX = float(win.left) / bufferWidth;
1219
1220 // Flip y-coordinates because GLConsumer expects OpenGL convention.
1221 mat4 tr = mat4::translate(vec4(.5f, .5f, 0.f, 1.f)) * mat4::scale(vec4(1.f, -1.f, 1.f, 1.f)) *
1222 mat4::translate(vec4(-.5f, -.5f, 0.f, 1.f)) *
1223 mat4::translate(vec4(translateX, translateY, 0.f, 1.f)) *
1224 mat4::scale(vec4(scaleWidth, scaleHeight, 1.0f, 1.0f));
1225
1226 layer.source.buffer.useTextureFiltering = useFiltering;
1227 layer.source.buffer.textureTransform = mat4(static_cast<const float*>(textureMatrix)) * tr;
1228
1229 return layer;
1230}
1231
1232bool BufferStateLayer::isHdrY410() const {
1233 // pixel format is HDR Y410 masquerading as RGBA_1010102
1234 return (mBufferInfo.mDataspace == ui::Dataspace::BT2020_ITU_PQ &&
1235 mBufferInfo.mApi == NATIVE_WINDOW_API_MEDIA &&
1236 mBufferInfo.mPixelFormat == HAL_PIXEL_FORMAT_RGBA_1010102);
1237}
1238
1239sp<compositionengine::LayerFE> BufferStateLayer::getCompositionEngineLayerFE() const {
1240 return asLayerFE();
1241}
1242
1243compositionengine::LayerFECompositionState* BufferStateLayer::editCompositionState() {
1244 return mCompositionState.get();
1245}
1246
1247const compositionengine::LayerFECompositionState* BufferStateLayer::getCompositionState() const {
1248 return mCompositionState.get();
1249}
1250
1251void BufferStateLayer::preparePerFrameCompositionState() {
1252 Layer::preparePerFrameCompositionState();
1253
1254 // Sideband layers
1255 auto* compositionState = editCompositionState();
1256 if (compositionState->sidebandStream.get() && !compositionState->sidebandStreamHasFrame) {
1257 compositionState->compositionType =
1258 aidl::android::hardware::graphics::composer3::Composition::SIDEBAND;
1259 return;
1260 } else if ((mDrawingState.flags & layer_state_t::eLayerIsDisplayDecoration) != 0) {
1261 compositionState->compositionType =
1262 aidl::android::hardware::graphics::composer3::Composition::DISPLAY_DECORATION;
1263 } else {
1264 // Normal buffer layers
1265 compositionState->hdrMetadata = mBufferInfo.mHdrMetadata;
1266 compositionState->compositionType = mPotentialCursor
1267 ? aidl::android::hardware::graphics::composer3::Composition::CURSOR
1268 : aidl::android::hardware::graphics::composer3::Composition::DEVICE;
1269 }
1270
1271 compositionState->buffer = getBuffer();
1272 compositionState->bufferSlot = (mBufferInfo.mBufferSlot == BufferQueue::INVALID_BUFFER_SLOT)
1273 ? 0
1274 : mBufferInfo.mBufferSlot;
1275 compositionState->acquireFence = mBufferInfo.mFence;
1276 compositionState->frameNumber = mBufferInfo.mFrameNumber;
1277 compositionState->sidebandStreamHasFrame = false;
1278}
1279
Vishnu Naira72f6c02022-07-01 05:33:08 +00001280void BufferStateLayer::onPostComposition(const DisplayDevice* display,
1281 const std::shared_ptr<FenceTime>& glDoneFence,
1282 const std::shared_ptr<FenceTime>& presentFence,
1283 const CompositorTiming& compositorTiming) {
1284 // mFrameLatencyNeeded is true when a new frame was latched for the
1285 // composition.
1286 if (!mBufferInfo.mFrameLatencyNeeded) return;
1287
Vishnu Nair397a0e32022-07-26 00:01:48 +00001288 for (const auto& handle : mDrawingState.callbackHandles) {
1289 handle->gpuCompositionDoneFence = glDoneFence;
1290 handle->compositorTiming = compositorTiming;
1291 }
Vishnu Naira72f6c02022-07-01 05:33:08 +00001292
1293 // Update mFrameTracker.
1294 nsecs_t desiredPresentTime = mBufferInfo.mDesiredPresentTime;
1295 mFrameTracker.setDesiredPresentTime(desiredPresentTime);
1296
1297 const int32_t layerId = getSequence();
1298 mFlinger->mTimeStats->setDesiredTime(layerId, mCurrentFrameNumber, desiredPresentTime);
1299
1300 const auto outputLayer = findOutputLayerForDisplay(display);
1301 if (outputLayer && outputLayer->requiresClientComposition()) {
1302 nsecs_t clientCompositionTimestamp = outputLayer->getState().clientCompositionTimestamp;
1303 mFlinger->mFrameTracer->traceTimestamp(layerId, getCurrentBufferId(), mCurrentFrameNumber,
1304 clientCompositionTimestamp,
1305 FrameTracer::FrameEvent::FALLBACK_COMPOSITION);
1306 // Update the SurfaceFrames in the drawing state
1307 if (mDrawingState.bufferSurfaceFrameTX) {
1308 mDrawingState.bufferSurfaceFrameTX->setGpuComposition();
1309 }
1310 for (auto& [token, surfaceFrame] : mDrawingState.bufferlessSurfaceFramesTX) {
1311 surfaceFrame->setGpuComposition();
1312 }
1313 }
1314
1315 std::shared_ptr<FenceTime> frameReadyFence = mBufferInfo.mFenceTime;
1316 if (frameReadyFence->isValid()) {
1317 mFrameTracker.setFrameReadyFence(std::move(frameReadyFence));
1318 } else {
1319 // There was no fence for this frame, so assume that it was ready
1320 // to be presented at the desired present time.
1321 mFrameTracker.setFrameReadyTime(desiredPresentTime);
1322 }
1323
1324 if (display) {
1325 const Fps refreshRate = display->refreshRateConfigs().getActiveMode()->getFps();
1326 const std::optional<Fps> renderRate =
1327 mFlinger->mScheduler->getFrameRateOverride(getOwnerUid());
1328
1329 const auto vote = frameRateToSetFrameRateVotePayload(mDrawingState.frameRate);
1330 const auto gameMode = getGameMode();
1331
1332 if (presentFence->isValid()) {
1333 mFlinger->mTimeStats->setPresentFence(layerId, mCurrentFrameNumber, presentFence,
1334 refreshRate, renderRate, vote, gameMode);
1335 mFlinger->mFrameTracer->traceFence(layerId, getCurrentBufferId(), mCurrentFrameNumber,
1336 presentFence,
1337 FrameTracer::FrameEvent::PRESENT_FENCE);
1338 mFrameTracker.setActualPresentFence(std::shared_ptr<FenceTime>(presentFence));
1339 } else if (const auto displayId = PhysicalDisplayId::tryCast(display->getId());
1340 displayId && mFlinger->getHwComposer().isConnected(*displayId)) {
1341 // The HWC doesn't support present fences, so use the refresh
1342 // timestamp instead.
1343 const nsecs_t actualPresentTime = display->getRefreshTimestamp();
1344 mFlinger->mTimeStats->setPresentTime(layerId, mCurrentFrameNumber, actualPresentTime,
1345 refreshRate, renderRate, vote, gameMode);
1346 mFlinger->mFrameTracer->traceTimestamp(layerId, getCurrentBufferId(),
1347 mCurrentFrameNumber, actualPresentTime,
1348 FrameTracer::FrameEvent::PRESENT_FENCE);
1349 mFrameTracker.setActualPresentTime(actualPresentTime);
1350 }
1351 }
1352
1353 mFrameTracker.advanceFrame();
1354 mBufferInfo.mFrameLatencyNeeded = false;
1355}
1356
Vishnu Nair397a0e32022-07-26 00:01:48 +00001357bool BufferStateLayer::latchBuffer(bool& recomputeVisibleRegions, nsecs_t latchTime) {
Vishnu Naira72f6c02022-07-01 05:33:08 +00001358 ATRACE_FORMAT_INSTANT("latchBuffer %s - %" PRIu64, getDebugName(),
1359 getDrawingState().frameNumber);
1360
1361 bool refreshRequired = latchSidebandStream(recomputeVisibleRegions);
1362
1363 if (refreshRequired) {
1364 return refreshRequired;
1365 }
1366
1367 // If the head buffer's acquire fence hasn't signaled yet, return and
1368 // try again later
1369 if (!fenceHasSignaled()) {
1370 ATRACE_NAME("!fenceHasSignaled()");
1371 mFlinger->onLayerUpdate();
1372 return false;
1373 }
1374
Vishnu Nair397a0e32022-07-26 00:01:48 +00001375 updateTexImage(latchTime);
1376 if (mDrawingState.buffer == nullptr) {
1377 return false;
1378 }
1379
Vishnu Naira72f6c02022-07-01 05:33:08 +00001380 // Capture the old state of the layer for comparisons later
Vishnu Naira72f6c02022-07-01 05:33:08 +00001381 BufferInfo oldBufferInfo = mBufferInfo;
Vishnu Nair397a0e32022-07-26 00:01:48 +00001382 const bool oldOpacity = isOpaque(mDrawingState);
1383 mPreviousFrameNumber = mCurrentFrameNumber;
1384 mCurrentFrameNumber = mDrawingState.frameNumber;
Vishnu Naira72f6c02022-07-01 05:33:08 +00001385 gatherBufferInfo();
1386
1387 if (oldBufferInfo.mBuffer == nullptr) {
1388 // the first time we receive a buffer, we need to trigger a
1389 // geometry invalidation.
1390 recomputeVisibleRegions = true;
1391 }
1392
1393 if ((mBufferInfo.mCrop != oldBufferInfo.mCrop) ||
1394 (mBufferInfo.mTransform != oldBufferInfo.mTransform) ||
1395 (mBufferInfo.mScaleMode != oldBufferInfo.mScaleMode) ||
1396 (mBufferInfo.mTransformToDisplayInverse != oldBufferInfo.mTransformToDisplayInverse)) {
1397 recomputeVisibleRegions = true;
1398 }
1399
1400 if (oldBufferInfo.mBuffer != nullptr) {
1401 uint32_t bufWidth = mBufferInfo.mBuffer->getWidth();
1402 uint32_t bufHeight = mBufferInfo.mBuffer->getHeight();
1403 if (bufWidth != oldBufferInfo.mBuffer->getWidth() ||
1404 bufHeight != oldBufferInfo.mBuffer->getHeight()) {
1405 recomputeVisibleRegions = true;
1406 }
1407 }
1408
Vishnu Nair397a0e32022-07-26 00:01:48 +00001409 if (oldOpacity != isOpaque(mDrawingState)) {
Vishnu Naira72f6c02022-07-01 05:33:08 +00001410 recomputeVisibleRegions = true;
1411 }
1412
1413 return true;
1414}
1415
1416bool BufferStateLayer::hasReadyFrame() const {
1417 return hasFrameUpdate() || getSidebandStreamChanged() || getAutoRefresh();
1418}
1419
1420bool BufferStateLayer::isProtected() const {
1421 return (mBufferInfo.mBuffer != nullptr) &&
1422 (mBufferInfo.mBuffer->getUsage() & GRALLOC_USAGE_PROTECTED);
1423}
1424
1425// As documented in libhardware header, formats in the range
1426// 0x100 - 0x1FF are specific to the HAL implementation, and
1427// are known to have no alpha channel
1428// TODO: move definition for device-specific range into
1429// hardware.h, instead of using hard-coded values here.
1430#define HARDWARE_IS_DEVICE_FORMAT(f) ((f) >= 0x100 && (f) <= 0x1FF)
1431
1432bool BufferStateLayer::getOpacityForFormat(PixelFormat format) {
1433 if (HARDWARE_IS_DEVICE_FORMAT(format)) {
1434 return true;
1435 }
1436 switch (format) {
1437 case PIXEL_FORMAT_RGBA_8888:
1438 case PIXEL_FORMAT_BGRA_8888:
1439 case PIXEL_FORMAT_RGBA_FP16:
1440 case PIXEL_FORMAT_RGBA_1010102:
1441 case PIXEL_FORMAT_R_8:
1442 return false;
1443 }
1444 // in all other case, we have no blending (also for unknown formats)
1445 return true;
1446}
1447
1448bool BufferStateLayer::needsFiltering(const DisplayDevice* display) const {
1449 const auto outputLayer = findOutputLayerForDisplay(display);
1450 if (outputLayer == nullptr) {
1451 return false;
1452 }
1453
1454 // We need filtering if the sourceCrop rectangle size does not match the
1455 // displayframe rectangle size (not a 1:1 render)
1456 const auto& compositionState = outputLayer->getState();
1457 const auto displayFrame = compositionState.displayFrame;
1458 const auto sourceCrop = compositionState.sourceCrop;
1459 return sourceCrop.getHeight() != displayFrame.getHeight() ||
1460 sourceCrop.getWidth() != displayFrame.getWidth();
1461}
1462
1463bool BufferStateLayer::needsFilteringForScreenshots(
1464 const DisplayDevice* display, const ui::Transform& inverseParentTransform) const {
1465 const auto outputLayer = findOutputLayerForDisplay(display);
1466 if (outputLayer == nullptr) {
1467 return false;
1468 }
1469
1470 // We need filtering if the sourceCrop rectangle size does not match the
1471 // viewport rectangle size (not a 1:1 render)
1472 const auto& compositionState = outputLayer->getState();
1473 const ui::Transform& displayTransform = display->getTransform();
1474 const ui::Transform inverseTransform = inverseParentTransform * displayTransform.inverse();
1475 // Undo the transformation of the displayFrame so that we're back into
1476 // layer-stack space.
1477 const Rect frame = inverseTransform.transform(compositionState.displayFrame);
1478 const FloatRect sourceCrop = compositionState.sourceCrop;
1479
1480 int32_t frameHeight = frame.getHeight();
1481 int32_t frameWidth = frame.getWidth();
1482 // If the display transform had a rotational component then undo the
1483 // rotation so that the orientation matches the source crop.
1484 if (displayTransform.getOrientation() & ui::Transform::ROT_90) {
1485 std::swap(frameHeight, frameWidth);
1486 }
1487 return sourceCrop.getHeight() != frameHeight || sourceCrop.getWidth() != frameWidth;
1488}
1489
1490void BufferStateLayer::latchAndReleaseBuffer() {
1491 if (hasReadyFrame()) {
1492 bool ignored = false;
Vishnu Nair397a0e32022-07-26 00:01:48 +00001493 latchBuffer(ignored, systemTime());
Vishnu Naira72f6c02022-07-01 05:33:08 +00001494 }
1495 releasePendingBuffer(systemTime());
1496}
1497
1498PixelFormat BufferStateLayer::getPixelFormat() const {
1499 return mBufferInfo.mPixelFormat;
1500}
1501
1502bool BufferStateLayer::getTransformToDisplayInverse() const {
1503 return mBufferInfo.mTransformToDisplayInverse;
1504}
1505
1506Rect BufferStateLayer::getBufferCrop() const {
1507 // this is the crop rectangle that applies to the buffer
1508 // itself (as opposed to the window)
1509 if (!mBufferInfo.mCrop.isEmpty()) {
1510 // if the buffer crop is defined, we use that
1511 return mBufferInfo.mCrop;
1512 } else if (mBufferInfo.mBuffer != nullptr) {
1513 // otherwise we use the whole buffer
1514 return mBufferInfo.mBuffer->getBounds();
1515 } else {
1516 // if we don't have a buffer yet, we use an empty/invalid crop
1517 return Rect();
1518 }
1519}
1520
1521uint32_t BufferStateLayer::getBufferTransform() const {
1522 return mBufferInfo.mTransform;
1523}
1524
1525ui::Dataspace BufferStateLayer::getDataSpace() const {
1526 return mBufferInfo.mDataspace;
1527}
1528
1529ui::Dataspace BufferStateLayer::translateDataspace(ui::Dataspace dataspace) {
1530 ui::Dataspace updatedDataspace = dataspace;
1531 // translate legacy dataspaces to modern dataspaces
1532 switch (dataspace) {
1533 case ui::Dataspace::SRGB:
1534 updatedDataspace = ui::Dataspace::V0_SRGB;
1535 break;
1536 case ui::Dataspace::SRGB_LINEAR:
1537 updatedDataspace = ui::Dataspace::V0_SRGB_LINEAR;
1538 break;
1539 case ui::Dataspace::JFIF:
1540 updatedDataspace = ui::Dataspace::V0_JFIF;
1541 break;
1542 case ui::Dataspace::BT601_625:
1543 updatedDataspace = ui::Dataspace::V0_BT601_625;
1544 break;
1545 case ui::Dataspace::BT601_525:
1546 updatedDataspace = ui::Dataspace::V0_BT601_525;
1547 break;
1548 case ui::Dataspace::BT709:
1549 updatedDataspace = ui::Dataspace::V0_BT709;
1550 break;
1551 default:
1552 break;
1553 }
1554
1555 return updatedDataspace;
1556}
1557
1558sp<GraphicBuffer> BufferStateLayer::getBuffer() const {
1559 return mBufferInfo.mBuffer ? mBufferInfo.mBuffer->getBuffer() : nullptr;
1560}
1561
1562void BufferStateLayer::getDrawingTransformMatrix(bool filteringEnabled, float outMatrix[16]) {
1563 sp<GraphicBuffer> buffer = getBuffer();
1564 if (!buffer) {
1565 ALOGE("Buffer should not be null!");
1566 return;
1567 }
1568 GLConsumer::computeTransformMatrix(outMatrix, buffer->getWidth(), buffer->getHeight(),
1569 buffer->getPixelFormat(), mBufferInfo.mCrop,
1570 mBufferInfo.mTransform, filteringEnabled);
1571}
1572
1573void BufferStateLayer::setInitialValuesForClone(const sp<Layer>& clonedFrom) {
1574 Layer::setInitialValuesForClone(clonedFrom);
1575
Ady Abrahamd11bade2022-08-01 16:18:03 -07001576 sp<BufferStateLayer> bufferClonedFrom =
1577 sp<BufferStateLayer>::fromExisting(static_cast<BufferStateLayer*>(clonedFrom.get()));
Vishnu Naira72f6c02022-07-01 05:33:08 +00001578 mPremultipliedAlpha = bufferClonedFrom->mPremultipliedAlpha;
1579 mPotentialCursor = bufferClonedFrom->mPotentialCursor;
1580 mProtectedByApp = bufferClonedFrom->mProtectedByApp;
1581
1582 updateCloneBufferInfo();
1583}
1584
1585void BufferStateLayer::updateCloneBufferInfo() {
1586 if (!isClone() || !isClonedFromAlive()) {
1587 return;
1588 }
1589
Ady Abrahamd11bade2022-08-01 16:18:03 -07001590 sp<BufferStateLayer> clonedFrom = sp<BufferStateLayer>::fromExisting(
1591 static_cast<BufferStateLayer*>(getClonedFrom().get()));
Vishnu Naira72f6c02022-07-01 05:33:08 +00001592 mBufferInfo = clonedFrom->mBufferInfo;
1593 mSidebandStream = clonedFrom->mSidebandStream;
1594 surfaceDamageRegion = clonedFrom->surfaceDamageRegion;
1595 mCurrentFrameNumber = clonedFrom->mCurrentFrameNumber.load();
1596 mPreviousFrameNumber = clonedFrom->mPreviousFrameNumber;
1597
1598 // After buffer info is updated, the drawingState from the real layer needs to be copied into
1599 // the cloned. This is because some properties of drawingState can change when latchBuffer is
1600 // called. However, copying the drawingState would also overwrite the cloned layer's relatives
1601 // and touchableRegionCrop. Therefore, temporarily store the relatives so they can be set in
1602 // the cloned drawingState again.
1603 wp<Layer> tmpZOrderRelativeOf = mDrawingState.zOrderRelativeOf;
1604 SortedVector<wp<Layer>> tmpZOrderRelatives = mDrawingState.zOrderRelatives;
1605 wp<Layer> tmpTouchableRegionCrop = mDrawingState.touchableRegionCrop;
1606 WindowInfo tmpInputInfo = mDrawingState.inputInfo;
1607
1608 cloneDrawingState(clonedFrom.get());
1609
1610 mDrawingState.touchableRegionCrop = tmpTouchableRegionCrop;
1611 mDrawingState.zOrderRelativeOf = tmpZOrderRelativeOf;
1612 mDrawingState.zOrderRelatives = tmpZOrderRelatives;
1613 mDrawingState.inputInfo = tmpInputInfo;
1614}
1615
1616void BufferStateLayer::setTransformHint(ui::Transform::RotationFlags displayTransformHint) {
1617 mTransformHint = getFixedTransformHint();
1618 if (mTransformHint == ui::Transform::ROT_INVALID) {
1619 mTransformHint = displayTransformHint;
1620 }
1621}
1622
1623const std::shared_ptr<renderengine::ExternalTexture>& BufferStateLayer::getExternalTexture() const {
1624 return mBufferInfo.mBuffer;
1625}
1626
Marissa Wall61c58622018-07-18 10:12:20 -07001627} // namespace android