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/test/fake_video_frame_provider.h"
12 #include "cc/test/layer_test_common.h"
13 #include "cc/trees/single_thread_proxy.h"
14 #include "media/base/video_frame.h"
15 #include "testing/gtest/include/gtest/gtest.h"
20 // NOTE: We cannot use DebugScopedSetImplThreadAndMainThreadBlocked in these
21 // tests because it gets destroyed before the VideoLayerImpl is destroyed. This
22 // causes a DCHECK in VideoLayerImpl's destructor to fail.
23 static void DebugSetImplThreadAndMainThreadBlocked(Proxy
* proxy
) {
25 proxy
->SetCurrentThreadIsImplThread(true);
26 proxy
->SetMainThreadBlocked(true);
30 TEST(VideoLayerImplTest
, Occlusion
) {
31 gfx::Size
layer_size(1000, 1000);
32 gfx::Size
viewport_size(1000, 1000);
34 LayerTestCommon::LayerImplTest impl
;
35 DebugSetImplThreadAndMainThreadBlocked(impl
.proxy());
37 scoped_refptr
<media::VideoFrame
> video_frame
=
38 media::VideoFrame::CreateFrame(media::VideoFrame::YV12
,
43 FakeVideoFrameProvider provider
;
44 provider
.set_frame(video_frame
);
46 VideoLayerImpl
* video_layer_impl
=
47 impl
.AddChildToRoot
<VideoLayerImpl
>(&provider
, media::VIDEO_ROTATION_0
);
48 video_layer_impl
->SetBounds(layer_size
);
49 video_layer_impl
->SetContentBounds(layer_size
);
50 video_layer_impl
->SetDrawsContent(true);
52 impl
.CalcDrawProps(viewport_size
);
55 SCOPED_TRACE("No occlusion");
57 impl
.AppendQuadsWithOcclusion(video_layer_impl
, occluded
);
59 LayerTestCommon::VerifyQuadsExactlyCoverRect(impl
.quad_list(),
60 gfx::Rect(layer_size
));
61 EXPECT_EQ(1u, impl
.quad_list().size());
65 SCOPED_TRACE("Full occlusion");
66 gfx::Rect
occluded(video_layer_impl
->visible_content_rect());
67 impl
.AppendQuadsWithOcclusion(video_layer_impl
, occluded
);
69 LayerTestCommon::VerifyQuadsExactlyCoverRect(impl
.quad_list(), gfx::Rect());
70 EXPECT_EQ(impl
.quad_list().size(), 0u);
74 SCOPED_TRACE("Partial occlusion");
75 gfx::Rect
occluded(200, 0, 800, 1000);
76 impl
.AppendQuadsWithOcclusion(video_layer_impl
, occluded
);
78 size_t partially_occluded_count
= 0;
79 LayerTestCommon::VerifyQuadsAreOccluded(
80 impl
.quad_list(), occluded
, &partially_occluded_count
);
81 // The layer outputs one quad, which is partially occluded.
82 EXPECT_EQ(1u, impl
.quad_list().size());
83 EXPECT_EQ(1u, partially_occluded_count
);
87 TEST(VideoLayerImplTest
, DidBecomeActiveShouldSetActiveVideoLayer
) {
88 LayerTestCommon::LayerImplTest impl
;
89 DebugSetImplThreadAndMainThreadBlocked(impl
.proxy());
91 FakeVideoFrameProvider provider
;
92 VideoLayerImpl
* video_layer_impl
=
93 impl
.AddChildToRoot
<VideoLayerImpl
>(&provider
, media::VIDEO_ROTATION_0
);
95 VideoFrameProviderClientImpl
* client
=
96 static_cast<VideoFrameProviderClientImpl
*>(provider
.client());
99 EXPECT_FALSE(client
->ActiveVideoLayer());
100 video_layer_impl
->DidBecomeActive();
101 EXPECT_EQ(video_layer_impl
, client
->ActiveVideoLayer());
104 TEST(VideoLayerImplTest
, Rotated0
) {
105 gfx::Size
layer_size(100, 50);
106 gfx::Size
viewport_size(1000, 500);
108 LayerTestCommon::LayerImplTest impl
;
109 DebugSetImplThreadAndMainThreadBlocked(impl
.proxy());
111 scoped_refptr
<media::VideoFrame
> video_frame
=
112 media::VideoFrame::CreateFrame(media::VideoFrame::YV12
,
117 FakeVideoFrameProvider provider
;
118 provider
.set_frame(video_frame
);
120 VideoLayerImpl
* video_layer_impl
=
121 impl
.AddChildToRoot
<VideoLayerImpl
>(&provider
, media::VIDEO_ROTATION_0
);
122 video_layer_impl
->SetBounds(layer_size
);
123 video_layer_impl
->SetContentBounds(layer_size
);
124 video_layer_impl
->SetDrawsContent(true);
126 impl
.CalcDrawProps(viewport_size
);
128 impl
.AppendQuadsWithOcclusion(video_layer_impl
, occluded
);
130 EXPECT_EQ(1u, impl
.quad_list().size());
132 gfx::Point3F
p1(0, impl
.quad_list().front()->rect
.height(), 0);
133 gfx::Point3F
p2(impl
.quad_list().front()->rect
.width(), 0, 0);
134 impl
.quad_list().front()->quadTransform().TransformPoint(&p1
);
135 impl
.quad_list().front()->quadTransform().TransformPoint(&p2
);
136 EXPECT_EQ(gfx::Point3F(0, 50, 0), p1
);
137 EXPECT_EQ(gfx::Point3F(100, 0, 0), p2
);
140 TEST(VideoLayerImplTest
, Rotated90
) {
141 gfx::Size
layer_size(100, 50);
142 gfx::Size
viewport_size(1000, 500);
144 LayerTestCommon::LayerImplTest impl
;
145 DebugSetImplThreadAndMainThreadBlocked(impl
.proxy());
147 scoped_refptr
<media::VideoFrame
> video_frame
=
148 media::VideoFrame::CreateFrame(media::VideoFrame::YV12
,
153 FakeVideoFrameProvider provider
;
154 provider
.set_frame(video_frame
);
156 VideoLayerImpl
* video_layer_impl
=
157 impl
.AddChildToRoot
<VideoLayerImpl
>(&provider
, media::VIDEO_ROTATION_90
);
158 video_layer_impl
->SetBounds(layer_size
);
159 video_layer_impl
->SetContentBounds(layer_size
);
160 video_layer_impl
->SetDrawsContent(true);
162 impl
.CalcDrawProps(viewport_size
);
164 impl
.AppendQuadsWithOcclusion(video_layer_impl
, occluded
);
166 EXPECT_EQ(1u, impl
.quad_list().size());
168 gfx::Point3F
p1(0, impl
.quad_list().front()->rect
.height(), 0);
169 gfx::Point3F
p2(impl
.quad_list().front()->rect
.width(), 0, 0);
170 impl
.quad_list().front()->quadTransform().TransformPoint(&p1
);
171 impl
.quad_list().front()->quadTransform().TransformPoint(&p2
);
172 EXPECT_EQ(gfx::Point3F(0, 0, 0), p1
);
173 EXPECT_EQ(gfx::Point3F(100, 50, 0), p2
);
176 TEST(VideoLayerImplTest
, Rotated180
) {
177 gfx::Size
layer_size(100, 50);
178 gfx::Size
viewport_size(1000, 500);
180 LayerTestCommon::LayerImplTest impl
;
181 DebugSetImplThreadAndMainThreadBlocked(impl
.proxy());
183 scoped_refptr
<media::VideoFrame
> video_frame
=
184 media::VideoFrame::CreateFrame(media::VideoFrame::YV12
,
189 FakeVideoFrameProvider provider
;
190 provider
.set_frame(video_frame
);
192 VideoLayerImpl
* video_layer_impl
=
193 impl
.AddChildToRoot
<VideoLayerImpl
>(&provider
, media::VIDEO_ROTATION_180
);
194 video_layer_impl
->SetBounds(layer_size
);
195 video_layer_impl
->SetContentBounds(layer_size
);
196 video_layer_impl
->SetDrawsContent(true);
198 impl
.CalcDrawProps(viewport_size
);
200 impl
.AppendQuadsWithOcclusion(video_layer_impl
, occluded
);
202 EXPECT_EQ(1u, impl
.quad_list().size());
204 gfx::Point3F
p1(0, impl
.quad_list().front()->rect
.height(), 0);
205 gfx::Point3F
p2(impl
.quad_list().front()->rect
.width(), 0, 0);
206 impl
.quad_list().front()->quadTransform().TransformPoint(&p1
);
207 impl
.quad_list().front()->quadTransform().TransformPoint(&p2
);
208 EXPECT_EQ(gfx::Point3F(100, 0, 0), p1
);
209 EXPECT_EQ(gfx::Point3F(0, 50, 0), p2
);
212 TEST(VideoLayerImplTest
, Rotated270
) {
213 gfx::Size
layer_size(100, 50);
214 gfx::Size
viewport_size(1000, 500);
216 LayerTestCommon::LayerImplTest impl
;
217 DebugSetImplThreadAndMainThreadBlocked(impl
.proxy());
219 scoped_refptr
<media::VideoFrame
> video_frame
=
220 media::VideoFrame::CreateFrame(media::VideoFrame::YV12
,
225 FakeVideoFrameProvider provider
;
226 provider
.set_frame(video_frame
);
228 VideoLayerImpl
* video_layer_impl
=
229 impl
.AddChildToRoot
<VideoLayerImpl
>(&provider
, media::VIDEO_ROTATION_270
);
230 video_layer_impl
->SetBounds(layer_size
);
231 video_layer_impl
->SetContentBounds(layer_size
);
232 video_layer_impl
->SetDrawsContent(true);
234 impl
.CalcDrawProps(viewport_size
);
236 impl
.AppendQuadsWithOcclusion(video_layer_impl
, occluded
);
238 EXPECT_EQ(1u, impl
.quad_list().size());
240 gfx::Point3F
p1(0, impl
.quad_list().front()->rect
.height(), 0);
241 gfx::Point3F
p2(impl
.quad_list().front()->rect
.width(), 0, 0);
242 impl
.quad_list().front()->quadTransform().TransformPoint(&p1
);
243 impl
.quad_list().front()->quadTransform().TransformPoint(&p2
);
244 EXPECT_EQ(gfx::Point3F(100, 50, 0), p1
);
245 EXPECT_EQ(gfx::Point3F(0, 0, 0), p2
);