blob: c213570c7fd1c32250f13a67a90ec0b98481b1b7 [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 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
Robert Carr6a160312021-05-17 12:08:20 -0700298 uint32_t bufferWidth = mDrawingState.buffer->getBuffer()->getWidth();
299 uint32_t bufferHeight = mDrawingState.buffer->getBuffer()->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
chaviwba4320c2021-09-15 15:20:53 -0500371std::shared_ptr<renderengine::ExternalTexture> BufferStateLayer::getBufferFromBufferData(
372 const BufferData& bufferData) {
373 bool cacheIdChanged = bufferData.flags.test(BufferData::BufferDataChange::cachedBufferChanged);
374 bool bufferSizeExceedsLimit = false;
375 std::shared_ptr<renderengine::ExternalTexture> buffer = nullptr;
376 if (cacheIdChanged && bufferData.buffer != nullptr) {
377 bufferSizeExceedsLimit =
378 mFlinger->exceedsMaxRenderTargetSize(bufferData.buffer->getWidth(),
379 bufferData.buffer->getHeight());
380 if (!bufferSizeExceedsLimit) {
381 ClientCache::getInstance().add(bufferData.cachedBuffer, bufferData.buffer);
382 buffer = ClientCache::getInstance().get(bufferData.cachedBuffer);
383 }
384 } else if (cacheIdChanged) {
385 buffer = ClientCache::getInstance().get(bufferData.cachedBuffer);
386 } else if (bufferData.buffer != nullptr) {
387 bufferSizeExceedsLimit =
388 mFlinger->exceedsMaxRenderTargetSize(bufferData.buffer->getWidth(),
389 bufferData.buffer->getHeight());
390 if (!bufferSizeExceedsLimit) {
391 buffer = std::make_shared<
392 renderengine::ExternalTexture>(bufferData.buffer, mFlinger->getRenderEngine(),
393 renderengine::ExternalTexture::Usage::READABLE);
394 }
395 }
396 ALOGE_IF(bufferSizeExceedsLimit,
397 "Attempted to create an ExternalTexture for layer %s that exceeds render target size "
398 "limit.",
399 getDebugName());
400 return buffer;
401}
402
403bool BufferStateLayer::setBuffer(const BufferData& bufferData, nsecs_t postTime,
Alec Mouria90a5702021-04-16 16:36:21 +0000404 nsecs_t desiredPresentTime, bool isAutoTimestamp,
chaviwba4320c2021-09-15 15:20:53 -0500405 std::optional<nsecs_t> dequeueTime,
406 const FrameTimelineInfo& info) {
Robert Carr0c1966e2020-10-19 12:12:08 -0700407 ATRACE_CALL();
408
chaviwba4320c2021-09-15 15:20:53 -0500409 const std::shared_ptr<renderengine::ExternalTexture>& buffer =
410 getBufferFromBufferData(bufferData);
411 if (!buffer) {
412 return false;
413 }
414
415 const bool frameNumberChanged =
416 bufferData.flags.test(BufferData::BufferDataChange::frameNumberChanged);
417 const uint64_t frameNumber =
418 frameNumberChanged ? bufferData.frameNumber : mDrawingState.frameNumber + 1;
419
Robert Carr6a160312021-05-17 12:08:20 -0700420 if (mDrawingState.buffer) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700421 mReleasePreviousBuffer = true;
Vishnu Nair52846cd2021-08-05 16:12:48 -0700422 if (mDrawingState.buffer != mBufferInfo.mBuffer ||
423 mDrawingState.frameNumber != mBufferInfo.mFrameNumber) {
Robert Carr6a160312021-05-17 12:08:20 -0700424 // If mDrawingState has a buffer, and we are about to update again
Robert Carr7121caf2020-12-15 13:07:32 -0800425 // before swapping to drawing state, then the first buffer will be
Vishnu Nair1506b182021-02-22 14:35:15 -0800426 // dropped and we should decrement the pending buffer count and
427 // call any release buffer callbacks if set.
Robert Carr6a160312021-05-17 12:08:20 -0700428 callReleaseBufferCallback(mDrawingState.releaseBufferListener,
Vishnu Nair4ba0c2e2021-06-24 11:27:17 -0700429 mDrawingState.buffer->getBuffer(), mDrawingState.frameNumber,
chaviw69058fb2021-09-27 09:37:30 -0500430 mDrawingState.acquireFence,
Ady Abraham899dcdb2021-06-15 16:56:21 -0700431 mFlinger->getMaxAcquiredBufferCountForCurrentRefreshRate(
432 mOwnerUid));
Robert Carr7121caf2020-12-15 13:07:32 -0800433 decrementPendingBufferCount();
Robert Carr6a160312021-05-17 12:08:20 -0700434 if (mDrawingState.bufferSurfaceFrameTX != nullptr &&
435 mDrawingState.bufferSurfaceFrameTX->getPresentState() != PresentState::Presented) {
436 addSurfaceFrameDroppedForBuffer(mDrawingState.bufferSurfaceFrameTX);
437 mDrawingState.bufferSurfaceFrameTX.reset();
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +0000438 }
Robert Carrccab4242021-09-28 16:53:03 -0700439 } else if (mLastClientCompositionFence != nullptr) {
440 callReleaseBufferCallback(mDrawingState.releaseBufferListener,
441 mDrawingState.buffer->getBuffer(), mDrawingState.frameNumber,
chaviw69058fb2021-09-27 09:37:30 -0500442 mLastClientCompositionFence,
Robert Carrccab4242021-09-28 16:53:03 -0700443 mFlinger->getMaxAcquiredBufferCountForCurrentRefreshRate(
chaviw69058fb2021-09-27 09:37:30 -0500444 mOwnerUid));
Robert Carrccab4242021-09-28 16:53:03 -0700445 mLastClientCompositionFence = nullptr;
Robert Carr7121caf2020-12-15 13:07:32 -0800446 }
Marissa Wallfda30bb2018-10-12 11:34:28 -0700447 }
Robert Carr6a160312021-05-17 12:08:20 -0700448
449 mDrawingState.frameNumber = frameNumber;
chaviwba4320c2021-09-15 15:20:53 -0500450 mDrawingState.releaseBufferListener = bufferData.releaseBufferListener;
Robert Carr6a160312021-05-17 12:08:20 -0700451 mDrawingState.buffer = buffer;
chaviwba4320c2021-09-15 15:20:53 -0500452 mDrawingState.clientCacheId = bufferData.cachedBuffer;
453
454 mDrawingState.acquireFence = bufferData.flags.test(BufferData::BufferDataChange::fenceChanged)
455 ? bufferData.acquireFence
456 : Fence::NO_FENCE;
457 mDrawingState.acquireFenceTime = std::make_unique<FenceTime>(mDrawingState.acquireFence);
458 // The acquire fences of BufferStateLayers have already signaled before they are set
459 mCallbackHandleAcquireTime = mDrawingState.acquireFenceTime->getSignalTime();
460
Robert Carr6a160312021-05-17 12:08:20 -0700461 mDrawingState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700462 setTransactionFlags(eTransactionNeeded);
Ady Abraham09bd3922019-04-08 10:44:56 -0700463
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800464 const int32_t layerId = getSequence();
Robert Carr6a160312021-05-17 12:08:20 -0700465 mFlinger->mTimeStats->setPostTime(layerId, mDrawingState.frameNumber, getName().c_str(),
Adithya Srinivasan58069dc2021-06-04 20:37:02 +0000466 mOwnerUid, postTime, getGameMode());
Robert Carr6a160312021-05-17 12:08:20 -0700467 mDrawingState.desiredPresentTime = desiredPresentTime;
468 mDrawingState.isAutoTimestamp = isAutoTimestamp;
Ady Abraham09bd3922019-04-08 10:44:56 -0700469
Ady Abrahamb7f15562021-03-15 18:34:08 -0700470 const nsecs_t presentTime = [&] {
471 if (!isAutoTimestamp) return desiredPresentTime;
472
473 const auto prediction =
474 mFlinger->mFrameTimeline->getTokenManager()->getPredictionsForToken(info.vsyncId);
475 if (prediction.has_value()) return prediction->presentTime;
476
477 return static_cast<nsecs_t>(0);
478 }();
479 mFlinger->mScheduler->recordLayerHistory(this, presentTime,
Ady Abraham5def7332020-05-29 16:13:47 -0700480 LayerHistory::LayerUpdateType::Buffer);
Ady Abraham09bd3922019-04-08 10:44:56 -0700481
chaviwba4320c2021-09-15 15:20:53 -0500482 addFrameEvent(mDrawingState.acquireFence, postTime, isAutoTimestamp ? 0 : desiredPresentTime);
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +0000483
Adithya Srinivasan891004e2021-02-12 20:20:47 +0000484 setFrameTimelineVsyncForBufferTransaction(info, postTime);
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +0000485
Alec Mouria90a5702021-04-16 16:36:21 +0000486 if (buffer && dequeueTime && *dequeueTime != 0) {
487 const uint64_t bufferId = buffer->getBuffer()->getId();
Adithya Srinivasanb238cd52021-02-04 17:54:05 +0000488 mFlinger->mFrameTracer->traceNewLayer(layerId, getName().c_str());
489 mFlinger->mFrameTracer->traceTimestamp(layerId, bufferId, frameNumber, *dequeueTime,
490 FrameTracer::FrameEvent::DEQUEUE);
491 mFlinger->mFrameTracer->traceTimestamp(layerId, bufferId, frameNumber, postTime,
492 FrameTracer::FrameEvent::QUEUE);
493 }
Chavi Weingartena5aedbd2021-04-09 13:37:33 +0000494
Robert Carr6a160312021-05-17 12:08:20 -0700495 mDrawingState.width = mDrawingState.buffer->getBuffer()->getWidth();
496 mDrawingState.height = mDrawingState.buffer->getBuffer()->getHeight();
chaviwba4320c2021-09-15 15:20:53 -0500497 mDrawingState.releaseBufferEndpoint = bufferData.releaseBufferEndpoint;
Marissa Wall61c58622018-07-18 10:12:20 -0700498 return true;
499}
500
501bool BufferStateLayer::setDataspace(ui::Dataspace dataspace) {
Robert Carr6a160312021-05-17 12:08:20 -0700502 if (mDrawingState.dataspace == dataspace) return false;
503 mDrawingState.dataspace = dataspace;
504 mDrawingState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700505 setTransactionFlags(eTransactionNeeded);
506 return true;
507}
508
509bool BufferStateLayer::setHdrMetadata(const HdrMetadata& hdrMetadata) {
Robert Carr6a160312021-05-17 12:08:20 -0700510 if (mDrawingState.hdrMetadata == hdrMetadata) return false;
511 mDrawingState.hdrMetadata = hdrMetadata;
512 mDrawingState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700513 setTransactionFlags(eTransactionNeeded);
514 return true;
515}
516
517bool BufferStateLayer::setSurfaceDamageRegion(const Region& surfaceDamage) {
Robert Carr6a160312021-05-17 12:08:20 -0700518 mDrawingState.surfaceDamageRegion = surfaceDamage;
519 mDrawingState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700520 setTransactionFlags(eTransactionNeeded);
521 return true;
522}
523
524bool BufferStateLayer::setApi(int32_t api) {
Robert Carr6a160312021-05-17 12:08:20 -0700525 if (mDrawingState.api == api) return false;
526 mDrawingState.api = api;
527 mDrawingState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700528 setTransactionFlags(eTransactionNeeded);
529 return true;
530}
531
532bool BufferStateLayer::setSidebandStream(const sp<NativeHandle>& sidebandStream) {
Robert Carr6a160312021-05-17 12:08:20 -0700533 if (mDrawingState.sidebandStream == sidebandStream) return false;
Robert Carr3e2a2992021-06-11 13:42:55 -0700534
535 if (mDrawingState.sidebandStream != nullptr && sidebandStream == nullptr) {
536 mFlinger->mTunnelModeEnabledReporter->decrementTunnelModeCount();
537 } else if (sidebandStream != nullptr) {
538 mFlinger->mTunnelModeEnabledReporter->incrementTunnelModeCount();
539 }
540
Robert Carr6a160312021-05-17 12:08:20 -0700541 mDrawingState.sidebandStream = sidebandStream;
542 mDrawingState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700543 setTransactionFlags(eTransactionNeeded);
Marissa Wall61c58622018-07-18 10:12:20 -0700544 if (!mSidebandStreamChanged.exchange(true)) {
545 // mSidebandStreamChanged was false
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -0700546 mFlinger->onLayerUpdate();
Marissa Wall61c58622018-07-18 10:12:20 -0700547 }
548 return true;
549}
550
Marissa Walle2ffb422018-10-12 11:33:52 -0700551bool BufferStateLayer::setTransactionCompletedListeners(
552 const std::vector<sp<CallbackHandle>>& handles) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700553 // If there is no handle, we will not send a callback so reset mReleasePreviousBuffer and return
Marissa Walle2ffb422018-10-12 11:33:52 -0700554 if (handles.empty()) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700555 mReleasePreviousBuffer = false;
Marissa Walle2ffb422018-10-12 11:33:52 -0700556 return false;
557 }
558
559 const bool willPresent = willPresentCurrentTransaction();
560
561 for (const auto& handle : handles) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700562 // If this transaction set a buffer on this layer, release its previous buffer
563 handle->releasePreviousBuffer = mReleasePreviousBuffer;
564
Marissa Walle2ffb422018-10-12 11:33:52 -0700565 // If this layer will be presented in this frame
566 if (willPresent) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700567 // If this transaction set an acquire fence on this layer, set its acquire time
568 handle->acquireTime = mCallbackHandleAcquireTime;
Robert Carr6a160312021-05-17 12:08:20 -0700569 handle->frameNumber = mDrawingState.frameNumber;
Marissa Wallfda30bb2018-10-12 11:34:28 -0700570
Marissa Walle2ffb422018-10-12 11:33:52 -0700571 // Store so latched time and release fence can be set
Robert Carr6a160312021-05-17 12:08:20 -0700572 mDrawingState.callbackHandles.push_back(handle);
Marissa Walle2ffb422018-10-12 11:33:52 -0700573
574 } else { // If this layer will NOT need to be relatched and presented this frame
575 // Notify the transaction completed thread this handle is done
Robert Carr9a803c32021-01-14 16:57:58 -0800576 mFlinger->getTransactionCallbackInvoker().registerUnpresentedCallbackHandle(handle);
Marissa Walle2ffb422018-10-12 11:33:52 -0700577 }
578 }
579
Marissa Wallfda30bb2018-10-12 11:34:28 -0700580 mReleasePreviousBuffer = false;
581 mCallbackHandleAcquireTime = -1;
582
Marissa Walle2ffb422018-10-12 11:33:52 -0700583 return willPresent;
584}
585
Marissa Wall61c58622018-07-18 10:12:20 -0700586bool BufferStateLayer::setTransparentRegionHint(const Region& transparent) {
Vishnu Nair27e3ed52021-07-08 18:24:25 -0700587 mDrawingState.sequence++;
Robert Carr6a160312021-05-17 12:08:20 -0700588 mDrawingState.transparentRegionHint = transparent;
589 mDrawingState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700590 setTransactionFlags(eTransactionNeeded);
591 return true;
592}
593
rnleeed20fa42021-08-10 18:00:03 -0700594Rect BufferStateLayer::getBufferSize(const State& /*s*/) const {
Marissa Wall861616d2018-10-22 12:52:23 -0700595 // for buffer state layers we use the display frame size as the buffer size.
Marissa Wall61c58622018-07-18 10:12:20 -0700596
chaviw7e72caf2020-12-02 16:50:43 -0800597 if (mBufferInfo.mBuffer == nullptr) {
598 return Rect::INVALID_RECT;
599 }
600
Vishnu Nair6bdec7d2021-05-10 15:01:13 -0700601 uint32_t bufWidth = mBufferInfo.mBuffer->getBuffer()->getWidth();
602 uint32_t bufHeight = mBufferInfo.mBuffer->getBuffer()->getHeight();
603
604 // Undo any transformations on the buffer and return the result.
605 if (mBufferInfo.mTransform & ui::Transform::ROT_90) {
606 std::swap(bufWidth, bufHeight);
607 }
608
609 if (getTransformToDisplayInverse()) {
610 uint32_t invTransform = DisplayDevice::getPrimaryDisplayRotationFlags();
611 if (invTransform & ui::Transform::ROT_90) {
612 std::swap(bufWidth, bufHeight);
Marissa Wall861616d2018-10-22 12:52:23 -0700613 }
614 }
615
rnleeed20fa42021-08-10 18:00:03 -0700616 return Rect(0, 0, static_cast<int32_t>(bufWidth), static_cast<int32_t>(bufHeight));
Marissa Wall61c58622018-07-18 10:12:20 -0700617}
Vishnu Nair4351ad52019-02-11 14:13:02 -0800618
619FloatRect BufferStateLayer::computeSourceBounds(const FloatRect& parentBounds) const {
Vishnu Nair6bdec7d2021-05-10 15:01:13 -0700620 if (mBufferInfo.mBuffer == nullptr) {
621 return parentBounds;
Vishnu Nair4351ad52019-02-11 14:13:02 -0800622 }
623
Vishnu Nair6bdec7d2021-05-10 15:01:13 -0700624 return getBufferSize(getDrawingState()).toFloatRect();
Vishnu Nair4351ad52019-02-11 14:13:02 -0800625}
626
Marissa Wall61c58622018-07-18 10:12:20 -0700627// -----------------------------------------------------------------------
628
629// -----------------------------------------------------------------------
630// Interface implementation for BufferLayer
631// -----------------------------------------------------------------------
632bool BufferStateLayer::fenceHasSignaled() const {
Huihong Luo86c80e32021-06-16 15:41:07 -0700633 if (SurfaceFlinger::enableLatchUnsignaled) {
634 return true;
635 }
636
Alec Mouri91f6df32020-01-30 08:48:58 -0800637 const bool fenceSignaled =
638 getDrawingState().acquireFence->getStatus() == Fence::Status::Signaled;
639 if (!fenceSignaled) {
640 mFlinger->mTimeStats->incrementLatchSkipped(getSequence(),
641 TimeStats::LatchSkipReason::LateAcquire);
642 }
643
644 return fenceSignaled;
Marissa Wall61c58622018-07-18 10:12:20 -0700645}
646
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700647bool BufferStateLayer::framePresentTimeIsCurrent(nsecs_t expectedPresentTime) const {
Ady Abrahamcd1580c2019-04-29 15:40:03 -0700648 if (!hasFrameUpdate() || isRemovedFromCurrentState()) {
649 return true;
650 }
651
Robert Carr6a160312021-05-17 12:08:20 -0700652 return mDrawingState.isAutoTimestamp || mDrawingState.desiredPresentTime <= expectedPresentTime;
Ady Abrahamcd1580c2019-04-29 15:40:03 -0700653}
654
Valerie Hau871d6352020-01-29 08:44:02 -0800655bool BufferStateLayer::onPreComposition(nsecs_t refreshStartTime) {
656 for (const auto& handle : mDrawingState.callbackHandles) {
657 handle->refreshStartTime = refreshStartTime;
658 }
659 return BufferLayer::onPreComposition(refreshStartTime);
660}
661
Vishnu Naircf26a0a2020-11-13 12:56:20 -0800662void BufferStateLayer::setAutoRefresh(bool autoRefresh) {
663 if (!mAutoRefresh.exchange(autoRefresh)) {
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -0700664 mFlinger->onLayerUpdate();
Vishnu Naircf26a0a2020-11-13 12:56:20 -0800665 }
Marissa Wall61c58622018-07-18 10:12:20 -0700666}
667
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800668bool BufferStateLayer::latchSidebandStream(bool& recomputeVisibleRegions) {
baocheng suna663c2b2021-05-13 18:51:28 +0800669 // 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 +0800670 editCompositionState()->sidebandStreamHasFrame = hasFrameUpdate() && mSidebandStream.get();
baocheng suna663c2b2021-05-13 18:51:28 +0800671
baocheng sun9691b9c2021-08-03 19:27:06 +0800672 if (mSidebandStreamChanged.exchange(false)) {
Marissa Wall61c58622018-07-18 10:12:20 -0700673 const State& s(getDrawingState());
674 // mSidebandStreamChanged was true
Lloyd Pique0b785d82018-12-04 17:25:27 -0800675 mSidebandStream = s.sidebandStream;
Lloyd Piquede196652020-01-22 17:29:58 -0800676 editCompositionState()->sidebandStream = mSidebandStream;
Lloyd Pique0b785d82018-12-04 17:25:27 -0800677 if (mSidebandStream != nullptr) {
Marissa Wall61c58622018-07-18 10:12:20 -0700678 setTransactionFlags(eTransactionNeeded);
679 mFlinger->setTransactionFlags(eTraversalNeeded);
680 }
681 recomputeVisibleRegions = true;
682
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800683 return true;
Marissa Wall61c58622018-07-18 10:12:20 -0700684 }
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800685 return false;
Marissa Wall61c58622018-07-18 10:12:20 -0700686}
687
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800688bool BufferStateLayer::hasFrameUpdate() const {
Robert Carr6a160312021-05-17 12:08:20 -0700689 const State& c(getDrawingState());
Robert Carr315f3c72021-06-24 21:58:09 -0700690 return (mDrawingStateModified || mDrawingState.modified) && (c.buffer != nullptr || c.bgColorLayer != nullptr);
Marissa Wall61c58622018-07-18 10:12:20 -0700691}
692
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700693status_t BufferStateLayer::updateTexImage(bool& /*recomputeVisibleRegions*/, nsecs_t latchTime,
694 nsecs_t /*expectedPresentTime*/) {
Marissa Wall61c58622018-07-18 10:12:20 -0700695 const State& s(getDrawingState());
696
697 if (!s.buffer) {
Valerie Hauaa194562019-02-05 16:21:38 -0800698 if (s.bgColorLayer) {
699 for (auto& handle : mDrawingState.callbackHandles) {
700 handle->latchTime = latchTime;
701 }
702 }
Marissa Wall61c58622018-07-18 10:12:20 -0700703 return NO_ERROR;
704 }
705
Marissa Wall5a68a772018-12-22 17:43:42 -0800706 for (auto& handle : mDrawingState.callbackHandles) {
Vishnu Nair935590e2021-02-10 13:05:52 -0800707 if (handle->frameNumber == mDrawingState.frameNumber) {
708 handle->latchTime = latchTime;
709 }
Marissa Wall5a68a772018-12-22 17:43:42 -0800710 }
Marissa Walle2ffb422018-10-12 11:33:52 -0700711
Vishnu Nairea0de002020-11-17 17:42:37 -0800712 const int32_t layerId = getSequence();
Alec Mouria90a5702021-04-16 16:36:21 +0000713 const uint64_t bufferId = mDrawingState.buffer->getBuffer()->getId();
Adithya Srinivasanb238cd52021-02-04 17:54:05 +0000714 const uint64_t frameNumber = mDrawingState.frameNumber;
715 const auto acquireFence = std::make_shared<FenceTime>(mDrawingState.acquireFence);
716 mFlinger->mTimeStats->setAcquireFence(layerId, frameNumber, acquireFence);
717 mFlinger->mTimeStats->setLatchTime(layerId, frameNumber, latchTime);
718
719 mFlinger->mFrameTracer->traceFence(layerId, bufferId, frameNumber, acquireFence,
720 FrameTracer::FrameEvent::ACQUIRE_FENCE);
721 mFlinger->mFrameTracer->traceTimestamp(layerId, bufferId, frameNumber, latchTime,
722 FrameTracer::FrameEvent::LATCH);
Marissa Wall61c58622018-07-18 10:12:20 -0700723
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +0000724 auto& bufferSurfaceFrame = mDrawingState.bufferSurfaceFrameTX;
725 if (bufferSurfaceFrame != nullptr &&
726 bufferSurfaceFrame->getPresentState() != PresentState::Presented) {
727 // Update only if the bufferSurfaceFrame wasn't already presented. A Presented
728 // bufferSurfaceFrame could be seen here if a pending state was applied successfully and we
729 // are processing the next state.
730 addSurfaceFramePresentedForBuffer(bufferSurfaceFrame,
Ady Abraham6c1b7ac2021-03-31 16:56:03 -0700731 mDrawingState.acquireFenceTime->getSignalTime(),
732 latchTime);
Robert Carr6a160312021-05-17 12:08:20 -0700733 mDrawingState.bufferSurfaceFrameTX.reset();
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +0000734 }
735
Vishnu Nairfc46c1e2021-04-21 08:31:32 -0700736 std::deque<sp<CallbackHandle>> remainingHandles;
737 mFlinger->getTransactionCallbackInvoker()
Robert Carr3d1047b2021-09-20 18:22:32 -0700738 .addOnCommitCallbackHandles(mDrawingState.callbackHandles, remainingHandles);
Vishnu Nairfc46c1e2021-04-21 08:31:32 -0700739 mDrawingState.callbackHandles = remainingHandles;
740
Robert Carr6a160312021-05-17 12:08:20 -0700741 mDrawingStateModified = false;
Marissa Wall16c112d2019-03-20 13:21:13 -0700742
Marissa Wall61c58622018-07-18 10:12:20 -0700743 return NO_ERROR;
744}
745
746status_t BufferStateLayer::updateActiveBuffer() {
747 const State& s(getDrawingState());
748
749 if (s.buffer == nullptr) {
750 return BAD_VALUE;
751 }
chaviwdf3c5e82021-01-07 13:00:37 -0800752
Alec Mouria90a5702021-04-16 16:36:21 +0000753 if (!mBufferInfo.mBuffer || s.buffer->getBuffer() != mBufferInfo.mBuffer->getBuffer()) {
chaviwdf3c5e82021-01-07 13:00:37 -0800754 decrementPendingBufferCount();
755 }
Marissa Wall61c58622018-07-18 10:12:20 -0700756
Vishnu Nair4ba0c2e2021-06-24 11:27:17 -0700757 mPreviousReleaseCallbackId = {getCurrentBufferId(), mBufferInfo.mFrameNumber};
chaviwd62d3062019-09-04 14:48:02 -0700758 mBufferInfo.mBuffer = s.buffer;
759 mBufferInfo.mFence = s.acquireFence;
Vishnu Nair4ba0c2e2021-06-24 11:27:17 -0700760 mBufferInfo.mFrameNumber = s.frameNumber;
Marissa Wall61c58622018-07-18 10:12:20 -0700761
762 return NO_ERROR;
763}
764
Valerie Haubf784642020-01-29 07:25:23 -0800765status_t BufferStateLayer::updateFrameNumber(nsecs_t latchTime) {
Marissa Wall61c58622018-07-18 10:12:20 -0700766 // TODO(marissaw): support frame history events
Mikael Pessa2e1608f2019-07-19 11:25:35 -0700767 mPreviousFrameNumber = mCurrentFrameNumber;
Valerie Hau134651a2020-01-28 16:21:22 -0800768 mCurrentFrameNumber = mDrawingState.frameNumber;
Valerie Haubf784642020-01-29 07:25:23 -0800769 {
770 Mutex::Autolock lock(mFrameEventHistoryMutex);
771 mFrameEventHistory.addLatch(mCurrentFrameNumber, latchTime);
772 }
Marissa Wall61c58622018-07-18 10:12:20 -0700773 return NO_ERROR;
774}
775
Marissa Wall947d34e2019-03-29 14:03:53 -0700776void BufferStateLayer::HwcSlotGenerator::bufferErased(const client_cache_t& clientCacheId) {
777 std::lock_guard lock(mMutex);
778 if (!clientCacheId.isValid()) {
779 ALOGE("invalid process, failed to erase buffer");
780 return;
781 }
782 eraseBufferLocked(clientCacheId);
783}
784
rnleeed20fa42021-08-10 18:00:03 -0700785int BufferStateLayer::HwcSlotGenerator::getHwcCacheSlot(const client_cache_t& clientCacheId) {
Marissa Wall947d34e2019-03-29 14:03:53 -0700786 std::lock_guard<std::mutex> lock(mMutex);
787 auto itr = mCachedBuffers.find(clientCacheId);
788 if (itr == mCachedBuffers.end()) {
789 return addCachedBuffer(clientCacheId);
790 }
791 auto& [hwcCacheSlot, counter] = itr->second;
792 counter = mCounter++;
793 return hwcCacheSlot;
794}
795
rnleeed20fa42021-08-10 18:00:03 -0700796int BufferStateLayer::HwcSlotGenerator::addCachedBuffer(const client_cache_t& clientCacheId)
Marissa Wall947d34e2019-03-29 14:03:53 -0700797 REQUIRES(mMutex) {
798 if (!clientCacheId.isValid()) {
799 ALOGE("invalid process, returning invalid slot");
800 return BufferQueue::INVALID_BUFFER_SLOT;
801 }
802
803 ClientCache::getInstance().registerErasedRecipient(clientCacheId, wp<ErasedRecipient>(this));
804
rnleeed20fa42021-08-10 18:00:03 -0700805 int hwcCacheSlot = getFreeHwcCacheSlot();
Marissa Wall947d34e2019-03-29 14:03:53 -0700806 mCachedBuffers[clientCacheId] = {hwcCacheSlot, mCounter++};
807 return hwcCacheSlot;
808}
809
rnleeed20fa42021-08-10 18:00:03 -0700810int BufferStateLayer::HwcSlotGenerator::getFreeHwcCacheSlot() REQUIRES(mMutex) {
Marissa Wall947d34e2019-03-29 14:03:53 -0700811 if (mFreeHwcCacheSlots.empty()) {
812 evictLeastRecentlyUsed();
813 }
814
rnleeed20fa42021-08-10 18:00:03 -0700815 int hwcCacheSlot = mFreeHwcCacheSlots.top();
Marissa Wall947d34e2019-03-29 14:03:53 -0700816 mFreeHwcCacheSlots.pop();
817 return hwcCacheSlot;
818}
819
820void BufferStateLayer::HwcSlotGenerator::evictLeastRecentlyUsed() REQUIRES(mMutex) {
821 uint64_t minCounter = UINT_MAX;
822 client_cache_t minClientCacheId = {};
823 for (const auto& [clientCacheId, slotCounter] : mCachedBuffers) {
824 const auto& [hwcCacheSlot, counter] = slotCounter;
825 if (counter < minCounter) {
826 minCounter = counter;
827 minClientCacheId = clientCacheId;
828 }
829 }
830 eraseBufferLocked(minClientCacheId);
831
832 ClientCache::getInstance().unregisterErasedRecipient(minClientCacheId, this);
833}
834
835void BufferStateLayer::HwcSlotGenerator::eraseBufferLocked(const client_cache_t& clientCacheId)
836 REQUIRES(mMutex) {
837 auto itr = mCachedBuffers.find(clientCacheId);
838 if (itr == mCachedBuffers.end()) {
839 return;
840 }
841 auto& [hwcCacheSlot, counter] = itr->second;
842
843 // TODO send to hwc cache and resources
844
845 mFreeHwcCacheSlots.push(hwcCacheSlot);
846 mCachedBuffers.erase(clientCacheId);
847}
chaviw4244e032019-09-04 11:27:49 -0700848
849void BufferStateLayer::gatherBufferInfo() {
chaviwdebadb82020-03-26 14:57:24 -0700850 BufferLayer::gatherBufferInfo();
chaviw4244e032019-09-04 11:27:49 -0700851
chaviwdebadb82020-03-26 14:57:24 -0700852 const State& s(getDrawingState());
chaviw4244e032019-09-04 11:27:49 -0700853 mBufferInfo.mDesiredPresentTime = s.desiredPresentTime;
854 mBufferInfo.mFenceTime = std::make_shared<FenceTime>(s.acquireFence);
855 mBufferInfo.mFence = s.acquireFence;
chaviw766c9c52021-02-10 17:36:47 -0800856 mBufferInfo.mTransform = s.bufferTransform;
Robert Carr167bdde2021-07-28 11:26:51 -0700857 auto lastDataspace = mBufferInfo.mDataspace;
chaviw4244e032019-09-04 11:27:49 -0700858 mBufferInfo.mDataspace = translateDataspace(s.dataspace);
Robert Carr167bdde2021-07-28 11:26:51 -0700859 if (lastDataspace != mBufferInfo.mDataspace) {
860 mFlinger->mSomeDataspaceChanged = true;
861 }
Vishnu Nair5cc9ac02021-04-19 13:23:38 -0700862 mBufferInfo.mCrop = computeBufferCrop(s);
chaviw4244e032019-09-04 11:27:49 -0700863 mBufferInfo.mScaleMode = NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW;
864 mBufferInfo.mSurfaceDamage = s.surfaceDamageRegion;
865 mBufferInfo.mHdrMetadata = s.hdrMetadata;
866 mBufferInfo.mApi = s.api;
chaviw4244e032019-09-04 11:27:49 -0700867 mBufferInfo.mTransformToDisplayInverse = s.transformToDisplayInverse;
chaviwf83ce182019-09-12 14:43:08 -0700868 mBufferInfo.mBufferSlot = mHwcSlotGenerator->getHwcCacheSlot(s.clientCacheId);
chaviw4244e032019-09-04 11:27:49 -0700869}
870
Robert Carr916b0362020-10-06 13:53:03 -0700871uint32_t BufferStateLayer::getEffectiveScalingMode() const {
872 return NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW;
873}
874
Vishnu Nair5cc9ac02021-04-19 13:23:38 -0700875Rect BufferStateLayer::computeBufferCrop(const State& s) {
chaviwf3f40fe2021-04-27 15:54:02 -0500876 if (s.buffer && !s.bufferCrop.isEmpty()) {
877 Rect bufferCrop;
878 s.buffer->getBuffer()->getBounds().intersect(s.bufferCrop, &bufferCrop);
879 return bufferCrop;
880 } else if (s.buffer) {
Alec Mouria90a5702021-04-16 16:36:21 +0000881 return s.buffer->getBuffer()->getBounds();
chaviwf3f40fe2021-04-27 15:54:02 -0500882 } else {
883 return s.bufferCrop;
chaviw4244e032019-09-04 11:27:49 -0700884 }
chaviw4244e032019-09-04 11:27:49 -0700885}
886
chaviwb4c6e582019-08-16 14:35:07 -0700887sp<Layer> BufferStateLayer::createClone() {
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700888 LayerCreationArgs args(mFlinger.get(), nullptr, mName + " (Mirror)", 0, 0, 0, LayerMetadata());
chaviwb4c6e582019-08-16 14:35:07 -0700889 args.textureName = mTextureName;
Lloyd Pique1c3a5eb2019-10-03 13:07:08 -0700890 sp<BufferStateLayer> layer = mFlinger->getFactory().createBufferStateLayer(args);
chaviwb4c6e582019-08-16 14:35:07 -0700891 layer->mHwcSlotGenerator = mHwcSlotGenerator;
892 layer->setInitialValuesForClone(this);
893 return layer;
894}
Valerie Hau92bf5482020-02-10 09:49:08 -0800895
Vishnu Naire7f79c52020-10-29 14:45:03 -0700896bool BufferStateLayer::bufferNeedsFiltering() const {
897 const State& s(getDrawingState());
898 if (!s.buffer) {
899 return false;
900 }
901
rnleeed20fa42021-08-10 18:00:03 -0700902 int32_t bufferWidth = s.buffer->getBuffer()->width;
903 int32_t bufferHeight = s.buffer->getBuffer()->height;
Vishnu Naire7f79c52020-10-29 14:45:03 -0700904
905 // Undo any transformations on the buffer and return the result.
chaviw766c9c52021-02-10 17:36:47 -0800906 if (s.bufferTransform & ui::Transform::ROT_90) {
Vishnu Naire7f79c52020-10-29 14:45:03 -0700907 std::swap(bufferWidth, bufferHeight);
908 }
909
910 if (s.transformToDisplayInverse) {
911 uint32_t invTransform = DisplayDevice::getPrimaryDisplayRotationFlags();
912 if (invTransform & ui::Transform::ROT_90) {
913 std::swap(bufferWidth, bufferHeight);
914 }
915 }
916
917 const Rect layerSize{getBounds()};
918 return layerSize.width() != bufferWidth || layerSize.height() != bufferHeight;
919}
Robert Carr7121caf2020-12-15 13:07:32 -0800920
Robert Carr7121caf2020-12-15 13:07:32 -0800921void BufferStateLayer::decrementPendingBufferCount() {
Vishnu Nair8eda69e2021-02-26 10:42:10 -0800922 int32_t pendingBuffers = --mPendingBufferTransactions;
923 tracePendingBufferCount(pendingBuffers);
Robert Carr7121caf2020-12-15 13:07:32 -0800924}
925
Vishnu Nair8eda69e2021-02-26 10:42:10 -0800926void BufferStateLayer::tracePendingBufferCount(int32_t pendingBuffers) {
927 ATRACE_INT(mBlastTransactionName.c_str(), pendingBuffers);
Robert Carr7121caf2020-12-15 13:07:32 -0800928}
929
Robert Carr7121caf2020-12-15 13:07:32 -0800930
chaviw39d01472021-04-08 14:26:24 -0500931/*
932 * We don't want to send the layer's transform to input, but rather the
933 * parent's transform. This is because BufferStateLayer's transform is
934 * information about how the buffer is placed on screen. The parent's
935 * transform makes more sense to send since it's information about how the
936 * layer is placed on screen. This transform is used by input to determine
937 * how to go from screen space back to window space.
938 */
939ui::Transform BufferStateLayer::getInputTransform() const {
Robert Carrcbdb79a2021-06-11 16:10:45 -0700940 auto parent = mDrawingParent;
chaviw39d01472021-04-08 14:26:24 -0500941 if (parent == nullptr) {
942 return ui::Transform();
943 }
944
945 return parent->getTransform();
946}
947
948/**
949 * Similar to getInputTransform, we need to update the bounds to include the transform.
950 * This is because bounds for BSL doesn't include buffer transform, where the input assumes
951 * that's already included.
952 */
953Rect BufferStateLayer::getInputBounds() const {
954 Rect bufferBounds = getCroppedBufferSize(getDrawingState());
955 if (mDrawingState.transform.getType() == ui::Transform::IDENTITY || !bufferBounds.isValid()) {
956 return bufferBounds;
957 }
958 return mDrawingState.transform.transform(bufferBounds);
959}
960
Marissa Wall61c58622018-07-18 10:12:20 -0700961} // namespace android