blob: 454363ac3fd7f06b1af9dc537d5f227a0a963d73 [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>
Lloyd Pique9755fb72019-03-26 14:44:40 -070027#include <compositionengine/LayerFECompositionState.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
Vishnu Nairfa247b12020-02-11 08:58:26 -080033#include "EffectLayer.h"
Adithya Srinivasanb238cd52021-02-04 17:54:05 +000034#include "FrameTracer/FrameTracer.h"
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080035#include "TimeStats/TimeStats.h"
Valerie Hau0bc09152018-12-20 07:42:47 -080036
Marissa Wall61c58622018-07-18 10:12:20 -070037namespace android {
38
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +000039using PresentState = frametimeline::SurfaceFrame::PresentState;
Vishnu Nair1506b182021-02-22 14:35:15 -080040namespace {
41void callReleaseBufferCallback(const sp<ITransactionCompletedListener>& listener,
Vishnu Nair4ba0c2e2021-06-24 11:27:17 -070042 const sp<GraphicBuffer>& buffer, uint64_t framenumber,
chaviw69058fb2021-09-27 09:37:30 -050043 const sp<Fence>& releaseFence,
Vishnu Nair4ba0c2e2021-06-24 11:27:17 -070044 uint32_t currentMaxAcquiredBufferCount) {
Vishnu Nair1506b182021-02-22 14:35:15 -080045 if (!listener) {
46 return;
47 }
Vishnu Nair4ba0c2e2021-06-24 11:27:17 -070048 listener->onReleaseBuffer({buffer->getId(), framenumber},
chaviw69058fb2021-09-27 09:37:30 -050049 releaseFence ? releaseFence : Fence::NO_FENCE,
Vishnu Nair4ba0c2e2021-06-24 11:27:17 -070050 currentMaxAcquiredBufferCount);
Vishnu Nair1506b182021-02-22 14:35:15 -080051}
52} // namespace
53
Marissa Wall947d34e2019-03-29 14:03:53 -070054BufferStateLayer::BufferStateLayer(const LayerCreationArgs& args)
55 : BufferLayer(args), mHwcSlotGenerator(new HwcSlotGenerator()) {
Robert Carr6a160312021-05-17 12:08:20 -070056 mDrawingState.dataspace = ui::Dataspace::V0_SRGB;
Vishnu Nair60356342018-11-13 13:00:45 -080057}
Marissa Wall61c58622018-07-18 10:12:20 -070058
Alec Mouri4545a8a2019-08-08 20:05:32 -070059BufferStateLayer::~BufferStateLayer() {
chaviwb4c6e582019-08-16 14:35:07 -070060 // The original layer and the clone layer share the same texture and buffer. Therefore, only
61 // one of the layers, in this case the original layer, needs to handle the deletion. The
62 // original layer and the clone should be removed at the same time so there shouldn't be any
63 // issue with the clone layer trying to use the texture.
64 if (mBufferInfo.mBuffer != nullptr && !isClone()) {
Alec Mouria90a5702021-04-16 16:36:21 +000065 callReleaseBufferCallback(mDrawingState.releaseBufferListener,
Vishnu Nair4ba0c2e2021-06-24 11:27:17 -070066 mBufferInfo.mBuffer->getBuffer(), mBufferInfo.mFrameNumber,
chaviw69058fb2021-09-27 09:37:30 -050067 mBufferInfo.mFence,
Ady Abraham899dcdb2021-06-15 16:56:21 -070068 mFlinger->getMaxAcquiredBufferCountForCurrentRefreshRate(
69 mOwnerUid));
Alec Mouri4545a8a2019-08-08 20:05:32 -070070 }
71}
72
Marissa Wall61c58622018-07-18 10:12:20 -070073// -----------------------------------------------------------------------
74// Interface implementation for Layer
75// -----------------------------------------------------------------------
Sally Qi403a05b2021-08-20 16:28:35 -070076void BufferStateLayer::onLayerDisplayed(
77 std::shared_future<renderengine::RenderEngineResult> futureRenderEngineResult) {
Robert Carrccab4242021-09-28 16:53:03 -070078 // If a layer has been displayed again we may need to clear
79 // the mLastClientComposition fence that we use for early release in setBuffer
80 // (as we now have a new fence which won't pass through the client composition path in some cases
81 // e.g. screenshot). We expect one call to onLayerDisplayed after receiving the GL comp fence
82 // from a single composition cycle, and want to clear on the second call
83 // (which we track with mLastClientCompositionDisplayed)
84 if (mLastClientCompositionDisplayed) {
85 mLastClientCompositionFence = nullptr;
86 mLastClientCompositionDisplayed = false;
87 } else if (mLastClientCompositionFence) {
88 mLastClientCompositionDisplayed = true;
89 }
90
Marissa Wall5a68a772018-12-22 17:43:42 -080091 // The previous release fence notifies the client that SurfaceFlinger is done with the previous
92 // buffer that was presented on this layer. The first transaction that came in this frame that
93 // replaced the previous buffer on this layer needs this release fence, because the fence will
94 // let the client know when that previous buffer is removed from the screen.
95 //
96 // Every other transaction on this layer does not need a release fence because no other
97 // Transactions that were set on this layer this frame are going to have their preceeding buffer
98 // removed from the display this frame.
99 //
100 // For example, if we have 3 transactions this frame. The first transaction doesn't contain a
101 // buffer so it doesn't need a previous release fence because the layer still needs the previous
102 // buffer. The second transaction contains a buffer so it needs a previous release fence because
103 // the previous buffer will be released this frame. The third transaction also contains a
104 // buffer. It replaces the buffer in the second transaction. The buffer in the second
105 // transaction will now no longer be presented so it is released immediately and the third
106 // transaction doesn't need a previous release fence.
Robert Carr8d958532020-11-10 14:09:16 -0800107 sp<CallbackHandle> ch;
Marissa Wall5a68a772018-12-22 17:43:42 -0800108 for (auto& handle : mDrawingState.callbackHandles) {
chaviw0b06a8d2021-08-06 11:49:08 -0500109 if (handle->releasePreviousBuffer &&
110 mDrawingState.releaseBufferEndpoint == handle->listener) {
Robert Carr8d958532020-11-10 14:09:16 -0800111 ch = handle;
Marissa Wall5a68a772018-12-22 17:43:42 -0800112 break;
113 }
114 }
Mikael Pessa2e1608f2019-07-19 11:25:35 -0700115
Sally Qi403a05b2021-08-20 16:28:35 -0700116 mListPreviousReleaseFences.emplace_back(futureRenderEngineResult);
Valerie Haubf784642020-01-29 07:25:23 -0800117
Mikael Pessa2e1608f2019-07-19 11:25:35 -0700118 // Prevent tracing the same release multiple times.
119 if (mPreviousFrameNumber != mPreviousReleasedFrameNumber) {
Mikael Pessa2e1608f2019-07-19 11:25:35 -0700120 mPreviousReleasedFrameNumber = mPreviousFrameNumber;
121 }
Sally Qi403a05b2021-08-20 16:28:35 -0700122
123 if (ch != nullptr) {
124 ch->previousReleaseCallbackId = mPreviousReleaseCallbackId;
125 ch->previousReleaseFences.emplace_back(futureRenderEngineResult);
126 ch->name = mName;
127 }
Marissa Wall61c58622018-07-18 10:12:20 -0700128}
129
Jorim Jaggi9c03b502020-11-24 23:51:31 +0100130void BufferStateLayer::onSurfaceFrameCreated(
131 const std::shared_ptr<frametimeline::SurfaceFrame>& surfaceFrame) {
Adithya Srinivasand17c7da2021-03-05 20:43:32 +0000132 while (mPendingJankClassifications.size() >= kPendingClassificationMaxSurfaceFrames) {
133 // Too many SurfaceFrames pending classification. The front of the deque is probably not
134 // tracked by FrameTimeline and will never be presented. This will only result in a memory
135 // leak.
136 ALOGW("Removing the front of pending jank deque from layer - %s to prevent memory leak",
137 mName.c_str());
Adithya Srinivasan785addd2021-03-09 00:38:00 +0000138 std::string miniDump = mPendingJankClassifications.front()->miniDump();
139 ALOGD("Head SurfaceFrame mini dump\n%s", miniDump.c_str());
Adithya Srinivasand17c7da2021-03-05 20:43:32 +0000140 mPendingJankClassifications.pop_front();
141 }
Jorim Jaggi9c03b502020-11-24 23:51:31 +0100142 mPendingJankClassifications.emplace_back(surfaceFrame);
143}
144
Valerie Haubf784642020-01-29 07:25:23 -0800145void BufferStateLayer::releasePendingBuffer(nsecs_t dequeueReadyTime) {
Valerie Hau32cdc1f2019-10-21 14:45:54 -0700146 for (const auto& handle : mDrawingState.callbackHandles) {
147 handle->transformHint = mTransformHint;
Valerie Hau871d6352020-01-29 08:44:02 -0800148 handle->dequeueReadyTime = dequeueReadyTime;
Ady Abraham899dcdb2021-06-15 16:56:21 -0700149 handle->currentMaxAcquiredBufferCount =
150 mFlinger->getMaxAcquiredBufferCountForCurrentRefreshRate(mOwnerUid);
Valerie Hau32cdc1f2019-10-21 14:45:54 -0700151 }
152
Vishnu Nair1506b182021-02-22 14:35:15 -0800153 for (auto& handle : mDrawingState.callbackHandles) {
chaviw0b06a8d2021-08-06 11:49:08 -0500154 if (handle->releasePreviousBuffer &&
155 mDrawingState.releaseBufferEndpoint == handle->listener) {
Vishnu Nair4ba0c2e2021-06-24 11:27:17 -0700156 handle->previousReleaseCallbackId = mPreviousReleaseCallbackId;
Vishnu Nair1506b182021-02-22 14:35:15 -0800157 break;
158 }
159 }
160
Jorim Jaggi9c03b502020-11-24 23:51:31 +0100161 std::vector<JankData> jankData;
162 jankData.reserve(mPendingJankClassifications.size());
163 while (!mPendingJankClassifications.empty()
164 && mPendingJankClassifications.front()->getJankType()) {
165 std::shared_ptr<frametimeline::SurfaceFrame> surfaceFrame =
166 mPendingJankClassifications.front();
167 mPendingJankClassifications.pop_front();
168 jankData.emplace_back(
169 JankData(surfaceFrame->getToken(), surfaceFrame->getJankType().value()));
170 }
171
Robert Carr3d1047b2021-09-20 18:22:32 -0700172 mFlinger->getTransactionCallbackInvoker().addCallbackHandles(
Jorim Jaggi9c03b502020-11-24 23:51:31 +0100173 mDrawingState.callbackHandles, jankData);
Marissa Wall5a68a772018-12-22 17:43:42 -0800174
Sally Qi403a05b2021-08-20 16:28:35 -0700175 sp<Fence> releaseFence = Fence::NO_FENCE;
176 for (auto& handle : mDrawingState.callbackHandles) {
177 if (handle->releasePreviousBuffer &&
178 mDrawingState.releaseBufferEndpoint == handle->listener) {
179 releaseFence =
180 handle->previousReleaseFence ? handle->previousReleaseFence : Fence::NO_FENCE;
181 break;
182 }
183 }
184
Marissa Wall5a68a772018-12-22 17:43:42 -0800185 mDrawingState.callbackHandles = {};
Valerie Haubf784642020-01-29 07:25:23 -0800186
Valerie Haubf784642020-01-29 07:25:23 -0800187 std::shared_ptr<FenceTime> releaseFenceTime = std::make_shared<FenceTime>(releaseFence);
188 {
189 Mutex::Autolock lock(mFrameEventHistoryMutex);
190 if (mPreviousFrameNumber != 0) {
191 mFrameEventHistory.addRelease(mPreviousFrameNumber, dequeueReadyTime,
192 std::move(releaseFenceTime));
193 }
194 }
Marissa Wall61c58622018-07-18 10:12:20 -0700195}
196
Valerie Hau871d6352020-01-29 08:44:02 -0800197void BufferStateLayer::finalizeFrameEventHistory(const std::shared_ptr<FenceTime>& glDoneFence,
198 const CompositorTiming& compositorTiming) {
199 for (const auto& handle : mDrawingState.callbackHandles) {
200 handle->gpuCompositionDoneFence = glDoneFence;
201 handle->compositorTiming = compositorTiming;
202 }
203}
204
Marissa Walle2ffb422018-10-12 11:33:52 -0700205bool BufferStateLayer::willPresentCurrentTransaction() const {
206 // Returns true if the most recent Transaction applied to CurrentState will be presented.
Robert Carr321e83c2019-08-19 15:49:30 -0700207 return (getSidebandStreamChanged() || getAutoRefresh() ||
Robert Carr6a160312021-05-17 12:08:20 -0700208 (mDrawingState.modified &&
209 (mDrawingState.buffer != nullptr || mDrawingState.bgColorLayer != nullptr)));
Marissa Wall61c58622018-07-18 10:12:20 -0700210}
211
Chavi Weingartena5aedbd2021-04-09 13:37:33 +0000212Rect BufferStateLayer::getCrop(const Layer::State& s) const {
213 return s.crop;
Marissa Wall61c58622018-07-18 10:12:20 -0700214}
215
216bool BufferStateLayer::setTransform(uint32_t transform) {
Robert Carr6a160312021-05-17 12:08:20 -0700217 if (mDrawingState.bufferTransform == transform) return false;
218 mDrawingState.bufferTransform = transform;
219 mDrawingState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700220 setTransactionFlags(eTransactionNeeded);
221 return true;
222}
223
224bool BufferStateLayer::setTransformToDisplayInverse(bool transformToDisplayInverse) {
Robert Carr6a160312021-05-17 12:08:20 -0700225 if (mDrawingState.transformToDisplayInverse == transformToDisplayInverse) return false;
226 mDrawingState.sequence++;
227 mDrawingState.transformToDisplayInverse = transformToDisplayInverse;
228 mDrawingState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700229 setTransactionFlags(eTransactionNeeded);
230 return true;
231}
232
233bool BufferStateLayer::setCrop(const Rect& crop) {
Robert Carr6a160312021-05-17 12:08:20 -0700234 if (mDrawingState.crop == crop) return false;
235 mDrawingState.sequence++;
236 mDrawingState.crop = crop;
Marissa Wall290ad082019-03-06 13:23:47 -0800237
Robert Carr6a160312021-05-17 12:08:20 -0700238 mDrawingState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700239 setTransactionFlags(eTransactionNeeded);
240 return true;
241}
242
chaviwf3f40fe2021-04-27 15:54:02 -0500243bool BufferStateLayer::setBufferCrop(const Rect& bufferCrop) {
Robert Carr6a160312021-05-17 12:08:20 -0700244 if (mDrawingState.bufferCrop == bufferCrop) return false;
chaviwf3f40fe2021-04-27 15:54:02 -0500245
Robert Carr6a160312021-05-17 12:08:20 -0700246 mDrawingState.sequence++;
247 mDrawingState.bufferCrop = bufferCrop;
chaviwf3f40fe2021-04-27 15:54:02 -0500248
Robert Carr6a160312021-05-17 12:08:20 -0700249 mDrawingState.modified = true;
chaviwf3f40fe2021-04-27 15:54:02 -0500250 setTransactionFlags(eTransactionNeeded);
251 return true;
252}
253
Vishnu Nair6bdec7d2021-05-10 15:01:13 -0700254bool BufferStateLayer::setDestinationFrame(const Rect& destinationFrame) {
Robert Carr6a160312021-05-17 12:08:20 -0700255 if (mDrawingState.destinationFrame == destinationFrame) return false;
Vishnu Nair6bdec7d2021-05-10 15:01:13 -0700256
Robert Carr6a160312021-05-17 12:08:20 -0700257 mDrawingState.sequence++;
258 mDrawingState.destinationFrame = destinationFrame;
Vishnu Nair6bdec7d2021-05-10 15:01:13 -0700259
Robert Carr6a160312021-05-17 12:08:20 -0700260 mDrawingState.modified = true;
Vishnu Nair6bdec7d2021-05-10 15:01:13 -0700261 setTransactionFlags(eTransactionNeeded);
262 return true;
263}
264
Robert Carr6a160312021-05-17 12:08:20 -0700265static bool assignTransform(ui::Transform* dst, ui::Transform& from) {
266 if (*dst == from) {
267 return false;
268 }
269 *dst = from;
270 return true;
271}
272
Vishnu Nair6bdec7d2021-05-10 15:01:13 -0700273// Translate destination frame into scale and position. If a destination frame is not set, use the
274// provided scale and position
Robert Carr6a160312021-05-17 12:08:20 -0700275bool BufferStateLayer::updateGeometry() {
276 if (mDrawingState.destinationFrame.isEmpty()) {
Vishnu Nair6bdec7d2021-05-10 15:01:13 -0700277 // If destination frame is not set, use the requested transform set via
278 // BufferStateLayer::setPosition and BufferStateLayer::setMatrix.
Robert Carr6a160312021-05-17 12:08:20 -0700279 return assignTransform(&mDrawingState.transform, mRequestedTransform);
Vishnu Nair6bdec7d2021-05-10 15:01:13 -0700280 }
281
Robert Carr6a160312021-05-17 12:08:20 -0700282 Rect destRect = mDrawingState.destinationFrame;
Vishnu Nair6bdec7d2021-05-10 15:01:13 -0700283 int32_t destW = destRect.width();
284 int32_t destH = destRect.height();
285 if (destRect.left < 0) {
286 destRect.left = 0;
287 destRect.right = destW;
288 }
289 if (destRect.top < 0) {
290 destRect.top = 0;
291 destRect.bottom = destH;
292 }
293
Robert Carr6a160312021-05-17 12:08:20 -0700294 if (!mDrawingState.buffer) {
Vishnu Nair6bdec7d2021-05-10 15:01:13 -0700295 ui::Transform t;
296 t.set(destRect.left, destRect.top);
Robert Carr6a160312021-05-17 12:08:20 -0700297 return assignTransform(&mDrawingState.transform, t);
Vishnu Nair6bdec7d2021-05-10 15:01:13 -0700298 }
299
Robert Carr6a160312021-05-17 12:08:20 -0700300 uint32_t bufferWidth = mDrawingState.buffer->getBuffer()->getWidth();
301 uint32_t bufferHeight = mDrawingState.buffer->getBuffer()->getHeight();
Vishnu Nair6bdec7d2021-05-10 15:01:13 -0700302 // Undo any transformations on the buffer.
Robert Carr6a160312021-05-17 12:08:20 -0700303 if (mDrawingState.bufferTransform & ui::Transform::ROT_90) {
Vishnu Nair6bdec7d2021-05-10 15:01:13 -0700304 std::swap(bufferWidth, bufferHeight);
305 }
306 uint32_t invTransform = DisplayDevice::getPrimaryDisplayRotationFlags();
Robert Carr6a160312021-05-17 12:08:20 -0700307 if (mDrawingState.transformToDisplayInverse) {
Vishnu Nair6bdec7d2021-05-10 15:01:13 -0700308 if (invTransform & ui::Transform::ROT_90) {
309 std::swap(bufferWidth, bufferHeight);
310 }
311 }
312
313 float sx = destW / static_cast<float>(bufferWidth);
314 float sy = destH / static_cast<float>(bufferHeight);
315 ui::Transform t;
316 t.set(sx, 0, 0, sy);
317 t.set(destRect.left, destRect.top);
Robert Carr6a160312021-05-17 12:08:20 -0700318 return assignTransform(&mDrawingState.transform, t);
Vishnu Nair6bdec7d2021-05-10 15:01:13 -0700319}
320
Chavi Weingartena5aedbd2021-04-09 13:37:33 +0000321bool BufferStateLayer::setMatrix(const layer_state_t::matrix22_t& matrix,
322 bool allowNonRectPreservingTransforms) {
Vishnu Nair6bdec7d2021-05-10 15:01:13 -0700323 if (mRequestedTransform.dsdx() == matrix.dsdx && mRequestedTransform.dtdy() == matrix.dtdy &&
324 mRequestedTransform.dtdx() == matrix.dtdx && mRequestedTransform.dsdy() == matrix.dsdy) {
Marissa Wall861616d2018-10-22 12:52:23 -0700325 return false;
326 }
327
Chavi Weingartena5aedbd2021-04-09 13:37:33 +0000328 ui::Transform t;
329 t.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
330
331 if (!allowNonRectPreservingTransforms && !t.preserveRects()) {
332 ALOGW("Attempt to set rotation matrix without permission ACCESS_SURFACE_FLINGER nor "
333 "ROTATE_SURFACE_FLINGER ignored");
334 return false;
Marissa Wall861616d2018-10-22 12:52:23 -0700335 }
Chavi Weingartena5aedbd2021-04-09 13:37:33 +0000336
Vishnu Nair6bdec7d2021-05-10 15:01:13 -0700337 mRequestedTransform.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
chaviw9a93ea62021-03-11 16:44:42 -0600338
Robert Carr6a160312021-05-17 12:08:20 -0700339 mDrawingState.sequence++;
340 mDrawingState.modified = true;
chaviw9a93ea62021-03-11 16:44:42 -0600341 setTransactionFlags(eTransactionNeeded);
Chavi Weingartena5aedbd2021-04-09 13:37:33 +0000342
343 return true;
344}
345
346bool BufferStateLayer::setPosition(float x, float y) {
Vishnu Nair6bdec7d2021-05-10 15:01:13 -0700347 if (mRequestedTransform.tx() == x && mRequestedTransform.ty() == y) {
Chavi Weingartena5aedbd2021-04-09 13:37:33 +0000348 return false;
349 }
350
Vishnu Nair6bdec7d2021-05-10 15:01:13 -0700351 mRequestedTransform.set(x, y);
Chavi Weingartena5aedbd2021-04-09 13:37:33 +0000352
Robert Carr6a160312021-05-17 12:08:20 -0700353 mDrawingState.sequence++;
354 mDrawingState.modified = true;
Chavi Weingartena5aedbd2021-04-09 13:37:33 +0000355 setTransactionFlags(eTransactionNeeded);
356
Marissa Wall861616d2018-10-22 12:52:23 -0700357 return true;
358}
359
Valerie Hau871d6352020-01-29 08:44:02 -0800360bool BufferStateLayer::addFrameEvent(const sp<Fence>& acquireFence, nsecs_t postedTime,
361 nsecs_t desiredPresentTime) {
Valerie Haubf784642020-01-29 07:25:23 -0800362 Mutex::Autolock lock(mFrameEventHistoryMutex);
363 mAcquireTimeline.updateSignalTimes();
364 std::shared_ptr<FenceTime> acquireFenceTime =
365 std::make_shared<FenceTime>((acquireFence ? acquireFence : Fence::NO_FENCE));
Robert Carr6a160312021-05-17 12:08:20 -0700366 NewFrameEventsEntry newTimestamps = {mDrawingState.frameNumber, postedTime, desiredPresentTime,
Valerie Haubf784642020-01-29 07:25:23 -0800367 acquireFenceTime};
Valerie Hau871d6352020-01-29 08:44:02 -0800368 mFrameEventHistory.setProducerWantsEvents();
Valerie Haubf784642020-01-29 07:25:23 -0800369 mFrameEventHistory.addQueue(newTimestamps);
370 return true;
371}
372
chaviwba4320c2021-09-15 15:20:53 -0500373std::shared_ptr<renderengine::ExternalTexture> BufferStateLayer::getBufferFromBufferData(
374 const BufferData& bufferData) {
375 bool cacheIdChanged = bufferData.flags.test(BufferData::BufferDataChange::cachedBufferChanged);
376 bool bufferSizeExceedsLimit = false;
377 std::shared_ptr<renderengine::ExternalTexture> buffer = nullptr;
378 if (cacheIdChanged && bufferData.buffer != nullptr) {
379 bufferSizeExceedsLimit =
380 mFlinger->exceedsMaxRenderTargetSize(bufferData.buffer->getWidth(),
381 bufferData.buffer->getHeight());
382 if (!bufferSizeExceedsLimit) {
383 ClientCache::getInstance().add(bufferData.cachedBuffer, bufferData.buffer);
384 buffer = ClientCache::getInstance().get(bufferData.cachedBuffer);
385 }
386 } else if (cacheIdChanged) {
387 buffer = ClientCache::getInstance().get(bufferData.cachedBuffer);
388 } else if (bufferData.buffer != nullptr) {
389 bufferSizeExceedsLimit =
390 mFlinger->exceedsMaxRenderTargetSize(bufferData.buffer->getWidth(),
391 bufferData.buffer->getHeight());
392 if (!bufferSizeExceedsLimit) {
393 buffer = std::make_shared<
394 renderengine::ExternalTexture>(bufferData.buffer, mFlinger->getRenderEngine(),
395 renderengine::ExternalTexture::Usage::READABLE);
396 }
397 }
398 ALOGE_IF(bufferSizeExceedsLimit,
399 "Attempted to create an ExternalTexture for layer %s that exceeds render target size "
400 "limit.",
401 getDebugName());
402 return buffer;
403}
404
405bool BufferStateLayer::setBuffer(const BufferData& bufferData, nsecs_t postTime,
Alec Mouria90a5702021-04-16 16:36:21 +0000406 nsecs_t desiredPresentTime, bool isAutoTimestamp,
chaviwba4320c2021-09-15 15:20:53 -0500407 std::optional<nsecs_t> dequeueTime,
408 const FrameTimelineInfo& info) {
Robert Carr0c1966e2020-10-19 12:12:08 -0700409 ATRACE_CALL();
410
chaviwba4320c2021-09-15 15:20:53 -0500411 const std::shared_ptr<renderengine::ExternalTexture>& buffer =
412 getBufferFromBufferData(bufferData);
413 if (!buffer) {
414 return false;
415 }
416
417 const bool frameNumberChanged =
418 bufferData.flags.test(BufferData::BufferDataChange::frameNumberChanged);
419 const uint64_t frameNumber =
420 frameNumberChanged ? bufferData.frameNumber : mDrawingState.frameNumber + 1;
421
Robert Carr6a160312021-05-17 12:08:20 -0700422 if (mDrawingState.buffer) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700423 mReleasePreviousBuffer = true;
Vishnu Nair52846cd2021-08-05 16:12:48 -0700424 if (mDrawingState.buffer != mBufferInfo.mBuffer ||
425 mDrawingState.frameNumber != mBufferInfo.mFrameNumber) {
Robert Carr6a160312021-05-17 12:08:20 -0700426 // If mDrawingState has a buffer, and we are about to update again
Robert Carr7121caf2020-12-15 13:07:32 -0800427 // before swapping to drawing state, then the first buffer will be
Vishnu Nair1506b182021-02-22 14:35:15 -0800428 // dropped and we should decrement the pending buffer count and
429 // call any release buffer callbacks if set.
Robert Carr6a160312021-05-17 12:08:20 -0700430 callReleaseBufferCallback(mDrawingState.releaseBufferListener,
Vishnu Nair4ba0c2e2021-06-24 11:27:17 -0700431 mDrawingState.buffer->getBuffer(), mDrawingState.frameNumber,
chaviw69058fb2021-09-27 09:37:30 -0500432 mDrawingState.acquireFence,
Ady Abraham899dcdb2021-06-15 16:56:21 -0700433 mFlinger->getMaxAcquiredBufferCountForCurrentRefreshRate(
434 mOwnerUid));
Robert Carr7121caf2020-12-15 13:07:32 -0800435 decrementPendingBufferCount();
Robert Carr6a160312021-05-17 12:08:20 -0700436 if (mDrawingState.bufferSurfaceFrameTX != nullptr &&
437 mDrawingState.bufferSurfaceFrameTX->getPresentState() != PresentState::Presented) {
438 addSurfaceFrameDroppedForBuffer(mDrawingState.bufferSurfaceFrameTX);
439 mDrawingState.bufferSurfaceFrameTX.reset();
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +0000440 }
Robert Carrccab4242021-09-28 16:53:03 -0700441 } else if (mLastClientCompositionFence != nullptr) {
442 callReleaseBufferCallback(mDrawingState.releaseBufferListener,
443 mDrawingState.buffer->getBuffer(), mDrawingState.frameNumber,
chaviw69058fb2021-09-27 09:37:30 -0500444 mLastClientCompositionFence,
Robert Carrccab4242021-09-28 16:53:03 -0700445 mFlinger->getMaxAcquiredBufferCountForCurrentRefreshRate(
chaviw69058fb2021-09-27 09:37:30 -0500446 mOwnerUid));
Robert Carrccab4242021-09-28 16:53:03 -0700447 mLastClientCompositionFence = nullptr;
Robert Carr7121caf2020-12-15 13:07:32 -0800448 }
Marissa Wallfda30bb2018-10-12 11:34:28 -0700449 }
Robert Carr6a160312021-05-17 12:08:20 -0700450
451 mDrawingState.frameNumber = frameNumber;
chaviwba4320c2021-09-15 15:20:53 -0500452 mDrawingState.releaseBufferListener = bufferData.releaseBufferListener;
Robert Carr6a160312021-05-17 12:08:20 -0700453 mDrawingState.buffer = buffer;
chaviwba4320c2021-09-15 15:20:53 -0500454 mDrawingState.clientCacheId = bufferData.cachedBuffer;
455
456 mDrawingState.acquireFence = bufferData.flags.test(BufferData::BufferDataChange::fenceChanged)
457 ? bufferData.acquireFence
458 : Fence::NO_FENCE;
459 mDrawingState.acquireFenceTime = std::make_unique<FenceTime>(mDrawingState.acquireFence);
460 // The acquire fences of BufferStateLayers have already signaled before they are set
461 mCallbackHandleAcquireTime = mDrawingState.acquireFenceTime->getSignalTime();
462
Robert Carr6a160312021-05-17 12:08:20 -0700463 mDrawingState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700464 setTransactionFlags(eTransactionNeeded);
Ady Abraham09bd3922019-04-08 10:44:56 -0700465
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800466 const int32_t layerId = getSequence();
Robert Carr6a160312021-05-17 12:08:20 -0700467 mFlinger->mTimeStats->setPostTime(layerId, mDrawingState.frameNumber, getName().c_str(),
Adithya Srinivasan58069dc2021-06-04 20:37:02 +0000468 mOwnerUid, postTime, getGameMode());
Robert Carr6a160312021-05-17 12:08:20 -0700469 mDrawingState.desiredPresentTime = desiredPresentTime;
470 mDrawingState.isAutoTimestamp = isAutoTimestamp;
Ady Abraham09bd3922019-04-08 10:44:56 -0700471
Ady Abrahamb7f15562021-03-15 18:34:08 -0700472 const nsecs_t presentTime = [&] {
473 if (!isAutoTimestamp) return desiredPresentTime;
474
475 const auto prediction =
476 mFlinger->mFrameTimeline->getTokenManager()->getPredictionsForToken(info.vsyncId);
477 if (prediction.has_value()) return prediction->presentTime;
478
479 return static_cast<nsecs_t>(0);
480 }();
481 mFlinger->mScheduler->recordLayerHistory(this, presentTime,
Ady Abraham5def7332020-05-29 16:13:47 -0700482 LayerHistory::LayerUpdateType::Buffer);
Ady Abraham09bd3922019-04-08 10:44:56 -0700483
chaviwba4320c2021-09-15 15:20:53 -0500484 addFrameEvent(mDrawingState.acquireFence, postTime, isAutoTimestamp ? 0 : desiredPresentTime);
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +0000485
Adithya Srinivasan891004e2021-02-12 20:20:47 +0000486 setFrameTimelineVsyncForBufferTransaction(info, postTime);
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +0000487
Alec Mouria90a5702021-04-16 16:36:21 +0000488 if (buffer && dequeueTime && *dequeueTime != 0) {
489 const uint64_t bufferId = buffer->getBuffer()->getId();
Adithya Srinivasanb238cd52021-02-04 17:54:05 +0000490 mFlinger->mFrameTracer->traceNewLayer(layerId, getName().c_str());
491 mFlinger->mFrameTracer->traceTimestamp(layerId, bufferId, frameNumber, *dequeueTime,
492 FrameTracer::FrameEvent::DEQUEUE);
493 mFlinger->mFrameTracer->traceTimestamp(layerId, bufferId, frameNumber, postTime,
494 FrameTracer::FrameEvent::QUEUE);
495 }
Chavi Weingartena5aedbd2021-04-09 13:37:33 +0000496
Robert Carr6a160312021-05-17 12:08:20 -0700497 mDrawingState.width = mDrawingState.buffer->getBuffer()->getWidth();
498 mDrawingState.height = mDrawingState.buffer->getBuffer()->getHeight();
chaviwba4320c2021-09-15 15:20:53 -0500499 mDrawingState.releaseBufferEndpoint = bufferData.releaseBufferEndpoint;
Marissa Wall61c58622018-07-18 10:12:20 -0700500 return true;
501}
502
503bool BufferStateLayer::setDataspace(ui::Dataspace dataspace) {
Robert Carr6a160312021-05-17 12:08:20 -0700504 if (mDrawingState.dataspace == dataspace) return false;
505 mDrawingState.dataspace = dataspace;
506 mDrawingState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700507 setTransactionFlags(eTransactionNeeded);
508 return true;
509}
510
511bool BufferStateLayer::setHdrMetadata(const HdrMetadata& hdrMetadata) {
Robert Carr6a160312021-05-17 12:08:20 -0700512 if (mDrawingState.hdrMetadata == hdrMetadata) return false;
513 mDrawingState.hdrMetadata = hdrMetadata;
514 mDrawingState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700515 setTransactionFlags(eTransactionNeeded);
516 return true;
517}
518
519bool BufferStateLayer::setSurfaceDamageRegion(const Region& surfaceDamage) {
Robert Carr6a160312021-05-17 12:08:20 -0700520 mDrawingState.surfaceDamageRegion = surfaceDamage;
521 mDrawingState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700522 setTransactionFlags(eTransactionNeeded);
523 return true;
524}
525
526bool BufferStateLayer::setApi(int32_t api) {
Robert Carr6a160312021-05-17 12:08:20 -0700527 if (mDrawingState.api == api) return false;
528 mDrawingState.api = api;
529 mDrawingState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700530 setTransactionFlags(eTransactionNeeded);
531 return true;
532}
533
534bool BufferStateLayer::setSidebandStream(const sp<NativeHandle>& sidebandStream) {
Robert Carr6a160312021-05-17 12:08:20 -0700535 if (mDrawingState.sidebandStream == sidebandStream) return false;
Robert Carr3e2a2992021-06-11 13:42:55 -0700536
537 if (mDrawingState.sidebandStream != nullptr && sidebandStream == nullptr) {
538 mFlinger->mTunnelModeEnabledReporter->decrementTunnelModeCount();
539 } else if (sidebandStream != nullptr) {
540 mFlinger->mTunnelModeEnabledReporter->incrementTunnelModeCount();
541 }
542
Robert Carr6a160312021-05-17 12:08:20 -0700543 mDrawingState.sidebandStream = sidebandStream;
544 mDrawingState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700545 setTransactionFlags(eTransactionNeeded);
Marissa Wall61c58622018-07-18 10:12:20 -0700546 if (!mSidebandStreamChanged.exchange(true)) {
547 // mSidebandStreamChanged was false
548 mFlinger->signalLayerUpdate();
549 }
550 return true;
551}
552
Marissa Walle2ffb422018-10-12 11:33:52 -0700553bool BufferStateLayer::setTransactionCompletedListeners(
554 const std::vector<sp<CallbackHandle>>& handles) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700555 // If there is no handle, we will not send a callback so reset mReleasePreviousBuffer and return
Marissa Walle2ffb422018-10-12 11:33:52 -0700556 if (handles.empty()) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700557 mReleasePreviousBuffer = false;
Marissa Walle2ffb422018-10-12 11:33:52 -0700558 return false;
559 }
560
561 const bool willPresent = willPresentCurrentTransaction();
562
563 for (const auto& handle : handles) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700564 // If this transaction set a buffer on this layer, release its previous buffer
565 handle->releasePreviousBuffer = mReleasePreviousBuffer;
566
Marissa Walle2ffb422018-10-12 11:33:52 -0700567 // If this layer will be presented in this frame
568 if (willPresent) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700569 // If this transaction set an acquire fence on this layer, set its acquire time
570 handle->acquireTime = mCallbackHandleAcquireTime;
Robert Carr6a160312021-05-17 12:08:20 -0700571 handle->frameNumber = mDrawingState.frameNumber;
Marissa Wallfda30bb2018-10-12 11:34:28 -0700572
Marissa Walle2ffb422018-10-12 11:33:52 -0700573 // Store so latched time and release fence can be set
Robert Carr6a160312021-05-17 12:08:20 -0700574 mDrawingState.callbackHandles.push_back(handle);
Marissa Walle2ffb422018-10-12 11:33:52 -0700575
576 } else { // If this layer will NOT need to be relatched and presented this frame
577 // Notify the transaction completed thread this handle is done
Robert Carr9a803c32021-01-14 16:57:58 -0800578 mFlinger->getTransactionCallbackInvoker().registerUnpresentedCallbackHandle(handle);
Marissa Walle2ffb422018-10-12 11:33:52 -0700579 }
580 }
581
Marissa Wallfda30bb2018-10-12 11:34:28 -0700582 mReleasePreviousBuffer = false;
583 mCallbackHandleAcquireTime = -1;
584
Marissa Walle2ffb422018-10-12 11:33:52 -0700585 return willPresent;
586}
587
Marissa Wall61c58622018-07-18 10:12:20 -0700588bool BufferStateLayer::setTransparentRegionHint(const Region& transparent) {
Vishnu Nair27e3ed52021-07-08 18:24:25 -0700589 mDrawingState.sequence++;
Robert Carr6a160312021-05-17 12:08:20 -0700590 mDrawingState.transparentRegionHint = transparent;
591 mDrawingState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700592 setTransactionFlags(eTransactionNeeded);
593 return true;
594}
595
rnleeed20fa42021-08-10 18:00:03 -0700596Rect BufferStateLayer::getBufferSize(const State& /*s*/) const {
Marissa Wall861616d2018-10-22 12:52:23 -0700597 // for buffer state layers we use the display frame size as the buffer size.
Marissa Wall61c58622018-07-18 10:12:20 -0700598
chaviw7e72caf2020-12-02 16:50:43 -0800599 if (mBufferInfo.mBuffer == nullptr) {
600 return Rect::INVALID_RECT;
601 }
602
Vishnu Nair6bdec7d2021-05-10 15:01:13 -0700603 uint32_t bufWidth = mBufferInfo.mBuffer->getBuffer()->getWidth();
604 uint32_t bufHeight = mBufferInfo.mBuffer->getBuffer()->getHeight();
605
606 // Undo any transformations on the buffer and return the result.
607 if (mBufferInfo.mTransform & ui::Transform::ROT_90) {
608 std::swap(bufWidth, bufHeight);
609 }
610
611 if (getTransformToDisplayInverse()) {
612 uint32_t invTransform = DisplayDevice::getPrimaryDisplayRotationFlags();
613 if (invTransform & ui::Transform::ROT_90) {
614 std::swap(bufWidth, bufHeight);
Marissa Wall861616d2018-10-22 12:52:23 -0700615 }
616 }
617
rnleeed20fa42021-08-10 18:00:03 -0700618 return Rect(0, 0, static_cast<int32_t>(bufWidth), static_cast<int32_t>(bufHeight));
Marissa Wall61c58622018-07-18 10:12:20 -0700619}
Vishnu Nair4351ad52019-02-11 14:13:02 -0800620
621FloatRect BufferStateLayer::computeSourceBounds(const FloatRect& parentBounds) const {
Vishnu Nair6bdec7d2021-05-10 15:01:13 -0700622 if (mBufferInfo.mBuffer == nullptr) {
623 return parentBounds;
Vishnu Nair4351ad52019-02-11 14:13:02 -0800624 }
625
Vishnu Nair6bdec7d2021-05-10 15:01:13 -0700626 return getBufferSize(getDrawingState()).toFloatRect();
Vishnu Nair4351ad52019-02-11 14:13:02 -0800627}
628
Marissa Wall61c58622018-07-18 10:12:20 -0700629// -----------------------------------------------------------------------
630
631// -----------------------------------------------------------------------
632// Interface implementation for BufferLayer
633// -----------------------------------------------------------------------
634bool BufferStateLayer::fenceHasSignaled() const {
Huihong Luo86c80e32021-06-16 15:41:07 -0700635 if (SurfaceFlinger::enableLatchUnsignaled) {
636 return true;
637 }
638
Alec Mouri91f6df32020-01-30 08:48:58 -0800639 const bool fenceSignaled =
640 getDrawingState().acquireFence->getStatus() == Fence::Status::Signaled;
641 if (!fenceSignaled) {
642 mFlinger->mTimeStats->incrementLatchSkipped(getSequence(),
643 TimeStats::LatchSkipReason::LateAcquire);
644 }
645
646 return fenceSignaled;
Marissa Wall61c58622018-07-18 10:12:20 -0700647}
648
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700649bool BufferStateLayer::framePresentTimeIsCurrent(nsecs_t expectedPresentTime) const {
Ady Abrahamcd1580c2019-04-29 15:40:03 -0700650 if (!hasFrameUpdate() || isRemovedFromCurrentState()) {
651 return true;
652 }
653
Robert Carr6a160312021-05-17 12:08:20 -0700654 return mDrawingState.isAutoTimestamp || mDrawingState.desiredPresentTime <= expectedPresentTime;
Ady Abrahamcd1580c2019-04-29 15:40:03 -0700655}
656
Valerie Hau871d6352020-01-29 08:44:02 -0800657bool BufferStateLayer::onPreComposition(nsecs_t refreshStartTime) {
658 for (const auto& handle : mDrawingState.callbackHandles) {
659 handle->refreshStartTime = refreshStartTime;
660 }
661 return BufferLayer::onPreComposition(refreshStartTime);
662}
663
Vishnu Naircf26a0a2020-11-13 12:56:20 -0800664void BufferStateLayer::setAutoRefresh(bool autoRefresh) {
665 if (!mAutoRefresh.exchange(autoRefresh)) {
666 mFlinger->signalLayerUpdate();
667 }
Marissa Wall61c58622018-07-18 10:12:20 -0700668}
669
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800670bool BufferStateLayer::latchSidebandStream(bool& recomputeVisibleRegions) {
baocheng suna663c2b2021-05-13 18:51:28 +0800671 // 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 +0800672 editCompositionState()->sidebandStreamHasFrame = hasFrameUpdate() && mSidebandStream.get();
baocheng suna663c2b2021-05-13 18:51:28 +0800673
baocheng sun9691b9c2021-08-03 19:27:06 +0800674 if (mSidebandStreamChanged.exchange(false)) {
Marissa Wall61c58622018-07-18 10:12:20 -0700675 const State& s(getDrawingState());
676 // mSidebandStreamChanged was true
Lloyd Pique0b785d82018-12-04 17:25:27 -0800677 mSidebandStream = s.sidebandStream;
Lloyd Piquede196652020-01-22 17:29:58 -0800678 editCompositionState()->sidebandStream = mSidebandStream;
Lloyd Pique0b785d82018-12-04 17:25:27 -0800679 if (mSidebandStream != nullptr) {
Marissa Wall61c58622018-07-18 10:12:20 -0700680 setTransactionFlags(eTransactionNeeded);
681 mFlinger->setTransactionFlags(eTraversalNeeded);
682 }
683 recomputeVisibleRegions = true;
684
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800685 return true;
Marissa Wall61c58622018-07-18 10:12:20 -0700686 }
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800687 return false;
Marissa Wall61c58622018-07-18 10:12:20 -0700688}
689
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800690bool BufferStateLayer::hasFrameUpdate() const {
Robert Carr6a160312021-05-17 12:08:20 -0700691 const State& c(getDrawingState());
Robert Carr315f3c72021-06-24 21:58:09 -0700692 return (mDrawingStateModified || mDrawingState.modified) && (c.buffer != nullptr || c.bgColorLayer != nullptr);
Marissa Wall61c58622018-07-18 10:12:20 -0700693}
694
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700695status_t BufferStateLayer::updateTexImage(bool& /*recomputeVisibleRegions*/, nsecs_t latchTime,
696 nsecs_t /*expectedPresentTime*/) {
Marissa Wall61c58622018-07-18 10:12:20 -0700697 const State& s(getDrawingState());
698
699 if (!s.buffer) {
Valerie Hauaa194562019-02-05 16:21:38 -0800700 if (s.bgColorLayer) {
701 for (auto& handle : mDrawingState.callbackHandles) {
702 handle->latchTime = latchTime;
703 }
704 }
Marissa Wall61c58622018-07-18 10:12:20 -0700705 return NO_ERROR;
706 }
707
Marissa Wall5a68a772018-12-22 17:43:42 -0800708 for (auto& handle : mDrawingState.callbackHandles) {
Vishnu Nair935590e2021-02-10 13:05:52 -0800709 if (handle->frameNumber == mDrawingState.frameNumber) {
710 handle->latchTime = latchTime;
711 }
Marissa Wall5a68a772018-12-22 17:43:42 -0800712 }
Marissa Walle2ffb422018-10-12 11:33:52 -0700713
Vishnu Nairea0de002020-11-17 17:42:37 -0800714 const int32_t layerId = getSequence();
Alec Mouria90a5702021-04-16 16:36:21 +0000715 const uint64_t bufferId = mDrawingState.buffer->getBuffer()->getId();
Adithya Srinivasanb238cd52021-02-04 17:54:05 +0000716 const uint64_t frameNumber = mDrawingState.frameNumber;
717 const auto acquireFence = std::make_shared<FenceTime>(mDrawingState.acquireFence);
718 mFlinger->mTimeStats->setAcquireFence(layerId, frameNumber, acquireFence);
719 mFlinger->mTimeStats->setLatchTime(layerId, frameNumber, latchTime);
720
721 mFlinger->mFrameTracer->traceFence(layerId, bufferId, frameNumber, acquireFence,
722 FrameTracer::FrameEvent::ACQUIRE_FENCE);
723 mFlinger->mFrameTracer->traceTimestamp(layerId, bufferId, frameNumber, latchTime,
724 FrameTracer::FrameEvent::LATCH);
Marissa Wall61c58622018-07-18 10:12:20 -0700725
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +0000726 auto& bufferSurfaceFrame = mDrawingState.bufferSurfaceFrameTX;
727 if (bufferSurfaceFrame != nullptr &&
728 bufferSurfaceFrame->getPresentState() != PresentState::Presented) {
729 // Update only if the bufferSurfaceFrame wasn't already presented. A Presented
730 // bufferSurfaceFrame could be seen here if a pending state was applied successfully and we
731 // are processing the next state.
732 addSurfaceFramePresentedForBuffer(bufferSurfaceFrame,
Ady Abraham6c1b7ac2021-03-31 16:56:03 -0700733 mDrawingState.acquireFenceTime->getSignalTime(),
734 latchTime);
Robert Carr6a160312021-05-17 12:08:20 -0700735 mDrawingState.bufferSurfaceFrameTX.reset();
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +0000736 }
737
Vishnu Nairfc46c1e2021-04-21 08:31:32 -0700738 std::deque<sp<CallbackHandle>> remainingHandles;
739 mFlinger->getTransactionCallbackInvoker()
Robert Carr3d1047b2021-09-20 18:22:32 -0700740 .addOnCommitCallbackHandles(mDrawingState.callbackHandles, remainingHandles);
Vishnu Nairfc46c1e2021-04-21 08:31:32 -0700741 mDrawingState.callbackHandles = remainingHandles;
742
Robert Carr6a160312021-05-17 12:08:20 -0700743 mDrawingStateModified = false;
Marissa Wall16c112d2019-03-20 13:21:13 -0700744
Marissa Wall61c58622018-07-18 10:12:20 -0700745 return NO_ERROR;
746}
747
748status_t BufferStateLayer::updateActiveBuffer() {
749 const State& s(getDrawingState());
750
751 if (s.buffer == nullptr) {
752 return BAD_VALUE;
753 }
chaviwdf3c5e82021-01-07 13:00:37 -0800754
Alec Mouria90a5702021-04-16 16:36:21 +0000755 if (!mBufferInfo.mBuffer || s.buffer->getBuffer() != mBufferInfo.mBuffer->getBuffer()) {
chaviwdf3c5e82021-01-07 13:00:37 -0800756 decrementPendingBufferCount();
757 }
Marissa Wall61c58622018-07-18 10:12:20 -0700758
Vishnu Nair4ba0c2e2021-06-24 11:27:17 -0700759 mPreviousReleaseCallbackId = {getCurrentBufferId(), mBufferInfo.mFrameNumber};
chaviwd62d3062019-09-04 14:48:02 -0700760 mBufferInfo.mBuffer = s.buffer;
761 mBufferInfo.mFence = s.acquireFence;
Vishnu Nair4ba0c2e2021-06-24 11:27:17 -0700762 mBufferInfo.mFrameNumber = s.frameNumber;
Marissa Wall61c58622018-07-18 10:12:20 -0700763
764 return NO_ERROR;
765}
766
Valerie Haubf784642020-01-29 07:25:23 -0800767status_t BufferStateLayer::updateFrameNumber(nsecs_t latchTime) {
Marissa Wall61c58622018-07-18 10:12:20 -0700768 // TODO(marissaw): support frame history events
Mikael Pessa2e1608f2019-07-19 11:25:35 -0700769 mPreviousFrameNumber = mCurrentFrameNumber;
Valerie Hau134651a2020-01-28 16:21:22 -0800770 mCurrentFrameNumber = mDrawingState.frameNumber;
Valerie Haubf784642020-01-29 07:25:23 -0800771 {
772 Mutex::Autolock lock(mFrameEventHistoryMutex);
773 mFrameEventHistory.addLatch(mCurrentFrameNumber, latchTime);
774 }
Marissa Wall61c58622018-07-18 10:12:20 -0700775 return NO_ERROR;
776}
777
Marissa Wall947d34e2019-03-29 14:03:53 -0700778void BufferStateLayer::HwcSlotGenerator::bufferErased(const client_cache_t& clientCacheId) {
779 std::lock_guard lock(mMutex);
780 if (!clientCacheId.isValid()) {
781 ALOGE("invalid process, failed to erase buffer");
782 return;
783 }
784 eraseBufferLocked(clientCacheId);
785}
786
rnleeed20fa42021-08-10 18:00:03 -0700787int BufferStateLayer::HwcSlotGenerator::getHwcCacheSlot(const client_cache_t& clientCacheId) {
Marissa Wall947d34e2019-03-29 14:03:53 -0700788 std::lock_guard<std::mutex> lock(mMutex);
789 auto itr = mCachedBuffers.find(clientCacheId);
790 if (itr == mCachedBuffers.end()) {
791 return addCachedBuffer(clientCacheId);
792 }
793 auto& [hwcCacheSlot, counter] = itr->second;
794 counter = mCounter++;
795 return hwcCacheSlot;
796}
797
rnleeed20fa42021-08-10 18:00:03 -0700798int BufferStateLayer::HwcSlotGenerator::addCachedBuffer(const client_cache_t& clientCacheId)
Marissa Wall947d34e2019-03-29 14:03:53 -0700799 REQUIRES(mMutex) {
800 if (!clientCacheId.isValid()) {
801 ALOGE("invalid process, returning invalid slot");
802 return BufferQueue::INVALID_BUFFER_SLOT;
803 }
804
805 ClientCache::getInstance().registerErasedRecipient(clientCacheId, wp<ErasedRecipient>(this));
806
rnleeed20fa42021-08-10 18:00:03 -0700807 int hwcCacheSlot = getFreeHwcCacheSlot();
Marissa Wall947d34e2019-03-29 14:03:53 -0700808 mCachedBuffers[clientCacheId] = {hwcCacheSlot, mCounter++};
809 return hwcCacheSlot;
810}
811
rnleeed20fa42021-08-10 18:00:03 -0700812int BufferStateLayer::HwcSlotGenerator::getFreeHwcCacheSlot() REQUIRES(mMutex) {
Marissa Wall947d34e2019-03-29 14:03:53 -0700813 if (mFreeHwcCacheSlots.empty()) {
814 evictLeastRecentlyUsed();
815 }
816
rnleeed20fa42021-08-10 18:00:03 -0700817 int hwcCacheSlot = mFreeHwcCacheSlots.top();
Marissa Wall947d34e2019-03-29 14:03:53 -0700818 mFreeHwcCacheSlots.pop();
819 return hwcCacheSlot;
820}
821
822void BufferStateLayer::HwcSlotGenerator::evictLeastRecentlyUsed() REQUIRES(mMutex) {
823 uint64_t minCounter = UINT_MAX;
824 client_cache_t minClientCacheId = {};
825 for (const auto& [clientCacheId, slotCounter] : mCachedBuffers) {
826 const auto& [hwcCacheSlot, counter] = slotCounter;
827 if (counter < minCounter) {
828 minCounter = counter;
829 minClientCacheId = clientCacheId;
830 }
831 }
832 eraseBufferLocked(minClientCacheId);
833
834 ClientCache::getInstance().unregisterErasedRecipient(minClientCacheId, this);
835}
836
837void BufferStateLayer::HwcSlotGenerator::eraseBufferLocked(const client_cache_t& clientCacheId)
838 REQUIRES(mMutex) {
839 auto itr = mCachedBuffers.find(clientCacheId);
840 if (itr == mCachedBuffers.end()) {
841 return;
842 }
843 auto& [hwcCacheSlot, counter] = itr->second;
844
845 // TODO send to hwc cache and resources
846
847 mFreeHwcCacheSlots.push(hwcCacheSlot);
848 mCachedBuffers.erase(clientCacheId);
849}
chaviw4244e032019-09-04 11:27:49 -0700850
851void BufferStateLayer::gatherBufferInfo() {
chaviwdebadb82020-03-26 14:57:24 -0700852 BufferLayer::gatherBufferInfo();
chaviw4244e032019-09-04 11:27:49 -0700853
chaviwdebadb82020-03-26 14:57:24 -0700854 const State& s(getDrawingState());
chaviw4244e032019-09-04 11:27:49 -0700855 mBufferInfo.mDesiredPresentTime = s.desiredPresentTime;
856 mBufferInfo.mFenceTime = std::make_shared<FenceTime>(s.acquireFence);
857 mBufferInfo.mFence = s.acquireFence;
chaviw766c9c52021-02-10 17:36:47 -0800858 mBufferInfo.mTransform = s.bufferTransform;
Robert Carr167bdde2021-07-28 11:26:51 -0700859 auto lastDataspace = mBufferInfo.mDataspace;
chaviw4244e032019-09-04 11:27:49 -0700860 mBufferInfo.mDataspace = translateDataspace(s.dataspace);
Robert Carr167bdde2021-07-28 11:26:51 -0700861 if (lastDataspace != mBufferInfo.mDataspace) {
862 mFlinger->mSomeDataspaceChanged = true;
863 }
Vishnu Nair5cc9ac02021-04-19 13:23:38 -0700864 mBufferInfo.mCrop = computeBufferCrop(s);
chaviw4244e032019-09-04 11:27:49 -0700865 mBufferInfo.mScaleMode = NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW;
866 mBufferInfo.mSurfaceDamage = s.surfaceDamageRegion;
867 mBufferInfo.mHdrMetadata = s.hdrMetadata;
868 mBufferInfo.mApi = s.api;
chaviw4244e032019-09-04 11:27:49 -0700869 mBufferInfo.mTransformToDisplayInverse = s.transformToDisplayInverse;
chaviwf83ce182019-09-12 14:43:08 -0700870 mBufferInfo.mBufferSlot = mHwcSlotGenerator->getHwcCacheSlot(s.clientCacheId);
chaviw4244e032019-09-04 11:27:49 -0700871}
872
Robert Carr916b0362020-10-06 13:53:03 -0700873uint32_t BufferStateLayer::getEffectiveScalingMode() const {
874 return NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW;
875}
876
Vishnu Nair5cc9ac02021-04-19 13:23:38 -0700877Rect BufferStateLayer::computeBufferCrop(const State& s) {
chaviwf3f40fe2021-04-27 15:54:02 -0500878 if (s.buffer && !s.bufferCrop.isEmpty()) {
879 Rect bufferCrop;
880 s.buffer->getBuffer()->getBounds().intersect(s.bufferCrop, &bufferCrop);
881 return bufferCrop;
882 } else if (s.buffer) {
Alec Mouria90a5702021-04-16 16:36:21 +0000883 return s.buffer->getBuffer()->getBounds();
chaviwf3f40fe2021-04-27 15:54:02 -0500884 } else {
885 return s.bufferCrop;
chaviw4244e032019-09-04 11:27:49 -0700886 }
chaviw4244e032019-09-04 11:27:49 -0700887}
888
chaviwb4c6e582019-08-16 14:35:07 -0700889sp<Layer> BufferStateLayer::createClone() {
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700890 LayerCreationArgs args(mFlinger.get(), nullptr, mName + " (Mirror)", 0, 0, 0, LayerMetadata());
chaviwb4c6e582019-08-16 14:35:07 -0700891 args.textureName = mTextureName;
Lloyd Pique1c3a5eb2019-10-03 13:07:08 -0700892 sp<BufferStateLayer> layer = mFlinger->getFactory().createBufferStateLayer(args);
chaviwb4c6e582019-08-16 14:35:07 -0700893 layer->mHwcSlotGenerator = mHwcSlotGenerator;
894 layer->setInitialValuesForClone(this);
895 return layer;
896}
Valerie Hau92bf5482020-02-10 09:49:08 -0800897
Vishnu Naire7f79c52020-10-29 14:45:03 -0700898bool BufferStateLayer::bufferNeedsFiltering() const {
899 const State& s(getDrawingState());
900 if (!s.buffer) {
901 return false;
902 }
903
rnleeed20fa42021-08-10 18:00:03 -0700904 int32_t bufferWidth = s.buffer->getBuffer()->width;
905 int32_t bufferHeight = s.buffer->getBuffer()->height;
Vishnu Naire7f79c52020-10-29 14:45:03 -0700906
907 // Undo any transformations on the buffer and return the result.
chaviw766c9c52021-02-10 17:36:47 -0800908 if (s.bufferTransform & ui::Transform::ROT_90) {
Vishnu Naire7f79c52020-10-29 14:45:03 -0700909 std::swap(bufferWidth, bufferHeight);
910 }
911
912 if (s.transformToDisplayInverse) {
913 uint32_t invTransform = DisplayDevice::getPrimaryDisplayRotationFlags();
914 if (invTransform & ui::Transform::ROT_90) {
915 std::swap(bufferWidth, bufferHeight);
916 }
917 }
918
919 const Rect layerSize{getBounds()};
920 return layerSize.width() != bufferWidth || layerSize.height() != bufferHeight;
921}
Robert Carr7121caf2020-12-15 13:07:32 -0800922
Robert Carr7121caf2020-12-15 13:07:32 -0800923void BufferStateLayer::decrementPendingBufferCount() {
Vishnu Nair8eda69e2021-02-26 10:42:10 -0800924 int32_t pendingBuffers = --mPendingBufferTransactions;
925 tracePendingBufferCount(pendingBuffers);
Robert Carr7121caf2020-12-15 13:07:32 -0800926}
927
Vishnu Nair8eda69e2021-02-26 10:42:10 -0800928void BufferStateLayer::tracePendingBufferCount(int32_t pendingBuffers) {
929 ATRACE_INT(mBlastTransactionName.c_str(), pendingBuffers);
Robert Carr7121caf2020-12-15 13:07:32 -0800930}
931
Robert Carr7121caf2020-12-15 13:07:32 -0800932
chaviw39d01472021-04-08 14:26:24 -0500933/*
934 * We don't want to send the layer's transform to input, but rather the
935 * parent's transform. This is because BufferStateLayer's transform is
936 * information about how the buffer is placed on screen. The parent's
937 * transform makes more sense to send since it's information about how the
938 * layer is placed on screen. This transform is used by input to determine
939 * how to go from screen space back to window space.
940 */
941ui::Transform BufferStateLayer::getInputTransform() const {
Robert Carrcbdb79a2021-06-11 16:10:45 -0700942 auto parent = mDrawingParent;
chaviw39d01472021-04-08 14:26:24 -0500943 if (parent == nullptr) {
944 return ui::Transform();
945 }
946
947 return parent->getTransform();
948}
949
950/**
951 * Similar to getInputTransform, we need to update the bounds to include the transform.
952 * This is because bounds for BSL doesn't include buffer transform, where the input assumes
953 * that's already included.
954 */
955Rect BufferStateLayer::getInputBounds() const {
956 Rect bufferBounds = getCroppedBufferSize(getDrawingState());
957 if (mDrawingState.transform.getType() == ui::Transform::IDENTITY || !bufferBounds.isValid()) {
958 return bufferBounds;
959 }
960 return mDrawingState.transform.transform(bufferBounds);
961}
962
Marissa Wall61c58622018-07-18 10:12:20 -0700963} // namespace android