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 "gpu/command_buffer/common/mailbox.h"
10 #include "gpu/command_buffer/common/mailbox_holder.h"
11 #include "mojo/converters/geometry/geometry_type_converters.h"
12 #include "mojo/converters/surfaces/surfaces_type_converters.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14 #include "third_party/skia/include/core/SkColor.h"
15 #include "third_party/skia/include/core/SkXfermode.h"
20 TEST(SurfaceLibTest
, SurfaceIdConverterNullId
) {
21 cc::SurfaceId null_id
;
22 cc::SurfaceId round_trip
= SurfaceId::From(null_id
).To
<cc::SurfaceId
>();
23 EXPECT_TRUE(round_trip
.is_null());
26 TEST(SurfaceLibTest
, SurfaceIdConverterValidId
) {
27 cc::SurfaceId
valid_id(7);
28 cc::SurfaceId round_trip
= SurfaceId::From(valid_id
).To
<cc::SurfaceId
>();
29 EXPECT_FALSE(round_trip
.is_null());
30 EXPECT_EQ(valid_id
, round_trip
);
33 TEST(SurfaceLibTest
, Color
) {
34 SkColor arbitrary_color
= SK_ColorMAGENTA
;
35 SkColor round_trip
= Color::From(arbitrary_color
).To
<SkColor
>();
36 EXPECT_EQ(arbitrary_color
, round_trip
);
39 class SurfaceLibQuadTest
: public testing::Test
{
44 visible_rect(9, 11, 5, 7),
45 needs_blending(false) {
46 pass
= cc::RenderPass::Create();
47 sqs
= pass
->CreateAndAppendSharedQuadState();
52 gfx::Rect opaque_rect
;
53 gfx::Rect visible_rect
;
55 scoped_ptr
<cc::RenderPass
> pass
;
56 cc::SharedQuadState
* sqs
;
59 TEST_F(SurfaceLibQuadTest
, ColorQuad
) {
60 cc::SolidColorDrawQuad
* color_quad
=
61 pass
->CreateAndAppendDrawQuad
<cc::SolidColorDrawQuad
>();
62 SkColor arbitrary_color
= SK_ColorGREEN
;
63 bool force_anti_aliasing_off
= true;
64 color_quad
->SetAll(sqs
,
70 force_anti_aliasing_off
);
72 QuadPtr mojo_quad
= Quad::From
<cc::DrawQuad
>(*color_quad
);
73 ASSERT_FALSE(mojo_quad
.is_null());
74 EXPECT_EQ(MATERIAL_SOLID_COLOR
, mojo_quad
->material
);
75 EXPECT_EQ(Rect::From(rect
), mojo_quad
->rect
);
76 EXPECT_EQ(Rect::From(opaque_rect
), mojo_quad
->opaque_rect
);
77 EXPECT_EQ(Rect::From(visible_rect
), mojo_quad
->visible_rect
);
78 EXPECT_EQ(needs_blending
, mojo_quad
->needs_blending
);
79 ASSERT_TRUE(mojo_quad
->solid_color_quad_state
);
80 SolidColorQuadStatePtr
& mojo_color_state
= mojo_quad
->solid_color_quad_state
;
81 EXPECT_EQ(Color::From(arbitrary_color
), mojo_color_state
->color
);
82 EXPECT_EQ(force_anti_aliasing_off
, mojo_color_state
->force_anti_aliasing_off
);
85 TEST_F(SurfaceLibQuadTest
, SurfaceQuad
) {
86 cc::SurfaceDrawQuad
* surface_quad
=
87 pass
->CreateAndAppendDrawQuad
<cc::SurfaceDrawQuad
>();
88 cc::SurfaceId
arbitrary_id(5);
90 sqs
, rect
, opaque_rect
, visible_rect
, needs_blending
, arbitrary_id
);
92 QuadPtr mojo_quad
= Quad::From
<cc::DrawQuad
>(*surface_quad
);
93 ASSERT_FALSE(mojo_quad
.is_null());
94 EXPECT_EQ(MATERIAL_SURFACE_CONTENT
, mojo_quad
->material
);
95 ASSERT_TRUE(mojo_quad
->surface_quad_state
);
96 SurfaceQuadStatePtr
& mojo_surface_state
= mojo_quad
->surface_quad_state
;
97 EXPECT_EQ(SurfaceId::From(arbitrary_id
),
98 mojo_surface_state
->surface
);
101 TEST_F(SurfaceLibQuadTest
, TextureQuad
) {
102 cc::TextureDrawQuad
* texture_quad
=
103 pass
->CreateAndAppendDrawQuad
<cc::TextureDrawQuad
>();
104 unsigned resource_id
= 9;
105 bool premultiplied_alpha
= true;
106 gfx::PointF
uv_top_left(1.7f
, 2.1f
);
107 gfx::PointF
uv_bottom_right(-7.f
, 16.3f
);
108 SkColor background_color
= SK_ColorYELLOW
;
109 float vertex_opacity
[4] = {0.1f
, 0.5f
, 0.4f
, 0.8f
};
110 bool flipped
= false;
111 bool nearest_neighbor
= false;
112 texture_quad
->SetAll(sqs
,
126 QuadPtr mojo_quad
= Quad::From
<cc::DrawQuad
>(*texture_quad
);
127 ASSERT_FALSE(mojo_quad
.is_null());
128 EXPECT_EQ(MATERIAL_TEXTURE_CONTENT
, mojo_quad
->material
);
129 ASSERT_TRUE(mojo_quad
->texture_quad_state
);
130 TextureQuadStatePtr
& mojo_texture_state
= mojo_quad
->texture_quad_state
;
131 EXPECT_EQ(resource_id
, mojo_texture_state
->resource_id
);
132 EXPECT_EQ(premultiplied_alpha
, mojo_texture_state
->premultiplied_alpha
);
133 EXPECT_EQ(PointF::From(uv_top_left
), mojo_texture_state
->uv_top_left
);
134 EXPECT_EQ(PointF::From(uv_bottom_right
), mojo_texture_state
->uv_bottom_right
);
135 EXPECT_EQ(Color::From(background_color
),
136 mojo_texture_state
->background_color
);
137 for (size_t i
= 0; i
< 4; ++i
) {
138 EXPECT_EQ(vertex_opacity
[i
], mojo_texture_state
->vertex_opacity
[i
]) << i
;
140 EXPECT_EQ(flipped
, mojo_texture_state
->flipped
);
143 TEST_F(SurfaceLibQuadTest
, TextureQuadEmptyVertexOpacity
) {
144 QuadPtr mojo_texture_quad
= Quad::New();
145 mojo_texture_quad
->material
= MATERIAL_TEXTURE_CONTENT
;
146 TextureQuadStatePtr mojo_texture_state
= TextureQuadState::New();
147 mojo_texture_state
->background_color
= Color::New();
148 mojo_texture_quad
->texture_quad_state
= mojo_texture_state
.Pass();
149 PassPtr mojo_pass
= Pass::New();
150 mojo_pass
->quads
.push_back(mojo_texture_quad
.Pass());
151 SharedQuadStatePtr mojo_sqs
= SharedQuadState::New();
152 mojo_pass
->shared_quad_states
.push_back(mojo_sqs
.Pass());
154 scoped_ptr
<cc::RenderPass
> pass
= mojo_pass
.To
<scoped_ptr
<cc::RenderPass
> >();
159 TEST_F(SurfaceLibQuadTest
, TextureQuadEmptyBackgroundColor
) {
160 QuadPtr mojo_texture_quad
= Quad::New();
161 mojo_texture_quad
->material
= MATERIAL_TEXTURE_CONTENT
;
162 TextureQuadStatePtr mojo_texture_state
= TextureQuadState::New();
163 mojo_texture_state
->vertex_opacity
= mojo::Array
<float>::New(4);
164 mojo_texture_quad
->texture_quad_state
= mojo_texture_state
.Pass();
165 PassPtr mojo_pass
= Pass::New();
166 mojo_pass
->quads
.push_back(mojo_texture_quad
.Pass());
167 SharedQuadStatePtr mojo_sqs
= SharedQuadState::New();
168 mojo_pass
->shared_quad_states
.push_back(mojo_sqs
.Pass());
170 scoped_ptr
<cc::RenderPass
> pass
= mojo_pass
.To
<scoped_ptr
<cc::RenderPass
> >();
174 TEST(SurfaceLibTest
, SharedQuadState
) {
175 gfx::Transform content_to_target_transform
;
176 content_to_target_transform
.Scale3d(0.3f
, 0.7f
, 0.9f
);
177 gfx::Size
content_bounds(57, 39);
178 gfx::Rect
visible_content_rect(3, 7, 28, 42);
179 gfx::Rect
clip_rect(9, 12, 21, 31);
180 bool is_clipped
= true;
181 float opacity
= 0.65f
;
182 int sorting_context_id
= 13;
183 ::SkXfermode::Mode blend_mode
= ::SkXfermode::kSrcOver_Mode
;
184 scoped_ptr
<cc::RenderPass
> pass
= cc::RenderPass::Create();
185 cc::SharedQuadState
* sqs
= pass
->CreateAndAppendSharedQuadState();
186 sqs
->SetAll(content_to_target_transform
,
188 visible_content_rect
,
195 SharedQuadStatePtr mojo_sqs
= SharedQuadState::From(*sqs
);
196 ASSERT_FALSE(mojo_sqs
.is_null());
197 EXPECT_EQ(Transform::From(content_to_target_transform
),
198 mojo_sqs
->content_to_target_transform
);
199 EXPECT_EQ(Size::From(content_bounds
), mojo_sqs
->content_bounds
);
200 EXPECT_EQ(Rect::From(visible_content_rect
), mojo_sqs
->visible_content_rect
);
201 EXPECT_EQ(Rect::From(clip_rect
), mojo_sqs
->clip_rect
);
202 EXPECT_EQ(is_clipped
, mojo_sqs
->is_clipped
);
203 EXPECT_EQ(opacity
, mojo_sqs
->opacity
);
204 EXPECT_EQ(sorting_context_id
, mojo_sqs
->sorting_context_id
);
207 TEST(SurfaceLibTest
, RenderPass
) {
208 scoped_ptr
<cc::RenderPass
> pass
= cc::RenderPass::Create();
209 cc::RenderPassId
pass_id(1, 6);
210 gfx::Rect
output_rect(4, 9, 13, 71);
211 gfx::Rect
damage_rect(9, 17, 41, 45);
212 gfx::Transform transform_to_root_target
;
213 transform_to_root_target
.SkewY(43.0);
214 bool has_transparent_background
= false;
215 pass
->SetAll(pass_id
,
218 transform_to_root_target
,
219 has_transparent_background
);
221 gfx::Transform content_to_target_transform
;
222 content_to_target_transform
.Scale3d(0.3f
, 0.7f
, 0.9f
);
223 gfx::Size
content_bounds(57, 39);
224 gfx::Rect
visible_content_rect(3, 7, 28, 42);
225 gfx::Rect
clip_rect(9, 12, 21, 31);
226 bool is_clipped
= true;
227 float opacity
= 0.65f
;
228 int sorting_context_id
= 13;
229 ::SkXfermode::Mode blend_mode
= ::SkXfermode::kSrcOver_Mode
;
230 cc::SharedQuadState
* sqs
= pass
->CreateAndAppendSharedQuadState();
231 sqs
->SetAll(content_to_target_transform
,
233 visible_content_rect
,
240 gfx::Rect
rect(5, 7, 13, 19);
241 gfx::Rect
opaque_rect(rect
);
242 gfx::Rect
visible_rect(9, 11, 5, 7);
243 bool needs_blending
= false;
245 cc::SolidColorDrawQuad
* color_quad
=
246 pass
->CreateAndAppendDrawQuad
<cc::SolidColorDrawQuad
>();
247 SkColor arbitrary_color
= SK_ColorGREEN
;
248 bool force_anti_aliasing_off
= true;
249 color_quad
->SetAll(pass
->shared_quad_state_list
.back(),
255 force_anti_aliasing_off
);
257 cc::SurfaceDrawQuad
* surface_quad
=
258 pass
->CreateAndAppendDrawQuad
<cc::SurfaceDrawQuad
>();
259 cc::SurfaceId
arbitrary_id(5);
260 surface_quad
->SetAll(
261 sqs
, rect
, opaque_rect
, visible_rect
, needs_blending
, arbitrary_id
);
263 cc::TextureDrawQuad
* texture_quad
=
264 pass
->CreateAndAppendDrawQuad
<cc::TextureDrawQuad
>();
265 unsigned resource_id
= 9;
266 bool premultiplied_alpha
= true;
267 gfx::PointF
uv_top_left(1.7f
, 2.1f
);
268 gfx::PointF
uv_bottom_right(-7.f
, 16.3f
);
269 SkColor background_color
= SK_ColorYELLOW
;
270 float vertex_opacity
[4] = {0.1f
, 0.5f
, 0.4f
, 0.8f
};
271 bool flipped
= false;
272 bool nearest_neighbor
= false;
273 texture_quad
->SetAll(sqs
,
287 PassPtr mojo_pass
= Pass::From(*pass
);
288 ASSERT_FALSE(mojo_pass
.is_null());
289 EXPECT_EQ(6, mojo_pass
->id
);
290 EXPECT_EQ(Rect::From(output_rect
), mojo_pass
->output_rect
);
291 EXPECT_EQ(Rect::From(damage_rect
), mojo_pass
->damage_rect
);
292 EXPECT_EQ(Transform::From(transform_to_root_target
),
293 mojo_pass
->transform_to_root_target
);
294 EXPECT_EQ(has_transparent_background
, mojo_pass
->has_transparent_background
);
295 ASSERT_EQ(1u, mojo_pass
->shared_quad_states
.size());
296 ASSERT_EQ(3u, mojo_pass
->quads
.size());
297 EXPECT_EQ(0u, mojo_pass
->quads
[0]->shared_quad_state_index
);
299 scoped_ptr
<cc::RenderPass
> round_trip_pass
=
300 mojo_pass
.To
<scoped_ptr
<cc::RenderPass
> >();
301 EXPECT_EQ(pass_id
, round_trip_pass
->id
);
302 EXPECT_EQ(output_rect
, round_trip_pass
->output_rect
);
303 EXPECT_EQ(damage_rect
, round_trip_pass
->damage_rect
);
304 EXPECT_EQ(transform_to_root_target
,
305 round_trip_pass
->transform_to_root_target
);
306 EXPECT_EQ(has_transparent_background
,
307 round_trip_pass
->has_transparent_background
);
308 ASSERT_EQ(1u, round_trip_pass
->shared_quad_state_list
.size());
309 ASSERT_EQ(3u, round_trip_pass
->quad_list
.size());
310 EXPECT_EQ(round_trip_pass
->shared_quad_state_list
.front(),
311 round_trip_pass
->quad_list
.front()->shared_quad_state
);
313 cc::SharedQuadState
* round_trip_sqs
=
314 round_trip_pass
->shared_quad_state_list
.front();
315 EXPECT_EQ(content_to_target_transform
,
316 round_trip_sqs
->content_to_target_transform
);
317 EXPECT_EQ(content_bounds
, round_trip_sqs
->content_bounds
);
318 EXPECT_EQ(visible_content_rect
, round_trip_sqs
->visible_content_rect
);
319 EXPECT_EQ(clip_rect
, round_trip_sqs
->clip_rect
);
320 EXPECT_EQ(is_clipped
, round_trip_sqs
->is_clipped
);
321 EXPECT_EQ(opacity
, round_trip_sqs
->opacity
);
322 EXPECT_EQ(sorting_context_id
, round_trip_sqs
->sorting_context_id
);
324 cc::DrawQuad
* round_trip_quad
= round_trip_pass
->quad_list
.front();
325 // First is solid color quad.
326 ASSERT_EQ(cc::DrawQuad::SOLID_COLOR
, round_trip_quad
->material
);
327 EXPECT_EQ(rect
, round_trip_quad
->rect
);
328 EXPECT_EQ(opaque_rect
, round_trip_quad
->opaque_rect
);
329 EXPECT_EQ(visible_rect
, round_trip_quad
->visible_rect
);
330 EXPECT_EQ(needs_blending
, round_trip_quad
->needs_blending
);
331 const cc::SolidColorDrawQuad
* round_trip_color_quad
=
332 cc::SolidColorDrawQuad::MaterialCast(round_trip_quad
);
333 EXPECT_EQ(arbitrary_color
, round_trip_color_quad
->color
);
334 EXPECT_EQ(force_anti_aliasing_off
,
335 round_trip_color_quad
->force_anti_aliasing_off
);
337 round_trip_quad
= round_trip_pass
->quad_list
.ElementAt(1);
338 // Second is surface quad.
339 ASSERT_EQ(cc::DrawQuad::SURFACE_CONTENT
, round_trip_quad
->material
);
340 const cc::SurfaceDrawQuad
* round_trip_surface_quad
=
341 cc::SurfaceDrawQuad::MaterialCast(round_trip_quad
);
342 EXPECT_EQ(arbitrary_id
, round_trip_surface_quad
->surface_id
);
344 round_trip_quad
= round_trip_pass
->quad_list
.ElementAt(2);
345 // Third is texture quad.
346 ASSERT_EQ(cc::DrawQuad::TEXTURE_CONTENT
, round_trip_quad
->material
);
347 const cc::TextureDrawQuad
* round_trip_texture_quad
=
348 cc::TextureDrawQuad::MaterialCast(round_trip_quad
);
349 EXPECT_EQ(resource_id
, round_trip_texture_quad
->resource_id
);
350 EXPECT_EQ(premultiplied_alpha
, round_trip_texture_quad
->premultiplied_alpha
);
351 EXPECT_EQ(uv_top_left
, round_trip_texture_quad
->uv_top_left
);
352 EXPECT_EQ(uv_bottom_right
, round_trip_texture_quad
->uv_bottom_right
);
353 EXPECT_EQ(background_color
, round_trip_texture_quad
->background_color
);
354 for (size_t i
= 0; i
< 4; ++i
) {
355 EXPECT_EQ(vertex_opacity
[i
], round_trip_texture_quad
->vertex_opacity
[i
])
358 EXPECT_EQ(flipped
, round_trip_texture_quad
->flipped
);
361 TEST(SurfaceLibTest
, Mailbox
) {
362 gpu::Mailbox mailbox
;
365 MailboxPtr mojo_mailbox
= Mailbox::From(mailbox
);
366 EXPECT_EQ(0, memcmp(mailbox
.name
, &mojo_mailbox
->name
.storage()[0], 64));
368 gpu::Mailbox round_trip_mailbox
= mojo_mailbox
.To
<gpu::Mailbox
>();
369 EXPECT_EQ(mailbox
, round_trip_mailbox
);
372 TEST(SurfaceLibTest
, MailboxEmptyName
) {
373 MailboxPtr mojo_mailbox
= Mailbox::New();
375 gpu::Mailbox converted_mailbox
= mojo_mailbox
.To
<gpu::Mailbox
>();
376 EXPECT_TRUE(converted_mailbox
.IsZero());
379 TEST(SurfaceLibTest
, MailboxHolder
) {
380 gpu::Mailbox mailbox
;
382 uint32_t texture_target
= GL_TEXTURE_2D
;
383 uint32_t sync_point
= 7u;
384 gpu::MailboxHolder
holder(mailbox
, texture_target
, sync_point
);
386 MailboxHolderPtr mojo_holder
= MailboxHolder::From(holder
);
387 EXPECT_EQ(texture_target
, mojo_holder
->texture_target
);
388 EXPECT_EQ(sync_point
, mojo_holder
->sync_point
);
390 gpu::MailboxHolder round_trip_holder
= mojo_holder
.To
<gpu::MailboxHolder
>();
391 EXPECT_EQ(mailbox
, round_trip_holder
.mailbox
);
392 EXPECT_EQ(texture_target
, round_trip_holder
.texture_target
);
393 EXPECT_EQ(sync_point
, round_trip_holder
.sync_point
);
396 TEST(SurfaceLibTest
, TransferableResource
) {
398 cc::ResourceFormat format
= cc::BGRA_8888
;
399 uint32_t filter
= 123u;
400 gfx::Size
size(17, 18);
401 gpu::MailboxHolder mailbox_holder
;
402 bool is_repeated
= false;
404 bool is_software
= false;
405 cc::TransferableResource resource
;
407 resource
.format
= format
;
408 resource
.filter
= filter
;
409 resource
.size
= size
;
410 resource
.mailbox_holder
= mailbox_holder
;
411 resource
.is_repeated
= is_repeated
;
412 resource
.is_software
= is_software
;
414 TransferableResourcePtr mojo_resource
= TransferableResource::From(resource
);
415 EXPECT_EQ(id
, mojo_resource
->id
);
416 EXPECT_EQ(static_cast<ResourceFormat
>(format
),
417 mojo_resource
->format
);
418 EXPECT_EQ(filter
, mojo_resource
->filter
);
419 EXPECT_EQ(Size::From(size
), mojo_resource
->size
);
420 EXPECT_EQ(is_repeated
, mojo_resource
->is_repeated
);
421 EXPECT_EQ(is_software
, mojo_resource
->is_software
);
423 cc::TransferableResource round_trip_resource
=
424 mojo_resource
.To
<cc::TransferableResource
>();
425 EXPECT_EQ(id
, round_trip_resource
.id
);
426 EXPECT_EQ(format
, round_trip_resource
.format
);
427 EXPECT_EQ(filter
, round_trip_resource
.filter
);
428 EXPECT_EQ(size
, round_trip_resource
.size
);
429 EXPECT_EQ(mailbox_holder
.mailbox
, round_trip_resource
.mailbox_holder
.mailbox
);
430 EXPECT_EQ(mailbox_holder
.texture_target
,
431 round_trip_resource
.mailbox_holder
.texture_target
);
432 EXPECT_EQ(mailbox_holder
.sync_point
,
433 round_trip_resource
.mailbox_holder
.sync_point
);
434 EXPECT_EQ(is_repeated
, round_trip_resource
.is_repeated
);
435 EXPECT_EQ(is_software
, round_trip_resource
.is_software
);
438 TEST(SurfaceLibTest
, ReturnedResource
) {
440 uint32_t sync_point
= 24u;
443 cc::ReturnedResource resource
;
445 resource
.sync_point
= sync_point
;
446 resource
.count
= count
;
447 resource
.lost
= lost
;
449 ReturnedResourcePtr mojo_resource
= ReturnedResource::From(resource
);
450 EXPECT_EQ(id
, mojo_resource
->id
);
451 EXPECT_EQ(sync_point
, mojo_resource
->sync_point
);
452 EXPECT_EQ(count
, mojo_resource
->count
);
453 EXPECT_EQ(lost
, mojo_resource
->lost
);
455 cc::ReturnedResource round_trip_resource
=
456 mojo_resource
.To
<cc::ReturnedResource
>();
457 EXPECT_EQ(id
, round_trip_resource
.id
);
458 EXPECT_EQ(sync_point
, round_trip_resource
.sync_point
);
459 EXPECT_EQ(count
, round_trip_resource
.count
);
460 EXPECT_EQ(lost
, round_trip_resource
.lost
);