Switch //chrome/browser code to use IsOriginSecure() instead of SchemeIsSecure().
[chromium-blink-merge.git] / cc / layers / video_frame_provider_client_impl.cc
blob5fab4e2f04c4d0cc01dc081ccc59a0a86e79880a
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 // 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
36 // on the Y axis.
37 stream_texture_matrix_ = gfx::Transform(
38 1.0, 0.0, 0.0, 0.0,
39 0.0, -1.0, 0.0, 1.0,
40 0.0, 0.0, 1.0, 0.0,
41 0.0, 0.0, 0.0, 1.0);
44 VideoFrameProviderClientImpl::~VideoFrameProviderClientImpl() {
45 DCHECK(thread_checker_.CalledOnValidThread());
46 DCHECK(stopped_);
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());
57 DCHECK(video_layer);
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.
64 if (provider_) {
65 provider_->SetVideoFrameProviderClient(nullptr);
66 provider_ = nullptr;
68 if (rendering_)
69 StopRendering();
70 active_video_layer_ = nullptr;
71 stopped_ = true;
74 bool VideoFrameProviderClientImpl::Stopped() const {
75 DCHECK(thread_checker_.CalledOnValidThread());
76 return stopped_;
79 scoped_refptr<media::VideoFrame>
80 VideoFrameProviderClientImpl::AcquireLockAndCurrentFrame() {
81 DCHECK(thread_checker_.CalledOnValidThread());
82 provider_lock_.Acquire(); // Balanced by call to ReleaseLock().
83 if (!provider_)
84 return nullptr;
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()
102 const {
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
109 // using the frame.
110 base::AutoLock locker(provider_lock_);
111 provider_ = nullptr;
112 if (rendering_)
113 StopRendering();
116 void VideoFrameProviderClientImpl::StartRendering() {
117 DCHECK(thread_checker_.CalledOnValidThread());
118 TRACE_EVENT0("cc", "VideoFrameProviderClientImpl::StartRendering");
119 DCHECK(!rendering_);
120 DCHECK(!stopped_);
121 rendering_ = true;
122 client_->AddVideoFrameController(this);
125 void VideoFrameProviderClientImpl::StopRendering() {
126 DCHECK(thread_checker_.CalledOnValidThread());
127 TRACE_EVENT0("cc", "VideoFrameProviderClientImpl::StopRendering");
128 DCHECK(rendering_);
129 DCHECK(!stopped_);
130 client_->RemoveVideoFrameController(this);
131 rendering_ = false;
134 void VideoFrameProviderClientImpl::DidReceiveFrame() {
135 TRACE_EVENT1("cc",
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());
157 DCHECK(rendering_);
158 DCHECK(!stopped_);
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.
165 if (!provider_ ||
166 !provider_->UpdateCurrentFrame(args.frame_time + args.interval,
167 args.frame_time + 2 * args.interval)) {
168 return;
171 DidReceiveFrame();
174 } // namespace cc