CompositorWidget.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. #ifndef mozilla_widget_CompositorWidget_h__
  5. #define mozilla_widget_CompositorWidget_h__
  6. #include "nsISupports.h"
  7. #include "mozilla/RefPtr.h"
  8. #include "Units.h"
  9. #include "mozilla/gfx/2D.h"
  10. #include "mozilla/layers/LayersTypes.h"
  11. class nsIWidget;
  12. class nsBaseWidget;
  13. namespace mozilla {
  14. class VsyncObserver;
  15. namespace layers {
  16. class Compositor;
  17. class LayerManagerComposite;
  18. class Compositor;
  19. class Composer2D;
  20. } // namespace layers
  21. namespace gfx {
  22. class DrawTarget;
  23. class SourceSurface;
  24. } // namespace gfx
  25. namespace widget {
  26. class WinCompositorWidget;
  27. class X11CompositorWidget;
  28. class CompositorWidgetInitData;
  29. // Gecko widgets usually need to communicate with the CompositorWidget with
  30. // platform-specific messages (for example to update the window size or
  31. // transparency). This functionality is controlled through a "host". Since
  32. // this functionality is platform-dependent, it is only forward declared
  33. // here.
  34. class CompositorWidgetDelegate;
  35. // Platforms that support out-of-process widgets.
  36. #if defined(XP_WIN) || defined(MOZ_X11)
  37. // CompositorWidgetParent should implement CompositorWidget and
  38. // PCompositorWidgetParent.
  39. class CompositorWidgetParent;
  40. // CompositorWidgetChild should implement CompositorWidgetDelegate and
  41. // PCompositorWidgetChild.
  42. class CompositorWidgetChild;
  43. # define MOZ_WIDGET_SUPPORTS_OOP_COMPOSITING
  44. #endif
  45. class WidgetRenderingContext
  46. {
  47. /** Mac Stub **/
  48. };
  49. /**
  50. * Access to a widget from the compositor is restricted to these methods.
  51. */
  52. class CompositorWidget
  53. {
  54. public:
  55. NS_INLINE_DECL_REFCOUNTING(mozilla::widget::CompositorWidget)
  56. /**
  57. * Create an in-process compositor widget. aWidget may be ignored if the
  58. * platform does not require it.
  59. */
  60. static RefPtr<CompositorWidget> CreateLocal(const CompositorWidgetInitData& aInitData, nsIWidget* aWidget);
  61. /**
  62. * Called before rendering using OMTC. Returns false when the widget is
  63. * not ready to be rendered (for example while the window is closed).
  64. *
  65. * Always called from the compositing thread, which may be the main-thread if
  66. * OMTC is not enabled.
  67. */
  68. virtual bool PreRender(WidgetRenderingContext* aContext) {
  69. return true;
  70. }
  71. /**
  72. * Called after rendering using OMTC. Not called when rendering was
  73. * cancelled by a negative return value from PreRender.
  74. *
  75. * Always called from the compositing thread, which may be the main-thread if
  76. * OMTC is not enabled.
  77. */
  78. virtual void PostRender(WidgetRenderingContext* aContext)
  79. {}
  80. /**
  81. * Called before the LayerManager draws the layer tree.
  82. *
  83. * Always called from the compositing thread.
  84. */
  85. virtual void DrawWindowUnderlay(WidgetRenderingContext* aContext,
  86. LayoutDeviceIntRect aRect)
  87. {}
  88. /**
  89. * Called after the LayerManager draws the layer tree
  90. *
  91. * Always called from the compositing thread.
  92. */
  93. virtual void DrawWindowOverlay(WidgetRenderingContext* aContext,
  94. LayoutDeviceIntRect aRect)
  95. {}
  96. /**
  97. * Return a DrawTarget for the window which can be composited into.
  98. *
  99. * Called by BasicCompositor on the compositor thread for OMTC drawing
  100. * before each composition.
  101. *
  102. * The window may specify its buffer mode. If unspecified, it is assumed
  103. * to require double-buffering.
  104. */
  105. virtual already_AddRefed<gfx::DrawTarget> StartRemoteDrawing();
  106. virtual already_AddRefed<gfx::DrawTarget>
  107. StartRemoteDrawingInRegion(LayoutDeviceIntRegion& aInvalidRegion,
  108. layers::BufferMode* aBufferMode)
  109. {
  110. return StartRemoteDrawing();
  111. }
  112. /**
  113. * Ensure that what was painted into the DrawTarget returned from
  114. * StartRemoteDrawing reaches the screen.
  115. *
  116. * Called by BasicCompositor on the compositor thread for OMTC drawing
  117. * after each composition.
  118. */
  119. virtual void EndRemoteDrawing()
  120. {}
  121. virtual void EndRemoteDrawingInRegion(gfx::DrawTarget* aDrawTarget,
  122. LayoutDeviceIntRegion& aInvalidRegion)
  123. {
  124. EndRemoteDrawing();
  125. }
  126. /**
  127. * Return true when it is better to defer EndRemoteDrawing().
  128. *
  129. * Called by BasicCompositor on the compositor thread for OMTC drawing
  130. * after each composition.
  131. */
  132. virtual bool NeedsToDeferEndRemoteDrawing() {
  133. return false;
  134. }
  135. /**
  136. * Called when shutting down the LayerManager to clean-up any cached resources.
  137. *
  138. * Always called from the compositing thread.
  139. */
  140. virtual void CleanupWindowEffects()
  141. {}
  142. /**
  143. * A hook for the widget to prepare a Compositor, during the latter's initialization.
  144. *
  145. * If this method returns true, it means that the widget will be able to
  146. * present frames from the compoositor.
  147. *
  148. * Returning false will cause the compositor's initialization to fail, and
  149. * a different compositor backend will be used (if any).
  150. */
  151. virtual bool InitCompositor(layers::Compositor* aCompositor) {
  152. return true;
  153. }
  154. /**
  155. * Return the size of the drawable area of the widget.
  156. */
  157. virtual LayoutDeviceIntSize GetClientSize() = 0;
  158. /**
  159. * Return the internal format of the default framebuffer for this
  160. * widget.
  161. */
  162. virtual uint32_t GetGLFrameBufferFormat();
  163. /**
  164. * If this widget has a more efficient composer available for its
  165. * native framebuffer, return it.
  166. *
  167. * This can be called from a non-main thread, but that thread must
  168. * hold a strong reference to this.
  169. */
  170. virtual layers::Composer2D* GetComposer2D() {
  171. return nullptr;
  172. }
  173. /*
  174. * Access the underlying nsIWidget. This method will be removed when the compositor no longer
  175. * depends on nsIWidget on any platform.
  176. */
  177. virtual nsIWidget* RealWidget() = 0;
  178. /**
  179. * Clean up any resources used by Start/EndRemoteDrawing.
  180. *
  181. * Called by BasicCompositor on the compositor thread for OMTC drawing
  182. * when the compositor is destroyed.
  183. */
  184. virtual void CleanupRemoteDrawing();
  185. /**
  186. * Return a key that can represent the widget object round-trip across the
  187. * CompositorBridge channel. This only needs to be implemented on GTK and
  188. * Windows.
  189. *
  190. * The key must be the nsIWidget pointer cast to a uintptr_t. See
  191. * CompositorBridgeChild::RecvHideAllPlugins and
  192. * CompositorBridgeParent::SendHideAllPlugins.
  193. */
  194. virtual uintptr_t GetWidgetKey() {
  195. return 0;
  196. }
  197. /**
  198. * Create a backbuffer for the software compositor.
  199. */
  200. virtual already_AddRefed<gfx::DrawTarget>
  201. GetBackBufferDrawTarget(gfx::DrawTarget* aScreenTarget,
  202. const LayoutDeviceIntRect& aRect,
  203. const LayoutDeviceIntRect& aClearRect);
  204. /**
  205. * Ensure end of composition to back buffer.
  206. *
  207. * Called by BasicCompositor on the compositor thread for OMTC drawing
  208. * after each composition to back buffer.
  209. */
  210. virtual already_AddRefed<gfx::SourceSurface> EndBackBufferDrawing();
  211. /**
  212. * Observe or unobserve vsync.
  213. */
  214. virtual void ObserveVsync(VsyncObserver* aObserver) = 0;
  215. /**
  216. * This is only used by out-of-process compositors.
  217. */
  218. virtual RefPtr<VsyncObserver> GetVsyncObserver() const;
  219. virtual WinCompositorWidget* AsWindows() {
  220. return nullptr;
  221. }
  222. virtual X11CompositorWidget* AsX11() {
  223. return nullptr;
  224. }
  225. /**
  226. * Return the platform-specific delegate for the widget, if any.
  227. */
  228. virtual CompositorWidgetDelegate* AsDelegate() {
  229. return nullptr;
  230. }
  231. protected:
  232. virtual ~CompositorWidget();
  233. // Back buffer of BasicCompositor
  234. RefPtr<gfx::DrawTarget> mLastBackBuffer;
  235. };
  236. } // namespace widget
  237. } // namespace mozilla
  238. #endif