blob: 63a07c35ca0be3d0b410ab7adf41ceecc3934c6a [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 Wall5a68a772018-12-22 17:43:42 -080098 mFlinger->getTransactionCompletedThread().addPresentedCallbackHandles(
99 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
229 mFlinger->mTimeStats->setPostTime(getSequence(), getFrameNumber(), getName().c_str(), postTime);
230 mDesiredPresentTime = desiredPresentTime;
231
232 if (mFlinger->mUseSmart90ForVideo) {
233 const nsecs_t presentTime = (mDesiredPresentTime == -1) ? 0 : mDesiredPresentTime;
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 Wall5a68a772018-12-22 17:43:42 -0800323 mFlinger->getTransactionCompletedThread().addUnpresentedCallbackHandle(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
Ady Abrahamcd1580c2019-04-29 15:40:03 -0700382bool BufferStateLayer::framePresentTimeIsCurrent() const {
383 if (!hasFrameUpdate() || isRemovedFromCurrentState()) {
384 return true;
385 }
386
Alec Mouriaa614192019-06-06 13:28:34 -0700387 return mDesiredPresentTime <= mFlinger->getExpectedPresentTime();
Ady Abrahamcd1580c2019-04-29 15:40:03 -0700388}
389
Marissa Wall61c58622018-07-18 10:12:20 -0700390nsecs_t BufferStateLayer::getDesiredPresentTime() {
Valerie Haubc6ddb12019-03-08 11:10:15 -0800391 return mDesiredPresentTime;
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
459uint64_t BufferStateLayer::getFrameNumber() const {
460 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
Alec Mouri56e538f2019-01-14 15:22:01 -0800507status_t BufferStateLayer::updateTexImage(bool& /*recomputeVisibleRegions*/, nsecs_t latchTime) {
Marissa Wall61c58622018-07-18 10:12:20 -0700508 const State& s(getDrawingState());
509
510 if (!s.buffer) {
Valerie Hauaa194562019-02-05 16:21:38 -0800511 if (s.bgColorLayer) {
512 for (auto& handle : mDrawingState.callbackHandles) {
513 handle->latchTime = latchTime;
514 }
515 }
Marissa Wall61c58622018-07-18 10:12:20 -0700516 return NO_ERROR;
517 }
518
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700519 const int32_t layerID = getSequence();
520
Marissa Wall61c58622018-07-18 10:12:20 -0700521 // Reject if the layer is invalid
522 uint32_t bufferWidth = s.buffer->width;
523 uint32_t bufferHeight = s.buffer->height;
524
Peiyong Linefefaac2018-08-17 12:27:51 -0700525 if (s.transform & ui::Transform::ROT_90) {
Peiyong Lin3db42342018-08-16 09:15:59 -0700526 std::swap(bufferWidth, bufferHeight);
Marissa Wall61c58622018-07-18 10:12:20 -0700527 }
528
529 if (s.transformToDisplayInverse) {
530 uint32_t invTransform = DisplayDevice::getPrimaryDisplayOrientationTransform();
Peiyong Linefefaac2018-08-17 12:27:51 -0700531 if (invTransform & ui::Transform::ROT_90) {
Peiyong Lin3db42342018-08-16 09:15:59 -0700532 std::swap(bufferWidth, bufferHeight);
Marissa Wall61c58622018-07-18 10:12:20 -0700533 }
534 }
535
Vishnu Nair60356342018-11-13 13:00:45 -0800536 if (getEffectiveScalingMode() == NATIVE_WINDOW_SCALING_MODE_FREEZE &&
Marissa Wall61c58622018-07-18 10:12:20 -0700537 (s.active.w != bufferWidth || s.active.h != bufferHeight)) {
538 ALOGE("[%s] rejecting buffer: "
539 "bufferWidth=%d, bufferHeight=%d, front.active.{w=%d, h=%d}",
540 mName.string(), bufferWidth, bufferHeight, s.active.w, s.active.h);
Yiwei Zhang7e666a52018-11-15 13:33:42 -0800541 mFlinger->mTimeStats->removeTimeRecord(layerID, getFrameNumber());
Marissa Wall61c58622018-07-18 10:12:20 -0700542 return BAD_VALUE;
543 }
544
Marissa Wall5a68a772018-12-22 17:43:42 -0800545 for (auto& handle : mDrawingState.callbackHandles) {
546 handle->latchTime = latchTime;
547 }
Marissa Walle2ffb422018-10-12 11:33:52 -0700548
Alec Mouri56e538f2019-01-14 15:22:01 -0800549 if (!SyncFeatures::getInstance().useNativeFenceSync()) {
Marissa Wall61c58622018-07-18 10:12:20 -0700550 // Bind the new buffer to the GL texture.
551 //
552 // Older devices require the "implicit" synchronization provided
553 // by glEGLImageTargetTexture2DOES, which this method calls. Newer
554 // devices will either call this in Layer::onDraw, or (if it's not
555 // a GL-composited layer) not at all.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800556 status_t err = bindTextureImage();
Marissa Wall61c58622018-07-18 10:12:20 -0700557 if (err != NO_ERROR) {
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800558 mFlinger->mTimeStats->onDestroy(layerID);
Marissa Wall61c58622018-07-18 10:12:20 -0700559 return BAD_VALUE;
560 }
561 }
562
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800563 mFlinger->mTimeStats->setAcquireFence(layerID, getFrameNumber(), getCurrentFenceTime());
Yiwei Zhang7e666a52018-11-15 13:33:42 -0800564 mFlinger->mTimeStats->setLatchTime(layerID, getFrameNumber(), latchTime);
Marissa Wall61c58622018-07-18 10:12:20 -0700565
Marissa Wall16c112d2019-03-20 13:21:13 -0700566 mCurrentStateModified = false;
567
Marissa Wall61c58622018-07-18 10:12:20 -0700568 return NO_ERROR;
569}
570
571status_t BufferStateLayer::updateActiveBuffer() {
572 const State& s(getDrawingState());
573
574 if (s.buffer == nullptr) {
575 return BAD_VALUE;
576 }
577
578 mActiveBuffer = s.buffer;
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000579 mActiveBufferFence = s.acquireFence;
Lloyd Pique0b785d82018-12-04 17:25:27 -0800580 auto& layerCompositionState = getCompositionLayer()->editState().frontEnd;
581 layerCompositionState.buffer = mActiveBuffer;
582 layerCompositionState.bufferSlot = 0;
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 Pique37c2c9b2018-12-04 17:25:10 -0800593void BufferStateLayer::setHwcLayerBuffer(const sp<const DisplayDevice>& display) {
594 const auto outputLayer = findOutputLayerForDisplay(display);
595 LOG_FATAL_IF(!outputLayer || !outputLayer->getState().hwc);
Valerie Hau6f89c372019-03-02 00:13:33 +0000596 auto& hwcInfo = *outputLayer->editState().hwc;
597 auto& hwcLayer = hwcInfo.hwcLayer;
Marissa Wall61c58622018-07-18 10:12:20 -0700598
599 const State& s(getDrawingState());
600
Valerie Hau13f0d1a2019-03-22 10:35:42 -0700601 uint32_t hwcSlot;
Valerie Hau6f89c372019-03-02 00:13:33 +0000602 sp<GraphicBuffer> buffer;
Marissa Wall947d34e2019-03-29 14:03:53 -0700603 hwcInfo.hwcBufferCache.getHwcBuffer(mHwcSlotGenerator->getHwcCacheSlot(s.clientCacheId),
604 s.buffer, &hwcSlot, &buffer);
Marissa Wall61c58622018-07-18 10:12:20 -0700605
Valerie Hau6f89c372019-03-02 00:13:33 +0000606 auto error = hwcLayer->setBuffer(hwcSlot, buffer, s.acquireFence);
Marissa Wall61c58622018-07-18 10:12:20 -0700607 if (error != HWC2::Error::None) {
608 ALOGE("[%s] Failed to set buffer %p: %s (%d)", mName.string(),
609 s.buffer->handle, to_string(error).c_str(), static_cast<int32_t>(error));
610 }
611
612 mFrameNumber++;
613}
614
615void BufferStateLayer::onFirstRef() {
Dan Stoza7b1b5a82018-07-31 16:00:21 -0700616 BufferLayer::onFirstRef();
617
Marissa Wall61c58622018-07-18 10:12:20 -0700618 if (const auto display = mFlinger->getDefaultDisplayDevice()) {
619 updateTransformHint(display);
620 }
621}
622
Marissa Wall947d34e2019-03-29 14:03:53 -0700623void BufferStateLayer::HwcSlotGenerator::bufferErased(const client_cache_t& clientCacheId) {
624 std::lock_guard lock(mMutex);
625 if (!clientCacheId.isValid()) {
626 ALOGE("invalid process, failed to erase buffer");
627 return;
628 }
629 eraseBufferLocked(clientCacheId);
630}
631
632uint32_t BufferStateLayer::HwcSlotGenerator::getHwcCacheSlot(const client_cache_t& clientCacheId) {
633 std::lock_guard<std::mutex> lock(mMutex);
634 auto itr = mCachedBuffers.find(clientCacheId);
635 if (itr == mCachedBuffers.end()) {
636 return addCachedBuffer(clientCacheId);
637 }
638 auto& [hwcCacheSlot, counter] = itr->second;
639 counter = mCounter++;
640 return hwcCacheSlot;
641}
642
643uint32_t BufferStateLayer::HwcSlotGenerator::addCachedBuffer(const client_cache_t& clientCacheId)
644 REQUIRES(mMutex) {
645 if (!clientCacheId.isValid()) {
646 ALOGE("invalid process, returning invalid slot");
647 return BufferQueue::INVALID_BUFFER_SLOT;
648 }
649
650 ClientCache::getInstance().registerErasedRecipient(clientCacheId, wp<ErasedRecipient>(this));
651
652 uint32_t hwcCacheSlot = getFreeHwcCacheSlot();
653 mCachedBuffers[clientCacheId] = {hwcCacheSlot, mCounter++};
654 return hwcCacheSlot;
655}
656
657uint32_t BufferStateLayer::HwcSlotGenerator::getFreeHwcCacheSlot() REQUIRES(mMutex) {
658 if (mFreeHwcCacheSlots.empty()) {
659 evictLeastRecentlyUsed();
660 }
661
662 uint32_t hwcCacheSlot = mFreeHwcCacheSlots.top();
663 mFreeHwcCacheSlots.pop();
664 return hwcCacheSlot;
665}
666
667void BufferStateLayer::HwcSlotGenerator::evictLeastRecentlyUsed() REQUIRES(mMutex) {
668 uint64_t minCounter = UINT_MAX;
669 client_cache_t minClientCacheId = {};
670 for (const auto& [clientCacheId, slotCounter] : mCachedBuffers) {
671 const auto& [hwcCacheSlot, counter] = slotCounter;
672 if (counter < minCounter) {
673 minCounter = counter;
674 minClientCacheId = clientCacheId;
675 }
676 }
677 eraseBufferLocked(minClientCacheId);
678
679 ClientCache::getInstance().unregisterErasedRecipient(minClientCacheId, this);
680}
681
682void BufferStateLayer::HwcSlotGenerator::eraseBufferLocked(const client_cache_t& clientCacheId)
683 REQUIRES(mMutex) {
684 auto itr = mCachedBuffers.find(clientCacheId);
685 if (itr == mCachedBuffers.end()) {
686 return;
687 }
688 auto& [hwcCacheSlot, counter] = itr->second;
689
690 // TODO send to hwc cache and resources
691
692 mFreeHwcCacheSlots.push(hwcCacheSlot);
693 mCachedBuffers.erase(clientCacheId);
694}
Marissa Wall61c58622018-07-18 10:12:20 -0700695} // namespace android