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

Side by Side Diff: gpu/ipc/service/gpu_channel.h

Issue 2772513005: gpu: Move out of order / control message handling to message filter. (Closed)
Patch Set: better dchecks Created 3 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
« no previous file with comments | « no previous file | gpu/ipc/service/gpu_channel.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef GPU_IPC_SERVICE_GPU_CHANNEL_H_ 5 #ifndef GPU_IPC_SERVICE_GPU_CHANNEL_H_
6 #define GPU_IPC_SERVICE_GPU_CHANNEL_H_ 6 #define GPU_IPC_SERVICE_GPU_CHANNEL_H_
7 7
8 #include <stddef.h> 8 #include <stddef.h>
9 #include <stdint.h> 9 #include <stdint.h>
10 10
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after
151 151
152 scoped_refptr<gl::GLImage> CreateImageForGpuMemoryBuffer( 152 scoped_refptr<gl::GLImage> CreateImageForGpuMemoryBuffer(
153 const gfx::GpuMemoryBufferHandle& handle, 153 const gfx::GpuMemoryBufferHandle& handle,
154 const gfx::Size& size, 154 const gfx::Size& size,
155 gfx::BufferFormat format, 155 gfx::BufferFormat format,
156 uint32_t internalformat, 156 uint32_t internalformat,
157 SurfaceHandle surface_handle); 157 SurfaceHandle surface_handle);
158 158
159 GpuChannelMessageFilter* filter() const { return filter_.get(); } 159 GpuChannelMessageFilter* filter() const { return filter_.get(); }
160 160
161 void PostHandleOutOfOrderMessage(const IPC::Message& message); 161 // Handle messages enqueued in |message_queue_|.
162 void PostHandleMessage(); 162 void HandleMessageOnQueue();
163
164 // Some messages such as WaitForGetOffsetInRange and WaitForTokenInRange are
165 // processed as soon as possible because the client is blocked until they
166 // are completed.
167 void HandleOutOfOrderMessage(const IPC::Message& msg);
163 168
164 // Synchronously handle the message to make testing convenient. 169 // Synchronously handle the message to make testing convenient.
165 void HandleMessageForTesting(const IPC::Message& msg); 170 void HandleMessageForTesting(const IPC::Message& msg);
166 171
167 #if defined(OS_ANDROID) 172 #if defined(OS_ANDROID)
168 const GpuCommandBufferStub* GetOneStub() const; 173 const GpuCommandBufferStub* GetOneStub() const;
169 #endif 174 #endif
170 175
171 protected: 176 protected:
172 scoped_refptr<GpuChannelMessageQueue> message_queue_; 177 scoped_refptr<GpuChannelMessageQueue> message_queue_;
173 178
174 // The message filter on the io thread. 179 // The message filter on the io thread.
175 scoped_refptr<GpuChannelMessageFilter> filter_; 180 scoped_refptr<GpuChannelMessageFilter> filter_;
176 181
177 // Map of routing id to command buffer stub. 182 // Map of routing id to command buffer stub.
178 std::unordered_map<int32_t, std::unique_ptr<GpuCommandBufferStub>> stubs_; 183 std::unordered_map<int32_t, std::unique_ptr<GpuCommandBufferStub>> stubs_;
179 184
180 private: 185 private:
181 friend class TestGpuChannel; 186 friend class TestGpuChannel;
182 187
183 bool OnControlMessageReceived(const IPC::Message& msg); 188 bool OnControlMessageReceived(const IPC::Message& msg);
184 189
185 void HandleMessage();
186
187 // Some messages such as WaitForGetOffsetInRange and WaitForTokenInRange are
188 // processed as soon as possible because the client is blocked until they
189 // are completed.
190 void HandleOutOfOrderMessage(const IPC::Message& msg);
191
192 void HandleMessageHelper(const IPC::Message& msg); 190 void HandleMessageHelper(const IPC::Message& msg);
193 191
194 // Message handlers for control messages. 192 // Message handlers for control messages.
195 void OnCreateCommandBuffer(const GPUCreateCommandBufferConfig& init_params, 193 void OnCreateCommandBuffer(const GPUCreateCommandBufferConfig& init_params,
196 int32_t route_id, 194 int32_t route_id,
197 base::SharedMemoryHandle shared_state_shm, 195 base::SharedMemoryHandle shared_state_shm,
198 bool* result, 196 bool* result,
199 gpu::Capabilities* capabilities); 197 gpu::Capabilities* capabilities);
200 void OnDestroyCommandBuffer(int32_t route_id); 198 void OnDestroyCommandBuffer(int32_t route_id);
201 void OnGetDriverBugWorkArounds( 199 void OnGetDriverBugWorkArounds(
202 std::vector<std::string>* gpu_driver_bug_workarounds); 200 std::vector<std::string>* gpu_driver_bug_workarounds);
203 201
204 std::unique_ptr<GpuCommandBufferStub> CreateCommandBuffer( 202 std::unique_ptr<GpuCommandBufferStub> CreateCommandBuffer(
205 const GPUCreateCommandBufferConfig& init_params, 203 const GPUCreateCommandBufferConfig& init_params,
206 int32_t route_id, 204 int32_t route_id,
207 std::unique_ptr<base::SharedMemory> shared_state_shm); 205 std::unique_ptr<base::SharedMemory> shared_state_shm);
208 206
209 // The lifetime of objects of this class is managed by a GpuChannelManager. 207 // The lifetime of objects of this class is managed by a GpuChannelManager.
210 // The GpuChannelManager destroy all the GpuChannels that they own when they 208 // The GpuChannelManager destroy all the GpuChannels that they own when they
211 // are destroyed. So a raw pointer is safe. 209 // are destroyed. So a raw pointer is safe.
212 GpuChannelManager* const gpu_channel_manager_; 210 GpuChannelManager* const gpu_channel_manager_;
213 211
214 // Sync point manager. Outlives the channel and is guaranteed to outlive the 212 // Sync point manager. Outlives the channel and is guaranteed to outlive the
215 // message loop. 213 // message loop.
216 SyncPointManager* const sync_point_manager_; 214 SyncPointManager* const sync_point_manager_;
217 215
218 std::unique_ptr<IPC::SyncChannel> channel_; 216 std::unique_ptr<IPC::SyncChannel> channel_;
219 217
220 IPC::Listener* unhandled_message_listener_; 218 IPC::Listener* unhandled_message_listener_ = nullptr;
221 219
222 // Used to implement message routing functionality to CommandBuffer objects 220 // Used to implement message routing functionality to CommandBuffer objects
223 IPC::MessageRouter router_; 221 IPC::MessageRouter router_;
224 222
225 // Whether the processing of IPCs on this channel is stalled and we should 223 // Whether the processing of IPCs on this channel is stalled and we should
226 // preempt other GpuChannels. 224 // preempt other GpuChannels.
227 scoped_refptr<PreemptionFlag> preempting_flag_; 225 scoped_refptr<PreemptionFlag> preempting_flag_;
228 226
229 // If non-NULL, all stubs on this channel should stop processing GL 227 // If non-NULL, all stubs on this channel should stop processing GL
230 // commands (via their CommandExecutor) when preempted_flag_->IsSet() 228 // commands (via their CommandExecutor) when preempted_flag_->IsSet()
(...skipping 16 matching lines...) Expand all
247 scoped_refptr<gles2::MailboxManager> mailbox_manager_; 245 scoped_refptr<gles2::MailboxManager> mailbox_manager_;
248 246
249 GpuWatchdogThread* const watchdog_; 247 GpuWatchdogThread* const watchdog_;
250 248
251 // Can view command buffers be created on this channel. 249 // Can view command buffers be created on this channel.
252 const bool allow_view_command_buffers_; 250 const bool allow_view_command_buffers_;
253 251
254 // Can real time streams be created on this channel. 252 // Can real time streams be created on this channel.
255 const bool allow_real_time_streams_; 253 const bool allow_real_time_streams_;
256 254
257 base::ProcessId peer_pid_; 255 base::ProcessId peer_pid_ = base::kNullProcessId;
258 256
259 // Member variables should appear before the WeakPtrFactory, to ensure 257 // Member variables should appear before the WeakPtrFactory, to ensure that
260 // that any WeakPtrs to Controller are invalidated before its members 258 // any WeakPtrs to Controller are invalidated before its members variable's
261 // variable's destructors are executed, rendering them invalid. 259 // destructors are executed, rendering them invalid.
262 base::WeakPtrFactory<GpuChannel> weak_factory_; 260 base::WeakPtrFactory<GpuChannel> weak_factory_;
263 261
264 DISALLOW_COPY_AND_ASSIGN(GpuChannel); 262 DISALLOW_COPY_AND_ASSIGN(GpuChannel);
265 }; 263 };
266 264
267 // This filter does three things: 265 // This filter does three things:
268 // - it counts and timestamps each message forwarded to the channel 266 // - it counts and timestamps each message forwarded to the channel
269 // so that we can preempt other channels if a message takes too long to 267 // so that we can preempt other channels if a message takes too long to
270 // process. To guarantee fairness, we must wait a minimum amount of time 268 // process. To guarantee fairness, we must wait a minimum amount of time
271 // before preempting and we limit the amount of time that we can preempt in 269 // before preempting and we limit the amount of time that we can preempt in
272 // one shot (see constants above). 270 // one shot (see constants above).
273 // - it handles the GpuCommandBufferMsg_InsertSyncPoint message on the IO 271 // - it handles the GpuCommandBufferMsg_InsertSyncPoint message on the IO
274 // thread, generating the sync point ID and responding immediately, and then 272 // thread, generating the sync point ID and responding immediately, and then
275 // posting a task to insert the GpuCommandBufferMsg_RetireSyncPoint message 273 // posting a task to insert the GpuCommandBufferMsg_RetireSyncPoint message
276 // into the channel's queue. 274 // into the channel's queue.
277 // - it generates mailbox names for clients of the GPU process on the IO thread. 275 // - it generates mailbox names for clients of the GPU process on the IO thread.
278 class GPU_EXPORT GpuChannelMessageFilter : public IPC::MessageFilter { 276 class GPU_EXPORT GpuChannelMessageFilter : public IPC::MessageFilter {
279 public: 277 public:
280 explicit GpuChannelMessageFilter( 278 GpuChannelMessageFilter(
281 scoped_refptr<GpuChannelMessageQueue> message_queue); 279 GpuChannel* gpu_channel,
280 scoped_refptr<GpuChannelMessageQueue> message_queue,
281 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner);
282
283 void Destroy();
282 284
283 // IPC::MessageFilter implementation. 285 // IPC::MessageFilter implementation.
284 void OnFilterAdded(IPC::Channel* channel) override; 286 void OnFilterAdded(IPC::Channel* channel) override;
285 void OnFilterRemoved() override; 287 void OnFilterRemoved() override;
286 void OnChannelConnected(int32_t peer_pid) override; 288 void OnChannelConnected(int32_t peer_pid) override;
287 void OnChannelError() override; 289 void OnChannelError() override;
288 void OnChannelClosing() override; 290 void OnChannelClosing() override;
289 bool OnMessageReceived(const IPC::Message& message) override; 291 bool OnMessageReceived(const IPC::Message& message) override;
290 292
291 void AddChannelFilter(scoped_refptr<IPC::MessageFilter> filter); 293 void AddChannelFilter(scoped_refptr<IPC::MessageFilter> filter);
292 void RemoveChannelFilter(scoped_refptr<IPC::MessageFilter> filter); 294 void RemoveChannelFilter(scoped_refptr<IPC::MessageFilter> filter);
293 295
294 bool Send(IPC::Message* message); 296 bool Send(IPC::Message* message);
295 297
296 protected: 298 private:
297 ~GpuChannelMessageFilter() override; 299 ~GpuChannelMessageFilter() override;
298 300
299 private:
300 bool MessageErrorHandler(const IPC::Message& message, const char* error_msg); 301 bool MessageErrorHandler(const IPC::Message& message, const char* error_msg);
301 302
303 IPC::Channel* ipc_channel_ = nullptr;
304 base::ProcessId peer_pid_ = base::kNullProcessId;
305 std::vector<scoped_refptr<IPC::MessageFilter>> channel_filters_;
306
307 GpuChannel* gpu_channel_ = nullptr;
308 base::Lock gpu_channel_lock_;
309
302 scoped_refptr<GpuChannelMessageQueue> message_queue_; 310 scoped_refptr<GpuChannelMessageQueue> message_queue_;
303 311 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner_;
304 IPC::Channel* channel_;
305 base::ProcessId peer_pid_;
306 std::vector<scoped_refptr<IPC::MessageFilter>> channel_filters_;
307 312
308 DISALLOW_COPY_AND_ASSIGN(GpuChannelMessageFilter); 313 DISALLOW_COPY_AND_ASSIGN(GpuChannelMessageFilter);
309 }; 314 };
310 315
311 struct GpuChannelMessage { 316 struct GpuChannelMessage {
312 IPC::Message message; 317 IPC::Message message;
313 uint32_t order_number; 318 uint32_t order_number;
314 base::TimeTicks time_received; 319 base::TimeTicks time_received;
315 320
316 GpuChannelMessage(const IPC::Message& msg, 321 GpuChannelMessage(const IPC::Message& msg,
317 uint32_t order_num, 322 uint32_t order_num,
318 base::TimeTicks ts) 323 base::TimeTicks ts)
319 : message(msg), order_number(order_num), time_received(ts) {} 324 : message(msg), order_number(order_num), time_received(ts) {}
320 325
321 private: 326 private:
322 DISALLOW_COPY_AND_ASSIGN(GpuChannelMessage); 327 DISALLOW_COPY_AND_ASSIGN(GpuChannelMessage);
323 }; 328 };
324 329
325 class GpuChannelMessageQueue 330 class GpuChannelMessageQueue
326 : public base::RefCountedThreadSafe<GpuChannelMessageQueue> { 331 : public base::RefCountedThreadSafe<GpuChannelMessageQueue> {
327 public: 332 public:
328 static scoped_refptr<GpuChannelMessageQueue> Create( 333 GpuChannelMessageQueue(
329 GpuChannel* channel, 334 GpuChannel* channel,
335 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner,
330 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner, 336 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner,
331 scoped_refptr<PreemptionFlag> preempting_flag, 337 scoped_refptr<PreemptionFlag> preempting_flag,
332 scoped_refptr<PreemptionFlag> preempted_flag, 338 scoped_refptr<PreemptionFlag> preempted_flag,
333 SyncPointManager* sync_point_manager); 339 SyncPointManager* sync_point_manager);
334 340
335 void Disable(); 341 void Destroy();
336 void DisableIO();
337 342
338 SequenceId sequence_id() const { 343 SequenceId sequence_id() const {
339 return sync_point_order_data_->sequence_id(); 344 return sync_point_order_data_->sequence_id();
340 } 345 }
341 346
342 bool IsScheduled() const; 347 bool IsScheduled() const;
343 void SetScheduled(bool scheduled); 348 void SetScheduled(bool scheduled);
344 349
345 bool HasQueuedMessages() const; 350 bool HasQueuedMessages() const;
346 351
347 base::TimeTicks GetNextMessageTimeTick() const;
348
349 // Should be called before a message begins to be processed. Returns false if 352 // Should be called before a message begins to be processed. Returns false if
350 // there are no messages to process. 353 // there are no messages to process.
351 const GpuChannelMessage* BeginMessageProcessing(); 354 const GpuChannelMessage* BeginMessageProcessing();
352 // Should be called if a message began processing but did not finish. 355 // Should be called if a message began processing but did not finish.
353 void PauseMessageProcessing(); 356 void PauseMessageProcessing();
354 // Should be called if a message is completely processed. Returns true if 357 // Should be called if a message is completely processed. Returns true if
355 // there are more messages to process. 358 // there are more messages to process.
356 void FinishMessageProcessing(); 359 void FinishMessageProcessing();
357 360
358 bool PushBackMessage(const IPC::Message& message); 361 void PushBackMessage(const IPC::Message& message);
359 362
360 private: 363 private:
361 enum PreemptionState { 364 enum PreemptionState {
362 // Either there's no other channel to preempt, there are no messages 365 // Either there's no other channel to preempt, there are no messages
363 // pending processing, or we just finished preempting and have to wait 366 // pending processing, or we just finished preempting and have to wait
364 // before preempting again. 367 // before preempting again.
365 IDLE, 368 IDLE,
366 // We are waiting kPreemptWaitTimeMs before checking if we should preempt. 369 // We are waiting kPreemptWaitTimeMs before checking if we should preempt.
367 WAITING, 370 WAITING,
368 // We can preempt whenever any IPC processing takes more than 371 // We can preempt whenever any IPC processing takes more than
369 // kPreemptWaitTimeMs. 372 // kPreemptWaitTimeMs.
370 CHECKING, 373 CHECKING,
371 // We are currently preempting (i.e. no stub is descheduled). 374 // We are currently preempting (i.e. no stub is descheduled).
372 PREEMPTING, 375 PREEMPTING,
373 // We would like to preempt, but some stub is descheduled. 376 // We would like to preempt, but some stub is descheduled.
374 WOULD_PREEMPT_DESCHEDULED, 377 WOULD_PREEMPT_DESCHEDULED,
375 }; 378 };
376 379
377 friend class base::RefCountedThreadSafe<GpuChannelMessageQueue>; 380 friend class base::RefCountedThreadSafe<GpuChannelMessageQueue>;
378 381
379 GpuChannelMessageQueue(
380 GpuChannel* channel,
381 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner,
382 scoped_refptr<PreemptionFlag> preempting_flag,
383 scoped_refptr<PreemptionFlag> preempted_flag,
384 SyncPointManager* sync_point_manager);
385 ~GpuChannelMessageQueue(); 382 ~GpuChannelMessageQueue();
386 383
384 void PostHandleMessageOnQueue();
385
387 void UpdatePreemptionState(); 386 void UpdatePreemptionState();
388 void UpdatePreemptionStateHelper(); 387 void UpdatePreemptionStateHelper();
389 388
390 void UpdateStateIdle(); 389 void UpdateStateIdle();
391 void UpdateStateWaiting(); 390 void UpdateStateWaiting();
392 void UpdateStateChecking(); 391 void UpdateStateChecking();
393 void UpdateStatePreempting(); 392 void UpdateStatePreempting();
394 void UpdateStateWouldPreemptDescheduled(); 393 void UpdateStateWouldPreemptDescheduled();
395 394
396 void TransitionToIdle(); 395 void TransitionToIdle();
397 void TransitionToWaiting(); 396 void TransitionToWaiting();
398 void TransitionToChecking(); 397 void TransitionToChecking();
399 void TransitionToPreempting(); 398 void TransitionToPreempting();
400 void TransitionToWouldPreemptDescheduled(); 399 void TransitionToWouldPreemptDescheduled();
401 400
402 bool ShouldTransitionToIdle() const; 401 bool ShouldTransitionToIdle() const;
403 402
404 // These can be accessed from both IO and main threads and are protected by 403 // These can be accessed from both IO and main threads and are protected by
405 // |channel_lock_|. 404 // |channel_lock_|.
406 bool enabled_; 405 bool scheduled_ = true;
407 bool scheduled_; 406 GpuChannel* channel_ = nullptr; // set to nullptr on Destroy
408 GpuChannel* const channel_;
409 std::deque<std::unique_ptr<GpuChannelMessage>> channel_messages_; 407 std::deque<std::unique_ptr<GpuChannelMessage>> channel_messages_;
408 bool handle_message_post_task_pending_ = false;
410 mutable base::Lock channel_lock_; 409 mutable base::Lock channel_lock_;
411 410
412 // The following are accessed on the IO thread only. 411 // The following are accessed on the IO thread only.
413 // No lock is necessary for preemption state because it's only accessed on the 412 // No lock is necessary for preemption state because it's only accessed on the
414 // IO thread. 413 // IO thread.
415 PreemptionState preemption_state_; 414 PreemptionState preemption_state_ = IDLE;
416 // Maximum amount of time that we can spend in PREEMPTING. 415 // Maximum amount of time that we can spend in PREEMPTING.
417 // It is reset when we transition to IDLE. 416 // It is reset when we transition to IDLE.
418 base::TimeDelta max_preemption_time_; 417 base::TimeDelta max_preemption_time_;
419 // This timer is used and runs tasks on the IO thread. 418 // This timer is used and runs tasks on the IO thread.
420 std::unique_ptr<base::OneShotTimer> timer_; 419 std::unique_ptr<base::OneShotTimer> timer_;
421 base::ThreadChecker io_thread_checker_; 420 base::ThreadChecker io_thread_checker_;
422 421
423 // Keeps track of sync point related state such as message order numbers. 422 // Keeps track of sync point related state such as message order numbers.
424 scoped_refptr<SyncPointOrderData> sync_point_order_data_; 423 scoped_refptr<SyncPointOrderData> sync_point_order_data_;
425 424
425 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner_;
426 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_; 426 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_;
427 scoped_refptr<PreemptionFlag> preempting_flag_; 427 scoped_refptr<PreemptionFlag> preempting_flag_;
428 scoped_refptr<PreemptionFlag> preempted_flag_; 428 scoped_refptr<PreemptionFlag> preempted_flag_;
429 SyncPointManager* const sync_point_manager_; 429 SyncPointManager* const sync_point_manager_;
430 430
431 DISALLOW_COPY_AND_ASSIGN(GpuChannelMessageQueue); 431 DISALLOW_COPY_AND_ASSIGN(GpuChannelMessageQueue);
432 }; 432 };
433 433
434 } // namespace gpu 434 } // namespace gpu
435 435
436 #endif // GPU_IPC_SERVICE_GPU_CHANNEL_H_ 436 #endif // GPU_IPC_SERVICE_GPU_CHANNEL_H_
OLDNEW
« no previous file with comments | « no previous file | gpu/ipc/service/gpu_channel.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698