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