blob: 1a73ff0391f8e3c8ebce99458e05ef9af2aab0a3 [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();
Peiyong Lind3788632018-09-18 16:01:31 -0700261 getBE().compositionInfo.hwc.colorTransform = getColorTransform();
Lloyd Pique074e8122018-07-26 12:57:23 -0700262
Marissa Wallfd668622018-05-10 10:21:13 -0700263 setHwcLayerBuffer(display);
David Sodman0c69cad2017-08-21 12:12:51 -0700264}
265
Marissa Wallfd668622018-05-10 10:21:13 -0700266bool BufferLayer::onPreComposition(nsecs_t refreshStartTime) {
267 if (mBufferLatched) {
268 Mutex::Autolock lock(mFrameEventHistoryMutex);
269 mFrameEventHistory.addPreComposition(mCurrentFrameNumber, refreshStartTime);
David Sodman0c69cad2017-08-21 12:12:51 -0700270 }
Marissa Wallfd668622018-05-10 10:21:13 -0700271 mRefreshPending = false;
272 return hasReadyFrame();
David Sodman0c69cad2017-08-21 12:12:51 -0700273}
274
Marissa Wallfd668622018-05-10 10:21:13 -0700275bool BufferLayer::onPostComposition(const std::shared_ptr<FenceTime>& glDoneFence,
276 const std::shared_ptr<FenceTime>& presentFence,
277 const CompositorTiming& compositorTiming) {
David Sodmanfb95bcc2017-12-22 15:45:30 -0800278
Marissa Wallfd668622018-05-10 10:21:13 -0700279 // mFrameLatencyNeeded is true when a new frame was latched for the
280 // composition.
281 if (!mFrameLatencyNeeded) return false;
282
283 // Update mFrameEventHistory.
Dan Stoza436ccf32018-06-21 12:10:12 -0700284 {
Marissa Wallfd668622018-05-10 10:21:13 -0700285 Mutex::Autolock lock(mFrameEventHistoryMutex);
286 mFrameEventHistory.addPostComposition(mCurrentFrameNumber, glDoneFence, presentFence,
287 compositorTiming);
David Sodman0c69cad2017-08-21 12:12:51 -0700288 }
289
Marissa Wallfd668622018-05-10 10:21:13 -0700290 // Update mFrameTracker.
291 nsecs_t desiredPresentTime = getDesiredPresentTime();
292 mFrameTracker.setDesiredPresentTime(desiredPresentTime);
293
294 const std::string layerName(getName().c_str());
295 mTimeStats.setDesiredTime(layerName, mCurrentFrameNumber, desiredPresentTime);
296
297 std::shared_ptr<FenceTime> frameReadyFence = getCurrentFenceTime();
298 if (frameReadyFence->isValid()) {
299 mFrameTracker.setFrameReadyFence(std::move(frameReadyFence));
300 } else {
301 // There was no fence for this frame, so assume that it was ready
302 // to be presented at the desired present time.
303 mFrameTracker.setFrameReadyTime(desiredPresentTime);
Dominik Laskowski45de9bd2018-06-11 17:44:10 -0700304 }
Marissa Wallfd668622018-05-10 10:21:13 -0700305
306 if (presentFence->isValid()) {
307 mTimeStats.setPresentFence(layerName, mCurrentFrameNumber, presentFence);
308 mFrameTracker.setActualPresentFence(std::shared_ptr<FenceTime>(presentFence));
309 } else if (mFlinger->getHwComposer().isConnected(HWC_DISPLAY_PRIMARY)) {
310 // The HWC doesn't support present fences, so use the refresh
311 // timestamp instead.
312 const nsecs_t actualPresentTime =
313 mFlinger->getHwComposer().getRefreshTimestamp(HWC_DISPLAY_PRIMARY);
314 mTimeStats.setPresentTime(layerName, mCurrentFrameNumber, actualPresentTime);
315 mFrameTracker.setActualPresentTime(actualPresentTime);
316 }
317
318 mFrameTracker.advanceFrame();
319 mFrameLatencyNeeded = false;
320 return true;
David Sodman0c69cad2017-08-21 12:12:51 -0700321}
322
Marissa Wallfd668622018-05-10 10:21:13 -0700323Region BufferLayer::latchBuffer(bool& recomputeVisibleRegions, nsecs_t latchTime) {
324 ATRACE_CALL();
David Sodman0c69cad2017-08-21 12:12:51 -0700325
Marissa Wallfd668622018-05-10 10:21:13 -0700326 std::optional<Region> sidebandStreamDirtyRegion = latchSidebandStream(recomputeVisibleRegions);
David Sodman0c69cad2017-08-21 12:12:51 -0700327
Marissa Wallfd668622018-05-10 10:21:13 -0700328 if (sidebandStreamDirtyRegion) {
329 return *sidebandStreamDirtyRegion;
David Sodman0c69cad2017-08-21 12:12:51 -0700330 }
331
Marissa Wallfd668622018-05-10 10:21:13 -0700332 Region dirtyRegion;
David Sodman0c69cad2017-08-21 12:12:51 -0700333
Marissa Wallfd668622018-05-10 10:21:13 -0700334 if (!hasReadyFrame()) {
335 return dirtyRegion;
David Sodman0c69cad2017-08-21 12:12:51 -0700336 }
David Sodman0c69cad2017-08-21 12:12:51 -0700337
Marissa Wallfd668622018-05-10 10:21:13 -0700338 // if we've already called updateTexImage() without going through
339 // a composition step, we have to skip this layer at this point
340 // because we cannot call updateTeximage() without a corresponding
341 // compositionComplete() call.
342 // we'll trigger an update in onPreComposition().
343 if (mRefreshPending) {
344 return dirtyRegion;
345 }
346
347 // If the head buffer's acquire fence hasn't signaled yet, return and
348 // try again later
349 if (!fenceHasSignaled()) {
David Sodman0c69cad2017-08-21 12:12:51 -0700350 mFlinger->signalLayerUpdate();
Marissa Wallfd668622018-05-10 10:21:13 -0700351 return dirtyRegion;
352 }
353
354 // Capture the old state of the layer for comparisons later
355 const State& s(getDrawingState());
356 const bool oldOpacity = isOpaque(s);
357 sp<GraphicBuffer> oldBuffer = mActiveBuffer;
358
359 if (!allTransactionsSignaled()) {
360 mFlinger->signalLayerUpdate();
361 return dirtyRegion;
362 }
363
364 status_t err = updateTexImage(recomputeVisibleRegions, latchTime);
365 if (err != NO_ERROR) {
366 return dirtyRegion;
367 }
368
369 err = updateActiveBuffer();
370 if (err != NO_ERROR) {
371 return dirtyRegion;
372 }
373
374 mBufferLatched = true;
375
376 err = updateFrameNumber(latchTime);
377 if (err != NO_ERROR) {
378 return dirtyRegion;
379 }
380
381 mRefreshPending = true;
382 mFrameLatencyNeeded = true;
383 if (oldBuffer == nullptr) {
384 // the first time we receive a buffer, we need to trigger a
385 // geometry invalidation.
386 recomputeVisibleRegions = true;
387 }
388
389 ui::Dataspace dataSpace = getDrawingDataSpace();
390 // treat modern dataspaces as legacy dataspaces whenever possible, until
391 // we can trust the buffer producers
392 switch (dataSpace) {
393 case ui::Dataspace::V0_SRGB:
394 dataSpace = ui::Dataspace::SRGB;
395 break;
396 case ui::Dataspace::V0_SRGB_LINEAR:
397 dataSpace = ui::Dataspace::SRGB_LINEAR;
398 break;
399 case ui::Dataspace::V0_JFIF:
400 dataSpace = ui::Dataspace::JFIF;
401 break;
402 case ui::Dataspace::V0_BT601_625:
403 dataSpace = ui::Dataspace::BT601_625;
404 break;
405 case ui::Dataspace::V0_BT601_525:
406 dataSpace = ui::Dataspace::BT601_525;
407 break;
408 case ui::Dataspace::V0_BT709:
409 dataSpace = ui::Dataspace::BT709;
410 break;
411 default:
412 break;
413 }
414 mCurrentDataSpace = dataSpace;
415
416 Rect crop(getDrawingCrop());
417 const uint32_t transform(getDrawingTransform());
418 const uint32_t scalingMode(getDrawingScalingMode());
419 if ((crop != mCurrentCrop) || (transform != mCurrentTransform) ||
420 (scalingMode != mCurrentScalingMode)) {
421 mCurrentCrop = crop;
422 mCurrentTransform = transform;
423 mCurrentScalingMode = scalingMode;
424 recomputeVisibleRegions = true;
425 }
426
427 if (oldBuffer != nullptr) {
428 uint32_t bufWidth = mActiveBuffer->getWidth();
429 uint32_t bufHeight = mActiveBuffer->getHeight();
430 if (bufWidth != uint32_t(oldBuffer->width) || bufHeight != uint32_t(oldBuffer->height)) {
431 recomputeVisibleRegions = true;
432 }
433 }
434
435 if (oldOpacity != isOpaque(s)) {
436 recomputeVisibleRegions = true;
437 }
438
439 // Remove any sync points corresponding to the buffer which was just
440 // latched
441 {
442 Mutex::Autolock lock(mLocalSyncPointMutex);
443 auto point = mLocalSyncPoints.begin();
444 while (point != mLocalSyncPoints.end()) {
445 if (!(*point)->frameIsAvailable() || !(*point)->transactionIsApplied()) {
446 // This sync point must have been added since we started
447 // latching. Don't drop it yet.
448 ++point;
449 continue;
450 }
451
452 if ((*point)->getFrameNumber() <= mCurrentFrameNumber) {
453 point = mLocalSyncPoints.erase(point);
454 } else {
455 ++point;
456 }
457 }
458 }
459
460 // FIXME: postedRegion should be dirty & bounds
461 // transform the dirty region to window-manager space
Marissa Wall61c58622018-07-18 10:12:20 -0700462 return getTransform().transform(Region(Rect(getActiveWidth(s), getActiveHeight(s))));
Marissa Wallfd668622018-05-10 10:21:13 -0700463}
464
465// transaction
466void BufferLayer::notifyAvailableFrames() {
467 auto headFrameNumber = getHeadFrameNumber();
468 bool headFenceSignaled = fenceHasSignaled();
469 Mutex::Autolock lock(mLocalSyncPointMutex);
470 for (auto& point : mLocalSyncPoints) {
471 if (headFrameNumber >= point->getFrameNumber() && headFenceSignaled) {
472 point->setFrameAvailable();
473 }
David Sodman0c69cad2017-08-21 12:12:51 -0700474 }
475}
476
Marissa Wallfd668622018-05-10 10:21:13 -0700477bool BufferLayer::hasReadyFrame() const {
478 return hasDrawingBuffer() || getSidebandStreamChanged() || getAutoRefresh();
479}
480
481uint32_t BufferLayer::getEffectiveScalingMode() const {
482 if (mOverrideScalingMode >= 0) {
483 return mOverrideScalingMode;
484 }
485
486 return mCurrentScalingMode;
487}
488
489bool BufferLayer::isProtected() const {
490 const sp<GraphicBuffer>& buffer(mActiveBuffer);
491 return (buffer != 0) && (buffer->getUsage() & GRALLOC_USAGE_PROTECTED);
492}
493
494bool BufferLayer::latchUnsignaledBuffers() {
495 static bool propertyLoaded = false;
496 static bool latch = false;
497 static std::mutex mutex;
498 std::lock_guard<std::mutex> lock(mutex);
499 if (!propertyLoaded) {
500 char value[PROPERTY_VALUE_MAX] = {};
501 property_get("debug.sf.latch_unsignaled", value, "0");
502 latch = atoi(value);
503 propertyLoaded = true;
504 }
505 return latch;
506}
507
508// h/w composer set-up
509bool BufferLayer::allTransactionsSignaled() {
510 auto headFrameNumber = getHeadFrameNumber();
511 bool matchingFramesFound = false;
512 bool allTransactionsApplied = true;
513 Mutex::Autolock lock(mLocalSyncPointMutex);
514
515 for (auto& point : mLocalSyncPoints) {
516 if (point->getFrameNumber() > headFrameNumber) {
517 break;
518 }
519 matchingFramesFound = true;
520
521 if (!point->frameIsAvailable()) {
522 // We haven't notified the remote layer that the frame for
523 // this point is available yet. Notify it now, and then
524 // abort this attempt to latch.
525 point->setFrameAvailable();
526 allTransactionsApplied = false;
527 break;
528 }
529
530 allTransactionsApplied = allTransactionsApplied && point->transactionIsApplied();
531 }
532 return !matchingFramesFound || allTransactionsApplied;
David Sodman0c69cad2017-08-21 12:12:51 -0700533}
534
535// As documented in libhardware header, formats in the range
536// 0x100 - 0x1FF are specific to the HAL implementation, and
537// are known to have no alpha channel
538// TODO: move definition for device-specific range into
539// hardware.h, instead of using hard-coded values here.
540#define HARDWARE_IS_DEVICE_FORMAT(f) ((f) >= 0x100 && (f) <= 0x1FF)
541
542bool BufferLayer::getOpacityForFormat(uint32_t format) {
543 if (HARDWARE_IS_DEVICE_FORMAT(format)) {
544 return true;
545 }
546 switch (format) {
547 case HAL_PIXEL_FORMAT_RGBA_8888:
548 case HAL_PIXEL_FORMAT_BGRA_8888:
549 case HAL_PIXEL_FORMAT_RGBA_FP16:
550 case HAL_PIXEL_FORMAT_RGBA_1010102:
551 return false;
552 }
553 // in all other case, we have no blending (also for unknown formats)
554 return true;
555}
556
Marissa Wallfd668622018-05-10 10:21:13 -0700557bool BufferLayer::needsFiltering(const RenderArea& renderArea) const {
558 return mNeedsFiltering || renderArea.needsFiltering();
Chia-I Wu692e0832018-06-05 15:46:58 -0700559}
560
David Sodman41fdfc92017-11-06 16:09:56 -0800561void BufferLayer::drawWithOpenGL(const RenderArea& renderArea, bool useIdentityTransform) const {
Dan Stoza84d619e2018-03-28 17:07:36 -0700562 ATRACE_CALL();
David Sodman0c69cad2017-08-21 12:12:51 -0700563 const State& s(getDrawingState());
564
David Sodman9eeae692017-11-02 10:53:32 -0700565 computeGeometry(renderArea, getBE().mMesh, useIdentityTransform);
David Sodman0c69cad2017-08-21 12:12:51 -0700566
567 /*
568 * NOTE: the way we compute the texture coordinates here produces
569 * different results than when we take the HWC path -- in the later case
570 * the "source crop" is rounded to texel boundaries.
571 * This can produce significantly different results when the texture
572 * is scaled by a large amount.
573 *
574 * The GL code below is more logical (imho), and the difference with
575 * HWC is due to a limitation of the HWC API to integers -- a question
576 * is suspend is whether we should ignore this problem or revert to
577 * GL composition when a buffer scaling is applied (maybe with some
578 * minimal value)? Or, we could make GL behave like HWC -- but this feel
579 * like more of a hack.
580 */
Dan Stoza80d61162017-12-20 15:57:52 -0800581 const Rect bounds{computeBounds()}; // Rounds from FloatRect
David Sodman0c69cad2017-08-21 12:12:51 -0700582
Peiyong Linefefaac2018-08-17 12:27:51 -0700583 ui::Transform t = getTransform();
Dan Stoza80d61162017-12-20 15:57:52 -0800584 Rect win = bounds;
David Sodman0c69cad2017-08-21 12:12:51 -0700585
Marissa Wall61c58622018-07-18 10:12:20 -0700586 float left = float(win.left) / float(getActiveWidth(s));
587 float top = float(win.top) / float(getActiveHeight(s));
588 float right = float(win.right) / float(getActiveWidth(s));
589 float bottom = float(win.bottom) / float(getActiveHeight(s));
David Sodman0c69cad2017-08-21 12:12:51 -0700590
591 // TODO: we probably want to generate the texture coords with the mesh
592 // here we assume that we only have 4 vertices
Peiyong Lin833074a2018-08-28 11:53:54 -0700593 renderengine::Mesh::VertexArray<vec2> texCoords(getBE().mMesh.getTexCoordArray<vec2>());
Chia-I Wu1be50b52018-08-29 10:44:48 -0700594 // flip texcoords vertically because BufferLayerConsumer expects them to be in GL convention
David Sodman0c69cad2017-08-21 12:12:51 -0700595 texCoords[0] = vec2(left, 1.0f - top);
596 texCoords[1] = vec2(left, 1.0f - bottom);
597 texCoords[2] = vec2(right, 1.0f - bottom);
598 texCoords[3] = vec2(right, 1.0f - top);
599
bohu21566132018-03-27 14:36:34 -0700600 auto& engine(mFlinger->getRenderEngine());
601 engine.setupLayerBlending(mPremultipliedAlpha, isOpaque(s), false /* disableTexture */,
602 getColor());
Chia-I Wu01591c92018-05-22 12:03:00 -0700603 engine.setSourceDataSpace(mCurrentDataSpace);
Chia-I Wu5c6e4632018-01-11 08:54:38 -0800604
Chia-I Wu692e0832018-06-05 15:46:58 -0700605 if (isHdrY410()) {
bohu21566132018-03-27 14:36:34 -0700606 engine.setSourceY410BT2020(true);
Chia-I Wu5c6e4632018-01-11 08:54:38 -0800607 }
bohu21566132018-03-27 14:36:34 -0700608
609 engine.drawMesh(getBE().mMesh);
610 engine.disableBlending();
611
612 engine.setSourceY410BT2020(false);
David Sodman0c69cad2017-08-21 12:12:51 -0700613}
614
David Sodman0c69cad2017-08-21 12:12:51 -0700615uint64_t BufferLayer::getHeadFrameNumber() const {
Marissa Wallfd668622018-05-10 10:21:13 -0700616 if (hasDrawingBuffer()) {
617 return getFrameNumber();
David Sodman0c69cad2017-08-21 12:12:51 -0700618 } else {
619 return mCurrentFrameNumber;
620 }
621}
622
David Sodman0c69cad2017-08-21 12:12:51 -0700623} // namespace android
624
625#if defined(__gl_h_)
626#error "don't include gl/gl.h in this file"
627#endif
628
629#if defined(__gl2_h_)
630#error "don't include gl2/gl2.h in this file"
631#endif