blob: 763b453caa443ba9fb4cbc4f1c02b7c90f8a555a [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
Robert Carr9a803c32021-01-14 16:57:58 -0800193 mFlinger->getTransactionCallbackInvoker().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;
Vishnu Nair935590e2021-02-10 13:05:52 -0800455 handle->frameNumber = mCurrentState.frameNumber;
Marissa Wallfda30bb2018-10-12 11:34:28 -0700456
Marissa Walle2ffb422018-10-12 11:33:52 -0700457 // Notify the transaction completed thread that there is a pending latched callback
458 // handle
Robert Carr9a803c32021-01-14 16:57:58 -0800459 mFlinger->getTransactionCallbackInvoker().registerPendingCallbackHandle(handle);
Marissa Walle2ffb422018-10-12 11:33:52 -0700460
461 // Store so latched time and release fence can be set
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800462 mCurrentState.callbackHandles.push_back(handle);
Marissa Walle2ffb422018-10-12 11:33:52 -0700463
464 } else { // If this layer will NOT need to be relatched and presented this frame
465 // Notify the transaction completed thread this handle is done
Robert Carr9a803c32021-01-14 16:57:58 -0800466 mFlinger->getTransactionCallbackInvoker().registerUnpresentedCallbackHandle(handle);
Marissa Walle2ffb422018-10-12 11:33:52 -0700467 }
468 }
469
Marissa Wallfda30bb2018-10-12 11:34:28 -0700470 mReleasePreviousBuffer = false;
471 mCallbackHandleAcquireTime = -1;
472
Marissa Walle2ffb422018-10-12 11:33:52 -0700473 return willPresent;
474}
475
Marissa Wall61c58622018-07-18 10:12:20 -0700476bool BufferStateLayer::setTransparentRegionHint(const Region& transparent) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800477 mCurrentState.transparentRegionHint = transparent;
478 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700479 setTransactionFlags(eTransactionNeeded);
480 return true;
481}
482
Marissa Wall861616d2018-10-22 12:52:23 -0700483Rect BufferStateLayer::getBufferSize(const State& s) const {
484 // for buffer state layers we use the display frame size as the buffer size.
485 if (getActiveWidth(s) < UINT32_MAX && getActiveHeight(s) < UINT32_MAX) {
486 return Rect(getActiveWidth(s), getActiveHeight(s));
Marissa Wall61c58622018-07-18 10:12:20 -0700487 }
488
chaviw7e72caf2020-12-02 16:50:43 -0800489 if (mBufferInfo.mBuffer == nullptr) {
490 return Rect::INVALID_RECT;
491 }
492
Marissa Wall861616d2018-10-22 12:52:23 -0700493 // if the display frame is not defined, use the parent bounds as the buffer size.
494 const auto& p = mDrawingParent.promote();
495 if (p != nullptr) {
Vishnu Nair4351ad52019-02-11 14:13:02 -0800496 Rect parentBounds = Rect(p->getBounds(Region()));
Marissa Wall861616d2018-10-22 12:52:23 -0700497 if (!parentBounds.isEmpty()) {
498 return parentBounds;
499 }
500 }
501
Marissa Wall861616d2018-10-22 12:52:23 -0700502 return Rect::INVALID_RECT;
Marissa Wall61c58622018-07-18 10:12:20 -0700503}
Vishnu Nair4351ad52019-02-11 14:13:02 -0800504
505FloatRect BufferStateLayer::computeSourceBounds(const FloatRect& parentBounds) const {
506 const State& s(getDrawingState());
507 // for buffer state layers we use the display frame size as the buffer size.
508 if (getActiveWidth(s) < UINT32_MAX && getActiveHeight(s) < UINT32_MAX) {
509 return FloatRect(0, 0, getActiveWidth(s), getActiveHeight(s));
510 }
511
512 // if the display frame is not defined, use the parent bounds as the buffer size.
513 return parentBounds;
514}
515
Marissa Wall61c58622018-07-18 10:12:20 -0700516// -----------------------------------------------------------------------
517
518// -----------------------------------------------------------------------
519// Interface implementation for BufferLayer
520// -----------------------------------------------------------------------
521bool BufferStateLayer::fenceHasSignaled() const {
Alec Mouri91f6df32020-01-30 08:48:58 -0800522 const bool fenceSignaled =
523 getDrawingState().acquireFence->getStatus() == Fence::Status::Signaled;
524 if (!fenceSignaled) {
525 mFlinger->mTimeStats->incrementLatchSkipped(getSequence(),
526 TimeStats::LatchSkipReason::LateAcquire);
527 }
528
529 return fenceSignaled;
Marissa Wall61c58622018-07-18 10:12:20 -0700530}
531
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700532bool BufferStateLayer::framePresentTimeIsCurrent(nsecs_t expectedPresentTime) const {
Ady Abrahamcd1580c2019-04-29 15:40:03 -0700533 if (!hasFrameUpdate() || isRemovedFromCurrentState()) {
534 return true;
535 }
536
Ady Abrahamf0c56492020-12-17 18:04:15 -0800537 return mCurrentState.isAutoTimestamp || mCurrentState.desiredPresentTime <= expectedPresentTime;
Ady Abrahamcd1580c2019-04-29 15:40:03 -0700538}
539
Valerie Hau871d6352020-01-29 08:44:02 -0800540bool BufferStateLayer::onPreComposition(nsecs_t refreshStartTime) {
541 for (const auto& handle : mDrawingState.callbackHandles) {
542 handle->refreshStartTime = refreshStartTime;
543 }
544 return BufferLayer::onPreComposition(refreshStartTime);
545}
546
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700547uint64_t BufferStateLayer::getFrameNumber(nsecs_t /*expectedPresentTime*/) const {
Valerie Hau134651a2020-01-28 16:21:22 -0800548 return mDrawingState.frameNumber;
Marissa Wall61c58622018-07-18 10:12:20 -0700549}
550
Robert Carrfe1209c2020-02-11 12:25:35 -0800551/**
552 * This is the frameNumber used for deferred transaction signalling. We need to use this because
553 * of cases where we defer a transaction for a surface to itself. In the BLAST world this
554 * may not make a huge amount of sense (Why not just merge the Buffer transaction with the
555 * deferred transaction?) but this is an important legacy use case, for example moving
556 * a window at the same time it draws makes use of this kind of technique. So anyway
557 * imagine we have something like this:
558 *
559 * Transaction { // containing
560 * Buffer -> frameNumber = 2
561 * DeferTransactionUntil -> frameNumber = 2
562 * Random other stuff
563 * }
564 * Now imagine getHeadFrameNumber returned mDrawingState.mFrameNumber (or mCurrentFrameNumber).
565 * Prior to doTransaction SurfaceFlinger will call notifyAvailableFrames, but because we
566 * haven't swapped mCurrentState to mDrawingState yet we will think the sync point
567 * is not ready. So we will return false from applyPendingState and not swap
568 * current state to drawing state. But because we don't swap current state
569 * to drawing state the number will never update and we will be stuck. This way
570 * we can see we need to return the frame number for the buffer we are about
571 * to apply.
572 */
573uint64_t BufferStateLayer::getHeadFrameNumber(nsecs_t /* expectedPresentTime */) const {
574 return mCurrentState.frameNumber;
575}
576
Vishnu Naircf26a0a2020-11-13 12:56:20 -0800577void BufferStateLayer::setAutoRefresh(bool autoRefresh) {
578 if (!mAutoRefresh.exchange(autoRefresh)) {
579 mFlinger->signalLayerUpdate();
580 }
Marissa Wall61c58622018-07-18 10:12:20 -0700581}
582
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800583bool BufferStateLayer::latchSidebandStream(bool& recomputeVisibleRegions) {
Marissa Wall61c58622018-07-18 10:12:20 -0700584 if (mSidebandStreamChanged.exchange(false)) {
585 const State& s(getDrawingState());
586 // mSidebandStreamChanged was true
Lloyd Pique0b785d82018-12-04 17:25:27 -0800587 mSidebandStream = s.sidebandStream;
Lloyd Piquede196652020-01-22 17:29:58 -0800588 editCompositionState()->sidebandStream = mSidebandStream;
Lloyd Pique0b785d82018-12-04 17:25:27 -0800589 if (mSidebandStream != nullptr) {
Marissa Wall61c58622018-07-18 10:12:20 -0700590 setTransactionFlags(eTransactionNeeded);
591 mFlinger->setTransactionFlags(eTraversalNeeded);
592 }
593 recomputeVisibleRegions = true;
594
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800595 return true;
Marissa Wall61c58622018-07-18 10:12:20 -0700596 }
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800597 return false;
Marissa Wall61c58622018-07-18 10:12:20 -0700598}
599
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800600bool BufferStateLayer::hasFrameUpdate() const {
Valerie Hauaa194562019-02-05 16:21:38 -0800601 const State& c(getCurrentState());
602 return mCurrentStateModified && (c.buffer != nullptr || c.bgColorLayer != nullptr);
Marissa Wall61c58622018-07-18 10:12:20 -0700603}
604
Ady Abraham63a3e592021-01-06 10:47:15 -0800605std::optional<nsecs_t> BufferStateLayer::nextPredictedPresentTime() const {
Ady Abrahamce4adf12020-12-15 18:45:12 -0800606 if (!getDrawingState().isAutoTimestamp || !mSurfaceFrame) {
Ady Abraham63a3e592021-01-06 10:47:15 -0800607 return std::nullopt;
Ady Abrahamce4adf12020-12-15 18:45:12 -0800608 }
609
610 return mSurfaceFrame->getPredictions().presentTime;
611}
612
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700613status_t BufferStateLayer::updateTexImage(bool& /*recomputeVisibleRegions*/, nsecs_t latchTime,
614 nsecs_t /*expectedPresentTime*/) {
Marissa Wall61c58622018-07-18 10:12:20 -0700615 const State& s(getDrawingState());
616
617 if (!s.buffer) {
Valerie Hauaa194562019-02-05 16:21:38 -0800618 if (s.bgColorLayer) {
619 for (auto& handle : mDrawingState.callbackHandles) {
620 handle->latchTime = latchTime;
621 }
622 }
Marissa Wall61c58622018-07-18 10:12:20 -0700623 return NO_ERROR;
624 }
625
Marissa Wall5a68a772018-12-22 17:43:42 -0800626 for (auto& handle : mDrawingState.callbackHandles) {
Vishnu Nair935590e2021-02-10 13:05:52 -0800627 if (handle->frameNumber == mDrawingState.frameNumber) {
628 handle->latchTime = latchTime;
629 }
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
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +0000637 auto& bufferSurfaceFrame = mDrawingState.bufferSurfaceFrameTX;
638 if (bufferSurfaceFrame != nullptr &&
639 bufferSurfaceFrame->getPresentState() != PresentState::Presented) {
640 // Update only if the bufferSurfaceFrame wasn't already presented. A Presented
641 // bufferSurfaceFrame could be seen here if a pending state was applied successfully and we
642 // are processing the next state.
643 addSurfaceFramePresentedForBuffer(bufferSurfaceFrame,
644 mDrawingState.acquireFence->getSignalTime(), latchTime);
645 bufferSurfaceFrame.reset();
646 }
647
Marissa Wall16c112d2019-03-20 13:21:13 -0700648 mCurrentStateModified = false;
649
Marissa Wall61c58622018-07-18 10:12:20 -0700650 return NO_ERROR;
651}
652
653status_t BufferStateLayer::updateActiveBuffer() {
654 const State& s(getDrawingState());
655
656 if (s.buffer == nullptr) {
657 return BAD_VALUE;
658 }
chaviwdf3c5e82021-01-07 13:00:37 -0800659
660 if (s.buffer != mBufferInfo.mBuffer) {
661 decrementPendingBufferCount();
662 }
Marissa Wall61c58622018-07-18 10:12:20 -0700663
Mikael Pessa2e1608f2019-07-19 11:25:35 -0700664 mPreviousBufferId = getCurrentBufferId();
chaviwd62d3062019-09-04 14:48:02 -0700665 mBufferInfo.mBuffer = s.buffer;
666 mBufferInfo.mFence = s.acquireFence;
Marissa Wall61c58622018-07-18 10:12:20 -0700667
668 return NO_ERROR;
669}
670
Valerie Haubf784642020-01-29 07:25:23 -0800671status_t BufferStateLayer::updateFrameNumber(nsecs_t latchTime) {
Marissa Wall61c58622018-07-18 10:12:20 -0700672 // TODO(marissaw): support frame history events
Mikael Pessa2e1608f2019-07-19 11:25:35 -0700673 mPreviousFrameNumber = mCurrentFrameNumber;
Valerie Hau134651a2020-01-28 16:21:22 -0800674 mCurrentFrameNumber = mDrawingState.frameNumber;
Valerie Haubf784642020-01-29 07:25:23 -0800675 {
676 Mutex::Autolock lock(mFrameEventHistoryMutex);
677 mFrameEventHistory.addLatch(mCurrentFrameNumber, latchTime);
678 }
Marissa Wall61c58622018-07-18 10:12:20 -0700679 return NO_ERROR;
680}
681
Marissa Wall947d34e2019-03-29 14:03:53 -0700682void BufferStateLayer::HwcSlotGenerator::bufferErased(const client_cache_t& clientCacheId) {
683 std::lock_guard lock(mMutex);
684 if (!clientCacheId.isValid()) {
685 ALOGE("invalid process, failed to erase buffer");
686 return;
687 }
688 eraseBufferLocked(clientCacheId);
689}
690
691uint32_t BufferStateLayer::HwcSlotGenerator::getHwcCacheSlot(const client_cache_t& clientCacheId) {
692 std::lock_guard<std::mutex> lock(mMutex);
693 auto itr = mCachedBuffers.find(clientCacheId);
694 if (itr == mCachedBuffers.end()) {
695 return addCachedBuffer(clientCacheId);
696 }
697 auto& [hwcCacheSlot, counter] = itr->second;
698 counter = mCounter++;
699 return hwcCacheSlot;
700}
701
702uint32_t BufferStateLayer::HwcSlotGenerator::addCachedBuffer(const client_cache_t& clientCacheId)
703 REQUIRES(mMutex) {
704 if (!clientCacheId.isValid()) {
705 ALOGE("invalid process, returning invalid slot");
706 return BufferQueue::INVALID_BUFFER_SLOT;
707 }
708
709 ClientCache::getInstance().registerErasedRecipient(clientCacheId, wp<ErasedRecipient>(this));
710
711 uint32_t hwcCacheSlot = getFreeHwcCacheSlot();
712 mCachedBuffers[clientCacheId] = {hwcCacheSlot, mCounter++};
713 return hwcCacheSlot;
714}
715
716uint32_t BufferStateLayer::HwcSlotGenerator::getFreeHwcCacheSlot() REQUIRES(mMutex) {
717 if (mFreeHwcCacheSlots.empty()) {
718 evictLeastRecentlyUsed();
719 }
720
721 uint32_t hwcCacheSlot = mFreeHwcCacheSlots.top();
722 mFreeHwcCacheSlots.pop();
723 return hwcCacheSlot;
724}
725
726void BufferStateLayer::HwcSlotGenerator::evictLeastRecentlyUsed() REQUIRES(mMutex) {
727 uint64_t minCounter = UINT_MAX;
728 client_cache_t minClientCacheId = {};
729 for (const auto& [clientCacheId, slotCounter] : mCachedBuffers) {
730 const auto& [hwcCacheSlot, counter] = slotCounter;
731 if (counter < minCounter) {
732 minCounter = counter;
733 minClientCacheId = clientCacheId;
734 }
735 }
736 eraseBufferLocked(minClientCacheId);
737
738 ClientCache::getInstance().unregisterErasedRecipient(minClientCacheId, this);
739}
740
741void BufferStateLayer::HwcSlotGenerator::eraseBufferLocked(const client_cache_t& clientCacheId)
742 REQUIRES(mMutex) {
743 auto itr = mCachedBuffers.find(clientCacheId);
744 if (itr == mCachedBuffers.end()) {
745 return;
746 }
747 auto& [hwcCacheSlot, counter] = itr->second;
748
749 // TODO send to hwc cache and resources
750
751 mFreeHwcCacheSlots.push(hwcCacheSlot);
752 mCachedBuffers.erase(clientCacheId);
753}
chaviw4244e032019-09-04 11:27:49 -0700754
755void BufferStateLayer::gatherBufferInfo() {
chaviwdebadb82020-03-26 14:57:24 -0700756 BufferLayer::gatherBufferInfo();
chaviw4244e032019-09-04 11:27:49 -0700757
chaviwdebadb82020-03-26 14:57:24 -0700758 const State& s(getDrawingState());
chaviw4244e032019-09-04 11:27:49 -0700759 mBufferInfo.mDesiredPresentTime = s.desiredPresentTime;
760 mBufferInfo.mFenceTime = std::make_shared<FenceTime>(s.acquireFence);
761 mBufferInfo.mFence = s.acquireFence;
chaviw4244e032019-09-04 11:27:49 -0700762 mBufferInfo.mTransform = s.transform;
763 mBufferInfo.mDataspace = translateDataspace(s.dataspace);
764 mBufferInfo.mCrop = computeCrop(s);
765 mBufferInfo.mScaleMode = NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW;
766 mBufferInfo.mSurfaceDamage = s.surfaceDamageRegion;
767 mBufferInfo.mHdrMetadata = s.hdrMetadata;
768 mBufferInfo.mApi = s.api;
chaviw4244e032019-09-04 11:27:49 -0700769 mBufferInfo.mTransformToDisplayInverse = s.transformToDisplayInverse;
chaviwf83ce182019-09-12 14:43:08 -0700770 mBufferInfo.mBufferSlot = mHwcSlotGenerator->getHwcCacheSlot(s.clientCacheId);
chaviw4244e032019-09-04 11:27:49 -0700771}
772
Robert Carr916b0362020-10-06 13:53:03 -0700773uint32_t BufferStateLayer::getEffectiveScalingMode() const {
774 return NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW;
775}
776
chaviw4244e032019-09-04 11:27:49 -0700777Rect BufferStateLayer::computeCrop(const State& s) {
778 if (s.crop.isEmpty() && s.buffer) {
779 return s.buffer->getBounds();
780 } else if (s.buffer) {
781 Rect crop = s.crop;
782 crop.left = std::max(crop.left, 0);
783 crop.top = std::max(crop.top, 0);
784 uint32_t bufferWidth = s.buffer->getWidth();
785 uint32_t bufferHeight = s.buffer->getHeight();
786 if (bufferHeight <= std::numeric_limits<int32_t>::max() &&
787 bufferWidth <= std::numeric_limits<int32_t>::max()) {
788 crop.right = std::min(crop.right, static_cast<int32_t>(bufferWidth));
789 crop.bottom = std::min(crop.bottom, static_cast<int32_t>(bufferHeight));
790 }
791 if (!crop.isValid()) {
792 // Crop rect is out of bounds, return whole buffer
793 return s.buffer->getBounds();
794 }
795 return crop;
796 }
797 return s.crop;
798}
799
chaviwb4c6e582019-08-16 14:35:07 -0700800sp<Layer> BufferStateLayer::createClone() {
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700801 LayerCreationArgs args(mFlinger.get(), nullptr, mName + " (Mirror)", 0, 0, 0, LayerMetadata());
chaviwb4c6e582019-08-16 14:35:07 -0700802 args.textureName = mTextureName;
Lloyd Pique1c3a5eb2019-10-03 13:07:08 -0700803 sp<BufferStateLayer> layer = mFlinger->getFactory().createBufferStateLayer(args);
chaviwb4c6e582019-08-16 14:35:07 -0700804 layer->mHwcSlotGenerator = mHwcSlotGenerator;
805 layer->setInitialValuesForClone(this);
806 return layer;
807}
Valerie Hau92bf5482020-02-10 09:49:08 -0800808
809Layer::RoundedCornerState BufferStateLayer::getRoundedCornerState() const {
810 const auto& p = mDrawingParent.promote();
811 if (p != nullptr) {
812 RoundedCornerState parentState = p->getRoundedCornerState();
813 if (parentState.radius > 0) {
814 ui::Transform t = getActiveTransform(getDrawingState());
815 t = t.inverse();
816 parentState.cropRect = t.transform(parentState.cropRect);
817 // The rounded corners shader only accepts 1 corner radius for performance reasons,
818 // but a transform matrix can define horizontal and vertical scales.
819 // Let's take the average between both of them and pass into the shader, practically we
820 // never do this type of transformation on windows anyway.
821 parentState.radius *= (t[0][0] + t[1][1]) / 2.0f;
822 return parentState;
823 }
824 }
825 const float radius = getDrawingState().cornerRadius;
826 const State& s(getDrawingState());
827 if (radius <= 0 || (getActiveWidth(s) == UINT32_MAX && getActiveHeight(s) == UINT32_MAX))
828 return RoundedCornerState();
829 return RoundedCornerState(FloatRect(static_cast<float>(s.active.transform.tx()),
830 static_cast<float>(s.active.transform.ty()),
831 static_cast<float>(s.active.transform.tx() + s.active.w),
832 static_cast<float>(s.active.transform.ty() + s.active.h)),
833 radius);
834}
Vishnu Naire7f79c52020-10-29 14:45:03 -0700835
836bool BufferStateLayer::bufferNeedsFiltering() const {
837 const State& s(getDrawingState());
838 if (!s.buffer) {
839 return false;
840 }
841
842 uint32_t bufferWidth = s.buffer->width;
843 uint32_t bufferHeight = s.buffer->height;
844
845 // Undo any transformations on the buffer and return the result.
846 if (s.transform & ui::Transform::ROT_90) {
847 std::swap(bufferWidth, bufferHeight);
848 }
849
850 if (s.transformToDisplayInverse) {
851 uint32_t invTransform = DisplayDevice::getPrimaryDisplayRotationFlags();
852 if (invTransform & ui::Transform::ROT_90) {
853 std::swap(bufferWidth, bufferHeight);
854 }
855 }
856
857 const Rect layerSize{getBounds()};
858 return layerSize.width() != bufferWidth || layerSize.height() != bufferHeight;
859}
Robert Carr7121caf2020-12-15 13:07:32 -0800860
861void BufferStateLayer::incrementPendingBufferCount() {
862 mPendingBufferTransactions++;
863 tracePendingBufferCount();
864}
865
866void BufferStateLayer::decrementPendingBufferCount() {
867 mPendingBufferTransactions--;
868 tracePendingBufferCount();
869}
870
871void BufferStateLayer::tracePendingBufferCount() {
872 ATRACE_INT(mBlastTransactionName.c_str(), mPendingBufferTransactions);
873}
874
875uint32_t BufferStateLayer::doTransaction(uint32_t flags) {
876 if (mDrawingState.buffer != nullptr && mDrawingState.buffer != mBufferInfo.mBuffer) {
877 // If we are about to update mDrawingState.buffer but it has not yet latched
878 // then we will drop a buffer and should decrement the pending buffer count.
879 // This logic may not work perfectly in the face of a BufferStateLayer being the
880 // deferred side of a deferred transaction, but we don't expect this use case.
881 decrementPendingBufferCount();
882 }
883 return Layer::doTransaction(flags);
884}
885
Marissa Wall61c58622018-07-18 10:12:20 -0700886} // namespace android
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800887
888// TODO(b/129481165): remove the #pragma below and fix conversion issues
Marin Shalamanovbed7fd32020-12-21 20:02:20 +0100889#pragma clang diagnostic pop // ignored "-Wconversion -Wextra"