blob: fba235d3976d839aa8d6481234e75cd12387a264 [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),
61 mTextureName(args.flinger->getNewTexture()),
62 mCompositionLayer{mFlinger->getCompositionEngine().createLayer(
63 compositionengine::LayerCreationArgs{this})} {
Lloyd Pique42ab75e2018-09-12 20:46:03 -070064 ALOGV("Creating Layer %s", args.name.string());
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() {
David Sodman0c69cad2017-08-21 12:12:51 -070073 mFlinger->deleteTextureAsync(mTextureName);
Mikael Pessa90092f42019-08-26 17:22:04 -070074 const int32_t layerID = getSequence();
75 mFlinger->mTimeStats->onDestroy(layerID);
76 mFlinger->mFrameTracer->onDestroy(layerID);
David Sodman0c69cad2017-08-21 12:12:51 -070077}
78
David Sodmaneb085e02017-10-05 18:49:04 -070079void BufferLayer::useSurfaceDamage() {
80 if (mFlinger->mForceFullDamage) {
81 surfaceDamageRegion = Region::INVALID_REGION;
82 } else {
chaviw4244e032019-09-04 11:27:49 -070083 surfaceDamageRegion = mBufferInfo.mSurfaceDamage;
David Sodmaneb085e02017-10-05 18:49:04 -070084 }
85}
86
87void BufferLayer::useEmptyDamage() {
88 surfaceDamageRegion.clear();
89}
90
Marissa Wallfd668622018-05-10 10:21:13 -070091bool BufferLayer::isOpaque(const Layer::State& s) const {
92 // if we don't have a buffer or sidebandStream yet, we're translucent regardless of the
93 // layer's opaque flag.
chaviwd62d3062019-09-04 14:48:02 -070094 if ((mSidebandStream == nullptr) && (mBufferInfo.mBuffer == nullptr)) {
Marissa Wallfd668622018-05-10 10:21:13 -070095 return false;
96 }
97
98 // if the layer has the opaque flag, then we're always opaque,
99 // otherwise we use the current buffer's format.
100 return ((s.flags & layer_state_t::eLayerOpaque) != 0) || getOpacityForFormat(getPixelFormat());
David Sodman0c69cad2017-08-21 12:12:51 -0700101}
102
103bool BufferLayer::isVisible() const {
Ady Abrahama315ce72019-04-24 14:35:20 -0700104 bool visible = !(isHiddenByPolicy()) && getAlpha() > 0.0f &&
chaviwd62d3062019-09-04 14:48:02 -0700105 (mBufferInfo.mBuffer != nullptr || mSidebandStream != nullptr);
Ady Abrahama315ce72019-04-24 14:35:20 -0700106 mFlinger->mScheduler->setLayerVisibility(mSchedulerLayerHandle, visible);
107
108 return visible;
David Sodman0c69cad2017-08-21 12:12:51 -0700109}
110
111bool BufferLayer::isFixedSize() const {
112 return getEffectiveScalingMode() != NATIVE_WINDOW_SCALING_MODE_FREEZE;
113}
114
Lloyd Piquea83776c2019-01-29 18:42:32 -0800115bool BufferLayer::usesSourceCrop() const {
116 return true;
117}
118
David Sodman0c69cad2017-08-21 12:12:51 -0700119static constexpr mat4 inverseOrientation(uint32_t transform) {
David Sodman41fdfc92017-11-06 16:09:56 -0800120 const mat4 flipH(-1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1);
121 const mat4 flipV(1, 0, 0, 0, 0, -1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1);
122 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 -0700123 mat4 tr;
124
125 if (transform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
126 tr = tr * rot90;
127 }
128 if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_H) {
129 tr = tr * flipH;
130 }
131 if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_V) {
132 tr = tr * flipV;
133 }
134 return inverse(tr);
135}
136
Lloyd Piquef16688f2019-02-19 17:47:57 -0800137std::optional<renderengine::LayerSettings> BufferLayer::prepareClientComposition(
138 compositionengine::LayerFE::ClientCompositionTargetSettings& targetSettings) {
David Sodman0c69cad2017-08-21 12:12:51 -0700139 ATRACE_CALL();
Lloyd Piquef16688f2019-02-19 17:47:57 -0800140
141 auto result = Layer::prepareClientComposition(targetSettings);
142 if (!result) {
143 return result;
144 }
145
chaviwd62d3062019-09-04 14:48:02 -0700146 if (CC_UNLIKELY(mBufferInfo.mBuffer == 0)) {
David Sodman0c69cad2017-08-21 12:12:51 -0700147 // the texture has not been created yet, this Layer has
148 // in fact never been drawn into. This happens frequently with
149 // SurfaceView because the WindowManager can't know when the client
150 // has drawn the first time.
151
152 // If there is nothing under us, we paint the screen in black, otherwise
153 // we just skip this update.
154
155 // figure out if there is something below us
156 Region under;
157 bool finished = false;
158 mFlinger->mDrawingState.traverseInZOrder([&](Layer* layer) {
159 if (finished || layer == static_cast<BufferLayer const*>(this)) {
160 finished = true;
161 return;
162 }
Lloyd Piquea2468662019-03-07 21:31:06 -0800163
164 under.orSelf(layer->getScreenBounds());
David Sodman0c69cad2017-08-21 12:12:51 -0700165 });
166 // if not everything below us is covered, we plug the holes!
Lloyd Piquef16688f2019-02-19 17:47:57 -0800167 Region holes(targetSettings.clip.subtract(under));
David Sodman0c69cad2017-08-21 12:12:51 -0700168 if (!holes.isEmpty()) {
Lloyd Piquef16688f2019-02-19 17:47:57 -0800169 targetSettings.clearRegion.orSelf(holes);
David Sodman0c69cad2017-08-21 12:12:51 -0700170 }
Lloyd Piquef16688f2019-02-19 17:47:57 -0800171 return std::nullopt;
David Sodman0c69cad2017-08-21 12:12:51 -0700172 }
Lloyd Pique688abd42019-02-15 15:42:24 -0800173 bool blackOutLayer = (isProtected() && !targetSettings.supportsProtectedContent) ||
Lloyd Piquef16688f2019-02-19 17:47:57 -0800174 (isSecure() && !targetSettings.isSecure);
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000175 const State& s(getDrawingState());
Lloyd Piquef16688f2019-02-19 17:47:57 -0800176 auto& layer = *result;
David Sodman0c69cad2017-08-21 12:12:51 -0700177 if (!blackOutLayer) {
chaviwd62d3062019-09-04 14:48:02 -0700178 layer.source.buffer.buffer = mBufferInfo.mBuffer;
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000179 layer.source.buffer.isOpaque = isOpaque(s);
chaviwd62d3062019-09-04 14:48:02 -0700180 layer.source.buffer.fence = mBufferInfo.mFence;
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000181 layer.source.buffer.textureName = mTextureName;
182 layer.source.buffer.usePremultipliedAlpha = getPremultipledAlpha();
183 layer.source.buffer.isY410BT2020 = isHdrY410();
David Sodman0c69cad2017-08-21 12:12:51 -0700184 // TODO: we could be more subtle with isFixedSize()
Lloyd Piquef16688f2019-02-19 17:47:57 -0800185 const bool useFiltering = targetSettings.needsFiltering || mNeedsFiltering || isFixedSize();
David Sodman0c69cad2017-08-21 12:12:51 -0700186
187 // Query the texture matrix given our current filtering mode.
188 float textureMatrix[16];
chaviwf83ce182019-09-12 14:43:08 -0700189 getDrawingTransformMatrix(useFiltering, textureMatrix);
David Sodman0c69cad2017-08-21 12:12:51 -0700190
191 if (getTransformToDisplayInverse()) {
192 /*
193 * the code below applies the primary display's inverse transform to
194 * the texture transform
195 */
196 uint32_t transform = DisplayDevice::getPrimaryDisplayOrientationTransform();
197 mat4 tr = inverseOrientation(transform);
198
199 /**
200 * TODO(b/36727915): This is basically a hack.
201 *
202 * Ensure that regardless of the parent transformation,
203 * this buffer is always transformed from native display
204 * orientation to display orientation. For example, in the case
205 * of a camera where the buffer remains in native orientation,
206 * we want the pixels to always be upright.
207 */
208 sp<Layer> p = mDrawingParent.promote();
209 if (p != nullptr) {
210 const auto parentTransform = p->getTransform();
211 tr = tr * inverseOrientation(parentTransform.getOrientation());
212 }
213
214 // and finally apply it to the original texture matrix
215 const mat4 texTransform(mat4(static_cast<const float*>(textureMatrix)) * tr);
216 memcpy(textureMatrix, texTransform.asArray(), sizeof(textureMatrix));
217 }
218
Vishnu Nair4351ad52019-02-11 14:13:02 -0800219 const Rect win{getBounds()};
Marissa Wall290ad082019-03-06 13:23:47 -0800220 float bufferWidth = getBufferSize(s).getWidth();
221 float bufferHeight = getBufferSize(s).getHeight();
222
223 // BufferStateLayers can have a "buffer size" of [0, 0, -1, -1] when no display frame has
224 // been set and there is no parent layer bounds. In that case, the scale is meaningless so
225 // ignore them.
226 if (!getBufferSize(s).isValid()) {
227 bufferWidth = float(win.right) - float(win.left);
228 bufferHeight = float(win.bottom) - float(win.top);
229 }
David Sodman0c69cad2017-08-21 12:12:51 -0700230
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000231 const float scaleHeight = (float(win.bottom) - float(win.top)) / bufferHeight;
232 const float scaleWidth = (float(win.right) - float(win.left)) / bufferWidth;
233 const float translateY = float(win.top) / bufferHeight;
234 const float translateX = float(win.left) / bufferWidth;
235
236 // Flip y-coordinates because GLConsumer expects OpenGL convention.
237 mat4 tr = mat4::translate(vec4(.5, .5, 0, 1)) * mat4::scale(vec4(1, -1, 1, 1)) *
238 mat4::translate(vec4(-.5, -.5, 0, 1)) *
239 mat4::translate(vec4(translateX, translateY, 0, 1)) *
240 mat4::scale(vec4(scaleWidth, scaleHeight, 1.0, 1.0));
241
242 layer.source.buffer.useTextureFiltering = useFiltering;
243 layer.source.buffer.textureTransform = mat4(static_cast<const float*>(textureMatrix)) * tr;
David Sodman0c69cad2017-08-21 12:12:51 -0700244 } else {
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000245 // If layer is blacked out, force alpha to 1 so that we draw a black color
246 // layer.
247 layer.source.buffer.buffer = nullptr;
248 layer.alpha = 1.0;
David Sodman0c69cad2017-08-21 12:12:51 -0700249 }
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000250
Lloyd Piquef16688f2019-02-19 17:47:57 -0800251 return result;
David Sodman0c69cad2017-08-21 12:12:51 -0700252}
253
Marissa Wallfd668622018-05-10 10:21:13 -0700254bool BufferLayer::isHdrY410() const {
255 // pixel format is HDR Y410 masquerading as RGBA_1010102
chaviw4244e032019-09-04 11:27:49 -0700256 return (mBufferInfo.mDataspace == ui::Dataspace::BT2020_ITU_PQ &&
257 mBufferInfo.mApi == NATIVE_WINDOW_API_MEDIA &&
chaviwd62d3062019-09-04 14:48:02 -0700258 mBufferInfo.mBuffer->getPixelFormat() == HAL_PIXEL_FORMAT_RGBA_1010102);
David Sodmaneb085e02017-10-05 18:49:04 -0700259}
260
Lloyd Piquef5275482019-01-29 18:42:42 -0800261void BufferLayer::latchPerFrameState(
262 compositionengine::LayerFECompositionState& compositionState) const {
263 Layer::latchPerFrameState(compositionState);
David Sodman0c69cad2017-08-21 12:12:51 -0700264
265 // Sideband layers
Lloyd Piquef5275482019-01-29 18:42:42 -0800266 if (compositionState.sidebandStream.get()) {
267 compositionState.compositionType = Hwc2::IComposerClient::Composition::SIDEBAND;
David Sodman15094112018-10-11 09:39:37 -0700268 } else {
Lloyd Piquef5275482019-01-29 18:42:42 -0800269 // Normal buffer layers
chaviw4244e032019-09-04 11:27:49 -0700270 compositionState.hdrMetadata = mBufferInfo.mHdrMetadata;
Lloyd Piquef5275482019-01-29 18:42:42 -0800271 compositionState.compositionType = mPotentialCursor
272 ? Hwc2::IComposerClient::Composition::CURSOR
273 : Hwc2::IComposerClient::Composition::DEVICE;
David Sodman0c69cad2017-08-21 12:12:51 -0700274 }
David Sodman0c69cad2017-08-21 12:12:51 -0700275}
276
Marissa Wallfd668622018-05-10 10:21:13 -0700277bool BufferLayer::onPreComposition(nsecs_t refreshStartTime) {
chaviwd62d3062019-09-04 14:48:02 -0700278 if (mBufferInfo.mBuffer != nullptr) {
Marissa Wallfd668622018-05-10 10:21:13 -0700279 Mutex::Autolock lock(mFrameEventHistoryMutex);
280 mFrameEventHistory.addPreComposition(mCurrentFrameNumber, refreshStartTime);
David Sodman0c69cad2017-08-21 12:12:51 -0700281 }
Marissa Wallfd668622018-05-10 10:21:13 -0700282 mRefreshPending = false;
283 return hasReadyFrame();
David Sodman0c69cad2017-08-21 12:12:51 -0700284}
285
Dominik Laskowski075d3172018-05-24 15:50:06 -0700286bool BufferLayer::onPostComposition(const std::optional<DisplayId>& displayId,
287 const std::shared_ptr<FenceTime>& glDoneFence,
Marissa Wallfd668622018-05-10 10:21:13 -0700288 const std::shared_ptr<FenceTime>& presentFence,
289 const CompositorTiming& compositorTiming) {
290 // mFrameLatencyNeeded is true when a new frame was latched for the
291 // composition.
292 if (!mFrameLatencyNeeded) return false;
293
294 // Update mFrameEventHistory.
Dan Stoza436ccf32018-06-21 12:10:12 -0700295 {
Marissa Wallfd668622018-05-10 10:21:13 -0700296 Mutex::Autolock lock(mFrameEventHistoryMutex);
297 mFrameEventHistory.addPostComposition(mCurrentFrameNumber, glDoneFence, presentFence,
298 compositorTiming);
David Sodman0c69cad2017-08-21 12:12:51 -0700299 }
300
Marissa Wallfd668622018-05-10 10:21:13 -0700301 // Update mFrameTracker.
chaviw4244e032019-09-04 11:27:49 -0700302 nsecs_t desiredPresentTime = mBufferInfo.mDesiredPresentTime;
Marissa Wallfd668622018-05-10 10:21:13 -0700303 mFrameTracker.setDesiredPresentTime(desiredPresentTime);
304
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700305 const int32_t layerID = getSequence();
Yiwei Zhang7e666a52018-11-15 13:33:42 -0800306 mFlinger->mTimeStats->setDesiredTime(layerID, mCurrentFrameNumber, desiredPresentTime);
Marissa Wallfd668622018-05-10 10:21:13 -0700307
chaviw4244e032019-09-04 11:27:49 -0700308 std::shared_ptr<FenceTime> frameReadyFence = mBufferInfo.mFenceTime;
Marissa Wallfd668622018-05-10 10:21:13 -0700309 if (frameReadyFence->isValid()) {
310 mFrameTracker.setFrameReadyFence(std::move(frameReadyFence));
311 } else {
312 // There was no fence for this frame, so assume that it was ready
313 // to be presented at the desired present time.
314 mFrameTracker.setFrameReadyTime(desiredPresentTime);
Dominik Laskowski45de9bd2018-06-11 17:44:10 -0700315 }
Marissa Wallfd668622018-05-10 10:21:13 -0700316
317 if (presentFence->isValid()) {
Yiwei Zhang7e666a52018-11-15 13:33:42 -0800318 mFlinger->mTimeStats->setPresentFence(layerID, mCurrentFrameNumber, presentFence);
Mikael Pessa90092f42019-08-26 17:22:04 -0700319 mFlinger->mFrameTracer->traceFence(layerID, getCurrentBufferId(), mCurrentFrameNumber,
320 presentFence, FrameTracer::FrameEvent::PRESENT_FENCE);
Marissa Wallfd668622018-05-10 10:21:13 -0700321 mFrameTracker.setActualPresentFence(std::shared_ptr<FenceTime>(presentFence));
Dominik Laskowski075d3172018-05-24 15:50:06 -0700322 } else if (displayId && mFlinger->getHwComposer().isConnected(*displayId)) {
Marissa Wallfd668622018-05-10 10:21:13 -0700323 // The HWC doesn't support present fences, so use the refresh
324 // timestamp instead.
Dominik Laskowski075d3172018-05-24 15:50:06 -0700325 const nsecs_t actualPresentTime = mFlinger->getHwComposer().getRefreshTimestamp(*displayId);
Yiwei Zhang7e666a52018-11-15 13:33:42 -0800326 mFlinger->mTimeStats->setPresentTime(layerID, mCurrentFrameNumber, actualPresentTime);
Mikael Pessa90092f42019-08-26 17:22:04 -0700327 mFlinger->mFrameTracer->traceTimestamp(layerID, getCurrentBufferId(), mCurrentFrameNumber,
328 actualPresentTime,
329 FrameTracer::FrameEvent::PRESENT_FENCE);
Marissa Wallfd668622018-05-10 10:21:13 -0700330 mFrameTracker.setActualPresentTime(actualPresentTime);
331 }
332
333 mFrameTracker.advanceFrame();
334 mFrameLatencyNeeded = false;
335 return true;
David Sodman0c69cad2017-08-21 12:12:51 -0700336}
337
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700338bool BufferLayer::latchBuffer(bool& recomputeVisibleRegions, nsecs_t latchTime,
339 nsecs_t expectedPresentTime) {
Marissa Wallfd668622018-05-10 10:21:13 -0700340 ATRACE_CALL();
David Sodman0c69cad2017-08-21 12:12:51 -0700341
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800342 bool refreshRequired = latchSidebandStream(recomputeVisibleRegions);
David Sodman0c69cad2017-08-21 12:12:51 -0700343
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800344 if (refreshRequired) {
345 return refreshRequired;
David Sodman0c69cad2017-08-21 12:12:51 -0700346 }
347
Marissa Wallfd668622018-05-10 10:21:13 -0700348 if (!hasReadyFrame()) {
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800349 return false;
David Sodman0c69cad2017-08-21 12:12:51 -0700350 }
David Sodman0c69cad2017-08-21 12:12:51 -0700351
Marissa Wallfd668622018-05-10 10:21:13 -0700352 // if we've already called updateTexImage() without going through
353 // a composition step, we have to skip this layer at this point
354 // because we cannot call updateTeximage() without a corresponding
355 // compositionComplete() call.
356 // we'll trigger an update in onPreComposition().
357 if (mRefreshPending) {
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800358 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700359 }
360
361 // If the head buffer's acquire fence hasn't signaled yet, return and
362 // try again later
363 if (!fenceHasSignaled()) {
Ady Abraham09bd3922019-04-08 10:44:56 -0700364 ATRACE_NAME("!fenceHasSignaled()");
David Sodman0c69cad2017-08-21 12:12:51 -0700365 mFlinger->signalLayerUpdate();
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800366 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700367 }
368
369 // Capture the old state of the layer for comparisons later
370 const State& s(getDrawingState());
371 const bool oldOpacity = isOpaque(s);
chaviwd62d3062019-09-04 14:48:02 -0700372
373 BufferInfo oldBufferInfo = mBufferInfo;
Marissa Wallfd668622018-05-10 10:21:13 -0700374
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700375 if (!allTransactionsSignaled(expectedPresentTime)) {
Marissa Wallebb486e2019-05-15 14:08:08 -0700376 mFlinger->setTransactionFlags(eTraversalNeeded);
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800377 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700378 }
379
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700380 status_t err = updateTexImage(recomputeVisibleRegions, latchTime, expectedPresentTime);
Marissa Wallfd668622018-05-10 10:21:13 -0700381 if (err != NO_ERROR) {
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800382 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700383 }
384
385 err = updateActiveBuffer();
386 if (err != NO_ERROR) {
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800387 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700388 }
389
Marissa Wallfd668622018-05-10 10:21:13 -0700390 err = updateFrameNumber(latchTime);
391 if (err != NO_ERROR) {
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800392 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700393 }
394
chaviw4244e032019-09-04 11:27:49 -0700395 gatherBufferInfo();
396
Marissa Wallfd668622018-05-10 10:21:13 -0700397 mRefreshPending = true;
398 mFrameLatencyNeeded = true;
chaviwd62d3062019-09-04 14:48:02 -0700399 if (oldBufferInfo.mBuffer == nullptr) {
Marissa Wallfd668622018-05-10 10:21:13 -0700400 // the first time we receive a buffer, we need to trigger a
401 // geometry invalidation.
402 recomputeVisibleRegions = true;
403 }
404
chaviw4244e032019-09-04 11:27:49 -0700405 if ((mBufferInfo.mCrop != oldBufferInfo.mCrop) ||
406 (mBufferInfo.mTransform != oldBufferInfo.mTransform) ||
407 (mBufferInfo.mScaleMode != oldBufferInfo.mScaleMode) ||
408 (mBufferInfo.mTransformToDisplayInverse != oldBufferInfo.mTransformToDisplayInverse)) {
Marissa Wallfd668622018-05-10 10:21:13 -0700409 recomputeVisibleRegions = true;
410 }
411
chaviwd62d3062019-09-04 14:48:02 -0700412 if (oldBufferInfo.mBuffer != nullptr) {
413 uint32_t bufWidth = mBufferInfo.mBuffer->getWidth();
414 uint32_t bufHeight = mBufferInfo.mBuffer->getHeight();
415 if (bufWidth != uint32_t(oldBufferInfo.mBuffer->width) ||
416 bufHeight != uint32_t(oldBufferInfo.mBuffer->height)) {
Marissa Wallfd668622018-05-10 10:21:13 -0700417 recomputeVisibleRegions = true;
418 }
419 }
420
421 if (oldOpacity != isOpaque(s)) {
422 recomputeVisibleRegions = true;
423 }
424
425 // Remove any sync points corresponding to the buffer which was just
426 // latched
427 {
428 Mutex::Autolock lock(mLocalSyncPointMutex);
429 auto point = mLocalSyncPoints.begin();
430 while (point != mLocalSyncPoints.end()) {
431 if (!(*point)->frameIsAvailable() || !(*point)->transactionIsApplied()) {
432 // This sync point must have been added since we started
433 // latching. Don't drop it yet.
434 ++point;
435 continue;
436 }
437
438 if ((*point)->getFrameNumber() <= mCurrentFrameNumber) {
Alec Mourie60041e2019-06-14 18:59:51 -0700439 std::stringstream ss;
440 ss << "Dropping sync point " << (*point)->getFrameNumber();
441 ATRACE_NAME(ss.str().c_str());
Marissa Wallfd668622018-05-10 10:21:13 -0700442 point = mLocalSyncPoints.erase(point);
443 } else {
444 ++point;
445 }
446 }
447 }
448
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800449 return true;
Marissa Wallfd668622018-05-10 10:21:13 -0700450}
451
452// transaction
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700453void BufferLayer::notifyAvailableFrames(nsecs_t expectedPresentTime) {
454 const auto headFrameNumber = getHeadFrameNumber(expectedPresentTime);
Ady Abrahamcd1580c2019-04-29 15:40:03 -0700455 const bool headFenceSignaled = fenceHasSignaled();
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700456 const bool presentTimeIsCurrent = framePresentTimeIsCurrent(expectedPresentTime);
Marissa Wallfd668622018-05-10 10:21:13 -0700457 Mutex::Autolock lock(mLocalSyncPointMutex);
458 for (auto& point : mLocalSyncPoints) {
Ady Abrahamcd1580c2019-04-29 15:40:03 -0700459 if (headFrameNumber >= point->getFrameNumber() && headFenceSignaled &&
460 presentTimeIsCurrent) {
Marissa Wallfd668622018-05-10 10:21:13 -0700461 point->setFrameAvailable();
chaviw43cb3cb2019-05-31 15:23:41 -0700462 sp<Layer> requestedSyncLayer = point->getRequestedSyncLayer();
463 if (requestedSyncLayer) {
464 // Need to update the transaction flag to ensure the layer's pending transaction
465 // gets applied.
466 requestedSyncLayer->setTransactionFlags(eTransactionNeeded);
467 }
Marissa Wallfd668622018-05-10 10:21:13 -0700468 }
David Sodman0c69cad2017-08-21 12:12:51 -0700469 }
470}
471
Marissa Wallfd668622018-05-10 10:21:13 -0700472bool BufferLayer::hasReadyFrame() const {
Marissa Wall024a1912018-08-13 13:55:35 -0700473 return hasFrameUpdate() || getSidebandStreamChanged() || getAutoRefresh();
Marissa Wallfd668622018-05-10 10:21:13 -0700474}
475
476uint32_t BufferLayer::getEffectiveScalingMode() const {
477 if (mOverrideScalingMode >= 0) {
478 return mOverrideScalingMode;
479 }
480
chaviw4244e032019-09-04 11:27:49 -0700481 return mBufferInfo.mScaleMode;
Marissa Wallfd668622018-05-10 10:21:13 -0700482}
483
484bool BufferLayer::isProtected() const {
chaviwd62d3062019-09-04 14:48:02 -0700485 const sp<GraphicBuffer>& buffer(mBufferInfo.mBuffer);
Marissa Wallfd668622018-05-10 10:21:13 -0700486 return (buffer != 0) && (buffer->getUsage() & GRALLOC_USAGE_PROTECTED);
487}
488
489bool BufferLayer::latchUnsignaledBuffers() {
490 static bool propertyLoaded = false;
491 static bool latch = false;
492 static std::mutex mutex;
493 std::lock_guard<std::mutex> lock(mutex);
494 if (!propertyLoaded) {
495 char value[PROPERTY_VALUE_MAX] = {};
496 property_get("debug.sf.latch_unsignaled", value, "0");
497 latch = atoi(value);
498 propertyLoaded = true;
499 }
500 return latch;
501}
502
503// h/w composer set-up
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700504bool BufferLayer::allTransactionsSignaled(nsecs_t expectedPresentTime) {
505 const auto headFrameNumber = getHeadFrameNumber(expectedPresentTime);
Marissa Wallfd668622018-05-10 10:21:13 -0700506 bool matchingFramesFound = false;
507 bool allTransactionsApplied = true;
508 Mutex::Autolock lock(mLocalSyncPointMutex);
509
510 for (auto& point : mLocalSyncPoints) {
511 if (point->getFrameNumber() > headFrameNumber) {
512 break;
513 }
514 matchingFramesFound = true;
515
516 if (!point->frameIsAvailable()) {
517 // We haven't notified the remote layer that the frame for
518 // this point is available yet. Notify it now, and then
519 // abort this attempt to latch.
520 point->setFrameAvailable();
521 allTransactionsApplied = false;
522 break;
523 }
524
525 allTransactionsApplied = allTransactionsApplied && point->transactionIsApplied();
526 }
527 return !matchingFramesFound || allTransactionsApplied;
David Sodman0c69cad2017-08-21 12:12:51 -0700528}
529
530// As documented in libhardware header, formats in the range
531// 0x100 - 0x1FF are specific to the HAL implementation, and
532// are known to have no alpha channel
533// TODO: move definition for device-specific range into
534// hardware.h, instead of using hard-coded values here.
535#define HARDWARE_IS_DEVICE_FORMAT(f) ((f) >= 0x100 && (f) <= 0x1FF)
536
537bool BufferLayer::getOpacityForFormat(uint32_t format) {
538 if (HARDWARE_IS_DEVICE_FORMAT(format)) {
539 return true;
540 }
541 switch (format) {
542 case HAL_PIXEL_FORMAT_RGBA_8888:
543 case HAL_PIXEL_FORMAT_BGRA_8888:
544 case HAL_PIXEL_FORMAT_RGBA_FP16:
545 case HAL_PIXEL_FORMAT_RGBA_1010102:
546 return false;
547 }
548 // in all other case, we have no blending (also for unknown formats)
549 return true;
550}
551
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800552bool BufferLayer::needsFiltering(const sp<const DisplayDevice>& displayDevice) const {
Lloyd Piquef16688f2019-02-19 17:47:57 -0800553 // If we are not capturing based on the state of a known display device,
554 // just return false.
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800555 if (displayDevice == nullptr) {
Lloyd Piquef16688f2019-02-19 17:47:57 -0800556 return false;
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800557 }
558
559 const auto outputLayer = findOutputLayerForDisplay(displayDevice);
560 if (outputLayer == nullptr) {
Lloyd Piquef16688f2019-02-19 17:47:57 -0800561 return false;
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800562 }
563
Lloyd Piquef16688f2019-02-19 17:47:57 -0800564 // We need filtering if the sourceCrop rectangle size does not match the
565 // displayframe rectangle size (not a 1:1 render)
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800566 const auto& compositionState = outputLayer->getState();
567 const auto displayFrame = compositionState.displayFrame;
568 const auto sourceCrop = compositionState.sourceCrop;
Lloyd Piquef16688f2019-02-19 17:47:57 -0800569 return sourceCrop.getHeight() != displayFrame.getHeight() ||
Peiyong Linc2020ca2019-01-10 11:36:12 -0800570 sourceCrop.getWidth() != displayFrame.getWidth();
Chia-I Wu692e0832018-06-05 15:46:58 -0700571}
572
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700573uint64_t BufferLayer::getHeadFrameNumber(nsecs_t expectedPresentTime) const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800574 if (hasFrameUpdate()) {
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700575 return getFrameNumber(expectedPresentTime);
David Sodman0c69cad2017-08-21 12:12:51 -0700576 } else {
577 return mCurrentFrameNumber;
578 }
579}
580
Vishnu Nair60356342018-11-13 13:00:45 -0800581Rect BufferLayer::getBufferSize(const State& s) const {
582 // If we have a sideband stream, or we are scaling the buffer then return the layer size since
583 // we cannot determine the buffer size.
584 if ((s.sidebandStream != nullptr) ||
585 (getEffectiveScalingMode() != NATIVE_WINDOW_SCALING_MODE_FREEZE)) {
586 return Rect(getActiveWidth(s), getActiveHeight(s));
587 }
588
chaviwd62d3062019-09-04 14:48:02 -0700589 if (mBufferInfo.mBuffer == nullptr) {
Vishnu Nair60356342018-11-13 13:00:45 -0800590 return Rect::INVALID_RECT;
591 }
592
chaviwd62d3062019-09-04 14:48:02 -0700593 uint32_t bufWidth = mBufferInfo.mBuffer->getWidth();
594 uint32_t bufHeight = mBufferInfo.mBuffer->getHeight();
Vishnu Nair60356342018-11-13 13:00:45 -0800595
596 // Undo any transformations on the buffer and return the result.
chaviw4244e032019-09-04 11:27:49 -0700597 if (mBufferInfo.mTransform & ui::Transform::ROT_90) {
Vishnu Nair60356342018-11-13 13:00:45 -0800598 std::swap(bufWidth, bufHeight);
599 }
600
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800601 if (getTransformToDisplayInverse()) {
Vishnu Nair60356342018-11-13 13:00:45 -0800602 uint32_t invTransform = DisplayDevice::getPrimaryDisplayOrientationTransform();
603 if (invTransform & ui::Transform::ROT_90) {
604 std::swap(bufWidth, bufHeight);
605 }
606 }
607
608 return Rect(bufWidth, bufHeight);
609}
610
Lloyd Piquefeb73d72018-12-04 17:23:44 -0800611std::shared_ptr<compositionengine::Layer> BufferLayer::getCompositionLayer() const {
612 return mCompositionLayer;
613}
614
Vishnu Nair4351ad52019-02-11 14:13:02 -0800615FloatRect BufferLayer::computeSourceBounds(const FloatRect& parentBounds) const {
616 const State& s(getDrawingState());
617
618 // If we have a sideband stream, or we are scaling the buffer then return the layer size since
619 // we cannot determine the buffer size.
620 if ((s.sidebandStream != nullptr) ||
621 (getEffectiveScalingMode() != NATIVE_WINDOW_SCALING_MODE_FREEZE)) {
622 return FloatRect(0, 0, getActiveWidth(s), getActiveHeight(s));
623 }
624
chaviwd62d3062019-09-04 14:48:02 -0700625 if (mBufferInfo.mBuffer == nullptr) {
Vishnu Nair4351ad52019-02-11 14:13:02 -0800626 return parentBounds;
627 }
628
chaviwd62d3062019-09-04 14:48:02 -0700629 uint32_t bufWidth = mBufferInfo.mBuffer->getWidth();
630 uint32_t bufHeight = mBufferInfo.mBuffer->getHeight();
Vishnu Nair4351ad52019-02-11 14:13:02 -0800631
632 // Undo any transformations on the buffer and return the result.
chaviw4244e032019-09-04 11:27:49 -0700633 if (mBufferInfo.mTransform & ui::Transform::ROT_90) {
Vishnu Nair4351ad52019-02-11 14:13:02 -0800634 std::swap(bufWidth, bufHeight);
635 }
636
637 if (getTransformToDisplayInverse()) {
638 uint32_t invTransform = DisplayDevice::getPrimaryDisplayOrientationTransform();
639 if (invTransform & ui::Transform::ROT_90) {
640 std::swap(bufWidth, bufHeight);
641 }
642 }
643
644 return FloatRect(0, 0, bufWidth, bufHeight);
645}
646
chaviw49a108c2019-08-12 11:23:06 -0700647void BufferLayer::latchAndReleaseBuffer() {
648 mRefreshPending = false;
649 if (hasReadyFrame()) {
650 bool ignored = false;
651 latchBuffer(ignored, systemTime(), 0 /* expectedPresentTime */);
652 }
653 releasePendingBuffer(systemTime());
654}
655
chaviw4244e032019-09-04 11:27:49 -0700656PixelFormat BufferLayer::getPixelFormat() const {
657 return mBufferInfo.mPixelFormat;
658}
659
660bool BufferLayer::getTransformToDisplayInverse() const {
661 return mBufferInfo.mTransformToDisplayInverse;
662}
663
664Rect BufferLayer::getBufferCrop() const {
665 // this is the crop rectangle that applies to the buffer
666 // itself (as opposed to the window)
667 if (!mBufferInfo.mCrop.isEmpty()) {
668 // if the buffer crop is defined, we use that
669 return mBufferInfo.mCrop;
chaviwd62d3062019-09-04 14:48:02 -0700670 } else if (mBufferInfo.mBuffer != nullptr) {
chaviw4244e032019-09-04 11:27:49 -0700671 // otherwise we use the whole buffer
chaviwd62d3062019-09-04 14:48:02 -0700672 return mBufferInfo.mBuffer->getBounds();
chaviw4244e032019-09-04 11:27:49 -0700673 } else {
674 // if we don't have a buffer yet, we use an empty/invalid crop
675 return Rect();
676 }
677}
678
679uint32_t BufferLayer::getBufferTransform() const {
680 return mBufferInfo.mTransform;
681}
682
683ui::Dataspace BufferLayer::getDataSpace() const {
684 return mBufferInfo.mDataspace;
685}
686
687ui::Dataspace BufferLayer::translateDataspace(ui::Dataspace dataspace) {
688 ui::Dataspace updatedDataspace = dataspace;
689 // translate legacy dataspaces to modern dataspaces
690 switch (dataspace) {
691 case ui::Dataspace::SRGB:
692 updatedDataspace = ui::Dataspace::V0_SRGB;
693 break;
694 case ui::Dataspace::SRGB_LINEAR:
695 updatedDataspace = ui::Dataspace::V0_SRGB_LINEAR;
696 break;
697 case ui::Dataspace::JFIF:
698 updatedDataspace = ui::Dataspace::V0_JFIF;
699 break;
700 case ui::Dataspace::BT601_625:
701 updatedDataspace = ui::Dataspace::V0_BT601_625;
702 break;
703 case ui::Dataspace::BT601_525:
704 updatedDataspace = ui::Dataspace::V0_BT601_525;
705 break;
706 case ui::Dataspace::BT709:
707 updatedDataspace = ui::Dataspace::V0_BT709;
708 break;
709 default:
710 break;
711 }
712
713 return updatedDataspace;
714}
715
chaviwd62d3062019-09-04 14:48:02 -0700716sp<GraphicBuffer> BufferLayer::getBuffer() const {
717 return mBufferInfo.mBuffer;
718}
719
chaviwf83ce182019-09-12 14:43:08 -0700720void BufferLayer::getDrawingTransformMatrix(bool filteringEnabled, float outMatrix[16]) {
721 GLConsumer::computeTransformMatrix(outMatrix, mBufferInfo.mBuffer, mBufferInfo.mCrop,
722 mBufferInfo.mTransform, filteringEnabled);
723}
724
David Sodman0c69cad2017-08-21 12:12:51 -0700725} // namespace android
726
727#if defined(__gl_h_)
728#error "don't include gl/gl.h in this file"
729#endif
730
731#if defined(__gl2_h_)
732#error "don't include gl2/gl2.h in this file"
733#endif