blob: 40fc342ec84fdb6c6b7035b21284f7ea0786f09a [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
Robert Carrde6d7b42022-01-07 18:23:06 -0800319bool BufferStateLayer::setMatrix(const layer_state_t::matrix22_t& matrix) {
Vishnu Nair6bdec7d2021-05-10 15:01:13 -0700320 if (mRequestedTransform.dsdx() == matrix.dsdx && mRequestedTransform.dtdy() == matrix.dtdy &&
321 mRequestedTransform.dtdx() == matrix.dtdx && mRequestedTransform.dsdy() == matrix.dsdy) {
Marissa Wall861616d2018-10-22 12:52:23 -0700322 return false;
323 }
324
Chavi Weingartena5aedbd2021-04-09 13:37:33 +0000325 ui::Transform t;
326 t.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
327
Vishnu Nair6bdec7d2021-05-10 15:01:13 -0700328 mRequestedTransform.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
chaviw9a93ea62021-03-11 16:44:42 -0600329
Robert Carr6a160312021-05-17 12:08:20 -0700330 mDrawingState.sequence++;
331 mDrawingState.modified = true;
chaviw9a93ea62021-03-11 16:44:42 -0600332 setTransactionFlags(eTransactionNeeded);
Chavi Weingartena5aedbd2021-04-09 13:37:33 +0000333
334 return true;
335}
336
337bool BufferStateLayer::setPosition(float x, float y) {
Vishnu Nair6bdec7d2021-05-10 15:01:13 -0700338 if (mRequestedTransform.tx() == x && mRequestedTransform.ty() == y) {
Chavi Weingartena5aedbd2021-04-09 13:37:33 +0000339 return false;
340 }
341
Vishnu Nair6bdec7d2021-05-10 15:01:13 -0700342 mRequestedTransform.set(x, y);
Chavi Weingartena5aedbd2021-04-09 13:37:33 +0000343
Robert Carr6a160312021-05-17 12:08:20 -0700344 mDrawingState.sequence++;
345 mDrawingState.modified = true;
Chavi Weingartena5aedbd2021-04-09 13:37:33 +0000346 setTransactionFlags(eTransactionNeeded);
347
Marissa Wall861616d2018-10-22 12:52:23 -0700348 return true;
349}
350
Valerie Hau871d6352020-01-29 08:44:02 -0800351bool BufferStateLayer::addFrameEvent(const sp<Fence>& acquireFence, nsecs_t postedTime,
352 nsecs_t desiredPresentTime) {
Valerie Haubf784642020-01-29 07:25:23 -0800353 Mutex::Autolock lock(mFrameEventHistoryMutex);
354 mAcquireTimeline.updateSignalTimes();
355 std::shared_ptr<FenceTime> acquireFenceTime =
356 std::make_shared<FenceTime>((acquireFence ? acquireFence : Fence::NO_FENCE));
Robert Carr6a160312021-05-17 12:08:20 -0700357 NewFrameEventsEntry newTimestamps = {mDrawingState.frameNumber, postedTime, desiredPresentTime,
Valerie Haubf784642020-01-29 07:25:23 -0800358 acquireFenceTime};
Valerie Hau871d6352020-01-29 08:44:02 -0800359 mFrameEventHistory.setProducerWantsEvents();
Valerie Haubf784642020-01-29 07:25:23 -0800360 mFrameEventHistory.addQueue(newTimestamps);
361 return true;
362}
363
Vishnu Nairdbbe3852022-01-12 20:22:11 -0800364bool BufferStateLayer::setBuffer(std::shared_ptr<renderengine::ExternalTexture>& buffer,
365 const BufferData& bufferData, nsecs_t postTime,
Alec Mouria90a5702021-04-16 16:36:21 +0000366 nsecs_t desiredPresentTime, bool isAutoTimestamp,
chaviwba4320c2021-09-15 15:20:53 -0500367 std::optional<nsecs_t> dequeueTime,
368 const FrameTimelineInfo& info) {
Alex Chaucf6b4b42021-12-07 10:48:52 +0000369 ATRACE_CALL();
Robert Carr0c1966e2020-10-19 12:12:08 -0700370
chaviwba4320c2021-09-15 15:20:53 -0500371 if (!buffer) {
372 return false;
373 }
374
375 const bool frameNumberChanged =
376 bufferData.flags.test(BufferData::BufferDataChange::frameNumberChanged);
377 const uint64_t frameNumber =
378 frameNumberChanged ? bufferData.frameNumber : mDrawingState.frameNumber + 1;
379
Robert Carr6a160312021-05-17 12:08:20 -0700380 if (mDrawingState.buffer) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700381 mReleasePreviousBuffer = true;
Vishnu Nairdbbe3852022-01-12 20:22:11 -0800382 if (!mBufferInfo.mBuffer ||
383 (!mDrawingState.buffer->hasSameBuffer(*mBufferInfo.mBuffer) ||
384 mDrawingState.frameNumber != mBufferInfo.mFrameNumber)) {
Robert Carr6a160312021-05-17 12:08:20 -0700385 // If mDrawingState has a buffer, and we are about to update again
Robert Carr7121caf2020-12-15 13:07:32 -0800386 // before swapping to drawing state, then the first buffer will be
Vishnu Nair1506b182021-02-22 14:35:15 -0800387 // dropped and we should decrement the pending buffer count and
388 // call any release buffer callbacks if set.
Robert Carr6a160312021-05-17 12:08:20 -0700389 callReleaseBufferCallback(mDrawingState.releaseBufferListener,
Vishnu Nair4ba0c2e2021-06-24 11:27:17 -0700390 mDrawingState.buffer->getBuffer(), mDrawingState.frameNumber,
chaviw69058fb2021-09-27 09:37:30 -0500391 mDrawingState.acquireFence,
Ady Abraham899dcdb2021-06-15 16:56:21 -0700392 mFlinger->getMaxAcquiredBufferCountForCurrentRefreshRate(
393 mOwnerUid));
Robert Carr7121caf2020-12-15 13:07:32 -0800394 decrementPendingBufferCount();
Robert Carr6a160312021-05-17 12:08:20 -0700395 if (mDrawingState.bufferSurfaceFrameTX != nullptr &&
396 mDrawingState.bufferSurfaceFrameTX->getPresentState() != PresentState::Presented) {
397 addSurfaceFrameDroppedForBuffer(mDrawingState.bufferSurfaceFrameTX);
398 mDrawingState.bufferSurfaceFrameTX.reset();
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +0000399 }
Robert Carrccab4242021-09-28 16:53:03 -0700400 } else if (mLastClientCompositionFence != nullptr) {
401 callReleaseBufferCallback(mDrawingState.releaseBufferListener,
402 mDrawingState.buffer->getBuffer(), mDrawingState.frameNumber,
chaviw69058fb2021-09-27 09:37:30 -0500403 mLastClientCompositionFence,
Robert Carrccab4242021-09-28 16:53:03 -0700404 mFlinger->getMaxAcquiredBufferCountForCurrentRefreshRate(
chaviw69058fb2021-09-27 09:37:30 -0500405 mOwnerUid));
Robert Carrccab4242021-09-28 16:53:03 -0700406 mLastClientCompositionFence = nullptr;
Robert Carr7121caf2020-12-15 13:07:32 -0800407 }
Marissa Wallfda30bb2018-10-12 11:34:28 -0700408 }
Robert Carr6a160312021-05-17 12:08:20 -0700409
410 mDrawingState.frameNumber = frameNumber;
chaviwba4320c2021-09-15 15:20:53 -0500411 mDrawingState.releaseBufferListener = bufferData.releaseBufferListener;
Vishnu Nairdbbe3852022-01-12 20:22:11 -0800412 mDrawingState.buffer = std::move(buffer);
chaviwba4320c2021-09-15 15:20:53 -0500413 mDrawingState.clientCacheId = bufferData.cachedBuffer;
414
415 mDrawingState.acquireFence = bufferData.flags.test(BufferData::BufferDataChange::fenceChanged)
416 ? bufferData.acquireFence
417 : Fence::NO_FENCE;
418 mDrawingState.acquireFenceTime = std::make_unique<FenceTime>(mDrawingState.acquireFence);
419 // The acquire fences of BufferStateLayers have already signaled before they are set
420 mCallbackHandleAcquireTime = mDrawingState.acquireFenceTime->getSignalTime();
421
Robert Carr6a160312021-05-17 12:08:20 -0700422 mDrawingState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700423 setTransactionFlags(eTransactionNeeded);
Ady Abraham09bd3922019-04-08 10:44:56 -0700424
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800425 const int32_t layerId = getSequence();
Robert Carr6a160312021-05-17 12:08:20 -0700426 mFlinger->mTimeStats->setPostTime(layerId, mDrawingState.frameNumber, getName().c_str(),
Adithya Srinivasan58069dc2021-06-04 20:37:02 +0000427 mOwnerUid, postTime, getGameMode());
Robert Carr6a160312021-05-17 12:08:20 -0700428 mDrawingState.desiredPresentTime = desiredPresentTime;
429 mDrawingState.isAutoTimestamp = isAutoTimestamp;
Ady Abraham09bd3922019-04-08 10:44:56 -0700430
Ady Abrahamb7f15562021-03-15 18:34:08 -0700431 const nsecs_t presentTime = [&] {
432 if (!isAutoTimestamp) return desiredPresentTime;
433
434 const auto prediction =
435 mFlinger->mFrameTimeline->getTokenManager()->getPredictionsForToken(info.vsyncId);
436 if (prediction.has_value()) return prediction->presentTime;
437
438 return static_cast<nsecs_t>(0);
439 }();
Dominik Laskowski068173d2021-08-11 17:22:59 -0700440
441 using LayerUpdateType = scheduler::LayerHistory::LayerUpdateType;
442 mFlinger->mScheduler->recordLayerHistory(this, presentTime, LayerUpdateType::Buffer);
Ady Abraham09bd3922019-04-08 10:44:56 -0700443
chaviwba4320c2021-09-15 15:20:53 -0500444 addFrameEvent(mDrawingState.acquireFence, postTime, isAutoTimestamp ? 0 : desiredPresentTime);
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +0000445
Adithya Srinivasan891004e2021-02-12 20:20:47 +0000446 setFrameTimelineVsyncForBufferTransaction(info, postTime);
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +0000447
Vishnu Nair26c49762022-01-18 22:58:52 +0000448 if (dequeueTime && *dequeueTime != 0) {
449 const uint64_t bufferId = mDrawingState.buffer->getId();
Adithya Srinivasanb238cd52021-02-04 17:54:05 +0000450 mFlinger->mFrameTracer->traceNewLayer(layerId, getName().c_str());
451 mFlinger->mFrameTracer->traceTimestamp(layerId, bufferId, frameNumber, *dequeueTime,
452 FrameTracer::FrameEvent::DEQUEUE);
453 mFlinger->mFrameTracer->traceTimestamp(layerId, bufferId, frameNumber, postTime,
454 FrameTracer::FrameEvent::QUEUE);
455 }
Chavi Weingartena5aedbd2021-04-09 13:37:33 +0000456
Vishnu Nairdbbe3852022-01-12 20:22:11 -0800457 mDrawingState.width = mDrawingState.buffer->getWidth();
458 mDrawingState.height = mDrawingState.buffer->getHeight();
chaviwba4320c2021-09-15 15:20:53 -0500459 mDrawingState.releaseBufferEndpoint = bufferData.releaseBufferEndpoint;
Marissa Wall61c58622018-07-18 10:12:20 -0700460 return true;
461}
462
463bool BufferStateLayer::setDataspace(ui::Dataspace dataspace) {
Robert Carr6a160312021-05-17 12:08:20 -0700464 if (mDrawingState.dataspace == dataspace) return false;
465 mDrawingState.dataspace = dataspace;
466 mDrawingState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700467 setTransactionFlags(eTransactionNeeded);
468 return true;
469}
470
471bool BufferStateLayer::setHdrMetadata(const HdrMetadata& hdrMetadata) {
Robert Carr6a160312021-05-17 12:08:20 -0700472 if (mDrawingState.hdrMetadata == hdrMetadata) return false;
473 mDrawingState.hdrMetadata = hdrMetadata;
474 mDrawingState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700475 setTransactionFlags(eTransactionNeeded);
476 return true;
477}
478
479bool BufferStateLayer::setSurfaceDamageRegion(const Region& surfaceDamage) {
Robert Carr6a160312021-05-17 12:08:20 -0700480 mDrawingState.surfaceDamageRegion = surfaceDamage;
481 mDrawingState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700482 setTransactionFlags(eTransactionNeeded);
483 return true;
484}
485
486bool BufferStateLayer::setApi(int32_t api) {
Robert Carr6a160312021-05-17 12:08:20 -0700487 if (mDrawingState.api == api) return false;
488 mDrawingState.api = api;
489 mDrawingState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700490 setTransactionFlags(eTransactionNeeded);
491 return true;
492}
493
494bool BufferStateLayer::setSidebandStream(const sp<NativeHandle>& sidebandStream) {
Robert Carr6a160312021-05-17 12:08:20 -0700495 if (mDrawingState.sidebandStream == sidebandStream) return false;
Robert Carr3e2a2992021-06-11 13:42:55 -0700496
497 if (mDrawingState.sidebandStream != nullptr && sidebandStream == nullptr) {
498 mFlinger->mTunnelModeEnabledReporter->decrementTunnelModeCount();
499 } else if (sidebandStream != nullptr) {
500 mFlinger->mTunnelModeEnabledReporter->incrementTunnelModeCount();
501 }
502
Robert Carr6a160312021-05-17 12:08:20 -0700503 mDrawingState.sidebandStream = sidebandStream;
504 mDrawingState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700505 setTransactionFlags(eTransactionNeeded);
Marissa Wall61c58622018-07-18 10:12:20 -0700506 if (!mSidebandStreamChanged.exchange(true)) {
507 // mSidebandStreamChanged was false
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -0700508 mFlinger->onLayerUpdate();
Marissa Wall61c58622018-07-18 10:12:20 -0700509 }
510 return true;
511}
512
Marissa Walle2ffb422018-10-12 11:33:52 -0700513bool BufferStateLayer::setTransactionCompletedListeners(
Jiakai Zhanga5505cb2021-11-09 11:46:30 +0000514 const std::vector<sp<CallbackHandle>>& handles) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700515 // If there is no handle, we will not send a callback so reset mReleasePreviousBuffer and return
Jiakai Zhanga5505cb2021-11-09 11:46:30 +0000516 if (handles.empty()) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700517 mReleasePreviousBuffer = false;
Marissa Walle2ffb422018-10-12 11:33:52 -0700518 return false;
519 }
520
521 const bool willPresent = willPresentCurrentTransaction();
522
523 for (const auto& handle : handles) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700524 // If this transaction set a buffer on this layer, release its previous buffer
525 handle->releasePreviousBuffer = mReleasePreviousBuffer;
526
Marissa Walle2ffb422018-10-12 11:33:52 -0700527 // If this layer will be presented in this frame
528 if (willPresent) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700529 // If this transaction set an acquire fence on this layer, set its acquire time
530 handle->acquireTime = mCallbackHandleAcquireTime;
Robert Carr6a160312021-05-17 12:08:20 -0700531 handle->frameNumber = mDrawingState.frameNumber;
Marissa Wallfda30bb2018-10-12 11:34:28 -0700532
Marissa Walle2ffb422018-10-12 11:33:52 -0700533 // Store so latched time and release fence can be set
Robert Carr6a160312021-05-17 12:08:20 -0700534 mDrawingState.callbackHandles.push_back(handle);
Marissa Walle2ffb422018-10-12 11:33:52 -0700535
Jiakai Zhanga5505cb2021-11-09 11:46:30 +0000536 } else { // If this layer will NOT need to be relatched and presented this frame
Marissa Walle2ffb422018-10-12 11:33:52 -0700537 // Notify the transaction completed thread this handle is done
Jiakai Zhanga5505cb2021-11-09 11:46:30 +0000538 mFlinger->getTransactionCallbackInvoker().registerUnpresentedCallbackHandle(handle);
Marissa Walle2ffb422018-10-12 11:33:52 -0700539 }
540 }
541
Marissa Wallfda30bb2018-10-12 11:34:28 -0700542 mReleasePreviousBuffer = false;
543 mCallbackHandleAcquireTime = -1;
544
Marissa Walle2ffb422018-10-12 11:33:52 -0700545 return willPresent;
546}
547
Marissa Wall61c58622018-07-18 10:12:20 -0700548bool BufferStateLayer::setTransparentRegionHint(const Region& transparent) {
Vishnu Nair27e3ed52021-07-08 18:24:25 -0700549 mDrawingState.sequence++;
Robert Carr6a160312021-05-17 12:08:20 -0700550 mDrawingState.transparentRegionHint = transparent;
551 mDrawingState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700552 setTransactionFlags(eTransactionNeeded);
553 return true;
554}
555
rnleeed20fa42021-08-10 18:00:03 -0700556Rect BufferStateLayer::getBufferSize(const State& /*s*/) const {
Marissa Wall861616d2018-10-22 12:52:23 -0700557 // for buffer state layers we use the display frame size as the buffer size.
Marissa Wall61c58622018-07-18 10:12:20 -0700558
chaviw7e72caf2020-12-02 16:50:43 -0800559 if (mBufferInfo.mBuffer == nullptr) {
560 return Rect::INVALID_RECT;
561 }
562
Vishnu Nairdbbe3852022-01-12 20:22:11 -0800563 uint32_t bufWidth = mBufferInfo.mBuffer->getWidth();
564 uint32_t bufHeight = mBufferInfo.mBuffer->getHeight();
Vishnu Nair6bdec7d2021-05-10 15:01:13 -0700565
566 // Undo any transformations on the buffer and return the result.
567 if (mBufferInfo.mTransform & ui::Transform::ROT_90) {
568 std::swap(bufWidth, bufHeight);
569 }
570
571 if (getTransformToDisplayInverse()) {
572 uint32_t invTransform = DisplayDevice::getPrimaryDisplayRotationFlags();
573 if (invTransform & ui::Transform::ROT_90) {
574 std::swap(bufWidth, bufHeight);
Marissa Wall861616d2018-10-22 12:52:23 -0700575 }
576 }
577
rnleeed20fa42021-08-10 18:00:03 -0700578 return Rect(0, 0, static_cast<int32_t>(bufWidth), static_cast<int32_t>(bufHeight));
Marissa Wall61c58622018-07-18 10:12:20 -0700579}
Vishnu Nair4351ad52019-02-11 14:13:02 -0800580
581FloatRect BufferStateLayer::computeSourceBounds(const FloatRect& parentBounds) const {
Vishnu Nair6bdec7d2021-05-10 15:01:13 -0700582 if (mBufferInfo.mBuffer == nullptr) {
583 return parentBounds;
Vishnu Nair4351ad52019-02-11 14:13:02 -0800584 }
585
Vishnu Nair6bdec7d2021-05-10 15:01:13 -0700586 return getBufferSize(getDrawingState()).toFloatRect();
Vishnu Nair4351ad52019-02-11 14:13:02 -0800587}
588
Marissa Wall61c58622018-07-18 10:12:20 -0700589// -----------------------------------------------------------------------
590
591// -----------------------------------------------------------------------
592// Interface implementation for BufferLayer
593// -----------------------------------------------------------------------
594bool BufferStateLayer::fenceHasSignaled() const {
ramindani4d48f902021-09-20 21:07:45 +0000595 if (SurfaceFlinger::enableLatchUnsignaledConfig != LatchUnsignaledConfig::Disabled) {
Huihong Luo86c80e32021-06-16 15:41:07 -0700596 return true;
597 }
598
Alec Mouri91f6df32020-01-30 08:48:58 -0800599 const bool fenceSignaled =
600 getDrawingState().acquireFence->getStatus() == Fence::Status::Signaled;
601 if (!fenceSignaled) {
602 mFlinger->mTimeStats->incrementLatchSkipped(getSequence(),
603 TimeStats::LatchSkipReason::LateAcquire);
604 }
605
606 return fenceSignaled;
Marissa Wall61c58622018-07-18 10:12:20 -0700607}
608
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700609bool BufferStateLayer::framePresentTimeIsCurrent(nsecs_t expectedPresentTime) const {
Ady Abrahamcd1580c2019-04-29 15:40:03 -0700610 if (!hasFrameUpdate() || isRemovedFromCurrentState()) {
611 return true;
612 }
613
Robert Carr6a160312021-05-17 12:08:20 -0700614 return mDrawingState.isAutoTimestamp || mDrawingState.desiredPresentTime <= expectedPresentTime;
Ady Abrahamcd1580c2019-04-29 15:40:03 -0700615}
616
Valerie Hau871d6352020-01-29 08:44:02 -0800617bool BufferStateLayer::onPreComposition(nsecs_t refreshStartTime) {
618 for (const auto& handle : mDrawingState.callbackHandles) {
619 handle->refreshStartTime = refreshStartTime;
620 }
621 return BufferLayer::onPreComposition(refreshStartTime);
622}
623
Vishnu Naircf26a0a2020-11-13 12:56:20 -0800624void BufferStateLayer::setAutoRefresh(bool autoRefresh) {
Vishnu Nair86653e92021-11-03 17:19:36 -0700625 mDrawingState.autoRefresh = autoRefresh;
Marissa Wall61c58622018-07-18 10:12:20 -0700626}
627
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800628bool BufferStateLayer::latchSidebandStream(bool& recomputeVisibleRegions) {
baocheng suna663c2b2021-05-13 18:51:28 +0800629 // 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 +0800630 editCompositionState()->sidebandStreamHasFrame = hasFrameUpdate() && mSidebandStream.get();
baocheng suna663c2b2021-05-13 18:51:28 +0800631
baocheng sun9691b9c2021-08-03 19:27:06 +0800632 if (mSidebandStreamChanged.exchange(false)) {
Marissa Wall61c58622018-07-18 10:12:20 -0700633 const State& s(getDrawingState());
634 // mSidebandStreamChanged was true
Lloyd Pique0b785d82018-12-04 17:25:27 -0800635 mSidebandStream = s.sidebandStream;
Lloyd Piquede196652020-01-22 17:29:58 -0800636 editCompositionState()->sidebandStream = mSidebandStream;
Lloyd Pique0b785d82018-12-04 17:25:27 -0800637 if (mSidebandStream != nullptr) {
Marissa Wall61c58622018-07-18 10:12:20 -0700638 setTransactionFlags(eTransactionNeeded);
639 mFlinger->setTransactionFlags(eTraversalNeeded);
640 }
641 recomputeVisibleRegions = true;
642
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800643 return true;
Marissa Wall61c58622018-07-18 10:12:20 -0700644 }
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800645 return false;
Marissa Wall61c58622018-07-18 10:12:20 -0700646}
647
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800648bool BufferStateLayer::hasFrameUpdate() const {
Robert Carr6a160312021-05-17 12:08:20 -0700649 const State& c(getDrawingState());
Robert Carr315f3c72021-06-24 21:58:09 -0700650 return (mDrawingStateModified || mDrawingState.modified) && (c.buffer != nullptr || c.bgColorLayer != nullptr);
Marissa Wall61c58622018-07-18 10:12:20 -0700651}
652
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700653status_t BufferStateLayer::updateTexImage(bool& /*recomputeVisibleRegions*/, nsecs_t latchTime,
654 nsecs_t /*expectedPresentTime*/) {
Marissa Wall61c58622018-07-18 10:12:20 -0700655 const State& s(getDrawingState());
656
657 if (!s.buffer) {
Valerie Hauaa194562019-02-05 16:21:38 -0800658 if (s.bgColorLayer) {
659 for (auto& handle : mDrawingState.callbackHandles) {
660 handle->latchTime = latchTime;
661 }
662 }
Marissa Wall61c58622018-07-18 10:12:20 -0700663 return NO_ERROR;
664 }
665
Marissa Wall5a68a772018-12-22 17:43:42 -0800666 for (auto& handle : mDrawingState.callbackHandles) {
Vishnu Nair935590e2021-02-10 13:05:52 -0800667 if (handle->frameNumber == mDrawingState.frameNumber) {
668 handle->latchTime = latchTime;
669 }
Marissa Wall5a68a772018-12-22 17:43:42 -0800670 }
Marissa Walle2ffb422018-10-12 11:33:52 -0700671
Vishnu Nairea0de002020-11-17 17:42:37 -0800672 const int32_t layerId = getSequence();
Vishnu Nairdbbe3852022-01-12 20:22:11 -0800673 const uint64_t bufferId = mDrawingState.buffer->getId();
Adithya Srinivasanb238cd52021-02-04 17:54:05 +0000674 const uint64_t frameNumber = mDrawingState.frameNumber;
675 const auto acquireFence = std::make_shared<FenceTime>(mDrawingState.acquireFence);
676 mFlinger->mTimeStats->setAcquireFence(layerId, frameNumber, acquireFence);
677 mFlinger->mTimeStats->setLatchTime(layerId, frameNumber, latchTime);
678
679 mFlinger->mFrameTracer->traceFence(layerId, bufferId, frameNumber, acquireFence,
680 FrameTracer::FrameEvent::ACQUIRE_FENCE);
681 mFlinger->mFrameTracer->traceTimestamp(layerId, bufferId, frameNumber, latchTime,
682 FrameTracer::FrameEvent::LATCH);
Marissa Wall61c58622018-07-18 10:12:20 -0700683
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +0000684 auto& bufferSurfaceFrame = mDrawingState.bufferSurfaceFrameTX;
685 if (bufferSurfaceFrame != nullptr &&
686 bufferSurfaceFrame->getPresentState() != PresentState::Presented) {
687 // Update only if the bufferSurfaceFrame wasn't already presented. A Presented
688 // bufferSurfaceFrame could be seen here if a pending state was applied successfully and we
689 // are processing the next state.
690 addSurfaceFramePresentedForBuffer(bufferSurfaceFrame,
Ady Abraham6c1b7ac2021-03-31 16:56:03 -0700691 mDrawingState.acquireFenceTime->getSignalTime(),
692 latchTime);
Robert Carr6a160312021-05-17 12:08:20 -0700693 mDrawingState.bufferSurfaceFrameTX.reset();
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +0000694 }
695
Vishnu Nairfc46c1e2021-04-21 08:31:32 -0700696 std::deque<sp<CallbackHandle>> remainingHandles;
697 mFlinger->getTransactionCallbackInvoker()
Robert Carr3d1047b2021-09-20 18:22:32 -0700698 .addOnCommitCallbackHandles(mDrawingState.callbackHandles, remainingHandles);
Vishnu Nairfc46c1e2021-04-21 08:31:32 -0700699 mDrawingState.callbackHandles = remainingHandles;
700
Robert Carr6a160312021-05-17 12:08:20 -0700701 mDrawingStateModified = false;
Marissa Wall16c112d2019-03-20 13:21:13 -0700702
Marissa Wall61c58622018-07-18 10:12:20 -0700703 return NO_ERROR;
704}
705
706status_t BufferStateLayer::updateActiveBuffer() {
707 const State& s(getDrawingState());
708
709 if (s.buffer == nullptr) {
710 return BAD_VALUE;
711 }
chaviwdf3c5e82021-01-07 13:00:37 -0800712
Vishnu Nairdbbe3852022-01-12 20:22:11 -0800713 if (!mBufferInfo.mBuffer || !s.buffer->hasSameBuffer(*mBufferInfo.mBuffer)) {
chaviwdf3c5e82021-01-07 13:00:37 -0800714 decrementPendingBufferCount();
715 }
Marissa Wall61c58622018-07-18 10:12:20 -0700716
Vishnu Nair4ba0c2e2021-06-24 11:27:17 -0700717 mPreviousReleaseCallbackId = {getCurrentBufferId(), mBufferInfo.mFrameNumber};
chaviwd62d3062019-09-04 14:48:02 -0700718 mBufferInfo.mBuffer = s.buffer;
719 mBufferInfo.mFence = s.acquireFence;
Vishnu Nair4ba0c2e2021-06-24 11:27:17 -0700720 mBufferInfo.mFrameNumber = s.frameNumber;
Marissa Wall61c58622018-07-18 10:12:20 -0700721
722 return NO_ERROR;
723}
724
Valerie Haubf784642020-01-29 07:25:23 -0800725status_t BufferStateLayer::updateFrameNumber(nsecs_t latchTime) {
Marissa Wall61c58622018-07-18 10:12:20 -0700726 // TODO(marissaw): support frame history events
Mikael Pessa2e1608f2019-07-19 11:25:35 -0700727 mPreviousFrameNumber = mCurrentFrameNumber;
Valerie Hau134651a2020-01-28 16:21:22 -0800728 mCurrentFrameNumber = mDrawingState.frameNumber;
Valerie Haubf784642020-01-29 07:25:23 -0800729 {
730 Mutex::Autolock lock(mFrameEventHistoryMutex);
731 mFrameEventHistory.addLatch(mCurrentFrameNumber, latchTime);
732 }
Marissa Wall61c58622018-07-18 10:12:20 -0700733 return NO_ERROR;
734}
735
Marissa Wall947d34e2019-03-29 14:03:53 -0700736void BufferStateLayer::HwcSlotGenerator::bufferErased(const client_cache_t& clientCacheId) {
737 std::lock_guard lock(mMutex);
738 if (!clientCacheId.isValid()) {
739 ALOGE("invalid process, failed to erase buffer");
740 return;
741 }
742 eraseBufferLocked(clientCacheId);
743}
744
rnleeed20fa42021-08-10 18:00:03 -0700745int BufferStateLayer::HwcSlotGenerator::getHwcCacheSlot(const client_cache_t& clientCacheId) {
Marissa Wall947d34e2019-03-29 14:03:53 -0700746 std::lock_guard<std::mutex> lock(mMutex);
747 auto itr = mCachedBuffers.find(clientCacheId);
748 if (itr == mCachedBuffers.end()) {
749 return addCachedBuffer(clientCacheId);
750 }
751 auto& [hwcCacheSlot, counter] = itr->second;
752 counter = mCounter++;
753 return hwcCacheSlot;
754}
755
rnleeed20fa42021-08-10 18:00:03 -0700756int BufferStateLayer::HwcSlotGenerator::addCachedBuffer(const client_cache_t& clientCacheId)
Marissa Wall947d34e2019-03-29 14:03:53 -0700757 REQUIRES(mMutex) {
758 if (!clientCacheId.isValid()) {
759 ALOGE("invalid process, returning invalid slot");
760 return BufferQueue::INVALID_BUFFER_SLOT;
761 }
762
763 ClientCache::getInstance().registerErasedRecipient(clientCacheId, wp<ErasedRecipient>(this));
764
rnleeed20fa42021-08-10 18:00:03 -0700765 int hwcCacheSlot = getFreeHwcCacheSlot();
Marissa Wall947d34e2019-03-29 14:03:53 -0700766 mCachedBuffers[clientCacheId] = {hwcCacheSlot, mCounter++};
767 return hwcCacheSlot;
768}
769
rnleeed20fa42021-08-10 18:00:03 -0700770int BufferStateLayer::HwcSlotGenerator::getFreeHwcCacheSlot() REQUIRES(mMutex) {
Marissa Wall947d34e2019-03-29 14:03:53 -0700771 if (mFreeHwcCacheSlots.empty()) {
772 evictLeastRecentlyUsed();
773 }
774
rnleeed20fa42021-08-10 18:00:03 -0700775 int hwcCacheSlot = mFreeHwcCacheSlots.top();
Marissa Wall947d34e2019-03-29 14:03:53 -0700776 mFreeHwcCacheSlots.pop();
777 return hwcCacheSlot;
778}
779
780void BufferStateLayer::HwcSlotGenerator::evictLeastRecentlyUsed() REQUIRES(mMutex) {
781 uint64_t minCounter = UINT_MAX;
782 client_cache_t minClientCacheId = {};
783 for (const auto& [clientCacheId, slotCounter] : mCachedBuffers) {
784 const auto& [hwcCacheSlot, counter] = slotCounter;
785 if (counter < minCounter) {
786 minCounter = counter;
787 minClientCacheId = clientCacheId;
788 }
789 }
790 eraseBufferLocked(minClientCacheId);
791
792 ClientCache::getInstance().unregisterErasedRecipient(minClientCacheId, this);
793}
794
795void BufferStateLayer::HwcSlotGenerator::eraseBufferLocked(const client_cache_t& clientCacheId)
796 REQUIRES(mMutex) {
797 auto itr = mCachedBuffers.find(clientCacheId);
798 if (itr == mCachedBuffers.end()) {
799 return;
800 }
801 auto& [hwcCacheSlot, counter] = itr->second;
802
803 // TODO send to hwc cache and resources
804
805 mFreeHwcCacheSlots.push(hwcCacheSlot);
806 mCachedBuffers.erase(clientCacheId);
807}
chaviw4244e032019-09-04 11:27:49 -0700808
809void BufferStateLayer::gatherBufferInfo() {
chaviwdebadb82020-03-26 14:57:24 -0700810 BufferLayer::gatherBufferInfo();
chaviw4244e032019-09-04 11:27:49 -0700811
chaviwdebadb82020-03-26 14:57:24 -0700812 const State& s(getDrawingState());
chaviw4244e032019-09-04 11:27:49 -0700813 mBufferInfo.mDesiredPresentTime = s.desiredPresentTime;
814 mBufferInfo.mFenceTime = std::make_shared<FenceTime>(s.acquireFence);
815 mBufferInfo.mFence = s.acquireFence;
chaviw766c9c52021-02-10 17:36:47 -0800816 mBufferInfo.mTransform = s.bufferTransform;
Robert Carr167bdde2021-07-28 11:26:51 -0700817 auto lastDataspace = mBufferInfo.mDataspace;
chaviw4244e032019-09-04 11:27:49 -0700818 mBufferInfo.mDataspace = translateDataspace(s.dataspace);
Robert Carr167bdde2021-07-28 11:26:51 -0700819 if (lastDataspace != mBufferInfo.mDataspace) {
820 mFlinger->mSomeDataspaceChanged = true;
821 }
Vishnu Nair5cc9ac02021-04-19 13:23:38 -0700822 mBufferInfo.mCrop = computeBufferCrop(s);
chaviw4244e032019-09-04 11:27:49 -0700823 mBufferInfo.mScaleMode = NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW;
824 mBufferInfo.mSurfaceDamage = s.surfaceDamageRegion;
825 mBufferInfo.mHdrMetadata = s.hdrMetadata;
826 mBufferInfo.mApi = s.api;
chaviw4244e032019-09-04 11:27:49 -0700827 mBufferInfo.mTransformToDisplayInverse = s.transformToDisplayInverse;
chaviwf83ce182019-09-12 14:43:08 -0700828 mBufferInfo.mBufferSlot = mHwcSlotGenerator->getHwcCacheSlot(s.clientCacheId);
chaviw4244e032019-09-04 11:27:49 -0700829}
830
Robert Carr916b0362020-10-06 13:53:03 -0700831uint32_t BufferStateLayer::getEffectiveScalingMode() const {
832 return NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW;
833}
834
Vishnu Nair5cc9ac02021-04-19 13:23:38 -0700835Rect BufferStateLayer::computeBufferCrop(const State& s) {
chaviwf3f40fe2021-04-27 15:54:02 -0500836 if (s.buffer && !s.bufferCrop.isEmpty()) {
837 Rect bufferCrop;
Vishnu Nairdbbe3852022-01-12 20:22:11 -0800838 s.buffer->getBounds().intersect(s.bufferCrop, &bufferCrop);
chaviwf3f40fe2021-04-27 15:54:02 -0500839 return bufferCrop;
840 } else if (s.buffer) {
Vishnu Nairdbbe3852022-01-12 20:22:11 -0800841 return s.buffer->getBounds();
chaviwf3f40fe2021-04-27 15:54:02 -0500842 } else {
843 return s.bufferCrop;
chaviw4244e032019-09-04 11:27:49 -0700844 }
chaviw4244e032019-09-04 11:27:49 -0700845}
846
chaviwb4c6e582019-08-16 14:35:07 -0700847sp<Layer> BufferStateLayer::createClone() {
Vishnu Nair7fb9e5a2021-11-08 12:44:05 -0800848 LayerCreationArgs args(mFlinger.get(), nullptr, mName + " (Mirror)", 0, LayerMetadata());
chaviwb4c6e582019-08-16 14:35:07 -0700849 args.textureName = mTextureName;
Lloyd Pique1c3a5eb2019-10-03 13:07:08 -0700850 sp<BufferStateLayer> layer = mFlinger->getFactory().createBufferStateLayer(args);
chaviwb4c6e582019-08-16 14:35:07 -0700851 layer->mHwcSlotGenerator = mHwcSlotGenerator;
852 layer->setInitialValuesForClone(this);
853 return layer;
854}
Valerie Hau92bf5482020-02-10 09:49:08 -0800855
Vishnu Naire7f79c52020-10-29 14:45:03 -0700856bool BufferStateLayer::bufferNeedsFiltering() const {
857 const State& s(getDrawingState());
858 if (!s.buffer) {
859 return false;
860 }
861
Vishnu Nairdbbe3852022-01-12 20:22:11 -0800862 int32_t bufferWidth = static_cast<int32_t>(s.buffer->getWidth());
863 int32_t bufferHeight = static_cast<int32_t>(s.buffer->getHeight());
Vishnu Naire7f79c52020-10-29 14:45:03 -0700864
865 // Undo any transformations on the buffer and return the result.
chaviw766c9c52021-02-10 17:36:47 -0800866 if (s.bufferTransform & ui::Transform::ROT_90) {
Vishnu Naire7f79c52020-10-29 14:45:03 -0700867 std::swap(bufferWidth, bufferHeight);
868 }
869
870 if (s.transformToDisplayInverse) {
871 uint32_t invTransform = DisplayDevice::getPrimaryDisplayRotationFlags();
872 if (invTransform & ui::Transform::ROT_90) {
873 std::swap(bufferWidth, bufferHeight);
874 }
875 }
876
877 const Rect layerSize{getBounds()};
878 return layerSize.width() != bufferWidth || layerSize.height() != bufferHeight;
879}
Robert Carr7121caf2020-12-15 13:07:32 -0800880
Robert Carr7121caf2020-12-15 13:07:32 -0800881void BufferStateLayer::decrementPendingBufferCount() {
Vishnu Nair8eda69e2021-02-26 10:42:10 -0800882 int32_t pendingBuffers = --mPendingBufferTransactions;
883 tracePendingBufferCount(pendingBuffers);
Robert Carr7121caf2020-12-15 13:07:32 -0800884}
885
Vishnu Nair8eda69e2021-02-26 10:42:10 -0800886void BufferStateLayer::tracePendingBufferCount(int32_t pendingBuffers) {
887 ATRACE_INT(mBlastTransactionName.c_str(), pendingBuffers);
Robert Carr7121caf2020-12-15 13:07:32 -0800888}
889
Robert Carr7121caf2020-12-15 13:07:32 -0800890
chaviw39d01472021-04-08 14:26:24 -0500891/*
892 * We don't want to send the layer's transform to input, but rather the
893 * parent's transform. This is because BufferStateLayer's transform is
894 * information about how the buffer is placed on screen. The parent's
895 * transform makes more sense to send since it's information about how the
896 * layer is placed on screen. This transform is used by input to determine
897 * how to go from screen space back to window space.
898 */
899ui::Transform BufferStateLayer::getInputTransform() const {
Rob Carrc6d2d2b2021-10-25 16:51:49 +0000900 sp<Layer> parent = mDrawingParent.promote();
chaviw39d01472021-04-08 14:26:24 -0500901 if (parent == nullptr) {
902 return ui::Transform();
903 }
904
905 return parent->getTransform();
906}
907
908/**
909 * Similar to getInputTransform, we need to update the bounds to include the transform.
910 * This is because bounds for BSL doesn't include buffer transform, where the input assumes
911 * that's already included.
912 */
913Rect BufferStateLayer::getInputBounds() const {
914 Rect bufferBounds = getCroppedBufferSize(getDrawingState());
915 if (mDrawingState.transform.getType() == ui::Transform::IDENTITY || !bufferBounds.isValid()) {
916 return bufferBounds;
917 }
918 return mDrawingState.transform.transform(bufferBounds);
919}
920
Marissa Wall61c58622018-07-18 10:12:20 -0700921} // namespace android