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 "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
;
49 RECORD_WITH_SK_NULL_CANVAS
,
50 RECORD_WITH_PAINTING_DISABLED
,
51 RECORDING_MODE_COUNT
, // Must be the last entry.
54 static scoped_refptr
<Picture
> Create(
55 const gfx::Rect
& layer_rect
,
56 ContentLayerClient
* client
,
57 const SkTileGridFactory::TileGridInfo
& tile_grid_info
,
58 bool gather_pixels_refs
,
59 RecordingMode recording_mode
);
60 static scoped_refptr
<Picture
> CreateFromValue(const base::Value
* value
);
61 static scoped_refptr
<Picture
> CreateFromSkpValue(const base::Value
* value
);
63 gfx::Rect
LayerRect() const { return layer_rect_
; }
65 // Has Record() been called yet?
66 bool HasRecording() const { return picture_
.get() != NULL
; }
68 bool IsSuitableForGpuRasterization() const;
69 int ApproximateOpCount() const;
73 // Apply this scale and raster the negated region into the canvas.
74 // |negated_content_region| specifies the region to be clipped out of the
75 // raster operation, i.e., the parts of the canvas which will not get drawn
77 int Raster(SkCanvas
* canvas
,
78 SkDrawPictureCallback
* callback
,
79 const Region
& negated_content_region
,
80 float contents_scale
) const;
82 // Draw the picture directly into the given canvas, without applying any
83 // clip/scale/layer transformations.
84 void Replay(SkCanvas
* canvas
);
86 scoped_ptr
<base::Value
> AsValue() const;
88 // This iterator imprecisely returns the set of pixel refs that are needed to
89 // raster this layer rect from this picture. Internally, pixel refs are
90 // clumped into tile grid buckets, so there may be false positives.
91 class CC_EXPORT PixelRefIterator
{
94 PixelRefIterator(const gfx::Rect
& layer_rect
, const Picture
* picture
);
97 SkPixelRef
* operator->() const {
98 DCHECK_LT(current_index_
, current_pixel_refs_
->size());
99 return (*current_pixel_refs_
)[current_index_
];
102 SkPixelRef
* operator*() const {
103 DCHECK_LT(current_index_
, current_pixel_refs_
->size());
104 return (*current_pixel_refs_
)[current_index_
];
107 PixelRefIterator
& operator++();
108 operator bool() const {
109 return current_index_
< current_pixel_refs_
->size();
113 static base::LazyInstance
<PixelRefs
> empty_pixel_refs_
;
114 const Picture
* picture_
;
115 const PixelRefs
* current_pixel_refs_
;
116 unsigned current_index_
;
118 gfx::Point min_point_
;
119 gfx::Point max_point_
;
124 void EmitTraceSnapshot() const;
125 void EmitTraceSnapshotAlias(Picture
* original
) const;
127 bool WillPlayBackBitmaps() const { return picture_
->willPlayBackBitmaps(); }
130 explicit Picture(const gfx::Rect
& layer_rect
);
131 // This constructor assumes SkPicture is already ref'd and transfers
132 // ownership to this picture.
133 Picture(const skia::RefPtr
<SkPicture
>&,
134 const gfx::Rect
& layer_rect
,
135 const PixelRefMap
& pixel_refs
);
136 // This constructor will call AdoptRef on the SkPicture.
137 Picture(SkPicture
*, const gfx::Rect
& layer_rect
);
140 // Record a paint operation. To be able to safely use this SkPicture for
141 // playback on a different thread this can only be called once.
142 void Record(ContentLayerClient
* client
,
143 const SkTileGridFactory::TileGridInfo
& tile_grid_info
,
144 RecordingMode recording_mode
);
146 // Gather pixel refs from recording.
147 void GatherPixelRefs(const SkTileGridFactory::TileGridInfo
& tile_grid_info
);
149 gfx::Rect layer_rect_
;
150 skia::RefPtr
<SkPicture
> picture_
;
152 PixelRefMap pixel_refs_
;
153 gfx::Point min_pixel_cell_
;
154 gfx::Point max_pixel_cell_
;
155 gfx::Size cell_size_
;
157 scoped_refptr
<base::debug::ConvertableToTraceFormat
>
158 AsTraceableRasterData(float scale
) const;
159 scoped_refptr
<base::debug::ConvertableToTraceFormat
>
160 AsTraceableRecordData() const;
162 friend class base::RefCountedThreadSafe
<Picture
>;
163 friend class PixelRefIterator
;
164 DISALLOW_COPY_AND_ASSIGN(Picture
);
169 #endif // CC_RESOURCES_PICTURE_H_