blob: 642ed2fefcc8e24305130ad000314c5c4449cbea [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
22#include "BufferLayer.h"
23#include "Colorizer.h"
24#include "DisplayDevice.h"
25#include "LayerRejecter.h"
David Sodman0c69cad2017-08-21 12:12:51 -070026
Peiyong Lincbc184f2018-08-22 13:24:10 -070027#include <renderengine/RenderEngine.h>
David Sodman0c69cad2017-08-21 12:12:51 -070028
29#include <gui/BufferItem.h>
30#include <gui/BufferQueue.h>
31#include <gui/LayerDebugInfo.h>
32#include <gui/Surface.h>
33
34#include <ui/DebugUtils.h>
35
36#include <utils/Errors.h>
37#include <utils/Log.h>
38#include <utils/NativeHandle.h>
39#include <utils/StopWatch.h>
40#include <utils/Trace.h>
41
42#include <cutils/compiler.h>
43#include <cutils/native_handle.h>
44#include <cutils/properties.h>
45
46#include <math.h>
47#include <stdlib.h>
48#include <mutex>
49
50namespace android {
51
Lloyd Pique42ab75e2018-09-12 20:46:03 -070052BufferLayer::BufferLayer(const LayerCreationArgs& args)
53 : Layer(args), mTextureName(args.flinger->getNewTexture()) {
54 ALOGV("Creating Layer %s", args.name.string());
David Sodman0c69cad2017-08-21 12:12:51 -070055
Peiyong Lin833074a2018-08-28 11:53:54 -070056 mTexture.init(renderengine::Texture::TEXTURE_EXTERNAL, mTextureName);
David Sodman0c69cad2017-08-21 12:12:51 -070057
Lloyd Pique42ab75e2018-09-12 20:46:03 -070058 mPremultipliedAlpha = !(args.flags & ISurfaceComposerClient::eNonPremultiplied);
David Sodman0c69cad2017-08-21 12:12:51 -070059
Lloyd Pique42ab75e2018-09-12 20:46:03 -070060 mPotentialCursor = args.flags & ISurfaceComposerClient::eCursorWindow;
61 mProtectedByApp = args.flags & ISurfaceComposerClient::eProtectedByApp;
David Sodman0c69cad2017-08-21 12:12:51 -070062}
63
64BufferLayer::~BufferLayer() {
David Sodman0c69cad2017-08-21 12:12:51 -070065 mFlinger->deleteTextureAsync(mTextureName);
66
David Sodman6f65f3e2017-11-03 14:28:09 -070067 if (!getBE().mHwcLayers.empty()) {
David Sodman0c69cad2017-08-21 12:12:51 -070068 ALOGE("Found stale hardware composer layers when destroying "
69 "surface flinger layer %s",
70 mName.string());
71 destroyAllHwcLayers();
72 }
David Sodman0c69cad2017-08-21 12:12:51 -070073}
74
David Sodmaneb085e02017-10-05 18:49:04 -070075void BufferLayer::useSurfaceDamage() {
76 if (mFlinger->mForceFullDamage) {
77 surfaceDamageRegion = Region::INVALID_REGION;
78 } else {
Marissa Wallfd668622018-05-10 10:21:13 -070079 surfaceDamageRegion = getDrawingSurfaceDamage();
David Sodmaneb085e02017-10-05 18:49:04 -070080 }
81}
82
83void BufferLayer::useEmptyDamage() {
84 surfaceDamageRegion.clear();
85}
86
Marissa Wallfd668622018-05-10 10:21:13 -070087bool BufferLayer::isOpaque(const Layer::State& s) const {
88 // if we don't have a buffer or sidebandStream yet, we're translucent regardless of the
89 // layer's opaque flag.
90 if ((getBE().compositionInfo.hwc.sidebandStream == nullptr) && (mActiveBuffer == nullptr)) {
91 return false;
92 }
93
94 // if the layer has the opaque flag, then we're always opaque,
95 // otherwise we use the current buffer's format.
96 return ((s.flags & layer_state_t::eLayerOpaque) != 0) || getOpacityForFormat(getPixelFormat());
David Sodman0c69cad2017-08-21 12:12:51 -070097}
98
99bool BufferLayer::isVisible() const {
100 return !(isHiddenByPolicy()) && getAlpha() > 0.0f &&
David Sodman0cf8f8d2017-12-20 18:19:45 -0800101 (mActiveBuffer != nullptr || getBE().compositionInfo.hwc.sidebandStream != nullptr);
David Sodman0c69cad2017-08-21 12:12:51 -0700102}
103
104bool BufferLayer::isFixedSize() const {
105 return getEffectiveScalingMode() != NATIVE_WINDOW_SCALING_MODE_FREEZE;
106}
107
David Sodman0c69cad2017-08-21 12:12:51 -0700108static constexpr mat4 inverseOrientation(uint32_t transform) {
David Sodman41fdfc92017-11-06 16:09:56 -0800109 const mat4 flipH(-1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1);
110 const mat4 flipV(1, 0, 0, 0, 0, -1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1);
111 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 -0700112 mat4 tr;
113
114 if (transform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
115 tr = tr * rot90;
116 }
117 if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_H) {
118 tr = tr * flipH;
119 }
120 if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_V) {
121 tr = tr * flipV;
122 }
123 return inverse(tr);
124}
125
126/*
127 * onDraw will draw the current layer onto the presentable buffer
128 */
129void BufferLayer::onDraw(const RenderArea& renderArea, const Region& clip,
Marissa Wall61c58622018-07-18 10:12:20 -0700130 bool useIdentityTransform) {
David Sodman0c69cad2017-08-21 12:12:51 -0700131 ATRACE_CALL();
132
David Sodman0cf8f8d2017-12-20 18:19:45 -0800133 if (CC_UNLIKELY(mActiveBuffer == 0)) {
David Sodman0c69cad2017-08-21 12:12:51 -0700134 // the texture has not been created yet, this Layer has
135 // in fact never been drawn into. This happens frequently with
136 // SurfaceView because the WindowManager can't know when the client
137 // has drawn the first time.
138
139 // If there is nothing under us, we paint the screen in black, otherwise
140 // we just skip this update.
141
142 // figure out if there is something below us
143 Region under;
144 bool finished = false;
145 mFlinger->mDrawingState.traverseInZOrder([&](Layer* layer) {
146 if (finished || layer == static_cast<BufferLayer const*>(this)) {
147 finished = true;
148 return;
149 }
150 under.orSelf(renderArea.getTransform().transform(layer->visibleRegion));
151 });
152 // if not everything below us is covered, we plug the holes!
153 Region holes(clip.subtract(under));
154 if (!holes.isEmpty()) {
155 clearWithOpenGL(renderArea, 0, 0, 0, 1);
156 }
157 return;
158 }
159
160 // Bind the current buffer to the GL texture, and wait for it to be
161 // ready for us to draw into.
Marissa Wallfd668622018-05-10 10:21:13 -0700162 status_t err = bindTextureImage();
David Sodman0c69cad2017-08-21 12:12:51 -0700163 if (err != NO_ERROR) {
164 ALOGW("onDraw: bindTextureImage failed (err=%d)", err);
165 // Go ahead and draw the buffer anyway; no matter what we do the screen
166 // is probably going to have something visibly wrong.
167 }
168
169 bool blackOutLayer = isProtected() || (isSecure() && !renderArea.isSecure());
170
Lloyd Pique144e1162017-12-20 16:44:52 -0800171 auto& engine(mFlinger->getRenderEngine());
David Sodman0c69cad2017-08-21 12:12:51 -0700172
173 if (!blackOutLayer) {
174 // TODO: we could be more subtle with isFixedSize()
Chia-I Wu5f6664c2018-08-28 11:01:44 -0700175 const bool useFiltering = needsFiltering(renderArea) || isFixedSize();
David Sodman0c69cad2017-08-21 12:12:51 -0700176
177 // Query the texture matrix given our current filtering mode.
178 float textureMatrix[16];
Marissa Wallfd668622018-05-10 10:21:13 -0700179 setFilteringEnabled(useFiltering);
180 getDrawingTransformMatrix(textureMatrix);
David Sodman0c69cad2017-08-21 12:12:51 -0700181
182 if (getTransformToDisplayInverse()) {
183 /*
184 * the code below applies the primary display's inverse transform to
185 * the texture transform
186 */
187 uint32_t transform = DisplayDevice::getPrimaryDisplayOrientationTransform();
188 mat4 tr = inverseOrientation(transform);
189
190 /**
191 * TODO(b/36727915): This is basically a hack.
192 *
193 * Ensure that regardless of the parent transformation,
194 * this buffer is always transformed from native display
195 * orientation to display orientation. For example, in the case
196 * of a camera where the buffer remains in native orientation,
197 * we want the pixels to always be upright.
198 */
199 sp<Layer> p = mDrawingParent.promote();
200 if (p != nullptr) {
201 const auto parentTransform = p->getTransform();
202 tr = tr * inverseOrientation(parentTransform.getOrientation());
203 }
204
205 // and finally apply it to the original texture matrix
206 const mat4 texTransform(mat4(static_cast<const float*>(textureMatrix)) * tr);
207 memcpy(textureMatrix, texTransform.asArray(), sizeof(textureMatrix));
208 }
209
210 // Set things up for texturing.
David Sodman0cf8f8d2017-12-20 18:19:45 -0800211 mTexture.setDimensions(mActiveBuffer->getWidth(), mActiveBuffer->getHeight());
David Sodman0c69cad2017-08-21 12:12:51 -0700212 mTexture.setFiltering(useFiltering);
213 mTexture.setMatrix(textureMatrix);
214
215 engine.setupLayerTexturing(mTexture);
216 } else {
217 engine.setupLayerBlackedOut();
218 }
219 drawWithOpenGL(renderArea, useIdentityTransform);
220 engine.disableTexturing();
221}
222
Marissa Wallfd668622018-05-10 10:21:13 -0700223bool BufferLayer::isHdrY410() const {
224 // pixel format is HDR Y410 masquerading as RGBA_1010102
225 return (mCurrentDataSpace == ui::Dataspace::BT2020_ITU_PQ &&
226 getDrawingApi() == NATIVE_WINDOW_API_MEDIA &&
227 getBE().compositionInfo.mBuffer->getPixelFormat() == HAL_PIXEL_FORMAT_RGBA_1010102);
David Sodmaneb085e02017-10-05 18:49:04 -0700228}
229
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700230void BufferLayer::setPerFrameData(const sp<const DisplayDevice>& display) {
David Sodman0c69cad2017-08-21 12:12:51 -0700231 // Apply this display's projection's viewport to the visible region
232 // before giving it to the HWC HAL.
Peiyong Linefefaac2018-08-17 12:27:51 -0700233 const ui::Transform& tr = display->getTransform();
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700234 const auto& viewport = display->getViewport();
David Sodman0c69cad2017-08-21 12:12:51 -0700235 Region visible = tr.transform(visibleRegion.intersect(viewport));
Dominik Laskowski7e045462018-05-30 13:02:02 -0700236 const auto displayId = display->getId();
David Sodmanba340492018-08-05 21:51:33 -0700237 getBE().compositionInfo.hwc.visibleRegion = visible;
David Sodmanba340492018-08-05 21:51:33 -0700238 getBE().compositionInfo.hwc.surfaceDamage = surfaceDamageRegion;
David Sodman0c69cad2017-08-21 12:12:51 -0700239
240 // Sideband layers
David Sodman0cc69182017-11-17 12:12:07 -0800241 if (getBE().compositionInfo.hwc.sidebandStream.get()) {
Dominik Laskowski7e045462018-05-30 13:02:02 -0700242 setCompositionType(displayId, HWC2::Composition::Sideband);
David Sodmanba340492018-08-05 21:51:33 -0700243 getBE().compositionInfo.compositionType = HWC2::Composition::Sideband;
David Sodman0c69cad2017-08-21 12:12:51 -0700244 return;
245 }
246
David Sodman10a41ff2018-08-05 12:14:17 -0700247 if (getBE().compositionInfo.hwc.skipGeometry) {
248 // Device or Cursor layers
249 if (mPotentialCursor) {
250 ALOGV("[%s] Requesting Cursor composition", mName.string());
251 setCompositionType(displayId, HWC2::Composition::Cursor);
252 } else {
253 ALOGV("[%s] Requesting Device composition", mName.string());
254 setCompositionType(displayId, HWC2::Composition::Device);
255 }
David Sodman0c69cad2017-08-21 12:12:51 -0700256 }
257
David Sodmanba340492018-08-05 21:51:33 -0700258 getBE().compositionInfo.hwc.dataspace = mCurrentDataSpace;
259 getBE().compositionInfo.hwc.hdrMetadata = getDrawingHdrMetadata();
260 getBE().compositionInfo.hwc.supportedPerFrameMetadata = display->getSupportedPerFrameMetadata();
Lloyd Pique074e8122018-07-26 12:57:23 -0700261
Marissa Wallfd668622018-05-10 10:21:13 -0700262 setHwcLayerBuffer(display);
David Sodman0c69cad2017-08-21 12:12:51 -0700263}
264
Marissa Wallfd668622018-05-10 10:21:13 -0700265bool BufferLayer::onPreComposition(nsecs_t refreshStartTime) {
266 if (mBufferLatched) {
267 Mutex::Autolock lock(mFrameEventHistoryMutex);
268 mFrameEventHistory.addPreComposition(mCurrentFrameNumber, refreshStartTime);
David Sodman0c69cad2017-08-21 12:12:51 -0700269 }
Marissa Wallfd668622018-05-10 10:21:13 -0700270 mRefreshPending = false;
271 return hasReadyFrame();
David Sodman0c69cad2017-08-21 12:12:51 -0700272}
273
Marissa Wallfd668622018-05-10 10:21:13 -0700274bool BufferLayer::onPostComposition(const std::shared_ptr<FenceTime>& glDoneFence,
275 const std::shared_ptr<FenceTime>& presentFence,
276 const CompositorTiming& compositorTiming) {
David Sodmanfb95bcc2017-12-22 15:45:30 -0800277
Marissa Wallfd668622018-05-10 10:21:13 -0700278 // mFrameLatencyNeeded is true when a new frame was latched for the
279 // composition.
280 if (!mFrameLatencyNeeded) return false;
281
282 // Update mFrameEventHistory.
Dan Stoza436ccf32018-06-21 12:10:12 -0700283 {
Marissa Wallfd668622018-05-10 10:21:13 -0700284 Mutex::Autolock lock(mFrameEventHistoryMutex);
285 mFrameEventHistory.addPostComposition(mCurrentFrameNumber, glDoneFence, presentFence,
286 compositorTiming);
David Sodman0c69cad2017-08-21 12:12:51 -0700287 }
288
Marissa Wallfd668622018-05-10 10:21:13 -0700289 // Update mFrameTracker.
290 nsecs_t desiredPresentTime = getDesiredPresentTime();
291 mFrameTracker.setDesiredPresentTime(desiredPresentTime);
292
293 const std::string layerName(getName().c_str());
294 mTimeStats.setDesiredTime(layerName, mCurrentFrameNumber, desiredPresentTime);
295
296 std::shared_ptr<FenceTime> frameReadyFence = getCurrentFenceTime();
297 if (frameReadyFence->isValid()) {
298 mFrameTracker.setFrameReadyFence(std::move(frameReadyFence));
299 } else {
300 // There was no fence for this frame, so assume that it was ready
301 // to be presented at the desired present time.
302 mFrameTracker.setFrameReadyTime(desiredPresentTime);
Dominik Laskowski45de9bd2018-06-11 17:44:10 -0700303 }
Marissa Wallfd668622018-05-10 10:21:13 -0700304
305 if (presentFence->isValid()) {
306 mTimeStats.setPresentFence(layerName, mCurrentFrameNumber, presentFence);
307 mFrameTracker.setActualPresentFence(std::shared_ptr<FenceTime>(presentFence));
308 } else if (mFlinger->getHwComposer().isConnected(HWC_DISPLAY_PRIMARY)) {
309 // The HWC doesn't support present fences, so use the refresh
310 // timestamp instead.
311 const nsecs_t actualPresentTime =
312 mFlinger->getHwComposer().getRefreshTimestamp(HWC_DISPLAY_PRIMARY);
313 mTimeStats.setPresentTime(layerName, mCurrentFrameNumber, actualPresentTime);
314 mFrameTracker.setActualPresentTime(actualPresentTime);
315 }
316
317 mFrameTracker.advanceFrame();
318 mFrameLatencyNeeded = false;
319 return true;
David Sodman0c69cad2017-08-21 12:12:51 -0700320}
321
Marissa Wallfd668622018-05-10 10:21:13 -0700322Region BufferLayer::latchBuffer(bool& recomputeVisibleRegions, nsecs_t latchTime) {
323 ATRACE_CALL();
David Sodman0c69cad2017-08-21 12:12:51 -0700324
Marissa Wallfd668622018-05-10 10:21:13 -0700325 std::optional<Region> sidebandStreamDirtyRegion = latchSidebandStream(recomputeVisibleRegions);
David Sodman0c69cad2017-08-21 12:12:51 -0700326
Marissa Wallfd668622018-05-10 10:21:13 -0700327 if (sidebandStreamDirtyRegion) {
328 return *sidebandStreamDirtyRegion;
David Sodman0c69cad2017-08-21 12:12:51 -0700329 }
330
Marissa Wallfd668622018-05-10 10:21:13 -0700331 Region dirtyRegion;
David Sodman0c69cad2017-08-21 12:12:51 -0700332
Marissa Wallfd668622018-05-10 10:21:13 -0700333 if (!hasReadyFrame()) {
334 return dirtyRegion;
David Sodman0c69cad2017-08-21 12:12:51 -0700335 }
David Sodman0c69cad2017-08-21 12:12:51 -0700336
Marissa Wallfd668622018-05-10 10:21:13 -0700337 // if we've already called updateTexImage() without going through
338 // a composition step, we have to skip this layer at this point
339 // because we cannot call updateTeximage() without a corresponding
340 // compositionComplete() call.
341 // we'll trigger an update in onPreComposition().
342 if (mRefreshPending) {
343 return dirtyRegion;
344 }
345
346 // If the head buffer's acquire fence hasn't signaled yet, return and
347 // try again later
348 if (!fenceHasSignaled()) {
David Sodman0c69cad2017-08-21 12:12:51 -0700349 mFlinger->signalLayerUpdate();
Marissa Wallfd668622018-05-10 10:21:13 -0700350 return dirtyRegion;
351 }
352
353 // Capture the old state of the layer for comparisons later
354 const State& s(getDrawingState());
355 const bool oldOpacity = isOpaque(s);
356 sp<GraphicBuffer> oldBuffer = mActiveBuffer;
357
358 if (!allTransactionsSignaled()) {
359 mFlinger->signalLayerUpdate();
360 return dirtyRegion;
361 }
362
363 status_t err = updateTexImage(recomputeVisibleRegions, latchTime);
364 if (err != NO_ERROR) {
365 return dirtyRegion;
366 }
367
368 err = updateActiveBuffer();
369 if (err != NO_ERROR) {
370 return dirtyRegion;
371 }
372
373 mBufferLatched = true;
374
375 err = updateFrameNumber(latchTime);
376 if (err != NO_ERROR) {
377 return dirtyRegion;
378 }
379
380 mRefreshPending = true;
381 mFrameLatencyNeeded = true;
382 if (oldBuffer == nullptr) {
383 // the first time we receive a buffer, we need to trigger a
384 // geometry invalidation.
385 recomputeVisibleRegions = true;
386 }
387
388 ui::Dataspace dataSpace = getDrawingDataSpace();
389 // treat modern dataspaces as legacy dataspaces whenever possible, until
390 // we can trust the buffer producers
391 switch (dataSpace) {
392 case ui::Dataspace::V0_SRGB:
393 dataSpace = ui::Dataspace::SRGB;
394 break;
395 case ui::Dataspace::V0_SRGB_LINEAR:
396 dataSpace = ui::Dataspace::SRGB_LINEAR;
397 break;
398 case ui::Dataspace::V0_JFIF:
399 dataSpace = ui::Dataspace::JFIF;
400 break;
401 case ui::Dataspace::V0_BT601_625:
402 dataSpace = ui::Dataspace::BT601_625;
403 break;
404 case ui::Dataspace::V0_BT601_525:
405 dataSpace = ui::Dataspace::BT601_525;
406 break;
407 case ui::Dataspace::V0_BT709:
408 dataSpace = ui::Dataspace::BT709;
409 break;
410 default:
411 break;
412 }
413 mCurrentDataSpace = dataSpace;
414
415 Rect crop(getDrawingCrop());
416 const uint32_t transform(getDrawingTransform());
417 const uint32_t scalingMode(getDrawingScalingMode());
418 if ((crop != mCurrentCrop) || (transform != mCurrentTransform) ||
419 (scalingMode != mCurrentScalingMode)) {
420 mCurrentCrop = crop;
421 mCurrentTransform = transform;
422 mCurrentScalingMode = scalingMode;
423 recomputeVisibleRegions = true;
424 }
425
426 if (oldBuffer != nullptr) {
427 uint32_t bufWidth = mActiveBuffer->getWidth();
428 uint32_t bufHeight = mActiveBuffer->getHeight();
429 if (bufWidth != uint32_t(oldBuffer->width) || bufHeight != uint32_t(oldBuffer->height)) {
430 recomputeVisibleRegions = true;
431 }
432 }
433
434 if (oldOpacity != isOpaque(s)) {
435 recomputeVisibleRegions = true;
436 }
437
438 // Remove any sync points corresponding to the buffer which was just
439 // latched
440 {
441 Mutex::Autolock lock(mLocalSyncPointMutex);
442 auto point = mLocalSyncPoints.begin();
443 while (point != mLocalSyncPoints.end()) {
444 if (!(*point)->frameIsAvailable() || !(*point)->transactionIsApplied()) {
445 // This sync point must have been added since we started
446 // latching. Don't drop it yet.
447 ++point;
448 continue;
449 }
450
451 if ((*point)->getFrameNumber() <= mCurrentFrameNumber) {
452 point = mLocalSyncPoints.erase(point);
453 } else {
454 ++point;
455 }
456 }
457 }
458
459 // FIXME: postedRegion should be dirty & bounds
460 // transform the dirty region to window-manager space
Marissa Wall61c58622018-07-18 10:12:20 -0700461 return getTransform().transform(Region(Rect(getActiveWidth(s), getActiveHeight(s))));
Marissa Wallfd668622018-05-10 10:21:13 -0700462}
463
464// transaction
465void BufferLayer::notifyAvailableFrames() {
466 auto headFrameNumber = getHeadFrameNumber();
467 bool headFenceSignaled = fenceHasSignaled();
468 Mutex::Autolock lock(mLocalSyncPointMutex);
469 for (auto& point : mLocalSyncPoints) {
470 if (headFrameNumber >= point->getFrameNumber() && headFenceSignaled) {
471 point->setFrameAvailable();
472 }
David Sodman0c69cad2017-08-21 12:12:51 -0700473 }
474}
475
Marissa Wallfd668622018-05-10 10:21:13 -0700476bool BufferLayer::hasReadyFrame() const {
477 return hasDrawingBuffer() || getSidebandStreamChanged() || getAutoRefresh();
478}
479
480uint32_t BufferLayer::getEffectiveScalingMode() const {
481 if (mOverrideScalingMode >= 0) {
482 return mOverrideScalingMode;
483 }
484
485 return mCurrentScalingMode;
486}
487
488bool BufferLayer::isProtected() const {
489 const sp<GraphicBuffer>& buffer(mActiveBuffer);
490 return (buffer != 0) && (buffer->getUsage() & GRALLOC_USAGE_PROTECTED);
491}
492
493bool BufferLayer::latchUnsignaledBuffers() {
494 static bool propertyLoaded = false;
495 static bool latch = false;
496 static std::mutex mutex;
497 std::lock_guard<std::mutex> lock(mutex);
498 if (!propertyLoaded) {
499 char value[PROPERTY_VALUE_MAX] = {};
500 property_get("debug.sf.latch_unsignaled", value, "0");
501 latch = atoi(value);
502 propertyLoaded = true;
503 }
504 return latch;
505}
506
507// h/w composer set-up
508bool BufferLayer::allTransactionsSignaled() {
509 auto headFrameNumber = getHeadFrameNumber();
510 bool matchingFramesFound = false;
511 bool allTransactionsApplied = true;
512 Mutex::Autolock lock(mLocalSyncPointMutex);
513
514 for (auto& point : mLocalSyncPoints) {
515 if (point->getFrameNumber() > headFrameNumber) {
516 break;
517 }
518 matchingFramesFound = true;
519
520 if (!point->frameIsAvailable()) {
521 // We haven't notified the remote layer that the frame for
522 // this point is available yet. Notify it now, and then
523 // abort this attempt to latch.
524 point->setFrameAvailable();
525 allTransactionsApplied = false;
526 break;
527 }
528
529 allTransactionsApplied = allTransactionsApplied && point->transactionIsApplied();
530 }
531 return !matchingFramesFound || allTransactionsApplied;
David Sodman0c69cad2017-08-21 12:12:51 -0700532}
533
534// As documented in libhardware header, formats in the range
535// 0x100 - 0x1FF are specific to the HAL implementation, and
536// are known to have no alpha channel
537// TODO: move definition for device-specific range into
538// hardware.h, instead of using hard-coded values here.
539#define HARDWARE_IS_DEVICE_FORMAT(f) ((f) >= 0x100 && (f) <= 0x1FF)
540
541bool BufferLayer::getOpacityForFormat(uint32_t format) {
542 if (HARDWARE_IS_DEVICE_FORMAT(format)) {
543 return true;
544 }
545 switch (format) {
546 case HAL_PIXEL_FORMAT_RGBA_8888:
547 case HAL_PIXEL_FORMAT_BGRA_8888:
548 case HAL_PIXEL_FORMAT_RGBA_FP16:
549 case HAL_PIXEL_FORMAT_RGBA_1010102:
550 return false;
551 }
552 // in all other case, we have no blending (also for unknown formats)
553 return true;
554}
555
Marissa Wallfd668622018-05-10 10:21:13 -0700556bool BufferLayer::needsFiltering(const RenderArea& renderArea) const {
557 return mNeedsFiltering || renderArea.needsFiltering();
Chia-I Wu692e0832018-06-05 15:46:58 -0700558}
559
David Sodman41fdfc92017-11-06 16:09:56 -0800560void BufferLayer::drawWithOpenGL(const RenderArea& renderArea, bool useIdentityTransform) const {
Dan Stoza84d619e2018-03-28 17:07:36 -0700561 ATRACE_CALL();
David Sodman0c69cad2017-08-21 12:12:51 -0700562 const State& s(getDrawingState());
563
David Sodman9eeae692017-11-02 10:53:32 -0700564 computeGeometry(renderArea, getBE().mMesh, useIdentityTransform);
David Sodman0c69cad2017-08-21 12:12:51 -0700565
566 /*
567 * NOTE: the way we compute the texture coordinates here produces
568 * different results than when we take the HWC path -- in the later case
569 * the "source crop" is rounded to texel boundaries.
570 * This can produce significantly different results when the texture
571 * is scaled by a large amount.
572 *
573 * The GL code below is more logical (imho), and the difference with
574 * HWC is due to a limitation of the HWC API to integers -- a question
575 * is suspend is whether we should ignore this problem or revert to
576 * GL composition when a buffer scaling is applied (maybe with some
577 * minimal value)? Or, we could make GL behave like HWC -- but this feel
578 * like more of a hack.
579 */
Dan Stoza80d61162017-12-20 15:57:52 -0800580 const Rect bounds{computeBounds()}; // Rounds from FloatRect
David Sodman0c69cad2017-08-21 12:12:51 -0700581
Peiyong Linefefaac2018-08-17 12:27:51 -0700582 ui::Transform t = getTransform();
Dan Stoza80d61162017-12-20 15:57:52 -0800583 Rect win = bounds;
David Sodman0c69cad2017-08-21 12:12:51 -0700584
Marissa Wall61c58622018-07-18 10:12:20 -0700585 float left = float(win.left) / float(getActiveWidth(s));
586 float top = float(win.top) / float(getActiveHeight(s));
587 float right = float(win.right) / float(getActiveWidth(s));
588 float bottom = float(win.bottom) / float(getActiveHeight(s));
David Sodman0c69cad2017-08-21 12:12:51 -0700589
590 // TODO: we probably want to generate the texture coords with the mesh
591 // here we assume that we only have 4 vertices
Peiyong Lin833074a2018-08-28 11:53:54 -0700592 renderengine::Mesh::VertexArray<vec2> texCoords(getBE().mMesh.getTexCoordArray<vec2>());
Chia-I Wu1be50b52018-08-29 10:44:48 -0700593 // flip texcoords vertically because BufferLayerConsumer expects them to be in GL convention
David Sodman0c69cad2017-08-21 12:12:51 -0700594 texCoords[0] = vec2(left, 1.0f - top);
595 texCoords[1] = vec2(left, 1.0f - bottom);
596 texCoords[2] = vec2(right, 1.0f - bottom);
597 texCoords[3] = vec2(right, 1.0f - top);
598
bohu21566132018-03-27 14:36:34 -0700599 auto& engine(mFlinger->getRenderEngine());
600 engine.setupLayerBlending(mPremultipliedAlpha, isOpaque(s), false /* disableTexture */,
601 getColor());
Chia-I Wu01591c92018-05-22 12:03:00 -0700602 engine.setSourceDataSpace(mCurrentDataSpace);
Chia-I Wu5c6e4632018-01-11 08:54:38 -0800603
Chia-I Wu692e0832018-06-05 15:46:58 -0700604 if (isHdrY410()) {
bohu21566132018-03-27 14:36:34 -0700605 engine.setSourceY410BT2020(true);
Chia-I Wu5c6e4632018-01-11 08:54:38 -0800606 }
bohu21566132018-03-27 14:36:34 -0700607
608 engine.drawMesh(getBE().mMesh);
609 engine.disableBlending();
610
611 engine.setSourceY410BT2020(false);
David Sodman0c69cad2017-08-21 12:12:51 -0700612}
613
David Sodman0c69cad2017-08-21 12:12:51 -0700614uint64_t BufferLayer::getHeadFrameNumber() const {
Marissa Wallfd668622018-05-10 10:21:13 -0700615 if (hasDrawingBuffer()) {
616 return getFrameNumber();
David Sodman0c69cad2017-08-21 12:12:51 -0700617 } else {
618 return mCurrentFrameNumber;
619 }
620}
621
David Sodman0c69cad2017-08-21 12:12:51 -0700622} // namespace android
623
624#if defined(__gl_h_)
625#error "don't include gl/gl.h in this file"
626#endif
627
628#if defined(__gl2_h_)
629#error "don't include gl2/gl2.h in this file"
630#endif