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..1e1d5e4749497b3dc9c0142db2fcd47dfe807a88 |
--- /dev/null |
+++ b/content/renderer/media/android/stream_texture_wrapper_impl.cc |
@@ -0,0 +1,175 @@ |
+// 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 = 0x8D65; |
+ |
+namespace { |
+// File-static function is to allow it to run even after this class is deleted. |
+static 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, |
+ const scoped_refptr<base::SingleThreadTaskRunner>& main_task_runner) |
+ : texture_id_(0), |
+ stream_id_(0), |
+ client_(NULL), |
+ factory_(factory), |
+ main_task_runner_(main_task_runner) {} |
+ |
+StreamTextureWrapperImpl::~StreamTextureWrapperImpl() { |
+ // TODO(tguilbert): Implement deleter function to run the following code on |
+ // |main_task_runner_| |
+ |
+ /* |
+ 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(); |
+ texture_id_ = 0; |
+ texture_mailbox_ = gpu::Mailbox(); |
+ stream_id_ = 0; |
+ } |
+ |
+ base::AutoLock auto_lock(current_frame_lock_); |
+ current_frame_ = NULL; |
+ */ |
+} |
+ |
+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_); |
+ // } |
+ |
+ SetCurrentFrameInternal(new_frame); |
+} |
+ |
+void StreamTextureWrapperImpl::SetCurrentFrameInternal( |
+ 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 (natural_size_ == new_size) |
+ return; |
+ |
+ if (!main_task_runner_->BelongsToCurrentThread()) { |
+ // TODO(tguilbert): Change the base::Unretained to the proper solution. |
+ main_task_runner_->PostTask( |
+ FROM_HERE, base::Bind(&StreamTextureWrapperImpl::UpdateTextureSize, |
+ base::Unretained(this), 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, |
+ const scoped_refptr<base::SingleThreadTaskRunner>& media_task_runner, |
+ const base::Closure& init_cb) { |
+ DVLOG(2) << __FUNCTION__; |
+ |
+ media_task_runner_ = media_task_runner; |
+ natural_size_ = natural_size; |
+ client_ = client; |
+ |
+ // TODO(tguilbert): Change the base::Unretained to the proper solution. |
+ main_task_runner_->PostTask( |
+ FROM_HERE, base::Bind(&StreamTextureWrapperImpl::InitializeInternal, |
+ base::Unretained(this), init_cb)); |
+} |
+ |
+void StreamTextureWrapperImpl::InitializeInternal( |
+ 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_, media_task_runner_); |
+ |
+ // TODO(tguilbert): Register the surface properly. See crbug.com/627658. |
+ factory_->EstablishPeer(stream_id_, 1234, 5678); |
+ |
+ media_task_runner_->PostTask(FROM_HERE, init_cb); |
+} |
+ |
+} // namespace content |