OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/renderer/media/android/stream_texture_factory_synchronous_impl
.h" | 5 #include "content/renderer/media/android/stream_texture_factory_synchronous_impl
.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/callback.h" | 10 #include "base/callback.h" |
(...skipping 16 matching lines...) Expand all Loading... |
27 | 27 |
28 class StreamTextureProxyImpl | 28 class StreamTextureProxyImpl |
29 : public StreamTextureProxy, | 29 : public StreamTextureProxy, |
30 public base::SupportsWeakPtr<StreamTextureProxyImpl> { | 30 public base::SupportsWeakPtr<StreamTextureProxyImpl> { |
31 public: | 31 public: |
32 explicit StreamTextureProxyImpl( | 32 explicit StreamTextureProxyImpl( |
33 StreamTextureFactorySynchronousImpl::ContextProvider* provider); | 33 StreamTextureFactorySynchronousImpl::ContextProvider* provider); |
34 virtual ~StreamTextureProxyImpl(); | 34 virtual ~StreamTextureProxyImpl(); |
35 | 35 |
36 // StreamTextureProxy implementation: | 36 // StreamTextureProxy implementation: |
37 virtual void BindToCurrentThread(int32 stream_id) OVERRIDE; | 37 virtual void BindToLoop(int32 stream_id, |
38 virtual void SetClient(cc::VideoFrameProvider::Client* client) OVERRIDE; | 38 cc::VideoFrameProvider::Client* client, |
| 39 scoped_refptr<base::MessageLoopProxy> loop) OVERRIDE; |
39 virtual void Release() OVERRIDE; | 40 virtual void Release() OVERRIDE; |
40 | 41 |
41 private: | 42 private: |
| 43 void SetClient(cc::VideoFrameProvider::Client* client); |
| 44 void BindOnThread(int32 stream_id, |
| 45 scoped_refptr<base::MessageLoopProxy> loop); |
42 void OnFrameAvailable(); | 46 void OnFrameAvailable(); |
43 | 47 |
44 scoped_refptr<base::MessageLoopProxy> loop_; | |
45 base::Lock client_lock_; | 48 base::Lock client_lock_; |
46 cc::VideoFrameProvider::Client* client_; | 49 cc::VideoFrameProvider::Client* client_; |
| 50 |
| 51 // Accessed on the |loop_| thread only. |
| 52 scoped_refptr<base::MessageLoopProxy> loop_; |
47 base::Closure callback_; | 53 base::Closure callback_; |
48 | |
49 scoped_refptr<StreamTextureFactorySynchronousImpl::ContextProvider> | 54 scoped_refptr<StreamTextureFactorySynchronousImpl::ContextProvider> |
50 context_provider_; | 55 context_provider_; |
51 scoped_refptr<gfx::SurfaceTexture> surface_texture_; | 56 scoped_refptr<gfx::SurfaceTexture> surface_texture_; |
52 | |
53 float current_matrix_[16]; | 57 float current_matrix_[16]; |
54 bool has_updated_; | 58 bool has_updated_; |
55 | 59 |
56 DISALLOW_IMPLICIT_CONSTRUCTORS(StreamTextureProxyImpl); | 60 DISALLOW_IMPLICIT_CONSTRUCTORS(StreamTextureProxyImpl); |
57 }; | 61 }; |
58 | 62 |
59 StreamTextureProxyImpl::StreamTextureProxyImpl( | 63 StreamTextureProxyImpl::StreamTextureProxyImpl( |
60 StreamTextureFactorySynchronousImpl::ContextProvider* provider) | 64 StreamTextureFactorySynchronousImpl::ContextProvider* provider) |
61 : context_provider_(provider), has_updated_(false) { | 65 : client_(NULL), context_provider_(provider), has_updated_(false) { |
62 std::fill(current_matrix_, current_matrix_ + 16, 0); | 66 std::fill(current_matrix_, current_matrix_ + 16, 0); |
63 } | 67 } |
64 | 68 |
65 StreamTextureProxyImpl::~StreamTextureProxyImpl() {} | 69 StreamTextureProxyImpl::~StreamTextureProxyImpl() {} |
66 | 70 |
67 void StreamTextureProxyImpl::Release() { | 71 void StreamTextureProxyImpl::Release() { |
| 72 // Assumes this is the last reference to this object. So no need to acquire |
| 73 // lock. |
68 SetClient(NULL); | 74 SetClient(NULL); |
69 if (loop_.get() && !loop_->BelongsToCurrentThread()) | 75 if (!loop_.get() || loop_->BelongsToCurrentThread() || |
70 loop_->DeleteSoon(FROM_HERE, this); | 76 !loop_->DeleteSoon(FROM_HERE, this)) { |
71 else | |
72 delete this; | 77 delete this; |
| 78 } |
73 } | 79 } |
74 | 80 |
75 void StreamTextureProxyImpl::SetClient(cc::VideoFrameProvider::Client* client) { | 81 void StreamTextureProxyImpl::SetClient(cc::VideoFrameProvider::Client* client) { |
76 base::AutoLock lock(client_lock_); | 82 base::AutoLock lock(client_lock_); |
77 client_ = client; | 83 client_ = client; |
78 } | 84 } |
79 | 85 |
80 void StreamTextureProxyImpl::BindToCurrentThread(int stream_id) { | 86 void StreamTextureProxyImpl::BindToLoop( |
81 loop_ = base::MessageLoopProxy::current(); | 87 int32 stream_id, |
| 88 cc::VideoFrameProvider::Client* client, |
| 89 scoped_refptr<base::MessageLoopProxy> loop) { |
| 90 DCHECK(loop); |
| 91 SetClient(client); |
| 92 if (loop->BelongsToCurrentThread()) { |
| 93 BindOnThread(stream_id, loop); |
| 94 return; |
| 95 } |
| 96 // Unretained is safe here only because the object is deleted on |loop_| |
| 97 // thread. |
| 98 loop->PostTask(FROM_HERE, |
| 99 base::Bind(&StreamTextureProxyImpl::BindOnThread, |
| 100 base::Unretained(this), |
| 101 stream_id, |
| 102 loop)); |
| 103 } |
| 104 |
| 105 void StreamTextureProxyImpl::BindOnThread( |
| 106 int32 stream_id, |
| 107 scoped_refptr<base::MessageLoopProxy> loop) { |
| 108 DCHECK(!loop_ || (loop == loop_)); |
| 109 loop_ = loop; |
| 110 |
82 surface_texture_ = context_provider_->GetSurfaceTexture(stream_id); | 111 surface_texture_ = context_provider_->GetSurfaceTexture(stream_id); |
83 if (!surface_texture_) { | 112 if (!surface_texture_) { |
84 LOG(ERROR) << "Failed to get SurfaceTexture for stream."; | 113 LOG(ERROR) << "Failed to get SurfaceTexture for stream."; |
85 return; | 114 return; |
86 } | 115 } |
87 | 116 |
88 callback_ = | 117 callback_ = |
89 base::Bind(&StreamTextureProxyImpl::OnFrameAvailable, AsWeakPtr()); | 118 base::Bind(&StreamTextureProxyImpl::OnFrameAvailable, AsWeakPtr()); |
90 surface_texture_->SetFrameAvailableCallback(callback_); | 119 surface_texture_->SetFrameAvailableCallback(callback_); |
91 } | 120 } |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
175 | 204 |
176 void StreamTextureFactorySynchronousImpl::SetStreamTextureSize( | 205 void StreamTextureFactorySynchronousImpl::SetStreamTextureSize( |
177 int32 stream_id, | 206 int32 stream_id, |
178 const gfx::Size& size) {} | 207 const gfx::Size& size) {} |
179 | 208 |
180 gpu::gles2::GLES2Interface* StreamTextureFactorySynchronousImpl::ContextGL() { | 209 gpu::gles2::GLES2Interface* StreamTextureFactorySynchronousImpl::ContextGL() { |
181 DCHECK(context_provider_); | 210 DCHECK(context_provider_); |
182 return context_provider_->ContextGL(); | 211 return context_provider_->ContextGL(); |
183 } | 212 } |
184 | 213 |
| 214 void StreamTextureFactorySynchronousImpl::AddObserver( |
| 215 StreamTextureFactoryContextObserver* obs) { |
| 216 context_provider_->AddObserver(obs); |
| 217 } |
| 218 |
| 219 void StreamTextureFactorySynchronousImpl::RemoveObserver( |
| 220 StreamTextureFactoryContextObserver* obs) { |
| 221 context_provider_->RemoveObserver(obs); |
| 222 } |
| 223 |
185 } // namespace content | 224 } // namespace content |
OLD | NEW |