blob: 96a66cbb8e33022880d998cd7fde3a504cf131a4 [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() ||
Lloyd Pique0449b0f2018-12-20 16:23:45 -080099 (mCurrentState.modified && mCurrentState.buffer != nullptr);
Marissa Wall61c58622018-07-18 10:12:20 -0700100}
101
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800102bool BufferStateLayer::getTransformToDisplayInverse() const {
103 return mCurrentState.transformToDisplayInverse;
Marissa Wall61c58622018-07-18 10:12:20 -0700104}
105
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800106void BufferStateLayer::pushPendingState() {
107 if (!mCurrentState.modified) {
Marissa Wall61c58622018-07-18 10:12:20 -0700108 return;
109 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800110 mPendingStates.push_back(mCurrentState);
111 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Marissa Wall61c58622018-07-18 10:12:20 -0700112}
113
114bool BufferStateLayer::applyPendingStates(Layer::State* stateToCommit) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800115 const bool stateUpdateAvailable = !mPendingStates.empty();
116 while (!mPendingStates.empty()) {
Marissa Wall61c58622018-07-18 10:12:20 -0700117 popPendingState(stateToCommit);
118 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800119 mCurrentStateModified = stateUpdateAvailable && mCurrentState.modified;
120 mCurrentState.modified = false;
Marissa Wall61c58622018-07-18 10:12:20 -0700121 return stateUpdateAvailable;
122}
123
Marissa Wall861616d2018-10-22 12:52:23 -0700124// Crop that applies to the window
125Rect BufferStateLayer::getCrop(const Layer::State& /*s*/) const {
126 return Rect::INVALID_RECT;
Marissa Wall61c58622018-07-18 10:12:20 -0700127}
128
129bool BufferStateLayer::setTransform(uint32_t transform) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800130 if (mCurrentState.transform == transform) return false;
131 mCurrentState.sequence++;
132 mCurrentState.transform = transform;
133 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700134 setTransactionFlags(eTransactionNeeded);
135 return true;
136}
137
138bool BufferStateLayer::setTransformToDisplayInverse(bool transformToDisplayInverse) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800139 if (mCurrentState.transformToDisplayInverse == transformToDisplayInverse) return false;
140 mCurrentState.sequence++;
141 mCurrentState.transformToDisplayInverse = transformToDisplayInverse;
142 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700143 setTransactionFlags(eTransactionNeeded);
144 return true;
145}
146
147bool BufferStateLayer::setCrop(const Rect& crop) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800148 if (mCurrentState.crop == crop) return false;
149 mCurrentState.sequence++;
150 mCurrentState.crop = crop;
151 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700152 setTransactionFlags(eTransactionNeeded);
153 return true;
154}
155
Marissa Wall861616d2018-10-22 12:52:23 -0700156bool BufferStateLayer::setFrame(const Rect& frame) {
157 int x = frame.left;
158 int y = frame.top;
159 int w = frame.getWidth();
160 int h = frame.getHeight();
161
Marissa Wall0f3242d2018-12-20 15:10:22 -0800162 if (x < 0) {
163 x = 0;
164 w = frame.right;
165 }
166
167 if (y < 0) {
168 y = 0;
169 h = frame.bottom;
170 }
171
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800172 if (mCurrentState.active.transform.tx() == x && mCurrentState.active.transform.ty() == y &&
173 mCurrentState.active.w == w && mCurrentState.active.h == h) {
Marissa Wall861616d2018-10-22 12:52:23 -0700174 return false;
175 }
176
177 if (!frame.isValid()) {
178 x = y = w = h = 0;
179 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800180 mCurrentState.active.transform.set(x, y);
181 mCurrentState.active.w = w;
182 mCurrentState.active.h = h;
Marissa Wall861616d2018-10-22 12:52:23 -0700183
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800184 mCurrentState.sequence++;
185 mCurrentState.modified = true;
Marissa Wall861616d2018-10-22 12:52:23 -0700186 setTransactionFlags(eTransactionNeeded);
187 return true;
188}
189
Marissa Wallfda30bb2018-10-12 11:34:28 -0700190bool BufferStateLayer::setBuffer(const sp<GraphicBuffer>& buffer) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800191 if (mCurrentState.buffer) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700192 mReleasePreviousBuffer = true;
193 }
194
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800195 mCurrentState.sequence++;
196 mCurrentState.buffer = buffer;
197 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700198 setTransactionFlags(eTransactionNeeded);
199 return true;
200}
201
202bool BufferStateLayer::setAcquireFence(const sp<Fence>& fence) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700203 // The acquire fences of BufferStateLayers have already signaled before they are set
204 mCallbackHandleAcquireTime = fence->getSignalTime();
205
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800206 mCurrentState.acquireFence = fence;
207 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700208 setTransactionFlags(eTransactionNeeded);
209 return true;
210}
211
212bool BufferStateLayer::setDataspace(ui::Dataspace dataspace) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800213 if (mCurrentState.dataspace == dataspace) return false;
214 mCurrentState.sequence++;
215 mCurrentState.dataspace = dataspace;
216 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700217 setTransactionFlags(eTransactionNeeded);
218 return true;
219}
220
221bool BufferStateLayer::setHdrMetadata(const HdrMetadata& hdrMetadata) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800222 if (mCurrentState.hdrMetadata == hdrMetadata) return false;
223 mCurrentState.sequence++;
224 mCurrentState.hdrMetadata = hdrMetadata;
225 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700226 setTransactionFlags(eTransactionNeeded);
227 return true;
228}
229
230bool BufferStateLayer::setSurfaceDamageRegion(const Region& surfaceDamage) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800231 mCurrentState.sequence++;
232 mCurrentState.surfaceDamageRegion = surfaceDamage;
233 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700234 setTransactionFlags(eTransactionNeeded);
235 return true;
236}
237
238bool BufferStateLayer::setApi(int32_t api) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800239 if (mCurrentState.api == api) return false;
240 mCurrentState.sequence++;
241 mCurrentState.api = api;
242 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700243 setTransactionFlags(eTransactionNeeded);
244 return true;
245}
246
247bool BufferStateLayer::setSidebandStream(const sp<NativeHandle>& sidebandStream) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800248 if (mCurrentState.sidebandStream == sidebandStream) return false;
249 mCurrentState.sequence++;
250 mCurrentState.sidebandStream = sidebandStream;
251 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700252 setTransactionFlags(eTransactionNeeded);
253
254 if (!mSidebandStreamChanged.exchange(true)) {
255 // mSidebandStreamChanged was false
256 mFlinger->signalLayerUpdate();
257 }
258 return true;
259}
260
Marissa Walle2ffb422018-10-12 11:33:52 -0700261bool BufferStateLayer::setTransactionCompletedListeners(
262 const std::vector<sp<CallbackHandle>>& handles) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700263 // If there is no handle, we will not send a callback so reset mReleasePreviousBuffer and return
Marissa Walle2ffb422018-10-12 11:33:52 -0700264 if (handles.empty()) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700265 mReleasePreviousBuffer = false;
Marissa Walle2ffb422018-10-12 11:33:52 -0700266 return false;
267 }
268
269 const bool willPresent = willPresentCurrentTransaction();
270
271 for (const auto& handle : handles) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700272 // If this transaction set a buffer on this layer, release its previous buffer
273 handle->releasePreviousBuffer = mReleasePreviousBuffer;
274
Marissa Walle2ffb422018-10-12 11:33:52 -0700275 // If this layer will be presented in this frame
276 if (willPresent) {
Marissa Wallfda30bb2018-10-12 11:34:28 -0700277 // If this transaction set an acquire fence on this layer, set its acquire time
278 handle->acquireTime = mCallbackHandleAcquireTime;
279
Marissa Walle2ffb422018-10-12 11:33:52 -0700280 // Notify the transaction completed thread that there is a pending latched callback
281 // handle
Marissa Wall5a68a772018-12-22 17:43:42 -0800282 mFlinger->getTransactionCompletedThread().registerPendingCallbackHandle(handle);
Marissa Walle2ffb422018-10-12 11:33:52 -0700283
284 // Store so latched time and release fence can be set
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800285 mCurrentState.callbackHandles.push_back(handle);
Marissa Walle2ffb422018-10-12 11:33:52 -0700286
287 } else { // If this layer will NOT need to be relatched and presented this frame
288 // Notify the transaction completed thread this handle is done
Marissa Wall5a68a772018-12-22 17:43:42 -0800289 mFlinger->getTransactionCompletedThread().addUnpresentedCallbackHandle(handle);
Marissa Walle2ffb422018-10-12 11:33:52 -0700290 }
291 }
292
Marissa Wallfda30bb2018-10-12 11:34:28 -0700293 mReleasePreviousBuffer = false;
294 mCallbackHandleAcquireTime = -1;
295
Marissa Walle2ffb422018-10-12 11:33:52 -0700296 return willPresent;
297}
298
Marissa Wall61c58622018-07-18 10:12:20 -0700299bool BufferStateLayer::setTransparentRegionHint(const Region& transparent) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800300 mCurrentState.transparentRegionHint = transparent;
301 mCurrentState.modified = true;
Marissa Wall61c58622018-07-18 10:12:20 -0700302 setTransactionFlags(eTransactionNeeded);
303 return true;
304}
305
Valerie Haua72e2812019-01-23 13:40:39 -0800306bool BufferStateLayer::setColor(const half3& color) {
307 // create color layer if one does not yet exist
308 if (!mCurrentState.bgColorLayer) {
309 uint32_t flags = ISurfaceComposerClient::eFXSurfaceColor;
310 const String8& name = mName + "BackgroundColorLayer";
311 mCurrentState.bgColorLayer =
312 new ColorLayer(LayerCreationArgs(mFlinger.get(), nullptr, name, 0, 0, flags));
313
314 // add to child list
315 addChild(mCurrentState.bgColorLayer);
316 mFlinger->mLayersAdded = true;
317 // set up SF to handle added color layer
318 if (isRemovedFromCurrentState()) {
319 mCurrentState.bgColorLayer->onRemovedFromCurrentState();
320 }
321 mFlinger->setTransactionFlags(eTransactionNeeded);
322 }
323
324 mCurrentState.bgColorLayer->setColor(color);
325 mCurrentState.bgColorLayer->setLayer(std::numeric_limits<int32_t>::min());
326
327 return true;
328}
329
330bool BufferStateLayer::setColorAlpha(float alpha) {
331 if (!mCurrentState.bgColorLayer) {
332 ALOGE("Attempting to set color alpha on a buffer state layer with no background color");
333 return false;
334 }
335 mCurrentState.bgColorLayer->setAlpha(alpha);
336 return true;
337}
338
339bool BufferStateLayer::setColorDataspace(ui::Dataspace dataspace) {
340 if (!mCurrentState.bgColorLayer) {
341 ALOGE("Attempting to set color dataspace on a buffer state layer with no background color");
342 return false;
343 }
344 mCurrentState.bgColorLayer->setDataspace(dataspace);
345 return true;
346}
347
Marissa Wall861616d2018-10-22 12:52:23 -0700348Rect BufferStateLayer::getBufferSize(const State& s) const {
349 // for buffer state layers we use the display frame size as the buffer size.
350 if (getActiveWidth(s) < UINT32_MAX && getActiveHeight(s) < UINT32_MAX) {
351 return Rect(getActiveWidth(s), getActiveHeight(s));
Marissa Wall61c58622018-07-18 10:12:20 -0700352 }
353
Marissa Wall861616d2018-10-22 12:52:23 -0700354 // if the display frame is not defined, use the parent bounds as the buffer size.
355 const auto& p = mDrawingParent.promote();
356 if (p != nullptr) {
357 Rect parentBounds = Rect(p->computeBounds(Region()));
358 if (!parentBounds.isEmpty()) {
359 return parentBounds;
360 }
361 }
362
363 // if there is no parent layer, use the buffer's bounds as the buffer size
364 if (s.buffer) {
365 return s.buffer->getBounds();
366 }
367 return Rect::INVALID_RECT;
Marissa Wall61c58622018-07-18 10:12:20 -0700368}
369// -----------------------------------------------------------------------
370
371// -----------------------------------------------------------------------
372// Interface implementation for BufferLayer
373// -----------------------------------------------------------------------
374bool BufferStateLayer::fenceHasSignaled() const {
375 if (latchUnsignaledBuffers()) {
376 return true;
377 }
378
379 return getDrawingState().acquireFence->getStatus() == Fence::Status::Signaled;
380}
381
382nsecs_t BufferStateLayer::getDesiredPresentTime() {
383 // TODO(marissaw): support an equivalent to desiredPresentTime for timestats metrics
384 return 0;
385}
386
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800387std::shared_ptr<FenceTime> BufferStateLayer::getCurrentFenceTime() const {
Marissa Wall61c58622018-07-18 10:12:20 -0700388 return std::make_shared<FenceTime>(getDrawingState().acquireFence);
389}
390
391void BufferStateLayer::getDrawingTransformMatrix(float *matrix) {
392 std::copy(std::begin(mTransformMatrix), std::end(mTransformMatrix), matrix);
393}
394
395uint32_t BufferStateLayer::getDrawingTransform() const {
396 return getDrawingState().transform;
397}
398
399ui::Dataspace BufferStateLayer::getDrawingDataSpace() const {
400 return getDrawingState().dataspace;
401}
402
Marissa Wall861616d2018-10-22 12:52:23 -0700403// Crop that applies to the buffer
Marissa Wall61c58622018-07-18 10:12:20 -0700404Rect BufferStateLayer::getDrawingCrop() const {
Marissa Wall861616d2018-10-22 12:52:23 -0700405 const State& s(getDrawingState());
406
407 if (s.crop.isEmpty() && s.buffer) {
408 return s.buffer->getBounds();
Valerie Hau0bc09152018-12-20 07:42:47 -0800409 } else if (s.buffer) {
410 Rect crop = s.crop;
411 crop.left = std::max(crop.left, 0);
412 crop.top = std::max(crop.top, 0);
413 uint32_t bufferWidth = s.buffer->getWidth();
414 uint32_t bufferHeight = s.buffer->getHeight();
415 if (bufferHeight <= std::numeric_limits<int32_t>::max() &&
416 bufferWidth <= std::numeric_limits<int32_t>::max()) {
417 crop.right = std::min(crop.right, static_cast<int32_t>(bufferWidth));
418 crop.bottom = std::min(crop.bottom, static_cast<int32_t>(bufferHeight));
419 }
420 if (!crop.isValid()) {
421 // Crop rect is out of bounds, return whole buffer
422 return s.buffer->getBounds();
423 }
424 return crop;
Marissa Wall861616d2018-10-22 12:52:23 -0700425 }
426 return s.crop;
Marissa Wall61c58622018-07-18 10:12:20 -0700427}
428
429uint32_t BufferStateLayer::getDrawingScalingMode() const {
Marissa Wallec463ac2018-10-08 12:35:04 -0700430 return NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW;
Marissa Wall61c58622018-07-18 10:12:20 -0700431}
432
433Region BufferStateLayer::getDrawingSurfaceDamage() const {
434 return getDrawingState().surfaceDamageRegion;
435}
436
437const HdrMetadata& BufferStateLayer::getDrawingHdrMetadata() const {
438 return getDrawingState().hdrMetadata;
439}
440
441int BufferStateLayer::getDrawingApi() const {
442 return getDrawingState().api;
443}
444
445PixelFormat BufferStateLayer::getPixelFormat() const {
Marissa Wall5aec6412018-11-14 11:49:18 -0800446 if (!mActiveBuffer) {
447 return PIXEL_FORMAT_NONE;
448 }
Marissa Wall61c58622018-07-18 10:12:20 -0700449 return mActiveBuffer->format;
450}
451
452uint64_t BufferStateLayer::getFrameNumber() const {
453 return mFrameNumber;
454}
455
456bool BufferStateLayer::getAutoRefresh() const {
457 // TODO(marissaw): support shared buffer mode
458 return false;
459}
460
461bool BufferStateLayer::getSidebandStreamChanged() const {
462 return mSidebandStreamChanged.load();
463}
464
465std::optional<Region> BufferStateLayer::latchSidebandStream(bool& recomputeVisibleRegions) {
466 if (mSidebandStreamChanged.exchange(false)) {
467 const State& s(getDrawingState());
468 // mSidebandStreamChanged was true
469 // replicated in LayerBE until FE/BE is ready to be synchronized
470 getBE().compositionInfo.hwc.sidebandStream = s.sidebandStream;
471 if (getBE().compositionInfo.hwc.sidebandStream != nullptr) {
472 setTransactionFlags(eTransactionNeeded);
473 mFlinger->setTransactionFlags(eTraversalNeeded);
474 }
475 recomputeVisibleRegions = true;
476
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800477 return getTransform().transform(Region(Rect(s.active.w, s.active.h)));
Marissa Wall61c58622018-07-18 10:12:20 -0700478 }
479 return {};
480}
481
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800482bool BufferStateLayer::hasFrameUpdate() const {
Marissa Wall024a1912018-08-13 13:55:35 -0700483 return mCurrentStateModified && getCurrentState().buffer != nullptr;
Marissa Wall61c58622018-07-18 10:12:20 -0700484}
485
486void BufferStateLayer::setFilteringEnabled(bool enabled) {
487 GLConsumer::computeTransformMatrix(mTransformMatrix.data(), mActiveBuffer, mCurrentCrop,
488 mCurrentTransform, enabled);
489}
490
Alec Mouri39801c02018-10-10 10:44:47 -0700491status_t BufferStateLayer::bindTextureImage() {
Marissa Wall61c58622018-07-18 10:12:20 -0700492 const State& s(getDrawingState());
493 auto& engine(mFlinger->getRenderEngine());
494
Alec Mouri0f714832018-11-12 15:31:06 -0800495 return engine.bindExternalTextureBuffer(mTextureName, s.buffer, s.acquireFence, false);
Marissa Wall61c58622018-07-18 10:12:20 -0700496}
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 Wall5a68a772018-12-22 17:43:42 -0800532 for (auto& handle : mDrawingState.callbackHandles) {
533 handle->latchTime = latchTime;
534 }
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");
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800557 mDrawingState.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 =
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800568 Fence::merge(fenceName, mDrawingState.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
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800573 mDrawingState.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 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800577 mDrawingState.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.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800586 mDrawingState.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.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800595 status_t err = bindTextureImage();
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);
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800604 mFlinger->mTimeStats->setAcquireFence(layerID, getFrameNumber(), getCurrentFenceTime());
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;
Alec Mouri0f714832018-11-12 15:31:06 -0800618 mActiveBufferFence = s.acquireFence;
Marissa Wall61c58622018-07-18 10:12:20 -0700619 getBE().compositionInfo.mBuffer = mActiveBuffer;
620 getBE().compositionInfo.mBufferSlot = 0;
621
622 return NO_ERROR;
623}
624
Alec Mouri0f714832018-11-12 15:31:06 -0800625bool BufferStateLayer::useCachedBufferForClientComposition() const {
626 // TODO: Store a proper staleness bit to support EGLImage caching.
627 return false;
628}
629
Marissa Wall61c58622018-07-18 10:12:20 -0700630status_t BufferStateLayer::updateFrameNumber(nsecs_t /*latchTime*/) {
631 // TODO(marissaw): support frame history events
632 mCurrentFrameNumber = mFrameNumber;
633 return NO_ERROR;
634}
635
Dominik Laskowski075d3172018-05-24 15:50:06 -0700636void BufferStateLayer::setHwcLayerBuffer(DisplayId displayId) {
Marissa Wall61c58622018-07-18 10:12:20 -0700637 auto& hwcInfo = getBE().mHwcLayers[displayId];
638 auto& hwcLayer = hwcInfo.layer;
639
640 const State& s(getDrawingState());
641
642 // TODO(marissaw): support more than one slot
643 uint32_t hwcSlot = 0;
644
645 auto error = hwcLayer->setBuffer(hwcSlot, s.buffer, s.acquireFence);
646 if (error != HWC2::Error::None) {
647 ALOGE("[%s] Failed to set buffer %p: %s (%d)", mName.string(),
648 s.buffer->handle, to_string(error).c_str(), static_cast<int32_t>(error));
649 }
650
Marissa Wall024a1912018-08-13 13:55:35 -0700651 mCurrentStateModified = false;
Marissa Wall61c58622018-07-18 10:12:20 -0700652 mFrameNumber++;
653}
654
655void BufferStateLayer::onFirstRef() {
Dan Stoza7b1b5a82018-07-31 16:00:21 -0700656 BufferLayer::onFirstRef();
657
Marissa Wall61c58622018-07-18 10:12:20 -0700658 if (const auto display = mFlinger->getDefaultDisplayDevice()) {
659 updateTransformHint(display);
660 }
661}
662
663} // namespace android