blob: e0804ff50edb0a759c4a3d2428c88068b3d050ab [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 Pique37c2c9b2018-12-04 17:25:10 -080022#include <limits>
Marissa Wall61c58622018-07-18 10:12:20 -070023
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080024#include <compositionengine/Display.h>
Lloyd Pique0b785d82018-12-04 17:25:27 -080025#include <compositionengine/Layer.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080026#include <compositionengine/OutputLayer.h>
Lloyd Pique0b785d82018-12-04 17:25:27 -080027#include <compositionengine/impl/LayerCompositionState.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080028#include <compositionengine/impl/OutputLayerCompositionState.h>
Marissa Wall947d34e2019-03-29 14:03:53 -070029#include <gui/BufferQueue.h>
Marissa Wall61c58622018-07-18 10:12:20 -070030#include <private/gui/SyncFeatures.h>
Peiyong Lincbc184f2018-08-22 13:24:10 -070031#include <renderengine/Image.h>
Marissa Wall61c58622018-07-18 10:12:20 -070032
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080033#include "BufferStateLayer.h"
34#include "ColorLayer.h"
35#include "TimeStats/TimeStats.h"
Valerie Hau0bc09152018-12-20 07:42:47 -080036
Marissa Wall61c58622018-07-18 10:12:20 -070037namespace android {
38
Lloyd Pique42ab75e2018-09-12 20:46:03 -070039// clang-format off
40const std::array<float, 16> BufferStateLayer::IDENTITY_MATRIX{
41 1, 0, 0, 0,
42 0, 1, 0, 0,
43 0, 0, 1, 0,
44 0, 0, 0, 1
45};
46// clang-format on
Marissa Wall61c58622018-07-18 10:12:20 -070047
Marissa Wall947d34e2019-03-29 14:03:53 -070048BufferStateLayer::BufferStateLayer(const LayerCreationArgs& args)
49 : BufferLayer(args), mHwcSlotGenerator(new HwcSlotGenerator()) {
Vishnu Nair60356342018-11-13 13:00:45 -080050 mOverrideScalingMode = NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW;
Marissa Wall3ff826c2019-02-07 11:58:25 -080051 mCurrentState.dataspace = ui::Dataspace::V0_SRGB;
Vishnu Nair60356342018-11-13 13:00:45 -080052}
Marissa Wall61c58622018-07-18 10:12:20 -070053
Alec Mouri4545a8a2019-08-08 20:05:32 -070054BufferStateLayer::~BufferStateLayer() {
55 if (mActiveBuffer != nullptr) {
56 // Ensure that mActiveBuffer is uncached from RenderEngine here, as
57 // RenderEngine may have been using the buffer as an external texture
58 // after the client uncached the buffer.
59 auto& engine(mFlinger->getRenderEngine());
60 engine.unbindExternalTextureBuffer(mActiveBuffer->getId());
61 }
62}
63
Marissa Wall61c58622018-07-18 10:12:20 -070064// -----------------------------------------------------------------------
65// Interface implementation for Layer
66// -----------------------------------------------------------------------
Marissa Wallfda30bb2018-10-12 11:34:28 -070067void BufferStateLayer::onLayerDisplayed(const sp<Fence>& releaseFence) {
Marissa Wall5a68a772018-12-22 17:43:42 -080068 // The previous release fence notifies the client that SurfaceFlinger is done with the previous
69 // buffer that was presented on this layer. The first transaction that came in this frame that
70 // replaced the previous buffer on this layer needs this release fence, because the fence will
71 // let the client know when that previous buffer is removed from the screen.
72 //
73 // Every other transaction on this layer does not need a release fence because no other
74 // Transactions that were set on this layer this frame are going to have their preceeding buffer
75 // removed from the display this frame.
76 //
77 // For example, if we have 3 transactions this frame. The first transaction doesn't contain a
78 // buffer so it doesn't need a previous release fence because the layer still needs the previous
79 // buffer. The second transaction contains a buffer so it needs a previous release fence because
80 // the previous buffer will be released this frame. The third transaction also contains a
81 // buffer. It replaces the buffer in the second transaction. The buffer in the second
82 // transaction will now no longer be presented so it is released immediately and the third
83 // transaction doesn't need a previous release fence.
84 for (auto& handle : mDrawingState.callbackHandles) {
85 if (handle->releasePreviousBuffer) {
86 handle->previousReleaseFence = releaseFence;
87 break;
88 }
89 }
Marissa Wall61c58622018-07-18 10:12:20 -070090}
91
92void BufferStateLayer::setTransformHint(uint32_t /*orientation*/) const {
93 // TODO(marissaw): send the transform hint to buffer owner
94 return;
95}
96
97void BufferStateLayer::releasePendingBuffer(nsecs_t /*dequeueReadyTime*/) {
Marissa Wallefb71af2019-06-27 14:45:53 -070098 mFlinger->getTransactionCompletedThread().finalizePendingCallbackHandles(
Marissa Wall5a68a772018-12-22 17:43:42 -080099 mDrawingState.callbackHandles);
100
101 mDrawingState.callbackHandles = {};
Marissa Wall61c58622018-07-18 10:12:20 -0700102}
103
Ana Krulec010d2192018-10-08 06:29:54 -0700104bool BufferStateLayer::shouldPresentNow(nsecs_t /*expectedPresentTime*/) const {
Marissa Wall61c58622018-07-18 10:12:20 -0700105 if (getSidebandStreamChanged() || getAutoRefresh()) {
106 return true;
107 }
108
Marissa Wall024a1912018-08-13 13:55:35 -0700109 return hasFrameUpdate();
Marissa Wall61c58622018-07-18 10:12:20 -0700110}
111
Marissa Walle2ffb422018-10-12 11:33:52 -0700112bool BufferStateLayer::willPresentCurrentTransaction() const {
113 // Returns true if the most recent Transaction applied to CurrentState will be presented.
114 return getSidebandStreamChanged() || getAutoRefresh() ||
Valerie Hauaa194562019-02-05 16:21:38 -0800115 (mCurrentState.modified &&
116 (mCurrentState.buffer != nullptr || mCurrentState.bgColorLayer != nullptr));
Marissa Wall61c58622018-07-18 10:12:20 -0700117}
118
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800119bool BufferStateLayer::getTransformToDisplayInverse() const {
120 return mCurrentState.transformToDisplayInverse;
Marissa Wall61c58622018-07-18 10:12:20 -0700121}
122
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800123void BufferStateLayer::pushPendingState() {
124 if (!mCurrentState.modified) {
Marissa Wall61c58622018-07-18 10:12:20 -0700125 return;
126 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800127 mPendingStates.push_back(mCurrentState);
128 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Marissa Wall61c58622018-07-18 10:12:20 -0700129}
130
131bool BufferStateLayer::applyPendingStates(Layer::State* stateToCommit) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800132 const bool stateUpdateAvailable = !mPendingStates.empty();
133 while (!mPendingStates.empty()) {
Marissa Wall61c58622018-07-18 10:12:20 -0700134 popPendingState(stateToCommit);
135 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800136 mCurrentStateModified = stateUpdateAvailable && mCurrentState.modified;
137 mCurrentState.modified = false;
Marissa Wall61c58622018-07-18 10:12:20 -0700138 return stateUpdateAvailable;
139}
140
Marissa Wall861616d2018-10-22 12:52:23 -0700141// Crop that applies to the window
142Rect BufferStateLayer::getCrop(const Layer::State& /*s*/) const {
143 return Rect::INVALID_RECT;
Marissa Wall61c58622018-07-18 10:12:20 -0700144}
145
146bool BufferStateLayer::setTransform(uint32_t transform) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800147 if (mCurrentState.transform == transform) return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800148 mCurrentState.transform = transform;
149 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700150 setTransactionFlags(eTransactionNeeded);
151 return true;
152}
153
154bool BufferStateLayer::setTransformToDisplayInverse(bool transformToDisplayInverse) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800155 if (mCurrentState.transformToDisplayInverse == transformToDisplayInverse) return false;
156 mCurrentState.sequence++;
157 mCurrentState.transformToDisplayInverse = transformToDisplayInverse;
158 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700159 setTransactionFlags(eTransactionNeeded);
160 return true;
161}
162
163bool BufferStateLayer::setCrop(const Rect& crop) {
Marissa Wall290ad082019-03-06 13:23:47 -0800164 Rect c = crop;
165 if (c.left < 0) {
166 c.left = 0;
167 }
168 if (c.top < 0) {
169 c.top = 0;
170 }
171 // If the width and/or height are < 0, make it [0, 0, -1, -1] so the equality comparision below
172 // treats all invalid rectangles the same.
173 if (!c.isValid()) {
174 c.makeInvalid();
175 }
176
177 if (mCurrentState.crop == c) return false;
Marissa Wall290ad082019-03-06 13:23:47 -0800178 mCurrentState.crop = c;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800179 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700180 setTransactionFlags(eTransactionNeeded);
181 return true;
182}
183
Marissa Wall861616d2018-10-22 12:52:23 -0700184bool BufferStateLayer::setFrame(const Rect& frame) {
185 int x = frame.left;
186 int y = frame.top;
187 int w = frame.getWidth();
188 int h = frame.getHeight();
189
Marissa Wall0f3242d2018-12-20 15:10:22 -0800190 if (x < 0) {
191 x = 0;
192 w = frame.right;
193 }
194
195 if (y < 0) {
196 y = 0;
197 h = frame.bottom;
198 }
199
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800200 if (mCurrentState.active.transform.tx() == x && mCurrentState.active.transform.ty() == y &&
201 mCurrentState.active.w == w && mCurrentState.active.h == h) {
Marissa Wall861616d2018-10-22 12:52:23 -0700202 return false;
203 }
204
205 if (!frame.isValid()) {
206 x = y = w = h = 0;
207 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800208 mCurrentState.active.transform.set(x, y);
209 mCurrentState.active.w = w;
210 mCurrentState.active.h = h;
Marissa Wall861616d2018-10-22 12:52:23 -0700211
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800212 mCurrentState.sequence++;
213 mCurrentState.modified = true;
Marissa Wall861616d2018-10-22 12:52:23 -0700214 setTransactionFlags(eTransactionNeeded);
215 return true;
216}
217
Ady Abraham09bd3922019-04-08 10:44:56 -0700218bool BufferStateLayer::setBuffer(const sp<GraphicBuffer>& buffer, nsecs_t postTime,
Marissa Wall947d34e2019-03-29 14:03:53 -0700219 nsecs_t desiredPresentTime, const client_cache_t& clientCacheId) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800220 if (mCurrentState.buffer) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700221 mReleasePreviousBuffer = true;
222 }
223
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800224 mCurrentState.buffer = buffer;
Marissa Wall947d34e2019-03-29 14:03:53 -0700225 mCurrentState.clientCacheId = clientCacheId;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800226 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700227 setTransactionFlags(eTransactionNeeded);
Ady Abraham09bd3922019-04-08 10:44:56 -0700228
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700229 mFlinger->mTimeStats->setPostTime(getSequence(), mFrameNumber, getName().c_str(), postTime);
chaviwfa67b552019-08-12 16:51:55 -0700230 mCurrentState.desiredPresentTime = desiredPresentTime;
Ady Abraham09bd3922019-04-08 10:44:56 -0700231
232 if (mFlinger->mUseSmart90ForVideo) {
chaviwfa67b552019-08-12 16:51:55 -0700233 const nsecs_t presentTime = (desiredPresentTime == -1) ? 0 : desiredPresentTime;
Ady Abrahama315ce72019-04-24 14:35:20 -0700234 mFlinger->mScheduler->addLayerPresentTimeAndHDR(mSchedulerLayerHandle, presentTime,
235 mCurrentState.hdrMetadata.validTypes != 0);
Ady Abraham09bd3922019-04-08 10:44:56 -0700236 }
237
Marissa Wall61c58622018-07-18 10:12:20 -0700238 return true;
239}
240
241bool BufferStateLayer::setAcquireFence(const sp<Fence>& fence) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700242 // The acquire fences of BufferStateLayers have already signaled before they are set
243 mCallbackHandleAcquireTime = fence->getSignalTime();
244
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800245 mCurrentState.acquireFence = fence;
246 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700247 setTransactionFlags(eTransactionNeeded);
248 return true;
249}
250
251bool BufferStateLayer::setDataspace(ui::Dataspace dataspace) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800252 if (mCurrentState.dataspace == dataspace) return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800253 mCurrentState.dataspace = dataspace;
254 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700255 setTransactionFlags(eTransactionNeeded);
256 return true;
257}
258
259bool BufferStateLayer::setHdrMetadata(const HdrMetadata& hdrMetadata) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800260 if (mCurrentState.hdrMetadata == hdrMetadata) return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800261 mCurrentState.hdrMetadata = hdrMetadata;
262 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700263 setTransactionFlags(eTransactionNeeded);
264 return true;
265}
266
267bool BufferStateLayer::setSurfaceDamageRegion(const Region& surfaceDamage) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800268 mCurrentState.surfaceDamageRegion = surfaceDamage;
269 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700270 setTransactionFlags(eTransactionNeeded);
271 return true;
272}
273
274bool BufferStateLayer::setApi(int32_t api) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800275 if (mCurrentState.api == api) return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800276 mCurrentState.api = api;
277 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700278 setTransactionFlags(eTransactionNeeded);
279 return true;
280}
281
282bool BufferStateLayer::setSidebandStream(const sp<NativeHandle>& sidebandStream) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800283 if (mCurrentState.sidebandStream == sidebandStream) return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800284 mCurrentState.sidebandStream = sidebandStream;
285 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700286 setTransactionFlags(eTransactionNeeded);
287
288 if (!mSidebandStreamChanged.exchange(true)) {
289 // mSidebandStreamChanged was false
290 mFlinger->signalLayerUpdate();
291 }
292 return true;
293}
294
Marissa Walle2ffb422018-10-12 11:33:52 -0700295bool BufferStateLayer::setTransactionCompletedListeners(
296 const std::vector<sp<CallbackHandle>>& handles) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700297 // If there is no handle, we will not send a callback so reset mReleasePreviousBuffer and return
Marissa Walle2ffb422018-10-12 11:33:52 -0700298 if (handles.empty()) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700299 mReleasePreviousBuffer = false;
Marissa Walle2ffb422018-10-12 11:33:52 -0700300 return false;
301 }
302
303 const bool willPresent = willPresentCurrentTransaction();
304
305 for (const auto& handle : handles) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700306 // If this transaction set a buffer on this layer, release its previous buffer
307 handle->releasePreviousBuffer = mReleasePreviousBuffer;
308
Marissa Walle2ffb422018-10-12 11:33:52 -0700309 // If this layer will be presented in this frame
310 if (willPresent) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700311 // If this transaction set an acquire fence on this layer, set its acquire time
312 handle->acquireTime = mCallbackHandleAcquireTime;
313
Marissa Walle2ffb422018-10-12 11:33:52 -0700314 // Notify the transaction completed thread that there is a pending latched callback
315 // handle
Marissa Wall5a68a772018-12-22 17:43:42 -0800316 mFlinger->getTransactionCompletedThread().registerPendingCallbackHandle(handle);
Marissa Walle2ffb422018-10-12 11:33:52 -0700317
318 // Store so latched time and release fence can be set
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800319 mCurrentState.callbackHandles.push_back(handle);
Marissa Walle2ffb422018-10-12 11:33:52 -0700320
321 } else { // If this layer will NOT need to be relatched and presented this frame
322 // Notify the transaction completed thread this handle is done
Marissa Wallefb71af2019-06-27 14:45:53 -0700323 mFlinger->getTransactionCompletedThread().registerUnpresentedCallbackHandle(handle);
Marissa Walle2ffb422018-10-12 11:33:52 -0700324 }
325 }
326
Marissa Wallfda30bb2018-10-12 11:34:28 -0700327 mReleasePreviousBuffer = false;
328 mCallbackHandleAcquireTime = -1;
329
Marissa Walle2ffb422018-10-12 11:33:52 -0700330 return willPresent;
331}
332
Marissa Wall61c58622018-07-18 10:12:20 -0700333bool BufferStateLayer::setTransparentRegionHint(const Region& transparent) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800334 mCurrentState.transparentRegionHint = transparent;
335 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700336 setTransactionFlags(eTransactionNeeded);
337 return true;
338}
339
Marissa Wall861616d2018-10-22 12:52:23 -0700340Rect BufferStateLayer::getBufferSize(const State& s) const {
341 // for buffer state layers we use the display frame size as the buffer size.
342 if (getActiveWidth(s) < UINT32_MAX && getActiveHeight(s) < UINT32_MAX) {
343 return Rect(getActiveWidth(s), getActiveHeight(s));
Marissa Wall61c58622018-07-18 10:12:20 -0700344 }
345
Marissa Wall861616d2018-10-22 12:52:23 -0700346 // if the display frame is not defined, use the parent bounds as the buffer size.
347 const auto& p = mDrawingParent.promote();
348 if (p != nullptr) {
Vishnu Nair4351ad52019-02-11 14:13:02 -0800349 Rect parentBounds = Rect(p->getBounds(Region()));
Marissa Wall861616d2018-10-22 12:52:23 -0700350 if (!parentBounds.isEmpty()) {
351 return parentBounds;
352 }
353 }
354
Marissa Wall861616d2018-10-22 12:52:23 -0700355 return Rect::INVALID_RECT;
Marissa Wall61c58622018-07-18 10:12:20 -0700356}
Vishnu Nair4351ad52019-02-11 14:13:02 -0800357
358FloatRect BufferStateLayer::computeSourceBounds(const FloatRect& parentBounds) const {
359 const State& s(getDrawingState());
360 // for buffer state layers we use the display frame size as the buffer size.
361 if (getActiveWidth(s) < UINT32_MAX && getActiveHeight(s) < UINT32_MAX) {
362 return FloatRect(0, 0, getActiveWidth(s), getActiveHeight(s));
363 }
364
365 // if the display frame is not defined, use the parent bounds as the buffer size.
366 return parentBounds;
367}
368
Marissa Wall61c58622018-07-18 10:12:20 -0700369// -----------------------------------------------------------------------
370
371// -----------------------------------------------------------------------
372// Interface implementation for BufferLayer
373// -----------------------------------------------------------------------
374bool BufferStateLayer::fenceHasSignaled() const {
375 if (latchUnsignaledBuffers()) {
376 return true;
377 }
378
379 return getDrawingState().acquireFence->getStatus() == Fence::Status::Signaled;
380}
381
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700382bool BufferStateLayer::framePresentTimeIsCurrent(nsecs_t expectedPresentTime) const {
Ady Abrahamcd1580c2019-04-29 15:40:03 -0700383 if (!hasFrameUpdate() || isRemovedFromCurrentState()) {
384 return true;
385 }
386
chaviwfa67b552019-08-12 16:51:55 -0700387 return mCurrentState.desiredPresentTime <= expectedPresentTime;
Ady Abrahamcd1580c2019-04-29 15:40:03 -0700388}
389
Marissa Wall61c58622018-07-18 10:12:20 -0700390nsecs_t BufferStateLayer::getDesiredPresentTime() {
chaviwfa67b552019-08-12 16:51:55 -0700391 return getDrawingState().desiredPresentTime;
Marissa Wall61c58622018-07-18 10:12:20 -0700392}
393
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800394std::shared_ptr<FenceTime> BufferStateLayer::getCurrentFenceTime() const {
Marissa Wall61c58622018-07-18 10:12:20 -0700395 return std::make_shared<FenceTime>(getDrawingState().acquireFence);
396}
397
398void BufferStateLayer::getDrawingTransformMatrix(float *matrix) {
399 std::copy(std::begin(mTransformMatrix), std::end(mTransformMatrix), matrix);
400}
401
402uint32_t BufferStateLayer::getDrawingTransform() const {
403 return getDrawingState().transform;
404}
405
406ui::Dataspace BufferStateLayer::getDrawingDataSpace() const {
407 return getDrawingState().dataspace;
408}
409
Marissa Wall861616d2018-10-22 12:52:23 -0700410// Crop that applies to the buffer
Marissa Wall61c58622018-07-18 10:12:20 -0700411Rect BufferStateLayer::getDrawingCrop() const {
Marissa Wall861616d2018-10-22 12:52:23 -0700412 const State& s(getDrawingState());
413
414 if (s.crop.isEmpty() && s.buffer) {
415 return s.buffer->getBounds();
Valerie Hau0bc09152018-12-20 07:42:47 -0800416 } else if (s.buffer) {
417 Rect crop = s.crop;
418 crop.left = std::max(crop.left, 0);
419 crop.top = std::max(crop.top, 0);
420 uint32_t bufferWidth = s.buffer->getWidth();
421 uint32_t bufferHeight = s.buffer->getHeight();
422 if (bufferHeight <= std::numeric_limits<int32_t>::max() &&
423 bufferWidth <= std::numeric_limits<int32_t>::max()) {
424 crop.right = std::min(crop.right, static_cast<int32_t>(bufferWidth));
425 crop.bottom = std::min(crop.bottom, static_cast<int32_t>(bufferHeight));
426 }
427 if (!crop.isValid()) {
428 // Crop rect is out of bounds, return whole buffer
429 return s.buffer->getBounds();
430 }
431 return crop;
Marissa Wall861616d2018-10-22 12:52:23 -0700432 }
433 return s.crop;
Marissa Wall61c58622018-07-18 10:12:20 -0700434}
435
436uint32_t BufferStateLayer::getDrawingScalingMode() const {
Marissa Wallec463ac2018-10-08 12:35:04 -0700437 return NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW;
Marissa Wall61c58622018-07-18 10:12:20 -0700438}
439
440Region BufferStateLayer::getDrawingSurfaceDamage() const {
Marissa Wall0ef8d602019-04-23 14:09:28 -0700441 return getDrawingState().surfaceDamageRegion;
Marissa Wall61c58622018-07-18 10:12:20 -0700442}
443
444const HdrMetadata& BufferStateLayer::getDrawingHdrMetadata() const {
445 return getDrawingState().hdrMetadata;
446}
447
448int BufferStateLayer::getDrawingApi() const {
449 return getDrawingState().api;
450}
451
452PixelFormat BufferStateLayer::getPixelFormat() const {
Marissa Wall5aec6412018-11-14 11:49:18 -0800453 if (!mActiveBuffer) {
454 return PIXEL_FORMAT_NONE;
455 }
Marissa Wall61c58622018-07-18 10:12:20 -0700456 return mActiveBuffer->format;
457}
458
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700459uint64_t BufferStateLayer::getFrameNumber(nsecs_t /*expectedPresentTime*/) const {
Marissa Wall61c58622018-07-18 10:12:20 -0700460 return mFrameNumber;
461}
462
463bool BufferStateLayer::getAutoRefresh() const {
464 // TODO(marissaw): support shared buffer mode
465 return false;
466}
467
468bool BufferStateLayer::getSidebandStreamChanged() const {
469 return mSidebandStreamChanged.load();
470}
471
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800472bool BufferStateLayer::latchSidebandStream(bool& recomputeVisibleRegions) {
Marissa Wall61c58622018-07-18 10:12:20 -0700473 if (mSidebandStreamChanged.exchange(false)) {
474 const State& s(getDrawingState());
475 // mSidebandStreamChanged was true
Lloyd Pique0b785d82018-12-04 17:25:27 -0800476 LOG_ALWAYS_FATAL_IF(!getCompositionLayer());
477 mSidebandStream = s.sidebandStream;
478 getCompositionLayer()->editState().frontEnd.sidebandStream = mSidebandStream;
479 if (mSidebandStream != nullptr) {
Marissa Wall61c58622018-07-18 10:12:20 -0700480 setTransactionFlags(eTransactionNeeded);
481 mFlinger->setTransactionFlags(eTraversalNeeded);
482 }
483 recomputeVisibleRegions = true;
484
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800485 return true;
Marissa Wall61c58622018-07-18 10:12:20 -0700486 }
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800487 return false;
Marissa Wall61c58622018-07-18 10:12:20 -0700488}
489
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800490bool BufferStateLayer::hasFrameUpdate() const {
Valerie Hauaa194562019-02-05 16:21:38 -0800491 const State& c(getCurrentState());
492 return mCurrentStateModified && (c.buffer != nullptr || c.bgColorLayer != nullptr);
Marissa Wall61c58622018-07-18 10:12:20 -0700493}
494
495void BufferStateLayer::setFilteringEnabled(bool enabled) {
496 GLConsumer::computeTransformMatrix(mTransformMatrix.data(), mActiveBuffer, mCurrentCrop,
497 mCurrentTransform, enabled);
498}
499
Alec Mouri39801c02018-10-10 10:44:47 -0700500status_t BufferStateLayer::bindTextureImage() {
Marissa Wall61c58622018-07-18 10:12:20 -0700501 const State& s(getDrawingState());
502 auto& engine(mFlinger->getRenderEngine());
503
Alec Mourib5c4f352019-02-19 19:46:38 -0800504 return engine.bindExternalTextureBuffer(mTextureName, s.buffer, s.acquireFence);
Marissa Wall61c58622018-07-18 10:12:20 -0700505}
506
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700507status_t BufferStateLayer::updateTexImage(bool& /*recomputeVisibleRegions*/, nsecs_t latchTime,
508 nsecs_t /*expectedPresentTime*/) {
Marissa Wall61c58622018-07-18 10:12:20 -0700509 const State& s(getDrawingState());
510
511 if (!s.buffer) {
Valerie Hauaa194562019-02-05 16:21:38 -0800512 if (s.bgColorLayer) {
513 for (auto& handle : mDrawingState.callbackHandles) {
514 handle->latchTime = latchTime;
515 }
516 }
Marissa Wall61c58622018-07-18 10:12:20 -0700517 return NO_ERROR;
518 }
519
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700520 const int32_t layerID = getSequence();
521
Marissa Wall61c58622018-07-18 10:12:20 -0700522 // Reject if the layer is invalid
523 uint32_t bufferWidth = s.buffer->width;
524 uint32_t bufferHeight = s.buffer->height;
525
Peiyong Linefefaac2018-08-17 12:27:51 -0700526 if (s.transform & ui::Transform::ROT_90) {
Peiyong Lin3db42342018-08-16 09:15:59 -0700527 std::swap(bufferWidth, bufferHeight);
Marissa Wall61c58622018-07-18 10:12:20 -0700528 }
529
530 if (s.transformToDisplayInverse) {
531 uint32_t invTransform = DisplayDevice::getPrimaryDisplayOrientationTransform();
Peiyong Linefefaac2018-08-17 12:27:51 -0700532 if (invTransform & ui::Transform::ROT_90) {
Peiyong Lin3db42342018-08-16 09:15:59 -0700533 std::swap(bufferWidth, bufferHeight);
Marissa Wall61c58622018-07-18 10:12:20 -0700534 }
535 }
536
Vishnu Nair60356342018-11-13 13:00:45 -0800537 if (getEffectiveScalingMode() == NATIVE_WINDOW_SCALING_MODE_FREEZE &&
Marissa Wall61c58622018-07-18 10:12:20 -0700538 (s.active.w != bufferWidth || s.active.h != bufferHeight)) {
539 ALOGE("[%s] rejecting buffer: "
540 "bufferWidth=%d, bufferHeight=%d, front.active.{w=%d, h=%d}",
541 mName.string(), bufferWidth, bufferHeight, s.active.w, s.active.h);
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700542 mFlinger->mTimeStats->removeTimeRecord(layerID, mFrameNumber);
Marissa Wall61c58622018-07-18 10:12:20 -0700543 return BAD_VALUE;
544 }
545
Marissa Wall5a68a772018-12-22 17:43:42 -0800546 for (auto& handle : mDrawingState.callbackHandles) {
547 handle->latchTime = latchTime;
548 }
Marissa Walle2ffb422018-10-12 11:33:52 -0700549
Alec Mouri56e538f2019-01-14 15:22:01 -0800550 if (!SyncFeatures::getInstance().useNativeFenceSync()) {
Marissa Wall61c58622018-07-18 10:12:20 -0700551 // Bind the new buffer to the GL texture.
552 //
553 // Older devices require the "implicit" synchronization provided
554 // by glEGLImageTargetTexture2DOES, which this method calls. Newer
555 // devices will either call this in Layer::onDraw, or (if it's not
556 // a GL-composited layer) not at all.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800557 status_t err = bindTextureImage();
Marissa Wall61c58622018-07-18 10:12:20 -0700558 if (err != NO_ERROR) {
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800559 mFlinger->mTimeStats->onDestroy(layerID);
Marissa Wall61c58622018-07-18 10:12:20 -0700560 return BAD_VALUE;
561 }
562 }
563
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700564 mFlinger->mTimeStats->setAcquireFence(layerID, mFrameNumber, getCurrentFenceTime());
565 mFlinger->mTimeStats->setLatchTime(layerID, mFrameNumber, latchTime);
Marissa Wall61c58622018-07-18 10:12:20 -0700566
Marissa Wall16c112d2019-03-20 13:21:13 -0700567 mCurrentStateModified = false;
568
Marissa Wall61c58622018-07-18 10:12:20 -0700569 return NO_ERROR;
570}
571
572status_t BufferStateLayer::updateActiveBuffer() {
573 const State& s(getDrawingState());
574
575 if (s.buffer == nullptr) {
576 return BAD_VALUE;
577 }
578
579 mActiveBuffer = s.buffer;
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000580 mActiveBufferFence = s.acquireFence;
Lloyd Pique0b785d82018-12-04 17:25:27 -0800581 auto& layerCompositionState = getCompositionLayer()->editState().frontEnd;
582 layerCompositionState.buffer = mActiveBuffer;
Marissa Wall61c58622018-07-18 10:12:20 -0700583
584 return NO_ERROR;
585}
586
587status_t BufferStateLayer::updateFrameNumber(nsecs_t /*latchTime*/) {
588 // TODO(marissaw): support frame history events
589 mCurrentFrameNumber = mFrameNumber;
590 return NO_ERROR;
591}
592
Lloyd Piquef5275482019-01-29 18:42:42 -0800593void BufferStateLayer::latchPerFrameState(
594 compositionengine::LayerFECompositionState& compositionState) const {
595 BufferLayer::latchPerFrameState(compositionState);
596 if (compositionState.compositionType == Hwc2::IComposerClient::Composition::SIDEBAND) {
597 return;
598 }
Marissa Wall61c58622018-07-18 10:12:20 -0700599
600 const State& s(getDrawingState());
601
Lloyd Piquef5275482019-01-29 18:42:42 -0800602 compositionState.buffer = s.buffer;
603 compositionState.bufferSlot = mHwcSlotGenerator->getHwcCacheSlot(s.clientCacheId);
604 compositionState.acquireFence = s.acquireFence;
Marissa Wall61c58622018-07-18 10:12:20 -0700605
606 mFrameNumber++;
607}
608
609void BufferStateLayer::onFirstRef() {
Dan Stoza7b1b5a82018-07-31 16:00:21 -0700610 BufferLayer::onFirstRef();
611
Marissa Wall61c58622018-07-18 10:12:20 -0700612 if (const auto display = mFlinger->getDefaultDisplayDevice()) {
613 updateTransformHint(display);
614 }
615}
616
Marissa Wall947d34e2019-03-29 14:03:53 -0700617void BufferStateLayer::HwcSlotGenerator::bufferErased(const client_cache_t& clientCacheId) {
618 std::lock_guard lock(mMutex);
619 if (!clientCacheId.isValid()) {
620 ALOGE("invalid process, failed to erase buffer");
621 return;
622 }
623 eraseBufferLocked(clientCacheId);
624}
625
626uint32_t BufferStateLayer::HwcSlotGenerator::getHwcCacheSlot(const client_cache_t& clientCacheId) {
627 std::lock_guard<std::mutex> lock(mMutex);
628 auto itr = mCachedBuffers.find(clientCacheId);
629 if (itr == mCachedBuffers.end()) {
630 return addCachedBuffer(clientCacheId);
631 }
632 auto& [hwcCacheSlot, counter] = itr->second;
633 counter = mCounter++;
634 return hwcCacheSlot;
635}
636
637uint32_t BufferStateLayer::HwcSlotGenerator::addCachedBuffer(const client_cache_t& clientCacheId)
638 REQUIRES(mMutex) {
639 if (!clientCacheId.isValid()) {
640 ALOGE("invalid process, returning invalid slot");
641 return BufferQueue::INVALID_BUFFER_SLOT;
642 }
643
644 ClientCache::getInstance().registerErasedRecipient(clientCacheId, wp<ErasedRecipient>(this));
645
646 uint32_t hwcCacheSlot = getFreeHwcCacheSlot();
647 mCachedBuffers[clientCacheId] = {hwcCacheSlot, mCounter++};
648 return hwcCacheSlot;
649}
650
651uint32_t BufferStateLayer::HwcSlotGenerator::getFreeHwcCacheSlot() REQUIRES(mMutex) {
652 if (mFreeHwcCacheSlots.empty()) {
653 evictLeastRecentlyUsed();
654 }
655
656 uint32_t hwcCacheSlot = mFreeHwcCacheSlots.top();
657 mFreeHwcCacheSlots.pop();
658 return hwcCacheSlot;
659}
660
661void BufferStateLayer::HwcSlotGenerator::evictLeastRecentlyUsed() REQUIRES(mMutex) {
662 uint64_t minCounter = UINT_MAX;
663 client_cache_t minClientCacheId = {};
664 for (const auto& [clientCacheId, slotCounter] : mCachedBuffers) {
665 const auto& [hwcCacheSlot, counter] = slotCounter;
666 if (counter < minCounter) {
667 minCounter = counter;
668 minClientCacheId = clientCacheId;
669 }
670 }
671 eraseBufferLocked(minClientCacheId);
672
673 ClientCache::getInstance().unregisterErasedRecipient(minClientCacheId, this);
674}
675
676void BufferStateLayer::HwcSlotGenerator::eraseBufferLocked(const client_cache_t& clientCacheId)
677 REQUIRES(mMutex) {
678 auto itr = mCachedBuffers.find(clientCacheId);
679 if (itr == mCachedBuffers.end()) {
680 return;
681 }
682 auto& [hwcCacheSlot, counter] = itr->second;
683
684 // TODO send to hwc cache and resources
685
686 mFreeHwcCacheSlots.push(hwcCacheSlot);
687 mCachedBuffers.erase(clientCacheId);
688}
Marissa Wall61c58622018-07-18 10:12:20 -0700689} // namespace android