blob: a431028bde21e02d07171a5f099fd64d8bb4d9f1 [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
Lloyd Pique9755fb72019-03-26 14:44:40 -070031#include <compositionengine/LayerFECompositionState.h>
Marissa Wall947d34e2019-03-29 14:03:53 -070032#include <gui/BufferQueue.h>
Marissa Wall61c58622018-07-18 10:12:20 -070033#include <private/gui/SyncFeatures.h>
Peiyong Lincbc184f2018-08-22 13:24:10 -070034#include <renderengine/Image.h>
Marissa Wall61c58622018-07-18 10:12:20 -070035
Vishnu Nairfa247b12020-02-11 08:58:26 -080036#include "EffectLayer.h"
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080037#include "TimeStats/TimeStats.h"
Valerie Hau0bc09152018-12-20 07:42:47 -080038
Marissa Wall61c58622018-07-18 10:12:20 -070039namespace android {
40
Lloyd Pique42ab75e2018-09-12 20:46:03 -070041// clang-format off
42const std::array<float, 16> BufferStateLayer::IDENTITY_MATRIX{
43 1, 0, 0, 0,
44 0, 1, 0, 0,
45 0, 0, 1, 0,
46 0, 0, 0, 1
47};
48// clang-format on
Marissa Wall61c58622018-07-18 10:12:20 -070049
Marissa Wall947d34e2019-03-29 14:03:53 -070050BufferStateLayer::BufferStateLayer(const LayerCreationArgs& args)
51 : BufferLayer(args), mHwcSlotGenerator(new HwcSlotGenerator()) {
Marissa Wall3ff826c2019-02-07 11:58:25 -080052 mCurrentState.dataspace = ui::Dataspace::V0_SRGB;
Vishnu Nair60356342018-11-13 13:00:45 -080053}
Marissa Wall61c58622018-07-18 10:12:20 -070054
Alec Mouri4545a8a2019-08-08 20:05:32 -070055BufferStateLayer::~BufferStateLayer() {
chaviwb4c6e582019-08-16 14:35:07 -070056 // The original layer and the clone layer share the same texture and buffer. Therefore, only
57 // one of the layers, in this case the original layer, needs to handle the deletion. The
58 // original layer and the clone should be removed at the same time so there shouldn't be any
59 // issue with the clone layer trying to use the texture.
60 if (mBufferInfo.mBuffer != nullptr && !isClone()) {
chaviwd62d3062019-09-04 14:48:02 -070061 // Ensure that mBuffer is uncached from RenderEngine here, as
Alec Mouri4545a8a2019-08-08 20:05:32 -070062 // RenderEngine may have been using the buffer as an external texture
63 // after the client uncached the buffer.
64 auto& engine(mFlinger->getRenderEngine());
chaviwd62d3062019-09-04 14:48:02 -070065 engine.unbindExternalTextureBuffer(mBufferInfo.mBuffer->getId());
Alec Mouri4545a8a2019-08-08 20:05:32 -070066 }
67}
68
Robert Carr8d958532020-11-10 14:09:16 -080069status_t BufferStateLayer::addReleaseFence(const sp<CallbackHandle>& ch,
70 const sp<Fence>& fence) {
71 if (ch == nullptr) {
72 return OK;
73 }
74 if (!ch->previousReleaseFence.get()) {
75 ch->previousReleaseFence = fence;
76 return OK;
77 }
78
79 // Below logic is lifted from ConsumerBase.cpp:
80 // Check status of fences first because merging is expensive.
81 // Merging an invalid fence with any other fence results in an
82 // invalid fence.
83 auto currentStatus = ch->previousReleaseFence->getStatus();
84 if (currentStatus == Fence::Status::Invalid) {
85 ALOGE("Existing fence has invalid state, layer: %s", mName.c_str());
86 return BAD_VALUE;
87 }
88
89 auto incomingStatus = fence->getStatus();
90 if (incomingStatus == Fence::Status::Invalid) {
91 ALOGE("New fence has invalid state, layer: %s", mName.c_str());
92 ch->previousReleaseFence = fence;
93 return BAD_VALUE;
94 }
95
96 // If both fences are signaled or both are unsignaled, we need to merge
97 // them to get an accurate timestamp.
98 if (currentStatus == incomingStatus) {
99 char fenceName[32] = {};
100 snprintf(fenceName, 32, "%.28s", mName.c_str());
101 sp<Fence> mergedFence = Fence::merge(
102 fenceName, ch->previousReleaseFence, fence);
103 if (!mergedFence.get()) {
104 ALOGE("failed to merge release fences, layer: %s", mName.c_str());
105 // synchronization is broken, the best we can do is hope fences
106 // signal in order so the new fence will act like a union
107 ch->previousReleaseFence = fence;
108 return BAD_VALUE;
109 }
110 ch->previousReleaseFence = mergedFence;
111 } else if (incomingStatus == Fence::Status::Unsignaled) {
112 // If one fence has signaled and the other hasn't, the unsignaled
113 // fence will approximately correspond with the correct timestamp.
114 // There's a small race if both fences signal at about the same time
115 // and their statuses are retrieved with unfortunate timing. However,
116 // by this point, they will have both signaled and only the timestamp
117 // will be slightly off; any dependencies after this point will
118 // already have been met.
119 ch->previousReleaseFence = fence;
120 }
121 // else if (currentStatus == Fence::Status::Unsignaled) is a no-op.
122
123 return OK;
124}
125
Marissa Wall61c58622018-07-18 10:12:20 -0700126// -----------------------------------------------------------------------
127// Interface implementation for Layer
128// -----------------------------------------------------------------------
Marissa Wallfda30bb2018-10-12 11:34:28 -0700129void BufferStateLayer::onLayerDisplayed(const sp<Fence>& releaseFence) {
Robert Carr8d958532020-11-10 14:09:16 -0800130 if (!releaseFence->isValid()) {
131 return;
132 }
Marissa Wall5a68a772018-12-22 17:43:42 -0800133 // The previous release fence notifies the client that SurfaceFlinger is done with the previous
134 // buffer that was presented on this layer. The first transaction that came in this frame that
135 // replaced the previous buffer on this layer needs this release fence, because the fence will
136 // let the client know when that previous buffer is removed from the screen.
137 //
138 // Every other transaction on this layer does not need a release fence because no other
139 // Transactions that were set on this layer this frame are going to have their preceeding buffer
140 // removed from the display this frame.
141 //
142 // For example, if we have 3 transactions this frame. The first transaction doesn't contain a
143 // buffer so it doesn't need a previous release fence because the layer still needs the previous
144 // buffer. The second transaction contains a buffer so it needs a previous release fence because
145 // the previous buffer will be released this frame. The third transaction also contains a
146 // buffer. It replaces the buffer in the second transaction. The buffer in the second
147 // transaction will now no longer be presented so it is released immediately and the third
148 // transaction doesn't need a previous release fence.
Robert Carr8d958532020-11-10 14:09:16 -0800149 sp<CallbackHandle> ch;
Marissa Wall5a68a772018-12-22 17:43:42 -0800150 for (auto& handle : mDrawingState.callbackHandles) {
151 if (handle->releasePreviousBuffer) {
Robert Carr8d958532020-11-10 14:09:16 -0800152 ch = handle;
Marissa Wall5a68a772018-12-22 17:43:42 -0800153 break;
154 }
155 }
Robert Carr8d958532020-11-10 14:09:16 -0800156 auto status = addReleaseFence(ch, releaseFence);
157 if (status != OK) {
158 ALOGE("Failed to add release fence for layer %s", getName().c_str());
159 }
Mikael Pessa2e1608f2019-07-19 11:25:35 -0700160
Valerie Haubf784642020-01-29 07:25:23 -0800161 mPreviousReleaseFence = releaseFence;
162
Mikael Pessa2e1608f2019-07-19 11:25:35 -0700163 // Prevent tracing the same release multiple times.
164 if (mPreviousFrameNumber != mPreviousReleasedFrameNumber) {
Mikael Pessa2e1608f2019-07-19 11:25:35 -0700165 mPreviousReleasedFrameNumber = mPreviousFrameNumber;
166 }
Marissa Wall61c58622018-07-18 10:12:20 -0700167}
168
Jorim Jaggi9c03b502020-11-24 23:51:31 +0100169void BufferStateLayer::onSurfaceFrameCreated(
170 const std::shared_ptr<frametimeline::SurfaceFrame>& surfaceFrame) {
171 mPendingJankClassifications.emplace_back(surfaceFrame);
172}
173
Valerie Haubf784642020-01-29 07:25:23 -0800174void BufferStateLayer::releasePendingBuffer(nsecs_t dequeueReadyTime) {
Valerie Hau32cdc1f2019-10-21 14:45:54 -0700175 for (const auto& handle : mDrawingState.callbackHandles) {
176 handle->transformHint = mTransformHint;
Valerie Hau871d6352020-01-29 08:44:02 -0800177 handle->dequeueReadyTime = dequeueReadyTime;
Valerie Hau32cdc1f2019-10-21 14:45:54 -0700178 }
179
Jorim Jaggi9c03b502020-11-24 23:51:31 +0100180 std::vector<JankData> jankData;
181 jankData.reserve(mPendingJankClassifications.size());
182 while (!mPendingJankClassifications.empty()
183 && mPendingJankClassifications.front()->getJankType()) {
184 std::shared_ptr<frametimeline::SurfaceFrame> surfaceFrame =
185 mPendingJankClassifications.front();
186 mPendingJankClassifications.pop_front();
187 jankData.emplace_back(
188 JankData(surfaceFrame->getToken(), surfaceFrame->getJankType().value()));
189 }
190
Marissa Wallefb71af2019-06-27 14:45:53 -0700191 mFlinger->getTransactionCompletedThread().finalizePendingCallbackHandles(
Jorim Jaggi9c03b502020-11-24 23:51:31 +0100192 mDrawingState.callbackHandles, jankData);
Marissa Wall5a68a772018-12-22 17:43:42 -0800193
194 mDrawingState.callbackHandles = {};
Valerie Haubf784642020-01-29 07:25:23 -0800195
196 const sp<Fence>& releaseFence(mPreviousReleaseFence);
197 std::shared_ptr<FenceTime> releaseFenceTime = std::make_shared<FenceTime>(releaseFence);
198 {
199 Mutex::Autolock lock(mFrameEventHistoryMutex);
200 if (mPreviousFrameNumber != 0) {
201 mFrameEventHistory.addRelease(mPreviousFrameNumber, dequeueReadyTime,
202 std::move(releaseFenceTime));
203 }
204 }
Marissa Wall61c58622018-07-18 10:12:20 -0700205}
206
Valerie Hau871d6352020-01-29 08:44:02 -0800207void BufferStateLayer::finalizeFrameEventHistory(const std::shared_ptr<FenceTime>& glDoneFence,
208 const CompositorTiming& compositorTiming) {
209 for (const auto& handle : mDrawingState.callbackHandles) {
210 handle->gpuCompositionDoneFence = glDoneFence;
211 handle->compositorTiming = compositorTiming;
212 }
213}
214
Ana Krulec010d2192018-10-08 06:29:54 -0700215bool BufferStateLayer::shouldPresentNow(nsecs_t /*expectedPresentTime*/) const {
Marissa Wall61c58622018-07-18 10:12:20 -0700216 if (getSidebandStreamChanged() || getAutoRefresh()) {
217 return true;
218 }
219
Marissa Wall024a1912018-08-13 13:55:35 -0700220 return hasFrameUpdate();
Marissa Wall61c58622018-07-18 10:12:20 -0700221}
222
Marissa Walle2ffb422018-10-12 11:33:52 -0700223bool BufferStateLayer::willPresentCurrentTransaction() const {
224 // Returns true if the most recent Transaction applied to CurrentState will be presented.
Robert Carr321e83c2019-08-19 15:49:30 -0700225 return (getSidebandStreamChanged() || getAutoRefresh() ||
Valerie Hauaa194562019-02-05 16:21:38 -0800226 (mCurrentState.modified &&
Robert Carr321e83c2019-08-19 15:49:30 -0700227 (mCurrentState.buffer != nullptr || mCurrentState.bgColorLayer != nullptr))) &&
228 !mLayerDetached;
Marissa Wall61c58622018-07-18 10:12:20 -0700229}
230
Valerie Hau3282b3c2020-02-03 15:37:27 -0800231/* TODO: vhau uncomment once deferred transaction migration complete in
232 * WindowManager
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800233void BufferStateLayer::pushPendingState() {
234 if (!mCurrentState.modified) {
Marissa Wall61c58622018-07-18 10:12:20 -0700235 return;
236 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800237 mPendingStates.push_back(mCurrentState);
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700238 ATRACE_INT(mTransactionName.c_str(), mPendingStates.size());
Marissa Wall61c58622018-07-18 10:12:20 -0700239}
Valerie Hau3282b3c2020-02-03 15:37:27 -0800240*/
Marissa Wall61c58622018-07-18 10:12:20 -0700241
242bool BufferStateLayer::applyPendingStates(Layer::State* stateToCommit) {
Valerie Hau3282b3c2020-02-03 15:37:27 -0800243 mCurrentStateModified = mCurrentState.modified;
244 bool stateUpdateAvailable = Layer::applyPendingStates(stateToCommit);
Ady Abraham22c7b5c2020-09-22 19:33:40 -0700245 if (stateUpdateAvailable && mCallbackHandleAcquireTime != -1) {
Ady Abraham7f8a1e62020-09-28 16:09:35 -0700246 // Update the acquire fence time if we have a buffer
247 mSurfaceFrame->setAcquireFenceTime(mCallbackHandleAcquireTime);
Ady Abraham22c7b5c2020-09-22 19:33:40 -0700248 }
Valerie Hau3282b3c2020-02-03 15:37:27 -0800249 mCurrentStateModified = stateUpdateAvailable && mCurrentStateModified;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800250 mCurrentState.modified = false;
Marissa Wall61c58622018-07-18 10:12:20 -0700251 return stateUpdateAvailable;
252}
253
Marissa Wall861616d2018-10-22 12:52:23 -0700254// Crop that applies to the window
255Rect BufferStateLayer::getCrop(const Layer::State& /*s*/) const {
256 return Rect::INVALID_RECT;
Marissa Wall61c58622018-07-18 10:12:20 -0700257}
258
259bool BufferStateLayer::setTransform(uint32_t transform) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800260 if (mCurrentState.transform == transform) return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800261 mCurrentState.transform = transform;
262 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700263 setTransactionFlags(eTransactionNeeded);
264 return true;
265}
266
267bool BufferStateLayer::setTransformToDisplayInverse(bool transformToDisplayInverse) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800268 if (mCurrentState.transformToDisplayInverse == transformToDisplayInverse) return false;
269 mCurrentState.sequence++;
270 mCurrentState.transformToDisplayInverse = transformToDisplayInverse;
271 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700272 setTransactionFlags(eTransactionNeeded);
273 return true;
274}
275
276bool BufferStateLayer::setCrop(const Rect& crop) {
Marissa Wall290ad082019-03-06 13:23:47 -0800277 Rect c = crop;
278 if (c.left < 0) {
279 c.left = 0;
280 }
281 if (c.top < 0) {
282 c.top = 0;
283 }
284 // If the width and/or height are < 0, make it [0, 0, -1, -1] so the equality comparision below
285 // treats all invalid rectangles the same.
286 if (!c.isValid()) {
287 c.makeInvalid();
288 }
289
290 if (mCurrentState.crop == c) return false;
Marissa Wall290ad082019-03-06 13:23:47 -0800291 mCurrentState.crop = c;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800292 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700293 setTransactionFlags(eTransactionNeeded);
294 return true;
295}
296
Marissa Wall861616d2018-10-22 12:52:23 -0700297bool BufferStateLayer::setFrame(const Rect& frame) {
298 int x = frame.left;
299 int y = frame.top;
300 int w = frame.getWidth();
301 int h = frame.getHeight();
302
Marissa Wall0f3242d2018-12-20 15:10:22 -0800303 if (x < 0) {
304 x = 0;
305 w = frame.right;
306 }
307
308 if (y < 0) {
309 y = 0;
310 h = frame.bottom;
311 }
312
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800313 if (mCurrentState.active.transform.tx() == x && mCurrentState.active.transform.ty() == y &&
314 mCurrentState.active.w == w && mCurrentState.active.h == h) {
Marissa Wall861616d2018-10-22 12:52:23 -0700315 return false;
316 }
317
318 if (!frame.isValid()) {
319 x = y = w = h = 0;
320 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800321 mCurrentState.active.transform.set(x, y);
322 mCurrentState.active.w = w;
323 mCurrentState.active.h = h;
Marissa Wall861616d2018-10-22 12:52:23 -0700324
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800325 mCurrentState.sequence++;
326 mCurrentState.modified = true;
Marissa Wall861616d2018-10-22 12:52:23 -0700327 setTransactionFlags(eTransactionNeeded);
328 return true;
329}
330
Valerie Hau871d6352020-01-29 08:44:02 -0800331bool BufferStateLayer::addFrameEvent(const sp<Fence>& acquireFence, nsecs_t postedTime,
332 nsecs_t desiredPresentTime) {
Valerie Haubf784642020-01-29 07:25:23 -0800333 Mutex::Autolock lock(mFrameEventHistoryMutex);
334 mAcquireTimeline.updateSignalTimes();
335 std::shared_ptr<FenceTime> acquireFenceTime =
336 std::make_shared<FenceTime>((acquireFence ? acquireFence : Fence::NO_FENCE));
337 NewFrameEventsEntry newTimestamps = {mCurrentState.frameNumber, postedTime, desiredPresentTime,
338 acquireFenceTime};
Valerie Hau871d6352020-01-29 08:44:02 -0800339 mFrameEventHistory.setProducerWantsEvents();
Valerie Haubf784642020-01-29 07:25:23 -0800340 mFrameEventHistory.addQueue(newTimestamps);
341 return true;
342}
343
344bool BufferStateLayer::setBuffer(const sp<GraphicBuffer>& buffer, const sp<Fence>& acquireFence,
Ady Abrahamf0c56492020-12-17 18:04:15 -0800345 nsecs_t postTime, nsecs_t desiredPresentTime, bool isAutoTimestamp,
Vishnu Nairadf632b2021-01-07 14:05:08 -0800346 const client_cache_t& clientCacheId, uint64_t frameNumber,
347 std::optional<nsecs_t> /* dequeueTime */) {
Robert Carr0c1966e2020-10-19 12:12:08 -0700348 ATRACE_CALL();
349
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800350 if (mCurrentState.buffer) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700351 mReleasePreviousBuffer = true;
Robert Carr7121caf2020-12-15 13:07:32 -0800352 if (mCurrentState.buffer != mDrawingState.buffer) {
353 // If mCurrentState has a buffer, and we are about to update again
354 // before swapping to drawing state, then the first buffer will be
355 // dropped and we should decrement the pending buffer count.
356 decrementPendingBufferCount();
357 }
Marissa Wallfda30bb2018-10-12 11:34:28 -0700358 }
359
Vishnu Nair6b7c5c92020-09-29 17:27:05 -0700360 mCurrentState.frameNumber = frameNumber;
Valerie Hau2f54d642020-01-22 09:37:03 -0800361
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800362 mCurrentState.buffer = buffer;
Marissa Wall947d34e2019-03-29 14:03:53 -0700363 mCurrentState.clientCacheId = clientCacheId;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800364 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700365 setTransactionFlags(eTransactionNeeded);
Ady Abraham09bd3922019-04-08 10:44:56 -0700366
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800367 const int32_t layerId = getSequence();
Valerie Hau134651a2020-01-28 16:21:22 -0800368 mFlinger->mTimeStats->setPostTime(layerId, mCurrentState.frameNumber, getName().c_str(),
Alec Mouri9a29e672020-09-14 12:39:14 -0700369 mOwnerUid, postTime);
chaviwfa67b552019-08-12 16:51:55 -0700370 mCurrentState.desiredPresentTime = desiredPresentTime;
Ady Abrahamf0c56492020-12-17 18:04:15 -0800371 mCurrentState.isAutoTimestamp = isAutoTimestamp;
Ady Abraham09bd3922019-04-08 10:44:56 -0700372
Ady Abrahamf0c56492020-12-17 18:04:15 -0800373 mFlinger->mScheduler->recordLayerHistory(this, isAutoTimestamp ? 0 : desiredPresentTime,
Ady Abraham5def7332020-05-29 16:13:47 -0700374 LayerHistory::LayerUpdateType::Buffer);
Ady Abraham09bd3922019-04-08 10:44:56 -0700375
Ady Abrahamf0c56492020-12-17 18:04:15 -0800376 addFrameEvent(acquireFence, postTime, isAutoTimestamp ? 0 : desiredPresentTime);
Marissa Wall61c58622018-07-18 10:12:20 -0700377 return true;
378}
379
380bool BufferStateLayer::setAcquireFence(const sp<Fence>& fence) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700381 // The acquire fences of BufferStateLayers have already signaled before they are set
382 mCallbackHandleAcquireTime = fence->getSignalTime();
383
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800384 mCurrentState.acquireFence = fence;
385 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700386 setTransactionFlags(eTransactionNeeded);
387 return true;
388}
389
390bool BufferStateLayer::setDataspace(ui::Dataspace dataspace) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800391 if (mCurrentState.dataspace == dataspace) return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800392 mCurrentState.dataspace = dataspace;
393 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700394 setTransactionFlags(eTransactionNeeded);
395 return true;
396}
397
398bool BufferStateLayer::setHdrMetadata(const HdrMetadata& hdrMetadata) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800399 if (mCurrentState.hdrMetadata == hdrMetadata) return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800400 mCurrentState.hdrMetadata = hdrMetadata;
401 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700402 setTransactionFlags(eTransactionNeeded);
403 return true;
404}
405
406bool BufferStateLayer::setSurfaceDamageRegion(const Region& surfaceDamage) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800407 mCurrentState.surfaceDamageRegion = surfaceDamage;
408 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700409 setTransactionFlags(eTransactionNeeded);
410 return true;
411}
412
413bool BufferStateLayer::setApi(int32_t api) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800414 if (mCurrentState.api == api) return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800415 mCurrentState.api = api;
416 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700417 setTransactionFlags(eTransactionNeeded);
418 return true;
419}
420
421bool BufferStateLayer::setSidebandStream(const sp<NativeHandle>& sidebandStream) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800422 if (mCurrentState.sidebandStream == sidebandStream) return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800423 mCurrentState.sidebandStream = sidebandStream;
424 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700425 setTransactionFlags(eTransactionNeeded);
426
427 if (!mSidebandStreamChanged.exchange(true)) {
428 // mSidebandStreamChanged was false
429 mFlinger->signalLayerUpdate();
430 }
431 return true;
432}
433
Marissa Walle2ffb422018-10-12 11:33:52 -0700434bool BufferStateLayer::setTransactionCompletedListeners(
435 const std::vector<sp<CallbackHandle>>& handles) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700436 // If there is no handle, we will not send a callback so reset mReleasePreviousBuffer and return
Marissa Walle2ffb422018-10-12 11:33:52 -0700437 if (handles.empty()) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700438 mReleasePreviousBuffer = false;
Marissa Walle2ffb422018-10-12 11:33:52 -0700439 return false;
440 }
441
442 const bool willPresent = willPresentCurrentTransaction();
443
444 for (const auto& handle : handles) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700445 // If this transaction set a buffer on this layer, release its previous buffer
446 handle->releasePreviousBuffer = mReleasePreviousBuffer;
447
Marissa Walle2ffb422018-10-12 11:33:52 -0700448 // If this layer will be presented in this frame
449 if (willPresent) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700450 // If this transaction set an acquire fence on this layer, set its acquire time
451 handle->acquireTime = mCallbackHandleAcquireTime;
452
Marissa Walle2ffb422018-10-12 11:33:52 -0700453 // Notify the transaction completed thread that there is a pending latched callback
454 // handle
Marissa Wall5a68a772018-12-22 17:43:42 -0800455 mFlinger->getTransactionCompletedThread().registerPendingCallbackHandle(handle);
Marissa Walle2ffb422018-10-12 11:33:52 -0700456
457 // Store so latched time and release fence can be set
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800458 mCurrentState.callbackHandles.push_back(handle);
Marissa Walle2ffb422018-10-12 11:33:52 -0700459
460 } else { // If this layer will NOT need to be relatched and presented this frame
461 // Notify the transaction completed thread this handle is done
Marissa Wallefb71af2019-06-27 14:45:53 -0700462 mFlinger->getTransactionCompletedThread().registerUnpresentedCallbackHandle(handle);
Marissa Walle2ffb422018-10-12 11:33:52 -0700463 }
464 }
465
Marissa Wallfda30bb2018-10-12 11:34:28 -0700466 mReleasePreviousBuffer = false;
467 mCallbackHandleAcquireTime = -1;
468
Marissa Walle2ffb422018-10-12 11:33:52 -0700469 return willPresent;
470}
471
Valerie Hau7618b232020-01-09 16:03:08 -0800472void BufferStateLayer::forceSendCallbacks() {
473 mFlinger->getTransactionCompletedThread().finalizePendingCallbackHandles(
Jorim Jaggi9c03b502020-11-24 23:51:31 +0100474 mCurrentState.callbackHandles, std::vector<JankData>());
Valerie Hau7618b232020-01-09 16:03:08 -0800475}
476
Marissa Wall61c58622018-07-18 10:12:20 -0700477bool BufferStateLayer::setTransparentRegionHint(const Region& transparent) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800478 mCurrentState.transparentRegionHint = transparent;
479 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700480 setTransactionFlags(eTransactionNeeded);
481 return true;
482}
483
Marissa Wall861616d2018-10-22 12:52:23 -0700484Rect BufferStateLayer::getBufferSize(const State& s) const {
485 // for buffer state layers we use the display frame size as the buffer size.
486 if (getActiveWidth(s) < UINT32_MAX && getActiveHeight(s) < UINT32_MAX) {
487 return Rect(getActiveWidth(s), getActiveHeight(s));
Marissa Wall61c58622018-07-18 10:12:20 -0700488 }
489
chaviw7e72caf2020-12-02 16:50:43 -0800490 if (mBufferInfo.mBuffer == nullptr) {
491 return Rect::INVALID_RECT;
492 }
493
Marissa Wall861616d2018-10-22 12:52:23 -0700494 // if the display frame is not defined, use the parent bounds as the buffer size.
495 const auto& p = mDrawingParent.promote();
496 if (p != nullptr) {
Vishnu Nair4351ad52019-02-11 14:13:02 -0800497 Rect parentBounds = Rect(p->getBounds(Region()));
Marissa Wall861616d2018-10-22 12:52:23 -0700498 if (!parentBounds.isEmpty()) {
499 return parentBounds;
500 }
501 }
502
Marissa Wall861616d2018-10-22 12:52:23 -0700503 return Rect::INVALID_RECT;
Marissa Wall61c58622018-07-18 10:12:20 -0700504}
Vishnu Nair4351ad52019-02-11 14:13:02 -0800505
506FloatRect BufferStateLayer::computeSourceBounds(const FloatRect& parentBounds) const {
507 const State& s(getDrawingState());
508 // for buffer state layers we use the display frame size as the buffer size.
509 if (getActiveWidth(s) < UINT32_MAX && getActiveHeight(s) < UINT32_MAX) {
510 return FloatRect(0, 0, getActiveWidth(s), getActiveHeight(s));
511 }
512
513 // if the display frame is not defined, use the parent bounds as the buffer size.
514 return parentBounds;
515}
516
Marissa Wall61c58622018-07-18 10:12:20 -0700517// -----------------------------------------------------------------------
518
519// -----------------------------------------------------------------------
520// Interface implementation for BufferLayer
521// -----------------------------------------------------------------------
522bool BufferStateLayer::fenceHasSignaled() const {
Alec Mouri91f6df32020-01-30 08:48:58 -0800523 const bool fenceSignaled =
524 getDrawingState().acquireFence->getStatus() == Fence::Status::Signaled;
525 if (!fenceSignaled) {
526 mFlinger->mTimeStats->incrementLatchSkipped(getSequence(),
527 TimeStats::LatchSkipReason::LateAcquire);
528 }
529
530 return fenceSignaled;
Marissa Wall61c58622018-07-18 10:12:20 -0700531}
532
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700533bool BufferStateLayer::framePresentTimeIsCurrent(nsecs_t expectedPresentTime) const {
Ady Abrahamcd1580c2019-04-29 15:40:03 -0700534 if (!hasFrameUpdate() || isRemovedFromCurrentState()) {
535 return true;
536 }
537
Ady Abrahamf0c56492020-12-17 18:04:15 -0800538 return mCurrentState.isAutoTimestamp || mCurrentState.desiredPresentTime <= expectedPresentTime;
Ady Abrahamcd1580c2019-04-29 15:40:03 -0700539}
540
Valerie Hau871d6352020-01-29 08:44:02 -0800541bool BufferStateLayer::onPreComposition(nsecs_t refreshStartTime) {
542 for (const auto& handle : mDrawingState.callbackHandles) {
543 handle->refreshStartTime = refreshStartTime;
544 }
545 return BufferLayer::onPreComposition(refreshStartTime);
546}
547
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700548uint64_t BufferStateLayer::getFrameNumber(nsecs_t /*expectedPresentTime*/) const {
Valerie Hau134651a2020-01-28 16:21:22 -0800549 return mDrawingState.frameNumber;
Marissa Wall61c58622018-07-18 10:12:20 -0700550}
551
Robert Carrfe1209c2020-02-11 12:25:35 -0800552/**
553 * This is the frameNumber used for deferred transaction signalling. We need to use this because
554 * of cases where we defer a transaction for a surface to itself. In the BLAST world this
555 * may not make a huge amount of sense (Why not just merge the Buffer transaction with the
556 * deferred transaction?) but this is an important legacy use case, for example moving
557 * a window at the same time it draws makes use of this kind of technique. So anyway
558 * imagine we have something like this:
559 *
560 * Transaction { // containing
561 * Buffer -> frameNumber = 2
562 * DeferTransactionUntil -> frameNumber = 2
563 * Random other stuff
564 * }
565 * Now imagine getHeadFrameNumber returned mDrawingState.mFrameNumber (or mCurrentFrameNumber).
566 * Prior to doTransaction SurfaceFlinger will call notifyAvailableFrames, but because we
567 * haven't swapped mCurrentState to mDrawingState yet we will think the sync point
568 * is not ready. So we will return false from applyPendingState and not swap
569 * current state to drawing state. But because we don't swap current state
570 * to drawing state the number will never update and we will be stuck. This way
571 * we can see we need to return the frame number for the buffer we are about
572 * to apply.
573 */
574uint64_t BufferStateLayer::getHeadFrameNumber(nsecs_t /* expectedPresentTime */) const {
575 return mCurrentState.frameNumber;
576}
577
Vishnu Naircf26a0a2020-11-13 12:56:20 -0800578void BufferStateLayer::setAutoRefresh(bool autoRefresh) {
579 if (!mAutoRefresh.exchange(autoRefresh)) {
580 mFlinger->signalLayerUpdate();
581 }
Marissa Wall61c58622018-07-18 10:12:20 -0700582}
583
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800584bool BufferStateLayer::latchSidebandStream(bool& recomputeVisibleRegions) {
Marissa Wall61c58622018-07-18 10:12:20 -0700585 if (mSidebandStreamChanged.exchange(false)) {
586 const State& s(getDrawingState());
587 // mSidebandStreamChanged was true
Lloyd Pique0b785d82018-12-04 17:25:27 -0800588 mSidebandStream = s.sidebandStream;
Lloyd Piquede196652020-01-22 17:29:58 -0800589 editCompositionState()->sidebandStream = mSidebandStream;
Lloyd Pique0b785d82018-12-04 17:25:27 -0800590 if (mSidebandStream != nullptr) {
Marissa Wall61c58622018-07-18 10:12:20 -0700591 setTransactionFlags(eTransactionNeeded);
592 mFlinger->setTransactionFlags(eTraversalNeeded);
593 }
594 recomputeVisibleRegions = true;
595
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800596 return true;
Marissa Wall61c58622018-07-18 10:12:20 -0700597 }
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800598 return false;
Marissa Wall61c58622018-07-18 10:12:20 -0700599}
600
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800601bool BufferStateLayer::hasFrameUpdate() const {
Valerie Hauaa194562019-02-05 16:21:38 -0800602 const State& c(getCurrentState());
603 return mCurrentStateModified && (c.buffer != nullptr || c.bgColorLayer != nullptr);
Marissa Wall61c58622018-07-18 10:12:20 -0700604}
605
Ady Abrahamce4adf12020-12-15 18:45:12 -0800606nsecs_t BufferStateLayer::nextPredictedPresentTime() const {
607 if (!getDrawingState().isAutoTimestamp || !mSurfaceFrame) {
608 return 0;
609 }
610
611 return mSurfaceFrame->getPredictions().presentTime;
612}
613
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700614status_t BufferStateLayer::updateTexImage(bool& /*recomputeVisibleRegions*/, nsecs_t latchTime,
615 nsecs_t /*expectedPresentTime*/) {
Marissa Wall61c58622018-07-18 10:12:20 -0700616 const State& s(getDrawingState());
617
618 if (!s.buffer) {
Valerie Hauaa194562019-02-05 16:21:38 -0800619 if (s.bgColorLayer) {
620 for (auto& handle : mDrawingState.callbackHandles) {
621 handle->latchTime = latchTime;
622 }
623 }
Marissa Wall61c58622018-07-18 10:12:20 -0700624 return NO_ERROR;
625 }
626
Marissa Wall5a68a772018-12-22 17:43:42 -0800627 for (auto& handle : mDrawingState.callbackHandles) {
628 handle->latchTime = latchTime;
Valerie Hau871d6352020-01-29 08:44:02 -0800629 handle->frameNumber = mDrawingState.frameNumber;
Marissa Wall5a68a772018-12-22 17:43:42 -0800630 }
Marissa Walle2ffb422018-10-12 11:33:52 -0700631
Vishnu Nairea0de002020-11-17 17:42:37 -0800632 const int32_t layerId = getSequence();
Valerie Hau134651a2020-01-28 16:21:22 -0800633 mFlinger->mTimeStats->setAcquireFence(layerId, mDrawingState.frameNumber,
chaviw95631e32020-06-09 13:43:32 -0700634 std::make_shared<FenceTime>(mDrawingState.acquireFence));
Valerie Hau134651a2020-01-28 16:21:22 -0800635 mFlinger->mTimeStats->setLatchTime(layerId, mDrawingState.frameNumber, latchTime);
Marissa Wall61c58622018-07-18 10:12:20 -0700636
Marissa Wall16c112d2019-03-20 13:21:13 -0700637 mCurrentStateModified = false;
638
Marissa Wall61c58622018-07-18 10:12:20 -0700639 return NO_ERROR;
640}
641
642status_t BufferStateLayer::updateActiveBuffer() {
643 const State& s(getDrawingState());
644
645 if (s.buffer == nullptr) {
646 return BAD_VALUE;
647 }
Robert Carr7121caf2020-12-15 13:07:32 -0800648 decrementPendingBufferCount();
Marissa Wall61c58622018-07-18 10:12:20 -0700649
Mikael Pessa2e1608f2019-07-19 11:25:35 -0700650 mPreviousBufferId = getCurrentBufferId();
chaviwd62d3062019-09-04 14:48:02 -0700651 mBufferInfo.mBuffer = s.buffer;
652 mBufferInfo.mFence = s.acquireFence;
Marissa Wall61c58622018-07-18 10:12:20 -0700653
654 return NO_ERROR;
655}
656
Valerie Haubf784642020-01-29 07:25:23 -0800657status_t BufferStateLayer::updateFrameNumber(nsecs_t latchTime) {
Marissa Wall61c58622018-07-18 10:12:20 -0700658 // TODO(marissaw): support frame history events
Mikael Pessa2e1608f2019-07-19 11:25:35 -0700659 mPreviousFrameNumber = mCurrentFrameNumber;
Valerie Hau134651a2020-01-28 16:21:22 -0800660 mCurrentFrameNumber = mDrawingState.frameNumber;
Valerie Haubf784642020-01-29 07:25:23 -0800661 {
662 Mutex::Autolock lock(mFrameEventHistoryMutex);
663 mFrameEventHistory.addLatch(mCurrentFrameNumber, latchTime);
664 }
Marissa Wall61c58622018-07-18 10:12:20 -0700665 return NO_ERROR;
666}
667
Marissa Wall947d34e2019-03-29 14:03:53 -0700668void BufferStateLayer::HwcSlotGenerator::bufferErased(const client_cache_t& clientCacheId) {
669 std::lock_guard lock(mMutex);
670 if (!clientCacheId.isValid()) {
671 ALOGE("invalid process, failed to erase buffer");
672 return;
673 }
674 eraseBufferLocked(clientCacheId);
675}
676
677uint32_t BufferStateLayer::HwcSlotGenerator::getHwcCacheSlot(const client_cache_t& clientCacheId) {
678 std::lock_guard<std::mutex> lock(mMutex);
679 auto itr = mCachedBuffers.find(clientCacheId);
680 if (itr == mCachedBuffers.end()) {
681 return addCachedBuffer(clientCacheId);
682 }
683 auto& [hwcCacheSlot, counter] = itr->second;
684 counter = mCounter++;
685 return hwcCacheSlot;
686}
687
688uint32_t BufferStateLayer::HwcSlotGenerator::addCachedBuffer(const client_cache_t& clientCacheId)
689 REQUIRES(mMutex) {
690 if (!clientCacheId.isValid()) {
691 ALOGE("invalid process, returning invalid slot");
692 return BufferQueue::INVALID_BUFFER_SLOT;
693 }
694
695 ClientCache::getInstance().registerErasedRecipient(clientCacheId, wp<ErasedRecipient>(this));
696
697 uint32_t hwcCacheSlot = getFreeHwcCacheSlot();
698 mCachedBuffers[clientCacheId] = {hwcCacheSlot, mCounter++};
699 return hwcCacheSlot;
700}
701
702uint32_t BufferStateLayer::HwcSlotGenerator::getFreeHwcCacheSlot() REQUIRES(mMutex) {
703 if (mFreeHwcCacheSlots.empty()) {
704 evictLeastRecentlyUsed();
705 }
706
707 uint32_t hwcCacheSlot = mFreeHwcCacheSlots.top();
708 mFreeHwcCacheSlots.pop();
709 return hwcCacheSlot;
710}
711
712void BufferStateLayer::HwcSlotGenerator::evictLeastRecentlyUsed() REQUIRES(mMutex) {
713 uint64_t minCounter = UINT_MAX;
714 client_cache_t minClientCacheId = {};
715 for (const auto& [clientCacheId, slotCounter] : mCachedBuffers) {
716 const auto& [hwcCacheSlot, counter] = slotCounter;
717 if (counter < minCounter) {
718 minCounter = counter;
719 minClientCacheId = clientCacheId;
720 }
721 }
722 eraseBufferLocked(minClientCacheId);
723
724 ClientCache::getInstance().unregisterErasedRecipient(minClientCacheId, this);
725}
726
727void BufferStateLayer::HwcSlotGenerator::eraseBufferLocked(const client_cache_t& clientCacheId)
728 REQUIRES(mMutex) {
729 auto itr = mCachedBuffers.find(clientCacheId);
730 if (itr == mCachedBuffers.end()) {
731 return;
732 }
733 auto& [hwcCacheSlot, counter] = itr->second;
734
735 // TODO send to hwc cache and resources
736
737 mFreeHwcCacheSlots.push(hwcCacheSlot);
738 mCachedBuffers.erase(clientCacheId);
739}
chaviw4244e032019-09-04 11:27:49 -0700740
741void BufferStateLayer::gatherBufferInfo() {
chaviwdebadb82020-03-26 14:57:24 -0700742 BufferLayer::gatherBufferInfo();
chaviw4244e032019-09-04 11:27:49 -0700743
chaviwdebadb82020-03-26 14:57:24 -0700744 const State& s(getDrawingState());
chaviw4244e032019-09-04 11:27:49 -0700745 mBufferInfo.mDesiredPresentTime = s.desiredPresentTime;
746 mBufferInfo.mFenceTime = std::make_shared<FenceTime>(s.acquireFence);
747 mBufferInfo.mFence = s.acquireFence;
chaviw4244e032019-09-04 11:27:49 -0700748 mBufferInfo.mTransform = s.transform;
749 mBufferInfo.mDataspace = translateDataspace(s.dataspace);
750 mBufferInfo.mCrop = computeCrop(s);
751 mBufferInfo.mScaleMode = NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW;
752 mBufferInfo.mSurfaceDamage = s.surfaceDamageRegion;
753 mBufferInfo.mHdrMetadata = s.hdrMetadata;
754 mBufferInfo.mApi = s.api;
chaviw4244e032019-09-04 11:27:49 -0700755 mBufferInfo.mTransformToDisplayInverse = s.transformToDisplayInverse;
chaviwf83ce182019-09-12 14:43:08 -0700756 mBufferInfo.mBufferSlot = mHwcSlotGenerator->getHwcCacheSlot(s.clientCacheId);
chaviw4244e032019-09-04 11:27:49 -0700757}
758
Robert Carr916b0362020-10-06 13:53:03 -0700759uint32_t BufferStateLayer::getEffectiveScalingMode() const {
760 return NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW;
761}
762
chaviw4244e032019-09-04 11:27:49 -0700763Rect BufferStateLayer::computeCrop(const State& s) {
764 if (s.crop.isEmpty() && s.buffer) {
765 return s.buffer->getBounds();
766 } else if (s.buffer) {
767 Rect crop = s.crop;
768 crop.left = std::max(crop.left, 0);
769 crop.top = std::max(crop.top, 0);
770 uint32_t bufferWidth = s.buffer->getWidth();
771 uint32_t bufferHeight = s.buffer->getHeight();
772 if (bufferHeight <= std::numeric_limits<int32_t>::max() &&
773 bufferWidth <= std::numeric_limits<int32_t>::max()) {
774 crop.right = std::min(crop.right, static_cast<int32_t>(bufferWidth));
775 crop.bottom = std::min(crop.bottom, static_cast<int32_t>(bufferHeight));
776 }
777 if (!crop.isValid()) {
778 // Crop rect is out of bounds, return whole buffer
779 return s.buffer->getBounds();
780 }
781 return crop;
782 }
783 return s.crop;
784}
785
chaviwb4c6e582019-08-16 14:35:07 -0700786sp<Layer> BufferStateLayer::createClone() {
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700787 LayerCreationArgs args(mFlinger.get(), nullptr, mName + " (Mirror)", 0, 0, 0, LayerMetadata());
chaviwb4c6e582019-08-16 14:35:07 -0700788 args.textureName = mTextureName;
Lloyd Pique1c3a5eb2019-10-03 13:07:08 -0700789 sp<BufferStateLayer> layer = mFlinger->getFactory().createBufferStateLayer(args);
chaviwb4c6e582019-08-16 14:35:07 -0700790 layer->mHwcSlotGenerator = mHwcSlotGenerator;
791 layer->setInitialValuesForClone(this);
792 return layer;
793}
Valerie Hau92bf5482020-02-10 09:49:08 -0800794
795Layer::RoundedCornerState BufferStateLayer::getRoundedCornerState() const {
796 const auto& p = mDrawingParent.promote();
797 if (p != nullptr) {
798 RoundedCornerState parentState = p->getRoundedCornerState();
799 if (parentState.radius > 0) {
800 ui::Transform t = getActiveTransform(getDrawingState());
801 t = t.inverse();
802 parentState.cropRect = t.transform(parentState.cropRect);
803 // The rounded corners shader only accepts 1 corner radius for performance reasons,
804 // but a transform matrix can define horizontal and vertical scales.
805 // Let's take the average between both of them and pass into the shader, practically we
806 // never do this type of transformation on windows anyway.
807 parentState.radius *= (t[0][0] + t[1][1]) / 2.0f;
808 return parentState;
809 }
810 }
811 const float radius = getDrawingState().cornerRadius;
812 const State& s(getDrawingState());
813 if (radius <= 0 || (getActiveWidth(s) == UINT32_MAX && getActiveHeight(s) == UINT32_MAX))
814 return RoundedCornerState();
815 return RoundedCornerState(FloatRect(static_cast<float>(s.active.transform.tx()),
816 static_cast<float>(s.active.transform.ty()),
817 static_cast<float>(s.active.transform.tx() + s.active.w),
818 static_cast<float>(s.active.transform.ty() + s.active.h)),
819 radius);
820}
Vishnu Naire7f79c52020-10-29 14:45:03 -0700821
822bool BufferStateLayer::bufferNeedsFiltering() const {
823 const State& s(getDrawingState());
824 if (!s.buffer) {
825 return false;
826 }
827
828 uint32_t bufferWidth = s.buffer->width;
829 uint32_t bufferHeight = s.buffer->height;
830
831 // Undo any transformations on the buffer and return the result.
832 if (s.transform & ui::Transform::ROT_90) {
833 std::swap(bufferWidth, bufferHeight);
834 }
835
836 if (s.transformToDisplayInverse) {
837 uint32_t invTransform = DisplayDevice::getPrimaryDisplayRotationFlags();
838 if (invTransform & ui::Transform::ROT_90) {
839 std::swap(bufferWidth, bufferHeight);
840 }
841 }
842
843 const Rect layerSize{getBounds()};
844 return layerSize.width() != bufferWidth || layerSize.height() != bufferHeight;
845}
Robert Carr7121caf2020-12-15 13:07:32 -0800846
847void BufferStateLayer::incrementPendingBufferCount() {
848 mPendingBufferTransactions++;
849 tracePendingBufferCount();
850}
851
852void BufferStateLayer::decrementPendingBufferCount() {
853 mPendingBufferTransactions--;
854 tracePendingBufferCount();
855}
856
857void BufferStateLayer::tracePendingBufferCount() {
858 ATRACE_INT(mBlastTransactionName.c_str(), mPendingBufferTransactions);
859}
860
861uint32_t BufferStateLayer::doTransaction(uint32_t flags) {
862 if (mDrawingState.buffer != nullptr && mDrawingState.buffer != mBufferInfo.mBuffer) {
863 // If we are about to update mDrawingState.buffer but it has not yet latched
864 // then we will drop a buffer and should decrement the pending buffer count.
865 // This logic may not work perfectly in the face of a BufferStateLayer being the
866 // deferred side of a deferred transaction, but we don't expect this use case.
867 decrementPendingBufferCount();
868 }
869 return Layer::doTransaction(flags);
870}
871
Marissa Wall61c58622018-07-18 10:12:20 -0700872} // namespace android
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800873
874// TODO(b/129481165): remove the #pragma below and fix conversion issues
Marin Shalamanovbed7fd32020-12-21 20:02:20 +0100875#pragma clang diagnostic pop // ignored "-Wconversion -Wextra"