6588ac56e6b276c0eff04cfc74cf4653ad8518f7
[platform/framework/web/crosswalk.git] / src / cc / resources / tile.h
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_H_
6 #define CC_RESOURCES_TILE_H_
7
8 #include "base/memory/ref_counted.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/memory/scoped_vector.h"
11 #include "cc/base/ref_counted_managed.h"
12 #include "cc/resources/managed_tile_state.h"
13 #include "cc/resources/picture_pile_impl.h"
14 #include "cc/resources/raster_mode.h"
15 #include "cc/resources/tile_priority.h"
16 #include "ui/gfx/rect.h"
17 #include "ui/gfx/size.h"
18
19 namespace cc {
20
21 class CC_EXPORT Tile : public RefCountedManaged<Tile> {
22  public:
23   enum TileRasterFlags { USE_PICTURE_ANALYSIS = 1 << 0 };
24
25   typedef uint64 Id;
26
27   Id id() const {
28     return id_;
29   }
30
31   PicturePileImpl* picture_pile() {
32     return picture_pile_.get();
33   }
34
35   const PicturePileImpl* picture_pile() const {
36     return picture_pile_.get();
37   }
38
39   const TilePriority& priority(WhichTree tree) const {
40     return priority_[tree];
41   }
42
43   TilePriority priority_for_tree_priority(TreePriority tree_priority) const {
44     switch (tree_priority) {
45       case SMOOTHNESS_TAKES_PRIORITY:
46         return priority_[ACTIVE_TREE];
47       case NEW_CONTENT_TAKES_PRIORITY:
48         return priority_[PENDING_TREE];
49       case SAME_PRIORITY_FOR_BOTH_TREES:
50         return combined_priority();
51       default:
52         NOTREACHED();
53         return TilePriority();
54     }
55   }
56
57   TilePriority combined_priority() const {
58     return TilePriority(priority_[ACTIVE_TREE],
59                         priority_[PENDING_TREE]);
60   }
61
62   void SetPriority(WhichTree tree, const TilePriority& priority);
63
64   void set_is_occluded(WhichTree tree, bool is_occluded) {
65     is_occluded_[tree] = is_occluded;
66   }
67
68   bool is_occluded(WhichTree tree) const { return is_occluded_[tree]; }
69
70   bool is_occluded_for_tree_priority(TreePriority tree_priority) const {
71     switch (tree_priority) {
72       case SMOOTHNESS_TAKES_PRIORITY:
73         return is_occluded_[ACTIVE_TREE];
74       case NEW_CONTENT_TAKES_PRIORITY:
75         return is_occluded_[PENDING_TREE];
76       case SAME_PRIORITY_FOR_BOTH_TREES:
77         return is_occluded_[ACTIVE_TREE] && is_occluded_[PENDING_TREE];
78       default:
79         NOTREACHED();
80         return false;
81     }
82   }
83
84   void MarkRequiredForActivation();
85
86   bool required_for_activation() const {
87     return priority_[PENDING_TREE].required_for_activation;
88   }
89
90   bool use_picture_analysis() const {
91     return !!(flags_ & USE_PICTURE_ANALYSIS);
92   }
93
94   bool NeedsRasterForMode(RasterMode mode) const {
95     return !managed_state_.tile_versions[mode].IsReadyToDraw();
96   }
97
98   bool HasResources() const {
99     for (int mode = 0; mode < NUM_RASTER_MODES; ++mode) {
100       if (managed_state_.tile_versions[mode].has_resource())
101         return true;
102     }
103     return false;
104   }
105
106   void AsValueInto(base::debug::TracedValue* dict) const;
107
108   inline bool IsReadyToDraw() const {
109     for (int mode = 0; mode < NUM_RASTER_MODES; ++mode) {
110       if (managed_state_.tile_versions[mode].IsReadyToDraw())
111         return true;
112     }
113     return false;
114   }
115
116   const ManagedTileState::TileVersion& GetTileVersionForDrawing() const {
117     for (int mode = 0; mode < NUM_RASTER_MODES; ++mode) {
118       if (managed_state_.tile_versions[mode].IsReadyToDraw())
119         return managed_state_.tile_versions[mode];
120     }
121     return managed_state_.tile_versions[HIGH_QUALITY_RASTER_MODE];
122   }
123
124   gfx::Rect opaque_rect() const { return opaque_rect_; }
125   float contents_scale() const { return contents_scale_; }
126   gfx::Rect content_rect() const { return content_rect_; }
127
128   int layer_id() const { return layer_id_; }
129
130   int source_frame_number() const { return source_frame_number_; }
131
132   void set_picture_pile(scoped_refptr<PicturePileImpl> pile) {
133     DCHECK(pile->CanRaster(contents_scale_, content_rect_))
134         << gfx::ScaleToEnclosingRect(content_rect_, 1.f / contents_scale_)
135                .ToString();
136     picture_pile_ = pile;
137   }
138
139   size_t GPUMemoryUsageInBytes() const;
140
141   gfx::Size size() const { return size_; }
142
143   RasterMode DetermineRasterModeForTree(WhichTree tree) const;
144   RasterMode DetermineOverallRasterMode() const;
145
146   // Functionality used in tests.
147   RasterMode GetRasterModeForTesting() const {
148     return managed_state().raster_mode;
149   }
150   ManagedTileState::TileVersion& GetTileVersionForTesting(RasterMode mode) {
151     return managed_state_.tile_versions[mode];
152   }
153
154  private:
155   friend class TileManager;
156   friend class PrioritizedTileSet;
157   friend class FakeTileManager;
158   friend class BinComparator;
159   friend class FakePictureLayerImpl;
160
161   // Methods called by by tile manager.
162   Tile(TileManager* tile_manager,
163        PicturePileImpl* picture_pile,
164        const gfx::Size& tile_size,
165        const gfx::Rect& content_rect,
166        const gfx::Rect& opaque_rect,
167        float contents_scale,
168        int layer_id,
169        int source_frame_number,
170        int flags);
171   ~Tile();
172
173   ManagedTileState& managed_state() { return managed_state_; }
174   const ManagedTileState& managed_state() const { return managed_state_; }
175   RasterMode DetermineRasterModeForResolution(TileResolution resolution) const;
176
177   bool HasRasterTask() const;
178
179   TileManager* tile_manager_;
180   scoped_refptr<PicturePileImpl> picture_pile_;
181   gfx::Size size_;
182   gfx::Rect content_rect_;
183   float contents_scale_;
184   gfx::Rect opaque_rect_;
185   bool is_occluded_[NUM_TREES];
186
187   TilePriority priority_[NUM_TREES];
188   ManagedTileState managed_state_;
189   int layer_id_;
190   int source_frame_number_;
191   int flags_;
192
193   Id id_;
194   static Id s_next_id_;
195
196   DISALLOW_COPY_AND_ASSIGN(Tile);
197 };
198
199 }  // namespace cc
200
201 #endif  // CC_RESOURCES_TILE_H_