Mac Overlays: Allow multiple overlays with sandwich strategy.
[chromium-blink-merge.git] / cc / playback / recording_source_unittest.cc
blobe0e602e0a9ebefed9b977726ad0b2d97fca38bdf
1 // Copyright 2015 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 <vector>
7 #include "cc/playback/display_list_raster_source.h"
8 #include "cc/test/fake_display_list_recording_source.h"
9 #include "cc/test/fake_picture_pile.h"
10 #include "cc/test/fake_picture_pile_impl.h"
11 #include "cc/test/skia_common.h"
12 #include "testing/gtest/include/gtest/gtest.h"
14 namespace cc {
15 namespace {
17 template <class T>
18 scoped_ptr<T> CreateRecordingSource(const gfx::Rect& viewport,
19 const gfx::Size& grid_cell_size);
21 template <>
22 scoped_ptr<FakePicturePile> CreateRecordingSource<FakePicturePile>(
23 const gfx::Rect& viewport,
24 const gfx::Size& grid_cell_size) {
25 return FakePicturePile::CreateFilledPile(grid_cell_size, viewport.size());
28 template <>
29 scoped_ptr<FakeDisplayListRecordingSource> CreateRecordingSource<
30 FakeDisplayListRecordingSource>(const gfx::Rect& viewport,
31 const gfx::Size& grid_cell_size) {
32 gfx::Rect layer_rect(viewport.right(), viewport.bottom());
33 scoped_ptr<FakeDisplayListRecordingSource> recording_source =
34 FakeDisplayListRecordingSource::CreateRecordingSource(viewport,
35 layer_rect.size());
36 recording_source->SetGridCellSize(grid_cell_size);
38 return recording_source.Pass();
41 template <class T>
42 scoped_refptr<RasterSource> CreateRasterSource(T* recording_source);
44 template <>
45 scoped_refptr<RasterSource> CreateRasterSource(
46 FakePicturePile* recording_source) {
47 return FakePicturePileImpl::CreateFromPile(recording_source, nullptr);
50 template <>
51 scoped_refptr<RasterSource> CreateRasterSource(
52 FakeDisplayListRecordingSource* recording_source) {
53 bool can_use_lcd_text = true;
54 return DisplayListRasterSource::CreateFromDisplayListRecordingSource(
55 recording_source, can_use_lcd_text);
58 template <typename T>
59 class RecordingSourceTest : public testing::Test {};
61 using testing::Types;
63 typedef Types<FakePicturePile, FakeDisplayListRecordingSource>
64 RecordingSourceImplementations;
66 TYPED_TEST_CASE(RecordingSourceTest, RecordingSourceImplementations);
68 TYPED_TEST(RecordingSourceTest, NoGatherPixelRefEmptyPixelRefs) {
69 gfx::Size grid_cell_size(128, 128);
70 gfx::Rect recorded_viewport(0, 0, 256, 256);
72 scoped_ptr<TypeParam> recording_source =
73 CreateRecordingSource<TypeParam>(recorded_viewport, grid_cell_size);
74 recording_source->SetGatherPixelRefs(false);
75 recording_source->Rerecord();
77 scoped_refptr<RasterSource> raster_source =
78 CreateRasterSource<TypeParam>(recording_source.get());
80 // If recording source do not gather pixel ref, raster source is not going to
81 // get pixel refs.
83 std::vector<skia::PositionPixelRef> pixel_refs;
84 raster_source->GatherPixelRefs(recorded_viewport, &pixel_refs);
85 EXPECT_TRUE(pixel_refs.empty());
89 TYPED_TEST(RecordingSourceTest, EmptyPixelRefs) {
90 gfx::Size grid_cell_size(128, 128);
91 gfx::Rect recorded_viewport(0, 0, 256, 256);
93 scoped_ptr<TypeParam> recording_source =
94 CreateRecordingSource<TypeParam>(recorded_viewport, grid_cell_size);
95 recording_source->SetGatherPixelRefs(true);
96 recording_source->Rerecord();
98 scoped_refptr<RasterSource> raster_source =
99 CreateRasterSource<TypeParam>(recording_source.get());
101 // Tile sized iterators.
103 std::vector<skia::PositionPixelRef> pixel_refs;
104 raster_source->GatherPixelRefs(gfx::Rect(0, 0, 128, 128), &pixel_refs);
105 EXPECT_TRUE(pixel_refs.empty());
107 // Shifted tile sized iterators.
109 std::vector<skia::PositionPixelRef> pixel_refs;
110 raster_source->GatherPixelRefs(gfx::Rect(140, 140, 128, 128), &pixel_refs);
111 EXPECT_TRUE(pixel_refs.empty());
113 // Layer sized iterators.
115 std::vector<skia::PositionPixelRef> pixel_refs;
116 raster_source->GatherPixelRefs(gfx::Rect(0, 0, 256, 256), &pixel_refs);
117 EXPECT_TRUE(pixel_refs.empty());
121 TYPED_TEST(RecordingSourceTest, NoDiscardablePixelRefs) {
122 gfx::Size grid_cell_size(128, 128);
123 gfx::Rect recorded_viewport(0, 0, 256, 256);
125 scoped_ptr<TypeParam> recording_source =
126 CreateRecordingSource<TypeParam>(recorded_viewport, grid_cell_size);
128 SkPaint simple_paint;
129 simple_paint.setColor(SkColorSetARGB(255, 12, 23, 34));
131 SkBitmap non_discardable_bitmap;
132 non_discardable_bitmap.allocN32Pixels(128, 128);
133 non_discardable_bitmap.setImmutable();
135 recording_source->add_draw_rect_with_paint(gfx::Rect(0, 0, 256, 256),
136 simple_paint);
137 recording_source->add_draw_rect_with_paint(gfx::Rect(128, 128, 512, 512),
138 simple_paint);
139 recording_source->add_draw_rect_with_paint(gfx::Rect(512, 0, 256, 256),
140 simple_paint);
141 recording_source->add_draw_rect_with_paint(gfx::Rect(0, 512, 256, 256),
142 simple_paint);
143 recording_source->add_draw_bitmap(non_discardable_bitmap, gfx::Point(128, 0));
144 recording_source->add_draw_bitmap(non_discardable_bitmap, gfx::Point(0, 128));
145 recording_source->add_draw_bitmap(non_discardable_bitmap,
146 gfx::Point(150, 150));
147 recording_source->SetGatherPixelRefs(true);
148 recording_source->Rerecord();
150 scoped_refptr<RasterSource> raster_source =
151 CreateRasterSource<TypeParam>(recording_source.get());
153 // Tile sized iterators.
155 std::vector<skia::PositionPixelRef> pixel_refs;
156 raster_source->GatherPixelRefs(gfx::Rect(0, 0, 128, 128), &pixel_refs);
157 EXPECT_TRUE(pixel_refs.empty());
159 // Shifted tile sized iterators.
161 std::vector<skia::PositionPixelRef> pixel_refs;
162 raster_source->GatherPixelRefs(gfx::Rect(140, 140, 128, 128), &pixel_refs);
163 EXPECT_TRUE(pixel_refs.empty());
165 // Layer sized iterators.
167 std::vector<skia::PositionPixelRef> pixel_refs;
168 raster_source->GatherPixelRefs(gfx::Rect(0, 0, 256, 256), &pixel_refs);
169 EXPECT_TRUE(pixel_refs.empty());
173 TYPED_TEST(RecordingSourceTest, DiscardablePixelRefs) {
174 gfx::Size grid_cell_size(128, 128);
175 gfx::Rect recorded_viewport(0, 0, 256, 256);
177 scoped_ptr<TypeParam> recording_source =
178 CreateRecordingSource<TypeParam>(recorded_viewport, grid_cell_size);
180 SkBitmap discardable_bitmap[2][2];
181 CreateDiscardableBitmap(gfx::Size(32, 32), &discardable_bitmap[0][0]);
182 CreateDiscardableBitmap(gfx::Size(32, 32), &discardable_bitmap[1][0]);
183 CreateDiscardableBitmap(gfx::Size(32, 32), &discardable_bitmap[1][1]);
185 // Discardable pixel refs are found in the following cells:
186 // |---|---|
187 // | x | |
188 // |---|---|
189 // | x | x |
190 // |---|---|
191 recording_source->add_draw_bitmap(discardable_bitmap[0][0], gfx::Point(0, 0));
192 recording_source->add_draw_bitmap(discardable_bitmap[1][0],
193 gfx::Point(0, 130));
194 recording_source->add_draw_bitmap(discardable_bitmap[1][1],
195 gfx::Point(140, 140));
196 recording_source->SetGatherPixelRefs(true);
197 recording_source->Rerecord();
199 scoped_refptr<RasterSource> raster_source =
200 CreateRasterSource<TypeParam>(recording_source.get());
202 // Tile sized iterators. These should find only one pixel ref.
204 std::vector<skia::PositionPixelRef> pixel_refs;
205 raster_source->GatherPixelRefs(gfx::Rect(0, 0, 128, 128), &pixel_refs);
206 EXPECT_EQ(1u, pixel_refs.size());
207 EXPECT_TRUE(pixel_refs[0].pixel_ref == discardable_bitmap[0][0].pixelRef());
208 EXPECT_EQ(gfx::RectF(32, 32).ToString(),
209 gfx::SkRectToRectF(pixel_refs[0].pixel_ref_rect).ToString());
212 // Shifted tile sized iterators. These should find only one pixel ref.
214 std::vector<skia::PositionPixelRef> pixel_refs;
215 raster_source->GatherPixelRefs(gfx::Rect(140, 140, 128, 128), &pixel_refs);
216 EXPECT_EQ(1u, pixel_refs.size());
217 EXPECT_TRUE(pixel_refs[0].pixel_ref == discardable_bitmap[1][1].pixelRef());
218 EXPECT_EQ(gfx::RectF(140, 140, 32, 32).ToString(),
219 gfx::SkRectToRectF(pixel_refs[0].pixel_ref_rect).ToString());
222 // Ensure there's no discardable pixel refs in the empty cell
224 std::vector<skia::PositionPixelRef> pixel_refs;
225 raster_source->GatherPixelRefs(gfx::Rect(140, 0, 128, 128), &pixel_refs);
226 EXPECT_TRUE(pixel_refs.empty());
229 // Layer sized iterators. These should find all 3 pixel refs.
231 std::vector<skia::PositionPixelRef> pixel_refs;
232 raster_source->GatherPixelRefs(gfx::Rect(0, 0, 256, 256), &pixel_refs);
233 EXPECT_EQ(3u, pixel_refs.size());
234 EXPECT_TRUE(pixel_refs[0].pixel_ref == discardable_bitmap[0][0].pixelRef());
235 EXPECT_TRUE(pixel_refs[1].pixel_ref == discardable_bitmap[1][0].pixelRef());
236 EXPECT_TRUE(pixel_refs[2].pixel_ref == discardable_bitmap[1][1].pixelRef());
237 EXPECT_EQ(gfx::RectF(32, 32).ToString(),
238 gfx::SkRectToRectF(pixel_refs[0].pixel_ref_rect).ToString());
239 EXPECT_EQ(gfx::RectF(0, 130, 32, 32).ToString(),
240 gfx::SkRectToRectF(pixel_refs[1].pixel_ref_rect).ToString());
241 EXPECT_EQ(gfx::RectF(140, 140, 32, 32).ToString(),
242 gfx::SkRectToRectF(pixel_refs[2].pixel_ref_rect).ToString());
246 TYPED_TEST(RecordingSourceTest, DiscardablePixelRefsBaseNonDiscardable) {
247 gfx::Size grid_cell_size(256, 256);
248 gfx::Rect recorded_viewport(0, 0, 512, 512);
250 scoped_ptr<TypeParam> recording_source =
251 CreateRecordingSource<TypeParam>(recorded_viewport, grid_cell_size);
253 SkBitmap non_discardable_bitmap;
254 non_discardable_bitmap.allocN32Pixels(512, 512);
255 non_discardable_bitmap.setImmutable();
257 SkBitmap discardable_bitmap[2][2];
258 CreateDiscardableBitmap(gfx::Size(128, 128), &discardable_bitmap[0][0]);
259 CreateDiscardableBitmap(gfx::Size(128, 128), &discardable_bitmap[0][1]);
260 CreateDiscardableBitmap(gfx::Size(128, 128), &discardable_bitmap[1][1]);
262 // One large non-discardable bitmap covers the whole grid.
263 // Discardable pixel refs are found in the following cells:
264 // |---|---|
265 // | x | x |
266 // |---|---|
267 // | | x |
268 // |---|---|
269 recording_source->add_draw_bitmap(non_discardable_bitmap, gfx::Point(0, 0));
270 recording_source->add_draw_bitmap(discardable_bitmap[0][0], gfx::Point(0, 0));
271 recording_source->add_draw_bitmap(discardable_bitmap[0][1],
272 gfx::Point(260, 0));
273 recording_source->add_draw_bitmap(discardable_bitmap[1][1],
274 gfx::Point(260, 260));
275 recording_source->SetGatherPixelRefs(true);
276 recording_source->Rerecord();
278 scoped_refptr<RasterSource> raster_source =
279 CreateRasterSource<TypeParam>(recording_source.get());
281 // Tile sized iterators. These should find only one pixel ref.
283 std::vector<skia::PositionPixelRef> pixel_refs;
284 raster_source->GatherPixelRefs(gfx::Rect(0, 0, 256, 256), &pixel_refs);
285 EXPECT_EQ(1u, pixel_refs.size());
286 EXPECT_TRUE(pixel_refs[0].pixel_ref == discardable_bitmap[0][0].pixelRef());
287 EXPECT_EQ(gfx::RectF(128, 128).ToString(),
288 gfx::SkRectToRectF(pixel_refs[0].pixel_ref_rect).ToString());
290 // Shifted tile sized iterators. These should find only one pixel ref.
292 std::vector<skia::PositionPixelRef> pixel_refs;
293 raster_source->GatherPixelRefs(gfx::Rect(260, 260, 256, 256), &pixel_refs);
294 EXPECT_EQ(1u, pixel_refs.size());
295 EXPECT_TRUE(pixel_refs[0].pixel_ref == discardable_bitmap[1][1].pixelRef());
296 EXPECT_EQ(gfx::RectF(260, 260, 128, 128).ToString(),
297 gfx::SkRectToRectF(pixel_refs[0].pixel_ref_rect).ToString());
299 // Ensure there's no discardable pixel refs in the empty cell
301 std::vector<skia::PositionPixelRef> pixel_refs;
302 raster_source->GatherPixelRefs(gfx::Rect(0, 256, 256, 256), &pixel_refs);
303 EXPECT_TRUE(pixel_refs.empty());
305 // Layer sized iterators. These should find three pixel ref.
307 std::vector<skia::PositionPixelRef> pixel_refs;
308 raster_source->GatherPixelRefs(gfx::Rect(0, 0, 512, 512), &pixel_refs);
309 EXPECT_EQ(3u, pixel_refs.size());
310 EXPECT_TRUE(pixel_refs[0].pixel_ref == discardable_bitmap[0][0].pixelRef());
311 EXPECT_TRUE(pixel_refs[1].pixel_ref == discardable_bitmap[0][1].pixelRef());
312 EXPECT_TRUE(pixel_refs[2].pixel_ref == discardable_bitmap[1][1].pixelRef());
313 EXPECT_EQ(gfx::RectF(128, 128).ToString(),
314 gfx::SkRectToRectF(pixel_refs[0].pixel_ref_rect).ToString());
315 EXPECT_EQ(gfx::RectF(260, 0, 128, 128).ToString(),
316 gfx::SkRectToRectF(pixel_refs[1].pixel_ref_rect).ToString());
317 EXPECT_EQ(gfx::RectF(260, 260, 128, 128).ToString(),
318 gfx::SkRectToRectF(pixel_refs[2].pixel_ref_rect).ToString());
322 } // namespace
323 } // namespace cc