blob: bdecdb78d092b37f6f46d50f0655623c03e4faff [file] [log] [blame]
David Sodman0c69cad2017-08-21 12:12:51 -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 "BufferLayer"
20#define ATRACE_TAG ATRACE_TAG_GRAPHICS
21
Alec Mourie60041e2019-06-14 18:59:51 -070022#include "BufferLayer.h"
Lloyd Piquefeb73d72018-12-04 17:23:44 -080023
24#include <compositionengine/CompositionEngine.h>
25#include <compositionengine/Layer.h>
26#include <compositionengine/LayerCreationArgs.h>
Lloyd Piquef5275482019-01-29 18:42:42 -080027#include <compositionengine/LayerFECompositionState.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080028#include <compositionengine/OutputLayer.h>
29#include <compositionengine/impl/OutputLayerCompositionState.h>
Lloyd Piquefeb73d72018-12-04 17:23:44 -080030#include <cutils/compiler.h>
31#include <cutils/native_handle.h>
32#include <cutils/properties.h>
33#include <gui/BufferItem.h>
34#include <gui/BufferQueue.h>
chaviwf83ce182019-09-12 14:43:08 -070035#include <gui/GLConsumer.h>
Lloyd Piquefeb73d72018-12-04 17:23:44 -080036#include <gui/LayerDebugInfo.h>
37#include <gui/Surface.h>
38#include <renderengine/RenderEngine.h>
39#include <ui/DebugUtils.h>
40#include <utils/Errors.h>
41#include <utils/Log.h>
42#include <utils/NativeHandle.h>
43#include <utils/StopWatch.h>
44#include <utils/Trace.h>
45
Alec Mourie60041e2019-06-14 18:59:51 -070046#include <cmath>
47#include <cstdlib>
48#include <mutex>
49#include <sstream>
50
David Sodman0c69cad2017-08-21 12:12:51 -070051#include "Colorizer.h"
52#include "DisplayDevice.h"
Mikael Pessa90092f42019-08-26 17:22:04 -070053#include "FrameTracer/FrameTracer.h"
David Sodman0c69cad2017-08-21 12:12:51 -070054#include "LayerRejecter.h"
Yiwei Zhang7e666a52018-11-15 13:33:42 -080055#include "TimeStats/TimeStats.h"
56
David Sodman0c69cad2017-08-21 12:12:51 -070057namespace android {
58
Peiyong Lin1a70eca2019-11-15 09:33:33 -080059static constexpr float defaultMaxMasteringLuminance = 1000.0;
60static constexpr float defaultMaxContentLuminance = 1000.0;
61
Lloyd Pique42ab75e2018-09-12 20:46:03 -070062BufferLayer::BufferLayer(const LayerCreationArgs& args)
Lloyd Piquefeb73d72018-12-04 17:23:44 -080063 : Layer(args),
chaviwb4c6e582019-08-16 14:35:07 -070064 mTextureName(args.textureName),
Lloyd Piquefeb73d72018-12-04 17:23:44 -080065 mCompositionLayer{mFlinger->getCompositionEngine().createLayer(
66 compositionengine::LayerCreationArgs{this})} {
Dominik Laskowski87a07e42019-10-10 20:38:02 -070067 ALOGV("Creating Layer %s", getDebugName());
David Sodman0c69cad2017-08-21 12:12:51 -070068
Lloyd Pique42ab75e2018-09-12 20:46:03 -070069 mPremultipliedAlpha = !(args.flags & ISurfaceComposerClient::eNonPremultiplied);
David Sodman0c69cad2017-08-21 12:12:51 -070070
Lloyd Pique42ab75e2018-09-12 20:46:03 -070071 mPotentialCursor = args.flags & ISurfaceComposerClient::eCursorWindow;
72 mProtectedByApp = args.flags & ISurfaceComposerClient::eProtectedByApp;
David Sodman0c69cad2017-08-21 12:12:51 -070073}
74
75BufferLayer::~BufferLayer() {
chaviwb4c6e582019-08-16 14:35:07 -070076 if (!isClone()) {
77 // The original layer and the clone layer share the same texture. Therefore, only one of
78 // the layers, in this case the original layer, needs to handle the deletion. The original
79 // layer and the clone should be removed at the same time so there shouldn't be any issue
80 // with the clone layer trying to use the deleted texture.
81 mFlinger->deleteTextureAsync(mTextureName);
82 }
Yiwei Zhang1a88c402019-11-18 10:43:58 -080083 const int32_t layerId = getSequence();
84 mFlinger->mTimeStats->onDestroy(layerId);
85 mFlinger->mFrameTracer->onDestroy(layerId);
David Sodman0c69cad2017-08-21 12:12:51 -070086}
87
David Sodmaneb085e02017-10-05 18:49:04 -070088void BufferLayer::useSurfaceDamage() {
89 if (mFlinger->mForceFullDamage) {
90 surfaceDamageRegion = Region::INVALID_REGION;
91 } else {
chaviw4244e032019-09-04 11:27:49 -070092 surfaceDamageRegion = mBufferInfo.mSurfaceDamage;
David Sodmaneb085e02017-10-05 18:49:04 -070093 }
94}
95
96void BufferLayer::useEmptyDamage() {
97 surfaceDamageRegion.clear();
98}
99
Marissa Wallfd668622018-05-10 10:21:13 -0700100bool BufferLayer::isOpaque(const Layer::State& s) const {
101 // if we don't have a buffer or sidebandStream yet, we're translucent regardless of the
102 // layer's opaque flag.
chaviwd62d3062019-09-04 14:48:02 -0700103 if ((mSidebandStream == nullptr) && (mBufferInfo.mBuffer == nullptr)) {
Marissa Wallfd668622018-05-10 10:21:13 -0700104 return false;
105 }
106
107 // if the layer has the opaque flag, then we're always opaque,
108 // otherwise we use the current buffer's format.
109 return ((s.flags & layer_state_t::eLayerOpaque) != 0) || getOpacityForFormat(getPixelFormat());
David Sodman0c69cad2017-08-21 12:12:51 -0700110}
111
112bool BufferLayer::isVisible() const {
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -0700113 return !isHiddenByPolicy() && getAlpha() > 0.0f &&
chaviwd62d3062019-09-04 14:48:02 -0700114 (mBufferInfo.mBuffer != nullptr || mSidebandStream != nullptr);
David Sodman0c69cad2017-08-21 12:12:51 -0700115}
116
117bool BufferLayer::isFixedSize() const {
118 return getEffectiveScalingMode() != NATIVE_WINDOW_SCALING_MODE_FREEZE;
119}
120
Lloyd Piquea83776c2019-01-29 18:42:32 -0800121bool BufferLayer::usesSourceCrop() const {
122 return true;
123}
124
David Sodman0c69cad2017-08-21 12:12:51 -0700125static constexpr mat4 inverseOrientation(uint32_t transform) {
David Sodman41fdfc92017-11-06 16:09:56 -0800126 const mat4 flipH(-1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1);
127 const mat4 flipV(1, 0, 0, 0, 0, -1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1);
128 const mat4 rot90(0, 1, 0, 0, -1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1);
David Sodman0c69cad2017-08-21 12:12:51 -0700129 mat4 tr;
130
131 if (transform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
132 tr = tr * rot90;
133 }
134 if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_H) {
135 tr = tr * flipH;
136 }
137 if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_V) {
138 tr = tr * flipV;
139 }
140 return inverse(tr);
141}
142
Lloyd Piquef16688f2019-02-19 17:47:57 -0800143std::optional<renderengine::LayerSettings> BufferLayer::prepareClientComposition(
144 compositionengine::LayerFE::ClientCompositionTargetSettings& targetSettings) {
David Sodman0c69cad2017-08-21 12:12:51 -0700145 ATRACE_CALL();
Lloyd Piquef16688f2019-02-19 17:47:57 -0800146
147 auto result = Layer::prepareClientComposition(targetSettings);
148 if (!result) {
149 return result;
150 }
151
chaviwd62d3062019-09-04 14:48:02 -0700152 if (CC_UNLIKELY(mBufferInfo.mBuffer == 0)) {
David Sodman0c69cad2017-08-21 12:12:51 -0700153 // the texture has not been created yet, this Layer has
154 // in fact never been drawn into. This happens frequently with
155 // SurfaceView because the WindowManager can't know when the client
156 // has drawn the first time.
157
158 // If there is nothing under us, we paint the screen in black, otherwise
159 // we just skip this update.
160
161 // figure out if there is something below us
162 Region under;
163 bool finished = false;
164 mFlinger->mDrawingState.traverseInZOrder([&](Layer* layer) {
165 if (finished || layer == static_cast<BufferLayer const*>(this)) {
166 finished = true;
167 return;
168 }
Lloyd Piquea2468662019-03-07 21:31:06 -0800169
170 under.orSelf(layer->getScreenBounds());
David Sodman0c69cad2017-08-21 12:12:51 -0700171 });
172 // if not everything below us is covered, we plug the holes!
Lloyd Piquef16688f2019-02-19 17:47:57 -0800173 Region holes(targetSettings.clip.subtract(under));
David Sodman0c69cad2017-08-21 12:12:51 -0700174 if (!holes.isEmpty()) {
Lloyd Piquef16688f2019-02-19 17:47:57 -0800175 targetSettings.clearRegion.orSelf(holes);
David Sodman0c69cad2017-08-21 12:12:51 -0700176 }
Lloyd Piquef16688f2019-02-19 17:47:57 -0800177 return std::nullopt;
David Sodman0c69cad2017-08-21 12:12:51 -0700178 }
Lloyd Pique688abd42019-02-15 15:42:24 -0800179 bool blackOutLayer = (isProtected() && !targetSettings.supportsProtectedContent) ||
Lloyd Piquef16688f2019-02-19 17:47:57 -0800180 (isSecure() && !targetSettings.isSecure);
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000181 const State& s(getDrawingState());
Lloyd Piquef16688f2019-02-19 17:47:57 -0800182 auto& layer = *result;
David Sodman0c69cad2017-08-21 12:12:51 -0700183 if (!blackOutLayer) {
chaviwd62d3062019-09-04 14:48:02 -0700184 layer.source.buffer.buffer = mBufferInfo.mBuffer;
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000185 layer.source.buffer.isOpaque = isOpaque(s);
chaviwd62d3062019-09-04 14:48:02 -0700186 layer.source.buffer.fence = mBufferInfo.mFence;
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000187 layer.source.buffer.textureName = mTextureName;
188 layer.source.buffer.usePremultipliedAlpha = getPremultipledAlpha();
189 layer.source.buffer.isY410BT2020 = isHdrY410();
Peiyong Lin1a70eca2019-11-15 09:33:33 -0800190 bool hasSmpte2086 = mBufferInfo.mHdrMetadata.validTypes & HdrMetadata::SMPTE2086;
191 bool hasCta861_3 = mBufferInfo.mHdrMetadata.validTypes & HdrMetadata::CTA861_3;
192 layer.source.buffer.maxMasteringLuminance = hasSmpte2086
193 ? mBufferInfo.mHdrMetadata.smpte2086.maxLuminance
194 : defaultMaxMasteringLuminance;
195 layer.source.buffer.maxContentLuminance = hasCta861_3
196 ? mBufferInfo.mHdrMetadata.cta8613.maxContentLightLevel
197 : defaultMaxContentLuminance;
David Sodman0c69cad2017-08-21 12:12:51 -0700198 // TODO: we could be more subtle with isFixedSize()
Lloyd Piquef16688f2019-02-19 17:47:57 -0800199 const bool useFiltering = targetSettings.needsFiltering || mNeedsFiltering || isFixedSize();
David Sodman0c69cad2017-08-21 12:12:51 -0700200
201 // Query the texture matrix given our current filtering mode.
202 float textureMatrix[16];
chaviwf83ce182019-09-12 14:43:08 -0700203 getDrawingTransformMatrix(useFiltering, textureMatrix);
David Sodman0c69cad2017-08-21 12:12:51 -0700204
205 if (getTransformToDisplayInverse()) {
206 /*
207 * the code below applies the primary display's inverse transform to
208 * the texture transform
209 */
210 uint32_t transform = DisplayDevice::getPrimaryDisplayOrientationTransform();
211 mat4 tr = inverseOrientation(transform);
212
213 /**
214 * TODO(b/36727915): This is basically a hack.
215 *
216 * Ensure that regardless of the parent transformation,
217 * this buffer is always transformed from native display
218 * orientation to display orientation. For example, in the case
219 * of a camera where the buffer remains in native orientation,
220 * we want the pixels to always be upright.
221 */
222 sp<Layer> p = mDrawingParent.promote();
223 if (p != nullptr) {
224 const auto parentTransform = p->getTransform();
225 tr = tr * inverseOrientation(parentTransform.getOrientation());
226 }
227
228 // and finally apply it to the original texture matrix
229 const mat4 texTransform(mat4(static_cast<const float*>(textureMatrix)) * tr);
230 memcpy(textureMatrix, texTransform.asArray(), sizeof(textureMatrix));
231 }
232
Vishnu Nair4351ad52019-02-11 14:13:02 -0800233 const Rect win{getBounds()};
Marissa Wall290ad082019-03-06 13:23:47 -0800234 float bufferWidth = getBufferSize(s).getWidth();
235 float bufferHeight = getBufferSize(s).getHeight();
236
237 // BufferStateLayers can have a "buffer size" of [0, 0, -1, -1] when no display frame has
238 // been set and there is no parent layer bounds. In that case, the scale is meaningless so
239 // ignore them.
240 if (!getBufferSize(s).isValid()) {
241 bufferWidth = float(win.right) - float(win.left);
242 bufferHeight = float(win.bottom) - float(win.top);
243 }
David Sodman0c69cad2017-08-21 12:12:51 -0700244
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000245 const float scaleHeight = (float(win.bottom) - float(win.top)) / bufferHeight;
246 const float scaleWidth = (float(win.right) - float(win.left)) / bufferWidth;
247 const float translateY = float(win.top) / bufferHeight;
248 const float translateX = float(win.left) / bufferWidth;
249
250 // Flip y-coordinates because GLConsumer expects OpenGL convention.
251 mat4 tr = mat4::translate(vec4(.5, .5, 0, 1)) * mat4::scale(vec4(1, -1, 1, 1)) *
252 mat4::translate(vec4(-.5, -.5, 0, 1)) *
253 mat4::translate(vec4(translateX, translateY, 0, 1)) *
254 mat4::scale(vec4(scaleWidth, scaleHeight, 1.0, 1.0));
255
256 layer.source.buffer.useTextureFiltering = useFiltering;
257 layer.source.buffer.textureTransform = mat4(static_cast<const float*>(textureMatrix)) * tr;
David Sodman0c69cad2017-08-21 12:12:51 -0700258 } else {
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000259 // If layer is blacked out, force alpha to 1 so that we draw a black color
260 // layer.
261 layer.source.buffer.buffer = nullptr;
262 layer.alpha = 1.0;
David Sodman0c69cad2017-08-21 12:12:51 -0700263 }
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000264
Lloyd Piquef16688f2019-02-19 17:47:57 -0800265 return result;
David Sodman0c69cad2017-08-21 12:12:51 -0700266}
267
Marissa Wallfd668622018-05-10 10:21:13 -0700268bool BufferLayer::isHdrY410() const {
269 // pixel format is HDR Y410 masquerading as RGBA_1010102
chaviw4244e032019-09-04 11:27:49 -0700270 return (mBufferInfo.mDataspace == ui::Dataspace::BT2020_ITU_PQ &&
271 mBufferInfo.mApi == NATIVE_WINDOW_API_MEDIA &&
chaviwd62d3062019-09-04 14:48:02 -0700272 mBufferInfo.mBuffer->getPixelFormat() == HAL_PIXEL_FORMAT_RGBA_1010102);
David Sodmaneb085e02017-10-05 18:49:04 -0700273}
274
Lloyd Piquef5275482019-01-29 18:42:42 -0800275void BufferLayer::latchPerFrameState(
276 compositionengine::LayerFECompositionState& compositionState) const {
277 Layer::latchPerFrameState(compositionState);
David Sodman0c69cad2017-08-21 12:12:51 -0700278
279 // Sideband layers
Lloyd Piquef5275482019-01-29 18:42:42 -0800280 if (compositionState.sidebandStream.get()) {
281 compositionState.compositionType = Hwc2::IComposerClient::Composition::SIDEBAND;
David Sodman15094112018-10-11 09:39:37 -0700282 } else {
Lloyd Piquef5275482019-01-29 18:42:42 -0800283 // Normal buffer layers
chaviw4244e032019-09-04 11:27:49 -0700284 compositionState.hdrMetadata = mBufferInfo.mHdrMetadata;
Lloyd Piquef5275482019-01-29 18:42:42 -0800285 compositionState.compositionType = mPotentialCursor
286 ? Hwc2::IComposerClient::Composition::CURSOR
287 : Hwc2::IComposerClient::Composition::DEVICE;
David Sodman0c69cad2017-08-21 12:12:51 -0700288 }
David Sodman0c69cad2017-08-21 12:12:51 -0700289}
290
Marissa Wallfd668622018-05-10 10:21:13 -0700291bool BufferLayer::onPreComposition(nsecs_t refreshStartTime) {
chaviwd62d3062019-09-04 14:48:02 -0700292 if (mBufferInfo.mBuffer != nullptr) {
Marissa Wallfd668622018-05-10 10:21:13 -0700293 Mutex::Autolock lock(mFrameEventHistoryMutex);
294 mFrameEventHistory.addPreComposition(mCurrentFrameNumber, refreshStartTime);
David Sodman0c69cad2017-08-21 12:12:51 -0700295 }
Marissa Wallfd668622018-05-10 10:21:13 -0700296 mRefreshPending = false;
297 return hasReadyFrame();
David Sodman0c69cad2017-08-21 12:12:51 -0700298}
299
Adithya Srinivasanb69e0762019-11-11 18:39:53 -0800300bool BufferLayer::onPostComposition(sp<const DisplayDevice> displayDevice,
Dominik Laskowski075d3172018-05-24 15:50:06 -0700301 const std::shared_ptr<FenceTime>& glDoneFence,
Marissa Wallfd668622018-05-10 10:21:13 -0700302 const std::shared_ptr<FenceTime>& presentFence,
303 const CompositorTiming& compositorTiming) {
304 // mFrameLatencyNeeded is true when a new frame was latched for the
305 // composition.
chaviw74b03172019-08-19 11:09:03 -0700306 if (!mBufferInfo.mFrameLatencyNeeded) return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700307
308 // Update mFrameEventHistory.
Dan Stoza436ccf32018-06-21 12:10:12 -0700309 {
Marissa Wallfd668622018-05-10 10:21:13 -0700310 Mutex::Autolock lock(mFrameEventHistoryMutex);
311 mFrameEventHistory.addPostComposition(mCurrentFrameNumber, glDoneFence, presentFence,
312 compositorTiming);
David Sodman0c69cad2017-08-21 12:12:51 -0700313 }
314
Marissa Wallfd668622018-05-10 10:21:13 -0700315 // Update mFrameTracker.
chaviw4244e032019-09-04 11:27:49 -0700316 nsecs_t desiredPresentTime = mBufferInfo.mDesiredPresentTime;
Marissa Wallfd668622018-05-10 10:21:13 -0700317 mFrameTracker.setDesiredPresentTime(desiredPresentTime);
318
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800319 const int32_t layerId = getSequence();
320 mFlinger->mTimeStats->setDesiredTime(layerId, mCurrentFrameNumber, desiredPresentTime);
Marissa Wallfd668622018-05-10 10:21:13 -0700321
Adithya Srinivasanb69e0762019-11-11 18:39:53 -0800322 const auto outputLayer = findOutputLayerForDisplay(displayDevice);
323 if (outputLayer && outputLayer->requiresClientComposition()) {
324 nsecs_t clientCompositionTimestamp = outputLayer->getState().clientCompositionTimestamp;
325 mFlinger->mFrameTracer->traceTimestamp(layerId, getCurrentBufferId(), mCurrentFrameNumber,
326 clientCompositionTimestamp,
327 FrameTracer::FrameEvent::FALLBACK_COMPOSITION);
328 }
329
chaviw4244e032019-09-04 11:27:49 -0700330 std::shared_ptr<FenceTime> frameReadyFence = mBufferInfo.mFenceTime;
Marissa Wallfd668622018-05-10 10:21:13 -0700331 if (frameReadyFence->isValid()) {
332 mFrameTracker.setFrameReadyFence(std::move(frameReadyFence));
333 } else {
334 // There was no fence for this frame, so assume that it was ready
335 // to be presented at the desired present time.
336 mFrameTracker.setFrameReadyTime(desiredPresentTime);
Dominik Laskowski45de9bd2018-06-11 17:44:10 -0700337 }
Marissa Wallfd668622018-05-10 10:21:13 -0700338
Adithya Srinivasanb69e0762019-11-11 18:39:53 -0800339 const auto displayId = displayDevice->getId();
Marissa Wallfd668622018-05-10 10:21:13 -0700340 if (presentFence->isValid()) {
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800341 mFlinger->mTimeStats->setPresentFence(layerId, mCurrentFrameNumber, presentFence);
342 mFlinger->mFrameTracer->traceFence(layerId, getCurrentBufferId(), mCurrentFrameNumber,
Mikael Pessa90092f42019-08-26 17:22:04 -0700343 presentFence, FrameTracer::FrameEvent::PRESENT_FENCE);
Marissa Wallfd668622018-05-10 10:21:13 -0700344 mFrameTracker.setActualPresentFence(std::shared_ptr<FenceTime>(presentFence));
Dominik Laskowski075d3172018-05-24 15:50:06 -0700345 } else if (displayId && mFlinger->getHwComposer().isConnected(*displayId)) {
Marissa Wallfd668622018-05-10 10:21:13 -0700346 // The HWC doesn't support present fences, so use the refresh
347 // timestamp instead.
Dominik Laskowski075d3172018-05-24 15:50:06 -0700348 const nsecs_t actualPresentTime = mFlinger->getHwComposer().getRefreshTimestamp(*displayId);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800349 mFlinger->mTimeStats->setPresentTime(layerId, mCurrentFrameNumber, actualPresentTime);
350 mFlinger->mFrameTracer->traceTimestamp(layerId, getCurrentBufferId(), mCurrentFrameNumber,
Mikael Pessa90092f42019-08-26 17:22:04 -0700351 actualPresentTime,
352 FrameTracer::FrameEvent::PRESENT_FENCE);
Marissa Wallfd668622018-05-10 10:21:13 -0700353 mFrameTracker.setActualPresentTime(actualPresentTime);
354 }
355
356 mFrameTracker.advanceFrame();
chaviw74b03172019-08-19 11:09:03 -0700357 mBufferInfo.mFrameLatencyNeeded = false;
Marissa Wallfd668622018-05-10 10:21:13 -0700358 return true;
David Sodman0c69cad2017-08-21 12:12:51 -0700359}
360
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700361bool BufferLayer::latchBuffer(bool& recomputeVisibleRegions, nsecs_t latchTime,
362 nsecs_t expectedPresentTime) {
Marissa Wallfd668622018-05-10 10:21:13 -0700363 ATRACE_CALL();
David Sodman0c69cad2017-08-21 12:12:51 -0700364
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800365 bool refreshRequired = latchSidebandStream(recomputeVisibleRegions);
David Sodman0c69cad2017-08-21 12:12:51 -0700366
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800367 if (refreshRequired) {
368 return refreshRequired;
David Sodman0c69cad2017-08-21 12:12:51 -0700369 }
370
Marissa Wallfd668622018-05-10 10:21:13 -0700371 if (!hasReadyFrame()) {
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800372 return false;
David Sodman0c69cad2017-08-21 12:12:51 -0700373 }
David Sodman0c69cad2017-08-21 12:12:51 -0700374
Marissa Wallfd668622018-05-10 10:21:13 -0700375 // if we've already called updateTexImage() without going through
376 // a composition step, we have to skip this layer at this point
377 // because we cannot call updateTeximage() without a corresponding
378 // compositionComplete() call.
379 // we'll trigger an update in onPreComposition().
380 if (mRefreshPending) {
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800381 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700382 }
383
384 // If the head buffer's acquire fence hasn't signaled yet, return and
385 // try again later
386 if (!fenceHasSignaled()) {
Ady Abraham09bd3922019-04-08 10:44:56 -0700387 ATRACE_NAME("!fenceHasSignaled()");
David Sodman0c69cad2017-08-21 12:12:51 -0700388 mFlinger->signalLayerUpdate();
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800389 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700390 }
391
392 // Capture the old state of the layer for comparisons later
393 const State& s(getDrawingState());
394 const bool oldOpacity = isOpaque(s);
chaviwd62d3062019-09-04 14:48:02 -0700395
396 BufferInfo oldBufferInfo = mBufferInfo;
Marissa Wallfd668622018-05-10 10:21:13 -0700397
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700398 if (!allTransactionsSignaled(expectedPresentTime)) {
Marissa Wallebb486e2019-05-15 14:08:08 -0700399 mFlinger->setTransactionFlags(eTraversalNeeded);
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800400 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700401 }
402
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700403 status_t err = updateTexImage(recomputeVisibleRegions, latchTime, expectedPresentTime);
Marissa Wallfd668622018-05-10 10:21:13 -0700404 if (err != NO_ERROR) {
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800405 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700406 }
407
408 err = updateActiveBuffer();
409 if (err != NO_ERROR) {
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800410 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700411 }
412
Marissa Wallfd668622018-05-10 10:21:13 -0700413 err = updateFrameNumber(latchTime);
414 if (err != NO_ERROR) {
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800415 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700416 }
417
chaviw4244e032019-09-04 11:27:49 -0700418 gatherBufferInfo();
419
Marissa Wallfd668622018-05-10 10:21:13 -0700420 mRefreshPending = true;
chaviw74b03172019-08-19 11:09:03 -0700421 mBufferInfo.mFrameLatencyNeeded = true;
chaviwd62d3062019-09-04 14:48:02 -0700422 if (oldBufferInfo.mBuffer == nullptr) {
Marissa Wallfd668622018-05-10 10:21:13 -0700423 // the first time we receive a buffer, we need to trigger a
424 // geometry invalidation.
425 recomputeVisibleRegions = true;
426 }
427
chaviw4244e032019-09-04 11:27:49 -0700428 if ((mBufferInfo.mCrop != oldBufferInfo.mCrop) ||
429 (mBufferInfo.mTransform != oldBufferInfo.mTransform) ||
430 (mBufferInfo.mScaleMode != oldBufferInfo.mScaleMode) ||
431 (mBufferInfo.mTransformToDisplayInverse != oldBufferInfo.mTransformToDisplayInverse)) {
Marissa Wallfd668622018-05-10 10:21:13 -0700432 recomputeVisibleRegions = true;
433 }
434
chaviwd62d3062019-09-04 14:48:02 -0700435 if (oldBufferInfo.mBuffer != nullptr) {
436 uint32_t bufWidth = mBufferInfo.mBuffer->getWidth();
437 uint32_t bufHeight = mBufferInfo.mBuffer->getHeight();
438 if (bufWidth != uint32_t(oldBufferInfo.mBuffer->width) ||
439 bufHeight != uint32_t(oldBufferInfo.mBuffer->height)) {
Marissa Wallfd668622018-05-10 10:21:13 -0700440 recomputeVisibleRegions = true;
441 }
442 }
443
444 if (oldOpacity != isOpaque(s)) {
445 recomputeVisibleRegions = true;
446 }
447
448 // Remove any sync points corresponding to the buffer which was just
449 // latched
450 {
451 Mutex::Autolock lock(mLocalSyncPointMutex);
452 auto point = mLocalSyncPoints.begin();
453 while (point != mLocalSyncPoints.end()) {
454 if (!(*point)->frameIsAvailable() || !(*point)->transactionIsApplied()) {
455 // This sync point must have been added since we started
456 // latching. Don't drop it yet.
457 ++point;
458 continue;
459 }
460
461 if ((*point)->getFrameNumber() <= mCurrentFrameNumber) {
Alec Mourie60041e2019-06-14 18:59:51 -0700462 std::stringstream ss;
463 ss << "Dropping sync point " << (*point)->getFrameNumber();
464 ATRACE_NAME(ss.str().c_str());
Marissa Wallfd668622018-05-10 10:21:13 -0700465 point = mLocalSyncPoints.erase(point);
466 } else {
467 ++point;
468 }
469 }
470 }
471
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800472 return true;
Marissa Wallfd668622018-05-10 10:21:13 -0700473}
474
475// transaction
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700476void BufferLayer::notifyAvailableFrames(nsecs_t expectedPresentTime) {
477 const auto headFrameNumber = getHeadFrameNumber(expectedPresentTime);
Ady Abrahamcd1580c2019-04-29 15:40:03 -0700478 const bool headFenceSignaled = fenceHasSignaled();
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700479 const bool presentTimeIsCurrent = framePresentTimeIsCurrent(expectedPresentTime);
Marissa Wallfd668622018-05-10 10:21:13 -0700480 Mutex::Autolock lock(mLocalSyncPointMutex);
481 for (auto& point : mLocalSyncPoints) {
Ady Abrahamcd1580c2019-04-29 15:40:03 -0700482 if (headFrameNumber >= point->getFrameNumber() && headFenceSignaled &&
483 presentTimeIsCurrent) {
Marissa Wallfd668622018-05-10 10:21:13 -0700484 point->setFrameAvailable();
chaviw43cb3cb2019-05-31 15:23:41 -0700485 sp<Layer> requestedSyncLayer = point->getRequestedSyncLayer();
486 if (requestedSyncLayer) {
487 // Need to update the transaction flag to ensure the layer's pending transaction
488 // gets applied.
489 requestedSyncLayer->setTransactionFlags(eTransactionNeeded);
490 }
Marissa Wallfd668622018-05-10 10:21:13 -0700491 }
David Sodman0c69cad2017-08-21 12:12:51 -0700492 }
493}
494
Marissa Wallfd668622018-05-10 10:21:13 -0700495bool BufferLayer::hasReadyFrame() const {
Marissa Wall024a1912018-08-13 13:55:35 -0700496 return hasFrameUpdate() || getSidebandStreamChanged() || getAutoRefresh();
Marissa Wallfd668622018-05-10 10:21:13 -0700497}
498
499uint32_t BufferLayer::getEffectiveScalingMode() const {
500 if (mOverrideScalingMode >= 0) {
501 return mOverrideScalingMode;
502 }
503
chaviw4244e032019-09-04 11:27:49 -0700504 return mBufferInfo.mScaleMode;
Marissa Wallfd668622018-05-10 10:21:13 -0700505}
506
507bool BufferLayer::isProtected() const {
chaviwd62d3062019-09-04 14:48:02 -0700508 const sp<GraphicBuffer>& buffer(mBufferInfo.mBuffer);
Marissa Wallfd668622018-05-10 10:21:13 -0700509 return (buffer != 0) && (buffer->getUsage() & GRALLOC_USAGE_PROTECTED);
510}
511
512bool BufferLayer::latchUnsignaledBuffers() {
513 static bool propertyLoaded = false;
514 static bool latch = false;
515 static std::mutex mutex;
516 std::lock_guard<std::mutex> lock(mutex);
517 if (!propertyLoaded) {
518 char value[PROPERTY_VALUE_MAX] = {};
519 property_get("debug.sf.latch_unsignaled", value, "0");
520 latch = atoi(value);
521 propertyLoaded = true;
522 }
523 return latch;
524}
525
526// h/w composer set-up
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700527bool BufferLayer::allTransactionsSignaled(nsecs_t expectedPresentTime) {
528 const auto headFrameNumber = getHeadFrameNumber(expectedPresentTime);
Marissa Wallfd668622018-05-10 10:21:13 -0700529 bool matchingFramesFound = false;
530 bool allTransactionsApplied = true;
531 Mutex::Autolock lock(mLocalSyncPointMutex);
532
533 for (auto& point : mLocalSyncPoints) {
534 if (point->getFrameNumber() > headFrameNumber) {
535 break;
536 }
537 matchingFramesFound = true;
538
539 if (!point->frameIsAvailable()) {
540 // We haven't notified the remote layer that the frame for
541 // this point is available yet. Notify it now, and then
542 // abort this attempt to latch.
543 point->setFrameAvailable();
544 allTransactionsApplied = false;
545 break;
546 }
547
548 allTransactionsApplied = allTransactionsApplied && point->transactionIsApplied();
549 }
550 return !matchingFramesFound || allTransactionsApplied;
David Sodman0c69cad2017-08-21 12:12:51 -0700551}
552
553// As documented in libhardware header, formats in the range
554// 0x100 - 0x1FF are specific to the HAL implementation, and
555// are known to have no alpha channel
556// TODO: move definition for device-specific range into
557// hardware.h, instead of using hard-coded values here.
558#define HARDWARE_IS_DEVICE_FORMAT(f) ((f) >= 0x100 && (f) <= 0x1FF)
559
560bool BufferLayer::getOpacityForFormat(uint32_t format) {
561 if (HARDWARE_IS_DEVICE_FORMAT(format)) {
562 return true;
563 }
564 switch (format) {
565 case HAL_PIXEL_FORMAT_RGBA_8888:
566 case HAL_PIXEL_FORMAT_BGRA_8888:
567 case HAL_PIXEL_FORMAT_RGBA_FP16:
568 case HAL_PIXEL_FORMAT_RGBA_1010102:
569 return false;
570 }
571 // in all other case, we have no blending (also for unknown formats)
572 return true;
573}
574
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800575bool BufferLayer::needsFiltering(const sp<const DisplayDevice>& displayDevice) const {
Lloyd Piquef16688f2019-02-19 17:47:57 -0800576 // If we are not capturing based on the state of a known display device,
577 // just return false.
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800578 if (displayDevice == nullptr) {
Lloyd Piquef16688f2019-02-19 17:47:57 -0800579 return false;
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800580 }
581
582 const auto outputLayer = findOutputLayerForDisplay(displayDevice);
583 if (outputLayer == nullptr) {
Lloyd Piquef16688f2019-02-19 17:47:57 -0800584 return false;
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800585 }
586
Lloyd Piquef16688f2019-02-19 17:47:57 -0800587 // We need filtering if the sourceCrop rectangle size does not match the
588 // displayframe rectangle size (not a 1:1 render)
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800589 const auto& compositionState = outputLayer->getState();
590 const auto displayFrame = compositionState.displayFrame;
591 const auto sourceCrop = compositionState.sourceCrop;
Lloyd Piquef16688f2019-02-19 17:47:57 -0800592 return sourceCrop.getHeight() != displayFrame.getHeight() ||
Peiyong Linc2020ca2019-01-10 11:36:12 -0800593 sourceCrop.getWidth() != displayFrame.getWidth();
Chia-I Wu692e0832018-06-05 15:46:58 -0700594}
595
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700596uint64_t BufferLayer::getHeadFrameNumber(nsecs_t expectedPresentTime) const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800597 if (hasFrameUpdate()) {
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700598 return getFrameNumber(expectedPresentTime);
David Sodman0c69cad2017-08-21 12:12:51 -0700599 } else {
600 return mCurrentFrameNumber;
601 }
602}
603
Vishnu Nair60356342018-11-13 13:00:45 -0800604Rect BufferLayer::getBufferSize(const State& s) const {
605 // If we have a sideband stream, or we are scaling the buffer then return the layer size since
606 // we cannot determine the buffer size.
607 if ((s.sidebandStream != nullptr) ||
608 (getEffectiveScalingMode() != NATIVE_WINDOW_SCALING_MODE_FREEZE)) {
609 return Rect(getActiveWidth(s), getActiveHeight(s));
610 }
611
chaviwd62d3062019-09-04 14:48:02 -0700612 if (mBufferInfo.mBuffer == nullptr) {
Vishnu Nair60356342018-11-13 13:00:45 -0800613 return Rect::INVALID_RECT;
614 }
615
chaviwd62d3062019-09-04 14:48:02 -0700616 uint32_t bufWidth = mBufferInfo.mBuffer->getWidth();
617 uint32_t bufHeight = mBufferInfo.mBuffer->getHeight();
Vishnu Nair60356342018-11-13 13:00:45 -0800618
619 // Undo any transformations on the buffer and return the result.
chaviw4244e032019-09-04 11:27:49 -0700620 if (mBufferInfo.mTransform & ui::Transform::ROT_90) {
Vishnu Nair60356342018-11-13 13:00:45 -0800621 std::swap(bufWidth, bufHeight);
622 }
623
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800624 if (getTransformToDisplayInverse()) {
Vishnu Nair60356342018-11-13 13:00:45 -0800625 uint32_t invTransform = DisplayDevice::getPrimaryDisplayOrientationTransform();
626 if (invTransform & ui::Transform::ROT_90) {
627 std::swap(bufWidth, bufHeight);
628 }
629 }
630
631 return Rect(bufWidth, bufHeight);
632}
633
Lloyd Piquefeb73d72018-12-04 17:23:44 -0800634std::shared_ptr<compositionengine::Layer> BufferLayer::getCompositionLayer() const {
635 return mCompositionLayer;
636}
637
Vishnu Nair4351ad52019-02-11 14:13:02 -0800638FloatRect BufferLayer::computeSourceBounds(const FloatRect& parentBounds) const {
639 const State& s(getDrawingState());
640
641 // If we have a sideband stream, or we are scaling the buffer then return the layer size since
642 // we cannot determine the buffer size.
643 if ((s.sidebandStream != nullptr) ||
644 (getEffectiveScalingMode() != NATIVE_WINDOW_SCALING_MODE_FREEZE)) {
645 return FloatRect(0, 0, getActiveWidth(s), getActiveHeight(s));
646 }
647
chaviwd62d3062019-09-04 14:48:02 -0700648 if (mBufferInfo.mBuffer == nullptr) {
Vishnu Nair4351ad52019-02-11 14:13:02 -0800649 return parentBounds;
650 }
651
chaviwd62d3062019-09-04 14:48:02 -0700652 uint32_t bufWidth = mBufferInfo.mBuffer->getWidth();
653 uint32_t bufHeight = mBufferInfo.mBuffer->getHeight();
Vishnu Nair4351ad52019-02-11 14:13:02 -0800654
655 // Undo any transformations on the buffer and return the result.
chaviw4244e032019-09-04 11:27:49 -0700656 if (mBufferInfo.mTransform & ui::Transform::ROT_90) {
Vishnu Nair4351ad52019-02-11 14:13:02 -0800657 std::swap(bufWidth, bufHeight);
658 }
659
660 if (getTransformToDisplayInverse()) {
661 uint32_t invTransform = DisplayDevice::getPrimaryDisplayOrientationTransform();
662 if (invTransform & ui::Transform::ROT_90) {
663 std::swap(bufWidth, bufHeight);
664 }
665 }
666
667 return FloatRect(0, 0, bufWidth, bufHeight);
668}
669
chaviw49a108c2019-08-12 11:23:06 -0700670void BufferLayer::latchAndReleaseBuffer() {
671 mRefreshPending = false;
672 if (hasReadyFrame()) {
673 bool ignored = false;
674 latchBuffer(ignored, systemTime(), 0 /* expectedPresentTime */);
675 }
676 releasePendingBuffer(systemTime());
677}
678
chaviw4244e032019-09-04 11:27:49 -0700679PixelFormat BufferLayer::getPixelFormat() const {
680 return mBufferInfo.mPixelFormat;
681}
682
683bool BufferLayer::getTransformToDisplayInverse() const {
684 return mBufferInfo.mTransformToDisplayInverse;
685}
686
687Rect BufferLayer::getBufferCrop() const {
688 // this is the crop rectangle that applies to the buffer
689 // itself (as opposed to the window)
690 if (!mBufferInfo.mCrop.isEmpty()) {
691 // if the buffer crop is defined, we use that
692 return mBufferInfo.mCrop;
chaviwd62d3062019-09-04 14:48:02 -0700693 } else if (mBufferInfo.mBuffer != nullptr) {
chaviw4244e032019-09-04 11:27:49 -0700694 // otherwise we use the whole buffer
chaviwd62d3062019-09-04 14:48:02 -0700695 return mBufferInfo.mBuffer->getBounds();
chaviw4244e032019-09-04 11:27:49 -0700696 } else {
697 // if we don't have a buffer yet, we use an empty/invalid crop
698 return Rect();
699 }
700}
701
702uint32_t BufferLayer::getBufferTransform() const {
703 return mBufferInfo.mTransform;
704}
705
706ui::Dataspace BufferLayer::getDataSpace() const {
707 return mBufferInfo.mDataspace;
708}
709
710ui::Dataspace BufferLayer::translateDataspace(ui::Dataspace dataspace) {
711 ui::Dataspace updatedDataspace = dataspace;
712 // translate legacy dataspaces to modern dataspaces
713 switch (dataspace) {
714 case ui::Dataspace::SRGB:
715 updatedDataspace = ui::Dataspace::V0_SRGB;
716 break;
717 case ui::Dataspace::SRGB_LINEAR:
718 updatedDataspace = ui::Dataspace::V0_SRGB_LINEAR;
719 break;
720 case ui::Dataspace::JFIF:
721 updatedDataspace = ui::Dataspace::V0_JFIF;
722 break;
723 case ui::Dataspace::BT601_625:
724 updatedDataspace = ui::Dataspace::V0_BT601_625;
725 break;
726 case ui::Dataspace::BT601_525:
727 updatedDataspace = ui::Dataspace::V0_BT601_525;
728 break;
729 case ui::Dataspace::BT709:
730 updatedDataspace = ui::Dataspace::V0_BT709;
731 break;
732 default:
733 break;
734 }
735
736 return updatedDataspace;
737}
738
chaviwd62d3062019-09-04 14:48:02 -0700739sp<GraphicBuffer> BufferLayer::getBuffer() const {
740 return mBufferInfo.mBuffer;
741}
742
chaviwf83ce182019-09-12 14:43:08 -0700743void BufferLayer::getDrawingTransformMatrix(bool filteringEnabled, float outMatrix[16]) {
744 GLConsumer::computeTransformMatrix(outMatrix, mBufferInfo.mBuffer, mBufferInfo.mCrop,
745 mBufferInfo.mTransform, filteringEnabled);
746}
747
chaviwb4c6e582019-08-16 14:35:07 -0700748void BufferLayer::setInitialValuesForClone(const sp<Layer>& clonedFrom) {
749 Layer::setInitialValuesForClone(clonedFrom);
750
751 sp<BufferLayer> bufferClonedFrom = static_cast<BufferLayer*>(clonedFrom.get());
752 mPremultipliedAlpha = bufferClonedFrom->mPremultipliedAlpha;
753 mPotentialCursor = bufferClonedFrom->mPotentialCursor;
754 mProtectedByApp = bufferClonedFrom->mProtectedByApp;
chaviw74b03172019-08-19 11:09:03 -0700755
756 updateCloneBufferInfo();
757}
758
759void BufferLayer::updateCloneBufferInfo() {
760 if (!isClone() || !isClonedFromAlive()) {
761 return;
762 }
763
764 sp<BufferLayer> clonedFrom = static_cast<BufferLayer*>(getClonedFrom().get());
765 mBufferInfo = clonedFrom->mBufferInfo;
766 mSidebandStream = clonedFrom->mSidebandStream;
767 surfaceDamageRegion = clonedFrom->surfaceDamageRegion;
768 mCurrentFrameNumber = clonedFrom->mCurrentFrameNumber.load();
769 mPreviousFrameNumber = clonedFrom->mPreviousFrameNumber;
770
771 // After buffer info is updated, the drawingState from the real layer needs to be copied into
772 // the cloned. This is because some properties of drawingState can change when latchBuffer is
chaviwaf87b3e2019-10-01 16:59:28 -0700773 // called. However, copying the drawingState would also overwrite the cloned layer's relatives
774 // and touchableRegionCrop. Therefore, temporarily store the relatives so they can be set in
775 // the cloned drawingState again.
chaviw74b03172019-08-19 11:09:03 -0700776 wp<Layer> tmpZOrderRelativeOf = mDrawingState.zOrderRelativeOf;
777 SortedVector<wp<Layer>> tmpZOrderRelatives = mDrawingState.zOrderRelatives;
chaviwaf87b3e2019-10-01 16:59:28 -0700778 wp<Layer> tmpTouchableRegionCrop = mDrawingState.touchableRegionCrop;
779 InputWindowInfo tmpInputInfo = mDrawingState.inputInfo;
780
chaviw74b03172019-08-19 11:09:03 -0700781 mDrawingState = clonedFrom->mDrawingState;
chaviwaf87b3e2019-10-01 16:59:28 -0700782
783 mDrawingState.touchableRegionCrop = tmpTouchableRegionCrop;
chaviw74b03172019-08-19 11:09:03 -0700784 mDrawingState.zOrderRelativeOf = tmpZOrderRelativeOf;
785 mDrawingState.zOrderRelatives = tmpZOrderRelatives;
chaviwaf87b3e2019-10-01 16:59:28 -0700786 mDrawingState.inputInfo = tmpInputInfo;
chaviwb4c6e582019-08-16 14:35:07 -0700787}
788
David Sodman0c69cad2017-08-21 12:12:51 -0700789} // namespace android
790
791#if defined(__gl_h_)
792#error "don't include gl/gl.h in this file"
793#endif
794
795#if defined(__gl2_h_)
796#error "don't include gl2/gl2.h in this file"
797#endif