Revert 268405 "Make sure that ScratchBuffer::Allocate() always r..."
[chromium-blink-merge.git] / cc / resources / picture.h
blob4761ca629a682b15f323257698bd8e86e2078548
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/SkBBHFactory.h"
24 #include "third_party/skia/include/core/SkPicture.h"
25 #include "third_party/skia/include/record/SkRecording.h"
26 #include "ui/gfx/rect.h"
28 class SkPixelRef;
30 namespace base {
31 class Value;
34 namespace skia {
35 class AnalysisCanvas;
38 namespace cc {
40 class ContentLayerClient;
42 class CC_EXPORT Picture
43 : public base::RefCountedThreadSafe<Picture> {
44 public:
45 typedef std::pair<int, int> PixelRefMapKey;
46 typedef std::vector<SkPixelRef*> PixelRefs;
47 typedef base::hash_map<PixelRefMapKey, PixelRefs> PixelRefMap;
49 enum RecordingMode {
50 RECORD_NORMALLY,
51 RECORD_WITH_SK_NULL_CANVAS,
52 RECORD_WITH_PAINTING_DISABLED,
53 RECORD_WITH_SKRECORD,
54 RECORDING_MODE_COUNT, // Must be the last entry.
57 static scoped_refptr<Picture> Create(
58 const gfx::Rect& layer_rect,
59 ContentLayerClient* client,
60 const SkTileGridFactory::TileGridInfo& tile_grid_info,
61 bool gather_pixels_refs,
62 int num_raster_threads,
63 RecordingMode recording_mode);
64 static scoped_refptr<Picture> CreateFromValue(const base::Value* value);
65 static scoped_refptr<Picture> CreateFromSkpValue(const base::Value* value);
67 gfx::Rect LayerRect() const { return layer_rect_; }
68 gfx::Rect OpaqueRect() const { return opaque_rect_; }
70 // Get thread-safe clone for rasterizing with on a specific thread.
71 Picture* GetCloneForDrawingOnThread(unsigned thread_index);
73 // Has Record() been called yet?
74 bool HasRecording() const { return picture_.get() != NULL; }
76 bool IsSuitableForGpuRasterization() const;
78 // Apply this scale and raster the negated region into the canvas. See comment
79 // in PicturePileImpl::RasterCommon for explanation on negated content region.
80 int Raster(SkCanvas* canvas,
81 SkDrawPictureCallback* callback,
82 const Region& negated_content_region,
83 float contents_scale);
85 // Draw the picture directly into the given canvas, without applying any
86 // clip/scale/layer transformations.
87 void Replay(SkCanvas* canvas);
89 scoped_ptr<base::Value> AsValue() const;
91 // This iterator imprecisely returns the set of pixel refs that are needed to
92 // raster this layer rect from this picture. Internally, pixel refs are
93 // clumped into tile grid buckets, so there may be false positives.
94 class CC_EXPORT PixelRefIterator {
95 public:
96 PixelRefIterator();
97 PixelRefIterator(const gfx::Rect& layer_rect, const Picture* picture);
98 ~PixelRefIterator();
100 SkPixelRef* operator->() const {
101 DCHECK_LT(current_index_, current_pixel_refs_->size());
102 return (*current_pixel_refs_)[current_index_];
105 SkPixelRef* operator*() const {
106 DCHECK_LT(current_index_, current_pixel_refs_->size());
107 return (*current_pixel_refs_)[current_index_];
110 PixelRefIterator& operator++();
111 operator bool() const {
112 return current_index_ < current_pixel_refs_->size();
115 private:
116 static base::LazyInstance<PixelRefs> empty_pixel_refs_;
117 const Picture* picture_;
118 const PixelRefs* current_pixel_refs_;
119 unsigned current_index_;
121 gfx::Point min_point_;
122 gfx::Point max_point_;
123 int current_x_;
124 int current_y_;
127 void EmitTraceSnapshot() const;
128 void EmitTraceSnapshotAlias(Picture* original) const;
130 bool WillPlayBackBitmaps() const { return picture_->willPlayBackBitmaps(); }
132 private:
133 explicit Picture(const gfx::Rect& layer_rect);
134 // This constructor assumes SkPicture is already ref'd and transfers
135 // ownership to this picture.
136 Picture(const skia::RefPtr<SkPicture>&,
137 const gfx::Rect& layer_rect,
138 const gfx::Rect& opaque_rect,
139 const PixelRefMap& pixel_refs);
140 // This constructor will call AdoptRef on the SkPicture.
141 Picture(SkPicture*,
142 const gfx::Rect& layer_rect,
143 const gfx::Rect& opaque_rect);
144 ~Picture();
146 // Make thread-safe clones for rasterizing with.
147 void CloneForDrawing(int num_threads);
149 // Record a paint operation. To be able to safely use this SkPicture for
150 // playback on a different thread this can only be called once.
151 void Record(ContentLayerClient* client,
152 const SkTileGridFactory::TileGridInfo& tile_grid_info,
153 RecordingMode recording_mode);
155 // Gather pixel refs from recording.
156 void GatherPixelRefs(const SkTileGridFactory::TileGridInfo& tile_grid_info);
158 gfx::Rect layer_rect_;
159 gfx::Rect opaque_rect_;
160 skia::RefPtr<SkPicture> picture_;
161 scoped_ptr<const EXPERIMENTAL::SkPlayback> playback_;
163 typedef std::vector<scoped_refptr<Picture> > PictureVector;
164 PictureVector clones_;
166 PixelRefMap pixel_refs_;
167 gfx::Point min_pixel_cell_;
168 gfx::Point max_pixel_cell_;
169 gfx::Size cell_size_;
171 scoped_refptr<base::debug::ConvertableToTraceFormat>
172 AsTraceableRasterData(float scale) const;
173 scoped_refptr<base::debug::ConvertableToTraceFormat>
174 AsTraceableRecordData() const;
176 base::ThreadChecker raster_thread_checker_;
178 friend class base::RefCountedThreadSafe<Picture>;
179 friend class PixelRefIterator;
180 DISALLOW_COPY_AND_ASSIGN(Picture);
183 } // namespace cc
185 #endif // CC_RESOURCES_PICTURE_H_