blob: cafe26b5281a5df1f48a861cb5259ae34275504e [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
Alec Mouri86770e52018-09-24 22:40:58 +0000323Region BufferLayer::latchBuffer(bool& recomputeVisibleRegions, nsecs_t latchTime,
324 const sp<Fence>& releaseFence) {
Marissa Wallfd668622018-05-10 10:21:13 -0700325 ATRACE_CALL();
David Sodman0c69cad2017-08-21 12:12:51 -0700326
Marissa Wallfd668622018-05-10 10:21:13 -0700327 std::optional<Region> sidebandStreamDirtyRegion = latchSidebandStream(recomputeVisibleRegions);
David Sodman0c69cad2017-08-21 12:12:51 -0700328
Marissa Wallfd668622018-05-10 10:21:13 -0700329 if (sidebandStreamDirtyRegion) {
330 return *sidebandStreamDirtyRegion;
David Sodman0c69cad2017-08-21 12:12:51 -0700331 }
332
Marissa Wallfd668622018-05-10 10:21:13 -0700333 Region dirtyRegion;
David Sodman0c69cad2017-08-21 12:12:51 -0700334
Marissa Wallfd668622018-05-10 10:21:13 -0700335 if (!hasReadyFrame()) {
336 return dirtyRegion;
David Sodman0c69cad2017-08-21 12:12:51 -0700337 }
David Sodman0c69cad2017-08-21 12:12:51 -0700338
Marissa Wallfd668622018-05-10 10:21:13 -0700339 // if we've already called updateTexImage() without going through
340 // a composition step, we have to skip this layer at this point
341 // because we cannot call updateTeximage() without a corresponding
342 // compositionComplete() call.
343 // we'll trigger an update in onPreComposition().
344 if (mRefreshPending) {
345 return dirtyRegion;
346 }
347
348 // If the head buffer's acquire fence hasn't signaled yet, return and
349 // try again later
350 if (!fenceHasSignaled()) {
David Sodman0c69cad2017-08-21 12:12:51 -0700351 mFlinger->signalLayerUpdate();
Marissa Wallfd668622018-05-10 10:21:13 -0700352 return dirtyRegion;
353 }
354
355 // Capture the old state of the layer for comparisons later
356 const State& s(getDrawingState());
357 const bool oldOpacity = isOpaque(s);
358 sp<GraphicBuffer> oldBuffer = mActiveBuffer;
359
360 if (!allTransactionsSignaled()) {
361 mFlinger->signalLayerUpdate();
362 return dirtyRegion;
363 }
364
Alec Mouri86770e52018-09-24 22:40:58 +0000365 status_t err = updateTexImage(recomputeVisibleRegions, latchTime, releaseFence);
Marissa Wallfd668622018-05-10 10:21:13 -0700366 if (err != NO_ERROR) {
367 return dirtyRegion;
368 }
369
370 err = updateActiveBuffer();
371 if (err != NO_ERROR) {
372 return dirtyRegion;
373 }
374
375 mBufferLatched = true;
376
377 err = updateFrameNumber(latchTime);
378 if (err != NO_ERROR) {
379 return dirtyRegion;
380 }
381
382 mRefreshPending = true;
383 mFrameLatencyNeeded = true;
384 if (oldBuffer == nullptr) {
385 // the first time we receive a buffer, we need to trigger a
386 // geometry invalidation.
387 recomputeVisibleRegions = true;
388 }
389
390 ui::Dataspace dataSpace = getDrawingDataSpace();
391 // treat modern dataspaces as legacy dataspaces whenever possible, until
392 // we can trust the buffer producers
393 switch (dataSpace) {
394 case ui::Dataspace::V0_SRGB:
395 dataSpace = ui::Dataspace::SRGB;
396 break;
397 case ui::Dataspace::V0_SRGB_LINEAR:
398 dataSpace = ui::Dataspace::SRGB_LINEAR;
399 break;
400 case ui::Dataspace::V0_JFIF:
401 dataSpace = ui::Dataspace::JFIF;
402 break;
403 case ui::Dataspace::V0_BT601_625:
404 dataSpace = ui::Dataspace::BT601_625;
405 break;
406 case ui::Dataspace::V0_BT601_525:
407 dataSpace = ui::Dataspace::BT601_525;
408 break;
409 case ui::Dataspace::V0_BT709:
410 dataSpace = ui::Dataspace::BT709;
411 break;
412 default:
413 break;
414 }
415 mCurrentDataSpace = dataSpace;
416
417 Rect crop(getDrawingCrop());
418 const uint32_t transform(getDrawingTransform());
419 const uint32_t scalingMode(getDrawingScalingMode());
420 if ((crop != mCurrentCrop) || (transform != mCurrentTransform) ||
421 (scalingMode != mCurrentScalingMode)) {
422 mCurrentCrop = crop;
423 mCurrentTransform = transform;
424 mCurrentScalingMode = scalingMode;
425 recomputeVisibleRegions = true;
426 }
427
428 if (oldBuffer != nullptr) {
429 uint32_t bufWidth = mActiveBuffer->getWidth();
430 uint32_t bufHeight = mActiveBuffer->getHeight();
431 if (bufWidth != uint32_t(oldBuffer->width) || bufHeight != uint32_t(oldBuffer->height)) {
432 recomputeVisibleRegions = true;
433 }
434 }
435
436 if (oldOpacity != isOpaque(s)) {
437 recomputeVisibleRegions = true;
438 }
439
440 // Remove any sync points corresponding to the buffer which was just
441 // latched
442 {
443 Mutex::Autolock lock(mLocalSyncPointMutex);
444 auto point = mLocalSyncPoints.begin();
445 while (point != mLocalSyncPoints.end()) {
446 if (!(*point)->frameIsAvailable() || !(*point)->transactionIsApplied()) {
447 // This sync point must have been added since we started
448 // latching. Don't drop it yet.
449 ++point;
450 continue;
451 }
452
453 if ((*point)->getFrameNumber() <= mCurrentFrameNumber) {
454 point = mLocalSyncPoints.erase(point);
455 } else {
456 ++point;
457 }
458 }
459 }
460
461 // FIXME: postedRegion should be dirty & bounds
462 // transform the dirty region to window-manager space
Marissa Wall61c58622018-07-18 10:12:20 -0700463 return getTransform().transform(Region(Rect(getActiveWidth(s), getActiveHeight(s))));
Marissa Wallfd668622018-05-10 10:21:13 -0700464}
465
466// transaction
467void BufferLayer::notifyAvailableFrames() {
468 auto headFrameNumber = getHeadFrameNumber();
469 bool headFenceSignaled = fenceHasSignaled();
470 Mutex::Autolock lock(mLocalSyncPointMutex);
471 for (auto& point : mLocalSyncPoints) {
472 if (headFrameNumber >= point->getFrameNumber() && headFenceSignaled) {
473 point->setFrameAvailable();
474 }
David Sodman0c69cad2017-08-21 12:12:51 -0700475 }
476}
477
Marissa Wallfd668622018-05-10 10:21:13 -0700478bool BufferLayer::hasReadyFrame() const {
479 return hasDrawingBuffer() || getSidebandStreamChanged() || getAutoRefresh();
480}
481
482uint32_t BufferLayer::getEffectiveScalingMode() const {
483 if (mOverrideScalingMode >= 0) {
484 return mOverrideScalingMode;
485 }
486
487 return mCurrentScalingMode;
488}
489
490bool BufferLayer::isProtected() const {
491 const sp<GraphicBuffer>& buffer(mActiveBuffer);
492 return (buffer != 0) && (buffer->getUsage() & GRALLOC_USAGE_PROTECTED);
493}
494
495bool BufferLayer::latchUnsignaledBuffers() {
496 static bool propertyLoaded = false;
497 static bool latch = false;
498 static std::mutex mutex;
499 std::lock_guard<std::mutex> lock(mutex);
500 if (!propertyLoaded) {
501 char value[PROPERTY_VALUE_MAX] = {};
502 property_get("debug.sf.latch_unsignaled", value, "0");
503 latch = atoi(value);
504 propertyLoaded = true;
505 }
506 return latch;
507}
508
509// h/w composer set-up
510bool BufferLayer::allTransactionsSignaled() {
511 auto headFrameNumber = getHeadFrameNumber();
512 bool matchingFramesFound = false;
513 bool allTransactionsApplied = true;
514 Mutex::Autolock lock(mLocalSyncPointMutex);
515
516 for (auto& point : mLocalSyncPoints) {
517 if (point->getFrameNumber() > headFrameNumber) {
518 break;
519 }
520 matchingFramesFound = true;
521
522 if (!point->frameIsAvailable()) {
523 // We haven't notified the remote layer that the frame for
524 // this point is available yet. Notify it now, and then
525 // abort this attempt to latch.
526 point->setFrameAvailable();
527 allTransactionsApplied = false;
528 break;
529 }
530
531 allTransactionsApplied = allTransactionsApplied && point->transactionIsApplied();
532 }
533 return !matchingFramesFound || allTransactionsApplied;
David Sodman0c69cad2017-08-21 12:12:51 -0700534}
535
536// As documented in libhardware header, formats in the range
537// 0x100 - 0x1FF are specific to the HAL implementation, and
538// are known to have no alpha channel
539// TODO: move definition for device-specific range into
540// hardware.h, instead of using hard-coded values here.
541#define HARDWARE_IS_DEVICE_FORMAT(f) ((f) >= 0x100 && (f) <= 0x1FF)
542
543bool BufferLayer::getOpacityForFormat(uint32_t format) {
544 if (HARDWARE_IS_DEVICE_FORMAT(format)) {
545 return true;
546 }
547 switch (format) {
548 case HAL_PIXEL_FORMAT_RGBA_8888:
549 case HAL_PIXEL_FORMAT_BGRA_8888:
550 case HAL_PIXEL_FORMAT_RGBA_FP16:
551 case HAL_PIXEL_FORMAT_RGBA_1010102:
552 return false;
553 }
554 // in all other case, we have no blending (also for unknown formats)
555 return true;
556}
557
Marissa Wallfd668622018-05-10 10:21:13 -0700558bool BufferLayer::needsFiltering(const RenderArea& renderArea) const {
559 return mNeedsFiltering || renderArea.needsFiltering();
Chia-I Wu692e0832018-06-05 15:46:58 -0700560}
561
David Sodman41fdfc92017-11-06 16:09:56 -0800562void BufferLayer::drawWithOpenGL(const RenderArea& renderArea, bool useIdentityTransform) const {
Dan Stoza84d619e2018-03-28 17:07:36 -0700563 ATRACE_CALL();
David Sodman0c69cad2017-08-21 12:12:51 -0700564 const State& s(getDrawingState());
565
David Sodman9eeae692017-11-02 10:53:32 -0700566 computeGeometry(renderArea, getBE().mMesh, useIdentityTransform);
David Sodman0c69cad2017-08-21 12:12:51 -0700567
568 /*
569 * NOTE: the way we compute the texture coordinates here produces
570 * different results than when we take the HWC path -- in the later case
571 * the "source crop" is rounded to texel boundaries.
572 * This can produce significantly different results when the texture
573 * is scaled by a large amount.
574 *
575 * The GL code below is more logical (imho), and the difference with
576 * HWC is due to a limitation of the HWC API to integers -- a question
577 * is suspend is whether we should ignore this problem or revert to
578 * GL composition when a buffer scaling is applied (maybe with some
579 * minimal value)? Or, we could make GL behave like HWC -- but this feel
580 * like more of a hack.
581 */
Dan Stoza80d61162017-12-20 15:57:52 -0800582 const Rect bounds{computeBounds()}; // Rounds from FloatRect
David Sodman0c69cad2017-08-21 12:12:51 -0700583
Peiyong Linefefaac2018-08-17 12:27:51 -0700584 ui::Transform t = getTransform();
Dan Stoza80d61162017-12-20 15:57:52 -0800585 Rect win = bounds;
David Sodman0c69cad2017-08-21 12:12:51 -0700586
Marissa Wall61c58622018-07-18 10:12:20 -0700587 float left = float(win.left) / float(getActiveWidth(s));
588 float top = float(win.top) / float(getActiveHeight(s));
589 float right = float(win.right) / float(getActiveWidth(s));
590 float bottom = float(win.bottom) / float(getActiveHeight(s));
David Sodman0c69cad2017-08-21 12:12:51 -0700591
592 // TODO: we probably want to generate the texture coords with the mesh
593 // here we assume that we only have 4 vertices
Peiyong Lin833074a2018-08-28 11:53:54 -0700594 renderengine::Mesh::VertexArray<vec2> texCoords(getBE().mMesh.getTexCoordArray<vec2>());
Chia-I Wu1be50b52018-08-29 10:44:48 -0700595 // flip texcoords vertically because BufferLayerConsumer expects them to be in GL convention
David Sodman0c69cad2017-08-21 12:12:51 -0700596 texCoords[0] = vec2(left, 1.0f - top);
597 texCoords[1] = vec2(left, 1.0f - bottom);
598 texCoords[2] = vec2(right, 1.0f - bottom);
599 texCoords[3] = vec2(right, 1.0f - top);
600
bohu21566132018-03-27 14:36:34 -0700601 auto& engine(mFlinger->getRenderEngine());
602 engine.setupLayerBlending(mPremultipliedAlpha, isOpaque(s), false /* disableTexture */,
603 getColor());
Chia-I Wu01591c92018-05-22 12:03:00 -0700604 engine.setSourceDataSpace(mCurrentDataSpace);
Chia-I Wu5c6e4632018-01-11 08:54:38 -0800605
Chia-I Wu692e0832018-06-05 15:46:58 -0700606 if (isHdrY410()) {
bohu21566132018-03-27 14:36:34 -0700607 engine.setSourceY410BT2020(true);
Chia-I Wu5c6e4632018-01-11 08:54:38 -0800608 }
bohu21566132018-03-27 14:36:34 -0700609
610 engine.drawMesh(getBE().mMesh);
611 engine.disableBlending();
612
613 engine.setSourceY410BT2020(false);
David Sodman0c69cad2017-08-21 12:12:51 -0700614}
615
David Sodman0c69cad2017-08-21 12:12:51 -0700616uint64_t BufferLayer::getHeadFrameNumber() const {
Marissa Wallfd668622018-05-10 10:21:13 -0700617 if (hasDrawingBuffer()) {
618 return getFrameNumber();
David Sodman0c69cad2017-08-21 12:12:51 -0700619 } else {
620 return mCurrentFrameNumber;
621 }
622}
623
David Sodman0c69cad2017-08-21 12:12:51 -0700624} // namespace android
625
626#if defined(__gl_h_)
627#error "don't include gl/gl.h in this file"
628#endif
629
630#if defined(__gl2_h_)
631#error "don't include gl2/gl2.h in this file"
632#endif