blob: b060b047afc0d97408f247d76c0a1d406a32af7f [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>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080025#include <compositionengine/Display.h>
Lloyd Piquefeb73d72018-12-04 17:23:44 -080026#include <compositionengine/Layer.h>
27#include <compositionengine/LayerCreationArgs.h>
Lloyd Piquef5275482019-01-29 18:42:42 -080028#include <compositionengine/LayerFECompositionState.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080029#include <compositionengine/OutputLayer.h>
Lloyd Pique0b785d82018-12-04 17:25:27 -080030#include <compositionengine/impl/LayerCompositionState.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080031#include <compositionengine/impl/OutputLayerCompositionState.h>
Lloyd Piquefeb73d72018-12-04 17:23:44 -080032#include <cutils/compiler.h>
33#include <cutils/native_handle.h>
34#include <cutils/properties.h>
35#include <gui/BufferItem.h>
36#include <gui/BufferQueue.h>
chaviwf83ce182019-09-12 14:43:08 -070037#include <gui/GLConsumer.h>
Lloyd Piquefeb73d72018-12-04 17:23:44 -080038#include <gui/LayerDebugInfo.h>
39#include <gui/Surface.h>
40#include <renderengine/RenderEngine.h>
41#include <ui/DebugUtils.h>
42#include <utils/Errors.h>
43#include <utils/Log.h>
44#include <utils/NativeHandle.h>
45#include <utils/StopWatch.h>
46#include <utils/Trace.h>
47
Alec Mourie60041e2019-06-14 18:59:51 -070048#include <cmath>
49#include <cstdlib>
50#include <mutex>
51#include <sstream>
52
David Sodman0c69cad2017-08-21 12:12:51 -070053#include "Colorizer.h"
54#include "DisplayDevice.h"
Mikael Pessa90092f42019-08-26 17:22:04 -070055#include "FrameTracer/FrameTracer.h"
David Sodman0c69cad2017-08-21 12:12:51 -070056#include "LayerRejecter.h"
Yiwei Zhang7e666a52018-11-15 13:33:42 -080057#include "TimeStats/TimeStats.h"
58
David Sodman0c69cad2017-08-21 12:12:51 -070059namespace android {
60
Lloyd Pique42ab75e2018-09-12 20:46:03 -070061BufferLayer::BufferLayer(const LayerCreationArgs& args)
Lloyd Piquefeb73d72018-12-04 17:23:44 -080062 : Layer(args),
63 mTextureName(args.flinger->getNewTexture()),
64 mCompositionLayer{mFlinger->getCompositionEngine().createLayer(
65 compositionengine::LayerCreationArgs{this})} {
Lloyd Pique42ab75e2018-09-12 20:46:03 -070066 ALOGV("Creating Layer %s", args.name.string());
David Sodman0c69cad2017-08-21 12:12:51 -070067
Lloyd Pique42ab75e2018-09-12 20:46:03 -070068 mPremultipliedAlpha = !(args.flags & ISurfaceComposerClient::eNonPremultiplied);
David Sodman0c69cad2017-08-21 12:12:51 -070069
Lloyd Pique42ab75e2018-09-12 20:46:03 -070070 mPotentialCursor = args.flags & ISurfaceComposerClient::eCursorWindow;
71 mProtectedByApp = args.flags & ISurfaceComposerClient::eProtectedByApp;
David Sodman0c69cad2017-08-21 12:12:51 -070072}
73
74BufferLayer::~BufferLayer() {
David Sodman0c69cad2017-08-21 12:12:51 -070075 mFlinger->deleteTextureAsync(mTextureName);
Mikael Pessa90092f42019-08-26 17:22:04 -070076 const int32_t layerID = getSequence();
77 mFlinger->mTimeStats->onDestroy(layerID);
78 mFlinger->mFrameTracer->onDestroy(layerID);
David Sodman0c69cad2017-08-21 12:12:51 -070079}
80
David Sodmaneb085e02017-10-05 18:49:04 -070081void BufferLayer::useSurfaceDamage() {
82 if (mFlinger->mForceFullDamage) {
83 surfaceDamageRegion = Region::INVALID_REGION;
84 } else {
chaviw4244e032019-09-04 11:27:49 -070085 surfaceDamageRegion = mBufferInfo.mSurfaceDamage;
David Sodmaneb085e02017-10-05 18:49:04 -070086 }
87}
88
89void BufferLayer::useEmptyDamage() {
90 surfaceDamageRegion.clear();
91}
92
Marissa Wallfd668622018-05-10 10:21:13 -070093bool BufferLayer::isOpaque(const Layer::State& s) const {
94 // if we don't have a buffer or sidebandStream yet, we're translucent regardless of the
95 // layer's opaque flag.
chaviwd62d3062019-09-04 14:48:02 -070096 if ((mSidebandStream == nullptr) && (mBufferInfo.mBuffer == nullptr)) {
Marissa Wallfd668622018-05-10 10:21:13 -070097 return false;
98 }
99
100 // if the layer has the opaque flag, then we're always opaque,
101 // otherwise we use the current buffer's format.
102 return ((s.flags & layer_state_t::eLayerOpaque) != 0) || getOpacityForFormat(getPixelFormat());
David Sodman0c69cad2017-08-21 12:12:51 -0700103}
104
105bool BufferLayer::isVisible() const {
Ady Abrahama315ce72019-04-24 14:35:20 -0700106 bool visible = !(isHiddenByPolicy()) && getAlpha() > 0.0f &&
chaviwd62d3062019-09-04 14:48:02 -0700107 (mBufferInfo.mBuffer != nullptr || mSidebandStream != nullptr);
Ady Abrahama315ce72019-04-24 14:35:20 -0700108 mFlinger->mScheduler->setLayerVisibility(mSchedulerLayerHandle, visible);
109
110 return visible;
David Sodman0c69cad2017-08-21 12:12:51 -0700111}
112
113bool BufferLayer::isFixedSize() const {
114 return getEffectiveScalingMode() != NATIVE_WINDOW_SCALING_MODE_FREEZE;
115}
116
Lloyd Piquea83776c2019-01-29 18:42:32 -0800117bool BufferLayer::usesSourceCrop() const {
118 return true;
119}
120
David Sodman0c69cad2017-08-21 12:12:51 -0700121static constexpr mat4 inverseOrientation(uint32_t transform) {
David Sodman41fdfc92017-11-06 16:09:56 -0800122 const mat4 flipH(-1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1);
123 const mat4 flipV(1, 0, 0, 0, 0, -1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1);
124 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 -0700125 mat4 tr;
126
127 if (transform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
128 tr = tr * rot90;
129 }
130 if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_H) {
131 tr = tr * flipH;
132 }
133 if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_V) {
134 tr = tr * flipV;
135 }
136 return inverse(tr);
137}
138
Lloyd Piquef16688f2019-02-19 17:47:57 -0800139std::optional<renderengine::LayerSettings> BufferLayer::prepareClientComposition(
140 compositionengine::LayerFE::ClientCompositionTargetSettings& targetSettings) {
David Sodman0c69cad2017-08-21 12:12:51 -0700141 ATRACE_CALL();
Lloyd Piquef16688f2019-02-19 17:47:57 -0800142
143 auto result = Layer::prepareClientComposition(targetSettings);
144 if (!result) {
145 return result;
146 }
147
chaviwd62d3062019-09-04 14:48:02 -0700148 if (CC_UNLIKELY(mBufferInfo.mBuffer == 0)) {
David Sodman0c69cad2017-08-21 12:12:51 -0700149 // the texture has not been created yet, this Layer has
150 // in fact never been drawn into. This happens frequently with
151 // SurfaceView because the WindowManager can't know when the client
152 // has drawn the first time.
153
154 // If there is nothing under us, we paint the screen in black, otherwise
155 // we just skip this update.
156
157 // figure out if there is something below us
158 Region under;
159 bool finished = false;
160 mFlinger->mDrawingState.traverseInZOrder([&](Layer* layer) {
161 if (finished || layer == static_cast<BufferLayer const*>(this)) {
162 finished = true;
163 return;
164 }
Lloyd Piquea2468662019-03-07 21:31:06 -0800165
166 under.orSelf(layer->getScreenBounds());
David Sodman0c69cad2017-08-21 12:12:51 -0700167 });
168 // if not everything below us is covered, we plug the holes!
Lloyd Piquef16688f2019-02-19 17:47:57 -0800169 Region holes(targetSettings.clip.subtract(under));
David Sodman0c69cad2017-08-21 12:12:51 -0700170 if (!holes.isEmpty()) {
Lloyd Piquef16688f2019-02-19 17:47:57 -0800171 targetSettings.clearRegion.orSelf(holes);
David Sodman0c69cad2017-08-21 12:12:51 -0700172 }
Lloyd Piquef16688f2019-02-19 17:47:57 -0800173 return std::nullopt;
David Sodman0c69cad2017-08-21 12:12:51 -0700174 }
Lloyd Pique688abd42019-02-15 15:42:24 -0800175 bool blackOutLayer = (isProtected() && !targetSettings.supportsProtectedContent) ||
Lloyd Piquef16688f2019-02-19 17:47:57 -0800176 (isSecure() && !targetSettings.isSecure);
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000177 const State& s(getDrawingState());
Lloyd Piquef16688f2019-02-19 17:47:57 -0800178 auto& layer = *result;
David Sodman0c69cad2017-08-21 12:12:51 -0700179 if (!blackOutLayer) {
chaviwd62d3062019-09-04 14:48:02 -0700180 layer.source.buffer.buffer = mBufferInfo.mBuffer;
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000181 layer.source.buffer.isOpaque = isOpaque(s);
chaviwd62d3062019-09-04 14:48:02 -0700182 layer.source.buffer.fence = mBufferInfo.mFence;
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000183 layer.source.buffer.textureName = mTextureName;
184 layer.source.buffer.usePremultipliedAlpha = getPremultipledAlpha();
185 layer.source.buffer.isY410BT2020 = isHdrY410();
David Sodman0c69cad2017-08-21 12:12:51 -0700186 // TODO: we could be more subtle with isFixedSize()
Lloyd Piquef16688f2019-02-19 17:47:57 -0800187 const bool useFiltering = targetSettings.needsFiltering || mNeedsFiltering || isFixedSize();
David Sodman0c69cad2017-08-21 12:12:51 -0700188
189 // Query the texture matrix given our current filtering mode.
190 float textureMatrix[16];
chaviwf83ce182019-09-12 14:43:08 -0700191 getDrawingTransformMatrix(useFiltering, textureMatrix);
David Sodman0c69cad2017-08-21 12:12:51 -0700192
193 if (getTransformToDisplayInverse()) {
194 /*
195 * the code below applies the primary display's inverse transform to
196 * the texture transform
197 */
198 uint32_t transform = DisplayDevice::getPrimaryDisplayOrientationTransform();
199 mat4 tr = inverseOrientation(transform);
200
201 /**
202 * TODO(b/36727915): This is basically a hack.
203 *
204 * Ensure that regardless of the parent transformation,
205 * this buffer is always transformed from native display
206 * orientation to display orientation. For example, in the case
207 * of a camera where the buffer remains in native orientation,
208 * we want the pixels to always be upright.
209 */
210 sp<Layer> p = mDrawingParent.promote();
211 if (p != nullptr) {
212 const auto parentTransform = p->getTransform();
213 tr = tr * inverseOrientation(parentTransform.getOrientation());
214 }
215
216 // and finally apply it to the original texture matrix
217 const mat4 texTransform(mat4(static_cast<const float*>(textureMatrix)) * tr);
218 memcpy(textureMatrix, texTransform.asArray(), sizeof(textureMatrix));
219 }
220
Vishnu Nair4351ad52019-02-11 14:13:02 -0800221 const Rect win{getBounds()};
Marissa Wall290ad082019-03-06 13:23:47 -0800222 float bufferWidth = getBufferSize(s).getWidth();
223 float bufferHeight = getBufferSize(s).getHeight();
224
225 // BufferStateLayers can have a "buffer size" of [0, 0, -1, -1] when no display frame has
226 // been set and there is no parent layer bounds. In that case, the scale is meaningless so
227 // ignore them.
228 if (!getBufferSize(s).isValid()) {
229 bufferWidth = float(win.right) - float(win.left);
230 bufferHeight = float(win.bottom) - float(win.top);
231 }
David Sodman0c69cad2017-08-21 12:12:51 -0700232
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000233 const float scaleHeight = (float(win.bottom) - float(win.top)) / bufferHeight;
234 const float scaleWidth = (float(win.right) - float(win.left)) / bufferWidth;
235 const float translateY = float(win.top) / bufferHeight;
236 const float translateX = float(win.left) / bufferWidth;
237
238 // Flip y-coordinates because GLConsumer expects OpenGL convention.
239 mat4 tr = mat4::translate(vec4(.5, .5, 0, 1)) * mat4::scale(vec4(1, -1, 1, 1)) *
240 mat4::translate(vec4(-.5, -.5, 0, 1)) *
241 mat4::translate(vec4(translateX, translateY, 0, 1)) *
242 mat4::scale(vec4(scaleWidth, scaleHeight, 1.0, 1.0));
243
244 layer.source.buffer.useTextureFiltering = useFiltering;
245 layer.source.buffer.textureTransform = mat4(static_cast<const float*>(textureMatrix)) * tr;
David Sodman0c69cad2017-08-21 12:12:51 -0700246 } else {
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000247 // If layer is blacked out, force alpha to 1 so that we draw a black color
248 // layer.
249 layer.source.buffer.buffer = nullptr;
250 layer.alpha = 1.0;
David Sodman0c69cad2017-08-21 12:12:51 -0700251 }
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000252
Lloyd Piquef16688f2019-02-19 17:47:57 -0800253 return result;
David Sodman0c69cad2017-08-21 12:12:51 -0700254}
255
Marissa Wallfd668622018-05-10 10:21:13 -0700256bool BufferLayer::isHdrY410() const {
257 // pixel format is HDR Y410 masquerading as RGBA_1010102
chaviw4244e032019-09-04 11:27:49 -0700258 return (mBufferInfo.mDataspace == ui::Dataspace::BT2020_ITU_PQ &&
259 mBufferInfo.mApi == NATIVE_WINDOW_API_MEDIA &&
chaviwd62d3062019-09-04 14:48:02 -0700260 mBufferInfo.mBuffer->getPixelFormat() == HAL_PIXEL_FORMAT_RGBA_1010102);
David Sodmaneb085e02017-10-05 18:49:04 -0700261}
262
Lloyd Piquef5275482019-01-29 18:42:42 -0800263void BufferLayer::latchPerFrameState(
264 compositionengine::LayerFECompositionState& compositionState) const {
265 Layer::latchPerFrameState(compositionState);
David Sodman0c69cad2017-08-21 12:12:51 -0700266
267 // Sideband layers
Lloyd Piquef5275482019-01-29 18:42:42 -0800268 if (compositionState.sidebandStream.get()) {
269 compositionState.compositionType = Hwc2::IComposerClient::Composition::SIDEBAND;
David Sodman15094112018-10-11 09:39:37 -0700270 } else {
Lloyd Piquef5275482019-01-29 18:42:42 -0800271 // Normal buffer layers
chaviw4244e032019-09-04 11:27:49 -0700272 compositionState.hdrMetadata = mBufferInfo.mHdrMetadata;
Lloyd Piquef5275482019-01-29 18:42:42 -0800273 compositionState.compositionType = mPotentialCursor
274 ? Hwc2::IComposerClient::Composition::CURSOR
275 : Hwc2::IComposerClient::Composition::DEVICE;
David Sodman0c69cad2017-08-21 12:12:51 -0700276 }
David Sodman0c69cad2017-08-21 12:12:51 -0700277}
278
Marissa Wallfd668622018-05-10 10:21:13 -0700279bool BufferLayer::onPreComposition(nsecs_t refreshStartTime) {
chaviwd62d3062019-09-04 14:48:02 -0700280 if (mBufferInfo.mBuffer != nullptr) {
Marissa Wallfd668622018-05-10 10:21:13 -0700281 Mutex::Autolock lock(mFrameEventHistoryMutex);
282 mFrameEventHistory.addPreComposition(mCurrentFrameNumber, refreshStartTime);
David Sodman0c69cad2017-08-21 12:12:51 -0700283 }
Marissa Wallfd668622018-05-10 10:21:13 -0700284 mRefreshPending = false;
285 return hasReadyFrame();
David Sodman0c69cad2017-08-21 12:12:51 -0700286}
287
Dominik Laskowski075d3172018-05-24 15:50:06 -0700288bool BufferLayer::onPostComposition(const std::optional<DisplayId>& displayId,
289 const std::shared_ptr<FenceTime>& glDoneFence,
Marissa Wallfd668622018-05-10 10:21:13 -0700290 const std::shared_ptr<FenceTime>& presentFence,
291 const CompositorTiming& compositorTiming) {
292 // mFrameLatencyNeeded is true when a new frame was latched for the
293 // composition.
294 if (!mFrameLatencyNeeded) return false;
295
296 // Update mFrameEventHistory.
Dan Stoza436ccf32018-06-21 12:10:12 -0700297 {
Marissa Wallfd668622018-05-10 10:21:13 -0700298 Mutex::Autolock lock(mFrameEventHistoryMutex);
299 mFrameEventHistory.addPostComposition(mCurrentFrameNumber, glDoneFence, presentFence,
300 compositorTiming);
David Sodman0c69cad2017-08-21 12:12:51 -0700301 }
302
Marissa Wallfd668622018-05-10 10:21:13 -0700303 // Update mFrameTracker.
chaviw4244e032019-09-04 11:27:49 -0700304 nsecs_t desiredPresentTime = mBufferInfo.mDesiredPresentTime;
Marissa Wallfd668622018-05-10 10:21:13 -0700305 mFrameTracker.setDesiredPresentTime(desiredPresentTime);
306
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700307 const int32_t layerID = getSequence();
Yiwei Zhang7e666a52018-11-15 13:33:42 -0800308 mFlinger->mTimeStats->setDesiredTime(layerID, mCurrentFrameNumber, desiredPresentTime);
Marissa Wallfd668622018-05-10 10:21:13 -0700309
chaviw4244e032019-09-04 11:27:49 -0700310 std::shared_ptr<FenceTime> frameReadyFence = mBufferInfo.mFenceTime;
Marissa Wallfd668622018-05-10 10:21:13 -0700311 if (frameReadyFence->isValid()) {
312 mFrameTracker.setFrameReadyFence(std::move(frameReadyFence));
313 } else {
314 // There was no fence for this frame, so assume that it was ready
315 // to be presented at the desired present time.
316 mFrameTracker.setFrameReadyTime(desiredPresentTime);
Dominik Laskowski45de9bd2018-06-11 17:44:10 -0700317 }
Marissa Wallfd668622018-05-10 10:21:13 -0700318
319 if (presentFence->isValid()) {
Yiwei Zhang7e666a52018-11-15 13:33:42 -0800320 mFlinger->mTimeStats->setPresentFence(layerID, mCurrentFrameNumber, presentFence);
Mikael Pessa90092f42019-08-26 17:22:04 -0700321 mFlinger->mFrameTracer->traceFence(layerID, getCurrentBufferId(), mCurrentFrameNumber,
322 presentFence, FrameTracer::FrameEvent::PRESENT_FENCE);
Marissa Wallfd668622018-05-10 10:21:13 -0700323 mFrameTracker.setActualPresentFence(std::shared_ptr<FenceTime>(presentFence));
Dominik Laskowski075d3172018-05-24 15:50:06 -0700324 } else if (displayId && mFlinger->getHwComposer().isConnected(*displayId)) {
Marissa Wallfd668622018-05-10 10:21:13 -0700325 // The HWC doesn't support present fences, so use the refresh
326 // timestamp instead.
Dominik Laskowski075d3172018-05-24 15:50:06 -0700327 const nsecs_t actualPresentTime = mFlinger->getHwComposer().getRefreshTimestamp(*displayId);
Yiwei Zhang7e666a52018-11-15 13:33:42 -0800328 mFlinger->mTimeStats->setPresentTime(layerID, mCurrentFrameNumber, actualPresentTime);
Mikael Pessa90092f42019-08-26 17:22:04 -0700329 mFlinger->mFrameTracer->traceTimestamp(layerID, getCurrentBufferId(), mCurrentFrameNumber,
330 actualPresentTime,
331 FrameTracer::FrameEvent::PRESENT_FENCE);
Marissa Wallfd668622018-05-10 10:21:13 -0700332 mFrameTracker.setActualPresentTime(actualPresentTime);
333 }
334
335 mFrameTracker.advanceFrame();
336 mFrameLatencyNeeded = false;
337 return true;
David Sodman0c69cad2017-08-21 12:12:51 -0700338}
339
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700340bool BufferLayer::latchBuffer(bool& recomputeVisibleRegions, nsecs_t latchTime,
341 nsecs_t expectedPresentTime) {
Marissa Wallfd668622018-05-10 10:21:13 -0700342 ATRACE_CALL();
David Sodman0c69cad2017-08-21 12:12:51 -0700343
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800344 bool refreshRequired = latchSidebandStream(recomputeVisibleRegions);
David Sodman0c69cad2017-08-21 12:12:51 -0700345
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800346 if (refreshRequired) {
347 return refreshRequired;
David Sodman0c69cad2017-08-21 12:12:51 -0700348 }
349
Marissa Wallfd668622018-05-10 10:21:13 -0700350 if (!hasReadyFrame()) {
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800351 return false;
David Sodman0c69cad2017-08-21 12:12:51 -0700352 }
David Sodman0c69cad2017-08-21 12:12:51 -0700353
Marissa Wallfd668622018-05-10 10:21:13 -0700354 // if we've already called updateTexImage() without going through
355 // a composition step, we have to skip this layer at this point
356 // because we cannot call updateTeximage() without a corresponding
357 // compositionComplete() call.
358 // we'll trigger an update in onPreComposition().
359 if (mRefreshPending) {
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800360 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700361 }
362
363 // If the head buffer's acquire fence hasn't signaled yet, return and
364 // try again later
365 if (!fenceHasSignaled()) {
Ady Abraham09bd3922019-04-08 10:44:56 -0700366 ATRACE_NAME("!fenceHasSignaled()");
David Sodman0c69cad2017-08-21 12:12:51 -0700367 mFlinger->signalLayerUpdate();
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800368 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700369 }
370
371 // Capture the old state of the layer for comparisons later
372 const State& s(getDrawingState());
373 const bool oldOpacity = isOpaque(s);
chaviwd62d3062019-09-04 14:48:02 -0700374
375 BufferInfo oldBufferInfo = mBufferInfo;
Marissa Wallfd668622018-05-10 10:21:13 -0700376
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700377 if (!allTransactionsSignaled(expectedPresentTime)) {
Marissa Wallebb486e2019-05-15 14:08:08 -0700378 mFlinger->setTransactionFlags(eTraversalNeeded);
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800379 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700380 }
381
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700382 status_t err = updateTexImage(recomputeVisibleRegions, latchTime, expectedPresentTime);
Marissa Wallfd668622018-05-10 10:21:13 -0700383 if (err != NO_ERROR) {
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800384 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700385 }
386
387 err = updateActiveBuffer();
388 if (err != NO_ERROR) {
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800389 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700390 }
391
Marissa Wallfd668622018-05-10 10:21:13 -0700392 err = updateFrameNumber(latchTime);
393 if (err != NO_ERROR) {
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800394 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700395 }
396
chaviw4244e032019-09-04 11:27:49 -0700397 gatherBufferInfo();
398
Marissa Wallfd668622018-05-10 10:21:13 -0700399 mRefreshPending = true;
400 mFrameLatencyNeeded = true;
chaviwd62d3062019-09-04 14:48:02 -0700401 if (oldBufferInfo.mBuffer == nullptr) {
Marissa Wallfd668622018-05-10 10:21:13 -0700402 // the first time we receive a buffer, we need to trigger a
403 // geometry invalidation.
404 recomputeVisibleRegions = true;
405 }
406
chaviw4244e032019-09-04 11:27:49 -0700407 if ((mBufferInfo.mCrop != oldBufferInfo.mCrop) ||
408 (mBufferInfo.mTransform != oldBufferInfo.mTransform) ||
409 (mBufferInfo.mScaleMode != oldBufferInfo.mScaleMode) ||
410 (mBufferInfo.mTransformToDisplayInverse != oldBufferInfo.mTransformToDisplayInverse)) {
Marissa Wallfd668622018-05-10 10:21:13 -0700411 recomputeVisibleRegions = true;
412 }
413
chaviwd62d3062019-09-04 14:48:02 -0700414 if (oldBufferInfo.mBuffer != nullptr) {
415 uint32_t bufWidth = mBufferInfo.mBuffer->getWidth();
416 uint32_t bufHeight = mBufferInfo.mBuffer->getHeight();
417 if (bufWidth != uint32_t(oldBufferInfo.mBuffer->width) ||
418 bufHeight != uint32_t(oldBufferInfo.mBuffer->height)) {
Marissa Wallfd668622018-05-10 10:21:13 -0700419 recomputeVisibleRegions = true;
420 }
421 }
422
423 if (oldOpacity != isOpaque(s)) {
424 recomputeVisibleRegions = true;
425 }
426
427 // Remove any sync points corresponding to the buffer which was just
428 // latched
429 {
430 Mutex::Autolock lock(mLocalSyncPointMutex);
431 auto point = mLocalSyncPoints.begin();
432 while (point != mLocalSyncPoints.end()) {
433 if (!(*point)->frameIsAvailable() || !(*point)->transactionIsApplied()) {
434 // This sync point must have been added since we started
435 // latching. Don't drop it yet.
436 ++point;
437 continue;
438 }
439
440 if ((*point)->getFrameNumber() <= mCurrentFrameNumber) {
Alec Mourie60041e2019-06-14 18:59:51 -0700441 std::stringstream ss;
442 ss << "Dropping sync point " << (*point)->getFrameNumber();
443 ATRACE_NAME(ss.str().c_str());
Marissa Wallfd668622018-05-10 10:21:13 -0700444 point = mLocalSyncPoints.erase(point);
445 } else {
446 ++point;
447 }
448 }
449 }
450
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800451 return true;
Marissa Wallfd668622018-05-10 10:21:13 -0700452}
453
454// transaction
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700455void BufferLayer::notifyAvailableFrames(nsecs_t expectedPresentTime) {
456 const auto headFrameNumber = getHeadFrameNumber(expectedPresentTime);
Ady Abrahamcd1580c2019-04-29 15:40:03 -0700457 const bool headFenceSignaled = fenceHasSignaled();
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700458 const bool presentTimeIsCurrent = framePresentTimeIsCurrent(expectedPresentTime);
Marissa Wallfd668622018-05-10 10:21:13 -0700459 Mutex::Autolock lock(mLocalSyncPointMutex);
460 for (auto& point : mLocalSyncPoints) {
Ady Abrahamcd1580c2019-04-29 15:40:03 -0700461 if (headFrameNumber >= point->getFrameNumber() && headFenceSignaled &&
462 presentTimeIsCurrent) {
Marissa Wallfd668622018-05-10 10:21:13 -0700463 point->setFrameAvailable();
chaviw43cb3cb2019-05-31 15:23:41 -0700464 sp<Layer> requestedSyncLayer = point->getRequestedSyncLayer();
465 if (requestedSyncLayer) {
466 // Need to update the transaction flag to ensure the layer's pending transaction
467 // gets applied.
468 requestedSyncLayer->setTransactionFlags(eTransactionNeeded);
469 }
Marissa Wallfd668622018-05-10 10:21:13 -0700470 }
David Sodman0c69cad2017-08-21 12:12:51 -0700471 }
472}
473
Marissa Wallfd668622018-05-10 10:21:13 -0700474bool BufferLayer::hasReadyFrame() const {
Marissa Wall024a1912018-08-13 13:55:35 -0700475 return hasFrameUpdate() || getSidebandStreamChanged() || getAutoRefresh();
Marissa Wallfd668622018-05-10 10:21:13 -0700476}
477
478uint32_t BufferLayer::getEffectiveScalingMode() const {
479 if (mOverrideScalingMode >= 0) {
480 return mOverrideScalingMode;
481 }
482
chaviw4244e032019-09-04 11:27:49 -0700483 return mBufferInfo.mScaleMode;
Marissa Wallfd668622018-05-10 10:21:13 -0700484}
485
486bool BufferLayer::isProtected() const {
chaviwd62d3062019-09-04 14:48:02 -0700487 const sp<GraphicBuffer>& buffer(mBufferInfo.mBuffer);
Marissa Wallfd668622018-05-10 10:21:13 -0700488 return (buffer != 0) && (buffer->getUsage() & GRALLOC_USAGE_PROTECTED);
489}
490
491bool BufferLayer::latchUnsignaledBuffers() {
492 static bool propertyLoaded = false;
493 static bool latch = false;
494 static std::mutex mutex;
495 std::lock_guard<std::mutex> lock(mutex);
496 if (!propertyLoaded) {
497 char value[PROPERTY_VALUE_MAX] = {};
498 property_get("debug.sf.latch_unsignaled", value, "0");
499 latch = atoi(value);
500 propertyLoaded = true;
501 }
502 return latch;
503}
504
505// h/w composer set-up
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700506bool BufferLayer::allTransactionsSignaled(nsecs_t expectedPresentTime) {
507 const auto headFrameNumber = getHeadFrameNumber(expectedPresentTime);
Marissa Wallfd668622018-05-10 10:21:13 -0700508 bool matchingFramesFound = false;
509 bool allTransactionsApplied = true;
510 Mutex::Autolock lock(mLocalSyncPointMutex);
511
512 for (auto& point : mLocalSyncPoints) {
513 if (point->getFrameNumber() > headFrameNumber) {
514 break;
515 }
516 matchingFramesFound = true;
517
518 if (!point->frameIsAvailable()) {
519 // We haven't notified the remote layer that the frame for
520 // this point is available yet. Notify it now, and then
521 // abort this attempt to latch.
522 point->setFrameAvailable();
523 allTransactionsApplied = false;
524 break;
525 }
526
527 allTransactionsApplied = allTransactionsApplied && point->transactionIsApplied();
528 }
529 return !matchingFramesFound || allTransactionsApplied;
David Sodman0c69cad2017-08-21 12:12:51 -0700530}
531
532// As documented in libhardware header, formats in the range
533// 0x100 - 0x1FF are specific to the HAL implementation, and
534// are known to have no alpha channel
535// TODO: move definition for device-specific range into
536// hardware.h, instead of using hard-coded values here.
537#define HARDWARE_IS_DEVICE_FORMAT(f) ((f) >= 0x100 && (f) <= 0x1FF)
538
539bool BufferLayer::getOpacityForFormat(uint32_t format) {
540 if (HARDWARE_IS_DEVICE_FORMAT(format)) {
541 return true;
542 }
543 switch (format) {
544 case HAL_PIXEL_FORMAT_RGBA_8888:
545 case HAL_PIXEL_FORMAT_BGRA_8888:
546 case HAL_PIXEL_FORMAT_RGBA_FP16:
547 case HAL_PIXEL_FORMAT_RGBA_1010102:
548 return false;
549 }
550 // in all other case, we have no blending (also for unknown formats)
551 return true;
552}
553
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800554bool BufferLayer::needsFiltering(const sp<const DisplayDevice>& displayDevice) const {
Lloyd Piquef16688f2019-02-19 17:47:57 -0800555 // If we are not capturing based on the state of a known display device,
556 // just return false.
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800557 if (displayDevice == nullptr) {
Lloyd Piquef16688f2019-02-19 17:47:57 -0800558 return false;
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800559 }
560
561 const auto outputLayer = findOutputLayerForDisplay(displayDevice);
562 if (outputLayer == nullptr) {
Lloyd Piquef16688f2019-02-19 17:47:57 -0800563 return false;
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800564 }
565
Lloyd Piquef16688f2019-02-19 17:47:57 -0800566 // We need filtering if the sourceCrop rectangle size does not match the
567 // displayframe rectangle size (not a 1:1 render)
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800568 const auto& compositionState = outputLayer->getState();
569 const auto displayFrame = compositionState.displayFrame;
570 const auto sourceCrop = compositionState.sourceCrop;
Lloyd Piquef16688f2019-02-19 17:47:57 -0800571 return sourceCrop.getHeight() != displayFrame.getHeight() ||
Peiyong Linc2020ca2019-01-10 11:36:12 -0800572 sourceCrop.getWidth() != displayFrame.getWidth();
Chia-I Wu692e0832018-06-05 15:46:58 -0700573}
574
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700575uint64_t BufferLayer::getHeadFrameNumber(nsecs_t expectedPresentTime) const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800576 if (hasFrameUpdate()) {
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700577 return getFrameNumber(expectedPresentTime);
David Sodman0c69cad2017-08-21 12:12:51 -0700578 } else {
579 return mCurrentFrameNumber;
580 }
581}
582
Vishnu Nair60356342018-11-13 13:00:45 -0800583Rect BufferLayer::getBufferSize(const State& s) const {
584 // If we have a sideband stream, or we are scaling the buffer then return the layer size since
585 // we cannot determine the buffer size.
586 if ((s.sidebandStream != nullptr) ||
587 (getEffectiveScalingMode() != NATIVE_WINDOW_SCALING_MODE_FREEZE)) {
588 return Rect(getActiveWidth(s), getActiveHeight(s));
589 }
590
chaviwd62d3062019-09-04 14:48:02 -0700591 if (mBufferInfo.mBuffer == nullptr) {
Vishnu Nair60356342018-11-13 13:00:45 -0800592 return Rect::INVALID_RECT;
593 }
594
chaviwd62d3062019-09-04 14:48:02 -0700595 uint32_t bufWidth = mBufferInfo.mBuffer->getWidth();
596 uint32_t bufHeight = mBufferInfo.mBuffer->getHeight();
Vishnu Nair60356342018-11-13 13:00:45 -0800597
598 // Undo any transformations on the buffer and return the result.
chaviw4244e032019-09-04 11:27:49 -0700599 if (mBufferInfo.mTransform & ui::Transform::ROT_90) {
Vishnu Nair60356342018-11-13 13:00:45 -0800600 std::swap(bufWidth, bufHeight);
601 }
602
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800603 if (getTransformToDisplayInverse()) {
Vishnu Nair60356342018-11-13 13:00:45 -0800604 uint32_t invTransform = DisplayDevice::getPrimaryDisplayOrientationTransform();
605 if (invTransform & ui::Transform::ROT_90) {
606 std::swap(bufWidth, bufHeight);
607 }
608 }
609
610 return Rect(bufWidth, bufHeight);
611}
612
Lloyd Piquefeb73d72018-12-04 17:23:44 -0800613std::shared_ptr<compositionengine::Layer> BufferLayer::getCompositionLayer() const {
614 return mCompositionLayer;
615}
616
Vishnu Nair4351ad52019-02-11 14:13:02 -0800617FloatRect BufferLayer::computeSourceBounds(const FloatRect& parentBounds) const {
618 const State& s(getDrawingState());
619
620 // If we have a sideband stream, or we are scaling the buffer then return the layer size since
621 // we cannot determine the buffer size.
622 if ((s.sidebandStream != nullptr) ||
623 (getEffectiveScalingMode() != NATIVE_WINDOW_SCALING_MODE_FREEZE)) {
624 return FloatRect(0, 0, getActiveWidth(s), getActiveHeight(s));
625 }
626
chaviwd62d3062019-09-04 14:48:02 -0700627 if (mBufferInfo.mBuffer == nullptr) {
Vishnu Nair4351ad52019-02-11 14:13:02 -0800628 return parentBounds;
629 }
630
chaviwd62d3062019-09-04 14:48:02 -0700631 uint32_t bufWidth = mBufferInfo.mBuffer->getWidth();
632 uint32_t bufHeight = mBufferInfo.mBuffer->getHeight();
Vishnu Nair4351ad52019-02-11 14:13:02 -0800633
634 // Undo any transformations on the buffer and return the result.
chaviw4244e032019-09-04 11:27:49 -0700635 if (mBufferInfo.mTransform & ui::Transform::ROT_90) {
Vishnu Nair4351ad52019-02-11 14:13:02 -0800636 std::swap(bufWidth, bufHeight);
637 }
638
639 if (getTransformToDisplayInverse()) {
640 uint32_t invTransform = DisplayDevice::getPrimaryDisplayOrientationTransform();
641 if (invTransform & ui::Transform::ROT_90) {
642 std::swap(bufWidth, bufHeight);
643 }
644 }
645
646 return FloatRect(0, 0, bufWidth, bufHeight);
647}
648
chaviw49a108c2019-08-12 11:23:06 -0700649void BufferLayer::latchAndReleaseBuffer() {
650 mRefreshPending = false;
651 if (hasReadyFrame()) {
652 bool ignored = false;
653 latchBuffer(ignored, systemTime(), 0 /* expectedPresentTime */);
654 }
655 releasePendingBuffer(systemTime());
656}
657
chaviw4244e032019-09-04 11:27:49 -0700658PixelFormat BufferLayer::getPixelFormat() const {
659 return mBufferInfo.mPixelFormat;
660}
661
662bool BufferLayer::getTransformToDisplayInverse() const {
663 return mBufferInfo.mTransformToDisplayInverse;
664}
665
666Rect BufferLayer::getBufferCrop() const {
667 // this is the crop rectangle that applies to the buffer
668 // itself (as opposed to the window)
669 if (!mBufferInfo.mCrop.isEmpty()) {
670 // if the buffer crop is defined, we use that
671 return mBufferInfo.mCrop;
chaviwd62d3062019-09-04 14:48:02 -0700672 } else if (mBufferInfo.mBuffer != nullptr) {
chaviw4244e032019-09-04 11:27:49 -0700673 // otherwise we use the whole buffer
chaviwd62d3062019-09-04 14:48:02 -0700674 return mBufferInfo.mBuffer->getBounds();
chaviw4244e032019-09-04 11:27:49 -0700675 } else {
676 // if we don't have a buffer yet, we use an empty/invalid crop
677 return Rect();
678 }
679}
680
681uint32_t BufferLayer::getBufferTransform() const {
682 return mBufferInfo.mTransform;
683}
684
685ui::Dataspace BufferLayer::getDataSpace() const {
686 return mBufferInfo.mDataspace;
687}
688
689ui::Dataspace BufferLayer::translateDataspace(ui::Dataspace dataspace) {
690 ui::Dataspace updatedDataspace = dataspace;
691 // translate legacy dataspaces to modern dataspaces
692 switch (dataspace) {
693 case ui::Dataspace::SRGB:
694 updatedDataspace = ui::Dataspace::V0_SRGB;
695 break;
696 case ui::Dataspace::SRGB_LINEAR:
697 updatedDataspace = ui::Dataspace::V0_SRGB_LINEAR;
698 break;
699 case ui::Dataspace::JFIF:
700 updatedDataspace = ui::Dataspace::V0_JFIF;
701 break;
702 case ui::Dataspace::BT601_625:
703 updatedDataspace = ui::Dataspace::V0_BT601_625;
704 break;
705 case ui::Dataspace::BT601_525:
706 updatedDataspace = ui::Dataspace::V0_BT601_525;
707 break;
708 case ui::Dataspace::BT709:
709 updatedDataspace = ui::Dataspace::V0_BT709;
710 break;
711 default:
712 break;
713 }
714
715 return updatedDataspace;
716}
717
chaviwd62d3062019-09-04 14:48:02 -0700718sp<GraphicBuffer> BufferLayer::getBuffer() const {
719 return mBufferInfo.mBuffer;
720}
721
chaviwf83ce182019-09-12 14:43:08 -0700722void BufferLayer::getDrawingTransformMatrix(bool filteringEnabled, float outMatrix[16]) {
723 GLConsumer::computeTransformMatrix(outMatrix, mBufferInfo.mBuffer, mBufferInfo.mCrop,
724 mBufferInfo.mTransform, filteringEnabled);
725}
726
David Sodman0c69cad2017-08-21 12:12:51 -0700727} // namespace android
728
729#if defined(__gl_h_)
730#error "don't include gl/gl.h in this file"
731#endif
732
733#if defined(__gl2_h_)
734#error "don't include gl2/gl2.h in this file"
735#endif