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_TILE_MANAGER_H_
      6 #define CC_RESOURCES_TILE_MANAGER_H_
      7 
      8 #include <deque>
      9 #include <queue>
     10 #include <set>
     11 #include <utility>
     12 #include <vector>
     13 
     14 #include "base/containers/hash_tables.h"
     15 #include "base/memory/scoped_ptr.h"
     16 #include "base/values.h"
     17 #include "cc/base/ref_counted_managed.h"
     18 #include "cc/base/unique_notifier.h"
     19 #include "cc/debug/rendering_stats_instrumentation.h"
     20 #include "cc/resources/eviction_tile_priority_queue.h"
     21 #include "cc/resources/managed_tile_state.h"
     22 #include "cc/resources/memory_history.h"
     23 #include "cc/resources/picture_pile_impl.h"
     24 #include "cc/resources/prioritized_tile_set.h"
     25 #include "cc/resources/raster_tile_priority_queue.h"
     26 #include "cc/resources/rasterizer.h"
     27 #include "cc/resources/resource_pool.h"
     28 #include "cc/resources/tile.h"
     29 
     30 namespace base {
     31 namespace debug {
     32 class ConvertableToTraceFormat;
     33 class TracedValue;
     34 }
     35 }
     36 
     37 namespace cc {
     38 class PictureLayerImpl;
     39 class ResourceProvider;
     40 
     41 class CC_EXPORT TileManagerClient {
     42  public:
     43   // Returns the set of layers that the tile manager should consider for raster.
     44   // TODO(vmpstr): Change the way we determine if we are ready to activate, so
     45   // that this can be removed.
     46   virtual const std::vector<PictureLayerImpl*>& GetPictureLayers() const = 0;
     47 
     48   // Called when all tiles marked as required for activation are ready to draw.
     49   virtual void NotifyReadyToActivate() = 0;
     50 
     51   // Called when the visible representation of a tile might have changed. Some
     52   // examples are:
     53   // - Tile version initialized.
     54   // - Tile resources freed.
     55   // - Tile marked for on-demand raster.
     56   virtual void NotifyTileStateChanged(const Tile* tile) = 0;
     57 
     58   // Given an empty raster tile priority queue, this will build a priority queue
     59   // that will return tiles in order in which they should be rasterized.
     60   // Note if the queue was previous built, Reset must be called on it.
     61   virtual void BuildRasterQueue(RasterTilePriorityQueue* queue,
     62                                 TreePriority tree_priority) = 0;
     63 
     64   // Given an empty eviction tile priority queue, this will build a priority
     65   // queue that will return tiles in order in which they should be evicted.
     66   // Note if the queue was previous built, Reset must be called on it.
     67   virtual void BuildEvictionQueue(EvictionTilePriorityQueue* queue,
     68                                   TreePriority tree_priority) = 0;
     69 
     70  protected:
     71   virtual ~TileManagerClient() {}
     72 };
     73 
     74 struct RasterTaskCompletionStats {
     75   RasterTaskCompletionStats();
     76 
     77   size_t completed_count;
     78   size_t canceled_count;
     79 };
     80 scoped_refptr<base::debug::ConvertableToTraceFormat>
     81     RasterTaskCompletionStatsAsValue(const RasterTaskCompletionStats& stats);
     82 
     83 // This class manages tiles, deciding which should get rasterized and which
     84 // should no longer have any memory assigned to them. Tile objects are "owned"
     85 // by layers; they automatically register with the manager when they are
     86 // created, and unregister from the manager when they are deleted.
     87 class CC_EXPORT TileManager : public RasterizerClient,
     88                               public RefCountedManager<Tile> {
     89  public:
     90   enum NamedTaskSet {
     91     REQUIRED_FOR_ACTIVATION = 0,
     92     ALL = 1,
     93     // Adding additional values requires increasing kNumberOfTaskSets in
     94     // rasterizer.h
     95   };
     96 
     97   static scoped_ptr<TileManager> Create(
     98       TileManagerClient* client,
     99       base::SequencedTaskRunner* task_runner,
    100       ResourcePool* resource_pool,
    101       Rasterizer* rasterizer,
    102       RenderingStatsInstrumentation* rendering_stats_instrumentation);
    103   virtual ~TileManager();
    104 
    105   void ManageTiles(const GlobalStateThatImpactsTilePriority& state);
    106 
    107   // Returns true when visible tiles have been initialized.
    108   bool UpdateVisibleTiles();
    109 
    110   scoped_refptr<Tile> CreateTile(PicturePileImpl* picture_pile,
    111                                  const gfx::Size& tile_size,
    112                                  const gfx::Rect& content_rect,
    113                                  float contents_scale,
    114                                  int layer_id,
    115                                  int source_frame_number,
    116                                  int flags);
    117 
    118   scoped_refptr<base::debug::ConvertableToTraceFormat> BasicStateAsValue()
    119       const;
    120   void BasicStateAsValueInto(base::debug::TracedValue* dict) const;
    121   const MemoryHistory::Entry& memory_stats_from_last_assign() const {
    122     return memory_stats_from_last_assign_;
    123   }
    124 
    125   void InitializeTilesWithResourcesForTesting(const std::vector<Tile*>& tiles) {
    126     for (size_t i = 0; i < tiles.size(); ++i) {
    127       ManagedTileState& mts = tiles[i]->managed_state();
    128       ManagedTileState::TileVersion& tile_version =
    129           mts.tile_versions[HIGH_QUALITY_RASTER_MODE];
    130 
    131       tile_version.resource_ =
    132           resource_pool_->AcquireResource(tiles[i]->size());
    133 
    134       bytes_releasable_ += BytesConsumedIfAllocated(tiles[i]);
    135       ++resources_releasable_;
    136     }
    137   }
    138 
    139   void ReleaseTileResourcesForTesting(const std::vector<Tile*>& tiles) {
    140     for (size_t i = 0; i < tiles.size(); ++i) {
    141       Tile* tile = tiles[i];
    142       for (int mode = 0; mode < NUM_RASTER_MODES; ++mode) {
    143         FreeResourceForTile(tile, static_cast<RasterMode>(mode));
    144       }
    145     }
    146   }
    147 
    148   void SetGlobalStateForTesting(
    149       const GlobalStateThatImpactsTilePriority& state) {
    150     // Soft limit is used for resource pool such that
    151     // memory returns to soft limit after going over.
    152     if (state != global_state_) {
    153       global_state_ = state;
    154       prioritized_tiles_dirty_ = true;
    155     }
    156   }
    157 
    158   void SetRasterizerForTesting(Rasterizer* rasterizer);
    159 
    160   void FreeResourcesAndCleanUpReleasedTilesForTesting() {
    161     prioritized_tiles_.Clear();
    162     FreeResourcesForReleasedTiles();
    163     CleanUpReleasedTiles();
    164   }
    165 
    166   std::vector<Tile*> AllTilesForTesting() const {
    167     std::vector<Tile*> tiles;
    168     for (TileMap::const_iterator it = tiles_.begin(); it != tiles_.end();
    169          ++it) {
    170       tiles.push_back(it->second);
    171     }
    172     return tiles;
    173   }
    174 
    175  protected:
    176   TileManager(TileManagerClient* client,
    177               const scoped_refptr<base::SequencedTaskRunner>& task_runner,
    178               ResourcePool* resource_pool,
    179               Rasterizer* rasterizer,
    180               RenderingStatsInstrumentation* rendering_stats_instrumentation);
    181 
    182   // Methods called by Tile
    183   friend class Tile;
    184   void DidChangeTilePriority(Tile* tile);
    185 
    186   void FreeResourcesForReleasedTiles();
    187   void CleanUpReleasedTiles();
    188 
    189   // Overriden from RefCountedManager<Tile>:
    190   virtual void Release(Tile* tile) OVERRIDE;
    191 
    192   // Overriden from RasterizerClient:
    193   virtual void DidFinishRunningTasks(TaskSet task_set) OVERRIDE;
    194   virtual TaskSetCollection TasksThatShouldBeForcedToComplete() const OVERRIDE;
    195 
    196   typedef std::vector<Tile*> TileVector;
    197   typedef std::set<Tile*> TileSet;
    198 
    199   // Virtual for test
    200   virtual void ScheduleTasks(
    201       const TileVector& tiles_that_need_to_be_rasterized);
    202 
    203   void AssignGpuMemoryToTiles(PrioritizedTileSet* tiles,
    204                               TileVector* tiles_that_need_to_be_rasterized);
    205   void GetTilesWithAssignedBins(PrioritizedTileSet* tiles);
    206 
    207  private:
    208   void OnImageDecodeTaskCompleted(int layer_id,
    209                                   SkPixelRef* pixel_ref,
    210                                   bool was_canceled);
    211   void OnRasterTaskCompleted(Tile::Id tile,
    212                              scoped_ptr<ScopedResource> resource,
    213                              RasterMode raster_mode,
    214                              const PicturePileImpl::Analysis& analysis,
    215                              bool was_canceled);
    216 
    217   inline size_t BytesConsumedIfAllocated(const Tile* tile) const {
    218     return Resource::MemorySizeBytes(tile->size(),
    219                                      resource_pool_->resource_format());
    220   }
    221 
    222   void FreeResourceForTile(Tile* tile, RasterMode mode);
    223   void FreeResourcesForTile(Tile* tile);
    224   void FreeUnusedResourcesForTile(Tile* tile);
    225   void FreeResourcesForTileAndNotifyClientIfTileWasReadyToDraw(Tile* tile);
    226   scoped_refptr<ImageDecodeTask> CreateImageDecodeTask(Tile* tile,
    227                                                        SkPixelRef* pixel_ref);
    228   scoped_refptr<RasterTask> CreateRasterTask(Tile* tile);
    229   void UpdatePrioritizedTileSetIfNeeded();
    230 
    231   bool IsReadyToActivate() const;
    232   void CheckIfReadyToActivate();
    233 
    234   TileManagerClient* client_;
    235   scoped_refptr<base::SequencedTaskRunner> task_runner_;
    236   ResourcePool* resource_pool_;
    237   Rasterizer* rasterizer_;
    238   GlobalStateThatImpactsTilePriority global_state_;
    239 
    240   typedef base::hash_map<Tile::Id, Tile*> TileMap;
    241   TileMap tiles_;
    242 
    243   PrioritizedTileSet prioritized_tiles_;
    244   bool prioritized_tiles_dirty_;
    245 
    246   bool all_tiles_that_need_to_be_rasterized_have_memory_;
    247   bool all_tiles_required_for_activation_have_memory_;
    248 
    249   size_t bytes_releasable_;
    250   size_t resources_releasable_;
    251 
    252   bool ever_exceeded_memory_budget_;
    253   MemoryHistory::Entry memory_stats_from_last_assign_;
    254 
    255   RenderingStatsInstrumentation* rendering_stats_instrumentation_;
    256 
    257   bool did_initialize_visible_tile_;
    258   bool did_check_for_completed_tasks_since_last_schedule_tasks_;
    259   bool did_oom_on_last_assign_;
    260 
    261   typedef base::hash_map<uint32_t, scoped_refptr<ImageDecodeTask> >
    262       PixelRefTaskMap;
    263   typedef base::hash_map<int, PixelRefTaskMap> LayerPixelRefTaskMap;
    264   LayerPixelRefTaskMap image_decode_tasks_;
    265 
    266   typedef base::hash_map<int, int> LayerCountMap;
    267   LayerCountMap used_layer_counts_;
    268 
    269   RasterTaskCompletionStats update_visible_tiles_stats_;
    270 
    271   std::vector<Tile*> released_tiles_;
    272 
    273   ResourceFormat resource_format_;
    274 
    275   // Queue used when scheduling raster tasks.
    276   RasterTaskQueue raster_queue_;
    277 
    278   std::vector<scoped_refptr<RasterTask> > orphan_raster_tasks_;
    279 
    280   UniqueNotifier ready_to_activate_check_notifier_;
    281 
    282   DISALLOW_COPY_AND_ASSIGN(TileManager);
    283 };
    284 
    285 }  // namespace cc
    286 
    287 #endif  // CC_RESOURCES_TILE_MANAGER_H_
    288