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