1 // Copyright 2011 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/resources/layer_tiling_data.h"
9 #include "base/logging.h"
13 scoped_ptr
<LayerTilingData
> LayerTilingData::Create(const gfx::Size
& tile_size
,
14 BorderTexelOption border
) {
15 return make_scoped_ptr(new LayerTilingData(tile_size
, border
));
18 LayerTilingData::LayerTilingData(const gfx::Size
& tile_size
,
19 BorderTexelOption border
)
20 : tiling_data_(tile_size
, gfx::Size(), border
== HAS_BORDER_TEXELS
) {
21 SetTileSize(tile_size
);
24 LayerTilingData::~LayerTilingData() {}
26 void LayerTilingData::SetTileSize(const gfx::Size
& size
) {
27 if (tile_size() == size
)
32 tiling_data_
.SetMaxTextureSize(size
);
35 gfx::Size
LayerTilingData::tile_size() const {
36 return tiling_data_
.max_texture_size();
39 void LayerTilingData::SetBorderTexelOption(
40 BorderTexelOption border_texel_option
) {
41 bool border_texels
= border_texel_option
== HAS_BORDER_TEXELS
;
42 if (has_border_texels() == border_texels
)
46 tiling_data_
.SetHasBorderTexels(border_texels
);
49 const LayerTilingData
& LayerTilingData::operator=
50 (const LayerTilingData
& tiler
) {
51 tiling_data_
= tiler
.tiling_data_
;
56 void LayerTilingData::AddTile(scoped_ptr
<Tile
> tile
, int i
, int j
) {
57 DCHECK(!TileAt(i
, j
));
59 tiles_
.add(std::make_pair(i
, j
), tile
.Pass());
62 scoped_ptr
<LayerTilingData::Tile
> LayerTilingData::TakeTile(int i
, int j
) {
63 return tiles_
.take_and_erase(std::make_pair(i
, j
));
66 LayerTilingData::Tile
* LayerTilingData::TileAt(int i
, int j
) const {
67 return tiles_
.get(std::make_pair(i
, j
));
70 void LayerTilingData::ContentRectToTileIndices(const gfx::Rect
& content_rect
,
75 // An empty rect doesn't result in an empty set of tiles, so don't pass an
77 // TODO(enne): Possibly we should fill a vector of tiles instead, since the
78 // normal use of this function is to enumerate some tiles.
79 DCHECK(!content_rect
.IsEmpty());
81 *left
= tiling_data_
.TileXIndexFromSrcCoord(content_rect
.x());
82 *top
= tiling_data_
.TileYIndexFromSrcCoord(content_rect
.y());
83 *right
= tiling_data_
.TileXIndexFromSrcCoord(content_rect
.right() - 1);
84 *bottom
= tiling_data_
.TileYIndexFromSrcCoord(content_rect
.bottom() - 1);
87 gfx::Rect
LayerTilingData::TileRect(const Tile
* tile
) const {
88 gfx::Rect tile_rect
= tiling_data_
.TileBoundsWithBorder(tile
->i(), tile
->j());
89 tile_rect
.set_size(tile_size());
93 Region
LayerTilingData::OpaqueRegionInContentRect(
94 const gfx::Rect
& content_rect
) const {
95 if (content_rect
.IsEmpty())
99 int left
, top
, right
, bottom
;
100 ContentRectToTileIndices(content_rect
, &left
, &top
, &right
, &bottom
);
101 for (int j
= top
; j
<= bottom
; ++j
) {
102 for (int i
= left
; i
<= right
; ++i
) {
103 Tile
* tile
= TileAt(i
, j
);
107 gfx::Rect tile_opaque_rect
=
108 gfx::IntersectRects(content_rect
, tile
->opaque_rect());
109 opaque_region
.Union(tile_opaque_rect
);
112 return opaque_region
;
115 void LayerTilingData::SetBounds(const gfx::Size
& size
) {
116 tiling_data_
.SetTotalSize(size
);
117 if (size
.IsEmpty()) {
122 // Any tiles completely outside our new bounds are invalid and should be
124 int left
, top
, right
, bottom
;
125 ContentRectToTileIndices(
126 gfx::Rect(size
), &left
, &top
, &right
, &bottom
);
127 std::vector
<TileMapKey
> invalid_tile_keys
;
128 for (TileMap::const_iterator it
= tiles_
.begin(); it
!= tiles_
.end(); ++it
) {
129 if (it
->first
.first
> right
|| it
->first
.second
> bottom
)
130 invalid_tile_keys
.push_back(it
->first
);
132 for (size_t i
= 0; i
< invalid_tile_keys
.size(); ++i
)
133 tiles_
.erase(invalid_tile_keys
[i
]);