blob: 31462569e2837547302f06a4844d508605ffd3bd [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
Lloyd Pique42ab75e2018-09-12 20:46:03 -070059BufferLayer::BufferLayer(const LayerCreationArgs& args)
Lloyd Piquefeb73d72018-12-04 17:23:44 -080060 : Layer(args),
chaviwb4c6e582019-08-16 14:35:07 -070061 mTextureName(args.textureName),
Lloyd Piquefeb73d72018-12-04 17:23:44 -080062 mCompositionLayer{mFlinger->getCompositionEngine().createLayer(
63 compositionengine::LayerCreationArgs{this})} {
Dominik Laskowski87a07e42019-10-10 20:38:02 -070064 ALOGV("Creating Layer %s", getDebugName());
David Sodman0c69cad2017-08-21 12:12:51 -070065
Lloyd Pique42ab75e2018-09-12 20:46:03 -070066 mPremultipliedAlpha = !(args.flags & ISurfaceComposerClient::eNonPremultiplied);
David Sodman0c69cad2017-08-21 12:12:51 -070067
Lloyd Pique42ab75e2018-09-12 20:46:03 -070068 mPotentialCursor = args.flags & ISurfaceComposerClient::eCursorWindow;
69 mProtectedByApp = args.flags & ISurfaceComposerClient::eProtectedByApp;
David Sodman0c69cad2017-08-21 12:12:51 -070070}
71
72BufferLayer::~BufferLayer() {
chaviwb4c6e582019-08-16 14:35:07 -070073 if (!isClone()) {
74 // The original layer and the clone layer share the same texture. Therefore, only one of
75 // the layers, in this case the original layer, needs to handle the deletion. The original
76 // layer and the clone should be removed at the same time so there shouldn't be any issue
77 // with the clone layer trying to use the deleted texture.
78 mFlinger->deleteTextureAsync(mTextureName);
79 }
Yiwei Zhang1a88c402019-11-18 10:43:58 -080080 const int32_t layerId = getSequence();
81 mFlinger->mTimeStats->onDestroy(layerId);
82 mFlinger->mFrameTracer->onDestroy(layerId);
David Sodman0c69cad2017-08-21 12:12:51 -070083}
84
David Sodmaneb085e02017-10-05 18:49:04 -070085void BufferLayer::useSurfaceDamage() {
86 if (mFlinger->mForceFullDamage) {
87 surfaceDamageRegion = Region::INVALID_REGION;
88 } else {
chaviw4244e032019-09-04 11:27:49 -070089 surfaceDamageRegion = mBufferInfo.mSurfaceDamage;
David Sodmaneb085e02017-10-05 18:49:04 -070090 }
91}
92
93void BufferLayer::useEmptyDamage() {
94 surfaceDamageRegion.clear();
95}
96
Marissa Wallfd668622018-05-10 10:21:13 -070097bool BufferLayer::isOpaque(const Layer::State& s) const {
98 // if we don't have a buffer or sidebandStream yet, we're translucent regardless of the
99 // layer's opaque flag.
chaviwd62d3062019-09-04 14:48:02 -0700100 if ((mSidebandStream == nullptr) && (mBufferInfo.mBuffer == nullptr)) {
Marissa Wallfd668622018-05-10 10:21:13 -0700101 return false;
102 }
103
104 // if the layer has the opaque flag, then we're always opaque,
105 // otherwise we use the current buffer's format.
106 return ((s.flags & layer_state_t::eLayerOpaque) != 0) || getOpacityForFormat(getPixelFormat());
David Sodman0c69cad2017-08-21 12:12:51 -0700107}
108
109bool BufferLayer::isVisible() const {
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -0700110 return !isHiddenByPolicy() && getAlpha() > 0.0f &&
chaviwd62d3062019-09-04 14:48:02 -0700111 (mBufferInfo.mBuffer != nullptr || mSidebandStream != nullptr);
David Sodman0c69cad2017-08-21 12:12:51 -0700112}
113
114bool BufferLayer::isFixedSize() const {
115 return getEffectiveScalingMode() != NATIVE_WINDOW_SCALING_MODE_FREEZE;
116}
117
Lloyd Piquea83776c2019-01-29 18:42:32 -0800118bool BufferLayer::usesSourceCrop() const {
119 return true;
120}
121
David Sodman0c69cad2017-08-21 12:12:51 -0700122static constexpr mat4 inverseOrientation(uint32_t transform) {
David Sodman41fdfc92017-11-06 16:09:56 -0800123 const mat4 flipH(-1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1);
124 const mat4 flipV(1, 0, 0, 0, 0, -1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1);
125 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 -0700126 mat4 tr;
127
128 if (transform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
129 tr = tr * rot90;
130 }
131 if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_H) {
132 tr = tr * flipH;
133 }
134 if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_V) {
135 tr = tr * flipV;
136 }
137 return inverse(tr);
138}
139
Lloyd Piquef16688f2019-02-19 17:47:57 -0800140std::optional<renderengine::LayerSettings> BufferLayer::prepareClientComposition(
141 compositionengine::LayerFE::ClientCompositionTargetSettings& targetSettings) {
David Sodman0c69cad2017-08-21 12:12:51 -0700142 ATRACE_CALL();
Lloyd Piquef16688f2019-02-19 17:47:57 -0800143
144 auto result = Layer::prepareClientComposition(targetSettings);
145 if (!result) {
146 return result;
147 }
148
chaviwd62d3062019-09-04 14:48:02 -0700149 if (CC_UNLIKELY(mBufferInfo.mBuffer == 0)) {
David Sodman0c69cad2017-08-21 12:12:51 -0700150 // the texture has not been created yet, this Layer has
151 // in fact never been drawn into. This happens frequently with
152 // SurfaceView because the WindowManager can't know when the client
153 // has drawn the first time.
154
155 // If there is nothing under us, we paint the screen in black, otherwise
156 // we just skip this update.
157
158 // figure out if there is something below us
159 Region under;
160 bool finished = false;
161 mFlinger->mDrawingState.traverseInZOrder([&](Layer* layer) {
162 if (finished || layer == static_cast<BufferLayer const*>(this)) {
163 finished = true;
164 return;
165 }
Lloyd Piquea2468662019-03-07 21:31:06 -0800166
167 under.orSelf(layer->getScreenBounds());
David Sodman0c69cad2017-08-21 12:12:51 -0700168 });
169 // if not everything below us is covered, we plug the holes!
Lloyd Piquef16688f2019-02-19 17:47:57 -0800170 Region holes(targetSettings.clip.subtract(under));
David Sodman0c69cad2017-08-21 12:12:51 -0700171 if (!holes.isEmpty()) {
Lloyd Piquef16688f2019-02-19 17:47:57 -0800172 targetSettings.clearRegion.orSelf(holes);
David Sodman0c69cad2017-08-21 12:12:51 -0700173 }
Lloyd Piquef16688f2019-02-19 17:47:57 -0800174 return std::nullopt;
David Sodman0c69cad2017-08-21 12:12:51 -0700175 }
Lloyd Pique688abd42019-02-15 15:42:24 -0800176 bool blackOutLayer = (isProtected() && !targetSettings.supportsProtectedContent) ||
Lloyd Piquef16688f2019-02-19 17:47:57 -0800177 (isSecure() && !targetSettings.isSecure);
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000178 const State& s(getDrawingState());
Lloyd Piquef16688f2019-02-19 17:47:57 -0800179 auto& layer = *result;
David Sodman0c69cad2017-08-21 12:12:51 -0700180 if (!blackOutLayer) {
chaviwd62d3062019-09-04 14:48:02 -0700181 layer.source.buffer.buffer = mBufferInfo.mBuffer;
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000182 layer.source.buffer.isOpaque = isOpaque(s);
chaviwd62d3062019-09-04 14:48:02 -0700183 layer.source.buffer.fence = mBufferInfo.mFence;
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000184 layer.source.buffer.textureName = mTextureName;
185 layer.source.buffer.usePremultipliedAlpha = getPremultipledAlpha();
186 layer.source.buffer.isY410BT2020 = isHdrY410();
David Sodman0c69cad2017-08-21 12:12:51 -0700187 // TODO: we could be more subtle with isFixedSize()
Lloyd Piquef16688f2019-02-19 17:47:57 -0800188 const bool useFiltering = targetSettings.needsFiltering || mNeedsFiltering || isFixedSize();
David Sodman0c69cad2017-08-21 12:12:51 -0700189
190 // Query the texture matrix given our current filtering mode.
191 float textureMatrix[16];
chaviwf83ce182019-09-12 14:43:08 -0700192 getDrawingTransformMatrix(useFiltering, textureMatrix);
David Sodman0c69cad2017-08-21 12:12:51 -0700193
194 if (getTransformToDisplayInverse()) {
195 /*
196 * the code below applies the primary display's inverse transform to
197 * the texture transform
198 */
199 uint32_t transform = DisplayDevice::getPrimaryDisplayOrientationTransform();
200 mat4 tr = inverseOrientation(transform);
201
202 /**
203 * TODO(b/36727915): This is basically a hack.
204 *
205 * Ensure that regardless of the parent transformation,
206 * this buffer is always transformed from native display
207 * orientation to display orientation. For example, in the case
208 * of a camera where the buffer remains in native orientation,
209 * we want the pixels to always be upright.
210 */
211 sp<Layer> p = mDrawingParent.promote();
212 if (p != nullptr) {
213 const auto parentTransform = p->getTransform();
214 tr = tr * inverseOrientation(parentTransform.getOrientation());
215 }
216
217 // and finally apply it to the original texture matrix
218 const mat4 texTransform(mat4(static_cast<const float*>(textureMatrix)) * tr);
219 memcpy(textureMatrix, texTransform.asArray(), sizeof(textureMatrix));
220 }
221
Vishnu Nair4351ad52019-02-11 14:13:02 -0800222 const Rect win{getBounds()};
Marissa Wall290ad082019-03-06 13:23:47 -0800223 float bufferWidth = getBufferSize(s).getWidth();
224 float bufferHeight = getBufferSize(s).getHeight();
225
226 // BufferStateLayers can have a "buffer size" of [0, 0, -1, -1] when no display frame has
227 // been set and there is no parent layer bounds. In that case, the scale is meaningless so
228 // ignore them.
229 if (!getBufferSize(s).isValid()) {
230 bufferWidth = float(win.right) - float(win.left);
231 bufferHeight = float(win.bottom) - float(win.top);
232 }
David Sodman0c69cad2017-08-21 12:12:51 -0700233
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000234 const float scaleHeight = (float(win.bottom) - float(win.top)) / bufferHeight;
235 const float scaleWidth = (float(win.right) - float(win.left)) / bufferWidth;
236 const float translateY = float(win.top) / bufferHeight;
237 const float translateX = float(win.left) / bufferWidth;
238
239 // Flip y-coordinates because GLConsumer expects OpenGL convention.
240 mat4 tr = mat4::translate(vec4(.5, .5, 0, 1)) * mat4::scale(vec4(1, -1, 1, 1)) *
241 mat4::translate(vec4(-.5, -.5, 0, 1)) *
242 mat4::translate(vec4(translateX, translateY, 0, 1)) *
243 mat4::scale(vec4(scaleWidth, scaleHeight, 1.0, 1.0));
244
245 layer.source.buffer.useTextureFiltering = useFiltering;
246 layer.source.buffer.textureTransform = mat4(static_cast<const float*>(textureMatrix)) * tr;
David Sodman0c69cad2017-08-21 12:12:51 -0700247 } else {
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000248 // If layer is blacked out, force alpha to 1 so that we draw a black color
249 // layer.
250 layer.source.buffer.buffer = nullptr;
251 layer.alpha = 1.0;
David Sodman0c69cad2017-08-21 12:12:51 -0700252 }
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000253
Lloyd Piquef16688f2019-02-19 17:47:57 -0800254 return result;
David Sodman0c69cad2017-08-21 12:12:51 -0700255}
256
Marissa Wallfd668622018-05-10 10:21:13 -0700257bool BufferLayer::isHdrY410() const {
258 // pixel format is HDR Y410 masquerading as RGBA_1010102
chaviw4244e032019-09-04 11:27:49 -0700259 return (mBufferInfo.mDataspace == ui::Dataspace::BT2020_ITU_PQ &&
260 mBufferInfo.mApi == NATIVE_WINDOW_API_MEDIA &&
chaviwd62d3062019-09-04 14:48:02 -0700261 mBufferInfo.mBuffer->getPixelFormat() == HAL_PIXEL_FORMAT_RGBA_1010102);
David Sodmaneb085e02017-10-05 18:49:04 -0700262}
263
Lloyd Piquef5275482019-01-29 18:42:42 -0800264void BufferLayer::latchPerFrameState(
265 compositionengine::LayerFECompositionState& compositionState) const {
266 Layer::latchPerFrameState(compositionState);
David Sodman0c69cad2017-08-21 12:12:51 -0700267
268 // Sideband layers
Lloyd Piquef5275482019-01-29 18:42:42 -0800269 if (compositionState.sidebandStream.get()) {
270 compositionState.compositionType = Hwc2::IComposerClient::Composition::SIDEBAND;
David Sodman15094112018-10-11 09:39:37 -0700271 } else {
Lloyd Piquef5275482019-01-29 18:42:42 -0800272 // Normal buffer layers
chaviw4244e032019-09-04 11:27:49 -0700273 compositionState.hdrMetadata = mBufferInfo.mHdrMetadata;
Lloyd Piquef5275482019-01-29 18:42:42 -0800274 compositionState.compositionType = mPotentialCursor
275 ? Hwc2::IComposerClient::Composition::CURSOR
276 : Hwc2::IComposerClient::Composition::DEVICE;
David Sodman0c69cad2017-08-21 12:12:51 -0700277 }
David Sodman0c69cad2017-08-21 12:12:51 -0700278}
279
Marissa Wallfd668622018-05-10 10:21:13 -0700280bool BufferLayer::onPreComposition(nsecs_t refreshStartTime) {
chaviwd62d3062019-09-04 14:48:02 -0700281 if (mBufferInfo.mBuffer != nullptr) {
Marissa Wallfd668622018-05-10 10:21:13 -0700282 Mutex::Autolock lock(mFrameEventHistoryMutex);
283 mFrameEventHistory.addPreComposition(mCurrentFrameNumber, refreshStartTime);
David Sodman0c69cad2017-08-21 12:12:51 -0700284 }
Marissa Wallfd668622018-05-10 10:21:13 -0700285 mRefreshPending = false;
286 return hasReadyFrame();
David Sodman0c69cad2017-08-21 12:12:51 -0700287}
288
Dominik Laskowski075d3172018-05-24 15:50:06 -0700289bool BufferLayer::onPostComposition(const std::optional<DisplayId>& displayId,
290 const std::shared_ptr<FenceTime>& glDoneFence,
Marissa Wallfd668622018-05-10 10:21:13 -0700291 const std::shared_ptr<FenceTime>& presentFence,
292 const CompositorTiming& compositorTiming) {
293 // mFrameLatencyNeeded is true when a new frame was latched for the
294 // composition.
chaviw74b03172019-08-19 11:09:03 -0700295 if (!mBufferInfo.mFrameLatencyNeeded) return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700296
297 // Update mFrameEventHistory.
Dan Stoza436ccf32018-06-21 12:10:12 -0700298 {
Marissa Wallfd668622018-05-10 10:21:13 -0700299 Mutex::Autolock lock(mFrameEventHistoryMutex);
300 mFrameEventHistory.addPostComposition(mCurrentFrameNumber, glDoneFence, presentFence,
301 compositorTiming);
David Sodman0c69cad2017-08-21 12:12:51 -0700302 }
303
Marissa Wallfd668622018-05-10 10:21:13 -0700304 // Update mFrameTracker.
chaviw4244e032019-09-04 11:27:49 -0700305 nsecs_t desiredPresentTime = mBufferInfo.mDesiredPresentTime;
Marissa Wallfd668622018-05-10 10:21:13 -0700306 mFrameTracker.setDesiredPresentTime(desiredPresentTime);
307
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800308 const int32_t layerId = getSequence();
309 mFlinger->mTimeStats->setDesiredTime(layerId, mCurrentFrameNumber, desiredPresentTime);
Marissa Wallfd668622018-05-10 10:21:13 -0700310
chaviw4244e032019-09-04 11:27:49 -0700311 std::shared_ptr<FenceTime> frameReadyFence = mBufferInfo.mFenceTime;
Marissa Wallfd668622018-05-10 10:21:13 -0700312 if (frameReadyFence->isValid()) {
313 mFrameTracker.setFrameReadyFence(std::move(frameReadyFence));
314 } else {
315 // There was no fence for this frame, so assume that it was ready
316 // to be presented at the desired present time.
317 mFrameTracker.setFrameReadyTime(desiredPresentTime);
Dominik Laskowski45de9bd2018-06-11 17:44:10 -0700318 }
Marissa Wallfd668622018-05-10 10:21:13 -0700319
320 if (presentFence->isValid()) {
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800321 mFlinger->mTimeStats->setPresentFence(layerId, mCurrentFrameNumber, presentFence);
322 mFlinger->mFrameTracer->traceFence(layerId, getCurrentBufferId(), mCurrentFrameNumber,
Mikael Pessa90092f42019-08-26 17:22:04 -0700323 presentFence, FrameTracer::FrameEvent::PRESENT_FENCE);
Marissa Wallfd668622018-05-10 10:21:13 -0700324 mFrameTracker.setActualPresentFence(std::shared_ptr<FenceTime>(presentFence));
Dominik Laskowski075d3172018-05-24 15:50:06 -0700325 } else if (displayId && mFlinger->getHwComposer().isConnected(*displayId)) {
Marissa Wallfd668622018-05-10 10:21:13 -0700326 // The HWC doesn't support present fences, so use the refresh
327 // timestamp instead.
Dominik Laskowski075d3172018-05-24 15:50:06 -0700328 const nsecs_t actualPresentTime = mFlinger->getHwComposer().getRefreshTimestamp(*displayId);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800329 mFlinger->mTimeStats->setPresentTime(layerId, mCurrentFrameNumber, actualPresentTime);
330 mFlinger->mFrameTracer->traceTimestamp(layerId, getCurrentBufferId(), mCurrentFrameNumber,
Mikael Pessa90092f42019-08-26 17:22:04 -0700331 actualPresentTime,
332 FrameTracer::FrameEvent::PRESENT_FENCE);
Marissa Wallfd668622018-05-10 10:21:13 -0700333 mFrameTracker.setActualPresentTime(actualPresentTime);
334 }
335
336 mFrameTracker.advanceFrame();
chaviw74b03172019-08-19 11:09:03 -0700337 mBufferInfo.mFrameLatencyNeeded = false;
Marissa Wallfd668622018-05-10 10:21:13 -0700338 return true;
David Sodman0c69cad2017-08-21 12:12:51 -0700339}
340
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700341bool BufferLayer::latchBuffer(bool& recomputeVisibleRegions, nsecs_t latchTime,
342 nsecs_t expectedPresentTime) {
Marissa Wallfd668622018-05-10 10:21:13 -0700343 ATRACE_CALL();
David Sodman0c69cad2017-08-21 12:12:51 -0700344
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800345 bool refreshRequired = latchSidebandStream(recomputeVisibleRegions);
David Sodman0c69cad2017-08-21 12:12:51 -0700346
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800347 if (refreshRequired) {
348 return refreshRequired;
David Sodman0c69cad2017-08-21 12:12:51 -0700349 }
350
Marissa Wallfd668622018-05-10 10:21:13 -0700351 if (!hasReadyFrame()) {
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800352 return false;
David Sodman0c69cad2017-08-21 12:12:51 -0700353 }
David Sodman0c69cad2017-08-21 12:12:51 -0700354
Marissa Wallfd668622018-05-10 10:21:13 -0700355 // if we've already called updateTexImage() without going through
356 // a composition step, we have to skip this layer at this point
357 // because we cannot call updateTeximage() without a corresponding
358 // compositionComplete() call.
359 // we'll trigger an update in onPreComposition().
360 if (mRefreshPending) {
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800361 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700362 }
363
364 // If the head buffer's acquire fence hasn't signaled yet, return and
365 // try again later
366 if (!fenceHasSignaled()) {
Ady Abraham09bd3922019-04-08 10:44:56 -0700367 ATRACE_NAME("!fenceHasSignaled()");
David Sodman0c69cad2017-08-21 12:12:51 -0700368 mFlinger->signalLayerUpdate();
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800369 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700370 }
371
372 // Capture the old state of the layer for comparisons later
373 const State& s(getDrawingState());
374 const bool oldOpacity = isOpaque(s);
chaviwd62d3062019-09-04 14:48:02 -0700375
376 BufferInfo oldBufferInfo = mBufferInfo;
Marissa Wallfd668622018-05-10 10:21:13 -0700377
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700378 if (!allTransactionsSignaled(expectedPresentTime)) {
Marissa Wallebb486e2019-05-15 14:08:08 -0700379 mFlinger->setTransactionFlags(eTraversalNeeded);
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800380 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700381 }
382
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700383 status_t err = updateTexImage(recomputeVisibleRegions, latchTime, expectedPresentTime);
Marissa Wallfd668622018-05-10 10:21:13 -0700384 if (err != NO_ERROR) {
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800385 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700386 }
387
388 err = updateActiveBuffer();
389 if (err != NO_ERROR) {
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800390 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700391 }
392
Marissa Wallfd668622018-05-10 10:21:13 -0700393 err = updateFrameNumber(latchTime);
394 if (err != NO_ERROR) {
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800395 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700396 }
397
chaviw4244e032019-09-04 11:27:49 -0700398 gatherBufferInfo();
399
Marissa Wallfd668622018-05-10 10:21:13 -0700400 mRefreshPending = true;
chaviw74b03172019-08-19 11:09:03 -0700401 mBufferInfo.mFrameLatencyNeeded = true;
chaviwd62d3062019-09-04 14:48:02 -0700402 if (oldBufferInfo.mBuffer == nullptr) {
Marissa Wallfd668622018-05-10 10:21:13 -0700403 // the first time we receive a buffer, we need to trigger a
404 // geometry invalidation.
405 recomputeVisibleRegions = true;
406 }
407
chaviw4244e032019-09-04 11:27:49 -0700408 if ((mBufferInfo.mCrop != oldBufferInfo.mCrop) ||
409 (mBufferInfo.mTransform != oldBufferInfo.mTransform) ||
410 (mBufferInfo.mScaleMode != oldBufferInfo.mScaleMode) ||
411 (mBufferInfo.mTransformToDisplayInverse != oldBufferInfo.mTransformToDisplayInverse)) {
Marissa Wallfd668622018-05-10 10:21:13 -0700412 recomputeVisibleRegions = true;
413 }
414
chaviwd62d3062019-09-04 14:48:02 -0700415 if (oldBufferInfo.mBuffer != nullptr) {
416 uint32_t bufWidth = mBufferInfo.mBuffer->getWidth();
417 uint32_t bufHeight = mBufferInfo.mBuffer->getHeight();
418 if (bufWidth != uint32_t(oldBufferInfo.mBuffer->width) ||
419 bufHeight != uint32_t(oldBufferInfo.mBuffer->height)) {
Marissa Wallfd668622018-05-10 10:21:13 -0700420 recomputeVisibleRegions = true;
421 }
422 }
423
424 if (oldOpacity != isOpaque(s)) {
425 recomputeVisibleRegions = true;
426 }
427
428 // Remove any sync points corresponding to the buffer which was just
429 // latched
430 {
431 Mutex::Autolock lock(mLocalSyncPointMutex);
432 auto point = mLocalSyncPoints.begin();
433 while (point != mLocalSyncPoints.end()) {
434 if (!(*point)->frameIsAvailable() || !(*point)->transactionIsApplied()) {
435 // This sync point must have been added since we started
436 // latching. Don't drop it yet.
437 ++point;
438 continue;
439 }
440
441 if ((*point)->getFrameNumber() <= mCurrentFrameNumber) {
Alec Mourie60041e2019-06-14 18:59:51 -0700442 std::stringstream ss;
443 ss << "Dropping sync point " << (*point)->getFrameNumber();
444 ATRACE_NAME(ss.str().c_str());
Marissa Wallfd668622018-05-10 10:21:13 -0700445 point = mLocalSyncPoints.erase(point);
446 } else {
447 ++point;
448 }
449 }
450 }
451
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800452 return true;
Marissa Wallfd668622018-05-10 10:21:13 -0700453}
454
455// transaction
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700456void BufferLayer::notifyAvailableFrames(nsecs_t expectedPresentTime) {
457 const auto headFrameNumber = getHeadFrameNumber(expectedPresentTime);
Ady Abrahamcd1580c2019-04-29 15:40:03 -0700458 const bool headFenceSignaled = fenceHasSignaled();
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700459 const bool presentTimeIsCurrent = framePresentTimeIsCurrent(expectedPresentTime);
Marissa Wallfd668622018-05-10 10:21:13 -0700460 Mutex::Autolock lock(mLocalSyncPointMutex);
461 for (auto& point : mLocalSyncPoints) {
Ady Abrahamcd1580c2019-04-29 15:40:03 -0700462 if (headFrameNumber >= point->getFrameNumber() && headFenceSignaled &&
463 presentTimeIsCurrent) {
Marissa Wallfd668622018-05-10 10:21:13 -0700464 point->setFrameAvailable();
chaviw43cb3cb2019-05-31 15:23:41 -0700465 sp<Layer> requestedSyncLayer = point->getRequestedSyncLayer();
466 if (requestedSyncLayer) {
467 // Need to update the transaction flag to ensure the layer's pending transaction
468 // gets applied.
469 requestedSyncLayer->setTransactionFlags(eTransactionNeeded);
470 }
Marissa Wallfd668622018-05-10 10:21:13 -0700471 }
David Sodman0c69cad2017-08-21 12:12:51 -0700472 }
473}
474
Marissa Wallfd668622018-05-10 10:21:13 -0700475bool BufferLayer::hasReadyFrame() const {
Marissa Wall024a1912018-08-13 13:55:35 -0700476 return hasFrameUpdate() || getSidebandStreamChanged() || getAutoRefresh();
Marissa Wallfd668622018-05-10 10:21:13 -0700477}
478
479uint32_t BufferLayer::getEffectiveScalingMode() const {
480 if (mOverrideScalingMode >= 0) {
481 return mOverrideScalingMode;
482 }
483
chaviw4244e032019-09-04 11:27:49 -0700484 return mBufferInfo.mScaleMode;
Marissa Wallfd668622018-05-10 10:21:13 -0700485}
486
487bool BufferLayer::isProtected() const {
chaviwd62d3062019-09-04 14:48:02 -0700488 const sp<GraphicBuffer>& buffer(mBufferInfo.mBuffer);
Marissa Wallfd668622018-05-10 10:21:13 -0700489 return (buffer != 0) && (buffer->getUsage() & GRALLOC_USAGE_PROTECTED);
490}
491
492bool BufferLayer::latchUnsignaledBuffers() {
493 static bool propertyLoaded = false;
494 static bool latch = false;
495 static std::mutex mutex;
496 std::lock_guard<std::mutex> lock(mutex);
497 if (!propertyLoaded) {
498 char value[PROPERTY_VALUE_MAX] = {};
499 property_get("debug.sf.latch_unsignaled", value, "0");
500 latch = atoi(value);
501 propertyLoaded = true;
502 }
503 return latch;
504}
505
506// h/w composer set-up
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700507bool BufferLayer::allTransactionsSignaled(nsecs_t expectedPresentTime) {
508 const auto headFrameNumber = getHeadFrameNumber(expectedPresentTime);
Marissa Wallfd668622018-05-10 10:21:13 -0700509 bool matchingFramesFound = false;
510 bool allTransactionsApplied = true;
511 Mutex::Autolock lock(mLocalSyncPointMutex);
512
513 for (auto& point : mLocalSyncPoints) {
514 if (point->getFrameNumber() > headFrameNumber) {
515 break;
516 }
517 matchingFramesFound = true;
518
519 if (!point->frameIsAvailable()) {
520 // We haven't notified the remote layer that the frame for
521 // this point is available yet. Notify it now, and then
522 // abort this attempt to latch.
523 point->setFrameAvailable();
524 allTransactionsApplied = false;
525 break;
526 }
527
528 allTransactionsApplied = allTransactionsApplied && point->transactionIsApplied();
529 }
530 return !matchingFramesFound || allTransactionsApplied;
David Sodman0c69cad2017-08-21 12:12:51 -0700531}
532
533// As documented in libhardware header, formats in the range
534// 0x100 - 0x1FF are specific to the HAL implementation, and
535// are known to have no alpha channel
536// TODO: move definition for device-specific range into
537// hardware.h, instead of using hard-coded values here.
538#define HARDWARE_IS_DEVICE_FORMAT(f) ((f) >= 0x100 && (f) <= 0x1FF)
539
540bool BufferLayer::getOpacityForFormat(uint32_t format) {
541 if (HARDWARE_IS_DEVICE_FORMAT(format)) {
542 return true;
543 }
544 switch (format) {
545 case HAL_PIXEL_FORMAT_RGBA_8888:
546 case HAL_PIXEL_FORMAT_BGRA_8888:
547 case HAL_PIXEL_FORMAT_RGBA_FP16:
548 case HAL_PIXEL_FORMAT_RGBA_1010102:
549 return false;
550 }
551 // in all other case, we have no blending (also for unknown formats)
552 return true;
553}
554
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800555bool BufferLayer::needsFiltering(const sp<const DisplayDevice>& displayDevice) const {
Lloyd Piquef16688f2019-02-19 17:47:57 -0800556 // If we are not capturing based on the state of a known display device,
557 // just return false.
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800558 if (displayDevice == nullptr) {
Lloyd Piquef16688f2019-02-19 17:47:57 -0800559 return false;
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800560 }
561
562 const auto outputLayer = findOutputLayerForDisplay(displayDevice);
563 if (outputLayer == nullptr) {
Lloyd Piquef16688f2019-02-19 17:47:57 -0800564 return false;
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800565 }
566
Lloyd Piquef16688f2019-02-19 17:47:57 -0800567 // We need filtering if the sourceCrop rectangle size does not match the
568 // displayframe rectangle size (not a 1:1 render)
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800569 const auto& compositionState = outputLayer->getState();
570 const auto displayFrame = compositionState.displayFrame;
571 const auto sourceCrop = compositionState.sourceCrop;
Lloyd Piquef16688f2019-02-19 17:47:57 -0800572 return sourceCrop.getHeight() != displayFrame.getHeight() ||
Peiyong Linc2020ca2019-01-10 11:36:12 -0800573 sourceCrop.getWidth() != displayFrame.getWidth();
Chia-I Wu692e0832018-06-05 15:46:58 -0700574}
575
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700576uint64_t BufferLayer::getHeadFrameNumber(nsecs_t expectedPresentTime) const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800577 if (hasFrameUpdate()) {
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700578 return getFrameNumber(expectedPresentTime);
David Sodman0c69cad2017-08-21 12:12:51 -0700579 } else {
580 return mCurrentFrameNumber;
581 }
582}
583
Vishnu Nair60356342018-11-13 13:00:45 -0800584Rect BufferLayer::getBufferSize(const State& s) const {
585 // If we have a sideband stream, or we are scaling the buffer then return the layer size since
586 // we cannot determine the buffer size.
587 if ((s.sidebandStream != nullptr) ||
588 (getEffectiveScalingMode() != NATIVE_WINDOW_SCALING_MODE_FREEZE)) {
589 return Rect(getActiveWidth(s), getActiveHeight(s));
590 }
591
chaviwd62d3062019-09-04 14:48:02 -0700592 if (mBufferInfo.mBuffer == nullptr) {
Vishnu Nair60356342018-11-13 13:00:45 -0800593 return Rect::INVALID_RECT;
594 }
595
chaviwd62d3062019-09-04 14:48:02 -0700596 uint32_t bufWidth = mBufferInfo.mBuffer->getWidth();
597 uint32_t bufHeight = mBufferInfo.mBuffer->getHeight();
Vishnu Nair60356342018-11-13 13:00:45 -0800598
599 // Undo any transformations on the buffer and return the result.
chaviw4244e032019-09-04 11:27:49 -0700600 if (mBufferInfo.mTransform & ui::Transform::ROT_90) {
Vishnu Nair60356342018-11-13 13:00:45 -0800601 std::swap(bufWidth, bufHeight);
602 }
603
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800604 if (getTransformToDisplayInverse()) {
Vishnu Nair60356342018-11-13 13:00:45 -0800605 uint32_t invTransform = DisplayDevice::getPrimaryDisplayOrientationTransform();
606 if (invTransform & ui::Transform::ROT_90) {
607 std::swap(bufWidth, bufHeight);
608 }
609 }
610
611 return Rect(bufWidth, bufHeight);
612}
613
Lloyd Piquefeb73d72018-12-04 17:23:44 -0800614std::shared_ptr<compositionengine::Layer> BufferLayer::getCompositionLayer() const {
615 return mCompositionLayer;
616}
617
Vishnu Nair4351ad52019-02-11 14:13:02 -0800618FloatRect BufferLayer::computeSourceBounds(const FloatRect& parentBounds) const {
619 const State& s(getDrawingState());
620
621 // If we have a sideband stream, or we are scaling the buffer then return the layer size since
622 // we cannot determine the buffer size.
623 if ((s.sidebandStream != nullptr) ||
624 (getEffectiveScalingMode() != NATIVE_WINDOW_SCALING_MODE_FREEZE)) {
625 return FloatRect(0, 0, getActiveWidth(s), getActiveHeight(s));
626 }
627
chaviwd62d3062019-09-04 14:48:02 -0700628 if (mBufferInfo.mBuffer == nullptr) {
Vishnu Nair4351ad52019-02-11 14:13:02 -0800629 return parentBounds;
630 }
631
chaviwd62d3062019-09-04 14:48:02 -0700632 uint32_t bufWidth = mBufferInfo.mBuffer->getWidth();
633 uint32_t bufHeight = mBufferInfo.mBuffer->getHeight();
Vishnu Nair4351ad52019-02-11 14:13:02 -0800634
635 // Undo any transformations on the buffer and return the result.
chaviw4244e032019-09-04 11:27:49 -0700636 if (mBufferInfo.mTransform & ui::Transform::ROT_90) {
Vishnu Nair4351ad52019-02-11 14:13:02 -0800637 std::swap(bufWidth, bufHeight);
638 }
639
640 if (getTransformToDisplayInverse()) {
641 uint32_t invTransform = DisplayDevice::getPrimaryDisplayOrientationTransform();
642 if (invTransform & ui::Transform::ROT_90) {
643 std::swap(bufWidth, bufHeight);
644 }
645 }
646
647 return FloatRect(0, 0, bufWidth, bufHeight);
648}
649
chaviw49a108c2019-08-12 11:23:06 -0700650void BufferLayer::latchAndReleaseBuffer() {
651 mRefreshPending = false;
652 if (hasReadyFrame()) {
653 bool ignored = false;
654 latchBuffer(ignored, systemTime(), 0 /* expectedPresentTime */);
655 }
656 releasePendingBuffer(systemTime());
657}
658
chaviw4244e032019-09-04 11:27:49 -0700659PixelFormat BufferLayer::getPixelFormat() const {
660 return mBufferInfo.mPixelFormat;
661}
662
663bool BufferLayer::getTransformToDisplayInverse() const {
664 return mBufferInfo.mTransformToDisplayInverse;
665}
666
667Rect BufferLayer::getBufferCrop() const {
668 // this is the crop rectangle that applies to the buffer
669 // itself (as opposed to the window)
670 if (!mBufferInfo.mCrop.isEmpty()) {
671 // if the buffer crop is defined, we use that
672 return mBufferInfo.mCrop;
chaviwd62d3062019-09-04 14:48:02 -0700673 } else if (mBufferInfo.mBuffer != nullptr) {
chaviw4244e032019-09-04 11:27:49 -0700674 // otherwise we use the whole buffer
chaviwd62d3062019-09-04 14:48:02 -0700675 return mBufferInfo.mBuffer->getBounds();
chaviw4244e032019-09-04 11:27:49 -0700676 } else {
677 // if we don't have a buffer yet, we use an empty/invalid crop
678 return Rect();
679 }
680}
681
682uint32_t BufferLayer::getBufferTransform() const {
683 return mBufferInfo.mTransform;
684}
685
686ui::Dataspace BufferLayer::getDataSpace() const {
687 return mBufferInfo.mDataspace;
688}
689
690ui::Dataspace BufferLayer::translateDataspace(ui::Dataspace dataspace) {
691 ui::Dataspace updatedDataspace = dataspace;
692 // translate legacy dataspaces to modern dataspaces
693 switch (dataspace) {
694 case ui::Dataspace::SRGB:
695 updatedDataspace = ui::Dataspace::V0_SRGB;
696 break;
697 case ui::Dataspace::SRGB_LINEAR:
698 updatedDataspace = ui::Dataspace::V0_SRGB_LINEAR;
699 break;
700 case ui::Dataspace::JFIF:
701 updatedDataspace = ui::Dataspace::V0_JFIF;
702 break;
703 case ui::Dataspace::BT601_625:
704 updatedDataspace = ui::Dataspace::V0_BT601_625;
705 break;
706 case ui::Dataspace::BT601_525:
707 updatedDataspace = ui::Dataspace::V0_BT601_525;
708 break;
709 case ui::Dataspace::BT709:
710 updatedDataspace = ui::Dataspace::V0_BT709;
711 break;
712 default:
713 break;
714 }
715
716 return updatedDataspace;
717}
718
chaviwd62d3062019-09-04 14:48:02 -0700719sp<GraphicBuffer> BufferLayer::getBuffer() const {
720 return mBufferInfo.mBuffer;
721}
722
chaviwf83ce182019-09-12 14:43:08 -0700723void BufferLayer::getDrawingTransformMatrix(bool filteringEnabled, float outMatrix[16]) {
724 GLConsumer::computeTransformMatrix(outMatrix, mBufferInfo.mBuffer, mBufferInfo.mCrop,
725 mBufferInfo.mTransform, filteringEnabled);
726}
727
chaviwb4c6e582019-08-16 14:35:07 -0700728void BufferLayer::setInitialValuesForClone(const sp<Layer>& clonedFrom) {
729 Layer::setInitialValuesForClone(clonedFrom);
730
731 sp<BufferLayer> bufferClonedFrom = static_cast<BufferLayer*>(clonedFrom.get());
732 mPremultipliedAlpha = bufferClonedFrom->mPremultipliedAlpha;
733 mPotentialCursor = bufferClonedFrom->mPotentialCursor;
734 mProtectedByApp = bufferClonedFrom->mProtectedByApp;
chaviw74b03172019-08-19 11:09:03 -0700735
736 updateCloneBufferInfo();
737}
738
739void BufferLayer::updateCloneBufferInfo() {
740 if (!isClone() || !isClonedFromAlive()) {
741 return;
742 }
743
744 sp<BufferLayer> clonedFrom = static_cast<BufferLayer*>(getClonedFrom().get());
745 mBufferInfo = clonedFrom->mBufferInfo;
746 mSidebandStream = clonedFrom->mSidebandStream;
747 surfaceDamageRegion = clonedFrom->surfaceDamageRegion;
748 mCurrentFrameNumber = clonedFrom->mCurrentFrameNumber.load();
749 mPreviousFrameNumber = clonedFrom->mPreviousFrameNumber;
750
751 // After buffer info is updated, the drawingState from the real layer needs to be copied into
752 // the cloned. This is because some properties of drawingState can change when latchBuffer is
753 // called. However, copying the drawingState would also overwrite the cloned layer's relatives.
754 // Therefore, temporarily store the relatives so they can be set in the cloned drawingState
755 // again.
756 wp<Layer> tmpZOrderRelativeOf = mDrawingState.zOrderRelativeOf;
757 SortedVector<wp<Layer>> tmpZOrderRelatives = mDrawingState.zOrderRelatives;
758 mDrawingState = clonedFrom->mDrawingState;
759 // TODO: (b/140756730) Ignore input for now since InputDispatcher doesn't support multiple
760 // InputWindows per client token yet.
761 mDrawingState.inputInfo.token = nullptr;
762 mDrawingState.zOrderRelativeOf = tmpZOrderRelativeOf;
763 mDrawingState.zOrderRelatives = tmpZOrderRelatives;
chaviwb4c6e582019-08-16 14:35:07 -0700764}
765
David Sodman0c69cad2017-08-21 12:12:51 -0700766} // namespace android
767
768#if defined(__gl_h_)
769#error "don't include gl/gl.h in this file"
770#endif
771
772#if defined(__gl2_h_)
773#error "don't include gl2/gl2.h in this file"
774#endif