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_mode.h"
11#include "cc/resources/rasterizer.h"
12#include "cc/resources/resource_pool.h"
13#include "cc/resources/resource_provider.h"
14#include "cc/resources/scoped_resource.h"
15#include "cc/resources/tile_priority.h"
16
17namespace cc {
18
19class TileManager;
20
21// Tile manager classifying tiles into a few basic bins:
22enum ManagedTileBin {
23  NOW_AND_READY_TO_DRAW_BIN = 0,  // Ready to draw and within viewport.
24  NOW_BIN = 1,                    // Needed ASAP.
25  SOON_BIN = 2,                   // Impl-side version of prepainting.
26  EVENTUALLY_AND_ACTIVE_BIN = 3,  // Nice to have, and has a task or resource.
27  EVENTUALLY_BIN = 4,             // Nice to have, if we've got memory and time.
28  AT_LAST_AND_ACTIVE_BIN = 5,     // Only do this after all other bins.
29  AT_LAST_BIN = 6,                // Only do this after all other bins.
30  NEVER_BIN = 7,                  // Dont bother.
31  NUM_BINS = 8
32  // NOTE: Be sure to update ManagedTileBinAsValue and kBinPolicyMap when adding
33  // or reordering fields.
34};
35scoped_ptr<base::Value> ManagedTileBinAsValue(ManagedTileBin bin);
36
37// This is state that is specific to a tile that is
38// managed by the TileManager.
39class CC_EXPORT ManagedTileState {
40 public:
41  class CC_EXPORT TileVersion {
42   public:
43    enum Mode { RESOURCE_MODE, SOLID_COLOR_MODE, PICTURE_PILE_MODE };
44
45    TileVersion();
46    ~TileVersion();
47
48    Mode mode() const { return mode_; }
49
50    bool IsReadyToDraw() const;
51
52    ResourceProvider::ResourceId get_resource_id() const {
53      DCHECK(mode_ == RESOURCE_MODE);
54      DCHECK(resource_);
55
56      return resource_->id();
57    }
58
59    SkColor get_solid_color() const {
60      DCHECK(mode_ == SOLID_COLOR_MODE);
61
62      return solid_color_;
63    }
64
65    bool contents_swizzled() const {
66      DCHECK(resource_);
67      return !PlatformColor::SameComponentOrder(resource_->format());
68    }
69
70    bool requires_resource() const {
71      return mode_ == RESOURCE_MODE || mode_ == PICTURE_PILE_MODE;
72    }
73
74    inline bool has_resource() const { return !!resource_; }
75
76    size_t GPUMemoryUsageInBytes() const;
77
78    void SetSolidColorForTesting(SkColor color) { set_solid_color(color); }
79    void SetResourceForTesting(scoped_ptr<ScopedResource> resource) {
80      resource_ = resource.Pass();
81    }
82
83   private:
84    friend class TileManager;
85    friend class PrioritizedTileSet;
86    friend class Tile;
87    friend class ManagedTileState;
88
89    void set_use_resource() { mode_ = RESOURCE_MODE; }
90
91    void set_solid_color(const SkColor& color) {
92      mode_ = SOLID_COLOR_MODE;
93      solid_color_ = color;
94    }
95
96    void set_rasterize_on_demand() { mode_ = PICTURE_PILE_MODE; }
97
98    Mode mode_;
99    SkColor solid_color_;
100    scoped_ptr<ScopedResource> resource_;
101    scoped_refptr<RasterTask> raster_task_;
102  };
103
104  ManagedTileState();
105  ~ManagedTileState();
106
107  void AsValueInto(base::debug::TracedValue* dict) const;
108
109  // Persisted state: valid all the time.
110  TileVersion tile_versions[NUM_RASTER_MODES];
111  RasterMode raster_mode;
112
113  ManagedTileBin bin;
114
115  TileResolution resolution;
116  bool required_for_activation;
117  TilePriority::PriorityBin priority_bin;
118  float distance_to_visible;
119  bool visible_and_ready_to_draw;
120
121  // Priority for this state from the last time we assigned memory.
122  unsigned scheduled_priority;
123};
124
125}  // namespace cc
126
127#endif  // CC_RESOURCES_MANAGED_TILE_STATE_H_
128