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. 4 5 #ifndef CC_BASE_TILING_DATA_H_ 6 #define CC_BASE_TILING_DATA_H_ 7 8 #include <utility> 9 10 #include "base/basictypes.h" 11 #include "base/logging.h" 12 #include "cc/base/cc_export.h" 13 #include "ui/gfx/rect.h" 14 #include "ui/gfx/size.h" 15 16 namespace gfx { 17 class Vector2d; 18 } 19 20 namespace cc { 21 22 class CC_EXPORT TilingData { 23 public: 24 TilingData(); 25 TilingData( 26 gfx::Size max_texture_size, 27 gfx::Size total_size, 28 bool has_border_texels); 29 TilingData( 30 gfx::Size max_texture_size, 31 gfx::Size total_size, 32 int border_texels); 33 34 gfx::Size total_size() const { return total_size_; } 35 void SetTotalSize(const gfx::Size total_size); 36 37 gfx::Size max_texture_size() const { return max_texture_size_; } 38 void SetMaxTextureSize(gfx::Size max_texture_size); 39 40 int border_texels() const { return border_texels_; } 41 void SetHasBorderTexels(bool has_border_texels); 42 void SetBorderTexels(int border_texels); 43 44 bool has_empty_bounds() const { return !num_tiles_x_ || !num_tiles_y_; } 45 int num_tiles_x() const { return num_tiles_x_; } 46 int num_tiles_y() const { return num_tiles_y_; } 47 // Return the tile index whose non-border texels include src_position. 48 int TileXIndexFromSrcCoord(int src_position) const; 49 int TileYIndexFromSrcCoord(int src_position) const; 50 // Return the lowest tile index whose border texels include src_position. 51 int FirstBorderTileXIndexFromSrcCoord(int src_position) const; 52 int FirstBorderTileYIndexFromSrcCoord(int src_position) const; 53 // Return the highest tile index whose border texels include src_position. 54 int LastBorderTileXIndexFromSrcCoord(int src_position) const; 55 int LastBorderTileYIndexFromSrcCoord(int src_position) const; 56 57 gfx::Rect TileBounds(int i, int j) const; 58 gfx::Rect TileBoundsWithBorder(int i, int j) const; 59 int TilePositionX(int x_index) const; 60 int TilePositionY(int y_index) const; 61 int TileSizeX(int x_index) const; 62 int TileSizeY(int y_index) const; 63 64 // Difference between TileBound's and TileBoundWithBorder's origin(). 65 gfx::Vector2d TextureOffset(int x_index, int y_index) const; 66 67 class CC_EXPORT BaseIterator { 68 public: 69 operator bool() const { return index_x_ != -1 && index_y_ != -1; } 70 71 int index_x() const { return index_x_; } 72 int index_y() const { return index_y_; } 73 std::pair<int, int> index() const { 74 return std::make_pair(index_x_, index_y_); 75 } 76 77 protected: 78 explicit BaseIterator(const TilingData* tiling_data); 79 void done() { 80 index_x_ = -1; 81 index_y_ = -1; 82 } 83 84 const TilingData* tiling_data_; 85 int index_x_; 86 int index_y_; 87 }; 88 89 // Iterate through tiles whose bounds + optional border intersect with |rect|. 90 class CC_EXPORT Iterator : public BaseIterator { 91 public: 92 Iterator(); 93 Iterator(const TilingData* tiling_data, 94 const gfx::Rect& tiling_rect, 95 bool include_borders); 96 Iterator& operator++(); 97 98 private: 99 int left_; 100 int right_; 101 int bottom_; 102 }; 103 104 // Iterate through all indices whose bounds + border intersect with 105 // |consider| but which also do not intersect with |ignore|. 106 class CC_EXPORT DifferenceIterator : public BaseIterator { 107 public: 108 DifferenceIterator( 109 const TilingData* tiling_data, 110 gfx::Rect consider, 111 gfx::Rect ignore); 112 DifferenceIterator& operator++(); 113 114 private: 115 bool in_ignore_rect() const { 116 return index_x_ >= ignore_left_ && index_x_ <= ignore_right_ && 117 index_y_ >= ignore_top_ && index_y_ <= ignore_bottom_; 118 } 119 120 int consider_left_; 121 int consider_top_; 122 int consider_right_; 123 int consider_bottom_; 124 int ignore_left_; 125 int ignore_top_; 126 int ignore_right_; 127 int ignore_bottom_; 128 }; 129 130 private: 131 void AssertTile(int i, int j) const { 132 DCHECK_GE(i, 0); 133 DCHECK_LT(i, num_tiles_x_); 134 DCHECK_GE(j, 0); 135 DCHECK_LT(j, num_tiles_y_); 136 } 137 138 void RecomputeNumTiles(); 139 140 gfx::Size max_texture_size_; 141 gfx::Size total_size_; 142 int border_texels_; 143 144 // These are computed values. 145 int num_tiles_x_; 146 int num_tiles_y_; 147 }; 148 149 } // namespace cc 150 151 #endif // CC_BASE_TILING_DATA_H_ 152