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