blob: 425f5c7002c97cb5b3785d0c528a4f066e244ed9 [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
24#include <private/gui/SyncFeatures.h>
Peiyong Lincbc184f2018-08-22 13:24:10 -070025#include <renderengine/Image.h>
Marissa Wall61c58622018-07-18 10:12:20 -070026
27namespace android {
28
Lloyd Pique42ab75e2018-09-12 20:46:03 -070029// clang-format off
30const std::array<float, 16> BufferStateLayer::IDENTITY_MATRIX{
31 1, 0, 0, 0,
32 0, 1, 0, 0,
33 0, 0, 1, 0,
34 0, 0, 0, 1
35};
36// clang-format on
Marissa Wall61c58622018-07-18 10:12:20 -070037
Vishnu Nair60356342018-11-13 13:00:45 -080038BufferStateLayer::BufferStateLayer(const LayerCreationArgs& args) : BufferLayer(args) {
39 mOverrideScalingMode = NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW;
40}
Lloyd Pique42ab75e2018-09-12 20:46:03 -070041BufferStateLayer::~BufferStateLayer() = default;
Marissa Wall61c58622018-07-18 10:12:20 -070042
43// -----------------------------------------------------------------------
44// Interface implementation for Layer
45// -----------------------------------------------------------------------
Marissa Wallfda30bb2018-10-12 11:34:28 -070046void BufferStateLayer::onLayerDisplayed(const sp<Fence>& releaseFence) {
47 // The transaction completed callback can only be sent if the release fence from the PREVIOUS
48 // frame has fired. In practice, we should never actually wait on the previous release fence
49 // but we should store it just in case.
50 mPreviousReleaseFence = releaseFence;
Marissa Wall61c58622018-07-18 10:12:20 -070051}
52
53void BufferStateLayer::setTransformHint(uint32_t /*orientation*/) const {
54 // TODO(marissaw): send the transform hint to buffer owner
55 return;
56}
57
58void BufferStateLayer::releasePendingBuffer(nsecs_t /*dequeueReadyTime*/) {
Marissa Wall61c58622018-07-18 10:12:20 -070059 return;
60}
61
Ana Krulec010d2192018-10-08 06:29:54 -070062bool BufferStateLayer::shouldPresentNow(nsecs_t /*expectedPresentTime*/) const {
Marissa Wall61c58622018-07-18 10:12:20 -070063 if (getSidebandStreamChanged() || getAutoRefresh()) {
64 return true;
65 }
66
Marissa Wall024a1912018-08-13 13:55:35 -070067 return hasFrameUpdate();
Marissa Wall61c58622018-07-18 10:12:20 -070068}
69
Marissa Walle2ffb422018-10-12 11:33:52 -070070bool BufferStateLayer::willPresentCurrentTransaction() const {
71 // Returns true if the most recent Transaction applied to CurrentState will be presented.
72 return getSidebandStreamChanged() || getAutoRefresh() ||
73 (mCurrentState.modified && mCurrentState.buffer != nullptr);
Marissa Wall61c58622018-07-18 10:12:20 -070074}
75
76bool BufferStateLayer::getTransformToDisplayInverse() const {
77 return mCurrentState.transformToDisplayInverse;
78}
79
80void BufferStateLayer::pushPendingState() {
81 if (!mCurrentState.modified) {
82 return;
83 }
84 mPendingStates.push_back(mCurrentState);
85 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
86}
87
88bool BufferStateLayer::applyPendingStates(Layer::State* stateToCommit) {
89 const bool stateUpdateAvailable = !mPendingStates.empty();
90 while (!mPendingStates.empty()) {
91 popPendingState(stateToCommit);
92 }
Marissa Wall024a1912018-08-13 13:55:35 -070093 mCurrentStateModified = stateUpdateAvailable && mCurrentState.modified;
Marissa Wall61c58622018-07-18 10:12:20 -070094 mCurrentState.modified = false;
95 return stateUpdateAvailable;
96}
97
98Rect BufferStateLayer::getCrop(const Layer::State& s) const {
99 return (getEffectiveScalingMode() == NATIVE_WINDOW_SCALING_MODE_SCALE_CROP)
100 ? GLConsumer::scaleDownCrop(s.crop, s.active.w, s.active.h)
101 : s.crop;
102}
103
104bool BufferStateLayer::setTransform(uint32_t transform) {
105 if (mCurrentState.transform == transform) return false;
106 mCurrentState.sequence++;
107 mCurrentState.transform = transform;
108 mCurrentState.modified = true;
109 setTransactionFlags(eTransactionNeeded);
110 return true;
111}
112
113bool BufferStateLayer::setTransformToDisplayInverse(bool transformToDisplayInverse) {
114 if (mCurrentState.transformToDisplayInverse == transformToDisplayInverse) return false;
115 mCurrentState.sequence++;
116 mCurrentState.transformToDisplayInverse = transformToDisplayInverse;
117 mCurrentState.modified = true;
118 setTransactionFlags(eTransactionNeeded);
119 return true;
120}
121
122bool BufferStateLayer::setCrop(const Rect& crop) {
123 if (mCurrentState.crop == crop) return false;
124 mCurrentState.sequence++;
125 mCurrentState.crop = crop;
126 mCurrentState.modified = true;
127 setTransactionFlags(eTransactionNeeded);
128 return true;
129}
130
Marissa Wallfda30bb2018-10-12 11:34:28 -0700131bool BufferStateLayer::setBuffer(const sp<GraphicBuffer>& buffer) {
132 if (mCurrentState.buffer) {
133 mReleasePreviousBuffer = true;
134 }
135
Marissa Wall61c58622018-07-18 10:12:20 -0700136 mCurrentState.sequence++;
137 mCurrentState.buffer = buffer;
138 mCurrentState.modified = true;
139 setTransactionFlags(eTransactionNeeded);
140 return true;
141}
142
143bool BufferStateLayer::setAcquireFence(const sp<Fence>& fence) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700144 // The acquire fences of BufferStateLayers have already signaled before they are set
145 mCallbackHandleAcquireTime = fence->getSignalTime();
146
Marissa Wall61c58622018-07-18 10:12:20 -0700147 mCurrentState.acquireFence = fence;
148 mCurrentState.modified = true;
149 setTransactionFlags(eTransactionNeeded);
150 return true;
151}
152
153bool BufferStateLayer::setDataspace(ui::Dataspace dataspace) {
154 if (mCurrentState.dataspace == dataspace) return false;
155 mCurrentState.sequence++;
156 mCurrentState.dataspace = dataspace;
157 mCurrentState.modified = true;
158 setTransactionFlags(eTransactionNeeded);
159 return true;
160}
161
162bool BufferStateLayer::setHdrMetadata(const HdrMetadata& hdrMetadata) {
163 if (mCurrentState.hdrMetadata == hdrMetadata) return false;
164 mCurrentState.sequence++;
165 mCurrentState.hdrMetadata = hdrMetadata;
166 mCurrentState.modified = true;
167 setTransactionFlags(eTransactionNeeded);
168 return true;
169}
170
171bool BufferStateLayer::setSurfaceDamageRegion(const Region& surfaceDamage) {
172 mCurrentState.sequence++;
173 mCurrentState.surfaceDamageRegion = surfaceDamage;
174 mCurrentState.modified = true;
175 setTransactionFlags(eTransactionNeeded);
176 return true;
177}
178
179bool BufferStateLayer::setApi(int32_t api) {
180 if (mCurrentState.api == api) return false;
181 mCurrentState.sequence++;
182 mCurrentState.api = api;
183 mCurrentState.modified = true;
184 setTransactionFlags(eTransactionNeeded);
185 return true;
186}
187
188bool BufferStateLayer::setSidebandStream(const sp<NativeHandle>& sidebandStream) {
189 if (mCurrentState.sidebandStream == sidebandStream) return false;
190 mCurrentState.sequence++;
191 mCurrentState.sidebandStream = sidebandStream;
192 mCurrentState.modified = true;
193 setTransactionFlags(eTransactionNeeded);
194
195 if (!mSidebandStreamChanged.exchange(true)) {
196 // mSidebandStreamChanged was false
197 mFlinger->signalLayerUpdate();
198 }
199 return true;
200}
201
Marissa Walle2ffb422018-10-12 11:33:52 -0700202bool BufferStateLayer::setTransactionCompletedListeners(
203 const std::vector<sp<CallbackHandle>>& handles) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700204 // If there is no handle, we will not send a callback so reset mReleasePreviousBuffer and return
Marissa Walle2ffb422018-10-12 11:33:52 -0700205 if (handles.empty()) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700206 mReleasePreviousBuffer = false;
Marissa Walle2ffb422018-10-12 11:33:52 -0700207 return false;
208 }
209
210 const bool willPresent = willPresentCurrentTransaction();
211
212 for (const auto& handle : handles) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700213 // If this transaction set a buffer on this layer, release its previous buffer
214 handle->releasePreviousBuffer = mReleasePreviousBuffer;
215
Marissa Walle2ffb422018-10-12 11:33:52 -0700216 // If this layer will be presented in this frame
217 if (willPresent) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700218 // If this transaction set an acquire fence on this layer, set its acquire time
219 handle->acquireTime = mCallbackHandleAcquireTime;
220
Marissa Walle2ffb422018-10-12 11:33:52 -0700221 // Notify the transaction completed thread that there is a pending latched callback
222 // handle
223 mFlinger->getTransactionCompletedThread().registerPendingLatchedCallbackHandle(handle);
224
225 // Store so latched time and release fence can be set
226 mCurrentState.callbackHandles.push_back(handle);
227
228 } else { // If this layer will NOT need to be relatched and presented this frame
229 // Notify the transaction completed thread this handle is done
230 mFlinger->getTransactionCompletedThread().addUnlatchedCallbackHandle(handle);
231 }
232 }
233
Marissa Wallfda30bb2018-10-12 11:34:28 -0700234 mReleasePreviousBuffer = false;
235 mCallbackHandleAcquireTime = -1;
236
Marissa Walle2ffb422018-10-12 11:33:52 -0700237 return willPresent;
238}
239
Marissa Wall61c58622018-07-18 10:12:20 -0700240bool BufferStateLayer::setSize(uint32_t w, uint32_t h) {
241 if (mCurrentState.active.w == w && mCurrentState.active.h == h) return false;
242 mCurrentState.active.w = w;
243 mCurrentState.active.h = h;
244 mCurrentState.modified = true;
245 setTransactionFlags(eTransactionNeeded);
246 return true;
247}
248
249bool BufferStateLayer::setPosition(float x, float y, bool /*immediate*/) {
250 if (mCurrentState.active.transform.tx() == x && mCurrentState.active.transform.ty() == y)
251 return false;
252
253 mCurrentState.active.transform.set(x, y);
254
255 mCurrentState.sequence++;
256 mCurrentState.modified = true;
257 setTransactionFlags(eTransactionNeeded);
258 return true;
259}
260
261bool BufferStateLayer::setTransparentRegionHint(const Region& transparent) {
262 mCurrentState.transparentRegionHint = transparent;
263 mCurrentState.modified = true;
264 setTransactionFlags(eTransactionNeeded);
265 return true;
266}
267
268bool BufferStateLayer::setMatrix(const layer_state_t::matrix22_t& matrix,
269 bool allowNonRectPreservingTransforms) {
Peiyong Linefefaac2018-08-17 12:27:51 -0700270 ui::Transform t;
Marissa Wall61c58622018-07-18 10:12:20 -0700271 t.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
272
273 if (!allowNonRectPreservingTransforms && !t.preserveRects()) {
274 ALOGW("Attempt to set rotation matrix without permission ACCESS_SURFACE_FLINGER ignored");
275 return false;
276 }
277
278 mCurrentState.sequence++;
279 mCurrentState.active.transform.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
280 mCurrentState.modified = true;
281 setTransactionFlags(eTransactionNeeded);
282 return true;
283}
284// -----------------------------------------------------------------------
285
286// -----------------------------------------------------------------------
287// Interface implementation for BufferLayer
288// -----------------------------------------------------------------------
289bool BufferStateLayer::fenceHasSignaled() const {
290 if (latchUnsignaledBuffers()) {
291 return true;
292 }
293
294 return getDrawingState().acquireFence->getStatus() == Fence::Status::Signaled;
295}
296
297nsecs_t BufferStateLayer::getDesiredPresentTime() {
298 // TODO(marissaw): support an equivalent to desiredPresentTime for timestats metrics
299 return 0;
300}
301
302std::shared_ptr<FenceTime> BufferStateLayer::getCurrentFenceTime() const {
303 return std::make_shared<FenceTime>(getDrawingState().acquireFence);
304}
305
306void BufferStateLayer::getDrawingTransformMatrix(float *matrix) {
307 std::copy(std::begin(mTransformMatrix), std::end(mTransformMatrix), matrix);
308}
309
310uint32_t BufferStateLayer::getDrawingTransform() const {
311 return getDrawingState().transform;
312}
313
314ui::Dataspace BufferStateLayer::getDrawingDataSpace() const {
315 return getDrawingState().dataspace;
316}
317
318Rect BufferStateLayer::getDrawingCrop() const {
319 return Rect::INVALID_RECT;
320}
321
322uint32_t BufferStateLayer::getDrawingScalingMode() const {
Marissa Wallec463ac2018-10-08 12:35:04 -0700323 return NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW;
Marissa Wall61c58622018-07-18 10:12:20 -0700324}
325
326Region BufferStateLayer::getDrawingSurfaceDamage() const {
327 return getDrawingState().surfaceDamageRegion;
328}
329
330const HdrMetadata& BufferStateLayer::getDrawingHdrMetadata() const {
331 return getDrawingState().hdrMetadata;
332}
333
334int BufferStateLayer::getDrawingApi() const {
335 return getDrawingState().api;
336}
337
338PixelFormat BufferStateLayer::getPixelFormat() const {
339 return mActiveBuffer->format;
340}
341
342uint64_t BufferStateLayer::getFrameNumber() const {
343 return mFrameNumber;
344}
345
346bool BufferStateLayer::getAutoRefresh() const {
347 // TODO(marissaw): support shared buffer mode
348 return false;
349}
350
351bool BufferStateLayer::getSidebandStreamChanged() const {
352 return mSidebandStreamChanged.load();
353}
354
355std::optional<Region> BufferStateLayer::latchSidebandStream(bool& recomputeVisibleRegions) {
356 if (mSidebandStreamChanged.exchange(false)) {
357 const State& s(getDrawingState());
358 // mSidebandStreamChanged was true
359 // replicated in LayerBE until FE/BE is ready to be synchronized
360 getBE().compositionInfo.hwc.sidebandStream = s.sidebandStream;
361 if (getBE().compositionInfo.hwc.sidebandStream != nullptr) {
362 setTransactionFlags(eTransactionNeeded);
363 mFlinger->setTransactionFlags(eTraversalNeeded);
364 }
365 recomputeVisibleRegions = true;
366
367 return getTransform().transform(Region(Rect(s.active.w, s.active.h)));
368 }
369 return {};
370}
371
Marissa Wall024a1912018-08-13 13:55:35 -0700372bool BufferStateLayer::hasFrameUpdate() const {
373 return mCurrentStateModified && getCurrentState().buffer != nullptr;
Marissa Wall61c58622018-07-18 10:12:20 -0700374}
375
376void BufferStateLayer::setFilteringEnabled(bool enabled) {
377 GLConsumer::computeTransformMatrix(mTransformMatrix.data(), mActiveBuffer, mCurrentCrop,
378 mCurrentTransform, enabled);
379}
380
Alec Mouri39801c02018-10-10 10:44:47 -0700381status_t BufferStateLayer::bindTextureImage() {
Marissa Wall61c58622018-07-18 10:12:20 -0700382 const State& s(getDrawingState());
383 auto& engine(mFlinger->getRenderEngine());
384
Marissa Wall61c58622018-07-18 10:12:20 -0700385 engine.checkErrors();
386
Alec Mouri39801c02018-10-10 10:44:47 -0700387 // TODO(marissaw): once buffers are cached, don't create a new image everytime
388 mTextureImage = engine.createImage();
Marissa Wall61c58622018-07-18 10:12:20 -0700389
390 bool created =
391 mTextureImage->setNativeWindowBuffer(s.buffer->getNativeBuffer(),
392 s.buffer->getUsage() & GRALLOC_USAGE_PROTECTED);
393 if (!created) {
394 ALOGE("Failed to create image. size=%ux%u st=%u usage=%#" PRIx64 " fmt=%d",
395 s.buffer->getWidth(), s.buffer->getHeight(), s.buffer->getStride(),
396 s.buffer->getUsage(), s.buffer->getPixelFormat());
397 engine.bindExternalTextureImage(mTextureName, *engine.createImage());
398 return NO_INIT;
399 }
400
401 engine.bindExternalTextureImage(mTextureName, *mTextureImage);
402
403 // Wait for the new buffer to be ready.
404 if (s.acquireFence->isValid()) {
405 if (SyncFeatures::getInstance().useWaitSync()) {
406 base::unique_fd fenceFd(s.acquireFence->dup());
407 if (fenceFd == -1) {
408 ALOGE("error dup'ing fence fd: %d", errno);
409 return -errno;
410 }
411 if (!engine.waitFence(std::move(fenceFd))) {
412 ALOGE("failed to wait on fence fd");
413 return UNKNOWN_ERROR;
414 }
415 } else {
416 status_t err = s.acquireFence->waitForever("BufferStateLayer::bindTextureImage");
417 if (err != NO_ERROR) {
418 ALOGE("error waiting for fence: %d", err);
419 return err;
420 }
421 }
422 }
423
424 return NO_ERROR;
425}
426
Alec Mouri86770e52018-09-24 22:40:58 +0000427status_t BufferStateLayer::updateTexImage(bool& /*recomputeVisibleRegions*/, nsecs_t latchTime,
428 const sp<Fence>& releaseFence) {
Marissa Wall61c58622018-07-18 10:12:20 -0700429 const State& s(getDrawingState());
430
431 if (!s.buffer) {
432 return NO_ERROR;
433 }
434
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700435 const int32_t layerID = getSequence();
436
Marissa Wall61c58622018-07-18 10:12:20 -0700437 // Reject if the layer is invalid
438 uint32_t bufferWidth = s.buffer->width;
439 uint32_t bufferHeight = s.buffer->height;
440
Peiyong Linefefaac2018-08-17 12:27:51 -0700441 if (s.transform & ui::Transform::ROT_90) {
Peiyong Lin3db42342018-08-16 09:15:59 -0700442 std::swap(bufferWidth, bufferHeight);
Marissa Wall61c58622018-07-18 10:12:20 -0700443 }
444
445 if (s.transformToDisplayInverse) {
446 uint32_t invTransform = DisplayDevice::getPrimaryDisplayOrientationTransform();
Peiyong Linefefaac2018-08-17 12:27:51 -0700447 if (invTransform & ui::Transform::ROT_90) {
Peiyong Lin3db42342018-08-16 09:15:59 -0700448 std::swap(bufferWidth, bufferHeight);
Marissa Wall61c58622018-07-18 10:12:20 -0700449 }
450 }
451
Vishnu Nair60356342018-11-13 13:00:45 -0800452 if (getEffectiveScalingMode() == NATIVE_WINDOW_SCALING_MODE_FREEZE &&
Marissa Wall61c58622018-07-18 10:12:20 -0700453 (s.active.w != bufferWidth || s.active.h != bufferHeight)) {
454 ALOGE("[%s] rejecting buffer: "
455 "bufferWidth=%d, bufferHeight=%d, front.active.{w=%d, h=%d}",
456 mName.string(), bufferWidth, bufferHeight, s.active.w, s.active.h);
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700457 mTimeStats.removeTimeRecord(layerID, getFrameNumber());
Marissa Wall61c58622018-07-18 10:12:20 -0700458 return BAD_VALUE;
459 }
460
Marissa Wallfda30bb2018-10-12 11:34:28 -0700461 mFlinger->getTransactionCompletedThread()
462 .addLatchedCallbackHandles(getDrawingState().callbackHandles, latchTime,
463 mPreviousReleaseFence);
Marissa Walle2ffb422018-10-12 11:33:52 -0700464
Marissa Wall61c58622018-07-18 10:12:20 -0700465 // Handle sync fences
Alec Mouri86770e52018-09-24 22:40:58 +0000466 if (SyncFeatures::getInstance().useNativeFenceSync() && releaseFence != Fence::NO_FENCE) {
467 // TODO(alecmouri): Fail somewhere upstream if the fence is invalid.
468 if (!releaseFence->isValid()) {
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700469 mTimeStats.clearLayerRecord(layerID);
Marissa Wall61c58622018-07-18 10:12:20 -0700470 return UNKNOWN_ERROR;
471 }
472
Marissa Wall61c58622018-07-18 10:12:20 -0700473 // Check status of fences first because merging is expensive.
474 // Merging an invalid fence with any other fence results in an
475 // invalid fence.
476 auto currentStatus = s.acquireFence->getStatus();
477 if (currentStatus == Fence::Status::Invalid) {
478 ALOGE("Existing fence has invalid state");
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700479 mTimeStats.clearLayerRecord(layerID);
Marissa Wall61c58622018-07-18 10:12:20 -0700480 return BAD_VALUE;
481 }
482
Alec Mouri86770e52018-09-24 22:40:58 +0000483 auto incomingStatus = releaseFence->getStatus();
Marissa Wall61c58622018-07-18 10:12:20 -0700484 if (incomingStatus == Fence::Status::Invalid) {
485 ALOGE("New fence has invalid state");
Alec Mouri86770e52018-09-24 22:40:58 +0000486 mDrawingState.acquireFence = releaseFence;
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700487 mTimeStats.clearLayerRecord(layerID);
Marissa Wall61c58622018-07-18 10:12:20 -0700488 return BAD_VALUE;
489 }
490
491 // If both fences are signaled or both are unsignaled, we need to merge
492 // them to get an accurate timestamp.
493 if (currentStatus == incomingStatus) {
494 char fenceName[32] = {};
495 snprintf(fenceName, 32, "%.28s:%d", mName.string(), mFrameNumber);
Alec Mouri86770e52018-09-24 22:40:58 +0000496 sp<Fence> mergedFence =
497 Fence::merge(fenceName, mDrawingState.acquireFence, releaseFence);
Marissa Wall61c58622018-07-18 10:12:20 -0700498 if (!mergedFence.get()) {
499 ALOGE("failed to merge release fences");
500 // synchronization is broken, the best we can do is hope fences
501 // signal in order so the new fence will act like a union
Alec Mouri86770e52018-09-24 22:40:58 +0000502 mDrawingState.acquireFence = releaseFence;
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700503 mTimeStats.clearLayerRecord(layerID);
Marissa Wall61c58622018-07-18 10:12:20 -0700504 return BAD_VALUE;
505 }
506 mDrawingState.acquireFence = mergedFence;
507 } else if (incomingStatus == Fence::Status::Unsignaled) {
508 // If one fence has signaled and the other hasn't, the unsignaled
509 // fence will approximately correspond with the correct timestamp.
510 // There's a small race if both fences signal at about the same time
511 // and their statuses are retrieved with unfortunate timing. However,
512 // by this point, they will have both signaled and only the timestamp
513 // will be slightly off; any dependencies after this point will
514 // already have been met.
Alec Mouri86770e52018-09-24 22:40:58 +0000515 mDrawingState.acquireFence = releaseFence;
Marissa Wall61c58622018-07-18 10:12:20 -0700516 }
517 } else {
518 // Bind the new buffer to the GL texture.
519 //
520 // Older devices require the "implicit" synchronization provided
521 // by glEGLImageTargetTexture2DOES, which this method calls. Newer
522 // devices will either call this in Layer::onDraw, or (if it's not
523 // a GL-composited layer) not at all.
524 status_t err = bindTextureImage();
525 if (err != NO_ERROR) {
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700526 mTimeStats.clearLayerRecord(layerID);
Marissa Wall61c58622018-07-18 10:12:20 -0700527 return BAD_VALUE;
528 }
529 }
530
531 // TODO(marissaw): properly support mTimeStats
Yiwei Zhang8e8fe522018-11-02 18:34:07 -0700532 mTimeStats.setPostTime(layerID, getFrameNumber(), getName().c_str(), latchTime);
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700533 mTimeStats.setAcquireFence(layerID, getFrameNumber(), getCurrentFenceTime());
534 mTimeStats.setLatchTime(layerID, getFrameNumber(), latchTime);
Marissa Wall61c58622018-07-18 10:12:20 -0700535
536 return NO_ERROR;
537}
538
539status_t BufferStateLayer::updateActiveBuffer() {
540 const State& s(getDrawingState());
541
542 if (s.buffer == nullptr) {
543 return BAD_VALUE;
544 }
545
546 mActiveBuffer = s.buffer;
547 getBE().compositionInfo.mBuffer = mActiveBuffer;
548 getBE().compositionInfo.mBufferSlot = 0;
549
550 return NO_ERROR;
551}
552
553status_t BufferStateLayer::updateFrameNumber(nsecs_t /*latchTime*/) {
554 // TODO(marissaw): support frame history events
555 mCurrentFrameNumber = mFrameNumber;
556 return NO_ERROR;
557}
558
Dominik Laskowski075d3172018-05-24 15:50:06 -0700559void BufferStateLayer::setHwcLayerBuffer(DisplayId displayId) {
Marissa Wall61c58622018-07-18 10:12:20 -0700560 auto& hwcInfo = getBE().mHwcLayers[displayId];
561 auto& hwcLayer = hwcInfo.layer;
562
563 const State& s(getDrawingState());
564
565 // TODO(marissaw): support more than one slot
566 uint32_t hwcSlot = 0;
567
568 auto error = hwcLayer->setBuffer(hwcSlot, s.buffer, s.acquireFence);
569 if (error != HWC2::Error::None) {
570 ALOGE("[%s] Failed to set buffer %p: %s (%d)", mName.string(),
571 s.buffer->handle, to_string(error).c_str(), static_cast<int32_t>(error));
572 }
573
Marissa Wall024a1912018-08-13 13:55:35 -0700574 mCurrentStateModified = false;
Marissa Wall61c58622018-07-18 10:12:20 -0700575 mFrameNumber++;
576}
577
578void BufferStateLayer::onFirstRef() {
Dan Stoza7b1b5a82018-07-31 16:00:21 -0700579 BufferLayer::onFirstRef();
580
Marissa Wall61c58622018-07-18 10:12:20 -0700581 if (const auto display = mFlinger->getDefaultDisplayDevice()) {
582 updateTransformHint(display);
583 }
584}
585
586} // namespace android