gl_surface.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef UI_GL_GL_SURFACE_H_
  5. #define UI_GL_GL_SURFACE_H_
  6. #include <vector>
  7. #include "base/callback.h"
  8. #include "base/memory/ref_counted.h"
  9. #include "base/memory/weak_ptr.h"
  10. #include "build/build_config.h"
  11. #include "mojo/public/cpp/bindings/pending_receiver.h"
  12. #include "third_party/abseil-cpp/absl/types/optional.h"
  13. #include "ui/gfx/delegated_ink_metadata.h"
  14. #include "ui/gfx/geometry/rect.h"
  15. #include "ui/gfx/geometry/rect_f.h"
  16. #include "ui/gfx/geometry/size.h"
  17. #include "ui/gfx/geometry/vector2d.h"
  18. #include "ui/gfx/native_widget_types.h"
  19. #include "ui/gfx/overlay_priority_hint.h"
  20. #include "ui/gfx/overlay_transform.h"
  21. #include "ui/gfx/presentation_feedback.h"
  22. #include "ui/gfx/surface_origin.h"
  23. #include "ui/gfx/swap_result.h"
  24. #include "ui/gl/gl_display.h"
  25. #include "ui/gl/gl_export.h"
  26. #include "ui/gl/gl_implementation.h"
  27. #include "ui/gl/gl_surface_format.h"
  28. #include "ui/gl/gpu_preference.h"
  29. namespace gfx {
  30. namespace mojom {
  31. class DelegatedInkPointRenderer;
  32. } // namespace mojom
  33. class ColorSpace;
  34. struct OverlayPlaneData;
  35. class GpuFence;
  36. class VSyncProvider;
  37. } // namespace gfx
  38. namespace ui {
  39. struct CARendererLayerParams;
  40. struct DCRendererLayerParams;
  41. } // namespace ui
  42. namespace gl {
  43. class GLContext;
  44. class GLImage;
  45. class EGLTimestampClient;
  46. // Encapsulates a surface that can be rendered to with GL, hiding platform
  47. // specific management.
  48. class GL_EXPORT GLSurface : public base::RefCounted<GLSurface>,
  49. public base::SupportsWeakPtr<GLSurface> {
  50. public:
  51. GLSurface();
  52. GLSurface(const GLSurface&) = delete;
  53. GLSurface& operator=(const GLSurface&) = delete;
  54. // Non-virtual initialization, this always calls Initialize with a
  55. // default GLSurfaceFormat. Subclasses should override the format-
  56. // specific Initialize method below and interpret the default format
  57. // as appropriate.
  58. bool Initialize();
  59. // (Re)create the surface. TODO(apatrick): This is an ugly hack to allow the
  60. // EGL surface associated to be recreated without destroying the associated
  61. // context. The implementation of this function for other GLSurface derived
  62. // classes is in a pending changelist.
  63. virtual bool Initialize(GLSurfaceFormat format);
  64. // Destroys the surface.
  65. virtual void Destroy() = 0;
  66. // Some implementations (macOS), in Destroy, will need to delete GL objects
  67. // that exist in the current GL context. This method is called before the
  68. // context's decoder (and potentially context itself) are destroyed, giving an
  69. // opportunity for this cleanup.
  70. virtual void PrepareToDestroy(bool have_context);
  71. // Resizes the surface, returning success. If failed, it is possible that the
  72. // context is no longer current.
  73. virtual bool Resize(const gfx::Size& size,
  74. float scale_factor,
  75. const gfx::ColorSpace& color_space,
  76. bool has_alpha);
  77. // Recreate the surface without changing the size, returning success. If
  78. // failed, it is possible that the context is no longer current.
  79. virtual bool Recreate();
  80. // Unschedule the CommandExecutor and return true to abort the processing of
  81. // a GL draw call to this surface and defer it until the CommandExecutor is
  82. // rescheduled.
  83. virtual bool DeferDraws();
  84. // Returns true if this surface is offscreen.
  85. virtual bool IsOffscreen() = 0;
  86. // The callback is for receiving presentation feedback from |SwapBuffers|,
  87. // |PostSubBuffer|, |CommitOverlayPlanes|, etc.
  88. // See |PresentationFeedback| for detail.
  89. using PresentationCallback =
  90. base::OnceCallback<void(const gfx::PresentationFeedback& feedback)>;
  91. // Swaps front and back buffers. This has no effect for off-screen
  92. // contexts. If it returns SWAP_FAILED, it is possible that the context is no
  93. // longer current.
  94. virtual gfx::SwapResult SwapBuffers(PresentationCallback callback) = 0;
  95. // Get the size of the surface.
  96. virtual gfx::Size GetSize() = 0;
  97. // Get the underlying platform specific surface "handle".
  98. virtual void* GetHandle() = 0;
  99. // Android SurfaceControl specific, notifies that we should not detach child
  100. // surface controls during destruction.
  101. virtual void PreserveChildSurfaceControls() {}
  102. // Returns whether or not the surface supports SwapBuffersWithBounds
  103. virtual bool SupportsSwapBuffersWithBounds();
  104. // Returns whether or not the surface supports PostSubBuffer.
  105. virtual bool SupportsPostSubBuffer();
  106. // Returns whether or not the surface supports CommitOverlayPlanes.
  107. virtual bool SupportsCommitOverlayPlanes();
  108. // Returns whether SwapBuffersAsync() is supported.
  109. virtual bool SupportsAsyncSwap();
  110. // Returns the internal frame buffer object name if the surface is backed by
  111. // FBO. Otherwise returns 0.
  112. virtual unsigned int GetBackingFramebufferObject();
  113. // The SwapCompletionCallback is used to receive notification about the
  114. // completion of the swap operation from |SwapBuffersAsync|,
  115. // |PostSubBufferAsync|, |CommitOverlayPlanesAsync|, etc. If a null gpu fence
  116. // is returned, then the swap is guaranteed to have already completed. If a
  117. // non-null gpu fence is returned, then the swap operation may still be in
  118. // progress when this callback is invoked, and the signaling of the gpu fence
  119. // will mark the completion of the swap operation.
  120. using SwapCompletionCallback =
  121. base::OnceCallback<void(gfx::SwapCompletionResult)>;
  122. // Swaps front and back buffers. This has no effect for off-screen
  123. // contexts. On some platforms, we want to send SwapBufferAck only after the
  124. // surface is displayed on screen. The callback can be used to delay sending
  125. // SwapBufferAck till that data is available. The callback should be run on
  126. // the calling thread (i.e. same thread SwapBuffersAsync is called)
  127. virtual void SwapBuffersAsync(SwapCompletionCallback completion_callback,
  128. PresentationCallback presentation_callback);
  129. // Swap buffers with content bounds. If it returns SWAP_FAILED, it is possible
  130. // that the context is no longer current.
  131. virtual gfx::SwapResult SwapBuffersWithBounds(
  132. const std::vector<gfx::Rect>& rects,
  133. PresentationCallback callback);
  134. // Copy part of the backbuffer to the frontbuffer. If it returns SWAP_FAILED,
  135. // it is possible that the context is no longer current.
  136. virtual gfx::SwapResult PostSubBuffer(int x,
  137. int y,
  138. int width,
  139. int height,
  140. PresentationCallback callback);
  141. // Copy part of the backbuffer to the frontbuffer. On some platforms, we want
  142. // to send SwapBufferAck only after the surface is displayed on screen. The
  143. // callback can be used to delay sending SwapBufferAck till that data is
  144. // available. The callback should be run on the calling thread (i.e. same
  145. // thread PostSubBufferAsync is called)
  146. virtual void PostSubBufferAsync(int x,
  147. int y,
  148. int width,
  149. int height,
  150. SwapCompletionCallback completion_callback,
  151. PresentationCallback presentation_callback);
  152. // Show overlay planes but don't swap the front and back buffers. This acts
  153. // like SwapBuffers from the point of view of the client, but is cheaper when
  154. // overlays account for all the damage. If it returns SWAP_FAILED,
  155. // it is possible that the context is no longer current.
  156. virtual gfx::SwapResult CommitOverlayPlanes(PresentationCallback callback);
  157. // Show overlay planes but don't swap the front and back buffers. On some
  158. // platforms, we want to send SwapBufferAck only after the overlays are
  159. // displayed on screen. The callback can be used to delay sending
  160. // SwapBufferAck till that data is available. The callback should be run on
  161. // the calling thread (i.e. same thread CommitOverlayPlanesAsync is called).
  162. virtual void CommitOverlayPlanesAsync(
  163. SwapCompletionCallback completion_callback,
  164. PresentationCallback presentation_callback);
  165. // Called after a context is made current with this surface. Returns false
  166. // on error.
  167. virtual bool OnMakeCurrent(GLContext* context);
  168. // Used for explicit buffer management.
  169. virtual bool SetBackbufferAllocation(bool allocated);
  170. virtual void SetFrontbufferAllocation(bool allocated);
  171. // Get a handle used to share the surface with another process. Returns null
  172. // if this is not possible.
  173. virtual void* GetShareHandle();
  174. // Get the platform specific display on which this surface resides, if
  175. // available.
  176. virtual GLDisplay* GetGLDisplay();
  177. // Get the platfrom specific configuration for this surface, if available.
  178. virtual void* GetConfig();
  179. // Get the GL pixel format of the surface. Must be implemented in a
  180. // subclass, though it's ok to just "return GLSurfaceFormat()" if
  181. // the default is appropriate.
  182. virtual GLSurfaceFormat GetFormat() = 0;
  183. // Get access to a helper providing time of recent refresh and period
  184. // of screen refresh. If unavailable, returns NULL.
  185. virtual gfx::VSyncProvider* GetVSyncProvider();
  186. // Set vsync to enabled or disabled. If supported, vsync is enabled by
  187. // default. Does nothing if vsync cannot be changed.
  188. virtual void SetVSyncEnabled(bool enabled);
  189. // Schedule an overlay plane to be shown at swap time, or on the next
  190. // CommitOverlayPlanes call.
  191. // |image| to be presented by the overlay.
  192. // |bounds_rect| specify where it is supposed to be on the screen in pixels.
  193. // |overlay_plane_data| specifies overlay data such as opacity, z_order, size,
  194. // etc.
  195. virtual bool ScheduleOverlayPlane(
  196. GLImage* image,
  197. std::unique_ptr<gfx::GpuFence> gpu_fence,
  198. const gfx::OverlayPlaneData& overlay_plane_data);
  199. // Schedule a CALayer to be shown at swap time.
  200. // All arguments correspond to their CALayer properties.
  201. virtual bool ScheduleCALayer(const ui::CARendererLayerParams& params);
  202. virtual bool ScheduleDCLayer(
  203. std::unique_ptr<ui::DCRendererLayerParams> params);
  204. // Enables or disables DC layers, returning success. If failed, it is possible
  205. // that the context is no longer current.
  206. virtual bool SetEnableDCLayers(bool enable);
  207. virtual bool IsSurfaceless() const;
  208. // Returns true if this surface permits scheduling an isothetic sub-rectangle
  209. // (i.e. viewport) of its contents for display.
  210. virtual bool SupportsViewporter() const;
  211. virtual gfx::SurfaceOrigin GetOrigin() const;
  212. // Returns true if SwapBuffers or PostSubBuffers causes a flip, such that
  213. // the next buffer may be 2 frames old.
  214. virtual bool BuffersFlipped() const;
  215. virtual bool SupportsDCLayers() const;
  216. virtual bool SupportsProtectedVideo() const;
  217. // Returns true if we are allowed to adopt a size different from the
  218. // platform's proposed surface size.
  219. virtual bool SupportsOverridePlatformSize() const;
  220. // Set the rectangle that will be drawn into on the surface, returning
  221. // success. If failed, it is possible that the context is no longer current.
  222. virtual bool SetDrawRectangle(const gfx::Rect& rect);
  223. // This is the amount by which the scissor and viewport rectangles should be
  224. // offset.
  225. virtual gfx::Vector2d GetDrawOffset() const;
  226. // Tells the surface to rely on implicit sync when swapping buffers.
  227. virtual void SetRelyOnImplicitSync();
  228. // Tells the surface to perform a glFlush() before swapping buffers.
  229. virtual void SetForceGlFlushOnSwapBuffers();
  230. // Support for eglGetFrameTimestamps.
  231. virtual bool SupportsSwapTimestamps() const;
  232. virtual void SetEnableSwapTimestamps();
  233. virtual bool SupportsPlaneGpuFences() const;
  234. // Returns the number of buffers the surface uses in the swap chain. For
  235. // example, most surfaces are double-buffered, so this would return 2. For
  236. // triple-buffered surfaces this would return 3, etc.
  237. virtual int GetBufferCount() const;
  238. // Return the interface used for querying EGL timestamps.
  239. virtual EGLTimestampClient* GetEGLTimestampClient();
  240. virtual bool SupportsGpuVSync() const;
  241. virtual void SetGpuVSyncEnabled(bool enabled);
  242. virtual void SetDisplayTransform(gfx::OverlayTransform transform) {}
  243. virtual void SetFrameRate(float frame_rate) {}
  244. virtual void SetChoreographerVsyncIdForNextFrame(
  245. absl::optional<int64_t> choreographer_vsync_id) {}
  246. static GLSurface* GetCurrent();
  247. virtual void SetCurrent();
  248. virtual bool IsCurrent();
  249. static bool ExtensionsContain(const char* extensions, const char* name);
  250. virtual bool SupportsDelegatedInk();
  251. virtual void SetDelegatedInkTrailStartPoint(
  252. std::unique_ptr<gfx::DelegatedInkMetadata> metadata) {}
  253. virtual void InitDelegatedInkPointRendererReceiver(
  254. mojo::PendingReceiver<gfx::mojom::DelegatedInkPointRenderer>
  255. pending_receiver);
  256. // This should be called at most once at GPU process startup time.
  257. static void SetForcedGpuPreference(GpuPreference gpu_preference);
  258. // If a gpu preference is forced (by GPU driver bug workaround, etc), return
  259. // it. Otherwise, return the original input preference.
  260. static GpuPreference AdjustGpuPreference(GpuPreference gpu_preference);
  261. virtual void SetCALayerErrorCode(gfx::CALayerResult ca_layer_error_code) {}
  262. protected:
  263. virtual ~GLSurface();
  264. static GpuPreference forced_gpu_preference_;
  265. private:
  266. static void ClearCurrent();
  267. friend class base::RefCounted<GLSurface>;
  268. friend class GLContext;
  269. };
  270. // Implementation of GLSurface that forwards all calls through to another
  271. // GLSurface.
  272. class GL_EXPORT GLSurfaceAdapter : public GLSurface {
  273. public:
  274. explicit GLSurfaceAdapter(GLSurface* surface);
  275. GLSurfaceAdapter(const GLSurfaceAdapter&) = delete;
  276. GLSurfaceAdapter& operator=(const GLSurfaceAdapter&) = delete;
  277. bool Initialize(GLSurfaceFormat format) override;
  278. void PrepareToDestroy(bool have_context) override;
  279. void Destroy() override;
  280. bool Resize(const gfx::Size& size,
  281. float scale_factor,
  282. const gfx::ColorSpace& color_space,
  283. bool has_alpha) override;
  284. bool Recreate() override;
  285. bool DeferDraws() override;
  286. bool IsOffscreen() override;
  287. gfx::SwapResult SwapBuffers(PresentationCallback callback) override;
  288. void SwapBuffersAsync(SwapCompletionCallback completion_callback,
  289. PresentationCallback presentation_callback) override;
  290. gfx::SwapResult SwapBuffersWithBounds(const std::vector<gfx::Rect>& rects,
  291. PresentationCallback callback) override;
  292. gfx::SwapResult PostSubBuffer(int x,
  293. int y,
  294. int width,
  295. int height,
  296. PresentationCallback callback) override;
  297. void PostSubBufferAsync(int x,
  298. int y,
  299. int width,
  300. int height,
  301. SwapCompletionCallback completion_callback,
  302. PresentationCallback presentation_callback) override;
  303. gfx::SwapResult CommitOverlayPlanes(PresentationCallback callback) override;
  304. void CommitOverlayPlanesAsync(
  305. SwapCompletionCallback completion_callback,
  306. PresentationCallback presentation_callback) override;
  307. bool SupportsSwapBuffersWithBounds() override;
  308. bool SupportsPostSubBuffer() override;
  309. bool SupportsCommitOverlayPlanes() override;
  310. bool SupportsAsyncSwap() override;
  311. gfx::Size GetSize() override;
  312. void* GetHandle() override;
  313. void PreserveChildSurfaceControls() override;
  314. unsigned int GetBackingFramebufferObject() override;
  315. bool OnMakeCurrent(GLContext* context) override;
  316. bool SetBackbufferAllocation(bool allocated) override;
  317. void SetFrontbufferAllocation(bool allocated) override;
  318. void* GetShareHandle() override;
  319. GLDisplay* GetGLDisplay() override;
  320. void* GetConfig() override;
  321. GLSurfaceFormat GetFormat() override;
  322. gfx::VSyncProvider* GetVSyncProvider() override;
  323. void SetVSyncEnabled(bool enabled) override;
  324. bool ScheduleOverlayPlane(
  325. GLImage* image,
  326. std::unique_ptr<gfx::GpuFence> gpu_fence,
  327. const gfx::OverlayPlaneData& overlay_plane_data) override;
  328. bool ScheduleDCLayer(
  329. std::unique_ptr<ui::DCRendererLayerParams> params) override;
  330. bool SetEnableDCLayers(bool enable) override;
  331. bool IsSurfaceless() const override;
  332. bool SupportsViewporter() const override;
  333. gfx::SurfaceOrigin GetOrigin() const override;
  334. bool BuffersFlipped() const override;
  335. bool SupportsDCLayers() const override;
  336. bool SupportsProtectedVideo() const override;
  337. bool SupportsOverridePlatformSize() const override;
  338. bool SetDrawRectangle(const gfx::Rect& rect) override;
  339. gfx::Vector2d GetDrawOffset() const override;
  340. void SetRelyOnImplicitSync() override;
  341. void SetForceGlFlushOnSwapBuffers() override;
  342. bool SupportsSwapTimestamps() const override;
  343. void SetEnableSwapTimestamps() override;
  344. bool SupportsPlaneGpuFences() const override;
  345. int GetBufferCount() const override;
  346. bool SupportsGpuVSync() const override;
  347. void SetGpuVSyncEnabled(bool enabled) override;
  348. void SetDisplayTransform(gfx::OverlayTransform transform) override;
  349. void SetFrameRate(float frame_rate) override;
  350. void SetChoreographerVsyncIdForNextFrame(
  351. absl::optional<int64_t> choreographer_vsync_id) override;
  352. void SetCurrent() override;
  353. bool IsCurrent() override;
  354. bool SupportsDelegatedInk() override;
  355. void SetDelegatedInkTrailStartPoint(
  356. std::unique_ptr<gfx::DelegatedInkMetadata> metadata) override;
  357. void InitDelegatedInkPointRendererReceiver(
  358. mojo::PendingReceiver<gfx::mojom::DelegatedInkPointRenderer>
  359. pending_receiver) override;
  360. GLSurface* surface() const { return surface_.get(); }
  361. protected:
  362. ~GLSurfaceAdapter() override;
  363. private:
  364. scoped_refptr<GLSurface> surface_;
  365. };
  366. // Wraps GLSurface in scoped_refptr and tries to initializes it. Returns a
  367. // scoped_refptr containing the initialized GLSurface or nullptr if
  368. // initialization fails.
  369. GL_EXPORT scoped_refptr<GLSurface> InitializeGLSurface(
  370. scoped_refptr<GLSurface> surface);
  371. GL_EXPORT scoped_refptr<GLSurface> InitializeGLSurfaceWithFormat(
  372. scoped_refptr<GLSurface> surface, GLSurfaceFormat format);
  373. } // namespace gl
  374. #endif // UI_GL_GL_SURFACE_H_