SurfaceFlinger.h revision c8251eb7a6ecfdd16b3e4cfbfb442aa4c789c039
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#ifndef ANDROID_SURFACE_FLINGER_H 18#define ANDROID_SURFACE_FLINGER_H 19 20#include <stdint.h> 21#include <sys/types.h> 22 23#include <EGL/egl.h> 24 25/* 26 * NOTE: Make sure this file doesn't include anything from <gl/ > or <gl2/ > 27 */ 28 29#include <cutils/compiler.h> 30 31#include <utils/Atomic.h> 32#include <utils/Errors.h> 33#include <utils/KeyedVector.h> 34#include <utils/RefBase.h> 35#include <utils/SortedVector.h> 36#include <utils/threads.h> 37 38#include <binder/IMemory.h> 39 40#include <ui/PixelFormat.h> 41#include <ui/mat4.h> 42 43#include <gui/ISurfaceComposer.h> 44#include <gui/ISurfaceComposerClient.h> 45#include <gui/OccupancyTracker.h> 46 47#include <hardware/hwcomposer_defs.h> 48 49#include <system/graphics.h> 50 51#include <private/gui/LayerState.h> 52 53#include "Barrier.h" 54#include "DisplayDevice.h" 55#include "DispSync.h" 56#include "FrameTracker.h" 57#include "MessageQueue.h" 58#include "SurfaceInterceptor.h" 59 60#include "DisplayHardware/HWComposer.h" 61#include "Effects/Daltonizer.h" 62 63#include <map> 64#include <string> 65 66namespace android { 67 68// --------------------------------------------------------------------------- 69 70class Client; 71class DisplayEventConnection; 72class EventThread; 73class IGraphicBufferAlloc; 74class Layer; 75class LayerDim; 76class Surface; 77class RenderEngine; 78class EventControlThread; 79class VSyncSource; 80class InjectVSyncSource; 81 82// --------------------------------------------------------------------------- 83 84enum { 85 eTransactionNeeded = 0x01, 86 eTraversalNeeded = 0x02, 87 eDisplayTransactionNeeded = 0x04, 88 eTransactionMask = 0x07 89}; 90 91class SurfaceFlinger : public BnSurfaceComposer, 92 private IBinder::DeathRecipient, 93 private HWComposer::EventHandler 94{ 95public: 96 static char const* getServiceName() ANDROID_API { 97 return "SurfaceFlinger"; 98 } 99 100 SurfaceFlinger() ANDROID_API; 101 102 // must be called before clients can connect 103 void init() ANDROID_API; 104 105 // starts SurfaceFlinger main loop in the current thread 106 void run() ANDROID_API; 107 108 enum { 109 EVENT_VSYNC = HWC_EVENT_VSYNC 110 }; 111 112 // post an asynchronous message to the main thread 113 status_t postMessageAsync(const sp<MessageBase>& msg, nsecs_t reltime = 0, uint32_t flags = 0); 114 115 // post a synchronous message to the main thread 116 status_t postMessageSync(const sp<MessageBase>& msg, nsecs_t reltime = 0, uint32_t flags = 0); 117 118 // force full composition on all displays 119 void repaintEverything(); 120 121 // returns the default Display 122 sp<const DisplayDevice> getDefaultDisplayDevice() const { 123 return getDisplayDevice(mBuiltinDisplays[DisplayDevice::DISPLAY_PRIMARY]); 124 } 125 126 // utility function to delete a texture on the main thread 127 void deleteTextureAsync(uint32_t texture); 128 129 // enable/disable h/w composer event 130 // TODO: this should be made accessible only to EventThread 131#ifdef USE_HWC2 132 void setVsyncEnabled(int disp, int enabled); 133#else 134 void eventControl(int disp, int event, int enabled); 135#endif 136 137 // called on the main thread by MessageQueue when an internal message 138 // is received 139 // TODO: this should be made accessible only to MessageQueue 140 void onMessageReceived(int32_t what); 141 142 // for debugging only 143 // TODO: this should be made accessible only to HWComposer 144 const Vector< sp<Layer> >& getLayerSortedByZForHwcDisplay(int id); 145 146 RenderEngine& getRenderEngine() const { 147 return *mRenderEngine; 148 } 149 150private: 151 friend class Client; 152 friend class DisplayEventConnection; 153 friend class EventThread; 154 friend class Layer; 155 friend class MonitoredProducer; 156 157 // This value is specified in number of frames. Log frame stats at most 158 // every half hour. 159 enum { LOG_FRAME_STATS_PERIOD = 30*60*60 }; 160 161 static const size_t MAX_LAYERS = 4096; 162 163 // We're reference counted, never destroy SurfaceFlinger directly 164 virtual ~SurfaceFlinger(); 165 166 /* ------------------------------------------------------------------------ 167 * Internal data structures 168 */ 169 170 class LayerVector : public SortedVector< sp<Layer> > { 171 public: 172 LayerVector(); 173 LayerVector(const LayerVector& rhs); 174 virtual int do_compare(const void* lhs, const void* rhs) const; 175 }; 176 177 struct State { 178 LayerVector layersSortedByZ; 179 DefaultKeyedVector< wp<IBinder>, DisplayDeviceState> displays; 180 }; 181 182 /* ------------------------------------------------------------------------ 183 * IBinder interface 184 */ 185 virtual status_t onTransact(uint32_t code, const Parcel& data, 186 Parcel* reply, uint32_t flags); 187 virtual status_t dump(int fd, const Vector<String16>& args); 188 189 /* ------------------------------------------------------------------------ 190 * ISurfaceComposer interface 191 */ 192 virtual sp<ISurfaceComposerClient> createConnection(); 193 virtual sp<IGraphicBufferAlloc> createGraphicBufferAlloc(); 194 virtual sp<IBinder> createDisplay(const String8& displayName, bool secure); 195 virtual void destroyDisplay(const sp<IBinder>& display); 196 virtual sp<IBinder> getBuiltInDisplay(int32_t id); 197 virtual void setTransactionState(const Vector<ComposerState>& state, 198 const Vector<DisplayState>& displays, uint32_t flags); 199 virtual void bootFinished(); 200 virtual bool authenticateSurfaceTexture( 201 const sp<IGraphicBufferProducer>& bufferProducer) const; 202 virtual status_t getSupportedFrameTimestamps( 203 std::vector<FrameEvent>* outSupported) const; 204 virtual sp<IDisplayEventConnection> createDisplayEventConnection(); 205 virtual status_t captureScreen(const sp<IBinder>& display, 206 const sp<IGraphicBufferProducer>& producer, 207 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight, 208 uint32_t minLayerZ, uint32_t maxLayerZ, 209 bool useIdentityTransform, ISurfaceComposer::Rotation rotation); 210 virtual status_t getDisplayStats(const sp<IBinder>& display, 211 DisplayStatInfo* stats); 212 virtual status_t getDisplayConfigs(const sp<IBinder>& display, 213 Vector<DisplayInfo>* configs); 214 virtual int getActiveConfig(const sp<IBinder>& display); 215 virtual status_t getDisplayColorModes(const sp<IBinder>& display, 216 Vector<android_color_mode_t>* configs); 217 virtual android_color_mode_t getActiveColorMode(const sp<IBinder>& display); 218 virtual status_t setActiveColorMode(const sp<IBinder>& display, android_color_mode_t colorMode); 219 virtual void setPowerMode(const sp<IBinder>& display, int mode); 220 virtual status_t setActiveConfig(const sp<IBinder>& display, int id); 221 virtual status_t clearAnimationFrameStats(); 222 virtual status_t getAnimationFrameStats(FrameStats* outStats) const; 223 virtual status_t getHdrCapabilities(const sp<IBinder>& display, 224 HdrCapabilities* outCapabilities) const; 225 virtual status_t enableVSyncInjections(bool enable); 226 virtual status_t injectVSync(nsecs_t when); 227 228 229 /* ------------------------------------------------------------------------ 230 * DeathRecipient interface 231 */ 232 virtual void binderDied(const wp<IBinder>& who); 233 234 /* ------------------------------------------------------------------------ 235 * RefBase interface 236 */ 237 virtual void onFirstRef(); 238 239 /* ------------------------------------------------------------------------ 240 * HWComposer::EventHandler interface 241 */ 242 virtual void onVSyncReceived(int type, nsecs_t timestamp); 243 virtual void onHotplugReceived(int disp, bool connected); 244 245 /* ------------------------------------------------------------------------ 246 * Message handling 247 */ 248 void waitForEvent(); 249 void signalTransaction(); 250 void signalLayerUpdate(); 251 void signalRefresh(); 252 253 // called on the main thread in response to initializeDisplays() 254 void onInitializeDisplays(); 255 // called on the main thread in response to setActiveConfig() 256 void setActiveConfigInternal(const sp<DisplayDevice>& hw, int mode); 257 // called on the main thread in response to setPowerMode() 258 void setPowerModeInternal(const sp<DisplayDevice>& hw, int mode); 259 260 // Called on the main thread in response to setActiveColorMode() 261 void setActiveColorModeInternal(const sp<DisplayDevice>& hw, android_color_mode_t colorMode); 262 263 // Returns whether the transaction actually modified any state 264 bool handleMessageTransaction(); 265 266 // Returns whether a new buffer has been latched (see handlePageFlip()) 267 bool handleMessageInvalidate(); 268 269 void handleMessageRefresh(); 270 271 void handleTransaction(uint32_t transactionFlags); 272 void handleTransactionLocked(uint32_t transactionFlags); 273 274 void updateCursorAsync(); 275 276 /* handlePageFlip - latch a new buffer if available and compute the dirty 277 * region. Returns whether a new buffer has been latched, i.e., whether it 278 * is necessary to perform a refresh during this vsync. 279 */ 280 bool handlePageFlip(); 281 282 /* ------------------------------------------------------------------------ 283 * Transactions 284 */ 285 uint32_t getTransactionFlags(uint32_t flags); 286 uint32_t peekTransactionFlags(); 287 uint32_t setTransactionFlags(uint32_t flags); 288 void commitTransaction(); 289 uint32_t setClientStateLocked(const sp<Client>& client, const layer_state_t& s); 290 uint32_t setDisplayStateLocked(const DisplayState& s); 291 292 /* ------------------------------------------------------------------------ 293 * Layer management 294 */ 295 status_t createLayer(const String8& name, const sp<Client>& client, 296 uint32_t w, uint32_t h, PixelFormat format, uint32_t flags, 297 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp); 298 299 status_t createNormalLayer(const sp<Client>& client, const String8& name, 300 uint32_t w, uint32_t h, uint32_t flags, PixelFormat& format, 301 sp<IBinder>* outHandle, sp<IGraphicBufferProducer>* outGbp, 302 sp<Layer>* outLayer); 303 304 status_t createDimLayer(const sp<Client>& client, const String8& name, 305 uint32_t w, uint32_t h, uint32_t flags, sp<IBinder>* outHandle, 306 sp<IGraphicBufferProducer>* outGbp, sp<Layer>* outLayer); 307 308 // called in response to the window-manager calling 309 // ISurfaceComposerClient::destroySurface() 310 status_t onLayerRemoved(const sp<Client>& client, const sp<IBinder>& handle); 311 312 // called when all clients have released all their references to 313 // this layer meaning it is entirely safe to destroy all 314 // resources associated to this layer. 315 status_t onLayerDestroyed(const wp<Layer>& layer); 316 317 // remove a layer from SurfaceFlinger immediately 318 status_t removeLayer(const wp<Layer>& layer); 319 320 // add a layer to SurfaceFlinger 321 status_t addClientLayer(const sp<Client>& client, 322 const sp<IBinder>& handle, 323 const sp<IGraphicBufferProducer>& gbc, 324 const sp<Layer>& lbc); 325 326 /* ------------------------------------------------------------------------ 327 * Boot animation, on/off animations and screen capture 328 */ 329 330 void startBootAnim(); 331 332 void renderScreenImplLocked( 333 const sp<const DisplayDevice>& hw, 334 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight, 335 uint32_t minLayerZ, uint32_t maxLayerZ, 336 bool yswap, bool useIdentityTransform, Transform::orientation_flags rotation); 337 338 status_t captureScreenImplLocked( 339 const sp<const DisplayDevice>& hw, 340 const sp<IGraphicBufferProducer>& producer, 341 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight, 342 uint32_t minLayerZ, uint32_t maxLayerZ, 343 bool useIdentityTransform, Transform::orientation_flags rotation, 344 bool isLocalScreenshot); 345 346 /* ------------------------------------------------------------------------ 347 * EGL 348 */ 349 size_t getMaxTextureSize() const; 350 size_t getMaxViewportDims() const; 351 352 /* ------------------------------------------------------------------------ 353 * Display and layer stack management 354 */ 355 // called when starting, or restarting after system_server death 356 void initializeDisplays(); 357 358 // Create an IBinder for a builtin display and add it to current state 359 void createBuiltinDisplayLocked(DisplayDevice::DisplayType type); 360 361 // NOTE: can only be called from the main thread or with mStateLock held 362 sp<const DisplayDevice> getDisplayDevice(const wp<IBinder>& dpy) const { 363 return mDisplays.valueFor(dpy); 364 } 365 366 // NOTE: can only be called from the main thread or with mStateLock held 367 sp<DisplayDevice> getDisplayDevice(const wp<IBinder>& dpy) { 368 return mDisplays.valueFor(dpy); 369 } 370 371 int32_t getDisplayType(const sp<IBinder>& display) { 372 if (!display.get()) return NAME_NOT_FOUND; 373 for (int i = 0; i < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES; ++i) { 374 if (display == mBuiltinDisplays[i]) { 375 return i; 376 } 377 } 378 return NAME_NOT_FOUND; 379 } 380 381 // mark a region of a layer stack dirty. this updates the dirty 382 // region of all screens presenting this layer stack. 383 void invalidateLayerStack(uint32_t layerStack, const Region& dirty); 384 385#ifndef USE_HWC2 386 int32_t allocateHwcDisplayId(DisplayDevice::DisplayType type); 387#endif 388 389 /* ------------------------------------------------------------------------ 390 * H/W composer 391 */ 392 393 HWComposer& getHwComposer() const { return *mHwc; } 394 395 /* ------------------------------------------------------------------------ 396 * Compositing 397 */ 398 void invalidateHwcGeometry(); 399 static void computeVisibleRegions( 400 const LayerVector& currentLayers, uint32_t layerStack, 401 Region& dirtyRegion, Region& opaqueRegion); 402 403 void preComposition(nsecs_t refreshStartTime); 404 void postComposition(); 405 void rebuildLayerStacks(); 406 void setUpHWComposer(); 407 void doComposition(); 408 void doDebugFlashRegions(); 409 void doDisplayComposition(const sp<const DisplayDevice>& displayDevice, const Region& dirtyRegion); 410 411 // compose surfaces for display hw. this fails if using GL and the surface 412 // has been destroyed and is no longer valid. 413 bool doComposeSurfaces(const sp<const DisplayDevice>& displayDevice, const Region& dirty); 414 415 void postFramebuffer(); 416 void drawWormhole(const sp<const DisplayDevice>& displayDevice, const Region& region) const; 417 418 /* ------------------------------------------------------------------------ 419 * Display management 420 */ 421 422 /* ------------------------------------------------------------------------ 423 * VSync 424 */ 425 void enableHardwareVsync(); 426 void resyncToHardwareVsync(bool makeAvailable); 427 void disableHardwareVsync(bool makeUnavailable); 428public: 429 void resyncWithRateLimit(); 430private: 431 432 /* ------------------------------------------------------------------------ 433 * Debugging & dumpsys 434 */ 435 void listLayersLocked(const Vector<String16>& args, size_t& index, String8& result) const; 436 void dumpStatsLocked(const Vector<String16>& args, size_t& index, String8& result) const; 437 void clearStatsLocked(const Vector<String16>& args, size_t& index, String8& result); 438 void dumpAllLocked(const Vector<String16>& args, size_t& index, String8& result) const; 439 bool startDdmConnection(); 440 static void appendSfConfigString(String8& result); 441 void checkScreenshot(size_t w, size_t s, size_t h, void const* vaddr, 442 const sp<const DisplayDevice>& hw, 443 uint32_t minLayerZ, uint32_t maxLayerZ); 444 445 void logFrameStats(); 446 447 void dumpStaticScreenStats(String8& result) const; 448 // Not const because each Layer needs to query Fences and cache timestamps. 449 void dumpFrameEventsLocked(String8& result); 450 451 void recordBufferingStats(const char* layerName, 452 std::vector<OccupancyTracker::Segment>&& history); 453 void dumpBufferingStats(String8& result) const; 454 455 /* ------------------------------------------------------------------------ 456 * Attributes 457 */ 458 459 // access must be protected by mStateLock 460 mutable Mutex mStateLock; 461 State mCurrentState; 462 volatile int32_t mTransactionFlags; 463 Condition mTransactionCV; 464 bool mTransactionPending; 465 bool mAnimTransactionPending; 466 Vector< sp<Layer> > mLayersPendingRemoval; 467 SortedVector< wp<IBinder> > mGraphicBufferProducerList; 468 469 // protected by mStateLock (but we could use another lock) 470 bool mLayersRemoved; 471 472 // access must be protected by mInvalidateLock 473 volatile int32_t mRepaintEverything; 474 475 // constant members (no synchronization needed for access) 476 HWComposer* mHwc; 477 RenderEngine* mRenderEngine; 478 nsecs_t mBootTime; 479 bool mGpuToCpuSupported; 480 sp<EventThread> mEventThread; 481 sp<EventThread> mSFEventThread; 482 sp<EventThread> mInjectorEventThread; 483 sp<InjectVSyncSource> mVSyncInjector; 484 sp<EventControlThread> mEventControlThread; 485 EGLContext mEGLContext; 486 EGLDisplay mEGLDisplay; 487 sp<IBinder> mBuiltinDisplays[DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES]; 488 489 // Can only accessed from the main thread, these members 490 // don't need synchronization 491 State mDrawingState; 492 bool mVisibleRegionsDirty; 493#ifndef USE_HWC2 494 bool mHwWorkListDirty; 495#else 496 bool mGeometryInvalid; 497#endif 498 bool mAnimCompositionPending; 499#ifdef USE_HWC2 500 std::vector<sp<Layer>> mLayersWithQueuedFrames; 501 sp<Fence> mPreviousPresentFence = Fence::NO_FENCE; 502 bool mHadClientComposition = false; 503#endif 504 505 // this may only be written from the main thread with mStateLock held 506 // it may be read from other threads with mStateLock held 507 DefaultKeyedVector< wp<IBinder>, sp<DisplayDevice> > mDisplays; 508 509 // don't use a lock for these, we don't care 510 int mDebugRegion; 511 int mDebugDDMS; 512 int mDebugDisableHWC; 513 int mDebugDisableTransformHint; 514 volatile nsecs_t mDebugInSwapBuffers; 515 nsecs_t mLastSwapBufferTime; 516 volatile nsecs_t mDebugInTransaction; 517 nsecs_t mLastTransactionTime; 518 bool mBootFinished; 519 bool mForceFullDamage; 520#ifdef USE_HWC2 521 bool mPropagateBackpressure = true; 522#endif 523 SurfaceInterceptor mInterceptor; 524 bool mUseHwcVirtualDisplays = true; 525 526 // these are thread safe 527 mutable MessageQueue mEventQueue; 528 FrameTracker mAnimFrameTracker; 529 DispSync mPrimaryDispSync; 530 531 // protected by mDestroyedLayerLock; 532 mutable Mutex mDestroyedLayerLock; 533 Vector<Layer const *> mDestroyedLayers; 534 535 // protected by mHWVsyncLock 536 Mutex mHWVsyncLock; 537 bool mPrimaryHWVsyncEnabled; 538 bool mHWVsyncAvailable; 539 540 /* ------------------------------------------------------------------------ 541 * Feature prototyping 542 */ 543 544 bool mInjectVSyncs; 545 546 Daltonizer mDaltonizer; 547#ifndef USE_HWC2 548 bool mDaltonize; 549#endif 550 551 mat4 mPreviousColorMatrix; 552 mat4 mColorMatrix; 553 bool mHasColorMatrix; 554 555 // Static screen stats 556 bool mHasPoweredOff; 557 static const size_t NUM_BUCKETS = 8; // < 1-7, 7+ 558 nsecs_t mFrameBuckets[NUM_BUCKETS]; 559 nsecs_t mTotalTime; 560 std::atomic<nsecs_t> mLastSwapTime; 561 562 // Double- vs. triple-buffering stats 563 struct BufferingStats { 564 BufferingStats() 565 : numSegments(0), 566 totalTime(0), 567 twoBufferTime(0), 568 doubleBufferedTime(0), 569 tripleBufferedTime(0) {} 570 571 size_t numSegments; 572 nsecs_t totalTime; 573 574 // "Two buffer" means that a third buffer was never used, whereas 575 // "double-buffered" means that on average the segment only used two 576 // buffers (though it may have used a third for some part of the 577 // segment) 578 nsecs_t twoBufferTime; 579 nsecs_t doubleBufferedTime; 580 nsecs_t tripleBufferedTime; 581 }; 582 mutable Mutex mBufferingStatsMutex; 583 std::unordered_map<std::string, BufferingStats> mBufferingStats; 584 585 // Verify that transaction is being called by an approved process: 586 // either AID_GRAPHICS or AID_SYSTEM. 587 status_t CheckTransactCodeCredentials(uint32_t code); 588 }; 589 590}; // namespace android 591 592#endif // ANDROID_SURFACE_FLINGER_H 593