Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / gpu / command_buffer / service / gpu_scheduler.h
blob11a6d31f941a39912a78432fa566233ea0d7dc7a
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_
8 #include <queue>
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"
24 namespace gfx {
25 class GLFence;
28 namespace gpu {
30 class PreemptionFlag
31 : public base::RefCountedThreadSafe<PreemptionFlag> {
32 public:
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); }
39 private:
40 base::AtomicRefCount flag_;
42 ~PreemptionFlag() {}
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
50 // on it.
51 class GPU_EXPORT GpuScheduler
52 : NON_EXPORTED_BASE(public CommandBufferEngine),
53 public base::SupportsWeakPtr<GpuScheduler> {
54 public:
55 GpuScheduler(CommandBufferServiceBase* command_buffer,
56 AsyncAPIInterface* handler,
57 gles2::GLES2Decoder* decoder);
59 ~GpuScheduler() override;
61 void PutChanged();
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.
69 void SetScheduled(bool scheduled);
71 bool scheduled() const { return scheduled_; }
73 // Returns whether the scheduler needs to be polled again in the future to
74 // process pending queries.
75 bool HasPendingQueries() const;
77 // Process pending queries and return. HasPendingQueries() can be used to
78 // determine if there's more pending queries after this has been called.
79 void ProcessPendingQueries();
81 typedef base::Callback<void(bool /* scheduled */)> SchedulingChangedCallback;
83 // Sets a callback that is invoked just before scheduler is rescheduled
84 // or descheduled. Takes ownership of callback object.
85 void SetSchedulingChangedCallback(const SchedulingChangedCallback& callback);
87 // Implementation of CommandBufferEngine.
88 scoped_refptr<Buffer> GetSharedMemoryBuffer(int32 shm_id) override;
89 void set_token(int32 token) override;
90 bool SetGetBuffer(int32 transfer_buffer_id) override;
91 bool SetGetOffset(int32 offset) override;
92 int32 GetGetOffset() override;
94 void SetCommandProcessedCallback(const base::Closure& callback);
96 // Returns whether the scheduler needs to be polled again in the future to
97 // process idle work.
98 bool HasMoreIdleWork() const;
100 // Perform some idle work and return. HasMoreIdleWork() can be used to
101 // determine if there's more idle work do be done after this has been called.
102 void PerformIdleWork();
104 CommandParser* parser() const {
105 return parser_.get();
108 private:
109 bool IsPreempted();
111 // The GpuScheduler holds a weak reference to the CommandBuffer. The
112 // CommandBuffer owns the GpuScheduler and holds a strong reference to it
113 // through the ProcessCommands callback.
114 CommandBufferServiceBase* command_buffer_;
116 // The parser uses this to execute commands.
117 AsyncAPIInterface* handler_;
119 // Does not own decoder. TODO(apatrick): The GpuScheduler shouldn't need a
120 // pointer to the decoder, it is only used to initialize the CommandParser,
121 // which could be an argument to the constructor, and to determine the
122 // reason for context lost.
123 gles2::GLES2Decoder* decoder_;
125 // TODO(apatrick): The GpuScheduler currently creates and owns the parser.
126 // This should be an argument to the constructor.
127 scoped_ptr<CommandParser> parser_;
129 // Whether the scheduler is currently able to process more commands.
130 bool scheduled_;
132 SchedulingChangedCallback scheduling_changed_callback_;
133 base::Closure descheduled_callback_;
134 base::Closure command_processed_callback_;
136 // If non-NULL and |preemption_flag_->IsSet()|, exit PutChanged early.
137 scoped_refptr<PreemptionFlag> preemption_flag_;
138 bool was_preempted_;
140 DISALLOW_COPY_AND_ASSIGN(GpuScheduler);
143 } // namespace gpu
145 #endif // GPU_COMMAND_BUFFER_SERVICE_GPU_SCHEDULER_H_