Home | History | Annotate | Download | only in resources
      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.
      4 
      5 #include "cc/resources/layer_tiling_data.h"
      6 
      7 #include <vector>
      8 
      9 #include "base/logging.h"
     10 #include "cc/base/region.h"
     11 #include "cc/base/simple_enclosed_region.h"
     12 
     13 namespace cc {
     14 
     15 scoped_ptr<LayerTilingData> LayerTilingData::Create(const gfx::Size& tile_size,
     16                                                     BorderTexelOption border) {
     17   return make_scoped_ptr(new LayerTilingData(tile_size, border));
     18 }
     19 
     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);
     24 }
     25 
     26 LayerTilingData::~LayerTilingData() {}
     27 
     28 void LayerTilingData::SetTileSize(const gfx::Size& size) {
     29   if (tile_size() == size)
     30     return;
     31 
     32   reset();
     33 
     34   tiling_data_.SetMaxTextureSize(size);
     35 }
     36 
     37 gfx::Size LayerTilingData::tile_size() const {
     38   return tiling_data_.max_texture_size();
     39 }
     40 
     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;
     46 
     47   reset();
     48   tiling_data_.SetHasBorderTexels(border_texels);
     49 }
     50 
     51 const LayerTilingData& LayerTilingData::operator=
     52     (const LayerTilingData & tiler) {
     53   tiling_data_ = tiler.tiling_data_;
     54 
     55   return *this;
     56 }
     57 
     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());
     62 }
     63 
     64 scoped_ptr<LayerTilingData::Tile> LayerTilingData::TakeTile(int i, int j) {
     65   return tiles_.take_and_erase(std::make_pair(i, j));
     66 }
     67 
     68 LayerTilingData::Tile* LayerTilingData::TileAt(int i, int j) const {
     69   return tiles_.get(std::make_pair(i, j));
     70 }
     71 
     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());
     82 
     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);
     87 }
     88 
     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;
     93 }
     94 
     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;
    100   }
    101 
    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);
    111   }
    112   for (size_t i = 0; i < invalid_tile_keys.size(); ++i)
    113     tiles_.erase(invalid_tile_keys[i]);
    114 }
    115 
    116 }  // namespace cc
    117