blob: 06b2364bcf662244c586d7542130560592d9b9d0 [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
54// -----------------------------------------------------------------------
55// Interface implementation for Layer
56// -----------------------------------------------------------------------
Marissa Wallfda30bb2018-10-12 11:34:28 -070057void BufferStateLayer::onLayerDisplayed(const sp<Fence>& releaseFence) {
Marissa Wall5a68a772018-12-22 17:43:42 -080058 // The previous release fence notifies the client that SurfaceFlinger is done with the previous
59 // buffer that was presented on this layer. The first transaction that came in this frame that
60 // replaced the previous buffer on this layer needs this release fence, because the fence will
61 // let the client know when that previous buffer is removed from the screen.
62 //
63 // Every other transaction on this layer does not need a release fence because no other
64 // Transactions that were set on this layer this frame are going to have their preceeding buffer
65 // removed from the display this frame.
66 //
67 // For example, if we have 3 transactions this frame. The first transaction doesn't contain a
68 // buffer so it doesn't need a previous release fence because the layer still needs the previous
69 // buffer. The second transaction contains a buffer so it needs a previous release fence because
70 // the previous buffer will be released this frame. The third transaction also contains a
71 // buffer. It replaces the buffer in the second transaction. The buffer in the second
72 // transaction will now no longer be presented so it is released immediately and the third
73 // transaction doesn't need a previous release fence.
74 for (auto& handle : mDrawingState.callbackHandles) {
75 if (handle->releasePreviousBuffer) {
76 handle->previousReleaseFence = releaseFence;
77 break;
78 }
79 }
Marissa Wall61c58622018-07-18 10:12:20 -070080}
81
82void BufferStateLayer::setTransformHint(uint32_t /*orientation*/) const {
83 // TODO(marissaw): send the transform hint to buffer owner
84 return;
85}
86
87void BufferStateLayer::releasePendingBuffer(nsecs_t /*dequeueReadyTime*/) {
Marissa Wallefb71af2019-06-27 14:45:53 -070088 mFlinger->getTransactionCompletedThread().finalizePendingCallbackHandles(
Marissa Wall5a68a772018-12-22 17:43:42 -080089 mDrawingState.callbackHandles);
90
91 mDrawingState.callbackHandles = {};
Marissa Wall61c58622018-07-18 10:12:20 -070092}
93
Ana Krulec010d2192018-10-08 06:29:54 -070094bool BufferStateLayer::shouldPresentNow(nsecs_t /*expectedPresentTime*/) const {
Marissa Wall61c58622018-07-18 10:12:20 -070095 if (getSidebandStreamChanged() || getAutoRefresh()) {
96 return true;
97 }
98
Marissa Wall024a1912018-08-13 13:55:35 -070099 return hasFrameUpdate();
Marissa Wall61c58622018-07-18 10:12:20 -0700100}
101
Marissa Walle2ffb422018-10-12 11:33:52 -0700102bool BufferStateLayer::willPresentCurrentTransaction() const {
103 // Returns true if the most recent Transaction applied to CurrentState will be presented.
104 return getSidebandStreamChanged() || getAutoRefresh() ||
Valerie Hauaa194562019-02-05 16:21:38 -0800105 (mCurrentState.modified &&
106 (mCurrentState.buffer != nullptr || mCurrentState.bgColorLayer != nullptr));
Marissa Wall61c58622018-07-18 10:12:20 -0700107}
108
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800109bool BufferStateLayer::getTransformToDisplayInverse() const {
110 return mCurrentState.transformToDisplayInverse;
Marissa Wall61c58622018-07-18 10:12:20 -0700111}
112
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800113void BufferStateLayer::pushPendingState() {
114 if (!mCurrentState.modified) {
Marissa Wall61c58622018-07-18 10:12:20 -0700115 return;
116 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800117 mPendingStates.push_back(mCurrentState);
118 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Marissa Wall61c58622018-07-18 10:12:20 -0700119}
120
121bool BufferStateLayer::applyPendingStates(Layer::State* stateToCommit) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800122 const bool stateUpdateAvailable = !mPendingStates.empty();
123 while (!mPendingStates.empty()) {
Marissa Wall61c58622018-07-18 10:12:20 -0700124 popPendingState(stateToCommit);
125 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800126 mCurrentStateModified = stateUpdateAvailable && mCurrentState.modified;
127 mCurrentState.modified = false;
Marissa Wall61c58622018-07-18 10:12:20 -0700128 return stateUpdateAvailable;
129}
130
Marissa Wall861616d2018-10-22 12:52:23 -0700131// Crop that applies to the window
132Rect BufferStateLayer::getCrop(const Layer::State& /*s*/) const {
133 return Rect::INVALID_RECT;
Marissa Wall61c58622018-07-18 10:12:20 -0700134}
135
136bool BufferStateLayer::setTransform(uint32_t transform) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800137 if (mCurrentState.transform == transform) return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800138 mCurrentState.transform = transform;
139 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700140 setTransactionFlags(eTransactionNeeded);
141 return true;
142}
143
144bool BufferStateLayer::setTransformToDisplayInverse(bool transformToDisplayInverse) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800145 if (mCurrentState.transformToDisplayInverse == transformToDisplayInverse) return false;
146 mCurrentState.sequence++;
147 mCurrentState.transformToDisplayInverse = transformToDisplayInverse;
148 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700149 setTransactionFlags(eTransactionNeeded);
150 return true;
151}
152
153bool BufferStateLayer::setCrop(const Rect& crop) {
Marissa Wall290ad082019-03-06 13:23:47 -0800154 Rect c = crop;
155 if (c.left < 0) {
156 c.left = 0;
157 }
158 if (c.top < 0) {
159 c.top = 0;
160 }
161 // If the width and/or height are < 0, make it [0, 0, -1, -1] so the equality comparision below
162 // treats all invalid rectangles the same.
163 if (!c.isValid()) {
164 c.makeInvalid();
165 }
166
167 if (mCurrentState.crop == c) return false;
Marissa Wall290ad082019-03-06 13:23:47 -0800168 mCurrentState.crop = c;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800169 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700170 setTransactionFlags(eTransactionNeeded);
171 return true;
172}
173
Marissa Wall861616d2018-10-22 12:52:23 -0700174bool BufferStateLayer::setFrame(const Rect& frame) {
175 int x = frame.left;
176 int y = frame.top;
177 int w = frame.getWidth();
178 int h = frame.getHeight();
179
Marissa Wall0f3242d2018-12-20 15:10:22 -0800180 if (x < 0) {
181 x = 0;
182 w = frame.right;
183 }
184
185 if (y < 0) {
186 y = 0;
187 h = frame.bottom;
188 }
189
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800190 if (mCurrentState.active.transform.tx() == x && mCurrentState.active.transform.ty() == y &&
191 mCurrentState.active.w == w && mCurrentState.active.h == h) {
Marissa Wall861616d2018-10-22 12:52:23 -0700192 return false;
193 }
194
195 if (!frame.isValid()) {
196 x = y = w = h = 0;
197 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800198 mCurrentState.active.transform.set(x, y);
199 mCurrentState.active.w = w;
200 mCurrentState.active.h = h;
Marissa Wall861616d2018-10-22 12:52:23 -0700201
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800202 mCurrentState.sequence++;
203 mCurrentState.modified = true;
Marissa Wall861616d2018-10-22 12:52:23 -0700204 setTransactionFlags(eTransactionNeeded);
205 return true;
206}
207
Ady Abraham09bd3922019-04-08 10:44:56 -0700208bool BufferStateLayer::setBuffer(const sp<GraphicBuffer>& buffer, nsecs_t postTime,
Marissa Wall947d34e2019-03-29 14:03:53 -0700209 nsecs_t desiredPresentTime, const client_cache_t& clientCacheId) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800210 if (mCurrentState.buffer) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700211 mReleasePreviousBuffer = true;
212 }
213
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800214 mCurrentState.buffer = buffer;
Marissa Wall947d34e2019-03-29 14:03:53 -0700215 mCurrentState.clientCacheId = clientCacheId;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800216 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700217 setTransactionFlags(eTransactionNeeded);
Ady Abraham09bd3922019-04-08 10:44:56 -0700218
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700219 mFlinger->mTimeStats->setPostTime(getSequence(), mFrameNumber, getName().c_str(), postTime);
Ady Abraham09bd3922019-04-08 10:44:56 -0700220 mDesiredPresentTime = desiredPresentTime;
221
222 if (mFlinger->mUseSmart90ForVideo) {
223 const nsecs_t presentTime = (mDesiredPresentTime == -1) ? 0 : mDesiredPresentTime;
Ady Abrahama315ce72019-04-24 14:35:20 -0700224 mFlinger->mScheduler->addLayerPresentTimeAndHDR(mSchedulerLayerHandle, presentTime,
225 mCurrentState.hdrMetadata.validTypes != 0);
Ady Abraham09bd3922019-04-08 10:44:56 -0700226 }
227
Marissa Wall61c58622018-07-18 10:12:20 -0700228 return true;
229}
230
231bool BufferStateLayer::setAcquireFence(const sp<Fence>& fence) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700232 // The acquire fences of BufferStateLayers have already signaled before they are set
233 mCallbackHandleAcquireTime = fence->getSignalTime();
234
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800235 mCurrentState.acquireFence = fence;
236 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700237 setTransactionFlags(eTransactionNeeded);
238 return true;
239}
240
241bool BufferStateLayer::setDataspace(ui::Dataspace dataspace) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800242 if (mCurrentState.dataspace == dataspace) return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800243 mCurrentState.dataspace = dataspace;
244 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700245 setTransactionFlags(eTransactionNeeded);
246 return true;
247}
248
249bool BufferStateLayer::setHdrMetadata(const HdrMetadata& hdrMetadata) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800250 if (mCurrentState.hdrMetadata == hdrMetadata) return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800251 mCurrentState.hdrMetadata = hdrMetadata;
252 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700253 setTransactionFlags(eTransactionNeeded);
254 return true;
255}
256
257bool BufferStateLayer::setSurfaceDamageRegion(const Region& surfaceDamage) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800258 mCurrentState.surfaceDamageRegion = surfaceDamage;
259 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700260 setTransactionFlags(eTransactionNeeded);
261 return true;
262}
263
264bool BufferStateLayer::setApi(int32_t api) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800265 if (mCurrentState.api == api) return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800266 mCurrentState.api = api;
267 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700268 setTransactionFlags(eTransactionNeeded);
269 return true;
270}
271
272bool BufferStateLayer::setSidebandStream(const sp<NativeHandle>& sidebandStream) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800273 if (mCurrentState.sidebandStream == sidebandStream) return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800274 mCurrentState.sidebandStream = sidebandStream;
275 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700276 setTransactionFlags(eTransactionNeeded);
277
278 if (!mSidebandStreamChanged.exchange(true)) {
279 // mSidebandStreamChanged was false
280 mFlinger->signalLayerUpdate();
281 }
282 return true;
283}
284
Marissa Walle2ffb422018-10-12 11:33:52 -0700285bool BufferStateLayer::setTransactionCompletedListeners(
286 const std::vector<sp<CallbackHandle>>& handles) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700287 // If there is no handle, we will not send a callback so reset mReleasePreviousBuffer and return
Marissa Walle2ffb422018-10-12 11:33:52 -0700288 if (handles.empty()) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700289 mReleasePreviousBuffer = false;
Marissa Walle2ffb422018-10-12 11:33:52 -0700290 return false;
291 }
292
293 const bool willPresent = willPresentCurrentTransaction();
294
295 for (const auto& handle : handles) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700296 // If this transaction set a buffer on this layer, release its previous buffer
297 handle->releasePreviousBuffer = mReleasePreviousBuffer;
298
Marissa Walle2ffb422018-10-12 11:33:52 -0700299 // If this layer will be presented in this frame
300 if (willPresent) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700301 // If this transaction set an acquire fence on this layer, set its acquire time
302 handle->acquireTime = mCallbackHandleAcquireTime;
303
Marissa Walle2ffb422018-10-12 11:33:52 -0700304 // Notify the transaction completed thread that there is a pending latched callback
305 // handle
Marissa Wall5a68a772018-12-22 17:43:42 -0800306 mFlinger->getTransactionCompletedThread().registerPendingCallbackHandle(handle);
Marissa Walle2ffb422018-10-12 11:33:52 -0700307
308 // Store so latched time and release fence can be set
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800309 mCurrentState.callbackHandles.push_back(handle);
Marissa Walle2ffb422018-10-12 11:33:52 -0700310
311 } else { // If this layer will NOT need to be relatched and presented this frame
312 // Notify the transaction completed thread this handle is done
Marissa Wallefb71af2019-06-27 14:45:53 -0700313 mFlinger->getTransactionCompletedThread().registerUnpresentedCallbackHandle(handle);
Marissa Walle2ffb422018-10-12 11:33:52 -0700314 }
315 }
316
Marissa Wallfda30bb2018-10-12 11:34:28 -0700317 mReleasePreviousBuffer = false;
318 mCallbackHandleAcquireTime = -1;
319
Marissa Walle2ffb422018-10-12 11:33:52 -0700320 return willPresent;
321}
322
Marissa Wall61c58622018-07-18 10:12:20 -0700323bool BufferStateLayer::setTransparentRegionHint(const Region& transparent) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800324 mCurrentState.transparentRegionHint = transparent;
325 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700326 setTransactionFlags(eTransactionNeeded);
327 return true;
328}
329
Marissa Wall861616d2018-10-22 12:52:23 -0700330Rect BufferStateLayer::getBufferSize(const State& s) const {
331 // for buffer state layers we use the display frame size as the buffer size.
332 if (getActiveWidth(s) < UINT32_MAX && getActiveHeight(s) < UINT32_MAX) {
333 return Rect(getActiveWidth(s), getActiveHeight(s));
Marissa Wall61c58622018-07-18 10:12:20 -0700334 }
335
Marissa Wall861616d2018-10-22 12:52:23 -0700336 // if the display frame is not defined, use the parent bounds as the buffer size.
337 const auto& p = mDrawingParent.promote();
338 if (p != nullptr) {
Vishnu Nair4351ad52019-02-11 14:13:02 -0800339 Rect parentBounds = Rect(p->getBounds(Region()));
Marissa Wall861616d2018-10-22 12:52:23 -0700340 if (!parentBounds.isEmpty()) {
341 return parentBounds;
342 }
343 }
344
Marissa Wall861616d2018-10-22 12:52:23 -0700345 return Rect::INVALID_RECT;
Marissa Wall61c58622018-07-18 10:12:20 -0700346}
Vishnu Nair4351ad52019-02-11 14:13:02 -0800347
348FloatRect BufferStateLayer::computeSourceBounds(const FloatRect& parentBounds) const {
349 const State& s(getDrawingState());
350 // for buffer state layers we use the display frame size as the buffer size.
351 if (getActiveWidth(s) < UINT32_MAX && getActiveHeight(s) < UINT32_MAX) {
352 return FloatRect(0, 0, getActiveWidth(s), getActiveHeight(s));
353 }
354
355 // if the display frame is not defined, use the parent bounds as the buffer size.
356 return parentBounds;
357}
358
Marissa Wall61c58622018-07-18 10:12:20 -0700359// -----------------------------------------------------------------------
360
361// -----------------------------------------------------------------------
362// Interface implementation for BufferLayer
363// -----------------------------------------------------------------------
364bool BufferStateLayer::fenceHasSignaled() const {
365 if (latchUnsignaledBuffers()) {
366 return true;
367 }
368
369 return getDrawingState().acquireFence->getStatus() == Fence::Status::Signaled;
370}
371
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700372bool BufferStateLayer::framePresentTimeIsCurrent(nsecs_t expectedPresentTime) const {
Ady Abrahamcd1580c2019-04-29 15:40:03 -0700373 if (!hasFrameUpdate() || isRemovedFromCurrentState()) {
374 return true;
375 }
376
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700377 return mDesiredPresentTime <= expectedPresentTime;
Ady Abrahamcd1580c2019-04-29 15:40:03 -0700378}
379
Marissa Wall61c58622018-07-18 10:12:20 -0700380nsecs_t BufferStateLayer::getDesiredPresentTime() {
Valerie Haubc6ddb12019-03-08 11:10:15 -0800381 return mDesiredPresentTime;
Marissa Wall61c58622018-07-18 10:12:20 -0700382}
383
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800384std::shared_ptr<FenceTime> BufferStateLayer::getCurrentFenceTime() const {
Marissa Wall61c58622018-07-18 10:12:20 -0700385 return std::make_shared<FenceTime>(getDrawingState().acquireFence);
386}
387
388void BufferStateLayer::getDrawingTransformMatrix(float *matrix) {
389 std::copy(std::begin(mTransformMatrix), std::end(mTransformMatrix), matrix);
390}
391
392uint32_t BufferStateLayer::getDrawingTransform() const {
393 return getDrawingState().transform;
394}
395
396ui::Dataspace BufferStateLayer::getDrawingDataSpace() const {
397 return getDrawingState().dataspace;
398}
399
Marissa Wall861616d2018-10-22 12:52:23 -0700400// Crop that applies to the buffer
Marissa Wall61c58622018-07-18 10:12:20 -0700401Rect BufferStateLayer::getDrawingCrop() const {
Marissa Wall861616d2018-10-22 12:52:23 -0700402 const State& s(getDrawingState());
403
404 if (s.crop.isEmpty() && s.buffer) {
405 return s.buffer->getBounds();
Valerie Hau0bc09152018-12-20 07:42:47 -0800406 } else if (s.buffer) {
407 Rect crop = s.crop;
408 crop.left = std::max(crop.left, 0);
409 crop.top = std::max(crop.top, 0);
410 uint32_t bufferWidth = s.buffer->getWidth();
411 uint32_t bufferHeight = s.buffer->getHeight();
412 if (bufferHeight <= std::numeric_limits<int32_t>::max() &&
413 bufferWidth <= std::numeric_limits<int32_t>::max()) {
414 crop.right = std::min(crop.right, static_cast<int32_t>(bufferWidth));
415 crop.bottom = std::min(crop.bottom, static_cast<int32_t>(bufferHeight));
416 }
417 if (!crop.isValid()) {
418 // Crop rect is out of bounds, return whole buffer
419 return s.buffer->getBounds();
420 }
421 return crop;
Marissa Wall861616d2018-10-22 12:52:23 -0700422 }
423 return s.crop;
Marissa Wall61c58622018-07-18 10:12:20 -0700424}
425
426uint32_t BufferStateLayer::getDrawingScalingMode() const {
Marissa Wallec463ac2018-10-08 12:35:04 -0700427 return NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW;
Marissa Wall61c58622018-07-18 10:12:20 -0700428}
429
430Region BufferStateLayer::getDrawingSurfaceDamage() const {
Marissa Wall0ef8d602019-04-23 14:09:28 -0700431 return getDrawingState().surfaceDamageRegion;
Marissa Wall61c58622018-07-18 10:12:20 -0700432}
433
434const HdrMetadata& BufferStateLayer::getDrawingHdrMetadata() const {
435 return getDrawingState().hdrMetadata;
436}
437
438int BufferStateLayer::getDrawingApi() const {
439 return getDrawingState().api;
440}
441
442PixelFormat BufferStateLayer::getPixelFormat() const {
Marissa Wall5aec6412018-11-14 11:49:18 -0800443 if (!mActiveBuffer) {
444 return PIXEL_FORMAT_NONE;
445 }
Marissa Wall61c58622018-07-18 10:12:20 -0700446 return mActiveBuffer->format;
447}
448
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700449uint64_t BufferStateLayer::getFrameNumber(nsecs_t /*expectedPresentTime*/) const {
Marissa Wall61c58622018-07-18 10:12:20 -0700450 return mFrameNumber;
451}
452
453bool BufferStateLayer::getAutoRefresh() const {
454 // TODO(marissaw): support shared buffer mode
455 return false;
456}
457
458bool BufferStateLayer::getSidebandStreamChanged() const {
459 return mSidebandStreamChanged.load();
460}
461
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800462bool BufferStateLayer::latchSidebandStream(bool& recomputeVisibleRegions) {
Marissa Wall61c58622018-07-18 10:12:20 -0700463 if (mSidebandStreamChanged.exchange(false)) {
464 const State& s(getDrawingState());
465 // mSidebandStreamChanged was true
Lloyd Pique0b785d82018-12-04 17:25:27 -0800466 LOG_ALWAYS_FATAL_IF(!getCompositionLayer());
467 mSidebandStream = s.sidebandStream;
468 getCompositionLayer()->editState().frontEnd.sidebandStream = mSidebandStream;
469 if (mSidebandStream != nullptr) {
Marissa Wall61c58622018-07-18 10:12:20 -0700470 setTransactionFlags(eTransactionNeeded);
471 mFlinger->setTransactionFlags(eTraversalNeeded);
472 }
473 recomputeVisibleRegions = true;
474
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800475 return true;
Marissa Wall61c58622018-07-18 10:12:20 -0700476 }
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800477 return false;
Marissa Wall61c58622018-07-18 10:12:20 -0700478}
479
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800480bool BufferStateLayer::hasFrameUpdate() const {
Valerie Hauaa194562019-02-05 16:21:38 -0800481 const State& c(getCurrentState());
482 return mCurrentStateModified && (c.buffer != nullptr || c.bgColorLayer != nullptr);
Marissa Wall61c58622018-07-18 10:12:20 -0700483}
484
485void BufferStateLayer::setFilteringEnabled(bool enabled) {
486 GLConsumer::computeTransformMatrix(mTransformMatrix.data(), mActiveBuffer, mCurrentCrop,
487 mCurrentTransform, enabled);
488}
489
Alec Mouri39801c02018-10-10 10:44:47 -0700490status_t BufferStateLayer::bindTextureImage() {
Marissa Wall61c58622018-07-18 10:12:20 -0700491 const State& s(getDrawingState());
492 auto& engine(mFlinger->getRenderEngine());
493
Alec Mourib5c4f352019-02-19 19:46:38 -0800494 return engine.bindExternalTextureBuffer(mTextureName, s.buffer, s.acquireFence);
Marissa Wall61c58622018-07-18 10:12:20 -0700495}
496
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700497status_t BufferStateLayer::updateTexImage(bool& /*recomputeVisibleRegions*/, nsecs_t latchTime,
498 nsecs_t /*expectedPresentTime*/) {
Marissa Wall61c58622018-07-18 10:12:20 -0700499 const State& s(getDrawingState());
500
501 if (!s.buffer) {
Valerie Hauaa194562019-02-05 16:21:38 -0800502 if (s.bgColorLayer) {
503 for (auto& handle : mDrawingState.callbackHandles) {
504 handle->latchTime = latchTime;
505 }
506 }
Marissa Wall61c58622018-07-18 10:12:20 -0700507 return NO_ERROR;
508 }
509
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700510 const int32_t layerID = getSequence();
511
Marissa Wall61c58622018-07-18 10:12:20 -0700512 // Reject if the layer is invalid
513 uint32_t bufferWidth = s.buffer->width;
514 uint32_t bufferHeight = s.buffer->height;
515
Peiyong Linefefaac2018-08-17 12:27:51 -0700516 if (s.transform & ui::Transform::ROT_90) {
Peiyong Lin3db42342018-08-16 09:15:59 -0700517 std::swap(bufferWidth, bufferHeight);
Marissa Wall61c58622018-07-18 10:12:20 -0700518 }
519
520 if (s.transformToDisplayInverse) {
521 uint32_t invTransform = DisplayDevice::getPrimaryDisplayOrientationTransform();
Peiyong Linefefaac2018-08-17 12:27:51 -0700522 if (invTransform & ui::Transform::ROT_90) {
Peiyong Lin3db42342018-08-16 09:15:59 -0700523 std::swap(bufferWidth, bufferHeight);
Marissa Wall61c58622018-07-18 10:12:20 -0700524 }
525 }
526
Vishnu Nair60356342018-11-13 13:00:45 -0800527 if (getEffectiveScalingMode() == NATIVE_WINDOW_SCALING_MODE_FREEZE &&
Marissa Wall61c58622018-07-18 10:12:20 -0700528 (s.active.w != bufferWidth || s.active.h != bufferHeight)) {
529 ALOGE("[%s] rejecting buffer: "
530 "bufferWidth=%d, bufferHeight=%d, front.active.{w=%d, h=%d}",
531 mName.string(), bufferWidth, bufferHeight, s.active.w, s.active.h);
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700532 mFlinger->mTimeStats->removeTimeRecord(layerID, mFrameNumber);
Marissa Wall61c58622018-07-18 10:12:20 -0700533 return BAD_VALUE;
534 }
535
Marissa Wall5a68a772018-12-22 17:43:42 -0800536 for (auto& handle : mDrawingState.callbackHandles) {
537 handle->latchTime = latchTime;
538 }
Marissa Walle2ffb422018-10-12 11:33:52 -0700539
Alec Mouri56e538f2019-01-14 15:22:01 -0800540 if (!SyncFeatures::getInstance().useNativeFenceSync()) {
Marissa Wall61c58622018-07-18 10:12:20 -0700541 // Bind the new buffer to the GL texture.
542 //
543 // Older devices require the "implicit" synchronization provided
544 // by glEGLImageTargetTexture2DOES, which this method calls. Newer
545 // devices will either call this in Layer::onDraw, or (if it's not
546 // a GL-composited layer) not at all.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800547 status_t err = bindTextureImage();
Marissa Wall61c58622018-07-18 10:12:20 -0700548 if (err != NO_ERROR) {
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800549 mFlinger->mTimeStats->onDestroy(layerID);
Marissa Wall61c58622018-07-18 10:12:20 -0700550 return BAD_VALUE;
551 }
552 }
553
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700554 mFlinger->mTimeStats->setAcquireFence(layerID, mFrameNumber, getCurrentFenceTime());
555 mFlinger->mTimeStats->setLatchTime(layerID, mFrameNumber, latchTime);
Marissa Wall61c58622018-07-18 10:12:20 -0700556
Marissa Wall16c112d2019-03-20 13:21:13 -0700557 mCurrentStateModified = false;
558
Marissa Wall61c58622018-07-18 10:12:20 -0700559 return NO_ERROR;
560}
561
562status_t BufferStateLayer::updateActiveBuffer() {
563 const State& s(getDrawingState());
564
565 if (s.buffer == nullptr) {
566 return BAD_VALUE;
567 }
568
569 mActiveBuffer = s.buffer;
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000570 mActiveBufferFence = s.acquireFence;
Lloyd Pique0b785d82018-12-04 17:25:27 -0800571 auto& layerCompositionState = getCompositionLayer()->editState().frontEnd;
572 layerCompositionState.buffer = mActiveBuffer;
573 layerCompositionState.bufferSlot = 0;
Marissa Wall61c58622018-07-18 10:12:20 -0700574
575 return NO_ERROR;
576}
577
578status_t BufferStateLayer::updateFrameNumber(nsecs_t /*latchTime*/) {
579 // TODO(marissaw): support frame history events
580 mCurrentFrameNumber = mFrameNumber;
581 return NO_ERROR;
582}
583
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800584void BufferStateLayer::setHwcLayerBuffer(const sp<const DisplayDevice>& display) {
585 const auto outputLayer = findOutputLayerForDisplay(display);
586 LOG_FATAL_IF(!outputLayer || !outputLayer->getState().hwc);
Valerie Hau6f89c372019-03-02 00:13:33 +0000587 auto& hwcInfo = *outputLayer->editState().hwc;
588 auto& hwcLayer = hwcInfo.hwcLayer;
Marissa Wall61c58622018-07-18 10:12:20 -0700589
590 const State& s(getDrawingState());
591
Valerie Hau13f0d1a2019-03-22 10:35:42 -0700592 uint32_t hwcSlot;
Valerie Hau6f89c372019-03-02 00:13:33 +0000593 sp<GraphicBuffer> buffer;
Marissa Wall947d34e2019-03-29 14:03:53 -0700594 hwcInfo.hwcBufferCache.getHwcBuffer(mHwcSlotGenerator->getHwcCacheSlot(s.clientCacheId),
595 s.buffer, &hwcSlot, &buffer);
Marissa Wall61c58622018-07-18 10:12:20 -0700596
Valerie Hau6f89c372019-03-02 00:13:33 +0000597 auto error = hwcLayer->setBuffer(hwcSlot, buffer, s.acquireFence);
Marissa Wall61c58622018-07-18 10:12:20 -0700598 if (error != HWC2::Error::None) {
599 ALOGE("[%s] Failed to set buffer %p: %s (%d)", mName.string(),
600 s.buffer->handle, to_string(error).c_str(), static_cast<int32_t>(error));
601 }
602
603 mFrameNumber++;
604}
605
606void BufferStateLayer::onFirstRef() {
Dan Stoza7b1b5a82018-07-31 16:00:21 -0700607 BufferLayer::onFirstRef();
608
Marissa Wall61c58622018-07-18 10:12:20 -0700609 if (const auto display = mFlinger->getDefaultDisplayDevice()) {
610 updateTransformHint(display);
611 }
612}
613
Alec Mouri1c8d7202019-06-01 18:51:35 -0700614void BufferStateLayer::bufferErased(const client_cache_t& clientCacheId) {
615 mFlinger->getRenderEngine().unbindExternalTextureBuffer(clientCacheId.id);
616}
617
Marissa Wall947d34e2019-03-29 14:03:53 -0700618void BufferStateLayer::HwcSlotGenerator::bufferErased(const client_cache_t& clientCacheId) {
619 std::lock_guard lock(mMutex);
620 if (!clientCacheId.isValid()) {
621 ALOGE("invalid process, failed to erase buffer");
622 return;
623 }
624 eraseBufferLocked(clientCacheId);
625}
626
627uint32_t BufferStateLayer::HwcSlotGenerator::getHwcCacheSlot(const client_cache_t& clientCacheId) {
628 std::lock_guard<std::mutex> lock(mMutex);
629 auto itr = mCachedBuffers.find(clientCacheId);
630 if (itr == mCachedBuffers.end()) {
631 return addCachedBuffer(clientCacheId);
632 }
633 auto& [hwcCacheSlot, counter] = itr->second;
634 counter = mCounter++;
635 return hwcCacheSlot;
636}
637
638uint32_t BufferStateLayer::HwcSlotGenerator::addCachedBuffer(const client_cache_t& clientCacheId)
639 REQUIRES(mMutex) {
640 if (!clientCacheId.isValid()) {
641 ALOGE("invalid process, returning invalid slot");
642 return BufferQueue::INVALID_BUFFER_SLOT;
643 }
644
645 ClientCache::getInstance().registerErasedRecipient(clientCacheId, wp<ErasedRecipient>(this));
646
647 uint32_t hwcCacheSlot = getFreeHwcCacheSlot();
648 mCachedBuffers[clientCacheId] = {hwcCacheSlot, mCounter++};
649 return hwcCacheSlot;
650}
651
652uint32_t BufferStateLayer::HwcSlotGenerator::getFreeHwcCacheSlot() REQUIRES(mMutex) {
653 if (mFreeHwcCacheSlots.empty()) {
654 evictLeastRecentlyUsed();
655 }
656
657 uint32_t hwcCacheSlot = mFreeHwcCacheSlots.top();
658 mFreeHwcCacheSlots.pop();
659 return hwcCacheSlot;
660}
661
662void BufferStateLayer::HwcSlotGenerator::evictLeastRecentlyUsed() REQUIRES(mMutex) {
663 uint64_t minCounter = UINT_MAX;
664 client_cache_t minClientCacheId = {};
665 for (const auto& [clientCacheId, slotCounter] : mCachedBuffers) {
666 const auto& [hwcCacheSlot, counter] = slotCounter;
667 if (counter < minCounter) {
668 minCounter = counter;
669 minClientCacheId = clientCacheId;
670 }
671 }
672 eraseBufferLocked(minClientCacheId);
673
674 ClientCache::getInstance().unregisterErasedRecipient(minClientCacheId, this);
675}
676
677void BufferStateLayer::HwcSlotGenerator::eraseBufferLocked(const client_cache_t& clientCacheId)
678 REQUIRES(mMutex) {
679 auto itr = mCachedBuffers.find(clientCacheId);
680 if (itr == mCachedBuffers.end()) {
681 return;
682 }
683 auto& [hwcCacheSlot, counter] = itr->second;
684
685 // TODO send to hwc cache and resources
686
687 mFreeHwcCacheSlots.push(hwcCacheSlot);
688 mCachedBuffers.erase(clientCacheId);
689}
Marissa Wall61c58622018-07-18 10:12:20 -0700690} // namespace android