Support swap promises that are pinned to a particular layer tree.
[chromium-blink-merge.git] / cc / layers / video_layer_impl_unittest.cc
blob28ba58acc2d4c38913002ff8adeec09f89f14bc8
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/layers/video_layer_impl.h"
7 #include "cc/layers/video_frame_provider_client_impl.h"
8 #include "cc/output/context_provider.h"
9 #include "cc/output/output_surface.h"
10 #include "cc/quads/draw_quad.h"
11 #include "cc/quads/yuv_video_draw_quad.h"
12 #include "cc/test/fake_video_frame_provider.h"
13 #include "cc/test/layer_test_common.h"
14 #include "cc/trees/single_thread_proxy.h"
15 #include "media/base/video_frame.h"
16 #include "testing/gtest/include/gtest/gtest.h"
18 namespace cc {
19 namespace {
21 // NOTE: We cannot use DebugScopedSetImplThreadAndMainThreadBlocked in these
22 // tests because it gets destroyed before the VideoLayerImpl is destroyed. This
23 // causes a DCHECK in VideoLayerImpl's destructor to fail.
24 static void DebugSetImplThreadAndMainThreadBlocked(Proxy* proxy) {
25 #if DCHECK_IS_ON()
26 proxy->SetCurrentThreadIsImplThread(true);
27 proxy->SetMainThreadBlocked(true);
28 #endif
31 TEST(VideoLayerImplTest, Occlusion) {
32 gfx::Size layer_size(1000, 1000);
33 gfx::Size viewport_size(1000, 1000);
35 LayerTestCommon::LayerImplTest impl;
36 DebugSetImplThreadAndMainThreadBlocked(impl.proxy());
38 scoped_refptr<media::VideoFrame> video_frame = media::VideoFrame::CreateFrame(
39 media::PIXEL_FORMAT_YV12, gfx::Size(10, 10), gfx::Rect(10, 10),
40 gfx::Size(10, 10), base::TimeDelta());
41 FakeVideoFrameProvider provider;
42 provider.set_frame(video_frame);
44 VideoLayerImpl* video_layer_impl =
45 impl.AddChildToRoot<VideoLayerImpl>(&provider, media::VIDEO_ROTATION_0);
46 video_layer_impl->SetBounds(layer_size);
47 video_layer_impl->SetDrawsContent(true);
49 impl.CalcDrawProps(viewport_size);
52 SCOPED_TRACE("No occlusion");
53 gfx::Rect occluded;
54 impl.AppendQuadsWithOcclusion(video_layer_impl, occluded);
56 LayerTestCommon::VerifyQuadsExactlyCoverRect(impl.quad_list(),
57 gfx::Rect(layer_size));
58 EXPECT_EQ(1u, impl.quad_list().size());
62 SCOPED_TRACE("Full occlusion");
63 gfx::Rect occluded(video_layer_impl->visible_layer_rect());
64 impl.AppendQuadsWithOcclusion(video_layer_impl, occluded);
66 LayerTestCommon::VerifyQuadsExactlyCoverRect(impl.quad_list(), gfx::Rect());
67 EXPECT_EQ(impl.quad_list().size(), 0u);
71 SCOPED_TRACE("Partial occlusion");
72 gfx::Rect occluded(200, 0, 800, 1000);
73 impl.AppendQuadsWithOcclusion(video_layer_impl, occluded);
75 size_t partially_occluded_count = 0;
76 LayerTestCommon::VerifyQuadsAreOccluded(
77 impl.quad_list(), occluded, &partially_occluded_count);
78 // The layer outputs one quad, which is partially occluded.
79 EXPECT_EQ(1u, impl.quad_list().size());
80 EXPECT_EQ(1u, partially_occluded_count);
84 TEST(VideoLayerImplTest, OccludesOtherLayers) {
85 gfx::Size layer_size(1000, 1000);
86 gfx::Rect visible(layer_size);
88 LayerTestCommon::LayerImplTest impl;
89 impl.host_impl()->SetViewportSize(layer_size);
90 DebugSetImplThreadAndMainThreadBlocked(impl.proxy());
91 auto active_tree = impl.host_impl()->active_tree();
93 // Create a video layer with no frame on top of another layer.
94 scoped_ptr<LayerImpl> layer_impl = LayerImpl::Create(active_tree, 3);
95 layer_impl->SetHasRenderSurface(true);
96 layer_impl->SetBounds(layer_size);
97 layer_impl->SetDrawsContent(true);
98 const auto& draw_properties = layer_impl->draw_properties();
100 FakeVideoFrameProvider provider;
101 scoped_ptr<VideoLayerImpl> video_layer_impl = VideoLayerImpl::Create(
102 active_tree, 4, &provider, media::VIDEO_ROTATION_0);
103 video_layer_impl->SetBounds(layer_size);
104 video_layer_impl->SetDrawsContent(true);
105 video_layer_impl->SetContentsOpaque(true);
107 layer_impl->AddChild(video_layer_impl.Pass());
108 active_tree->SetRootLayer(layer_impl.Pass());
110 active_tree->BuildPropertyTreesForTesting();
112 active_tree->UpdateDrawProperties(false);
114 // We don't have a frame yet, so the video doesn't occlude the layer below it.
115 EXPECT_FALSE(draw_properties.occlusion_in_content_space.IsOccluded(visible));
117 scoped_refptr<media::VideoFrame> video_frame = media::VideoFrame::CreateFrame(
118 media::PIXEL_FORMAT_YV12, gfx::Size(10, 10), gfx::Rect(10, 10),
119 gfx::Size(10, 10), base::TimeDelta());
120 provider.set_frame(video_frame);
121 active_tree->set_needs_update_draw_properties();
122 active_tree->UpdateDrawProperties(false);
124 // We have a frame now, so the video occludes the layer below it.
125 EXPECT_TRUE(draw_properties.occlusion_in_content_space.IsOccluded(visible));
128 TEST(VideoLayerImplTest, DidBecomeActiveShouldSetActiveVideoLayer) {
129 LayerTestCommon::LayerImplTest impl;
130 DebugSetImplThreadAndMainThreadBlocked(impl.proxy());
132 FakeVideoFrameProvider provider;
133 VideoLayerImpl* video_layer_impl =
134 impl.AddChildToRoot<VideoLayerImpl>(&provider, media::VIDEO_ROTATION_0);
136 VideoFrameProviderClientImpl* client =
137 static_cast<VideoFrameProviderClientImpl*>(provider.client());
138 ASSERT_TRUE(client);
140 EXPECT_FALSE(client->ActiveVideoLayer());
141 video_layer_impl->DidBecomeActive();
142 EXPECT_EQ(video_layer_impl, client->ActiveVideoLayer());
145 TEST(VideoLayerImplTest, Rotated0) {
146 gfx::Size layer_size(100, 50);
147 gfx::Size viewport_size(1000, 500);
149 LayerTestCommon::LayerImplTest impl;
150 DebugSetImplThreadAndMainThreadBlocked(impl.proxy());
152 scoped_refptr<media::VideoFrame> video_frame = media::VideoFrame::CreateFrame(
153 media::PIXEL_FORMAT_YV12, gfx::Size(20, 10), gfx::Rect(20, 10),
154 gfx::Size(20, 10), base::TimeDelta());
155 FakeVideoFrameProvider provider;
156 provider.set_frame(video_frame);
158 VideoLayerImpl* video_layer_impl =
159 impl.AddChildToRoot<VideoLayerImpl>(&provider, media::VIDEO_ROTATION_0);
160 video_layer_impl->SetBounds(layer_size);
161 video_layer_impl->SetDrawsContent(true);
163 impl.CalcDrawProps(viewport_size);
164 gfx::Rect occluded;
165 impl.AppendQuadsWithOcclusion(video_layer_impl, occluded);
167 EXPECT_EQ(1u, impl.quad_list().size());
169 gfx::Point3F p1(0, impl.quad_list().front()->rect.height(), 0);
170 gfx::Point3F p2(impl.quad_list().front()->rect.width(), 0, 0);
171 impl.quad_list()
172 .front()
173 ->shared_quad_state->quad_to_target_transform.TransformPoint(&p1);
174 impl.quad_list()
175 .front()
176 ->shared_quad_state->quad_to_target_transform.TransformPoint(&p2);
177 EXPECT_EQ(gfx::Point3F(0, 50, 0), p1);
178 EXPECT_EQ(gfx::Point3F(100, 0, 0), p2);
181 TEST(VideoLayerImplTest, Rotated90) {
182 gfx::Size layer_size(100, 50);
183 gfx::Size viewport_size(1000, 500);
185 LayerTestCommon::LayerImplTest impl;
186 DebugSetImplThreadAndMainThreadBlocked(impl.proxy());
188 scoped_refptr<media::VideoFrame> video_frame = media::VideoFrame::CreateFrame(
189 media::PIXEL_FORMAT_YV12, gfx::Size(20, 10), gfx::Rect(20, 10),
190 gfx::Size(20, 10), base::TimeDelta());
191 FakeVideoFrameProvider provider;
192 provider.set_frame(video_frame);
194 VideoLayerImpl* video_layer_impl =
195 impl.AddChildToRoot<VideoLayerImpl>(&provider, media::VIDEO_ROTATION_90);
196 video_layer_impl->SetBounds(layer_size);
197 video_layer_impl->SetDrawsContent(true);
199 impl.CalcDrawProps(viewport_size);
200 gfx::Rect occluded;
201 impl.AppendQuadsWithOcclusion(video_layer_impl, occluded);
203 EXPECT_EQ(1u, impl.quad_list().size());
205 gfx::Point3F p1(0, impl.quad_list().front()->rect.height(), 0);
206 gfx::Point3F p2(impl.quad_list().front()->rect.width(), 0, 0);
207 impl.quad_list()
208 .front()
209 ->shared_quad_state->quad_to_target_transform.TransformPoint(&p1);
210 impl.quad_list()
211 .front()
212 ->shared_quad_state->quad_to_target_transform.TransformPoint(&p2);
213 EXPECT_EQ(gfx::Point3F(0, 0, 0), p1);
214 EXPECT_EQ(gfx::Point3F(100, 50, 0), p2);
217 TEST(VideoLayerImplTest, Rotated180) {
218 gfx::Size layer_size(100, 50);
219 gfx::Size viewport_size(1000, 500);
221 LayerTestCommon::LayerImplTest impl;
222 DebugSetImplThreadAndMainThreadBlocked(impl.proxy());
224 scoped_refptr<media::VideoFrame> video_frame = media::VideoFrame::CreateFrame(
225 media::PIXEL_FORMAT_YV12, gfx::Size(20, 10), gfx::Rect(20, 10),
226 gfx::Size(20, 10), base::TimeDelta());
227 FakeVideoFrameProvider provider;
228 provider.set_frame(video_frame);
230 VideoLayerImpl* video_layer_impl =
231 impl.AddChildToRoot<VideoLayerImpl>(&provider, media::VIDEO_ROTATION_180);
232 video_layer_impl->SetBounds(layer_size);
233 video_layer_impl->SetDrawsContent(true);
235 impl.CalcDrawProps(viewport_size);
236 gfx::Rect occluded;
237 impl.AppendQuadsWithOcclusion(video_layer_impl, occluded);
239 EXPECT_EQ(1u, impl.quad_list().size());
241 gfx::Point3F p1(0, impl.quad_list().front()->rect.height(), 0);
242 gfx::Point3F p2(impl.quad_list().front()->rect.width(), 0, 0);
243 impl.quad_list()
244 .front()
245 ->shared_quad_state->quad_to_target_transform.TransformPoint(&p1);
246 impl.quad_list()
247 .front()
248 ->shared_quad_state->quad_to_target_transform.TransformPoint(&p2);
249 EXPECT_EQ(gfx::Point3F(100, 0, 0), p1);
250 EXPECT_EQ(gfx::Point3F(0, 50, 0), p2);
253 TEST(VideoLayerImplTest, Rotated270) {
254 gfx::Size layer_size(100, 50);
255 gfx::Size viewport_size(1000, 500);
257 LayerTestCommon::LayerImplTest impl;
258 DebugSetImplThreadAndMainThreadBlocked(impl.proxy());
260 scoped_refptr<media::VideoFrame> video_frame = media::VideoFrame::CreateFrame(
261 media::PIXEL_FORMAT_YV12, gfx::Size(20, 10), gfx::Rect(20, 10),
262 gfx::Size(20, 10), base::TimeDelta());
263 FakeVideoFrameProvider provider;
264 provider.set_frame(video_frame);
266 VideoLayerImpl* video_layer_impl =
267 impl.AddChildToRoot<VideoLayerImpl>(&provider, media::VIDEO_ROTATION_270);
268 video_layer_impl->SetBounds(layer_size);
269 video_layer_impl->SetDrawsContent(true);
271 impl.CalcDrawProps(viewport_size);
272 gfx::Rect occluded;
273 impl.AppendQuadsWithOcclusion(video_layer_impl, occluded);
275 EXPECT_EQ(1u, impl.quad_list().size());
277 gfx::Point3F p1(0, impl.quad_list().front()->rect.height(), 0);
278 gfx::Point3F p2(impl.quad_list().front()->rect.width(), 0, 0);
279 impl.quad_list()
280 .front()
281 ->shared_quad_state->quad_to_target_transform.TransformPoint(&p1);
282 impl.quad_list()
283 .front()
284 ->shared_quad_state->quad_to_target_transform.TransformPoint(&p2);
285 EXPECT_EQ(gfx::Point3F(100, 50, 0), p1);
286 EXPECT_EQ(gfx::Point3F(0, 0, 0), p2);
289 void EmptyCallback(unsigned sync_point) {
292 TEST(VideoLayerImplTest, SoftwareVideoFrameGeneratesYUVQuad) {
293 gfx::Size layer_size(1000, 1000);
294 gfx::Size viewport_size(1000, 1000);
296 LayerTestCommon::LayerImplTest impl;
297 DebugSetImplThreadAndMainThreadBlocked(impl.proxy());
299 gpu::MailboxHolder mailbox_holder;
300 mailbox_holder.mailbox.name[0] = 1;
302 scoped_refptr<media::VideoFrame> video_frame = media::VideoFrame::CreateFrame(
303 media::PIXEL_FORMAT_YV12, gfx::Size(20, 10), gfx::Rect(20, 10),
304 gfx::Size(20, 10), base::TimeDelta());
306 FakeVideoFrameProvider provider;
307 provider.set_frame(video_frame);
309 VideoLayerImpl* video_layer_impl =
310 impl.AddChildToRoot<VideoLayerImpl>(&provider, media::VIDEO_ROTATION_0);
311 video_layer_impl->SetBounds(layer_size);
312 video_layer_impl->SetDrawsContent(true);
314 gfx::Rect occluded;
315 impl.AppendQuadsWithOcclusion(video_layer_impl, occluded);
317 EXPECT_EQ(1u, impl.quad_list().size());
318 const DrawQuad* draw_quad = impl.quad_list().ElementAt(0);
319 ASSERT_EQ(DrawQuad::YUV_VIDEO_CONTENT, draw_quad->material);
321 const YUVVideoDrawQuad* yuv_draw_quad =
322 static_cast<const YUVVideoDrawQuad*>(draw_quad);
323 EXPECT_EQ(yuv_draw_quad->uv_tex_size.height(),
324 (yuv_draw_quad->ya_tex_size.height() + 1) / 2);
325 EXPECT_EQ(yuv_draw_quad->uv_tex_size.width(),
326 (yuv_draw_quad->ya_tex_size.width() + 1) / 2);
329 TEST(VideoLayerImplTest, NativeYUVFrameGeneratesYUVQuad) {
330 gfx::Size layer_size(1000, 1000);
331 gfx::Size viewport_size(1000, 1000);
333 LayerTestCommon::LayerImplTest impl;
334 DebugSetImplThreadAndMainThreadBlocked(impl.proxy());
336 gpu::MailboxHolder mailbox_holder;
337 mailbox_holder.mailbox.name[0] = 1;
339 scoped_refptr<media::VideoFrame> video_frame =
340 media::VideoFrame::WrapYUV420NativeTextures(
341 mailbox_holder, mailbox_holder, mailbox_holder,
342 base::Bind(EmptyCallback), gfx::Size(10, 10), gfx::Rect(10, 10),
343 gfx::Size(10, 10), base::TimeDelta());
344 video_frame->metadata()->SetBoolean(media::VideoFrameMetadata::ALLOW_OVERLAY,
345 true);
346 FakeVideoFrameProvider provider;
347 provider.set_frame(video_frame);
349 VideoLayerImpl* video_layer_impl =
350 impl.AddChildToRoot<VideoLayerImpl>(&provider, media::VIDEO_ROTATION_0);
351 video_layer_impl->SetBounds(layer_size);
352 video_layer_impl->SetDrawsContent(true);
354 gfx::Rect occluded;
355 impl.AppendQuadsWithOcclusion(video_layer_impl, occluded);
357 EXPECT_EQ(1u, impl.quad_list().size());
358 const DrawQuad* draw_quad = impl.quad_list().ElementAt(0);
359 ASSERT_EQ(DrawQuad::YUV_VIDEO_CONTENT, draw_quad->material);
361 const YUVVideoDrawQuad* yuv_draw_quad =
362 static_cast<const YUVVideoDrawQuad*>(draw_quad);
363 EXPECT_EQ(yuv_draw_quad->uv_tex_size.height(),
364 (yuv_draw_quad->ya_tex_size.height() + 1) / 2);
365 EXPECT_EQ(yuv_draw_quad->uv_tex_size.width(),
366 (yuv_draw_quad->ya_tex_size.width() + 1) / 2);
369 } // namespace
370 } // namespace cc