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