1 // Copyright (c) 2014 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_VALUEBUFFER_MANAGER_H_
6 #define GPU_COMMAND_BUFFER_SERVICE_VALUEBUFFER_MANAGER_H_
8 #include "base/basictypes.h"
9 #include "base/containers/hash_tables.h"
10 #include "base/memory/ref_counted.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/observer_list.h"
13 #include "gpu/command_buffer/common/value_state.h"
14 #include "gpu/gpu_export.h"
23 class ValuebufferManager
;
25 class GPU_EXPORT SubscriptionRefSet
26 : public base::RefCounted
<SubscriptionRefSet
> {
28 class GPU_EXPORT Observer
{
32 virtual void OnAddSubscription(unsigned int target
) = 0;
33 virtual void OnRemoveSubscription(unsigned int target
) = 0;
38 void AddObserver(Observer
* observer
);
39 void RemoveObserver(Observer
* observer
);
42 virtual ~SubscriptionRefSet();
45 friend class base::RefCounted
<SubscriptionRefSet
>;
46 friend class ValuebufferManager
;
48 typedef base::hash_map
<unsigned int, int> RefSet
;
50 void AddSubscription(unsigned int target
);
51 void RemoveSubscription(unsigned int target
);
53 RefSet reference_set_
;
55 ObserverList
<Observer
, true> observers_
;
57 DISALLOW_COPY_AND_ASSIGN(SubscriptionRefSet
);
60 class GPU_EXPORT Valuebuffer
: public base::RefCounted
<Valuebuffer
> {
62 Valuebuffer(ValuebufferManager
* manager
, unsigned int client_id
);
64 unsigned int client_id() const { return client_id_
; }
66 bool IsDeleted() const { return client_id_
== 0; }
68 void MarkAsValid() { has_been_bound_
= true; }
70 bool IsValid() const { return has_been_bound_
&& !IsDeleted(); }
72 void AddSubscription(unsigned int subscription
);
73 void RemoveSubscription(unsigned int subscription
);
75 // Returns true if this Valuebuffer is subscribed to subscription
76 bool IsSubscribed(unsigned int subscription
);
78 // Returns the active state for a given target in this Valuebuffer
79 // returns NULL if target state doesn't exist
80 const ValueState
* GetState(unsigned int target
) const;
83 friend class ValuebufferManager
;
84 friend class base::RefCounted
<Valuebuffer
>;
86 typedef base::hash_set
<unsigned int> SubscriptionSet
;
90 void UpdateState(const ValueStateMap
* pending_state
);
92 void MarkAsDeleted() { client_id_
= 0; }
94 // ValuebufferManager that owns this Valuebuffer.
95 ValuebufferManager
* manager_
;
97 // Client side Valuebuffer id.
98 unsigned int client_id_
;
100 // Whether this Valuebuffer has ever been bound.
101 bool has_been_bound_
;
103 SubscriptionSet subscriptions_
;
105 scoped_refptr
<ValueStateMap
> active_state_map_
;
108 class GPU_EXPORT ValuebufferManager
{
110 ValuebufferManager(SubscriptionRefSet
* ref_set
, ValueStateMap
* state_map
);
111 ~ValuebufferManager();
113 // Must call before destruction.
116 // Creates a Valuebuffer for the given Valuebuffer ids.
117 void CreateValuebuffer(unsigned int client_id
);
119 // Gets the Valuebuffer for the given Valuebuffer id.
120 Valuebuffer
* GetValuebuffer(unsigned int client_id
);
122 // Removes a Valuebuffer for the given Valuebuffer id.
123 void RemoveValuebuffer(unsigned int client_id
);
125 // Updates the value state for the given Valuebuffer
126 void UpdateValuebufferState(Valuebuffer
* valuebuffer
);
128 static uint32
ApiTypeForSubscriptionTarget(unsigned int target
);
131 friend class Valuebuffer
;
133 typedef base::hash_map
<unsigned int, scoped_refptr
<Valuebuffer
>>
136 void StartTracking(Valuebuffer
* valuebuffer
);
137 void StopTracking(Valuebuffer
* valuebuffer
);
139 void NotifyAddSubscription(unsigned int target
);
140 void NotifyRemoveSubscription(unsigned int target
);
142 // Counts the number of Valuebuffer allocated with 'this' as its manager.
143 // Allows to check no Valuebuffer will outlive this.
144 unsigned int valuebuffer_count_
;
146 // Info for each Valuebuffer in the system.
147 ValuebufferMap valuebuffer_map_
;
149 // Current value state in the system
150 // Updated by GpuChannel
151 scoped_refptr
<ValueStateMap
> pending_state_map_
;
153 // Subscription targets which are currently subscribed and how
154 // many value buffers are currently subscribed to each
155 scoped_refptr
<SubscriptionRefSet
> subscription_ref_set_
;
157 DISALLOW_COPY_AND_ASSIGN(ValuebufferManager
);
163 #endif // GPU_COMMAND_BUFFER_SERVICE_VALUEBUFFER_MANAGER_H_