Add signalSyncPoint to the WebGraphicsContext3D command buffer impls.
[chromium-blink-merge.git] / cc / base / tiling_data.cc
blobba6aaa8153fb92b5943c397601ea6362b82a3312
1 // Copyright 2010 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 #include "cc/base/tiling_data.h"
7 #include <algorithm>
9 #include "ui/gfx/rect.h"
10 #include "ui/gfx/vector2d.h"
12 namespace cc {
14 static int ComputeNumTiles(int max_texture_size,
15 int total_size,
16 int border_texels) {
17 if (max_texture_size - 2 * border_texels <= 0)
18 return total_size > 0 && max_texture_size >= total_size ? 1 : 0;
20 int num_tiles = std::max(1,
21 1 + (total_size - 1 - 2 * border_texels) /
22 (max_texture_size - 2 * border_texels));
23 return total_size > 0 ? num_tiles : 0;
26 TilingData::TilingData()
27 : border_texels_(0) {
28 RecomputeNumTiles();
31 TilingData::TilingData(
32 gfx::Size max_texture_size,
33 gfx::Size total_size,
34 bool has_border_texels)
35 : max_texture_size_(max_texture_size),
36 total_size_(total_size),
37 border_texels_(has_border_texels ? 1 : 0) {
38 RecomputeNumTiles();
41 TilingData::TilingData(
42 gfx::Size max_texture_size,
43 gfx::Size total_size,
44 int border_texels)
45 : max_texture_size_(max_texture_size),
46 total_size_(total_size),
47 border_texels_(border_texels) {
48 RecomputeNumTiles();
51 void TilingData::SetTotalSize(gfx::Size total_size) {
52 total_size_ = total_size;
53 RecomputeNumTiles();
56 void TilingData::SetMaxTextureSize(gfx::Size max_texture_size) {
57 max_texture_size_ = max_texture_size;
58 RecomputeNumTiles();
61 void TilingData::SetHasBorderTexels(bool has_border_texels) {
62 border_texels_ = has_border_texels ? 1 : 0;
63 RecomputeNumTiles();
66 void TilingData::SetBorderTexels(int border_texels) {
67 border_texels_ = border_texels;
68 RecomputeNumTiles();
71 int TilingData::TileXIndexFromSrcCoord(int src_position) const {
72 if (num_tiles_x_ <= 1)
73 return 0;
75 DCHECK_GT(max_texture_size_.width() - 2 * border_texels_, 0);
76 int x = (src_position - border_texels_) /
77 (max_texture_size_.width() - 2 * border_texels_);
78 return std::min(std::max(x, 0), num_tiles_x_ - 1);
81 int TilingData::TileYIndexFromSrcCoord(int src_position) const {
82 if (num_tiles_y_ <= 1)
83 return 0;
85 DCHECK_GT(max_texture_size_.height() - 2 * border_texels_, 0);
86 int y = (src_position - border_texels_) /
87 (max_texture_size_.height() - 2 * border_texels_);
88 return std::min(std::max(y, 0), num_tiles_y_ - 1);
91 int TilingData::FirstBorderTileXIndexFromSrcCoord(int src_position) const {
92 if (num_tiles_x_ <= 1)
93 return 0;
95 DCHECK_GT(max_texture_size_.width() - 2 * border_texels_, 0);
96 int inner_tile_size = max_texture_size_.width() - 2 * border_texels_;
97 int x = (src_position - 2 * border_texels_) / inner_tile_size;
98 return std::min(std::max(x, 0), num_tiles_x_ - 1);
101 int TilingData::FirstBorderTileYIndexFromSrcCoord(int src_position) const {
102 if (num_tiles_y_ <= 1)
103 return 0;
105 DCHECK_GT(max_texture_size_.height() - 2 * border_texels_, 0);
106 int inner_tile_size = max_texture_size_.height() - 2 * border_texels_;
107 int y = (src_position - 2 * border_texels_) / inner_tile_size;
108 return std::min(std::max(y, 0), num_tiles_y_ - 1);
111 int TilingData::LastBorderTileXIndexFromSrcCoord(int src_position) const {
112 if (num_tiles_x_ <= 1)
113 return 0;
115 DCHECK_GT(max_texture_size_.width() - 2 * border_texels_, 0);
116 int inner_tile_size = max_texture_size_.width() - 2 * border_texels_;
117 int x = src_position / inner_tile_size;
118 return std::min(std::max(x, 0), num_tiles_x_ - 1);
121 int TilingData::LastBorderTileYIndexFromSrcCoord(int src_position) const {
122 if (num_tiles_y_ <= 1)
123 return 0;
125 DCHECK_GT(max_texture_size_.height() - 2 * border_texels_, 0);
126 int inner_tile_size = max_texture_size_.height() - 2 * border_texels_;
127 int y = src_position / inner_tile_size;
128 return std::min(std::max(y, 0), num_tiles_y_ - 1);
131 gfx::Rect TilingData::TileBounds(int i, int j) const {
132 AssertTile(i, j);
133 int max_texture_size_x = max_texture_size_.width() - 2 * border_texels_;
134 int max_texture_size_y = max_texture_size_.height() - 2 * border_texels_;
135 int total_size_x = total_size_.width();
136 int total_size_y = total_size_.height();
138 int lo_x = max_texture_size_x * i;
139 if (i != 0)
140 lo_x += border_texels_;
142 int lo_y = max_texture_size_y * j;
143 if (j != 0)
144 lo_y += border_texels_;
146 int hi_x = max_texture_size_x * (i + 1) + border_texels_;
147 if (i + 1 == num_tiles_x_)
148 hi_x += border_texels_;
149 if (hi_x > total_size_x)
150 hi_x = total_size_x;
152 int hi_y = max_texture_size_y * (j + 1) + border_texels_;
153 if (j + 1 == num_tiles_y_)
154 hi_y += border_texels_;
155 if (hi_y > total_size_y)
156 hi_y = total_size_y;
158 int x = lo_x;
159 int y = lo_y;
160 int width = hi_x - lo_x;
161 int height = hi_y - lo_y;
162 DCHECK_GE(x, 0);
163 DCHECK_GE(y, 0);
164 DCHECK_GE(width, 0);
165 DCHECK_GE(height, 0);
166 DCHECK_LE(x, total_size_.width());
167 DCHECK_LE(y, total_size_.height());
168 return gfx::Rect(x, y, width, height);
171 gfx::Rect TilingData::TileBoundsWithBorder(int i, int j) const {
172 gfx::Rect bounds = TileBounds(i, j);
174 if (border_texels_) {
175 int x1 = bounds.x();
176 int x2 = bounds.right();
177 int y1 = bounds.y();
178 int y2 = bounds.bottom();
180 if (i > 0)
181 x1-= border_texels_;
182 if (i < (num_tiles_x_ - 1))
183 x2+= border_texels_;
184 if (j > 0)
185 y1-= border_texels_;
186 if (j < (num_tiles_y_ - 1))
187 y2+= border_texels_;
189 bounds = gfx::Rect(x1, y1, x2 - x1, y2 - y1);
192 return bounds;
195 int TilingData::TilePositionX(int x_index) const {
196 DCHECK_GE(x_index, 0);
197 DCHECK_LT(x_index, num_tiles_x_);
199 int pos = (max_texture_size_.width() - 2 * border_texels_) * x_index;
200 if (x_index != 0)
201 pos += border_texels_;
203 return pos;
206 int TilingData::TilePositionY(int y_index) const {
207 DCHECK_GE(y_index, 0);
208 DCHECK_LT(y_index, num_tiles_y_);
210 int pos = (max_texture_size_.height() - 2 * border_texels_) * y_index;
211 if (y_index != 0)
212 pos += border_texels_;
214 return pos;
217 int TilingData::TileSizeX(int x_index) const {
218 DCHECK_GE(x_index, 0);
219 DCHECK_LT(x_index, num_tiles_x_);
221 if (!x_index && num_tiles_x_ == 1)
222 return total_size_.width();
223 if (!x_index && num_tiles_x_ > 1)
224 return max_texture_size_.width() - border_texels_;
225 if (x_index < num_tiles_x_ - 1)
226 return max_texture_size_.width() - 2 * border_texels_;
227 if (x_index == num_tiles_x_ - 1)
228 return total_size_.width() - TilePositionX(x_index);
230 NOTREACHED();
231 return 0;
234 int TilingData::TileSizeY(int y_index) const {
235 DCHECK_GE(y_index, 0);
236 DCHECK_LT(y_index, num_tiles_y_);
238 if (!y_index && num_tiles_y_ == 1)
239 return total_size_.height();
240 if (!y_index && num_tiles_y_ > 1)
241 return max_texture_size_.height() - border_texels_;
242 if (y_index < num_tiles_y_ - 1)
243 return max_texture_size_.height() - 2 * border_texels_;
244 if (y_index == num_tiles_y_ - 1)
245 return total_size_.height() - TilePositionY(y_index);
247 NOTREACHED();
248 return 0;
251 gfx::Vector2d TilingData::TextureOffset(int x_index, int y_index) const {
252 int left = (!x_index || num_tiles_x_ == 1) ? 0 : border_texels_;
253 int top = (!y_index || num_tiles_y_ == 1) ? 0 : border_texels_;
255 return gfx::Vector2d(left, top);
258 void TilingData::RecomputeNumTiles() {
259 num_tiles_x_ = ComputeNumTiles(
260 max_texture_size_.width(), total_size_.width(), border_texels_);
261 num_tiles_y_ = ComputeNumTiles(
262 max_texture_size_.height(), total_size_.height(), border_texels_);
265 TilingData::BaseIterator::BaseIterator(const TilingData* tiling_data)
266 : tiling_data_(tiling_data),
267 index_x_(-1),
268 index_y_(-1) {
271 TilingData::Iterator::Iterator(const TilingData* tiling_data, gfx::Rect rect)
272 : BaseIterator(tiling_data),
273 left_(-1),
274 right_(-1),
275 bottom_(-1) {
276 if (tiling_data_->num_tiles_x() <= 0 || tiling_data_->num_tiles_y() <= 0) {
277 done();
278 return;
281 rect.Intersect(gfx::Rect(tiling_data_->total_size()));
282 index_x_ = tiling_data_->FirstBorderTileXIndexFromSrcCoord(rect.x());
283 index_y_ = tiling_data_->FirstBorderTileYIndexFromSrcCoord(rect.y());
284 left_ = index_x_;
285 right_ = tiling_data_->LastBorderTileXIndexFromSrcCoord(rect.right() - 1);
286 bottom_ = tiling_data_->LastBorderTileYIndexFromSrcCoord(rect.bottom() - 1);
288 // Index functions always return valid indices, so explicitly check
289 // for non-intersecting rects.
290 gfx::Rect new_rect = tiling_data_->TileBoundsWithBorder(index_x_, index_y_);
291 if (!new_rect.Intersects(rect))
292 done();
295 TilingData::Iterator& TilingData::Iterator::operator++() {
296 if (!*this)
297 return *this;
299 index_x_++;
300 if (index_x_ > right_) {
301 index_x_ = left_;
302 index_y_++;
303 if (index_y_ > bottom_)
304 done();
307 return *this;
310 TilingData::DifferenceIterator::DifferenceIterator(
311 const TilingData* tiling_data,
312 gfx::Rect consider,
313 gfx::Rect ignore)
314 : BaseIterator(tiling_data),
315 consider_left_(-1),
316 consider_top_(-1),
317 consider_right_(-1),
318 consider_bottom_(-1),
319 ignore_left_(-1),
320 ignore_top_(-1),
321 ignore_right_(-1),
322 ignore_bottom_(-1) {
323 if (tiling_data_->num_tiles_x() <= 0 || tiling_data_->num_tiles_y() <= 0) {
324 done();
325 return;
328 gfx::Rect bounds(tiling_data_->total_size());
329 consider.Intersect(bounds);
330 ignore.Intersect(bounds);
331 if (consider.IsEmpty()) {
332 done();
333 return;
336 consider_left_ =
337 tiling_data_->FirstBorderTileXIndexFromSrcCoord(consider.x());
338 consider_top_ =
339 tiling_data_->FirstBorderTileYIndexFromSrcCoord(consider.y());
340 consider_right_ =
341 tiling_data_->LastBorderTileXIndexFromSrcCoord(consider.right() - 1);
342 consider_bottom_ =
343 tiling_data_->LastBorderTileYIndexFromSrcCoord(consider.bottom() - 1);
345 if (!ignore.IsEmpty()) {
346 ignore_left_ =
347 tiling_data_->FirstBorderTileXIndexFromSrcCoord(ignore.x());
348 ignore_top_ =
349 tiling_data_->FirstBorderTileYIndexFromSrcCoord(ignore.y());
350 ignore_right_ =
351 tiling_data_->LastBorderTileXIndexFromSrcCoord(ignore.right() - 1);
352 ignore_bottom_ =
353 tiling_data_->LastBorderTileYIndexFromSrcCoord(ignore.bottom() - 1);
355 // Clamp ignore indices to consider indices.
356 ignore_left_ = std::max(ignore_left_, consider_left_);
357 ignore_top_ = std::max(ignore_top_, consider_top_);
358 ignore_right_ = std::min(ignore_right_, consider_right_);
359 ignore_bottom_ = std::min(ignore_bottom_, consider_bottom_);
362 if (ignore_left_ == consider_left_ && ignore_right_ == consider_right_ &&
363 ignore_top_ == consider_top_ && ignore_bottom_ == consider_bottom_) {
364 done();
365 return;
368 index_x_ = consider_left_;
369 index_y_ = consider_top_;
371 if (in_ignore_rect())
372 ++(*this);
375 TilingData::DifferenceIterator& TilingData::DifferenceIterator::operator++() {
376 if (!*this)
377 return *this;
379 index_x_++;
380 if (in_ignore_rect())
381 index_x_ = ignore_right_ + 1;
383 if (index_x_ > consider_right_) {
384 index_x_ = consider_left_;
385 index_y_++;
387 if (in_ignore_rect()) {
388 index_x_ = ignore_right_ + 1;
389 // If the ignore rect spans the whole consider rect horizontally, then
390 // ignore_right + 1 will be out of bounds.
391 if (in_ignore_rect() || index_x_ > consider_right_) {
392 index_y_ = ignore_bottom_ + 1;
393 index_x_ = consider_left_;
397 if (index_y_ > consider_bottom_)
398 done();
401 return *this;
404 } // namespace cc