1 // Copyright 2019 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 GPU_IPC_GPU_TASK_SCHEDULER_HELPER_H_
6 #define GPU_IPC_GPU_TASK_SCHEDULER_HELPER_H_
7 
8 #include "base/callback.h"
9 #include "gpu/command_buffer/common/sync_token.h"
10 #include "gpu/command_buffer/service/sequence_id.h"
11 #include "gpu/ipc/gl_in_process_context_export.h"
12 
13 namespace viz {
14 class VizProcessContextProvider;
15 class DisplayCompositorMemoryAndTaskController;
16 }
17 
18 namespace gpu {
19 class CommandBufferTaskExecutor;
20 class CommandBufferHelper;
21 class GLInProcessContext;
22 class SingleTaskSequence;
23 class InProcessCommandBuffer;
24 
25 // This class is a wrapper around a |gpu::SingleTaskSequence|. When we have
26 // SkiaRenderer enabled, this should behave exactly like a
27 // |gpu::SingleTaskSequence|. When we have GLRenderer with CommandBuffer, we
28 // need to initialize this class with a |CommandBufferHelper|. This is because
29 // when this class is used outside of actual CommandBuffer, we would need to
30 // make sure the order of post tasks still corresponds to the order that tasks
31 // are posted to the CommandBuffer.
32 // This class is per display compositor. When this is used with command buffer,
33 // it is created on VizProcessContextProvider. When this is used with
34 // SkiaRenderer, it is created on SkiaOutputSurfaceImpl. Each user of this class
35 // would hold a reference.
36 class GL_IN_PROCESS_CONTEXT_EXPORT GpuTaskSchedulerHelper {
37  public:
38   // This constructor is only used for SkiaOutputSurface.
39   explicit GpuTaskSchedulerHelper(
40       std::unique_ptr<SingleTaskSequence> task_sequence);
41   // This constructor is used for command buffer GLOutputSurface.
42   explicit GpuTaskSchedulerHelper(
43       CommandBufferTaskExecutor* command_buffer_task_executor);
44   ~GpuTaskSchedulerHelper();
45 
46   // This function sets up the |command_buffer_helper| which flushes the command
47   // buffer when a user outside of the command buffer shares the same
48   // GpuTaskSchedulerHelper. This is only needed for sharing with the command
49   // buffer, thus no need to be called when using SkiaRenderer.
50   void Initialize(CommandBufferHelper* command_buffer_helper);
51 
52   // This is called outside of CommandBuffer and would need to flush the command
53   // buffer if the CommandBufferHelper is present. CommandBuffer is a friend of
54   // this class and gets a direct pointer to the internal
55   // |gpu::SingleTaskSequence|.
56   void ScheduleGpuTask(base::OnceClosure task,
57                        std::vector<SyncToken> sync_tokens);
58 
59   // This is only called with SkiaOutputSurface, no need to flush command buffer
60   // here.
61   void ScheduleOrRetainGpuTask(base::OnceClosure task,
62                                std::vector<SyncToken> sync_tokens);
63   // This is only called with SkiaOutputSurface.
64   SequenceId GetSequenceId();
65 
66  private:
67   // If |using_command_buffer_| is true, we are using this class with
68   // GLOutputSurface. Otherwise we are using this class with
69   // SkiaOutputSurface.
70   bool using_command_buffer_;
71 
72   friend class gpu::GLInProcessContext;
73   friend class gpu::InProcessCommandBuffer;
74   friend class viz::VizProcessContextProvider;
75   friend class viz::DisplayCompositorMemoryAndTaskController;
76   // Only used for inside CommandBuffer implementation.
77   SingleTaskSequence* GetTaskSequence() const;
78 
79   // This |task_sequence_| handles task scheduling.
80   const std::unique_ptr<SingleTaskSequence> task_sequence_;
81 
82   // When this class is used with the command buffer, this bool indicates
83   // whether we have initialized the |command_buffer_helper_|. The command
84   // buffer requires the |task_sequence_| in order to initialize on gpu thread,
85   // but the |command_buffer_helper_| is created after initialization of the
86   // command buffer.
87   bool initialized_;
88 
89   // In the case where the TaskSequence is shared between command buffer and
90   // other users, |command_buffer_helper_| is used to flush the command buffer
91   // before posting tasks from a different user. This gives the command buffer a
92   // chance to post any pending tasks and maintains the ordering between command
93   // buffer and other user tasks.
94   CommandBufferHelper* command_buffer_helper_ = nullptr;
95 
96   DISALLOW_COPY_AND_ASSIGN(GpuTaskSchedulerHelper);
97 };
98 
99 }  // namespace gpu
100 
101 #endif  // GPU_IPC_GPU_TASK_SCHEDULER_HELPER_H_
102