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
3 // found in the LICENSE file.
5 #include "cc/resources/display_item_list.h"
9 #include "cc/resources/clip_display_item.h"
10 #include "cc/resources/drawing_display_item.h"
11 #include "cc/resources/transform_display_item.h"
12 #include "cc/test/skia_common.h"
13 #include "skia/ext/refptr.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15 #include "third_party/skia/include/core/SkBitmap.h"
16 #include "third_party/skia/include/core/SkCanvas.h"
17 #include "third_party/skia/include/core/SkColor.h"
18 #include "third_party/skia/include/core/SkPictureRecorder.h"
19 #include "ui/gfx/skia_util.h"
24 TEST(DisplayItemListTest
, SingleDrawingItem
) {
25 gfx::Rect
layer_rect(100, 100);
26 SkPictureRecorder recorder
;
27 skia::RefPtr
<SkCanvas
> canvas
;
28 skia::RefPtr
<SkPicture
> picture
;
30 blue_paint
.setColor(SK_ColorBLUE
);
32 red_paint
.setColor(SK_ColorRED
);
33 unsigned char pixels
[4 * 100 * 100] = {0};
34 scoped_refptr
<DisplayItemList
> list
= DisplayItemList::Create();
36 canvas
= skia::SharePtr(
37 recorder
.beginRecording(layer_rect
.width(), layer_rect
.height()));
38 canvas
->drawRectCoords(0.f
, 0.f
, 60.f
, 60.f
, red_paint
);
39 canvas
->drawRectCoords(50.f
, 50.f
, 75.f
, 75.f
, blue_paint
);
40 picture
= skia::AdoptRef(recorder
.endRecording());
41 gfx::PointF
offset(8.f
, 9.f
);
42 list
->AppendItem(DrawingDisplayItem::Create(picture
, offset
));
43 DrawDisplayList(pixels
, layer_rect
, list
);
45 SkBitmap expected_bitmap
;
46 unsigned char expected_pixels
[4 * 100 * 100] = {0};
48 SkImageInfo::MakeN32Premul(layer_rect
.width(), layer_rect
.height());
49 expected_bitmap
.installPixels(info
, expected_pixels
, info
.minRowBytes());
50 SkCanvas
expected_canvas(expected_bitmap
);
51 expected_canvas
.clipRect(gfx::RectToSkRect(layer_rect
));
52 expected_canvas
.drawRectCoords(0.f
+ offset
.x(), 0.f
+ offset
.y(),
53 60.f
+ offset
.x(), 60.f
+ offset
.y(),
55 expected_canvas
.drawRectCoords(50.f
+ offset
.x(), 50.f
+ offset
.y(),
56 75.f
+ offset
.x(), 75.f
+ offset
.y(),
59 EXPECT_EQ(0, memcmp(pixels
, expected_pixels
, 4 * 100 * 100));
62 TEST(DisplayItemListTest
, ClipItem
) {
63 gfx::Rect
layer_rect(100, 100);
64 SkPictureRecorder recorder
;
65 skia::RefPtr
<SkCanvas
> canvas
;
66 skia::RefPtr
<SkPicture
> picture
;
68 blue_paint
.setColor(SK_ColorBLUE
);
70 red_paint
.setColor(SK_ColorRED
);
71 unsigned char pixels
[4 * 100 * 100] = {0};
72 scoped_refptr
<DisplayItemList
> list
= DisplayItemList::Create();
74 canvas
= skia::SharePtr(
75 recorder
.beginRecording(layer_rect
.width(), layer_rect
.height()));
76 canvas
->drawRectCoords(0.f
, 0.f
, 60.f
, 60.f
, red_paint
);
77 picture
= skia::AdoptRef(recorder
.endRecording());
78 gfx::PointF
first_offset(8.f
, 9.f
);
79 list
->AppendItem(DrawingDisplayItem::Create(picture
, first_offset
));
81 gfx::Rect
clip_rect(60, 60, 10, 10);
82 list
->AppendItem(ClipDisplayItem::Create(clip_rect
, std::vector
<SkRRect
>()));
84 canvas
= skia::SharePtr(
85 recorder
.beginRecording(layer_rect
.width(), layer_rect
.height()));
86 canvas
->drawRectCoords(50.f
, 50.f
, 75.f
, 75.f
, blue_paint
);
87 picture
= skia::AdoptRef(recorder
.endRecording());
88 gfx::PointF
second_offset(2.f
, 3.f
);
89 list
->AppendItem(DrawingDisplayItem::Create(picture
, second_offset
));
91 list
->AppendItem(EndClipDisplayItem::Create());
93 DrawDisplayList(pixels
, layer_rect
, list
);
95 SkBitmap expected_bitmap
;
96 unsigned char expected_pixels
[4 * 100 * 100] = {0};
98 SkImageInfo::MakeN32Premul(layer_rect
.width(), layer_rect
.height());
99 expected_bitmap
.installPixels(info
, expected_pixels
, info
.minRowBytes());
100 SkCanvas
expected_canvas(expected_bitmap
);
101 expected_canvas
.clipRect(gfx::RectToSkRect(layer_rect
));
102 expected_canvas
.drawRectCoords(0.f
+ first_offset
.x(), 0.f
+ first_offset
.y(),
103 60.f
+ first_offset
.x(),
104 60.f
+ first_offset
.y(), red_paint
);
105 expected_canvas
.clipRect(gfx::RectToSkRect(clip_rect
));
106 expected_canvas
.drawRectCoords(
107 50.f
+ second_offset
.x(), 50.f
+ second_offset
.y(),
108 75.f
+ second_offset
.x(), 75.f
+ second_offset
.y(), blue_paint
);
110 EXPECT_EQ(0, memcmp(pixels
, expected_pixels
, 4 * 100 * 100));
113 TEST(DisplayItemListTest
, TransformItem
) {
114 gfx::Rect
layer_rect(100, 100);
115 SkPictureRecorder recorder
;
116 skia::RefPtr
<SkCanvas
> canvas
;
117 skia::RefPtr
<SkPicture
> picture
;
119 blue_paint
.setColor(SK_ColorBLUE
);
121 red_paint
.setColor(SK_ColorRED
);
122 unsigned char pixels
[4 * 100 * 100] = {0};
123 scoped_refptr
<DisplayItemList
> list
= DisplayItemList::Create();
125 canvas
= skia::SharePtr(
126 recorder
.beginRecording(layer_rect
.width(), layer_rect
.height()));
127 canvas
->drawRectCoords(0.f
, 0.f
, 60.f
, 60.f
, red_paint
);
128 picture
= skia::AdoptRef(recorder
.endRecording());
129 gfx::PointF
first_offset(8.f
, 9.f
);
130 list
->AppendItem(DrawingDisplayItem::Create(picture
, first_offset
));
132 gfx::Transform transform
;
133 transform
.Rotate(45.0);
134 list
->AppendItem(TransformDisplayItem::Create(transform
));
136 canvas
= skia::SharePtr(
137 recorder
.beginRecording(layer_rect
.width(), layer_rect
.height()));
138 canvas
->drawRectCoords(50.f
, 50.f
, 75.f
, 75.f
, blue_paint
);
139 picture
= skia::AdoptRef(recorder
.endRecording());
140 gfx::PointF
second_offset(2.f
, 3.f
);
141 list
->AppendItem(DrawingDisplayItem::Create(picture
, second_offset
));
143 list
->AppendItem(EndTransformDisplayItem::Create());
145 DrawDisplayList(pixels
, layer_rect
, list
);
147 SkBitmap expected_bitmap
;
148 unsigned char expected_pixels
[4 * 100 * 100] = {0};
150 SkImageInfo::MakeN32Premul(layer_rect
.width(), layer_rect
.height());
151 expected_bitmap
.installPixels(info
, expected_pixels
, info
.minRowBytes());
152 SkCanvas
expected_canvas(expected_bitmap
);
153 expected_canvas
.clipRect(gfx::RectToSkRect(layer_rect
));
154 expected_canvas
.drawRectCoords(0.f
+ first_offset
.x(), 0.f
+ first_offset
.y(),
155 60.f
+ first_offset
.x(),
156 60.f
+ first_offset
.y(), red_paint
);
157 expected_canvas
.setMatrix(transform
.matrix());
158 expected_canvas
.drawRectCoords(
159 50.f
+ second_offset
.x(), 50.f
+ second_offset
.y(),
160 75.f
+ second_offset
.x(), 75.f
+ second_offset
.y(), blue_paint
);
162 EXPECT_EQ(0, memcmp(pixels
, expected_pixels
, 4 * 100 * 100));