Home | History | Annotate | Download | only in resources
      1 // Copyright 2013 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_MANAGED_TILE_STATE_H_
      6 #define CC_RESOURCES_MANAGED_TILE_STATE_H_
      7 
      8 #include "base/memory/scoped_ptr.h"
      9 #include "cc/resources/platform_color.h"
     10 #include "cc/resources/raster_worker_pool.h"
     11 #include "cc/resources/resource_pool.h"
     12 #include "cc/resources/resource_provider.h"
     13 
     14 namespace cc {
     15 
     16 class TileManager;
     17 
     18 // Tile manager classifying tiles into a few basic bins:
     19 enum ManagedTileBin {
     20   NOW_AND_READY_TO_DRAW_BIN = 0,  // Ready to draw and within viewport.
     21   NOW_BIN = 1,                    // Needed ASAP.
     22   SOON_BIN = 2,                   // Impl-side version of prepainting.
     23   EVENTUALLY_AND_ACTIVE_BIN = 3,  // Nice to have, and has a task or resource.
     24   EVENTUALLY_BIN = 4,             // Nice to have, if we've got memory and time.
     25   NEVER_AND_ACTIVE_BIN = 5,       // Dont bother, but has a task or resource.
     26   NEVER_BIN = 6,                  // Dont bother.
     27   NUM_BINS = 7
     28   // NOTE: Be sure to update ManagedTileBinAsValue and kBinPolicyMap when adding
     29   // or reordering fields.
     30 };
     31 scoped_ptr<base::Value> ManagedTileBinAsValue(
     32     ManagedTileBin bin);
     33 
     34 enum ManagedTileBinPriority {
     35   HIGH_PRIORITY_BIN = 0,
     36   LOW_PRIORITY_BIN = 1,
     37   NUM_BIN_PRIORITIES = 2
     38 };
     39 scoped_ptr<base::Value> ManagedTileBinPriorityAsValue(
     40     ManagedTileBinPriority bin);
     41 
     42 // This is state that is specific to a tile that is
     43 // managed by the TileManager.
     44 class CC_EXPORT ManagedTileState {
     45  public:
     46   class CC_EXPORT TileVersion {
     47     public:
     48       enum Mode {
     49         RESOURCE_MODE,
     50         SOLID_COLOR_MODE,
     51         PICTURE_PILE_MODE,
     52         NUM_MODES
     53       };
     54 
     55       TileVersion();
     56       ~TileVersion();
     57 
     58       Mode mode() const {
     59         return mode_;
     60       }
     61 
     62       bool IsReadyToDraw() const;
     63 
     64       ResourceProvider::ResourceId get_resource_id() const {
     65         DCHECK(mode_ == RESOURCE_MODE);
     66         DCHECK(resource_);
     67 
     68         return resource_->id();
     69       }
     70 
     71       SkColor get_solid_color() const {
     72         DCHECK(mode_ == SOLID_COLOR_MODE);
     73 
     74         return solid_color_;
     75       }
     76 
     77       bool contents_swizzled() const {
     78         DCHECK(resource_);
     79         return !PlatformColor::SameComponentOrder(resource_->format());
     80       }
     81 
     82       bool requires_resource() const {
     83         return mode_ == RESOURCE_MODE ||
     84                mode_ == PICTURE_PILE_MODE;
     85       }
     86 
     87       size_t GPUMemoryUsageInBytes() const;
     88 
     89       void SetResourceForTesting(scoped_ptr<ResourcePool::Resource> resource) {
     90         resource_ = resource.Pass();
     91       }
     92       const ResourcePool::Resource* GetResourceForTesting() const {
     93         return resource_.get();
     94       }
     95       void SetSolidColorForTesting(SkColor color) {
     96         set_solid_color(color);
     97       }
     98       void SetHasTextForTesting(bool has_text) {
     99         has_text_ = has_text;
    100       }
    101 
    102     private:
    103       friend class TileManager;
    104       friend class PrioritizedTileSet;
    105       friend class Tile;
    106       friend class ManagedTileState;
    107 
    108       void set_use_resource() {
    109         mode_ = RESOURCE_MODE;
    110       }
    111 
    112       void set_solid_color(const SkColor& color) {
    113         mode_ = SOLID_COLOR_MODE;
    114         solid_color_ = color;
    115       }
    116 
    117       void set_has_text(bool has_text) {
    118         has_text_ = has_text;
    119       }
    120 
    121       void set_rasterize_on_demand() {
    122         mode_ = PICTURE_PILE_MODE;
    123       }
    124 
    125       Mode mode_;
    126       SkColor solid_color_;
    127       bool has_text_;
    128       scoped_ptr<ResourcePool::Resource> resource_;
    129       RasterWorkerPool::RasterTask raster_task_;
    130   };
    131 
    132   ManagedTileState();
    133   ~ManagedTileState();
    134 
    135   scoped_ptr<base::Value> AsValue() const;
    136 
    137   // Persisted state: valid all the time.
    138   TileVersion tile_versions[NUM_RASTER_MODES];
    139   RasterMode raster_mode;
    140 
    141   // Ephemeral state, valid only during TileManager::ManageTiles.
    142   bool is_in_never_bin_on_both_trees() const {
    143     return (bin[HIGH_PRIORITY_BIN] == NEVER_BIN ||
    144             bin[HIGH_PRIORITY_BIN] == NEVER_AND_ACTIVE_BIN) &&
    145            (bin[LOW_PRIORITY_BIN] == NEVER_BIN ||
    146             bin[LOW_PRIORITY_BIN] == NEVER_AND_ACTIVE_BIN);
    147   }
    148 
    149   ManagedTileBin bin[NUM_BIN_PRIORITIES];
    150   ManagedTileBin tree_bin[NUM_TREES];
    151 
    152   // The bin that the tile would have if the GPU memory manager had
    153   // a maximally permissive policy, send to the GPU memory manager
    154   // to determine policy.
    155   ManagedTileBin gpu_memmgr_stats_bin;
    156   TileResolution resolution;
    157   bool required_for_activation;
    158   float time_to_needed_in_seconds;
    159   float distance_to_visible_in_pixels;
    160   bool visible_and_ready_to_draw;
    161 
    162   // Priority for this state from the last time we assigned memory.
    163   unsigned scheduled_priority;
    164 };
    165 
    166 }  // namespace cc
    167 
    168 #endif  // CC_RESOURCES_MANAGED_TILE_STATE_H_
    169