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 const gfx::Transform
& VideoFrameProviderClientImpl::StreamTextureMatrix()
103 DCHECK(thread_checker_
.CalledOnValidThread());
104 return stream_texture_matrix_
;
107 void VideoFrameProviderClientImpl::StopUsingProvider() {
108 // Block the provider from shutting down until this client is done
110 base::AutoLock
locker(provider_lock_
);
116 void VideoFrameProviderClientImpl::StartRendering() {
117 DCHECK(thread_checker_
.CalledOnValidThread());
118 TRACE_EVENT0("cc", "VideoFrameProviderClientImpl::StartRendering");
122 client_
->AddVideoFrameController(this);
125 void VideoFrameProviderClientImpl::StopRendering() {
126 DCHECK(thread_checker_
.CalledOnValidThread());
127 TRACE_EVENT0("cc", "VideoFrameProviderClientImpl::StopRendering");
130 client_
->RemoveVideoFrameController(this);
134 void VideoFrameProviderClientImpl::DidReceiveFrame() {
136 "VideoFrameProviderClientImpl::DidReceiveFrame",
137 "active_video_layer",
138 !!active_video_layer_
);
139 DCHECK(thread_checker_
.CalledOnValidThread());
140 if (active_video_layer_
)
141 active_video_layer_
->SetNeedsRedraw();
144 void VideoFrameProviderClientImpl::DidUpdateMatrix(const float* matrix
) {
145 DCHECK(thread_checker_
.CalledOnValidThread());
146 stream_texture_matrix_
= gfx::Transform(
147 matrix
[0], matrix
[4], matrix
[8], matrix
[12],
148 matrix
[1], matrix
[5], matrix
[9], matrix
[13],
149 matrix
[2], matrix
[6], matrix
[10], matrix
[14],
150 matrix
[3], matrix
[7], matrix
[11], matrix
[15]);
151 if (active_video_layer_
)
152 active_video_layer_
->SetNeedsRedraw();
155 void VideoFrameProviderClientImpl::OnBeginFrame(const BeginFrameArgs
& args
) {
156 DCHECK(thread_checker_
.CalledOnValidThread());
160 TRACE_EVENT0("cc", "VideoFrameProviderClientImpl::OnBeginFrame");
161 base::AutoLock
locker(provider_lock_
);
163 // We use frame_time + interval here because that is the estimated time at
164 // which a frame returned during this phase will end up being displayed.
166 !provider_
->UpdateCurrentFrame(args
.frame_time
+ args
.interval
,
167 args
.frame_time
+ 2 * args
.interval
)) {