blob: e470eb940ca7f86cc6d1ef71b3c0dbd74586e38d [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
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -080017// TODO(b/129481165): remove the #pragma below and fix conversion issues
18#pragma clang diagnostic push
19#pragma clang diagnostic ignored "-Wconversion"
Marin Shalamanovbed7fd32020-12-21 20:02:20 +010020#pragma clang diagnostic ignored "-Wextra"
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -080021
Marissa Wall61c58622018-07-18 10:12:20 -070022//#define LOG_NDEBUG 0
23#undef LOG_TAG
24#define LOG_TAG "BufferStateLayer"
25#define ATRACE_TAG ATRACE_TAG_GRAPHICS
26
Lloyd Pique9755fb72019-03-26 14:44:40 -070027#include "BufferStateLayer.h"
28
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080029#include <limits>
Marissa Wall61c58622018-07-18 10:12:20 -070030
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +000031#include <FrameTimeline/FrameTimeline.h>
Lloyd Pique9755fb72019-03-26 14:44:40 -070032#include <compositionengine/LayerFECompositionState.h>
Marissa Wall947d34e2019-03-29 14:03:53 -070033#include <gui/BufferQueue.h>
Marissa Wall61c58622018-07-18 10:12:20 -070034#include <private/gui/SyncFeatures.h>
Peiyong Lincbc184f2018-08-22 13:24:10 -070035#include <renderengine/Image.h>
Marissa Wall61c58622018-07-18 10:12:20 -070036
Vishnu Nairfa247b12020-02-11 08:58:26 -080037#include "EffectLayer.h"
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080038#include "TimeStats/TimeStats.h"
Valerie Hau0bc09152018-12-20 07:42:47 -080039
Marissa Wall61c58622018-07-18 10:12:20 -070040namespace android {
41
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +000042using PresentState = frametimeline::SurfaceFrame::PresentState;
Lloyd Pique42ab75e2018-09-12 20:46:03 -070043// clang-format off
44const std::array<float, 16> BufferStateLayer::IDENTITY_MATRIX{
45 1, 0, 0, 0,
46 0, 1, 0, 0,
47 0, 0, 1, 0,
48 0, 0, 0, 1
49};
50// clang-format on
Marissa Wall61c58622018-07-18 10:12:20 -070051
Marissa Wall947d34e2019-03-29 14:03:53 -070052BufferStateLayer::BufferStateLayer(const LayerCreationArgs& args)
53 : BufferLayer(args), mHwcSlotGenerator(new HwcSlotGenerator()) {
Marissa Wall3ff826c2019-02-07 11:58:25 -080054 mCurrentState.dataspace = ui::Dataspace::V0_SRGB;
Vishnu Nair60356342018-11-13 13:00:45 -080055}
Marissa Wall61c58622018-07-18 10:12:20 -070056
Alec Mouri4545a8a2019-08-08 20:05:32 -070057BufferStateLayer::~BufferStateLayer() {
chaviwb4c6e582019-08-16 14:35:07 -070058 // The original layer and the clone layer share the same texture and buffer. Therefore, only
59 // one of the layers, in this case the original layer, needs to handle the deletion. The
60 // original layer and the clone should be removed at the same time so there shouldn't be any
61 // issue with the clone layer trying to use the texture.
62 if (mBufferInfo.mBuffer != nullptr && !isClone()) {
chaviwd62d3062019-09-04 14:48:02 -070063 // Ensure that mBuffer is uncached from RenderEngine here, as
Alec Mouri4545a8a2019-08-08 20:05:32 -070064 // RenderEngine may have been using the buffer as an external texture
65 // after the client uncached the buffer.
66 auto& engine(mFlinger->getRenderEngine());
chaviwd62d3062019-09-04 14:48:02 -070067 engine.unbindExternalTextureBuffer(mBufferInfo.mBuffer->getId());
Alec Mouri4545a8a2019-08-08 20:05:32 -070068 }
69}
70
Robert Carr8d958532020-11-10 14:09:16 -080071status_t BufferStateLayer::addReleaseFence(const sp<CallbackHandle>& ch,
72 const sp<Fence>& fence) {
73 if (ch == nullptr) {
74 return OK;
75 }
76 if (!ch->previousReleaseFence.get()) {
77 ch->previousReleaseFence = fence;
78 return OK;
79 }
80
81 // Below logic is lifted from ConsumerBase.cpp:
82 // Check status of fences first because merging is expensive.
83 // Merging an invalid fence with any other fence results in an
84 // invalid fence.
85 auto currentStatus = ch->previousReleaseFence->getStatus();
86 if (currentStatus == Fence::Status::Invalid) {
87 ALOGE("Existing fence has invalid state, layer: %s", mName.c_str());
88 return BAD_VALUE;
89 }
90
91 auto incomingStatus = fence->getStatus();
92 if (incomingStatus == Fence::Status::Invalid) {
93 ALOGE("New fence has invalid state, layer: %s", mName.c_str());
94 ch->previousReleaseFence = fence;
95 return BAD_VALUE;
96 }
97
98 // If both fences are signaled or both are unsignaled, we need to merge
99 // them to get an accurate timestamp.
100 if (currentStatus == incomingStatus) {
101 char fenceName[32] = {};
102 snprintf(fenceName, 32, "%.28s", mName.c_str());
103 sp<Fence> mergedFence = Fence::merge(
104 fenceName, ch->previousReleaseFence, fence);
105 if (!mergedFence.get()) {
106 ALOGE("failed to merge release fences, layer: %s", mName.c_str());
107 // synchronization is broken, the best we can do is hope fences
108 // signal in order so the new fence will act like a union
109 ch->previousReleaseFence = fence;
110 return BAD_VALUE;
111 }
112 ch->previousReleaseFence = mergedFence;
113 } else if (incomingStatus == Fence::Status::Unsignaled) {
114 // If one fence has signaled and the other hasn't, the unsignaled
115 // fence will approximately correspond with the correct timestamp.
116 // There's a small race if both fences signal at about the same time
117 // and their statuses are retrieved with unfortunate timing. However,
118 // by this point, they will have both signaled and only the timestamp
119 // will be slightly off; any dependencies after this point will
120 // already have been met.
121 ch->previousReleaseFence = fence;
122 }
123 // else if (currentStatus == Fence::Status::Unsignaled) is a no-op.
124
125 return OK;
126}
127
Marissa Wall61c58622018-07-18 10:12:20 -0700128// -----------------------------------------------------------------------
129// Interface implementation for Layer
130// -----------------------------------------------------------------------
Marissa Wallfda30bb2018-10-12 11:34:28 -0700131void BufferStateLayer::onLayerDisplayed(const sp<Fence>& releaseFence) {
Robert Carr8d958532020-11-10 14:09:16 -0800132 if (!releaseFence->isValid()) {
133 return;
134 }
Marissa Wall5a68a772018-12-22 17:43:42 -0800135 // The previous release fence notifies the client that SurfaceFlinger is done with the previous
136 // buffer that was presented on this layer. The first transaction that came in this frame that
137 // replaced the previous buffer on this layer needs this release fence, because the fence will
138 // let the client know when that previous buffer is removed from the screen.
139 //
140 // Every other transaction on this layer does not need a release fence because no other
141 // Transactions that were set on this layer this frame are going to have their preceeding buffer
142 // removed from the display this frame.
143 //
144 // For example, if we have 3 transactions this frame. The first transaction doesn't contain a
145 // buffer so it doesn't need a previous release fence because the layer still needs the previous
146 // buffer. The second transaction contains a buffer so it needs a previous release fence because
147 // the previous buffer will be released this frame. The third transaction also contains a
148 // buffer. It replaces the buffer in the second transaction. The buffer in the second
149 // transaction will now no longer be presented so it is released immediately and the third
150 // transaction doesn't need a previous release fence.
Robert Carr8d958532020-11-10 14:09:16 -0800151 sp<CallbackHandle> ch;
Marissa Wall5a68a772018-12-22 17:43:42 -0800152 for (auto& handle : mDrawingState.callbackHandles) {
153 if (handle->releasePreviousBuffer) {
Robert Carr8d958532020-11-10 14:09:16 -0800154 ch = handle;
Marissa Wall5a68a772018-12-22 17:43:42 -0800155 break;
156 }
157 }
Robert Carr8d958532020-11-10 14:09:16 -0800158 auto status = addReleaseFence(ch, releaseFence);
159 if (status != OK) {
160 ALOGE("Failed to add release fence for layer %s", getName().c_str());
161 }
Mikael Pessa2e1608f2019-07-19 11:25:35 -0700162
Valerie Haubf784642020-01-29 07:25:23 -0800163 mPreviousReleaseFence = releaseFence;
164
Mikael Pessa2e1608f2019-07-19 11:25:35 -0700165 // Prevent tracing the same release multiple times.
166 if (mPreviousFrameNumber != mPreviousReleasedFrameNumber) {
Mikael Pessa2e1608f2019-07-19 11:25:35 -0700167 mPreviousReleasedFrameNumber = mPreviousFrameNumber;
168 }
Marissa Wall61c58622018-07-18 10:12:20 -0700169}
170
Jorim Jaggi9c03b502020-11-24 23:51:31 +0100171void BufferStateLayer::onSurfaceFrameCreated(
172 const std::shared_ptr<frametimeline::SurfaceFrame>& surfaceFrame) {
173 mPendingJankClassifications.emplace_back(surfaceFrame);
174}
175
Valerie Haubf784642020-01-29 07:25:23 -0800176void BufferStateLayer::releasePendingBuffer(nsecs_t dequeueReadyTime) {
Valerie Hau32cdc1f2019-10-21 14:45:54 -0700177 for (const auto& handle : mDrawingState.callbackHandles) {
178 handle->transformHint = mTransformHint;
Valerie Hau871d6352020-01-29 08:44:02 -0800179 handle->dequeueReadyTime = dequeueReadyTime;
Valerie Hau32cdc1f2019-10-21 14:45:54 -0700180 }
181
Jorim Jaggi9c03b502020-11-24 23:51:31 +0100182 std::vector<JankData> jankData;
183 jankData.reserve(mPendingJankClassifications.size());
184 while (!mPendingJankClassifications.empty()
185 && mPendingJankClassifications.front()->getJankType()) {
186 std::shared_ptr<frametimeline::SurfaceFrame> surfaceFrame =
187 mPendingJankClassifications.front();
188 mPendingJankClassifications.pop_front();
189 jankData.emplace_back(
190 JankData(surfaceFrame->getToken(), surfaceFrame->getJankType().value()));
191 }
192
Marissa Wallefb71af2019-06-27 14:45:53 -0700193 mFlinger->getTransactionCompletedThread().finalizePendingCallbackHandles(
Jorim Jaggi9c03b502020-11-24 23:51:31 +0100194 mDrawingState.callbackHandles, jankData);
Marissa Wall5a68a772018-12-22 17:43:42 -0800195
196 mDrawingState.callbackHandles = {};
Valerie Haubf784642020-01-29 07:25:23 -0800197
198 const sp<Fence>& releaseFence(mPreviousReleaseFence);
199 std::shared_ptr<FenceTime> releaseFenceTime = std::make_shared<FenceTime>(releaseFence);
200 {
201 Mutex::Autolock lock(mFrameEventHistoryMutex);
202 if (mPreviousFrameNumber != 0) {
203 mFrameEventHistory.addRelease(mPreviousFrameNumber, dequeueReadyTime,
204 std::move(releaseFenceTime));
205 }
206 }
Marissa Wall61c58622018-07-18 10:12:20 -0700207}
208
Valerie Hau871d6352020-01-29 08:44:02 -0800209void BufferStateLayer::finalizeFrameEventHistory(const std::shared_ptr<FenceTime>& glDoneFence,
210 const CompositorTiming& compositorTiming) {
211 for (const auto& handle : mDrawingState.callbackHandles) {
212 handle->gpuCompositionDoneFence = glDoneFence;
213 handle->compositorTiming = compositorTiming;
214 }
215}
216
Marissa Walle2ffb422018-10-12 11:33:52 -0700217bool BufferStateLayer::willPresentCurrentTransaction() const {
218 // Returns true if the most recent Transaction applied to CurrentState will be presented.
Robert Carr321e83c2019-08-19 15:49:30 -0700219 return (getSidebandStreamChanged() || getAutoRefresh() ||
Valerie Hauaa194562019-02-05 16:21:38 -0800220 (mCurrentState.modified &&
chaviw8ba8b072021-01-25 14:55:46 -0800221 (mCurrentState.buffer != nullptr || mCurrentState.bgColorLayer != nullptr)));
Marissa Wall61c58622018-07-18 10:12:20 -0700222}
223
Valerie Hau3282b3c2020-02-03 15:37:27 -0800224/* TODO: vhau uncomment once deferred transaction migration complete in
225 * WindowManager
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800226void BufferStateLayer::pushPendingState() {
227 if (!mCurrentState.modified) {
Marissa Wall61c58622018-07-18 10:12:20 -0700228 return;
229 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800230 mPendingStates.push_back(mCurrentState);
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700231 ATRACE_INT(mTransactionName.c_str(), mPendingStates.size());
Marissa Wall61c58622018-07-18 10:12:20 -0700232}
Valerie Hau3282b3c2020-02-03 15:37:27 -0800233*/
Marissa Wall61c58622018-07-18 10:12:20 -0700234
235bool BufferStateLayer::applyPendingStates(Layer::State* stateToCommit) {
Valerie Hau3282b3c2020-02-03 15:37:27 -0800236 mCurrentStateModified = mCurrentState.modified;
237 bool stateUpdateAvailable = Layer::applyPendingStates(stateToCommit);
Ady Abraham22c7b5c2020-09-22 19:33:40 -0700238 if (stateUpdateAvailable && mCallbackHandleAcquireTime != -1) {
Ady Abraham7f8a1e62020-09-28 16:09:35 -0700239 // Update the acquire fence time if we have a buffer
240 mSurfaceFrame->setAcquireFenceTime(mCallbackHandleAcquireTime);
Ady Abraham22c7b5c2020-09-22 19:33:40 -0700241 }
Valerie Hau3282b3c2020-02-03 15:37:27 -0800242 mCurrentStateModified = stateUpdateAvailable && mCurrentStateModified;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800243 mCurrentState.modified = false;
Marissa Wall61c58622018-07-18 10:12:20 -0700244 return stateUpdateAvailable;
245}
246
Marissa Wall861616d2018-10-22 12:52:23 -0700247// Crop that applies to the window
248Rect BufferStateLayer::getCrop(const Layer::State& /*s*/) const {
249 return Rect::INVALID_RECT;
Marissa Wall61c58622018-07-18 10:12:20 -0700250}
251
252bool BufferStateLayer::setTransform(uint32_t transform) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800253 if (mCurrentState.transform == transform) return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800254 mCurrentState.transform = transform;
255 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700256 setTransactionFlags(eTransactionNeeded);
257 return true;
258}
259
260bool BufferStateLayer::setTransformToDisplayInverse(bool transformToDisplayInverse) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800261 if (mCurrentState.transformToDisplayInverse == transformToDisplayInverse) return false;
262 mCurrentState.sequence++;
263 mCurrentState.transformToDisplayInverse = transformToDisplayInverse;
264 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700265 setTransactionFlags(eTransactionNeeded);
266 return true;
267}
268
269bool BufferStateLayer::setCrop(const Rect& crop) {
Marissa Wall290ad082019-03-06 13:23:47 -0800270 Rect c = crop;
271 if (c.left < 0) {
272 c.left = 0;
273 }
274 if (c.top < 0) {
275 c.top = 0;
276 }
277 // If the width and/or height are < 0, make it [0, 0, -1, -1] so the equality comparision below
278 // treats all invalid rectangles the same.
279 if (!c.isValid()) {
280 c.makeInvalid();
281 }
282
283 if (mCurrentState.crop == c) return false;
Marissa Wall290ad082019-03-06 13:23:47 -0800284 mCurrentState.crop = c;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800285 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700286 setTransactionFlags(eTransactionNeeded);
287 return true;
288}
289
Marissa Wall861616d2018-10-22 12:52:23 -0700290bool BufferStateLayer::setFrame(const Rect& frame) {
291 int x = frame.left;
292 int y = frame.top;
293 int w = frame.getWidth();
294 int h = frame.getHeight();
295
Marissa Wall0f3242d2018-12-20 15:10:22 -0800296 if (x < 0) {
297 x = 0;
298 w = frame.right;
299 }
300
301 if (y < 0) {
302 y = 0;
303 h = frame.bottom;
304 }
305
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800306 if (mCurrentState.active.transform.tx() == x && mCurrentState.active.transform.ty() == y &&
307 mCurrentState.active.w == w && mCurrentState.active.h == h) {
Marissa Wall861616d2018-10-22 12:52:23 -0700308 return false;
309 }
310
311 if (!frame.isValid()) {
312 x = y = w = h = 0;
313 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800314 mCurrentState.active.transform.set(x, y);
315 mCurrentState.active.w = w;
316 mCurrentState.active.h = h;
Marissa Wall861616d2018-10-22 12:52:23 -0700317
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800318 mCurrentState.sequence++;
319 mCurrentState.modified = true;
Marissa Wall861616d2018-10-22 12:52:23 -0700320 setTransactionFlags(eTransactionNeeded);
321 return true;
322}
323
Valerie Hau871d6352020-01-29 08:44:02 -0800324bool BufferStateLayer::addFrameEvent(const sp<Fence>& acquireFence, nsecs_t postedTime,
325 nsecs_t desiredPresentTime) {
Valerie Haubf784642020-01-29 07:25:23 -0800326 Mutex::Autolock lock(mFrameEventHistoryMutex);
327 mAcquireTimeline.updateSignalTimes();
328 std::shared_ptr<FenceTime> acquireFenceTime =
329 std::make_shared<FenceTime>((acquireFence ? acquireFence : Fence::NO_FENCE));
330 NewFrameEventsEntry newTimestamps = {mCurrentState.frameNumber, postedTime, desiredPresentTime,
331 acquireFenceTime};
Valerie Hau871d6352020-01-29 08:44:02 -0800332 mFrameEventHistory.setProducerWantsEvents();
Valerie Haubf784642020-01-29 07:25:23 -0800333 mFrameEventHistory.addQueue(newTimestamps);
334 return true;
335}
336
337bool BufferStateLayer::setBuffer(const sp<GraphicBuffer>& buffer, const sp<Fence>& acquireFence,
Ady Abrahamf0c56492020-12-17 18:04:15 -0800338 nsecs_t postTime, nsecs_t desiredPresentTime, bool isAutoTimestamp,
Vishnu Nairadf632b2021-01-07 14:05:08 -0800339 const client_cache_t& clientCacheId, uint64_t frameNumber,
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +0000340 std::optional<nsecs_t> /* dequeueTime */,
341 const FrameTimelineInfo& info) {
Robert Carr0c1966e2020-10-19 12:12:08 -0700342 ATRACE_CALL();
343
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800344 if (mCurrentState.buffer) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700345 mReleasePreviousBuffer = true;
Robert Carr7121caf2020-12-15 13:07:32 -0800346 if (mCurrentState.buffer != mDrawingState.buffer) {
347 // If mCurrentState has a buffer, and we are about to update again
348 // before swapping to drawing state, then the first buffer will be
349 // dropped and we should decrement the pending buffer count.
350 decrementPendingBufferCount();
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +0000351 if (mCurrentState.bufferSurfaceFrameTX != nullptr) {
352 addSurfaceFrameDroppedForBuffer(mCurrentState.bufferSurfaceFrameTX);
353 mCurrentState.bufferSurfaceFrameTX.reset();
354 }
Robert Carr7121caf2020-12-15 13:07:32 -0800355 }
Marissa Wallfda30bb2018-10-12 11:34:28 -0700356 }
357
Vishnu Nair6b7c5c92020-09-29 17:27:05 -0700358 mCurrentState.frameNumber = frameNumber;
Valerie Hau2f54d642020-01-22 09:37:03 -0800359
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800360 mCurrentState.buffer = buffer;
Marissa Wall947d34e2019-03-29 14:03:53 -0700361 mCurrentState.clientCacheId = clientCacheId;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800362 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700363 setTransactionFlags(eTransactionNeeded);
Ady Abraham09bd3922019-04-08 10:44:56 -0700364
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800365 const int32_t layerId = getSequence();
Valerie Hau134651a2020-01-28 16:21:22 -0800366 mFlinger->mTimeStats->setPostTime(layerId, mCurrentState.frameNumber, getName().c_str(),
Alec Mouri9a29e672020-09-14 12:39:14 -0700367 mOwnerUid, postTime);
chaviwfa67b552019-08-12 16:51:55 -0700368 mCurrentState.desiredPresentTime = desiredPresentTime;
Ady Abrahamf0c56492020-12-17 18:04:15 -0800369 mCurrentState.isAutoTimestamp = isAutoTimestamp;
Ady Abraham09bd3922019-04-08 10:44:56 -0700370
Ady Abrahamf0c56492020-12-17 18:04:15 -0800371 mFlinger->mScheduler->recordLayerHistory(this, isAutoTimestamp ? 0 : desiredPresentTime,
Ady Abraham5def7332020-05-29 16:13:47 -0700372 LayerHistory::LayerUpdateType::Buffer);
Ady Abraham09bd3922019-04-08 10:44:56 -0700373
Ady Abrahamf0c56492020-12-17 18:04:15 -0800374 addFrameEvent(acquireFence, postTime, isAutoTimestamp ? 0 : desiredPresentTime);
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +0000375
376 if (info.vsyncId != FrameTimelineInfo::INVALID_VSYNC_ID) {
377 setFrameTimelineVsyncForBufferTransaction(info, postTime);
378 }
379
Marissa Wall61c58622018-07-18 10:12:20 -0700380 return true;
381}
382
383bool BufferStateLayer::setAcquireFence(const sp<Fence>& fence) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700384 // The acquire fences of BufferStateLayers have already signaled before they are set
385 mCallbackHandleAcquireTime = fence->getSignalTime();
386
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800387 mCurrentState.acquireFence = fence;
388 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700389 setTransactionFlags(eTransactionNeeded);
390 return true;
391}
392
393bool BufferStateLayer::setDataspace(ui::Dataspace dataspace) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800394 if (mCurrentState.dataspace == dataspace) return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800395 mCurrentState.dataspace = dataspace;
396 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700397 setTransactionFlags(eTransactionNeeded);
398 return true;
399}
400
401bool BufferStateLayer::setHdrMetadata(const HdrMetadata& hdrMetadata) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800402 if (mCurrentState.hdrMetadata == hdrMetadata) return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800403 mCurrentState.hdrMetadata = hdrMetadata;
404 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700405 setTransactionFlags(eTransactionNeeded);
406 return true;
407}
408
409bool BufferStateLayer::setSurfaceDamageRegion(const Region& surfaceDamage) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800410 mCurrentState.surfaceDamageRegion = surfaceDamage;
411 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700412 setTransactionFlags(eTransactionNeeded);
413 return true;
414}
415
416bool BufferStateLayer::setApi(int32_t api) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800417 if (mCurrentState.api == api) return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800418 mCurrentState.api = api;
419 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700420 setTransactionFlags(eTransactionNeeded);
421 return true;
422}
423
424bool BufferStateLayer::setSidebandStream(const sp<NativeHandle>& sidebandStream) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800425 if (mCurrentState.sidebandStream == sidebandStream) return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800426 mCurrentState.sidebandStream = sidebandStream;
427 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700428 setTransactionFlags(eTransactionNeeded);
429
430 if (!mSidebandStreamChanged.exchange(true)) {
431 // mSidebandStreamChanged was false
432 mFlinger->signalLayerUpdate();
433 }
434 return true;
435}
436
Marissa Walle2ffb422018-10-12 11:33:52 -0700437bool BufferStateLayer::setTransactionCompletedListeners(
438 const std::vector<sp<CallbackHandle>>& handles) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700439 // If there is no handle, we will not send a callback so reset mReleasePreviousBuffer and return
Marissa Walle2ffb422018-10-12 11:33:52 -0700440 if (handles.empty()) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700441 mReleasePreviousBuffer = false;
Marissa Walle2ffb422018-10-12 11:33:52 -0700442 return false;
443 }
444
445 const bool willPresent = willPresentCurrentTransaction();
446
447 for (const auto& handle : handles) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700448 // If this transaction set a buffer on this layer, release its previous buffer
449 handle->releasePreviousBuffer = mReleasePreviousBuffer;
450
Marissa Walle2ffb422018-10-12 11:33:52 -0700451 // If this layer will be presented in this frame
452 if (willPresent) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700453 // If this transaction set an acquire fence on this layer, set its acquire time
454 handle->acquireTime = mCallbackHandleAcquireTime;
455
Marissa Walle2ffb422018-10-12 11:33:52 -0700456 // Notify the transaction completed thread that there is a pending latched callback
457 // handle
Marissa Wall5a68a772018-12-22 17:43:42 -0800458 mFlinger->getTransactionCompletedThread().registerPendingCallbackHandle(handle);
Marissa Walle2ffb422018-10-12 11:33:52 -0700459
460 // Store so latched time and release fence can be set
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800461 mCurrentState.callbackHandles.push_back(handle);
Marissa Walle2ffb422018-10-12 11:33:52 -0700462
463 } else { // If this layer will NOT need to be relatched and presented this frame
464 // Notify the transaction completed thread this handle is done
Marissa Wallefb71af2019-06-27 14:45:53 -0700465 mFlinger->getTransactionCompletedThread().registerUnpresentedCallbackHandle(handle);
Marissa Walle2ffb422018-10-12 11:33:52 -0700466 }
467 }
468
Marissa Wallfda30bb2018-10-12 11:34:28 -0700469 mReleasePreviousBuffer = false;
470 mCallbackHandleAcquireTime = -1;
471
Marissa Walle2ffb422018-10-12 11:33:52 -0700472 return willPresent;
473}
474
Marissa Wall61c58622018-07-18 10:12:20 -0700475bool BufferStateLayer::setTransparentRegionHint(const Region& transparent) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800476 mCurrentState.transparentRegionHint = transparent;
477 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700478 setTransactionFlags(eTransactionNeeded);
479 return true;
480}
481
Marissa Wall861616d2018-10-22 12:52:23 -0700482Rect BufferStateLayer::getBufferSize(const State& s) const {
483 // for buffer state layers we use the display frame size as the buffer size.
484 if (getActiveWidth(s) < UINT32_MAX && getActiveHeight(s) < UINT32_MAX) {
485 return Rect(getActiveWidth(s), getActiveHeight(s));
Marissa Wall61c58622018-07-18 10:12:20 -0700486 }
487
chaviw7e72caf2020-12-02 16:50:43 -0800488 if (mBufferInfo.mBuffer == nullptr) {
489 return Rect::INVALID_RECT;
490 }
491
Marissa Wall861616d2018-10-22 12:52:23 -0700492 // if the display frame is not defined, use the parent bounds as the buffer size.
493 const auto& p = mDrawingParent.promote();
494 if (p != nullptr) {
Vishnu Nair4351ad52019-02-11 14:13:02 -0800495 Rect parentBounds = Rect(p->getBounds(Region()));
Marissa Wall861616d2018-10-22 12:52:23 -0700496 if (!parentBounds.isEmpty()) {
497 return parentBounds;
498 }
499 }
500
Marissa Wall861616d2018-10-22 12:52:23 -0700501 return Rect::INVALID_RECT;
Marissa Wall61c58622018-07-18 10:12:20 -0700502}
Vishnu Nair4351ad52019-02-11 14:13:02 -0800503
504FloatRect BufferStateLayer::computeSourceBounds(const FloatRect& parentBounds) const {
505 const State& s(getDrawingState());
506 // for buffer state layers we use the display frame size as the buffer size.
507 if (getActiveWidth(s) < UINT32_MAX && getActiveHeight(s) < UINT32_MAX) {
508 return FloatRect(0, 0, getActiveWidth(s), getActiveHeight(s));
509 }
510
511 // if the display frame is not defined, use the parent bounds as the buffer size.
512 return parentBounds;
513}
514
Marissa Wall61c58622018-07-18 10:12:20 -0700515// -----------------------------------------------------------------------
516
517// -----------------------------------------------------------------------
518// Interface implementation for BufferLayer
519// -----------------------------------------------------------------------
520bool BufferStateLayer::fenceHasSignaled() const {
Alec Mouri91f6df32020-01-30 08:48:58 -0800521 const bool fenceSignaled =
522 getDrawingState().acquireFence->getStatus() == Fence::Status::Signaled;
523 if (!fenceSignaled) {
524 mFlinger->mTimeStats->incrementLatchSkipped(getSequence(),
525 TimeStats::LatchSkipReason::LateAcquire);
526 }
527
528 return fenceSignaled;
Marissa Wall61c58622018-07-18 10:12:20 -0700529}
530
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700531bool BufferStateLayer::framePresentTimeIsCurrent(nsecs_t expectedPresentTime) const {
Ady Abrahamcd1580c2019-04-29 15:40:03 -0700532 if (!hasFrameUpdate() || isRemovedFromCurrentState()) {
533 return true;
534 }
535
Ady Abrahamf0c56492020-12-17 18:04:15 -0800536 return mCurrentState.isAutoTimestamp || mCurrentState.desiredPresentTime <= expectedPresentTime;
Ady Abrahamcd1580c2019-04-29 15:40:03 -0700537}
538
Valerie Hau871d6352020-01-29 08:44:02 -0800539bool BufferStateLayer::onPreComposition(nsecs_t refreshStartTime) {
540 for (const auto& handle : mDrawingState.callbackHandles) {
541 handle->refreshStartTime = refreshStartTime;
542 }
543 return BufferLayer::onPreComposition(refreshStartTime);
544}
545
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700546uint64_t BufferStateLayer::getFrameNumber(nsecs_t /*expectedPresentTime*/) const {
Valerie Hau134651a2020-01-28 16:21:22 -0800547 return mDrawingState.frameNumber;
Marissa Wall61c58622018-07-18 10:12:20 -0700548}
549
Robert Carrfe1209c2020-02-11 12:25:35 -0800550/**
551 * This is the frameNumber used for deferred transaction signalling. We need to use this because
552 * of cases where we defer a transaction for a surface to itself. In the BLAST world this
553 * may not make a huge amount of sense (Why not just merge the Buffer transaction with the
554 * deferred transaction?) but this is an important legacy use case, for example moving
555 * a window at the same time it draws makes use of this kind of technique. So anyway
556 * imagine we have something like this:
557 *
558 * Transaction { // containing
559 * Buffer -> frameNumber = 2
560 * DeferTransactionUntil -> frameNumber = 2
561 * Random other stuff
562 * }
563 * Now imagine getHeadFrameNumber returned mDrawingState.mFrameNumber (or mCurrentFrameNumber).
564 * Prior to doTransaction SurfaceFlinger will call notifyAvailableFrames, but because we
565 * haven't swapped mCurrentState to mDrawingState yet we will think the sync point
566 * is not ready. So we will return false from applyPendingState and not swap
567 * current state to drawing state. But because we don't swap current state
568 * to drawing state the number will never update and we will be stuck. This way
569 * we can see we need to return the frame number for the buffer we are about
570 * to apply.
571 */
572uint64_t BufferStateLayer::getHeadFrameNumber(nsecs_t /* expectedPresentTime */) const {
573 return mCurrentState.frameNumber;
574}
575
Vishnu Naircf26a0a2020-11-13 12:56:20 -0800576void BufferStateLayer::setAutoRefresh(bool autoRefresh) {
577 if (!mAutoRefresh.exchange(autoRefresh)) {
578 mFlinger->signalLayerUpdate();
579 }
Marissa Wall61c58622018-07-18 10:12:20 -0700580}
581
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800582bool BufferStateLayer::latchSidebandStream(bool& recomputeVisibleRegions) {
Marissa Wall61c58622018-07-18 10:12:20 -0700583 if (mSidebandStreamChanged.exchange(false)) {
584 const State& s(getDrawingState());
585 // mSidebandStreamChanged was true
Lloyd Pique0b785d82018-12-04 17:25:27 -0800586 mSidebandStream = s.sidebandStream;
Lloyd Piquede196652020-01-22 17:29:58 -0800587 editCompositionState()->sidebandStream = mSidebandStream;
Lloyd Pique0b785d82018-12-04 17:25:27 -0800588 if (mSidebandStream != nullptr) {
Marissa Wall61c58622018-07-18 10:12:20 -0700589 setTransactionFlags(eTransactionNeeded);
590 mFlinger->setTransactionFlags(eTraversalNeeded);
591 }
592 recomputeVisibleRegions = true;
593
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800594 return true;
Marissa Wall61c58622018-07-18 10:12:20 -0700595 }
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800596 return false;
Marissa Wall61c58622018-07-18 10:12:20 -0700597}
598
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800599bool BufferStateLayer::hasFrameUpdate() const {
Valerie Hauaa194562019-02-05 16:21:38 -0800600 const State& c(getCurrentState());
601 return mCurrentStateModified && (c.buffer != nullptr || c.bgColorLayer != nullptr);
Marissa Wall61c58622018-07-18 10:12:20 -0700602}
603
Ady Abraham63a3e592021-01-06 10:47:15 -0800604std::optional<nsecs_t> BufferStateLayer::nextPredictedPresentTime() const {
Ady Abrahamce4adf12020-12-15 18:45:12 -0800605 if (!getDrawingState().isAutoTimestamp || !mSurfaceFrame) {
Ady Abraham63a3e592021-01-06 10:47:15 -0800606 return std::nullopt;
Ady Abrahamce4adf12020-12-15 18:45:12 -0800607 }
608
609 return mSurfaceFrame->getPredictions().presentTime;
610}
611
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700612status_t BufferStateLayer::updateTexImage(bool& /*recomputeVisibleRegions*/, nsecs_t latchTime,
613 nsecs_t /*expectedPresentTime*/) {
Marissa Wall61c58622018-07-18 10:12:20 -0700614 const State& s(getDrawingState());
615
616 if (!s.buffer) {
Valerie Hauaa194562019-02-05 16:21:38 -0800617 if (s.bgColorLayer) {
618 for (auto& handle : mDrawingState.callbackHandles) {
619 handle->latchTime = latchTime;
620 }
621 }
Marissa Wall61c58622018-07-18 10:12:20 -0700622 return NO_ERROR;
623 }
624
Marissa Wall5a68a772018-12-22 17:43:42 -0800625 for (auto& handle : mDrawingState.callbackHandles) {
626 handle->latchTime = latchTime;
Valerie Hau871d6352020-01-29 08:44:02 -0800627 handle->frameNumber = mDrawingState.frameNumber;
Marissa Wall5a68a772018-12-22 17:43:42 -0800628 }
Marissa Walle2ffb422018-10-12 11:33:52 -0700629
Vishnu Nairea0de002020-11-17 17:42:37 -0800630 const int32_t layerId = getSequence();
Valerie Hau134651a2020-01-28 16:21:22 -0800631 mFlinger->mTimeStats->setAcquireFence(layerId, mDrawingState.frameNumber,
chaviw95631e32020-06-09 13:43:32 -0700632 std::make_shared<FenceTime>(mDrawingState.acquireFence));
Valerie Hau134651a2020-01-28 16:21:22 -0800633 mFlinger->mTimeStats->setLatchTime(layerId, mDrawingState.frameNumber, latchTime);
Marissa Wall61c58622018-07-18 10:12:20 -0700634
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +0000635 auto& bufferSurfaceFrame = mDrawingState.bufferSurfaceFrameTX;
636 if (bufferSurfaceFrame != nullptr &&
637 bufferSurfaceFrame->getPresentState() != PresentState::Presented) {
638 // Update only if the bufferSurfaceFrame wasn't already presented. A Presented
639 // bufferSurfaceFrame could be seen here if a pending state was applied successfully and we
640 // are processing the next state.
641 addSurfaceFramePresentedForBuffer(bufferSurfaceFrame,
642 mDrawingState.acquireFence->getSignalTime(), latchTime);
643 bufferSurfaceFrame.reset();
644 }
645
Marissa Wall16c112d2019-03-20 13:21:13 -0700646 mCurrentStateModified = false;
647
Marissa Wall61c58622018-07-18 10:12:20 -0700648 return NO_ERROR;
649}
650
651status_t BufferStateLayer::updateActiveBuffer() {
652 const State& s(getDrawingState());
653
654 if (s.buffer == nullptr) {
655 return BAD_VALUE;
656 }
chaviwdf3c5e82021-01-07 13:00:37 -0800657
658 if (s.buffer != mBufferInfo.mBuffer) {
659 decrementPendingBufferCount();
660 }
Marissa Wall61c58622018-07-18 10:12:20 -0700661
Mikael Pessa2e1608f2019-07-19 11:25:35 -0700662 mPreviousBufferId = getCurrentBufferId();
chaviwd62d3062019-09-04 14:48:02 -0700663 mBufferInfo.mBuffer = s.buffer;
664 mBufferInfo.mFence = s.acquireFence;
Marissa Wall61c58622018-07-18 10:12:20 -0700665
666 return NO_ERROR;
667}
668
Valerie Haubf784642020-01-29 07:25:23 -0800669status_t BufferStateLayer::updateFrameNumber(nsecs_t latchTime) {
Marissa Wall61c58622018-07-18 10:12:20 -0700670 // TODO(marissaw): support frame history events
Mikael Pessa2e1608f2019-07-19 11:25:35 -0700671 mPreviousFrameNumber = mCurrentFrameNumber;
Valerie Hau134651a2020-01-28 16:21:22 -0800672 mCurrentFrameNumber = mDrawingState.frameNumber;
Valerie Haubf784642020-01-29 07:25:23 -0800673 {
674 Mutex::Autolock lock(mFrameEventHistoryMutex);
675 mFrameEventHistory.addLatch(mCurrentFrameNumber, latchTime);
676 }
Marissa Wall61c58622018-07-18 10:12:20 -0700677 return NO_ERROR;
678}
679
Marissa Wall947d34e2019-03-29 14:03:53 -0700680void BufferStateLayer::HwcSlotGenerator::bufferErased(const client_cache_t& clientCacheId) {
681 std::lock_guard lock(mMutex);
682 if (!clientCacheId.isValid()) {
683 ALOGE("invalid process, failed to erase buffer");
684 return;
685 }
686 eraseBufferLocked(clientCacheId);
687}
688
689uint32_t BufferStateLayer::HwcSlotGenerator::getHwcCacheSlot(const client_cache_t& clientCacheId) {
690 std::lock_guard<std::mutex> lock(mMutex);
691 auto itr = mCachedBuffers.find(clientCacheId);
692 if (itr == mCachedBuffers.end()) {
693 return addCachedBuffer(clientCacheId);
694 }
695 auto& [hwcCacheSlot, counter] = itr->second;
696 counter = mCounter++;
697 return hwcCacheSlot;
698}
699
700uint32_t BufferStateLayer::HwcSlotGenerator::addCachedBuffer(const client_cache_t& clientCacheId)
701 REQUIRES(mMutex) {
702 if (!clientCacheId.isValid()) {
703 ALOGE("invalid process, returning invalid slot");
704 return BufferQueue::INVALID_BUFFER_SLOT;
705 }
706
707 ClientCache::getInstance().registerErasedRecipient(clientCacheId, wp<ErasedRecipient>(this));
708
709 uint32_t hwcCacheSlot = getFreeHwcCacheSlot();
710 mCachedBuffers[clientCacheId] = {hwcCacheSlot, mCounter++};
711 return hwcCacheSlot;
712}
713
714uint32_t BufferStateLayer::HwcSlotGenerator::getFreeHwcCacheSlot() REQUIRES(mMutex) {
715 if (mFreeHwcCacheSlots.empty()) {
716 evictLeastRecentlyUsed();
717 }
718
719 uint32_t hwcCacheSlot = mFreeHwcCacheSlots.top();
720 mFreeHwcCacheSlots.pop();
721 return hwcCacheSlot;
722}
723
724void BufferStateLayer::HwcSlotGenerator::evictLeastRecentlyUsed() REQUIRES(mMutex) {
725 uint64_t minCounter = UINT_MAX;
726 client_cache_t minClientCacheId = {};
727 for (const auto& [clientCacheId, slotCounter] : mCachedBuffers) {
728 const auto& [hwcCacheSlot, counter] = slotCounter;
729 if (counter < minCounter) {
730 minCounter = counter;
731 minClientCacheId = clientCacheId;
732 }
733 }
734 eraseBufferLocked(minClientCacheId);
735
736 ClientCache::getInstance().unregisterErasedRecipient(minClientCacheId, this);
737}
738
739void BufferStateLayer::HwcSlotGenerator::eraseBufferLocked(const client_cache_t& clientCacheId)
740 REQUIRES(mMutex) {
741 auto itr = mCachedBuffers.find(clientCacheId);
742 if (itr == mCachedBuffers.end()) {
743 return;
744 }
745 auto& [hwcCacheSlot, counter] = itr->second;
746
747 // TODO send to hwc cache and resources
748
749 mFreeHwcCacheSlots.push(hwcCacheSlot);
750 mCachedBuffers.erase(clientCacheId);
751}
chaviw4244e032019-09-04 11:27:49 -0700752
753void BufferStateLayer::gatherBufferInfo() {
chaviwdebadb82020-03-26 14:57:24 -0700754 BufferLayer::gatherBufferInfo();
chaviw4244e032019-09-04 11:27:49 -0700755
chaviwdebadb82020-03-26 14:57:24 -0700756 const State& s(getDrawingState());
chaviw4244e032019-09-04 11:27:49 -0700757 mBufferInfo.mDesiredPresentTime = s.desiredPresentTime;
758 mBufferInfo.mFenceTime = std::make_shared<FenceTime>(s.acquireFence);
759 mBufferInfo.mFence = s.acquireFence;
chaviw4244e032019-09-04 11:27:49 -0700760 mBufferInfo.mTransform = s.transform;
761 mBufferInfo.mDataspace = translateDataspace(s.dataspace);
762 mBufferInfo.mCrop = computeCrop(s);
763 mBufferInfo.mScaleMode = NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW;
764 mBufferInfo.mSurfaceDamage = s.surfaceDamageRegion;
765 mBufferInfo.mHdrMetadata = s.hdrMetadata;
766 mBufferInfo.mApi = s.api;
chaviw4244e032019-09-04 11:27:49 -0700767 mBufferInfo.mTransformToDisplayInverse = s.transformToDisplayInverse;
chaviwf83ce182019-09-12 14:43:08 -0700768 mBufferInfo.mBufferSlot = mHwcSlotGenerator->getHwcCacheSlot(s.clientCacheId);
chaviw4244e032019-09-04 11:27:49 -0700769}
770
Robert Carr916b0362020-10-06 13:53:03 -0700771uint32_t BufferStateLayer::getEffectiveScalingMode() const {
772 return NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW;
773}
774
chaviw4244e032019-09-04 11:27:49 -0700775Rect BufferStateLayer::computeCrop(const State& s) {
776 if (s.crop.isEmpty() && s.buffer) {
777 return s.buffer->getBounds();
778 } else if (s.buffer) {
779 Rect crop = s.crop;
780 crop.left = std::max(crop.left, 0);
781 crop.top = std::max(crop.top, 0);
782 uint32_t bufferWidth = s.buffer->getWidth();
783 uint32_t bufferHeight = s.buffer->getHeight();
784 if (bufferHeight <= std::numeric_limits<int32_t>::max() &&
785 bufferWidth <= std::numeric_limits<int32_t>::max()) {
786 crop.right = std::min(crop.right, static_cast<int32_t>(bufferWidth));
787 crop.bottom = std::min(crop.bottom, static_cast<int32_t>(bufferHeight));
788 }
789 if (!crop.isValid()) {
790 // Crop rect is out of bounds, return whole buffer
791 return s.buffer->getBounds();
792 }
793 return crop;
794 }
795 return s.crop;
796}
797
chaviwb4c6e582019-08-16 14:35:07 -0700798sp<Layer> BufferStateLayer::createClone() {
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700799 LayerCreationArgs args(mFlinger.get(), nullptr, mName + " (Mirror)", 0, 0, 0, LayerMetadata());
chaviwb4c6e582019-08-16 14:35:07 -0700800 args.textureName = mTextureName;
Lloyd Pique1c3a5eb2019-10-03 13:07:08 -0700801 sp<BufferStateLayer> layer = mFlinger->getFactory().createBufferStateLayer(args);
chaviwb4c6e582019-08-16 14:35:07 -0700802 layer->mHwcSlotGenerator = mHwcSlotGenerator;
803 layer->setInitialValuesForClone(this);
804 return layer;
805}
Valerie Hau92bf5482020-02-10 09:49:08 -0800806
807Layer::RoundedCornerState BufferStateLayer::getRoundedCornerState() const {
808 const auto& p = mDrawingParent.promote();
809 if (p != nullptr) {
810 RoundedCornerState parentState = p->getRoundedCornerState();
811 if (parentState.radius > 0) {
812 ui::Transform t = getActiveTransform(getDrawingState());
813 t = t.inverse();
814 parentState.cropRect = t.transform(parentState.cropRect);
815 // The rounded corners shader only accepts 1 corner radius for performance reasons,
816 // but a transform matrix can define horizontal and vertical scales.
817 // Let's take the average between both of them and pass into the shader, practically we
818 // never do this type of transformation on windows anyway.
819 parentState.radius *= (t[0][0] + t[1][1]) / 2.0f;
820 return parentState;
821 }
822 }
823 const float radius = getDrawingState().cornerRadius;
824 const State& s(getDrawingState());
825 if (radius <= 0 || (getActiveWidth(s) == UINT32_MAX && getActiveHeight(s) == UINT32_MAX))
826 return RoundedCornerState();
827 return RoundedCornerState(FloatRect(static_cast<float>(s.active.transform.tx()),
828 static_cast<float>(s.active.transform.ty()),
829 static_cast<float>(s.active.transform.tx() + s.active.w),
830 static_cast<float>(s.active.transform.ty() + s.active.h)),
831 radius);
832}
Vishnu Naire7f79c52020-10-29 14:45:03 -0700833
834bool BufferStateLayer::bufferNeedsFiltering() const {
835 const State& s(getDrawingState());
836 if (!s.buffer) {
837 return false;
838 }
839
840 uint32_t bufferWidth = s.buffer->width;
841 uint32_t bufferHeight = s.buffer->height;
842
843 // Undo any transformations on the buffer and return the result.
844 if (s.transform & ui::Transform::ROT_90) {
845 std::swap(bufferWidth, bufferHeight);
846 }
847
848 if (s.transformToDisplayInverse) {
849 uint32_t invTransform = DisplayDevice::getPrimaryDisplayRotationFlags();
850 if (invTransform & ui::Transform::ROT_90) {
851 std::swap(bufferWidth, bufferHeight);
852 }
853 }
854
855 const Rect layerSize{getBounds()};
856 return layerSize.width() != bufferWidth || layerSize.height() != bufferHeight;
857}
Robert Carr7121caf2020-12-15 13:07:32 -0800858
859void BufferStateLayer::incrementPendingBufferCount() {
860 mPendingBufferTransactions++;
861 tracePendingBufferCount();
862}
863
864void BufferStateLayer::decrementPendingBufferCount() {
865 mPendingBufferTransactions--;
866 tracePendingBufferCount();
867}
868
869void BufferStateLayer::tracePendingBufferCount() {
870 ATRACE_INT(mBlastTransactionName.c_str(), mPendingBufferTransactions);
871}
872
873uint32_t BufferStateLayer::doTransaction(uint32_t flags) {
874 if (mDrawingState.buffer != nullptr && mDrawingState.buffer != mBufferInfo.mBuffer) {
875 // If we are about to update mDrawingState.buffer but it has not yet latched
876 // then we will drop a buffer and should decrement the pending buffer count.
877 // This logic may not work perfectly in the face of a BufferStateLayer being the
878 // deferred side of a deferred transaction, but we don't expect this use case.
879 decrementPendingBufferCount();
880 }
881 return Layer::doTransaction(flags);
882}
883
Marissa Wall61c58622018-07-18 10:12:20 -0700884} // namespace android
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800885
886// TODO(b/129481165): remove the #pragma below and fix conversion issues
Marin Shalamanovbed7fd32020-12-21 20:02:20 +0100887#pragma clang diagnostic pop // ignored "-Wconversion -Wextra"