Home | History | Annotate | Download | only in resources
      1 // Copyright 2012 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_RESOURCES_PICTURE_LAYER_TILING_H_
      6 #define CC_RESOURCES_PICTURE_LAYER_TILING_H_
      7 
      8 #include <utility>
      9 #include <vector>
     10 
     11 #include "base/basictypes.h"
     12 #include "base/containers/hash_tables.h"
     13 #include "base/memory/scoped_ptr.h"
     14 #include "cc/base/cc_export.h"
     15 #include "cc/base/region.h"
     16 #include "cc/base/tiling_data.h"
     17 #include "cc/resources/tile.h"
     18 #include "cc/resources/tile_priority.h"
     19 #include "ui/gfx/rect.h"
     20 
     21 namespace cc {
     22 
     23 class PictureLayerTiling;
     24 
     25 class CC_EXPORT PictureLayerTilingClient {
     26  public:
     27   // Create a tile at the given content_rect (in the contents scale of the
     28   // tiling) This might return null if the client cannot create such a tile.
     29   virtual scoped_refptr<Tile> CreateTile(
     30     PictureLayerTiling* tiling,
     31     gfx::Rect content_rect) = 0;
     32   virtual void UpdatePile(Tile* tile) = 0;
     33   virtual gfx::Size CalculateTileSize(
     34     gfx::Size content_bounds) const = 0;
     35   virtual const Region* GetInvalidation() = 0;
     36   virtual const PictureLayerTiling* GetTwinTiling(
     37       const PictureLayerTiling* tiling) = 0;
     38 
     39  protected:
     40   virtual ~PictureLayerTilingClient() {}
     41 };
     42 
     43 class CC_EXPORT PictureLayerTiling {
     44  public:
     45   ~PictureLayerTiling();
     46 
     47   // Create a tiling with no tiles.  CreateTiles must be called to add some.
     48   static scoped_ptr<PictureLayerTiling> Create(
     49       float contents_scale,
     50       gfx::Size layer_bounds,
     51       PictureLayerTilingClient* client);
     52   gfx::Size layer_bounds() const { return layer_bounds_; }
     53   void SetLayerBounds(gfx::Size layer_bounds);
     54   void Invalidate(const Region& layer_region);
     55   void CreateMissingTilesInLiveTilesRect();
     56 
     57   void SetCanUseLCDText(bool can_use_lcd_text);
     58 
     59   void SetClient(PictureLayerTilingClient* client);
     60   void set_resolution(TileResolution resolution) { resolution_ = resolution; }
     61   TileResolution resolution() const { return resolution_; }
     62 
     63   gfx::Rect ContentRect() const;
     64   gfx::SizeF ContentSizeF() const;
     65   gfx::Rect live_tiles_rect() const { return live_tiles_rect_; }
     66   gfx::Size tile_size() const { return tiling_data_.max_texture_size(); }
     67   float contents_scale() const { return contents_scale_; }
     68 
     69   void CreateAllTilesForTesting() {
     70     SetLiveTilesRect(gfx::Rect(tiling_data_.total_size()));
     71   }
     72 
     73   std::vector<Tile*> AllTilesForTesting() const {
     74     std::vector<Tile*> all_tiles;
     75     for (TileMap::const_iterator it = tiles_.begin();
     76          it != tiles_.end(); ++it)
     77       all_tiles.push_back(it->second.get());
     78     return all_tiles;
     79   }
     80 
     81   // Iterate over all tiles to fill content_rect.  Even if tiles are invalid
     82   // (i.e. no valid resource) this tiling should still iterate over them.
     83   // The union of all geometry_rect calls for each element iterated over should
     84   // exactly equal content_rect and no two geometry_rects should intersect.
     85   class CC_EXPORT CoverageIterator {
     86    public:
     87     CoverageIterator();
     88     CoverageIterator(const PictureLayerTiling* tiling,
     89         float dest_scale,
     90         gfx::Rect rect);
     91     ~CoverageIterator();
     92 
     93     // Visible rect (no borders), always in the space of content_rect,
     94     // regardless of the contents scale of the tiling.
     95     gfx::Rect geometry_rect() const;
     96     // Texture rect (in texels) for geometry_rect
     97     gfx::RectF texture_rect() const;
     98     gfx::Size texture_size() const;
     99 
    100     // Full rect (including borders) of the current tile, always in the space
    101     // of content_rect, regardless of the contents scale of the tiling.
    102     gfx::Rect full_tile_geometry_rect() const;
    103 
    104     Tile* operator->() const { return current_tile_; }
    105     Tile* operator*() const { return current_tile_; }
    106 
    107     CoverageIterator& operator++();
    108     operator bool() const { return tile_j_ <= bottom_; }
    109 
    110    private:
    111     const PictureLayerTiling* tiling_;
    112     gfx::Rect dest_rect_;
    113     float dest_to_content_scale_;
    114 
    115     Tile* current_tile_;
    116     gfx::Rect current_geometry_rect_;
    117     int tile_i_;
    118     int tile_j_;
    119     int left_;
    120     int top_;
    121     int right_;
    122     int bottom_;
    123 
    124     friend class PictureLayerTiling;
    125   };
    126 
    127   Region OpaqueRegionInContentRect(gfx::Rect content_rect) const;
    128 
    129   void Reset();
    130 
    131   void UpdateTilePriorities(
    132       WhichTree tree,
    133       gfx::Size device_viewport,
    134       gfx::Rect viewport_in_layer_space,
    135       gfx::Rect visible_layer_rect,
    136       gfx::Size last_layer_bounds,
    137       gfx::Size current_layer_bounds,
    138       float last_layer_contents_scale,
    139       float current_layer_contents_scale,
    140       const gfx::Transform& last_screen_transform,
    141       const gfx::Transform& current_screen_transform,
    142       double current_frame_time_in_seconds,
    143       size_t max_tiles_for_interest_area);
    144 
    145   // Copies the src_tree priority into the dst_tree priority for all tiles.
    146   // The src_tree priority is reset to the lowest priority possible.  This
    147   // also updates the pile on each tile to be the current client's pile.
    148   void DidBecomeActive();
    149 
    150   // Resets the active priority for all tiles in a tiling, when an active
    151   // tiling is becoming recycled. This may include some tiles which are
    152   // not in the the pending tiling (due to invalidations). This must
    153   // be called before DidBecomeActive, as it resets the active priority
    154   // while DidBecomeActive promotes pending priority on a similar set of tiles.
    155   void DidBecomeRecycled();
    156 
    157   void UpdateTilesToCurrentPile();
    158 
    159   bool NeedsUpdateForFrameAtTime(double frame_time_in_seconds) {
    160     return frame_time_in_seconds != last_impl_frame_time_in_seconds_;
    161   }
    162 
    163   scoped_ptr<base::Value> AsValue() const;
    164   size_t GPUMemoryUsageInBytes() const;
    165 
    166   static gfx::Rect ExpandRectEquallyToAreaBoundedBy(
    167       gfx::Rect starting_rect,
    168       int64 target_area,
    169       gfx::Rect bounding_rect);
    170 
    171   bool has_ever_been_updated() const {
    172     return last_impl_frame_time_in_seconds_ != 0.0;
    173   }
    174 
    175  protected:
    176   typedef std::pair<int, int> TileMapKey;
    177   typedef base::hash_map<TileMapKey, scoped_refptr<Tile> > TileMap;
    178 
    179   PictureLayerTiling(float contents_scale,
    180                      gfx::Size layer_bounds,
    181                      PictureLayerTilingClient* client);
    182   void SetLiveTilesRect(gfx::Rect live_tiles_rect);
    183   void CreateTile(int i, int j, const PictureLayerTiling* twin_tiling);
    184   Tile* TileAt(int, int) const;
    185 
    186   // Given properties.
    187   float contents_scale_;
    188   gfx::Size layer_bounds_;
    189   TileResolution resolution_;
    190   PictureLayerTilingClient* client_;
    191 
    192   // Internal data.
    193   TilingData tiling_data_;
    194   TileMap tiles_;  // It is not legal to have a NULL tile in the tiles_ map.
    195   gfx::Rect live_tiles_rect_;
    196 
    197   // State saved for computing velocities based upon finite differences.
    198   double last_impl_frame_time_in_seconds_;
    199 
    200   friend class CoverageIterator;
    201 
    202  private:
    203   DISALLOW_ASSIGN(PictureLayerTiling);
    204 };
    205 
    206 }  // namespace cc
    207 
    208 #endif  // CC_RESOURCES_PICTURE_LAYER_TILING_H_
    209