blob: 6cecd8956a09fc5b642aff369ed4f11ceab65e56 [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.
Vishnu Nair3bb11d02021-11-26 09:24:11 -080064 if (mBufferInfo.mBuffer != nullptr) {
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 Qi59a9f502021-10-12 18:53:23 +000076void 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 }
Valerie Haubf784642020-01-29 07:25:23 -0800115
Mikael Pessa2e1608f2019-07-19 11:25:35 -0700116 // Prevent tracing the same release multiple times.
117 if (mPreviousFrameNumber != mPreviousReleasedFrameNumber) {
Mikael Pessa2e1608f2019-07-19 11:25:35 -0700118 mPreviousReleasedFrameNumber = mPreviousFrameNumber;
119 }
Sally Qi59a9f502021-10-12 18:53:23 +0000120
121 if (ch != nullptr) {
122 ch->previousReleaseCallbackId = mPreviousReleaseCallbackId;
123 ch->previousReleaseFences.emplace_back(futureRenderEngineResult);
124 ch->name = mName;
125 }
Marissa Wall61c58622018-07-18 10:12:20 -0700126}
127
Jorim Jaggi9c03b502020-11-24 23:51:31 +0100128void BufferStateLayer::onSurfaceFrameCreated(
129 const std::shared_ptr<frametimeline::SurfaceFrame>& surfaceFrame) {
Adithya Srinivasand17c7da2021-03-05 20:43:32 +0000130 while (mPendingJankClassifications.size() >= kPendingClassificationMaxSurfaceFrames) {
131 // Too many SurfaceFrames pending classification. The front of the deque is probably not
132 // tracked by FrameTimeline and will never be presented. This will only result in a memory
133 // leak.
134 ALOGW("Removing the front of pending jank deque from layer - %s to prevent memory leak",
135 mName.c_str());
Adithya Srinivasan785addd2021-03-09 00:38:00 +0000136 std::string miniDump = mPendingJankClassifications.front()->miniDump();
137 ALOGD("Head SurfaceFrame mini dump\n%s", miniDump.c_str());
Adithya Srinivasand17c7da2021-03-05 20:43:32 +0000138 mPendingJankClassifications.pop_front();
139 }
Jorim Jaggi9c03b502020-11-24 23:51:31 +0100140 mPendingJankClassifications.emplace_back(surfaceFrame);
141}
142
Valerie Haubf784642020-01-29 07:25:23 -0800143void BufferStateLayer::releasePendingBuffer(nsecs_t dequeueReadyTime) {
Valerie Hau32cdc1f2019-10-21 14:45:54 -0700144 for (const auto& handle : mDrawingState.callbackHandles) {
145 handle->transformHint = mTransformHint;
Valerie Hau871d6352020-01-29 08:44:02 -0800146 handle->dequeueReadyTime = dequeueReadyTime;
Ady Abraham899dcdb2021-06-15 16:56:21 -0700147 handle->currentMaxAcquiredBufferCount =
148 mFlinger->getMaxAcquiredBufferCountForCurrentRefreshRate(mOwnerUid);
Valerie Hau32cdc1f2019-10-21 14:45:54 -0700149 }
150
Vishnu Nair1506b182021-02-22 14:35:15 -0800151 for (auto& handle : mDrawingState.callbackHandles) {
chaviw0b06a8d2021-08-06 11:49:08 -0500152 if (handle->releasePreviousBuffer &&
153 mDrawingState.releaseBufferEndpoint == handle->listener) {
Vishnu Nair4ba0c2e2021-06-24 11:27:17 -0700154 handle->previousReleaseCallbackId = mPreviousReleaseCallbackId;
Vishnu Nair1506b182021-02-22 14:35:15 -0800155 break;
156 }
157 }
158
Jorim Jaggi9c03b502020-11-24 23:51:31 +0100159 std::vector<JankData> jankData;
160 jankData.reserve(mPendingJankClassifications.size());
161 while (!mPendingJankClassifications.empty()
162 && mPendingJankClassifications.front()->getJankType()) {
163 std::shared_ptr<frametimeline::SurfaceFrame> surfaceFrame =
164 mPendingJankClassifications.front();
165 mPendingJankClassifications.pop_front();
166 jankData.emplace_back(
167 JankData(surfaceFrame->getToken(), surfaceFrame->getJankType().value()));
168 }
169
Robert Carr3d1047b2021-09-20 18:22:32 -0700170 mFlinger->getTransactionCallbackInvoker().addCallbackHandles(
Jorim Jaggi9c03b502020-11-24 23:51:31 +0100171 mDrawingState.callbackHandles, jankData);
Marissa Wall5a68a772018-12-22 17:43:42 -0800172
Sally Qi59a9f502021-10-12 18:53:23 +0000173 sp<Fence> releaseFence = Fence::NO_FENCE;
174 for (auto& handle : mDrawingState.callbackHandles) {
175 if (handle->releasePreviousBuffer &&
176 mDrawingState.releaseBufferEndpoint == handle->listener) {
177 releaseFence =
178 handle->previousReleaseFence ? handle->previousReleaseFence : Fence::NO_FENCE;
179 break;
180 }
181 }
182
Marissa Wall5a68a772018-12-22 17:43:42 -0800183 mDrawingState.callbackHandles = {};
Valerie Haubf784642020-01-29 07:25:23 -0800184
Valerie Haubf784642020-01-29 07:25:23 -0800185 std::shared_ptr<FenceTime> releaseFenceTime = std::make_shared<FenceTime>(releaseFence);
186 {
187 Mutex::Autolock lock(mFrameEventHistoryMutex);
188 if (mPreviousFrameNumber != 0) {
189 mFrameEventHistory.addRelease(mPreviousFrameNumber, dequeueReadyTime,
190 std::move(releaseFenceTime));
191 }
192 }
Marissa Wall61c58622018-07-18 10:12:20 -0700193}
194
Valerie Hau871d6352020-01-29 08:44:02 -0800195void BufferStateLayer::finalizeFrameEventHistory(const std::shared_ptr<FenceTime>& glDoneFence,
196 const CompositorTiming& compositorTiming) {
197 for (const auto& handle : mDrawingState.callbackHandles) {
198 handle->gpuCompositionDoneFence = glDoneFence;
199 handle->compositorTiming = compositorTiming;
200 }
201}
202
Marissa Walle2ffb422018-10-12 11:33:52 -0700203bool BufferStateLayer::willPresentCurrentTransaction() const {
204 // Returns true if the most recent Transaction applied to CurrentState will be presented.
Robert Carr321e83c2019-08-19 15:49:30 -0700205 return (getSidebandStreamChanged() || getAutoRefresh() ||
Robert Carr6a160312021-05-17 12:08:20 -0700206 (mDrawingState.modified &&
207 (mDrawingState.buffer != nullptr || mDrawingState.bgColorLayer != nullptr)));
Marissa Wall61c58622018-07-18 10:12:20 -0700208}
209
Chavi Weingartena5aedbd2021-04-09 13:37:33 +0000210Rect BufferStateLayer::getCrop(const Layer::State& s) const {
211 return s.crop;
Marissa Wall61c58622018-07-18 10:12:20 -0700212}
213
214bool BufferStateLayer::setTransform(uint32_t transform) {
Robert Carr6a160312021-05-17 12:08:20 -0700215 if (mDrawingState.bufferTransform == transform) return false;
216 mDrawingState.bufferTransform = transform;
217 mDrawingState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700218 setTransactionFlags(eTransactionNeeded);
219 return true;
220}
221
222bool BufferStateLayer::setTransformToDisplayInverse(bool transformToDisplayInverse) {
Robert Carr6a160312021-05-17 12:08:20 -0700223 if (mDrawingState.transformToDisplayInverse == transformToDisplayInverse) return false;
224 mDrawingState.sequence++;
225 mDrawingState.transformToDisplayInverse = transformToDisplayInverse;
226 mDrawingState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700227 setTransactionFlags(eTransactionNeeded);
228 return true;
229}
230
231bool BufferStateLayer::setCrop(const Rect& crop) {
Robert Carr6a160312021-05-17 12:08:20 -0700232 if (mDrawingState.crop == crop) return false;
233 mDrawingState.sequence++;
234 mDrawingState.crop = crop;
Marissa Wall290ad082019-03-06 13:23:47 -0800235
Robert Carr6a160312021-05-17 12:08:20 -0700236 mDrawingState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700237 setTransactionFlags(eTransactionNeeded);
238 return true;
239}
240
chaviwf3f40fe2021-04-27 15:54:02 -0500241bool BufferStateLayer::setBufferCrop(const Rect& bufferCrop) {
Robert Carr6a160312021-05-17 12:08:20 -0700242 if (mDrawingState.bufferCrop == bufferCrop) return false;
chaviwf3f40fe2021-04-27 15:54:02 -0500243
Robert Carr6a160312021-05-17 12:08:20 -0700244 mDrawingState.sequence++;
245 mDrawingState.bufferCrop = bufferCrop;
chaviwf3f40fe2021-04-27 15:54:02 -0500246
Robert Carr6a160312021-05-17 12:08:20 -0700247 mDrawingState.modified = true;
chaviwf3f40fe2021-04-27 15:54:02 -0500248 setTransactionFlags(eTransactionNeeded);
249 return true;
250}
251
Vishnu Nair6bdec7d2021-05-10 15:01:13 -0700252bool BufferStateLayer::setDestinationFrame(const Rect& destinationFrame) {
Robert Carr6a160312021-05-17 12:08:20 -0700253 if (mDrawingState.destinationFrame == destinationFrame) return false;
Vishnu Nair6bdec7d2021-05-10 15:01:13 -0700254
Robert Carr6a160312021-05-17 12:08:20 -0700255 mDrawingState.sequence++;
256 mDrawingState.destinationFrame = destinationFrame;
Vishnu Nair6bdec7d2021-05-10 15:01:13 -0700257
Robert Carr6a160312021-05-17 12:08:20 -0700258 mDrawingState.modified = true;
Vishnu Nair6bdec7d2021-05-10 15:01:13 -0700259 setTransactionFlags(eTransactionNeeded);
260 return true;
261}
262
Robert Carr6a160312021-05-17 12:08:20 -0700263static bool assignTransform(ui::Transform* dst, ui::Transform& from) {
264 if (*dst == from) {
265 return false;
266 }
267 *dst = from;
268 return true;
269}
270
Vishnu Nair6bdec7d2021-05-10 15:01:13 -0700271// Translate destination frame into scale and position. If a destination frame is not set, use the
272// provided scale and position
Robert Carr6a160312021-05-17 12:08:20 -0700273bool BufferStateLayer::updateGeometry() {
274 if (mDrawingState.destinationFrame.isEmpty()) {
Vishnu Nair6bdec7d2021-05-10 15:01:13 -0700275 // If destination frame is not set, use the requested transform set via
276 // BufferStateLayer::setPosition and BufferStateLayer::setMatrix.
Robert Carr6a160312021-05-17 12:08:20 -0700277 return assignTransform(&mDrawingState.transform, mRequestedTransform);
Vishnu Nair6bdec7d2021-05-10 15:01:13 -0700278 }
279
Robert Carr6a160312021-05-17 12:08:20 -0700280 Rect destRect = mDrawingState.destinationFrame;
Vishnu Nair6bdec7d2021-05-10 15:01:13 -0700281 int32_t destW = destRect.width();
282 int32_t destH = destRect.height();
283 if (destRect.left < 0) {
284 destRect.left = 0;
285 destRect.right = destW;
286 }
287 if (destRect.top < 0) {
288 destRect.top = 0;
289 destRect.bottom = destH;
290 }
291
Robert Carr6a160312021-05-17 12:08:20 -0700292 if (!mDrawingState.buffer) {
Vishnu Nair6bdec7d2021-05-10 15:01:13 -0700293 ui::Transform t;
294 t.set(destRect.left, destRect.top);
Robert Carr6a160312021-05-17 12:08:20 -0700295 return assignTransform(&mDrawingState.transform, t);
Vishnu Nair6bdec7d2021-05-10 15:01:13 -0700296 }
297
Vishnu Nairdbbe3852022-01-12 20:22:11 -0800298 uint32_t bufferWidth = mDrawingState.buffer->getWidth();
299 uint32_t bufferHeight = mDrawingState.buffer->getHeight();
Vishnu Nair6bdec7d2021-05-10 15:01:13 -0700300 // Undo any transformations on the buffer.
Robert Carr6a160312021-05-17 12:08:20 -0700301 if (mDrawingState.bufferTransform & ui::Transform::ROT_90) {
Vishnu Nair6bdec7d2021-05-10 15:01:13 -0700302 std::swap(bufferWidth, bufferHeight);
303 }
304 uint32_t invTransform = DisplayDevice::getPrimaryDisplayRotationFlags();
Robert Carr6a160312021-05-17 12:08:20 -0700305 if (mDrawingState.transformToDisplayInverse) {
Vishnu Nair6bdec7d2021-05-10 15:01:13 -0700306 if (invTransform & ui::Transform::ROT_90) {
307 std::swap(bufferWidth, bufferHeight);
308 }
309 }
310
311 float sx = destW / static_cast<float>(bufferWidth);
312 float sy = destH / static_cast<float>(bufferHeight);
313 ui::Transform t;
314 t.set(sx, 0, 0, sy);
315 t.set(destRect.left, destRect.top);
Robert Carr6a160312021-05-17 12:08:20 -0700316 return assignTransform(&mDrawingState.transform, t);
Vishnu Nair6bdec7d2021-05-10 15:01:13 -0700317}
318
Chavi Weingartena5aedbd2021-04-09 13:37:33 +0000319bool BufferStateLayer::setMatrix(const layer_state_t::matrix22_t& matrix,
320 bool allowNonRectPreservingTransforms) {
Vishnu Nair6bdec7d2021-05-10 15:01:13 -0700321 if (mRequestedTransform.dsdx() == matrix.dsdx && mRequestedTransform.dtdy() == matrix.dtdy &&
322 mRequestedTransform.dtdx() == matrix.dtdx && mRequestedTransform.dsdy() == matrix.dsdy) {
Marissa Wall861616d2018-10-22 12:52:23 -0700323 return false;
324 }
325
Chavi Weingartena5aedbd2021-04-09 13:37:33 +0000326 ui::Transform t;
327 t.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
328
329 if (!allowNonRectPreservingTransforms && !t.preserveRects()) {
330 ALOGW("Attempt to set rotation matrix without permission ACCESS_SURFACE_FLINGER nor "
331 "ROTATE_SURFACE_FLINGER ignored");
332 return false;
Marissa Wall861616d2018-10-22 12:52:23 -0700333 }
Chavi Weingartena5aedbd2021-04-09 13:37:33 +0000334
Vishnu Nair6bdec7d2021-05-10 15:01:13 -0700335 mRequestedTransform.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
chaviw9a93ea62021-03-11 16:44:42 -0600336
Robert Carr6a160312021-05-17 12:08:20 -0700337 mDrawingState.sequence++;
338 mDrawingState.modified = true;
chaviw9a93ea62021-03-11 16:44:42 -0600339 setTransactionFlags(eTransactionNeeded);
Chavi Weingartena5aedbd2021-04-09 13:37:33 +0000340
341 return true;
342}
343
344bool BufferStateLayer::setPosition(float x, float y) {
Vishnu Nair6bdec7d2021-05-10 15:01:13 -0700345 if (mRequestedTransform.tx() == x && mRequestedTransform.ty() == y) {
Chavi Weingartena5aedbd2021-04-09 13:37:33 +0000346 return false;
347 }
348
Vishnu Nair6bdec7d2021-05-10 15:01:13 -0700349 mRequestedTransform.set(x, y);
Chavi Weingartena5aedbd2021-04-09 13:37:33 +0000350
Robert Carr6a160312021-05-17 12:08:20 -0700351 mDrawingState.sequence++;
352 mDrawingState.modified = true;
Chavi Weingartena5aedbd2021-04-09 13:37:33 +0000353 setTransactionFlags(eTransactionNeeded);
354
Marissa Wall861616d2018-10-22 12:52:23 -0700355 return true;
356}
357
Valerie Hau871d6352020-01-29 08:44:02 -0800358bool BufferStateLayer::addFrameEvent(const sp<Fence>& acquireFence, nsecs_t postedTime,
359 nsecs_t desiredPresentTime) {
Valerie Haubf784642020-01-29 07:25:23 -0800360 Mutex::Autolock lock(mFrameEventHistoryMutex);
361 mAcquireTimeline.updateSignalTimes();
362 std::shared_ptr<FenceTime> acquireFenceTime =
363 std::make_shared<FenceTime>((acquireFence ? acquireFence : Fence::NO_FENCE));
Robert Carr6a160312021-05-17 12:08:20 -0700364 NewFrameEventsEntry newTimestamps = {mDrawingState.frameNumber, postedTime, desiredPresentTime,
Valerie Haubf784642020-01-29 07:25:23 -0800365 acquireFenceTime};
Valerie Hau871d6352020-01-29 08:44:02 -0800366 mFrameEventHistory.setProducerWantsEvents();
Valerie Haubf784642020-01-29 07:25:23 -0800367 mFrameEventHistory.addQueue(newTimestamps);
368 return true;
369}
370
Vishnu Nairdbbe3852022-01-12 20:22:11 -0800371bool BufferStateLayer::setBuffer(std::shared_ptr<renderengine::ExternalTexture>& buffer,
372 const BufferData& bufferData, nsecs_t postTime,
Alec Mouria90a5702021-04-16 16:36:21 +0000373 nsecs_t desiredPresentTime, bool isAutoTimestamp,
chaviwba4320c2021-09-15 15:20:53 -0500374 std::optional<nsecs_t> dequeueTime,
375 const FrameTimelineInfo& info) {
Alex Chaucf6b4b42021-12-07 10:48:52 +0000376 ATRACE_CALL();
Robert Carr0c1966e2020-10-19 12:12:08 -0700377
chaviwba4320c2021-09-15 15:20:53 -0500378 if (!buffer) {
379 return false;
380 }
381
382 const bool frameNumberChanged =
383 bufferData.flags.test(BufferData::BufferDataChange::frameNumberChanged);
384 const uint64_t frameNumber =
385 frameNumberChanged ? bufferData.frameNumber : mDrawingState.frameNumber + 1;
386
Robert Carr6a160312021-05-17 12:08:20 -0700387 if (mDrawingState.buffer) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700388 mReleasePreviousBuffer = true;
Vishnu Nairdbbe3852022-01-12 20:22:11 -0800389 if (!mBufferInfo.mBuffer ||
390 (!mDrawingState.buffer->hasSameBuffer(*mBufferInfo.mBuffer) ||
391 mDrawingState.frameNumber != mBufferInfo.mFrameNumber)) {
Robert Carr6a160312021-05-17 12:08:20 -0700392 // If mDrawingState has a buffer, and we are about to update again
Robert Carr7121caf2020-12-15 13:07:32 -0800393 // before swapping to drawing state, then the first buffer will be
Vishnu Nair1506b182021-02-22 14:35:15 -0800394 // dropped and we should decrement the pending buffer count and
395 // call any release buffer callbacks if set.
Robert Carr6a160312021-05-17 12:08:20 -0700396 callReleaseBufferCallback(mDrawingState.releaseBufferListener,
Vishnu Nair4ba0c2e2021-06-24 11:27:17 -0700397 mDrawingState.buffer->getBuffer(), mDrawingState.frameNumber,
chaviw69058fb2021-09-27 09:37:30 -0500398 mDrawingState.acquireFence,
Ady Abraham899dcdb2021-06-15 16:56:21 -0700399 mFlinger->getMaxAcquiredBufferCountForCurrentRefreshRate(
400 mOwnerUid));
Robert Carr7121caf2020-12-15 13:07:32 -0800401 decrementPendingBufferCount();
Robert Carr6a160312021-05-17 12:08:20 -0700402 if (mDrawingState.bufferSurfaceFrameTX != nullptr &&
403 mDrawingState.bufferSurfaceFrameTX->getPresentState() != PresentState::Presented) {
404 addSurfaceFrameDroppedForBuffer(mDrawingState.bufferSurfaceFrameTX);
405 mDrawingState.bufferSurfaceFrameTX.reset();
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +0000406 }
Robert Carrccab4242021-09-28 16:53:03 -0700407 } else if (mLastClientCompositionFence != nullptr) {
408 callReleaseBufferCallback(mDrawingState.releaseBufferListener,
409 mDrawingState.buffer->getBuffer(), mDrawingState.frameNumber,
chaviw69058fb2021-09-27 09:37:30 -0500410 mLastClientCompositionFence,
Robert Carrccab4242021-09-28 16:53:03 -0700411 mFlinger->getMaxAcquiredBufferCountForCurrentRefreshRate(
chaviw69058fb2021-09-27 09:37:30 -0500412 mOwnerUid));
Robert Carrccab4242021-09-28 16:53:03 -0700413 mLastClientCompositionFence = nullptr;
Robert Carr7121caf2020-12-15 13:07:32 -0800414 }
Marissa Wallfda30bb2018-10-12 11:34:28 -0700415 }
Robert Carr6a160312021-05-17 12:08:20 -0700416
417 mDrawingState.frameNumber = frameNumber;
chaviwba4320c2021-09-15 15:20:53 -0500418 mDrawingState.releaseBufferListener = bufferData.releaseBufferListener;
Vishnu Nairdbbe3852022-01-12 20:22:11 -0800419 mDrawingState.buffer = std::move(buffer);
chaviwba4320c2021-09-15 15:20:53 -0500420 mDrawingState.clientCacheId = bufferData.cachedBuffer;
421
422 mDrawingState.acquireFence = bufferData.flags.test(BufferData::BufferDataChange::fenceChanged)
423 ? bufferData.acquireFence
424 : Fence::NO_FENCE;
425 mDrawingState.acquireFenceTime = std::make_unique<FenceTime>(mDrawingState.acquireFence);
426 // The acquire fences of BufferStateLayers have already signaled before they are set
427 mCallbackHandleAcquireTime = mDrawingState.acquireFenceTime->getSignalTime();
428
Robert Carr6a160312021-05-17 12:08:20 -0700429 mDrawingState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700430 setTransactionFlags(eTransactionNeeded);
Ady Abraham09bd3922019-04-08 10:44:56 -0700431
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800432 const int32_t layerId = getSequence();
Robert Carr6a160312021-05-17 12:08:20 -0700433 mFlinger->mTimeStats->setPostTime(layerId, mDrawingState.frameNumber, getName().c_str(),
Adithya Srinivasan58069dc2021-06-04 20:37:02 +0000434 mOwnerUid, postTime, getGameMode());
Robert Carr6a160312021-05-17 12:08:20 -0700435 mDrawingState.desiredPresentTime = desiredPresentTime;
436 mDrawingState.isAutoTimestamp = isAutoTimestamp;
Ady Abraham09bd3922019-04-08 10:44:56 -0700437
Ady Abrahamb7f15562021-03-15 18:34:08 -0700438 const nsecs_t presentTime = [&] {
439 if (!isAutoTimestamp) return desiredPresentTime;
440
441 const auto prediction =
442 mFlinger->mFrameTimeline->getTokenManager()->getPredictionsForToken(info.vsyncId);
443 if (prediction.has_value()) return prediction->presentTime;
444
445 return static_cast<nsecs_t>(0);
446 }();
Dominik Laskowski068173d2021-08-11 17:22:59 -0700447
448 using LayerUpdateType = scheduler::LayerHistory::LayerUpdateType;
449 mFlinger->mScheduler->recordLayerHistory(this, presentTime, LayerUpdateType::Buffer);
Ady Abraham09bd3922019-04-08 10:44:56 -0700450
chaviwba4320c2021-09-15 15:20:53 -0500451 addFrameEvent(mDrawingState.acquireFence, postTime, isAutoTimestamp ? 0 : desiredPresentTime);
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +0000452
Adithya Srinivasan891004e2021-02-12 20:20:47 +0000453 setFrameTimelineVsyncForBufferTransaction(info, postTime);
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +0000454
Alec Mouria90a5702021-04-16 16:36:21 +0000455 if (buffer && dequeueTime && *dequeueTime != 0) {
Vishnu Nairdbbe3852022-01-12 20:22:11 -0800456 const uint64_t bufferId = buffer->getId();
Adithya Srinivasanb238cd52021-02-04 17:54:05 +0000457 mFlinger->mFrameTracer->traceNewLayer(layerId, getName().c_str());
458 mFlinger->mFrameTracer->traceTimestamp(layerId, bufferId, frameNumber, *dequeueTime,
459 FrameTracer::FrameEvent::DEQUEUE);
460 mFlinger->mFrameTracer->traceTimestamp(layerId, bufferId, frameNumber, postTime,
461 FrameTracer::FrameEvent::QUEUE);
462 }
Chavi Weingartena5aedbd2021-04-09 13:37:33 +0000463
Vishnu Nairdbbe3852022-01-12 20:22:11 -0800464 mDrawingState.width = mDrawingState.buffer->getWidth();
465 mDrawingState.height = mDrawingState.buffer->getHeight();
chaviwba4320c2021-09-15 15:20:53 -0500466 mDrawingState.releaseBufferEndpoint = bufferData.releaseBufferEndpoint;
Marissa Wall61c58622018-07-18 10:12:20 -0700467 return true;
468}
469
470bool BufferStateLayer::setDataspace(ui::Dataspace dataspace) {
Robert Carr6a160312021-05-17 12:08:20 -0700471 if (mDrawingState.dataspace == dataspace) return false;
472 mDrawingState.dataspace = dataspace;
473 mDrawingState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700474 setTransactionFlags(eTransactionNeeded);
475 return true;
476}
477
478bool BufferStateLayer::setHdrMetadata(const HdrMetadata& hdrMetadata) {
Robert Carr6a160312021-05-17 12:08:20 -0700479 if (mDrawingState.hdrMetadata == hdrMetadata) return false;
480 mDrawingState.hdrMetadata = hdrMetadata;
481 mDrawingState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700482 setTransactionFlags(eTransactionNeeded);
483 return true;
484}
485
486bool BufferStateLayer::setSurfaceDamageRegion(const Region& surfaceDamage) {
Robert Carr6a160312021-05-17 12:08:20 -0700487 mDrawingState.surfaceDamageRegion = surfaceDamage;
488 mDrawingState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700489 setTransactionFlags(eTransactionNeeded);
490 return true;
491}
492
493bool BufferStateLayer::setApi(int32_t api) {
Robert Carr6a160312021-05-17 12:08:20 -0700494 if (mDrawingState.api == api) return false;
495 mDrawingState.api = api;
496 mDrawingState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700497 setTransactionFlags(eTransactionNeeded);
498 return true;
499}
500
501bool BufferStateLayer::setSidebandStream(const sp<NativeHandle>& sidebandStream) {
Robert Carr6a160312021-05-17 12:08:20 -0700502 if (mDrawingState.sidebandStream == sidebandStream) return false;
Robert Carr3e2a2992021-06-11 13:42:55 -0700503
504 if (mDrawingState.sidebandStream != nullptr && sidebandStream == nullptr) {
505 mFlinger->mTunnelModeEnabledReporter->decrementTunnelModeCount();
506 } else if (sidebandStream != nullptr) {
507 mFlinger->mTunnelModeEnabledReporter->incrementTunnelModeCount();
508 }
509
Robert Carr6a160312021-05-17 12:08:20 -0700510 mDrawingState.sidebandStream = sidebandStream;
511 mDrawingState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700512 setTransactionFlags(eTransactionNeeded);
Marissa Wall61c58622018-07-18 10:12:20 -0700513 if (!mSidebandStreamChanged.exchange(true)) {
514 // mSidebandStreamChanged was false
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -0700515 mFlinger->onLayerUpdate();
Marissa Wall61c58622018-07-18 10:12:20 -0700516 }
517 return true;
518}
519
Marissa Walle2ffb422018-10-12 11:33:52 -0700520bool BufferStateLayer::setTransactionCompletedListeners(
Jiakai Zhanga5505cb2021-11-09 11:46:30 +0000521 const std::vector<sp<CallbackHandle>>& handles) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700522 // If there is no handle, we will not send a callback so reset mReleasePreviousBuffer and return
Jiakai Zhanga5505cb2021-11-09 11:46:30 +0000523 if (handles.empty()) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700524 mReleasePreviousBuffer = false;
Marissa Walle2ffb422018-10-12 11:33:52 -0700525 return false;
526 }
527
528 const bool willPresent = willPresentCurrentTransaction();
529
530 for (const auto& handle : handles) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700531 // If this transaction set a buffer on this layer, release its previous buffer
532 handle->releasePreviousBuffer = mReleasePreviousBuffer;
533
Marissa Walle2ffb422018-10-12 11:33:52 -0700534 // If this layer will be presented in this frame
535 if (willPresent) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700536 // If this transaction set an acquire fence on this layer, set its acquire time
537 handle->acquireTime = mCallbackHandleAcquireTime;
Robert Carr6a160312021-05-17 12:08:20 -0700538 handle->frameNumber = mDrawingState.frameNumber;
Marissa Wallfda30bb2018-10-12 11:34:28 -0700539
Marissa Walle2ffb422018-10-12 11:33:52 -0700540 // Store so latched time and release fence can be set
Robert Carr6a160312021-05-17 12:08:20 -0700541 mDrawingState.callbackHandles.push_back(handle);
Marissa Walle2ffb422018-10-12 11:33:52 -0700542
Jiakai Zhanga5505cb2021-11-09 11:46:30 +0000543 } else { // If this layer will NOT need to be relatched and presented this frame
Marissa Walle2ffb422018-10-12 11:33:52 -0700544 // Notify the transaction completed thread this handle is done
Jiakai Zhanga5505cb2021-11-09 11:46:30 +0000545 mFlinger->getTransactionCallbackInvoker().registerUnpresentedCallbackHandle(handle);
Marissa Walle2ffb422018-10-12 11:33:52 -0700546 }
547 }
548
Marissa Wallfda30bb2018-10-12 11:34:28 -0700549 mReleasePreviousBuffer = false;
550 mCallbackHandleAcquireTime = -1;
551
Marissa Walle2ffb422018-10-12 11:33:52 -0700552 return willPresent;
553}
554
Marissa Wall61c58622018-07-18 10:12:20 -0700555bool BufferStateLayer::setTransparentRegionHint(const Region& transparent) {
Vishnu Nair27e3ed52021-07-08 18:24:25 -0700556 mDrawingState.sequence++;
Robert Carr6a160312021-05-17 12:08:20 -0700557 mDrawingState.transparentRegionHint = transparent;
558 mDrawingState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700559 setTransactionFlags(eTransactionNeeded);
560 return true;
561}
562
rnleeed20fa42021-08-10 18:00:03 -0700563Rect BufferStateLayer::getBufferSize(const State& /*s*/) const {
Marissa Wall861616d2018-10-22 12:52:23 -0700564 // for buffer state layers we use the display frame size as the buffer size.
Marissa Wall61c58622018-07-18 10:12:20 -0700565
chaviw7e72caf2020-12-02 16:50:43 -0800566 if (mBufferInfo.mBuffer == nullptr) {
567 return Rect::INVALID_RECT;
568 }
569
Vishnu Nairdbbe3852022-01-12 20:22:11 -0800570 uint32_t bufWidth = mBufferInfo.mBuffer->getWidth();
571 uint32_t bufHeight = mBufferInfo.mBuffer->getHeight();
Vishnu Nair6bdec7d2021-05-10 15:01:13 -0700572
573 // Undo any transformations on the buffer and return the result.
574 if (mBufferInfo.mTransform & ui::Transform::ROT_90) {
575 std::swap(bufWidth, bufHeight);
576 }
577
578 if (getTransformToDisplayInverse()) {
579 uint32_t invTransform = DisplayDevice::getPrimaryDisplayRotationFlags();
580 if (invTransform & ui::Transform::ROT_90) {
581 std::swap(bufWidth, bufHeight);
Marissa Wall861616d2018-10-22 12:52:23 -0700582 }
583 }
584
rnleeed20fa42021-08-10 18:00:03 -0700585 return Rect(0, 0, static_cast<int32_t>(bufWidth), static_cast<int32_t>(bufHeight));
Marissa Wall61c58622018-07-18 10:12:20 -0700586}
Vishnu Nair4351ad52019-02-11 14:13:02 -0800587
588FloatRect BufferStateLayer::computeSourceBounds(const FloatRect& parentBounds) const {
Vishnu Nair6bdec7d2021-05-10 15:01:13 -0700589 if (mBufferInfo.mBuffer == nullptr) {
590 return parentBounds;
Vishnu Nair4351ad52019-02-11 14:13:02 -0800591 }
592
Vishnu Nair6bdec7d2021-05-10 15:01:13 -0700593 return getBufferSize(getDrawingState()).toFloatRect();
Vishnu Nair4351ad52019-02-11 14:13:02 -0800594}
595
Marissa Wall61c58622018-07-18 10:12:20 -0700596// -----------------------------------------------------------------------
597
598// -----------------------------------------------------------------------
599// Interface implementation for BufferLayer
600// -----------------------------------------------------------------------
601bool BufferStateLayer::fenceHasSignaled() const {
ramindani4d48f902021-09-20 21:07:45 +0000602 if (SurfaceFlinger::enableLatchUnsignaledConfig != LatchUnsignaledConfig::Disabled) {
Huihong Luo86c80e32021-06-16 15:41:07 -0700603 return true;
604 }
605
Alec Mouri91f6df32020-01-30 08:48:58 -0800606 const bool fenceSignaled =
607 getDrawingState().acquireFence->getStatus() == Fence::Status::Signaled;
608 if (!fenceSignaled) {
609 mFlinger->mTimeStats->incrementLatchSkipped(getSequence(),
610 TimeStats::LatchSkipReason::LateAcquire);
611 }
612
613 return fenceSignaled;
Marissa Wall61c58622018-07-18 10:12:20 -0700614}
615
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700616bool BufferStateLayer::framePresentTimeIsCurrent(nsecs_t expectedPresentTime) const {
Ady Abrahamcd1580c2019-04-29 15:40:03 -0700617 if (!hasFrameUpdate() || isRemovedFromCurrentState()) {
618 return true;
619 }
620
Robert Carr6a160312021-05-17 12:08:20 -0700621 return mDrawingState.isAutoTimestamp || mDrawingState.desiredPresentTime <= expectedPresentTime;
Ady Abrahamcd1580c2019-04-29 15:40:03 -0700622}
623
Valerie Hau871d6352020-01-29 08:44:02 -0800624bool BufferStateLayer::onPreComposition(nsecs_t refreshStartTime) {
625 for (const auto& handle : mDrawingState.callbackHandles) {
626 handle->refreshStartTime = refreshStartTime;
627 }
628 return BufferLayer::onPreComposition(refreshStartTime);
629}
630
Vishnu Naircf26a0a2020-11-13 12:56:20 -0800631void BufferStateLayer::setAutoRefresh(bool autoRefresh) {
Vishnu Nair86653e92021-11-03 17:19:36 -0700632 mDrawingState.autoRefresh = autoRefresh;
Marissa Wall61c58622018-07-18 10:12:20 -0700633}
634
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800635bool BufferStateLayer::latchSidebandStream(bool& recomputeVisibleRegions) {
baocheng suna663c2b2021-05-13 18:51:28 +0800636 // 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 +0800637 editCompositionState()->sidebandStreamHasFrame = hasFrameUpdate() && mSidebandStream.get();
baocheng suna663c2b2021-05-13 18:51:28 +0800638
baocheng sun9691b9c2021-08-03 19:27:06 +0800639 if (mSidebandStreamChanged.exchange(false)) {
Marissa Wall61c58622018-07-18 10:12:20 -0700640 const State& s(getDrawingState());
641 // mSidebandStreamChanged was true
Lloyd Pique0b785d82018-12-04 17:25:27 -0800642 mSidebandStream = s.sidebandStream;
Lloyd Piquede196652020-01-22 17:29:58 -0800643 editCompositionState()->sidebandStream = mSidebandStream;
Lloyd Pique0b785d82018-12-04 17:25:27 -0800644 if (mSidebandStream != nullptr) {
Marissa Wall61c58622018-07-18 10:12:20 -0700645 setTransactionFlags(eTransactionNeeded);
646 mFlinger->setTransactionFlags(eTraversalNeeded);
647 }
648 recomputeVisibleRegions = true;
649
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800650 return true;
Marissa Wall61c58622018-07-18 10:12:20 -0700651 }
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800652 return false;
Marissa Wall61c58622018-07-18 10:12:20 -0700653}
654
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800655bool BufferStateLayer::hasFrameUpdate() const {
Robert Carr6a160312021-05-17 12:08:20 -0700656 const State& c(getDrawingState());
Robert Carr315f3c72021-06-24 21:58:09 -0700657 return (mDrawingStateModified || mDrawingState.modified) && (c.buffer != nullptr || c.bgColorLayer != nullptr);
Marissa Wall61c58622018-07-18 10:12:20 -0700658}
659
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700660status_t BufferStateLayer::updateTexImage(bool& /*recomputeVisibleRegions*/, nsecs_t latchTime,
661 nsecs_t /*expectedPresentTime*/) {
Marissa Wall61c58622018-07-18 10:12:20 -0700662 const State& s(getDrawingState());
663
664 if (!s.buffer) {
Valerie Hauaa194562019-02-05 16:21:38 -0800665 if (s.bgColorLayer) {
666 for (auto& handle : mDrawingState.callbackHandles) {
667 handle->latchTime = latchTime;
668 }
669 }
Marissa Wall61c58622018-07-18 10:12:20 -0700670 return NO_ERROR;
671 }
672
Marissa Wall5a68a772018-12-22 17:43:42 -0800673 for (auto& handle : mDrawingState.callbackHandles) {
Vishnu Nair935590e2021-02-10 13:05:52 -0800674 if (handle->frameNumber == mDrawingState.frameNumber) {
675 handle->latchTime = latchTime;
676 }
Marissa Wall5a68a772018-12-22 17:43:42 -0800677 }
Marissa Walle2ffb422018-10-12 11:33:52 -0700678
Vishnu Nairea0de002020-11-17 17:42:37 -0800679 const int32_t layerId = getSequence();
Vishnu Nairdbbe3852022-01-12 20:22:11 -0800680 const uint64_t bufferId = mDrawingState.buffer->getId();
Adithya Srinivasanb238cd52021-02-04 17:54:05 +0000681 const uint64_t frameNumber = mDrawingState.frameNumber;
682 const auto acquireFence = std::make_shared<FenceTime>(mDrawingState.acquireFence);
683 mFlinger->mTimeStats->setAcquireFence(layerId, frameNumber, acquireFence);
684 mFlinger->mTimeStats->setLatchTime(layerId, frameNumber, latchTime);
685
686 mFlinger->mFrameTracer->traceFence(layerId, bufferId, frameNumber, acquireFence,
687 FrameTracer::FrameEvent::ACQUIRE_FENCE);
688 mFlinger->mFrameTracer->traceTimestamp(layerId, bufferId, frameNumber, latchTime,
689 FrameTracer::FrameEvent::LATCH);
Marissa Wall61c58622018-07-18 10:12:20 -0700690
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +0000691 auto& bufferSurfaceFrame = mDrawingState.bufferSurfaceFrameTX;
692 if (bufferSurfaceFrame != nullptr &&
693 bufferSurfaceFrame->getPresentState() != PresentState::Presented) {
694 // Update only if the bufferSurfaceFrame wasn't already presented. A Presented
695 // bufferSurfaceFrame could be seen here if a pending state was applied successfully and we
696 // are processing the next state.
697 addSurfaceFramePresentedForBuffer(bufferSurfaceFrame,
Ady Abraham6c1b7ac2021-03-31 16:56:03 -0700698 mDrawingState.acquireFenceTime->getSignalTime(),
699 latchTime);
Robert Carr6a160312021-05-17 12:08:20 -0700700 mDrawingState.bufferSurfaceFrameTX.reset();
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +0000701 }
702
Vishnu Nairfc46c1e2021-04-21 08:31:32 -0700703 std::deque<sp<CallbackHandle>> remainingHandles;
704 mFlinger->getTransactionCallbackInvoker()
Robert Carr3d1047b2021-09-20 18:22:32 -0700705 .addOnCommitCallbackHandles(mDrawingState.callbackHandles, remainingHandles);
Vishnu Nairfc46c1e2021-04-21 08:31:32 -0700706 mDrawingState.callbackHandles = remainingHandles;
707
Robert Carr6a160312021-05-17 12:08:20 -0700708 mDrawingStateModified = false;
Marissa Wall16c112d2019-03-20 13:21:13 -0700709
Marissa Wall61c58622018-07-18 10:12:20 -0700710 return NO_ERROR;
711}
712
713status_t BufferStateLayer::updateActiveBuffer() {
714 const State& s(getDrawingState());
715
716 if (s.buffer == nullptr) {
717 return BAD_VALUE;
718 }
chaviwdf3c5e82021-01-07 13:00:37 -0800719
Vishnu Nairdbbe3852022-01-12 20:22:11 -0800720 if (!mBufferInfo.mBuffer || !s.buffer->hasSameBuffer(*mBufferInfo.mBuffer)) {
chaviwdf3c5e82021-01-07 13:00:37 -0800721 decrementPendingBufferCount();
722 }
Marissa Wall61c58622018-07-18 10:12:20 -0700723
Vishnu Nair4ba0c2e2021-06-24 11:27:17 -0700724 mPreviousReleaseCallbackId = {getCurrentBufferId(), mBufferInfo.mFrameNumber};
chaviwd62d3062019-09-04 14:48:02 -0700725 mBufferInfo.mBuffer = s.buffer;
726 mBufferInfo.mFence = s.acquireFence;
Vishnu Nair4ba0c2e2021-06-24 11:27:17 -0700727 mBufferInfo.mFrameNumber = s.frameNumber;
Marissa Wall61c58622018-07-18 10:12:20 -0700728
729 return NO_ERROR;
730}
731
Valerie Haubf784642020-01-29 07:25:23 -0800732status_t BufferStateLayer::updateFrameNumber(nsecs_t latchTime) {
Marissa Wall61c58622018-07-18 10:12:20 -0700733 // TODO(marissaw): support frame history events
Mikael Pessa2e1608f2019-07-19 11:25:35 -0700734 mPreviousFrameNumber = mCurrentFrameNumber;
Valerie Hau134651a2020-01-28 16:21:22 -0800735 mCurrentFrameNumber = mDrawingState.frameNumber;
Valerie Haubf784642020-01-29 07:25:23 -0800736 {
737 Mutex::Autolock lock(mFrameEventHistoryMutex);
738 mFrameEventHistory.addLatch(mCurrentFrameNumber, latchTime);
739 }
Marissa Wall61c58622018-07-18 10:12:20 -0700740 return NO_ERROR;
741}
742
Marissa Wall947d34e2019-03-29 14:03:53 -0700743void BufferStateLayer::HwcSlotGenerator::bufferErased(const client_cache_t& clientCacheId) {
744 std::lock_guard lock(mMutex);
745 if (!clientCacheId.isValid()) {
746 ALOGE("invalid process, failed to erase buffer");
747 return;
748 }
749 eraseBufferLocked(clientCacheId);
750}
751
rnleeed20fa42021-08-10 18:00:03 -0700752int BufferStateLayer::HwcSlotGenerator::getHwcCacheSlot(const client_cache_t& clientCacheId) {
Marissa Wall947d34e2019-03-29 14:03:53 -0700753 std::lock_guard<std::mutex> lock(mMutex);
754 auto itr = mCachedBuffers.find(clientCacheId);
755 if (itr == mCachedBuffers.end()) {
756 return addCachedBuffer(clientCacheId);
757 }
758 auto& [hwcCacheSlot, counter] = itr->second;
759 counter = mCounter++;
760 return hwcCacheSlot;
761}
762
rnleeed20fa42021-08-10 18:00:03 -0700763int BufferStateLayer::HwcSlotGenerator::addCachedBuffer(const client_cache_t& clientCacheId)
Marissa Wall947d34e2019-03-29 14:03:53 -0700764 REQUIRES(mMutex) {
765 if (!clientCacheId.isValid()) {
766 ALOGE("invalid process, returning invalid slot");
767 return BufferQueue::INVALID_BUFFER_SLOT;
768 }
769
770 ClientCache::getInstance().registerErasedRecipient(clientCacheId, wp<ErasedRecipient>(this));
771
rnleeed20fa42021-08-10 18:00:03 -0700772 int hwcCacheSlot = getFreeHwcCacheSlot();
Marissa Wall947d34e2019-03-29 14:03:53 -0700773 mCachedBuffers[clientCacheId] = {hwcCacheSlot, mCounter++};
774 return hwcCacheSlot;
775}
776
rnleeed20fa42021-08-10 18:00:03 -0700777int BufferStateLayer::HwcSlotGenerator::getFreeHwcCacheSlot() REQUIRES(mMutex) {
Marissa Wall947d34e2019-03-29 14:03:53 -0700778 if (mFreeHwcCacheSlots.empty()) {
779 evictLeastRecentlyUsed();
780 }
781
rnleeed20fa42021-08-10 18:00:03 -0700782 int hwcCacheSlot = mFreeHwcCacheSlots.top();
Marissa Wall947d34e2019-03-29 14:03:53 -0700783 mFreeHwcCacheSlots.pop();
784 return hwcCacheSlot;
785}
786
787void BufferStateLayer::HwcSlotGenerator::evictLeastRecentlyUsed() REQUIRES(mMutex) {
788 uint64_t minCounter = UINT_MAX;
789 client_cache_t minClientCacheId = {};
790 for (const auto& [clientCacheId, slotCounter] : mCachedBuffers) {
791 const auto& [hwcCacheSlot, counter] = slotCounter;
792 if (counter < minCounter) {
793 minCounter = counter;
794 minClientCacheId = clientCacheId;
795 }
796 }
797 eraseBufferLocked(minClientCacheId);
798
799 ClientCache::getInstance().unregisterErasedRecipient(minClientCacheId, this);
800}
801
802void BufferStateLayer::HwcSlotGenerator::eraseBufferLocked(const client_cache_t& clientCacheId)
803 REQUIRES(mMutex) {
804 auto itr = mCachedBuffers.find(clientCacheId);
805 if (itr == mCachedBuffers.end()) {
806 return;
807 }
808 auto& [hwcCacheSlot, counter] = itr->second;
809
810 // TODO send to hwc cache and resources
811
812 mFreeHwcCacheSlots.push(hwcCacheSlot);
813 mCachedBuffers.erase(clientCacheId);
814}
chaviw4244e032019-09-04 11:27:49 -0700815
816void BufferStateLayer::gatherBufferInfo() {
chaviwdebadb82020-03-26 14:57:24 -0700817 BufferLayer::gatherBufferInfo();
chaviw4244e032019-09-04 11:27:49 -0700818
chaviwdebadb82020-03-26 14:57:24 -0700819 const State& s(getDrawingState());
chaviw4244e032019-09-04 11:27:49 -0700820 mBufferInfo.mDesiredPresentTime = s.desiredPresentTime;
821 mBufferInfo.mFenceTime = std::make_shared<FenceTime>(s.acquireFence);
822 mBufferInfo.mFence = s.acquireFence;
chaviw766c9c52021-02-10 17:36:47 -0800823 mBufferInfo.mTransform = s.bufferTransform;
Robert Carr167bdde2021-07-28 11:26:51 -0700824 auto lastDataspace = mBufferInfo.mDataspace;
chaviw4244e032019-09-04 11:27:49 -0700825 mBufferInfo.mDataspace = translateDataspace(s.dataspace);
Robert Carr167bdde2021-07-28 11:26:51 -0700826 if (lastDataspace != mBufferInfo.mDataspace) {
827 mFlinger->mSomeDataspaceChanged = true;
828 }
Vishnu Nair5cc9ac02021-04-19 13:23:38 -0700829 mBufferInfo.mCrop = computeBufferCrop(s);
chaviw4244e032019-09-04 11:27:49 -0700830 mBufferInfo.mScaleMode = NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW;
831 mBufferInfo.mSurfaceDamage = s.surfaceDamageRegion;
832 mBufferInfo.mHdrMetadata = s.hdrMetadata;
833 mBufferInfo.mApi = s.api;
chaviw4244e032019-09-04 11:27:49 -0700834 mBufferInfo.mTransformToDisplayInverse = s.transformToDisplayInverse;
chaviwf83ce182019-09-12 14:43:08 -0700835 mBufferInfo.mBufferSlot = mHwcSlotGenerator->getHwcCacheSlot(s.clientCacheId);
chaviw4244e032019-09-04 11:27:49 -0700836}
837
Robert Carr916b0362020-10-06 13:53:03 -0700838uint32_t BufferStateLayer::getEffectiveScalingMode() const {
839 return NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW;
840}
841
Vishnu Nair5cc9ac02021-04-19 13:23:38 -0700842Rect BufferStateLayer::computeBufferCrop(const State& s) {
chaviwf3f40fe2021-04-27 15:54:02 -0500843 if (s.buffer && !s.bufferCrop.isEmpty()) {
844 Rect bufferCrop;
Vishnu Nairdbbe3852022-01-12 20:22:11 -0800845 s.buffer->getBounds().intersect(s.bufferCrop, &bufferCrop);
chaviwf3f40fe2021-04-27 15:54:02 -0500846 return bufferCrop;
847 } else if (s.buffer) {
Vishnu Nairdbbe3852022-01-12 20:22:11 -0800848 return s.buffer->getBounds();
chaviwf3f40fe2021-04-27 15:54:02 -0500849 } else {
850 return s.bufferCrop;
chaviw4244e032019-09-04 11:27:49 -0700851 }
chaviw4244e032019-09-04 11:27:49 -0700852}
853
chaviwb4c6e582019-08-16 14:35:07 -0700854sp<Layer> BufferStateLayer::createClone() {
Vishnu Nair7fb9e5a2021-11-08 12:44:05 -0800855 LayerCreationArgs args(mFlinger.get(), nullptr, mName + " (Mirror)", 0, LayerMetadata());
chaviwb4c6e582019-08-16 14:35:07 -0700856 args.textureName = mTextureName;
Lloyd Pique1c3a5eb2019-10-03 13:07:08 -0700857 sp<BufferStateLayer> layer = mFlinger->getFactory().createBufferStateLayer(args);
chaviwb4c6e582019-08-16 14:35:07 -0700858 layer->mHwcSlotGenerator = mHwcSlotGenerator;
859 layer->setInitialValuesForClone(this);
860 return layer;
861}
Valerie Hau92bf5482020-02-10 09:49:08 -0800862
Vishnu Naire7f79c52020-10-29 14:45:03 -0700863bool BufferStateLayer::bufferNeedsFiltering() const {
864 const State& s(getDrawingState());
865 if (!s.buffer) {
866 return false;
867 }
868
Vishnu Nairdbbe3852022-01-12 20:22:11 -0800869 int32_t bufferWidth = static_cast<int32_t>(s.buffer->getWidth());
870 int32_t bufferHeight = static_cast<int32_t>(s.buffer->getHeight());
Vishnu Naire7f79c52020-10-29 14:45:03 -0700871
872 // Undo any transformations on the buffer and return the result.
chaviw766c9c52021-02-10 17:36:47 -0800873 if (s.bufferTransform & ui::Transform::ROT_90) {
Vishnu Naire7f79c52020-10-29 14:45:03 -0700874 std::swap(bufferWidth, bufferHeight);
875 }
876
877 if (s.transformToDisplayInverse) {
878 uint32_t invTransform = DisplayDevice::getPrimaryDisplayRotationFlags();
879 if (invTransform & ui::Transform::ROT_90) {
880 std::swap(bufferWidth, bufferHeight);
881 }
882 }
883
884 const Rect layerSize{getBounds()};
885 return layerSize.width() != bufferWidth || layerSize.height() != bufferHeight;
886}
Robert Carr7121caf2020-12-15 13:07:32 -0800887
Robert Carr7121caf2020-12-15 13:07:32 -0800888void BufferStateLayer::decrementPendingBufferCount() {
Vishnu Nair8eda69e2021-02-26 10:42:10 -0800889 int32_t pendingBuffers = --mPendingBufferTransactions;
890 tracePendingBufferCount(pendingBuffers);
Robert Carr7121caf2020-12-15 13:07:32 -0800891}
892
Vishnu Nair8eda69e2021-02-26 10:42:10 -0800893void BufferStateLayer::tracePendingBufferCount(int32_t pendingBuffers) {
894 ATRACE_INT(mBlastTransactionName.c_str(), pendingBuffers);
Robert Carr7121caf2020-12-15 13:07:32 -0800895}
896
Robert Carr7121caf2020-12-15 13:07:32 -0800897
chaviw39d01472021-04-08 14:26:24 -0500898/*
899 * We don't want to send the layer's transform to input, but rather the
900 * parent's transform. This is because BufferStateLayer's transform is
901 * information about how the buffer is placed on screen. The parent's
902 * transform makes more sense to send since it's information about how the
903 * layer is placed on screen. This transform is used by input to determine
904 * how to go from screen space back to window space.
905 */
906ui::Transform BufferStateLayer::getInputTransform() const {
Rob Carrc6d2d2b2021-10-25 16:51:49 +0000907 sp<Layer> parent = mDrawingParent.promote();
chaviw39d01472021-04-08 14:26:24 -0500908 if (parent == nullptr) {
909 return ui::Transform();
910 }
911
912 return parent->getTransform();
913}
914
915/**
916 * Similar to getInputTransform, we need to update the bounds to include the transform.
917 * This is because bounds for BSL doesn't include buffer transform, where the input assumes
918 * that's already included.
919 */
920Rect BufferStateLayer::getInputBounds() const {
921 Rect bufferBounds = getCroppedBufferSize(getDrawingState());
922 if (mDrawingState.transform.getType() == ui::Transform::IDENTITY || !bufferBounds.isValid()) {
923 return bufferBounds;
924 }
925 return mDrawingState.transform.transform(bufferBounds);
926}
927
Marissa Wall61c58622018-07-18 10:12:20 -0700928} // namespace android