blob: 6a3d60f50c41c66cd7b577238dd414cae555d22b [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>
37#include <gui/LayerDebugInfo.h>
38#include <gui/Surface.h>
39#include <renderengine/RenderEngine.h>
40#include <ui/DebugUtils.h>
41#include <utils/Errors.h>
42#include <utils/Log.h>
43#include <utils/NativeHandle.h>
44#include <utils/StopWatch.h>
45#include <utils/Trace.h>
46
Alec Mourie60041e2019-06-14 18:59:51 -070047#include <cmath>
48#include <cstdlib>
49#include <mutex>
50#include <sstream>
51
David Sodman0c69cad2017-08-21 12:12:51 -070052#include "Colorizer.h"
53#include "DisplayDevice.h"
Mikael Pessa90092f42019-08-26 17:22:04 -070054#include "FrameTracer/FrameTracer.h"
David Sodman0c69cad2017-08-21 12:12:51 -070055#include "LayerRejecter.h"
Yiwei Zhang7e666a52018-11-15 13:33:42 -080056#include "TimeStats/TimeStats.h"
57
David Sodman0c69cad2017-08-21 12:12:51 -070058namespace android {
59
Lloyd Pique42ab75e2018-09-12 20:46:03 -070060BufferLayer::BufferLayer(const LayerCreationArgs& args)
Lloyd Piquefeb73d72018-12-04 17:23:44 -080061 : Layer(args),
62 mTextureName(args.flinger->getNewTexture()),
63 mCompositionLayer{mFlinger->getCompositionEngine().createLayer(
64 compositionengine::LayerCreationArgs{this})} {
Lloyd Pique42ab75e2018-09-12 20:46:03 -070065 ALOGV("Creating Layer %s", args.name.string());
David Sodman0c69cad2017-08-21 12:12:51 -070066
Lloyd Pique42ab75e2018-09-12 20:46:03 -070067 mPremultipliedAlpha = !(args.flags & ISurfaceComposerClient::eNonPremultiplied);
David Sodman0c69cad2017-08-21 12:12:51 -070068
Lloyd Pique42ab75e2018-09-12 20:46:03 -070069 mPotentialCursor = args.flags & ISurfaceComposerClient::eCursorWindow;
70 mProtectedByApp = args.flags & ISurfaceComposerClient::eProtectedByApp;
David Sodman0c69cad2017-08-21 12:12:51 -070071}
72
73BufferLayer::~BufferLayer() {
David Sodman0c69cad2017-08-21 12:12:51 -070074 mFlinger->deleteTextureAsync(mTextureName);
Mikael Pessa90092f42019-08-26 17:22:04 -070075 const int32_t layerID = getSequence();
76 mFlinger->mTimeStats->onDestroy(layerID);
77 mFlinger->mFrameTracer->onDestroy(layerID);
David Sodman0c69cad2017-08-21 12:12:51 -070078}
79
David Sodmaneb085e02017-10-05 18:49:04 -070080void BufferLayer::useSurfaceDamage() {
81 if (mFlinger->mForceFullDamage) {
82 surfaceDamageRegion = Region::INVALID_REGION;
83 } else {
chaviw4244e032019-09-04 11:27:49 -070084 surfaceDamageRegion = mBufferInfo.mSurfaceDamage;
David Sodmaneb085e02017-10-05 18:49:04 -070085 }
86}
87
88void BufferLayer::useEmptyDamage() {
89 surfaceDamageRegion.clear();
90}
91
Marissa Wallfd668622018-05-10 10:21:13 -070092bool BufferLayer::isOpaque(const Layer::State& s) const {
93 // if we don't have a buffer or sidebandStream yet, we're translucent regardless of the
94 // layer's opaque flag.
chaviwd62d3062019-09-04 14:48:02 -070095 if ((mSidebandStream == nullptr) && (mBufferInfo.mBuffer == nullptr)) {
Marissa Wallfd668622018-05-10 10:21:13 -070096 return false;
97 }
98
99 // if the layer has the opaque flag, then we're always opaque,
100 // otherwise we use the current buffer's format.
101 return ((s.flags & layer_state_t::eLayerOpaque) != 0) || getOpacityForFormat(getPixelFormat());
David Sodman0c69cad2017-08-21 12:12:51 -0700102}
103
104bool BufferLayer::isVisible() const {
Ady Abrahama315ce72019-04-24 14:35:20 -0700105 bool visible = !(isHiddenByPolicy()) && getAlpha() > 0.0f &&
chaviwd62d3062019-09-04 14:48:02 -0700106 (mBufferInfo.mBuffer != nullptr || mSidebandStream != nullptr);
Ady Abrahama315ce72019-04-24 14:35:20 -0700107 mFlinger->mScheduler->setLayerVisibility(mSchedulerLayerHandle, visible);
108
109 return visible;
David Sodman0c69cad2017-08-21 12:12:51 -0700110}
111
112bool BufferLayer::isFixedSize() const {
113 return getEffectiveScalingMode() != NATIVE_WINDOW_SCALING_MODE_FREEZE;
114}
115
Lloyd Piquea83776c2019-01-29 18:42:32 -0800116bool BufferLayer::usesSourceCrop() const {
117 return true;
118}
119
David Sodman0c69cad2017-08-21 12:12:51 -0700120static constexpr mat4 inverseOrientation(uint32_t transform) {
David Sodman41fdfc92017-11-06 16:09:56 -0800121 const mat4 flipH(-1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1);
122 const mat4 flipV(1, 0, 0, 0, 0, -1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1);
123 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 -0700124 mat4 tr;
125
126 if (transform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
127 tr = tr * rot90;
128 }
129 if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_H) {
130 tr = tr * flipH;
131 }
132 if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_V) {
133 tr = tr * flipV;
134 }
135 return inverse(tr);
136}
137
Lloyd Piquef16688f2019-02-19 17:47:57 -0800138std::optional<renderengine::LayerSettings> BufferLayer::prepareClientComposition(
139 compositionengine::LayerFE::ClientCompositionTargetSettings& targetSettings) {
David Sodman0c69cad2017-08-21 12:12:51 -0700140 ATRACE_CALL();
Lloyd Piquef16688f2019-02-19 17:47:57 -0800141
142 auto result = Layer::prepareClientComposition(targetSettings);
143 if (!result) {
144 return result;
145 }
146
chaviwd62d3062019-09-04 14:48:02 -0700147 if (CC_UNLIKELY(mBufferInfo.mBuffer == 0)) {
David Sodman0c69cad2017-08-21 12:12:51 -0700148 // the texture has not been created yet, this Layer has
149 // in fact never been drawn into. This happens frequently with
150 // SurfaceView because the WindowManager can't know when the client
151 // has drawn the first time.
152
153 // If there is nothing under us, we paint the screen in black, otherwise
154 // we just skip this update.
155
156 // figure out if there is something below us
157 Region under;
158 bool finished = false;
159 mFlinger->mDrawingState.traverseInZOrder([&](Layer* layer) {
160 if (finished || layer == static_cast<BufferLayer const*>(this)) {
161 finished = true;
162 return;
163 }
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000164 under.orSelf(layer->visibleRegion);
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];
Marissa Wallfd668622018-05-10 10:21:13 -0700189 setFilteringEnabled(useFiltering);
chaviw4244e032019-09-04 11:27:49 -0700190 memcpy(textureMatrix, mBufferInfo.mTransformMatrix, sizeof(mBufferInfo.mTransformMatrix));
David Sodman0c69cad2017-08-21 12:12:51 -0700191
192 if (getTransformToDisplayInverse()) {
193 /*
194 * the code below applies the primary display's inverse transform to
195 * the texture transform
196 */
197 uint32_t transform = DisplayDevice::getPrimaryDisplayOrientationTransform();
198 mat4 tr = inverseOrientation(transform);
199
200 /**
201 * TODO(b/36727915): This is basically a hack.
202 *
203 * Ensure that regardless of the parent transformation,
204 * this buffer is always transformed from native display
205 * orientation to display orientation. For example, in the case
206 * of a camera where the buffer remains in native orientation,
207 * we want the pixels to always be upright.
208 */
209 sp<Layer> p = mDrawingParent.promote();
210 if (p != nullptr) {
211 const auto parentTransform = p->getTransform();
212 tr = tr * inverseOrientation(parentTransform.getOrientation());
213 }
214
215 // and finally apply it to the original texture matrix
216 const mat4 texTransform(mat4(static_cast<const float*>(textureMatrix)) * tr);
217 memcpy(textureMatrix, texTransform.asArray(), sizeof(textureMatrix));
218 }
219
Vishnu Nair4351ad52019-02-11 14:13:02 -0800220 const Rect win{getBounds()};
Marissa Wall290ad082019-03-06 13:23:47 -0800221 float bufferWidth = getBufferSize(s).getWidth();
222 float bufferHeight = getBufferSize(s).getHeight();
223
224 // BufferStateLayers can have a "buffer size" of [0, 0, -1, -1] when no display frame has
225 // been set and there is no parent layer bounds. In that case, the scale is meaningless so
226 // ignore them.
227 if (!getBufferSize(s).isValid()) {
228 bufferWidth = float(win.right) - float(win.left);
229 bufferHeight = float(win.bottom) - float(win.top);
230 }
David Sodman0c69cad2017-08-21 12:12:51 -0700231
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000232 const float scaleHeight = (float(win.bottom) - float(win.top)) / bufferHeight;
233 const float scaleWidth = (float(win.right) - float(win.left)) / bufferWidth;
234 const float translateY = float(win.top) / bufferHeight;
235 const float translateX = float(win.left) / bufferWidth;
236
237 // Flip y-coordinates because GLConsumer expects OpenGL convention.
238 mat4 tr = mat4::translate(vec4(.5, .5, 0, 1)) * mat4::scale(vec4(1, -1, 1, 1)) *
239 mat4::translate(vec4(-.5, -.5, 0, 1)) *
240 mat4::translate(vec4(translateX, translateY, 0, 1)) *
241 mat4::scale(vec4(scaleWidth, scaleHeight, 1.0, 1.0));
242
243 layer.source.buffer.useTextureFiltering = useFiltering;
244 layer.source.buffer.textureTransform = mat4(static_cast<const float*>(textureMatrix)) * tr;
David Sodman0c69cad2017-08-21 12:12:51 -0700245 } else {
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000246 // If layer is blacked out, force alpha to 1 so that we draw a black color
247 // layer.
248 layer.source.buffer.buffer = nullptr;
249 layer.alpha = 1.0;
David Sodman0c69cad2017-08-21 12:12:51 -0700250 }
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000251
Lloyd Piquef16688f2019-02-19 17:47:57 -0800252 return result;
David Sodman0c69cad2017-08-21 12:12:51 -0700253}
254
Marissa Wallfd668622018-05-10 10:21:13 -0700255bool BufferLayer::isHdrY410() const {
256 // pixel format is HDR Y410 masquerading as RGBA_1010102
chaviw4244e032019-09-04 11:27:49 -0700257 return (mBufferInfo.mDataspace == ui::Dataspace::BT2020_ITU_PQ &&
258 mBufferInfo.mApi == NATIVE_WINDOW_API_MEDIA &&
chaviwd62d3062019-09-04 14:48:02 -0700259 mBufferInfo.mBuffer->getPixelFormat() == HAL_PIXEL_FORMAT_RGBA_1010102);
David Sodmaneb085e02017-10-05 18:49:04 -0700260}
261
Lloyd Piquef5275482019-01-29 18:42:42 -0800262void BufferLayer::latchPerFrameState(
263 compositionengine::LayerFECompositionState& compositionState) const {
264 Layer::latchPerFrameState(compositionState);
David Sodman0c69cad2017-08-21 12:12:51 -0700265
266 // Sideband layers
Lloyd Piquef5275482019-01-29 18:42:42 -0800267 if (compositionState.sidebandStream.get()) {
268 compositionState.compositionType = Hwc2::IComposerClient::Composition::SIDEBAND;
David Sodman15094112018-10-11 09:39:37 -0700269 } else {
Lloyd Piquef5275482019-01-29 18:42:42 -0800270 // Normal buffer layers
chaviw4244e032019-09-04 11:27:49 -0700271 compositionState.hdrMetadata = mBufferInfo.mHdrMetadata;
Lloyd Piquef5275482019-01-29 18:42:42 -0800272 compositionState.compositionType = mPotentialCursor
273 ? Hwc2::IComposerClient::Composition::CURSOR
274 : Hwc2::IComposerClient::Composition::DEVICE;
David Sodman0c69cad2017-08-21 12:12:51 -0700275 }
David Sodman0c69cad2017-08-21 12:12:51 -0700276}
277
Marissa Wallfd668622018-05-10 10:21:13 -0700278bool BufferLayer::onPreComposition(nsecs_t refreshStartTime) {
chaviwd62d3062019-09-04 14:48:02 -0700279 if (mBufferInfo.mBuffer != nullptr) {
Marissa Wallfd668622018-05-10 10:21:13 -0700280 Mutex::Autolock lock(mFrameEventHistoryMutex);
281 mFrameEventHistory.addPreComposition(mCurrentFrameNumber, refreshStartTime);
David Sodman0c69cad2017-08-21 12:12:51 -0700282 }
Marissa Wallfd668622018-05-10 10:21:13 -0700283 mRefreshPending = false;
284 return hasReadyFrame();
David Sodman0c69cad2017-08-21 12:12:51 -0700285}
286
Dominik Laskowski075d3172018-05-24 15:50:06 -0700287bool BufferLayer::onPostComposition(const std::optional<DisplayId>& displayId,
288 const std::shared_ptr<FenceTime>& glDoneFence,
Marissa Wallfd668622018-05-10 10:21:13 -0700289 const std::shared_ptr<FenceTime>& presentFence,
290 const CompositorTiming& compositorTiming) {
291 // mFrameLatencyNeeded is true when a new frame was latched for the
292 // composition.
293 if (!mFrameLatencyNeeded) return false;
294
295 // Update mFrameEventHistory.
Dan Stoza436ccf32018-06-21 12:10:12 -0700296 {
Marissa Wallfd668622018-05-10 10:21:13 -0700297 Mutex::Autolock lock(mFrameEventHistoryMutex);
298 mFrameEventHistory.addPostComposition(mCurrentFrameNumber, glDoneFence, presentFence,
299 compositorTiming);
David Sodman0c69cad2017-08-21 12:12:51 -0700300 }
301
Marissa Wallfd668622018-05-10 10:21:13 -0700302 // Update mFrameTracker.
chaviw4244e032019-09-04 11:27:49 -0700303 nsecs_t desiredPresentTime = mBufferInfo.mDesiredPresentTime;
Marissa Wallfd668622018-05-10 10:21:13 -0700304 mFrameTracker.setDesiredPresentTime(desiredPresentTime);
305
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700306 const int32_t layerID = getSequence();
Yiwei Zhang7e666a52018-11-15 13:33:42 -0800307 mFlinger->mTimeStats->setDesiredTime(layerID, mCurrentFrameNumber, desiredPresentTime);
Marissa Wallfd668622018-05-10 10:21:13 -0700308
chaviw4244e032019-09-04 11:27:49 -0700309 std::shared_ptr<FenceTime> frameReadyFence = mBufferInfo.mFenceTime;
Marissa Wallfd668622018-05-10 10:21:13 -0700310 if (frameReadyFence->isValid()) {
311 mFrameTracker.setFrameReadyFence(std::move(frameReadyFence));
312 } else {
313 // There was no fence for this frame, so assume that it was ready
314 // to be presented at the desired present time.
315 mFrameTracker.setFrameReadyTime(desiredPresentTime);
Dominik Laskowski45de9bd2018-06-11 17:44:10 -0700316 }
Marissa Wallfd668622018-05-10 10:21:13 -0700317
318 if (presentFence->isValid()) {
Yiwei Zhang7e666a52018-11-15 13:33:42 -0800319 mFlinger->mTimeStats->setPresentFence(layerID, mCurrentFrameNumber, presentFence);
Mikael Pessa90092f42019-08-26 17:22:04 -0700320 mFlinger->mFrameTracer->traceFence(layerID, getCurrentBufferId(), mCurrentFrameNumber,
321 presentFence, FrameTracer::FrameEvent::PRESENT_FENCE);
Marissa Wallfd668622018-05-10 10:21:13 -0700322 mFrameTracker.setActualPresentFence(std::shared_ptr<FenceTime>(presentFence));
Dominik Laskowski075d3172018-05-24 15:50:06 -0700323 } else if (displayId && mFlinger->getHwComposer().isConnected(*displayId)) {
Marissa Wallfd668622018-05-10 10:21:13 -0700324 // The HWC doesn't support present fences, so use the refresh
325 // timestamp instead.
Dominik Laskowski075d3172018-05-24 15:50:06 -0700326 const nsecs_t actualPresentTime = mFlinger->getHwComposer().getRefreshTimestamp(*displayId);
Yiwei Zhang7e666a52018-11-15 13:33:42 -0800327 mFlinger->mTimeStats->setPresentTime(layerID, mCurrentFrameNumber, actualPresentTime);
Mikael Pessa90092f42019-08-26 17:22:04 -0700328 mFlinger->mFrameTracer->traceTimestamp(layerID, getCurrentBufferId(), mCurrentFrameNumber,
329 actualPresentTime,
330 FrameTracer::FrameEvent::PRESENT_FENCE);
Marissa Wallfd668622018-05-10 10:21:13 -0700331 mFrameTracker.setActualPresentTime(actualPresentTime);
332 }
333
334 mFrameTracker.advanceFrame();
335 mFrameLatencyNeeded = false;
336 return true;
David Sodman0c69cad2017-08-21 12:12:51 -0700337}
338
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700339bool BufferLayer::latchBuffer(bool& recomputeVisibleRegions, nsecs_t latchTime,
340 nsecs_t expectedPresentTime) {
Marissa Wallfd668622018-05-10 10:21:13 -0700341 ATRACE_CALL();
David Sodman0c69cad2017-08-21 12:12:51 -0700342
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800343 bool refreshRequired = latchSidebandStream(recomputeVisibleRegions);
David Sodman0c69cad2017-08-21 12:12:51 -0700344
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800345 if (refreshRequired) {
346 return refreshRequired;
David Sodman0c69cad2017-08-21 12:12:51 -0700347 }
348
Marissa Wallfd668622018-05-10 10:21:13 -0700349 if (!hasReadyFrame()) {
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800350 return false;
David Sodman0c69cad2017-08-21 12:12:51 -0700351 }
David Sodman0c69cad2017-08-21 12:12:51 -0700352
Marissa Wallfd668622018-05-10 10:21:13 -0700353 // if we've already called updateTexImage() without going through
354 // a composition step, we have to skip this layer at this point
355 // because we cannot call updateTeximage() without a corresponding
356 // compositionComplete() call.
357 // we'll trigger an update in onPreComposition().
358 if (mRefreshPending) {
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800359 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700360 }
361
362 // If the head buffer's acquire fence hasn't signaled yet, return and
363 // try again later
364 if (!fenceHasSignaled()) {
Ady Abraham09bd3922019-04-08 10:44:56 -0700365 ATRACE_NAME("!fenceHasSignaled()");
David Sodman0c69cad2017-08-21 12:12:51 -0700366 mFlinger->signalLayerUpdate();
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800367 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700368 }
369
370 // Capture the old state of the layer for comparisons later
371 const State& s(getDrawingState());
372 const bool oldOpacity = isOpaque(s);
chaviwd62d3062019-09-04 14:48:02 -0700373
374 BufferInfo oldBufferInfo = mBufferInfo;
Marissa Wallfd668622018-05-10 10:21:13 -0700375
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700376 if (!allTransactionsSignaled(expectedPresentTime)) {
Marissa Wallebb486e2019-05-15 14:08:08 -0700377 mFlinger->setTransactionFlags(eTraversalNeeded);
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800378 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700379 }
380
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700381 status_t err = updateTexImage(recomputeVisibleRegions, latchTime, expectedPresentTime);
Marissa Wallfd668622018-05-10 10:21:13 -0700382 if (err != NO_ERROR) {
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800383 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700384 }
385
386 err = updateActiveBuffer();
387 if (err != NO_ERROR) {
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800388 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700389 }
390
Marissa Wallfd668622018-05-10 10:21:13 -0700391 err = updateFrameNumber(latchTime);
392 if (err != NO_ERROR) {
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800393 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700394 }
395
chaviw4244e032019-09-04 11:27:49 -0700396 gatherBufferInfo();
397
Marissa Wallfd668622018-05-10 10:21:13 -0700398 mRefreshPending = true;
399 mFrameLatencyNeeded = true;
chaviwd62d3062019-09-04 14:48:02 -0700400 if (oldBufferInfo.mBuffer == nullptr) {
Marissa Wallfd668622018-05-10 10:21:13 -0700401 // the first time we receive a buffer, we need to trigger a
402 // geometry invalidation.
403 recomputeVisibleRegions = true;
404 }
405
chaviw4244e032019-09-04 11:27:49 -0700406 if ((mBufferInfo.mCrop != oldBufferInfo.mCrop) ||
407 (mBufferInfo.mTransform != oldBufferInfo.mTransform) ||
408 (mBufferInfo.mScaleMode != oldBufferInfo.mScaleMode) ||
409 (mBufferInfo.mTransformToDisplayInverse != oldBufferInfo.mTransformToDisplayInverse)) {
Marissa Wallfd668622018-05-10 10:21:13 -0700410 recomputeVisibleRegions = true;
411 }
412
chaviwd62d3062019-09-04 14:48:02 -0700413 if (oldBufferInfo.mBuffer != nullptr) {
414 uint32_t bufWidth = mBufferInfo.mBuffer->getWidth();
415 uint32_t bufHeight = mBufferInfo.mBuffer->getHeight();
416 if (bufWidth != uint32_t(oldBufferInfo.mBuffer->width) ||
417 bufHeight != uint32_t(oldBufferInfo.mBuffer->height)) {
Marissa Wallfd668622018-05-10 10:21:13 -0700418 recomputeVisibleRegions = true;
419 }
420 }
421
422 if (oldOpacity != isOpaque(s)) {
423 recomputeVisibleRegions = true;
424 }
425
426 // Remove any sync points corresponding to the buffer which was just
427 // latched
428 {
429 Mutex::Autolock lock(mLocalSyncPointMutex);
430 auto point = mLocalSyncPoints.begin();
431 while (point != mLocalSyncPoints.end()) {
432 if (!(*point)->frameIsAvailable() || !(*point)->transactionIsApplied()) {
433 // This sync point must have been added since we started
434 // latching. Don't drop it yet.
435 ++point;
436 continue;
437 }
438
439 if ((*point)->getFrameNumber() <= mCurrentFrameNumber) {
Alec Mourie60041e2019-06-14 18:59:51 -0700440 std::stringstream ss;
441 ss << "Dropping sync point " << (*point)->getFrameNumber();
442 ATRACE_NAME(ss.str().c_str());
Marissa Wallfd668622018-05-10 10:21:13 -0700443 point = mLocalSyncPoints.erase(point);
444 } else {
445 ++point;
446 }
447 }
448 }
449
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800450 return true;
Marissa Wallfd668622018-05-10 10:21:13 -0700451}
452
453// transaction
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700454void BufferLayer::notifyAvailableFrames(nsecs_t expectedPresentTime) {
455 const auto headFrameNumber = getHeadFrameNumber(expectedPresentTime);
Ady Abrahamcd1580c2019-04-29 15:40:03 -0700456 const bool headFenceSignaled = fenceHasSignaled();
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700457 const bool presentTimeIsCurrent = framePresentTimeIsCurrent(expectedPresentTime);
Marissa Wallfd668622018-05-10 10:21:13 -0700458 Mutex::Autolock lock(mLocalSyncPointMutex);
459 for (auto& point : mLocalSyncPoints) {
Ady Abrahamcd1580c2019-04-29 15:40:03 -0700460 if (headFrameNumber >= point->getFrameNumber() && headFenceSignaled &&
461 presentTimeIsCurrent) {
Marissa Wallfd668622018-05-10 10:21:13 -0700462 point->setFrameAvailable();
chaviw43cb3cb2019-05-31 15:23:41 -0700463 sp<Layer> requestedSyncLayer = point->getRequestedSyncLayer();
464 if (requestedSyncLayer) {
465 // Need to update the transaction flag to ensure the layer's pending transaction
466 // gets applied.
467 requestedSyncLayer->setTransactionFlags(eTransactionNeeded);
468 }
Marissa Wallfd668622018-05-10 10:21:13 -0700469 }
David Sodman0c69cad2017-08-21 12:12:51 -0700470 }
471}
472
Marissa Wallfd668622018-05-10 10:21:13 -0700473bool BufferLayer::hasReadyFrame() const {
Marissa Wall024a1912018-08-13 13:55:35 -0700474 return hasFrameUpdate() || getSidebandStreamChanged() || getAutoRefresh();
Marissa Wallfd668622018-05-10 10:21:13 -0700475}
476
477uint32_t BufferLayer::getEffectiveScalingMode() const {
478 if (mOverrideScalingMode >= 0) {
479 return mOverrideScalingMode;
480 }
481
chaviw4244e032019-09-04 11:27:49 -0700482 return mBufferInfo.mScaleMode;
Marissa Wallfd668622018-05-10 10:21:13 -0700483}
484
485bool BufferLayer::isProtected() const {
chaviwd62d3062019-09-04 14:48:02 -0700486 const sp<GraphicBuffer>& buffer(mBufferInfo.mBuffer);
Marissa Wallfd668622018-05-10 10:21:13 -0700487 return (buffer != 0) && (buffer->getUsage() & GRALLOC_USAGE_PROTECTED);
488}
489
490bool BufferLayer::latchUnsignaledBuffers() {
491 static bool propertyLoaded = false;
492 static bool latch = false;
493 static std::mutex mutex;
494 std::lock_guard<std::mutex> lock(mutex);
495 if (!propertyLoaded) {
496 char value[PROPERTY_VALUE_MAX] = {};
497 property_get("debug.sf.latch_unsignaled", value, "0");
498 latch = atoi(value);
499 propertyLoaded = true;
500 }
501 return latch;
502}
503
504// h/w composer set-up
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700505bool BufferLayer::allTransactionsSignaled(nsecs_t expectedPresentTime) {
506 const auto headFrameNumber = getHeadFrameNumber(expectedPresentTime);
Marissa Wallfd668622018-05-10 10:21:13 -0700507 bool matchingFramesFound = false;
508 bool allTransactionsApplied = true;
509 Mutex::Autolock lock(mLocalSyncPointMutex);
510
511 for (auto& point : mLocalSyncPoints) {
512 if (point->getFrameNumber() > headFrameNumber) {
513 break;
514 }
515 matchingFramesFound = true;
516
517 if (!point->frameIsAvailable()) {
518 // We haven't notified the remote layer that the frame for
519 // this point is available yet. Notify it now, and then
520 // abort this attempt to latch.
521 point->setFrameAvailable();
522 allTransactionsApplied = false;
523 break;
524 }
525
526 allTransactionsApplied = allTransactionsApplied && point->transactionIsApplied();
527 }
528 return !matchingFramesFound || allTransactionsApplied;
David Sodman0c69cad2017-08-21 12:12:51 -0700529}
530
531// As documented in libhardware header, formats in the range
532// 0x100 - 0x1FF are specific to the HAL implementation, and
533// are known to have no alpha channel
534// TODO: move definition for device-specific range into
535// hardware.h, instead of using hard-coded values here.
536#define HARDWARE_IS_DEVICE_FORMAT(f) ((f) >= 0x100 && (f) <= 0x1FF)
537
538bool BufferLayer::getOpacityForFormat(uint32_t format) {
539 if (HARDWARE_IS_DEVICE_FORMAT(format)) {
540 return true;
541 }
542 switch (format) {
543 case HAL_PIXEL_FORMAT_RGBA_8888:
544 case HAL_PIXEL_FORMAT_BGRA_8888:
545 case HAL_PIXEL_FORMAT_RGBA_FP16:
546 case HAL_PIXEL_FORMAT_RGBA_1010102:
547 return false;
548 }
549 // in all other case, we have no blending (also for unknown formats)
550 return true;
551}
552
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800553bool BufferLayer::needsFiltering(const sp<const DisplayDevice>& displayDevice) const {
Lloyd Piquef16688f2019-02-19 17:47:57 -0800554 // If we are not capturing based on the state of a known display device,
555 // just return false.
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800556 if (displayDevice == nullptr) {
Lloyd Piquef16688f2019-02-19 17:47:57 -0800557 return false;
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800558 }
559
560 const auto outputLayer = findOutputLayerForDisplay(displayDevice);
561 if (outputLayer == nullptr) {
Lloyd Piquef16688f2019-02-19 17:47:57 -0800562 return false;
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800563 }
564
Lloyd Piquef16688f2019-02-19 17:47:57 -0800565 // We need filtering if the sourceCrop rectangle size does not match the
566 // displayframe rectangle size (not a 1:1 render)
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800567 const auto& compositionState = outputLayer->getState();
568 const auto displayFrame = compositionState.displayFrame;
569 const auto sourceCrop = compositionState.sourceCrop;
Lloyd Piquef16688f2019-02-19 17:47:57 -0800570 return sourceCrop.getHeight() != displayFrame.getHeight() ||
Peiyong Linc2020ca2019-01-10 11:36:12 -0800571 sourceCrop.getWidth() != displayFrame.getWidth();
Chia-I Wu692e0832018-06-05 15:46:58 -0700572}
573
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700574uint64_t BufferLayer::getHeadFrameNumber(nsecs_t expectedPresentTime) const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800575 if (hasFrameUpdate()) {
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700576 return getFrameNumber(expectedPresentTime);
David Sodman0c69cad2017-08-21 12:12:51 -0700577 } else {
578 return mCurrentFrameNumber;
579 }
580}
581
Vishnu Nair60356342018-11-13 13:00:45 -0800582Rect BufferLayer::getBufferSize(const State& s) const {
583 // If we have a sideband stream, or we are scaling the buffer then return the layer size since
584 // we cannot determine the buffer size.
585 if ((s.sidebandStream != nullptr) ||
586 (getEffectiveScalingMode() != NATIVE_WINDOW_SCALING_MODE_FREEZE)) {
587 return Rect(getActiveWidth(s), getActiveHeight(s));
588 }
589
chaviwd62d3062019-09-04 14:48:02 -0700590 if (mBufferInfo.mBuffer == nullptr) {
Vishnu Nair60356342018-11-13 13:00:45 -0800591 return Rect::INVALID_RECT;
592 }
593
chaviwd62d3062019-09-04 14:48:02 -0700594 uint32_t bufWidth = mBufferInfo.mBuffer->getWidth();
595 uint32_t bufHeight = mBufferInfo.mBuffer->getHeight();
Vishnu Nair60356342018-11-13 13:00:45 -0800596
597 // Undo any transformations on the buffer and return the result.
chaviw4244e032019-09-04 11:27:49 -0700598 if (mBufferInfo.mTransform & ui::Transform::ROT_90) {
Vishnu Nair60356342018-11-13 13:00:45 -0800599 std::swap(bufWidth, bufHeight);
600 }
601
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800602 if (getTransformToDisplayInverse()) {
Vishnu Nair60356342018-11-13 13:00:45 -0800603 uint32_t invTransform = DisplayDevice::getPrimaryDisplayOrientationTransform();
604 if (invTransform & ui::Transform::ROT_90) {
605 std::swap(bufWidth, bufHeight);
606 }
607 }
608
609 return Rect(bufWidth, bufHeight);
610}
611
Lloyd Piquefeb73d72018-12-04 17:23:44 -0800612std::shared_ptr<compositionengine::Layer> BufferLayer::getCompositionLayer() const {
613 return mCompositionLayer;
614}
615
Vishnu Nair4351ad52019-02-11 14:13:02 -0800616FloatRect BufferLayer::computeSourceBounds(const FloatRect& parentBounds) const {
617 const State& s(getDrawingState());
618
619 // If we have a sideband stream, or we are scaling the buffer then return the layer size since
620 // we cannot determine the buffer size.
621 if ((s.sidebandStream != nullptr) ||
622 (getEffectiveScalingMode() != NATIVE_WINDOW_SCALING_MODE_FREEZE)) {
623 return FloatRect(0, 0, getActiveWidth(s), getActiveHeight(s));
624 }
625
chaviwd62d3062019-09-04 14:48:02 -0700626 if (mBufferInfo.mBuffer == nullptr) {
Vishnu Nair4351ad52019-02-11 14:13:02 -0800627 return parentBounds;
628 }
629
chaviwd62d3062019-09-04 14:48:02 -0700630 uint32_t bufWidth = mBufferInfo.mBuffer->getWidth();
631 uint32_t bufHeight = mBufferInfo.mBuffer->getHeight();
Vishnu Nair4351ad52019-02-11 14:13:02 -0800632
633 // Undo any transformations on the buffer and return the result.
chaviw4244e032019-09-04 11:27:49 -0700634 if (mBufferInfo.mTransform & ui::Transform::ROT_90) {
Vishnu Nair4351ad52019-02-11 14:13:02 -0800635 std::swap(bufWidth, bufHeight);
636 }
637
638 if (getTransformToDisplayInverse()) {
639 uint32_t invTransform = DisplayDevice::getPrimaryDisplayOrientationTransform();
640 if (invTransform & ui::Transform::ROT_90) {
641 std::swap(bufWidth, bufHeight);
642 }
643 }
644
645 return FloatRect(0, 0, bufWidth, bufHeight);
646}
647
chaviw49a108c2019-08-12 11:23:06 -0700648void BufferLayer::latchAndReleaseBuffer() {
649 mRefreshPending = false;
650 if (hasReadyFrame()) {
651 bool ignored = false;
652 latchBuffer(ignored, systemTime(), 0 /* expectedPresentTime */);
653 }
654 releasePendingBuffer(systemTime());
655}
656
chaviw4244e032019-09-04 11:27:49 -0700657PixelFormat BufferLayer::getPixelFormat() const {
658 return mBufferInfo.mPixelFormat;
659}
660
661bool BufferLayer::getTransformToDisplayInverse() const {
662 return mBufferInfo.mTransformToDisplayInverse;
663}
664
665Rect BufferLayer::getBufferCrop() const {
666 // this is the crop rectangle that applies to the buffer
667 // itself (as opposed to the window)
668 if (!mBufferInfo.mCrop.isEmpty()) {
669 // if the buffer crop is defined, we use that
670 return mBufferInfo.mCrop;
chaviwd62d3062019-09-04 14:48:02 -0700671 } else if (mBufferInfo.mBuffer != nullptr) {
chaviw4244e032019-09-04 11:27:49 -0700672 // otherwise we use the whole buffer
chaviwd62d3062019-09-04 14:48:02 -0700673 return mBufferInfo.mBuffer->getBounds();
chaviw4244e032019-09-04 11:27:49 -0700674 } else {
675 // if we don't have a buffer yet, we use an empty/invalid crop
676 return Rect();
677 }
678}
679
680uint32_t BufferLayer::getBufferTransform() const {
681 return mBufferInfo.mTransform;
682}
683
684ui::Dataspace BufferLayer::getDataSpace() const {
685 return mBufferInfo.mDataspace;
686}
687
688ui::Dataspace BufferLayer::translateDataspace(ui::Dataspace dataspace) {
689 ui::Dataspace updatedDataspace = dataspace;
690 // translate legacy dataspaces to modern dataspaces
691 switch (dataspace) {
692 case ui::Dataspace::SRGB:
693 updatedDataspace = ui::Dataspace::V0_SRGB;
694 break;
695 case ui::Dataspace::SRGB_LINEAR:
696 updatedDataspace = ui::Dataspace::V0_SRGB_LINEAR;
697 break;
698 case ui::Dataspace::JFIF:
699 updatedDataspace = ui::Dataspace::V0_JFIF;
700 break;
701 case ui::Dataspace::BT601_625:
702 updatedDataspace = ui::Dataspace::V0_BT601_625;
703 break;
704 case ui::Dataspace::BT601_525:
705 updatedDataspace = ui::Dataspace::V0_BT601_525;
706 break;
707 case ui::Dataspace::BT709:
708 updatedDataspace = ui::Dataspace::V0_BT709;
709 break;
710 default:
711 break;
712 }
713
714 return updatedDataspace;
715}
716
chaviwd62d3062019-09-04 14:48:02 -0700717sp<GraphicBuffer> BufferLayer::getBuffer() const {
718 return mBufferInfo.mBuffer;
719}
720
David Sodman0c69cad2017-08-21 12:12:51 -0700721} // namespace android
722
723#if defined(__gl_h_)
724#error "don't include gl/gl.h in this file"
725#endif
726
727#if defined(__gl2_h_)
728#error "don't include gl2/gl2.h in this file"
729#endif