Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(112)

Side by Side Diff: content/common/gpu/gpu_channel.h

Issue 1845563005: Refactor content/common/gpu into gpu/ipc/service (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix gn libs defs Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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_CHANNEL_H_
6 #define CONTENT_COMMON_GPU_GPU_CHANNEL_H_
7
8 #include <stddef.h>
9 #include <stdint.h>
10
11 #include <string>
12
13 #include "base/containers/hash_tables.h"
14 #include "base/containers/scoped_ptr_hash_map.h"
15 #include "base/macros.h"
16 #include "base/memory/ref_counted.h"
17 #include "base/memory/scoped_ptr.h"
18 #include "base/memory/weak_ptr.h"
19 #include "base/process/process.h"
20 #include "base/threading/thread_checker.h"
21 #include "base/trace_event/memory_dump_provider.h"
22 #include "build/build_config.h"
23 #include "content/common/content_export.h"
24 #include "content/common/gpu/gpu_command_buffer_stub.h"
25 #include "content/common/gpu/gpu_memory_manager.h"
26 #include "gpu/command_buffer/service/valuebuffer_manager.h"
27 #include "gpu/ipc/common/gpu_stream_constants.h"
28 #include "ipc/ipc_sync_channel.h"
29 #include "ipc/message_router.h"
30 #include "ui/gfx/geometry/size.h"
31 #include "ui/gfx/native_widget_types.h"
32 #include "ui/gl/gl_share_group.h"
33 #include "ui/gl/gpu_preference.h"
34
35 struct GPUCreateCommandBufferConfig;
36
37 namespace base {
38 class WaitableEvent;
39 }
40
41 namespace gpu {
42 class PreemptionFlag;
43 class SyncPointOrderData;
44 class SyncPointManager;
45 union ValueState;
46 class ValueStateMap;
47 namespace gles2 {
48 class SubscriptionRefSet;
49 }
50 }
51
52 namespace IPC {
53 class MessageFilter;
54 }
55
56 namespace content {
57 class GpuChannelManager;
58 class GpuChannelMessageFilter;
59 class GpuChannelMessageQueue;
60 class GpuWatchdog;
61
62 // Encapsulates an IPC channel between the GPU process and one renderer
63 // process. On the renderer side there's a corresponding GpuChannelHost.
64 class CONTENT_EXPORT GpuChannel
65 : public IPC::Listener,
66 public IPC::Sender,
67 public gpu::gles2::SubscriptionRefSet::Observer {
68 public:
69 // Takes ownership of the renderer process handle.
70 GpuChannel(GpuChannelManager* gpu_channel_manager,
71 gpu::SyncPointManager* sync_point_manager,
72 GpuWatchdog* watchdog,
73 gfx::GLShareGroup* share_group,
74 gpu::gles2::MailboxManager* mailbox_manager,
75 gpu::PreemptionFlag* preempting_flag,
76 gpu::PreemptionFlag* preempted_flag,
77 base::SingleThreadTaskRunner* task_runner,
78 base::SingleThreadTaskRunner* io_task_runner,
79 int32_t client_id,
80 uint64_t client_tracing_id,
81 bool allow_view_command_buffers,
82 bool allow_real_time_streams);
83 ~GpuChannel() override;
84
85 // Initializes the IPC channel. Caller takes ownership of the client FD in
86 // the returned handle and is responsible for closing it.
87 virtual IPC::ChannelHandle Init(base::WaitableEvent* shutdown_event);
88
89 void SetUnhandledMessageListener(IPC::Listener* listener);
90
91 // Get the GpuChannelManager that owns this channel.
92 GpuChannelManager* gpu_channel_manager() const {
93 return gpu_channel_manager_;
94 }
95
96 const std::string& channel_id() const { return channel_id_; }
97
98 virtual base::ProcessId GetClientPID() const;
99
100 int client_id() const { return client_id_; }
101
102 uint64_t client_tracing_id() const { return client_tracing_id_; }
103
104 base::WeakPtr<GpuChannel> AsWeakPtr();
105
106 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner() const {
107 return io_task_runner_;
108 }
109
110 // IPC::Listener implementation:
111 bool OnMessageReceived(const IPC::Message& msg) override;
112 void OnChannelError() override;
113
114 // IPC::Sender implementation:
115 bool Send(IPC::Message* msg) override;
116
117 // SubscriptionRefSet::Observer implementation
118 void OnAddSubscription(unsigned int target) override;
119 void OnRemoveSubscription(unsigned int target) override;
120
121 void OnStreamRescheduled(int32_t stream_id, bool scheduled);
122
123 gfx::GLShareGroup* share_group() const { return share_group_.get(); }
124
125 GpuCommandBufferStub* LookupCommandBuffer(int32_t route_id);
126
127 void LoseAllContexts();
128 void MarkAllContextsLost();
129
130 // Called to add a listener for a particular message routing ID.
131 // Returns true if succeeded.
132 bool AddRoute(int32_t route_id, int32_t stream_id, IPC::Listener* listener);
133
134 // Called to remove a listener for a particular message routing ID.
135 void RemoveRoute(int32_t route_id);
136
137 void CacheShader(const std::string& key, const std::string& shader);
138
139 void AddFilter(IPC::MessageFilter* filter);
140 void RemoveFilter(IPC::MessageFilter* filter);
141
142 uint64_t GetMemoryUsage();
143
144 scoped_refptr<gl::GLImage> CreateImageForGpuMemoryBuffer(
145 const gfx::GpuMemoryBufferHandle& handle,
146 const gfx::Size& size,
147 gfx::BufferFormat format,
148 uint32_t internalformat);
149
150 void HandleUpdateValueState(unsigned int target,
151 const gpu::ValueState& state);
152
153 GpuChannelMessageFilter* filter() const { return filter_.get(); }
154
155 // Visible for testing.
156 const gpu::ValueStateMap* pending_valuebuffer_state() const {
157 return pending_valuebuffer_state_.get();
158 }
159
160 // Returns the global order number for the last processed IPC message.
161 uint32_t GetProcessedOrderNum() const;
162
163 // Returns the global order number for the last unprocessed IPC message.
164 uint32_t GetUnprocessedOrderNum() const;
165
166 // Returns the shared sync point global order data for the stream.
167 scoped_refptr<gpu::SyncPointOrderData> GetSyncPointOrderData(
168 int32_t stream_id);
169
170 void PostHandleOutOfOrderMessage(const IPC::Message& message);
171 void PostHandleMessage(const scoped_refptr<GpuChannelMessageQueue>& queue);
172
173 // Synchronously handle the message to make testing convenient.
174 void HandleMessageForTesting(const IPC::Message& msg);
175
176 #if defined(OS_ANDROID)
177 const GpuCommandBufferStub* GetOneStub() const;
178 #endif
179
180 protected:
181 // The message filter on the io thread.
182 scoped_refptr<GpuChannelMessageFilter> filter_;
183
184 // Map of routing id to command buffer stub.
185 base::ScopedPtrHashMap<int32_t, scoped_ptr<GpuCommandBufferStub>> stubs_;
186
187 private:
188 bool OnControlMessageReceived(const IPC::Message& msg);
189
190 void HandleMessage(const scoped_refptr<GpuChannelMessageQueue>& queue);
191
192 // Some messages such as WaitForGetOffsetInRange and WaitForTokenInRange are
193 // processed as soon as possible because the client is blocked until they
194 // are completed.
195 void HandleOutOfOrderMessage(const IPC::Message& msg);
196
197 void HandleMessageHelper(const IPC::Message& msg);
198
199 scoped_refptr<GpuChannelMessageQueue> CreateStream(
200 int32_t stream_id,
201 gpu::GpuStreamPriority stream_priority);
202
203 scoped_refptr<GpuChannelMessageQueue> LookupStream(int32_t stream_id);
204
205 void DestroyStreamIfNecessary(
206 const scoped_refptr<GpuChannelMessageQueue>& queue);
207
208 void AddRouteToStream(int32_t route_id, int32_t stream_id);
209 void RemoveRouteFromStream(int32_t route_id);
210
211 // Message handlers for control messages.
212 void OnCreateCommandBuffer(gpu::SurfaceHandle surface_handle,
213 const gfx::Size& size,
214 const GPUCreateCommandBufferConfig& init_params,
215 int32_t route_id,
216 bool* succeeded);
217 void OnDestroyCommandBuffer(int32_t route_id);
218
219
220 // The lifetime of objects of this class is managed by a GpuChannelManager.
221 // The GpuChannelManager destroy all the GpuChannels that they own when they
222 // are destroyed. So a raw pointer is safe.
223 GpuChannelManager* const gpu_channel_manager_;
224
225 // Sync point manager. Outlives the channel and is guaranteed to outlive the
226 // message loop.
227 gpu::SyncPointManager* const sync_point_manager_;
228
229 scoped_ptr<IPC::SyncChannel> channel_;
230
231 IPC::Listener* unhandled_message_listener_;
232
233 // Uniquely identifies the channel within this GPU process.
234 std::string channel_id_;
235
236 // Used to implement message routing functionality to CommandBuffer objects
237 IPC::MessageRouter router_;
238
239 // Whether the processing of IPCs on this channel is stalled and we should
240 // preempt other GpuChannels.
241 scoped_refptr<gpu::PreemptionFlag> preempting_flag_;
242
243 // If non-NULL, all stubs on this channel should stop processing GL
244 // commands (via their CommandExecutor) when preempted_flag_->IsSet()
245 scoped_refptr<gpu::PreemptionFlag> preempted_flag_;
246
247 // The id of the client who is on the other side of the channel.
248 const int32_t client_id_;
249
250 // The tracing ID used for memory allocations associated with this client.
251 const uint64_t client_tracing_id_;
252
253 // The task runners for the main thread and the io thread.
254 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
255 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_;
256
257 // The share group that all contexts associated with a particular renderer
258 // process use.
259 scoped_refptr<gfx::GLShareGroup> share_group_;
260
261 scoped_refptr<gpu::gles2::MailboxManager> mailbox_manager_;
262
263 scoped_refptr<gpu::gles2::SubscriptionRefSet> subscription_ref_set_;
264
265 scoped_refptr<gpu::ValueStateMap> pending_valuebuffer_state_;
266
267 gpu::gles2::DisallowedFeatures disallowed_features_;
268 GpuWatchdog* const watchdog_;
269
270 // Map of stream id to appropriate message queue.
271 base::hash_map<int32_t, scoped_refptr<GpuChannelMessageQueue>> streams_;
272
273 // Multimap of stream id to route ids.
274 base::hash_map<int32_t, int> streams_to_num_routes_;
275
276 // Map of route id to stream id;
277 base::hash_map<int32_t, int32_t> routes_to_streams_;
278
279 // Can view command buffers be created on this channel.
280 const bool allow_view_command_buffers_;
281
282 // Can real time streams be created on this channel.
283 const bool allow_real_time_streams_;
284
285 // Member variables should appear before the WeakPtrFactory, to ensure
286 // that any WeakPtrs to Controller are invalidated before its members
287 // variable's destructors are executed, rendering them invalid.
288 base::WeakPtrFactory<GpuChannel> weak_factory_;
289
290 DISALLOW_COPY_AND_ASSIGN(GpuChannel);
291 };
292
293 // This filter does three things:
294 // - it counts and timestamps each message forwarded to the channel
295 // so that we can preempt other channels if a message takes too long to
296 // process. To guarantee fairness, we must wait a minimum amount of time
297 // before preempting and we limit the amount of time that we can preempt in
298 // one shot (see constants above).
299 // - it handles the GpuCommandBufferMsg_InsertSyncPoint message on the IO
300 // thread, generating the sync point ID and responding immediately, and then
301 // posting a task to insert the GpuCommandBufferMsg_RetireSyncPoint message
302 // into the channel's queue.
303 // - it generates mailbox names for clients of the GPU process on the IO thread.
304 class GpuChannelMessageFilter : public IPC::MessageFilter {
305 public:
306 GpuChannelMessageFilter();
307
308 // IPC::MessageFilter implementation.
309 void OnFilterAdded(IPC::Sender* sender) override;
310 void OnFilterRemoved() override;
311 void OnChannelConnected(int32_t peer_pid) override;
312 void OnChannelError() override;
313 void OnChannelClosing() override;
314 bool OnMessageReceived(const IPC::Message& message) override;
315
316 void AddChannelFilter(scoped_refptr<IPC::MessageFilter> filter);
317 void RemoveChannelFilter(scoped_refptr<IPC::MessageFilter> filter);
318
319 void AddRoute(int32_t route_id,
320 const scoped_refptr<GpuChannelMessageQueue>& queue);
321 void RemoveRoute(int32_t route_id);
322
323 bool Send(IPC::Message* message);
324
325 protected:
326 ~GpuChannelMessageFilter() override;
327
328 private:
329 scoped_refptr<GpuChannelMessageQueue> LookupStreamByRoute(int32_t route_id);
330
331 bool MessageErrorHandler(const IPC::Message& message, const char* error_msg);
332
333 // Map of route id to message queue.
334 base::hash_map<int32_t, scoped_refptr<GpuChannelMessageQueue>> routes_;
335 base::Lock routes_lock_; // Protects |routes_|.
336
337 IPC::Sender* sender_;
338 base::ProcessId peer_pid_;
339 std::vector<scoped_refptr<IPC::MessageFilter>> channel_filters_;
340
341 DISALLOW_COPY_AND_ASSIGN(GpuChannelMessageFilter);
342 };
343
344 struct GpuChannelMessage {
345 IPC::Message message;
346 uint32_t order_number;
347 base::TimeTicks time_received;
348
349 GpuChannelMessage(const IPC::Message& msg,
350 uint32_t order_num,
351 base::TimeTicks ts)
352 : message(msg), order_number(order_num), time_received(ts) {}
353
354 private:
355 DISALLOW_COPY_AND_ASSIGN(GpuChannelMessage);
356 };
357
358 class GpuChannelMessageQueue
359 : public base::RefCountedThreadSafe<GpuChannelMessageQueue> {
360 public:
361 static scoped_refptr<GpuChannelMessageQueue> Create(
362 int32_t stream_id,
363 gpu::GpuStreamPriority stream_priority,
364 GpuChannel* channel,
365 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner,
366 const scoped_refptr<gpu::PreemptionFlag>& preempting_flag,
367 const scoped_refptr<gpu::PreemptionFlag>& preempted_flag,
368 gpu::SyncPointManager* sync_point_manager);
369
370 void Disable();
371 void DisableIO();
372
373 int32_t stream_id() const { return stream_id_; }
374 gpu::GpuStreamPriority stream_priority() const { return stream_priority_; }
375
376 bool IsScheduled() const;
377 void OnRescheduled(bool scheduled);
378
379 bool HasQueuedMessages() const;
380
381 base::TimeTicks GetNextMessageTimeTick() const;
382
383 scoped_refptr<gpu::SyncPointOrderData> GetSyncPointOrderData();
384
385 // Returns the global order number for the last unprocessed IPC message.
386 uint32_t GetUnprocessedOrderNum() const;
387
388 // Returns the global order number for the last unprocessed IPC message.
389 uint32_t GetProcessedOrderNum() const;
390
391 // Should be called before a message begins to be processed. Returns false if
392 // there are no messages to process.
393 const GpuChannelMessage* BeginMessageProcessing();
394 // Should be called if a message began processing but did not finish.
395 void PauseMessageProcessing();
396 // Should be called if a message is completely processed. Returns true if
397 // there are more messages to process.
398 void FinishMessageProcessing();
399
400 bool PushBackMessage(const IPC::Message& message);
401
402 private:
403 enum PreemptionState {
404 // Either there's no other channel to preempt, there are no messages
405 // pending processing, or we just finished preempting and have to wait
406 // before preempting again.
407 IDLE,
408 // We are waiting kPreemptWaitTimeMs before checking if we should preempt.
409 WAITING,
410 // We can preempt whenever any IPC processing takes more than
411 // kPreemptWaitTimeMs.
412 CHECKING,
413 // We are currently preempting (i.e. no stub is descheduled).
414 PREEMPTING,
415 // We would like to preempt, but some stub is descheduled.
416 WOULD_PREEMPT_DESCHEDULED,
417 };
418
419 friend class base::RefCountedThreadSafe<GpuChannelMessageQueue>;
420
421 GpuChannelMessageQueue(
422 int32_t stream_id,
423 gpu::GpuStreamPriority stream_priority,
424 GpuChannel* channel,
425 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner,
426 const scoped_refptr<gpu::PreemptionFlag>& preempting_flag,
427 const scoped_refptr<gpu::PreemptionFlag>& preempted_flag,
428 gpu::SyncPointManager* sync_point_manager);
429 ~GpuChannelMessageQueue();
430
431 void UpdatePreemptionState();
432 void UpdatePreemptionStateHelper();
433
434 void UpdateStateIdle();
435 void UpdateStateWaiting();
436 void UpdateStateChecking();
437 void UpdateStatePreempting();
438 void UpdateStateWouldPreemptDescheduled();
439
440 void TransitionToIdle();
441 void TransitionToWaiting();
442 void TransitionToChecking();
443 void TransitionToPreempting();
444 void TransitionToWouldPreemptDescheduled();
445
446 bool ShouldTransitionToIdle() const;
447
448 const int32_t stream_id_;
449 const gpu::GpuStreamPriority stream_priority_;
450
451 // These can be accessed from both IO and main threads and are protected by
452 // |channel_lock_|.
453 bool enabled_;
454 bool scheduled_;
455 GpuChannel* const channel_;
456 std::deque<scoped_ptr<GpuChannelMessage>> channel_messages_;
457 mutable base::Lock channel_lock_;
458
459 // The following are accessed on the IO thread only.
460 // No lock is necessary for preemption state because it's only accessed on the
461 // IO thread.
462 PreemptionState preemption_state_;
463 // Maximum amount of time that we can spend in PREEMPTING.
464 // It is reset when we transition to IDLE.
465 base::TimeDelta max_preemption_time_;
466 // This timer is used and runs tasks on the IO thread.
467 scoped_ptr<base::OneShotTimer> timer_;
468 base::ThreadChecker io_thread_checker_;
469
470 // Keeps track of sync point related state such as message order numbers.
471 scoped_refptr<gpu::SyncPointOrderData> sync_point_order_data_;
472
473 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_;
474 scoped_refptr<gpu::PreemptionFlag> preempting_flag_;
475 scoped_refptr<gpu::PreemptionFlag> preempted_flag_;
476 gpu::SyncPointManager* const sync_point_manager_;
477
478 DISALLOW_COPY_AND_ASSIGN(GpuChannelMessageQueue);
479 };
480
481 } // namespace content
482
483 #endif // CONTENT_COMMON_GPU_GPU_CHANNEL_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698