blob: b500ad3eee287f1c32791d46ea8ad6e3c0a795ca [file] [log] [blame]
David Sodman0c69cad2017-08-21 12:12:51 -07001/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17//#define LOG_NDEBUG 0
18#undef LOG_TAG
19#define LOG_TAG "BufferLayer"
20#define ATRACE_TAG ATRACE_TAG_GRAPHICS
21
Alec Mourie60041e2019-06-14 18:59:51 -070022#include "BufferLayer.h"
Lloyd Piquefeb73d72018-12-04 17:23:44 -080023
24#include <compositionengine/CompositionEngine.h>
25#include <compositionengine/Layer.h>
26#include <compositionengine/LayerCreationArgs.h>
Lloyd Piquef5275482019-01-29 18:42:42 -080027#include <compositionengine/LayerFECompositionState.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080028#include <compositionengine/OutputLayer.h>
29#include <compositionengine/impl/OutputLayerCompositionState.h>
Lloyd Piquefeb73d72018-12-04 17:23:44 -080030#include <cutils/compiler.h>
31#include <cutils/native_handle.h>
32#include <cutils/properties.h>
33#include <gui/BufferItem.h>
34#include <gui/BufferQueue.h>
chaviwf83ce182019-09-12 14:43:08 -070035#include <gui/GLConsumer.h>
Lloyd Piquefeb73d72018-12-04 17:23:44 -080036#include <gui/LayerDebugInfo.h>
37#include <gui/Surface.h>
38#include <renderengine/RenderEngine.h>
39#include <ui/DebugUtils.h>
40#include <utils/Errors.h>
41#include <utils/Log.h>
42#include <utils/NativeHandle.h>
43#include <utils/StopWatch.h>
44#include <utils/Trace.h>
45
Alec Mourie60041e2019-06-14 18:59:51 -070046#include <cmath>
47#include <cstdlib>
48#include <mutex>
49#include <sstream>
50
David Sodman0c69cad2017-08-21 12:12:51 -070051#include "Colorizer.h"
52#include "DisplayDevice.h"
Mikael Pessa90092f42019-08-26 17:22:04 -070053#include "FrameTracer/FrameTracer.h"
David Sodman0c69cad2017-08-21 12:12:51 -070054#include "LayerRejecter.h"
Yiwei Zhang7e666a52018-11-15 13:33:42 -080055#include "TimeStats/TimeStats.h"
56
David Sodman0c69cad2017-08-21 12:12:51 -070057namespace android {
58
Lloyd Pique42ab75e2018-09-12 20:46:03 -070059BufferLayer::BufferLayer(const LayerCreationArgs& args)
Lloyd Piquefeb73d72018-12-04 17:23:44 -080060 : Layer(args),
chaviwb4c6e582019-08-16 14:35:07 -070061 mTextureName(args.textureName),
Lloyd Piquefeb73d72018-12-04 17:23:44 -080062 mCompositionLayer{mFlinger->getCompositionEngine().createLayer(
63 compositionengine::LayerCreationArgs{this})} {
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() {
chaviwb4c6e582019-08-16 14:35:07 -070073 if (!isClone()) {
74 // The original layer and the clone layer share the same texture. Therefore, only one of
75 // the layers, in this case the original layer, needs to handle the deletion. The original
76 // layer and the clone should be removed at the same time so there shouldn't be any issue
77 // with the clone layer trying to use the deleted texture.
78 mFlinger->deleteTextureAsync(mTextureName);
79 }
Mikael Pessa90092f42019-08-26 17:22:04 -070080 const int32_t layerID = getSequence();
81 mFlinger->mTimeStats->onDestroy(layerID);
82 mFlinger->mFrameTracer->onDestroy(layerID);
David Sodman0c69cad2017-08-21 12:12:51 -070083}
84
David Sodmaneb085e02017-10-05 18:49:04 -070085void BufferLayer::useSurfaceDamage() {
86 if (mFlinger->mForceFullDamage) {
87 surfaceDamageRegion = Region::INVALID_REGION;
88 } else {
chaviw4244e032019-09-04 11:27:49 -070089 surfaceDamageRegion = mBufferInfo.mSurfaceDamage;
David Sodmaneb085e02017-10-05 18:49:04 -070090 }
91}
92
93void BufferLayer::useEmptyDamage() {
94 surfaceDamageRegion.clear();
95}
96
Marissa Wallfd668622018-05-10 10:21:13 -070097bool BufferLayer::isOpaque(const Layer::State& s) const {
98 // if we don't have a buffer or sidebandStream yet, we're translucent regardless of the
99 // layer's opaque flag.
chaviwd62d3062019-09-04 14:48:02 -0700100 if ((mSidebandStream == nullptr) && (mBufferInfo.mBuffer == nullptr)) {
Marissa Wallfd668622018-05-10 10:21:13 -0700101 return false;
102 }
103
104 // if the layer has the opaque flag, then we're always opaque,
105 // otherwise we use the current buffer's format.
106 return ((s.flags & layer_state_t::eLayerOpaque) != 0) || getOpacityForFormat(getPixelFormat());
David Sodman0c69cad2017-08-21 12:12:51 -0700107}
108
109bool BufferLayer::isVisible() const {
Ady Abrahama315ce72019-04-24 14:35:20 -0700110 bool visible = !(isHiddenByPolicy()) && getAlpha() > 0.0f &&
chaviwd62d3062019-09-04 14:48:02 -0700111 (mBufferInfo.mBuffer != nullptr || mSidebandStream != nullptr);
Ady Abrahama315ce72019-04-24 14:35:20 -0700112 mFlinger->mScheduler->setLayerVisibility(mSchedulerLayerHandle, visible);
113
114 return visible;
David Sodman0c69cad2017-08-21 12:12:51 -0700115}
116
117bool BufferLayer::isFixedSize() const {
118 return getEffectiveScalingMode() != NATIVE_WINDOW_SCALING_MODE_FREEZE;
119}
120
Lloyd Piquea83776c2019-01-29 18:42:32 -0800121bool BufferLayer::usesSourceCrop() const {
122 return true;
123}
124
David Sodman0c69cad2017-08-21 12:12:51 -0700125static constexpr mat4 inverseOrientation(uint32_t transform) {
David Sodman41fdfc92017-11-06 16:09:56 -0800126 const mat4 flipH(-1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1);
127 const mat4 flipV(1, 0, 0, 0, 0, -1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1);
128 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 -0700129 mat4 tr;
130
131 if (transform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
132 tr = tr * rot90;
133 }
134 if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_H) {
135 tr = tr * flipH;
136 }
137 if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_V) {
138 tr = tr * flipV;
139 }
140 return inverse(tr);
141}
142
Lloyd Piquef16688f2019-02-19 17:47:57 -0800143std::optional<renderengine::LayerSettings> BufferLayer::prepareClientComposition(
144 compositionengine::LayerFE::ClientCompositionTargetSettings& targetSettings) {
David Sodman0c69cad2017-08-21 12:12:51 -0700145 ATRACE_CALL();
Lloyd Piquef16688f2019-02-19 17:47:57 -0800146
147 auto result = Layer::prepareClientComposition(targetSettings);
148 if (!result) {
149 return result;
150 }
151
chaviwd62d3062019-09-04 14:48:02 -0700152 if (CC_UNLIKELY(mBufferInfo.mBuffer == 0)) {
David Sodman0c69cad2017-08-21 12:12:51 -0700153 // the texture has not been created yet, this Layer has
154 // in fact never been drawn into. This happens frequently with
155 // SurfaceView because the WindowManager can't know when the client
156 // has drawn the first time.
157
158 // If there is nothing under us, we paint the screen in black, otherwise
159 // we just skip this update.
160
161 // figure out if there is something below us
162 Region under;
163 bool finished = false;
164 mFlinger->mDrawingState.traverseInZOrder([&](Layer* layer) {
165 if (finished || layer == static_cast<BufferLayer const*>(this)) {
166 finished = true;
167 return;
168 }
Lloyd Piquea2468662019-03-07 21:31:06 -0800169
170 under.orSelf(layer->getScreenBounds());
David Sodman0c69cad2017-08-21 12:12:51 -0700171 });
172 // if not everything below us is covered, we plug the holes!
Lloyd Piquef16688f2019-02-19 17:47:57 -0800173 Region holes(targetSettings.clip.subtract(under));
David Sodman0c69cad2017-08-21 12:12:51 -0700174 if (!holes.isEmpty()) {
Lloyd Piquef16688f2019-02-19 17:47:57 -0800175 targetSettings.clearRegion.orSelf(holes);
David Sodman0c69cad2017-08-21 12:12:51 -0700176 }
Lloyd Piquef16688f2019-02-19 17:47:57 -0800177 return std::nullopt;
David Sodman0c69cad2017-08-21 12:12:51 -0700178 }
Lloyd Pique688abd42019-02-15 15:42:24 -0800179 bool blackOutLayer = (isProtected() && !targetSettings.supportsProtectedContent) ||
Lloyd Piquef16688f2019-02-19 17:47:57 -0800180 (isSecure() && !targetSettings.isSecure);
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000181 const State& s(getDrawingState());
Lloyd Piquef16688f2019-02-19 17:47:57 -0800182 auto& layer = *result;
David Sodman0c69cad2017-08-21 12:12:51 -0700183 if (!blackOutLayer) {
chaviwd62d3062019-09-04 14:48:02 -0700184 layer.source.buffer.buffer = mBufferInfo.mBuffer;
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000185 layer.source.buffer.isOpaque = isOpaque(s);
chaviwd62d3062019-09-04 14:48:02 -0700186 layer.source.buffer.fence = mBufferInfo.mFence;
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000187 layer.source.buffer.textureName = mTextureName;
188 layer.source.buffer.usePremultipliedAlpha = getPremultipledAlpha();
189 layer.source.buffer.isY410BT2020 = isHdrY410();
David Sodman0c69cad2017-08-21 12:12:51 -0700190 // TODO: we could be more subtle with isFixedSize()
Lloyd Piquef16688f2019-02-19 17:47:57 -0800191 const bool useFiltering = targetSettings.needsFiltering || mNeedsFiltering || isFixedSize();
David Sodman0c69cad2017-08-21 12:12:51 -0700192
193 // Query the texture matrix given our current filtering mode.
194 float textureMatrix[16];
chaviwf83ce182019-09-12 14:43:08 -0700195 getDrawingTransformMatrix(useFiltering, textureMatrix);
David Sodman0c69cad2017-08-21 12:12:51 -0700196
197 if (getTransformToDisplayInverse()) {
198 /*
199 * the code below applies the primary display's inverse transform to
200 * the texture transform
201 */
202 uint32_t transform = DisplayDevice::getPrimaryDisplayOrientationTransform();
203 mat4 tr = inverseOrientation(transform);
204
205 /**
206 * TODO(b/36727915): This is basically a hack.
207 *
208 * Ensure that regardless of the parent transformation,
209 * this buffer is always transformed from native display
210 * orientation to display orientation. For example, in the case
211 * of a camera where the buffer remains in native orientation,
212 * we want the pixels to always be upright.
213 */
214 sp<Layer> p = mDrawingParent.promote();
215 if (p != nullptr) {
216 const auto parentTransform = p->getTransform();
217 tr = tr * inverseOrientation(parentTransform.getOrientation());
218 }
219
220 // and finally apply it to the original texture matrix
221 const mat4 texTransform(mat4(static_cast<const float*>(textureMatrix)) * tr);
222 memcpy(textureMatrix, texTransform.asArray(), sizeof(textureMatrix));
223 }
224
Vishnu Nair4351ad52019-02-11 14:13:02 -0800225 const Rect win{getBounds()};
Marissa Wall290ad082019-03-06 13:23:47 -0800226 float bufferWidth = getBufferSize(s).getWidth();
227 float bufferHeight = getBufferSize(s).getHeight();
228
229 // BufferStateLayers can have a "buffer size" of [0, 0, -1, -1] when no display frame has
230 // been set and there is no parent layer bounds. In that case, the scale is meaningless so
231 // ignore them.
232 if (!getBufferSize(s).isValid()) {
233 bufferWidth = float(win.right) - float(win.left);
234 bufferHeight = float(win.bottom) - float(win.top);
235 }
David Sodman0c69cad2017-08-21 12:12:51 -0700236
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000237 const float scaleHeight = (float(win.bottom) - float(win.top)) / bufferHeight;
238 const float scaleWidth = (float(win.right) - float(win.left)) / bufferWidth;
239 const float translateY = float(win.top) / bufferHeight;
240 const float translateX = float(win.left) / bufferWidth;
241
242 // Flip y-coordinates because GLConsumer expects OpenGL convention.
243 mat4 tr = mat4::translate(vec4(.5, .5, 0, 1)) * mat4::scale(vec4(1, -1, 1, 1)) *
244 mat4::translate(vec4(-.5, -.5, 0, 1)) *
245 mat4::translate(vec4(translateX, translateY, 0, 1)) *
246 mat4::scale(vec4(scaleWidth, scaleHeight, 1.0, 1.0));
247
248 layer.source.buffer.useTextureFiltering = useFiltering;
249 layer.source.buffer.textureTransform = mat4(static_cast<const float*>(textureMatrix)) * tr;
David Sodman0c69cad2017-08-21 12:12:51 -0700250 } else {
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000251 // If layer is blacked out, force alpha to 1 so that we draw a black color
252 // layer.
253 layer.source.buffer.buffer = nullptr;
254 layer.alpha = 1.0;
David Sodman0c69cad2017-08-21 12:12:51 -0700255 }
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000256
Lloyd Piquef16688f2019-02-19 17:47:57 -0800257 return result;
David Sodman0c69cad2017-08-21 12:12:51 -0700258}
259
Marissa Wallfd668622018-05-10 10:21:13 -0700260bool BufferLayer::isHdrY410() const {
261 // pixel format is HDR Y410 masquerading as RGBA_1010102
chaviw4244e032019-09-04 11:27:49 -0700262 return (mBufferInfo.mDataspace == ui::Dataspace::BT2020_ITU_PQ &&
263 mBufferInfo.mApi == NATIVE_WINDOW_API_MEDIA &&
chaviwd62d3062019-09-04 14:48:02 -0700264 mBufferInfo.mBuffer->getPixelFormat() == HAL_PIXEL_FORMAT_RGBA_1010102);
David Sodmaneb085e02017-10-05 18:49:04 -0700265}
266
Lloyd Piquef5275482019-01-29 18:42:42 -0800267void BufferLayer::latchPerFrameState(
268 compositionengine::LayerFECompositionState& compositionState) const {
269 Layer::latchPerFrameState(compositionState);
David Sodman0c69cad2017-08-21 12:12:51 -0700270
271 // Sideband layers
Lloyd Piquef5275482019-01-29 18:42:42 -0800272 if (compositionState.sidebandStream.get()) {
273 compositionState.compositionType = Hwc2::IComposerClient::Composition::SIDEBAND;
David Sodman15094112018-10-11 09:39:37 -0700274 } else {
Lloyd Piquef5275482019-01-29 18:42:42 -0800275 // Normal buffer layers
chaviw4244e032019-09-04 11:27:49 -0700276 compositionState.hdrMetadata = mBufferInfo.mHdrMetadata;
Lloyd Piquef5275482019-01-29 18:42:42 -0800277 compositionState.compositionType = mPotentialCursor
278 ? Hwc2::IComposerClient::Composition::CURSOR
279 : Hwc2::IComposerClient::Composition::DEVICE;
David Sodman0c69cad2017-08-21 12:12:51 -0700280 }
David Sodman0c69cad2017-08-21 12:12:51 -0700281}
282
Marissa Wallfd668622018-05-10 10:21:13 -0700283bool BufferLayer::onPreComposition(nsecs_t refreshStartTime) {
chaviwd62d3062019-09-04 14:48:02 -0700284 if (mBufferInfo.mBuffer != nullptr) {
Marissa Wallfd668622018-05-10 10:21:13 -0700285 Mutex::Autolock lock(mFrameEventHistoryMutex);
286 mFrameEventHistory.addPreComposition(mCurrentFrameNumber, refreshStartTime);
David Sodman0c69cad2017-08-21 12:12:51 -0700287 }
Marissa Wallfd668622018-05-10 10:21:13 -0700288 mRefreshPending = false;
289 return hasReadyFrame();
David Sodman0c69cad2017-08-21 12:12:51 -0700290}
291
Dominik Laskowski075d3172018-05-24 15:50:06 -0700292bool BufferLayer::onPostComposition(const std::optional<DisplayId>& displayId,
293 const std::shared_ptr<FenceTime>& glDoneFence,
Marissa Wallfd668622018-05-10 10:21:13 -0700294 const std::shared_ptr<FenceTime>& presentFence,
295 const CompositorTiming& compositorTiming) {
296 // mFrameLatencyNeeded is true when a new frame was latched for the
297 // composition.
chaviw74b03172019-08-19 11:09:03 -0700298 if (!mBufferInfo.mFrameLatencyNeeded) return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700299
300 // Update mFrameEventHistory.
Dan Stoza436ccf32018-06-21 12:10:12 -0700301 {
Marissa Wallfd668622018-05-10 10:21:13 -0700302 Mutex::Autolock lock(mFrameEventHistoryMutex);
303 mFrameEventHistory.addPostComposition(mCurrentFrameNumber, glDoneFence, presentFence,
304 compositorTiming);
David Sodman0c69cad2017-08-21 12:12:51 -0700305 }
306
Marissa Wallfd668622018-05-10 10:21:13 -0700307 // Update mFrameTracker.
chaviw4244e032019-09-04 11:27:49 -0700308 nsecs_t desiredPresentTime = mBufferInfo.mDesiredPresentTime;
Marissa Wallfd668622018-05-10 10:21:13 -0700309 mFrameTracker.setDesiredPresentTime(desiredPresentTime);
310
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700311 const int32_t layerID = getSequence();
Yiwei Zhang7e666a52018-11-15 13:33:42 -0800312 mFlinger->mTimeStats->setDesiredTime(layerID, mCurrentFrameNumber, desiredPresentTime);
Marissa Wallfd668622018-05-10 10:21:13 -0700313
chaviw4244e032019-09-04 11:27:49 -0700314 std::shared_ptr<FenceTime> frameReadyFence = mBufferInfo.mFenceTime;
Marissa Wallfd668622018-05-10 10:21:13 -0700315 if (frameReadyFence->isValid()) {
316 mFrameTracker.setFrameReadyFence(std::move(frameReadyFence));
317 } else {
318 // There was no fence for this frame, so assume that it was ready
319 // to be presented at the desired present time.
320 mFrameTracker.setFrameReadyTime(desiredPresentTime);
Dominik Laskowski45de9bd2018-06-11 17:44:10 -0700321 }
Marissa Wallfd668622018-05-10 10:21:13 -0700322
323 if (presentFence->isValid()) {
Yiwei Zhang7e666a52018-11-15 13:33:42 -0800324 mFlinger->mTimeStats->setPresentFence(layerID, mCurrentFrameNumber, presentFence);
Mikael Pessa90092f42019-08-26 17:22:04 -0700325 mFlinger->mFrameTracer->traceFence(layerID, getCurrentBufferId(), mCurrentFrameNumber,
326 presentFence, FrameTracer::FrameEvent::PRESENT_FENCE);
Marissa Wallfd668622018-05-10 10:21:13 -0700327 mFrameTracker.setActualPresentFence(std::shared_ptr<FenceTime>(presentFence));
Dominik Laskowski075d3172018-05-24 15:50:06 -0700328 } else if (displayId && mFlinger->getHwComposer().isConnected(*displayId)) {
Marissa Wallfd668622018-05-10 10:21:13 -0700329 // The HWC doesn't support present fences, so use the refresh
330 // timestamp instead.
Dominik Laskowski075d3172018-05-24 15:50:06 -0700331 const nsecs_t actualPresentTime = mFlinger->getHwComposer().getRefreshTimestamp(*displayId);
Yiwei Zhang7e666a52018-11-15 13:33:42 -0800332 mFlinger->mTimeStats->setPresentTime(layerID, mCurrentFrameNumber, actualPresentTime);
Mikael Pessa90092f42019-08-26 17:22:04 -0700333 mFlinger->mFrameTracer->traceTimestamp(layerID, getCurrentBufferId(), mCurrentFrameNumber,
334 actualPresentTime,
335 FrameTracer::FrameEvent::PRESENT_FENCE);
Marissa Wallfd668622018-05-10 10:21:13 -0700336 mFrameTracker.setActualPresentTime(actualPresentTime);
337 }
338
339 mFrameTracker.advanceFrame();
chaviw74b03172019-08-19 11:09:03 -0700340 mBufferInfo.mFrameLatencyNeeded = false;
Marissa Wallfd668622018-05-10 10:21:13 -0700341 return true;
David Sodman0c69cad2017-08-21 12:12:51 -0700342}
343
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700344bool BufferLayer::latchBuffer(bool& recomputeVisibleRegions, nsecs_t latchTime,
345 nsecs_t expectedPresentTime) {
Marissa Wallfd668622018-05-10 10:21:13 -0700346 ATRACE_CALL();
David Sodman0c69cad2017-08-21 12:12:51 -0700347
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800348 bool refreshRequired = latchSidebandStream(recomputeVisibleRegions);
David Sodman0c69cad2017-08-21 12:12:51 -0700349
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800350 if (refreshRequired) {
351 return refreshRequired;
David Sodman0c69cad2017-08-21 12:12:51 -0700352 }
353
Marissa Wallfd668622018-05-10 10:21:13 -0700354 if (!hasReadyFrame()) {
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800355 return false;
David Sodman0c69cad2017-08-21 12:12:51 -0700356 }
David Sodman0c69cad2017-08-21 12:12:51 -0700357
Marissa Wallfd668622018-05-10 10:21:13 -0700358 // if we've already called updateTexImage() without going through
359 // a composition step, we have to skip this layer at this point
360 // because we cannot call updateTeximage() without a corresponding
361 // compositionComplete() call.
362 // we'll trigger an update in onPreComposition().
363 if (mRefreshPending) {
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800364 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700365 }
366
367 // If the head buffer's acquire fence hasn't signaled yet, return and
368 // try again later
369 if (!fenceHasSignaled()) {
Ady Abraham09bd3922019-04-08 10:44:56 -0700370 ATRACE_NAME("!fenceHasSignaled()");
David Sodman0c69cad2017-08-21 12:12:51 -0700371 mFlinger->signalLayerUpdate();
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800372 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700373 }
374
375 // Capture the old state of the layer for comparisons later
376 const State& s(getDrawingState());
377 const bool oldOpacity = isOpaque(s);
chaviwd62d3062019-09-04 14:48:02 -0700378
379 BufferInfo oldBufferInfo = mBufferInfo;
Marissa Wallfd668622018-05-10 10:21:13 -0700380
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700381 if (!allTransactionsSignaled(expectedPresentTime)) {
Marissa Wallebb486e2019-05-15 14:08:08 -0700382 mFlinger->setTransactionFlags(eTraversalNeeded);
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800383 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700384 }
385
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700386 status_t err = updateTexImage(recomputeVisibleRegions, latchTime, expectedPresentTime);
Marissa Wallfd668622018-05-10 10:21:13 -0700387 if (err != NO_ERROR) {
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800388 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700389 }
390
391 err = updateActiveBuffer();
392 if (err != NO_ERROR) {
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800393 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700394 }
395
Marissa Wallfd668622018-05-10 10:21:13 -0700396 err = updateFrameNumber(latchTime);
397 if (err != NO_ERROR) {
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800398 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700399 }
400
chaviw4244e032019-09-04 11:27:49 -0700401 gatherBufferInfo();
402
Marissa Wallfd668622018-05-10 10:21:13 -0700403 mRefreshPending = true;
chaviw74b03172019-08-19 11:09:03 -0700404 mBufferInfo.mFrameLatencyNeeded = true;
chaviwd62d3062019-09-04 14:48:02 -0700405 if (oldBufferInfo.mBuffer == nullptr) {
Marissa Wallfd668622018-05-10 10:21:13 -0700406 // the first time we receive a buffer, we need to trigger a
407 // geometry invalidation.
408 recomputeVisibleRegions = true;
409 }
410
chaviw4244e032019-09-04 11:27:49 -0700411 if ((mBufferInfo.mCrop != oldBufferInfo.mCrop) ||
412 (mBufferInfo.mTransform != oldBufferInfo.mTransform) ||
413 (mBufferInfo.mScaleMode != oldBufferInfo.mScaleMode) ||
414 (mBufferInfo.mTransformToDisplayInverse != oldBufferInfo.mTransformToDisplayInverse)) {
Marissa Wallfd668622018-05-10 10:21:13 -0700415 recomputeVisibleRegions = true;
416 }
417
chaviwd62d3062019-09-04 14:48:02 -0700418 if (oldBufferInfo.mBuffer != nullptr) {
419 uint32_t bufWidth = mBufferInfo.mBuffer->getWidth();
420 uint32_t bufHeight = mBufferInfo.mBuffer->getHeight();
421 if (bufWidth != uint32_t(oldBufferInfo.mBuffer->width) ||
422 bufHeight != uint32_t(oldBufferInfo.mBuffer->height)) {
Marissa Wallfd668622018-05-10 10:21:13 -0700423 recomputeVisibleRegions = true;
424 }
425 }
426
427 if (oldOpacity != isOpaque(s)) {
428 recomputeVisibleRegions = true;
429 }
430
431 // Remove any sync points corresponding to the buffer which was just
432 // latched
433 {
434 Mutex::Autolock lock(mLocalSyncPointMutex);
435 auto point = mLocalSyncPoints.begin();
436 while (point != mLocalSyncPoints.end()) {
437 if (!(*point)->frameIsAvailable() || !(*point)->transactionIsApplied()) {
438 // This sync point must have been added since we started
439 // latching. Don't drop it yet.
440 ++point;
441 continue;
442 }
443
444 if ((*point)->getFrameNumber() <= mCurrentFrameNumber) {
Alec Mourie60041e2019-06-14 18:59:51 -0700445 std::stringstream ss;
446 ss << "Dropping sync point " << (*point)->getFrameNumber();
447 ATRACE_NAME(ss.str().c_str());
Marissa Wallfd668622018-05-10 10:21:13 -0700448 point = mLocalSyncPoints.erase(point);
449 } else {
450 ++point;
451 }
452 }
453 }
454
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800455 return true;
Marissa Wallfd668622018-05-10 10:21:13 -0700456}
457
458// transaction
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700459void BufferLayer::notifyAvailableFrames(nsecs_t expectedPresentTime) {
460 const auto headFrameNumber = getHeadFrameNumber(expectedPresentTime);
Ady Abrahamcd1580c2019-04-29 15:40:03 -0700461 const bool headFenceSignaled = fenceHasSignaled();
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700462 const bool presentTimeIsCurrent = framePresentTimeIsCurrent(expectedPresentTime);
Marissa Wallfd668622018-05-10 10:21:13 -0700463 Mutex::Autolock lock(mLocalSyncPointMutex);
464 for (auto& point : mLocalSyncPoints) {
Ady Abrahamcd1580c2019-04-29 15:40:03 -0700465 if (headFrameNumber >= point->getFrameNumber() && headFenceSignaled &&
466 presentTimeIsCurrent) {
Marissa Wallfd668622018-05-10 10:21:13 -0700467 point->setFrameAvailable();
chaviw43cb3cb2019-05-31 15:23:41 -0700468 sp<Layer> requestedSyncLayer = point->getRequestedSyncLayer();
469 if (requestedSyncLayer) {
470 // Need to update the transaction flag to ensure the layer's pending transaction
471 // gets applied.
472 requestedSyncLayer->setTransactionFlags(eTransactionNeeded);
473 }
Marissa Wallfd668622018-05-10 10:21:13 -0700474 }
David Sodman0c69cad2017-08-21 12:12:51 -0700475 }
476}
477
Marissa Wallfd668622018-05-10 10:21:13 -0700478bool BufferLayer::hasReadyFrame() const {
Marissa Wall024a1912018-08-13 13:55:35 -0700479 return hasFrameUpdate() || getSidebandStreamChanged() || getAutoRefresh();
Marissa Wallfd668622018-05-10 10:21:13 -0700480}
481
482uint32_t BufferLayer::getEffectiveScalingMode() const {
483 if (mOverrideScalingMode >= 0) {
484 return mOverrideScalingMode;
485 }
486
chaviw4244e032019-09-04 11:27:49 -0700487 return mBufferInfo.mScaleMode;
Marissa Wallfd668622018-05-10 10:21:13 -0700488}
489
490bool BufferLayer::isProtected() const {
chaviwd62d3062019-09-04 14:48:02 -0700491 const sp<GraphicBuffer>& buffer(mBufferInfo.mBuffer);
Marissa Wallfd668622018-05-10 10:21:13 -0700492 return (buffer != 0) && (buffer->getUsage() & GRALLOC_USAGE_PROTECTED);
493}
494
495bool BufferLayer::latchUnsignaledBuffers() {
496 static bool propertyLoaded = false;
497 static bool latch = false;
498 static std::mutex mutex;
499 std::lock_guard<std::mutex> lock(mutex);
500 if (!propertyLoaded) {
501 char value[PROPERTY_VALUE_MAX] = {};
502 property_get("debug.sf.latch_unsignaled", value, "0");
503 latch = atoi(value);
504 propertyLoaded = true;
505 }
506 return latch;
507}
508
509// h/w composer set-up
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700510bool BufferLayer::allTransactionsSignaled(nsecs_t expectedPresentTime) {
511 const auto headFrameNumber = getHeadFrameNumber(expectedPresentTime);
Marissa Wallfd668622018-05-10 10:21:13 -0700512 bool matchingFramesFound = false;
513 bool allTransactionsApplied = true;
514 Mutex::Autolock lock(mLocalSyncPointMutex);
515
516 for (auto& point : mLocalSyncPoints) {
517 if (point->getFrameNumber() > headFrameNumber) {
518 break;
519 }
520 matchingFramesFound = true;
521
522 if (!point->frameIsAvailable()) {
523 // We haven't notified the remote layer that the frame for
524 // this point is available yet. Notify it now, and then
525 // abort this attempt to latch.
526 point->setFrameAvailable();
527 allTransactionsApplied = false;
528 break;
529 }
530
531 allTransactionsApplied = allTransactionsApplied && point->transactionIsApplied();
532 }
533 return !matchingFramesFound || allTransactionsApplied;
David Sodman0c69cad2017-08-21 12:12:51 -0700534}
535
536// As documented in libhardware header, formats in the range
537// 0x100 - 0x1FF are specific to the HAL implementation, and
538// are known to have no alpha channel
539// TODO: move definition for device-specific range into
540// hardware.h, instead of using hard-coded values here.
541#define HARDWARE_IS_DEVICE_FORMAT(f) ((f) >= 0x100 && (f) <= 0x1FF)
542
543bool BufferLayer::getOpacityForFormat(uint32_t format) {
544 if (HARDWARE_IS_DEVICE_FORMAT(format)) {
545 return true;
546 }
547 switch (format) {
548 case HAL_PIXEL_FORMAT_RGBA_8888:
549 case HAL_PIXEL_FORMAT_BGRA_8888:
550 case HAL_PIXEL_FORMAT_RGBA_FP16:
551 case HAL_PIXEL_FORMAT_RGBA_1010102:
552 return false;
553 }
554 // in all other case, we have no blending (also for unknown formats)
555 return true;
556}
557
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800558bool BufferLayer::needsFiltering(const sp<const DisplayDevice>& displayDevice) const {
Lloyd Piquef16688f2019-02-19 17:47:57 -0800559 // If we are not capturing based on the state of a known display device,
560 // just return false.
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800561 if (displayDevice == nullptr) {
Lloyd Piquef16688f2019-02-19 17:47:57 -0800562 return false;
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800563 }
564
565 const auto outputLayer = findOutputLayerForDisplay(displayDevice);
566 if (outputLayer == nullptr) {
Lloyd Piquef16688f2019-02-19 17:47:57 -0800567 return false;
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800568 }
569
Lloyd Piquef16688f2019-02-19 17:47:57 -0800570 // We need filtering if the sourceCrop rectangle size does not match the
571 // displayframe rectangle size (not a 1:1 render)
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800572 const auto& compositionState = outputLayer->getState();
573 const auto displayFrame = compositionState.displayFrame;
574 const auto sourceCrop = compositionState.sourceCrop;
Lloyd Piquef16688f2019-02-19 17:47:57 -0800575 return sourceCrop.getHeight() != displayFrame.getHeight() ||
Peiyong Linc2020ca2019-01-10 11:36:12 -0800576 sourceCrop.getWidth() != displayFrame.getWidth();
Chia-I Wu692e0832018-06-05 15:46:58 -0700577}
578
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700579uint64_t BufferLayer::getHeadFrameNumber(nsecs_t expectedPresentTime) const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800580 if (hasFrameUpdate()) {
Dominik Laskowskia8955dd2019-07-10 10:19:09 -0700581 return getFrameNumber(expectedPresentTime);
David Sodman0c69cad2017-08-21 12:12:51 -0700582 } else {
583 return mCurrentFrameNumber;
584 }
585}
586
Vishnu Nair60356342018-11-13 13:00:45 -0800587Rect BufferLayer::getBufferSize(const State& s) const {
588 // If we have a sideband stream, or we are scaling the buffer then return the layer size since
589 // we cannot determine the buffer size.
590 if ((s.sidebandStream != nullptr) ||
591 (getEffectiveScalingMode() != NATIVE_WINDOW_SCALING_MODE_FREEZE)) {
592 return Rect(getActiveWidth(s), getActiveHeight(s));
593 }
594
chaviwd62d3062019-09-04 14:48:02 -0700595 if (mBufferInfo.mBuffer == nullptr) {
Vishnu Nair60356342018-11-13 13:00:45 -0800596 return Rect::INVALID_RECT;
597 }
598
chaviwd62d3062019-09-04 14:48:02 -0700599 uint32_t bufWidth = mBufferInfo.mBuffer->getWidth();
600 uint32_t bufHeight = mBufferInfo.mBuffer->getHeight();
Vishnu Nair60356342018-11-13 13:00:45 -0800601
602 // Undo any transformations on the buffer and return the result.
chaviw4244e032019-09-04 11:27:49 -0700603 if (mBufferInfo.mTransform & ui::Transform::ROT_90) {
Vishnu Nair60356342018-11-13 13:00:45 -0800604 std::swap(bufWidth, bufHeight);
605 }
606
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800607 if (getTransformToDisplayInverse()) {
Vishnu Nair60356342018-11-13 13:00:45 -0800608 uint32_t invTransform = DisplayDevice::getPrimaryDisplayOrientationTransform();
609 if (invTransform & ui::Transform::ROT_90) {
610 std::swap(bufWidth, bufHeight);
611 }
612 }
613
614 return Rect(bufWidth, bufHeight);
615}
616
Lloyd Piquefeb73d72018-12-04 17:23:44 -0800617std::shared_ptr<compositionengine::Layer> BufferLayer::getCompositionLayer() const {
618 return mCompositionLayer;
619}
620
Vishnu Nair4351ad52019-02-11 14:13:02 -0800621FloatRect BufferLayer::computeSourceBounds(const FloatRect& parentBounds) const {
622 const State& s(getDrawingState());
623
624 // If we have a sideband stream, or we are scaling the buffer then return the layer size since
625 // we cannot determine the buffer size.
626 if ((s.sidebandStream != nullptr) ||
627 (getEffectiveScalingMode() != NATIVE_WINDOW_SCALING_MODE_FREEZE)) {
628 return FloatRect(0, 0, getActiveWidth(s), getActiveHeight(s));
629 }
630
chaviwd62d3062019-09-04 14:48:02 -0700631 if (mBufferInfo.mBuffer == nullptr) {
Vishnu Nair4351ad52019-02-11 14:13:02 -0800632 return parentBounds;
633 }
634
chaviwd62d3062019-09-04 14:48:02 -0700635 uint32_t bufWidth = mBufferInfo.mBuffer->getWidth();
636 uint32_t bufHeight = mBufferInfo.mBuffer->getHeight();
Vishnu Nair4351ad52019-02-11 14:13:02 -0800637
638 // Undo any transformations on the buffer and return the result.
chaviw4244e032019-09-04 11:27:49 -0700639 if (mBufferInfo.mTransform & ui::Transform::ROT_90) {
Vishnu Nair4351ad52019-02-11 14:13:02 -0800640 std::swap(bufWidth, bufHeight);
641 }
642
643 if (getTransformToDisplayInverse()) {
644 uint32_t invTransform = DisplayDevice::getPrimaryDisplayOrientationTransform();
645 if (invTransform & ui::Transform::ROT_90) {
646 std::swap(bufWidth, bufHeight);
647 }
648 }
649
650 return FloatRect(0, 0, bufWidth, bufHeight);
651}
652
chaviw49a108c2019-08-12 11:23:06 -0700653void BufferLayer::latchAndReleaseBuffer() {
654 mRefreshPending = false;
655 if (hasReadyFrame()) {
656 bool ignored = false;
657 latchBuffer(ignored, systemTime(), 0 /* expectedPresentTime */);
658 }
659 releasePendingBuffer(systemTime());
660}
661
chaviw4244e032019-09-04 11:27:49 -0700662PixelFormat BufferLayer::getPixelFormat() const {
663 return mBufferInfo.mPixelFormat;
664}
665
666bool BufferLayer::getTransformToDisplayInverse() const {
667 return mBufferInfo.mTransformToDisplayInverse;
668}
669
670Rect BufferLayer::getBufferCrop() const {
671 // this is the crop rectangle that applies to the buffer
672 // itself (as opposed to the window)
673 if (!mBufferInfo.mCrop.isEmpty()) {
674 // if the buffer crop is defined, we use that
675 return mBufferInfo.mCrop;
chaviwd62d3062019-09-04 14:48:02 -0700676 } else if (mBufferInfo.mBuffer != nullptr) {
chaviw4244e032019-09-04 11:27:49 -0700677 // otherwise we use the whole buffer
chaviwd62d3062019-09-04 14:48:02 -0700678 return mBufferInfo.mBuffer->getBounds();
chaviw4244e032019-09-04 11:27:49 -0700679 } else {
680 // if we don't have a buffer yet, we use an empty/invalid crop
681 return Rect();
682 }
683}
684
685uint32_t BufferLayer::getBufferTransform() const {
686 return mBufferInfo.mTransform;
687}
688
689ui::Dataspace BufferLayer::getDataSpace() const {
690 return mBufferInfo.mDataspace;
691}
692
693ui::Dataspace BufferLayer::translateDataspace(ui::Dataspace dataspace) {
694 ui::Dataspace updatedDataspace = dataspace;
695 // translate legacy dataspaces to modern dataspaces
696 switch (dataspace) {
697 case ui::Dataspace::SRGB:
698 updatedDataspace = ui::Dataspace::V0_SRGB;
699 break;
700 case ui::Dataspace::SRGB_LINEAR:
701 updatedDataspace = ui::Dataspace::V0_SRGB_LINEAR;
702 break;
703 case ui::Dataspace::JFIF:
704 updatedDataspace = ui::Dataspace::V0_JFIF;
705 break;
706 case ui::Dataspace::BT601_625:
707 updatedDataspace = ui::Dataspace::V0_BT601_625;
708 break;
709 case ui::Dataspace::BT601_525:
710 updatedDataspace = ui::Dataspace::V0_BT601_525;
711 break;
712 case ui::Dataspace::BT709:
713 updatedDataspace = ui::Dataspace::V0_BT709;
714 break;
715 default:
716 break;
717 }
718
719 return updatedDataspace;
720}
721
chaviwd62d3062019-09-04 14:48:02 -0700722sp<GraphicBuffer> BufferLayer::getBuffer() const {
723 return mBufferInfo.mBuffer;
724}
725
chaviwf83ce182019-09-12 14:43:08 -0700726void BufferLayer::getDrawingTransformMatrix(bool filteringEnabled, float outMatrix[16]) {
727 GLConsumer::computeTransformMatrix(outMatrix, mBufferInfo.mBuffer, mBufferInfo.mCrop,
728 mBufferInfo.mTransform, filteringEnabled);
729}
730
chaviwb4c6e582019-08-16 14:35:07 -0700731void BufferLayer::setInitialValuesForClone(const sp<Layer>& clonedFrom) {
732 Layer::setInitialValuesForClone(clonedFrom);
733
734 sp<BufferLayer> bufferClonedFrom = static_cast<BufferLayer*>(clonedFrom.get());
735 mPremultipliedAlpha = bufferClonedFrom->mPremultipliedAlpha;
736 mPotentialCursor = bufferClonedFrom->mPotentialCursor;
737 mProtectedByApp = bufferClonedFrom->mProtectedByApp;
chaviw74b03172019-08-19 11:09:03 -0700738
739 updateCloneBufferInfo();
740}
741
742void BufferLayer::updateCloneBufferInfo() {
743 if (!isClone() || !isClonedFromAlive()) {
744 return;
745 }
746
747 sp<BufferLayer> clonedFrom = static_cast<BufferLayer*>(getClonedFrom().get());
748 mBufferInfo = clonedFrom->mBufferInfo;
749 mSidebandStream = clonedFrom->mSidebandStream;
750 surfaceDamageRegion = clonedFrom->surfaceDamageRegion;
751 mCurrentFrameNumber = clonedFrom->mCurrentFrameNumber.load();
752 mPreviousFrameNumber = clonedFrom->mPreviousFrameNumber;
753
754 // After buffer info is updated, the drawingState from the real layer needs to be copied into
755 // the cloned. This is because some properties of drawingState can change when latchBuffer is
756 // called. However, copying the drawingState would also overwrite the cloned layer's relatives.
757 // Therefore, temporarily store the relatives so they can be set in the cloned drawingState
758 // again.
759 wp<Layer> tmpZOrderRelativeOf = mDrawingState.zOrderRelativeOf;
760 SortedVector<wp<Layer>> tmpZOrderRelatives = mDrawingState.zOrderRelatives;
761 mDrawingState = clonedFrom->mDrawingState;
762 // TODO: (b/140756730) Ignore input for now since InputDispatcher doesn't support multiple
763 // InputWindows per client token yet.
764 mDrawingState.inputInfo.token = nullptr;
765 mDrawingState.zOrderRelativeOf = tmpZOrderRelativeOf;
766 mDrawingState.zOrderRelatives = tmpZOrderRelatives;
chaviwb4c6e582019-08-16 14:35:07 -0700767}
768
David Sodman0c69cad2017-08-21 12:12:51 -0700769} // namespace android
770
771#if defined(__gl_h_)
772#error "don't include gl/gl.h in this file"
773#endif
774
775#if defined(__gl2_h_)
776#error "don't include gl2/gl2.h in this file"
777#endif