blob: 077be868e9a24690ba50f407e07e4d51f292acb8 [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"
Marissa Wall61c58622018-07-18 10:12:20 -070023
Yiwei Zhang7e666a52018-11-15 13:33:42 -080024#include "TimeStats/TimeStats.h"
25
Marissa Wall61c58622018-07-18 10:12:20 -070026#include <private/gui/SyncFeatures.h>
Peiyong Lincbc184f2018-08-22 13:24:10 -070027#include <renderengine/Image.h>
Marissa Wall61c58622018-07-18 10:12:20 -070028
Valerie Hau0bc09152018-12-20 07:42:47 -080029#include <limits>
30
Marissa Wall61c58622018-07-18 10:12:20 -070031namespace android {
32
Lloyd Pique42ab75e2018-09-12 20:46:03 -070033// clang-format off
34const std::array<float, 16> BufferStateLayer::IDENTITY_MATRIX{
35 1, 0, 0, 0,
36 0, 1, 0, 0,
37 0, 0, 1, 0,
38 0, 0, 0, 1
39};
40// clang-format on
Marissa Wall61c58622018-07-18 10:12:20 -070041
Vishnu Nair60356342018-11-13 13:00:45 -080042BufferStateLayer::BufferStateLayer(const LayerCreationArgs& args) : BufferLayer(args) {
43 mOverrideScalingMode = NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW;
44}
Lloyd Pique42ab75e2018-09-12 20:46:03 -070045BufferStateLayer::~BufferStateLayer() = default;
Marissa Wall61c58622018-07-18 10:12:20 -070046
47// -----------------------------------------------------------------------
48// Interface implementation for Layer
49// -----------------------------------------------------------------------
Marissa Wallfda30bb2018-10-12 11:34:28 -070050void BufferStateLayer::onLayerDisplayed(const sp<Fence>& releaseFence) {
51 // The transaction completed callback can only be sent if the release fence from the PREVIOUS
52 // frame has fired. In practice, we should never actually wait on the previous release fence
53 // but we should store it just in case.
54 mPreviousReleaseFence = releaseFence;
Marissa Wall61c58622018-07-18 10:12:20 -070055}
56
57void BufferStateLayer::setTransformHint(uint32_t /*orientation*/) const {
58 // TODO(marissaw): send the transform hint to buffer owner
59 return;
60}
61
62void BufferStateLayer::releasePendingBuffer(nsecs_t /*dequeueReadyTime*/) {
Marissa Wall61c58622018-07-18 10:12:20 -070063 return;
64}
65
Ana Krulec010d2192018-10-08 06:29:54 -070066bool BufferStateLayer::shouldPresentNow(nsecs_t /*expectedPresentTime*/) const {
Marissa Wall61c58622018-07-18 10:12:20 -070067 if (getSidebandStreamChanged() || getAutoRefresh()) {
68 return true;
69 }
70
Marissa Wall024a1912018-08-13 13:55:35 -070071 return hasFrameUpdate();
Marissa Wall61c58622018-07-18 10:12:20 -070072}
73
Marissa Walle2ffb422018-10-12 11:33:52 -070074bool BufferStateLayer::willPresentCurrentTransaction() const {
Ady Abraham83729882018-12-07 12:26:48 -080075 Mutex::Autolock lock(mStateMutex);
Marissa Walle2ffb422018-10-12 11:33:52 -070076 // Returns true if the most recent Transaction applied to CurrentState will be presented.
77 return getSidebandStreamChanged() || getAutoRefresh() ||
Ady Abraham83729882018-12-07 12:26:48 -080078 (mState.current.modified && mState.current.buffer != nullptr);
Marissa Wall61c58622018-07-18 10:12:20 -070079}
80
Ady Abraham83729882018-12-07 12:26:48 -080081bool BufferStateLayer::getTransformToDisplayInverseLocked() const {
82 return mState.current.transformToDisplayInverse;
Marissa Wall61c58622018-07-18 10:12:20 -070083}
84
Ady Abraham83729882018-12-07 12:26:48 -080085void BufferStateLayer::pushPendingStateLocked() {
86 if (!mState.current.modified) {
Marissa Wall61c58622018-07-18 10:12:20 -070087 return;
88 }
Ady Abraham83729882018-12-07 12:26:48 -080089 mState.pending.push_back(mState.current);
90 ATRACE_INT(mTransactionName.string(), mState.pending.size());
Marissa Wall61c58622018-07-18 10:12:20 -070091}
92
93bool BufferStateLayer::applyPendingStates(Layer::State* stateToCommit) {
Ady Abraham83729882018-12-07 12:26:48 -080094 const bool stateUpdateAvailable = !mState.pending.empty();
95 while (!mState.pending.empty()) {
Marissa Wall61c58622018-07-18 10:12:20 -070096 popPendingState(stateToCommit);
97 }
Ady Abraham83729882018-12-07 12:26:48 -080098 mCurrentStateModified = stateUpdateAvailable && mState.current.modified;
99 mState.current.modified = false;
Marissa Wall61c58622018-07-18 10:12:20 -0700100 return stateUpdateAvailable;
101}
102
Marissa Wall861616d2018-10-22 12:52:23 -0700103// Crop that applies to the window
104Rect BufferStateLayer::getCrop(const Layer::State& /*s*/) const {
105 return Rect::INVALID_RECT;
Marissa Wall61c58622018-07-18 10:12:20 -0700106}
107
108bool BufferStateLayer::setTransform(uint32_t transform) {
Ady Abraham83729882018-12-07 12:26:48 -0800109 Mutex::Autolock lock(mStateMutex);
110 if (mState.current.transform == transform) return false;
111 mState.current.sequence++;
112 mState.current.transform = transform;
113 mState.current.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700114 setTransactionFlags(eTransactionNeeded);
115 return true;
116}
117
118bool BufferStateLayer::setTransformToDisplayInverse(bool transformToDisplayInverse) {
Ady Abraham83729882018-12-07 12:26:48 -0800119 Mutex::Autolock lock(mStateMutex);
120 if (mState.current.transformToDisplayInverse == transformToDisplayInverse) return false;
121 mState.current.sequence++;
122 mState.current.transformToDisplayInverse = transformToDisplayInverse;
123 mState.current.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700124 setTransactionFlags(eTransactionNeeded);
125 return true;
126}
127
128bool BufferStateLayer::setCrop(const Rect& crop) {
Ady Abraham83729882018-12-07 12:26:48 -0800129 Mutex::Autolock lock(mStateMutex);
130 if (mState.current.crop == crop) return false;
131 mState.current.sequence++;
132 mState.current.crop = crop;
133 mState.current.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700134 setTransactionFlags(eTransactionNeeded);
135 return true;
136}
137
Marissa Wall861616d2018-10-22 12:52:23 -0700138bool BufferStateLayer::setFrame(const Rect& frame) {
139 int x = frame.left;
140 int y = frame.top;
141 int w = frame.getWidth();
142 int h = frame.getHeight();
143
Marissa Wall0f3242d2018-12-20 15:10:22 -0800144 if (x < 0) {
145 x = 0;
146 w = frame.right;
147 }
148
149 if (y < 0) {
150 y = 0;
151 h = frame.bottom;
152 }
153
Ady Abraham83729882018-12-07 12:26:48 -0800154 Mutex::Autolock lock(mStateMutex);
155 if (mState.current.active.transform.tx() == x && mState.current.active.transform.ty() == y &&
156 mState.current.active.w == w && mState.current.active.h == h) {
Marissa Wall861616d2018-10-22 12:52:23 -0700157 return false;
158 }
159
160 if (!frame.isValid()) {
161 x = y = w = h = 0;
162 }
Ady Abraham83729882018-12-07 12:26:48 -0800163 mState.current.active.transform.set(x, y);
164 mState.current.active.w = w;
165 mState.current.active.h = h;
Marissa Wall861616d2018-10-22 12:52:23 -0700166
Ady Abraham83729882018-12-07 12:26:48 -0800167 mState.current.sequence++;
168 mState.current.modified = true;
Marissa Wall861616d2018-10-22 12:52:23 -0700169 setTransactionFlags(eTransactionNeeded);
170 return true;
171}
172
Marissa Wallfda30bb2018-10-12 11:34:28 -0700173bool BufferStateLayer::setBuffer(const sp<GraphicBuffer>& buffer) {
Ady Abraham83729882018-12-07 12:26:48 -0800174 Mutex::Autolock lock(mStateMutex);
175 if (mState.current.buffer) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700176 mReleasePreviousBuffer = true;
177 }
178
Ady Abraham83729882018-12-07 12:26:48 -0800179 mState.current.sequence++;
180 mState.current.buffer = buffer;
181 mState.current.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700182 setTransactionFlags(eTransactionNeeded);
183 return true;
184}
185
186bool BufferStateLayer::setAcquireFence(const sp<Fence>& fence) {
Ady Abraham83729882018-12-07 12:26:48 -0800187 Mutex::Autolock lock(mStateMutex);
Marissa Wallfda30bb2018-10-12 11:34:28 -0700188 // The acquire fences of BufferStateLayers have already signaled before they are set
189 mCallbackHandleAcquireTime = fence->getSignalTime();
190
Ady Abraham83729882018-12-07 12:26:48 -0800191 mState.current.acquireFence = fence;
192 mState.current.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700193 setTransactionFlags(eTransactionNeeded);
194 return true;
195}
196
197bool BufferStateLayer::setDataspace(ui::Dataspace dataspace) {
Ady Abraham83729882018-12-07 12:26:48 -0800198 Mutex::Autolock lock(mStateMutex);
199 if (mState.current.dataspace == dataspace) return false;
200 mState.current.sequence++;
201 mState.current.dataspace = dataspace;
202 mState.current.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700203 setTransactionFlags(eTransactionNeeded);
204 return true;
205}
206
207bool BufferStateLayer::setHdrMetadata(const HdrMetadata& hdrMetadata) {
Ady Abraham83729882018-12-07 12:26:48 -0800208 Mutex::Autolock lock(mStateMutex);
209 if (mState.current.hdrMetadata == hdrMetadata) return false;
210 mState.current.sequence++;
211 mState.current.hdrMetadata = hdrMetadata;
212 mState.current.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700213 setTransactionFlags(eTransactionNeeded);
214 return true;
215}
216
217bool BufferStateLayer::setSurfaceDamageRegion(const Region& surfaceDamage) {
Ady Abraham83729882018-12-07 12:26:48 -0800218 Mutex::Autolock lock(mStateMutex);
219 mState.current.sequence++;
220 mState.current.surfaceDamageRegion = surfaceDamage;
221 mState.current.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700222 setTransactionFlags(eTransactionNeeded);
223 return true;
224}
225
226bool BufferStateLayer::setApi(int32_t api) {
Ady Abraham83729882018-12-07 12:26:48 -0800227 Mutex::Autolock lock(mStateMutex);
228 if (mState.current.api == api) return false;
229 mState.current.sequence++;
230 mState.current.api = api;
231 mState.current.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700232 setTransactionFlags(eTransactionNeeded);
233 return true;
234}
235
236bool BufferStateLayer::setSidebandStream(const sp<NativeHandle>& sidebandStream) {
Ady Abraham83729882018-12-07 12:26:48 -0800237 Mutex::Autolock lock(mStateMutex);
238 if (mState.current.sidebandStream == sidebandStream) return false;
239 mState.current.sequence++;
240 mState.current.sidebandStream = sidebandStream;
241 mState.current.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700242 setTransactionFlags(eTransactionNeeded);
243
244 if (!mSidebandStreamChanged.exchange(true)) {
245 // mSidebandStreamChanged was false
246 mFlinger->signalLayerUpdate();
247 }
248 return true;
249}
250
Marissa Walle2ffb422018-10-12 11:33:52 -0700251bool BufferStateLayer::setTransactionCompletedListeners(
252 const std::vector<sp<CallbackHandle>>& handles) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700253 // If there is no handle, we will not send a callback so reset mReleasePreviousBuffer and return
Marissa Walle2ffb422018-10-12 11:33:52 -0700254 if (handles.empty()) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700255 mReleasePreviousBuffer = false;
Marissa Walle2ffb422018-10-12 11:33:52 -0700256 return false;
257 }
258
259 const bool willPresent = willPresentCurrentTransaction();
260
261 for (const auto& handle : handles) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700262 // If this transaction set a buffer on this layer, release its previous buffer
263 handle->releasePreviousBuffer = mReleasePreviousBuffer;
264
Marissa Walle2ffb422018-10-12 11:33:52 -0700265 // If this layer will be presented in this frame
266 if (willPresent) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700267 // If this transaction set an acquire fence on this layer, set its acquire time
268 handle->acquireTime = mCallbackHandleAcquireTime;
269
Marissa Walle2ffb422018-10-12 11:33:52 -0700270 // Notify the transaction completed thread that there is a pending latched callback
271 // handle
272 mFlinger->getTransactionCompletedThread().registerPendingLatchedCallbackHandle(handle);
273
274 // Store so latched time and release fence can be set
Ady Abraham83729882018-12-07 12:26:48 -0800275 {
276 Mutex::Autolock lock(mStateMutex);
277 mState.current.callbackHandles.push_back(handle);
278 }
Marissa Walle2ffb422018-10-12 11:33:52 -0700279
280 } else { // If this layer will NOT need to be relatched and presented this frame
281 // Notify the transaction completed thread this handle is done
282 mFlinger->getTransactionCompletedThread().addUnlatchedCallbackHandle(handle);
283 }
284 }
285
Marissa Wallfda30bb2018-10-12 11:34:28 -0700286 mReleasePreviousBuffer = false;
287 mCallbackHandleAcquireTime = -1;
288
Marissa Walle2ffb422018-10-12 11:33:52 -0700289 return willPresent;
290}
291
Marissa Wall61c58622018-07-18 10:12:20 -0700292bool BufferStateLayer::setTransparentRegionHint(const Region& transparent) {
Ady Abraham83729882018-12-07 12:26:48 -0800293 Mutex::Autolock lock(mStateMutex);
294 mState.current.transparentRegionHint = transparent;
295 mState.current.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700296 setTransactionFlags(eTransactionNeeded);
297 return true;
298}
299
Marissa Wall861616d2018-10-22 12:52:23 -0700300Rect BufferStateLayer::getBufferSize(const State& s) const {
301 // for buffer state layers we use the display frame size as the buffer size.
302 if (getActiveWidth(s) < UINT32_MAX && getActiveHeight(s) < UINT32_MAX) {
303 return Rect(getActiveWidth(s), getActiveHeight(s));
Marissa Wall61c58622018-07-18 10:12:20 -0700304 }
305
Marissa Wall861616d2018-10-22 12:52:23 -0700306 // if the display frame is not defined, use the parent bounds as the buffer size.
307 const auto& p = mDrawingParent.promote();
308 if (p != nullptr) {
309 Rect parentBounds = Rect(p->computeBounds(Region()));
310 if (!parentBounds.isEmpty()) {
311 return parentBounds;
312 }
313 }
314
315 // if there is no parent layer, use the buffer's bounds as the buffer size
316 if (s.buffer) {
317 return s.buffer->getBounds();
318 }
319 return Rect::INVALID_RECT;
Marissa Wall61c58622018-07-18 10:12:20 -0700320}
321// -----------------------------------------------------------------------
322
323// -----------------------------------------------------------------------
324// Interface implementation for BufferLayer
325// -----------------------------------------------------------------------
326bool BufferStateLayer::fenceHasSignaled() const {
327 if (latchUnsignaledBuffers()) {
328 return true;
329 }
330
Ady Abraham83729882018-12-07 12:26:48 -0800331 Mutex::Autolock lock(mStateMutex);
Marissa Wall61c58622018-07-18 10:12:20 -0700332 return getDrawingState().acquireFence->getStatus() == Fence::Status::Signaled;
333}
334
335nsecs_t BufferStateLayer::getDesiredPresentTime() {
336 // TODO(marissaw): support an equivalent to desiredPresentTime for timestats metrics
337 return 0;
338}
339
Ady Abraham83729882018-12-07 12:26:48 -0800340std::shared_ptr<FenceTime> BufferStateLayer::getCurrentFenceTimeLocked() const {
Marissa Wall61c58622018-07-18 10:12:20 -0700341 return std::make_shared<FenceTime>(getDrawingState().acquireFence);
342}
343
344void BufferStateLayer::getDrawingTransformMatrix(float *matrix) {
345 std::copy(std::begin(mTransformMatrix), std::end(mTransformMatrix), matrix);
346}
347
348uint32_t BufferStateLayer::getDrawingTransform() const {
349 return getDrawingState().transform;
350}
351
352ui::Dataspace BufferStateLayer::getDrawingDataSpace() const {
353 return getDrawingState().dataspace;
354}
355
Marissa Wall861616d2018-10-22 12:52:23 -0700356// Crop that applies to the buffer
Marissa Wall61c58622018-07-18 10:12:20 -0700357Rect BufferStateLayer::getDrawingCrop() const {
Marissa Wall861616d2018-10-22 12:52:23 -0700358 const State& s(getDrawingState());
359
360 if (s.crop.isEmpty() && s.buffer) {
361 return s.buffer->getBounds();
Valerie Hau0bc09152018-12-20 07:42:47 -0800362 } else if (s.buffer) {
363 Rect crop = s.crop;
364 crop.left = std::max(crop.left, 0);
365 crop.top = std::max(crop.top, 0);
366 uint32_t bufferWidth = s.buffer->getWidth();
367 uint32_t bufferHeight = s.buffer->getHeight();
368 if (bufferHeight <= std::numeric_limits<int32_t>::max() &&
369 bufferWidth <= std::numeric_limits<int32_t>::max()) {
370 crop.right = std::min(crop.right, static_cast<int32_t>(bufferWidth));
371 crop.bottom = std::min(crop.bottom, static_cast<int32_t>(bufferHeight));
372 }
373 if (!crop.isValid()) {
374 // Crop rect is out of bounds, return whole buffer
375 return s.buffer->getBounds();
376 }
377 return crop;
Marissa Wall861616d2018-10-22 12:52:23 -0700378 }
379 return s.crop;
Marissa Wall61c58622018-07-18 10:12:20 -0700380}
381
382uint32_t BufferStateLayer::getDrawingScalingMode() const {
Marissa Wallec463ac2018-10-08 12:35:04 -0700383 return NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW;
Marissa Wall61c58622018-07-18 10:12:20 -0700384}
385
386Region BufferStateLayer::getDrawingSurfaceDamage() const {
Ady Abraham83729882018-12-07 12:26:48 -0800387 Mutex::Autolock lock(mStateMutex);
Marissa Wall61c58622018-07-18 10:12:20 -0700388 return getDrawingState().surfaceDamageRegion;
389}
390
391const HdrMetadata& BufferStateLayer::getDrawingHdrMetadata() const {
Ady Abraham83729882018-12-07 12:26:48 -0800392 Mutex::Autolock lock(mStateMutex);
Marissa Wall61c58622018-07-18 10:12:20 -0700393 return getDrawingState().hdrMetadata;
394}
395
396int BufferStateLayer::getDrawingApi() const {
Ady Abraham83729882018-12-07 12:26:48 -0800397 Mutex::Autolock lock(mStateMutex);
Marissa Wall61c58622018-07-18 10:12:20 -0700398 return getDrawingState().api;
399}
400
401PixelFormat BufferStateLayer::getPixelFormat() const {
Marissa Wall5aec6412018-11-14 11:49:18 -0800402 if (!mActiveBuffer) {
403 return PIXEL_FORMAT_NONE;
404 }
Marissa Wall61c58622018-07-18 10:12:20 -0700405 return mActiveBuffer->format;
406}
407
408uint64_t BufferStateLayer::getFrameNumber() const {
409 return mFrameNumber;
410}
411
412bool BufferStateLayer::getAutoRefresh() const {
413 // TODO(marissaw): support shared buffer mode
414 return false;
415}
416
417bool BufferStateLayer::getSidebandStreamChanged() const {
418 return mSidebandStreamChanged.load();
419}
420
421std::optional<Region> BufferStateLayer::latchSidebandStream(bool& recomputeVisibleRegions) {
Ady Abraham83729882018-12-07 12:26:48 -0800422 Mutex::Autolock lock(mStateMutex);
Marissa Wall61c58622018-07-18 10:12:20 -0700423 if (mSidebandStreamChanged.exchange(false)) {
424 const State& s(getDrawingState());
425 // mSidebandStreamChanged was true
426 // replicated in LayerBE until FE/BE is ready to be synchronized
427 getBE().compositionInfo.hwc.sidebandStream = s.sidebandStream;
428 if (getBE().compositionInfo.hwc.sidebandStream != nullptr) {
429 setTransactionFlags(eTransactionNeeded);
430 mFlinger->setTransactionFlags(eTraversalNeeded);
431 }
432 recomputeVisibleRegions = true;
433
Ady Abraham83729882018-12-07 12:26:48 -0800434 return getTransformLocked().transform(Region(Rect(s.active.w, s.active.h)));
Marissa Wall61c58622018-07-18 10:12:20 -0700435 }
436 return {};
437}
438
Ady Abraham83729882018-12-07 12:26:48 -0800439bool BufferStateLayer::hasFrameUpdateLocked() const {
Marissa Wall024a1912018-08-13 13:55:35 -0700440 return mCurrentStateModified && getCurrentState().buffer != nullptr;
Marissa Wall61c58622018-07-18 10:12:20 -0700441}
442
443void BufferStateLayer::setFilteringEnabled(bool enabled) {
444 GLConsumer::computeTransformMatrix(mTransformMatrix.data(), mActiveBuffer, mCurrentCrop,
445 mCurrentTransform, enabled);
446}
447
Alec Mouri39801c02018-10-10 10:44:47 -0700448status_t BufferStateLayer::bindTextureImage() {
Ady Abraham83729882018-12-07 12:26:48 -0800449 Mutex::Autolock lock(mStateMutex);
450 return bindTextureImageLocked();
451}
452status_t BufferStateLayer::bindTextureImageLocked() {
Marissa Wall61c58622018-07-18 10:12:20 -0700453 const State& s(getDrawingState());
454 auto& engine(mFlinger->getRenderEngine());
455
Marissa Wall61c58622018-07-18 10:12:20 -0700456 engine.checkErrors();
457
Alec Mouri39801c02018-10-10 10:44:47 -0700458 // TODO(marissaw): once buffers are cached, don't create a new image everytime
459 mTextureImage = engine.createImage();
Marissa Wall61c58622018-07-18 10:12:20 -0700460
461 bool created =
462 mTextureImage->setNativeWindowBuffer(s.buffer->getNativeBuffer(),
463 s.buffer->getUsage() & GRALLOC_USAGE_PROTECTED);
464 if (!created) {
465 ALOGE("Failed to create image. size=%ux%u st=%u usage=%#" PRIx64 " fmt=%d",
466 s.buffer->getWidth(), s.buffer->getHeight(), s.buffer->getStride(),
467 s.buffer->getUsage(), s.buffer->getPixelFormat());
468 engine.bindExternalTextureImage(mTextureName, *engine.createImage());
469 return NO_INIT;
470 }
471
472 engine.bindExternalTextureImage(mTextureName, *mTextureImage);
473
474 // Wait for the new buffer to be ready.
475 if (s.acquireFence->isValid()) {
476 if (SyncFeatures::getInstance().useWaitSync()) {
477 base::unique_fd fenceFd(s.acquireFence->dup());
478 if (fenceFd == -1) {
479 ALOGE("error dup'ing fence fd: %d", errno);
480 return -errno;
481 }
482 if (!engine.waitFence(std::move(fenceFd))) {
483 ALOGE("failed to wait on fence fd");
484 return UNKNOWN_ERROR;
485 }
486 } else {
487 status_t err = s.acquireFence->waitForever("BufferStateLayer::bindTextureImage");
488 if (err != NO_ERROR) {
489 ALOGE("error waiting for fence: %d", err);
490 return err;
491 }
492 }
493 }
494
495 return NO_ERROR;
496}
497
Alec Mouri86770e52018-09-24 22:40:58 +0000498status_t BufferStateLayer::updateTexImage(bool& /*recomputeVisibleRegions*/, nsecs_t latchTime,
499 const sp<Fence>& releaseFence) {
Marissa Wall61c58622018-07-18 10:12:20 -0700500 const State& s(getDrawingState());
501
502 if (!s.buffer) {
503 return NO_ERROR;
504 }
505
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700506 const int32_t layerID = getSequence();
507
Marissa Wall61c58622018-07-18 10:12:20 -0700508 // Reject if the layer is invalid
509 uint32_t bufferWidth = s.buffer->width;
510 uint32_t bufferHeight = s.buffer->height;
511
Peiyong Linefefaac2018-08-17 12:27:51 -0700512 if (s.transform & ui::Transform::ROT_90) {
Peiyong Lin3db42342018-08-16 09:15:59 -0700513 std::swap(bufferWidth, bufferHeight);
Marissa Wall61c58622018-07-18 10:12:20 -0700514 }
515
516 if (s.transformToDisplayInverse) {
517 uint32_t invTransform = DisplayDevice::getPrimaryDisplayOrientationTransform();
Peiyong Linefefaac2018-08-17 12:27:51 -0700518 if (invTransform & ui::Transform::ROT_90) {
Peiyong Lin3db42342018-08-16 09:15:59 -0700519 std::swap(bufferWidth, bufferHeight);
Marissa Wall61c58622018-07-18 10:12:20 -0700520 }
521 }
522
Vishnu Nair60356342018-11-13 13:00:45 -0800523 if (getEffectiveScalingMode() == NATIVE_WINDOW_SCALING_MODE_FREEZE &&
Marissa Wall61c58622018-07-18 10:12:20 -0700524 (s.active.w != bufferWidth || s.active.h != bufferHeight)) {
525 ALOGE("[%s] rejecting buffer: "
526 "bufferWidth=%d, bufferHeight=%d, front.active.{w=%d, h=%d}",
527 mName.string(), bufferWidth, bufferHeight, s.active.w, s.active.h);
Yiwei Zhang7e666a52018-11-15 13:33:42 -0800528 mFlinger->mTimeStats->removeTimeRecord(layerID, getFrameNumber());
Marissa Wall61c58622018-07-18 10:12:20 -0700529 return BAD_VALUE;
530 }
531
Marissa Wallfda30bb2018-10-12 11:34:28 -0700532 mFlinger->getTransactionCompletedThread()
533 .addLatchedCallbackHandles(getDrawingState().callbackHandles, latchTime,
534 mPreviousReleaseFence);
Marissa Walle2ffb422018-10-12 11:33:52 -0700535
Marissa Wall61c58622018-07-18 10:12:20 -0700536 // Handle sync fences
Alec Mouri86770e52018-09-24 22:40:58 +0000537 if (SyncFeatures::getInstance().useNativeFenceSync() && releaseFence != Fence::NO_FENCE) {
538 // TODO(alecmouri): Fail somewhere upstream if the fence is invalid.
539 if (!releaseFence->isValid()) {
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800540 mFlinger->mTimeStats->onDestroy(layerID);
Marissa Wall61c58622018-07-18 10:12:20 -0700541 return UNKNOWN_ERROR;
542 }
543
Marissa Wall61c58622018-07-18 10:12:20 -0700544 // Check status of fences first because merging is expensive.
545 // Merging an invalid fence with any other fence results in an
546 // invalid fence.
547 auto currentStatus = s.acquireFence->getStatus();
548 if (currentStatus == Fence::Status::Invalid) {
549 ALOGE("Existing fence has invalid state");
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800550 mFlinger->mTimeStats->onDestroy(layerID);
Marissa Wall61c58622018-07-18 10:12:20 -0700551 return BAD_VALUE;
552 }
553
Alec Mouri86770e52018-09-24 22:40:58 +0000554 auto incomingStatus = releaseFence->getStatus();
Marissa Wall61c58622018-07-18 10:12:20 -0700555 if (incomingStatus == Fence::Status::Invalid) {
556 ALOGE("New fence has invalid state");
Ady Abraham83729882018-12-07 12:26:48 -0800557 mState.drawing.acquireFence = releaseFence;
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800558 mFlinger->mTimeStats->onDestroy(layerID);
Marissa Wall61c58622018-07-18 10:12:20 -0700559 return BAD_VALUE;
560 }
561
562 // If both fences are signaled or both are unsignaled, we need to merge
563 // them to get an accurate timestamp.
564 if (currentStatus == incomingStatus) {
565 char fenceName[32] = {};
566 snprintf(fenceName, 32, "%.28s:%d", mName.string(), mFrameNumber);
Alec Mouri86770e52018-09-24 22:40:58 +0000567 sp<Fence> mergedFence =
Ady Abraham83729882018-12-07 12:26:48 -0800568 Fence::merge(fenceName, mState.drawing.acquireFence, releaseFence);
Marissa Wall61c58622018-07-18 10:12:20 -0700569 if (!mergedFence.get()) {
570 ALOGE("failed to merge release fences");
571 // synchronization is broken, the best we can do is hope fences
572 // signal in order so the new fence will act like a union
Ady Abraham83729882018-12-07 12:26:48 -0800573 mState.drawing.acquireFence = releaseFence;
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800574 mFlinger->mTimeStats->onDestroy(layerID);
Marissa Wall61c58622018-07-18 10:12:20 -0700575 return BAD_VALUE;
576 }
Ady Abraham83729882018-12-07 12:26:48 -0800577 mState.drawing.acquireFence = mergedFence;
Marissa Wall61c58622018-07-18 10:12:20 -0700578 } else if (incomingStatus == Fence::Status::Unsignaled) {
579 // If one fence has signaled and the other hasn't, the unsignaled
580 // fence will approximately correspond with the correct timestamp.
581 // There's a small race if both fences signal at about the same time
582 // and their statuses are retrieved with unfortunate timing. However,
583 // by this point, they will have both signaled and only the timestamp
584 // will be slightly off; any dependencies after this point will
585 // already have been met.
Ady Abraham83729882018-12-07 12:26:48 -0800586 mState.drawing.acquireFence = releaseFence;
Marissa Wall61c58622018-07-18 10:12:20 -0700587 }
588 } else {
589 // Bind the new buffer to the GL texture.
590 //
591 // Older devices require the "implicit" synchronization provided
592 // by glEGLImageTargetTexture2DOES, which this method calls. Newer
593 // devices will either call this in Layer::onDraw, or (if it's not
594 // a GL-composited layer) not at all.
Ady Abraham83729882018-12-07 12:26:48 -0800595 status_t err = bindTextureImageLocked();
Marissa Wall61c58622018-07-18 10:12:20 -0700596 if (err != NO_ERROR) {
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800597 mFlinger->mTimeStats->onDestroy(layerID);
Marissa Wall61c58622018-07-18 10:12:20 -0700598 return BAD_VALUE;
599 }
600 }
601
602 // TODO(marissaw): properly support mTimeStats
Yiwei Zhang7e666a52018-11-15 13:33:42 -0800603 mFlinger->mTimeStats->setPostTime(layerID, getFrameNumber(), getName().c_str(), latchTime);
Ady Abraham83729882018-12-07 12:26:48 -0800604 mFlinger->mTimeStats->setAcquireFence(layerID, getFrameNumber(), getCurrentFenceTimeLocked());
Yiwei Zhang7e666a52018-11-15 13:33:42 -0800605 mFlinger->mTimeStats->setLatchTime(layerID, getFrameNumber(), latchTime);
Marissa Wall61c58622018-07-18 10:12:20 -0700606
607 return NO_ERROR;
608}
609
610status_t BufferStateLayer::updateActiveBuffer() {
611 const State& s(getDrawingState());
612
613 if (s.buffer == nullptr) {
614 return BAD_VALUE;
615 }
616
617 mActiveBuffer = s.buffer;
618 getBE().compositionInfo.mBuffer = mActiveBuffer;
619 getBE().compositionInfo.mBufferSlot = 0;
620
621 return NO_ERROR;
622}
623
624status_t BufferStateLayer::updateFrameNumber(nsecs_t /*latchTime*/) {
625 // TODO(marissaw): support frame history events
626 mCurrentFrameNumber = mFrameNumber;
627 return NO_ERROR;
628}
629
Dominik Laskowski075d3172018-05-24 15:50:06 -0700630void BufferStateLayer::setHwcLayerBuffer(DisplayId displayId) {
Ady Abraham83729882018-12-07 12:26:48 -0800631 Mutex::Autolock lock(mStateMutex);
Marissa Wall61c58622018-07-18 10:12:20 -0700632 auto& hwcInfo = getBE().mHwcLayers[displayId];
633 auto& hwcLayer = hwcInfo.layer;
634
635 const State& s(getDrawingState());
636
637 // TODO(marissaw): support more than one slot
638 uint32_t hwcSlot = 0;
639
640 auto error = hwcLayer->setBuffer(hwcSlot, s.buffer, s.acquireFence);
641 if (error != HWC2::Error::None) {
642 ALOGE("[%s] Failed to set buffer %p: %s (%d)", mName.string(),
643 s.buffer->handle, to_string(error).c_str(), static_cast<int32_t>(error));
644 }
645
Marissa Wall024a1912018-08-13 13:55:35 -0700646 mCurrentStateModified = false;
Marissa Wall61c58622018-07-18 10:12:20 -0700647 mFrameNumber++;
648}
649
650void BufferStateLayer::onFirstRef() {
Dan Stoza7b1b5a82018-07-31 16:00:21 -0700651 BufferLayer::onFirstRef();
652
Marissa Wall61c58622018-07-18 10:12:20 -0700653 if (const auto display = mFlinger->getDefaultDisplayDevice()) {
654 updateTransformHint(display);
655 }
656}
657
658} // namespace android