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 GPU_COMMAND_BUFFER_SERVICE_GPU_SCHEDULER_H_
6 #define GPU_COMMAND_BUFFER_SERVICE_GPU_SCHEDULER_H_
10 #include "base/atomic_ref_count.h"
11 #include "base/atomicops.h"
12 #include "base/callback.h"
13 #include "base/memory/linked_ptr.h"
14 #include "base/memory/ref_counted.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/memory/shared_memory.h"
17 #include "base/memory/weak_ptr.h"
18 #include "gpu/command_buffer/service/cmd_buffer_engine.h"
19 #include "gpu/command_buffer/service/cmd_parser.h"
20 #include "gpu/command_buffer/service/command_buffer_service.h"
21 #include "gpu/command_buffer/service/gles2_cmd_decoder.h"
22 #include "gpu/gpu_export.h"
31 : public base::RefCountedThreadSafe
<PreemptionFlag
> {
33 PreemptionFlag() : flag_(0) {}
35 bool IsSet() { return !base::AtomicRefCountIsZero(&flag_
); }
36 void Set() { base::AtomicRefCountInc(&flag_
); }
37 void Reset() { base::subtle::NoBarrier_Store(&flag_
, 0); }
40 base::AtomicRefCount flag_
;
44 friend class base::RefCountedThreadSafe
<PreemptionFlag
>;
47 // This class schedules commands that have been flushed. They are received via
48 // a command buffer and forwarded to a command parser. TODO(apatrick): This
49 // class should not know about the decoder. Do not add additional dependencies
51 class GPU_EXPORT GpuScheduler
52 : NON_EXPORTED_BASE(public CommandBufferEngine
),
53 public base::SupportsWeakPtr
<GpuScheduler
> {
55 GpuScheduler(CommandBufferServiceBase
* command_buffer
,
56 AsyncAPIInterface
* handler
,
57 gles2::GLES2Decoder
* decoder
);
59 ~GpuScheduler() override
;
63 void SetPreemptByFlag(scoped_refptr
<PreemptionFlag
> flag
) {
64 preemption_flag_
= flag
;
67 // Sets whether commands should be processed by this scheduler. Setting to
68 // false unschedules. Setting to true reschedules. Whether or not the
69 // scheduler is currently scheduled is "reference counted". Every call with
70 // false must eventually be paired by a call with true.
71 void SetScheduled(bool is_scheduled
);
73 // Returns whether the scheduler is currently able to process more commands.
76 // Returns whether the scheduler needs to be polled again in the future.
79 typedef base::Callback
<void(bool /* scheduled */)> SchedulingChangedCallback
;
81 // Sets a callback that is invoked just before scheduler is rescheduled
82 // or descheduled. Takes ownership of callback object.
83 void SetSchedulingChangedCallback(const SchedulingChangedCallback
& callback
);
85 // Implementation of CommandBufferEngine.
86 scoped_refptr
<Buffer
> GetSharedMemoryBuffer(int32 shm_id
) override
;
87 void set_token(int32 token
) override
;
88 bool SetGetBuffer(int32 transfer_buffer_id
) override
;
89 bool SetGetOffset(int32 offset
) override
;
90 int32
GetGetOffset() override
;
92 void SetCommandProcessedCallback(const base::Closure
& callback
);
94 bool HasMoreIdleWork();
95 void PerformIdleWork();
97 CommandParser
* parser() const {
104 // Artificially reschedule if the scheduler is still unscheduled after a
106 void RescheduleTimeOut();
108 // The GpuScheduler holds a weak reference to the CommandBuffer. The
109 // CommandBuffer owns the GpuScheduler and holds a strong reference to it
110 // through the ProcessCommands callback.
111 CommandBufferServiceBase
* command_buffer_
;
113 // The parser uses this to execute commands.
114 AsyncAPIInterface
* handler_
;
116 // Does not own decoder. TODO(apatrick): The GpuScheduler shouldn't need a
117 // pointer to the decoder, it is only used to initialize the CommandParser,
118 // which could be an argument to the constructor, and to determine the
119 // reason for context lost.
120 gles2::GLES2Decoder
* decoder_
;
122 // TODO(apatrick): The GpuScheduler currently creates and owns the parser.
123 // This should be an argument to the constructor.
124 scoped_ptr
<CommandParser
> parser_
;
126 // Greater than zero if this is waiting to be rescheduled before continuing.
127 int unscheduled_count_
;
129 // The number of times this scheduler has been artificially rescheduled on
130 // account of a timeout.
131 int rescheduled_count_
;
133 SchedulingChangedCallback scheduling_changed_callback_
;
134 base::Closure descheduled_callback_
;
135 base::Closure command_processed_callback_
;
137 // If non-NULL and |preemption_flag_->IsSet()|, exit PutChanged early.
138 scoped_refptr
<PreemptionFlag
> preemption_flag_
;
141 // A factory for outstanding rescheduling tasks that is invalidated whenever
142 // the scheduler is rescheduled.
143 base::WeakPtrFactory
<GpuScheduler
> reschedule_task_factory_
;
145 DISALLOW_COPY_AND_ASSIGN(GpuScheduler
);
150 #endif // GPU_COMMAND_BUFFER_SERVICE_GPU_SCHEDULER_H_