blob: 98cc5290caaaf5c04009486b4ac90467933bf047 [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 Pique0b785d82018-12-04 17:25:27 -080030#include <compositionengine/Layer.h>
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
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080036#include "ColorLayer.h"
Mikael Pessa90092f42019-08-26 17:22:04 -070037#include "FrameTracer/FrameTracer.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
Lloyd Pique42ab75e2018-09-12 20:46:03 -070042// clang-format off
43const std::array<float, 16> BufferStateLayer::IDENTITY_MATRIX{
44 1, 0, 0, 0,
45 0, 1, 0, 0,
46 0, 0, 1, 0,
47 0, 0, 0, 1
48};
49// clang-format on
Marissa Wall61c58622018-07-18 10:12:20 -070050
Marissa Wall947d34e2019-03-29 14:03:53 -070051BufferStateLayer::BufferStateLayer(const LayerCreationArgs& args)
52 : BufferLayer(args), mHwcSlotGenerator(new HwcSlotGenerator()) {
Vishnu Nair60356342018-11-13 13:00:45 -080053 mOverrideScalingMode = NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW;
Marissa Wall3ff826c2019-02-07 11:58:25 -080054 mCurrentState.dataspace = ui::Dataspace::V0_SRGB;
chaviwb4c6e582019-08-16 14:35:07 -070055 if (const auto display = args.displayDevice) {
56 updateTransformHint(display);
57 }
Vishnu Nair60356342018-11-13 13:00:45 -080058}
Marissa Wall61c58622018-07-18 10:12:20 -070059
Alec Mouri4545a8a2019-08-08 20:05:32 -070060BufferStateLayer::~BufferStateLayer() {
chaviwb4c6e582019-08-16 14:35:07 -070061 // The original layer and the clone layer share the same texture and buffer. Therefore, only
62 // one of the layers, in this case the original layer, needs to handle the deletion. The
63 // original layer and the clone should be removed at the same time so there shouldn't be any
64 // issue with the clone layer trying to use the texture.
65 if (mBufferInfo.mBuffer != nullptr && !isClone()) {
chaviwd62d3062019-09-04 14:48:02 -070066 // Ensure that mBuffer is uncached from RenderEngine here, as
Alec Mouri4545a8a2019-08-08 20:05:32 -070067 // RenderEngine may have been using the buffer as an external texture
68 // after the client uncached the buffer.
69 auto& engine(mFlinger->getRenderEngine());
chaviwd62d3062019-09-04 14:48:02 -070070 engine.unbindExternalTextureBuffer(mBufferInfo.mBuffer->getId());
Alec Mouri4545a8a2019-08-08 20:05:32 -070071 }
72}
73
Marissa Wall61c58622018-07-18 10:12:20 -070074// -----------------------------------------------------------------------
75// Interface implementation for Layer
76// -----------------------------------------------------------------------
Marissa Wallfda30bb2018-10-12 11:34:28 -070077void BufferStateLayer::onLayerDisplayed(const sp<Fence>& releaseFence) {
Marissa Wall5a68a772018-12-22 17:43:42 -080078 // The previous release fence notifies the client that SurfaceFlinger is done with the previous
79 // buffer that was presented on this layer. The first transaction that came in this frame that
80 // replaced the previous buffer on this layer needs this release fence, because the fence will
81 // let the client know when that previous buffer is removed from the screen.
82 //
83 // Every other transaction on this layer does not need a release fence because no other
84 // Transactions that were set on this layer this frame are going to have their preceeding buffer
85 // removed from the display this frame.
86 //
87 // For example, if we have 3 transactions this frame. The first transaction doesn't contain a
88 // buffer so it doesn't need a previous release fence because the layer still needs the previous
89 // buffer. The second transaction contains a buffer so it needs a previous release fence because
90 // the previous buffer will be released this frame. The third transaction also contains a
91 // buffer. It replaces the buffer in the second transaction. The buffer in the second
92 // transaction will now no longer be presented so it is released immediately and the third
93 // transaction doesn't need a previous release fence.
94 for (auto& handle : mDrawingState.callbackHandles) {
95 if (handle->releasePreviousBuffer) {
96 handle->previousReleaseFence = releaseFence;
97 break;
98 }
99 }
Mikael Pessa2e1608f2019-07-19 11:25:35 -0700100
101 // Prevent tracing the same release multiple times.
102 if (mPreviousFrameNumber != mPreviousReleasedFrameNumber) {
Mikael Pessa90092f42019-08-26 17:22:04 -0700103 mFlinger->mFrameTracer->traceFence(getSequence(), mPreviousBufferId, mPreviousFrameNumber,
104 std::make_shared<FenceTime>(releaseFence),
105 FrameTracer::FrameEvent::RELEASE_FENCE);
Mikael Pessa2e1608f2019-07-19 11:25:35 -0700106 mPreviousReleasedFrameNumber = mPreviousFrameNumber;
107 }
Marissa Wall61c58622018-07-18 10:12:20 -0700108}
109
Valerie Hau32cdc1f2019-10-21 14:45:54 -0700110void BufferStateLayer::setTransformHint(uint32_t orientation) const {
111 mTransformHint = orientation;
Marissa Wall61c58622018-07-18 10:12:20 -0700112}
113
114void BufferStateLayer::releasePendingBuffer(nsecs_t /*dequeueReadyTime*/) {
Valerie Hau32cdc1f2019-10-21 14:45:54 -0700115 for (const auto& handle : mDrawingState.callbackHandles) {
116 handle->transformHint = mTransformHint;
117 }
118
Marissa Wallefb71af2019-06-27 14:45:53 -0700119 mFlinger->getTransactionCompletedThread().finalizePendingCallbackHandles(
Marissa Wall5a68a772018-12-22 17:43:42 -0800120 mDrawingState.callbackHandles);
121
122 mDrawingState.callbackHandles = {};
Marissa Wall61c58622018-07-18 10:12:20 -0700123}
124
Ana Krulec010d2192018-10-08 06:29:54 -0700125bool BufferStateLayer::shouldPresentNow(nsecs_t /*expectedPresentTime*/) const {
Marissa Wall61c58622018-07-18 10:12:20 -0700126 if (getSidebandStreamChanged() || getAutoRefresh()) {
127 return true;
128 }
129
Marissa Wall024a1912018-08-13 13:55:35 -0700130 return hasFrameUpdate();
Marissa Wall61c58622018-07-18 10:12:20 -0700131}
132
Marissa Walle2ffb422018-10-12 11:33:52 -0700133bool BufferStateLayer::willPresentCurrentTransaction() const {
134 // Returns true if the most recent Transaction applied to CurrentState will be presented.
Robert Carr321e83c2019-08-19 15:49:30 -0700135 return (getSidebandStreamChanged() || getAutoRefresh() ||
Valerie Hauaa194562019-02-05 16:21:38 -0800136 (mCurrentState.modified &&
Robert Carr321e83c2019-08-19 15:49:30 -0700137 (mCurrentState.buffer != nullptr || mCurrentState.bgColorLayer != nullptr))) &&
138 !mLayerDetached;
Marissa Wall61c58622018-07-18 10:12:20 -0700139}
140
Valerie Hau3282b3c2020-02-03 15:37:27 -0800141/* TODO: vhau uncomment once deferred transaction migration complete in
142 * WindowManager
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800143void BufferStateLayer::pushPendingState() {
144 if (!mCurrentState.modified) {
Marissa Wall61c58622018-07-18 10:12:20 -0700145 return;
146 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800147 mPendingStates.push_back(mCurrentState);
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700148 ATRACE_INT(mTransactionName.c_str(), mPendingStates.size());
Marissa Wall61c58622018-07-18 10:12:20 -0700149}
Valerie Hau3282b3c2020-02-03 15:37:27 -0800150*/
Marissa Wall61c58622018-07-18 10:12:20 -0700151
152bool BufferStateLayer::applyPendingStates(Layer::State* stateToCommit) {
Valerie Hau3282b3c2020-02-03 15:37:27 -0800153 mCurrentStateModified = mCurrentState.modified;
154 bool stateUpdateAvailable = Layer::applyPendingStates(stateToCommit);
155 mCurrentStateModified = stateUpdateAvailable && mCurrentStateModified;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800156 mCurrentState.modified = false;
Marissa Wall61c58622018-07-18 10:12:20 -0700157 return stateUpdateAvailable;
158}
159
Marissa Wall861616d2018-10-22 12:52:23 -0700160// Crop that applies to the window
161Rect BufferStateLayer::getCrop(const Layer::State& /*s*/) const {
162 return Rect::INVALID_RECT;
Marissa Wall61c58622018-07-18 10:12:20 -0700163}
164
165bool BufferStateLayer::setTransform(uint32_t transform) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800166 if (mCurrentState.transform == transform) return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800167 mCurrentState.transform = transform;
168 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700169 setTransactionFlags(eTransactionNeeded);
170 return true;
171}
172
173bool BufferStateLayer::setTransformToDisplayInverse(bool transformToDisplayInverse) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800174 if (mCurrentState.transformToDisplayInverse == transformToDisplayInverse) return false;
175 mCurrentState.sequence++;
176 mCurrentState.transformToDisplayInverse = transformToDisplayInverse;
177 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700178 setTransactionFlags(eTransactionNeeded);
179 return true;
180}
181
182bool BufferStateLayer::setCrop(const Rect& crop) {
Marissa Wall290ad082019-03-06 13:23:47 -0800183 Rect c = crop;
184 if (c.left < 0) {
185 c.left = 0;
186 }
187 if (c.top < 0) {
188 c.top = 0;
189 }
190 // If the width and/or height are < 0, make it [0, 0, -1, -1] so the equality comparision below
191 // treats all invalid rectangles the same.
192 if (!c.isValid()) {
193 c.makeInvalid();
194 }
195
196 if (mCurrentState.crop == c) return false;
Marissa Wall290ad082019-03-06 13:23:47 -0800197 mCurrentState.crop = c;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800198 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700199 setTransactionFlags(eTransactionNeeded);
200 return true;
201}
202
Marissa Wall861616d2018-10-22 12:52:23 -0700203bool BufferStateLayer::setFrame(const Rect& frame) {
204 int x = frame.left;
205 int y = frame.top;
206 int w = frame.getWidth();
207 int h = frame.getHeight();
208
Marissa Wall0f3242d2018-12-20 15:10:22 -0800209 if (x < 0) {
210 x = 0;
211 w = frame.right;
212 }
213
214 if (y < 0) {
215 y = 0;
216 h = frame.bottom;
217 }
218
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800219 if (mCurrentState.active.transform.tx() == x && mCurrentState.active.transform.ty() == y &&
220 mCurrentState.active.w == w && mCurrentState.active.h == h) {
Marissa Wall861616d2018-10-22 12:52:23 -0700221 return false;
222 }
223
224 if (!frame.isValid()) {
225 x = y = w = h = 0;
226 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800227 mCurrentState.active.transform.set(x, y);
228 mCurrentState.active.w = w;
229 mCurrentState.active.h = h;
Marissa Wall861616d2018-10-22 12:52:23 -0700230
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800231 mCurrentState.sequence++;
232 mCurrentState.modified = true;
Marissa Wall861616d2018-10-22 12:52:23 -0700233 setTransactionFlags(eTransactionNeeded);
234 return true;
235}
236
Ady Abraham09bd3922019-04-08 10:44:56 -0700237bool BufferStateLayer::setBuffer(const sp<GraphicBuffer>& buffer, nsecs_t postTime,
Marissa Wall947d34e2019-03-29 14:03:53 -0700238 nsecs_t desiredPresentTime, const client_cache_t& clientCacheId) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800239 if (mCurrentState.buffer) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700240 mReleasePreviousBuffer = true;
241 }
242
Valerie Hau134651a2020-01-28 16:21:22 -0800243 mCurrentState.frameNumber++;
Valerie Hau2f54d642020-01-22 09:37:03 -0800244
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800245 mCurrentState.buffer = buffer;
Marissa Wall947d34e2019-03-29 14:03:53 -0700246 mCurrentState.clientCacheId = clientCacheId;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800247 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700248 setTransactionFlags(eTransactionNeeded);
Ady Abraham09bd3922019-04-08 10:44:56 -0700249
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800250 const int32_t layerId = getSequence();
Valerie Hau134651a2020-01-28 16:21:22 -0800251 mFlinger->mTimeStats->setPostTime(layerId, mCurrentState.frameNumber, getName().c_str(),
252 postTime);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800253 mFlinger->mFrameTracer->traceNewLayer(layerId, getName().c_str());
Valerie Hau134651a2020-01-28 16:21:22 -0800254 mFlinger->mFrameTracer->traceTimestamp(layerId, buffer->getId(), mCurrentState.frameNumber,
255 postTime, FrameTracer::FrameEvent::POST);
chaviwfa67b552019-08-12 16:51:55 -0700256 mCurrentState.desiredPresentTime = desiredPresentTime;
Ady Abraham09bd3922019-04-08 10:44:56 -0700257
Ady Abraham8a82ba62020-01-17 12:43:17 -0800258 mFlinger->mScheduler->recordLayerHistory(this,
259 desiredPresentTime <= 0 ? 0 : desiredPresentTime);
Ady Abraham09bd3922019-04-08 10:44:56 -0700260
Marissa Wall61c58622018-07-18 10:12:20 -0700261 return true;
262}
263
264bool BufferStateLayer::setAcquireFence(const sp<Fence>& fence) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700265 // The acquire fences of BufferStateLayers have already signaled before they are set
266 mCallbackHandleAcquireTime = fence->getSignalTime();
267
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800268 mCurrentState.acquireFence = fence;
269 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700270 setTransactionFlags(eTransactionNeeded);
271 return true;
272}
273
274bool BufferStateLayer::setDataspace(ui::Dataspace dataspace) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800275 if (mCurrentState.dataspace == dataspace) return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800276 mCurrentState.dataspace = dataspace;
277 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700278 setTransactionFlags(eTransactionNeeded);
279 return true;
280}
281
282bool BufferStateLayer::setHdrMetadata(const HdrMetadata& hdrMetadata) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800283 if (mCurrentState.hdrMetadata == hdrMetadata) return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800284 mCurrentState.hdrMetadata = hdrMetadata;
285 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700286 setTransactionFlags(eTransactionNeeded);
287 return true;
288}
289
290bool BufferStateLayer::setSurfaceDamageRegion(const Region& surfaceDamage) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800291 mCurrentState.surfaceDamageRegion = surfaceDamage;
292 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700293 setTransactionFlags(eTransactionNeeded);
294 return true;
295}
296
297bool BufferStateLayer::setApi(int32_t api) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800298 if (mCurrentState.api == api) return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800299 mCurrentState.api = api;
300 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700301 setTransactionFlags(eTransactionNeeded);
302 return true;
303}
304
305bool BufferStateLayer::setSidebandStream(const sp<NativeHandle>& sidebandStream) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800306 if (mCurrentState.sidebandStream == sidebandStream) return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800307 mCurrentState.sidebandStream = sidebandStream;
308 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700309 setTransactionFlags(eTransactionNeeded);
310
311 if (!mSidebandStreamChanged.exchange(true)) {
312 // mSidebandStreamChanged was false
313 mFlinger->signalLayerUpdate();
314 }
315 return true;
316}
317
Marissa Walle2ffb422018-10-12 11:33:52 -0700318bool BufferStateLayer::setTransactionCompletedListeners(
319 const std::vector<sp<CallbackHandle>>& handles) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700320 // If there is no handle, we will not send a callback so reset mReleasePreviousBuffer and return
Marissa Walle2ffb422018-10-12 11:33:52 -0700321 if (handles.empty()) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700322 mReleasePreviousBuffer = false;
Marissa Walle2ffb422018-10-12 11:33:52 -0700323 return false;
324 }
325
326 const bool willPresent = willPresentCurrentTransaction();
327
328 for (const auto& handle : handles) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700329 // If this transaction set a buffer on this layer, release its previous buffer
330 handle->releasePreviousBuffer = mReleasePreviousBuffer;
331
Marissa Walle2ffb422018-10-12 11:33:52 -0700332 // If this layer will be presented in this frame
333 if (willPresent) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700334 // If this transaction set an acquire fence on this layer, set its acquire time
335 handle->acquireTime = mCallbackHandleAcquireTime;
336
Marissa Walle2ffb422018-10-12 11:33:52 -0700337 // Notify the transaction completed thread that there is a pending latched callback
338 // handle
Marissa Wall5a68a772018-12-22 17:43:42 -0800339 mFlinger->getTransactionCompletedThread().registerPendingCallbackHandle(handle);
Marissa Walle2ffb422018-10-12 11:33:52 -0700340
341 // Store so latched time and release fence can be set
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800342 mCurrentState.callbackHandles.push_back(handle);
Marissa Walle2ffb422018-10-12 11:33:52 -0700343
344 } else { // If this layer will NOT need to be relatched and presented this frame
345 // Notify the transaction completed thread this handle is done
Marissa Wallefb71af2019-06-27 14:45:53 -0700346 mFlinger->getTransactionCompletedThread().registerUnpresentedCallbackHandle(handle);
Marissa Walle2ffb422018-10-12 11:33:52 -0700347 }
348 }
349
Marissa Wallfda30bb2018-10-12 11:34:28 -0700350 mReleasePreviousBuffer = false;
351 mCallbackHandleAcquireTime = -1;
352
Marissa Walle2ffb422018-10-12 11:33:52 -0700353 return willPresent;
354}
355
Valerie Hau7618b232020-01-09 16:03:08 -0800356void BufferStateLayer::forceSendCallbacks() {
357 mFlinger->getTransactionCompletedThread().finalizePendingCallbackHandles(
358 mCurrentState.callbackHandles);
359}
360
Marissa Wall61c58622018-07-18 10:12:20 -0700361bool BufferStateLayer::setTransparentRegionHint(const Region& transparent) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800362 mCurrentState.transparentRegionHint = transparent;
363 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700364 setTransactionFlags(eTransactionNeeded);
365 return true;
366}
367
Marissa Wall861616d2018-10-22 12:52:23 -0700368Rect BufferStateLayer::getBufferSize(const State& s) const {
369 // for buffer state layers we use the display frame size as the buffer size.
370 if (getActiveWidth(s) < UINT32_MAX && getActiveHeight(s) < UINT32_MAX) {
371 return Rect(getActiveWidth(s), getActiveHeight(s));
Marissa Wall61c58622018-07-18 10:12:20 -0700372 }
373
Marissa Wall861616d2018-10-22 12:52:23 -0700374 // if the display frame is not defined, use the parent bounds as the buffer size.
375 const auto& p = mDrawingParent.promote();
376 if (p != nullptr) {
Vishnu Nair4351ad52019-02-11 14:13:02 -0800377 Rect parentBounds = Rect(p->getBounds(Region()));
Marissa Wall861616d2018-10-22 12:52:23 -0700378 if (!parentBounds.isEmpty()) {
379 return parentBounds;
380 }
381 }
382
Marissa Wall861616d2018-10-22 12:52:23 -0700383 return Rect::INVALID_RECT;
Marissa Wall61c58622018-07-18 10:12:20 -0700384}
Vishnu Nair4351ad52019-02-11 14:13:02 -0800385
386FloatRect BufferStateLayer::computeSourceBounds(const FloatRect& parentBounds) const {
387 const State& s(getDrawingState());
388 // for buffer state layers we use the display frame size as the buffer size.
389 if (getActiveWidth(s) < UINT32_MAX && getActiveHeight(s) < UINT32_MAX) {
390 return FloatRect(0, 0, getActiveWidth(s), getActiveHeight(s));
391 }
392
393 // if the display frame is not defined, use the parent bounds as the buffer size.
394 return parentBounds;
395}
396
Marissa Wall61c58622018-07-18 10:12:20 -0700397// -----------------------------------------------------------------------
398
399// -----------------------------------------------------------------------
400// Interface implementation for BufferLayer
401// -----------------------------------------------------------------------
402bool BufferStateLayer::fenceHasSignaled() const {
403 if (latchUnsignaledBuffers()) {
404 return true;
405 }
406
Alec Mouri91f6df32020-01-30 08:48:58 -0800407 const bool fenceSignaled =
408 getDrawingState().acquireFence->getStatus() == Fence::Status::Signaled;
409 if (!fenceSignaled) {
410 mFlinger->mTimeStats->incrementLatchSkipped(getSequence(),
411 TimeStats::LatchSkipReason::LateAcquire);
412 }
413
414 return fenceSignaled;
Marissa Wall61c58622018-07-18 10:12:20 -0700415}
416
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700417bool BufferStateLayer::framePresentTimeIsCurrent(nsecs_t expectedPresentTime) const {
Ady Abrahamcd1580c2019-04-29 15:40:03 -0700418 if (!hasFrameUpdate() || isRemovedFromCurrentState()) {
419 return true;
420 }
421
chaviwfa67b552019-08-12 16:51:55 -0700422 return mCurrentState.desiredPresentTime <= expectedPresentTime;
Ady Abrahamcd1580c2019-04-29 15:40:03 -0700423}
424
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700425uint64_t BufferStateLayer::getFrameNumber(nsecs_t /*expectedPresentTime*/) const {
Valerie Hau134651a2020-01-28 16:21:22 -0800426 return mDrawingState.frameNumber;
Marissa Wall61c58622018-07-18 10:12:20 -0700427}
428
429bool BufferStateLayer::getAutoRefresh() const {
430 // TODO(marissaw): support shared buffer mode
431 return false;
432}
433
434bool BufferStateLayer::getSidebandStreamChanged() const {
435 return mSidebandStreamChanged.load();
436}
437
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800438bool BufferStateLayer::latchSidebandStream(bool& recomputeVisibleRegions) {
Marissa Wall61c58622018-07-18 10:12:20 -0700439 if (mSidebandStreamChanged.exchange(false)) {
440 const State& s(getDrawingState());
441 // mSidebandStreamChanged was true
Lloyd Pique0b785d82018-12-04 17:25:27 -0800442 LOG_ALWAYS_FATAL_IF(!getCompositionLayer());
443 mSidebandStream = s.sidebandStream;
Lloyd Pique9755fb72019-03-26 14:44:40 -0700444 getCompositionLayer()->editFEState().sidebandStream = mSidebandStream;
Lloyd Pique0b785d82018-12-04 17:25:27 -0800445 if (mSidebandStream != nullptr) {
Marissa Wall61c58622018-07-18 10:12:20 -0700446 setTransactionFlags(eTransactionNeeded);
447 mFlinger->setTransactionFlags(eTraversalNeeded);
448 }
449 recomputeVisibleRegions = true;
450
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800451 return true;
Marissa Wall61c58622018-07-18 10:12:20 -0700452 }
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800453 return false;
Marissa Wall61c58622018-07-18 10:12:20 -0700454}
455
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800456bool BufferStateLayer::hasFrameUpdate() const {
Valerie Hauaa194562019-02-05 16:21:38 -0800457 const State& c(getCurrentState());
458 return mCurrentStateModified && (c.buffer != nullptr || c.bgColorLayer != nullptr);
Marissa Wall61c58622018-07-18 10:12:20 -0700459}
460
Alec Mouri39801c02018-10-10 10:44:47 -0700461status_t BufferStateLayer::bindTextureImage() {
Marissa Wall61c58622018-07-18 10:12:20 -0700462 const State& s(getDrawingState());
463 auto& engine(mFlinger->getRenderEngine());
464
Alec Mourib5c4f352019-02-19 19:46:38 -0800465 return engine.bindExternalTextureBuffer(mTextureName, s.buffer, s.acquireFence);
Marissa Wall61c58622018-07-18 10:12:20 -0700466}
467
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700468status_t BufferStateLayer::updateTexImage(bool& /*recomputeVisibleRegions*/, nsecs_t latchTime,
469 nsecs_t /*expectedPresentTime*/) {
Marissa Wall61c58622018-07-18 10:12:20 -0700470 const State& s(getDrawingState());
471
472 if (!s.buffer) {
Valerie Hauaa194562019-02-05 16:21:38 -0800473 if (s.bgColorLayer) {
474 for (auto& handle : mDrawingState.callbackHandles) {
475 handle->latchTime = latchTime;
476 }
477 }
Marissa Wall61c58622018-07-18 10:12:20 -0700478 return NO_ERROR;
479 }
480
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800481 const int32_t layerId = getSequence();
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700482
Marissa Wall61c58622018-07-18 10:12:20 -0700483 // Reject if the layer is invalid
484 uint32_t bufferWidth = s.buffer->width;
485 uint32_t bufferHeight = s.buffer->height;
486
Peiyong Linefefaac2018-08-17 12:27:51 -0700487 if (s.transform & ui::Transform::ROT_90) {
Peiyong Lin3db42342018-08-16 09:15:59 -0700488 std::swap(bufferWidth, bufferHeight);
Marissa Wall61c58622018-07-18 10:12:20 -0700489 }
490
491 if (s.transformToDisplayInverse) {
Dominik Laskowski718f9602019-11-09 20:01:35 -0800492 uint32_t invTransform = DisplayDevice::getPrimaryDisplayRotationFlags();
Peiyong Linefefaac2018-08-17 12:27:51 -0700493 if (invTransform & ui::Transform::ROT_90) {
Peiyong Lin3db42342018-08-16 09:15:59 -0700494 std::swap(bufferWidth, bufferHeight);
Marissa Wall61c58622018-07-18 10:12:20 -0700495 }
496 }
497
Vishnu Nair60356342018-11-13 13:00:45 -0800498 if (getEffectiveScalingMode() == NATIVE_WINDOW_SCALING_MODE_FREEZE &&
Marissa Wall61c58622018-07-18 10:12:20 -0700499 (s.active.w != bufferWidth || s.active.h != bufferHeight)) {
500 ALOGE("[%s] rejecting buffer: "
501 "bufferWidth=%d, bufferHeight=%d, front.active.{w=%d, h=%d}",
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700502 getDebugName(), bufferWidth, bufferHeight, s.active.w, s.active.h);
Valerie Hau134651a2020-01-28 16:21:22 -0800503 mFlinger->mTimeStats->removeTimeRecord(layerId, mDrawingState.frameNumber);
Marissa Wall61c58622018-07-18 10:12:20 -0700504 return BAD_VALUE;
505 }
506
Marissa Wall5a68a772018-12-22 17:43:42 -0800507 for (auto& handle : mDrawingState.callbackHandles) {
508 handle->latchTime = latchTime;
509 }
Marissa Walle2ffb422018-10-12 11:33:52 -0700510
Alec Mouri56e538f2019-01-14 15:22:01 -0800511 if (!SyncFeatures::getInstance().useNativeFenceSync()) {
Marissa Wall61c58622018-07-18 10:12:20 -0700512 // Bind the new buffer to the GL texture.
513 //
514 // Older devices require the "implicit" synchronization provided
515 // by glEGLImageTargetTexture2DOES, which this method calls. Newer
516 // devices will either call this in Layer::onDraw, or (if it's not
517 // a GL-composited layer) not at all.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800518 status_t err = bindTextureImage();
Marissa Wall61c58622018-07-18 10:12:20 -0700519 if (err != NO_ERROR) {
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800520 mFlinger->mTimeStats->onDestroy(layerId);
521 mFlinger->mFrameTracer->onDestroy(layerId);
Marissa Wall61c58622018-07-18 10:12:20 -0700522 return BAD_VALUE;
523 }
524 }
525
Mikael Pessa2e1608f2019-07-19 11:25:35 -0700526 const uint64_t bufferID = getCurrentBufferId();
Valerie Hau134651a2020-01-28 16:21:22 -0800527 mFlinger->mTimeStats->setAcquireFence(layerId, mDrawingState.frameNumber,
528 mBufferInfo.mFenceTime);
529 mFlinger->mFrameTracer->traceFence(layerId, bufferID, mDrawingState.frameNumber,
530 mBufferInfo.mFenceTime,
Mikael Pessa90092f42019-08-26 17:22:04 -0700531 FrameTracer::FrameEvent::ACQUIRE_FENCE);
Valerie Hau134651a2020-01-28 16:21:22 -0800532 mFlinger->mTimeStats->setLatchTime(layerId, mDrawingState.frameNumber, latchTime);
533 mFlinger->mFrameTracer->traceTimestamp(layerId, bufferID, mDrawingState.frameNumber, latchTime,
Mikael Pessa90092f42019-08-26 17:22:04 -0700534 FrameTracer::FrameEvent::LATCH);
Marissa Wall61c58622018-07-18 10:12:20 -0700535
Marissa Wall16c112d2019-03-20 13:21:13 -0700536 mCurrentStateModified = false;
537
Marissa Wall61c58622018-07-18 10:12:20 -0700538 return NO_ERROR;
539}
540
541status_t BufferStateLayer::updateActiveBuffer() {
542 const State& s(getDrawingState());
543
544 if (s.buffer == nullptr) {
545 return BAD_VALUE;
546 }
547
Mikael Pessa2e1608f2019-07-19 11:25:35 -0700548 mPreviousBufferId = getCurrentBufferId();
chaviwd62d3062019-09-04 14:48:02 -0700549 mBufferInfo.mBuffer = s.buffer;
550 mBufferInfo.mFence = s.acquireFence;
Lloyd Pique9755fb72019-03-26 14:44:40 -0700551 auto& layerCompositionState = getCompositionLayer()->editFEState();
chaviwd62d3062019-09-04 14:48:02 -0700552 layerCompositionState.buffer = mBufferInfo.mBuffer;
Marissa Wall61c58622018-07-18 10:12:20 -0700553
554 return NO_ERROR;
555}
556
557status_t BufferStateLayer::updateFrameNumber(nsecs_t /*latchTime*/) {
558 // TODO(marissaw): support frame history events
Mikael Pessa2e1608f2019-07-19 11:25:35 -0700559 mPreviousFrameNumber = mCurrentFrameNumber;
Valerie Hau134651a2020-01-28 16:21:22 -0800560 mCurrentFrameNumber = mDrawingState.frameNumber;
Marissa Wall61c58622018-07-18 10:12:20 -0700561 return NO_ERROR;
562}
563
Lloyd Piquef5275482019-01-29 18:42:42 -0800564void BufferStateLayer::latchPerFrameState(
565 compositionengine::LayerFECompositionState& compositionState) const {
566 BufferLayer::latchPerFrameState(compositionState);
567 if (compositionState.compositionType == Hwc2::IComposerClient::Composition::SIDEBAND) {
568 return;
569 }
Marissa Wall61c58622018-07-18 10:12:20 -0700570
chaviwf83ce182019-09-12 14:43:08 -0700571 compositionState.buffer = mBufferInfo.mBuffer;
572 compositionState.bufferSlot = mBufferInfo.mBufferSlot;
chaviw4244e032019-09-04 11:27:49 -0700573 compositionState.acquireFence = mBufferInfo.mFence;
Marissa Wall61c58622018-07-18 10:12:20 -0700574}
575
Marissa Wall947d34e2019-03-29 14:03:53 -0700576void BufferStateLayer::HwcSlotGenerator::bufferErased(const client_cache_t& clientCacheId) {
577 std::lock_guard lock(mMutex);
578 if (!clientCacheId.isValid()) {
579 ALOGE("invalid process, failed to erase buffer");
580 return;
581 }
582 eraseBufferLocked(clientCacheId);
583}
584
585uint32_t BufferStateLayer::HwcSlotGenerator::getHwcCacheSlot(const client_cache_t& clientCacheId) {
586 std::lock_guard<std::mutex> lock(mMutex);
587 auto itr = mCachedBuffers.find(clientCacheId);
588 if (itr == mCachedBuffers.end()) {
589 return addCachedBuffer(clientCacheId);
590 }
591 auto& [hwcCacheSlot, counter] = itr->second;
592 counter = mCounter++;
593 return hwcCacheSlot;
594}
595
596uint32_t BufferStateLayer::HwcSlotGenerator::addCachedBuffer(const client_cache_t& clientCacheId)
597 REQUIRES(mMutex) {
598 if (!clientCacheId.isValid()) {
599 ALOGE("invalid process, returning invalid slot");
600 return BufferQueue::INVALID_BUFFER_SLOT;
601 }
602
603 ClientCache::getInstance().registerErasedRecipient(clientCacheId, wp<ErasedRecipient>(this));
604
605 uint32_t hwcCacheSlot = getFreeHwcCacheSlot();
606 mCachedBuffers[clientCacheId] = {hwcCacheSlot, mCounter++};
607 return hwcCacheSlot;
608}
609
610uint32_t BufferStateLayer::HwcSlotGenerator::getFreeHwcCacheSlot() REQUIRES(mMutex) {
611 if (mFreeHwcCacheSlots.empty()) {
612 evictLeastRecentlyUsed();
613 }
614
615 uint32_t hwcCacheSlot = mFreeHwcCacheSlots.top();
616 mFreeHwcCacheSlots.pop();
617 return hwcCacheSlot;
618}
619
620void BufferStateLayer::HwcSlotGenerator::evictLeastRecentlyUsed() REQUIRES(mMutex) {
621 uint64_t minCounter = UINT_MAX;
622 client_cache_t minClientCacheId = {};
623 for (const auto& [clientCacheId, slotCounter] : mCachedBuffers) {
624 const auto& [hwcCacheSlot, counter] = slotCounter;
625 if (counter < minCounter) {
626 minCounter = counter;
627 minClientCacheId = clientCacheId;
628 }
629 }
630 eraseBufferLocked(minClientCacheId);
631
632 ClientCache::getInstance().unregisterErasedRecipient(minClientCacheId, this);
633}
634
635void BufferStateLayer::HwcSlotGenerator::eraseBufferLocked(const client_cache_t& clientCacheId)
636 REQUIRES(mMutex) {
637 auto itr = mCachedBuffers.find(clientCacheId);
638 if (itr == mCachedBuffers.end()) {
639 return;
640 }
641 auto& [hwcCacheSlot, counter] = itr->second;
642
643 // TODO send to hwc cache and resources
644
645 mFreeHwcCacheSlots.push(hwcCacheSlot);
646 mCachedBuffers.erase(clientCacheId);
647}
chaviw4244e032019-09-04 11:27:49 -0700648
649void BufferStateLayer::gatherBufferInfo() {
650 const State& s(getDrawingState());
651
652 mBufferInfo.mDesiredPresentTime = s.desiredPresentTime;
653 mBufferInfo.mFenceTime = std::make_shared<FenceTime>(s.acquireFence);
654 mBufferInfo.mFence = s.acquireFence;
chaviw4244e032019-09-04 11:27:49 -0700655 mBufferInfo.mTransform = s.transform;
656 mBufferInfo.mDataspace = translateDataspace(s.dataspace);
657 mBufferInfo.mCrop = computeCrop(s);
658 mBufferInfo.mScaleMode = NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW;
659 mBufferInfo.mSurfaceDamage = s.surfaceDamageRegion;
660 mBufferInfo.mHdrMetadata = s.hdrMetadata;
661 mBufferInfo.mApi = s.api;
chaviwd62d3062019-09-04 14:48:02 -0700662 mBufferInfo.mPixelFormat =
663 !mBufferInfo.mBuffer ? PIXEL_FORMAT_NONE : mBufferInfo.mBuffer->format;
chaviw4244e032019-09-04 11:27:49 -0700664 mBufferInfo.mTransformToDisplayInverse = s.transformToDisplayInverse;
chaviwf83ce182019-09-12 14:43:08 -0700665 mBufferInfo.mBufferSlot = mHwcSlotGenerator->getHwcCacheSlot(s.clientCacheId);
chaviw4244e032019-09-04 11:27:49 -0700666}
667
668Rect BufferStateLayer::computeCrop(const State& s) {
669 if (s.crop.isEmpty() && s.buffer) {
670 return s.buffer->getBounds();
671 } else if (s.buffer) {
672 Rect crop = s.crop;
673 crop.left = std::max(crop.left, 0);
674 crop.top = std::max(crop.top, 0);
675 uint32_t bufferWidth = s.buffer->getWidth();
676 uint32_t bufferHeight = s.buffer->getHeight();
677 if (bufferHeight <= std::numeric_limits<int32_t>::max() &&
678 bufferWidth <= std::numeric_limits<int32_t>::max()) {
679 crop.right = std::min(crop.right, static_cast<int32_t>(bufferWidth));
680 crop.bottom = std::min(crop.bottom, static_cast<int32_t>(bufferHeight));
681 }
682 if (!crop.isValid()) {
683 // Crop rect is out of bounds, return whole buffer
684 return s.buffer->getBounds();
685 }
686 return crop;
687 }
688 return s.crop;
689}
690
chaviwb4c6e582019-08-16 14:35:07 -0700691sp<Layer> BufferStateLayer::createClone() {
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700692 LayerCreationArgs args(mFlinger.get(), nullptr, mName + " (Mirror)", 0, 0, 0, LayerMetadata());
chaviwb4c6e582019-08-16 14:35:07 -0700693 args.textureName = mTextureName;
Lloyd Pique1c3a5eb2019-10-03 13:07:08 -0700694 sp<BufferStateLayer> layer = mFlinger->getFactory().createBufferStateLayer(args);
chaviwb4c6e582019-08-16 14:35:07 -0700695 layer->mHwcSlotGenerator = mHwcSlotGenerator;
696 layer->setInitialValuesForClone(this);
697 return layer;
698}
Marissa Wall61c58622018-07-18 10:12:20 -0700699} // namespace android
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800700
701// TODO(b/129481165): remove the #pragma below and fix conversion issues
702#pragma clang diagnostic pop // ignored "-Wconversion"