blob: 90a02b3ad9f2cac7872614a7015ff7a8a223fbb8 [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 {
Marissa Wallfd668622018-05-10 10:21:13 -070084 surfaceDamageRegion = getDrawingSurfaceDamage();
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.
Lloyd Pique0b785d82018-12-04 17:25:27 -080095 if ((mSidebandStream == nullptr) && (mActiveBuffer == 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 &&
Lloyd Pique0b785d82018-12-04 17:25:27 -0800106 (mActiveBuffer != 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
David Sodman0cf8f8d2017-12-20 18:19:45 -0800147 if (CC_UNLIKELY(mActiveBuffer == 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) {
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000178 layer.source.buffer.buffer = mActiveBuffer;
179 layer.source.buffer.isOpaque = isOpaque(s);
180 layer.source.buffer.fence = mActiveBufferFence;
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);
190 getDrawingTransformMatrix(textureMatrix);
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
257 return (mCurrentDataSpace == ui::Dataspace::BT2020_ITU_PQ &&
258 getDrawingApi() == NATIVE_WINDOW_API_MEDIA &&
Lloyd Pique0b785d82018-12-04 17:25:27 -0800259 mActiveBuffer->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
271 compositionState.hdrMetadata = getDrawingHdrMetadata();
272 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) {
279 if (mBufferLatched) {
280 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.
303 nsecs_t desiredPresentTime = getDesiredPresentTime();
304 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
309 std::shared_ptr<FenceTime> frameReadyFence = getCurrentFenceTime();
310 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);
373 sp<GraphicBuffer> oldBuffer = mActiveBuffer;
374
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
390 mBufferLatched = true;
391
392 err = updateFrameNumber(latchTime);
393 if (err != NO_ERROR) {
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800394 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700395 }
396
397 mRefreshPending = true;
398 mFrameLatencyNeeded = true;
399 if (oldBuffer == nullptr) {
400 // the first time we receive a buffer, we need to trigger a
401 // geometry invalidation.
402 recomputeVisibleRegions = true;
403 }
404
405 ui::Dataspace dataSpace = getDrawingDataSpace();
Peiyong Lin14724e62018-12-05 07:27:30 -0800406 // translate legacy dataspaces to modern dataspaces
Marissa Wallfd668622018-05-10 10:21:13 -0700407 switch (dataSpace) {
Peiyong Lin14724e62018-12-05 07:27:30 -0800408 case ui::Dataspace::SRGB:
409 dataSpace = ui::Dataspace::V0_SRGB;
Marissa Wallfd668622018-05-10 10:21:13 -0700410 break;
Peiyong Lin14724e62018-12-05 07:27:30 -0800411 case ui::Dataspace::SRGB_LINEAR:
412 dataSpace = ui::Dataspace::V0_SRGB_LINEAR;
Marissa Wallfd668622018-05-10 10:21:13 -0700413 break;
Peiyong Lin14724e62018-12-05 07:27:30 -0800414 case ui::Dataspace::JFIF:
415 dataSpace = ui::Dataspace::V0_JFIF;
Marissa Wallfd668622018-05-10 10:21:13 -0700416 break;
Peiyong Lin14724e62018-12-05 07:27:30 -0800417 case ui::Dataspace::BT601_625:
418 dataSpace = ui::Dataspace::V0_BT601_625;
Marissa Wallfd668622018-05-10 10:21:13 -0700419 break;
Peiyong Lin14724e62018-12-05 07:27:30 -0800420 case ui::Dataspace::BT601_525:
421 dataSpace = ui::Dataspace::V0_BT601_525;
Marissa Wallfd668622018-05-10 10:21:13 -0700422 break;
Peiyong Lin14724e62018-12-05 07:27:30 -0800423 case ui::Dataspace::BT709:
424 dataSpace = ui::Dataspace::V0_BT709;
Marissa Wallfd668622018-05-10 10:21:13 -0700425 break;
426 default:
427 break;
428 }
429 mCurrentDataSpace = dataSpace;
430
431 Rect crop(getDrawingCrop());
432 const uint32_t transform(getDrawingTransform());
433 const uint32_t scalingMode(getDrawingScalingMode());
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800434 const bool transformToDisplayInverse(getTransformToDisplayInverse());
Marissa Wallfd668622018-05-10 10:21:13 -0700435 if ((crop != mCurrentCrop) || (transform != mCurrentTransform) ||
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800436 (scalingMode != mCurrentScalingMode) ||
437 (transformToDisplayInverse != mTransformToDisplayInverse)) {
Marissa Wallfd668622018-05-10 10:21:13 -0700438 mCurrentCrop = crop;
439 mCurrentTransform = transform;
440 mCurrentScalingMode = scalingMode;
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800441 mTransformToDisplayInverse = transformToDisplayInverse;
Marissa Wallfd668622018-05-10 10:21:13 -0700442 recomputeVisibleRegions = true;
443 }
444
445 if (oldBuffer != nullptr) {
446 uint32_t bufWidth = mActiveBuffer->getWidth();
447 uint32_t bufHeight = mActiveBuffer->getHeight();
448 if (bufWidth != uint32_t(oldBuffer->width) || bufHeight != uint32_t(oldBuffer->height)) {
449 recomputeVisibleRegions = true;
450 }
451 }
452
453 if (oldOpacity != isOpaque(s)) {
454 recomputeVisibleRegions = true;
455 }
456
457 // Remove any sync points corresponding to the buffer which was just
458 // latched
459 {
460 Mutex::Autolock lock(mLocalSyncPointMutex);
461 auto point = mLocalSyncPoints.begin();
462 while (point != mLocalSyncPoints.end()) {
463 if (!(*point)->frameIsAvailable() || !(*point)->transactionIsApplied()) {
464 // This sync point must have been added since we started
465 // latching. Don't drop it yet.
466 ++point;
467 continue;
468 }
469
470 if ((*point)->getFrameNumber() <= mCurrentFrameNumber) {
Alec Mourie60041e2019-06-14 18:59:51 -0700471 std::stringstream ss;
472 ss << "Dropping sync point " << (*point)->getFrameNumber();
473 ATRACE_NAME(ss.str().c_str());
Marissa Wallfd668622018-05-10 10:21:13 -0700474 point = mLocalSyncPoints.erase(point);
475 } else {
476 ++point;
477 }
478 }
479 }
480
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800481 return true;
Marissa Wallfd668622018-05-10 10:21:13 -0700482}
483
484// transaction
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700485void BufferLayer::notifyAvailableFrames(nsecs_t expectedPresentTime) {
486 const auto headFrameNumber = getHeadFrameNumber(expectedPresentTime);
Ady Abrahamcd1580c2019-04-29 15:40:03 -0700487 const bool headFenceSignaled = fenceHasSignaled();
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700488 const bool presentTimeIsCurrent = framePresentTimeIsCurrent(expectedPresentTime);
Marissa Wallfd668622018-05-10 10:21:13 -0700489 Mutex::Autolock lock(mLocalSyncPointMutex);
490 for (auto& point : mLocalSyncPoints) {
Ady Abrahamcd1580c2019-04-29 15:40:03 -0700491 if (headFrameNumber >= point->getFrameNumber() && headFenceSignaled &&
492 presentTimeIsCurrent) {
Marissa Wallfd668622018-05-10 10:21:13 -0700493 point->setFrameAvailable();
chaviw43cb3cb2019-05-31 15:23:41 -0700494 sp<Layer> requestedSyncLayer = point->getRequestedSyncLayer();
495 if (requestedSyncLayer) {
496 // Need to update the transaction flag to ensure the layer's pending transaction
497 // gets applied.
498 requestedSyncLayer->setTransactionFlags(eTransactionNeeded);
499 }
Marissa Wallfd668622018-05-10 10:21:13 -0700500 }
David Sodman0c69cad2017-08-21 12:12:51 -0700501 }
502}
503
Marissa Wallfd668622018-05-10 10:21:13 -0700504bool BufferLayer::hasReadyFrame() const {
Marissa Wall024a1912018-08-13 13:55:35 -0700505 return hasFrameUpdate() || getSidebandStreamChanged() || getAutoRefresh();
Marissa Wallfd668622018-05-10 10:21:13 -0700506}
507
508uint32_t BufferLayer::getEffectiveScalingMode() const {
509 if (mOverrideScalingMode >= 0) {
510 return mOverrideScalingMode;
511 }
512
513 return mCurrentScalingMode;
514}
515
516bool BufferLayer::isProtected() const {
517 const sp<GraphicBuffer>& buffer(mActiveBuffer);
518 return (buffer != 0) && (buffer->getUsage() & GRALLOC_USAGE_PROTECTED);
519}
520
521bool BufferLayer::latchUnsignaledBuffers() {
522 static bool propertyLoaded = false;
523 static bool latch = false;
524 static std::mutex mutex;
525 std::lock_guard<std::mutex> lock(mutex);
526 if (!propertyLoaded) {
527 char value[PROPERTY_VALUE_MAX] = {};
528 property_get("debug.sf.latch_unsignaled", value, "0");
529 latch = atoi(value);
530 propertyLoaded = true;
531 }
532 return latch;
533}
534
535// h/w composer set-up
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700536bool BufferLayer::allTransactionsSignaled(nsecs_t expectedPresentTime) {
537 const auto headFrameNumber = getHeadFrameNumber(expectedPresentTime);
Marissa Wallfd668622018-05-10 10:21:13 -0700538 bool matchingFramesFound = false;
539 bool allTransactionsApplied = true;
540 Mutex::Autolock lock(mLocalSyncPointMutex);
541
542 for (auto& point : mLocalSyncPoints) {
543 if (point->getFrameNumber() > headFrameNumber) {
544 break;
545 }
546 matchingFramesFound = true;
547
548 if (!point->frameIsAvailable()) {
549 // We haven't notified the remote layer that the frame for
550 // this point is available yet. Notify it now, and then
551 // abort this attempt to latch.
552 point->setFrameAvailable();
553 allTransactionsApplied = false;
554 break;
555 }
556
557 allTransactionsApplied = allTransactionsApplied && point->transactionIsApplied();
558 }
559 return !matchingFramesFound || allTransactionsApplied;
David Sodman0c69cad2017-08-21 12:12:51 -0700560}
561
562// As documented in libhardware header, formats in the range
563// 0x100 - 0x1FF are specific to the HAL implementation, and
564// are known to have no alpha channel
565// TODO: move definition for device-specific range into
566// hardware.h, instead of using hard-coded values here.
567#define HARDWARE_IS_DEVICE_FORMAT(f) ((f) >= 0x100 && (f) <= 0x1FF)
568
569bool BufferLayer::getOpacityForFormat(uint32_t format) {
570 if (HARDWARE_IS_DEVICE_FORMAT(format)) {
571 return true;
572 }
573 switch (format) {
574 case HAL_PIXEL_FORMAT_RGBA_8888:
575 case HAL_PIXEL_FORMAT_BGRA_8888:
576 case HAL_PIXEL_FORMAT_RGBA_FP16:
577 case HAL_PIXEL_FORMAT_RGBA_1010102:
578 return false;
579 }
580 // in all other case, we have no blending (also for unknown formats)
581 return true;
582}
583
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800584bool BufferLayer::needsFiltering(const sp<const DisplayDevice>& displayDevice) const {
Lloyd Piquef16688f2019-02-19 17:47:57 -0800585 // If we are not capturing based on the state of a known display device,
586 // just return false.
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800587 if (displayDevice == nullptr) {
Lloyd Piquef16688f2019-02-19 17:47:57 -0800588 return false;
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800589 }
590
591 const auto outputLayer = findOutputLayerForDisplay(displayDevice);
592 if (outputLayer == nullptr) {
Lloyd Piquef16688f2019-02-19 17:47:57 -0800593 return false;
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800594 }
595
Lloyd Piquef16688f2019-02-19 17:47:57 -0800596 // We need filtering if the sourceCrop rectangle size does not match the
597 // displayframe rectangle size (not a 1:1 render)
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800598 const auto& compositionState = outputLayer->getState();
599 const auto displayFrame = compositionState.displayFrame;
600 const auto sourceCrop = compositionState.sourceCrop;
Lloyd Piquef16688f2019-02-19 17:47:57 -0800601 return sourceCrop.getHeight() != displayFrame.getHeight() ||
Peiyong Linc2020ca2019-01-10 11:36:12 -0800602 sourceCrop.getWidth() != displayFrame.getWidth();
Chia-I Wu692e0832018-06-05 15:46:58 -0700603}
604
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700605uint64_t BufferLayer::getHeadFrameNumber(nsecs_t expectedPresentTime) const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800606 if (hasFrameUpdate()) {
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700607 return getFrameNumber(expectedPresentTime);
David Sodman0c69cad2017-08-21 12:12:51 -0700608 } else {
609 return mCurrentFrameNumber;
610 }
611}
612
Vishnu Nair60356342018-11-13 13:00:45 -0800613Rect BufferLayer::getBufferSize(const State& s) const {
614 // If we have a sideband stream, or we are scaling the buffer then return the layer size since
615 // we cannot determine the buffer size.
616 if ((s.sidebandStream != nullptr) ||
617 (getEffectiveScalingMode() != NATIVE_WINDOW_SCALING_MODE_FREEZE)) {
618 return Rect(getActiveWidth(s), getActiveHeight(s));
619 }
620
621 if (mActiveBuffer == nullptr) {
622 return Rect::INVALID_RECT;
623 }
624
625 uint32_t bufWidth = mActiveBuffer->getWidth();
626 uint32_t bufHeight = mActiveBuffer->getHeight();
627
628 // Undo any transformations on the buffer and return the result.
629 if (mCurrentTransform & ui::Transform::ROT_90) {
630 std::swap(bufWidth, bufHeight);
631 }
632
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800633 if (getTransformToDisplayInverse()) {
Vishnu Nair60356342018-11-13 13:00:45 -0800634 uint32_t invTransform = DisplayDevice::getPrimaryDisplayOrientationTransform();
635 if (invTransform & ui::Transform::ROT_90) {
636 std::swap(bufWidth, bufHeight);
637 }
638 }
639
640 return Rect(bufWidth, bufHeight);
641}
642
Lloyd Piquefeb73d72018-12-04 17:23:44 -0800643std::shared_ptr<compositionengine::Layer> BufferLayer::getCompositionLayer() const {
644 return mCompositionLayer;
645}
646
Vishnu Nair4351ad52019-02-11 14:13:02 -0800647FloatRect BufferLayer::computeSourceBounds(const FloatRect& parentBounds) const {
648 const State& s(getDrawingState());
649
650 // If we have a sideband stream, or we are scaling the buffer then return the layer size since
651 // we cannot determine the buffer size.
652 if ((s.sidebandStream != nullptr) ||
653 (getEffectiveScalingMode() != NATIVE_WINDOW_SCALING_MODE_FREEZE)) {
654 return FloatRect(0, 0, getActiveWidth(s), getActiveHeight(s));
655 }
656
657 if (mActiveBuffer == nullptr) {
658 return parentBounds;
659 }
660
661 uint32_t bufWidth = mActiveBuffer->getWidth();
662 uint32_t bufHeight = mActiveBuffer->getHeight();
663
664 // Undo any transformations on the buffer and return the result.
665 if (mCurrentTransform & ui::Transform::ROT_90) {
666 std::swap(bufWidth, bufHeight);
667 }
668
669 if (getTransformToDisplayInverse()) {
670 uint32_t invTransform = DisplayDevice::getPrimaryDisplayOrientationTransform();
671 if (invTransform & ui::Transform::ROT_90) {
672 std::swap(bufWidth, bufHeight);
673 }
674 }
675
676 return FloatRect(0, 0, bufWidth, bufHeight);
677}
678
chaviw49a108c2019-08-12 11:23:06 -0700679void BufferLayer::latchAndReleaseBuffer() {
680 mRefreshPending = false;
681 if (hasReadyFrame()) {
682 bool ignored = false;
683 latchBuffer(ignored, systemTime(), 0 /* expectedPresentTime */);
684 }
685 releasePendingBuffer(systemTime());
686}
687
David Sodman0c69cad2017-08-21 12:12:51 -0700688} // namespace android
689
690#if defined(__gl_h_)
691#error "don't include gl/gl.h in this file"
692#endif
693
694#if defined(__gl2_h_)
695#error "don't include gl2/gl2.h in this file"
696#endif