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 #ifndef CC_LAYERS_VIDEO_FRAME_PROVIDER_CLIENT_IMPL_H_
6 #define CC_LAYERS_VIDEO_FRAME_PROVIDER_CLIENT_IMPL_H_
7 
8 #include "base/memory/ref_counted.h"
9 #include "base/synchronization/lock.h"
10 #include "base/threading/thread_checker.h"
11 #include "cc/cc_export.h"
12 #include "cc/layers/video_frame_provider.h"
13 #include "cc/scheduler/video_frame_controller.h"
14 #include "ui/gfx/transform.h"
15 
16 namespace media { class VideoFrame; }
17 
18 namespace cc {
19 class VideoLayerImpl;
20 
21 // VideoFrameProviderClientImpl liaisons with the VideoFrameProvider and the
22 // VideoLayer. It receives updates from the provider and updates the layer as a
23 // result. It also allows the layer to access the video frame that the provider
24 // has.
25 class CC_EXPORT VideoFrameProviderClientImpl
26     : public VideoFrameProvider::Client,
27       public VideoFrameController,
28       public base::RefCounted<VideoFrameProviderClientImpl> {
29  public:
30   // Must be created on the impl thread while the main thread is blocked.
31   static scoped_refptr<VideoFrameProviderClientImpl> Create(
32       VideoFrameProvider* provider,
33       VideoFrameControllerClient* client);
34 
35   VideoFrameProviderClientImpl(const VideoFrameProviderClientImpl&) = delete;
36   VideoFrameProviderClientImpl& operator=(const VideoFrameProviderClientImpl&) =
37       delete;
38 
39   VideoLayerImpl* ActiveVideoLayer() const;
40   void SetActiveVideoLayer(VideoLayerImpl* video_layer);
41 
42   bool Stopped() const;
43   // Must be called on the impl thread while the main thread is blocked.
44   void Stop();
45 
46   scoped_refptr<media::VideoFrame> AcquireLockAndCurrentFrame()
47       EXCLUSIVE_LOCK_FUNCTION(provider_lock_);
48   void PutCurrentFrame() EXCLUSIVE_LOCKS_REQUIRED(provider_lock_);
49   void ReleaseLock() UNLOCK_FUNCTION(provider_lock_);
AssertLocked()50   void AssertLocked() const ASSERT_EXCLUSIVE_LOCK(provider_lock_) {
51     provider_lock_.AssertAcquired();
52   }
53   bool HasCurrentFrame();
54 
55   // VideoFrameController implementation.
56   void OnBeginFrame(const viz::BeginFrameArgs& args) override;
57   void DidDrawFrame() override;
58 
59   // VideoFrameProvider::Client implementation.
60   // Called on the main thread.
61   void StopUsingProvider() override;
62   // Called on the impl thread.
63   void StartRendering() override;
64   void StopRendering() override;
65   void DidReceiveFrame() override;
66   bool IsDrivingFrameUpdates() const override;
67 
get_provider_for_testing()68   const VideoFrameProvider* get_provider_for_testing() const {
69     return provider_;
70   }
71 
72  private:
73   friend class base::RefCounted<VideoFrameProviderClientImpl>;
74 
75   VideoFrameProviderClientImpl(VideoFrameProvider* provider,
76                                VideoFrameControllerClient* client);
77   ~VideoFrameProviderClientImpl() override;
78 
79   VideoFrameProvider* provider_;
80   VideoFrameControllerClient* client_;
81   VideoLayerImpl* active_video_layer_;
82   bool stopped_;
83   bool rendering_;
84   bool needs_put_current_frame_;
85 
86   // Since the provider lives on another thread, it can be destroyed while the
87   // frame controller are accessing its frame. Before being destroyed the
88   // provider calls StopUsingProvider. provider_lock_ blocks StopUsingProvider
89   // from returning until the frame controller is done using the frame.
90   base::Lock provider_lock_;
91   base::ThreadChecker thread_checker_;
92 };
93 
94 }  // namespace cc
95 
96 #endif  // CC_LAYERS_VIDEO_FRAME_PROVIDER_CLIENT_IMPL_H_
97