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"
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 gfx::Size
& tile_grid_size
,
51 bool gather_pixels_refs
,
52 RecordingSource::RecordingMode recording_mode
);
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_
; }
58 // Has Record() been called yet?
59 bool HasRecording() const { return picture_
.get() != NULL
; }
61 bool IsSuitableForGpuRasterization(const char** reason
) const;
62 int ApproximateOpCount() const;
63 size_t ApproximateMemoryUsage() const;
67 // Apply this scale and raster the negated region into the canvas.
68 // |negated_content_region| specifies the region to be clipped out of the
69 // raster operation, i.e., the parts of the canvas which will not get drawn
71 int Raster(SkCanvas
* canvas
,
72 SkPicture::AbortCallback
* callback
,
73 const Region
& negated_content_region
,
74 float contents_scale
) const;
76 // Draw the picture directly into the given canvas, without applying any
77 // clip/scale/layer transformations.
78 void Replay(SkCanvas
* canvas
, SkPicture::AbortCallback
* callback
= NULL
);
80 scoped_ptr
<base::Value
> AsValue() const;
82 // This iterator imprecisely returns the set of pixel refs that are needed to
83 // raster this layer rect from this picture. Internally, pixel refs are
84 // clumped into tile grid buckets, so there may be false positives.
85 class CC_EXPORT PixelRefIterator
{
88 PixelRefIterator(const gfx::Rect
& layer_rect
, const Picture
* picture
);
91 SkPixelRef
* operator->() const {
92 DCHECK_LT(current_index_
, current_pixel_refs_
->size());
93 return (*current_pixel_refs_
)[current_index_
];
96 SkPixelRef
* operator*() const {
97 DCHECK_LT(current_index_
, current_pixel_refs_
->size());
98 return (*current_pixel_refs_
)[current_index_
];
101 PixelRefIterator
& operator++();
102 operator bool() const {
103 return current_index_
< current_pixel_refs_
->size();
107 static base::LazyInstance
<PixelRefs
> empty_pixel_refs_
;
108 const Picture
* picture_
;
109 const PixelRefs
* current_pixel_refs_
;
110 unsigned current_index_
;
112 gfx::Point min_point_
;
113 gfx::Point max_point_
;
118 void EmitTraceSnapshot() const;
119 void EmitTraceSnapshotAlias(Picture
* original
) const;
121 bool WillPlayBackBitmaps() const { return picture_
->willPlayBackBitmaps(); }
124 explicit Picture(const gfx::Rect
& layer_rect
);
125 // This constructor assumes SkPicture is already ref'd and transfers
126 // ownership to this picture.
127 Picture(const skia::RefPtr
<SkPicture
>&,
128 const gfx::Rect
& layer_rect
,
129 const PixelRefMap
& pixel_refs
);
130 // This constructor will call AdoptRef on the SkPicture.
131 Picture(SkPicture
*, const gfx::Rect
& layer_rect
);
134 // Record a paint operation. To be able to safely use this SkPicture for
135 // playback on a different thread this can only be called once.
136 void Record(ContentLayerClient
* client
,
137 const gfx::Size
& tile_grid_size
,
138 RecordingSource::RecordingMode recording_mode
);
140 // Gather pixel refs from recording.
141 void GatherPixelRefs(const gfx::Size
& tile_grid_info
);
143 gfx::Rect layer_rect_
;
144 skia::RefPtr
<SkPicture
> picture_
;
146 PixelRefMap pixel_refs_
;
147 gfx::Point min_pixel_cell_
;
148 gfx::Point max_pixel_cell_
;
149 gfx::Size cell_size_
;
151 scoped_refptr
<base::trace_event::ConvertableToTraceFormat
>
152 AsTraceableRasterData(float scale
) const;
153 scoped_refptr
<base::trace_event::ConvertableToTraceFormat
>
154 AsTraceableRecordData() const;
156 friend class base::RefCountedThreadSafe
<Picture
>;
157 friend class PixelRefIterator
;
158 DISALLOW_COPY_AND_ASSIGN(Picture
);
163 #endif // CC_RESOURCES_PICTURE_H_