[email protected] | 48871fc | 2013-01-23 07:36:51 | [diff] [blame] | 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. |
| 4 | |
[email protected] | cc3cfaa | 2013-03-18 09:05:52 | [diff] [blame] | 5 | #include "cc/layers/video_frame_provider_client_impl.h" |
[email protected] | 48871fc | 2013-01-23 07:36:51 | [diff] [blame] | 6 | |
primiano | c06e238 | 2015-01-28 04:21:49 | [diff] [blame] | 7 | #include "base/trace_event/trace_event.h" |
[email protected] | 681ccff | 2013-03-18 06:13:52 | [diff] [blame] | 8 | #include "cc/base/math_util.h" |
[email protected] | cc3cfaa | 2013-03-18 09:05:52 | [diff] [blame] | 9 | #include "cc/layers/video_layer_impl.h" |
[email protected] | b7cfc63 | 2013-04-10 04:44:49 | [diff] [blame] | 10 | #include "media/base/video_frame.h" |
[email protected] | 48871fc | 2013-01-23 07:36:51 | [diff] [blame] | 11 | |
| 12 | namespace cc { |
| 13 | |
| 14 | // static |
| 15 | scoped_refptr<VideoFrameProviderClientImpl> |
sunnyps | a528172 | 2015-03-27 02:39:48 | [diff] [blame] | 16 | VideoFrameProviderClientImpl::Create(VideoFrameProvider* provider) { |
| 17 | return make_scoped_refptr(new VideoFrameProviderClientImpl(provider)); |
[email protected] | 48871fc | 2013-01-23 07:36:51 | [diff] [blame] | 18 | } |
| 19 | |
[email protected] | 48871fc | 2013-01-23 07:36:51 | [diff] [blame] | 20 | VideoFrameProviderClientImpl::VideoFrameProviderClientImpl( |
| 21 | VideoFrameProvider* provider) |
sunnyps | a528172 | 2015-03-27 02:39:48 | [diff] [blame] | 22 | : provider_(provider), active_video_layer_(nullptr), stopped_(false) { |
[email protected] | 48871fc | 2013-01-23 07:36:51 | [diff] [blame] | 23 | // 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 | |
sunnyps | a528172 | 2015-03-27 02:39:48 | [diff] [blame] | 38 | VideoFrameProviderClientImpl::~VideoFrameProviderClientImpl() { |
| 39 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 40 | DCHECK(stopped_); |
| 41 | } |
| 42 | |
| 43 | VideoLayerImpl* VideoFrameProviderClientImpl::ActiveVideoLayer() const { |
| 44 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 45 | return active_video_layer_; |
| 46 | } |
| 47 | |
dongseong.hwang | ee88f0aa | 2015-02-10 19:39:59 | [diff] [blame] | 48 | void 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] | 48871fc | 2013-01-23 07:36:51 | [diff] [blame] | 55 | void VideoFrameProviderClientImpl::Stop() { |
dongseong.hwang | ee88f0aa | 2015-02-10 19:39:59 | [diff] [blame] | 56 | DCHECK(thread_checker_.CalledOnValidThread()); |
sunnyps | a528172 | 2015-03-27 02:39:48 | [diff] [blame] | 57 | // 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] | 48871fc | 2013-01-23 07:36:51 | [diff] [blame] | 64 | } |
| 65 | |
sunnyps | a528172 | 2015-03-27 02:39:48 | [diff] [blame] | 66 | bool VideoFrameProviderClientImpl::Stopped() const { |
dongseong.hwang | ee88f0aa | 2015-02-10 19:39:59 | [diff] [blame] | 67 | DCHECK(thread_checker_.CalledOnValidThread()); |
sunnyps | a528172 | 2015-03-27 02:39:48 | [diff] [blame] | 68 | return stopped_; |
dongseong.hwang | ee88f0aa | 2015-02-10 19:39:59 | [diff] [blame] | 69 | } |
| 70 | |
[email protected] | af2cd32 | 2013-03-22 16:56:18 | [diff] [blame] | 71 | scoped_refptr<media::VideoFrame> |
| 72 | VideoFrameProviderClientImpl::AcquireLockAndCurrentFrame() { |
dongseong.hwang | ee88f0aa | 2015-02-10 19:39:59 | [diff] [blame] | 73 | DCHECK(thread_checker_.CalledOnValidThread()); |
[email protected] | 48871fc | 2013-01-23 07:36:51 | [diff] [blame] | 74 | provider_lock_.Acquire(); // Balanced by call to ReleaseLock(). |
| 75 | if (!provider_) |
kulkarni.a | 4015690f1 | 2014-10-10 13:50:06 | [diff] [blame] | 76 | return nullptr; |
[email protected] | 48871fc | 2013-01-23 07:36:51 | [diff] [blame] | 77 | |
| 78 | return provider_->GetCurrentFrame(); |
| 79 | } |
| 80 | |
Bartosz Fabianowski | 85a82381 | 2015-04-16 10:27:51 | [diff] [blame^] | 81 | void VideoFrameProviderClientImpl::PutCurrentFrame() { |
dongseong.hwang | ee88f0aa | 2015-02-10 19:39:59 | [diff] [blame] | 82 | DCHECK(thread_checker_.CalledOnValidThread()); |
[email protected] | 48871fc | 2013-01-23 07:36:51 | [diff] [blame] | 83 | provider_lock_.AssertAcquired(); |
Bartosz Fabianowski | 85a82381 | 2015-04-16 10:27:51 | [diff] [blame^] | 84 | provider_->PutCurrentFrame(); |
[email protected] | 48871fc | 2013-01-23 07:36:51 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | void VideoFrameProviderClientImpl::ReleaseLock() { |
dongseong.hwang | ee88f0aa | 2015-02-10 19:39:59 | [diff] [blame] | 88 | DCHECK(thread_checker_.CalledOnValidThread()); |
[email protected] | 48871fc | 2013-01-23 07:36:51 | [diff] [blame] | 89 | provider_lock_.AssertAcquired(); |
| 90 | provider_lock_.Release(); |
| 91 | } |
| 92 | |
sunnyps | a528172 | 2015-03-27 02:39:48 | [diff] [blame] | 93 | const gfx::Transform& VideoFrameProviderClientImpl::StreamTextureMatrix() |
| 94 | const { |
| 95 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 96 | return stream_texture_matrix_; |
| 97 | } |
| 98 | |
[email protected] | 48871fc | 2013-01-23 07:36:51 | [diff] [blame] | 99 | void 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.a | 4015690f1 | 2014-10-10 13:50:06 | [diff] [blame] | 103 | provider_ = nullptr; |
[email protected] | 48871fc | 2013-01-23 07:36:51 | [diff] [blame] | 104 | } |
| 105 | |
Bartosz Fabianowski | 85a82381 | 2015-04-16 10:27:51 | [diff] [blame^] | 106 | void 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 | |
| 111 | void 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] | 48871fc | 2013-01-23 07:36:51 | [diff] [blame] | 116 | void VideoFrameProviderClientImpl::DidReceiveFrame() { |
[email protected] | 9469d78 | 2014-04-18 01:23:56 | [diff] [blame] | 117 | TRACE_EVENT1("cc", |
| 118 | "VideoFrameProviderClientImpl::DidReceiveFrame", |
| 119 | "active_video_layer", |
| 120 | !!active_video_layer_); |
dongseong.hwang | ee88f0aa | 2015-02-10 19:39:59 | [diff] [blame] | 121 | DCHECK(thread_checker_.CalledOnValidThread()); |
[email protected] | 48871fc | 2013-01-23 07:36:51 | [diff] [blame] | 122 | if (active_video_layer_) |
[email protected] | 4af49d54 | 2013-03-15 22:18:47 | [diff] [blame] | 123 | active_video_layer_->SetNeedsRedraw(); |
[email protected] | 48871fc | 2013-01-23 07:36:51 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | void VideoFrameProviderClientImpl::DidUpdateMatrix(const float* matrix) { |
dongseong.hwang | ee88f0aa | 2015-02-10 19:39:59 | [diff] [blame] | 127 | DCHECK(thread_checker_.CalledOnValidThread()); |
[email protected] | 48871fc | 2013-01-23 07:36:51 | [diff] [blame] | 128 | 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] | 4af49d54 | 2013-03-15 22:18:47 | [diff] [blame] | 134 | active_video_layer_->SetNeedsRedraw(); |
[email protected] | 48871fc | 2013-01-23 07:36:51 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | } // namespace cc |