1 // Copyright 2015 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_frame_provider_client_impl.h"
6 #include "cc/layers/video_layer_impl.h"
7 #include "cc/output/begin_frame_args.h"
8 #include "cc/test/fake_video_frame_provider.h"
9 #include "cc/test/layer_test_common.h"
10 #include "media/base/video_frame.h"
11 #include "testing/gmock/include/gmock/gmock.h"
12 #include "testing/gtest/include/gtest/gtest.h"
18 // NOTE: We cannot use DebugScopedSetImplThreadAndMainThreadBlocked in these
19 // tests because it gets destroyed before the VideoLayerImpl is destroyed. This
20 // causes a DCHECK in VideoLayerImpl's destructor to fail.
21 static void DebugSetImplThreadAndMainThreadBlocked(Proxy
* proxy
) {
23 proxy
->SetCurrentThreadIsImplThread(true);
24 proxy
->SetMainThreadBlocked(true);
28 class VideoFrameProviderClientImplTest
: public testing::Test
,
29 public VideoFrameControllerClient
{
31 VideoFrameProviderClientImplTest()
32 : client_impl_(VideoFrameProviderClientImpl::Create(&provider_
, this)),
33 video_layer_impl_(nullptr),
34 test_frame_(media::VideoFrame::CreateFrame(media::PIXEL_FORMAT_YV12
,
39 DebugSetImplThreadAndMainThreadBlocked(impl_
.proxy());
42 ~VideoFrameProviderClientImplTest() {
43 if (!client_impl_
->Stopped()) {
45 DCHECK(client_impl_
->Stopped());
46 DCHECK(!client_impl_
->ActiveVideoLayer());
49 provider_
.SetVideoFrameProviderClient(nullptr);
52 void StartRendering() {
53 EXPECT_CALL(*this, AddVideoFrameController(_
));
54 client_impl_
->StartRendering();
57 void StopRendering() {
58 EXPECT_CALL(*this, RemoveVideoFrameController(_
));
59 client_impl_
->StopRendering();
62 void StartRenderingAndRenderFrame() {
63 EXPECT_FALSE(client_impl_
->HasCurrentFrame());
64 provider_
.set_frame(test_frame_
);
65 EXPECT_TRUE(client_impl_
->HasCurrentFrame());
67 // Start rendering and verify SetNeedsRedraw() was called for the new frame.
69 EXPECT_EQ(gfx::Rect(), video_layer_impl_
->update_rect());
70 client_impl_
->OnBeginFrame(BeginFrameArgs());
71 EXPECT_NE(gfx::Rect(), video_layer_impl_
->update_rect());
74 void CreateActiveVideoLayer() {
75 gfx::Size
layer_size(100, 100);
76 video_layer_impl_
= impl_
.AddChildToRoot
<VideoLayerImpl
>(
77 &provider_
, media::VIDEO_ROTATION_0
);
78 video_layer_impl_
->SetBounds(layer_size
);
79 video_layer_impl_
->SetDrawsContent(true);
80 client_impl_
->SetActiveVideoLayer(video_layer_impl_
);
81 ASSERT_TRUE(client_impl_
->ActiveVideoLayer());
84 MOCK_METHOD1(AddVideoFrameController
, void(VideoFrameController
*));
85 MOCK_METHOD1(RemoveVideoFrameController
, void(VideoFrameController
*));
88 FakeVideoFrameProvider provider_
;
89 LayerTestCommon::LayerImplTest impl_
;
90 scoped_refptr
<VideoFrameProviderClientImpl
> client_impl_
;
91 VideoLayerImpl
* video_layer_impl_
;
92 scoped_refptr
<media::VideoFrame
> test_frame_
;
95 DISALLOW_COPY_AND_ASSIGN(VideoFrameProviderClientImplTest
);
98 TEST_F(VideoFrameProviderClientImplTest
, StartStopRendering
) {
103 TEST_F(VideoFrameProviderClientImplTest
, StopUsingProvider
) {
104 ASSERT_TRUE(client_impl_
->get_provider_for_testing());
106 EXPECT_CALL(*this, RemoveVideoFrameController(_
));
107 client_impl_
->StopUsingProvider();
108 ASSERT_FALSE(client_impl_
->get_provider_for_testing());
111 TEST_F(VideoFrameProviderClientImplTest
, FrameAcquisition
) {
112 CreateActiveVideoLayer();
113 StartRenderingAndRenderFrame();
115 // Verify GetCurrentFrame() and PutCurrentFrame() work correctly.
116 EXPECT_EQ(test_frame_
, client_impl_
->AcquireLockAndCurrentFrame());
117 EXPECT_EQ(0, provider_
.put_current_frame_count());
118 client_impl_
->PutCurrentFrame();
119 EXPECT_EQ(1, provider_
.put_current_frame_count());
121 client_impl_
->ReleaseLock();
125 TEST_F(VideoFrameProviderClientImplTest
, DidReceiveFrame
) {
126 CreateActiveVideoLayer();
127 EXPECT_EQ(gfx::Rect(), video_layer_impl_
->update_rect());
128 client_impl_
->DidReceiveFrame();
129 EXPECT_NE(gfx::Rect(), video_layer_impl_
->update_rect());
132 TEST_F(VideoFrameProviderClientImplTest
, DidDrawFrameIssuesPutCurrentFrame
) {
133 CreateActiveVideoLayer();
134 StartRenderingAndRenderFrame();
135 EXPECT_EQ(0, provider_
.put_current_frame_count());
136 client_impl_
->DidDrawFrame();
137 EXPECT_EQ(1, provider_
.put_current_frame_count());
141 TEST_F(VideoFrameProviderClientImplTest
, StreamTextureMatrix
) {
142 const float kIdentityMatrix
[] = {
161 EXPECT_FALSE(client_impl_
->StreamTextureMatrix().IsIdentity());
162 client_impl_
->DidUpdateMatrix(kIdentityMatrix
);
163 EXPECT_TRUE(client_impl_
->StreamTextureMatrix().IsIdentity());