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/lazy_instance.h"
15 #include "base/logging.h"
16 #include "base/memory/ref_counted.h"
17 #include "base/memory/scoped_ptr.h"
18 #include "base/trace_event/trace_event.h"
19 #include "cc/base/cc_export.h"
20 #include "cc/base/region.h"
21 #include "cc/resources/recording_source.h"
22 #include "skia/ext/refptr.h"
23 #include "third_party/skia/include/core/SkPicture.h"
24 #include "ui/gfx/geometry/rect.h"
26 class SkDrawPictureCallback
;
39 class ContentLayerClient
;
41 class CC_EXPORT Picture
42 : public base::RefCountedThreadSafe
<Picture
> {
44 typedef std::pair
<int, int> PixelRefMapKey
;
45 typedef std::vector
<SkPixelRef
*> PixelRefs
;
46 typedef base::hash_map
<PixelRefMapKey
, PixelRefs
> PixelRefMap
;
48 static scoped_refptr
<Picture
> Create(
49 const gfx::Rect
& layer_rect
,
50 ContentLayerClient
* client
,
51 const gfx::Size
& tile_grid_size
,
52 bool gather_pixels_refs
,
53 RecordingSource::RecordingMode recording_mode
);
54 static scoped_refptr
<Picture
> CreateFromValue(const base::Value
* value
);
55 static scoped_refptr
<Picture
> CreateFromSkpValue(const base::Value
* value
);
57 gfx::Rect
LayerRect() const { return layer_rect_
; }
59 // Has Record() been called yet?
60 bool HasRecording() const { return picture_
.get() != NULL
; }
62 bool IsSuitableForGpuRasterization(const char** reason
) const;
63 int ApproximateOpCount() const;
64 size_t ApproximateMemoryUsage() const;
68 // Apply this scale and raster the negated region into the canvas.
69 // |negated_content_region| specifies the region to be clipped out of the
70 // raster operation, i.e., the parts of the canvas which will not get drawn
72 int Raster(SkCanvas
* canvas
,
73 SkDrawPictureCallback
* callback
,
74 const Region
& negated_content_region
,
75 float contents_scale
) const;
77 // Draw the picture directly into the given canvas, without applying any
78 // clip/scale/layer transformations.
79 void Replay(SkCanvas
* canvas
);
81 scoped_ptr
<base::Value
> AsValue() const;
83 // This iterator imprecisely returns the set of pixel refs that are needed to
84 // raster this layer rect from this picture. Internally, pixel refs are
85 // clumped into tile grid buckets, so there may be false positives.
86 class CC_EXPORT PixelRefIterator
{
89 PixelRefIterator(const gfx::Rect
& layer_rect
, const Picture
* picture
);
92 SkPixelRef
* operator->() const {
93 DCHECK_LT(current_index_
, current_pixel_refs_
->size());
94 return (*current_pixel_refs_
)[current_index_
];
97 SkPixelRef
* operator*() const {
98 DCHECK_LT(current_index_
, current_pixel_refs_
->size());
99 return (*current_pixel_refs_
)[current_index_
];
102 PixelRefIterator
& operator++();
103 operator bool() const {
104 return current_index_
< current_pixel_refs_
->size();
108 static base::LazyInstance
<PixelRefs
> empty_pixel_refs_
;
109 const Picture
* picture_
;
110 const PixelRefs
* current_pixel_refs_
;
111 unsigned current_index_
;
113 gfx::Point min_point_
;
114 gfx::Point max_point_
;
119 void EmitTraceSnapshot() const;
120 void EmitTraceSnapshotAlias(Picture
* original
) const;
122 bool WillPlayBackBitmaps() const { return picture_
->willPlayBackBitmaps(); }
125 explicit Picture(const gfx::Rect
& layer_rect
);
126 // This constructor assumes SkPicture is already ref'd and transfers
127 // ownership to this picture.
128 Picture(const skia::RefPtr
<SkPicture
>&,
129 const gfx::Rect
& layer_rect
,
130 const PixelRefMap
& pixel_refs
);
131 // This constructor will call AdoptRef on the SkPicture.
132 Picture(SkPicture
*, const gfx::Rect
& layer_rect
);
135 // Record a paint operation. To be able to safely use this SkPicture for
136 // playback on a different thread this can only be called once.
137 void Record(ContentLayerClient
* client
,
138 const gfx::Size
& tile_grid_size
,
139 RecordingSource::RecordingMode recording_mode
);
141 // Gather pixel refs from recording.
142 void GatherPixelRefs(const gfx::Size
& tile_grid_info
);
144 gfx::Rect layer_rect_
;
145 skia::RefPtr
<SkPicture
> picture_
;
147 PixelRefMap pixel_refs_
;
148 gfx::Point min_pixel_cell_
;
149 gfx::Point max_pixel_cell_
;
150 gfx::Size cell_size_
;
152 scoped_refptr
<base::debug::ConvertableToTraceFormat
>
153 AsTraceableRasterData(float scale
) const;
154 scoped_refptr
<base::debug::ConvertableToTraceFormat
>
155 AsTraceableRecordData() const;
157 friend class base::RefCountedThreadSafe
<Picture
>;
158 friend class PixelRefIterator
;
159 DISALLOW_COPY_AND_ASSIGN(Picture
);
164 #endif // CC_RESOURCES_PICTURE_H_