Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(299)

Side by Side Diff: content/renderer/media/android/stream_texture_wrapper_impl.cc

Issue 2136103010: Add StreamTextureWrapper/StreamTextureWrapperImpl (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressing comments Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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(
20 const scoped_refptr<content::StreamTextureFactory>& factories,
DaleCurtis 2016/07/26 20:13:40 no const& for scoped_refptr ?
tguilbert 2016/07/26 22:20:20 Missed this one. Done.
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 factory_(factory),
39 main_task_runner_(main_task_runner),
40 weak_factory_(this) {}
41
42 StreamTextureWrapperImpl::~StreamTextureWrapperImpl() {
43 DCHECK(main_task_runner_->BelongsToCurrentThread());
44
45 if (stream_id_) {
46 GLES2Interface* gl = factory_->ContextGL();
47 gl->DeleteTextures(1, &texture_id_);
48 // Flush to ensure that the stream texture gets deleted in a timely fashion.
49 gl->ShallowFlushCHROMIUM();
50 }
51
52 SetCurrentFrameInternal(nullptr);
53 }
54
55 scoped_refptr<media::VideoFrame> StreamTextureWrapperImpl::GetCurrentFrame() {
56 base::AutoLock auto_lock(current_frame_lock_);
DaleCurtis 2016/07/26 20:13:40 What thread is this called on if not the main thre
tguilbert 2016/07/26 22:20:20 On the compositor thread only (|compositor_task_ru
57 return current_frame_;
58 }
59
60 void StreamTextureWrapperImpl::ReallocateVideoFrame(
61 const gfx::Size& natural_size) {
62 DVLOG(2) << __FUNCTION__;
63 DCHECK(main_task_runner_->BelongsToCurrentThread());
64
65 GLES2Interface* gl = factory_->ContextGL();
66 GLuint texture_target = kGLTextureExternalOES;
67 GLuint texture_id_ref = gl->CreateAndConsumeTextureCHROMIUM(
68 texture_target, texture_mailbox_.name);
69 const GLuint64 fence_sync = gl->InsertFenceSyncCHROMIUM();
70 gl->Flush();
71
72 gpu::SyncToken texture_mailbox_sync_token;
73 gl->GenUnverifiedSyncTokenCHROMIUM(fence_sync,
74 texture_mailbox_sync_token.GetData());
75 if (texture_mailbox_sync_token.namespace_id() ==
76 gpu::CommandBufferNamespace::IN_PROCESS) {
77 // TODO(boliu): Remove this once Android WebView switches to IPC-based
78 // command buffer for video.
79 GLbyte* sync_tokens[] = {texture_mailbox_sync_token.GetData()};
80 gl->VerifySyncTokensCHROMIUM(sync_tokens, arraysize(sync_tokens));
81 }
82
83 gpu::MailboxHolder holders[media::VideoFrame::kMaxPlanes] = {
84 gpu::MailboxHolder(texture_mailbox_, texture_mailbox_sync_token,
85 texture_target)};
86
87 scoped_refptr<media::VideoFrame> new_frame =
88 media::VideoFrame::WrapNativeTextures(
89 media::PIXEL_FORMAT_ARGB, holders,
90 media::BindToCurrentLoop(
91 base::Bind(&OnReleaseTexture, factory_, texture_id_ref)),
92 natural_size, gfx::Rect(natural_size), natural_size,
93 base::TimeDelta());
94
95 // TODO(tguilbert): Create and pipe the enable_texture_copy_ flag for Webview
96 // scenarios. See crbug.com/628066.
97
98 SetCurrentFrameInternal(new_frame);
99 }
100
101 void StreamTextureWrapperImpl::SetCurrentFrameInternal(
102 const scoped_refptr<media::VideoFrame>& video_frame) {
103 base::AutoLock auto_lock(current_frame_lock_);
104 current_frame_ = video_frame;
105 }
106
107 void StreamTextureWrapperImpl::UpdateTextureSize(const gfx::Size& new_size) {
108 DVLOG(2) << __FUNCTION__;
109
110 if (!main_task_runner_->BelongsToCurrentThread()) {
111 main_task_runner_->PostTask(
112 FROM_HERE, base::Bind(&StreamTextureWrapperImpl::UpdateTextureSize,
113 weak_factory_.GetWeakPtr(), new_size));
114 return;
115 }
116
117 if (natural_size_ == new_size)
118 return;
119
120 natural_size_ = new_size;
121
122 ReallocateVideoFrame(new_size);
123 factory_->SetStreamTextureSize(stream_id_, new_size);
124 }
125
126 void StreamTextureWrapperImpl::Initialize(
127 cc::VideoFrameProvider::Client* client,
128 const gfx::Size& natural_size,
129 scoped_refptr<base::SingleThreadTaskRunner> compositor_task_runner,
130 const base::Closure& init_cb) {
131 DVLOG(2) << __FUNCTION__;
132
133 compositor_task_runner_ = compositor_task_runner;
134 natural_size_ = natural_size;
135
136 if (main_task_runner_->BelongsToCurrentThread()) {
137 InitializeOnMainThread(client, init_cb);
watk 2016/07/26 20:02:32 It's worth noting in the header that init_cb could
DaleCurtis 2016/07/26 20:13:40 +1, keep it consistent to reduce oddities.
tguilbert 2016/07/26 22:20:20 Done.
138 } else {
139 main_task_runner_->PostTask(
140 FROM_HERE, base::Bind(&StreamTextureWrapperImpl::InitializeOnMainThread,
141 weak_factory_.GetWeakPtr(), client,
142 media::BindToCurrentLoop(init_cb)));
143 }
144 }
145
146 void StreamTextureWrapperImpl::InitializeOnMainThread(
147 cc::VideoFrameProvider::Client* client,
148 const base::Closure& init_cb) {
149 DCHECK(main_task_runner_->BelongsToCurrentThread());
150 DVLOG(2) << __FUNCTION__;
151
152 stream_texture_proxy_.reset(factory_->CreateProxy());
153
154 stream_id_ = factory_->CreateStreamTexture(kGLTextureExternalOES,
155 &texture_id_, &texture_mailbox_);
156 ReallocateVideoFrame(natural_size_);
157
158 stream_texture_proxy_->BindToLoop(stream_id_, client,
159 compositor_task_runner_);
160
161 // TODO(tguilbert): Register the surface properly. See crbug.com/627658.
162
163 // |init_cb| is bound to the thread that originally called Initialize().
164 init_cb.Run();
165 }
166
167 void StreamTextureWrapperImpl::Destroy() {
168 // Note: StreamTextureProxy will release its reference to |client|
169 // immediately (and stop calling back DidReceiveFrame()), and then destroy
170 // itself on |main_task_runner_|.
171 stream_texture_proxy_.reset();
172 main_task_runner_->DeleteSoon(FROM_HERE, this);
173 }
174
175 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698