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