blob: 8091b941f83057b5925fa9a80f30148e683ae00b [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
29namespace android {
30
Lloyd Pique42ab75e2018-09-12 20:46:03 -070031// clang-format off
32const std::array<float, 16> BufferStateLayer::IDENTITY_MATRIX{
33 1, 0, 0, 0,
34 0, 1, 0, 0,
35 0, 0, 1, 0,
36 0, 0, 0, 1
37};
38// clang-format on
Marissa Wall61c58622018-07-18 10:12:20 -070039
Vishnu Nair60356342018-11-13 13:00:45 -080040BufferStateLayer::BufferStateLayer(const LayerCreationArgs& args) : BufferLayer(args) {
41 mOverrideScalingMode = NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW;
42}
Lloyd Pique42ab75e2018-09-12 20:46:03 -070043BufferStateLayer::~BufferStateLayer() = default;
Marissa Wall61c58622018-07-18 10:12:20 -070044
45// -----------------------------------------------------------------------
46// Interface implementation for Layer
47// -----------------------------------------------------------------------
Marissa Wallfda30bb2018-10-12 11:34:28 -070048void BufferStateLayer::onLayerDisplayed(const sp<Fence>& releaseFence) {
49 // The transaction completed callback can only be sent if the release fence from the PREVIOUS
50 // frame has fired. In practice, we should never actually wait on the previous release fence
51 // but we should store it just in case.
52 mPreviousReleaseFence = releaseFence;
Marissa Wall61c58622018-07-18 10:12:20 -070053}
54
55void BufferStateLayer::setTransformHint(uint32_t /*orientation*/) const {
56 // TODO(marissaw): send the transform hint to buffer owner
57 return;
58}
59
60void BufferStateLayer::releasePendingBuffer(nsecs_t /*dequeueReadyTime*/) {
Marissa Wall61c58622018-07-18 10:12:20 -070061 return;
62}
63
Ana Krulec010d2192018-10-08 06:29:54 -070064bool BufferStateLayer::shouldPresentNow(nsecs_t /*expectedPresentTime*/) const {
Marissa Wall61c58622018-07-18 10:12:20 -070065 if (getSidebandStreamChanged() || getAutoRefresh()) {
66 return true;
67 }
68
Marissa Wall024a1912018-08-13 13:55:35 -070069 return hasFrameUpdate();
Marissa Wall61c58622018-07-18 10:12:20 -070070}
71
Marissa Walle2ffb422018-10-12 11:33:52 -070072bool BufferStateLayer::willPresentCurrentTransaction() const {
Ady Abraham83729882018-12-07 12:26:48 -080073 Mutex::Autolock lock(mStateMutex);
Marissa Walle2ffb422018-10-12 11:33:52 -070074 // Returns true if the most recent Transaction applied to CurrentState will be presented.
75 return getSidebandStreamChanged() || getAutoRefresh() ||
Ady Abraham83729882018-12-07 12:26:48 -080076 (mState.current.modified && mState.current.buffer != nullptr);
Marissa Wall61c58622018-07-18 10:12:20 -070077}
78
Ady Abraham83729882018-12-07 12:26:48 -080079bool BufferStateLayer::getTransformToDisplayInverseLocked() const {
80 return mState.current.transformToDisplayInverse;
Marissa Wall61c58622018-07-18 10:12:20 -070081}
82
Ady Abraham83729882018-12-07 12:26:48 -080083void BufferStateLayer::pushPendingStateLocked() {
84 if (!mState.current.modified) {
Marissa Wall61c58622018-07-18 10:12:20 -070085 return;
86 }
Ady Abraham83729882018-12-07 12:26:48 -080087 mState.pending.push_back(mState.current);
88 ATRACE_INT(mTransactionName.string(), mState.pending.size());
Marissa Wall61c58622018-07-18 10:12:20 -070089}
90
91bool BufferStateLayer::applyPendingStates(Layer::State* stateToCommit) {
Ady Abraham83729882018-12-07 12:26:48 -080092 const bool stateUpdateAvailable = !mState.pending.empty();
93 while (!mState.pending.empty()) {
Marissa Wall61c58622018-07-18 10:12:20 -070094 popPendingState(stateToCommit);
95 }
Ady Abraham83729882018-12-07 12:26:48 -080096 mCurrentStateModified = stateUpdateAvailable && mState.current.modified;
97 mState.current.modified = false;
Marissa Wall61c58622018-07-18 10:12:20 -070098 return stateUpdateAvailable;
99}
100
Marissa Wall861616d2018-10-22 12:52:23 -0700101// Crop that applies to the window
102Rect BufferStateLayer::getCrop(const Layer::State& /*s*/) const {
103 return Rect::INVALID_RECT;
Marissa Wall61c58622018-07-18 10:12:20 -0700104}
105
106bool BufferStateLayer::setTransform(uint32_t transform) {
Ady Abraham83729882018-12-07 12:26:48 -0800107 Mutex::Autolock lock(mStateMutex);
108 if (mState.current.transform == transform) return false;
109 mState.current.sequence++;
110 mState.current.transform = transform;
111 mState.current.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700112 setTransactionFlags(eTransactionNeeded);
113 return true;
114}
115
116bool BufferStateLayer::setTransformToDisplayInverse(bool transformToDisplayInverse) {
Ady Abraham83729882018-12-07 12:26:48 -0800117 Mutex::Autolock lock(mStateMutex);
118 if (mState.current.transformToDisplayInverse == transformToDisplayInverse) return false;
119 mState.current.sequence++;
120 mState.current.transformToDisplayInverse = transformToDisplayInverse;
121 mState.current.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700122 setTransactionFlags(eTransactionNeeded);
123 return true;
124}
125
126bool BufferStateLayer::setCrop(const Rect& crop) {
Ady Abraham83729882018-12-07 12:26:48 -0800127 Mutex::Autolock lock(mStateMutex);
128 if (mState.current.crop == crop) return false;
129 mState.current.sequence++;
130 mState.current.crop = crop;
131 mState.current.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700132 setTransactionFlags(eTransactionNeeded);
133 return true;
134}
135
Marissa Wall861616d2018-10-22 12:52:23 -0700136bool BufferStateLayer::setFrame(const Rect& frame) {
137 int x = frame.left;
138 int y = frame.top;
139 int w = frame.getWidth();
140 int h = frame.getHeight();
141
Ady Abraham83729882018-12-07 12:26:48 -0800142 Mutex::Autolock lock(mStateMutex);
143 if (mState.current.active.transform.tx() == x && mState.current.active.transform.ty() == y &&
144 mState.current.active.w == w && mState.current.active.h == h) {
Marissa Wall861616d2018-10-22 12:52:23 -0700145 return false;
146 }
147
148 if (!frame.isValid()) {
149 x = y = w = h = 0;
150 }
Ady Abraham83729882018-12-07 12:26:48 -0800151 mState.current.active.transform.set(x, y);
152 mState.current.active.w = w;
153 mState.current.active.h = h;
Marissa Wall861616d2018-10-22 12:52:23 -0700154
Ady Abraham83729882018-12-07 12:26:48 -0800155 mState.current.sequence++;
156 mState.current.modified = true;
Marissa Wall861616d2018-10-22 12:52:23 -0700157 setTransactionFlags(eTransactionNeeded);
158 return true;
159}
160
Marissa Wallfda30bb2018-10-12 11:34:28 -0700161bool BufferStateLayer::setBuffer(const sp<GraphicBuffer>& buffer) {
Ady Abraham83729882018-12-07 12:26:48 -0800162 Mutex::Autolock lock(mStateMutex);
163 if (mState.current.buffer) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700164 mReleasePreviousBuffer = true;
165 }
166
Ady Abraham83729882018-12-07 12:26:48 -0800167 mState.current.sequence++;
168 mState.current.buffer = buffer;
169 mState.current.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700170 setTransactionFlags(eTransactionNeeded);
171 return true;
172}
173
174bool BufferStateLayer::setAcquireFence(const sp<Fence>& fence) {
Ady Abraham83729882018-12-07 12:26:48 -0800175 Mutex::Autolock lock(mStateMutex);
Marissa Wallfda30bb2018-10-12 11:34:28 -0700176 // The acquire fences of BufferStateLayers have already signaled before they are set
177 mCallbackHandleAcquireTime = fence->getSignalTime();
178
Ady Abraham83729882018-12-07 12:26:48 -0800179 mState.current.acquireFence = fence;
180 mState.current.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700181 setTransactionFlags(eTransactionNeeded);
182 return true;
183}
184
185bool BufferStateLayer::setDataspace(ui::Dataspace dataspace) {
Ady Abraham83729882018-12-07 12:26:48 -0800186 Mutex::Autolock lock(mStateMutex);
187 if (mState.current.dataspace == dataspace) return false;
188 mState.current.sequence++;
189 mState.current.dataspace = dataspace;
190 mState.current.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700191 setTransactionFlags(eTransactionNeeded);
192 return true;
193}
194
195bool BufferStateLayer::setHdrMetadata(const HdrMetadata& hdrMetadata) {
Ady Abraham83729882018-12-07 12:26:48 -0800196 Mutex::Autolock lock(mStateMutex);
197 if (mState.current.hdrMetadata == hdrMetadata) return false;
198 mState.current.sequence++;
199 mState.current.hdrMetadata = hdrMetadata;
200 mState.current.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700201 setTransactionFlags(eTransactionNeeded);
202 return true;
203}
204
205bool BufferStateLayer::setSurfaceDamageRegion(const Region& surfaceDamage) {
Ady Abraham83729882018-12-07 12:26:48 -0800206 Mutex::Autolock lock(mStateMutex);
207 mState.current.sequence++;
208 mState.current.surfaceDamageRegion = surfaceDamage;
209 mState.current.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700210 setTransactionFlags(eTransactionNeeded);
211 return true;
212}
213
214bool BufferStateLayer::setApi(int32_t api) {
Ady Abraham83729882018-12-07 12:26:48 -0800215 Mutex::Autolock lock(mStateMutex);
216 if (mState.current.api == api) return false;
217 mState.current.sequence++;
218 mState.current.api = api;
219 mState.current.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700220 setTransactionFlags(eTransactionNeeded);
221 return true;
222}
223
224bool BufferStateLayer::setSidebandStream(const sp<NativeHandle>& sidebandStream) {
Ady Abraham83729882018-12-07 12:26:48 -0800225 Mutex::Autolock lock(mStateMutex);
226 if (mState.current.sidebandStream == sidebandStream) return false;
227 mState.current.sequence++;
228 mState.current.sidebandStream = sidebandStream;
229 mState.current.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700230 setTransactionFlags(eTransactionNeeded);
231
232 if (!mSidebandStreamChanged.exchange(true)) {
233 // mSidebandStreamChanged was false
234 mFlinger->signalLayerUpdate();
235 }
236 return true;
237}
238
Marissa Walle2ffb422018-10-12 11:33:52 -0700239bool BufferStateLayer::setTransactionCompletedListeners(
240 const std::vector<sp<CallbackHandle>>& handles) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700241 // If there is no handle, we will not send a callback so reset mReleasePreviousBuffer and return
Marissa Walle2ffb422018-10-12 11:33:52 -0700242 if (handles.empty()) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700243 mReleasePreviousBuffer = false;
Marissa Walle2ffb422018-10-12 11:33:52 -0700244 return false;
245 }
246
247 const bool willPresent = willPresentCurrentTransaction();
248
249 for (const auto& handle : handles) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700250 // If this transaction set a buffer on this layer, release its previous buffer
251 handle->releasePreviousBuffer = mReleasePreviousBuffer;
252
Marissa Walle2ffb422018-10-12 11:33:52 -0700253 // If this layer will be presented in this frame
254 if (willPresent) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700255 // If this transaction set an acquire fence on this layer, set its acquire time
256 handle->acquireTime = mCallbackHandleAcquireTime;
257
Marissa Walle2ffb422018-10-12 11:33:52 -0700258 // Notify the transaction completed thread that there is a pending latched callback
259 // handle
260 mFlinger->getTransactionCompletedThread().registerPendingLatchedCallbackHandle(handle);
261
262 // Store so latched time and release fence can be set
Ady Abraham83729882018-12-07 12:26:48 -0800263 {
264 Mutex::Autolock lock(mStateMutex);
265 mState.current.callbackHandles.push_back(handle);
266 }
Marissa Walle2ffb422018-10-12 11:33:52 -0700267
268 } else { // If this layer will NOT need to be relatched and presented this frame
269 // Notify the transaction completed thread this handle is done
270 mFlinger->getTransactionCompletedThread().addUnlatchedCallbackHandle(handle);
271 }
272 }
273
Marissa Wallfda30bb2018-10-12 11:34:28 -0700274 mReleasePreviousBuffer = false;
275 mCallbackHandleAcquireTime = -1;
276
Marissa Walle2ffb422018-10-12 11:33:52 -0700277 return willPresent;
278}
279
Marissa Wall61c58622018-07-18 10:12:20 -0700280bool BufferStateLayer::setTransparentRegionHint(const Region& transparent) {
Ady Abraham83729882018-12-07 12:26:48 -0800281 Mutex::Autolock lock(mStateMutex);
282 mState.current.transparentRegionHint = transparent;
283 mState.current.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700284 setTransactionFlags(eTransactionNeeded);
285 return true;
286}
287
Marissa Wall861616d2018-10-22 12:52:23 -0700288Rect BufferStateLayer::getBufferSize(const State& s) const {
289 // for buffer state layers we use the display frame size as the buffer size.
290 if (getActiveWidth(s) < UINT32_MAX && getActiveHeight(s) < UINT32_MAX) {
291 return Rect(getActiveWidth(s), getActiveHeight(s));
Marissa Wall61c58622018-07-18 10:12:20 -0700292 }
293
Marissa Wall861616d2018-10-22 12:52:23 -0700294 // if the display frame is not defined, use the parent bounds as the buffer size.
295 const auto& p = mDrawingParent.promote();
296 if (p != nullptr) {
297 Rect parentBounds = Rect(p->computeBounds(Region()));
298 if (!parentBounds.isEmpty()) {
299 return parentBounds;
300 }
301 }
302
303 // if there is no parent layer, use the buffer's bounds as the buffer size
304 if (s.buffer) {
305 return s.buffer->getBounds();
306 }
307 return Rect::INVALID_RECT;
Marissa Wall61c58622018-07-18 10:12:20 -0700308}
309// -----------------------------------------------------------------------
310
311// -----------------------------------------------------------------------
312// Interface implementation for BufferLayer
313// -----------------------------------------------------------------------
314bool BufferStateLayer::fenceHasSignaled() const {
315 if (latchUnsignaledBuffers()) {
316 return true;
317 }
318
Ady Abraham83729882018-12-07 12:26:48 -0800319 Mutex::Autolock lock(mStateMutex);
Marissa Wall61c58622018-07-18 10:12:20 -0700320 return getDrawingState().acquireFence->getStatus() == Fence::Status::Signaled;
321}
322
323nsecs_t BufferStateLayer::getDesiredPresentTime() {
324 // TODO(marissaw): support an equivalent to desiredPresentTime for timestats metrics
325 return 0;
326}
327
Ady Abraham83729882018-12-07 12:26:48 -0800328std::shared_ptr<FenceTime> BufferStateLayer::getCurrentFenceTimeLocked() const {
Marissa Wall61c58622018-07-18 10:12:20 -0700329 return std::make_shared<FenceTime>(getDrawingState().acquireFence);
330}
331
332void BufferStateLayer::getDrawingTransformMatrix(float *matrix) {
333 std::copy(std::begin(mTransformMatrix), std::end(mTransformMatrix), matrix);
334}
335
336uint32_t BufferStateLayer::getDrawingTransform() const {
337 return getDrawingState().transform;
338}
339
340ui::Dataspace BufferStateLayer::getDrawingDataSpace() const {
341 return getDrawingState().dataspace;
342}
343
Marissa Wall861616d2018-10-22 12:52:23 -0700344// Crop that applies to the buffer
Marissa Wall61c58622018-07-18 10:12:20 -0700345Rect BufferStateLayer::getDrawingCrop() const {
Marissa Wall861616d2018-10-22 12:52:23 -0700346 const State& s(getDrawingState());
347
348 if (s.crop.isEmpty() && s.buffer) {
349 return s.buffer->getBounds();
350 }
351 return s.crop;
Marissa Wall61c58622018-07-18 10:12:20 -0700352}
353
354uint32_t BufferStateLayer::getDrawingScalingMode() const {
Marissa Wallec463ac2018-10-08 12:35:04 -0700355 return NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW;
Marissa Wall61c58622018-07-18 10:12:20 -0700356}
357
358Region BufferStateLayer::getDrawingSurfaceDamage() const {
Ady Abraham83729882018-12-07 12:26:48 -0800359 Mutex::Autolock lock(mStateMutex);
Marissa Wall61c58622018-07-18 10:12:20 -0700360 return getDrawingState().surfaceDamageRegion;
361}
362
363const HdrMetadata& BufferStateLayer::getDrawingHdrMetadata() const {
Ady Abraham83729882018-12-07 12:26:48 -0800364 Mutex::Autolock lock(mStateMutex);
Marissa Wall61c58622018-07-18 10:12:20 -0700365 return getDrawingState().hdrMetadata;
366}
367
368int BufferStateLayer::getDrawingApi() const {
Ady Abraham83729882018-12-07 12:26:48 -0800369 Mutex::Autolock lock(mStateMutex);
Marissa Wall61c58622018-07-18 10:12:20 -0700370 return getDrawingState().api;
371}
372
373PixelFormat BufferStateLayer::getPixelFormat() const {
Marissa Wall5aec6412018-11-14 11:49:18 -0800374 if (!mActiveBuffer) {
375 return PIXEL_FORMAT_NONE;
376 }
Marissa Wall61c58622018-07-18 10:12:20 -0700377 return mActiveBuffer->format;
378}
379
380uint64_t BufferStateLayer::getFrameNumber() const {
381 return mFrameNumber;
382}
383
384bool BufferStateLayer::getAutoRefresh() const {
385 // TODO(marissaw): support shared buffer mode
386 return false;
387}
388
389bool BufferStateLayer::getSidebandStreamChanged() const {
390 return mSidebandStreamChanged.load();
391}
392
393std::optional<Region> BufferStateLayer::latchSidebandStream(bool& recomputeVisibleRegions) {
Ady Abraham83729882018-12-07 12:26:48 -0800394 Mutex::Autolock lock(mStateMutex);
Marissa Wall61c58622018-07-18 10:12:20 -0700395 if (mSidebandStreamChanged.exchange(false)) {
396 const State& s(getDrawingState());
397 // mSidebandStreamChanged was true
398 // replicated in LayerBE until FE/BE is ready to be synchronized
399 getBE().compositionInfo.hwc.sidebandStream = s.sidebandStream;
400 if (getBE().compositionInfo.hwc.sidebandStream != nullptr) {
401 setTransactionFlags(eTransactionNeeded);
402 mFlinger->setTransactionFlags(eTraversalNeeded);
403 }
404 recomputeVisibleRegions = true;
405
Ady Abraham83729882018-12-07 12:26:48 -0800406 return getTransformLocked().transform(Region(Rect(s.active.w, s.active.h)));
Marissa Wall61c58622018-07-18 10:12:20 -0700407 }
408 return {};
409}
410
Ady Abraham83729882018-12-07 12:26:48 -0800411bool BufferStateLayer::hasFrameUpdateLocked() const {
Marissa Wall024a1912018-08-13 13:55:35 -0700412 return mCurrentStateModified && getCurrentState().buffer != nullptr;
Marissa Wall61c58622018-07-18 10:12:20 -0700413}
414
415void BufferStateLayer::setFilteringEnabled(bool enabled) {
416 GLConsumer::computeTransformMatrix(mTransformMatrix.data(), mActiveBuffer, mCurrentCrop,
417 mCurrentTransform, enabled);
418}
419
Alec Mouri39801c02018-10-10 10:44:47 -0700420status_t BufferStateLayer::bindTextureImage() {
Ady Abraham83729882018-12-07 12:26:48 -0800421 Mutex::Autolock lock(mStateMutex);
422 return bindTextureImageLocked();
423}
424status_t BufferStateLayer::bindTextureImageLocked() {
Marissa Wall61c58622018-07-18 10:12:20 -0700425 const State& s(getDrawingState());
426 auto& engine(mFlinger->getRenderEngine());
427
Marissa Wall61c58622018-07-18 10:12:20 -0700428 engine.checkErrors();
429
Alec Mouri39801c02018-10-10 10:44:47 -0700430 // TODO(marissaw): once buffers are cached, don't create a new image everytime
431 mTextureImage = engine.createImage();
Marissa Wall61c58622018-07-18 10:12:20 -0700432
433 bool created =
434 mTextureImage->setNativeWindowBuffer(s.buffer->getNativeBuffer(),
435 s.buffer->getUsage() & GRALLOC_USAGE_PROTECTED);
436 if (!created) {
437 ALOGE("Failed to create image. size=%ux%u st=%u usage=%#" PRIx64 " fmt=%d",
438 s.buffer->getWidth(), s.buffer->getHeight(), s.buffer->getStride(),
439 s.buffer->getUsage(), s.buffer->getPixelFormat());
440 engine.bindExternalTextureImage(mTextureName, *engine.createImage());
441 return NO_INIT;
442 }
443
444 engine.bindExternalTextureImage(mTextureName, *mTextureImage);
445
446 // Wait for the new buffer to be ready.
447 if (s.acquireFence->isValid()) {
448 if (SyncFeatures::getInstance().useWaitSync()) {
449 base::unique_fd fenceFd(s.acquireFence->dup());
450 if (fenceFd == -1) {
451 ALOGE("error dup'ing fence fd: %d", errno);
452 return -errno;
453 }
454 if (!engine.waitFence(std::move(fenceFd))) {
455 ALOGE("failed to wait on fence fd");
456 return UNKNOWN_ERROR;
457 }
458 } else {
459 status_t err = s.acquireFence->waitForever("BufferStateLayer::bindTextureImage");
460 if (err != NO_ERROR) {
461 ALOGE("error waiting for fence: %d", err);
462 return err;
463 }
464 }
465 }
466
467 return NO_ERROR;
468}
469
Alec Mouri86770e52018-09-24 22:40:58 +0000470status_t BufferStateLayer::updateTexImage(bool& /*recomputeVisibleRegions*/, nsecs_t latchTime,
471 const sp<Fence>& releaseFence) {
Marissa Wall61c58622018-07-18 10:12:20 -0700472 const State& s(getDrawingState());
473
474 if (!s.buffer) {
475 return NO_ERROR;
476 }
477
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700478 const int32_t layerID = getSequence();
479
Marissa Wall61c58622018-07-18 10:12:20 -0700480 // Reject if the layer is invalid
481 uint32_t bufferWidth = s.buffer->width;
482 uint32_t bufferHeight = s.buffer->height;
483
Peiyong Linefefaac2018-08-17 12:27:51 -0700484 if (s.transform & ui::Transform::ROT_90) {
Peiyong Lin3db42342018-08-16 09:15:59 -0700485 std::swap(bufferWidth, bufferHeight);
Marissa Wall61c58622018-07-18 10:12:20 -0700486 }
487
488 if (s.transformToDisplayInverse) {
489 uint32_t invTransform = DisplayDevice::getPrimaryDisplayOrientationTransform();
Peiyong Linefefaac2018-08-17 12:27:51 -0700490 if (invTransform & ui::Transform::ROT_90) {
Peiyong Lin3db42342018-08-16 09:15:59 -0700491 std::swap(bufferWidth, bufferHeight);
Marissa Wall61c58622018-07-18 10:12:20 -0700492 }
493 }
494
Vishnu Nair60356342018-11-13 13:00:45 -0800495 if (getEffectiveScalingMode() == NATIVE_WINDOW_SCALING_MODE_FREEZE &&
Marissa Wall61c58622018-07-18 10:12:20 -0700496 (s.active.w != bufferWidth || s.active.h != bufferHeight)) {
497 ALOGE("[%s] rejecting buffer: "
498 "bufferWidth=%d, bufferHeight=%d, front.active.{w=%d, h=%d}",
499 mName.string(), bufferWidth, bufferHeight, s.active.w, s.active.h);
Yiwei Zhang7e666a52018-11-15 13:33:42 -0800500 mFlinger->mTimeStats->removeTimeRecord(layerID, getFrameNumber());
Marissa Wall61c58622018-07-18 10:12:20 -0700501 return BAD_VALUE;
502 }
503
Marissa Wallfda30bb2018-10-12 11:34:28 -0700504 mFlinger->getTransactionCompletedThread()
505 .addLatchedCallbackHandles(getDrawingState().callbackHandles, latchTime,
506 mPreviousReleaseFence);
Marissa Walle2ffb422018-10-12 11:33:52 -0700507
Marissa Wall61c58622018-07-18 10:12:20 -0700508 // Handle sync fences
Alec Mouri86770e52018-09-24 22:40:58 +0000509 if (SyncFeatures::getInstance().useNativeFenceSync() && releaseFence != Fence::NO_FENCE) {
510 // TODO(alecmouri): Fail somewhere upstream if the fence is invalid.
511 if (!releaseFence->isValid()) {
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800512 mFlinger->mTimeStats->onDestroy(layerID);
Marissa Wall61c58622018-07-18 10:12:20 -0700513 return UNKNOWN_ERROR;
514 }
515
Marissa Wall61c58622018-07-18 10:12:20 -0700516 // Check status of fences first because merging is expensive.
517 // Merging an invalid fence with any other fence results in an
518 // invalid fence.
519 auto currentStatus = s.acquireFence->getStatus();
520 if (currentStatus == Fence::Status::Invalid) {
521 ALOGE("Existing fence has invalid state");
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800522 mFlinger->mTimeStats->onDestroy(layerID);
Marissa Wall61c58622018-07-18 10:12:20 -0700523 return BAD_VALUE;
524 }
525
Alec Mouri86770e52018-09-24 22:40:58 +0000526 auto incomingStatus = releaseFence->getStatus();
Marissa Wall61c58622018-07-18 10:12:20 -0700527 if (incomingStatus == Fence::Status::Invalid) {
528 ALOGE("New fence has invalid state");
Ady Abraham83729882018-12-07 12:26:48 -0800529 mState.drawing.acquireFence = releaseFence;
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
534 // If both fences are signaled or both are unsignaled, we need to merge
535 // them to get an accurate timestamp.
536 if (currentStatus == incomingStatus) {
537 char fenceName[32] = {};
538 snprintf(fenceName, 32, "%.28s:%d", mName.string(), mFrameNumber);
Alec Mouri86770e52018-09-24 22:40:58 +0000539 sp<Fence> mergedFence =
Ady Abraham83729882018-12-07 12:26:48 -0800540 Fence::merge(fenceName, mState.drawing.acquireFence, releaseFence);
Marissa Wall61c58622018-07-18 10:12:20 -0700541 if (!mergedFence.get()) {
542 ALOGE("failed to merge release fences");
543 // synchronization is broken, the best we can do is hope fences
544 // signal in order so the new fence will act like a union
Ady Abraham83729882018-12-07 12:26:48 -0800545 mState.drawing.acquireFence = releaseFence;
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800546 mFlinger->mTimeStats->onDestroy(layerID);
Marissa Wall61c58622018-07-18 10:12:20 -0700547 return BAD_VALUE;
548 }
Ady Abraham83729882018-12-07 12:26:48 -0800549 mState.drawing.acquireFence = mergedFence;
Marissa Wall61c58622018-07-18 10:12:20 -0700550 } else if (incomingStatus == Fence::Status::Unsignaled) {
551 // If one fence has signaled and the other hasn't, the unsignaled
552 // fence will approximately correspond with the correct timestamp.
553 // There's a small race if both fences signal at about the same time
554 // and their statuses are retrieved with unfortunate timing. However,
555 // by this point, they will have both signaled and only the timestamp
556 // will be slightly off; any dependencies after this point will
557 // already have been met.
Ady Abraham83729882018-12-07 12:26:48 -0800558 mState.drawing.acquireFence = releaseFence;
Marissa Wall61c58622018-07-18 10:12:20 -0700559 }
560 } else {
561 // Bind the new buffer to the GL texture.
562 //
563 // Older devices require the "implicit" synchronization provided
564 // by glEGLImageTargetTexture2DOES, which this method calls. Newer
565 // devices will either call this in Layer::onDraw, or (if it's not
566 // a GL-composited layer) not at all.
Ady Abraham83729882018-12-07 12:26:48 -0800567 status_t err = bindTextureImageLocked();
Marissa Wall61c58622018-07-18 10:12:20 -0700568 if (err != NO_ERROR) {
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800569 mFlinger->mTimeStats->onDestroy(layerID);
Marissa Wall61c58622018-07-18 10:12:20 -0700570 return BAD_VALUE;
571 }
572 }
573
574 // TODO(marissaw): properly support mTimeStats
Yiwei Zhang7e666a52018-11-15 13:33:42 -0800575 mFlinger->mTimeStats->setPostTime(layerID, getFrameNumber(), getName().c_str(), latchTime);
Ady Abraham83729882018-12-07 12:26:48 -0800576 mFlinger->mTimeStats->setAcquireFence(layerID, getFrameNumber(), getCurrentFenceTimeLocked());
Yiwei Zhang7e666a52018-11-15 13:33:42 -0800577 mFlinger->mTimeStats->setLatchTime(layerID, getFrameNumber(), latchTime);
Marissa Wall61c58622018-07-18 10:12:20 -0700578
579 return NO_ERROR;
580}
581
582status_t BufferStateLayer::updateActiveBuffer() {
583 const State& s(getDrawingState());
584
585 if (s.buffer == nullptr) {
586 return BAD_VALUE;
587 }
588
589 mActiveBuffer = s.buffer;
590 getBE().compositionInfo.mBuffer = mActiveBuffer;
591 getBE().compositionInfo.mBufferSlot = 0;
592
593 return NO_ERROR;
594}
595
596status_t BufferStateLayer::updateFrameNumber(nsecs_t /*latchTime*/) {
597 // TODO(marissaw): support frame history events
598 mCurrentFrameNumber = mFrameNumber;
599 return NO_ERROR;
600}
601
Dominik Laskowski075d3172018-05-24 15:50:06 -0700602void BufferStateLayer::setHwcLayerBuffer(DisplayId displayId) {
Ady Abraham83729882018-12-07 12:26:48 -0800603 Mutex::Autolock lock(mStateMutex);
Marissa Wall61c58622018-07-18 10:12:20 -0700604 auto& hwcInfo = getBE().mHwcLayers[displayId];
605 auto& hwcLayer = hwcInfo.layer;
606
607 const State& s(getDrawingState());
608
609 // TODO(marissaw): support more than one slot
610 uint32_t hwcSlot = 0;
611
612 auto error = hwcLayer->setBuffer(hwcSlot, s.buffer, s.acquireFence);
613 if (error != HWC2::Error::None) {
614 ALOGE("[%s] Failed to set buffer %p: %s (%d)", mName.string(),
615 s.buffer->handle, to_string(error).c_str(), static_cast<int32_t>(error));
616 }
617
Marissa Wall024a1912018-08-13 13:55:35 -0700618 mCurrentStateModified = false;
Marissa Wall61c58622018-07-18 10:12:20 -0700619 mFrameNumber++;
620}
621
622void BufferStateLayer::onFirstRef() {
Dan Stoza7b1b5a82018-07-31 16:00:21 -0700623 BufferLayer::onFirstRef();
624
Marissa Wall61c58622018-07-18 10:12:20 -0700625 if (const auto display = mFlinger->getDefaultDisplayDevice()) {
626 updateTransformHint(display);
627 }
628}
629
630} // namespace android