blob: 664c19961ffd215d3d332db4d5fd7ee538cfbc08 [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
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080035#include "ColorLayer.h"
Mikael Pessa90092f42019-08-26 17:22:04 -070036#include "FrameTracer/FrameTracer.h"
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080037#include "TimeStats/TimeStats.h"
Valerie Hau0bc09152018-12-20 07:42:47 -080038
Marissa Wall61c58622018-07-18 10:12:20 -070039namespace android {
40
Lloyd Pique42ab75e2018-09-12 20:46:03 -070041// clang-format off
42const std::array<float, 16> BufferStateLayer::IDENTITY_MATRIX{
43 1, 0, 0, 0,
44 0, 1, 0, 0,
45 0, 0, 1, 0,
46 0, 0, 0, 1
47};
48// clang-format on
Marissa Wall61c58622018-07-18 10:12:20 -070049
Marissa Wall947d34e2019-03-29 14:03:53 -070050BufferStateLayer::BufferStateLayer(const LayerCreationArgs& args)
51 : BufferLayer(args), mHwcSlotGenerator(new HwcSlotGenerator()) {
Vishnu Nair60356342018-11-13 13:00:45 -080052 mOverrideScalingMode = NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW;
Marissa Wall3ff826c2019-02-07 11:58:25 -080053 mCurrentState.dataspace = ui::Dataspace::V0_SRGB;
chaviwb4c6e582019-08-16 14:35:07 -070054 if (const auto display = args.displayDevice) {
55 updateTransformHint(display);
56 }
Vishnu Nair60356342018-11-13 13:00:45 -080057}
Marissa Wall61c58622018-07-18 10:12:20 -070058
Alec Mouri4545a8a2019-08-08 20:05:32 -070059BufferStateLayer::~BufferStateLayer() {
chaviwb4c6e582019-08-16 14:35:07 -070060 // The original layer and the clone layer share the same texture and buffer. Therefore, only
61 // one of the layers, in this case the original layer, needs to handle the deletion. The
62 // original layer and the clone should be removed at the same time so there shouldn't be any
63 // issue with the clone layer trying to use the texture.
64 if (mBufferInfo.mBuffer != nullptr && !isClone()) {
chaviwd62d3062019-09-04 14:48:02 -070065 // Ensure that mBuffer is uncached from RenderEngine here, as
Alec Mouri4545a8a2019-08-08 20:05:32 -070066 // RenderEngine may have been using the buffer as an external texture
67 // after the client uncached the buffer.
68 auto& engine(mFlinger->getRenderEngine());
chaviwd62d3062019-09-04 14:48:02 -070069 engine.unbindExternalTextureBuffer(mBufferInfo.mBuffer->getId());
Alec Mouri4545a8a2019-08-08 20:05:32 -070070 }
71}
72
Marissa Wall61c58622018-07-18 10:12:20 -070073// -----------------------------------------------------------------------
74// Interface implementation for Layer
75// -----------------------------------------------------------------------
Marissa Wallfda30bb2018-10-12 11:34:28 -070076void BufferStateLayer::onLayerDisplayed(const sp<Fence>& releaseFence) {
Marissa Wall5a68a772018-12-22 17:43:42 -080077 // The previous release fence notifies the client that SurfaceFlinger is done with the previous
78 // buffer that was presented on this layer. The first transaction that came in this frame that
79 // replaced the previous buffer on this layer needs this release fence, because the fence will
80 // let the client know when that previous buffer is removed from the screen.
81 //
82 // Every other transaction on this layer does not need a release fence because no other
83 // Transactions that were set on this layer this frame are going to have their preceeding buffer
84 // removed from the display this frame.
85 //
86 // For example, if we have 3 transactions this frame. The first transaction doesn't contain a
87 // buffer so it doesn't need a previous release fence because the layer still needs the previous
88 // buffer. The second transaction contains a buffer so it needs a previous release fence because
89 // the previous buffer will be released this frame. The third transaction also contains a
90 // buffer. It replaces the buffer in the second transaction. The buffer in the second
91 // transaction will now no longer be presented so it is released immediately and the third
92 // transaction doesn't need a previous release fence.
93 for (auto& handle : mDrawingState.callbackHandles) {
94 if (handle->releasePreviousBuffer) {
95 handle->previousReleaseFence = releaseFence;
96 break;
97 }
98 }
Mikael Pessa2e1608f2019-07-19 11:25:35 -070099
100 // Prevent tracing the same release multiple times.
101 if (mPreviousFrameNumber != mPreviousReleasedFrameNumber) {
Mikael Pessa90092f42019-08-26 17:22:04 -0700102 mFlinger->mFrameTracer->traceFence(getSequence(), mPreviousBufferId, mPreviousFrameNumber,
103 std::make_shared<FenceTime>(releaseFence),
104 FrameTracer::FrameEvent::RELEASE_FENCE);
Mikael Pessa2e1608f2019-07-19 11:25:35 -0700105 mPreviousReleasedFrameNumber = mPreviousFrameNumber;
106 }
Marissa Wall61c58622018-07-18 10:12:20 -0700107}
108
Valerie Hau32cdc1f2019-10-21 14:45:54 -0700109void BufferStateLayer::setTransformHint(uint32_t orientation) const {
110 mTransformHint = orientation;
Marissa Wall61c58622018-07-18 10:12:20 -0700111}
112
113void BufferStateLayer::releasePendingBuffer(nsecs_t /*dequeueReadyTime*/) {
Valerie Hau32cdc1f2019-10-21 14:45:54 -0700114 for (const auto& handle : mDrawingState.callbackHandles) {
115 handle->transformHint = mTransformHint;
116 }
117
Marissa Wallefb71af2019-06-27 14:45:53 -0700118 mFlinger->getTransactionCompletedThread().finalizePendingCallbackHandles(
Marissa Wall5a68a772018-12-22 17:43:42 -0800119 mDrawingState.callbackHandles);
120
121 mDrawingState.callbackHandles = {};
Marissa Wall61c58622018-07-18 10:12:20 -0700122}
123
Ana Krulec010d2192018-10-08 06:29:54 -0700124bool BufferStateLayer::shouldPresentNow(nsecs_t /*expectedPresentTime*/) const {
Marissa Wall61c58622018-07-18 10:12:20 -0700125 if (getSidebandStreamChanged() || getAutoRefresh()) {
126 return true;
127 }
128
Marissa Wall024a1912018-08-13 13:55:35 -0700129 return hasFrameUpdate();
Marissa Wall61c58622018-07-18 10:12:20 -0700130}
131
Marissa Walle2ffb422018-10-12 11:33:52 -0700132bool BufferStateLayer::willPresentCurrentTransaction() const {
133 // Returns true if the most recent Transaction applied to CurrentState will be presented.
Robert Carr321e83c2019-08-19 15:49:30 -0700134 return (getSidebandStreamChanged() || getAutoRefresh() ||
Valerie Hauaa194562019-02-05 16:21:38 -0800135 (mCurrentState.modified &&
Robert Carr321e83c2019-08-19 15:49:30 -0700136 (mCurrentState.buffer != nullptr || mCurrentState.bgColorLayer != nullptr))) &&
137 !mLayerDetached;
Marissa Wall61c58622018-07-18 10:12:20 -0700138}
139
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800140void BufferStateLayer::pushPendingState() {
141 if (!mCurrentState.modified) {
Marissa Wall61c58622018-07-18 10:12:20 -0700142 return;
143 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800144 mPendingStates.push_back(mCurrentState);
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700145 ATRACE_INT(mTransactionName.c_str(), mPendingStates.size());
Marissa Wall61c58622018-07-18 10:12:20 -0700146}
147
148bool BufferStateLayer::applyPendingStates(Layer::State* stateToCommit) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800149 const bool stateUpdateAvailable = !mPendingStates.empty();
150 while (!mPendingStates.empty()) {
Marissa Wall61c58622018-07-18 10:12:20 -0700151 popPendingState(stateToCommit);
152 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800153 mCurrentStateModified = stateUpdateAvailable && mCurrentState.modified;
154 mCurrentState.modified = false;
Marissa Wall61c58622018-07-18 10:12:20 -0700155 return stateUpdateAvailable;
156}
157
Marissa Wall861616d2018-10-22 12:52:23 -0700158// Crop that applies to the window
159Rect BufferStateLayer::getCrop(const Layer::State& /*s*/) const {
160 return Rect::INVALID_RECT;
Marissa Wall61c58622018-07-18 10:12:20 -0700161}
162
163bool BufferStateLayer::setTransform(uint32_t transform) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800164 if (mCurrentState.transform == transform) return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800165 mCurrentState.transform = transform;
166 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700167 setTransactionFlags(eTransactionNeeded);
168 return true;
169}
170
171bool BufferStateLayer::setTransformToDisplayInverse(bool transformToDisplayInverse) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800172 if (mCurrentState.transformToDisplayInverse == transformToDisplayInverse) return false;
173 mCurrentState.sequence++;
174 mCurrentState.transformToDisplayInverse = transformToDisplayInverse;
175 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700176 setTransactionFlags(eTransactionNeeded);
177 return true;
178}
179
180bool BufferStateLayer::setCrop(const Rect& crop) {
Marissa Wall290ad082019-03-06 13:23:47 -0800181 Rect c = crop;
182 if (c.left < 0) {
183 c.left = 0;
184 }
185 if (c.top < 0) {
186 c.top = 0;
187 }
188 // If the width and/or height are < 0, make it [0, 0, -1, -1] so the equality comparision below
189 // treats all invalid rectangles the same.
190 if (!c.isValid()) {
191 c.makeInvalid();
192 }
193
194 if (mCurrentState.crop == c) return false;
Marissa Wall290ad082019-03-06 13:23:47 -0800195 mCurrentState.crop = c;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800196 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700197 setTransactionFlags(eTransactionNeeded);
198 return true;
199}
200
Marissa Wall861616d2018-10-22 12:52:23 -0700201bool BufferStateLayer::setFrame(const Rect& frame) {
202 int x = frame.left;
203 int y = frame.top;
204 int w = frame.getWidth();
205 int h = frame.getHeight();
206
Marissa Wall0f3242d2018-12-20 15:10:22 -0800207 if (x < 0) {
208 x = 0;
209 w = frame.right;
210 }
211
212 if (y < 0) {
213 y = 0;
214 h = frame.bottom;
215 }
216
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800217 if (mCurrentState.active.transform.tx() == x && mCurrentState.active.transform.ty() == y &&
218 mCurrentState.active.w == w && mCurrentState.active.h == h) {
Marissa Wall861616d2018-10-22 12:52:23 -0700219 return false;
220 }
221
222 if (!frame.isValid()) {
223 x = y = w = h = 0;
224 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800225 mCurrentState.active.transform.set(x, y);
226 mCurrentState.active.w = w;
227 mCurrentState.active.h = h;
Marissa Wall861616d2018-10-22 12:52:23 -0700228
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800229 mCurrentState.sequence++;
230 mCurrentState.modified = true;
Marissa Wall861616d2018-10-22 12:52:23 -0700231 setTransactionFlags(eTransactionNeeded);
232 return true;
233}
234
Ady Abraham09bd3922019-04-08 10:44:56 -0700235bool BufferStateLayer::setBuffer(const sp<GraphicBuffer>& buffer, nsecs_t postTime,
Marissa Wall947d34e2019-03-29 14:03:53 -0700236 nsecs_t desiredPresentTime, const client_cache_t& clientCacheId) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800237 if (mCurrentState.buffer) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700238 mReleasePreviousBuffer = true;
239 }
240
Valerie Hau2f54d642020-01-22 09:37:03 -0800241 mFrameCounter++;
242
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800243 mCurrentState.buffer = buffer;
Marissa Wall947d34e2019-03-29 14:03:53 -0700244 mCurrentState.clientCacheId = clientCacheId;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800245 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700246 setTransactionFlags(eTransactionNeeded);
Ady Abraham09bd3922019-04-08 10:44:56 -0700247
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800248 const int32_t layerId = getSequence();
249 mFlinger->mTimeStats->setPostTime(layerId, mFrameNumber, getName().c_str(), postTime);
250 mFlinger->mFrameTracer->traceNewLayer(layerId, getName().c_str());
251 mFlinger->mFrameTracer->traceTimestamp(layerId, buffer->getId(), mFrameNumber, postTime,
Mikael Pessa90092f42019-08-26 17:22:04 -0700252 FrameTracer::FrameEvent::POST);
chaviwfa67b552019-08-12 16:51:55 -0700253 mCurrentState.desiredPresentTime = desiredPresentTime;
Ady Abraham09bd3922019-04-08 10:44:56 -0700254
Ady Abraham8a82ba62020-01-17 12:43:17 -0800255 mFlinger->mScheduler->recordLayerHistory(this,
256 desiredPresentTime <= 0 ? 0 : desiredPresentTime);
Ady Abraham09bd3922019-04-08 10:44:56 -0700257
Marissa Wall61c58622018-07-18 10:12:20 -0700258 return true;
259}
260
261bool BufferStateLayer::setAcquireFence(const sp<Fence>& fence) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700262 // The acquire fences of BufferStateLayers have already signaled before they are set
263 mCallbackHandleAcquireTime = fence->getSignalTime();
264
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800265 mCurrentState.acquireFence = fence;
266 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700267 setTransactionFlags(eTransactionNeeded);
268 return true;
269}
270
271bool BufferStateLayer::setDataspace(ui::Dataspace dataspace) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800272 if (mCurrentState.dataspace == dataspace) return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800273 mCurrentState.dataspace = dataspace;
274 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700275 setTransactionFlags(eTransactionNeeded);
276 return true;
277}
278
279bool BufferStateLayer::setHdrMetadata(const HdrMetadata& hdrMetadata) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800280 if (mCurrentState.hdrMetadata == hdrMetadata) return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800281 mCurrentState.hdrMetadata = hdrMetadata;
282 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700283 setTransactionFlags(eTransactionNeeded);
284 return true;
285}
286
287bool BufferStateLayer::setSurfaceDamageRegion(const Region& surfaceDamage) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800288 mCurrentState.surfaceDamageRegion = surfaceDamage;
289 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700290 setTransactionFlags(eTransactionNeeded);
291 return true;
292}
293
294bool BufferStateLayer::setApi(int32_t api) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800295 if (mCurrentState.api == api) return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800296 mCurrentState.api = api;
297 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700298 setTransactionFlags(eTransactionNeeded);
299 return true;
300}
301
302bool BufferStateLayer::setSidebandStream(const sp<NativeHandle>& sidebandStream) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800303 if (mCurrentState.sidebandStream == sidebandStream) return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800304 mCurrentState.sidebandStream = sidebandStream;
305 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700306 setTransactionFlags(eTransactionNeeded);
307
308 if (!mSidebandStreamChanged.exchange(true)) {
309 // mSidebandStreamChanged was false
310 mFlinger->signalLayerUpdate();
311 }
312 return true;
313}
314
Marissa Walle2ffb422018-10-12 11:33:52 -0700315bool BufferStateLayer::setTransactionCompletedListeners(
316 const std::vector<sp<CallbackHandle>>& handles) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700317 // If there is no handle, we will not send a callback so reset mReleasePreviousBuffer and return
Marissa Walle2ffb422018-10-12 11:33:52 -0700318 if (handles.empty()) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700319 mReleasePreviousBuffer = false;
Marissa Walle2ffb422018-10-12 11:33:52 -0700320 return false;
321 }
322
323 const bool willPresent = willPresentCurrentTransaction();
324
325 for (const auto& handle : handles) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700326 // If this transaction set a buffer on this layer, release its previous buffer
327 handle->releasePreviousBuffer = mReleasePreviousBuffer;
328
Marissa Walle2ffb422018-10-12 11:33:52 -0700329 // If this layer will be presented in this frame
330 if (willPresent) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700331 // If this transaction set an acquire fence on this layer, set its acquire time
332 handle->acquireTime = mCallbackHandleAcquireTime;
333
Marissa Walle2ffb422018-10-12 11:33:52 -0700334 // Notify the transaction completed thread that there is a pending latched callback
335 // handle
Marissa Wall5a68a772018-12-22 17:43:42 -0800336 mFlinger->getTransactionCompletedThread().registerPendingCallbackHandle(handle);
Marissa Walle2ffb422018-10-12 11:33:52 -0700337
338 // Store so latched time and release fence can be set
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800339 mCurrentState.callbackHandles.push_back(handle);
Marissa Walle2ffb422018-10-12 11:33:52 -0700340
341 } else { // If this layer will NOT need to be relatched and presented this frame
342 // Notify the transaction completed thread this handle is done
Marissa Wallefb71af2019-06-27 14:45:53 -0700343 mFlinger->getTransactionCompletedThread().registerUnpresentedCallbackHandle(handle);
Marissa Walle2ffb422018-10-12 11:33:52 -0700344 }
345 }
346
Marissa Wallfda30bb2018-10-12 11:34:28 -0700347 mReleasePreviousBuffer = false;
348 mCallbackHandleAcquireTime = -1;
349
Marissa Walle2ffb422018-10-12 11:33:52 -0700350 return willPresent;
351}
352
Valerie Hau7618b232020-01-09 16:03:08 -0800353void BufferStateLayer::forceSendCallbacks() {
354 mFlinger->getTransactionCompletedThread().finalizePendingCallbackHandles(
355 mCurrentState.callbackHandles);
356}
357
Marissa Wall61c58622018-07-18 10:12:20 -0700358bool BufferStateLayer::setTransparentRegionHint(const Region& transparent) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800359 mCurrentState.transparentRegionHint = transparent;
360 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700361 setTransactionFlags(eTransactionNeeded);
362 return true;
363}
364
Marissa Wall861616d2018-10-22 12:52:23 -0700365Rect BufferStateLayer::getBufferSize(const State& s) const {
366 // for buffer state layers we use the display frame size as the buffer size.
367 if (getActiveWidth(s) < UINT32_MAX && getActiveHeight(s) < UINT32_MAX) {
368 return Rect(getActiveWidth(s), getActiveHeight(s));
Marissa Wall61c58622018-07-18 10:12:20 -0700369 }
370
Marissa Wall861616d2018-10-22 12:52:23 -0700371 // if the display frame is not defined, use the parent bounds as the buffer size.
372 const auto& p = mDrawingParent.promote();
373 if (p != nullptr) {
Vishnu Nair4351ad52019-02-11 14:13:02 -0800374 Rect parentBounds = Rect(p->getBounds(Region()));
Marissa Wall861616d2018-10-22 12:52:23 -0700375 if (!parentBounds.isEmpty()) {
376 return parentBounds;
377 }
378 }
379
Marissa Wall861616d2018-10-22 12:52:23 -0700380 return Rect::INVALID_RECT;
Marissa Wall61c58622018-07-18 10:12:20 -0700381}
Vishnu Nair4351ad52019-02-11 14:13:02 -0800382
383FloatRect BufferStateLayer::computeSourceBounds(const FloatRect& parentBounds) const {
384 const State& s(getDrawingState());
385 // for buffer state layers we use the display frame size as the buffer size.
386 if (getActiveWidth(s) < UINT32_MAX && getActiveHeight(s) < UINT32_MAX) {
387 return FloatRect(0, 0, getActiveWidth(s), getActiveHeight(s));
388 }
389
390 // if the display frame is not defined, use the parent bounds as the buffer size.
391 return parentBounds;
392}
393
Marissa Wall61c58622018-07-18 10:12:20 -0700394// -----------------------------------------------------------------------
395
396// -----------------------------------------------------------------------
397// Interface implementation for BufferLayer
398// -----------------------------------------------------------------------
399bool BufferStateLayer::fenceHasSignaled() const {
400 if (latchUnsignaledBuffers()) {
401 return true;
402 }
403
404 return getDrawingState().acquireFence->getStatus() == Fence::Status::Signaled;
405}
406
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700407bool BufferStateLayer::framePresentTimeIsCurrent(nsecs_t expectedPresentTime) const {
Ady Abrahamcd1580c2019-04-29 15:40:03 -0700408 if (!hasFrameUpdate() || isRemovedFromCurrentState()) {
409 return true;
410 }
411
chaviwfa67b552019-08-12 16:51:55 -0700412 return mCurrentState.desiredPresentTime <= expectedPresentTime;
Ady Abrahamcd1580c2019-04-29 15:40:03 -0700413}
414
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700415uint64_t BufferStateLayer::getFrameNumber(nsecs_t /*expectedPresentTime*/) const {
Marissa Wall61c58622018-07-18 10:12:20 -0700416 return mFrameNumber;
417}
418
419bool BufferStateLayer::getAutoRefresh() const {
420 // TODO(marissaw): support shared buffer mode
421 return false;
422}
423
424bool BufferStateLayer::getSidebandStreamChanged() const {
425 return mSidebandStreamChanged.load();
426}
427
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800428bool BufferStateLayer::latchSidebandStream(bool& recomputeVisibleRegions) {
Marissa Wall61c58622018-07-18 10:12:20 -0700429 if (mSidebandStreamChanged.exchange(false)) {
430 const State& s(getDrawingState());
431 // mSidebandStreamChanged was true
Lloyd Pique0b785d82018-12-04 17:25:27 -0800432 mSidebandStream = s.sidebandStream;
Lloyd Piquede196652020-01-22 17:29:58 -0800433 editCompositionState()->sidebandStream = mSidebandStream;
Lloyd Pique0b785d82018-12-04 17:25:27 -0800434 if (mSidebandStream != nullptr) {
Marissa Wall61c58622018-07-18 10:12:20 -0700435 setTransactionFlags(eTransactionNeeded);
436 mFlinger->setTransactionFlags(eTraversalNeeded);
437 }
438 recomputeVisibleRegions = true;
439
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800440 return true;
Marissa Wall61c58622018-07-18 10:12:20 -0700441 }
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800442 return false;
Marissa Wall61c58622018-07-18 10:12:20 -0700443}
444
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800445bool BufferStateLayer::hasFrameUpdate() const {
Valerie Hauaa194562019-02-05 16:21:38 -0800446 const State& c(getCurrentState());
447 return mCurrentStateModified && (c.buffer != nullptr || c.bgColorLayer != nullptr);
Marissa Wall61c58622018-07-18 10:12:20 -0700448}
449
Alec Mouri39801c02018-10-10 10:44:47 -0700450status_t BufferStateLayer::bindTextureImage() {
Marissa Wall61c58622018-07-18 10:12:20 -0700451 const State& s(getDrawingState());
452 auto& engine(mFlinger->getRenderEngine());
453
Alec Mourib5c4f352019-02-19 19:46:38 -0800454 return engine.bindExternalTextureBuffer(mTextureName, s.buffer, s.acquireFence);
Marissa Wall61c58622018-07-18 10:12:20 -0700455}
456
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700457status_t BufferStateLayer::updateTexImage(bool& /*recomputeVisibleRegions*/, nsecs_t latchTime,
458 nsecs_t /*expectedPresentTime*/) {
Marissa Wall61c58622018-07-18 10:12:20 -0700459 const State& s(getDrawingState());
460
461 if (!s.buffer) {
Valerie Hauaa194562019-02-05 16:21:38 -0800462 if (s.bgColorLayer) {
463 for (auto& handle : mDrawingState.callbackHandles) {
464 handle->latchTime = latchTime;
465 }
466 }
Marissa Wall61c58622018-07-18 10:12:20 -0700467 return NO_ERROR;
468 }
469
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800470 const int32_t layerId = getSequence();
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700471
Marissa Wall61c58622018-07-18 10:12:20 -0700472 // Reject if the layer is invalid
473 uint32_t bufferWidth = s.buffer->width;
474 uint32_t bufferHeight = s.buffer->height;
475
Peiyong Linefefaac2018-08-17 12:27:51 -0700476 if (s.transform & ui::Transform::ROT_90) {
Peiyong Lin3db42342018-08-16 09:15:59 -0700477 std::swap(bufferWidth, bufferHeight);
Marissa Wall61c58622018-07-18 10:12:20 -0700478 }
479
480 if (s.transformToDisplayInverse) {
Dominik Laskowski718f9602019-11-09 20:01:35 -0800481 uint32_t invTransform = DisplayDevice::getPrimaryDisplayRotationFlags();
Peiyong Linefefaac2018-08-17 12:27:51 -0700482 if (invTransform & ui::Transform::ROT_90) {
Peiyong Lin3db42342018-08-16 09:15:59 -0700483 std::swap(bufferWidth, bufferHeight);
Marissa Wall61c58622018-07-18 10:12:20 -0700484 }
485 }
486
Vishnu Nair60356342018-11-13 13:00:45 -0800487 if (getEffectiveScalingMode() == NATIVE_WINDOW_SCALING_MODE_FREEZE &&
Marissa Wall61c58622018-07-18 10:12:20 -0700488 (s.active.w != bufferWidth || s.active.h != bufferHeight)) {
489 ALOGE("[%s] rejecting buffer: "
490 "bufferWidth=%d, bufferHeight=%d, front.active.{w=%d, h=%d}",
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700491 getDebugName(), bufferWidth, bufferHeight, s.active.w, s.active.h);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800492 mFlinger->mTimeStats->removeTimeRecord(layerId, mFrameNumber);
Marissa Wall61c58622018-07-18 10:12:20 -0700493 return BAD_VALUE;
494 }
495
Marissa Wall5a68a772018-12-22 17:43:42 -0800496 for (auto& handle : mDrawingState.callbackHandles) {
497 handle->latchTime = latchTime;
498 }
Marissa Walle2ffb422018-10-12 11:33:52 -0700499
Valerie Hau2f54d642020-01-22 09:37:03 -0800500 mFrameNumber = mFrameCounter;
501
Alec Mouri56e538f2019-01-14 15:22:01 -0800502 if (!SyncFeatures::getInstance().useNativeFenceSync()) {
Marissa Wall61c58622018-07-18 10:12:20 -0700503 // Bind the new buffer to the GL texture.
504 //
505 // Older devices require the "implicit" synchronization provided
506 // by glEGLImageTargetTexture2DOES, which this method calls. Newer
507 // devices will either call this in Layer::onDraw, or (if it's not
508 // a GL-composited layer) not at all.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800509 status_t err = bindTextureImage();
Marissa Wall61c58622018-07-18 10:12:20 -0700510 if (err != NO_ERROR) {
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800511 mFlinger->mTimeStats->onDestroy(layerId);
512 mFlinger->mFrameTracer->onDestroy(layerId);
Marissa Wall61c58622018-07-18 10:12:20 -0700513 return BAD_VALUE;
514 }
515 }
516
Mikael Pessa2e1608f2019-07-19 11:25:35 -0700517 const uint64_t bufferID = getCurrentBufferId();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800518 mFlinger->mTimeStats->setAcquireFence(layerId, mFrameNumber, mBufferInfo.mFenceTime);
519 mFlinger->mFrameTracer->traceFence(layerId, bufferID, mFrameNumber, mBufferInfo.mFenceTime,
Mikael Pessa90092f42019-08-26 17:22:04 -0700520 FrameTracer::FrameEvent::ACQUIRE_FENCE);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800521 mFlinger->mTimeStats->setLatchTime(layerId, mFrameNumber, latchTime);
522 mFlinger->mFrameTracer->traceTimestamp(layerId, bufferID, mFrameNumber, latchTime,
Mikael Pessa90092f42019-08-26 17:22:04 -0700523 FrameTracer::FrameEvent::LATCH);
Marissa Wall61c58622018-07-18 10:12:20 -0700524
Marissa Wall16c112d2019-03-20 13:21:13 -0700525 mCurrentStateModified = false;
526
Marissa Wall61c58622018-07-18 10:12:20 -0700527 return NO_ERROR;
528}
529
530status_t BufferStateLayer::updateActiveBuffer() {
531 const State& s(getDrawingState());
532
533 if (s.buffer == nullptr) {
534 return BAD_VALUE;
535 }
536
Mikael Pessa2e1608f2019-07-19 11:25:35 -0700537 mPreviousBufferId = getCurrentBufferId();
chaviwd62d3062019-09-04 14:48:02 -0700538 mBufferInfo.mBuffer = s.buffer;
539 mBufferInfo.mFence = s.acquireFence;
Lloyd Piquede196652020-01-22 17:29:58 -0800540 editCompositionState()->buffer = mBufferInfo.mBuffer;
Marissa Wall61c58622018-07-18 10:12:20 -0700541
542 return NO_ERROR;
543}
544
545status_t BufferStateLayer::updateFrameNumber(nsecs_t /*latchTime*/) {
546 // TODO(marissaw): support frame history events
Mikael Pessa2e1608f2019-07-19 11:25:35 -0700547 mPreviousFrameNumber = mCurrentFrameNumber;
Marissa Wall61c58622018-07-18 10:12:20 -0700548 mCurrentFrameNumber = mFrameNumber;
549 return NO_ERROR;
550}
551
Lloyd Piquede196652020-01-22 17:29:58 -0800552void BufferStateLayer::preparePerFrameCompositionState() {
553 BufferLayer::preparePerFrameCompositionState();
554
555 auto* compositionState = editCompositionState();
556 if (compositionState->compositionType == Hwc2::IComposerClient::Composition::SIDEBAND) {
Lloyd Piquef5275482019-01-29 18:42:42 -0800557 return;
558 }
Marissa Wall61c58622018-07-18 10:12:20 -0700559
Lloyd Piquede196652020-01-22 17:29:58 -0800560 compositionState->buffer = mBufferInfo.mBuffer;
561 compositionState->bufferSlot = mBufferInfo.mBufferSlot;
562 compositionState->acquireFence = mBufferInfo.mFence;
Marissa Wall61c58622018-07-18 10:12:20 -0700563}
564
Marissa Wall947d34e2019-03-29 14:03:53 -0700565void BufferStateLayer::HwcSlotGenerator::bufferErased(const client_cache_t& clientCacheId) {
566 std::lock_guard lock(mMutex);
567 if (!clientCacheId.isValid()) {
568 ALOGE("invalid process, failed to erase buffer");
569 return;
570 }
571 eraseBufferLocked(clientCacheId);
572}
573
574uint32_t BufferStateLayer::HwcSlotGenerator::getHwcCacheSlot(const client_cache_t& clientCacheId) {
575 std::lock_guard<std::mutex> lock(mMutex);
576 auto itr = mCachedBuffers.find(clientCacheId);
577 if (itr == mCachedBuffers.end()) {
578 return addCachedBuffer(clientCacheId);
579 }
580 auto& [hwcCacheSlot, counter] = itr->second;
581 counter = mCounter++;
582 return hwcCacheSlot;
583}
584
585uint32_t BufferStateLayer::HwcSlotGenerator::addCachedBuffer(const client_cache_t& clientCacheId)
586 REQUIRES(mMutex) {
587 if (!clientCacheId.isValid()) {
588 ALOGE("invalid process, returning invalid slot");
589 return BufferQueue::INVALID_BUFFER_SLOT;
590 }
591
592 ClientCache::getInstance().registerErasedRecipient(clientCacheId, wp<ErasedRecipient>(this));
593
594 uint32_t hwcCacheSlot = getFreeHwcCacheSlot();
595 mCachedBuffers[clientCacheId] = {hwcCacheSlot, mCounter++};
596 return hwcCacheSlot;
597}
598
599uint32_t BufferStateLayer::HwcSlotGenerator::getFreeHwcCacheSlot() REQUIRES(mMutex) {
600 if (mFreeHwcCacheSlots.empty()) {
601 evictLeastRecentlyUsed();
602 }
603
604 uint32_t hwcCacheSlot = mFreeHwcCacheSlots.top();
605 mFreeHwcCacheSlots.pop();
606 return hwcCacheSlot;
607}
608
609void BufferStateLayer::HwcSlotGenerator::evictLeastRecentlyUsed() REQUIRES(mMutex) {
610 uint64_t minCounter = UINT_MAX;
611 client_cache_t minClientCacheId = {};
612 for (const auto& [clientCacheId, slotCounter] : mCachedBuffers) {
613 const auto& [hwcCacheSlot, counter] = slotCounter;
614 if (counter < minCounter) {
615 minCounter = counter;
616 minClientCacheId = clientCacheId;
617 }
618 }
619 eraseBufferLocked(minClientCacheId);
620
621 ClientCache::getInstance().unregisterErasedRecipient(minClientCacheId, this);
622}
623
624void BufferStateLayer::HwcSlotGenerator::eraseBufferLocked(const client_cache_t& clientCacheId)
625 REQUIRES(mMutex) {
626 auto itr = mCachedBuffers.find(clientCacheId);
627 if (itr == mCachedBuffers.end()) {
628 return;
629 }
630 auto& [hwcCacheSlot, counter] = itr->second;
631
632 // TODO send to hwc cache and resources
633
634 mFreeHwcCacheSlots.push(hwcCacheSlot);
635 mCachedBuffers.erase(clientCacheId);
636}
chaviw4244e032019-09-04 11:27:49 -0700637
638void BufferStateLayer::gatherBufferInfo() {
639 const State& s(getDrawingState());
640
641 mBufferInfo.mDesiredPresentTime = s.desiredPresentTime;
642 mBufferInfo.mFenceTime = std::make_shared<FenceTime>(s.acquireFence);
643 mBufferInfo.mFence = s.acquireFence;
chaviw4244e032019-09-04 11:27:49 -0700644 mBufferInfo.mTransform = s.transform;
645 mBufferInfo.mDataspace = translateDataspace(s.dataspace);
646 mBufferInfo.mCrop = computeCrop(s);
647 mBufferInfo.mScaleMode = NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW;
648 mBufferInfo.mSurfaceDamage = s.surfaceDamageRegion;
649 mBufferInfo.mHdrMetadata = s.hdrMetadata;
650 mBufferInfo.mApi = s.api;
chaviwd62d3062019-09-04 14:48:02 -0700651 mBufferInfo.mPixelFormat =
652 !mBufferInfo.mBuffer ? PIXEL_FORMAT_NONE : mBufferInfo.mBuffer->format;
chaviw4244e032019-09-04 11:27:49 -0700653 mBufferInfo.mTransformToDisplayInverse = s.transformToDisplayInverse;
chaviwf83ce182019-09-12 14:43:08 -0700654 mBufferInfo.mBufferSlot = mHwcSlotGenerator->getHwcCacheSlot(s.clientCacheId);
chaviw4244e032019-09-04 11:27:49 -0700655}
656
657Rect BufferStateLayer::computeCrop(const State& s) {
658 if (s.crop.isEmpty() && s.buffer) {
659 return s.buffer->getBounds();
660 } else if (s.buffer) {
661 Rect crop = s.crop;
662 crop.left = std::max(crop.left, 0);
663 crop.top = std::max(crop.top, 0);
664 uint32_t bufferWidth = s.buffer->getWidth();
665 uint32_t bufferHeight = s.buffer->getHeight();
666 if (bufferHeight <= std::numeric_limits<int32_t>::max() &&
667 bufferWidth <= std::numeric_limits<int32_t>::max()) {
668 crop.right = std::min(crop.right, static_cast<int32_t>(bufferWidth));
669 crop.bottom = std::min(crop.bottom, static_cast<int32_t>(bufferHeight));
670 }
671 if (!crop.isValid()) {
672 // Crop rect is out of bounds, return whole buffer
673 return s.buffer->getBounds();
674 }
675 return crop;
676 }
677 return s.crop;
678}
679
chaviwb4c6e582019-08-16 14:35:07 -0700680sp<Layer> BufferStateLayer::createClone() {
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700681 LayerCreationArgs args(mFlinger.get(), nullptr, mName + " (Mirror)", 0, 0, 0, LayerMetadata());
chaviwb4c6e582019-08-16 14:35:07 -0700682 args.textureName = mTextureName;
Lloyd Pique1c3a5eb2019-10-03 13:07:08 -0700683 sp<BufferStateLayer> layer = mFlinger->getFactory().createBufferStateLayer(args);
chaviwb4c6e582019-08-16 14:35:07 -0700684 layer->mHwcSlotGenerator = mHwcSlotGenerator;
685 layer->setInitialValuesForClone(this);
686 return layer;
687}
Marissa Wall61c58622018-07-18 10:12:20 -0700688} // namespace android
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800689
690// TODO(b/129481165): remove the #pragma below and fix conversion issues
691#pragma clang diagnostic pop // ignored "-Wconversion"