1 // Copyright 2016 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_VIZ_PROCESS_CONTEXT_PROVIDER_H_
6 #define COMPONENTS_VIZ_SERVICE_DISPLAY_EMBEDDER_VIZ_PROCESS_CONTEXT_PROVIDER_H_
7 
8 #include <stdint.h>
9 
10 #include <memory>
11 
12 #include "base/callback_helpers.h"
13 #include "base/observer_list.h"
14 #include "base/trace_event/memory_dump_provider.h"
15 #include "components/viz/common/display/update_vsync_parameters_callback.h"
16 #include "components/viz/common/gpu/context_cache_controller.h"
17 #include "components/viz/common/gpu/context_provider.h"
18 #include "components/viz/common/gpu/gpu_vsync_callback.h"
19 #include "components/viz/service/viz_service_export.h"
20 #include "gpu/command_buffer/common/context_creation_attribs.h"
21 #include "gpu/ipc/common/surface_handle.h"
22 #include "gpu/ipc/gpu_task_scheduler_helper.h"
23 #include "ui/gfx/native_widget_types.h"
24 
25 class GrContext;
26 
27 namespace gpu {
28 namespace gles2 {
29 class GLES2CmdHelper;
30 class GLES2Implementation;
31 }  // namespace gles2
32 class CommandBufferTaskExecutor;
33 class GpuChannelManagerDelegate;
34 class GpuMemoryBufferManager;
35 class ImageFactory;
36 class InProcessCommandBuffer;
37 class TransferBuffer;
38 struct SharedMemoryLimits;
39 }  // namespace gpu
40 
41 namespace skia_bindings {
42 class GrContextForGLES2Interface;
43 }
44 
45 namespace viz {
46 class ContextLostObserver;
47 class GpuTaskSchedulerHelper;
48 class RendererSettings;
49 
50 // A ContextProvider used in the viz process to setup an InProcessCommandBuffer
51 // for the display compositor.
52 class VIZ_SERVICE_EXPORT VizProcessContextProvider
53     : public base::RefCountedThreadSafe<VizProcessContextProvider>,
54       public ContextProvider,
55       public base::trace_event::MemoryDumpProvider {
56  public:
57   VizProcessContextProvider(
58       gpu::CommandBufferTaskExecutor* task_executor,
59       gpu::SurfaceHandle surface_handle,
60       gpu::GpuMemoryBufferManager* gpu_memory_buffer_manager,
61       gpu::ImageFactory* image_factory,
62       gpu::GpuChannelManagerDelegate* gpu_channel_manager_delegate,
63       const RendererSettings& renderer_settings);
64 
65   // ContextProvider implementation.
66   void AddRef() const override;
67   void Release() const override;
68   gpu::ContextResult BindToCurrentThread() override;
69   gpu::gles2::GLES2Interface* ContextGL() override;
70   gpu::ContextSupport* ContextSupport() override;
71   class GrContext* GrContext() override;
72   gpu::SharedImageInterface* SharedImageInterface() override;
73   ContextCacheController* CacheController() override;
74   base::Lock* GetLock() override;
75   const gpu::Capabilities& ContextCapabilities() const override;
76   const gpu::GpuFeatureInfo& GetGpuFeatureInfo() const override;
77   void AddObserver(ContextLostObserver* obs) override;
78   void RemoveObserver(ContextLostObserver* obs) override;
79   gpu::SharedImageManager* GetSharedImageManager() override;
80   gpu::MemoryTracker* GetMemoryTracker() override;
81 
82   virtual void SetUpdateVSyncParametersCallback(
83       UpdateVSyncParametersCallback callback);
84   virtual void SetGpuVSyncCallback(GpuVSyncCallback callback);
85   virtual void SetGpuVSyncEnabled(bool enabled);
86   virtual bool UseRGB565PixelFormat() const;
87 
88   // Provides the GL internal format that should be used when calling
89   // glCopyTexImage2D() on the default framebuffer.
90   virtual uint32_t GetCopyTextureInternalFormat();
91 
92   virtual base::ScopedClosureRunner GetCacheBackBufferCb();
93 
94 #ifdef TOOLKIT_QT
command_buffer()95   gpu::InProcessCommandBuffer *command_buffer() { return command_buffer_.get(); }
96 #endif
97 
98   scoped_refptr<gpu::GpuTaskSchedulerHelper> GetGpuTaskSchedulerHelper();
99 
100  protected:
101   friend class base::RefCountedThreadSafe<VizProcessContextProvider>;
102   VizProcessContextProvider();  // For testing only.
103   ~VizProcessContextProvider() override;
104 
105  private:
106   void InitializeContext(
107       gpu::CommandBufferTaskExecutor* task_executor,
108       gpu::SurfaceHandle surface_handle,
109       gpu::GpuMemoryBufferManager* gpu_memory_buffer_manager,
110       gpu::ImageFactory* image_factory,
111       gpu::GpuChannelManagerDelegate* gpu_channel_manager_delegate,
112       const gpu::SharedMemoryLimits& mem_limits);
113   void OnContextLost();
114 
115   // base::trace_event::MemoryDumpProvider implementation.
116   bool OnMemoryDump(const base::trace_event::MemoryDumpArgs& args,
117                     base::trace_event::ProcessMemoryDump* pmd) override;
118 
119   const gpu::ContextCreationAttribs attributes_;
120 
121   // The |gpu_task_scheduler_helper_| has 1:1 relationship with the Display
122   // compositor.
123   scoped_refptr<gpu::GpuTaskSchedulerHelper> gpu_task_scheduler_helper_;
124   std::unique_ptr<gpu::InProcessCommandBuffer> command_buffer_;
125   std::unique_ptr<gpu::gles2::GLES2CmdHelper> gles2_helper_;
126   std::unique_ptr<gpu::TransferBuffer> transfer_buffer_;
127   std::unique_ptr<gpu::gles2::GLES2Implementation> gles2_implementation_;
128   std::unique_ptr<ContextCacheController> cache_controller_;
129   gpu::ContextResult context_result_ = gpu::ContextResult::kSuccess;
130 
131   std::unique_ptr<skia_bindings::GrContextForGLES2Interface> gr_context_;
132 
133   base::ObserverList<ContextLostObserver>::Unchecked observers_;
134 };
135 
136 }  // namespace viz
137 
138 #endif  // COMPONENTS_VIZ_SERVICE_DISPLAY_EMBEDDER_VIZ_PROCESS_CONTEXT_PROVIDER_H_
139