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 "cc/base/cc_export.h"
20 #include "cc/base/region.h"
21 #include "skia/ext/refptr.h"
22 #include "third_party/skia/include/core/SkBBHFactory.h"
23 #include "third_party/skia/include/core/SkPicture.h"
24 #include "third_party/skia/include/record/SkRecording.h"
25 #include "ui/gfx/rect.h"
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
;
50 RECORD_WITH_SK_NULL_CANVAS
,
51 RECORD_WITH_PAINTING_DISABLED
,
53 RECORDING_MODE_COUNT
, // Must be the last entry.
56 static scoped_refptr
<Picture
> Create(
57 const gfx::Rect
& layer_rect
,
58 ContentLayerClient
* client
,
59 const SkTileGridFactory::TileGridInfo
& tile_grid_info
,
60 bool gather_pixels_refs
,
61 RecordingMode recording_mode
);
62 static scoped_refptr
<Picture
> CreateFromValue(const base::Value
* value
);
63 static scoped_refptr
<Picture
> CreateFromSkpValue(const base::Value
* value
);
65 gfx::Rect
LayerRect() const { return layer_rect_
; }
66 gfx::Rect
OpaqueRect() const { return opaque_rect_
; }
68 // Has Record() been called yet?
69 bool HasRecording() const { return picture_
.get() != NULL
; }
71 bool IsSuitableForGpuRasterization() const;
72 int ApproximateOpCount() const;
76 // Apply this scale and raster the negated region into the canvas.
77 // |negated_content_region| specifies the region to be clipped out of the
78 // raster operation, i.e., the parts of the canvas which will not get drawn
80 int Raster(SkCanvas
* canvas
,
81 SkDrawPictureCallback
* callback
,
82 const Region
& negated_content_region
,
83 float contents_scale
) const;
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
{
97 PixelRefIterator(const gfx::Rect
& layer_rect
, const Picture
* picture
);
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();
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_
;
127 void EmitTraceSnapshot() const;
128 void EmitTraceSnapshotAlias(Picture
* original
) const;
130 bool WillPlayBackBitmaps() const { return picture_
->willPlayBackBitmaps(); }
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.
142 const gfx::Rect
& layer_rect
,
143 const gfx::Rect
& opaque_rect
);
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 SkTileGridFactory::TileGridInfo
& tile_grid_info
,
150 RecordingMode recording_mode
);
152 // Gather pixel refs from recording.
153 void GatherPixelRefs(const SkTileGridFactory::TileGridInfo
& tile_grid_info
);
155 gfx::Rect layer_rect_
;
156 gfx::Rect opaque_rect_
;
157 skia::RefPtr
<SkPicture
> picture_
;
158 scoped_ptr
<const EXPERIMENTAL::SkPlayback
> playback_
;
160 PixelRefMap pixel_refs_
;
161 gfx::Point min_pixel_cell_
;
162 gfx::Point max_pixel_cell_
;
163 gfx::Size cell_size_
;
165 scoped_refptr
<base::debug::ConvertableToTraceFormat
>
166 AsTraceableRasterData(float scale
) const;
167 scoped_refptr
<base::debug::ConvertableToTraceFormat
>
168 AsTraceableRecordData() const;
170 friend class base::RefCountedThreadSafe
<Picture
>;
171 friend class PixelRefIterator
;
172 DISALLOW_COPY_AND_ASSIGN(Picture
);
177 #endif // CC_RESOURCES_PICTURE_H_