1 // Copyright 2013 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"
7 #include "base/trace_event/trace_event.h"
8 #include "cc/base/math_util.h"
9 #include "cc/layers/video_layer_impl.h"
10 #include "media/base/video_frame.h"
15 scoped_refptr
<VideoFrameProviderClientImpl
>
16 VideoFrameProviderClientImpl::Create(VideoFrameProvider
* provider
,
17 VideoFrameControllerClient
* client
) {
18 return make_scoped_refptr(new VideoFrameProviderClientImpl(provider
, client
));
21 VideoFrameProviderClientImpl::VideoFrameProviderClientImpl(
22 VideoFrameProvider
* provider
,
23 VideoFrameControllerClient
* client
)
24 : provider_(provider
),
26 active_video_layer_(nullptr),
29 needs_put_current_frame_(false) {
30 // This only happens during a commit on the compositor thread while the main
31 // thread is blocked. That makes this a thread-safe call to set the video
32 // frame provider client that does not require a lock. The same is true of
33 // the call to Stop().
34 provider_
->SetVideoFrameProviderClient(this);
36 // This matrix is the default transformation for stream textures, and flips
38 stream_texture_matrix_
= gfx::Transform(
45 VideoFrameProviderClientImpl::~VideoFrameProviderClientImpl() {
46 DCHECK(thread_checker_
.CalledOnValidThread());
50 VideoLayerImpl
* VideoFrameProviderClientImpl::ActiveVideoLayer() const {
51 DCHECK(thread_checker_
.CalledOnValidThread());
52 return active_video_layer_
;
55 void VideoFrameProviderClientImpl::SetActiveVideoLayer(
56 VideoLayerImpl
* video_layer
) {
57 DCHECK(thread_checker_
.CalledOnValidThread());
59 active_video_layer_
= video_layer
;
62 void VideoFrameProviderClientImpl::Stop() {
63 DCHECK(thread_checker_
.CalledOnValidThread());
64 // It's called when the main thread is blocked, so lock isn't needed.
66 provider_
->SetVideoFrameProviderClient(nullptr);
71 active_video_layer_
= nullptr;
75 bool VideoFrameProviderClientImpl::Stopped() const {
76 DCHECK(thread_checker_
.CalledOnValidThread());
80 scoped_refptr
<media::VideoFrame
>
81 VideoFrameProviderClientImpl::AcquireLockAndCurrentFrame() {
82 DCHECK(thread_checker_
.CalledOnValidThread());
83 provider_lock_
.Acquire(); // Balanced by call to ReleaseLock().
87 return provider_
->GetCurrentFrame();
90 void VideoFrameProviderClientImpl::PutCurrentFrame() {
91 DCHECK(thread_checker_
.CalledOnValidThread());
92 provider_lock_
.AssertAcquired();
93 provider_
->PutCurrentFrame();
94 needs_put_current_frame_
= false;
97 void VideoFrameProviderClientImpl::ReleaseLock() {
98 DCHECK(thread_checker_
.CalledOnValidThread());
99 provider_lock_
.AssertAcquired();
100 provider_lock_
.Release();
103 bool VideoFrameProviderClientImpl::HasCurrentFrame() {
104 base::AutoLock
locker(provider_lock_
);
105 return provider_
&& provider_
->HasCurrentFrame();
108 const gfx::Transform
& VideoFrameProviderClientImpl::StreamTextureMatrix()
110 DCHECK(thread_checker_
.CalledOnValidThread());
111 return stream_texture_matrix_
;
114 void VideoFrameProviderClientImpl::StopUsingProvider() {
116 // Block the provider from shutting down until this client is done
118 base::AutoLock
locker(provider_lock_
);
125 void VideoFrameProviderClientImpl::StartRendering() {
126 DCHECK(thread_checker_
.CalledOnValidThread());
127 TRACE_EVENT0("cc", "VideoFrameProviderClientImpl::StartRendering");
131 client_
->AddVideoFrameController(this);
134 void VideoFrameProviderClientImpl::StopRendering() {
135 DCHECK(thread_checker_
.CalledOnValidThread());
136 TRACE_EVENT0("cc", "VideoFrameProviderClientImpl::StopRendering");
139 client_
->RemoveVideoFrameController(this);
143 void VideoFrameProviderClientImpl::DidReceiveFrame() {
145 "VideoFrameProviderClientImpl::DidReceiveFrame",
146 "active_video_layer",
147 !!active_video_layer_
);
148 DCHECK(thread_checker_
.CalledOnValidThread());
149 needs_put_current_frame_
= true;
150 if (active_video_layer_
)
151 active_video_layer_
->SetNeedsRedraw();
154 void VideoFrameProviderClientImpl::DidUpdateMatrix(const float* matrix
) {
155 DCHECK(thread_checker_
.CalledOnValidThread());
156 stream_texture_matrix_
= gfx::Transform(
157 matrix
[0], matrix
[4], matrix
[8], matrix
[12],
158 matrix
[1], matrix
[5], matrix
[9], matrix
[13],
159 matrix
[2], matrix
[6], matrix
[10], matrix
[14],
160 matrix
[3], matrix
[7], matrix
[11], matrix
[15]);
161 if (active_video_layer_
)
162 active_video_layer_
->SetNeedsRedraw();
165 void VideoFrameProviderClientImpl::OnBeginFrame(const BeginFrameArgs
& args
) {
166 DCHECK(thread_checker_
.CalledOnValidThread());
170 TRACE_EVENT0("cc", "VideoFrameProviderClientImpl::OnBeginFrame");
172 base::AutoLock
locker(provider_lock_
);
174 // We use frame_time + interval here because that is the estimated time at
175 // which a frame returned during this phase will end up being displayed.
177 !provider_
->UpdateCurrentFrame(args
.frame_time
+ args
.interval
,
178 args
.frame_time
+ 2 * args
.interval
)) {
183 // Warning: Do not hold |provider_lock_| while calling this function, it may
184 // lead to a reentrant call to HasCurrentFrame() above.
188 void VideoFrameProviderClientImpl::DidDrawFrame() {
189 DCHECK(thread_checker_
.CalledOnValidThread());
191 base::AutoLock
locker(provider_lock_
);
192 if (provider_
&& needs_put_current_frame_
)
193 provider_
->PutCurrentFrame();
195 needs_put_current_frame_
= false;