[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> |
| 16 | VideoFrameProviderClientImpl::Create( |
| 17 | VideoFrameProvider* provider) { |
| 18 | return make_scoped_refptr( |
| 19 | new VideoFrameProviderClientImpl(provider)); |
| 20 | } |
| 21 | |
| 22 | VideoFrameProviderClientImpl::~VideoFrameProviderClientImpl() {} |
| 23 | |
| 24 | VideoFrameProviderClientImpl::VideoFrameProviderClientImpl( |
| 25 | VideoFrameProvider* provider) |
kulkarni.a | 4015690f1 | 2014-10-10 13:50:06 | [diff] [blame] | 26 | : active_video_layer_(nullptr), provider_(provider) { |
[email protected] | 48871fc | 2013-01-23 07:36:51 | [diff] [blame] | 27 | // This only happens during a commit on the compositor thread while the main |
| 28 | // thread is blocked. That makes this a thread-safe call to set the video |
| 29 | // frame provider client that does not require a lock. The same is true of |
| 30 | // the call to Stop(). |
| 31 | provider_->SetVideoFrameProviderClient(this); |
| 32 | |
| 33 | // This matrix is the default transformation for stream textures, and flips |
| 34 | // on the Y axis. |
| 35 | stream_texture_matrix_ = gfx::Transform( |
| 36 | 1.0, 0.0, 0.0, 0.0, |
| 37 | 0.0, -1.0, 0.0, 1.0, |
| 38 | 0.0, 0.0, 1.0, 0.0, |
| 39 | 0.0, 0.0, 0.0, 1.0); |
| 40 | } |
| 41 | |
dongseong.hwang | ee88f0aa | 2015-02-10 19:39:59 | [diff] [blame^] | 42 | void VideoFrameProviderClientImpl::SetActiveVideoLayer( |
| 43 | VideoLayerImpl* video_layer) { |
| 44 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 45 | DCHECK(video_layer); |
| 46 | active_video_layer_ = video_layer; |
| 47 | } |
| 48 | |
[email protected] | 48871fc | 2013-01-23 07:36:51 | [diff] [blame] | 49 | void VideoFrameProviderClientImpl::Stop() { |
dongseong.hwang | ee88f0aa | 2015-02-10 19:39:59 | [diff] [blame^] | 50 | // It's called when the main thread is blocked, so lock isn't needed. |
[email protected] | 48871fc | 2013-01-23 07:36:51 | [diff] [blame] | 51 | if (!provider_) |
| 52 | return; |
dongseong.hwang | ee88f0aa | 2015-02-10 19:39:59 | [diff] [blame^] | 53 | DCHECK(thread_checker_.CalledOnValidThread()); |
kulkarni.a | 4015690f1 | 2014-10-10 13:50:06 | [diff] [blame] | 54 | provider_->SetVideoFrameProviderClient(nullptr); |
| 55 | provider_ = nullptr; |
[email protected] | 48871fc | 2013-01-23 07:36:51 | [diff] [blame] | 56 | } |
| 57 | |
dongseong.hwang | ee88f0aa | 2015-02-10 19:39:59 | [diff] [blame^] | 58 | bool VideoFrameProviderClientImpl::Stopped() { |
| 59 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 60 | // |provider_| is changed while the main thread is blocked, and not changed |
| 61 | // thereafter, so lock isn't needed. |
| 62 | return !provider_; |
| 63 | } |
| 64 | |
[email protected] | af2cd32 | 2013-03-22 16:56:18 | [diff] [blame] | 65 | scoped_refptr<media::VideoFrame> |
| 66 | VideoFrameProviderClientImpl::AcquireLockAndCurrentFrame() { |
dongseong.hwang | ee88f0aa | 2015-02-10 19:39:59 | [diff] [blame^] | 67 | DCHECK(thread_checker_.CalledOnValidThread()); |
[email protected] | 48871fc | 2013-01-23 07:36:51 | [diff] [blame] | 68 | provider_lock_.Acquire(); // Balanced by call to ReleaseLock(). |
| 69 | if (!provider_) |
kulkarni.a | 4015690f1 | 2014-10-10 13:50:06 | [diff] [blame] | 70 | return nullptr; |
[email protected] | 48871fc | 2013-01-23 07:36:51 | [diff] [blame] | 71 | |
| 72 | return provider_->GetCurrentFrame(); |
| 73 | } |
| 74 | |
[email protected] | af2cd32 | 2013-03-22 16:56:18 | [diff] [blame] | 75 | void VideoFrameProviderClientImpl::PutCurrentFrame( |
| 76 | const scoped_refptr<media::VideoFrame>& frame) { |
dongseong.hwang | ee88f0aa | 2015-02-10 19:39:59 | [diff] [blame^] | 77 | DCHECK(thread_checker_.CalledOnValidThread()); |
[email protected] | 48871fc | 2013-01-23 07:36:51 | [diff] [blame] | 78 | provider_lock_.AssertAcquired(); |
| 79 | provider_->PutCurrentFrame(frame); |
| 80 | } |
| 81 | |
| 82 | void VideoFrameProviderClientImpl::ReleaseLock() { |
dongseong.hwang | ee88f0aa | 2015-02-10 19:39:59 | [diff] [blame^] | 83 | DCHECK(thread_checker_.CalledOnValidThread()); |
[email protected] | 48871fc | 2013-01-23 07:36:51 | [diff] [blame] | 84 | provider_lock_.AssertAcquired(); |
| 85 | provider_lock_.Release(); |
| 86 | } |
| 87 | |
| 88 | void VideoFrameProviderClientImpl::StopUsingProvider() { |
| 89 | // Block the provider from shutting down until this client is done |
| 90 | // using the frame. |
| 91 | base::AutoLock locker(provider_lock_); |
kulkarni.a | 4015690f1 | 2014-10-10 13:50:06 | [diff] [blame] | 92 | provider_ = nullptr; |
[email protected] | 48871fc | 2013-01-23 07:36:51 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | void VideoFrameProviderClientImpl::DidReceiveFrame() { |
[email protected] | 9469d78 | 2014-04-18 01:23:56 | [diff] [blame] | 96 | TRACE_EVENT1("cc", |
| 97 | "VideoFrameProviderClientImpl::DidReceiveFrame", |
| 98 | "active_video_layer", |
| 99 | !!active_video_layer_); |
dongseong.hwang | ee88f0aa | 2015-02-10 19:39:59 | [diff] [blame^] | 100 | DCHECK(thread_checker_.CalledOnValidThread()); |
[email protected] | 48871fc | 2013-01-23 07:36:51 | [diff] [blame] | 101 | if (active_video_layer_) |
[email protected] | 4af49d54 | 2013-03-15 22:18:47 | [diff] [blame] | 102 | active_video_layer_->SetNeedsRedraw(); |
[email protected] | 48871fc | 2013-01-23 07:36:51 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | void VideoFrameProviderClientImpl::DidUpdateMatrix(const float* matrix) { |
dongseong.hwang | ee88f0aa | 2015-02-10 19:39:59 | [diff] [blame^] | 106 | DCHECK(thread_checker_.CalledOnValidThread()); |
[email protected] | 48871fc | 2013-01-23 07:36:51 | [diff] [blame] | 107 | stream_texture_matrix_ = gfx::Transform( |
| 108 | matrix[0], matrix[4], matrix[8], matrix[12], |
| 109 | matrix[1], matrix[5], matrix[9], matrix[13], |
| 110 | matrix[2], matrix[6], matrix[10], matrix[14], |
| 111 | matrix[3], matrix[7], matrix[11], matrix[15]); |
| 112 | if (active_video_layer_) |
[email protected] | 4af49d54 | 2013-03-15 22:18:47 | [diff] [blame] | 113 | active_video_layer_->SetNeedsRedraw(); |
[email protected] | 48871fc | 2013-01-23 07:36:51 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | } // namespace cc |