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_
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"
38 class ContentLayerClient;
40 class CC_EXPORT Picture
41 : public base::RefCountedThreadSafe<Picture> {
43 typedef std::pair<int, int> PixelRefMapKey;
44 typedef std::vector<SkPixelRef*> PixelRefs;
45 typedef base::hash_map<PixelRefMapKey, PixelRefs> PixelRefMap;
47 static scoped_refptr<Picture> Create(
48 const gfx::Rect& layer_rect,
49 ContentLayerClient* client,
50 const SkTileGridPicture::TileGridInfo& tile_grid_info,
51 bool gather_pixels_refs,
52 int num_raster_threads);
53 static scoped_refptr<Picture> CreateFromValue(const base::Value* value);
54 static scoped_refptr<Picture> CreateFromSkpValue(const base::Value* value);
56 gfx::Rect LayerRect() const { return layer_rect_; }
57 gfx::Rect OpaqueRect() const { return opaque_rect_; }
59 // Get thread-safe clone for rasterizing with on a specific thread.
60 Picture* GetCloneForDrawingOnThread(unsigned thread_index);
62 // Has Record() been called yet?
63 bool HasRecording() const { return picture_.get() != NULL; }
65 // Apply this scale and raster the negated region into the canvas. See comment
66 // in PicturePileImpl::RasterCommon for explanation on negated content region.
67 int Raster(SkCanvas* canvas,
68 SkDrawPictureCallback* callback,
69 const Region& negated_content_region,
70 float contents_scale);
72 // Draw the picture directly into the given canvas, without applying any
73 // clip/scale/layer transformations.
74 void Replay(SkCanvas* canvas);
76 scoped_ptr<base::Value> AsValue() const;
78 class CC_EXPORT PixelRefIterator {
81 PixelRefIterator(const gfx::Rect& layer_rect, const Picture* picture);
84 SkPixelRef* operator->() const {
85 DCHECK_LT(current_index_, current_pixel_refs_->size());
86 return (*current_pixel_refs_)[current_index_];
89 SkPixelRef* operator*() const {
90 DCHECK_LT(current_index_, current_pixel_refs_->size());
91 return (*current_pixel_refs_)[current_index_];
94 PixelRefIterator& operator++();
95 operator bool() const {
96 return current_index_ < current_pixel_refs_->size();
100 static base::LazyInstance<PixelRefs> empty_pixel_refs_;
101 const Picture* picture_;
102 const PixelRefs* current_pixel_refs_;
103 unsigned current_index_;
105 gfx::Point min_point_;
106 gfx::Point max_point_;
111 void EmitTraceSnapshot() const;
112 void EmitTraceSnapshotAlias(Picture* original) const;
114 bool WillPlayBackBitmaps() const { return picture_->willPlayBackBitmaps(); }
117 explicit Picture(const gfx::Rect& layer_rect);
118 // This constructor assumes SkPicture is already ref'd and transfers
119 // ownership to this picture.
120 Picture(const skia::RefPtr<SkPicture>&,
121 const gfx::Rect& layer_rect,
122 const gfx::Rect& opaque_rect,
123 const PixelRefMap& pixel_refs);
124 // This constructor will call AdoptRef on the SkPicture.
126 const gfx::Rect& layer_rect,
127 const gfx::Rect& opaque_rect);
130 // Make thread-safe clones for rasterizing with.
131 void CloneForDrawing(int num_threads);
133 // Record a paint operation. To be able to safely use this SkPicture for
134 // playback on a different thread this can only be called once.
135 void Record(ContentLayerClient* client,
136 const SkTileGridPicture::TileGridInfo& tile_grid_info);
138 // Gather pixel refs from recording.
139 void GatherPixelRefs(const SkTileGridPicture::TileGridInfo& tile_grid_info);
141 gfx::Rect layer_rect_;
142 gfx::Rect opaque_rect_;
143 skia::RefPtr<SkPicture> picture_;
145 typedef std::vector<scoped_refptr<Picture> > PictureVector;
146 PictureVector clones_;
148 PixelRefMap pixel_refs_;
149 gfx::Point min_pixel_cell_;
150 gfx::Point max_pixel_cell_;
151 gfx::Size cell_size_;
153 scoped_refptr<base::debug::ConvertableToTraceFormat>
154 AsTraceableRasterData(float scale) const;
155 scoped_refptr<base::debug::ConvertableToTraceFormat>
156 AsTraceableRecordData() const;
158 base::ThreadChecker raster_thread_checker_;
160 friend class base::RefCountedThreadSafe<Picture>;
161 friend class PixelRefIterator;
162 DISALLOW_COPY_AND_ASSIGN(Picture);
167 #endif // CC_RESOURCES_PICTURE_H_