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 AppendFirstSerializationTestPicture(scoped_refptr<DisplayItemList> list, |
| 39 const gfx::Size& layer_size) { |
| 40 gfx::PointF offset(2.f, 3.f); |
| 41 SkPictureRecorder recorder; |
| 42 skia::RefPtr<SkCanvas> canvas; |
| 43 skia::RefPtr<SkPicture> picture; |
| 44 |
| 45 SkPaint red_paint; |
| 46 red_paint.setColor(SK_ColorRED); |
| 47 |
| 48 canvas = skia::SharePtr(recorder.beginRecording(SkRect::MakeXYWH( |
| 49 offset.x(), offset.y(), layer_size.width(), layer_size.height()))); |
| 50 canvas->translate(offset.x(), offset.y()); |
| 51 canvas->drawRectCoords(0.f, 0.f, 4.f, 4.f, red_paint); |
| 52 picture = skia::AdoptRef(recorder.endRecordingAsPicture()); |
| 53 list->CreateAndAppendItem<DrawingDisplayItem>()->SetNew(picture); |
| 54 } |
| 55 |
| 56 void AppendSecondSerializationTestPicture(scoped_refptr<DisplayItemList> list, |
| 57 const gfx::Size& layer_size) { |
| 58 gfx::PointF offset(2.f, 2.f); |
| 59 SkPictureRecorder recorder; |
| 60 skia::RefPtr<SkCanvas> canvas; |
| 61 skia::RefPtr<SkPicture> picture; |
| 62 |
| 63 SkPaint blue_paint; |
| 64 blue_paint.setColor(SK_ColorBLUE); |
| 65 |
| 66 canvas = skia::SharePtr(recorder.beginRecording(SkRect::MakeXYWH( |
| 67 offset.x(), offset.y(), layer_size.width(), layer_size.height()))); |
| 68 canvas->translate(offset.x(), offset.y()); |
| 69 canvas->drawRectCoords(3.f, 3.f, 7.f, 7.f, blue_paint); |
| 70 picture = skia::AdoptRef(recorder.endRecordingAsPicture()); |
| 71 list->CreateAndAppendItem<DrawingDisplayItem>()->SetNew(picture); |
| 72 } |
| 73 |
| 74 void ValidateDisplayItemListSerialization(const gfx::Size& layer_size, |
| 75 scoped_refptr<DisplayItemList> list) { |
| 76 // Serialize and deserialize the DisplayItemList. |
| 77 proto::DisplayItemList proto; |
| 78 list->ToProtobuf(&proto); |
| 79 scoped_refptr<DisplayItemList> new_list = DisplayItemList::Create(proto); |
| 80 |
| 81 // Finalize the DisplayItemLists to perform raster. |
| 82 list->Finalize(); |
| 83 new_list->Finalize(); |
| 84 |
| 85 const int pixel_size = 4 * layer_size.GetArea(); |
| 86 |
| 87 // Get the rendered contents of the old DisplayItemList. |
| 88 scoped_ptr<unsigned char[]> pixels(new unsigned char[pixel_size]); |
| 89 memset(pixels.get(), 0, pixel_size); |
| 90 DrawDisplayList(pixels.get(), gfx::Rect(layer_size), list); |
| 91 |
| 92 // Get the rendered contents of the new DisplayItemList. |
| 93 scoped_ptr<unsigned char[]> new_pixels(new unsigned char[pixel_size]); |
| 94 memset(new_pixels.get(), 0, pixel_size); |
| 95 DrawDisplayList(new_pixels.get(), gfx::Rect(layer_size), new_list); |
| 96 |
| 97 EXPECT_EQ(0, memcmp(pixels.get(), new_pixels.get(), pixel_size)); |
| 98 } |
| 99 |
| 100 } // namespace |
| 101 |
| 102 TEST(DisplayItemListTest, SerializeDisplayItemListSettings) { |
| 103 DisplayItemListSettings settings; |
| 104 settings.use_cached_picture = false; |
| 105 |
| 106 { |
| 107 proto::DisplayItemListSettings proto; |
| 108 settings.ToProtobuf(&proto); |
| 109 DisplayItemListSettings deserialized(proto); |
| 110 EXPECT_EQ(settings.use_cached_picture, deserialized.use_cached_picture); |
| 111 } |
| 112 |
| 113 settings.use_cached_picture = true; |
| 114 { |
| 115 proto::DisplayItemListSettings proto; |
| 116 settings.ToProtobuf(&proto); |
| 117 DisplayItemListSettings deserialized(proto); |
| 118 EXPECT_EQ(settings.use_cached_picture, deserialized.use_cached_picture); |
| 119 } |
| 120 } |
| 121 |
| 122 TEST(DisplayItemListTest, SerializeSingleDrawingItem) { |
| 123 gfx::Size layer_size(10, 10); |
| 124 |
| 125 DisplayItemListSettings settings; |
| 126 settings.use_cached_picture = true; |
| 127 scoped_refptr<DisplayItemList> list = |
| 128 DisplayItemList::Create(gfx::Rect(layer_size), settings); |
| 129 |
| 130 // Build the DrawingDisplayItem. |
| 131 AppendFirstSerializationTestPicture(list, layer_size); |
| 132 |
| 133 ValidateDisplayItemListSerialization(layer_size, list); |
| 134 } |
| 135 |
| 136 TEST(DisplayItemListTest, SerializeClipItem) { |
| 137 gfx::Size layer_size(10, 10); |
| 138 |
| 139 DisplayItemListSettings settings; |
| 140 settings.use_cached_picture = true; |
| 141 scoped_refptr<DisplayItemList> list = |
| 142 DisplayItemList::Create(gfx::Rect(layer_size), settings); |
| 143 |
| 144 // Build the DrawingDisplayItem. |
| 145 AppendFirstSerializationTestPicture(list, layer_size); |
| 146 |
| 147 // Build the ClipDisplayItem. |
| 148 gfx::Rect clip_rect(6, 6, 1, 1); |
| 149 std::vector<SkRRect> rrects; |
| 150 rrects.push_back(SkRRect::MakeOval(SkRect::MakeXYWH(5.f, 5.f, 4.f, 4.f))); |
| 151 auto* item = list->CreateAndAppendItem<ClipDisplayItem>(); |
| 152 item->SetNew(clip_rect, rrects); |
| 153 |
| 154 // Build the second DrawingDisplayItem. |
| 155 AppendSecondSerializationTestPicture(list, layer_size); |
| 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 |
| 166 DisplayItemListSettings settings; |
| 167 settings.use_cached_picture = true; |
| 168 scoped_refptr<DisplayItemList> list = |
| 169 DisplayItemList::Create(gfx::Rect(layer_size), settings); |
| 170 |
| 171 // Build the DrawingDisplayItem. |
| 172 AppendFirstSerializationTestPicture(list, layer_size); |
| 173 |
| 174 // Build the ClipPathDisplayItem. |
| 175 SkPath path; |
| 176 path.addCircle(5.f, 5.f, 2.f, SkPath::Direction::kCW_Direction); |
| 177 auto* item = list->CreateAndAppendItem<ClipPathDisplayItem>(); |
| 178 item->SetNew(path, SkRegion::Op::kReplace_Op, false); |
| 179 |
| 180 // Build the second DrawingDisplayItem. |
| 181 AppendSecondSerializationTestPicture(list, layer_size); |
| 182 |
| 183 // Build the EndClipPathDisplayItem. |
| 184 list->CreateAndAppendItem<EndClipPathDisplayItem>(); |
| 185 |
| 186 ValidateDisplayItemListSerialization(layer_size, list); |
| 187 } |
| 188 |
| 189 TEST(DisplayItemListTest, SerializeCompositingItem) { |
| 190 gfx::Size layer_size(10, 10); |
| 191 |
| 192 DisplayItemListSettings settings; |
| 193 settings.use_cached_picture = true; |
| 194 scoped_refptr<DisplayItemList> list = |
| 195 DisplayItemList::Create(gfx::Rect(layer_size), settings); |
| 196 |
| 197 // Build the DrawingDisplayItem. |
| 198 AppendFirstSerializationTestPicture(list, layer_size); |
| 199 |
| 200 // Build the CompositingDisplayItem. |
| 201 skia::RefPtr<SkColorFilter> filter = skia::AdoptRef( |
| 202 SkColorFilter::CreateLightingFilter(SK_ColorRED, SK_ColorGREEN)); |
| 203 auto* item = list->CreateAndAppendItem<CompositingDisplayItem>(); |
| 204 item->SetNew(150, SkXfermode::Mode::kDst_Mode, nullptr, filter); |
| 205 |
| 206 // Build the second DrawingDisplayItem. |
| 207 AppendSecondSerializationTestPicture(list, layer_size); |
| 208 |
| 209 // Build the EndCompositingDisplayItem. |
| 210 list->CreateAndAppendItem<EndCompositingDisplayItem>(); |
| 211 |
| 212 ValidateDisplayItemListSerialization(layer_size, list); |
| 213 } |
| 214 |
| 215 TEST(DisplayItemListTest, SerializeFloatClipItem) { |
| 216 gfx::Size layer_size(10, 10); |
| 217 |
| 218 DisplayItemListSettings settings; |
| 219 settings.use_cached_picture = true; |
| 220 scoped_refptr<DisplayItemList> list = |
| 221 DisplayItemList::Create(gfx::Rect(layer_size), settings); |
| 222 |
| 223 // Build the DrawingDisplayItem. |
| 224 AppendFirstSerializationTestPicture(list, layer_size); |
| 225 |
| 226 // Build the FloatClipDisplayItem. |
| 227 gfx::RectF clip_rect(6.f, 6.f, 1.f, 1.f); |
| 228 auto* item2 = list->CreateAndAppendItem<FloatClipDisplayItem>(); |
| 229 item2->SetNew(clip_rect); |
| 230 |
| 231 // Build the second DrawingDisplayItem. |
| 232 AppendSecondSerializationTestPicture(list, layer_size); |
| 233 |
| 234 // Build the EndFloatClipDisplayItem. |
| 235 list->CreateAndAppendItem<EndFloatClipDisplayItem>(); |
| 236 |
| 237 ValidateDisplayItemListSerialization(layer_size, list); |
| 238 } |
| 239 |
| 240 TEST(DisplayItemListTest, SerializeTransformItem) { |
| 241 gfx::Size layer_size(10, 10); |
| 242 |
| 243 DisplayItemListSettings settings; |
| 244 settings.use_cached_picture = true; |
| 245 scoped_refptr<DisplayItemList> list = |
| 246 DisplayItemList::Create(gfx::Rect(layer_size), settings); |
| 247 |
| 248 // Build the DrawingDisplayItem. |
| 249 AppendFirstSerializationTestPicture(list, layer_size); |
| 250 |
| 251 // Build the TransformDisplayItem. |
| 252 gfx::Transform transform; |
| 253 transform.Scale(1.25f, 1.25f); |
| 254 transform.Translate(-1.f, -1.f); |
| 255 auto* item2 = list->CreateAndAppendItem<TransformDisplayItem>(); |
| 256 item2->SetNew(transform); |
| 257 |
| 258 // Build the second DrawingDisplayItem. |
| 259 AppendSecondSerializationTestPicture(list, layer_size); |
| 260 |
| 261 // Build the EndTransformDisplayItem. |
| 262 list->CreateAndAppendItem<EndTransformDisplayItem>(); |
| 263 |
| 264 ValidateDisplayItemListSerialization(layer_size, list); |
| 265 } |
| 266 |
31 TEST(DisplayItemListTest, SingleDrawingItem) { | 267 TEST(DisplayItemListTest, SingleDrawingItem) { |
32 gfx::Rect layer_rect(100, 100); | 268 gfx::Rect layer_rect(100, 100); |
33 SkPictureRecorder recorder; | 269 SkPictureRecorder recorder; |
34 skia::RefPtr<SkCanvas> canvas; | 270 skia::RefPtr<SkCanvas> canvas; |
35 skia::RefPtr<SkPicture> picture; | 271 skia::RefPtr<SkPicture> picture; |
36 SkPaint blue_paint; | 272 SkPaint blue_paint; |
37 blue_paint.setColor(SK_ColorBLUE); | 273 blue_paint.setColor(SK_ColorBLUE); |
38 SkPaint red_paint; | 274 SkPaint red_paint; |
39 red_paint.setColor(SK_ColorRED); | 275 red_paint.setColor(SK_ColorRED); |
40 unsigned char pixels[4 * 100 * 100] = {0}; | 276 unsigned char pixels[4 * 100 * 100] = {0}; |
(...skipping 428 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
469 // categories being traced). | 705 // categories being traced). |
470 list = new DisplayItemList(layer_rect, caching_settings, true); | 706 list = new DisplayItemList(layer_rect, caching_settings, true); |
471 item = list->CreateAndAppendItem<DrawingDisplayItem>(); | 707 item = list->CreateAndAppendItem<DrawingDisplayItem>(); |
472 item->SetNew(picture); | 708 item->SetNew(picture); |
473 list->Finalize(); | 709 list->Finalize(); |
474 memory_usage = list->ApproximateMemoryUsage(); | 710 memory_usage = list->ApproximateMemoryUsage(); |
475 EXPECT_EQ(static_cast<size_t>(0), memory_usage); | 711 EXPECT_EQ(static_cast<size_t>(0), memory_usage); |
476 } | 712 } |
477 | 713 |
478 } // namespace cc | 714 } // namespace cc |
OLD | NEW |