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 = 0x8D65; |
| 16 |
| 17 namespace { |
| 18 // File-static function is to allow it to run even after this class is deleted. |
| 19 static void OnReleaseTexture( |
| 20 const scoped_refptr<content::StreamTextureFactory>& factories, |
| 21 uint32_t texture_id, |
| 22 const gpu::SyncToken& sync_token) { |
| 23 GLES2Interface* gl = factories->ContextGL(); |
| 24 gl->WaitSyncTokenCHROMIUM(sync_token.GetConstData()); |
| 25 gl->DeleteTextures(1, &texture_id); |
| 26 // Flush to ensure that the stream texture gets deleted in a timely fashion. |
| 27 gl->ShallowFlushCHROMIUM(); |
| 28 } |
| 29 } |
| 30 |
| 31 namespace content { |
| 32 |
| 33 StreamTextureWrapperImpl::StreamTextureWrapperImpl( |
| 34 scoped_refptr<StreamTextureFactory> factory, |
| 35 const scoped_refptr<base::SingleThreadTaskRunner>& main_task_runner) |
| 36 : texture_id_(0), |
| 37 stream_id_(0), |
| 38 client_(NULL), |
| 39 factory_(factory), |
| 40 main_task_runner_(main_task_runner) {} |
| 41 |
| 42 StreamTextureWrapperImpl::~StreamTextureWrapperImpl() { |
| 43 // TODO(tguilbert): Implement deleter function to run the following code on |
| 44 // |main_task_runner_| |
| 45 |
| 46 /* |
| 47 if (stream_id_) { |
| 48 GLES2Interface* gl = factory_->ContextGL(); |
| 49 gl->DeleteTextures(1, &texture_id_); |
| 50 // Flush to ensure that the stream texture gets deleted in a timely fashion. |
| 51 gl->ShallowFlushCHROMIUM(); |
| 52 texture_id_ = 0; |
| 53 texture_mailbox_ = gpu::Mailbox(); |
| 54 stream_id_ = 0; |
| 55 } |
| 56 |
| 57 base::AutoLock auto_lock(current_frame_lock_); |
| 58 current_frame_ = NULL; |
| 59 */ |
| 60 } |
| 61 |
| 62 scoped_refptr<media::VideoFrame> StreamTextureWrapperImpl::GetCurrentFrame() { |
| 63 base::AutoLock auto_lock(current_frame_lock_); |
| 64 return current_frame_; |
| 65 } |
| 66 |
| 67 void StreamTextureWrapperImpl::ReallocateVideoFrame( |
| 68 const gfx::Size& natural_size) { |
| 69 DVLOG(2) << __FUNCTION__; |
| 70 DCHECK(main_task_runner_->BelongsToCurrentThread()); |
| 71 |
| 72 GLES2Interface* gl = factory_->ContextGL(); |
| 73 GLuint texture_target = kGLTextureExternalOES; |
| 74 GLuint texture_id_ref = gl->CreateAndConsumeTextureCHROMIUM( |
| 75 texture_target, texture_mailbox_.name); |
| 76 const GLuint64 fence_sync = gl->InsertFenceSyncCHROMIUM(); |
| 77 gl->Flush(); |
| 78 |
| 79 gpu::SyncToken texture_mailbox_sync_token; |
| 80 gl->GenUnverifiedSyncTokenCHROMIUM(fence_sync, |
| 81 texture_mailbox_sync_token.GetData()); |
| 82 if (texture_mailbox_sync_token.namespace_id() == |
| 83 gpu::CommandBufferNamespace::IN_PROCESS) { |
| 84 // TODO(boliu): Remove this once Android WebView switches to IPC-based |
| 85 // command buffer for video. |
| 86 GLbyte* sync_tokens[] = {texture_mailbox_sync_token.GetData()}; |
| 87 gl->VerifySyncTokensCHROMIUM(sync_tokens, arraysize(sync_tokens)); |
| 88 } |
| 89 |
| 90 gpu::MailboxHolder holders[media::VideoFrame::kMaxPlanes] = { |
| 91 gpu::MailboxHolder(texture_mailbox_, texture_mailbox_sync_token, |
| 92 texture_target)}; |
| 93 |
| 94 scoped_refptr<media::VideoFrame> new_frame = |
| 95 media::VideoFrame::WrapNativeTextures( |
| 96 media::PIXEL_FORMAT_ARGB, holders, |
| 97 media::BindToCurrentLoop( |
| 98 base::Bind(&OnReleaseTexture, factory_, texture_id_ref)), |
| 99 natural_size, gfx::Rect(natural_size), natural_size, |
| 100 base::TimeDelta()); |
| 101 |
| 102 // TODO(tguilbert): Create and pipe the enable_texture_copy_ flag for Webview |
| 103 // scenarios. See crbug.com/628066. |
| 104 // |
| 105 // if (new_frame.get()) { |
| 106 // new_frame->metadata()->SetBoolean( |
| 107 // media::VideoFrameMetadata::COPY_REQUIRED, enable_texture_copy_); |
| 108 // } |
| 109 |
| 110 SetCurrentFrameInternal(new_frame); |
| 111 } |
| 112 |
| 113 void StreamTextureWrapperImpl::SetCurrentFrameInternal( |
| 114 scoped_refptr<media::VideoFrame>& video_frame) { |
| 115 base::AutoLock auto_lock(current_frame_lock_); |
| 116 current_frame_ = video_frame; |
| 117 } |
| 118 |
| 119 void StreamTextureWrapperImpl::UpdateTextureSize(const gfx::Size& new_size) { |
| 120 DVLOG(2) << __FUNCTION__; |
| 121 |
| 122 if (natural_size_ == new_size) |
| 123 return; |
| 124 |
| 125 if (!main_task_runner_->BelongsToCurrentThread()) { |
| 126 // TODO(tguilbert): Change the base::Unretained to the proper solution. |
| 127 main_task_runner_->PostTask( |
| 128 FROM_HERE, base::Bind(&StreamTextureWrapperImpl::UpdateTextureSize, |
| 129 base::Unretained(this), new_size)); |
| 130 return; |
| 131 } |
| 132 |
| 133 natural_size_ = new_size; |
| 134 |
| 135 ReallocateVideoFrame(new_size); |
| 136 factory_->SetStreamTextureSize(stream_id_, new_size); |
| 137 } |
| 138 |
| 139 void StreamTextureWrapperImpl::Initialize( |
| 140 cc::VideoFrameProvider::Client* client, |
| 141 const gfx::Size& natural_size, |
| 142 const scoped_refptr<base::SingleThreadTaskRunner>& media_task_runner, |
| 143 const base::Closure& init_cb) { |
| 144 DVLOG(2) << __FUNCTION__; |
| 145 |
| 146 media_task_runner_ = media_task_runner; |
| 147 natural_size_ = natural_size; |
| 148 client_ = client; |
| 149 |
| 150 // TODO(tguilbert): Change the base::Unretained to the proper solution. |
| 151 main_task_runner_->PostTask( |
| 152 FROM_HERE, base::Bind(&StreamTextureWrapperImpl::InitializeInternal, |
| 153 base::Unretained(this), init_cb)); |
| 154 } |
| 155 |
| 156 void StreamTextureWrapperImpl::InitializeInternal( |
| 157 const base::Closure& init_cb) { |
| 158 DCHECK(main_task_runner_->BelongsToCurrentThread()); |
| 159 DVLOG(2) << __FUNCTION__; |
| 160 |
| 161 stream_texture_proxy_.reset(factory_->CreateProxy()); |
| 162 |
| 163 stream_id_ = factory_->CreateStreamTexture(kGLTextureExternalOES, |
| 164 &texture_id_, &texture_mailbox_); |
| 165 ReallocateVideoFrame(natural_size_); |
| 166 |
| 167 stream_texture_proxy_->BindToLoop(stream_id_, client_, media_task_runner_); |
| 168 |
| 169 // TODO(tguilbert): Register the surface properly. See crbug.com/627658. |
| 170 factory_->EstablishPeer(stream_id_, 1234, 5678); |
| 171 |
| 172 media_task_runner_->PostTask(FROM_HERE, init_cb); |
| 173 } |
| 174 |
| 175 } // namespace content |
OLD | NEW |