Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(41)

Side by Side Diff: cc/tiling_data.cc

Issue 11887027: cc: Add iterator and big border support to TilingData (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright 2010 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "cc/tiling_data.h" 5 #include "cc/tiling_data.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 8
9 #include "ui/gfx/rect.h" 9 #include "ui/gfx/rect.h"
10 #include "ui/gfx/vector2d.h" 10 #include "ui/gfx/vector2d.h"
11 11
12 namespace cc { 12 namespace cc {
13 13
14 static int ComputeNumTiles(int max_texture_size, int total_size, int border_texe ls) { 14 static int ComputeNumTiles(int max_texture_size, int total_size, int border_texe ls) {
15 if (max_texture_size - 2 * border_texels <= 0) 15 if (max_texture_size - 2 * border_texels <= 0)
16 return total_size > 0 && max_texture_size >= total_size ? 1 : 0; 16 return total_size > 0 && max_texture_size >= total_size ? 1 : 0;
17 17
18 int num_tiles = std::max(1, 1 + (total_size - 1 - 2 * border_texels) / (max_te xture_size - 2 * border_texels)); 18 int num_tiles = std::max(1, 1 + (total_size - 1 - 2 * border_texels) / (max_te xture_size - 2 * border_texels));
19 return total_size > 0 ? num_tiles : 0; 19 return total_size > 0 ? num_tiles : 0;
20 } 20 }
21 21
22 TilingData::TilingData(gfx::Size max_texture_size, gfx::Size total_size, bool ha sBorderTexels) 22 TilingData::TilingData(
23 gfx::Size max_texture_size,
24 gfx::Size total_size,
25 bool hasBorderTexels)
23 : max_texture_size_(max_texture_size), 26 : max_texture_size_(max_texture_size),
24 total_size_(total_size), 27 total_size_(total_size),
25 border_texels_(hasBorderTexels ? 1 : 0) { 28 border_texels_(hasBorderTexels ? 1 : 0) {
26 RecomputeNumTiles(); 29 RecomputeNumTiles();
27 } 30 }
28 31
29 TilingData::~TilingData() { 32 TilingData::TilingData(
33 gfx::Size max_texture_size,
34 gfx::Size total_size,
35 int border_texels)
36 : max_texture_size_(max_texture_size),
37 total_size_(total_size),
38 border_texels_(border_texels) {
39 RecomputeNumTiles();
30 } 40 }
31 41
32 void TilingData::SetTotalSize(gfx::Size total_size) { 42 void TilingData::SetTotalSize(gfx::Size total_size) {
33 total_size_ = total_size; 43 total_size_ = total_size;
34 RecomputeNumTiles(); 44 RecomputeNumTiles();
35 } 45 }
36 46
37 void TilingData::SetMaxTextureSize(gfx::Size max_texture_size) { 47 void TilingData::SetMaxTextureSize(gfx::Size max_texture_size) {
38 max_texture_size_ = max_texture_size; 48 max_texture_size_ = max_texture_size;
39 RecomputeNumTiles(); 49 RecomputeNumTiles();
40 } 50 }
41 51
42 void TilingData::SetHasBorderTexels(bool has_border_texels) { 52 void TilingData::SetHasBorderTexels(bool has_border_texels) {
43 border_texels_ = has_border_texels ? 1 : 0; 53 border_texels_ = has_border_texels ? 1 : 0;
44 RecomputeNumTiles(); 54 RecomputeNumTiles();
45 } 55 }
46 56
57 void TilingData::SetBorderTexels(int border_texels) {
58 border_texels_ = border_texels;
59 RecomputeNumTiles();
60 }
61
47 int TilingData::TileXIndexFromSrcCoord(int src_position) const { 62 int TilingData::TileXIndexFromSrcCoord(int src_position) const {
48 if (num_tiles_x_ <= 1) 63 if (num_tiles_x_ <= 1)
49 return 0; 64 return 0;
50 65
51 DCHECK(max_texture_size_.width() - 2 * border_texels_); 66 DCHECK_GT(max_texture_size_.width() - 2 * border_texels_, 0);
52 int x = (src_position - border_texels_) / (max_texture_size_.width() - 2 * bor der_texels_); 67 int x = (src_position - border_texels_) /
68 (max_texture_size_.width() - 2 * border_texels_);
53 return std::min(std::max(x, 0), num_tiles_x_ - 1); 69 return std::min(std::max(x, 0), num_tiles_x_ - 1);
54 } 70 }
55 71
56 int TilingData::TileYIndexFromSrcCoord(int src_position) const { 72 int TilingData::TileYIndexFromSrcCoord(int src_position) const {
57 if (num_tiles_y_ <= 1) 73 if (num_tiles_y_ <= 1)
58 return 0; 74 return 0;
59 75
60 DCHECK(max_texture_size_.height() - 2 * border_texels_); 76 DCHECK_GT(max_texture_size_.width() - 2 * border_texels_, 0);
danakj 2013/01/15 23:09:05 did you mean to change this to width?
enne (OOO) 2013/01/15 23:31:40 Done.
61 int y = (src_position - border_texels_) / (max_texture_size_.height() - 2 * bo rder_texels_); 77 int y = (src_position - border_texels_) /
78 (max_texture_size_.height() - 2 * border_texels_);
62 return std::min(std::max(y, 0), num_tiles_y_ - 1); 79 return std::min(std::max(y, 0), num_tiles_y_ - 1);
63 } 80 }
64 81
82 int TilingData::BorderTileXIndexFromSrcCoord(int src_position) const {
83 if (num_tiles_x_ <= 1)
84 return 0;
85
86 DCHECK_GT(max_texture_size_.width() - 2 * border_texels_, 0);
87 int inner_tile_size = max_texture_size_.width() - 2 * border_texels_;
88 int x = (src_position - 2 * border_texels_) / inner_tile_size;
89 return std::min(std::max(x, 0), num_tiles_x_ - 1);
90 }
91
92 int TilingData::BorderTileYIndexFromSrcCoord(int src_position) const {
93 if (num_tiles_y_ <= 1)
94 return 0;
95
96 DCHECK_GT(max_texture_size_.width() - 2 * border_texels_, 0);
danakj 2013/01/15 23:09:05 should this be .height()?
enne (OOO) 2013/01/15 23:31:40 Done.
97 int inner_tile_size = max_texture_size_.height() - 2 * border_texels_;
98 int y = (src_position - 2 * border_texels_) / inner_tile_size;
99 return std::min(std::max(y, 0), num_tiles_y_ - 1);
100 }
101
65 gfx::Rect TilingData::TileBounds(int i, int j) const { 102 gfx::Rect TilingData::TileBounds(int i, int j) const {
66 AssertTile(i, j); 103 AssertTile(i, j);
67 int x = TilePositionX(i); 104 int x = TilePositionX(i);
68 int y = TilePositionY(j); 105 int y = TilePositionY(j);
69 int width = TileSizeX(i); 106 int width = TileSizeX(i);
70 int height = TileSizeY(j); 107 int height = TileSizeY(j);
71 DCHECK_GE(x, 0); 108 DCHECK_GE(x, 0);
72 DCHECK_GE(y, 0); 109 DCHECK_GE(y, 0);
73 DCHECK_GE(width, 0); 110 DCHECK_GE(width, 0);
74 DCHECK_GE(height, 0); 111 DCHECK_GE(height, 0);
75 DCHECK_LE(x, total_size_.width()); 112 DCHECK_LE(x, total_size_.width());
76 DCHECK_LE(y, total_size_.height()); 113 DCHECK_LE(y, total_size_.height());
77 return gfx::Rect(x, y, width, height); 114 return gfx::Rect(x, y, width, height);
78 } 115 }
79 116
80 gfx::Rect TilingData::TileBoundsWithBorder(int i, int j) const { 117 gfx::Rect TilingData::TileBoundsWithBorder(int i, int j) const {
81 gfx::Rect bounds = TileBounds(i, j); 118 gfx::Rect bounds = TileBounds(i, j);
82 119
83 if (border_texels_) { 120 if (border_texels_) {
84 int x1 = bounds.x(); 121 int x1 = bounds.x();
85 int x2 = bounds.right(); 122 int x2 = bounds.right();
86 int y1 = bounds.y(); 123 int y1 = bounds.y();
87 int y2 = bounds.bottom(); 124 int y2 = bounds.bottom();
88 125
89 if (i > 0) 126 if (i > 0)
90 x1--; 127 x1-= border_texels_;
91 if (i < (num_tiles_x_ - 1)) 128 if (i < (num_tiles_x_ - 1))
92 x2++; 129 x2+= border_texels_;
93 if (j > 0) 130 if (j > 0)
94 y1--; 131 y1-= border_texels_;
95 if (j < (num_tiles_y_ - 1)) 132 if (j < (num_tiles_y_ - 1))
96 y2++; 133 y2+= border_texels_;
97 134
98 bounds = gfx::Rect(x1, y1, x2 - x1, y2 - y1); 135 bounds = gfx::Rect(x1, y1, x2 - x1, y2 - y1);
99 } 136 }
100 137
101 return bounds; 138 return bounds;
102 } 139 }
103 140
104 int TilingData::TilePositionX(int x_index) const { 141 int TilingData::TilePositionX(int x_index) const {
105 DCHECK_GE(x_index, 0); 142 DCHECK_GE(x_index, 0);
106 DCHECK_LT(x_index, num_tiles_x_); 143 DCHECK_LT(x_index, num_tiles_x_);
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
162 int top = (!y_index || num_tiles_y_ == 1) ? 0 : border_texels_; 199 int top = (!y_index || num_tiles_y_ == 1) ? 0 : border_texels_;
163 200
164 return gfx::Vector2d(left, top); 201 return gfx::Vector2d(left, top);
165 } 202 }
166 203
167 void TilingData::RecomputeNumTiles() { 204 void TilingData::RecomputeNumTiles() {
168 num_tiles_x_ = ComputeNumTiles(max_texture_size_.width(), total_size_.width(), border_texels_); 205 num_tiles_x_ = ComputeNumTiles(max_texture_size_.width(), total_size_.width(), border_texels_);
169 num_tiles_y_ = ComputeNumTiles(max_texture_size_.height(), total_size_.height( ), border_texels_); 206 num_tiles_y_ = ComputeNumTiles(max_texture_size_.height(), total_size_.height( ), border_texels_);
170 } 207 }
171 208
209 TilingData::Iterator::Iterator(const TilingData* tiling_data, gfx::Rect rect)
210 : tiling_data_(tiling_data) {
211 rect_ = gfx::IntersectRects(rect, gfx::Rect(tiling_data_->total_size()));
danakj 2013/01/15 23:09:05 why isn't this set via constructor initialization?
enne (OOO) 2013/01/15 23:31:40 Done.
212 if (tiling_data_->num_tiles_x() > 0 && tiling_data_->num_tiles_y() > 0) {
danakj 2013/01/15 23:09:05 nit: invert this and done(); return; instead of ne
enne (OOO) 2013/01/15 23:31:40 Done().
213 index_x_ = tiling_data_->BorderTileXIndexFromSrcCoord(rect_.x());
214 index_y_ = tiling_data_->BorderTileYIndexFromSrcCoord(rect_.y());
215
216 // Index functions always return valid indices, so explicitly check
217 // for non-intersecting rects.
218 gfx::Rect new_rect = tiling_data_->TileBoundsWithBorder(index_x_, index_y_);
219 if (!new_rect.Intersects(rect_))
220 done();
221 } else {
222 done();
223 }
224 }
225
226 TilingData::Iterator& TilingData::Iterator::operator++() {
227 index_x_++;
danakj 2013/01/15 23:09:05 DCHECK(index_x_ >= 0 && index_y_ >= 0) and early o
enne (OOO) 2013/01/15 23:31:40 Actually, multiple calls to ++ on a "done" iterato
228
229 bool new_row = index_x_ >= tiling_data_->num_tiles_x();
230 if (!new_row) {
231 gfx::Rect new_rect = tiling_data_->TileBoundsWithBorder(index_x_, index_y_);
232 new_row = new_rect.x() >= rect_.right();
233 }
234
235 if (new_row) {
236 index_x_ = tiling_data_->BorderTileXIndexFromSrcCoord(rect_.x());
237 index_y_++;
238
239 if (index_y_ >= tiling_data_->num_tiles_y())
danakj 2013/01/15 23:09:05 nit: if one block gets {} they all do, in chromium
enne (OOO) 2013/01/15 23:31:40 What do you know, a part of Chromium style I like!
240 done();
241 else {
242 gfx::Rect new_rect =
243 tiling_data_->TileBoundsWithBorder(index_x_, index_y_);
244 if (new_rect.y() >= rect_.bottom())
245 done();
246 }
247 }
248
249 return *this;
250 }
251
252 TilingData::Iterator::operator bool() const {
253 return index_x_ != -1 && index_y_ != -1;
254 }
255
256 void TilingData::Iterator::done() {
257 index_x_ = -1;
258 index_y_ = -1;
259 }
260
172 } // namespace cc 261 } // namespace cc
OLDNEW
« cc/tiling_data.h ('K') | « cc/tiling_data.h ('k') | cc/tiling_data_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698