SurfaceFlinger.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  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. #ifndef ANDROID_SURFACE_FLINGER_H
  17. #define ANDROID_SURFACE_FLINGER_H
  18. #include <stdint.h>
  19. #include <sys/types.h>
  20. #include <EGL/egl.h>
  21. /*
  22. * NOTE: Make sure this file doesn't include anything from <gl/ > or <gl2/ >
  23. */
  24. #include <cutils/compiler.h>
  25. #include <utils/Atomic.h>
  26. #include <utils/Errors.h>
  27. #include <utils/KeyedVector.h>
  28. #include <utils/RefBase.h>
  29. #include <utils/SortedVector.h>
  30. #include <utils/threads.h>
  31. #include <binder/IMemory.h>
  32. #include <ui/PixelFormat.h>
  33. #include <ui/mat4.h>
  34. #include <gui/ISurfaceComposer.h>
  35. #include <gui/ISurfaceComposerClient.h>
  36. #include <hardware/hwcomposer_defs.h>
  37. #include <private/gui/LayerState.h>
  38. #include "Barrier.h"
  39. #include "DisplayDevice.h"
  40. #include "DispSync.h"
  41. #include "FrameTracker.h"
  42. #include "MessageQueue.h"
  43. #include "DisplayHardware/HWComposer.h"
  44. #include "Effects/Daltonizer.h"
  45. #include "FrameRateHelper.h"
  46. namespace android {
  47. // ---------------------------------------------------------------------------
  48. class Client;
  49. class DisplayEventConnection;
  50. class EventThread;
  51. class IGraphicBufferAlloc;
  52. class Layer;
  53. class LayerDim;
  54. class LayerBlur;
  55. class Surface;
  56. class RenderEngine;
  57. class EventControlThread;
  58. // ---------------------------------------------------------------------------
  59. enum {
  60. eTransactionNeeded = 0x01,
  61. eTraversalNeeded = 0x02,
  62. eDisplayTransactionNeeded = 0x04,
  63. eTransactionMask = 0x07
  64. };
  65. class SurfaceFlinger : public BnSurfaceComposer,
  66. private IBinder::DeathRecipient,
  67. private HWComposer::EventHandler
  68. {
  69. public:
  70. #ifdef QTI_BSP
  71. friend class ExSurfaceFlinger;
  72. #endif
  73. static char const* getServiceName() ANDROID_API {
  74. return "SurfaceFlinger";
  75. }
  76. SurfaceFlinger() ANDROID_API;
  77. // must be called before clients can connect
  78. void init() ANDROID_API;
  79. // starts SurfaceFlinger main loop in the current thread
  80. void run() ANDROID_API;
  81. enum {
  82. EVENT_VSYNC = HWC_EVENT_VSYNC
  83. };
  84. // post an asynchronous message to the main thread
  85. status_t postMessageAsync(const sp<MessageBase>& msg, nsecs_t reltime = 0, uint32_t flags = 0);
  86. // post a synchronous message to the main thread
  87. status_t postMessageSync(const sp<MessageBase>& msg, nsecs_t reltime = 0, uint32_t flags = 0);
  88. // force full composition on all displays
  89. void repaintEverything();
  90. // returns the default Display
  91. sp<const DisplayDevice> getDefaultDisplayDevice() const {
  92. return getDisplayDevice(mBuiltinDisplays[DisplayDevice::DISPLAY_PRIMARY]);
  93. }
  94. // utility function to delete a texture on the main thread
  95. void deleteTextureAsync(uint32_t texture);
  96. // enable/disable h/w composer event
  97. // TODO: this should be made accessible only to EventThread
  98. void eventControl(int disp, int event, int enabled);
  99. // called on the main thread by MessageQueue when an internal message
  100. // is received
  101. // TODO: this should be made accessible only to MessageQueue
  102. void onMessageReceived(int32_t what);
  103. // for debugging only
  104. // TODO: this should be made accessible only to HWComposer
  105. const Vector< sp<Layer> >& getLayerSortedByZForHwcDisplay(int id);
  106. RenderEngine& getRenderEngine() const {
  107. return *mRenderEngine;
  108. }
  109. private:
  110. friend class Client;
  111. friend class DisplayEventConnection;
  112. friend class Layer;
  113. friend class LayerDim;
  114. #ifdef QTI_BSP
  115. friend class ExLayer;
  116. #endif
  117. friend class MonitoredProducer;
  118. friend class LayerBlur;
  119. // This value is specified in number of frames. Log frame stats at most
  120. // every half hour.
  121. enum { LOG_FRAME_STATS_PERIOD = 30*60*60 };
  122. static const size_t MAX_LAYERS = 4096;
  123. // We're reference counted, never destroy SurfaceFlinger directly
  124. virtual ~SurfaceFlinger();
  125. /* ------------------------------------------------------------------------
  126. * Internal data structures
  127. */
  128. class LayerVector : public SortedVector< sp<Layer> > {
  129. public:
  130. LayerVector();
  131. LayerVector(const LayerVector& rhs);
  132. virtual int do_compare(const void* lhs, const void* rhs) const;
  133. };
  134. struct DisplayDeviceState {
  135. DisplayDeviceState();
  136. DisplayDeviceState(DisplayDevice::DisplayType type);
  137. bool isValid() const { return type >= 0; }
  138. bool isMainDisplay() const { return type == DisplayDevice::DISPLAY_PRIMARY; }
  139. bool isVirtualDisplay() const { return type >= DisplayDevice::DISPLAY_VIRTUAL; }
  140. DisplayDevice::DisplayType type;
  141. sp<IGraphicBufferProducer> surface;
  142. uint32_t layerStack;
  143. Rect viewport;
  144. Rect frame;
  145. uint8_t orientation;
  146. uint32_t width, height;
  147. String8 displayName;
  148. bool isSecure;
  149. };
  150. struct State {
  151. LayerVector layersSortedByZ;
  152. DefaultKeyedVector< wp<IBinder>, DisplayDeviceState> displays;
  153. };
  154. /* ------------------------------------------------------------------------
  155. * IBinder interface
  156. */
  157. virtual status_t onTransact(uint32_t code, const Parcel& data,
  158. Parcel* reply, uint32_t flags);
  159. virtual status_t dump(int fd, const Vector<String16>& args);
  160. /* ------------------------------------------------------------------------
  161. * ISurfaceComposer interface
  162. */
  163. virtual sp<ISurfaceComposerClient> createConnection();
  164. virtual sp<IGraphicBufferAlloc> createGraphicBufferAlloc();
  165. virtual sp<IBinder> createDisplay(const String8& displayName, bool secure);
  166. virtual void destroyDisplay(const sp<IBinder>& display);
  167. virtual sp<IBinder> getBuiltInDisplay(int32_t id);
  168. virtual void setTransactionState(const Vector<ComposerState>& state,
  169. const Vector<DisplayState>& displays, uint32_t flags);
  170. virtual void bootFinished();
  171. virtual bool authenticateSurfaceTexture(
  172. const sp<IGraphicBufferProducer>& bufferProducer) const;
  173. virtual sp<IDisplayEventConnection> createDisplayEventConnection();
  174. virtual status_t captureScreen(const sp<IBinder>& display,
  175. const sp<IGraphicBufferProducer>& producer,
  176. Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
  177. uint32_t minLayerZ, uint32_t maxLayerZ,
  178. bool useIdentityTransform, ISurfaceComposer::Rotation rotation,
  179. bool isCpuConsumer);
  180. virtual status_t getDisplayStats(const sp<IBinder>& display,
  181. DisplayStatInfo* stats);
  182. virtual status_t getDisplayConfigs(const sp<IBinder>& display,
  183. Vector<DisplayInfo>* configs);
  184. virtual int getActiveConfig(const sp<IBinder>& display);
  185. virtual void setPowerMode(const sp<IBinder>& display, int mode);
  186. virtual status_t setActiveConfig(const sp<IBinder>& display, int id);
  187. virtual status_t clearAnimationFrameStats();
  188. virtual status_t getAnimationFrameStats(FrameStats* outStats) const;
  189. /* ------------------------------------------------------------------------
  190. * DeathRecipient interface
  191. */
  192. virtual void binderDied(const wp<IBinder>& who);
  193. /* ------------------------------------------------------------------------
  194. * RefBase interface
  195. */
  196. virtual void onFirstRef();
  197. /* ------------------------------------------------------------------------
  198. * HWComposer::EventHandler interface
  199. */
  200. virtual void onVSyncReceived(int type, nsecs_t timestamp);
  201. virtual void onHotplugReceived(int disp, bool connected);
  202. /* ------------------------------------------------------------------------
  203. * Extensions
  204. */
  205. virtual void updateExtendedMode() { }
  206. virtual void getIndexLOI(size_t /*dpy*/,
  207. const LayerVector& /*currentLayers*/,
  208. bool& /*bIgnoreLayers*/,
  209. int& /*indexLOI*/) { }
  210. virtual bool updateLayerVisibleNonTransparentRegion(
  211. const int& dpy, const sp<Layer>& layer,
  212. bool& bIgnoreLayers, int& indexLOI,
  213. uint32_t layerStack, const int& i);
  214. virtual void delayDPTransactionIfNeeded(
  215. const Vector<DisplayState>& /*displays*/) { }
  216. virtual bool canDrawLayerinScreenShot(
  217. const sp<const DisplayDevice>& hw,
  218. const sp<Layer>& layer);
  219. virtual void isfreezeSurfacePresent(
  220. bool& freezeSurfacePresent,
  221. const sp<const DisplayDevice>& /*hw*/,
  222. const int32_t& /*id*/) { freezeSurfacePresent = false; }
  223. virtual void setOrientationEventControl(
  224. bool& /*freezeSurfacePresent*/,
  225. const int32_t& /*id*/) { }
  226. virtual void updateVisibleRegionsDirty() { }
  227. virtual void drawWormHoleIfRequired(HWComposer::LayerListIterator &cur,
  228. const HWComposer::LayerListIterator &end,
  229. const sp<const DisplayDevice>& hw,
  230. const Region& region);
  231. /* ------------------------------------------------------------------------
  232. * Message handling
  233. */
  234. void waitForEvent();
  235. void signalTransaction();
  236. void signalLayerUpdate();
  237. void signalRefresh();
  238. // called on the main thread in response to initializeDisplays()
  239. void onInitializeDisplays();
  240. // called on the main thread in response to setActiveConfig()
  241. void setActiveConfigInternal(const sp<DisplayDevice>& hw, int mode);
  242. // called on the main thread in response to setPowerMode()
  243. void setPowerModeInternal(const sp<DisplayDevice>& hw, int mode);
  244. // Returns whether the transaction actually modified any state
  245. bool handleMessageTransaction();
  246. // Returns whether a new buffer has been latched (see handlePageFlip())
  247. bool handleMessageInvalidate();
  248. void handleMessageRefresh();
  249. void handleTransaction(uint32_t transactionFlags);
  250. void handleTransactionLocked(uint32_t transactionFlags);
  251. void updateCursorAsync();
  252. /* handlePageFlip - latch a new buffer if available and compute the dirty
  253. * region. Returns whether a new buffer has been latched, i.e., whether it
  254. * is necessary to perform a refresh during this vsync.
  255. */
  256. bool handlePageFlip();
  257. /* ------------------------------------------------------------------------
  258. * Transactions
  259. */
  260. uint32_t getTransactionFlags(uint32_t flags);
  261. uint32_t peekTransactionFlags(uint32_t flags);
  262. uint32_t setTransactionFlags(uint32_t flags);
  263. void commitTransaction();
  264. uint32_t setClientStateLocked(const sp<Client>& client, const layer_state_t& s);
  265. uint32_t setDisplayStateLocked(const DisplayState& s);
  266. /* ------------------------------------------------------------------------
  267. * Layer management
  268. */
  269. status_t createLayer(const String8& name, const sp<Client>& client,
  270. uint32_t w, uint32_t h, PixelFormat format, uint32_t flags,
  271. sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp);
  272. status_t createNormalLayer(const sp<Client>& client, const String8& name,
  273. uint32_t w, uint32_t h, uint32_t flags, PixelFormat& format,
  274. sp<IBinder>* outHandle, sp<IGraphicBufferProducer>* outGbp,
  275. sp<Layer>* outLayer);
  276. status_t createDimLayer(const sp<Client>& client, const String8& name,
  277. uint32_t w, uint32_t h, uint32_t flags, sp<IBinder>* outHandle,
  278. sp<IGraphicBufferProducer>* outGbp, sp<Layer>* outLayer);
  279. status_t createBlurLayer(const sp<Client>& client, const String8& name,
  280. uint32_t w, uint32_t h, uint32_t flags, sp<IBinder>* outHandle,
  281. sp<IGraphicBufferProducer>* outGbp, sp<Layer>* outLayer);
  282. // called in response to the window-manager calling
  283. // ISurfaceComposerClient::destroySurface()
  284. status_t onLayerRemoved(const sp<Client>& client, const sp<IBinder>& handle);
  285. // called when all clients have released all their references to
  286. // this layer meaning it is entirely safe to destroy all
  287. // resources associated to this layer.
  288. status_t onLayerDestroyed(const wp<Layer>& layer);
  289. // remove a layer from SurfaceFlinger immediately
  290. status_t removeLayer(const sp<Layer>& layer);
  291. // add a layer to SurfaceFlinger
  292. status_t addClientLayer(const sp<Client>& client,
  293. const sp<IBinder>& handle,
  294. const sp<IGraphicBufferProducer>& gbc,
  295. const sp<Layer>& lbc);
  296. /* ------------------------------------------------------------------------
  297. * Boot animation, on/off animations and screen capture
  298. */
  299. void startBootAnim();
  300. void renderScreenImplLocked(
  301. const sp<const DisplayDevice>& hw,
  302. Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
  303. uint32_t minLayerZ, uint32_t maxLayerZ,
  304. bool yswap, bool useIdentityTransform, Transform::orientation_flags rotation);
  305. status_t captureScreenImplLocked(
  306. const sp<const DisplayDevice>& hw,
  307. const sp<IGraphicBufferProducer>& producer,
  308. Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
  309. uint32_t minLayerZ, uint32_t maxLayerZ,
  310. bool useIdentityTransform, Transform::orientation_flags rotation,
  311. bool useReadPixels);
  312. /* ------------------------------------------------------------------------
  313. * EGL
  314. */
  315. size_t getMaxTextureSize() const;
  316. size_t getMaxViewportDims() const;
  317. /* ------------------------------------------------------------------------
  318. * Display and layer stack management
  319. */
  320. // called when starting, or restarting after system_server death
  321. void initializeDisplays();
  322. // Create an IBinder for a builtin display and add it to current state
  323. void createBuiltinDisplayLocked(DisplayDevice::DisplayType type);
  324. // NOTE: can only be called from the main thread or with mStateLock held
  325. sp<const DisplayDevice> getDisplayDevice(const wp<IBinder>& dpy) const {
  326. return mDisplays.valueFor(dpy);
  327. }
  328. // NOTE: can only be called from the main thread or with mStateLock held
  329. sp<DisplayDevice> getDisplayDevice(const wp<IBinder>& dpy) {
  330. return mDisplays.valueFor(dpy);
  331. }
  332. // mark a region of a layer stack dirty. this updates the dirty
  333. // region of all screens presenting this layer stack.
  334. void invalidateLayerStack(uint32_t layerStack, const Region& dirty);
  335. // allocate a h/w composer display id
  336. int32_t allocateHwcDisplayId(DisplayDevice::DisplayType type);
  337. /* ------------------------------------------------------------------------
  338. * H/W composer
  339. */
  340. HWComposer& getHwComposer() const { return *mHwc; }
  341. /* ------------------------------------------------------------------------
  342. * Compositing
  343. */
  344. void invalidateHwcGeometry();
  345. void computeVisibleRegions(size_t dpy,
  346. const LayerVector& currentLayers, uint32_t layerStack,
  347. Region& dirtyRegion, Region& opaqueRegion);
  348. void preComposition();
  349. void postComposition();
  350. void rebuildLayerStacks();
  351. void setUpHWComposer();
  352. void doComposition();
  353. void doDebugFlashRegions();
  354. void doDisplayComposition(const sp<const DisplayDevice>& hw, const Region& dirtyRegion);
  355. // compose surfaces for display hw. this fails if using GL and the surface
  356. // has been destroyed and is no longer valid.
  357. bool doComposeSurfaces(const sp<const DisplayDevice>& hw, const Region& dirty);
  358. void postFramebuffer();
  359. void drawWormhole(const sp<const DisplayDevice>& hw, const Region& region) const;
  360. /* ------------------------------------------------------------------------
  361. * Display management
  362. */
  363. /* ------------------------------------------------------------------------
  364. * VSync
  365. */
  366. void enableHardwareVsync();
  367. void disableHardwareVsync(bool makeUnavailable);
  368. void resyncToHardwareVsync(bool makeAvailable);
  369. /* ------------------------------------------------------------------------
  370. * Debugging & dumpsys
  371. */
  372. void listLayersLocked(const Vector<String16>& args, size_t& index, String8& result) const;
  373. void dumpStatsLocked(const Vector<String16>& args, size_t& index, String8& result) const;
  374. void clearStatsLocked(const Vector<String16>& args, size_t& index, String8& result);
  375. void dumpAllLocked(const Vector<String16>& args, size_t& index, String8& result) const;
  376. bool startDdmConnection();
  377. static void appendSfConfigString(String8& result);
  378. void checkScreenshot(size_t w, size_t s, size_t h, void const* vaddr,
  379. const sp<const DisplayDevice>& hw,
  380. uint32_t minLayerZ, uint32_t maxLayerZ);
  381. void logFrameStats();
  382. void dumpStaticScreenStats(String8& result) const;
  383. virtual void dumpDrawCycle(bool /* prePrepare */ ) { }
  384. /* ------------------------------------------------------------------------
  385. * Attributes
  386. */
  387. // access must be protected by mStateLock
  388. mutable Mutex mStateLock;
  389. State mCurrentState;
  390. volatile int32_t mTransactionFlags;
  391. Condition mTransactionCV;
  392. bool mTransactionPending;
  393. bool mAnimTransactionPending;
  394. Vector< sp<Layer> > mLayersPendingRemoval;
  395. SortedVector< wp<IBinder> > mGraphicBufferProducerList;
  396. // protected by mStateLock (but we could use another lock)
  397. bool mLayersRemoved;
  398. // access must be protected by mInvalidateLock
  399. volatile int32_t mRepaintEverything;
  400. // constant members (no synchronization needed for access)
  401. HWComposer* mHwc;
  402. RenderEngine* mRenderEngine;
  403. nsecs_t mBootTime;
  404. bool mGpuToCpuSupported;
  405. bool mDropMissedFrames;
  406. sp<EventThread> mEventThread;
  407. sp<EventThread> mSFEventThread;
  408. sp<EventControlThread> mEventControlThread;
  409. EGLContext mEGLContext;
  410. EGLDisplay mEGLDisplay;
  411. sp<IBinder> mBuiltinDisplays[DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES];
  412. // Can only accessed from the main thread, these members
  413. // don't need synchronization
  414. State mDrawingState;
  415. bool mVisibleRegionsDirty;
  416. bool mHwWorkListDirty;
  417. bool mAnimCompositionPending;
  418. // this may only be written from the main thread with mStateLock held
  419. // it may be read from other threads with mStateLock held
  420. DefaultKeyedVector< wp<IBinder>, sp<DisplayDevice> > mDisplays;
  421. // don't use a lock for these, we don't care
  422. int mDebugRegion;
  423. int mDebugDDMS;
  424. int mDebugDisableHWC;
  425. int mDebugDisableTransformHint;
  426. volatile nsecs_t mDebugInSwapBuffers;
  427. nsecs_t mLastSwapBufferTime;
  428. volatile nsecs_t mDebugInTransaction;
  429. nsecs_t mLastTransactionTime;
  430. bool mBootFinished;
  431. bool mForceFullDamage;
  432. // these are thread safe
  433. mutable MessageQueue mEventQueue;
  434. FrameTracker mAnimFrameTracker;
  435. DispSync mPrimaryDispSync;
  436. // protected by mDestroyedLayerLock;
  437. mutable Mutex mDestroyedLayerLock;
  438. Vector<Layer const *> mDestroyedLayers;
  439. // protected by mHWVsyncLock
  440. Mutex mHWVsyncLock;
  441. bool mPrimaryHWVsyncEnabled;
  442. bool mHWVsyncAvailable;
  443. /* ------------------------------------------------------------------------
  444. * Feature prototyping
  445. */
  446. Daltonizer mDaltonizer;
  447. bool mDaltonize;
  448. mat4 mColorMatrix;
  449. bool mHasColorMatrix;
  450. mat4 mSecondaryColorMatrix;
  451. bool mHasSecondaryColorMatrix;
  452. // Static screen stats
  453. bool mHasPoweredOff;
  454. static const size_t NUM_BUCKETS = 8; // < 1-7, 7+
  455. nsecs_t mFrameBuckets[NUM_BUCKETS];
  456. nsecs_t mTotalTime;
  457. nsecs_t mLastSwapTime;
  458. FrameRateHelper mFrameRateHelper;
  459. /*
  460. * A number that increases on every new frame composition and screen capture.
  461. * LayerBlur can speed up it's drawing by caching texture using this variable
  462. * if multiple LayerBlur objects draw in one frame composition.
  463. * In case of display mirroring, this variable should be increased on every display.
  464. */
  465. uint32_t mActiveFrameSequence;
  466. };
  467. }; // namespace android
  468. #endif // ANDROID_SURFACE_FLINGER_H