The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 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 | |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 17 | //#define LOG_NDEBUG 0 |
| 18 | #undef LOG_TAG |
| 19 | #define LOG_TAG "Layer" |
Jamie Gennis | 1c8e95c | 2012-02-23 19:27:23 -0800 | [diff] [blame] | 20 | #define ATRACE_TAG ATRACE_TAG_GRAPHICS |
| 21 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 22 | #include <stdlib.h> |
| 23 | #include <stdint.h> |
| 24 | #include <sys/types.h> |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 25 | #include <math.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 26 | |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 27 | #include <cutils/compiler.h> |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 28 | #include <cutils/native_handle.h> |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 29 | #include <cutils/properties.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 30 | |
| 31 | #include <utils/Errors.h> |
| 32 | #include <utils/Log.h> |
Jesse Hall | 399184a | 2014-03-03 15:42:54 -0800 | [diff] [blame] | 33 | #include <utils/NativeHandle.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 34 | #include <utils/StopWatch.h> |
Jamie Gennis | 1c8e95c | 2012-02-23 19:27:23 -0800 | [diff] [blame] | 35 | #include <utils/Trace.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 36 | |
Courtney Goeltzenleuchter | 36c44dc | 2017-04-14 09:33:16 -0600 | [diff] [blame] | 37 | #include <ui/DebugUtils.h> |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 38 | #include <ui/GraphicBuffer.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 39 | #include <ui/PixelFormat.h> |
Mathias Agopian | 9cce325 | 2010-02-09 17:46:37 -0800 | [diff] [blame] | 40 | |
Dan Stoza | 6b9454d | 2014-11-07 16:00:59 -0800 | [diff] [blame] | 41 | #include <gui/BufferItem.h> |
Mathias Agopian | a934764 | 2017-02-13 16:42:28 -0800 | [diff] [blame] | 42 | #include <gui/BufferQueue.h> |
Mathias Agopian | 90ac799 | 2012-02-25 18:48:35 -0800 | [diff] [blame] | 43 | #include <gui/Surface.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 44 | |
| 45 | #include "clz.h" |
Mathias Agopian | 3e25fd8 | 2013-04-22 17:52:16 +0200 | [diff] [blame] | 46 | #include "Colorizer.h" |
Mathias Agopian | 0f2f5ff | 2012-07-31 23:09:07 -0700 | [diff] [blame] | 47 | #include "DisplayDevice.h" |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 48 | #include "Layer.h" |
Fabien Sanglard | 7b1563a | 2016-10-13 12:05:28 -0700 | [diff] [blame] | 49 | #include "LayerRejecter.h" |
Dan Stoza | b9b0883 | 2014-03-13 11:55:57 -0700 | [diff] [blame] | 50 | #include "MonitoredProducer.h" |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 51 | #include "SurfaceFlinger.h" |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 52 | |
Mathias Agopian | 1b03149 | 2012-06-20 17:51:20 -0700 | [diff] [blame] | 53 | #include "DisplayHardware/HWComposer.h" |
| 54 | |
Mathias Agopian | 875d8e1 | 2013-06-07 15:35:48 -0700 | [diff] [blame] | 55 | #include "RenderEngine/RenderEngine.h" |
| 56 | |
Dan Stoza | c5da271 | 2016-07-20 15:38:12 -0700 | [diff] [blame] | 57 | #include <mutex> |
| 58 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 59 | #define DEBUG_RESIZE 0 |
| 60 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 61 | namespace android { |
| 62 | |
| 63 | // --------------------------------------------------------------------------- |
| 64 | |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 65 | int32_t Layer::sSequence = 1; |
| 66 | |
Mathias Agopian | 4d9b822 | 2013-03-12 17:11:48 -0700 | [diff] [blame] | 67 | Layer::Layer(SurfaceFlinger* flinger, const sp<Client>& client, |
| 68 | const String8& name, uint32_t w, uint32_t h, uint32_t flags) |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 69 | : contentDirty(false), |
| 70 | sequence(uint32_t(android_atomic_inc(&sSequence))), |
| 71 | mFlinger(flinger), |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 72 | mTextureName(-1U), |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 73 | mPremultipliedAlpha(true), |
| 74 | mName("unnamed"), |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 75 | mFormat(PIXEL_FORMAT_NONE), |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 76 | mTransactionFlags(0), |
Dan Stoza | 7dde599 | 2015-05-22 09:51:44 -0700 | [diff] [blame] | 77 | mPendingStateMutex(), |
| 78 | mPendingStates(), |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 79 | mQueuedFrames(0), |
Jesse Hall | 399184a | 2014-03-03 15:42:54 -0800 | [diff] [blame] | 80 | mSidebandStreamChanged(false), |
Mathias Agopian | a934764 | 2017-02-13 16:42:28 -0800 | [diff] [blame] | 81 | mActiveBufferSlot(BufferQueue::INVALID_BUFFER_SLOT), |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 82 | mCurrentTransform(0), |
Mathias Agopian | 933389f | 2011-07-18 16:15:08 -0700 | [diff] [blame] | 83 | mCurrentScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE), |
Robert Carr | c3574f7 | 2016-03-24 12:19:32 -0700 | [diff] [blame] | 84 | mOverrideScalingMode(-1), |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 85 | mCurrentOpacity(true), |
Brian Anderson | d6927fb | 2016-07-23 23:37:30 -0700 | [diff] [blame] | 86 | mBufferLatched(false), |
Dan Stoza | cac3538 | 2016-01-27 12:21:06 -0800 | [diff] [blame] | 87 | mCurrentFrameNumber(0), |
Brian Anderson | 8cc8b10 | 2016-10-21 12:43:09 -0700 | [diff] [blame] | 88 | mPreviousFrameNumber(0), |
Mathias Agopian | 4d143ee | 2012-02-23 20:05:39 -0800 | [diff] [blame] | 89 | mRefreshPending(false), |
Mathias Agopian | 82d7ab6 | 2012-01-19 18:34:40 -0800 | [diff] [blame] | 90 | mFrameLatencyNeeded(false), |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 91 | mFiltering(false), |
| 92 | mNeedsFiltering(false), |
Mathias Agopian | 5cdc899 | 2013-08-13 20:51:23 -0700 | [diff] [blame] | 93 | mMesh(Mesh::TRIANGLE_FAN, 4, 2, 2), |
Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 94 | #ifndef USE_HWC2 |
| 95 | mIsGlesComposition(false), |
| 96 | #endif |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 97 | mProtectedByApp(false), |
| 98 | mHasSurface(false), |
Riley Andrews | 03414a1 | 2014-07-01 14:22:59 -0700 | [diff] [blame] | 99 | mClientRef(client), |
Dan Stoza | a4650a5 | 2015-05-12 12:56:16 -0700 | [diff] [blame] | 100 | mPotentialCursor(false), |
| 101 | mQueueItemLock(), |
| 102 | mQueueItemCondition(), |
| 103 | mQueueItems(), |
Dan Stoza | 65476f3 | 2015-05-14 09:27:25 -0700 | [diff] [blame] | 104 | mLastFrameNumberReceived(0), |
Pablo Ceballos | 04839ab | 2015-11-13 13:39:23 -0800 | [diff] [blame] | 105 | mUpdateTexImageFailed(false), |
Robert Carr | 82364e3 | 2016-05-15 11:27:47 -0700 | [diff] [blame] | 106 | mAutoRefresh(false), |
Robert Carr | 7bf247e | 2017-05-18 14:02:49 -0700 | [diff] [blame] | 107 | mFreezeGeometryUpdates(false) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 108 | { |
Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 109 | #ifdef USE_HWC2 |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 110 | ALOGV("Creating Layer %s", name.string()); |
Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 111 | #endif |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 112 | |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 113 | mCurrentCrop.makeInvalid(); |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 114 | mFlinger->getRenderEngine().genTextures(1, &mTextureName); |
Mathias Agopian | 49457ac | 2013-08-14 18:20:17 -0700 | [diff] [blame] | 115 | mTexture.init(Texture::TEXTURE_EXTERNAL, mTextureName); |
Mathias Agopian | 4d9b822 | 2013-03-12 17:11:48 -0700 | [diff] [blame] | 116 | |
| 117 | uint32_t layerFlags = 0; |
| 118 | if (flags & ISurfaceComposerClient::eHidden) |
Andy McFadden | 4125a4f | 2014-01-29 17:17:11 -0800 | [diff] [blame] | 119 | layerFlags |= layer_state_t::eLayerHidden; |
| 120 | if (flags & ISurfaceComposerClient::eOpaque) |
| 121 | layerFlags |= layer_state_t::eLayerOpaque; |
Dan Stoza | 2311608 | 2015-06-18 14:58:39 -0700 | [diff] [blame] | 122 | if (flags & ISurfaceComposerClient::eSecure) |
| 123 | layerFlags |= layer_state_t::eLayerSecure; |
Mathias Agopian | 4d9b822 | 2013-03-12 17:11:48 -0700 | [diff] [blame] | 124 | |
| 125 | if (flags & ISurfaceComposerClient::eNonPremultiplied) |
| 126 | mPremultipliedAlpha = false; |
| 127 | |
| 128 | mName = name; |
Dan Stoza | f7ba41a | 2017-05-10 15:11:11 -0700 | [diff] [blame] | 129 | mTransactionName = String8("TX - ") + mName; |
Mathias Agopian | 4d9b822 | 2013-03-12 17:11:48 -0700 | [diff] [blame] | 130 | |
| 131 | mCurrentState.active.w = w; |
| 132 | mCurrentState.active.h = h; |
Robert Carr | 3dcabfa | 2016-03-01 18:36:58 -0800 | [diff] [blame] | 133 | mCurrentState.active.transform.set(0, 0); |
Robert Carr | b5d3d26 | 2016-03-25 15:08:13 -0700 | [diff] [blame] | 134 | mCurrentState.crop.makeInvalid(); |
| 135 | mCurrentState.finalCrop.makeInvalid(); |
Robert Carr | 7bf247e | 2017-05-18 14:02:49 -0700 | [diff] [blame] | 136 | mCurrentState.requestedFinalCrop = mCurrentState.finalCrop; |
| 137 | mCurrentState.requestedCrop = mCurrentState.crop; |
Mathias Agopian | 4d9b822 | 2013-03-12 17:11:48 -0700 | [diff] [blame] | 138 | mCurrentState.z = 0; |
Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 139 | #ifdef USE_HWC2 |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 140 | mCurrentState.alpha = 1.0f; |
Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 141 | #else |
| 142 | mCurrentState.alpha = 0xFF; |
| 143 | #endif |
Mathias Agopian | 4d9b822 | 2013-03-12 17:11:48 -0700 | [diff] [blame] | 144 | mCurrentState.layerStack = 0; |
| 145 | mCurrentState.flags = layerFlags; |
| 146 | mCurrentState.sequence = 0; |
Mathias Agopian | 4d9b822 | 2013-03-12 17:11:48 -0700 | [diff] [blame] | 147 | mCurrentState.requested = mCurrentState.active; |
Courtney Goeltzenleuchter | bb09b43 | 2016-11-30 13:51:28 -0700 | [diff] [blame] | 148 | mCurrentState.dataSpace = HAL_DATASPACE_UNKNOWN; |
Daniel Nicoara | 2f5f8a5 | 2016-12-20 16:11:58 -0500 | [diff] [blame] | 149 | mCurrentState.appId = 0; |
| 150 | mCurrentState.type = 0; |
Mathias Agopian | 4d9b822 | 2013-03-12 17:11:48 -0700 | [diff] [blame] | 151 | |
| 152 | // drawing state & current state are identical |
| 153 | mDrawingState = mCurrentState; |
Jamie Gennis | 6547ff4 | 2013-07-16 20:12:42 -0700 | [diff] [blame] | 154 | |
Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 155 | #ifdef USE_HWC2 |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 156 | const auto& hwc = flinger->getHwComposer(); |
| 157 | const auto& activeConfig = hwc.getActiveConfig(HWC_DISPLAY_PRIMARY); |
| 158 | nsecs_t displayPeriod = activeConfig->getVsyncPeriod(); |
Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 159 | #else |
| 160 | nsecs_t displayPeriod = |
| 161 | flinger->getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY); |
| 162 | #endif |
Jamie Gennis | 6547ff4 | 2013-07-16 20:12:42 -0700 | [diff] [blame] | 163 | mFrameTracker.setDisplayRefreshPeriod(displayPeriod); |
Brian Anderson | 0a61b0c | 2016-12-07 14:55:56 -0800 | [diff] [blame] | 164 | |
| 165 | CompositorTiming compositorTiming; |
| 166 | flinger->getCompositorTiming(&compositorTiming); |
| 167 | mFrameEventHistory.initializeCompositorTiming(compositorTiming); |
Jamie Gennis | e8696a4 | 2012-01-15 18:54:57 -0800 | [diff] [blame] | 168 | } |
| 169 | |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 170 | void Layer::onFirstRef() { |
Andy McFadden | bf974ab | 2012-12-04 16:51:15 -0800 | [diff] [blame] | 171 | // Creates a custom BufferQueue for SurfaceFlingerConsumer to use |
Dan Stoza | b3d0bdf | 2014-04-07 16:33:59 -0700 | [diff] [blame] | 172 | sp<IGraphicBufferProducer> producer; |
| 173 | sp<IGraphicBufferConsumer> consumer; |
Mathias Agopian | 0556d79 | 2017-03-22 15:49:32 -0700 | [diff] [blame] | 174 | BufferQueue::createBufferQueue(&producer, &consumer, true); |
Robert Carr | 1db73f6 | 2016-12-21 12:58:51 -0800 | [diff] [blame] | 175 | mProducer = new MonitoredProducer(producer, mFlinger, this); |
Irvel | 468051e | 2016-06-13 16:44:44 -0700 | [diff] [blame] | 176 | mSurfaceFlingerConsumer = new SurfaceFlingerConsumer(consumer, mTextureName, this); |
Andy McFadden | bf974ab | 2012-12-04 16:51:15 -0800 | [diff] [blame] | 177 | mSurfaceFlingerConsumer->setConsumerUsageBits(getEffectiveUsage(0)); |
Jesse Hall | 399184a | 2014-03-03 15:42:54 -0800 | [diff] [blame] | 178 | mSurfaceFlingerConsumer->setContentsChangedListener(this); |
Mathias Agopian | 4d9b822 | 2013-03-12 17:11:48 -0700 | [diff] [blame] | 179 | mSurfaceFlingerConsumer->setName(mName); |
Daniel Lam | b267579 | 2012-02-23 14:35:13 -0800 | [diff] [blame] | 180 | |
Fabien Sanglard | 63a5fcd | 2016-12-29 15:13:07 -0800 | [diff] [blame] | 181 | if (mFlinger->isLayerTripleBufferingDisabled()) { |
| 182 | mProducer->setMaxDequeuedBufferCount(2); |
| 183 | } |
Andy McFadden | 6905205 | 2012-09-14 16:10:11 -0700 | [diff] [blame] | 184 | |
Mathias Agopian | 8430095 | 2012-11-21 16:02:13 -0800 | [diff] [blame] | 185 | const sp<const DisplayDevice> hw(mFlinger->getDefaultDisplayDevice()); |
| 186 | updateTransformHint(hw); |
Mathias Agopian | b7e930d | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 187 | } |
| 188 | |
Mathias Agopian | 4d9b822 | 2013-03-12 17:11:48 -0700 | [diff] [blame] | 189 | Layer::~Layer() { |
Pablo Ceballos | 8ea4e7b | 2016-03-03 15:20:02 -0800 | [diff] [blame] | 190 | sp<Client> c(mClientRef.promote()); |
| 191 | if (c != 0) { |
| 192 | c->detachLayer(this); |
| 193 | } |
| 194 | |
Dan Stoza | cac3538 | 2016-01-27 12:21:06 -0800 | [diff] [blame] | 195 | for (auto& point : mRemoteSyncPoints) { |
| 196 | point->setTransactionApplied(); |
| 197 | } |
Dan Stoza | c814517 | 2016-04-28 16:29:06 -0700 | [diff] [blame] | 198 | for (auto& point : mLocalSyncPoints) { |
| 199 | point->setFrameAvailable(); |
| 200 | } |
Mathias Agopian | 921e6ac | 2012-07-23 23:11:29 -0700 | [diff] [blame] | 201 | mFlinger->deleteTextureAsync(mTextureName); |
Jamie Gennis | 6547ff4 | 2013-07-16 20:12:42 -0700 | [diff] [blame] | 202 | mFrameTracker.logAndResetStats(mName); |
Mathias Agopian | 96f0819 | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 203 | } |
| 204 | |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 205 | // --------------------------------------------------------------------------- |
| 206 | // callbacks |
| 207 | // --------------------------------------------------------------------------- |
| 208 | |
Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 209 | #ifdef USE_HWC2 |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 210 | void Layer::onLayerDisplayed(const sp<Fence>& releaseFence) { |
| 211 | if (mHwcLayers.empty()) { |
| 212 | return; |
| 213 | } |
| 214 | mSurfaceFlingerConsumer->setReleaseFence(releaseFence); |
| 215 | } |
Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 216 | #else |
| 217 | void Layer::onLayerDisplayed(const sp<const DisplayDevice>& /* hw */, |
| 218 | HWComposer::HWCLayerInterface* layer) { |
| 219 | if (layer) { |
| 220 | layer->onDisplayed(); |
| 221 | mSurfaceFlingerConsumer->setReleaseFence(layer->getAndResetReleaseFence()); |
| 222 | } |
| 223 | } |
| 224 | #endif |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 225 | |
Dan Stoza | 6b9454d | 2014-11-07 16:00:59 -0800 | [diff] [blame] | 226 | void Layer::onFrameAvailable(const BufferItem& item) { |
| 227 | // Add this buffer from our internal queue tracker |
| 228 | { // Autolock scope |
| 229 | Mutex::Autolock lock(mQueueItemLock); |
Irvel | 468051e | 2016-06-13 16:44:44 -0700 | [diff] [blame] | 230 | mFlinger->mInterceptor.saveBufferUpdate(this, item.mGraphicBuffer->getWidth(), |
| 231 | item.mGraphicBuffer->getHeight(), item.mFrameNumber); |
Dan Stoza | a4650a5 | 2015-05-12 12:56:16 -0700 | [diff] [blame] | 232 | // Reset the frame number tracker when we receive the first buffer after |
| 233 | // a frame number reset |
| 234 | if (item.mFrameNumber == 1) { |
| 235 | mLastFrameNumberReceived = 0; |
| 236 | } |
| 237 | |
| 238 | // Ensure that callbacks are handled in order |
| 239 | while (item.mFrameNumber != mLastFrameNumberReceived + 1) { |
| 240 | status_t result = mQueueItemCondition.waitRelative(mQueueItemLock, |
| 241 | ms2ns(500)); |
| 242 | if (result != NO_ERROR) { |
| 243 | ALOGE("[%s] Timed out waiting on callback", mName.string()); |
| 244 | } |
| 245 | } |
| 246 | |
Dan Stoza | 6b9454d | 2014-11-07 16:00:59 -0800 | [diff] [blame] | 247 | mQueueItems.push_back(item); |
Dan Stoza | ecc5040 | 2015-04-28 14:42:06 -0700 | [diff] [blame] | 248 | android_atomic_inc(&mQueuedFrames); |
Dan Stoza | a4650a5 | 2015-05-12 12:56:16 -0700 | [diff] [blame] | 249 | |
| 250 | // Wake up any pending callbacks |
| 251 | mLastFrameNumberReceived = item.mFrameNumber; |
| 252 | mQueueItemCondition.broadcast(); |
Dan Stoza | 6b9454d | 2014-11-07 16:00:59 -0800 | [diff] [blame] | 253 | } |
| 254 | |
Mathias Agopian | 99ce5cd | 2012-01-31 18:24:27 -0800 | [diff] [blame] | 255 | mFlinger->signalLayerUpdate(); |
Mathias Agopian | 579b3f8 | 2010-06-08 19:54:15 -0700 | [diff] [blame] | 256 | } |
| 257 | |
Dan Stoza | 6b9454d | 2014-11-07 16:00:59 -0800 | [diff] [blame] | 258 | void Layer::onFrameReplaced(const BufferItem& item) { |
Dan Stoza | 7dde599 | 2015-05-22 09:51:44 -0700 | [diff] [blame] | 259 | { // Autolock scope |
| 260 | Mutex::Autolock lock(mQueueItemLock); |
Dan Stoza | a4650a5 | 2015-05-12 12:56:16 -0700 | [diff] [blame] | 261 | |
Dan Stoza | 7dde599 | 2015-05-22 09:51:44 -0700 | [diff] [blame] | 262 | // Ensure that callbacks are handled in order |
| 263 | while (item.mFrameNumber != mLastFrameNumberReceived + 1) { |
| 264 | status_t result = mQueueItemCondition.waitRelative(mQueueItemLock, |
| 265 | ms2ns(500)); |
| 266 | if (result != NO_ERROR) { |
| 267 | ALOGE("[%s] Timed out waiting on callback", mName.string()); |
| 268 | } |
Dan Stoza | a4650a5 | 2015-05-12 12:56:16 -0700 | [diff] [blame] | 269 | } |
Dan Stoza | 7dde599 | 2015-05-22 09:51:44 -0700 | [diff] [blame] | 270 | |
| 271 | if (mQueueItems.empty()) { |
| 272 | ALOGE("Can't replace a frame on an empty queue"); |
| 273 | return; |
| 274 | } |
Pablo Ceballos | 4d85da4 | 2016-04-19 20:11:56 -0700 | [diff] [blame] | 275 | mQueueItems.editItemAt(mQueueItems.size() - 1) = item; |
Dan Stoza | 7dde599 | 2015-05-22 09:51:44 -0700 | [diff] [blame] | 276 | |
| 277 | // Wake up any pending callbacks |
| 278 | mLastFrameNumberReceived = item.mFrameNumber; |
| 279 | mQueueItemCondition.broadcast(); |
Dan Stoza | a4650a5 | 2015-05-12 12:56:16 -0700 | [diff] [blame] | 280 | } |
Dan Stoza | 6b9454d | 2014-11-07 16:00:59 -0800 | [diff] [blame] | 281 | } |
| 282 | |
Jesse Hall | 399184a | 2014-03-03 15:42:54 -0800 | [diff] [blame] | 283 | void Layer::onSidebandStreamChanged() { |
| 284 | if (android_atomic_release_cas(false, true, &mSidebandStreamChanged) == 0) { |
| 285 | // mSidebandStreamChanged was false |
| 286 | mFlinger->signalLayerUpdate(); |
| 287 | } |
| 288 | } |
| 289 | |
Mathias Agopian | 6710604 | 2013-03-14 19:18:13 -0700 | [diff] [blame] | 290 | // called with SurfaceFlinger::mStateLock from the drawing thread after |
| 291 | // the layer has been remove from the current state list (and just before |
| 292 | // it's removed from the drawing state list) |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 293 | void Layer::onRemoved() { |
Robert Carr | 5edb1ad | 2017-04-25 10:54:24 -0700 | [diff] [blame] | 294 | if (mCurrentState.zOrderRelativeOf != nullptr) { |
| 295 | sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote(); |
| 296 | if (strongRelative != nullptr) { |
| 297 | strongRelative->removeZOrderRelative(this); |
| 298 | } |
| 299 | mCurrentState.zOrderRelativeOf = nullptr; |
| 300 | } |
| 301 | |
Andy McFadden | bf974ab | 2012-12-04 16:51:15 -0800 | [diff] [blame] | 302 | mSurfaceFlingerConsumer->abandon(); |
Chia-I Wu | 3851225 | 2017-05-17 14:36:16 -0700 | [diff] [blame] | 303 | |
| 304 | #ifdef USE_HWC2 |
| 305 | clearHwcLayers(); |
| 306 | #endif |
| 307 | |
Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 308 | for (const auto& child : mCurrentChildren) { |
| 309 | child->onRemoved(); |
| 310 | } |
Mathias Agopian | 48d819a | 2009-09-10 19:41:18 -0700 | [diff] [blame] | 311 | } |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 312 | |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 313 | // --------------------------------------------------------------------------- |
| 314 | // set-up |
| 315 | // --------------------------------------------------------------------------- |
| 316 | |
Mathias Agopian | 1eae0ee | 2013-06-05 16:59:15 -0700 | [diff] [blame] | 317 | const String8& Layer::getName() const { |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 318 | return mName; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 319 | } |
| 320 | |
Mathias Agopian | f9d9327 | 2009-06-19 17:00:27 -0700 | [diff] [blame] | 321 | status_t Layer::setBuffers( uint32_t w, uint32_t h, |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 322 | PixelFormat format, uint32_t flags) |
| 323 | { |
Mathias Agopian | ca99fb8 | 2010-04-14 16:43:44 -0700 | [diff] [blame] | 324 | uint32_t const maxSurfaceDims = min( |
Mathias Agopian | a491260 | 2012-07-12 14:25:33 -0700 | [diff] [blame] | 325 | mFlinger->getMaxTextureSize(), mFlinger->getMaxViewportDims()); |
Mathias Agopian | ca99fb8 | 2010-04-14 16:43:44 -0700 | [diff] [blame] | 326 | |
| 327 | // never allow a surface larger than what our underlying GL implementation |
| 328 | // can handle. |
| 329 | if ((uint32_t(w)>maxSurfaceDims) || (uint32_t(h)>maxSurfaceDims)) { |
Mathias Agopian | ff615cc | 2012-02-24 14:58:36 -0800 | [diff] [blame] | 330 | ALOGE("dimensions too large %u x %u", uint32_t(w), uint32_t(h)); |
Mathias Agopian | ca99fb8 | 2010-04-14 16:43:44 -0700 | [diff] [blame] | 331 | return BAD_VALUE; |
| 332 | } |
| 333 | |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 334 | mFormat = format; |
Mathias Agopian | eff062c | 2010-08-25 14:59:15 -0700 | [diff] [blame] | 335 | |
Riley Andrews | 03414a1 | 2014-07-01 14:22:59 -0700 | [diff] [blame] | 336 | mPotentialCursor = (flags & ISurfaceComposerClient::eCursorWindow) ? true : false; |
Mathias Agopian | 3165cc2 | 2012-08-08 19:42:09 -0700 | [diff] [blame] | 337 | mProtectedByApp = (flags & ISurfaceComposerClient::eProtectedByApp) ? true : false; |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 338 | mCurrentOpacity = getOpacityForFormat(format); |
| 339 | |
Andy McFadden | bf974ab | 2012-12-04 16:51:15 -0800 | [diff] [blame] | 340 | mSurfaceFlingerConsumer->setDefaultBufferSize(w, h); |
| 341 | mSurfaceFlingerConsumer->setDefaultBufferFormat(format); |
| 342 | mSurfaceFlingerConsumer->setConsumerUsageBits(getEffectiveUsage(0)); |
Mathias Agopian | ca99fb8 | 2010-04-14 16:43:44 -0700 | [diff] [blame] | 343 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 344 | return NO_ERROR; |
| 345 | } |
| 346 | |
Mathias Agopian | 4d9b822 | 2013-03-12 17:11:48 -0700 | [diff] [blame] | 347 | sp<IBinder> Layer::getHandle() { |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 348 | Mutex::Autolock _l(mLock); |
| 349 | |
| 350 | LOG_ALWAYS_FATAL_IF(mHasSurface, |
Mathias Agopian | 4d9b822 | 2013-03-12 17:11:48 -0700 | [diff] [blame] | 351 | "Layer::getHandle() has already been called"); |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 352 | |
| 353 | mHasSurface = true; |
Mathias Agopian | 4d9b822 | 2013-03-12 17:11:48 -0700 | [diff] [blame] | 354 | |
Mathias Agopian | 4d9b822 | 2013-03-12 17:11:48 -0700 | [diff] [blame] | 355 | return new Handle(mFlinger, this); |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 356 | } |
| 357 | |
Dan Stoza | b9b0883 | 2014-03-13 11:55:57 -0700 | [diff] [blame] | 358 | sp<IGraphicBufferProducer> Layer::getProducer() const { |
| 359 | return mProducer; |
Mathias Agopian | 4d9b822 | 2013-03-12 17:11:48 -0700 | [diff] [blame] | 360 | } |
| 361 | |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 362 | // --------------------------------------------------------------------------- |
| 363 | // h/w composer set-up |
| 364 | // --------------------------------------------------------------------------- |
| 365 | |
Mathias Agopian | a8bca8d | 2013-02-27 22:03:19 -0800 | [diff] [blame] | 366 | Rect Layer::getContentCrop() const { |
| 367 | // this is the crop rectangle that applies to the buffer |
| 368 | // itself (as opposed to the window) |
Jamie Gennis | f15a83f | 2012-05-10 20:43:55 -0700 | [diff] [blame] | 369 | Rect crop; |
| 370 | if (!mCurrentCrop.isEmpty()) { |
Mathias Agopian | a8bca8d | 2013-02-27 22:03:19 -0800 | [diff] [blame] | 371 | // if the buffer crop is defined, we use that |
Jamie Gennis | f15a83f | 2012-05-10 20:43:55 -0700 | [diff] [blame] | 372 | crop = mCurrentCrop; |
Mathias Agopian | a8bca8d | 2013-02-27 22:03:19 -0800 | [diff] [blame] | 373 | } else if (mActiveBuffer != NULL) { |
| 374 | // otherwise we use the whole buffer |
| 375 | crop = mActiveBuffer->getBounds(); |
Jamie Gennis | f15a83f | 2012-05-10 20:43:55 -0700 | [diff] [blame] | 376 | } else { |
Mathias Agopian | a8bca8d | 2013-02-27 22:03:19 -0800 | [diff] [blame] | 377 | // if we don't have a buffer yet, we use an empty/invalid crop |
Mathias Agopian | 4fec873 | 2012-06-29 14:12:52 -0700 | [diff] [blame] | 378 | crop.makeInvalid(); |
Jamie Gennis | f15a83f | 2012-05-10 20:43:55 -0700 | [diff] [blame] | 379 | } |
Jamie Gennis | f15a83f | 2012-05-10 20:43:55 -0700 | [diff] [blame] | 380 | return crop; |
| 381 | } |
| 382 | |
Mathias Agopian | f3e85d4 | 2013-05-10 18:01:12 -0700 | [diff] [blame] | 383 | static Rect reduce(const Rect& win, const Region& exclude) { |
| 384 | if (CC_LIKELY(exclude.isEmpty())) { |
| 385 | return win; |
| 386 | } |
| 387 | if (exclude.isRect()) { |
| 388 | return win.reduce(exclude.getBounds()); |
| 389 | } |
| 390 | return Region(win).subtract(exclude).getBounds(); |
| 391 | } |
| 392 | |
Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 393 | Rect Layer::computeScreenBounds(bool reduceTransparentRegion) const { |
| 394 | const Layer::State& s(getDrawingState()); |
| 395 | Rect win(s.active.w, s.active.h); |
| 396 | |
| 397 | if (!s.crop.isEmpty()) { |
| 398 | win.intersect(s.crop, &win); |
| 399 | } |
| 400 | |
| 401 | Transform t = getTransform(); |
| 402 | win = t.transform(win); |
| 403 | |
Robert Carr | 41b08b5 | 2017-06-01 16:11:34 -0700 | [diff] [blame] | 404 | if (!s.finalCrop.isEmpty()) { |
| 405 | win.intersect(s.finalCrop, &win); |
| 406 | } |
| 407 | |
Chia-I Wu | e41dbe6 | 2017-06-13 14:10:56 -0700 | [diff] [blame] | 408 | const sp<Layer>& p = mDrawingParent.promote(); |
Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 409 | // Now we need to calculate the parent bounds, so we can clip ourselves to those. |
| 410 | // When calculating the parent bounds for purposes of clipping, |
| 411 | // we don't need to constrain the parent to its transparent region. |
| 412 | // The transparent region is an optimization based on the |
| 413 | // buffer contents of the layer, but does not affect the space allocated to |
| 414 | // it by policy, and thus children should be allowed to extend into the |
| 415 | // parent's transparent region. In fact one of the main uses, is to reduce |
| 416 | // buffer allocation size in cases where a child window sits behind a main window |
| 417 | // (by marking the hole in the parent window as a transparent region) |
| 418 | if (p != nullptr) { |
| 419 | Rect bounds = p->computeScreenBounds(false); |
| 420 | bounds.intersect(win, &win); |
| 421 | } |
| 422 | |
| 423 | if (reduceTransparentRegion) { |
| 424 | auto const screenTransparentRegion = t.transform(s.activeTransparentRegion); |
| 425 | win = reduce(win, screenTransparentRegion); |
| 426 | } |
| 427 | |
| 428 | return win; |
| 429 | } |
| 430 | |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 431 | Rect Layer::computeBounds() const { |
Mathias Agopian | 1eae0ee | 2013-06-05 16:59:15 -0700 | [diff] [blame] | 432 | const Layer::State& s(getDrawingState()); |
Michael Lentine | 6c925ed | 2014-09-26 17:55:01 -0700 | [diff] [blame] | 433 | return computeBounds(s.activeTransparentRegion); |
| 434 | } |
| 435 | |
| 436 | Rect Layer::computeBounds(const Region& activeTransparentRegion) const { |
| 437 | const Layer::State& s(getDrawingState()); |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 438 | Rect win(s.active.w, s.active.h); |
Robert Carr | b5d3d26 | 2016-03-25 15:08:13 -0700 | [diff] [blame] | 439 | |
| 440 | if (!s.crop.isEmpty()) { |
| 441 | win.intersect(s.crop, &win); |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 442 | } |
Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 443 | |
| 444 | Rect bounds = win; |
Chia-I Wu | e41dbe6 | 2017-06-13 14:10:56 -0700 | [diff] [blame] | 445 | const auto& p = mDrawingParent.promote(); |
Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 446 | if (p != nullptr) { |
Robert Carr | de9ec44 | 2017-02-08 17:43:36 -0800 | [diff] [blame] | 447 | // Look in computeScreenBounds recursive call for explanation of |
| 448 | // why we pass false here. |
| 449 | bounds = p->computeScreenBounds(false /* reduceTransparentRegion */); |
Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | Transform t = getTransform(); |
| 453 | if (p != nullptr) { |
| 454 | win = t.transform(win); |
| 455 | win.intersect(bounds, &win); |
| 456 | win = t.inverse().transform(win); |
| 457 | } |
| 458 | |
Mathias Agopian | 6c7f25a | 2013-05-09 20:37:10 -0700 | [diff] [blame] | 459 | // subtract the transparent region and snap to the bounds |
Michael Lentine | 6c925ed | 2014-09-26 17:55:01 -0700 | [diff] [blame] | 460 | return reduce(win, activeTransparentRegion); |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 461 | } |
| 462 | |
Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 463 | Rect Layer::computeInitialCrop(const sp<const DisplayDevice>& hw) const { |
Robert Carr | b5d3d26 | 2016-03-25 15:08:13 -0700 | [diff] [blame] | 464 | // the crop is the area of the window that gets cropped, but not |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 465 | // scaled in any ways. |
Mathias Agopian | 1eae0ee | 2013-06-05 16:59:15 -0700 | [diff] [blame] | 466 | const State& s(getDrawingState()); |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 467 | |
| 468 | // apply the projection's clipping to the window crop in |
| 469 | // layerstack space, and convert-back to layer space. |
Mathias Agopian | 6b44267 | 2013-07-09 21:24:52 -0700 | [diff] [blame] | 470 | // if there are no window scaling involved, this operation will map to full |
| 471 | // pixels in the buffer. |
| 472 | // FIXME: the 3 lines below can produce slightly incorrect clipping when we have |
| 473 | // a viewport clipping and a window transform. we should use floating point to fix this. |
Mathias Agopian | 0e8f144 | 2013-08-20 21:41:07 -0700 | [diff] [blame] | 474 | |
| 475 | Rect activeCrop(s.active.w, s.active.h); |
Robert Carr | b5d3d26 | 2016-03-25 15:08:13 -0700 | [diff] [blame] | 476 | if (!s.crop.isEmpty()) { |
Chia-I Wu | df7867f | 2017-07-20 14:24:37 -0700 | [diff] [blame] | 477 | activeCrop.intersect(s.crop, &activeCrop); |
Mathias Agopian | 0e8f144 | 2013-08-20 21:41:07 -0700 | [diff] [blame] | 478 | } |
| 479 | |
Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 480 | Transform t = getTransform(); |
| 481 | activeCrop = t.transform(activeCrop); |
Pablo Ceballos | acbe678 | 2016-03-04 17:54:21 +0000 | [diff] [blame] | 482 | if (!activeCrop.intersect(hw->getViewport(), &activeCrop)) { |
| 483 | activeCrop.clear(); |
| 484 | } |
Robert Carr | b5d3d26 | 2016-03-25 15:08:13 -0700 | [diff] [blame] | 485 | if (!s.finalCrop.isEmpty()) { |
| 486 | if(!activeCrop.intersect(s.finalCrop, &activeCrop)) { |
Pablo Ceballos | acbe678 | 2016-03-04 17:54:21 +0000 | [diff] [blame] | 487 | activeCrop.clear(); |
| 488 | } |
| 489 | } |
Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 490 | return activeCrop; |
| 491 | } |
| 492 | |
Dan Stoza | 5a423ea | 2017-02-16 14:10:39 -0800 | [diff] [blame] | 493 | FloatRect Layer::computeCrop(const sp<const DisplayDevice>& hw) const { |
Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 494 | // the content crop is the area of the content that gets scaled to the |
| 495 | // layer's size. This is in buffer space. |
Dan Stoza | 5a423ea | 2017-02-16 14:10:39 -0800 | [diff] [blame] | 496 | FloatRect crop = getContentCrop().toFloatRect(); |
Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 497 | |
| 498 | // In addition there is a WM-specified crop we pull from our drawing state. |
| 499 | const State& s(getDrawingState()); |
| 500 | |
| 501 | // Screen space to make reduction to parent crop clearer. |
| 502 | Rect activeCrop = computeInitialCrop(hw); |
Chia-I Wu | e41dbe6 | 2017-06-13 14:10:56 -0700 | [diff] [blame] | 503 | const auto& p = mDrawingParent.promote(); |
Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 504 | if (p != nullptr) { |
| 505 | auto parentCrop = p->computeInitialCrop(hw); |
| 506 | activeCrop.intersect(parentCrop, &activeCrop); |
| 507 | } |
| 508 | Transform t = getTransform(); |
| 509 | // Back to layer space to work with the content crop. |
| 510 | activeCrop = t.inverse().transform(activeCrop); |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 511 | |
Michael Lentine | 28ea217 | 2014-11-19 18:32:37 -0800 | [diff] [blame] | 512 | // This needs to be here as transform.transform(Rect) computes the |
| 513 | // transformed rect and then takes the bounding box of the result before |
| 514 | // returning. This means |
| 515 | // transform.inverse().transform(transform.transform(Rect)) != Rect |
| 516 | // in which case we need to make sure the final rect is clipped to the |
| 517 | // display bounds. |
Pablo Ceballos | acbe678 | 2016-03-04 17:54:21 +0000 | [diff] [blame] | 518 | if (!activeCrop.intersect(Rect(s.active.w, s.active.h), &activeCrop)) { |
| 519 | activeCrop.clear(); |
| 520 | } |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 521 | |
Mathias Agopian | f3e85d4 | 2013-05-10 18:01:12 -0700 | [diff] [blame] | 522 | // subtract the transparent region and snap to the bounds |
| 523 | activeCrop = reduce(activeCrop, s.activeTransparentRegion); |
| 524 | |
Pablo Ceballos | acbe678 | 2016-03-04 17:54:21 +0000 | [diff] [blame] | 525 | // Transform the window crop to match the buffer coordinate system, |
| 526 | // which means using the inverse of the current transform set on the |
| 527 | // SurfaceFlingerConsumer. |
| 528 | uint32_t invTransform = mCurrentTransform; |
Robert Carr | cae605c | 2017-03-29 12:10:31 -0700 | [diff] [blame] | 529 | if (getTransformToDisplayInverse()) { |
Pablo Ceballos | acbe678 | 2016-03-04 17:54:21 +0000 | [diff] [blame] | 530 | /* |
Pablo Ceballos | 021623b | 2016-04-15 17:31:51 -0700 | [diff] [blame] | 531 | * the code below applies the primary display's inverse transform to the |
| 532 | * buffer |
Pablo Ceballos | acbe678 | 2016-03-04 17:54:21 +0000 | [diff] [blame] | 533 | */ |
Pablo Ceballos | 021623b | 2016-04-15 17:31:51 -0700 | [diff] [blame] | 534 | uint32_t invTransformOrient = |
| 535 | DisplayDevice::getPrimaryDisplayOrientationTransform(); |
Pablo Ceballos | acbe678 | 2016-03-04 17:54:21 +0000 | [diff] [blame] | 536 | // calculate the inverse transform |
| 537 | if (invTransformOrient & NATIVE_WINDOW_TRANSFORM_ROT_90) { |
| 538 | invTransformOrient ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | |
| 539 | NATIVE_WINDOW_TRANSFORM_FLIP_H; |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 540 | } |
Pablo Ceballos | acbe678 | 2016-03-04 17:54:21 +0000 | [diff] [blame] | 541 | // and apply to the current transform |
Pablo Ceballos | 0f5131f | 2016-05-17 13:34:45 -0700 | [diff] [blame] | 542 | invTransform = (Transform(invTransformOrient) * Transform(invTransform)) |
| 543 | .getOrientation(); |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 544 | } |
Pablo Ceballos | acbe678 | 2016-03-04 17:54:21 +0000 | [diff] [blame] | 545 | |
| 546 | int winWidth = s.active.w; |
| 547 | int winHeight = s.active.h; |
| 548 | if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) { |
| 549 | // If the activeCrop has been rotate the ends are rotated but not |
| 550 | // the space itself so when transforming ends back we can't rely on |
| 551 | // a modification of the axes of rotation. To account for this we |
| 552 | // need to reorient the inverse rotation in terms of the current |
| 553 | // axes of rotation. |
| 554 | bool is_h_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) != 0; |
| 555 | bool is_v_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) != 0; |
| 556 | if (is_h_flipped == is_v_flipped) { |
| 557 | invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | |
| 558 | NATIVE_WINDOW_TRANSFORM_FLIP_H; |
| 559 | } |
| 560 | winWidth = s.active.h; |
| 561 | winHeight = s.active.w; |
| 562 | } |
| 563 | const Rect winCrop = activeCrop.transform( |
| 564 | invTransform, s.active.w, s.active.h); |
| 565 | |
| 566 | // below, crop is intersected with winCrop expressed in crop's coordinate space |
| 567 | float xScale = crop.getWidth() / float(winWidth); |
| 568 | float yScale = crop.getHeight() / float(winHeight); |
| 569 | |
| 570 | float insetL = winCrop.left * xScale; |
| 571 | float insetT = winCrop.top * yScale; |
| 572 | float insetR = (winWidth - winCrop.right ) * xScale; |
| 573 | float insetB = (winHeight - winCrop.bottom) * yScale; |
| 574 | |
| 575 | crop.left += insetL; |
| 576 | crop.top += insetT; |
| 577 | crop.right -= insetR; |
| 578 | crop.bottom -= insetB; |
| 579 | |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 580 | return crop; |
| 581 | } |
| 582 | |
Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 583 | #ifdef USE_HWC2 |
Robert Carr | ae06083 | 2016-11-28 10:51:00 -0800 | [diff] [blame] | 584 | void Layer::setGeometry(const sp<const DisplayDevice>& displayDevice, uint32_t z) |
Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 585 | #else |
| 586 | void Layer::setGeometry( |
| 587 | const sp<const DisplayDevice>& hw, |
| 588 | HWComposer::HWCLayerInterface& layer) |
| 589 | #endif |
Mathias Agopian | a350ff9 | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 590 | { |
Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 591 | #ifdef USE_HWC2 |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 592 | const auto hwcId = displayDevice->getHwcDisplayId(); |
| 593 | auto& hwcInfo = mHwcLayers[hwcId]; |
Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 594 | #else |
| 595 | layer.setDefaultState(); |
| 596 | #endif |
Mathias Agopian | a537c0f | 2011-08-02 15:51:37 -0700 | [diff] [blame] | 597 | |
Mathias Agopian | 3e8b853 | 2012-05-13 20:42:01 -0700 | [diff] [blame] | 598 | // enable this layer |
Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 599 | #ifdef USE_HWC2 |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 600 | hwcInfo.forceClientComposition = false; |
| 601 | |
| 602 | if (isSecure() && !displayDevice->isSecure()) { |
| 603 | hwcInfo.forceClientComposition = true; |
| 604 | } |
| 605 | |
| 606 | auto& hwcLayer = hwcInfo.layer; |
Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 607 | #else |
| 608 | layer.setSkip(false); |
| 609 | |
| 610 | if (isSecure() && !hw->isSecure()) { |
| 611 | layer.setSkip(true); |
| 612 | } |
| 613 | #endif |
Jamie Gennis | dd3cb84 | 2012-10-19 18:19:11 -0700 | [diff] [blame] | 614 | |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 615 | // this gives us only the "orientation" component of the transform |
Mathias Agopian | 1eae0ee | 2013-06-05 16:59:15 -0700 | [diff] [blame] | 616 | const State& s(getDrawingState()); |
Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 617 | #ifdef USE_HWC2 |
David Reveman | ecf0fa5 | 2017-03-03 11:32:44 -0500 | [diff] [blame] | 618 | auto blendMode = HWC2::BlendMode::None; |
Robert Carr | 6452f12 | 2017-03-21 10:41:29 -0700 | [diff] [blame] | 619 | if (!isOpaque(s) || getAlpha() != 1.0f) { |
David Reveman | ecf0fa5 | 2017-03-03 11:32:44 -0500 | [diff] [blame] | 620 | blendMode = mPremultipliedAlpha ? |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 621 | HWC2::BlendMode::Premultiplied : HWC2::BlendMode::Coverage; |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 622 | } |
David Reveman | ecf0fa5 | 2017-03-03 11:32:44 -0500 | [diff] [blame] | 623 | auto error = hwcLayer->setBlendMode(blendMode); |
| 624 | ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set blend mode %s:" |
| 625 | " %s (%d)", mName.string(), to_string(blendMode).c_str(), |
| 626 | to_string(error).c_str(), static_cast<int32_t>(error)); |
Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 627 | #else |
Robert Carr | 6452f12 | 2017-03-21 10:41:29 -0700 | [diff] [blame] | 628 | if (!isOpaque(s) || getAlpha() != 0xFF) { |
Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 629 | layer.setBlending(mPremultipliedAlpha ? |
| 630 | HWC_BLENDING_PREMULT : |
| 631 | HWC_BLENDING_COVERAGE); |
| 632 | } |
| 633 | #endif |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 634 | |
| 635 | // apply the layer's transform, followed by the display's global transform |
| 636 | // here we're guaranteed that the layer's transform preserves rects |
Michael Lentine | 6c925ed | 2014-09-26 17:55:01 -0700 | [diff] [blame] | 637 | Region activeTransparentRegion(s.activeTransparentRegion); |
Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 638 | Transform t = getTransform(); |
Robert Carr | b5d3d26 | 2016-03-25 15:08:13 -0700 | [diff] [blame] | 639 | if (!s.crop.isEmpty()) { |
| 640 | Rect activeCrop(s.crop); |
Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 641 | activeCrop = t.transform(activeCrop); |
Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 642 | #ifdef USE_HWC2 |
Pablo Ceballos | acbe678 | 2016-03-04 17:54:21 +0000 | [diff] [blame] | 643 | if(!activeCrop.intersect(displayDevice->getViewport(), &activeCrop)) { |
Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 644 | #else |
| 645 | if(!activeCrop.intersect(hw->getViewport(), &activeCrop)) { |
| 646 | #endif |
Pablo Ceballos | acbe678 | 2016-03-04 17:54:21 +0000 | [diff] [blame] | 647 | activeCrop.clear(); |
| 648 | } |
Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 649 | activeCrop = t.inverse().transform(activeCrop, true); |
Michael Lentine | 28ea217 | 2014-11-19 18:32:37 -0800 | [diff] [blame] | 650 | // This needs to be here as transform.transform(Rect) computes the |
| 651 | // transformed rect and then takes the bounding box of the result before |
| 652 | // returning. This means |
| 653 | // transform.inverse().transform(transform.transform(Rect)) != Rect |
| 654 | // in which case we need to make sure the final rect is clipped to the |
| 655 | // display bounds. |
Pablo Ceballos | acbe678 | 2016-03-04 17:54:21 +0000 | [diff] [blame] | 656 | if(!activeCrop.intersect(Rect(s.active.w, s.active.h), &activeCrop)) { |
| 657 | activeCrop.clear(); |
| 658 | } |
Michael Lentine | 6c925ed | 2014-09-26 17:55:01 -0700 | [diff] [blame] | 659 | // mark regions outside the crop as transparent |
| 660 | activeTransparentRegion.orSelf(Rect(0, 0, s.active.w, activeCrop.top)); |
| 661 | activeTransparentRegion.orSelf(Rect(0, activeCrop.bottom, |
| 662 | s.active.w, s.active.h)); |
| 663 | activeTransparentRegion.orSelf(Rect(0, activeCrop.top, |
| 664 | activeCrop.left, activeCrop.bottom)); |
| 665 | activeTransparentRegion.orSelf(Rect(activeCrop.right, activeCrop.top, |
| 666 | s.active.w, activeCrop.bottom)); |
| 667 | } |
Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 668 | |
| 669 | Rect frame(t.transform(computeBounds(activeTransparentRegion))); |
Robert Carr | b5d3d26 | 2016-03-25 15:08:13 -0700 | [diff] [blame] | 670 | if (!s.finalCrop.isEmpty()) { |
| 671 | if(!frame.intersect(s.finalCrop, &frame)) { |
Pablo Ceballos | acbe678 | 2016-03-04 17:54:21 +0000 | [diff] [blame] | 672 | frame.clear(); |
| 673 | } |
| 674 | } |
Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 675 | #ifdef USE_HWC2 |
Pablo Ceballos | acbe678 | 2016-03-04 17:54:21 +0000 | [diff] [blame] | 676 | if (!frame.intersect(displayDevice->getViewport(), &frame)) { |
| 677 | frame.clear(); |
| 678 | } |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 679 | const Transform& tr(displayDevice->getTransform()); |
| 680 | Rect transformedFrame = tr.transform(frame); |
David Reveman | ecf0fa5 | 2017-03-03 11:32:44 -0500 | [diff] [blame] | 681 | error = hwcLayer->setDisplayFrame(transformedFrame); |
Dan Stoza | e22aec7 | 2016-08-01 13:20:59 -0700 | [diff] [blame] | 682 | if (error != HWC2::Error::None) { |
| 683 | ALOGE("[%s] Failed to set display frame [%d, %d, %d, %d]: %s (%d)", |
| 684 | mName.string(), transformedFrame.left, transformedFrame.top, |
| 685 | transformedFrame.right, transformedFrame.bottom, |
| 686 | to_string(error).c_str(), static_cast<int32_t>(error)); |
| 687 | } else { |
| 688 | hwcInfo.displayFrame = transformedFrame; |
| 689 | } |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 690 | |
Dan Stoza | 5a423ea | 2017-02-16 14:10:39 -0800 | [diff] [blame] | 691 | FloatRect sourceCrop = computeCrop(displayDevice); |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 692 | error = hwcLayer->setSourceCrop(sourceCrop); |
Dan Stoza | e22aec7 | 2016-08-01 13:20:59 -0700 | [diff] [blame] | 693 | if (error != HWC2::Error::None) { |
| 694 | ALOGE("[%s] Failed to set source crop [%.3f, %.3f, %.3f, %.3f]: " |
| 695 | "%s (%d)", mName.string(), sourceCrop.left, sourceCrop.top, |
| 696 | sourceCrop.right, sourceCrop.bottom, to_string(error).c_str(), |
| 697 | static_cast<int32_t>(error)); |
| 698 | } else { |
| 699 | hwcInfo.sourceCrop = sourceCrop; |
| 700 | } |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 701 | |
Robert Carr | 6452f12 | 2017-03-21 10:41:29 -0700 | [diff] [blame] | 702 | float alpha = getAlpha(); |
| 703 | error = hwcLayer->setPlaneAlpha(alpha); |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 704 | ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set plane alpha %.3f: " |
Robert Carr | 6452f12 | 2017-03-21 10:41:29 -0700 | [diff] [blame] | 705 | "%s (%d)", mName.string(), alpha, to_string(error).c_str(), |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 706 | static_cast<int32_t>(error)); |
| 707 | |
Robert Carr | ae06083 | 2016-11-28 10:51:00 -0800 | [diff] [blame] | 708 | error = hwcLayer->setZOrder(z); |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 709 | ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set Z %u: %s (%d)", |
Robert Carr | ae06083 | 2016-11-28 10:51:00 -0800 | [diff] [blame] | 710 | mName.string(), z, to_string(error).c_str(), |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 711 | static_cast<int32_t>(error)); |
Daniel Nicoara | 2f5f8a5 | 2016-12-20 16:11:58 -0500 | [diff] [blame] | 712 | |
Albert Chaulk | 2a58963 | 2017-05-04 16:59:44 -0400 | [diff] [blame] | 713 | int type = s.type; |
| 714 | int appId = s.appId; |
Chia-I Wu | e41dbe6 | 2017-06-13 14:10:56 -0700 | [diff] [blame] | 715 | sp<Layer> parent = mDrawingParent.promote(); |
Albert Chaulk | 2a58963 | 2017-05-04 16:59:44 -0400 | [diff] [blame] | 716 | if (parent.get()) { |
| 717 | auto& parentState = parent->getDrawingState(); |
| 718 | type = parentState.type; |
| 719 | appId = parentState.appId; |
| 720 | } |
| 721 | |
| 722 | error = hwcLayer->setInfo(type, appId); |
Daniel Nicoara | 2f5f8a5 | 2016-12-20 16:11:58 -0500 | [diff] [blame] | 723 | ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set info (%d)", |
| 724 | mName.string(), static_cast<int32_t>(error)); |
Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 725 | #else |
| 726 | if (!frame.intersect(hw->getViewport(), &frame)) { |
| 727 | frame.clear(); |
| 728 | } |
| 729 | const Transform& tr(hw->getTransform()); |
| 730 | layer.setFrame(tr.transform(frame)); |
| 731 | layer.setCrop(computeCrop(hw)); |
Robert Carr | 6452f12 | 2017-03-21 10:41:29 -0700 | [diff] [blame] | 732 | layer.setPlaneAlpha(getAlpha()); |
Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 733 | #endif |
Mathias Agopian | 9f8386e | 2013-01-29 18:56:42 -0800 | [diff] [blame] | 734 | |
Mathias Agopian | 29a367b | 2011-07-12 14:51:45 -0700 | [diff] [blame] | 735 | /* |
| 736 | * Transformations are applied in this order: |
| 737 | * 1) buffer orientation/flip/mirror |
| 738 | * 2) state transformation (window manager) |
| 739 | * 3) layer orientation (screen orientation) |
| 740 | * (NOTE: the matrices are multiplied in reverse order) |
| 741 | */ |
| 742 | |
| 743 | const Transform bufferOrientation(mCurrentTransform); |
Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 744 | Transform transform(tr * t * bufferOrientation); |
Mathias Agopian | c1c05de | 2013-09-17 23:45:22 -0700 | [diff] [blame] | 745 | |
Robert Carr | cae605c | 2017-03-29 12:10:31 -0700 | [diff] [blame] | 746 | if (getTransformToDisplayInverse()) { |
Mathias Agopian | c1c05de | 2013-09-17 23:45:22 -0700 | [diff] [blame] | 747 | /* |
Pablo Ceballos | 021623b | 2016-04-15 17:31:51 -0700 | [diff] [blame] | 748 | * the code below applies the primary display's inverse transform to the |
| 749 | * buffer |
Mathias Agopian | c1c05de | 2013-09-17 23:45:22 -0700 | [diff] [blame] | 750 | */ |
Pablo Ceballos | 021623b | 2016-04-15 17:31:51 -0700 | [diff] [blame] | 751 | uint32_t invTransform = |
| 752 | DisplayDevice::getPrimaryDisplayOrientationTransform(); |
Mathias Agopian | c1c05de | 2013-09-17 23:45:22 -0700 | [diff] [blame] | 753 | // calculate the inverse transform |
| 754 | if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) { |
| 755 | invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | |
| 756 | NATIVE_WINDOW_TRANSFORM_FLIP_H; |
| 757 | } |
Robert Carr | cae605c | 2017-03-29 12:10:31 -0700 | [diff] [blame] | 758 | |
| 759 | /* |
| 760 | * Here we cancel out the orientation component of the WM transform. |
| 761 | * The scaling and translate components are already included in our bounds |
| 762 | * computation so it's enough to just omit it in the composition. |
| 763 | * See comment in onDraw with ref to b/36727915 for why. |
| 764 | */ |
| 765 | transform = Transform(invTransform) * tr * bufferOrientation; |
Mathias Agopian | c1c05de | 2013-09-17 23:45:22 -0700 | [diff] [blame] | 766 | } |
Mathias Agopian | 29a367b | 2011-07-12 14:51:45 -0700 | [diff] [blame] | 767 | |
| 768 | // this gives us only the "orientation" component of the transform |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 769 | const uint32_t orientation = transform.getOrientation(); |
Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 770 | #ifdef USE_HWC2 |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 771 | if (orientation & Transform::ROT_INVALID) { |
| 772 | // we can only handle simple transformation |
| 773 | hwcInfo.forceClientComposition = true; |
| 774 | } else { |
| 775 | auto transform = static_cast<HWC2::Transform>(orientation); |
| 776 | auto error = hwcLayer->setTransform(transform); |
| 777 | ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set transform %s: " |
| 778 | "%s (%d)", mName.string(), to_string(transform).c_str(), |
| 779 | to_string(error).c_str(), static_cast<int32_t>(error)); |
| 780 | } |
Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 781 | #else |
| 782 | if (orientation & Transform::ROT_INVALID) { |
| 783 | // we can only handle simple transformation |
| 784 | layer.setSkip(true); |
| 785 | } else { |
| 786 | layer.setTransform(orientation); |
| 787 | } |
| 788 | #endif |
Mathias Agopian | a350ff9 | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 789 | } |
| 790 | |
Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 791 | #ifdef USE_HWC2 |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 792 | void Layer::forceClientComposition(int32_t hwcId) { |
| 793 | if (mHwcLayers.count(hwcId) == 0) { |
| 794 | ALOGE("forceClientComposition: no HWC layer found (%d)", hwcId); |
| 795 | return; |
| 796 | } |
| 797 | |
| 798 | mHwcLayers[hwcId].forceClientComposition = true; |
| 799 | } |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 800 | |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 801 | void Layer::setPerFrameData(const sp<const DisplayDevice>& displayDevice) { |
| 802 | // Apply this display's projection's viewport to the visible region |
| 803 | // before giving it to the HWC HAL. |
| 804 | const Transform& tr = displayDevice->getTransform(); |
| 805 | const auto& viewport = displayDevice->getViewport(); |
| 806 | Region visible = tr.transform(visibleRegion.intersect(viewport)); |
| 807 | auto hwcId = displayDevice->getHwcDisplayId(); |
Chia-I Wu | aaff73f | 2017-02-13 12:28:24 -0800 | [diff] [blame] | 808 | auto& hwcInfo = mHwcLayers[hwcId]; |
| 809 | auto& hwcLayer = hwcInfo.layer; |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 810 | auto error = hwcLayer->setVisibleRegion(visible); |
| 811 | if (error != HWC2::Error::None) { |
| 812 | ALOGE("[%s] Failed to set visible region: %s (%d)", mName.string(), |
| 813 | to_string(error).c_str(), static_cast<int32_t>(error)); |
| 814 | visible.dump(LOG_TAG); |
| 815 | } |
| 816 | |
| 817 | error = hwcLayer->setSurfaceDamage(surfaceDamageRegion); |
| 818 | if (error != HWC2::Error::None) { |
| 819 | ALOGE("[%s] Failed to set surface damage: %s (%d)", mName.string(), |
| 820 | to_string(error).c_str(), static_cast<int32_t>(error)); |
| 821 | surfaceDamageRegion.dump(LOG_TAG); |
| 822 | } |
| 823 | |
Dan Stoza | 0f67b3f | 2016-05-17 10:48:38 -0700 | [diff] [blame] | 824 | // Sideband layers |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 825 | if (mSidebandStream.get()) { |
Dan Stoza | 0f67b3f | 2016-05-17 10:48:38 -0700 | [diff] [blame] | 826 | setCompositionType(hwcId, HWC2::Composition::Sideband); |
| 827 | ALOGV("[%s] Requesting Sideband composition", mName.string()); |
| 828 | error = hwcLayer->setSidebandStream(mSidebandStream->handle()); |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 829 | if (error != HWC2::Error::None) { |
| 830 | ALOGE("[%s] Failed to set sideband stream %p: %s (%d)", |
| 831 | mName.string(), mSidebandStream->handle(), |
| 832 | to_string(error).c_str(), static_cast<int32_t>(error)); |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 833 | } |
Dan Stoza | 0f67b3f | 2016-05-17 10:48:38 -0700 | [diff] [blame] | 834 | return; |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 835 | } |
| 836 | |
Dan Stoza | 0a21df7 | 2016-07-20 12:52:38 -0700 | [diff] [blame] | 837 | // Client layers |
Chia-I Wu | aaff73f | 2017-02-13 12:28:24 -0800 | [diff] [blame] | 838 | if (hwcInfo.forceClientComposition || |
Dan Stoza | 0a21df7 | 2016-07-20 12:52:38 -0700 | [diff] [blame] | 839 | (mActiveBuffer != nullptr && mActiveBuffer->handle == nullptr)) { |
Dan Stoza | 0f67b3f | 2016-05-17 10:48:38 -0700 | [diff] [blame] | 840 | ALOGV("[%s] Requesting Client composition", mName.string()); |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 841 | setCompositionType(hwcId, HWC2::Composition::Client); |
Dan Stoza | 0f67b3f | 2016-05-17 10:48:38 -0700 | [diff] [blame] | 842 | return; |
| 843 | } |
| 844 | |
Dan Stoza | 0a21df7 | 2016-07-20 12:52:38 -0700 | [diff] [blame] | 845 | // SolidColor layers |
| 846 | if (mActiveBuffer == nullptr) { |
| 847 | setCompositionType(hwcId, HWC2::Composition::SolidColor); |
Dan Stoza | c6c8954 | 2016-07-27 10:16:52 -0700 | [diff] [blame] | 848 | |
| 849 | // For now, we only support black for DimLayer |
Dan Stoza | 0a21df7 | 2016-07-20 12:52:38 -0700 | [diff] [blame] | 850 | error = hwcLayer->setColor({0, 0, 0, 255}); |
| 851 | if (error != HWC2::Error::None) { |
| 852 | ALOGE("[%s] Failed to set color: %s (%d)", mName.string(), |
| 853 | to_string(error).c_str(), static_cast<int32_t>(error)); |
| 854 | } |
Dan Stoza | c6c8954 | 2016-07-27 10:16:52 -0700 | [diff] [blame] | 855 | |
| 856 | // Clear out the transform, because it doesn't make sense absent a |
| 857 | // source buffer |
| 858 | error = hwcLayer->setTransform(HWC2::Transform::None); |
| 859 | if (error != HWC2::Error::None) { |
| 860 | ALOGE("[%s] Failed to clear transform: %s (%d)", mName.string(), |
| 861 | to_string(error).c_str(), static_cast<int32_t>(error)); |
| 862 | } |
| 863 | |
Dan Stoza | 0a21df7 | 2016-07-20 12:52:38 -0700 | [diff] [blame] | 864 | return; |
| 865 | } |
| 866 | |
Dan Stoza | 0f67b3f | 2016-05-17 10:48:38 -0700 | [diff] [blame] | 867 | // Device or Cursor layers |
| 868 | if (mPotentialCursor) { |
| 869 | ALOGV("[%s] Requesting Cursor composition", mName.string()); |
| 870 | setCompositionType(hwcId, HWC2::Composition::Cursor); |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 871 | } else { |
| 872 | ALOGV("[%s] Requesting Device composition", mName.string()); |
| 873 | setCompositionType(hwcId, HWC2::Composition::Device); |
| 874 | } |
Dan Stoza | 0f67b3f | 2016-05-17 10:48:38 -0700 | [diff] [blame] | 875 | |
Courtney Goeltzenleuchter | bb09b43 | 2016-11-30 13:51:28 -0700 | [diff] [blame] | 876 | ALOGV("setPerFrameData: dataspace = %d", mCurrentState.dataSpace); |
| 877 | error = hwcLayer->setDataspace(mCurrentState.dataSpace); |
| 878 | if (error != HWC2::Error::None) { |
| 879 | ALOGE("[%s] Failed to set dataspace %d: %s (%d)", mName.string(), |
| 880 | mCurrentState.dataSpace, to_string(error).c_str(), |
| 881 | static_cast<int32_t>(error)); |
| 882 | } |
| 883 | |
Chia-I Wu | 06d63de | 2017-01-04 14:58:51 +0800 | [diff] [blame] | 884 | uint32_t hwcSlot = 0; |
Daniel Nicoara | 1f42e3a | 2017-04-10 13:27:32 -0400 | [diff] [blame] | 885 | sp<GraphicBuffer> hwcBuffer; |
| 886 | hwcInfo.bufferCache.getHwcBuffer(mActiveBufferSlot, mActiveBuffer, |
| 887 | &hwcSlot, &hwcBuffer); |
Chia-I Wu | 06d63de | 2017-01-04 14:58:51 +0800 | [diff] [blame] | 888 | |
Dan Stoza | 0f67b3f | 2016-05-17 10:48:38 -0700 | [diff] [blame] | 889 | auto acquireFence = mSurfaceFlingerConsumer->getCurrentFence(); |
Daniel Nicoara | 1f42e3a | 2017-04-10 13:27:32 -0400 | [diff] [blame] | 890 | error = hwcLayer->setBuffer(hwcSlot, hwcBuffer, acquireFence); |
Dan Stoza | 0f67b3f | 2016-05-17 10:48:38 -0700 | [diff] [blame] | 891 | if (error != HWC2::Error::None) { |
| 892 | ALOGE("[%s] Failed to set buffer %p: %s (%d)", mName.string(), |
| 893 | mActiveBuffer->handle, to_string(error).c_str(), |
| 894 | static_cast<int32_t>(error)); |
| 895 | } |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 896 | } |
Courtney Goeltzenleuchter | 9551fd3 | 2016-10-20 17:18:15 -0600 | [diff] [blame] | 897 | |
Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 898 | #else |
| 899 | void Layer::setPerFrameData(const sp<const DisplayDevice>& hw, |
| 900 | HWComposer::HWCLayerInterface& layer) { |
| 901 | // we have to set the visible region on every frame because |
| 902 | // we currently free it during onLayerDisplayed(), which is called |
| 903 | // after HWComposer::commit() -- every frame. |
| 904 | // Apply this display's projection's viewport to the visible region |
| 905 | // before giving it to the HWC HAL. |
| 906 | const Transform& tr = hw->getTransform(); |
| 907 | Region visible = tr.transform(visibleRegion.intersect(hw->getViewport())); |
| 908 | layer.setVisibleRegionScreen(visible); |
| 909 | layer.setSurfaceDamage(surfaceDamageRegion); |
| 910 | mIsGlesComposition = (layer.getCompositionType() == HWC_FRAMEBUFFER); |
Dan Stoza | ee44edd | 2015-03-23 15:50:23 -0700 | [diff] [blame] | 911 | |
Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 912 | if (mSidebandStream.get()) { |
| 913 | layer.setSidebandStream(mSidebandStream); |
| 914 | } else { |
| 915 | // NOTE: buffer can be NULL if the client never drew into this |
| 916 | // layer yet, or if we ran out of memory |
| 917 | layer.setBuffer(mActiveBuffer); |
| 918 | } |
| 919 | } |
| 920 | #endif |
| 921 | |
| 922 | #ifdef USE_HWC2 |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 923 | void Layer::updateCursorPosition(const sp<const DisplayDevice>& displayDevice) { |
| 924 | auto hwcId = displayDevice->getHwcDisplayId(); |
| 925 | if (mHwcLayers.count(hwcId) == 0 || |
| 926 | getCompositionType(hwcId) != HWC2::Composition::Cursor) { |
| 927 | return; |
| 928 | } |
| 929 | |
| 930 | // This gives us only the "orientation" component of the transform |
| 931 | const State& s(getCurrentState()); |
| 932 | |
| 933 | // Apply the layer's transform, followed by the display's global transform |
| 934 | // Here we're guaranteed that the layer's transform preserves rects |
| 935 | Rect win(s.active.w, s.active.h); |
Robert Carr | b5d3d26 | 2016-03-25 15:08:13 -0700 | [diff] [blame] | 936 | if (!s.crop.isEmpty()) { |
| 937 | win.intersect(s.crop, &win); |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 938 | } |
| 939 | // Subtract the transparent region and snap to the bounds |
| 940 | Rect bounds = reduce(win, s.activeTransparentRegion); |
Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 941 | Rect frame(getTransform().transform(bounds)); |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 942 | frame.intersect(displayDevice->getViewport(), &frame); |
Robert Carr | b5d3d26 | 2016-03-25 15:08:13 -0700 | [diff] [blame] | 943 | if (!s.finalCrop.isEmpty()) { |
| 944 | frame.intersect(s.finalCrop, &frame); |
Pablo Ceballos | acbe678 | 2016-03-04 17:54:21 +0000 | [diff] [blame] | 945 | } |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 946 | auto& displayTransform(displayDevice->getTransform()); |
| 947 | auto position = displayTransform.transform(frame); |
| 948 | |
| 949 | auto error = mHwcLayers[hwcId].layer->setCursorPosition(position.left, |
| 950 | position.top); |
| 951 | ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set cursor position " |
| 952 | "to (%d, %d): %s (%d)", mName.string(), position.left, |
| 953 | position.top, to_string(error).c_str(), |
| 954 | static_cast<int32_t>(error)); |
| 955 | } |
Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 956 | #else |
| 957 | void Layer::setAcquireFence(const sp<const DisplayDevice>& /* hw */, |
| 958 | HWComposer::HWCLayerInterface& layer) { |
| 959 | int fenceFd = -1; |
| 960 | |
| 961 | // TODO: there is a possible optimization here: we only need to set the |
| 962 | // acquire fence the first time a new buffer is acquired on EACH display. |
| 963 | |
| 964 | if (layer.getCompositionType() == HWC_OVERLAY || layer.getCompositionType() == HWC_CURSOR_OVERLAY) { |
| 965 | sp<Fence> fence = mSurfaceFlingerConsumer->getCurrentFence(); |
| 966 | if (fence->isValid()) { |
| 967 | fenceFd = fence->dup(); |
| 968 | if (fenceFd == -1) { |
| 969 | ALOGW("failed to dup layer fence, skipping sync: %d", errno); |
| 970 | } |
| 971 | } |
| 972 | } |
| 973 | layer.setAcquireFenceFd(fenceFd); |
| 974 | } |
| 975 | |
| 976 | Rect Layer::getPosition( |
| 977 | const sp<const DisplayDevice>& hw) |
| 978 | { |
| 979 | // this gives us only the "orientation" component of the transform |
| 980 | const State& s(getCurrentState()); |
| 981 | |
| 982 | // apply the layer's transform, followed by the display's global transform |
| 983 | // here we're guaranteed that the layer's transform preserves rects |
| 984 | Rect win(s.active.w, s.active.h); |
| 985 | if (!s.crop.isEmpty()) { |
| 986 | win.intersect(s.crop, &win); |
| 987 | } |
| 988 | // subtract the transparent region and snap to the bounds |
| 989 | Rect bounds = reduce(win, s.activeTransparentRegion); |
Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 990 | Rect frame(getTransform().transform(bounds)); |
Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 991 | frame.intersect(hw->getViewport(), &frame); |
| 992 | if (!s.finalCrop.isEmpty()) { |
| 993 | frame.intersect(s.finalCrop, &frame); |
| 994 | } |
| 995 | const Transform& tr(hw->getTransform()); |
| 996 | return Rect(tr.transform(frame)); |
| 997 | } |
| 998 | #endif |
Riley Andrews | 03414a1 | 2014-07-01 14:22:59 -0700 | [diff] [blame] | 999 | |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 1000 | // --------------------------------------------------------------------------- |
| 1001 | // drawing... |
| 1002 | // --------------------------------------------------------------------------- |
| 1003 | |
| 1004 | void Layer::draw(const sp<const DisplayDevice>& hw, const Region& clip) const { |
Dan Stoza | c701401 | 2014-02-14 15:03:43 -0800 | [diff] [blame] | 1005 | onDraw(hw, clip, false); |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 1006 | } |
| 1007 | |
Dan Stoza | c701401 | 2014-02-14 15:03:43 -0800 | [diff] [blame] | 1008 | void Layer::draw(const sp<const DisplayDevice>& hw, |
| 1009 | bool useIdentityTransform) const { |
| 1010 | onDraw(hw, Region(hw->bounds()), useIdentityTransform); |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 1011 | } |
| 1012 | |
Dan Stoza | c701401 | 2014-02-14 15:03:43 -0800 | [diff] [blame] | 1013 | void Layer::draw(const sp<const DisplayDevice>& hw) const { |
| 1014 | onDraw(hw, Region(hw->bounds()), false); |
| 1015 | } |
| 1016 | |
Robert Carr | cae605c | 2017-03-29 12:10:31 -0700 | [diff] [blame] | 1017 | static constexpr mat4 inverseOrientation(uint32_t transform) { |
| 1018 | const mat4 flipH(-1,0,0,0, 0,1,0,0, 0,0,1,0, 1,0,0,1); |
| 1019 | const mat4 flipV( 1,0,0,0, 0,-1,0,0, 0,0,1,0, 0,1,0,1); |
| 1020 | const mat4 rot90( 0,1,0,0, -1,0,0,0, 0,0,1,0, 1,0,0,1); |
| 1021 | mat4 tr; |
| 1022 | |
| 1023 | if (transform & NATIVE_WINDOW_TRANSFORM_ROT_90) { |
| 1024 | tr = tr * rot90; |
| 1025 | } |
| 1026 | if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_H) { |
| 1027 | tr = tr * flipH; |
| 1028 | } |
| 1029 | if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_V) { |
| 1030 | tr = tr * flipV; |
| 1031 | } |
| 1032 | return inverse(tr); |
| 1033 | } |
| 1034 | |
Courtney Goeltzenleuchter | 5d94389 | 2017-03-22 13:46:46 -0600 | [diff] [blame] | 1035 | /* |
| 1036 | * onDraw will draw the current layer onto the presentable buffer |
| 1037 | */ |
Dan Stoza | c701401 | 2014-02-14 15:03:43 -0800 | [diff] [blame] | 1038 | void Layer::onDraw(const sp<const DisplayDevice>& hw, const Region& clip, |
| 1039 | bool useIdentityTransform) const |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1040 | { |
Jamie Gennis | 1c8e95c | 2012-02-23 19:27:23 -0800 | [diff] [blame] | 1041 | ATRACE_CALL(); |
| 1042 | |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 1043 | if (CC_UNLIKELY(mActiveBuffer == 0)) { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1044 | // the texture has not been created yet, this Layer has |
Mathias Agopian | 179169e | 2010-05-06 20:21:45 -0700 | [diff] [blame] | 1045 | // in fact never been drawn into. This happens frequently with |
| 1046 | // SurfaceView because the WindowManager can't know when the client |
| 1047 | // has drawn the first time. |
| 1048 | |
| 1049 | // If there is nothing under us, we paint the screen in black, otherwise |
| 1050 | // we just skip this update. |
| 1051 | |
| 1052 | // figure out if there is something below us |
| 1053 | Region under; |
Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 1054 | bool finished = false; |
Dan Stoza | 412903f | 2017-04-27 13:42:17 -0700 | [diff] [blame] | 1055 | mFlinger->mDrawingState.traverseInZOrder([&](Layer* layer) { |
Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 1056 | if (finished || layer == static_cast<Layer const*>(this)) { |
| 1057 | finished = true; |
| 1058 | return; |
| 1059 | } |
Mathias Agopian | 4297734 | 2012-08-05 00:40:46 -0700 | [diff] [blame] | 1060 | under.orSelf( hw->getTransform().transform(layer->visibleRegion) ); |
Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 1061 | }); |
Mathias Agopian | 179169e | 2010-05-06 20:21:45 -0700 | [diff] [blame] | 1062 | // if not everything below us is covered, we plug the holes! |
| 1063 | Region holes(clip.subtract(under)); |
| 1064 | if (!holes.isEmpty()) { |
Fabien Sanglard | 1748719 | 2016-12-07 13:03:32 -0800 | [diff] [blame] | 1065 | clearWithOpenGL(hw, 0, 0, 0, 1); |
Mathias Agopian | 179169e | 2010-05-06 20:21:45 -0700 | [diff] [blame] | 1066 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1067 | return; |
| 1068 | } |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 1069 | |
Andy McFadden | 97eba89 | 2012-12-11 15:21:45 -0800 | [diff] [blame] | 1070 | // Bind the current buffer to the GL texture, and wait for it to be |
| 1071 | // ready for us to draw into. |
Andy McFadden | bf974ab | 2012-12-04 16:51:15 -0800 | [diff] [blame] | 1072 | status_t err = mSurfaceFlingerConsumer->bindTextureImage(); |
| 1073 | if (err != NO_ERROR) { |
Andy McFadden | 97eba89 | 2012-12-11 15:21:45 -0800 | [diff] [blame] | 1074 | ALOGW("onDraw: bindTextureImage failed (err=%d)", err); |
Jesse Hall | dc5b485 | 2012-06-29 15:21:18 -0700 | [diff] [blame] | 1075 | // Go ahead and draw the buffer anyway; no matter what we do the screen |
| 1076 | // is probably going to have something visibly wrong. |
| 1077 | } |
| 1078 | |
Jamie Gennis | dd3cb84 | 2012-10-19 18:19:11 -0700 | [diff] [blame] | 1079 | bool blackOutLayer = isProtected() || (isSecure() && !hw->isSecure()); |
| 1080 | |
Mathias Agopian | 875d8e1 | 2013-06-07 15:35:48 -0700 | [diff] [blame] | 1081 | RenderEngine& engine(mFlinger->getRenderEngine()); |
| 1082 | |
Jamie Gennis | dd3cb84 | 2012-10-19 18:19:11 -0700 | [diff] [blame] | 1083 | if (!blackOutLayer) { |
Jamie Gennis | cbb1a95 | 2012-05-08 17:05:52 -0700 | [diff] [blame] | 1084 | // TODO: we could be more subtle with isFixedSize() |
Mathias Agopian | eba8c68 | 2012-09-19 23:14:45 -0700 | [diff] [blame] | 1085 | const bool useFiltering = getFiltering() || needsFiltering(hw) || isFixedSize(); |
Jamie Gennis | cbb1a95 | 2012-05-08 17:05:52 -0700 | [diff] [blame] | 1086 | |
| 1087 | // Query the texture matrix given our current filtering mode. |
| 1088 | float textureMatrix[16]; |
Andy McFadden | bf974ab | 2012-12-04 16:51:15 -0800 | [diff] [blame] | 1089 | mSurfaceFlingerConsumer->setFilteringEnabled(useFiltering); |
| 1090 | mSurfaceFlingerConsumer->getTransformMatrix(textureMatrix); |
Jamie Gennis | cbb1a95 | 2012-05-08 17:05:52 -0700 | [diff] [blame] | 1091 | |
Robert Carr | cae605c | 2017-03-29 12:10:31 -0700 | [diff] [blame] | 1092 | if (getTransformToDisplayInverse()) { |
Mathias Agopian | c1c05de | 2013-09-17 23:45:22 -0700 | [diff] [blame] | 1093 | |
| 1094 | /* |
Pablo Ceballos | 021623b | 2016-04-15 17:31:51 -0700 | [diff] [blame] | 1095 | * the code below applies the primary display's inverse transform to |
| 1096 | * the texture transform |
Mathias Agopian | c1c05de | 2013-09-17 23:45:22 -0700 | [diff] [blame] | 1097 | */ |
Pablo Ceballos | 021623b | 2016-04-15 17:31:51 -0700 | [diff] [blame] | 1098 | uint32_t transform = |
| 1099 | DisplayDevice::getPrimaryDisplayOrientationTransform(); |
Robert Carr | cae605c | 2017-03-29 12:10:31 -0700 | [diff] [blame] | 1100 | mat4 tr = inverseOrientation(transform); |
Mathias Agopian | c1c05de | 2013-09-17 23:45:22 -0700 | [diff] [blame] | 1101 | |
Robert Carr | cae605c | 2017-03-29 12:10:31 -0700 | [diff] [blame] | 1102 | /** |
| 1103 | * TODO(b/36727915): This is basically a hack. |
| 1104 | * |
| 1105 | * Ensure that regardless of the parent transformation, |
| 1106 | * this buffer is always transformed from native display |
| 1107 | * orientation to display orientation. For example, in the case |
| 1108 | * of a camera where the buffer remains in native orientation, |
| 1109 | * we want the pixels to always be upright. |
| 1110 | */ |
Chia-I Wu | e41dbe6 | 2017-06-13 14:10:56 -0700 | [diff] [blame] | 1111 | sp<Layer> p = mDrawingParent.promote(); |
| 1112 | if (p != nullptr) { |
| 1113 | const auto parentTransform = p->getTransform(); |
Robert Carr | cae605c | 2017-03-29 12:10:31 -0700 | [diff] [blame] | 1114 | tr = tr * inverseOrientation(parentTransform.getOrientation()); |
| 1115 | } |
Mathias Agopian | c1c05de | 2013-09-17 23:45:22 -0700 | [diff] [blame] | 1116 | |
| 1117 | // and finally apply it to the original texture matrix |
| 1118 | const mat4 texTransform(mat4(static_cast<const float*>(textureMatrix)) * tr); |
| 1119 | memcpy(textureMatrix, texTransform.asArray(), sizeof(textureMatrix)); |
| 1120 | } |
| 1121 | |
Jamie Gennis | cbb1a95 | 2012-05-08 17:05:52 -0700 | [diff] [blame] | 1122 | // Set things up for texturing. |
Mathias Agopian | 49457ac | 2013-08-14 18:20:17 -0700 | [diff] [blame] | 1123 | mTexture.setDimensions(mActiveBuffer->getWidth(), mActiveBuffer->getHeight()); |
| 1124 | mTexture.setFiltering(useFiltering); |
| 1125 | mTexture.setMatrix(textureMatrix); |
| 1126 | |
| 1127 | engine.setupLayerTexturing(mTexture); |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 1128 | } else { |
Mathias Agopian | 875d8e1 | 2013-06-07 15:35:48 -0700 | [diff] [blame] | 1129 | engine.setupLayerBlackedOut(); |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 1130 | } |
Fabien Sanglard | 8578980 | 2016-12-07 13:08:24 -0800 | [diff] [blame] | 1131 | drawWithOpenGL(hw, useIdentityTransform); |
Mathias Agopian | 875d8e1 | 2013-06-07 15:35:48 -0700 | [diff] [blame] | 1132 | engine.disableTexturing(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1133 | } |
| 1134 | |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 1135 | |
Dan Stoza | c701401 | 2014-02-14 15:03:43 -0800 | [diff] [blame] | 1136 | void Layer::clearWithOpenGL(const sp<const DisplayDevice>& hw, |
Fabien Sanglard | 1748719 | 2016-12-07 13:03:32 -0800 | [diff] [blame] | 1137 | float red, float green, float blue, |
Dan Stoza | c701401 | 2014-02-14 15:03:43 -0800 | [diff] [blame] | 1138 | float alpha) const |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 1139 | { |
Mathias Agopian | 19733a3 | 2013-08-28 18:13:56 -0700 | [diff] [blame] | 1140 | RenderEngine& engine(mFlinger->getRenderEngine()); |
Dan Stoza | c701401 | 2014-02-14 15:03:43 -0800 | [diff] [blame] | 1141 | computeGeometry(hw, mMesh, false); |
Mathias Agopian | 19733a3 | 2013-08-28 18:13:56 -0700 | [diff] [blame] | 1142 | engine.setupFillWithColor(red, green, blue, alpha); |
| 1143 | engine.drawMesh(mMesh); |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 1144 | } |
| 1145 | |
| 1146 | void Layer::clearWithOpenGL( |
Fabien Sanglard | 1748719 | 2016-12-07 13:03:32 -0800 | [diff] [blame] | 1147 | const sp<const DisplayDevice>& hw) const { |
| 1148 | clearWithOpenGL(hw, 0,0,0,0); |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 1149 | } |
| 1150 | |
Dan Stoza | c701401 | 2014-02-14 15:03:43 -0800 | [diff] [blame] | 1151 | void Layer::drawWithOpenGL(const sp<const DisplayDevice>& hw, |
Fabien Sanglard | 8578980 | 2016-12-07 13:08:24 -0800 | [diff] [blame] | 1152 | bool useIdentityTransform) const { |
Mathias Agopian | 1eae0ee | 2013-06-05 16:59:15 -0700 | [diff] [blame] | 1153 | const State& s(getDrawingState()); |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 1154 | |
Dan Stoza | c701401 | 2014-02-14 15:03:43 -0800 | [diff] [blame] | 1155 | computeGeometry(hw, mMesh, useIdentityTransform); |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 1156 | |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 1157 | /* |
| 1158 | * NOTE: the way we compute the texture coordinates here produces |
| 1159 | * different results than when we take the HWC path -- in the later case |
| 1160 | * the "source crop" is rounded to texel boundaries. |
| 1161 | * This can produce significantly different results when the texture |
| 1162 | * is scaled by a large amount. |
| 1163 | * |
| 1164 | * The GL code below is more logical (imho), and the difference with |
| 1165 | * HWC is due to a limitation of the HWC API to integers -- a question |
Mathias Agopian | c1c05de | 2013-09-17 23:45:22 -0700 | [diff] [blame] | 1166 | * is suspend is whether we should ignore this problem or revert to |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 1167 | * GL composition when a buffer scaling is applied (maybe with some |
| 1168 | * minimal value)? Or, we could make GL behave like HWC -- but this feel |
| 1169 | * like more of a hack. |
| 1170 | */ |
Pablo Ceballos | acbe678 | 2016-03-04 17:54:21 +0000 | [diff] [blame] | 1171 | Rect win(computeBounds()); |
| 1172 | |
Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 1173 | Transform t = getTransform(); |
Robert Carr | b5d3d26 | 2016-03-25 15:08:13 -0700 | [diff] [blame] | 1174 | if (!s.finalCrop.isEmpty()) { |
Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 1175 | win = t.transform(win); |
Robert Carr | b5d3d26 | 2016-03-25 15:08:13 -0700 | [diff] [blame] | 1176 | if (!win.intersect(s.finalCrop, &win)) { |
Pablo Ceballos | acbe678 | 2016-03-04 17:54:21 +0000 | [diff] [blame] | 1177 | win.clear(); |
| 1178 | } |
Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 1179 | win = t.inverse().transform(win); |
Pablo Ceballos | acbe678 | 2016-03-04 17:54:21 +0000 | [diff] [blame] | 1180 | if (!win.intersect(computeBounds(), &win)) { |
| 1181 | win.clear(); |
| 1182 | } |
| 1183 | } |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 1184 | |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 1185 | float left = float(win.left) / float(s.active.w); |
| 1186 | float top = float(win.top) / float(s.active.h); |
| 1187 | float right = float(win.right) / float(s.active.w); |
| 1188 | float bottom = float(win.bottom) / float(s.active.h); |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 1189 | |
Mathias Agopian | 875d8e1 | 2013-06-07 15:35:48 -0700 | [diff] [blame] | 1190 | // TODO: we probably want to generate the texture coords with the mesh |
| 1191 | // here we assume that we only have 4 vertices |
Mathias Agopian | ff2ed70 | 2013-09-01 21:36:12 -0700 | [diff] [blame] | 1192 | Mesh::VertexArray<vec2> texCoords(mMesh.getTexCoordArray<vec2>()); |
| 1193 | texCoords[0] = vec2(left, 1.0f - top); |
| 1194 | texCoords[1] = vec2(left, 1.0f - bottom); |
| 1195 | texCoords[2] = vec2(right, 1.0f - bottom); |
| 1196 | texCoords[3] = vec2(right, 1.0f - top); |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 1197 | |
Mathias Agopian | 875d8e1 | 2013-06-07 15:35:48 -0700 | [diff] [blame] | 1198 | RenderEngine& engine(mFlinger->getRenderEngine()); |
Robert Carr | 6452f12 | 2017-03-21 10:41:29 -0700 | [diff] [blame] | 1199 | engine.setupLayerBlending(mPremultipliedAlpha, isOpaque(s), getAlpha()); |
Courtney Goeltzenleuchter | 5d94389 | 2017-03-22 13:46:46 -0600 | [diff] [blame] | 1200 | #ifdef USE_HWC2 |
| 1201 | engine.setSourceDataSpace(mCurrentState.dataSpace); |
| 1202 | #endif |
Mathias Agopian | 5cdc899 | 2013-08-13 20:51:23 -0700 | [diff] [blame] | 1203 | engine.drawMesh(mMesh); |
Mathias Agopian | 875d8e1 | 2013-06-07 15:35:48 -0700 | [diff] [blame] | 1204 | engine.disableBlending(); |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 1205 | } |
| 1206 | |
Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 1207 | #ifdef USE_HWC2 |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 1208 | void Layer::setCompositionType(int32_t hwcId, HWC2::Composition type, |
| 1209 | bool callIntoHwc) { |
| 1210 | if (mHwcLayers.count(hwcId) == 0) { |
| 1211 | ALOGE("setCompositionType called without a valid HWC layer"); |
| 1212 | return; |
| 1213 | } |
| 1214 | auto& hwcInfo = mHwcLayers[hwcId]; |
| 1215 | auto& hwcLayer = hwcInfo.layer; |
| 1216 | ALOGV("setCompositionType(%" PRIx64 ", %s, %d)", hwcLayer->getId(), |
| 1217 | to_string(type).c_str(), static_cast<int>(callIntoHwc)); |
| 1218 | if (hwcInfo.compositionType != type) { |
| 1219 | ALOGV(" actually setting"); |
| 1220 | hwcInfo.compositionType = type; |
| 1221 | if (callIntoHwc) { |
| 1222 | auto error = hwcLayer->setCompositionType(type); |
| 1223 | ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set " |
| 1224 | "composition type %s: %s (%d)", mName.string(), |
| 1225 | to_string(type).c_str(), to_string(error).c_str(), |
| 1226 | static_cast<int32_t>(error)); |
| 1227 | } |
| 1228 | } |
| 1229 | } |
| 1230 | |
| 1231 | HWC2::Composition Layer::getCompositionType(int32_t hwcId) const { |
Dan Stoza | ec0f717 | 2016-07-21 11:09:40 -0700 | [diff] [blame] | 1232 | if (hwcId == DisplayDevice::DISPLAY_ID_INVALID) { |
| 1233 | // If we're querying the composition type for a display that does not |
| 1234 | // have a HWC counterpart, then it will always be Client |
| 1235 | return HWC2::Composition::Client; |
| 1236 | } |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 1237 | if (mHwcLayers.count(hwcId) == 0) { |
Dan Stoza | ec0f717 | 2016-07-21 11:09:40 -0700 | [diff] [blame] | 1238 | ALOGE("getCompositionType called with an invalid HWC layer"); |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 1239 | return HWC2::Composition::Invalid; |
| 1240 | } |
| 1241 | return mHwcLayers.at(hwcId).compositionType; |
| 1242 | } |
| 1243 | |
| 1244 | void Layer::setClearClientTarget(int32_t hwcId, bool clear) { |
| 1245 | if (mHwcLayers.count(hwcId) == 0) { |
| 1246 | ALOGE("setClearClientTarget called without a valid HWC layer"); |
| 1247 | return; |
| 1248 | } |
| 1249 | mHwcLayers[hwcId].clearClientTarget = clear; |
| 1250 | } |
| 1251 | |
| 1252 | bool Layer::getClearClientTarget(int32_t hwcId) const { |
| 1253 | if (mHwcLayers.count(hwcId) == 0) { |
| 1254 | ALOGE("getClearClientTarget called without a valid HWC layer"); |
| 1255 | return false; |
| 1256 | } |
| 1257 | return mHwcLayers.at(hwcId).clearClientTarget; |
| 1258 | } |
Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 1259 | #endif |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 1260 | |
Ruben Brunk | 1681d95 | 2014-06-27 15:51:55 -0700 | [diff] [blame] | 1261 | uint32_t Layer::getProducerStickyTransform() const { |
| 1262 | int producerStickyTransform = 0; |
| 1263 | int ret = mProducer->query(NATIVE_WINDOW_STICKY_TRANSFORM, &producerStickyTransform); |
| 1264 | if (ret != OK) { |
| 1265 | ALOGW("%s: Error %s (%d) while querying window sticky transform.", __FUNCTION__, |
| 1266 | strerror(-ret), ret); |
| 1267 | return 0; |
| 1268 | } |
| 1269 | return static_cast<uint32_t>(producerStickyTransform); |
| 1270 | } |
| 1271 | |
Dan Stoza | c5da271 | 2016-07-20 15:38:12 -0700 | [diff] [blame] | 1272 | bool Layer::latchUnsignaledBuffers() { |
| 1273 | static bool propertyLoaded = false; |
| 1274 | static bool latch = false; |
| 1275 | static std::mutex mutex; |
| 1276 | std::lock_guard<std::mutex> lock(mutex); |
| 1277 | if (!propertyLoaded) { |
| 1278 | char value[PROPERTY_VALUE_MAX] = {}; |
| 1279 | property_get("debug.sf.latch_unsignaled", value, "0"); |
| 1280 | latch = atoi(value); |
| 1281 | propertyLoaded = true; |
| 1282 | } |
| 1283 | return latch; |
| 1284 | } |
| 1285 | |
Dan Stoza | cac3538 | 2016-01-27 12:21:06 -0800 | [diff] [blame] | 1286 | uint64_t Layer::getHeadFrameNumber() const { |
| 1287 | Mutex::Autolock lock(mQueueItemLock); |
| 1288 | if (!mQueueItems.empty()) { |
| 1289 | return mQueueItems[0].mFrameNumber; |
| 1290 | } else { |
| 1291 | return mCurrentFrameNumber; |
| 1292 | } |
| 1293 | } |
| 1294 | |
Dan Stoza | 1ce6581 | 2016-06-15 16:26:27 -0700 | [diff] [blame] | 1295 | bool Layer::headFenceHasSignaled() const { |
Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 1296 | #ifdef USE_HWC2 |
Dan Stoza | c5da271 | 2016-07-20 15:38:12 -0700 | [diff] [blame] | 1297 | if (latchUnsignaledBuffers()) { |
| 1298 | return true; |
| 1299 | } |
| 1300 | |
Dan Stoza | 1ce6581 | 2016-06-15 16:26:27 -0700 | [diff] [blame] | 1301 | Mutex::Autolock lock(mQueueItemLock); |
| 1302 | if (mQueueItems.empty()) { |
| 1303 | return true; |
| 1304 | } |
| 1305 | if (mQueueItems[0].mIsDroppable) { |
| 1306 | // Even though this buffer's fence may not have signaled yet, it could |
| 1307 | // be replaced by another buffer before it has a chance to, which means |
| 1308 | // that it's possible to get into a situation where a buffer is never |
| 1309 | // able to be latched. To avoid this, grab this buffer anyway. |
| 1310 | return true; |
| 1311 | } |
Brian Anderson | fbc80ae | 2017-05-26 16:23:54 -0700 | [diff] [blame] | 1312 | return mQueueItems[0].mFenceTime->getSignalTime() != |
| 1313 | Fence::SIGNAL_TIME_PENDING; |
Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 1314 | #else |
| 1315 | return true; |
| 1316 | #endif |
Dan Stoza | 1ce6581 | 2016-06-15 16:26:27 -0700 | [diff] [blame] | 1317 | } |
| 1318 | |
Dan Stoza | cac3538 | 2016-01-27 12:21:06 -0800 | [diff] [blame] | 1319 | bool Layer::addSyncPoint(const std::shared_ptr<SyncPoint>& point) { |
| 1320 | if (point->getFrameNumber() <= mCurrentFrameNumber) { |
| 1321 | // Don't bother with a SyncPoint, since we've already latched the |
| 1322 | // relevant frame |
| 1323 | return false; |
Dan Stoza | 7dde599 | 2015-05-22 09:51:44 -0700 | [diff] [blame] | 1324 | } |
| 1325 | |
Dan Stoza | cac3538 | 2016-01-27 12:21:06 -0800 | [diff] [blame] | 1326 | Mutex::Autolock lock(mLocalSyncPointMutex); |
| 1327 | mLocalSyncPoints.push_back(point); |
| 1328 | return true; |
Dan Stoza | 7dde599 | 2015-05-22 09:51:44 -0700 | [diff] [blame] | 1329 | } |
| 1330 | |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 1331 | void Layer::setFiltering(bool filtering) { |
| 1332 | mFiltering = filtering; |
| 1333 | } |
| 1334 | |
| 1335 | bool Layer::getFiltering() const { |
| 1336 | return mFiltering; |
| 1337 | } |
| 1338 | |
Eric Hassold | ac45e6b | 2011-02-10 14:41:26 -0800 | [diff] [blame] | 1339 | // As documented in libhardware header, formats in the range |
| 1340 | // 0x100 - 0x1FF are specific to the HAL implementation, and |
| 1341 | // are known to have no alpha channel |
| 1342 | // TODO: move definition for device-specific range into |
| 1343 | // hardware.h, instead of using hard-coded values here. |
| 1344 | #define HARDWARE_IS_DEVICE_FORMAT(f) ((f) >= 0x100 && (f) <= 0x1FF) |
| 1345 | |
Mathias Agopian | 5773d3f | 2013-07-25 19:24:31 -0700 | [diff] [blame] | 1346 | bool Layer::getOpacityForFormat(uint32_t format) { |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 1347 | if (HARDWARE_IS_DEVICE_FORMAT(format)) { |
| 1348 | return true; |
Eric Hassold | ac45e6b | 2011-02-10 14:41:26 -0800 | [diff] [blame] | 1349 | } |
Mathias Agopian | 5773d3f | 2013-07-25 19:24:31 -0700 | [diff] [blame] | 1350 | switch (format) { |
| 1351 | case HAL_PIXEL_FORMAT_RGBA_8888: |
| 1352 | case HAL_PIXEL_FORMAT_BGRA_8888: |
Romain Guy | ff41514 | 2016-12-13 16:51:25 -0800 | [diff] [blame] | 1353 | case HAL_PIXEL_FORMAT_RGBA_FP16: |
Romain Guy | 541f226 | 2017-02-10 18:50:17 -0800 | [diff] [blame] | 1354 | case HAL_PIXEL_FORMAT_RGBA_1010102: |
Mathias Agopian | dd53371 | 2013-07-26 15:31:39 -0700 | [diff] [blame] | 1355 | return false; |
Mathias Agopian | 5773d3f | 2013-07-25 19:24:31 -0700 | [diff] [blame] | 1356 | } |
| 1357 | // in all other case, we have no blending (also for unknown formats) |
Mathias Agopian | dd53371 | 2013-07-26 15:31:39 -0700 | [diff] [blame] | 1358 | return true; |
Eric Hassold | ac45e6b | 2011-02-10 14:41:26 -0800 | [diff] [blame] | 1359 | } |
| 1360 | |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 1361 | // ---------------------------------------------------------------------------- |
| 1362 | // local state |
| 1363 | // ---------------------------------------------------------------------------- |
| 1364 | |
Pablo Ceballos | acbe678 | 2016-03-04 17:54:21 +0000 | [diff] [blame] | 1365 | static void boundPoint(vec2* point, const Rect& crop) { |
| 1366 | if (point->x < crop.left) { |
| 1367 | point->x = crop.left; |
| 1368 | } |
| 1369 | if (point->x > crop.right) { |
| 1370 | point->x = crop.right; |
| 1371 | } |
| 1372 | if (point->y < crop.top) { |
| 1373 | point->y = crop.top; |
| 1374 | } |
| 1375 | if (point->y > crop.bottom) { |
| 1376 | point->y = crop.bottom; |
| 1377 | } |
| 1378 | } |
| 1379 | |
Dan Stoza | c701401 | 2014-02-14 15:03:43 -0800 | [diff] [blame] | 1380 | void Layer::computeGeometry(const sp<const DisplayDevice>& hw, Mesh& mesh, |
| 1381 | bool useIdentityTransform) const |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 1382 | { |
Mathias Agopian | 1eae0ee | 2013-06-05 16:59:15 -0700 | [diff] [blame] | 1383 | const Layer::State& s(getDrawingState()); |
Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 1384 | const Transform hwTransform(hw->getTransform()); |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 1385 | const uint32_t hw_h = hw->getHeight(); |
Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 1386 | Rect win = computeBounds(); |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 1387 | |
Pablo Ceballos | acbe678 | 2016-03-04 17:54:21 +0000 | [diff] [blame] | 1388 | vec2 lt = vec2(win.left, win.top); |
| 1389 | vec2 lb = vec2(win.left, win.bottom); |
| 1390 | vec2 rb = vec2(win.right, win.bottom); |
| 1391 | vec2 rt = vec2(win.right, win.top); |
| 1392 | |
Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 1393 | Transform layerTransform = getTransform(); |
Pablo Ceballos | acbe678 | 2016-03-04 17:54:21 +0000 | [diff] [blame] | 1394 | if (!useIdentityTransform) { |
Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 1395 | lt = layerTransform.transform(lt); |
| 1396 | lb = layerTransform.transform(lb); |
| 1397 | rb = layerTransform.transform(rb); |
| 1398 | rt = layerTransform.transform(rt); |
Pablo Ceballos | acbe678 | 2016-03-04 17:54:21 +0000 | [diff] [blame] | 1399 | } |
| 1400 | |
Robert Carr | b5d3d26 | 2016-03-25 15:08:13 -0700 | [diff] [blame] | 1401 | if (!s.finalCrop.isEmpty()) { |
| 1402 | boundPoint(<, s.finalCrop); |
| 1403 | boundPoint(&lb, s.finalCrop); |
| 1404 | boundPoint(&rb, s.finalCrop); |
| 1405 | boundPoint(&rt, s.finalCrop); |
Pablo Ceballos | acbe678 | 2016-03-04 17:54:21 +0000 | [diff] [blame] | 1406 | } |
| 1407 | |
Mathias Agopian | ff2ed70 | 2013-09-01 21:36:12 -0700 | [diff] [blame] | 1408 | Mesh::VertexArray<vec2> position(mesh.getPositionArray<vec2>()); |
Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 1409 | position[0] = hwTransform.transform(lt); |
| 1410 | position[1] = hwTransform.transform(lb); |
| 1411 | position[2] = hwTransform.transform(rb); |
| 1412 | position[3] = hwTransform.transform(rt); |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 1413 | for (size_t i=0 ; i<4 ; i++) { |
Mathias Agopian | 5cdc899 | 2013-08-13 20:51:23 -0700 | [diff] [blame] | 1414 | position[i].y = hw_h - position[i].y; |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 1415 | } |
| 1416 | } |
Eric Hassold | ac45e6b | 2011-02-10 14:41:26 -0800 | [diff] [blame] | 1417 | |
Andy McFadden | 4125a4f | 2014-01-29 17:17:11 -0800 | [diff] [blame] | 1418 | bool Layer::isOpaque(const Layer::State& s) const |
Mathias Agopian | a7f6692 | 2010-05-26 22:08:52 -0700 | [diff] [blame] | 1419 | { |
neo.he | e6fd41d | 2017-02-17 14:35:33 +0800 | [diff] [blame] | 1420 | // if we don't have a buffer or sidebandStream yet, we're translucent regardless of the |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 1421 | // layer's opaque flag. |
neo.he | e6fd41d | 2017-02-17 14:35:33 +0800 | [diff] [blame] | 1422 | if ((mSidebandStream == nullptr) && (mActiveBuffer == nullptr)) { |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 1423 | return false; |
Jamie Gennis | db5230f | 2011-07-28 14:54:07 -0700 | [diff] [blame] | 1424 | } |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 1425 | |
| 1426 | // if the layer has the opaque flag, then we're always opaque, |
| 1427 | // otherwise we use the current buffer's format. |
Andy McFadden | 4125a4f | 2014-01-29 17:17:11 -0800 | [diff] [blame] | 1428 | return ((s.flags & layer_state_t::eLayerOpaque) != 0) || mCurrentOpacity; |
Mathias Agopian | a7f6692 | 2010-05-26 22:08:52 -0700 | [diff] [blame] | 1429 | } |
| 1430 | |
Dan Stoza | 2311608 | 2015-06-18 14:58:39 -0700 | [diff] [blame] | 1431 | bool Layer::isSecure() const |
| 1432 | { |
| 1433 | const Layer::State& s(mDrawingState); |
| 1434 | return (s.flags & layer_state_t::eLayerSecure); |
| 1435 | } |
| 1436 | |
Jamie Gennis | 7a4d0df | 2011-03-09 17:05:02 -0800 | [diff] [blame] | 1437 | bool Layer::isProtected() const |
| 1438 | { |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 1439 | const sp<GraphicBuffer>& activeBuffer(mActiveBuffer); |
Jamie Gennis | 7a4d0df | 2011-03-09 17:05:02 -0800 | [diff] [blame] | 1440 | return (activeBuffer != 0) && |
| 1441 | (activeBuffer->getUsage() & GRALLOC_USAGE_PROTECTED); |
| 1442 | } |
Mathias Agopian | b5b7f26 | 2010-05-07 15:58:44 -0700 | [diff] [blame] | 1443 | |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 1444 | bool Layer::isFixedSize() const { |
Robert Carr | c3574f7 | 2016-03-24 12:19:32 -0700 | [diff] [blame] | 1445 | return getEffectiveScalingMode() != NATIVE_WINDOW_SCALING_MODE_FREEZE; |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 1446 | } |
| 1447 | |
| 1448 | bool Layer::isCropped() const { |
| 1449 | return !mCurrentCrop.isEmpty(); |
| 1450 | } |
| 1451 | |
| 1452 | bool Layer::needsFiltering(const sp<const DisplayDevice>& hw) const { |
| 1453 | return mNeedsFiltering || hw->needsFiltering(); |
| 1454 | } |
| 1455 | |
| 1456 | void Layer::setVisibleRegion(const Region& visibleRegion) { |
| 1457 | // always called from main thread |
| 1458 | this->visibleRegion = visibleRegion; |
| 1459 | } |
| 1460 | |
| 1461 | void Layer::setCoveredRegion(const Region& coveredRegion) { |
| 1462 | // always called from main thread |
| 1463 | this->coveredRegion = coveredRegion; |
| 1464 | } |
| 1465 | |
| 1466 | void Layer::setVisibleNonTransparentRegion(const Region& |
| 1467 | setVisibleNonTransparentRegion) { |
| 1468 | // always called from main thread |
| 1469 | this->visibleNonTransparentRegion = setVisibleNonTransparentRegion; |
| 1470 | } |
| 1471 | |
| 1472 | // ---------------------------------------------------------------------------- |
| 1473 | // transaction |
| 1474 | // ---------------------------------------------------------------------------- |
| 1475 | |
Dan Stoza | 7dde599 | 2015-05-22 09:51:44 -0700 | [diff] [blame] | 1476 | void Layer::pushPendingState() { |
| 1477 | if (!mCurrentState.modified) { |
| 1478 | return; |
| 1479 | } |
| 1480 | |
Dan Stoza | 7dde599 | 2015-05-22 09:51:44 -0700 | [diff] [blame] | 1481 | // If this transaction is waiting on the receipt of a frame, generate a sync |
| 1482 | // point and send it to the remote layer. |
Robert Carr | 0d48072 | 2017-01-10 16:42:54 -0800 | [diff] [blame] | 1483 | if (mCurrentState.barrierLayer != nullptr) { |
| 1484 | sp<Layer> barrierLayer = mCurrentState.barrierLayer.promote(); |
| 1485 | if (barrierLayer == nullptr) { |
| 1486 | ALOGE("[%s] Unable to promote barrier Layer.", mName.string()); |
Dan Stoza | 7dde599 | 2015-05-22 09:51:44 -0700 | [diff] [blame] | 1487 | // If we can't promote the layer we are intended to wait on, |
| 1488 | // then it is expired or otherwise invalid. Allow this transaction |
| 1489 | // to be applied as per normal (no synchronization). |
Robert Carr | 0d48072 | 2017-01-10 16:42:54 -0800 | [diff] [blame] | 1490 | mCurrentState.barrierLayer = nullptr; |
Pablo Ceballos | 3bddd5b | 2015-11-19 14:39:14 -0800 | [diff] [blame] | 1491 | } else { |
| 1492 | auto syncPoint = std::make_shared<SyncPoint>( |
| 1493 | mCurrentState.frameNumber); |
Robert Carr | 0d48072 | 2017-01-10 16:42:54 -0800 | [diff] [blame] | 1494 | if (barrierLayer->addSyncPoint(syncPoint)) { |
Dan Stoza | cac3538 | 2016-01-27 12:21:06 -0800 | [diff] [blame] | 1495 | mRemoteSyncPoints.push_back(std::move(syncPoint)); |
| 1496 | } else { |
| 1497 | // We already missed the frame we're supposed to synchronize |
| 1498 | // on, so go ahead and apply the state update |
Robert Carr | 0d48072 | 2017-01-10 16:42:54 -0800 | [diff] [blame] | 1499 | mCurrentState.barrierLayer = nullptr; |
Dan Stoza | cac3538 | 2016-01-27 12:21:06 -0800 | [diff] [blame] | 1500 | } |
Dan Stoza | 7dde599 | 2015-05-22 09:51:44 -0700 | [diff] [blame] | 1501 | } |
| 1502 | |
Dan Stoza | 7dde599 | 2015-05-22 09:51:44 -0700 | [diff] [blame] | 1503 | // Wake us up to check if the frame has been received |
| 1504 | setTransactionFlags(eTransactionNeeded); |
Dan Stoza | f5702ff | 2016-11-02 16:27:47 -0700 | [diff] [blame] | 1505 | mFlinger->setTransactionFlags(eTraversalNeeded); |
Dan Stoza | 7dde599 | 2015-05-22 09:51:44 -0700 | [diff] [blame] | 1506 | } |
| 1507 | mPendingStates.push_back(mCurrentState); |
Dan Stoza | f7ba41a | 2017-05-10 15:11:11 -0700 | [diff] [blame] | 1508 | ATRACE_INT(mTransactionName.string(), mPendingStates.size()); |
Dan Stoza | 7dde599 | 2015-05-22 09:51:44 -0700 | [diff] [blame] | 1509 | } |
| 1510 | |
Pablo Ceballos | 05289c2 | 2016-04-14 15:49:55 -0700 | [diff] [blame] | 1511 | void Layer::popPendingState(State* stateToCommit) { |
| 1512 | auto oldFlags = stateToCommit->flags; |
| 1513 | *stateToCommit = mPendingStates[0]; |
| 1514 | stateToCommit->flags = (oldFlags & ~stateToCommit->mask) | |
| 1515 | (stateToCommit->flags & stateToCommit->mask); |
Dan Stoza | 7dde599 | 2015-05-22 09:51:44 -0700 | [diff] [blame] | 1516 | |
| 1517 | mPendingStates.removeAt(0); |
Dan Stoza | f7ba41a | 2017-05-10 15:11:11 -0700 | [diff] [blame] | 1518 | ATRACE_INT(mTransactionName.string(), mPendingStates.size()); |
Dan Stoza | 7dde599 | 2015-05-22 09:51:44 -0700 | [diff] [blame] | 1519 | } |
| 1520 | |
Pablo Ceballos | 05289c2 | 2016-04-14 15:49:55 -0700 | [diff] [blame] | 1521 | bool Layer::applyPendingStates(State* stateToCommit) { |
Dan Stoza | 7dde599 | 2015-05-22 09:51:44 -0700 | [diff] [blame] | 1522 | bool stateUpdateAvailable = false; |
| 1523 | while (!mPendingStates.empty()) { |
Robert Carr | 0d48072 | 2017-01-10 16:42:54 -0800 | [diff] [blame] | 1524 | if (mPendingStates[0].barrierLayer != nullptr) { |
Dan Stoza | 7dde599 | 2015-05-22 09:51:44 -0700 | [diff] [blame] | 1525 | if (mRemoteSyncPoints.empty()) { |
| 1526 | // If we don't have a sync point for this, apply it anyway. It |
| 1527 | // will be visually wrong, but it should keep us from getting |
| 1528 | // into too much trouble. |
| 1529 | ALOGE("[%s] No local sync point found", mName.string()); |
Pablo Ceballos | 05289c2 | 2016-04-14 15:49:55 -0700 | [diff] [blame] | 1530 | popPendingState(stateToCommit); |
Dan Stoza | 7dde599 | 2015-05-22 09:51:44 -0700 | [diff] [blame] | 1531 | stateUpdateAvailable = true; |
| 1532 | continue; |
| 1533 | } |
| 1534 | |
Dan Stoza | cac3538 | 2016-01-27 12:21:06 -0800 | [diff] [blame] | 1535 | if (mRemoteSyncPoints.front()->getFrameNumber() != |
| 1536 | mPendingStates[0].frameNumber) { |
| 1537 | ALOGE("[%s] Unexpected sync point frame number found", |
| 1538 | mName.string()); |
| 1539 | |
| 1540 | // Signal our end of the sync point and then dispose of it |
| 1541 | mRemoteSyncPoints.front()->setTransactionApplied(); |
| 1542 | mRemoteSyncPoints.pop_front(); |
| 1543 | continue; |
| 1544 | } |
| 1545 | |
Dan Stoza | 7dde599 | 2015-05-22 09:51:44 -0700 | [diff] [blame] | 1546 | if (mRemoteSyncPoints.front()->frameIsAvailable()) { |
| 1547 | // Apply the state update |
Pablo Ceballos | 05289c2 | 2016-04-14 15:49:55 -0700 | [diff] [blame] | 1548 | popPendingState(stateToCommit); |
Dan Stoza | 7dde599 | 2015-05-22 09:51:44 -0700 | [diff] [blame] | 1549 | stateUpdateAvailable = true; |
| 1550 | |
| 1551 | // Signal our end of the sync point and then dispose of it |
| 1552 | mRemoteSyncPoints.front()->setTransactionApplied(); |
| 1553 | mRemoteSyncPoints.pop_front(); |
Dan Stoza | 792e529 | 2016-02-11 11:43:58 -0800 | [diff] [blame] | 1554 | } else { |
| 1555 | break; |
Dan Stoza | 7dde599 | 2015-05-22 09:51:44 -0700 | [diff] [blame] | 1556 | } |
Dan Stoza | 7dde599 | 2015-05-22 09:51:44 -0700 | [diff] [blame] | 1557 | } else { |
Pablo Ceballos | 05289c2 | 2016-04-14 15:49:55 -0700 | [diff] [blame] | 1558 | popPendingState(stateToCommit); |
Dan Stoza | 7dde599 | 2015-05-22 09:51:44 -0700 | [diff] [blame] | 1559 | stateUpdateAvailable = true; |
| 1560 | } |
| 1561 | } |
| 1562 | |
| 1563 | // If we still have pending updates, wake SurfaceFlinger back up and point |
| 1564 | // it at this layer so we can process them |
| 1565 | if (!mPendingStates.empty()) { |
| 1566 | setTransactionFlags(eTransactionNeeded); |
| 1567 | mFlinger->setTransactionFlags(eTraversalNeeded); |
| 1568 | } |
| 1569 | |
| 1570 | mCurrentState.modified = false; |
| 1571 | return stateUpdateAvailable; |
| 1572 | } |
| 1573 | |
| 1574 | void Layer::notifyAvailableFrames() { |
Dan Stoza | cac3538 | 2016-01-27 12:21:06 -0800 | [diff] [blame] | 1575 | auto headFrameNumber = getHeadFrameNumber(); |
Dan Stoza | 1ce6581 | 2016-06-15 16:26:27 -0700 | [diff] [blame] | 1576 | bool headFenceSignaled = headFenceHasSignaled(); |
Dan Stoza | cac3538 | 2016-01-27 12:21:06 -0800 | [diff] [blame] | 1577 | Mutex::Autolock lock(mLocalSyncPointMutex); |
| 1578 | for (auto& point : mLocalSyncPoints) { |
Dan Stoza | 1ce6581 | 2016-06-15 16:26:27 -0700 | [diff] [blame] | 1579 | if (headFrameNumber >= point->getFrameNumber() && headFenceSignaled) { |
Dan Stoza | cac3538 | 2016-01-27 12:21:06 -0800 | [diff] [blame] | 1580 | point->setFrameAvailable(); |
| 1581 | } |
Dan Stoza | 7dde599 | 2015-05-22 09:51:44 -0700 | [diff] [blame] | 1582 | } |
| 1583 | } |
| 1584 | |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 1585 | uint32_t Layer::doTransaction(uint32_t flags) { |
Jamie Gennis | 1c8e95c | 2012-02-23 19:27:23 -0800 | [diff] [blame] | 1586 | ATRACE_CALL(); |
| 1587 | |
Dan Stoza | 7dde599 | 2015-05-22 09:51:44 -0700 | [diff] [blame] | 1588 | pushPendingState(); |
Pablo Ceballos | 05289c2 | 2016-04-14 15:49:55 -0700 | [diff] [blame] | 1589 | Layer::State c = getCurrentState(); |
| 1590 | if (!applyPendingStates(&c)) { |
Dan Stoza | 7dde599 | 2015-05-22 09:51:44 -0700 | [diff] [blame] | 1591 | return 0; |
| 1592 | } |
| 1593 | |
Mathias Agopian | 1eae0ee | 2013-06-05 16:59:15 -0700 | [diff] [blame] | 1594 | const Layer::State& s(getDrawingState()); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1595 | |
Mathias Agopian | 1eae0ee | 2013-06-05 16:59:15 -0700 | [diff] [blame] | 1596 | const bool sizeChanged = (c.requested.w != s.requested.w) || |
| 1597 | (c.requested.h != s.requested.h); |
Mathias Agopian | a138f89 | 2010-05-21 17:24:35 -0700 | [diff] [blame] | 1598 | |
| 1599 | if (sizeChanged) { |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 1600 | // the size changed, we need to ask our client to request a new buffer |
Steve Block | 9d45368 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 1601 | ALOGD_IF(DEBUG_RESIZE, |
Andy McFadden | 6905205 | 2012-09-14 16:10:11 -0700 | [diff] [blame] | 1602 | "doTransaction: geometry (layer=%p '%s'), tr=%02x, scalingMode=%d\n" |
Mathias Agopian | 419e196 | 2012-05-23 14:34:07 -0700 | [diff] [blame] | 1603 | " current={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n" |
Robert Carr | b5d3d26 | 2016-03-25 15:08:13 -0700 | [diff] [blame] | 1604 | " requested={ wh={%4u,%4u} }}\n" |
Mathias Agopian | 419e196 | 2012-05-23 14:34:07 -0700 | [diff] [blame] | 1605 | " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n" |
Robert Carr | b5d3d26 | 2016-03-25 15:08:13 -0700 | [diff] [blame] | 1606 | " requested={ wh={%4u,%4u} }}\n", |
Robert Carr | c3574f7 | 2016-03-24 12:19:32 -0700 | [diff] [blame] | 1607 | this, getName().string(), mCurrentTransform, |
| 1608 | getEffectiveScalingMode(), |
Mathias Agopian | 1eae0ee | 2013-06-05 16:59:15 -0700 | [diff] [blame] | 1609 | c.active.w, c.active.h, |
Robert Carr | b5d3d26 | 2016-03-25 15:08:13 -0700 | [diff] [blame] | 1610 | c.crop.left, |
| 1611 | c.crop.top, |
| 1612 | c.crop.right, |
| 1613 | c.crop.bottom, |
| 1614 | c.crop.getWidth(), |
| 1615 | c.crop.getHeight(), |
Mathias Agopian | 1eae0ee | 2013-06-05 16:59:15 -0700 | [diff] [blame] | 1616 | c.requested.w, c.requested.h, |
Mathias Agopian | 1eae0ee | 2013-06-05 16:59:15 -0700 | [diff] [blame] | 1617 | s.active.w, s.active.h, |
Robert Carr | b5d3d26 | 2016-03-25 15:08:13 -0700 | [diff] [blame] | 1618 | s.crop.left, |
| 1619 | s.crop.top, |
| 1620 | s.crop.right, |
| 1621 | s.crop.bottom, |
| 1622 | s.crop.getWidth(), |
| 1623 | s.crop.getHeight(), |
| 1624 | s.requested.w, s.requested.h); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1625 | |
Jamie Gennis | 2a0d5b6 | 2011-09-26 16:54:44 -0700 | [diff] [blame] | 1626 | // record the new size, form this point on, when the client request |
| 1627 | // a buffer, it'll get the new size. |
Andy McFadden | bf974ab | 2012-12-04 16:51:15 -0800 | [diff] [blame] | 1628 | mSurfaceFlingerConsumer->setDefaultBufferSize( |
Mathias Agopian | 1eae0ee | 2013-06-05 16:59:15 -0700 | [diff] [blame] | 1629 | c.requested.w, c.requested.h); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1630 | } |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 1631 | |
Robert Carr | 82364e3 | 2016-05-15 11:27:47 -0700 | [diff] [blame] | 1632 | const bool resizePending = (c.requested.w != c.active.w) || |
| 1633 | (c.requested.h != c.active.h); |
Mathias Agopian | 0cd545f | 2012-06-07 14:18:55 -0700 | [diff] [blame] | 1634 | if (!isFixedSize()) { |
Dan Stoza | 9e9b044 | 2015-04-22 14:59:08 -0700 | [diff] [blame] | 1635 | if (resizePending && mSidebandStream == NULL) { |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 1636 | // don't let Layer::doTransaction update the drawing state |
Mathias Agopian | 0cd545f | 2012-06-07 14:18:55 -0700 | [diff] [blame] | 1637 | // if we have a pending resize, unless we are in fixed-size mode. |
| 1638 | // the drawing state will be updated only once we receive a buffer |
| 1639 | // with the correct size. |
| 1640 | // |
| 1641 | // in particular, we want to make sure the clip (which is part |
| 1642 | // of the geometry state) is latched together with the size but is |
| 1643 | // latched immediately when no resizing is involved. |
Dan Stoza | 9e9b044 | 2015-04-22 14:59:08 -0700 | [diff] [blame] | 1644 | // |
| 1645 | // If a sideband stream is attached, however, we want to skip this |
| 1646 | // optimization so that transactions aren't missed when a buffer |
| 1647 | // never arrives |
Mathias Agopian | 0cd545f | 2012-06-07 14:18:55 -0700 | [diff] [blame] | 1648 | |
| 1649 | flags |= eDontUpdateGeometryState; |
| 1650 | } |
| 1651 | } |
| 1652 | |
Robert Carr | 7bf247e | 2017-05-18 14:02:49 -0700 | [diff] [blame] | 1653 | // Here we apply various requested geometry states, depending on our |
| 1654 | // latching configuration. See Layer.h for a detailed discussion of |
| 1655 | // how geometry latching is controlled. |
| 1656 | if (!(flags & eDontUpdateGeometryState)) { |
Pablo Ceballos | 7d05257 | 2016-06-02 17:46:05 -0700 | [diff] [blame] | 1657 | Layer::State& editCurrentState(getCurrentState()); |
Robert Carr | 7bf247e | 2017-05-18 14:02:49 -0700 | [diff] [blame] | 1658 | |
| 1659 | // If mFreezeGeometryUpdates is true we are in the setGeometryAppliesWithResize |
| 1660 | // mode, which causes attributes which normally latch regardless of scaling mode, |
| 1661 | // to be delayed. We copy the requested state to the active state making sure |
| 1662 | // to respect these rules (again see Layer.h for a detailed discussion). |
| 1663 | // |
| 1664 | // There is an awkward asymmetry in the handling of the crop states in the position |
| 1665 | // states, as can be seen below. Largely this arises from position and transform |
| 1666 | // being stored in the same data structure while having different latching rules. |
| 1667 | // b/38182305 |
| 1668 | // |
| 1669 | // Careful that "c" and editCurrentState may not begin as equivalent due to |
| 1670 | // applyPendingStates in the presence of deferred transactions. |
| 1671 | if (mFreezeGeometryUpdates) { |
Robert Carr | 82364e3 | 2016-05-15 11:27:47 -0700 | [diff] [blame] | 1672 | float tx = c.active.transform.tx(); |
| 1673 | float ty = c.active.transform.ty(); |
| 1674 | c.active = c.requested; |
| 1675 | c.active.transform.set(tx, ty); |
| 1676 | editCurrentState.active = c.active; |
| 1677 | } else { |
| 1678 | editCurrentState.active = editCurrentState.requested; |
| 1679 | c.active = c.requested; |
| 1680 | } |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 1681 | } |
| 1682 | |
Mathias Agopian | 1eae0ee | 2013-06-05 16:59:15 -0700 | [diff] [blame] | 1683 | if (s.active != c.active) { |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 1684 | // invalidate and recompute the visible regions if needed |
| 1685 | flags |= Layer::eVisibleRegion; |
| 1686 | } |
| 1687 | |
Mathias Agopian | 1eae0ee | 2013-06-05 16:59:15 -0700 | [diff] [blame] | 1688 | if (c.sequence != s.sequence) { |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 1689 | // invalidate and recompute the visible regions if needed |
| 1690 | flags |= eVisibleRegion; |
| 1691 | this->contentDirty = true; |
| 1692 | |
| 1693 | // we may use linear filtering, if the matrix scales us |
Robert Carr | 3dcabfa | 2016-03-01 18:36:58 -0800 | [diff] [blame] | 1694 | const uint8_t type = c.active.transform.getType(); |
| 1695 | mNeedsFiltering = (!c.active.transform.preserveRects() || |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 1696 | (type >= Transform::SCALE)); |
| 1697 | } |
| 1698 | |
Dan Stoza | c814517 | 2016-04-28 16:29:06 -0700 | [diff] [blame] | 1699 | // If the layer is hidden, signal and clear out all local sync points so |
| 1700 | // that transactions for layers depending on this layer's frames becoming |
| 1701 | // visible are not blocked |
| 1702 | if (c.flags & layer_state_t::eLayerHidden) { |
Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 1703 | clearSyncPoints(); |
Dan Stoza | c814517 | 2016-04-28 16:29:06 -0700 | [diff] [blame] | 1704 | } |
| 1705 | |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 1706 | // Commit the transaction |
Pablo Ceballos | 05289c2 | 2016-04-14 15:49:55 -0700 | [diff] [blame] | 1707 | commitTransaction(c); |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 1708 | return flags; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1709 | } |
| 1710 | |
Pablo Ceballos | 05289c2 | 2016-04-14 15:49:55 -0700 | [diff] [blame] | 1711 | void Layer::commitTransaction(const State& stateToCommit) { |
| 1712 | mDrawingState = stateToCommit; |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 1713 | } |
| 1714 | |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 1715 | uint32_t Layer::getTransactionFlags(uint32_t flags) { |
| 1716 | return android_atomic_and(~flags, &mTransactionFlags) & flags; |
| 1717 | } |
| 1718 | |
| 1719 | uint32_t Layer::setTransactionFlags(uint32_t flags) { |
| 1720 | return android_atomic_or(flags, &mTransactionFlags); |
| 1721 | } |
| 1722 | |
Robert Carr | 82364e3 | 2016-05-15 11:27:47 -0700 | [diff] [blame] | 1723 | bool Layer::setPosition(float x, float y, bool immediate) { |
Robert Carr | 3dcabfa | 2016-03-01 18:36:58 -0800 | [diff] [blame] | 1724 | if (mCurrentState.requested.transform.tx() == x && mCurrentState.requested.transform.ty() == y) |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 1725 | return false; |
| 1726 | mCurrentState.sequence++; |
Robert Carr | 69663fb | 2016-03-27 19:59:19 -0700 | [diff] [blame] | 1727 | |
| 1728 | // We update the requested and active position simultaneously because |
| 1729 | // we want to apply the position portion of the transform matrix immediately, |
| 1730 | // but still delay scaling when resizing a SCALING_MODE_FREEZE layer. |
Robert Carr | 3dcabfa | 2016-03-01 18:36:58 -0800 | [diff] [blame] | 1731 | mCurrentState.requested.transform.set(x, y); |
Robert Carr | 7bf247e | 2017-05-18 14:02:49 -0700 | [diff] [blame] | 1732 | if (immediate && !mFreezeGeometryUpdates) { |
| 1733 | // Here we directly update the active state |
| 1734 | // unlike other setters, because we store it within |
| 1735 | // the transform, but use different latching rules. |
| 1736 | // b/38182305 |
Robert Carr | 82364e3 | 2016-05-15 11:27:47 -0700 | [diff] [blame] | 1737 | mCurrentState.active.transform.set(x, y); |
| 1738 | } |
Robert Carr | 7bf247e | 2017-05-18 14:02:49 -0700 | [diff] [blame] | 1739 | mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate; |
Robert Carr | 69663fb | 2016-03-27 19:59:19 -0700 | [diff] [blame] | 1740 | |
Dan Stoza | 7dde599 | 2015-05-22 09:51:44 -0700 | [diff] [blame] | 1741 | mCurrentState.modified = true; |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 1742 | setTransactionFlags(eTransactionNeeded); |
| 1743 | return true; |
| 1744 | } |
Robert Carr | 82364e3 | 2016-05-15 11:27:47 -0700 | [diff] [blame] | 1745 | |
Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 1746 | bool Layer::setChildLayer(const sp<Layer>& childLayer, int32_t z) { |
| 1747 | ssize_t idx = mCurrentChildren.indexOf(childLayer); |
| 1748 | if (idx < 0) { |
| 1749 | return false; |
| 1750 | } |
| 1751 | if (childLayer->setLayer(z)) { |
| 1752 | mCurrentChildren.removeAt(idx); |
| 1753 | mCurrentChildren.add(childLayer); |
| 1754 | } |
| 1755 | return true; |
| 1756 | } |
| 1757 | |
Robert Carr | ae06083 | 2016-11-28 10:51:00 -0800 | [diff] [blame] | 1758 | bool Layer::setLayer(int32_t z) { |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 1759 | if (mCurrentState.z == z) |
| 1760 | return false; |
| 1761 | mCurrentState.sequence++; |
| 1762 | mCurrentState.z = z; |
Dan Stoza | 7dde599 | 2015-05-22 09:51:44 -0700 | [diff] [blame] | 1763 | mCurrentState.modified = true; |
Robert Carr | db66e62 | 2017-04-10 16:55:57 -0700 | [diff] [blame] | 1764 | |
| 1765 | // Discard all relative layering. |
| 1766 | if (mCurrentState.zOrderRelativeOf != nullptr) { |
| 1767 | sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote(); |
| 1768 | if (strongRelative != nullptr) { |
| 1769 | strongRelative->removeZOrderRelative(this); |
| 1770 | } |
| 1771 | mCurrentState.zOrderRelativeOf = nullptr; |
| 1772 | } |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 1773 | setTransactionFlags(eTransactionNeeded); |
| 1774 | return true; |
| 1775 | } |
Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 1776 | |
Robert Carr | db66e62 | 2017-04-10 16:55:57 -0700 | [diff] [blame] | 1777 | void Layer::removeZOrderRelative(const wp<Layer>& relative) { |
| 1778 | mCurrentState.zOrderRelatives.remove(relative); |
| 1779 | mCurrentState.sequence++; |
| 1780 | mCurrentState.modified = true; |
| 1781 | setTransactionFlags(eTransactionNeeded); |
| 1782 | } |
| 1783 | |
| 1784 | void Layer::addZOrderRelative(const wp<Layer>& relative) { |
| 1785 | mCurrentState.zOrderRelatives.add(relative); |
| 1786 | mCurrentState.modified = true; |
| 1787 | mCurrentState.sequence++; |
| 1788 | setTransactionFlags(eTransactionNeeded); |
| 1789 | } |
| 1790 | |
| 1791 | bool Layer::setRelativeLayer(const sp<IBinder>& relativeToHandle, int32_t z) { |
| 1792 | sp<Handle> handle = static_cast<Handle*>(relativeToHandle.get()); |
| 1793 | if (handle == nullptr) { |
| 1794 | return false; |
| 1795 | } |
| 1796 | sp<Layer> relative = handle->owner.promote(); |
| 1797 | if (relative == nullptr) { |
| 1798 | return false; |
| 1799 | } |
| 1800 | |
| 1801 | mCurrentState.sequence++; |
| 1802 | mCurrentState.modified = true; |
| 1803 | mCurrentState.z = z; |
| 1804 | |
| 1805 | mCurrentState.zOrderRelativeOf = relative; |
| 1806 | relative->addZOrderRelative(this); |
| 1807 | |
| 1808 | setTransactionFlags(eTransactionNeeded); |
| 1809 | |
| 1810 | return true; |
| 1811 | } |
| 1812 | |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 1813 | bool Layer::setSize(uint32_t w, uint32_t h) { |
| 1814 | if (mCurrentState.requested.w == w && mCurrentState.requested.h == h) |
| 1815 | return false; |
| 1816 | mCurrentState.requested.w = w; |
| 1817 | mCurrentState.requested.h = h; |
Dan Stoza | 7dde599 | 2015-05-22 09:51:44 -0700 | [diff] [blame] | 1818 | mCurrentState.modified = true; |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 1819 | setTransactionFlags(eTransactionNeeded); |
| 1820 | return true; |
| 1821 | } |
Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 1822 | #ifdef USE_HWC2 |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 1823 | bool Layer::setAlpha(float alpha) { |
Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 1824 | #else |
| 1825 | bool Layer::setAlpha(uint8_t alpha) { |
| 1826 | #endif |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 1827 | if (mCurrentState.alpha == alpha) |
| 1828 | return false; |
| 1829 | mCurrentState.sequence++; |
| 1830 | mCurrentState.alpha = alpha; |
Dan Stoza | 7dde599 | 2015-05-22 09:51:44 -0700 | [diff] [blame] | 1831 | mCurrentState.modified = true; |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 1832 | setTransactionFlags(eTransactionNeeded); |
| 1833 | return true; |
| 1834 | } |
| 1835 | bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix) { |
| 1836 | mCurrentState.sequence++; |
Robert Carr | 3dcabfa | 2016-03-01 18:36:58 -0800 | [diff] [blame] | 1837 | mCurrentState.requested.transform.set( |
Robert Carr | cb6e1e3 | 2017-02-21 19:48:26 -0800 | [diff] [blame] | 1838 | matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy); |
Dan Stoza | 7dde599 | 2015-05-22 09:51:44 -0700 | [diff] [blame] | 1839 | mCurrentState.modified = true; |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 1840 | setTransactionFlags(eTransactionNeeded); |
| 1841 | return true; |
| 1842 | } |
| 1843 | bool Layer::setTransparentRegionHint(const Region& transparent) { |
Mathias Agopian | 2ca7939 | 2013-04-02 18:30:32 -0700 | [diff] [blame] | 1844 | mCurrentState.requestedTransparentRegion = transparent; |
Dan Stoza | 7dde599 | 2015-05-22 09:51:44 -0700 | [diff] [blame] | 1845 | mCurrentState.modified = true; |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 1846 | setTransactionFlags(eTransactionNeeded); |
| 1847 | return true; |
| 1848 | } |
| 1849 | bool Layer::setFlags(uint8_t flags, uint8_t mask) { |
| 1850 | const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask); |
| 1851 | if (mCurrentState.flags == newFlags) |
| 1852 | return false; |
| 1853 | mCurrentState.sequence++; |
| 1854 | mCurrentState.flags = newFlags; |
Dan Stoza | 7dde599 | 2015-05-22 09:51:44 -0700 | [diff] [blame] | 1855 | mCurrentState.mask = mask; |
| 1856 | mCurrentState.modified = true; |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 1857 | setTransactionFlags(eTransactionNeeded); |
| 1858 | return true; |
| 1859 | } |
Robert Carr | 99e27f0 | 2016-06-16 15:18:02 -0700 | [diff] [blame] | 1860 | |
| 1861 | bool Layer::setCrop(const Rect& crop, bool immediate) { |
Robert Carr | 7bf247e | 2017-05-18 14:02:49 -0700 | [diff] [blame] | 1862 | if (mCurrentState.requestedCrop == crop) |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 1863 | return false; |
| 1864 | mCurrentState.sequence++; |
Robert Carr | 99e27f0 | 2016-06-16 15:18:02 -0700 | [diff] [blame] | 1865 | mCurrentState.requestedCrop = crop; |
Robert Carr | 7bf247e | 2017-05-18 14:02:49 -0700 | [diff] [blame] | 1866 | if (immediate && !mFreezeGeometryUpdates) { |
Robert Carr | 99e27f0 | 2016-06-16 15:18:02 -0700 | [diff] [blame] | 1867 | mCurrentState.crop = crop; |
| 1868 | } |
Robert Carr | 7bf247e | 2017-05-18 14:02:49 -0700 | [diff] [blame] | 1869 | mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate; |
| 1870 | |
Dan Stoza | 7dde599 | 2015-05-22 09:51:44 -0700 | [diff] [blame] | 1871 | mCurrentState.modified = true; |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 1872 | setTransactionFlags(eTransactionNeeded); |
| 1873 | return true; |
| 1874 | } |
Robert Carr | 8d5227b | 2017-03-16 15:41:03 -0700 | [diff] [blame] | 1875 | |
| 1876 | bool Layer::setFinalCrop(const Rect& crop, bool immediate) { |
Robert Carr | 7bf247e | 2017-05-18 14:02:49 -0700 | [diff] [blame] | 1877 | if (mCurrentState.requestedFinalCrop == crop) |
Pablo Ceballos | acbe678 | 2016-03-04 17:54:21 +0000 | [diff] [blame] | 1878 | return false; |
| 1879 | mCurrentState.sequence++; |
Robert Carr | 8d5227b | 2017-03-16 15:41:03 -0700 | [diff] [blame] | 1880 | mCurrentState.requestedFinalCrop = crop; |
Robert Carr | 7bf247e | 2017-05-18 14:02:49 -0700 | [diff] [blame] | 1881 | if (immediate && !mFreezeGeometryUpdates) { |
Robert Carr | 8d5227b | 2017-03-16 15:41:03 -0700 | [diff] [blame] | 1882 | mCurrentState.finalCrop = crop; |
| 1883 | } |
Robert Carr | 7bf247e | 2017-05-18 14:02:49 -0700 | [diff] [blame] | 1884 | mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate; |
| 1885 | |
Pablo Ceballos | acbe678 | 2016-03-04 17:54:21 +0000 | [diff] [blame] | 1886 | mCurrentState.modified = true; |
| 1887 | setTransactionFlags(eTransactionNeeded); |
| 1888 | return true; |
| 1889 | } |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 1890 | |
Robert Carr | c3574f7 | 2016-03-24 12:19:32 -0700 | [diff] [blame] | 1891 | bool Layer::setOverrideScalingMode(int32_t scalingMode) { |
| 1892 | if (scalingMode == mOverrideScalingMode) |
| 1893 | return false; |
| 1894 | mOverrideScalingMode = scalingMode; |
Robert Carr | 82364e3 | 2016-05-15 11:27:47 -0700 | [diff] [blame] | 1895 | setTransactionFlags(eTransactionNeeded); |
Robert Carr | c3574f7 | 2016-03-24 12:19:32 -0700 | [diff] [blame] | 1896 | return true; |
| 1897 | } |
| 1898 | |
Daniel Nicoara | 2f5f8a5 | 2016-12-20 16:11:58 -0500 | [diff] [blame] | 1899 | void Layer::setInfo(uint32_t type, uint32_t appId) { |
| 1900 | mCurrentState.appId = appId; |
| 1901 | mCurrentState.type = type; |
| 1902 | mCurrentState.modified = true; |
| 1903 | setTransactionFlags(eTransactionNeeded); |
| 1904 | } |
| 1905 | |
Robert Carr | c3574f7 | 2016-03-24 12:19:32 -0700 | [diff] [blame] | 1906 | uint32_t Layer::getEffectiveScalingMode() const { |
| 1907 | if (mOverrideScalingMode >= 0) { |
| 1908 | return mOverrideScalingMode; |
| 1909 | } |
| 1910 | return mCurrentScalingMode; |
| 1911 | } |
| 1912 | |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 1913 | bool Layer::setLayerStack(uint32_t layerStack) { |
| 1914 | if (mCurrentState.layerStack == layerStack) |
| 1915 | return false; |
| 1916 | mCurrentState.sequence++; |
| 1917 | mCurrentState.layerStack = layerStack; |
Dan Stoza | 7dde599 | 2015-05-22 09:51:44 -0700 | [diff] [blame] | 1918 | mCurrentState.modified = true; |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 1919 | setTransactionFlags(eTransactionNeeded); |
| 1920 | return true; |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 1921 | } |
| 1922 | |
Courtney Goeltzenleuchter | bb09b43 | 2016-11-30 13:51:28 -0700 | [diff] [blame] | 1923 | bool Layer::setDataSpace(android_dataspace dataSpace) { |
| 1924 | if (mCurrentState.dataSpace == dataSpace) |
| 1925 | return false; |
| 1926 | mCurrentState.sequence++; |
| 1927 | mCurrentState.dataSpace = dataSpace; |
| 1928 | mCurrentState.modified = true; |
| 1929 | setTransactionFlags(eTransactionNeeded); |
| 1930 | return true; |
| 1931 | } |
| 1932 | |
Courtney Goeltzenleuchter | 532b263 | 2017-05-05 16:34:38 -0600 | [diff] [blame] | 1933 | android_dataspace Layer::getDataSpace() const { |
| 1934 | return mCurrentState.dataSpace; |
| 1935 | } |
| 1936 | |
Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 1937 | uint32_t Layer::getLayerStack() const { |
Chia-I Wu | e41dbe6 | 2017-06-13 14:10:56 -0700 | [diff] [blame] | 1938 | auto p = mDrawingParent.promote(); |
Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 1939 | if (p == nullptr) { |
| 1940 | return getDrawingState().layerStack; |
| 1941 | } |
| 1942 | return p->getLayerStack(); |
| 1943 | } |
| 1944 | |
Robert Carr | 0d48072 | 2017-01-10 16:42:54 -0800 | [diff] [blame] | 1945 | void Layer::deferTransactionUntil(const sp<Layer>& barrierLayer, |
Dan Stoza | 7dde599 | 2015-05-22 09:51:44 -0700 | [diff] [blame] | 1946 | uint64_t frameNumber) { |
Robert Carr | 0d48072 | 2017-01-10 16:42:54 -0800 | [diff] [blame] | 1947 | mCurrentState.barrierLayer = barrierLayer; |
Dan Stoza | 7dde599 | 2015-05-22 09:51:44 -0700 | [diff] [blame] | 1948 | mCurrentState.frameNumber = frameNumber; |
| 1949 | // We don't set eTransactionNeeded, because just receiving a deferral |
| 1950 | // request without any other state updates shouldn't actually induce a delay |
| 1951 | mCurrentState.modified = true; |
| 1952 | pushPendingState(); |
Robert Carr | 0d48072 | 2017-01-10 16:42:54 -0800 | [diff] [blame] | 1953 | mCurrentState.barrierLayer = nullptr; |
Dan Stoza | 792e529 | 2016-02-11 11:43:58 -0800 | [diff] [blame] | 1954 | mCurrentState.frameNumber = 0; |
Dan Stoza | 7dde599 | 2015-05-22 09:51:44 -0700 | [diff] [blame] | 1955 | mCurrentState.modified = false; |
Robert Carr | 0d48072 | 2017-01-10 16:42:54 -0800 | [diff] [blame] | 1956 | } |
| 1957 | |
| 1958 | void Layer::deferTransactionUntil(const sp<IBinder>& barrierHandle, |
| 1959 | uint64_t frameNumber) { |
| 1960 | sp<Handle> handle = static_cast<Handle*>(barrierHandle.get()); |
| 1961 | deferTransactionUntil(handle->owner.promote(), frameNumber); |
Dan Stoza | 7dde599 | 2015-05-22 09:51:44 -0700 | [diff] [blame] | 1962 | } |
| 1963 | |
Dan Stoza | ee44edd | 2015-03-23 15:50:23 -0700 | [diff] [blame] | 1964 | void Layer::useSurfaceDamage() { |
| 1965 | if (mFlinger->mForceFullDamage) { |
| 1966 | surfaceDamageRegion = Region::INVALID_REGION; |
| 1967 | } else { |
| 1968 | surfaceDamageRegion = mSurfaceFlingerConsumer->getSurfaceDamage(); |
| 1969 | } |
| 1970 | } |
| 1971 | |
| 1972 | void Layer::useEmptyDamage() { |
| 1973 | surfaceDamageRegion.clear(); |
| 1974 | } |
| 1975 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1976 | // ---------------------------------------------------------------------------- |
| 1977 | // pageflip handling... |
| 1978 | // ---------------------------------------------------------------------------- |
| 1979 | |
Dan Stoza | 6b9454d | 2014-11-07 16:00:59 -0800 | [diff] [blame] | 1980 | bool Layer::shouldPresentNow(const DispSync& dispSync) const { |
Pablo Ceballos | ff95aab | 2016-01-13 17:09:58 -0800 | [diff] [blame] | 1981 | if (mSidebandStreamChanged || mAutoRefresh) { |
Dan Stoza | d87defa | 2015-07-29 16:15:50 -0700 | [diff] [blame] | 1982 | return true; |
| 1983 | } |
| 1984 | |
Dan Stoza | 6b9454d | 2014-11-07 16:00:59 -0800 | [diff] [blame] | 1985 | Mutex::Autolock lock(mQueueItemLock); |
Dan Stoza | 0eb2d39 | 2015-07-06 12:56:50 -0700 | [diff] [blame] | 1986 | if (mQueueItems.empty()) { |
| 1987 | return false; |
| 1988 | } |
| 1989 | auto timestamp = mQueueItems[0].mTimestamp; |
Dan Stoza | 6b9454d | 2014-11-07 16:00:59 -0800 | [diff] [blame] | 1990 | nsecs_t expectedPresent = |
| 1991 | mSurfaceFlingerConsumer->computeExpectedPresent(dispSync); |
Dan Stoza | 0eb2d39 | 2015-07-06 12:56:50 -0700 | [diff] [blame] | 1992 | |
| 1993 | // Ignore timestamps more than a second in the future |
| 1994 | bool isPlausible = timestamp < (expectedPresent + s2ns(1)); |
| 1995 | ALOGW_IF(!isPlausible, "[%s] Timestamp %" PRId64 " seems implausible " |
| 1996 | "relative to expectedPresent %" PRId64, mName.string(), timestamp, |
| 1997 | expectedPresent); |
| 1998 | |
| 1999 | bool isDue = timestamp < expectedPresent; |
| 2000 | return isDue || !isPlausible; |
Dan Stoza | 6b9454d | 2014-11-07 16:00:59 -0800 | [diff] [blame] | 2001 | } |
| 2002 | |
Brian Anderson | d6927fb | 2016-07-23 23:37:30 -0700 | [diff] [blame] | 2003 | bool Layer::onPreComposition(nsecs_t refreshStartTime) { |
| 2004 | if (mBufferLatched) { |
| 2005 | Mutex::Autolock lock(mFrameEventHistoryMutex); |
| 2006 | mFrameEventHistory.addPreComposition(mCurrentFrameNumber, refreshStartTime); |
| 2007 | } |
Mathias Agopian | 4d143ee | 2012-02-23 20:05:39 -0800 | [diff] [blame] | 2008 | mRefreshPending = false; |
Pablo Ceballos | ff95aab | 2016-01-13 17:09:58 -0800 | [diff] [blame] | 2009 | return mQueuedFrames > 0 || mSidebandStreamChanged || mAutoRefresh; |
Mathias Agopian | 99ce5cd | 2012-01-31 18:24:27 -0800 | [diff] [blame] | 2010 | } |
| 2011 | |
Brian Anderson | 0a61b0c | 2016-12-07 14:55:56 -0800 | [diff] [blame] | 2012 | bool Layer::onPostComposition(const std::shared_ptr<FenceTime>& glDoneFence, |
Brian Anderson | 3d4039d | 2016-09-23 16:31:30 -0700 | [diff] [blame] | 2013 | const std::shared_ptr<FenceTime>& presentFence, |
Brian Anderson | 0a61b0c | 2016-12-07 14:55:56 -0800 | [diff] [blame] | 2014 | const CompositorTiming& compositorTiming) { |
Brian Anderson | d6927fb | 2016-07-23 23:37:30 -0700 | [diff] [blame] | 2015 | // mFrameLatencyNeeded is true when a new frame was latched for the |
| 2016 | // composition. |
Fabien Sanglard | 28e9808 | 2016-12-05 10:19:46 -0800 | [diff] [blame] | 2017 | if (!mFrameLatencyNeeded) |
| 2018 | return false; |
| 2019 | |
Fabien Sanglard | 28e9808 | 2016-12-05 10:19:46 -0800 | [diff] [blame] | 2020 | // Update mFrameEventHistory. |
| 2021 | { |
| 2022 | Mutex::Autolock lock(mFrameEventHistoryMutex); |
| 2023 | mFrameEventHistory.addPostComposition(mCurrentFrameNumber, |
Brian Anderson | 0a61b0c | 2016-12-07 14:55:56 -0800 | [diff] [blame] | 2024 | glDoneFence, presentFence, compositorTiming); |
Mathias Agopian | d3ee231 | 2012-08-02 14:01:42 -0700 | [diff] [blame] | 2025 | } |
Fabien Sanglard | 28e9808 | 2016-12-05 10:19:46 -0800 | [diff] [blame] | 2026 | |
| 2027 | // Update mFrameTracker. |
| 2028 | nsecs_t desiredPresentTime = mSurfaceFlingerConsumer->getTimestamp(); |
| 2029 | mFrameTracker.setDesiredPresentTime(desiredPresentTime); |
| 2030 | |
Brian Anderson | 3d4039d | 2016-09-23 16:31:30 -0700 | [diff] [blame] | 2031 | std::shared_ptr<FenceTime> frameReadyFence = |
| 2032 | mSurfaceFlingerConsumer->getCurrentFenceTime(); |
Fabien Sanglard | 28e9808 | 2016-12-05 10:19:46 -0800 | [diff] [blame] | 2033 | if (frameReadyFence->isValid()) { |
Brian Anderson | 3d4039d | 2016-09-23 16:31:30 -0700 | [diff] [blame] | 2034 | mFrameTracker.setFrameReadyFence(std::move(frameReadyFence)); |
Fabien Sanglard | 28e9808 | 2016-12-05 10:19:46 -0800 | [diff] [blame] | 2035 | } else { |
| 2036 | // There was no fence for this frame, so assume that it was ready |
| 2037 | // to be presented at the desired present time. |
| 2038 | mFrameTracker.setFrameReadyTime(desiredPresentTime); |
| 2039 | } |
| 2040 | |
Brian Anderson | 3d4039d | 2016-09-23 16:31:30 -0700 | [diff] [blame] | 2041 | if (presentFence->isValid()) { |
| 2042 | mFrameTracker.setActualPresentFence( |
| 2043 | std::shared_ptr<FenceTime>(presentFence)); |
Fabien Sanglard | 28e9808 | 2016-12-05 10:19:46 -0800 | [diff] [blame] | 2044 | } else { |
| 2045 | // The HWC doesn't support present fences, so use the refresh |
| 2046 | // timestamp instead. |
Brian Anderson | 3d4039d | 2016-09-23 16:31:30 -0700 | [diff] [blame] | 2047 | mFrameTracker.setActualPresentTime( |
| 2048 | mFlinger->getHwComposer().getRefreshTimestamp( |
| 2049 | HWC_DISPLAY_PRIMARY)); |
Fabien Sanglard | 28e9808 | 2016-12-05 10:19:46 -0800 | [diff] [blame] | 2050 | } |
| 2051 | |
| 2052 | mFrameTracker.advanceFrame(); |
| 2053 | mFrameLatencyNeeded = false; |
| 2054 | return true; |
Mathias Agopian | d3ee231 | 2012-08-02 14:01:42 -0700 | [diff] [blame] | 2055 | } |
| 2056 | |
Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 2057 | #ifdef USE_HWC2 |
Brian Anderson | f638686 | 2016-10-31 16:34:13 -0700 | [diff] [blame] | 2058 | void Layer::releasePendingBuffer(nsecs_t dequeueReadyTime) { |
Brian Anderson | 5ea5e59 | 2016-12-01 16:54:33 -0800 | [diff] [blame] | 2059 | if (!mSurfaceFlingerConsumer->releasePendingBuffer()) { |
| 2060 | return; |
| 2061 | } |
| 2062 | |
Brian Anderson | 3d4039d | 2016-09-23 16:31:30 -0700 | [diff] [blame] | 2063 | auto releaseFenceTime = std::make_shared<FenceTime>( |
Brian Anderson | d6927fb | 2016-07-23 23:37:30 -0700 | [diff] [blame] | 2064 | mSurfaceFlingerConsumer->getPrevFinalReleaseFence()); |
Brian Anderson | fbc80ae | 2017-05-26 16:23:54 -0700 | [diff] [blame] | 2065 | mReleaseTimeline.updateSignalTimes(); |
Brian Anderson | 3d4039d | 2016-09-23 16:31:30 -0700 | [diff] [blame] | 2066 | mReleaseTimeline.push(releaseFenceTime); |
| 2067 | |
| 2068 | Mutex::Autolock lock(mFrameEventHistoryMutex); |
Brian Anderson | 8cc8b10 | 2016-10-21 12:43:09 -0700 | [diff] [blame] | 2069 | if (mPreviousFrameNumber != 0) { |
| 2070 | mFrameEventHistory.addRelease(mPreviousFrameNumber, |
| 2071 | dequeueReadyTime, std::move(releaseFenceTime)); |
| 2072 | } |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 2073 | } |
Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 2074 | #endif |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 2075 | |
Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 2076 | bool Layer::isHiddenByPolicy() const { |
| 2077 | const Layer::State& s(mDrawingState); |
Chia-I Wu | e41dbe6 | 2017-06-13 14:10:56 -0700 | [diff] [blame] | 2078 | const auto& parent = mDrawingParent.promote(); |
Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 2079 | if (parent != nullptr && parent->isHiddenByPolicy()) { |
| 2080 | return true; |
| 2081 | } |
| 2082 | return s.flags & layer_state_t::eLayerHidden; |
| 2083 | } |
| 2084 | |
Mathias Agopian | da27af9 | 2012-09-13 18:17:13 -0700 | [diff] [blame] | 2085 | bool Layer::isVisible() const { |
Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 2086 | #ifdef USE_HWC2 |
Robert Carr | 6452f12 | 2017-03-21 10:41:29 -0700 | [diff] [blame] | 2087 | return !(isHiddenByPolicy()) && getAlpha() > 0.0f |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 2088 | && (mActiveBuffer != NULL || mSidebandStream != NULL); |
Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 2089 | #else |
Robert Carr | 6452f12 | 2017-03-21 10:41:29 -0700 | [diff] [blame] | 2090 | return !(isHiddenByPolicy()) && getAlpha() |
Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 2091 | && (mActiveBuffer != NULL || mSidebandStream != NULL); |
| 2092 | #endif |
Mathias Agopian | da27af9 | 2012-09-13 18:17:13 -0700 | [diff] [blame] | 2093 | } |
| 2094 | |
Fabien Sanglard | cd6fd54 | 2016-10-13 12:47:39 -0700 | [diff] [blame] | 2095 | bool Layer::allTransactionsSignaled() { |
| 2096 | auto headFrameNumber = getHeadFrameNumber(); |
| 2097 | bool matchingFramesFound = false; |
| 2098 | bool allTransactionsApplied = true; |
| 2099 | Mutex::Autolock lock(mLocalSyncPointMutex); |
| 2100 | |
| 2101 | for (auto& point : mLocalSyncPoints) { |
| 2102 | if (point->getFrameNumber() > headFrameNumber) { |
| 2103 | break; |
| 2104 | } |
| 2105 | matchingFramesFound = true; |
| 2106 | |
| 2107 | if (!point->frameIsAvailable()) { |
| 2108 | // We haven't notified the remote layer that the frame for |
| 2109 | // this point is available yet. Notify it now, and then |
| 2110 | // abort this attempt to latch. |
| 2111 | point->setFrameAvailable(); |
| 2112 | allTransactionsApplied = false; |
| 2113 | break; |
| 2114 | } |
| 2115 | |
| 2116 | allTransactionsApplied = allTransactionsApplied && point->transactionIsApplied(); |
| 2117 | } |
| 2118 | return !matchingFramesFound || allTransactionsApplied; |
| 2119 | } |
| 2120 | |
Brian Anderson | d6927fb | 2016-07-23 23:37:30 -0700 | [diff] [blame] | 2121 | Region Layer::latchBuffer(bool& recomputeVisibleRegions, nsecs_t latchTime) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2122 | { |
Jamie Gennis | 1c8e95c | 2012-02-23 19:27:23 -0800 | [diff] [blame] | 2123 | ATRACE_CALL(); |
| 2124 | |
Jesse Hall | 399184a | 2014-03-03 15:42:54 -0800 | [diff] [blame] | 2125 | if (android_atomic_acquire_cas(true, false, &mSidebandStreamChanged) == 0) { |
| 2126 | // mSidebandStreamChanged was true |
| 2127 | mSidebandStream = mSurfaceFlingerConsumer->getSidebandStream(); |
Dan Stoza | 12e0a27 | 2015-05-05 14:00:52 -0700 | [diff] [blame] | 2128 | if (mSidebandStream != NULL) { |
| 2129 | setTransactionFlags(eTransactionNeeded); |
| 2130 | mFlinger->setTransactionFlags(eTraversalNeeded); |
| 2131 | } |
Jesse Hall | 5bf786d | 2014-09-30 10:35:11 -0700 | [diff] [blame] | 2132 | recomputeVisibleRegions = true; |
| 2133 | |
| 2134 | const State& s(getDrawingState()); |
Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 2135 | return getTransform().transform(Region(Rect(s.active.w, s.active.h))); |
Jesse Hall | 399184a | 2014-03-03 15:42:54 -0800 | [diff] [blame] | 2136 | } |
| 2137 | |
Mathias Agopian | 4fec873 | 2012-06-29 14:12:52 -0700 | [diff] [blame] | 2138 | Region outDirtyRegion; |
Fabien Sanglard | 223eb91 | 2016-10-13 10:15:06 -0700 | [diff] [blame] | 2139 | if (mQueuedFrames <= 0 && !mAutoRefresh) { |
| 2140 | return outDirtyRegion; |
| 2141 | } |
Mathias Agopian | 99ce5cd | 2012-01-31 18:24:27 -0800 | [diff] [blame] | 2142 | |
Fabien Sanglard | 223eb91 | 2016-10-13 10:15:06 -0700 | [diff] [blame] | 2143 | // if we've already called updateTexImage() without going through |
| 2144 | // a composition step, we have to skip this layer at this point |
| 2145 | // because we cannot call updateTeximage() without a corresponding |
| 2146 | // compositionComplete() call. |
| 2147 | // we'll trigger an update in onPreComposition(). |
| 2148 | if (mRefreshPending) { |
| 2149 | return outDirtyRegion; |
| 2150 | } |
| 2151 | |
| 2152 | // If the head buffer's acquire fence hasn't signaled yet, return and |
| 2153 | // try again later |
| 2154 | if (!headFenceHasSignaled()) { |
| 2155 | mFlinger->signalLayerUpdate(); |
| 2156 | return outDirtyRegion; |
| 2157 | } |
| 2158 | |
| 2159 | // Capture the old state of the layer for comparisons later |
| 2160 | const State& s(getDrawingState()); |
| 2161 | const bool oldOpacity = isOpaque(s); |
| 2162 | sp<GraphicBuffer> oldActiveBuffer = mActiveBuffer; |
| 2163 | |
Fabien Sanglard | cd6fd54 | 2016-10-13 12:47:39 -0700 | [diff] [blame] | 2164 | if (!allTransactionsSignaled()) { |
Fabien Sanglard | 223eb91 | 2016-10-13 10:15:06 -0700 | [diff] [blame] | 2165 | mFlinger->signalLayerUpdate(); |
| 2166 | return outDirtyRegion; |
| 2167 | } |
| 2168 | |
| 2169 | // This boolean is used to make sure that SurfaceFlinger's shadow copy |
| 2170 | // of the buffer queue isn't modified when the buffer queue is returning |
| 2171 | // BufferItem's that weren't actually queued. This can happen in shared |
| 2172 | // buffer mode. |
| 2173 | bool queuedBuffer = false; |
Fabien Sanglard | 7b1563a | 2016-10-13 12:05:28 -0700 | [diff] [blame] | 2174 | LayerRejecter r(mDrawingState, getCurrentState(), recomputeVisibleRegions, |
| 2175 | getProducerStickyTransform() != 0, mName.string(), |
Robert Carr | 7bf247e | 2017-05-18 14:02:49 -0700 | [diff] [blame] | 2176 | mOverrideScalingMode, mFreezeGeometryUpdates); |
Fabien Sanglard | 223eb91 | 2016-10-13 10:15:06 -0700 | [diff] [blame] | 2177 | status_t updateResult = mSurfaceFlingerConsumer->updateTexImage(&r, |
| 2178 | mFlinger->mPrimaryDispSync, &mAutoRefresh, &queuedBuffer, |
| 2179 | mLastFrameNumberReceived); |
| 2180 | if (updateResult == BufferQueue::PRESENT_LATER) { |
| 2181 | // Producer doesn't want buffer to be displayed yet. Signal a |
| 2182 | // layer update so we check again at the next opportunity. |
| 2183 | mFlinger->signalLayerUpdate(); |
| 2184 | return outDirtyRegion; |
| 2185 | } else if (updateResult == SurfaceFlingerConsumer::BUFFER_REJECTED) { |
| 2186 | // If the buffer has been rejected, remove it from the shadow queue |
| 2187 | // and return early |
| 2188 | if (queuedBuffer) { |
| 2189 | Mutex::Autolock lock(mQueueItemLock); |
| 2190 | mQueueItems.removeAt(0); |
| 2191 | android_atomic_dec(&mQueuedFrames); |
| 2192 | } |
| 2193 | return outDirtyRegion; |
| 2194 | } else if (updateResult != NO_ERROR || mUpdateTexImageFailed) { |
| 2195 | // This can occur if something goes wrong when trying to create the |
| 2196 | // EGLImage for this buffer. If this happens, the buffer has already |
| 2197 | // been released, so we need to clean up the queue and bug out |
| 2198 | // early. |
| 2199 | if (queuedBuffer) { |
| 2200 | Mutex::Autolock lock(mQueueItemLock); |
| 2201 | mQueueItems.clear(); |
| 2202 | android_atomic_and(0, &mQueuedFrames); |
Mathias Agopian | 702634a | 2012-05-23 17:50:31 -0700 | [diff] [blame] | 2203 | } |
| 2204 | |
Fabien Sanglard | 223eb91 | 2016-10-13 10:15:06 -0700 | [diff] [blame] | 2205 | // Once we have hit this state, the shadow queue may no longer |
| 2206 | // correctly reflect the incoming BufferQueue's contents, so even if |
| 2207 | // updateTexImage starts working, the only safe course of action is |
| 2208 | // to continue to ignore updates. |
| 2209 | mUpdateTexImageFailed = true; |
| 2210 | |
| 2211 | return outDirtyRegion; |
| 2212 | } |
| 2213 | |
| 2214 | if (queuedBuffer) { |
| 2215 | // Autolock scope |
| 2216 | auto currentFrameNumber = mSurfaceFlingerConsumer->getFrameNumber(); |
| 2217 | |
| 2218 | Mutex::Autolock lock(mQueueItemLock); |
| 2219 | |
| 2220 | // Remove any stale buffers that have been dropped during |
| 2221 | // updateTexImage |
| 2222 | while (mQueueItems[0].mFrameNumber != currentFrameNumber) { |
| 2223 | mQueueItems.removeAt(0); |
| 2224 | android_atomic_dec(&mQueuedFrames); |
| 2225 | } |
| 2226 | |
| 2227 | mQueueItems.removeAt(0); |
| 2228 | } |
| 2229 | |
| 2230 | |
| 2231 | // Decrement the queued-frames count. Signal another event if we |
| 2232 | // have more frames pending. |
| 2233 | if ((queuedBuffer && android_atomic_dec(&mQueuedFrames) > 1) |
| 2234 | || mAutoRefresh) { |
| 2235 | mFlinger->signalLayerUpdate(); |
| 2236 | } |
| 2237 | |
Fabien Sanglard | 223eb91 | 2016-10-13 10:15:06 -0700 | [diff] [blame] | 2238 | // update the active buffer |
Chia-I Wu | 06d63de | 2017-01-04 14:58:51 +0800 | [diff] [blame] | 2239 | mActiveBuffer = mSurfaceFlingerConsumer->getCurrentBuffer( |
| 2240 | &mActiveBufferSlot); |
Fabien Sanglard | 223eb91 | 2016-10-13 10:15:06 -0700 | [diff] [blame] | 2241 | if (mActiveBuffer == NULL) { |
| 2242 | // this can only happen if the very first buffer was rejected. |
| 2243 | return outDirtyRegion; |
| 2244 | } |
| 2245 | |
Brian Anderson | d6927fb | 2016-07-23 23:37:30 -0700 | [diff] [blame] | 2246 | mBufferLatched = true; |
| 2247 | mPreviousFrameNumber = mCurrentFrameNumber; |
| 2248 | mCurrentFrameNumber = mSurfaceFlingerConsumer->getFrameNumber(); |
| 2249 | |
| 2250 | { |
| 2251 | Mutex::Autolock lock(mFrameEventHistoryMutex); |
| 2252 | mFrameEventHistory.addLatch(mCurrentFrameNumber, latchTime); |
| 2253 | #ifndef USE_HWC2 |
Brian Anderson | 3d4039d | 2016-09-23 16:31:30 -0700 | [diff] [blame] | 2254 | auto releaseFenceTime = std::make_shared<FenceTime>( |
Brian Anderson | d6927fb | 2016-07-23 23:37:30 -0700 | [diff] [blame] | 2255 | mSurfaceFlingerConsumer->getPrevFinalReleaseFence()); |
Brian Anderson | fbc80ae | 2017-05-26 16:23:54 -0700 | [diff] [blame] | 2256 | mReleaseTimeline.updateSignalTimes(); |
Brian Anderson | 3d4039d | 2016-09-23 16:31:30 -0700 | [diff] [blame] | 2257 | mReleaseTimeline.push(releaseFenceTime); |
Brian Anderson | 8cc8b10 | 2016-10-21 12:43:09 -0700 | [diff] [blame] | 2258 | if (mPreviousFrameNumber != 0) { |
| 2259 | mFrameEventHistory.addRelease(mPreviousFrameNumber, |
| 2260 | latchTime, std::move(releaseFenceTime)); |
| 2261 | } |
Brian Anderson | d6927fb | 2016-07-23 23:37:30 -0700 | [diff] [blame] | 2262 | #endif |
| 2263 | } |
| 2264 | |
Fabien Sanglard | 223eb91 | 2016-10-13 10:15:06 -0700 | [diff] [blame] | 2265 | mRefreshPending = true; |
| 2266 | mFrameLatencyNeeded = true; |
| 2267 | if (oldActiveBuffer == NULL) { |
| 2268 | // the first time we receive a buffer, we need to trigger a |
| 2269 | // geometry invalidation. |
| 2270 | recomputeVisibleRegions = true; |
| 2271 | } |
| 2272 | |
Courtney Goeltzenleuchter | bb09b43 | 2016-11-30 13:51:28 -0700 | [diff] [blame] | 2273 | setDataSpace(mSurfaceFlingerConsumer->getCurrentDataSpace()); |
| 2274 | |
Fabien Sanglard | 223eb91 | 2016-10-13 10:15:06 -0700 | [diff] [blame] | 2275 | Rect crop(mSurfaceFlingerConsumer->getCurrentCrop()); |
| 2276 | const uint32_t transform(mSurfaceFlingerConsumer->getCurrentTransform()); |
| 2277 | const uint32_t scalingMode(mSurfaceFlingerConsumer->getCurrentScalingMode()); |
| 2278 | if ((crop != mCurrentCrop) || |
| 2279 | (transform != mCurrentTransform) || |
| 2280 | (scalingMode != mCurrentScalingMode)) |
| 2281 | { |
| 2282 | mCurrentCrop = crop; |
| 2283 | mCurrentTransform = transform; |
| 2284 | mCurrentScalingMode = scalingMode; |
| 2285 | recomputeVisibleRegions = true; |
| 2286 | } |
| 2287 | |
| 2288 | if (oldActiveBuffer != NULL) { |
| 2289 | uint32_t bufWidth = mActiveBuffer->getWidth(); |
| 2290 | uint32_t bufHeight = mActiveBuffer->getHeight(); |
| 2291 | if (bufWidth != uint32_t(oldActiveBuffer->width) || |
| 2292 | bufHeight != uint32_t(oldActiveBuffer->height)) { |
Mathias Agopian | 702634a | 2012-05-23 17:50:31 -0700 | [diff] [blame] | 2293 | recomputeVisibleRegions = true; |
| 2294 | } |
Fabien Sanglard | 223eb91 | 2016-10-13 10:15:06 -0700 | [diff] [blame] | 2295 | } |
Mathias Agopian | 702634a | 2012-05-23 17:50:31 -0700 | [diff] [blame] | 2296 | |
Fabien Sanglard | 223eb91 | 2016-10-13 10:15:06 -0700 | [diff] [blame] | 2297 | mCurrentOpacity = getOpacityForFormat(mActiveBuffer->format); |
| 2298 | if (oldOpacity != isOpaque(s)) { |
| 2299 | recomputeVisibleRegions = true; |
| 2300 | } |
Dan Stoza | cac3538 | 2016-01-27 12:21:06 -0800 | [diff] [blame] | 2301 | |
Fabien Sanglard | 223eb91 | 2016-10-13 10:15:06 -0700 | [diff] [blame] | 2302 | // Remove any sync points corresponding to the buffer which was just |
| 2303 | // latched |
| 2304 | { |
| 2305 | Mutex::Autolock lock(mLocalSyncPointMutex); |
| 2306 | auto point = mLocalSyncPoints.begin(); |
| 2307 | while (point != mLocalSyncPoints.end()) { |
| 2308 | if (!(*point)->frameIsAvailable() || |
| 2309 | !(*point)->transactionIsApplied()) { |
| 2310 | // This sync point must have been added since we started |
| 2311 | // latching. Don't drop it yet. |
| 2312 | ++point; |
| 2313 | continue; |
| 2314 | } |
| 2315 | |
| 2316 | if ((*point)->getFrameNumber() <= mCurrentFrameNumber) { |
| 2317 | point = mLocalSyncPoints.erase(point); |
| 2318 | } else { |
| 2319 | ++point; |
Dan Stoza | cac3538 | 2016-01-27 12:21:06 -0800 | [diff] [blame] | 2320 | } |
| 2321 | } |
Mathias Agopian | caa600c | 2009-09-16 18:27:24 -0700 | [diff] [blame] | 2322 | } |
Fabien Sanglard | 223eb91 | 2016-10-13 10:15:06 -0700 | [diff] [blame] | 2323 | |
| 2324 | // FIXME: postedRegion should be dirty & bounds |
| 2325 | Region dirtyRegion(Rect(s.active.w, s.active.h)); |
| 2326 | |
| 2327 | // transform the dirty region to window-manager space |
Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 2328 | outDirtyRegion = (getTransform().transform(dirtyRegion)); |
Fabien Sanglard | 223eb91 | 2016-10-13 10:15:06 -0700 | [diff] [blame] | 2329 | |
Mathias Agopian | 4fec873 | 2012-06-29 14:12:52 -0700 | [diff] [blame] | 2330 | return outDirtyRegion; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2331 | } |
| 2332 | |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 2333 | uint32_t Layer::getEffectiveUsage(uint32_t usage) const |
Mathias Agopian | b7e930d | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 2334 | { |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 2335 | // TODO: should we do something special if mSecure is set? |
| 2336 | if (mProtectedByApp) { |
| 2337 | // need a hardware-protected path to external video sink |
| 2338 | usage |= GraphicBuffer::USAGE_PROTECTED; |
Jamie Gennis | 54cc83e | 2010-11-02 11:51:32 -0700 | [diff] [blame] | 2339 | } |
Riley Andrews | 03414a1 | 2014-07-01 14:22:59 -0700 | [diff] [blame] | 2340 | if (mPotentialCursor) { |
| 2341 | usage |= GraphicBuffer::USAGE_CURSOR; |
| 2342 | } |
Jamie Gennis | 3599bf2 | 2011-08-10 11:48:07 -0700 | [diff] [blame] | 2343 | usage |= GraphicBuffer::USAGE_HW_COMPOSER; |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 2344 | return usage; |
Mathias Agopian | b5b7f26 | 2010-05-07 15:58:44 -0700 | [diff] [blame] | 2345 | } |
| 2346 | |
Mathias Agopian | 8430095 | 2012-11-21 16:02:13 -0800 | [diff] [blame] | 2347 | void Layer::updateTransformHint(const sp<const DisplayDevice>& hw) const { |
Mathias Agopian | a458364 | 2011-08-23 18:03:18 -0700 | [diff] [blame] | 2348 | uint32_t orientation = 0; |
| 2349 | if (!mFlinger->mDebugDisableTransformHint) { |
Mathias Agopian | 8430095 | 2012-11-21 16:02:13 -0800 | [diff] [blame] | 2350 | // The transform hint is used to improve performance, but we can |
| 2351 | // only have a single transform hint, it cannot |
Mathias Agopian | 4fec873 | 2012-06-29 14:12:52 -0700 | [diff] [blame] | 2352 | // apply to all displays. |
Mathias Agopian | 4297734 | 2012-08-05 00:40:46 -0700 | [diff] [blame] | 2353 | const Transform& planeTransform(hw->getTransform()); |
Mathias Agopian | 4fec873 | 2012-06-29 14:12:52 -0700 | [diff] [blame] | 2354 | orientation = planeTransform.getOrientation(); |
Mathias Agopian | a458364 | 2011-08-23 18:03:18 -0700 | [diff] [blame] | 2355 | if (orientation & Transform::ROT_INVALID) { |
| 2356 | orientation = 0; |
| 2357 | } |
| 2358 | } |
Andy McFadden | bf974ab | 2012-12-04 16:51:15 -0800 | [diff] [blame] | 2359 | mSurfaceFlingerConsumer->setTransformHint(orientation); |
Mathias Agopian | a458364 | 2011-08-23 18:03:18 -0700 | [diff] [blame] | 2360 | } |
| 2361 | |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 2362 | // ---------------------------------------------------------------------------- |
| 2363 | // debugging |
| 2364 | // ---------------------------------------------------------------------------- |
| 2365 | |
Mathias Agopian | 3e25fd8 | 2013-04-22 17:52:16 +0200 | [diff] [blame] | 2366 | void Layer::dump(String8& result, Colorizer& colorizer) const |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 2367 | { |
Mathias Agopian | 1eae0ee | 2013-06-05 16:59:15 -0700 | [diff] [blame] | 2368 | const Layer::State& s(getDrawingState()); |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 2369 | |
Mathias Agopian | 3e25fd8 | 2013-04-22 17:52:16 +0200 | [diff] [blame] | 2370 | colorizer.colorize(result, Colorizer::GREEN); |
Mathias Agopian | 74d211a | 2013-04-22 16:55:35 +0200 | [diff] [blame] | 2371 | result.appendFormat( |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 2372 | "+ %s %p (%s)\n", |
| 2373 | getTypeId(), this, getName().string()); |
Mathias Agopian | 3e25fd8 | 2013-04-22 17:52:16 +0200 | [diff] [blame] | 2374 | colorizer.reset(result); |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 2375 | |
Mathias Agopian | 2ca7939 | 2013-04-02 18:30:32 -0700 | [diff] [blame] | 2376 | s.activeTransparentRegion.dump(result, "transparentRegion"); |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 2377 | visibleRegion.dump(result, "visibleRegion"); |
Dan Stoza | ee44edd | 2015-03-23 15:50:23 -0700 | [diff] [blame] | 2378 | surfaceDamageRegion.dump(result, "surfaceDamageRegion"); |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 2379 | sp<Client> client(mClientRef.promote()); |
Courtney Goeltzenleuchter | 36c44dc | 2017-04-14 09:33:16 -0600 | [diff] [blame] | 2380 | PixelFormat pf = PIXEL_FORMAT_UNKNOWN; |
| 2381 | const sp<GraphicBuffer>& buffer(getActiveBuffer()); |
| 2382 | if (buffer != NULL) { |
| 2383 | pf = buffer->getPixelFormat(); |
| 2384 | } |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 2385 | |
Mathias Agopian | 74d211a | 2013-04-22 16:55:35 +0200 | [diff] [blame] | 2386 | result.appendFormat( " " |
Pablo Ceballos | acbe678 | 2016-03-04 17:54:21 +0000 | [diff] [blame] | 2387 | "layerStack=%4d, z=%9d, pos=(%g,%g), size=(%4d,%4d), " |
| 2388 | "crop=(%4d,%4d,%4d,%4d), finalCrop=(%4d,%4d,%4d,%4d), " |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 2389 | "isOpaque=%1d, invalidate=%1d, " |
Courtney Goeltzenleuchter | 36c44dc | 2017-04-14 09:33:16 -0600 | [diff] [blame] | 2390 | "dataspace=%s, pixelformat=%s " |
Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 2391 | #ifdef USE_HWC2 |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 2392 | "alpha=%.3f, flags=0x%08x, tr=[%.2f, %.2f][%.2f, %.2f]\n" |
Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 2393 | #else |
| 2394 | "alpha=0x%02x, flags=0x%08x, tr=[%.2f, %.2f][%.2f, %.2f]\n" |
| 2395 | #endif |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 2396 | " client=%p\n", |
Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 2397 | getLayerStack(), s.z, |
| 2398 | s.active.transform.tx(), s.active.transform.ty(), |
| 2399 | s.active.w, s.active.h, |
Robert Carr | b5d3d26 | 2016-03-25 15:08:13 -0700 | [diff] [blame] | 2400 | s.crop.left, s.crop.top, |
| 2401 | s.crop.right, s.crop.bottom, |
| 2402 | s.finalCrop.left, s.finalCrop.top, |
| 2403 | s.finalCrop.right, s.finalCrop.bottom, |
Andy McFadden | 4125a4f | 2014-01-29 17:17:11 -0800 | [diff] [blame] | 2404 | isOpaque(s), contentDirty, |
Courtney Goeltzenleuchter | 36c44dc | 2017-04-14 09:33:16 -0600 | [diff] [blame] | 2405 | dataspaceDetails(getDataSpace()).c_str(), decodePixelFormat(pf).c_str(), |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 2406 | s.alpha, s.flags, |
Robert Carr | 3dcabfa | 2016-03-01 18:36:58 -0800 | [diff] [blame] | 2407 | s.active.transform[0][0], s.active.transform[0][1], |
| 2408 | s.active.transform[1][0], s.active.transform[1][1], |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 2409 | client.get()); |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 2410 | |
| 2411 | sp<const GraphicBuffer> buf0(mActiveBuffer); |
| 2412 | uint32_t w0=0, h0=0, s0=0, f0=0; |
| 2413 | if (buf0 != 0) { |
| 2414 | w0 = buf0->getWidth(); |
| 2415 | h0 = buf0->getHeight(); |
| 2416 | s0 = buf0->getStride(); |
| 2417 | f0 = buf0->format; |
| 2418 | } |
Mathias Agopian | 74d211a | 2013-04-22 16:55:35 +0200 | [diff] [blame] | 2419 | result.appendFormat( |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 2420 | " " |
| 2421 | "format=%2d, activeBuffer=[%4ux%4u:%4u,%3X]," |
| 2422 | " queued-frames=%d, mRefreshPending=%d\n", |
| 2423 | mFormat, w0, h0, s0,f0, |
| 2424 | mQueuedFrames, mRefreshPending); |
| 2425 | |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 2426 | if (mSurfaceFlingerConsumer != 0) { |
Colin Cross | 3d1d280 | 2016-09-26 18:10:16 -0700 | [diff] [blame] | 2427 | mSurfaceFlingerConsumer->dumpState(result, " "); |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 2428 | } |
| 2429 | } |
| 2430 | |
Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 2431 | #ifdef USE_HWC2 |
Dan Stoza | e22aec7 | 2016-08-01 13:20:59 -0700 | [diff] [blame] | 2432 | void Layer::miniDumpHeader(String8& result) { |
| 2433 | result.append("----------------------------------------"); |
| 2434 | result.append("---------------------------------------\n"); |
| 2435 | result.append(" Layer name\n"); |
| 2436 | result.append(" Z | "); |
| 2437 | result.append(" Comp Type | "); |
| 2438 | result.append(" Disp Frame (LTRB) | "); |
| 2439 | result.append(" Source Crop (LTRB)\n"); |
| 2440 | result.append("----------------------------------------"); |
| 2441 | result.append("---------------------------------------\n"); |
| 2442 | } |
| 2443 | |
| 2444 | void Layer::miniDump(String8& result, int32_t hwcId) const { |
| 2445 | if (mHwcLayers.count(hwcId) == 0) { |
| 2446 | return; |
| 2447 | } |
| 2448 | |
| 2449 | String8 name; |
| 2450 | if (mName.length() > 77) { |
| 2451 | std::string shortened; |
| 2452 | shortened.append(mName.string(), 36); |
| 2453 | shortened.append("[...]"); |
| 2454 | shortened.append(mName.string() + (mName.length() - 36), 36); |
| 2455 | name = shortened.c_str(); |
| 2456 | } else { |
| 2457 | name = mName; |
| 2458 | } |
| 2459 | |
| 2460 | result.appendFormat(" %s\n", name.string()); |
| 2461 | |
| 2462 | const Layer::State& layerState(getDrawingState()); |
| 2463 | const HWCInfo& hwcInfo = mHwcLayers.at(hwcId); |
| 2464 | result.appendFormat(" %10u | ", layerState.z); |
| 2465 | result.appendFormat("%10s | ", |
| 2466 | to_string(getCompositionType(hwcId)).c_str()); |
| 2467 | const Rect& frame = hwcInfo.displayFrame; |
| 2468 | result.appendFormat("%4d %4d %4d %4d | ", frame.left, frame.top, |
| 2469 | frame.right, frame.bottom); |
Dan Stoza | 5a423ea | 2017-02-16 14:10:39 -0800 | [diff] [blame] | 2470 | const FloatRect& crop = hwcInfo.sourceCrop; |
Dan Stoza | e22aec7 | 2016-08-01 13:20:59 -0700 | [diff] [blame] | 2471 | result.appendFormat("%6.1f %6.1f %6.1f %6.1f\n", crop.left, crop.top, |
| 2472 | crop.right, crop.bottom); |
| 2473 | |
| 2474 | result.append("- - - - - - - - - - - - - - - - - - - - "); |
| 2475 | result.append("- - - - - - - - - - - - - - - - - - - -\n"); |
| 2476 | } |
Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 2477 | #endif |
Dan Stoza | e22aec7 | 2016-08-01 13:20:59 -0700 | [diff] [blame] | 2478 | |
Svetoslav | d85084b | 2014-03-20 10:28:31 -0700 | [diff] [blame] | 2479 | void Layer::dumpFrameStats(String8& result) const { |
| 2480 | mFrameTracker.dumpStats(result); |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 2481 | } |
| 2482 | |
Svetoslav | d85084b | 2014-03-20 10:28:31 -0700 | [diff] [blame] | 2483 | void Layer::clearFrameStats() { |
| 2484 | mFrameTracker.clearStats(); |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 2485 | } |
| 2486 | |
Jamie Gennis | 6547ff4 | 2013-07-16 20:12:42 -0700 | [diff] [blame] | 2487 | void Layer::logFrameStats() { |
| 2488 | mFrameTracker.logAndResetStats(mName); |
| 2489 | } |
| 2490 | |
Svetoslav | d85084b | 2014-03-20 10:28:31 -0700 | [diff] [blame] | 2491 | void Layer::getFrameStats(FrameStats* outStats) const { |
| 2492 | mFrameTracker.getStats(outStats); |
| 2493 | } |
| 2494 | |
Brian Anderson | d6927fb | 2016-07-23 23:37:30 -0700 | [diff] [blame] | 2495 | void Layer::dumpFrameEvents(String8& result) { |
| 2496 | result.appendFormat("- Layer %s (%s, %p)\n", |
| 2497 | getName().string(), getTypeId(), this); |
| 2498 | Mutex::Autolock lock(mFrameEventHistoryMutex); |
| 2499 | mFrameEventHistory.checkFencesForCompletion(); |
| 2500 | mFrameEventHistory.dump(result); |
| 2501 | } |
Pablo Ceballos | 40845df | 2016-01-25 17:41:15 -0800 | [diff] [blame] | 2502 | |
Brian Anderson | 5ea5e59 | 2016-12-01 16:54:33 -0800 | [diff] [blame] | 2503 | void Layer::onDisconnect() { |
| 2504 | Mutex::Autolock lock(mFrameEventHistoryMutex); |
| 2505 | mFrameEventHistory.onDisconnect(); |
| 2506 | } |
| 2507 | |
Brian Anderson | 3890c39 | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 2508 | void Layer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps, |
| 2509 | FrameEventHistoryDelta *outDelta) { |
Brian Anderson | d6927fb | 2016-07-23 23:37:30 -0700 | [diff] [blame] | 2510 | Mutex::Autolock lock(mFrameEventHistoryMutex); |
| 2511 | if (newTimestamps) { |
Brian Anderson | fbc80ae | 2017-05-26 16:23:54 -0700 | [diff] [blame] | 2512 | // If there are any unsignaled fences in the aquire timeline at this |
| 2513 | // point, the previously queued frame hasn't been latched yet. Go ahead |
| 2514 | // and try to get the signal time here so the syscall is taken out of |
| 2515 | // the main thread's critical path. |
| 2516 | mAcquireTimeline.updateSignalTimes(); |
| 2517 | // Push the new fence after updating since it's likely still pending. |
Brian Anderson | 3d4039d | 2016-09-23 16:31:30 -0700 | [diff] [blame] | 2518 | mAcquireTimeline.push(newTimestamps->acquireFence); |
Brian Anderson | d6927fb | 2016-07-23 23:37:30 -0700 | [diff] [blame] | 2519 | mFrameEventHistory.addQueue(*newTimestamps); |
| 2520 | } |
| 2521 | |
Brian Anderson | 3890c39 | 2016-07-25 12:48:08 -0700 | [diff] [blame] | 2522 | if (outDelta) { |
| 2523 | mFrameEventHistory.getAndResetDelta(outDelta); |
Brian Anderson | d6927fb | 2016-07-23 23:37:30 -0700 | [diff] [blame] | 2524 | } |
Pablo Ceballos | 40845df | 2016-01-25 17:41:15 -0800 | [diff] [blame] | 2525 | } |
Dan Stoza | e77c766 | 2016-05-13 11:37:28 -0700 | [diff] [blame] | 2526 | |
| 2527 | std::vector<OccupancyTracker::Segment> Layer::getOccupancyHistory( |
| 2528 | bool forceFlush) { |
| 2529 | std::vector<OccupancyTracker::Segment> history; |
| 2530 | status_t result = mSurfaceFlingerConsumer->getOccupancyHistory(forceFlush, |
| 2531 | &history); |
| 2532 | if (result != NO_ERROR) { |
| 2533 | ALOGW("[%s] Failed to obtain occupancy history (%d)", mName.string(), |
| 2534 | result); |
| 2535 | return {}; |
| 2536 | } |
| 2537 | return history; |
| 2538 | } |
| 2539 | |
Robert Carr | 367c568 | 2016-06-20 11:55:28 -0700 | [diff] [blame] | 2540 | bool Layer::getTransformToDisplayInverse() const { |
| 2541 | return mSurfaceFlingerConsumer->getTransformToDisplayInverse(); |
| 2542 | } |
| 2543 | |
Chia-I Wu | 98f1c10 | 2017-05-30 14:54:08 -0700 | [diff] [blame] | 2544 | size_t Layer::getChildrenCount() const { |
| 2545 | size_t count = 0; |
| 2546 | for (const sp<Layer>& child : mCurrentChildren) { |
| 2547 | count += 1 + child->getChildrenCount(); |
| 2548 | } |
| 2549 | return count; |
| 2550 | } |
| 2551 | |
Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 2552 | void Layer::addChild(const sp<Layer>& layer) { |
| 2553 | mCurrentChildren.add(layer); |
| 2554 | layer->setParent(this); |
| 2555 | } |
| 2556 | |
| 2557 | ssize_t Layer::removeChild(const sp<Layer>& layer) { |
| 2558 | layer->setParent(nullptr); |
| 2559 | return mCurrentChildren.remove(layer); |
| 2560 | } |
| 2561 | |
Robert Carr | 1db73f6 | 2016-12-21 12:58:51 -0800 | [diff] [blame] | 2562 | bool Layer::reparentChildren(const sp<IBinder>& newParentHandle) { |
| 2563 | sp<Handle> handle = nullptr; |
| 2564 | sp<Layer> newParent = nullptr; |
| 2565 | if (newParentHandle == nullptr) { |
| 2566 | return false; |
| 2567 | } |
| 2568 | handle = static_cast<Handle*>(newParentHandle.get()); |
| 2569 | newParent = handle->owner.promote(); |
| 2570 | if (newParent == nullptr) { |
| 2571 | ALOGE("Unable to promote Layer handle"); |
| 2572 | return false; |
| 2573 | } |
| 2574 | |
| 2575 | for (const sp<Layer>& child : mCurrentChildren) { |
Chia-I Wu | e41dbe6 | 2017-06-13 14:10:56 -0700 | [diff] [blame] | 2576 | newParent->addChild(child); |
Robert Carr | 1db73f6 | 2016-12-21 12:58:51 -0800 | [diff] [blame] | 2577 | |
| 2578 | sp<Client> client(child->mClientRef.promote()); |
| 2579 | if (client != nullptr) { |
| 2580 | client->setParentLayer(newParent); |
| 2581 | } |
| 2582 | } |
| 2583 | mCurrentChildren.clear(); |
| 2584 | |
| 2585 | return true; |
| 2586 | } |
| 2587 | |
Robert Carr | 9524cb3 | 2017-02-13 11:32:32 -0800 | [diff] [blame] | 2588 | bool Layer::detachChildren() { |
Dan Stoza | 412903f | 2017-04-27 13:42:17 -0700 | [diff] [blame] | 2589 | traverseInZOrder(LayerVector::StateSet::Drawing, [this](Layer* child) { |
Robert Carr | 9524cb3 | 2017-02-13 11:32:32 -0800 | [diff] [blame] | 2590 | if (child == this) { |
| 2591 | return; |
| 2592 | } |
| 2593 | |
| 2594 | sp<Client> client(child->mClientRef.promote()); |
| 2595 | if (client != nullptr) { |
| 2596 | client->detachLayer(child); |
| 2597 | } |
| 2598 | }); |
| 2599 | |
| 2600 | return true; |
| 2601 | } |
| 2602 | |
Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 2603 | void Layer::setParent(const sp<Layer>& layer) { |
Chia-I Wu | e41dbe6 | 2017-06-13 14:10:56 -0700 | [diff] [blame] | 2604 | mCurrentParent = layer; |
Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 2605 | } |
| 2606 | |
| 2607 | void Layer::clearSyncPoints() { |
| 2608 | for (const auto& child : mCurrentChildren) { |
| 2609 | child->clearSyncPoints(); |
| 2610 | } |
| 2611 | |
| 2612 | Mutex::Autolock lock(mLocalSyncPointMutex); |
| 2613 | for (auto& point : mLocalSyncPoints) { |
| 2614 | point->setFrameAvailable(); |
| 2615 | } |
| 2616 | mLocalSyncPoints.clear(); |
| 2617 | } |
| 2618 | |
| 2619 | int32_t Layer::getZ() const { |
| 2620 | return mDrawingState.z; |
| 2621 | } |
| 2622 | |
Dan Stoza | 412903f | 2017-04-27 13:42:17 -0700 | [diff] [blame] | 2623 | LayerVector Layer::makeTraversalList(LayerVector::StateSet stateSet) { |
| 2624 | LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid, |
| 2625 | "makeTraversalList received invalid stateSet"); |
| 2626 | const bool useDrawing = stateSet == LayerVector::StateSet::Drawing; |
| 2627 | const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren; |
| 2628 | const State& state = useDrawing ? mDrawingState : mCurrentState; |
| 2629 | |
| 2630 | if (state.zOrderRelatives.size() == 0) { |
| 2631 | return children; |
Robert Carr | db66e62 | 2017-04-10 16:55:57 -0700 | [diff] [blame] | 2632 | } |
| 2633 | LayerVector traverse; |
| 2634 | |
Dan Stoza | 412903f | 2017-04-27 13:42:17 -0700 | [diff] [blame] | 2635 | for (const wp<Layer>& weakRelative : state.zOrderRelatives) { |
Robert Carr | db66e62 | 2017-04-10 16:55:57 -0700 | [diff] [blame] | 2636 | sp<Layer> strongRelative = weakRelative.promote(); |
| 2637 | if (strongRelative != nullptr) { |
| 2638 | traverse.add(strongRelative); |
Robert Carr | db66e62 | 2017-04-10 16:55:57 -0700 | [diff] [blame] | 2639 | } |
| 2640 | } |
| 2641 | |
Dan Stoza | 412903f | 2017-04-27 13:42:17 -0700 | [diff] [blame] | 2642 | for (const sp<Layer>& child : children) { |
Robert Carr | db66e62 | 2017-04-10 16:55:57 -0700 | [diff] [blame] | 2643 | traverse.add(child); |
| 2644 | } |
| 2645 | |
| 2646 | return traverse; |
| 2647 | } |
| 2648 | |
Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 2649 | /** |
Robert Carr | db66e62 | 2017-04-10 16:55:57 -0700 | [diff] [blame] | 2650 | * Negatively signed relatives are before 'this' in Z-order. |
Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 2651 | */ |
Dan Stoza | 412903f | 2017-04-27 13:42:17 -0700 | [diff] [blame] | 2652 | void Layer::traverseInZOrder(LayerVector::StateSet stateSet, const LayerVector::Visitor& visitor) { |
| 2653 | LayerVector list = makeTraversalList(stateSet); |
Robert Carr | db66e62 | 2017-04-10 16:55:57 -0700 | [diff] [blame] | 2654 | |
Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 2655 | size_t i = 0; |
Robert Carr | db66e62 | 2017-04-10 16:55:57 -0700 | [diff] [blame] | 2656 | for (; i < list.size(); i++) { |
| 2657 | const auto& relative = list[i]; |
| 2658 | if (relative->getZ() >= 0) { |
Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 2659 | break; |
Robert Carr | db66e62 | 2017-04-10 16:55:57 -0700 | [diff] [blame] | 2660 | } |
Dan Stoza | 412903f | 2017-04-27 13:42:17 -0700 | [diff] [blame] | 2661 | relative->traverseInZOrder(stateSet, visitor); |
Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 2662 | } |
Dan Stoza | 412903f | 2017-04-27 13:42:17 -0700 | [diff] [blame] | 2663 | visitor(this); |
Robert Carr | db66e62 | 2017-04-10 16:55:57 -0700 | [diff] [blame] | 2664 | for (; i < list.size(); i++) { |
| 2665 | const auto& relative = list[i]; |
Dan Stoza | 412903f | 2017-04-27 13:42:17 -0700 | [diff] [blame] | 2666 | relative->traverseInZOrder(stateSet, visitor); |
Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 2667 | } |
| 2668 | } |
| 2669 | |
| 2670 | /** |
Robert Carr | db66e62 | 2017-04-10 16:55:57 -0700 | [diff] [blame] | 2671 | * Positively signed relatives are before 'this' in reverse Z-order. |
Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 2672 | */ |
Dan Stoza | 412903f | 2017-04-27 13:42:17 -0700 | [diff] [blame] | 2673 | void Layer::traverseInReverseZOrder(LayerVector::StateSet stateSet, |
| 2674 | const LayerVector::Visitor& visitor) { |
| 2675 | LayerVector list = makeTraversalList(stateSet); |
Robert Carr | db66e62 | 2017-04-10 16:55:57 -0700 | [diff] [blame] | 2676 | |
Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 2677 | int32_t i = 0; |
Robert Carr | db66e62 | 2017-04-10 16:55:57 -0700 | [diff] [blame] | 2678 | for (i = list.size()-1; i>=0; i--) { |
| 2679 | const auto& relative = list[i]; |
| 2680 | if (relative->getZ() < 0) { |
Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 2681 | break; |
| 2682 | } |
Dan Stoza | 412903f | 2017-04-27 13:42:17 -0700 | [diff] [blame] | 2683 | relative->traverseInReverseZOrder(stateSet, visitor); |
Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 2684 | } |
Dan Stoza | 412903f | 2017-04-27 13:42:17 -0700 | [diff] [blame] | 2685 | visitor(this); |
Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 2686 | for (; i>=0; i--) { |
Robert Carr | db66e62 | 2017-04-10 16:55:57 -0700 | [diff] [blame] | 2687 | const auto& relative = list[i]; |
Dan Stoza | 412903f | 2017-04-27 13:42:17 -0700 | [diff] [blame] | 2688 | relative->traverseInReverseZOrder(stateSet, visitor); |
Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 2689 | } |
| 2690 | } |
| 2691 | |
| 2692 | Transform Layer::getTransform() const { |
| 2693 | Transform t; |
Chia-I Wu | e41dbe6 | 2017-06-13 14:10:56 -0700 | [diff] [blame] | 2694 | const auto& p = mDrawingParent.promote(); |
Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 2695 | if (p != nullptr) { |
| 2696 | t = p->getTransform(); |
Robert Carr | 9b429f4 | 2017-04-17 14:56:57 -0700 | [diff] [blame] | 2697 | |
| 2698 | // If the parent is not using NATIVE_WINDOW_SCALING_MODE_FREEZE (e.g. |
| 2699 | // it isFixedSize) then there may be additional scaling not accounted |
| 2700 | // for in the transform. We need to mirror this scaling in child surfaces |
| 2701 | // or we will break the contract where WM can treat child surfaces as |
| 2702 | // pixels in the parent surface. |
Chia-I Wu | 0a68b46 | 2017-07-18 11:30:05 -0700 | [diff] [blame] | 2703 | if (p->isFixedSize() && p->mActiveBuffer != nullptr) { |
Robert Carr | 1725eee | 2017-04-26 18:32:15 -0700 | [diff] [blame] | 2704 | int bufferWidth; |
| 2705 | int bufferHeight; |
| 2706 | if ((p->mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) == 0) { |
| 2707 | bufferWidth = p->mActiveBuffer->getWidth(); |
| 2708 | bufferHeight = p->mActiveBuffer->getHeight(); |
| 2709 | } else { |
| 2710 | bufferHeight = p->mActiveBuffer->getWidth(); |
| 2711 | bufferWidth = p->mActiveBuffer->getHeight(); |
| 2712 | } |
Robert Carr | 9b429f4 | 2017-04-17 14:56:57 -0700 | [diff] [blame] | 2713 | float sx = p->getDrawingState().active.w / |
Robert Carr | 1725eee | 2017-04-26 18:32:15 -0700 | [diff] [blame] | 2714 | static_cast<float>(bufferWidth); |
Robert Carr | 9b429f4 | 2017-04-17 14:56:57 -0700 | [diff] [blame] | 2715 | float sy = p->getDrawingState().active.h / |
Robert Carr | 1725eee | 2017-04-26 18:32:15 -0700 | [diff] [blame] | 2716 | static_cast<float>(bufferHeight); |
Robert Carr | 9b429f4 | 2017-04-17 14:56:57 -0700 | [diff] [blame] | 2717 | Transform extraParentScaling; |
| 2718 | extraParentScaling.set(sx, 0, 0, sy); |
| 2719 | t = t * extraParentScaling; |
| 2720 | } |
Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 2721 | } |
| 2722 | return t * getDrawingState().active.transform; |
| 2723 | } |
| 2724 | |
Robert Carr | 6452f12 | 2017-03-21 10:41:29 -0700 | [diff] [blame] | 2725 | #ifdef USE_HWC2 |
| 2726 | float Layer::getAlpha() const { |
Chia-I Wu | e41dbe6 | 2017-06-13 14:10:56 -0700 | [diff] [blame] | 2727 | const auto& p = mDrawingParent.promote(); |
Robert Carr | 6452f12 | 2017-03-21 10:41:29 -0700 | [diff] [blame] | 2728 | |
| 2729 | float parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0; |
| 2730 | return parentAlpha * getDrawingState().alpha; |
| 2731 | } |
| 2732 | #else |
| 2733 | uint8_t Layer::getAlpha() const { |
Chia-I Wu | e41dbe6 | 2017-06-13 14:10:56 -0700 | [diff] [blame] | 2734 | const auto& p = mDrawingParent.promote(); |
Robert Carr | 6452f12 | 2017-03-21 10:41:29 -0700 | [diff] [blame] | 2735 | |
| 2736 | float parentAlpha = (p != nullptr) ? (p->getAlpha() / 255.0f) : 1.0; |
| 2737 | float drawingAlpha = getDrawingState().alpha / 255.0f; |
| 2738 | drawingAlpha = drawingAlpha * parentAlpha; |
| 2739 | return static_cast<uint8_t>(std::round(drawingAlpha * 255)); |
| 2740 | } |
| 2741 | #endif |
| 2742 | |
Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 2743 | void Layer::commitChildList() { |
| 2744 | for (size_t i = 0; i < mCurrentChildren.size(); i++) { |
| 2745 | const auto& child = mCurrentChildren[i]; |
| 2746 | child->commitChildList(); |
| 2747 | } |
| 2748 | mDrawingChildren = mCurrentChildren; |
Chia-I Wu | e41dbe6 | 2017-06-13 14:10:56 -0700 | [diff] [blame] | 2749 | mDrawingParent = mCurrentParent; |
Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 2750 | } |
| 2751 | |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 2752 | // --------------------------------------------------------------------------- |
| 2753 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2754 | }; // namespace android |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 2755 | |
| 2756 | #if defined(__gl_h_) |
| 2757 | #error "don't include gl/gl.h in this file" |
| 2758 | #endif |
| 2759 | |
| 2760 | #if defined(__gl2_h_) |
| 2761 | #error "don't include gl2/gl2.h in this file" |
| 2762 | #endif |