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

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

Issue 1536713002: Apply the Surface Texture transformation matrix during texture copy. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Add comment explaining the need for the extra y flip. Created 4 years, 11 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
« no previous file with comments | « content/common/gpu/stream_texture_android.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 "content/common/android/surface_texture_peer.h" 11 #include "content/common/android/surface_texture_peer.h"
11 #include "content/common/gpu/gpu_channel.h" 12 #include "content/common/gpu/gpu_channel.h"
12 #include "content/common/gpu/gpu_messages.h" 13 #include "content/common/gpu/gpu_messages.h"
13 #include "gpu/command_buffer/service/context_group.h" 14 #include "gpu/command_buffer/service/context_group.h"
14 #include "gpu/command_buffer/service/context_state.h" 15 #include "gpu/command_buffer/service/context_state.h"
15 #include "gpu/command_buffer/service/gles2_cmd_decoder.h" 16 #include "gpu/command_buffer/service/gles2_cmd_decoder.h"
16 #include "gpu/command_buffer/service/texture_manager.h" 17 #include "gpu/command_buffer/service/texture_manager.h"
17 #include "ui/gfx/geometry/size.h" 18 #include "ui/gfx/geometry/size.h"
18 #include "ui/gl/gl_context.h" 19 #include "ui/gl/gl_context.h"
20 #include "ui/gl/gl_helper.h"
21 #include "ui/gl/scoped_binders.h"
19 #include "ui/gl/scoped_make_current.h" 22 #include "ui/gl/scoped_make_current.h"
20 23
21 namespace content { 24 namespace content {
22 25
23 using gpu::gles2::ContextGroup; 26 using gpu::gles2::ContextGroup;
24 using gpu::gles2::GLES2Decoder; 27 using gpu::gles2::GLES2Decoder;
25 using gpu::gles2::TextureManager; 28 using gpu::gles2::TextureManager;
26 using gpu::gles2::TextureRef; 29 using gpu::gles2::TextureRef;
27 30
28 // static 31 // static
(...skipping 30 matching lines...) Expand all
59 int32_t route_id, 62 int32_t route_id,
60 uint32_t texture_id) 63 uint32_t texture_id)
61 : surface_texture_(gfx::SurfaceTexture::Create(texture_id)), 64 : surface_texture_(gfx::SurfaceTexture::Create(texture_id)),
62 size_(0, 0), 65 size_(0, 0),
63 has_valid_frame_(false), 66 has_valid_frame_(false),
64 has_pending_frame_(false), 67 has_pending_frame_(false),
65 owner_stub_(owner_stub), 68 owner_stub_(owner_stub),
66 route_id_(route_id), 69 route_id_(route_id),
67 has_listener_(false), 70 has_listener_(false),
68 texture_id_(texture_id), 71 texture_id_(texture_id),
72 framebuffer_(0),
73 vertex_shader_(0),
74 fragment_shader_(0),
75 program_(0),
76 vertex_buffer_(0),
77 u_xform_location_(-1),
69 weak_factory_(this) { 78 weak_factory_(this) {
70 owner_stub->AddDestructionObserver(this); 79 owner_stub->AddDestructionObserver(this);
71 memset(current_matrix_, 0, sizeof(current_matrix_)); 80 memset(current_matrix_, 0, sizeof(current_matrix_));
72 owner_stub->channel()->AddRoute(route_id, this); 81 owner_stub->channel()->AddRoute(route_id, this);
73 surface_texture_->SetFrameAvailableCallback(base::Bind( 82 surface_texture_->SetFrameAvailableCallback(base::Bind(
74 &StreamTexture::OnFrameAvailable, weak_factory_.GetWeakPtr())); 83 &StreamTexture::OnFrameAvailable, weak_factory_.GetWeakPtr()));
75 } 84 }
76 85
77 StreamTexture::~StreamTexture() { 86 StreamTexture::~StreamTexture() {
78 if (owner_stub_) { 87 if (owner_stub_) {
79 owner_stub_->RemoveDestructionObserver(this); 88 owner_stub_->RemoveDestructionObserver(this);
80 owner_stub_->channel()->RemoveRoute(route_id_); 89 owner_stub_->channel()->RemoveRoute(route_id_);
81 } 90 }
82 } 91 }
83 92
84 void StreamTexture::OnWillDestroyStub() { 93 void StreamTexture::OnWillDestroyStub() {
85 owner_stub_->RemoveDestructionObserver(this); 94 owner_stub_->RemoveDestructionObserver(this);
86 owner_stub_->channel()->RemoveRoute(route_id_); 95 owner_stub_->channel()->RemoveRoute(route_id_);
96
97 if (framebuffer_) {
98 scoped_ptr<ui::ScopedMakeCurrent> scoped_make_current(MakeStubCurrent());
99
100 glDeleteProgram(program_);
101 glDeleteShader(vertex_shader_);
102 glDeleteShader(fragment_shader_);
103 glDeleteBuffersARB(1, &vertex_buffer_);
104 glDeleteFramebuffersEXT(1, &framebuffer_);
105 program_ = 0;
106 vertex_shader_ = 0;
107 fragment_shader_ = 0;
108 vertex_buffer_ = 0;
109 framebuffer_ = 0;
110 u_xform_location_ = -1;
111 }
112
87 owner_stub_ = NULL; 113 owner_stub_ = NULL;
88 114
89 // If the owner goes away, there is no need to keep the SurfaceTexture around. 115 // If the owner goes away, there is no need to keep the SurfaceTexture around.
90 // The GL texture will keep working regardless with the currently bound frame. 116 // The GL texture will keep working regardless with the currently bound frame.
91 surface_texture_ = NULL; 117 surface_texture_ = NULL;
92 } 118 }
93 119
94 void StreamTexture::Destroy(bool have_context) { 120 void StreamTexture::Destroy(bool have_context) {
95 NOTREACHED(); 121 NOTREACHED();
96 } 122 }
97 123
124 scoped_ptr<ui::ScopedMakeCurrent> StreamTexture::MakeStubCurrent() {
125 scoped_ptr<ui::ScopedMakeCurrent> scoped_make_current;
126 bool needs_make_current =
127 !owner_stub_->decoder()->GetGLContext()->IsCurrent(NULL);
128 // On Android we should not have to perform a real context switch here when
129 // using virtual contexts.
130 DCHECK(!needs_make_current ||
131 !owner_stub_->decoder()
132 ->GetContextGroup()
133 ->feature_info()
134 ->workarounds()
135 .use_virtualized_gl_contexts);
136 if (needs_make_current) {
137 scoped_make_current.reset(new ui::ScopedMakeCurrent(
138 owner_stub_->decoder()->GetGLContext(), owner_stub_->surface()));
139 }
140 return scoped_make_current;
141 }
142
143 void StreamTexture::UpdateTexImage() {
144 DCHECK(surface_texture_.get());
145 DCHECK(owner_stub_);
146
147 if (!has_pending_frame_) return;
148
149 scoped_ptr<ui::ScopedMakeCurrent> scoped_make_current(MakeStubCurrent());
150
151 surface_texture_->UpdateTexImage();
152
153 has_valid_frame_ = true;
154 has_pending_frame_ = false;
155
156 float mtx[16];
157 surface_texture_->GetTransformMatrix(mtx);
158
159 if (memcmp(current_matrix_, mtx, sizeof(mtx)) != 0) {
160 memcpy(current_matrix_, mtx, sizeof(mtx));
161
162 if (has_listener_) {
163 GpuStreamTextureMsg_MatrixChanged_Params params;
164 memcpy(&params.m00, mtx, sizeof(mtx));
165 owner_stub_->channel()->Send(
166 new GpuStreamTextureMsg_MatrixChanged(route_id_, params));
167 }
168 }
169
170 if (scoped_make_current.get()) {
171 // UpdateTexImage() implies glBindTexture().
172 // The cmd decoder takes care of restoring the binding for this GLImage as
173 // far as the current context is concerned, but if we temporarily change
174 // it, we have to keep the state intact in *that* context also.
175 const gpu::gles2::ContextState* state =
176 owner_stub_->decoder()->GetContextState();
177 const gpu::gles2::TextureUnit& active_unit =
178 state->texture_units[state->active_texture_unit];
179 glBindTexture(GL_TEXTURE_EXTERNAL_OES,
180 active_unit.bound_texture_external_oes.get()
181 ? active_unit.bound_texture_external_oes->service_id()
182 : 0);
183 }
184 }
185
98 bool StreamTexture::CopyTexImage(unsigned target) { 186 bool StreamTexture::CopyTexImage(unsigned target) {
187 if (target == GL_TEXTURE_2D) {
188 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, size_.width(), size_.height(), 0,
189 GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
190 return CopyTexSubImage(GL_TEXTURE_2D, gfx::Point(), gfx::Rect(size_));
191 }
192
99 if (target != GL_TEXTURE_EXTERNAL_OES) 193 if (target != GL_TEXTURE_EXTERNAL_OES)
100 return false; 194 return false;
101 195
102 if (!owner_stub_ || !surface_texture_.get()) 196 if (!owner_stub_ || !surface_texture_.get())
103 return true; 197 return true;
104 198
105 GLint texture_id; 199 GLint texture_id;
106 glGetIntegerv(GL_TEXTURE_BINDING_EXTERNAL_OES, &texture_id); 200 glGetIntegerv(GL_TEXTURE_BINDING_EXTERNAL_OES, &texture_id);
107 DCHECK(texture_id); 201 DCHECK(texture_id);
108 202
109 // The following code only works if we're being asked to copy into 203 // The following code only works if we're being asked to copy into
110 // |texture_id_|. Copying into a different texture is not supported. 204 // |texture_id_|. Copying into a different texture is not supported.
111 if (static_cast<unsigned>(texture_id) != texture_id_) 205 if (static_cast<unsigned>(texture_id) != texture_id_)
112 return false; 206 return false;
113 207
114 if (has_pending_frame_) { 208 UpdateTexImage();
115 scoped_ptr<ui::ScopedMakeCurrent> scoped_make_current;
116 bool needs_make_current =
117 !owner_stub_->decoder()->GetGLContext()->IsCurrent(NULL);
118 // On Android we should not have to perform a real context switch here when
119 // using virtual contexts.
120 DCHECK(!needs_make_current || !owner_stub_->decoder()
121 ->GetContextGroup()
122 ->feature_info()
123 ->workarounds()
124 .use_virtualized_gl_contexts);
125 if (needs_make_current) {
126 scoped_make_current.reset(new ui::ScopedMakeCurrent(
127 owner_stub_->decoder()->GetGLContext(), owner_stub_->surface()));
128 }
129 surface_texture_->UpdateTexImage();
130 has_valid_frame_ = true;
131 has_pending_frame_ = false;
132 if (scoped_make_current.get()) {
133 // UpdateTexImage() implies glBindTexture().
134 // The cmd decoder takes care of restoring the binding for this GLImage as
135 // far as the current context is concerned, but if we temporarily change
136 // it, we have to keep the state intact in *that* context also.
137 const gpu::gles2::ContextState* state =
138 owner_stub_->decoder()->GetContextState();
139 const gpu::gles2::TextureUnit& active_unit =
140 state->texture_units[state->active_texture_unit];
141 glBindTexture(GL_TEXTURE_EXTERNAL_OES,
142 active_unit.bound_texture_external_oes.get()
143 ? active_unit.bound_texture_external_oes->service_id()
144 : 0);
145 }
146 }
147 209
148 TextureManager* texture_manager = 210 TextureManager* texture_manager =
149 owner_stub_->decoder()->GetContextGroup()->texture_manager(); 211 owner_stub_->decoder()->GetContextGroup()->texture_manager();
150 gpu::gles2::Texture* texture = 212 gpu::gles2::Texture* texture =
151 texture_manager->GetTextureForServiceId(texture_id_); 213 texture_manager->GetTextureForServiceId(texture_id_);
152 if (texture) { 214 if (texture) {
153 // By setting image state to UNBOUND instead of COPIED we ensure that 215 // By setting image state to UNBOUND instead of COPIED we ensure that
154 // CopyTexImage() is called each time the surface texture is used for 216 // CopyTexImage() is called each time the surface texture is used for
155 // drawing. 217 // drawing.
156 texture->SetLevelImage(GL_TEXTURE_EXTERNAL_OES, 0, this, 218 texture->SetLevelImage(GL_TEXTURE_EXTERNAL_OES, 0, this,
157 gpu::gles2::Texture::UNBOUND); 219 gpu::gles2::Texture::UNBOUND);
158 } 220 }
159 221
160 if (has_listener_ && has_valid_frame_) {
161 float mtx[16];
162 surface_texture_->GetTransformMatrix(mtx);
163
164 // Only query the matrix once we have bound a valid frame.
165 if (memcmp(current_matrix_, mtx, sizeof(mtx)) != 0) {
166 memcpy(current_matrix_, mtx, sizeof(mtx));
167
168 GpuStreamTextureMsg_MatrixChanged_Params params;
169 memcpy(&params.m00, mtx, sizeof(mtx));
170 owner_stub_->channel()->Send(
171 new GpuStreamTextureMsg_MatrixChanged(route_id_, params));
172 }
173 }
174
175 return true; 222 return true;
176 } 223 }
177 224
178 void StreamTexture::OnFrameAvailable() { 225 void StreamTexture::OnFrameAvailable() {
179 has_pending_frame_ = true; 226 has_pending_frame_ = true;
180 if (has_listener_ && owner_stub_) { 227 if (has_listener_ && owner_stub_) {
181 owner_stub_->channel()->Send( 228 owner_stub_->channel()->Send(
182 new GpuStreamTextureMsg_FrameAvailable(route_id_)); 229 new GpuStreamTextureMsg_FrameAvailable(route_id_));
183 } 230 }
184 } 231 }
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
224 return false; 271 return false;
225 } 272 }
226 273
227 void StreamTexture::ReleaseTexImage(unsigned target) { 274 void StreamTexture::ReleaseTexImage(unsigned target) {
228 NOTREACHED(); 275 NOTREACHED();
229 } 276 }
230 277
231 bool StreamTexture::CopyTexSubImage(unsigned target, 278 bool StreamTexture::CopyTexSubImage(unsigned target,
232 const gfx::Point& offset, 279 const gfx::Point& offset,
233 const gfx::Rect& rect) { 280 const gfx::Rect& rect) {
234 return false; 281 if (target != GL_TEXTURE_2D)
282 return false;
283
284 if (!owner_stub_ || !surface_texture_.get())
285 return true;
286
287 if (!offset.IsOrigin()) {
288 LOG(ERROR) << "Non-origin offset is not supported";
289 return false;
290 }
291
292 if (rect != gfx::Rect(size_)) {
293 LOG(ERROR) << "Sub-rectangle is not supported";
294 return false;
295 }
296
297 GLint target_texture = 0;
298 glGetIntegerv(GL_TEXTURE_BINDING_2D, &target_texture);
299 DCHECK(target_texture);
300
301 UpdateTexImage();
302
303 if (!framebuffer_) {
304 glGenFramebuffersEXT(1, &framebuffer_);
305
306 // This vertex shader introduces a y flip before applying the stream
307 // texture matrix. This is required because the stream texture matrix
308 // Android provides is intended to be used in a y-up coordinate system,
309 // whereas Chromium expects y-down.
310
311 // clang-format off
312 const char kVertexShader[] = STRINGIZE(
313 attribute vec2 a_position;
314 varying vec2 v_texCoord;
315 uniform mat4 u_xform;
316 void main() {
317 gl_Position = vec4(a_position.x, a_position.y, 0.0, 1.0);
318 vec2 uv_untransformed = a_position * vec2(0.5, -0.5) + vec2(0.5, 0.5);
319 v_texCoord = (u_xform * vec4(uv_untransformed, 0.0, 1.0)).xy;
320 }
321 );
322 const char kFragmentShader[] =
323 "#extension GL_OES_EGL_image_external : require\n" STRINGIZE(
324 precision mediump float;
325 uniform samplerExternalOES a_texture;
326 varying vec2 v_texCoord;
327 void main() {
328 gl_FragColor = texture2D(a_texture, v_texCoord);
329 }
330 );
331 // clang-format on
332
333 vertex_buffer_ = gfx::GLHelper::SetupQuadVertexBuffer();
334 vertex_shader_ = gfx::GLHelper::LoadShader(GL_VERTEX_SHADER, kVertexShader);
335 fragment_shader_ =
336 gfx::GLHelper::LoadShader(GL_FRAGMENT_SHADER, kFragmentShader);
337 program_ = gfx::GLHelper::SetupProgram(vertex_shader_, fragment_shader_);
338 gfx::ScopedUseProgram use_program(program_);
339 int sampler_location = glGetUniformLocation(program_, "a_texture");
340 DCHECK_NE(-1, sampler_location);
341 glUniform1i(sampler_location, 0);
342 u_xform_location_ = glGetUniformLocation(program_, "u_xform");
343 DCHECK_NE(-1, u_xform_location_);
344 }
345
346 gfx::ScopedActiveTexture active_texture(GL_TEXTURE0);
347 // UpdateTexImage() call below will bind the surface texture to
348 // TEXTURE_EXTERNAL_OES. This scoped texture binder will restore the current
349 // binding before this function returns.
350 gfx::ScopedTextureBinder texture_binder(GL_TEXTURE_EXTERNAL_OES, texture_id_);
351
352 {
353 gfx::ScopedFrameBufferBinder framebuffer_binder(framebuffer_);
354 gfx::ScopedViewport viewport(0, 0, size_.width(), size_.height());
355 glFramebufferTexture2DEXT(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
356 GL_TEXTURE_2D, target_texture, 0);
357 DCHECK_EQ(static_cast<GLenum>(GL_FRAMEBUFFER_COMPLETE),
358 glCheckFramebufferStatusEXT(GL_FRAMEBUFFER));
359 gfx::ScopedUseProgram use_program(program_);
360
361 glUniformMatrix4fv(u_xform_location_, 1, false, current_matrix_);
362 gfx::GLHelper::DrawQuad(vertex_buffer_);
363
364 // Detach the output texture from the fbo.
365 glFramebufferTexture2DEXT(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
366 GL_TEXTURE_2D, 0, 0);
367 }
368 return true;
235 } 369 }
236 370
237 bool StreamTexture::ScheduleOverlayPlane(gfx::AcceleratedWidget widget, 371 bool StreamTexture::ScheduleOverlayPlane(gfx::AcceleratedWidget widget,
238 int z_order, 372 int z_order,
239 gfx::OverlayTransform transform, 373 gfx::OverlayTransform transform,
240 const gfx::Rect& bounds_rect, 374 const gfx::Rect& bounds_rect,
241 const gfx::RectF& crop_rect) { 375 const gfx::RectF& crop_rect) {
242 NOTREACHED(); 376 NOTREACHED();
243 return false; 377 return false;
244 } 378 }
245 379
246 void StreamTexture::OnMemoryDump(base::trace_event::ProcessMemoryDump* pmd, 380 void StreamTexture::OnMemoryDump(base::trace_event::ProcessMemoryDump* pmd,
247 uint64_t process_tracing_id, 381 uint64_t process_tracing_id,
248 const std::string& dump_name) { 382 const std::string& dump_name) {
249 // TODO(ericrk): Add OnMemoryDump for GLImages. crbug.com/514914 383 // TODO(ericrk): Add OnMemoryDump for GLImages. crbug.com/514914
250 } 384 }
251 385
252 } // namespace content 386 } // namespace content
OLDNEW
« no previous file with comments | « content/common/gpu/stream_texture_android.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698