Revert 264226 "Reduce dependency of TiclInvalidationService on P..."
[chromium-blink-merge.git] / cc / resources / picture.h
blobd32b8435afcc882cc70e29ab59d0fcf1def9e710
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.
5 #ifndef CC_RESOURCES_PICTURE_H_
6 #define CC_RESOURCES_PICTURE_H_
8 #include <string>
9 #include <utility>
10 #include <vector>
12 #include "base/basictypes.h"
13 #include "base/containers/hash_tables.h"
14 #include "base/debug/trace_event.h"
15 #include "base/lazy_instance.h"
16 #include "base/logging.h"
17 #include "base/memory/ref_counted.h"
18 #include "base/memory/scoped_ptr.h"
19 #include "base/threading/thread_checker.h"
20 #include "cc/base/cc_export.h"
21 #include "cc/base/region.h"
22 #include "skia/ext/refptr.h"
23 #include "third_party/skia/include/core/SkTileGridPicture.h"
24 #include "ui/gfx/rect.h"
26 class SkPixelRef;
28 namespace base {
29 class Value;
32 namespace skia {
33 class AnalysisCanvas;
36 namespace cc {
38 class ContentLayerClient;
40 class CC_EXPORT Picture
41 : public base::RefCountedThreadSafe<Picture> {
42 public:
43 typedef std::pair<int, int> PixelRefMapKey;
44 typedef std::vector<SkPixelRef*> PixelRefs;
45 typedef base::hash_map<PixelRefMapKey, PixelRefs> PixelRefMap;
47 enum RecordingMode {
48 RECORD_NORMALLY,
49 RECORD_WITH_SK_NULL_CANVAS,
50 RECORD_WITH_PAINTING_DISABLED,
51 RECORDING_MODE_COUNT, // Must be the last entry.
54 static scoped_refptr<Picture> Create(
55 const gfx::Rect& layer_rect,
56 ContentLayerClient* client,
57 const SkTileGridPicture::TileGridInfo& tile_grid_info,
58 bool gather_pixels_refs,
59 int num_raster_threads,
60 RecordingMode recording_mode);
61 static scoped_refptr<Picture> CreateFromValue(const base::Value* value);
62 static scoped_refptr<Picture> CreateFromSkpValue(const base::Value* value);
64 gfx::Rect LayerRect() const { return layer_rect_; }
65 gfx::Rect OpaqueRect() const { return opaque_rect_; }
67 // Get thread-safe clone for rasterizing with on a specific thread.
68 Picture* GetCloneForDrawingOnThread(unsigned thread_index);
70 // Has Record() been called yet?
71 bool HasRecording() const { return picture_.get() != NULL; }
73 bool IsSuitableForGpuRasterization() const;
75 // Apply this scale and raster the negated region into the canvas. See comment
76 // in PicturePileImpl::RasterCommon for explanation on negated content region.
77 int Raster(SkCanvas* canvas,
78 SkDrawPictureCallback* callback,
79 const Region& negated_content_region,
80 float contents_scale);
82 // Draw the picture directly into the given canvas, without applying any
83 // clip/scale/layer transformations.
84 void Replay(SkCanvas* canvas);
86 scoped_ptr<base::Value> AsValue() const;
88 // This iterator imprecisely returns the set of pixel refs that are needed to
89 // raster this layer rect from this picture. Internally, pixel refs are
90 // clumped into tile grid buckets, so there may be false positives.
91 class CC_EXPORT PixelRefIterator {
92 public:
93 PixelRefIterator();
94 PixelRefIterator(const gfx::Rect& layer_rect, const Picture* picture);
95 ~PixelRefIterator();
97 SkPixelRef* operator->() const {
98 DCHECK_LT(current_index_, current_pixel_refs_->size());
99 return (*current_pixel_refs_)[current_index_];
102 SkPixelRef* operator*() const {
103 DCHECK_LT(current_index_, current_pixel_refs_->size());
104 return (*current_pixel_refs_)[current_index_];
107 PixelRefIterator& operator++();
108 operator bool() const {
109 return current_index_ < current_pixel_refs_->size();
112 private:
113 static base::LazyInstance<PixelRefs> empty_pixel_refs_;
114 const Picture* picture_;
115 const PixelRefs* current_pixel_refs_;
116 unsigned current_index_;
118 gfx::Point min_point_;
119 gfx::Point max_point_;
120 int current_x_;
121 int current_y_;
124 void EmitTraceSnapshot() const;
125 void EmitTraceSnapshotAlias(Picture* original) const;
127 bool WillPlayBackBitmaps() const { return picture_->willPlayBackBitmaps(); }
129 private:
130 explicit Picture(const gfx::Rect& layer_rect);
131 // This constructor assumes SkPicture is already ref'd and transfers
132 // ownership to this picture.
133 Picture(const skia::RefPtr<SkPicture>&,
134 const gfx::Rect& layer_rect,
135 const gfx::Rect& opaque_rect,
136 const PixelRefMap& pixel_refs);
137 // This constructor will call AdoptRef on the SkPicture.
138 Picture(SkPicture*,
139 const gfx::Rect& layer_rect,
140 const gfx::Rect& opaque_rect);
141 ~Picture();
143 // Make thread-safe clones for rasterizing with.
144 void CloneForDrawing(int num_threads);
146 // Record a paint operation. To be able to safely use this SkPicture for
147 // playback on a different thread this can only be called once.
148 void Record(ContentLayerClient* client,
149 const SkTileGridPicture::TileGridInfo& tile_grid_info,
150 RecordingMode recording_mode);
152 // Gather pixel refs from recording.
153 void GatherPixelRefs(const SkTileGridPicture::TileGridInfo& tile_grid_info);
155 gfx::Rect layer_rect_;
156 gfx::Rect opaque_rect_;
157 skia::RefPtr<SkPicture> picture_;
159 typedef std::vector<scoped_refptr<Picture> > PictureVector;
160 PictureVector clones_;
162 PixelRefMap pixel_refs_;
163 gfx::Point min_pixel_cell_;
164 gfx::Point max_pixel_cell_;
165 gfx::Size cell_size_;
167 scoped_refptr<base::debug::ConvertableToTraceFormat>
168 AsTraceableRasterData(float scale) const;
169 scoped_refptr<base::debug::ConvertableToTraceFormat>
170 AsTraceableRecordData() const;
172 base::ThreadChecker raster_thread_checker_;
174 friend class base::RefCountedThreadSafe<Picture>;
175 friend class PixelRefIterator;
176 DISALLOW_COPY_AND_ASSIGN(Picture);
179 } // namespace cc
181 #endif // CC_RESOURCES_PICTURE_H_