• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
34 namespace gpu {
35 class GpuControlService;
36 struct Mailbox;
37 namespace gles2 {
38 class ImageManager;
39 class MailboxManager;
40 }
41 }
42 
43 namespace content {
44 
45 class GpuChannel;
46 class GpuVideoDecodeAccelerator;
47 class GpuWatchdog;
48 
49 class 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:
~DestructionObserver()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.
IsPreempted()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 
decoder()111   gpu::gles2::GLES2Decoder* decoder() const { return decoder_.get(); }
scheduler()112   gpu::GpuScheduler* scheduler() const { return scheduler_.get(); }
channel()113   GpuChannel* channel() const { return channel_; }
114 
115   // Identifies the target surface.
surface_id()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.
route_id()120   int32 route_id() const { return route_id_; }
121 
gpu_preference()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 
surface()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   uint64 GetMemoryUsage() const;
147 
148  private:
149   GpuMemoryManager* GetMemoryManager() const;
150   bool MakeCurrent();
151   void Destroy();
152 
153   // Cleans up and sends reply if OnInitialize failed.
154   void OnInitializeFailed(IPC::Message* reply_message);
155 
156   // Message handlers:
157   void OnInitialize(base::SharedMemoryHandle shared_state_shm,
158                     IPC::Message* reply_message);
159   void OnSetGetBuffer(int32 shm_id, IPC::Message* reply_message);
160   void OnProduceFrontBuffer(const gpu::Mailbox& mailbox);
161   void OnGetState(IPC::Message* reply_message);
162   void OnGetStateFast(IPC::Message* reply_message);
163   void OnAsyncFlush(int32 put_offset, uint32 flush_count);
164   void OnEcho(const IPC::Message& message);
165   void OnRescheduled();
166   void OnRegisterTransferBuffer(int32 id,
167                                 base::SharedMemoryHandle transfer_buffer,
168                                 uint32 size);
169   void OnDestroyTransferBuffer(int32 id);
170   void OnGetTransferBuffer(int32 id, IPC::Message* reply_message);
171 
172   void OnCreateVideoDecoder(
173       media::VideoCodecProfile profile,
174       IPC::Message* reply_message);
175 
176   void OnSetSurfaceVisible(bool visible);
177 
178   void OnEnsureBackbuffer();
179 
180   void OnRetireSyncPoint(uint32 sync_point);
181   bool OnWaitSyncPoint(uint32 sync_point);
182   void OnSyncPointRetired();
183   void OnSignalSyncPoint(uint32 sync_point, uint32 id);
184   void OnSignalSyncPointAck(uint32 id);
185   void OnSignalQuery(uint32 query, uint32 id);
186 
187   void OnReceivedClientManagedMemoryStats(const gpu::ManagedMemoryStats& stats);
188   void OnSetClientHasMemoryAllocationChangedCallback(bool has_callback);
189 
190   void OnRegisterGpuMemoryBuffer(int32 id,
191                                  gfx::GpuMemoryBufferHandle gpu_memory_buffer,
192                                  uint32 width,
193                                  uint32 height,
194                                  uint32 internalformat);
195   void OnDestroyGpuMemoryBuffer(int32 id);
196 
197   void OnCommandProcessed();
198   void OnParseError();
199   void OnSetLatencyInfo(const ui::LatencyInfo& latency_info);
200 
201   void ReportState();
202 
203   // Wrapper for GpuScheduler::PutChanged that sets the crash report URL.
204   void PutChanged();
205 
206   // Poll the command buffer to execute work.
207   void PollWork();
208 
209   // Whether this command buffer needs to be polled again in the future.
210   bool HasMoreWork();
211 
212   void ScheduleDelayedWork(int64 delay);
213 
214   bool CheckContextLost();
215 
216   // The lifetime of objects of this class is managed by a GpuChannel. The
217   // GpuChannels destroy all the GpuCommandBufferStubs that they own when they
218   // are destroyed. So a raw pointer is safe.
219   GpuChannel* channel_;
220 
221   // The group of contexts that share namespaces with this context.
222   scoped_refptr<gpu::gles2::ContextGroup> context_group_;
223 
224   gfx::GLSurfaceHandle handle_;
225   gfx::Size initial_size_;
226   gpu::gles2::DisallowedFeatures disallowed_features_;
227   std::vector<int32> requested_attribs_;
228   gfx::GpuPreference gpu_preference_;
229   bool use_virtualized_gl_context_;
230   int32 route_id_;
231   int32 surface_id_;
232   bool software_;
233   uint32 last_flush_count_;
234 
235   scoped_ptr<gpu::CommandBufferService> command_buffer_;
236   scoped_ptr<gpu::gles2::GLES2Decoder> decoder_;
237   scoped_ptr<gpu::GpuScheduler> scheduler_;
238   scoped_refptr<gfx::GLSurface> surface_;
239   scoped_ptr<gpu::GpuControlService> gpu_control_;
240 
241   scoped_ptr<GpuMemoryManagerClientState> memory_manager_client_state_;
242   // The last memory allocation received from the GpuMemoryManager (used to
243   // elide redundant work).
244   bool last_memory_allocation_valid_;
245   gpu::MemoryAllocation last_memory_allocation_;
246 
247   GpuWatchdog* watchdog_;
248 
249   ObserverList<DestructionObserver> destruction_observers_;
250 
251   // A queue of sync points associated with this stub.
252   std::deque<uint32> sync_points_;
253   int sync_point_wait_count_;
254 
255   bool delayed_work_scheduled_;
256   uint64 previous_messages_processed_;
257   base::TimeTicks last_idle_time_;
258 
259   scoped_refptr<gpu::PreemptionFlag> preemption_flag_;
260 
261   LatencyInfoCallback latency_info_callback_;
262 
263   GURL active_url_;
264   size_t active_url_hash_;
265 
266   size_t total_gpu_memory_;
267 
268   DISALLOW_COPY_AND_ASSIGN(GpuCommandBufferStub);
269 };
270 
271 }  // namespace content
272 
273 #endif  // CONTENT_COMMON_GPU_GPU_COMMAND_BUFFER_STUB_H_
274