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 |
Jamie Gennis | 1c8e95c | 2012-02-23 19:27:23 -0800 | [diff] [blame] | 18 | #define ATRACE_TAG ATRACE_TAG_GRAPHICS |
| 19 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 20 | #include <stdint.h> |
Mathias Agopian | 921e6ac | 2012-07-23 23:11:29 -0700 | [diff] [blame] | 21 | #include <sys/types.h> |
Romain Guy | 0147a17 | 2017-06-01 13:53:56 -0700 | [diff] [blame] | 22 | #include <algorithm> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 23 | #include <errno.h> |
| 24 | #include <math.h> |
Sahil Dhanju | c1ba5c4 | 2016-06-07 20:09:20 -0700 | [diff] [blame] | 25 | #include <mutex> |
Keun young Park | 63f165f | 2012-08-31 10:53:36 -0700 | [diff] [blame] | 26 | #include <dlfcn.h> |
Greg Hackmann | 86efcc0 | 2014-03-07 12:44:02 -0800 | [diff] [blame] | 27 | #include <inttypes.h> |
Jesse Hall | b154c42 | 2014-07-13 12:47:02 -0700 | [diff] [blame] | 28 | #include <stdatomic.h> |
Dan Stoza | 2b6d38e | 2017-06-01 16:40:30 -0700 | [diff] [blame] | 29 | #include <optional> |
Mathias Agopian | 921e6ac | 2012-07-23 23:11:29 -0700 | [diff] [blame] | 30 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 31 | #include <cutils/properties.h> |
Mark Salyzyn | 7823e12 | 2016-09-29 08:08:05 -0700 | [diff] [blame] | 32 | #include <log/log.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 33 | |
Mathias Agopian | c5b2c0b | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 34 | #include <binder/IPCThreadState.h> |
| 35 | #include <binder/IServiceManager.h> |
Mathias Agopian | 99b4984 | 2011-06-27 16:05:52 -0700 | [diff] [blame] | 36 | #include <binder/PermissionCache.h> |
Mathias Agopian | 7303c6b | 2009-07-02 18:11:53 -0700 | [diff] [blame] | 37 | |
Hendrik Wagenaar | 87670ff | 2017-02-01 12:10:46 -0800 | [diff] [blame] | 38 | #include <dvr/vr_flinger.h> |
| 39 | |
Courtney Goeltzenleuchter | 5d94389 | 2017-03-22 13:46:46 -0600 | [diff] [blame] | 40 | #include <ui/DebugUtils.h> |
Mathias Agopian | c666cae | 2012-07-25 18:56:13 -0700 | [diff] [blame] | 41 | #include <ui/DisplayInfo.h> |
Lajos Molnar | 67d8bd6 | 2014-09-11 14:58:45 -0700 | [diff] [blame] | 42 | #include <ui/DisplayStatInfo.h> |
Mathias Agopian | c666cae | 2012-07-25 18:56:13 -0700 | [diff] [blame] | 43 | |
Jamie Gennis | 1a4d883 | 2012-08-02 20:11:05 -0700 | [diff] [blame] | 44 | #include <gui/BufferQueue.h> |
Andy McFadden | 4803b74 | 2012-09-24 19:07:20 -0700 | [diff] [blame] | 45 | #include <gui/GuiConfig.h> |
Jamie Gennis | 1a4d883 | 2012-08-02 20:11:05 -0700 | [diff] [blame] | 46 | #include <gui/IDisplayEventConnection.h> |
Kalle Raita | a099a24 | 2017-01-11 11:17:29 -0800 | [diff] [blame] | 47 | #include <gui/LayerDebugInfo.h> |
Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 48 | #include <gui/Surface.h> |
Mathias Agopian | 921e6ac | 2012-07-23 23:11:29 -0700 | [diff] [blame] | 49 | |
| 50 | #include <ui/GraphicBufferAllocator.h> |
| 51 | #include <ui/PixelFormat.h> |
Andy McFadden | 4803b74 | 2012-09-24 19:07:20 -0700 | [diff] [blame] | 52 | #include <ui/UiConfig.h> |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 53 | |
Mathias Agopian | cde87a3 | 2012-09-13 14:09:01 -0700 | [diff] [blame] | 54 | #include <utils/misc.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 55 | #include <utils/String8.h> |
| 56 | #include <utils/String16.h> |
| 57 | #include <utils/StopWatch.h> |
Yusuke Sato | 0a688f5 | 2015-07-30 23:05:39 -0700 | [diff] [blame] | 58 | #include <utils/Timers.h> |
Jamie Gennis | 1c8e95c | 2012-02-23 19:27:23 -0800 | [diff] [blame] | 59 | #include <utils/Trace.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 60 | |
Mathias Agopian | 921e6ac | 2012-07-23 23:11:29 -0700 | [diff] [blame] | 61 | #include <private/android_filesystem_config.h> |
Mathias Agopian | ca08833 | 2013-03-28 17:44:13 -0700 | [diff] [blame] | 62 | #include <private/gui/SyncFeatures.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 63 | |
Robert Carr | 578038f | 2018-03-09 12:25:24 -0800 | [diff] [blame] | 64 | #include "BufferLayer.h" |
Mathias Agopian | 3e25fd8 | 2013-04-22 17:52:16 +0200 | [diff] [blame] | 65 | #include "Client.h" |
Robert Carr | 578038f | 2018-03-09 12:25:24 -0800 | [diff] [blame] | 66 | #include "ColorLayer.h" |
Mathias Agopian | 3e25fd8 | 2013-04-22 17:52:16 +0200 | [diff] [blame] | 67 | #include "Colorizer.h" |
Robert Carr | 578038f | 2018-03-09 12:25:24 -0800 | [diff] [blame] | 68 | #include "ContainerLayer.h" |
Mathias Agopian | 90ac799 | 2012-02-25 18:48:35 -0800 | [diff] [blame] | 69 | #include "DdmConnection.h" |
Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 70 | #include "DispSync.h" |
Robert Carr | 578038f | 2018-03-09 12:25:24 -0800 | [diff] [blame] | 71 | #include "DisplayDevice.h" |
Jamie Gennis | d170075 | 2013-10-14 12:22:52 -0700 | [diff] [blame] | 72 | #include "EventControlThread.h" |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 73 | #include "EventThread.h" |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 74 | #include "Layer.h" |
Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 75 | #include "LayerVector.h" |
Robert Carr | 1db73f6 | 2016-12-21 12:58:51 -0800 | [diff] [blame] | 76 | #include "MonitoredProducer.h" |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 77 | #include "SurfaceFlinger.h" |
Robert Carr | 578038f | 2018-03-09 12:25:24 -0800 | [diff] [blame] | 78 | #include "clz.h" |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 79 | |
Steven Thomas | b02664d | 2017-07-26 18:48:28 -0700 | [diff] [blame] | 80 | #include "DisplayHardware/ComposerHal.h" |
Dominik Laskowski | e9ef7c4 | 2018-03-12 19:34:30 -0700 | [diff] [blame] | 81 | #include "DisplayHardware/DisplayIdentification.h" |
Mathias Agopian | a491260 | 2012-07-12 14:25:33 -0700 | [diff] [blame] | 82 | #include "DisplayHardware/FramebufferSurface.h" |
Mathias Agopian | a350ff9 | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 83 | #include "DisplayHardware/HWComposer.h" |
Jesse Hall | 99c7dbb | 2013-03-14 14:29:29 -0700 | [diff] [blame] | 84 | #include "DisplayHardware/VirtualDisplaySurface.h" |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 85 | |
Mathias Agopian | ff2ed70 | 2013-09-01 21:36:12 -0700 | [diff] [blame] | 86 | #include "Effects/Daltonizer.h" |
| 87 | |
Mathias Agopian | 875d8e1 | 2013-06-07 15:35:48 -0700 | [diff] [blame] | 88 | #include "RenderEngine/RenderEngine.h" |
Mathias Agopian | ff2ed70 | 2013-09-01 21:36:12 -0700 | [diff] [blame] | 89 | #include <cutils/compiler.h> |
Mathias Agopian | 875d8e1 | 2013-06-07 15:35:48 -0700 | [diff] [blame] | 90 | |
Jiyong Park | 4b20c2e | 2017-01-14 19:45:11 +0900 | [diff] [blame] | 91 | #include <android/hardware/configstore/1.0/ISurfaceFlingerConfigs.h> |
Jaesoo Lee | 4351857 | 2017-01-23 19:03:16 +0900 | [diff] [blame] | 92 | #include <configstore/Utils.h> |
Jiyong Park | 4b20c2e | 2017-01-14 19:45:11 +0900 | [diff] [blame] | 93 | |
chaviw | 1d04428 | 2017-09-27 12:19:28 -0700 | [diff] [blame] | 94 | #include <layerproto/LayerProtoParser.h> |
| 95 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 96 | #define DISPLAY_COUNT 1 |
| 97 | |
Mathias Agopian | fee2b46 | 2013-07-03 12:34:01 -0700 | [diff] [blame] | 98 | /* |
| 99 | * DEBUG_SCREENSHOTS: set to true to check that screenshots are not all |
| 100 | * black pixels. |
| 101 | */ |
| 102 | #define DEBUG_SCREENSHOTS false |
| 103 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 104 | namespace android { |
Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 105 | |
Jaesoo Lee | 4351857 | 2017-01-23 19:03:16 +0900 | [diff] [blame] | 106 | using namespace android::hardware::configstore; |
Jiyong Park | 4b20c2e | 2017-01-14 19:45:11 +0900 | [diff] [blame] | 107 | using namespace android::hardware::configstore::V1_0; |
Peiyong Lin | 9f03447 | 2018-03-28 15:29:00 -0700 | [diff] [blame] | 108 | using ui::ColorMode; |
Peiyong Lin | 34beb7a | 2018-03-28 11:57:12 -0700 | [diff] [blame] | 109 | using ui::Dataspace; |
Peiyong Lin | 6266589 | 2018-04-16 11:07:44 -0700 | [diff] [blame] | 110 | using ui::Hdr; |
Peiyong Lin | 0e7a791 | 2018-04-05 14:36:36 -0700 | [diff] [blame] | 111 | using ui::RenderIntent; |
Jiyong Park | 4b20c2e | 2017-01-14 19:45:11 +0900 | [diff] [blame] | 112 | |
Steven Thomas | b02664d | 2017-07-26 18:48:28 -0700 | [diff] [blame] | 113 | namespace { |
| 114 | class ConditionalLock { |
| 115 | public: |
| 116 | ConditionalLock(Mutex& mutex, bool lock) : mMutex(mutex), mLocked(lock) { |
| 117 | if (lock) { |
| 118 | mMutex.lock(); |
| 119 | } |
| 120 | } |
| 121 | ~ConditionalLock() { if (mLocked) mMutex.unlock(); } |
| 122 | private: |
| 123 | Mutex& mMutex; |
| 124 | bool mLocked; |
| 125 | }; |
| 126 | } // namespace anonymous |
| 127 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 128 | // --------------------------------------------------------------------------- |
| 129 | |
Mathias Agopian | 99b4984 | 2011-06-27 16:05:52 -0700 | [diff] [blame] | 130 | const String16 sHardwareTest("android.permission.HARDWARE_TEST"); |
| 131 | const String16 sAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER"); |
| 132 | const String16 sReadFramebuffer("android.permission.READ_FRAME_BUFFER"); |
| 133 | const String16 sDump("android.permission.DUMP"); |
| 134 | |
| 135 | // --------------------------------------------------------------------------- |
Fabien Sanglard | 0cc1938 | 2017-03-06 11:54:40 -0800 | [diff] [blame] | 136 | int64_t SurfaceFlinger::vsyncPhaseOffsetNs; |
| 137 | int64_t SurfaceFlinger::sfVsyncPhaseOffsetNs; |
Fabien Sanglard | c45a7d9 | 2017-03-14 13:24:22 -0700 | [diff] [blame] | 138 | int64_t SurfaceFlinger::dispSyncPresentTimeOffset; |
Fabien Sanglard | a34ed63 | 2017-03-14 11:43:52 -0700 | [diff] [blame] | 139 | bool SurfaceFlinger::useHwcForRgbToYuv; |
Fabien Sanglard | c8e387e | 2017-03-10 10:30:28 -0800 | [diff] [blame] | 140 | uint64_t SurfaceFlinger::maxVirtualDisplaySize; |
Fabien Sanglard | cbf153b | 2017-03-10 17:57:12 -0800 | [diff] [blame] | 141 | bool SurfaceFlinger::hasSyncFramework; |
Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 142 | bool SurfaceFlinger::useVrFlinger; |
Fabien Sanglard | 1971b63 | 2017-03-10 14:50:03 -0800 | [diff] [blame] | 143 | int64_t SurfaceFlinger::maxFrameBufferAcquiredBuffers; |
Lloyd Pique | c520831 | 2018-01-08 17:59:02 -0800 | [diff] [blame] | 144 | // TODO(courtneygo): Rename hasWideColorDisplay to clarify its actual meaning. |
Courtney Goeltzenleuchter | 5d94389 | 2017-03-22 13:46:46 -0600 | [diff] [blame] | 145 | bool SurfaceFlinger::hasWideColorDisplay; |
Mathias Agopian | 99b4984 | 2011-06-27 16:05:52 -0700 | [diff] [blame] | 146 | |
Kalle Raita | a099a24 | 2017-01-11 11:17:29 -0800 | [diff] [blame] | 147 | |
| 148 | std::string getHwcServiceName() { |
| 149 | char value[PROPERTY_VALUE_MAX] = {}; |
| 150 | property_get("debug.sf.hwc_service_name", value, "default"); |
| 151 | ALOGI("Using HWComposer service: '%s'", value); |
| 152 | return std::string(value); |
| 153 | } |
| 154 | |
| 155 | bool useTrebleTestingOverride() { |
| 156 | char value[PROPERTY_VALUE_MAX] = {}; |
| 157 | property_get("debug.sf.treble_testing_override", value, "false"); |
| 158 | ALOGI("Treble testing override: '%s'", value); |
| 159 | return std::string(value) == "true"; |
| 160 | } |
| 161 | |
Peiyong Lin | dd9b2ae | 2018-03-01 16:22:45 -0800 | [diff] [blame] | 162 | DisplayColorSetting toDisplayColorSetting(int value) { |
| 163 | switch(value) { |
| 164 | case 0: |
| 165 | return DisplayColorSetting::MANAGED; |
| 166 | case 1: |
| 167 | return DisplayColorSetting::UNMANAGED; |
| 168 | case 2: |
| 169 | return DisplayColorSetting::ENHANCED; |
| 170 | default: |
| 171 | return DisplayColorSetting::MANAGED; |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | std::string decodeDisplayColorSetting(DisplayColorSetting displayColorSetting) { |
| 176 | switch(displayColorSetting) { |
| 177 | case DisplayColorSetting::MANAGED: |
| 178 | return std::string("Natural Mode"); |
| 179 | case DisplayColorSetting::UNMANAGED: |
| 180 | return std::string("Saturated Mode"); |
| 181 | case DisplayColorSetting::ENHANCED: |
| 182 | return std::string("Auto Color Mode"); |
| 183 | } |
| 184 | return std::string("Unknown Display Color Setting"); |
| 185 | } |
| 186 | |
Lloyd Pique | 99d3da5 | 2018-01-22 17:48:03 -0800 | [diff] [blame] | 187 | NativeWindowSurface::~NativeWindowSurface() = default; |
| 188 | |
| 189 | namespace impl { |
| 190 | |
| 191 | class NativeWindowSurface final : public android::NativeWindowSurface { |
| 192 | public: |
| 193 | static std::unique_ptr<android::NativeWindowSurface> create( |
| 194 | const sp<IGraphicBufferProducer>& producer) { |
| 195 | return std::make_unique<NativeWindowSurface>(producer); |
| 196 | } |
| 197 | |
| 198 | explicit NativeWindowSurface(const sp<IGraphicBufferProducer>& producer) |
| 199 | : surface(new Surface(producer, false)) {} |
| 200 | |
| 201 | ~NativeWindowSurface() override = default; |
| 202 | |
| 203 | private: |
| 204 | sp<ANativeWindow> getNativeWindow() const override { return surface; } |
| 205 | |
| 206 | void preallocateBuffers() override { surface->allocateBuffers(); } |
| 207 | |
| 208 | sp<Surface> surface; |
| 209 | }; |
| 210 | |
| 211 | } // namespace impl |
| 212 | |
David Sodman | bc81528 | 2017-11-05 18:57:52 -0800 | [diff] [blame] | 213 | SurfaceFlingerBE::SurfaceFlingerBE() |
| 214 | : mHwcServiceName(getHwcServiceName()), |
| 215 | mRenderEngine(nullptr), |
David Sodman | 4a36e93 | 2017-11-07 14:29:47 -0800 | [diff] [blame] | 216 | mFrameBuckets(), |
| 217 | mTotalTime(0), |
| 218 | mLastSwapTime(0), |
David Sodman | bc81528 | 2017-11-05 18:57:52 -0800 | [diff] [blame] | 219 | mComposerSequenceId(0) { |
| 220 | } |
| 221 | |
Lloyd Pique | ac648ee | 2018-01-17 13:42:24 -0800 | [diff] [blame] | 222 | SurfaceFlinger::SurfaceFlinger(SurfaceFlinger::SkipInitializationTag) |
Lloyd Pique | 12eb423 | 2018-01-17 11:54:43 -0800 | [diff] [blame] | 223 | : BnSurfaceComposer(), |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 224 | mTransactionFlags(0), |
Jamie Gennis | 2d5e230 | 2012-10-15 18:24:43 -0700 | [diff] [blame] | 225 | mTransactionPending(false), |
| 226 | mAnimTransactionPending(false), |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 227 | mLayersRemoved(false), |
Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 228 | mLayersAdded(false), |
Mathias Agopian | 52bbb1a | 2012-07-31 19:01:53 -0700 | [diff] [blame] | 229 | mRepaintEverything(0), |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 230 | mBootTime(systemTime()), |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 231 | mBuiltinDisplays(), |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 232 | mVisibleRegionsDirty(false), |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 233 | mGeometryInvalid(false), |
Jamie Gennis | 4b0eba9 | 2013-02-05 13:30:24 -0800 | [diff] [blame] | 234 | mAnimCompositionPending(false), |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 235 | mDebugRegion(0), |
Mathias Agopian | 8afb7e3 | 2011-08-15 20:44:40 -0700 | [diff] [blame] | 236 | mDebugDDMS(0), |
Mathias Agopian | 73d3ba9 | 2010-09-22 18:58:01 -0700 | [diff] [blame] | 237 | mDebugDisableHWC(0), |
Mathias Agopian | a458364 | 2011-08-23 18:03:18 -0700 | [diff] [blame] | 238 | mDebugDisableTransformHint(0), |
Mathias Agopian | 9795c42 | 2009-08-26 16:36:26 -0700 | [diff] [blame] | 239 | mDebugInSwapBuffers(0), |
| 240 | mLastSwapBufferTime(0), |
| 241 | mDebugInTransaction(0), |
| 242 | mLastTransactionTime(0), |
Mathias Agopian | ff2ed70 | 2013-09-01 21:36:12 -0700 | [diff] [blame] | 243 | mBootFinished(false), |
Dan Stoza | ee44edd | 2015-03-23 15:50:23 -0700 | [diff] [blame] | 244 | mForceFullDamage(false), |
Tim Murray | 4a4e4a2 | 2016-04-19 16:29:23 +0000 | [diff] [blame] | 245 | mPrimaryDispSync("PrimaryDispSync"), |
Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 246 | mPrimaryHWVsyncEnabled(false), |
Jesse Hall | 948fe0c | 2013-10-14 12:56:09 -0700 | [diff] [blame] | 247 | mHWVsyncAvailable(false), |
Dan Stoza | b90cf07 | 2015-03-05 11:05:59 -0800 | [diff] [blame] | 248 | mHasPoweredOff(false), |
Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 249 | mNumLayers(0), |
Steven Thomas | b02664d | 2017-07-26 18:48:28 -0700 | [diff] [blame] | 250 | mVrFlingerRequestsDisplay(false), |
Lloyd Pique | 12eb423 | 2018-01-17 11:54:43 -0800 | [diff] [blame] | 251 | mMainThreadId(std::this_thread::get_id()), |
Lloyd Pique | 99d3da5 | 2018-01-22 17:48:03 -0800 | [diff] [blame] | 252 | mCreateBufferQueue(&BufferQueue::createBufferQueue), |
| 253 | mCreateNativeWindowSurface(&impl::NativeWindowSurface::create) {} |
Lloyd Pique | ac648ee | 2018-01-17 13:42:24 -0800 | [diff] [blame] | 254 | |
| 255 | SurfaceFlinger::SurfaceFlinger() : SurfaceFlinger(SkipInitialization) { |
Fabien Sanglard | cbf153b | 2017-03-10 17:57:12 -0800 | [diff] [blame] | 256 | ALOGI("SurfaceFlinger is starting"); |
Fabien Sanglard | 0cc1938 | 2017-03-06 11:54:40 -0800 | [diff] [blame] | 257 | |
| 258 | vsyncPhaseOffsetNs = getInt64< ISurfaceFlingerConfigs, |
| 259 | &ISurfaceFlingerConfigs::vsyncEventPhaseOffsetNs>(1000000); |
| 260 | |
| 261 | sfVsyncPhaseOffsetNs = getInt64< ISurfaceFlingerConfigs, |
| 262 | &ISurfaceFlingerConfigs::vsyncSfEventPhaseOffsetNs>(1000000); |
| 263 | |
Fabien Sanglard | cbf153b | 2017-03-10 17:57:12 -0800 | [diff] [blame] | 264 | hasSyncFramework = getBool< ISurfaceFlingerConfigs, |
| 265 | &ISurfaceFlingerConfigs::hasSyncFramework>(true); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 266 | |
Fabien Sanglard | c45a7d9 | 2017-03-14 13:24:22 -0700 | [diff] [blame] | 267 | dispSyncPresentTimeOffset = getInt64< ISurfaceFlingerConfigs, |
| 268 | &ISurfaceFlingerConfigs::presentTimeOffsetFromVSyncNs>(0); |
| 269 | |
Fabien Sanglard | a34ed63 | 2017-03-14 11:43:52 -0700 | [diff] [blame] | 270 | useHwcForRgbToYuv = getBool< ISurfaceFlingerConfigs, |
| 271 | &ISurfaceFlingerConfigs::useHwcForRGBtoYUV>(false); |
| 272 | |
Fabien Sanglard | c8e387e | 2017-03-10 10:30:28 -0800 | [diff] [blame] | 273 | maxVirtualDisplaySize = getUInt64<ISurfaceFlingerConfigs, |
| 274 | &ISurfaceFlingerConfigs::maxVirtualDisplaySize>(0); |
| 275 | |
Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 276 | // Vr flinger is only enabled on Daydream ready devices. |
| 277 | useVrFlinger = getBool< ISurfaceFlingerConfigs, |
| 278 | &ISurfaceFlingerConfigs::useVrFlinger>(false); |
| 279 | |
Fabien Sanglard | 1971b63 | 2017-03-10 14:50:03 -0800 | [diff] [blame] | 280 | maxFrameBufferAcquiredBuffers = getInt64< ISurfaceFlingerConfigs, |
| 281 | &ISurfaceFlingerConfigs::maxFrameBufferAcquiredBuffers>(2); |
| 282 | |
Courtney Goeltzenleuchter | 5d94389 | 2017-03-22 13:46:46 -0600 | [diff] [blame] | 283 | hasWideColorDisplay = |
| 284 | getBool<ISurfaceFlingerConfigs, &ISurfaceFlingerConfigs::hasWideColorDisplay>(false); |
| 285 | |
David Sodman | 99974d2 | 2017-11-28 12:04:33 -0800 | [diff] [blame] | 286 | mPrimaryDispSync.init(SurfaceFlinger::hasSyncFramework, SurfaceFlinger::dispSyncPresentTimeOffset); |
Saurabh Shah | f417453 | 2017-07-13 10:45:07 -0700 | [diff] [blame] | 287 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 288 | // debugging stuff... |
| 289 | char value[PROPERTY_VALUE_MAX]; |
Mathias Agopian | 8afb7e3 | 2011-08-15 20:44:40 -0700 | [diff] [blame] | 290 | |
Mathias Agopian | b4b1730 | 2013-03-20 18:36:41 -0700 | [diff] [blame] | 291 | property_get("ro.bq.gpu_to_cpu_unsupported", value, "0"); |
Mathias Agopian | 50210b9 | 2013-03-21 21:13:21 -0700 | [diff] [blame] | 292 | mGpuToCpuSupported = !atoi(value); |
Mathias Agopian | b4b1730 | 2013-03-20 18:36:41 -0700 | [diff] [blame] | 293 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 294 | property_get("debug.sf.showupdates", value, "0"); |
| 295 | mDebugRegion = atoi(value); |
Mathias Agopian | 8afb7e3 | 2011-08-15 20:44:40 -0700 | [diff] [blame] | 296 | |
Mathias Agopian | 8afb7e3 | 2011-08-15 20:44:40 -0700 | [diff] [blame] | 297 | property_get("debug.sf.ddms", value, "0"); |
| 298 | mDebugDDMS = atoi(value); |
| 299 | if (mDebugDDMS) { |
Keun young Park | 63f165f | 2012-08-31 10:53:36 -0700 | [diff] [blame] | 300 | if (!startDdmConnection()) { |
| 301 | // start failed, and DDMS debugging not enabled |
| 302 | mDebugDDMS = 0; |
| 303 | } |
Mathias Agopian | 8afb7e3 | 2011-08-15 20:44:40 -0700 | [diff] [blame] | 304 | } |
Mathias Agopian | c1d359d | 2012-08-04 20:09:03 -0700 | [diff] [blame] | 305 | ALOGI_IF(mDebugRegion, "showupdates enabled"); |
| 306 | ALOGI_IF(mDebugDDMS, "DDMS debugging enabled"); |
Dan Stoza | c5da271 | 2016-07-20 15:38:12 -0700 | [diff] [blame] | 307 | |
| 308 | property_get("debug.sf.disable_backpressure", value, "0"); |
| 309 | mPropagateBackpressure = !atoi(value); |
| 310 | ALOGI_IF(!mPropagateBackpressure, "Disabling backpressure propagation"); |
Dan Stoza | 8cf150a | 2016-08-02 10:27:31 -0700 | [diff] [blame] | 311 | |
Fabien Sanglard | 642b23d | 2017-02-09 12:29:39 -0800 | [diff] [blame] | 312 | property_get("debug.sf.enable_hwc_vds", value, "0"); |
| 313 | mUseHwcVirtualDisplays = atoi(value); |
| 314 | ALOGI_IF(!mUseHwcVirtualDisplays, "Enabling HWC virtual displays"); |
Fabien Sanglard | 63a5fcd | 2016-12-29 15:13:07 -0800 | [diff] [blame] | 315 | |
Fabien Sanglard | c65dafa | 2017-02-07 14:06:39 -0800 | [diff] [blame] | 316 | property_get("ro.sf.disable_triple_buffer", value, "1"); |
| 317 | mLayerTripleBufferingDisabled = atoi(value); |
Fabien Sanglard | 63a5fcd | 2016-12-29 15:13:07 -0800 | [diff] [blame] | 318 | ALOGI_IF(mLayerTripleBufferingDisabled, "Disabling Triple Buffering"); |
Romain Guy | 3054f00 | 2017-06-05 18:38:53 -0700 | [diff] [blame] | 319 | |
Dan Stoza | 0a0158c | 2018-03-16 13:38:54 -0700 | [diff] [blame] | 320 | // TODO (b/74616334): Reduce the default value once we isolate the leak |
| 321 | const size_t defaultListSize = 4 * MAX_LAYERS; |
| 322 | auto listSize = property_get_int32("debug.sf.max_igbp_list_size", int32_t(defaultListSize)); |
| 323 | mMaxGraphicBufferProducerListSize = (listSize > 0) ? size_t(listSize) : defaultListSize; |
| 324 | |
Dan Stoza | 84d619e | 2018-03-28 17:07:36 -0700 | [diff] [blame] | 325 | property_get("debug.sf.early_phase_offset_ns", value, "0"); |
| 326 | const int earlyWakeupOffsetOffsetNs = atoi(value); |
| 327 | ALOGI_IF(earlyWakeupOffsetOffsetNs != 0, "Enabling separate early offset"); |
| 328 | mVsyncModulator.setPhaseOffsets(sfVsyncPhaseOffsetNs - earlyWakeupOffsetOffsetNs, |
| 329 | sfVsyncPhaseOffsetNs); |
| 330 | |
Romain Guy | 11d63f4 | 2017-07-20 12:47:14 -0700 | [diff] [blame] | 331 | // We should be reading 'persist.sys.sf.color_saturation' here |
| 332 | // but since /data may be encrypted, we need to wait until after vold |
| 333 | // comes online to attempt to read the property. The property is |
| 334 | // instead read after the boot animation |
Kalle Raita | a099a24 | 2017-01-11 11:17:29 -0800 | [diff] [blame] | 335 | |
| 336 | if (useTrebleTestingOverride()) { |
| 337 | // Without the override SurfaceFlinger cannot connect to HIDL |
| 338 | // services that are not listed in the manifests. Considered |
| 339 | // deriving the setting from the set service name, but it |
| 340 | // would be brittle if the name that's not 'default' is used |
| 341 | // for production purposes later on. |
| 342 | setenv("TREBLE_TESTING_OVERRIDE", "true", true); |
| 343 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 344 | } |
| 345 | |
Mathias Agopian | 99ce5cd | 2012-01-31 18:24:27 -0800 | [diff] [blame] | 346 | void SurfaceFlinger::onFirstRef() |
| 347 | { |
Lloyd Pique | 3fcdef1 | 2018-01-22 17:14:00 -0800 | [diff] [blame] | 348 | mEventQueue->init(this); |
Mathias Agopian | 99ce5cd | 2012-01-31 18:24:27 -0800 | [diff] [blame] | 349 | } |
| 350 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 351 | SurfaceFlinger::~SurfaceFlinger() |
| 352 | { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 353 | } |
| 354 | |
Dan Stoza | c701401 | 2014-02-14 15:03:43 -0800 | [diff] [blame] | 355 | void SurfaceFlinger::binderDied(const wp<IBinder>& /* who */) |
Mathias Agopian | 99ce5cd | 2012-01-31 18:24:27 -0800 | [diff] [blame] | 356 | { |
| 357 | // the window manager died on us. prepare its eulogy. |
| 358 | |
Andy McFadden | 13a082e | 2012-08-24 10:16:42 -0700 | [diff] [blame] | 359 | // restore initial conditions (default device unblank, etc) |
| 360 | initializeDisplays(); |
Mathias Agopian | 99ce5cd | 2012-01-31 18:24:27 -0800 | [diff] [blame] | 361 | |
| 362 | // restart the boot-animation |
Mathias Agopian | a67e418 | 2012-06-19 17:26:12 -0700 | [diff] [blame] | 363 | startBootAnim(); |
Mathias Agopian | 99ce5cd | 2012-01-31 18:24:27 -0800 | [diff] [blame] | 364 | } |
| 365 | |
Robert Carr | 1db73f6 | 2016-12-21 12:58:51 -0800 | [diff] [blame] | 366 | static sp<ISurfaceComposerClient> initClient(const sp<Client>& client) { |
Mathias Agopian | 96f0819 | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 367 | status_t err = client->initCheck(); |
| 368 | if (err == NO_ERROR) { |
Robert Carr | 1db73f6 | 2016-12-21 12:58:51 -0800 | [diff] [blame] | 369 | return client; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 370 | } |
Robert Carr | 1db73f6 | 2016-12-21 12:58:51 -0800 | [diff] [blame] | 371 | return nullptr; |
| 372 | } |
| 373 | |
| 374 | sp<ISurfaceComposerClient> SurfaceFlinger::createConnection() { |
| 375 | return initClient(new Client(this)); |
| 376 | } |
| 377 | |
| 378 | sp<ISurfaceComposerClient> SurfaceFlinger::createScopedConnection( |
| 379 | const sp<IGraphicBufferProducer>& gbp) { |
| 380 | if (authenticateSurfaceTexture(gbp) == false) { |
| 381 | return nullptr; |
| 382 | } |
| 383 | const auto& layer = (static_cast<MonitoredProducer*>(gbp.get()))->getLayer(); |
| 384 | if (layer == nullptr) { |
| 385 | return nullptr; |
| 386 | } |
| 387 | |
| 388 | return initClient(new Client(this, layer)); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 389 | } |
| 390 | |
Jamie Gennis | dd3cb84 | 2012-10-19 18:19:11 -0700 | [diff] [blame] | 391 | sp<IBinder> SurfaceFlinger::createDisplay(const String8& displayName, |
| 392 | bool secure) |
Mathias Agopian | e57f292 | 2012-08-09 16:29:12 -0700 | [diff] [blame] | 393 | { |
| 394 | class DisplayToken : public BBinder { |
| 395 | sp<SurfaceFlinger> flinger; |
| 396 | virtual ~DisplayToken() { |
| 397 | // no more references, this display must be terminated |
| 398 | Mutex::Autolock _l(flinger->mStateLock); |
| 399 | flinger->mCurrentState.displays.removeItem(this); |
| 400 | flinger->setTransactionFlags(eDisplayTransactionNeeded); |
| 401 | } |
| 402 | public: |
Chih-Hung Hsieh | c406791 | 2016-05-03 14:03:27 -0700 | [diff] [blame] | 403 | explicit DisplayToken(const sp<SurfaceFlinger>& flinger) |
Mathias Agopian | e57f292 | 2012-08-09 16:29:12 -0700 | [diff] [blame] | 404 | : flinger(flinger) { |
| 405 | } |
| 406 | }; |
| 407 | |
| 408 | sp<BBinder> token = new DisplayToken(this); |
| 409 | |
| 410 | Mutex::Autolock _l(mStateLock); |
Dominik Laskowski | 663bd28 | 2018-04-19 15:26:54 -0700 | [diff] [blame] | 411 | DisplayDeviceState info; |
| 412 | info.type = DisplayDevice::DISPLAY_VIRTUAL; |
Andy McFadden | 8dfa92f | 2012-09-17 18:27:17 -0700 | [diff] [blame] | 413 | info.displayName = displayName; |
Dominik Laskowski | 663bd28 | 2018-04-19 15:26:54 -0700 | [diff] [blame] | 414 | info.isSecure = secure; |
Mathias Agopian | e57f292 | 2012-08-09 16:29:12 -0700 | [diff] [blame] | 415 | mCurrentState.displays.add(token, info); |
Lloyd Pique | 4dccc41 | 2018-01-22 17:21:36 -0800 | [diff] [blame] | 416 | mInterceptor->saveDisplayCreation(info); |
Mathias Agopian | e57f292 | 2012-08-09 16:29:12 -0700 | [diff] [blame] | 417 | return token; |
| 418 | } |
| 419 | |
Jesse Hall | 6c913be | 2013-08-08 12:15:49 -0700 | [diff] [blame] | 420 | void SurfaceFlinger::destroyDisplay(const sp<IBinder>& display) { |
| 421 | Mutex::Autolock _l(mStateLock); |
| 422 | |
| 423 | ssize_t idx = mCurrentState.displays.indexOfKey(display); |
| 424 | if (idx < 0) { |
| 425 | ALOGW("destroyDisplay: invalid display token"); |
| 426 | return; |
| 427 | } |
| 428 | |
| 429 | const DisplayDeviceState& info(mCurrentState.displays.valueAt(idx)); |
Dominik Laskowski | 663bd28 | 2018-04-19 15:26:54 -0700 | [diff] [blame] | 430 | if (!info.isVirtual()) { |
Jesse Hall | 6c913be | 2013-08-08 12:15:49 -0700 | [diff] [blame] | 431 | ALOGE("destroyDisplay called for non-virtual display"); |
| 432 | return; |
| 433 | } |
Dominik Laskowski | 663bd28 | 2018-04-19 15:26:54 -0700 | [diff] [blame] | 434 | mInterceptor->saveDisplayDeletion(info.sequenceId); |
Jesse Hall | 6c913be | 2013-08-08 12:15:49 -0700 | [diff] [blame] | 435 | mCurrentState.displays.removeItemsAt(idx); |
| 436 | setTransactionFlags(eDisplayTransactionNeeded); |
| 437 | } |
| 438 | |
Mathias Agopian | e57f292 | 2012-08-09 16:29:12 -0700 | [diff] [blame] | 439 | sp<IBinder> SurfaceFlinger::getBuiltInDisplay(int32_t id) { |
Jesse Hall | 9e663de | 2013-08-16 14:28:37 -0700 | [diff] [blame] | 440 | if (uint32_t(id) >= DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) { |
Mathias Agopian | e57f292 | 2012-08-09 16:29:12 -0700 | [diff] [blame] | 441 | ALOGE("getDefaultDisplay: id=%d is not a valid default display id", id); |
Peiyong Lin | 566a3b4 | 2018-01-09 18:22:43 -0800 | [diff] [blame] | 442 | return nullptr; |
Mathias Agopian | e57f292 | 2012-08-09 16:29:12 -0700 | [diff] [blame] | 443 | } |
Jesse Hall | 692c723 | 2012-11-08 15:41:56 -0800 | [diff] [blame] | 444 | return mBuiltinDisplays[id]; |
Mathias Agopian | e57f292 | 2012-08-09 16:29:12 -0700 | [diff] [blame] | 445 | } |
| 446 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 447 | void SurfaceFlinger::bootFinished() |
| 448 | { |
Wei Wang | f9b05ee | 2017-07-19 20:59:39 -0700 | [diff] [blame] | 449 | if (mStartPropertySetThread->join() != NO_ERROR) { |
| 450 | ALOGE("Join StartPropertySetThread failed!"); |
Wei Wang | b254fa3 | 2017-01-31 17:43:23 -0800 | [diff] [blame] | 451 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 452 | const nsecs_t now = systemTime(); |
| 453 | const nsecs_t duration = now - mBootTime; |
Steve Block | a19954a | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 454 | ALOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) ); |
Mathias Agopian | 1f339ff | 2011-07-01 17:08:43 -0700 | [diff] [blame] | 455 | |
| 456 | // wait patiently for the window manager death |
| 457 | const String16 name("window"); |
| 458 | sp<IBinder> window(defaultServiceManager()->getService(name)); |
| 459 | if (window != 0) { |
Mathias Agopian | 921e6ac | 2012-07-23 23:11:29 -0700 | [diff] [blame] | 460 | window->linkToDeath(static_cast<IBinder::DeathRecipient*>(this)); |
Mathias Agopian | 1f339ff | 2011-07-01 17:08:43 -0700 | [diff] [blame] | 461 | } |
| 462 | |
Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 463 | if (mVrFlinger) { |
| 464 | mVrFlinger->OnBootFinished(); |
| 465 | } |
| 466 | |
Mathias Agopian | 1f339ff | 2011-07-01 17:08:43 -0700 | [diff] [blame] | 467 | // stop boot animation |
Mathias Agopian | a67e418 | 2012-06-19 17:26:12 -0700 | [diff] [blame] | 468 | // formerly we would just kill the process, but we now ask it to exit so it |
| 469 | // can choose where to stop the animation. |
| 470 | property_set("service.bootanim.exit", "1"); |
Yusuke Sato | 0a688f5 | 2015-07-30 23:05:39 -0700 | [diff] [blame] | 471 | |
| 472 | const int LOGTAG_SF_STOP_BOOTANIM = 60110; |
| 473 | LOG_EVENT_LONG(LOGTAG_SF_STOP_BOOTANIM, |
| 474 | ns2ms(systemTime(SYSTEM_TIME_MONOTONIC))); |
Romain Guy | 11d63f4 | 2017-07-20 12:47:14 -0700 | [diff] [blame] | 475 | |
Thierry Strudel | 2924d01 | 2017-08-14 15:19:37 -0700 | [diff] [blame] | 476 | sp<LambdaMessage> readProperties = new LambdaMessage([&]() { |
Romain Guy | 11d63f4 | 2017-07-20 12:47:14 -0700 | [diff] [blame] | 477 | readPersistentProperties(); |
| 478 | }); |
Thierry Strudel | 2924d01 | 2017-08-14 15:19:37 -0700 | [diff] [blame] | 479 | postMessageAsync(readProperties); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 480 | } |
| 481 | |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 482 | void SurfaceFlinger::deleteTextureAsync(uint32_t texture) { |
Mathias Agopian | 921e6ac | 2012-07-23 23:11:29 -0700 | [diff] [blame] | 483 | class MessageDestroyGLTexture : public MessageBase { |
Lloyd Pique | 144e116 | 2017-12-20 16:44:52 -0800 | [diff] [blame] | 484 | RE::RenderEngine& engine; |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 485 | uint32_t texture; |
Mathias Agopian | 921e6ac | 2012-07-23 23:11:29 -0700 | [diff] [blame] | 486 | public: |
Lloyd Pique | 144e116 | 2017-12-20 16:44:52 -0800 | [diff] [blame] | 487 | MessageDestroyGLTexture(RE::RenderEngine& engine, uint32_t texture) |
| 488 | : engine(engine), texture(texture) {} |
Mathias Agopian | 921e6ac | 2012-07-23 23:11:29 -0700 | [diff] [blame] | 489 | virtual bool handler() { |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 490 | engine.deleteTextures(1, &texture); |
Mathias Agopian | 921e6ac | 2012-07-23 23:11:29 -0700 | [diff] [blame] | 491 | return true; |
| 492 | } |
| 493 | }; |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 494 | postMessageAsync(new MessageDestroyGLTexture(getRenderEngine(), texture)); |
Mathias Agopian | 921e6ac | 2012-07-23 23:11:29 -0700 | [diff] [blame] | 495 | } |
| 496 | |
Lloyd Pique | e83f931 | 2018-02-01 12:53:17 -0800 | [diff] [blame] | 497 | class DispSyncSource final : public VSyncSource, private DispSync::Callback { |
Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 498 | public: |
Andy McFadden | 5167ec6 | 2014-05-22 13:08:43 -0700 | [diff] [blame] | 499 | DispSyncSource(DispSync* dispSync, nsecs_t phaseOffset, bool traceVsync, |
Tim Murray | 4a4e4a2 | 2016-04-19 16:29:23 +0000 | [diff] [blame] | 500 | const char* name) : |
| 501 | mName(name), |
Jamie Gennis | 0a645cc | 2013-10-14 20:52:46 -0700 | [diff] [blame] | 502 | mValue(0), |
Jamie Gennis | 0a645cc | 2013-10-14 20:52:46 -0700 | [diff] [blame] | 503 | mTraceVsync(traceVsync), |
Tim Murray | 4a4e4a2 | 2016-04-19 16:29:23 +0000 | [diff] [blame] | 504 | mVsyncOnLabel(String8::format("VsyncOn-%s", name)), |
| 505 | mVsyncEventLabel(String8::format("VSYNC-%s", name)), |
Dan Stoza | db4ac3c | 2015-04-14 11:34:01 -0700 | [diff] [blame] | 506 | mDispSync(dispSync), |
| 507 | mCallbackMutex(), |
Dan Stoza | db4ac3c | 2015-04-14 11:34:01 -0700 | [diff] [blame] | 508 | mVsyncMutex(), |
| 509 | mPhaseOffset(phaseOffset), |
| 510 | mEnabled(false) {} |
Mathias Agopian | a491260 | 2012-07-12 14:25:33 -0700 | [diff] [blame] | 511 | |
Lloyd Pique | e83f931 | 2018-02-01 12:53:17 -0800 | [diff] [blame] | 512 | ~DispSyncSource() override = default; |
Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 513 | |
Lloyd Pique | e83f931 | 2018-02-01 12:53:17 -0800 | [diff] [blame] | 514 | void setVSyncEnabled(bool enable) override { |
Dan Stoza | db4ac3c | 2015-04-14 11:34:01 -0700 | [diff] [blame] | 515 | Mutex::Autolock lock(mVsyncMutex); |
Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 516 | if (enable) { |
Tim Murray | 4a4e4a2 | 2016-04-19 16:29:23 +0000 | [diff] [blame] | 517 | status_t err = mDispSync->addEventListener(mName, mPhaseOffset, |
Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 518 | static_cast<DispSync::Callback*>(this)); |
| 519 | if (err != NO_ERROR) { |
| 520 | ALOGE("error registering vsync callback: %s (%d)", |
| 521 | strerror(-err), err); |
| 522 | } |
Andy McFadden | 5167ec6 | 2014-05-22 13:08:43 -0700 | [diff] [blame] | 523 | //ATRACE_INT(mVsyncOnLabel.string(), 1); |
Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 524 | } else { |
| 525 | status_t err = mDispSync->removeEventListener( |
| 526 | static_cast<DispSync::Callback*>(this)); |
| 527 | if (err != NO_ERROR) { |
| 528 | ALOGE("error unregistering vsync callback: %s (%d)", |
| 529 | strerror(-err), err); |
| 530 | } |
Andy McFadden | 5167ec6 | 2014-05-22 13:08:43 -0700 | [diff] [blame] | 531 | //ATRACE_INT(mVsyncOnLabel.string(), 0); |
Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 532 | } |
Dan Stoza | db4ac3c | 2015-04-14 11:34:01 -0700 | [diff] [blame] | 533 | mEnabled = enable; |
Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 534 | } |
| 535 | |
Lloyd Pique | e83f931 | 2018-02-01 12:53:17 -0800 | [diff] [blame] | 536 | void setCallback(VSyncSource::Callback* callback) override{ |
Dan Stoza | db4ac3c | 2015-04-14 11:34:01 -0700 | [diff] [blame] | 537 | Mutex::Autolock lock(mCallbackMutex); |
Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 538 | mCallback = callback; |
| 539 | } |
| 540 | |
Lloyd Pique | e83f931 | 2018-02-01 12:53:17 -0800 | [diff] [blame] | 541 | void setPhaseOffset(nsecs_t phaseOffset) override { |
Dan Stoza | db4ac3c | 2015-04-14 11:34:01 -0700 | [diff] [blame] | 542 | Mutex::Autolock lock(mVsyncMutex); |
| 543 | |
| 544 | // Normalize phaseOffset to [0, period) |
| 545 | auto period = mDispSync->getPeriod(); |
| 546 | phaseOffset %= period; |
| 547 | if (phaseOffset < 0) { |
| 548 | // If we're here, then phaseOffset is in (-period, 0). After this |
| 549 | // operation, it will be in (0, period) |
| 550 | phaseOffset += period; |
| 551 | } |
| 552 | mPhaseOffset = phaseOffset; |
| 553 | |
| 554 | // If we're not enabled, we don't need to mess with the listeners |
| 555 | if (!mEnabled) { |
| 556 | return; |
| 557 | } |
| 558 | |
Dan Stoza | 84d619e | 2018-03-28 17:07:36 -0700 | [diff] [blame] | 559 | status_t err = mDispSync->changePhaseOffset(static_cast<DispSync::Callback*>(this), |
| 560 | mPhaseOffset); |
Dan Stoza | db4ac3c | 2015-04-14 11:34:01 -0700 | [diff] [blame] | 561 | if (err != NO_ERROR) { |
Dan Stoza | 84d619e | 2018-03-28 17:07:36 -0700 | [diff] [blame] | 562 | ALOGE("error changing vsync offset: %s (%d)", |
Dan Stoza | db4ac3c | 2015-04-14 11:34:01 -0700 | [diff] [blame] | 563 | strerror(-err), err); |
| 564 | } |
| 565 | } |
| 566 | |
Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 567 | private: |
| 568 | virtual void onDispSyncEvent(nsecs_t when) { |
Lloyd Pique | e83f931 | 2018-02-01 12:53:17 -0800 | [diff] [blame] | 569 | VSyncSource::Callback* callback; |
Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 570 | { |
Dan Stoza | db4ac3c | 2015-04-14 11:34:01 -0700 | [diff] [blame] | 571 | Mutex::Autolock lock(mCallbackMutex); |
Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 572 | callback = mCallback; |
| 573 | |
Jamie Gennis | 0a645cc | 2013-10-14 20:52:46 -0700 | [diff] [blame] | 574 | if (mTraceVsync) { |
| 575 | mValue = (mValue + 1) % 2; |
Andy McFadden | 5167ec6 | 2014-05-22 13:08:43 -0700 | [diff] [blame] | 576 | ATRACE_INT(mVsyncEventLabel.string(), mValue); |
Jamie Gennis | 0a645cc | 2013-10-14 20:52:46 -0700 | [diff] [blame] | 577 | } |
Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 578 | } |
| 579 | |
Peiyong Lin | 566a3b4 | 2018-01-09 18:22:43 -0800 | [diff] [blame] | 580 | if (callback != nullptr) { |
Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 581 | callback->onVSyncEvent(when); |
| 582 | } |
| 583 | } |
| 584 | |
Tim Murray | 4a4e4a2 | 2016-04-19 16:29:23 +0000 | [diff] [blame] | 585 | const char* const mName; |
| 586 | |
Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 587 | int mValue; |
| 588 | |
Jamie Gennis | 0a645cc | 2013-10-14 20:52:46 -0700 | [diff] [blame] | 589 | const bool mTraceVsync; |
Andy McFadden | 5167ec6 | 2014-05-22 13:08:43 -0700 | [diff] [blame] | 590 | const String8 mVsyncOnLabel; |
| 591 | const String8 mVsyncEventLabel; |
Jamie Gennis | 0a645cc | 2013-10-14 20:52:46 -0700 | [diff] [blame] | 592 | |
Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 593 | DispSync* mDispSync; |
Dan Stoza | db4ac3c | 2015-04-14 11:34:01 -0700 | [diff] [blame] | 594 | |
| 595 | Mutex mCallbackMutex; // Protects the following |
Lloyd Pique | e83f931 | 2018-02-01 12:53:17 -0800 | [diff] [blame] | 596 | VSyncSource::Callback* mCallback = nullptr; |
Dan Stoza | db4ac3c | 2015-04-14 11:34:01 -0700 | [diff] [blame] | 597 | |
| 598 | Mutex mVsyncMutex; // Protects the following |
| 599 | nsecs_t mPhaseOffset; |
| 600 | bool mEnabled; |
Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 601 | }; |
| 602 | |
Lloyd Pique | e83f931 | 2018-02-01 12:53:17 -0800 | [diff] [blame] | 603 | class InjectVSyncSource final : public VSyncSource { |
Sahil Dhanju | c1ba5c4 | 2016-06-07 20:09:20 -0700 | [diff] [blame] | 604 | public: |
Lloyd Pique | e83f931 | 2018-02-01 12:53:17 -0800 | [diff] [blame] | 605 | InjectVSyncSource() = default; |
| 606 | ~InjectVSyncSource() override = default; |
Sahil Dhanju | c1ba5c4 | 2016-06-07 20:09:20 -0700 | [diff] [blame] | 607 | |
Lloyd Pique | e83f931 | 2018-02-01 12:53:17 -0800 | [diff] [blame] | 608 | void setCallback(VSyncSource::Callback* callback) override { |
Sahil Dhanju | c1ba5c4 | 2016-06-07 20:09:20 -0700 | [diff] [blame] | 609 | std::lock_guard<std::mutex> lock(mCallbackMutex); |
| 610 | mCallback = callback; |
| 611 | } |
| 612 | |
Lloyd Pique | e83f931 | 2018-02-01 12:53:17 -0800 | [diff] [blame] | 613 | void onInjectSyncEvent(nsecs_t when) { |
Sahil Dhanju | c1ba5c4 | 2016-06-07 20:09:20 -0700 | [diff] [blame] | 614 | std::lock_guard<std::mutex> lock(mCallbackMutex); |
Chia-I Wu | 90f669f | 2017-10-05 14:24:41 -0700 | [diff] [blame] | 615 | if (mCallback) { |
| 616 | mCallback->onVSyncEvent(when); |
| 617 | } |
Sahil Dhanju | c1ba5c4 | 2016-06-07 20:09:20 -0700 | [diff] [blame] | 618 | } |
| 619 | |
Lloyd Pique | e83f931 | 2018-02-01 12:53:17 -0800 | [diff] [blame] | 620 | void setVSyncEnabled(bool) override {} |
| 621 | void setPhaseOffset(nsecs_t) override {} |
Sahil Dhanju | c1ba5c4 | 2016-06-07 20:09:20 -0700 | [diff] [blame] | 622 | |
| 623 | private: |
| 624 | std::mutex mCallbackMutex; // Protects the following |
Lloyd Pique | e83f931 | 2018-02-01 12:53:17 -0800 | [diff] [blame] | 625 | VSyncSource::Callback* mCallback = nullptr; |
Sahil Dhanju | c1ba5c4 | 2016-06-07 20:09:20 -0700 | [diff] [blame] | 626 | }; |
| 627 | |
Wei Wang | f9b05ee | 2017-07-19 20:59:39 -0700 | [diff] [blame] | 628 | // Do not call property_set on main thread which will be blocked by init |
| 629 | // Use StartPropertySetThread instead. |
Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 630 | void SurfaceFlinger::init() { |
Mathias Agopian | a491260 | 2012-07-12 14:25:33 -0700 | [diff] [blame] | 631 | ALOGI( "SurfaceFlinger's main thread ready to run. " |
| 632 | "Initializing graphics H/W..."); |
| 633 | |
Thierry Strudel | 2924d01 | 2017-08-14 15:19:37 -0700 | [diff] [blame] | 634 | ALOGI("Phase offest NS: %" PRId64 "", vsyncPhaseOffsetNs); |
Jiyong Park | 4b20c2e | 2017-01-14 19:45:11 +0900 | [diff] [blame] | 635 | |
Steven Thomas | b02664d | 2017-07-26 18:48:28 -0700 | [diff] [blame] | 636 | Mutex::Autolock _l(mStateLock); |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 637 | |
Steven Thomas | b02664d | 2017-07-26 18:48:28 -0700 | [diff] [blame] | 638 | // start the EventThread |
Lloyd Pique | 0fcde1b | 2017-12-20 16:50:21 -0800 | [diff] [blame] | 639 | mEventThreadSource = |
| 640 | std::make_unique<DispSyncSource>(&mPrimaryDispSync, SurfaceFlinger::vsyncPhaseOffsetNs, |
| 641 | true, "app"); |
| 642 | mEventThread = std::make_unique<impl::EventThread>(mEventThreadSource.get(), *this, false, |
| 643 | "appEventThread"); |
| 644 | mSfEventThreadSource = |
| 645 | std::make_unique<DispSyncSource>(&mPrimaryDispSync, |
| 646 | SurfaceFlinger::sfVsyncPhaseOffsetNs, true, "sf"); |
Lloyd Pique | 3fcdef1 | 2018-01-22 17:14:00 -0800 | [diff] [blame] | 647 | |
Lloyd Pique | 0fcde1b | 2017-12-20 16:50:21 -0800 | [diff] [blame] | 648 | mSFEventThread = std::make_unique<impl::EventThread>(mSfEventThreadSource.get(), *this, true, |
| 649 | "sfEventThread"); |
Lloyd Pique | 3fcdef1 | 2018-01-22 17:14:00 -0800 | [diff] [blame] | 650 | mEventQueue->setEventThread(mSFEventThread.get()); |
Dan Stoza | 84d619e | 2018-03-28 17:07:36 -0700 | [diff] [blame] | 651 | mVsyncModulator.setEventThread(mSFEventThread.get()); |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 652 | |
Steven Thomas | b02664d | 2017-07-26 18:48:28 -0700 | [diff] [blame] | 653 | // Get a RenderEngine for the given display / config (can't fail) |
Lloyd Pique | 144e116 | 2017-12-20 16:44:52 -0800 | [diff] [blame] | 654 | getBE().mRenderEngine = |
| 655 | RE::impl::RenderEngine::create(HAL_PIXEL_FORMAT_RGBA_8888, |
| 656 | hasWideColorDisplay |
| 657 | ? RE::RenderEngine::WIDE_COLOR_SUPPORT |
| 658 | : 0); |
David Sodman | bc81528 | 2017-11-05 18:57:52 -0800 | [diff] [blame] | 659 | LOG_ALWAYS_FATAL_IF(getBE().mRenderEngine == nullptr, "couldn't create RenderEngine"); |
Steven Thomas | b02664d | 2017-07-26 18:48:28 -0700 | [diff] [blame] | 660 | |
| 661 | LOG_ALWAYS_FATAL_IF(mVrFlingerRequestsDisplay, |
| 662 | "Starting with vr flinger active is not currently supported."); |
Lloyd Pique | a822d52 | 2017-12-20 16:42:57 -0800 | [diff] [blame] | 663 | getBE().mHwc.reset( |
| 664 | new HWComposer(std::make_unique<Hwc2::impl::Composer>(getBE().mHwcServiceName))); |
David Sodman | 105b7dc | 2017-11-04 20:28:14 -0700 | [diff] [blame] | 665 | getBE().mHwc->registerCallback(this, getBE().mComposerSequenceId); |
Lloyd Pique | ba04e62 | 2017-12-14 17:11:26 -0800 | [diff] [blame] | 666 | // Process any initial hotplug and resulting display changes. |
| 667 | processDisplayHotplugEventsLocked(); |
| 668 | LOG_ALWAYS_FATAL_IF(!getBE().mHwc->isConnected(HWC_DISPLAY_PRIMARY), |
| 669 | "Registered composer callback but didn't create the default primary display"); |
Jesse Hall | 692c723 | 2012-11-08 15:41:56 -0800 | [diff] [blame] | 670 | |
Lloyd Pique | fcd8661 | 2017-12-14 17:15:36 -0800 | [diff] [blame] | 671 | // make the default display GLContext current so that we can create textures |
| 672 | // when creating Layers (which may happens before we render something) |
| 673 | getDefaultDisplayDeviceLocked()->makeCurrent(); |
| 674 | |
Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 675 | if (useVrFlinger) { |
| 676 | auto vrFlingerRequestDisplayCallback = [this] (bool requestDisplay) { |
Steven Thomas | be6cbae | 2017-09-28 15:30:23 -0700 | [diff] [blame] | 677 | // This callback is called from the vr flinger dispatch thread. We |
| 678 | // need to call signalTransaction(), which requires holding |
| 679 | // mStateLock when we're not on the main thread. Acquiring |
| 680 | // mStateLock from the vr flinger dispatch thread might trigger a |
| 681 | // deadlock in surface flinger (see b/66916578), so post a message |
| 682 | // to be handled on the main thread instead. |
| 683 | sp<LambdaMessage> message = new LambdaMessage([=]() { |
| 684 | ALOGI("VR request display mode: requestDisplay=%d", requestDisplay); |
| 685 | mVrFlingerRequestsDisplay = requestDisplay; |
| 686 | signalTransaction(); |
| 687 | }); |
| 688 | postMessageAsync(message); |
Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 689 | }; |
David Sodman | 105b7dc | 2017-11-04 20:28:14 -0700 | [diff] [blame] | 690 | mVrFlinger = dvr::VrFlinger::Create(getBE().mHwc->getComposer(), |
| 691 | getBE().mHwc->getHwcDisplayId(HWC_DISPLAY_PRIMARY).value_or(0), |
Steven Thomas | 6e8f706 | 2017-11-22 14:15:29 -0800 | [diff] [blame] | 692 | vrFlingerRequestDisplayCallback); |
Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 693 | if (!mVrFlinger) { |
| 694 | ALOGE("Failed to start vrflinger"); |
| 695 | } |
| 696 | } |
| 697 | |
Lloyd Pique | 379adc1 | 2018-01-22 17:31:47 -0800 | [diff] [blame] | 698 | mEventControlThread = std::make_unique<impl::EventControlThread>( |
| 699 | [this](bool enabled) { setVsyncEnabled(HWC_DISPLAY_PRIMARY, enabled); }); |
Jamie Gennis | d170075 | 2013-10-14 12:22:52 -0700 | [diff] [blame] | 700 | |
Mathias Agopian | 92a979a | 2012-08-02 18:32:23 -0700 | [diff] [blame] | 701 | // initialize our drawing state |
| 702 | mDrawingState = mCurrentState; |
Mathias Agopian | 8630320 | 2012-07-24 22:46:10 -0700 | [diff] [blame] | 703 | |
Andy McFadden | 13a082e | 2012-08-24 10:16:42 -0700 | [diff] [blame] | 704 | // set initial conditions (e.g. unblank default device) |
| 705 | initializeDisplays(); |
| 706 | |
David Sodman | bc81528 | 2017-11-05 18:57:52 -0800 | [diff] [blame] | 707 | getBE().mRenderEngine->primeCache(); |
Dan Stoza | 4e63777 | 2016-07-28 13:31:51 -0700 | [diff] [blame] | 708 | |
Wei Wang | f9b05ee | 2017-07-19 20:59:39 -0700 | [diff] [blame] | 709 | // Inform native graphics APIs whether the present timestamp is supported: |
| 710 | if (getHwComposer().hasCapability( |
| 711 | HWC2::Capability::PresentFenceIsNotReliable)) { |
| 712 | mStartPropertySetThread = new StartPropertySetThread(false); |
| 713 | } else { |
| 714 | mStartPropertySetThread = new StartPropertySetThread(true); |
| 715 | } |
| 716 | |
| 717 | if (mStartPropertySetThread->Start() != NO_ERROR) { |
| 718 | ALOGE("Run StartPropertySetThread failed!"); |
Wei Wang | b254fa3 | 2017-01-31 17:43:23 -0800 | [diff] [blame] | 719 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 720 | |
Peiyong Lin | dd9b2ae | 2018-03-01 16:22:45 -0800 | [diff] [blame] | 721 | mLegacySrgbSaturationMatrix = getBE().mHwc->getDataspaceSaturationMatrix(HWC_DISPLAY_PRIMARY, |
| 722 | Dataspace::SRGB_LINEAR); |
| 723 | |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 724 | ALOGV("Done initializing"); |
Mathias Agopian | 3ee454a | 2012-08-27 16:28:24 -0700 | [diff] [blame] | 725 | } |
| 726 | |
Romain Guy | 11d63f4 | 2017-07-20 12:47:14 -0700 | [diff] [blame] | 727 | void SurfaceFlinger::readPersistentProperties() { |
Chia-I Wu | 28f320b | 2018-05-03 11:02:56 -0700 | [diff] [blame] | 728 | Mutex::Autolock _l(mStateLock); |
| 729 | |
Romain Guy | 11d63f4 | 2017-07-20 12:47:14 -0700 | [diff] [blame] | 730 | char value[PROPERTY_VALUE_MAX]; |
| 731 | |
| 732 | property_get("persist.sys.sf.color_saturation", value, "1.0"); |
Peiyong Lin | dd9b2ae | 2018-03-01 16:22:45 -0800 | [diff] [blame] | 733 | mGlobalSaturationFactor = atof(value); |
Chia-I Wu | 28f320b | 2018-05-03 11:02:56 -0700 | [diff] [blame] | 734 | updateColorMatrixLocked(); |
Peiyong Lin | dd9b2ae | 2018-03-01 16:22:45 -0800 | [diff] [blame] | 735 | ALOGV("Saturation is set to %.2f", mGlobalSaturationFactor); |
Romain Guy | 54f154a | 2017-10-24 21:40:32 +0100 | [diff] [blame] | 736 | |
| 737 | property_get("persist.sys.sf.native_mode", value, "0"); |
Peiyong Lin | dd9b2ae | 2018-03-01 16:22:45 -0800 | [diff] [blame] | 738 | mDisplayColorSetting = toDisplayColorSetting(atoi(value)); |
| 739 | ALOGV("Display Color Setting is set to %s.", |
| 740 | decodeDisplayColorSetting(mDisplayColorSetting).c_str()); |
Romain Guy | 11d63f4 | 2017-07-20 12:47:14 -0700 | [diff] [blame] | 741 | } |
| 742 | |
Mathias Agopian | a67e418 | 2012-06-19 17:26:12 -0700 | [diff] [blame] | 743 | void SurfaceFlinger::startBootAnim() { |
Wei Wang | b254fa3 | 2017-01-31 17:43:23 -0800 | [diff] [blame] | 744 | // Start boot animation service by setting a property mailbox |
| 745 | // if property setting thread is already running, Start() will be just a NOP |
Wei Wang | f9b05ee | 2017-07-19 20:59:39 -0700 | [diff] [blame] | 746 | mStartPropertySetThread->Start(); |
Wei Wang | b254fa3 | 2017-01-31 17:43:23 -0800 | [diff] [blame] | 747 | // Wait until property was set |
Wei Wang | f9b05ee | 2017-07-19 20:59:39 -0700 | [diff] [blame] | 748 | if (mStartPropertySetThread->join() != NO_ERROR) { |
| 749 | ALOGE("Join StartPropertySetThread failed!"); |
Wei Wang | b254fa3 | 2017-01-31 17:43:23 -0800 | [diff] [blame] | 750 | } |
Mathias Agopian | a67e418 | 2012-06-19 17:26:12 -0700 | [diff] [blame] | 751 | } |
| 752 | |
Mathias Agopian | 875d8e1 | 2013-06-07 15:35:48 -0700 | [diff] [blame] | 753 | size_t SurfaceFlinger::getMaxTextureSize() const { |
David Sodman | bc81528 | 2017-11-05 18:57:52 -0800 | [diff] [blame] | 754 | return getBE().mRenderEngine->getMaxTextureSize(); |
Mathias Agopian | a491260 | 2012-07-12 14:25:33 -0700 | [diff] [blame] | 755 | } |
| 756 | |
Mathias Agopian | 875d8e1 | 2013-06-07 15:35:48 -0700 | [diff] [blame] | 757 | size_t SurfaceFlinger::getMaxViewportDims() const { |
David Sodman | bc81528 | 2017-11-05 18:57:52 -0800 | [diff] [blame] | 758 | return getBE().mRenderEngine->getMaxViewportDims(); |
Mathias Agopian | a491260 | 2012-07-12 14:25:33 -0700 | [diff] [blame] | 759 | } |
| 760 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 761 | // ---------------------------------------------------------------------------- |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 762 | |
Jamie Gennis | 582270d | 2011-08-17 18:19:00 -0700 | [diff] [blame] | 763 | bool SurfaceFlinger::authenticateSurfaceTexture( |
Andy McFadden | 2adaf04 | 2012-12-18 09:49:45 -0800 | [diff] [blame] | 764 | const sp<IGraphicBufferProducer>& bufferProducer) const { |
Jamie Gennis | 134f042 | 2011-03-08 12:18:54 -0800 | [diff] [blame] | 765 | Mutex::Autolock _l(mStateLock); |
Robert Carr | 0d48072 | 2017-01-10 16:42:54 -0800 | [diff] [blame] | 766 | return authenticateSurfaceTextureLocked(bufferProducer); |
| 767 | } |
| 768 | |
| 769 | bool SurfaceFlinger::authenticateSurfaceTextureLocked( |
| 770 | const sp<IGraphicBufferProducer>& bufferProducer) const { |
Marco Nelissen | 097ca27 | 2014-11-14 08:01:01 -0800 | [diff] [blame] | 771 | sp<IBinder> surfaceTextureBinder(IInterface::asBinder(bufferProducer)); |
Dan Stoza | 101d8dc | 2018-02-27 15:42:25 -0800 | [diff] [blame] | 772 | return mGraphicBufferProducerList.count(surfaceTextureBinder.get()) > 0; |
Jamie Gennis | 134f042 | 2011-03-08 12:18:54 -0800 | [diff] [blame] | 773 | } |
| 774 | |
Brian Anderson | 6b37671 | 2017-04-04 10:51:39 -0700 | [diff] [blame] | 775 | status_t SurfaceFlinger::getSupportedFrameTimestamps( |
| 776 | std::vector<FrameEvent>* outSupported) const { |
| 777 | *outSupported = { |
| 778 | FrameEvent::REQUESTED_PRESENT, |
| 779 | FrameEvent::ACQUIRE, |
| 780 | FrameEvent::LATCH, |
| 781 | FrameEvent::FIRST_REFRESH_START, |
| 782 | FrameEvent::LAST_REFRESH_START, |
| 783 | FrameEvent::GPU_COMPOSITION_DONE, |
| 784 | FrameEvent::DEQUEUE_READY, |
| 785 | FrameEvent::RELEASE, |
| 786 | }; |
Steven Thomas | 6d8110b | 2017-08-31 18:24:21 -0700 | [diff] [blame] | 787 | ConditionalLock _l(mStateLock, |
| 788 | std::this_thread::get_id() != mMainThreadId); |
Brian Anderson | 6b37671 | 2017-04-04 10:51:39 -0700 | [diff] [blame] | 789 | if (!getHwComposer().hasCapability( |
| 790 | HWC2::Capability::PresentFenceIsNotReliable)) { |
| 791 | outSupported->push_back(FrameEvent::DISPLAY_PRESENT); |
| 792 | } |
| 793 | return NO_ERROR; |
| 794 | } |
| 795 | |
Dan Stoza | 7f7da32 | 2014-05-02 15:26:25 -0700 | [diff] [blame] | 796 | status_t SurfaceFlinger::getDisplayConfigs(const sp<IBinder>& display, |
| 797 | Vector<DisplayInfo>* configs) { |
Peiyong Lin | 566a3b4 | 2018-01-09 18:22:43 -0800 | [diff] [blame] | 798 | if (configs == nullptr || display.get() == nullptr) { |
Dan Stoza | 7f7da32 | 2014-05-02 15:26:25 -0700 | [diff] [blame] | 799 | return BAD_VALUE; |
| 800 | } |
| 801 | |
Naseer Ahmed | 7aa0c47 | 2014-11-03 14:49:23 -0500 | [diff] [blame] | 802 | if (!display.get()) |
| 803 | return NAME_NOT_FOUND; |
| 804 | |
Jesse Hall | 692c723 | 2012-11-08 15:41:56 -0800 | [diff] [blame] | 805 | int32_t type = NAME_NOT_FOUND; |
Jesse Hall | 9e663de | 2013-08-16 14:28:37 -0700 | [diff] [blame] | 806 | for (int i=0 ; i<DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES ; i++) { |
Jesse Hall | 692c723 | 2012-11-08 15:41:56 -0800 | [diff] [blame] | 807 | if (display == mBuiltinDisplays[i]) { |
Mathias Agopian | 1604f77 | 2012-09-18 21:54:42 -0700 | [diff] [blame] | 808 | type = i; |
| 809 | break; |
| 810 | } |
| 811 | } |
| 812 | |
| 813 | if (type < 0) { |
| 814 | return type; |
Mathias Agopian | c666cae | 2012-07-25 18:56:13 -0700 | [diff] [blame] | 815 | } |
Mathias Agopian | 8b736f1 | 2012-08-13 17:54:26 -0700 | [diff] [blame] | 816 | |
Mathias Agopian | 8b736f1 | 2012-08-13 17:54:26 -0700 | [diff] [blame] | 817 | // TODO: Not sure if display density should handled by SF any longer |
| 818 | class Density { |
| 819 | static int getDensityFromProperty(char const* propName) { |
| 820 | char property[PROPERTY_VALUE_MAX]; |
| 821 | int density = 0; |
Peiyong Lin | 566a3b4 | 2018-01-09 18:22:43 -0800 | [diff] [blame] | 822 | if (property_get(propName, property, nullptr) > 0) { |
Mathias Agopian | 8b736f1 | 2012-08-13 17:54:26 -0700 | [diff] [blame] | 823 | density = atoi(property); |
| 824 | } |
| 825 | return density; |
| 826 | } |
| 827 | public: |
| 828 | static int getEmuDensity() { |
| 829 | return getDensityFromProperty("qemu.sf.lcd_density"); } |
| 830 | static int getBuildDensity() { |
| 831 | return getDensityFromProperty("ro.sf.lcd_density"); } |
| 832 | }; |
Mathias Agopian | 1604f77 | 2012-09-18 21:54:42 -0700 | [diff] [blame] | 833 | |
Dan Stoza | 7f7da32 | 2014-05-02 15:26:25 -0700 | [diff] [blame] | 834 | configs->clear(); |
Mathias Agopian | 1604f77 | 2012-09-18 21:54:42 -0700 | [diff] [blame] | 835 | |
Steven Thomas | 6d8110b | 2017-08-31 18:24:21 -0700 | [diff] [blame] | 836 | ConditionalLock _l(mStateLock, |
| 837 | std::this_thread::get_id() != mMainThreadId); |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 838 | for (const auto& hwConfig : getHwComposer().getConfigs(type)) { |
Dan Stoza | 7f7da32 | 2014-05-02 15:26:25 -0700 | [diff] [blame] | 839 | DisplayInfo info = DisplayInfo(); |
| 840 | |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 841 | float xdpi = hwConfig->getDpiX(); |
| 842 | float ydpi = hwConfig->getDpiY(); |
Dan Stoza | 7f7da32 | 2014-05-02 15:26:25 -0700 | [diff] [blame] | 843 | |
| 844 | if (type == DisplayDevice::DISPLAY_PRIMARY) { |
| 845 | // The density of the device is provided by a build property |
| 846 | float density = Density::getBuildDensity() / 160.0f; |
| 847 | if (density == 0) { |
| 848 | // the build doesn't provide a density -- this is wrong! |
| 849 | // use xdpi instead |
| 850 | ALOGE("ro.sf.lcd_density must be defined as a build property"); |
| 851 | density = xdpi / 160.0f; |
| 852 | } |
| 853 | if (Density::getEmuDensity()) { |
| 854 | // if "qemu.sf.lcd_density" is specified, it overrides everything |
| 855 | xdpi = ydpi = density = Density::getEmuDensity(); |
| 856 | density /= 160.0f; |
| 857 | } |
| 858 | info.density = density; |
| 859 | |
| 860 | // TODO: this needs to go away (currently needed only by webkit) |
Steven Thomas | 6d8110b | 2017-08-31 18:24:21 -0700 | [diff] [blame] | 861 | sp<const DisplayDevice> hw(getDefaultDisplayDeviceLocked()); |
Lloyd Pique | 2ae2b3b | 2017-12-14 17:18:17 -0800 | [diff] [blame] | 862 | info.orientation = hw ? hw->getOrientation() : 0; |
Dan Stoza | 7f7da32 | 2014-05-02 15:26:25 -0700 | [diff] [blame] | 863 | } else { |
| 864 | // TODO: where should this value come from? |
| 865 | static const int TV_DENSITY = 213; |
| 866 | info.density = TV_DENSITY / 160.0f; |
| 867 | info.orientation = 0; |
| 868 | } |
| 869 | |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 870 | info.w = hwConfig->getWidth(); |
| 871 | info.h = hwConfig->getHeight(); |
Dan Stoza | 7f7da32 | 2014-05-02 15:26:25 -0700 | [diff] [blame] | 872 | info.xdpi = xdpi; |
| 873 | info.ydpi = ydpi; |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 874 | info.fps = 1e9 / hwConfig->getVsyncPeriod(); |
Jiyong Park | 4b20c2e | 2017-01-14 19:45:11 +0900 | [diff] [blame] | 875 | info.appVsyncOffset = vsyncPhaseOffsetNs; |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 876 | |
Andy McFadden | 91b2ca8 | 2014-06-13 14:04:23 -0700 | [diff] [blame] | 877 | // This is how far in advance a buffer must be queued for |
| 878 | // presentation at a given time. If you want a buffer to appear |
| 879 | // on the screen at time N, you must submit the buffer before |
| 880 | // (N - presentationDeadline). |
| 881 | // |
| 882 | // Normally it's one full refresh period (to give SF a chance to |
| 883 | // latch the buffer), but this can be reduced by configuring a |
| 884 | // DispSync offset. Any additional delays introduced by the hardware |
| 885 | // composer or panel must be accounted for here. |
| 886 | // |
| 887 | // We add an additional 1ms to allow for processing time and |
| 888 | // differences between the ideal and actual refresh rate. |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 889 | info.presentationDeadline = hwConfig->getVsyncPeriod() - |
Fabien Sanglard | 0cc1938 | 2017-03-06 11:54:40 -0800 | [diff] [blame] | 890 | sfVsyncPhaseOffsetNs + 1000000; |
Dan Stoza | 7f7da32 | 2014-05-02 15:26:25 -0700 | [diff] [blame] | 891 | |
| 892 | // All non-virtual displays are currently considered secure. |
| 893 | info.secure = true; |
| 894 | |
Michael Wright | 28f24d0 | 2016-07-12 13:30:53 -0700 | [diff] [blame] | 895 | configs->push_back(info); |
Mathias Agopian | 8b736f1 | 2012-08-13 17:54:26 -0700 | [diff] [blame] | 896 | } |
| 897 | |
Dan Stoza | 7f7da32 | 2014-05-02 15:26:25 -0700 | [diff] [blame] | 898 | return NO_ERROR; |
| 899 | } |
Jamie Gennis | dd3cb84 | 2012-10-19 18:19:11 -0700 | [diff] [blame] | 900 | |
Andreas Gampe | 89fd4f7 | 2014-11-13 14:18:56 -0800 | [diff] [blame] | 901 | status_t SurfaceFlinger::getDisplayStats(const sp<IBinder>& /* display */, |
Lajos Molnar | 67d8bd6 | 2014-09-11 14:58:45 -0700 | [diff] [blame] | 902 | DisplayStatInfo* stats) { |
Peiyong Lin | 566a3b4 | 2018-01-09 18:22:43 -0800 | [diff] [blame] | 903 | if (stats == nullptr) { |
Lajos Molnar | 67d8bd6 | 2014-09-11 14:58:45 -0700 | [diff] [blame] | 904 | return BAD_VALUE; |
| 905 | } |
| 906 | |
| 907 | // FIXME for now we always return stats for the primary display |
| 908 | memset(stats, 0, sizeof(*stats)); |
| 909 | stats->vsyncTime = mPrimaryDispSync.computeNextRefresh(0); |
| 910 | stats->vsyncPeriod = mPrimaryDispSync.getPeriod(); |
| 911 | return NO_ERROR; |
| 912 | } |
| 913 | |
Michael Lentine | 6c9e34a | 2014-07-14 13:48:55 -0700 | [diff] [blame] | 914 | int SurfaceFlinger::getActiveConfig(const sp<IBinder>& display) { |
Peiyong Lin | 566a3b4 | 2018-01-09 18:22:43 -0800 | [diff] [blame] | 915 | if (display == nullptr) { |
| 916 | ALOGE("%s : display is nullptr", __func__); |
Jinguang Dong | 9f8b9ae | 2016-11-29 13:55:57 +0800 | [diff] [blame] | 917 | return BAD_VALUE; |
| 918 | } |
Stephen Kiazyk | 7d3dcb9 | 2017-04-05 16:46:49 -0700 | [diff] [blame] | 919 | |
| 920 | sp<const DisplayDevice> device(getDisplayDevice(display)); |
Peiyong Lin | 566a3b4 | 2018-01-09 18:22:43 -0800 | [diff] [blame] | 921 | if (device != nullptr) { |
Dan Stoza | 24a42e9 | 2015-03-09 10:04:11 -0700 | [diff] [blame] | 922 | return device->getActiveConfig(); |
| 923 | } |
Stephen Kiazyk | 7d3dcb9 | 2017-04-05 16:46:49 -0700 | [diff] [blame] | 924 | |
Dan Stoza | 24a42e9 | 2015-03-09 10:04:11 -0700 | [diff] [blame] | 925 | return BAD_VALUE; |
Dan Stoza | 7f7da32 | 2014-05-02 15:26:25 -0700 | [diff] [blame] | 926 | } |
Jamie Gennis | dd3cb84 | 2012-10-19 18:19:11 -0700 | [diff] [blame] | 927 | |
Michael Lentine | 6c9e34a | 2014-07-14 13:48:55 -0700 | [diff] [blame] | 928 | void SurfaceFlinger::setActiveConfigInternal(const sp<DisplayDevice>& hw, int mode) { |
| 929 | ALOGD("Set active config mode=%d, type=%d flinger=%p", mode, hw->getDisplayType(), |
| 930 | this); |
Michael Lentine | 6c9e34a | 2014-07-14 13:48:55 -0700 | [diff] [blame] | 931 | int currentMode = hw->getActiveConfig(); |
| 932 | |
| 933 | if (mode == currentMode) { |
| 934 | ALOGD("Screen type=%d is already mode=%d", hw->getDisplayType(), mode); |
| 935 | return; |
| 936 | } |
| 937 | |
Dominik Laskowski | 281644e | 2018-04-19 15:47:35 -0700 | [diff] [blame] | 938 | if (hw->isVirtual()) { |
Michael Lentine | 6c9e34a | 2014-07-14 13:48:55 -0700 | [diff] [blame] | 939 | ALOGW("Trying to set config for virtual display"); |
| 940 | return; |
| 941 | } |
| 942 | |
| 943 | hw->setActiveConfig(mode); |
Dominik Laskowski | 281644e | 2018-04-19 15:47:35 -0700 | [diff] [blame] | 944 | getHwComposer().setActiveConfig(hw->getDisplayType(), mode); |
Michael Lentine | 6c9e34a | 2014-07-14 13:48:55 -0700 | [diff] [blame] | 945 | } |
| 946 | |
| 947 | status_t SurfaceFlinger::setActiveConfig(const sp<IBinder>& display, int mode) { |
| 948 | class MessageSetActiveConfig: public MessageBase { |
| 949 | SurfaceFlinger& mFlinger; |
| 950 | sp<IBinder> mDisplay; |
| 951 | int mMode; |
| 952 | public: |
| 953 | MessageSetActiveConfig(SurfaceFlinger& flinger, const sp<IBinder>& disp, |
| 954 | int mode) : |
| 955 | mFlinger(flinger), mDisplay(disp) { mMode = mode; } |
| 956 | virtual bool handler() { |
Michael Lentine | 7306c67 | 2014-07-30 13:00:37 -0700 | [diff] [blame] | 957 | Vector<DisplayInfo> configs; |
| 958 | mFlinger.getDisplayConfigs(mDisplay, &configs); |
Jesse Hall | 7844211 | 2014-08-07 22:43:06 -0700 | [diff] [blame] | 959 | if (mMode < 0 || mMode >= static_cast<int>(configs.size())) { |
Michael Lentine | 9ae79d8 | 2014-07-30 16:42:12 -0700 | [diff] [blame] | 960 | ALOGE("Attempt to set active config = %d for display with %zu configs", |
Michael Lentine | 7306c67 | 2014-07-30 13:00:37 -0700 | [diff] [blame] | 961 | mMode, configs.size()); |
Michael Wright | 28f24d0 | 2016-07-12 13:30:53 -0700 | [diff] [blame] | 962 | return true; |
Michael Lentine | 7306c67 | 2014-07-30 13:00:37 -0700 | [diff] [blame] | 963 | } |
Michael Lentine | 6c9e34a | 2014-07-14 13:48:55 -0700 | [diff] [blame] | 964 | sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay)); |
Peiyong Lin | 566a3b4 | 2018-01-09 18:22:43 -0800 | [diff] [blame] | 965 | if (hw == nullptr) { |
Michael Lentine | 6c9e34a | 2014-07-14 13:48:55 -0700 | [diff] [blame] | 966 | ALOGE("Attempt to set active config = %d for null display %p", |
Michael Lentine | 7306c67 | 2014-07-30 13:00:37 -0700 | [diff] [blame] | 967 | mMode, mDisplay.get()); |
Dominik Laskowski | 281644e | 2018-04-19 15:47:35 -0700 | [diff] [blame] | 968 | } else if (hw->isVirtual()) { |
Michael Lentine | 6c9e34a | 2014-07-14 13:48:55 -0700 | [diff] [blame] | 969 | ALOGW("Attempt to set active config = %d for virtual display", |
| 970 | mMode); |
| 971 | } else { |
| 972 | mFlinger.setActiveConfigInternal(hw, mMode); |
| 973 | } |
| 974 | return true; |
| 975 | } |
| 976 | }; |
| 977 | sp<MessageBase> msg = new MessageSetActiveConfig(*this, display, mode); |
| 978 | postMessageSync(msg); |
Mathias Agopian | 888c822 | 2012-08-04 21:10:38 -0700 | [diff] [blame] | 979 | return NO_ERROR; |
Mathias Agopian | c666cae | 2012-07-25 18:56:13 -0700 | [diff] [blame] | 980 | } |
Michael Wright | 28f24d0 | 2016-07-12 13:30:53 -0700 | [diff] [blame] | 981 | status_t SurfaceFlinger::getDisplayColorModes(const sp<IBinder>& display, |
Peiyong Lin | a52f029 | 2018-03-14 17:26:31 -0700 | [diff] [blame] | 982 | Vector<ColorMode>* outColorModes) { |
Michael Wright | 28f24d0 | 2016-07-12 13:30:53 -0700 | [diff] [blame] | 983 | if ((outColorModes == nullptr) || (display.get() == nullptr)) { |
| 984 | return BAD_VALUE; |
| 985 | } |
| 986 | |
| 987 | if (!display.get()) { |
| 988 | return NAME_NOT_FOUND; |
| 989 | } |
| 990 | |
| 991 | int32_t type = NAME_NOT_FOUND; |
| 992 | for (int i=0 ; i<DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES ; i++) { |
| 993 | if (display == mBuiltinDisplays[i]) { |
| 994 | type = i; |
| 995 | break; |
| 996 | } |
| 997 | } |
| 998 | |
| 999 | if (type < 0) { |
| 1000 | return type; |
| 1001 | } |
| 1002 | |
Peiyong Lin | a52f029 | 2018-03-14 17:26:31 -0700 | [diff] [blame] | 1003 | std::vector<ColorMode> modes; |
Steven Thomas | 6d8110b | 2017-08-31 18:24:21 -0700 | [diff] [blame] | 1004 | { |
| 1005 | ConditionalLock _l(mStateLock, |
| 1006 | std::this_thread::get_id() != mMainThreadId); |
| 1007 | modes = getHwComposer().getColorModes(type); |
| 1008 | } |
Michael Wright | 28f24d0 | 2016-07-12 13:30:53 -0700 | [diff] [blame] | 1009 | outColorModes->clear(); |
| 1010 | std::copy(modes.cbegin(), modes.cend(), std::back_inserter(*outColorModes)); |
| 1011 | |
| 1012 | return NO_ERROR; |
| 1013 | } |
| 1014 | |
Peiyong Lin | a52f029 | 2018-03-14 17:26:31 -0700 | [diff] [blame] | 1015 | ColorMode SurfaceFlinger::getActiveColorMode(const sp<IBinder>& display) { |
Stephen Kiazyk | 7d3dcb9 | 2017-04-05 16:46:49 -0700 | [diff] [blame] | 1016 | sp<const DisplayDevice> device(getDisplayDevice(display)); |
Michael Wright | 28f24d0 | 2016-07-12 13:30:53 -0700 | [diff] [blame] | 1017 | if (device != nullptr) { |
| 1018 | return device->getActiveColorMode(); |
| 1019 | } |
Peiyong Lin | a52f029 | 2018-03-14 17:26:31 -0700 | [diff] [blame] | 1020 | return static_cast<ColorMode>(BAD_VALUE); |
Michael Wright | 28f24d0 | 2016-07-12 13:30:53 -0700 | [diff] [blame] | 1021 | } |
| 1022 | |
| 1023 | void SurfaceFlinger::setActiveColorModeInternal(const sp<DisplayDevice>& hw, |
Peiyong Lin | 136fbbc | 2018-04-17 15:09:44 -0700 | [diff] [blame^] | 1024 | ColorMode mode, Dataspace dataSpace, |
| 1025 | RenderIntent renderIntent) { |
Peiyong Lin | a52f029 | 2018-03-14 17:26:31 -0700 | [diff] [blame] | 1026 | ColorMode currentMode = hw->getActiveColorMode(); |
Peiyong Lin | dd9b2ae | 2018-03-01 16:22:45 -0800 | [diff] [blame] | 1027 | Dataspace currentDataSpace = hw->getCompositionDataSpace(); |
Peiyong Lin | 38e9a56 | 2018-04-10 16:24:20 -0700 | [diff] [blame] | 1028 | RenderIntent currentRenderIntent = hw->getActiveRenderIntent(); |
Michael Wright | 28f24d0 | 2016-07-12 13:30:53 -0700 | [diff] [blame] | 1029 | |
Peiyong Lin | 38e9a56 | 2018-04-10 16:24:20 -0700 | [diff] [blame] | 1030 | if (mode == currentMode && dataSpace == currentDataSpace && |
| 1031 | renderIntent == currentRenderIntent) { |
| 1032 | return; |
| 1033 | } |
| 1034 | |
Dominik Laskowski | 281644e | 2018-04-19 15:47:35 -0700 | [diff] [blame] | 1035 | if (hw->isVirtual()) { |
Peiyong Lin | 38e9a56 | 2018-04-10 16:24:20 -0700 | [diff] [blame] | 1036 | ALOGW("Trying to set config for virtual display"); |
| 1037 | return; |
| 1038 | } |
| 1039 | |
| 1040 | hw->setActiveColorMode(mode); |
| 1041 | hw->setCompositionDataSpace(dataSpace); |
Peiyong Lin | dd9b2ae | 2018-03-01 16:22:45 -0800 | [diff] [blame] | 1042 | hw->setActiveRenderIntent(renderIntent); |
Dominik Laskowski | 281644e | 2018-04-19 15:47:35 -0700 | [diff] [blame] | 1043 | getHwComposer().setActiveColorMode(hw->getDisplayType(), mode, renderIntent); |
Peiyong Lin | dd9b2ae | 2018-03-01 16:22:45 -0800 | [diff] [blame] | 1044 | |
| 1045 | ALOGV("Set active color mode: %s (%d), active render intent: %s (%d), type=%d", |
| 1046 | decodeColorMode(mode).c_str(), mode, |
| 1047 | decodeRenderIntent(renderIntent).c_str(), renderIntent, |
| 1048 | hw->getDisplayType()); |
Michael Wright | 28f24d0 | 2016-07-12 13:30:53 -0700 | [diff] [blame] | 1049 | } |
| 1050 | |
| 1051 | |
| 1052 | status_t SurfaceFlinger::setActiveColorMode(const sp<IBinder>& display, |
Peiyong Lin | a52f029 | 2018-03-14 17:26:31 -0700 | [diff] [blame] | 1053 | ColorMode colorMode) { |
Michael Wright | 28f24d0 | 2016-07-12 13:30:53 -0700 | [diff] [blame] | 1054 | class MessageSetActiveColorMode: public MessageBase { |
| 1055 | SurfaceFlinger& mFlinger; |
| 1056 | sp<IBinder> mDisplay; |
Peiyong Lin | a52f029 | 2018-03-14 17:26:31 -0700 | [diff] [blame] | 1057 | ColorMode mMode; |
Michael Wright | 28f24d0 | 2016-07-12 13:30:53 -0700 | [diff] [blame] | 1058 | public: |
| 1059 | MessageSetActiveColorMode(SurfaceFlinger& flinger, const sp<IBinder>& disp, |
Peiyong Lin | a52f029 | 2018-03-14 17:26:31 -0700 | [diff] [blame] | 1060 | ColorMode mode) : |
Michael Wright | 28f24d0 | 2016-07-12 13:30:53 -0700 | [diff] [blame] | 1061 | mFlinger(flinger), mDisplay(disp) { mMode = mode; } |
| 1062 | virtual bool handler() { |
Peiyong Lin | a52f029 | 2018-03-14 17:26:31 -0700 | [diff] [blame] | 1063 | Vector<ColorMode> modes; |
Michael Wright | 28f24d0 | 2016-07-12 13:30:53 -0700 | [diff] [blame] | 1064 | mFlinger.getDisplayColorModes(mDisplay, &modes); |
| 1065 | bool exists = std::find(std::begin(modes), std::end(modes), mMode) != std::end(modes); |
Peiyong Lin | a52f029 | 2018-03-14 17:26:31 -0700 | [diff] [blame] | 1066 | if (mMode < ColorMode::NATIVE || !exists) { |
Courtney Goeltzenleuchter | 5d94389 | 2017-03-22 13:46:46 -0600 | [diff] [blame] | 1067 | ALOGE("Attempt to set invalid active color mode %s (%d) for display %p", |
| 1068 | decodeColorMode(mMode).c_str(), mMode, mDisplay.get()); |
Michael Wright | 28f24d0 | 2016-07-12 13:30:53 -0700 | [diff] [blame] | 1069 | return true; |
| 1070 | } |
| 1071 | sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay)); |
| 1072 | if (hw == nullptr) { |
Courtney Goeltzenleuchter | 5d94389 | 2017-03-22 13:46:46 -0600 | [diff] [blame] | 1073 | ALOGE("Attempt to set active color mode %s (%d) for null display %p", |
| 1074 | decodeColorMode(mMode).c_str(), mMode, mDisplay.get()); |
Dominik Laskowski | 281644e | 2018-04-19 15:47:35 -0700 | [diff] [blame] | 1075 | } else if (hw->isVirtual()) { |
Courtney Goeltzenleuchter | 5d94389 | 2017-03-22 13:46:46 -0600 | [diff] [blame] | 1076 | ALOGW("Attempt to set active color mode %s %d for virtual display", |
| 1077 | decodeColorMode(mMode).c_str(), mMode); |
Michael Wright | 28f24d0 | 2016-07-12 13:30:53 -0700 | [diff] [blame] | 1078 | } else { |
Peiyong Lin | 136fbbc | 2018-04-17 15:09:44 -0700 | [diff] [blame^] | 1079 | mFlinger.setActiveColorModeInternal(hw, mMode, Dataspace::UNKNOWN, |
| 1080 | RenderIntent::COLORIMETRIC); |
Michael Wright | 28f24d0 | 2016-07-12 13:30:53 -0700 | [diff] [blame] | 1081 | } |
| 1082 | return true; |
| 1083 | } |
| 1084 | }; |
| 1085 | sp<MessageBase> msg = new MessageSetActiveColorMode(*this, display, colorMode); |
| 1086 | postMessageSync(msg); |
| 1087 | return NO_ERROR; |
| 1088 | } |
Mathias Agopian | c666cae | 2012-07-25 18:56:13 -0700 | [diff] [blame] | 1089 | |
Svetoslav | d85084b | 2014-03-20 10:28:31 -0700 | [diff] [blame] | 1090 | status_t SurfaceFlinger::clearAnimationFrameStats() { |
| 1091 | Mutex::Autolock _l(mStateLock); |
| 1092 | mAnimFrameTracker.clearStats(); |
| 1093 | return NO_ERROR; |
| 1094 | } |
| 1095 | |
| 1096 | status_t SurfaceFlinger::getAnimationFrameStats(FrameStats* outStats) const { |
| 1097 | Mutex::Autolock _l(mStateLock); |
| 1098 | mAnimFrameTracker.getStats(outStats); |
| 1099 | return NO_ERROR; |
| 1100 | } |
| 1101 | |
Dan Stoza | c4f471e | 2016-03-24 09:31:08 -0700 | [diff] [blame] | 1102 | status_t SurfaceFlinger::getHdrCapabilities(const sp<IBinder>& display, |
| 1103 | HdrCapabilities* outCapabilities) const { |
| 1104 | Mutex::Autolock _l(mStateLock); |
| 1105 | |
Stephen Kiazyk | 7d3dcb9 | 2017-04-05 16:46:49 -0700 | [diff] [blame] | 1106 | sp<const DisplayDevice> displayDevice(getDisplayDeviceLocked(display)); |
Dan Stoza | c4f471e | 2016-03-24 09:31:08 -0700 | [diff] [blame] | 1107 | if (displayDevice == nullptr) { |
| 1108 | ALOGE("getHdrCapabilities: Invalid display %p", displayDevice.get()); |
| 1109 | return BAD_VALUE; |
| 1110 | } |
| 1111 | |
Peiyong Lin | fb06930 | 2018-04-25 14:34:31 -0700 | [diff] [blame] | 1112 | // At this point the DisplayDeivce should already be set up, |
| 1113 | // meaning the luminance information is already queried from |
| 1114 | // hardware composer and stored properly. |
| 1115 | const HdrCapabilities& capabilities = displayDevice->getHdrCapabilities(); |
| 1116 | *outCapabilities = HdrCapabilities(capabilities.getSupportedHdrTypes(), |
| 1117 | capabilities.getDesiredMaxLuminance(), |
| 1118 | capabilities.getDesiredMaxAverageLuminance(), |
| 1119 | capabilities.getDesiredMinLuminance()); |
Dan Stoza | c4f471e | 2016-03-24 09:31:08 -0700 | [diff] [blame] | 1120 | |
| 1121 | return NO_ERROR; |
| 1122 | } |
| 1123 | |
Sahil Dhanju | c1ba5c4 | 2016-06-07 20:09:20 -0700 | [diff] [blame] | 1124 | status_t SurfaceFlinger::enableVSyncInjections(bool enable) { |
Chia-I Wu | 90f669f | 2017-10-05 14:24:41 -0700 | [diff] [blame] | 1125 | sp<LambdaMessage> enableVSyncInjections = new LambdaMessage([&]() { |
| 1126 | Mutex::Autolock _l(mStateLock); |
Sahil Dhanju | c1ba5c4 | 2016-06-07 20:09:20 -0700 | [diff] [blame] | 1127 | |
Chia-I Wu | 90f669f | 2017-10-05 14:24:41 -0700 | [diff] [blame] | 1128 | if (mInjectVSyncs == enable) { |
| 1129 | return; |
Sahil Dhanju | c1ba5c4 | 2016-06-07 20:09:20 -0700 | [diff] [blame] | 1130 | } |
Chia-I Wu | 90f669f | 2017-10-05 14:24:41 -0700 | [diff] [blame] | 1131 | |
| 1132 | if (enable) { |
| 1133 | ALOGV("VSync Injections enabled"); |
| 1134 | if (mVSyncInjector.get() == nullptr) { |
Lloyd Pique | e83f931 | 2018-02-01 12:53:17 -0800 | [diff] [blame] | 1135 | mVSyncInjector = std::make_unique<InjectVSyncSource>(); |
Lloyd Pique | 0fcde1b | 2017-12-20 16:50:21 -0800 | [diff] [blame] | 1136 | mInjectorEventThread = |
| 1137 | std::make_unique<impl::EventThread>(mVSyncInjector.get(), *this, false, |
| 1138 | "injEventThread"); |
Chia-I Wu | 90f669f | 2017-10-05 14:24:41 -0700 | [diff] [blame] | 1139 | } |
Lloyd Pique | 3fcdef1 | 2018-01-22 17:14:00 -0800 | [diff] [blame] | 1140 | mEventQueue->setEventThread(mInjectorEventThread.get()); |
Chia-I Wu | 90f669f | 2017-10-05 14:24:41 -0700 | [diff] [blame] | 1141 | } else { |
| 1142 | ALOGV("VSync Injections disabled"); |
Lloyd Pique | 3fcdef1 | 2018-01-22 17:14:00 -0800 | [diff] [blame] | 1143 | mEventQueue->setEventThread(mSFEventThread.get()); |
Chia-I Wu | 90f669f | 2017-10-05 14:24:41 -0700 | [diff] [blame] | 1144 | } |
| 1145 | |
Sahil Dhanju | c1ba5c4 | 2016-06-07 20:09:20 -0700 | [diff] [blame] | 1146 | mInjectVSyncs = enable; |
Chia-I Wu | 90f669f | 2017-10-05 14:24:41 -0700 | [diff] [blame] | 1147 | }); |
| 1148 | postMessageSync(enableVSyncInjections); |
Sahil Dhanju | c1ba5c4 | 2016-06-07 20:09:20 -0700 | [diff] [blame] | 1149 | return NO_ERROR; |
| 1150 | } |
| 1151 | |
| 1152 | status_t SurfaceFlinger::injectVSync(nsecs_t when) { |
Chia-I Wu | 90f669f | 2017-10-05 14:24:41 -0700 | [diff] [blame] | 1153 | Mutex::Autolock _l(mStateLock); |
| 1154 | |
Sahil Dhanju | c1ba5c4 | 2016-06-07 20:09:20 -0700 | [diff] [blame] | 1155 | if (!mInjectVSyncs) { |
| 1156 | ALOGE("VSync Injections not enabled"); |
| 1157 | return BAD_VALUE; |
| 1158 | } |
| 1159 | if (mInjectVSyncs && mInjectorEventThread.get() != nullptr) { |
| 1160 | ALOGV("Injecting VSync inside SurfaceFlinger"); |
| 1161 | mVSyncInjector->onInjectSyncEvent(when); |
| 1162 | } |
| 1163 | return NO_ERROR; |
| 1164 | } |
| 1165 | |
Lloyd Pique | 755e319 | 2018-01-31 16:46:15 -0800 | [diff] [blame] | 1166 | status_t SurfaceFlinger::getLayerDebugInfo(std::vector<LayerDebugInfo>* outLayers) const |
| 1167 | NO_THREAD_SAFETY_ANALYSIS { |
Kalle Raita | a099a24 | 2017-01-11 11:17:29 -0800 | [diff] [blame] | 1168 | IPCThreadState* ipc = IPCThreadState::self(); |
| 1169 | const int pid = ipc->getCallingPid(); |
| 1170 | const int uid = ipc->getCallingUid(); |
| 1171 | if ((uid != AID_SHELL) && |
| 1172 | !PermissionCache::checkPermission(sDump, pid, uid)) { |
| 1173 | ALOGE("Layer debug info permission denied for pid=%d, uid=%d", pid, uid); |
| 1174 | return PERMISSION_DENIED; |
| 1175 | } |
| 1176 | |
| 1177 | // Try to acquire a lock for 1s, fail gracefully |
| 1178 | const status_t err = mStateLock.timedLock(s2ns(1)); |
| 1179 | const bool locked = (err == NO_ERROR); |
| 1180 | if (!locked) { |
| 1181 | ALOGE("LayerDebugInfo: SurfaceFlinger unresponsive (%s [%d]) - exit", strerror(-err), err); |
| 1182 | return TIMED_OUT; |
| 1183 | } |
| 1184 | |
| 1185 | outLayers->clear(); |
| 1186 | mCurrentState.traverseInZOrder([&](Layer* layer) { |
| 1187 | outLayers->push_back(layer->getLayerDebugInfo()); |
| 1188 | }); |
| 1189 | |
| 1190 | mStateLock.unlock(); |
| 1191 | return NO_ERROR; |
| 1192 | } |
| 1193 | |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 1194 | // ---------------------------------------------------------------------------- |
| 1195 | |
Jorim Jaggi | b1e2f8d | 2017-06-08 15:43:59 -0700 | [diff] [blame] | 1196 | sp<IDisplayEventConnection> SurfaceFlinger::createDisplayEventConnection( |
| 1197 | ISurfaceComposer::VsyncSource vsyncSource) { |
| 1198 | if (vsyncSource == eVsyncSourceSurfaceFlinger) { |
| 1199 | return mSFEventThread->createEventConnection(); |
| 1200 | } else { |
| 1201 | return mEventThread->createEventConnection(); |
| 1202 | } |
Mathias Agopian | bb64124 | 2010-05-18 17:06:55 -0700 | [diff] [blame] | 1203 | } |
| 1204 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1205 | // ---------------------------------------------------------------------------- |
Mathias Agopian | 99ce5cd | 2012-01-31 18:24:27 -0800 | [diff] [blame] | 1206 | |
| 1207 | void SurfaceFlinger::waitForEvent() { |
Lloyd Pique | 3fcdef1 | 2018-01-22 17:14:00 -0800 | [diff] [blame] | 1208 | mEventQueue->waitMessage(); |
Mathias Agopian | 99ce5cd | 2012-01-31 18:24:27 -0800 | [diff] [blame] | 1209 | } |
| 1210 | |
| 1211 | void SurfaceFlinger::signalTransaction() { |
Lloyd Pique | 3fcdef1 | 2018-01-22 17:14:00 -0800 | [diff] [blame] | 1212 | mEventQueue->invalidate(); |
Mathias Agopian | 99ce5cd | 2012-01-31 18:24:27 -0800 | [diff] [blame] | 1213 | } |
| 1214 | |
| 1215 | void SurfaceFlinger::signalLayerUpdate() { |
Lloyd Pique | 3fcdef1 | 2018-01-22 17:14:00 -0800 | [diff] [blame] | 1216 | mEventQueue->invalidate(); |
Mathias Agopian | 99ce5cd | 2012-01-31 18:24:27 -0800 | [diff] [blame] | 1217 | } |
| 1218 | |
| 1219 | void SurfaceFlinger::signalRefresh() { |
Dan Stoza | 2b6d38e | 2017-06-01 16:40:30 -0700 | [diff] [blame] | 1220 | mRefreshPending = true; |
Lloyd Pique | 3fcdef1 | 2018-01-22 17:14:00 -0800 | [diff] [blame] | 1221 | mEventQueue->refresh(); |
Mathias Agopian | 99ce5cd | 2012-01-31 18:24:27 -0800 | [diff] [blame] | 1222 | } |
| 1223 | |
| 1224 | status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg, |
Dan Stoza | c701401 | 2014-02-14 15:03:43 -0800 | [diff] [blame] | 1225 | nsecs_t reltime, uint32_t /* flags */) { |
Lloyd Pique | 3fcdef1 | 2018-01-22 17:14:00 -0800 | [diff] [blame] | 1226 | return mEventQueue->postMessage(msg, reltime); |
Mathias Agopian | 99ce5cd | 2012-01-31 18:24:27 -0800 | [diff] [blame] | 1227 | } |
| 1228 | |
| 1229 | status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg, |
Dan Stoza | c701401 | 2014-02-14 15:03:43 -0800 | [diff] [blame] | 1230 | nsecs_t reltime, uint32_t /* flags */) { |
Lloyd Pique | 3fcdef1 | 2018-01-22 17:14:00 -0800 | [diff] [blame] | 1231 | status_t res = mEventQueue->postMessage(msg, reltime); |
Mathias Agopian | 99ce5cd | 2012-01-31 18:24:27 -0800 | [diff] [blame] | 1232 | if (res == NO_ERROR) { |
| 1233 | msg->wait(); |
| 1234 | } |
| 1235 | return res; |
| 1236 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1237 | |
Mathias Agopian | 4f4f094 | 2013-08-19 17:26:18 -0700 | [diff] [blame] | 1238 | void SurfaceFlinger::run() { |
Mathias Agopian | 4f4f094 | 2013-08-19 17:26:18 -0700 | [diff] [blame] | 1239 | do { |
| 1240 | waitForEvent(); |
| 1241 | } while (true); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1242 | } |
| 1243 | |
Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 1244 | void SurfaceFlinger::enableHardwareVsync() { |
| 1245 | Mutex::Autolock _l(mHWVsyncLock); |
Jesse Hall | 948fe0c | 2013-10-14 12:56:09 -0700 | [diff] [blame] | 1246 | if (!mPrimaryHWVsyncEnabled && mHWVsyncAvailable) { |
Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 1247 | mPrimaryDispSync.beginResync(); |
Jamie Gennis | d170075 | 2013-10-14 12:22:52 -0700 | [diff] [blame] | 1248 | //eventControl(HWC_DISPLAY_PRIMARY, SurfaceFlinger::EVENT_VSYNC, true); |
| 1249 | mEventControlThread->setVsyncEnabled(true); |
Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 1250 | mPrimaryHWVsyncEnabled = true; |
Andy McFadden | 43601a2 | 2012-09-11 15:15:13 -0700 | [diff] [blame] | 1251 | } |
Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 1252 | } |
| 1253 | |
Jesse Hall | 948fe0c | 2013-10-14 12:56:09 -0700 | [diff] [blame] | 1254 | void SurfaceFlinger::resyncToHardwareVsync(bool makeAvailable) { |
Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 1255 | Mutex::Autolock _l(mHWVsyncLock); |
| 1256 | |
Jesse Hall | 948fe0c | 2013-10-14 12:56:09 -0700 | [diff] [blame] | 1257 | if (makeAvailable) { |
| 1258 | mHWVsyncAvailable = true; |
| 1259 | } else if (!mHWVsyncAvailable) { |
Dan Stoza | 0a3c4d6 | 2016-04-19 11:56:20 -0700 | [diff] [blame] | 1260 | // Hardware vsync is not currently available, so abort the resync |
| 1261 | // attempt for now |
Jesse Hall | 948fe0c | 2013-10-14 12:56:09 -0700 | [diff] [blame] | 1262 | return; |
| 1263 | } |
| 1264 | |
David Sodman | 105b7dc | 2017-11-04 20:28:14 -0700 | [diff] [blame] | 1265 | const auto& activeConfig = getBE().mHwc->getActiveConfig(HWC_DISPLAY_PRIMARY); |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 1266 | const nsecs_t period = activeConfig->getVsyncPeriod(); |
Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 1267 | |
| 1268 | mPrimaryDispSync.reset(); |
| 1269 | mPrimaryDispSync.setPeriod(period); |
| 1270 | |
| 1271 | if (!mPrimaryHWVsyncEnabled) { |
| 1272 | mPrimaryDispSync.beginResync(); |
Jamie Gennis | d170075 | 2013-10-14 12:22:52 -0700 | [diff] [blame] | 1273 | //eventControl(HWC_DISPLAY_PRIMARY, SurfaceFlinger::EVENT_VSYNC, true); |
| 1274 | mEventControlThread->setVsyncEnabled(true); |
Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 1275 | mPrimaryHWVsyncEnabled = true; |
| 1276 | } |
| 1277 | } |
| 1278 | |
Jesse Hall | 948fe0c | 2013-10-14 12:56:09 -0700 | [diff] [blame] | 1279 | void SurfaceFlinger::disableHardwareVsync(bool makeUnavailable) { |
Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 1280 | Mutex::Autolock _l(mHWVsyncLock); |
| 1281 | if (mPrimaryHWVsyncEnabled) { |
Jamie Gennis | d170075 | 2013-10-14 12:22:52 -0700 | [diff] [blame] | 1282 | //eventControl(HWC_DISPLAY_PRIMARY, SurfaceFlinger::EVENT_VSYNC, false); |
| 1283 | mEventControlThread->setVsyncEnabled(false); |
Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 1284 | mPrimaryDispSync.endResync(); |
| 1285 | mPrimaryHWVsyncEnabled = false; |
| 1286 | } |
Jesse Hall | 948fe0c | 2013-10-14 12:56:09 -0700 | [diff] [blame] | 1287 | if (makeUnavailable) { |
| 1288 | mHWVsyncAvailable = false; |
| 1289 | } |
Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 1290 | } |
| 1291 | |
Tim Murray | 4a4e4a2 | 2016-04-19 16:29:23 +0000 | [diff] [blame] | 1292 | void SurfaceFlinger::resyncWithRateLimit() { |
| 1293 | static constexpr nsecs_t kIgnoreDelay = ms2ns(500); |
Dan Stoza | 5716430 | 2017-05-08 14:03:54 -0700 | [diff] [blame] | 1294 | |
| 1295 | // No explicit locking is needed here since EventThread holds a lock while calling this method |
| 1296 | static nsecs_t sLastResyncAttempted = 0; |
| 1297 | const nsecs_t now = systemTime(); |
| 1298 | if (now - sLastResyncAttempted > kIgnoreDelay) { |
Dan Stoza | 0a3c4d6 | 2016-04-19 11:56:20 -0700 | [diff] [blame] | 1299 | resyncToHardwareVsync(false); |
Tim Murray | 4a4e4a2 | 2016-04-19 16:29:23 +0000 | [diff] [blame] | 1300 | } |
Dan Stoza | 5716430 | 2017-05-08 14:03:54 -0700 | [diff] [blame] | 1301 | sLastResyncAttempted = now; |
Tim Murray | 4a4e4a2 | 2016-04-19 16:29:23 +0000 | [diff] [blame] | 1302 | } |
| 1303 | |
Steven Thomas | b02664d | 2017-07-26 18:48:28 -0700 | [diff] [blame] | 1304 | void SurfaceFlinger::onVsyncReceived(int32_t sequenceId, |
| 1305 | hwc2_display_t displayId, int64_t timestamp) { |
Steven Thomas | 3cfac28 | 2017-02-06 12:29:30 -0800 | [diff] [blame] | 1306 | Mutex::Autolock lock(mStateLock); |
Steven Thomas | b02664d | 2017-07-26 18:48:28 -0700 | [diff] [blame] | 1307 | // Ignore any vsyncs from a previous hardware composer. |
David Sodman | 105b7dc | 2017-11-04 20:28:14 -0700 | [diff] [blame] | 1308 | if (sequenceId != getBE().mComposerSequenceId) { |
Steven Thomas | b02664d | 2017-07-26 18:48:28 -0700 | [diff] [blame] | 1309 | return; |
| 1310 | } |
| 1311 | |
| 1312 | int32_t type; |
David Sodman | 105b7dc | 2017-11-04 20:28:14 -0700 | [diff] [blame] | 1313 | if (!getBE().mHwc->onVsync(displayId, timestamp, &type)) { |
Steven Thomas | 3cfac28 | 2017-02-06 12:29:30 -0800 | [diff] [blame] | 1314 | return; |
| 1315 | } |
| 1316 | |
Jamie Gennis | d170075 | 2013-10-14 12:22:52 -0700 | [diff] [blame] | 1317 | bool needsHwVsync = false; |
Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 1318 | |
Jamie Gennis | d170075 | 2013-10-14 12:22:52 -0700 | [diff] [blame] | 1319 | { // Scope for the lock |
| 1320 | Mutex::Autolock _l(mHWVsyncLock); |
Steven Thomas | b02664d | 2017-07-26 18:48:28 -0700 | [diff] [blame] | 1321 | if (type == DisplayDevice::DISPLAY_PRIMARY && mPrimaryHWVsyncEnabled) { |
Jamie Gennis | d170075 | 2013-10-14 12:22:52 -0700 | [diff] [blame] | 1322 | needsHwVsync = mPrimaryDispSync.addResyncSample(timestamp); |
Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 1323 | } |
Mathias Agopian | 148994e | 2012-09-19 17:31:36 -0700 | [diff] [blame] | 1324 | } |
Jamie Gennis | d170075 | 2013-10-14 12:22:52 -0700 | [diff] [blame] | 1325 | |
| 1326 | if (needsHwVsync) { |
| 1327 | enableHardwareVsync(); |
| 1328 | } else { |
| 1329 | disableHardwareVsync(false); |
| 1330 | } |
Mathias Agopian | 148994e | 2012-09-19 17:31:36 -0700 | [diff] [blame] | 1331 | } |
| 1332 | |
Brian Anderson | 0a61b0c | 2016-12-07 14:55:56 -0800 | [diff] [blame] | 1333 | void SurfaceFlinger::getCompositorTiming(CompositorTiming* compositorTiming) { |
David Sodman | 99974d2 | 2017-11-28 12:04:33 -0800 | [diff] [blame] | 1334 | std::lock_guard<std::mutex> lock(getBE().mCompositorTimingLock); |
| 1335 | *compositorTiming = getBE().mCompositorTiming; |
Brian Anderson | 0a61b0c | 2016-12-07 14:55:56 -0800 | [diff] [blame] | 1336 | } |
| 1337 | |
Lloyd Pique | 715a2c1 | 2017-12-14 17:18:08 -0800 | [diff] [blame] | 1338 | void SurfaceFlinger::onHotplugReceived(int32_t sequenceId, hwc2_display_t display, |
| 1339 | HWC2::Connection connection) { |
| 1340 | ALOGV("onHotplugReceived(%d, %" PRIu64 ", %s)", sequenceId, display, |
| 1341 | connection == HWC2::Connection::Connected ? "connected" : "disconnected"); |
Stephen Kiazyk | 7d3dcb9 | 2017-04-05 16:46:49 -0700 | [diff] [blame] | 1342 | |
Lloyd Pique | ba04e62 | 2017-12-14 17:11:26 -0800 | [diff] [blame] | 1343 | // Ignore events that do not have the right sequenceId. |
| 1344 | if (sequenceId != getBE().mComposerSequenceId) { |
| 1345 | return; |
| 1346 | } |
| 1347 | |
Steven Thomas | b02664d | 2017-07-26 18:48:28 -0700 | [diff] [blame] | 1348 | // Only lock if we're not on the main thread. This function is normally |
| 1349 | // called on a hwbinder thread, but for the primary display it's called on |
| 1350 | // the main thread with the state lock already held, so don't attempt to |
| 1351 | // acquire it here. |
Lloyd Pique | ba04e62 | 2017-12-14 17:11:26 -0800 | [diff] [blame] | 1352 | ConditionalLock lock(mStateLock, std::this_thread::get_id() != mMainThreadId); |
Steven Thomas | b02664d | 2017-07-26 18:48:28 -0700 | [diff] [blame] | 1353 | |
Lloyd Pique | 715a2c1 | 2017-12-14 17:18:08 -0800 | [diff] [blame] | 1354 | mPendingHotplugEvents.emplace_back(HotplugEvent{display, connection}); |
Mathias Agopian | 9e2463e | 2012-09-21 18:26:16 -0700 | [diff] [blame] | 1355 | |
Jiwen 'Steve' Cai | ccfd682 | 2018-02-21 16:15:58 -0800 | [diff] [blame] | 1356 | if (std::this_thread::get_id() == mMainThreadId) { |
| 1357 | // Process all pending hot plug events immediately if we are on the main thread. |
| 1358 | processDisplayHotplugEventsLocked(); |
| 1359 | } |
| 1360 | |
Lloyd Pique | ba04e62 | 2017-12-14 17:11:26 -0800 | [diff] [blame] | 1361 | setTransactionFlags(eDisplayTransactionNeeded); |
Mathias Agopian | 8630320 | 2012-07-24 22:46:10 -0700 | [diff] [blame] | 1362 | } |
| 1363 | |
Steven Thomas | b02664d | 2017-07-26 18:48:28 -0700 | [diff] [blame] | 1364 | void SurfaceFlinger::onRefreshReceived(int sequenceId, |
| 1365 | hwc2_display_t /*display*/) { |
Steven Thomas | 3cfac28 | 2017-02-06 12:29:30 -0800 | [diff] [blame] | 1366 | Mutex::Autolock lock(mStateLock); |
David Sodman | 105b7dc | 2017-11-04 20:28:14 -0700 | [diff] [blame] | 1367 | if (sequenceId != getBE().mComposerSequenceId) { |
Steven Thomas | b02664d | 2017-07-26 18:48:28 -0700 | [diff] [blame] | 1368 | return; |
Steven Thomas | 3cfac28 | 2017-02-06 12:29:30 -0800 | [diff] [blame] | 1369 | } |
Dan Stoza | c7a25ad | 2018-04-12 11:45:09 -0700 | [diff] [blame] | 1370 | repaintEverything(); |
Steven Thomas | 3cfac28 | 2017-02-06 12:29:30 -0800 | [diff] [blame] | 1371 | } |
| 1372 | |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 1373 | void SurfaceFlinger::setVsyncEnabled(int disp, int enabled) { |
Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 1374 | ATRACE_CALL(); |
Steven Thomas | b02664d | 2017-07-26 18:48:28 -0700 | [diff] [blame] | 1375 | Mutex::Autolock lock(mStateLock); |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 1376 | getHwComposer().setVsyncEnabled(disp, |
| 1377 | enabled ? HWC2::Vsync::Enable : HWC2::Vsync::Disable); |
Mathias Agopian | 8630320 | 2012-07-24 22:46:10 -0700 | [diff] [blame] | 1378 | } |
| 1379 | |
Stephen Kiazyk | 7d3dcb9 | 2017-04-05 16:46:49 -0700 | [diff] [blame] | 1380 | // Note: it is assumed the caller holds |mStateLock| when this is called |
Steven Thomas | b02664d | 2017-07-26 18:48:28 -0700 | [diff] [blame] | 1381 | void SurfaceFlinger::resetDisplayState() { |
Hendrik Wagenaar | 87670ff | 2017-02-01 12:10:46 -0800 | [diff] [blame] | 1382 | disableHardwareVsync(true); |
Hendrik Wagenaar | 87670ff | 2017-02-01 12:10:46 -0800 | [diff] [blame] | 1383 | // Clear the drawing state so that the logic inside of |
| 1384 | // handleTransactionLocked will fire. It will determine the delta between |
| 1385 | // mCurrentState and mDrawingState and re-apply all changes when we make the |
| 1386 | // transition. |
| 1387 | mDrawingState.displays.clear(); |
Chia-I Wu | 7f40290 | 2017-11-09 12:51:10 -0800 | [diff] [blame] | 1388 | getRenderEngine().resetCurrentSurface(); |
Hendrik Wagenaar | 87670ff | 2017-02-01 12:10:46 -0800 | [diff] [blame] | 1389 | mDisplays.clear(); |
| 1390 | } |
| 1391 | |
Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 1392 | void SurfaceFlinger::updateVrFlinger() { |
| 1393 | if (!mVrFlinger) |
| 1394 | return; |
| 1395 | bool vrFlingerRequestsDisplay = mVrFlingerRequestsDisplay; |
David Sodman | 105b7dc | 2017-11-04 20:28:14 -0700 | [diff] [blame] | 1396 | if (vrFlingerRequestsDisplay == getBE().mHwc->isUsingVrComposer()) { |
Mark Urbanus | 209beca | 2017-02-23 11:16:04 -0800 | [diff] [blame] | 1397 | return; |
| 1398 | } |
Stephen Kiazyk | 7d3dcb9 | 2017-04-05 16:46:49 -0700 | [diff] [blame] | 1399 | |
David Sodman | 105b7dc | 2017-11-04 20:28:14 -0700 | [diff] [blame] | 1400 | if (vrFlingerRequestsDisplay && !getBE().mHwc->getComposer()->isRemote()) { |
Steven Thomas | b02664d | 2017-07-26 18:48:28 -0700 | [diff] [blame] | 1401 | ALOGE("Vr flinger is only supported for remote hardware composer" |
| 1402 | " service connections. Ignoring request to transition to vr" |
| 1403 | " flinger."); |
| 1404 | mVrFlingerRequestsDisplay = false; |
| 1405 | return; |
Mark Urbanus | 209beca | 2017-02-23 11:16:04 -0800 | [diff] [blame] | 1406 | } |
Hendrik Wagenaar | 87670ff | 2017-02-01 12:10:46 -0800 | [diff] [blame] | 1407 | |
Stephen Kiazyk | 7d3dcb9 | 2017-04-05 16:46:49 -0700 | [diff] [blame] | 1408 | Mutex::Autolock _l(mStateLock); |
Hendrik Wagenaar | 87670ff | 2017-02-01 12:10:46 -0800 | [diff] [blame] | 1409 | |
Steven Thomas | b02664d | 2017-07-26 18:48:28 -0700 | [diff] [blame] | 1410 | int currentDisplayPowerMode = getDisplayDeviceLocked( |
| 1411 | mBuiltinDisplays[DisplayDevice::DISPLAY_PRIMARY])->getPowerMode(); |
Hendrik Wagenaar | 87670ff | 2017-02-01 12:10:46 -0800 | [diff] [blame] | 1412 | |
Steven Thomas | b02664d | 2017-07-26 18:48:28 -0700 | [diff] [blame] | 1413 | if (!vrFlingerRequestsDisplay) { |
Stephen Kiazyk | 7d3dcb9 | 2017-04-05 16:46:49 -0700 | [diff] [blame] | 1414 | mVrFlinger->SeizeDisplayOwnership(); |
Steven Thomas | b02664d | 2017-07-26 18:48:28 -0700 | [diff] [blame] | 1415 | } |
Hendrik Wagenaar | 87670ff | 2017-02-01 12:10:46 -0800 | [diff] [blame] | 1416 | |
Steven Thomas | b02664d | 2017-07-26 18:48:28 -0700 | [diff] [blame] | 1417 | resetDisplayState(); |
David Sodman | 105b7dc | 2017-11-04 20:28:14 -0700 | [diff] [blame] | 1418 | getBE().mHwc.reset(); // Delete the current instance before creating the new one |
Lloyd Pique | a822d52 | 2017-12-20 16:42:57 -0800 | [diff] [blame] | 1419 | getBE().mHwc.reset(new HWComposer(std::make_unique<Hwc2::impl::Composer>( |
| 1420 | vrFlingerRequestsDisplay ? "vr" : getBE().mHwcServiceName))); |
David Sodman | 105b7dc | 2017-11-04 20:28:14 -0700 | [diff] [blame] | 1421 | getBE().mHwc->registerCallback(this, ++getBE().mComposerSequenceId); |
Stephen Kiazyk | 7d3dcb9 | 2017-04-05 16:46:49 -0700 | [diff] [blame] | 1422 | |
David Sodman | 105b7dc | 2017-11-04 20:28:14 -0700 | [diff] [blame] | 1423 | LOG_ALWAYS_FATAL_IF(!getBE().mHwc->getComposer()->isRemote(), |
| 1424 | "Switched to non-remote hardware composer"); |
Steven Thomas | b02664d | 2017-07-26 18:48:28 -0700 | [diff] [blame] | 1425 | |
| 1426 | if (vrFlingerRequestsDisplay) { |
| 1427 | mVrFlinger->GrantDisplayOwnership(); |
| 1428 | } else { |
Stephen Kiazyk | 7d3dcb9 | 2017-04-05 16:46:49 -0700 | [diff] [blame] | 1429 | enableHardwareVsync(); |
Hendrik Wagenaar | 87670ff | 2017-02-01 12:10:46 -0800 | [diff] [blame] | 1430 | } |
Stephen Kiazyk | 7d3dcb9 | 2017-04-05 16:46:49 -0700 | [diff] [blame] | 1431 | |
| 1432 | mVisibleRegionsDirty = true; |
| 1433 | invalidateHwcGeometry(); |
| 1434 | |
Alex Sakhartchouk | 096cb99 | 2017-06-05 10:37:57 -0400 | [diff] [blame] | 1435 | // Re-enable default display. |
Steven Thomas | b02664d | 2017-07-26 18:48:28 -0700 | [diff] [blame] | 1436 | sp<DisplayDevice> hw(getDisplayDeviceLocked( |
| 1437 | mBuiltinDisplays[DisplayDevice::DISPLAY_PRIMARY])); |
| 1438 | setPowerModeInternal(hw, currentDisplayPowerMode, /*stateLockHeld*/ true); |
Alex Sakhartchouk | 096cb99 | 2017-06-05 10:37:57 -0400 | [diff] [blame] | 1439 | |
Steven Thomas | b02664d | 2017-07-26 18:48:28 -0700 | [diff] [blame] | 1440 | // Reset the timing values to account for the period of the swapped in HWC |
David Sodman | 105b7dc | 2017-11-04 20:28:14 -0700 | [diff] [blame] | 1441 | const auto& activeConfig = getBE().mHwc->getActiveConfig(HWC_DISPLAY_PRIMARY); |
Steven Thomas | b02664d | 2017-07-26 18:48:28 -0700 | [diff] [blame] | 1442 | const nsecs_t period = activeConfig->getVsyncPeriod(); |
| 1443 | mAnimFrameTracker.setDisplayRefreshPeriod(period); |
Alex Sakhartchouk | 096cb99 | 2017-06-05 10:37:57 -0400 | [diff] [blame] | 1444 | |
Steven Thomas | b02664d | 2017-07-26 18:48:28 -0700 | [diff] [blame] | 1445 | // Use phase of 0 since phase is not known. |
| 1446 | // Use latency of 0, which will snap to the ideal latency. |
| 1447 | setCompositorTimingSnapped(0, period, 0); |
Stephen Kiazyk | 82386cd | 2017-04-14 13:43:29 -0700 | [diff] [blame] | 1448 | |
Stephen Kiazyk | 7d3dcb9 | 2017-04-05 16:46:49 -0700 | [diff] [blame] | 1449 | android_atomic_or(1, &mRepaintEverything); |
| 1450 | setTransactionFlags(eDisplayTransactionNeeded); |
Hendrik Wagenaar | 87670ff | 2017-02-01 12:10:46 -0800 | [diff] [blame] | 1451 | } |
| 1452 | |
Mathias Agopian | 4fec873 | 2012-06-29 14:12:52 -0700 | [diff] [blame] | 1453 | void SurfaceFlinger::onMessageReceived(int32_t what) { |
Jamie Gennis | 1c8e95c | 2012-02-23 19:27:23 -0800 | [diff] [blame] | 1454 | ATRACE_CALL(); |
Mathias Agopian | 99ce5cd | 2012-01-31 18:24:27 -0800 | [diff] [blame] | 1455 | switch (what) { |
Dan Stoza | 6b9454d | 2014-11-07 16:00:59 -0800 | [diff] [blame] | 1456 | case MessageQueue::INVALIDATE: { |
Dan Stoza | 5018288 | 2016-07-08 12:02:20 -0700 | [diff] [blame] | 1457 | bool frameMissed = !mHadClientComposition && |
| 1458 | mPreviousPresentFence != Fence::NO_FENCE && |
Brian Anderson | 0a61b0c | 2016-12-07 14:55:56 -0800 | [diff] [blame] | 1459 | (mPreviousPresentFence->getSignalTime() == |
| 1460 | Fence::SIGNAL_TIME_PENDING); |
Dan Stoza | 5018288 | 2016-07-08 12:02:20 -0700 | [diff] [blame] | 1461 | ATRACE_INT("FrameMissed", static_cast<int>(frameMissed)); |
Dan Stoza | c5da271 | 2016-07-20 15:38:12 -0700 | [diff] [blame] | 1462 | if (mPropagateBackpressure && frameMissed) { |
Yiwei Zhang | faf3ded | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 1463 | mTimeStats.incrementMissedFrames(true); |
Dan Stoza | 5018288 | 2016-07-08 12:02:20 -0700 | [diff] [blame] | 1464 | signalLayerUpdate(); |
| 1465 | break; |
| 1466 | } |
Yiwei Zhang | faf3ded | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 1467 | if (frameMissed) { |
| 1468 | mTimeStats.incrementMissedFrames(false); |
| 1469 | } |
Dan Stoza | 5018288 | 2016-07-08 12:02:20 -0700 | [diff] [blame] | 1470 | |
Steven Thomas | 050b2c8 | 2017-03-06 11:45:16 -0800 | [diff] [blame] | 1471 | // Now that we're going to make it to the handleMessageTransaction() |
| 1472 | // call below it's safe to call updateVrFlinger(), which will |
| 1473 | // potentially trigger a display handoff. |
| 1474 | updateVrFlinger(); |
| 1475 | |
Dan Stoza | 6b9454d | 2014-11-07 16:00:59 -0800 | [diff] [blame] | 1476 | bool refreshNeeded = handleMessageTransaction(); |
| 1477 | refreshNeeded |= handleMessageInvalidate(); |
Dan Stoza | 5878444 | 2014-12-02 16:58:17 -0800 | [diff] [blame] | 1478 | refreshNeeded |= mRepaintEverything; |
Dan Stoza | 6b9454d | 2014-11-07 16:00:59 -0800 | [diff] [blame] | 1479 | if (refreshNeeded) { |
Dan Stoza | 5878444 | 2014-12-02 16:58:17 -0800 | [diff] [blame] | 1480 | // Signal a refresh if a transaction modified the window state, |
| 1481 | // a new buffer was latched, or if HWC has requested a full |
| 1482 | // repaint |
Dan Stoza | 6b9454d | 2014-11-07 16:00:59 -0800 | [diff] [blame] | 1483 | signalRefresh(); |
| 1484 | } |
| 1485 | break; |
| 1486 | } |
| 1487 | case MessageQueue::REFRESH: { |
| 1488 | handleMessageRefresh(); |
| 1489 | break; |
| 1490 | } |
Mathias Agopian | 99ce5cd | 2012-01-31 18:24:27 -0800 | [diff] [blame] | 1491 | } |
| 1492 | } |
| 1493 | |
Dan Stoza | 6b9454d | 2014-11-07 16:00:59 -0800 | [diff] [blame] | 1494 | bool SurfaceFlinger::handleMessageTransaction() { |
Fabien Sanglard | c8251eb | 2016-12-07 13:59:48 -0800 | [diff] [blame] | 1495 | uint32_t transactionFlags = peekTransactionFlags(); |
Mathias Agopian | 4fec873 | 2012-06-29 14:12:52 -0700 | [diff] [blame] | 1496 | if (transactionFlags) { |
Mathias Agopian | 87baae1 | 2012-07-31 12:38:26 -0700 | [diff] [blame] | 1497 | handleTransaction(transactionFlags); |
Dan Stoza | 6b9454d | 2014-11-07 16:00:59 -0800 | [diff] [blame] | 1498 | return true; |
Mathias Agopian | 4fec873 | 2012-06-29 14:12:52 -0700 | [diff] [blame] | 1499 | } |
Dan Stoza | 6b9454d | 2014-11-07 16:00:59 -0800 | [diff] [blame] | 1500 | return false; |
Mathias Agopian | 4fec873 | 2012-06-29 14:12:52 -0700 | [diff] [blame] | 1501 | } |
| 1502 | |
Dan Stoza | 6b9454d | 2014-11-07 16:00:59 -0800 | [diff] [blame] | 1503 | bool SurfaceFlinger::handleMessageInvalidate() { |
Mathias Agopian | cd60f99 | 2012-08-16 16:28:27 -0700 | [diff] [blame] | 1504 | ATRACE_CALL(); |
Dan Stoza | 6b9454d | 2014-11-07 16:00:59 -0800 | [diff] [blame] | 1505 | return handlePageFlip(); |
Mathias Agopian | 4fec873 | 2012-06-29 14:12:52 -0700 | [diff] [blame] | 1506 | } |
| 1507 | |
| 1508 | void SurfaceFlinger::handleMessageRefresh() { |
Mathias Agopian | cd60f99 | 2012-08-16 16:28:27 -0700 | [diff] [blame] | 1509 | ATRACE_CALL(); |
Dan Stoza | 14cd37c | 2015-07-09 12:43:33 -0700 | [diff] [blame] | 1510 | |
Dan Stoza | 2b6d38e | 2017-06-01 16:40:30 -0700 | [diff] [blame] | 1511 | mRefreshPending = false; |
| 1512 | |
Chia-I Wu | 30505fb | 2018-03-26 16:20:31 -0700 | [diff] [blame] | 1513 | nsecs_t refreshStartTime = systemTime(SYSTEM_TIME_MONOTONIC); |
Dan Stoza | 14cd37c | 2015-07-09 12:43:33 -0700 | [diff] [blame] | 1514 | |
Chia-I Wu | 30505fb | 2018-03-26 16:20:31 -0700 | [diff] [blame] | 1515 | preComposition(refreshStartTime); |
| 1516 | rebuildLayerStacks(); |
| 1517 | setUpHWComposer(); |
Dan Stoza | 05dacfb | 2016-07-01 13:33:38 -0700 | [diff] [blame] | 1518 | doDebugFlashRegions(); |
Adrian Roos | 1e1a128 | 2017-11-01 19:05:31 +0100 | [diff] [blame] | 1519 | doTracing("handleRefresh"); |
Yiwei Zhang | 7124ad3 | 2018-02-21 13:02:45 -0800 | [diff] [blame] | 1520 | logLayerStats(); |
Dan Stoza | 05dacfb | 2016-07-01 13:33:38 -0700 | [diff] [blame] | 1521 | doComposition(); |
Chia-I Wu | 30505fb | 2018-03-26 16:20:31 -0700 | [diff] [blame] | 1522 | postComposition(refreshStartTime); |
Dan Stoza | 05dacfb | 2016-07-01 13:33:38 -0700 | [diff] [blame] | 1523 | |
David Sodman | 105b7dc | 2017-11-04 20:28:14 -0700 | [diff] [blame] | 1524 | mPreviousPresentFence = getBE().mHwc->getPresentFence(HWC_DISPLAY_PRIMARY); |
Dan Stoza | bfbffeb | 2016-07-21 14:49:33 -0700 | [diff] [blame] | 1525 | |
| 1526 | mHadClientComposition = false; |
| 1527 | for (size_t displayId = 0; displayId < mDisplays.size(); ++displayId) { |
| 1528 | const sp<DisplayDevice>& displayDevice = mDisplays[displayId]; |
| 1529 | mHadClientComposition = mHadClientComposition || |
David Sodman | 105b7dc | 2017-11-04 20:28:14 -0700 | [diff] [blame] | 1530 | getBE().mHwc->hasClientComposition(displayDevice->getHwcDisplayId()); |
Dan Stoza | bfbffeb | 2016-07-21 14:49:33 -0700 | [diff] [blame] | 1531 | } |
Dan Stoza | 84d619e | 2018-03-28 17:07:36 -0700 | [diff] [blame] | 1532 | mVsyncModulator.setLastFrameUsedRenderEngine(mHadClientComposition); |
Dan Stoza | 14cd37c | 2015-07-09 12:43:33 -0700 | [diff] [blame] | 1533 | |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 1534 | mLayersWithQueuedFrames.clear(); |
Mathias Agopian | cd60f99 | 2012-08-16 16:28:27 -0700 | [diff] [blame] | 1535 | } |
Mathias Agopian | 4fec873 | 2012-06-29 14:12:52 -0700 | [diff] [blame] | 1536 | |
Mathias Agopian | cd60f99 | 2012-08-16 16:28:27 -0700 | [diff] [blame] | 1537 | void SurfaceFlinger::doDebugFlashRegions() |
| 1538 | { |
| 1539 | // is debugging enabled |
| 1540 | if (CC_LIKELY(!mDebugRegion)) |
| 1541 | return; |
| 1542 | |
| 1543 | const bool repaintEverything = mRepaintEverything; |
| 1544 | for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) { |
| 1545 | const sp<DisplayDevice>& hw(mDisplays[dpy]); |
Prashant Malani | 2c9b11f | 2014-05-25 01:36:31 -0700 | [diff] [blame] | 1546 | if (hw->isDisplayOn()) { |
Mathias Agopian | cd60f99 | 2012-08-16 16:28:27 -0700 | [diff] [blame] | 1547 | // transform the dirty region into this screen's coordinate space |
| 1548 | const Region dirtyRegion(hw->getDirtyRegion(repaintEverything)); |
| 1549 | if (!dirtyRegion.isEmpty()) { |
| 1550 | // redraw the whole screen |
Chia-I Wu | b02087d | 2017-11-09 10:19:54 -0800 | [diff] [blame] | 1551 | doComposeSurfaces(hw); |
Mathias Agopian | cd60f99 | 2012-08-16 16:28:27 -0700 | [diff] [blame] | 1552 | |
| 1553 | // and draw the dirty region |
Mathias Agopian | cd60f99 | 2012-08-16 16:28:27 -0700 | [diff] [blame] | 1554 | const int32_t height = hw->getHeight(); |
Lloyd Pique | 144e116 | 2017-12-20 16:44:52 -0800 | [diff] [blame] | 1555 | auto& engine(getRenderEngine()); |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 1556 | engine.fillRegionWithColor(dirtyRegion, height, 1, 0, 1, 1); |
| 1557 | |
Mathias Agopian | da27af9 | 2012-09-13 18:17:13 -0700 | [diff] [blame] | 1558 | hw->swapBuffers(getHwComposer()); |
Mathias Agopian | cd60f99 | 2012-08-16 16:28:27 -0700 | [diff] [blame] | 1559 | } |
| 1560 | } |
| 1561 | } |
| 1562 | |
| 1563 | postFramebuffer(); |
| 1564 | |
| 1565 | if (mDebugRegion > 1) { |
| 1566 | usleep(mDebugRegion * 1000); |
| 1567 | } |
Mathias Agopian | bb53b0e | 2012-09-24 21:27:29 -0700 | [diff] [blame] | 1568 | |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 1569 | for (size_t displayId = 0; displayId < mDisplays.size(); ++displayId) { |
Dan Stoza | 7bdf55a | 2016-06-17 11:29:01 -0700 | [diff] [blame] | 1570 | auto& displayDevice = mDisplays[displayId]; |
| 1571 | if (!displayDevice->isDisplayOn()) { |
| 1572 | continue; |
| 1573 | } |
| 1574 | |
David Sodman | 105b7dc | 2017-11-04 20:28:14 -0700 | [diff] [blame] | 1575 | status_t result = displayDevice->prepareFrame(*getBE().mHwc); |
| 1576 | ALOGE_IF(result != NO_ERROR, |
| 1577 | "prepareFrame for display %zd failed:" |
| 1578 | " %d (%s)", |
| 1579 | displayId, result, strerror(-result)); |
Mathias Agopian | bb53b0e | 2012-09-24 21:27:29 -0700 | [diff] [blame] | 1580 | } |
Mathias Agopian | cd60f99 | 2012-08-16 16:28:27 -0700 | [diff] [blame] | 1581 | } |
| 1582 | |
Adrian Roos | 1e1a128 | 2017-11-01 19:05:31 +0100 | [diff] [blame] | 1583 | void SurfaceFlinger::doTracing(const char* where) { |
| 1584 | ATRACE_CALL(); |
| 1585 | ATRACE_NAME(where); |
| 1586 | if (CC_UNLIKELY(mTracing.isEnabled())) { |
Jorim Jaggi | 8e0af36 | 2017-11-14 16:28:28 +0100 | [diff] [blame] | 1587 | mTracing.traceLayers(where, dumpProtoInfo(LayerVector::StateSet::Drawing)); |
Adrian Roos | 1e1a128 | 2017-11-01 19:05:31 +0100 | [diff] [blame] | 1588 | } |
| 1589 | } |
| 1590 | |
Yiwei Zhang | 7124ad3 | 2018-02-21 13:02:45 -0800 | [diff] [blame] | 1591 | void SurfaceFlinger::logLayerStats() { |
| 1592 | ATRACE_CALL(); |
| 1593 | if (CC_UNLIKELY(mLayerStats.isEnabled())) { |
| 1594 | int32_t hwcId = -1; |
| 1595 | for (size_t dpy = 0; dpy < mDisplays.size(); ++dpy) { |
| 1596 | const sp<const DisplayDevice>& displayDevice(mDisplays[dpy]); |
| 1597 | if (displayDevice->isPrimary()) { |
| 1598 | hwcId = displayDevice->getHwcDisplayId(); |
| 1599 | break; |
| 1600 | } |
| 1601 | } |
| 1602 | if (hwcId < 0) { |
| 1603 | ALOGE("LayerStats: Hmmm, no primary display?"); |
| 1604 | return; |
| 1605 | } |
| 1606 | mLayerStats.logLayerStats(dumpVisibleLayersProtoInfo(hwcId)); |
| 1607 | } |
| 1608 | } |
| 1609 | |
Chia-I Wu | 30505fb | 2018-03-26 16:20:31 -0700 | [diff] [blame] | 1610 | void SurfaceFlinger::preComposition(nsecs_t refreshStartTime) |
Mathias Agopian | cd60f99 | 2012-08-16 16:28:27 -0700 | [diff] [blame] | 1611 | { |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 1612 | ATRACE_CALL(); |
| 1613 | ALOGV("preComposition"); |
| 1614 | |
Mathias Agopian | cd60f99 | 2012-08-16 16:28:27 -0700 | [diff] [blame] | 1615 | bool needExtraInvalidate = false; |
Robert Carr | 2047fae | 2016-11-28 14:09:09 -0800 | [diff] [blame] | 1616 | mDrawingState.traverseInZOrder([&](Layer* layer) { |
Chia-I Wu | 30505fb | 2018-03-26 16:20:31 -0700 | [diff] [blame] | 1617 | if (layer->onPreComposition(refreshStartTime)) { |
Mathias Agopian | cd60f99 | 2012-08-16 16:28:27 -0700 | [diff] [blame] | 1618 | needExtraInvalidate = true; |
| 1619 | } |
Robert Carr | 2047fae | 2016-11-28 14:09:09 -0800 | [diff] [blame] | 1620 | }); |
| 1621 | |
Mathias Agopian | cd60f99 | 2012-08-16 16:28:27 -0700 | [diff] [blame] | 1622 | if (needExtraInvalidate) { |
| 1623 | signalLayerUpdate(); |
| 1624 | } |
| 1625 | } |
| 1626 | |
Brian Anderson | 0a61b0c | 2016-12-07 14:55:56 -0800 | [diff] [blame] | 1627 | void SurfaceFlinger::updateCompositorTiming( |
| 1628 | nsecs_t vsyncPhase, nsecs_t vsyncInterval, nsecs_t compositeTime, |
| 1629 | std::shared_ptr<FenceTime>& presentFenceTime) { |
| 1630 | // Update queue of past composite+present times and determine the |
| 1631 | // most recently known composite to present latency. |
David Sodman | 99974d2 | 2017-11-28 12:04:33 -0800 | [diff] [blame] | 1632 | getBE().mCompositePresentTimes.push({compositeTime, presentFenceTime}); |
Brian Anderson | 0a61b0c | 2016-12-07 14:55:56 -0800 | [diff] [blame] | 1633 | nsecs_t compositeToPresentLatency = -1; |
David Sodman | 99974d2 | 2017-11-28 12:04:33 -0800 | [diff] [blame] | 1634 | while (!getBE().mCompositePresentTimes.empty()) { |
| 1635 | SurfaceFlingerBE::CompositePresentTime& cpt = getBE().mCompositePresentTimes.front(); |
Brian Anderson | 0a61b0c | 2016-12-07 14:55:56 -0800 | [diff] [blame] | 1636 | // Cached values should have been updated before calling this method, |
| 1637 | // which helps avoid duplicate syscalls. |
| 1638 | nsecs_t displayTime = cpt.display->getCachedSignalTime(); |
| 1639 | if (displayTime == Fence::SIGNAL_TIME_PENDING) { |
| 1640 | break; |
| 1641 | } |
| 1642 | compositeToPresentLatency = displayTime - cpt.composite; |
David Sodman | 99974d2 | 2017-11-28 12:04:33 -0800 | [diff] [blame] | 1643 | getBE().mCompositePresentTimes.pop(); |
Brian Anderson | 0a61b0c | 2016-12-07 14:55:56 -0800 | [diff] [blame] | 1644 | } |
| 1645 | |
| 1646 | // Don't let mCompositePresentTimes grow unbounded, just in case. |
David Sodman | 99974d2 | 2017-11-28 12:04:33 -0800 | [diff] [blame] | 1647 | while (getBE().mCompositePresentTimes.size() > 16) { |
| 1648 | getBE().mCompositePresentTimes.pop(); |
Brian Anderson | 0a61b0c | 2016-12-07 14:55:56 -0800 | [diff] [blame] | 1649 | } |
| 1650 | |
Brian Anderson | d001058 | 2017-03-07 13:20:31 -0800 | [diff] [blame] | 1651 | setCompositorTimingSnapped( |
| 1652 | vsyncPhase, vsyncInterval, compositeToPresentLatency); |
| 1653 | } |
| 1654 | |
| 1655 | void SurfaceFlinger::setCompositorTimingSnapped(nsecs_t vsyncPhase, |
| 1656 | nsecs_t vsyncInterval, nsecs_t compositeToPresentLatency) { |
Brian Anderson | 0a61b0c | 2016-12-07 14:55:56 -0800 | [diff] [blame] | 1657 | // Integer division and modulo round toward 0 not -inf, so we need to |
| 1658 | // treat negative and positive offsets differently. |
Brian Anderson | d001058 | 2017-03-07 13:20:31 -0800 | [diff] [blame] | 1659 | nsecs_t idealLatency = (sfVsyncPhaseOffsetNs > 0) ? |
Brian Anderson | 0a61b0c | 2016-12-07 14:55:56 -0800 | [diff] [blame] | 1660 | (vsyncInterval - (sfVsyncPhaseOffsetNs % vsyncInterval)) : |
| 1661 | ((-sfVsyncPhaseOffsetNs) % vsyncInterval); |
| 1662 | |
Brian Anderson | d001058 | 2017-03-07 13:20:31 -0800 | [diff] [blame] | 1663 | // Just in case sfVsyncPhaseOffsetNs == -vsyncInterval. |
| 1664 | if (idealLatency <= 0) { |
| 1665 | idealLatency = vsyncInterval; |
| 1666 | } |
| 1667 | |
Brian Anderson | 0a61b0c | 2016-12-07 14:55:56 -0800 | [diff] [blame] | 1668 | // Snap the latency to a value that removes scheduling jitter from the |
| 1669 | // composition and present times, which often have >1ms of jitter. |
| 1670 | // Reducing jitter is important if an app attempts to extrapolate |
| 1671 | // something (such as user input) to an accurate diasplay time. |
| 1672 | // Snapping also allows an app to precisely calculate sfVsyncPhaseOffsetNs |
| 1673 | // with (presentLatency % interval). |
Brian Anderson | d001058 | 2017-03-07 13:20:31 -0800 | [diff] [blame] | 1674 | nsecs_t bias = vsyncInterval / 2; |
| 1675 | int64_t extraVsyncs = |
| 1676 | (compositeToPresentLatency - idealLatency + bias) / vsyncInterval; |
| 1677 | nsecs_t snappedCompositeToPresentLatency = (extraVsyncs > 0) ? |
| 1678 | idealLatency + (extraVsyncs * vsyncInterval) : idealLatency; |
Brian Anderson | 0a61b0c | 2016-12-07 14:55:56 -0800 | [diff] [blame] | 1679 | |
David Sodman | 99974d2 | 2017-11-28 12:04:33 -0800 | [diff] [blame] | 1680 | std::lock_guard<std::mutex> lock(getBE().mCompositorTimingLock); |
| 1681 | getBE().mCompositorTiming.deadline = vsyncPhase - idealLatency; |
| 1682 | getBE().mCompositorTiming.interval = vsyncInterval; |
| 1683 | getBE().mCompositorTiming.presentLatency = snappedCompositeToPresentLatency; |
Brian Anderson | 0a61b0c | 2016-12-07 14:55:56 -0800 | [diff] [blame] | 1684 | } |
| 1685 | |
Chia-I Wu | 30505fb | 2018-03-26 16:20:31 -0700 | [diff] [blame] | 1686 | void SurfaceFlinger::postComposition(nsecs_t refreshStartTime) |
Mathias Agopian | cd60f99 | 2012-08-16 16:28:27 -0700 | [diff] [blame] | 1687 | { |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 1688 | ATRACE_CALL(); |
| 1689 | ALOGV("postComposition"); |
| 1690 | |
Brian Anderson | 3546a3f | 2016-07-14 11:51:14 -0700 | [diff] [blame] | 1691 | // Release any buffers which were replaced this frame |
Brian Anderson | f638686 | 2016-10-31 16:34:13 -0700 | [diff] [blame] | 1692 | nsecs_t dequeueReadyTime = systemTime(); |
Brian Anderson | 3546a3f | 2016-07-14 11:51:14 -0700 | [diff] [blame] | 1693 | for (auto& layer : mLayersWithQueuedFrames) { |
Brian Anderson | f638686 | 2016-10-31 16:34:13 -0700 | [diff] [blame] | 1694 | layer->releasePendingBuffer(dequeueReadyTime); |
Brian Anderson | 3546a3f | 2016-07-14 11:51:14 -0700 | [diff] [blame] | 1695 | } |
| 1696 | |
Stephen Kiazyk | 7d3dcb9 | 2017-04-05 16:46:49 -0700 | [diff] [blame] | 1697 | // |mStateLock| not needed as we are on the main thread |
| 1698 | const sp<const DisplayDevice> hw(getDefaultDisplayDeviceLocked()); |
Brian Anderson | 3d4039d | 2016-09-23 16:31:30 -0700 | [diff] [blame] | 1699 | |
David Sodman | 73beded | 2017-11-15 11:56:06 -0800 | [diff] [blame] | 1700 | getBE().mGlCompositionDoneTimeline.updateSignalTimes(); |
Brian Anderson | 3d4039d | 2016-09-23 16:31:30 -0700 | [diff] [blame] | 1701 | std::shared_ptr<FenceTime> glCompositionDoneFenceTime; |
Lloyd Pique | 2ae2b3b | 2017-12-14 17:18:17 -0800 | [diff] [blame] | 1702 | if (hw && getBE().mHwc->hasClientComposition(HWC_DISPLAY_PRIMARY)) { |
Brian Anderson | 3d4039d | 2016-09-23 16:31:30 -0700 | [diff] [blame] | 1703 | glCompositionDoneFenceTime = |
| 1704 | std::make_shared<FenceTime>(hw->getClientTargetAcquireFence()); |
David Sodman | 73beded | 2017-11-15 11:56:06 -0800 | [diff] [blame] | 1705 | getBE().mGlCompositionDoneTimeline.push(glCompositionDoneFenceTime); |
Brian Anderson | 3d4039d | 2016-09-23 16:31:30 -0700 | [diff] [blame] | 1706 | } else { |
| 1707 | glCompositionDoneFenceTime = FenceTime::NO_FENCE; |
| 1708 | } |
Brian Anderson | 3d4039d | 2016-09-23 16:31:30 -0700 | [diff] [blame] | 1709 | |
David Sodman | 73beded | 2017-11-15 11:56:06 -0800 | [diff] [blame] | 1710 | getBE().mDisplayTimeline.updateSignalTimes(); |
David Sodman | 105b7dc | 2017-11-04 20:28:14 -0700 | [diff] [blame] | 1711 | sp<Fence> presentFence = getBE().mHwc->getPresentFence(HWC_DISPLAY_PRIMARY); |
Brian Anderson | 4e606e3 | 2017-03-16 15:34:57 -0700 | [diff] [blame] | 1712 | auto presentFenceTime = std::make_shared<FenceTime>(presentFence); |
David Sodman | 73beded | 2017-11-15 11:56:06 -0800 | [diff] [blame] | 1713 | getBE().mDisplayTimeline.push(presentFenceTime); |
Brian Anderson | 3d4039d | 2016-09-23 16:31:30 -0700 | [diff] [blame] | 1714 | |
Brian Anderson | 0a61b0c | 2016-12-07 14:55:56 -0800 | [diff] [blame] | 1715 | nsecs_t vsyncPhase = mPrimaryDispSync.computeNextRefresh(0); |
| 1716 | nsecs_t vsyncInterval = mPrimaryDispSync.getPeriod(); |
| 1717 | |
Chia-I Wu | 30505fb | 2018-03-26 16:20:31 -0700 | [diff] [blame] | 1718 | // We use the refreshStartTime which might be sampled a little later than |
Brian Anderson | 0a61b0c | 2016-12-07 14:55:56 -0800 | [diff] [blame] | 1719 | // when we started doing work for this frame, but that should be okay |
| 1720 | // since updateCompositorTiming has snapping logic. |
| 1721 | updateCompositorTiming( |
Chia-I Wu | 30505fb | 2018-03-26 16:20:31 -0700 | [diff] [blame] | 1722 | vsyncPhase, vsyncInterval, refreshStartTime, presentFenceTime); |
Brian Anderson | d001058 | 2017-03-07 13:20:31 -0800 | [diff] [blame] | 1723 | CompositorTiming compositorTiming; |
| 1724 | { |
David Sodman | 99974d2 | 2017-11-28 12:04:33 -0800 | [diff] [blame] | 1725 | std::lock_guard<std::mutex> lock(getBE().mCompositorTimingLock); |
| 1726 | compositorTiming = getBE().mCompositorTiming; |
Brian Anderson | d001058 | 2017-03-07 13:20:31 -0800 | [diff] [blame] | 1727 | } |
Brian Anderson | 0a61b0c | 2016-12-07 14:55:56 -0800 | [diff] [blame] | 1728 | |
Robert Carr | 2047fae | 2016-11-28 14:09:09 -0800 | [diff] [blame] | 1729 | mDrawingState.traverseInZOrder([&](Layer* layer) { |
| 1730 | bool frameLatched = layer->onPostComposition(glCompositionDoneFenceTime, |
Brian Anderson | 4e606e3 | 2017-03-16 15:34:57 -0700 | [diff] [blame] | 1731 | presentFenceTime, compositorTiming); |
Dan Stoza | e77c766 | 2016-05-13 11:37:28 -0700 | [diff] [blame] | 1732 | if (frameLatched) { |
Robert Carr | 2047fae | 2016-11-28 14:09:09 -0800 | [diff] [blame] | 1733 | recordBufferingStats(layer->getName().string(), |
| 1734 | layer->getOccupancyHistory(false)); |
Dan Stoza | e77c766 | 2016-05-13 11:37:28 -0700 | [diff] [blame] | 1735 | } |
Robert Carr | 2047fae | 2016-11-28 14:09:09 -0800 | [diff] [blame] | 1736 | }); |
Jamie Gennis | 4b0eba9 | 2013-02-05 13:30:24 -0800 | [diff] [blame] | 1737 | |
Brian Anderson | fbc80ae | 2017-05-26 16:23:54 -0700 | [diff] [blame] | 1738 | if (presentFenceTime->isValid()) { |
| 1739 | if (mPrimaryDispSync.addPresentFence(presentFenceTime)) { |
Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 1740 | enableHardwareVsync(); |
| 1741 | } else { |
Jesse Hall | 948fe0c | 2013-10-14 12:56:09 -0700 | [diff] [blame] | 1742 | disableHardwareVsync(false); |
Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 1743 | } |
| 1744 | } |
| 1745 | |
Fabien Sanglard | cbf153b | 2017-03-10 17:57:12 -0800 | [diff] [blame] | 1746 | if (!hasSyncFramework) { |
Lloyd Pique | 2ae2b3b | 2017-12-14 17:18:17 -0800 | [diff] [blame] | 1747 | if (getBE().mHwc->isConnected(HWC_DISPLAY_PRIMARY) && hw->isDisplayOn()) { |
Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 1748 | enableHardwareVsync(); |
| 1749 | } |
| 1750 | } |
| 1751 | |
Jamie Gennis | 4b0eba9 | 2013-02-05 13:30:24 -0800 | [diff] [blame] | 1752 | if (mAnimCompositionPending) { |
| 1753 | mAnimCompositionPending = false; |
| 1754 | |
Brian Anderson | 4e606e3 | 2017-03-16 15:34:57 -0700 | [diff] [blame] | 1755 | if (presentFenceTime->isValid()) { |
Brian Anderson | 3d4039d | 2016-09-23 16:31:30 -0700 | [diff] [blame] | 1756 | mAnimFrameTracker.setActualPresentFence( |
Brian Anderson | 4e606e3 | 2017-03-16 15:34:57 -0700 | [diff] [blame] | 1757 | std::move(presentFenceTime)); |
Lloyd Pique | 2ae2b3b | 2017-12-14 17:18:17 -0800 | [diff] [blame] | 1758 | } else if (getBE().mHwc->isConnected(HWC_DISPLAY_PRIMARY)) { |
Jamie Gennis | 4b0eba9 | 2013-02-05 13:30:24 -0800 | [diff] [blame] | 1759 | // The HWC doesn't support present fences, so use the refresh |
| 1760 | // timestamp instead. |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 1761 | nsecs_t presentTime = |
David Sodman | 105b7dc | 2017-11-04 20:28:14 -0700 | [diff] [blame] | 1762 | getBE().mHwc->getRefreshTimestamp(HWC_DISPLAY_PRIMARY); |
Jamie Gennis | 4b0eba9 | 2013-02-05 13:30:24 -0800 | [diff] [blame] | 1763 | mAnimFrameTracker.setActualPresentTime(presentTime); |
| 1764 | } |
| 1765 | mAnimFrameTracker.advanceFrame(); |
| 1766 | } |
Dan Stoza | b90cf07 | 2015-03-05 11:05:59 -0800 | [diff] [blame] | 1767 | |
Yiwei Zhang | faf3ded | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 1768 | mTimeStats.incrementTotalFrames(); |
| 1769 | if (mHadClientComposition) { |
| 1770 | mTimeStats.incrementClientCompositionFrames(); |
| 1771 | } |
| 1772 | |
Lloyd Pique | 2ae2b3b | 2017-12-14 17:18:17 -0800 | [diff] [blame] | 1773 | if (getBE().mHwc->isConnected(HWC_DISPLAY_PRIMARY) && |
| 1774 | hw->getPowerMode() == HWC_POWER_MODE_OFF) { |
Dan Stoza | b90cf07 | 2015-03-05 11:05:59 -0800 | [diff] [blame] | 1775 | return; |
| 1776 | } |
| 1777 | |
| 1778 | nsecs_t currentTime = systemTime(); |
| 1779 | if (mHasPoweredOff) { |
| 1780 | mHasPoweredOff = false; |
| 1781 | } else { |
David Sodman | 4a36e93 | 2017-11-07 14:29:47 -0800 | [diff] [blame] | 1782 | nsecs_t elapsedTime = currentTime - getBE().mLastSwapTime; |
Brian Anderson | 0a61b0c | 2016-12-07 14:55:56 -0800 | [diff] [blame] | 1783 | size_t numPeriods = static_cast<size_t>(elapsedTime / vsyncInterval); |
David Sodman | 4a36e93 | 2017-11-07 14:29:47 -0800 | [diff] [blame] | 1784 | if (numPeriods < SurfaceFlingerBE::NUM_BUCKETS - 1) { |
| 1785 | getBE().mFrameBuckets[numPeriods] += elapsedTime; |
Dan Stoza | b90cf07 | 2015-03-05 11:05:59 -0800 | [diff] [blame] | 1786 | } else { |
David Sodman | 4a36e93 | 2017-11-07 14:29:47 -0800 | [diff] [blame] | 1787 | getBE().mFrameBuckets[SurfaceFlingerBE::NUM_BUCKETS - 1] += elapsedTime; |
Dan Stoza | b90cf07 | 2015-03-05 11:05:59 -0800 | [diff] [blame] | 1788 | } |
David Sodman | 4a36e93 | 2017-11-07 14:29:47 -0800 | [diff] [blame] | 1789 | getBE().mTotalTime += elapsedTime; |
Dan Stoza | b90cf07 | 2015-03-05 11:05:59 -0800 | [diff] [blame] | 1790 | } |
David Sodman | 4a36e93 | 2017-11-07 14:29:47 -0800 | [diff] [blame] | 1791 | getBE().mLastSwapTime = currentTime; |
Mathias Agopian | cd60f99 | 2012-08-16 16:28:27 -0700 | [diff] [blame] | 1792 | } |
| 1793 | |
| 1794 | void SurfaceFlinger::rebuildLayerStacks() { |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 1795 | ATRACE_CALL(); |
| 1796 | ALOGV("rebuildLayerStacks"); |
| 1797 | |
Mathias Agopian | cd60f99 | 2012-08-16 16:28:27 -0700 | [diff] [blame] | 1798 | // rebuild the visible layer list per screen |
Jeff Sharkey | 7648811 | 2017-02-27 14:15:18 -0700 | [diff] [blame] | 1799 | if (CC_UNLIKELY(mVisibleRegionsDirty)) { |
Siarhei Vishniakou | fc2589e | 2017-09-21 15:35:13 -0700 | [diff] [blame] | 1800 | ATRACE_NAME("rebuildLayerStacks VR Dirty"); |
Jeff Sharkey | 7648811 | 2017-02-27 14:15:18 -0700 | [diff] [blame] | 1801 | mVisibleRegionsDirty = false; |
| 1802 | invalidateHwcGeometry(); |
Fabien Sanglard | 3beb711 | 2017-02-24 17:33:52 -0800 | [diff] [blame] | 1803 | |
Jeff Sharkey | 7648811 | 2017-02-27 14:15:18 -0700 | [diff] [blame] | 1804 | for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) { |
| 1805 | Region opaqueRegion; |
| 1806 | Region dirtyRegion; |
| 1807 | Vector<sp<Layer>> layersSortedByZ; |
Chia-I Wu | 8380689 | 2017-11-16 10:50:20 -0800 | [diff] [blame] | 1808 | Vector<sp<Layer>> layersNeedingFences; |
Jeff Sharkey | 7648811 | 2017-02-27 14:15:18 -0700 | [diff] [blame] | 1809 | const sp<DisplayDevice>& displayDevice(mDisplays[dpy]); |
| 1810 | const Transform& tr(displayDevice->getTransform()); |
| 1811 | const Rect bounds(displayDevice->getBounds()); |
| 1812 | if (displayDevice->isDisplayOn()) { |
Chia-I Wu | ab0c319 | 2017-08-01 11:29:00 -0700 | [diff] [blame] | 1813 | computeVisibleRegions(displayDevice, dirtyRegion, opaqueRegion); |
Fabien Sanglard | 3beb711 | 2017-02-24 17:33:52 -0800 | [diff] [blame] | 1814 | |
Jeff Sharkey | 7648811 | 2017-02-27 14:15:18 -0700 | [diff] [blame] | 1815 | mDrawingState.traverseInZOrder([&](Layer* layer) { |
Chia-I Wu | 8380689 | 2017-11-16 10:50:20 -0800 | [diff] [blame] | 1816 | bool hwcLayerDestroyed = false; |
Chia-I Wu | ab0c319 | 2017-08-01 11:29:00 -0700 | [diff] [blame] | 1817 | if (layer->belongsToDisplay(displayDevice->getLayerStack(), |
| 1818 | displayDevice->isPrimary())) { |
Jeff Sharkey | 7648811 | 2017-02-27 14:15:18 -0700 | [diff] [blame] | 1819 | Region drawRegion(tr.transform( |
| 1820 | layer->visibleNonTransparentRegion)); |
| 1821 | drawRegion.andSelf(bounds); |
| 1822 | if (!drawRegion.isEmpty()) { |
| 1823 | layersSortedByZ.add(layer); |
| 1824 | } else { |
Chia-I Wu | 30505fb | 2018-03-26 16:20:31 -0700 | [diff] [blame] | 1825 | // Clear out the HWC layer if this layer was |
| 1826 | // previously visible, but no longer is |
Chia-I Wu | 8380689 | 2017-11-16 10:50:20 -0800 | [diff] [blame] | 1827 | hwcLayerDestroyed = layer->destroyHwcLayer( |
Steven Thomas | b02664d | 2017-07-26 18:48:28 -0700 | [diff] [blame] | 1828 | displayDevice->getHwcDisplayId()); |
Jeff Sharkey | 7648811 | 2017-02-27 14:15:18 -0700 | [diff] [blame] | 1829 | } |
Chia-I Wu | 30505fb | 2018-03-26 16:20:31 -0700 | [diff] [blame] | 1830 | } else { |
| 1831 | // WM changes displayDevice->layerStack upon sleep/awake. |
| 1832 | // Here we make sure we delete the HWC layers even if |
| 1833 | // WM changed their layer stack. |
| 1834 | hwcLayerDestroyed = layer->destroyHwcLayer( |
| 1835 | displayDevice->getHwcDisplayId()); |
Chia-I Wu | 8380689 | 2017-11-16 10:50:20 -0800 | [diff] [blame] | 1836 | } |
| 1837 | |
| 1838 | // If a layer is not going to get a release fence because |
| 1839 | // it is invisible, but it is also going to release its |
| 1840 | // old buffer, add it to the list of layers needing |
| 1841 | // fences. |
| 1842 | if (hwcLayerDestroyed) { |
| 1843 | auto found = std::find(mLayersWithQueuedFrames.cbegin(), |
| 1844 | mLayersWithQueuedFrames.cend(), layer); |
| 1845 | if (found != mLayersWithQueuedFrames.cend()) { |
| 1846 | layersNeedingFences.add(layer); |
| 1847 | } |
Fabien Sanglard | 3beb711 | 2017-02-24 17:33:52 -0800 | [diff] [blame] | 1848 | } |
Jeff Sharkey | 7648811 | 2017-02-27 14:15:18 -0700 | [diff] [blame] | 1849 | }); |
| 1850 | } |
| 1851 | displayDevice->setVisibleLayersSortedByZ(layersSortedByZ); |
Chia-I Wu | 8380689 | 2017-11-16 10:50:20 -0800 | [diff] [blame] | 1852 | displayDevice->setLayersNeedingFences(layersNeedingFences); |
Jeff Sharkey | 7648811 | 2017-02-27 14:15:18 -0700 | [diff] [blame] | 1853 | displayDevice->undefinedRegion.set(bounds); |
| 1854 | displayDevice->undefinedRegion.subtractSelf( |
| 1855 | tr.transform(opaqueRegion)); |
| 1856 | displayDevice->dirtyRegion.orSelf(dirtyRegion); |
Fabien Sanglard | 3beb711 | 2017-02-24 17:33:52 -0800 | [diff] [blame] | 1857 | } |
Fabien Sanglard | 3beb711 | 2017-02-24 17:33:52 -0800 | [diff] [blame] | 1858 | } |
Mathias Agopian | cd60f99 | 2012-08-16 16:28:27 -0700 | [diff] [blame] | 1859 | } |
Mathias Agopian | 3b1d2b6 | 2012-07-11 13:48:17 -0700 | [diff] [blame] | 1860 | |
Peiyong Lin | 136fbbc | 2018-04-17 15:09:44 -0700 | [diff] [blame^] | 1861 | // Returns a data space that fits all visible layers. The returned data space |
Peiyong Lin | dd9b2ae | 2018-03-01 16:22:45 -0800 | [diff] [blame] | 1862 | // can only be one of |
Peiyong Lin | dd9b2ae | 2018-03-01 16:22:45 -0800 | [diff] [blame] | 1863 | // - Dataspace::V0_SRGB |
| 1864 | // - Dataspace::DISPLAY_P3 |
| 1865 | // - Dataspace::V0_SCRGB_LINEAR |
Peiyong Lin | 136fbbc | 2018-04-17 15:09:44 -0700 | [diff] [blame^] | 1866 | // The returned HDR data space is one of |
| 1867 | // - Dataspace::UNKNOWN |
| 1868 | // - Dataspace::BT2020_HLG |
| 1869 | // - Dataspace::BT2020_PQ |
| 1870 | Dataspace SurfaceFlinger::getBestDataspace( |
| 1871 | const sp<const DisplayDevice>& displayDevice, Dataspace* outHdrDataSpace) const { |
| 1872 | Dataspace bestDataSpace = Dataspace::V0_SRGB; |
| 1873 | *outHdrDataSpace = Dataspace::UNKNOWN; |
| 1874 | |
Peiyong Lin | dd9b2ae | 2018-03-01 16:22:45 -0800 | [diff] [blame] | 1875 | for (const auto& layer : displayDevice->getVisibleLayersSortedByZ()) { |
| 1876 | switch (layer->getDataSpace()) { |
| 1877 | case Dataspace::V0_SCRGB: |
| 1878 | case Dataspace::V0_SCRGB_LINEAR: |
Peiyong Lin | 136fbbc | 2018-04-17 15:09:44 -0700 | [diff] [blame^] | 1879 | bestDataSpace = Dataspace::V0_SCRGB_LINEAR; |
Peiyong Lin | f59a719 | 2018-04-25 11:19:31 -0700 | [diff] [blame] | 1880 | break; |
Peiyong Lin | 136fbbc | 2018-04-17 15:09:44 -0700 | [diff] [blame^] | 1881 | case Dataspace::DISPLAY_P3: |
| 1882 | if (bestDataSpace == Dataspace::V0_SRGB) { |
| 1883 | bestDataSpace = Dataspace::DISPLAY_P3; |
| 1884 | } |
| 1885 | break; |
Peiyong Lin | dd9b2ae | 2018-03-01 16:22:45 -0800 | [diff] [blame] | 1886 | case Dataspace::BT2020_PQ: |
| 1887 | case Dataspace::BT2020_ITU_PQ: |
Peiyong Lin | 136fbbc | 2018-04-17 15:09:44 -0700 | [diff] [blame^] | 1888 | *outHdrDataSpace = Dataspace::BT2020_PQ; |
Peiyong Lin | dd9b2ae | 2018-03-01 16:22:45 -0800 | [diff] [blame] | 1889 | break; |
Peiyong Lin | f59a719 | 2018-04-25 11:19:31 -0700 | [diff] [blame] | 1890 | case Dataspace::BT2020_HLG: |
| 1891 | case Dataspace::BT2020_ITU_HLG: |
Peiyong Lin | 136fbbc | 2018-04-17 15:09:44 -0700 | [diff] [blame^] | 1892 | // When there's mixed PQ content and HLG content, we set the HDR |
| 1893 | // data space to be BT2020_PQ and convert HLG to PQ. |
| 1894 | if (*outHdrDataSpace == Dataspace::UNKNOWN) { |
| 1895 | *outHdrDataSpace = Dataspace::BT2020_HLG; |
Peiyong Lin | f59a719 | 2018-04-25 11:19:31 -0700 | [diff] [blame] | 1896 | } |
Peiyong Lin | dd9b2ae | 2018-03-01 16:22:45 -0800 | [diff] [blame] | 1897 | break; |
| 1898 | default: |
| 1899 | break; |
| 1900 | } |
Romain Guy | 54f154a | 2017-10-24 21:40:32 +0100 | [diff] [blame] | 1901 | } |
| 1902 | |
Peiyong Lin | 136fbbc | 2018-04-17 15:09:44 -0700 | [diff] [blame^] | 1903 | return bestDataSpace; |
Courtney Goeltzenleuchter | 5d94389 | 2017-03-22 13:46:46 -0600 | [diff] [blame] | 1904 | } |
| 1905 | |
Peiyong Lin | dd9b2ae | 2018-03-01 16:22:45 -0800 | [diff] [blame] | 1906 | // Pick the ColorMode / Dataspace for the display device. |
Peiyong Lin | dd9b2ae | 2018-03-01 16:22:45 -0800 | [diff] [blame] | 1907 | void SurfaceFlinger::pickColorMode(const sp<DisplayDevice>& displayDevice, |
Peiyong Lin | 136fbbc | 2018-04-17 15:09:44 -0700 | [diff] [blame^] | 1908 | ColorMode* outMode, Dataspace* outDataSpace, |
| 1909 | RenderIntent* outRenderIntent) const { |
Peiyong Lin | dd9b2ae | 2018-03-01 16:22:45 -0800 | [diff] [blame] | 1910 | if (mDisplayColorSetting == DisplayColorSetting::UNMANAGED) { |
| 1911 | *outMode = ColorMode::NATIVE; |
| 1912 | *outDataSpace = Dataspace::UNKNOWN; |
Peiyong Lin | 136fbbc | 2018-04-17 15:09:44 -0700 | [diff] [blame^] | 1913 | *outRenderIntent = RenderIntent::COLORIMETRIC; |
Peiyong Lin | dd9b2ae | 2018-03-01 16:22:45 -0800 | [diff] [blame] | 1914 | return; |
Chia-I Wu | 5c6e463 | 2018-01-11 08:54:38 -0800 | [diff] [blame] | 1915 | } |
Romain Guy | 88d37dd | 2017-05-26 17:57:05 -0700 | [diff] [blame] | 1916 | |
Peiyong Lin | 136fbbc | 2018-04-17 15:09:44 -0700 | [diff] [blame^] | 1917 | Dataspace hdrDataSpace; |
| 1918 | Dataspace bestDataSpace = getBestDataspace(displayDevice, &hdrDataSpace); |
| 1919 | |
| 1920 | if (hdrDataSpace == Dataspace::BT2020_PQ) { |
| 1921 | // Hardware composer can handle BT2100 ColorMode only when |
| 1922 | // - colorimetrical tone mapping is supported, or |
| 1923 | // - Auto mode is turned on and enhanced tone mapping is supported. |
| 1924 | if (displayDevice->hasBT2100PQColorimetricSupport() || |
| 1925 | (mDisplayColorSetting == DisplayColorSetting::ENHANCED && |
| 1926 | displayDevice->hasBT2100PQEnhanceSupport())) { |
| 1927 | *outMode = ColorMode::BT2100_PQ; |
| 1928 | *outDataSpace = Dataspace::BT2020_PQ; |
| 1929 | } else if (displayDevice->hasHDR10Support()) { |
| 1930 | // Legacy HDR support. HDR layers are treated as UNKNOWN layers. |
| 1931 | hdrDataSpace = Dataspace::UNKNOWN; |
| 1932 | } else { |
| 1933 | // Simulate PQ through RenderEngine, pick DISPLAY_P3 color mode. |
Peiyong Lin | dd9b2ae | 2018-03-01 16:22:45 -0800 | [diff] [blame] | 1934 | *outMode = ColorMode::DISPLAY_P3; |
| 1935 | *outDataSpace = Dataspace::DISPLAY_P3; |
Peiyong Lin | 136fbbc | 2018-04-17 15:09:44 -0700 | [diff] [blame^] | 1936 | } |
| 1937 | } else if (hdrDataSpace == Dataspace::BT2020_HLG) { |
| 1938 | if (displayDevice->hasBT2100HLGColorimetricSupport() || |
| 1939 | (mDisplayColorSetting == DisplayColorSetting::ENHANCED && |
| 1940 | displayDevice->hasBT2100HLGEnhanceSupport())) { |
| 1941 | *outMode = ColorMode::BT2100_HLG; |
| 1942 | *outDataSpace = Dataspace::BT2020_HLG; |
| 1943 | } else if (displayDevice->hasHLGSupport()) { |
| 1944 | // Legacy HDR support. HDR layers are treated as UNKNOWN layers. |
| 1945 | hdrDataSpace = Dataspace::UNKNOWN; |
| 1946 | } else { |
| 1947 | // Simulate HLG through RenderEngine, pick DISPLAY_P3 color mode. |
| 1948 | *outMode = ColorMode::DISPLAY_P3; |
| 1949 | *outDataSpace = Dataspace::DISPLAY_P3; |
| 1950 | } |
| 1951 | } |
| 1952 | |
| 1953 | // At this point, there's no HDR layer. |
| 1954 | if (hdrDataSpace == Dataspace::UNKNOWN) { |
| 1955 | switch (bestDataSpace) { |
| 1956 | case Dataspace::DISPLAY_P3: |
| 1957 | case Dataspace::V0_SCRGB_LINEAR: |
| 1958 | *outMode = ColorMode::DISPLAY_P3; |
| 1959 | *outDataSpace = Dataspace::DISPLAY_P3; |
| 1960 | break; |
| 1961 | default: |
| 1962 | *outMode = ColorMode::SRGB; |
| 1963 | *outDataSpace = Dataspace::V0_SRGB; |
| 1964 | break; |
| 1965 | } |
| 1966 | } |
| 1967 | *outRenderIntent = pickRenderIntent(displayDevice, *outMode); |
| 1968 | } |
| 1969 | |
| 1970 | RenderIntent SurfaceFlinger::pickRenderIntent(const sp<DisplayDevice>& displayDevice, |
| 1971 | ColorMode colorMode) const { |
| 1972 | // Native Mode means the display is not color managed, and whichever |
| 1973 | // render intent is picked doesn't matter, thus return |
| 1974 | // RenderIntent::COLORIMETRIC as default here. |
| 1975 | if (mDisplayColorSetting == DisplayColorSetting::UNMANAGED) { |
| 1976 | return RenderIntent::COLORIMETRIC; |
| 1977 | } |
| 1978 | |
| 1979 | // In Auto Color Mode, we want to strech to panel color space, right now |
| 1980 | // only the built-in display supports it. |
| 1981 | if (mDisplayColorSetting == DisplayColorSetting::ENHANCED && |
| 1982 | mBuiltinDisplaySupportsEnhance && displayDevice->isPrimary()) { |
| 1983 | switch (colorMode) { |
| 1984 | case ColorMode::DISPLAY_P3: |
| 1985 | case ColorMode::SRGB: |
| 1986 | return RenderIntent::ENHANCE; |
| 1987 | // In Auto Color Mode, BT2100_PQ and BT2100_HLG will only be picked |
| 1988 | // when TONE_MAP_ENHANCE or TONE_MAP_COLORIMETRIC is supported. |
| 1989 | // If TONE_MAP_ENHANCE is not supported, fall back to TONE_MAP_COLORIMETRIC. |
| 1990 | case ColorMode::BT2100_PQ: |
| 1991 | return displayDevice->hasBT2100PQEnhanceSupport() ? |
| 1992 | RenderIntent::TONE_MAP_ENHANCE : RenderIntent::TONE_MAP_COLORIMETRIC; |
| 1993 | case ColorMode::BT2100_HLG: |
| 1994 | return displayDevice->hasBT2100HLGEnhanceSupport() ? |
| 1995 | RenderIntent::TONE_MAP_ENHANCE : RenderIntent::TONE_MAP_COLORIMETRIC; |
| 1996 | // This statement shouldn't be reached, switch cases will always |
| 1997 | // cover all possible ColorMode returned by pickColorMode. |
| 1998 | default: |
| 1999 | return RenderIntent::COLORIMETRIC; |
| 2000 | } |
| 2001 | } |
| 2002 | |
| 2003 | // Either enhance is not supported or we are in natural mode. |
| 2004 | |
| 2005 | // Natural Mode means it's color managed and the color must be right, |
| 2006 | // thus we pick RenderIntent::COLORIMETRIC as render intent for non-HDR |
| 2007 | // content and pick RenderIntent::TONE_MAP_COLORIMETRIC for HDR content. |
| 2008 | switch (colorMode) { |
| 2009 | // In Natural Color Mode, BT2100_PQ and BT2100_HLG will only be picked |
| 2010 | // when TONE_MAP_COLORIMETRIC is supported. |
| 2011 | case ColorMode::BT2100_PQ: |
| 2012 | case ColorMode::BT2100_HLG: |
| 2013 | return RenderIntent::TONE_MAP_COLORIMETRIC; |
Peiyong Lin | dd9b2ae | 2018-03-01 16:22:45 -0800 | [diff] [blame] | 2014 | default: |
Peiyong Lin | 136fbbc | 2018-04-17 15:09:44 -0700 | [diff] [blame^] | 2015 | return RenderIntent::COLORIMETRIC; |
Peiyong Lin | dd9b2ae | 2018-03-01 16:22:45 -0800 | [diff] [blame] | 2016 | } |
Courtney Goeltzenleuchter | 5d94389 | 2017-03-22 13:46:46 -0600 | [diff] [blame] | 2017 | } |
| 2018 | |
Chia-I Wu | 30505fb | 2018-03-26 16:20:31 -0700 | [diff] [blame] | 2019 | void SurfaceFlinger::setUpHWComposer() { |
| 2020 | ATRACE_CALL(); |
| 2021 | ALOGV("setUpHWComposer"); |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 2022 | |
Jesse Hall | 028dc8f | 2013-08-20 16:35:32 -0700 | [diff] [blame] | 2023 | for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) { |
Dan Stoza | c7a25ad | 2018-04-12 11:45:09 -0700 | [diff] [blame] | 2024 | bool dirty = !mDisplays[dpy]->getDirtyRegion(mRepaintEverything).isEmpty(); |
Jesse Hall | b7a0549 | 2014-08-14 15:45:06 -0700 | [diff] [blame] | 2025 | bool empty = mDisplays[dpy]->getVisibleLayersSortedByZ().size() == 0; |
| 2026 | bool wasEmpty = !mDisplays[dpy]->lastCompositionHadVisibleLayers; |
| 2027 | |
| 2028 | // If nothing has changed (!dirty), don't recompose. |
| 2029 | // If something changed, but we don't currently have any visible layers, |
| 2030 | // and didn't when we last did a composition, then skip it this time. |
| 2031 | // The second rule does two things: |
| 2032 | // - When all layers are removed from a display, we'll emit one black |
| 2033 | // frame, then nothing more until we get new layers. |
| 2034 | // - When a display is created with a private layer stack, we won't |
| 2035 | // emit any black frames until a layer is added to the layer stack. |
| 2036 | bool mustRecompose = dirty && !(empty && wasEmpty); |
| 2037 | |
Dominik Laskowski | 281644e | 2018-04-19 15:47:35 -0700 | [diff] [blame] | 2038 | ALOGV_IF(mDisplays[dpy]->isVirtual(), |
Jesse Hall | b7a0549 | 2014-08-14 15:45:06 -0700 | [diff] [blame] | 2039 | "dpy[%zu]: %s composition (%sdirty %sempty %swasEmpty)", dpy, |
| 2040 | mustRecompose ? "doing" : "skipping", |
| 2041 | dirty ? "+" : "-", |
| 2042 | empty ? "+" : "-", |
| 2043 | wasEmpty ? "+" : "-"); |
| 2044 | |
Dan Stoza | 7143316 | 2014-02-04 16:22:36 -0800 | [diff] [blame] | 2045 | mDisplays[dpy]->beginFrame(mustRecompose); |
Jesse Hall | b7a0549 | 2014-08-14 15:45:06 -0700 | [diff] [blame] | 2046 | |
| 2047 | if (mustRecompose) { |
| 2048 | mDisplays[dpy]->lastCompositionHadVisibleLayers = !empty; |
| 2049 | } |
Jesse Hall | 028dc8f | 2013-08-20 16:35:32 -0700 | [diff] [blame] | 2050 | } |
| 2051 | |
Chia-I Wu | 30505fb | 2018-03-26 16:20:31 -0700 | [diff] [blame] | 2052 | // build the h/w work list |
| 2053 | if (CC_UNLIKELY(mGeometryInvalid)) { |
| 2054 | mGeometryInvalid = false; |
| 2055 | for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) { |
| 2056 | sp<const DisplayDevice> displayDevice(mDisplays[dpy]); |
| 2057 | const auto hwcId = displayDevice->getHwcDisplayId(); |
| 2058 | if (hwcId >= 0) { |
| 2059 | const Vector<sp<Layer>>& currentLayers( |
| 2060 | displayDevice->getVisibleLayersSortedByZ()); |
| 2061 | for (size_t i = 0; i < currentLayers.size(); i++) { |
| 2062 | const auto& layer = currentLayers[i]; |
| 2063 | if (!layer->hasHwcLayer(hwcId)) { |
| 2064 | if (!layer->createHwcLayer(getBE().mHwc.get(), hwcId)) { |
| 2065 | layer->forceClientComposition(hwcId); |
| 2066 | continue; |
| 2067 | } |
| 2068 | } |
| 2069 | |
| 2070 | layer->setGeometry(displayDevice, i); |
| 2071 | if (mDebugDisableHWC || mDebugRegion) { |
| 2072 | layer->forceClientComposition(hwcId); |
| 2073 | } |
| 2074 | } |
| 2075 | } |
| 2076 | } |
| 2077 | } |
| 2078 | |
Chia-I Wu | 30505fb | 2018-03-26 16:20:31 -0700 | [diff] [blame] | 2079 | // Set the per-frame data |
| 2080 | for (size_t displayId = 0; displayId < mDisplays.size(); ++displayId) { |
| 2081 | auto& displayDevice = mDisplays[displayId]; |
| 2082 | const auto hwcId = displayDevice->getHwcDisplayId(); |
| 2083 | |
| 2084 | if (hwcId < 0) { |
| 2085 | continue; |
| 2086 | } |
Chia-I Wu | 28f320b | 2018-05-03 11:02:56 -0700 | [diff] [blame] | 2087 | if (mDrawingState.colorMatrixChanged) { |
| 2088 | displayDevice->setColorTransform(mDrawingState.colorMatrix); |
| 2089 | status_t result = getBE().mHwc->setColorTransform(hwcId, mDrawingState.colorMatrix); |
Chia-I Wu | 30505fb | 2018-03-26 16:20:31 -0700 | [diff] [blame] | 2090 | ALOGE_IF(result != NO_ERROR, "Failed to set color transform on " |
| 2091 | "display %zd: %d", displayId, result); |
| 2092 | } |
| 2093 | for (auto& layer : displayDevice->getVisibleLayersSortedByZ()) { |
Peiyong Lin | 34beb7a | 2018-03-28 11:57:12 -0700 | [diff] [blame] | 2094 | if ((layer->getDataSpace() == Dataspace::BT2020_PQ || |
| 2095 | layer->getDataSpace() == Dataspace::BT2020_ITU_PQ) && |
Peiyong Lin | 6266589 | 2018-04-16 11:07:44 -0700 | [diff] [blame] | 2096 | !displayDevice->hasHDR10Support()) { |
Chia-I Wu | 30505fb | 2018-03-26 16:20:31 -0700 | [diff] [blame] | 2097 | layer->forceClientComposition(hwcId); |
| 2098 | } |
Peiyong Lin | f59a719 | 2018-04-25 11:19:31 -0700 | [diff] [blame] | 2099 | if ((layer->getDataSpace() == Dataspace::BT2020_HLG || |
| 2100 | layer->getDataSpace() == Dataspace::BT2020_ITU_HLG) && |
| 2101 | !displayDevice->hasHLGSupport()) { |
| 2102 | layer->forceClientComposition(hwcId); |
| 2103 | } |
Chia-I Wu | 30505fb | 2018-03-26 16:20:31 -0700 | [diff] [blame] | 2104 | |
| 2105 | if (layer->getForceClientComposition(hwcId)) { |
| 2106 | ALOGV("[%s] Requesting Client composition", layer->getName().string()); |
| 2107 | layer->setCompositionType(hwcId, HWC2::Composition::Client); |
| 2108 | continue; |
| 2109 | } |
| 2110 | |
| 2111 | layer->setPerFrameData(displayDevice); |
| 2112 | } |
| 2113 | |
| 2114 | if (hasWideColorDisplay) { |
Peiyong Lin | dd9b2ae | 2018-03-01 16:22:45 -0800 | [diff] [blame] | 2115 | ColorMode colorMode; |
| 2116 | Dataspace dataSpace; |
Peiyong Lin | 136fbbc | 2018-04-17 15:09:44 -0700 | [diff] [blame^] | 2117 | RenderIntent renderIntent; |
| 2118 | pickColorMode(displayDevice, &colorMode, &dataSpace, &renderIntent); |
| 2119 | setActiveColorModeInternal(displayDevice, colorMode, dataSpace, renderIntent); |
Chia-I Wu | 30505fb | 2018-03-26 16:20:31 -0700 | [diff] [blame] | 2120 | } |
| 2121 | } |
| 2122 | |
Chia-I Wu | 28f320b | 2018-05-03 11:02:56 -0700 | [diff] [blame] | 2123 | mDrawingState.colorMatrixChanged = false; |
Chia-I Wu | 30505fb | 2018-03-26 16:20:31 -0700 | [diff] [blame] | 2124 | |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 2125 | for (size_t displayId = 0; displayId < mDisplays.size(); ++displayId) { |
Dan Stoza | 7bdf55a | 2016-06-17 11:29:01 -0700 | [diff] [blame] | 2126 | auto& displayDevice = mDisplays[displayId]; |
| 2127 | if (!displayDevice->isDisplayOn()) { |
| 2128 | continue; |
| 2129 | } |
| 2130 | |
David Sodman | 105b7dc | 2017-11-04 20:28:14 -0700 | [diff] [blame] | 2131 | status_t result = displayDevice->prepareFrame(*getBE().mHwc); |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 2132 | ALOGE_IF(result != NO_ERROR, "prepareFrame for display %zd failed:" |
| 2133 | " %d (%s)", displayId, result, strerror(-result)); |
Mathias Agopian | 52bbb1a | 2012-07-31 19:01:53 -0700 | [diff] [blame] | 2134 | } |
Mathias Agopian | cd60f99 | 2012-08-16 16:28:27 -0700 | [diff] [blame] | 2135 | } |
Mathias Agopian | 52bbb1a | 2012-07-31 19:01:53 -0700 | [diff] [blame] | 2136 | |
Mathias Agopian | cd60f99 | 2012-08-16 16:28:27 -0700 | [diff] [blame] | 2137 | void SurfaceFlinger::doComposition() { |
| 2138 | ATRACE_CALL(); |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 2139 | ALOGV("doComposition"); |
| 2140 | |
Mathias Agopian | 52bbb1a | 2012-07-31 19:01:53 -0700 | [diff] [blame] | 2141 | const bool repaintEverything = android_atomic_and(0, &mRepaintEverything); |
Mathias Agopian | 92a979a | 2012-08-02 18:32:23 -0700 | [diff] [blame] | 2142 | for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) { |
Mathias Agopian | 4297734 | 2012-08-05 00:40:46 -0700 | [diff] [blame] | 2143 | const sp<DisplayDevice>& hw(mDisplays[dpy]); |
Prashant Malani | 2c9b11f | 2014-05-25 01:36:31 -0700 | [diff] [blame] | 2144 | if (hw->isDisplayOn()) { |
Mathias Agopian | cd60f99 | 2012-08-16 16:28:27 -0700 | [diff] [blame] | 2145 | // transform the dirty region into this screen's coordinate space |
| 2146 | const Region dirtyRegion(hw->getDirtyRegion(repaintEverything)); |
Mathias Agopian | 02b9510 | 2012-11-05 17:50:57 -0800 | [diff] [blame] | 2147 | |
| 2148 | // repaint the framebuffer (if needed) |
| 2149 | doDisplayComposition(hw, dirtyRegion); |
| 2150 | |
Mathias Agopian | cd60f99 | 2012-08-16 16:28:27 -0700 | [diff] [blame] | 2151 | hw->dirtyRegion.clear(); |
Chia-I Wu | b02087d | 2017-11-09 10:19:54 -0800 | [diff] [blame] | 2152 | hw->flip(); |
Mathias Agopian | 87baae1 | 2012-07-31 12:38:26 -0700 | [diff] [blame] | 2153 | } |
Mathias Agopian | 4fec873 | 2012-06-29 14:12:52 -0700 | [diff] [blame] | 2154 | } |
Mathias Agopian | 52bbb1a | 2012-07-31 19:01:53 -0700 | [diff] [blame] | 2155 | postFramebuffer(); |
Mathias Agopian | 4fec873 | 2012-06-29 14:12:52 -0700 | [diff] [blame] | 2156 | } |
| 2157 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2158 | void SurfaceFlinger::postFramebuffer() |
| 2159 | { |
Mathias Agopian | 841cde5 | 2012-03-01 15:44:37 -0800 | [diff] [blame] | 2160 | ATRACE_CALL(); |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 2161 | ALOGV("postFramebuffer"); |
Mathias Agopian | b048cef | 2012-02-04 15:44:04 -0800 | [diff] [blame] | 2162 | |
Mathias Agopian | a44b041 | 2011-10-16 18:46:35 -0700 | [diff] [blame] | 2163 | const nsecs_t now = systemTime(); |
| 2164 | mDebugInSwapBuffers = now; |
Jesse Hall | c5c5a14 | 2012-07-02 16:49:28 -0700 | [diff] [blame] | 2165 | |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 2166 | for (size_t displayId = 0; displayId < mDisplays.size(); ++displayId) { |
| 2167 | auto& displayDevice = mDisplays[displayId]; |
Dan Stoza | 7bdf55a | 2016-06-17 11:29:01 -0700 | [diff] [blame] | 2168 | if (!displayDevice->isDisplayOn()) { |
| 2169 | continue; |
| 2170 | } |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 2171 | const auto hwcId = displayDevice->getHwcDisplayId(); |
| 2172 | if (hwcId >= 0) { |
David Sodman | 105b7dc | 2017-11-04 20:28:14 -0700 | [diff] [blame] | 2173 | getBE().mHwc->presentAndGetReleaseFences(hwcId); |
Mathias Agopian | 2a23184 | 2012-09-24 18:12:35 -0700 | [diff] [blame] | 2174 | } |
Dan Stoza | 2dc3be8 | 2016-04-06 14:05:37 -0700 | [diff] [blame] | 2175 | displayDevice->onSwapBuffersCompleted(); |
Chia-I Wu | 7f40290 | 2017-11-09 12:51:10 -0800 | [diff] [blame] | 2176 | displayDevice->makeCurrent(); |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 2177 | for (auto& layer : displayDevice->getVisibleLayersSortedByZ()) { |
Chia-I Wu | 7b54959 | 2017-11-15 09:14:57 -0800 | [diff] [blame] | 2178 | // The layer buffer from the previous frame (if any) is released |
| 2179 | // by HWC only when the release fence from this frame (if any) is |
| 2180 | // signaled. Always get the release fence from HWC first. |
| 2181 | auto hwcLayer = layer->getHwcLayer(hwcId); |
David Sodman | 105b7dc | 2017-11-04 20:28:14 -0700 | [diff] [blame] | 2182 | sp<Fence> releaseFence = getBE().mHwc->getLayerReleaseFence(hwcId, hwcLayer); |
Chia-I Wu | 7b54959 | 2017-11-15 09:14:57 -0800 | [diff] [blame] | 2183 | |
| 2184 | // If the layer was client composited in the previous frame, we |
| 2185 | // need to merge with the previous client target acquire fence. |
| 2186 | // Since we do not track that, always merge with the current |
| 2187 | // client target acquire fence when it is available, even though |
| 2188 | // this is suboptimal. |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 2189 | if (layer->getCompositionType(hwcId) == HWC2::Composition::Client) { |
Chia-I Wu | 7b54959 | 2017-11-15 09:14:57 -0800 | [diff] [blame] | 2190 | releaseFence = Fence::merge("LayerRelease", releaseFence, |
| 2191 | displayDevice->getClientTargetAcquireFence()); |
Mathias Agopian | 52bbb1a | 2012-07-31 19:01:53 -0700 | [diff] [blame] | 2192 | } |
Chia-I Wu | 7b54959 | 2017-11-15 09:14:57 -0800 | [diff] [blame] | 2193 | |
David Sodman | b8af792 | 2017-12-21 15:17:55 -0800 | [diff] [blame] | 2194 | layer->getBE().onLayerDisplayed(releaseFence); |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 2195 | } |
Chia-I Wu | 8380689 | 2017-11-16 10:50:20 -0800 | [diff] [blame] | 2196 | |
| 2197 | // We've got a list of layers needing fences, that are disjoint with |
| 2198 | // displayDevice->getVisibleLayersSortedByZ. The best we can do is to |
| 2199 | // supply them with the present fence. |
| 2200 | if (!displayDevice->getLayersNeedingFences().isEmpty()) { |
David Sodman | 105b7dc | 2017-11-04 20:28:14 -0700 | [diff] [blame] | 2201 | sp<Fence> presentFence = getBE().mHwc->getPresentFence(hwcId); |
Chia-I Wu | 8380689 | 2017-11-16 10:50:20 -0800 | [diff] [blame] | 2202 | for (auto& layer : displayDevice->getLayersNeedingFences()) { |
David Sodman | b8af792 | 2017-12-21 15:17:55 -0800 | [diff] [blame] | 2203 | layer->getBE().onLayerDisplayed(presentFence); |
Chia-I Wu | 8380689 | 2017-11-16 10:50:20 -0800 | [diff] [blame] | 2204 | } |
| 2205 | } |
| 2206 | |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 2207 | if (hwcId >= 0) { |
David Sodman | 105b7dc | 2017-11-04 20:28:14 -0700 | [diff] [blame] | 2208 | getBE().mHwc->clearReleaseFences(hwcId); |
Jesse Hall | ef19414 | 2012-06-14 14:45:17 -0700 | [diff] [blame] | 2209 | } |
Jamie Gennis | e8696a4 | 2012-01-15 18:54:57 -0800 | [diff] [blame] | 2210 | } |
| 2211 | |
Mathias Agopian | a44b041 | 2011-10-16 18:46:35 -0700 | [diff] [blame] | 2212 | mLastSwapBufferTime = systemTime() - now; |
| 2213 | mDebugInSwapBuffers = 0; |
Jamie Gennis | 6547ff4 | 2013-07-16 20:12:42 -0700 | [diff] [blame] | 2214 | |
Stephen Kiazyk | 7d3dcb9 | 2017-04-05 16:46:49 -0700 | [diff] [blame] | 2215 | // |mStateLock| not needed as we are on the main thread |
Lloyd Pique | 2ae2b3b | 2017-12-14 17:18:17 -0800 | [diff] [blame] | 2216 | if (getBE().mHwc->isConnected(HWC_DISPLAY_PRIMARY)) { |
| 2217 | uint32_t flipCount = getDefaultDisplayDeviceLocked()->getPageFlipCount(); |
| 2218 | if (flipCount % LOG_FRAME_STATS_PERIOD == 0) { |
| 2219 | logFrameStats(); |
| 2220 | } |
Jamie Gennis | 6547ff4 | 2013-07-16 20:12:42 -0700 | [diff] [blame] | 2221 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2222 | } |
| 2223 | |
Mathias Agopian | 87baae1 | 2012-07-31 12:38:26 -0700 | [diff] [blame] | 2224 | void SurfaceFlinger::handleTransaction(uint32_t transactionFlags) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2225 | { |
Mathias Agopian | 841cde5 | 2012-03-01 15:44:37 -0800 | [diff] [blame] | 2226 | ATRACE_CALL(); |
| 2227 | |
Mathias Agopian | 7cc6df5 | 2013-06-05 14:30:54 -0700 | [diff] [blame] | 2228 | // here we keep a copy of the drawing state (that is the state that's |
| 2229 | // going to be overwritten by handleTransactionLocked()) outside of |
| 2230 | // mStateLock so that the side-effects of the State assignment |
| 2231 | // don't happen with mStateLock held (which can cause deadlocks). |
| 2232 | State drawingState(mDrawingState); |
| 2233 | |
Mathias Agopian | ca4d360 | 2011-05-19 15:38:14 -0700 | [diff] [blame] | 2234 | Mutex::Autolock _l(mStateLock); |
| 2235 | const nsecs_t now = systemTime(); |
| 2236 | mDebugInTransaction = now; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2237 | |
Mathias Agopian | ca4d360 | 2011-05-19 15:38:14 -0700 | [diff] [blame] | 2238 | // Here we're guaranteed that some transaction flags are set |
| 2239 | // so we can call handleTransactionLocked() unconditionally. |
| 2240 | // We call getTransactionFlags(), which will also clear the flags, |
| 2241 | // with mStateLock held to guarantee that mCurrentState won't change |
| 2242 | // until the transaction is committed. |
Mathias Agopian | 4da7519 | 2010-08-10 17:19:56 -0700 | [diff] [blame] | 2243 | |
Jorim Jaggi | f15c3be | 2018-04-12 12:56:58 +0100 | [diff] [blame] | 2244 | mVsyncModulator.onTransactionHandled(); |
Mathias Agopian | e57f292 | 2012-08-09 16:29:12 -0700 | [diff] [blame] | 2245 | transactionFlags = getTransactionFlags(eTransactionMask); |
Mathias Agopian | 87baae1 | 2012-07-31 12:38:26 -0700 | [diff] [blame] | 2246 | handleTransactionLocked(transactionFlags); |
Mathias Agopian | dea20b1 | 2011-05-03 17:04:02 -0700 | [diff] [blame] | 2247 | |
Mathias Agopian | ca4d360 | 2011-05-19 15:38:14 -0700 | [diff] [blame] | 2248 | mLastTransactionTime = systemTime() - now; |
| 2249 | mDebugInTransaction = 0; |
| 2250 | invalidateHwcGeometry(); |
| 2251 | // here the transaction has been committed |
Mathias Agopian | 3d57964 | 2009-06-04 18:46:21 -0700 | [diff] [blame] | 2252 | } |
| 2253 | |
Lloyd Pique | 715a2c1 | 2017-12-14 17:18:08 -0800 | [diff] [blame] | 2254 | DisplayDevice::DisplayType SurfaceFlinger::determineDisplayType(hwc2_display_t display, |
Lloyd Pique | 99d3da5 | 2018-01-22 17:48:03 -0800 | [diff] [blame] | 2255 | HWC2::Connection connection) const { |
Lloyd Pique | 715a2c1 | 2017-12-14 17:18:08 -0800 | [diff] [blame] | 2256 | // Figure out whether the event is for the primary display or an |
| 2257 | // external display by matching the Hwc display id against one for a |
| 2258 | // connected display. If we did not find a match, we then check what |
| 2259 | // displays are not already connected to determine the type. If we don't |
| 2260 | // have a connected primary display, we assume the new display is meant to |
| 2261 | // be the primary display, and then if we don't have an external display, |
| 2262 | // we assume it is that. |
| 2263 | const auto primaryDisplayId = |
| 2264 | getBE().mHwc->getHwcDisplayId(DisplayDevice::DISPLAY_PRIMARY); |
| 2265 | const auto externalDisplayId = |
| 2266 | getBE().mHwc->getHwcDisplayId(DisplayDevice::DISPLAY_EXTERNAL); |
| 2267 | if (primaryDisplayId && primaryDisplayId == display) { |
| 2268 | return DisplayDevice::DISPLAY_PRIMARY; |
| 2269 | } else if (externalDisplayId && externalDisplayId == display) { |
Dominik Laskowski | e9ef7c4 | 2018-03-12 19:34:30 -0700 | [diff] [blame] | 2270 | return DisplayDevice::DISPLAY_EXTERNAL; |
Lloyd Pique | 715a2c1 | 2017-12-14 17:18:08 -0800 | [diff] [blame] | 2271 | } else if (connection == HWC2::Connection::Connected && !primaryDisplayId) { |
| 2272 | return DisplayDevice::DISPLAY_PRIMARY; |
| 2273 | } else if (connection == HWC2::Connection::Connected && !externalDisplayId) { |
| 2274 | return DisplayDevice::DISPLAY_EXTERNAL; |
| 2275 | } |
| 2276 | |
| 2277 | return DisplayDevice::DISPLAY_ID_INVALID; |
| 2278 | } |
| 2279 | |
Lloyd Pique | ba04e62 | 2017-12-14 17:11:26 -0800 | [diff] [blame] | 2280 | void SurfaceFlinger::processDisplayHotplugEventsLocked() { |
| 2281 | for (const auto& event : mPendingHotplugEvents) { |
Lloyd Pique | 715a2c1 | 2017-12-14 17:18:08 -0800 | [diff] [blame] | 2282 | auto displayType = determineDisplayType(event.display, event.connection); |
| 2283 | if (displayType == DisplayDevice::DISPLAY_ID_INVALID) { |
| 2284 | ALOGW("Unable to determine the display type for display %" PRIu64, event.display); |
| 2285 | continue; |
| 2286 | } |
Lloyd Pique | ba04e62 | 2017-12-14 17:11:26 -0800 | [diff] [blame] | 2287 | |
| 2288 | if (getBE().mHwc->isUsingVrComposer() && displayType == DisplayDevice::DISPLAY_EXTERNAL) { |
| 2289 | ALOGE("External displays are not supported by the vr hardware composer."); |
| 2290 | continue; |
| 2291 | } |
| 2292 | |
Dominik Laskowski | e9ef7c4 | 2018-03-12 19:34:30 -0700 | [diff] [blame] | 2293 | const auto displayId = |
| 2294 | getBE().mHwc->onHotplug(event.display, displayType, event.connection); |
| 2295 | if (displayId) { |
| 2296 | ALOGV("Display %" PRIu64 " has stable ID %" PRIu64, event.display, *displayId); |
| 2297 | } |
Lloyd Pique | ba04e62 | 2017-12-14 17:11:26 -0800 | [diff] [blame] | 2298 | |
| 2299 | if (event.connection == HWC2::Connection::Connected) { |
Steven Thomas | eb6d205 | 2018-03-20 15:40:48 -0700 | [diff] [blame] | 2300 | if (!mBuiltinDisplays[displayType].get()) { |
| 2301 | ALOGV("Creating built in display %d", displayType); |
| 2302 | mBuiltinDisplays[displayType] = new BBinder(); |
Dominik Laskowski | 663bd28 | 2018-04-19 15:26:54 -0700 | [diff] [blame] | 2303 | DisplayDeviceState info; |
| 2304 | info.type = displayType; |
Steven Thomas | eb6d205 | 2018-03-20 15:40:48 -0700 | [diff] [blame] | 2305 | info.displayName = displayType == DisplayDevice::DISPLAY_PRIMARY ? |
| 2306 | "Built-in Screen" : "External Screen"; |
Dominik Laskowski | 663bd28 | 2018-04-19 15:26:54 -0700 | [diff] [blame] | 2307 | info.isSecure = true; // All physical displays are currently considered secure. |
Steven Thomas | eb6d205 | 2018-03-20 15:40:48 -0700 | [diff] [blame] | 2308 | mCurrentState.displays.add(mBuiltinDisplays[displayType], info); |
| 2309 | mInterceptor->saveDisplayCreation(info); |
| 2310 | } |
Lloyd Pique | ba04e62 | 2017-12-14 17:11:26 -0800 | [diff] [blame] | 2311 | } else { |
Lloyd Pique | fcd8661 | 2017-12-14 17:15:36 -0800 | [diff] [blame] | 2312 | ALOGV("Removing built in display %d", displayType); |
Lloyd Pique | ba04e62 | 2017-12-14 17:11:26 -0800 | [diff] [blame] | 2313 | |
Lloyd Pique | fcd8661 | 2017-12-14 17:15:36 -0800 | [diff] [blame] | 2314 | ssize_t idx = mCurrentState.displays.indexOfKey(mBuiltinDisplays[displayType]); |
| 2315 | if (idx >= 0) { |
| 2316 | const DisplayDeviceState& info(mCurrentState.displays.valueAt(idx)); |
Dominik Laskowski | 663bd28 | 2018-04-19 15:26:54 -0700 | [diff] [blame] | 2317 | mInterceptor->saveDisplayDeletion(info.sequenceId); |
Lloyd Pique | fcd8661 | 2017-12-14 17:15:36 -0800 | [diff] [blame] | 2318 | mCurrentState.displays.removeItemsAt(idx); |
| 2319 | } |
| 2320 | mBuiltinDisplays[displayType].clear(); |
Lloyd Pique | ba04e62 | 2017-12-14 17:11:26 -0800 | [diff] [blame] | 2321 | } |
| 2322 | |
| 2323 | processDisplayChangesLocked(); |
| 2324 | } |
| 2325 | |
| 2326 | mPendingHotplugEvents.clear(); |
| 2327 | } |
| 2328 | |
Lloyd Pique | 99d3da5 | 2018-01-22 17:48:03 -0800 | [diff] [blame] | 2329 | sp<DisplayDevice> SurfaceFlinger::setupNewDisplayDeviceInternal( |
| 2330 | const wp<IBinder>& display, int hwcId, const DisplayDeviceState& state, |
| 2331 | const sp<DisplaySurface>& dispSurface, const sp<IGraphicBufferProducer>& producer) { |
Peiyong Lin | dd9b2ae | 2018-03-01 16:22:45 -0800 | [diff] [blame] | 2332 | bool hasWideColorGamut = false; |
Peiyong Lin | 136fbbc | 2018-04-17 15:09:44 -0700 | [diff] [blame^] | 2333 | std::unordered_map<ColorMode, std::vector<RenderIntent>> hdrAndRenderIntents; |
| 2334 | |
Lloyd Pique | 99d3da5 | 2018-01-22 17:48:03 -0800 | [diff] [blame] | 2335 | if (hasWideColorDisplay) { |
Peiyong Lin | dd9b2ae | 2018-03-01 16:22:45 -0800 | [diff] [blame] | 2336 | std::vector<ColorMode> modes = getHwComposer().getColorModes(hwcId); |
Lloyd Pique | 99d3da5 | 2018-01-22 17:48:03 -0800 | [diff] [blame] | 2337 | for (ColorMode colorMode : modes) { |
| 2338 | switch (colorMode) { |
| 2339 | case ColorMode::DISPLAY_P3: |
| 2340 | case ColorMode::ADOBE_RGB: |
| 2341 | case ColorMode::DCI_P3: |
Peiyong Lin | dd9b2ae | 2018-03-01 16:22:45 -0800 | [diff] [blame] | 2342 | hasWideColorGamut = true; |
Lloyd Pique | 99d3da5 | 2018-01-22 17:48:03 -0800 | [diff] [blame] | 2343 | break; |
| 2344 | default: |
| 2345 | break; |
| 2346 | } |
Peiyong Lin | dd9b2ae | 2018-03-01 16:22:45 -0800 | [diff] [blame] | 2347 | |
| 2348 | std::vector<RenderIntent> renderIntents = getHwComposer().getRenderIntents(hwcId, |
| 2349 | colorMode); |
| 2350 | if (state.type == DisplayDevice::DISPLAY_PRIMARY) { |
| 2351 | for (auto intent : renderIntents) { |
| 2352 | if (intent == RenderIntent::ENHANCE) { |
| 2353 | mBuiltinDisplaySupportsEnhance = true; |
| 2354 | break; |
| 2355 | } |
| 2356 | } |
| 2357 | } |
Peiyong Lin | 136fbbc | 2018-04-17 15:09:44 -0700 | [diff] [blame^] | 2358 | |
| 2359 | if (colorMode == ColorMode::BT2100_PQ || colorMode == ColorMode::BT2100_HLG) { |
| 2360 | hdrAndRenderIntents.emplace(colorMode, renderIntents); |
| 2361 | } |
Lloyd Pique | 99d3da5 | 2018-01-22 17:48:03 -0800 | [diff] [blame] | 2362 | } |
| 2363 | } |
| 2364 | |
Peiyong Lin | 6266589 | 2018-04-16 11:07:44 -0700 | [diff] [blame] | 2365 | HdrCapabilities hdrCapabilities; |
| 2366 | getHwComposer().getHdrCapabilities(hwcId, &hdrCapabilities); |
Lloyd Pique | 99d3da5 | 2018-01-22 17:48:03 -0800 | [diff] [blame] | 2367 | |
| 2368 | auto nativeWindowSurface = mCreateNativeWindowSurface(producer); |
| 2369 | auto nativeWindow = nativeWindowSurface->getNativeWindow(); |
| 2370 | |
| 2371 | /* |
| 2372 | * Create our display's surface |
| 2373 | */ |
| 2374 | std::unique_ptr<RE::Surface> renderSurface = getRenderEngine().createSurface(); |
| 2375 | renderSurface->setCritical(state.type == DisplayDevice::DISPLAY_PRIMARY); |
Dominik Laskowski | 281644e | 2018-04-19 15:47:35 -0700 | [diff] [blame] | 2376 | renderSurface->setAsync(state.isVirtual()); |
Lloyd Pique | 99d3da5 | 2018-01-22 17:48:03 -0800 | [diff] [blame] | 2377 | renderSurface->setNativeWindow(nativeWindow.get()); |
| 2378 | const int displayWidth = renderSurface->queryWidth(); |
| 2379 | const int displayHeight = renderSurface->queryHeight(); |
| 2380 | |
| 2381 | // Make sure that composition can never be stalled by a virtual display |
| 2382 | // consumer that isn't processing buffers fast enough. We have to do this |
| 2383 | // in two places: |
| 2384 | // * Here, in case the display is composed entirely by HWC. |
| 2385 | // * In makeCurrent(), using eglSwapInterval. Some EGL drivers set the |
| 2386 | // window's swap interval in eglMakeCurrent, so they'll override the |
| 2387 | // interval we set here. |
Dominik Laskowski | 281644e | 2018-04-19 15:47:35 -0700 | [diff] [blame] | 2388 | if (state.isVirtual()) { |
Lloyd Pique | 99d3da5 | 2018-01-22 17:48:03 -0800 | [diff] [blame] | 2389 | nativeWindow->setSwapInterval(nativeWindow.get(), 0); |
| 2390 | } |
| 2391 | |
| 2392 | // virtual displays are always considered enabled |
Dominik Laskowski | 281644e | 2018-04-19 15:47:35 -0700 | [diff] [blame] | 2393 | auto initialPowerMode = state.isVirtual() ? HWC_POWER_MODE_NORMAL : HWC_POWER_MODE_OFF; |
Lloyd Pique | 99d3da5 | 2018-01-22 17:48:03 -0800 | [diff] [blame] | 2394 | |
| 2395 | sp<DisplayDevice> hw = |
| 2396 | new DisplayDevice(this, state.type, hwcId, state.isSecure, display, nativeWindow, |
| 2397 | dispSurface, std::move(renderSurface), displayWidth, displayHeight, |
Peiyong Lin | 0ac5f4e | 2018-04-19 22:06:34 -0700 | [diff] [blame] | 2398 | hasWideColorGamut, hdrCapabilities, |
| 2399 | getHwComposer().getSupportedPerFrameMetadata(hwcId), |
Peiyong Lin | 136fbbc | 2018-04-17 15:09:44 -0700 | [diff] [blame^] | 2400 | hdrAndRenderIntents, initialPowerMode); |
Lloyd Pique | 99d3da5 | 2018-01-22 17:48:03 -0800 | [diff] [blame] | 2401 | |
| 2402 | if (maxFrameBufferAcquiredBuffers >= 3) { |
| 2403 | nativeWindowSurface->preallocateBuffers(); |
| 2404 | } |
| 2405 | |
| 2406 | ColorMode defaultColorMode = ColorMode::NATIVE; |
Peiyong Lin | dd9b2ae | 2018-03-01 16:22:45 -0800 | [diff] [blame] | 2407 | Dataspace defaultDataSpace = Dataspace::UNKNOWN; |
| 2408 | if (hasWideColorGamut) { |
Lloyd Pique | 99d3da5 | 2018-01-22 17:48:03 -0800 | [diff] [blame] | 2409 | defaultColorMode = ColorMode::SRGB; |
Peiyong Lin | dd9b2ae | 2018-03-01 16:22:45 -0800 | [diff] [blame] | 2410 | defaultDataSpace = Dataspace::V0_SRGB; |
Lloyd Pique | 99d3da5 | 2018-01-22 17:48:03 -0800 | [diff] [blame] | 2411 | } |
Peiyong Lin | 136fbbc | 2018-04-17 15:09:44 -0700 | [diff] [blame^] | 2412 | setActiveColorModeInternal(hw, defaultColorMode, defaultDataSpace, |
| 2413 | RenderIntent::COLORIMETRIC); |
Lloyd Pique | 99d3da5 | 2018-01-22 17:48:03 -0800 | [diff] [blame] | 2414 | hw->setLayerStack(state.layerStack); |
| 2415 | hw->setProjection(state.orientation, state.viewport, state.frame); |
| 2416 | hw->setDisplayName(state.displayName); |
| 2417 | |
| 2418 | return hw; |
| 2419 | } |
| 2420 | |
Lloyd Pique | 347200f | 2017-12-14 17:00:15 -0800 | [diff] [blame] | 2421 | void SurfaceFlinger::processDisplayChangesLocked() { |
| 2422 | // here we take advantage of Vector's copy-on-write semantics to |
| 2423 | // improve performance by skipping the transaction entirely when |
| 2424 | // know that the lists are identical |
| 2425 | const KeyedVector<wp<IBinder>, DisplayDeviceState>& curr(mCurrentState.displays); |
| 2426 | const KeyedVector<wp<IBinder>, DisplayDeviceState>& draw(mDrawingState.displays); |
| 2427 | if (!curr.isIdenticalTo(draw)) { |
| 2428 | mVisibleRegionsDirty = true; |
| 2429 | const size_t cc = curr.size(); |
| 2430 | size_t dc = draw.size(); |
| 2431 | |
| 2432 | // find the displays that were removed |
| 2433 | // (ie: in drawing state but not in current state) |
| 2434 | // also handle displays that changed |
| 2435 | // (ie: displays that are in both lists) |
| 2436 | for (size_t i = 0; i < dc;) { |
| 2437 | const ssize_t j = curr.indexOfKey(draw.keyAt(i)); |
| 2438 | if (j < 0) { |
| 2439 | // in drawing state but not in current state |
Lloyd Pique | 2ae2b3b | 2017-12-14 17:18:17 -0800 | [diff] [blame] | 2440 | // Call makeCurrent() on the primary display so we can |
| 2441 | // be sure that nothing associated with this display |
| 2442 | // is current. |
| 2443 | const sp<const DisplayDevice> defaultDisplay(getDefaultDisplayDeviceLocked()); |
| 2444 | if (defaultDisplay != nullptr) defaultDisplay->makeCurrent(); |
| 2445 | sp<DisplayDevice> hw(getDisplayDeviceLocked(draw.keyAt(i))); |
| 2446 | if (hw != nullptr) hw->disconnect(getHwComposer()); |
| 2447 | if (draw[i].type < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) |
| 2448 | mEventThread->onHotplugReceived(draw[i].type, false); |
| 2449 | mDisplays.removeItem(draw.keyAt(i)); |
Lloyd Pique | 347200f | 2017-12-14 17:00:15 -0800 | [diff] [blame] | 2450 | } else { |
| 2451 | // this display is in both lists. see if something changed. |
| 2452 | const DisplayDeviceState& state(curr[j]); |
| 2453 | const wp<IBinder>& display(curr.keyAt(j)); |
| 2454 | const sp<IBinder> state_binder = IInterface::asBinder(state.surface); |
| 2455 | const sp<IBinder> draw_binder = IInterface::asBinder(draw[i].surface); |
| 2456 | if (state_binder != draw_binder) { |
| 2457 | // changing the surface is like destroying and |
| 2458 | // recreating the DisplayDevice, so we just remove it |
| 2459 | // from the drawing state, so that it get re-added |
| 2460 | // below. |
| 2461 | sp<DisplayDevice> hw(getDisplayDeviceLocked(display)); |
| 2462 | if (hw != nullptr) hw->disconnect(getHwComposer()); |
| 2463 | mDisplays.removeItem(display); |
| 2464 | mDrawingState.displays.removeItemsAt(i); |
| 2465 | dc--; |
| 2466 | // at this point we must loop to the next item |
| 2467 | continue; |
| 2468 | } |
| 2469 | |
| 2470 | const sp<DisplayDevice> disp(getDisplayDeviceLocked(display)); |
| 2471 | if (disp != nullptr) { |
| 2472 | if (state.layerStack != draw[i].layerStack) { |
| 2473 | disp->setLayerStack(state.layerStack); |
| 2474 | } |
| 2475 | if ((state.orientation != draw[i].orientation) || |
| 2476 | (state.viewport != draw[i].viewport) || (state.frame != draw[i].frame)) { |
| 2477 | disp->setProjection(state.orientation, state.viewport, state.frame); |
| 2478 | } |
| 2479 | if (state.width != draw[i].width || state.height != draw[i].height) { |
| 2480 | disp->setDisplaySize(state.width, state.height); |
| 2481 | } |
| 2482 | } |
| 2483 | } |
| 2484 | ++i; |
| 2485 | } |
| 2486 | |
| 2487 | // find displays that were added |
| 2488 | // (ie: in current state but not in drawing state) |
| 2489 | for (size_t i = 0; i < cc; i++) { |
| 2490 | if (draw.indexOfKey(curr.keyAt(i)) < 0) { |
| 2491 | const DisplayDeviceState& state(curr[i]); |
| 2492 | |
| 2493 | sp<DisplaySurface> dispSurface; |
| 2494 | sp<IGraphicBufferProducer> producer; |
| 2495 | sp<IGraphicBufferProducer> bqProducer; |
| 2496 | sp<IGraphicBufferConsumer> bqConsumer; |
Lloyd Pique | 12eb423 | 2018-01-17 11:54:43 -0800 | [diff] [blame] | 2497 | mCreateBufferQueue(&bqProducer, &bqConsumer, false); |
Lloyd Pique | 347200f | 2017-12-14 17:00:15 -0800 | [diff] [blame] | 2498 | |
| 2499 | int32_t hwcId = -1; |
Dominik Laskowski | 663bd28 | 2018-04-19 15:26:54 -0700 | [diff] [blame] | 2500 | if (state.isVirtual()) { |
Lloyd Pique | 347200f | 2017-12-14 17:00:15 -0800 | [diff] [blame] | 2501 | // Virtual displays without a surface are dormant: |
| 2502 | // they have external state (layer stack, projection, |
| 2503 | // etc.) but no internal state (i.e. a DisplayDevice). |
| 2504 | if (state.surface != nullptr) { |
| 2505 | // Allow VR composer to use virtual displays. |
| 2506 | if (mUseHwcVirtualDisplays || getBE().mHwc->isUsingVrComposer()) { |
| 2507 | int width = 0; |
| 2508 | int status = state.surface->query(NATIVE_WINDOW_WIDTH, &width); |
| 2509 | ALOGE_IF(status != NO_ERROR, "Unable to query width (%d)", status); |
| 2510 | int height = 0; |
| 2511 | status = state.surface->query(NATIVE_WINDOW_HEIGHT, &height); |
| 2512 | ALOGE_IF(status != NO_ERROR, "Unable to query height (%d)", status); |
| 2513 | int intFormat = 0; |
| 2514 | status = state.surface->query(NATIVE_WINDOW_FORMAT, &intFormat); |
| 2515 | ALOGE_IF(status != NO_ERROR, "Unable to query format (%d)", status); |
Peiyong Lin | 34beb7a | 2018-03-28 11:57:12 -0700 | [diff] [blame] | 2516 | auto format = static_cast<ui::PixelFormat>(intFormat); |
Lloyd Pique | 347200f | 2017-12-14 17:00:15 -0800 | [diff] [blame] | 2517 | |
| 2518 | getBE().mHwc->allocateVirtualDisplay(width, height, &format, &hwcId); |
| 2519 | } |
| 2520 | |
| 2521 | // TODO: Plumb requested format back up to consumer |
| 2522 | |
| 2523 | sp<VirtualDisplaySurface> vds = |
| 2524 | new VirtualDisplaySurface(*getBE().mHwc, hwcId, state.surface, |
| 2525 | bqProducer, bqConsumer, |
| 2526 | state.displayName); |
| 2527 | |
| 2528 | dispSurface = vds; |
| 2529 | producer = vds; |
| 2530 | } |
| 2531 | } else { |
| 2532 | ALOGE_IF(state.surface != nullptr, |
| 2533 | "adding a supported display, but rendering " |
| 2534 | "surface is provided (%p), ignoring it", |
| 2535 | state.surface.get()); |
| 2536 | |
| 2537 | hwcId = state.type; |
| 2538 | dispSurface = new FramebufferSurface(*getBE().mHwc, hwcId, bqConsumer); |
| 2539 | producer = bqProducer; |
| 2540 | } |
| 2541 | |
| 2542 | const wp<IBinder>& display(curr.keyAt(i)); |
| 2543 | if (dispSurface != nullptr) { |
Lloyd Pique | 99d3da5 | 2018-01-22 17:48:03 -0800 | [diff] [blame] | 2544 | mDisplays.add(display, |
| 2545 | setupNewDisplayDeviceInternal(display, hwcId, state, dispSurface, |
| 2546 | producer)); |
Dominik Laskowski | 663bd28 | 2018-04-19 15:26:54 -0700 | [diff] [blame] | 2547 | if (!state.isVirtual()) { |
Lloyd Pique | 347200f | 2017-12-14 17:00:15 -0800 | [diff] [blame] | 2548 | mEventThread->onHotplugReceived(state.type, true); |
| 2549 | } |
| 2550 | } |
| 2551 | } |
| 2552 | } |
| 2553 | } |
Lloyd Pique | ba04e62 | 2017-12-14 17:11:26 -0800 | [diff] [blame] | 2554 | |
| 2555 | mDrawingState.displays = mCurrentState.displays; |
Lloyd Pique | 347200f | 2017-12-14 17:00:15 -0800 | [diff] [blame] | 2556 | } |
| 2557 | |
Mathias Agopian | 87baae1 | 2012-07-31 12:38:26 -0700 | [diff] [blame] | 2558 | void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags) |
Mathias Agopian | 3d57964 | 2009-06-04 18:46:21 -0700 | [diff] [blame] | 2559 | { |
Dan Stoza | 7dde599 | 2015-05-22 09:51:44 -0700 | [diff] [blame] | 2560 | // Notify all layers of available frames |
Robert Carr | 2047fae | 2016-11-28 14:09:09 -0800 | [diff] [blame] | 2561 | mCurrentState.traverseInZOrder([](Layer* layer) { |
| 2562 | layer->notifyAvailableFrames(); |
| 2563 | }); |
Dan Stoza | 7dde599 | 2015-05-22 09:51:44 -0700 | [diff] [blame] | 2564 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2565 | /* |
| 2566 | * Traversal of the children |
| 2567 | * (perform the transaction for each of them if needed) |
| 2568 | */ |
| 2569 | |
Mathias Agopian | 3559b07 | 2012-08-15 13:46:03 -0700 | [diff] [blame] | 2570 | if (transactionFlags & eTraversalNeeded) { |
Robert Carr | 2047fae | 2016-11-28 14:09:09 -0800 | [diff] [blame] | 2571 | mCurrentState.traverseInZOrder([&](Layer* layer) { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2572 | uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded); |
Robert Carr | 2047fae | 2016-11-28 14:09:09 -0800 | [diff] [blame] | 2573 | if (!trFlags) return; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2574 | |
| 2575 | const uint32_t flags = layer->doTransaction(0); |
| 2576 | if (flags & Layer::eVisibleRegion) |
| 2577 | mVisibleRegionsDirty = true; |
Robert Carr | 2047fae | 2016-11-28 14:09:09 -0800 | [diff] [blame] | 2578 | }); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2579 | } |
| 2580 | |
| 2581 | /* |
Mathias Agopian | 3559b07 | 2012-08-15 13:46:03 -0700 | [diff] [blame] | 2582 | * Perform display own transactions if needed |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2583 | */ |
| 2584 | |
Mathias Agopian | e57f292 | 2012-08-09 16:29:12 -0700 | [diff] [blame] | 2585 | if (transactionFlags & eDisplayTransactionNeeded) { |
Lloyd Pique | 347200f | 2017-12-14 17:00:15 -0800 | [diff] [blame] | 2586 | processDisplayChangesLocked(); |
Lloyd Pique | ba04e62 | 2017-12-14 17:11:26 -0800 | [diff] [blame] | 2587 | processDisplayHotplugEventsLocked(); |
Mathias Agopian | 3559b07 | 2012-08-15 13:46:03 -0700 | [diff] [blame] | 2588 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2589 | |
Lloyd Pique | d432a7c | 2018-03-23 16:05:31 -0700 | [diff] [blame] | 2590 | if (transactionFlags & (eDisplayLayerStackChanged|eDisplayTransactionNeeded)) { |
Mathias Agopian | 8430095 | 2012-11-21 16:02:13 -0800 | [diff] [blame] | 2591 | // The transform hint might have changed for some layers |
| 2592 | // (either because a display has changed, or because a layer |
| 2593 | // as changed). |
| 2594 | // |
| 2595 | // Walk through all the layers in currentLayers, |
| 2596 | // and update their transform hint. |
| 2597 | // |
| 2598 | // If a layer is visible only on a single display, then that |
| 2599 | // display is used to calculate the hint, otherwise we use the |
| 2600 | // default display. |
| 2601 | // |
| 2602 | // NOTE: we do this here, rather than in rebuildLayerStacks() so that |
| 2603 | // the hint is set before we acquire a buffer from the surface texture. |
| 2604 | // |
| 2605 | // NOTE: layer transactions have taken place already, so we use their |
| 2606 | // drawing state. However, SurfaceFlinger's own transaction has not |
| 2607 | // happened yet, so we must use the current state layer list |
| 2608 | // (soon to become the drawing state list). |
| 2609 | // |
| 2610 | sp<const DisplayDevice> disp; |
| 2611 | uint32_t currentlayerStack = 0; |
Robert Carr | 2047fae | 2016-11-28 14:09:09 -0800 | [diff] [blame] | 2612 | bool first = true; |
| 2613 | mCurrentState.traverseInZOrder([&](Layer* layer) { |
Mathias Agopian | 8430095 | 2012-11-21 16:02:13 -0800 | [diff] [blame] | 2614 | // NOTE: we rely on the fact that layers are sorted by |
| 2615 | // layerStack first (so we don't have to traverse the list |
| 2616 | // of displays for every layer). |
Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 2617 | uint32_t layerStack = layer->getLayerStack(); |
Robert Carr | 2047fae | 2016-11-28 14:09:09 -0800 | [diff] [blame] | 2618 | if (first || currentlayerStack != layerStack) { |
Mathias Agopian | 8430095 | 2012-11-21 16:02:13 -0800 | [diff] [blame] | 2619 | currentlayerStack = layerStack; |
| 2620 | // figure out if this layerstack is mirrored |
| 2621 | // (more than one display) if so, pick the default display, |
| 2622 | // if not, pick the only display it's on. |
| 2623 | disp.clear(); |
| 2624 | for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) { |
| 2625 | sp<const DisplayDevice> hw(mDisplays[dpy]); |
Chia-I Wu | ab0c319 | 2017-08-01 11:29:00 -0700 | [diff] [blame] | 2626 | if (layer->belongsToDisplay(hw->getLayerStack(), hw->isPrimary())) { |
Peiyong Lin | 566a3b4 | 2018-01-09 18:22:43 -0800 | [diff] [blame] | 2627 | if (disp == nullptr) { |
George Burgess IV | 406a285 | 2017-08-29 17:57:25 -0700 | [diff] [blame] | 2628 | disp = std::move(hw); |
Mathias Agopian | 8430095 | 2012-11-21 16:02:13 -0800 | [diff] [blame] | 2629 | } else { |
Peiyong Lin | 566a3b4 | 2018-01-09 18:22:43 -0800 | [diff] [blame] | 2630 | disp = nullptr; |
Mathias Agopian | 8430095 | 2012-11-21 16:02:13 -0800 | [diff] [blame] | 2631 | break; |
| 2632 | } |
| 2633 | } |
| 2634 | } |
| 2635 | } |
Chet Haase | 91d2593 | 2013-04-11 15:24:55 -0700 | [diff] [blame] | 2636 | |
Lloyd Pique | d432a7c | 2018-03-23 16:05:31 -0700 | [diff] [blame] | 2637 | if (disp == nullptr) { |
| 2638 | // NOTE: TEMPORARY FIX ONLY. Real fix should cause layers to |
| 2639 | // redraw after transform hint changes. See bug 8508397. |
Robert Carr | 56a0b9a | 2017-12-04 16:06:13 -0800 | [diff] [blame] | 2640 | |
Lloyd Pique | d432a7c | 2018-03-23 16:05:31 -0700 | [diff] [blame] | 2641 | // could be null when this layer is using a layerStack |
| 2642 | // that is not visible on any display. Also can occur at |
| 2643 | // screen off/on times. |
| 2644 | disp = getDefaultDisplayDeviceLocked(); |
Mathias Agopian | 8430095 | 2012-11-21 16:02:13 -0800 | [diff] [blame] | 2645 | } |
Lloyd Pique | d432a7c | 2018-03-23 16:05:31 -0700 | [diff] [blame] | 2646 | |
| 2647 | // disp can be null if there is no display available at all to get |
| 2648 | // the transform hint from. |
Lloyd Pique | 2ae2b3b | 2017-12-14 17:18:17 -0800 | [diff] [blame] | 2649 | if (disp != nullptr) { |
| 2650 | layer->updateTransformHint(disp); |
| 2651 | } |
Robert Carr | 2047fae | 2016-11-28 14:09:09 -0800 | [diff] [blame] | 2652 | |
| 2653 | first = false; |
| 2654 | }); |
Mathias Agopian | 8430095 | 2012-11-21 16:02:13 -0800 | [diff] [blame] | 2655 | } |
| 2656 | |
| 2657 | |
Mathias Agopian | 3559b07 | 2012-08-15 13:46:03 -0700 | [diff] [blame] | 2658 | /* |
| 2659 | * Perform our own transaction if needed |
| 2660 | */ |
Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 2661 | |
| 2662 | if (mLayersAdded) { |
| 2663 | mLayersAdded = false; |
| 2664 | // Layers have been added. |
Mathias Agopian | 3559b07 | 2012-08-15 13:46:03 -0700 | [diff] [blame] | 2665 | mVisibleRegionsDirty = true; |
| 2666 | } |
| 2667 | |
| 2668 | // some layers might have been removed, so |
| 2669 | // we need to update the regions they're exposing. |
| 2670 | if (mLayersRemoved) { |
| 2671 | mLayersRemoved = false; |
| 2672 | mVisibleRegionsDirty = true; |
Robert Carr | 2047fae | 2016-11-28 14:09:09 -0800 | [diff] [blame] | 2673 | mDrawingState.traverseInZOrder([&](Layer* layer) { |
Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 2674 | if (mLayersPendingRemoval.indexOf(layer) >= 0) { |
Mathias Agopian | 3559b07 | 2012-08-15 13:46:03 -0700 | [diff] [blame] | 2675 | // this layer is not visible anymore |
| 2676 | // TODO: we could traverse the tree from front to back and |
| 2677 | // compute the actual visible region |
| 2678 | // TODO: we could cache the transformed region |
Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 2679 | Region visibleReg; |
| 2680 | visibleReg.set(layer->computeScreenBounds()); |
Chia-I Wu | ab0c319 | 2017-08-01 11:29:00 -0700 | [diff] [blame] | 2681 | invalidateLayerStack(layer, visibleReg); |
Mathias Agopian | 0aa758d | 2009-04-22 15:23:34 -0700 | [diff] [blame] | 2682 | } |
Robert Carr | 2047fae | 2016-11-28 14:09:09 -0800 | [diff] [blame] | 2683 | }); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2684 | } |
| 2685 | |
| 2686 | commitTransaction(); |
Riley Andrews | 03414a1 | 2014-07-01 14:22:59 -0700 | [diff] [blame] | 2687 | |
| 2688 | updateCursorAsync(); |
| 2689 | } |
| 2690 | |
| 2691 | void SurfaceFlinger::updateCursorAsync() |
| 2692 | { |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 2693 | for (size_t displayId = 0; displayId < mDisplays.size(); ++displayId) { |
| 2694 | auto& displayDevice = mDisplays[displayId]; |
| 2695 | if (displayDevice->getHwcDisplayId() < 0) { |
Riley Andrews | 03414a1 | 2014-07-01 14:22:59 -0700 | [diff] [blame] | 2696 | continue; |
| 2697 | } |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 2698 | |
| 2699 | for (auto& layer : displayDevice->getVisibleLayersSortedByZ()) { |
| 2700 | layer->updateCursorPosition(displayDevice); |
Riley Andrews | 03414a1 | 2014-07-01 14:22:59 -0700 | [diff] [blame] | 2701 | } |
| 2702 | } |
Mathias Agopian | 4fec873 | 2012-06-29 14:12:52 -0700 | [diff] [blame] | 2703 | } |
| 2704 | |
| 2705 | void SurfaceFlinger::commitTransaction() |
| 2706 | { |
Steve Pfetsch | 598f6d5 | 2016-10-25 21:47:58 +0000 | [diff] [blame] | 2707 | if (!mLayersPendingRemoval.isEmpty()) { |
Mathias Agopian | 4fec873 | 2012-06-29 14:12:52 -0700 | [diff] [blame] | 2708 | // Notify removed layers now that they can't be drawn from |
Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 2709 | for (const auto& l : mLayersPendingRemoval) { |
| 2710 | recordBufferingStats(l->getName().string(), |
| 2711 | l->getOccupancyHistory(true)); |
| 2712 | l->onRemoved(); |
Mathias Agopian | 4fec873 | 2012-06-29 14:12:52 -0700 | [diff] [blame] | 2713 | } |
| 2714 | mLayersPendingRemoval.clear(); |
| 2715 | } |
| 2716 | |
Jamie Gennis | 4b0eba9 | 2013-02-05 13:30:24 -0800 | [diff] [blame] | 2717 | // If this transaction is part of a window animation then the next frame |
| 2718 | // we composite should be considered an animation as well. |
| 2719 | mAnimCompositionPending = mAnimTransactionPending; |
| 2720 | |
Mathias Agopian | 4fec873 | 2012-06-29 14:12:52 -0700 | [diff] [blame] | 2721 | mDrawingState = mCurrentState; |
Chia-I Wu | 28f320b | 2018-05-03 11:02:56 -0700 | [diff] [blame] | 2722 | // clear the "changed" flags in current state |
| 2723 | mCurrentState.colorMatrixChanged = false; |
| 2724 | |
Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 2725 | mDrawingState.traverseInZOrder([](Layer* layer) { |
| 2726 | layer->commitChildList(); |
| 2727 | }); |
Jamie Gennis | 2d5e230 | 2012-10-15 18:24:43 -0700 | [diff] [blame] | 2728 | mTransactionPending = false; |
| 2729 | mAnimTransactionPending = false; |
Mathias Agopian | 4fec873 | 2012-06-29 14:12:52 -0700 | [diff] [blame] | 2730 | mTransactionCV.broadcast(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2731 | } |
| 2732 | |
Chia-I Wu | ab0c319 | 2017-08-01 11:29:00 -0700 | [diff] [blame] | 2733 | void SurfaceFlinger::computeVisibleRegions(const sp<const DisplayDevice>& displayDevice, |
Mathias Agopian | 87baae1 | 2012-07-31 12:38:26 -0700 | [diff] [blame] | 2734 | Region& outDirtyRegion, Region& outOpaqueRegion) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2735 | { |
Mathias Agopian | 841cde5 | 2012-03-01 15:44:37 -0800 | [diff] [blame] | 2736 | ATRACE_CALL(); |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 2737 | ALOGV("computeVisibleRegions"); |
Mathias Agopian | 841cde5 | 2012-03-01 15:44:37 -0800 | [diff] [blame] | 2738 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2739 | Region aboveOpaqueLayers; |
| 2740 | Region aboveCoveredLayers; |
| 2741 | Region dirty; |
| 2742 | |
Mathias Agopian | 87baae1 | 2012-07-31 12:38:26 -0700 | [diff] [blame] | 2743 | outDirtyRegion.clear(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2744 | |
Robert Carr | 2047fae | 2016-11-28 14:09:09 -0800 | [diff] [blame] | 2745 | mDrawingState.traverseInReverseZOrder([&](Layer* layer) { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2746 | // start with the whole surface at its current location |
Mathias Agopian | 1eae0ee | 2013-06-05 16:59:15 -0700 | [diff] [blame] | 2747 | const Layer::State& s(layer->getDrawingState()); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2748 | |
Jesse Hall | 01e2905 | 2013-02-19 16:13:35 -0800 | [diff] [blame] | 2749 | // only consider the layers on the given layer stack |
Chia-I Wu | ab0c319 | 2017-08-01 11:29:00 -0700 | [diff] [blame] | 2750 | if (!layer->belongsToDisplay(displayDevice->getLayerStack(), displayDevice->isPrimary())) |
Robert Carr | 2047fae | 2016-11-28 14:09:09 -0800 | [diff] [blame] | 2751 | return; |
Mathias Agopian | 87baae1 | 2012-07-31 12:38:26 -0700 | [diff] [blame] | 2752 | |
Mathias Agopian | ab02873 | 2010-03-16 16:41:46 -0700 | [diff] [blame] | 2753 | /* |
| 2754 | * opaqueRegion: area of a surface that is fully opaque. |
| 2755 | */ |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2756 | Region opaqueRegion; |
Mathias Agopian | ab02873 | 2010-03-16 16:41:46 -0700 | [diff] [blame] | 2757 | |
| 2758 | /* |
| 2759 | * visibleRegion: area of a surface that is visible on screen |
| 2760 | * and not fully transparent. This is essentially the layer's |
| 2761 | * footprint minus the opaque regions above it. |
| 2762 | * Areas covered by a translucent surface are considered visible. |
| 2763 | */ |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2764 | Region visibleRegion; |
Mathias Agopian | ab02873 | 2010-03-16 16:41:46 -0700 | [diff] [blame] | 2765 | |
| 2766 | /* |
| 2767 | * coveredRegion: area of a surface that is covered by all |
| 2768 | * visible regions above it (which includes the translucent areas). |
| 2769 | */ |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2770 | Region coveredRegion; |
Mathias Agopian | ab02873 | 2010-03-16 16:41:46 -0700 | [diff] [blame] | 2771 | |
Jesse Hall | a8026d2 | 2012-09-25 13:25:04 -0700 | [diff] [blame] | 2772 | /* |
| 2773 | * transparentRegion: area of a surface that is hinted to be completely |
| 2774 | * transparent. This is only used to tell when the layer has no visible |
| 2775 | * non-transparent regions and can be removed from the layer list. It |
| 2776 | * does not affect the visibleRegion of this layer or any layers |
| 2777 | * beneath it. The hint may not be correct if apps don't respect the |
| 2778 | * SurfaceView restrictions (which, sadly, some don't). |
| 2779 | */ |
| 2780 | Region transparentRegion; |
| 2781 | |
Mathias Agopian | ab02873 | 2010-03-16 16:41:46 -0700 | [diff] [blame] | 2782 | |
| 2783 | // handle hidden surfaces by setting the visible region to empty |
Mathias Agopian | da27af9 | 2012-09-13 18:17:13 -0700 | [diff] [blame] | 2784 | if (CC_LIKELY(layer->isVisible())) { |
Andy McFadden | 4125a4f | 2014-01-29 17:17:11 -0800 | [diff] [blame] | 2785 | const bool translucent = !layer->isOpaque(s); |
Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 2786 | Rect bounds(layer->computeScreenBounds()); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2787 | visibleRegion.set(bounds); |
Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 2788 | Transform tr = layer->getTransform(); |
Mathias Agopian | ab02873 | 2010-03-16 16:41:46 -0700 | [diff] [blame] | 2789 | if (!visibleRegion.isEmpty()) { |
| 2790 | // Remove the transparent area from the visible region |
| 2791 | if (translucent) { |
Dan Stoza | 22f7fc4 | 2016-05-10 16:19:53 -0700 | [diff] [blame] | 2792 | if (tr.preserveRects()) { |
| 2793 | // transform the transparent region |
| 2794 | transparentRegion = tr.transform(s.activeTransparentRegion); |
Mathias Agopian | 4fec873 | 2012-06-29 14:12:52 -0700 | [diff] [blame] | 2795 | } else { |
Dan Stoza | 22f7fc4 | 2016-05-10 16:19:53 -0700 | [diff] [blame] | 2796 | // transformation too complex, can't do the |
| 2797 | // transparent region optimization. |
| 2798 | transparentRegion.clear(); |
Mathias Agopian | 4fec873 | 2012-06-29 14:12:52 -0700 | [diff] [blame] | 2799 | } |
Mathias Agopian | ab02873 | 2010-03-16 16:41:46 -0700 | [diff] [blame] | 2800 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2801 | |
Mathias Agopian | ab02873 | 2010-03-16 16:41:46 -0700 | [diff] [blame] | 2802 | // compute the opaque region |
Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 2803 | const int32_t layerOrientation = tr.getOrientation(); |
Jorim Jaggi | 039bbb8 | 2017-09-06 18:12:05 +0200 | [diff] [blame] | 2804 | if (layer->getAlpha() == 1.0f && !translucent && |
Mathias Agopian | ab02873 | 2010-03-16 16:41:46 -0700 | [diff] [blame] | 2805 | ((layerOrientation & Transform::ROT_INVALID) == false)) { |
| 2806 | // the opaque region is the layer's footprint |
| 2807 | opaqueRegion = visibleRegion; |
| 2808 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2809 | } |
| 2810 | } |
| 2811 | |
Robert Carr | e5f4f69 | 2018-01-12 13:12:28 -0800 | [diff] [blame] | 2812 | if (visibleRegion.isEmpty()) { |
| 2813 | layer->clearVisibilityRegions(); |
| 2814 | return; |
| 2815 | } |
| 2816 | |
Mathias Agopian | ab02873 | 2010-03-16 16:41:46 -0700 | [diff] [blame] | 2817 | // Clip the covered region to the visible region |
| 2818 | coveredRegion = aboveCoveredLayers.intersect(visibleRegion); |
| 2819 | |
| 2820 | // Update aboveCoveredLayers for next (lower) layer |
| 2821 | aboveCoveredLayers.orSelf(visibleRegion); |
| 2822 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2823 | // subtract the opaque region covered by the layers above us |
| 2824 | visibleRegion.subtractSelf(aboveOpaqueLayers); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2825 | |
| 2826 | // compute this layer's dirty region |
| 2827 | if (layer->contentDirty) { |
| 2828 | // we need to invalidate the whole region |
| 2829 | dirty = visibleRegion; |
| 2830 | // as well, as the old visible region |
Mathias Agopian | 4fec873 | 2012-06-29 14:12:52 -0700 | [diff] [blame] | 2831 | dirty.orSelf(layer->visibleRegion); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2832 | layer->contentDirty = false; |
| 2833 | } else { |
Mathias Agopian | a8d44f7 | 2009-06-28 02:54:16 -0700 | [diff] [blame] | 2834 | /* compute the exposed region: |
Mathias Agopian | ab02873 | 2010-03-16 16:41:46 -0700 | [diff] [blame] | 2835 | * the exposed region consists of two components: |
| 2836 | * 1) what's VISIBLE now and was COVERED before |
| 2837 | * 2) what's EXPOSED now less what was EXPOSED before |
| 2838 | * |
| 2839 | * note that (1) is conservative, we start with the whole |
| 2840 | * visible region but only keep what used to be covered by |
| 2841 | * something -- which mean it may have been exposed. |
| 2842 | * |
| 2843 | * (2) handles areas that were not covered by anything but got |
| 2844 | * exposed because of a resize. |
Mathias Agopian | a8d44f7 | 2009-06-28 02:54:16 -0700 | [diff] [blame] | 2845 | */ |
Mathias Agopian | ab02873 | 2010-03-16 16:41:46 -0700 | [diff] [blame] | 2846 | const Region newExposed = visibleRegion - coveredRegion; |
Mathias Agopian | 4fec873 | 2012-06-29 14:12:52 -0700 | [diff] [blame] | 2847 | const Region oldVisibleRegion = layer->visibleRegion; |
| 2848 | const Region oldCoveredRegion = layer->coveredRegion; |
Mathias Agopian | ab02873 | 2010-03-16 16:41:46 -0700 | [diff] [blame] | 2849 | const Region oldExposed = oldVisibleRegion - oldCoveredRegion; |
| 2850 | dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2851 | } |
| 2852 | dirty.subtractSelf(aboveOpaqueLayers); |
| 2853 | |
| 2854 | // accumulate to the screen dirty region |
Mathias Agopian | 87baae1 | 2012-07-31 12:38:26 -0700 | [diff] [blame] | 2855 | outDirtyRegion.orSelf(dirty); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2856 | |
Mathias Agopian | ab02873 | 2010-03-16 16:41:46 -0700 | [diff] [blame] | 2857 | // Update aboveOpaqueLayers for next (lower) layer |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2858 | aboveOpaqueLayers.orSelf(opaqueRegion); |
Andreas Huber | 8b42e8a | 2010-08-16 08:49:37 -0700 | [diff] [blame] | 2859 | |
Jesse Hall | a8026d2 | 2012-09-25 13:25:04 -0700 | [diff] [blame] | 2860 | // Store the visible region in screen space |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2861 | layer->setVisibleRegion(visibleRegion); |
| 2862 | layer->setCoveredRegion(coveredRegion); |
Jesse Hall | a8026d2 | 2012-09-25 13:25:04 -0700 | [diff] [blame] | 2863 | layer->setVisibleNonTransparentRegion( |
| 2864 | visibleRegion.subtract(transparentRegion)); |
Robert Carr | 2047fae | 2016-11-28 14:09:09 -0800 | [diff] [blame] | 2865 | }); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2866 | |
Mathias Agopian | 87baae1 | 2012-07-31 12:38:26 -0700 | [diff] [blame] | 2867 | outOpaqueRegion = aboveOpaqueLayers; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2868 | } |
| 2869 | |
Chia-I Wu | ab0c319 | 2017-08-01 11:29:00 -0700 | [diff] [blame] | 2870 | void SurfaceFlinger::invalidateLayerStack(const sp<const Layer>& layer, const Region& dirty) { |
Mathias Agopian | 92a979a | 2012-08-02 18:32:23 -0700 | [diff] [blame] | 2871 | for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) { |
Mathias Agopian | 4297734 | 2012-08-05 00:40:46 -0700 | [diff] [blame] | 2872 | const sp<DisplayDevice>& hw(mDisplays[dpy]); |
Chia-I Wu | ab0c319 | 2017-08-01 11:29:00 -0700 | [diff] [blame] | 2873 | if (layer->belongsToDisplay(hw->getLayerStack(), hw->isPrimary())) { |
Mathias Agopian | 4297734 | 2012-08-05 00:40:46 -0700 | [diff] [blame] | 2874 | hw->dirtyRegion.orSelf(dirty); |
Mathias Agopian | 92a979a | 2012-08-02 18:32:23 -0700 | [diff] [blame] | 2875 | } |
| 2876 | } |
Mathias Agopian | 87baae1 | 2012-07-31 12:38:26 -0700 | [diff] [blame] | 2877 | } |
| 2878 | |
Dan Stoza | 6b9454d | 2014-11-07 16:00:59 -0800 | [diff] [blame] | 2879 | bool SurfaceFlinger::handlePageFlip() |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2880 | { |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 2881 | ALOGV("handlePageFlip"); |
| 2882 | |
Brian Anderson | d6927fb | 2016-07-23 23:37:30 -0700 | [diff] [blame] | 2883 | nsecs_t latchTime = systemTime(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2884 | |
Mathias Agopian | 4fec873 | 2012-06-29 14:12:52 -0700 | [diff] [blame] | 2885 | bool visibleRegions = false; |
Dan Stoza | 6b9454d | 2014-11-07 16:00:59 -0800 | [diff] [blame] | 2886 | bool frameQueued = false; |
Mike Stroyan | 0cd7619 | 2017-04-20 12:10:48 -0600 | [diff] [blame] | 2887 | bool newDataLatched = false; |
Eric Penner | 51c59cd | 2014-07-28 19:51:58 -0700 | [diff] [blame] | 2888 | |
| 2889 | // Store the set of layers that need updates. This set must not change as |
| 2890 | // buffers are being latched, as this could result in a deadlock. |
| 2891 | // Example: Two producers share the same command stream and: |
| 2892 | // 1.) Layer 0 is latched |
| 2893 | // 2.) Layer 0 gets a new frame |
| 2894 | // 2.) Layer 1 gets a new frame |
| 2895 | // 3.) Layer 1 is latched. |
| 2896 | // Display is now waiting on Layer 1's frame, which is behind layer 0's |
| 2897 | // second frame. But layer 0's second frame could be waiting on display. |
Robert Carr | 2047fae | 2016-11-28 14:09:09 -0800 | [diff] [blame] | 2898 | mDrawingState.traverseInZOrder([&](Layer* layer) { |
Dan Stoza | 6b9454d | 2014-11-07 16:00:59 -0800 | [diff] [blame] | 2899 | if (layer->hasQueuedFrame()) { |
| 2900 | frameQueued = true; |
| 2901 | if (layer->shouldPresentNow(mPrimaryDispSync)) { |
Robert Carr | 2047fae | 2016-11-28 14:09:09 -0800 | [diff] [blame] | 2902 | mLayersWithQueuedFrames.push_back(layer); |
Dan Stoza | ee44edd | 2015-03-23 15:50:23 -0700 | [diff] [blame] | 2903 | } else { |
| 2904 | layer->useEmptyDamage(); |
Dan Stoza | 6b9454d | 2014-11-07 16:00:59 -0800 | [diff] [blame] | 2905 | } |
Dan Stoza | ee44edd | 2015-03-23 15:50:23 -0700 | [diff] [blame] | 2906 | } else { |
| 2907 | layer->useEmptyDamage(); |
Dan Stoza | 6b9454d | 2014-11-07 16:00:59 -0800 | [diff] [blame] | 2908 | } |
Robert Carr | 2047fae | 2016-11-28 14:09:09 -0800 | [diff] [blame] | 2909 | }); |
| 2910 | |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 2911 | for (auto& layer : mLayersWithQueuedFrames) { |
Brian Anderson | d6927fb | 2016-07-23 23:37:30 -0700 | [diff] [blame] | 2912 | const Region dirty(layer->latchBuffer(visibleRegions, latchTime)); |
Dan Stoza | ee44edd | 2015-03-23 15:50:23 -0700 | [diff] [blame] | 2913 | layer->useSurfaceDamage(); |
Chia-I Wu | ab0c319 | 2017-08-01 11:29:00 -0700 | [diff] [blame] | 2914 | invalidateLayerStack(layer, dirty); |
Chia-I Wu | a36bf92 | 2017-06-30 12:51:05 -0700 | [diff] [blame] | 2915 | if (layer->isBufferLatched()) { |
Mike Stroyan | 0cd7619 | 2017-04-20 12:10:48 -0600 | [diff] [blame] | 2916 | newDataLatched = true; |
| 2917 | } |
Mathias Agopian | 4fec873 | 2012-06-29 14:12:52 -0700 | [diff] [blame] | 2918 | } |
Mathias Agopian | 4da7519 | 2010-08-10 17:19:56 -0700 | [diff] [blame] | 2919 | |
Mathias Agopian | 3b1d2b6 | 2012-07-11 13:48:17 -0700 | [diff] [blame] | 2920 | mVisibleRegionsDirty |= visibleRegions; |
Dan Stoza | 6b9454d | 2014-11-07 16:00:59 -0800 | [diff] [blame] | 2921 | |
| 2922 | // If we will need to wake up at some time in the future to deal with a |
| 2923 | // queued frame that shouldn't be displayed during this vsync period, wake |
| 2924 | // up during the next vsync period to check again. |
Chia-I Wu | a36bf92 | 2017-06-30 12:51:05 -0700 | [diff] [blame] | 2925 | if (frameQueued && (mLayersWithQueuedFrames.empty() || !newDataLatched)) { |
Dan Stoza | 6b9454d | 2014-11-07 16:00:59 -0800 | [diff] [blame] | 2926 | signalLayerUpdate(); |
| 2927 | } |
| 2928 | |
| 2929 | // Only continue with the refresh if there is actually new work to do |
Mike Stroyan | 0cd7619 | 2017-04-20 12:10:48 -0600 | [diff] [blame] | 2930 | return !mLayersWithQueuedFrames.empty() && newDataLatched; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2931 | } |
| 2932 | |
Mathias Agopian | ad456f9 | 2011-01-13 17:53:01 -0800 | [diff] [blame] | 2933 | void SurfaceFlinger::invalidateHwcGeometry() |
| 2934 | { |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 2935 | mGeometryInvalid = true; |
Mathias Agopian | ad456f9 | 2011-01-13 17:53:01 -0800 | [diff] [blame] | 2936 | } |
| 2937 | |
Mathias Agopian | 99ce5cd | 2012-01-31 18:24:27 -0800 | [diff] [blame] | 2938 | |
Fabien Sanglard | 830b847 | 2016-11-30 16:35:58 -0800 | [diff] [blame] | 2939 | void SurfaceFlinger::doDisplayComposition( |
| 2940 | const sp<const DisplayDevice>& displayDevice, |
Mathias Agopian | 87baae1 | 2012-07-31 12:38:26 -0700 | [diff] [blame] | 2941 | const Region& inDirtyRegion) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2942 | { |
Dan Stoza | 7143316 | 2014-02-04 16:22:36 -0800 | [diff] [blame] | 2943 | // We only need to actually compose the display if: |
| 2944 | // 1) It is being handled by hardware composer, which may need this to |
| 2945 | // keep its virtual display state machine in sync, or |
| 2946 | // 2) There is work to be done (the dirty region isn't empty) |
Fabien Sanglard | 830b847 | 2016-11-30 16:35:58 -0800 | [diff] [blame] | 2947 | bool isHwcDisplay = displayDevice->getHwcDisplayId() >= 0; |
Dan Stoza | 7143316 | 2014-02-04 16:22:36 -0800 | [diff] [blame] | 2948 | if (!isHwcDisplay && inDirtyRegion.isEmpty()) { |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 2949 | ALOGV("Skipping display composition"); |
Dan Stoza | 7143316 | 2014-02-04 16:22:36 -0800 | [diff] [blame] | 2950 | return; |
| 2951 | } |
| 2952 | |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 2953 | ALOGV("doDisplayComposition"); |
Chia-I Wu | b02087d | 2017-11-09 10:19:54 -0800 | [diff] [blame] | 2954 | if (!doComposeSurfaces(displayDevice)) return; |
Mathias Agopian | da27af9 | 2012-09-13 18:17:13 -0700 | [diff] [blame] | 2955 | |
| 2956 | // swap buffers (presentation) |
Fabien Sanglard | 830b847 | 2016-11-30 16:35:58 -0800 | [diff] [blame] | 2957 | displayDevice->swapBuffers(getHwComposer()); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2958 | } |
| 2959 | |
Chia-I Wu | b02087d | 2017-11-09 10:19:54 -0800 | [diff] [blame] | 2960 | bool SurfaceFlinger::doComposeSurfaces(const sp<const DisplayDevice>& displayDevice) |
Mathias Agopian | f384cc3 | 2011-09-08 18:31:55 -0700 | [diff] [blame] | 2961 | { |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 2962 | ALOGV("doComposeSurfaces"); |
Mathias Agopian | cd20eb0 | 2011-09-22 20:57:04 -0700 | [diff] [blame] | 2963 | |
Chia-I Wu | b02087d | 2017-11-09 10:19:54 -0800 | [diff] [blame] | 2964 | const Region bounds(displayDevice->bounds()); |
chaviw | a76b271 | 2017-09-20 12:02:26 -0700 | [diff] [blame] | 2965 | const DisplayRenderArea renderArea(displayDevice); |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 2966 | const auto hwcId = displayDevice->getHwcDisplayId(); |
Peiyong Lin | dd9b2ae | 2018-03-01 16:22:45 -0800 | [diff] [blame] | 2967 | const bool hasClientComposition = getBE().mHwc->hasClientComposition(hwcId); |
| 2968 | const bool hasDeviceComposition = getBE().mHwc->hasDeviceComposition(hwcId); |
| 2969 | const bool skipClientColorTransform = getBE().mHwc->hasCapability( |
| 2970 | HWC2::Capability::SkipClientColorTransform); |
| 2971 | ATRACE_INT("hasClientComposition", hasClientComposition); |
Dan Stoza | 9f26a9c | 2016-06-22 14:51:09 -0700 | [diff] [blame] | 2972 | |
| 2973 | mat4 oldColorMatrix; |
Peiyong Lin | dd9b2ae | 2018-03-01 16:22:45 -0800 | [diff] [blame] | 2974 | mat4 legacySrgbSaturationMatrix = mLegacySrgbSaturationMatrix; |
| 2975 | const bool applyColorMatrix = !hasDeviceComposition && !skipClientColorTransform; |
Dan Stoza | 9f26a9c | 2016-06-22 14:51:09 -0700 | [diff] [blame] | 2976 | if (applyColorMatrix) { |
Chia-I Wu | 28f320b | 2018-05-03 11:02:56 -0700 | [diff] [blame] | 2977 | oldColorMatrix = getRenderEngine().setupColorTransform(mDrawingState.colorMatrix); |
| 2978 | legacySrgbSaturationMatrix = mDrawingState.colorMatrix * legacySrgbSaturationMatrix; |
Dan Stoza | 9f26a9c | 2016-06-22 14:51:09 -0700 | [diff] [blame] | 2979 | } |
| 2980 | |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 2981 | if (hasClientComposition) { |
| 2982 | ALOGV("hasClientComposition"); |
| 2983 | |
Peiyong Lin | 34beb7a | 2018-03-28 11:57:12 -0700 | [diff] [blame] | 2984 | Dataspace outputDataspace = Dataspace::UNKNOWN; |
Peiyong Lin | dd9b2ae | 2018-03-01 16:22:45 -0800 | [diff] [blame] | 2985 | if (displayDevice->hasWideColorGamut()) { |
| 2986 | outputDataspace = displayDevice->getCompositionDataSpace(); |
Chia-I Wu | 69bf10f | 2018-02-20 13:04:50 -0800 | [diff] [blame] | 2987 | } |
| 2988 | getBE().mRenderEngine->setOutputDataSpace(outputDataspace); |
Peiyong Lin | fb06930 | 2018-04-25 14:34:31 -0700 | [diff] [blame] | 2989 | getBE().mRenderEngine->setDisplayMaxLuminance( |
| 2990 | displayDevice->getHdrCapabilities().getDesiredMaxLuminance()); |
Chia-I Wu | 69bf10f | 2018-02-20 13:04:50 -0800 | [diff] [blame] | 2991 | |
Chia-I Wu | 7f40290 | 2017-11-09 12:51:10 -0800 | [diff] [blame] | 2992 | if (!displayDevice->makeCurrent()) { |
Michael Chock | c8c7109 | 2013-03-04 15:15:46 -0800 | [diff] [blame] | 2993 | ALOGW("DisplayDevice::makeCurrent failed. Aborting surface composition for display %s", |
Dominik Laskowski | bf170d9 | 2018-04-19 15:08:05 -0700 | [diff] [blame] | 2994 | displayDevice->getDisplayName().c_str()); |
Chia-I Wu | 7f40290 | 2017-11-09 12:51:10 -0800 | [diff] [blame] | 2995 | getRenderEngine().resetCurrentSurface(); |
Stephen Kiazyk | 7d3dcb9 | 2017-04-05 16:46:49 -0700 | [diff] [blame] | 2996 | |
| 2997 | // |mStateLock| not needed as we are on the main thread |
Chia-I Wu | 7f40290 | 2017-11-09 12:51:10 -0800 | [diff] [blame] | 2998 | if(!getDefaultDisplayDeviceLocked()->makeCurrent()) { |
Michael Lentine | 3f121fc | 2014-10-01 11:17:28 -0700 | [diff] [blame] | 2999 | ALOGE("DisplayDevice::makeCurrent on default display failed. Aborting."); |
| 3000 | } |
| 3001 | return false; |
Michael Chock | c8c7109 | 2013-03-04 15:15:46 -0800 | [diff] [blame] | 3002 | } |
Mathias Agopian | 52bbb1a | 2012-07-31 19:01:53 -0700 | [diff] [blame] | 3003 | |
Mathias Agopian | 52bbb1a | 2012-07-31 19:01:53 -0700 | [diff] [blame] | 3004 | // Never touch the framebuffer if we don't have any framebuffer layers |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 3005 | if (hasDeviceComposition) { |
Mathias Agopian | b9494d5 | 2012-04-18 02:28:45 -0700 | [diff] [blame] | 3006 | // when using overlays, we assume a fully transparent framebuffer |
| 3007 | // NOTE: we could reduce how much we need to clear, for instance |
| 3008 | // remove where there are opaque FB layers. however, on some |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 3009 | // GPUs doing a "clean slate" clear might be more efficient. |
Mathias Agopian | b9494d5 | 2012-04-18 02:28:45 -0700 | [diff] [blame] | 3010 | // We'll revisit later if needed. |
David Sodman | bc81528 | 2017-11-05 18:57:52 -0800 | [diff] [blame] | 3011 | getBE().mRenderEngine->clearWithColor(0, 0, 0, 0); |
Mathias Agopian | b9494d5 | 2012-04-18 02:28:45 -0700 | [diff] [blame] | 3012 | } else { |
Chia-I Wu | b02087d | 2017-11-09 10:19:54 -0800 | [diff] [blame] | 3013 | // we start with the whole screen area and remove the scissor part |
Mathias Agopian | 766dc49 | 2012-10-30 18:08:06 -0700 | [diff] [blame] | 3014 | // we're left with the letterbox region |
| 3015 | // (common case is that letterbox ends-up being empty) |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 3016 | const Region letterbox(bounds.subtract(displayDevice->getScissor())); |
Mathias Agopian | 766dc49 | 2012-10-30 18:08:06 -0700 | [diff] [blame] | 3017 | |
| 3018 | // compute the area to clear |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 3019 | Region region(displayDevice->undefinedRegion.merge(letterbox)); |
Mathias Agopian | 766dc49 | 2012-10-30 18:08:06 -0700 | [diff] [blame] | 3020 | |
Mathias Agopian | b9494d5 | 2012-04-18 02:28:45 -0700 | [diff] [blame] | 3021 | // screen is already cleared here |
Mathias Agopian | 87baae1 | 2012-07-31 12:38:26 -0700 | [diff] [blame] | 3022 | if (!region.isEmpty()) { |
Mathias Agopian | b9494d5 | 2012-04-18 02:28:45 -0700 | [diff] [blame] | 3023 | // can happen with SurfaceView |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 3024 | drawWormhole(displayDevice, region); |
Mathias Agopian | b9494d5 | 2012-04-18 02:28:45 -0700 | [diff] [blame] | 3025 | } |
Mathias Agopian | a2f4e56 | 2012-04-15 23:34:59 -0700 | [diff] [blame] | 3026 | } |
Mathias Agopian | f45c510 | 2012-10-24 16:29:17 -0700 | [diff] [blame] | 3027 | |
Dominik Laskowski | 281644e | 2018-04-19 15:47:35 -0700 | [diff] [blame] | 3028 | if (!displayDevice->isPrimary()) { |
Mathias Agopian | 766dc49 | 2012-10-30 18:08:06 -0700 | [diff] [blame] | 3029 | // just to be on the safe side, we don't set the |
Mathias Agopian | f45c510 | 2012-10-24 16:29:17 -0700 | [diff] [blame] | 3030 | // scissor on the main display. It should never be needed |
| 3031 | // anyways (though in theory it could since the API allows it). |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 3032 | const Rect& bounds(displayDevice->getBounds()); |
| 3033 | const Rect& scissor(displayDevice->getScissor()); |
Mathias Agopian | f45c510 | 2012-10-24 16:29:17 -0700 | [diff] [blame] | 3034 | if (scissor != bounds) { |
| 3035 | // scissor doesn't match the screen's dimensions, so we |
| 3036 | // need to clear everything outside of it and enable |
| 3037 | // the GL scissor so we don't draw anything where we shouldn't |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 3038 | |
Mathias Agopian | f45c510 | 2012-10-24 16:29:17 -0700 | [diff] [blame] | 3039 | // enable scissor for this frame |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 3040 | const uint32_t height = displayDevice->getHeight(); |
David Sodman | bc81528 | 2017-11-05 18:57:52 -0800 | [diff] [blame] | 3041 | getBE().mRenderEngine->setScissor(scissor.left, height - scissor.bottom, |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 3042 | scissor.getWidth(), scissor.getHeight()); |
Mathias Agopian | f45c510 | 2012-10-24 16:29:17 -0700 | [diff] [blame] | 3043 | } |
| 3044 | } |
Mathias Agopian | 85d751c | 2012-08-29 16:59:24 -0700 | [diff] [blame] | 3045 | } |
Mathias Agopian | 4b2ba53 | 2012-03-29 12:23:51 -0700 | [diff] [blame] | 3046 | |
Mathias Agopian | 85d751c | 2012-08-29 16:59:24 -0700 | [diff] [blame] | 3047 | /* |
| 3048 | * and then, render the layers targeted at the framebuffer |
| 3049 | */ |
Mathias Agopian | 4b2ba53 | 2012-03-29 12:23:51 -0700 | [diff] [blame] | 3050 | |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 3051 | ALOGV("Rendering client layers"); |
| 3052 | const Transform& displayTransform = displayDevice->getTransform(); |
| 3053 | if (hwcId >= 0) { |
Mathias Agopian | 85d751c | 2012-08-29 16:59:24 -0700 | [diff] [blame] | 3054 | // we're using h/w composer |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 3055 | bool firstLayer = true; |
| 3056 | for (auto& layer : displayDevice->getVisibleLayersSortedByZ()) { |
Chia-I Wu | b02087d | 2017-11-09 10:19:54 -0800 | [diff] [blame] | 3057 | const Region clip(bounds.intersect( |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 3058 | displayTransform.transform(layer->visibleRegion))); |
| 3059 | ALOGV("Layer: %s", layer->getName().string()); |
| 3060 | ALOGV(" Composition type: %s", |
| 3061 | to_string(layer->getCompositionType(hwcId)).c_str()); |
Mathias Agopian | 85d751c | 2012-08-29 16:59:24 -0700 | [diff] [blame] | 3062 | if (!clip.isEmpty()) { |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 3063 | switch (layer->getCompositionType(hwcId)) { |
| 3064 | case HWC2::Composition::Cursor: |
| 3065 | case HWC2::Composition::Device: |
Gray Huang | 267ab79 | 2017-01-11 13:41:09 +0800 | [diff] [blame] | 3066 | case HWC2::Composition::Sideband: |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 3067 | case HWC2::Composition::SolidColor: { |
Mathias Agopian | ac68302 | 2013-10-01 15:36:52 -0700 | [diff] [blame] | 3068 | const Layer::State& state(layer->getDrawingState()); |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 3069 | if (layer->getClearClientTarget(hwcId) && !firstLayer && |
chaviw | 13fdc49 | 2017-06-27 12:40:18 -0700 | [diff] [blame] | 3070 | layer->isOpaque(state) && (state.color.a == 1.0f) |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 3071 | && hasClientComposition) { |
Mathias Agopian | cd60f99 | 2012-08-16 16:28:27 -0700 | [diff] [blame] | 3072 | // never clear the very first layer since we're |
| 3073 | // guaranteed the FB is already cleared |
chaviw | a76b271 | 2017-09-20 12:02:26 -0700 | [diff] [blame] | 3074 | layer->clearWithOpenGL(renderArea); |
Mathias Agopian | cd60f99 | 2012-08-16 16:28:27 -0700 | [diff] [blame] | 3075 | } |
Mathias Agopian | 85d751c | 2012-08-29 16:59:24 -0700 | [diff] [blame] | 3076 | break; |
Mathias Agopian | cd60f99 | 2012-08-16 16:28:27 -0700 | [diff] [blame] | 3077 | } |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 3078 | case HWC2::Composition::Client: { |
Peiyong Lin | dd9b2ae | 2018-03-01 16:22:45 -0800 | [diff] [blame] | 3079 | // Only apply saturation matrix layer that is legacy SRGB dataspace |
| 3080 | // when auto color mode is on. |
| 3081 | bool restore = false; |
| 3082 | mat4 savedMatrix; |
| 3083 | if (mDisplayColorSetting == DisplayColorSetting::ENHANCED && |
| 3084 | layer->isLegacySrgbDataSpace()) { |
Peiyong Lin | 136fbbc | 2018-04-17 15:09:44 -0700 | [diff] [blame^] | 3085 | // TODO(b/78891890) Legacy sRGB saturation matrix should be set |
| 3086 | // separately. |
Peiyong Lin | dd9b2ae | 2018-03-01 16:22:45 -0800 | [diff] [blame] | 3087 | savedMatrix = |
| 3088 | getRenderEngine().setupColorTransform(legacySrgbSaturationMatrix); |
| 3089 | restore = true; |
| 3090 | } |
chaviw | a76b271 | 2017-09-20 12:02:26 -0700 | [diff] [blame] | 3091 | layer->draw(renderArea, clip); |
Peiyong Lin | dd9b2ae | 2018-03-01 16:22:45 -0800 | [diff] [blame] | 3092 | if (restore) { |
| 3093 | getRenderEngine().setupColorTransform(savedMatrix); |
| 3094 | } |
Mathias Agopian | 85d751c | 2012-08-29 16:59:24 -0700 | [diff] [blame] | 3095 | break; |
| 3096 | } |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 3097 | default: |
Mathias Agopian | da27af9 | 2012-09-13 18:17:13 -0700 | [diff] [blame] | 3098 | break; |
Mathias Agopian | cd60f99 | 2012-08-16 16:28:27 -0700 | [diff] [blame] | 3099 | } |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 3100 | } else { |
| 3101 | ALOGV(" Skipping for empty clip"); |
Mathias Agopian | 85d751c | 2012-08-29 16:59:24 -0700 | [diff] [blame] | 3102 | } |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 3103 | firstLayer = false; |
Mathias Agopian | 85d751c | 2012-08-29 16:59:24 -0700 | [diff] [blame] | 3104 | } |
| 3105 | } else { |
| 3106 | // we're not using h/w composer |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 3107 | for (auto& layer : displayDevice->getVisibleLayersSortedByZ()) { |
Chia-I Wu | b02087d | 2017-11-09 10:19:54 -0800 | [diff] [blame] | 3108 | const Region clip(bounds.intersect( |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 3109 | displayTransform.transform(layer->visibleRegion))); |
Mathias Agopian | 85d751c | 2012-08-29 16:59:24 -0700 | [diff] [blame] | 3110 | if (!clip.isEmpty()) { |
chaviw | a76b271 | 2017-09-20 12:02:26 -0700 | [diff] [blame] | 3111 | layer->draw(renderArea, clip); |
Jesse Hall | a6b32db | 2012-07-19 16:44:38 -0700 | [diff] [blame] | 3112 | } |
Mathias Agopian | 4b2ba53 | 2012-03-29 12:23:51 -0700 | [diff] [blame] | 3113 | } |
| 3114 | } |
Mathias Agopian | f45c510 | 2012-10-24 16:29:17 -0700 | [diff] [blame] | 3115 | |
Dan Stoza | 9f26a9c | 2016-06-22 14:51:09 -0700 | [diff] [blame] | 3116 | if (applyColorMatrix) { |
| 3117 | getRenderEngine().setupColorTransform(oldColorMatrix); |
| 3118 | } |
| 3119 | |
Mathias Agopian | f45c510 | 2012-10-24 16:29:17 -0700 | [diff] [blame] | 3120 | // disable scissor at the end of the frame |
David Sodman | bc81528 | 2017-11-05 18:57:52 -0800 | [diff] [blame] | 3121 | getBE().mRenderEngine->disableScissor(); |
Michael Lentine | 3f121fc | 2014-10-01 11:17:28 -0700 | [diff] [blame] | 3122 | return true; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3123 | } |
| 3124 | |
Fabien Sanglard | 830b847 | 2016-11-30 16:35:58 -0800 | [diff] [blame] | 3125 | void SurfaceFlinger::drawWormhole(const sp<const DisplayDevice>& displayDevice, const Region& region) const { |
| 3126 | const int32_t height = displayDevice->getHeight(); |
Lloyd Pique | 144e116 | 2017-12-20 16:44:52 -0800 | [diff] [blame] | 3127 | auto& engine(getRenderEngine()); |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 3128 | engine.fillRegionWithColor(region, height, 0, 0, 0, 0); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3129 | } |
| 3130 | |
Dan Stoza | 7d89d06 | 2015-04-30 13:29:25 -0700 | [diff] [blame] | 3131 | status_t SurfaceFlinger::addClientLayer(const sp<Client>& client, |
Mathias Agopian | ac9fa42 | 2013-02-11 16:40:36 -0800 | [diff] [blame] | 3132 | const sp<IBinder>& handle, |
Mathias Agopian | 6710604 | 2013-03-14 19:18:13 -0700 | [diff] [blame] | 3133 | const sp<IGraphicBufferProducer>& gbc, |
Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 3134 | const sp<Layer>& lbc, |
| 3135 | const sp<Layer>& parent) |
Mathias Agopian | 96f0819 | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 3136 | { |
Dan Stoza | 7d89d06 | 2015-04-30 13:29:25 -0700 | [diff] [blame] | 3137 | // add this layer to the current state list |
| 3138 | { |
| 3139 | Mutex::Autolock _l(mStateLock); |
Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 3140 | if (mNumLayers >= MAX_LAYERS) { |
Courtney Goeltzenleuchter | ab702f5 | 2017-04-19 15:14:02 -0600 | [diff] [blame] | 3141 | ALOGE("AddClientLayer failed, mNumLayers (%zu) >= MAX_LAYERS (%zu)", mNumLayers, |
| 3142 | MAX_LAYERS); |
Dan Stoza | 7d89d06 | 2015-04-30 13:29:25 -0700 | [diff] [blame] | 3143 | return NO_MEMORY; |
| 3144 | } |
Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 3145 | if (parent == nullptr) { |
| 3146 | mCurrentState.layersSortedByZ.add(lbc); |
| 3147 | } else { |
Robert Carr | ebd62af | 2017-11-28 08:49:59 -0800 | [diff] [blame] | 3148 | if (parent->isPendingRemoval()) { |
Chia-I Wu | 98f1c10 | 2017-05-30 14:54:08 -0700 | [diff] [blame] | 3149 | ALOGE("addClientLayer called with a removed parent"); |
| 3150 | return NAME_NOT_FOUND; |
| 3151 | } |
Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 3152 | parent->addChild(lbc); |
| 3153 | } |
Chia-I Wu | 98f1c10 | 2017-05-30 14:54:08 -0700 | [diff] [blame] | 3154 | |
Dan Stoza | 101d8dc | 2018-02-27 15:42:25 -0800 | [diff] [blame] | 3155 | mGraphicBufferProducerList.insert(IInterface::asBinder(gbc).get()); |
Dan Stoza | 0a0158c | 2018-03-16 13:38:54 -0700 | [diff] [blame] | 3156 | // TODO (b/74616334): Change this back to a fatal assert once the leak is fixed |
| 3157 | ALOGE_IF(mGraphicBufferProducerList.size() > mMaxGraphicBufferProducerListSize, |
| 3158 | "Suspected IGBP leak: %zu IGBPs (%zu max), %zu Layers", |
| 3159 | mGraphicBufferProducerList.size(), mMaxGraphicBufferProducerListSize, |
| 3160 | mNumLayers); |
Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 3161 | mLayersAdded = true; |
| 3162 | mNumLayers++; |
Dan Stoza | 7d89d06 | 2015-04-30 13:29:25 -0700 | [diff] [blame] | 3163 | } |
| 3164 | |
Mathias Agopian | 96f0819 | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 3165 | // attach this layer to the client |
Mathias Agopian | ac9fa42 | 2013-02-11 16:40:36 -0800 | [diff] [blame] | 3166 | client->attachLayer(handle, lbc); |
Mathias Agopian | 4f11374 | 2011-05-03 16:21:41 -0700 | [diff] [blame] | 3167 | |
Dan Stoza | 7d89d06 | 2015-04-30 13:29:25 -0700 | [diff] [blame] | 3168 | return NO_ERROR; |
Mathias Agopian | 96f0819 | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 3169 | } |
| 3170 | |
Chia-I Wu | 515dc9c | 2017-06-15 12:53:59 -0700 | [diff] [blame] | 3171 | status_t SurfaceFlinger::removeLayer(const sp<Layer>& layer, bool topLevelOnly) { |
Robert Carr | 7f9b899 | 2017-03-10 11:08:39 -0800 | [diff] [blame] | 3172 | Mutex::Autolock _l(mStateLock); |
chaviw | ca27f25 | 2018-02-06 16:46:39 -0800 | [diff] [blame] | 3173 | return removeLayerLocked(mStateLock, layer, topLevelOnly); |
| 3174 | } |
Robert Carr | 7f9b899 | 2017-03-10 11:08:39 -0800 | [diff] [blame] | 3175 | |
chaviw | ca27f25 | 2018-02-06 16:46:39 -0800 | [diff] [blame] | 3176 | status_t SurfaceFlinger::removeLayerLocked(const Mutex&, const sp<Layer>& layer, |
| 3177 | bool topLevelOnly) { |
chaviw | 8b3871a | 2017-11-01 17:41:01 -0700 | [diff] [blame] | 3178 | if (layer->isPendingRemoval()) { |
| 3179 | return NO_ERROR; |
| 3180 | } |
| 3181 | |
Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 3182 | const auto& p = layer->getParent(); |
Chia-I Wu | 515dc9c | 2017-06-15 12:53:59 -0700 | [diff] [blame] | 3183 | ssize_t index; |
Chia-I Wu | 98f1c10 | 2017-05-30 14:54:08 -0700 | [diff] [blame] | 3184 | if (p != nullptr) { |
Chia-I Wu | 515dc9c | 2017-06-15 12:53:59 -0700 | [diff] [blame] | 3185 | if (topLevelOnly) { |
| 3186 | return NO_ERROR; |
| 3187 | } |
| 3188 | |
Chia-I Wu | 98f1c10 | 2017-05-30 14:54:08 -0700 | [diff] [blame] | 3189 | sp<Layer> ancestor = p; |
| 3190 | while (ancestor->getParent() != nullptr) { |
| 3191 | ancestor = ancestor->getParent(); |
| 3192 | } |
| 3193 | if (mCurrentState.layersSortedByZ.indexOf(ancestor) < 0) { |
| 3194 | ALOGE("removeLayer called with a layer whose parent has been removed"); |
| 3195 | return NAME_NOT_FOUND; |
| 3196 | } |
Chia-I Wu | fae51c4 | 2017-06-15 12:53:59 -0700 | [diff] [blame] | 3197 | |
| 3198 | index = p->removeChild(layer); |
Chia-I Wu | 515dc9c | 2017-06-15 12:53:59 -0700 | [diff] [blame] | 3199 | } else { |
| 3200 | index = mCurrentState.layersSortedByZ.remove(layer); |
Chia-I Wu | 98f1c10 | 2017-05-30 14:54:08 -0700 | [diff] [blame] | 3201 | } |
| 3202 | |
Robert Carr | 136e2f6 | 2017-02-08 17:54:29 -0800 | [diff] [blame] | 3203 | // As a matter of normal operation, the LayerCleaner will produce a second |
| 3204 | // attempt to remove the surface. The Layer will be kept alive in mDrawingState |
| 3205 | // so we will succeed in promoting it, but it's already been removed |
| 3206 | // from mCurrentState. As long as we can find it in mDrawingState we have no problem |
| 3207 | // otherwise something has gone wrong and we are leaking the layer. |
| 3208 | if (index < 0 && mDrawingState.layersSortedByZ.indexOf(layer) < 0) { |
Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 3209 | ALOGE("Failed to find layer (%s) in layer parent (%s).", |
| 3210 | layer->getName().string(), |
| 3211 | (p != nullptr) ? p->getName().string() : "no-parent"); |
| 3212 | return BAD_VALUE; |
Robert Carr | 136e2f6 | 2017-02-08 17:54:29 -0800 | [diff] [blame] | 3213 | } else if (index < 0) { |
| 3214 | return NO_ERROR; |
Steve Pfetsch | 598f6d5 | 2016-10-25 21:47:58 +0000 | [diff] [blame] | 3215 | } |
Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 3216 | |
Chia-I Wu | c665702 | 2017-08-15 11:18:17 -0700 | [diff] [blame] | 3217 | layer->onRemovedFromCurrentState(); |
Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 3218 | mLayersPendingRemoval.add(layer); |
| 3219 | mLayersRemoved = true; |
Chia-I Wu | 98f1c10 | 2017-05-30 14:54:08 -0700 | [diff] [blame] | 3220 | mNumLayers -= 1 + layer->getChildrenCount(); |
Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 3221 | setTransactionFlags(eTransactionNeeded); |
| 3222 | return NO_ERROR; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3223 | } |
| 3224 | |
Fabien Sanglard | c8251eb | 2016-12-07 13:59:48 -0800 | [diff] [blame] | 3225 | uint32_t SurfaceFlinger::peekTransactionFlags() { |
Mathias Agopian | dea20b1 | 2011-05-03 17:04:02 -0700 | [diff] [blame] | 3226 | return android_atomic_release_load(&mTransactionFlags); |
| 3227 | } |
| 3228 | |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 3229 | uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags) { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3230 | return android_atomic_and(~flags, &mTransactionFlags) & flags; |
| 3231 | } |
| 3232 | |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 3233 | uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags) { |
Dan Stoza | 84d619e | 2018-03-28 17:07:36 -0700 | [diff] [blame] | 3234 | return setTransactionFlags(flags, VSyncModulator::TransactionStart::NORMAL); |
| 3235 | } |
| 3236 | |
| 3237 | uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags, |
| 3238 | VSyncModulator::TransactionStart transactionStart) { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3239 | uint32_t old = android_atomic_or(flags, &mTransactionFlags); |
Dan Stoza | 84d619e | 2018-03-28 17:07:36 -0700 | [diff] [blame] | 3240 | mVsyncModulator.setTransactionStart(transactionStart); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3241 | if ((old & flags)==0) { // wake the server up |
Mathias Agopian | 99ce5cd | 2012-01-31 18:24:27 -0800 | [diff] [blame] | 3242 | signalTransaction(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3243 | } |
| 3244 | return old; |
| 3245 | } |
| 3246 | |
chaviw | ca27f25 | 2018-02-06 16:46:39 -0800 | [diff] [blame] | 3247 | bool SurfaceFlinger::containsAnyInvalidClientState(const Vector<ComposerState>& states) { |
| 3248 | for (const ComposerState& state : states) { |
| 3249 | // Here we need to check that the interface we're given is indeed |
| 3250 | // one of our own. A malicious client could give us a nullptr |
| 3251 | // IInterface, or one of its own or even one of our own but a |
| 3252 | // different type. All these situations would cause us to crash. |
| 3253 | if (state.client == nullptr) { |
| 3254 | return true; |
| 3255 | } |
| 3256 | |
| 3257 | sp<IBinder> binder = IInterface::asBinder(state.client); |
| 3258 | if (binder == nullptr) { |
| 3259 | return true; |
| 3260 | } |
| 3261 | |
| 3262 | if (binder->queryLocalInterface(ISurfaceComposerClient::descriptor) == nullptr) { |
| 3263 | return true; |
| 3264 | } |
| 3265 | } |
| 3266 | return false; |
| 3267 | } |
| 3268 | |
Mathias Agopian | 8b33f03 | 2012-07-24 20:43:54 -0700 | [diff] [blame] | 3269 | void SurfaceFlinger::setTransactionState( |
chaviw | ca27f25 | 2018-02-06 16:46:39 -0800 | [diff] [blame] | 3270 | const Vector<ComposerState>& states, |
Mathias Agopian | 8b33f03 | 2012-07-24 20:43:54 -0700 | [diff] [blame] | 3271 | const Vector<DisplayState>& displays, |
| 3272 | uint32_t flags) |
| 3273 | { |
Jamie Gennis | 7c41bf7 | 2012-10-17 09:29:47 -0700 | [diff] [blame] | 3274 | ATRACE_CALL(); |
Mathias Agopian | 698c087 | 2011-06-28 19:09:31 -0700 | [diff] [blame] | 3275 | Mutex::Autolock _l(mStateLock); |
Jamie Gennis | 2837839 | 2011-10-12 17:39:00 -0700 | [diff] [blame] | 3276 | uint32_t transactionFlags = 0; |
Mathias Agopian | e57f292 | 2012-08-09 16:29:12 -0700 | [diff] [blame] | 3277 | |
chaviw | ca27f25 | 2018-02-06 16:46:39 -0800 | [diff] [blame] | 3278 | if (containsAnyInvalidClientState(states)) { |
| 3279 | return; |
| 3280 | } |
| 3281 | |
Jamie Gennis | 2d5e230 | 2012-10-15 18:24:43 -0700 | [diff] [blame] | 3282 | if (flags & eAnimation) { |
| 3283 | // For window updates that are part of an animation we must wait for |
| 3284 | // previous animation "frames" to be handled. |
| 3285 | while (mAnimTransactionPending) { |
Jamie Gennis | 7c41bf7 | 2012-10-17 09:29:47 -0700 | [diff] [blame] | 3286 | status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5)); |
Jamie Gennis | 2d5e230 | 2012-10-15 18:24:43 -0700 | [diff] [blame] | 3287 | if (CC_UNLIKELY(err != NO_ERROR)) { |
| 3288 | // just in case something goes wrong in SF, return to the |
Jamie Gennis | 7c41bf7 | 2012-10-17 09:29:47 -0700 | [diff] [blame] | 3289 | // caller after a few seconds. |
| 3290 | ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out " |
| 3291 | "waiting for previous animation frame"); |
Jamie Gennis | 2d5e230 | 2012-10-15 18:24:43 -0700 | [diff] [blame] | 3292 | mAnimTransactionPending = false; |
| 3293 | break; |
| 3294 | } |
| 3295 | } |
| 3296 | } |
| 3297 | |
chaviw | ca27f25 | 2018-02-06 16:46:39 -0800 | [diff] [blame] | 3298 | for (const DisplayState& display : displays) { |
| 3299 | transactionFlags |= setDisplayStateLocked(display); |
Jamie Gennis | b8d69a5 | 2011-10-10 15:48:06 -0700 | [diff] [blame] | 3300 | } |
| 3301 | |
chaviw | ca27f25 | 2018-02-06 16:46:39 -0800 | [diff] [blame] | 3302 | for (const ComposerState& state : states) { |
| 3303 | transactionFlags |= setClientStateLocked(state); |
| 3304 | } |
| 3305 | |
| 3306 | // Iterate through all layers again to determine if any need to be destroyed. Marking layers |
| 3307 | // as destroyed should only occur after setting all other states. This is to allow for a |
| 3308 | // child re-parent to happen before marking its original parent as destroyed (which would |
| 3309 | // then mark the child as destroyed). |
| 3310 | for (const ComposerState& state : states) { |
| 3311 | setDestroyStateLocked(state); |
Mathias Agopian | 698c087 | 2011-06-28 19:09:31 -0700 | [diff] [blame] | 3312 | } |
Mathias Agopian | 698c087 | 2011-06-28 19:09:31 -0700 | [diff] [blame] | 3313 | |
Jorim Jaggi | bdcf09c | 2017-08-08 15:22:08 +0200 | [diff] [blame] | 3314 | // If a synchronous transaction is explicitly requested without any changes, force a transaction |
| 3315 | // anyway. This can be used as a flush mechanism for previous async transactions. |
| 3316 | // Empty animation transaction can be used to simulate back-pressure, so also force a |
| 3317 | // transaction for empty animation transactions. |
| 3318 | if (transactionFlags == 0 && |
| 3319 | ((flags & eSynchronous) || (flags & eAnimation))) { |
Robert Carr | 2a7dbb4 | 2016-05-24 11:41:28 -0700 | [diff] [blame] | 3320 | transactionFlags = eTransactionNeeded; |
| 3321 | } |
| 3322 | |
Mathias Agopian | 386aa98 | 2011-11-07 21:58:03 -0800 | [diff] [blame] | 3323 | if (transactionFlags) { |
Lloyd Pique | 4dccc41 | 2018-01-22 17:21:36 -0800 | [diff] [blame] | 3324 | if (mInterceptor->isEnabled()) { |
| 3325 | mInterceptor->saveTransaction(states, mCurrentState.displays, displays, flags); |
Irvel | ffc9efc | 2016-07-27 15:16:37 -0700 | [diff] [blame] | 3326 | } |
Irvel | 468051e | 2016-06-13 16:44:44 -0700 | [diff] [blame] | 3327 | |
Mathias Agopian | 386aa98 | 2011-11-07 21:58:03 -0800 | [diff] [blame] | 3328 | // this triggers the transaction |
Dan Stoza | 84d619e | 2018-03-28 17:07:36 -0700 | [diff] [blame] | 3329 | const auto start = (flags & eEarlyWakeup) |
| 3330 | ? VSyncModulator::TransactionStart::EARLY |
| 3331 | : VSyncModulator::TransactionStart::NORMAL; |
| 3332 | setTransactionFlags(transactionFlags, start); |
Mathias Agopian | 386aa98 | 2011-11-07 21:58:03 -0800 | [diff] [blame] | 3333 | |
| 3334 | // if this is a synchronous transaction, wait for it to take effect |
| 3335 | // before returning. |
| 3336 | if (flags & eSynchronous) { |
Jamie Gennis | 2d5e230 | 2012-10-15 18:24:43 -0700 | [diff] [blame] | 3337 | mTransactionPending = true; |
Mathias Agopian | 386aa98 | 2011-11-07 21:58:03 -0800 | [diff] [blame] | 3338 | } |
Jamie Gennis | 2d5e230 | 2012-10-15 18:24:43 -0700 | [diff] [blame] | 3339 | if (flags & eAnimation) { |
| 3340 | mAnimTransactionPending = true; |
| 3341 | } |
| 3342 | while (mTransactionPending) { |
Mathias Agopian | 386aa98 | 2011-11-07 21:58:03 -0800 | [diff] [blame] | 3343 | status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5)); |
| 3344 | if (CC_UNLIKELY(err != NO_ERROR)) { |
| 3345 | // just in case something goes wrong in SF, return to the |
| 3346 | // called after a few seconds. |
Jamie Gennis | 2d5e230 | 2012-10-15 18:24:43 -0700 | [diff] [blame] | 3347 | ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out!"); |
| 3348 | mTransactionPending = false; |
Mathias Agopian | 386aa98 | 2011-11-07 21:58:03 -0800 | [diff] [blame] | 3349 | break; |
| 3350 | } |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 3351 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3352 | } |
| 3353 | } |
| 3354 | |
Mathias Agopian | e57f292 | 2012-08-09 16:29:12 -0700 | [diff] [blame] | 3355 | uint32_t SurfaceFlinger::setDisplayStateLocked(const DisplayState& s) |
| 3356 | { |
Jesse Hall | 9a14392 | 2012-10-04 16:29:19 -0700 | [diff] [blame] | 3357 | ssize_t dpyIdx = mCurrentState.displays.indexOfKey(s.token); |
| 3358 | if (dpyIdx < 0) |
| 3359 | return 0; |
| 3360 | |
Mathias Agopian | e57f292 | 2012-08-09 16:29:12 -0700 | [diff] [blame] | 3361 | uint32_t flags = 0; |
Jesse Hall | 9a14392 | 2012-10-04 16:29:19 -0700 | [diff] [blame] | 3362 | DisplayDeviceState& disp(mCurrentState.displays.editValueAt(dpyIdx)); |
Mathias Agopian | 3ee454a | 2012-08-27 16:28:24 -0700 | [diff] [blame] | 3363 | if (disp.isValid()) { |
Mathias Agopian | e57f292 | 2012-08-09 16:29:12 -0700 | [diff] [blame] | 3364 | const uint32_t what = s.what; |
| 3365 | if (what & DisplayState::eSurfaceChanged) { |
Marco Nelissen | 097ca27 | 2014-11-14 08:01:01 -0800 | [diff] [blame] | 3366 | if (IInterface::asBinder(disp.surface) != IInterface::asBinder(s.surface)) { |
Mathias Agopian | e57f292 | 2012-08-09 16:29:12 -0700 | [diff] [blame] | 3367 | disp.surface = s.surface; |
| 3368 | flags |= eDisplayTransactionNeeded; |
| 3369 | } |
| 3370 | } |
| 3371 | if (what & DisplayState::eLayerStackChanged) { |
| 3372 | if (disp.layerStack != s.layerStack) { |
| 3373 | disp.layerStack = s.layerStack; |
| 3374 | flags |= eDisplayTransactionNeeded; |
| 3375 | } |
| 3376 | } |
Mathias Agopian | 00e8c7a | 2012-09-04 19:30:46 -0700 | [diff] [blame] | 3377 | if (what & DisplayState::eDisplayProjectionChanged) { |
Mathias Agopian | e57f292 | 2012-08-09 16:29:12 -0700 | [diff] [blame] | 3378 | if (disp.orientation != s.orientation) { |
| 3379 | disp.orientation = s.orientation; |
| 3380 | flags |= eDisplayTransactionNeeded; |
| 3381 | } |
| 3382 | if (disp.frame != s.frame) { |
| 3383 | disp.frame = s.frame; |
| 3384 | flags |= eDisplayTransactionNeeded; |
| 3385 | } |
| 3386 | if (disp.viewport != s.viewport) { |
| 3387 | disp.viewport = s.viewport; |
| 3388 | flags |= eDisplayTransactionNeeded; |
| 3389 | } |
| 3390 | } |
Michael Lentine | 47e4540 | 2014-07-18 15:34:25 -0700 | [diff] [blame] | 3391 | if (what & DisplayState::eDisplaySizeChanged) { |
| 3392 | if (disp.width != s.width) { |
| 3393 | disp.width = s.width; |
| 3394 | flags |= eDisplayTransactionNeeded; |
| 3395 | } |
| 3396 | if (disp.height != s.height) { |
| 3397 | disp.height = s.height; |
| 3398 | flags |= eDisplayTransactionNeeded; |
| 3399 | } |
| 3400 | } |
Mathias Agopian | e57f292 | 2012-08-09 16:29:12 -0700 | [diff] [blame] | 3401 | } |
| 3402 | return flags; |
| 3403 | } |
| 3404 | |
chaviw | ca27f25 | 2018-02-06 16:46:39 -0800 | [diff] [blame] | 3405 | uint32_t SurfaceFlinger::setClientStateLocked(const ComposerState& composerState) { |
| 3406 | const layer_state_t& s = composerState.state; |
| 3407 | sp<Client> client(static_cast<Client*>(composerState.client.get())); |
| 3408 | |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 3409 | sp<Layer> layer(client->getLayerUser(s.surface)); |
chaviw | 8b3871a | 2017-11-01 17:41:01 -0700 | [diff] [blame] | 3410 | if (layer == nullptr) { |
| 3411 | return 0; |
| 3412 | } |
| 3413 | |
| 3414 | if (layer->isPendingRemoval()) { |
| 3415 | ALOGW("Attempting to set client state on removed layer: %s", layer->getName().string()); |
| 3416 | return 0; |
| 3417 | } |
| 3418 | |
| 3419 | uint32_t flags = 0; |
| 3420 | |
| 3421 | const uint32_t what = s.what; |
| 3422 | bool geometryAppliesWithResize = |
| 3423 | what & layer_state_t::eGeometryAppliesWithResize; |
| 3424 | if (what & layer_state_t::ePositionChanged) { |
| 3425 | if (layer->setPosition(s.x, s.y, !geometryAppliesWithResize)) { |
| 3426 | flags |= eTraversalNeeded; |
Mathias Agopian | e57f292 | 2012-08-09 16:29:12 -0700 | [diff] [blame] | 3427 | } |
chaviw | 8b3871a | 2017-11-01 17:41:01 -0700 | [diff] [blame] | 3428 | } |
| 3429 | if (what & layer_state_t::eLayerChanged) { |
| 3430 | // NOTE: index needs to be calculated before we update the state |
| 3431 | const auto& p = layer->getParent(); |
| 3432 | if (p == nullptr) { |
chaviw | 64f7b42 | 2017-07-12 10:31:58 -0700 | [diff] [blame] | 3433 | ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer); |
chaviw | 8b3871a | 2017-11-01 17:41:01 -0700 | [diff] [blame] | 3434 | if (layer->setLayer(s.z) && idx >= 0) { |
Mathias Agopian | e57f292 | 2012-08-09 16:29:12 -0700 | [diff] [blame] | 3435 | mCurrentState.layersSortedByZ.removeAt(idx); |
| 3436 | mCurrentState.layersSortedByZ.add(layer); |
| 3437 | // we need traversal (state changed) |
| 3438 | // AND transaction (list changed) |
| 3439 | flags |= eTransactionNeeded|eTraversalNeeded; |
| 3440 | } |
chaviw | 8b3871a | 2017-11-01 17:41:01 -0700 | [diff] [blame] | 3441 | } else { |
| 3442 | if (p->setChildLayer(layer, s.z)) { |
chaviw | 0617894 | 2017-07-27 10:25:59 -0700 | [diff] [blame] | 3443 | flags |= eTransactionNeeded|eTraversalNeeded; |
| 3444 | } |
| 3445 | } |
chaviw | 8b3871a | 2017-11-01 17:41:01 -0700 | [diff] [blame] | 3446 | } |
| 3447 | if (what & layer_state_t::eRelativeLayerChanged) { |
Robert Carr | 503c704 | 2017-09-27 15:06:08 -0700 | [diff] [blame] | 3448 | // NOTE: index needs to be calculated before we update the state |
| 3449 | const auto& p = layer->getParent(); |
| 3450 | if (p == nullptr) { |
| 3451 | ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer); |
| 3452 | if (layer->setRelativeLayer(s.relativeLayerHandle, s.z) && idx >= 0) { |
| 3453 | mCurrentState.layersSortedByZ.removeAt(idx); |
| 3454 | mCurrentState.layersSortedByZ.add(layer); |
| 3455 | // we need traversal (state changed) |
| 3456 | // AND transaction (list changed) |
| 3457 | flags |= eTransactionNeeded|eTraversalNeeded; |
| 3458 | } |
| 3459 | } else { |
| 3460 | if (p->setChildRelativeLayer(layer, s.relativeLayerHandle, s.z)) { |
| 3461 | flags |= eTransactionNeeded|eTraversalNeeded; |
| 3462 | } |
chaviw | 8b3871a | 2017-11-01 17:41:01 -0700 | [diff] [blame] | 3463 | } |
| 3464 | } |
| 3465 | if (what & layer_state_t::eSizeChanged) { |
| 3466 | if (layer->setSize(s.w, s.h)) { |
| 3467 | flags |= eTraversalNeeded; |
| 3468 | } |
| 3469 | } |
| 3470 | if (what & layer_state_t::eAlphaChanged) { |
| 3471 | if (layer->setAlpha(s.alpha)) |
| 3472 | flags |= eTraversalNeeded; |
| 3473 | } |
| 3474 | if (what & layer_state_t::eColorChanged) { |
| 3475 | if (layer->setColor(s.color)) |
| 3476 | flags |= eTraversalNeeded; |
| 3477 | } |
| 3478 | if (what & layer_state_t::eMatrixChanged) { |
| 3479 | if (layer->setMatrix(s.matrix)) |
| 3480 | flags |= eTraversalNeeded; |
| 3481 | } |
| 3482 | if (what & layer_state_t::eTransparentRegionChanged) { |
| 3483 | if (layer->setTransparentRegionHint(s.transparentRegion)) |
| 3484 | flags |= eTraversalNeeded; |
| 3485 | } |
| 3486 | if (what & layer_state_t::eFlagsChanged) { |
| 3487 | if (layer->setFlags(s.flags, s.mask)) |
| 3488 | flags |= eTraversalNeeded; |
| 3489 | } |
| 3490 | if (what & layer_state_t::eCropChanged) { |
| 3491 | if (layer->setCrop(s.crop, !geometryAppliesWithResize)) |
| 3492 | flags |= eTraversalNeeded; |
| 3493 | } |
| 3494 | if (what & layer_state_t::eFinalCropChanged) { |
| 3495 | if (layer->setFinalCrop(s.finalCrop, !geometryAppliesWithResize)) |
| 3496 | flags |= eTraversalNeeded; |
| 3497 | } |
| 3498 | if (what & layer_state_t::eLayerStackChanged) { |
| 3499 | ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer); |
| 3500 | // We only allow setting layer stacks for top level layers, |
| 3501 | // everything else inherits layer stack from its parent. |
| 3502 | if (layer->hasParent()) { |
| 3503 | ALOGE("Attempt to set layer stack on layer with parent (%s) is invalid", |
| 3504 | layer->getName().string()); |
| 3505 | } else if (idx < 0) { |
| 3506 | ALOGE("Attempt to set layer stack on layer without parent (%s) that " |
| 3507 | "that also does not appear in the top level layer list. Something" |
| 3508 | " has gone wrong.", layer->getName().string()); |
| 3509 | } else if (layer->setLayerStack(s.layerStack)) { |
| 3510 | mCurrentState.layersSortedByZ.removeAt(idx); |
| 3511 | mCurrentState.layersSortedByZ.add(layer); |
| 3512 | // we need traversal (state changed) |
| 3513 | // AND transaction (list changed) |
Lloyd Pique | d432a7c | 2018-03-23 16:05:31 -0700 | [diff] [blame] | 3514 | flags |= eTransactionNeeded|eTraversalNeeded|eDisplayLayerStackChanged; |
chaviw | 8b3871a | 2017-11-01 17:41:01 -0700 | [diff] [blame] | 3515 | } |
| 3516 | } |
| 3517 | if (what & layer_state_t::eDeferTransaction) { |
| 3518 | if (s.barrierHandle != nullptr) { |
| 3519 | layer->deferTransactionUntil(s.barrierHandle, s.frameNumber); |
| 3520 | } else if (s.barrierGbp != nullptr) { |
| 3521 | const sp<IGraphicBufferProducer>& gbp = s.barrierGbp; |
| 3522 | if (authenticateSurfaceTextureLocked(gbp)) { |
| 3523 | const auto& otherLayer = |
| 3524 | (static_cast<MonitoredProducer*>(gbp.get()))->getLayer(); |
| 3525 | layer->deferTransactionUntil(otherLayer, s.frameNumber); |
| 3526 | } else { |
| 3527 | ALOGE("Attempt to defer transaction to to an" |
| 3528 | " unrecognized GraphicBufferProducer"); |
Robert Carr | 1db73f6 | 2016-12-21 12:58:51 -0800 | [diff] [blame] | 3529 | } |
| 3530 | } |
chaviw | 8b3871a | 2017-11-01 17:41:01 -0700 | [diff] [blame] | 3531 | // We don't trigger a traversal here because if no other state is |
| 3532 | // changed, we don't want this to cause any more work |
| 3533 | } |
| 3534 | if (what & layer_state_t::eReparent) { |
chaviw | 8ea97bb | 2017-11-29 10:05:15 -0800 | [diff] [blame] | 3535 | bool hadParent = layer->hasParent(); |
chaviw | 8b3871a | 2017-11-01 17:41:01 -0700 | [diff] [blame] | 3536 | if (layer->reparent(s.parentHandleForChild)) { |
chaviw | 8ea97bb | 2017-11-29 10:05:15 -0800 | [diff] [blame] | 3537 | if (!hadParent) { |
| 3538 | mCurrentState.layersSortedByZ.remove(layer); |
| 3539 | } |
chaviw | 8b3871a | 2017-11-01 17:41:01 -0700 | [diff] [blame] | 3540 | flags |= eTransactionNeeded|eTraversalNeeded; |
Robert Carr | 9524cb3 | 2017-02-13 11:32:32 -0800 | [diff] [blame] | 3541 | } |
chaviw | 8b3871a | 2017-11-01 17:41:01 -0700 | [diff] [blame] | 3542 | } |
| 3543 | if (what & layer_state_t::eReparentChildren) { |
| 3544 | if (layer->reparentChildren(s.reparentHandle)) { |
| 3545 | flags |= eTransactionNeeded|eTraversalNeeded; |
Robert Carr | c3574f7 | 2016-03-24 12:19:32 -0700 | [diff] [blame] | 3546 | } |
Mathias Agopian | e57f292 | 2012-08-09 16:29:12 -0700 | [diff] [blame] | 3547 | } |
chaviw | 8b3871a | 2017-11-01 17:41:01 -0700 | [diff] [blame] | 3548 | if (what & layer_state_t::eDetachChildren) { |
| 3549 | layer->detachChildren(); |
| 3550 | } |
| 3551 | if (what & layer_state_t::eOverrideScalingModeChanged) { |
| 3552 | layer->setOverrideScalingMode(s.overrideScalingMode); |
| 3553 | // We don't trigger a traversal here because if no other state is |
| 3554 | // changed, we don't want this to cause any more work |
| 3555 | } |
Mathias Agopian | e57f292 | 2012-08-09 16:29:12 -0700 | [diff] [blame] | 3556 | return flags; |
| 3557 | } |
| 3558 | |
chaviw | ca27f25 | 2018-02-06 16:46:39 -0800 | [diff] [blame] | 3559 | void SurfaceFlinger::setDestroyStateLocked(const ComposerState& composerState) { |
| 3560 | const layer_state_t& state = composerState.state; |
| 3561 | sp<Client> client(static_cast<Client*>(composerState.client.get())); |
| 3562 | |
| 3563 | sp<Layer> layer(client->getLayerUser(state.surface)); |
| 3564 | if (layer == nullptr) { |
| 3565 | return; |
| 3566 | } |
| 3567 | |
| 3568 | if (layer->isPendingRemoval()) { |
| 3569 | ALOGW("Attempting to destroy on removed layer: %s", layer->getName().string()); |
| 3570 | return; |
| 3571 | } |
| 3572 | |
| 3573 | if (state.what & layer_state_t::eDestroySurface) { |
| 3574 | removeLayerLocked(mStateLock, layer); |
| 3575 | } |
| 3576 | } |
| 3577 | |
Mathias Agopian | 4d9b822 | 2013-03-12 17:11:48 -0700 | [diff] [blame] | 3578 | status_t SurfaceFlinger::createLayer( |
Mathias Agopian | 0ef4e15 | 2011-04-20 14:19:32 -0700 | [diff] [blame] | 3579 | const String8& name, |
| 3580 | const sp<Client>& client, |
Mathias Agopian | 4d9b822 | 2013-03-12 17:11:48 -0700 | [diff] [blame] | 3581 | uint32_t w, uint32_t h, PixelFormat format, uint32_t flags, |
rongliu | cfb187b | 2018-03-14 12:26:23 -0700 | [diff] [blame] | 3582 | int32_t windowType, int32_t ownerUid, sp<IBinder>* handle, |
Albert Chaulk | 479c60c | 2017-01-27 14:21:34 -0500 | [diff] [blame] | 3583 | sp<IGraphicBufferProducer>* gbp, sp<Layer>* parent) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3584 | { |
Mathias Agopian | 6e2d648 | 2009-07-09 18:16:43 -0700 | [diff] [blame] | 3585 | if (int32_t(w|h) < 0) { |
Mathias Agopian | 921e6ac | 2012-07-23 23:11:29 -0700 | [diff] [blame] | 3586 | ALOGE("createLayer() failed, w or h is negative (w=%d, h=%d)", |
Mathias Agopian | 6e2d648 | 2009-07-09 18:16:43 -0700 | [diff] [blame] | 3587 | int(w), int(h)); |
Mathias Agopian | 4d9b822 | 2013-03-12 17:11:48 -0700 | [diff] [blame] | 3588 | return BAD_VALUE; |
Mathias Agopian | 6e2d648 | 2009-07-09 18:16:43 -0700 | [diff] [blame] | 3589 | } |
Andreas Huber | 8b42e8a | 2010-08-16 08:49:37 -0700 | [diff] [blame] | 3590 | |
Mathias Agopian | 4d9b822 | 2013-03-12 17:11:48 -0700 | [diff] [blame] | 3591 | status_t result = NO_ERROR; |
| 3592 | |
| 3593 | sp<Layer> layer; |
| 3594 | |
Cody Northrop | bc75528 | 2017-03-31 12:00:08 -0600 | [diff] [blame] | 3595 | String8 uniqueName = getUniqueLayerName(name); |
| 3596 | |
Mathias Agopian | 3165cc2 | 2012-08-08 19:42:09 -0700 | [diff] [blame] | 3597 | switch (flags & ISurfaceComposerClient::eFXSurfaceMask) { |
| 3598 | case ISurfaceComposerClient::eFXSurfaceNormal: |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 3599 | result = createBufferLayer(client, |
Cody Northrop | bc75528 | 2017-03-31 12:00:08 -0600 | [diff] [blame] | 3600 | uniqueName, w, h, flags, format, |
Mathias Agopian | 4d9b822 | 2013-03-12 17:11:48 -0700 | [diff] [blame] | 3601 | handle, gbp, &layer); |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 3602 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3603 | break; |
chaviw | 13fdc49 | 2017-06-27 12:40:18 -0700 | [diff] [blame] | 3604 | case ISurfaceComposerClient::eFXSurfaceColor: |
| 3605 | result = createColorLayer(client, |
Cody Northrop | bc75528 | 2017-03-31 12:00:08 -0600 | [diff] [blame] | 3606 | uniqueName, w, h, flags, |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 3607 | handle, &layer); |
Mathias Agopian | 4d9b822 | 2013-03-12 17:11:48 -0700 | [diff] [blame] | 3608 | break; |
| 3609 | default: |
| 3610 | result = BAD_VALUE; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3611 | break; |
| 3612 | } |
| 3613 | |
Dan Stoza | 7d89d06 | 2015-04-30 13:29:25 -0700 | [diff] [blame] | 3614 | if (result != NO_ERROR) { |
| 3615 | return result; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3616 | } |
Dan Stoza | 7d89d06 | 2015-04-30 13:29:25 -0700 | [diff] [blame] | 3617 | |
Chia-I Wu | ab0c319 | 2017-08-01 11:29:00 -0700 | [diff] [blame] | 3618 | // window type is WINDOW_TYPE_DONT_SCREENSHOT from SurfaceControl.java |
| 3619 | // TODO b/64227542 |
| 3620 | if (windowType == 441731) { |
| 3621 | windowType = 2024; // TYPE_NAVIGATION_BAR_PANEL |
| 3622 | layer->setPrimaryDisplayOnly(); |
| 3623 | } |
| 3624 | |
Albert Chaulk | 479c60c | 2017-01-27 14:21:34 -0500 | [diff] [blame] | 3625 | layer->setInfo(windowType, ownerUid); |
| 3626 | |
Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 3627 | result = addClientLayer(client, *handle, *gbp, layer, *parent); |
Dan Stoza | 7d89d06 | 2015-04-30 13:29:25 -0700 | [diff] [blame] | 3628 | if (result != NO_ERROR) { |
| 3629 | return result; |
| 3630 | } |
Lloyd Pique | 4dccc41 | 2018-01-22 17:21:36 -0800 | [diff] [blame] | 3631 | mInterceptor->saveSurfaceCreation(layer); |
Dan Stoza | 7d89d06 | 2015-04-30 13:29:25 -0700 | [diff] [blame] | 3632 | |
| 3633 | setTransactionFlags(eTransactionNeeded); |
Mathias Agopian | 4d9b822 | 2013-03-12 17:11:48 -0700 | [diff] [blame] | 3634 | return result; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3635 | } |
| 3636 | |
Cody Northrop | bc75528 | 2017-03-31 12:00:08 -0600 | [diff] [blame] | 3637 | String8 SurfaceFlinger::getUniqueLayerName(const String8& name) |
| 3638 | { |
| 3639 | bool matchFound = true; |
| 3640 | uint32_t dupeCounter = 0; |
| 3641 | |
| 3642 | // Tack on our counter whether there is a hit or not, so everyone gets a tag |
| 3643 | String8 uniqueName = name + "#" + String8(std::to_string(dupeCounter).c_str()); |
| 3644 | |
| 3645 | // Loop over layers until we're sure there is no matching name |
| 3646 | while (matchFound) { |
| 3647 | matchFound = false; |
| 3648 | mDrawingState.traverseInZOrder([&](Layer* layer) { |
| 3649 | if (layer->getName() == uniqueName) { |
| 3650 | matchFound = true; |
| 3651 | uniqueName = name + "#" + String8(std::to_string(++dupeCounter).c_str()); |
| 3652 | } |
| 3653 | }); |
| 3654 | } |
| 3655 | |
| 3656 | ALOGD_IF(dupeCounter > 0, "duplicate layer name: changing %s to %s", name.c_str(), uniqueName.c_str()); |
| 3657 | |
| 3658 | return uniqueName; |
| 3659 | } |
| 3660 | |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 3661 | status_t SurfaceFlinger::createBufferLayer(const sp<Client>& client, |
Mathias Agopian | 4d9b822 | 2013-03-12 17:11:48 -0700 | [diff] [blame] | 3662 | const String8& name, uint32_t w, uint32_t h, uint32_t flags, PixelFormat& format, |
| 3663 | sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3664 | { |
| 3665 | // initialize the surfaces |
Mathias Agopian | 92a979a | 2012-08-02 18:32:23 -0700 | [diff] [blame] | 3666 | switch (format) { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3667 | case PIXEL_FORMAT_TRANSPARENT: |
| 3668 | case PIXEL_FORMAT_TRANSLUCENT: |
| 3669 | format = PIXEL_FORMAT_RGBA_8888; |
| 3670 | break; |
| 3671 | case PIXEL_FORMAT_OPAQUE: |
Mathias Agopian | 8f10540 | 2010-04-05 18:01:24 -0700 | [diff] [blame] | 3672 | format = PIXEL_FORMAT_RGBX_8888; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3673 | break; |
| 3674 | } |
| 3675 | |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 3676 | sp<BufferLayer> layer = new BufferLayer(this, client, name, w, h, flags); |
| 3677 | status_t err = layer->setBuffers(w, h, format, flags); |
Mathias Agopian | 4d9b822 | 2013-03-12 17:11:48 -0700 | [diff] [blame] | 3678 | if (err == NO_ERROR) { |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 3679 | *handle = layer->getHandle(); |
| 3680 | *gbp = layer->getProducer(); |
| 3681 | *outLayer = layer; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3682 | } |
Mathias Agopian | 4d9b822 | 2013-03-12 17:11:48 -0700 | [diff] [blame] | 3683 | |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 3684 | ALOGE_IF(err, "createBufferLayer() failed (%s)", strerror(-err)); |
Mathias Agopian | 4d9b822 | 2013-03-12 17:11:48 -0700 | [diff] [blame] | 3685 | return err; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3686 | } |
| 3687 | |
chaviw | 13fdc49 | 2017-06-27 12:40:18 -0700 | [diff] [blame] | 3688 | status_t SurfaceFlinger::createColorLayer(const sp<Client>& client, |
Mathias Agopian | 4d9b822 | 2013-03-12 17:11:48 -0700 | [diff] [blame] | 3689 | const String8& name, uint32_t w, uint32_t h, uint32_t flags, |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 3690 | sp<IBinder>* handle, sp<Layer>* outLayer) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3691 | { |
chaviw | 13fdc49 | 2017-06-27 12:40:18 -0700 | [diff] [blame] | 3692 | *outLayer = new ColorLayer(this, client, name, w, h, flags); |
Mathias Agopian | 4d9b822 | 2013-03-12 17:11:48 -0700 | [diff] [blame] | 3693 | *handle = (*outLayer)->getHandle(); |
Mathias Agopian | 4d9b822 | 2013-03-12 17:11:48 -0700 | [diff] [blame] | 3694 | return NO_ERROR; |
Mathias Agopian | 118d024 | 2011-10-13 16:02:48 -0700 | [diff] [blame] | 3695 | } |
| 3696 | |
Mathias Agopian | ac9fa42 | 2013-02-11 16:40:36 -0800 | [diff] [blame] | 3697 | status_t SurfaceFlinger::onLayerRemoved(const sp<Client>& client, const sp<IBinder>& handle) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3698 | { |
Robert Carr | 9524cb3 | 2017-02-13 11:32:32 -0800 | [diff] [blame] | 3699 | // called by a client when it wants to remove a Layer |
Mathias Agopian | 6710604 | 2013-03-14 19:18:13 -0700 | [diff] [blame] | 3700 | status_t err = NO_ERROR; |
| 3701 | sp<Layer> l(client->getLayerUser(handle)); |
Peiyong Lin | 566a3b4 | 2018-01-09 18:22:43 -0800 | [diff] [blame] | 3702 | if (l != nullptr) { |
Lloyd Pique | 4dccc41 | 2018-01-22 17:21:36 -0800 | [diff] [blame] | 3703 | mInterceptor->saveSurfaceDeletion(l); |
Mathias Agopian | 6710604 | 2013-03-14 19:18:13 -0700 | [diff] [blame] | 3704 | err = removeLayer(l); |
| 3705 | ALOGE_IF(err<0 && err != NAME_NOT_FOUND, |
| 3706 | "error removing layer=%p (%s)", l.get(), strerror(-err)); |
Mathias Agopian | 9a11206 | 2009-04-17 19:36:26 -0700 | [diff] [blame] | 3707 | } |
| 3708 | return err; |
| 3709 | } |
| 3710 | |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 3711 | status_t SurfaceFlinger::onLayerDestroyed(const wp<Layer>& layer) |
Mathias Agopian | 9a11206 | 2009-04-17 19:36:26 -0700 | [diff] [blame] | 3712 | { |
Mathias Agopian | 6710604 | 2013-03-14 19:18:13 -0700 | [diff] [blame] | 3713 | // called by ~LayerCleaner() when all references to the IBinder (handle) |
| 3714 | // are gone |
Robert Carr | 9524cb3 | 2017-02-13 11:32:32 -0800 | [diff] [blame] | 3715 | sp<Layer> l = layer.promote(); |
| 3716 | if (l == nullptr) { |
| 3717 | // The layer has already been removed, carry on |
| 3718 | return NO_ERROR; |
Robert Carr | 9524cb3 | 2017-02-13 11:32:32 -0800 | [diff] [blame] | 3719 | } |
Chia-I Wu | 515dc9c | 2017-06-15 12:53:59 -0700 | [diff] [blame] | 3720 | // If we have a parent, then we can continue to live as long as it does. |
| 3721 | return removeLayer(l, true); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3722 | } |
| 3723 | |
Mathias Agopian | b60314a | 2012-04-10 22:09:54 -0700 | [diff] [blame] | 3724 | // --------------------------------------------------------------------------- |
| 3725 | |
Andy McFadden | 13a082e | 2012-08-24 10:16:42 -0700 | [diff] [blame] | 3726 | void SurfaceFlinger::onInitializeDisplays() { |
Jesse Hall | 01e2905 | 2013-02-19 16:13:35 -0800 | [diff] [blame] | 3727 | // reset screen orientation and use primary layer stack |
Andy McFadden | 13a082e | 2012-08-24 10:16:42 -0700 | [diff] [blame] | 3728 | Vector<ComposerState> state; |
| 3729 | Vector<DisplayState> displays; |
| 3730 | DisplayState d; |
Jesse Hall | 01e2905 | 2013-02-19 16:13:35 -0800 | [diff] [blame] | 3731 | d.what = DisplayState::eDisplayProjectionChanged | |
| 3732 | DisplayState::eLayerStackChanged; |
Jesse Hall | 692c723 | 2012-11-08 15:41:56 -0800 | [diff] [blame] | 3733 | d.token = mBuiltinDisplays[DisplayDevice::DISPLAY_PRIMARY]; |
Jesse Hall | 01e2905 | 2013-02-19 16:13:35 -0800 | [diff] [blame] | 3734 | d.layerStack = 0; |
Andy McFadden | 13a082e | 2012-08-24 10:16:42 -0700 | [diff] [blame] | 3735 | d.orientation = DisplayState::eOrientationDefault; |
Jeff Brown | 4c05dd1 | 2012-09-09 00:07:17 -0700 | [diff] [blame] | 3736 | d.frame.makeInvalid(); |
| 3737 | d.viewport.makeInvalid(); |
Michael Lentine | 47e4540 | 2014-07-18 15:34:25 -0700 | [diff] [blame] | 3738 | d.width = 0; |
| 3739 | d.height = 0; |
Andy McFadden | 13a082e | 2012-08-24 10:16:42 -0700 | [diff] [blame] | 3740 | displays.add(d); |
| 3741 | setTransactionState(state, displays, 0); |
Steven Thomas | b02664d | 2017-07-26 18:48:28 -0700 | [diff] [blame] | 3742 | setPowerModeInternal(getDisplayDevice(d.token), HWC_POWER_MODE_NORMAL, |
| 3743 | /*stateLockHeld*/ false); |
Jamie Gennis | 6547ff4 | 2013-07-16 20:12:42 -0700 | [diff] [blame] | 3744 | |
David Sodman | 105b7dc | 2017-11-04 20:28:14 -0700 | [diff] [blame] | 3745 | const auto& activeConfig = getBE().mHwc->getActiveConfig(HWC_DISPLAY_PRIMARY); |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 3746 | const nsecs_t period = activeConfig->getVsyncPeriod(); |
Jamie Gennis | 6547ff4 | 2013-07-16 20:12:42 -0700 | [diff] [blame] | 3747 | mAnimFrameTracker.setDisplayRefreshPeriod(period); |
Brian Anderson | 0a61b0c | 2016-12-07 14:55:56 -0800 | [diff] [blame] | 3748 | |
Brian Anderson | d001058 | 2017-03-07 13:20:31 -0800 | [diff] [blame] | 3749 | // Use phase of 0 since phase is not known. |
| 3750 | // Use latency of 0, which will snap to the ideal latency. |
| 3751 | setCompositorTimingSnapped(0, period, 0); |
Andy McFadden | 13a082e | 2012-08-24 10:16:42 -0700 | [diff] [blame] | 3752 | } |
| 3753 | |
| 3754 | void SurfaceFlinger::initializeDisplays() { |
| 3755 | class MessageScreenInitialized : public MessageBase { |
| 3756 | SurfaceFlinger* flinger; |
| 3757 | public: |
Chih-Hung Hsieh | c406791 | 2016-05-03 14:03:27 -0700 | [diff] [blame] | 3758 | explicit MessageScreenInitialized(SurfaceFlinger* flinger) : flinger(flinger) { } |
Andy McFadden | 13a082e | 2012-08-24 10:16:42 -0700 | [diff] [blame] | 3759 | virtual bool handler() { |
| 3760 | flinger->onInitializeDisplays(); |
| 3761 | return true; |
| 3762 | } |
| 3763 | }; |
| 3764 | sp<MessageBase> msg = new MessageScreenInitialized(this); |
| 3765 | postMessageAsync(msg); // we may be called from main thread, use async message |
| 3766 | } |
| 3767 | |
Prashant Malani | 2c9b11f | 2014-05-25 01:36:31 -0700 | [diff] [blame] | 3768 | void SurfaceFlinger::setPowerModeInternal(const sp<DisplayDevice>& hw, |
Steven Thomas | b02664d | 2017-07-26 18:48:28 -0700 | [diff] [blame] | 3769 | int mode, bool stateLockHeld) { |
Prashant Malani | 2c9b11f | 2014-05-25 01:36:31 -0700 | [diff] [blame] | 3770 | ALOGD("Set power mode=%d, type=%d flinger=%p", mode, hw->getDisplayType(), |
| 3771 | this); |
Prashant Malani | 2c9b11f | 2014-05-25 01:36:31 -0700 | [diff] [blame] | 3772 | int currentMode = hw->getPowerMode(); |
Andy McFadden | 13a082e | 2012-08-24 10:16:42 -0700 | [diff] [blame] | 3773 | |
Prashant Malani | 2c9b11f | 2014-05-25 01:36:31 -0700 | [diff] [blame] | 3774 | if (mode == currentMode) { |
Andy McFadden | c01a79d | 2012-09-27 16:02:06 -0700 | [diff] [blame] | 3775 | return; |
| 3776 | } |
| 3777 | |
Prashant Malani | 2c9b11f | 2014-05-25 01:36:31 -0700 | [diff] [blame] | 3778 | hw->setPowerMode(mode); |
Dominik Laskowski | 281644e | 2018-04-19 15:47:35 -0700 | [diff] [blame] | 3779 | if (hw->isVirtual()) { |
Prashant Malani | 2c9b11f | 2014-05-25 01:36:31 -0700 | [diff] [blame] | 3780 | ALOGW("Trying to set power mode for virtual display"); |
| 3781 | return; |
| 3782 | } |
Andy McFadden | c01a79d | 2012-09-27 16:02:06 -0700 | [diff] [blame] | 3783 | |
Lloyd Pique | 4dccc41 | 2018-01-22 17:21:36 -0800 | [diff] [blame] | 3784 | if (mInterceptor->isEnabled()) { |
Steven Thomas | b02664d | 2017-07-26 18:48:28 -0700 | [diff] [blame] | 3785 | ConditionalLock lock(mStateLock, !stateLockHeld); |
Irvel | ffc9efc | 2016-07-27 15:16:37 -0700 | [diff] [blame] | 3786 | ssize_t idx = mCurrentState.displays.indexOfKey(hw->getDisplayToken()); |
| 3787 | if (idx < 0) { |
| 3788 | ALOGW("Surface Interceptor SavePowerMode: invalid display token"); |
| 3789 | return; |
| 3790 | } |
Dominik Laskowski | 663bd28 | 2018-04-19 15:26:54 -0700 | [diff] [blame] | 3791 | mInterceptor->savePowerModeUpdate(mCurrentState.displays.valueAt(idx).sequenceId, mode); |
Irvel | ffc9efc | 2016-07-27 15:16:37 -0700 | [diff] [blame] | 3792 | } |
| 3793 | |
Dominik Laskowski | 281644e | 2018-04-19 15:47:35 -0700 | [diff] [blame] | 3794 | int32_t type = hw->getDisplayType(); |
Prashant Malani | 2c9b11f | 2014-05-25 01:36:31 -0700 | [diff] [blame] | 3795 | if (currentMode == HWC_POWER_MODE_OFF) { |
Tim Murray | f9d4e44 | 2016-08-02 15:43:59 -0700 | [diff] [blame] | 3796 | // Turn on the display |
Prashant Malani | 2c9b11f | 2014-05-25 01:36:31 -0700 | [diff] [blame] | 3797 | getHwComposer().setPowerMode(type, mode); |
Dominik Laskowski | 281644e | 2018-04-19 15:47:35 -0700 | [diff] [blame] | 3798 | if (hw->isPrimary() && mode != HWC_POWER_MODE_DOZE_SUSPEND) { |
Andy McFadden | c01a79d | 2012-09-27 16:02:06 -0700 | [diff] [blame] | 3799 | // FIXME: eventthread only knows about the main display right now |
| 3800 | mEventThread->onScreenAcquired(); |
Jesse Hall | 948fe0c | 2013-10-14 12:56:09 -0700 | [diff] [blame] | 3801 | resyncToHardwareVsync(true); |
Andy McFadden | c01a79d | 2012-09-27 16:02:06 -0700 | [diff] [blame] | 3802 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3803 | |
Prashant Malani | 2c9b11f | 2014-05-25 01:36:31 -0700 | [diff] [blame] | 3804 | mVisibleRegionsDirty = true; |
Dan Stoza | b90cf07 | 2015-03-05 11:05:59 -0800 | [diff] [blame] | 3805 | mHasPoweredOff = true; |
Dan Stoza | c7a25ad | 2018-04-12 11:45:09 -0700 | [diff] [blame] | 3806 | repaintEverything(); |
Tim Murray | f9d4e44 | 2016-08-02 15:43:59 -0700 | [diff] [blame] | 3807 | |
| 3808 | struct sched_param param = {0}; |
| 3809 | param.sched_priority = 1; |
| 3810 | if (sched_setscheduler(0, SCHED_FIFO, ¶m) != 0) { |
| 3811 | ALOGW("Couldn't set SCHED_FIFO on display on"); |
| 3812 | } |
Prashant Malani | 2c9b11f | 2014-05-25 01:36:31 -0700 | [diff] [blame] | 3813 | } else if (mode == HWC_POWER_MODE_OFF) { |
Tim Murray | f9d4e44 | 2016-08-02 15:43:59 -0700 | [diff] [blame] | 3814 | // Turn off the display |
| 3815 | struct sched_param param = {0}; |
| 3816 | if (sched_setscheduler(0, SCHED_OTHER, ¶m) != 0) { |
| 3817 | ALOGW("Couldn't set SCHED_OTHER on display off"); |
| 3818 | } |
| 3819 | |
Dominik Laskowski | 281644e | 2018-04-19 15:47:35 -0700 | [diff] [blame] | 3820 | if (hw->isPrimary() && currentMode != HWC_POWER_MODE_DOZE_SUSPEND) { |
Jesse Hall | 948fe0c | 2013-10-14 12:56:09 -0700 | [diff] [blame] | 3821 | disableHardwareVsync(true); // also cancels any in-progress resync |
| 3822 | |
Mathias Agopian | cde87a3 | 2012-09-13 14:09:01 -0700 | [diff] [blame] | 3823 | // FIXME: eventthread only knows about the main display right now |
| 3824 | mEventThread->onScreenReleased(); |
| 3825 | } |
Andy McFadden | c01a79d | 2012-09-27 16:02:06 -0700 | [diff] [blame] | 3826 | |
Prashant Malani | 2c9b11f | 2014-05-25 01:36:31 -0700 | [diff] [blame] | 3827 | getHwComposer().setPowerMode(type, mode); |
| 3828 | mVisibleRegionsDirty = true; |
| 3829 | // from this point on, SF will stop drawing on this display |
Matthew Bouyack | 38d4961 | 2017-05-12 12:49:32 -0700 | [diff] [blame] | 3830 | } else if (mode == HWC_POWER_MODE_DOZE || |
| 3831 | mode == HWC_POWER_MODE_NORMAL) { |
Zheng Zhang | 8a43fe6 | 2017-03-17 11:19:39 +0100 | [diff] [blame] | 3832 | // Update display while dozing |
| 3833 | getHwComposer().setPowerMode(type, mode); |
Dominik Laskowski | 281644e | 2018-04-19 15:47:35 -0700 | [diff] [blame] | 3834 | if (hw->isPrimary() && currentMode == HWC_POWER_MODE_DOZE_SUSPEND) { |
Zheng Zhang | 8a43fe6 | 2017-03-17 11:19:39 +0100 | [diff] [blame] | 3835 | // FIXME: eventthread only knows about the main display right now |
| 3836 | mEventThread->onScreenAcquired(); |
| 3837 | resyncToHardwareVsync(true); |
| 3838 | } |
| 3839 | } else if (mode == HWC_POWER_MODE_DOZE_SUSPEND) { |
| 3840 | // Leave display going to doze |
Dominik Laskowski | 281644e | 2018-04-19 15:47:35 -0700 | [diff] [blame] | 3841 | if (hw->isPrimary()) { |
Zheng Zhang | 8a43fe6 | 2017-03-17 11:19:39 +0100 | [diff] [blame] | 3842 | disableHardwareVsync(true); // also cancels any in-progress resync |
| 3843 | // FIXME: eventthread only knows about the main display right now |
| 3844 | mEventThread->onScreenReleased(); |
| 3845 | } |
| 3846 | getHwComposer().setPowerMode(type, mode); |
Prashant Malani | 2c9b11f | 2014-05-25 01:36:31 -0700 | [diff] [blame] | 3847 | } else { |
Matthew Bouyack | 38d4961 | 2017-05-12 12:49:32 -0700 | [diff] [blame] | 3848 | ALOGE("Attempting to set unknown power mode: %d\n", mode); |
Prashant Malani | 2c9b11f | 2014-05-25 01:36:31 -0700 | [diff] [blame] | 3849 | getHwComposer().setPowerMode(type, mode); |
Andy McFadden | c01a79d | 2012-09-27 16:02:06 -0700 | [diff] [blame] | 3850 | } |
Alice Sheng | 05ee4c9 | 2018-03-27 15:03:10 -0700 | [diff] [blame] | 3851 | ALOGD("Finished set power mode=%d, type=%d", mode, hw->getDisplayType()); |
Andy McFadden | c01a79d | 2012-09-27 16:02:06 -0700 | [diff] [blame] | 3852 | } |
| 3853 | |
Prashant Malani | 2c9b11f | 2014-05-25 01:36:31 -0700 | [diff] [blame] | 3854 | void SurfaceFlinger::setPowerMode(const sp<IBinder>& display, int mode) { |
| 3855 | class MessageSetPowerMode: public MessageBase { |
Mathias Agopian | db9b41f | 2012-10-15 16:51:41 -0700 | [diff] [blame] | 3856 | SurfaceFlinger& mFlinger; |
| 3857 | sp<IBinder> mDisplay; |
Prashant Malani | 2c9b11f | 2014-05-25 01:36:31 -0700 | [diff] [blame] | 3858 | int mMode; |
Andy McFadden | c01a79d | 2012-09-27 16:02:06 -0700 | [diff] [blame] | 3859 | public: |
Prashant Malani | 2c9b11f | 2014-05-25 01:36:31 -0700 | [diff] [blame] | 3860 | MessageSetPowerMode(SurfaceFlinger& flinger, |
| 3861 | const sp<IBinder>& disp, int mode) : mFlinger(flinger), |
| 3862 | mDisplay(disp) { mMode = mode; } |
Andy McFadden | c01a79d | 2012-09-27 16:02:06 -0700 | [diff] [blame] | 3863 | virtual bool handler() { |
Prashant Malani | 2c9b11f | 2014-05-25 01:36:31 -0700 | [diff] [blame] | 3864 | sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay)); |
Peiyong Lin | 566a3b4 | 2018-01-09 18:22:43 -0800 | [diff] [blame] | 3865 | if (hw == nullptr) { |
Prashant Malani | 2c9b11f | 2014-05-25 01:36:31 -0700 | [diff] [blame] | 3866 | ALOGE("Attempt to set power mode = %d for null display %p", |
Michael Lentine | 7306c67 | 2014-07-30 13:00:37 -0700 | [diff] [blame] | 3867 | mMode, mDisplay.get()); |
Dominik Laskowski | 281644e | 2018-04-19 15:47:35 -0700 | [diff] [blame] | 3868 | } else if (hw->isVirtual()) { |
Prashant Malani | 2c9b11f | 2014-05-25 01:36:31 -0700 | [diff] [blame] | 3869 | ALOGW("Attempt to set power mode = %d for virtual display", |
| 3870 | mMode); |
Mathias Agopian | db9b41f | 2012-10-15 16:51:41 -0700 | [diff] [blame] | 3871 | } else { |
Steven Thomas | b02664d | 2017-07-26 18:48:28 -0700 | [diff] [blame] | 3872 | mFlinger.setPowerModeInternal( |
| 3873 | hw, mMode, /*stateLockHeld*/ false); |
Mathias Agopian | db9b41f | 2012-10-15 16:51:41 -0700 | [diff] [blame] | 3874 | } |
Andy McFadden | c01a79d | 2012-09-27 16:02:06 -0700 | [diff] [blame] | 3875 | return true; |
| 3876 | } |
| 3877 | }; |
Prashant Malani | 2c9b11f | 2014-05-25 01:36:31 -0700 | [diff] [blame] | 3878 | sp<MessageBase> msg = new MessageSetPowerMode(*this, display, mode); |
Mathias Agopian | db9b41f | 2012-10-15 16:51:41 -0700 | [diff] [blame] | 3879 | postMessageSync(msg); |
Mathias Agopian | b60314a | 2012-04-10 22:09:54 -0700 | [diff] [blame] | 3880 | } |
| 3881 | |
| 3882 | // --------------------------------------------------------------------------- |
| 3883 | |
Lloyd Pique | 755e319 | 2018-01-31 16:46:15 -0800 | [diff] [blame] | 3884 | status_t SurfaceFlinger::doDump(int fd, const Vector<String16>& args, bool asProto) |
| 3885 | NO_THREAD_SAFETY_ANALYSIS { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3886 | String8 result; |
Mathias Agopian | 99b4984 | 2011-06-27 16:05:52 -0700 | [diff] [blame] | 3887 | |
Mathias Agopian | bd11533 | 2013-04-18 16:41:04 -0700 | [diff] [blame] | 3888 | IPCThreadState* ipc = IPCThreadState::self(); |
| 3889 | const int pid = ipc->getCallingPid(); |
| 3890 | const int uid = ipc->getCallingUid(); |
Vishnu Nair | 6a40853 | 2017-10-24 09:11:27 -0700 | [diff] [blame] | 3891 | |
Mathias Agopian | bd11533 | 2013-04-18 16:41:04 -0700 | [diff] [blame] | 3892 | if ((uid != AID_SHELL) && |
| 3893 | !PermissionCache::checkPermission(sDump, pid, uid)) { |
Mathias Agopian | 74d211a | 2013-04-22 16:55:35 +0200 | [diff] [blame] | 3894 | result.appendFormat("Permission Denial: " |
Mathias Agopian | bd11533 | 2013-04-18 16:41:04 -0700 | [diff] [blame] | 3895 | "can't dump SurfaceFlinger from pid=%d, uid=%d\n", pid, uid); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3896 | } else { |
Jesse Hall | fcd15b4 | 2014-12-23 13:57:23 -0800 | [diff] [blame] | 3897 | // Try to get the main lock, but give up after one second |
Mathias Agopian | 9795c42 | 2009-08-26 16:36:26 -0700 | [diff] [blame] | 3898 | // (this would indicate SF is stuck, but we want to be able to |
| 3899 | // print something in dumpsys). |
Jesse Hall | fcd15b4 | 2014-12-23 13:57:23 -0800 | [diff] [blame] | 3900 | status_t err = mStateLock.timedLock(s2ns(1)); |
| 3901 | bool locked = (err == NO_ERROR); |
Mathias Agopian | 9795c42 | 2009-08-26 16:36:26 -0700 | [diff] [blame] | 3902 | if (!locked) { |
Jesse Hall | fcd15b4 | 2014-12-23 13:57:23 -0800 | [diff] [blame] | 3903 | result.appendFormat( |
| 3904 | "SurfaceFlinger appears to be unresponsive (%s [%d]), " |
| 3905 | "dumping anyways (no locks held)\n", strerror(-err), err); |
Mathias Agopian | 9795c42 | 2009-08-26 16:36:26 -0700 | [diff] [blame] | 3906 | } |
| 3907 | |
Mathias Agopian | 82d7ab6 | 2012-01-19 18:34:40 -0800 | [diff] [blame] | 3908 | bool dumpAll = true; |
| 3909 | size_t index = 0; |
Mathias Agopian | 25e66fc | 2012-01-28 22:31:55 -0800 | [diff] [blame] | 3910 | size_t numArgs = args.size(); |
chaviw | a3d7bd3 | 2017-11-03 09:41:53 -0700 | [diff] [blame] | 3911 | |
Mathias Agopian | 25e66fc | 2012-01-28 22:31:55 -0800 | [diff] [blame] | 3912 | if (numArgs) { |
Mathias Agopian | 25e66fc | 2012-01-28 22:31:55 -0800 | [diff] [blame] | 3913 | if ((index < numArgs) && |
| 3914 | (args[index] == String16("--list"))) { |
| 3915 | index++; |
Mathias Agopian | 74d211a | 2013-04-22 16:55:35 +0200 | [diff] [blame] | 3916 | listLayersLocked(args, index, result); |
Mathias Agopian | 35aadd6 | 2012-03-08 22:01:51 -0800 | [diff] [blame] | 3917 | dumpAll = false; |
Mathias Agopian | 25e66fc | 2012-01-28 22:31:55 -0800 | [diff] [blame] | 3918 | } |
| 3919 | |
| 3920 | if ((index < numArgs) && |
| 3921 | (args[index] == String16("--latency"))) { |
Mathias Agopian | 82d7ab6 | 2012-01-19 18:34:40 -0800 | [diff] [blame] | 3922 | index++; |
Mathias Agopian | 74d211a | 2013-04-22 16:55:35 +0200 | [diff] [blame] | 3923 | dumpStatsLocked(args, index, result); |
Mathias Agopian | 35aadd6 | 2012-03-08 22:01:51 -0800 | [diff] [blame] | 3924 | dumpAll = false; |
Mathias Agopian | 82d7ab6 | 2012-01-19 18:34:40 -0800 | [diff] [blame] | 3925 | } |
Mathias Agopian | 25e66fc | 2012-01-28 22:31:55 -0800 | [diff] [blame] | 3926 | |
| 3927 | if ((index < numArgs) && |
| 3928 | (args[index] == String16("--latency-clear"))) { |
| 3929 | index++; |
Mathias Agopian | 74d211a | 2013-04-22 16:55:35 +0200 | [diff] [blame] | 3930 | clearStatsLocked(args, index, result); |
Mathias Agopian | 35aadd6 | 2012-03-08 22:01:51 -0800 | [diff] [blame] | 3931 | dumpAll = false; |
Mathias Agopian | 25e66fc | 2012-01-28 22:31:55 -0800 | [diff] [blame] | 3932 | } |
Andy McFadden | c751e92 | 2014-05-08 14:53:26 -0700 | [diff] [blame] | 3933 | |
| 3934 | if ((index < numArgs) && |
| 3935 | (args[index] == String16("--dispsync"))) { |
| 3936 | index++; |
| 3937 | mPrimaryDispSync.dump(result); |
| 3938 | dumpAll = false; |
| 3939 | } |
Dan Stoza | b90cf07 | 2015-03-05 11:05:59 -0800 | [diff] [blame] | 3940 | |
| 3941 | if ((index < numArgs) && |
| 3942 | (args[index] == String16("--static-screen"))) { |
| 3943 | index++; |
| 3944 | dumpStaticScreenStats(result); |
| 3945 | dumpAll = false; |
| 3946 | } |
Pablo Ceballos | 40845df | 2016-01-25 17:41:15 -0800 | [diff] [blame] | 3947 | |
| 3948 | if ((index < numArgs) && |
Brian Anderson | d6927fb | 2016-07-23 23:37:30 -0700 | [diff] [blame] | 3949 | (args[index] == String16("--frame-events"))) { |
Pablo Ceballos | 40845df | 2016-01-25 17:41:15 -0800 | [diff] [blame] | 3950 | index++; |
Brian Anderson | d6927fb | 2016-07-23 23:37:30 -0700 | [diff] [blame] | 3951 | dumpFrameEventsLocked(result); |
Pablo Ceballos | 40845df | 2016-01-25 17:41:15 -0800 | [diff] [blame] | 3952 | dumpAll = false; |
| 3953 | } |
Courtney Goeltzenleuchter | f3b2de1 | 2017-03-27 12:18:12 -0600 | [diff] [blame] | 3954 | |
| 3955 | if ((index < numArgs) && (args[index] == String16("--wide-color"))) { |
| 3956 | index++; |
| 3957 | dumpWideColorInfo(result); |
| 3958 | dumpAll = false; |
| 3959 | } |
Yiwei Zhang | 7124ad3 | 2018-02-21 13:02:45 -0800 | [diff] [blame] | 3960 | |
| 3961 | if ((index < numArgs) && |
| 3962 | (args[index] == String16("--enable-layer-stats"))) { |
| 3963 | index++; |
| 3964 | mLayerStats.enable(); |
| 3965 | dumpAll = false; |
| 3966 | } |
| 3967 | |
| 3968 | if ((index < numArgs) && |
| 3969 | (args[index] == String16("--disable-layer-stats"))) { |
| 3970 | index++; |
| 3971 | mLayerStats.disable(); |
| 3972 | dumpAll = false; |
| 3973 | } |
| 3974 | |
| 3975 | if ((index < numArgs) && |
| 3976 | (args[index] == String16("--clear-layer-stats"))) { |
| 3977 | index++; |
| 3978 | mLayerStats.clear(); |
| 3979 | dumpAll = false; |
| 3980 | } |
| 3981 | |
| 3982 | if ((index < numArgs) && |
| 3983 | (args[index] == String16("--dump-layer-stats"))) { |
| 3984 | index++; |
| 3985 | mLayerStats.dump(result); |
| 3986 | dumpAll = false; |
| 3987 | } |
Dominik Laskowski | e9ef7c4 | 2018-03-12 19:34:30 -0700 | [diff] [blame] | 3988 | |
| 3989 | if ((index < numArgs) && |
| 3990 | (args[index] == String16("--display-identification"))) { |
| 3991 | index++; |
| 3992 | dumpDisplayIdentificationData(result); |
| 3993 | dumpAll = false; |
| 3994 | } |
Yiwei Zhang | faf3ded | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 3995 | |
| 3996 | if ((index < numArgs) && (args[index] == String16("--timestats"))) { |
| 3997 | index++; |
| 3998 | mTimeStats.parseArgs(asProto, args, index, result); |
| 3999 | dumpAll = false; |
| 4000 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4001 | } |
Mathias Agopian | 1b5e102 | 2010-04-20 17:55:49 -0700 | [diff] [blame] | 4002 | |
Mathias Agopian | 82d7ab6 | 2012-01-19 18:34:40 -0800 | [diff] [blame] | 4003 | if (dumpAll) { |
Yiwei Zhang | faf3ded | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 4004 | if (asProto) { |
| 4005 | LayersProto layersProto = dumpProtoInfo(LayerVector::StateSet::Current); |
| 4006 | result.append(layersProto.SerializeAsString().c_str(), layersProto.ByteSize()); |
| 4007 | } else { |
| 4008 | dumpAllLocked(args, index, result); |
| 4009 | } |
Mathias Agopian | 48b888a | 2011-01-19 16:15:53 -0800 | [diff] [blame] | 4010 | } |
| 4011 | |
Mathias Agopian | 9795c42 | 2009-08-26 16:36:26 -0700 | [diff] [blame] | 4012 | if (locked) { |
| 4013 | mStateLock.unlock(); |
| 4014 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4015 | } |
| 4016 | write(fd, result.string(), result.size()); |
| 4017 | return NO_ERROR; |
| 4018 | } |
| 4019 | |
Dan Stoza | c701401 | 2014-02-14 15:03:43 -0800 | [diff] [blame] | 4020 | void SurfaceFlinger::listLayersLocked(const Vector<String16>& /* args */, |
| 4021 | size_t& /* index */, String8& result) const |
Mathias Agopian | 25e66fc | 2012-01-28 22:31:55 -0800 | [diff] [blame] | 4022 | { |
Robert Carr | 2047fae | 2016-11-28 14:09:09 -0800 | [diff] [blame] | 4023 | mCurrentState.traverseInZOrder([&](Layer* layer) { |
Mathias Agopian | 74d211a | 2013-04-22 16:55:35 +0200 | [diff] [blame] | 4024 | result.appendFormat("%s\n", layer->getName().string()); |
Robert Carr | 2047fae | 2016-11-28 14:09:09 -0800 | [diff] [blame] | 4025 | }); |
Mathias Agopian | 25e66fc | 2012-01-28 22:31:55 -0800 | [diff] [blame] | 4026 | } |
| 4027 | |
Mathias Agopian | 82d7ab6 | 2012-01-19 18:34:40 -0800 | [diff] [blame] | 4028 | void SurfaceFlinger::dumpStatsLocked(const Vector<String16>& args, size_t& index, |
Mathias Agopian | 74d211a | 2013-04-22 16:55:35 +0200 | [diff] [blame] | 4029 | String8& result) const |
Mathias Agopian | 82d7ab6 | 2012-01-19 18:34:40 -0800 | [diff] [blame] | 4030 | { |
| 4031 | String8 name; |
| 4032 | if (index < args.size()) { |
| 4033 | name = String8(args[index]); |
| 4034 | index++; |
| 4035 | } |
| 4036 | |
David Sodman | 105b7dc | 2017-11-04 20:28:14 -0700 | [diff] [blame] | 4037 | const auto& activeConfig = getBE().mHwc->getActiveConfig(HWC_DISPLAY_PRIMARY); |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 4038 | const nsecs_t period = activeConfig->getVsyncPeriod(); |
Greg Hackmann | 86efcc0 | 2014-03-07 12:44:02 -0800 | [diff] [blame] | 4039 | result.appendFormat("%" PRId64 "\n", period); |
Jamie Gennis | 4b0eba9 | 2013-02-05 13:30:24 -0800 | [diff] [blame] | 4040 | |
| 4041 | if (name.isEmpty()) { |
Svetoslav | d85084b | 2014-03-20 10:28:31 -0700 | [diff] [blame] | 4042 | mAnimFrameTracker.dumpStats(result); |
Jamie Gennis | 4b0eba9 | 2013-02-05 13:30:24 -0800 | [diff] [blame] | 4043 | } else { |
Robert Carr | 2047fae | 2016-11-28 14:09:09 -0800 | [diff] [blame] | 4044 | mCurrentState.traverseInZOrder([&](Layer* layer) { |
Jamie Gennis | 4b0eba9 | 2013-02-05 13:30:24 -0800 | [diff] [blame] | 4045 | if (name == layer->getName()) { |
Svetoslav | d85084b | 2014-03-20 10:28:31 -0700 | [diff] [blame] | 4046 | layer->dumpFrameStats(result); |
Jamie Gennis | 4b0eba9 | 2013-02-05 13:30:24 -0800 | [diff] [blame] | 4047 | } |
Robert Carr | 2047fae | 2016-11-28 14:09:09 -0800 | [diff] [blame] | 4048 | }); |
Mathias Agopian | 82d7ab6 | 2012-01-19 18:34:40 -0800 | [diff] [blame] | 4049 | } |
| 4050 | } |
| 4051 | |
Mathias Agopian | 25e66fc | 2012-01-28 22:31:55 -0800 | [diff] [blame] | 4052 | void SurfaceFlinger::clearStatsLocked(const Vector<String16>& args, size_t& index, |
Dan Stoza | c701401 | 2014-02-14 15:03:43 -0800 | [diff] [blame] | 4053 | String8& /* result */) |
Mathias Agopian | 25e66fc | 2012-01-28 22:31:55 -0800 | [diff] [blame] | 4054 | { |
| 4055 | String8 name; |
| 4056 | if (index < args.size()) { |
| 4057 | name = String8(args[index]); |
| 4058 | index++; |
| 4059 | } |
| 4060 | |
Robert Carr | 2047fae | 2016-11-28 14:09:09 -0800 | [diff] [blame] | 4061 | mCurrentState.traverseInZOrder([&](Layer* layer) { |
Mathias Agopian | 25e66fc | 2012-01-28 22:31:55 -0800 | [diff] [blame] | 4062 | if (name.isEmpty() || (name == layer->getName())) { |
Svetoslav | d85084b | 2014-03-20 10:28:31 -0700 | [diff] [blame] | 4063 | layer->clearFrameStats(); |
Mathias Agopian | 25e66fc | 2012-01-28 22:31:55 -0800 | [diff] [blame] | 4064 | } |
Robert Carr | 2047fae | 2016-11-28 14:09:09 -0800 | [diff] [blame] | 4065 | }); |
Jamie Gennis | 4b0eba9 | 2013-02-05 13:30:24 -0800 | [diff] [blame] | 4066 | |
Svetoslav | d85084b | 2014-03-20 10:28:31 -0700 | [diff] [blame] | 4067 | mAnimFrameTracker.clearStats(); |
Mathias Agopian | 25e66fc | 2012-01-28 22:31:55 -0800 | [diff] [blame] | 4068 | } |
| 4069 | |
Jamie Gennis | 6547ff4 | 2013-07-16 20:12:42 -0700 | [diff] [blame] | 4070 | // This should only be called from the main thread. Otherwise it would need |
| 4071 | // the lock and should use mCurrentState rather than mDrawingState. |
| 4072 | void SurfaceFlinger::logFrameStats() { |
Robert Carr | 2047fae | 2016-11-28 14:09:09 -0800 | [diff] [blame] | 4073 | mDrawingState.traverseInZOrder([&](Layer* layer) { |
Jamie Gennis | 6547ff4 | 2013-07-16 20:12:42 -0700 | [diff] [blame] | 4074 | layer->logFrameStats(); |
Robert Carr | 2047fae | 2016-11-28 14:09:09 -0800 | [diff] [blame] | 4075 | }); |
Jamie Gennis | 6547ff4 | 2013-07-16 20:12:42 -0700 | [diff] [blame] | 4076 | |
| 4077 | mAnimFrameTracker.logAndResetStats(String8("<win-anim>")); |
| 4078 | } |
| 4079 | |
Fabien Sanglard | 63a5fcd | 2016-12-29 15:13:07 -0800 | [diff] [blame] | 4080 | void SurfaceFlinger::appendSfConfigString(String8& result) const |
Andy McFadden | 4803b74 | 2012-09-24 19:07:20 -0700 | [diff] [blame] | 4081 | { |
Fabien Sanglard | 63a5fcd | 2016-12-29 15:13:07 -0800 | [diff] [blame] | 4082 | result.append(" [sf"); |
Fabien Sanglard | c93afd5 | 2017-03-13 13:02:42 -0700 | [diff] [blame] | 4083 | |
Fabien Sanglard | 63a5fcd | 2016-12-29 15:13:07 -0800 | [diff] [blame] | 4084 | if (isLayerTripleBufferingDisabled()) |
| 4085 | result.append(" DISABLE_TRIPLE_BUFFERING"); |
Fabien Sanglard | c45a7d9 | 2017-03-14 13:24:22 -0700 | [diff] [blame] | 4086 | |
| 4087 | result.appendFormat(" PRESENT_TIME_OFFSET=%" PRId64 , dispSyncPresentTimeOffset); |
Fabien Sanglard | a34ed63 | 2017-03-14 11:43:52 -0700 | [diff] [blame] | 4088 | result.appendFormat(" FORCE_HWC_FOR_RBG_TO_YUV=%d", useHwcForRgbToYuv); |
Fabien Sanglard | c8e387e | 2017-03-10 10:30:28 -0800 | [diff] [blame] | 4089 | result.appendFormat(" MAX_VIRT_DISPLAY_DIM=%" PRIu64, maxVirtualDisplaySize); |
Fabien Sanglard | cbf153b | 2017-03-10 17:57:12 -0800 | [diff] [blame] | 4090 | result.appendFormat(" RUNNING_WITHOUT_SYNC_FRAMEWORK=%d", !hasSyncFramework); |
Fabien Sanglard | 1971b63 | 2017-03-10 14:50:03 -0800 | [diff] [blame] | 4091 | result.appendFormat(" NUM_FRAMEBUFFER_SURFACE_BUFFERS=%" PRId64, |
| 4092 | maxFrameBufferAcquiredBuffers); |
Fabien Sanglard | 63a5fcd | 2016-12-29 15:13:07 -0800 | [diff] [blame] | 4093 | result.append("]"); |
Andy McFadden | 4803b74 | 2012-09-24 19:07:20 -0700 | [diff] [blame] | 4094 | } |
| 4095 | |
Dan Stoza | b90cf07 | 2015-03-05 11:05:59 -0800 | [diff] [blame] | 4096 | void SurfaceFlinger::dumpStaticScreenStats(String8& result) const |
| 4097 | { |
| 4098 | result.appendFormat("Static screen stats:\n"); |
David Sodman | 4a36e93 | 2017-11-07 14:29:47 -0800 | [diff] [blame] | 4099 | for (size_t b = 0; b < SurfaceFlingerBE::NUM_BUCKETS - 1; ++b) { |
| 4100 | float bucketTimeSec = getBE().mFrameBuckets[b] / 1e9; |
Dan Stoza | b90cf07 | 2015-03-05 11:05:59 -0800 | [diff] [blame] | 4101 | float percent = 100.0f * |
David Sodman | 4a36e93 | 2017-11-07 14:29:47 -0800 | [diff] [blame] | 4102 | static_cast<float>(getBE().mFrameBuckets[b]) / getBE().mTotalTime; |
Dan Stoza | b90cf07 | 2015-03-05 11:05:59 -0800 | [diff] [blame] | 4103 | result.appendFormat(" < %zd frames: %.3f s (%.1f%%)\n", |
| 4104 | b + 1, bucketTimeSec, percent); |
| 4105 | } |
David Sodman | 4a36e93 | 2017-11-07 14:29:47 -0800 | [diff] [blame] | 4106 | float bucketTimeSec = getBE().mFrameBuckets[SurfaceFlingerBE::NUM_BUCKETS - 1] / 1e9; |
Dan Stoza | b90cf07 | 2015-03-05 11:05:59 -0800 | [diff] [blame] | 4107 | float percent = 100.0f * |
David Sodman | 4a36e93 | 2017-11-07 14:29:47 -0800 | [diff] [blame] | 4108 | static_cast<float>(getBE().mFrameBuckets[SurfaceFlingerBE::NUM_BUCKETS - 1]) / getBE().mTotalTime; |
Dan Stoza | b90cf07 | 2015-03-05 11:05:59 -0800 | [diff] [blame] | 4109 | result.appendFormat(" %zd+ frames: %.3f s (%.1f%%)\n", |
David Sodman | 4a36e93 | 2017-11-07 14:29:47 -0800 | [diff] [blame] | 4110 | SurfaceFlingerBE::NUM_BUCKETS - 1, bucketTimeSec, percent); |
Dan Stoza | b90cf07 | 2015-03-05 11:05:59 -0800 | [diff] [blame] | 4111 | } |
| 4112 | |
Dan Stoza | e77c766 | 2016-05-13 11:37:28 -0700 | [diff] [blame] | 4113 | void SurfaceFlinger::recordBufferingStats(const char* layerName, |
| 4114 | std::vector<OccupancyTracker::Segment>&& history) { |
David Sodman | cbaf083 | 2017-11-07 14:21:36 -0800 | [diff] [blame] | 4115 | Mutex::Autolock lock(getBE().mBufferingStatsMutex); |
| 4116 | auto& stats = getBE().mBufferingStats[layerName]; |
Dan Stoza | e77c766 | 2016-05-13 11:37:28 -0700 | [diff] [blame] | 4117 | for (const auto& segment : history) { |
| 4118 | if (!segment.usedThirdBuffer) { |
| 4119 | stats.twoBufferTime += segment.totalTime; |
| 4120 | } |
| 4121 | if (segment.occupancyAverage < 1.0f) { |
| 4122 | stats.doubleBufferedTime += segment.totalTime; |
| 4123 | } else if (segment.occupancyAverage < 2.0f) { |
| 4124 | stats.tripleBufferedTime += segment.totalTime; |
| 4125 | } |
| 4126 | ++stats.numSegments; |
| 4127 | stats.totalTime += segment.totalTime; |
| 4128 | } |
| 4129 | } |
| 4130 | |
Brian Anderson | d6927fb | 2016-07-23 23:37:30 -0700 | [diff] [blame] | 4131 | void SurfaceFlinger::dumpFrameEventsLocked(String8& result) { |
| 4132 | result.appendFormat("Layer frame timestamps:\n"); |
| 4133 | |
| 4134 | const LayerVector& currentLayers = mCurrentState.layersSortedByZ; |
| 4135 | const size_t count = currentLayers.size(); |
| 4136 | for (size_t i=0 ; i<count ; i++) { |
| 4137 | currentLayers[i]->dumpFrameEvents(result); |
| 4138 | } |
| 4139 | } |
| 4140 | |
Dan Stoza | e77c766 | 2016-05-13 11:37:28 -0700 | [diff] [blame] | 4141 | void SurfaceFlinger::dumpBufferingStats(String8& result) const { |
| 4142 | result.append("Buffering stats:\n"); |
| 4143 | result.append(" [Layer name] <Active time> <Two buffer> " |
| 4144 | "<Double buffered> <Triple buffered>\n"); |
David Sodman | cbaf083 | 2017-11-07 14:21:36 -0800 | [diff] [blame] | 4145 | Mutex::Autolock lock(getBE().mBufferingStatsMutex); |
Dan Stoza | e77c766 | 2016-05-13 11:37:28 -0700 | [diff] [blame] | 4146 | typedef std::tuple<std::string, float, float, float> BufferTuple; |
| 4147 | std::map<float, BufferTuple, std::greater<float>> sorted; |
David Sodman | cbaf083 | 2017-11-07 14:21:36 -0800 | [diff] [blame] | 4148 | for (const auto& statsPair : getBE().mBufferingStats) { |
Dan Stoza | e77c766 | 2016-05-13 11:37:28 -0700 | [diff] [blame] | 4149 | const char* name = statsPair.first.c_str(); |
David Sodman | cbaf083 | 2017-11-07 14:21:36 -0800 | [diff] [blame] | 4150 | const SurfaceFlingerBE::BufferingStats& stats = statsPair.second; |
Dan Stoza | e77c766 | 2016-05-13 11:37:28 -0700 | [diff] [blame] | 4151 | if (stats.numSegments == 0) { |
| 4152 | continue; |
| 4153 | } |
| 4154 | float activeTime = ns2ms(stats.totalTime) / 1000.0f; |
| 4155 | float twoBufferRatio = static_cast<float>(stats.twoBufferTime) / |
| 4156 | stats.totalTime; |
| 4157 | float doubleBufferRatio = static_cast<float>( |
| 4158 | stats.doubleBufferedTime) / stats.totalTime; |
| 4159 | float tripleBufferRatio = static_cast<float>( |
| 4160 | stats.tripleBufferedTime) / stats.totalTime; |
| 4161 | sorted.insert({activeTime, {name, twoBufferRatio, |
| 4162 | doubleBufferRatio, tripleBufferRatio}}); |
| 4163 | } |
| 4164 | for (const auto& sortedPair : sorted) { |
| 4165 | float activeTime = sortedPair.first; |
| 4166 | const BufferTuple& values = sortedPair.second; |
| 4167 | result.appendFormat(" [%s] %.2f %.3f %.3f %.3f\n", |
| 4168 | std::get<0>(values).c_str(), activeTime, |
| 4169 | std::get<1>(values), std::get<2>(values), |
| 4170 | std::get<3>(values)); |
| 4171 | } |
| 4172 | result.append("\n"); |
| 4173 | } |
| 4174 | |
Dominik Laskowski | e9ef7c4 | 2018-03-12 19:34:30 -0700 | [diff] [blame] | 4175 | void SurfaceFlinger::dumpDisplayIdentificationData(String8& result) const { |
| 4176 | for (size_t d = 0; d < mDisplays.size(); d++) { |
| 4177 | const sp<const DisplayDevice>& displayDevice(mDisplays[d]); |
| 4178 | const int32_t hwcId = displayDevice->getHwcDisplayId(); |
| 4179 | const auto displayId = getHwComposer().getHwcDisplayId(hwcId); |
| 4180 | if (!displayId) { |
| 4181 | continue; |
| 4182 | } |
| 4183 | |
| 4184 | result.appendFormat("Display %d: ", hwcId); |
| 4185 | uint8_t port; |
| 4186 | DisplayIdentificationData data; |
| 4187 | if (!getHwComposer().getDisplayIdentificationData(*displayId, &port, &data)) { |
| 4188 | result.append("no identification data\n"); |
| 4189 | continue; |
| 4190 | } |
| 4191 | |
| 4192 | if (!isEdid(data)) { |
| 4193 | result.append("unknown identification data: "); |
| 4194 | for (uint8_t byte : data) { |
| 4195 | result.appendFormat("%x ", byte); |
| 4196 | } |
| 4197 | result.append("\n"); |
| 4198 | continue; |
| 4199 | } |
| 4200 | |
| 4201 | const auto edid = parseEdid(data); |
| 4202 | if (!edid) { |
| 4203 | result.append("invalid EDID: "); |
| 4204 | for (uint8_t byte : data) { |
| 4205 | result.appendFormat("%x ", byte); |
| 4206 | } |
| 4207 | result.append("\n"); |
| 4208 | continue; |
| 4209 | } |
| 4210 | |
| 4211 | result.appendFormat("port=%u pnpId=%s displayName=\"", port, edid->pnpId.data()); |
| 4212 | result.append(edid->displayName.data(), edid->displayName.length()); |
| 4213 | result.append("\"\n"); |
| 4214 | } |
| 4215 | result.append("\n"); |
| 4216 | } |
| 4217 | |
Courtney Goeltzenleuchter | f3b2de1 | 2017-03-27 12:18:12 -0600 | [diff] [blame] | 4218 | void SurfaceFlinger::dumpWideColorInfo(String8& result) const { |
| 4219 | result.appendFormat("hasWideColorDisplay: %d\n", hasWideColorDisplay); |
Peiyong Lin | dd9b2ae | 2018-03-01 16:22:45 -0800 | [diff] [blame] | 4220 | result.appendFormat("DisplayColorSetting: %d\n", mDisplayColorSetting); |
Courtney Goeltzenleuchter | f3b2de1 | 2017-03-27 12:18:12 -0600 | [diff] [blame] | 4221 | |
| 4222 | // TODO: print out if wide-color mode is active or not |
| 4223 | |
| 4224 | for (size_t d = 0; d < mDisplays.size(); d++) { |
| 4225 | const sp<const DisplayDevice>& displayDevice(mDisplays[d]); |
| 4226 | int32_t hwcId = displayDevice->getHwcDisplayId(); |
| 4227 | if (hwcId == DisplayDevice::DISPLAY_ID_INVALID) { |
| 4228 | continue; |
| 4229 | } |
| 4230 | |
| 4231 | result.appendFormat("Display %d color modes:\n", hwcId); |
Peiyong Lin | a52f029 | 2018-03-14 17:26:31 -0700 | [diff] [blame] | 4232 | std::vector<ColorMode> modes = getHwComposer().getColorModes(hwcId); |
Courtney Goeltzenleuchter | f3b2de1 | 2017-03-27 12:18:12 -0600 | [diff] [blame] | 4233 | for (auto&& mode : modes) { |
| 4234 | result.appendFormat(" %s (%d)\n", decodeColorMode(mode).c_str(), mode); |
| 4235 | } |
| 4236 | |
Peiyong Lin | a52f029 | 2018-03-14 17:26:31 -0700 | [diff] [blame] | 4237 | ColorMode currentMode = displayDevice->getActiveColorMode(); |
Courtney Goeltzenleuchter | f3b2de1 | 2017-03-27 12:18:12 -0600 | [diff] [blame] | 4238 | result.appendFormat(" Current color mode: %s (%d)\n", |
| 4239 | decodeColorMode(currentMode).c_str(), currentMode); |
| 4240 | } |
| 4241 | result.append("\n"); |
| 4242 | } |
| 4243 | |
Jorim Jaggi | 8e0af36 | 2017-11-14 16:28:28 +0100 | [diff] [blame] | 4244 | LayersProto SurfaceFlinger::dumpProtoInfo(LayerVector::StateSet stateSet) const { |
chaviw | 1d04428 | 2017-09-27 12:19:28 -0700 | [diff] [blame] | 4245 | LayersProto layersProto; |
Jorim Jaggi | 8e0af36 | 2017-11-14 16:28:28 +0100 | [diff] [blame] | 4246 | const bool useDrawing = stateSet == LayerVector::StateSet::Drawing; |
| 4247 | const State& state = useDrawing ? mDrawingState : mCurrentState; |
| 4248 | state.traverseInZOrder([&](Layer* layer) { |
chaviw | 1d04428 | 2017-09-27 12:19:28 -0700 | [diff] [blame] | 4249 | LayerProto* layerProto = layersProto.add_layers(); |
Jorim Jaggi | 8e0af36 | 2017-11-14 16:28:28 +0100 | [diff] [blame] | 4250 | layer->writeToProto(layerProto, stateSet); |
chaviw | 1d04428 | 2017-09-27 12:19:28 -0700 | [diff] [blame] | 4251 | }); |
| 4252 | |
| 4253 | return layersProto; |
| 4254 | } |
| 4255 | |
Yiwei Zhang | 7124ad3 | 2018-02-21 13:02:45 -0800 | [diff] [blame] | 4256 | LayersProto SurfaceFlinger::dumpVisibleLayersProtoInfo(int32_t hwcId) const { |
| 4257 | LayersProto layersProto; |
Yiwei Zhang | 7124ad3 | 2018-02-21 13:02:45 -0800 | [diff] [blame] | 4258 | const sp<DisplayDevice>& displayDevice(mDisplays[hwcId]); |
Yiwei Zhang | 60d1a19 | 2018-03-07 14:52:28 -0800 | [diff] [blame] | 4259 | |
Yiwei Zhang | 7124ad3 | 2018-02-21 13:02:45 -0800 | [diff] [blame] | 4260 | SizeProto* resolution = layersProto.mutable_resolution(); |
| 4261 | resolution->set_w(displayDevice->getWidth()); |
| 4262 | resolution->set_h(displayDevice->getHeight()); |
| 4263 | |
Yiwei Zhang | 60d1a19 | 2018-03-07 14:52:28 -0800 | [diff] [blame] | 4264 | layersProto.set_color_mode(decodeColorMode(displayDevice->getActiveColorMode())); |
| 4265 | layersProto.set_color_transform(decodeColorTransform(displayDevice->getColorTransform())); |
| 4266 | layersProto.set_global_transform( |
| 4267 | static_cast<int32_t>(displayDevice->getOrientationTransform())); |
| 4268 | |
Yiwei Zhang | 7124ad3 | 2018-02-21 13:02:45 -0800 | [diff] [blame] | 4269 | mDrawingState.traverseInZOrder([&](Layer* layer) { |
Yiwei Zhang | 60d1a19 | 2018-03-07 14:52:28 -0800 | [diff] [blame] | 4270 | if (!layer->visibleRegion.isEmpty() && layer->getBE().mHwcLayers.count(hwcId)) { |
Yiwei Zhang | 7124ad3 | 2018-02-21 13:02:45 -0800 | [diff] [blame] | 4271 | LayerProto* layerProto = layersProto.add_layers(); |
| 4272 | layer->writeToProto(layerProto, hwcId); |
| 4273 | } |
| 4274 | }); |
| 4275 | |
| 4276 | return layersProto; |
| 4277 | } |
| 4278 | |
Mathias Agopian | 74d211a | 2013-04-22 16:55:35 +0200 | [diff] [blame] | 4279 | void SurfaceFlinger::dumpAllLocked(const Vector<String16>& args, size_t& index, |
| 4280 | String8& result) const |
Mathias Agopian | 82d7ab6 | 2012-01-19 18:34:40 -0800 | [diff] [blame] | 4281 | { |
Mathias Agopian | 3e25fd8 | 2013-04-22 17:52:16 +0200 | [diff] [blame] | 4282 | bool colorize = false; |
| 4283 | if (index < args.size() |
| 4284 | && (args[index] == String16("--color"))) { |
| 4285 | colorize = true; |
| 4286 | index++; |
| 4287 | } |
| 4288 | |
| 4289 | Colorizer colorizer(colorize); |
| 4290 | |
Mathias Agopian | 82d7ab6 | 2012-01-19 18:34:40 -0800 | [diff] [blame] | 4291 | // figure out if we're stuck somewhere |
| 4292 | const nsecs_t now = systemTime(); |
| 4293 | const nsecs_t inSwapBuffers(mDebugInSwapBuffers); |
| 4294 | const nsecs_t inTransaction(mDebugInTransaction); |
| 4295 | nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0; |
| 4296 | nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0; |
| 4297 | |
| 4298 | /* |
Andy McFadden | 4803b74 | 2012-09-24 19:07:20 -0700 | [diff] [blame] | 4299 | * Dump library configuration. |
| 4300 | */ |
Mathias Agopian | 3e25fd8 | 2013-04-22 17:52:16 +0200 | [diff] [blame] | 4301 | |
| 4302 | colorizer.bold(result); |
Andy McFadden | 4803b74 | 2012-09-24 19:07:20 -0700 | [diff] [blame] | 4303 | result.append("Build configuration:"); |
Mathias Agopian | 3e25fd8 | 2013-04-22 17:52:16 +0200 | [diff] [blame] | 4304 | colorizer.reset(result); |
Andy McFadden | 4803b74 | 2012-09-24 19:07:20 -0700 | [diff] [blame] | 4305 | appendSfConfigString(result); |
| 4306 | appendUiConfigString(result); |
| 4307 | appendGuiConfigString(result); |
| 4308 | result.append("\n"); |
| 4309 | |
Dominik Laskowski | e9ef7c4 | 2018-03-12 19:34:30 -0700 | [diff] [blame] | 4310 | result.append("\nDisplay identification data:\n"); |
| 4311 | dumpDisplayIdentificationData(result); |
| 4312 | |
Courtney Goeltzenleuchter | f3b2de1 | 2017-03-27 12:18:12 -0600 | [diff] [blame] | 4313 | result.append("\nWide-Color information:\n"); |
| 4314 | dumpWideColorInfo(result); |
| 4315 | |
Mathias Agopian | 3e25fd8 | 2013-04-22 17:52:16 +0200 | [diff] [blame] | 4316 | colorizer.bold(result); |
Mathias Agopian | ca08833 | 2013-03-28 17:44:13 -0700 | [diff] [blame] | 4317 | result.append("Sync configuration: "); |
Mathias Agopian | 3e25fd8 | 2013-04-22 17:52:16 +0200 | [diff] [blame] | 4318 | colorizer.reset(result); |
Mathias Agopian | ca08833 | 2013-03-28 17:44:13 -0700 | [diff] [blame] | 4319 | result.append(SyncFeatures::getInstance().toString()); |
| 4320 | result.append("\n"); |
| 4321 | |
David Sodman | 105b7dc | 2017-11-04 20:28:14 -0700 | [diff] [blame] | 4322 | const auto& activeConfig = getBE().mHwc->getActiveConfig(HWC_DISPLAY_PRIMARY); |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 4323 | |
Andy McFadden | 41d67d7 | 2014-04-25 16:58:34 -0700 | [diff] [blame] | 4324 | colorizer.bold(result); |
| 4325 | result.append("DispSync configuration: "); |
| 4326 | colorizer.reset(result); |
Dan Stoza | 84d619e | 2018-03-28 17:07:36 -0700 | [diff] [blame] | 4327 | result.appendFormat("app phase %" PRId64 " ns, sf phase %" PRId64 " ns, early sf phase %" PRId64 |
| 4328 | " ns, present offset %" PRId64 " ns (refresh %" PRId64 " ns)", |
| 4329 | vsyncPhaseOffsetNs, sfVsyncPhaseOffsetNs, mVsyncModulator.getEarlyPhaseOffset(), |
Fabien Sanglard | c45a7d9 | 2017-03-14 13:24:22 -0700 | [diff] [blame] | 4330 | dispSyncPresentTimeOffset, activeConfig->getVsyncPeriod()); |
Andy McFadden | 41d67d7 | 2014-04-25 16:58:34 -0700 | [diff] [blame] | 4331 | result.append("\n"); |
| 4332 | |
Dan Stoza | b90cf07 | 2015-03-05 11:05:59 -0800 | [diff] [blame] | 4333 | // Dump static screen stats |
| 4334 | result.append("\n"); |
| 4335 | dumpStaticScreenStats(result); |
| 4336 | result.append("\n"); |
| 4337 | |
Dan Stoza | e77c766 | 2016-05-13 11:37:28 -0700 | [diff] [blame] | 4338 | dumpBufferingStats(result); |
| 4339 | |
Andy McFadden | 4803b74 | 2012-09-24 19:07:20 -0700 | [diff] [blame] | 4340 | /* |
Mathias Agopian | 82d7ab6 | 2012-01-19 18:34:40 -0800 | [diff] [blame] | 4341 | * Dump the visible layer list |
| 4342 | */ |
Mathias Agopian | 3e25fd8 | 2013-04-22 17:52:16 +0200 | [diff] [blame] | 4343 | colorizer.bold(result); |
Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 4344 | result.appendFormat("Visible layers (count = %zu)\n", mNumLayers); |
Dan Stoza | 0a0158c | 2018-03-16 13:38:54 -0700 | [diff] [blame] | 4345 | result.appendFormat("GraphicBufferProducers: %zu, max %zu\n", |
| 4346 | mGraphicBufferProducerList.size(), mMaxGraphicBufferProducerListSize); |
Mathias Agopian | 3e25fd8 | 2013-04-22 17:52:16 +0200 | [diff] [blame] | 4347 | colorizer.reset(result); |
chaviw | 1d04428 | 2017-09-27 12:19:28 -0700 | [diff] [blame] | 4348 | |
Jorim Jaggi | 8e0af36 | 2017-11-14 16:28:28 +0100 | [diff] [blame] | 4349 | LayersProto layersProto = dumpProtoInfo(LayerVector::StateSet::Current); |
chaviw | 1d04428 | 2017-09-27 12:19:28 -0700 | [diff] [blame] | 4350 | auto layerTree = LayerProtoParser::generateLayerTree(layersProto); |
chaviw | 7ba019b | 2018-03-14 13:28:39 -0700 | [diff] [blame] | 4351 | result.append(LayerProtoParser::layersToString(std::move(layerTree)).c_str()); |
Chia-I Wu | 1e04361 | 2018-03-01 09:45:09 -0800 | [diff] [blame] | 4352 | result.append("\n"); |
Mathias Agopian | 82d7ab6 | 2012-01-19 18:34:40 -0800 | [diff] [blame] | 4353 | |
| 4354 | /* |
Mathias Agopian | 5f20e2d | 2012-08-10 18:50:38 -0700 | [diff] [blame] | 4355 | * Dump Display state |
| 4356 | */ |
| 4357 | |
Mathias Agopian | 3e25fd8 | 2013-04-22 17:52:16 +0200 | [diff] [blame] | 4358 | colorizer.bold(result); |
Greg Hackmann | 86efcc0 | 2014-03-07 12:44:02 -0800 | [diff] [blame] | 4359 | result.appendFormat("Displays (%zu entries)\n", mDisplays.size()); |
Mathias Agopian | 3e25fd8 | 2013-04-22 17:52:16 +0200 | [diff] [blame] | 4360 | colorizer.reset(result); |
Mathias Agopian | 5f20e2d | 2012-08-10 18:50:38 -0700 | [diff] [blame] | 4361 | for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) { |
| 4362 | const sp<const DisplayDevice>& hw(mDisplays[dpy]); |
Mathias Agopian | 74d211a | 2013-04-22 16:55:35 +0200 | [diff] [blame] | 4363 | hw->dump(result); |
Mathias Agopian | 5f20e2d | 2012-08-10 18:50:38 -0700 | [diff] [blame] | 4364 | } |
Chia-I Wu | 1e04361 | 2018-03-01 09:45:09 -0800 | [diff] [blame] | 4365 | result.append("\n"); |
Mathias Agopian | 5f20e2d | 2012-08-10 18:50:38 -0700 | [diff] [blame] | 4366 | |
| 4367 | /* |
Mathias Agopian | 82d7ab6 | 2012-01-19 18:34:40 -0800 | [diff] [blame] | 4368 | * Dump SurfaceFlinger global state |
| 4369 | */ |
| 4370 | |
Mathias Agopian | 3e25fd8 | 2013-04-22 17:52:16 +0200 | [diff] [blame] | 4371 | colorizer.bold(result); |
Mathias Agopian | 74d211a | 2013-04-22 16:55:35 +0200 | [diff] [blame] | 4372 | result.append("SurfaceFlinger global state:\n"); |
Mathias Agopian | 3e25fd8 | 2013-04-22 17:52:16 +0200 | [diff] [blame] | 4373 | colorizer.reset(result); |
Mathias Agopian | 82d7ab6 | 2012-01-19 18:34:40 -0800 | [diff] [blame] | 4374 | |
Mathias Agopian | 888c822 | 2012-08-04 21:10:38 -0700 | [diff] [blame] | 4375 | HWComposer& hwc(getHwComposer()); |
Stephen Kiazyk | 7d3dcb9 | 2017-04-05 16:46:49 -0700 | [diff] [blame] | 4376 | sp<const DisplayDevice> hw(getDefaultDisplayDeviceLocked()); |
Mathias Agopian | ca08833 | 2013-03-28 17:44:13 -0700 | [diff] [blame] | 4377 | |
David Sodman | bc81528 | 2017-11-05 18:57:52 -0800 | [diff] [blame] | 4378 | getBE().mRenderEngine->dump(result); |
Mathias Agopian | 82d7ab6 | 2012-01-19 18:34:40 -0800 | [diff] [blame] | 4379 | |
Lloyd Pique | 2ae2b3b | 2017-12-14 17:18:17 -0800 | [diff] [blame] | 4380 | if (hw) { |
| 4381 | hw->undefinedRegion.dump(result, "undefinedRegion"); |
| 4382 | result.appendFormat(" orientation=%d, isDisplayOn=%d\n", |
| 4383 | hw->getOrientation(), hw->isDisplayOn()); |
| 4384 | } |
Mathias Agopian | 74d211a | 2013-04-22 16:55:35 +0200 | [diff] [blame] | 4385 | result.appendFormat( |
Mathias Agopian | 82d7ab6 | 2012-01-19 18:34:40 -0800 | [diff] [blame] | 4386 | " last eglSwapBuffers() time: %f us\n" |
| 4387 | " last transaction time : %f us\n" |
Mathias Agopian | c95dbdc | 2012-02-05 00:19:27 -0800 | [diff] [blame] | 4388 | " transaction-flags : %08x\n" |
Mathias Agopian | 82d7ab6 | 2012-01-19 18:34:40 -0800 | [diff] [blame] | 4389 | " refresh-rate : %f fps\n" |
| 4390 | " x-dpi : %f\n" |
Mathias Agopian | ed98557 | 2013-03-22 00:24:39 -0700 | [diff] [blame] | 4391 | " y-dpi : %f\n" |
Mathias Agopian | ed98557 | 2013-03-22 00:24:39 -0700 | [diff] [blame] | 4392 | " gpu_to_cpu_unsupported : %d\n" |
| 4393 | , |
Mathias Agopian | 82d7ab6 | 2012-01-19 18:34:40 -0800 | [diff] [blame] | 4394 | mLastSwapBufferTime/1000.0, |
| 4395 | mLastTransactionTime/1000.0, |
Mathias Agopian | c95dbdc | 2012-02-05 00:19:27 -0800 | [diff] [blame] | 4396 | mTransactionFlags, |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 4397 | 1e9 / activeConfig->getVsyncPeriod(), |
| 4398 | activeConfig->getDpiX(), |
| 4399 | activeConfig->getDpiY(), |
Mathias Agopian | ed98557 | 2013-03-22 00:24:39 -0700 | [diff] [blame] | 4400 | !mGpuToCpuSupported); |
Mathias Agopian | 82d7ab6 | 2012-01-19 18:34:40 -0800 | [diff] [blame] | 4401 | |
Mathias Agopian | 74d211a | 2013-04-22 16:55:35 +0200 | [diff] [blame] | 4402 | result.appendFormat(" eglSwapBuffers time: %f us\n", |
Mathias Agopian | 82d7ab6 | 2012-01-19 18:34:40 -0800 | [diff] [blame] | 4403 | inSwapBuffersDuration/1000.0); |
Mathias Agopian | 82d7ab6 | 2012-01-19 18:34:40 -0800 | [diff] [blame] | 4404 | |
Mathias Agopian | 74d211a | 2013-04-22 16:55:35 +0200 | [diff] [blame] | 4405 | result.appendFormat(" transaction time: %f us\n", |
Mathias Agopian | 82d7ab6 | 2012-01-19 18:34:40 -0800 | [diff] [blame] | 4406 | inTransactionDuration/1000.0); |
Mathias Agopian | 82d7ab6 | 2012-01-19 18:34:40 -0800 | [diff] [blame] | 4407 | |
| 4408 | /* |
| 4409 | * VSYNC state |
| 4410 | */ |
Mathias Agopian | 74d211a | 2013-04-22 16:55:35 +0200 | [diff] [blame] | 4411 | mEventThread->dump(result); |
Dan Stoza | e22aec7 | 2016-08-01 13:20:59 -0700 | [diff] [blame] | 4412 | result.append("\n"); |
| 4413 | |
| 4414 | /* |
| 4415 | * HWC layer minidump |
| 4416 | */ |
| 4417 | for (size_t d = 0; d < mDisplays.size(); d++) { |
| 4418 | const sp<const DisplayDevice>& displayDevice(mDisplays[d]); |
| 4419 | int32_t hwcId = displayDevice->getHwcDisplayId(); |
| 4420 | if (hwcId == DisplayDevice::DISPLAY_ID_INVALID) { |
| 4421 | continue; |
| 4422 | } |
| 4423 | |
| 4424 | result.appendFormat("Display %d HWC layers:\n", hwcId); |
| 4425 | Layer::miniDumpHeader(result); |
Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 4426 | mCurrentState.traverseInZOrder([&](Layer* layer) { |
Dan Stoza | e22aec7 | 2016-08-01 13:20:59 -0700 | [diff] [blame] | 4427 | layer->miniDump(result, hwcId); |
Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 4428 | }); |
Dan Stoza | e22aec7 | 2016-08-01 13:20:59 -0700 | [diff] [blame] | 4429 | result.append("\n"); |
| 4430 | } |
Mathias Agopian | 82d7ab6 | 2012-01-19 18:34:40 -0800 | [diff] [blame] | 4431 | |
| 4432 | /* |
| 4433 | * Dump HWComposer state |
| 4434 | */ |
Mathias Agopian | 3e25fd8 | 2013-04-22 17:52:16 +0200 | [diff] [blame] | 4435 | colorizer.bold(result); |
Mathias Agopian | 74d211a | 2013-04-22 16:55:35 +0200 | [diff] [blame] | 4436 | result.append("h/w composer state:\n"); |
Mathias Agopian | 3e25fd8 | 2013-04-22 17:52:16 +0200 | [diff] [blame] | 4437 | colorizer.reset(result); |
Dan Stoza | 9f26a9c | 2016-06-22 14:51:09 -0700 | [diff] [blame] | 4438 | bool hwcDisabled = mDebugDisableHWC || mDebugRegion; |
Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 4439 | result.appendFormat(" h/w composer %s\n", |
| 4440 | hwcDisabled ? "disabled" : "enabled"); |
Mathias Agopian | 74d211a | 2013-04-22 16:55:35 +0200 | [diff] [blame] | 4441 | hwc.dump(result); |
Mathias Agopian | 82d7ab6 | 2012-01-19 18:34:40 -0800 | [diff] [blame] | 4442 | |
| 4443 | /* |
| 4444 | * Dump gralloc state |
| 4445 | */ |
| 4446 | const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get()); |
| 4447 | alloc.dump(result); |
Karthik Ravi Shankar | 171155a | 2017-06-28 15:40:24 -0700 | [diff] [blame] | 4448 | |
| 4449 | /* |
| 4450 | * Dump VrFlinger state if in use. |
| 4451 | */ |
| 4452 | if (mVrFlingerRequestsDisplay && mVrFlinger) { |
| 4453 | result.append("VrFlinger state:\n"); |
| 4454 | result.append(mVrFlinger->Dump().c_str()); |
| 4455 | result.append("\n"); |
| 4456 | } |
Mathias Agopian | 82d7ab6 | 2012-01-19 18:34:40 -0800 | [diff] [blame] | 4457 | } |
| 4458 | |
Mathias Agopian | 13127d8 | 2013-03-05 17:47:11 -0800 | [diff] [blame] | 4459 | const Vector< sp<Layer> >& |
Jesse Hall | 48bc05b | 2013-03-21 14:06:52 -0700 | [diff] [blame] | 4460 | SurfaceFlinger::getLayerSortedByZForHwcDisplay(int id) { |
Mathias Agopian | db9b41f | 2012-10-15 16:51:41 -0700 | [diff] [blame] | 4461 | // Note: mStateLock is held here |
Jesse Hall | 48bc05b | 2013-03-21 14:06:52 -0700 | [diff] [blame] | 4462 | wp<IBinder> dpy; |
| 4463 | for (size_t i=0 ; i<mDisplays.size() ; i++) { |
| 4464 | if (mDisplays.valueAt(i)->getHwcDisplayId() == id) { |
| 4465 | dpy = mDisplays.keyAt(i); |
| 4466 | break; |
| 4467 | } |
| 4468 | } |
Peiyong Lin | 566a3b4 | 2018-01-09 18:22:43 -0800 | [diff] [blame] | 4469 | if (dpy == nullptr) { |
Jesse Hall | 48bc05b | 2013-03-21 14:06:52 -0700 | [diff] [blame] | 4470 | ALOGE("getLayerSortedByZForHwcDisplay: invalid hwc display id %d", id); |
| 4471 | // Just use the primary display so we have something to return |
| 4472 | dpy = getBuiltInDisplay(DisplayDevice::DISPLAY_PRIMARY); |
| 4473 | } |
Stephen Kiazyk | 7d3dcb9 | 2017-04-05 16:46:49 -0700 | [diff] [blame] | 4474 | return getDisplayDeviceLocked(dpy)->getVisibleLayersSortedByZ(); |
Mathias Agopian | cb55857 | 2012-10-04 15:58:54 -0700 | [diff] [blame] | 4475 | } |
| 4476 | |
Keun young Park | 63f165f | 2012-08-31 10:53:36 -0700 | [diff] [blame] | 4477 | bool SurfaceFlinger::startDdmConnection() |
| 4478 | { |
| 4479 | void* libddmconnection_dso = |
| 4480 | dlopen("libsurfaceflinger_ddmconnection.so", RTLD_NOW); |
| 4481 | if (!libddmconnection_dso) { |
| 4482 | return false; |
| 4483 | } |
| 4484 | void (*DdmConnection_start)(const char* name); |
| 4485 | DdmConnection_start = |
Jesse Hall | 24cd98e | 2014-07-13 14:37:16 -0700 | [diff] [blame] | 4486 | (decltype(DdmConnection_start))dlsym(libddmconnection_dso, "DdmConnection_start"); |
Keun young Park | 63f165f | 2012-08-31 10:53:36 -0700 | [diff] [blame] | 4487 | if (!DdmConnection_start) { |
| 4488 | dlclose(libddmconnection_dso); |
| 4489 | return false; |
| 4490 | } |
| 4491 | (*DdmConnection_start)(getServiceName()); |
| 4492 | return true; |
| 4493 | } |
| 4494 | |
Chia-I Wu | 28f320b | 2018-05-03 11:02:56 -0700 | [diff] [blame] | 4495 | void SurfaceFlinger::updateColorMatrixLocked() { |
| 4496 | mat4 colorMatrix; |
| 4497 | if (mGlobalSaturationFactor != 1.0f) { |
| 4498 | // Rec.709 luma coefficients |
| 4499 | float3 luminance{0.213f, 0.715f, 0.072f}; |
| 4500 | luminance *= 1.0f - mGlobalSaturationFactor; |
| 4501 | mat4 saturationMatrix = mat4( |
| 4502 | vec4{luminance.r + mGlobalSaturationFactor, luminance.r, luminance.r, 0.0f}, |
| 4503 | vec4{luminance.g, luminance.g + mGlobalSaturationFactor, luminance.g, 0.0f}, |
| 4504 | vec4{luminance.b, luminance.b, luminance.b + mGlobalSaturationFactor, 0.0f}, |
| 4505 | vec4{0.0f, 0.0f, 0.0f, 1.0f} |
| 4506 | ); |
| 4507 | colorMatrix = mClientColorMatrix * saturationMatrix * mDaltonizer(); |
| 4508 | } else { |
| 4509 | colorMatrix = mClientColorMatrix * mDaltonizer(); |
| 4510 | } |
| 4511 | |
| 4512 | if (mCurrentState.colorMatrix != colorMatrix) { |
| 4513 | mCurrentState.colorMatrix = colorMatrix; |
| 4514 | mCurrentState.colorMatrixChanged = true; |
| 4515 | setTransactionFlags(eTransactionNeeded); |
| 4516 | } |
| 4517 | } |
| 4518 | |
Fabien Sanglard | 6e8e98a | 2016-10-27 20:04:11 -0700 | [diff] [blame] | 4519 | status_t SurfaceFlinger::CheckTransactCodeCredentials(uint32_t code) { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4520 | switch (code) { |
| 4521 | case CREATE_CONNECTION: |
Mathias Agopian | 041a075 | 2013-03-15 18:31:56 -0700 | [diff] [blame] | 4522 | case CREATE_DISPLAY: |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4523 | case BOOT_FINISHED: |
Svetoslav | d85084b | 2014-03-20 10:28:31 -0700 | [diff] [blame] | 4524 | case CLEAR_ANIMATION_FRAME_STATS: |
| 4525 | case GET_ANIMATION_FRAME_STATS: |
Prashant Malani | 2c9b11f | 2014-05-25 01:36:31 -0700 | [diff] [blame] | 4526 | case SET_POWER_MODE: |
Dan Stoza | c4f471e | 2016-03-24 09:31:08 -0700 | [diff] [blame] | 4527 | case GET_HDR_CAPABILITIES: |
Chia-I Wu | 90f669f | 2017-10-05 14:24:41 -0700 | [diff] [blame] | 4528 | case ENABLE_VSYNC_INJECTIONS: |
| 4529 | case INJECT_VSYNC: |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4530 | { |
| 4531 | // codes that require permission check |
| 4532 | IPCThreadState* ipc = IPCThreadState::self(); |
| 4533 | const int pid = ipc->getCallingPid(); |
Mathias Agopian | a1ecca9 | 2009-05-21 19:21:59 -0700 | [diff] [blame] | 4534 | const int uid = ipc->getCallingUid(); |
Jeff Brown | 3bfe51d | 2015-04-10 20:20:13 -0700 | [diff] [blame] | 4535 | if ((uid != AID_GRAPHICS && uid != AID_SYSTEM) && |
Mathias Agopian | 99b4984 | 2011-06-27 16:05:52 -0700 | [diff] [blame] | 4536 | !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) { |
Fabien Sanglard | 6e8e98a | 2016-10-27 20:04:11 -0700 | [diff] [blame] | 4537 | ALOGE("Permission Denial: can't access SurfaceFlinger pid=%d, uid=%d", pid, uid); |
Mathias Agopian | 375f563 | 2009-06-15 18:24:59 -0700 | [diff] [blame] | 4538 | return PERMISSION_DENIED; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4539 | } |
Mathias Agopian | 1b0b30d | 2010-09-24 11:26:58 -0700 | [diff] [blame] | 4540 | break; |
| 4541 | } |
Robert Carr | 1db73f6 | 2016-12-21 12:58:51 -0800 | [diff] [blame] | 4542 | /* |
| 4543 | * Calling setTransactionState is safe, because you need to have been |
| 4544 | * granted a reference to Client* and Handle* to do anything with it. |
| 4545 | * |
| 4546 | * Creating a scoped connection is safe, as per discussion in ISurfaceComposer.h |
| 4547 | */ |
| 4548 | case SET_TRANSACTION_STATE: |
| 4549 | case CREATE_SCOPED_CONNECTION: |
| 4550 | { |
| 4551 | return OK; |
| 4552 | } |
Mathias Agopian | 1b0b30d | 2010-09-24 11:26:58 -0700 | [diff] [blame] | 4553 | case CAPTURE_SCREEN: |
| 4554 | { |
| 4555 | // codes that require permission check |
| 4556 | IPCThreadState* ipc = IPCThreadState::self(); |
| 4557 | const int pid = ipc->getCallingPid(); |
| 4558 | const int uid = ipc->getCallingUid(); |
Mathias Agopian | 99b4984 | 2011-06-27 16:05:52 -0700 | [diff] [blame] | 4559 | if ((uid != AID_GRAPHICS) && |
| 4560 | !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) { |
Fabien Sanglard | 6e8e98a | 2016-10-27 20:04:11 -0700 | [diff] [blame] | 4561 | ALOGE("Permission Denial: can't read framebuffer pid=%d, uid=%d", pid, uid); |
Mathias Agopian | 1b0b30d | 2010-09-24 11:26:58 -0700 | [diff] [blame] | 4562 | return PERMISSION_DENIED; |
| 4563 | } |
| 4564 | break; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4565 | } |
chaviw | a76b271 | 2017-09-20 12:02:26 -0700 | [diff] [blame] | 4566 | case CAPTURE_LAYERS: { |
| 4567 | IPCThreadState* ipc = IPCThreadState::self(); |
| 4568 | const int pid = ipc->getCallingPid(); |
| 4569 | const int uid = ipc->getCallingUid(); |
| 4570 | if ((uid != AID_GRAPHICS) && |
| 4571 | !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) { |
| 4572 | ALOGE("Permission Denial: can't read framebuffer pid=%d, uid=%d", pid, uid); |
| 4573 | return PERMISSION_DENIED; |
| 4574 | } |
| 4575 | break; |
| 4576 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4577 | } |
Fabien Sanglard | 6e8e98a | 2016-10-27 20:04:11 -0700 | [diff] [blame] | 4578 | return OK; |
| 4579 | } |
| 4580 | |
| 4581 | status_t SurfaceFlinger::onTransact( |
| 4582 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) |
| 4583 | { |
| 4584 | status_t credentialCheck = CheckTransactCodeCredentials(code); |
| 4585 | if (credentialCheck != OK) { |
| 4586 | return credentialCheck; |
| 4587 | } |
Mathias Agopian | 1b0b30d | 2010-09-24 11:26:58 -0700 | [diff] [blame] | 4588 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4589 | status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags); |
| 4590 | if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) { |
Mathias Agopian | b8a5560 | 2009-06-26 19:06:36 -0700 | [diff] [blame] | 4591 | CHECK_INTERFACE(ISurfaceComposer, data, reply); |
Romain Guy | 8c6bbd6 | 2017-06-05 13:51:43 -0700 | [diff] [blame] | 4592 | IPCThreadState* ipc = IPCThreadState::self(); |
| 4593 | const int uid = ipc->getCallingUid(); |
| 4594 | if (CC_UNLIKELY(uid != AID_SYSTEM |
| 4595 | && !PermissionCache::checkCallingPermission(sHardwareTest))) { |
Mathias Agopian | 375f563 | 2009-06-15 18:24:59 -0700 | [diff] [blame] | 4596 | const int pid = ipc->getCallingPid(); |
Steve Block | e6f43dd | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 4597 | ALOGE("Permission Denial: " |
Mathias Agopian | 375f563 | 2009-06-15 18:24:59 -0700 | [diff] [blame] | 4598 | "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4599 | return PERMISSION_DENIED; |
| 4600 | } |
| 4601 | int n; |
| 4602 | switch (code) { |
Mathias Agopian | 01b7668 | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 4603 | case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE |
Mathias Agopian | 35b48d1 | 2010-09-13 22:57:58 -0700 | [diff] [blame] | 4604 | case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4605 | return NO_ERROR; |
| 4606 | case 1002: // SHOW_UPDATES |
| 4607 | n = data.readInt32(); |
| 4608 | mDebugRegion = n ? n : (mDebugRegion ? 0 : 1); |
Mathias Agopian | 53331da | 2011-08-22 21:44:41 -0700 | [diff] [blame] | 4609 | invalidateHwcGeometry(); |
| 4610 | repaintEverything(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4611 | return NO_ERROR; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4612 | case 1004:{ // repaint everything |
Mathias Agopian | 53331da | 2011-08-22 21:44:41 -0700 | [diff] [blame] | 4613 | repaintEverything(); |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 4614 | return NO_ERROR; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4615 | } |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 4616 | case 1005:{ // force transaction |
Steven Thomas | 6d8110b | 2017-08-31 18:24:21 -0700 | [diff] [blame] | 4617 | Mutex::Autolock _l(mStateLock); |
Mathias Agopian | e57f292 | 2012-08-09 16:29:12 -0700 | [diff] [blame] | 4618 | setTransactionFlags( |
| 4619 | eTransactionNeeded| |
| 4620 | eDisplayTransactionNeeded| |
| 4621 | eTraversalNeeded); |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 4622 | return NO_ERROR; |
| 4623 | } |
Mathias Agopian | 4d143ee | 2012-02-23 20:05:39 -0800 | [diff] [blame] | 4624 | case 1006:{ // send empty update |
| 4625 | signalRefresh(); |
| 4626 | return NO_ERROR; |
| 4627 | } |
Mathias Agopian | 53331da | 2011-08-22 21:44:41 -0700 | [diff] [blame] | 4628 | case 1008: // toggle use of hw composer |
| 4629 | n = data.readInt32(); |
| 4630 | mDebugDisableHWC = n ? 1 : 0; |
| 4631 | invalidateHwcGeometry(); |
| 4632 | repaintEverything(); |
| 4633 | return NO_ERROR; |
Mathias Agopian | a458364 | 2011-08-23 18:03:18 -0700 | [diff] [blame] | 4634 | case 1009: // toggle use of transform hint |
| 4635 | n = data.readInt32(); |
| 4636 | mDebugDisableTransformHint = n ? 1 : 0; |
| 4637 | invalidateHwcGeometry(); |
| 4638 | repaintEverything(); |
| 4639 | return NO_ERROR; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4640 | case 1010: // interrogate. |
Mathias Agopian | 01b7668 | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 4641 | reply->writeInt32(0); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4642 | reply->writeInt32(0); |
| 4643 | reply->writeInt32(mDebugRegion); |
Mathias Agopian | b9494d5 | 2012-04-18 02:28:45 -0700 | [diff] [blame] | 4644 | reply->writeInt32(0); |
Dianne Hackborn | 12839be | 2012-02-06 21:21:05 -0800 | [diff] [blame] | 4645 | reply->writeInt32(mDebugDisableHWC); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4646 | return NO_ERROR; |
| 4647 | case 1013: { |
Tomasz Wasilczyk | 8722a31 | 2017-04-13 19:14:30 +0000 | [diff] [blame] | 4648 | sp<const DisplayDevice> hw(getDefaultDisplayDevice()); |
Mathias Agopian | 4297734 | 2012-08-05 00:40:46 -0700 | [diff] [blame] | 4649 | reply->writeInt32(hw->getPageFlipCount()); |
Mathias Agopian | ff2ed70 | 2013-09-01 21:36:12 -0700 | [diff] [blame] | 4650 | return NO_ERROR; |
| 4651 | } |
| 4652 | case 1014: { |
Chia-I Wu | 28f320b | 2018-05-03 11:02:56 -0700 | [diff] [blame] | 4653 | Mutex::Autolock _l(mStateLock); |
Mathias Agopian | ff2ed70 | 2013-09-01 21:36:12 -0700 | [diff] [blame] | 4654 | // daltonize |
| 4655 | n = data.readInt32(); |
| 4656 | switch (n % 10) { |
Dan Stoza | 9f26a9c | 2016-06-22 14:51:09 -0700 | [diff] [blame] | 4657 | case 1: |
| 4658 | mDaltonizer.setType(ColorBlindnessType::Protanomaly); |
| 4659 | break; |
| 4660 | case 2: |
| 4661 | mDaltonizer.setType(ColorBlindnessType::Deuteranomaly); |
| 4662 | break; |
| 4663 | case 3: |
| 4664 | mDaltonizer.setType(ColorBlindnessType::Tritanomaly); |
| 4665 | break; |
| 4666 | default: |
| 4667 | mDaltonizer.setType(ColorBlindnessType::None); |
| 4668 | break; |
Mathias Agopian | ff2ed70 | 2013-09-01 21:36:12 -0700 | [diff] [blame] | 4669 | } |
| 4670 | if (n >= 10) { |
Dan Stoza | 9f26a9c | 2016-06-22 14:51:09 -0700 | [diff] [blame] | 4671 | mDaltonizer.setMode(ColorBlindnessMode::Correction); |
Mathias Agopian | ff2ed70 | 2013-09-01 21:36:12 -0700 | [diff] [blame] | 4672 | } else { |
Dan Stoza | 9f26a9c | 2016-06-22 14:51:09 -0700 | [diff] [blame] | 4673 | mDaltonizer.setMode(ColorBlindnessMode::Simulation); |
Mathias Agopian | ff2ed70 | 2013-09-01 21:36:12 -0700 | [diff] [blame] | 4674 | } |
Chia-I Wu | 28f320b | 2018-05-03 11:02:56 -0700 | [diff] [blame] | 4675 | |
| 4676 | updateColorMatrixLocked(); |
Alan Viverette | 9c5a333 | 2013-09-12 20:04:35 -0700 | [diff] [blame] | 4677 | return NO_ERROR; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4678 | } |
Alan Viverette | 9c5a333 | 2013-09-12 20:04:35 -0700 | [diff] [blame] | 4679 | case 1015: { |
Chia-I Wu | 28f320b | 2018-05-03 11:02:56 -0700 | [diff] [blame] | 4680 | Mutex::Autolock _l(mStateLock); |
Alan Viverette | 9c5a333 | 2013-09-12 20:04:35 -0700 | [diff] [blame] | 4681 | // apply a color matrix |
| 4682 | n = data.readInt32(); |
Alan Viverette | 9c5a333 | 2013-09-12 20:04:35 -0700 | [diff] [blame] | 4683 | if (n) { |
Romain Guy | 0147a17 | 2017-06-01 13:53:56 -0700 | [diff] [blame] | 4684 | // color matrix is sent as a column-major mat4 matrix |
Alan Viverette | 794c5ba | 2013-10-03 16:40:52 -0700 | [diff] [blame] | 4685 | for (size_t i = 0 ; i < 4; i++) { |
Dan Stoza | 9f26a9c | 2016-06-22 14:51:09 -0700 | [diff] [blame] | 4686 | for (size_t j = 0; j < 4; j++) { |
Chia-I Wu | 28f320b | 2018-05-03 11:02:56 -0700 | [diff] [blame] | 4687 | mClientColorMatrix[i][j] = data.readFloat(); |
Dan Stoza | 9f26a9c | 2016-06-22 14:51:09 -0700 | [diff] [blame] | 4688 | } |
Alan Viverette | 9c5a333 | 2013-09-12 20:04:35 -0700 | [diff] [blame] | 4689 | } |
Alan Viverette | 9c5a333 | 2013-09-12 20:04:35 -0700 | [diff] [blame] | 4690 | } else { |
Chia-I Wu | 28f320b | 2018-05-03 11:02:56 -0700 | [diff] [blame] | 4691 | mClientColorMatrix = mat4(); |
Alan Viverette | 9c5a333 | 2013-09-12 20:04:35 -0700 | [diff] [blame] | 4692 | } |
Romain Guy | 88d37dd | 2017-05-26 17:57:05 -0700 | [diff] [blame] | 4693 | |
| 4694 | // Check that supplied matrix's last row is {0,0,0,1} so we can avoid |
| 4695 | // the division by w in the fragment shader |
Chia-I Wu | 28f320b | 2018-05-03 11:02:56 -0700 | [diff] [blame] | 4696 | float4 lastRow(transpose(mClientColorMatrix)[3]); |
Romain Guy | 88d37dd | 2017-05-26 17:57:05 -0700 | [diff] [blame] | 4697 | if (any(greaterThan(abs(lastRow - float4{0, 0, 0, 1}), float4{1e-4f}))) { |
| 4698 | ALOGE("The color transform's last row must be (0, 0, 0, 1)"); |
| 4699 | } |
| 4700 | |
Chia-I Wu | 28f320b | 2018-05-03 11:02:56 -0700 | [diff] [blame] | 4701 | updateColorMatrixLocked(); |
Alan Viverette | 9c5a333 | 2013-09-12 20:04:35 -0700 | [diff] [blame] | 4702 | return NO_ERROR; |
| 4703 | } |
Ruchi Kandoi | f52b3c8 | 2014-04-24 16:42:35 -0700 | [diff] [blame] | 4704 | // This is an experimental interface |
| 4705 | // Needs to be shifted to proper binder interface when we productize |
| 4706 | case 1016: { |
Andy McFadden | 645b1f7 | 2014-06-10 14:43:32 -0700 | [diff] [blame] | 4707 | n = data.readInt32(); |
| 4708 | mPrimaryDispSync.setRefreshSkipCount(n); |
Ruchi Kandoi | f52b3c8 | 2014-04-24 16:42:35 -0700 | [diff] [blame] | 4709 | return NO_ERROR; |
| 4710 | } |
Dan Stoza | ee44edd | 2015-03-23 15:50:23 -0700 | [diff] [blame] | 4711 | case 1017: { |
| 4712 | n = data.readInt32(); |
| 4713 | mForceFullDamage = static_cast<bool>(n); |
| 4714 | return NO_ERROR; |
| 4715 | } |
Dan Stoza | db4ac3c | 2015-04-14 11:34:01 -0700 | [diff] [blame] | 4716 | case 1018: { // Modify Choreographer's phase offset |
| 4717 | n = data.readInt32(); |
| 4718 | mEventThread->setPhaseOffset(static_cast<nsecs_t>(n)); |
| 4719 | return NO_ERROR; |
| 4720 | } |
| 4721 | case 1019: { // Modify SurfaceFlinger's phase offset |
| 4722 | n = data.readInt32(); |
| 4723 | mSFEventThread->setPhaseOffset(static_cast<nsecs_t>(n)); |
| 4724 | return NO_ERROR; |
| 4725 | } |
Irvel | 468051e | 2016-06-13 16:44:44 -0700 | [diff] [blame] | 4726 | case 1020: { // Layer updates interceptor |
| 4727 | n = data.readInt32(); |
| 4728 | if (n) { |
| 4729 | ALOGV("Interceptor enabled"); |
Lloyd Pique | 4dccc41 | 2018-01-22 17:21:36 -0800 | [diff] [blame] | 4730 | mInterceptor->enable(mDrawingState.layersSortedByZ, mDrawingState.displays); |
Irvel | 468051e | 2016-06-13 16:44:44 -0700 | [diff] [blame] | 4731 | } |
| 4732 | else{ |
| 4733 | ALOGV("Interceptor disabled"); |
Lloyd Pique | 4dccc41 | 2018-01-22 17:21:36 -0800 | [diff] [blame] | 4734 | mInterceptor->disable(); |
Irvel | 468051e | 2016-06-13 16:44:44 -0700 | [diff] [blame] | 4735 | } |
| 4736 | return NO_ERROR; |
| 4737 | } |
Dan Stoza | 8cf150a | 2016-08-02 10:27:31 -0700 | [diff] [blame] | 4738 | case 1021: { // Disable HWC virtual displays |
| 4739 | n = data.readInt32(); |
| 4740 | mUseHwcVirtualDisplays = !n; |
| 4741 | return NO_ERROR; |
| 4742 | } |
Romain Guy | 0147a17 | 2017-06-01 13:53:56 -0700 | [diff] [blame] | 4743 | case 1022: { // Set saturation boost |
Chia-I Wu | 28f320b | 2018-05-03 11:02:56 -0700 | [diff] [blame] | 4744 | Mutex::Autolock _l(mStateLock); |
Peiyong Lin | dd9b2ae | 2018-03-01 16:22:45 -0800 | [diff] [blame] | 4745 | mGlobalSaturationFactor = std::max(0.0f, std::min(data.readFloat(), 2.0f)); |
Romain Guy | 0147a17 | 2017-06-01 13:53:56 -0700 | [diff] [blame] | 4746 | |
Chia-I Wu | 28f320b | 2018-05-03 11:02:56 -0700 | [diff] [blame] | 4747 | updateColorMatrixLocked(); |
Romain Guy | 0147a17 | 2017-06-01 13:53:56 -0700 | [diff] [blame] | 4748 | return NO_ERROR; |
| 4749 | } |
Romain Guy | 54f154a | 2017-10-24 21:40:32 +0100 | [diff] [blame] | 4750 | case 1023: { // Set native mode |
Peiyong Lin | dd9b2ae | 2018-03-01 16:22:45 -0800 | [diff] [blame] | 4751 | int32_t value = data.readInt32(); |
| 4752 | if (value > 2) { |
| 4753 | return BAD_VALUE; |
| 4754 | } |
| 4755 | if (value == 2 && !mBuiltinDisplaySupportsEnhance) { |
| 4756 | return BAD_VALUE; |
| 4757 | } |
Romain Guy | 54f154a | 2017-10-24 21:40:32 +0100 | [diff] [blame] | 4758 | |
Peiyong Lin | dd9b2ae | 2018-03-01 16:22:45 -0800 | [diff] [blame] | 4759 | mDisplayColorSetting = toDisplayColorSetting(value); |
Romain Guy | 54f154a | 2017-10-24 21:40:32 +0100 | [diff] [blame] | 4760 | invalidateHwcGeometry(); |
| 4761 | repaintEverything(); |
| 4762 | return NO_ERROR; |
| 4763 | } |
| 4764 | case 1024: { // Is wide color gamut rendering/color management supported? |
| 4765 | reply->writeBool(hasWideColorDisplay); |
| 4766 | return NO_ERROR; |
| 4767 | } |
Vishnu Nair | 87704c9 | 2018-01-08 15:32:57 -0800 | [diff] [blame] | 4768 | case 1025: { // Set layer tracing |
Adrian Roos | 1e1a128 | 2017-11-01 19:05:31 +0100 | [diff] [blame] | 4769 | n = data.readInt32(); |
| 4770 | if (n) { |
| 4771 | ALOGV("LayerTracing enabled"); |
| 4772 | mTracing.enable(); |
| 4773 | doTracing("tracing.enable"); |
| 4774 | reply->writeInt32(NO_ERROR); |
| 4775 | } else { |
| 4776 | ALOGV("LayerTracing disabled"); |
| 4777 | status_t err = mTracing.disable(); |
| 4778 | reply->writeInt32(err); |
| 4779 | } |
| 4780 | return NO_ERROR; |
| 4781 | } |
Vishnu Nair | 87704c9 | 2018-01-08 15:32:57 -0800 | [diff] [blame] | 4782 | case 1026: { // Get layer tracing status |
| 4783 | reply->writeBool(mTracing.isEnabled()); |
| 4784 | return NO_ERROR; |
| 4785 | } |
Peiyong Lin | dd9b2ae | 2018-03-01 16:22:45 -0800 | [diff] [blame] | 4786 | // Is a DisplayColorSetting supported? |
| 4787 | case 1027: { |
| 4788 | int32_t value = data.readInt32(); |
| 4789 | switch (value) { |
| 4790 | case 0: |
| 4791 | reply->writeBool(hasWideColorDisplay); |
| 4792 | return NO_ERROR; |
| 4793 | case 1: |
| 4794 | reply->writeBool(true); |
| 4795 | return NO_ERROR; |
| 4796 | case 2: |
| 4797 | reply->writeBool(mBuiltinDisplaySupportsEnhance); |
| 4798 | return NO_ERROR; |
| 4799 | default: |
| 4800 | return BAD_VALUE; |
| 4801 | } |
| 4802 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4803 | } |
| 4804 | } |
| 4805 | return err; |
| 4806 | } |
| 4807 | |
Steven Thomas | 6d8110b | 2017-08-31 18:24:21 -0700 | [diff] [blame] | 4808 | void SurfaceFlinger::repaintEverything() { |
Dan Stoza | c7a25ad | 2018-04-12 11:45:09 -0700 | [diff] [blame] | 4809 | android_atomic_or(1, &mRepaintEverything); |
| 4810 | signalTransaction(); |
Steven Thomas | 6d8110b | 2017-08-31 18:24:21 -0700 | [diff] [blame] | 4811 | } |
| 4812 | |
Dan Stoza | 2b6d38e | 2017-06-01 16:40:30 -0700 | [diff] [blame] | 4813 | // A simple RAII class to disconnect from an ANativeWindow* when it goes out of scope |
| 4814 | class WindowDisconnector { |
Mathias Agopian | 2ed0fe5 | 2013-07-08 17:09:41 -0700 | [diff] [blame] | 4815 | public: |
Dan Stoza | 2b6d38e | 2017-06-01 16:40:30 -0700 | [diff] [blame] | 4816 | WindowDisconnector(ANativeWindow* window, int api) : mWindow(window), mApi(api) {} |
| 4817 | ~WindowDisconnector() { |
| 4818 | native_window_api_disconnect(mWindow, mApi); |
Mathias Agopian | 2ed0fe5 | 2013-07-08 17:09:41 -0700 | [diff] [blame] | 4819 | } |
| 4820 | |
Dan Stoza | 2b6d38e | 2017-06-01 16:40:30 -0700 | [diff] [blame] | 4821 | private: |
| 4822 | ANativeWindow* mWindow; |
| 4823 | const int mApi; |
Mathias Agopian | 2ed0fe5 | 2013-07-08 17:09:41 -0700 | [diff] [blame] | 4824 | }; |
| 4825 | |
Chavi Weingarten | 40482ff | 2017-11-30 01:51:40 +0000 | [diff] [blame] | 4826 | status_t SurfaceFlinger::captureScreen(const sp<IBinder>& display, sp<GraphicBuffer>* outBuffer, |
| 4827 | Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight, |
| 4828 | int32_t minLayerZ, int32_t maxLayerZ, |
| 4829 | bool useIdentityTransform, |
chaviw | a76b271 | 2017-09-20 12:02:26 -0700 | [diff] [blame] | 4830 | ISurfaceComposer::Rotation rotation) { |
Dan Stoza | 2b6d38e | 2017-06-01 16:40:30 -0700 | [diff] [blame] | 4831 | ATRACE_CALL(); |
Mathias Agopian | 2a9fc49 | 2013-03-01 13:42:57 -0800 | [diff] [blame] | 4832 | |
chaviw | a76b271 | 2017-09-20 12:02:26 -0700 | [diff] [blame] | 4833 | if (CC_UNLIKELY(display == 0)) return BAD_VALUE; |
| 4834 | |
| 4835 | const sp<const DisplayDevice> device(getDisplayDeviceLocked(display)); |
Garfield Tan | 3b1b8af | 2018-03-16 17:37:33 -0700 | [diff] [blame] | 4836 | if (CC_UNLIKELY(device == 0)) return BAD_VALUE; |
| 4837 | |
chaviw | a76b271 | 2017-09-20 12:02:26 -0700 | [diff] [blame] | 4838 | DisplayRenderArea renderArea(device, sourceCrop, reqHeight, reqWidth, rotation); |
| 4839 | |
| 4840 | auto traverseLayers = std::bind(std::mem_fn(&SurfaceFlinger::traverseLayersInDisplay), this, |
| 4841 | device, minLayerZ, maxLayerZ, std::placeholders::_1); |
Chavi Weingarten | 40482ff | 2017-11-30 01:51:40 +0000 | [diff] [blame] | 4842 | return captureScreenCommon(renderArea, traverseLayers, outBuffer, useIdentityTransform); |
chaviw | a76b271 | 2017-09-20 12:02:26 -0700 | [diff] [blame] | 4843 | } |
| 4844 | |
| 4845 | status_t SurfaceFlinger::captureLayers(const sp<IBinder>& layerHandleBinder, |
Vishnu Nair | 87704c9 | 2018-01-08 15:32:57 -0800 | [diff] [blame] | 4846 | sp<GraphicBuffer>* outBuffer, const Rect& sourceCrop, |
Robert Carr | 578038f | 2018-03-09 12:25:24 -0800 | [diff] [blame] | 4847 | float frameScale, bool childrenOnly) { |
chaviw | a76b271 | 2017-09-20 12:02:26 -0700 | [diff] [blame] | 4848 | ATRACE_CALL(); |
| 4849 | |
| 4850 | class LayerRenderArea : public RenderArea { |
| 4851 | public: |
Robert Carr | 578038f | 2018-03-09 12:25:24 -0800 | [diff] [blame] | 4852 | LayerRenderArea(SurfaceFlinger* flinger, const sp<Layer>& layer, const Rect crop, |
| 4853 | int32_t reqWidth, int32_t reqHeight, bool childrenOnly) |
chaviw | 50da504 | 2018-04-09 13:49:37 -0700 | [diff] [blame] | 4854 | : RenderArea(reqHeight, reqWidth, CaptureFill::CLEAR), |
Robert Carr | 578038f | 2018-03-09 12:25:24 -0800 | [diff] [blame] | 4855 | mLayer(layer), |
| 4856 | mCrop(crop), |
| 4857 | mFlinger(flinger), |
| 4858 | mChildrenOnly(childrenOnly) {} |
Peiyong Lin | dd9b2ae | 2018-03-01 16:22:45 -0800 | [diff] [blame] | 4859 | const Transform& getTransform() const override { return mTransform; } |
chaviw | a76b271 | 2017-09-20 12:02:26 -0700 | [diff] [blame] | 4860 | Rect getBounds() const override { |
| 4861 | const Layer::State& layerState(mLayer->getDrawingState()); |
| 4862 | return Rect(layerState.active.w, layerState.active.h); |
| 4863 | } |
| 4864 | int getHeight() const override { return mLayer->getDrawingState().active.h; } |
| 4865 | int getWidth() const override { return mLayer->getDrawingState().active.w; } |
| 4866 | bool isSecure() const override { return false; } |
| 4867 | bool needsFiltering() const override { return false; } |
Peiyong Lin | dd9b2ae | 2018-03-01 16:22:45 -0800 | [diff] [blame] | 4868 | Rect getSourceCrop() const override { |
| 4869 | if (mCrop.isEmpty()) { |
| 4870 | return getBounds(); |
| 4871 | } else { |
| 4872 | return mCrop; |
| 4873 | } |
| 4874 | } |
| 4875 | bool getWideColorSupport() const override { return false; } |
| 4876 | Dataspace getDataSpace() const override { return Dataspace::UNKNOWN; } |
Peiyong Lin | fb06930 | 2018-04-25 14:34:31 -0700 | [diff] [blame] | 4877 | float getDisplayMaxLuminance() const override { |
| 4878 | return DisplayDevice::sDefaultMaxLumiance; |
| 4879 | } |
Robert Carr | 578038f | 2018-03-09 12:25:24 -0800 | [diff] [blame] | 4880 | |
| 4881 | class ReparentForDrawing { |
| 4882 | public: |
| 4883 | const sp<Layer>& oldParent; |
| 4884 | const sp<Layer>& newParent; |
| 4885 | |
| 4886 | ReparentForDrawing(const sp<Layer>& oldParent, const sp<Layer>& newParent) |
| 4887 | : oldParent(oldParent), newParent(newParent) { |
Robert Carr | 15eae09 | 2018-03-23 13:43:53 -0700 | [diff] [blame] | 4888 | oldParent->setChildrenDrawingParent(newParent); |
Robert Carr | 578038f | 2018-03-09 12:25:24 -0800 | [diff] [blame] | 4889 | } |
Robert Carr | 15eae09 | 2018-03-23 13:43:53 -0700 | [diff] [blame] | 4890 | ~ReparentForDrawing() { oldParent->setChildrenDrawingParent(oldParent); } |
Robert Carr | 578038f | 2018-03-09 12:25:24 -0800 | [diff] [blame] | 4891 | }; |
| 4892 | |
| 4893 | void render(std::function<void()> drawLayers) override { |
| 4894 | if (!mChildrenOnly) { |
| 4895 | mTransform = mLayer->getTransform().inverse(); |
| 4896 | drawLayers(); |
| 4897 | } else { |
| 4898 | Rect bounds = getBounds(); |
| 4899 | screenshotParentLayer = |
| 4900 | new ContainerLayer(mFlinger, nullptr, String8("Screenshot Parent"), |
| 4901 | bounds.getWidth(), bounds.getHeight(), 0); |
| 4902 | |
| 4903 | ReparentForDrawing reparent(mLayer, screenshotParentLayer); |
| 4904 | drawLayers(); |
| 4905 | } |
| 4906 | } |
chaviw | a76b271 | 2017-09-20 12:02:26 -0700 | [diff] [blame] | 4907 | |
chaviw | a76b271 | 2017-09-20 12:02:26 -0700 | [diff] [blame] | 4908 | private: |
chaviw | 7206d49 | 2017-11-10 16:16:12 -0800 | [diff] [blame] | 4909 | const sp<Layer> mLayer; |
| 4910 | const Rect mCrop; |
Robert Carr | 578038f | 2018-03-09 12:25:24 -0800 | [diff] [blame] | 4911 | |
| 4912 | // In the "childrenOnly" case we reparent the children to a screenshot |
| 4913 | // layer which has no properties set and which does not draw. |
| 4914 | sp<ContainerLayer> screenshotParentLayer; |
| 4915 | Transform mTransform; |
| 4916 | |
| 4917 | SurfaceFlinger* mFlinger; |
| 4918 | const bool mChildrenOnly; |
chaviw | a76b271 | 2017-09-20 12:02:26 -0700 | [diff] [blame] | 4919 | }; |
| 4920 | |
| 4921 | auto layerHandle = reinterpret_cast<Layer::Handle*>(layerHandleBinder.get()); |
| 4922 | auto parent = layerHandle->owner.promote(); |
| 4923 | |
chaviw | 7206d49 | 2017-11-10 16:16:12 -0800 | [diff] [blame] | 4924 | if (parent == nullptr || parent->isPendingRemoval()) { |
| 4925 | ALOGE("captureLayers called with a removed parent"); |
| 4926 | return NAME_NOT_FOUND; |
| 4927 | } |
| 4928 | |
Robert Carr | 578038f | 2018-03-09 12:25:24 -0800 | [diff] [blame] | 4929 | const int uid = IPCThreadState::self()->getCallingUid(); |
| 4930 | const bool forSystem = uid == AID_GRAPHICS || uid == AID_SYSTEM; |
| 4931 | if (!forSystem && parent->getCurrentState().flags & layer_state_t::eLayerSecure) { |
| 4932 | ALOGW("Attempting to capture secure layer: PERMISSION_DENIED"); |
| 4933 | return PERMISSION_DENIED; |
| 4934 | } |
| 4935 | |
chaviw | 7206d49 | 2017-11-10 16:16:12 -0800 | [diff] [blame] | 4936 | Rect crop(sourceCrop); |
| 4937 | if (sourceCrop.width() <= 0) { |
| 4938 | crop.left = 0; |
| 4939 | crop.right = parent->getCurrentState().active.w; |
| 4940 | } |
| 4941 | |
| 4942 | if (sourceCrop.height() <= 0) { |
| 4943 | crop.top = 0; |
| 4944 | crop.bottom = parent->getCurrentState().active.h; |
| 4945 | } |
| 4946 | |
| 4947 | int32_t reqWidth = crop.width() * frameScale; |
| 4948 | int32_t reqHeight = crop.height() * frameScale; |
| 4949 | |
Robert Carr | 578038f | 2018-03-09 12:25:24 -0800 | [diff] [blame] | 4950 | LayerRenderArea renderArea(this, parent, crop, reqWidth, reqHeight, childrenOnly); |
chaviw | 7206d49 | 2017-11-10 16:16:12 -0800 | [diff] [blame] | 4951 | |
Robert Carr | 578038f | 2018-03-09 12:25:24 -0800 | [diff] [blame] | 4952 | auto traverseLayers = [parent, childrenOnly](const LayerVector::Visitor& visitor) { |
chaviw | a76b271 | 2017-09-20 12:02:26 -0700 | [diff] [blame] | 4953 | parent->traverseChildrenInZOrder(LayerVector::StateSet::Drawing, [&](Layer* layer) { |
| 4954 | if (!layer->isVisible()) { |
| 4955 | return; |
Robert Carr | 578038f | 2018-03-09 12:25:24 -0800 | [diff] [blame] | 4956 | } else if (childrenOnly && layer == parent.get()) { |
| 4957 | return; |
chaviw | a76b271 | 2017-09-20 12:02:26 -0700 | [diff] [blame] | 4958 | } |
| 4959 | visitor(layer); |
| 4960 | }); |
| 4961 | }; |
Chavi Weingarten | 40482ff | 2017-11-30 01:51:40 +0000 | [diff] [blame] | 4962 | return captureScreenCommon(renderArea, traverseLayers, outBuffer, false); |
chaviw | a76b271 | 2017-09-20 12:02:26 -0700 | [diff] [blame] | 4963 | } |
| 4964 | |
| 4965 | status_t SurfaceFlinger::captureScreenCommon(RenderArea& renderArea, |
| 4966 | TraverseLayersFunction traverseLayers, |
Chavi Weingarten | 40482ff | 2017-11-30 01:51:40 +0000 | [diff] [blame] | 4967 | sp<GraphicBuffer>* outBuffer, |
chaviw | a76b271 | 2017-09-20 12:02:26 -0700 | [diff] [blame] | 4968 | bool useIdentityTransform) { |
| 4969 | ATRACE_CALL(); |
Mathias Agopian | 2a9fc49 | 2013-03-01 13:42:57 -0800 | [diff] [blame] | 4970 | |
chaviw | a76b271 | 2017-09-20 12:02:26 -0700 | [diff] [blame] | 4971 | renderArea.updateDimensions(); |
| 4972 | |
Chavi Weingarten | 40482ff | 2017-11-30 01:51:40 +0000 | [diff] [blame] | 4973 | const uint32_t usage = GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN | |
| 4974 | GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE; |
| 4975 | *outBuffer = new GraphicBuffer(renderArea.getReqWidth(), renderArea.getReqHeight(), |
| 4976 | HAL_PIXEL_FORMAT_RGBA_8888, 1, usage, "screenshot"); |
Dan Stoza | 2b6d38e | 2017-06-01 16:40:30 -0700 | [diff] [blame] | 4977 | |
| 4978 | // This mutex protects syncFd and captureResult for communication of the return values from the |
| 4979 | // main thread back to this Binder thread |
| 4980 | std::mutex captureMutex; |
| 4981 | std::condition_variable captureCondition; |
| 4982 | std::unique_lock<std::mutex> captureLock(captureMutex); |
| 4983 | int syncFd = -1; |
| 4984 | std::optional<status_t> captureResult; |
| 4985 | |
Robert Carr | 03480e2 | 2018-01-04 16:02:06 -0800 | [diff] [blame] | 4986 | const int uid = IPCThreadState::self()->getCallingUid(); |
| 4987 | const bool forSystem = uid == AID_GRAPHICS || uid == AID_SYSTEM; |
| 4988 | |
Dan Stoza | 2b6d38e | 2017-06-01 16:40:30 -0700 | [diff] [blame] | 4989 | sp<LambdaMessage> message = new LambdaMessage([&]() { |
| 4990 | // If there is a refresh pending, bug out early and tell the binder thread to try again |
| 4991 | // after the refresh. |
| 4992 | if (mRefreshPending) { |
| 4993 | ATRACE_NAME("Skipping screenshot for now"); |
| 4994 | std::unique_lock<std::mutex> captureLock(captureMutex); |
| 4995 | captureResult = std::make_optional<status_t>(EAGAIN); |
| 4996 | captureCondition.notify_one(); |
| 4997 | return; |
| 4998 | } |
| 4999 | |
| 5000 | status_t result = NO_ERROR; |
| 5001 | int fd = -1; |
| 5002 | { |
| 5003 | Mutex::Autolock _l(mStateLock); |
Robert Carr | 578038f | 2018-03-09 12:25:24 -0800 | [diff] [blame] | 5004 | renderArea.render([&]() { |
| 5005 | result = captureScreenImplLocked(renderArea, traverseLayers, (*outBuffer).get(), |
| 5006 | useIdentityTransform, forSystem, &fd); |
| 5007 | }); |
Dan Stoza | 2b6d38e | 2017-06-01 16:40:30 -0700 | [diff] [blame] | 5008 | } |
| 5009 | |
| 5010 | { |
| 5011 | std::unique_lock<std::mutex> captureLock(captureMutex); |
| 5012 | syncFd = fd; |
| 5013 | captureResult = std::make_optional<status_t>(result); |
| 5014 | captureCondition.notify_one(); |
| 5015 | } |
| 5016 | }); |
| 5017 | |
Chavi Weingarten | 40482ff | 2017-11-30 01:51:40 +0000 | [diff] [blame] | 5018 | status_t result = postMessageAsync(message); |
Dan Stoza | 2b6d38e | 2017-06-01 16:40:30 -0700 | [diff] [blame] | 5019 | if (result == NO_ERROR) { |
| 5020 | captureCondition.wait(captureLock, [&]() { return captureResult; }); |
| 5021 | while (*captureResult == EAGAIN) { |
| 5022 | captureResult.reset(); |
| 5023 | result = postMessageAsync(message); |
| 5024 | if (result != NO_ERROR) { |
| 5025 | return result; |
| 5026 | } |
| 5027 | captureCondition.wait(captureLock, [&]() { return captureResult; }); |
| 5028 | } |
| 5029 | result = *captureResult; |
| 5030 | } |
| 5031 | |
| 5032 | if (result == NO_ERROR) { |
Chavi Weingarten | 40482ff | 2017-11-30 01:51:40 +0000 | [diff] [blame] | 5033 | sync_wait(syncFd, -1); |
| 5034 | close(syncFd); |
Dan Stoza | 2b6d38e | 2017-06-01 16:40:30 -0700 | [diff] [blame] | 5035 | } |
Chavi Weingarten | 40482ff | 2017-11-30 01:51:40 +0000 | [diff] [blame] | 5036 | |
Dan Stoza | 2b6d38e | 2017-06-01 16:40:30 -0700 | [diff] [blame] | 5037 | return result; |
Mathias Agopian | 2a9fc49 | 2013-03-01 13:42:57 -0800 | [diff] [blame] | 5038 | } |
| 5039 | |
chaviw | a76b271 | 2017-09-20 12:02:26 -0700 | [diff] [blame] | 5040 | void SurfaceFlinger::renderScreenImplLocked(const RenderArea& renderArea, |
| 5041 | TraverseLayersFunction traverseLayers, bool yswap, |
| 5042 | bool useIdentityTransform) { |
Mathias Agopian | 180f10d | 2013-04-10 22:55:41 -0700 | [diff] [blame] | 5043 | ATRACE_CALL(); |
chaviw | a76b271 | 2017-09-20 12:02:26 -0700 | [diff] [blame] | 5044 | |
Lloyd Pique | 144e116 | 2017-12-20 16:44:52 -0800 | [diff] [blame] | 5045 | auto& engine(getRenderEngine()); |
Mathias Agopian | 180f10d | 2013-04-10 22:55:41 -0700 | [diff] [blame] | 5046 | |
| 5047 | // get screen geometry |
chaviw | a76b271 | 2017-09-20 12:02:26 -0700 | [diff] [blame] | 5048 | const auto raWidth = renderArea.getWidth(); |
| 5049 | const auto raHeight = renderArea.getHeight(); |
| 5050 | |
| 5051 | const auto reqWidth = renderArea.getReqWidth(); |
| 5052 | const auto reqHeight = renderArea.getReqHeight(); |
| 5053 | Rect sourceCrop = renderArea.getSourceCrop(); |
| 5054 | |
| 5055 | const bool filtering = static_cast<int32_t>(reqWidth) != raWidth || |
| 5056 | static_cast<int32_t>(reqHeight) != raHeight; |
Mathias Agopian | 180f10d | 2013-04-10 22:55:41 -0700 | [diff] [blame] | 5057 | |
Dan Stoza | c187900 | 2014-05-22 15:59:05 -0700 | [diff] [blame] | 5058 | // if a default or invalid sourceCrop is passed in, set reasonable values |
chaviw | a76b271 | 2017-09-20 12:02:26 -0700 | [diff] [blame] | 5059 | if (sourceCrop.width() == 0 || sourceCrop.height() == 0 || !sourceCrop.isValid()) { |
Dan Stoza | c187900 | 2014-05-22 15:59:05 -0700 | [diff] [blame] | 5060 | sourceCrop.setLeftTop(Point(0, 0)); |
chaviw | a76b271 | 2017-09-20 12:02:26 -0700 | [diff] [blame] | 5061 | sourceCrop.setRightBottom(Point(raWidth, raHeight)); |
Dan Stoza | c187900 | 2014-05-22 15:59:05 -0700 | [diff] [blame] | 5062 | } |
| 5063 | |
| 5064 | // ensure that sourceCrop is inside screen |
| 5065 | if (sourceCrop.left < 0) { |
| 5066 | ALOGE("Invalid crop rect: l = %d (< 0)", sourceCrop.left); |
| 5067 | } |
chaviw | a76b271 | 2017-09-20 12:02:26 -0700 | [diff] [blame] | 5068 | if (sourceCrop.right > raWidth) { |
| 5069 | ALOGE("Invalid crop rect: r = %d (> %d)", sourceCrop.right, raWidth); |
Dan Stoza | c187900 | 2014-05-22 15:59:05 -0700 | [diff] [blame] | 5070 | } |
| 5071 | if (sourceCrop.top < 0) { |
| 5072 | ALOGE("Invalid crop rect: t = %d (< 0)", sourceCrop.top); |
| 5073 | } |
chaviw | a76b271 | 2017-09-20 12:02:26 -0700 | [diff] [blame] | 5074 | if (sourceCrop.bottom > raHeight) { |
| 5075 | ALOGE("Invalid crop rect: b = %d (> %d)", sourceCrop.bottom, raHeight); |
Dan Stoza | c187900 | 2014-05-22 15:59:05 -0700 | [diff] [blame] | 5076 | } |
| 5077 | |
Peiyong Lin | 34beb7a | 2018-03-28 11:57:12 -0700 | [diff] [blame] | 5078 | Dataspace outputDataspace = Dataspace::UNKNOWN; |
Peiyong Lin | dd9b2ae | 2018-03-01 16:22:45 -0800 | [diff] [blame] | 5079 | if (renderArea.getWideColorSupport()) { |
| 5080 | outputDataspace = renderArea.getDataSpace(); |
Chia-I Wu | 69bf10f | 2018-02-20 13:04:50 -0800 | [diff] [blame] | 5081 | } |
Peiyong Lin | fb06930 | 2018-04-25 14:34:31 -0700 | [diff] [blame] | 5082 | engine.setOutputDataSpace(outputDataspace); |
| 5083 | engine.setDisplayMaxLuminance(renderArea.getDisplayMaxLuminance()); |
Romain Guy | 88d37dd | 2017-05-26 17:57:05 -0700 | [diff] [blame] | 5084 | |
Mathias Agopian | 180f10d | 2013-04-10 22:55:41 -0700 | [diff] [blame] | 5085 | // make sure to clear all GL error flags |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 5086 | engine.checkErrors(); |
Mathias Agopian | 180f10d | 2013-04-10 22:55:41 -0700 | [diff] [blame] | 5087 | |
| 5088 | // set-up our viewport |
chaviw | a76b271 | 2017-09-20 12:02:26 -0700 | [diff] [blame] | 5089 | engine.setViewportAndProjection(reqWidth, reqHeight, sourceCrop, raHeight, yswap, |
| 5090 | renderArea.getRotationFlags()); |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 5091 | engine.disableTexturing(); |
Mathias Agopian | 180f10d | 2013-04-10 22:55:41 -0700 | [diff] [blame] | 5092 | |
chaviw | 50da504 | 2018-04-09 13:49:37 -0700 | [diff] [blame] | 5093 | const float alpha = RenderArea::getCaptureFillValue(renderArea.getCaptureFill()); |
Mathias Agopian | 180f10d | 2013-04-10 22:55:41 -0700 | [diff] [blame] | 5094 | // redraw the screen entirely... |
chaviw | 50da504 | 2018-04-09 13:49:37 -0700 | [diff] [blame] | 5095 | engine.clearWithColor(0, 0, 0, alpha); |
Mathias Agopian | 180f10d | 2013-04-10 22:55:41 -0700 | [diff] [blame] | 5096 | |
chaviw | a76b271 | 2017-09-20 12:02:26 -0700 | [diff] [blame] | 5097 | traverseLayers([&](Layer* layer) { |
| 5098 | if (filtering) layer->setFiltering(true); |
bohu | 2156613 | 2018-03-27 14:36:34 -0700 | [diff] [blame] | 5099 | layer->draw(renderArea, useIdentityTransform); |
chaviw | a76b271 | 2017-09-20 12:02:26 -0700 | [diff] [blame] | 5100 | if (filtering) layer->setFiltering(false); |
| 5101 | }); |
Mathias Agopian | 180f10d | 2013-04-10 22:55:41 -0700 | [diff] [blame] | 5102 | } |
| 5103 | |
chaviw | a76b271 | 2017-09-20 12:02:26 -0700 | [diff] [blame] | 5104 | status_t SurfaceFlinger::captureScreenImplLocked(const RenderArea& renderArea, |
| 5105 | TraverseLayersFunction traverseLayers, |
| 5106 | ANativeWindowBuffer* buffer, |
Chavi Weingarten | 40482ff | 2017-11-30 01:51:40 +0000 | [diff] [blame] | 5107 | bool useIdentityTransform, |
Robert Carr | 03480e2 | 2018-01-04 16:02:06 -0800 | [diff] [blame] | 5108 | bool forSystem, |
chaviw | a76b271 | 2017-09-20 12:02:26 -0700 | [diff] [blame] | 5109 | int* outSyncFd) { |
Mathias Agopian | 2a9fc49 | 2013-03-01 13:42:57 -0800 | [diff] [blame] | 5110 | ATRACE_CALL(); |
| 5111 | |
Pablo Ceballos | b5b3563 | 2016-02-23 11:18:51 -0800 | [diff] [blame] | 5112 | bool secureLayerIsVisible = false; |
chaviw | a76b271 | 2017-09-20 12:02:26 -0700 | [diff] [blame] | 5113 | |
| 5114 | traverseLayers([&](Layer* layer) { |
| 5115 | secureLayerIsVisible = secureLayerIsVisible || (layer->isVisible() && layer->isSecure()); |
| 5116 | }); |
Pablo Ceballos | b5b3563 | 2016-02-23 11:18:51 -0800 | [diff] [blame] | 5117 | |
Robert Carr | 03480e2 | 2018-01-04 16:02:06 -0800 | [diff] [blame] | 5118 | // We allow the system server to take screenshots of secure layers for |
| 5119 | // use in situations like the Screen-rotation animation and place |
| 5120 | // the impetus on WindowManager to not persist them. |
| 5121 | if (secureLayerIsVisible && !forSystem) { |
Pablo Ceballos | b5b3563 | 2016-02-23 11:18:51 -0800 | [diff] [blame] | 5122 | ALOGW("FB is protected: PERMISSION_DENIED"); |
| 5123 | return PERMISSION_DENIED; |
| 5124 | } |
| 5125 | |
Dan Stoza | a9b1aa0 | 2017-06-01 14:16:23 -0700 | [diff] [blame] | 5126 | // this binds the given EGLImage as a framebuffer for the |
| 5127 | // duration of this scope. |
Lloyd Pique | 144e116 | 2017-12-20 16:44:52 -0800 | [diff] [blame] | 5128 | RE::BindNativeBufferAsFramebuffer bufferBond(getRenderEngine(), buffer); |
Chia-I Wu | eadbaa6 | 2017-11-09 11:26:15 -0800 | [diff] [blame] | 5129 | if (bufferBond.getStatus() != NO_ERROR) { |
| 5130 | ALOGE("got ANWB binding error while taking screenshot"); |
Dan Stoza | abcda35 | 2017-06-01 14:37:39 -0700 | [diff] [blame] | 5131 | return INVALID_OPERATION; |
| 5132 | } |
Dan Stoza | a9b1aa0 | 2017-06-01 14:16:23 -0700 | [diff] [blame] | 5133 | |
Dan Stoza | abcda35 | 2017-06-01 14:37:39 -0700 | [diff] [blame] | 5134 | // this will in fact render into our dequeued buffer |
| 5135 | // via an FBO, which means we didn't have to create |
| 5136 | // an EGLSurface and therefore we're not |
| 5137 | // dependent on the context's EGLConfig. |
chaviw | a76b271 | 2017-09-20 12:02:26 -0700 | [diff] [blame] | 5138 | renderScreenImplLocked(renderArea, traverseLayers, true, useIdentityTransform); |
Dan Stoza | a9b1aa0 | 2017-06-01 14:16:23 -0700 | [diff] [blame] | 5139 | |
Dan Stoza | abcda35 | 2017-06-01 14:37:39 -0700 | [diff] [blame] | 5140 | if (DEBUG_SCREENSHOTS) { |
Chia-I Wu | 767fcf7 | 2017-11-30 22:07:38 -0800 | [diff] [blame] | 5141 | getRenderEngine().finish(); |
| 5142 | *outSyncFd = -1; |
| 5143 | |
chaviw | a76b271 | 2017-09-20 12:02:26 -0700 | [diff] [blame] | 5144 | const auto reqWidth = renderArea.getReqWidth(); |
| 5145 | const auto reqHeight = renderArea.getReqHeight(); |
| 5146 | |
Dan Stoza | abcda35 | 2017-06-01 14:37:39 -0700 | [diff] [blame] | 5147 | uint32_t* pixels = new uint32_t[reqWidth*reqHeight]; |
| 5148 | getRenderEngine().readPixels(0, 0, reqWidth, reqHeight, pixels); |
chaviw | a76b271 | 2017-09-20 12:02:26 -0700 | [diff] [blame] | 5149 | checkScreenshot(reqWidth, reqHeight, reqWidth, pixels, traverseLayers); |
Dan Stoza | abcda35 | 2017-06-01 14:37:39 -0700 | [diff] [blame] | 5150 | delete [] pixels; |
Chia-I Wu | 767fcf7 | 2017-11-30 22:07:38 -0800 | [diff] [blame] | 5151 | } else { |
| 5152 | base::unique_fd syncFd = getRenderEngine().flush(); |
| 5153 | if (syncFd < 0) { |
| 5154 | getRenderEngine().finish(); |
| 5155 | } |
| 5156 | *outSyncFd = syncFd.release(); |
Dan Stoza | abcda35 | 2017-06-01 14:37:39 -0700 | [diff] [blame] | 5157 | } |
| 5158 | |
Dan Stoza | 2b6d38e | 2017-06-01 16:40:30 -0700 | [diff] [blame] | 5159 | return NO_ERROR; |
Mathias Agopian | 74c40c0 | 2010-09-29 13:02:36 -0700 | [diff] [blame] | 5160 | } |
| 5161 | |
Mathias Agopian | d555684 | 2013-09-19 17:08:37 -0700 | [diff] [blame] | 5162 | void SurfaceFlinger::checkScreenshot(size_t w, size_t s, size_t h, void const* vaddr, |
chaviw | a76b271 | 2017-09-20 12:02:26 -0700 | [diff] [blame] | 5163 | TraverseLayersFunction traverseLayers) { |
Mathias Agopian | fee2b46 | 2013-07-03 12:34:01 -0700 | [diff] [blame] | 5164 | if (DEBUG_SCREENSHOTS) { |
chaviw | a76b271 | 2017-09-20 12:02:26 -0700 | [diff] [blame] | 5165 | for (size_t y = 0; y < h; y++) { |
| 5166 | uint32_t const* p = (uint32_t const*)vaddr + y * s; |
| 5167 | for (size_t x = 0; x < w; x++) { |
Mathias Agopian | fee2b46 | 2013-07-03 12:34:01 -0700 | [diff] [blame] | 5168 | if (p[x] != 0xFF000000) return; |
| 5169 | } |
| 5170 | } |
chaviw | a76b271 | 2017-09-20 12:02:26 -0700 | [diff] [blame] | 5171 | ALOGE("*** we just took a black screenshot ***"); |
Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 5172 | |
Robert Carr | 2047fae | 2016-11-28 14:09:09 -0800 | [diff] [blame] | 5173 | size_t i = 0; |
chaviw | a76b271 | 2017-09-20 12:02:26 -0700 | [diff] [blame] | 5174 | traverseLayers([&](Layer* layer) { |
Mathias Agopian | fee2b46 | 2013-07-03 12:34:01 -0700 | [diff] [blame] | 5175 | const Layer::State& state(layer->getDrawingState()); |
chaviw | a76b271 | 2017-09-20 12:02:26 -0700 | [diff] [blame] | 5176 | ALOGE("%c index=%zu, name=%s, layerStack=%d, z=%d, visible=%d, flags=%x, alpha=%.3f", |
| 5177 | layer->isVisible() ? '+' : '-', i, layer->getName().string(), |
| 5178 | layer->getLayerStack(), state.z, layer->isVisible(), state.flags, |
| 5179 | static_cast<float>(state.color.a)); |
| 5180 | i++; |
| 5181 | }); |
Mathias Agopian | fee2b46 | 2013-07-03 12:34:01 -0700 | [diff] [blame] | 5182 | } |
| 5183 | } |
| 5184 | |
Mathias Agopian | 1b0b30d | 2010-09-24 11:26:58 -0700 | [diff] [blame] | 5185 | // --------------------------------------------------------------------------- |
| 5186 | |
Dan Stoza | 412903f | 2017-04-27 13:42:17 -0700 | [diff] [blame] | 5187 | void SurfaceFlinger::State::traverseInZOrder(const LayerVector::Visitor& visitor) const { |
| 5188 | layersSortedByZ.traverseInZOrder(stateSet, visitor); |
Mathias Agopian | 921e6ac | 2012-07-23 23:11:29 -0700 | [diff] [blame] | 5189 | } |
| 5190 | |
Dan Stoza | 412903f | 2017-04-27 13:42:17 -0700 | [diff] [blame] | 5191 | void SurfaceFlinger::State::traverseInReverseZOrder(const LayerVector::Visitor& visitor) const { |
| 5192 | layersSortedByZ.traverseInReverseZOrder(stateSet, visitor); |
Mathias Agopian | 921e6ac | 2012-07-23 23:11:29 -0700 | [diff] [blame] | 5193 | } |
| 5194 | |
chaviw | a76b271 | 2017-09-20 12:02:26 -0700 | [diff] [blame] | 5195 | void SurfaceFlinger::traverseLayersInDisplay(const sp<const DisplayDevice>& hw, int32_t minLayerZ, |
| 5196 | int32_t maxLayerZ, |
| 5197 | const LayerVector::Visitor& visitor) { |
| 5198 | // We loop through the first level of layers without traversing, |
| 5199 | // as we need to interpret min/max layer Z in the top level Z space. |
| 5200 | for (const auto& layer : mDrawingState.layersSortedByZ) { |
| 5201 | if (!layer->belongsToDisplay(hw->getLayerStack(), false)) { |
| 5202 | continue; |
| 5203 | } |
| 5204 | const Layer::State& state(layer->getDrawingState()); |
Chia-I Wu | ec2d985 | 2017-11-21 09:21:01 -0800 | [diff] [blame] | 5205 | // relative layers are traversed in Layer::traverseInZOrder |
| 5206 | if (state.zOrderRelativeOf != nullptr || state.z < minLayerZ || state.z > maxLayerZ) { |
chaviw | a76b271 | 2017-09-20 12:02:26 -0700 | [diff] [blame] | 5207 | continue; |
| 5208 | } |
| 5209 | layer->traverseInZOrder(LayerVector::StateSet::Drawing, [&](Layer* layer) { |
Adrian Roos | 8acf5a0 | 2018-01-17 21:28:19 +0100 | [diff] [blame] | 5210 | if (!layer->belongsToDisplay(hw->getLayerStack(), false)) { |
| 5211 | return; |
| 5212 | } |
chaviw | a76b271 | 2017-09-20 12:02:26 -0700 | [diff] [blame] | 5213 | if (!layer->isVisible()) { |
| 5214 | return; |
| 5215 | } |
| 5216 | visitor(layer); |
| 5217 | }); |
| 5218 | } |
| 5219 | } |
| 5220 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5221 | }; // namespace android |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 5222 | |
Chia-I Wu | 30505fb | 2018-03-26 16:20:31 -0700 | [diff] [blame] | 5223 | |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 5224 | #if defined(__gl_h_) |
| 5225 | #error "don't include gl/gl.h in this file" |
| 5226 | #endif |
| 5227 | |
| 5228 | #if defined(__gl2_h_) |
| 5229 | #error "don't include gl2/gl2.h in this file" |
Chia-I Wu | 767fcf7 | 2017-11-30 22:07:38 -0800 | [diff] [blame] | 5230 | #endif |