Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / cc / layers / video_frame_provider_client_impl.cc
blobf2c5eb2627a9140831b433939e3a21268deb5416
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"
12 namespace cc {
14 // static
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),
25 client_(client),
26 active_video_layer_(nullptr),
27 stopped_(false),
28 rendering_(false),
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
37 // on the Y axis.
38 stream_texture_matrix_ = gfx::Transform(
39 1.0, 0.0, 0.0, 0.0,
40 0.0, -1.0, 0.0, 1.0,
41 0.0, 0.0, 1.0, 0.0,
42 0.0, 0.0, 0.0, 1.0);
45 VideoFrameProviderClientImpl::~VideoFrameProviderClientImpl() {
46 DCHECK(thread_checker_.CalledOnValidThread());
47 DCHECK(stopped_);
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());
58 DCHECK(video_layer);
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.
65 if (provider_) {
66 provider_->SetVideoFrameProviderClient(nullptr);
67 provider_ = nullptr;
69 if (rendering_)
70 StopRendering();
71 active_video_layer_ = nullptr;
72 stopped_ = true;
75 bool VideoFrameProviderClientImpl::Stopped() const {
76 DCHECK(thread_checker_.CalledOnValidThread());
77 return stopped_;
80 scoped_refptr<media::VideoFrame>
81 VideoFrameProviderClientImpl::AcquireLockAndCurrentFrame() {
82 DCHECK(thread_checker_.CalledOnValidThread());
83 provider_lock_.Acquire(); // Balanced by call to ReleaseLock().
84 if (!provider_)
85 return nullptr;
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()
109 const {
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
117 // using the frame.
118 base::AutoLock locker(provider_lock_);
119 provider_ = nullptr;
121 if (rendering_)
122 StopRendering();
125 void VideoFrameProviderClientImpl::StartRendering() {
126 DCHECK(thread_checker_.CalledOnValidThread());
127 TRACE_EVENT0("cc", "VideoFrameProviderClientImpl::StartRendering");
128 DCHECK(!rendering_);
129 DCHECK(!stopped_);
130 rendering_ = true;
131 client_->AddVideoFrameController(this);
134 void VideoFrameProviderClientImpl::StopRendering() {
135 DCHECK(thread_checker_.CalledOnValidThread());
136 TRACE_EVENT0("cc", "VideoFrameProviderClientImpl::StopRendering");
137 DCHECK(rendering_);
138 DCHECK(!stopped_);
139 client_->RemoveVideoFrameController(this);
140 rendering_ = false;
143 void VideoFrameProviderClientImpl::DidReceiveFrame() {
144 TRACE_EVENT1("cc",
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());
167 DCHECK(rendering_);
168 DCHECK(!stopped_);
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.
176 if (!provider_ ||
177 !provider_->UpdateCurrentFrame(args.frame_time + args.interval,
178 args.frame_time + 2 * args.interval)) {
179 return;
183 // Warning: Do not hold |provider_lock_| while calling this function, it may
184 // lead to a reentrant call to HasCurrentFrame() above.
185 DidReceiveFrame();
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;
198 } // namespace cc