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