gpu_command_buffer_stub.h revision f2477e01787aa58f445919b809d89e252beef54f
1// Copyright (c) 2012 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 CONTENT_COMMON_GPU_GPU_COMMAND_BUFFER_STUB_H_
6#define CONTENT_COMMON_GPU_GPU_COMMAND_BUFFER_STUB_H_
7
8#include <deque>
9#include <string>
10#include <vector>
11
12#include "base/memory/scoped_vector.h"
13#include "base/memory/weak_ptr.h"
14#include "base/observer_list.h"
15#include "content/common/content_export.h"
16#include "content/common/gpu/gpu_memory_manager.h"
17#include "content/common/gpu/gpu_memory_manager_client.h"
18#include "gpu/command_buffer/common/constants.h"
19#include "gpu/command_buffer/common/gpu_memory_allocation.h"
20#include "gpu/command_buffer/service/command_buffer_service.h"
21#include "gpu/command_buffer/service/context_group.h"
22#include "gpu/command_buffer/service/gpu_scheduler.h"
23#include "ipc/ipc_listener.h"
24#include "ipc/ipc_sender.h"
25#include "media/base/video_decoder_config.h"
26#include "ui/events/latency_info.h"
27#include "ui/gfx/gpu_memory_buffer.h"
28#include "ui/gfx/native_widget_types.h"
29#include "ui/gfx/size.h"
30#include "ui/gl/gl_surface.h"
31#include "ui/gl/gpu_preference.h"
32#include "url/gurl.h"
33
34namespace gpu {
35class GpuControlService;
36struct Mailbox;
37namespace gles2 {
38class ImageManager;
39class MailboxManager;
40}
41}
42
43namespace content {
44
45class GpuChannel;
46class GpuVideoDecodeAccelerator;
47class GpuWatchdog;
48
49class GpuCommandBufferStub
50    : public GpuMemoryManagerClient,
51      public IPC::Listener,
52      public IPC::Sender,
53      public base::SupportsWeakPtr<GpuCommandBufferStub> {
54 public:
55  class DestructionObserver {
56   public:
57    // Called in Destroy(), before the context/surface are released.
58    virtual void OnWillDestroyStub() = 0;
59
60   protected:
61    virtual ~DestructionObserver() {}
62  };
63
64  typedef base::Callback<void(const ui::LatencyInfo&)>
65      LatencyInfoCallback;
66
67  GpuCommandBufferStub(
68      GpuChannel* channel,
69      GpuCommandBufferStub* share_group,
70      const gfx::GLSurfaceHandle& handle,
71      gpu::gles2::MailboxManager* mailbox_manager,
72      gpu::gles2::ImageManager* image_manager,
73      const gfx::Size& size,
74      const gpu::gles2::DisallowedFeatures& disallowed_features,
75      const std::vector<int32>& attribs,
76      gfx::GpuPreference gpu_preference,
77      bool use_virtualized_gl_context,
78      int32 route_id,
79      int32 surface_id,
80      GpuWatchdog* watchdog,
81      bool software,
82      const GURL& active_url);
83
84  virtual ~GpuCommandBufferStub();
85
86  // IPC::Listener implementation:
87  virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
88
89  // IPC::Sender implementation:
90  virtual bool Send(IPC::Message* msg) OVERRIDE;
91
92  // GpuMemoryManagerClient implementation:
93  virtual gfx::Size GetSurfaceSize() const OVERRIDE;
94  virtual gpu::gles2::MemoryTracker* GetMemoryTracker() const OVERRIDE;
95  virtual void SetMemoryAllocation(
96      const gpu::MemoryAllocation& allocation) OVERRIDE;
97  virtual void SuggestHaveFrontBuffer(bool suggest_have_frontbuffer) OVERRIDE;
98  virtual bool GetTotalGpuMemory(uint64* bytes) OVERRIDE;
99
100  // Whether this command buffer can currently handle IPC messages.
101  bool IsScheduled();
102
103  // If the command buffer is pre-empted and cannot process commands.
104  bool IsPreempted() const {
105    return scheduler_.get() && scheduler_->IsPreempted();
106  }
107
108  // Whether there are commands in the buffer that haven't been processed.
109  bool HasUnprocessedCommands();
110
111  gpu::gles2::GLES2Decoder* decoder() const { return decoder_.get(); }
112  gpu::GpuScheduler* scheduler() const { return scheduler_.get(); }
113  GpuChannel* channel() const { return channel_; }
114
115  // Identifies the target surface.
116  int32 surface_id() const { return surface_id_; }
117
118  // Identifies the various GpuCommandBufferStubs in the GPU process belonging
119  // to the same renderer process.
120  int32 route_id() const { return route_id_; }
121
122  gfx::GpuPreference gpu_preference() { return gpu_preference_; }
123
124  int32 GetRequestedAttribute(int attr) const;
125
126  // Sends a message to the console.
127  void SendConsoleMessage(int32 id, const std::string& message);
128
129  void SendCachedShader(const std::string& key, const std::string& shader);
130
131  gfx::GLSurface* surface() const { return surface_.get(); }
132
133  void AddDestructionObserver(DestructionObserver* observer);
134  void RemoveDestructionObserver(DestructionObserver* observer);
135
136  // Associates a sync point to this stub. When the stub is destroyed, it will
137  // retire all sync points that haven't been previously retired.
138  void AddSyncPoint(uint32 sync_point);
139
140  void SetPreemptByFlag(scoped_refptr<gpu::PreemptionFlag> flag);
141
142  void SetLatencyInfoCallback(const LatencyInfoCallback& callback);
143
144  void MarkContextLost();
145
146 private:
147  GpuMemoryManager* GetMemoryManager();
148  bool MakeCurrent();
149  void Destroy();
150
151  // Cleans up and sends reply if OnInitialize failed.
152  void OnInitializeFailed(IPC::Message* reply_message);
153
154  // Message handlers:
155  void OnInitialize(base::SharedMemoryHandle shared_state_shm,
156                    IPC::Message* reply_message);
157  void OnSetGetBuffer(int32 shm_id, IPC::Message* reply_message);
158  void OnProduceFrontBuffer(const gpu::Mailbox& mailbox);
159  void OnGetState(IPC::Message* reply_message);
160  void OnGetStateFast(IPC::Message* reply_message);
161  void OnAsyncFlush(int32 put_offset, uint32 flush_count);
162  void OnEcho(const IPC::Message& message);
163  void OnRescheduled();
164  void OnRegisterTransferBuffer(int32 id,
165                                base::SharedMemoryHandle transfer_buffer,
166                                uint32 size);
167  void OnDestroyTransferBuffer(int32 id);
168  void OnGetTransferBuffer(int32 id, IPC::Message* reply_message);
169
170  void OnCreateVideoDecoder(
171      media::VideoCodecProfile profile,
172      IPC::Message* reply_message);
173
174  void OnSetSurfaceVisible(bool visible);
175
176  void OnDiscardBackbuffer();
177  void OnEnsureBackbuffer();
178
179  void OnRetireSyncPoint(uint32 sync_point);
180  bool OnWaitSyncPoint(uint32 sync_point);
181  void OnSyncPointRetired();
182  void OnSignalSyncPoint(uint32 sync_point, uint32 id);
183  void OnSignalSyncPointAck(uint32 id);
184  void OnSignalQuery(uint32 query, uint32 id);
185
186  void OnReceivedClientManagedMemoryStats(const gpu::ManagedMemoryStats& stats);
187  void OnSetClientHasMemoryAllocationChangedCallback(bool has_callback);
188
189  void OnRegisterGpuMemoryBuffer(int32 id,
190                                 gfx::GpuMemoryBufferHandle gpu_memory_buffer,
191                                 uint32 width,
192                                 uint32 height,
193                                 uint32 internalformat);
194  void OnDestroyGpuMemoryBuffer(int32 id);
195
196  void OnCommandProcessed();
197  void OnParseError();
198  void OnSetLatencyInfo(const ui::LatencyInfo& latency_info);
199
200  void ReportState();
201
202  // Wrapper for GpuScheduler::PutChanged that sets the crash report URL.
203  void PutChanged();
204
205  // Poll the command buffer to execute work.
206  void PollWork();
207
208  // Whether this command buffer needs to be polled again in the future.
209  bool HasMoreWork();
210
211  void ScheduleDelayedWork(int64 delay);
212
213  bool CheckContextLost();
214
215  // The lifetime of objects of this class is managed by a GpuChannel. The
216  // GpuChannels destroy all the GpuCommandBufferStubs that they own when they
217  // are destroyed. So a raw pointer is safe.
218  GpuChannel* channel_;
219
220  // The group of contexts that share namespaces with this context.
221  scoped_refptr<gpu::gles2::ContextGroup> context_group_;
222
223  gfx::GLSurfaceHandle handle_;
224  gfx::Size initial_size_;
225  gpu::gles2::DisallowedFeatures disallowed_features_;
226  std::vector<int32> requested_attribs_;
227  gfx::GpuPreference gpu_preference_;
228  bool use_virtualized_gl_context_;
229  int32 route_id_;
230  int32 surface_id_;
231  bool software_;
232  uint32 last_flush_count_;
233
234  scoped_ptr<gpu::CommandBufferService> command_buffer_;
235  scoped_ptr<gpu::gles2::GLES2Decoder> decoder_;
236  scoped_ptr<gpu::GpuScheduler> scheduler_;
237  scoped_refptr<gfx::GLSurface> surface_;
238  scoped_ptr<gpu::GpuControlService> gpu_control_;
239
240  scoped_ptr<GpuMemoryManagerClientState> memory_manager_client_state_;
241  // The last memory allocation received from the GpuMemoryManager (used to
242  // elide redundant work).
243  bool last_memory_allocation_valid_;
244  gpu::MemoryAllocation last_memory_allocation_;
245
246  GpuWatchdog* watchdog_;
247
248  ObserverList<DestructionObserver> destruction_observers_;
249
250  // A queue of sync points associated with this stub.
251  std::deque<uint32> sync_points_;
252  int sync_point_wait_count_;
253
254  bool delayed_work_scheduled_;
255  uint64 previous_messages_processed_;
256  base::TimeTicks last_idle_time_;
257
258  scoped_refptr<gpu::PreemptionFlag> preemption_flag_;
259
260  LatencyInfoCallback latency_info_callback_;
261
262  GURL active_url_;
263  size_t active_url_hash_;
264
265  size_t total_gpu_memory_;
266
267  DISALLOW_COPY_AND_ASSIGN(GpuCommandBufferStub);
268};
269
270}  // namespace content
271
272#endif  // CONTENT_COMMON_GPU_GPU_COMMAND_BUFFER_STUB_H_
273