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