blob: 2f3906b348bcad82bb81b87fd80b5e854e0c18a4 [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
17//#define LOG_NDEBUG 0
18#undef LOG_TAG
19#define LOG_TAG "BufferStateLayer"
20#define ATRACE_TAG ATRACE_TAG_GRAPHICS
21
Lloyd Pique9755fb72019-03-26 14:44:40 -070022#include "BufferStateLayer.h"
23
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080024#include <limits>
Marissa Wall61c58622018-07-18 10:12:20 -070025
Lloyd Pique0b785d82018-12-04 17:25:27 -080026#include <compositionengine/Layer.h>
Lloyd Pique9755fb72019-03-26 14:44:40 -070027#include <compositionengine/LayerFECompositionState.h>
Marissa Wall947d34e2019-03-29 14:03:53 -070028#include <gui/BufferQueue.h>
Marissa Wall61c58622018-07-18 10:12:20 -070029#include <private/gui/SyncFeatures.h>
Peiyong Lincbc184f2018-08-22 13:24:10 -070030#include <renderengine/Image.h>
Marissa Wall61c58622018-07-18 10:12:20 -070031
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080032#include "ColorLayer.h"
Mikael Pessa90092f42019-08-26 17:22:04 -070033#include "FrameTracer/FrameTracer.h"
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080034#include "TimeStats/TimeStats.h"
Valerie Hau0bc09152018-12-20 07:42:47 -080035
Marissa Wall61c58622018-07-18 10:12:20 -070036namespace android {
37
Lloyd Pique42ab75e2018-09-12 20:46:03 -070038// clang-format off
39const std::array<float, 16> BufferStateLayer::IDENTITY_MATRIX{
40 1, 0, 0, 0,
41 0, 1, 0, 0,
42 0, 0, 1, 0,
43 0, 0, 0, 1
44};
45// clang-format on
Marissa Wall61c58622018-07-18 10:12:20 -070046
Marissa Wall947d34e2019-03-29 14:03:53 -070047BufferStateLayer::BufferStateLayer(const LayerCreationArgs& args)
48 : BufferLayer(args), mHwcSlotGenerator(new HwcSlotGenerator()) {
Vishnu Nair60356342018-11-13 13:00:45 -080049 mOverrideScalingMode = NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW;
Marissa Wall3ff826c2019-02-07 11:58:25 -080050 mCurrentState.dataspace = ui::Dataspace::V0_SRGB;
chaviwb4c6e582019-08-16 14:35:07 -070051 if (const auto display = args.displayDevice) {
52 updateTransformHint(display);
53 }
Vishnu Nair60356342018-11-13 13:00:45 -080054}
Marissa Wall61c58622018-07-18 10:12:20 -070055
Alec Mouri4545a8a2019-08-08 20:05:32 -070056BufferStateLayer::~BufferStateLayer() {
chaviwb4c6e582019-08-16 14:35:07 -070057 // The original layer and the clone layer share the same texture and buffer. Therefore, only
58 // one of the layers, in this case the original layer, needs to handle the deletion. The
59 // original layer and the clone should be removed at the same time so there shouldn't be any
60 // issue with the clone layer trying to use the texture.
61 if (mBufferInfo.mBuffer != nullptr && !isClone()) {
chaviwd62d3062019-09-04 14:48:02 -070062 // Ensure that mBuffer is uncached from RenderEngine here, as
Alec Mouri4545a8a2019-08-08 20:05:32 -070063 // RenderEngine may have been using the buffer as an external texture
64 // after the client uncached the buffer.
65 auto& engine(mFlinger->getRenderEngine());
chaviwd62d3062019-09-04 14:48:02 -070066 engine.unbindExternalTextureBuffer(mBufferInfo.mBuffer->getId());
Alec Mouri4545a8a2019-08-08 20:05:32 -070067 }
68}
69
Marissa Wall61c58622018-07-18 10:12:20 -070070// -----------------------------------------------------------------------
71// Interface implementation for Layer
72// -----------------------------------------------------------------------
Marissa Wallfda30bb2018-10-12 11:34:28 -070073void BufferStateLayer::onLayerDisplayed(const sp<Fence>& releaseFence) {
Marissa Wall5a68a772018-12-22 17:43:42 -080074 // The previous release fence notifies the client that SurfaceFlinger is done with the previous
75 // buffer that was presented on this layer. The first transaction that came in this frame that
76 // replaced the previous buffer on this layer needs this release fence, because the fence will
77 // let the client know when that previous buffer is removed from the screen.
78 //
79 // Every other transaction on this layer does not need a release fence because no other
80 // Transactions that were set on this layer this frame are going to have their preceeding buffer
81 // removed from the display this frame.
82 //
83 // For example, if we have 3 transactions this frame. The first transaction doesn't contain a
84 // buffer so it doesn't need a previous release fence because the layer still needs the previous
85 // buffer. The second transaction contains a buffer so it needs a previous release fence because
86 // the previous buffer will be released this frame. The third transaction also contains a
87 // buffer. It replaces the buffer in the second transaction. The buffer in the second
88 // transaction will now no longer be presented so it is released immediately and the third
89 // transaction doesn't need a previous release fence.
90 for (auto& handle : mDrawingState.callbackHandles) {
91 if (handle->releasePreviousBuffer) {
92 handle->previousReleaseFence = releaseFence;
93 break;
94 }
95 }
Mikael Pessa2e1608f2019-07-19 11:25:35 -070096
97 // Prevent tracing the same release multiple times.
98 if (mPreviousFrameNumber != mPreviousReleasedFrameNumber) {
Mikael Pessa90092f42019-08-26 17:22:04 -070099 mFlinger->mFrameTracer->traceFence(getSequence(), mPreviousBufferId, mPreviousFrameNumber,
100 std::make_shared<FenceTime>(releaseFence),
101 FrameTracer::FrameEvent::RELEASE_FENCE);
Mikael Pessa2e1608f2019-07-19 11:25:35 -0700102 mPreviousReleasedFrameNumber = mPreviousFrameNumber;
103 }
Marissa Wall61c58622018-07-18 10:12:20 -0700104}
105
Valerie Hau32cdc1f2019-10-21 14:45:54 -0700106void BufferStateLayer::setTransformHint(uint32_t orientation) const {
107 mTransformHint = orientation;
Marissa Wall61c58622018-07-18 10:12:20 -0700108}
109
110void BufferStateLayer::releasePendingBuffer(nsecs_t /*dequeueReadyTime*/) {
Valerie Hau32cdc1f2019-10-21 14:45:54 -0700111 for (const auto& handle : mDrawingState.callbackHandles) {
112 handle->transformHint = mTransformHint;
113 }
114
Marissa Wallefb71af2019-06-27 14:45:53 -0700115 mFlinger->getTransactionCompletedThread().finalizePendingCallbackHandles(
Marissa Wall5a68a772018-12-22 17:43:42 -0800116 mDrawingState.callbackHandles);
117
118 mDrawingState.callbackHandles = {};
Marissa Wall61c58622018-07-18 10:12:20 -0700119}
120
Ana Krulec010d2192018-10-08 06:29:54 -0700121bool BufferStateLayer::shouldPresentNow(nsecs_t /*expectedPresentTime*/) const {
Marissa Wall61c58622018-07-18 10:12:20 -0700122 if (getSidebandStreamChanged() || getAutoRefresh()) {
123 return true;
124 }
125
Marissa Wall024a1912018-08-13 13:55:35 -0700126 return hasFrameUpdate();
Marissa Wall61c58622018-07-18 10:12:20 -0700127}
128
Marissa Walle2ffb422018-10-12 11:33:52 -0700129bool BufferStateLayer::willPresentCurrentTransaction() const {
130 // Returns true if the most recent Transaction applied to CurrentState will be presented.
Robert Carr321e83c2019-08-19 15:49:30 -0700131 return (getSidebandStreamChanged() || getAutoRefresh() ||
Valerie Hauaa194562019-02-05 16:21:38 -0800132 (mCurrentState.modified &&
Robert Carr321e83c2019-08-19 15:49:30 -0700133 (mCurrentState.buffer != nullptr || mCurrentState.bgColorLayer != nullptr))) &&
134 !mLayerDetached;
Marissa Wall61c58622018-07-18 10:12:20 -0700135}
136
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800137void BufferStateLayer::pushPendingState() {
138 if (!mCurrentState.modified) {
Marissa Wall61c58622018-07-18 10:12:20 -0700139 return;
140 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800141 mPendingStates.push_back(mCurrentState);
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700142 ATRACE_INT(mTransactionName.c_str(), mPendingStates.size());
Marissa Wall61c58622018-07-18 10:12:20 -0700143}
144
145bool BufferStateLayer::applyPendingStates(Layer::State* stateToCommit) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800146 const bool stateUpdateAvailable = !mPendingStates.empty();
147 while (!mPendingStates.empty()) {
Marissa Wall61c58622018-07-18 10:12:20 -0700148 popPendingState(stateToCommit);
149 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800150 mCurrentStateModified = stateUpdateAvailable && mCurrentState.modified;
151 mCurrentState.modified = false;
Marissa Wall61c58622018-07-18 10:12:20 -0700152 return stateUpdateAvailable;
153}
154
Marissa Wall861616d2018-10-22 12:52:23 -0700155// Crop that applies to the window
156Rect BufferStateLayer::getCrop(const Layer::State& /*s*/) const {
157 return Rect::INVALID_RECT;
Marissa Wall61c58622018-07-18 10:12:20 -0700158}
159
160bool BufferStateLayer::setTransform(uint32_t transform) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800161 if (mCurrentState.transform == transform) return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800162 mCurrentState.transform = transform;
163 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700164 setTransactionFlags(eTransactionNeeded);
165 return true;
166}
167
168bool BufferStateLayer::setTransformToDisplayInverse(bool transformToDisplayInverse) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800169 if (mCurrentState.transformToDisplayInverse == transformToDisplayInverse) return false;
170 mCurrentState.sequence++;
171 mCurrentState.transformToDisplayInverse = transformToDisplayInverse;
172 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700173 setTransactionFlags(eTransactionNeeded);
174 return true;
175}
176
177bool BufferStateLayer::setCrop(const Rect& crop) {
Marissa Wall290ad082019-03-06 13:23:47 -0800178 Rect c = crop;
179 if (c.left < 0) {
180 c.left = 0;
181 }
182 if (c.top < 0) {
183 c.top = 0;
184 }
185 // If the width and/or height are < 0, make it [0, 0, -1, -1] so the equality comparision below
186 // treats all invalid rectangles the same.
187 if (!c.isValid()) {
188 c.makeInvalid();
189 }
190
191 if (mCurrentState.crop == c) return false;
Marissa Wall290ad082019-03-06 13:23:47 -0800192 mCurrentState.crop = c;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800193 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700194 setTransactionFlags(eTransactionNeeded);
195 return true;
196}
197
Marissa Wall861616d2018-10-22 12:52:23 -0700198bool BufferStateLayer::setFrame(const Rect& frame) {
199 int x = frame.left;
200 int y = frame.top;
201 int w = frame.getWidth();
202 int h = frame.getHeight();
203
Marissa Wall0f3242d2018-12-20 15:10:22 -0800204 if (x < 0) {
205 x = 0;
206 w = frame.right;
207 }
208
209 if (y < 0) {
210 y = 0;
211 h = frame.bottom;
212 }
213
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800214 if (mCurrentState.active.transform.tx() == x && mCurrentState.active.transform.ty() == y &&
215 mCurrentState.active.w == w && mCurrentState.active.h == h) {
Marissa Wall861616d2018-10-22 12:52:23 -0700216 return false;
217 }
218
219 if (!frame.isValid()) {
220 x = y = w = h = 0;
221 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800222 mCurrentState.active.transform.set(x, y);
223 mCurrentState.active.w = w;
224 mCurrentState.active.h = h;
Marissa Wall861616d2018-10-22 12:52:23 -0700225
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800226 mCurrentState.sequence++;
227 mCurrentState.modified = true;
Marissa Wall861616d2018-10-22 12:52:23 -0700228 setTransactionFlags(eTransactionNeeded);
229 return true;
230}
231
Ady Abraham09bd3922019-04-08 10:44:56 -0700232bool BufferStateLayer::setBuffer(const sp<GraphicBuffer>& buffer, nsecs_t postTime,
Marissa Wall947d34e2019-03-29 14:03:53 -0700233 nsecs_t desiredPresentTime, const client_cache_t& clientCacheId) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800234 if (mCurrentState.buffer) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700235 mReleasePreviousBuffer = true;
236 }
237
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800238 mCurrentState.buffer = buffer;
Marissa Wall947d34e2019-03-29 14:03:53 -0700239 mCurrentState.clientCacheId = clientCacheId;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800240 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700241 setTransactionFlags(eTransactionNeeded);
Ady Abraham09bd3922019-04-08 10:44:56 -0700242
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800243 const int32_t layerId = getSequence();
244 mFlinger->mTimeStats->setPostTime(layerId, mFrameNumber, getName().c_str(), postTime);
245 mFlinger->mFrameTracer->traceNewLayer(layerId, getName().c_str());
246 mFlinger->mFrameTracer->traceTimestamp(layerId, buffer->getId(), mFrameNumber, postTime,
Mikael Pessa90092f42019-08-26 17:22:04 -0700247 FrameTracer::FrameEvent::POST);
chaviwfa67b552019-08-12 16:51:55 -0700248 mCurrentState.desiredPresentTime = desiredPresentTime;
Ady Abraham09bd3922019-04-08 10:44:56 -0700249
250 if (mFlinger->mUseSmart90ForVideo) {
chaviwfa67b552019-08-12 16:51:55 -0700251 const nsecs_t presentTime = (desiredPresentTime == -1) ? 0 : desiredPresentTime;
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -0700252 mFlinger->mScheduler->recordLayerHistory(this, presentTime,
253 mCurrentState.hdrMetadata.validTypes != 0);
Ady Abraham09bd3922019-04-08 10:44:56 -0700254 }
255
Marissa Wall61c58622018-07-18 10:12:20 -0700256 return true;
257}
258
259bool BufferStateLayer::setAcquireFence(const sp<Fence>& fence) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700260 // The acquire fences of BufferStateLayers have already signaled before they are set
261 mCallbackHandleAcquireTime = fence->getSignalTime();
262
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800263 mCurrentState.acquireFence = fence;
264 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700265 setTransactionFlags(eTransactionNeeded);
266 return true;
267}
268
269bool BufferStateLayer::setDataspace(ui::Dataspace dataspace) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800270 if (mCurrentState.dataspace == dataspace) return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800271 mCurrentState.dataspace = dataspace;
272 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700273 setTransactionFlags(eTransactionNeeded);
274 return true;
275}
276
277bool BufferStateLayer::setHdrMetadata(const HdrMetadata& hdrMetadata) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800278 if (mCurrentState.hdrMetadata == hdrMetadata) return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800279 mCurrentState.hdrMetadata = hdrMetadata;
280 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700281 setTransactionFlags(eTransactionNeeded);
282 return true;
283}
284
285bool BufferStateLayer::setSurfaceDamageRegion(const Region& surfaceDamage) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800286 mCurrentState.surfaceDamageRegion = surfaceDamage;
287 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700288 setTransactionFlags(eTransactionNeeded);
289 return true;
290}
291
292bool BufferStateLayer::setApi(int32_t api) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800293 if (mCurrentState.api == api) return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800294 mCurrentState.api = api;
295 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700296 setTransactionFlags(eTransactionNeeded);
297 return true;
298}
299
300bool BufferStateLayer::setSidebandStream(const sp<NativeHandle>& sidebandStream) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800301 if (mCurrentState.sidebandStream == sidebandStream) return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800302 mCurrentState.sidebandStream = sidebandStream;
303 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700304 setTransactionFlags(eTransactionNeeded);
305
306 if (!mSidebandStreamChanged.exchange(true)) {
307 // mSidebandStreamChanged was false
308 mFlinger->signalLayerUpdate();
309 }
310 return true;
311}
312
Marissa Walle2ffb422018-10-12 11:33:52 -0700313bool BufferStateLayer::setTransactionCompletedListeners(
314 const std::vector<sp<CallbackHandle>>& handles) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700315 // If there is no handle, we will not send a callback so reset mReleasePreviousBuffer and return
Marissa Walle2ffb422018-10-12 11:33:52 -0700316 if (handles.empty()) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700317 mReleasePreviousBuffer = false;
Marissa Walle2ffb422018-10-12 11:33:52 -0700318 return false;
319 }
320
321 const bool willPresent = willPresentCurrentTransaction();
322
323 for (const auto& handle : handles) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700324 // If this transaction set a buffer on this layer, release its previous buffer
325 handle->releasePreviousBuffer = mReleasePreviousBuffer;
326
Marissa Walle2ffb422018-10-12 11:33:52 -0700327 // If this layer will be presented in this frame
328 if (willPresent) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700329 // If this transaction set an acquire fence on this layer, set its acquire time
330 handle->acquireTime = mCallbackHandleAcquireTime;
331
Marissa Walle2ffb422018-10-12 11:33:52 -0700332 // Notify the transaction completed thread that there is a pending latched callback
333 // handle
Marissa Wall5a68a772018-12-22 17:43:42 -0800334 mFlinger->getTransactionCompletedThread().registerPendingCallbackHandle(handle);
Marissa Walle2ffb422018-10-12 11:33:52 -0700335
336 // Store so latched time and release fence can be set
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800337 mCurrentState.callbackHandles.push_back(handle);
Marissa Walle2ffb422018-10-12 11:33:52 -0700338
339 } else { // If this layer will NOT need to be relatched and presented this frame
340 // Notify the transaction completed thread this handle is done
Marissa Wallefb71af2019-06-27 14:45:53 -0700341 mFlinger->getTransactionCompletedThread().registerUnpresentedCallbackHandle(handle);
Marissa Walle2ffb422018-10-12 11:33:52 -0700342 }
343 }
344
Marissa Wallfda30bb2018-10-12 11:34:28 -0700345 mReleasePreviousBuffer = false;
346 mCallbackHandleAcquireTime = -1;
347
Marissa Walle2ffb422018-10-12 11:33:52 -0700348 return willPresent;
349}
350
Marissa Wall61c58622018-07-18 10:12:20 -0700351bool BufferStateLayer::setTransparentRegionHint(const Region& transparent) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800352 mCurrentState.transparentRegionHint = transparent;
353 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700354 setTransactionFlags(eTransactionNeeded);
355 return true;
356}
357
Marissa Wall861616d2018-10-22 12:52:23 -0700358Rect BufferStateLayer::getBufferSize(const State& s) const {
359 // for buffer state layers we use the display frame size as the buffer size.
360 if (getActiveWidth(s) < UINT32_MAX && getActiveHeight(s) < UINT32_MAX) {
361 return Rect(getActiveWidth(s), getActiveHeight(s));
Marissa Wall61c58622018-07-18 10:12:20 -0700362 }
363
Marissa Wall861616d2018-10-22 12:52:23 -0700364 // if the display frame is not defined, use the parent bounds as the buffer size.
365 const auto& p = mDrawingParent.promote();
366 if (p != nullptr) {
Vishnu Nair4351ad52019-02-11 14:13:02 -0800367 Rect parentBounds = Rect(p->getBounds(Region()));
Marissa Wall861616d2018-10-22 12:52:23 -0700368 if (!parentBounds.isEmpty()) {
369 return parentBounds;
370 }
371 }
372
Marissa Wall861616d2018-10-22 12:52:23 -0700373 return Rect::INVALID_RECT;
Marissa Wall61c58622018-07-18 10:12:20 -0700374}
Vishnu Nair4351ad52019-02-11 14:13:02 -0800375
376FloatRect BufferStateLayer::computeSourceBounds(const FloatRect& parentBounds) const {
377 const State& s(getDrawingState());
378 // for buffer state layers we use the display frame size as the buffer size.
379 if (getActiveWidth(s) < UINT32_MAX && getActiveHeight(s) < UINT32_MAX) {
380 return FloatRect(0, 0, getActiveWidth(s), getActiveHeight(s));
381 }
382
383 // if the display frame is not defined, use the parent bounds as the buffer size.
384 return parentBounds;
385}
386
Marissa Wall61c58622018-07-18 10:12:20 -0700387// -----------------------------------------------------------------------
388
389// -----------------------------------------------------------------------
390// Interface implementation for BufferLayer
391// -----------------------------------------------------------------------
392bool BufferStateLayer::fenceHasSignaled() const {
393 if (latchUnsignaledBuffers()) {
394 return true;
395 }
396
397 return getDrawingState().acquireFence->getStatus() == Fence::Status::Signaled;
398}
399
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700400bool BufferStateLayer::framePresentTimeIsCurrent(nsecs_t expectedPresentTime) const {
Ady Abrahamcd1580c2019-04-29 15:40:03 -0700401 if (!hasFrameUpdate() || isRemovedFromCurrentState()) {
402 return true;
403 }
404
chaviwfa67b552019-08-12 16:51:55 -0700405 return mCurrentState.desiredPresentTime <= expectedPresentTime;
Ady Abrahamcd1580c2019-04-29 15:40:03 -0700406}
407
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700408uint64_t BufferStateLayer::getFrameNumber(nsecs_t /*expectedPresentTime*/) const {
Marissa Wall61c58622018-07-18 10:12:20 -0700409 return mFrameNumber;
410}
411
412bool BufferStateLayer::getAutoRefresh() const {
413 // TODO(marissaw): support shared buffer mode
414 return false;
415}
416
417bool BufferStateLayer::getSidebandStreamChanged() const {
418 return mSidebandStreamChanged.load();
419}
420
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800421bool BufferStateLayer::latchSidebandStream(bool& recomputeVisibleRegions) {
Marissa Wall61c58622018-07-18 10:12:20 -0700422 if (mSidebandStreamChanged.exchange(false)) {
423 const State& s(getDrawingState());
424 // mSidebandStreamChanged was true
Lloyd Pique0b785d82018-12-04 17:25:27 -0800425 LOG_ALWAYS_FATAL_IF(!getCompositionLayer());
426 mSidebandStream = s.sidebandStream;
Lloyd Pique9755fb72019-03-26 14:44:40 -0700427 getCompositionLayer()->editFEState().sidebandStream = mSidebandStream;
Lloyd Pique0b785d82018-12-04 17:25:27 -0800428 if (mSidebandStream != nullptr) {
Marissa Wall61c58622018-07-18 10:12:20 -0700429 setTransactionFlags(eTransactionNeeded);
430 mFlinger->setTransactionFlags(eTraversalNeeded);
431 }
432 recomputeVisibleRegions = true;
433
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800434 return true;
Marissa Wall61c58622018-07-18 10:12:20 -0700435 }
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800436 return false;
Marissa Wall61c58622018-07-18 10:12:20 -0700437}
438
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800439bool BufferStateLayer::hasFrameUpdate() const {
Valerie Hauaa194562019-02-05 16:21:38 -0800440 const State& c(getCurrentState());
441 return mCurrentStateModified && (c.buffer != nullptr || c.bgColorLayer != nullptr);
Marissa Wall61c58622018-07-18 10:12:20 -0700442}
443
Alec Mouri39801c02018-10-10 10:44:47 -0700444status_t BufferStateLayer::bindTextureImage() {
Marissa Wall61c58622018-07-18 10:12:20 -0700445 const State& s(getDrawingState());
446 auto& engine(mFlinger->getRenderEngine());
447
Alec Mourib5c4f352019-02-19 19:46:38 -0800448 return engine.bindExternalTextureBuffer(mTextureName, s.buffer, s.acquireFence);
Marissa Wall61c58622018-07-18 10:12:20 -0700449}
450
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700451status_t BufferStateLayer::updateTexImage(bool& /*recomputeVisibleRegions*/, nsecs_t latchTime,
452 nsecs_t /*expectedPresentTime*/) {
Marissa Wall61c58622018-07-18 10:12:20 -0700453 const State& s(getDrawingState());
454
455 if (!s.buffer) {
Valerie Hauaa194562019-02-05 16:21:38 -0800456 if (s.bgColorLayer) {
457 for (auto& handle : mDrawingState.callbackHandles) {
458 handle->latchTime = latchTime;
459 }
460 }
Marissa Wall61c58622018-07-18 10:12:20 -0700461 return NO_ERROR;
462 }
463
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800464 const int32_t layerId = getSequence();
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700465
Marissa Wall61c58622018-07-18 10:12:20 -0700466 // Reject if the layer is invalid
467 uint32_t bufferWidth = s.buffer->width;
468 uint32_t bufferHeight = s.buffer->height;
469
Peiyong Linefefaac2018-08-17 12:27:51 -0700470 if (s.transform & ui::Transform::ROT_90) {
Peiyong Lin3db42342018-08-16 09:15:59 -0700471 std::swap(bufferWidth, bufferHeight);
Marissa Wall61c58622018-07-18 10:12:20 -0700472 }
473
474 if (s.transformToDisplayInverse) {
475 uint32_t invTransform = DisplayDevice::getPrimaryDisplayOrientationTransform();
Peiyong Linefefaac2018-08-17 12:27:51 -0700476 if (invTransform & 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
Vishnu Nair60356342018-11-13 13:00:45 -0800481 if (getEffectiveScalingMode() == NATIVE_WINDOW_SCALING_MODE_FREEZE &&
Marissa Wall61c58622018-07-18 10:12:20 -0700482 (s.active.w != bufferWidth || s.active.h != bufferHeight)) {
483 ALOGE("[%s] rejecting buffer: "
484 "bufferWidth=%d, bufferHeight=%d, front.active.{w=%d, h=%d}",
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700485 getDebugName(), bufferWidth, bufferHeight, s.active.w, s.active.h);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800486 mFlinger->mTimeStats->removeTimeRecord(layerId, mFrameNumber);
Marissa Wall61c58622018-07-18 10:12:20 -0700487 return BAD_VALUE;
488 }
489
Marissa Wall5a68a772018-12-22 17:43:42 -0800490 for (auto& handle : mDrawingState.callbackHandles) {
491 handle->latchTime = latchTime;
492 }
Marissa Walle2ffb422018-10-12 11:33:52 -0700493
Alec Mouri56e538f2019-01-14 15:22:01 -0800494 if (!SyncFeatures::getInstance().useNativeFenceSync()) {
Marissa Wall61c58622018-07-18 10:12:20 -0700495 // Bind the new buffer to the GL texture.
496 //
497 // Older devices require the "implicit" synchronization provided
498 // by glEGLImageTargetTexture2DOES, which this method calls. Newer
499 // devices will either call this in Layer::onDraw, or (if it's not
500 // a GL-composited layer) not at all.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800501 status_t err = bindTextureImage();
Marissa Wall61c58622018-07-18 10:12:20 -0700502 if (err != NO_ERROR) {
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800503 mFlinger->mTimeStats->onDestroy(layerId);
504 mFlinger->mFrameTracer->onDestroy(layerId);
Marissa Wall61c58622018-07-18 10:12:20 -0700505 return BAD_VALUE;
506 }
507 }
508
Mikael Pessa2e1608f2019-07-19 11:25:35 -0700509 const uint64_t bufferID = getCurrentBufferId();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800510 mFlinger->mTimeStats->setAcquireFence(layerId, mFrameNumber, mBufferInfo.mFenceTime);
511 mFlinger->mFrameTracer->traceFence(layerId, bufferID, mFrameNumber, mBufferInfo.mFenceTime,
Mikael Pessa90092f42019-08-26 17:22:04 -0700512 FrameTracer::FrameEvent::ACQUIRE_FENCE);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800513 mFlinger->mTimeStats->setLatchTime(layerId, mFrameNumber, latchTime);
514 mFlinger->mFrameTracer->traceTimestamp(layerId, bufferID, mFrameNumber, latchTime,
Mikael Pessa90092f42019-08-26 17:22:04 -0700515 FrameTracer::FrameEvent::LATCH);
Marissa Wall61c58622018-07-18 10:12:20 -0700516
Marissa Wall16c112d2019-03-20 13:21:13 -0700517 mCurrentStateModified = false;
518
Marissa Wall61c58622018-07-18 10:12:20 -0700519 return NO_ERROR;
520}
521
522status_t BufferStateLayer::updateActiveBuffer() {
523 const State& s(getDrawingState());
524
525 if (s.buffer == nullptr) {
526 return BAD_VALUE;
527 }
528
Mikael Pessa2e1608f2019-07-19 11:25:35 -0700529 mPreviousBufferId = getCurrentBufferId();
chaviwd62d3062019-09-04 14:48:02 -0700530 mBufferInfo.mBuffer = s.buffer;
531 mBufferInfo.mFence = s.acquireFence;
Lloyd Pique9755fb72019-03-26 14:44:40 -0700532 auto& layerCompositionState = getCompositionLayer()->editFEState();
chaviwd62d3062019-09-04 14:48:02 -0700533 layerCompositionState.buffer = mBufferInfo.mBuffer;
Marissa Wall61c58622018-07-18 10:12:20 -0700534
535 return NO_ERROR;
536}
537
538status_t BufferStateLayer::updateFrameNumber(nsecs_t /*latchTime*/) {
539 // TODO(marissaw): support frame history events
Mikael Pessa2e1608f2019-07-19 11:25:35 -0700540 mPreviousFrameNumber = mCurrentFrameNumber;
Marissa Wall61c58622018-07-18 10:12:20 -0700541 mCurrentFrameNumber = mFrameNumber;
542 return NO_ERROR;
543}
544
Lloyd Piquef5275482019-01-29 18:42:42 -0800545void BufferStateLayer::latchPerFrameState(
546 compositionengine::LayerFECompositionState& compositionState) const {
547 BufferLayer::latchPerFrameState(compositionState);
548 if (compositionState.compositionType == Hwc2::IComposerClient::Composition::SIDEBAND) {
549 return;
550 }
Marissa Wall61c58622018-07-18 10:12:20 -0700551
chaviwf83ce182019-09-12 14:43:08 -0700552 compositionState.buffer = mBufferInfo.mBuffer;
553 compositionState.bufferSlot = mBufferInfo.mBufferSlot;
chaviw4244e032019-09-04 11:27:49 -0700554 compositionState.acquireFence = mBufferInfo.mFence;
Marissa Wall61c58622018-07-18 10:12:20 -0700555
556 mFrameNumber++;
557}
558
Marissa Wall947d34e2019-03-29 14:03:53 -0700559void BufferStateLayer::HwcSlotGenerator::bufferErased(const client_cache_t& clientCacheId) {
560 std::lock_guard lock(mMutex);
561 if (!clientCacheId.isValid()) {
562 ALOGE("invalid process, failed to erase buffer");
563 return;
564 }
565 eraseBufferLocked(clientCacheId);
566}
567
568uint32_t BufferStateLayer::HwcSlotGenerator::getHwcCacheSlot(const client_cache_t& clientCacheId) {
569 std::lock_guard<std::mutex> lock(mMutex);
570 auto itr = mCachedBuffers.find(clientCacheId);
571 if (itr == mCachedBuffers.end()) {
572 return addCachedBuffer(clientCacheId);
573 }
574 auto& [hwcCacheSlot, counter] = itr->second;
575 counter = mCounter++;
576 return hwcCacheSlot;
577}
578
579uint32_t BufferStateLayer::HwcSlotGenerator::addCachedBuffer(const client_cache_t& clientCacheId)
580 REQUIRES(mMutex) {
581 if (!clientCacheId.isValid()) {
582 ALOGE("invalid process, returning invalid slot");
583 return BufferQueue::INVALID_BUFFER_SLOT;
584 }
585
586 ClientCache::getInstance().registerErasedRecipient(clientCacheId, wp<ErasedRecipient>(this));
587
588 uint32_t hwcCacheSlot = getFreeHwcCacheSlot();
589 mCachedBuffers[clientCacheId] = {hwcCacheSlot, mCounter++};
590 return hwcCacheSlot;
591}
592
593uint32_t BufferStateLayer::HwcSlotGenerator::getFreeHwcCacheSlot() REQUIRES(mMutex) {
594 if (mFreeHwcCacheSlots.empty()) {
595 evictLeastRecentlyUsed();
596 }
597
598 uint32_t hwcCacheSlot = mFreeHwcCacheSlots.top();
599 mFreeHwcCacheSlots.pop();
600 return hwcCacheSlot;
601}
602
603void BufferStateLayer::HwcSlotGenerator::evictLeastRecentlyUsed() REQUIRES(mMutex) {
604 uint64_t minCounter = UINT_MAX;
605 client_cache_t minClientCacheId = {};
606 for (const auto& [clientCacheId, slotCounter] : mCachedBuffers) {
607 const auto& [hwcCacheSlot, counter] = slotCounter;
608 if (counter < minCounter) {
609 minCounter = counter;
610 minClientCacheId = clientCacheId;
611 }
612 }
613 eraseBufferLocked(minClientCacheId);
614
615 ClientCache::getInstance().unregisterErasedRecipient(minClientCacheId, this);
616}
617
618void BufferStateLayer::HwcSlotGenerator::eraseBufferLocked(const client_cache_t& clientCacheId)
619 REQUIRES(mMutex) {
620 auto itr = mCachedBuffers.find(clientCacheId);
621 if (itr == mCachedBuffers.end()) {
622 return;
623 }
624 auto& [hwcCacheSlot, counter] = itr->second;
625
626 // TODO send to hwc cache and resources
627
628 mFreeHwcCacheSlots.push(hwcCacheSlot);
629 mCachedBuffers.erase(clientCacheId);
630}
chaviw4244e032019-09-04 11:27:49 -0700631
632void BufferStateLayer::gatherBufferInfo() {
633 const State& s(getDrawingState());
634
635 mBufferInfo.mDesiredPresentTime = s.desiredPresentTime;
636 mBufferInfo.mFenceTime = std::make_shared<FenceTime>(s.acquireFence);
637 mBufferInfo.mFence = s.acquireFence;
chaviw4244e032019-09-04 11:27:49 -0700638 mBufferInfo.mTransform = s.transform;
639 mBufferInfo.mDataspace = translateDataspace(s.dataspace);
640 mBufferInfo.mCrop = computeCrop(s);
641 mBufferInfo.mScaleMode = NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW;
642 mBufferInfo.mSurfaceDamage = s.surfaceDamageRegion;
643 mBufferInfo.mHdrMetadata = s.hdrMetadata;
644 mBufferInfo.mApi = s.api;
chaviwd62d3062019-09-04 14:48:02 -0700645 mBufferInfo.mPixelFormat =
646 !mBufferInfo.mBuffer ? PIXEL_FORMAT_NONE : mBufferInfo.mBuffer->format;
chaviw4244e032019-09-04 11:27:49 -0700647 mBufferInfo.mTransformToDisplayInverse = s.transformToDisplayInverse;
chaviwf83ce182019-09-12 14:43:08 -0700648 mBufferInfo.mBufferSlot = mHwcSlotGenerator->getHwcCacheSlot(s.clientCacheId);
chaviw4244e032019-09-04 11:27:49 -0700649}
650
651Rect BufferStateLayer::computeCrop(const State& s) {
652 if (s.crop.isEmpty() && s.buffer) {
653 return s.buffer->getBounds();
654 } else if (s.buffer) {
655 Rect crop = s.crop;
656 crop.left = std::max(crop.left, 0);
657 crop.top = std::max(crop.top, 0);
658 uint32_t bufferWidth = s.buffer->getWidth();
659 uint32_t bufferHeight = s.buffer->getHeight();
660 if (bufferHeight <= std::numeric_limits<int32_t>::max() &&
661 bufferWidth <= std::numeric_limits<int32_t>::max()) {
662 crop.right = std::min(crop.right, static_cast<int32_t>(bufferWidth));
663 crop.bottom = std::min(crop.bottom, static_cast<int32_t>(bufferHeight));
664 }
665 if (!crop.isValid()) {
666 // Crop rect is out of bounds, return whole buffer
667 return s.buffer->getBounds();
668 }
669 return crop;
670 }
671 return s.crop;
672}
673
chaviwb4c6e582019-08-16 14:35:07 -0700674sp<Layer> BufferStateLayer::createClone() {
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700675 LayerCreationArgs args(mFlinger.get(), nullptr, mName + " (Mirror)", 0, 0, 0, LayerMetadata());
chaviwb4c6e582019-08-16 14:35:07 -0700676 args.textureName = mTextureName;
Lloyd Pique1c3a5eb2019-10-03 13:07:08 -0700677 sp<BufferStateLayer> layer = mFlinger->getFactory().createBufferStateLayer(args);
chaviwb4c6e582019-08-16 14:35:07 -0700678 layer->mHwcSlotGenerator = mHwcSlotGenerator;
679 layer->setInitialValuesForClone(this);
680 return layer;
681}
Marissa Wall61c58622018-07-18 10:12:20 -0700682} // namespace android