blob: f6b69ebccb84d58d56cb08421458cd8a72006ea3 [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.buffer = buffer;
202 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700203 setTransactionFlags(eTransactionNeeded);
204 return true;
205}
206
207bool BufferStateLayer::setAcquireFence(const sp<Fence>& fence) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700208 // The acquire fences of BufferStateLayers have already signaled before they are set
209 mCallbackHandleAcquireTime = fence->getSignalTime();
210
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800211 mCurrentState.acquireFence = fence;
212 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700213 setTransactionFlags(eTransactionNeeded);
214 return true;
215}
216
217bool BufferStateLayer::setDataspace(ui::Dataspace dataspace) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800218 if (mCurrentState.dataspace == dataspace) return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800219 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
Lloyd Pique0b785d82018-12-04 17:25:27 -0800443 LOG_ALWAYS_FATAL_IF(!getCompositionLayer());
444 mSidebandStream = s.sidebandStream;
445 getCompositionLayer()->editState().frontEnd.sidebandStream = mSidebandStream;
446 if (mSidebandStream != nullptr) {
Marissa Wall61c58622018-07-18 10:12:20 -0700447 setTransactionFlags(eTransactionNeeded);
448 mFlinger->setTransactionFlags(eTraversalNeeded);
449 }
450 recomputeVisibleRegions = true;
451
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800452 return true;
Marissa Wall61c58622018-07-18 10:12:20 -0700453 }
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800454 return false;
Marissa Wall61c58622018-07-18 10:12:20 -0700455}
456
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800457bool BufferStateLayer::hasFrameUpdate() const {
Valerie Hauaa194562019-02-05 16:21:38 -0800458 const State& c(getCurrentState());
459 return mCurrentStateModified && (c.buffer != nullptr || c.bgColorLayer != nullptr);
Marissa Wall61c58622018-07-18 10:12:20 -0700460}
461
462void BufferStateLayer::setFilteringEnabled(bool enabled) {
463 GLConsumer::computeTransformMatrix(mTransformMatrix.data(), mActiveBuffer, mCurrentCrop,
464 mCurrentTransform, enabled);
465}
466
Alec Mouri39801c02018-10-10 10:44:47 -0700467status_t BufferStateLayer::bindTextureImage() {
Marissa Wall61c58622018-07-18 10:12:20 -0700468 const State& s(getDrawingState());
469 auto& engine(mFlinger->getRenderEngine());
470
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000471 return engine.bindExternalTextureBuffer(mTextureName, s.buffer, s.acquireFence, false);
Marissa Wall61c58622018-07-18 10:12:20 -0700472}
473
Alec Mouri56e538f2019-01-14 15:22:01 -0800474status_t BufferStateLayer::updateTexImage(bool& /*recomputeVisibleRegions*/, nsecs_t latchTime) {
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
Alec Mouri56e538f2019-01-14 15:22:01 -0800516 if (!SyncFeatures::getInstance().useNativeFenceSync()) {
Marissa Wall61c58622018-07-18 10:12:20 -0700517 // Bind the new buffer to the GL texture.
518 //
519 // Older devices require the "implicit" synchronization provided
520 // by glEGLImageTargetTexture2DOES, which this method calls. Newer
521 // devices will either call this in Layer::onDraw, or (if it's not
522 // a GL-composited layer) not at all.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800523 status_t err = bindTextureImage();
Marissa Wall61c58622018-07-18 10:12:20 -0700524 if (err != NO_ERROR) {
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800525 mFlinger->mTimeStats->onDestroy(layerID);
Marissa Wall61c58622018-07-18 10:12:20 -0700526 return BAD_VALUE;
527 }
528 }
529
530 // TODO(marissaw): properly support mTimeStats
Yiwei Zhang7e666a52018-11-15 13:33:42 -0800531 mFlinger->mTimeStats->setPostTime(layerID, getFrameNumber(), getName().c_str(), latchTime);
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800532 mFlinger->mTimeStats->setAcquireFence(layerID, getFrameNumber(), getCurrentFenceTime());
Yiwei Zhang7e666a52018-11-15 13:33:42 -0800533 mFlinger->mTimeStats->setLatchTime(layerID, getFrameNumber(), latchTime);
Marissa Wall61c58622018-07-18 10:12:20 -0700534
535 return NO_ERROR;
536}
537
538status_t BufferStateLayer::updateActiveBuffer() {
539 const State& s(getDrawingState());
540
541 if (s.buffer == nullptr) {
542 return BAD_VALUE;
543 }
544
545 mActiveBuffer = s.buffer;
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000546 mActiveBufferFence = s.acquireFence;
Lloyd Pique0b785d82018-12-04 17:25:27 -0800547 auto& layerCompositionState = getCompositionLayer()->editState().frontEnd;
548 layerCompositionState.buffer = mActiveBuffer;
549 layerCompositionState.bufferSlot = 0;
Marissa Wall61c58622018-07-18 10:12:20 -0700550
551 return NO_ERROR;
552}
553
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000554bool BufferStateLayer::useCachedBufferForClientComposition() const {
555 // TODO: Store a proper staleness bit to support EGLImage caching.
556 return false;
557}
558
Marissa Wall61c58622018-07-18 10:12:20 -0700559status_t BufferStateLayer::updateFrameNumber(nsecs_t /*latchTime*/) {
560 // TODO(marissaw): support frame history events
561 mCurrentFrameNumber = mFrameNumber;
562 return NO_ERROR;
563}
564
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800565void BufferStateLayer::setHwcLayerBuffer(const sp<const DisplayDevice>& display) {
566 const auto outputLayer = findOutputLayerForDisplay(display);
567 LOG_FATAL_IF(!outputLayer || !outputLayer->getState().hwc);
568 auto& hwcLayer = (*outputLayer->getState().hwc).hwcLayer;
Marissa Wall61c58622018-07-18 10:12:20 -0700569
570 const State& s(getDrawingState());
571
572 // TODO(marissaw): support more than one slot
573 uint32_t hwcSlot = 0;
574
575 auto error = hwcLayer->setBuffer(hwcSlot, s.buffer, s.acquireFence);
576 if (error != HWC2::Error::None) {
577 ALOGE("[%s] Failed to set buffer %p: %s (%d)", mName.string(),
578 s.buffer->handle, to_string(error).c_str(), static_cast<int32_t>(error));
579 }
580
Marissa Wall024a1912018-08-13 13:55:35 -0700581 mCurrentStateModified = false;
Marissa Wall61c58622018-07-18 10:12:20 -0700582 mFrameNumber++;
583}
584
585void BufferStateLayer::onFirstRef() {
Dan Stoza7b1b5a82018-07-31 16:00:21 -0700586 BufferLayer::onFirstRef();
587
Marissa Wall61c58622018-07-18 10:12:20 -0700588 if (const auto display = mFlinger->getDefaultDisplayDevice()) {
589 updateTransformHint(display);
590 }
591}
592
593} // namespace android