blob: b95cd2e21e32e0b57b0e8e041a0d089dbe909044 [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
22#include "BufferStateLayer.h"
Valerie Haua72e2812019-01-23 13:40:39 -080023#include "ColorLayer.h"
Marissa Wall61c58622018-07-18 10:12:20 -070024
Yiwei Zhang7e666a52018-11-15 13:33:42 -080025#include "TimeStats/TimeStats.h"
26
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
Valerie Hau0bc09152018-12-20 07:42:47 -080030#include <limits>
31
Marissa Wall61c58622018-07-18 10:12:20 -070032namespace android {
33
Lloyd Pique42ab75e2018-09-12 20:46:03 -070034// clang-format off
35const std::array<float, 16> BufferStateLayer::IDENTITY_MATRIX{
36 1, 0, 0, 0,
37 0, 1, 0, 0,
38 0, 0, 1, 0,
39 0, 0, 0, 1
40};
41// clang-format on
Marissa Wall61c58622018-07-18 10:12:20 -070042
Vishnu Nair60356342018-11-13 13:00:45 -080043BufferStateLayer::BufferStateLayer(const LayerCreationArgs& args) : BufferLayer(args) {
44 mOverrideScalingMode = NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW;
45}
Lloyd Pique42ab75e2018-09-12 20:46:03 -070046BufferStateLayer::~BufferStateLayer() = default;
Marissa Wall61c58622018-07-18 10:12:20 -070047
48// -----------------------------------------------------------------------
49// Interface implementation for Layer
50// -----------------------------------------------------------------------
Marissa Wallfda30bb2018-10-12 11:34:28 -070051void BufferStateLayer::onLayerDisplayed(const sp<Fence>& releaseFence) {
Marissa Wall5a68a772018-12-22 17:43:42 -080052 // The previous release fence notifies the client that SurfaceFlinger is done with the previous
53 // buffer that was presented on this layer. The first transaction that came in this frame that
54 // replaced the previous buffer on this layer needs this release fence, because the fence will
55 // let the client know when that previous buffer is removed from the screen.
56 //
57 // Every other transaction on this layer does not need a release fence because no other
58 // Transactions that were set on this layer this frame are going to have their preceeding buffer
59 // removed from the display this frame.
60 //
61 // For example, if we have 3 transactions this frame. The first transaction doesn't contain a
62 // buffer so it doesn't need a previous release fence because the layer still needs the previous
63 // buffer. The second transaction contains a buffer so it needs a previous release fence because
64 // the previous buffer will be released this frame. The third transaction also contains a
65 // buffer. It replaces the buffer in the second transaction. The buffer in the second
66 // transaction will now no longer be presented so it is released immediately and the third
67 // transaction doesn't need a previous release fence.
68 for (auto& handle : mDrawingState.callbackHandles) {
69 if (handle->releasePreviousBuffer) {
70 handle->previousReleaseFence = releaseFence;
71 break;
72 }
73 }
Marissa Wall61c58622018-07-18 10:12:20 -070074}
75
76void BufferStateLayer::setTransformHint(uint32_t /*orientation*/) const {
77 // TODO(marissaw): send the transform hint to buffer owner
78 return;
79}
80
81void BufferStateLayer::releasePendingBuffer(nsecs_t /*dequeueReadyTime*/) {
Marissa Wall5a68a772018-12-22 17:43:42 -080082 mFlinger->getTransactionCompletedThread().addPresentedCallbackHandles(
83 mDrawingState.callbackHandles);
84
85 mDrawingState.callbackHandles = {};
Marissa Wall61c58622018-07-18 10:12:20 -070086}
87
Ana Krulec010d2192018-10-08 06:29:54 -070088bool BufferStateLayer::shouldPresentNow(nsecs_t /*expectedPresentTime*/) const {
Marissa Wall61c58622018-07-18 10:12:20 -070089 if (getSidebandStreamChanged() || getAutoRefresh()) {
90 return true;
91 }
92
Marissa Wall024a1912018-08-13 13:55:35 -070093 return hasFrameUpdate();
Marissa Wall61c58622018-07-18 10:12:20 -070094}
95
Marissa Walle2ffb422018-10-12 11:33:52 -070096bool BufferStateLayer::willPresentCurrentTransaction() const {
97 // Returns true if the most recent Transaction applied to CurrentState will be presented.
98 return getSidebandStreamChanged() || getAutoRefresh() ||
Valerie Hauaa194562019-02-05 16:21:38 -080099 (mCurrentState.modified &&
100 (mCurrentState.buffer != nullptr || mCurrentState.bgColorLayer != nullptr));
Marissa Wall61c58622018-07-18 10:12:20 -0700101}
102
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800103bool BufferStateLayer::getTransformToDisplayInverse() const {
104 return mCurrentState.transformToDisplayInverse;
Marissa Wall61c58622018-07-18 10:12:20 -0700105}
106
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800107void BufferStateLayer::pushPendingState() {
108 if (!mCurrentState.modified) {
Marissa Wall61c58622018-07-18 10:12:20 -0700109 return;
110 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800111 mPendingStates.push_back(mCurrentState);
112 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Marissa Wall61c58622018-07-18 10:12:20 -0700113}
114
115bool BufferStateLayer::applyPendingStates(Layer::State* stateToCommit) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800116 const bool stateUpdateAvailable = !mPendingStates.empty();
117 while (!mPendingStates.empty()) {
Marissa Wall61c58622018-07-18 10:12:20 -0700118 popPendingState(stateToCommit);
119 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800120 mCurrentStateModified = stateUpdateAvailable && mCurrentState.modified;
121 mCurrentState.modified = false;
Marissa Wall61c58622018-07-18 10:12:20 -0700122 return stateUpdateAvailable;
123}
124
Marissa Wall861616d2018-10-22 12:52:23 -0700125// Crop that applies to the window
126Rect BufferStateLayer::getCrop(const Layer::State& /*s*/) const {
127 return Rect::INVALID_RECT;
Marissa Wall61c58622018-07-18 10:12:20 -0700128}
129
130bool BufferStateLayer::setTransform(uint32_t transform) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800131 if (mCurrentState.transform == transform) return false;
132 mCurrentState.sequence++;
133 mCurrentState.transform = transform;
134 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700135 setTransactionFlags(eTransactionNeeded);
136 return true;
137}
138
139bool BufferStateLayer::setTransformToDisplayInverse(bool transformToDisplayInverse) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800140 if (mCurrentState.transformToDisplayInverse == transformToDisplayInverse) return false;
141 mCurrentState.sequence++;
142 mCurrentState.transformToDisplayInverse = transformToDisplayInverse;
143 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700144 setTransactionFlags(eTransactionNeeded);
145 return true;
146}
147
148bool BufferStateLayer::setCrop(const Rect& crop) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800149 if (mCurrentState.crop == crop) return false;
150 mCurrentState.sequence++;
151 mCurrentState.crop = crop;
152 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700153 setTransactionFlags(eTransactionNeeded);
154 return true;
155}
156
Marissa Wall861616d2018-10-22 12:52:23 -0700157bool BufferStateLayer::setFrame(const Rect& frame) {
158 int x = frame.left;
159 int y = frame.top;
160 int w = frame.getWidth();
161 int h = frame.getHeight();
162
Marissa Wall0f3242d2018-12-20 15:10:22 -0800163 if (x < 0) {
164 x = 0;
165 w = frame.right;
166 }
167
168 if (y < 0) {
169 y = 0;
170 h = frame.bottom;
171 }
172
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800173 if (mCurrentState.active.transform.tx() == x && mCurrentState.active.transform.ty() == y &&
174 mCurrentState.active.w == w && mCurrentState.active.h == h) {
Marissa Wall861616d2018-10-22 12:52:23 -0700175 return false;
176 }
177
178 if (!frame.isValid()) {
179 x = y = w = h = 0;
180 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800181 mCurrentState.active.transform.set(x, y);
182 mCurrentState.active.w = w;
183 mCurrentState.active.h = h;
Marissa Wall861616d2018-10-22 12:52:23 -0700184
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800185 mCurrentState.sequence++;
186 mCurrentState.modified = true;
Marissa Wall861616d2018-10-22 12:52:23 -0700187 setTransactionFlags(eTransactionNeeded);
188 return true;
189}
190
Marissa Wallfda30bb2018-10-12 11:34:28 -0700191bool BufferStateLayer::setBuffer(const sp<GraphicBuffer>& buffer) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800192 if (mCurrentState.buffer) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700193 mReleasePreviousBuffer = true;
194 }
195
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800196 mCurrentState.sequence++;
197 mCurrentState.buffer = buffer;
198 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700199 setTransactionFlags(eTransactionNeeded);
200 return true;
201}
202
203bool BufferStateLayer::setAcquireFence(const sp<Fence>& fence) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700204 // The acquire fences of BufferStateLayers have already signaled before they are set
205 mCallbackHandleAcquireTime = fence->getSignalTime();
206
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800207 mCurrentState.acquireFence = fence;
208 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700209 setTransactionFlags(eTransactionNeeded);
210 return true;
211}
212
213bool BufferStateLayer::setDataspace(ui::Dataspace dataspace) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800214 if (mCurrentState.dataspace == dataspace) return false;
215 mCurrentState.sequence++;
216 mCurrentState.dataspace = dataspace;
217 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700218 setTransactionFlags(eTransactionNeeded);
219 return true;
220}
221
222bool BufferStateLayer::setHdrMetadata(const HdrMetadata& hdrMetadata) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800223 if (mCurrentState.hdrMetadata == hdrMetadata) return false;
224 mCurrentState.sequence++;
225 mCurrentState.hdrMetadata = hdrMetadata;
226 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700227 setTransactionFlags(eTransactionNeeded);
228 return true;
229}
230
231bool BufferStateLayer::setSurfaceDamageRegion(const Region& surfaceDamage) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800232 mCurrentState.sequence++;
233 mCurrentState.surfaceDamageRegion = surfaceDamage;
234 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700235 setTransactionFlags(eTransactionNeeded);
236 return true;
237}
238
239bool BufferStateLayer::setApi(int32_t api) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800240 if (mCurrentState.api == api) return false;
241 mCurrentState.sequence++;
242 mCurrentState.api = api;
243 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700244 setTransactionFlags(eTransactionNeeded);
245 return true;
246}
247
248bool BufferStateLayer::setSidebandStream(const sp<NativeHandle>& sidebandStream) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800249 if (mCurrentState.sidebandStream == sidebandStream) return false;
250 mCurrentState.sequence++;
251 mCurrentState.sidebandStream = sidebandStream;
252 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700253 setTransactionFlags(eTransactionNeeded);
254
255 if (!mSidebandStreamChanged.exchange(true)) {
256 // mSidebandStreamChanged was false
257 mFlinger->signalLayerUpdate();
258 }
259 return true;
260}
261
Marissa Walle2ffb422018-10-12 11:33:52 -0700262bool BufferStateLayer::setTransactionCompletedListeners(
263 const std::vector<sp<CallbackHandle>>& handles) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700264 // If there is no handle, we will not send a callback so reset mReleasePreviousBuffer and return
Marissa Walle2ffb422018-10-12 11:33:52 -0700265 if (handles.empty()) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700266 mReleasePreviousBuffer = false;
Marissa Walle2ffb422018-10-12 11:33:52 -0700267 return false;
268 }
269
270 const bool willPresent = willPresentCurrentTransaction();
271
272 for (const auto& handle : handles) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700273 // If this transaction set a buffer on this layer, release its previous buffer
274 handle->releasePreviousBuffer = mReleasePreviousBuffer;
275
Marissa Walle2ffb422018-10-12 11:33:52 -0700276 // If this layer will be presented in this frame
277 if (willPresent) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700278 // If this transaction set an acquire fence on this layer, set its acquire time
279 handle->acquireTime = mCallbackHandleAcquireTime;
280
Marissa Walle2ffb422018-10-12 11:33:52 -0700281 // Notify the transaction completed thread that there is a pending latched callback
282 // handle
Marissa Wall5a68a772018-12-22 17:43:42 -0800283 mFlinger->getTransactionCompletedThread().registerPendingCallbackHandle(handle);
Marissa Walle2ffb422018-10-12 11:33:52 -0700284
285 // Store so latched time and release fence can be set
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800286 mCurrentState.callbackHandles.push_back(handle);
Marissa Walle2ffb422018-10-12 11:33:52 -0700287
288 } else { // If this layer will NOT need to be relatched and presented this frame
289 // Notify the transaction completed thread this handle is done
Marissa Wall5a68a772018-12-22 17:43:42 -0800290 mFlinger->getTransactionCompletedThread().addUnpresentedCallbackHandle(handle);
Marissa Walle2ffb422018-10-12 11:33:52 -0700291 }
292 }
293
Marissa Wallfda30bb2018-10-12 11:34:28 -0700294 mReleasePreviousBuffer = false;
295 mCallbackHandleAcquireTime = -1;
296
Marissa Walle2ffb422018-10-12 11:33:52 -0700297 return willPresent;
298}
299
Marissa Wall61c58622018-07-18 10:12:20 -0700300bool BufferStateLayer::setTransparentRegionHint(const Region& transparent) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800301 mCurrentState.transparentRegionHint = transparent;
302 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700303 setTransactionFlags(eTransactionNeeded);
304 return true;
305}
306
Marissa Wall861616d2018-10-22 12:52:23 -0700307Rect BufferStateLayer::getBufferSize(const State& s) const {
308 // for buffer state layers we use the display frame size as the buffer size.
309 if (getActiveWidth(s) < UINT32_MAX && getActiveHeight(s) < UINT32_MAX) {
310 return Rect(getActiveWidth(s), getActiveHeight(s));
Marissa Wall61c58622018-07-18 10:12:20 -0700311 }
312
Marissa Wall861616d2018-10-22 12:52:23 -0700313 // if the display frame is not defined, use the parent bounds as the buffer size.
314 const auto& p = mDrawingParent.promote();
315 if (p != nullptr) {
316 Rect parentBounds = Rect(p->computeBounds(Region()));
317 if (!parentBounds.isEmpty()) {
318 return parentBounds;
319 }
320 }
321
322 // if there is no parent layer, use the buffer's bounds as the buffer size
323 if (s.buffer) {
324 return s.buffer->getBounds();
325 }
326 return Rect::INVALID_RECT;
Marissa Wall61c58622018-07-18 10:12:20 -0700327}
328// -----------------------------------------------------------------------
329
330// -----------------------------------------------------------------------
331// Interface implementation for BufferLayer
332// -----------------------------------------------------------------------
333bool BufferStateLayer::fenceHasSignaled() const {
334 if (latchUnsignaledBuffers()) {
335 return true;
336 }
337
338 return getDrawingState().acquireFence->getStatus() == Fence::Status::Signaled;
339}
340
341nsecs_t BufferStateLayer::getDesiredPresentTime() {
342 // TODO(marissaw): support an equivalent to desiredPresentTime for timestats metrics
343 return 0;
344}
345
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800346std::shared_ptr<FenceTime> BufferStateLayer::getCurrentFenceTime() const {
Marissa Wall61c58622018-07-18 10:12:20 -0700347 return std::make_shared<FenceTime>(getDrawingState().acquireFence);
348}
349
350void BufferStateLayer::getDrawingTransformMatrix(float *matrix) {
351 std::copy(std::begin(mTransformMatrix), std::end(mTransformMatrix), matrix);
352}
353
354uint32_t BufferStateLayer::getDrawingTransform() const {
355 return getDrawingState().transform;
356}
357
358ui::Dataspace BufferStateLayer::getDrawingDataSpace() const {
359 return getDrawingState().dataspace;
360}
361
Marissa Wall861616d2018-10-22 12:52:23 -0700362// Crop that applies to the buffer
Marissa Wall61c58622018-07-18 10:12:20 -0700363Rect BufferStateLayer::getDrawingCrop() const {
Marissa Wall861616d2018-10-22 12:52:23 -0700364 const State& s(getDrawingState());
365
366 if (s.crop.isEmpty() && s.buffer) {
367 return s.buffer->getBounds();
Valerie Hau0bc09152018-12-20 07:42:47 -0800368 } else if (s.buffer) {
369 Rect crop = s.crop;
370 crop.left = std::max(crop.left, 0);
371 crop.top = std::max(crop.top, 0);
372 uint32_t bufferWidth = s.buffer->getWidth();
373 uint32_t bufferHeight = s.buffer->getHeight();
374 if (bufferHeight <= std::numeric_limits<int32_t>::max() &&
375 bufferWidth <= std::numeric_limits<int32_t>::max()) {
376 crop.right = std::min(crop.right, static_cast<int32_t>(bufferWidth));
377 crop.bottom = std::min(crop.bottom, static_cast<int32_t>(bufferHeight));
378 }
379 if (!crop.isValid()) {
380 // Crop rect is out of bounds, return whole buffer
381 return s.buffer->getBounds();
382 }
383 return crop;
Marissa Wall861616d2018-10-22 12:52:23 -0700384 }
385 return s.crop;
Marissa Wall61c58622018-07-18 10:12:20 -0700386}
387
388uint32_t BufferStateLayer::getDrawingScalingMode() const {
Marissa Wallec463ac2018-10-08 12:35:04 -0700389 return NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW;
Marissa Wall61c58622018-07-18 10:12:20 -0700390}
391
392Region BufferStateLayer::getDrawingSurfaceDamage() const {
393 return getDrawingState().surfaceDamageRegion;
394}
395
396const HdrMetadata& BufferStateLayer::getDrawingHdrMetadata() const {
397 return getDrawingState().hdrMetadata;
398}
399
400int BufferStateLayer::getDrawingApi() const {
401 return getDrawingState().api;
402}
403
404PixelFormat BufferStateLayer::getPixelFormat() const {
Marissa Wall5aec6412018-11-14 11:49:18 -0800405 if (!mActiveBuffer) {
406 return PIXEL_FORMAT_NONE;
407 }
Marissa Wall61c58622018-07-18 10:12:20 -0700408 return mActiveBuffer->format;
409}
410
411uint64_t BufferStateLayer::getFrameNumber() const {
412 return mFrameNumber;
413}
414
415bool BufferStateLayer::getAutoRefresh() const {
416 // TODO(marissaw): support shared buffer mode
417 return false;
418}
419
420bool BufferStateLayer::getSidebandStreamChanged() const {
421 return mSidebandStreamChanged.load();
422}
423
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800424bool BufferStateLayer::latchSidebandStream(bool& recomputeVisibleRegions) {
Marissa Wall61c58622018-07-18 10:12:20 -0700425 if (mSidebandStreamChanged.exchange(false)) {
426 const State& s(getDrawingState());
427 // mSidebandStreamChanged was true
428 // replicated in LayerBE until FE/BE is ready to be synchronized
429 getBE().compositionInfo.hwc.sidebandStream = s.sidebandStream;
430 if (getBE().compositionInfo.hwc.sidebandStream != nullptr) {
431 setTransactionFlags(eTransactionNeeded);
432 mFlinger->setTransactionFlags(eTraversalNeeded);
433 }
434 recomputeVisibleRegions = true;
435
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800436 return true;
Marissa Wall61c58622018-07-18 10:12:20 -0700437 }
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800438 return false;
Marissa Wall61c58622018-07-18 10:12:20 -0700439}
440
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800441bool BufferStateLayer::hasFrameUpdate() const {
Valerie Hauaa194562019-02-05 16:21:38 -0800442 const State& c(getCurrentState());
443 return mCurrentStateModified && (c.buffer != nullptr || c.bgColorLayer != nullptr);
Marissa Wall61c58622018-07-18 10:12:20 -0700444}
445
446void BufferStateLayer::setFilteringEnabled(bool enabled) {
447 GLConsumer::computeTransformMatrix(mTransformMatrix.data(), mActiveBuffer, mCurrentCrop,
448 mCurrentTransform, enabled);
449}
450
Alec Mouri39801c02018-10-10 10:44:47 -0700451status_t BufferStateLayer::bindTextureImage() {
Marissa Wall61c58622018-07-18 10:12:20 -0700452 const State& s(getDrawingState());
453 auto& engine(mFlinger->getRenderEngine());
454
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000455 return engine.bindExternalTextureBuffer(mTextureName, s.buffer, s.acquireFence, false);
Marissa Wall61c58622018-07-18 10:12:20 -0700456}
457
Alec Mouri86770e52018-09-24 22:40:58 +0000458status_t BufferStateLayer::updateTexImage(bool& /*recomputeVisibleRegions*/, nsecs_t latchTime,
459 const sp<Fence>& releaseFence) {
Marissa Wall61c58622018-07-18 10:12:20 -0700460 const State& s(getDrawingState());
461
462 if (!s.buffer) {
Valerie Hauaa194562019-02-05 16:21:38 -0800463 if (s.bgColorLayer) {
464 for (auto& handle : mDrawingState.callbackHandles) {
465 handle->latchTime = latchTime;
466 }
467 }
Marissa Wall61c58622018-07-18 10:12:20 -0700468 return NO_ERROR;
469 }
470
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700471 const int32_t layerID = getSequence();
472
Marissa Wall61c58622018-07-18 10:12:20 -0700473 // Reject if the layer is invalid
474 uint32_t bufferWidth = s.buffer->width;
475 uint32_t bufferHeight = s.buffer->height;
476
Peiyong Linefefaac2018-08-17 12:27:51 -0700477 if (s.transform & ui::Transform::ROT_90) {
Peiyong Lin3db42342018-08-16 09:15:59 -0700478 std::swap(bufferWidth, bufferHeight);
Marissa Wall61c58622018-07-18 10:12:20 -0700479 }
480
481 if (s.transformToDisplayInverse) {
482 uint32_t invTransform = DisplayDevice::getPrimaryDisplayOrientationTransform();
Peiyong Linefefaac2018-08-17 12:27:51 -0700483 if (invTransform & ui::Transform::ROT_90) {
Peiyong Lin3db42342018-08-16 09:15:59 -0700484 std::swap(bufferWidth, bufferHeight);
Marissa Wall61c58622018-07-18 10:12:20 -0700485 }
486 }
487
Vishnu Nair60356342018-11-13 13:00:45 -0800488 if (getEffectiveScalingMode() == NATIVE_WINDOW_SCALING_MODE_FREEZE &&
Marissa Wall61c58622018-07-18 10:12:20 -0700489 (s.active.w != bufferWidth || s.active.h != bufferHeight)) {
490 ALOGE("[%s] rejecting buffer: "
491 "bufferWidth=%d, bufferHeight=%d, front.active.{w=%d, h=%d}",
492 mName.string(), bufferWidth, bufferHeight, s.active.w, s.active.h);
Yiwei Zhang7e666a52018-11-15 13:33:42 -0800493 mFlinger->mTimeStats->removeTimeRecord(layerID, getFrameNumber());
Marissa Wall61c58622018-07-18 10:12:20 -0700494 return BAD_VALUE;
495 }
496
Marissa Wall5a68a772018-12-22 17:43:42 -0800497 for (auto& handle : mDrawingState.callbackHandles) {
498 handle->latchTime = latchTime;
499 }
Marissa Walle2ffb422018-10-12 11:33:52 -0700500
Marissa Wall61c58622018-07-18 10:12:20 -0700501 // Handle sync fences
Alec Mouri86770e52018-09-24 22:40:58 +0000502 if (SyncFeatures::getInstance().useNativeFenceSync() && releaseFence != Fence::NO_FENCE) {
503 // TODO(alecmouri): Fail somewhere upstream if the fence is invalid.
504 if (!releaseFence->isValid()) {
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800505 mFlinger->mTimeStats->onDestroy(layerID);
Marissa Wall61c58622018-07-18 10:12:20 -0700506 return UNKNOWN_ERROR;
507 }
508
Marissa Wall61c58622018-07-18 10:12:20 -0700509 // Check status of fences first because merging is expensive.
510 // Merging an invalid fence with any other fence results in an
511 // invalid fence.
512 auto currentStatus = s.acquireFence->getStatus();
513 if (currentStatus == Fence::Status::Invalid) {
514 ALOGE("Existing fence has invalid state");
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800515 mFlinger->mTimeStats->onDestroy(layerID);
Marissa Wall61c58622018-07-18 10:12:20 -0700516 return BAD_VALUE;
517 }
518
Alec Mouri86770e52018-09-24 22:40:58 +0000519 auto incomingStatus = releaseFence->getStatus();
Marissa Wall61c58622018-07-18 10:12:20 -0700520 if (incomingStatus == Fence::Status::Invalid) {
521 ALOGE("New fence has invalid state");
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800522 mDrawingState.acquireFence = releaseFence;
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800523 mFlinger->mTimeStats->onDestroy(layerID);
Marissa Wall61c58622018-07-18 10:12:20 -0700524 return BAD_VALUE;
525 }
526
527 // If both fences are signaled or both are unsignaled, we need to merge
528 // them to get an accurate timestamp.
529 if (currentStatus == incomingStatus) {
530 char fenceName[32] = {};
531 snprintf(fenceName, 32, "%.28s:%d", mName.string(), mFrameNumber);
Alec Mouri86770e52018-09-24 22:40:58 +0000532 sp<Fence> mergedFence =
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800533 Fence::merge(fenceName, mDrawingState.acquireFence, releaseFence);
Marissa Wall61c58622018-07-18 10:12:20 -0700534 if (!mergedFence.get()) {
535 ALOGE("failed to merge release fences");
536 // synchronization is broken, the best we can do is hope fences
537 // signal in order so the new fence will act like a union
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800538 mDrawingState.acquireFence = releaseFence;
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800539 mFlinger->mTimeStats->onDestroy(layerID);
Marissa Wall61c58622018-07-18 10:12:20 -0700540 return BAD_VALUE;
541 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800542 mDrawingState.acquireFence = mergedFence;
Marissa Wall61c58622018-07-18 10:12:20 -0700543 } else if (incomingStatus == Fence::Status::Unsignaled) {
544 // If one fence has signaled and the other hasn't, the unsignaled
545 // fence will approximately correspond with the correct timestamp.
546 // There's a small race if both fences signal at about the same time
547 // and their statuses are retrieved with unfortunate timing. However,
548 // by this point, they will have both signaled and only the timestamp
549 // will be slightly off; any dependencies after this point will
550 // already have been met.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800551 mDrawingState.acquireFence = releaseFence;
Marissa Wall61c58622018-07-18 10:12:20 -0700552 }
553 } else {
554 // Bind the new buffer to the GL texture.
555 //
556 // Older devices require the "implicit" synchronization provided
557 // by glEGLImageTargetTexture2DOES, which this method calls. Newer
558 // devices will either call this in Layer::onDraw, or (if it's not
559 // a GL-composited layer) not at all.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800560 status_t err = bindTextureImage();
Marissa Wall61c58622018-07-18 10:12:20 -0700561 if (err != NO_ERROR) {
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800562 mFlinger->mTimeStats->onDestroy(layerID);
Marissa Wall61c58622018-07-18 10:12:20 -0700563 return BAD_VALUE;
564 }
565 }
566
567 // TODO(marissaw): properly support mTimeStats
Yiwei Zhang7e666a52018-11-15 13:33:42 -0800568 mFlinger->mTimeStats->setPostTime(layerID, getFrameNumber(), getName().c_str(), latchTime);
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800569 mFlinger->mTimeStats->setAcquireFence(layerID, getFrameNumber(), getCurrentFenceTime());
Yiwei Zhang7e666a52018-11-15 13:33:42 -0800570 mFlinger->mTimeStats->setLatchTime(layerID, getFrameNumber(), latchTime);
Marissa Wall61c58622018-07-18 10:12:20 -0700571
572 return NO_ERROR;
573}
574
575status_t BufferStateLayer::updateActiveBuffer() {
576 const State& s(getDrawingState());
577
578 if (s.buffer == nullptr) {
579 return BAD_VALUE;
580 }
581
582 mActiveBuffer = s.buffer;
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000583 mActiveBufferFence = s.acquireFence;
Marissa Wall61c58622018-07-18 10:12:20 -0700584 getBE().compositionInfo.mBuffer = mActiveBuffer;
585 getBE().compositionInfo.mBufferSlot = 0;
586
587 return NO_ERROR;
588}
589
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000590bool BufferStateLayer::useCachedBufferForClientComposition() const {
591 // TODO: Store a proper staleness bit to support EGLImage caching.
592 return false;
593}
594
Marissa Wall61c58622018-07-18 10:12:20 -0700595status_t BufferStateLayer::updateFrameNumber(nsecs_t /*latchTime*/) {
596 // TODO(marissaw): support frame history events
597 mCurrentFrameNumber = mFrameNumber;
598 return NO_ERROR;
599}
600
Dominik Laskowski075d3172018-05-24 15:50:06 -0700601void BufferStateLayer::setHwcLayerBuffer(DisplayId displayId) {
Marissa Wall61c58622018-07-18 10:12:20 -0700602 auto& hwcInfo = getBE().mHwcLayers[displayId];
603 auto& hwcLayer = hwcInfo.layer;
604
605 const State& s(getDrawingState());
606
607 // TODO(marissaw): support more than one slot
608 uint32_t hwcSlot = 0;
609
610 auto error = hwcLayer->setBuffer(hwcSlot, s.buffer, s.acquireFence);
611 if (error != HWC2::Error::None) {
612 ALOGE("[%s] Failed to set buffer %p: %s (%d)", mName.string(),
613 s.buffer->handle, to_string(error).c_str(), static_cast<int32_t>(error));
614 }
615
Marissa Wall024a1912018-08-13 13:55:35 -0700616 mCurrentStateModified = false;
Marissa Wall61c58622018-07-18 10:12:20 -0700617 mFrameNumber++;
618}
619
620void BufferStateLayer::onFirstRef() {
Dan Stoza7b1b5a82018-07-31 16:00:21 -0700621 BufferLayer::onFirstRef();
622
Marissa Wall61c58622018-07-18 10:12:20 -0700623 if (const auto display = mFlinger->getDefaultDisplayDevice()) {
624 updateTransformHint(display);
625 }
626}
627
628} // namespace android