blob: c3b171847cfeaff0e6ebeb60a369afc11deda0ec [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"
54#include "LayerRejecter.h"
Yiwei Zhang7e666a52018-11-15 13:33:42 -080055#include "TimeStats/TimeStats.h"
56
David Sodman0c69cad2017-08-21 12:12:51 -070057namespace android {
58
Lloyd Pique42ab75e2018-09-12 20:46:03 -070059BufferLayer::BufferLayer(const LayerCreationArgs& args)
Lloyd Piquefeb73d72018-12-04 17:23:44 -080060 : Layer(args),
61 mTextureName(args.flinger->getNewTexture()),
62 mCompositionLayer{mFlinger->getCompositionEngine().createLayer(
63 compositionengine::LayerCreationArgs{this})} {
Lloyd Pique42ab75e2018-09-12 20:46:03 -070064 ALOGV("Creating Layer %s", args.name.string());
David Sodman0c69cad2017-08-21 12:12:51 -070065
Lloyd Pique42ab75e2018-09-12 20:46:03 -070066 mPremultipliedAlpha = !(args.flags & ISurfaceComposerClient::eNonPremultiplied);
David Sodman0c69cad2017-08-21 12:12:51 -070067
Lloyd Pique42ab75e2018-09-12 20:46:03 -070068 mPotentialCursor = args.flags & ISurfaceComposerClient::eCursorWindow;
69 mProtectedByApp = args.flags & ISurfaceComposerClient::eProtectedByApp;
David Sodman0c69cad2017-08-21 12:12:51 -070070}
71
72BufferLayer::~BufferLayer() {
David Sodman0c69cad2017-08-21 12:12:51 -070073 mFlinger->deleteTextureAsync(mTextureName);
Yiwei Zhang7e666a52018-11-15 13:33:42 -080074 mFlinger->mTimeStats->onDestroy(getSequence());
David Sodman0c69cad2017-08-21 12:12:51 -070075}
76
David Sodmaneb085e02017-10-05 18:49:04 -070077void BufferLayer::useSurfaceDamage() {
78 if (mFlinger->mForceFullDamage) {
79 surfaceDamageRegion = Region::INVALID_REGION;
80 } else {
Marissa Wallfd668622018-05-10 10:21:13 -070081 surfaceDamageRegion = getDrawingSurfaceDamage();
David Sodmaneb085e02017-10-05 18:49:04 -070082 }
83}
84
85void BufferLayer::useEmptyDamage() {
86 surfaceDamageRegion.clear();
87}
88
Marissa Wallfd668622018-05-10 10:21:13 -070089bool BufferLayer::isOpaque(const Layer::State& s) const {
90 // if we don't have a buffer or sidebandStream yet, we're translucent regardless of the
91 // layer's opaque flag.
Lloyd Pique0b785d82018-12-04 17:25:27 -080092 if ((mSidebandStream == nullptr) && (mActiveBuffer == nullptr)) {
Marissa Wallfd668622018-05-10 10:21:13 -070093 return false;
94 }
95
96 // if the layer has the opaque flag, then we're always opaque,
97 // otherwise we use the current buffer's format.
98 return ((s.flags & layer_state_t::eLayerOpaque) != 0) || getOpacityForFormat(getPixelFormat());
David Sodman0c69cad2017-08-21 12:12:51 -070099}
100
101bool BufferLayer::isVisible() const {
Ady Abrahama315ce72019-04-24 14:35:20 -0700102 bool visible = !(isHiddenByPolicy()) && getAlpha() > 0.0f &&
Lloyd Pique0b785d82018-12-04 17:25:27 -0800103 (mActiveBuffer != nullptr || mSidebandStream != nullptr);
Ady Abrahama315ce72019-04-24 14:35:20 -0700104 mFlinger->mScheduler->setLayerVisibility(mSchedulerLayerHandle, visible);
105
106 return visible;
David Sodman0c69cad2017-08-21 12:12:51 -0700107}
108
109bool BufferLayer::isFixedSize() const {
110 return getEffectiveScalingMode() != NATIVE_WINDOW_SCALING_MODE_FREEZE;
111}
112
Lloyd Piquea83776c2019-01-29 18:42:32 -0800113bool BufferLayer::usesSourceCrop() const {
114 return true;
115}
116
David Sodman0c69cad2017-08-21 12:12:51 -0700117static constexpr mat4 inverseOrientation(uint32_t transform) {
David Sodman41fdfc92017-11-06 16:09:56 -0800118 const mat4 flipH(-1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1);
119 const mat4 flipV(1, 0, 0, 0, 0, -1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1);
120 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 -0700121 mat4 tr;
122
123 if (transform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
124 tr = tr * rot90;
125 }
126 if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_H) {
127 tr = tr * flipH;
128 }
129 if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_V) {
130 tr = tr * flipV;
131 }
132 return inverse(tr);
133}
134
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000135bool BufferLayer::prepareClientLayer(const RenderArea& renderArea, const Region& clip,
136 bool useIdentityTransform, Region& clearRegion,
Peiyong Lin8f28a1d2019-02-07 17:25:12 -0800137 const bool supportProtectedContent,
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000138 renderengine::LayerSettings& layer) {
David Sodman0c69cad2017-08-21 12:12:51 -0700139 ATRACE_CALL();
Peiyong Lin8f28a1d2019-02-07 17:25:12 -0800140 Layer::prepareClientLayer(renderArea, clip, useIdentityTransform, clearRegion,
141 supportProtectedContent, layer);
David Sodman0cf8f8d2017-12-20 18:19:45 -0800142 if (CC_UNLIKELY(mActiveBuffer == 0)) {
David Sodman0c69cad2017-08-21 12:12:51 -0700143 // the texture has not been created yet, this Layer has
144 // in fact never been drawn into. This happens frequently with
145 // SurfaceView because the WindowManager can't know when the client
146 // has drawn the first time.
147
148 // If there is nothing under us, we paint the screen in black, otherwise
149 // we just skip this update.
150
151 // figure out if there is something below us
152 Region under;
153 bool finished = false;
154 mFlinger->mDrawingState.traverseInZOrder([&](Layer* layer) {
155 if (finished || layer == static_cast<BufferLayer const*>(this)) {
156 finished = true;
157 return;
158 }
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000159 under.orSelf(layer->visibleRegion);
David Sodman0c69cad2017-08-21 12:12:51 -0700160 });
161 // if not everything below us is covered, we plug the holes!
162 Region holes(clip.subtract(under));
163 if (!holes.isEmpty()) {
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000164 clearRegion.orSelf(holes);
David Sodman0c69cad2017-08-21 12:12:51 -0700165 }
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000166 return false;
David Sodman0c69cad2017-08-21 12:12:51 -0700167 }
Peiyong Lin8f28a1d2019-02-07 17:25:12 -0800168 bool blackOutLayer =
169 (isProtected() && !supportProtectedContent) || (isSecure() && !renderArea.isSecure());
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000170 const State& s(getDrawingState());
David Sodman0c69cad2017-08-21 12:12:51 -0700171 if (!blackOutLayer) {
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000172 layer.source.buffer.buffer = mActiveBuffer;
173 layer.source.buffer.isOpaque = isOpaque(s);
174 layer.source.buffer.fence = mActiveBufferFence;
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000175 layer.source.buffer.textureName = mTextureName;
176 layer.source.buffer.usePremultipliedAlpha = getPremultipledAlpha();
177 layer.source.buffer.isY410BT2020 = isHdrY410();
David Sodman0c69cad2017-08-21 12:12:51 -0700178 // TODO: we could be more subtle with isFixedSize()
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800179 const bool useFiltering = needsFiltering(renderArea.getDisplayDevice()) ||
180 renderArea.needsFiltering() || isFixedSize();
David Sodman0c69cad2017-08-21 12:12:51 -0700181
182 // Query the texture matrix given our current filtering mode.
183 float textureMatrix[16];
Marissa Wallfd668622018-05-10 10:21:13 -0700184 setFilteringEnabled(useFiltering);
185 getDrawingTransformMatrix(textureMatrix);
David Sodman0c69cad2017-08-21 12:12:51 -0700186
187 if (getTransformToDisplayInverse()) {
188 /*
189 * the code below applies the primary display's inverse transform to
190 * the texture transform
191 */
192 uint32_t transform = DisplayDevice::getPrimaryDisplayOrientationTransform();
193 mat4 tr = inverseOrientation(transform);
194
195 /**
196 * TODO(b/36727915): This is basically a hack.
197 *
198 * Ensure that regardless of the parent transformation,
199 * this buffer is always transformed from native display
200 * orientation to display orientation. For example, in the case
201 * of a camera where the buffer remains in native orientation,
202 * we want the pixels to always be upright.
203 */
204 sp<Layer> p = mDrawingParent.promote();
205 if (p != nullptr) {
206 const auto parentTransform = p->getTransform();
207 tr = tr * inverseOrientation(parentTransform.getOrientation());
208 }
209
210 // and finally apply it to the original texture matrix
211 const mat4 texTransform(mat4(static_cast<const float*>(textureMatrix)) * tr);
212 memcpy(textureMatrix, texTransform.asArray(), sizeof(textureMatrix));
213 }
214
Vishnu Nair4351ad52019-02-11 14:13:02 -0800215 const Rect win{getBounds()};
Marissa Wall290ad082019-03-06 13:23:47 -0800216 float bufferWidth = getBufferSize(s).getWidth();
217 float bufferHeight = getBufferSize(s).getHeight();
218
219 // BufferStateLayers can have a "buffer size" of [0, 0, -1, -1] when no display frame has
220 // been set and there is no parent layer bounds. In that case, the scale is meaningless so
221 // ignore them.
222 if (!getBufferSize(s).isValid()) {
223 bufferWidth = float(win.right) - float(win.left);
224 bufferHeight = float(win.bottom) - float(win.top);
225 }
David Sodman0c69cad2017-08-21 12:12:51 -0700226
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000227 const float scaleHeight = (float(win.bottom) - float(win.top)) / bufferHeight;
228 const float scaleWidth = (float(win.right) - float(win.left)) / bufferWidth;
229 const float translateY = float(win.top) / bufferHeight;
230 const float translateX = float(win.left) / bufferWidth;
231
232 // Flip y-coordinates because GLConsumer expects OpenGL convention.
233 mat4 tr = mat4::translate(vec4(.5, .5, 0, 1)) * mat4::scale(vec4(1, -1, 1, 1)) *
234 mat4::translate(vec4(-.5, -.5, 0, 1)) *
235 mat4::translate(vec4(translateX, translateY, 0, 1)) *
236 mat4::scale(vec4(scaleWidth, scaleHeight, 1.0, 1.0));
237
238 layer.source.buffer.useTextureFiltering = useFiltering;
239 layer.source.buffer.textureTransform = mat4(static_cast<const float*>(textureMatrix)) * tr;
David Sodman0c69cad2017-08-21 12:12:51 -0700240 } else {
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000241 // If layer is blacked out, force alpha to 1 so that we draw a black color
242 // layer.
243 layer.source.buffer.buffer = nullptr;
244 layer.alpha = 1.0;
David Sodman0c69cad2017-08-21 12:12:51 -0700245 }
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000246
247 return true;
David Sodman0c69cad2017-08-21 12:12:51 -0700248}
249
Marissa Wallfd668622018-05-10 10:21:13 -0700250bool BufferLayer::isHdrY410() const {
251 // pixel format is HDR Y410 masquerading as RGBA_1010102
252 return (mCurrentDataSpace == ui::Dataspace::BT2020_ITU_PQ &&
253 getDrawingApi() == NATIVE_WINDOW_API_MEDIA &&
Lloyd Pique0b785d82018-12-04 17:25:27 -0800254 mActiveBuffer->getPixelFormat() == HAL_PIXEL_FORMAT_RGBA_1010102);
David Sodmaneb085e02017-10-05 18:49:04 -0700255}
256
Lloyd Piquef5275482019-01-29 18:42:42 -0800257void BufferLayer::latchPerFrameState(
258 compositionengine::LayerFECompositionState& compositionState) const {
259 Layer::latchPerFrameState(compositionState);
David Sodman0c69cad2017-08-21 12:12:51 -0700260
261 // Sideband layers
Lloyd Piquef5275482019-01-29 18:42:42 -0800262 if (compositionState.sidebandStream.get()) {
263 compositionState.compositionType = Hwc2::IComposerClient::Composition::SIDEBAND;
David Sodman15094112018-10-11 09:39:37 -0700264 } else {
Lloyd Piquef5275482019-01-29 18:42:42 -0800265 // Normal buffer layers
266 compositionState.hdrMetadata = getDrawingHdrMetadata();
267 compositionState.compositionType = mPotentialCursor
268 ? Hwc2::IComposerClient::Composition::CURSOR
269 : Hwc2::IComposerClient::Composition::DEVICE;
David Sodman0c69cad2017-08-21 12:12:51 -0700270 }
David Sodman0c69cad2017-08-21 12:12:51 -0700271}
272
Marissa Wallfd668622018-05-10 10:21:13 -0700273bool BufferLayer::onPreComposition(nsecs_t refreshStartTime) {
274 if (mBufferLatched) {
275 Mutex::Autolock lock(mFrameEventHistoryMutex);
276 mFrameEventHistory.addPreComposition(mCurrentFrameNumber, refreshStartTime);
David Sodman0c69cad2017-08-21 12:12:51 -0700277 }
Marissa Wallfd668622018-05-10 10:21:13 -0700278 mRefreshPending = false;
279 return hasReadyFrame();
David Sodman0c69cad2017-08-21 12:12:51 -0700280}
281
Dominik Laskowski075d3172018-05-24 15:50:06 -0700282bool BufferLayer::onPostComposition(const std::optional<DisplayId>& displayId,
283 const std::shared_ptr<FenceTime>& glDoneFence,
Marissa Wallfd668622018-05-10 10:21:13 -0700284 const std::shared_ptr<FenceTime>& presentFence,
285 const CompositorTiming& compositorTiming) {
286 // mFrameLatencyNeeded is true when a new frame was latched for the
287 // composition.
288 if (!mFrameLatencyNeeded) return false;
289
290 // Update mFrameEventHistory.
Dan Stoza436ccf32018-06-21 12:10:12 -0700291 {
Marissa Wallfd668622018-05-10 10:21:13 -0700292 Mutex::Autolock lock(mFrameEventHistoryMutex);
293 mFrameEventHistory.addPostComposition(mCurrentFrameNumber, glDoneFence, presentFence,
294 compositorTiming);
David Sodman0c69cad2017-08-21 12:12:51 -0700295 }
296
Marissa Wallfd668622018-05-10 10:21:13 -0700297 // Update mFrameTracker.
298 nsecs_t desiredPresentTime = getDesiredPresentTime();
299 mFrameTracker.setDesiredPresentTime(desiredPresentTime);
300
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700301 const int32_t layerID = getSequence();
Yiwei Zhang7e666a52018-11-15 13:33:42 -0800302 mFlinger->mTimeStats->setDesiredTime(layerID, mCurrentFrameNumber, desiredPresentTime);
Marissa Wallfd668622018-05-10 10:21:13 -0700303
304 std::shared_ptr<FenceTime> frameReadyFence = getCurrentFenceTime();
305 if (frameReadyFence->isValid()) {
306 mFrameTracker.setFrameReadyFence(std::move(frameReadyFence));
307 } else {
308 // There was no fence for this frame, so assume that it was ready
309 // to be presented at the desired present time.
310 mFrameTracker.setFrameReadyTime(desiredPresentTime);
Dominik Laskowski45de9bd2018-06-11 17:44:10 -0700311 }
Marissa Wallfd668622018-05-10 10:21:13 -0700312
313 if (presentFence->isValid()) {
Yiwei Zhang7e666a52018-11-15 13:33:42 -0800314 mFlinger->mTimeStats->setPresentFence(layerID, mCurrentFrameNumber, presentFence);
Marissa Wallfd668622018-05-10 10:21:13 -0700315 mFrameTracker.setActualPresentFence(std::shared_ptr<FenceTime>(presentFence));
Dominik Laskowski075d3172018-05-24 15:50:06 -0700316 } else if (displayId && mFlinger->getHwComposer().isConnected(*displayId)) {
Marissa Wallfd668622018-05-10 10:21:13 -0700317 // The HWC doesn't support present fences, so use the refresh
318 // timestamp instead.
Dominik Laskowski075d3172018-05-24 15:50:06 -0700319 const nsecs_t actualPresentTime = mFlinger->getHwComposer().getRefreshTimestamp(*displayId);
Yiwei Zhang7e666a52018-11-15 13:33:42 -0800320 mFlinger->mTimeStats->setPresentTime(layerID, mCurrentFrameNumber, actualPresentTime);
Marissa Wallfd668622018-05-10 10:21:13 -0700321 mFrameTracker.setActualPresentTime(actualPresentTime);
322 }
323
324 mFrameTracker.advanceFrame();
325 mFrameLatencyNeeded = false;
326 return true;
David Sodman0c69cad2017-08-21 12:12:51 -0700327}
328
Alec Mouri56e538f2019-01-14 15:22:01 -0800329bool BufferLayer::latchBuffer(bool& recomputeVisibleRegions, nsecs_t latchTime) {
Marissa Wallfd668622018-05-10 10:21:13 -0700330 ATRACE_CALL();
David Sodman0c69cad2017-08-21 12:12:51 -0700331
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800332 bool refreshRequired = latchSidebandStream(recomputeVisibleRegions);
David Sodman0c69cad2017-08-21 12:12:51 -0700333
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800334 if (refreshRequired) {
335 return refreshRequired;
David Sodman0c69cad2017-08-21 12:12:51 -0700336 }
337
Marissa Wallfd668622018-05-10 10:21:13 -0700338 if (!hasReadyFrame()) {
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800339 return false;
David Sodman0c69cad2017-08-21 12:12:51 -0700340 }
David Sodman0c69cad2017-08-21 12:12:51 -0700341
Marissa Wallfd668622018-05-10 10:21:13 -0700342 // if we've already called updateTexImage() without going through
343 // a composition step, we have to skip this layer at this point
344 // because we cannot call updateTeximage() without a corresponding
345 // compositionComplete() call.
346 // we'll trigger an update in onPreComposition().
347 if (mRefreshPending) {
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800348 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700349 }
350
351 // If the head buffer's acquire fence hasn't signaled yet, return and
352 // try again later
353 if (!fenceHasSignaled()) {
Ady Abraham09bd3922019-04-08 10:44:56 -0700354 ATRACE_NAME("!fenceHasSignaled()");
David Sodman0c69cad2017-08-21 12:12:51 -0700355 mFlinger->signalLayerUpdate();
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800356 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700357 }
358
359 // Capture the old state of the layer for comparisons later
360 const State& s(getDrawingState());
361 const bool oldOpacity = isOpaque(s);
362 sp<GraphicBuffer> oldBuffer = mActiveBuffer;
363
364 if (!allTransactionsSignaled()) {
Marissa Wallebb486e2019-05-15 14:08:08 -0700365 mFlinger->setTransactionFlags(eTraversalNeeded);
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800366 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700367 }
368
Alec Mouri56e538f2019-01-14 15:22:01 -0800369 status_t err = updateTexImage(recomputeVisibleRegions, latchTime);
Marissa Wallfd668622018-05-10 10:21:13 -0700370 if (err != NO_ERROR) {
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800371 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700372 }
373
374 err = updateActiveBuffer();
375 if (err != NO_ERROR) {
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800376 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700377 }
378
379 mBufferLatched = true;
380
381 err = updateFrameNumber(latchTime);
382 if (err != NO_ERROR) {
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800383 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700384 }
385
386 mRefreshPending = true;
387 mFrameLatencyNeeded = true;
388 if (oldBuffer == nullptr) {
389 // the first time we receive a buffer, we need to trigger a
390 // geometry invalidation.
391 recomputeVisibleRegions = true;
392 }
393
394 ui::Dataspace dataSpace = getDrawingDataSpace();
Peiyong Lin14724e62018-12-05 07:27:30 -0800395 // translate legacy dataspaces to modern dataspaces
Marissa Wallfd668622018-05-10 10:21:13 -0700396 switch (dataSpace) {
Peiyong Lin14724e62018-12-05 07:27:30 -0800397 case ui::Dataspace::SRGB:
398 dataSpace = ui::Dataspace::V0_SRGB;
Marissa Wallfd668622018-05-10 10:21:13 -0700399 break;
Peiyong Lin14724e62018-12-05 07:27:30 -0800400 case ui::Dataspace::SRGB_LINEAR:
401 dataSpace = ui::Dataspace::V0_SRGB_LINEAR;
Marissa Wallfd668622018-05-10 10:21:13 -0700402 break;
Peiyong Lin14724e62018-12-05 07:27:30 -0800403 case ui::Dataspace::JFIF:
404 dataSpace = ui::Dataspace::V0_JFIF;
Marissa Wallfd668622018-05-10 10:21:13 -0700405 break;
Peiyong Lin14724e62018-12-05 07:27:30 -0800406 case ui::Dataspace::BT601_625:
407 dataSpace = ui::Dataspace::V0_BT601_625;
Marissa Wallfd668622018-05-10 10:21:13 -0700408 break;
Peiyong Lin14724e62018-12-05 07:27:30 -0800409 case ui::Dataspace::BT601_525:
410 dataSpace = ui::Dataspace::V0_BT601_525;
Marissa Wallfd668622018-05-10 10:21:13 -0700411 break;
Peiyong Lin14724e62018-12-05 07:27:30 -0800412 case ui::Dataspace::BT709:
413 dataSpace = ui::Dataspace::V0_BT709;
Marissa Wallfd668622018-05-10 10:21:13 -0700414 break;
415 default:
416 break;
417 }
418 mCurrentDataSpace = dataSpace;
419
420 Rect crop(getDrawingCrop());
421 const uint32_t transform(getDrawingTransform());
422 const uint32_t scalingMode(getDrawingScalingMode());
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800423 const bool transformToDisplayInverse(getTransformToDisplayInverse());
Marissa Wallfd668622018-05-10 10:21:13 -0700424 if ((crop != mCurrentCrop) || (transform != mCurrentTransform) ||
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800425 (scalingMode != mCurrentScalingMode) ||
426 (transformToDisplayInverse != mTransformToDisplayInverse)) {
Marissa Wallfd668622018-05-10 10:21:13 -0700427 mCurrentCrop = crop;
428 mCurrentTransform = transform;
429 mCurrentScalingMode = scalingMode;
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800430 mTransformToDisplayInverse = transformToDisplayInverse;
Marissa Wallfd668622018-05-10 10:21:13 -0700431 recomputeVisibleRegions = true;
432 }
433
434 if (oldBuffer != nullptr) {
435 uint32_t bufWidth = mActiveBuffer->getWidth();
436 uint32_t bufHeight = mActiveBuffer->getHeight();
437 if (bufWidth != uint32_t(oldBuffer->width) || bufHeight != uint32_t(oldBuffer->height)) {
438 recomputeVisibleRegions = true;
439 }
440 }
441
442 if (oldOpacity != isOpaque(s)) {
443 recomputeVisibleRegions = true;
444 }
445
446 // Remove any sync points corresponding to the buffer which was just
447 // latched
448 {
449 Mutex::Autolock lock(mLocalSyncPointMutex);
450 auto point = mLocalSyncPoints.begin();
451 while (point != mLocalSyncPoints.end()) {
452 if (!(*point)->frameIsAvailable() || !(*point)->transactionIsApplied()) {
453 // This sync point must have been added since we started
454 // latching. Don't drop it yet.
455 ++point;
456 continue;
457 }
458
459 if ((*point)->getFrameNumber() <= mCurrentFrameNumber) {
Alec Mourie60041e2019-06-14 18:59:51 -0700460 std::stringstream ss;
461 ss << "Dropping sync point " << (*point)->getFrameNumber();
462 ATRACE_NAME(ss.str().c_str());
Marissa Wallfd668622018-05-10 10:21:13 -0700463 point = mLocalSyncPoints.erase(point);
464 } else {
465 ++point;
466 }
467 }
468 }
469
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800470 return true;
Marissa Wallfd668622018-05-10 10:21:13 -0700471}
472
473// transaction
474void BufferLayer::notifyAvailableFrames() {
Ady Abrahamcd1580c2019-04-29 15:40:03 -0700475 const auto headFrameNumber = getHeadFrameNumber();
476 const bool headFenceSignaled = fenceHasSignaled();
477 const bool presentTimeIsCurrent = framePresentTimeIsCurrent();
Marissa Wallfd668622018-05-10 10:21:13 -0700478 Mutex::Autolock lock(mLocalSyncPointMutex);
479 for (auto& point : mLocalSyncPoints) {
Ady Abrahamcd1580c2019-04-29 15:40:03 -0700480 if (headFrameNumber >= point->getFrameNumber() && headFenceSignaled &&
481 presentTimeIsCurrent) {
Marissa Wallfd668622018-05-10 10:21:13 -0700482 point->setFrameAvailable();
chaviw43cb3cb2019-05-31 15:23:41 -0700483 sp<Layer> requestedSyncLayer = point->getRequestedSyncLayer();
484 if (requestedSyncLayer) {
485 // Need to update the transaction flag to ensure the layer's pending transaction
486 // gets applied.
487 requestedSyncLayer->setTransactionFlags(eTransactionNeeded);
488 }
Marissa Wallfd668622018-05-10 10:21:13 -0700489 }
David Sodman0c69cad2017-08-21 12:12:51 -0700490 }
491}
492
Marissa Wallfd668622018-05-10 10:21:13 -0700493bool BufferLayer::hasReadyFrame() const {
Marissa Wall024a1912018-08-13 13:55:35 -0700494 return hasFrameUpdate() || getSidebandStreamChanged() || getAutoRefresh();
Marissa Wallfd668622018-05-10 10:21:13 -0700495}
496
497uint32_t BufferLayer::getEffectiveScalingMode() const {
498 if (mOverrideScalingMode >= 0) {
499 return mOverrideScalingMode;
500 }
501
502 return mCurrentScalingMode;
503}
504
505bool BufferLayer::isProtected() const {
506 const sp<GraphicBuffer>& buffer(mActiveBuffer);
507 return (buffer != 0) && (buffer->getUsage() & GRALLOC_USAGE_PROTECTED);
508}
509
510bool BufferLayer::latchUnsignaledBuffers() {
511 static bool propertyLoaded = false;
512 static bool latch = false;
513 static std::mutex mutex;
514 std::lock_guard<std::mutex> lock(mutex);
515 if (!propertyLoaded) {
516 char value[PROPERTY_VALUE_MAX] = {};
517 property_get("debug.sf.latch_unsignaled", value, "0");
518 latch = atoi(value);
519 propertyLoaded = true;
520 }
521 return latch;
522}
523
524// h/w composer set-up
525bool BufferLayer::allTransactionsSignaled() {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800526 auto headFrameNumber = getHeadFrameNumber();
Marissa Wallfd668622018-05-10 10:21:13 -0700527 bool matchingFramesFound = false;
528 bool allTransactionsApplied = true;
529 Mutex::Autolock lock(mLocalSyncPointMutex);
530
531 for (auto& point : mLocalSyncPoints) {
532 if (point->getFrameNumber() > headFrameNumber) {
533 break;
534 }
535 matchingFramesFound = true;
536
537 if (!point->frameIsAvailable()) {
538 // We haven't notified the remote layer that the frame for
539 // this point is available yet. Notify it now, and then
540 // abort this attempt to latch.
541 point->setFrameAvailable();
542 allTransactionsApplied = false;
543 break;
544 }
545
546 allTransactionsApplied = allTransactionsApplied && point->transactionIsApplied();
547 }
548 return !matchingFramesFound || allTransactionsApplied;
David Sodman0c69cad2017-08-21 12:12:51 -0700549}
550
551// As documented in libhardware header, formats in the range
552// 0x100 - 0x1FF are specific to the HAL implementation, and
553// are known to have no alpha channel
554// TODO: move definition for device-specific range into
555// hardware.h, instead of using hard-coded values here.
556#define HARDWARE_IS_DEVICE_FORMAT(f) ((f) >= 0x100 && (f) <= 0x1FF)
557
558bool BufferLayer::getOpacityForFormat(uint32_t format) {
559 if (HARDWARE_IS_DEVICE_FORMAT(format)) {
560 return true;
561 }
562 switch (format) {
563 case HAL_PIXEL_FORMAT_RGBA_8888:
564 case HAL_PIXEL_FORMAT_BGRA_8888:
565 case HAL_PIXEL_FORMAT_RGBA_FP16:
566 case HAL_PIXEL_FORMAT_RGBA_1010102:
567 return false;
568 }
569 // in all other case, we have no blending (also for unknown formats)
570 return true;
571}
572
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800573bool BufferLayer::needsFiltering(const sp<const DisplayDevice>& displayDevice) const {
574 // If we are not capturing based on the state of a known display device, we
575 // only return mNeedsFiltering
576 if (displayDevice == nullptr) {
577 return mNeedsFiltering;
578 }
579
580 const auto outputLayer = findOutputLayerForDisplay(displayDevice);
581 if (outputLayer == nullptr) {
582 return mNeedsFiltering;
583 }
584
585 const auto& compositionState = outputLayer->getState();
586 const auto displayFrame = compositionState.displayFrame;
587 const auto sourceCrop = compositionState.sourceCrop;
Peiyong Linc2020ca2019-01-10 11:36:12 -0800588 return mNeedsFiltering || sourceCrop.getHeight() != displayFrame.getHeight() ||
589 sourceCrop.getWidth() != displayFrame.getWidth();
Chia-I Wu692e0832018-06-05 15:46:58 -0700590}
591
David Sodman0c69cad2017-08-21 12:12:51 -0700592uint64_t BufferLayer::getHeadFrameNumber() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800593 if (hasFrameUpdate()) {
Marissa Wallfd668622018-05-10 10:21:13 -0700594 return getFrameNumber();
David Sodman0c69cad2017-08-21 12:12:51 -0700595 } else {
596 return mCurrentFrameNumber;
597 }
598}
599
Vishnu Nair60356342018-11-13 13:00:45 -0800600Rect BufferLayer::getBufferSize(const State& s) const {
601 // If we have a sideband stream, or we are scaling the buffer then return the layer size since
602 // we cannot determine the buffer size.
603 if ((s.sidebandStream != nullptr) ||
604 (getEffectiveScalingMode() != NATIVE_WINDOW_SCALING_MODE_FREEZE)) {
605 return Rect(getActiveWidth(s), getActiveHeight(s));
606 }
607
608 if (mActiveBuffer == nullptr) {
609 return Rect::INVALID_RECT;
610 }
611
612 uint32_t bufWidth = mActiveBuffer->getWidth();
613 uint32_t bufHeight = mActiveBuffer->getHeight();
614
615 // Undo any transformations on the buffer and return the result.
616 if (mCurrentTransform & ui::Transform::ROT_90) {
617 std::swap(bufWidth, bufHeight);
618 }
619
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800620 if (getTransformToDisplayInverse()) {
Vishnu Nair60356342018-11-13 13:00:45 -0800621 uint32_t invTransform = DisplayDevice::getPrimaryDisplayOrientationTransform();
622 if (invTransform & ui::Transform::ROT_90) {
623 std::swap(bufWidth, bufHeight);
624 }
625 }
626
627 return Rect(bufWidth, bufHeight);
628}
629
Lloyd Piquefeb73d72018-12-04 17:23:44 -0800630std::shared_ptr<compositionengine::Layer> BufferLayer::getCompositionLayer() const {
631 return mCompositionLayer;
632}
633
Vishnu Nair4351ad52019-02-11 14:13:02 -0800634FloatRect BufferLayer::computeSourceBounds(const FloatRect& parentBounds) const {
635 const State& s(getDrawingState());
636
637 // If we have a sideband stream, or we are scaling the buffer then return the layer size since
638 // we cannot determine the buffer size.
639 if ((s.sidebandStream != nullptr) ||
640 (getEffectiveScalingMode() != NATIVE_WINDOW_SCALING_MODE_FREEZE)) {
641 return FloatRect(0, 0, getActiveWidth(s), getActiveHeight(s));
642 }
643
644 if (mActiveBuffer == nullptr) {
645 return parentBounds;
646 }
647
648 uint32_t bufWidth = mActiveBuffer->getWidth();
649 uint32_t bufHeight = mActiveBuffer->getHeight();
650
651 // Undo any transformations on the buffer and return the result.
652 if (mCurrentTransform & ui::Transform::ROT_90) {
653 std::swap(bufWidth, bufHeight);
654 }
655
656 if (getTransformToDisplayInverse()) {
657 uint32_t invTransform = DisplayDevice::getPrimaryDisplayOrientationTransform();
658 if (invTransform & ui::Transform::ROT_90) {
659 std::swap(bufWidth, bufHeight);
660 }
661 }
662
663 return FloatRect(0, 0, bufWidth, bufHeight);
664}
665
David Sodman0c69cad2017-08-21 12:12:51 -0700666} // namespace android
667
668#if defined(__gl_h_)
669#error "don't include gl/gl.h in this file"
670#endif
671
672#if defined(__gl2_h_)
673#error "don't include gl2/gl2.h in this file"
674#endif