18 #ifndef ION_GFX_RESOURCEHOLDER_H_
19 #define ION_GFX_RESOURCEHOLDER_H_
24 #include "base/macros.h"
74 return resource_count_;
82 for (
const auto& group : resources_)
83 for (
const auto& entry : group)
84 total += entry.second->GetGpuMemoryUsed();
89 const std::string&
GetLabel()
const {
return label_.Get(); }
93 using Notifier::Notify;
102 int GetBit()
const {
return change_bit_; }
107 : change_bit_(change_bit),
110 holder->AddField(
this);
115 holder_->OnChanged(change_bit_);
123 holder_->OnChanged(bit);
132 DISALLOW_IMPLICIT_CONSTRUCTORS(
FieldBase);
137 template <
typename T>
142 value_(initial_value) {}
146 value_(other.value_) {}
157 const T&
Get()
const {
return value_; }
170 if (!IsValid(value)) {
171 LOG(
ERROR) <<
"***ION: invalid value passed to Field::Set()";
172 }
else if (value != value_) {
185 template <
typename T>
189 const T& min_value,
const T& max_value,
191 :
Field<
T>(change_bit, initial_value, holder),
192 min_value_(min_value),
193 max_value_(max_value) {
200 return value >= min_value_ && value <= max_value_;
215 template <
typename T>
221 max_entries_(max_entries),
224 holder->AddField(
this);
232 if (entries_.size() >= max_entries_) {
233 LOG(
ERROR) <<
"***ION: Too many entries added to VectorField"
234 <<
"with " << entries_.size() <<
" entries";
237 Entry(GetBit() + static_cast<int>(entries_.size()), value));
239 OnChanged(entries_.back().bit);
248 if (i < entries_.size()) {
249 entries_[i].value = entries_.back().value;
250 entries_.resize(entries_.size() - 1U);
252 if (!entries_.empty())
253 OnChanged(entries_[i].bit);
259 const T&
Get(
const size_t i)
const {
260 if (i < entries_.size()) {
261 return entries_[i].value;
264 return base::InvalidReference<T>();
270 if (i < entries_.size()) {
271 OnChanged(entries_[i].bit);
272 return &entries_[i].value;
283 if (i < entries_.size()) {
284 if (value != entries_[i].value) {
285 entries_[i].value =
value;
286 OnChanged(entries_[i].bit);
295 size_t GetCount()
const {
return entries_.size(); }
300 Entry(
int bit_in,
const T& value_in)
307 void LogIndexError(
const size_t i)
const {
308 LOG(
ERROR) <<
"***ION: Invalid index " << i <<
" passed to VectorField "
309 <<
"with " << entries_.size() <<
" entries";
313 base::AllocVector<Entry> entries_;
315 DISALLOW_IMPLICIT_CONSTRUCTORS(VectorField);
323 ~ResourceHolder()
override;
331 for (
const auto& group : resources_)
332 for (
const auto& entry : group)
333 entry.second->OnChanged(bit);
343 void AddField(
FieldBase* field) { fields_.push_back(field); }
356 mutable std::atomic<int> resource_count_;
363 Field<std::string> label_;
369 #endif // ION_GFX_RESOURCEHOLDER_H_
T * GetMutable()
Gets an editable version of the current value.
Field(const Field &other)
A Field that has a limited valid range of values.
This class can be used in place of std::unordered_map to allow an Ion Allocator to be used for memory...
int GetBit() const
Get the change bit.
RangedField(const int change_bit, const T &initial_value, const T &min_value, const T &max_value, ResourceHolder *holder)
void Remove(size_t i)
Removes an element from the VectorField, replacing it with the last entry in the VectorField.
#define LOG(severity)
Logs the streamed message unconditionally with a severity of severity.
A Notifier both sends notifications to and receives notifications from other Notifiers.
A Field that holds a vector of up to some number of values.
void Add(const T &value)
Adds a value to the vector.
bool Set(const T &value)
Sets the value of the Field if it is valid and tells the resource what has changed.
size_t GetGpuMemoryUsed() const
Returns the total amount of GPU memory used by this Holder's resource.
void OnChanged(int bit) const
Forwards OnChanged to all resources.
FieldBase(const int change_bit, ResourceHolder *holder)
The constructor is protected because this is an abstract base class.
bool IsValid(const T &value) override
Check if the proposed value falls within the correct range.
A LockGuard locks a mutex when created, and unlocks it when destroyed.
Base class for Fields (see below).
void SetLabel(const std::string &label)
T * GetMutable(const size_t i)
Gets a non-const pointer to a value and triggers the change.
BaseChanges
All ResourceHolders derived from this should start their own change enums from kNumBaseChanges.
void OnChanged(const int bit)
Trigger a change for a specific bit.
The ReadWriteLock class defines a non-promotable lock that is very fast when only readers try to obta...
Copyright 2016 Google Inc.
Field(const int change_bit, const T &initial_value, ResourceHolder *holder)
InlinedAllocVectoris a similar to AllocVector, but uses inlined storage for its first N elements...
intptr_t ResourceKey
Type of identifiers used to disambiguate between multiple resources created for the same Ion object b...
A generic field that represents some state in the resource.
int GetResourceCount() const
Returns the number of resources that this holder holds.
const T & Get() const
Gets a const version of the current value.
bool Set(const size_t i, const T &value)
If the index i is valid, sets the value at index i and tells the resource what has changed...
A ReadLock obtains a read lock, but has a similar interface to a Mutex and can be used with a ReadGua...
ResourceBase is an internal abstract base class for managed resources.
const std::string & GetLabel() const
Returns/sets the label of this.
virtual bool IsValid(const T &value)
Checks if a proposed value is valid.
The Renderer class handles rendering ION scene graphs using OpenGL.
ResourceHolder is an internal base class for objects that hold resources managed by an outside entity...
const T & Get(const size_t i) const
Gets a const reference to a value, if the index is valid, otherwise returns an InvalidReference.
VectorField(const int change_bit_start, const size_t max_entries, ResourceHolder *holder)
This class can be used in place of std::vector to allow an Ion Allocator to be used for memory alloca...