blob: a3d5b89be26b2222bc7cae70624b3464a7c198ea [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 Wall61c58622018-07-18 10:12:20 -070029#include <private/gui/SyncFeatures.h>
Peiyong Lincbc184f2018-08-22 13:24:10 -070030#include <renderengine/Image.h>
Marissa Wall61c58622018-07-18 10:12:20 -070031
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080032#include "BufferStateLayer.h"
33#include "ColorLayer.h"
34#include "TimeStats/TimeStats.h"
Valerie Hau0bc09152018-12-20 07:42:47 -080035
Marissa Wall61c58622018-07-18 10:12:20 -070036namespace android {
37
Lloyd Pique42ab75e2018-09-12 20:46:03 -070038// clang-format off
39const std::array<float, 16> BufferStateLayer::IDENTITY_MATRIX{
40 1, 0, 0, 0,
41 0, 1, 0, 0,
42 0, 0, 1, 0,
43 0, 0, 0, 1
44};
45// clang-format on
Marissa Wall61c58622018-07-18 10:12:20 -070046
Vishnu Nair60356342018-11-13 13:00:45 -080047BufferStateLayer::BufferStateLayer(const LayerCreationArgs& args) : BufferLayer(args) {
48 mOverrideScalingMode = NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW;
Marissa Wall3ff826c2019-02-07 11:58:25 -080049 mCurrentState.dataspace = ui::Dataspace::V0_SRGB;
Vishnu Nair60356342018-11-13 13:00:45 -080050}
Lloyd Pique42ab75e2018-09-12 20:46:03 -070051BufferStateLayer::~BufferStateLayer() = default;
Marissa Wall61c58622018-07-18 10:12:20 -070052
53// -----------------------------------------------------------------------
54// Interface implementation for Layer
55// -----------------------------------------------------------------------
Marissa Wallfda30bb2018-10-12 11:34:28 -070056void BufferStateLayer::onLayerDisplayed(const sp<Fence>& releaseFence) {
Marissa Wall5a68a772018-12-22 17:43:42 -080057 // The previous release fence notifies the client that SurfaceFlinger is done with the previous
58 // buffer that was presented on this layer. The first transaction that came in this frame that
59 // replaced the previous buffer on this layer needs this release fence, because the fence will
60 // let the client know when that previous buffer is removed from the screen.
61 //
62 // Every other transaction on this layer does not need a release fence because no other
63 // Transactions that were set on this layer this frame are going to have their preceeding buffer
64 // removed from the display this frame.
65 //
66 // For example, if we have 3 transactions this frame. The first transaction doesn't contain a
67 // buffer so it doesn't need a previous release fence because the layer still needs the previous
68 // buffer. The second transaction contains a buffer so it needs a previous release fence because
69 // the previous buffer will be released this frame. The third transaction also contains a
70 // buffer. It replaces the buffer in the second transaction. The buffer in the second
71 // transaction will now no longer be presented so it is released immediately and the third
72 // transaction doesn't need a previous release fence.
73 for (auto& handle : mDrawingState.callbackHandles) {
74 if (handle->releasePreviousBuffer) {
75 handle->previousReleaseFence = releaseFence;
76 break;
77 }
78 }
Marissa Wall61c58622018-07-18 10:12:20 -070079}
80
81void BufferStateLayer::setTransformHint(uint32_t /*orientation*/) const {
82 // TODO(marissaw): send the transform hint to buffer owner
83 return;
84}
85
86void BufferStateLayer::releasePendingBuffer(nsecs_t /*dequeueReadyTime*/) {
Marissa Wall5a68a772018-12-22 17:43:42 -080087 mFlinger->getTransactionCompletedThread().addPresentedCallbackHandles(
88 mDrawingState.callbackHandles);
89
90 mDrawingState.callbackHandles = {};
Marissa Wall61c58622018-07-18 10:12:20 -070091}
92
Ana Krulec010d2192018-10-08 06:29:54 -070093bool BufferStateLayer::shouldPresentNow(nsecs_t /*expectedPresentTime*/) const {
Marissa Wall61c58622018-07-18 10:12:20 -070094 if (getSidebandStreamChanged() || getAutoRefresh()) {
95 return true;
96 }
97
Marissa Wall024a1912018-08-13 13:55:35 -070098 return hasFrameUpdate();
Marissa Wall61c58622018-07-18 10:12:20 -070099}
100
Marissa Walle2ffb422018-10-12 11:33:52 -0700101bool BufferStateLayer::willPresentCurrentTransaction() const {
102 // Returns true if the most recent Transaction applied to CurrentState will be presented.
103 return getSidebandStreamChanged() || getAutoRefresh() ||
Valerie Hauaa194562019-02-05 16:21:38 -0800104 (mCurrentState.modified &&
105 (mCurrentState.buffer != nullptr || mCurrentState.bgColorLayer != nullptr));
Marissa Wall61c58622018-07-18 10:12:20 -0700106}
107
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800108bool BufferStateLayer::getTransformToDisplayInverse() const {
109 return mCurrentState.transformToDisplayInverse;
Marissa Wall61c58622018-07-18 10:12:20 -0700110}
111
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800112void BufferStateLayer::pushPendingState() {
113 if (!mCurrentState.modified) {
Marissa Wall61c58622018-07-18 10:12:20 -0700114 return;
115 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800116 mPendingStates.push_back(mCurrentState);
117 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Marissa Wall61c58622018-07-18 10:12:20 -0700118}
119
120bool BufferStateLayer::applyPendingStates(Layer::State* stateToCommit) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800121 const bool stateUpdateAvailable = !mPendingStates.empty();
122 while (!mPendingStates.empty()) {
Marissa Wall61c58622018-07-18 10:12:20 -0700123 popPendingState(stateToCommit);
124 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800125 mCurrentStateModified = stateUpdateAvailable && mCurrentState.modified;
126 mCurrentState.modified = false;
Marissa Wall61c58622018-07-18 10:12:20 -0700127 return stateUpdateAvailable;
128}
129
Marissa Wall861616d2018-10-22 12:52:23 -0700130// Crop that applies to the window
131Rect BufferStateLayer::getCrop(const Layer::State& /*s*/) const {
132 return Rect::INVALID_RECT;
Marissa Wall61c58622018-07-18 10:12:20 -0700133}
134
135bool BufferStateLayer::setTransform(uint32_t transform) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800136 if (mCurrentState.transform == transform) return false;
137 mCurrentState.sequence++;
138 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) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800154 if (mCurrentState.crop == crop) return false;
155 mCurrentState.sequence++;
156 mCurrentState.crop = crop;
157 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700158 setTransactionFlags(eTransactionNeeded);
159 return true;
160}
161
Marissa Wall861616d2018-10-22 12:52:23 -0700162bool BufferStateLayer::setFrame(const Rect& frame) {
163 int x = frame.left;
164 int y = frame.top;
165 int w = frame.getWidth();
166 int h = frame.getHeight();
167
Marissa Wall0f3242d2018-12-20 15:10:22 -0800168 if (x < 0) {
169 x = 0;
170 w = frame.right;
171 }
172
173 if (y < 0) {
174 y = 0;
175 h = frame.bottom;
176 }
177
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800178 if (mCurrentState.active.transform.tx() == x && mCurrentState.active.transform.ty() == y &&
179 mCurrentState.active.w == w && mCurrentState.active.h == h) {
Marissa Wall861616d2018-10-22 12:52:23 -0700180 return false;
181 }
182
183 if (!frame.isValid()) {
184 x = y = w = h = 0;
185 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800186 mCurrentState.active.transform.set(x, y);
187 mCurrentState.active.w = w;
188 mCurrentState.active.h = h;
Marissa Wall861616d2018-10-22 12:52:23 -0700189
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800190 mCurrentState.sequence++;
191 mCurrentState.modified = true;
Marissa Wall861616d2018-10-22 12:52:23 -0700192 setTransactionFlags(eTransactionNeeded);
193 return true;
194}
195
Marissa Wallfda30bb2018-10-12 11:34:28 -0700196bool BufferStateLayer::setBuffer(const sp<GraphicBuffer>& buffer) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800197 if (mCurrentState.buffer) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700198 mReleasePreviousBuffer = true;
199 }
200
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800201 mCurrentState.sequence++;
202 mCurrentState.buffer = buffer;
203 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700204 setTransactionFlags(eTransactionNeeded);
205 return true;
206}
207
208bool BufferStateLayer::setAcquireFence(const sp<Fence>& fence) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700209 // The acquire fences of BufferStateLayers have already signaled before they are set
210 mCallbackHandleAcquireTime = fence->getSignalTime();
211
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800212 mCurrentState.acquireFence = fence;
213 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700214 setTransactionFlags(eTransactionNeeded);
215 return true;
216}
217
218bool BufferStateLayer::setDataspace(ui::Dataspace dataspace) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800219 if (mCurrentState.dataspace == dataspace) return false;
220 mCurrentState.sequence++;
221 mCurrentState.dataspace = dataspace;
222 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700223 setTransactionFlags(eTransactionNeeded);
224 return true;
225}
226
227bool BufferStateLayer::setHdrMetadata(const HdrMetadata& hdrMetadata) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800228 if (mCurrentState.hdrMetadata == hdrMetadata) return false;
229 mCurrentState.sequence++;
230 mCurrentState.hdrMetadata = hdrMetadata;
231 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700232 setTransactionFlags(eTransactionNeeded);
233 return true;
234}
235
236bool BufferStateLayer::setSurfaceDamageRegion(const Region& surfaceDamage) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800237 mCurrentState.sequence++;
238 mCurrentState.surfaceDamageRegion = surfaceDamage;
239 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700240 setTransactionFlags(eTransactionNeeded);
241 return true;
242}
243
244bool BufferStateLayer::setApi(int32_t api) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800245 if (mCurrentState.api == api) return false;
246 mCurrentState.sequence++;
247 mCurrentState.api = api;
248 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700249 setTransactionFlags(eTransactionNeeded);
250 return true;
251}
252
253bool BufferStateLayer::setSidebandStream(const sp<NativeHandle>& sidebandStream) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800254 if (mCurrentState.sidebandStream == sidebandStream) return false;
255 mCurrentState.sequence++;
256 mCurrentState.sidebandStream = sidebandStream;
257 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700258 setTransactionFlags(eTransactionNeeded);
259
260 if (!mSidebandStreamChanged.exchange(true)) {
261 // mSidebandStreamChanged was false
262 mFlinger->signalLayerUpdate();
263 }
264 return true;
265}
266
Marissa Walle2ffb422018-10-12 11:33:52 -0700267bool BufferStateLayer::setTransactionCompletedListeners(
268 const std::vector<sp<CallbackHandle>>& handles) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700269 // If there is no handle, we will not send a callback so reset mReleasePreviousBuffer and return
Marissa Walle2ffb422018-10-12 11:33:52 -0700270 if (handles.empty()) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700271 mReleasePreviousBuffer = false;
Marissa Walle2ffb422018-10-12 11:33:52 -0700272 return false;
273 }
274
275 const bool willPresent = willPresentCurrentTransaction();
276
277 for (const auto& handle : handles) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700278 // If this transaction set a buffer on this layer, release its previous buffer
279 handle->releasePreviousBuffer = mReleasePreviousBuffer;
280
Marissa Walle2ffb422018-10-12 11:33:52 -0700281 // If this layer will be presented in this frame
282 if (willPresent) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700283 // If this transaction set an acquire fence on this layer, set its acquire time
284 handle->acquireTime = mCallbackHandleAcquireTime;
285
Marissa Walle2ffb422018-10-12 11:33:52 -0700286 // Notify the transaction completed thread that there is a pending latched callback
287 // handle
Marissa Wall5a68a772018-12-22 17:43:42 -0800288 mFlinger->getTransactionCompletedThread().registerPendingCallbackHandle(handle);
Marissa Walle2ffb422018-10-12 11:33:52 -0700289
290 // Store so latched time and release fence can be set
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800291 mCurrentState.callbackHandles.push_back(handle);
Marissa Walle2ffb422018-10-12 11:33:52 -0700292
293 } else { // If this layer will NOT need to be relatched and presented this frame
294 // Notify the transaction completed thread this handle is done
Marissa Wall5a68a772018-12-22 17:43:42 -0800295 mFlinger->getTransactionCompletedThread().addUnpresentedCallbackHandle(handle);
Marissa Walle2ffb422018-10-12 11:33:52 -0700296 }
297 }
298
Marissa Wallfda30bb2018-10-12 11:34:28 -0700299 mReleasePreviousBuffer = false;
300 mCallbackHandleAcquireTime = -1;
301
Marissa Walle2ffb422018-10-12 11:33:52 -0700302 return willPresent;
303}
304
Marissa Wall61c58622018-07-18 10:12:20 -0700305bool BufferStateLayer::setTransparentRegionHint(const Region& transparent) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800306 mCurrentState.transparentRegionHint = transparent;
307 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700308 setTransactionFlags(eTransactionNeeded);
309 return true;
310}
311
Marissa Wall861616d2018-10-22 12:52:23 -0700312Rect BufferStateLayer::getBufferSize(const State& s) const {
313 // for buffer state layers we use the display frame size as the buffer size.
314 if (getActiveWidth(s) < UINT32_MAX && getActiveHeight(s) < UINT32_MAX) {
315 return Rect(getActiveWidth(s), getActiveHeight(s));
Marissa Wall61c58622018-07-18 10:12:20 -0700316 }
317
Marissa Wall861616d2018-10-22 12:52:23 -0700318 // if the display frame is not defined, use the parent bounds as the buffer size.
319 const auto& p = mDrawingParent.promote();
320 if (p != nullptr) {
Vishnu Nair4351ad52019-02-11 14:13:02 -0800321 Rect parentBounds = Rect(p->getBounds(Region()));
Marissa Wall861616d2018-10-22 12:52:23 -0700322 if (!parentBounds.isEmpty()) {
323 return parentBounds;
324 }
325 }
326
327 // if there is no parent layer, use the buffer's bounds as the buffer size
328 if (s.buffer) {
329 return s.buffer->getBounds();
330 }
331 return Rect::INVALID_RECT;
Marissa Wall61c58622018-07-18 10:12:20 -0700332}
Vishnu Nair4351ad52019-02-11 14:13:02 -0800333
334FloatRect BufferStateLayer::computeSourceBounds(const FloatRect& parentBounds) const {
335 const State& s(getDrawingState());
336 // for buffer state layers we use the display frame size as the buffer size.
337 if (getActiveWidth(s) < UINT32_MAX && getActiveHeight(s) < UINT32_MAX) {
338 return FloatRect(0, 0, getActiveWidth(s), getActiveHeight(s));
339 }
340
341 // if the display frame is not defined, use the parent bounds as the buffer size.
342 return parentBounds;
343}
344
Marissa Wall61c58622018-07-18 10:12:20 -0700345// -----------------------------------------------------------------------
346
347// -----------------------------------------------------------------------
348// Interface implementation for BufferLayer
349// -----------------------------------------------------------------------
350bool BufferStateLayer::fenceHasSignaled() const {
351 if (latchUnsignaledBuffers()) {
352 return true;
353 }
354
355 return getDrawingState().acquireFence->getStatus() == Fence::Status::Signaled;
356}
357
358nsecs_t BufferStateLayer::getDesiredPresentTime() {
359 // TODO(marissaw): support an equivalent to desiredPresentTime for timestats metrics
360 return 0;
361}
362
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800363std::shared_ptr<FenceTime> BufferStateLayer::getCurrentFenceTime() const {
Marissa Wall61c58622018-07-18 10:12:20 -0700364 return std::make_shared<FenceTime>(getDrawingState().acquireFence);
365}
366
367void BufferStateLayer::getDrawingTransformMatrix(float *matrix) {
368 std::copy(std::begin(mTransformMatrix), std::end(mTransformMatrix), matrix);
369}
370
371uint32_t BufferStateLayer::getDrawingTransform() const {
372 return getDrawingState().transform;
373}
374
375ui::Dataspace BufferStateLayer::getDrawingDataSpace() const {
376 return getDrawingState().dataspace;
377}
378
Marissa Wall861616d2018-10-22 12:52:23 -0700379// Crop that applies to the buffer
Marissa Wall61c58622018-07-18 10:12:20 -0700380Rect BufferStateLayer::getDrawingCrop() const {
Marissa Wall861616d2018-10-22 12:52:23 -0700381 const State& s(getDrawingState());
382
383 if (s.crop.isEmpty() && s.buffer) {
384 return s.buffer->getBounds();
Valerie Hau0bc09152018-12-20 07:42:47 -0800385 } else if (s.buffer) {
386 Rect crop = s.crop;
387 crop.left = std::max(crop.left, 0);
388 crop.top = std::max(crop.top, 0);
389 uint32_t bufferWidth = s.buffer->getWidth();
390 uint32_t bufferHeight = s.buffer->getHeight();
391 if (bufferHeight <= std::numeric_limits<int32_t>::max() &&
392 bufferWidth <= std::numeric_limits<int32_t>::max()) {
393 crop.right = std::min(crop.right, static_cast<int32_t>(bufferWidth));
394 crop.bottom = std::min(crop.bottom, static_cast<int32_t>(bufferHeight));
395 }
396 if (!crop.isValid()) {
397 // Crop rect is out of bounds, return whole buffer
398 return s.buffer->getBounds();
399 }
400 return crop;
Marissa Wall861616d2018-10-22 12:52:23 -0700401 }
402 return s.crop;
Marissa Wall61c58622018-07-18 10:12:20 -0700403}
404
405uint32_t BufferStateLayer::getDrawingScalingMode() const {
Marissa Wallec463ac2018-10-08 12:35:04 -0700406 return NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW;
Marissa Wall61c58622018-07-18 10:12:20 -0700407}
408
409Region BufferStateLayer::getDrawingSurfaceDamage() const {
410 return getDrawingState().surfaceDamageRegion;
411}
412
413const HdrMetadata& BufferStateLayer::getDrawingHdrMetadata() const {
414 return getDrawingState().hdrMetadata;
415}
416
417int BufferStateLayer::getDrawingApi() const {
418 return getDrawingState().api;
419}
420
421PixelFormat BufferStateLayer::getPixelFormat() const {
Marissa Wall5aec6412018-11-14 11:49:18 -0800422 if (!mActiveBuffer) {
423 return PIXEL_FORMAT_NONE;
424 }
Marissa Wall61c58622018-07-18 10:12:20 -0700425 return mActiveBuffer->format;
426}
427
428uint64_t BufferStateLayer::getFrameNumber() const {
429 return mFrameNumber;
430}
431
432bool BufferStateLayer::getAutoRefresh() const {
433 // TODO(marissaw): support shared buffer mode
434 return false;
435}
436
437bool BufferStateLayer::getSidebandStreamChanged() const {
438 return mSidebandStreamChanged.load();
439}
440
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800441bool BufferStateLayer::latchSidebandStream(bool& recomputeVisibleRegions) {
Marissa Wall61c58622018-07-18 10:12:20 -0700442 if (mSidebandStreamChanged.exchange(false)) {
443 const State& s(getDrawingState());
444 // mSidebandStreamChanged was true
Lloyd Pique0b785d82018-12-04 17:25:27 -0800445 LOG_ALWAYS_FATAL_IF(!getCompositionLayer());
446 mSidebandStream = s.sidebandStream;
447 getCompositionLayer()->editState().frontEnd.sidebandStream = mSidebandStream;
448 if (mSidebandStream != nullptr) {
Marissa Wall61c58622018-07-18 10:12:20 -0700449 setTransactionFlags(eTransactionNeeded);
450 mFlinger->setTransactionFlags(eTraversalNeeded);
451 }
452 recomputeVisibleRegions = true;
453
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800454 return true;
Marissa Wall61c58622018-07-18 10:12:20 -0700455 }
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800456 return false;
Marissa Wall61c58622018-07-18 10:12:20 -0700457}
458
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800459bool BufferStateLayer::hasFrameUpdate() const {
Valerie Hauaa194562019-02-05 16:21:38 -0800460 const State& c(getCurrentState());
461 return mCurrentStateModified && (c.buffer != nullptr || c.bgColorLayer != nullptr);
Marissa Wall61c58622018-07-18 10:12:20 -0700462}
463
464void BufferStateLayer::setFilteringEnabled(bool enabled) {
465 GLConsumer::computeTransformMatrix(mTransformMatrix.data(), mActiveBuffer, mCurrentCrop,
466 mCurrentTransform, enabled);
467}
468
Alec Mouri39801c02018-10-10 10:44:47 -0700469status_t BufferStateLayer::bindTextureImage() {
Marissa Wall61c58622018-07-18 10:12:20 -0700470 const State& s(getDrawingState());
471 auto& engine(mFlinger->getRenderEngine());
472
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000473 return engine.bindExternalTextureBuffer(mTextureName, s.buffer, s.acquireFence, false);
Marissa Wall61c58622018-07-18 10:12:20 -0700474}
475
Alec Mouri86770e52018-09-24 22:40:58 +0000476status_t BufferStateLayer::updateTexImage(bool& /*recomputeVisibleRegions*/, nsecs_t latchTime,
477 const sp<Fence>& releaseFence) {
Marissa Wall61c58622018-07-18 10:12:20 -0700478 const State& s(getDrawingState());
479
480 if (!s.buffer) {
Valerie Hauaa194562019-02-05 16:21:38 -0800481 if (s.bgColorLayer) {
482 for (auto& handle : mDrawingState.callbackHandles) {
483 handle->latchTime = latchTime;
484 }
485 }
Marissa Wall61c58622018-07-18 10:12:20 -0700486 return NO_ERROR;
487 }
488
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700489 const int32_t layerID = getSequence();
490
Marissa Wall61c58622018-07-18 10:12:20 -0700491 // Reject if the layer is invalid
492 uint32_t bufferWidth = s.buffer->width;
493 uint32_t bufferHeight = s.buffer->height;
494
Peiyong Linefefaac2018-08-17 12:27:51 -0700495 if (s.transform & ui::Transform::ROT_90) {
Peiyong Lin3db42342018-08-16 09:15:59 -0700496 std::swap(bufferWidth, bufferHeight);
Marissa Wall61c58622018-07-18 10:12:20 -0700497 }
498
499 if (s.transformToDisplayInverse) {
500 uint32_t invTransform = DisplayDevice::getPrimaryDisplayOrientationTransform();
Peiyong Linefefaac2018-08-17 12:27:51 -0700501 if (invTransform & ui::Transform::ROT_90) {
Peiyong Lin3db42342018-08-16 09:15:59 -0700502 std::swap(bufferWidth, bufferHeight);
Marissa Wall61c58622018-07-18 10:12:20 -0700503 }
504 }
505
Vishnu Nair60356342018-11-13 13:00:45 -0800506 if (getEffectiveScalingMode() == NATIVE_WINDOW_SCALING_MODE_FREEZE &&
Marissa Wall61c58622018-07-18 10:12:20 -0700507 (s.active.w != bufferWidth || s.active.h != bufferHeight)) {
508 ALOGE("[%s] rejecting buffer: "
509 "bufferWidth=%d, bufferHeight=%d, front.active.{w=%d, h=%d}",
510 mName.string(), bufferWidth, bufferHeight, s.active.w, s.active.h);
Yiwei Zhang7e666a52018-11-15 13:33:42 -0800511 mFlinger->mTimeStats->removeTimeRecord(layerID, getFrameNumber());
Marissa Wall61c58622018-07-18 10:12:20 -0700512 return BAD_VALUE;
513 }
514
Marissa Wall5a68a772018-12-22 17:43:42 -0800515 for (auto& handle : mDrawingState.callbackHandles) {
516 handle->latchTime = latchTime;
517 }
Marissa Walle2ffb422018-10-12 11:33:52 -0700518
Marissa Wall61c58622018-07-18 10:12:20 -0700519 // Handle sync fences
Alec Mouri86770e52018-09-24 22:40:58 +0000520 if (SyncFeatures::getInstance().useNativeFenceSync() && releaseFence != Fence::NO_FENCE) {
521 // TODO(alecmouri): Fail somewhere upstream if the fence is invalid.
522 if (!releaseFence->isValid()) {
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800523 mFlinger->mTimeStats->onDestroy(layerID);
Marissa Wall61c58622018-07-18 10:12:20 -0700524 return UNKNOWN_ERROR;
525 }
526
Marissa Wall61c58622018-07-18 10:12:20 -0700527 // Check status of fences first because merging is expensive.
528 // Merging an invalid fence with any other fence results in an
529 // invalid fence.
530 auto currentStatus = s.acquireFence->getStatus();
531 if (currentStatus == Fence::Status::Invalid) {
532 ALOGE("Existing fence has invalid state");
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800533 mFlinger->mTimeStats->onDestroy(layerID);
Marissa Wall61c58622018-07-18 10:12:20 -0700534 return BAD_VALUE;
535 }
536
Alec Mouri86770e52018-09-24 22:40:58 +0000537 auto incomingStatus = releaseFence->getStatus();
Marissa Wall61c58622018-07-18 10:12:20 -0700538 if (incomingStatus == Fence::Status::Invalid) {
539 ALOGE("New fence has invalid state");
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800540 mDrawingState.acquireFence = releaseFence;
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800541 mFlinger->mTimeStats->onDestroy(layerID);
Marissa Wall61c58622018-07-18 10:12:20 -0700542 return BAD_VALUE;
543 }
544
545 // If both fences are signaled or both are unsignaled, we need to merge
546 // them to get an accurate timestamp.
547 if (currentStatus == incomingStatus) {
548 char fenceName[32] = {};
549 snprintf(fenceName, 32, "%.28s:%d", mName.string(), mFrameNumber);
Alec Mouri86770e52018-09-24 22:40:58 +0000550 sp<Fence> mergedFence =
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800551 Fence::merge(fenceName, mDrawingState.acquireFence, releaseFence);
Marissa Wall61c58622018-07-18 10:12:20 -0700552 if (!mergedFence.get()) {
553 ALOGE("failed to merge release fences");
554 // synchronization is broken, the best we can do is hope fences
555 // signal in order so the new fence will act like a union
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800556 mDrawingState.acquireFence = releaseFence;
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800557 mFlinger->mTimeStats->onDestroy(layerID);
Marissa Wall61c58622018-07-18 10:12:20 -0700558 return BAD_VALUE;
559 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800560 mDrawingState.acquireFence = mergedFence;
Marissa Wall61c58622018-07-18 10:12:20 -0700561 } else if (incomingStatus == Fence::Status::Unsignaled) {
562 // If one fence has signaled and the other hasn't, the unsignaled
563 // fence will approximately correspond with the correct timestamp.
564 // There's a small race if both fences signal at about the same time
565 // and their statuses are retrieved with unfortunate timing. However,
566 // by this point, they will have both signaled and only the timestamp
567 // will be slightly off; any dependencies after this point will
568 // already have been met.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800569 mDrawingState.acquireFence = releaseFence;
Marissa Wall61c58622018-07-18 10:12:20 -0700570 }
571 } else {
572 // Bind the new buffer to the GL texture.
573 //
574 // Older devices require the "implicit" synchronization provided
575 // by glEGLImageTargetTexture2DOES, which this method calls. Newer
576 // devices will either call this in Layer::onDraw, or (if it's not
577 // a GL-composited layer) not at all.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800578 status_t err = bindTextureImage();
Marissa Wall61c58622018-07-18 10:12:20 -0700579 if (err != NO_ERROR) {
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800580 mFlinger->mTimeStats->onDestroy(layerID);
Marissa Wall61c58622018-07-18 10:12:20 -0700581 return BAD_VALUE;
582 }
583 }
584
585 // TODO(marissaw): properly support mTimeStats
Yiwei Zhang7e666a52018-11-15 13:33:42 -0800586 mFlinger->mTimeStats->setPostTime(layerID, getFrameNumber(), getName().c_str(), latchTime);
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800587 mFlinger->mTimeStats->setAcquireFence(layerID, getFrameNumber(), getCurrentFenceTime());
Yiwei Zhang7e666a52018-11-15 13:33:42 -0800588 mFlinger->mTimeStats->setLatchTime(layerID, getFrameNumber(), latchTime);
Marissa Wall61c58622018-07-18 10:12:20 -0700589
590 return NO_ERROR;
591}
592
593status_t BufferStateLayer::updateActiveBuffer() {
594 const State& s(getDrawingState());
595
596 if (s.buffer == nullptr) {
597 return BAD_VALUE;
598 }
599
600 mActiveBuffer = s.buffer;
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000601 mActiveBufferFence = s.acquireFence;
Lloyd Pique0b785d82018-12-04 17:25:27 -0800602 auto& layerCompositionState = getCompositionLayer()->editState().frontEnd;
603 layerCompositionState.buffer = mActiveBuffer;
604 layerCompositionState.bufferSlot = 0;
Marissa Wall61c58622018-07-18 10:12:20 -0700605
606 return NO_ERROR;
607}
608
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000609bool BufferStateLayer::useCachedBufferForClientComposition() const {
610 // TODO: Store a proper staleness bit to support EGLImage caching.
611 return false;
612}
613
Marissa Wall61c58622018-07-18 10:12:20 -0700614status_t BufferStateLayer::updateFrameNumber(nsecs_t /*latchTime*/) {
615 // TODO(marissaw): support frame history events
616 mCurrentFrameNumber = mFrameNumber;
617 return NO_ERROR;
618}
619
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800620void BufferStateLayer::setHwcLayerBuffer(const sp<const DisplayDevice>& display) {
621 const auto outputLayer = findOutputLayerForDisplay(display);
622 LOG_FATAL_IF(!outputLayer || !outputLayer->getState().hwc);
623 auto& hwcLayer = (*outputLayer->getState().hwc).hwcLayer;
Marissa Wall61c58622018-07-18 10:12:20 -0700624
625 const State& s(getDrawingState());
626
627 // TODO(marissaw): support more than one slot
628 uint32_t hwcSlot = 0;
629
630 auto error = hwcLayer->setBuffer(hwcSlot, s.buffer, s.acquireFence);
631 if (error != HWC2::Error::None) {
632 ALOGE("[%s] Failed to set buffer %p: %s (%d)", mName.string(),
633 s.buffer->handle, to_string(error).c_str(), static_cast<int32_t>(error));
634 }
635
Marissa Wall024a1912018-08-13 13:55:35 -0700636 mCurrentStateModified = false;
Marissa Wall61c58622018-07-18 10:12:20 -0700637 mFrameNumber++;
638}
639
640void BufferStateLayer::onFirstRef() {
Dan Stoza7b1b5a82018-07-31 16:00:21 -0700641 BufferLayer::onFirstRef();
642
Marissa Wall61c58622018-07-18 10:12:20 -0700643 if (const auto display = mFlinger->getDefaultDisplayDevice()) {
644 updateTransformHint(display);
645 }
646}
647
648} // namespace android