Ion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
texturemanager.cc
Go to the documentation of this file.
1 
18 #include "ion/gfx/texturemanager.h"
19 
20 #include <algorithm>
21 #include <cstring>
22 
23 #include "ion/base/logging.h"
24 
25 namespace ion {
26 namespace gfx {
27 
28 const int TextureManager::kEnd = -1;
29 
62 TextureManager::TextureManager(int max_texture_units)
63  : items_(max_texture_units), back_(max_texture_units - 1), front_(0) {
64  DCHECK_EQ(items_.size(), static_cast<size_t>(max_texture_units));
65  DCHECK_GE(max_texture_units, 1) << "TextureManager was initialized with < 1 "
66  "texture units. This could mean that "
67  "there is no valid GL context bound.";
68  SetUnitRange(math::Range1i(0, max_texture_units - 1));
69 }
70 
72 
74  DCHECK_GE(unit, 0);
75  DCHECK_LT(unit, static_cast<int>(items_.size()));
76  return items_[unit].texture;
77 }
78 
80  DCHECK_LT(current_unit, static_cast<int>(items_.size()));
81  if (current_unit != kEnd && items_[current_unit].texture == texture) {
83  Touch(current_unit);
84  } else {
86  Touch(front_); // This changes front_ to back_.
87  }
89  items_[back_].texture = texture;
91  return back_;
92 }
93 
95 void TextureManager::Touch(int unit) {
96  DCHECK_LT(unit, static_cast<int>(items_.size()));
97  if (unit == back_)
98  return;
99 
100  Item& this_item = items_[unit];
102  if (this_item.prev != kEnd) {
104  Item& prev_item = items_[this_item.prev];
105  prev_item.next = this_item.next;
106  } else {
107  front_ = this_item.next;
108  }
109  if (this_item.next != kEnd) {
111  Item& next_item = items_[this_item.next];
112  next_item.prev = this_item.prev;
113  }
116 
118  Item& back_item = items_[back_];
119  back_item.next = unit;
120  this_item.prev = back_;
121  this_item.next = kEnd;
122  back_ = unit;
123 }
124 
126  if (units.GetMinPoint() < 0) {
127  LOG(ERROR) << "The minimum unit for TextureManager to use must be >= 0.";
128  return;
129  }
130 
131  front_ = std::min(static_cast<int>(units.GetMinPoint()),
132  static_cast<int>(items_.size() - 1U));
133  back_ = std::min(static_cast<int>(units.GetMaxPoint()),
134  static_cast<int>(items_.size() - 1U));
135 
137  memset(&items_[0], 0, sizeof(Item) * items_.size());
138 
140  items_[front_].prev = kEnd;
141  items_[front_].texture = NULL;
142  items_[front_].next = (front_ < back_) ? front_ + 1 : kEnd;
143  items_[back_].prev = (front_ < back_) ? back_ - 1 : kEnd;
144  items_[back_].texture = NULL;
145  items_[back_].next = kEnd;
146  for (int i = front_ + 1; i < back_; ++i) {
147  items_[i].prev = i - 1;
148  items_[i].texture = NULL;
149  items_[i].next = i + 1;
150  }
151 }
152 
153 } // namespace gfx
154 } // namespace ion
int GetUnit(TextureType texture, int current_unit)
Returns the unit associated with the data pointer.
TextureType GetTexture(int unit) const
Returns the data at index unit.
Range< 1, int32 > Range1i
Definition: range.h:363
#define LOG(severity)
Logs the streamed message unconditionally with a severity of severity.
Definition: logging.h:216
TextureManager(int max_texture_units)
A TextureManager must be initialized with a size of at least 1.
void SetUnitRange(const math::Range1i &units)
Sets the inclusive range of units that the TextureManager uses.
#define DCHECK_GE(val1, val2)
Definition: logging.h:336
Copyright 2016 Google Inc.
#define DCHECK_EQ(val1, val2)
Definition: logging.h:332
TexturePtr texture
The Texture to add sub-image data to.
Definition: fontimage.cc:107
#define DCHECK_LT(val1, val2)
Definition: logging.h:335