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_PICTURE_PILE_IMPL_H_
6#define CC_RESOURCES_PICTURE_PILE_IMPL_H_
7
8#include <list>
9#include <map>
10#include <set>
11#include <vector>
12
13#include "base/time/time.h"
14#include "cc/base/cc_export.h"
15#include "cc/debug/rendering_stats_instrumentation.h"
16#include "cc/resources/picture_pile_base.h"
17#include "skia/ext/analysis_canvas.h"
18#include "skia/ext/refptr.h"
19#include "third_party/skia/include/core/SkPicture.h"
20
21namespace cc {
22
23class CC_EXPORT PicturePileImpl : public PicturePileBase {
24 public:
25  static scoped_refptr<PicturePileImpl> Create();
26  static scoped_refptr<PicturePileImpl> CreateFromOther(
27      const PicturePileBase* other);
28
29  // Get paint-safe version of this picture for a specific thread.
30  PicturePileImpl* GetCloneForDrawingOnThread(unsigned thread_index) const;
31
32  // Raster a subrect of this PicturePileImpl into the given canvas.
33  // It's only safe to call paint on a cloned version.  It is assumed
34  // that contents_scale has already been applied to this canvas.
35  // Writes the total number of pixels rasterized and the time spent
36  // rasterizing to the stats if the respective pointer is not
37  // NULL. When slow-down-raster-scale-factor is set to a value
38  // greater than 1, the reported rasterize time is the minimum
39  // measured value over all runs.
40  void RasterDirect(
41      SkCanvas* canvas,
42      gfx::Rect canvas_rect,
43      float contents_scale,
44      RenderingStatsInstrumentation* rendering_stats_instrumentation);
45
46  // Similar to the above RasterDirect method, but this is a convenience method
47  // for when it is known that the raster is going to an intermediate bitmap
48  // that itself will then be blended and thus that a canvas clear is required.
49  void RasterToBitmap(
50      SkCanvas* canvas,
51      gfx::Rect canvas_rect,
52      float contents_scale,
53      RenderingStatsInstrumentation* stats_instrumentation);
54
55  // Called when analyzing a tile. We can use AnalysisCanvas as
56  // SkDrawPictureCallback, which allows us to early out from analysis.
57  void RasterForAnalysis(
58      skia::AnalysisCanvas* canvas,
59      gfx::Rect canvas_rect,
60      float contents_scale,
61      RenderingStatsInstrumentation* stats_instrumentation);
62
63  skia::RefPtr<SkPicture> GetFlattenedPicture();
64
65  struct CC_EXPORT Analysis {
66    Analysis();
67    ~Analysis();
68
69    bool is_solid_color;
70    bool has_text;
71    SkColor solid_color;
72  };
73
74  void AnalyzeInRect(gfx::Rect content_rect,
75                     float contents_scale,
76                     Analysis* analysis);
77
78  void AnalyzeInRect(gfx::Rect content_rect,
79                     float contents_scale,
80                     Analysis* analysis,
81                     RenderingStatsInstrumentation* stats_instrumentation);
82
83  class CC_EXPORT PixelRefIterator {
84   public:
85    PixelRefIterator(gfx::Rect content_rect,
86                     float contents_scale,
87                     const PicturePileImpl* picture_pile);
88    ~PixelRefIterator();
89
90    skia::LazyPixelRef* operator->() const { return *pixel_ref_iterator_; }
91    skia::LazyPixelRef* operator*() const { return *pixel_ref_iterator_; }
92    PixelRefIterator& operator++();
93    operator bool() const { return pixel_ref_iterator_; }
94
95   private:
96    void AdvanceToTilePictureWithPixelRefs();
97
98    const PicturePileImpl* picture_pile_;
99    gfx::Rect layer_rect_;
100    TilingData::Iterator tile_iterator_;
101    Picture::PixelRefIterator pixel_ref_iterator_;
102    std::set<const void*> processed_pictures_;
103  };
104
105  void DidBeginTracing();
106
107 protected:
108  friend class PicturePile;
109  friend class PixelRefIterator;
110
111  PicturePileImpl();
112  explicit PicturePileImpl(const PicturePileBase* other);
113  virtual ~PicturePileImpl();
114
115 private:
116  class ClonesForDrawing {
117   public:
118    ClonesForDrawing(const PicturePileImpl* pile, int num_threads);
119    ~ClonesForDrawing();
120
121    typedef std::vector<scoped_refptr<PicturePileImpl> > PicturePileVector;
122    PicturePileVector clones_;
123  };
124
125  static scoped_refptr<PicturePileImpl> CreateCloneForDrawing(
126      const PicturePileImpl* other, unsigned thread_index);
127
128  PicturePileImpl(const PicturePileImpl* other, unsigned thread_index);
129
130 private:
131  typedef std::map<Picture*, Region> PictureRegionMap;
132  void CoalesceRasters(gfx::Rect canvas_rect,
133                       gfx::Rect content_rect,
134                       float contents_scale,
135                       PictureRegionMap* result);
136
137  void RasterCommon(
138      SkCanvas* canvas,
139      SkDrawPictureCallback* callback,
140      gfx::Rect canvas_rect,
141      float contents_scale,
142      RenderingStatsInstrumentation* rendering_stats_instrumentation,
143      bool is_analysis);
144
145  // Once instantiated, |clones_for_drawing_| can't be modified.  This
146  // guarantees thread-safe access during the life time of a PicturePileImpl
147  // instance.  This member variable must be last so that other member
148  // variables have already been initialized and can be clonable.
149  const ClonesForDrawing clones_for_drawing_;
150
151  DISALLOW_COPY_AND_ASSIGN(PicturePileImpl);
152};
153
154}  // namespace cc
155
156#endif  // CC_RESOURCES_PICTURE_PILE_IMPL_H_
157