Add P2PDatagramSocket and P2PStreamSocket interfaces.
[chromium-blink-merge.git] / content / common / gpu / gpu_command_buffer_stub.h
blobe19c32085f54efda387ded78ec71daf9a6a0659d
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_
8 #include <deque>
9 #include <string>
10 #include <vector>
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/geometry/size.h"
28 #include "ui/gfx/gpu_memory_buffer.h"
29 #include "ui/gfx/native_widget_types.h"
30 #include "ui/gfx/swap_result.h"
31 #include "ui/gl/gl_surface.h"
32 #include "ui/gl/gpu_preference.h"
33 #include "url/gurl.h"
35 namespace gpu {
36 struct Mailbox;
37 class ValueStateMap;
38 namespace gles2 {
39 class MailboxManager;
40 class SubscriptionRefSet;
44 namespace content {
46 class GpuChannel;
47 class GpuVideoDecodeAccelerator;
48 class GpuVideoEncodeAccelerator;
49 class GpuWatchdog;
50 struct WaitForCommandState;
52 class GpuCommandBufferStub
53 : public GpuMemoryManagerClient,
54 public IPC::Listener,
55 public IPC::Sender,
56 public base::SupportsWeakPtr<GpuCommandBufferStub> {
57 public:
58 class DestructionObserver {
59 public:
60 // Called in Destroy(), before the context/surface are released.
61 virtual void OnWillDestroyStub() = 0;
63 protected:
64 virtual ~DestructionObserver() {}
67 typedef base::Callback<void(const std::vector<ui::LatencyInfo>&)>
68 LatencyInfoCallback;
70 GpuCommandBufferStub(
71 GpuChannel* channel,
72 GpuCommandBufferStub* share_group,
73 const gfx::GLSurfaceHandle& handle,
74 gpu::gles2::MailboxManager* mailbox_manager,
75 gpu::gles2::SubscriptionRefSet* subscription_ref_set,
76 gpu::ValueStateMap* pending_valuebuffer_state,
77 const gfx::Size& size,
78 const gpu::gles2::DisallowedFeatures& disallowed_features,
79 const std::vector<int32>& attribs,
80 gfx::GpuPreference gpu_preference,
81 bool use_virtualized_gl_context,
82 int32 route_id,
83 int32 surface_id,
84 GpuWatchdog* watchdog,
85 bool software,
86 const GURL& active_url);
88 ~GpuCommandBufferStub() override;
90 // IPC::Listener implementation:
91 bool OnMessageReceived(const IPC::Message& message) override;
93 // IPC::Sender implementation:
94 bool Send(IPC::Message* msg) override;
96 // GpuMemoryManagerClient implementation:
97 gfx::Size GetSurfaceSize() const override;
98 gpu::gles2::MemoryTracker* GetMemoryTracker() const override;
99 void SetMemoryAllocation(const gpu::MemoryAllocation& allocation) override;
100 void SuggestHaveFrontBuffer(bool suggest_have_frontbuffer) override;
101 bool GetTotalGpuMemory(uint64* bytes) override;
103 // Whether this command buffer can currently handle IPC messages.
104 bool IsScheduled();
106 // If the command buffer is pre-empted and cannot process commands.
107 bool IsPreempted() const {
108 return scheduler_.get() && scheduler_->IsPreempted();
111 // Whether there are commands in the buffer that haven't been processed.
112 bool HasUnprocessedCommands();
114 gpu::gles2::GLES2Decoder* decoder() const { return decoder_.get(); }
115 gpu::GpuScheduler* scheduler() const { return scheduler_.get(); }
116 GpuChannel* channel() const { return channel_; }
118 // Identifies the target surface.
119 int32 surface_id() const { return surface_id_; }
121 // Identifies the various GpuCommandBufferStubs in the GPU process belonging
122 // to the same renderer process.
123 int32 route_id() const { return route_id_; }
125 gfx::GpuPreference gpu_preference() { return gpu_preference_; }
127 int32 GetRequestedAttribute(int attr) const;
129 // Sends a message to the console.
130 void SendConsoleMessage(int32 id, const std::string& message);
132 void SendCachedShader(const std::string& key, const std::string& shader);
134 gfx::GLSurface* surface() const { return surface_.get(); }
136 void AddDestructionObserver(DestructionObserver* observer);
137 void RemoveDestructionObserver(DestructionObserver* observer);
139 // Associates a sync point to this stub. When the stub is destroyed, it will
140 // retire all sync points that haven't been previously retired.
141 void AddSyncPoint(uint32 sync_point);
143 void SetPreemptByFlag(scoped_refptr<gpu::PreemptionFlag> flag);
145 void SetLatencyInfoCallback(const LatencyInfoCallback& callback);
147 void MarkContextLost();
149 const gpu::gles2::FeatureInfo* GetFeatureInfo() const;
151 uint64 GetMemoryUsage() const;
153 void SendSwapBuffersCompleted(
154 const std::vector<ui::LatencyInfo>& latency_info,
155 gfx::SwapResult result);
156 void SendUpdateVSyncParameters(base::TimeTicks timebase,
157 base::TimeDelta interval);
159 private:
160 GpuMemoryManager* GetMemoryManager() const;
161 bool MakeCurrent();
162 void Destroy();
164 // Cleans up and sends reply if OnInitialize failed.
165 void OnInitializeFailed(IPC::Message* reply_message);
167 // Message handlers:
168 void OnInitialize(base::SharedMemoryHandle shared_state_shm,
169 IPC::Message* reply_message);
170 void OnSetGetBuffer(int32 shm_id, IPC::Message* reply_message);
171 void OnProduceFrontBuffer(const gpu::Mailbox& mailbox);
172 void OnGetState(IPC::Message* reply_message);
173 void OnWaitForTokenInRange(int32 start,
174 int32 end,
175 IPC::Message* reply_message);
176 void OnWaitForGetOffsetInRange(int32 start,
177 int32 end,
178 IPC::Message* reply_message);
179 void OnAsyncFlush(int32 put_offset, uint32 flush_count,
180 const std::vector<ui::LatencyInfo>& latency_info);
181 void OnRescheduled();
182 void OnRegisterTransferBuffer(int32 id,
183 base::SharedMemoryHandle transfer_buffer,
184 uint32 size);
185 void OnDestroyTransferBuffer(int32 id);
186 void OnGetTransferBuffer(int32 id, IPC::Message* reply_message);
188 void OnCreateVideoDecoder(media::VideoCodecProfile profile,
189 int32 route_id,
190 IPC::Message* reply_message);
191 void OnCreateVideoEncoder(media::VideoPixelFormat input_format,
192 const gfx::Size& input_visible_size,
193 media::VideoCodecProfile output_profile,
194 uint32 initial_bitrate,
195 int32 route_id,
196 IPC::Message* reply_message);
198 void OnSetSurfaceVisible(bool visible);
200 void OnEnsureBackbuffer();
202 void OnRetireSyncPoint(uint32 sync_point);
203 bool OnWaitSyncPoint(uint32 sync_point);
204 void OnWaitSyncPointCompleted(uint32 sync_point);
205 void OnSignalSyncPoint(uint32 sync_point, uint32 id);
206 void OnSignalSyncPointAck(uint32 id);
207 void OnSignalQuery(uint32 query, uint32 id);
209 void OnSetClientHasMemoryAllocationChangedCallback(bool has_callback);
211 void OnCreateImage(int32 id,
212 gfx::GpuMemoryBufferHandle handle,
213 gfx::Size size,
214 gfx::GpuMemoryBuffer::Format format,
215 uint32 internalformat);
216 void OnDestroyImage(int32 id);
218 void OnCommandProcessed();
219 void OnParseError();
220 void OnCreateStreamTexture(
221 uint32 texture_id, int32 stream_id, bool* succeeded);
223 void ReportState();
225 // Wrapper for GpuScheduler::PutChanged that sets the crash report URL.
226 void PutChanged();
228 // Poll the command buffer to execute work.
229 void PollWork();
231 // Whether this command buffer needs to be polled again in the future.
232 bool HasMoreWork();
234 void ScheduleDelayedWork(int64 delay);
236 bool CheckContextLost();
237 void CheckCompleteWaits();
238 void PullTextureUpdates(uint32 sync_point);
240 // The lifetime of objects of this class is managed by a GpuChannel. The
241 // GpuChannels destroy all the GpuCommandBufferStubs that they own when they
242 // are destroyed. So a raw pointer is safe.
243 GpuChannel* channel_;
245 // The group of contexts that share namespaces with this context.
246 scoped_refptr<gpu::gles2::ContextGroup> context_group_;
248 gfx::GLSurfaceHandle handle_;
249 gfx::Size initial_size_;
250 gpu::gles2::DisallowedFeatures disallowed_features_;
251 std::vector<int32> requested_attribs_;
252 gfx::GpuPreference gpu_preference_;
253 bool use_virtualized_gl_context_;
254 int32 route_id_;
255 int32 surface_id_;
256 bool software_;
257 uint32 last_flush_count_;
259 scoped_ptr<gpu::CommandBufferService> command_buffer_;
260 scoped_ptr<gpu::gles2::GLES2Decoder> decoder_;
261 scoped_ptr<gpu::GpuScheduler> scheduler_;
262 scoped_refptr<gfx::GLSurface> surface_;
264 scoped_ptr<GpuMemoryManagerClientState> memory_manager_client_state_;
265 // The last memory allocation received from the GpuMemoryManager (used to
266 // elide redundant work).
267 bool last_memory_allocation_valid_;
268 gpu::MemoryAllocation last_memory_allocation_;
270 GpuWatchdog* watchdog_;
272 base::ObserverList<DestructionObserver> destruction_observers_;
274 // A queue of sync points associated with this stub.
275 std::deque<uint32> sync_points_;
276 int sync_point_wait_count_;
278 bool delayed_work_scheduled_;
279 uint64 previous_messages_processed_;
280 base::TimeTicks last_idle_time_;
282 scoped_refptr<gpu::PreemptionFlag> preemption_flag_;
284 LatencyInfoCallback latency_info_callback_;
286 GURL active_url_;
287 size_t active_url_hash_;
289 size_t total_gpu_memory_;
290 scoped_ptr<WaitForCommandState> wait_for_token_;
291 scoped_ptr<WaitForCommandState> wait_for_get_offset_;
293 DISALLOW_COPY_AND_ASSIGN(GpuCommandBufferStub);
296 } // namespace content
298 #endif // CONTENT_COMMON_GPU_GPU_COMMAND_BUFFER_STUB_H_