blob: 07a83b9d35889d8bc031d0e929e0629c80f64d21 [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"
Mikael Pessa90092f42019-08-26 17:22:04 -070035#include "FrameTracer/FrameTracer.h"
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080036#include "TimeStats/TimeStats.h"
Valerie Hau0bc09152018-12-20 07:42:47 -080037
Marissa Wall61c58622018-07-18 10:12:20 -070038namespace android {
39
Lloyd Pique42ab75e2018-09-12 20:46:03 -070040// clang-format off
41const std::array<float, 16> BufferStateLayer::IDENTITY_MATRIX{
42 1, 0, 0, 0,
43 0, 1, 0, 0,
44 0, 0, 1, 0,
45 0, 0, 0, 1
46};
47// clang-format on
Marissa Wall61c58622018-07-18 10:12:20 -070048
Marissa Wall947d34e2019-03-29 14:03:53 -070049BufferStateLayer::BufferStateLayer(const LayerCreationArgs& args)
50 : BufferLayer(args), mHwcSlotGenerator(new HwcSlotGenerator()) {
Vishnu Nair60356342018-11-13 13:00:45 -080051 mOverrideScalingMode = NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW;
Marissa Wall3ff826c2019-02-07 11:58:25 -080052 mCurrentState.dataspace = ui::Dataspace::V0_SRGB;
Vishnu Nair60356342018-11-13 13:00:45 -080053}
Marissa Wall61c58622018-07-18 10:12:20 -070054
Alec Mouri4545a8a2019-08-08 20:05:32 -070055BufferStateLayer::~BufferStateLayer() {
56 if (mActiveBuffer != nullptr) {
57 // Ensure that mActiveBuffer is uncached from RenderEngine here, as
58 // RenderEngine may have been using the buffer as an external texture
59 // after the client uncached the buffer.
60 auto& engine(mFlinger->getRenderEngine());
61 engine.unbindExternalTextureBuffer(mActiveBuffer->getId());
62 }
63}
64
Marissa Wall61c58622018-07-18 10:12:20 -070065// -----------------------------------------------------------------------
66// Interface implementation for Layer
67// -----------------------------------------------------------------------
Marissa Wallfda30bb2018-10-12 11:34:28 -070068void BufferStateLayer::onLayerDisplayed(const sp<Fence>& releaseFence) {
Marissa Wall5a68a772018-12-22 17:43:42 -080069 // The previous release fence notifies the client that SurfaceFlinger is done with the previous
70 // buffer that was presented on this layer. The first transaction that came in this frame that
71 // replaced the previous buffer on this layer needs this release fence, because the fence will
72 // let the client know when that previous buffer is removed from the screen.
73 //
74 // Every other transaction on this layer does not need a release fence because no other
75 // Transactions that were set on this layer this frame are going to have their preceeding buffer
76 // removed from the display this frame.
77 //
78 // For example, if we have 3 transactions this frame. The first transaction doesn't contain a
79 // buffer so it doesn't need a previous release fence because the layer still needs the previous
80 // buffer. The second transaction contains a buffer so it needs a previous release fence because
81 // the previous buffer will be released this frame. The third transaction also contains a
82 // buffer. It replaces the buffer in the second transaction. The buffer in the second
83 // transaction will now no longer be presented so it is released immediately and the third
84 // transaction doesn't need a previous release fence.
85 for (auto& handle : mDrawingState.callbackHandles) {
86 if (handle->releasePreviousBuffer) {
87 handle->previousReleaseFence = releaseFence;
88 break;
89 }
90 }
Mikael Pessa2e1608f2019-07-19 11:25:35 -070091
92 // Prevent tracing the same release multiple times.
93 if (mPreviousFrameNumber != mPreviousReleasedFrameNumber) {
Mikael Pessa90092f42019-08-26 17:22:04 -070094 mFlinger->mFrameTracer->traceFence(getSequence(), mPreviousBufferId, mPreviousFrameNumber,
95 std::make_shared<FenceTime>(releaseFence),
96 FrameTracer::FrameEvent::RELEASE_FENCE);
Mikael Pessa2e1608f2019-07-19 11:25:35 -070097 mPreviousReleasedFrameNumber = mPreviousFrameNumber;
98 }
Marissa Wall61c58622018-07-18 10:12:20 -070099}
100
101void BufferStateLayer::setTransformHint(uint32_t /*orientation*/) const {
102 // TODO(marissaw): send the transform hint to buffer owner
103 return;
104}
105
106void BufferStateLayer::releasePendingBuffer(nsecs_t /*dequeueReadyTime*/) {
Marissa Wallefb71af2019-06-27 14:45:53 -0700107 mFlinger->getTransactionCompletedThread().finalizePendingCallbackHandles(
Marissa Wall5a68a772018-12-22 17:43:42 -0800108 mDrawingState.callbackHandles);
109
110 mDrawingState.callbackHandles = {};
Marissa Wall61c58622018-07-18 10:12:20 -0700111}
112
Ana Krulec010d2192018-10-08 06:29:54 -0700113bool BufferStateLayer::shouldPresentNow(nsecs_t /*expectedPresentTime*/) const {
Marissa Wall61c58622018-07-18 10:12:20 -0700114 if (getSidebandStreamChanged() || getAutoRefresh()) {
115 return true;
116 }
117
Marissa Wall024a1912018-08-13 13:55:35 -0700118 return hasFrameUpdate();
Marissa Wall61c58622018-07-18 10:12:20 -0700119}
120
Marissa Walle2ffb422018-10-12 11:33:52 -0700121bool BufferStateLayer::willPresentCurrentTransaction() const {
122 // Returns true if the most recent Transaction applied to CurrentState will be presented.
123 return getSidebandStreamChanged() || getAutoRefresh() ||
Valerie Hauaa194562019-02-05 16:21:38 -0800124 (mCurrentState.modified &&
125 (mCurrentState.buffer != nullptr || mCurrentState.bgColorLayer != nullptr));
Marissa Wall61c58622018-07-18 10:12:20 -0700126}
127
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800128void BufferStateLayer::pushPendingState() {
129 if (!mCurrentState.modified) {
Marissa Wall61c58622018-07-18 10:12:20 -0700130 return;
131 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800132 mPendingStates.push_back(mCurrentState);
133 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Marissa Wall61c58622018-07-18 10:12:20 -0700134}
135
136bool BufferStateLayer::applyPendingStates(Layer::State* stateToCommit) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800137 const bool stateUpdateAvailable = !mPendingStates.empty();
138 while (!mPendingStates.empty()) {
Marissa Wall61c58622018-07-18 10:12:20 -0700139 popPendingState(stateToCommit);
140 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800141 mCurrentStateModified = stateUpdateAvailable && mCurrentState.modified;
142 mCurrentState.modified = false;
Marissa Wall61c58622018-07-18 10:12:20 -0700143 return stateUpdateAvailable;
144}
145
Marissa Wall861616d2018-10-22 12:52:23 -0700146// Crop that applies to the window
147Rect BufferStateLayer::getCrop(const Layer::State& /*s*/) const {
148 return Rect::INVALID_RECT;
Marissa Wall61c58622018-07-18 10:12:20 -0700149}
150
151bool BufferStateLayer::setTransform(uint32_t transform) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800152 if (mCurrentState.transform == transform) return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800153 mCurrentState.transform = transform;
154 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700155 setTransactionFlags(eTransactionNeeded);
156 return true;
157}
158
159bool BufferStateLayer::setTransformToDisplayInverse(bool transformToDisplayInverse) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800160 if (mCurrentState.transformToDisplayInverse == transformToDisplayInverse) return false;
161 mCurrentState.sequence++;
162 mCurrentState.transformToDisplayInverse = transformToDisplayInverse;
163 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700164 setTransactionFlags(eTransactionNeeded);
165 return true;
166}
167
168bool BufferStateLayer::setCrop(const Rect& crop) {
Marissa Wall290ad082019-03-06 13:23:47 -0800169 Rect c = crop;
170 if (c.left < 0) {
171 c.left = 0;
172 }
173 if (c.top < 0) {
174 c.top = 0;
175 }
176 // If the width and/or height are < 0, make it [0, 0, -1, -1] so the equality comparision below
177 // treats all invalid rectangles the same.
178 if (!c.isValid()) {
179 c.makeInvalid();
180 }
181
182 if (mCurrentState.crop == c) return false;
Marissa Wall290ad082019-03-06 13:23:47 -0800183 mCurrentState.crop = c;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800184 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700185 setTransactionFlags(eTransactionNeeded);
186 return true;
187}
188
Marissa Wall861616d2018-10-22 12:52:23 -0700189bool BufferStateLayer::setFrame(const Rect& frame) {
190 int x = frame.left;
191 int y = frame.top;
192 int w = frame.getWidth();
193 int h = frame.getHeight();
194
Marissa Wall0f3242d2018-12-20 15:10:22 -0800195 if (x < 0) {
196 x = 0;
197 w = frame.right;
198 }
199
200 if (y < 0) {
201 y = 0;
202 h = frame.bottom;
203 }
204
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800205 if (mCurrentState.active.transform.tx() == x && mCurrentState.active.transform.ty() == y &&
206 mCurrentState.active.w == w && mCurrentState.active.h == h) {
Marissa Wall861616d2018-10-22 12:52:23 -0700207 return false;
208 }
209
210 if (!frame.isValid()) {
211 x = y = w = h = 0;
212 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800213 mCurrentState.active.transform.set(x, y);
214 mCurrentState.active.w = w;
215 mCurrentState.active.h = h;
Marissa Wall861616d2018-10-22 12:52:23 -0700216
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800217 mCurrentState.sequence++;
218 mCurrentState.modified = true;
Marissa Wall861616d2018-10-22 12:52:23 -0700219 setTransactionFlags(eTransactionNeeded);
220 return true;
221}
222
Ady Abraham09bd3922019-04-08 10:44:56 -0700223bool BufferStateLayer::setBuffer(const sp<GraphicBuffer>& buffer, nsecs_t postTime,
Marissa Wall947d34e2019-03-29 14:03:53 -0700224 nsecs_t desiredPresentTime, const client_cache_t& clientCacheId) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800225 if (mCurrentState.buffer) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700226 mReleasePreviousBuffer = true;
227 }
228
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800229 mCurrentState.buffer = buffer;
Marissa Wall947d34e2019-03-29 14:03:53 -0700230 mCurrentState.clientCacheId = clientCacheId;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800231 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700232 setTransactionFlags(eTransactionNeeded);
Ady Abraham09bd3922019-04-08 10:44:56 -0700233
Mikael Pessa2e1608f2019-07-19 11:25:35 -0700234 const int32_t layerID = getSequence();
235 mFlinger->mTimeStats->setPostTime(layerID, mFrameNumber, getName().c_str(), postTime);
Mikael Pessa90092f42019-08-26 17:22:04 -0700236 mFlinger->mFrameTracer->traceNewLayer(layerID, getName().c_str());
237 mFlinger->mFrameTracer->traceTimestamp(layerID, buffer->getId(), mFrameNumber, postTime,
238 FrameTracer::FrameEvent::POST);
chaviwfa67b552019-08-12 16:51:55 -0700239 mCurrentState.desiredPresentTime = desiredPresentTime;
Ady Abraham09bd3922019-04-08 10:44:56 -0700240
241 if (mFlinger->mUseSmart90ForVideo) {
chaviwfa67b552019-08-12 16:51:55 -0700242 const nsecs_t presentTime = (desiredPresentTime == -1) ? 0 : desiredPresentTime;
Ady Abrahama315ce72019-04-24 14:35:20 -0700243 mFlinger->mScheduler->addLayerPresentTimeAndHDR(mSchedulerLayerHandle, presentTime,
244 mCurrentState.hdrMetadata.validTypes != 0);
Ady Abraham09bd3922019-04-08 10:44:56 -0700245 }
246
Marissa Wall61c58622018-07-18 10:12:20 -0700247 return true;
248}
249
250bool BufferStateLayer::setAcquireFence(const sp<Fence>& fence) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700251 // The acquire fences of BufferStateLayers have already signaled before they are set
252 mCallbackHandleAcquireTime = fence->getSignalTime();
253
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800254 mCurrentState.acquireFence = fence;
255 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700256 setTransactionFlags(eTransactionNeeded);
257 return true;
258}
259
260bool BufferStateLayer::setDataspace(ui::Dataspace dataspace) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800261 if (mCurrentState.dataspace == dataspace) return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800262 mCurrentState.dataspace = dataspace;
263 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700264 setTransactionFlags(eTransactionNeeded);
265 return true;
266}
267
268bool BufferStateLayer::setHdrMetadata(const HdrMetadata& hdrMetadata) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800269 if (mCurrentState.hdrMetadata == hdrMetadata) return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800270 mCurrentState.hdrMetadata = hdrMetadata;
271 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700272 setTransactionFlags(eTransactionNeeded);
273 return true;
274}
275
276bool BufferStateLayer::setSurfaceDamageRegion(const Region& surfaceDamage) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800277 mCurrentState.surfaceDamageRegion = surfaceDamage;
278 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700279 setTransactionFlags(eTransactionNeeded);
280 return true;
281}
282
283bool BufferStateLayer::setApi(int32_t api) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800284 if (mCurrentState.api == api) return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800285 mCurrentState.api = api;
286 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700287 setTransactionFlags(eTransactionNeeded);
288 return true;
289}
290
291bool BufferStateLayer::setSidebandStream(const sp<NativeHandle>& sidebandStream) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800292 if (mCurrentState.sidebandStream == sidebandStream) return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800293 mCurrentState.sidebandStream = sidebandStream;
294 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700295 setTransactionFlags(eTransactionNeeded);
296
297 if (!mSidebandStreamChanged.exchange(true)) {
298 // mSidebandStreamChanged was false
299 mFlinger->signalLayerUpdate();
300 }
301 return true;
302}
303
Marissa Walle2ffb422018-10-12 11:33:52 -0700304bool BufferStateLayer::setTransactionCompletedListeners(
305 const std::vector<sp<CallbackHandle>>& handles) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700306 // If there is no handle, we will not send a callback so reset mReleasePreviousBuffer and return
Marissa Walle2ffb422018-10-12 11:33:52 -0700307 if (handles.empty()) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700308 mReleasePreviousBuffer = false;
Marissa Walle2ffb422018-10-12 11:33:52 -0700309 return false;
310 }
311
312 const bool willPresent = willPresentCurrentTransaction();
313
314 for (const auto& handle : handles) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700315 // If this transaction set a buffer on this layer, release its previous buffer
316 handle->releasePreviousBuffer = mReleasePreviousBuffer;
317
Marissa Walle2ffb422018-10-12 11:33:52 -0700318 // If this layer will be presented in this frame
319 if (willPresent) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700320 // If this transaction set an acquire fence on this layer, set its acquire time
321 handle->acquireTime = mCallbackHandleAcquireTime;
322
Marissa Walle2ffb422018-10-12 11:33:52 -0700323 // Notify the transaction completed thread that there is a pending latched callback
324 // handle
Marissa Wall5a68a772018-12-22 17:43:42 -0800325 mFlinger->getTransactionCompletedThread().registerPendingCallbackHandle(handle);
Marissa Walle2ffb422018-10-12 11:33:52 -0700326
327 // Store so latched time and release fence can be set
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800328 mCurrentState.callbackHandles.push_back(handle);
Marissa Walle2ffb422018-10-12 11:33:52 -0700329
330 } else { // If this layer will NOT need to be relatched and presented this frame
331 // Notify the transaction completed thread this handle is done
Marissa Wallefb71af2019-06-27 14:45:53 -0700332 mFlinger->getTransactionCompletedThread().registerUnpresentedCallbackHandle(handle);
Marissa Walle2ffb422018-10-12 11:33:52 -0700333 }
334 }
335
Marissa Wallfda30bb2018-10-12 11:34:28 -0700336 mReleasePreviousBuffer = false;
337 mCallbackHandleAcquireTime = -1;
338
Marissa Walle2ffb422018-10-12 11:33:52 -0700339 return willPresent;
340}
341
Marissa Wall61c58622018-07-18 10:12:20 -0700342bool BufferStateLayer::setTransparentRegionHint(const Region& transparent) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800343 mCurrentState.transparentRegionHint = transparent;
344 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700345 setTransactionFlags(eTransactionNeeded);
346 return true;
347}
348
Marissa Wall861616d2018-10-22 12:52:23 -0700349Rect BufferStateLayer::getBufferSize(const State& s) const {
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 Rect(getActiveWidth(s), getActiveHeight(s));
Marissa Wall61c58622018-07-18 10:12:20 -0700353 }
354
Marissa Wall861616d2018-10-22 12:52:23 -0700355 // if the display frame is not defined, use the parent bounds as the buffer size.
356 const auto& p = mDrawingParent.promote();
357 if (p != nullptr) {
Vishnu Nair4351ad52019-02-11 14:13:02 -0800358 Rect parentBounds = Rect(p->getBounds(Region()));
Marissa Wall861616d2018-10-22 12:52:23 -0700359 if (!parentBounds.isEmpty()) {
360 return parentBounds;
361 }
362 }
363
Marissa Wall861616d2018-10-22 12:52:23 -0700364 return Rect::INVALID_RECT;
Marissa Wall61c58622018-07-18 10:12:20 -0700365}
Vishnu Nair4351ad52019-02-11 14:13:02 -0800366
367FloatRect BufferStateLayer::computeSourceBounds(const FloatRect& parentBounds) const {
368 const State& s(getDrawingState());
369 // for buffer state layers we use the display frame size as the buffer size.
370 if (getActiveWidth(s) < UINT32_MAX && getActiveHeight(s) < UINT32_MAX) {
371 return FloatRect(0, 0, getActiveWidth(s), getActiveHeight(s));
372 }
373
374 // if the display frame is not defined, use the parent bounds as the buffer size.
375 return parentBounds;
376}
377
Marissa Wall61c58622018-07-18 10:12:20 -0700378// -----------------------------------------------------------------------
379
380// -----------------------------------------------------------------------
381// Interface implementation for BufferLayer
382// -----------------------------------------------------------------------
383bool BufferStateLayer::fenceHasSignaled() const {
384 if (latchUnsignaledBuffers()) {
385 return true;
386 }
387
388 return getDrawingState().acquireFence->getStatus() == Fence::Status::Signaled;
389}
390
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700391bool BufferStateLayer::framePresentTimeIsCurrent(nsecs_t expectedPresentTime) const {
Ady Abrahamcd1580c2019-04-29 15:40:03 -0700392 if (!hasFrameUpdate() || isRemovedFromCurrentState()) {
393 return true;
394 }
395
chaviwfa67b552019-08-12 16:51:55 -0700396 return mCurrentState.desiredPresentTime <= expectedPresentTime;
Ady Abrahamcd1580c2019-04-29 15:40:03 -0700397}
398
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700399uint64_t BufferStateLayer::getFrameNumber(nsecs_t /*expectedPresentTime*/) const {
Marissa Wall61c58622018-07-18 10:12:20 -0700400 return mFrameNumber;
401}
402
403bool BufferStateLayer::getAutoRefresh() const {
404 // TODO(marissaw): support shared buffer mode
405 return false;
406}
407
408bool BufferStateLayer::getSidebandStreamChanged() const {
409 return mSidebandStreamChanged.load();
410}
411
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800412bool BufferStateLayer::latchSidebandStream(bool& recomputeVisibleRegions) {
Marissa Wall61c58622018-07-18 10:12:20 -0700413 if (mSidebandStreamChanged.exchange(false)) {
414 const State& s(getDrawingState());
415 // mSidebandStreamChanged was true
Lloyd Pique0b785d82018-12-04 17:25:27 -0800416 LOG_ALWAYS_FATAL_IF(!getCompositionLayer());
417 mSidebandStream = s.sidebandStream;
418 getCompositionLayer()->editState().frontEnd.sidebandStream = mSidebandStream;
419 if (mSidebandStream != nullptr) {
Marissa Wall61c58622018-07-18 10:12:20 -0700420 setTransactionFlags(eTransactionNeeded);
421 mFlinger->setTransactionFlags(eTraversalNeeded);
422 }
423 recomputeVisibleRegions = true;
424
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800425 return true;
Marissa Wall61c58622018-07-18 10:12:20 -0700426 }
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800427 return false;
Marissa Wall61c58622018-07-18 10:12:20 -0700428}
429
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800430bool BufferStateLayer::hasFrameUpdate() const {
Valerie Hauaa194562019-02-05 16:21:38 -0800431 const State& c(getCurrentState());
432 return mCurrentStateModified && (c.buffer != nullptr || c.bgColorLayer != nullptr);
Marissa Wall61c58622018-07-18 10:12:20 -0700433}
434
435void BufferStateLayer::setFilteringEnabled(bool enabled) {
chaviw4244e032019-09-04 11:27:49 -0700436 GLConsumer::computeTransformMatrix(mTransformMatrix.data(), mActiveBuffer, mBufferInfo.mCrop,
437 mBufferInfo.mTransform, enabled);
Marissa Wall61c58622018-07-18 10:12:20 -0700438}
439
Alec Mouri39801c02018-10-10 10:44:47 -0700440status_t BufferStateLayer::bindTextureImage() {
Marissa Wall61c58622018-07-18 10:12:20 -0700441 const State& s(getDrawingState());
442 auto& engine(mFlinger->getRenderEngine());
443
Alec Mourib5c4f352019-02-19 19:46:38 -0800444 return engine.bindExternalTextureBuffer(mTextureName, s.buffer, s.acquireFence);
Marissa Wall61c58622018-07-18 10:12:20 -0700445}
446
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700447status_t BufferStateLayer::updateTexImage(bool& /*recomputeVisibleRegions*/, nsecs_t latchTime,
448 nsecs_t /*expectedPresentTime*/) {
Marissa Wall61c58622018-07-18 10:12:20 -0700449 const State& s(getDrawingState());
450
451 if (!s.buffer) {
Valerie Hauaa194562019-02-05 16:21:38 -0800452 if (s.bgColorLayer) {
453 for (auto& handle : mDrawingState.callbackHandles) {
454 handle->latchTime = latchTime;
455 }
456 }
Marissa Wall61c58622018-07-18 10:12:20 -0700457 return NO_ERROR;
458 }
459
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700460 const int32_t layerID = getSequence();
461
Marissa Wall61c58622018-07-18 10:12:20 -0700462 // Reject if the layer is invalid
463 uint32_t bufferWidth = s.buffer->width;
464 uint32_t bufferHeight = s.buffer->height;
465
Peiyong Linefefaac2018-08-17 12:27:51 -0700466 if (s.transform & ui::Transform::ROT_90) {
Peiyong Lin3db42342018-08-16 09:15:59 -0700467 std::swap(bufferWidth, bufferHeight);
Marissa Wall61c58622018-07-18 10:12:20 -0700468 }
469
470 if (s.transformToDisplayInverse) {
471 uint32_t invTransform = DisplayDevice::getPrimaryDisplayOrientationTransform();
Peiyong Linefefaac2018-08-17 12:27:51 -0700472 if (invTransform & ui::Transform::ROT_90) {
Peiyong Lin3db42342018-08-16 09:15:59 -0700473 std::swap(bufferWidth, bufferHeight);
Marissa Wall61c58622018-07-18 10:12:20 -0700474 }
475 }
476
Vishnu Nair60356342018-11-13 13:00:45 -0800477 if (getEffectiveScalingMode() == NATIVE_WINDOW_SCALING_MODE_FREEZE &&
Marissa Wall61c58622018-07-18 10:12:20 -0700478 (s.active.w != bufferWidth || s.active.h != bufferHeight)) {
479 ALOGE("[%s] rejecting buffer: "
480 "bufferWidth=%d, bufferHeight=%d, front.active.{w=%d, h=%d}",
481 mName.string(), bufferWidth, bufferHeight, s.active.w, s.active.h);
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700482 mFlinger->mTimeStats->removeTimeRecord(layerID, mFrameNumber);
Marissa Wall61c58622018-07-18 10:12:20 -0700483 return BAD_VALUE;
484 }
485
Marissa Wall5a68a772018-12-22 17:43:42 -0800486 for (auto& handle : mDrawingState.callbackHandles) {
487 handle->latchTime = latchTime;
488 }
Marissa Walle2ffb422018-10-12 11:33:52 -0700489
Alec Mouri56e538f2019-01-14 15:22:01 -0800490 if (!SyncFeatures::getInstance().useNativeFenceSync()) {
Marissa Wall61c58622018-07-18 10:12:20 -0700491 // Bind the new buffer to the GL texture.
492 //
493 // Older devices require the "implicit" synchronization provided
494 // by glEGLImageTargetTexture2DOES, which this method calls. Newer
495 // devices will either call this in Layer::onDraw, or (if it's not
496 // a GL-composited layer) not at all.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800497 status_t err = bindTextureImage();
Marissa Wall61c58622018-07-18 10:12:20 -0700498 if (err != NO_ERROR) {
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800499 mFlinger->mTimeStats->onDestroy(layerID);
Mikael Pessa90092f42019-08-26 17:22:04 -0700500 mFlinger->mFrameTracer->onDestroy(layerID);
Marissa Wall61c58622018-07-18 10:12:20 -0700501 return BAD_VALUE;
502 }
503 }
504
Mikael Pessa2e1608f2019-07-19 11:25:35 -0700505 const uint64_t bufferID = getCurrentBufferId();
chaviw4244e032019-09-04 11:27:49 -0700506 mFlinger->mTimeStats->setAcquireFence(layerID, mFrameNumber, mBufferInfo.mFenceTime);
507 mFlinger->mFrameTracer->traceFence(layerID, bufferID, mFrameNumber, mBufferInfo.mFenceTime,
Mikael Pessa90092f42019-08-26 17:22:04 -0700508 FrameTracer::FrameEvent::ACQUIRE_FENCE);
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700509 mFlinger->mTimeStats->setLatchTime(layerID, mFrameNumber, latchTime);
Mikael Pessa90092f42019-08-26 17:22:04 -0700510 mFlinger->mFrameTracer->traceTimestamp(layerID, bufferID, mFrameNumber, latchTime,
511 FrameTracer::FrameEvent::LATCH);
Marissa Wall61c58622018-07-18 10:12:20 -0700512
Marissa Wall16c112d2019-03-20 13:21:13 -0700513 mCurrentStateModified = false;
514
Marissa Wall61c58622018-07-18 10:12:20 -0700515 return NO_ERROR;
516}
517
518status_t BufferStateLayer::updateActiveBuffer() {
519 const State& s(getDrawingState());
520
521 if (s.buffer == nullptr) {
522 return BAD_VALUE;
523 }
524
Mikael Pessa2e1608f2019-07-19 11:25:35 -0700525 mPreviousBufferId = getCurrentBufferId();
Marissa Wall61c58622018-07-18 10:12:20 -0700526 mActiveBuffer = s.buffer;
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000527 mActiveBufferFence = s.acquireFence;
Lloyd Pique0b785d82018-12-04 17:25:27 -0800528 auto& layerCompositionState = getCompositionLayer()->editState().frontEnd;
529 layerCompositionState.buffer = mActiveBuffer;
Marissa Wall61c58622018-07-18 10:12:20 -0700530
531 return NO_ERROR;
532}
533
534status_t BufferStateLayer::updateFrameNumber(nsecs_t /*latchTime*/) {
535 // TODO(marissaw): support frame history events
Mikael Pessa2e1608f2019-07-19 11:25:35 -0700536 mPreviousFrameNumber = mCurrentFrameNumber;
Marissa Wall61c58622018-07-18 10:12:20 -0700537 mCurrentFrameNumber = mFrameNumber;
538 return NO_ERROR;
539}
540
Lloyd Piquef5275482019-01-29 18:42:42 -0800541void BufferStateLayer::latchPerFrameState(
542 compositionengine::LayerFECompositionState& compositionState) const {
543 BufferLayer::latchPerFrameState(compositionState);
544 if (compositionState.compositionType == Hwc2::IComposerClient::Composition::SIDEBAND) {
545 return;
546 }
Marissa Wall61c58622018-07-18 10:12:20 -0700547
548 const State& s(getDrawingState());
549
Lloyd Piquef5275482019-01-29 18:42:42 -0800550 compositionState.buffer = s.buffer;
551 compositionState.bufferSlot = mHwcSlotGenerator->getHwcCacheSlot(s.clientCacheId);
chaviw4244e032019-09-04 11:27:49 -0700552 compositionState.acquireFence = mBufferInfo.mFence;
Marissa Wall61c58622018-07-18 10:12:20 -0700553
554 mFrameNumber++;
555}
556
557void BufferStateLayer::onFirstRef() {
Dan Stoza7b1b5a82018-07-31 16:00:21 -0700558 BufferLayer::onFirstRef();
559
Marissa Wall61c58622018-07-18 10:12:20 -0700560 if (const auto display = mFlinger->getDefaultDisplayDevice()) {
561 updateTransformHint(display);
562 }
563}
564
Marissa Wall947d34e2019-03-29 14:03:53 -0700565void BufferStateLayer::HwcSlotGenerator::bufferErased(const client_cache_t& clientCacheId) {
566 std::lock_guard lock(mMutex);
567 if (!clientCacheId.isValid()) {
568 ALOGE("invalid process, failed to erase buffer");
569 return;
570 }
571 eraseBufferLocked(clientCacheId);
572}
573
574uint32_t BufferStateLayer::HwcSlotGenerator::getHwcCacheSlot(const client_cache_t& clientCacheId) {
575 std::lock_guard<std::mutex> lock(mMutex);
576 auto itr = mCachedBuffers.find(clientCacheId);
577 if (itr == mCachedBuffers.end()) {
578 return addCachedBuffer(clientCacheId);
579 }
580 auto& [hwcCacheSlot, counter] = itr->second;
581 counter = mCounter++;
582 return hwcCacheSlot;
583}
584
585uint32_t BufferStateLayer::HwcSlotGenerator::addCachedBuffer(const client_cache_t& clientCacheId)
586 REQUIRES(mMutex) {
587 if (!clientCacheId.isValid()) {
588 ALOGE("invalid process, returning invalid slot");
589 return BufferQueue::INVALID_BUFFER_SLOT;
590 }
591
592 ClientCache::getInstance().registerErasedRecipient(clientCacheId, wp<ErasedRecipient>(this));
593
594 uint32_t hwcCacheSlot = getFreeHwcCacheSlot();
595 mCachedBuffers[clientCacheId] = {hwcCacheSlot, mCounter++};
596 return hwcCacheSlot;
597}
598
599uint32_t BufferStateLayer::HwcSlotGenerator::getFreeHwcCacheSlot() REQUIRES(mMutex) {
600 if (mFreeHwcCacheSlots.empty()) {
601 evictLeastRecentlyUsed();
602 }
603
604 uint32_t hwcCacheSlot = mFreeHwcCacheSlots.top();
605 mFreeHwcCacheSlots.pop();
606 return hwcCacheSlot;
607}
608
609void BufferStateLayer::HwcSlotGenerator::evictLeastRecentlyUsed() REQUIRES(mMutex) {
610 uint64_t minCounter = UINT_MAX;
611 client_cache_t minClientCacheId = {};
612 for (const auto& [clientCacheId, slotCounter] : mCachedBuffers) {
613 const auto& [hwcCacheSlot, counter] = slotCounter;
614 if (counter < minCounter) {
615 minCounter = counter;
616 minClientCacheId = clientCacheId;
617 }
618 }
619 eraseBufferLocked(minClientCacheId);
620
621 ClientCache::getInstance().unregisterErasedRecipient(minClientCacheId, this);
622}
623
624void BufferStateLayer::HwcSlotGenerator::eraseBufferLocked(const client_cache_t& clientCacheId)
625 REQUIRES(mMutex) {
626 auto itr = mCachedBuffers.find(clientCacheId);
627 if (itr == mCachedBuffers.end()) {
628 return;
629 }
630 auto& [hwcCacheSlot, counter] = itr->second;
631
632 // TODO send to hwc cache and resources
633
634 mFreeHwcCacheSlots.push(hwcCacheSlot);
635 mCachedBuffers.erase(clientCacheId);
636}
chaviw4244e032019-09-04 11:27:49 -0700637
638void BufferStateLayer::gatherBufferInfo() {
639 const State& s(getDrawingState());
640
641 mBufferInfo.mDesiredPresentTime = s.desiredPresentTime;
642 mBufferInfo.mFenceTime = std::make_shared<FenceTime>(s.acquireFence);
643 mBufferInfo.mFence = s.acquireFence;
644 std::copy(std::begin(mTransformMatrix), std::end(mTransformMatrix),
645 mBufferInfo.mTransformMatrix);
646 mBufferInfo.mTransform = s.transform;
647 mBufferInfo.mDataspace = translateDataspace(s.dataspace);
648 mBufferInfo.mCrop = computeCrop(s);
649 mBufferInfo.mScaleMode = NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW;
650 mBufferInfo.mSurfaceDamage = s.surfaceDamageRegion;
651 mBufferInfo.mHdrMetadata = s.hdrMetadata;
652 mBufferInfo.mApi = s.api;
653 mBufferInfo.mPixelFormat = !mActiveBuffer ? PIXEL_FORMAT_NONE : mActiveBuffer->format;
654 mBufferInfo.mTransformToDisplayInverse = s.transformToDisplayInverse;
655}
656
657Rect BufferStateLayer::computeCrop(const State& s) {
658 if (s.crop.isEmpty() && s.buffer) {
659 return s.buffer->getBounds();
660 } else if (s.buffer) {
661 Rect crop = s.crop;
662 crop.left = std::max(crop.left, 0);
663 crop.top = std::max(crop.top, 0);
664 uint32_t bufferWidth = s.buffer->getWidth();
665 uint32_t bufferHeight = s.buffer->getHeight();
666 if (bufferHeight <= std::numeric_limits<int32_t>::max() &&
667 bufferWidth <= std::numeric_limits<int32_t>::max()) {
668 crop.right = std::min(crop.right, static_cast<int32_t>(bufferWidth));
669 crop.bottom = std::min(crop.bottom, static_cast<int32_t>(bufferHeight));
670 }
671 if (!crop.isValid()) {
672 // Crop rect is out of bounds, return whole buffer
673 return s.buffer->getBounds();
674 }
675 return crop;
676 }
677 return s.crop;
678}
679
Marissa Wall61c58622018-07-18 10:12:20 -0700680} // namespace android