Remove unused parameter.
[chromium-blink-merge.git] / mojo / converters / surfaces / tests / surface_unittest.cc
blob10e1b4114d957f4b6fff4ced676a2caf5a3c4d93
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 "mojo/converters/transform/transform_type_converters.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15 #include "third_party/skia/include/core/SkColor.h"
16 #include "third_party/skia/include/core/SkXfermode.h"
18 namespace mojo {
19 namespace {
21 TEST(SurfaceLibTest, SurfaceIdConverterNullId) {
22 cc::SurfaceId null_id;
23 cc::SurfaceId round_trip = SurfaceId::From(null_id).To<cc::SurfaceId>();
24 EXPECT_TRUE(round_trip.is_null());
27 TEST(SurfaceLibTest, SurfaceIdConverterValidId) {
28 cc::SurfaceId valid_id(7);
29 cc::SurfaceId round_trip = SurfaceId::From(valid_id).To<cc::SurfaceId>();
30 EXPECT_FALSE(round_trip.is_null());
31 EXPECT_EQ(valid_id, round_trip);
34 TEST(SurfaceLibTest, Color) {
35 SkColor arbitrary_color = SK_ColorMAGENTA;
36 SkColor round_trip = Color::From(arbitrary_color).To<SkColor>();
37 EXPECT_EQ(arbitrary_color, round_trip);
40 class SurfaceLibQuadTest : public testing::Test {
41 public:
42 SurfaceLibQuadTest()
43 : rect(5, 7, 13, 19),
44 opaque_rect(rect),
45 visible_rect(9, 11, 5, 7),
46 needs_blending(false) {
47 pass = cc::RenderPass::Create();
48 sqs = pass->CreateAndAppendSharedQuadState();
51 protected:
52 gfx::Rect rect;
53 gfx::Rect opaque_rect;
54 gfx::Rect visible_rect;
55 bool needs_blending;
56 scoped_ptr<cc::RenderPass> pass;
57 cc::SharedQuadState* sqs;
60 TEST_F(SurfaceLibQuadTest, ColorQuad) {
61 cc::SolidColorDrawQuad* color_quad =
62 pass->CreateAndAppendDrawQuad<cc::SolidColorDrawQuad>();
63 SkColor arbitrary_color = SK_ColorGREEN;
64 bool force_anti_aliasing_off = true;
65 color_quad->SetAll(sqs,
66 rect,
67 opaque_rect,
68 visible_rect,
69 needs_blending,
70 arbitrary_color,
71 force_anti_aliasing_off);
73 QuadPtr mojo_quad = Quad::From<cc::DrawQuad>(*color_quad);
74 ASSERT_FALSE(mojo_quad.is_null());
75 EXPECT_EQ(MATERIAL_SOLID_COLOR, mojo_quad->material);
76 EXPECT_EQ(Rect::From(rect), mojo_quad->rect);
77 EXPECT_EQ(Rect::From(opaque_rect), mojo_quad->opaque_rect);
78 EXPECT_EQ(Rect::From(visible_rect), mojo_quad->visible_rect);
79 EXPECT_EQ(needs_blending, mojo_quad->needs_blending);
80 ASSERT_TRUE(mojo_quad->solid_color_quad_state);
81 SolidColorQuadStatePtr& mojo_color_state = mojo_quad->solid_color_quad_state;
82 EXPECT_EQ(Color::From(arbitrary_color), mojo_color_state->color);
83 EXPECT_EQ(force_anti_aliasing_off, mojo_color_state->force_anti_aliasing_off);
86 TEST_F(SurfaceLibQuadTest, SurfaceQuad) {
87 cc::SurfaceDrawQuad* surface_quad =
88 pass->CreateAndAppendDrawQuad<cc::SurfaceDrawQuad>();
89 cc::SurfaceId arbitrary_id(5);
90 surface_quad->SetAll(
91 sqs, rect, opaque_rect, visible_rect, needs_blending, arbitrary_id);
93 QuadPtr mojo_quad = Quad::From<cc::DrawQuad>(*surface_quad);
94 ASSERT_FALSE(mojo_quad.is_null());
95 EXPECT_EQ(MATERIAL_SURFACE_CONTENT, mojo_quad->material);
96 ASSERT_TRUE(mojo_quad->surface_quad_state);
97 SurfaceQuadStatePtr& mojo_surface_state = mojo_quad->surface_quad_state;
98 EXPECT_EQ(SurfaceId::From(arbitrary_id),
99 mojo_surface_state->surface);
102 TEST_F(SurfaceLibQuadTest, TextureQuad) {
103 cc::TextureDrawQuad* texture_quad =
104 pass->CreateAndAppendDrawQuad<cc::TextureDrawQuad>();
105 unsigned resource_id = 9;
106 bool premultiplied_alpha = true;
107 gfx::PointF uv_top_left(1.7f, 2.1f);
108 gfx::PointF uv_bottom_right(-7.f, 16.3f);
109 SkColor background_color = SK_ColorYELLOW;
110 float vertex_opacity[4] = {0.1f, 0.5f, 0.4f, 0.8f};
111 bool flipped = false;
112 bool nearest_neighbor = false;
113 texture_quad->SetAll(sqs,
114 rect,
115 opaque_rect,
116 visible_rect,
117 needs_blending,
118 resource_id,
119 premultiplied_alpha,
120 uv_top_left,
121 uv_bottom_right,
122 background_color,
123 vertex_opacity,
124 flipped,
125 nearest_neighbor);
127 QuadPtr mojo_quad = Quad::From<cc::DrawQuad>(*texture_quad);
128 ASSERT_FALSE(mojo_quad.is_null());
129 EXPECT_EQ(MATERIAL_TEXTURE_CONTENT, mojo_quad->material);
130 ASSERT_TRUE(mojo_quad->texture_quad_state);
131 TextureQuadStatePtr& mojo_texture_state = mojo_quad->texture_quad_state;
132 EXPECT_EQ(resource_id, mojo_texture_state->resource_id);
133 EXPECT_EQ(premultiplied_alpha, mojo_texture_state->premultiplied_alpha);
134 EXPECT_EQ(PointF::From(uv_top_left), mojo_texture_state->uv_top_left);
135 EXPECT_EQ(PointF::From(uv_bottom_right), mojo_texture_state->uv_bottom_right);
136 EXPECT_EQ(Color::From(background_color),
137 mojo_texture_state->background_color);
138 for (size_t i = 0; i < 4; ++i) {
139 EXPECT_EQ(vertex_opacity[i], mojo_texture_state->vertex_opacity[i]) << i;
141 EXPECT_EQ(flipped, mojo_texture_state->flipped);
144 TEST_F(SurfaceLibQuadTest, TextureQuadEmptyVertexOpacity) {
145 QuadPtr mojo_texture_quad = Quad::New();
146 mojo_texture_quad->material = MATERIAL_TEXTURE_CONTENT;
147 TextureQuadStatePtr mojo_texture_state = TextureQuadState::New();
148 mojo_texture_state->background_color = Color::New();
149 mojo_texture_quad->texture_quad_state = mojo_texture_state.Pass();
150 PassPtr mojo_pass = Pass::New();
151 mojo_pass->quads.push_back(mojo_texture_quad.Pass());
152 SharedQuadStatePtr mojo_sqs = SharedQuadState::New();
153 mojo_pass->shared_quad_states.push_back(mojo_sqs.Pass());
155 scoped_ptr<cc::RenderPass> pass = mojo_pass.To<scoped_ptr<cc::RenderPass> >();
157 EXPECT_FALSE(pass);
160 TEST_F(SurfaceLibQuadTest, TextureQuadEmptyBackgroundColor) {
161 QuadPtr mojo_texture_quad = Quad::New();
162 mojo_texture_quad->material = MATERIAL_TEXTURE_CONTENT;
163 TextureQuadStatePtr mojo_texture_state = TextureQuadState::New();
164 mojo_texture_state->vertex_opacity = mojo::Array<float>::New(4);
165 mojo_texture_quad->texture_quad_state = mojo_texture_state.Pass();
166 PassPtr mojo_pass = Pass::New();
167 mojo_pass->quads.push_back(mojo_texture_quad.Pass());
168 SharedQuadStatePtr mojo_sqs = SharedQuadState::New();
169 mojo_pass->shared_quad_states.push_back(mojo_sqs.Pass());
171 scoped_ptr<cc::RenderPass> pass = mojo_pass.To<scoped_ptr<cc::RenderPass> >();
172 EXPECT_FALSE(pass);
175 TEST(SurfaceLibTest, SharedQuadState) {
176 gfx::Transform content_to_target_transform;
177 content_to_target_transform.Scale3d(0.3f, 0.7f, 0.9f);
178 gfx::Size content_bounds(57, 39);
179 gfx::Rect visible_content_rect(3, 7, 28, 42);
180 gfx::Rect clip_rect(9, 12, 21, 31);
181 bool is_clipped = true;
182 float opacity = 0.65f;
183 int sorting_context_id = 13;
184 ::SkXfermode::Mode blend_mode = ::SkXfermode::kSrcOver_Mode;
185 scoped_ptr<cc::RenderPass> pass = cc::RenderPass::Create();
186 cc::SharedQuadState* sqs = pass->CreateAndAppendSharedQuadState();
187 sqs->SetAll(content_to_target_transform,
188 content_bounds,
189 visible_content_rect,
190 clip_rect,
191 is_clipped,
192 opacity,
193 blend_mode,
194 sorting_context_id);
196 SharedQuadStatePtr mojo_sqs = SharedQuadState::From(*sqs);
197 ASSERT_FALSE(mojo_sqs.is_null());
198 EXPECT_EQ(Transform::From(content_to_target_transform),
199 mojo_sqs->content_to_target_transform);
200 EXPECT_EQ(Size::From(content_bounds), mojo_sqs->content_bounds);
201 EXPECT_EQ(Rect::From(visible_content_rect), mojo_sqs->visible_content_rect);
202 EXPECT_EQ(Rect::From(clip_rect), mojo_sqs->clip_rect);
203 EXPECT_EQ(is_clipped, mojo_sqs->is_clipped);
204 EXPECT_EQ(opacity, mojo_sqs->opacity);
205 EXPECT_EQ(sorting_context_id, mojo_sqs->sorting_context_id);
208 TEST(SurfaceLibTest, RenderPass) {
209 scoped_ptr<cc::RenderPass> pass = cc::RenderPass::Create();
210 cc::RenderPassId pass_id(1, 6);
211 gfx::Rect output_rect(4, 9, 13, 71);
212 gfx::Rect damage_rect(9, 17, 41, 45);
213 gfx::Transform transform_to_root_target;
214 transform_to_root_target.SkewY(43.0);
215 bool has_transparent_background = false;
216 pass->SetAll(pass_id,
217 output_rect,
218 damage_rect,
219 transform_to_root_target,
220 has_transparent_background);
222 gfx::Transform content_to_target_transform;
223 content_to_target_transform.Scale3d(0.3f, 0.7f, 0.9f);
224 gfx::Size content_bounds(57, 39);
225 gfx::Rect visible_content_rect(3, 7, 28, 42);
226 gfx::Rect clip_rect(9, 12, 21, 31);
227 bool is_clipped = true;
228 float opacity = 0.65f;
229 int sorting_context_id = 13;
230 ::SkXfermode::Mode blend_mode = ::SkXfermode::kSrcOver_Mode;
231 cc::SharedQuadState* sqs = pass->CreateAndAppendSharedQuadState();
232 sqs->SetAll(content_to_target_transform,
233 content_bounds,
234 visible_content_rect,
235 clip_rect,
236 is_clipped,
237 opacity,
238 blend_mode,
239 sorting_context_id);
241 gfx::Rect rect(5, 7, 13, 19);
242 gfx::Rect opaque_rect(rect);
243 gfx::Rect visible_rect(9, 11, 5, 7);
244 bool needs_blending = false;
246 cc::SolidColorDrawQuad* color_quad =
247 pass->CreateAndAppendDrawQuad<cc::SolidColorDrawQuad>();
248 SkColor arbitrary_color = SK_ColorGREEN;
249 bool force_anti_aliasing_off = true;
250 color_quad->SetAll(pass->shared_quad_state_list.back(),
251 rect,
252 opaque_rect,
253 visible_rect,
254 needs_blending,
255 arbitrary_color,
256 force_anti_aliasing_off);
258 cc::SurfaceDrawQuad* surface_quad =
259 pass->CreateAndAppendDrawQuad<cc::SurfaceDrawQuad>();
260 cc::SurfaceId arbitrary_id(5);
261 surface_quad->SetAll(
262 sqs, rect, opaque_rect, visible_rect, needs_blending, arbitrary_id);
264 cc::TextureDrawQuad* texture_quad =
265 pass->CreateAndAppendDrawQuad<cc::TextureDrawQuad>();
266 unsigned resource_id = 9;
267 bool premultiplied_alpha = true;
268 gfx::PointF uv_top_left(1.7f, 2.1f);
269 gfx::PointF uv_bottom_right(-7.f, 16.3f);
270 SkColor background_color = SK_ColorYELLOW;
271 float vertex_opacity[4] = {0.1f, 0.5f, 0.4f, 0.8f};
272 bool flipped = false;
273 bool nearest_neighbor = false;
274 texture_quad->SetAll(sqs,
275 rect,
276 opaque_rect,
277 visible_rect,
278 needs_blending,
279 resource_id,
280 premultiplied_alpha,
281 uv_top_left,
282 uv_bottom_right,
283 background_color,
284 vertex_opacity,
285 flipped,
286 nearest_neighbor);
288 PassPtr mojo_pass = Pass::From(*pass);
289 ASSERT_FALSE(mojo_pass.is_null());
290 EXPECT_EQ(6, mojo_pass->id);
291 EXPECT_EQ(Rect::From(output_rect), mojo_pass->output_rect);
292 EXPECT_EQ(Rect::From(damage_rect), mojo_pass->damage_rect);
293 EXPECT_EQ(Transform::From(transform_to_root_target),
294 mojo_pass->transform_to_root_target);
295 EXPECT_EQ(has_transparent_background, mojo_pass->has_transparent_background);
296 ASSERT_EQ(1u, mojo_pass->shared_quad_states.size());
297 ASSERT_EQ(3u, mojo_pass->quads.size());
298 EXPECT_EQ(0u, mojo_pass->quads[0]->shared_quad_state_index);
300 scoped_ptr<cc::RenderPass> round_trip_pass =
301 mojo_pass.To<scoped_ptr<cc::RenderPass> >();
302 EXPECT_EQ(pass_id, round_trip_pass->id);
303 EXPECT_EQ(output_rect, round_trip_pass->output_rect);
304 EXPECT_EQ(damage_rect, round_trip_pass->damage_rect);
305 EXPECT_EQ(transform_to_root_target,
306 round_trip_pass->transform_to_root_target);
307 EXPECT_EQ(has_transparent_background,
308 round_trip_pass->has_transparent_background);
309 ASSERT_EQ(1u, round_trip_pass->shared_quad_state_list.size());
310 ASSERT_EQ(3u, round_trip_pass->quad_list.size());
311 EXPECT_EQ(round_trip_pass->shared_quad_state_list.front(),
312 round_trip_pass->quad_list.front()->shared_quad_state);
314 cc::SharedQuadState* round_trip_sqs =
315 round_trip_pass->shared_quad_state_list.front();
316 EXPECT_EQ(content_to_target_transform,
317 round_trip_sqs->content_to_target_transform);
318 EXPECT_EQ(content_bounds, round_trip_sqs->content_bounds);
319 EXPECT_EQ(visible_content_rect, round_trip_sqs->visible_content_rect);
320 EXPECT_EQ(clip_rect, round_trip_sqs->clip_rect);
321 EXPECT_EQ(is_clipped, round_trip_sqs->is_clipped);
322 EXPECT_EQ(opacity, round_trip_sqs->opacity);
323 EXPECT_EQ(sorting_context_id, round_trip_sqs->sorting_context_id);
325 cc::DrawQuad* round_trip_quad = round_trip_pass->quad_list.front();
326 // First is solid color quad.
327 ASSERT_EQ(cc::DrawQuad::SOLID_COLOR, round_trip_quad->material);
328 EXPECT_EQ(rect, round_trip_quad->rect);
329 EXPECT_EQ(opaque_rect, round_trip_quad->opaque_rect);
330 EXPECT_EQ(visible_rect, round_trip_quad->visible_rect);
331 EXPECT_EQ(needs_blending, round_trip_quad->needs_blending);
332 const cc::SolidColorDrawQuad* round_trip_color_quad =
333 cc::SolidColorDrawQuad::MaterialCast(round_trip_quad);
334 EXPECT_EQ(arbitrary_color, round_trip_color_quad->color);
335 EXPECT_EQ(force_anti_aliasing_off,
336 round_trip_color_quad->force_anti_aliasing_off);
338 round_trip_quad = round_trip_pass->quad_list.ElementAt(1);
339 // Second is surface quad.
340 ASSERT_EQ(cc::DrawQuad::SURFACE_CONTENT, round_trip_quad->material);
341 const cc::SurfaceDrawQuad* round_trip_surface_quad =
342 cc::SurfaceDrawQuad::MaterialCast(round_trip_quad);
343 EXPECT_EQ(arbitrary_id, round_trip_surface_quad->surface_id);
345 round_trip_quad = round_trip_pass->quad_list.ElementAt(2);
346 // Third is texture quad.
347 ASSERT_EQ(cc::DrawQuad::TEXTURE_CONTENT, round_trip_quad->material);
348 const cc::TextureDrawQuad* round_trip_texture_quad =
349 cc::TextureDrawQuad::MaterialCast(round_trip_quad);
350 EXPECT_EQ(resource_id, round_trip_texture_quad->resource_id);
351 EXPECT_EQ(premultiplied_alpha, round_trip_texture_quad->premultiplied_alpha);
352 EXPECT_EQ(uv_top_left, round_trip_texture_quad->uv_top_left);
353 EXPECT_EQ(uv_bottom_right, round_trip_texture_quad->uv_bottom_right);
354 EXPECT_EQ(background_color, round_trip_texture_quad->background_color);
355 for (size_t i = 0; i < 4; ++i) {
356 EXPECT_EQ(vertex_opacity[i], round_trip_texture_quad->vertex_opacity[i])
357 << i;
359 EXPECT_EQ(flipped, round_trip_texture_quad->flipped);
362 TEST(SurfaceLibTest, Mailbox) {
363 gpu::Mailbox mailbox;
364 mailbox.Generate();
366 MailboxPtr mojo_mailbox = Mailbox::From(mailbox);
367 EXPECT_EQ(0, memcmp(mailbox.name, &mojo_mailbox->name.storage()[0], 64));
369 gpu::Mailbox round_trip_mailbox = mojo_mailbox.To<gpu::Mailbox>();
370 EXPECT_EQ(mailbox, round_trip_mailbox);
373 TEST(SurfaceLibTest, MailboxEmptyName) {
374 MailboxPtr mojo_mailbox = Mailbox::New();
376 gpu::Mailbox converted_mailbox = mojo_mailbox.To<gpu::Mailbox>();
377 EXPECT_TRUE(converted_mailbox.IsZero());
380 TEST(SurfaceLibTest, MailboxHolder) {
381 gpu::Mailbox mailbox;
382 mailbox.Generate();
383 uint32_t texture_target = GL_TEXTURE_2D;
384 uint32_t sync_point = 7u;
385 gpu::MailboxHolder holder(mailbox, texture_target, sync_point);
387 MailboxHolderPtr mojo_holder = MailboxHolder::From(holder);
388 EXPECT_EQ(texture_target, mojo_holder->texture_target);
389 EXPECT_EQ(sync_point, mojo_holder->sync_point);
391 gpu::MailboxHolder round_trip_holder = mojo_holder.To<gpu::MailboxHolder>();
392 EXPECT_EQ(mailbox, round_trip_holder.mailbox);
393 EXPECT_EQ(texture_target, round_trip_holder.texture_target);
394 EXPECT_EQ(sync_point, round_trip_holder.sync_point);
397 TEST(SurfaceLibTest, TransferableResource) {
398 uint32_t id = 7u;
399 cc::ResourceFormat format = cc::BGRA_8888;
400 uint32_t filter = 123u;
401 gfx::Size size(17, 18);
402 gpu::MailboxHolder mailbox_holder;
403 bool is_repeated = false;
405 bool is_software = false;
406 cc::TransferableResource resource;
407 resource.id = id;
408 resource.format = format;
409 resource.filter = filter;
410 resource.size = size;
411 resource.mailbox_holder = mailbox_holder;
412 resource.is_repeated = is_repeated;
413 resource.is_software = is_software;
415 TransferableResourcePtr mojo_resource = TransferableResource::From(resource);
416 EXPECT_EQ(id, mojo_resource->id);
417 EXPECT_EQ(static_cast<ResourceFormat>(format),
418 mojo_resource->format);
419 EXPECT_EQ(filter, mojo_resource->filter);
420 EXPECT_EQ(Size::From(size), mojo_resource->size);
421 EXPECT_EQ(is_repeated, mojo_resource->is_repeated);
422 EXPECT_EQ(is_software, mojo_resource->is_software);
424 cc::TransferableResource round_trip_resource =
425 mojo_resource.To<cc::TransferableResource>();
426 EXPECT_EQ(id, round_trip_resource.id);
427 EXPECT_EQ(format, round_trip_resource.format);
428 EXPECT_EQ(filter, round_trip_resource.filter);
429 EXPECT_EQ(size, round_trip_resource.size);
430 EXPECT_EQ(mailbox_holder.mailbox, round_trip_resource.mailbox_holder.mailbox);
431 EXPECT_EQ(mailbox_holder.texture_target,
432 round_trip_resource.mailbox_holder.texture_target);
433 EXPECT_EQ(mailbox_holder.sync_point,
434 round_trip_resource.mailbox_holder.sync_point);
435 EXPECT_EQ(is_repeated, round_trip_resource.is_repeated);
436 EXPECT_EQ(is_software, round_trip_resource.is_software);
439 TEST(SurfaceLibTest, ReturnedResource) {
440 uint32_t id = 5u;
441 uint32_t sync_point = 24u;
442 int count = 2;
443 bool lost = false;
444 cc::ReturnedResource resource;
445 resource.id = id;
446 resource.sync_point = sync_point;
447 resource.count = count;
448 resource.lost = lost;
450 ReturnedResourcePtr mojo_resource = ReturnedResource::From(resource);
451 EXPECT_EQ(id, mojo_resource->id);
452 EXPECT_EQ(sync_point, mojo_resource->sync_point);
453 EXPECT_EQ(count, mojo_resource->count);
454 EXPECT_EQ(lost, mojo_resource->lost);
456 cc::ReturnedResource round_trip_resource =
457 mojo_resource.To<cc::ReturnedResource>();
458 EXPECT_EQ(id, round_trip_resource.id);
459 EXPECT_EQ(sync_point, round_trip_resource.sync_point);
460 EXPECT_EQ(count, round_trip_resource.count);
461 EXPECT_EQ(lost, round_trip_resource.lost);
464 } // namespace
465 } // namespace mojo