image_transport_surface_overlay_mac.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. // Copyright 2015 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 GPU_IPC_SERVICE_IMAGE_TRANSPORT_SURFACE_OVERLAY_MAC_H_
  5. #define GPU_IPC_SERVICE_IMAGE_TRANSPORT_SURFACE_OVERLAY_MAC_H_
  6. #include <vector>
  7. #import "base/mac/scoped_nsobject.h"
  8. #include "base/memory/weak_ptr.h"
  9. #include "gpu/ipc/service/command_buffer_stub.h"
  10. #include "gpu/ipc/service/image_transport_surface.h"
  11. #include "ui/gfx/ca_layer_result.h"
  12. #include "ui/gfx/presentation_feedback.h"
  13. #include "ui/gl/gl_bindings.h"
  14. #include "ui/gl/gl_surface.h"
  15. #include "ui/gl/gpu_switching_observer.h"
  16. #if defined(USE_EGL)
  17. #include "ui/gl/gl_surface_egl.h"
  18. #endif
  19. @class CAContext;
  20. @class CALayer;
  21. namespace ui {
  22. class CALayerTreeCoordinator;
  23. struct CARendererLayerParams;
  24. }
  25. namespace gl {
  26. class GLFence;
  27. }
  28. namespace gpu {
  29. // ImageTransportSurfaceOverlayMac is used by the validating command decoder
  30. // and ImageTransportSurfaceOverlayMacEGL is used by the passthrough command
  31. // decoder.
  32. // Once the validating command decoder is removed,
  33. // ImageTransportSurfaceOverlayMac can be removed.
  34. class ImageTransportSurfaceOverlayMac : public gl::GLSurface,
  35. public ui::GpuSwitchingObserver {
  36. public:
  37. explicit ImageTransportSurfaceOverlayMac(
  38. base::WeakPtr<ImageTransportSurfaceDelegate> delegate);
  39. // GLSurface implementation
  40. bool Initialize(gl::GLSurfaceFormat format) override;
  41. void Destroy() override;
  42. void PrepareToDestroy(bool have_context) override;
  43. bool Resize(const gfx::Size& size,
  44. float scale_factor,
  45. const gfx::ColorSpace& color_space,
  46. bool has_alpha) override;
  47. bool IsOffscreen() override;
  48. gfx::SwapResult SwapBuffers(
  49. gl::GLSurface::PresentationCallback callback) override;
  50. void SwapBuffersAsync(
  51. gl::GLSurface::SwapCompletionCallback completion_callback,
  52. gl::GLSurface::PresentationCallback presentation_callback) override;
  53. gfx::SwapResult PostSubBuffer(
  54. int x,
  55. int y,
  56. int width,
  57. int height,
  58. gl::GLSurface::PresentationCallback callback) override;
  59. void PostSubBufferAsync(
  60. int x,
  61. int y,
  62. int width,
  63. int height,
  64. gl::GLSurface::SwapCompletionCallback completion_callback,
  65. gl::GLSurface::PresentationCallback presentation_callback) override;
  66. gfx::SwapResult CommitOverlayPlanes(
  67. gl::GLSurface::PresentationCallback callback) override;
  68. void CommitOverlayPlanesAsync(
  69. gl::GLSurface::SwapCompletionCallback completion_callback,
  70. gl::GLSurface::PresentationCallback presentation_callback) override;
  71. bool SupportsPostSubBuffer() override;
  72. bool SupportsCommitOverlayPlanes() override;
  73. bool SupportsAsyncSwap() override;
  74. gfx::Size GetSize() override;
  75. void* GetHandle() override;
  76. gl::GLSurfaceFormat GetFormat() override;
  77. bool OnMakeCurrent(gl::GLContext* context) override;
  78. bool ScheduleOverlayPlane(
  79. gl::GLImage* image,
  80. std::unique_ptr<gfx::GpuFence> gpu_fence,
  81. const gfx::OverlayPlaneData& overlay_plane_data) override;
  82. bool ScheduleCALayer(const ui::CARendererLayerParams& params) override;
  83. bool IsSurfaceless() const override;
  84. gfx::SurfaceOrigin GetOrigin() const override;
  85. // ui::GpuSwitchingObserver implementation.
  86. void OnGpuSwitched(gl::GpuPreference active_gpu_heuristic) override;
  87. void SetCALayerErrorCode(gfx::CALayerResult ca_layer_error_code) override;
  88. private:
  89. ~ImageTransportSurfaceOverlayMac() override;
  90. gfx::SwapResult SwapBuffersInternal(
  91. gl::GLSurface::SwapCompletionCallback completion_callback,
  92. gl::GLSurface::PresentationCallback presentation_callback);
  93. void ApplyBackpressure();
  94. void BufferPresented(gl::GLSurface::PresentationCallback callback,
  95. const gfx::PresentationFeedback& feedback);
  96. base::WeakPtr<ImageTransportSurfaceDelegate> delegate_;
  97. bool use_remote_layer_api_;
  98. base::scoped_nsobject<CAContext> ca_context_;
  99. std::unique_ptr<ui::CALayerTreeCoordinator> ca_layer_tree_coordinator_;
  100. gfx::Size pixel_size_;
  101. float scale_factor_;
  102. gfx::CALayerResult ca_layer_error_code_ = gfx::kCALayerSuccess;
  103. // A GLFence marking the end of the previous frame, used for applying
  104. // backpressure.
  105. uint64_t previous_frame_fence_ = 0;
  106. // The renderer ID that all contexts made current to this surface should be
  107. // targeting.
  108. GLint gl_renderer_id_;
  109. base::WeakPtrFactory<ImageTransportSurfaceOverlayMac> weak_ptr_factory_;
  110. };
  111. #if defined(USE_EGL)
  112. class ImageTransportSurfaceOverlayMacEGL : public gl::GLSurfaceEGL,
  113. public ui::GpuSwitchingObserver {
  114. public:
  115. ImageTransportSurfaceOverlayMacEGL(
  116. gl::GLDisplayEGL* display,
  117. base::WeakPtr<ImageTransportSurfaceDelegate> delegate);
  118. // GLSurface implementation
  119. bool Initialize(gl::GLSurfaceFormat format) override;
  120. void Destroy() override;
  121. void PrepareToDestroy(bool have_context) override;
  122. bool Resize(const gfx::Size& size,
  123. float scale_factor,
  124. const gfx::ColorSpace& color_space,
  125. bool has_alpha) override;
  126. bool IsOffscreen() override;
  127. gfx::SwapResult SwapBuffers(
  128. gl::GLSurface::PresentationCallback callback) override;
  129. void SwapBuffersAsync(
  130. gl::GLSurface::SwapCompletionCallback completion_callback,
  131. gl::GLSurface::PresentationCallback presentation_callback) override;
  132. gfx::SwapResult PostSubBuffer(
  133. int x,
  134. int y,
  135. int width,
  136. int height,
  137. gl::GLSurface::PresentationCallback callback) override;
  138. void PostSubBufferAsync(
  139. int x,
  140. int y,
  141. int width,
  142. int height,
  143. gl::GLSurface::SwapCompletionCallback completion_callback,
  144. gl::GLSurface::PresentationCallback presentation_callback) override;
  145. gfx::SwapResult CommitOverlayPlanes(
  146. gl::GLSurface::PresentationCallback callback) override;
  147. void CommitOverlayPlanesAsync(
  148. gl::GLSurface::SwapCompletionCallback completion_callback,
  149. gl::GLSurface::PresentationCallback presentation_callback) override;
  150. bool SupportsPostSubBuffer() override;
  151. bool SupportsCommitOverlayPlanes() override;
  152. bool SupportsAsyncSwap() override;
  153. gfx::Size GetSize() override;
  154. void* GetHandle() override;
  155. gl::GLSurfaceFormat GetFormat() override;
  156. bool OnMakeCurrent(gl::GLContext* context) override;
  157. bool ScheduleOverlayPlane(
  158. gl::GLImage* image,
  159. std::unique_ptr<gfx::GpuFence> gpu_fence,
  160. const gfx::OverlayPlaneData& overlay_plane_data) override;
  161. bool ScheduleCALayer(const ui::CARendererLayerParams& params) override;
  162. bool IsSurfaceless() const override;
  163. gfx::SurfaceOrigin GetOrigin() const override;
  164. // ui::GpuSwitchingObserver implementation.
  165. void OnGpuSwitched(gl::GpuPreference active_gpu_heuristic) override;
  166. void SetCALayerErrorCode(gfx::CALayerResult ca_layer_error_code) override;
  167. private:
  168. ~ImageTransportSurfaceOverlayMacEGL() override;
  169. gfx::SwapResult SwapBuffersInternal(
  170. gl::GLSurface::SwapCompletionCallback completion_callback,
  171. gl::GLSurface::PresentationCallback presentation_callback);
  172. void ApplyBackpressure();
  173. void BufferPresented(gl::GLSurface::PresentationCallback callback,
  174. const gfx::PresentationFeedback& feedback);
  175. base::WeakPtr<ImageTransportSurfaceDelegate> delegate_;
  176. bool use_remote_layer_api_;
  177. base::scoped_nsobject<CAContext> ca_context_;
  178. std::unique_ptr<ui::CALayerTreeCoordinator> ca_layer_tree_coordinator_;
  179. gfx::Size pixel_size_;
  180. float scale_factor_;
  181. gfx::CALayerResult ca_layer_error_code_ = gfx::kCALayerSuccess;
  182. // A GLFence marking the end of the previous frame, used for applying
  183. // backpressure.
  184. uint64_t previous_frame_fence_ = 0;
  185. // The renderer ID that all contexts made current to this surface should be
  186. // targeting.
  187. GLint gl_renderer_id_;
  188. base::WeakPtrFactory<ImageTransportSurfaceOverlayMacEGL> weak_ptr_factory_;
  189. };
  190. #endif // USE_EGL
  191. } // namespace gpu
  192. #endif // GPU_IPC_SERVICE_IMAGE_TRANSPORT_SURFACE_OVERLAY_MAC_H_