Re-land: C++ readability review
[chromium-blink-merge.git] / cc / resources / display_item_list_unittest.cc
blobc9b95582934e07f2da1458a9615448ae5cf62bc8
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"
7 #include <vector>
9 #include "cc/output/filter_operation.h"
10 #include "cc/output/filter_operations.h"
11 #include "cc/resources/clip_display_item.h"
12 #include "cc/resources/drawing_display_item.h"
13 #include "cc/resources/filter_display_item.h"
14 #include "cc/resources/transform_display_item.h"
15 #include "cc/test/skia_common.h"
16 #include "skia/ext/refptr.h"
17 #include "testing/gtest/include/gtest/gtest.h"
18 #include "third_party/skia/include/core/SkBitmap.h"
19 #include "third_party/skia/include/core/SkCanvas.h"
20 #include "third_party/skia/include/core/SkColor.h"
21 #include "third_party/skia/include/core/SkPictureRecorder.h"
22 #include "third_party/skia/include/effects/SkBitmapSource.h"
23 #include "ui/gfx/skia_util.h"
25 namespace cc {
26 namespace {
28 TEST(DisplayItemListTest, SingleDrawingItem) {
29 gfx::Rect layer_rect(100, 100);
30 SkPictureRecorder recorder;
31 skia::RefPtr<SkCanvas> canvas;
32 skia::RefPtr<SkPicture> picture;
33 SkPaint blue_paint;
34 blue_paint.setColor(SK_ColorBLUE);
35 SkPaint red_paint;
36 red_paint.setColor(SK_ColorRED);
37 unsigned char pixels[4 * 100 * 100] = {0};
38 scoped_refptr<DisplayItemList> list = DisplayItemList::Create();
40 gfx::PointF offset(8.f, 9.f);
41 gfx::RectF recording_rect(offset, layer_rect.size());
42 canvas = skia::SharePtr(
43 recorder.beginRecording(gfx::RectFToSkRect(recording_rect)));
44 canvas->translate(offset.x(), offset.y());
45 canvas->drawRectCoords(0.f, 0.f, 60.f, 60.f, red_paint);
46 canvas->drawRectCoords(50.f, 50.f, 75.f, 75.f, blue_paint);
47 picture = skia::AdoptRef(recorder.endRecording());
48 list->AppendItem(DrawingDisplayItem::Create(picture));
49 DrawDisplayList(pixels, layer_rect, list);
51 SkBitmap expected_bitmap;
52 unsigned char expected_pixels[4 * 100 * 100] = {0};
53 SkImageInfo info =
54 SkImageInfo::MakeN32Premul(layer_rect.width(), layer_rect.height());
55 expected_bitmap.installPixels(info, expected_pixels, info.minRowBytes());
56 SkCanvas expected_canvas(expected_bitmap);
57 expected_canvas.clipRect(gfx::RectToSkRect(layer_rect));
58 expected_canvas.drawRectCoords(0.f + offset.x(), 0.f + offset.y(),
59 60.f + offset.x(), 60.f + offset.y(),
60 red_paint);
61 expected_canvas.drawRectCoords(50.f + offset.x(), 50.f + offset.y(),
62 75.f + offset.x(), 75.f + offset.y(),
63 blue_paint);
65 EXPECT_EQ(0, memcmp(pixels, expected_pixels, 4 * 100 * 100));
68 TEST(DisplayItemListTest, ClipItem) {
69 gfx::Rect layer_rect(100, 100);
70 SkPictureRecorder recorder;
71 skia::RefPtr<SkCanvas> canvas;
72 skia::RefPtr<SkPicture> picture;
73 SkPaint blue_paint;
74 blue_paint.setColor(SK_ColorBLUE);
75 SkPaint red_paint;
76 red_paint.setColor(SK_ColorRED);
77 unsigned char pixels[4 * 100 * 100] = {0};
78 scoped_refptr<DisplayItemList> list = DisplayItemList::Create();
80 gfx::PointF first_offset(8.f, 9.f);
81 gfx::RectF first_recording_rect(first_offset, layer_rect.size());
82 canvas = skia::SharePtr(
83 recorder.beginRecording(gfx::RectFToSkRect(first_recording_rect)));
84 canvas->translate(first_offset.x(), first_offset.y());
85 canvas->drawRectCoords(0.f, 0.f, 60.f, 60.f, red_paint);
86 picture = skia::AdoptRef(recorder.endRecording());
87 list->AppendItem(DrawingDisplayItem::Create(picture));
89 gfx::Rect clip_rect(60, 60, 10, 10);
90 list->AppendItem(ClipDisplayItem::Create(clip_rect, std::vector<SkRRect>()));
92 gfx::PointF second_offset(2.f, 3.f);
93 gfx::RectF second_recording_rect(second_offset, layer_rect.size());
94 canvas = skia::SharePtr(
95 recorder.beginRecording(gfx::RectFToSkRect(second_recording_rect)));
96 canvas->translate(second_offset.x(), second_offset.y());
97 canvas->drawRectCoords(50.f, 50.f, 75.f, 75.f, blue_paint);
98 picture = skia::AdoptRef(recorder.endRecording());
99 list->AppendItem(DrawingDisplayItem::Create(picture));
101 list->AppendItem(EndClipDisplayItem::Create());
103 DrawDisplayList(pixels, layer_rect, list);
105 SkBitmap expected_bitmap;
106 unsigned char expected_pixels[4 * 100 * 100] = {0};
107 SkImageInfo info =
108 SkImageInfo::MakeN32Premul(layer_rect.width(), layer_rect.height());
109 expected_bitmap.installPixels(info, expected_pixels, info.minRowBytes());
110 SkCanvas expected_canvas(expected_bitmap);
111 expected_canvas.clipRect(gfx::RectToSkRect(layer_rect));
112 expected_canvas.drawRectCoords(0.f + first_offset.x(), 0.f + first_offset.y(),
113 60.f + first_offset.x(),
114 60.f + first_offset.y(), red_paint);
115 expected_canvas.clipRect(gfx::RectToSkRect(clip_rect));
116 expected_canvas.drawRectCoords(
117 50.f + second_offset.x(), 50.f + second_offset.y(),
118 75.f + second_offset.x(), 75.f + second_offset.y(), blue_paint);
120 EXPECT_EQ(0, memcmp(pixels, expected_pixels, 4 * 100 * 100));
123 TEST(DisplayItemListTest, TransformItem) {
124 gfx::Rect layer_rect(100, 100);
125 SkPictureRecorder recorder;
126 skia::RefPtr<SkCanvas> canvas;
127 skia::RefPtr<SkPicture> picture;
128 SkPaint blue_paint;
129 blue_paint.setColor(SK_ColorBLUE);
130 SkPaint red_paint;
131 red_paint.setColor(SK_ColorRED);
132 unsigned char pixels[4 * 100 * 100] = {0};
133 scoped_refptr<DisplayItemList> list = DisplayItemList::Create();
135 gfx::PointF first_offset(8.f, 9.f);
136 gfx::RectF first_recording_rect(first_offset, layer_rect.size());
137 canvas = skia::SharePtr(
138 recorder.beginRecording(gfx::RectFToSkRect(first_recording_rect)));
139 canvas->translate(first_offset.x(), first_offset.y());
140 canvas->drawRectCoords(0.f, 0.f, 60.f, 60.f, red_paint);
141 picture = skia::AdoptRef(recorder.endRecording());
142 list->AppendItem(DrawingDisplayItem::Create(picture));
144 gfx::Transform transform;
145 transform.Rotate(45.0);
146 list->AppendItem(TransformDisplayItem::Create(transform));
148 gfx::PointF second_offset(2.f, 3.f);
149 gfx::RectF second_recording_rect(second_offset, layer_rect.size());
150 canvas = skia::SharePtr(
151 recorder.beginRecording(gfx::RectFToSkRect(second_recording_rect)));
152 canvas->translate(second_offset.x(), second_offset.y());
153 canvas->drawRectCoords(50.f, 50.f, 75.f, 75.f, blue_paint);
154 picture = skia::AdoptRef(recorder.endRecording());
155 list->AppendItem(DrawingDisplayItem::Create(picture));
157 list->AppendItem(EndTransformDisplayItem::Create());
159 DrawDisplayList(pixels, layer_rect, list);
161 SkBitmap expected_bitmap;
162 unsigned char expected_pixels[4 * 100 * 100] = {0};
163 SkImageInfo info =
164 SkImageInfo::MakeN32Premul(layer_rect.width(), layer_rect.height());
165 expected_bitmap.installPixels(info, expected_pixels, info.minRowBytes());
166 SkCanvas expected_canvas(expected_bitmap);
167 expected_canvas.clipRect(gfx::RectToSkRect(layer_rect));
168 expected_canvas.drawRectCoords(0.f + first_offset.x(), 0.f + first_offset.y(),
169 60.f + first_offset.x(),
170 60.f + first_offset.y(), red_paint);
171 expected_canvas.setMatrix(transform.matrix());
172 expected_canvas.drawRectCoords(
173 50.f + second_offset.x(), 50.f + second_offset.y(),
174 75.f + second_offset.x(), 75.f + second_offset.y(), blue_paint);
176 EXPECT_EQ(0, memcmp(pixels, expected_pixels, 4 * 100 * 100));
179 TEST(DisplayItemList, FilterItem) {
180 gfx::Rect layer_rect(100, 100);
181 FilterOperations filters;
182 unsigned char pixels[4 * 100 * 100] = {0};
183 scoped_refptr<DisplayItemList> list = DisplayItemList::Create();
185 SkBitmap source_bitmap;
186 source_bitmap.allocN32Pixels(50, 50);
187 SkCanvas source_canvas(source_bitmap);
188 source_canvas.clear(SkColorSetRGB(128, 128, 128));
190 // For most SkImageFilters, the |dst| bounds computed by computeFastBounds are
191 // dependent on the provided |src| bounds. This means, for example, that
192 // translating |src| results in a corresponding translation of |dst|. But this
193 // is not the case for all SkImageFilters; for some of them (e.g.
194 // SkBitmapSource), the computation of |dst| in computeFastBounds doesn't
195 // involve |src| at all. Incorrectly assuming such a relationship (e.g. by
196 // translating |dst| after it is computed by computeFastBounds, rather than
197 // translating |src| before it provided to computedFastBounds) can cause
198 // incorrect clipping of filter output. To test for this, we include an
199 // SkBitmapSource filter in |filters|. Here, |src| is |filter_bounds|, defined
200 // below.
201 skia::RefPtr<SkImageFilter> image_filter =
202 skia::AdoptRef(SkBitmapSource::Create(source_bitmap));
203 filters.Append(FilterOperation::CreateReferenceFilter(image_filter));
204 filters.Append(FilterOperation::CreateBrightnessFilter(0.5f));
205 gfx::RectF filter_bounds(10.f, 10.f, 50.f, 50.f);
206 list->AppendItem(FilterDisplayItem::Create(filters, filter_bounds));
207 list->AppendItem(EndFilterDisplayItem::Create());
209 DrawDisplayList(pixels, layer_rect, list);
211 SkBitmap expected_bitmap;
212 unsigned char expected_pixels[4 * 100 * 100] = {0};
213 SkPaint paint;
214 paint.setColor(SkColorSetRGB(64, 64, 64));
215 SkImageInfo info =
216 SkImageInfo::MakeN32Premul(layer_rect.width(), layer_rect.height());
217 expected_bitmap.installPixels(info, expected_pixels, info.minRowBytes());
218 SkCanvas expected_canvas(expected_bitmap);
219 expected_canvas.drawRect(RectFToSkRect(filter_bounds), paint);
221 EXPECT_EQ(0, memcmp(pixels, expected_pixels, 4 * 100 * 100));
224 } // namespace
225 } // namespace cc