blob: 339d4fcf847ff367b63daec742811b10011ae0ef [file] [log] [blame]
[email protected]48871fc2013-01-23 07:36:511// 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.
4
[email protected]cc3cfaa2013-03-18 09:05:525#include "cc/layers/video_frame_provider_client_impl.h"
[email protected]48871fc2013-01-23 07:36:516
primianoc06e2382015-01-28 04:21:497#include "base/trace_event/trace_event.h"
[email protected]681ccff2013-03-18 06:13:528#include "cc/base/math_util.h"
[email protected]cc3cfaa2013-03-18 09:05:529#include "cc/layers/video_layer_impl.h"
[email protected]b7cfc632013-04-10 04:44:4910#include "media/base/video_frame.h"
[email protected]48871fc2013-01-23 07:36:5111
12namespace cc {
13
14// static
15scoped_refptr<VideoFrameProviderClientImpl>
sunnypsa5281722015-03-27 02:39:4816VideoFrameProviderClientImpl::Create(VideoFrameProvider* provider) {
17 return make_scoped_refptr(new VideoFrameProviderClientImpl(provider));
[email protected]48871fc2013-01-23 07:36:5118}
19
[email protected]48871fc2013-01-23 07:36:5120VideoFrameProviderClientImpl::VideoFrameProviderClientImpl(
21 VideoFrameProvider* provider)
sunnypsa5281722015-03-27 02:39:4822 : provider_(provider), active_video_layer_(nullptr), stopped_(false) {
[email protected]48871fc2013-01-23 07:36:5123 // This only happens during a commit on the compositor thread while the main
24 // thread is blocked. That makes this a thread-safe call to set the video
25 // frame provider client that does not require a lock. The same is true of
26 // the call to Stop().
27 provider_->SetVideoFrameProviderClient(this);
28
29 // This matrix is the default transformation for stream textures, and flips
30 // on the Y axis.
31 stream_texture_matrix_ = gfx::Transform(
32 1.0, 0.0, 0.0, 0.0,
33 0.0, -1.0, 0.0, 1.0,
34 0.0, 0.0, 1.0, 0.0,
35 0.0, 0.0, 0.0, 1.0);
36}
37
sunnypsa5281722015-03-27 02:39:4838VideoFrameProviderClientImpl::~VideoFrameProviderClientImpl() {
39 DCHECK(thread_checker_.CalledOnValidThread());
40 DCHECK(stopped_);
41}
42
43VideoLayerImpl* VideoFrameProviderClientImpl::ActiveVideoLayer() const {
44 DCHECK(thread_checker_.CalledOnValidThread());
45 return active_video_layer_;
46}
47
dongseong.hwangee88f0aa2015-02-10 19:39:5948void VideoFrameProviderClientImpl::SetActiveVideoLayer(
49 VideoLayerImpl* video_layer) {
50 DCHECK(thread_checker_.CalledOnValidThread());
51 DCHECK(video_layer);
52 active_video_layer_ = video_layer;
53}
54
[email protected]48871fc2013-01-23 07:36:5155void VideoFrameProviderClientImpl::Stop() {
dongseong.hwangee88f0aa2015-02-10 19:39:5956 DCHECK(thread_checker_.CalledOnValidThread());
sunnypsa5281722015-03-27 02:39:4857 // It's called when the main thread is blocked, so lock isn't needed.
58 if (provider_) {
59 provider_->SetVideoFrameProviderClient(nullptr);
60 provider_ = nullptr;
61 }
62 active_video_layer_ = nullptr;
63 stopped_ = true;
[email protected]48871fc2013-01-23 07:36:5164}
65
sunnypsa5281722015-03-27 02:39:4866bool VideoFrameProviderClientImpl::Stopped() const {
dongseong.hwangee88f0aa2015-02-10 19:39:5967 DCHECK(thread_checker_.CalledOnValidThread());
sunnypsa5281722015-03-27 02:39:4868 return stopped_;
dongseong.hwangee88f0aa2015-02-10 19:39:5969}
70
[email protected]af2cd322013-03-22 16:56:1871scoped_refptr<media::VideoFrame>
72VideoFrameProviderClientImpl::AcquireLockAndCurrentFrame() {
dongseong.hwangee88f0aa2015-02-10 19:39:5973 DCHECK(thread_checker_.CalledOnValidThread());
[email protected]48871fc2013-01-23 07:36:5174 provider_lock_.Acquire(); // Balanced by call to ReleaseLock().
75 if (!provider_)
kulkarni.a4015690f12014-10-10 13:50:0676 return nullptr;
[email protected]48871fc2013-01-23 07:36:5177
78 return provider_->GetCurrentFrame();
79}
80
Bartosz Fabianowski85a823812015-04-16 10:27:5181void VideoFrameProviderClientImpl::PutCurrentFrame() {
dongseong.hwangee88f0aa2015-02-10 19:39:5982 DCHECK(thread_checker_.CalledOnValidThread());
[email protected]48871fc2013-01-23 07:36:5183 provider_lock_.AssertAcquired();
Bartosz Fabianowski85a823812015-04-16 10:27:5184 provider_->PutCurrentFrame();
[email protected]48871fc2013-01-23 07:36:5185}
86
87void VideoFrameProviderClientImpl::ReleaseLock() {
dongseong.hwangee88f0aa2015-02-10 19:39:5988 DCHECK(thread_checker_.CalledOnValidThread());
[email protected]48871fc2013-01-23 07:36:5189 provider_lock_.AssertAcquired();
90 provider_lock_.Release();
91}
92
sunnypsa5281722015-03-27 02:39:4893const gfx::Transform& VideoFrameProviderClientImpl::StreamTextureMatrix()
94 const {
95 DCHECK(thread_checker_.CalledOnValidThread());
96 return stream_texture_matrix_;
97}
98
[email protected]48871fc2013-01-23 07:36:5199void VideoFrameProviderClientImpl::StopUsingProvider() {
100 // Block the provider from shutting down until this client is done
101 // using the frame.
102 base::AutoLock locker(provider_lock_);
kulkarni.a4015690f12014-10-10 13:50:06103 provider_ = nullptr;
[email protected]48871fc2013-01-23 07:36:51104}
105
Bartosz Fabianowski85a823812015-04-16 10:27:51106void VideoFrameProviderClientImpl::StartRendering() {
107 // TODO(dalecurtis, sunnyps): Hook this method up to control when to start
108 // observing vsync intervals. http://crbug.com/336733
109}
110
111void VideoFrameProviderClientImpl::StopRendering() {
112 // TODO(dalecurtis, sunnyps): Hook this method up to control when to stop
113 // observing vsync intervals. http://crbug.com/336733
114}
115
[email protected]48871fc2013-01-23 07:36:51116void VideoFrameProviderClientImpl::DidReceiveFrame() {
[email protected]9469d782014-04-18 01:23:56117 TRACE_EVENT1("cc",
118 "VideoFrameProviderClientImpl::DidReceiveFrame",
119 "active_video_layer",
120 !!active_video_layer_);
dongseong.hwangee88f0aa2015-02-10 19:39:59121 DCHECK(thread_checker_.CalledOnValidThread());
[email protected]48871fc2013-01-23 07:36:51122 if (active_video_layer_)
[email protected]4af49d542013-03-15 22:18:47123 active_video_layer_->SetNeedsRedraw();
[email protected]48871fc2013-01-23 07:36:51124}
125
126void VideoFrameProviderClientImpl::DidUpdateMatrix(const float* matrix) {
dongseong.hwangee88f0aa2015-02-10 19:39:59127 DCHECK(thread_checker_.CalledOnValidThread());
[email protected]48871fc2013-01-23 07:36:51128 stream_texture_matrix_ = gfx::Transform(
129 matrix[0], matrix[4], matrix[8], matrix[12],
130 matrix[1], matrix[5], matrix[9], matrix[13],
131 matrix[2], matrix[6], matrix[10], matrix[14],
132 matrix[3], matrix[7], matrix[11], matrix[15]);
133 if (active_video_layer_)
[email protected]4af49d542013-03-15 22:18:47134 active_video_layer_->SetNeedsRedraw();
[email protected]48871fc2013-01-23 07:36:51135}
136
137} // namespace cc