1 // Copyright 2013 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.
5 #ifndef GPU_COMMAND_BUFFER_SERVICE_IN_PROCESS_COMMAND_BUFFER_H_
6 #define GPU_COMMAND_BUFFER_SERVICE_IN_PROCESS_COMMAND_BUFFER_H_
10 #include "base/callback.h"
11 #include "base/compiler_specific.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/memory/weak_ptr.h"
15 #include "base/synchronization/lock.h"
16 #include "base/synchronization/waitable_event.h"
17 #include "gpu/command_buffer/common/command_buffer.h"
18 #include "gpu/command_buffer/common/gpu_control.h"
19 #include "gpu/gpu_export.h"
20 #include "ui/gfx/gpu_memory_buffer.h"
21 #include "ui/gfx/native_widget_types.h"
22 #include "ui/gl/gl_surface.h"
23 #include "ui/gl/gpu_preference.h"
26 class SequenceChecker;
35 #if defined(OS_ANDROID)
40 class StreamTextureManagerInProcess;
50 class GpuMemoryBufferFactory;
52 class TransferBufferManagerInterface;
54 // This class provides a thread-safe interface to the global GPU service (for
55 // example GPU thread) when being run in single process mode.
56 // However, the behavior for accessing one context (i.e. one instance of this
57 // class) from different client threads is undefined.
58 class GPU_EXPORT InProcessCommandBuffer : public CommandBuffer,
61 InProcessCommandBuffer();
62 virtual ~InProcessCommandBuffer();
64 // Used to override the GPU thread with explicit scheduling.
65 // (By default an internal GPU thread will be spawned to handle all GL work
66 // and the two functions are unused.)
67 // The callback will be called from different client threads. After the
68 // callback is issued, the client is expected to eventually call
69 // ProcessGpuWorkOnCurrentThread(). The latter cannot be called from different
71 // The callback needs to be set before any context is created.
72 static void SetScheduleCallback(const base::Closure& callback);
73 static void ProcessGpuWorkOnCurrentThread();
75 static void EnableVirtualizedContext();
76 static void SetGpuMemoryBufferFactory(GpuMemoryBufferFactory* factory);
78 // If |surface| is not NULL, use it directly; in this case, the command
79 // buffer gpu thread must be the same as the client thread. Otherwise create
81 bool Initialize(scoped_refptr<gfx::GLSurface> surface,
84 gfx::AcceleratedWidget window,
85 const gfx::Size& size,
86 const std::vector<int32>& attribs,
87 gfx::GpuPreference gpu_preference,
88 const base::Closure& context_lost_callback,
89 unsigned int share_group_id);
92 // CommandBuffer implementation:
93 virtual bool Initialize() OVERRIDE;
94 virtual State GetState() OVERRIDE;
95 virtual State GetLastState() OVERRIDE;
96 virtual int32 GetLastToken() OVERRIDE;
97 virtual void Flush(int32 put_offset) OVERRIDE;
98 virtual State FlushSync(int32 put_offset, int32 last_known_get) OVERRIDE;
99 virtual void SetGetBuffer(int32 shm_id) OVERRIDE;
100 virtual void SetGetOffset(int32 get_offset) OVERRIDE;
101 virtual gpu::Buffer CreateTransferBuffer(size_t size, int32* id) OVERRIDE;
102 virtual void DestroyTransferBuffer(int32 id) OVERRIDE;
103 virtual gpu::Buffer GetTransferBuffer(int32 id) OVERRIDE;
104 virtual void SetToken(int32 token) OVERRIDE;
105 virtual void SetParseError(gpu::error::Error error) OVERRIDE;
106 virtual void SetContextLostReason(
107 gpu::error::ContextLostReason reason) OVERRIDE;
108 virtual gpu::error::Error GetLastError() OVERRIDE;
110 // GpuControl implementation:
111 virtual bool SupportsGpuMemoryBuffer() OVERRIDE;
112 virtual gfx::GpuMemoryBuffer* CreateGpuMemoryBuffer(
115 unsigned internalformat,
117 virtual void DestroyGpuMemoryBuffer(int32 id) OVERRIDE;
118 virtual bool GenerateMailboxNames(unsigned num,
119 std::vector<gpu::Mailbox>* names) OVERRIDE;
120 virtual uint32 InsertSyncPoint() OVERRIDE;
121 virtual void SignalSyncPoint(uint32 sync_point,
122 const base::Closure& callback) OVERRIDE;
123 virtual void SignalQuery(uint32 query,
124 const base::Closure& callback) OVERRIDE;
125 virtual void SendManagedMemoryStats(const gpu::ManagedMemoryStats& stats)
128 // The serializer interface to the GPU service (i.e. thread).
129 class SchedulerClient {
131 virtual ~SchedulerClient() {}
133 // Queues a task to run as soon as possible.
134 virtual void QueueTask(const base::Closure& task) = 0;
136 // Schedules |callback| to run at an appropriate time for performing idle
138 virtual void ScheduleIdleWork(const base::Closure& task) = 0;
141 #if defined(OS_ANDROID)
142 scoped_refptr<gfx::SurfaceTexture> GetSurfaceTexture(
147 bool InitializeOnGpuThread(bool is_offscreen,
148 gfx::AcceleratedWidget window,
149 const gfx::Size& size,
150 const std::vector<int32>& attribs,
151 gfx::GpuPreference gpu_preference);
152 bool DestroyOnGpuThread();
153 void FlushOnGpuThread(int32 put_offset);
155 bool IsContextLost();
156 base::Closure WrapCallback(const base::Closure& callback);
157 State GetStateFast();
158 void QueueTask(const base::Closure& task) { queue_->QueueTask(task); }
159 void CheckSequencedThread();
162 void OnContextLost();
163 void OnResizeView(gfx::Size size, float scale_factor);
164 bool GetBufferChanged(int32 transfer_buffer_id);
166 void ScheduleMoreIdleWork();
168 // Members accessed on the gpu thread (possibly with the exception of
171 bool share_resources_;
172 scoped_ptr<TransferBufferManagerInterface> transfer_buffer_manager_;
173 scoped_ptr<GpuScheduler> gpu_scheduler_;
174 scoped_ptr<gles2::GLES2Decoder> decoder_;
175 scoped_refptr<gfx::GLContext> context_;
176 scoped_refptr<gfx::GLSurface> surface_;
177 base::Closure context_lost_callback_;
178 unsigned int share_group_id_;
180 // Members accessed on the client thread:
182 int32 last_put_offset_;
183 bool supports_gpu_memory_buffer_;
185 // Accessed on both threads:
186 scoped_ptr<CommandBuffer> command_buffer_;
187 base::Lock command_buffer_lock_;
188 base::WaitableEvent flush_event_;
189 scoped_ptr<SchedulerClient> queue_;
190 State state_after_last_flush_;
191 base::Lock state_after_last_flush_lock_;
192 scoped_ptr<GpuControl> gpu_control_;
194 #if defined(OS_ANDROID)
195 scoped_refptr<StreamTextureManagerInProcess> stream_texture_manager_;
198 // Only used with explicit scheduling and the gpu thread is the same as
199 // the client thread.
200 scoped_ptr<base::SequenceChecker> sequence_checker_;
202 base::WeakPtr<InProcessCommandBuffer> gpu_thread_weak_ptr_;
203 base::WeakPtrFactory<InProcessCommandBuffer> gpu_thread_weak_ptr_factory_;
205 DISALLOW_COPY_AND_ASSIGN(InProcessCommandBuffer);
210 #endif // GPU_COMMAND_BUFFER_SERVICE_IN_PROCESS_COMMAND_BUFFER_H_