blob: 0b9cabae3d62efa80d5dbe5588c8ded7945fa9c3 [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"
20
Marissa Wall61c58622018-07-18 10:12:20 -070021//#define LOG_NDEBUG 0
22#undef LOG_TAG
23#define LOG_TAG "BufferStateLayer"
24#define ATRACE_TAG ATRACE_TAG_GRAPHICS
25
Lloyd Pique9755fb72019-03-26 14:44:40 -070026#include "BufferStateLayer.h"
27
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080028#include <limits>
Marissa Wall61c58622018-07-18 10:12:20 -070029
Lloyd Pique9755fb72019-03-26 14:44:40 -070030#include <compositionengine/LayerFECompositionState.h>
Marissa Wall947d34e2019-03-29 14:03:53 -070031#include <gui/BufferQueue.h>
Marissa Wall61c58622018-07-18 10:12:20 -070032#include <private/gui/SyncFeatures.h>
Peiyong Lincbc184f2018-08-22 13:24:10 -070033#include <renderengine/Image.h>
Marissa Wall61c58622018-07-18 10:12:20 -070034
Vishnu Nairfa247b12020-02-11 08:58:26 -080035#include "EffectLayer.h"
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080036#include "TimeStats/TimeStats.h"
Valerie Hau0bc09152018-12-20 07:42:47 -080037
Marissa Wall61c58622018-07-18 10:12:20 -070038namespace android {
39
Lloyd Pique42ab75e2018-09-12 20:46:03 -070040// clang-format off
41const std::array<float, 16> BufferStateLayer::IDENTITY_MATRIX{
42 1, 0, 0, 0,
43 0, 1, 0, 0,
44 0, 0, 1, 0,
45 0, 0, 0, 1
46};
47// clang-format on
Marissa Wall61c58622018-07-18 10:12:20 -070048
Marissa Wall947d34e2019-03-29 14:03:53 -070049BufferStateLayer::BufferStateLayer(const LayerCreationArgs& args)
50 : BufferLayer(args), mHwcSlotGenerator(new HwcSlotGenerator()) {
Marissa Wall3ff826c2019-02-07 11:58:25 -080051 mCurrentState.dataspace = ui::Dataspace::V0_SRGB;
Vishnu Nair60356342018-11-13 13:00:45 -080052}
Marissa Wall61c58622018-07-18 10:12:20 -070053
Alec Mouri4545a8a2019-08-08 20:05:32 -070054BufferStateLayer::~BufferStateLayer() {
chaviwb4c6e582019-08-16 14:35:07 -070055 // The original layer and the clone layer share the same texture and buffer. Therefore, only
56 // one of the layers, in this case the original layer, needs to handle the deletion. The
57 // original layer and the clone should be removed at the same time so there shouldn't be any
58 // issue with the clone layer trying to use the texture.
59 if (mBufferInfo.mBuffer != nullptr && !isClone()) {
chaviwd62d3062019-09-04 14:48:02 -070060 // Ensure that mBuffer is uncached from RenderEngine here, as
Alec Mouri4545a8a2019-08-08 20:05:32 -070061 // RenderEngine may have been using the buffer as an external texture
62 // after the client uncached the buffer.
63 auto& engine(mFlinger->getRenderEngine());
chaviwd62d3062019-09-04 14:48:02 -070064 engine.unbindExternalTextureBuffer(mBufferInfo.mBuffer->getId());
Alec Mouri4545a8a2019-08-08 20:05:32 -070065 }
66}
67
Robert Carr8d958532020-11-10 14:09:16 -080068status_t BufferStateLayer::addReleaseFence(const sp<CallbackHandle>& ch,
69 const sp<Fence>& fence) {
70 if (ch == nullptr) {
71 return OK;
72 }
73 if (!ch->previousReleaseFence.get()) {
74 ch->previousReleaseFence = fence;
75 return OK;
76 }
77
78 // Below logic is lifted from ConsumerBase.cpp:
79 // Check status of fences first because merging is expensive.
80 // Merging an invalid fence with any other fence results in an
81 // invalid fence.
82 auto currentStatus = ch->previousReleaseFence->getStatus();
83 if (currentStatus == Fence::Status::Invalid) {
84 ALOGE("Existing fence has invalid state, layer: %s", mName.c_str());
85 return BAD_VALUE;
86 }
87
88 auto incomingStatus = fence->getStatus();
89 if (incomingStatus == Fence::Status::Invalid) {
90 ALOGE("New fence has invalid state, layer: %s", mName.c_str());
91 ch->previousReleaseFence = fence;
92 return BAD_VALUE;
93 }
94
95 // If both fences are signaled or both are unsignaled, we need to merge
96 // them to get an accurate timestamp.
97 if (currentStatus == incomingStatus) {
98 char fenceName[32] = {};
99 snprintf(fenceName, 32, "%.28s", mName.c_str());
100 sp<Fence> mergedFence = Fence::merge(
101 fenceName, ch->previousReleaseFence, fence);
102 if (!mergedFence.get()) {
103 ALOGE("failed to merge release fences, layer: %s", mName.c_str());
104 // synchronization is broken, the best we can do is hope fences
105 // signal in order so the new fence will act like a union
106 ch->previousReleaseFence = fence;
107 return BAD_VALUE;
108 }
109 ch->previousReleaseFence = mergedFence;
110 } else if (incomingStatus == Fence::Status::Unsignaled) {
111 // If one fence has signaled and the other hasn't, the unsignaled
112 // fence will approximately correspond with the correct timestamp.
113 // There's a small race if both fences signal at about the same time
114 // and their statuses are retrieved with unfortunate timing. However,
115 // by this point, they will have both signaled and only the timestamp
116 // will be slightly off; any dependencies after this point will
117 // already have been met.
118 ch->previousReleaseFence = fence;
119 }
120 // else if (currentStatus == Fence::Status::Unsignaled) is a no-op.
121
122 return OK;
123}
124
Marissa Wall61c58622018-07-18 10:12:20 -0700125// -----------------------------------------------------------------------
126// Interface implementation for Layer
127// -----------------------------------------------------------------------
Marissa Wallfda30bb2018-10-12 11:34:28 -0700128void BufferStateLayer::onLayerDisplayed(const sp<Fence>& releaseFence) {
Robert Carr8d958532020-11-10 14:09:16 -0800129 if (!releaseFence->isValid()) {
130 return;
131 }
Marissa Wall5a68a772018-12-22 17:43:42 -0800132 // The previous release fence notifies the client that SurfaceFlinger is done with the previous
133 // buffer that was presented on this layer. The first transaction that came in this frame that
134 // replaced the previous buffer on this layer needs this release fence, because the fence will
135 // let the client know when that previous buffer is removed from the screen.
136 //
137 // Every other transaction on this layer does not need a release fence because no other
138 // Transactions that were set on this layer this frame are going to have their preceeding buffer
139 // removed from the display this frame.
140 //
141 // For example, if we have 3 transactions this frame. The first transaction doesn't contain a
142 // buffer so it doesn't need a previous release fence because the layer still needs the previous
143 // buffer. The second transaction contains a buffer so it needs a previous release fence because
144 // the previous buffer will be released this frame. The third transaction also contains a
145 // buffer. It replaces the buffer in the second transaction. The buffer in the second
146 // transaction will now no longer be presented so it is released immediately and the third
147 // transaction doesn't need a previous release fence.
Robert Carr8d958532020-11-10 14:09:16 -0800148 sp<CallbackHandle> ch;
Marissa Wall5a68a772018-12-22 17:43:42 -0800149 for (auto& handle : mDrawingState.callbackHandles) {
150 if (handle->releasePreviousBuffer) {
Robert Carr8d958532020-11-10 14:09:16 -0800151 ch = handle;
Marissa Wall5a68a772018-12-22 17:43:42 -0800152 break;
153 }
154 }
Robert Carr8d958532020-11-10 14:09:16 -0800155 auto status = addReleaseFence(ch, releaseFence);
156 if (status != OK) {
157 ALOGE("Failed to add release fence for layer %s", getName().c_str());
158 }
Mikael Pessa2e1608f2019-07-19 11:25:35 -0700159
Valerie Haubf784642020-01-29 07:25:23 -0800160 mPreviousReleaseFence = releaseFence;
161
Mikael Pessa2e1608f2019-07-19 11:25:35 -0700162 // Prevent tracing the same release multiple times.
163 if (mPreviousFrameNumber != mPreviousReleasedFrameNumber) {
Mikael Pessa2e1608f2019-07-19 11:25:35 -0700164 mPreviousReleasedFrameNumber = mPreviousFrameNumber;
165 }
Marissa Wall61c58622018-07-18 10:12:20 -0700166}
167
Jorim Jaggi9c03b502020-11-24 23:51:31 +0100168void BufferStateLayer::onSurfaceFrameCreated(
169 const std::shared_ptr<frametimeline::SurfaceFrame>& surfaceFrame) {
170 mPendingJankClassifications.emplace_back(surfaceFrame);
171}
172
Valerie Haubf784642020-01-29 07:25:23 -0800173void BufferStateLayer::releasePendingBuffer(nsecs_t dequeueReadyTime) {
Valerie Hau32cdc1f2019-10-21 14:45:54 -0700174 for (const auto& handle : mDrawingState.callbackHandles) {
175 handle->transformHint = mTransformHint;
Valerie Hau871d6352020-01-29 08:44:02 -0800176 handle->dequeueReadyTime = dequeueReadyTime;
Valerie Hau32cdc1f2019-10-21 14:45:54 -0700177 }
178
Jorim Jaggi9c03b502020-11-24 23:51:31 +0100179 std::vector<JankData> jankData;
180 jankData.reserve(mPendingJankClassifications.size());
181 while (!mPendingJankClassifications.empty()
182 && mPendingJankClassifications.front()->getJankType()) {
183 std::shared_ptr<frametimeline::SurfaceFrame> surfaceFrame =
184 mPendingJankClassifications.front();
185 mPendingJankClassifications.pop_front();
186 jankData.emplace_back(
187 JankData(surfaceFrame->getToken(), surfaceFrame->getJankType().value()));
188 }
189
Marissa Wallefb71af2019-06-27 14:45:53 -0700190 mFlinger->getTransactionCompletedThread().finalizePendingCallbackHandles(
Jorim Jaggi9c03b502020-11-24 23:51:31 +0100191 mDrawingState.callbackHandles, jankData);
Marissa Wall5a68a772018-12-22 17:43:42 -0800192
193 mDrawingState.callbackHandles = {};
Valerie Haubf784642020-01-29 07:25:23 -0800194
195 const sp<Fence>& releaseFence(mPreviousReleaseFence);
196 std::shared_ptr<FenceTime> releaseFenceTime = std::make_shared<FenceTime>(releaseFence);
197 {
198 Mutex::Autolock lock(mFrameEventHistoryMutex);
199 if (mPreviousFrameNumber != 0) {
200 mFrameEventHistory.addRelease(mPreviousFrameNumber, dequeueReadyTime,
201 std::move(releaseFenceTime));
202 }
203 }
Marissa Wall61c58622018-07-18 10:12:20 -0700204}
205
Valerie Hau871d6352020-01-29 08:44:02 -0800206void BufferStateLayer::finalizeFrameEventHistory(const std::shared_ptr<FenceTime>& glDoneFence,
207 const CompositorTiming& compositorTiming) {
208 for (const auto& handle : mDrawingState.callbackHandles) {
209 handle->gpuCompositionDoneFence = glDoneFence;
210 handle->compositorTiming = compositorTiming;
211 }
212}
213
Ana Krulec010d2192018-10-08 06:29:54 -0700214bool BufferStateLayer::shouldPresentNow(nsecs_t /*expectedPresentTime*/) const {
Marissa Wall61c58622018-07-18 10:12:20 -0700215 if (getSidebandStreamChanged() || getAutoRefresh()) {
216 return true;
217 }
218
Marissa Wall024a1912018-08-13 13:55:35 -0700219 return hasFrameUpdate();
Marissa Wall61c58622018-07-18 10:12:20 -0700220}
221
Marissa Walle2ffb422018-10-12 11:33:52 -0700222bool BufferStateLayer::willPresentCurrentTransaction() const {
223 // Returns true if the most recent Transaction applied to CurrentState will be presented.
Robert Carr321e83c2019-08-19 15:49:30 -0700224 return (getSidebandStreamChanged() || getAutoRefresh() ||
Valerie Hauaa194562019-02-05 16:21:38 -0800225 (mCurrentState.modified &&
Robert Carr321e83c2019-08-19 15:49:30 -0700226 (mCurrentState.buffer != nullptr || mCurrentState.bgColorLayer != nullptr))) &&
227 !mLayerDetached;
Marissa Wall61c58622018-07-18 10:12:20 -0700228}
229
Valerie Hau3282b3c2020-02-03 15:37:27 -0800230/* TODO: vhau uncomment once deferred transaction migration complete in
231 * WindowManager
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800232void BufferStateLayer::pushPendingState() {
233 if (!mCurrentState.modified) {
Marissa Wall61c58622018-07-18 10:12:20 -0700234 return;
235 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800236 mPendingStates.push_back(mCurrentState);
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700237 ATRACE_INT(mTransactionName.c_str(), mPendingStates.size());
Marissa Wall61c58622018-07-18 10:12:20 -0700238}
Valerie Hau3282b3c2020-02-03 15:37:27 -0800239*/
Marissa Wall61c58622018-07-18 10:12:20 -0700240
241bool BufferStateLayer::applyPendingStates(Layer::State* stateToCommit) {
Valerie Hau3282b3c2020-02-03 15:37:27 -0800242 mCurrentStateModified = mCurrentState.modified;
243 bool stateUpdateAvailable = Layer::applyPendingStates(stateToCommit);
Ady Abraham22c7b5c2020-09-22 19:33:40 -0700244 if (stateUpdateAvailable && mCallbackHandleAcquireTime != -1) {
Ady Abraham7f8a1e62020-09-28 16:09:35 -0700245 // Update the acquire fence time if we have a buffer
246 mSurfaceFrame->setAcquireFenceTime(mCallbackHandleAcquireTime);
Ady Abraham22c7b5c2020-09-22 19:33:40 -0700247 }
Valerie Hau3282b3c2020-02-03 15:37:27 -0800248 mCurrentStateModified = stateUpdateAvailable && mCurrentStateModified;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800249 mCurrentState.modified = false;
Marissa Wall61c58622018-07-18 10:12:20 -0700250 return stateUpdateAvailable;
251}
252
Marissa Wall861616d2018-10-22 12:52:23 -0700253// Crop that applies to the window
254Rect BufferStateLayer::getCrop(const Layer::State& /*s*/) const {
255 return Rect::INVALID_RECT;
Marissa Wall61c58622018-07-18 10:12:20 -0700256}
257
258bool BufferStateLayer::setTransform(uint32_t transform) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800259 if (mCurrentState.transform == transform) return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800260 mCurrentState.transform = transform;
261 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700262 setTransactionFlags(eTransactionNeeded);
263 return true;
264}
265
266bool BufferStateLayer::setTransformToDisplayInverse(bool transformToDisplayInverse) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800267 if (mCurrentState.transformToDisplayInverse == transformToDisplayInverse) return false;
268 mCurrentState.sequence++;
269 mCurrentState.transformToDisplayInverse = transformToDisplayInverse;
270 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700271 setTransactionFlags(eTransactionNeeded);
272 return true;
273}
274
275bool BufferStateLayer::setCrop(const Rect& crop) {
Marissa Wall290ad082019-03-06 13:23:47 -0800276 Rect c = crop;
277 if (c.left < 0) {
278 c.left = 0;
279 }
280 if (c.top < 0) {
281 c.top = 0;
282 }
283 // If the width and/or height are < 0, make it [0, 0, -1, -1] so the equality comparision below
284 // treats all invalid rectangles the same.
285 if (!c.isValid()) {
286 c.makeInvalid();
287 }
288
289 if (mCurrentState.crop == c) return false;
Marissa Wall290ad082019-03-06 13:23:47 -0800290 mCurrentState.crop = c;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800291 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700292 setTransactionFlags(eTransactionNeeded);
293 return true;
294}
295
Marissa Wall861616d2018-10-22 12:52:23 -0700296bool BufferStateLayer::setFrame(const Rect& frame) {
297 int x = frame.left;
298 int y = frame.top;
299 int w = frame.getWidth();
300 int h = frame.getHeight();
301
Marissa Wall0f3242d2018-12-20 15:10:22 -0800302 if (x < 0) {
303 x = 0;
304 w = frame.right;
305 }
306
307 if (y < 0) {
308 y = 0;
309 h = frame.bottom;
310 }
311
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800312 if (mCurrentState.active.transform.tx() == x && mCurrentState.active.transform.ty() == y &&
313 mCurrentState.active.w == w && mCurrentState.active.h == h) {
Marissa Wall861616d2018-10-22 12:52:23 -0700314 return false;
315 }
316
317 if (!frame.isValid()) {
318 x = y = w = h = 0;
319 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800320 mCurrentState.active.transform.set(x, y);
321 mCurrentState.active.w = w;
322 mCurrentState.active.h = h;
Marissa Wall861616d2018-10-22 12:52:23 -0700323
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800324 mCurrentState.sequence++;
325 mCurrentState.modified = true;
Marissa Wall861616d2018-10-22 12:52:23 -0700326 setTransactionFlags(eTransactionNeeded);
327 return true;
328}
329
Valerie Hau871d6352020-01-29 08:44:02 -0800330bool BufferStateLayer::addFrameEvent(const sp<Fence>& acquireFence, nsecs_t postedTime,
331 nsecs_t desiredPresentTime) {
Valerie Haubf784642020-01-29 07:25:23 -0800332 Mutex::Autolock lock(mFrameEventHistoryMutex);
333 mAcquireTimeline.updateSignalTimes();
334 std::shared_ptr<FenceTime> acquireFenceTime =
335 std::make_shared<FenceTime>((acquireFence ? acquireFence : Fence::NO_FENCE));
336 NewFrameEventsEntry newTimestamps = {mCurrentState.frameNumber, postedTime, desiredPresentTime,
337 acquireFenceTime};
Valerie Hau871d6352020-01-29 08:44:02 -0800338 mFrameEventHistory.setProducerWantsEvents();
Valerie Haubf784642020-01-29 07:25:23 -0800339 mFrameEventHistory.addQueue(newTimestamps);
340 return true;
341}
342
343bool BufferStateLayer::setBuffer(const sp<GraphicBuffer>& buffer, const sp<Fence>& acquireFence,
344 nsecs_t postTime, nsecs_t desiredPresentTime,
Vishnu Nair6b7c5c92020-09-29 17:27:05 -0700345 const client_cache_t& clientCacheId, uint64_t frameNumber) {
Robert Carr0c1966e2020-10-19 12:12:08 -0700346 ATRACE_CALL();
347
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800348 if (mCurrentState.buffer) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700349 mReleasePreviousBuffer = true;
Robert Carr7121caf2020-12-15 13:07:32 -0800350 if (mCurrentState.buffer != mDrawingState.buffer) {
351 // If mCurrentState has a buffer, and we are about to update again
352 // before swapping to drawing state, then the first buffer will be
353 // dropped and we should decrement the pending buffer count.
354 decrementPendingBufferCount();
355 }
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);
Valerie Hau871d6352020-01-29 08:44:02 -0800368 desiredPresentTime = desiredPresentTime <= 0 ? 0 : desiredPresentTime;
chaviwfa67b552019-08-12 16:51:55 -0700369 mCurrentState.desiredPresentTime = desiredPresentTime;
Ady Abraham09bd3922019-04-08 10:44:56 -0700370
Ady Abraham5def7332020-05-29 16:13:47 -0700371 mFlinger->mScheduler->recordLayerHistory(this, desiredPresentTime,
372 LayerHistory::LayerUpdateType::Buffer);
Ady Abraham09bd3922019-04-08 10:44:56 -0700373
Valerie Hau871d6352020-01-29 08:44:02 -0800374 addFrameEvent(acquireFence, postTime, desiredPresentTime);
Marissa Wall61c58622018-07-18 10:12:20 -0700375 return true;
376}
377
378bool BufferStateLayer::setAcquireFence(const sp<Fence>& fence) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700379 // The acquire fences of BufferStateLayers have already signaled before they are set
380 mCallbackHandleAcquireTime = fence->getSignalTime();
381
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800382 mCurrentState.acquireFence = fence;
383 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700384 setTransactionFlags(eTransactionNeeded);
385 return true;
386}
387
388bool BufferStateLayer::setDataspace(ui::Dataspace dataspace) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800389 if (mCurrentState.dataspace == dataspace) return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800390 mCurrentState.dataspace = dataspace;
391 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700392 setTransactionFlags(eTransactionNeeded);
393 return true;
394}
395
396bool BufferStateLayer::setHdrMetadata(const HdrMetadata& hdrMetadata) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800397 if (mCurrentState.hdrMetadata == hdrMetadata) return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800398 mCurrentState.hdrMetadata = hdrMetadata;
399 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700400 setTransactionFlags(eTransactionNeeded);
401 return true;
402}
403
404bool BufferStateLayer::setSurfaceDamageRegion(const Region& surfaceDamage) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800405 mCurrentState.surfaceDamageRegion = surfaceDamage;
406 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700407 setTransactionFlags(eTransactionNeeded);
408 return true;
409}
410
411bool BufferStateLayer::setApi(int32_t api) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800412 if (mCurrentState.api == api) return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800413 mCurrentState.api = api;
414 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700415 setTransactionFlags(eTransactionNeeded);
416 return true;
417}
418
419bool BufferStateLayer::setSidebandStream(const sp<NativeHandle>& sidebandStream) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800420 if (mCurrentState.sidebandStream == sidebandStream) return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800421 mCurrentState.sidebandStream = sidebandStream;
422 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700423 setTransactionFlags(eTransactionNeeded);
424
425 if (!mSidebandStreamChanged.exchange(true)) {
426 // mSidebandStreamChanged was false
427 mFlinger->signalLayerUpdate();
428 }
429 return true;
430}
431
Marissa Walle2ffb422018-10-12 11:33:52 -0700432bool BufferStateLayer::setTransactionCompletedListeners(
433 const std::vector<sp<CallbackHandle>>& handles) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700434 // If there is no handle, we will not send a callback so reset mReleasePreviousBuffer and return
Marissa Walle2ffb422018-10-12 11:33:52 -0700435 if (handles.empty()) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700436 mReleasePreviousBuffer = false;
Marissa Walle2ffb422018-10-12 11:33:52 -0700437 return false;
438 }
439
440 const bool willPresent = willPresentCurrentTransaction();
441
442 for (const auto& handle : handles) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700443 // If this transaction set a buffer on this layer, release its previous buffer
444 handle->releasePreviousBuffer = mReleasePreviousBuffer;
445
Marissa Walle2ffb422018-10-12 11:33:52 -0700446 // If this layer will be presented in this frame
447 if (willPresent) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700448 // If this transaction set an acquire fence on this layer, set its acquire time
449 handle->acquireTime = mCallbackHandleAcquireTime;
450
Marissa Walle2ffb422018-10-12 11:33:52 -0700451 // Notify the transaction completed thread that there is a pending latched callback
452 // handle
Marissa Wall5a68a772018-12-22 17:43:42 -0800453 mFlinger->getTransactionCompletedThread().registerPendingCallbackHandle(handle);
Marissa Walle2ffb422018-10-12 11:33:52 -0700454
455 // Store so latched time and release fence can be set
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800456 mCurrentState.callbackHandles.push_back(handle);
Marissa Walle2ffb422018-10-12 11:33:52 -0700457
458 } else { // If this layer will NOT need to be relatched and presented this frame
459 // Notify the transaction completed thread this handle is done
Marissa Wallefb71af2019-06-27 14:45:53 -0700460 mFlinger->getTransactionCompletedThread().registerUnpresentedCallbackHandle(handle);
Marissa Walle2ffb422018-10-12 11:33:52 -0700461 }
462 }
463
Marissa Wallfda30bb2018-10-12 11:34:28 -0700464 mReleasePreviousBuffer = false;
465 mCallbackHandleAcquireTime = -1;
466
Marissa Walle2ffb422018-10-12 11:33:52 -0700467 return willPresent;
468}
469
Valerie Hau7618b232020-01-09 16:03:08 -0800470void BufferStateLayer::forceSendCallbacks() {
471 mFlinger->getTransactionCompletedThread().finalizePendingCallbackHandles(
Jorim Jaggi9c03b502020-11-24 23:51:31 +0100472 mCurrentState.callbackHandles, std::vector<JankData>());
Valerie Hau7618b232020-01-09 16:03:08 -0800473}
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
chaviwfa67b552019-08-12 16:51:55 -0700536 return 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
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700604status_t BufferStateLayer::updateTexImage(bool& /*recomputeVisibleRegions*/, nsecs_t latchTime,
605 nsecs_t /*expectedPresentTime*/) {
Marissa Wall61c58622018-07-18 10:12:20 -0700606 const State& s(getDrawingState());
607
608 if (!s.buffer) {
Valerie Hauaa194562019-02-05 16:21:38 -0800609 if (s.bgColorLayer) {
610 for (auto& handle : mDrawingState.callbackHandles) {
611 handle->latchTime = latchTime;
612 }
613 }
Marissa Wall61c58622018-07-18 10:12:20 -0700614 return NO_ERROR;
615 }
616
Marissa Wall5a68a772018-12-22 17:43:42 -0800617 for (auto& handle : mDrawingState.callbackHandles) {
618 handle->latchTime = latchTime;
Valerie Hau871d6352020-01-29 08:44:02 -0800619 handle->frameNumber = mDrawingState.frameNumber;
Marissa Wall5a68a772018-12-22 17:43:42 -0800620 }
Marissa Walle2ffb422018-10-12 11:33:52 -0700621
Vishnu Nairea0de002020-11-17 17:42:37 -0800622 const int32_t layerId = getSequence();
Valerie Hau134651a2020-01-28 16:21:22 -0800623 mFlinger->mTimeStats->setAcquireFence(layerId, mDrawingState.frameNumber,
chaviw95631e32020-06-09 13:43:32 -0700624 std::make_shared<FenceTime>(mDrawingState.acquireFence));
Valerie Hau134651a2020-01-28 16:21:22 -0800625 mFlinger->mTimeStats->setLatchTime(layerId, mDrawingState.frameNumber, latchTime);
Marissa Wall61c58622018-07-18 10:12:20 -0700626
Marissa Wall16c112d2019-03-20 13:21:13 -0700627 mCurrentStateModified = false;
628
Marissa Wall61c58622018-07-18 10:12:20 -0700629 return NO_ERROR;
630}
631
632status_t BufferStateLayer::updateActiveBuffer() {
633 const State& s(getDrawingState());
634
635 if (s.buffer == nullptr) {
636 return BAD_VALUE;
637 }
Robert Carr7121caf2020-12-15 13:07:32 -0800638 decrementPendingBufferCount();
Marissa Wall61c58622018-07-18 10:12:20 -0700639
Mikael Pessa2e1608f2019-07-19 11:25:35 -0700640 mPreviousBufferId = getCurrentBufferId();
chaviwd62d3062019-09-04 14:48:02 -0700641 mBufferInfo.mBuffer = s.buffer;
642 mBufferInfo.mFence = s.acquireFence;
Marissa Wall61c58622018-07-18 10:12:20 -0700643
644 return NO_ERROR;
645}
646
Valerie Haubf784642020-01-29 07:25:23 -0800647status_t BufferStateLayer::updateFrameNumber(nsecs_t latchTime) {
Marissa Wall61c58622018-07-18 10:12:20 -0700648 // TODO(marissaw): support frame history events
Mikael Pessa2e1608f2019-07-19 11:25:35 -0700649 mPreviousFrameNumber = mCurrentFrameNumber;
Valerie Hau134651a2020-01-28 16:21:22 -0800650 mCurrentFrameNumber = mDrawingState.frameNumber;
Valerie Haubf784642020-01-29 07:25:23 -0800651 {
652 Mutex::Autolock lock(mFrameEventHistoryMutex);
653 mFrameEventHistory.addLatch(mCurrentFrameNumber, latchTime);
654 }
Marissa Wall61c58622018-07-18 10:12:20 -0700655 return NO_ERROR;
656}
657
Marissa Wall947d34e2019-03-29 14:03:53 -0700658void BufferStateLayer::HwcSlotGenerator::bufferErased(const client_cache_t& clientCacheId) {
659 std::lock_guard lock(mMutex);
660 if (!clientCacheId.isValid()) {
661 ALOGE("invalid process, failed to erase buffer");
662 return;
663 }
664 eraseBufferLocked(clientCacheId);
665}
666
667uint32_t BufferStateLayer::HwcSlotGenerator::getHwcCacheSlot(const client_cache_t& clientCacheId) {
668 std::lock_guard<std::mutex> lock(mMutex);
669 auto itr = mCachedBuffers.find(clientCacheId);
670 if (itr == mCachedBuffers.end()) {
671 return addCachedBuffer(clientCacheId);
672 }
673 auto& [hwcCacheSlot, counter] = itr->second;
674 counter = mCounter++;
675 return hwcCacheSlot;
676}
677
678uint32_t BufferStateLayer::HwcSlotGenerator::addCachedBuffer(const client_cache_t& clientCacheId)
679 REQUIRES(mMutex) {
680 if (!clientCacheId.isValid()) {
681 ALOGE("invalid process, returning invalid slot");
682 return BufferQueue::INVALID_BUFFER_SLOT;
683 }
684
685 ClientCache::getInstance().registerErasedRecipient(clientCacheId, wp<ErasedRecipient>(this));
686
687 uint32_t hwcCacheSlot = getFreeHwcCacheSlot();
688 mCachedBuffers[clientCacheId] = {hwcCacheSlot, mCounter++};
689 return hwcCacheSlot;
690}
691
692uint32_t BufferStateLayer::HwcSlotGenerator::getFreeHwcCacheSlot() REQUIRES(mMutex) {
693 if (mFreeHwcCacheSlots.empty()) {
694 evictLeastRecentlyUsed();
695 }
696
697 uint32_t hwcCacheSlot = mFreeHwcCacheSlots.top();
698 mFreeHwcCacheSlots.pop();
699 return hwcCacheSlot;
700}
701
702void BufferStateLayer::HwcSlotGenerator::evictLeastRecentlyUsed() REQUIRES(mMutex) {
703 uint64_t minCounter = UINT_MAX;
704 client_cache_t minClientCacheId = {};
705 for (const auto& [clientCacheId, slotCounter] : mCachedBuffers) {
706 const auto& [hwcCacheSlot, counter] = slotCounter;
707 if (counter < minCounter) {
708 minCounter = counter;
709 minClientCacheId = clientCacheId;
710 }
711 }
712 eraseBufferLocked(minClientCacheId);
713
714 ClientCache::getInstance().unregisterErasedRecipient(minClientCacheId, this);
715}
716
717void BufferStateLayer::HwcSlotGenerator::eraseBufferLocked(const client_cache_t& clientCacheId)
718 REQUIRES(mMutex) {
719 auto itr = mCachedBuffers.find(clientCacheId);
720 if (itr == mCachedBuffers.end()) {
721 return;
722 }
723 auto& [hwcCacheSlot, counter] = itr->second;
724
725 // TODO send to hwc cache and resources
726
727 mFreeHwcCacheSlots.push(hwcCacheSlot);
728 mCachedBuffers.erase(clientCacheId);
729}
chaviw4244e032019-09-04 11:27:49 -0700730
731void BufferStateLayer::gatherBufferInfo() {
chaviwdebadb82020-03-26 14:57:24 -0700732 BufferLayer::gatherBufferInfo();
chaviw4244e032019-09-04 11:27:49 -0700733
chaviwdebadb82020-03-26 14:57:24 -0700734 const State& s(getDrawingState());
chaviw4244e032019-09-04 11:27:49 -0700735 mBufferInfo.mDesiredPresentTime = s.desiredPresentTime;
736 mBufferInfo.mFenceTime = std::make_shared<FenceTime>(s.acquireFence);
737 mBufferInfo.mFence = s.acquireFence;
chaviw4244e032019-09-04 11:27:49 -0700738 mBufferInfo.mTransform = s.transform;
739 mBufferInfo.mDataspace = translateDataspace(s.dataspace);
740 mBufferInfo.mCrop = computeCrop(s);
741 mBufferInfo.mScaleMode = NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW;
742 mBufferInfo.mSurfaceDamage = s.surfaceDamageRegion;
743 mBufferInfo.mHdrMetadata = s.hdrMetadata;
744 mBufferInfo.mApi = s.api;
chaviw4244e032019-09-04 11:27:49 -0700745 mBufferInfo.mTransformToDisplayInverse = s.transformToDisplayInverse;
chaviwf83ce182019-09-12 14:43:08 -0700746 mBufferInfo.mBufferSlot = mHwcSlotGenerator->getHwcCacheSlot(s.clientCacheId);
chaviw4244e032019-09-04 11:27:49 -0700747}
748
Robert Carr916b0362020-10-06 13:53:03 -0700749uint32_t BufferStateLayer::getEffectiveScalingMode() const {
750 return NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW;
751}
752
chaviw4244e032019-09-04 11:27:49 -0700753Rect BufferStateLayer::computeCrop(const State& s) {
754 if (s.crop.isEmpty() && s.buffer) {
755 return s.buffer->getBounds();
756 } else if (s.buffer) {
757 Rect crop = s.crop;
758 crop.left = std::max(crop.left, 0);
759 crop.top = std::max(crop.top, 0);
760 uint32_t bufferWidth = s.buffer->getWidth();
761 uint32_t bufferHeight = s.buffer->getHeight();
762 if (bufferHeight <= std::numeric_limits<int32_t>::max() &&
763 bufferWidth <= std::numeric_limits<int32_t>::max()) {
764 crop.right = std::min(crop.right, static_cast<int32_t>(bufferWidth));
765 crop.bottom = std::min(crop.bottom, static_cast<int32_t>(bufferHeight));
766 }
767 if (!crop.isValid()) {
768 // Crop rect is out of bounds, return whole buffer
769 return s.buffer->getBounds();
770 }
771 return crop;
772 }
773 return s.crop;
774}
775
chaviwb4c6e582019-08-16 14:35:07 -0700776sp<Layer> BufferStateLayer::createClone() {
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700777 LayerCreationArgs args(mFlinger.get(), nullptr, mName + " (Mirror)", 0, 0, 0, LayerMetadata());
chaviwb4c6e582019-08-16 14:35:07 -0700778 args.textureName = mTextureName;
Lloyd Pique1c3a5eb2019-10-03 13:07:08 -0700779 sp<BufferStateLayer> layer = mFlinger->getFactory().createBufferStateLayer(args);
chaviwb4c6e582019-08-16 14:35:07 -0700780 layer->mHwcSlotGenerator = mHwcSlotGenerator;
781 layer->setInitialValuesForClone(this);
782 return layer;
783}
Valerie Hau92bf5482020-02-10 09:49:08 -0800784
785Layer::RoundedCornerState BufferStateLayer::getRoundedCornerState() const {
786 const auto& p = mDrawingParent.promote();
787 if (p != nullptr) {
788 RoundedCornerState parentState = p->getRoundedCornerState();
789 if (parentState.radius > 0) {
790 ui::Transform t = getActiveTransform(getDrawingState());
791 t = t.inverse();
792 parentState.cropRect = t.transform(parentState.cropRect);
793 // The rounded corners shader only accepts 1 corner radius for performance reasons,
794 // but a transform matrix can define horizontal and vertical scales.
795 // Let's take the average between both of them and pass into the shader, practically we
796 // never do this type of transformation on windows anyway.
797 parentState.radius *= (t[0][0] + t[1][1]) / 2.0f;
798 return parentState;
799 }
800 }
801 const float radius = getDrawingState().cornerRadius;
802 const State& s(getDrawingState());
803 if (radius <= 0 || (getActiveWidth(s) == UINT32_MAX && getActiveHeight(s) == UINT32_MAX))
804 return RoundedCornerState();
805 return RoundedCornerState(FloatRect(static_cast<float>(s.active.transform.tx()),
806 static_cast<float>(s.active.transform.ty()),
807 static_cast<float>(s.active.transform.tx() + s.active.w),
808 static_cast<float>(s.active.transform.ty() + s.active.h)),
809 radius);
810}
Vishnu Naire7f79c52020-10-29 14:45:03 -0700811
812bool BufferStateLayer::bufferNeedsFiltering() const {
813 const State& s(getDrawingState());
814 if (!s.buffer) {
815 return false;
816 }
817
818 uint32_t bufferWidth = s.buffer->width;
819 uint32_t bufferHeight = s.buffer->height;
820
821 // Undo any transformations on the buffer and return the result.
822 if (s.transform & ui::Transform::ROT_90) {
823 std::swap(bufferWidth, bufferHeight);
824 }
825
826 if (s.transformToDisplayInverse) {
827 uint32_t invTransform = DisplayDevice::getPrimaryDisplayRotationFlags();
828 if (invTransform & ui::Transform::ROT_90) {
829 std::swap(bufferWidth, bufferHeight);
830 }
831 }
832
833 const Rect layerSize{getBounds()};
834 return layerSize.width() != bufferWidth || layerSize.height() != bufferHeight;
835}
Robert Carr7121caf2020-12-15 13:07:32 -0800836
837void BufferStateLayer::incrementPendingBufferCount() {
838 mPendingBufferTransactions++;
839 tracePendingBufferCount();
840}
841
842void BufferStateLayer::decrementPendingBufferCount() {
843 mPendingBufferTransactions--;
844 tracePendingBufferCount();
845}
846
847void BufferStateLayer::tracePendingBufferCount() {
848 ATRACE_INT(mBlastTransactionName.c_str(), mPendingBufferTransactions);
849}
850
851uint32_t BufferStateLayer::doTransaction(uint32_t flags) {
852 if (mDrawingState.buffer != nullptr && mDrawingState.buffer != mBufferInfo.mBuffer) {
853 // If we are about to update mDrawingState.buffer but it has not yet latched
854 // then we will drop a buffer and should decrement the pending buffer count.
855 // This logic may not work perfectly in the face of a BufferStateLayer being the
856 // deferred side of a deferred transaction, but we don't expect this use case.
857 decrementPendingBufferCount();
858 }
859 return Layer::doTransaction(flags);
860}
861
Marissa Wall61c58622018-07-18 10:12:20 -0700862} // namespace android
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800863
864// TODO(b/129481165): remove the #pragma below and fix conversion issues
865#pragma clang diagnostic pop // ignored "-Wconversion"