SurfaceFlinger.h revision 40845df1285b387bcbf8f43ac72228eee2606d80
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 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 disableHardwareVsync(bool makeUnavailable); 411 void resyncToHardwareVsync(bool makeAvailable); 412 413 /* ------------------------------------------------------------------------ 414 * Debugging & dumpsys 415 */ 416 void listLayersLocked(const Vector<String16>& args, size_t& index, String8& result) const; 417 void dumpStatsLocked(const Vector<String16>& args, size_t& index, String8& result) const; 418 void clearStatsLocked(const Vector<String16>& args, size_t& index, String8& result); 419 void dumpAllLocked(const Vector<String16>& args, size_t& index, String8& result) const; 420 bool startDdmConnection(); 421 static void appendSfConfigString(String8& result); 422 void checkScreenshot(size_t w, size_t s, size_t h, void const* vaddr, 423 const sp<const DisplayDevice>& hw, 424 uint32_t minLayerZ, uint32_t maxLayerZ); 425 426 void logFrameStats(); 427 428 void dumpStaticScreenStats(String8& result) const; 429 430 /* ------------------------------------------------------------------------ 431 * Attributes 432 */ 433 434 // access must be protected by mStateLock 435 mutable Mutex mStateLock; 436 State mCurrentState; 437 volatile int32_t mTransactionFlags; 438 Condition mTransactionCV; 439 bool mTransactionPending; 440 bool mAnimTransactionPending; 441 Vector< sp<Layer> > mLayersPendingRemoval; 442 SortedVector< wp<IBinder> > mGraphicBufferProducerList; 443 444 // protected by mStateLock (but we could use another lock) 445 bool mLayersRemoved; 446 447 // access must be protected by mInvalidateLock 448 volatile int32_t mRepaintEverything; 449 450 // constant members (no synchronization needed for access) 451 HWComposer* mHwc; 452 RenderEngine* mRenderEngine; 453 nsecs_t mBootTime; 454 bool mGpuToCpuSupported; 455 bool mDropMissedFrames; 456 sp<EventThread> mEventThread; 457 sp<EventThread> mSFEventThread; 458 sp<EventControlThread> mEventControlThread; 459 EGLContext mEGLContext; 460 EGLDisplay mEGLDisplay; 461 sp<IBinder> mBuiltinDisplays[DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES]; 462 463 // Can only accessed from the main thread, these members 464 // don't need synchronization 465 State mDrawingState; 466 bool mVisibleRegionsDirty; 467#ifndef USE_HWC2 468 bool mHwWorkListDirty; 469#else 470 bool mGeometryInvalid; 471#endif 472 bool mAnimCompositionPending; 473#ifdef USE_HWC2 474 std::vector<sp<Layer>> mLayersWithQueuedFrames; 475#endif 476 477 // this may only be written from the main thread with mStateLock held 478 // it may be read from other threads with mStateLock held 479 DefaultKeyedVector< wp<IBinder>, sp<DisplayDevice> > mDisplays; 480 481 // don't use a lock for these, we don't care 482 int mDebugRegion; 483 int mDebugDDMS; 484 int mDebugDisableHWC; 485 int mDebugDisableTransformHint; 486 volatile nsecs_t mDebugInSwapBuffers; 487 nsecs_t mLastSwapBufferTime; 488 volatile nsecs_t mDebugInTransaction; 489 nsecs_t mLastTransactionTime; 490 bool mBootFinished; 491 bool mForceFullDamage; 492 FenceTracker mFenceTracker; 493 494 // these are thread safe 495 mutable MessageQueue mEventQueue; 496 FrameTracker mAnimFrameTracker; 497 DispSync mPrimaryDispSync; 498 499 // protected by mDestroyedLayerLock; 500 mutable Mutex mDestroyedLayerLock; 501 Vector<Layer const *> mDestroyedLayers; 502 503 // protected by mHWVsyncLock 504 Mutex mHWVsyncLock; 505 bool mPrimaryHWVsyncEnabled; 506 bool mHWVsyncAvailable; 507 508 /* ------------------------------------------------------------------------ 509 * Feature prototyping 510 */ 511 512 Daltonizer mDaltonizer; 513 bool mDaltonize; 514 515 mat4 mColorMatrix; 516 bool mHasColorMatrix; 517 518 // Static screen stats 519 bool mHasPoweredOff; 520 static const size_t NUM_BUCKETS = 8; // < 1-7, 7+ 521 nsecs_t mFrameBuckets[NUM_BUCKETS]; 522 nsecs_t mTotalTime; 523 nsecs_t mLastSwapTime; 524}; 525 526}; // namespace android 527 528#endif // ANDROID_SURFACE_FLINGER_H 529