OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "cc/playback/display_item_list.h" | 5 #include "cc/playback/display_item_list.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "cc/output/filter_operation.h" | 9 #include "cc/output/filter_operation.h" |
10 #include "cc/output/filter_operations.h" | 10 #include "cc/output/filter_operations.h" |
11 #include "cc/playback/clip_display_item.h" | 11 #include "cc/playback/clip_display_item.h" |
12 #include "cc/playback/clip_path_display_item.h" | |
13 #include "cc/playback/compositing_display_item.h" | |
12 #include "cc/playback/display_item_list_settings.h" | 14 #include "cc/playback/display_item_list_settings.h" |
13 #include "cc/playback/drawing_display_item.h" | 15 #include "cc/playback/drawing_display_item.h" |
14 #include "cc/playback/filter_display_item.h" | 16 #include "cc/playback/filter_display_item.h" |
17 #include "cc/playback/float_clip_display_item.h" | |
15 #include "cc/playback/transform_display_item.h" | 18 #include "cc/playback/transform_display_item.h" |
19 #include "cc/proto/display_item.pb.h" | |
16 #include "cc/test/skia_common.h" | 20 #include "cc/test/skia_common.h" |
17 #include "skia/ext/refptr.h" | 21 #include "skia/ext/refptr.h" |
18 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
19 #include "third_party/skia/include/core/SkBitmap.h" | 23 #include "third_party/skia/include/core/SkBitmap.h" |
20 #include "third_party/skia/include/core/SkCanvas.h" | 24 #include "third_party/skia/include/core/SkCanvas.h" |
21 #include "third_party/skia/include/core/SkColor.h" | 25 #include "third_party/skia/include/core/SkColor.h" |
22 #include "third_party/skia/include/core/SkPictureRecorder.h" | 26 #include "third_party/skia/include/core/SkPictureRecorder.h" |
23 #include "third_party/skia/include/core/SkSurface.h" | 27 #include "third_party/skia/include/core/SkSurface.h" |
28 #include "third_party/skia/include/core/SkXfermode.h" | |
24 #include "third_party/skia/include/effects/SkImageSource.h" | 29 #include "third_party/skia/include/effects/SkImageSource.h" |
25 #include "third_party/skia/include/utils/SkPictureUtils.h" | 30 #include "third_party/skia/include/utils/SkPictureUtils.h" |
26 #include "ui/gfx/geometry/rect_conversions.h" | 31 #include "ui/gfx/geometry/rect_conversions.h" |
27 #include "ui/gfx/skia_util.h" | 32 #include "ui/gfx/skia_util.h" |
28 | 33 |
29 namespace cc { | 34 namespace cc { |
30 | 35 |
36 namespace { | |
37 | |
38 void ValidateDisplayItemListSerialization(const gfx::Size& layer_size, | |
39 scoped_refptr<DisplayItemList> list) { | |
40 // Serialize and deserialize the DisplayItemList. | |
41 proto::DisplayItemList proto; | |
42 list->ToProtobuf(&proto); | |
43 scoped_refptr<DisplayItemList> new_list = DisplayItemList::Create(proto); | |
44 | |
45 // Finalize the DisplayItemLists to perform raster. | |
46 list->Finalize(); | |
47 new_list->Finalize(); | |
48 | |
49 const int pixel_size = 4 * layer_size.GetArea(); | |
50 | |
51 // Get the rendered contents of the old DisplayItemList. | |
52 scoped_ptr<unsigned char[]> pixels(new unsigned char[pixel_size]); | |
53 memset(pixels.get(), 0, pixel_size); | |
54 DrawDisplayList(pixels.get(), gfx::Rect(layer_size), list); | |
55 | |
56 // Get the rendered contents of the new DisplayItemList. | |
57 scoped_ptr<unsigned char[]> new_pixels(new unsigned char[pixel_size]); | |
58 memset(new_pixels.get(), 0, pixel_size); | |
59 DrawDisplayList(new_pixels.get(), gfx::Rect(layer_size), new_list); | |
60 | |
61 EXPECT_EQ(0, memcmp(pixels.get(), new_pixels.get(), pixel_size)); | |
62 } | |
63 | |
64 } // namespace | |
65 | |
66 TEST(DisplayItemListTest, SerializeDisplayItemListSettings) { | |
67 DisplayItemListSettings settings; | |
68 settings.use_cached_picture = false; | |
69 | |
70 { | |
71 proto::DisplayItemListSettings proto; | |
72 settings.ToProtobuf(&proto); | |
73 DisplayItemListSettings deserialized(proto); | |
74 EXPECT_EQ(settings.use_cached_picture, deserialized.use_cached_picture); | |
75 } | |
76 | |
77 settings.use_cached_picture = true; | |
78 { | |
79 proto::DisplayItemListSettings proto; | |
80 settings.ToProtobuf(&proto); | |
81 DisplayItemListSettings deserialized(proto); | |
82 EXPECT_EQ(settings.use_cached_picture, deserialized.use_cached_picture); | |
83 } | |
84 } | |
85 | |
86 TEST(DisplayItemListTest, SerializeSingleDrawingItem) { | |
87 gfx::Size layer_size(10, 10); | |
88 gfx::PointF offset(2.f, 3.f); | |
89 | |
90 DisplayItemListSettings settings; | |
91 settings.use_cached_picture = true; | |
92 scoped_refptr<DisplayItemList> list = | |
93 DisplayItemList::Create(gfx::Rect(layer_size), settings); | |
94 | |
95 // Build the DrawingDisplayItem. | |
96 SkPaint blue_paint; | |
97 blue_paint.setColor(SK_ColorBLUE); | |
98 SkPaint red_paint; | |
99 red_paint.setColor(SK_ColorRED); | |
100 SkPictureRecorder recorder; | |
101 skia::RefPtr<SkCanvas> canvas; | |
102 skia::RefPtr<SkPicture> picture; | |
103 canvas = skia::SharePtr(recorder.beginRecording(SkRect::MakeXYWH( | |
104 offset.x(), offset.y(), layer_size.width(), layer_size.height()))); | |
105 canvas->translate(offset.x(), offset.y()); | |
106 canvas->drawRectCoords(0.f, 0.f, 4.f, 4.f, red_paint); | |
107 canvas->drawRectCoords(3.f, 3.f, 7.f, 7.f, blue_paint); | |
108 picture = skia::AdoptRef(recorder.endRecordingAsPicture()); | |
109 auto* item = list->CreateAndAppendItem<DrawingDisplayItem>(); | |
110 item->SetNew(picture); | |
111 | |
112 ValidateDisplayItemListSerialization(layer_size, list); | |
113 } | |
114 | |
115 TEST(DisplayItemListTest, SerializeClipItem) { | |
116 gfx::Size layer_size(10, 10); | |
117 gfx::PointF offset(2.f, 3.f); | |
118 | |
119 DisplayItemListSettings settings; | |
120 settings.use_cached_picture = true; | |
121 scoped_refptr<DisplayItemList> list = | |
122 DisplayItemList::Create(gfx::Rect(layer_size), settings); | |
123 | |
124 // Build the DrawingDisplayItem. | |
David Trainor- moved to gerrit
2015/10/14 23:09:28
TODO(me): Pull out the common code for these unit
David Trainor- moved to gerrit
2015/10/20 20:26:49
Done.
| |
125 SkPaint blue_paint; | |
126 blue_paint.setColor(SK_ColorBLUE); | |
127 SkPaint red_paint; | |
128 red_paint.setColor(SK_ColorRED); | |
129 SkPictureRecorder recorder; | |
130 skia::RefPtr<SkCanvas> canvas; | |
131 skia::RefPtr<SkPicture> picture; | |
132 canvas = skia::SharePtr(recorder.beginRecording(SkRect::MakeXYWH( | |
133 offset.x(), offset.y(), layer_size.width(), layer_size.height()))); | |
134 canvas->translate(offset.x(), offset.y()); | |
135 canvas->drawRectCoords(0.f, 0.f, 4.f, 4.f, red_paint); | |
136 picture = skia::AdoptRef(recorder.endRecordingAsPicture()); | |
137 auto* item1 = list->CreateAndAppendItem<DrawingDisplayItem>(); | |
138 item1->SetNew(picture); | |
139 | |
140 // Build the ClipDisplayItem. | |
141 gfx::Rect clip_rect(6, 6, 1, 1); | |
142 std::vector<SkRRect> rrects; | |
143 rrects.push_back(SkRRect::MakeOval(SkRect::MakeXYWH(5.f, 5.f, 4.f, 4.f))); | |
144 auto* item2 = list->CreateAndAppendItem<ClipDisplayItem>(); | |
145 item2->SetNew(clip_rect, rrects); | |
146 | |
147 // Build the second DrawingDisplayItem. | |
148 offset.SetPoint(2.f, 2.f); | |
149 canvas = skia::SharePtr(recorder.beginRecording(SkRect::MakeXYWH( | |
150 offset.x(), offset.y(), layer_size.width(), layer_size.height()))); | |
151 canvas->translate(offset.x(), offset.y()); | |
152 canvas->drawRectCoords(3.f, 3.f, 7.f, 7.f, blue_paint); | |
153 picture = skia::AdoptRef(recorder.endRecordingAsPicture()); | |
154 auto* item3 = list->CreateAndAppendItem<DrawingDisplayItem>(); | |
155 item3->SetNew(picture); | |
156 | |
157 // Build the EndClipDisplayItem. | |
158 list->CreateAndAppendItem<EndClipDisplayItem>(); | |
159 | |
160 ValidateDisplayItemListSerialization(layer_size, list); | |
161 } | |
162 | |
163 TEST(DisplayItemListTest, SerializeClipPathItem) { | |
164 gfx::Size layer_size(10, 10); | |
165 gfx::PointF offset(2.f, 3.f); | |
166 | |
167 DisplayItemListSettings settings; | |
168 settings.use_cached_picture = true; | |
169 scoped_refptr<DisplayItemList> list = | |
170 DisplayItemList::Create(gfx::Rect(layer_size), settings); | |
171 | |
172 // Build the DrawingDisplayItem. | |
173 SkPaint blue_paint; | |
174 blue_paint.setColor(SK_ColorBLUE); | |
175 SkPaint red_paint; | |
176 red_paint.setColor(SK_ColorRED); | |
177 SkPictureRecorder recorder; | |
178 skia::RefPtr<SkCanvas> canvas; | |
179 skia::RefPtr<SkPicture> picture; | |
180 canvas = skia::SharePtr(recorder.beginRecording(SkRect::MakeXYWH( | |
181 offset.x(), offset.y(), layer_size.width(), layer_size.height()))); | |
182 canvas->translate(offset.x(), offset.y()); | |
183 canvas->drawRectCoords(0.f, 0.f, 4.f, 4.f, red_paint); | |
184 picture = skia::AdoptRef(recorder.endRecordingAsPicture()); | |
185 auto* item1 = list->CreateAndAppendItem<DrawingDisplayItem>(); | |
186 item1->SetNew(picture); | |
187 | |
188 // Build the ClipPathDisplayItem. | |
189 SkPath path; | |
190 path.addCircle(5.f, 5.f, 2.f, SkPath::Direction::kCW_Direction); | |
191 auto* item2 = list->CreateAndAppendItem<ClipPathDisplayItem>(); | |
192 item2->SetNew(path, SkRegion::Op::kReplace_Op, false); | |
193 | |
194 // Build the second DrawingDisplayItem. | |
195 offset.SetPoint(2.f, 2.f); | |
196 canvas = skia::SharePtr(recorder.beginRecording(SkRect::MakeXYWH( | |
197 offset.x(), offset.y(), layer_size.width(), layer_size.height()))); | |
198 canvas->translate(offset.x(), offset.y()); | |
199 canvas->drawRectCoords(3.f, 3.f, 7.f, 7.f, blue_paint); | |
200 picture = skia::AdoptRef(recorder.endRecordingAsPicture()); | |
201 auto* item3 = list->CreateAndAppendItem<DrawingDisplayItem>(); | |
202 item3->SetNew(picture); | |
203 | |
204 // Build the EndClipPathDisplayItem. | |
205 list->CreateAndAppendItem<EndClipPathDisplayItem>(); | |
206 | |
207 ValidateDisplayItemListSerialization(layer_size, list); | |
208 } | |
209 | |
210 TEST(DisplayItemListTest, SerializeCompositingItem) { | |
211 gfx::Size layer_size(10, 10); | |
212 gfx::PointF offset(2.f, 3.f); | |
213 | |
214 DisplayItemListSettings settings; | |
215 settings.use_cached_picture = true; | |
216 scoped_refptr<DisplayItemList> list = | |
217 DisplayItemList::Create(gfx::Rect(layer_size), settings); | |
218 | |
219 // Build the DrawingDisplayItem. | |
220 SkPaint blue_paint; | |
221 blue_paint.setColor(SK_ColorBLUE); | |
222 SkPaint red_paint; | |
223 red_paint.setColor(SK_ColorRED); | |
224 SkPictureRecorder recorder; | |
225 skia::RefPtr<SkCanvas> canvas; | |
226 skia::RefPtr<SkPicture> picture; | |
227 canvas = skia::SharePtr(recorder.beginRecording(SkRect::MakeXYWH( | |
228 offset.x(), offset.y(), layer_size.width(), layer_size.height()))); | |
229 canvas->translate(offset.x(), offset.y()); | |
230 canvas->drawRectCoords(0.f, 0.f, 4.f, 4.f, red_paint); | |
231 picture = skia::AdoptRef(recorder.endRecordingAsPicture()); | |
232 auto* item1 = list->CreateAndAppendItem<DrawingDisplayItem>(); | |
233 item1->SetNew(picture); | |
234 | |
235 // Build the CompositingDisplayItem. | |
236 skia::RefPtr<SkColorFilter> filter = skia::AdoptRef( | |
237 SkColorFilter::CreateLightingFilter(SK_ColorRED, SK_ColorGREEN)); | |
238 auto* item2 = list->CreateAndAppendItem<CompositingDisplayItem>(); | |
239 item2->SetNew(150, SkXfermode::Mode::kDst_Mode, nullptr, filter); | |
240 | |
241 // Build the second DrawingDisplayItem. | |
242 offset.SetPoint(2.f, 2.f); | |
243 canvas = skia::SharePtr(recorder.beginRecording(SkRect::MakeXYWH( | |
244 offset.x(), offset.y(), layer_size.width(), layer_size.height()))); | |
245 canvas->translate(offset.x(), offset.y()); | |
246 canvas->drawRectCoords(3.f, 3.f, 7.f, 7.f, blue_paint); | |
247 picture = skia::AdoptRef(recorder.endRecordingAsPicture()); | |
248 auto* item3 = list->CreateAndAppendItem<DrawingDisplayItem>(); | |
249 item3->SetNew(picture); | |
250 | |
251 // Build the EndCompositingDisplayItem. | |
252 list->CreateAndAppendItem<EndCompositingDisplayItem>(); | |
253 | |
254 ValidateDisplayItemListSerialization(layer_size, list); | |
255 } | |
256 | |
257 TEST(DisplayItemListTest, SerializeFloatClipItem) { | |
258 gfx::Size layer_size(10, 10); | |
259 gfx::PointF offset(2.f, 3.f); | |
260 | |
261 DisplayItemListSettings settings; | |
262 settings.use_cached_picture = true; | |
263 scoped_refptr<DisplayItemList> list = | |
264 DisplayItemList::Create(gfx::Rect(layer_size), settings); | |
265 | |
266 // Build the DrawingDisplayItem. | |
267 SkPaint blue_paint; | |
268 blue_paint.setColor(SK_ColorBLUE); | |
269 SkPaint red_paint; | |
270 red_paint.setColor(SK_ColorRED); | |
271 SkPictureRecorder recorder; | |
272 skia::RefPtr<SkCanvas> canvas; | |
273 skia::RefPtr<SkPicture> picture; | |
274 canvas = skia::SharePtr(recorder.beginRecording(SkRect::MakeXYWH( | |
275 offset.x(), offset.y(), layer_size.width(), layer_size.height()))); | |
276 canvas->translate(offset.x(), offset.y()); | |
277 canvas->drawRectCoords(0.f, 0.f, 4.f, 4.f, red_paint); | |
278 picture = skia::AdoptRef(recorder.endRecordingAsPicture()); | |
279 auto* item1 = list->CreateAndAppendItem<DrawingDisplayItem>(); | |
280 item1->SetNew(picture); | |
281 | |
282 // Build the FloatClipDisplayItem. | |
283 gfx::RectF clip_rect(6.f, 6.f, 1.f, 1.f); | |
284 auto* item2 = list->CreateAndAppendItem<FloatClipDisplayItem>(); | |
285 item2->SetNew(clip_rect); | |
286 | |
287 // Build the second DrawingDisplayItem. | |
288 offset.SetPoint(2.f, 2.f); | |
289 canvas = skia::SharePtr(recorder.beginRecording(SkRect::MakeXYWH( | |
290 offset.x(), offset.y(), layer_size.width(), layer_size.height()))); | |
291 canvas->translate(offset.x(), offset.y()); | |
292 canvas->drawRectCoords(3.f, 3.f, 7.f, 7.f, blue_paint); | |
293 picture = skia::AdoptRef(recorder.endRecordingAsPicture()); | |
294 auto* item3 = list->CreateAndAppendItem<DrawingDisplayItem>(); | |
295 item3->SetNew(picture); | |
296 | |
297 // Build the EndFloatClipDisplayItem. | |
298 list->CreateAndAppendItem<EndFloatClipDisplayItem>(); | |
299 | |
300 ValidateDisplayItemListSerialization(layer_size, list); | |
301 } | |
302 | |
303 TEST(DisplayItemListTest, SerializeTransformItem) { | |
304 gfx::Size layer_size(10, 10); | |
305 gfx::PointF offset(2.f, 3.f); | |
306 | |
307 DisplayItemListSettings settings; | |
308 settings.use_cached_picture = true; | |
309 scoped_refptr<DisplayItemList> list = | |
310 DisplayItemList::Create(gfx::Rect(layer_size), settings); | |
311 | |
312 // Build the DrawingDisplayItem. | |
313 SkPaint blue_paint; | |
314 blue_paint.setColor(SK_ColorBLUE); | |
315 SkPaint red_paint; | |
316 red_paint.setColor(SK_ColorRED); | |
317 SkPictureRecorder recorder; | |
318 skia::RefPtr<SkCanvas> canvas; | |
319 skia::RefPtr<SkPicture> picture; | |
320 canvas = skia::SharePtr(recorder.beginRecording(SkRect::MakeXYWH( | |
321 offset.x(), offset.y(), layer_size.width(), layer_size.height()))); | |
322 canvas->translate(offset.x(), offset.y()); | |
323 canvas->drawRectCoords(0.f, 0.f, 4.f, 4.f, red_paint); | |
324 picture = skia::AdoptRef(recorder.endRecordingAsPicture()); | |
325 auto* item1 = list->CreateAndAppendItem<DrawingDisplayItem>(); | |
326 item1->SetNew(picture); | |
327 | |
328 // Build the TransformDisplayItem. | |
329 gfx::Transform transform; | |
330 transform.Scale(1.25f, 1.25f); | |
331 transform.Translate(-1.f, -1.f); | |
332 auto* item2 = list->CreateAndAppendItem<TransformDisplayItem>(); | |
333 item2->SetNew(transform); | |
334 | |
335 // Build the second DrawingDisplayItem. | |
336 offset.SetPoint(2.f, 2.f); | |
337 canvas = skia::SharePtr(recorder.beginRecording(SkRect::MakeXYWH( | |
338 offset.x(), offset.y(), layer_size.width(), layer_size.height()))); | |
339 canvas->translate(offset.x(), offset.y()); | |
340 canvas->drawRectCoords(3.f, 3.f, 7.f, 7.f, blue_paint); | |
341 picture = skia::AdoptRef(recorder.endRecordingAsPicture()); | |
342 auto* item3 = list->CreateAndAppendItem<DrawingDisplayItem>(); | |
343 item3->SetNew(picture); | |
344 | |
345 // Build the EndTransformDisplayItem. | |
346 list->CreateAndAppendItem<EndTransformDisplayItem>(); | |
347 | |
348 ValidateDisplayItemListSerialization(layer_size, list); | |
349 } | |
350 | |
31 TEST(DisplayItemListTest, SingleDrawingItem) { | 351 TEST(DisplayItemListTest, SingleDrawingItem) { |
32 gfx::Rect layer_rect(100, 100); | 352 gfx::Rect layer_rect(100, 100); |
33 SkPictureRecorder recorder; | 353 SkPictureRecorder recorder; |
34 skia::RefPtr<SkCanvas> canvas; | 354 skia::RefPtr<SkCanvas> canvas; |
35 skia::RefPtr<SkPicture> picture; | 355 skia::RefPtr<SkPicture> picture; |
36 SkPaint blue_paint; | 356 SkPaint blue_paint; |
37 blue_paint.setColor(SK_ColorBLUE); | 357 blue_paint.setColor(SK_ColorBLUE); |
38 SkPaint red_paint; | 358 SkPaint red_paint; |
39 red_paint.setColor(SK_ColorRED); | 359 red_paint.setColor(SK_ColorRED); |
40 unsigned char pixels[4 * 100 * 100] = {0}; | 360 unsigned char pixels[4 * 100 * 100] = {0}; |
(...skipping 428 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
469 // categories being traced). | 789 // categories being traced). |
470 list = new DisplayItemList(layer_rect, caching_settings, true); | 790 list = new DisplayItemList(layer_rect, caching_settings, true); |
471 item = list->CreateAndAppendItem<DrawingDisplayItem>(); | 791 item = list->CreateAndAppendItem<DrawingDisplayItem>(); |
472 item->SetNew(picture); | 792 item->SetNew(picture); |
473 list->Finalize(); | 793 list->Finalize(); |
474 memory_usage = list->ApproximateMemoryUsage(); | 794 memory_usage = list->ApproximateMemoryUsage(); |
475 EXPECT_EQ(static_cast<size_t>(0), memory_usage); | 795 EXPECT_EQ(static_cast<size_t>(0), memory_usage); |
476 } | 796 } |
477 | 797 |
478 } // namespace cc | 798 } // namespace cc |
OLD | NEW |