1 // Copyright (c) 2012 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 "content/common/cc_messages.h"
9 #include "cc/compositor_frame.h"
10 #include "ipc/ipc_message.h"
11 #include "testing/gtest/include/gtest/gtest.h"
13 using cc::CheckerboardDrawQuad
;
14 using cc::DelegatedFrameData
;
15 using cc::DebugBorderDrawQuad
;
17 using cc::IOSurfaceDrawQuad
;
19 using cc::RenderPassDrawQuad
;
20 using cc::ResourceProvider
;
21 using cc::SharedQuadState
;
22 using cc::SolidColorDrawQuad
;
23 using cc::TextureDrawQuad
;
24 using cc::TileDrawQuad
;
25 using cc::TransferableResource
;
26 using cc::StreamVideoDrawQuad
;
27 using cc::VideoLayerImpl
;
28 using cc::YUVVideoDrawQuad
;
30 using WebKit::WebFilterOperation
;
31 using WebKit::WebFilterOperations
;
36 class CCMessagesTest
: public testing::Test
{
38 void Compare(const RenderPass
* a
, const RenderPass
* b
) {
39 EXPECT_EQ(a
->id
, b
->id
);
40 EXPECT_EQ(a
->output_rect
.ToString(), b
->output_rect
.ToString());
41 EXPECT_EQ(a
->damage_rect
.ToString(), b
->damage_rect
.ToString());
42 EXPECT_EQ(a
->transform_to_root_target
, b
->transform_to_root_target
);
43 EXPECT_EQ(a
->has_transparent_background
, b
->has_transparent_background
);
44 EXPECT_EQ(a
->has_occlusion_from_outside_target_surface
,
45 b
->has_occlusion_from_outside_target_surface
);
46 EXPECT_EQ(a
->filters
, b
->filters
);
47 EXPECT_EQ(a
->filter
, b
->filter
);
48 EXPECT_EQ(a
->background_filters
, b
->background_filters
);
51 void Compare(const SharedQuadState
* a
, const SharedQuadState
* b
) {
52 EXPECT_EQ(a
->content_to_target_transform
, b
->content_to_target_transform
);
53 EXPECT_EQ(a
->visible_content_rect
, b
->visible_content_rect
);
54 EXPECT_EQ(a
->clipped_rect_in_target
, b
->clipped_rect_in_target
);
55 EXPECT_EQ(a
->clip_rect
, b
->clip_rect
);
56 EXPECT_EQ(a
->is_clipped
, b
->is_clipped
);
57 EXPECT_EQ(a
->opacity
, b
->opacity
);
60 void Compare(const DrawQuad
* a
, const DrawQuad
* b
) {
61 ASSERT_NE(DrawQuad::INVALID
, a
->material
);
62 ASSERT_EQ(a
->material
, b
->material
);
63 EXPECT_EQ(a
->rect
.ToString(), b
->rect
.ToString());
64 EXPECT_EQ(a
->opaque_rect
.ToString(), b
->opaque_rect
.ToString());
65 EXPECT_EQ(a
->visible_rect
.ToString(), b
->visible_rect
.ToString());
66 EXPECT_EQ(a
->needs_blending
, b
->needs_blending
);
68 Compare(a
->shared_quad_state
, b
->shared_quad_state
);
70 switch (a
->material
) {
71 case DrawQuad::CHECKERBOARD
:
72 Compare(CheckerboardDrawQuad::MaterialCast(a
),
73 CheckerboardDrawQuad::MaterialCast(b
));
75 case DrawQuad::DEBUG_BORDER
:
76 Compare(DebugBorderDrawQuad::MaterialCast(a
),
77 DebugBorderDrawQuad::MaterialCast(b
));
79 case DrawQuad::IO_SURFACE_CONTENT
:
80 Compare(IOSurfaceDrawQuad::MaterialCast(a
),
81 IOSurfaceDrawQuad::MaterialCast(b
));
83 case DrawQuad::RENDER_PASS
:
84 Compare(RenderPassDrawQuad::MaterialCast(a
),
85 RenderPassDrawQuad::MaterialCast(b
));
87 case DrawQuad::TEXTURE_CONTENT
:
88 Compare(TextureDrawQuad::MaterialCast(a
),
89 TextureDrawQuad::MaterialCast(b
));
91 case DrawQuad::TILED_CONTENT
:
92 Compare(TileDrawQuad::MaterialCast(a
),
93 TileDrawQuad::MaterialCast(b
));
95 case DrawQuad::SOLID_COLOR
:
96 Compare(SolidColorDrawQuad::MaterialCast(a
),
97 SolidColorDrawQuad::MaterialCast(b
));
99 case DrawQuad::STREAM_VIDEO_CONTENT
:
100 Compare(StreamVideoDrawQuad::MaterialCast(a
),
101 StreamVideoDrawQuad::MaterialCast(b
));
103 case DrawQuad::YUV_VIDEO_CONTENT
:
104 Compare(YUVVideoDrawQuad::MaterialCast(a
),
105 YUVVideoDrawQuad::MaterialCast(b
));
107 case DrawQuad::INVALID
:
112 void Compare(const CheckerboardDrawQuad
* a
, const CheckerboardDrawQuad
* b
) {
113 EXPECT_EQ(a
->color
, b
->color
);
116 void Compare(const DebugBorderDrawQuad
* a
, const DebugBorderDrawQuad
* b
) {
117 EXPECT_EQ(a
->color
, b
->color
);
118 EXPECT_EQ(a
->width
, b
->width
);
121 void Compare(const IOSurfaceDrawQuad
* a
, const IOSurfaceDrawQuad
* b
) {
122 EXPECT_EQ(a
->io_surface_size
.ToString(), b
->io_surface_size
.ToString());
123 EXPECT_EQ(a
->io_surface_texture_id
, b
->io_surface_texture_id
);
124 EXPECT_EQ(a
->orientation
, b
->orientation
);
127 void Compare(const RenderPassDrawQuad
* a
, const RenderPassDrawQuad
* b
) {
128 EXPECT_EQ(a
->is_replica
, b
->is_replica
);
129 EXPECT_EQ(a
->mask_resource_id
, b
->mask_resource_id
);
130 EXPECT_EQ(a
->contents_changed_since_last_frame
,
131 b
->contents_changed_since_last_frame
);
132 EXPECT_EQ(a
->mask_uv_rect
.ToString(), b
->mask_uv_rect
.ToString());
135 void Compare(const SolidColorDrawQuad
* a
, const SolidColorDrawQuad
* b
) {
136 EXPECT_EQ(a
->color
, b
->color
);
139 void Compare(const StreamVideoDrawQuad
* a
, const StreamVideoDrawQuad
* b
) {
140 EXPECT_EQ(a
->texture_id
, b
->texture_id
);
141 EXPECT_EQ(a
->matrix
, b
->matrix
);
144 void Compare(const TextureDrawQuad
* a
, const TextureDrawQuad
* b
) {
145 EXPECT_EQ(a
->resource_id
, b
->resource_id
);
146 EXPECT_EQ(a
->premultiplied_alpha
, b
->premultiplied_alpha
);
147 EXPECT_EQ(a
->uv_rect
, b
->uv_rect
);
148 EXPECT_EQ(a
->flipped
, b
->flipped
);
151 void Compare(const TileDrawQuad
* a
, const TileDrawQuad
* b
) {
152 EXPECT_EQ(a
->resource_id
, b
->resource_id
);
153 EXPECT_EQ(a
->tex_coord_rect
, b
->tex_coord_rect
);
154 EXPECT_EQ(a
->texture_size
, b
->texture_size
);
155 EXPECT_EQ(a
->swizzle_contents
, b
->swizzle_contents
);
156 EXPECT_EQ(a
->left_edge_aa
, b
->left_edge_aa
);
157 EXPECT_EQ(a
->top_edge_aa
, b
->top_edge_aa
);
158 EXPECT_EQ(a
->right_edge_aa
, b
->right_edge_aa
);
159 EXPECT_EQ(a
->bottom_edge_aa
, b
->bottom_edge_aa
);
162 void Compare(const YUVVideoDrawQuad
* a
, const YUVVideoDrawQuad
* b
) {
163 EXPECT_EQ(a
->tex_scale
, b
->tex_scale
);
164 EXPECT_EQ(a
->y_plane
.resourceId
, b
->y_plane
.resourceId
);
165 EXPECT_EQ(a
->y_plane
.size
.ToString(), b
->y_plane
.size
.ToString());
166 EXPECT_EQ(a
->y_plane
.format
, b
->y_plane
.format
);
167 EXPECT_EQ(a
->u_plane
.resourceId
, b
->u_plane
.resourceId
);
168 EXPECT_EQ(a
->u_plane
.size
.ToString(), b
->u_plane
.size
.ToString());
169 EXPECT_EQ(a
->u_plane
.format
, b
->u_plane
.format
);
170 EXPECT_EQ(a
->v_plane
.resourceId
, b
->v_plane
.resourceId
);
171 EXPECT_EQ(a
->v_plane
.size
.ToString(), b
->v_plane
.size
.ToString());
172 EXPECT_EQ(a
->v_plane
.format
, b
->v_plane
.format
);
175 void Compare(const TransferableResource
& a
, const TransferableResource
& b
) {
176 EXPECT_EQ(a
.id
, b
.id
);
177 EXPECT_EQ(a
.format
, b
.format
);
178 EXPECT_EQ(a
.size
.ToString(), b
.size
.ToString());
179 for (size_t i
= 0; i
< arraysize(a
.mailbox
.name
); ++i
)
180 EXPECT_EQ(a
.mailbox
.name
[i
], b
.mailbox
.name
[i
]);
184 TEST_F(CCMessagesTest
, AllQuads
) {
185 IPC::Message
msg(1, 2, IPC::Message::PRIORITY_NORMAL
);
187 Transform arbitrary_matrix
;
188 arbitrary_matrix
.Scale(3, 3);
189 arbitrary_matrix
.Translate(-5, 20);
190 arbitrary_matrix
.Rotate(15);
191 gfx::Rect
arbitrary_rect1(-5, 9, 3, 15);
192 gfx::Rect
arbitrary_rect2(40, 23, 11, 7);
193 gfx::Rect
arbitrary_rect3(7, -53, 22, 19);
194 gfx::Size
arbitrary_size1(15, 19);
195 gfx::Size
arbitrary_size2(3, 99);
196 gfx::Size
arbitrary_size3(75, 1281);
197 gfx::RectF
arbitrary_rectf1(4.2f
, -922.1f
, 15.6f
, 29.5f
);
198 gfx::SizeF
arbitrary_sizef1(15.2f
, 104.6f
);
199 float arbitrary_float1
= 0.7f
;
200 float arbitrary_float2
= 0.3f
;
201 float arbitrary_float3
= 0.9f
;
202 bool arbitrary_bool1
= true;
203 bool arbitrary_bool2
= false;
204 int arbitrary_int
= 5;
205 SkColor arbitrary_color
= SkColorSetARGB(25, 36, 47, 58);
206 IOSurfaceDrawQuad::Orientation arbitrary_orientation
=
207 IOSurfaceDrawQuad::UNFLIPPED
;
208 RenderPass::Id
arbitrary_id(10, 14);
209 ResourceProvider::ResourceId arbitrary_resourceid
= 55;
211 VideoLayerImpl::FramePlane arbitrary_plane1
;
212 arbitrary_plane1
.resourceId
= arbitrary_resourceid
;
213 arbitrary_plane1
.size
= arbitrary_size1
;
214 arbitrary_plane1
.format
= arbitrary_int
;
216 VideoLayerImpl::FramePlane arbitrary_plane2
;
217 arbitrary_plane2
.resourceId
= arbitrary_resourceid
;
218 arbitrary_plane2
.size
= arbitrary_size2
;
219 arbitrary_plane2
.format
= arbitrary_int
;
221 VideoLayerImpl::FramePlane arbitrary_plane3
;
222 arbitrary_plane3
.resourceId
= arbitrary_resourceid
;
223 arbitrary_plane3
.size
= arbitrary_size3
;
224 arbitrary_plane3
.format
= arbitrary_int
;
226 WebFilterOperations arbitrary_filters1
;
227 arbitrary_filters1
.append(WebFilterOperation::createGrayscaleFilter(
230 WebFilterOperations arbitrary_filters2
;
231 arbitrary_filters2
.append(WebFilterOperation::createBrightnessFilter(
234 // TODO(danakj): filter is not serialized.
235 skia::RefPtr
<SkImageFilter
> arbitrary_filter
;
237 scoped_ptr
<SharedQuadState
> shared_state1_in
= SharedQuadState::Create();
238 shared_state1_in
->SetAll(arbitrary_matrix
,
244 scoped_ptr
<SharedQuadState
> shared_state1_cmp
= shared_state1_in
->Copy();
246 scoped_ptr
<CheckerboardDrawQuad
> checkerboard_in
=
247 CheckerboardDrawQuad::Create();
248 checkerboard_in
->SetAll(shared_state1_in
.get(),
254 scoped_ptr
<DrawQuad
> checkerboard_cmp
= checkerboard_in
->Copy(
255 checkerboard_in
->shared_quad_state
);
257 scoped_ptr
<DebugBorderDrawQuad
> debugborder_in
=
258 DebugBorderDrawQuad::Create();
259 debugborder_in
->SetAll(shared_state1_in
.get(),
266 scoped_ptr
<DrawQuad
> debugborder_cmp
= debugborder_in
->Copy(
267 debugborder_in
->shared_quad_state
);
269 scoped_ptr
<IOSurfaceDrawQuad
> iosurface_in
=
270 IOSurfaceDrawQuad::Create();
271 iosurface_in
->SetAll(shared_state1_in
.get(),
278 arbitrary_orientation
);
279 scoped_ptr
<DrawQuad
> iosurface_cmp
= iosurface_in
->Copy(
280 iosurface_in
->shared_quad_state
);
282 scoped_ptr
<RenderPassDrawQuad
> renderpass_in
=
283 RenderPassDrawQuad::Create();
284 renderpass_in
->SetAll(shared_state1_in
.get(),
291 arbitrary_resourceid
,
294 scoped_ptr
<RenderPassDrawQuad
> renderpass_cmp
= renderpass_in
->Copy(
295 renderpass_in
->shared_quad_state
, renderpass_in
->render_pass_id
);
297 scoped_ptr
<SharedQuadState
> shared_state2_in
= SharedQuadState::Create();
298 shared_state2_in
->SetAll(arbitrary_matrix
,
304 scoped_ptr
<SharedQuadState
> shared_state2_cmp
= shared_state2_in
->Copy();
306 scoped_ptr
<SharedQuadState
> shared_state3_in
= SharedQuadState::Create();
307 shared_state3_in
->SetAll(arbitrary_matrix
,
313 scoped_ptr
<SharedQuadState
> shared_state3_cmp
= shared_state3_in
->Copy();
315 scoped_ptr
<SolidColorDrawQuad
> solidcolor_in
=
316 SolidColorDrawQuad::Create();
317 solidcolor_in
->SetAll(shared_state1_in
.get(),
323 scoped_ptr
<DrawQuad
> solidcolor_cmp
= solidcolor_in
->Copy(
324 solidcolor_in
->shared_quad_state
);
326 scoped_ptr
<StreamVideoDrawQuad
> streamvideo_in
=
327 StreamVideoDrawQuad::Create();
328 streamvideo_in
->SetAll(shared_state1_in
.get(),
335 scoped_ptr
<DrawQuad
> streamvideo_cmp
= streamvideo_in
->Copy(
336 streamvideo_in
->shared_quad_state
);
338 scoped_ptr
<YUVVideoDrawQuad
> yuvvideo_in
=
339 YUVVideoDrawQuad::Create();
340 yuvvideo_in
->SetAll(shared_state1_in
.get(),
349 scoped_ptr
<DrawQuad
> yuvvideo_cmp
= yuvvideo_in
->Copy(
350 yuvvideo_in
->shared_quad_state
);
352 scoped_ptr
<RenderPass
> pass_in
= RenderPass::Create();
353 pass_in
->SetAll(arbitrary_id
,
360 arbitrary_filter
, // TODO(danakj): filter is not serialized.
363 pass_in
->shared_quad_state_list
.append(shared_state1_in
.Pass());
364 pass_in
->quad_list
.append(checkerboard_in
.PassAs
<DrawQuad
>());
365 pass_in
->quad_list
.append(debugborder_in
.PassAs
<DrawQuad
>());
366 pass_in
->quad_list
.append(iosurface_in
.PassAs
<DrawQuad
>());
367 pass_in
->quad_list
.append(renderpass_in
.PassAs
<DrawQuad
>());
368 pass_in
->shared_quad_state_list
.append(shared_state2_in
.Pass());
369 pass_in
->shared_quad_state_list
.append(shared_state3_in
.Pass());
370 pass_in
->quad_list
.append(solidcolor_in
.PassAs
<DrawQuad
>());
371 pass_in
->quad_list
.append(streamvideo_in
.PassAs
<DrawQuad
>());
372 pass_in
->quad_list
.append(yuvvideo_in
.PassAs
<DrawQuad
>());
374 scoped_ptr
<RenderPass
> pass_cmp
= RenderPass::Create();
375 pass_cmp
->SetAll(arbitrary_id
,
382 arbitrary_filter
, // TODO(danakj): filter is not serialized.
385 pass_cmp
->shared_quad_state_list
.append(shared_state1_cmp
.Pass());
386 pass_cmp
->quad_list
.append(checkerboard_cmp
.PassAs
<DrawQuad
>());
387 pass_cmp
->quad_list
.append(debugborder_cmp
.PassAs
<DrawQuad
>());
388 pass_cmp
->quad_list
.append(iosurface_cmp
.PassAs
<DrawQuad
>());
389 pass_cmp
->quad_list
.append(renderpass_cmp
.PassAs
<DrawQuad
>());
390 pass_cmp
->shared_quad_state_list
.append(shared_state2_cmp
.Pass());
391 pass_cmp
->shared_quad_state_list
.append(shared_state3_cmp
.Pass());
392 pass_cmp
->quad_list
.append(solidcolor_cmp
.PassAs
<DrawQuad
>());
393 pass_cmp
->quad_list
.append(streamvideo_cmp
.PassAs
<DrawQuad
>());
394 pass_cmp
->quad_list
.append(yuvvideo_cmp
.PassAs
<DrawQuad
>());
396 // Make sure the in and cmp RenderPasses match.
397 Compare(pass_cmp
.get(), pass_in
.get());
398 ASSERT_EQ(3u, pass_in
->shared_quad_state_list
.size());
399 ASSERT_EQ(7u, pass_in
->quad_list
.size());
400 for (size_t i
= 0; i
< 3; ++i
) {
401 Compare(pass_cmp
->shared_quad_state_list
[i
],
402 pass_in
->shared_quad_state_list
[i
]);
404 for (size_t i
= 0; i
< 7; ++i
)
405 Compare(pass_cmp
->quad_list
[i
], pass_in
->quad_list
[i
]);
406 for (size_t i
= 1; i
< 7; ++i
) {
407 bool same_shared_quad_state_cmp
=
408 pass_cmp
->quad_list
[i
]->shared_quad_state
==
409 pass_cmp
->quad_list
[i
- 1]->shared_quad_state
;
410 bool same_shared_quad_state_in
=
411 pass_in
->quad_list
[i
]->shared_quad_state
==
412 pass_in
->quad_list
[i
- 1]->shared_quad_state
;
413 EXPECT_EQ(same_shared_quad_state_cmp
, same_shared_quad_state_in
);
416 DelegatedFrameData frame_in
;
417 frame_in
.size
= arbitrary_size1
;
418 frame_in
.render_pass_list
.append(pass_in
.Pass());
420 IPC::ParamTraits
<DelegatedFrameData
>::Write(&msg
, frame_in
);
422 DelegatedFrameData frame_out
;
423 PickleIterator
iter(msg
);
424 EXPECT_TRUE(IPC::ParamTraits
<DelegatedFrameData
>::Read(&msg
,
427 EXPECT_EQ(arbitrary_size1
, frame_out
.size
);
429 // Make sure the out and cmp RenderPasses match.
430 scoped_ptr
<RenderPass
> pass_out
= frame_out
.render_pass_list
.take(0);
431 Compare(pass_cmp
.get(), pass_out
.get());
432 ASSERT_EQ(3u, pass_out
->shared_quad_state_list
.size());
433 ASSERT_EQ(7u, pass_out
->quad_list
.size());
434 for (size_t i
= 0; i
< 3; ++i
) {
435 Compare(pass_cmp
->shared_quad_state_list
[i
],
436 pass_out
->shared_quad_state_list
[i
]);
438 for (size_t i
= 0; i
< 7; ++i
)
439 Compare(pass_cmp
->quad_list
[i
], pass_out
->quad_list
[i
]);
440 for (size_t i
= 1; i
< 7; ++i
) {
441 bool same_shared_quad_state_cmp
=
442 pass_cmp
->quad_list
[i
]->shared_quad_state
==
443 pass_cmp
->quad_list
[i
- 1]->shared_quad_state
;
444 bool same_shared_quad_state_out
=
445 pass_out
->quad_list
[i
]->shared_quad_state
==
446 pass_out
->quad_list
[i
- 1]->shared_quad_state
;
447 EXPECT_EQ(same_shared_quad_state_cmp
, same_shared_quad_state_out
);
451 TEST_F(CCMessagesTest
, Resources
) {
452 IPC::Message
msg(1, 2, IPC::Message::PRIORITY_NORMAL
);
453 gfx::Size
arbitrary_size(757, 1281);
454 unsigned int arbitrary_uint
= 71234838;
456 GLbyte arbitrary_mailbox1
[64] = {
457 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0,
458 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0,
459 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0,
463 GLbyte arbitrary_mailbox2
[64] = {
464 0, 9, 8, 7, 6, 5, 4, 3, 2, 1, 9, 7, 5, 3, 1, 2, 4, 6, 8, 0,
465 0, 9, 8, 7, 6, 5, 4, 3, 2, 1, 9, 7, 5, 3, 1, 2, 4, 6, 8, 0,
466 0, 9, 8, 7, 6, 5, 4, 3, 2, 1, 9, 7, 5, 3, 1, 2, 4, 6, 8, 0,
470 TransferableResource arbitrary_resource1
;
471 arbitrary_resource1
.id
= 2178312;
472 arbitrary_resource1
.format
= 7;
473 arbitrary_resource1
.size
= gfx::Size(37189, 123123);
474 arbitrary_resource1
.mailbox
.setName(arbitrary_mailbox1
);
476 TransferableResource arbitrary_resource2
;
477 arbitrary_resource2
.id
= 789132;
478 arbitrary_resource2
.format
= 30;
479 arbitrary_resource2
.size
= gfx::Size(89123, 23789);
480 arbitrary_resource2
.mailbox
.setName(arbitrary_mailbox2
);
482 DelegatedFrameData frame_in
;
483 frame_in
.size
= arbitrary_size
;
485 frame_in
.resource_list
.sync_point
= arbitrary_uint
;
486 frame_in
.resource_list
.resources
.push_back(arbitrary_resource1
);
487 frame_in
.resource_list
.resources
.push_back(arbitrary_resource2
);
489 IPC::ParamTraits
<DelegatedFrameData
>::Write(&msg
, frame_in
);
491 DelegatedFrameData frame_out
;
492 PickleIterator
iter(msg
);
493 EXPECT_TRUE(IPC::ParamTraits
<DelegatedFrameData
>::Read(&msg
,
496 EXPECT_EQ(arbitrary_size
.ToString(), frame_out
.size
.ToString());
497 EXPECT_EQ(arbitrary_uint
, frame_out
.resource_list
.sync_point
);
499 EXPECT_EQ(2u, frame_out
.resource_list
.resources
.size());
500 Compare(arbitrary_resource1
, frame_out
.resource_list
.resources
[0]);
501 Compare(arbitrary_resource2
, frame_out
.resource_list
.resources
[1]);
505 } // namespace content