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/base/ref_counted_managed.h"
     16 #include "cc/debug/rendering_stats_instrumentation.h"
     17 #include "cc/resources/managed_tile_state.h"
     18 #include "cc/resources/memory_history.h"
     19 #include "cc/resources/picture_pile_impl.h"
     20 #include "cc/resources/prioritized_tile_set.h"
     21 #include "cc/resources/raster_worker_pool.h"
     22 #include "cc/resources/resource_pool.h"
     23 #include "cc/resources/tile.h"
     24 
     25 namespace cc {
     26 class ResourceProvider;
     27 
     28 class CC_EXPORT TileManagerClient {
     29  public:
     30   virtual void NotifyReadyToActivate() = 0;
     31 
     32  protected:
     33   virtual ~TileManagerClient() {}
     34 };
     35 
     36 struct RasterTaskCompletionStats {
     37   RasterTaskCompletionStats();
     38 
     39   size_t completed_count;
     40   size_t canceled_count;
     41 };
     42 scoped_ptr<base::Value> RasterTaskCompletionStatsAsValue(
     43     const RasterTaskCompletionStats& stats);
     44 
     45 // This class manages tiles, deciding which should get rasterized and which
     46 // should no longer have any memory assigned to them. Tile objects are "owned"
     47 // by layers; they automatically register with the manager when they are
     48 // created, and unregister from the manager when they are deleted.
     49 class CC_EXPORT TileManager : public RasterWorkerPoolClient,
     50                               public RefCountedManager<Tile> {
     51  public:
     52   static scoped_ptr<TileManager> Create(
     53       TileManagerClient* client,
     54       ResourceProvider* resource_provider,
     55       size_t num_raster_threads,
     56       RenderingStatsInstrumentation* rendering_stats_instrumentation,
     57       bool use_map_image,
     58       size_t max_transfer_buffer_usage_bytes,
     59       size_t max_raster_usage_bytes,
     60       GLenum map_image_texture_target);
     61   virtual ~TileManager();
     62 
     63   void ManageTiles(const GlobalStateThatImpactsTilePriority& state);
     64 
     65   // Returns true when visible tiles have been initialized.
     66   bool UpdateVisibleTiles();
     67 
     68   scoped_refptr<Tile> CreateTile(PicturePileImpl* picture_pile,
     69                                  gfx::Size tile_size,
     70                                  gfx::Rect content_rect,
     71                                  gfx::Rect opaque_rect,
     72                                  float contents_scale,
     73                                  int layer_id,
     74                                  int source_frame_number,
     75                                  int flags);
     76 
     77   scoped_ptr<base::Value> BasicStateAsValue() const;
     78   scoped_ptr<base::Value> AllTilesAsValue() const;
     79   void GetMemoryStats(size_t* memory_required_bytes,
     80                       size_t* memory_nice_to_have_bytes,
     81                       size_t* memory_allocated_bytes,
     82                       size_t* memory_used_bytes) const;
     83 
     84   const MemoryHistory::Entry& memory_stats_from_last_assign() const {
     85     return memory_stats_from_last_assign_;
     86   }
     87 
     88   void InitializeTilesWithResourcesForTesting(
     89       const std::vector<Tile*>& tiles,
     90       ResourceProvider* resource_provider) {
     91     for (size_t i = 0; i < tiles.size(); ++i) {
     92       ManagedTileState& mts = tiles[i]->managed_state();
     93       ManagedTileState::TileVersion& tile_version =
     94           mts.tile_versions[HIGH_QUALITY_NO_LCD_RASTER_MODE];
     95 
     96       tile_version.resource_ = resource_pool_->AcquireResource(
     97           gfx::Size(1, 1));
     98 
     99       bytes_releasable_ += BytesConsumedIfAllocated(tiles[i]);
    100       ++resources_releasable_;
    101     }
    102   }
    103   RasterWorkerPool* RasterWorkerPoolForTesting() {
    104     return raster_worker_pool_.get();
    105   }
    106 
    107   void SetGlobalStateForTesting(
    108       const GlobalStateThatImpactsTilePriority& state) {
    109     if (state != global_state_) {
    110       global_state_ = state;
    111       prioritized_tiles_dirty_ = true;
    112       resource_pool_->SetResourceUsageLimits(
    113           global_state_.memory_limit_in_bytes,
    114           global_state_.unused_memory_limit_in_bytes,
    115           global_state_.num_resources_limit);
    116     }
    117   }
    118 
    119  protected:
    120   TileManager(TileManagerClient* client,
    121               ResourceProvider* resource_provider,
    122               scoped_ptr<RasterWorkerPool> raster_worker_pool,
    123               size_t num_raster_threads,
    124               size_t max_raster_usage_bytes,
    125               RenderingStatsInstrumentation* rendering_stats_instrumentation);
    126 
    127   // Methods called by Tile
    128   friend class Tile;
    129   void DidChangeTilePriority(Tile* tile);
    130 
    131   void CleanUpReleasedTiles();
    132 
    133   // Overriden from RefCountedManager<Tile>:
    134   virtual void Release(Tile* tile) OVERRIDE;
    135 
    136   // Overriden from RasterWorkerPoolClient:
    137   virtual bool ShouldForceTasksRequiredForActivationToComplete() const
    138       OVERRIDE;
    139   virtual void DidFinishRunningTasks() OVERRIDE;
    140   virtual void DidFinishRunningTasksRequiredForActivation() OVERRIDE;
    141 
    142   typedef std::vector<Tile*> TileVector;
    143   typedef std::set<Tile*> TileSet;
    144 
    145   // Virtual for test
    146   virtual void ScheduleTasks(
    147       const TileVector& tiles_that_need_to_be_rasterized);
    148 
    149   void AssignGpuMemoryToTiles(
    150       PrioritizedTileSet* tiles,
    151       TileVector* tiles_that_need_to_be_rasterized);
    152   void GetTilesWithAssignedBins(PrioritizedTileSet* tiles);
    153 
    154  private:
    155   void OnImageDecodeTaskCompleted(
    156       int layer_id,
    157       skia::LazyPixelRef* pixel_ref,
    158       bool was_canceled);
    159   void OnRasterTaskCompleted(Tile::Id tile,
    160                              scoped_ptr<ScopedResource> resource,
    161                              RasterMode raster_mode,
    162                              const PicturePileImpl::Analysis& analysis,
    163                              bool was_canceled);
    164 
    165   inline size_t BytesConsumedIfAllocated(const Tile* tile) const {
    166     return Resource::MemorySizeBytes(tile->size(),
    167                                      raster_worker_pool_->GetResourceFormat());
    168   }
    169 
    170   RasterMode DetermineRasterMode(const Tile* tile) const;
    171   void FreeResourceForTile(Tile* tile, RasterMode mode);
    172   void FreeResourcesForTile(Tile* tile);
    173   void FreeUnusedResourcesForTile(Tile* tile);
    174   RasterWorkerPool::Task CreateImageDecodeTask(
    175       Tile* tile, skia::LazyPixelRef* pixel_ref);
    176   RasterWorkerPool::RasterTask CreateRasterTask(Tile* tile);
    177   scoped_ptr<base::Value> GetMemoryRequirementsAsValue() const;
    178   void UpdatePrioritizedTileSetIfNeeded();
    179 
    180   TileManagerClient* client_;
    181   scoped_ptr<ResourcePool> resource_pool_;
    182   scoped_ptr<RasterWorkerPool> raster_worker_pool_;
    183   GlobalStateThatImpactsTilePriority global_state_;
    184 
    185   typedef base::hash_map<Tile::Id, Tile*> TileMap;
    186   TileMap tiles_;
    187 
    188   PrioritizedTileSet prioritized_tiles_;
    189   bool prioritized_tiles_dirty_;
    190 
    191   bool all_tiles_that_need_to_be_rasterized_have_memory_;
    192   bool all_tiles_required_for_activation_have_memory_;
    193 
    194   size_t memory_required_bytes_;
    195   size_t memory_nice_to_have_bytes_;
    196 
    197   size_t bytes_releasable_;
    198   size_t resources_releasable_;
    199   size_t max_raster_usage_bytes_;
    200 
    201   bool ever_exceeded_memory_budget_;
    202   MemoryHistory::Entry memory_stats_from_last_assign_;
    203 
    204   RenderingStatsInstrumentation* rendering_stats_instrumentation_;
    205 
    206   bool did_initialize_visible_tile_;
    207   bool did_check_for_completed_tasks_since_last_schedule_tasks_;
    208 
    209   typedef base::hash_map<uint32_t, RasterWorkerPool::Task> PixelRefTaskMap;
    210   typedef base::hash_map<int, PixelRefTaskMap> LayerPixelRefTaskMap;
    211   LayerPixelRefTaskMap image_decode_tasks_;
    212 
    213   typedef base::hash_map<int, int> LayerCountMap;
    214   LayerCountMap used_layer_counts_;
    215 
    216   RasterTaskCompletionStats update_visible_tiles_stats_;
    217 
    218   std::vector<Tile*> released_tiles_;
    219 
    220   DISALLOW_COPY_AND_ASSIGN(TileManager);
    221 };
    222 
    223 }  // namespace cc
    224 
    225 #endif  // CC_RESOURCES_TILE_MANAGER_H_
    226