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.
5 #ifndef CONTENT_COMMON_GPU_GPU_COMMAND_BUFFER_STUB_H_
6 #define CONTENT_COMMON_GPU_GPU_COMMAND_BUFFER_STUB_H_
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"
44 class GpuVideoDecodeAccelerator;
45 class GpuVideoEncodeAccelerator;
47 struct WaitForCommandState;
49 class GpuCommandBufferStub
50 : public GpuMemoryManagerClient,
53 public base::SupportsWeakPtr<GpuCommandBufferStub> {
55 class DestructionObserver {
57 // Called in Destroy(), before the context/surface are released.
58 virtual void OnWillDestroyStub() = 0;
61 virtual ~DestructionObserver() {}
64 typedef base::Callback<void(const std::vector<ui::LatencyInfo>&)>
69 GpuCommandBufferStub* share_group,
70 const gfx::GLSurfaceHandle& handle,
71 gpu::gles2::MailboxManager* mailbox_manager,
72 const gfx::Size& size,
73 const gpu::gles2::DisallowedFeatures& disallowed_features,
74 const std::vector<int32>& attribs,
75 gfx::GpuPreference gpu_preference,
76 bool use_virtualized_gl_context,
79 GpuWatchdog* watchdog,
81 const GURL& active_url);
83 virtual ~GpuCommandBufferStub();
85 // IPC::Listener implementation:
86 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
88 // IPC::Sender implementation:
89 virtual bool Send(IPC::Message* msg) OVERRIDE;
91 // GpuMemoryManagerClient implementation:
92 virtual gfx::Size GetSurfaceSize() const OVERRIDE;
93 virtual gpu::gles2::MemoryTracker* GetMemoryTracker() const OVERRIDE;
94 virtual void SetMemoryAllocation(
95 const gpu::MemoryAllocation& allocation) OVERRIDE;
96 virtual void SuggestHaveFrontBuffer(bool suggest_have_frontbuffer) OVERRIDE;
97 virtual bool GetTotalGpuMemory(uint64* bytes) OVERRIDE;
99 // Whether this command buffer can currently handle IPC messages.
102 // If the command buffer is pre-empted and cannot process commands.
103 bool IsPreempted() const {
104 return scheduler_.get() && scheduler_->IsPreempted();
107 // Whether there are commands in the buffer that haven't been processed.
108 bool HasUnprocessedCommands();
110 gpu::gles2::GLES2Decoder* decoder() const { return decoder_.get(); }
111 gpu::GpuScheduler* scheduler() const { return scheduler_.get(); }
112 GpuChannel* channel() const { return channel_; }
114 // Identifies the target surface.
115 int32 surface_id() const { return surface_id_; }
117 // Identifies the various GpuCommandBufferStubs in the GPU process belonging
118 // to the same renderer process.
119 int32 route_id() const { return route_id_; }
121 gfx::GpuPreference gpu_preference() { return gpu_preference_; }
123 int32 GetRequestedAttribute(int attr) const;
125 // Sends a message to the console.
126 void SendConsoleMessage(int32 id, const std::string& message);
128 void SendCachedShader(const std::string& key, const std::string& shader);
130 gfx::GLSurface* surface() const { return surface_.get(); }
132 void AddDestructionObserver(DestructionObserver* observer);
133 void RemoveDestructionObserver(DestructionObserver* observer);
135 // Associates a sync point to this stub. When the stub is destroyed, it will
136 // retire all sync points that haven't been previously retired.
137 void AddSyncPoint(uint32 sync_point);
139 void SetPreemptByFlag(scoped_refptr<gpu::PreemptionFlag> flag);
141 void SetLatencyInfoCallback(const LatencyInfoCallback& callback);
143 void MarkContextLost();
145 uint64 GetMemoryUsage() const;
148 GpuMemoryManager* GetMemoryManager() const;
152 // Cleans up and sends reply if OnInitialize failed.
153 void OnInitializeFailed(IPC::Message* reply_message);
156 void OnInitialize(base::SharedMemoryHandle shared_state_shm,
157 IPC::Message* reply_message);
158 void OnSetGetBuffer(int32 shm_id, IPC::Message* reply_message);
159 void OnProduceFrontBuffer(const gpu::Mailbox& mailbox);
160 void OnGetState(IPC::Message* reply_message);
161 void OnWaitForTokenInRange(int32 start,
163 IPC::Message* reply_message);
164 void OnWaitForGetOffsetInRange(int32 start,
166 IPC::Message* reply_message);
167 void OnAsyncFlush(int32 put_offset, uint32 flush_count,
168 const std::vector<ui::LatencyInfo>& latency_info);
169 void OnEcho(const IPC::Message& message);
170 void OnRescheduled();
171 void OnRegisterTransferBuffer(int32 id,
172 base::SharedMemoryHandle transfer_buffer,
174 void OnDestroyTransferBuffer(int32 id);
175 void OnGetTransferBuffer(int32 id, IPC::Message* reply_message);
177 void OnCreateVideoDecoder(media::VideoCodecProfile profile,
179 IPC::Message* reply_message);
180 void OnCreateVideoEncoder(media::VideoFrame::Format input_format,
181 const gfx::Size& input_visible_size,
182 media::VideoCodecProfile output_profile,
183 uint32 initial_bitrate,
185 IPC::Message* reply_message);
187 void OnSetSurfaceVisible(bool visible);
189 void OnEnsureBackbuffer();
191 void OnRetireSyncPoint(uint32 sync_point);
192 bool OnWaitSyncPoint(uint32 sync_point);
193 void OnSyncPointRetired();
194 void OnSignalSyncPoint(uint32 sync_point, uint32 id);
195 void OnSignalSyncPointAck(uint32 id);
196 void OnSignalQuery(uint32 query, uint32 id);
198 void OnSetClientHasMemoryAllocationChangedCallback(bool has_callback);
200 void OnRegisterGpuMemoryBuffer(int32 id,
201 gfx::GpuMemoryBufferHandle handle,
204 uint32 internalformat);
205 void OnUnregisterGpuMemoryBuffer(int32 id);
207 void OnCommandProcessed();
209 void OnCreateStreamTexture(
210 uint32 texture_id, int32 stream_id, bool* succeeded);
214 // Wrapper for GpuScheduler::PutChanged that sets the crash report URL.
217 // Poll the command buffer to execute work.
220 // Whether this command buffer needs to be polled again in the future.
223 void ScheduleDelayedWork(int64 delay);
225 bool CheckContextLost();
226 void CheckCompleteWaits();
228 // The lifetime of objects of this class is managed by a GpuChannel. The
229 // GpuChannels destroy all the GpuCommandBufferStubs that they own when they
230 // are destroyed. So a raw pointer is safe.
231 GpuChannel* channel_;
233 // The group of contexts that share namespaces with this context.
234 scoped_refptr<gpu::gles2::ContextGroup> context_group_;
236 gfx::GLSurfaceHandle handle_;
237 gfx::Size initial_size_;
238 gpu::gles2::DisallowedFeatures disallowed_features_;
239 std::vector<int32> requested_attribs_;
240 gfx::GpuPreference gpu_preference_;
241 bool use_virtualized_gl_context_;
245 uint32 last_flush_count_;
247 scoped_ptr<gpu::CommandBufferService> command_buffer_;
248 scoped_ptr<gpu::gles2::GLES2Decoder> decoder_;
249 scoped_ptr<gpu::GpuScheduler> scheduler_;
250 scoped_refptr<gfx::GLSurface> surface_;
252 scoped_ptr<GpuMemoryManagerClientState> memory_manager_client_state_;
253 // The last memory allocation received from the GpuMemoryManager (used to
254 // elide redundant work).
255 bool last_memory_allocation_valid_;
256 gpu::MemoryAllocation last_memory_allocation_;
258 GpuWatchdog* watchdog_;
260 ObserverList<DestructionObserver> destruction_observers_;
262 // A queue of sync points associated with this stub.
263 std::deque<uint32> sync_points_;
264 int sync_point_wait_count_;
266 bool delayed_work_scheduled_;
267 uint64 previous_messages_processed_;
268 base::TimeTicks last_idle_time_;
270 scoped_refptr<gpu::PreemptionFlag> preemption_flag_;
272 LatencyInfoCallback latency_info_callback_;
275 size_t active_url_hash_;
277 size_t total_gpu_memory_;
278 scoped_ptr<WaitForCommandState> wait_for_token_;
279 scoped_ptr<WaitForCommandState> wait_for_get_offset_;
281 DISALLOW_COPY_AND_ASSIGN(GpuCommandBufferStub);
284 } // namespace content
286 #endif // CONTENT_COMMON_GPU_GPU_COMMAND_BUFFER_STUB_H_