in_process_command_buffer.h revision a36e5920737c6adbddd3e43b760e5de8431db6e0
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.
4
5#ifndef GPU_COMMAND_BUFFER_SERVICE_IN_PROCESS_COMMAND_BUFFER_H_
6#define GPU_COMMAND_BUFFER_SERVICE_IN_PROCESS_COMMAND_BUFFER_H_
7
8#include <vector>
9
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/synchronization/lock.h"
15#include "base/synchronization/waitable_event.h"
16#include "gpu/command_buffer/common/command_buffer.h"
17#include "gpu/gpu_export.h"
18#include "ui/gfx/gpu_memory_buffer.h"
19#include "ui/gfx/native_widget_types.h"
20#include "ui/gl/gpu_preference.h"
21
22namespace gfx {
23class GLContext;
24class GLImage;
25class GLSurface;
26class Size;
27}
28
29namespace gpu {
30
31namespace gles2 {
32class GLES2Decoder;
33}
34
35class GpuScheduler;
36class TransferBufferManagerInterface;
37
38// This class provides a thread-safe interface to the global GPU service (for
39// example GPU thread) when being run in single process mode.
40// However, the behavior for accessing one context (i.e. one instance of this
41// class) from different client threads is undefined.
42class GPU_EXPORT InProcessCommandBuffer : public CommandBuffer {
43 public:
44  InProcessCommandBuffer();
45  virtual ~InProcessCommandBuffer();
46
47  // Used to override the GPU thread with explicit scheduling.
48  // (By default an internal GPU thread will be spawned to handle all GL work
49  // and the two functions are unused.)
50  // The callback will be called from different client threads. After the
51  // callback is issued, the client is expected to eventually call
52  // ProcessGpuWorkOnCurrentThread(). The latter cannot be called from different
53  // threads.
54  // The callback needs to be set before any context is created.
55  static void SetScheduleCallback(const base::Closure& callback);
56  static void ProcessGpuWorkOnCurrentThread();
57
58  static void EnableVirtualizedContext();
59
60  bool Initialize(bool is_offscreen,
61                  bool share_resources,
62                  gfx::AcceleratedWidget window,
63                  const gfx::Size& size,
64                  const char* allowed_extensions,
65                  const std::vector<int32>& attribs,
66                  gfx::GpuPreference gpu_preference,
67                  const base::Closure& context_lost_callback,
68                  unsigned int share_group_id);
69  void Destroy();
70  void SignalSyncPoint(unsigned sync_point,
71                       const base::Closure& callback);
72  unsigned int CreateImageForGpuMemoryBuffer(
73      gfx::GpuMemoryBufferHandle buffer,
74      gfx::Size size);
75  void RemoveImage(unsigned int image_id);
76
77  // CommandBuffer implementation:
78  virtual bool Initialize() OVERRIDE;
79  virtual State GetState() OVERRIDE;
80  virtual State GetLastState() OVERRIDE;
81  virtual int32 GetLastToken() OVERRIDE;
82  virtual void Flush(int32 put_offset) OVERRIDE;
83  virtual State FlushSync(int32 put_offset, int32 last_known_get) OVERRIDE;
84  virtual void SetGetBuffer(int32 shm_id) OVERRIDE;
85  virtual void SetGetOffset(int32 get_offset) OVERRIDE;
86  virtual gpu::Buffer CreateTransferBuffer(size_t size, int32* id) OVERRIDE;
87  virtual void DestroyTransferBuffer(int32 id) OVERRIDE;
88  virtual gpu::Buffer GetTransferBuffer(int32 id) OVERRIDE;
89  virtual void SetToken(int32 token) OVERRIDE;
90  virtual void SetParseError(gpu::error::Error error) OVERRIDE;
91  virtual void SetContextLostReason(
92      gpu::error::ContextLostReason reason) OVERRIDE;
93  virtual uint32 InsertSyncPoint() OVERRIDE;
94  virtual gpu::error::Error GetLastError() OVERRIDE;
95
96  // The serializer interface to the GPU service (i.e. thread).
97  class SchedulerClient {
98   public:
99     virtual ~SchedulerClient() {}
100     virtual void QueueTask(const base::Closure& task) = 0;
101  };
102
103 private:
104  bool InitializeOnGpuThread(bool is_offscreen,
105                             gfx::AcceleratedWidget window,
106                             const gfx::Size& size,
107                             const char* allowed_extensions,
108                             const std::vector<int32>& attribs,
109                             gfx::GpuPreference gpu_preference);
110  bool DestroyOnGpuThread();
111  void FlushOnGpuThread(int32 put_offset);
112  void CreateImageOnGpuThread(gfx::GpuMemoryBufferHandle buffer,
113                              gfx::Size size,
114                              unsigned int image_id);
115  void RemoveImageOnGpuThread(unsigned int image_id);
116  bool MakeCurrent();
117  bool IsContextLost();
118  base::Closure WrapCallback(const base::Closure& callback);
119  State GetStateFast();
120  void QueueTask(const base::Closure& task) { queue_->QueueTask(task); }
121
122  // Callbacks:
123  void OnContextLost();
124  void OnResizeView(gfx::Size size, float scale_factor);
125  bool GetBufferChanged(int32 transfer_buffer_id);
126  void PumpCommands();
127
128  // Members accessed on the gpu thread (possibly with the exception of
129  // creation):
130  bool context_lost_;
131  bool share_resources_;
132  scoped_ptr<TransferBufferManagerInterface> transfer_buffer_manager_;
133  scoped_ptr<GpuScheduler> gpu_scheduler_;
134  scoped_ptr<gles2::GLES2Decoder> decoder_;
135  scoped_refptr<gfx::GLContext> context_;
136  scoped_refptr<gfx::GLSurface> surface_;
137  base::Closure context_lost_callback_;
138  unsigned int share_group_id_;
139
140  // Members accessed on the client thread:
141  State last_state_;
142  int32 last_put_offset_;
143
144  // Accessed on both threads:
145  scoped_ptr<CommandBuffer> command_buffer_;
146  base::Lock command_buffer_lock_;
147  base::WaitableEvent flush_event_;
148  scoped_ptr<SchedulerClient> queue_;
149
150  DISALLOW_COPY_AND_ASSIGN(InProcessCommandBuffer);
151};
152
153}  // namespace gpu
154
155#endif  // GPU_COMMAND_BUFFER_SERVICE_IN_PROCESS_COMMAND_BUFFER_H_
156