| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2016 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_wrapper_impl.h" |
| 6 |
| 7 #include "base/callback.h" |
| 8 #include "cc/layers/video_frame_provider.h" |
| 9 #include "gpu/GLES2/gl2extchromium.h" |
| 10 #include "gpu/command_buffer/client/gles2_interface.h" |
| 11 #include "media/base/bind_to_current_loop.h" |
| 12 |
| 13 using gpu::gles2::GLES2Interface; |
| 14 |
| 15 static const uint32_t kGLTextureExternalOES = GL_TEXTURE_EXTERNAL_OES; |
| 16 |
| 17 namespace { |
| 18 // Non-member function to allow it to run even after this class is deleted. |
| 19 void OnReleaseTexture(scoped_refptr<content::StreamTextureFactory> factories, |
| 20 uint32_t texture_id, |
| 21 const gpu::SyncToken& sync_token) { |
| 22 GLES2Interface* gl = factories->ContextGL(); |
| 23 gl->WaitSyncTokenCHROMIUM(sync_token.GetConstData()); |
| 24 gl->DeleteTextures(1, &texture_id); |
| 25 // Flush to ensure that the stream texture gets deleted in a timely fashion. |
| 26 gl->ShallowFlushCHROMIUM(); |
| 27 } |
| 28 } |
| 29 |
| 30 namespace content { |
| 31 |
| 32 StreamTextureWrapperImpl::StreamTextureWrapperImpl( |
| 33 scoped_refptr<StreamTextureFactory> factory, |
| 34 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner) |
| 35 : texture_id_(0), |
| 36 stream_id_(0), |
| 37 factory_(factory), |
| 38 main_task_runner_(main_task_runner), |
| 39 weak_factory_(this) {} |
| 40 |
| 41 StreamTextureWrapperImpl::~StreamTextureWrapperImpl() { |
| 42 DCHECK(main_task_runner_->BelongsToCurrentThread()); |
| 43 |
| 44 if (stream_id_) { |
| 45 GLES2Interface* gl = factory_->ContextGL(); |
| 46 gl->DeleteTextures(1, &texture_id_); |
| 47 // Flush to ensure that the stream texture gets deleted in a timely fashion. |
| 48 gl->ShallowFlushCHROMIUM(); |
| 49 } |
| 50 |
| 51 SetCurrentFrameInternal(nullptr); |
| 52 } |
| 53 |
| 54 media::ScopedStreamTextureWrapper StreamTextureWrapperImpl::Create( |
| 55 scoped_refptr<StreamTextureFactory> factory, |
| 56 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner) { |
| 57 return media::ScopedStreamTextureWrapper( |
| 58 new StreamTextureWrapperImpl(factory, main_task_runner)); |
| 59 } |
| 60 |
| 61 scoped_refptr<media::VideoFrame> StreamTextureWrapperImpl::GetCurrentFrame() { |
| 62 base::AutoLock auto_lock(current_frame_lock_); |
| 63 return current_frame_; |
| 64 } |
| 65 |
| 66 void StreamTextureWrapperImpl::ReallocateVideoFrame( |
| 67 const gfx::Size& natural_size) { |
| 68 DVLOG(2) << __FUNCTION__; |
| 69 DCHECK(main_task_runner_->BelongsToCurrentThread()); |
| 70 |
| 71 GLES2Interface* gl = factory_->ContextGL(); |
| 72 GLuint texture_target = kGLTextureExternalOES; |
| 73 GLuint texture_id_ref = gl->CreateAndConsumeTextureCHROMIUM( |
| 74 texture_target, texture_mailbox_.name); |
| 75 const GLuint64 fence_sync = gl->InsertFenceSyncCHROMIUM(); |
| 76 gl->Flush(); |
| 77 |
| 78 gpu::SyncToken texture_mailbox_sync_token; |
| 79 gl->GenUnverifiedSyncTokenCHROMIUM(fence_sync, |
| 80 texture_mailbox_sync_token.GetData()); |
| 81 if (texture_mailbox_sync_token.namespace_id() == |
| 82 gpu::CommandBufferNamespace::IN_PROCESS) { |
| 83 // TODO(boliu): Remove this once Android WebView switches to IPC-based |
| 84 // command buffer for video. |
| 85 GLbyte* sync_tokens[] = {texture_mailbox_sync_token.GetData()}; |
| 86 gl->VerifySyncTokensCHROMIUM(sync_tokens, arraysize(sync_tokens)); |
| 87 } |
| 88 |
| 89 gpu::MailboxHolder holders[media::VideoFrame::kMaxPlanes] = { |
| 90 gpu::MailboxHolder(texture_mailbox_, texture_mailbox_sync_token, |
| 91 texture_target)}; |
| 92 |
| 93 scoped_refptr<media::VideoFrame> new_frame = |
| 94 media::VideoFrame::WrapNativeTextures( |
| 95 media::PIXEL_FORMAT_ARGB, holders, |
| 96 media::BindToCurrentLoop( |
| 97 base::Bind(&OnReleaseTexture, factory_, texture_id_ref)), |
| 98 natural_size, gfx::Rect(natural_size), natural_size, |
| 99 base::TimeDelta()); |
| 100 |
| 101 // TODO(tguilbert): Create and pipe the enable_texture_copy_ flag for Webview |
| 102 // scenarios. See crbug.com/628066. |
| 103 |
| 104 SetCurrentFrameInternal(new_frame); |
| 105 } |
| 106 |
| 107 void StreamTextureWrapperImpl::SetCurrentFrameInternal( |
| 108 const scoped_refptr<media::VideoFrame>& video_frame) { |
| 109 base::AutoLock auto_lock(current_frame_lock_); |
| 110 current_frame_ = video_frame; |
| 111 } |
| 112 |
| 113 void StreamTextureWrapperImpl::UpdateTextureSize(const gfx::Size& new_size) { |
| 114 DVLOG(2) << __FUNCTION__; |
| 115 |
| 116 if (!main_task_runner_->BelongsToCurrentThread()) { |
| 117 main_task_runner_->PostTask( |
| 118 FROM_HERE, base::Bind(&StreamTextureWrapperImpl::UpdateTextureSize, |
| 119 weak_factory_.GetWeakPtr(), new_size)); |
| 120 return; |
| 121 } |
| 122 |
| 123 if (natural_size_ == new_size) |
| 124 return; |
| 125 |
| 126 natural_size_ = new_size; |
| 127 |
| 128 ReallocateVideoFrame(new_size); |
| 129 factory_->SetStreamTextureSize(stream_id_, new_size); |
| 130 } |
| 131 |
| 132 void StreamTextureWrapperImpl::Initialize( |
| 133 cc::VideoFrameProvider::Client* client, |
| 134 const gfx::Size& natural_size, |
| 135 scoped_refptr<base::SingleThreadTaskRunner> compositor_task_runner, |
| 136 const base::Closure& init_cb) { |
| 137 DVLOG(2) << __FUNCTION__; |
| 138 |
| 139 compositor_task_runner_ = compositor_task_runner; |
| 140 natural_size_ = natural_size; |
| 141 |
| 142 main_task_runner_->PostTask( |
| 143 FROM_HERE, base::Bind(&StreamTextureWrapperImpl::InitializeOnMainThread, |
| 144 weak_factory_.GetWeakPtr(), client, |
| 145 media::BindToCurrentLoop(init_cb))); |
| 146 } |
| 147 |
| 148 void StreamTextureWrapperImpl::InitializeOnMainThread( |
| 149 cc::VideoFrameProvider::Client* client, |
| 150 const base::Closure& init_cb) { |
| 151 DCHECK(main_task_runner_->BelongsToCurrentThread()); |
| 152 DVLOG(2) << __FUNCTION__; |
| 153 |
| 154 stream_texture_proxy_.reset(factory_->CreateProxy()); |
| 155 |
| 156 stream_id_ = factory_->CreateStreamTexture(kGLTextureExternalOES, |
| 157 &texture_id_, &texture_mailbox_); |
| 158 ReallocateVideoFrame(natural_size_); |
| 159 |
| 160 stream_texture_proxy_->BindToLoop(stream_id_, client, |
| 161 compositor_task_runner_); |
| 162 |
| 163 // TODO(tguilbert): Register the surface properly. See crbug.com/627658. |
| 164 |
| 165 // |init_cb| is bound to the thread that originally called Initialize(). |
| 166 init_cb.Run(); |
| 167 } |
| 168 |
| 169 void StreamTextureWrapperImpl::Destroy() { |
| 170 // Note: StreamTextureProxy will release its reference to |client| |
| 171 // immediately (and stop calling back DidReceiveFrame()). |
| 172 stream_texture_proxy_.reset(); |
| 173 |
| 174 if (!main_task_runner_->BelongsToCurrentThread()) { |
| 175 // base::Unretained is safe here because this function is the only one that |
| 176 // can can call delete. |
| 177 main_task_runner_->PostTask( |
| 178 FROM_HERE, |
| 179 base::Bind(&StreamTextureWrapperImpl::Destroy, base::Unretained(this))); |
| 180 return; |
| 181 } |
| 182 |
| 183 delete this; |
| 184 } |
| 185 |
| 186 } // namespace content |
| OLD | NEW |