| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [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 |  | 
|  | 17 | #define ATRACE_TAG ATRACE_TAG_GRAPHICS | 
|  | 18 |  | 
| Mark Salyzyn | 7823e12 | 2016-09-29 08:08:05 -0700 | [diff] [blame] | 19 | #include <dlfcn.h> | 
|  | 20 | #include <errno.h> | 
|  | 21 | #include <inttypes.h> | 
|  | 22 | #include <math.h> | 
|  | 23 | #include <stdatomic.h> | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 24 | #include <stdint.h> | 
|  | 25 | #include <sys/types.h> | 
| Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 26 |  | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 27 | #include <mutex> | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 28 |  | 
|  | 29 | #include <EGL/egl.h> | 
|  | 30 |  | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 31 | #include <cutils/properties.h> | 
| Mark Salyzyn | 7823e12 | 2016-09-29 08:08:05 -0700 | [diff] [blame] | 32 | #include <log/log.h> | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 33 |  | 
|  | 34 | #include <binder/IPCThreadState.h> | 
|  | 35 | #include <binder/IServiceManager.h> | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 36 | #include <binder/PermissionCache.h> | 
|  | 37 |  | 
|  | 38 | #include <ui/DisplayInfo.h> | 
|  | 39 | #include <ui/DisplayStatInfo.h> | 
|  | 40 |  | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 41 | #include <gui/BufferQueue.h> | 
|  | 42 | #include <gui/GuiConfig.h> | 
|  | 43 | #include <gui/IDisplayEventConnection.h> | 
|  | 44 | #include <gui/Surface.h> | 
| Romain Guy | f8b4ca5 | 2017-03-16 18:39:20 +0000 | [diff] [blame] | 45 | #include <gui/GraphicBufferAlloc.h> | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 46 |  | 
|  | 47 | #include <ui/GraphicBufferAllocator.h> | 
|  | 48 | #include <ui/HdrCapabilities.h> | 
|  | 49 | #include <ui/PixelFormat.h> | 
|  | 50 | #include <ui/UiConfig.h> | 
|  | 51 |  | 
|  | 52 | #include <utils/misc.h> | 
|  | 53 | #include <utils/String8.h> | 
|  | 54 | #include <utils/String16.h> | 
|  | 55 | #include <utils/StopWatch.h> | 
|  | 56 | #include <utils/Timers.h> | 
|  | 57 | #include <utils/Trace.h> | 
|  | 58 |  | 
|  | 59 | #include <private/android_filesystem_config.h> | 
|  | 60 | #include <private/gui/SyncFeatures.h> | 
|  | 61 |  | 
|  | 62 | #include <set> | 
|  | 63 |  | 
|  | 64 | #include "Client.h" | 
|  | 65 | #include "clz.h" | 
|  | 66 | #include "Colorizer.h" | 
|  | 67 | #include "DdmConnection.h" | 
|  | 68 | #include "DisplayDevice.h" | 
|  | 69 | #include "DispSync.h" | 
|  | 70 | #include "EventControlThread.h" | 
|  | 71 | #include "EventThread.h" | 
|  | 72 | #include "Layer.h" | 
| Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 73 | #include "LayerVector.h" | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 74 | #include "LayerDim.h" | 
| Robert Carr | 1db73f6 | 2016-12-21 12:58:51 -0800 | [diff] [blame] | 75 | #include "MonitoredProducer.h" | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 76 | #include "SurfaceFlinger.h" | 
|  | 77 |  | 
|  | 78 | #include "DisplayHardware/FramebufferSurface.h" | 
|  | 79 | #include "DisplayHardware/HWComposer.h" | 
|  | 80 | #include "DisplayHardware/VirtualDisplaySurface.h" | 
|  | 81 |  | 
|  | 82 | #include "Effects/Daltonizer.h" | 
|  | 83 |  | 
|  | 84 | #include "RenderEngine/RenderEngine.h" | 
|  | 85 | #include <cutils/compiler.h> | 
|  | 86 |  | 
| Fabien Sanglard | 0cc1938 | 2017-03-06 11:54:40 -0800 | [diff] [blame] | 87 | #include <android/hardware/configstore/1.0/ISurfaceFlingerConfigs.h> | 
|  | 88 | #include <configstore/Utils.h> | 
|  | 89 |  | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 90 | #define DISPLAY_COUNT       1 | 
|  | 91 |  | 
|  | 92 | /* | 
|  | 93 | * DEBUG_SCREENSHOTS: set to true to check that screenshots are not all | 
|  | 94 | * black pixels. | 
|  | 95 | */ | 
|  | 96 | #define DEBUG_SCREENSHOTS   false | 
|  | 97 |  | 
|  | 98 | EGLAPI const char* eglQueryStringImplementationANDROID(EGLDisplay dpy, EGLint name); | 
|  | 99 |  | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 100 | namespace android { | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 101 | // --------------------------------------------------------------------------- | 
|  | 102 |  | 
| Fabien Sanglard | 0cc1938 | 2017-03-06 11:54:40 -0800 | [diff] [blame] | 103 | using namespace android::hardware::configstore; | 
|  | 104 | using namespace android::hardware::configstore::V1_0; | 
|  | 105 |  | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 106 | const String16 sHardwareTest("android.permission.HARDWARE_TEST"); | 
|  | 107 | const String16 sAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER"); | 
|  | 108 | const String16 sReadFramebuffer("android.permission.READ_FRAME_BUFFER"); | 
|  | 109 | const String16 sDump("android.permission.DUMP"); | 
|  | 110 |  | 
|  | 111 | // --------------------------------------------------------------------------- | 
| Fabien Sanglard | 0cc1938 | 2017-03-06 11:54:40 -0800 | [diff] [blame] | 112 | int64_t SurfaceFlinger::vsyncPhaseOffsetNs; | 
|  | 113 | int64_t SurfaceFlinger::sfVsyncPhaseOffsetNs; | 
| Fabien Sanglard | c93afd5 | 2017-03-13 13:02:42 -0700 | [diff] [blame] | 114 | bool SurfaceFlinger::useContextPriority; | 
| Fabien Sanglard | c45a7d9 | 2017-03-14 13:24:22 -0700 | [diff] [blame] | 115 | int64_t SurfaceFlinger::dispSyncPresentTimeOffset; | 
| Fabien Sanglard | a34ed63 | 2017-03-14 11:43:52 -0700 | [diff] [blame] | 116 | bool SurfaceFlinger::useHwcForRgbToYuv; | 
| Fabien Sanglard | c8e387e | 2017-03-10 10:30:28 -0800 | [diff] [blame] | 117 | uint64_t SurfaceFlinger::maxVirtualDisplaySize; | 
| Fabien Sanglard | cbf153b | 2017-03-10 17:57:12 -0800 | [diff] [blame] | 118 | bool SurfaceFlinger::hasSyncFramework; | 
| Fabien Sanglard | 1971b63 | 2017-03-10 14:50:03 -0800 | [diff] [blame] | 119 | int64_t SurfaceFlinger::maxFrameBufferAcquiredBuffers; | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 120 |  | 
|  | 121 | SurfaceFlinger::SurfaceFlinger() | 
|  | 122 | :   BnSurfaceComposer(), | 
|  | 123 | mTransactionFlags(0), | 
|  | 124 | mTransactionPending(false), | 
|  | 125 | mAnimTransactionPending(false), | 
|  | 126 | mLayersRemoved(false), | 
| Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 127 | mLayersAdded(false), | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 128 | mRepaintEverything(0), | 
|  | 129 | mRenderEngine(NULL), | 
|  | 130 | mBootTime(systemTime()), | 
|  | 131 | mVisibleRegionsDirty(false), | 
|  | 132 | mHwWorkListDirty(false), | 
|  | 133 | mAnimCompositionPending(false), | 
|  | 134 | mDebugRegion(0), | 
|  | 135 | mDebugDDMS(0), | 
|  | 136 | mDebugDisableHWC(0), | 
|  | 137 | mDebugDisableTransformHint(0), | 
|  | 138 | mDebugInSwapBuffers(0), | 
|  | 139 | mLastSwapBufferTime(0), | 
|  | 140 | mDebugInTransaction(0), | 
|  | 141 | mLastTransactionTime(0), | 
|  | 142 | mBootFinished(false), | 
|  | 143 | mForceFullDamage(false), | 
| Robert Carr | 0d48072 | 2017-01-10 16:42:54 -0800 | [diff] [blame] | 144 | mInterceptor(this), | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 145 | mPrimaryDispSync("PrimaryDispSync"), | 
|  | 146 | mPrimaryHWVsyncEnabled(false), | 
|  | 147 | mHWVsyncAvailable(false), | 
|  | 148 | mDaltonize(false), | 
|  | 149 | mHasColorMatrix(false), | 
|  | 150 | mHasPoweredOff(false), | 
|  | 151 | mFrameBuckets(), | 
|  | 152 | mTotalTime(0), | 
| Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 153 | mLastSwapTime(0), | 
|  | 154 | mNumLayers(0) | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 155 | { | 
| Fabien Sanglard | c8e387e | 2017-03-10 10:30:28 -0800 | [diff] [blame] | 156 | ALOGI("SurfaceFlinger is starting"); | 
|  | 157 |  | 
| Fabien Sanglard | 0cc1938 | 2017-03-06 11:54:40 -0800 | [diff] [blame] | 158 | vsyncPhaseOffsetNs = getInt64< ISurfaceFlingerConfigs, | 
|  | 159 | &ISurfaceFlingerConfigs::vsyncEventPhaseOffsetNs>(1000000); | 
|  | 160 |  | 
|  | 161 | sfVsyncPhaseOffsetNs = getInt64< ISurfaceFlingerConfigs, | 
|  | 162 | &ISurfaceFlingerConfigs::vsyncSfEventPhaseOffsetNs>(1000000); | 
|  | 163 |  | 
| Fabien Sanglard | c8e387e | 2017-03-10 10:30:28 -0800 | [diff] [blame] | 164 | maxVirtualDisplaySize = getUInt64<ISurfaceFlingerConfigs, | 
|  | 165 | &ISurfaceFlingerConfigs::maxVirtualDisplaySize>(0); | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 166 |  | 
| Fabien Sanglard | cbf153b | 2017-03-10 17:57:12 -0800 | [diff] [blame] | 167 | hasSyncFramework = getBool< ISurfaceFlingerConfigs, | 
|  | 168 | &ISurfaceFlingerConfigs::hasSyncFramework>(true); | 
|  | 169 |  | 
| Fabien Sanglard | c93afd5 | 2017-03-13 13:02:42 -0700 | [diff] [blame] | 170 | useContextPriority = getBool< ISurfaceFlingerConfigs, | 
|  | 171 | &ISurfaceFlingerConfigs::useContextPriority>(false); | 
|  | 172 |  | 
| Fabien Sanglard | c45a7d9 | 2017-03-14 13:24:22 -0700 | [diff] [blame] | 173 | dispSyncPresentTimeOffset = getInt64< ISurfaceFlingerConfigs, | 
|  | 174 | &ISurfaceFlingerConfigs::presentTimeOffsetFromVSyncNs>(0); | 
|  | 175 |  | 
| Fabien Sanglard | a34ed63 | 2017-03-14 11:43:52 -0700 | [diff] [blame] | 176 | useHwcForRgbToYuv = getBool< ISurfaceFlingerConfigs, | 
|  | 177 | &ISurfaceFlingerConfigs::useHwcForRGBtoYUV>(false); | 
|  | 178 |  | 
| Fabien Sanglard | 1971b63 | 2017-03-10 14:50:03 -0800 | [diff] [blame] | 179 | maxFrameBufferAcquiredBuffers = getInt64< ISurfaceFlingerConfigs, | 
|  | 180 | &ISurfaceFlingerConfigs::maxFrameBufferAcquiredBuffers>(2); | 
|  | 181 |  | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 182 | char value[PROPERTY_VALUE_MAX]; | 
|  | 183 |  | 
|  | 184 | property_get("ro.bq.gpu_to_cpu_unsupported", value, "0"); | 
|  | 185 | mGpuToCpuSupported = !atoi(value); | 
|  | 186 |  | 
|  | 187 | property_get("debug.sf.showupdates", value, "0"); | 
|  | 188 | mDebugRegion = atoi(value); | 
|  | 189 |  | 
|  | 190 | property_get("debug.sf.ddms", value, "0"); | 
|  | 191 | mDebugDDMS = atoi(value); | 
|  | 192 | if (mDebugDDMS) { | 
|  | 193 | if (!startDdmConnection()) { | 
|  | 194 | // start failed, and DDMS debugging not enabled | 
|  | 195 | mDebugDDMS = 0; | 
|  | 196 | } | 
|  | 197 | } | 
|  | 198 | ALOGI_IF(mDebugRegion, "showupdates enabled"); | 
|  | 199 | ALOGI_IF(mDebugDDMS, "DDMS debugging enabled"); | 
|  | 200 |  | 
| Fabien Sanglard | 642b23d | 2017-02-09 12:29:39 -0800 | [diff] [blame] | 201 | property_get("debug.sf.enable_hwc_vds", value, "0"); | 
|  | 202 | mUseHwcVirtualDisplays = atoi(value); | 
|  | 203 | ALOGI_IF(!mUseHwcVirtualDisplays, "Enabling HWC virtual displays"); | 
| Fabien Sanglard | 63a5fcd | 2016-12-29 15:13:07 -0800 | [diff] [blame] | 204 |  | 
| Fabien Sanglard | c65dafa | 2017-02-07 14:06:39 -0800 | [diff] [blame] | 205 | property_get("ro.sf.disable_triple_buffer", value, "1"); | 
|  | 206 | mLayerTripleBufferingDisabled = atoi(value); | 
| Fabien Sanglard | 63a5fcd | 2016-12-29 15:13:07 -0800 | [diff] [blame] | 207 | ALOGI_IF(mLayerTripleBufferingDisabled, "Disabling Triple Buffering"); | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 208 | } | 
|  | 209 |  | 
|  | 210 | void SurfaceFlinger::onFirstRef() | 
|  | 211 | { | 
|  | 212 | mEventQueue.init(this); | 
|  | 213 | } | 
|  | 214 |  | 
|  | 215 | SurfaceFlinger::~SurfaceFlinger() | 
|  | 216 | { | 
|  | 217 | EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY); | 
|  | 218 | eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT); | 
|  | 219 | eglTerminate(display); | 
|  | 220 | } | 
|  | 221 |  | 
|  | 222 | void SurfaceFlinger::binderDied(const wp<IBinder>& /* who */) | 
|  | 223 | { | 
|  | 224 | // the window manager died on us. prepare its eulogy. | 
|  | 225 |  | 
|  | 226 | // restore initial conditions (default device unblank, etc) | 
|  | 227 | initializeDisplays(); | 
|  | 228 |  | 
|  | 229 | // restart the boot-animation | 
|  | 230 | startBootAnim(); | 
|  | 231 | } | 
|  | 232 |  | 
| Robert Carr | 1db73f6 | 2016-12-21 12:58:51 -0800 | [diff] [blame] | 233 | static sp<ISurfaceComposerClient> initClient(const sp<Client>& client) { | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 234 | status_t err = client->initCheck(); | 
|  | 235 | if (err == NO_ERROR) { | 
| Robert Carr | 1db73f6 | 2016-12-21 12:58:51 -0800 | [diff] [blame] | 236 | return client; | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 237 | } | 
| Robert Carr | 1db73f6 | 2016-12-21 12:58:51 -0800 | [diff] [blame] | 238 | return nullptr; | 
|  | 239 | } | 
|  | 240 |  | 
|  | 241 | sp<ISurfaceComposerClient> SurfaceFlinger::createConnection() { | 
|  | 242 | return initClient(new Client(this)); | 
|  | 243 | } | 
|  | 244 |  | 
|  | 245 | sp<ISurfaceComposerClient> SurfaceFlinger::createScopedConnection( | 
|  | 246 | const sp<IGraphicBufferProducer>& gbp) { | 
|  | 247 | if (authenticateSurfaceTexture(gbp) == false) { | 
|  | 248 | return nullptr; | 
|  | 249 | } | 
|  | 250 | const auto& layer = (static_cast<MonitoredProducer*>(gbp.get()))->getLayer(); | 
|  | 251 | if (layer == nullptr) { | 
|  | 252 | return nullptr; | 
|  | 253 | } | 
|  | 254 |  | 
|  | 255 | return initClient(new Client(this, layer)); | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 256 | } | 
|  | 257 |  | 
|  | 258 | sp<IBinder> SurfaceFlinger::createDisplay(const String8& displayName, | 
|  | 259 | bool secure) | 
|  | 260 | { | 
|  | 261 | class DisplayToken : public BBinder { | 
|  | 262 | sp<SurfaceFlinger> flinger; | 
|  | 263 | virtual ~DisplayToken() { | 
|  | 264 | // no more references, this display must be terminated | 
|  | 265 | Mutex::Autolock _l(flinger->mStateLock); | 
|  | 266 | flinger->mCurrentState.displays.removeItem(this); | 
|  | 267 | flinger->setTransactionFlags(eDisplayTransactionNeeded); | 
|  | 268 | } | 
|  | 269 | public: | 
|  | 270 | explicit DisplayToken(const sp<SurfaceFlinger>& flinger) | 
|  | 271 | : flinger(flinger) { | 
|  | 272 | } | 
|  | 273 | }; | 
|  | 274 |  | 
|  | 275 | sp<BBinder> token = new DisplayToken(this); | 
|  | 276 |  | 
|  | 277 | Mutex::Autolock _l(mStateLock); | 
|  | 278 | DisplayDeviceState info(DisplayDevice::DISPLAY_VIRTUAL, secure); | 
|  | 279 | info.displayName = displayName; | 
|  | 280 | mCurrentState.displays.add(token, info); | 
|  | 281 | mInterceptor.saveDisplayCreation(info); | 
|  | 282 | return token; | 
|  | 283 | } | 
|  | 284 |  | 
|  | 285 | void SurfaceFlinger::destroyDisplay(const sp<IBinder>& display) { | 
|  | 286 | Mutex::Autolock _l(mStateLock); | 
|  | 287 |  | 
|  | 288 | ssize_t idx = mCurrentState.displays.indexOfKey(display); | 
|  | 289 | if (idx < 0) { | 
|  | 290 | ALOGW("destroyDisplay: invalid display token"); | 
|  | 291 | return; | 
|  | 292 | } | 
|  | 293 |  | 
|  | 294 | const DisplayDeviceState& info(mCurrentState.displays.valueAt(idx)); | 
|  | 295 | if (!info.isVirtualDisplay()) { | 
|  | 296 | ALOGE("destroyDisplay called for non-virtual display"); | 
|  | 297 | return; | 
|  | 298 | } | 
|  | 299 | mInterceptor.saveDisplayDeletion(info.displayId); | 
|  | 300 | mCurrentState.displays.removeItemsAt(idx); | 
|  | 301 | setTransactionFlags(eDisplayTransactionNeeded); | 
|  | 302 | } | 
|  | 303 |  | 
|  | 304 | void SurfaceFlinger::createBuiltinDisplayLocked(DisplayDevice::DisplayType type) { | 
|  | 305 | ALOGW_IF(mBuiltinDisplays[type], | 
|  | 306 | "Overwriting display token for display type %d", type); | 
|  | 307 | mBuiltinDisplays[type] = new BBinder(); | 
|  | 308 | // All non-virtual displays are currently considered secure. | 
|  | 309 | DisplayDeviceState info(type, true); | 
|  | 310 | mCurrentState.displays.add(mBuiltinDisplays[type], info); | 
|  | 311 | mInterceptor.saveDisplayCreation(info); | 
|  | 312 | } | 
|  | 313 |  | 
|  | 314 | sp<IBinder> SurfaceFlinger::getBuiltInDisplay(int32_t id) { | 
|  | 315 | if (uint32_t(id) >= DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) { | 
|  | 316 | ALOGE("getDefaultDisplay: id=%d is not a valid default display id", id); | 
|  | 317 | return NULL; | 
|  | 318 | } | 
|  | 319 | return mBuiltinDisplays[id]; | 
|  | 320 | } | 
|  | 321 |  | 
| Romain Guy | f8b4ca5 | 2017-03-16 18:39:20 +0000 | [diff] [blame] | 322 | sp<IGraphicBufferAlloc> SurfaceFlinger::createGraphicBufferAlloc() | 
|  | 323 | { | 
|  | 324 | sp<GraphicBufferAlloc> gba(new GraphicBufferAlloc()); | 
|  | 325 | return gba; | 
|  | 326 | } | 
|  | 327 |  | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 328 | void SurfaceFlinger::bootFinished() | 
|  | 329 | { | 
| Wei Wang | b254fa3 | 2017-01-31 17:43:23 -0800 | [diff] [blame] | 330 | if (mStartBootAnimThread->join() != NO_ERROR) { | 
|  | 331 | ALOGE("Join StartBootAnimThread failed!"); | 
|  | 332 | } | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 333 | const nsecs_t now = systemTime(); | 
|  | 334 | const nsecs_t duration = now - mBootTime; | 
|  | 335 | ALOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) ); | 
|  | 336 | mBootFinished = true; | 
|  | 337 |  | 
|  | 338 | // wait patiently for the window manager death | 
|  | 339 | const String16 name("window"); | 
|  | 340 | sp<IBinder> window(defaultServiceManager()->getService(name)); | 
|  | 341 | if (window != 0) { | 
|  | 342 | window->linkToDeath(static_cast<IBinder::DeathRecipient*>(this)); | 
|  | 343 | } | 
|  | 344 |  | 
|  | 345 | // stop boot animation | 
|  | 346 | // formerly we would just kill the process, but we now ask it to exit so it | 
|  | 347 | // can choose where to stop the animation. | 
|  | 348 | property_set("service.bootanim.exit", "1"); | 
|  | 349 |  | 
|  | 350 | const int LOGTAG_SF_STOP_BOOTANIM = 60110; | 
|  | 351 | LOG_EVENT_LONG(LOGTAG_SF_STOP_BOOTANIM, | 
|  | 352 | ns2ms(systemTime(SYSTEM_TIME_MONOTONIC))); | 
|  | 353 | } | 
|  | 354 |  | 
|  | 355 | void SurfaceFlinger::deleteTextureAsync(uint32_t texture) { | 
|  | 356 | class MessageDestroyGLTexture : public MessageBase { | 
|  | 357 | RenderEngine& engine; | 
|  | 358 | uint32_t texture; | 
|  | 359 | public: | 
|  | 360 | MessageDestroyGLTexture(RenderEngine& engine, uint32_t texture) | 
|  | 361 | : engine(engine), texture(texture) { | 
|  | 362 | } | 
|  | 363 | virtual bool handler() { | 
|  | 364 | engine.deleteTextures(1, &texture); | 
|  | 365 | return true; | 
|  | 366 | } | 
|  | 367 | }; | 
|  | 368 | postMessageAsync(new MessageDestroyGLTexture(getRenderEngine(), texture)); | 
|  | 369 | } | 
|  | 370 |  | 
|  | 371 | class DispSyncSource : public VSyncSource, private DispSync::Callback { | 
|  | 372 | public: | 
|  | 373 | DispSyncSource(DispSync* dispSync, nsecs_t phaseOffset, bool traceVsync, | 
|  | 374 | const char* name) : | 
|  | 375 | mName(name), | 
|  | 376 | mValue(0), | 
|  | 377 | mTraceVsync(traceVsync), | 
|  | 378 | mVsyncOnLabel(String8::format("VsyncOn-%s", name)), | 
|  | 379 | mVsyncEventLabel(String8::format("VSYNC-%s", name)), | 
|  | 380 | mDispSync(dispSync), | 
|  | 381 | mCallbackMutex(), | 
|  | 382 | mCallback(), | 
|  | 383 | mVsyncMutex(), | 
|  | 384 | mPhaseOffset(phaseOffset), | 
|  | 385 | mEnabled(false) {} | 
|  | 386 |  | 
|  | 387 | virtual ~DispSyncSource() {} | 
|  | 388 |  | 
|  | 389 | virtual void setVSyncEnabled(bool enable) { | 
|  | 390 | Mutex::Autolock lock(mVsyncMutex); | 
|  | 391 | if (enable) { | 
|  | 392 | status_t err = mDispSync->addEventListener(mName, mPhaseOffset, | 
|  | 393 | static_cast<DispSync::Callback*>(this)); | 
|  | 394 | if (err != NO_ERROR) { | 
|  | 395 | ALOGE("error registering vsync callback: %s (%d)", | 
|  | 396 | strerror(-err), err); | 
|  | 397 | } | 
|  | 398 | //ATRACE_INT(mVsyncOnLabel.string(), 1); | 
|  | 399 | } else { | 
|  | 400 | status_t err = mDispSync->removeEventListener( | 
|  | 401 | static_cast<DispSync::Callback*>(this)); | 
|  | 402 | if (err != NO_ERROR) { | 
|  | 403 | ALOGE("error unregistering vsync callback: %s (%d)", | 
|  | 404 | strerror(-err), err); | 
|  | 405 | } | 
|  | 406 | //ATRACE_INT(mVsyncOnLabel.string(), 0); | 
|  | 407 | } | 
|  | 408 | mEnabled = enable; | 
|  | 409 | } | 
|  | 410 |  | 
|  | 411 | virtual void setCallback(const sp<VSyncSource::Callback>& callback) { | 
|  | 412 | Mutex::Autolock lock(mCallbackMutex); | 
|  | 413 | mCallback = callback; | 
|  | 414 | } | 
|  | 415 |  | 
|  | 416 | virtual void setPhaseOffset(nsecs_t phaseOffset) { | 
|  | 417 | Mutex::Autolock lock(mVsyncMutex); | 
|  | 418 |  | 
|  | 419 | // Normalize phaseOffset to [0, period) | 
|  | 420 | auto period = mDispSync->getPeriod(); | 
|  | 421 | phaseOffset %= period; | 
|  | 422 | if (phaseOffset < 0) { | 
|  | 423 | // If we're here, then phaseOffset is in (-period, 0). After this | 
|  | 424 | // operation, it will be in (0, period) | 
|  | 425 | phaseOffset += period; | 
|  | 426 | } | 
|  | 427 | mPhaseOffset = phaseOffset; | 
|  | 428 |  | 
|  | 429 | // If we're not enabled, we don't need to mess with the listeners | 
|  | 430 | if (!mEnabled) { | 
|  | 431 | return; | 
|  | 432 | } | 
|  | 433 |  | 
|  | 434 | // Remove the listener with the old offset | 
|  | 435 | status_t err = mDispSync->removeEventListener( | 
|  | 436 | static_cast<DispSync::Callback*>(this)); | 
|  | 437 | if (err != NO_ERROR) { | 
|  | 438 | ALOGE("error unregistering vsync callback: %s (%d)", | 
|  | 439 | strerror(-err), err); | 
|  | 440 | } | 
|  | 441 |  | 
|  | 442 | // Add a listener with the new offset | 
|  | 443 | err = mDispSync->addEventListener(mName, mPhaseOffset, | 
|  | 444 | static_cast<DispSync::Callback*>(this)); | 
|  | 445 | if (err != NO_ERROR) { | 
|  | 446 | ALOGE("error registering vsync callback: %s (%d)", | 
|  | 447 | strerror(-err), err); | 
|  | 448 | } | 
|  | 449 | } | 
|  | 450 |  | 
|  | 451 | private: | 
|  | 452 | virtual void onDispSyncEvent(nsecs_t when) { | 
|  | 453 | sp<VSyncSource::Callback> callback; | 
|  | 454 | { | 
|  | 455 | Mutex::Autolock lock(mCallbackMutex); | 
|  | 456 | callback = mCallback; | 
|  | 457 |  | 
|  | 458 | if (mTraceVsync) { | 
|  | 459 | mValue = (mValue + 1) % 2; | 
|  | 460 | ATRACE_INT(mVsyncEventLabel.string(), mValue); | 
|  | 461 | } | 
|  | 462 | } | 
|  | 463 |  | 
|  | 464 | if (callback != NULL) { | 
|  | 465 | callback->onVSyncEvent(when); | 
|  | 466 | } | 
|  | 467 | } | 
|  | 468 |  | 
|  | 469 | const char* const mName; | 
|  | 470 |  | 
|  | 471 | int mValue; | 
|  | 472 |  | 
|  | 473 | const bool mTraceVsync; | 
|  | 474 | const String8 mVsyncOnLabel; | 
|  | 475 | const String8 mVsyncEventLabel; | 
|  | 476 |  | 
|  | 477 | DispSync* mDispSync; | 
|  | 478 |  | 
|  | 479 | Mutex mCallbackMutex; // Protects the following | 
|  | 480 | sp<VSyncSource::Callback> mCallback; | 
|  | 481 |  | 
|  | 482 | Mutex mVsyncMutex; // Protects the following | 
|  | 483 | nsecs_t mPhaseOffset; | 
|  | 484 | bool mEnabled; | 
|  | 485 | }; | 
|  | 486 |  | 
|  | 487 | class InjectVSyncSource : public VSyncSource { | 
|  | 488 | public: | 
|  | 489 | InjectVSyncSource() {} | 
|  | 490 |  | 
|  | 491 | virtual ~InjectVSyncSource() {} | 
|  | 492 |  | 
|  | 493 | virtual void setCallback(const sp<VSyncSource::Callback>& callback) { | 
|  | 494 | std::lock_guard<std::mutex> lock(mCallbackMutex); | 
|  | 495 | mCallback = callback; | 
|  | 496 | } | 
|  | 497 |  | 
|  | 498 | virtual void onInjectSyncEvent(nsecs_t when) { | 
|  | 499 | std::lock_guard<std::mutex> lock(mCallbackMutex); | 
|  | 500 | mCallback->onVSyncEvent(when); | 
|  | 501 | } | 
|  | 502 |  | 
|  | 503 | virtual void setVSyncEnabled(bool) {} | 
|  | 504 | virtual void setPhaseOffset(nsecs_t) {} | 
|  | 505 |  | 
|  | 506 | private: | 
|  | 507 | std::mutex mCallbackMutex; // Protects the following | 
|  | 508 | sp<VSyncSource::Callback> mCallback; | 
|  | 509 | }; | 
|  | 510 |  | 
|  | 511 | void SurfaceFlinger::init() { | 
|  | 512 | ALOGI(  "SurfaceFlinger's main thread ready to run. " | 
|  | 513 | "Initializing graphics H/W..."); | 
|  | 514 |  | 
|  | 515 | Mutex::Autolock _l(mStateLock); | 
|  | 516 |  | 
|  | 517 | // initialize EGL for the default display | 
|  | 518 | mEGLDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY); | 
|  | 519 | eglInitialize(mEGLDisplay, NULL, NULL); | 
|  | 520 |  | 
|  | 521 | // start the EventThread | 
|  | 522 | sp<VSyncSource> vsyncSrc = new DispSyncSource(&mPrimaryDispSync, | 
|  | 523 | vsyncPhaseOffsetNs, true, "app"); | 
|  | 524 | mEventThread = new EventThread(vsyncSrc, *this, false); | 
|  | 525 | sp<VSyncSource> sfVsyncSrc = new DispSyncSource(&mPrimaryDispSync, | 
|  | 526 | sfVsyncPhaseOffsetNs, true, "sf"); | 
|  | 527 | mSFEventThread = new EventThread(sfVsyncSrc, *this, true); | 
|  | 528 | mEventQueue.setEventThread(mSFEventThread); | 
|  | 529 |  | 
|  | 530 | // set SFEventThread to SCHED_FIFO to minimize jitter | 
|  | 531 | struct sched_param param = {0}; | 
|  | 532 | param.sched_priority = 2; | 
|  | 533 | if (sched_setscheduler(mSFEventThread->getTid(), SCHED_FIFO, ¶m) != 0) { | 
|  | 534 | ALOGE("Couldn't set SCHED_FIFO for SFEventThread"); | 
|  | 535 | } | 
|  | 536 |  | 
|  | 537 |  | 
|  | 538 | // Initialize the H/W composer object.  There may or may not be an | 
|  | 539 | // actual hardware composer underneath. | 
|  | 540 | mHwc = new HWComposer(this, | 
|  | 541 | *static_cast<HWComposer::EventHandler *>(this)); | 
|  | 542 |  | 
|  | 543 | // get a RenderEngine for the given display / config (can't fail) | 
|  | 544 | mRenderEngine = RenderEngine::create(mEGLDisplay, mHwc->getVisualID()); | 
|  | 545 |  | 
|  | 546 | // retrieve the EGL context that was selected/created | 
|  | 547 | mEGLContext = mRenderEngine->getEGLContext(); | 
|  | 548 |  | 
|  | 549 | LOG_ALWAYS_FATAL_IF(mEGLContext == EGL_NO_CONTEXT, | 
|  | 550 | "couldn't create EGLContext"); | 
|  | 551 |  | 
| Ian Elliott | 5c34de2 | 2017-04-10 14:42:30 -0600 | [diff] [blame] | 552 | // Inform native graphics APIs that the present timestamp is NOT supported: | 
|  | 553 | property_set(kTimestampProperty, "0"); | 
|  | 554 |  | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 555 | // initialize our non-virtual displays | 
|  | 556 | for (size_t i=0 ; i<DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES ; i++) { | 
|  | 557 | DisplayDevice::DisplayType type((DisplayDevice::DisplayType)i); | 
|  | 558 | // set-up the displays that are already connected | 
|  | 559 | if (mHwc->isConnected(i) || type==DisplayDevice::DISPLAY_PRIMARY) { | 
|  | 560 | // All non-virtual displays are currently considered secure. | 
|  | 561 | bool isSecure = true; | 
|  | 562 | createBuiltinDisplayLocked(type); | 
|  | 563 | wp<IBinder> token = mBuiltinDisplays[i]; | 
|  | 564 |  | 
|  | 565 | sp<IGraphicBufferProducer> producer; | 
|  | 566 | sp<IGraphicBufferConsumer> consumer; | 
| Romain Guy | f8b4ca5 | 2017-03-16 18:39:20 +0000 | [diff] [blame] | 567 | BufferQueue::createBufferQueue(&producer, &consumer, | 
|  | 568 | new GraphicBufferAlloc()); | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 569 |  | 
|  | 570 | sp<FramebufferSurface> fbs = new FramebufferSurface(*mHwc, i, | 
|  | 571 | consumer); | 
|  | 572 | int32_t hwcId = allocateHwcDisplayId(type); | 
|  | 573 | sp<DisplayDevice> hw = new DisplayDevice(this, | 
|  | 574 | type, hwcId, mHwc->getFormat(hwcId), isSecure, token, | 
|  | 575 | fbs, producer, | 
| Courtney Goeltzenleuchter | 4f20f9c | 2017-04-06 08:18:34 -0600 | [diff] [blame] | 576 | mRenderEngine->getEGLConfig(), false); | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 577 | if (i > DisplayDevice::DISPLAY_PRIMARY) { | 
|  | 578 | // FIXME: currently we don't get blank/unblank requests | 
|  | 579 | // for displays other than the main display, so we always | 
|  | 580 | // assume a connected display is unblanked. | 
|  | 581 | ALOGD("marking display %zu as acquired/unblanked", i); | 
|  | 582 | hw->setPowerMode(HWC_POWER_MODE_NORMAL); | 
|  | 583 | } | 
|  | 584 | mDisplays.add(token, hw); | 
|  | 585 | } | 
|  | 586 | } | 
|  | 587 |  | 
|  | 588 | // make the GLContext current so that we can create textures when creating Layers | 
|  | 589 | // (which may happens before we render something) | 
| Stephen Kiazyk | 8d6c16d | 2017-04-05 16:46:49 -0700 | [diff] [blame] | 590 | getDefaultDisplayDeviceLocked()->makeCurrent(mEGLDisplay, mEGLContext); | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 591 |  | 
|  | 592 | mEventControlThread = new EventControlThread(this); | 
|  | 593 | mEventControlThread->run("EventControl", PRIORITY_URGENT_DISPLAY); | 
|  | 594 |  | 
|  | 595 | // set a fake vsync period if there is no HWComposer | 
|  | 596 | if (mHwc->initCheck() != NO_ERROR) { | 
|  | 597 | mPrimaryDispSync.setPeriod(16666667); | 
|  | 598 | } | 
|  | 599 |  | 
|  | 600 | // initialize our drawing state | 
|  | 601 | mDrawingState = mCurrentState; | 
|  | 602 |  | 
|  | 603 | // set initial conditions (e.g. unblank default device) | 
|  | 604 | initializeDisplays(); | 
|  | 605 |  | 
|  | 606 | mRenderEngine->primeCache(); | 
|  | 607 |  | 
| Wei Wang | b254fa3 | 2017-01-31 17:43:23 -0800 | [diff] [blame] | 608 | mStartBootAnimThread = new StartBootAnimThread(); | 
|  | 609 | if (mStartBootAnimThread->Start() != NO_ERROR) { | 
|  | 610 | ALOGE("Run StartBootAnimThread failed!"); | 
|  | 611 | } | 
|  | 612 |  | 
|  | 613 | ALOGV("Done initializing"); | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 614 | } | 
|  | 615 |  | 
|  | 616 | int32_t SurfaceFlinger::allocateHwcDisplayId(DisplayDevice::DisplayType type) { | 
|  | 617 | return (uint32_t(type) < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) ? | 
|  | 618 | type : mHwc->allocateDisplayId(); | 
|  | 619 | } | 
|  | 620 |  | 
|  | 621 | void SurfaceFlinger::startBootAnim() { | 
| Wei Wang | b254fa3 | 2017-01-31 17:43:23 -0800 | [diff] [blame] | 622 | // Start boot animation service by setting a property mailbox | 
|  | 623 | // if property setting thread is already running, Start() will be just a NOP | 
|  | 624 | mStartBootAnimThread->Start(); | 
|  | 625 | // Wait until property was set | 
|  | 626 | if (mStartBootAnimThread->join() != NO_ERROR) { | 
|  | 627 | ALOGE("Join StartBootAnimThread failed!"); | 
|  | 628 | } | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 629 | } | 
|  | 630 |  | 
|  | 631 | size_t SurfaceFlinger::getMaxTextureSize() const { | 
|  | 632 | return mRenderEngine->getMaxTextureSize(); | 
|  | 633 | } | 
|  | 634 |  | 
|  | 635 | size_t SurfaceFlinger::getMaxViewportDims() const { | 
|  | 636 | return mRenderEngine->getMaxViewportDims(); | 
|  | 637 | } | 
|  | 638 |  | 
|  | 639 | // ---------------------------------------------------------------------------- | 
|  | 640 |  | 
|  | 641 | bool SurfaceFlinger::authenticateSurfaceTexture( | 
|  | 642 | const sp<IGraphicBufferProducer>& bufferProducer) const { | 
|  | 643 | Mutex::Autolock _l(mStateLock); | 
| Robert Carr | 0d48072 | 2017-01-10 16:42:54 -0800 | [diff] [blame] | 644 | return authenticateSurfaceTextureLocked(bufferProducer); | 
|  | 645 | } | 
|  | 646 |  | 
|  | 647 | bool SurfaceFlinger::authenticateSurfaceTextureLocked( | 
|  | 648 | const sp<IGraphicBufferProducer>& bufferProducer) const { | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 649 | sp<IBinder> surfaceTextureBinder(IInterface::asBinder(bufferProducer)); | 
|  | 650 | return mGraphicBufferProducerList.indexOf(surfaceTextureBinder) >= 0; | 
|  | 651 | } | 
|  | 652 |  | 
| Brian Anderson | 6b37671 | 2017-04-04 10:51:39 -0700 | [diff] [blame] | 653 | status_t SurfaceFlinger::getSupportedFrameTimestamps( | 
|  | 654 | std::vector<FrameEvent>* outSupported) const { | 
|  | 655 | *outSupported = { | 
|  | 656 | FrameEvent::REQUESTED_PRESENT, | 
|  | 657 | FrameEvent::ACQUIRE, | 
|  | 658 | FrameEvent::LATCH, | 
|  | 659 | FrameEvent::FIRST_REFRESH_START, | 
|  | 660 | FrameEvent::LAST_REFRESH_START, | 
|  | 661 | FrameEvent::GPU_COMPOSITION_DONE, | 
|  | 662 | FrameEvent::DEQUEUE_READY, | 
|  | 663 | FrameEvent::RELEASE, | 
|  | 664 | }; | 
|  | 665 | return NO_ERROR; | 
|  | 666 | } | 
|  | 667 |  | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 668 | status_t SurfaceFlinger::getDisplayConfigs(const sp<IBinder>& display, | 
|  | 669 | Vector<DisplayInfo>* configs) { | 
|  | 670 | if ((configs == NULL) || (display.get() == NULL)) { | 
|  | 671 | return BAD_VALUE; | 
|  | 672 | } | 
|  | 673 |  | 
|  | 674 | int32_t type = getDisplayType(display); | 
|  | 675 | if (type < 0) return type; | 
|  | 676 |  | 
|  | 677 | // TODO: Not sure if display density should handled by SF any longer | 
|  | 678 | class Density { | 
|  | 679 | static int getDensityFromProperty(char const* propName) { | 
|  | 680 | char property[PROPERTY_VALUE_MAX]; | 
|  | 681 | int density = 0; | 
|  | 682 | if (property_get(propName, property, NULL) > 0) { | 
|  | 683 | density = atoi(property); | 
|  | 684 | } | 
|  | 685 | return density; | 
|  | 686 | } | 
|  | 687 | public: | 
|  | 688 | static int getEmuDensity() { | 
|  | 689 | return getDensityFromProperty("qemu.sf.lcd_density"); } | 
|  | 690 | static int getBuildDensity()  { | 
|  | 691 | return getDensityFromProperty("ro.sf.lcd_density"); } | 
|  | 692 | }; | 
|  | 693 |  | 
|  | 694 | configs->clear(); | 
|  | 695 |  | 
|  | 696 | const Vector<HWComposer::DisplayConfig>& hwConfigs = | 
|  | 697 | getHwComposer().getConfigs(type); | 
|  | 698 | for (size_t c = 0; c < hwConfigs.size(); ++c) { | 
|  | 699 | const HWComposer::DisplayConfig& hwConfig = hwConfigs[c]; | 
|  | 700 | DisplayInfo info = DisplayInfo(); | 
|  | 701 |  | 
|  | 702 | float xdpi = hwConfig.xdpi; | 
|  | 703 | float ydpi = hwConfig.ydpi; | 
|  | 704 |  | 
|  | 705 | if (type == DisplayDevice::DISPLAY_PRIMARY) { | 
|  | 706 | // The density of the device is provided by a build property | 
|  | 707 | float density = Density::getBuildDensity() / 160.0f; | 
|  | 708 | if (density == 0) { | 
|  | 709 | // the build doesn't provide a density -- this is wrong! | 
|  | 710 | // use xdpi instead | 
|  | 711 | ALOGE("ro.sf.lcd_density must be defined as a build property"); | 
|  | 712 | density = xdpi / 160.0f; | 
|  | 713 | } | 
|  | 714 | if (Density::getEmuDensity()) { | 
|  | 715 | // if "qemu.sf.lcd_density" is specified, it overrides everything | 
|  | 716 | xdpi = ydpi = density = Density::getEmuDensity(); | 
|  | 717 | density /= 160.0f; | 
|  | 718 | } | 
|  | 719 | info.density = density; | 
|  | 720 |  | 
|  | 721 | // TODO: this needs to go away (currently needed only by webkit) | 
|  | 722 | sp<const DisplayDevice> hw(getDefaultDisplayDevice()); | 
|  | 723 | info.orientation = hw->getOrientation(); | 
|  | 724 | } else { | 
|  | 725 | // TODO: where should this value come from? | 
|  | 726 | static const int TV_DENSITY = 213; | 
|  | 727 | info.density = TV_DENSITY / 160.0f; | 
|  | 728 | info.orientation = 0; | 
|  | 729 | } | 
|  | 730 |  | 
|  | 731 | info.w = hwConfig.width; | 
|  | 732 | info.h = hwConfig.height; | 
|  | 733 | info.xdpi = xdpi; | 
|  | 734 | info.ydpi = ydpi; | 
|  | 735 | info.fps = float(1e9 / hwConfig.refresh); | 
| Fabien Sanglard | 0cc1938 | 2017-03-06 11:54:40 -0800 | [diff] [blame] | 736 | info.appVsyncOffset = vsyncPhaseOffsetNs; | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 737 |  | 
|  | 738 | // This is how far in advance a buffer must be queued for | 
|  | 739 | // presentation at a given time.  If you want a buffer to appear | 
|  | 740 | // on the screen at time N, you must submit the buffer before | 
|  | 741 | // (N - presentationDeadline). | 
|  | 742 | // | 
|  | 743 | // Normally it's one full refresh period (to give SF a chance to | 
|  | 744 | // latch the buffer), but this can be reduced by configuring a | 
|  | 745 | // DispSync offset.  Any additional delays introduced by the hardware | 
|  | 746 | // composer or panel must be accounted for here. | 
|  | 747 | // | 
|  | 748 | // We add an additional 1ms to allow for processing time and | 
|  | 749 | // differences between the ideal and actual refresh rate. | 
|  | 750 | info.presentationDeadline = | 
| Fabien Sanglard | 0cc1938 | 2017-03-06 11:54:40 -0800 | [diff] [blame] | 751 | hwConfig.refresh - sfVsyncPhaseOffsetNs + 1000000; | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 752 |  | 
|  | 753 | // All non-virtual displays are currently considered secure. | 
|  | 754 | info.secure = true; | 
|  | 755 |  | 
|  | 756 | configs->push_back(info); | 
|  | 757 | } | 
|  | 758 |  | 
|  | 759 | return NO_ERROR; | 
|  | 760 | } | 
|  | 761 |  | 
|  | 762 | status_t SurfaceFlinger::getDisplayStats(const sp<IBinder>& /* display */, | 
|  | 763 | DisplayStatInfo* stats) { | 
|  | 764 | if (stats == NULL) { | 
|  | 765 | return BAD_VALUE; | 
|  | 766 | } | 
|  | 767 |  | 
|  | 768 | // FIXME for now we always return stats for the primary display | 
|  | 769 | memset(stats, 0, sizeof(*stats)); | 
|  | 770 | stats->vsyncTime   = mPrimaryDispSync.computeNextRefresh(0); | 
|  | 771 | stats->vsyncPeriod = mPrimaryDispSync.getPeriod(); | 
|  | 772 | return NO_ERROR; | 
|  | 773 | } | 
|  | 774 |  | 
|  | 775 | int SurfaceFlinger::getActiveConfig(const sp<IBinder>& display) { | 
|  | 776 | sp<DisplayDevice> device(getDisplayDevice(display)); | 
|  | 777 | if (device != NULL) { | 
|  | 778 | return device->getActiveConfig(); | 
|  | 779 | } | 
|  | 780 | return BAD_VALUE; | 
|  | 781 | } | 
|  | 782 |  | 
|  | 783 | void SurfaceFlinger::setActiveConfigInternal(const sp<DisplayDevice>& hw, int mode) { | 
|  | 784 | ALOGD("Set active config mode=%d, type=%d flinger=%p", mode, hw->getDisplayType(), | 
|  | 785 | this); | 
|  | 786 | int32_t type = hw->getDisplayType(); | 
|  | 787 | int currentMode = hw->getActiveConfig(); | 
|  | 788 |  | 
|  | 789 | if (mode == currentMode) { | 
|  | 790 | ALOGD("Screen type=%d is already mode=%d", hw->getDisplayType(), mode); | 
|  | 791 | return; | 
|  | 792 | } | 
|  | 793 |  | 
|  | 794 | if (type >= DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) { | 
|  | 795 | ALOGW("Trying to set config for virtual display"); | 
|  | 796 | return; | 
|  | 797 | } | 
|  | 798 |  | 
|  | 799 | hw->setActiveConfig(mode); | 
|  | 800 | getHwComposer().setActiveConfig(type, mode); | 
|  | 801 | } | 
|  | 802 |  | 
|  | 803 | status_t SurfaceFlinger::setActiveConfig(const sp<IBinder>& display, int mode) { | 
|  | 804 | class MessageSetActiveConfig: public MessageBase { | 
|  | 805 | SurfaceFlinger& mFlinger; | 
|  | 806 | sp<IBinder> mDisplay; | 
|  | 807 | int mMode; | 
|  | 808 | public: | 
|  | 809 | MessageSetActiveConfig(SurfaceFlinger& flinger, const sp<IBinder>& disp, | 
|  | 810 | int mode) : | 
|  | 811 | mFlinger(flinger), mDisplay(disp) { mMode = mode; } | 
|  | 812 | virtual bool handler() { | 
|  | 813 | Vector<DisplayInfo> configs; | 
|  | 814 | mFlinger.getDisplayConfigs(mDisplay, &configs); | 
|  | 815 | if (mMode < 0 || mMode >= static_cast<int>(configs.size())) { | 
|  | 816 | ALOGE("Attempt to set active config = %d for display with %zu configs", | 
|  | 817 | mMode, configs.size()); | 
|  | 818 | } | 
|  | 819 | sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay)); | 
|  | 820 | if (hw == NULL) { | 
|  | 821 | ALOGE("Attempt to set active config = %d for null display %p", | 
|  | 822 | mMode, mDisplay.get()); | 
|  | 823 | } else if (hw->getDisplayType() >= DisplayDevice::DISPLAY_VIRTUAL) { | 
|  | 824 | ALOGW("Attempt to set active config = %d for virtual display", | 
|  | 825 | mMode); | 
|  | 826 | } else { | 
|  | 827 | mFlinger.setActiveConfigInternal(hw, mMode); | 
|  | 828 | } | 
|  | 829 | return true; | 
|  | 830 | } | 
|  | 831 | }; | 
|  | 832 | sp<MessageBase> msg = new MessageSetActiveConfig(*this, display, mode); | 
|  | 833 | postMessageSync(msg); | 
|  | 834 | return NO_ERROR; | 
|  | 835 | } | 
|  | 836 |  | 
|  | 837 | status_t SurfaceFlinger::getDisplayColorModes(const sp<IBinder>& display, | 
|  | 838 | Vector<android_color_mode_t>* outColorModes) { | 
|  | 839 | if (outColorModes == nullptr || display.get() == nullptr) { | 
|  | 840 | return BAD_VALUE; | 
|  | 841 | } | 
|  | 842 |  | 
|  | 843 | int32_t type = getDisplayType(display); | 
|  | 844 | if (type < 0) return type; | 
|  | 845 |  | 
|  | 846 | std::set<android_color_mode_t> colorModes; | 
|  | 847 | for (const HWComposer::DisplayConfig& hwConfig : getHwComposer().getConfigs(type)) { | 
|  | 848 | colorModes.insert(hwConfig.colorMode); | 
|  | 849 | } | 
|  | 850 |  | 
|  | 851 | outColorModes->clear(); | 
|  | 852 | std::copy(colorModes.cbegin(), colorModes.cend(), std::back_inserter(*outColorModes)); | 
|  | 853 |  | 
|  | 854 | return NO_ERROR; | 
|  | 855 | } | 
|  | 856 |  | 
|  | 857 | android_color_mode_t SurfaceFlinger::getActiveColorMode(const sp<IBinder>& display) { | 
|  | 858 | if (display.get() == nullptr) return static_cast<android_color_mode_t>(BAD_VALUE); | 
|  | 859 |  | 
|  | 860 | int32_t type = getDisplayType(display); | 
|  | 861 | if (type < 0) return static_cast<android_color_mode_t>(type); | 
|  | 862 |  | 
|  | 863 | return getHwComposer().getColorMode(type); | 
|  | 864 | } | 
|  | 865 |  | 
|  | 866 | status_t SurfaceFlinger::setActiveColorMode(const sp<IBinder>& display, | 
|  | 867 | android_color_mode_t colorMode) { | 
|  | 868 | if (display.get() == nullptr || colorMode < 0) { | 
|  | 869 | return BAD_VALUE; | 
|  | 870 | } | 
|  | 871 |  | 
|  | 872 | int32_t type = getDisplayType(display); | 
|  | 873 | if (type < 0) return type; | 
|  | 874 | const Vector<HWComposer::DisplayConfig>& hwConfigs = getHwComposer().getConfigs(type); | 
|  | 875 | HWComposer::DisplayConfig desiredConfig = hwConfigs[getHwComposer().getCurrentConfig(type)]; | 
|  | 876 | desiredConfig.colorMode = colorMode; | 
|  | 877 | for (size_t c = 0; c < hwConfigs.size(); ++c) { | 
|  | 878 | const HWComposer::DisplayConfig config = hwConfigs[c]; | 
|  | 879 | if (config == desiredConfig) { | 
|  | 880 | return setActiveConfig(display, c); | 
|  | 881 | } | 
|  | 882 | } | 
|  | 883 | return BAD_VALUE; | 
|  | 884 | } | 
|  | 885 |  | 
|  | 886 | status_t SurfaceFlinger::clearAnimationFrameStats() { | 
|  | 887 | Mutex::Autolock _l(mStateLock); | 
|  | 888 | mAnimFrameTracker.clearStats(); | 
|  | 889 | return NO_ERROR; | 
|  | 890 | } | 
|  | 891 |  | 
|  | 892 | status_t SurfaceFlinger::getAnimationFrameStats(FrameStats* outStats) const { | 
|  | 893 | Mutex::Autolock _l(mStateLock); | 
|  | 894 | mAnimFrameTracker.getStats(outStats); | 
|  | 895 | return NO_ERROR; | 
|  | 896 | } | 
|  | 897 |  | 
|  | 898 | status_t SurfaceFlinger::getHdrCapabilities(const sp<IBinder>& /*display*/, | 
|  | 899 | HdrCapabilities* outCapabilities) const { | 
|  | 900 | // HWC1 does not provide HDR capabilities | 
|  | 901 | *outCapabilities = HdrCapabilities(); | 
|  | 902 | return NO_ERROR; | 
|  | 903 | } | 
|  | 904 |  | 
|  | 905 | status_t SurfaceFlinger::enableVSyncInjections(bool enable) { | 
|  | 906 | if (enable == mInjectVSyncs) { | 
|  | 907 | return NO_ERROR; | 
|  | 908 | } | 
|  | 909 |  | 
|  | 910 | if (enable) { | 
|  | 911 | mInjectVSyncs = enable; | 
|  | 912 | ALOGV("VSync Injections enabled"); | 
|  | 913 | if (mVSyncInjector.get() == nullptr) { | 
|  | 914 | mVSyncInjector = new InjectVSyncSource(); | 
|  | 915 | mInjectorEventThread = new EventThread(mVSyncInjector, *this, false); | 
|  | 916 | } | 
|  | 917 | mEventQueue.setEventThread(mInjectorEventThread); | 
|  | 918 | } else { | 
|  | 919 | mInjectVSyncs = enable; | 
|  | 920 | ALOGV("VSync Injections disabled"); | 
|  | 921 | mEventQueue.setEventThread(mSFEventThread); | 
|  | 922 | mVSyncInjector.clear(); | 
|  | 923 | } | 
|  | 924 | return NO_ERROR; | 
|  | 925 | } | 
|  | 926 |  | 
|  | 927 | status_t SurfaceFlinger::injectVSync(nsecs_t when) { | 
|  | 928 | if (!mInjectVSyncs) { | 
|  | 929 | ALOGE("VSync Injections not enabled"); | 
|  | 930 | return BAD_VALUE; | 
|  | 931 | } | 
|  | 932 | if (mInjectVSyncs && mInjectorEventThread.get() != nullptr) { | 
|  | 933 | ALOGV("Injecting VSync inside SurfaceFlinger"); | 
|  | 934 | mVSyncInjector->onInjectSyncEvent(when); | 
|  | 935 | } | 
|  | 936 | return NO_ERROR; | 
|  | 937 | } | 
|  | 938 |  | 
|  | 939 | // ---------------------------------------------------------------------------- | 
|  | 940 |  | 
|  | 941 | sp<IDisplayEventConnection> SurfaceFlinger::createDisplayEventConnection() { | 
|  | 942 | return mEventThread->createEventConnection(); | 
|  | 943 | } | 
|  | 944 |  | 
|  | 945 | // ---------------------------------------------------------------------------- | 
|  | 946 |  | 
|  | 947 | void SurfaceFlinger::waitForEvent() { | 
|  | 948 | mEventQueue.waitMessage(); | 
|  | 949 | } | 
|  | 950 |  | 
|  | 951 | void SurfaceFlinger::signalTransaction() { | 
|  | 952 | mEventQueue.invalidate(); | 
|  | 953 | } | 
|  | 954 |  | 
|  | 955 | void SurfaceFlinger::signalLayerUpdate() { | 
|  | 956 | mEventQueue.invalidate(); | 
|  | 957 | } | 
|  | 958 |  | 
|  | 959 | void SurfaceFlinger::signalRefresh() { | 
|  | 960 | mEventQueue.refresh(); | 
|  | 961 | } | 
|  | 962 |  | 
|  | 963 | status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg, | 
|  | 964 | nsecs_t reltime, uint32_t /* flags */) { | 
|  | 965 | return mEventQueue.postMessage(msg, reltime); | 
|  | 966 | } | 
|  | 967 |  | 
|  | 968 | status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg, | 
|  | 969 | nsecs_t reltime, uint32_t /* flags */) { | 
|  | 970 | status_t res = mEventQueue.postMessage(msg, reltime); | 
|  | 971 | if (res == NO_ERROR) { | 
|  | 972 | msg->wait(); | 
|  | 973 | } | 
|  | 974 | return res; | 
|  | 975 | } | 
|  | 976 |  | 
|  | 977 | void SurfaceFlinger::run() { | 
|  | 978 | do { | 
|  | 979 | waitForEvent(); | 
|  | 980 | } while (true); | 
|  | 981 | } | 
|  | 982 |  | 
|  | 983 | void SurfaceFlinger::enableHardwareVsync() { | 
|  | 984 | Mutex::Autolock _l(mHWVsyncLock); | 
|  | 985 | if (!mPrimaryHWVsyncEnabled && mHWVsyncAvailable) { | 
|  | 986 | mPrimaryDispSync.beginResync(); | 
|  | 987 | //eventControl(HWC_DISPLAY_PRIMARY, SurfaceFlinger::EVENT_VSYNC, true); | 
|  | 988 | mEventControlThread->setVsyncEnabled(true); | 
|  | 989 | mPrimaryHWVsyncEnabled = true; | 
|  | 990 | } | 
|  | 991 | } | 
|  | 992 |  | 
|  | 993 | void SurfaceFlinger::resyncToHardwareVsync(bool makeAvailable) { | 
|  | 994 | Mutex::Autolock _l(mHWVsyncLock); | 
|  | 995 |  | 
|  | 996 | if (makeAvailable) { | 
|  | 997 | mHWVsyncAvailable = true; | 
|  | 998 | } else if (!mHWVsyncAvailable) { | 
|  | 999 | // Hardware vsync is not currently available, so abort the resync | 
|  | 1000 | // attempt for now | 
|  | 1001 | return; | 
|  | 1002 | } | 
|  | 1003 |  | 
|  | 1004 | const nsecs_t period = | 
|  | 1005 | getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY); | 
|  | 1006 |  | 
|  | 1007 | mPrimaryDispSync.reset(); | 
|  | 1008 | mPrimaryDispSync.setPeriod(period); | 
|  | 1009 |  | 
|  | 1010 | if (!mPrimaryHWVsyncEnabled) { | 
|  | 1011 | mPrimaryDispSync.beginResync(); | 
|  | 1012 | //eventControl(HWC_DISPLAY_PRIMARY, SurfaceFlinger::EVENT_VSYNC, true); | 
|  | 1013 | mEventControlThread->setVsyncEnabled(true); | 
|  | 1014 | mPrimaryHWVsyncEnabled = true; | 
|  | 1015 | } | 
|  | 1016 | } | 
|  | 1017 |  | 
|  | 1018 | void SurfaceFlinger::disableHardwareVsync(bool makeUnavailable) { | 
|  | 1019 | Mutex::Autolock _l(mHWVsyncLock); | 
|  | 1020 | if (mPrimaryHWVsyncEnabled) { | 
|  | 1021 | //eventControl(HWC_DISPLAY_PRIMARY, SurfaceFlinger::EVENT_VSYNC, false); | 
|  | 1022 | mEventControlThread->setVsyncEnabled(false); | 
|  | 1023 | mPrimaryDispSync.endResync(); | 
|  | 1024 | mPrimaryHWVsyncEnabled = false; | 
|  | 1025 | } | 
|  | 1026 | if (makeUnavailable) { | 
|  | 1027 | mHWVsyncAvailable = false; | 
|  | 1028 | } | 
|  | 1029 | } | 
|  | 1030 |  | 
|  | 1031 | void SurfaceFlinger::resyncWithRateLimit() { | 
|  | 1032 | static constexpr nsecs_t kIgnoreDelay = ms2ns(500); | 
|  | 1033 | if (systemTime() - mLastSwapTime > kIgnoreDelay) { | 
|  | 1034 | resyncToHardwareVsync(false); | 
|  | 1035 | } | 
|  | 1036 | } | 
|  | 1037 |  | 
| Steven Thomas | 3cfac28 | 2017-02-06 12:29:30 -0800 | [diff] [blame] | 1038 | void SurfaceFlinger::onVSyncReceived(HWComposer* /*composer*/, int type, | 
|  | 1039 | nsecs_t timestamp) { | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 1040 | bool needsHwVsync = false; | 
|  | 1041 |  | 
|  | 1042 | { // Scope for the lock | 
|  | 1043 | Mutex::Autolock _l(mHWVsyncLock); | 
|  | 1044 | if (type == 0 && mPrimaryHWVsyncEnabled) { | 
|  | 1045 | needsHwVsync = mPrimaryDispSync.addResyncSample(timestamp); | 
|  | 1046 | } | 
|  | 1047 | } | 
|  | 1048 |  | 
|  | 1049 | if (needsHwVsync) { | 
|  | 1050 | enableHardwareVsync(); | 
|  | 1051 | } else { | 
|  | 1052 | disableHardwareVsync(false); | 
|  | 1053 | } | 
|  | 1054 | } | 
|  | 1055 |  | 
| Brian Anderson | 0a61b0c | 2016-12-07 14:55:56 -0800 | [diff] [blame] | 1056 | void SurfaceFlinger::getCompositorTiming(CompositorTiming* compositorTiming) { | 
| Brian Anderson | d001058 | 2017-03-07 13:20:31 -0800 | [diff] [blame] | 1057 | std::lock_guard<std::mutex> lock(mCompositorTimingLock); | 
| Brian Anderson | 0a61b0c | 2016-12-07 14:55:56 -0800 | [diff] [blame] | 1058 | *compositorTiming = mCompositorTiming; | 
|  | 1059 | } | 
|  | 1060 |  | 
| Stephen Kiazyk | 8d6c16d | 2017-04-05 16:46:49 -0700 | [diff] [blame] | 1061 | void SurfaceFlinger::onHotplugReceived(HWComposer* /*composer*/, int type, bool connected) { | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 1062 | if (mEventThread == NULL) { | 
|  | 1063 | // This is a temporary workaround for b/7145521.  A non-null pointer | 
|  | 1064 | // does not mean EventThread has finished initializing, so this | 
|  | 1065 | // is not a correct fix. | 
|  | 1066 | ALOGW("WARNING: EventThread not started, ignoring hotplug"); | 
|  | 1067 | return; | 
|  | 1068 | } | 
|  | 1069 |  | 
|  | 1070 | if (uint32_t(type) < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) { | 
|  | 1071 | Mutex::Autolock _l(mStateLock); | 
|  | 1072 | if (connected) { | 
|  | 1073 | createBuiltinDisplayLocked((DisplayDevice::DisplayType)type); | 
|  | 1074 | } else { | 
|  | 1075 | mCurrentState.displays.removeItem(mBuiltinDisplays[type]); | 
|  | 1076 | mBuiltinDisplays[type].clear(); | 
|  | 1077 | } | 
|  | 1078 | setTransactionFlags(eDisplayTransactionNeeded); | 
|  | 1079 |  | 
|  | 1080 | // Defer EventThread notification until SF has updated mDisplays. | 
|  | 1081 | } | 
|  | 1082 | } | 
|  | 1083 |  | 
| Steven Thomas | 3cfac28 | 2017-02-06 12:29:30 -0800 | [diff] [blame] | 1084 | void SurfaceFlinger::onInvalidateReceived(HWComposer* /*composer*/) { | 
|  | 1085 | repaintEverything(); | 
|  | 1086 | } | 
|  | 1087 |  | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 1088 | void SurfaceFlinger::eventControl(int disp, int event, int enabled) { | 
|  | 1089 | ATRACE_CALL(); | 
|  | 1090 | getHwComposer().eventControl(disp, event, enabled); | 
|  | 1091 | } | 
|  | 1092 |  | 
|  | 1093 | void SurfaceFlinger::onMessageReceived(int32_t what) { | 
|  | 1094 | ATRACE_CALL(); | 
|  | 1095 | switch (what) { | 
|  | 1096 | case MessageQueue::INVALIDATE: { | 
|  | 1097 | bool refreshNeeded = handleMessageTransaction(); | 
|  | 1098 | refreshNeeded |= handleMessageInvalidate(); | 
|  | 1099 | refreshNeeded |= mRepaintEverything; | 
|  | 1100 | if (refreshNeeded) { | 
|  | 1101 | // Signal a refresh if a transaction modified the window state, | 
|  | 1102 | // a new buffer was latched, or if HWC has requested a full | 
|  | 1103 | // repaint | 
|  | 1104 | signalRefresh(); | 
|  | 1105 | } | 
|  | 1106 | break; | 
|  | 1107 | } | 
|  | 1108 | case MessageQueue::REFRESH: { | 
|  | 1109 | handleMessageRefresh(); | 
|  | 1110 | break; | 
|  | 1111 | } | 
|  | 1112 | } | 
|  | 1113 | } | 
|  | 1114 |  | 
|  | 1115 | bool SurfaceFlinger::handleMessageTransaction() { | 
| Fabien Sanglard | c8251eb | 2016-12-07 13:59:48 -0800 | [diff] [blame] | 1116 | uint32_t transactionFlags = peekTransactionFlags(); | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 1117 | if (transactionFlags) { | 
|  | 1118 | handleTransaction(transactionFlags); | 
|  | 1119 | return true; | 
|  | 1120 | } | 
|  | 1121 | return false; | 
|  | 1122 | } | 
|  | 1123 |  | 
|  | 1124 | bool SurfaceFlinger::handleMessageInvalidate() { | 
|  | 1125 | ATRACE_CALL(); | 
|  | 1126 | return handlePageFlip(); | 
|  | 1127 | } | 
|  | 1128 |  | 
|  | 1129 | void SurfaceFlinger::handleMessageRefresh() { | 
|  | 1130 | ATRACE_CALL(); | 
|  | 1131 |  | 
|  | 1132 | nsecs_t refreshStartTime = systemTime(SYSTEM_TIME_MONOTONIC); | 
|  | 1133 |  | 
| Brian Anderson | d6927fb | 2016-07-23 23:37:30 -0700 | [diff] [blame] | 1134 | preComposition(refreshStartTime); | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 1135 | rebuildLayerStacks(); | 
|  | 1136 | setUpHWComposer(); | 
|  | 1137 | doDebugFlashRegions(); | 
|  | 1138 | doComposition(); | 
| Brian Anderson | 0a61b0c | 2016-12-07 14:55:56 -0800 | [diff] [blame] | 1139 | postComposition(refreshStartTime); | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 1140 | } | 
|  | 1141 |  | 
|  | 1142 | void SurfaceFlinger::doDebugFlashRegions() | 
|  | 1143 | { | 
|  | 1144 | // is debugging enabled | 
|  | 1145 | if (CC_LIKELY(!mDebugRegion)) | 
|  | 1146 | return; | 
|  | 1147 |  | 
|  | 1148 | const bool repaintEverything = mRepaintEverything; | 
|  | 1149 | for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) { | 
|  | 1150 | const sp<DisplayDevice>& hw(mDisplays[dpy]); | 
|  | 1151 | if (hw->isDisplayOn()) { | 
|  | 1152 | // transform the dirty region into this screen's coordinate space | 
|  | 1153 | const Region dirtyRegion(hw->getDirtyRegion(repaintEverything)); | 
|  | 1154 | if (!dirtyRegion.isEmpty()) { | 
|  | 1155 | // redraw the whole screen | 
|  | 1156 | doComposeSurfaces(hw, Region(hw->bounds())); | 
|  | 1157 |  | 
|  | 1158 | // and draw the dirty region | 
|  | 1159 | const int32_t height = hw->getHeight(); | 
|  | 1160 | RenderEngine& engine(getRenderEngine()); | 
|  | 1161 | engine.fillRegionWithColor(dirtyRegion, height, 1, 0, 1, 1); | 
|  | 1162 |  | 
|  | 1163 | hw->compositionComplete(); | 
|  | 1164 | hw->swapBuffers(getHwComposer()); | 
|  | 1165 | } | 
|  | 1166 | } | 
|  | 1167 | } | 
|  | 1168 |  | 
|  | 1169 | postFramebuffer(); | 
|  | 1170 |  | 
|  | 1171 | if (mDebugRegion > 1) { | 
|  | 1172 | usleep(mDebugRegion * 1000); | 
|  | 1173 | } | 
|  | 1174 |  | 
|  | 1175 | HWComposer& hwc(getHwComposer()); | 
|  | 1176 | if (hwc.initCheck() == NO_ERROR) { | 
|  | 1177 | status_t err = hwc.prepare(); | 
|  | 1178 | ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err)); | 
|  | 1179 | } | 
|  | 1180 | } | 
|  | 1181 |  | 
| Brian Anderson | d6927fb | 2016-07-23 23:37:30 -0700 | [diff] [blame] | 1182 | void SurfaceFlinger::preComposition(nsecs_t refreshStartTime) | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 1183 | { | 
|  | 1184 | bool needExtraInvalidate = false; | 
| Robert Carr | 2047fae | 2016-11-28 14:09:09 -0800 | [diff] [blame] | 1185 | mDrawingState.traverseInZOrder([&](Layer* layer) { | 
|  | 1186 | if (layer->onPreComposition(refreshStartTime)) { | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 1187 | needExtraInvalidate = true; | 
|  | 1188 | } | 
| Robert Carr | 2047fae | 2016-11-28 14:09:09 -0800 | [diff] [blame] | 1189 | }); | 
|  | 1190 |  | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 1191 | if (needExtraInvalidate) { | 
|  | 1192 | signalLayerUpdate(); | 
|  | 1193 | } | 
|  | 1194 | } | 
|  | 1195 |  | 
| Brian Anderson | 0a61b0c | 2016-12-07 14:55:56 -0800 | [diff] [blame] | 1196 | void SurfaceFlinger::updateCompositorTiming( | 
|  | 1197 | nsecs_t vsyncPhase, nsecs_t vsyncInterval, nsecs_t compositeTime, | 
|  | 1198 | std::shared_ptr<FenceTime>& presentFenceTime) { | 
|  | 1199 | // Update queue of past composite+present times and determine the | 
|  | 1200 | // most recently known composite to present latency. | 
|  | 1201 | mCompositePresentTimes.push({compositeTime, presentFenceTime}); | 
|  | 1202 | nsecs_t compositeToPresentLatency = -1; | 
|  | 1203 | while (!mCompositePresentTimes.empty()) { | 
|  | 1204 | CompositePresentTime& cpt = mCompositePresentTimes.front(); | 
|  | 1205 | // Cached values should have been updated before calling this method, | 
|  | 1206 | // which helps avoid duplicate syscalls. | 
|  | 1207 | nsecs_t displayTime = cpt.display->getCachedSignalTime(); | 
|  | 1208 | if (displayTime == Fence::SIGNAL_TIME_PENDING) { | 
|  | 1209 | break; | 
|  | 1210 | } | 
|  | 1211 | compositeToPresentLatency = displayTime - cpt.composite; | 
|  | 1212 | mCompositePresentTimes.pop(); | 
|  | 1213 | } | 
|  | 1214 |  | 
|  | 1215 | // Don't let mCompositePresentTimes grow unbounded, just in case. | 
|  | 1216 | while (mCompositePresentTimes.size() > 16) { | 
|  | 1217 | mCompositePresentTimes.pop(); | 
|  | 1218 | } | 
|  | 1219 |  | 
| Brian Anderson | d001058 | 2017-03-07 13:20:31 -0800 | [diff] [blame] | 1220 | setCompositorTimingSnapped( | 
|  | 1221 | vsyncPhase, vsyncInterval, compositeToPresentLatency); | 
|  | 1222 | } | 
|  | 1223 |  | 
|  | 1224 | void SurfaceFlinger::setCompositorTimingSnapped(nsecs_t vsyncPhase, | 
|  | 1225 | nsecs_t vsyncInterval, nsecs_t compositeToPresentLatency) { | 
| Brian Anderson | 0a61b0c | 2016-12-07 14:55:56 -0800 | [diff] [blame] | 1226 | // Integer division and modulo round toward 0 not -inf, so we need to | 
|  | 1227 | // treat negative and positive offsets differently. | 
| Brian Anderson | d001058 | 2017-03-07 13:20:31 -0800 | [diff] [blame] | 1228 | nsecs_t idealLatency = (sfVsyncPhaseOffsetNs > 0) ? | 
| Brian Anderson | 0a61b0c | 2016-12-07 14:55:56 -0800 | [diff] [blame] | 1229 | (vsyncInterval - (sfVsyncPhaseOffsetNs % vsyncInterval)) : | 
|  | 1230 | ((-sfVsyncPhaseOffsetNs) % vsyncInterval); | 
|  | 1231 |  | 
| Brian Anderson | d001058 | 2017-03-07 13:20:31 -0800 | [diff] [blame] | 1232 | // Just in case sfVsyncPhaseOffsetNs == -vsyncInterval. | 
|  | 1233 | if (idealLatency <= 0) { | 
|  | 1234 | idealLatency = vsyncInterval; | 
|  | 1235 | } | 
|  | 1236 |  | 
| Brian Anderson | 0a61b0c | 2016-12-07 14:55:56 -0800 | [diff] [blame] | 1237 | // Snap the latency to a value that removes scheduling jitter from the | 
|  | 1238 | // composition and present times, which often have >1ms of jitter. | 
|  | 1239 | // Reducing jitter is important if an app attempts to extrapolate | 
|  | 1240 | // something (such as user input) to an accurate diasplay time. | 
|  | 1241 | // Snapping also allows an app to precisely calculate sfVsyncPhaseOffsetNs | 
|  | 1242 | // with (presentLatency % interval). | 
| Brian Anderson | d001058 | 2017-03-07 13:20:31 -0800 | [diff] [blame] | 1243 | nsecs_t bias = vsyncInterval / 2; | 
|  | 1244 | int64_t extraVsyncs = | 
|  | 1245 | (compositeToPresentLatency - idealLatency + bias) / vsyncInterval; | 
|  | 1246 | nsecs_t snappedCompositeToPresentLatency = (extraVsyncs > 0) ? | 
|  | 1247 | idealLatency + (extraVsyncs * vsyncInterval) : idealLatency; | 
| Brian Anderson | 0a61b0c | 2016-12-07 14:55:56 -0800 | [diff] [blame] | 1248 |  | 
| Brian Anderson | d001058 | 2017-03-07 13:20:31 -0800 | [diff] [blame] | 1249 | std::lock_guard<std::mutex> lock(mCompositorTimingLock); | 
| Brian Anderson | 0a61b0c | 2016-12-07 14:55:56 -0800 | [diff] [blame] | 1250 | mCompositorTiming.deadline = vsyncPhase - idealLatency; | 
|  | 1251 | mCompositorTiming.interval = vsyncInterval; | 
| Brian Anderson | d001058 | 2017-03-07 13:20:31 -0800 | [diff] [blame] | 1252 | mCompositorTiming.presentLatency = snappedCompositeToPresentLatency; | 
| Brian Anderson | 0a61b0c | 2016-12-07 14:55:56 -0800 | [diff] [blame] | 1253 | } | 
|  | 1254 |  | 
|  | 1255 | void SurfaceFlinger::postComposition(nsecs_t refreshStartTime) | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 1256 | { | 
| Brian Anderson | d6927fb | 2016-07-23 23:37:30 -0700 | [diff] [blame] | 1257 | const HWComposer& hwc = getHwComposer(); | 
|  | 1258 | const sp<const DisplayDevice> hw(getDefaultDisplayDevice()); | 
|  | 1259 |  | 
| Brian Anderson | 3d4039d | 2016-09-23 16:31:30 -0700 | [diff] [blame] | 1260 | std::shared_ptr<FenceTime> glCompositionDoneFenceTime; | 
|  | 1261 | if (getHwComposer().hasGlesComposition(hw->getHwcDisplayId())) { | 
|  | 1262 | glCompositionDoneFenceTime = | 
|  | 1263 | std::make_shared<FenceTime>(hw->getClientTargetAcquireFence()); | 
|  | 1264 | mGlCompositionDoneTimeline.push(glCompositionDoneFenceTime); | 
|  | 1265 | } else { | 
|  | 1266 | glCompositionDoneFenceTime = FenceTime::NO_FENCE; | 
|  | 1267 | } | 
|  | 1268 | mGlCompositionDoneTimeline.updateSignalTimes(); | 
|  | 1269 |  | 
| Brian Anderson | 4e606e3 | 2017-03-16 15:34:57 -0700 | [diff] [blame] | 1270 | sp<Fence> retireFence = mHwc->getDisplayFence(HWC_DISPLAY_PRIMARY); | 
|  | 1271 | auto retireFenceTime = std::make_shared<FenceTime>(retireFence); | 
| Brian Anderson | 3d4039d | 2016-09-23 16:31:30 -0700 | [diff] [blame] | 1272 | mDisplayTimeline.push(retireFenceTime); | 
|  | 1273 | mDisplayTimeline.updateSignalTimes(); | 
| Brian Anderson | d6927fb | 2016-07-23 23:37:30 -0700 | [diff] [blame] | 1274 |  | 
| Brian Anderson | 0a61b0c | 2016-12-07 14:55:56 -0800 | [diff] [blame] | 1275 | nsecs_t vsyncPhase = mPrimaryDispSync.computeNextRefresh(0); | 
|  | 1276 | nsecs_t vsyncInterval = mPrimaryDispSync.getPeriod(); | 
|  | 1277 |  | 
|  | 1278 | // We use the refreshStartTime which might be sampled a little later than | 
|  | 1279 | // when we started doing work for this frame, but that should be okay | 
|  | 1280 | // since updateCompositorTiming has snapping logic. | 
|  | 1281 | updateCompositorTiming( | 
|  | 1282 | vsyncPhase, vsyncInterval, refreshStartTime, retireFenceTime); | 
| Brian Anderson | d001058 | 2017-03-07 13:20:31 -0800 | [diff] [blame] | 1283 | CompositorTiming compositorTiming; | 
|  | 1284 | { | 
|  | 1285 | std::lock_guard<std::mutex> lock(mCompositorTimingLock); | 
|  | 1286 | compositorTiming = mCompositorTiming; | 
|  | 1287 | } | 
| Brian Anderson | 0a61b0c | 2016-12-07 14:55:56 -0800 | [diff] [blame] | 1288 |  | 
| Robert Carr | 2047fae | 2016-11-28 14:09:09 -0800 | [diff] [blame] | 1289 | mDrawingState.traverseInZOrder([&](Layer* layer) { | 
| Brian Anderson | 4e606e3 | 2017-03-16 15:34:57 -0700 | [diff] [blame] | 1290 | // TODO(brianderson): The retire fence is incorrectly passed in as the | 
|  | 1291 | // present fence. Fix this if this file lives on. | 
| Robert Carr | 2047fae | 2016-11-28 14:09:09 -0800 | [diff] [blame] | 1292 | bool frameLatched = layer->onPostComposition(glCompositionDoneFenceTime, | 
| Brian Anderson | 4e606e3 | 2017-03-16 15:34:57 -0700 | [diff] [blame] | 1293 | retireFenceTime, compositorTiming); | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 1294 | if (frameLatched) { | 
| Robert Carr | 2047fae | 2016-11-28 14:09:09 -0800 | [diff] [blame] | 1295 | recordBufferingStats(layer->getName().string(), | 
|  | 1296 | layer->getOccupancyHistory(false)); | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 1297 | } | 
| Robert Carr | 2047fae | 2016-11-28 14:09:09 -0800 | [diff] [blame] | 1298 | }); | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 1299 |  | 
| Brian Anderson | 4e606e3 | 2017-03-16 15:34:57 -0700 | [diff] [blame] | 1300 | if (retireFence->isValid()) { | 
|  | 1301 | if (mPrimaryDispSync.addPresentFence(retireFence)) { | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 1302 | enableHardwareVsync(); | 
|  | 1303 | } else { | 
|  | 1304 | disableHardwareVsync(false); | 
|  | 1305 | } | 
|  | 1306 | } | 
|  | 1307 |  | 
| Fabien Sanglard | cbf153b | 2017-03-10 17:57:12 -0800 | [diff] [blame] | 1308 | if (!hasSyncFramework) { | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 1309 | if (hw->isDisplayOn()) { | 
|  | 1310 | enableHardwareVsync(); | 
|  | 1311 | } | 
|  | 1312 | } | 
|  | 1313 |  | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 1314 | if (mAnimCompositionPending) { | 
|  | 1315 | mAnimCompositionPending = false; | 
|  | 1316 |  | 
| Brian Anderson | 3d4039d | 2016-09-23 16:31:30 -0700 | [diff] [blame] | 1317 | if (retireFenceTime->isValid()) { | 
|  | 1318 | mAnimFrameTracker.setActualPresentFence(std::move(retireFenceTime)); | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 1319 | } else { | 
|  | 1320 | // The HWC doesn't support present fences, so use the refresh | 
|  | 1321 | // timestamp instead. | 
|  | 1322 | nsecs_t presentTime = hwc.getRefreshTimestamp(HWC_DISPLAY_PRIMARY); | 
|  | 1323 | mAnimFrameTracker.setActualPresentTime(presentTime); | 
|  | 1324 | } | 
|  | 1325 | mAnimFrameTracker.advanceFrame(); | 
|  | 1326 | } | 
|  | 1327 |  | 
|  | 1328 | if (hw->getPowerMode() == HWC_POWER_MODE_OFF) { | 
|  | 1329 | return; | 
|  | 1330 | } | 
|  | 1331 |  | 
|  | 1332 | nsecs_t currentTime = systemTime(); | 
|  | 1333 | if (mHasPoweredOff) { | 
|  | 1334 | mHasPoweredOff = false; | 
|  | 1335 | } else { | 
|  | 1336 | nsecs_t period = mPrimaryDispSync.getPeriod(); | 
|  | 1337 | nsecs_t elapsedTime = currentTime - mLastSwapTime; | 
|  | 1338 | size_t numPeriods = static_cast<size_t>(elapsedTime / period); | 
|  | 1339 | if (numPeriods < NUM_BUCKETS - 1) { | 
|  | 1340 | mFrameBuckets[numPeriods] += elapsedTime; | 
|  | 1341 | } else { | 
|  | 1342 | mFrameBuckets[NUM_BUCKETS - 1] += elapsedTime; | 
|  | 1343 | } | 
|  | 1344 | mTotalTime += elapsedTime; | 
|  | 1345 | } | 
|  | 1346 | mLastSwapTime = currentTime; | 
|  | 1347 | } | 
|  | 1348 |  | 
|  | 1349 | void SurfaceFlinger::rebuildLayerStacks() { | 
|  | 1350 | // rebuild the visible layer list per screen | 
|  | 1351 | if (CC_UNLIKELY(mVisibleRegionsDirty)) { | 
|  | 1352 | ATRACE_CALL(); | 
|  | 1353 | mVisibleRegionsDirty = false; | 
|  | 1354 | invalidateHwcGeometry(); | 
|  | 1355 |  | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 1356 | for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) { | 
|  | 1357 | Region opaqueRegion; | 
|  | 1358 | Region dirtyRegion; | 
|  | 1359 | Vector< sp<Layer> > layersSortedByZ; | 
|  | 1360 | const sp<DisplayDevice>& hw(mDisplays[dpy]); | 
|  | 1361 | const Transform& tr(hw->getTransform()); | 
|  | 1362 | const Rect bounds(hw->getBounds()); | 
|  | 1363 | if (hw->isDisplayOn()) { | 
| Robert Carr | 2047fae | 2016-11-28 14:09:09 -0800 | [diff] [blame] | 1364 | computeVisibleRegions(hw->getLayerStack(), dirtyRegion, | 
|  | 1365 | opaqueRegion); | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 1366 |  | 
| Robert Carr | 2047fae | 2016-11-28 14:09:09 -0800 | [diff] [blame] | 1367 | mDrawingState.traverseInZOrder([&](Layer* layer) { | 
| Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 1368 | if (layer->getLayerStack() == hw->getLayerStack()) { | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 1369 | Region drawRegion(tr.transform( | 
|  | 1370 | layer->visibleNonTransparentRegion)); | 
|  | 1371 | drawRegion.andSelf(bounds); | 
|  | 1372 | if (!drawRegion.isEmpty()) { | 
|  | 1373 | layersSortedByZ.add(layer); | 
|  | 1374 | } | 
|  | 1375 | } | 
| Robert Carr | 2047fae | 2016-11-28 14:09:09 -0800 | [diff] [blame] | 1376 | }); | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 1377 | } | 
|  | 1378 | hw->setVisibleLayersSortedByZ(layersSortedByZ); | 
|  | 1379 | hw->undefinedRegion.set(bounds); | 
|  | 1380 | hw->undefinedRegion.subtractSelf(tr.transform(opaqueRegion)); | 
|  | 1381 | hw->dirtyRegion.orSelf(dirtyRegion); | 
|  | 1382 | } | 
|  | 1383 | } | 
|  | 1384 | } | 
|  | 1385 |  | 
|  | 1386 | void SurfaceFlinger::setUpHWComposer() { | 
|  | 1387 | for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) { | 
|  | 1388 | bool dirty = !mDisplays[dpy]->getDirtyRegion(false).isEmpty(); | 
|  | 1389 | bool empty = mDisplays[dpy]->getVisibleLayersSortedByZ().size() == 0; | 
|  | 1390 | bool wasEmpty = !mDisplays[dpy]->lastCompositionHadVisibleLayers; | 
|  | 1391 |  | 
|  | 1392 | // If nothing has changed (!dirty), don't recompose. | 
|  | 1393 | // If something changed, but we don't currently have any visible layers, | 
|  | 1394 | //   and didn't when we last did a composition, then skip it this time. | 
|  | 1395 | // The second rule does two things: | 
|  | 1396 | // - When all layers are removed from a display, we'll emit one black | 
|  | 1397 | //   frame, then nothing more until we get new layers. | 
|  | 1398 | // - When a display is created with a private layer stack, we won't | 
|  | 1399 | //   emit any black frames until a layer is added to the layer stack. | 
|  | 1400 | bool mustRecompose = dirty && !(empty && wasEmpty); | 
|  | 1401 |  | 
|  | 1402 | ALOGV_IF(mDisplays[dpy]->getDisplayType() == DisplayDevice::DISPLAY_VIRTUAL, | 
|  | 1403 | "dpy[%zu]: %s composition (%sdirty %sempty %swasEmpty)", dpy, | 
|  | 1404 | mustRecompose ? "doing" : "skipping", | 
|  | 1405 | dirty ? "+" : "-", | 
|  | 1406 | empty ? "+" : "-", | 
|  | 1407 | wasEmpty ? "+" : "-"); | 
|  | 1408 |  | 
|  | 1409 | mDisplays[dpy]->beginFrame(mustRecompose); | 
|  | 1410 |  | 
|  | 1411 | if (mustRecompose) { | 
|  | 1412 | mDisplays[dpy]->lastCompositionHadVisibleLayers = !empty; | 
|  | 1413 | } | 
|  | 1414 | } | 
|  | 1415 |  | 
|  | 1416 | HWComposer& hwc(getHwComposer()); | 
|  | 1417 | if (hwc.initCheck() == NO_ERROR) { | 
|  | 1418 | // build the h/w work list | 
|  | 1419 | if (CC_UNLIKELY(mHwWorkListDirty)) { | 
|  | 1420 | mHwWorkListDirty = false; | 
|  | 1421 | for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) { | 
|  | 1422 | sp<const DisplayDevice> hw(mDisplays[dpy]); | 
|  | 1423 | const int32_t id = hw->getHwcDisplayId(); | 
|  | 1424 | if (id >= 0) { | 
|  | 1425 | const Vector< sp<Layer> >& currentLayers( | 
|  | 1426 | hw->getVisibleLayersSortedByZ()); | 
|  | 1427 | const size_t count = currentLayers.size(); | 
|  | 1428 | if (hwc.createWorkList(id, count) == NO_ERROR) { | 
|  | 1429 | HWComposer::LayerListIterator cur = hwc.begin(id); | 
|  | 1430 | const HWComposer::LayerListIterator end = hwc.end(id); | 
|  | 1431 | for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) { | 
|  | 1432 | const sp<Layer>& layer(currentLayers[i]); | 
|  | 1433 | layer->setGeometry(hw, *cur); | 
|  | 1434 | if (mDebugDisableHWC || mDebugRegion || mDaltonize || mHasColorMatrix) { | 
|  | 1435 | cur->setSkip(true); | 
|  | 1436 | } | 
|  | 1437 | } | 
|  | 1438 | } | 
|  | 1439 | } | 
|  | 1440 | } | 
|  | 1441 | } | 
|  | 1442 |  | 
|  | 1443 | // set the per-frame data | 
|  | 1444 | for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) { | 
|  | 1445 | sp<const DisplayDevice> hw(mDisplays[dpy]); | 
|  | 1446 | const int32_t id = hw->getHwcDisplayId(); | 
|  | 1447 | if (id >= 0) { | 
|  | 1448 | const Vector< sp<Layer> >& currentLayers( | 
|  | 1449 | hw->getVisibleLayersSortedByZ()); | 
|  | 1450 | const size_t count = currentLayers.size(); | 
|  | 1451 | HWComposer::LayerListIterator cur = hwc.begin(id); | 
|  | 1452 | const HWComposer::LayerListIterator end = hwc.end(id); | 
|  | 1453 | for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) { | 
|  | 1454 | /* | 
|  | 1455 | * update the per-frame h/w composer data for each layer | 
|  | 1456 | * and build the transparent region of the FB | 
|  | 1457 | */ | 
|  | 1458 | const sp<Layer>& layer(currentLayers[i]); | 
|  | 1459 | layer->setPerFrameData(hw, *cur); | 
|  | 1460 | } | 
|  | 1461 | } | 
|  | 1462 | } | 
|  | 1463 |  | 
|  | 1464 | // If possible, attempt to use the cursor overlay on each display. | 
|  | 1465 | for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) { | 
|  | 1466 | sp<const DisplayDevice> hw(mDisplays[dpy]); | 
|  | 1467 | const int32_t id = hw->getHwcDisplayId(); | 
|  | 1468 | if (id >= 0) { | 
|  | 1469 | const Vector< sp<Layer> >& currentLayers( | 
|  | 1470 | hw->getVisibleLayersSortedByZ()); | 
|  | 1471 | const size_t count = currentLayers.size(); | 
|  | 1472 | HWComposer::LayerListIterator cur = hwc.begin(id); | 
|  | 1473 | const HWComposer::LayerListIterator end = hwc.end(id); | 
|  | 1474 | for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) { | 
|  | 1475 | const sp<Layer>& layer(currentLayers[i]); | 
|  | 1476 | if (layer->isPotentialCursor()) { | 
|  | 1477 | cur->setIsCursorLayerHint(); | 
|  | 1478 | break; | 
|  | 1479 | } | 
|  | 1480 | } | 
|  | 1481 | } | 
|  | 1482 | } | 
|  | 1483 |  | 
|  | 1484 | status_t err = hwc.prepare(); | 
|  | 1485 | ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err)); | 
|  | 1486 |  | 
|  | 1487 | for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) { | 
|  | 1488 | sp<const DisplayDevice> hw(mDisplays[dpy]); | 
|  | 1489 | hw->prepareFrame(hwc); | 
|  | 1490 | } | 
|  | 1491 | } | 
|  | 1492 | } | 
|  | 1493 |  | 
|  | 1494 | void SurfaceFlinger::doComposition() { | 
|  | 1495 | ATRACE_CALL(); | 
|  | 1496 | const bool repaintEverything = android_atomic_and(0, &mRepaintEverything); | 
|  | 1497 | for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) { | 
|  | 1498 | const sp<DisplayDevice>& hw(mDisplays[dpy]); | 
|  | 1499 | if (hw->isDisplayOn()) { | 
|  | 1500 | // transform the dirty region into this screen's coordinate space | 
|  | 1501 | const Region dirtyRegion(hw->getDirtyRegion(repaintEverything)); | 
|  | 1502 |  | 
|  | 1503 | // repaint the framebuffer (if needed) | 
|  | 1504 | doDisplayComposition(hw, dirtyRegion); | 
|  | 1505 |  | 
|  | 1506 | hw->dirtyRegion.clear(); | 
|  | 1507 | hw->flip(hw->swapRegion); | 
|  | 1508 | hw->swapRegion.clear(); | 
|  | 1509 | } | 
|  | 1510 | // inform the h/w that we're done compositing | 
|  | 1511 | hw->compositionComplete(); | 
|  | 1512 | } | 
|  | 1513 | postFramebuffer(); | 
|  | 1514 | } | 
|  | 1515 |  | 
|  | 1516 | void SurfaceFlinger::postFramebuffer() | 
|  | 1517 | { | 
|  | 1518 | ATRACE_CALL(); | 
|  | 1519 |  | 
|  | 1520 | const nsecs_t now = systemTime(); | 
|  | 1521 | mDebugInSwapBuffers = now; | 
|  | 1522 |  | 
|  | 1523 | HWComposer& hwc(getHwComposer()); | 
|  | 1524 | if (hwc.initCheck() == NO_ERROR) { | 
|  | 1525 | if (!hwc.supportsFramebufferTarget()) { | 
|  | 1526 | // EGL spec says: | 
|  | 1527 | //   "surface must be bound to the calling thread's current context, | 
|  | 1528 | //    for the current rendering API." | 
|  | 1529 | getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext); | 
|  | 1530 | } | 
|  | 1531 | hwc.commit(); | 
|  | 1532 | } | 
|  | 1533 |  | 
|  | 1534 | // make the default display current because the VirtualDisplayDevice code cannot | 
|  | 1535 | // deal with dequeueBuffer() being called outside of the composition loop; however | 
|  | 1536 | // the code below can call glFlush() which is allowed (and does in some case) call | 
|  | 1537 | // dequeueBuffer(). | 
|  | 1538 | getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext); | 
|  | 1539 |  | 
|  | 1540 | for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) { | 
|  | 1541 | sp<const DisplayDevice> hw(mDisplays[dpy]); | 
|  | 1542 | const Vector< sp<Layer> >& currentLayers(hw->getVisibleLayersSortedByZ()); | 
|  | 1543 | hw->onSwapBuffersCompleted(hwc); | 
|  | 1544 | const size_t count = currentLayers.size(); | 
|  | 1545 | int32_t id = hw->getHwcDisplayId(); | 
|  | 1546 | if (id >=0 && hwc.initCheck() == NO_ERROR) { | 
|  | 1547 | HWComposer::LayerListIterator cur = hwc.begin(id); | 
|  | 1548 | const HWComposer::LayerListIterator end = hwc.end(id); | 
|  | 1549 | for (size_t i = 0; cur != end && i < count; ++i, ++cur) { | 
|  | 1550 | currentLayers[i]->onLayerDisplayed(hw, &*cur); | 
|  | 1551 | } | 
|  | 1552 | } else { | 
|  | 1553 | for (size_t i = 0; i < count; i++) { | 
|  | 1554 | currentLayers[i]->onLayerDisplayed(hw, NULL); | 
|  | 1555 | } | 
|  | 1556 | } | 
|  | 1557 | } | 
|  | 1558 |  | 
|  | 1559 | mLastSwapBufferTime = systemTime() - now; | 
|  | 1560 | mDebugInSwapBuffers = 0; | 
|  | 1561 |  | 
|  | 1562 | uint32_t flipCount = getDefaultDisplayDevice()->getPageFlipCount(); | 
|  | 1563 | if (flipCount % LOG_FRAME_STATS_PERIOD == 0) { | 
|  | 1564 | logFrameStats(); | 
|  | 1565 | } | 
|  | 1566 | } | 
|  | 1567 |  | 
|  | 1568 | void SurfaceFlinger::handleTransaction(uint32_t transactionFlags) | 
|  | 1569 | { | 
|  | 1570 | ATRACE_CALL(); | 
|  | 1571 |  | 
|  | 1572 | // here we keep a copy of the drawing state (that is the state that's | 
|  | 1573 | // going to be overwritten by handleTransactionLocked()) outside of | 
|  | 1574 | // mStateLock so that the side-effects of the State assignment | 
|  | 1575 | // don't happen with mStateLock held (which can cause deadlocks). | 
|  | 1576 | State drawingState(mDrawingState); | 
|  | 1577 |  | 
|  | 1578 | Mutex::Autolock _l(mStateLock); | 
|  | 1579 | const nsecs_t now = systemTime(); | 
|  | 1580 | mDebugInTransaction = now; | 
|  | 1581 |  | 
|  | 1582 | // Here we're guaranteed that some transaction flags are set | 
|  | 1583 | // so we can call handleTransactionLocked() unconditionally. | 
|  | 1584 | // We call getTransactionFlags(), which will also clear the flags, | 
|  | 1585 | // with mStateLock held to guarantee that mCurrentState won't change | 
|  | 1586 | // until the transaction is committed. | 
|  | 1587 |  | 
|  | 1588 | transactionFlags = getTransactionFlags(eTransactionMask); | 
|  | 1589 | handleTransactionLocked(transactionFlags); | 
|  | 1590 |  | 
|  | 1591 | mLastTransactionTime = systemTime() - now; | 
|  | 1592 | mDebugInTransaction = 0; | 
|  | 1593 | invalidateHwcGeometry(); | 
|  | 1594 | // here the transaction has been committed | 
|  | 1595 | } | 
|  | 1596 |  | 
|  | 1597 | void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags) | 
|  | 1598 | { | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 1599 | // Notify all layers of available frames | 
| Robert Carr | 2047fae | 2016-11-28 14:09:09 -0800 | [diff] [blame] | 1600 | mCurrentState.traverseInZOrder([](Layer* layer) { | 
|  | 1601 | layer->notifyAvailableFrames(); | 
|  | 1602 | }); | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 1603 |  | 
|  | 1604 | /* | 
|  | 1605 | * Traversal of the children | 
|  | 1606 | * (perform the transaction for each of them if needed) | 
|  | 1607 | */ | 
|  | 1608 |  | 
|  | 1609 | if (transactionFlags & eTraversalNeeded) { | 
| Robert Carr | 2047fae | 2016-11-28 14:09:09 -0800 | [diff] [blame] | 1610 | mCurrentState.traverseInZOrder([&](Layer* layer) { | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 1611 | uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded); | 
| Robert Carr | 2047fae | 2016-11-28 14:09:09 -0800 | [diff] [blame] | 1612 | if (!trFlags) return; | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 1613 |  | 
|  | 1614 | const uint32_t flags = layer->doTransaction(0); | 
|  | 1615 | if (flags & Layer::eVisibleRegion) | 
|  | 1616 | mVisibleRegionsDirty = true; | 
| Robert Carr | 2047fae | 2016-11-28 14:09:09 -0800 | [diff] [blame] | 1617 | }); | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 1618 | } | 
|  | 1619 |  | 
|  | 1620 | /* | 
|  | 1621 | * Perform display own transactions if needed | 
|  | 1622 | */ | 
|  | 1623 |  | 
|  | 1624 | if (transactionFlags & eDisplayTransactionNeeded) { | 
|  | 1625 | // here we take advantage of Vector's copy-on-write semantics to | 
|  | 1626 | // improve performance by skipping the transaction entirely when | 
|  | 1627 | // know that the lists are identical | 
|  | 1628 | const KeyedVector<  wp<IBinder>, DisplayDeviceState>& curr(mCurrentState.displays); | 
|  | 1629 | const KeyedVector<  wp<IBinder>, DisplayDeviceState>& draw(mDrawingState.displays); | 
|  | 1630 | if (!curr.isIdenticalTo(draw)) { | 
|  | 1631 | mVisibleRegionsDirty = true; | 
|  | 1632 | const size_t cc = curr.size(); | 
|  | 1633 | size_t dc = draw.size(); | 
|  | 1634 |  | 
|  | 1635 | // find the displays that were removed | 
|  | 1636 | // (ie: in drawing state but not in current state) | 
|  | 1637 | // also handle displays that changed | 
|  | 1638 | // (ie: displays that are in both lists) | 
|  | 1639 | for (size_t i=0 ; i<dc ; i++) { | 
|  | 1640 | const ssize_t j = curr.indexOfKey(draw.keyAt(i)); | 
|  | 1641 | if (j < 0) { | 
|  | 1642 | // in drawing state but not in current state | 
|  | 1643 | if (!draw[i].isMainDisplay()) { | 
|  | 1644 | // Call makeCurrent() on the primary display so we can | 
|  | 1645 | // be sure that nothing associated with this display | 
|  | 1646 | // is current. | 
|  | 1647 | const sp<const DisplayDevice> defaultDisplay(getDefaultDisplayDevice()); | 
|  | 1648 | defaultDisplay->makeCurrent(mEGLDisplay, mEGLContext); | 
|  | 1649 | sp<DisplayDevice> hw(getDisplayDevice(draw.keyAt(i))); | 
|  | 1650 | if (hw != NULL) | 
|  | 1651 | hw->disconnect(getHwComposer()); | 
|  | 1652 | if (draw[i].type < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) | 
|  | 1653 | mEventThread->onHotplugReceived(draw[i].type, false); | 
|  | 1654 | mDisplays.removeItem(draw.keyAt(i)); | 
|  | 1655 | } else { | 
|  | 1656 | ALOGW("trying to remove the main display"); | 
|  | 1657 | } | 
|  | 1658 | } else { | 
|  | 1659 | // this display is in both lists. see if something changed. | 
|  | 1660 | const DisplayDeviceState& state(curr[j]); | 
|  | 1661 | const wp<IBinder>& display(curr.keyAt(j)); | 
|  | 1662 | const sp<IBinder> state_binder = IInterface::asBinder(state.surface); | 
|  | 1663 | const sp<IBinder> draw_binder = IInterface::asBinder(draw[i].surface); | 
|  | 1664 | if (state_binder != draw_binder) { | 
|  | 1665 | // changing the surface is like destroying and | 
|  | 1666 | // recreating the DisplayDevice, so we just remove it | 
|  | 1667 | // from the drawing state, so that it get re-added | 
|  | 1668 | // below. | 
|  | 1669 | sp<DisplayDevice> hw(getDisplayDevice(display)); | 
|  | 1670 | if (hw != NULL) | 
|  | 1671 | hw->disconnect(getHwComposer()); | 
|  | 1672 | mDisplays.removeItem(display); | 
|  | 1673 | mDrawingState.displays.removeItemsAt(i); | 
|  | 1674 | dc--; i--; | 
|  | 1675 | // at this point we must loop to the next item | 
|  | 1676 | continue; | 
|  | 1677 | } | 
|  | 1678 |  | 
|  | 1679 | const sp<DisplayDevice> disp(getDisplayDevice(display)); | 
|  | 1680 | if (disp != NULL) { | 
|  | 1681 | if (state.layerStack != draw[i].layerStack) { | 
|  | 1682 | disp->setLayerStack(state.layerStack); | 
|  | 1683 | } | 
|  | 1684 | if ((state.orientation != draw[i].orientation) | 
|  | 1685 | || (state.viewport != draw[i].viewport) | 
|  | 1686 | || (state.frame != draw[i].frame)) | 
|  | 1687 | { | 
|  | 1688 | disp->setProjection(state.orientation, | 
|  | 1689 | state.viewport, state.frame); | 
|  | 1690 | } | 
|  | 1691 | if (state.width != draw[i].width || state.height != draw[i].height) { | 
|  | 1692 | disp->setDisplaySize(state.width, state.height); | 
|  | 1693 | } | 
|  | 1694 | } | 
|  | 1695 | } | 
|  | 1696 | } | 
|  | 1697 |  | 
|  | 1698 | // find displays that were added | 
|  | 1699 | // (ie: in current state but not in drawing state) | 
|  | 1700 | for (size_t i=0 ; i<cc ; i++) { | 
|  | 1701 | if (draw.indexOfKey(curr.keyAt(i)) < 0) { | 
|  | 1702 | const DisplayDeviceState& state(curr[i]); | 
|  | 1703 |  | 
|  | 1704 | sp<DisplaySurface> dispSurface; | 
|  | 1705 | sp<IGraphicBufferProducer> producer; | 
|  | 1706 | sp<IGraphicBufferProducer> bqProducer; | 
|  | 1707 | sp<IGraphicBufferConsumer> bqConsumer; | 
| Romain Guy | f8b4ca5 | 2017-03-16 18:39:20 +0000 | [diff] [blame] | 1708 | BufferQueue::createBufferQueue(&bqProducer, &bqConsumer, | 
|  | 1709 | new GraphicBufferAlloc()); | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 1710 |  | 
|  | 1711 | int32_t hwcDisplayId = -1; | 
|  | 1712 | if (state.isVirtualDisplay()) { | 
|  | 1713 | // Virtual displays without a surface are dormant: | 
|  | 1714 | // they have external state (layer stack, projection, | 
|  | 1715 | // etc.) but no internal state (i.e. a DisplayDevice). | 
|  | 1716 | if (state.surface != NULL) { | 
|  | 1717 |  | 
|  | 1718 | int width = 0; | 
|  | 1719 | int status = state.surface->query( | 
|  | 1720 | NATIVE_WINDOW_WIDTH, &width); | 
|  | 1721 | ALOGE_IF(status != NO_ERROR, | 
|  | 1722 | "Unable to query width (%d)", status); | 
|  | 1723 | int height = 0; | 
|  | 1724 | status = state.surface->query( | 
|  | 1725 | NATIVE_WINDOW_HEIGHT, &height); | 
|  | 1726 | ALOGE_IF(status != NO_ERROR, | 
|  | 1727 | "Unable to query height (%d)", status); | 
|  | 1728 | if (mUseHwcVirtualDisplays && | 
| Fabien Sanglard | c8e387e | 2017-03-10 10:30:28 -0800 | [diff] [blame] | 1729 | (SurfaceFlinger::maxVirtualDisplaySize == 0 || | 
|  | 1730 | (width <= static_cast<int>(SurfaceFlinger::maxVirtualDisplaySize) && | 
|  | 1731 | height <= static_cast<int>(SurfaceFlinger::maxVirtualDisplaySize)))) { | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 1732 | hwcDisplayId = allocateHwcDisplayId(state.type); | 
|  | 1733 | } | 
|  | 1734 |  | 
|  | 1735 | sp<VirtualDisplaySurface> vds = new VirtualDisplaySurface( | 
|  | 1736 | *mHwc, hwcDisplayId, state.surface, | 
|  | 1737 | bqProducer, bqConsumer, state.displayName); | 
|  | 1738 |  | 
|  | 1739 | dispSurface = vds; | 
|  | 1740 | producer = vds; | 
|  | 1741 | } | 
|  | 1742 | } else { | 
|  | 1743 | ALOGE_IF(state.surface!=NULL, | 
|  | 1744 | "adding a supported display, but rendering " | 
|  | 1745 | "surface is provided (%p), ignoring it", | 
|  | 1746 | state.surface.get()); | 
|  | 1747 | hwcDisplayId = allocateHwcDisplayId(state.type); | 
|  | 1748 | // for supported (by hwc) displays we provide our | 
|  | 1749 | // own rendering surface | 
|  | 1750 | dispSurface = new FramebufferSurface(*mHwc, state.type, | 
|  | 1751 | bqConsumer); | 
|  | 1752 | producer = bqProducer; | 
|  | 1753 | } | 
|  | 1754 |  | 
|  | 1755 | const wp<IBinder>& display(curr.keyAt(i)); | 
|  | 1756 | if (dispSurface != NULL) { | 
|  | 1757 | sp<DisplayDevice> hw = new DisplayDevice(this, | 
|  | 1758 | state.type, hwcDisplayId, | 
|  | 1759 | mHwc->getFormat(hwcDisplayId), state.isSecure, | 
|  | 1760 | display, dispSurface, producer, | 
| Courtney Goeltzenleuchter | 4f20f9c | 2017-04-06 08:18:34 -0600 | [diff] [blame] | 1761 | mRenderEngine->getEGLConfig(), false); | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 1762 | hw->setLayerStack(state.layerStack); | 
|  | 1763 | hw->setProjection(state.orientation, | 
|  | 1764 | state.viewport, state.frame); | 
|  | 1765 | hw->setDisplayName(state.displayName); | 
|  | 1766 | mDisplays.add(display, hw); | 
|  | 1767 | if (state.isVirtualDisplay()) { | 
|  | 1768 | if (hwcDisplayId >= 0) { | 
|  | 1769 | mHwc->setVirtualDisplayProperties(hwcDisplayId, | 
|  | 1770 | hw->getWidth(), hw->getHeight(), | 
|  | 1771 | hw->getFormat()); | 
|  | 1772 | } | 
|  | 1773 | } else { | 
|  | 1774 | mEventThread->onHotplugReceived(state.type, true); | 
|  | 1775 | } | 
|  | 1776 | } | 
|  | 1777 | } | 
|  | 1778 | } | 
|  | 1779 | } | 
|  | 1780 | } | 
|  | 1781 |  | 
|  | 1782 | if (transactionFlags & (eTraversalNeeded|eDisplayTransactionNeeded)) { | 
|  | 1783 | // The transform hint might have changed for some layers | 
|  | 1784 | // (either because a display has changed, or because a layer | 
|  | 1785 | // as changed). | 
|  | 1786 | // | 
|  | 1787 | // Walk through all the layers in currentLayers, | 
|  | 1788 | // and update their transform hint. | 
|  | 1789 | // | 
|  | 1790 | // If a layer is visible only on a single display, then that | 
|  | 1791 | // display is used to calculate the hint, otherwise we use the | 
|  | 1792 | // default display. | 
|  | 1793 | // | 
|  | 1794 | // NOTE: we do this here, rather than in rebuildLayerStacks() so that | 
|  | 1795 | // the hint is set before we acquire a buffer from the surface texture. | 
|  | 1796 | // | 
|  | 1797 | // NOTE: layer transactions have taken place already, so we use their | 
|  | 1798 | // drawing state. However, SurfaceFlinger's own transaction has not | 
|  | 1799 | // happened yet, so we must use the current state layer list | 
|  | 1800 | // (soon to become the drawing state list). | 
|  | 1801 | // | 
|  | 1802 | sp<const DisplayDevice> disp; | 
|  | 1803 | uint32_t currentlayerStack = 0; | 
| Robert Carr | 2047fae | 2016-11-28 14:09:09 -0800 | [diff] [blame] | 1804 | bool first = true; | 
|  | 1805 | mCurrentState.traverseInZOrder([&](Layer* layer) { | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 1806 | // NOTE: we rely on the fact that layers are sorted by | 
|  | 1807 | // layerStack first (so we don't have to traverse the list | 
|  | 1808 | // of displays for every layer). | 
| Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 1809 | uint32_t layerStack = layer->getLayerStack(); | 
| Robert Carr | 2047fae | 2016-11-28 14:09:09 -0800 | [diff] [blame] | 1810 | if (first || currentlayerStack != layerStack) { | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 1811 | currentlayerStack = layerStack; | 
|  | 1812 | // figure out if this layerstack is mirrored | 
|  | 1813 | // (more than one display) if so, pick the default display, | 
|  | 1814 | // if not, pick the only display it's on. | 
|  | 1815 | disp.clear(); | 
|  | 1816 | for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) { | 
|  | 1817 | sp<const DisplayDevice> hw(mDisplays[dpy]); | 
|  | 1818 | if (hw->getLayerStack() == currentlayerStack) { | 
|  | 1819 | if (disp == NULL) { | 
|  | 1820 | disp = hw; | 
|  | 1821 | } else { | 
|  | 1822 | disp = NULL; | 
|  | 1823 | break; | 
|  | 1824 | } | 
|  | 1825 | } | 
|  | 1826 | } | 
|  | 1827 | } | 
|  | 1828 | if (disp == NULL) { | 
|  | 1829 | // NOTE: TEMPORARY FIX ONLY. Real fix should cause layers to | 
|  | 1830 | // redraw after transform hint changes. See bug 8508397. | 
|  | 1831 |  | 
|  | 1832 | // could be null when this layer is using a layerStack | 
|  | 1833 | // that is not visible on any display. Also can occur at | 
|  | 1834 | // screen off/on times. | 
|  | 1835 | disp = getDefaultDisplayDevice(); | 
|  | 1836 | } | 
|  | 1837 | layer->updateTransformHint(disp); | 
| Robert Carr | 2047fae | 2016-11-28 14:09:09 -0800 | [diff] [blame] | 1838 |  | 
|  | 1839 | first = false; | 
|  | 1840 | }); | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 1841 | } | 
|  | 1842 |  | 
|  | 1843 |  | 
|  | 1844 | /* | 
|  | 1845 | * Perform our own transaction if needed | 
|  | 1846 | */ | 
| Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 1847 |  | 
|  | 1848 | if (mLayersAdded) { | 
|  | 1849 | mLayersAdded = false; | 
|  | 1850 | // Layers have been added. | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 1851 | mVisibleRegionsDirty = true; | 
|  | 1852 | } | 
|  | 1853 |  | 
|  | 1854 | // some layers might have been removed, so | 
|  | 1855 | // we need to update the regions they're exposing. | 
|  | 1856 | if (mLayersRemoved) { | 
|  | 1857 | mLayersRemoved = false; | 
|  | 1858 | mVisibleRegionsDirty = true; | 
| Robert Carr | 2047fae | 2016-11-28 14:09:09 -0800 | [diff] [blame] | 1859 | mDrawingState.traverseInZOrder([&](Layer* layer) { | 
| Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 1860 | if (mLayersPendingRemoval.indexOf(layer) >= 0) { | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 1861 | // this layer is not visible anymore | 
|  | 1862 | // TODO: we could traverse the tree from front to back and | 
|  | 1863 | //       compute the actual visible region | 
|  | 1864 | // TODO: we could cache the transformed region | 
| Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 1865 | Region visibleReg; | 
|  | 1866 | visibleReg.set(layer->computeScreenBounds()); | 
|  | 1867 | invalidateLayerStack(layer->getLayerStack(), visibleReg); | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 1868 | } | 
| Robert Carr | 2047fae | 2016-11-28 14:09:09 -0800 | [diff] [blame] | 1869 | }); | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 1870 | } | 
|  | 1871 |  | 
|  | 1872 | commitTransaction(); | 
|  | 1873 |  | 
|  | 1874 | updateCursorAsync(); | 
|  | 1875 | } | 
|  | 1876 |  | 
|  | 1877 | void SurfaceFlinger::updateCursorAsync() | 
|  | 1878 | { | 
|  | 1879 | HWComposer& hwc(getHwComposer()); | 
|  | 1880 | for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) { | 
|  | 1881 | sp<const DisplayDevice> hw(mDisplays[dpy]); | 
|  | 1882 | const int32_t id = hw->getHwcDisplayId(); | 
|  | 1883 | if (id < 0) { | 
|  | 1884 | continue; | 
|  | 1885 | } | 
|  | 1886 | const Vector< sp<Layer> >& currentLayers( | 
|  | 1887 | hw->getVisibleLayersSortedByZ()); | 
|  | 1888 | const size_t count = currentLayers.size(); | 
|  | 1889 | HWComposer::LayerListIterator cur = hwc.begin(id); | 
|  | 1890 | const HWComposer::LayerListIterator end = hwc.end(id); | 
|  | 1891 | for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) { | 
|  | 1892 | if (cur->getCompositionType() != HWC_CURSOR_OVERLAY) { | 
|  | 1893 | continue; | 
|  | 1894 | } | 
|  | 1895 | const sp<Layer>& layer(currentLayers[i]); | 
|  | 1896 | Rect cursorPos = layer->getPosition(hw); | 
|  | 1897 | hwc.setCursorPositionAsync(id, cursorPos); | 
|  | 1898 | break; | 
|  | 1899 | } | 
|  | 1900 | } | 
|  | 1901 | } | 
|  | 1902 |  | 
|  | 1903 | void SurfaceFlinger::commitTransaction() | 
|  | 1904 | { | 
|  | 1905 | if (!mLayersPendingRemoval.isEmpty()) { | 
|  | 1906 | // Notify removed layers now that they can't be drawn from | 
| Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 1907 | for (const auto& l : mLayersPendingRemoval) { | 
|  | 1908 | recordBufferingStats(l->getName().string(), | 
|  | 1909 | l->getOccupancyHistory(true)); | 
|  | 1910 | l->onRemoved(); | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 1911 | } | 
|  | 1912 | mLayersPendingRemoval.clear(); | 
|  | 1913 | } | 
|  | 1914 |  | 
|  | 1915 | // If this transaction is part of a window animation then the next frame | 
|  | 1916 | // we composite should be considered an animation as well. | 
|  | 1917 | mAnimCompositionPending = mAnimTransactionPending; | 
|  | 1918 |  | 
|  | 1919 | mDrawingState = mCurrentState; | 
| Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 1920 | mDrawingState.traverseInZOrder([](Layer* layer) { | 
|  | 1921 | layer->commitChildList(); | 
|  | 1922 | }); | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 1923 | mTransactionPending = false; | 
|  | 1924 | mAnimTransactionPending = false; | 
|  | 1925 | mTransactionCV.broadcast(); | 
|  | 1926 | } | 
|  | 1927 |  | 
| Robert Carr | 2047fae | 2016-11-28 14:09:09 -0800 | [diff] [blame] | 1928 | void SurfaceFlinger::computeVisibleRegions(uint32_t layerStack, | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 1929 | Region& outDirtyRegion, Region& outOpaqueRegion) | 
|  | 1930 | { | 
|  | 1931 | ATRACE_CALL(); | 
|  | 1932 |  | 
|  | 1933 | Region aboveOpaqueLayers; | 
|  | 1934 | Region aboveCoveredLayers; | 
|  | 1935 | Region dirty; | 
|  | 1936 |  | 
|  | 1937 | outDirtyRegion.clear(); | 
|  | 1938 |  | 
| Robert Carr | 2047fae | 2016-11-28 14:09:09 -0800 | [diff] [blame] | 1939 | mDrawingState.traverseInReverseZOrder([&](Layer* layer) { | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 1940 | // start with the whole surface at its current location | 
|  | 1941 | const Layer::State& s(layer->getDrawingState()); | 
|  | 1942 |  | 
|  | 1943 | // only consider the layers on the given layer stack | 
| Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 1944 | if (layer->getLayerStack() != layerStack) | 
| Robert Carr | 2047fae | 2016-11-28 14:09:09 -0800 | [diff] [blame] | 1945 | return; | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 1946 |  | 
|  | 1947 | /* | 
|  | 1948 | * opaqueRegion: area of a surface that is fully opaque. | 
|  | 1949 | */ | 
|  | 1950 | Region opaqueRegion; | 
|  | 1951 |  | 
|  | 1952 | /* | 
|  | 1953 | * visibleRegion: area of a surface that is visible on screen | 
|  | 1954 | * and not fully transparent. This is essentially the layer's | 
|  | 1955 | * footprint minus the opaque regions above it. | 
|  | 1956 | * Areas covered by a translucent surface are considered visible. | 
|  | 1957 | */ | 
|  | 1958 | Region visibleRegion; | 
|  | 1959 |  | 
|  | 1960 | /* | 
|  | 1961 | * coveredRegion: area of a surface that is covered by all | 
|  | 1962 | * visible regions above it (which includes the translucent areas). | 
|  | 1963 | */ | 
|  | 1964 | Region coveredRegion; | 
|  | 1965 |  | 
|  | 1966 | /* | 
|  | 1967 | * transparentRegion: area of a surface that is hinted to be completely | 
|  | 1968 | * transparent. This is only used to tell when the layer has no visible | 
|  | 1969 | * non-transparent regions and can be removed from the layer list. It | 
|  | 1970 | * does not affect the visibleRegion of this layer or any layers | 
|  | 1971 | * beneath it. The hint may not be correct if apps don't respect the | 
|  | 1972 | * SurfaceView restrictions (which, sadly, some don't). | 
|  | 1973 | */ | 
|  | 1974 | Region transparentRegion; | 
|  | 1975 |  | 
|  | 1976 |  | 
|  | 1977 | // handle hidden surfaces by setting the visible region to empty | 
|  | 1978 | if (CC_LIKELY(layer->isVisible())) { | 
|  | 1979 | const bool translucent = !layer->isOpaque(s); | 
| Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 1980 | Rect bounds(layer->computeScreenBounds()); | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 1981 | visibleRegion.set(bounds); | 
| Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 1982 | Transform tr = layer->getTransform(); | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 1983 | if (!visibleRegion.isEmpty()) { | 
|  | 1984 | // Remove the transparent area from the visible region | 
|  | 1985 | if (translucent) { | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 1986 | if (tr.preserveRects()) { | 
|  | 1987 | // transform the transparent region | 
|  | 1988 | transparentRegion = tr.transform(s.activeTransparentRegion); | 
|  | 1989 | } else { | 
|  | 1990 | // transformation too complex, can't do the | 
|  | 1991 | // transparent region optimization. | 
|  | 1992 | transparentRegion.clear(); | 
|  | 1993 | } | 
|  | 1994 | } | 
|  | 1995 |  | 
|  | 1996 | // compute the opaque region | 
| Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 1997 | const int32_t layerOrientation = tr.getOrientation(); | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 1998 | if (s.alpha==255 && !translucent && | 
|  | 1999 | ((layerOrientation & Transform::ROT_INVALID) == false)) { | 
|  | 2000 | // the opaque region is the layer's footprint | 
|  | 2001 | opaqueRegion = visibleRegion; | 
|  | 2002 | } | 
|  | 2003 | } | 
|  | 2004 | } | 
|  | 2005 |  | 
|  | 2006 | // Clip the covered region to the visible region | 
|  | 2007 | coveredRegion = aboveCoveredLayers.intersect(visibleRegion); | 
|  | 2008 |  | 
|  | 2009 | // Update aboveCoveredLayers for next (lower) layer | 
|  | 2010 | aboveCoveredLayers.orSelf(visibleRegion); | 
|  | 2011 |  | 
|  | 2012 | // subtract the opaque region covered by the layers above us | 
|  | 2013 | visibleRegion.subtractSelf(aboveOpaqueLayers); | 
|  | 2014 |  | 
|  | 2015 | // compute this layer's dirty region | 
|  | 2016 | if (layer->contentDirty) { | 
|  | 2017 | // we need to invalidate the whole region | 
|  | 2018 | dirty = visibleRegion; | 
|  | 2019 | // as well, as the old visible region | 
|  | 2020 | dirty.orSelf(layer->visibleRegion); | 
|  | 2021 | layer->contentDirty = false; | 
|  | 2022 | } else { | 
|  | 2023 | /* compute the exposed region: | 
|  | 2024 | *   the exposed region consists of two components: | 
|  | 2025 | *   1) what's VISIBLE now and was COVERED before | 
|  | 2026 | *   2) what's EXPOSED now less what was EXPOSED before | 
|  | 2027 | * | 
|  | 2028 | * note that (1) is conservative, we start with the whole | 
|  | 2029 | * visible region but only keep what used to be covered by | 
|  | 2030 | * something -- which mean it may have been exposed. | 
|  | 2031 | * | 
|  | 2032 | * (2) handles areas that were not covered by anything but got | 
|  | 2033 | * exposed because of a resize. | 
|  | 2034 | */ | 
|  | 2035 | const Region newExposed = visibleRegion - coveredRegion; | 
|  | 2036 | const Region oldVisibleRegion = layer->visibleRegion; | 
|  | 2037 | const Region oldCoveredRegion = layer->coveredRegion; | 
|  | 2038 | const Region oldExposed = oldVisibleRegion - oldCoveredRegion; | 
|  | 2039 | dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed); | 
|  | 2040 | } | 
|  | 2041 | dirty.subtractSelf(aboveOpaqueLayers); | 
|  | 2042 |  | 
|  | 2043 | // accumulate to the screen dirty region | 
|  | 2044 | outDirtyRegion.orSelf(dirty); | 
|  | 2045 |  | 
|  | 2046 | // Update aboveOpaqueLayers for next (lower) layer | 
|  | 2047 | aboveOpaqueLayers.orSelf(opaqueRegion); | 
|  | 2048 |  | 
|  | 2049 | // Store the visible region in screen space | 
|  | 2050 | layer->setVisibleRegion(visibleRegion); | 
|  | 2051 | layer->setCoveredRegion(coveredRegion); | 
|  | 2052 | layer->setVisibleNonTransparentRegion( | 
|  | 2053 | visibleRegion.subtract(transparentRegion)); | 
| Robert Carr | 2047fae | 2016-11-28 14:09:09 -0800 | [diff] [blame] | 2054 | }); | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 2055 |  | 
|  | 2056 | outOpaqueRegion = aboveOpaqueLayers; | 
|  | 2057 | } | 
|  | 2058 |  | 
|  | 2059 | void SurfaceFlinger::invalidateLayerStack(uint32_t layerStack, | 
|  | 2060 | const Region& dirty) { | 
|  | 2061 | for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) { | 
|  | 2062 | const sp<DisplayDevice>& hw(mDisplays[dpy]); | 
|  | 2063 | if (hw->getLayerStack() == layerStack) { | 
|  | 2064 | hw->dirtyRegion.orSelf(dirty); | 
|  | 2065 | } | 
|  | 2066 | } | 
|  | 2067 | } | 
|  | 2068 |  | 
|  | 2069 | bool SurfaceFlinger::handlePageFlip() | 
|  | 2070 | { | 
| Brian Anderson | d6927fb | 2016-07-23 23:37:30 -0700 | [diff] [blame] | 2071 | nsecs_t latchTime = systemTime(); | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 2072 | Region dirtyRegion; | 
|  | 2073 |  | 
|  | 2074 | bool visibleRegions = false; | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 2075 | bool frameQueued = false; | 
|  | 2076 |  | 
|  | 2077 | // Store the set of layers that need updates. This set must not change as | 
|  | 2078 | // buffers are being latched, as this could result in a deadlock. | 
|  | 2079 | // Example: Two producers share the same command stream and: | 
|  | 2080 | // 1.) Layer 0 is latched | 
|  | 2081 | // 2.) Layer 0 gets a new frame | 
|  | 2082 | // 2.) Layer 1 gets a new frame | 
|  | 2083 | // 3.) Layer 1 is latched. | 
|  | 2084 | // Display is now waiting on Layer 1's frame, which is behind layer 0's | 
|  | 2085 | // second frame. But layer 0's second frame could be waiting on display. | 
|  | 2086 | Vector<Layer*> layersWithQueuedFrames; | 
| Robert Carr | 2047fae | 2016-11-28 14:09:09 -0800 | [diff] [blame] | 2087 | mDrawingState.traverseInZOrder([&](Layer* layer) { | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 2088 | if (layer->hasQueuedFrame()) { | 
|  | 2089 | frameQueued = true; | 
|  | 2090 | if (layer->shouldPresentNow(mPrimaryDispSync)) { | 
| Robert Carr | 2047fae | 2016-11-28 14:09:09 -0800 | [diff] [blame] | 2091 | layersWithQueuedFrames.push_back(layer); | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 2092 | } else { | 
|  | 2093 | layer->useEmptyDamage(); | 
|  | 2094 | } | 
|  | 2095 | } else { | 
|  | 2096 | layer->useEmptyDamage(); | 
|  | 2097 | } | 
| Robert Carr | 2047fae | 2016-11-28 14:09:09 -0800 | [diff] [blame] | 2098 | }); | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 2099 | for (size_t i = 0, count = layersWithQueuedFrames.size() ; i<count ; i++) { | 
|  | 2100 | Layer* layer = layersWithQueuedFrames[i]; | 
| Brian Anderson | d6927fb | 2016-07-23 23:37:30 -0700 | [diff] [blame] | 2101 | const Region dirty(layer->latchBuffer(visibleRegions, latchTime)); | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 2102 | layer->useSurfaceDamage(); | 
| Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 2103 | invalidateLayerStack(layer->getLayerStack(), dirty); | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 2104 | } | 
|  | 2105 |  | 
|  | 2106 | mVisibleRegionsDirty |= visibleRegions; | 
|  | 2107 |  | 
|  | 2108 | // If we will need to wake up at some time in the future to deal with a | 
|  | 2109 | // queued frame that shouldn't be displayed during this vsync period, wake | 
|  | 2110 | // up during the next vsync period to check again. | 
|  | 2111 | if (frameQueued && layersWithQueuedFrames.empty()) { | 
|  | 2112 | signalLayerUpdate(); | 
|  | 2113 | } | 
|  | 2114 |  | 
|  | 2115 | // Only continue with the refresh if there is actually new work to do | 
|  | 2116 | return !layersWithQueuedFrames.empty(); | 
|  | 2117 | } | 
|  | 2118 |  | 
|  | 2119 | void SurfaceFlinger::invalidateHwcGeometry() | 
|  | 2120 | { | 
|  | 2121 | mHwWorkListDirty = true; | 
|  | 2122 | } | 
|  | 2123 |  | 
|  | 2124 |  | 
|  | 2125 | void SurfaceFlinger::doDisplayComposition(const sp<const DisplayDevice>& hw, | 
|  | 2126 | const Region& inDirtyRegion) | 
|  | 2127 | { | 
|  | 2128 | // We only need to actually compose the display if: | 
|  | 2129 | // 1) It is being handled by hardware composer, which may need this to | 
|  | 2130 | //    keep its virtual display state machine in sync, or | 
|  | 2131 | // 2) There is work to be done (the dirty region isn't empty) | 
|  | 2132 | bool isHwcDisplay = hw->getHwcDisplayId() >= 0; | 
|  | 2133 | if (!isHwcDisplay && inDirtyRegion.isEmpty()) { | 
|  | 2134 | return; | 
|  | 2135 | } | 
|  | 2136 |  | 
|  | 2137 | Region dirtyRegion(inDirtyRegion); | 
|  | 2138 |  | 
|  | 2139 | // compute the invalid region | 
|  | 2140 | hw->swapRegion.orSelf(dirtyRegion); | 
|  | 2141 |  | 
|  | 2142 | uint32_t flags = hw->getFlags(); | 
|  | 2143 | if (flags & DisplayDevice::SWAP_RECTANGLE) { | 
|  | 2144 | // we can redraw only what's dirty, but since SWAP_RECTANGLE only | 
|  | 2145 | // takes a rectangle, we must make sure to update that whole | 
|  | 2146 | // rectangle in that case | 
|  | 2147 | dirtyRegion.set(hw->swapRegion.bounds()); | 
|  | 2148 | } else { | 
|  | 2149 | if (flags & DisplayDevice::PARTIAL_UPDATES) { | 
|  | 2150 | // We need to redraw the rectangle that will be updated | 
|  | 2151 | // (pushed to the framebuffer). | 
|  | 2152 | // This is needed because PARTIAL_UPDATES only takes one | 
|  | 2153 | // rectangle instead of a region (see DisplayDevice::flip()) | 
|  | 2154 | dirtyRegion.set(hw->swapRegion.bounds()); | 
|  | 2155 | } else { | 
|  | 2156 | // we need to redraw everything (the whole screen) | 
|  | 2157 | dirtyRegion.set(hw->bounds()); | 
|  | 2158 | hw->swapRegion = dirtyRegion; | 
|  | 2159 | } | 
|  | 2160 | } | 
|  | 2161 |  | 
|  | 2162 | if (CC_LIKELY(!mDaltonize && !mHasColorMatrix)) { | 
|  | 2163 | if (!doComposeSurfaces(hw, dirtyRegion)) return; | 
|  | 2164 | } else { | 
|  | 2165 | RenderEngine& engine(getRenderEngine()); | 
|  | 2166 | mat4 colorMatrix = mColorMatrix; | 
|  | 2167 | if (mDaltonize) { | 
|  | 2168 | colorMatrix = colorMatrix * mDaltonizer(); | 
|  | 2169 | } | 
|  | 2170 | mat4 oldMatrix = engine.setupColorTransform(colorMatrix); | 
|  | 2171 | doComposeSurfaces(hw, dirtyRegion); | 
|  | 2172 | engine.setupColorTransform(oldMatrix); | 
|  | 2173 | } | 
|  | 2174 |  | 
|  | 2175 | // update the swap region and clear the dirty region | 
|  | 2176 | hw->swapRegion.orSelf(dirtyRegion); | 
|  | 2177 |  | 
|  | 2178 | // swap buffers (presentation) | 
|  | 2179 | hw->swapBuffers(getHwComposer()); | 
|  | 2180 | } | 
|  | 2181 |  | 
|  | 2182 | bool SurfaceFlinger::doComposeSurfaces(const sp<const DisplayDevice>& hw, const Region& dirty) | 
|  | 2183 | { | 
|  | 2184 | RenderEngine& engine(getRenderEngine()); | 
|  | 2185 | const int32_t id = hw->getHwcDisplayId(); | 
|  | 2186 | HWComposer& hwc(getHwComposer()); | 
|  | 2187 | HWComposer::LayerListIterator cur = hwc.begin(id); | 
|  | 2188 | const HWComposer::LayerListIterator end = hwc.end(id); | 
|  | 2189 |  | 
|  | 2190 | bool hasGlesComposition = hwc.hasGlesComposition(id); | 
|  | 2191 | if (hasGlesComposition) { | 
|  | 2192 | if (!hw->makeCurrent(mEGLDisplay, mEGLContext)) { | 
|  | 2193 | ALOGW("DisplayDevice::makeCurrent failed. Aborting surface composition for display %s", | 
|  | 2194 | hw->getDisplayName().string()); | 
|  | 2195 | eglMakeCurrent(mEGLDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT); | 
|  | 2196 | if(!getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext)) { | 
|  | 2197 | ALOGE("DisplayDevice::makeCurrent on default display failed. Aborting."); | 
|  | 2198 | } | 
|  | 2199 | return false; | 
|  | 2200 | } | 
|  | 2201 |  | 
|  | 2202 | // Never touch the framebuffer if we don't have any framebuffer layers | 
|  | 2203 | const bool hasHwcComposition = hwc.hasHwcComposition(id); | 
|  | 2204 | if (hasHwcComposition) { | 
|  | 2205 | // when using overlays, we assume a fully transparent framebuffer | 
|  | 2206 | // NOTE: we could reduce how much we need to clear, for instance | 
|  | 2207 | // remove where there are opaque FB layers. however, on some | 
|  | 2208 | // GPUs doing a "clean slate" clear might be more efficient. | 
|  | 2209 | // We'll revisit later if needed. | 
|  | 2210 | engine.clearWithColor(0, 0, 0, 0); | 
|  | 2211 | } else { | 
|  | 2212 | // we start with the whole screen area | 
|  | 2213 | const Region bounds(hw->getBounds()); | 
|  | 2214 |  | 
|  | 2215 | // we remove the scissor part | 
|  | 2216 | // we're left with the letterbox region | 
|  | 2217 | // (common case is that letterbox ends-up being empty) | 
|  | 2218 | const Region letterbox(bounds.subtract(hw->getScissor())); | 
|  | 2219 |  | 
|  | 2220 | // compute the area to clear | 
|  | 2221 | Region region(hw->undefinedRegion.merge(letterbox)); | 
|  | 2222 |  | 
|  | 2223 | // but limit it to the dirty region | 
|  | 2224 | region.andSelf(dirty); | 
|  | 2225 |  | 
|  | 2226 | // screen is already cleared here | 
|  | 2227 | if (!region.isEmpty()) { | 
|  | 2228 | // can happen with SurfaceView | 
|  | 2229 | drawWormhole(hw, region); | 
|  | 2230 | } | 
|  | 2231 | } | 
|  | 2232 |  | 
|  | 2233 | if (hw->getDisplayType() != DisplayDevice::DISPLAY_PRIMARY) { | 
|  | 2234 | // just to be on the safe side, we don't set the | 
|  | 2235 | // scissor on the main display. It should never be needed | 
|  | 2236 | // anyways (though in theory it could since the API allows it). | 
|  | 2237 | const Rect& bounds(hw->getBounds()); | 
|  | 2238 | const Rect& scissor(hw->getScissor()); | 
|  | 2239 | if (scissor != bounds) { | 
|  | 2240 | // scissor doesn't match the screen's dimensions, so we | 
|  | 2241 | // need to clear everything outside of it and enable | 
|  | 2242 | // the GL scissor so we don't draw anything where we shouldn't | 
|  | 2243 |  | 
|  | 2244 | // enable scissor for this frame | 
|  | 2245 | const uint32_t height = hw->getHeight(); | 
|  | 2246 | engine.setScissor(scissor.left, height - scissor.bottom, | 
|  | 2247 | scissor.getWidth(), scissor.getHeight()); | 
|  | 2248 | } | 
|  | 2249 | } | 
|  | 2250 | } | 
|  | 2251 |  | 
|  | 2252 | /* | 
|  | 2253 | * and then, render the layers targeted at the framebuffer | 
|  | 2254 | */ | 
|  | 2255 |  | 
|  | 2256 | const Vector< sp<Layer> >& layers(hw->getVisibleLayersSortedByZ()); | 
|  | 2257 | const size_t count = layers.size(); | 
|  | 2258 | const Transform& tr = hw->getTransform(); | 
|  | 2259 | if (cur != end) { | 
|  | 2260 | // we're using h/w composer | 
|  | 2261 | for (size_t i=0 ; i<count && cur!=end ; ++i, ++cur) { | 
|  | 2262 | const sp<Layer>& layer(layers[i]); | 
|  | 2263 | const Region clip(dirty.intersect(tr.transform(layer->visibleRegion))); | 
|  | 2264 | if (!clip.isEmpty()) { | 
|  | 2265 | switch (cur->getCompositionType()) { | 
|  | 2266 | case HWC_CURSOR_OVERLAY: | 
|  | 2267 | case HWC_OVERLAY: { | 
|  | 2268 | const Layer::State& state(layer->getDrawingState()); | 
|  | 2269 | if ((cur->getHints() & HWC_HINT_CLEAR_FB) | 
|  | 2270 | && i | 
|  | 2271 | && layer->isOpaque(state) && (state.alpha == 0xFF) | 
|  | 2272 | && hasGlesComposition) { | 
|  | 2273 | // never clear the very first layer since we're | 
|  | 2274 | // guaranteed the FB is already cleared | 
| Fabien Sanglard | 1748719 | 2016-12-07 13:03:32 -0800 | [diff] [blame] | 2275 | layer->clearWithOpenGL(hw); | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 2276 | } | 
|  | 2277 | break; | 
|  | 2278 | } | 
|  | 2279 | case HWC_FRAMEBUFFER: { | 
|  | 2280 | layer->draw(hw, clip); | 
|  | 2281 | break; | 
|  | 2282 | } | 
|  | 2283 | case HWC_FRAMEBUFFER_TARGET: { | 
|  | 2284 | // this should not happen as the iterator shouldn't | 
|  | 2285 | // let us get there. | 
|  | 2286 | ALOGW("HWC_FRAMEBUFFER_TARGET found in hwc list (index=%zu)", i); | 
|  | 2287 | break; | 
|  | 2288 | } | 
|  | 2289 | } | 
|  | 2290 | } | 
|  | 2291 | layer->setAcquireFence(hw, *cur); | 
|  | 2292 | } | 
|  | 2293 | } else { | 
|  | 2294 | // we're not using h/w composer | 
|  | 2295 | for (size_t i=0 ; i<count ; ++i) { | 
|  | 2296 | const sp<Layer>& layer(layers[i]); | 
|  | 2297 | const Region clip(dirty.intersect( | 
|  | 2298 | tr.transform(layer->visibleRegion))); | 
|  | 2299 | if (!clip.isEmpty()) { | 
|  | 2300 | layer->draw(hw, clip); | 
|  | 2301 | } | 
|  | 2302 | } | 
|  | 2303 | } | 
|  | 2304 |  | 
|  | 2305 | // disable scissor at the end of the frame | 
|  | 2306 | engine.disableScissor(); | 
|  | 2307 | return true; | 
|  | 2308 | } | 
|  | 2309 |  | 
|  | 2310 | void SurfaceFlinger::drawWormhole(const sp<const DisplayDevice>& hw, const Region& region) const { | 
|  | 2311 | const int32_t height = hw->getHeight(); | 
|  | 2312 | RenderEngine& engine(getRenderEngine()); | 
|  | 2313 | engine.fillRegionWithColor(region, height, 0, 0, 0, 0); | 
|  | 2314 | } | 
|  | 2315 |  | 
|  | 2316 | status_t SurfaceFlinger::addClientLayer(const sp<Client>& client, | 
|  | 2317 | const sp<IBinder>& handle, | 
|  | 2318 | const sp<IGraphicBufferProducer>& gbc, | 
| Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 2319 | const sp<Layer>& lbc, | 
|  | 2320 | const sp<Layer>& parent) | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 2321 | { | 
|  | 2322 | // add this layer to the current state list | 
|  | 2323 | { | 
|  | 2324 | Mutex::Autolock _l(mStateLock); | 
| Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 2325 | if (mNumLayers >= MAX_LAYERS) { | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 2326 | return NO_MEMORY; | 
|  | 2327 | } | 
| Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 2328 | if (parent == nullptr) { | 
|  | 2329 | mCurrentState.layersSortedByZ.add(lbc); | 
|  | 2330 | } else { | 
|  | 2331 | parent->addChild(lbc); | 
|  | 2332 | } | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 2333 | mGraphicBufferProducerList.add(IInterface::asBinder(gbc)); | 
| Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 2334 | mLayersAdded = true; | 
|  | 2335 | mNumLayers++; | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 2336 | } | 
|  | 2337 |  | 
|  | 2338 | // attach this layer to the client | 
|  | 2339 | client->attachLayer(handle, lbc); | 
|  | 2340 |  | 
|  | 2341 | return NO_ERROR; | 
|  | 2342 | } | 
|  | 2343 |  | 
| Robert Carr | 9524cb3 | 2017-02-13 11:32:32 -0800 | [diff] [blame] | 2344 | status_t SurfaceFlinger::removeLayer(const sp<Layer>& layer) { | 
| Robert Carr | 7f9b899 | 2017-03-10 11:08:39 -0800 | [diff] [blame] | 2345 | Mutex::Autolock _l(mStateLock); | 
|  | 2346 |  | 
| Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 2347 | const auto& p = layer->getParent(); | 
|  | 2348 | const ssize_t index = (p != nullptr) ? p->removeChild(layer) : | 
|  | 2349 | mCurrentState.layersSortedByZ.remove(layer); | 
|  | 2350 |  | 
| Robert Carr | 136e2f6 | 2017-02-08 17:54:29 -0800 | [diff] [blame] | 2351 | // As a matter of normal operation, the LayerCleaner will produce a second | 
|  | 2352 | // attempt to remove the surface. The Layer will be kept alive in mDrawingState | 
|  | 2353 | // so we will succeed in promoting it, but it's already been removed | 
|  | 2354 | // from mCurrentState. As long as we can find it in mDrawingState we have no problem | 
|  | 2355 | // otherwise something has gone wrong and we are leaking the layer. | 
|  | 2356 | if (index < 0 && mDrawingState.layersSortedByZ.indexOf(layer) < 0) { | 
| Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 2357 | ALOGE("Failed to find layer (%s) in layer parent (%s).", | 
|  | 2358 | layer->getName().string(), | 
|  | 2359 | (p != nullptr) ? p->getName().string() : "no-parent"); | 
|  | 2360 | return BAD_VALUE; | 
| Robert Carr | 136e2f6 | 2017-02-08 17:54:29 -0800 | [diff] [blame] | 2361 | } else if (index < 0) { | 
|  | 2362 | return NO_ERROR; | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 2363 | } | 
| Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 2364 |  | 
|  | 2365 | mLayersPendingRemoval.add(layer); | 
|  | 2366 | mLayersRemoved = true; | 
|  | 2367 | mNumLayers--; | 
|  | 2368 | setTransactionFlags(eTransactionNeeded); | 
|  | 2369 | return NO_ERROR; | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 2370 | } | 
|  | 2371 |  | 
| Fabien Sanglard | c8251eb | 2016-12-07 13:59:48 -0800 | [diff] [blame] | 2372 | uint32_t SurfaceFlinger::peekTransactionFlags() { | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 2373 | return android_atomic_release_load(&mTransactionFlags); | 
|  | 2374 | } | 
|  | 2375 |  | 
|  | 2376 | uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags) { | 
|  | 2377 | return android_atomic_and(~flags, &mTransactionFlags) & flags; | 
|  | 2378 | } | 
|  | 2379 |  | 
|  | 2380 | uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags) { | 
|  | 2381 | uint32_t old = android_atomic_or(flags, &mTransactionFlags); | 
|  | 2382 | if ((old & flags)==0) { // wake the server up | 
|  | 2383 | signalTransaction(); | 
|  | 2384 | } | 
|  | 2385 | return old; | 
|  | 2386 | } | 
|  | 2387 |  | 
|  | 2388 | void SurfaceFlinger::setTransactionState( | 
|  | 2389 | const Vector<ComposerState>& state, | 
|  | 2390 | const Vector<DisplayState>& displays, | 
|  | 2391 | uint32_t flags) | 
|  | 2392 | { | 
|  | 2393 | ATRACE_CALL(); | 
|  | 2394 | Mutex::Autolock _l(mStateLock); | 
|  | 2395 | uint32_t transactionFlags = 0; | 
|  | 2396 |  | 
|  | 2397 | if (flags & eAnimation) { | 
|  | 2398 | // For window updates that are part of an animation we must wait for | 
|  | 2399 | // previous animation "frames" to be handled. | 
|  | 2400 | while (mAnimTransactionPending) { | 
|  | 2401 | status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5)); | 
|  | 2402 | if (CC_UNLIKELY(err != NO_ERROR)) { | 
|  | 2403 | // just in case something goes wrong in SF, return to the | 
|  | 2404 | // caller after a few seconds. | 
|  | 2405 | ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out " | 
|  | 2406 | "waiting for previous animation frame"); | 
|  | 2407 | mAnimTransactionPending = false; | 
|  | 2408 | break; | 
|  | 2409 | } | 
|  | 2410 | } | 
|  | 2411 | } | 
|  | 2412 |  | 
|  | 2413 | size_t count = displays.size(); | 
|  | 2414 | for (size_t i=0 ; i<count ; i++) { | 
|  | 2415 | const DisplayState& s(displays[i]); | 
|  | 2416 | transactionFlags |= setDisplayStateLocked(s); | 
|  | 2417 | } | 
|  | 2418 |  | 
|  | 2419 | count = state.size(); | 
|  | 2420 | for (size_t i=0 ; i<count ; i++) { | 
|  | 2421 | const ComposerState& s(state[i]); | 
|  | 2422 | // Here we need to check that the interface we're given is indeed | 
|  | 2423 | // one of our own. A malicious client could give us a NULL | 
|  | 2424 | // IInterface, or one of its own or even one of our own but a | 
|  | 2425 | // different type. All these situations would cause us to crash. | 
|  | 2426 | // | 
|  | 2427 | // NOTE: it would be better to use RTTI as we could directly check | 
|  | 2428 | // that we have a Client*. however, RTTI is disabled in Android. | 
|  | 2429 | if (s.client != NULL) { | 
|  | 2430 | sp<IBinder> binder = IInterface::asBinder(s.client); | 
|  | 2431 | if (binder != NULL) { | 
| Fabien Sanglard | 45b2025 | 2017-01-18 16:43:18 -0800 | [diff] [blame] | 2432 | if (binder->queryLocalInterface(ISurfaceComposerClient::descriptor) != NULL) { | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 2433 | sp<Client> client( static_cast<Client *>(s.client.get()) ); | 
|  | 2434 | transactionFlags |= setClientStateLocked(client, s.state); | 
|  | 2435 | } | 
|  | 2436 | } | 
|  | 2437 | } | 
|  | 2438 | } | 
|  | 2439 |  | 
|  | 2440 | // If a synchronous transaction is explicitly requested without any changes, | 
|  | 2441 | // force a transaction anyway. This can be used as a flush mechanism for | 
|  | 2442 | // previous async transactions. | 
|  | 2443 | if (transactionFlags == 0 && (flags & eSynchronous)) { | 
|  | 2444 | transactionFlags = eTransactionNeeded; | 
|  | 2445 | } | 
|  | 2446 |  | 
|  | 2447 | if (transactionFlags) { | 
|  | 2448 | if (mInterceptor.isEnabled()) { | 
|  | 2449 | mInterceptor.saveTransaction(state, mCurrentState.displays, displays, flags); | 
|  | 2450 | } | 
|  | 2451 |  | 
|  | 2452 | // this triggers the transaction | 
|  | 2453 | setTransactionFlags(transactionFlags); | 
|  | 2454 |  | 
|  | 2455 | // if this is a synchronous transaction, wait for it to take effect | 
|  | 2456 | // before returning. | 
|  | 2457 | if (flags & eSynchronous) { | 
|  | 2458 | mTransactionPending = true; | 
|  | 2459 | } | 
|  | 2460 | if (flags & eAnimation) { | 
|  | 2461 | mAnimTransactionPending = true; | 
|  | 2462 | } | 
|  | 2463 | while (mTransactionPending) { | 
|  | 2464 | status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5)); | 
|  | 2465 | if (CC_UNLIKELY(err != NO_ERROR)) { | 
|  | 2466 | // just in case something goes wrong in SF, return to the | 
|  | 2467 | // called after a few seconds. | 
|  | 2468 | ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out!"); | 
|  | 2469 | mTransactionPending = false; | 
|  | 2470 | break; | 
|  | 2471 | } | 
|  | 2472 | } | 
|  | 2473 | } | 
|  | 2474 | } | 
|  | 2475 |  | 
|  | 2476 | uint32_t SurfaceFlinger::setDisplayStateLocked(const DisplayState& s) | 
|  | 2477 | { | 
|  | 2478 | ssize_t dpyIdx = mCurrentState.displays.indexOfKey(s.token); | 
|  | 2479 | if (dpyIdx < 0) | 
|  | 2480 | return 0; | 
|  | 2481 |  | 
|  | 2482 | uint32_t flags = 0; | 
|  | 2483 | DisplayDeviceState& disp(mCurrentState.displays.editValueAt(dpyIdx)); | 
|  | 2484 | if (disp.isValid()) { | 
|  | 2485 | const uint32_t what = s.what; | 
|  | 2486 | if (what & DisplayState::eSurfaceChanged) { | 
|  | 2487 | if (IInterface::asBinder(disp.surface) != IInterface::asBinder(s.surface)) { | 
|  | 2488 | disp.surface = s.surface; | 
|  | 2489 | flags |= eDisplayTransactionNeeded; | 
|  | 2490 | } | 
|  | 2491 | } | 
|  | 2492 | if (what & DisplayState::eLayerStackChanged) { | 
|  | 2493 | if (disp.layerStack != s.layerStack) { | 
|  | 2494 | disp.layerStack = s.layerStack; | 
|  | 2495 | flags |= eDisplayTransactionNeeded; | 
|  | 2496 | } | 
|  | 2497 | } | 
|  | 2498 | if (what & DisplayState::eDisplayProjectionChanged) { | 
|  | 2499 | if (disp.orientation != s.orientation) { | 
|  | 2500 | disp.orientation = s.orientation; | 
|  | 2501 | flags |= eDisplayTransactionNeeded; | 
|  | 2502 | } | 
|  | 2503 | if (disp.frame != s.frame) { | 
|  | 2504 | disp.frame = s.frame; | 
|  | 2505 | flags |= eDisplayTransactionNeeded; | 
|  | 2506 | } | 
|  | 2507 | if (disp.viewport != s.viewport) { | 
|  | 2508 | disp.viewport = s.viewport; | 
|  | 2509 | flags |= eDisplayTransactionNeeded; | 
|  | 2510 | } | 
|  | 2511 | } | 
|  | 2512 | if (what & DisplayState::eDisplaySizeChanged) { | 
|  | 2513 | if (disp.width != s.width) { | 
|  | 2514 | disp.width = s.width; | 
|  | 2515 | flags |= eDisplayTransactionNeeded; | 
|  | 2516 | } | 
|  | 2517 | if (disp.height != s.height) { | 
|  | 2518 | disp.height = s.height; | 
|  | 2519 | flags |= eDisplayTransactionNeeded; | 
|  | 2520 | } | 
|  | 2521 | } | 
|  | 2522 | } | 
|  | 2523 | return flags; | 
|  | 2524 | } | 
|  | 2525 |  | 
|  | 2526 | uint32_t SurfaceFlinger::setClientStateLocked( | 
|  | 2527 | const sp<Client>& client, | 
|  | 2528 | const layer_state_t& s) | 
|  | 2529 | { | 
|  | 2530 | uint32_t flags = 0; | 
|  | 2531 | sp<Layer> layer(client->getLayerUser(s.surface)); | 
|  | 2532 | if (layer != 0) { | 
|  | 2533 | const uint32_t what = s.what; | 
|  | 2534 | bool geometryAppliesWithResize = | 
|  | 2535 | what & layer_state_t::eGeometryAppliesWithResize; | 
|  | 2536 | if (what & layer_state_t::ePositionChanged) { | 
|  | 2537 | if (layer->setPosition(s.x, s.y, !geometryAppliesWithResize)) { | 
|  | 2538 | flags |= eTraversalNeeded; | 
|  | 2539 | } | 
|  | 2540 | } | 
|  | 2541 | if (what & layer_state_t::eLayerChanged) { | 
|  | 2542 | // NOTE: index needs to be calculated before we update the state | 
| Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 2543 | const auto& p = layer->getParent(); | 
|  | 2544 | if (p == nullptr) { | 
|  | 2545 | ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer); | 
|  | 2546 | if (layer->setLayer(s.z) && idx >= 0) { | 
|  | 2547 | mCurrentState.layersSortedByZ.removeAt(idx); | 
|  | 2548 | mCurrentState.layersSortedByZ.add(layer); | 
|  | 2549 | // we need traversal (state changed) | 
|  | 2550 | // AND transaction (list changed) | 
|  | 2551 | flags |= eTransactionNeeded|eTraversalNeeded; | 
|  | 2552 | } | 
|  | 2553 | } else { | 
|  | 2554 | if (p->setChildLayer(layer, s.z)) { | 
|  | 2555 | flags |= eTransactionNeeded|eTraversalNeeded; | 
|  | 2556 | } | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 2557 | } | 
|  | 2558 | } | 
|  | 2559 | if (what & layer_state_t::eSizeChanged) { | 
|  | 2560 | if (layer->setSize(s.w, s.h)) { | 
|  | 2561 | flags |= eTraversalNeeded; | 
|  | 2562 | } | 
|  | 2563 | } | 
|  | 2564 | if (what & layer_state_t::eAlphaChanged) { | 
|  | 2565 | if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f))) | 
|  | 2566 | flags |= eTraversalNeeded; | 
|  | 2567 | } | 
|  | 2568 | if (what & layer_state_t::eMatrixChanged) { | 
|  | 2569 | if (layer->setMatrix(s.matrix)) | 
|  | 2570 | flags |= eTraversalNeeded; | 
|  | 2571 | } | 
|  | 2572 | if (what & layer_state_t::eTransparentRegionChanged) { | 
|  | 2573 | if (layer->setTransparentRegionHint(s.transparentRegion)) | 
|  | 2574 | flags |= eTraversalNeeded; | 
|  | 2575 | } | 
|  | 2576 | if (what & layer_state_t::eFlagsChanged) { | 
|  | 2577 | if (layer->setFlags(s.flags, s.mask)) | 
|  | 2578 | flags |= eTraversalNeeded; | 
|  | 2579 | } | 
|  | 2580 | if (what & layer_state_t::eCropChanged) { | 
|  | 2581 | if (layer->setCrop(s.crop, !geometryAppliesWithResize)) | 
|  | 2582 | flags |= eTraversalNeeded; | 
|  | 2583 | } | 
|  | 2584 | if (what & layer_state_t::eFinalCropChanged) { | 
| Robert Carr | 6fb9ca7 | 2017-03-22 18:23:23 -0700 | [diff] [blame] | 2585 | if (layer->setFinalCrop(s.finalCrop, !geometryAppliesWithResize)) | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 2586 | flags |= eTraversalNeeded; | 
|  | 2587 | } | 
|  | 2588 | if (what & layer_state_t::eLayerStackChanged) { | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 2589 | ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer); | 
| Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 2590 | // We only allow setting layer stacks for top level layers, | 
|  | 2591 | // everything else inherits layer stack from its parent. | 
|  | 2592 | if (layer->hasParent()) { | 
|  | 2593 | ALOGE("Attempt to set layer stack on layer with parent (%s) is invalid", | 
|  | 2594 | layer->getName().string()); | 
|  | 2595 | } else if (idx < 0) { | 
|  | 2596 | ALOGE("Attempt to set layer stack on layer without parent (%s) that " | 
|  | 2597 | "that also does not appear in the top level layer list. Something" | 
|  | 2598 | " has gone wrong.", layer->getName().string()); | 
|  | 2599 | } else if (layer->setLayerStack(s.layerStack)) { | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 2600 | mCurrentState.layersSortedByZ.removeAt(idx); | 
|  | 2601 | mCurrentState.layersSortedByZ.add(layer); | 
|  | 2602 | // we need traversal (state changed) | 
|  | 2603 | // AND transaction (list changed) | 
|  | 2604 | flags |= eTransactionNeeded|eTraversalNeeded; | 
|  | 2605 | } | 
|  | 2606 | } | 
|  | 2607 | if (what & layer_state_t::eDeferTransaction) { | 
| Robert Carr | 0d48072 | 2017-01-10 16:42:54 -0800 | [diff] [blame] | 2608 | if (s.barrierHandle != nullptr) { | 
|  | 2609 | layer->deferTransactionUntil(s.barrierHandle, s.frameNumber); | 
|  | 2610 | } else if (s.barrierGbp != nullptr) { | 
|  | 2611 | const sp<IGraphicBufferProducer>& gbp = s.barrierGbp; | 
|  | 2612 | if (authenticateSurfaceTextureLocked(gbp)) { | 
|  | 2613 | const auto& otherLayer = | 
|  | 2614 | (static_cast<MonitoredProducer*>(gbp.get()))->getLayer(); | 
|  | 2615 | layer->deferTransactionUntil(otherLayer, s.frameNumber); | 
|  | 2616 | } else { | 
|  | 2617 | ALOGE("Attempt to defer transaction to to an" | 
|  | 2618 | " unrecognized GraphicBufferProducer"); | 
|  | 2619 | } | 
|  | 2620 | } | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 2621 | // We don't trigger a traversal here because if no other state is | 
|  | 2622 | // changed, we don't want this to cause any more work | 
|  | 2623 | } | 
| Robert Carr | 1db73f6 | 2016-12-21 12:58:51 -0800 | [diff] [blame] | 2624 | if (what & layer_state_t::eReparentChildren) { | 
|  | 2625 | if (layer->reparentChildren(s.reparentHandle)) { | 
|  | 2626 | flags |= eTransactionNeeded|eTraversalNeeded; | 
|  | 2627 | } | 
|  | 2628 | } | 
| Robert Carr | 9524cb3 | 2017-02-13 11:32:32 -0800 | [diff] [blame] | 2629 | if (what & layer_state_t::eDetachChildren) { | 
|  | 2630 | layer->detachChildren(); | 
|  | 2631 | } | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 2632 | if (what & layer_state_t::eOverrideScalingModeChanged) { | 
|  | 2633 | layer->setOverrideScalingMode(s.overrideScalingMode); | 
|  | 2634 | // We don't trigger a traversal here because if no other state is | 
|  | 2635 | // changed, we don't want this to cause any more work | 
|  | 2636 | } | 
|  | 2637 | } | 
|  | 2638 | return flags; | 
|  | 2639 | } | 
|  | 2640 |  | 
|  | 2641 | status_t SurfaceFlinger::createLayer( | 
|  | 2642 | const String8& name, | 
|  | 2643 | const sp<Client>& client, | 
|  | 2644 | uint32_t w, uint32_t h, PixelFormat format, uint32_t flags, | 
| Albert Chaulk | 479c60c | 2017-01-27 14:21:34 -0500 | [diff] [blame] | 2645 | uint32_t windowType, uint32_t ownerUid, sp<IBinder>* handle, | 
|  | 2646 | sp<IGraphicBufferProducer>* gbp, sp<Layer>* parent) | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 2647 | { | 
|  | 2648 | if (int32_t(w|h) < 0) { | 
|  | 2649 | ALOGE("createLayer() failed, w or h is negative (w=%d, h=%d)", | 
|  | 2650 | int(w), int(h)); | 
|  | 2651 | return BAD_VALUE; | 
|  | 2652 | } | 
|  | 2653 |  | 
|  | 2654 | status_t result = NO_ERROR; | 
|  | 2655 |  | 
|  | 2656 | sp<Layer> layer; | 
|  | 2657 |  | 
| Cody Northrop | bc75528 | 2017-03-31 12:00:08 -0600 | [diff] [blame] | 2658 | String8 uniqueName = getUniqueLayerName(name); | 
|  | 2659 |  | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 2660 | switch (flags & ISurfaceComposerClient::eFXSurfaceMask) { | 
|  | 2661 | case ISurfaceComposerClient::eFXSurfaceNormal: | 
|  | 2662 | result = createNormalLayer(client, | 
| Cody Northrop | bc75528 | 2017-03-31 12:00:08 -0600 | [diff] [blame] | 2663 | uniqueName, w, h, flags, format, | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 2664 | handle, gbp, &layer); | 
|  | 2665 | break; | 
|  | 2666 | case ISurfaceComposerClient::eFXSurfaceDim: | 
|  | 2667 | result = createDimLayer(client, | 
| Cody Northrop | bc75528 | 2017-03-31 12:00:08 -0600 | [diff] [blame] | 2668 | uniqueName, w, h, flags, | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 2669 | handle, gbp, &layer); | 
|  | 2670 | break; | 
|  | 2671 | default: | 
|  | 2672 | result = BAD_VALUE; | 
|  | 2673 | break; | 
|  | 2674 | } | 
|  | 2675 |  | 
|  | 2676 | if (result != NO_ERROR) { | 
|  | 2677 | return result; | 
|  | 2678 | } | 
|  | 2679 |  | 
| Albert Chaulk | 479c60c | 2017-01-27 14:21:34 -0500 | [diff] [blame] | 2680 | layer->setInfo(windowType, ownerUid); | 
|  | 2681 |  | 
| Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 2682 | result = addClientLayer(client, *handle, *gbp, layer, *parent); | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 2683 | if (result != NO_ERROR) { | 
|  | 2684 | return result; | 
|  | 2685 | } | 
|  | 2686 | mInterceptor.saveSurfaceCreation(layer); | 
|  | 2687 |  | 
|  | 2688 | setTransactionFlags(eTransactionNeeded); | 
|  | 2689 | return result; | 
|  | 2690 | } | 
|  | 2691 |  | 
| Cody Northrop | bc75528 | 2017-03-31 12:00:08 -0600 | [diff] [blame] | 2692 | String8 SurfaceFlinger::getUniqueLayerName(const String8& name) | 
|  | 2693 | { | 
|  | 2694 | bool matchFound = true; | 
|  | 2695 | uint32_t dupeCounter = 0; | 
|  | 2696 |  | 
|  | 2697 | // Tack on our counter whether there is a hit or not, so everyone gets a tag | 
|  | 2698 | String8 uniqueName = name + "#" + String8(std::to_string(dupeCounter).c_str()); | 
|  | 2699 |  | 
|  | 2700 | // Loop over layers until we're sure there is no matching name | 
|  | 2701 | while (matchFound) { | 
|  | 2702 | matchFound = false; | 
|  | 2703 | mDrawingState.traverseInZOrder([&](Layer* layer) { | 
|  | 2704 | if (layer->getName() == uniqueName) { | 
|  | 2705 | matchFound = true; | 
|  | 2706 | uniqueName = name + "#" + String8(std::to_string(++dupeCounter).c_str()); | 
|  | 2707 | } | 
|  | 2708 | }); | 
|  | 2709 | } | 
|  | 2710 |  | 
|  | 2711 | ALOGD_IF(dupeCounter > 0, "duplicate layer name: changing %s to %s", name.c_str(), uniqueName.c_str()); | 
|  | 2712 |  | 
|  | 2713 | return uniqueName; | 
|  | 2714 | } | 
|  | 2715 |  | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 2716 | status_t SurfaceFlinger::createNormalLayer(const sp<Client>& client, | 
|  | 2717 | const String8& name, uint32_t w, uint32_t h, uint32_t flags, PixelFormat& format, | 
|  | 2718 | sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer) | 
|  | 2719 | { | 
|  | 2720 | // initialize the surfaces | 
|  | 2721 | switch (format) { | 
|  | 2722 | case PIXEL_FORMAT_TRANSPARENT: | 
|  | 2723 | case PIXEL_FORMAT_TRANSLUCENT: | 
|  | 2724 | format = PIXEL_FORMAT_RGBA_8888; | 
|  | 2725 | break; | 
|  | 2726 | case PIXEL_FORMAT_OPAQUE: | 
|  | 2727 | format = PIXEL_FORMAT_RGBX_8888; | 
|  | 2728 | break; | 
|  | 2729 | } | 
|  | 2730 |  | 
|  | 2731 | *outLayer = new Layer(this, client, name, w, h, flags); | 
|  | 2732 | status_t err = (*outLayer)->setBuffers(w, h, format, flags); | 
|  | 2733 | if (err == NO_ERROR) { | 
|  | 2734 | *handle = (*outLayer)->getHandle(); | 
|  | 2735 | *gbp = (*outLayer)->getProducer(); | 
|  | 2736 | } | 
|  | 2737 |  | 
|  | 2738 | ALOGE_IF(err, "createNormalLayer() failed (%s)", strerror(-err)); | 
|  | 2739 | return err; | 
|  | 2740 | } | 
|  | 2741 |  | 
|  | 2742 | status_t SurfaceFlinger::createDimLayer(const sp<Client>& client, | 
|  | 2743 | const String8& name, uint32_t w, uint32_t h, uint32_t flags, | 
|  | 2744 | sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer) | 
|  | 2745 | { | 
|  | 2746 | *outLayer = new LayerDim(this, client, name, w, h, flags); | 
|  | 2747 | *handle = (*outLayer)->getHandle(); | 
|  | 2748 | *gbp = (*outLayer)->getProducer(); | 
|  | 2749 | return NO_ERROR; | 
|  | 2750 | } | 
|  | 2751 |  | 
|  | 2752 | status_t SurfaceFlinger::onLayerRemoved(const sp<Client>& client, const sp<IBinder>& handle) | 
|  | 2753 | { | 
| Robert Carr | 9524cb3 | 2017-02-13 11:32:32 -0800 | [diff] [blame] | 2754 | // called by a client when it wants to remove a Layer | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 2755 | status_t err = NO_ERROR; | 
|  | 2756 | sp<Layer> l(client->getLayerUser(handle)); | 
|  | 2757 | if (l != NULL) { | 
|  | 2758 | mInterceptor.saveSurfaceDeletion(l); | 
|  | 2759 | err = removeLayer(l); | 
|  | 2760 | ALOGE_IF(err<0 && err != NAME_NOT_FOUND, | 
|  | 2761 | "error removing layer=%p (%s)", l.get(), strerror(-err)); | 
|  | 2762 | } | 
|  | 2763 | return err; | 
|  | 2764 | } | 
|  | 2765 |  | 
|  | 2766 | status_t SurfaceFlinger::onLayerDestroyed(const wp<Layer>& layer) | 
|  | 2767 | { | 
|  | 2768 | // called by ~LayerCleaner() when all references to the IBinder (handle) | 
|  | 2769 | // are gone | 
| Robert Carr | 9524cb3 | 2017-02-13 11:32:32 -0800 | [diff] [blame] | 2770 | sp<Layer> l = layer.promote(); | 
|  | 2771 | if (l == nullptr) { | 
|  | 2772 | // The layer has already been removed, carry on | 
|  | 2773 | return NO_ERROR; | 
|  | 2774 | } if (l->getParent() != nullptr) { | 
|  | 2775 | // If we have a parent, then we can continue to live as long as it does. | 
|  | 2776 | return NO_ERROR; | 
|  | 2777 | } | 
|  | 2778 | return removeLayer(l); | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 2779 | } | 
|  | 2780 |  | 
|  | 2781 | // --------------------------------------------------------------------------- | 
|  | 2782 |  | 
|  | 2783 | void SurfaceFlinger::onInitializeDisplays() { | 
|  | 2784 | // reset screen orientation and use primary layer stack | 
|  | 2785 | Vector<ComposerState> state; | 
|  | 2786 | Vector<DisplayState> displays; | 
|  | 2787 | DisplayState d; | 
|  | 2788 | d.what = DisplayState::eDisplayProjectionChanged | | 
|  | 2789 | DisplayState::eLayerStackChanged; | 
|  | 2790 | d.token = mBuiltinDisplays[DisplayDevice::DISPLAY_PRIMARY]; | 
|  | 2791 | d.layerStack = 0; | 
|  | 2792 | d.orientation = DisplayState::eOrientationDefault; | 
|  | 2793 | d.frame.makeInvalid(); | 
|  | 2794 | d.viewport.makeInvalid(); | 
|  | 2795 | d.width = 0; | 
|  | 2796 | d.height = 0; | 
|  | 2797 | displays.add(d); | 
|  | 2798 | setTransactionState(state, displays, 0); | 
|  | 2799 | setPowerModeInternal(getDisplayDevice(d.token), HWC_POWER_MODE_NORMAL); | 
|  | 2800 |  | 
|  | 2801 | const nsecs_t period = | 
|  | 2802 | getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY); | 
|  | 2803 | mAnimFrameTracker.setDisplayRefreshPeriod(period); | 
| Brian Anderson | d001058 | 2017-03-07 13:20:31 -0800 | [diff] [blame] | 2804 |  | 
|  | 2805 | // Use phase of 0 since phase is not known. | 
|  | 2806 | // Use latency of 0, which will snap to the ideal latency. | 
|  | 2807 | setCompositorTimingSnapped(0, period, 0); | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 2808 | } | 
|  | 2809 |  | 
|  | 2810 | void SurfaceFlinger::initializeDisplays() { | 
|  | 2811 | class MessageScreenInitialized : public MessageBase { | 
|  | 2812 | SurfaceFlinger* flinger; | 
|  | 2813 | public: | 
|  | 2814 | explicit MessageScreenInitialized(SurfaceFlinger* flinger) : flinger(flinger) { } | 
|  | 2815 | virtual bool handler() { | 
|  | 2816 | flinger->onInitializeDisplays(); | 
|  | 2817 | return true; | 
|  | 2818 | } | 
|  | 2819 | }; | 
|  | 2820 | sp<MessageBase> msg = new MessageScreenInitialized(this); | 
|  | 2821 | postMessageAsync(msg);  // we may be called from main thread, use async message | 
|  | 2822 | } | 
|  | 2823 |  | 
|  | 2824 | void SurfaceFlinger::setPowerModeInternal(const sp<DisplayDevice>& hw, | 
|  | 2825 | int mode) { | 
|  | 2826 | ALOGD("Set power mode=%d, type=%d flinger=%p", mode, hw->getDisplayType(), | 
|  | 2827 | this); | 
|  | 2828 | int32_t type = hw->getDisplayType(); | 
|  | 2829 | int currentMode = hw->getPowerMode(); | 
|  | 2830 |  | 
|  | 2831 | if (mode == currentMode) { | 
|  | 2832 | ALOGD("Screen type=%d is already mode=%d", hw->getDisplayType(), mode); | 
|  | 2833 | return; | 
|  | 2834 | } | 
|  | 2835 |  | 
|  | 2836 | hw->setPowerMode(mode); | 
|  | 2837 | if (type >= DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) { | 
|  | 2838 | ALOGW("Trying to set power mode for virtual display"); | 
|  | 2839 | return; | 
|  | 2840 | } | 
|  | 2841 |  | 
|  | 2842 | if (mInterceptor.isEnabled()) { | 
|  | 2843 | Mutex::Autolock _l(mStateLock); | 
|  | 2844 | ssize_t idx = mCurrentState.displays.indexOfKey(hw->getDisplayToken()); | 
|  | 2845 | if (idx < 0) { | 
|  | 2846 | ALOGW("Surface Interceptor SavePowerMode: invalid display token"); | 
|  | 2847 | return; | 
|  | 2848 | } | 
|  | 2849 | mInterceptor.savePowerModeUpdate(mCurrentState.displays.valueAt(idx).displayId, mode); | 
|  | 2850 | } | 
|  | 2851 |  | 
|  | 2852 | if (currentMode == HWC_POWER_MODE_OFF) { | 
|  | 2853 | // Turn on the display | 
|  | 2854 | getHwComposer().setPowerMode(type, mode); | 
|  | 2855 | if (type == DisplayDevice::DISPLAY_PRIMARY) { | 
|  | 2856 | // FIXME: eventthread only knows about the main display right now | 
|  | 2857 | mEventThread->onScreenAcquired(); | 
|  | 2858 | resyncToHardwareVsync(true); | 
|  | 2859 | } | 
|  | 2860 |  | 
|  | 2861 | mVisibleRegionsDirty = true; | 
|  | 2862 | mHasPoweredOff = true; | 
|  | 2863 | repaintEverything(); | 
|  | 2864 |  | 
|  | 2865 | struct sched_param param = {0}; | 
|  | 2866 | param.sched_priority = 1; | 
|  | 2867 | if (sched_setscheduler(0, SCHED_FIFO, ¶m) != 0) { | 
|  | 2868 | ALOGW("Couldn't set SCHED_FIFO on display on"); | 
|  | 2869 | } | 
|  | 2870 | } else if (mode == HWC_POWER_MODE_OFF) { | 
|  | 2871 | // Turn off the display | 
|  | 2872 | struct sched_param param = {0}; | 
|  | 2873 | if (sched_setscheduler(0, SCHED_OTHER, ¶m) != 0) { | 
|  | 2874 | ALOGW("Couldn't set SCHED_OTHER on display off"); | 
|  | 2875 | } | 
|  | 2876 |  | 
|  | 2877 | if (type == DisplayDevice::DISPLAY_PRIMARY) { | 
|  | 2878 | disableHardwareVsync(true); // also cancels any in-progress resync | 
|  | 2879 |  | 
|  | 2880 | // FIXME: eventthread only knows about the main display right now | 
|  | 2881 | mEventThread->onScreenReleased(); | 
|  | 2882 | } | 
|  | 2883 |  | 
|  | 2884 | getHwComposer().setPowerMode(type, mode); | 
|  | 2885 | mVisibleRegionsDirty = true; | 
|  | 2886 | // from this point on, SF will stop drawing on this display | 
|  | 2887 | } else { | 
|  | 2888 | getHwComposer().setPowerMode(type, mode); | 
|  | 2889 | } | 
|  | 2890 | } | 
|  | 2891 |  | 
|  | 2892 | void SurfaceFlinger::setPowerMode(const sp<IBinder>& display, int mode) { | 
|  | 2893 | class MessageSetPowerMode: public MessageBase { | 
|  | 2894 | SurfaceFlinger& mFlinger; | 
|  | 2895 | sp<IBinder> mDisplay; | 
|  | 2896 | int mMode; | 
|  | 2897 | public: | 
|  | 2898 | MessageSetPowerMode(SurfaceFlinger& flinger, | 
|  | 2899 | const sp<IBinder>& disp, int mode) : mFlinger(flinger), | 
|  | 2900 | mDisplay(disp) { mMode = mode; } | 
|  | 2901 | virtual bool handler() { | 
|  | 2902 | sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay)); | 
|  | 2903 | if (hw == NULL) { | 
|  | 2904 | ALOGE("Attempt to set power mode = %d for null display %p", | 
|  | 2905 | mMode, mDisplay.get()); | 
|  | 2906 | } else if (hw->getDisplayType() >= DisplayDevice::DISPLAY_VIRTUAL) { | 
|  | 2907 | ALOGW("Attempt to set power mode = %d for virtual display", | 
|  | 2908 | mMode); | 
|  | 2909 | } else { | 
|  | 2910 | mFlinger.setPowerModeInternal(hw, mMode); | 
|  | 2911 | } | 
|  | 2912 | return true; | 
|  | 2913 | } | 
|  | 2914 | }; | 
|  | 2915 | sp<MessageBase> msg = new MessageSetPowerMode(*this, display, mode); | 
|  | 2916 | postMessageSync(msg); | 
|  | 2917 | } | 
|  | 2918 |  | 
|  | 2919 | // --------------------------------------------------------------------------- | 
|  | 2920 |  | 
|  | 2921 | status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args) | 
|  | 2922 | { | 
|  | 2923 | String8 result; | 
|  | 2924 |  | 
|  | 2925 | IPCThreadState* ipc = IPCThreadState::self(); | 
|  | 2926 | const int pid = ipc->getCallingPid(); | 
|  | 2927 | const int uid = ipc->getCallingUid(); | 
|  | 2928 | if ((uid != AID_SHELL) && | 
|  | 2929 | !PermissionCache::checkPermission(sDump, pid, uid)) { | 
|  | 2930 | result.appendFormat("Permission Denial: " | 
|  | 2931 | "can't dump SurfaceFlinger from pid=%d, uid=%d\n", pid, uid); | 
|  | 2932 | } else { | 
|  | 2933 | // Try to get the main lock, but give up after one second | 
|  | 2934 | // (this would indicate SF is stuck, but we want to be able to | 
|  | 2935 | // print something in dumpsys). | 
|  | 2936 | status_t err = mStateLock.timedLock(s2ns(1)); | 
|  | 2937 | bool locked = (err == NO_ERROR); | 
|  | 2938 | if (!locked) { | 
|  | 2939 | result.appendFormat( | 
|  | 2940 | "SurfaceFlinger appears to be unresponsive (%s [%d]), " | 
|  | 2941 | "dumping anyways (no locks held)\n", strerror(-err), err); | 
|  | 2942 | } | 
|  | 2943 |  | 
|  | 2944 | bool dumpAll = true; | 
|  | 2945 | size_t index = 0; | 
|  | 2946 | size_t numArgs = args.size(); | 
|  | 2947 | if (numArgs) { | 
|  | 2948 | if ((index < numArgs) && | 
|  | 2949 | (args[index] == String16("--list"))) { | 
|  | 2950 | index++; | 
|  | 2951 | listLayersLocked(args, index, result); | 
|  | 2952 | dumpAll = false; | 
|  | 2953 | } | 
|  | 2954 |  | 
|  | 2955 | if ((index < numArgs) && | 
|  | 2956 | (args[index] == String16("--latency"))) { | 
|  | 2957 | index++; | 
|  | 2958 | dumpStatsLocked(args, index, result); | 
|  | 2959 | dumpAll = false; | 
|  | 2960 | } | 
|  | 2961 |  | 
|  | 2962 | if ((index < numArgs) && | 
|  | 2963 | (args[index] == String16("--latency-clear"))) { | 
|  | 2964 | index++; | 
|  | 2965 | clearStatsLocked(args, index, result); | 
|  | 2966 | dumpAll = false; | 
|  | 2967 | } | 
|  | 2968 |  | 
|  | 2969 | if ((index < numArgs) && | 
|  | 2970 | (args[index] == String16("--dispsync"))) { | 
|  | 2971 | index++; | 
|  | 2972 | mPrimaryDispSync.dump(result); | 
|  | 2973 | dumpAll = false; | 
|  | 2974 | } | 
|  | 2975 |  | 
|  | 2976 | if ((index < numArgs) && | 
|  | 2977 | (args[index] == String16("--static-screen"))) { | 
|  | 2978 | index++; | 
|  | 2979 | dumpStaticScreenStats(result); | 
|  | 2980 | dumpAll = false; | 
|  | 2981 | } | 
|  | 2982 |  | 
|  | 2983 | if ((index < numArgs) && | 
| Brian Anderson | d6927fb | 2016-07-23 23:37:30 -0700 | [diff] [blame] | 2984 | (args[index] == String16("--frame-events"))) { | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 2985 | index++; | 
| Brian Anderson | d6927fb | 2016-07-23 23:37:30 -0700 | [diff] [blame] | 2986 | dumpFrameEventsLocked(result); | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 2987 | dumpAll = false; | 
|  | 2988 | } | 
|  | 2989 | } | 
|  | 2990 |  | 
|  | 2991 | if (dumpAll) { | 
|  | 2992 | dumpAllLocked(args, index, result); | 
|  | 2993 | } | 
|  | 2994 |  | 
|  | 2995 | if (locked) { | 
|  | 2996 | mStateLock.unlock(); | 
|  | 2997 | } | 
|  | 2998 | } | 
|  | 2999 | write(fd, result.string(), result.size()); | 
|  | 3000 | return NO_ERROR; | 
|  | 3001 | } | 
|  | 3002 |  | 
|  | 3003 | void SurfaceFlinger::listLayersLocked(const Vector<String16>& /* args */, | 
|  | 3004 | size_t& /* index */, String8& result) const | 
|  | 3005 | { | 
| Robert Carr | 2047fae | 2016-11-28 14:09:09 -0800 | [diff] [blame] | 3006 | mCurrentState.traverseInZOrder([&](Layer* layer) { | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 3007 | result.appendFormat("%s\n", layer->getName().string()); | 
| Robert Carr | 2047fae | 2016-11-28 14:09:09 -0800 | [diff] [blame] | 3008 | }); | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 3009 | } | 
|  | 3010 |  | 
|  | 3011 | void SurfaceFlinger::dumpStatsLocked(const Vector<String16>& args, size_t& index, | 
|  | 3012 | String8& result) const | 
|  | 3013 | { | 
|  | 3014 | String8 name; | 
|  | 3015 | if (index < args.size()) { | 
|  | 3016 | name = String8(args[index]); | 
|  | 3017 | index++; | 
|  | 3018 | } | 
|  | 3019 |  | 
|  | 3020 | const nsecs_t period = | 
|  | 3021 | getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY); | 
|  | 3022 | result.appendFormat("%" PRId64 "\n", period); | 
|  | 3023 |  | 
|  | 3024 | if (name.isEmpty()) { | 
|  | 3025 | mAnimFrameTracker.dumpStats(result); | 
|  | 3026 | } else { | 
| Robert Carr | 2047fae | 2016-11-28 14:09:09 -0800 | [diff] [blame] | 3027 | mCurrentState.traverseInZOrder([&](Layer* layer) { | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 3028 | if (name == layer->getName()) { | 
|  | 3029 | layer->dumpFrameStats(result); | 
|  | 3030 | } | 
| Robert Carr | 2047fae | 2016-11-28 14:09:09 -0800 | [diff] [blame] | 3031 | }); | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 3032 | } | 
|  | 3033 | } | 
|  | 3034 |  | 
|  | 3035 | void SurfaceFlinger::clearStatsLocked(const Vector<String16>& args, size_t& index, | 
|  | 3036 | String8& /* result */) | 
|  | 3037 | { | 
|  | 3038 | String8 name; | 
|  | 3039 | if (index < args.size()) { | 
|  | 3040 | name = String8(args[index]); | 
|  | 3041 | index++; | 
|  | 3042 | } | 
|  | 3043 |  | 
| Robert Carr | 2047fae | 2016-11-28 14:09:09 -0800 | [diff] [blame] | 3044 | mCurrentState.traverseInZOrder([&](Layer* layer) { | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 3045 | if (name.isEmpty() || (name == layer->getName())) { | 
|  | 3046 | layer->clearFrameStats(); | 
|  | 3047 | } | 
| Robert Carr | 2047fae | 2016-11-28 14:09:09 -0800 | [diff] [blame] | 3048 | }); | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 3049 |  | 
|  | 3050 | mAnimFrameTracker.clearStats(); | 
|  | 3051 | } | 
|  | 3052 |  | 
|  | 3053 | // This should only be called from the main thread.  Otherwise it would need | 
|  | 3054 | // the lock and should use mCurrentState rather than mDrawingState. | 
|  | 3055 | void SurfaceFlinger::logFrameStats() { | 
| Robert Carr | 2047fae | 2016-11-28 14:09:09 -0800 | [diff] [blame] | 3056 | mDrawingState.traverseInZOrder([&](Layer* layer) { | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 3057 | layer->logFrameStats(); | 
| Robert Carr | 2047fae | 2016-11-28 14:09:09 -0800 | [diff] [blame] | 3058 | }); | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 3059 |  | 
|  | 3060 | mAnimFrameTracker.logAndResetStats(String8("<win-anim>")); | 
|  | 3061 | } | 
|  | 3062 |  | 
| Fabien Sanglard | 63a5fcd | 2016-12-29 15:13:07 -0800 | [diff] [blame] | 3063 | void SurfaceFlinger::appendSfConfigString(String8& result) const | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 3064 | { | 
| Fabien Sanglard | 63a5fcd | 2016-12-29 15:13:07 -0800 | [diff] [blame] | 3065 | result.append(" [sf"); | 
| Fabien Sanglard | c93afd5 | 2017-03-13 13:02:42 -0700 | [diff] [blame] | 3066 | result.appendFormat(" HAS_CONTEXT_PRIORITY=%d", useContextPriority); | 
|  | 3067 |  | 
| Fabien Sanglard | 63a5fcd | 2016-12-29 15:13:07 -0800 | [diff] [blame] | 3068 | if (isLayerTripleBufferingDisabled()) | 
|  | 3069 | result.append(" DISABLE_TRIPLE_BUFFERING"); | 
| Fabien Sanglard | a34ed63 | 2017-03-14 11:43:52 -0700 | [diff] [blame] | 3070 |  | 
| Fabien Sanglard | c45a7d9 | 2017-03-14 13:24:22 -0700 | [diff] [blame] | 3071 | result.appendFormat(" PRESENT_TIME_OFFSET=%" PRId64, dispSyncPresentTimeOffset); | 
| Fabien Sanglard | a34ed63 | 2017-03-14 11:43:52 -0700 | [diff] [blame] | 3072 | result.appendFormat(" FORCE_HWC_FOR_RBG_TO_YUV=%d", useHwcForRgbToYuv); | 
| Fabien Sanglard | c8e387e | 2017-03-10 10:30:28 -0800 | [diff] [blame] | 3073 | result.appendFormat(" MAX_VIRT_DISPLAY_DIM=%" PRIu64, maxVirtualDisplaySize); | 
| Fabien Sanglard | cbf153b | 2017-03-10 17:57:12 -0800 | [diff] [blame] | 3074 | result.appendFormat(" RUNNING_WITHOUT_SYNC_FRAMEWORK=%d", !hasSyncFramework); | 
| Fabien Sanglard | 1971b63 | 2017-03-10 14:50:03 -0800 | [diff] [blame] | 3075 | result.appendFormat(" NUM_FRAMEBUFFER_SURFACE_BUFFERS=%" PRId64, | 
|  | 3076 | maxFrameBufferAcquiredBuffers); | 
| Fabien Sanglard | 63a5fcd | 2016-12-29 15:13:07 -0800 | [diff] [blame] | 3077 | result.append("]"); | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 3078 | } | 
|  | 3079 |  | 
|  | 3080 | void SurfaceFlinger::dumpStaticScreenStats(String8& result) const | 
|  | 3081 | { | 
|  | 3082 | result.appendFormat("Static screen stats:\n"); | 
|  | 3083 | for (size_t b = 0; b < NUM_BUCKETS - 1; ++b) { | 
|  | 3084 | float bucketTimeSec = mFrameBuckets[b] / 1e9; | 
|  | 3085 | float percent = 100.0f * | 
|  | 3086 | static_cast<float>(mFrameBuckets[b]) / mTotalTime; | 
|  | 3087 | result.appendFormat("  < %zd frames: %.3f s (%.1f%%)\n", | 
|  | 3088 | b + 1, bucketTimeSec, percent); | 
|  | 3089 | } | 
|  | 3090 | float bucketTimeSec = mFrameBuckets[NUM_BUCKETS - 1] / 1e9; | 
|  | 3091 | float percent = 100.0f * | 
|  | 3092 | static_cast<float>(mFrameBuckets[NUM_BUCKETS - 1]) / mTotalTime; | 
|  | 3093 | result.appendFormat("  %zd+ frames: %.3f s (%.1f%%)\n", | 
|  | 3094 | NUM_BUCKETS - 1, bucketTimeSec, percent); | 
|  | 3095 | } | 
|  | 3096 |  | 
| Brian Anderson | d6927fb | 2016-07-23 23:37:30 -0700 | [diff] [blame] | 3097 | void SurfaceFlinger::dumpFrameEventsLocked(String8& result) { | 
|  | 3098 | result.appendFormat("Layer frame timestamps:\n"); | 
|  | 3099 |  | 
|  | 3100 | const LayerVector& currentLayers = mCurrentState.layersSortedByZ; | 
|  | 3101 | const size_t count = currentLayers.size(); | 
|  | 3102 | for (size_t i=0 ; i<count ; i++) { | 
|  | 3103 | currentLayers[i]->dumpFrameEvents(result); | 
|  | 3104 | } | 
|  | 3105 | } | 
|  | 3106 |  | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 3107 | void SurfaceFlinger::recordBufferingStats(const char* layerName, | 
|  | 3108 | std::vector<OccupancyTracker::Segment>&& history) { | 
|  | 3109 | Mutex::Autolock lock(mBufferingStatsMutex); | 
|  | 3110 | auto& stats = mBufferingStats[layerName]; | 
|  | 3111 | for (const auto& segment : history) { | 
|  | 3112 | if (!segment.usedThirdBuffer) { | 
|  | 3113 | stats.twoBufferTime += segment.totalTime; | 
|  | 3114 | } | 
|  | 3115 | if (segment.occupancyAverage < 1.0f) { | 
|  | 3116 | stats.doubleBufferedTime += segment.totalTime; | 
|  | 3117 | } else if (segment.occupancyAverage < 2.0f) { | 
|  | 3118 | stats.tripleBufferedTime += segment.totalTime; | 
|  | 3119 | } | 
|  | 3120 | ++stats.numSegments; | 
|  | 3121 | stats.totalTime += segment.totalTime; | 
|  | 3122 | } | 
|  | 3123 | } | 
|  | 3124 |  | 
|  | 3125 | void SurfaceFlinger::dumpBufferingStats(String8& result) const { | 
|  | 3126 | result.append("Buffering stats:\n"); | 
|  | 3127 | result.append("  [Layer name] <Active time> <Two buffer> " | 
|  | 3128 | "<Double buffered> <Triple buffered>\n"); | 
|  | 3129 | Mutex::Autolock lock(mBufferingStatsMutex); | 
|  | 3130 | typedef std::tuple<std::string, float, float, float> BufferTuple; | 
|  | 3131 | std::map<float, BufferTuple, std::greater<float>> sorted; | 
|  | 3132 | for (const auto& statsPair : mBufferingStats) { | 
|  | 3133 | const char* name = statsPair.first.c_str(); | 
|  | 3134 | const BufferingStats& stats = statsPair.second; | 
|  | 3135 | if (stats.numSegments == 0) { | 
|  | 3136 | continue; | 
|  | 3137 | } | 
|  | 3138 | float activeTime = ns2ms(stats.totalTime) / 1000.0f; | 
|  | 3139 | float twoBufferRatio = static_cast<float>(stats.twoBufferTime) / | 
|  | 3140 | stats.totalTime; | 
|  | 3141 | float doubleBufferRatio = static_cast<float>( | 
|  | 3142 | stats.doubleBufferedTime) / stats.totalTime; | 
|  | 3143 | float tripleBufferRatio = static_cast<float>( | 
|  | 3144 | stats.tripleBufferedTime) / stats.totalTime; | 
|  | 3145 | sorted.insert({activeTime, {name, twoBufferRatio, | 
|  | 3146 | doubleBufferRatio, tripleBufferRatio}}); | 
|  | 3147 | } | 
|  | 3148 | for (const auto& sortedPair : sorted) { | 
|  | 3149 | float activeTime = sortedPair.first; | 
|  | 3150 | const BufferTuple& values = sortedPair.second; | 
|  | 3151 | result.appendFormat("  [%s] %.2f %.3f %.3f %.3f\n", | 
|  | 3152 | std::get<0>(values).c_str(), activeTime, | 
|  | 3153 | std::get<1>(values), std::get<2>(values), | 
|  | 3154 | std::get<3>(values)); | 
|  | 3155 | } | 
|  | 3156 | result.append("\n"); | 
|  | 3157 | } | 
|  | 3158 |  | 
|  | 3159 | void SurfaceFlinger::dumpAllLocked(const Vector<String16>& args, size_t& index, | 
|  | 3160 | String8& result) const | 
|  | 3161 | { | 
|  | 3162 | bool colorize = false; | 
|  | 3163 | if (index < args.size() | 
|  | 3164 | && (args[index] == String16("--color"))) { | 
|  | 3165 | colorize = true; | 
|  | 3166 | index++; | 
|  | 3167 | } | 
|  | 3168 |  | 
|  | 3169 | Colorizer colorizer(colorize); | 
|  | 3170 |  | 
|  | 3171 | // figure out if we're stuck somewhere | 
|  | 3172 | const nsecs_t now = systemTime(); | 
|  | 3173 | const nsecs_t inSwapBuffers(mDebugInSwapBuffers); | 
|  | 3174 | const nsecs_t inTransaction(mDebugInTransaction); | 
|  | 3175 | nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0; | 
|  | 3176 | nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0; | 
|  | 3177 |  | 
|  | 3178 | /* | 
|  | 3179 | * Dump library configuration. | 
|  | 3180 | */ | 
|  | 3181 |  | 
|  | 3182 | colorizer.bold(result); | 
|  | 3183 | result.append("Build configuration:"); | 
|  | 3184 | colorizer.reset(result); | 
|  | 3185 | appendSfConfigString(result); | 
|  | 3186 | appendUiConfigString(result); | 
|  | 3187 | appendGuiConfigString(result); | 
|  | 3188 | result.append("\n"); | 
|  | 3189 |  | 
|  | 3190 | colorizer.bold(result); | 
|  | 3191 | result.append("Sync configuration: "); | 
|  | 3192 | colorizer.reset(result); | 
|  | 3193 | result.append(SyncFeatures::getInstance().toString()); | 
|  | 3194 | result.append("\n"); | 
|  | 3195 |  | 
|  | 3196 | colorizer.bold(result); | 
|  | 3197 | result.append("DispSync configuration: "); | 
|  | 3198 | colorizer.reset(result); | 
|  | 3199 | result.appendFormat("app phase %" PRId64 " ns, sf phase %" PRId64 " ns, " | 
| Fabien Sanglard | c45a7d9 | 2017-03-14 13:24:22 -0700 | [diff] [blame] | 3200 | "present offset %" PRId64 " ns (refresh %" PRId64 " ns)", | 
|  | 3201 | vsyncPhaseOffsetNs, sfVsyncPhaseOffsetNs, dispSyncPresentTimeOffset, | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 3202 | mHwc->getRefreshPeriod(HWC_DISPLAY_PRIMARY)); | 
|  | 3203 | result.append("\n"); | 
|  | 3204 |  | 
|  | 3205 | // Dump static screen stats | 
|  | 3206 | result.append("\n"); | 
|  | 3207 | dumpStaticScreenStats(result); | 
|  | 3208 | result.append("\n"); | 
|  | 3209 |  | 
|  | 3210 | dumpBufferingStats(result); | 
|  | 3211 |  | 
|  | 3212 | /* | 
|  | 3213 | * Dump the visible layer list | 
|  | 3214 | */ | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 3215 | colorizer.bold(result); | 
| Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 3216 | result.appendFormat("Visible layers (count = %zu)\n", mNumLayers); | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 3217 | colorizer.reset(result); | 
| Robert Carr | 2047fae | 2016-11-28 14:09:09 -0800 | [diff] [blame] | 3218 | mCurrentState.traverseInZOrder([&](Layer* layer) { | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 3219 | layer->dump(result, colorizer); | 
| Robert Carr | 2047fae | 2016-11-28 14:09:09 -0800 | [diff] [blame] | 3220 | }); | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 3221 |  | 
|  | 3222 | /* | 
|  | 3223 | * Dump Display state | 
|  | 3224 | */ | 
|  | 3225 |  | 
|  | 3226 | colorizer.bold(result); | 
|  | 3227 | result.appendFormat("Displays (%zu entries)\n", mDisplays.size()); | 
|  | 3228 | colorizer.reset(result); | 
|  | 3229 | for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) { | 
|  | 3230 | const sp<const DisplayDevice>& hw(mDisplays[dpy]); | 
|  | 3231 | hw->dump(result); | 
|  | 3232 | } | 
|  | 3233 |  | 
|  | 3234 | /* | 
|  | 3235 | * Dump SurfaceFlinger global state | 
|  | 3236 | */ | 
|  | 3237 |  | 
|  | 3238 | colorizer.bold(result); | 
|  | 3239 | result.append("SurfaceFlinger global state:\n"); | 
|  | 3240 | colorizer.reset(result); | 
|  | 3241 |  | 
|  | 3242 | HWComposer& hwc(getHwComposer()); | 
| Stephen Kiazyk | 8d6c16d | 2017-04-05 16:46:49 -0700 | [diff] [blame] | 3243 | sp<const DisplayDevice> hw(getDefaultDisplayDeviceLocked()); | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 3244 |  | 
|  | 3245 | colorizer.bold(result); | 
|  | 3246 | result.appendFormat("EGL implementation : %s\n", | 
|  | 3247 | eglQueryStringImplementationANDROID(mEGLDisplay, EGL_VERSION)); | 
|  | 3248 | colorizer.reset(result); | 
|  | 3249 | result.appendFormat("%s\n", | 
|  | 3250 | eglQueryStringImplementationANDROID(mEGLDisplay, EGL_EXTENSIONS)); | 
|  | 3251 |  | 
|  | 3252 | mRenderEngine->dump(result); | 
|  | 3253 |  | 
|  | 3254 | hw->undefinedRegion.dump(result, "undefinedRegion"); | 
|  | 3255 | result.appendFormat("  orientation=%d, isDisplayOn=%d\n", | 
|  | 3256 | hw->getOrientation(), hw->isDisplayOn()); | 
|  | 3257 | result.appendFormat( | 
|  | 3258 | "  last eglSwapBuffers() time: %f us\n" | 
|  | 3259 | "  last transaction time     : %f us\n" | 
|  | 3260 | "  transaction-flags         : %08x\n" | 
|  | 3261 | "  refresh-rate              : %f fps\n" | 
|  | 3262 | "  x-dpi                     : %f\n" | 
|  | 3263 | "  y-dpi                     : %f\n" | 
|  | 3264 | "  gpu_to_cpu_unsupported    : %d\n" | 
|  | 3265 | , | 
|  | 3266 | mLastSwapBufferTime/1000.0, | 
|  | 3267 | mLastTransactionTime/1000.0, | 
|  | 3268 | mTransactionFlags, | 
|  | 3269 | 1e9 / hwc.getRefreshPeriod(HWC_DISPLAY_PRIMARY), | 
|  | 3270 | hwc.getDpiX(HWC_DISPLAY_PRIMARY), | 
|  | 3271 | hwc.getDpiY(HWC_DISPLAY_PRIMARY), | 
|  | 3272 | !mGpuToCpuSupported); | 
|  | 3273 |  | 
|  | 3274 | result.appendFormat("  eglSwapBuffers time: %f us\n", | 
|  | 3275 | inSwapBuffersDuration/1000.0); | 
|  | 3276 |  | 
|  | 3277 | result.appendFormat("  transaction time: %f us\n", | 
|  | 3278 | inTransactionDuration/1000.0); | 
|  | 3279 |  | 
|  | 3280 | /* | 
|  | 3281 | * VSYNC state | 
|  | 3282 | */ | 
|  | 3283 | mEventThread->dump(result); | 
|  | 3284 |  | 
|  | 3285 | /* | 
|  | 3286 | * Dump HWComposer state | 
|  | 3287 | */ | 
|  | 3288 | colorizer.bold(result); | 
|  | 3289 | result.append("h/w composer state:\n"); | 
|  | 3290 | colorizer.reset(result); | 
|  | 3291 | result.appendFormat("  h/w composer %s and %s\n", | 
|  | 3292 | hwc.initCheck()==NO_ERROR ? "present" : "not present", | 
|  | 3293 | (mDebugDisableHWC || mDebugRegion || mDaltonize | 
|  | 3294 | || mHasColorMatrix) ? "disabled" : "enabled"); | 
|  | 3295 | hwc.dump(result); | 
|  | 3296 |  | 
|  | 3297 | /* | 
|  | 3298 | * Dump gralloc state | 
|  | 3299 | */ | 
|  | 3300 | const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get()); | 
|  | 3301 | alloc.dump(result); | 
|  | 3302 | } | 
|  | 3303 |  | 
|  | 3304 | const Vector< sp<Layer> >& | 
|  | 3305 | SurfaceFlinger::getLayerSortedByZForHwcDisplay(int id) { | 
|  | 3306 | // Note: mStateLock is held here | 
|  | 3307 | wp<IBinder> dpy; | 
|  | 3308 | for (size_t i=0 ; i<mDisplays.size() ; i++) { | 
|  | 3309 | if (mDisplays.valueAt(i)->getHwcDisplayId() == id) { | 
|  | 3310 | dpy = mDisplays.keyAt(i); | 
|  | 3311 | break; | 
|  | 3312 | } | 
|  | 3313 | } | 
|  | 3314 | if (dpy == NULL) { | 
|  | 3315 | ALOGE("getLayerSortedByZForHwcDisplay: invalid hwc display id %d", id); | 
|  | 3316 | // Just use the primary display so we have something to return | 
|  | 3317 | dpy = getBuiltInDisplay(DisplayDevice::DISPLAY_PRIMARY); | 
|  | 3318 | } | 
|  | 3319 | return getDisplayDevice(dpy)->getVisibleLayersSortedByZ(); | 
|  | 3320 | } | 
|  | 3321 |  | 
|  | 3322 | bool SurfaceFlinger::startDdmConnection() | 
|  | 3323 | { | 
|  | 3324 | void* libddmconnection_dso = | 
|  | 3325 | dlopen("libsurfaceflinger_ddmconnection.so", RTLD_NOW); | 
|  | 3326 | if (!libddmconnection_dso) { | 
|  | 3327 | return false; | 
|  | 3328 | } | 
|  | 3329 | void (*DdmConnection_start)(const char* name); | 
|  | 3330 | DdmConnection_start = | 
|  | 3331 | (decltype(DdmConnection_start))dlsym(libddmconnection_dso, "DdmConnection_start"); | 
|  | 3332 | if (!DdmConnection_start) { | 
|  | 3333 | dlclose(libddmconnection_dso); | 
|  | 3334 | return false; | 
|  | 3335 | } | 
|  | 3336 | (*DdmConnection_start)(getServiceName()); | 
|  | 3337 | return true; | 
|  | 3338 | } | 
|  | 3339 |  | 
|  | 3340 | status_t SurfaceFlinger::onTransact( | 
|  | 3341 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) | 
|  | 3342 | { | 
|  | 3343 | switch (code) { | 
|  | 3344 | case CREATE_CONNECTION: | 
|  | 3345 | case CREATE_DISPLAY: | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 3346 | case BOOT_FINISHED: | 
|  | 3347 | case CLEAR_ANIMATION_FRAME_STATS: | 
|  | 3348 | case GET_ANIMATION_FRAME_STATS: | 
|  | 3349 | case SET_POWER_MODE: | 
|  | 3350 | case GET_HDR_CAPABILITIES: | 
|  | 3351 | { | 
|  | 3352 | // codes that require permission check | 
|  | 3353 | IPCThreadState* ipc = IPCThreadState::self(); | 
|  | 3354 | const int pid = ipc->getCallingPid(); | 
|  | 3355 | const int uid = ipc->getCallingUid(); | 
|  | 3356 | if ((uid != AID_GRAPHICS && uid != AID_SYSTEM) && | 
|  | 3357 | !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) { | 
|  | 3358 | ALOGE("Permission Denial: " | 
|  | 3359 | "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid); | 
|  | 3360 | return PERMISSION_DENIED; | 
|  | 3361 | } | 
|  | 3362 | break; | 
|  | 3363 | } | 
| Robert Carr | 1db73f6 | 2016-12-21 12:58:51 -0800 | [diff] [blame] | 3364 | /* | 
|  | 3365 | * Calling setTransactionState is safe, because you need to have been | 
|  | 3366 | * granted a reference to Client* and Handle* to do anything with it. | 
|  | 3367 | * | 
|  | 3368 | * Creating a scoped connection is safe, as per discussion in ISurfaceComposer.h | 
|  | 3369 | */ | 
|  | 3370 | case SET_TRANSACTION_STATE: | 
|  | 3371 | case CREATE_SCOPED_CONNECTION: | 
|  | 3372 | { | 
|  | 3373 | break; | 
|  | 3374 | } | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 3375 | case CAPTURE_SCREEN: | 
|  | 3376 | { | 
|  | 3377 | // codes that require permission check | 
|  | 3378 | IPCThreadState* ipc = IPCThreadState::self(); | 
|  | 3379 | const int pid = ipc->getCallingPid(); | 
|  | 3380 | const int uid = ipc->getCallingUid(); | 
|  | 3381 | if ((uid != AID_GRAPHICS) && | 
|  | 3382 | !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) { | 
|  | 3383 | ALOGE("Permission Denial: " | 
|  | 3384 | "can't read framebuffer pid=%d, uid=%d", pid, uid); | 
|  | 3385 | return PERMISSION_DENIED; | 
|  | 3386 | } | 
|  | 3387 | break; | 
|  | 3388 | } | 
|  | 3389 | } | 
|  | 3390 |  | 
|  | 3391 | status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags); | 
|  | 3392 | if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) { | 
|  | 3393 | CHECK_INTERFACE(ISurfaceComposer, data, reply); | 
|  | 3394 | if (CC_UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) { | 
|  | 3395 | IPCThreadState* ipc = IPCThreadState::self(); | 
|  | 3396 | const int pid = ipc->getCallingPid(); | 
|  | 3397 | const int uid = ipc->getCallingUid(); | 
|  | 3398 | ALOGE("Permission Denial: " | 
|  | 3399 | "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid); | 
|  | 3400 | return PERMISSION_DENIED; | 
|  | 3401 | } | 
|  | 3402 | int n; | 
|  | 3403 | switch (code) { | 
|  | 3404 | case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE | 
|  | 3405 | case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE | 
|  | 3406 | return NO_ERROR; | 
|  | 3407 | case 1002:  // SHOW_UPDATES | 
|  | 3408 | n = data.readInt32(); | 
|  | 3409 | mDebugRegion = n ? n : (mDebugRegion ? 0 : 1); | 
|  | 3410 | invalidateHwcGeometry(); | 
|  | 3411 | repaintEverything(); | 
|  | 3412 | return NO_ERROR; | 
|  | 3413 | case 1004:{ // repaint everything | 
|  | 3414 | repaintEverything(); | 
|  | 3415 | return NO_ERROR; | 
|  | 3416 | } | 
|  | 3417 | case 1005:{ // force transaction | 
|  | 3418 | setTransactionFlags( | 
|  | 3419 | eTransactionNeeded| | 
|  | 3420 | eDisplayTransactionNeeded| | 
|  | 3421 | eTraversalNeeded); | 
|  | 3422 | return NO_ERROR; | 
|  | 3423 | } | 
|  | 3424 | case 1006:{ // send empty update | 
|  | 3425 | signalRefresh(); | 
|  | 3426 | return NO_ERROR; | 
|  | 3427 | } | 
|  | 3428 | case 1008:  // toggle use of hw composer | 
|  | 3429 | n = data.readInt32(); | 
|  | 3430 | mDebugDisableHWC = n ? 1 : 0; | 
|  | 3431 | invalidateHwcGeometry(); | 
|  | 3432 | repaintEverything(); | 
|  | 3433 | return NO_ERROR; | 
|  | 3434 | case 1009:  // toggle use of transform hint | 
|  | 3435 | n = data.readInt32(); | 
|  | 3436 | mDebugDisableTransformHint = n ? 1 : 0; | 
|  | 3437 | invalidateHwcGeometry(); | 
|  | 3438 | repaintEverything(); | 
|  | 3439 | return NO_ERROR; | 
|  | 3440 | case 1010:  // interrogate. | 
|  | 3441 | reply->writeInt32(0); | 
|  | 3442 | reply->writeInt32(0); | 
|  | 3443 | reply->writeInt32(mDebugRegion); | 
|  | 3444 | reply->writeInt32(0); | 
|  | 3445 | reply->writeInt32(mDebugDisableHWC); | 
|  | 3446 | return NO_ERROR; | 
|  | 3447 | case 1013: { | 
|  | 3448 | Mutex::Autolock _l(mStateLock); | 
|  | 3449 | sp<const DisplayDevice> hw(getDefaultDisplayDevice()); | 
|  | 3450 | reply->writeInt32(hw->getPageFlipCount()); | 
|  | 3451 | return NO_ERROR; | 
|  | 3452 | } | 
|  | 3453 | case 1014: { | 
|  | 3454 | // daltonize | 
|  | 3455 | n = data.readInt32(); | 
|  | 3456 | switch (n % 10) { | 
|  | 3457 | case 1: | 
|  | 3458 | mDaltonizer.setType(ColorBlindnessType::Protanomaly); | 
|  | 3459 | break; | 
|  | 3460 | case 2: | 
|  | 3461 | mDaltonizer.setType(ColorBlindnessType::Deuteranomaly); | 
|  | 3462 | break; | 
|  | 3463 | case 3: | 
|  | 3464 | mDaltonizer.setType(ColorBlindnessType::Tritanomaly); | 
|  | 3465 | break; | 
|  | 3466 | } | 
|  | 3467 | if (n >= 10) { | 
|  | 3468 | mDaltonizer.setMode(ColorBlindnessMode::Correction); | 
|  | 3469 | } else { | 
|  | 3470 | mDaltonizer.setMode(ColorBlindnessMode::Simulation); | 
|  | 3471 | } | 
|  | 3472 | mDaltonize = n > 0; | 
|  | 3473 | invalidateHwcGeometry(); | 
|  | 3474 | repaintEverything(); | 
|  | 3475 | return NO_ERROR; | 
|  | 3476 | } | 
|  | 3477 | case 1015: { | 
|  | 3478 | // apply a color matrix | 
|  | 3479 | n = data.readInt32(); | 
|  | 3480 | mHasColorMatrix = n ? 1 : 0; | 
|  | 3481 | if (n) { | 
|  | 3482 | // color matrix is sent as mat3 matrix followed by vec3 | 
|  | 3483 | // offset, then packed into a mat4 where the last row is | 
|  | 3484 | // the offset and extra values are 0 | 
|  | 3485 | for (size_t i = 0 ; i < 4; i++) { | 
|  | 3486 | for (size_t j = 0; j < 4; j++) { | 
|  | 3487 | mColorMatrix[i][j] = data.readFloat(); | 
|  | 3488 | } | 
|  | 3489 | } | 
|  | 3490 | } else { | 
|  | 3491 | mColorMatrix = mat4(); | 
|  | 3492 | } | 
|  | 3493 | invalidateHwcGeometry(); | 
|  | 3494 | repaintEverything(); | 
|  | 3495 | return NO_ERROR; | 
|  | 3496 | } | 
|  | 3497 | // This is an experimental interface | 
|  | 3498 | // Needs to be shifted to proper binder interface when we productize | 
|  | 3499 | case 1016: { | 
|  | 3500 | n = data.readInt32(); | 
|  | 3501 | mPrimaryDispSync.setRefreshSkipCount(n); | 
|  | 3502 | return NO_ERROR; | 
|  | 3503 | } | 
|  | 3504 | case 1017: { | 
|  | 3505 | n = data.readInt32(); | 
|  | 3506 | mForceFullDamage = static_cast<bool>(n); | 
|  | 3507 | return NO_ERROR; | 
|  | 3508 | } | 
|  | 3509 | case 1018: { // Modify Choreographer's phase offset | 
|  | 3510 | n = data.readInt32(); | 
|  | 3511 | mEventThread->setPhaseOffset(static_cast<nsecs_t>(n)); | 
|  | 3512 | return NO_ERROR; | 
|  | 3513 | } | 
|  | 3514 | case 1019: { // Modify SurfaceFlinger's phase offset | 
|  | 3515 | n = data.readInt32(); | 
|  | 3516 | mSFEventThread->setPhaseOffset(static_cast<nsecs_t>(n)); | 
|  | 3517 | return NO_ERROR; | 
|  | 3518 | } | 
|  | 3519 | case 1020: { // Layer updates interceptor | 
|  | 3520 | n = data.readInt32(); | 
|  | 3521 | if (n) { | 
|  | 3522 | ALOGV("Interceptor enabled"); | 
|  | 3523 | mInterceptor.enable(mDrawingState.layersSortedByZ, mDrawingState.displays); | 
|  | 3524 | } | 
|  | 3525 | else{ | 
|  | 3526 | ALOGV("Interceptor disabled"); | 
|  | 3527 | mInterceptor.disable(); | 
|  | 3528 | } | 
|  | 3529 | return NO_ERROR; | 
|  | 3530 | } | 
|  | 3531 | case 1021: { // Disable HWC virtual displays | 
|  | 3532 | n = data.readInt32(); | 
|  | 3533 | mUseHwcVirtualDisplays = !n; | 
|  | 3534 | return NO_ERROR; | 
|  | 3535 | } | 
|  | 3536 | } | 
|  | 3537 | } | 
|  | 3538 | return err; | 
|  | 3539 | } | 
|  | 3540 |  | 
|  | 3541 | void SurfaceFlinger::repaintEverything() { | 
|  | 3542 | android_atomic_or(1, &mRepaintEverything); | 
|  | 3543 | signalTransaction(); | 
|  | 3544 | } | 
|  | 3545 |  | 
|  | 3546 | // --------------------------------------------------------------------------- | 
|  | 3547 | // Capture screen into an IGraphiBufferProducer | 
|  | 3548 | // --------------------------------------------------------------------------- | 
|  | 3549 |  | 
|  | 3550 | /* The code below is here to handle b/8734824 | 
|  | 3551 | * | 
|  | 3552 | * We create a IGraphicBufferProducer wrapper that forwards all calls | 
|  | 3553 | * from the surfaceflinger thread to the calling binder thread, where they | 
|  | 3554 | * are executed. This allows the calling thread in the calling process to be | 
|  | 3555 | * reused and not depend on having "enough" binder threads to handle the | 
|  | 3556 | * requests. | 
|  | 3557 | */ | 
|  | 3558 | class GraphicProducerWrapper : public BBinder, public MessageHandler { | 
|  | 3559 | /* Parts of GraphicProducerWrapper are run on two different threads, | 
|  | 3560 | * communicating by sending messages via Looper but also by shared member | 
|  | 3561 | * data. Coherence maintenance is subtle and in places implicit (ugh). | 
|  | 3562 | * | 
|  | 3563 | * Don't rely on Looper's sendMessage/handleMessage providing | 
|  | 3564 | * release/acquire semantics for any data not actually in the Message. | 
|  | 3565 | * Data going from surfaceflinger to binder threads needs to be | 
|  | 3566 | * synchronized explicitly. | 
|  | 3567 | * | 
|  | 3568 | * Barrier open/wait do provide release/acquire semantics. This provides | 
|  | 3569 | * implicit synchronization for data coming back from binder to | 
|  | 3570 | * surfaceflinger threads. | 
|  | 3571 | */ | 
|  | 3572 |  | 
|  | 3573 | sp<IGraphicBufferProducer> impl; | 
|  | 3574 | sp<Looper> looper; | 
|  | 3575 | status_t result; | 
|  | 3576 | bool exitPending; | 
|  | 3577 | bool exitRequested; | 
|  | 3578 | Barrier barrier; | 
|  | 3579 | uint32_t code; | 
|  | 3580 | Parcel const* data; | 
|  | 3581 | Parcel* reply; | 
|  | 3582 |  | 
|  | 3583 | enum { | 
|  | 3584 | MSG_API_CALL, | 
|  | 3585 | MSG_EXIT | 
|  | 3586 | }; | 
|  | 3587 |  | 
|  | 3588 | /* | 
|  | 3589 | * Called on surfaceflinger thread. This is called by our "fake" | 
|  | 3590 | * BpGraphicBufferProducer. We package the data and reply Parcel and | 
|  | 3591 | * forward them to the binder thread. | 
|  | 3592 | */ | 
|  | 3593 | virtual status_t transact(uint32_t code, | 
|  | 3594 | const Parcel& data, Parcel* reply, uint32_t /* flags */) { | 
|  | 3595 | this->code = code; | 
|  | 3596 | this->data = &data; | 
|  | 3597 | this->reply = reply; | 
|  | 3598 | if (exitPending) { | 
|  | 3599 | // if we've exited, we run the message synchronously right here. | 
|  | 3600 | // note (JH): as far as I can tell from looking at the code, this | 
|  | 3601 | // never actually happens. if it does, i'm not sure if it happens | 
|  | 3602 | // on the surfaceflinger or binder thread. | 
|  | 3603 | handleMessage(Message(MSG_API_CALL)); | 
|  | 3604 | } else { | 
|  | 3605 | barrier.close(); | 
|  | 3606 | // Prevent stores to this->{code, data, reply} from being | 
|  | 3607 | // reordered later than the construction of Message. | 
|  | 3608 | atomic_thread_fence(memory_order_release); | 
|  | 3609 | looper->sendMessage(this, Message(MSG_API_CALL)); | 
|  | 3610 | barrier.wait(); | 
|  | 3611 | } | 
|  | 3612 | return result; | 
|  | 3613 | } | 
|  | 3614 |  | 
|  | 3615 | /* | 
|  | 3616 | * here we run on the binder thread. All we've got to do is | 
|  | 3617 | * call the real BpGraphicBufferProducer. | 
|  | 3618 | */ | 
|  | 3619 | virtual void handleMessage(const Message& message) { | 
|  | 3620 | int what = message.what; | 
|  | 3621 | // Prevent reads below from happening before the read from Message | 
|  | 3622 | atomic_thread_fence(memory_order_acquire); | 
|  | 3623 | if (what == MSG_API_CALL) { | 
|  | 3624 | result = IInterface::asBinder(impl)->transact(code, data[0], reply); | 
|  | 3625 | barrier.open(); | 
|  | 3626 | } else if (what == MSG_EXIT) { | 
|  | 3627 | exitRequested = true; | 
|  | 3628 | } | 
|  | 3629 | } | 
|  | 3630 |  | 
|  | 3631 | public: | 
|  | 3632 | explicit GraphicProducerWrapper(const sp<IGraphicBufferProducer>& impl) | 
|  | 3633 | :   impl(impl), | 
|  | 3634 | looper(new Looper(true)), | 
|  | 3635 | result(NO_ERROR), | 
|  | 3636 | exitPending(false), | 
|  | 3637 | exitRequested(false), | 
|  | 3638 | code(0), | 
|  | 3639 | data(NULL), | 
|  | 3640 | reply(NULL) | 
|  | 3641 | {} | 
|  | 3642 |  | 
|  | 3643 | // Binder thread | 
|  | 3644 | status_t waitForResponse() { | 
|  | 3645 | do { | 
|  | 3646 | looper->pollOnce(-1); | 
|  | 3647 | } while (!exitRequested); | 
|  | 3648 | return result; | 
|  | 3649 | } | 
|  | 3650 |  | 
|  | 3651 | // Client thread | 
|  | 3652 | void exit(status_t result) { | 
|  | 3653 | this->result = result; | 
|  | 3654 | exitPending = true; | 
|  | 3655 | // Ensure this->result is visible to the binder thread before it | 
|  | 3656 | // handles the message. | 
|  | 3657 | atomic_thread_fence(memory_order_release); | 
|  | 3658 | looper->sendMessage(this, Message(MSG_EXIT)); | 
|  | 3659 | } | 
|  | 3660 | }; | 
|  | 3661 |  | 
|  | 3662 |  | 
|  | 3663 | status_t SurfaceFlinger::captureScreen(const sp<IBinder>& display, | 
|  | 3664 | const sp<IGraphicBufferProducer>& producer, | 
|  | 3665 | Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight, | 
| Robert Carr | ae06083 | 2016-11-28 10:51:00 -0800 | [diff] [blame] | 3666 | int32_t minLayerZ, int32_t maxLayerZ, | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 3667 | bool useIdentityTransform, ISurfaceComposer::Rotation rotation) { | 
|  | 3668 |  | 
|  | 3669 | if (CC_UNLIKELY(display == 0)) | 
|  | 3670 | return BAD_VALUE; | 
|  | 3671 |  | 
|  | 3672 | if (CC_UNLIKELY(producer == 0)) | 
|  | 3673 | return BAD_VALUE; | 
|  | 3674 |  | 
|  | 3675 | // if we have secure windows on this display, never allow the screen capture | 
|  | 3676 | // unless the producer interface is local (i.e.: we can take a screenshot for | 
|  | 3677 | // ourselves). | 
|  | 3678 | bool isLocalScreenshot = IInterface::asBinder(producer)->localBinder(); | 
|  | 3679 |  | 
|  | 3680 | // Convert to surfaceflinger's internal rotation type. | 
|  | 3681 | Transform::orientation_flags rotationFlags; | 
|  | 3682 | switch (rotation) { | 
|  | 3683 | case ISurfaceComposer::eRotateNone: | 
|  | 3684 | rotationFlags = Transform::ROT_0; | 
|  | 3685 | break; | 
|  | 3686 | case ISurfaceComposer::eRotate90: | 
|  | 3687 | rotationFlags = Transform::ROT_90; | 
|  | 3688 | break; | 
|  | 3689 | case ISurfaceComposer::eRotate180: | 
|  | 3690 | rotationFlags = Transform::ROT_180; | 
|  | 3691 | break; | 
|  | 3692 | case ISurfaceComposer::eRotate270: | 
|  | 3693 | rotationFlags = Transform::ROT_270; | 
|  | 3694 | break; | 
|  | 3695 | default: | 
|  | 3696 | rotationFlags = Transform::ROT_0; | 
|  | 3697 | ALOGE("Invalid rotation passed to captureScreen(): %d\n", rotation); | 
|  | 3698 | break; | 
|  | 3699 | } | 
|  | 3700 |  | 
|  | 3701 | class MessageCaptureScreen : public MessageBase { | 
|  | 3702 | SurfaceFlinger* flinger; | 
|  | 3703 | sp<IBinder> display; | 
|  | 3704 | sp<IGraphicBufferProducer> producer; | 
|  | 3705 | Rect sourceCrop; | 
|  | 3706 | uint32_t reqWidth, reqHeight; | 
| Robert Carr | ae06083 | 2016-11-28 10:51:00 -0800 | [diff] [blame] | 3707 | int32_t minLayerZ,maxLayerZ; | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 3708 | bool useIdentityTransform; | 
|  | 3709 | Transform::orientation_flags rotation; | 
|  | 3710 | status_t result; | 
|  | 3711 | bool isLocalScreenshot; | 
|  | 3712 | public: | 
|  | 3713 | MessageCaptureScreen(SurfaceFlinger* flinger, | 
|  | 3714 | const sp<IBinder>& display, | 
|  | 3715 | const sp<IGraphicBufferProducer>& producer, | 
|  | 3716 | Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight, | 
| Robert Carr | ae06083 | 2016-11-28 10:51:00 -0800 | [diff] [blame] | 3717 | int32_t minLayerZ, int32_t maxLayerZ, | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 3718 | bool useIdentityTransform, | 
|  | 3719 | Transform::orientation_flags rotation, | 
|  | 3720 | bool isLocalScreenshot) | 
|  | 3721 | : flinger(flinger), display(display), producer(producer), | 
|  | 3722 | sourceCrop(sourceCrop), reqWidth(reqWidth), reqHeight(reqHeight), | 
|  | 3723 | minLayerZ(minLayerZ), maxLayerZ(maxLayerZ), | 
|  | 3724 | useIdentityTransform(useIdentityTransform), | 
|  | 3725 | rotation(rotation), result(PERMISSION_DENIED), | 
|  | 3726 | isLocalScreenshot(isLocalScreenshot) | 
|  | 3727 | { | 
|  | 3728 | } | 
|  | 3729 | status_t getResult() const { | 
|  | 3730 | return result; | 
|  | 3731 | } | 
|  | 3732 | virtual bool handler() { | 
|  | 3733 | Mutex::Autolock _l(flinger->mStateLock); | 
|  | 3734 | sp<const DisplayDevice> hw(flinger->getDisplayDevice(display)); | 
|  | 3735 | result = flinger->captureScreenImplLocked(hw, producer, | 
|  | 3736 | sourceCrop, reqWidth, reqHeight, minLayerZ, maxLayerZ, | 
|  | 3737 | useIdentityTransform, rotation, isLocalScreenshot); | 
|  | 3738 | static_cast<GraphicProducerWrapper*>(IInterface::asBinder(producer).get())->exit(result); | 
|  | 3739 | return true; | 
|  | 3740 | } | 
|  | 3741 | }; | 
|  | 3742 |  | 
|  | 3743 | // this creates a "fake" BBinder which will serve as a "fake" remote | 
|  | 3744 | // binder to receive the marshaled calls and forward them to the | 
|  | 3745 | // real remote (a BpGraphicBufferProducer) | 
|  | 3746 | sp<GraphicProducerWrapper> wrapper = new GraphicProducerWrapper(producer); | 
|  | 3747 |  | 
|  | 3748 | // the asInterface() call below creates our "fake" BpGraphicBufferProducer | 
|  | 3749 | // which does the marshaling work forwards to our "fake remote" above. | 
|  | 3750 | sp<MessageBase> msg = new MessageCaptureScreen(this, | 
|  | 3751 | display, IGraphicBufferProducer::asInterface( wrapper ), | 
|  | 3752 | sourceCrop, reqWidth, reqHeight, minLayerZ, maxLayerZ, | 
|  | 3753 | useIdentityTransform, rotationFlags, isLocalScreenshot); | 
|  | 3754 |  | 
|  | 3755 | status_t res = postMessageAsync(msg); | 
|  | 3756 | if (res == NO_ERROR) { | 
|  | 3757 | res = wrapper->waitForResponse(); | 
|  | 3758 | } | 
|  | 3759 | return res; | 
|  | 3760 | } | 
|  | 3761 |  | 
|  | 3762 |  | 
|  | 3763 | void SurfaceFlinger::renderScreenImplLocked( | 
|  | 3764 | const sp<const DisplayDevice>& hw, | 
|  | 3765 | Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight, | 
| Robert Carr | ae06083 | 2016-11-28 10:51:00 -0800 | [diff] [blame] | 3766 | int32_t minLayerZ, int32_t maxLayerZ, | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 3767 | bool yswap, bool useIdentityTransform, Transform::orientation_flags rotation) | 
|  | 3768 | { | 
|  | 3769 | ATRACE_CALL(); | 
|  | 3770 | RenderEngine& engine(getRenderEngine()); | 
|  | 3771 |  | 
|  | 3772 | // get screen geometry | 
|  | 3773 | const int32_t hw_w = hw->getWidth(); | 
|  | 3774 | const int32_t hw_h = hw->getHeight(); | 
|  | 3775 | const bool filtering = static_cast<int32_t>(reqWidth) != hw_w || | 
|  | 3776 | static_cast<int32_t>(reqHeight) != hw_h; | 
|  | 3777 |  | 
|  | 3778 | // if a default or invalid sourceCrop is passed in, set reasonable values | 
|  | 3779 | if (sourceCrop.width() == 0 || sourceCrop.height() == 0 || | 
|  | 3780 | !sourceCrop.isValid()) { | 
|  | 3781 | sourceCrop.setLeftTop(Point(0, 0)); | 
|  | 3782 | sourceCrop.setRightBottom(Point(hw_w, hw_h)); | 
|  | 3783 | } | 
|  | 3784 |  | 
|  | 3785 | // ensure that sourceCrop is inside screen | 
|  | 3786 | if (sourceCrop.left < 0) { | 
|  | 3787 | ALOGE("Invalid crop rect: l = %d (< 0)", sourceCrop.left); | 
|  | 3788 | } | 
|  | 3789 | if (sourceCrop.right > hw_w) { | 
|  | 3790 | ALOGE("Invalid crop rect: r = %d (> %d)", sourceCrop.right, hw_w); | 
|  | 3791 | } | 
|  | 3792 | if (sourceCrop.top < 0) { | 
|  | 3793 | ALOGE("Invalid crop rect: t = %d (< 0)", sourceCrop.top); | 
|  | 3794 | } | 
|  | 3795 | if (sourceCrop.bottom > hw_h) { | 
|  | 3796 | ALOGE("Invalid crop rect: b = %d (> %d)", sourceCrop.bottom, hw_h); | 
|  | 3797 | } | 
|  | 3798 |  | 
|  | 3799 | // make sure to clear all GL error flags | 
|  | 3800 | engine.checkErrors(); | 
|  | 3801 |  | 
|  | 3802 | // set-up our viewport | 
|  | 3803 | engine.setViewportAndProjection( | 
|  | 3804 | reqWidth, reqHeight, sourceCrop, hw_h, yswap, rotation); | 
|  | 3805 | engine.disableTexturing(); | 
|  | 3806 |  | 
|  | 3807 | // redraw the screen entirely... | 
|  | 3808 | engine.clearWithColor(0, 0, 0, 1); | 
|  | 3809 |  | 
| Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 3810 | // We loop through the first level of layers without traversing, | 
|  | 3811 | // as we need to interpret min/max layer Z in the top level Z space. | 
|  | 3812 | for (const auto& layer : mDrawingState.layersSortedByZ) { | 
|  | 3813 | if (layer->getLayerStack() != hw->getLayerStack()) { | 
|  | 3814 | continue; | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 3815 | } | 
| Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 3816 | const Layer::State& state(layer->getDrawingState()); | 
|  | 3817 | if (state.z < minLayerZ || state.z > maxLayerZ) { | 
|  | 3818 | continue; | 
|  | 3819 | } | 
|  | 3820 | layer->traverseInZOrder([&](Layer* layer) { | 
|  | 3821 | if (!layer->isVisible()) { | 
|  | 3822 | return; | 
|  | 3823 | } | 
|  | 3824 | if (filtering) layer->setFiltering(true); | 
|  | 3825 | layer->draw(hw, useIdentityTransform); | 
|  | 3826 | if (filtering) layer->setFiltering(false); | 
|  | 3827 | }); | 
|  | 3828 | } | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 3829 |  | 
|  | 3830 | // compositionComplete is needed for older driver | 
|  | 3831 | hw->compositionComplete(); | 
|  | 3832 | hw->setViewportAndProjection(); | 
|  | 3833 | } | 
|  | 3834 |  | 
|  | 3835 |  | 
|  | 3836 | status_t SurfaceFlinger::captureScreenImplLocked( | 
|  | 3837 | const sp<const DisplayDevice>& hw, | 
|  | 3838 | const sp<IGraphicBufferProducer>& producer, | 
|  | 3839 | Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight, | 
| Robert Carr | ae06083 | 2016-11-28 10:51:00 -0800 | [diff] [blame] | 3840 | int32_t minLayerZ, int32_t maxLayerZ, | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 3841 | bool useIdentityTransform, Transform::orientation_flags rotation, | 
|  | 3842 | bool isLocalScreenshot) | 
|  | 3843 | { | 
|  | 3844 | ATRACE_CALL(); | 
|  | 3845 |  | 
|  | 3846 | // get screen geometry | 
|  | 3847 | uint32_t hw_w = hw->getWidth(); | 
|  | 3848 | uint32_t hw_h = hw->getHeight(); | 
|  | 3849 |  | 
|  | 3850 | if (rotation & Transform::ROT_90) { | 
|  | 3851 | std::swap(hw_w, hw_h); | 
|  | 3852 | } | 
|  | 3853 |  | 
|  | 3854 | if ((reqWidth > hw_w) || (reqHeight > hw_h)) { | 
|  | 3855 | ALOGE("size mismatch (%d, %d) > (%d, %d)", | 
|  | 3856 | reqWidth, reqHeight, hw_w, hw_h); | 
|  | 3857 | return BAD_VALUE; | 
|  | 3858 | } | 
|  | 3859 |  | 
|  | 3860 | reqWidth  = (!reqWidth)  ? hw_w : reqWidth; | 
|  | 3861 | reqHeight = (!reqHeight) ? hw_h : reqHeight; | 
|  | 3862 |  | 
|  | 3863 | bool secureLayerIsVisible = false; | 
| Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 3864 | for (const auto& layer : mDrawingState.layersSortedByZ) { | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 3865 | const Layer::State& state(layer->getDrawingState()); | 
| Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 3866 | if ((layer->getLayerStack() != hw->getLayerStack()) || | 
|  | 3867 | (state.z < minLayerZ || state.z > maxLayerZ)) { | 
|  | 3868 | continue; | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 3869 | } | 
| Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 3870 | layer->traverseInZOrder([&](Layer *layer) { | 
|  | 3871 | secureLayerIsVisible = secureLayerIsVisible || (layer->isVisible() && | 
|  | 3872 | layer->isSecure()); | 
|  | 3873 | }); | 
|  | 3874 | } | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 3875 |  | 
|  | 3876 | if (!isLocalScreenshot && secureLayerIsVisible) { | 
|  | 3877 | ALOGW("FB is protected: PERMISSION_DENIED"); | 
|  | 3878 | return PERMISSION_DENIED; | 
|  | 3879 | } | 
|  | 3880 |  | 
|  | 3881 | // create a surface (because we're a producer, and we need to | 
|  | 3882 | // dequeue/queue a buffer) | 
|  | 3883 | sp<Surface> sur = new Surface(producer, false); | 
|  | 3884 | ANativeWindow* window = sur.get(); | 
|  | 3885 |  | 
|  | 3886 | status_t result = native_window_api_connect(window, NATIVE_WINDOW_API_EGL); | 
|  | 3887 | if (result == NO_ERROR) { | 
|  | 3888 | uint32_t usage = GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN | | 
|  | 3889 | GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE; | 
|  | 3890 |  | 
|  | 3891 | int err = 0; | 
|  | 3892 | err = native_window_set_buffers_dimensions(window, reqWidth, reqHeight); | 
|  | 3893 | err |= native_window_set_scaling_mode(window, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW); | 
|  | 3894 | err |= native_window_set_buffers_format(window, HAL_PIXEL_FORMAT_RGBA_8888); | 
|  | 3895 | err |= native_window_set_usage(window, usage); | 
|  | 3896 |  | 
|  | 3897 | if (err == NO_ERROR) { | 
|  | 3898 | ANativeWindowBuffer* buffer; | 
|  | 3899 | /* TODO: Once we have the sync framework everywhere this can use | 
|  | 3900 | * server-side waits on the fence that dequeueBuffer returns. | 
|  | 3901 | */ | 
|  | 3902 | result = native_window_dequeue_buffer_and_wait(window,  &buffer); | 
|  | 3903 | if (result == NO_ERROR) { | 
|  | 3904 | int syncFd = -1; | 
|  | 3905 | // create an EGLImage from the buffer so we can later | 
|  | 3906 | // turn it into a texture | 
|  | 3907 | EGLImageKHR image = eglCreateImageKHR(mEGLDisplay, EGL_NO_CONTEXT, | 
|  | 3908 | EGL_NATIVE_BUFFER_ANDROID, buffer, NULL); | 
|  | 3909 | if (image != EGL_NO_IMAGE_KHR) { | 
|  | 3910 | // this binds the given EGLImage as a framebuffer for the | 
|  | 3911 | // duration of this scope. | 
|  | 3912 | RenderEngine::BindImageAsFramebuffer imageBond(getRenderEngine(), image); | 
|  | 3913 | if (imageBond.getStatus() == NO_ERROR) { | 
|  | 3914 | // this will in fact render into our dequeued buffer | 
|  | 3915 | // via an FBO, which means we didn't have to create | 
|  | 3916 | // an EGLSurface and therefore we're not | 
|  | 3917 | // dependent on the context's EGLConfig. | 
|  | 3918 | renderScreenImplLocked( | 
|  | 3919 | hw, sourceCrop, reqWidth, reqHeight, minLayerZ, maxLayerZ, true, | 
|  | 3920 | useIdentityTransform, rotation); | 
|  | 3921 |  | 
|  | 3922 | // Attempt to create a sync khr object that can produce a sync point. If that | 
|  | 3923 | // isn't available, create a non-dupable sync object in the fallback path and | 
|  | 3924 | // wait on it directly. | 
|  | 3925 | EGLSyncKHR sync; | 
|  | 3926 | if (!DEBUG_SCREENSHOTS) { | 
|  | 3927 | sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_NATIVE_FENCE_ANDROID, NULL); | 
|  | 3928 | // native fence fd will not be populated until flush() is done. | 
|  | 3929 | getRenderEngine().flush(); | 
|  | 3930 | } else { | 
|  | 3931 | sync = EGL_NO_SYNC_KHR; | 
|  | 3932 | } | 
|  | 3933 | if (sync != EGL_NO_SYNC_KHR) { | 
|  | 3934 | // get the sync fd | 
|  | 3935 | syncFd = eglDupNativeFenceFDANDROID(mEGLDisplay, sync); | 
|  | 3936 | if (syncFd == EGL_NO_NATIVE_FENCE_FD_ANDROID) { | 
|  | 3937 | ALOGW("captureScreen: failed to dup sync khr object"); | 
|  | 3938 | syncFd = -1; | 
|  | 3939 | } | 
|  | 3940 | eglDestroySyncKHR(mEGLDisplay, sync); | 
|  | 3941 | } else { | 
|  | 3942 | // fallback path | 
|  | 3943 | sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_FENCE_KHR, NULL); | 
|  | 3944 | if (sync != EGL_NO_SYNC_KHR) { | 
|  | 3945 | EGLint result = eglClientWaitSyncKHR(mEGLDisplay, sync, | 
|  | 3946 | EGL_SYNC_FLUSH_COMMANDS_BIT_KHR, 2000000000 /*2 sec*/); | 
|  | 3947 | EGLint eglErr = eglGetError(); | 
|  | 3948 | if (result == EGL_TIMEOUT_EXPIRED_KHR) { | 
|  | 3949 | ALOGW("captureScreen: fence wait timed out"); | 
|  | 3950 | } else { | 
|  | 3951 | ALOGW_IF(eglErr != EGL_SUCCESS, | 
|  | 3952 | "captureScreen: error waiting on EGL fence: %#x", eglErr); | 
|  | 3953 | } | 
|  | 3954 | eglDestroySyncKHR(mEGLDisplay, sync); | 
|  | 3955 | } else { | 
|  | 3956 | ALOGW("captureScreen: error creating EGL fence: %#x", eglGetError()); | 
|  | 3957 | } | 
|  | 3958 | } | 
|  | 3959 | if (DEBUG_SCREENSHOTS) { | 
|  | 3960 | uint32_t* pixels = new uint32_t[reqWidth*reqHeight]; | 
|  | 3961 | getRenderEngine().readPixels(0, 0, reqWidth, reqHeight, pixels); | 
|  | 3962 | checkScreenshot(reqWidth, reqHeight, reqWidth, pixels, | 
|  | 3963 | hw, minLayerZ, maxLayerZ); | 
|  | 3964 | delete [] pixels; | 
|  | 3965 | } | 
|  | 3966 |  | 
|  | 3967 | } else { | 
|  | 3968 | ALOGE("got GL_FRAMEBUFFER_COMPLETE_OES error while taking screenshot"); | 
|  | 3969 | result = INVALID_OPERATION; | 
|  | 3970 | window->cancelBuffer(window, buffer, syncFd); | 
|  | 3971 | buffer = NULL; | 
|  | 3972 | } | 
|  | 3973 | // destroy our image | 
|  | 3974 | eglDestroyImageKHR(mEGLDisplay, image); | 
|  | 3975 | } else { | 
|  | 3976 | result = BAD_VALUE; | 
|  | 3977 | } | 
|  | 3978 | if (buffer) { | 
|  | 3979 | // queueBuffer takes ownership of syncFd | 
|  | 3980 | result = window->queueBuffer(window, buffer, syncFd); | 
|  | 3981 | } | 
|  | 3982 | } | 
|  | 3983 | } else { | 
|  | 3984 | result = BAD_VALUE; | 
|  | 3985 | } | 
|  | 3986 | native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL); | 
|  | 3987 | } | 
|  | 3988 |  | 
|  | 3989 | return result; | 
|  | 3990 | } | 
|  | 3991 |  | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 3992 | void SurfaceFlinger::checkScreenshot(size_t w, size_t s, size_t h, void const* vaddr, | 
| Robert Carr | ae06083 | 2016-11-28 10:51:00 -0800 | [diff] [blame] | 3993 | const sp<const DisplayDevice>& hw, int32_t minLayerZ, int32_t maxLayerZ) { | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 3994 | if (DEBUG_SCREENSHOTS) { | 
|  | 3995 | for (size_t y=0 ; y<h ; y++) { | 
|  | 3996 | uint32_t const * p = (uint32_t const *)vaddr + y*s; | 
|  | 3997 | for (size_t x=0 ; x<w ; x++) { | 
|  | 3998 | if (p[x] != 0xFF000000) return; | 
|  | 3999 | } | 
|  | 4000 | } | 
|  | 4001 | ALOGE("*** we just took a black screenshot ***\n" | 
|  | 4002 | "requested minz=%d, maxz=%d, layerStack=%d", | 
|  | 4003 | minLayerZ, maxLayerZ, hw->getLayerStack()); | 
| Robert Carr | 2047fae | 2016-11-28 14:09:09 -0800 | [diff] [blame] | 4004 | size_t i = 0; | 
| Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 4005 | for (const auto& layer : mDrawingState.layersSortedByZ) { | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 4006 | const Layer::State& state(layer->getDrawingState()); | 
| Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 4007 | if (layer->getLayerStack() == hw->getLayerStack() && state.z >= minLayerZ && | 
|  | 4008 | state.z <= maxLayerZ) { | 
|  | 4009 | layer->traverseInZOrder([&](Layer* layer) { | 
|  | 4010 | ALOGE("%c index=%zu, name=%s, layerStack=%d, z=%d, visible=%d, flags=%x, alpha=%x", | 
|  | 4011 | layer->isVisible() ? '+' : '-', | 
|  | 4012 | i, layer->getName().string(), layer->getLayerStack(), state.z, | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 4013 | layer->isVisible(), state.flags, state.alpha); | 
| Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 4014 | i++; | 
|  | 4015 | }); | 
|  | 4016 | } | 
|  | 4017 | } | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 4018 | } | 
|  | 4019 | } | 
|  | 4020 |  | 
|  | 4021 | // --------------------------------------------------------------------------- | 
|  | 4022 |  | 
| Robert Carr | 2047fae | 2016-11-28 14:09:09 -0800 | [diff] [blame] | 4023 | void SurfaceFlinger::State::traverseInZOrder(const std::function<void(Layer*)>& consume) const { | 
|  | 4024 | layersSortedByZ.traverseInZOrder(consume); | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 4025 | } | 
|  | 4026 |  | 
| Robert Carr | 2047fae | 2016-11-28 14:09:09 -0800 | [diff] [blame] | 4027 | void SurfaceFlinger::State::traverseInReverseZOrder(const std::function<void(Layer*)>& consume) const { | 
|  | 4028 | layersSortedByZ.traverseInReverseZOrder(consume); | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 4029 | } | 
|  | 4030 |  | 
|  | 4031 | }; // namespace android | 
|  | 4032 |  | 
|  | 4033 |  | 
|  | 4034 | #if defined(__gl_h_) | 
|  | 4035 | #error "don't include gl/gl.h in this file" | 
|  | 4036 | #endif | 
|  | 4037 |  | 
|  | 4038 | #if defined(__gl2_h_) | 
|  | 4039 | #error "don't include gl2/gl2.h in this file" | 
|  | 4040 | #endif |