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