- add sources.
[platform/framework/web/crosswalk.git] / src / cc / layers / video_frame_provider_client_impl.cc
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
5 #include "cc/layers/video_frame_provider_client_impl.h"
6
7 #include "cc/base/math_util.h"
8 #include "cc/layers/video_layer_impl.h"
9 #include "media/base/video_frame.h"
10
11 namespace cc {
12
13 // static
14 scoped_refptr<VideoFrameProviderClientImpl>
15     VideoFrameProviderClientImpl::Create(
16         VideoFrameProvider* provider) {
17   return make_scoped_refptr(
18       new VideoFrameProviderClientImpl(provider));
19 }
20
21 VideoFrameProviderClientImpl::~VideoFrameProviderClientImpl() {}
22
23 VideoFrameProviderClientImpl::VideoFrameProviderClientImpl(
24     VideoFrameProvider* provider)
25     : provider_(provider) {
26   // This only happens during a commit on the compositor thread while the main
27   // thread is blocked. That makes this a thread-safe call to set the video
28   // frame provider client that does not require a lock. The same is true of
29   // the call to Stop().
30   provider_->SetVideoFrameProviderClient(this);
31
32   // This matrix is the default transformation for stream textures, and flips
33   // on the Y axis.
34   stream_texture_matrix_ = gfx::Transform(
35       1.0, 0.0, 0.0, 0.0,
36       0.0, -1.0, 0.0, 1.0,
37       0.0, 0.0, 1.0, 0.0,
38       0.0, 0.0, 0.0, 1.0);
39 }
40
41 void VideoFrameProviderClientImpl::Stop() {
42   if (!provider_)
43     return;
44   provider_->SetVideoFrameProviderClient(NULL);
45   provider_ = NULL;
46 }
47
48 scoped_refptr<media::VideoFrame>
49 VideoFrameProviderClientImpl::AcquireLockAndCurrentFrame() {
50   provider_lock_.Acquire();  // Balanced by call to ReleaseLock().
51   if (!provider_)
52     return NULL;
53
54   return provider_->GetCurrentFrame();
55 }
56
57 void VideoFrameProviderClientImpl::PutCurrentFrame(
58     const scoped_refptr<media::VideoFrame>& frame) {
59   provider_lock_.AssertAcquired();
60   provider_->PutCurrentFrame(frame);
61 }
62
63 void VideoFrameProviderClientImpl::ReleaseLock() {
64   provider_lock_.AssertAcquired();
65   provider_lock_.Release();
66 }
67
68 void VideoFrameProviderClientImpl::StopUsingProvider() {
69   // Block the provider from shutting down until this client is done
70   // using the frame.
71   base::AutoLock locker(provider_lock_);
72   provider_ = NULL;
73 }
74
75 void VideoFrameProviderClientImpl::DidReceiveFrame() {
76   if (active_video_layer_)
77     active_video_layer_->SetNeedsRedraw();
78 }
79
80 void VideoFrameProviderClientImpl::DidUpdateMatrix(const float* matrix) {
81   stream_texture_matrix_ = gfx::Transform(
82       matrix[0], matrix[4], matrix[8], matrix[12],
83       matrix[1], matrix[5], matrix[9], matrix[13],
84       matrix[2], matrix[6], matrix[10], matrix[14],
85       matrix[3], matrix[7], matrix[11], matrix[15]);
86   if (active_video_layer_)
87     active_video_layer_->SetNeedsRedraw();
88 }
89
90 }  // namespace cc