blob: 369e71f2cf20537325c8037f8fa177f483571149 [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>
25#include <compositionengine/OutputLayer.h>
26#include <compositionengine/impl/OutputLayerCompositionState.h>
Marissa Wall61c58622018-07-18 10:12:20 -070027#include <private/gui/SyncFeatures.h>
Peiyong Lincbc184f2018-08-22 13:24:10 -070028#include <renderengine/Image.h>
Marissa Wall61c58622018-07-18 10:12:20 -070029
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080030#include "BufferStateLayer.h"
31#include "ColorLayer.h"
32#include "TimeStats/TimeStats.h"
Valerie Hau0bc09152018-12-20 07:42:47 -080033
Marissa Wall61c58622018-07-18 10:12:20 -070034namespace android {
35
Lloyd Pique42ab75e2018-09-12 20:46:03 -070036// clang-format off
37const std::array<float, 16> BufferStateLayer::IDENTITY_MATRIX{
38 1, 0, 0, 0,
39 0, 1, 0, 0,
40 0, 0, 1, 0,
41 0, 0, 0, 1
42};
43// clang-format on
Marissa Wall61c58622018-07-18 10:12:20 -070044
Vishnu Nair60356342018-11-13 13:00:45 -080045BufferStateLayer::BufferStateLayer(const LayerCreationArgs& args) : BufferLayer(args) {
46 mOverrideScalingMode = NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW;
Marissa Wall3ff826c2019-02-07 11:58:25 -080047 mCurrentState.dataspace = ui::Dataspace::V0_SRGB;
Vishnu Nair60356342018-11-13 13:00:45 -080048}
Lloyd Pique42ab75e2018-09-12 20:46:03 -070049BufferStateLayer::~BufferStateLayer() = default;
Marissa Wall61c58622018-07-18 10:12:20 -070050
51// -----------------------------------------------------------------------
52// Interface implementation for Layer
53// -----------------------------------------------------------------------
Marissa Wallfda30bb2018-10-12 11:34:28 -070054void BufferStateLayer::onLayerDisplayed(const sp<Fence>& releaseFence) {
Marissa Wall5a68a772018-12-22 17:43:42 -080055 // The previous release fence notifies the client that SurfaceFlinger is done with the previous
56 // buffer that was presented on this layer. The first transaction that came in this frame that
57 // replaced the previous buffer on this layer needs this release fence, because the fence will
58 // let the client know when that previous buffer is removed from the screen.
59 //
60 // Every other transaction on this layer does not need a release fence because no other
61 // Transactions that were set on this layer this frame are going to have their preceeding buffer
62 // removed from the display this frame.
63 //
64 // For example, if we have 3 transactions this frame. The first transaction doesn't contain a
65 // buffer so it doesn't need a previous release fence because the layer still needs the previous
66 // buffer. The second transaction contains a buffer so it needs a previous release fence because
67 // the previous buffer will be released this frame. The third transaction also contains a
68 // buffer. It replaces the buffer in the second transaction. The buffer in the second
69 // transaction will now no longer be presented so it is released immediately and the third
70 // transaction doesn't need a previous release fence.
71 for (auto& handle : mDrawingState.callbackHandles) {
72 if (handle->releasePreviousBuffer) {
73 handle->previousReleaseFence = releaseFence;
74 break;
75 }
76 }
Marissa Wall61c58622018-07-18 10:12:20 -070077}
78
79void BufferStateLayer::setTransformHint(uint32_t /*orientation*/) const {
80 // TODO(marissaw): send the transform hint to buffer owner
81 return;
82}
83
84void BufferStateLayer::releasePendingBuffer(nsecs_t /*dequeueReadyTime*/) {
Marissa Wall5a68a772018-12-22 17:43:42 -080085 mFlinger->getTransactionCompletedThread().addPresentedCallbackHandles(
86 mDrawingState.callbackHandles);
87
88 mDrawingState.callbackHandles = {};
Marissa Wall61c58622018-07-18 10:12:20 -070089}
90
Ana Krulec010d2192018-10-08 06:29:54 -070091bool BufferStateLayer::shouldPresentNow(nsecs_t /*expectedPresentTime*/) const {
Marissa Wall61c58622018-07-18 10:12:20 -070092 if (getSidebandStreamChanged() || getAutoRefresh()) {
93 return true;
94 }
95
Marissa Wall024a1912018-08-13 13:55:35 -070096 return hasFrameUpdate();
Marissa Wall61c58622018-07-18 10:12:20 -070097}
98
Marissa Walle2ffb422018-10-12 11:33:52 -070099bool BufferStateLayer::willPresentCurrentTransaction() const {
100 // Returns true if the most recent Transaction applied to CurrentState will be presented.
101 return getSidebandStreamChanged() || getAutoRefresh() ||
Valerie Hauaa194562019-02-05 16:21:38 -0800102 (mCurrentState.modified &&
103 (mCurrentState.buffer != nullptr || mCurrentState.bgColorLayer != nullptr));
Marissa Wall61c58622018-07-18 10:12:20 -0700104}
105
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800106bool BufferStateLayer::getTransformToDisplayInverse() const {
107 return mCurrentState.transformToDisplayInverse;
Marissa Wall61c58622018-07-18 10:12:20 -0700108}
109
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800110void BufferStateLayer::pushPendingState() {
111 if (!mCurrentState.modified) {
Marissa Wall61c58622018-07-18 10:12:20 -0700112 return;
113 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800114 mPendingStates.push_back(mCurrentState);
115 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Marissa Wall61c58622018-07-18 10:12:20 -0700116}
117
118bool BufferStateLayer::applyPendingStates(Layer::State* stateToCommit) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800119 const bool stateUpdateAvailable = !mPendingStates.empty();
120 while (!mPendingStates.empty()) {
Marissa Wall61c58622018-07-18 10:12:20 -0700121 popPendingState(stateToCommit);
122 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800123 mCurrentStateModified = stateUpdateAvailable && mCurrentState.modified;
124 mCurrentState.modified = false;
Marissa Wall61c58622018-07-18 10:12:20 -0700125 return stateUpdateAvailable;
126}
127
Marissa Wall861616d2018-10-22 12:52:23 -0700128// Crop that applies to the window
129Rect BufferStateLayer::getCrop(const Layer::State& /*s*/) const {
130 return Rect::INVALID_RECT;
Marissa Wall61c58622018-07-18 10:12:20 -0700131}
132
133bool BufferStateLayer::setTransform(uint32_t transform) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800134 if (mCurrentState.transform == transform) return false;
135 mCurrentState.sequence++;
136 mCurrentState.transform = transform;
137 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700138 setTransactionFlags(eTransactionNeeded);
139 return true;
140}
141
142bool BufferStateLayer::setTransformToDisplayInverse(bool transformToDisplayInverse) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800143 if (mCurrentState.transformToDisplayInverse == transformToDisplayInverse) return false;
144 mCurrentState.sequence++;
145 mCurrentState.transformToDisplayInverse = transformToDisplayInverse;
146 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700147 setTransactionFlags(eTransactionNeeded);
148 return true;
149}
150
151bool BufferStateLayer::setCrop(const Rect& crop) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800152 if (mCurrentState.crop == crop) return false;
153 mCurrentState.sequence++;
154 mCurrentState.crop = crop;
155 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700156 setTransactionFlags(eTransactionNeeded);
157 return true;
158}
159
Marissa Wall861616d2018-10-22 12:52:23 -0700160bool BufferStateLayer::setFrame(const Rect& frame) {
161 int x = frame.left;
162 int y = frame.top;
163 int w = frame.getWidth();
164 int h = frame.getHeight();
165
Marissa Wall0f3242d2018-12-20 15:10:22 -0800166 if (x < 0) {
167 x = 0;
168 w = frame.right;
169 }
170
171 if (y < 0) {
172 y = 0;
173 h = frame.bottom;
174 }
175
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800176 if (mCurrentState.active.transform.tx() == x && mCurrentState.active.transform.ty() == y &&
177 mCurrentState.active.w == w && mCurrentState.active.h == h) {
Marissa Wall861616d2018-10-22 12:52:23 -0700178 return false;
179 }
180
181 if (!frame.isValid()) {
182 x = y = w = h = 0;
183 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800184 mCurrentState.active.transform.set(x, y);
185 mCurrentState.active.w = w;
186 mCurrentState.active.h = h;
Marissa Wall861616d2018-10-22 12:52:23 -0700187
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800188 mCurrentState.sequence++;
189 mCurrentState.modified = true;
Marissa Wall861616d2018-10-22 12:52:23 -0700190 setTransactionFlags(eTransactionNeeded);
191 return true;
192}
193
Marissa Wallfda30bb2018-10-12 11:34:28 -0700194bool BufferStateLayer::setBuffer(const sp<GraphicBuffer>& buffer) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800195 if (mCurrentState.buffer) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700196 mReleasePreviousBuffer = true;
197 }
198
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800199 mCurrentState.sequence++;
200 mCurrentState.buffer = buffer;
201 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700202 setTransactionFlags(eTransactionNeeded);
203 return true;
204}
205
206bool BufferStateLayer::setAcquireFence(const sp<Fence>& fence) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700207 // The acquire fences of BufferStateLayers have already signaled before they are set
208 mCallbackHandleAcquireTime = fence->getSignalTime();
209
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800210 mCurrentState.acquireFence = fence;
211 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700212 setTransactionFlags(eTransactionNeeded);
213 return true;
214}
215
216bool BufferStateLayer::setDataspace(ui::Dataspace dataspace) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800217 if (mCurrentState.dataspace == dataspace) return false;
218 mCurrentState.sequence++;
219 mCurrentState.dataspace = dataspace;
220 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700221 setTransactionFlags(eTransactionNeeded);
222 return true;
223}
224
225bool BufferStateLayer::setHdrMetadata(const HdrMetadata& hdrMetadata) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800226 if (mCurrentState.hdrMetadata == hdrMetadata) return false;
227 mCurrentState.sequence++;
228 mCurrentState.hdrMetadata = hdrMetadata;
229 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700230 setTransactionFlags(eTransactionNeeded);
231 return true;
232}
233
234bool BufferStateLayer::setSurfaceDamageRegion(const Region& surfaceDamage) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800235 mCurrentState.sequence++;
236 mCurrentState.surfaceDamageRegion = surfaceDamage;
237 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700238 setTransactionFlags(eTransactionNeeded);
239 return true;
240}
241
242bool BufferStateLayer::setApi(int32_t api) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800243 if (mCurrentState.api == api) return false;
244 mCurrentState.sequence++;
245 mCurrentState.api = api;
246 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700247 setTransactionFlags(eTransactionNeeded);
248 return true;
249}
250
251bool BufferStateLayer::setSidebandStream(const sp<NativeHandle>& sidebandStream) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800252 if (mCurrentState.sidebandStream == sidebandStream) return false;
253 mCurrentState.sequence++;
254 mCurrentState.sidebandStream = sidebandStream;
255 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700256 setTransactionFlags(eTransactionNeeded);
257
258 if (!mSidebandStreamChanged.exchange(true)) {
259 // mSidebandStreamChanged was false
260 mFlinger->signalLayerUpdate();
261 }
262 return true;
263}
264
Marissa Walle2ffb422018-10-12 11:33:52 -0700265bool BufferStateLayer::setTransactionCompletedListeners(
266 const std::vector<sp<CallbackHandle>>& handles) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700267 // If there is no handle, we will not send a callback so reset mReleasePreviousBuffer and return
Marissa Walle2ffb422018-10-12 11:33:52 -0700268 if (handles.empty()) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700269 mReleasePreviousBuffer = false;
Marissa Walle2ffb422018-10-12 11:33:52 -0700270 return false;
271 }
272
273 const bool willPresent = willPresentCurrentTransaction();
274
275 for (const auto& handle : handles) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700276 // If this transaction set a buffer on this layer, release its previous buffer
277 handle->releasePreviousBuffer = mReleasePreviousBuffer;
278
Marissa Walle2ffb422018-10-12 11:33:52 -0700279 // If this layer will be presented in this frame
280 if (willPresent) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700281 // If this transaction set an acquire fence on this layer, set its acquire time
282 handle->acquireTime = mCallbackHandleAcquireTime;
283
Marissa Walle2ffb422018-10-12 11:33:52 -0700284 // Notify the transaction completed thread that there is a pending latched callback
285 // handle
Marissa Wall5a68a772018-12-22 17:43:42 -0800286 mFlinger->getTransactionCompletedThread().registerPendingCallbackHandle(handle);
Marissa Walle2ffb422018-10-12 11:33:52 -0700287
288 // Store so latched time and release fence can be set
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800289 mCurrentState.callbackHandles.push_back(handle);
Marissa Walle2ffb422018-10-12 11:33:52 -0700290
291 } else { // If this layer will NOT need to be relatched and presented this frame
292 // Notify the transaction completed thread this handle is done
Marissa Wall5a68a772018-12-22 17:43:42 -0800293 mFlinger->getTransactionCompletedThread().addUnpresentedCallbackHandle(handle);
Marissa Walle2ffb422018-10-12 11:33:52 -0700294 }
295 }
296
Marissa Wallfda30bb2018-10-12 11:34:28 -0700297 mReleasePreviousBuffer = false;
298 mCallbackHandleAcquireTime = -1;
299
Marissa Walle2ffb422018-10-12 11:33:52 -0700300 return willPresent;
301}
302
Marissa Wall61c58622018-07-18 10:12:20 -0700303bool BufferStateLayer::setTransparentRegionHint(const Region& transparent) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800304 mCurrentState.transparentRegionHint = transparent;
305 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700306 setTransactionFlags(eTransactionNeeded);
307 return true;
308}
309
Marissa Wall861616d2018-10-22 12:52:23 -0700310Rect BufferStateLayer::getBufferSize(const State& s) const {
311 // for buffer state layers we use the display frame size as the buffer size.
312 if (getActiveWidth(s) < UINT32_MAX && getActiveHeight(s) < UINT32_MAX) {
313 return Rect(getActiveWidth(s), getActiveHeight(s));
Marissa Wall61c58622018-07-18 10:12:20 -0700314 }
315
Marissa Wall861616d2018-10-22 12:52:23 -0700316 // if the display frame is not defined, use the parent bounds as the buffer size.
317 const auto& p = mDrawingParent.promote();
318 if (p != nullptr) {
Vishnu Nair4351ad52019-02-11 14:13:02 -0800319 Rect parentBounds = Rect(p->getBounds(Region()));
Marissa Wall861616d2018-10-22 12:52:23 -0700320 if (!parentBounds.isEmpty()) {
321 return parentBounds;
322 }
323 }
324
325 // if there is no parent layer, use the buffer's bounds as the buffer size
326 if (s.buffer) {
327 return s.buffer->getBounds();
328 }
329 return Rect::INVALID_RECT;
Marissa Wall61c58622018-07-18 10:12:20 -0700330}
Vishnu Nair4351ad52019-02-11 14:13:02 -0800331
332FloatRect BufferStateLayer::computeSourceBounds(const FloatRect& parentBounds) const {
333 const State& s(getDrawingState());
334 // for buffer state layers we use the display frame size as the buffer size.
335 if (getActiveWidth(s) < UINT32_MAX && getActiveHeight(s) < UINT32_MAX) {
336 return FloatRect(0, 0, getActiveWidth(s), getActiveHeight(s));
337 }
338
339 // if the display frame is not defined, use the parent bounds as the buffer size.
340 return parentBounds;
341}
342
Marissa Wall61c58622018-07-18 10:12:20 -0700343// -----------------------------------------------------------------------
344
345// -----------------------------------------------------------------------
346// Interface implementation for BufferLayer
347// -----------------------------------------------------------------------
348bool BufferStateLayer::fenceHasSignaled() const {
349 if (latchUnsignaledBuffers()) {
350 return true;
351 }
352
353 return getDrawingState().acquireFence->getStatus() == Fence::Status::Signaled;
354}
355
356nsecs_t BufferStateLayer::getDesiredPresentTime() {
357 // TODO(marissaw): support an equivalent to desiredPresentTime for timestats metrics
358 return 0;
359}
360
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800361std::shared_ptr<FenceTime> BufferStateLayer::getCurrentFenceTime() const {
Marissa Wall61c58622018-07-18 10:12:20 -0700362 return std::make_shared<FenceTime>(getDrawingState().acquireFence);
363}
364
365void BufferStateLayer::getDrawingTransformMatrix(float *matrix) {
366 std::copy(std::begin(mTransformMatrix), std::end(mTransformMatrix), matrix);
367}
368
369uint32_t BufferStateLayer::getDrawingTransform() const {
370 return getDrawingState().transform;
371}
372
373ui::Dataspace BufferStateLayer::getDrawingDataSpace() const {
374 return getDrawingState().dataspace;
375}
376
Marissa Wall861616d2018-10-22 12:52:23 -0700377// Crop that applies to the buffer
Marissa Wall61c58622018-07-18 10:12:20 -0700378Rect BufferStateLayer::getDrawingCrop() const {
Marissa Wall861616d2018-10-22 12:52:23 -0700379 const State& s(getDrawingState());
380
381 if (s.crop.isEmpty() && s.buffer) {
382 return s.buffer->getBounds();
Valerie Hau0bc09152018-12-20 07:42:47 -0800383 } else if (s.buffer) {
384 Rect crop = s.crop;
385 crop.left = std::max(crop.left, 0);
386 crop.top = std::max(crop.top, 0);
387 uint32_t bufferWidth = s.buffer->getWidth();
388 uint32_t bufferHeight = s.buffer->getHeight();
389 if (bufferHeight <= std::numeric_limits<int32_t>::max() &&
390 bufferWidth <= std::numeric_limits<int32_t>::max()) {
391 crop.right = std::min(crop.right, static_cast<int32_t>(bufferWidth));
392 crop.bottom = std::min(crop.bottom, static_cast<int32_t>(bufferHeight));
393 }
394 if (!crop.isValid()) {
395 // Crop rect is out of bounds, return whole buffer
396 return s.buffer->getBounds();
397 }
398 return crop;
Marissa Wall861616d2018-10-22 12:52:23 -0700399 }
400 return s.crop;
Marissa Wall61c58622018-07-18 10:12:20 -0700401}
402
403uint32_t BufferStateLayer::getDrawingScalingMode() const {
Marissa Wallec463ac2018-10-08 12:35:04 -0700404 return NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW;
Marissa Wall61c58622018-07-18 10:12:20 -0700405}
406
407Region BufferStateLayer::getDrawingSurfaceDamage() const {
408 return getDrawingState().surfaceDamageRegion;
409}
410
411const HdrMetadata& BufferStateLayer::getDrawingHdrMetadata() const {
412 return getDrawingState().hdrMetadata;
413}
414
415int BufferStateLayer::getDrawingApi() const {
416 return getDrawingState().api;
417}
418
419PixelFormat BufferStateLayer::getPixelFormat() const {
Marissa Wall5aec6412018-11-14 11:49:18 -0800420 if (!mActiveBuffer) {
421 return PIXEL_FORMAT_NONE;
422 }
Marissa Wall61c58622018-07-18 10:12:20 -0700423 return mActiveBuffer->format;
424}
425
426uint64_t BufferStateLayer::getFrameNumber() const {
427 return mFrameNumber;
428}
429
430bool BufferStateLayer::getAutoRefresh() const {
431 // TODO(marissaw): support shared buffer mode
432 return false;
433}
434
435bool BufferStateLayer::getSidebandStreamChanged() const {
436 return mSidebandStreamChanged.load();
437}
438
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800439bool BufferStateLayer::latchSidebandStream(bool& recomputeVisibleRegions) {
Marissa Wall61c58622018-07-18 10:12:20 -0700440 if (mSidebandStreamChanged.exchange(false)) {
441 const State& s(getDrawingState());
442 // mSidebandStreamChanged was true
443 // replicated in LayerBE until FE/BE is ready to be synchronized
444 getBE().compositionInfo.hwc.sidebandStream = s.sidebandStream;
445 if (getBE().compositionInfo.hwc.sidebandStream != nullptr) {
446 setTransactionFlags(eTransactionNeeded);
447 mFlinger->setTransactionFlags(eTraversalNeeded);
448 }
449 recomputeVisibleRegions = true;
450
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800451 return true;
Marissa Wall61c58622018-07-18 10:12:20 -0700452 }
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800453 return false;
Marissa Wall61c58622018-07-18 10:12:20 -0700454}
455
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800456bool BufferStateLayer::hasFrameUpdate() const {
Valerie Hauaa194562019-02-05 16:21:38 -0800457 const State& c(getCurrentState());
458 return mCurrentStateModified && (c.buffer != nullptr || c.bgColorLayer != nullptr);
Marissa Wall61c58622018-07-18 10:12:20 -0700459}
460
461void BufferStateLayer::setFilteringEnabled(bool enabled) {
462 GLConsumer::computeTransformMatrix(mTransformMatrix.data(), mActiveBuffer, mCurrentCrop,
463 mCurrentTransform, enabled);
464}
465
Alec Mouri39801c02018-10-10 10:44:47 -0700466status_t BufferStateLayer::bindTextureImage() {
Marissa Wall61c58622018-07-18 10:12:20 -0700467 const State& s(getDrawingState());
468 auto& engine(mFlinger->getRenderEngine());
469
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000470 return engine.bindExternalTextureBuffer(mTextureName, s.buffer, s.acquireFence, false);
Marissa Wall61c58622018-07-18 10:12:20 -0700471}
472
Alec Mouri86770e52018-09-24 22:40:58 +0000473status_t BufferStateLayer::updateTexImage(bool& /*recomputeVisibleRegions*/, nsecs_t latchTime,
474 const sp<Fence>& releaseFence) {
Marissa Wall61c58622018-07-18 10:12:20 -0700475 const State& s(getDrawingState());
476
477 if (!s.buffer) {
Valerie Hauaa194562019-02-05 16:21:38 -0800478 if (s.bgColorLayer) {
479 for (auto& handle : mDrawingState.callbackHandles) {
480 handle->latchTime = latchTime;
481 }
482 }
Marissa Wall61c58622018-07-18 10:12:20 -0700483 return NO_ERROR;
484 }
485
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700486 const int32_t layerID = getSequence();
487
Marissa Wall61c58622018-07-18 10:12:20 -0700488 // Reject if the layer is invalid
489 uint32_t bufferWidth = s.buffer->width;
490 uint32_t bufferHeight = s.buffer->height;
491
Peiyong Linefefaac2018-08-17 12:27:51 -0700492 if (s.transform & ui::Transform::ROT_90) {
Peiyong Lin3db42342018-08-16 09:15:59 -0700493 std::swap(bufferWidth, bufferHeight);
Marissa Wall61c58622018-07-18 10:12:20 -0700494 }
495
496 if (s.transformToDisplayInverse) {
497 uint32_t invTransform = DisplayDevice::getPrimaryDisplayOrientationTransform();
Peiyong Linefefaac2018-08-17 12:27:51 -0700498 if (invTransform & ui::Transform::ROT_90) {
Peiyong Lin3db42342018-08-16 09:15:59 -0700499 std::swap(bufferWidth, bufferHeight);
Marissa Wall61c58622018-07-18 10:12:20 -0700500 }
501 }
502
Vishnu Nair60356342018-11-13 13:00:45 -0800503 if (getEffectiveScalingMode() == NATIVE_WINDOW_SCALING_MODE_FREEZE &&
Marissa Wall61c58622018-07-18 10:12:20 -0700504 (s.active.w != bufferWidth || s.active.h != bufferHeight)) {
505 ALOGE("[%s] rejecting buffer: "
506 "bufferWidth=%d, bufferHeight=%d, front.active.{w=%d, h=%d}",
507 mName.string(), bufferWidth, bufferHeight, s.active.w, s.active.h);
Yiwei Zhang7e666a52018-11-15 13:33:42 -0800508 mFlinger->mTimeStats->removeTimeRecord(layerID, getFrameNumber());
Marissa Wall61c58622018-07-18 10:12:20 -0700509 return BAD_VALUE;
510 }
511
Marissa Wall5a68a772018-12-22 17:43:42 -0800512 for (auto& handle : mDrawingState.callbackHandles) {
513 handle->latchTime = latchTime;
514 }
Marissa Walle2ffb422018-10-12 11:33:52 -0700515
Marissa Wall61c58622018-07-18 10:12:20 -0700516 // Handle sync fences
Alec Mouri86770e52018-09-24 22:40:58 +0000517 if (SyncFeatures::getInstance().useNativeFenceSync() && releaseFence != Fence::NO_FENCE) {
518 // TODO(alecmouri): Fail somewhere upstream if the fence is invalid.
519 if (!releaseFence->isValid()) {
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800520 mFlinger->mTimeStats->onDestroy(layerID);
Marissa Wall61c58622018-07-18 10:12:20 -0700521 return UNKNOWN_ERROR;
522 }
523
Marissa Wall61c58622018-07-18 10:12:20 -0700524 // Check status of fences first because merging is expensive.
525 // Merging an invalid fence with any other fence results in an
526 // invalid fence.
527 auto currentStatus = s.acquireFence->getStatus();
528 if (currentStatus == Fence::Status::Invalid) {
529 ALOGE("Existing fence has invalid state");
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800530 mFlinger->mTimeStats->onDestroy(layerID);
Marissa Wall61c58622018-07-18 10:12:20 -0700531 return BAD_VALUE;
532 }
533
Alec Mouri86770e52018-09-24 22:40:58 +0000534 auto incomingStatus = releaseFence->getStatus();
Marissa Wall61c58622018-07-18 10:12:20 -0700535 if (incomingStatus == Fence::Status::Invalid) {
536 ALOGE("New fence has invalid state");
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800537 mDrawingState.acquireFence = releaseFence;
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800538 mFlinger->mTimeStats->onDestroy(layerID);
Marissa Wall61c58622018-07-18 10:12:20 -0700539 return BAD_VALUE;
540 }
541
542 // If both fences are signaled or both are unsignaled, we need to merge
543 // them to get an accurate timestamp.
544 if (currentStatus == incomingStatus) {
545 char fenceName[32] = {};
546 snprintf(fenceName, 32, "%.28s:%d", mName.string(), mFrameNumber);
Alec Mouri86770e52018-09-24 22:40:58 +0000547 sp<Fence> mergedFence =
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800548 Fence::merge(fenceName, mDrawingState.acquireFence, releaseFence);
Marissa Wall61c58622018-07-18 10:12:20 -0700549 if (!mergedFence.get()) {
550 ALOGE("failed to merge release fences");
551 // synchronization is broken, the best we can do is hope fences
552 // signal in order so the new fence will act like a union
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800553 mDrawingState.acquireFence = releaseFence;
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800554 mFlinger->mTimeStats->onDestroy(layerID);
Marissa Wall61c58622018-07-18 10:12:20 -0700555 return BAD_VALUE;
556 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800557 mDrawingState.acquireFence = mergedFence;
Marissa Wall61c58622018-07-18 10:12:20 -0700558 } else if (incomingStatus == Fence::Status::Unsignaled) {
559 // If one fence has signaled and the other hasn't, the unsignaled
560 // fence will approximately correspond with the correct timestamp.
561 // There's a small race if both fences signal at about the same time
562 // and their statuses are retrieved with unfortunate timing. However,
563 // by this point, they will have both signaled and only the timestamp
564 // will be slightly off; any dependencies after this point will
565 // already have been met.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800566 mDrawingState.acquireFence = releaseFence;
Marissa Wall61c58622018-07-18 10:12:20 -0700567 }
568 } else {
569 // Bind the new buffer to the GL texture.
570 //
571 // Older devices require the "implicit" synchronization provided
572 // by glEGLImageTargetTexture2DOES, which this method calls. Newer
573 // devices will either call this in Layer::onDraw, or (if it's not
574 // a GL-composited layer) not at all.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800575 status_t err = bindTextureImage();
Marissa Wall61c58622018-07-18 10:12:20 -0700576 if (err != NO_ERROR) {
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800577 mFlinger->mTimeStats->onDestroy(layerID);
Marissa Wall61c58622018-07-18 10:12:20 -0700578 return BAD_VALUE;
579 }
580 }
581
582 // TODO(marissaw): properly support mTimeStats
Yiwei Zhang7e666a52018-11-15 13:33:42 -0800583 mFlinger->mTimeStats->setPostTime(layerID, getFrameNumber(), getName().c_str(), latchTime);
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800584 mFlinger->mTimeStats->setAcquireFence(layerID, getFrameNumber(), getCurrentFenceTime());
Yiwei Zhang7e666a52018-11-15 13:33:42 -0800585 mFlinger->mTimeStats->setLatchTime(layerID, getFrameNumber(), latchTime);
Marissa Wall61c58622018-07-18 10:12:20 -0700586
587 return NO_ERROR;
588}
589
590status_t BufferStateLayer::updateActiveBuffer() {
591 const State& s(getDrawingState());
592
593 if (s.buffer == nullptr) {
594 return BAD_VALUE;
595 }
596
597 mActiveBuffer = s.buffer;
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000598 mActiveBufferFence = s.acquireFence;
Marissa Wall61c58622018-07-18 10:12:20 -0700599 getBE().compositionInfo.mBuffer = mActiveBuffer;
600 getBE().compositionInfo.mBufferSlot = 0;
601
602 return NO_ERROR;
603}
604
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000605bool BufferStateLayer::useCachedBufferForClientComposition() const {
606 // TODO: Store a proper staleness bit to support EGLImage caching.
607 return false;
608}
609
Marissa Wall61c58622018-07-18 10:12:20 -0700610status_t BufferStateLayer::updateFrameNumber(nsecs_t /*latchTime*/) {
611 // TODO(marissaw): support frame history events
612 mCurrentFrameNumber = mFrameNumber;
613 return NO_ERROR;
614}
615
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800616void BufferStateLayer::setHwcLayerBuffer(const sp<const DisplayDevice>& display) {
617 const auto outputLayer = findOutputLayerForDisplay(display);
618 LOG_FATAL_IF(!outputLayer || !outputLayer->getState().hwc);
619 auto& hwcLayer = (*outputLayer->getState().hwc).hwcLayer;
Marissa Wall61c58622018-07-18 10:12:20 -0700620
621 const State& s(getDrawingState());
622
623 // TODO(marissaw): support more than one slot
624 uint32_t hwcSlot = 0;
625
626 auto error = hwcLayer->setBuffer(hwcSlot, s.buffer, s.acquireFence);
627 if (error != HWC2::Error::None) {
628 ALOGE("[%s] Failed to set buffer %p: %s (%d)", mName.string(),
629 s.buffer->handle, to_string(error).c_str(), static_cast<int32_t>(error));
630 }
631
Marissa Wall024a1912018-08-13 13:55:35 -0700632 mCurrentStateModified = false;
Marissa Wall61c58622018-07-18 10:12:20 -0700633 mFrameNumber++;
634}
635
636void BufferStateLayer::onFirstRef() {
Dan Stoza7b1b5a82018-07-31 16:00:21 -0700637 BufferLayer::onFirstRef();
638
Marissa Wall61c58622018-07-18 10:12:20 -0700639 if (const auto display = mFlinger->getDefaultDisplayDevice()) {
640 updateTransformHint(display);
641 }
642}
643
644} // namespace android