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

Side by Side Diff: content/common/gpu/stream_texture_android.cc

Issue 1746983002: Make Android StreamTexture implement GLStreamTextureImage (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase Created 4 years, 9 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
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "content/common/gpu/stream_texture_android.h" 5 #include "content/common/gpu/stream_texture_android.h"
6 6
7 #include <string.h> 7 #include <string.h>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/strings/stringize_macros.h" 10 #include "base/strings/stringize_macros.h"
(...skipping 24 matching lines...) Expand all
35 GLES2Decoder* decoder = owner_stub->decoder(); 35 GLES2Decoder* decoder = owner_stub->decoder();
36 TextureManager* texture_manager = 36 TextureManager* texture_manager =
37 decoder->GetContextGroup()->texture_manager(); 37 decoder->GetContextGroup()->texture_manager();
38 TextureRef* texture = texture_manager->GetTexture(client_texture_id); 38 TextureRef* texture = texture_manager->GetTexture(client_texture_id);
39 39
40 if (texture && (!texture->texture()->target() || 40 if (texture && (!texture->texture()->target() ||
41 texture->texture()->target() == GL_TEXTURE_EXTERNAL_OES)) { 41 texture->texture()->target() == GL_TEXTURE_EXTERNAL_OES)) {
42 42
43 // TODO: Ideally a valid image id was returned to the client so that 43 // TODO: Ideally a valid image id was returned to the client so that
44 // it could then call glBindTexImage2D() for doing the following. 44 // it could then call glBindTexImage2D() for doing the following.
45 scoped_refptr<gl::GLImage> gl_image( 45 scoped_refptr<gpu::gles2::GLStreamTextureImage> gl_image(
46 new StreamTexture(owner_stub, stream_id, texture->service_id())); 46 new StreamTexture(owner_stub, stream_id, texture->service_id()));
47 gfx::Size size = gl_image->GetSize(); 47 gfx::Size size = gl_image->GetSize();
48 texture_manager->SetTarget(texture, GL_TEXTURE_EXTERNAL_OES); 48 texture_manager->SetTarget(texture, GL_TEXTURE_EXTERNAL_OES);
49 texture_manager->SetLevelInfo(texture, GL_TEXTURE_EXTERNAL_OES, 0, GL_RGBA, 49 texture_manager->SetLevelInfo(texture, GL_TEXTURE_EXTERNAL_OES, 0, GL_RGBA,
50 size.width(), size.height(), 1, 0, GL_RGBA, 50 size.width(), size.height(), 1, 0, GL_RGBA,
51 GL_UNSIGNED_BYTE, gfx::Rect(size)); 51 GL_UNSIGNED_BYTE, gfx::Rect(size));
52 texture_manager->SetLevelImage(texture, GL_TEXTURE_EXTERNAL_OES, 0, 52 texture_manager->SetLevelStreamTextureImage(
53 gl_image.get(), 53 texture, GL_TEXTURE_EXTERNAL_OES, 0, gl_image.get(),
54 gpu::gles2::Texture::UNBOUND); 54 gpu::gles2::Texture::UNBOUND);
55 return true; 55 return true;
56 } 56 }
57 57
58 return false; 58 return false;
59 } 59 }
60 60
61 StreamTexture::StreamTexture(GpuCommandBufferStub* owner_stub, 61 StreamTexture::StreamTexture(GpuCommandBufferStub* owner_stub,
62 int32_t route_id, 62 int32_t route_id,
63 uint32_t texture_id) 63 uint32_t texture_id)
64 : surface_texture_(gfx::SurfaceTexture::Create(texture_id)), 64 : surface_texture_(gfx::SurfaceTexture::Create(texture_id)),
65 size_(0, 0), 65 size_(0, 0),
66 has_valid_frame_(false),
67 has_pending_frame_(false), 66 has_pending_frame_(false),
68 owner_stub_(owner_stub), 67 owner_stub_(owner_stub),
69 route_id_(route_id), 68 route_id_(route_id),
70 has_listener_(false), 69 has_listener_(false),
71 texture_id_(texture_id), 70 texture_id_(texture_id),
72 framebuffer_(0), 71 framebuffer_(0),
73 vertex_shader_(0), 72 vertex_shader_(0),
74 fragment_shader_(0), 73 fragment_shader_(0),
75 program_(0), 74 program_(0),
76 vertex_buffer_(0), 75 vertex_buffer_(0),
77 u_xform_location_(-1), 76 u_xform_location_(-1),
78 weak_factory_(this) { 77 weak_factory_(this) {
79 owner_stub->AddDestructionObserver(this); 78 owner_stub->AddDestructionObserver(this);
80 memset(current_matrix_, 0, sizeof(current_matrix_)); 79 memset(current_matrix_, 0, sizeof(current_matrix_));
81 owner_stub->channel()->AddRoute(route_id, owner_stub->stream_id(), this); 80 owner_stub->channel()->AddRoute(route_id, owner_stub->stream_id(), this);
82 surface_texture_->SetFrameAvailableCallback(base::Bind( 81 surface_texture_->SetFrameAvailableCallback(base::Bind(
83 &StreamTexture::OnFrameAvailable, weak_factory_.GetWeakPtr())); 82 &StreamTexture::OnFrameAvailable, weak_factory_.GetWeakPtr()));
84 } 83 }
85 84
86 StreamTexture::~StreamTexture() { 85 StreamTexture::~StreamTexture() {
87 if (owner_stub_) { 86 if (owner_stub_) {
88 owner_stub_->RemoveDestructionObserver(this); 87 owner_stub_->RemoveDestructionObserver(this);
89 owner_stub_->channel()->RemoveRoute(route_id_); 88 owner_stub_->channel()->RemoveRoute(route_id_);
90 } 89 }
91 } 90 }
92 91
92 // gpu::gles2::GLStreamTextureMatrix implementation
93 void StreamTexture::GetTextureMatrix(float xform[16]) {
94 surface_texture_->GetTransformMatrix(xform);
liberato (no reviews please) 2016/03/17 14:38:01 i think that you need to call UpdateTexImage here.
Tobias Sargeant 2016/03/17 15:17:58 Done.
95 }
96
93 void StreamTexture::OnWillDestroyStub() { 97 void StreamTexture::OnWillDestroyStub() {
94 owner_stub_->RemoveDestructionObserver(this); 98 owner_stub_->RemoveDestructionObserver(this);
95 owner_stub_->channel()->RemoveRoute(route_id_); 99 owner_stub_->channel()->RemoveRoute(route_id_);
96 100
97 if (framebuffer_) { 101 if (framebuffer_) {
98 scoped_ptr<ui::ScopedMakeCurrent> scoped_make_current(MakeStubCurrent()); 102 scoped_ptr<ui::ScopedMakeCurrent> scoped_make_current(MakeStubCurrent());
99 103
100 glDeleteProgram(program_); 104 glDeleteProgram(program_);
101 glDeleteShader(vertex_shader_); 105 glDeleteShader(vertex_shader_);
102 glDeleteShader(fragment_shader_); 106 glDeleteShader(fragment_shader_);
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
135 void StreamTexture::UpdateTexImage() { 139 void StreamTexture::UpdateTexImage() {
136 DCHECK(surface_texture_.get()); 140 DCHECK(surface_texture_.get());
137 DCHECK(owner_stub_); 141 DCHECK(owner_stub_);
138 142
139 if (!has_pending_frame_) return; 143 if (!has_pending_frame_) return;
140 144
141 scoped_ptr<ui::ScopedMakeCurrent> scoped_make_current(MakeStubCurrent()); 145 scoped_ptr<ui::ScopedMakeCurrent> scoped_make_current(MakeStubCurrent());
142 146
143 surface_texture_->UpdateTexImage(); 147 surface_texture_->UpdateTexImage();
144 148
145 has_valid_frame_ = true;
146 has_pending_frame_ = false; 149 has_pending_frame_ = false;
147 150
148 float mtx[16];
149 surface_texture_->GetTransformMatrix(mtx);
150
151 if (memcmp(current_matrix_, mtx, sizeof(mtx)) != 0) {
152 memcpy(current_matrix_, mtx, sizeof(mtx));
153
154 if (has_listener_) {
155 GpuStreamTextureMsg_MatrixChanged_Params params;
156 memcpy(&params.m00, mtx, sizeof(mtx));
157 owner_stub_->channel()->Send(
158 new GpuStreamTextureMsg_MatrixChanged(route_id_, params));
159 }
160 }
161
162 if (scoped_make_current.get()) { 151 if (scoped_make_current.get()) {
163 // UpdateTexImage() implies glBindTexture(). 152 // UpdateTexImage() implies glBindTexture().
164 // The cmd decoder takes care of restoring the binding for this GLImage as 153 // The cmd decoder takes care of restoring the binding for this GLImage as
165 // far as the current context is concerned, but if we temporarily change 154 // far as the current context is concerned, but if we temporarily change
166 // it, we have to keep the state intact in *that* context also. 155 // it, we have to keep the state intact in *that* context also.
167 const gpu::gles2::ContextState* state = 156 const gpu::gles2::ContextState* state =
168 owner_stub_->decoder()->GetContextState(); 157 owner_stub_->decoder()->GetContextState();
169 const gpu::gles2::TextureUnit& active_unit = 158 const gpu::gles2::TextureUnit& active_unit =
170 state->texture_units[state->active_texture_unit]; 159 state->texture_units[state->active_texture_unit];
171 glBindTexture(GL_TEXTURE_EXTERNAL_OES, 160 glBindTexture(GL_TEXTURE_EXTERNAL_OES,
172 active_unit.bound_texture_external_oes.get() 161 active_unit.bound_texture_external_oes.get()
173 ? active_unit.bound_texture_external_oes->service_id() 162 ? active_unit.bound_texture_external_oes->service_id()
174 : 0); 163 : 0);
175 } 164 }
176 } 165 }
177 166
178 bool StreamTexture::CopyTexImage(unsigned target) { 167 bool StreamTexture::CopyTexImage(unsigned target) {
179 if (target == GL_TEXTURE_2D) {
180 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, size_.width(), size_.height(), 0,
181 GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
182 return CopyTexSubImage(GL_TEXTURE_2D, gfx::Point(), gfx::Rect(size_));
183 }
184
185 if (target != GL_TEXTURE_EXTERNAL_OES) 168 if (target != GL_TEXTURE_EXTERNAL_OES)
186 return false; 169 return false;
187 170
188 if (!owner_stub_ || !surface_texture_.get()) 171 if (!owner_stub_ || !surface_texture_.get())
189 return true; 172 return true;
190 173
191 GLint texture_id; 174 GLint texture_id;
192 glGetIntegerv(GL_TEXTURE_BINDING_EXTERNAL_OES, &texture_id); 175 glGetIntegerv(GL_TEXTURE_BINDING_EXTERNAL_OES, &texture_id);
193 DCHECK(texture_id); 176 DCHECK(texture_id);
194 177
195 // The following code only works if we're being asked to copy into 178 // The following code only works if we're being asked to copy into
196 // |texture_id_|. Copying into a different texture is not supported. 179 // |texture_id_|. Copying into a different texture is not supported.
197 if (static_cast<unsigned>(texture_id) != texture_id_) 180 if (static_cast<unsigned>(texture_id) != texture_id_)
198 return false; 181 return false;
199 182
200 UpdateTexImage(); 183 UpdateTexImage();
201 184
202 TextureManager* texture_manager = 185 TextureManager* texture_manager =
203 owner_stub_->decoder()->GetContextGroup()->texture_manager(); 186 owner_stub_->decoder()->GetContextGroup()->texture_manager();
204 gpu::gles2::Texture* texture = 187 gpu::gles2::Texture* texture =
205 texture_manager->GetTextureForServiceId(texture_id_); 188 texture_manager->GetTextureForServiceId(texture_id_);
206 if (texture) { 189 if (texture) {
207 // By setting image state to UNBOUND instead of COPIED we ensure that 190 // By setting image state to UNBOUND instead of COPIED we ensure that
208 // CopyTexImage() is called each time the surface texture is used for 191 // CopyTexImage() is called each time the surface texture is used for
209 // drawing. 192 // drawing.
210 texture->SetLevelImage(GL_TEXTURE_EXTERNAL_OES, 0, this, 193 texture->SetLevelStreamTextureImage(GL_TEXTURE_EXTERNAL_OES, 0, this,
211 gpu::gles2::Texture::UNBOUND); 194 gpu::gles2::Texture::UNBOUND);
212 } 195 }
213 196
214 return true; 197 return true;
215 } 198 }
216 199
217 void StreamTexture::OnFrameAvailable() { 200 void StreamTexture::OnFrameAvailable() {
218 has_pending_frame_ = true; 201 has_pending_frame_ = true;
219 if (has_listener_ && owner_stub_) { 202 if (has_listener_ && owner_stub_) {
220 owner_stub_->channel()->Send( 203 owner_stub_->channel()->Send(
221 new GpuStreamTextureMsg_FrameAvailable(route_id_)); 204 new GpuStreamTextureMsg_FrameAvailable(route_id_));
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
263 return false; 246 return false;
264 } 247 }
265 248
266 void StreamTexture::ReleaseTexImage(unsigned target) { 249 void StreamTexture::ReleaseTexImage(unsigned target) {
267 NOTREACHED(); 250 NOTREACHED();
268 } 251 }
269 252
270 bool StreamTexture::CopyTexSubImage(unsigned target, 253 bool StreamTexture::CopyTexSubImage(unsigned target,
271 const gfx::Point& offset, 254 const gfx::Point& offset,
272 const gfx::Rect& rect) { 255 const gfx::Rect& rect) {
273 if (target != GL_TEXTURE_2D) 256 return false;
274 return false;
275
276 if (!owner_stub_ || !surface_texture_.get())
277 return true;
278
279 if (!offset.IsOrigin()) {
280 LOG(ERROR) << "Non-origin offset is not supported";
281 return false;
282 }
283
284 if (rect != gfx::Rect(size_)) {
285 LOG(ERROR) << "Sub-rectangle is not supported";
286 return false;
287 }
288
289 GLint target_texture = 0;
290 glGetIntegerv(GL_TEXTURE_BINDING_2D, &target_texture);
291 DCHECK(target_texture);
292
293 UpdateTexImage();
294
295 if (!framebuffer_) {
296 glGenFramebuffersEXT(1, &framebuffer_);
297
298 // This vertex shader introduces a y flip before applying the stream
299 // texture matrix. This is required because the stream texture matrix
300 // Android provides is intended to be used in a y-up coordinate system,
301 // whereas Chromium expects y-down.
302
303 // clang-format off
304 const char kVertexShader[] = STRINGIZE(
305 attribute vec2 a_position;
306 varying vec2 v_texCoord;
307 uniform mat4 u_xform;
308 void main() {
309 gl_Position = vec4(a_position.x, a_position.y, 0.0, 1.0);
310 vec2 uv_untransformed = a_position * vec2(0.5, -0.5) + vec2(0.5, 0.5);
311 v_texCoord = (u_xform * vec4(uv_untransformed, 0.0, 1.0)).xy;
312 }
313 );
314 const char kFragmentShader[] =
315 "#extension GL_OES_EGL_image_external : require\n" STRINGIZE(
316 precision mediump float;
317 uniform samplerExternalOES a_texture;
318 varying vec2 v_texCoord;
319 void main() {
320 gl_FragColor = texture2D(a_texture, v_texCoord);
321 }
322 );
323 // clang-format on
324
325 vertex_buffer_ = gfx::GLHelper::SetupQuadVertexBuffer();
326 vertex_shader_ = gfx::GLHelper::LoadShader(GL_VERTEX_SHADER, kVertexShader);
327 fragment_shader_ =
328 gfx::GLHelper::LoadShader(GL_FRAGMENT_SHADER, kFragmentShader);
329 program_ = gfx::GLHelper::SetupProgram(vertex_shader_, fragment_shader_);
330 gfx::ScopedUseProgram use_program(program_);
331 int sampler_location = glGetUniformLocation(program_, "a_texture");
332 DCHECK_NE(-1, sampler_location);
333 glUniform1i(sampler_location, 0);
334 u_xform_location_ = glGetUniformLocation(program_, "u_xform");
335 DCHECK_NE(-1, u_xform_location_);
336 }
337
338 gfx::ScopedActiveTexture active_texture(GL_TEXTURE0);
339 // UpdateTexImage() call below will bind the surface texture to
340 // TEXTURE_EXTERNAL_OES. This scoped texture binder will restore the current
341 // binding before this function returns.
342 gfx::ScopedTextureBinder texture_binder(GL_TEXTURE_EXTERNAL_OES, texture_id_);
343
344 {
345 gfx::ScopedFrameBufferBinder framebuffer_binder(framebuffer_);
346 gfx::ScopedViewport viewport(0, 0, size_.width(), size_.height());
347 glFramebufferTexture2DEXT(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
348 GL_TEXTURE_2D, target_texture, 0);
349 DCHECK_EQ(static_cast<GLenum>(GL_FRAMEBUFFER_COMPLETE),
350 glCheckFramebufferStatusEXT(GL_FRAMEBUFFER));
351 gfx::ScopedUseProgram use_program(program_);
352
353 glUniformMatrix4fv(u_xform_location_, 1, false, current_matrix_);
354 gfx::GLHelper::DrawQuad(vertex_buffer_);
355
356 // Detach the output texture from the fbo.
357 glFramebufferTexture2DEXT(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
358 GL_TEXTURE_2D, 0, 0);
359 }
360 return true;
361 } 257 }
362 258
363 bool StreamTexture::ScheduleOverlayPlane(gfx::AcceleratedWidget widget, 259 bool StreamTexture::ScheduleOverlayPlane(gfx::AcceleratedWidget widget,
364 int z_order, 260 int z_order,
365 gfx::OverlayTransform transform, 261 gfx::OverlayTransform transform,
366 const gfx::Rect& bounds_rect, 262 const gfx::Rect& bounds_rect,
367 const gfx::RectF& crop_rect) { 263 const gfx::RectF& crop_rect) {
368 NOTREACHED(); 264 NOTREACHED();
369 return false; 265 return false;
370 } 266 }
371 267
372 void StreamTexture::OnMemoryDump(base::trace_event::ProcessMemoryDump* pmd, 268 void StreamTexture::OnMemoryDump(base::trace_event::ProcessMemoryDump* pmd,
373 uint64_t process_tracing_id, 269 uint64_t process_tracing_id,
374 const std::string& dump_name) { 270 const std::string& dump_name) {
375 // TODO(ericrk): Add OnMemoryDump for GLImages. crbug.com/514914 271 // TODO(ericrk): Add OnMemoryDump for GLImages. crbug.com/514914
376 } 272 }
377 273
378 } // namespace content 274 } // namespace content
OLDNEW
« no previous file with comments | « content/common/gpu/stream_texture_android.h ('k') | content/renderer/gpu/stream_texture_host_android.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698