SurfaceFlinger.h revision 1f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9
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/FenceTime.h> 41#include <ui/PixelFormat.h> 42#include <ui/mat4.h> 43 44#include <gui/ISurfaceComposer.h> 45#include <gui/ISurfaceComposerClient.h> 46#include <gui/OccupancyTracker.h> 47 48#include <hardware/hwcomposer_defs.h> 49 50#include <system/graphics.h> 51 52#include <private/gui/LayerState.h> 53 54#include "Barrier.h" 55#include "DisplayDevice.h" 56#include "DispSync.h" 57#include "FrameTracker.h" 58#include "LayerVector.h" 59#include "MessageQueue.h" 60#include "SurfaceInterceptor.h" 61 62#include "DisplayHardware/HWComposer.h" 63#include "Effects/Daltonizer.h" 64 65#include <map> 66#include <string> 67 68namespace android { 69 70// --------------------------------------------------------------------------- 71 72class Client; 73class DisplayEventConnection; 74class EventThread; 75class IGraphicBufferAlloc; 76class Layer; 77class LayerDim; 78class Surface; 79class RenderEngine; 80class EventControlThread; 81class VSyncSource; 82class InjectVSyncSource; 83 84// --------------------------------------------------------------------------- 85 86enum { 87 eTransactionNeeded = 0x01, 88 eTraversalNeeded = 0x02, 89 eDisplayTransactionNeeded = 0x04, 90 eTransactionMask = 0x07 91}; 92 93class SurfaceFlinger : public BnSurfaceComposer, 94 private IBinder::DeathRecipient, 95 private HWComposer::EventHandler 96{ 97public: 98 static char const* getServiceName() ANDROID_API { 99 return "SurfaceFlinger"; 100 } 101 102 SurfaceFlinger() ANDROID_API; 103 104 // must be called before clients can connect 105 void init() ANDROID_API; 106 107 // starts SurfaceFlinger main loop in the current thread 108 void run() ANDROID_API; 109 110 enum { 111 EVENT_VSYNC = HWC_EVENT_VSYNC 112 }; 113 114 // post an asynchronous message to the main thread 115 status_t postMessageAsync(const sp<MessageBase>& msg, nsecs_t reltime = 0, uint32_t flags = 0); 116 117 // post a synchronous message to the main thread 118 status_t postMessageSync(const sp<MessageBase>& msg, nsecs_t reltime = 0, uint32_t flags = 0); 119 120 // force full composition on all displays 121 void repaintEverything(); 122 123 // returns the default Display 124 sp<const DisplayDevice> getDefaultDisplayDevice() const { 125 return getDisplayDevice(mBuiltinDisplays[DisplayDevice::DISPLAY_PRIMARY]); 126 } 127 128 // utility function to delete a texture on the main thread 129 void deleteTextureAsync(uint32_t texture); 130 131 // enable/disable h/w composer event 132 // TODO: this should be made accessible only to EventThread 133#ifdef USE_HWC2 134 void setVsyncEnabled(int disp, int enabled); 135#else 136 void eventControl(int disp, int event, int enabled); 137#endif 138 139 // called on the main thread by MessageQueue when an internal message 140 // is received 141 // TODO: this should be made accessible only to MessageQueue 142 void onMessageReceived(int32_t what); 143 144 // for debugging only 145 // TODO: this should be made accessible only to HWComposer 146 const Vector< sp<Layer> >& getLayerSortedByZForHwcDisplay(int id); 147 148 RenderEngine& getRenderEngine() const { 149 return *mRenderEngine; 150 } 151 152private: 153 friend class Client; 154 friend class DisplayEventConnection; 155 friend class EventThread; 156 friend class Layer; 157 friend class MonitoredProducer; 158 159 // This value is specified in number of frames. Log frame stats at most 160 // every half hour. 161 enum { LOG_FRAME_STATS_PERIOD = 30*60*60 }; 162 163 static const size_t MAX_LAYERS = 4096; 164 165 // We're reference counted, never destroy SurfaceFlinger directly 166 virtual ~SurfaceFlinger(); 167 168 /* ------------------------------------------------------------------------ 169 * Internal data structures 170 */ 171 172 class State { 173 public: 174 LayerVector layersSortedByZ; 175 DefaultKeyedVector< wp<IBinder>, DisplayDeviceState> displays; 176 177 void traverseInZOrder(const std::function<void(Layer*)>& consume) const; 178 void traverseInReverseZOrder(const std::function<void(Layer*)>& consume) const; 179 }; 180 181 /* ------------------------------------------------------------------------ 182 * IBinder interface 183 */ 184 virtual status_t onTransact(uint32_t code, const Parcel& data, 185 Parcel* reply, uint32_t flags); 186 virtual status_t dump(int fd, const Vector<String16>& args); 187 188 /* ------------------------------------------------------------------------ 189 * ISurfaceComposer interface 190 */ 191 virtual sp<ISurfaceComposerClient> createConnection(); 192 virtual sp<IGraphicBufferAlloc> createGraphicBufferAlloc(); 193 virtual sp<IBinder> createDisplay(const String8& displayName, bool secure); 194 virtual void destroyDisplay(const sp<IBinder>& display); 195 virtual sp<IBinder> getBuiltInDisplay(int32_t id); 196 virtual void setTransactionState(const Vector<ComposerState>& state, 197 const Vector<DisplayState>& displays, uint32_t flags); 198 virtual void bootFinished(); 199 virtual bool authenticateSurfaceTexture( 200 const sp<IGraphicBufferProducer>& bufferProducer) const; 201 virtual status_t getSupportedFrameTimestamps( 202 std::vector<FrameEvent>* outSupported) const; 203 virtual sp<IDisplayEventConnection> createDisplayEventConnection(); 204 virtual status_t captureScreen(const sp<IBinder>& display, 205 const sp<IGraphicBufferProducer>& producer, 206 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight, 207 int32_t minLayerZ, int32_t maxLayerZ, 208 bool useIdentityTransform, ISurfaceComposer::Rotation rotation); 209 virtual status_t getDisplayStats(const sp<IBinder>& display, 210 DisplayStatInfo* stats); 211 virtual status_t getDisplayConfigs(const sp<IBinder>& display, 212 Vector<DisplayInfo>* configs); 213 virtual int getActiveConfig(const sp<IBinder>& display); 214 virtual status_t getDisplayColorModes(const sp<IBinder>& display, 215 Vector<android_color_mode_t>* configs); 216 virtual android_color_mode_t getActiveColorMode(const sp<IBinder>& display); 217 virtual status_t setActiveColorMode(const sp<IBinder>& display, android_color_mode_t colorMode); 218 virtual void setPowerMode(const sp<IBinder>& display, int mode); 219 virtual status_t setActiveConfig(const sp<IBinder>& display, int id); 220 virtual status_t clearAnimationFrameStats(); 221 virtual status_t getAnimationFrameStats(FrameStats* outStats) const; 222 virtual status_t getHdrCapabilities(const sp<IBinder>& display, 223 HdrCapabilities* outCapabilities) const; 224 virtual status_t enableVSyncInjections(bool enable); 225 virtual status_t injectVSync(nsecs_t when); 226 227 228 /* ------------------------------------------------------------------------ 229 * DeathRecipient interface 230 */ 231 virtual void binderDied(const wp<IBinder>& who); 232 233 /* ------------------------------------------------------------------------ 234 * RefBase interface 235 */ 236 virtual void onFirstRef(); 237 238 /* ------------------------------------------------------------------------ 239 * HWComposer::EventHandler interface 240 */ 241 virtual void onVSyncReceived(int type, nsecs_t timestamp); 242 virtual void onHotplugReceived(int disp, bool connected); 243 244 /* ------------------------------------------------------------------------ 245 * Message handling 246 */ 247 void waitForEvent(); 248 void signalTransaction(); 249 void signalLayerUpdate(); 250 void signalRefresh(); 251 252 // called on the main thread in response to initializeDisplays() 253 void onInitializeDisplays(); 254 // called on the main thread in response to setActiveConfig() 255 void setActiveConfigInternal(const sp<DisplayDevice>& hw, int mode); 256 // called on the main thread in response to setPowerMode() 257 void setPowerModeInternal(const sp<DisplayDevice>& hw, int mode); 258 259 // Called on the main thread in response to setActiveColorMode() 260 void setActiveColorModeInternal(const sp<DisplayDevice>& hw, android_color_mode_t colorMode); 261 262 // Returns whether the transaction actually modified any state 263 bool handleMessageTransaction(); 264 265 // Returns whether a new buffer has been latched (see handlePageFlip()) 266 bool handleMessageInvalidate(); 267 268 void handleMessageRefresh(); 269 270 void handleTransaction(uint32_t transactionFlags); 271 void handleTransactionLocked(uint32_t transactionFlags); 272 273 void updateCursorAsync(); 274 275 /* handlePageFlip - latch a new buffer if available and compute the dirty 276 * region. Returns whether a new buffer has been latched, i.e., whether it 277 * is necessary to perform a refresh during this vsync. 278 */ 279 bool handlePageFlip(); 280 281 /* ------------------------------------------------------------------------ 282 * Transactions 283 */ 284 uint32_t getTransactionFlags(uint32_t flags); 285 uint32_t peekTransactionFlags(); 286 uint32_t setTransactionFlags(uint32_t flags); 287 void commitTransaction(); 288 uint32_t setClientStateLocked(const sp<Client>& client, const layer_state_t& s); 289 uint32_t setDisplayStateLocked(const DisplayState& s); 290 291 /* ------------------------------------------------------------------------ 292 * Layer management 293 */ 294 status_t createLayer(const String8& name, const sp<Client>& client, 295 uint32_t w, uint32_t h, PixelFormat format, uint32_t flags, 296 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, 297 sp<Layer>* parent); 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 const sp<Layer>& parent); 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 int32_t minLayerZ, int32_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 int32_t minLayerZ, int32_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 void computeVisibleRegions(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); 428 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 void appendSfConfigString(String8& result) const; 442 void checkScreenshot(size_t w, size_t s, size_t h, void const* vaddr, 443 const sp<const DisplayDevice>& hw, 444 int32_t minLayerZ, int32_t maxLayerZ); 445 446 void logFrameStats(); 447 448 void dumpStaticScreenStats(String8& result) const; 449 // Not const because each Layer needs to query Fences and cache timestamps. 450 void dumpFrameEventsLocked(String8& result); 451 452 void recordBufferingStats(const char* layerName, 453 std::vector<OccupancyTracker::Segment>&& history); 454 void dumpBufferingStats(String8& result) const; 455 456 bool isLayerTripleBufferingDisabled() const { 457 return this->mLayerTripleBufferingDisabled; 458 } 459 /* ------------------------------------------------------------------------ 460 * Attributes 461 */ 462 463 // access must be protected by mStateLock 464 mutable Mutex mStateLock; 465 State mCurrentState; 466 volatile int32_t mTransactionFlags; 467 Condition mTransactionCV; 468 bool mTransactionPending; 469 bool mAnimTransactionPending; 470 SortedVector< sp<Layer> > mLayersPendingRemoval; 471 SortedVector< wp<IBinder> > mGraphicBufferProducerList; 472 473 // protected by mStateLock (but we could use another lock) 474 bool mLayersRemoved; 475 bool mLayersAdded; 476 477 // access must be protected by mInvalidateLock 478 volatile int32_t mRepaintEverything; 479 480 // constant members (no synchronization needed for access) 481 HWComposer* mHwc; 482 RenderEngine* mRenderEngine; 483 nsecs_t mBootTime; 484 bool mGpuToCpuSupported; 485 sp<EventThread> mEventThread; 486 sp<EventThread> mSFEventThread; 487 sp<EventThread> mInjectorEventThread; 488 sp<InjectVSyncSource> mVSyncInjector; 489 sp<EventControlThread> mEventControlThread; 490 EGLContext mEGLContext; 491 EGLDisplay mEGLDisplay; 492 sp<IBinder> mBuiltinDisplays[DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES]; 493 494 // Can only accessed from the main thread, these members 495 // don't need synchronization 496 State mDrawingState; 497 bool mVisibleRegionsDirty; 498#ifndef USE_HWC2 499 bool mHwWorkListDirty; 500#else 501 bool mGeometryInvalid; 502#endif 503 bool mAnimCompositionPending; 504#ifdef USE_HWC2 505 std::vector<sp<Layer>> mLayersWithQueuedFrames; 506 sp<Fence> mPreviousPresentFence = Fence::NO_FENCE; 507 bool mHadClientComposition = false; 508#endif 509 FenceTimeline mGlCompositionDoneTimeline; 510 FenceTimeline mDisplayTimeline; 511 512 // this may only be written from the main thread with mStateLock held 513 // it may be read from other threads with mStateLock held 514 DefaultKeyedVector< wp<IBinder>, sp<DisplayDevice> > mDisplays; 515 516 // don't use a lock for these, we don't care 517 int mDebugRegion; 518 int mDebugDDMS; 519 int mDebugDisableHWC; 520 int mDebugDisableTransformHint; 521 volatile nsecs_t mDebugInSwapBuffers; 522 nsecs_t mLastSwapBufferTime; 523 volatile nsecs_t mDebugInTransaction; 524 nsecs_t mLastTransactionTime; 525 bool mBootFinished; 526 bool mForceFullDamage; 527#ifdef USE_HWC2 528 bool mPropagateBackpressure = true; 529#endif 530 SurfaceInterceptor mInterceptor; 531 bool mUseHwcVirtualDisplays = true; 532 533 // Restrict layers to use two buffers in their bufferqueues. 534 bool mLayerTripleBufferingDisabled = false; 535 536 // these are thread safe 537 mutable MessageQueue mEventQueue; 538 FrameTracker mAnimFrameTracker; 539 DispSync mPrimaryDispSync; 540 541 // protected by mDestroyedLayerLock; 542 mutable Mutex mDestroyedLayerLock; 543 Vector<Layer const *> mDestroyedLayers; 544 545 // protected by mHWVsyncLock 546 Mutex mHWVsyncLock; 547 bool mPrimaryHWVsyncEnabled; 548 bool mHWVsyncAvailable; 549 550 /* ------------------------------------------------------------------------ 551 * Feature prototyping 552 */ 553 554 bool mInjectVSyncs; 555 556 Daltonizer mDaltonizer; 557#ifndef USE_HWC2 558 bool mDaltonize; 559#endif 560 561 mat4 mPreviousColorMatrix; 562 mat4 mColorMatrix; 563 bool mHasColorMatrix; 564 565 // Static screen stats 566 bool mHasPoweredOff; 567 static const size_t NUM_BUCKETS = 8; // < 1-7, 7+ 568 nsecs_t mFrameBuckets[NUM_BUCKETS]; 569 nsecs_t mTotalTime; 570 std::atomic<nsecs_t> mLastSwapTime; 571 572 size_t mNumLayers; 573 574 // Double- vs. triple-buffering stats 575 struct BufferingStats { 576 BufferingStats() 577 : numSegments(0), 578 totalTime(0), 579 twoBufferTime(0), 580 doubleBufferedTime(0), 581 tripleBufferedTime(0) {} 582 583 size_t numSegments; 584 nsecs_t totalTime; 585 586 // "Two buffer" means that a third buffer was never used, whereas 587 // "double-buffered" means that on average the segment only used two 588 // buffers (though it may have used a third for some part of the 589 // segment) 590 nsecs_t twoBufferTime; 591 nsecs_t doubleBufferedTime; 592 nsecs_t tripleBufferedTime; 593 }; 594 mutable Mutex mBufferingStatsMutex; 595 std::unordered_map<std::string, BufferingStats> mBufferingStats; 596 597 // Verify that transaction is being called by an approved process: 598 // either AID_GRAPHICS or AID_SYSTEM. 599 status_t CheckTransactCodeCredentials(uint32_t code); 600 }; 601 602}; // namespace android 603 604#endif // ANDROID_SURFACE_FLINGER_H 605