GPU workaround to simulate Out of Memory errors with large textures
[chromium-blink-merge.git] / cc / resources / layer_tiling_data.cc
blob63b003628c346ae02898e785fb127fd7d3844de7
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"
7 #include <vector>
9 #include "base/logging.h"
10 #include "cc/base/region.h"
11 #include "cc/base/simple_enclosed_region.h"
13 namespace cc {
15 scoped_ptr<LayerTilingData> LayerTilingData::Create(const gfx::Size& tile_size,
16 BorderTexelOption border) {
17 return make_scoped_ptr(new LayerTilingData(tile_size, border));
20 LayerTilingData::LayerTilingData(const gfx::Size& tile_size,
21 BorderTexelOption border)
22 : tiling_data_(tile_size, gfx::Size(), border == HAS_BORDER_TEXELS) {
23 SetTileSize(tile_size);
26 LayerTilingData::~LayerTilingData() {}
28 void LayerTilingData::SetTileSize(const gfx::Size& size) {
29 if (tile_size() == size)
30 return;
32 reset();
34 tiling_data_.SetMaxTextureSize(size);
37 gfx::Size LayerTilingData::tile_size() const {
38 return tiling_data_.max_texture_size();
41 void LayerTilingData::SetBorderTexelOption(
42 BorderTexelOption border_texel_option) {
43 bool border_texels = border_texel_option == HAS_BORDER_TEXELS;
44 if (has_border_texels() == border_texels)
45 return;
47 reset();
48 tiling_data_.SetHasBorderTexels(border_texels);
51 const LayerTilingData& LayerTilingData::operator=
52 (const LayerTilingData & tiler) {
53 tiling_data_ = tiler.tiling_data_;
55 return *this;
58 void LayerTilingData::AddTile(scoped_ptr<Tile> tile, int i, int j) {
59 DCHECK(!TileAt(i, j));
60 tile->move_to(i, j);
61 tiles_.add(std::make_pair(i, j), tile.Pass());
64 scoped_ptr<LayerTilingData::Tile> LayerTilingData::TakeTile(int i, int j) {
65 return tiles_.take_and_erase(std::make_pair(i, j));
68 LayerTilingData::Tile* LayerTilingData::TileAt(int i, int j) const {
69 return tiles_.get(std::make_pair(i, j));
72 void LayerTilingData::ContentRectToTileIndices(const gfx::Rect& content_rect,
73 int* left,
74 int* top,
75 int* right,
76 int* bottom) const {
77 // An empty rect doesn't result in an empty set of tiles, so don't pass an
78 // empty rect.
79 // TODO(enne): Possibly we should fill a vector of tiles instead, since the
80 // normal use of this function is to enumerate some tiles.
81 DCHECK(!content_rect.IsEmpty());
83 *left = tiling_data_.TileXIndexFromSrcCoord(content_rect.x());
84 *top = tiling_data_.TileYIndexFromSrcCoord(content_rect.y());
85 *right = tiling_data_.TileXIndexFromSrcCoord(content_rect.right() - 1);
86 *bottom = tiling_data_.TileYIndexFromSrcCoord(content_rect.bottom() - 1);
89 gfx::Rect LayerTilingData::TileRect(const Tile* tile) const {
90 gfx::Rect tile_rect = tiling_data_.TileBoundsWithBorder(tile->i(), tile->j());
91 tile_rect.set_size(tile_size());
92 return tile_rect;
95 void LayerTilingData::SetTilingSize(const gfx::Size& tiling_size) {
96 tiling_data_.SetTilingSize(tiling_size);
97 if (tiling_size.IsEmpty()) {
98 tiles_.clear();
99 return;
102 // Any tiles completely outside our new bounds are invalid and should be
103 // dropped.
104 int left, top, right, bottom;
105 ContentRectToTileIndices(
106 gfx::Rect(tiling_size), &left, &top, &right, &bottom);
107 std::vector<TileMapKey> invalid_tile_keys;
108 for (TileMap::const_iterator it = tiles_.begin(); it != tiles_.end(); ++it) {
109 if (it->first.first > right || it->first.second > bottom)
110 invalid_tile_keys.push_back(it->first);
112 for (size_t i = 0; i < invalid_tile_keys.size(); ++i)
113 tiles_.erase(invalid_tile_keys[i]);
116 } // namespace cc