SurfaceFlinger.h revision 4a4e4a239f034cb8af2df9a438b26c3bc088889c
165953da4636fbf5f0a24b8f5f2b5fa7d76ff13d9Marius Renn/* 265953da4636fbf5f0a24b8f5f2b5fa7d76ff13d9Marius Renn * Copyright (C) 2007 The Android Open Source Project 365953da4636fbf5f0a24b8f5f2b5fa7d76ff13d9Marius Renn * 465953da4636fbf5f0a24b8f5f2b5fa7d76ff13d9Marius Renn * Licensed under the Apache License, Version 2.0 (the "License"); 565953da4636fbf5f0a24b8f5f2b5fa7d76ff13d9Marius Renn * you may not use this file except in compliance with the License. 665953da4636fbf5f0a24b8f5f2b5fa7d76ff13d9Marius Renn * You may obtain a copy of the License at 765953da4636fbf5f0a24b8f5f2b5fa7d76ff13d9Marius Renn * 865953da4636fbf5f0a24b8f5f2b5fa7d76ff13d9Marius Renn * http://www.apache.org/licenses/LICENSE-2.0 965953da4636fbf5f0a24b8f5f2b5fa7d76ff13d9Marius Renn * 1065953da4636fbf5f0a24b8f5f2b5fa7d76ff13d9Marius Renn * Unless required by applicable law or agreed to in writing, software 1165953da4636fbf5f0a24b8f5f2b5fa7d76ff13d9Marius Renn * distributed under the License is distributed on an "AS IS" BASIS, 1265953da4636fbf5f0a24b8f5f2b5fa7d76ff13d9Marius Renn * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1365953da4636fbf5f0a24b8f5f2b5fa7d76ff13d9Marius Renn * See the License for the specific language governing permissions and 1465953da4636fbf5f0a24b8f5f2b5fa7d76ff13d9Marius Renn * limitations under the License. 1565953da4636fbf5f0a24b8f5f2b5fa7d76ff13d9Marius Renn */ 1665953da4636fbf5f0a24b8f5f2b5fa7d76ff13d9Marius Renn 1765953da4636fbf5f0a24b8f5f2b5fa7d76ff13d9Marius Renn#ifndef ANDROID_SURFACE_FLINGER_H 1865953da4636fbf5f0a24b8f5f2b5fa7d76ff13d9Marius Renn#define ANDROID_SURFACE_FLINGER_H 1965953da4636fbf5f0a24b8f5f2b5fa7d76ff13d9Marius Renn 2065953da4636fbf5f0a24b8f5f2b5fa7d76ff13d9Marius Renn#include <stdint.h> 2165953da4636fbf5f0a24b8f5f2b5fa7d76ff13d9Marius Renn#include <sys/types.h> 2265953da4636fbf5f0a24b8f5f2b5fa7d76ff13d9Marius Renn 2365953da4636fbf5f0a24b8f5f2b5fa7d76ff13d9Marius Renn#include <EGL/egl.h> 2465953da4636fbf5f0a24b8f5f2b5fa7d76ff13d9Marius Renn 2565953da4636fbf5f0a24b8f5f2b5fa7d76ff13d9Marius Renn/* 2665953da4636fbf5f0a24b8f5f2b5fa7d76ff13d9Marius Renn * NOTE: Make sure this file doesn't include anything from <gl/ > or <gl2/ > 2765953da4636fbf5f0a24b8f5f2b5fa7d76ff13d9Marius Renn */ 2865953da4636fbf5f0a24b8f5f2b5fa7d76ff13d9Marius Renn 2965953da4636fbf5f0a24b8f5f2b5fa7d76ff13d9Marius Renn#include <cutils/compiler.h> 3065953da4636fbf5f0a24b8f5f2b5fa7d76ff13d9Marius Renn 3165953da4636fbf5f0a24b8f5f2b5fa7d76ff13d9Marius Renn#include <utils/Atomic.h> 3265953da4636fbf5f0a24b8f5f2b5fa7d76ff13d9Marius Renn#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 46#include <hardware/hwcomposer_defs.h> 47 48#include <private/gui/LayerState.h> 49 50#include "Barrier.h" 51#include "DisplayDevice.h" 52#include "DispSync.h" 53#include "FenceTracker.h" 54#include "FrameTracker.h" 55#include "MessageQueue.h" 56 57#include "DisplayHardware/HWComposer.h" 58#include "Effects/Daltonizer.h" 59 60namespace android { 61 62// --------------------------------------------------------------------------- 63 64class Client; 65class DisplayEventConnection; 66class EventThread; 67class IGraphicBufferAlloc; 68class Layer; 69class LayerDim; 70class Surface; 71class RenderEngine; 72class EventControlThread; 73 74// --------------------------------------------------------------------------- 75 76enum { 77 eTransactionNeeded = 0x01, 78 eTraversalNeeded = 0x02, 79 eDisplayTransactionNeeded = 0x04, 80 eTransactionMask = 0x07 81}; 82 83class SurfaceFlinger : public BnSurfaceComposer, 84 private IBinder::DeathRecipient, 85 private HWComposer::EventHandler 86{ 87public: 88 static char const* getServiceName() ANDROID_API { 89 return "SurfaceFlinger"; 90 } 91 92 SurfaceFlinger() ANDROID_API; 93 94 // must be called before clients can connect 95 void init() ANDROID_API; 96 97 // starts SurfaceFlinger main loop in the current thread 98 void run() ANDROID_API; 99 100 enum { 101 EVENT_VSYNC = HWC_EVENT_VSYNC 102 }; 103 104 // post an asynchronous message to the main thread 105 status_t postMessageAsync(const sp<MessageBase>& msg, nsecs_t reltime = 0, uint32_t flags = 0); 106 107 // post a synchronous message to the main thread 108 status_t postMessageSync(const sp<MessageBase>& msg, nsecs_t reltime = 0, uint32_t flags = 0); 109 110 // force full composition on all displays 111 void repaintEverything(); 112 113 // returns the default Display 114 sp<const DisplayDevice> getDefaultDisplayDevice() const { 115 return getDisplayDevice(mBuiltinDisplays[DisplayDevice::DISPLAY_PRIMARY]); 116 } 117 118 // utility function to delete a texture on the main thread 119 void deleteTextureAsync(uint32_t texture); 120 121 // enable/disable h/w composer event 122 // TODO: this should be made accessible only to EventThread 123#ifdef USE_HWC2 124 void setVsyncEnabled(int disp, int enabled); 125#else 126 void eventControl(int disp, int event, int enabled); 127#endif 128 129 // called on the main thread by MessageQueue when an internal message 130 // is received 131 // TODO: this should be made accessible only to MessageQueue 132 void onMessageReceived(int32_t what); 133 134 // for debugging only 135 // TODO: this should be made accessible only to HWComposer 136 const Vector< sp<Layer> >& getLayerSortedByZForHwcDisplay(int id); 137 138 RenderEngine& getRenderEngine() const { 139 return *mRenderEngine; 140 } 141 142private: 143 friend class Client; 144 friend class DisplayEventConnection; 145 friend class Layer; 146 friend class MonitoredProducer; 147 148 // This value is specified in number of frames. Log frame stats at most 149 // every half hour. 150 enum { LOG_FRAME_STATS_PERIOD = 30*60*60 }; 151 152 static const size_t MAX_LAYERS = 4096; 153 154 // We're reference counted, never destroy SurfaceFlinger directly 155 virtual ~SurfaceFlinger(); 156 157 /* ------------------------------------------------------------------------ 158 * Internal data structures 159 */ 160 161 class LayerVector : public SortedVector< sp<Layer> > { 162 public: 163 LayerVector(); 164 LayerVector(const LayerVector& rhs); 165 virtual int do_compare(const void* lhs, const void* rhs) const; 166 }; 167 168 struct DisplayDeviceState { 169 DisplayDeviceState(); 170 DisplayDeviceState(DisplayDevice::DisplayType type, bool isSecure); 171 bool isValid() const { return type >= 0; } 172 bool isMainDisplay() const { return type == DisplayDevice::DISPLAY_PRIMARY; } 173 bool isVirtualDisplay() const { return type >= DisplayDevice::DISPLAY_VIRTUAL; } 174 DisplayDevice::DisplayType type; 175 sp<IGraphicBufferProducer> surface; 176 uint32_t layerStack; 177 Rect viewport; 178 Rect frame; 179 uint8_t orientation; 180 uint32_t width, height; 181 String8 displayName; 182 bool isSecure; 183 }; 184 185 struct State { 186 LayerVector layersSortedByZ; 187 DefaultKeyedVector< wp<IBinder>, DisplayDeviceState> displays; 188 }; 189 190 /* ------------------------------------------------------------------------ 191 * IBinder interface 192 */ 193 virtual status_t onTransact(uint32_t code, const Parcel& data, 194 Parcel* reply, uint32_t flags); 195 virtual status_t dump(int fd, const Vector<String16>& args); 196 197 /* ------------------------------------------------------------------------ 198 * ISurfaceComposer interface 199 */ 200 virtual sp<ISurfaceComposerClient> createConnection(); 201 virtual sp<IGraphicBufferAlloc> createGraphicBufferAlloc(); 202 virtual sp<IBinder> createDisplay(const String8& displayName, bool secure); 203 virtual void destroyDisplay(const sp<IBinder>& display); 204 virtual sp<IBinder> getBuiltInDisplay(int32_t id); 205 virtual void setTransactionState(const Vector<ComposerState>& state, 206 const Vector<DisplayState>& displays, uint32_t flags); 207 virtual void bootFinished(); 208 virtual bool authenticateSurfaceTexture( 209 const sp<IGraphicBufferProducer>& bufferProducer) const; 210 virtual sp<IDisplayEventConnection> createDisplayEventConnection(); 211 virtual status_t captureScreen(const sp<IBinder>& display, 212 const sp<IGraphicBufferProducer>& producer, 213 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight, 214 uint32_t minLayerZ, uint32_t maxLayerZ, 215 bool useIdentityTransform, ISurfaceComposer::Rotation rotation); 216 virtual status_t getDisplayStats(const sp<IBinder>& display, 217 DisplayStatInfo* stats); 218 virtual status_t getDisplayConfigs(const sp<IBinder>& display, 219 Vector<DisplayInfo>* configs); 220 virtual int getActiveConfig(const sp<IBinder>& display); 221 virtual void setPowerMode(const sp<IBinder>& display, int mode); 222 virtual status_t setActiveConfig(const sp<IBinder>& display, int id); 223 virtual status_t clearAnimationFrameStats(); 224 virtual status_t getAnimationFrameStats(FrameStats* outStats) const; 225 226 /* ------------------------------------------------------------------------ 227 * DeathRecipient interface 228 */ 229 virtual void binderDied(const wp<IBinder>& who); 230 231 /* ------------------------------------------------------------------------ 232 * RefBase interface 233 */ 234 virtual void onFirstRef(); 235 236 /* ------------------------------------------------------------------------ 237 * HWComposer::EventHandler interface 238 */ 239 virtual void onVSyncReceived(int type, nsecs_t timestamp); 240 virtual void onHotplugReceived(int disp, bool connected); 241 242 /* ------------------------------------------------------------------------ 243 * Message handling 244 */ 245 void waitForEvent(); 246 void signalTransaction(); 247 void signalLayerUpdate(); 248 void signalRefresh(); 249 250 // called on the main thread in response to initializeDisplays() 251 void onInitializeDisplays(); 252 // called on the main thread in response to setActiveConfig() 253 void setActiveConfigInternal(const sp<DisplayDevice>& hw, int mode); 254 // called on the main thread in response to setPowerMode() 255 void setPowerModeInternal(const sp<DisplayDevice>& hw, int mode); 256 257 // Returns whether the transaction actually modified any state 258 bool handleMessageTransaction(); 259 260 // Returns whether a new buffer has been latched (see handlePageFlip()) 261 bool handleMessageInvalidate(); 262 263 void handleMessageRefresh(); 264 265 void handleTransaction(uint32_t transactionFlags); 266 void handleTransactionLocked(uint32_t transactionFlags); 267 268 void updateCursorAsync(); 269 270 /* handlePageFlip - latch a new buffer if available and compute the dirty 271 * region. Returns whether a new buffer has been latched, i.e., whether it 272 * is necessary to perform a refresh during this vsync. 273 */ 274 bool handlePageFlip(); 275 276 /* ------------------------------------------------------------------------ 277 * Transactions 278 */ 279 uint32_t getTransactionFlags(uint32_t flags); 280 uint32_t peekTransactionFlags(uint32_t flags); 281 uint32_t setTransactionFlags(uint32_t flags); 282 void commitTransaction(); 283 uint32_t setClientStateLocked(const sp<Client>& client, const layer_state_t& s); 284 uint32_t setDisplayStateLocked(const DisplayState& s); 285 286 /* ------------------------------------------------------------------------ 287 * Layer management 288 */ 289 status_t createLayer(const String8& name, const sp<Client>& client, 290 uint32_t w, uint32_t h, PixelFormat format, uint32_t flags, 291 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp); 292 293 status_t createNormalLayer(const sp<Client>& client, const String8& name, 294 uint32_t w, uint32_t h, uint32_t flags, PixelFormat& format, 295 sp<IBinder>* outHandle, sp<IGraphicBufferProducer>* outGbp, 296 sp<Layer>* outLayer); 297 298 status_t createDimLayer(const sp<Client>& client, const String8& name, 299 uint32_t w, uint32_t h, uint32_t flags, sp<IBinder>* outHandle, 300 sp<IGraphicBufferProducer>* outGbp, sp<Layer>* outLayer); 301 302 // called in response to the window-manager calling 303 // ISurfaceComposerClient::destroySurface() 304 status_t onLayerRemoved(const sp<Client>& client, const sp<IBinder>& handle); 305 306 // called when all clients have released all their references to 307 // this layer meaning it is entirely safe to destroy all 308 // resources associated to this layer. 309 status_t onLayerDestroyed(const wp<Layer>& layer); 310 311 // remove a layer from SurfaceFlinger immediately 312 status_t removeLayer(const sp<Layer>& layer); 313 314 // add a layer to SurfaceFlinger 315 status_t addClientLayer(const sp<Client>& client, 316 const sp<IBinder>& handle, 317 const sp<IGraphicBufferProducer>& gbc, 318 const sp<Layer>& lbc); 319 320 /* ------------------------------------------------------------------------ 321 * Boot animation, on/off animations and screen capture 322 */ 323 324 void startBootAnim(); 325 326 void renderScreenImplLocked( 327 const sp<const DisplayDevice>& hw, 328 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight, 329 uint32_t minLayerZ, uint32_t maxLayerZ, 330 bool yswap, bool useIdentityTransform, Transform::orientation_flags rotation); 331 332 status_t captureScreenImplLocked( 333 const sp<const DisplayDevice>& hw, 334 const sp<IGraphicBufferProducer>& producer, 335 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight, 336 uint32_t minLayerZ, uint32_t maxLayerZ, 337 bool useIdentityTransform, Transform::orientation_flags rotation, 338 bool isLocalScreenshot); 339 340 /* ------------------------------------------------------------------------ 341 * EGL 342 */ 343 size_t getMaxTextureSize() const; 344 size_t getMaxViewportDims() const; 345 346 /* ------------------------------------------------------------------------ 347 * Display and layer stack management 348 */ 349 // called when starting, or restarting after system_server death 350 void initializeDisplays(); 351 352 // Create an IBinder for a builtin display and add it to current state 353 void createBuiltinDisplayLocked(DisplayDevice::DisplayType type); 354 355 // NOTE: can only be called from the main thread or with mStateLock held 356 sp<const DisplayDevice> getDisplayDevice(const wp<IBinder>& dpy) const { 357 return mDisplays.valueFor(dpy); 358 } 359 360 // NOTE: can only be called from the main thread or with mStateLock held 361 sp<DisplayDevice> getDisplayDevice(const wp<IBinder>& dpy) { 362 return mDisplays.valueFor(dpy); 363 } 364 365 // mark a region of a layer stack dirty. this updates the dirty 366 // region of all screens presenting this layer stack. 367 void invalidateLayerStack(uint32_t layerStack, const Region& dirty); 368 369#ifndef USE_HWC2 370 int32_t allocateHwcDisplayId(DisplayDevice::DisplayType type); 371#endif 372 373 /* ------------------------------------------------------------------------ 374 * H/W composer 375 */ 376 377 HWComposer& getHwComposer() const { return *mHwc; } 378 379 /* ------------------------------------------------------------------------ 380 * Compositing 381 */ 382 void invalidateHwcGeometry(); 383 static void computeVisibleRegions( 384 const LayerVector& currentLayers, uint32_t layerStack, 385 Region& dirtyRegion, Region& opaqueRegion); 386 387 void preComposition(); 388 void postComposition(nsecs_t refreshStartTime); 389 void rebuildLayerStacks(); 390 void setUpHWComposer(); 391 void doComposition(); 392 void doDebugFlashRegions(); 393 void doDisplayComposition(const sp<const DisplayDevice>& hw, const Region& dirtyRegion); 394 395 // compose surfaces for display hw. this fails if using GL and the surface 396 // has been destroyed and is no longer valid. 397 bool doComposeSurfaces(const sp<const DisplayDevice>& hw, const Region& dirty); 398 399 void postFramebuffer(); 400 void drawWormhole(const sp<const DisplayDevice>& hw, const Region& region) const; 401 402 /* ------------------------------------------------------------------------ 403 * Display management 404 */ 405 406 /* ------------------------------------------------------------------------ 407 * VSync 408 */ 409 void enableHardwareVsync(); 410 void resyncToHardwareVsync(bool makeAvailable); 411 void disableHardwareVsync(bool makeUnavailable); 412public: 413 void resyncWithRateLimit(); 414private: 415 416 /* ------------------------------------------------------------------------ 417 * Debugging & dumpsys 418 */ 419 void listLayersLocked(const Vector<String16>& args, size_t& index, String8& result) const; 420 void dumpStatsLocked(const Vector<String16>& args, size_t& index, String8& result) const; 421 void clearStatsLocked(const Vector<String16>& args, size_t& index, String8& result); 422 void dumpAllLocked(const Vector<String16>& args, size_t& index, String8& result) const; 423 bool startDdmConnection(); 424 static void appendSfConfigString(String8& result); 425 void checkScreenshot(size_t w, size_t s, size_t h, void const* vaddr, 426 const sp<const DisplayDevice>& hw, 427 uint32_t minLayerZ, uint32_t maxLayerZ); 428 429 void logFrameStats(); 430 431 void dumpStaticScreenStats(String8& result) const; 432 433 /* ------------------------------------------------------------------------ 434 * Attributes 435 */ 436 437 // access must be protected by mStateLock 438 mutable Mutex mStateLock; 439 State mCurrentState; 440 volatile int32_t mTransactionFlags; 441 Condition mTransactionCV; 442 bool mTransactionPending; 443 bool mAnimTransactionPending; 444 Vector< sp<Layer> > mLayersPendingRemoval; 445 SortedVector< wp<IBinder> > mGraphicBufferProducerList; 446 447 // protected by mStateLock (but we could use another lock) 448 bool mLayersRemoved; 449 450 // access must be protected by mInvalidateLock 451 volatile int32_t mRepaintEverything; 452 453 // constant members (no synchronization needed for access) 454 HWComposer* mHwc; 455 RenderEngine* mRenderEngine; 456 nsecs_t mBootTime; 457 bool mGpuToCpuSupported; 458 bool mDropMissedFrames; 459 sp<EventThread> mEventThread; 460 sp<EventThread> mSFEventThread; 461 sp<EventControlThread> mEventControlThread; 462 EGLContext mEGLContext; 463 EGLDisplay mEGLDisplay; 464 sp<IBinder> mBuiltinDisplays[DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES]; 465 466 // Can only accessed from the main thread, these members 467 // don't need synchronization 468 State mDrawingState; 469 bool mVisibleRegionsDirty; 470#ifndef USE_HWC2 471 bool mHwWorkListDirty; 472#else 473 bool mGeometryInvalid; 474#endif 475 bool mAnimCompositionPending; 476#ifdef USE_HWC2 477 std::vector<sp<Layer>> mLayersWithQueuedFrames; 478#endif 479 480 // this may only be written from the main thread with mStateLock held 481 // it may be read from other threads with mStateLock held 482 DefaultKeyedVector< wp<IBinder>, sp<DisplayDevice> > mDisplays; 483 484 // don't use a lock for these, we don't care 485 int mDebugRegion; 486 int mDebugDDMS; 487 int mDebugDisableHWC; 488 int mDebugDisableTransformHint; 489 volatile nsecs_t mDebugInSwapBuffers; 490 nsecs_t mLastSwapBufferTime; 491 volatile nsecs_t mDebugInTransaction; 492 nsecs_t mLastTransactionTime; 493 bool mBootFinished; 494 bool mForceFullDamage; 495 FenceTracker mFenceTracker; 496 497 // these are thread safe 498 mutable MessageQueue mEventQueue; 499 FrameTracker mAnimFrameTracker; 500 DispSync mPrimaryDispSync; 501 502 // protected by mDestroyedLayerLock; 503 mutable Mutex mDestroyedLayerLock; 504 Vector<Layer const *> mDestroyedLayers; 505 506 // protected by mHWVsyncLock 507 Mutex mHWVsyncLock; 508 bool mPrimaryHWVsyncEnabled; 509 bool mHWVsyncAvailable; 510 511 /* ------------------------------------------------------------------------ 512 * Feature prototyping 513 */ 514 515 Daltonizer mDaltonizer; 516 bool mDaltonize; 517 518 mat4 mColorMatrix; 519 bool mHasColorMatrix; 520 521 // Static screen stats 522 bool mHasPoweredOff; 523 static const size_t NUM_BUCKETS = 8; // < 1-7, 7+ 524 nsecs_t mFrameBuckets[NUM_BUCKETS]; 525 nsecs_t mTotalTime; 526 std::atomic<nsecs_t> mLastSwapTime; 527}; 528 529}; // namespace android 530 531#endif // ANDROID_SURFACE_FLINGER_H 532