Clean the usage of QuadList (Const)Iterator
[chromium-blink-merge.git] / mojo / services / public / cpp / surfaces / tests / surface_unittest.cc
blob9076e5fe95cb8df5b56f97eb18309f7e9639f000
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/services/public/cpp/geometry/geometry_type_converters.h"
12 #include "mojo/services/public/cpp/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"
17 namespace mojo {
18 namespace {
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 {
40 public:
41 SurfaceLibQuadTest()
42 : rect(5, 7, 13, 19),
43 opaque_rect(rect),
44 visible_rect(9, 11, 5, 7),
45 needs_blending(false) {
46 pass = cc::RenderPass::Create();
47 sqs = pass->CreateAndAppendSharedQuadState();
50 protected:
51 gfx::Rect rect;
52 gfx::Rect opaque_rect;
53 gfx::Rect visible_rect;
54 bool needs_blending;
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,
65 rect,
66 opaque_rect,
67 visible_rect,
68 needs_blending,
69 arbitrary_color,
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);
89 surface_quad->SetAll(
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 texture_quad->SetAll(sqs,
112 rect,
113 opaque_rect,
114 visible_rect,
115 needs_blending,
116 resource_id,
117 premultiplied_alpha,
118 uv_top_left,
119 uv_bottom_right,
120 background_color,
121 vertex_opacity,
122 flipped);
124 QuadPtr mojo_quad = Quad::From<cc::DrawQuad>(*texture_quad);
125 ASSERT_FALSE(mojo_quad.is_null());
126 EXPECT_EQ(MATERIAL_TEXTURE_CONTENT, mojo_quad->material);
127 ASSERT_TRUE(mojo_quad->texture_quad_state);
128 TextureQuadStatePtr& mojo_texture_state = mojo_quad->texture_quad_state;
129 EXPECT_EQ(resource_id, mojo_texture_state->resource_id);
130 EXPECT_EQ(premultiplied_alpha, mojo_texture_state->premultiplied_alpha);
131 EXPECT_EQ(PointF::From(uv_top_left), mojo_texture_state->uv_top_left);
132 EXPECT_EQ(PointF::From(uv_bottom_right), mojo_texture_state->uv_bottom_right);
133 EXPECT_EQ(Color::From(background_color),
134 mojo_texture_state->background_color);
135 for (size_t i = 0; i < 4; ++i) {
136 EXPECT_EQ(vertex_opacity[i], mojo_texture_state->vertex_opacity[i]) << i;
138 EXPECT_EQ(flipped, mojo_texture_state->flipped);
141 TEST_F(SurfaceLibQuadTest, TextureQuadEmptyVertexOpacity) {
142 QuadPtr mojo_texture_quad = Quad::New();
143 mojo_texture_quad->material = MATERIAL_TEXTURE_CONTENT;
144 TextureQuadStatePtr mojo_texture_state = TextureQuadState::New();
145 mojo_texture_state->background_color = Color::New();
146 mojo_texture_quad->texture_quad_state = mojo_texture_state.Pass();
147 PassPtr mojo_pass = Pass::New();
148 mojo_pass->quads.push_back(mojo_texture_quad.Pass());
149 SharedQuadStatePtr mojo_sqs = SharedQuadState::New();
150 mojo_pass->shared_quad_states.push_back(mojo_sqs.Pass());
152 scoped_ptr<cc::RenderPass> pass = mojo_pass.To<scoped_ptr<cc::RenderPass> >();
154 EXPECT_FALSE(pass);
157 TEST_F(SurfaceLibQuadTest, TextureQuadEmptyBackgroundColor) {
158 QuadPtr mojo_texture_quad = Quad::New();
159 mojo_texture_quad->material = MATERIAL_TEXTURE_CONTENT;
160 TextureQuadStatePtr mojo_texture_state = TextureQuadState::New();
161 mojo_texture_state->vertex_opacity = mojo::Array<float>::New(4);
162 mojo_texture_quad->texture_quad_state = mojo_texture_state.Pass();
163 PassPtr mojo_pass = Pass::New();
164 mojo_pass->quads.push_back(mojo_texture_quad.Pass());
165 SharedQuadStatePtr mojo_sqs = SharedQuadState::New();
166 mojo_pass->shared_quad_states.push_back(mojo_sqs.Pass());
168 scoped_ptr<cc::RenderPass> pass = mojo_pass.To<scoped_ptr<cc::RenderPass> >();
169 EXPECT_FALSE(pass);
172 TEST(SurfaceLibTest, SharedQuadState) {
173 gfx::Transform content_to_target_transform;
174 content_to_target_transform.Scale3d(0.3f, 0.7f, 0.9f);
175 gfx::Size content_bounds(57, 39);
176 gfx::Rect visible_content_rect(3, 7, 28, 42);
177 gfx::Rect clip_rect(9, 12, 21, 31);
178 bool is_clipped = true;
179 float opacity = 0.65f;
180 int sorting_context_id = 13;
181 ::SkXfermode::Mode blend_mode = ::SkXfermode::kSrcOver_Mode;
182 scoped_ptr<cc::RenderPass> pass = cc::RenderPass::Create();
183 cc::SharedQuadState* sqs = pass->CreateAndAppendSharedQuadState();
184 sqs->SetAll(content_to_target_transform,
185 content_bounds,
186 visible_content_rect,
187 clip_rect,
188 is_clipped,
189 opacity,
190 blend_mode,
191 sorting_context_id);
193 SharedQuadStatePtr mojo_sqs = SharedQuadState::From(*sqs);
194 ASSERT_FALSE(mojo_sqs.is_null());
195 EXPECT_EQ(Transform::From(content_to_target_transform),
196 mojo_sqs->content_to_target_transform);
197 EXPECT_EQ(Size::From(content_bounds), mojo_sqs->content_bounds);
198 EXPECT_EQ(Rect::From(visible_content_rect), mojo_sqs->visible_content_rect);
199 EXPECT_EQ(Rect::From(clip_rect), mojo_sqs->clip_rect);
200 EXPECT_EQ(is_clipped, mojo_sqs->is_clipped);
201 EXPECT_EQ(opacity, mojo_sqs->opacity);
202 EXPECT_EQ(sorting_context_id, mojo_sqs->sorting_context_id);
205 TEST(SurfaceLibTest, RenderPass) {
206 scoped_ptr<cc::RenderPass> pass = cc::RenderPass::Create();
207 cc::RenderPassId pass_id(1, 6);
208 gfx::Rect output_rect(4, 9, 13, 71);
209 gfx::Rect damage_rect(9, 17, 41, 45);
210 gfx::Transform transform_to_root_target;
211 transform_to_root_target.SkewY(43.0);
212 bool has_transparent_background = false;
213 pass->SetAll(pass_id,
214 output_rect,
215 damage_rect,
216 transform_to_root_target,
217 has_transparent_background);
219 gfx::Transform content_to_target_transform;
220 content_to_target_transform.Scale3d(0.3f, 0.7f, 0.9f);
221 gfx::Size content_bounds(57, 39);
222 gfx::Rect visible_content_rect(3, 7, 28, 42);
223 gfx::Rect clip_rect(9, 12, 21, 31);
224 bool is_clipped = true;
225 float opacity = 0.65f;
226 int sorting_context_id = 13;
227 ::SkXfermode::Mode blend_mode = ::SkXfermode::kSrcOver_Mode;
228 cc::SharedQuadState* sqs = pass->CreateAndAppendSharedQuadState();
229 sqs->SetAll(content_to_target_transform,
230 content_bounds,
231 visible_content_rect,
232 clip_rect,
233 is_clipped,
234 opacity,
235 blend_mode,
236 sorting_context_id);
238 gfx::Rect rect(5, 7, 13, 19);
239 gfx::Rect opaque_rect(rect);
240 gfx::Rect visible_rect(9, 11, 5, 7);
241 bool needs_blending = false;
243 cc::SolidColorDrawQuad* color_quad =
244 pass->CreateAndAppendDrawQuad<cc::SolidColorDrawQuad>();
245 SkColor arbitrary_color = SK_ColorGREEN;
246 bool force_anti_aliasing_off = true;
247 color_quad->SetAll(pass->shared_quad_state_list.back(),
248 rect,
249 opaque_rect,
250 visible_rect,
251 needs_blending,
252 arbitrary_color,
253 force_anti_aliasing_off);
255 cc::SurfaceDrawQuad* surface_quad =
256 pass->CreateAndAppendDrawQuad<cc::SurfaceDrawQuad>();
257 cc::SurfaceId arbitrary_id(5);
258 surface_quad->SetAll(
259 sqs, rect, opaque_rect, visible_rect, needs_blending, arbitrary_id);
261 cc::TextureDrawQuad* texture_quad =
262 pass->CreateAndAppendDrawQuad<cc::TextureDrawQuad>();
263 unsigned resource_id = 9;
264 bool premultiplied_alpha = true;
265 gfx::PointF uv_top_left(1.7f, 2.1f);
266 gfx::PointF uv_bottom_right(-7.f, 16.3f);
267 SkColor background_color = SK_ColorYELLOW;
268 float vertex_opacity[4] = {0.1f, 0.5f, 0.4f, 0.8f};
269 bool flipped = false;
270 texture_quad->SetAll(sqs,
271 rect,
272 opaque_rect,
273 visible_rect,
274 needs_blending,
275 resource_id,
276 premultiplied_alpha,
277 uv_top_left,
278 uv_bottom_right,
279 background_color,
280 vertex_opacity,
281 flipped);
283 PassPtr mojo_pass = Pass::From(*pass);
284 ASSERT_FALSE(mojo_pass.is_null());
285 EXPECT_EQ(6, mojo_pass->id);
286 EXPECT_EQ(Rect::From(output_rect), mojo_pass->output_rect);
287 EXPECT_EQ(Rect::From(damage_rect), mojo_pass->damage_rect);
288 EXPECT_EQ(Transform::From(transform_to_root_target),
289 mojo_pass->transform_to_root_target);
290 EXPECT_EQ(has_transparent_background, mojo_pass->has_transparent_background);
291 ASSERT_EQ(1u, mojo_pass->shared_quad_states.size());
292 ASSERT_EQ(3u, mojo_pass->quads.size());
293 EXPECT_EQ(0u, mojo_pass->quads[0]->shared_quad_state_index);
295 scoped_ptr<cc::RenderPass> round_trip_pass =
296 mojo_pass.To<scoped_ptr<cc::RenderPass> >();
297 EXPECT_EQ(pass_id, round_trip_pass->id);
298 EXPECT_EQ(output_rect, round_trip_pass->output_rect);
299 EXPECT_EQ(damage_rect, round_trip_pass->damage_rect);
300 EXPECT_EQ(transform_to_root_target,
301 round_trip_pass->transform_to_root_target);
302 EXPECT_EQ(has_transparent_background,
303 round_trip_pass->has_transparent_background);
304 ASSERT_EQ(1u, round_trip_pass->shared_quad_state_list.size());
305 ASSERT_EQ(3u, round_trip_pass->quad_list.size());
306 EXPECT_EQ(round_trip_pass->shared_quad_state_list.front(),
307 round_trip_pass->quad_list.front()->shared_quad_state);
309 cc::SharedQuadState* round_trip_sqs =
310 round_trip_pass->shared_quad_state_list.front();
311 EXPECT_EQ(content_to_target_transform,
312 round_trip_sqs->content_to_target_transform);
313 EXPECT_EQ(content_bounds, round_trip_sqs->content_bounds);
314 EXPECT_EQ(visible_content_rect, round_trip_sqs->visible_content_rect);
315 EXPECT_EQ(clip_rect, round_trip_sqs->clip_rect);
316 EXPECT_EQ(is_clipped, round_trip_sqs->is_clipped);
317 EXPECT_EQ(opacity, round_trip_sqs->opacity);
318 EXPECT_EQ(sorting_context_id, round_trip_sqs->sorting_context_id);
320 cc::DrawQuad* round_trip_quad = round_trip_pass->quad_list.front();
321 // First is solid color quad.
322 ASSERT_EQ(cc::DrawQuad::SOLID_COLOR, round_trip_quad->material);
323 EXPECT_EQ(rect, round_trip_quad->rect);
324 EXPECT_EQ(opaque_rect, round_trip_quad->opaque_rect);
325 EXPECT_EQ(visible_rect, round_trip_quad->visible_rect);
326 EXPECT_EQ(needs_blending, round_trip_quad->needs_blending);
327 const cc::SolidColorDrawQuad* round_trip_color_quad =
328 cc::SolidColorDrawQuad::MaterialCast(round_trip_quad);
329 EXPECT_EQ(arbitrary_color, round_trip_color_quad->color);
330 EXPECT_EQ(force_anti_aliasing_off,
331 round_trip_color_quad->force_anti_aliasing_off);
333 round_trip_quad = round_trip_pass->quad_list.ElementAt(1);
334 // Second is surface quad.
335 ASSERT_EQ(cc::DrawQuad::SURFACE_CONTENT, round_trip_quad->material);
336 const cc::SurfaceDrawQuad* round_trip_surface_quad =
337 cc::SurfaceDrawQuad::MaterialCast(round_trip_quad);
338 EXPECT_EQ(arbitrary_id, round_trip_surface_quad->surface_id);
340 round_trip_quad = round_trip_pass->quad_list.ElementAt(2);
341 // Third is texture quad.
342 ASSERT_EQ(cc::DrawQuad::TEXTURE_CONTENT, round_trip_quad->material);
343 const cc::TextureDrawQuad* round_trip_texture_quad =
344 cc::TextureDrawQuad::MaterialCast(round_trip_quad);
345 EXPECT_EQ(resource_id, round_trip_texture_quad->resource_id);
346 EXPECT_EQ(premultiplied_alpha, round_trip_texture_quad->premultiplied_alpha);
347 EXPECT_EQ(uv_top_left, round_trip_texture_quad->uv_top_left);
348 EXPECT_EQ(uv_bottom_right, round_trip_texture_quad->uv_bottom_right);
349 EXPECT_EQ(background_color, round_trip_texture_quad->background_color);
350 for (size_t i = 0; i < 4; ++i) {
351 EXPECT_EQ(vertex_opacity[i], round_trip_texture_quad->vertex_opacity[i])
352 << i;
354 EXPECT_EQ(flipped, round_trip_texture_quad->flipped);
357 TEST(SurfaceLibTest, Mailbox) {
358 gpu::Mailbox mailbox;
359 mailbox.Generate();
361 MailboxPtr mojo_mailbox = Mailbox::From(mailbox);
362 EXPECT_EQ(0, memcmp(mailbox.name, &mojo_mailbox->name.storage()[0], 64));
364 gpu::Mailbox round_trip_mailbox = mojo_mailbox.To<gpu::Mailbox>();
365 EXPECT_EQ(mailbox, round_trip_mailbox);
368 TEST(SurfaceLibTest, MailboxEmptyName) {
369 MailboxPtr mojo_mailbox = Mailbox::New();
371 gpu::Mailbox converted_mailbox = mojo_mailbox.To<gpu::Mailbox>();
372 EXPECT_TRUE(converted_mailbox.IsZero());
375 TEST(SurfaceLibTest, MailboxHolder) {
376 gpu::Mailbox mailbox;
377 mailbox.Generate();
378 uint32_t texture_target = GL_TEXTURE_2D;
379 uint32_t sync_point = 7u;
380 gpu::MailboxHolder holder(mailbox, texture_target, sync_point);
382 MailboxHolderPtr mojo_holder = MailboxHolder::From(holder);
383 EXPECT_EQ(texture_target, mojo_holder->texture_target);
384 EXPECT_EQ(sync_point, mojo_holder->sync_point);
386 gpu::MailboxHolder round_trip_holder = mojo_holder.To<gpu::MailboxHolder>();
387 EXPECT_EQ(mailbox, round_trip_holder.mailbox);
388 EXPECT_EQ(texture_target, round_trip_holder.texture_target);
389 EXPECT_EQ(sync_point, round_trip_holder.sync_point);
392 TEST(SurfaceLibTest, TransferableResource) {
393 uint32_t id = 7u;
394 cc::ResourceFormat format = cc::BGRA_8888;
395 uint32_t filter = 123u;
396 gfx::Size size(17, 18);
397 gpu::MailboxHolder mailbox_holder;
398 bool is_repeated = false;
400 bool is_software = false;
401 cc::TransferableResource resource;
402 resource.id = id;
403 resource.format = format;
404 resource.filter = filter;
405 resource.size = size;
406 resource.mailbox_holder = mailbox_holder;
407 resource.is_repeated = is_repeated;
408 resource.is_software = is_software;
410 TransferableResourcePtr mojo_resource = TransferableResource::From(resource);
411 EXPECT_EQ(id, mojo_resource->id);
412 EXPECT_EQ(static_cast<ResourceFormat>(format),
413 mojo_resource->format);
414 EXPECT_EQ(filter, mojo_resource->filter);
415 EXPECT_EQ(Size::From(size), mojo_resource->size);
416 EXPECT_EQ(is_repeated, mojo_resource->is_repeated);
417 EXPECT_EQ(is_software, mojo_resource->is_software);
419 cc::TransferableResource round_trip_resource =
420 mojo_resource.To<cc::TransferableResource>();
421 EXPECT_EQ(id, round_trip_resource.id);
422 EXPECT_EQ(format, round_trip_resource.format);
423 EXPECT_EQ(filter, round_trip_resource.filter);
424 EXPECT_EQ(size, round_trip_resource.size);
425 EXPECT_EQ(mailbox_holder.mailbox, round_trip_resource.mailbox_holder.mailbox);
426 EXPECT_EQ(mailbox_holder.texture_target,
427 round_trip_resource.mailbox_holder.texture_target);
428 EXPECT_EQ(mailbox_holder.sync_point,
429 round_trip_resource.mailbox_holder.sync_point);
430 EXPECT_EQ(is_repeated, round_trip_resource.is_repeated);
431 EXPECT_EQ(is_software, round_trip_resource.is_software);
434 TEST(SurfaceLibTest, ReturnedResource) {
435 uint32_t id = 5u;
436 uint32_t sync_point = 24u;
437 int count = 2;
438 bool lost = false;
439 cc::ReturnedResource resource;
440 resource.id = id;
441 resource.sync_point = sync_point;
442 resource.count = count;
443 resource.lost = lost;
445 ReturnedResourcePtr mojo_resource = ReturnedResource::From(resource);
446 EXPECT_EQ(id, mojo_resource->id);
447 EXPECT_EQ(sync_point, mojo_resource->sync_point);
448 EXPECT_EQ(count, mojo_resource->count);
449 EXPECT_EQ(lost, mojo_resource->lost);
451 cc::ReturnedResource round_trip_resource =
452 mojo_resource.To<cc::ReturnedResource>();
453 EXPECT_EQ(id, round_trip_resource.id);
454 EXPECT_EQ(sync_point, round_trip_resource.sync_point);
455 EXPECT_EQ(count, round_trip_resource.count);
456 EXPECT_EQ(lost, round_trip_resource.lost);
459 } // namespace
460 } // namespace mojo