Chromium Code Reviews| 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; | |
|
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
| |
| 16 | |
| 17 namespace { | |
| 18 // 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.
| |
| 19 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 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner) | |
| 36 : texture_id_(0), | |
| 37 stream_id_(0), | |
| 38 client_(nullptr), | |
| 39 factory_(factory), | |
| 40 main_task_runner_(main_task_runner), | |
| 41 weak_factory_(this) {} | |
| 42 | |
| 43 StreamTextureWrapperImpl::~StreamTextureWrapperImpl() { | |
| 44 DCHECK(main_task_runner_->BelongsToCurrentThread()); | |
| 45 | |
| 46 if (stream_id_) { | |
| 47 GLES2Interface* gl = factory_->ContextGL(); | |
| 48 gl->DeleteTextures(1, &texture_id_); | |
| 49 // Flush to ensure that the stream texture gets deleted in a timely fashion. | |
| 50 gl->ShallowFlushCHROMIUM(); | |
| 51 } | |
| 52 | |
| 53 SetCurrentFrameInternal(nullptr); | |
| 54 } | |
| 55 | |
| 56 scoped_refptr<media::VideoFrame> StreamTextureWrapperImpl::GetCurrentFrame() { | |
| 57 base::AutoLock auto_lock(current_frame_lock_); | |
| 58 return current_frame_; | |
| 59 } | |
| 60 | |
| 61 void StreamTextureWrapperImpl::ReallocateVideoFrame( | |
| 62 const gfx::Size& natural_size) { | |
| 63 DVLOG(2) << __FUNCTION__; | |
| 64 DCHECK(main_task_runner_->BelongsToCurrentThread()); | |
| 65 | |
| 66 GLES2Interface* gl = factory_->ContextGL(); | |
| 67 GLuint texture_target = kGLTextureExternalOES; | |
| 68 GLuint texture_id_ref = gl->CreateAndConsumeTextureCHROMIUM( | |
| 69 texture_target, texture_mailbox_.name); | |
| 70 const GLuint64 fence_sync = gl->InsertFenceSyncCHROMIUM(); | |
| 71 gl->Flush(); | |
| 72 | |
| 73 gpu::SyncToken texture_mailbox_sync_token; | |
| 74 gl->GenUnverifiedSyncTokenCHROMIUM(fence_sync, | |
| 75 texture_mailbox_sync_token.GetData()); | |
| 76 if (texture_mailbox_sync_token.namespace_id() == | |
| 77 gpu::CommandBufferNamespace::IN_PROCESS) { | |
| 78 // TODO(boliu): Remove this once Android WebView switches to IPC-based | |
| 79 // command buffer for video. | |
| 80 GLbyte* sync_tokens[] = {texture_mailbox_sync_token.GetData()}; | |
| 81 gl->VerifySyncTokensCHROMIUM(sync_tokens, arraysize(sync_tokens)); | |
| 82 } | |
| 83 | |
| 84 gpu::MailboxHolder holders[media::VideoFrame::kMaxPlanes] = { | |
| 85 gpu::MailboxHolder(texture_mailbox_, texture_mailbox_sync_token, | |
| 86 texture_target)}; | |
| 87 | |
| 88 scoped_refptr<media::VideoFrame> new_frame = | |
| 89 media::VideoFrame::WrapNativeTextures( | |
| 90 media::PIXEL_FORMAT_ARGB, holders, | |
| 91 media::BindToCurrentLoop( | |
| 92 base::Bind(&OnReleaseTexture, factory_, texture_id_ref)), | |
| 93 natural_size, gfx::Rect(natural_size), natural_size, | |
| 94 base::TimeDelta()); | |
| 95 | |
| 96 // TODO(tguilbert): Create and pipe the enable_texture_copy_ flag for Webview | |
| 97 // scenarios. See crbug.com/628066. | |
| 98 // | |
| 99 // if (new_frame.get()) { | |
| 100 // new_frame->metadata()->SetBoolean( | |
| 101 // media::VideoFrameMetadata::COPY_REQUIRED, enable_texture_copy_); | |
| 102 // } | |
|
watk
2016/07/23 03:13:39
Delete commented code? Put it in the bug maybe.
tguilbert
2016/07/26 00:13:51
Done.
| |
| 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 client_ = client; | |
| 142 | |
| 143 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
| |
| 144 FROM_HERE, base::Bind(&StreamTextureWrapperImpl::InitializeOnMainThread, | |
| 145 weak_factory_.GetWeakPtr(), init_cb)); | |
| 146 } | |
| 147 | |
| 148 void StreamTextureWrapperImpl::InitializeOnMainThread( | |
| 149 const base::Closure& init_cb) { | |
| 150 DCHECK(main_task_runner_->BelongsToCurrentThread()); | |
| 151 DVLOG(2) << __FUNCTION__; | |
| 152 | |
| 153 stream_texture_proxy_.reset(factory_->CreateProxy()); | |
| 154 | |
| 155 stream_id_ = factory_->CreateStreamTexture(kGLTextureExternalOES, | |
| 156 &texture_id_, &texture_mailbox_); | |
| 157 ReallocateVideoFrame(natural_size_); | |
| 158 | |
| 159 stream_texture_proxy_->BindToLoop(stream_id_, client_, | |
| 160 compositor_task_runner_); | |
| 161 | |
| 162 // TODO(tguilbert): Register the surface properly. See crbug.com/627658. | |
| 163 | |
| 164 // |init_cb| should be bound to the media thread, and continue its execution | |
| 165 // there. | |
| 166 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
| |
| 167 } | |
| 168 | |
| 169 void StreamTextureWrapperImpl::Destroy() { | |
| 170 // Note: StreamTextureProxy will release its reference to |client_| | |
| 171 // immediately (and stop calling back DidReceiveFrame()), and then destroy | |
| 172 // 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.
| |
| 173 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
| |
| 174 main_task_runner_->DeleteSoon(FROM_HERE, this); | |
| 175 } | |
| 176 | |
| 177 } // namespace content | |
| OLD | NEW |