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 // This only happens during a commit on the compositor thread while the main
30 // thread is blocked. That makes this a thread-safe call to set the video
31 // frame provider client that does not require a lock. The same is true of
32 // the call to Stop().
33 provider_
->SetVideoFrameProviderClient(this);
35 // This matrix is the default transformation for stream textures, and flips
37 stream_texture_matrix_
= gfx::Transform(
44 VideoFrameProviderClientImpl::~VideoFrameProviderClientImpl() {
45 DCHECK(thread_checker_
.CalledOnValidThread());
49 VideoLayerImpl
* VideoFrameProviderClientImpl::ActiveVideoLayer() const {
50 DCHECK(thread_checker_
.CalledOnValidThread());
51 return active_video_layer_
;
54 void VideoFrameProviderClientImpl::SetActiveVideoLayer(
55 VideoLayerImpl
* video_layer
) {
56 DCHECK(thread_checker_
.CalledOnValidThread());
58 active_video_layer_
= video_layer
;
61 void VideoFrameProviderClientImpl::Stop() {
62 DCHECK(thread_checker_
.CalledOnValidThread());
63 // It's called when the main thread is blocked, so lock isn't needed.
65 provider_
->SetVideoFrameProviderClient(nullptr);
70 active_video_layer_
= nullptr;
74 bool VideoFrameProviderClientImpl::Stopped() const {
75 DCHECK(thread_checker_
.CalledOnValidThread());
79 scoped_refptr
<media::VideoFrame
>
80 VideoFrameProviderClientImpl::AcquireLockAndCurrentFrame() {
81 DCHECK(thread_checker_
.CalledOnValidThread());
82 provider_lock_
.Acquire(); // Balanced by call to ReleaseLock().
86 return provider_
->GetCurrentFrame();
89 void VideoFrameProviderClientImpl::PutCurrentFrame() {
90 DCHECK(thread_checker_
.CalledOnValidThread());
91 provider_lock_
.AssertAcquired();
92 provider_
->PutCurrentFrame();
95 void VideoFrameProviderClientImpl::ReleaseLock() {
96 DCHECK(thread_checker_
.CalledOnValidThread());
97 provider_lock_
.AssertAcquired();
98 provider_lock_
.Release();
101 bool VideoFrameProviderClientImpl::HasCurrentFrame() {
102 base::AutoLock
locker(provider_lock_
);
103 return provider_
&& provider_
->HasCurrentFrame();
106 const gfx::Transform
& VideoFrameProviderClientImpl::StreamTextureMatrix()
108 DCHECK(thread_checker_
.CalledOnValidThread());
109 return stream_texture_matrix_
;
112 void VideoFrameProviderClientImpl::StopUsingProvider() {
113 // Block the provider from shutting down until this client is done
115 base::AutoLock
locker(provider_lock_
);
121 void VideoFrameProviderClientImpl::StartRendering() {
122 DCHECK(thread_checker_
.CalledOnValidThread());
123 TRACE_EVENT0("cc", "VideoFrameProviderClientImpl::StartRendering");
127 client_
->AddVideoFrameController(this);
130 void VideoFrameProviderClientImpl::StopRendering() {
131 DCHECK(thread_checker_
.CalledOnValidThread());
132 TRACE_EVENT0("cc", "VideoFrameProviderClientImpl::StopRendering");
135 client_
->RemoveVideoFrameController(this);
139 void VideoFrameProviderClientImpl::DidReceiveFrame() {
141 "VideoFrameProviderClientImpl::DidReceiveFrame",
142 "active_video_layer",
143 !!active_video_layer_
);
144 DCHECK(thread_checker_
.CalledOnValidThread());
145 if (active_video_layer_
)
146 active_video_layer_
->SetNeedsRedraw();
149 void VideoFrameProviderClientImpl::DidUpdateMatrix(const float* matrix
) {
150 DCHECK(thread_checker_
.CalledOnValidThread());
151 stream_texture_matrix_
= gfx::Transform(
152 matrix
[0], matrix
[4], matrix
[8], matrix
[12],
153 matrix
[1], matrix
[5], matrix
[9], matrix
[13],
154 matrix
[2], matrix
[6], matrix
[10], matrix
[14],
155 matrix
[3], matrix
[7], matrix
[11], matrix
[15]);
156 if (active_video_layer_
)
157 active_video_layer_
->SetNeedsRedraw();
160 void VideoFrameProviderClientImpl::OnBeginFrame(const BeginFrameArgs
& args
) {
161 DCHECK(thread_checker_
.CalledOnValidThread());
165 TRACE_EVENT0("cc", "VideoFrameProviderClientImpl::OnBeginFrame");
166 base::AutoLock
locker(provider_lock_
);
168 // We use frame_time + interval here because that is the estimated time at
169 // which a frame returned during this phase will end up being displayed.
171 !provider_
->UpdateCurrentFrame(args
.frame_time
+ args
.interval
,
172 args
.frame_time
+ 2 * args
.interval
)) {