WebKit Roll 139512:139548
[chromium-blink-merge.git] / cc / tiling_data.cc
blob3906d2c97bcd944421b671ea746b278c747b6460
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/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, int total_size, int border_texels) {
15 if (max_texture_size - 2 * border_texels <= 0)
16 return total_size > 0 && max_texture_size >= total_size ? 1 : 0;
18 int num_tiles = std::max(1, 1 + (total_size - 1 - 2 * border_texels) / (max_texture_size - 2 * border_texels));
19 return total_size > 0 ? num_tiles : 0;
22 TilingData::TilingData(gfx::Size max_texture_size, gfx::Size total_size, bool hasBorderTexels)
23 : max_texture_size_(max_texture_size),
24 total_size_(total_size),
25 border_texels_(hasBorderTexels ? 1 : 0) {
26 RecomputeNumTiles();
29 TilingData::~TilingData() {
32 void TilingData::SetTotalSize(gfx::Size total_size) {
33 total_size_ = total_size;
34 RecomputeNumTiles();
37 void TilingData::SetMaxTextureSize(gfx::Size max_texture_size) {
38 max_texture_size_ = max_texture_size;
39 RecomputeNumTiles();
42 void TilingData::SetHasBorderTexels(bool has_border_texels) {
43 border_texels_ = has_border_texels ? 1 : 0;
44 RecomputeNumTiles();
47 int TilingData::TileXIndexFromSrcCoord(int src_position) const {
48 if (num_tiles_x_ <= 1)
49 return 0;
51 DCHECK(max_texture_size_.width() - 2 * border_texels_);
52 int x = (src_position - border_texels_) / (max_texture_size_.width() - 2 * border_texels_);
53 return std::min(std::max(x, 0), num_tiles_x_ - 1);
56 int TilingData::TileYIndexFromSrcCoord(int src_position) const {
57 if (num_tiles_y_ <= 1)
58 return 0;
60 DCHECK(max_texture_size_.height() - 2 * border_texels_);
61 int y = (src_position - border_texels_) / (max_texture_size_.height() - 2 * border_texels_);
62 return std::min(std::max(y, 0), num_tiles_y_ - 1);
65 gfx::Rect TilingData::TileBounds(int i, int j) const {
66 AssertTile(i, j);
67 int x = TilePositionX(i);
68 int y = TilePositionY(j);
69 int width = TileSizeX(i);
70 int height = TileSizeY(j);
71 DCHECK_GE(x, 0);
72 DCHECK_GE(y, 0);
73 DCHECK_GE(width, 0);
74 DCHECK_GE(height, 0);
75 DCHECK_LE(x, total_size_.width());
76 DCHECK_LE(y, total_size_.height());
77 return gfx::Rect(x, y, width, height);
80 gfx::Rect TilingData::TileBoundsWithBorder(int i, int j) const {
81 gfx::Rect bounds = TileBounds(i, j);
83 if (border_texels_) {
84 int x1 = bounds.x();
85 int x2 = bounds.right();
86 int y1 = bounds.y();
87 int y2 = bounds.bottom();
89 if (i > 0)
90 x1--;
91 if (i < (num_tiles_x_ - 1))
92 x2++;
93 if (j > 0)
94 y1--;
95 if (j < (num_tiles_y_ - 1))
96 y2++;
98 bounds = gfx::Rect(x1, y1, x2 - x1, y2 - y1);
101 return bounds;
104 int TilingData::TilePositionX(int x_index) const {
105 DCHECK_GE(x_index, 0);
106 DCHECK_LT(x_index, num_tiles_x_);
108 int pos = 0;
109 for (int i = 0; i < x_index; i++)
110 pos += TileSizeX(i);
112 return pos;
115 int TilingData::TilePositionY(int y_index) const {
116 DCHECK_GE(y_index, 0);
117 DCHECK_LT(y_index, num_tiles_y_);
119 int pos = 0;
120 for (int i = 0; i < y_index; i++)
121 pos += TileSizeY(i);
123 return pos;
126 int TilingData::TileSizeX(int x_index) const {
127 DCHECK_GE(x_index, 0);
128 DCHECK_LT(x_index, num_tiles_x_);
130 if (!x_index && num_tiles_x_ == 1)
131 return total_size_.width();
132 if (!x_index && num_tiles_x_ > 1)
133 return max_texture_size_.width() - border_texels_;
134 if (x_index < num_tiles_x_ - 1)
135 return max_texture_size_.width() - 2 * border_texels_;
136 if (x_index == num_tiles_x_ - 1)
137 return total_size_.width() - TilePositionX(x_index);
139 NOTREACHED();
140 return 0;
143 int TilingData::TileSizeY(int y_index) const {
144 DCHECK_GE(y_index, 0);
145 DCHECK_LT(y_index, num_tiles_y_);
147 if (!y_index && num_tiles_y_ == 1)
148 return total_size_.height();
149 if (!y_index && num_tiles_y_ > 1)
150 return max_texture_size_.height() - border_texels_;
151 if (y_index < num_tiles_y_ - 1)
152 return max_texture_size_.height() - 2 * border_texels_;
153 if (y_index == num_tiles_y_ - 1)
154 return total_size_.height() - TilePositionY(y_index);
156 NOTREACHED();
157 return 0;
160 gfx::Vector2d TilingData::TextureOffset(int x_index, int y_index) const {
161 int left = (!x_index || num_tiles_x_ == 1) ? 0 : border_texels_;
162 int top = (!y_index || num_tiles_y_ == 1) ? 0 : border_texels_;
164 return gfx::Vector2d(left, top);
167 void TilingData::RecomputeNumTiles() {
168 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_);
172 } // namespace cc