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 <queue>
      9 #include <set>
     10 #include <vector>
     11 
     12 #include "base/containers/hash_tables.h"
     13 #include "base/memory/scoped_ptr.h"
     14 #include "base/values.h"
     15 #include "cc/debug/rendering_stats_instrumentation.h"
     16 #include "cc/resources/managed_tile_state.h"
     17 #include "cc/resources/memory_history.h"
     18 #include "cc/resources/picture_pile_impl.h"
     19 #include "cc/resources/prioritized_tile_set.h"
     20 #include "cc/resources/raster_worker_pool.h"
     21 #include "cc/resources/resource_pool.h"
     22 #include "cc/resources/tile.h"
     23 
     24 namespace cc {
     25 class ResourceProvider;
     26 
     27 class CC_EXPORT TileManagerClient {
     28  public:
     29   virtual void NotifyReadyToActivate() = 0;
     30 
     31  protected:
     32   virtual ~TileManagerClient() {}
     33 };
     34 
     35 struct RasterTaskCompletionStats {
     36   RasterTaskCompletionStats();
     37 
     38   size_t completed_count;
     39   size_t canceled_count;
     40 };
     41 scoped_ptr<base::Value> RasterTaskCompletionStatsAsValue(
     42     const RasterTaskCompletionStats& stats);
     43 
     44 // This class manages tiles, deciding which should get rasterized and which
     45 // should no longer have any memory assigned to them. Tile objects are "owned"
     46 // by layers; they automatically register with the manager when they are
     47 // created, and unregister from the manager when they are deleted.
     48 class CC_EXPORT TileManager : public RasterWorkerPoolClient {
     49  public:
     50   static scoped_ptr<TileManager> Create(
     51       TileManagerClient* client,
     52       ResourceProvider* resource_provider,
     53       size_t num_raster_threads,
     54       RenderingStatsInstrumentation* rendering_stats_instrumentation,
     55       bool use_map_image);
     56   virtual ~TileManager();
     57 
     58   const GlobalStateThatImpactsTilePriority& GlobalState() const {
     59       return global_state_;
     60   }
     61   void SetGlobalState(const GlobalStateThatImpactsTilePriority& state);
     62 
     63   void ManageTiles();
     64 
     65   // Returns true when visible tiles have been initialized.
     66   bool UpdateVisibleTiles();
     67 
     68   scoped_ptr<base::Value> BasicStateAsValue() const;
     69   scoped_ptr<base::Value> AllTilesAsValue() const;
     70   void GetMemoryStats(size_t* memory_required_bytes,
     71                       size_t* memory_nice_to_have_bytes,
     72                       size_t* memory_used_bytes) const;
     73 
     74   const MemoryHistory::Entry& memory_stats_from_last_assign() const {
     75     return memory_stats_from_last_assign_;
     76   }
     77 
     78   bool AreTilesRequiredForActivationReady() const {
     79     return all_tiles_required_for_activation_have_been_initialized_;
     80   }
     81 
     82  protected:
     83   TileManager(TileManagerClient* client,
     84               ResourceProvider* resource_provider,
     85               scoped_ptr<RasterWorkerPool> raster_worker_pool,
     86               size_t num_raster_threads,
     87               RenderingStatsInstrumentation* rendering_stats_instrumentation,
     88               GLenum texture_format);
     89 
     90   // Methods called by Tile
     91   friend class Tile;
     92   void RegisterTile(Tile* tile);
     93   void UnregisterTile(Tile* tile);
     94 
     95   // Overriden from RasterWorkerPoolClient:
     96   virtual bool ShouldForceTasksRequiredForActivationToComplete() const
     97       OVERRIDE;
     98   virtual void DidFinishRunningTasks() OVERRIDE;
     99   virtual void DidFinishRunningTasksRequiredForActivation() OVERRIDE;
    100 
    101   typedef std::vector<Tile*> TileVector;
    102   typedef std::set<Tile*> TileSet;
    103 
    104   // Virtual for test
    105   virtual void ScheduleTasks(
    106       const TileVector& tiles_that_need_to_be_rasterized);
    107 
    108   void AssignGpuMemoryToTiles(
    109       PrioritizedTileSet* tiles,
    110       TileVector* tiles_that_need_to_be_rasterized);
    111   void GetTilesWithAssignedBins(PrioritizedTileSet* tiles);
    112   void GetPrioritizedTileSet(PrioritizedTileSet* tiles);
    113 
    114  private:
    115   void OnImageDecodeTaskCompleted(
    116       int layer_id,
    117       skia::LazyPixelRef* pixel_ref,
    118       bool was_canceled);
    119   void OnRasterTaskCompleted(
    120       Tile::Id tile,
    121       scoped_ptr<ResourcePool::Resource> resource,
    122       RasterMode raster_mode,
    123       const PicturePileImpl::Analysis& analysis,
    124       bool was_canceled);
    125 
    126   RasterMode DetermineRasterMode(const Tile* tile) const;
    127   void CleanUpUnusedImageDecodeTasks();
    128   void FreeResourceForTile(Tile* tile, RasterMode mode);
    129   void FreeResourcesForTile(Tile* tile);
    130   void FreeUnusedResourcesForTile(Tile* tile);
    131   RasterWorkerPool::Task CreateImageDecodeTask(
    132       Tile* tile, skia::LazyPixelRef* pixel_ref);
    133   RasterWorkerPool::RasterTask CreateRasterTask(Tile* tile);
    134   scoped_ptr<base::Value> GetMemoryRequirementsAsValue() const;
    135 
    136   TileManagerClient* client_;
    137   scoped_ptr<ResourcePool> resource_pool_;
    138   scoped_ptr<RasterWorkerPool> raster_worker_pool_;
    139   GlobalStateThatImpactsTilePriority global_state_;
    140 
    141   typedef base::hash_map<Tile::Id, Tile*> TileMap;
    142   TileMap tiles_;
    143 
    144   PrioritizedTileSet prioritized_tiles_;
    145 
    146   bool all_tiles_that_need_to_be_rasterized_have_memory_;
    147   bool all_tiles_required_for_activation_have_memory_;
    148   bool all_tiles_required_for_activation_have_been_initialized_;
    149 
    150   bool ever_exceeded_memory_budget_;
    151   MemoryHistory::Entry memory_stats_from_last_assign_;
    152 
    153   RenderingStatsInstrumentation* rendering_stats_instrumentation_;
    154 
    155   bool did_initialize_visible_tile_;
    156 
    157   GLenum texture_format_;
    158 
    159   typedef base::hash_map<uint32_t, RasterWorkerPool::Task> PixelRefTaskMap;
    160   typedef base::hash_map<int, PixelRefTaskMap> LayerPixelRefTaskMap;
    161   LayerPixelRefTaskMap image_decode_tasks_;
    162 
    163   RasterTaskCompletionStats update_visible_tiles_stats_;
    164 
    165   DISALLOW_COPY_AND_ASSIGN(TileManager);
    166 };
    167 
    168 }  // namespace cc
    169 
    170 #endif  // CC_RESOURCES_TILE_MANAGER_H_
    171