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/quads/render_pass.h"
6 #include "cc/quads/solid_color_draw_quad.h"
7 #include "cc/quads/surface_draw_quad.h"
8 #include "cc/quads/texture_draw_quad.h"
9 #include "cc/resources/resource_provider.h"
10 #include "gpu/command_buffer/common/mailbox.h"
11 #include "gpu/command_buffer/common/mailbox_holder.h"
12 #include "mojo/converters/geometry/geometry_type_converters.h"
13 #include "mojo/converters/surfaces/surfaces_type_converters.h"
14 #include "mojo/converters/transform/transform_type_converters.h"
15 #include "testing/gtest/include/gtest/gtest.h"
16 #include "third_party/skia/include/core/SkColor.h"
17 #include "third_party/skia/include/core/SkXfermode.h"
22 TEST(SurfaceLibTest
, SurfaceIdConverterNullId
) {
23 cc::SurfaceId null_id
;
24 cc::SurfaceId round_trip
= SurfaceId::From(null_id
).To
<cc::SurfaceId
>();
25 EXPECT_TRUE(round_trip
.is_null());
28 TEST(SurfaceLibTest
, SurfaceIdConverterValidId
) {
29 cc::SurfaceId
valid_id(7);
30 cc::SurfaceId round_trip
= SurfaceId::From(valid_id
).To
<cc::SurfaceId
>();
31 EXPECT_FALSE(round_trip
.is_null());
32 EXPECT_EQ(valid_id
, round_trip
);
35 TEST(SurfaceLibTest
, Color
) {
36 SkColor arbitrary_color
= SK_ColorMAGENTA
;
37 SkColor round_trip
= Color::From(arbitrary_color
).To
<SkColor
>();
38 EXPECT_EQ(arbitrary_color
, round_trip
);
41 class SurfaceLibQuadTest
: public testing::Test
{
46 visible_rect(9, 11, 5, 7),
47 needs_blending(false) {
48 pass
= cc::RenderPass::Create();
49 sqs
= pass
->CreateAndAppendSharedQuadState();
54 gfx::Rect opaque_rect
;
55 gfx::Rect visible_rect
;
57 scoped_ptr
<cc::RenderPass
> pass
;
58 cc::SharedQuadState
* sqs
;
61 TEST_F(SurfaceLibQuadTest
, ColorQuad
) {
62 cc::SolidColorDrawQuad
* color_quad
=
63 pass
->CreateAndAppendDrawQuad
<cc::SolidColorDrawQuad
>();
64 SkColor arbitrary_color
= SK_ColorGREEN
;
65 bool force_anti_aliasing_off
= true;
66 color_quad
->SetAll(sqs
,
72 force_anti_aliasing_off
);
74 QuadPtr mojo_quad
= Quad::From
<cc::DrawQuad
>(*color_quad
);
75 ASSERT_FALSE(mojo_quad
.is_null());
76 EXPECT_EQ(MATERIAL_SOLID_COLOR
, mojo_quad
->material
);
77 EXPECT_EQ(Rect::From(rect
), mojo_quad
->rect
);
78 EXPECT_EQ(Rect::From(opaque_rect
), mojo_quad
->opaque_rect
);
79 EXPECT_EQ(Rect::From(visible_rect
), mojo_quad
->visible_rect
);
80 EXPECT_EQ(needs_blending
, mojo_quad
->needs_blending
);
81 ASSERT_TRUE(mojo_quad
->solid_color_quad_state
);
82 SolidColorQuadStatePtr
& mojo_color_state
= mojo_quad
->solid_color_quad_state
;
83 EXPECT_EQ(Color::From(arbitrary_color
), mojo_color_state
->color
);
84 EXPECT_EQ(force_anti_aliasing_off
, mojo_color_state
->force_anti_aliasing_off
);
87 TEST_F(SurfaceLibQuadTest
, SurfaceQuad
) {
88 cc::SurfaceDrawQuad
* surface_quad
=
89 pass
->CreateAndAppendDrawQuad
<cc::SurfaceDrawQuad
>();
90 cc::SurfaceId
arbitrary_id(5);
92 sqs
, rect
, opaque_rect
, visible_rect
, needs_blending
, arbitrary_id
);
94 QuadPtr mojo_quad
= Quad::From
<cc::DrawQuad
>(*surface_quad
);
95 ASSERT_FALSE(mojo_quad
.is_null());
96 EXPECT_EQ(MATERIAL_SURFACE_CONTENT
, mojo_quad
->material
);
97 ASSERT_TRUE(mojo_quad
->surface_quad_state
);
98 SurfaceQuadStatePtr
& mojo_surface_state
= mojo_quad
->surface_quad_state
;
99 EXPECT_EQ(SurfaceId::From(arbitrary_id
),
100 mojo_surface_state
->surface
);
103 TEST_F(SurfaceLibQuadTest
, TextureQuad
) {
104 cc::TextureDrawQuad
* texture_quad
=
105 pass
->CreateAndAppendDrawQuad
<cc::TextureDrawQuad
>();
106 unsigned resource_id
= 9;
107 bool premultiplied_alpha
= true;
108 gfx::PointF
uv_top_left(1.7f
, 2.1f
);
109 gfx::PointF
uv_bottom_right(-7.f
, 16.3f
);
110 SkColor background_color
= SK_ColorYELLOW
;
111 float vertex_opacity
[4] = {0.1f
, 0.5f
, 0.4f
, 0.8f
};
112 bool y_flipped
= false;
113 bool nearest_neighbor
= false;
114 texture_quad
->SetAll(sqs
,
128 QuadPtr mojo_quad
= Quad::From
<cc::DrawQuad
>(*texture_quad
);
129 ASSERT_FALSE(mojo_quad
.is_null());
130 EXPECT_EQ(MATERIAL_TEXTURE_CONTENT
, mojo_quad
->material
);
131 ASSERT_TRUE(mojo_quad
->texture_quad_state
);
132 TextureQuadStatePtr
& mojo_texture_state
= mojo_quad
->texture_quad_state
;
133 EXPECT_EQ(resource_id
, mojo_texture_state
->resource_id
);
134 EXPECT_EQ(premultiplied_alpha
, mojo_texture_state
->premultiplied_alpha
);
135 EXPECT_EQ(PointF::From(uv_top_left
), mojo_texture_state
->uv_top_left
);
136 EXPECT_EQ(PointF::From(uv_bottom_right
), mojo_texture_state
->uv_bottom_right
);
137 EXPECT_EQ(Color::From(background_color
),
138 mojo_texture_state
->background_color
);
139 for (size_t i
= 0; i
< 4; ++i
) {
140 EXPECT_EQ(vertex_opacity
[i
], mojo_texture_state
->vertex_opacity
[i
]) << i
;
142 EXPECT_EQ(y_flipped
, mojo_texture_state
->y_flipped
);
145 TEST_F(SurfaceLibQuadTest
, TextureQuadEmptyVertexOpacity
) {
146 QuadPtr mojo_texture_quad
= Quad::New();
147 mojo_texture_quad
->material
= MATERIAL_TEXTURE_CONTENT
;
148 TextureQuadStatePtr mojo_texture_state
= TextureQuadState::New();
149 mojo_texture_state
->background_color
= Color::New();
150 mojo_texture_quad
->texture_quad_state
= mojo_texture_state
.Pass();
151 PassPtr mojo_pass
= Pass::New();
152 mojo_pass
->id
= RenderPassId::New();
153 mojo_pass
->id
->layer_id
= 1;
154 mojo_pass
->id
->index
= 1;
155 mojo_pass
->quads
.push_back(mojo_texture_quad
.Pass());
156 SharedQuadStatePtr mojo_sqs
= SharedQuadState::New();
157 mojo_pass
->shared_quad_states
.push_back(mojo_sqs
.Pass());
159 scoped_ptr
<cc::RenderPass
> pass
= mojo_pass
.To
<scoped_ptr
<cc::RenderPass
> >();
164 TEST_F(SurfaceLibQuadTest
, TextureQuadEmptyBackgroundColor
) {
165 QuadPtr mojo_texture_quad
= Quad::New();
166 mojo_texture_quad
->material
= MATERIAL_TEXTURE_CONTENT
;
167 TextureQuadStatePtr mojo_texture_state
= TextureQuadState::New();
168 mojo_texture_state
->vertex_opacity
= mojo::Array
<float>::New(4);
169 mojo_texture_quad
->texture_quad_state
= mojo_texture_state
.Pass();
170 PassPtr mojo_pass
= Pass::New();
171 mojo_pass
->id
= RenderPassId::New();
172 mojo_pass
->id
->layer_id
= 1;
173 mojo_pass
->id
->index
= 1;
174 mojo_pass
->quads
.push_back(mojo_texture_quad
.Pass());
175 SharedQuadStatePtr mojo_sqs
= SharedQuadState::New();
176 mojo_pass
->shared_quad_states
.push_back(mojo_sqs
.Pass());
178 scoped_ptr
<cc::RenderPass
> pass
= mojo_pass
.To
<scoped_ptr
<cc::RenderPass
> >();
182 TEST(SurfaceLibTest
, SharedQuadState
) {
183 gfx::Transform content_to_target_transform
;
184 content_to_target_transform
.Scale3d(0.3f
, 0.7f
, 0.9f
);
185 gfx::Size
content_bounds(57, 39);
186 gfx::Rect
visible_content_rect(3, 7, 28, 42);
187 gfx::Rect
clip_rect(9, 12, 21, 31);
188 bool is_clipped
= true;
189 float opacity
= 0.65f
;
190 int sorting_context_id
= 13;
191 ::SkXfermode::Mode blend_mode
= ::SkXfermode::kSrcOver_Mode
;
192 scoped_ptr
<cc::RenderPass
> pass
= cc::RenderPass::Create();
193 cc::SharedQuadState
* sqs
= pass
->CreateAndAppendSharedQuadState();
194 sqs
->SetAll(content_to_target_transform
,
196 visible_content_rect
,
203 SharedQuadStatePtr mojo_sqs
= SharedQuadState::From(*sqs
);
204 ASSERT_FALSE(mojo_sqs
.is_null());
205 EXPECT_EQ(Transform::From(content_to_target_transform
),
206 mojo_sqs
->content_to_target_transform
);
207 EXPECT_EQ(Size::From(content_bounds
), mojo_sqs
->content_bounds
);
208 EXPECT_EQ(Rect::From(visible_content_rect
), mojo_sqs
->visible_content_rect
);
209 EXPECT_EQ(Rect::From(clip_rect
), mojo_sqs
->clip_rect
);
210 EXPECT_EQ(is_clipped
, mojo_sqs
->is_clipped
);
211 EXPECT_EQ(opacity
, mojo_sqs
->opacity
);
212 EXPECT_EQ(sorting_context_id
, mojo_sqs
->sorting_context_id
);
215 TEST(SurfaceLibTest
, RenderPass
) {
216 scoped_ptr
<cc::RenderPass
> pass
= cc::RenderPass::Create();
217 cc::RenderPassId
pass_id(1, 6);
218 gfx::Rect
output_rect(4, 9, 13, 71);
219 gfx::Rect
damage_rect(9, 17, 41, 45);
220 gfx::Transform transform_to_root_target
;
221 transform_to_root_target
.SkewY(43.0);
222 bool has_transparent_background
= false;
223 pass
->SetAll(pass_id
,
226 transform_to_root_target
,
227 has_transparent_background
);
229 gfx::Transform content_to_target_transform
;
230 content_to_target_transform
.Scale3d(0.3f
, 0.7f
, 0.9f
);
231 gfx::Size
content_bounds(57, 39);
232 gfx::Rect
visible_content_rect(3, 7, 28, 42);
233 gfx::Rect
clip_rect(9, 12, 21, 31);
234 bool is_clipped
= true;
235 float opacity
= 0.65f
;
236 int sorting_context_id
= 13;
237 ::SkXfermode::Mode blend_mode
= ::SkXfermode::kSrcOver_Mode
;
238 cc::SharedQuadState
* sqs
= pass
->CreateAndAppendSharedQuadState();
239 sqs
->SetAll(content_to_target_transform
,
241 visible_content_rect
,
248 gfx::Rect
rect(5, 7, 13, 19);
249 gfx::Rect
opaque_rect(rect
);
250 gfx::Rect
visible_rect(9, 11, 5, 7);
251 bool needs_blending
= false;
253 cc::SolidColorDrawQuad
* color_quad
=
254 pass
->CreateAndAppendDrawQuad
<cc::SolidColorDrawQuad
>();
255 SkColor arbitrary_color
= SK_ColorGREEN
;
256 bool force_anti_aliasing_off
= true;
257 color_quad
->SetAll(pass
->shared_quad_state_list
.back(),
263 force_anti_aliasing_off
);
265 cc::SurfaceDrawQuad
* surface_quad
=
266 pass
->CreateAndAppendDrawQuad
<cc::SurfaceDrawQuad
>();
267 cc::SurfaceId
arbitrary_id(5);
268 surface_quad
->SetAll(
269 sqs
, rect
, opaque_rect
, visible_rect
, needs_blending
, arbitrary_id
);
271 cc::TextureDrawQuad
* texture_quad
=
272 pass
->CreateAndAppendDrawQuad
<cc::TextureDrawQuad
>();
273 unsigned resource_id
= 9;
274 bool premultiplied_alpha
= true;
275 gfx::PointF
uv_top_left(1.7f
, 2.1f
);
276 gfx::PointF
uv_bottom_right(-7.f
, 16.3f
);
277 SkColor background_color
= SK_ColorYELLOW
;
278 float vertex_opacity
[4] = {0.1f
, 0.5f
, 0.4f
, 0.8f
};
279 bool y_flipped
= false;
280 bool nearest_neighbor
= false;
281 texture_quad
->SetAll(sqs
,
295 PassPtr mojo_pass
= Pass::From(*pass
);
296 ASSERT_FALSE(mojo_pass
.is_null());
297 EXPECT_EQ(6, mojo_pass
->id
->index
);
298 EXPECT_EQ(Rect::From(output_rect
), mojo_pass
->output_rect
);
299 EXPECT_EQ(Rect::From(damage_rect
), mojo_pass
->damage_rect
);
300 EXPECT_EQ(Transform::From(transform_to_root_target
),
301 mojo_pass
->transform_to_root_target
);
302 EXPECT_EQ(has_transparent_background
, mojo_pass
->has_transparent_background
);
303 ASSERT_EQ(1u, mojo_pass
->shared_quad_states
.size());
304 ASSERT_EQ(3u, mojo_pass
->quads
.size());
305 EXPECT_EQ(0u, mojo_pass
->quads
[0]->shared_quad_state_index
);
307 scoped_ptr
<cc::RenderPass
> round_trip_pass
=
308 mojo_pass
.To
<scoped_ptr
<cc::RenderPass
> >();
309 EXPECT_EQ(pass_id
, round_trip_pass
->id
);
310 EXPECT_EQ(output_rect
, round_trip_pass
->output_rect
);
311 EXPECT_EQ(damage_rect
, round_trip_pass
->damage_rect
);
312 EXPECT_EQ(transform_to_root_target
,
313 round_trip_pass
->transform_to_root_target
);
314 EXPECT_EQ(has_transparent_background
,
315 round_trip_pass
->has_transparent_background
);
316 ASSERT_EQ(1u, round_trip_pass
->shared_quad_state_list
.size());
317 ASSERT_EQ(3u, round_trip_pass
->quad_list
.size());
318 EXPECT_EQ(round_trip_pass
->shared_quad_state_list
.front(),
319 round_trip_pass
->quad_list
.front()->shared_quad_state
);
321 cc::SharedQuadState
* round_trip_sqs
=
322 round_trip_pass
->shared_quad_state_list
.front();
323 EXPECT_EQ(content_to_target_transform
,
324 round_trip_sqs
->content_to_target_transform
);
325 EXPECT_EQ(content_bounds
, round_trip_sqs
->content_bounds
);
326 EXPECT_EQ(visible_content_rect
, round_trip_sqs
->visible_content_rect
);
327 EXPECT_EQ(clip_rect
, round_trip_sqs
->clip_rect
);
328 EXPECT_EQ(is_clipped
, round_trip_sqs
->is_clipped
);
329 EXPECT_EQ(opacity
, round_trip_sqs
->opacity
);
330 EXPECT_EQ(sorting_context_id
, round_trip_sqs
->sorting_context_id
);
332 cc::DrawQuad
* round_trip_quad
= round_trip_pass
->quad_list
.front();
333 // First is solid color quad.
334 ASSERT_EQ(cc::DrawQuad::SOLID_COLOR
, round_trip_quad
->material
);
335 EXPECT_EQ(rect
, round_trip_quad
->rect
);
336 EXPECT_EQ(opaque_rect
, round_trip_quad
->opaque_rect
);
337 EXPECT_EQ(visible_rect
, round_trip_quad
->visible_rect
);
338 EXPECT_EQ(needs_blending
, round_trip_quad
->needs_blending
);
339 const cc::SolidColorDrawQuad
* round_trip_color_quad
=
340 cc::SolidColorDrawQuad::MaterialCast(round_trip_quad
);
341 EXPECT_EQ(arbitrary_color
, round_trip_color_quad
->color
);
342 EXPECT_EQ(force_anti_aliasing_off
,
343 round_trip_color_quad
->force_anti_aliasing_off
);
345 round_trip_quad
= round_trip_pass
->quad_list
.ElementAt(1);
346 // Second is surface quad.
347 ASSERT_EQ(cc::DrawQuad::SURFACE_CONTENT
, round_trip_quad
->material
);
348 const cc::SurfaceDrawQuad
* round_trip_surface_quad
=
349 cc::SurfaceDrawQuad::MaterialCast(round_trip_quad
);
350 EXPECT_EQ(arbitrary_id
, round_trip_surface_quad
->surface_id
);
352 round_trip_quad
= round_trip_pass
->quad_list
.ElementAt(2);
353 // Third is texture quad.
354 ASSERT_EQ(cc::DrawQuad::TEXTURE_CONTENT
, round_trip_quad
->material
);
355 const cc::TextureDrawQuad
* round_trip_texture_quad
=
356 cc::TextureDrawQuad::MaterialCast(round_trip_quad
);
357 EXPECT_EQ(resource_id
, round_trip_texture_quad
->resource_id
);
358 EXPECT_EQ(premultiplied_alpha
, round_trip_texture_quad
->premultiplied_alpha
);
359 EXPECT_EQ(uv_top_left
, round_trip_texture_quad
->uv_top_left
);
360 EXPECT_EQ(uv_bottom_right
, round_trip_texture_quad
->uv_bottom_right
);
361 EXPECT_EQ(background_color
, round_trip_texture_quad
->background_color
);
362 for (size_t i
= 0; i
< 4; ++i
) {
363 EXPECT_EQ(vertex_opacity
[i
], round_trip_texture_quad
->vertex_opacity
[i
])
366 EXPECT_EQ(y_flipped
, round_trip_texture_quad
->y_flipped
);
369 TEST(SurfaceLibTest
, Mailbox
) {
370 gpu::Mailbox mailbox
;
373 MailboxPtr mojo_mailbox
= Mailbox::From(mailbox
);
374 EXPECT_EQ(0, memcmp(mailbox
.name
, &mojo_mailbox
->name
.storage()[0], 64));
376 gpu::Mailbox round_trip_mailbox
= mojo_mailbox
.To
<gpu::Mailbox
>();
377 EXPECT_EQ(mailbox
, round_trip_mailbox
);
380 TEST(SurfaceLibTest
, MailboxEmptyName
) {
381 MailboxPtr mojo_mailbox
= Mailbox::New();
383 gpu::Mailbox converted_mailbox
= mojo_mailbox
.To
<gpu::Mailbox
>();
384 EXPECT_TRUE(converted_mailbox
.IsZero());
387 TEST(SurfaceLibTest
, MailboxHolder
) {
388 gpu::Mailbox mailbox
;
390 uint32_t texture_target
= GL_TEXTURE_2D
;
391 uint32_t sync_point
= 7u;
392 gpu::MailboxHolder
holder(mailbox
, texture_target
, sync_point
);
394 MailboxHolderPtr mojo_holder
= MailboxHolder::From(holder
);
395 EXPECT_EQ(texture_target
, mojo_holder
->texture_target
);
396 EXPECT_EQ(sync_point
, mojo_holder
->sync_point
);
398 gpu::MailboxHolder round_trip_holder
= mojo_holder
.To
<gpu::MailboxHolder
>();
399 EXPECT_EQ(mailbox
, round_trip_holder
.mailbox
);
400 EXPECT_EQ(texture_target
, round_trip_holder
.texture_target
);
401 EXPECT_EQ(sync_point
, round_trip_holder
.sync_point
);
404 TEST(SurfaceLibTest
, TransferableResource
) {
406 cc::ResourceFormat format
= cc::BGRA_8888
;
407 uint32_t filter
= 123u;
408 gfx::Size
size(17, 18);
409 gpu::MailboxHolder mailbox_holder
;
410 bool is_repeated
= false;
412 bool is_software
= false;
413 cc::TransferableResource resource
;
415 resource
.format
= format
;
416 resource
.filter
= filter
;
417 resource
.size
= size
;
418 resource
.mailbox_holder
= mailbox_holder
;
419 resource
.is_repeated
= is_repeated
;
420 resource
.is_software
= is_software
;
422 TransferableResourcePtr mojo_resource
= TransferableResource::From(resource
);
423 EXPECT_EQ(id
, mojo_resource
->id
);
424 EXPECT_EQ(static_cast<ResourceFormat
>(format
),
425 mojo_resource
->format
);
426 EXPECT_EQ(filter
, mojo_resource
->filter
);
427 EXPECT_EQ(Size::From(size
), mojo_resource
->size
);
428 EXPECT_EQ(is_repeated
, mojo_resource
->is_repeated
);
429 EXPECT_EQ(is_software
, mojo_resource
->is_software
);
431 cc::TransferableResource round_trip_resource
=
432 mojo_resource
.To
<cc::TransferableResource
>();
433 EXPECT_EQ(id
, round_trip_resource
.id
);
434 EXPECT_EQ(format
, round_trip_resource
.format
);
435 EXPECT_EQ(filter
, round_trip_resource
.filter
);
436 EXPECT_EQ(size
, round_trip_resource
.size
);
437 EXPECT_EQ(mailbox_holder
.mailbox
, round_trip_resource
.mailbox_holder
.mailbox
);
438 EXPECT_EQ(mailbox_holder
.texture_target
,
439 round_trip_resource
.mailbox_holder
.texture_target
);
440 EXPECT_EQ(mailbox_holder
.sync_point
,
441 round_trip_resource
.mailbox_holder
.sync_point
);
442 EXPECT_EQ(is_repeated
, round_trip_resource
.is_repeated
);
443 EXPECT_EQ(is_software
, round_trip_resource
.is_software
);
446 TEST(SurfaceLibTest
, ReturnedResource
) {
448 uint32_t sync_point
= 24u;
451 cc::ReturnedResource resource
;
453 resource
.sync_point
= sync_point
;
454 resource
.count
= count
;
455 resource
.lost
= lost
;
457 ReturnedResourcePtr mojo_resource
= ReturnedResource::From(resource
);
458 EXPECT_EQ(id
, mojo_resource
->id
);
459 EXPECT_EQ(sync_point
, mojo_resource
->sync_point
);
460 EXPECT_EQ(count
, mojo_resource
->count
);
461 EXPECT_EQ(lost
, mojo_resource
->lost
);
463 cc::ReturnedResource round_trip_resource
=
464 mojo_resource
.To
<cc::ReturnedResource
>();
465 EXPECT_EQ(id
, round_trip_resource
.id
);
466 EXPECT_EQ(sync_point
, round_trip_resource
.sync_point
);
467 EXPECT_EQ(count
, round_trip_resource
.count
);
468 EXPECT_EQ(lost
, round_trip_resource
.lost
);