1 // Copyright 2014 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 
5 #ifndef COMPONENTS_VIZ_SERVICE_DISPLAY_EMBEDDER_GL_OUTPUT_SURFACE_H_
6 #define COMPONENTS_VIZ_SERVICE_DISPLAY_EMBEDDER_GL_OUTPUT_SURFACE_H_
7 
8 #include <memory>
9 #include <vector>
10 
11 #include "components/viz/common/display/update_vsync_parameters_callback.h"
12 #include "components/viz/service/display/output_surface.h"
13 #include "components/viz/service/display_embedder/viz_process_context_provider.h"
14 #include "gpu/command_buffer/client/context_support.h"
15 #include "ui/latency/latency_tracker.h"
16 
17 namespace viz {
18 
19 // An OutputSurface implementation that directly draws and
20 // swaps to an actual GL surface.
21 class GLOutputSurface : public OutputSurface {
22  public:
23   GLOutputSurface(scoped_refptr<VizProcessContextProvider> context_provider,
24                   gpu::SurfaceHandle surface_handle);
25   ~GLOutputSurface() override;
26 
27   // OutputSurface implementation
28   void BindToClient(OutputSurfaceClient* client) override;
29   void EnsureBackbuffer() override;
30   void DiscardBackbuffer() override;
31   void BindFramebuffer() override;
32   void SetDrawRectangle(const gfx::Rect& draw_rectangle) override;
33   void SetEnableDCLayers(bool enabled) override;
34   void Reshape(const gfx::Size& size,
35                float device_scale_factor,
36                const gfx::ColorSpace& color_space,
37                gfx::BufferFormat format,
38                bool use_stencil) override;
39   void SwapBuffers(OutputSurfaceFrame frame) override;
40   uint32_t GetFramebufferCopyTextureFormat() override;
41   bool IsDisplayedAsOverlayPlane() const override;
42   unsigned GetOverlayTextureId() const override;
43   bool HasExternalStencilTest() const override;
44   void ApplyExternalStencil() override;
45   unsigned UpdateGpuFence() override;
46   void SetNeedsSwapSizeNotifications(
47       bool needs_swap_size_notifications) override;
48   void SetUpdateVSyncParametersCallback(
49       UpdateVSyncParametersCallback callback) override;
50   void SetGpuVSyncCallback(GpuVSyncCallback callback) override;
51   void SetGpuVSyncEnabled(bool enabled) override;
SetDisplayTransformHint(gfx::OverlayTransform transform)52   void SetDisplayTransformHint(gfx::OverlayTransform transform) override {}
53   gfx::OverlayTransform GetDisplayTransform() override;
54   base::ScopedClosureRunner GetCacheBackBufferCb() override;
55 
56   gpu::SurfaceHandle GetSurfaceHandle() const override;
57   void SetFrameRate(float frame_rate) override;
58   void SetNeedsMeasureNextDrawLatency() override;
59 
60  protected:
client()61   OutputSurfaceClient* client() const { return client_; }
latency_tracker()62   ui::LatencyTracker* latency_tracker() { return &latency_tracker_; }
needs_swap_size_notifications()63   bool needs_swap_size_notifications() {
64     return needs_swap_size_notifications_;
65   }
66 
67   // Called when a swap completion is signaled from ImageTransportSurface.
68   virtual void DidReceiveSwapBuffersAck(const gfx::SwapResponse& response);
69 
70   // Called in SwapBuffers() when a swap is determined to be partial. Subclasses
71   // might override this method because different platforms handle partial swaps
72   // differently.
73   virtual void HandlePartialSwap(
74       const gfx::Rect& sub_buffer_rect,
75       uint32_t flags,
76       gpu::ContextSupport::SwapCompletedCallback swap_callback,
77       gpu::ContextSupport::PresentationCallback presentation_callback);
78 
79  private:
80   // Called when a swap completion is signaled from ImageTransportSurface.
81   void OnGpuSwapBuffersCompleted(std::vector<ui::LatencyInfo> latency_info,
82                                  bool top_controls_visible_height_changed,
83                                  const gfx::Size& pixel_size,
84                                  const gpu::SwapBuffersCompleteParams& params);
85   void OnPresentation(const gfx::PresentationFeedback& feedback);
86   void OnGpuVSync(base::TimeTicks vsync_time, base::TimeDelta vsync_interval);
87   gfx::Rect ApplyDisplayInverse(const gfx::Rect& input);
88 
89   scoped_refptr<VizProcessContextProvider> viz_context_provider_;
90   OutputSurfaceClient* client_ = nullptr;
91   bool wants_vsync_parameter_updates_ = false;
92   ui::LatencyTracker latency_tracker_;
93 
94   const gpu::SurfaceHandle surface_handle_;
95 
96   bool set_draw_rectangle_for_frame_ = false;
97   // True if the draw rectangle has been set at all since the last resize.
98   bool has_set_draw_rectangle_since_last_resize_ = false;
99   gfx::Size size_;
100   bool use_gpu_fence_;
101   unsigned gpu_fence_id_ = 0;
102   // Whether to send OutputSurfaceClient::DidSwapWithSize notifications.
103   bool needs_swap_size_notifications_ = false;
104 
105   base::WeakPtrFactory<GLOutputSurface> weak_ptr_factory_{this};
106 };
107 
108 }  // namespace viz
109 
110 #endif  // COMPONENTS_VIZ_SERVICE_DISPLAY_EMBEDDER_GL_OUTPUT_SURFACE_H_
111