blob: f98632924579fd4aec8ef5c4f8b448e40529e38e [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 Mouri0f714832018-11-12 15:31:06 -0800130bool 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 Mouri0f714832018-11-12 15:31:06 -0800134 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 Mouri0f714832018-11-12 15:31:06 -0800152 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 Mouri0f714832018-11-12 15:31:06 -0800157 clearRegion.orSelf(holes);
David Sodman0c69cad2017-08-21 12:12:51 -0700158 }
Alec Mouri0f714832018-11-12 15:31:06 -0800159 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 Mouri0f714832018-11-12 15:31:06 -0800162 const State& s(getDrawingState());
David Sodman0c69cad2017-08-21 12:12:51 -0700163 if (!blackOutLayer) {
Alec Mouri0f714832018-11-12 15:31:06 -0800164 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
Alec Mouri0f714832018-11-12 15:31:06 -0800209 const Rect win{computeBounds()};
210 const float bufferWidth = getBufferSize(s).getWidth();
211 const float bufferHeight = getBufferSize(s).getHeight();
David Sodman0c69cad2017-08-21 12:12:51 -0700212
Alec Mouri0f714832018-11-12 15:31:06 -0800213 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 Mouri0f714832018-11-12 15:31:06 -0800227 // 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 Mouri0f714832018-11-12 15:31:06 -0800232
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
Alec Mouri86770e52018-09-24 22:40:58 +0000375Region 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
Marissa Wallfd668622018-05-10 10:21:13 -0700379 std::optional<Region> sidebandStreamDirtyRegion = latchSidebandStream(recomputeVisibleRegions);
David Sodman0c69cad2017-08-21 12:12:51 -0700380
Marissa Wallfd668622018-05-10 10:21:13 -0700381 if (sidebandStreamDirtyRegion) {
382 return *sidebandStreamDirtyRegion;
David Sodman0c69cad2017-08-21 12:12:51 -0700383 }
384
Marissa Wallfd668622018-05-10 10:21:13 -0700385 Region dirtyRegion;
David Sodman0c69cad2017-08-21 12:12:51 -0700386
Marissa Wallfd668622018-05-10 10:21:13 -0700387 if (!hasReadyFrame()) {
388 return dirtyRegion;
David Sodman0c69cad2017-08-21 12:12:51 -0700389 }
David Sodman0c69cad2017-08-21 12:12:51 -0700390
Marissa Wallfd668622018-05-10 10:21:13 -0700391 // if we've already called updateTexImage() without going through
392 // a composition step, we have to skip this layer at this point
393 // because we cannot call updateTeximage() without a corresponding
394 // compositionComplete() call.
395 // we'll trigger an update in onPreComposition().
396 if (mRefreshPending) {
397 return dirtyRegion;
398 }
399
400 // If the head buffer's acquire fence hasn't signaled yet, return and
401 // try again later
402 if (!fenceHasSignaled()) {
David Sodman0c69cad2017-08-21 12:12:51 -0700403 mFlinger->signalLayerUpdate();
Marissa Wallfd668622018-05-10 10:21:13 -0700404 return dirtyRegion;
405 }
406
407 // Capture the old state of the layer for comparisons later
408 const State& s(getDrawingState());
409 const bool oldOpacity = isOpaque(s);
410 sp<GraphicBuffer> oldBuffer = mActiveBuffer;
411
412 if (!allTransactionsSignaled()) {
413 mFlinger->signalLayerUpdate();
414 return dirtyRegion;
415 }
416
Alec Mouri86770e52018-09-24 22:40:58 +0000417 status_t err = updateTexImage(recomputeVisibleRegions, latchTime, releaseFence);
Marissa Wallfd668622018-05-10 10:21:13 -0700418 if (err != NO_ERROR) {
419 return dirtyRegion;
420 }
421
422 err = updateActiveBuffer();
423 if (err != NO_ERROR) {
424 return dirtyRegion;
425 }
426
427 mBufferLatched = true;
428
429 err = updateFrameNumber(latchTime);
430 if (err != NO_ERROR) {
431 return dirtyRegion;
432 }
433
434 mRefreshPending = true;
435 mFrameLatencyNeeded = true;
436 if (oldBuffer == nullptr) {
437 // the first time we receive a buffer, we need to trigger a
438 // geometry invalidation.
439 recomputeVisibleRegions = true;
440 }
441
442 ui::Dataspace dataSpace = getDrawingDataSpace();
Peiyong Lin14724e62018-12-05 07:27:30 -0800443 // translate legacy dataspaces to modern dataspaces
Marissa Wallfd668622018-05-10 10:21:13 -0700444 switch (dataSpace) {
Peiyong Lin14724e62018-12-05 07:27:30 -0800445 case ui::Dataspace::SRGB:
446 dataSpace = ui::Dataspace::V0_SRGB;
Marissa Wallfd668622018-05-10 10:21:13 -0700447 break;
Peiyong Lin14724e62018-12-05 07:27:30 -0800448 case ui::Dataspace::SRGB_LINEAR:
449 dataSpace = ui::Dataspace::V0_SRGB_LINEAR;
Marissa Wallfd668622018-05-10 10:21:13 -0700450 break;
Peiyong Lin14724e62018-12-05 07:27:30 -0800451 case ui::Dataspace::JFIF:
452 dataSpace = ui::Dataspace::V0_JFIF;
Marissa Wallfd668622018-05-10 10:21:13 -0700453 break;
Peiyong Lin14724e62018-12-05 07:27:30 -0800454 case ui::Dataspace::BT601_625:
455 dataSpace = ui::Dataspace::V0_BT601_625;
Marissa Wallfd668622018-05-10 10:21:13 -0700456 break;
Peiyong Lin14724e62018-12-05 07:27:30 -0800457 case ui::Dataspace::BT601_525:
458 dataSpace = ui::Dataspace::V0_BT601_525;
Marissa Wallfd668622018-05-10 10:21:13 -0700459 break;
Peiyong Lin14724e62018-12-05 07:27:30 -0800460 case ui::Dataspace::BT709:
461 dataSpace = ui::Dataspace::V0_BT709;
Marissa Wallfd668622018-05-10 10:21:13 -0700462 break;
463 default:
464 break;
465 }
466 mCurrentDataSpace = dataSpace;
467
468 Rect crop(getDrawingCrop());
469 const uint32_t transform(getDrawingTransform());
470 const uint32_t scalingMode(getDrawingScalingMode());
471 if ((crop != mCurrentCrop) || (transform != mCurrentTransform) ||
472 (scalingMode != mCurrentScalingMode)) {
473 mCurrentCrop = crop;
474 mCurrentTransform = transform;
475 mCurrentScalingMode = scalingMode;
476 recomputeVisibleRegions = true;
477 }
478
479 if (oldBuffer != nullptr) {
480 uint32_t bufWidth = mActiveBuffer->getWidth();
481 uint32_t bufHeight = mActiveBuffer->getHeight();
482 if (bufWidth != uint32_t(oldBuffer->width) || bufHeight != uint32_t(oldBuffer->height)) {
483 recomputeVisibleRegions = true;
484 }
485 }
486
487 if (oldOpacity != isOpaque(s)) {
488 recomputeVisibleRegions = true;
489 }
490
491 // Remove any sync points corresponding to the buffer which was just
492 // latched
493 {
494 Mutex::Autolock lock(mLocalSyncPointMutex);
495 auto point = mLocalSyncPoints.begin();
496 while (point != mLocalSyncPoints.end()) {
497 if (!(*point)->frameIsAvailable() || !(*point)->transactionIsApplied()) {
498 // This sync point must have been added since we started
499 // latching. Don't drop it yet.
500 ++point;
501 continue;
502 }
503
504 if ((*point)->getFrameNumber() <= mCurrentFrameNumber) {
505 point = mLocalSyncPoints.erase(point);
506 } else {
507 ++point;
508 }
509 }
510 }
511
512 // FIXME: postedRegion should be dirty & bounds
513 // transform the dirty region to window-manager space
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800514 return getTransform().transform(Region(getBufferSize(s)));
Marissa Wallfd668622018-05-10 10:21:13 -0700515}
516
517// transaction
518void BufferLayer::notifyAvailableFrames() {
519 auto headFrameNumber = getHeadFrameNumber();
520 bool headFenceSignaled = fenceHasSignaled();
521 Mutex::Autolock lock(mLocalSyncPointMutex);
522 for (auto& point : mLocalSyncPoints) {
523 if (headFrameNumber >= point->getFrameNumber() && headFenceSignaled) {
524 point->setFrameAvailable();
525 }
David Sodman0c69cad2017-08-21 12:12:51 -0700526 }
527}
528
Marissa Wallfd668622018-05-10 10:21:13 -0700529bool BufferLayer::hasReadyFrame() const {
Marissa Wall024a1912018-08-13 13:55:35 -0700530 return hasFrameUpdate() || getSidebandStreamChanged() || getAutoRefresh();
Marissa Wallfd668622018-05-10 10:21:13 -0700531}
532
533uint32_t BufferLayer::getEffectiveScalingMode() const {
534 if (mOverrideScalingMode >= 0) {
535 return mOverrideScalingMode;
536 }
537
538 return mCurrentScalingMode;
539}
540
541bool BufferLayer::isProtected() const {
542 const sp<GraphicBuffer>& buffer(mActiveBuffer);
543 return (buffer != 0) && (buffer->getUsage() & GRALLOC_USAGE_PROTECTED);
544}
545
546bool BufferLayer::latchUnsignaledBuffers() {
547 static bool propertyLoaded = false;
548 static bool latch = false;
549 static std::mutex mutex;
550 std::lock_guard<std::mutex> lock(mutex);
551 if (!propertyLoaded) {
552 char value[PROPERTY_VALUE_MAX] = {};
553 property_get("debug.sf.latch_unsignaled", value, "0");
554 latch = atoi(value);
555 propertyLoaded = true;
556 }
557 return latch;
558}
559
560// h/w composer set-up
561bool BufferLayer::allTransactionsSignaled() {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800562 auto headFrameNumber = getHeadFrameNumber();
Marissa Wallfd668622018-05-10 10:21:13 -0700563 bool matchingFramesFound = false;
564 bool allTransactionsApplied = true;
565 Mutex::Autolock lock(mLocalSyncPointMutex);
566
567 for (auto& point : mLocalSyncPoints) {
568 if (point->getFrameNumber() > headFrameNumber) {
569 break;
570 }
571 matchingFramesFound = true;
572
573 if (!point->frameIsAvailable()) {
574 // We haven't notified the remote layer that the frame for
575 // this point is available yet. Notify it now, and then
576 // abort this attempt to latch.
577 point->setFrameAvailable();
578 allTransactionsApplied = false;
579 break;
580 }
581
582 allTransactionsApplied = allTransactionsApplied && point->transactionIsApplied();
583 }
584 return !matchingFramesFound || allTransactionsApplied;
David Sodman0c69cad2017-08-21 12:12:51 -0700585}
586
587// As documented in libhardware header, formats in the range
588// 0x100 - 0x1FF are specific to the HAL implementation, and
589// are known to have no alpha channel
590// TODO: move definition for device-specific range into
591// hardware.h, instead of using hard-coded values here.
592#define HARDWARE_IS_DEVICE_FORMAT(f) ((f) >= 0x100 && (f) <= 0x1FF)
593
594bool BufferLayer::getOpacityForFormat(uint32_t format) {
595 if (HARDWARE_IS_DEVICE_FORMAT(format)) {
596 return true;
597 }
598 switch (format) {
599 case HAL_PIXEL_FORMAT_RGBA_8888:
600 case HAL_PIXEL_FORMAT_BGRA_8888:
601 case HAL_PIXEL_FORMAT_RGBA_FP16:
602 case HAL_PIXEL_FORMAT_RGBA_1010102:
603 return false;
604 }
605 // in all other case, we have no blending (also for unknown formats)
606 return true;
607}
608
Peiyong Linc2020ca2019-01-10 11:36:12 -0800609bool BufferLayer::needsFiltering() const {
610 const auto displayFrame = getBE().compositionInfo.hwc.displayFrame;
611 const auto sourceCrop = getBE().compositionInfo.hwc.sourceCrop;
612 return mNeedsFiltering || sourceCrop.getHeight() != displayFrame.getHeight() ||
613 sourceCrop.getWidth() != displayFrame.getWidth();
Chia-I Wu692e0832018-06-05 15:46:58 -0700614}
615
David Sodman0c69cad2017-08-21 12:12:51 -0700616uint64_t BufferLayer::getHeadFrameNumber() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800617 if (hasFrameUpdate()) {
Marissa Wallfd668622018-05-10 10:21:13 -0700618 return getFrameNumber();
David Sodman0c69cad2017-08-21 12:12:51 -0700619 } else {
620 return mCurrentFrameNumber;
621 }
622}
623
Vishnu Nair60356342018-11-13 13:00:45 -0800624Rect BufferLayer::getBufferSize(const State& s) const {
625 // If we have a sideband stream, or we are scaling the buffer then return the layer size since
626 // we cannot determine the buffer size.
627 if ((s.sidebandStream != nullptr) ||
628 (getEffectiveScalingMode() != NATIVE_WINDOW_SCALING_MODE_FREEZE)) {
629 return Rect(getActiveWidth(s), getActiveHeight(s));
630 }
631
632 if (mActiveBuffer == nullptr) {
633 return Rect::INVALID_RECT;
634 }
635
636 uint32_t bufWidth = mActiveBuffer->getWidth();
637 uint32_t bufHeight = mActiveBuffer->getHeight();
638
639 // Undo any transformations on the buffer and return the result.
640 if (mCurrentTransform & ui::Transform::ROT_90) {
641 std::swap(bufWidth, bufHeight);
642 }
643
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800644 if (getTransformToDisplayInverse()) {
Vishnu Nair60356342018-11-13 13:00:45 -0800645 uint32_t invTransform = DisplayDevice::getPrimaryDisplayOrientationTransform();
646 if (invTransform & ui::Transform::ROT_90) {
647 std::swap(bufWidth, bufHeight);
648 }
649 }
650
651 return Rect(bufWidth, bufHeight);
652}
653
Lloyd Piquefeb73d72018-12-04 17:23:44 -0800654std::shared_ptr<compositionengine::Layer> BufferLayer::getCompositionLayer() const {
655 return mCompositionLayer;
656}
657
David Sodman0c69cad2017-08-21 12:12:51 -0700658} // namespace android
659
660#if defined(__gl_h_)
661#error "don't include gl/gl.h in this file"
662#endif
663
664#if defined(__gl2_h_)
665#error "don't include gl2/gl2.h in this file"
666#endif