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_LAYERS_PICTURE_LAYER_IMPL_H_
6#define CC_LAYERS_PICTURE_LAYER_IMPL_H_
7
8#include <set>
9#include <string>
10#include <vector>
11
12#include "cc/base/cc_export.h"
13#include "cc/base/scoped_ptr_vector.h"
14#include "cc/layers/layer_impl.h"
15#include "cc/resources/picture_layer_tiling.h"
16#include "cc/resources/picture_layer_tiling_set.h"
17#include "cc/resources/picture_pile_impl.h"
18#include "skia/ext/refptr.h"
19#include "third_party/skia/include/core/SkPicture.h"
20
21namespace cc {
22
23struct AppendQuadsData;
24class MicroBenchmarkImpl;
25class Tile;
26
27class CC_EXPORT PictureLayerImpl
28    : public LayerImpl,
29      NON_EXPORTED_BASE(public PictureLayerTilingClient) {
30 public:
31  struct CC_EXPORT Pair {
32    Pair();
33    Pair(PictureLayerImpl* active_layer, PictureLayerImpl* pending_layer);
34    ~Pair();
35
36    PictureLayerImpl* active;
37    PictureLayerImpl* pending;
38  };
39
40  class CC_EXPORT LayerRasterTileIterator {
41   public:
42    LayerRasterTileIterator();
43    LayerRasterTileIterator(PictureLayerImpl* layer, bool prioritize_low_res);
44    ~LayerRasterTileIterator();
45
46    Tile* operator*();
47    const Tile* operator*() const;
48    LayerRasterTileIterator& operator++();
49    operator bool() const;
50
51   private:
52    enum IteratorType { LOW_RES, HIGH_RES, NUM_ITERATORS };
53
54    void AdvanceToNextStage();
55
56    PictureLayerImpl* layer_;
57
58    struct IterationStage {
59      IteratorType iterator_type;
60      TilePriority::PriorityBin tile_type;
61    };
62
63    size_t current_stage_;
64
65    // One low res stage, and three high res stages.
66    IterationStage stages_[4];
67    PictureLayerTiling::TilingRasterTileIterator iterators_[NUM_ITERATORS];
68  };
69
70  class CC_EXPORT LayerEvictionTileIterator {
71   public:
72    LayerEvictionTileIterator();
73    LayerEvictionTileIterator(PictureLayerImpl* layer,
74                              TreePriority tree_priority);
75    ~LayerEvictionTileIterator();
76
77    Tile* operator*();
78    const Tile* operator*() const;
79    LayerEvictionTileIterator& operator++();
80    operator bool() const;
81
82   private:
83    bool AdvanceToNextCategory();
84    bool AdvanceToNextTilingRangeType();
85    bool AdvanceToNextTiling();
86
87    PictureLayerTilingSet::TilingRange CurrentTilingRange() const;
88    size_t CurrentTilingIndex() const;
89
90    PictureLayerImpl* layer_;
91    TreePriority tree_priority_;
92
93    PictureLayerTiling::EvictionCategory current_category_;
94    PictureLayerTilingSet::TilingRangeType current_tiling_range_type_;
95    size_t current_tiling_;
96    PictureLayerTiling::TilingEvictionTileIterator current_iterator_;
97  };
98
99  static scoped_ptr<PictureLayerImpl> Create(LayerTreeImpl* tree_impl, int id) {
100    return make_scoped_ptr(new PictureLayerImpl(tree_impl, id));
101  }
102  virtual ~PictureLayerImpl();
103
104  // LayerImpl overrides.
105  virtual const char* LayerTypeAsString() const OVERRIDE;
106  virtual scoped_ptr<LayerImpl> CreateLayerImpl(LayerTreeImpl* tree_impl)
107      OVERRIDE;
108  virtual void PushPropertiesTo(LayerImpl* layer) OVERRIDE;
109  virtual void AppendQuads(RenderPass* render_pass,
110                           const OcclusionTracker<LayerImpl>& occlusion_tracker,
111                           AppendQuadsData* append_quads_data) OVERRIDE;
112  virtual void UpdateTiles(const Occlusion& occlusion_in_content_space,
113                           bool resourceless_software_draw) OVERRIDE;
114  virtual void NotifyTileStateChanged(const Tile* tile) OVERRIDE;
115  virtual void DidBecomeActive() OVERRIDE;
116  virtual void DidBeginTracing() OVERRIDE;
117  virtual void ReleaseResources() OVERRIDE;
118  virtual skia::RefPtr<SkPicture> GetPicture() OVERRIDE;
119
120  // PictureLayerTilingClient overrides.
121  virtual scoped_refptr<Tile> CreateTile(
122    PictureLayerTiling* tiling,
123    const gfx::Rect& content_rect) OVERRIDE;
124  virtual PicturePileImpl* GetPile() OVERRIDE;
125  virtual gfx::Size CalculateTileSize(
126      const gfx::Size& content_bounds) const OVERRIDE;
127  virtual const Region* GetInvalidation() OVERRIDE;
128  virtual const PictureLayerTiling* GetTwinTiling(
129      const PictureLayerTiling* tiling) const OVERRIDE;
130  virtual PictureLayerTiling* GetRecycledTwinTiling(
131      const PictureLayerTiling* tiling) OVERRIDE;
132  virtual size_t GetMaxTilesForInterestArea() const OVERRIDE;
133  virtual float GetSkewportTargetTimeInSeconds() const OVERRIDE;
134  virtual int GetSkewportExtrapolationLimitInContentPixels() const OVERRIDE;
135  virtual WhichTree GetTree() const OVERRIDE;
136
137  // PushPropertiesTo active tree => pending tree.
138  void SyncTiling(const PictureLayerTiling* tiling);
139
140  // Mask-related functions.
141  virtual ResourceProvider::ResourceId ContentsResourceId() const OVERRIDE;
142
143  virtual size_t GPUMemoryUsageInBytes() const OVERRIDE;
144
145  virtual void RunMicroBenchmark(MicroBenchmarkImpl* benchmark) OVERRIDE;
146
147  // Functions used by tile manager.
148  PictureLayerImpl* GetTwinLayer() { return twin_layer_; }
149  bool IsOnActiveOrPendingTree() const;
150  // Virtual for testing.
151  virtual bool HasValidTilePriorities() const;
152  bool AllTilesRequiredForActivationAreReadyToDraw() const;
153
154 protected:
155  friend class LayerRasterTileIterator;
156
157  PictureLayerImpl(LayerTreeImpl* tree_impl, int id);
158  PictureLayerTiling* AddTiling(float contents_scale);
159  void RemoveTiling(float contents_scale);
160  void RemoveAllTilings();
161  void SyncFromActiveLayer(const PictureLayerImpl* other);
162  void AddTilingsForRasterScale();
163  void UpdateTilePriorities(const Occlusion& occlusion_in_content_space);
164  virtual bool ShouldAdjustRasterScale() const;
165  virtual void RecalculateRasterScales();
166  void CleanUpTilingsOnActiveLayer(
167      std::vector<PictureLayerTiling*> used_tilings);
168  float MinimumContentsScale() const;
169  float SnappedContentsScale(float new_contents_scale);
170  void ResetRasterScale();
171  void MarkVisibleResourcesAsRequired() const;
172  bool MarkVisibleTilesAsRequired(
173      PictureLayerTiling* tiling,
174      const PictureLayerTiling* optional_twin_tiling,
175      const gfx::Rect& rect,
176      const Region& missing_region) const;
177  gfx::Rect GetViewportForTilePriorityInContentSpace() const;
178  PictureLayerImpl* GetRecycledTwinLayer();
179  void UpdatePile(scoped_refptr<PicturePileImpl> pile);
180
181  void DoPostCommitInitializationIfNeeded() {
182    if (needs_post_commit_initialization_)
183      DoPostCommitInitialization();
184  }
185  void DoPostCommitInitialization();
186
187  bool CanHaveTilings() const;
188  bool CanHaveTilingWithScale(float contents_scale) const;
189  void SanityCheckTilingState() const;
190
191  bool ShouldAdjustRasterScaleDuringScaleAnimations() const;
192
193  virtual void GetDebugBorderProperties(
194      SkColor* color, float* width) const OVERRIDE;
195  virtual void GetAllTilesForTracing(
196      std::set<const Tile*>* tiles) const OVERRIDE;
197  virtual void AsValueInto(base::debug::TracedValue* dict) const OVERRIDE;
198
199  virtual void UpdateIdealScales();
200  float MaximumTilingContentsScale() const;
201
202  PictureLayerImpl* twin_layer_;
203
204  scoped_ptr<PictureLayerTilingSet> tilings_;
205  scoped_refptr<PicturePileImpl> pile_;
206  Region invalidation_;
207
208  float ideal_page_scale_;
209  float ideal_device_scale_;
210  float ideal_source_scale_;
211  float ideal_contents_scale_;
212
213  float raster_page_scale_;
214  float raster_device_scale_;
215  float raster_source_scale_;
216  float raster_contents_scale_;
217  float low_res_raster_contents_scale_;
218
219  bool raster_source_scale_is_fixed_;
220  bool was_screen_space_transform_animating_;
221  bool needs_post_commit_initialization_;
222  // A sanity state check to make sure UpdateTilePriorities only gets called
223  // after a CalculateContentsScale/ManageTilings.
224  bool should_update_tile_priorities_;
225
226  // Save a copy of the visible rect and viewport size of the last frame that
227  // has a valid viewport for prioritizing tiles.
228  gfx::Rect visible_rect_for_tile_priority_;
229  gfx::Rect viewport_rect_for_tile_priority_;
230  gfx::Transform screen_space_transform_for_tile_priority_;
231
232  friend class PictureLayer;
233  DISALLOW_COPY_AND_ASSIGN(PictureLayerImpl);
234};
235
236}  // namespace cc
237
238#endif  // CC_LAYERS_PICTURE_LAYER_IMPL_H_
239