blob: 75fc0e9c06c7d5d91f3ef788ac6727960566af79 [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
106void BufferStateLayer::setTransformHint(uint32_t /*orientation*/) const {
107 // TODO(marissaw): send the transform hint to buffer owner
108 return;
109}
110
111void BufferStateLayer::releasePendingBuffer(nsecs_t /*dequeueReadyTime*/) {
Marissa Wallefb71af2019-06-27 14:45:53 -0700112 mFlinger->getTransactionCompletedThread().finalizePendingCallbackHandles(
Marissa Wall5a68a772018-12-22 17:43:42 -0800113 mDrawingState.callbackHandles);
114
115 mDrawingState.callbackHandles = {};
Marissa Wall61c58622018-07-18 10:12:20 -0700116}
117
Ana Krulec010d2192018-10-08 06:29:54 -0700118bool BufferStateLayer::shouldPresentNow(nsecs_t /*expectedPresentTime*/) const {
Marissa Wall61c58622018-07-18 10:12:20 -0700119 if (getSidebandStreamChanged() || getAutoRefresh()) {
120 return true;
121 }
122
Marissa Wall024a1912018-08-13 13:55:35 -0700123 return hasFrameUpdate();
Marissa Wall61c58622018-07-18 10:12:20 -0700124}
125
Marissa Walle2ffb422018-10-12 11:33:52 -0700126bool BufferStateLayer::willPresentCurrentTransaction() const {
127 // Returns true if the most recent Transaction applied to CurrentState will be presented.
Robert Carr321e83c2019-08-19 15:49:30 -0700128 return (getSidebandStreamChanged() || getAutoRefresh() ||
Valerie Hauaa194562019-02-05 16:21:38 -0800129 (mCurrentState.modified &&
Robert Carr321e83c2019-08-19 15:49:30 -0700130 (mCurrentState.buffer != nullptr || mCurrentState.bgColorLayer != nullptr))) &&
131 !mLayerDetached;
Marissa Wall61c58622018-07-18 10:12:20 -0700132}
133
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800134void BufferStateLayer::pushPendingState() {
135 if (!mCurrentState.modified) {
Marissa Wall61c58622018-07-18 10:12:20 -0700136 return;
137 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800138 mPendingStates.push_back(mCurrentState);
139 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Marissa Wall61c58622018-07-18 10:12:20 -0700140}
141
142bool BufferStateLayer::applyPendingStates(Layer::State* stateToCommit) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800143 const bool stateUpdateAvailable = !mPendingStates.empty();
144 while (!mPendingStates.empty()) {
Marissa Wall61c58622018-07-18 10:12:20 -0700145 popPendingState(stateToCommit);
146 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800147 mCurrentStateModified = stateUpdateAvailable && mCurrentState.modified;
148 mCurrentState.modified = false;
Marissa Wall61c58622018-07-18 10:12:20 -0700149 return stateUpdateAvailable;
150}
151
Marissa Wall861616d2018-10-22 12:52:23 -0700152// Crop that applies to the window
153Rect BufferStateLayer::getCrop(const Layer::State& /*s*/) const {
154 return Rect::INVALID_RECT;
Marissa Wall61c58622018-07-18 10:12:20 -0700155}
156
157bool BufferStateLayer::setTransform(uint32_t transform) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800158 if (mCurrentState.transform == transform) return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800159 mCurrentState.transform = transform;
160 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700161 setTransactionFlags(eTransactionNeeded);
162 return true;
163}
164
165bool BufferStateLayer::setTransformToDisplayInverse(bool transformToDisplayInverse) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800166 if (mCurrentState.transformToDisplayInverse == transformToDisplayInverse) return false;
167 mCurrentState.sequence++;
168 mCurrentState.transformToDisplayInverse = transformToDisplayInverse;
169 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700170 setTransactionFlags(eTransactionNeeded);
171 return true;
172}
173
174bool BufferStateLayer::setCrop(const Rect& crop) {
Marissa Wall290ad082019-03-06 13:23:47 -0800175 Rect c = crop;
176 if (c.left < 0) {
177 c.left = 0;
178 }
179 if (c.top < 0) {
180 c.top = 0;
181 }
182 // If the width and/or height are < 0, make it [0, 0, -1, -1] so the equality comparision below
183 // treats all invalid rectangles the same.
184 if (!c.isValid()) {
185 c.makeInvalid();
186 }
187
188 if (mCurrentState.crop == c) return false;
Marissa Wall290ad082019-03-06 13:23:47 -0800189 mCurrentState.crop = c;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800190 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700191 setTransactionFlags(eTransactionNeeded);
192 return true;
193}
194
Marissa Wall861616d2018-10-22 12:52:23 -0700195bool BufferStateLayer::setFrame(const Rect& frame) {
196 int x = frame.left;
197 int y = frame.top;
198 int w = frame.getWidth();
199 int h = frame.getHeight();
200
Marissa Wall0f3242d2018-12-20 15:10:22 -0800201 if (x < 0) {
202 x = 0;
203 w = frame.right;
204 }
205
206 if (y < 0) {
207 y = 0;
208 h = frame.bottom;
209 }
210
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800211 if (mCurrentState.active.transform.tx() == x && mCurrentState.active.transform.ty() == y &&
212 mCurrentState.active.w == w && mCurrentState.active.h == h) {
Marissa Wall861616d2018-10-22 12:52:23 -0700213 return false;
214 }
215
216 if (!frame.isValid()) {
217 x = y = w = h = 0;
218 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800219 mCurrentState.active.transform.set(x, y);
220 mCurrentState.active.w = w;
221 mCurrentState.active.h = h;
Marissa Wall861616d2018-10-22 12:52:23 -0700222
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800223 mCurrentState.sequence++;
224 mCurrentState.modified = true;
Marissa Wall861616d2018-10-22 12:52:23 -0700225 setTransactionFlags(eTransactionNeeded);
226 return true;
227}
228
Ady Abraham09bd3922019-04-08 10:44:56 -0700229bool BufferStateLayer::setBuffer(const sp<GraphicBuffer>& buffer, nsecs_t postTime,
Marissa Wall947d34e2019-03-29 14:03:53 -0700230 nsecs_t desiredPresentTime, const client_cache_t& clientCacheId) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800231 if (mCurrentState.buffer) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700232 mReleasePreviousBuffer = true;
233 }
234
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800235 mCurrentState.buffer = buffer;
Marissa Wall947d34e2019-03-29 14:03:53 -0700236 mCurrentState.clientCacheId = clientCacheId;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800237 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700238 setTransactionFlags(eTransactionNeeded);
Ady Abraham09bd3922019-04-08 10:44:56 -0700239
Mikael Pessa2e1608f2019-07-19 11:25:35 -0700240 const int32_t layerID = getSequence();
241 mFlinger->mTimeStats->setPostTime(layerID, mFrameNumber, getName().c_str(), postTime);
Mikael Pessa90092f42019-08-26 17:22:04 -0700242 mFlinger->mFrameTracer->traceNewLayer(layerID, getName().c_str());
243 mFlinger->mFrameTracer->traceTimestamp(layerID, buffer->getId(), mFrameNumber, postTime,
244 FrameTracer::FrameEvent::POST);
chaviwfa67b552019-08-12 16:51:55 -0700245 mCurrentState.desiredPresentTime = desiredPresentTime;
Ady Abraham09bd3922019-04-08 10:44:56 -0700246
247 if (mFlinger->mUseSmart90ForVideo) {
chaviwfa67b552019-08-12 16:51:55 -0700248 const nsecs_t presentTime = (desiredPresentTime == -1) ? 0 : desiredPresentTime;
Ady Abrahama315ce72019-04-24 14:35:20 -0700249 mFlinger->mScheduler->addLayerPresentTimeAndHDR(mSchedulerLayerHandle, presentTime,
250 mCurrentState.hdrMetadata.validTypes != 0);
Ady Abraham09bd3922019-04-08 10:44:56 -0700251 }
252
Marissa Wall61c58622018-07-18 10:12:20 -0700253 return true;
254}
255
256bool BufferStateLayer::setAcquireFence(const sp<Fence>& fence) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700257 // The acquire fences of BufferStateLayers have already signaled before they are set
258 mCallbackHandleAcquireTime = fence->getSignalTime();
259
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800260 mCurrentState.acquireFence = fence;
261 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700262 setTransactionFlags(eTransactionNeeded);
263 return true;
264}
265
266bool BufferStateLayer::setDataspace(ui::Dataspace dataspace) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800267 if (mCurrentState.dataspace == dataspace) return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800268 mCurrentState.dataspace = dataspace;
269 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700270 setTransactionFlags(eTransactionNeeded);
271 return true;
272}
273
274bool BufferStateLayer::setHdrMetadata(const HdrMetadata& hdrMetadata) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800275 if (mCurrentState.hdrMetadata == hdrMetadata) return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800276 mCurrentState.hdrMetadata = hdrMetadata;
277 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700278 setTransactionFlags(eTransactionNeeded);
279 return true;
280}
281
282bool BufferStateLayer::setSurfaceDamageRegion(const Region& surfaceDamage) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800283 mCurrentState.surfaceDamageRegion = surfaceDamage;
284 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700285 setTransactionFlags(eTransactionNeeded);
286 return true;
287}
288
289bool BufferStateLayer::setApi(int32_t api) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800290 if (mCurrentState.api == api) return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800291 mCurrentState.api = api;
292 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700293 setTransactionFlags(eTransactionNeeded);
294 return true;
295}
296
297bool BufferStateLayer::setSidebandStream(const sp<NativeHandle>& sidebandStream) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800298 if (mCurrentState.sidebandStream == sidebandStream) return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800299 mCurrentState.sidebandStream = sidebandStream;
300 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700301 setTransactionFlags(eTransactionNeeded);
302
303 if (!mSidebandStreamChanged.exchange(true)) {
304 // mSidebandStreamChanged was false
305 mFlinger->signalLayerUpdate();
306 }
307 return true;
308}
309
Marissa Walle2ffb422018-10-12 11:33:52 -0700310bool BufferStateLayer::setTransactionCompletedListeners(
311 const std::vector<sp<CallbackHandle>>& handles) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700312 // If there is no handle, we will not send a callback so reset mReleasePreviousBuffer and return
Marissa Walle2ffb422018-10-12 11:33:52 -0700313 if (handles.empty()) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700314 mReleasePreviousBuffer = false;
Marissa Walle2ffb422018-10-12 11:33:52 -0700315 return false;
316 }
317
318 const bool willPresent = willPresentCurrentTransaction();
319
320 for (const auto& handle : handles) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700321 // If this transaction set a buffer on this layer, release its previous buffer
322 handle->releasePreviousBuffer = mReleasePreviousBuffer;
323
Marissa Walle2ffb422018-10-12 11:33:52 -0700324 // If this layer will be presented in this frame
325 if (willPresent) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700326 // If this transaction set an acquire fence on this layer, set its acquire time
327 handle->acquireTime = mCallbackHandleAcquireTime;
328
Marissa Walle2ffb422018-10-12 11:33:52 -0700329 // Notify the transaction completed thread that there is a pending latched callback
330 // handle
Marissa Wall5a68a772018-12-22 17:43:42 -0800331 mFlinger->getTransactionCompletedThread().registerPendingCallbackHandle(handle);
Marissa Walle2ffb422018-10-12 11:33:52 -0700332
333 // Store so latched time and release fence can be set
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800334 mCurrentState.callbackHandles.push_back(handle);
Marissa Walle2ffb422018-10-12 11:33:52 -0700335
336 } else { // If this layer will NOT need to be relatched and presented this frame
337 // Notify the transaction completed thread this handle is done
Marissa Wallefb71af2019-06-27 14:45:53 -0700338 mFlinger->getTransactionCompletedThread().registerUnpresentedCallbackHandle(handle);
Marissa Walle2ffb422018-10-12 11:33:52 -0700339 }
340 }
341
Marissa Wallfda30bb2018-10-12 11:34:28 -0700342 mReleasePreviousBuffer = false;
343 mCallbackHandleAcquireTime = -1;
344
Marissa Walle2ffb422018-10-12 11:33:52 -0700345 return willPresent;
346}
347
Marissa Wall61c58622018-07-18 10:12:20 -0700348bool BufferStateLayer::setTransparentRegionHint(const Region& transparent) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800349 mCurrentState.transparentRegionHint = transparent;
350 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700351 setTransactionFlags(eTransactionNeeded);
352 return true;
353}
354
Marissa Wall861616d2018-10-22 12:52:23 -0700355Rect BufferStateLayer::getBufferSize(const State& s) const {
356 // for buffer state layers we use the display frame size as the buffer size.
357 if (getActiveWidth(s) < UINT32_MAX && getActiveHeight(s) < UINT32_MAX) {
358 return Rect(getActiveWidth(s), getActiveHeight(s));
Marissa Wall61c58622018-07-18 10:12:20 -0700359 }
360
Marissa Wall861616d2018-10-22 12:52:23 -0700361 // if the display frame is not defined, use the parent bounds as the buffer size.
362 const auto& p = mDrawingParent.promote();
363 if (p != nullptr) {
Vishnu Nair4351ad52019-02-11 14:13:02 -0800364 Rect parentBounds = Rect(p->getBounds(Region()));
Marissa Wall861616d2018-10-22 12:52:23 -0700365 if (!parentBounds.isEmpty()) {
366 return parentBounds;
367 }
368 }
369
Marissa Wall861616d2018-10-22 12:52:23 -0700370 return Rect::INVALID_RECT;
Marissa Wall61c58622018-07-18 10:12:20 -0700371}
Vishnu Nair4351ad52019-02-11 14:13:02 -0800372
373FloatRect BufferStateLayer::computeSourceBounds(const FloatRect& parentBounds) const {
374 const State& s(getDrawingState());
375 // for buffer state layers we use the display frame size as the buffer size.
376 if (getActiveWidth(s) < UINT32_MAX && getActiveHeight(s) < UINT32_MAX) {
377 return FloatRect(0, 0, getActiveWidth(s), getActiveHeight(s));
378 }
379
380 // if the display frame is not defined, use the parent bounds as the buffer size.
381 return parentBounds;
382}
383
Marissa Wall61c58622018-07-18 10:12:20 -0700384// -----------------------------------------------------------------------
385
386// -----------------------------------------------------------------------
387// Interface implementation for BufferLayer
388// -----------------------------------------------------------------------
389bool BufferStateLayer::fenceHasSignaled() const {
390 if (latchUnsignaledBuffers()) {
391 return true;
392 }
393
394 return getDrawingState().acquireFence->getStatus() == Fence::Status::Signaled;
395}
396
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700397bool BufferStateLayer::framePresentTimeIsCurrent(nsecs_t expectedPresentTime) const {
Ady Abrahamcd1580c2019-04-29 15:40:03 -0700398 if (!hasFrameUpdate() || isRemovedFromCurrentState()) {
399 return true;
400 }
401
chaviwfa67b552019-08-12 16:51:55 -0700402 return mCurrentState.desiredPresentTime <= expectedPresentTime;
Ady Abrahamcd1580c2019-04-29 15:40:03 -0700403}
404
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700405uint64_t BufferStateLayer::getFrameNumber(nsecs_t /*expectedPresentTime*/) const {
Marissa Wall61c58622018-07-18 10:12:20 -0700406 return mFrameNumber;
407}
408
409bool BufferStateLayer::getAutoRefresh() const {
410 // TODO(marissaw): support shared buffer mode
411 return false;
412}
413
414bool BufferStateLayer::getSidebandStreamChanged() const {
415 return mSidebandStreamChanged.load();
416}
417
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800418bool BufferStateLayer::latchSidebandStream(bool& recomputeVisibleRegions) {
Marissa Wall61c58622018-07-18 10:12:20 -0700419 if (mSidebandStreamChanged.exchange(false)) {
420 const State& s(getDrawingState());
421 // mSidebandStreamChanged was true
Lloyd Pique0b785d82018-12-04 17:25:27 -0800422 LOG_ALWAYS_FATAL_IF(!getCompositionLayer());
423 mSidebandStream = s.sidebandStream;
Lloyd Pique9755fb72019-03-26 14:44:40 -0700424 getCompositionLayer()->editFEState().sidebandStream = mSidebandStream;
Lloyd Pique0b785d82018-12-04 17:25:27 -0800425 if (mSidebandStream != nullptr) {
Marissa Wall61c58622018-07-18 10:12:20 -0700426 setTransactionFlags(eTransactionNeeded);
427 mFlinger->setTransactionFlags(eTraversalNeeded);
428 }
429 recomputeVisibleRegions = true;
430
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800431 return true;
Marissa Wall61c58622018-07-18 10:12:20 -0700432 }
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800433 return false;
Marissa Wall61c58622018-07-18 10:12:20 -0700434}
435
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800436bool BufferStateLayer::hasFrameUpdate() const {
Valerie Hauaa194562019-02-05 16:21:38 -0800437 const State& c(getCurrentState());
438 return mCurrentStateModified && (c.buffer != nullptr || c.bgColorLayer != nullptr);
Marissa Wall61c58622018-07-18 10:12:20 -0700439}
440
Alec Mouri39801c02018-10-10 10:44:47 -0700441status_t BufferStateLayer::bindTextureImage() {
Marissa Wall61c58622018-07-18 10:12:20 -0700442 const State& s(getDrawingState());
443 auto& engine(mFlinger->getRenderEngine());
444
Alec Mourib5c4f352019-02-19 19:46:38 -0800445 return engine.bindExternalTextureBuffer(mTextureName, s.buffer, s.acquireFence);
Marissa Wall61c58622018-07-18 10:12:20 -0700446}
447
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700448status_t BufferStateLayer::updateTexImage(bool& /*recomputeVisibleRegions*/, nsecs_t latchTime,
449 nsecs_t /*expectedPresentTime*/) {
Marissa Wall61c58622018-07-18 10:12:20 -0700450 const State& s(getDrawingState());
451
452 if (!s.buffer) {
Valerie Hauaa194562019-02-05 16:21:38 -0800453 if (s.bgColorLayer) {
454 for (auto& handle : mDrawingState.callbackHandles) {
455 handle->latchTime = latchTime;
456 }
457 }
Marissa Wall61c58622018-07-18 10:12:20 -0700458 return NO_ERROR;
459 }
460
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700461 const int32_t layerID = getSequence();
462
Marissa Wall61c58622018-07-18 10:12:20 -0700463 // Reject if the layer is invalid
464 uint32_t bufferWidth = s.buffer->width;
465 uint32_t bufferHeight = s.buffer->height;
466
Peiyong Linefefaac2018-08-17 12:27:51 -0700467 if (s.transform & ui::Transform::ROT_90) {
Peiyong Lin3db42342018-08-16 09:15:59 -0700468 std::swap(bufferWidth, bufferHeight);
Marissa Wall61c58622018-07-18 10:12:20 -0700469 }
470
471 if (s.transformToDisplayInverse) {
472 uint32_t invTransform = DisplayDevice::getPrimaryDisplayOrientationTransform();
Peiyong Linefefaac2018-08-17 12:27:51 -0700473 if (invTransform & ui::Transform::ROT_90) {
Peiyong Lin3db42342018-08-16 09:15:59 -0700474 std::swap(bufferWidth, bufferHeight);
Marissa Wall61c58622018-07-18 10:12:20 -0700475 }
476 }
477
Vishnu Nair60356342018-11-13 13:00:45 -0800478 if (getEffectiveScalingMode() == NATIVE_WINDOW_SCALING_MODE_FREEZE &&
Marissa Wall61c58622018-07-18 10:12:20 -0700479 (s.active.w != bufferWidth || s.active.h != bufferHeight)) {
480 ALOGE("[%s] rejecting buffer: "
481 "bufferWidth=%d, bufferHeight=%d, front.active.{w=%d, h=%d}",
482 mName.string(), bufferWidth, bufferHeight, s.active.w, s.active.h);
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700483 mFlinger->mTimeStats->removeTimeRecord(layerID, mFrameNumber);
Marissa Wall61c58622018-07-18 10:12:20 -0700484 return BAD_VALUE;
485 }
486
Marissa Wall5a68a772018-12-22 17:43:42 -0800487 for (auto& handle : mDrawingState.callbackHandles) {
488 handle->latchTime = latchTime;
489 }
Marissa Walle2ffb422018-10-12 11:33:52 -0700490
Alec Mouri56e538f2019-01-14 15:22:01 -0800491 if (!SyncFeatures::getInstance().useNativeFenceSync()) {
Marissa Wall61c58622018-07-18 10:12:20 -0700492 // Bind the new buffer to the GL texture.
493 //
494 // Older devices require the "implicit" synchronization provided
495 // by glEGLImageTargetTexture2DOES, which this method calls. Newer
496 // devices will either call this in Layer::onDraw, or (if it's not
497 // a GL-composited layer) not at all.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800498 status_t err = bindTextureImage();
Marissa Wall61c58622018-07-18 10:12:20 -0700499 if (err != NO_ERROR) {
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800500 mFlinger->mTimeStats->onDestroy(layerID);
Mikael Pessa90092f42019-08-26 17:22:04 -0700501 mFlinger->mFrameTracer->onDestroy(layerID);
Marissa Wall61c58622018-07-18 10:12:20 -0700502 return BAD_VALUE;
503 }
504 }
505
Mikael Pessa2e1608f2019-07-19 11:25:35 -0700506 const uint64_t bufferID = getCurrentBufferId();
chaviw4244e032019-09-04 11:27:49 -0700507 mFlinger->mTimeStats->setAcquireFence(layerID, mFrameNumber, mBufferInfo.mFenceTime);
508 mFlinger->mFrameTracer->traceFence(layerID, bufferID, mFrameNumber, mBufferInfo.mFenceTime,
Mikael Pessa90092f42019-08-26 17:22:04 -0700509 FrameTracer::FrameEvent::ACQUIRE_FENCE);
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700510 mFlinger->mTimeStats->setLatchTime(layerID, mFrameNumber, latchTime);
Mikael Pessa90092f42019-08-26 17:22:04 -0700511 mFlinger->mFrameTracer->traceTimestamp(layerID, bufferID, mFrameNumber, latchTime,
512 FrameTracer::FrameEvent::LATCH);
Marissa Wall61c58622018-07-18 10:12:20 -0700513
Marissa Wall16c112d2019-03-20 13:21:13 -0700514 mCurrentStateModified = false;
515
Marissa Wall61c58622018-07-18 10:12:20 -0700516 return NO_ERROR;
517}
518
519status_t BufferStateLayer::updateActiveBuffer() {
520 const State& s(getDrawingState());
521
522 if (s.buffer == nullptr) {
523 return BAD_VALUE;
524 }
525
Mikael Pessa2e1608f2019-07-19 11:25:35 -0700526 mPreviousBufferId = getCurrentBufferId();
chaviwd62d3062019-09-04 14:48:02 -0700527 mBufferInfo.mBuffer = s.buffer;
528 mBufferInfo.mFence = s.acquireFence;
Lloyd Pique9755fb72019-03-26 14:44:40 -0700529 auto& layerCompositionState = getCompositionLayer()->editFEState();
chaviwd62d3062019-09-04 14:48:02 -0700530 layerCompositionState.buffer = mBufferInfo.mBuffer;
Marissa Wall61c58622018-07-18 10:12:20 -0700531
532 return NO_ERROR;
533}
534
535status_t BufferStateLayer::updateFrameNumber(nsecs_t /*latchTime*/) {
536 // TODO(marissaw): support frame history events
Mikael Pessa2e1608f2019-07-19 11:25:35 -0700537 mPreviousFrameNumber = mCurrentFrameNumber;
Marissa Wall61c58622018-07-18 10:12:20 -0700538 mCurrentFrameNumber = mFrameNumber;
539 return NO_ERROR;
540}
541
Lloyd Piquef5275482019-01-29 18:42:42 -0800542void BufferStateLayer::latchPerFrameState(
543 compositionengine::LayerFECompositionState& compositionState) const {
544 BufferLayer::latchPerFrameState(compositionState);
545 if (compositionState.compositionType == Hwc2::IComposerClient::Composition::SIDEBAND) {
546 return;
547 }
Marissa Wall61c58622018-07-18 10:12:20 -0700548
chaviwf83ce182019-09-12 14:43:08 -0700549 compositionState.buffer = mBufferInfo.mBuffer;
550 compositionState.bufferSlot = mBufferInfo.mBufferSlot;
chaviw4244e032019-09-04 11:27:49 -0700551 compositionState.acquireFence = mBufferInfo.mFence;
Marissa Wall61c58622018-07-18 10:12:20 -0700552
553 mFrameNumber++;
554}
555
Marissa Wall947d34e2019-03-29 14:03:53 -0700556void BufferStateLayer::HwcSlotGenerator::bufferErased(const client_cache_t& clientCacheId) {
557 std::lock_guard lock(mMutex);
558 if (!clientCacheId.isValid()) {
559 ALOGE("invalid process, failed to erase buffer");
560 return;
561 }
562 eraseBufferLocked(clientCacheId);
563}
564
565uint32_t BufferStateLayer::HwcSlotGenerator::getHwcCacheSlot(const client_cache_t& clientCacheId) {
566 std::lock_guard<std::mutex> lock(mMutex);
567 auto itr = mCachedBuffers.find(clientCacheId);
568 if (itr == mCachedBuffers.end()) {
569 return addCachedBuffer(clientCacheId);
570 }
571 auto& [hwcCacheSlot, counter] = itr->second;
572 counter = mCounter++;
573 return hwcCacheSlot;
574}
575
576uint32_t BufferStateLayer::HwcSlotGenerator::addCachedBuffer(const client_cache_t& clientCacheId)
577 REQUIRES(mMutex) {
578 if (!clientCacheId.isValid()) {
579 ALOGE("invalid process, returning invalid slot");
580 return BufferQueue::INVALID_BUFFER_SLOT;
581 }
582
583 ClientCache::getInstance().registerErasedRecipient(clientCacheId, wp<ErasedRecipient>(this));
584
585 uint32_t hwcCacheSlot = getFreeHwcCacheSlot();
586 mCachedBuffers[clientCacheId] = {hwcCacheSlot, mCounter++};
587 return hwcCacheSlot;
588}
589
590uint32_t BufferStateLayer::HwcSlotGenerator::getFreeHwcCacheSlot() REQUIRES(mMutex) {
591 if (mFreeHwcCacheSlots.empty()) {
592 evictLeastRecentlyUsed();
593 }
594
595 uint32_t hwcCacheSlot = mFreeHwcCacheSlots.top();
596 mFreeHwcCacheSlots.pop();
597 return hwcCacheSlot;
598}
599
600void BufferStateLayer::HwcSlotGenerator::evictLeastRecentlyUsed() REQUIRES(mMutex) {
601 uint64_t minCounter = UINT_MAX;
602 client_cache_t minClientCacheId = {};
603 for (const auto& [clientCacheId, slotCounter] : mCachedBuffers) {
604 const auto& [hwcCacheSlot, counter] = slotCounter;
605 if (counter < minCounter) {
606 minCounter = counter;
607 minClientCacheId = clientCacheId;
608 }
609 }
610 eraseBufferLocked(minClientCacheId);
611
612 ClientCache::getInstance().unregisterErasedRecipient(minClientCacheId, this);
613}
614
615void BufferStateLayer::HwcSlotGenerator::eraseBufferLocked(const client_cache_t& clientCacheId)
616 REQUIRES(mMutex) {
617 auto itr = mCachedBuffers.find(clientCacheId);
618 if (itr == mCachedBuffers.end()) {
619 return;
620 }
621 auto& [hwcCacheSlot, counter] = itr->second;
622
623 // TODO send to hwc cache and resources
624
625 mFreeHwcCacheSlots.push(hwcCacheSlot);
626 mCachedBuffers.erase(clientCacheId);
627}
chaviw4244e032019-09-04 11:27:49 -0700628
629void BufferStateLayer::gatherBufferInfo() {
630 const State& s(getDrawingState());
631
632 mBufferInfo.mDesiredPresentTime = s.desiredPresentTime;
633 mBufferInfo.mFenceTime = std::make_shared<FenceTime>(s.acquireFence);
634 mBufferInfo.mFence = s.acquireFence;
chaviw4244e032019-09-04 11:27:49 -0700635 mBufferInfo.mTransform = s.transform;
636 mBufferInfo.mDataspace = translateDataspace(s.dataspace);
637 mBufferInfo.mCrop = computeCrop(s);
638 mBufferInfo.mScaleMode = NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW;
639 mBufferInfo.mSurfaceDamage = s.surfaceDamageRegion;
640 mBufferInfo.mHdrMetadata = s.hdrMetadata;
641 mBufferInfo.mApi = s.api;
chaviwd62d3062019-09-04 14:48:02 -0700642 mBufferInfo.mPixelFormat =
643 !mBufferInfo.mBuffer ? PIXEL_FORMAT_NONE : mBufferInfo.mBuffer->format;
chaviw4244e032019-09-04 11:27:49 -0700644 mBufferInfo.mTransformToDisplayInverse = s.transformToDisplayInverse;
chaviwf83ce182019-09-12 14:43:08 -0700645 mBufferInfo.mBufferSlot = mHwcSlotGenerator->getHwcCacheSlot(s.clientCacheId);
chaviw4244e032019-09-04 11:27:49 -0700646}
647
648Rect BufferStateLayer::computeCrop(const State& s) {
649 if (s.crop.isEmpty() && s.buffer) {
650 return s.buffer->getBounds();
651 } else if (s.buffer) {
652 Rect crop = s.crop;
653 crop.left = std::max(crop.left, 0);
654 crop.top = std::max(crop.top, 0);
655 uint32_t bufferWidth = s.buffer->getWidth();
656 uint32_t bufferHeight = s.buffer->getHeight();
657 if (bufferHeight <= std::numeric_limits<int32_t>::max() &&
658 bufferWidth <= std::numeric_limits<int32_t>::max()) {
659 crop.right = std::min(crop.right, static_cast<int32_t>(bufferWidth));
660 crop.bottom = std::min(crop.bottom, static_cast<int32_t>(bufferHeight));
661 }
662 if (!crop.isValid()) {
663 // Crop rect is out of bounds, return whole buffer
664 return s.buffer->getBounds();
665 }
666 return crop;
667 }
668 return s.crop;
669}
670
chaviwb4c6e582019-08-16 14:35:07 -0700671sp<Layer> BufferStateLayer::createClone() {
672 const String8 name = mName + " (Mirror)";
673 LayerCreationArgs args =
674 LayerCreationArgs(mFlinger.get(), nullptr, name, 0, 0, 0, LayerMetadata());
675 args.textureName = mTextureName;
Lloyd Pique1c3a5eb2019-10-03 13:07:08 -0700676 sp<BufferStateLayer> layer = mFlinger->getFactory().createBufferStateLayer(args);
chaviwb4c6e582019-08-16 14:35:07 -0700677 layer->mHwcSlotGenerator = mHwcSlotGenerator;
678 layer->setInitialValuesForClone(this);
679 return layer;
680}
Marissa Wall61c58622018-07-18 10:12:20 -0700681} // namespace android