blob: 89eee6b62197e48335619b15b34abae2bf21cbf4 [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
Lloyd Piquefeb73d72018-12-04 17:23:44 -080022#include <cmath>
23#include <cstdlib>
24#include <mutex>
25
26#include <compositionengine/CompositionEngine.h>
27#include <compositionengine/Layer.h>
28#include <compositionengine/LayerCreationArgs.h>
29#include <cutils/compiler.h>
30#include <cutils/native_handle.h>
31#include <cutils/properties.h>
32#include <gui/BufferItem.h>
33#include <gui/BufferQueue.h>
34#include <gui/LayerDebugInfo.h>
35#include <gui/Surface.h>
36#include <renderengine/RenderEngine.h>
37#include <ui/DebugUtils.h>
38#include <utils/Errors.h>
39#include <utils/Log.h>
40#include <utils/NativeHandle.h>
41#include <utils/StopWatch.h>
42#include <utils/Trace.h>
43
David Sodman0c69cad2017-08-21 12:12:51 -070044#include "BufferLayer.h"
45#include "Colorizer.h"
46#include "DisplayDevice.h"
47#include "LayerRejecter.h"
David Sodman0c69cad2017-08-21 12:12:51 -070048
Yiwei Zhang7e666a52018-11-15 13:33:42 -080049#include "TimeStats/TimeStats.h"
50
David Sodman0c69cad2017-08-21 12:12:51 -070051namespace android {
52
Lloyd Pique42ab75e2018-09-12 20:46:03 -070053BufferLayer::BufferLayer(const LayerCreationArgs& args)
Lloyd Piquefeb73d72018-12-04 17:23:44 -080054 : Layer(args),
55 mTextureName(args.flinger->getNewTexture()),
56 mCompositionLayer{mFlinger->getCompositionEngine().createLayer(
57 compositionengine::LayerCreationArgs{this})} {
Lloyd Pique42ab75e2018-09-12 20:46:03 -070058 ALOGV("Creating Layer %s", args.name.string());
David Sodman0c69cad2017-08-21 12:12:51 -070059
Lloyd Pique42ab75e2018-09-12 20:46:03 -070060 mPremultipliedAlpha = !(args.flags & ISurfaceComposerClient::eNonPremultiplied);
David Sodman0c69cad2017-08-21 12:12:51 -070061
Lloyd Pique42ab75e2018-09-12 20:46:03 -070062 mPotentialCursor = args.flags & ISurfaceComposerClient::eCursorWindow;
63 mProtectedByApp = args.flags & ISurfaceComposerClient::eProtectedByApp;
David Sodman0c69cad2017-08-21 12:12:51 -070064}
65
66BufferLayer::~BufferLayer() {
David Sodman0c69cad2017-08-21 12:12:51 -070067 mFlinger->deleteTextureAsync(mTextureName);
68
David Sodman6f65f3e2017-11-03 14:28:09 -070069 if (!getBE().mHwcLayers.empty()) {
David Sodman0c69cad2017-08-21 12:12:51 -070070 ALOGE("Found stale hardware composer layers when destroying "
71 "surface flinger layer %s",
72 mName.string());
chaviw61626f22018-11-15 16:26:27 -080073 destroyAllHwcLayersPlusChildren();
David Sodman0c69cad2017-08-21 12:12:51 -070074 }
Yiwei Zhangdc224042018-10-18 15:34:00 -070075
Yiwei Zhang7e666a52018-11-15 13:33:42 -080076 mFlinger->mTimeStats->onDestroy(getSequence());
David Sodman0c69cad2017-08-21 12:12:51 -070077}
78
David Sodmaneb085e02017-10-05 18:49:04 -070079void BufferLayer::useSurfaceDamage() {
80 if (mFlinger->mForceFullDamage) {
81 surfaceDamageRegion = Region::INVALID_REGION;
82 } else {
Marissa Wallfd668622018-05-10 10:21:13 -070083 surfaceDamageRegion = getDrawingSurfaceDamage();
David Sodmaneb085e02017-10-05 18:49:04 -070084 }
85}
86
87void BufferLayer::useEmptyDamage() {
88 surfaceDamageRegion.clear();
89}
90
Marissa Wallfd668622018-05-10 10:21:13 -070091bool BufferLayer::isOpaque(const Layer::State& s) const {
92 // if we don't have a buffer or sidebandStream yet, we're translucent regardless of the
93 // layer's opaque flag.
94 if ((getBE().compositionInfo.hwc.sidebandStream == nullptr) && (mActiveBuffer == nullptr)) {
95 return false;
96 }
97
98 // if the layer has the opaque flag, then we're always opaque,
99 // otherwise we use the current buffer's format.
100 return ((s.flags & layer_state_t::eLayerOpaque) != 0) || getOpacityForFormat(getPixelFormat());
David Sodman0c69cad2017-08-21 12:12:51 -0700101}
102
103bool BufferLayer::isVisible() const {
104 return !(isHiddenByPolicy()) && getAlpha() > 0.0f &&
David Sodman0cf8f8d2017-12-20 18:19:45 -0800105 (mActiveBuffer != nullptr || getBE().compositionInfo.hwc.sidebandStream != nullptr);
David Sodman0c69cad2017-08-21 12:12:51 -0700106}
107
108bool BufferLayer::isFixedSize() const {
109 return getEffectiveScalingMode() != NATIVE_WINDOW_SCALING_MODE_FREEZE;
110}
111
David Sodman0c69cad2017-08-21 12:12:51 -0700112static constexpr mat4 inverseOrientation(uint32_t transform) {
David Sodman41fdfc92017-11-06 16:09:56 -0800113 const mat4 flipH(-1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1);
114 const mat4 flipV(1, 0, 0, 0, 0, -1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1);
115 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 -0700116 mat4 tr;
117
118 if (transform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
119 tr = tr * rot90;
120 }
121 if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_H) {
122 tr = tr * flipH;
123 }
124 if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_V) {
125 tr = tr * flipV;
126 }
127 return inverse(tr);
128}
129
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000130bool BufferLayer::prepareClientLayer(const RenderArea& renderArea, const Region& clip,
131 bool useIdentityTransform, Region& clearRegion,
132 renderengine::LayerSettings& layer) {
David Sodman0c69cad2017-08-21 12:12:51 -0700133 ATRACE_CALL();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000134 Layer::prepareClientLayer(renderArea, clip, useIdentityTransform, clearRegion, layer);
David Sodman0cf8f8d2017-12-20 18:19:45 -0800135 if (CC_UNLIKELY(mActiveBuffer == 0)) {
David Sodman0c69cad2017-08-21 12:12:51 -0700136 // the texture has not been created yet, this Layer has
137 // in fact never been drawn into. This happens frequently with
138 // SurfaceView because the WindowManager can't know when the client
139 // has drawn the first time.
140
141 // If there is nothing under us, we paint the screen in black, otherwise
142 // we just skip this update.
143
144 // figure out if there is something below us
145 Region under;
146 bool finished = false;
147 mFlinger->mDrawingState.traverseInZOrder([&](Layer* layer) {
148 if (finished || layer == static_cast<BufferLayer const*>(this)) {
149 finished = true;
150 return;
151 }
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000152 under.orSelf(layer->visibleRegion);
David Sodman0c69cad2017-08-21 12:12:51 -0700153 });
154 // if not everything below us is covered, we plug the holes!
155 Region holes(clip.subtract(under));
156 if (!holes.isEmpty()) {
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000157 clearRegion.orSelf(holes);
David Sodman0c69cad2017-08-21 12:12:51 -0700158 }
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000159 return false;
David Sodman0c69cad2017-08-21 12:12:51 -0700160 }
David Sodman0c69cad2017-08-21 12:12:51 -0700161 bool blackOutLayer = isProtected() || (isSecure() && !renderArea.isSecure());
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000162 const State& s(getDrawingState());
David Sodman0c69cad2017-08-21 12:12:51 -0700163 if (!blackOutLayer) {
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000164 layer.source.buffer.buffer = mActiveBuffer;
165 layer.source.buffer.isOpaque = isOpaque(s);
166 layer.source.buffer.fence = mActiveBufferFence;
167 layer.source.buffer.cacheHint = useCachedBufferForClientComposition()
168 ? renderengine::Buffer::CachingHint::USE_CACHE
169 : renderengine::Buffer::CachingHint::NO_CACHE;
170 layer.source.buffer.textureName = mTextureName;
171 layer.source.buffer.usePremultipliedAlpha = getPremultipledAlpha();
172 layer.source.buffer.isY410BT2020 = isHdrY410();
David Sodman0c69cad2017-08-21 12:12:51 -0700173 // TODO: we could be more subtle with isFixedSize()
Peiyong Linc2020ca2019-01-10 11:36:12 -0800174 const bool useFiltering = needsFiltering() || renderArea.needsFiltering() || isFixedSize();
David Sodman0c69cad2017-08-21 12:12:51 -0700175
176 // Query the texture matrix given our current filtering mode.
177 float textureMatrix[16];
Marissa Wallfd668622018-05-10 10:21:13 -0700178 setFilteringEnabled(useFiltering);
179 getDrawingTransformMatrix(textureMatrix);
David Sodman0c69cad2017-08-21 12:12:51 -0700180
181 if (getTransformToDisplayInverse()) {
182 /*
183 * the code below applies the primary display's inverse transform to
184 * the texture transform
185 */
186 uint32_t transform = DisplayDevice::getPrimaryDisplayOrientationTransform();
187 mat4 tr = inverseOrientation(transform);
188
189 /**
190 * TODO(b/36727915): This is basically a hack.
191 *
192 * Ensure that regardless of the parent transformation,
193 * this buffer is always transformed from native display
194 * orientation to display orientation. For example, in the case
195 * of a camera where the buffer remains in native orientation,
196 * we want the pixels to always be upright.
197 */
198 sp<Layer> p = mDrawingParent.promote();
199 if (p != nullptr) {
200 const auto parentTransform = p->getTransform();
201 tr = tr * inverseOrientation(parentTransform.getOrientation());
202 }
203
204 // and finally apply it to the original texture matrix
205 const mat4 texTransform(mat4(static_cast<const float*>(textureMatrix)) * tr);
206 memcpy(textureMatrix, texTransform.asArray(), sizeof(textureMatrix));
207 }
208
Vishnu Nair4351ad52019-02-11 14:13:02 -0800209 const Rect win{getBounds()};
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000210 const float bufferWidth = getBufferSize(s).getWidth();
211 const float bufferHeight = getBufferSize(s).getHeight();
David Sodman0c69cad2017-08-21 12:12:51 -0700212
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000213 const float scaleHeight = (float(win.bottom) - float(win.top)) / bufferHeight;
214 const float scaleWidth = (float(win.right) - float(win.left)) / bufferWidth;
215 const float translateY = float(win.top) / bufferHeight;
216 const float translateX = float(win.left) / bufferWidth;
217
218 // Flip y-coordinates because GLConsumer expects OpenGL convention.
219 mat4 tr = mat4::translate(vec4(.5, .5, 0, 1)) * mat4::scale(vec4(1, -1, 1, 1)) *
220 mat4::translate(vec4(-.5, -.5, 0, 1)) *
221 mat4::translate(vec4(translateX, translateY, 0, 1)) *
222 mat4::scale(vec4(scaleWidth, scaleHeight, 1.0, 1.0));
223
224 layer.source.buffer.useTextureFiltering = useFiltering;
225 layer.source.buffer.textureTransform = mat4(static_cast<const float*>(textureMatrix)) * tr;
David Sodman0c69cad2017-08-21 12:12:51 -0700226 } else {
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000227 // If layer is blacked out, force alpha to 1 so that we draw a black color
228 // layer.
229 layer.source.buffer.buffer = nullptr;
230 layer.alpha = 1.0;
David Sodman0c69cad2017-08-21 12:12:51 -0700231 }
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000232
233 return true;
David Sodman0c69cad2017-08-21 12:12:51 -0700234}
235
Marissa Wallfd668622018-05-10 10:21:13 -0700236bool BufferLayer::isHdrY410() const {
237 // pixel format is HDR Y410 masquerading as RGBA_1010102
238 return (mCurrentDataSpace == ui::Dataspace::BT2020_ITU_PQ &&
239 getDrawingApi() == NATIVE_WINDOW_API_MEDIA &&
240 getBE().compositionInfo.mBuffer->getPixelFormat() == HAL_PIXEL_FORMAT_RGBA_1010102);
David Sodmaneb085e02017-10-05 18:49:04 -0700241}
242
Dominik Laskowski075d3172018-05-24 15:50:06 -0700243void BufferLayer::setPerFrameData(DisplayId displayId, const ui::Transform& transform,
244 const Rect& viewport, int32_t supportedPerFrameMetadata) {
Dominik Laskowski34157762018-10-31 13:07:19 -0700245 RETURN_IF_NO_HWC_LAYER(displayId);
246
David Sodman0c69cad2017-08-21 12:12:51 -0700247 // Apply this display's projection's viewport to the visible region
248 // before giving it to the HWC HAL.
Dominik Laskowski075d3172018-05-24 15:50:06 -0700249 Region visible = transform.transform(visibleRegion.intersect(viewport));
250
David Sodman15094112018-10-11 09:39:37 -0700251 auto& hwcInfo = getBE().mHwcLayers[displayId];
252 auto& hwcLayer = hwcInfo.layer;
253 auto error = hwcLayer->setVisibleRegion(visible);
254 if (error != HWC2::Error::None) {
255 ALOGE("[%s] Failed to set visible region: %s (%d)", mName.string(),
256 to_string(error).c_str(), static_cast<int32_t>(error));
257 visible.dump(LOG_TAG);
258 }
David Sodmanba340492018-08-05 21:51:33 -0700259 getBE().compositionInfo.hwc.visibleRegion = visible;
David Sodman15094112018-10-11 09:39:37 -0700260
261 error = hwcLayer->setSurfaceDamage(surfaceDamageRegion);
262 if (error != HWC2::Error::None) {
263 ALOGE("[%s] Failed to set surface damage: %s (%d)", mName.string(),
264 to_string(error).c_str(), static_cast<int32_t>(error));
265 surfaceDamageRegion.dump(LOG_TAG);
266 }
David Sodmanba340492018-08-05 21:51:33 -0700267 getBE().compositionInfo.hwc.surfaceDamage = surfaceDamageRegion;
David Sodman0c69cad2017-08-21 12:12:51 -0700268
269 // Sideband layers
David Sodman0cc69182017-11-17 12:12:07 -0800270 if (getBE().compositionInfo.hwc.sidebandStream.get()) {
Dominik Laskowski7e045462018-05-30 13:02:02 -0700271 setCompositionType(displayId, HWC2::Composition::Sideband);
David Sodman15094112018-10-11 09:39:37 -0700272 ALOGV("[%s] Requesting Sideband composition", mName.string());
273 error = hwcLayer->setSidebandStream(getBE().compositionInfo.hwc.sidebandStream->handle());
274 if (error != HWC2::Error::None) {
275 ALOGE("[%s] Failed to set sideband stream %p: %s (%d)", mName.string(),
276 getBE().compositionInfo.hwc.sidebandStream->handle(), to_string(error).c_str(),
277 static_cast<int32_t>(error));
278 }
David Sodmanba340492018-08-05 21:51:33 -0700279 getBE().compositionInfo.compositionType = HWC2::Composition::Sideband;
David Sodman0c69cad2017-08-21 12:12:51 -0700280 return;
281 }
282
David Sodman15094112018-10-11 09:39:37 -0700283 // Device or Cursor layers
284 if (mPotentialCursor) {
285 ALOGV("[%s] Requesting Cursor composition", mName.string());
286 setCompositionType(displayId, HWC2::Composition::Cursor);
287 } else {
288 ALOGV("[%s] Requesting Device composition", mName.string());
289 setCompositionType(displayId, HWC2::Composition::Device);
David Sodman0c69cad2017-08-21 12:12:51 -0700290 }
291
David Sodman15094112018-10-11 09:39:37 -0700292 ALOGV("setPerFrameData: dataspace = %d", mCurrentDataSpace);
293 error = hwcLayer->setDataspace(mCurrentDataSpace);
294 if (error != HWC2::Error::None) {
295 ALOGE("[%s] Failed to set dataspace %d: %s (%d)", mName.string(), mCurrentDataSpace,
296 to_string(error).c_str(), static_cast<int32_t>(error));
297 }
298
299 const HdrMetadata& metadata = getDrawingHdrMetadata();
Dominik Laskowski075d3172018-05-24 15:50:06 -0700300 error = hwcLayer->setPerFrameMetadata(supportedPerFrameMetadata, metadata);
David Sodman15094112018-10-11 09:39:37 -0700301 if (error != HWC2::Error::None && error != HWC2::Error::Unsupported) {
302 ALOGE("[%s] Failed to set hdrMetadata: %s (%d)", mName.string(),
303 to_string(error).c_str(), static_cast<int32_t>(error));
304 }
305
306 error = hwcLayer->setColorTransform(getColorTransform());
307 if (error != HWC2::Error::None) {
308 ALOGE("[%s] Failed to setColorTransform: %s (%d)", mName.string(),
309 to_string(error).c_str(), static_cast<int32_t>(error));
310 }
David Sodmanba340492018-08-05 21:51:33 -0700311 getBE().compositionInfo.hwc.dataspace = mCurrentDataSpace;
312 getBE().compositionInfo.hwc.hdrMetadata = getDrawingHdrMetadata();
Dominik Laskowski075d3172018-05-24 15:50:06 -0700313 getBE().compositionInfo.hwc.supportedPerFrameMetadata = supportedPerFrameMetadata;
Peiyong Lind3788632018-09-18 16:01:31 -0700314 getBE().compositionInfo.hwc.colorTransform = getColorTransform();
Lloyd Pique074e8122018-07-26 12:57:23 -0700315
Dominik Laskowski075d3172018-05-24 15:50:06 -0700316 setHwcLayerBuffer(displayId);
David Sodman0c69cad2017-08-21 12:12:51 -0700317}
318
Marissa Wallfd668622018-05-10 10:21:13 -0700319bool BufferLayer::onPreComposition(nsecs_t refreshStartTime) {
320 if (mBufferLatched) {
321 Mutex::Autolock lock(mFrameEventHistoryMutex);
322 mFrameEventHistory.addPreComposition(mCurrentFrameNumber, refreshStartTime);
David Sodman0c69cad2017-08-21 12:12:51 -0700323 }
Marissa Wallfd668622018-05-10 10:21:13 -0700324 mRefreshPending = false;
325 return hasReadyFrame();
David Sodman0c69cad2017-08-21 12:12:51 -0700326}
327
Dominik Laskowski075d3172018-05-24 15:50:06 -0700328bool BufferLayer::onPostComposition(const std::optional<DisplayId>& displayId,
329 const std::shared_ptr<FenceTime>& glDoneFence,
Marissa Wallfd668622018-05-10 10:21:13 -0700330 const std::shared_ptr<FenceTime>& presentFence,
331 const CompositorTiming& compositorTiming) {
332 // mFrameLatencyNeeded is true when a new frame was latched for the
333 // composition.
334 if (!mFrameLatencyNeeded) return false;
335
336 // Update mFrameEventHistory.
Dan Stoza436ccf32018-06-21 12:10:12 -0700337 {
Marissa Wallfd668622018-05-10 10:21:13 -0700338 Mutex::Autolock lock(mFrameEventHistoryMutex);
339 mFrameEventHistory.addPostComposition(mCurrentFrameNumber, glDoneFence, presentFence,
340 compositorTiming);
David Sodman0c69cad2017-08-21 12:12:51 -0700341 }
342
Marissa Wallfd668622018-05-10 10:21:13 -0700343 // Update mFrameTracker.
344 nsecs_t desiredPresentTime = getDesiredPresentTime();
345 mFrameTracker.setDesiredPresentTime(desiredPresentTime);
346
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700347 const int32_t layerID = getSequence();
Yiwei Zhang7e666a52018-11-15 13:33:42 -0800348 mFlinger->mTimeStats->setDesiredTime(layerID, mCurrentFrameNumber, desiredPresentTime);
Marissa Wallfd668622018-05-10 10:21:13 -0700349
350 std::shared_ptr<FenceTime> frameReadyFence = getCurrentFenceTime();
351 if (frameReadyFence->isValid()) {
352 mFrameTracker.setFrameReadyFence(std::move(frameReadyFence));
353 } else {
354 // There was no fence for this frame, so assume that it was ready
355 // to be presented at the desired present time.
356 mFrameTracker.setFrameReadyTime(desiredPresentTime);
Dominik Laskowski45de9bd2018-06-11 17:44:10 -0700357 }
Marissa Wallfd668622018-05-10 10:21:13 -0700358
359 if (presentFence->isValid()) {
Yiwei Zhang7e666a52018-11-15 13:33:42 -0800360 mFlinger->mTimeStats->setPresentFence(layerID, mCurrentFrameNumber, presentFence);
Marissa Wallfd668622018-05-10 10:21:13 -0700361 mFrameTracker.setActualPresentFence(std::shared_ptr<FenceTime>(presentFence));
Dominik Laskowski075d3172018-05-24 15:50:06 -0700362 } else if (displayId && mFlinger->getHwComposer().isConnected(*displayId)) {
Marissa Wallfd668622018-05-10 10:21:13 -0700363 // The HWC doesn't support present fences, so use the refresh
364 // timestamp instead.
Dominik Laskowski075d3172018-05-24 15:50:06 -0700365 const nsecs_t actualPresentTime = mFlinger->getHwComposer().getRefreshTimestamp(*displayId);
Yiwei Zhang7e666a52018-11-15 13:33:42 -0800366 mFlinger->mTimeStats->setPresentTime(layerID, mCurrentFrameNumber, actualPresentTime);
Marissa Wallfd668622018-05-10 10:21:13 -0700367 mFrameTracker.setActualPresentTime(actualPresentTime);
368 }
369
370 mFrameTracker.advanceFrame();
371 mFrameLatencyNeeded = false;
372 return true;
David Sodman0c69cad2017-08-21 12:12:51 -0700373}
374
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800375bool BufferLayer::latchBuffer(bool& recomputeVisibleRegions, nsecs_t latchTime,
376 const sp<Fence>& releaseFence) {
Marissa Wallfd668622018-05-10 10:21:13 -0700377 ATRACE_CALL();
David Sodman0c69cad2017-08-21 12:12:51 -0700378
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800379 bool refreshRequired = latchSidebandStream(recomputeVisibleRegions);
David Sodman0c69cad2017-08-21 12:12:51 -0700380
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800381 if (refreshRequired) {
382 return refreshRequired;
David Sodman0c69cad2017-08-21 12:12:51 -0700383 }
384
Marissa Wallfd668622018-05-10 10:21:13 -0700385 if (!hasReadyFrame()) {
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800386 return false;
David Sodman0c69cad2017-08-21 12:12:51 -0700387 }
David Sodman0c69cad2017-08-21 12:12:51 -0700388
Marissa Wallfd668622018-05-10 10:21:13 -0700389 // if we've already called updateTexImage() without going through
390 // a composition step, we have to skip this layer at this point
391 // because we cannot call updateTeximage() without a corresponding
392 // compositionComplete() call.
393 // we'll trigger an update in onPreComposition().
394 if (mRefreshPending) {
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800395 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700396 }
397
398 // If the head buffer's acquire fence hasn't signaled yet, return and
399 // try again later
400 if (!fenceHasSignaled()) {
David Sodman0c69cad2017-08-21 12:12:51 -0700401 mFlinger->signalLayerUpdate();
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800402 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700403 }
404
405 // Capture the old state of the layer for comparisons later
406 const State& s(getDrawingState());
407 const bool oldOpacity = isOpaque(s);
408 sp<GraphicBuffer> oldBuffer = mActiveBuffer;
409
410 if (!allTransactionsSignaled()) {
411 mFlinger->signalLayerUpdate();
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800412 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700413 }
414
Alec Mouri86770e52018-09-24 22:40:58 +0000415 status_t err = updateTexImage(recomputeVisibleRegions, latchTime, releaseFence);
Marissa Wallfd668622018-05-10 10:21:13 -0700416 if (err != NO_ERROR) {
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800417 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700418 }
419
420 err = updateActiveBuffer();
421 if (err != NO_ERROR) {
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800422 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700423 }
424
425 mBufferLatched = true;
426
427 err = updateFrameNumber(latchTime);
428 if (err != NO_ERROR) {
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800429 return false;
Marissa Wallfd668622018-05-10 10:21:13 -0700430 }
431
432 mRefreshPending = true;
433 mFrameLatencyNeeded = true;
434 if (oldBuffer == nullptr) {
435 // the first time we receive a buffer, we need to trigger a
436 // geometry invalidation.
437 recomputeVisibleRegions = true;
438 }
439
440 ui::Dataspace dataSpace = getDrawingDataSpace();
Peiyong Lin14724e62018-12-05 07:27:30 -0800441 // translate legacy dataspaces to modern dataspaces
Marissa Wallfd668622018-05-10 10:21:13 -0700442 switch (dataSpace) {
Peiyong Lin14724e62018-12-05 07:27:30 -0800443 case ui::Dataspace::SRGB:
444 dataSpace = ui::Dataspace::V0_SRGB;
Marissa Wallfd668622018-05-10 10:21:13 -0700445 break;
Peiyong Lin14724e62018-12-05 07:27:30 -0800446 case ui::Dataspace::SRGB_LINEAR:
447 dataSpace = ui::Dataspace::V0_SRGB_LINEAR;
Marissa Wallfd668622018-05-10 10:21:13 -0700448 break;
Peiyong Lin14724e62018-12-05 07:27:30 -0800449 case ui::Dataspace::JFIF:
450 dataSpace = ui::Dataspace::V0_JFIF;
Marissa Wallfd668622018-05-10 10:21:13 -0700451 break;
Peiyong Lin14724e62018-12-05 07:27:30 -0800452 case ui::Dataspace::BT601_625:
453 dataSpace = ui::Dataspace::V0_BT601_625;
Marissa Wallfd668622018-05-10 10:21:13 -0700454 break;
Peiyong Lin14724e62018-12-05 07:27:30 -0800455 case ui::Dataspace::BT601_525:
456 dataSpace = ui::Dataspace::V0_BT601_525;
Marissa Wallfd668622018-05-10 10:21:13 -0700457 break;
Peiyong Lin14724e62018-12-05 07:27:30 -0800458 case ui::Dataspace::BT709:
459 dataSpace = ui::Dataspace::V0_BT709;
Marissa Wallfd668622018-05-10 10:21:13 -0700460 break;
461 default:
462 break;
463 }
464 mCurrentDataSpace = dataSpace;
465
466 Rect crop(getDrawingCrop());
467 const uint32_t transform(getDrawingTransform());
468 const uint32_t scalingMode(getDrawingScalingMode());
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800469 const bool transformToDisplayInverse(getTransformToDisplayInverse());
Marissa Wallfd668622018-05-10 10:21:13 -0700470 if ((crop != mCurrentCrop) || (transform != mCurrentTransform) ||
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800471 (scalingMode != mCurrentScalingMode) ||
472 (transformToDisplayInverse != mTransformToDisplayInverse)) {
Marissa Wallfd668622018-05-10 10:21:13 -0700473 mCurrentCrop = crop;
474 mCurrentTransform = transform;
475 mCurrentScalingMode = scalingMode;
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800476 mTransformToDisplayInverse = transformToDisplayInverse;
Marissa Wallfd668622018-05-10 10:21:13 -0700477 recomputeVisibleRegions = true;
478 }
479
480 if (oldBuffer != nullptr) {
481 uint32_t bufWidth = mActiveBuffer->getWidth();
482 uint32_t bufHeight = mActiveBuffer->getHeight();
483 if (bufWidth != uint32_t(oldBuffer->width) || bufHeight != uint32_t(oldBuffer->height)) {
484 recomputeVisibleRegions = true;
485 }
486 }
487
488 if (oldOpacity != isOpaque(s)) {
489 recomputeVisibleRegions = true;
490 }
491
492 // Remove any sync points corresponding to the buffer which was just
493 // latched
494 {
495 Mutex::Autolock lock(mLocalSyncPointMutex);
496 auto point = mLocalSyncPoints.begin();
497 while (point != mLocalSyncPoints.end()) {
498 if (!(*point)->frameIsAvailable() || !(*point)->transactionIsApplied()) {
499 // This sync point must have been added since we started
500 // latching. Don't drop it yet.
501 ++point;
502 continue;
503 }
504
505 if ((*point)->getFrameNumber() <= mCurrentFrameNumber) {
506 point = mLocalSyncPoints.erase(point);
507 } else {
508 ++point;
509 }
510 }
511 }
512
Vishnu Nair6194e2e2019-02-06 12:58:39 -0800513 return true;
Marissa Wallfd668622018-05-10 10:21:13 -0700514}
515
516// transaction
517void BufferLayer::notifyAvailableFrames() {
518 auto headFrameNumber = getHeadFrameNumber();
519 bool headFenceSignaled = fenceHasSignaled();
520 Mutex::Autolock lock(mLocalSyncPointMutex);
521 for (auto& point : mLocalSyncPoints) {
522 if (headFrameNumber >= point->getFrameNumber() && headFenceSignaled) {
523 point->setFrameAvailable();
524 }
David Sodman0c69cad2017-08-21 12:12:51 -0700525 }
526}
527
Marissa Wallfd668622018-05-10 10:21:13 -0700528bool BufferLayer::hasReadyFrame() const {
Marissa Wall024a1912018-08-13 13:55:35 -0700529 return hasFrameUpdate() || getSidebandStreamChanged() || getAutoRefresh();
Marissa Wallfd668622018-05-10 10:21:13 -0700530}
531
532uint32_t BufferLayer::getEffectiveScalingMode() const {
533 if (mOverrideScalingMode >= 0) {
534 return mOverrideScalingMode;
535 }
536
537 return mCurrentScalingMode;
538}
539
540bool BufferLayer::isProtected() const {
541 const sp<GraphicBuffer>& buffer(mActiveBuffer);
542 return (buffer != 0) && (buffer->getUsage() & GRALLOC_USAGE_PROTECTED);
543}
544
545bool BufferLayer::latchUnsignaledBuffers() {
546 static bool propertyLoaded = false;
547 static bool latch = false;
548 static std::mutex mutex;
549 std::lock_guard<std::mutex> lock(mutex);
550 if (!propertyLoaded) {
551 char value[PROPERTY_VALUE_MAX] = {};
552 property_get("debug.sf.latch_unsignaled", value, "0");
553 latch = atoi(value);
554 propertyLoaded = true;
555 }
556 return latch;
557}
558
559// h/w composer set-up
560bool BufferLayer::allTransactionsSignaled() {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800561 auto headFrameNumber = getHeadFrameNumber();
Marissa Wallfd668622018-05-10 10:21:13 -0700562 bool matchingFramesFound = false;
563 bool allTransactionsApplied = true;
564 Mutex::Autolock lock(mLocalSyncPointMutex);
565
566 for (auto& point : mLocalSyncPoints) {
567 if (point->getFrameNumber() > headFrameNumber) {
568 break;
569 }
570 matchingFramesFound = true;
571
572 if (!point->frameIsAvailable()) {
573 // We haven't notified the remote layer that the frame for
574 // this point is available yet. Notify it now, and then
575 // abort this attempt to latch.
576 point->setFrameAvailable();
577 allTransactionsApplied = false;
578 break;
579 }
580
581 allTransactionsApplied = allTransactionsApplied && point->transactionIsApplied();
582 }
583 return !matchingFramesFound || allTransactionsApplied;
David Sodman0c69cad2017-08-21 12:12:51 -0700584}
585
586// As documented in libhardware header, formats in the range
587// 0x100 - 0x1FF are specific to the HAL implementation, and
588// are known to have no alpha channel
589// TODO: move definition for device-specific range into
590// hardware.h, instead of using hard-coded values here.
591#define HARDWARE_IS_DEVICE_FORMAT(f) ((f) >= 0x100 && (f) <= 0x1FF)
592
593bool BufferLayer::getOpacityForFormat(uint32_t format) {
594 if (HARDWARE_IS_DEVICE_FORMAT(format)) {
595 return true;
596 }
597 switch (format) {
598 case HAL_PIXEL_FORMAT_RGBA_8888:
599 case HAL_PIXEL_FORMAT_BGRA_8888:
600 case HAL_PIXEL_FORMAT_RGBA_FP16:
601 case HAL_PIXEL_FORMAT_RGBA_1010102:
602 return false;
603 }
604 // in all other case, we have no blending (also for unknown formats)
605 return true;
606}
607
Peiyong Linc2020ca2019-01-10 11:36:12 -0800608bool BufferLayer::needsFiltering() const {
609 const auto displayFrame = getBE().compositionInfo.hwc.displayFrame;
610 const auto sourceCrop = getBE().compositionInfo.hwc.sourceCrop;
611 return mNeedsFiltering || sourceCrop.getHeight() != displayFrame.getHeight() ||
612 sourceCrop.getWidth() != displayFrame.getWidth();
Chia-I Wu692e0832018-06-05 15:46:58 -0700613}
614
David Sodman0c69cad2017-08-21 12:12:51 -0700615uint64_t BufferLayer::getHeadFrameNumber() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800616 if (hasFrameUpdate()) {
Marissa Wallfd668622018-05-10 10:21:13 -0700617 return getFrameNumber();
David Sodman0c69cad2017-08-21 12:12:51 -0700618 } else {
619 return mCurrentFrameNumber;
620 }
621}
622
Vishnu Nair60356342018-11-13 13:00:45 -0800623Rect BufferLayer::getBufferSize(const State& s) const {
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 Rect(getActiveWidth(s), getActiveHeight(s));
629 }
630
631 if (mActiveBuffer == nullptr) {
632 return Rect::INVALID_RECT;
633 }
634
635 uint32_t bufWidth = mActiveBuffer->getWidth();
636 uint32_t bufHeight = mActiveBuffer->getHeight();
637
638 // Undo any transformations on the buffer and return the result.
639 if (mCurrentTransform & ui::Transform::ROT_90) {
640 std::swap(bufWidth, bufHeight);
641 }
642
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800643 if (getTransformToDisplayInverse()) {
Vishnu Nair60356342018-11-13 13:00:45 -0800644 uint32_t invTransform = DisplayDevice::getPrimaryDisplayOrientationTransform();
645 if (invTransform & ui::Transform::ROT_90) {
646 std::swap(bufWidth, bufHeight);
647 }
648 }
649
650 return Rect(bufWidth, bufHeight);
651}
652
Lloyd Piquefeb73d72018-12-04 17:23:44 -0800653std::shared_ptr<compositionengine::Layer> BufferLayer::getCompositionLayer() const {
654 return mCompositionLayer;
655}
656
Vishnu Nair4351ad52019-02-11 14:13:02 -0800657FloatRect BufferLayer::computeSourceBounds(const FloatRect& parentBounds) const {
658 const State& s(getDrawingState());
659
660 // If we have a sideband stream, or we are scaling the buffer then return the layer size since
661 // we cannot determine the buffer size.
662 if ((s.sidebandStream != nullptr) ||
663 (getEffectiveScalingMode() != NATIVE_WINDOW_SCALING_MODE_FREEZE)) {
664 return FloatRect(0, 0, getActiveWidth(s), getActiveHeight(s));
665 }
666
667 if (mActiveBuffer == nullptr) {
668 return parentBounds;
669 }
670
671 uint32_t bufWidth = mActiveBuffer->getWidth();
672 uint32_t bufHeight = mActiveBuffer->getHeight();
673
674 // Undo any transformations on the buffer and return the result.
675 if (mCurrentTransform & ui::Transform::ROT_90) {
676 std::swap(bufWidth, bufHeight);
677 }
678
679 if (getTransformToDisplayInverse()) {
680 uint32_t invTransform = DisplayDevice::getPrimaryDisplayOrientationTransform();
681 if (invTransform & ui::Transform::ROT_90) {
682 std::swap(bufWidth, bufHeight);
683 }
684 }
685
686 return FloatRect(0, 0, bufWidth, bufHeight);
687}
688
David Sodman0c69cad2017-08-21 12:12:51 -0700689} // namespace android
690
691#if defined(__gl_h_)
692#error "don't include gl/gl.h in this file"
693#endif
694
695#if defined(__gl2_h_)
696#error "don't include gl2/gl2.h in this file"
697#endif