| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "content/renderer/media/android/stream_texture_factory_synchronous_impl
.h" | |
| 6 | |
| 7 #include <algorithm> | |
| 8 | |
| 9 #include "base/bind.h" | |
| 10 #include "base/callback.h" | |
| 11 #include "base/location.h" | |
| 12 #include "base/macros.h" | |
| 13 #include "base/memory/weak_ptr.h" | |
| 14 #include "base/process/process_handle.h" | |
| 15 #include "base/single_thread_task_runner.h" | |
| 16 #include "base/synchronization/lock.h" | |
| 17 #include "cc/output/context_provider.h" | |
| 18 #include "content/renderer/render_thread_impl.h" | |
| 19 #include "gpu/command_buffer/client/gles2_interface.h" | |
| 20 #include "gpu/ipc/common/android/surface_texture_peer.h" | |
| 21 #include "ui/gl/android/surface_texture.h" | |
| 22 | |
| 23 using gpu::gles2::GLES2Interface; | |
| 24 | |
| 25 namespace content { | |
| 26 | |
| 27 namespace { | |
| 28 | |
| 29 class StreamTextureProxyImpl | |
| 30 : public StreamTextureProxy, | |
| 31 public base::SupportsWeakPtr<StreamTextureProxyImpl> { | |
| 32 public: | |
| 33 explicit StreamTextureProxyImpl( | |
| 34 StreamTextureFactorySynchronousImpl::ContextProvider* provider); | |
| 35 ~StreamTextureProxyImpl() override; | |
| 36 | |
| 37 // StreamTextureProxy implementation: | |
| 38 void BindToLoop(int32_t stream_id, | |
| 39 cc::VideoFrameProvider::Client* client, | |
| 40 scoped_refptr<base::SingleThreadTaskRunner> loop) override; | |
| 41 void Release() override; | |
| 42 | |
| 43 private: | |
| 44 void BindOnThread(int32_t stream_id); | |
| 45 void OnFrameAvailable(); | |
| 46 | |
| 47 // Protects access to |client_| and |loop_|. | |
| 48 base::Lock lock_; | |
| 49 cc::VideoFrameProvider::Client* client_; | |
| 50 scoped_refptr<base::SingleThreadTaskRunner> loop_; | |
| 51 | |
| 52 // Accessed on the |loop_| thread only. | |
| 53 base::Closure callback_; | |
| 54 scoped_refptr<StreamTextureFactorySynchronousImpl::ContextProvider> | |
| 55 context_provider_; | |
| 56 scoped_refptr<gfx::SurfaceTexture> surface_texture_; | |
| 57 | |
| 58 DISALLOW_IMPLICIT_CONSTRUCTORS(StreamTextureProxyImpl); | |
| 59 }; | |
| 60 | |
| 61 StreamTextureProxyImpl::StreamTextureProxyImpl( | |
| 62 StreamTextureFactorySynchronousImpl::ContextProvider* provider) | |
| 63 : client_(NULL), context_provider_(provider) { | |
| 64 } | |
| 65 | |
| 66 StreamTextureProxyImpl::~StreamTextureProxyImpl() {} | |
| 67 | |
| 68 void StreamTextureProxyImpl::Release() { | |
| 69 { | |
| 70 // Cannot call into |client_| anymore (from any thread) after returning | |
| 71 // from here. | |
| 72 base::AutoLock lock(lock_); | |
| 73 client_ = NULL; | |
| 74 } | |
| 75 // Release is analogous to the destructor, so there should be no more external | |
| 76 // calls to this object in Release. Therefore there is no need to acquire the | |
| 77 // lock to access |loop_|. | |
| 78 if (!loop_.get() || loop_->BelongsToCurrentThread() || | |
| 79 !loop_->DeleteSoon(FROM_HERE, this)) { | |
| 80 delete this; | |
| 81 } | |
| 82 } | |
| 83 | |
| 84 void StreamTextureProxyImpl::BindToLoop( | |
| 85 int32_t stream_id, | |
| 86 cc::VideoFrameProvider::Client* client, | |
| 87 scoped_refptr<base::SingleThreadTaskRunner> loop) { | |
| 88 DCHECK(loop.get()); | |
| 89 | |
| 90 { | |
| 91 base::AutoLock lock(lock_); | |
| 92 DCHECK(!loop_.get() || (loop.get() == loop_.get())); | |
| 93 loop_ = loop; | |
| 94 client_ = client; | |
| 95 } | |
| 96 | |
| 97 if (loop->BelongsToCurrentThread()) { | |
| 98 BindOnThread(stream_id); | |
| 99 return; | |
| 100 } | |
| 101 // Unretained is safe here only because the object is deleted on |loop_| | |
| 102 // thread. | |
| 103 loop->PostTask(FROM_HERE, | |
| 104 base::Bind(&StreamTextureProxyImpl::BindOnThread, | |
| 105 base::Unretained(this), | |
| 106 stream_id)); | |
| 107 } | |
| 108 | |
| 109 void StreamTextureProxyImpl::BindOnThread(int32_t stream_id) { | |
| 110 surface_texture_ = context_provider_->GetSurfaceTexture(stream_id); | |
| 111 if (!surface_texture_.get()) { | |
| 112 LOG(ERROR) << "Failed to get SurfaceTexture for stream."; | |
| 113 return; | |
| 114 } | |
| 115 | |
| 116 callback_ = | |
| 117 base::Bind(&StreamTextureProxyImpl::OnFrameAvailable, AsWeakPtr()); | |
| 118 surface_texture_->SetFrameAvailableCallback(callback_); | |
| 119 } | |
| 120 | |
| 121 void StreamTextureProxyImpl::OnFrameAvailable() { | |
| 122 base::AutoLock lock(lock_); | |
| 123 if (client_) | |
| 124 client_->DidReceiveFrame(); | |
| 125 } | |
| 126 | |
| 127 } // namespace | |
| 128 | |
| 129 // static | |
| 130 scoped_refptr<StreamTextureFactorySynchronousImpl> | |
| 131 StreamTextureFactorySynchronousImpl::Create( | |
| 132 const CreateContextProviderCallback& try_create_callback) { | |
| 133 return new StreamTextureFactorySynchronousImpl(try_create_callback); | |
| 134 } | |
| 135 | |
| 136 StreamTextureFactorySynchronousImpl::StreamTextureFactorySynchronousImpl( | |
| 137 const CreateContextProviderCallback& try_create_callback) | |
| 138 : create_context_provider_callback_(try_create_callback) {} | |
| 139 | |
| 140 StreamTextureFactorySynchronousImpl::~StreamTextureFactorySynchronousImpl() {} | |
| 141 | |
| 142 StreamTextureProxy* StreamTextureFactorySynchronousImpl::CreateProxy() { | |
| 143 bool had_proxy = !!context_provider_.get(); | |
| 144 if (!had_proxy) | |
| 145 context_provider_ = create_context_provider_callback_.Run(); | |
| 146 | |
| 147 if (!context_provider_.get()) | |
| 148 return NULL; | |
| 149 | |
| 150 if (!observers_.empty() && !had_proxy) { | |
| 151 for (auto& observer : observers_) | |
| 152 context_provider_->AddObserver(observer); | |
| 153 } | |
| 154 return new StreamTextureProxyImpl(context_provider_.get()); | |
| 155 } | |
| 156 | |
| 157 void StreamTextureFactorySynchronousImpl::EstablishPeer(int32_t stream_id, | |
| 158 int player_id, | |
| 159 int frame_id) { | |
| 160 DCHECK(context_provider_.get()); | |
| 161 scoped_refptr<gfx::SurfaceTexture> surface_texture = | |
| 162 context_provider_->GetSurfaceTexture(stream_id); | |
| 163 if (surface_texture.get()) { | |
| 164 gpu::SurfaceTexturePeer::GetInstance()->EstablishSurfaceTexturePeer( | |
| 165 base::GetCurrentProcessHandle(), surface_texture, frame_id, player_id); | |
| 166 } | |
| 167 } | |
| 168 | |
| 169 unsigned StreamTextureFactorySynchronousImpl::CreateStreamTexture( | |
| 170 unsigned texture_target, | |
| 171 unsigned* texture_id, | |
| 172 gpu::Mailbox* texture_mailbox) { | |
| 173 DCHECK(context_provider_.get()); | |
| 174 unsigned stream_id = 0; | |
| 175 GLES2Interface* gl = context_provider_->ContextGL(); | |
| 176 gl->GenTextures(1, texture_id); | |
| 177 gl->ShallowFlushCHROMIUM(); | |
| 178 stream_id = context_provider_->CreateStreamTexture(*texture_id); | |
| 179 gl->GenMailboxCHROMIUM(texture_mailbox->name); | |
| 180 gl->ProduceTextureDirectCHROMIUM( | |
| 181 *texture_id, texture_target, texture_mailbox->name); | |
| 182 return stream_id; | |
| 183 } | |
| 184 | |
| 185 void StreamTextureFactorySynchronousImpl::SetStreamTextureSize( | |
| 186 int32_t stream_id, | |
| 187 const gfx::Size& size) {} | |
| 188 | |
| 189 gpu::gles2::GLES2Interface* StreamTextureFactorySynchronousImpl::ContextGL() { | |
| 190 DCHECK(context_provider_.get()); | |
| 191 return context_provider_->ContextGL(); | |
| 192 } | |
| 193 | |
| 194 void StreamTextureFactorySynchronousImpl::AddObserver( | |
| 195 StreamTextureFactoryContextObserver* obs) { | |
| 196 DCHECK(observers_.find(obs) == observers_.end()); | |
| 197 observers_.insert(obs); | |
| 198 if (context_provider_.get()) | |
| 199 context_provider_->AddObserver(obs); | |
| 200 } | |
| 201 | |
| 202 void StreamTextureFactorySynchronousImpl::RemoveObserver( | |
| 203 StreamTextureFactoryContextObserver* obs) { | |
| 204 DCHECK(observers_.find(obs) != observers_.end()); | |
| 205 observers_.erase(obs); | |
| 206 if (context_provider_.get()) | |
| 207 context_provider_->RemoveObserver(obs); | |
| 208 } | |
| 209 | |
| 210 } // namespace content | |
| OLD | NEW |