#ifndef CC_RESOURCES_PICTURE_LAYER_TILING_H_
#define CC_RESOURCES_PICTURE_LAYER_TILING_H_
#include <utility>
#include <vector>
#include "base/basictypes.h"
#include "base/containers/hash_tables.h"
#include "base/memory/scoped_ptr.h"
#include "cc/base/cc_export.h"
#include "cc/base/region.h"
#include "cc/base/tiling_data.h"
#include "cc/resources/tile.h"
#include "cc/resources/tile_priority.h"
#include "ui/gfx/rect.h"
namespace cc {
class PictureLayerTiling;
class CC_EXPORT PictureLayerTilingClient {
public:
virtual scoped_refptr<Tile> CreateTile(
PictureLayerTiling* tiling,
const gfx::Rect& content_rect) = 0;
virtual void UpdatePile(Tile* tile) = 0;
virtual gfx::Size CalculateTileSize(
const gfx::Size& content_bounds) const = 0;
virtual const Region* GetInvalidation() = 0;
virtual const PictureLayerTiling* GetTwinTiling(
const PictureLayerTiling* tiling) const = 0;
virtual size_t GetMaxTilesForInterestArea() const = 0;
virtual float GetSkewportTargetTimeInSeconds() const = 0;
virtual int GetSkewportExtrapolationLimitInContentPixels() const = 0;
protected:
virtual ~PictureLayerTilingClient() {}
};
class CC_EXPORT PictureLayerTiling {
public:
class CC_EXPORT TilingRasterTileIterator {
public:
TilingRasterTileIterator();
TilingRasterTileIterator(PictureLayerTiling* tiling, WhichTree tree);
~TilingRasterTileIterator();
operator bool() const {
return current_tile_ && TileNeedsRaster(current_tile_);
}
Tile* operator*() { return current_tile_; }
TilePriority::PriorityBin get_type() const { return type_; }
TilingRasterTileIterator& operator++();
gfx::Rect TileBounds() const {
DCHECK(*this);
if (type_ == TilePriority::NOW) {
return tiling_->tiling_data_.TileBounds(visible_iterator_.index_x(),
visible_iterator_.index_y());
}
return tiling_->tiling_data_.TileBounds(spiral_iterator_.index_x(),
spiral_iterator_.index_y());
}
private:
void AdvancePhase();
bool TileNeedsRaster(Tile* tile) const {
RasterMode mode = tile->DetermineRasterModeForTree(tree_);
return tile->NeedsRasterForMode(mode);
};
PictureLayerTiling* tiling_;
TilePriority::PriorityBin type_;
gfx::Rect visible_rect_in_content_space_;
gfx::Rect skewport_in_content_space_;
gfx::Rect eventually_rect_in_content_space_;
WhichTree tree_;
Tile* current_tile_;
TilingData::Iterator visible_iterator_;
TilingData::SpiralDifferenceIterator spiral_iterator_;
};
class CC_EXPORT TilingEvictionTileIterator {
public:
TilingEvictionTileIterator();
TilingEvictionTileIterator(PictureLayerTiling* tiling, WhichTree tree);
~TilingEvictionTileIterator();
operator bool();
Tile* operator*();
TilingEvictionTileIterator& operator++();
TilePriority::PriorityBin get_type() {
DCHECK(*this);
return (*tile_iterator_)->priority(tree_).priority_bin;
}
private:
void Initialize();
bool IsValid() const { return is_valid_; }
bool is_valid_;
PictureLayerTiling* tiling_;
WhichTree tree_;
std::vector<Tile*>::iterator tile_iterator_;
};
~PictureLayerTiling();
static scoped_ptr<PictureLayerTiling> Create(
float contents_scale,
const gfx::Size& layer_bounds,
PictureLayerTilingClient* client);
gfx::Size layer_bounds() const { return layer_bounds_; }
void SetLayerBounds(const gfx::Size& layer_bounds);
void Invalidate(const Region& layer_region);
void CreateMissingTilesInLiveTilesRect();
void SetCanUseLCDText(bool can_use_lcd_text);
void SetClient(PictureLayerTilingClient* client);
void set_resolution(TileResolution resolution) { resolution_ = resolution; }
TileResolution resolution() const { return resolution_; }
gfx::Rect ContentRect() const;
gfx::SizeF ContentSizeF() const;
gfx::Rect live_tiles_rect() const { return live_tiles_rect_; }
gfx::Size tile_size() const { return tiling_data_.max_texture_size(); }
float contents_scale() const { return contents_scale_; }
Tile* TileAt(int i, int j) const {
TileMap::const_iterator iter = tiles_.find(TileMapKey(i, j));
return (iter == tiles_.end()) ? NULL : iter->second.get();
}
void CreateAllTilesForTesting() {
SetLiveTilesRect(gfx::Rect(tiling_data_.total_size()));
}
std::vector<Tile*> AllTilesForTesting() const {
std::vector<Tile*> all_tiles;
for (TileMap::const_iterator it = tiles_.begin();
it != tiles_.end(); ++it)
all_tiles.push_back(it->second.get());
return all_tiles;
}
class CC_EXPORT CoverageIterator {
public:
CoverageIterator();
CoverageIterator(const PictureLayerTiling* tiling,
float dest_scale,
const gfx::Rect& rect);
~CoverageIterator();
gfx::Rect geometry_rect() const;
gfx::RectF texture_rect() const;
gfx::Size texture_size() const;
gfx::Rect full_tile_geometry_rect() const;
Tile* operator->() const { return current_tile_; }
Tile* operator*() const { return current_tile_; }
CoverageIterator& operator++();
operator bool() const { return tile_j_ <= bottom_; }
int i() const { return tile_i_; }
int j() const { return tile_j_; }
private:
const PictureLayerTiling* tiling_;
gfx::Rect dest_rect_;
float dest_to_content_scale_;
Tile* current_tile_;
gfx::Rect current_geometry_rect_;
int tile_i_;
int tile_j_;
int left_;
int top_;
int right_;
int bottom_;
friend class PictureLayerTiling;
};
Region OpaqueRegionInContentRect(const gfx::Rect& content_rect) const;
void Reset();
void UpdateTilePriorities(WhichTree tree,
const gfx::Rect& visible_layer_rect,
float layer_contents_scale,
double current_frame_time_in_seconds);
void DidBecomeActive();
void DidBecomeRecycled();
void UpdateTilesToCurrentPile();
bool NeedsUpdateForFrameAtTime(double frame_time_in_seconds) {
return frame_time_in_seconds != last_impl_frame_time_in_seconds_;
}
scoped_ptr<base::Value> AsValue() const;
size_t GPUMemoryUsageInBytes() const;
struct RectExpansionCache {
RectExpansionCache();
gfx::Rect previous_start;
gfx::Rect previous_bounds;
gfx::Rect previous_result;
int64 previous_target;
};
static
gfx::Rect ExpandRectEquallyToAreaBoundedBy(
const gfx::Rect& starting_rect,
int64 target_area,
const gfx::Rect& bounding_rect,
RectExpansionCache* cache);
bool has_ever_been_updated() const {
return last_impl_frame_time_in_seconds_ != 0.0;
}
protected:
friend class CoverageIterator;
friend class TilingRasterTileIterator;
friend class TilingEvictionTileIterator;
typedef std::pair<int, int> TileMapKey;
typedef base::hash_map<TileMapKey, scoped_refptr<Tile> > TileMap;
PictureLayerTiling(float contents_scale,
const gfx::Size& layer_bounds,
PictureLayerTilingClient* client);
void SetLiveTilesRect(const gfx::Rect& live_tiles_rect);
Tile* CreateTile(int i, int j, const PictureLayerTiling* twin_tiling);
gfx::Rect ComputeSkewport(double current_frame_time_in_seconds,
const gfx::Rect& visible_rect_in_content_space)
const;
void UpdateEvictionCacheIfNeeded(WhichTree tree);
float contents_scale_;
gfx::Size layer_bounds_;
TileResolution resolution_;
PictureLayerTilingClient* client_;
TilingData tiling_data_;
TileMap tiles_;
gfx::Rect live_tiles_rect_;
double last_impl_frame_time_in_seconds_;
gfx::Rect last_visible_rect_in_content_space_;
gfx::Rect current_visible_rect_in_content_space_;
gfx::Rect current_skewport_;
gfx::Rect current_eventually_rect_;
std::vector<Tile*> eviction_tiles_cache_;
bool eviction_tiles_cache_valid_;
private:
DISALLOW_ASSIGN(PictureLayerTiling);
RectExpansionCache expansion_cache_;
};
}
#endif