Chromium Code Reviews| Index: content/renderer/media/android/stream_texture_wrapper_impl.cc |
| diff --git a/content/renderer/media/android/stream_texture_wrapper_impl.cc b/content/renderer/media/android/stream_texture_wrapper_impl.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..191d48172bffd92810a9dda99a254f0dc40d8305 |
| --- /dev/null |
| +++ b/content/renderer/media/android/stream_texture_wrapper_impl.cc |
| @@ -0,0 +1,177 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "content/renderer/media/android/stream_texture_wrapper_impl.h" |
| + |
| +#include "base/callback.h" |
| +#include "cc/layers/video_frame_provider.h" |
| +#include "gpu/GLES2/gl2extchromium.h" |
| +#include "gpu/command_buffer/client/gles2_interface.h" |
| +#include "media/base/bind_to_current_loop.h" |
| + |
| +using gpu::gles2::GLES2Interface; |
| + |
| +static const uint32_t kGLTextureExternalOES = GL_TEXTURE_EXTERNAL_OES; |
|
watk
2016/07/23 03:13:39
Any reason not to inline this?
tguilbert
2016/07/26 00:13:51
No strong reason other than, AFAIK, using the cons
|
| + |
| +namespace { |
| +// File-static function is to allow it to run even after this class is deleted. |
|
watk
2016/07/23 03:13:39
nit: Looks like you copied this, but "file-static"
tguilbert
2016/07/26 00:13:51
Done.
|
| +void OnReleaseTexture( |
| + const scoped_refptr<content::StreamTextureFactory>& factories, |
| + uint32_t texture_id, |
| + const gpu::SyncToken& sync_token) { |
| + GLES2Interface* gl = factories->ContextGL(); |
| + gl->WaitSyncTokenCHROMIUM(sync_token.GetConstData()); |
| + gl->DeleteTextures(1, &texture_id); |
| + // Flush to ensure that the stream texture gets deleted in a timely fashion. |
| + gl->ShallowFlushCHROMIUM(); |
| +} |
| +} |
| + |
| +namespace content { |
| + |
| +StreamTextureWrapperImpl::StreamTextureWrapperImpl( |
| + scoped_refptr<StreamTextureFactory> factory, |
| + scoped_refptr<base::SingleThreadTaskRunner> main_task_runner) |
| + : texture_id_(0), |
| + stream_id_(0), |
| + client_(nullptr), |
| + factory_(factory), |
| + main_task_runner_(main_task_runner), |
| + weak_factory_(this) {} |
| + |
| +StreamTextureWrapperImpl::~StreamTextureWrapperImpl() { |
| + DCHECK(main_task_runner_->BelongsToCurrentThread()); |
| + |
| + if (stream_id_) { |
| + GLES2Interface* gl = factory_->ContextGL(); |
| + gl->DeleteTextures(1, &texture_id_); |
| + // Flush to ensure that the stream texture gets deleted in a timely fashion. |
| + gl->ShallowFlushCHROMIUM(); |
| + } |
| + |
| + SetCurrentFrameInternal(nullptr); |
| +} |
| + |
| +scoped_refptr<media::VideoFrame> StreamTextureWrapperImpl::GetCurrentFrame() { |
| + base::AutoLock auto_lock(current_frame_lock_); |
| + return current_frame_; |
| +} |
| + |
| +void StreamTextureWrapperImpl::ReallocateVideoFrame( |
| + const gfx::Size& natural_size) { |
| + DVLOG(2) << __FUNCTION__; |
| + DCHECK(main_task_runner_->BelongsToCurrentThread()); |
| + |
| + GLES2Interface* gl = factory_->ContextGL(); |
| + GLuint texture_target = kGLTextureExternalOES; |
| + GLuint texture_id_ref = gl->CreateAndConsumeTextureCHROMIUM( |
| + texture_target, texture_mailbox_.name); |
| + const GLuint64 fence_sync = gl->InsertFenceSyncCHROMIUM(); |
| + gl->Flush(); |
| + |
| + gpu::SyncToken texture_mailbox_sync_token; |
| + gl->GenUnverifiedSyncTokenCHROMIUM(fence_sync, |
| + texture_mailbox_sync_token.GetData()); |
| + if (texture_mailbox_sync_token.namespace_id() == |
| + gpu::CommandBufferNamespace::IN_PROCESS) { |
| + // TODO(boliu): Remove this once Android WebView switches to IPC-based |
| + // command buffer for video. |
| + GLbyte* sync_tokens[] = {texture_mailbox_sync_token.GetData()}; |
| + gl->VerifySyncTokensCHROMIUM(sync_tokens, arraysize(sync_tokens)); |
| + } |
| + |
| + gpu::MailboxHolder holders[media::VideoFrame::kMaxPlanes] = { |
| + gpu::MailboxHolder(texture_mailbox_, texture_mailbox_sync_token, |
| + texture_target)}; |
| + |
| + scoped_refptr<media::VideoFrame> new_frame = |
| + media::VideoFrame::WrapNativeTextures( |
| + media::PIXEL_FORMAT_ARGB, holders, |
| + media::BindToCurrentLoop( |
| + base::Bind(&OnReleaseTexture, factory_, texture_id_ref)), |
| + natural_size, gfx::Rect(natural_size), natural_size, |
| + base::TimeDelta()); |
| + |
| + // TODO(tguilbert): Create and pipe the enable_texture_copy_ flag for Webview |
| + // scenarios. See crbug.com/628066. |
| + // |
| + // if (new_frame.get()) { |
| + // new_frame->metadata()->SetBoolean( |
| + // media::VideoFrameMetadata::COPY_REQUIRED, enable_texture_copy_); |
| + // } |
|
watk
2016/07/23 03:13:39
Delete commented code? Put it in the bug maybe.
tguilbert
2016/07/26 00:13:51
Done.
|
| + |
| + SetCurrentFrameInternal(new_frame); |
| +} |
| + |
| +void StreamTextureWrapperImpl::SetCurrentFrameInternal( |
| + const scoped_refptr<media::VideoFrame>& video_frame) { |
| + base::AutoLock auto_lock(current_frame_lock_); |
| + current_frame_ = video_frame; |
| +} |
| + |
| +void StreamTextureWrapperImpl::UpdateTextureSize(const gfx::Size& new_size) { |
| + DVLOG(2) << __FUNCTION__; |
| + |
| + if (!main_task_runner_->BelongsToCurrentThread()) { |
| + main_task_runner_->PostTask( |
| + FROM_HERE, base::Bind(&StreamTextureWrapperImpl::UpdateTextureSize, |
| + weak_factory_.GetWeakPtr(), new_size)); |
| + return; |
| + } |
| + |
| + if (natural_size_ == new_size) |
| + return; |
| + |
| + natural_size_ = new_size; |
| + |
| + ReallocateVideoFrame(new_size); |
| + factory_->SetStreamTextureSize(stream_id_, new_size); |
| +} |
| + |
| +void StreamTextureWrapperImpl::Initialize( |
| + cc::VideoFrameProvider::Client* client, |
| + const gfx::Size& natural_size, |
| + scoped_refptr<base::SingleThreadTaskRunner> compositor_task_runner, |
| + const base::Closure& init_cb) { |
| + DVLOG(2) << __FUNCTION__; |
| + |
| + compositor_task_runner_ = compositor_task_runner; |
| + natural_size_ = natural_size; |
| + client_ = client; |
| + |
| + main_task_runner_->PostTask( |
|
liberato (no reviews please)
2016/07/22 16:13:57
not sure if this matters for how you're using this
tguilbert
2016/07/26 00:13:51
It would not have been a problem the way it's used
|
| + FROM_HERE, base::Bind(&StreamTextureWrapperImpl::InitializeOnMainThread, |
| + weak_factory_.GetWeakPtr(), init_cb)); |
| +} |
| + |
| +void StreamTextureWrapperImpl::InitializeOnMainThread( |
| + const base::Closure& init_cb) { |
| + DCHECK(main_task_runner_->BelongsToCurrentThread()); |
| + DVLOG(2) << __FUNCTION__; |
| + |
| + stream_texture_proxy_.reset(factory_->CreateProxy()); |
| + |
| + stream_id_ = factory_->CreateStreamTexture(kGLTextureExternalOES, |
| + &texture_id_, &texture_mailbox_); |
| + ReallocateVideoFrame(natural_size_); |
| + |
| + stream_texture_proxy_->BindToLoop(stream_id_, client_, |
| + compositor_task_runner_); |
| + |
| + // TODO(tguilbert): Register the surface properly. See crbug.com/627658. |
| + |
| + // |init_cb| should be bound to the media thread, and continue its execution |
| + // there. |
| + init_cb.Run(); |
|
liberato (no reviews please)
2016/07/22 16:13:57
might want to add a comment in the .h that init_cb
tguilbert
2016/07/26 00:13:51
In the prototype, I had bound |init_cb| to the med
|
| +} |
| + |
| +void StreamTextureWrapperImpl::Destroy() { |
| + // Note: StreamTextureProxy will release its reference to |client_| |
| + // immediately (and stop calling back DidReceiveFrame()), and then destroy |
| + // itself on |compositor_task_runner_|. |
|
liberato (no reviews please)
2016/07/22 16:13:57
s/compositor/main/
tguilbert
2016/07/26 00:13:51
Done.
|
| + stream_texture_proxy_.reset(); |
|
watk
2016/07/23 03:13:39
I wish STP had clearer docs about its threading co
tguilbert
2016/07/26 00:13:51
The STP acquires a lock before releasing its refer
|
| + main_task_runner_->DeleteSoon(FROM_HERE, this); |
| +} |
| + |
| +} // namespace content |