OLD | NEW |
---|---|
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 Loading... | |
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 UpdateTexImage(); | |
no sievers
2016/03/18 01:34:11
Doesn't this blow up in Chrome without a |scoped_p
Tobias Sargeant
2016/03/18 10:30:13
This is calling the UpdateTexImage below, which do
| |
95 surface_texture_->GetTransformMatrix(xform); | |
96 } | |
97 | |
93 void StreamTexture::OnWillDestroyStub() { | 98 void StreamTexture::OnWillDestroyStub() { |
94 owner_stub_->RemoveDestructionObserver(this); | 99 owner_stub_->RemoveDestructionObserver(this); |
95 owner_stub_->channel()->RemoveRoute(route_id_); | 100 owner_stub_->channel()->RemoveRoute(route_id_); |
96 | 101 |
97 if (framebuffer_) { | 102 if (framebuffer_) { |
98 scoped_ptr<ui::ScopedMakeCurrent> scoped_make_current(MakeStubCurrent()); | 103 scoped_ptr<ui::ScopedMakeCurrent> scoped_make_current(MakeStubCurrent()); |
99 | 104 |
100 glDeleteProgram(program_); | 105 glDeleteProgram(program_); |
101 glDeleteShader(vertex_shader_); | 106 glDeleteShader(vertex_shader_); |
102 glDeleteShader(fragment_shader_); | 107 glDeleteShader(fragment_shader_); |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
135 void StreamTexture::UpdateTexImage() { | 140 void StreamTexture::UpdateTexImage() { |
136 DCHECK(surface_texture_.get()); | 141 DCHECK(surface_texture_.get()); |
137 DCHECK(owner_stub_); | 142 DCHECK(owner_stub_); |
138 | 143 |
139 if (!has_pending_frame_) return; | 144 if (!has_pending_frame_) return; |
140 | 145 |
141 scoped_ptr<ui::ScopedMakeCurrent> scoped_make_current(MakeStubCurrent()); | 146 scoped_ptr<ui::ScopedMakeCurrent> scoped_make_current(MakeStubCurrent()); |
142 | 147 |
143 surface_texture_->UpdateTexImage(); | 148 surface_texture_->UpdateTexImage(); |
144 | 149 |
145 has_valid_frame_ = true; | |
146 has_pending_frame_ = false; | 150 has_pending_frame_ = false; |
147 | 151 |
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(¶ms.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()) { | 152 if (scoped_make_current.get()) { |
163 // UpdateTexImage() implies glBindTexture(). | 153 // UpdateTexImage() implies glBindTexture(). |
164 // The cmd decoder takes care of restoring the binding for this GLImage as | 154 // 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 | 155 // 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. | 156 // it, we have to keep the state intact in *that* context also. |
167 const gpu::gles2::ContextState* state = | 157 const gpu::gles2::ContextState* state = |
168 owner_stub_->decoder()->GetContextState(); | 158 owner_stub_->decoder()->GetContextState(); |
169 const gpu::gles2::TextureUnit& active_unit = | 159 const gpu::gles2::TextureUnit& active_unit = |
170 state->texture_units[state->active_texture_unit]; | 160 state->texture_units[state->active_texture_unit]; |
171 glBindTexture(GL_TEXTURE_EXTERNAL_OES, | 161 glBindTexture(GL_TEXTURE_EXTERNAL_OES, |
172 active_unit.bound_texture_external_oes.get() | 162 active_unit.bound_texture_external_oes.get() |
173 ? active_unit.bound_texture_external_oes->service_id() | 163 ? active_unit.bound_texture_external_oes->service_id() |
174 : 0); | 164 : 0); |
175 } | 165 } |
176 } | 166 } |
177 | 167 |
178 bool StreamTexture::CopyTexImage(unsigned target) { | 168 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) | 169 if (target != GL_TEXTURE_EXTERNAL_OES) |
186 return false; | 170 return false; |
187 | 171 |
188 if (!owner_stub_ || !surface_texture_.get()) | 172 if (!owner_stub_ || !surface_texture_.get()) |
189 return true; | 173 return true; |
190 | 174 |
191 GLint texture_id; | 175 GLint texture_id; |
192 glGetIntegerv(GL_TEXTURE_BINDING_EXTERNAL_OES, &texture_id); | 176 glGetIntegerv(GL_TEXTURE_BINDING_EXTERNAL_OES, &texture_id); |
193 DCHECK(texture_id); | 177 DCHECK(texture_id); |
194 | 178 |
195 // The following code only works if we're being asked to copy into | 179 // The following code only works if we're being asked to copy into |
196 // |texture_id_|. Copying into a different texture is not supported. | 180 // |texture_id_|. Copying into a different texture is not supported. |
197 if (static_cast<unsigned>(texture_id) != texture_id_) | 181 if (static_cast<unsigned>(texture_id) != texture_id_) |
198 return false; | 182 return false; |
199 | 183 |
200 UpdateTexImage(); | 184 UpdateTexImage(); |
201 | 185 |
202 TextureManager* texture_manager = | 186 TextureManager* texture_manager = |
203 owner_stub_->decoder()->GetContextGroup()->texture_manager(); | 187 owner_stub_->decoder()->GetContextGroup()->texture_manager(); |
204 gpu::gles2::Texture* texture = | 188 gpu::gles2::Texture* texture = |
205 texture_manager->GetTextureForServiceId(texture_id_); | 189 texture_manager->GetTextureForServiceId(texture_id_); |
206 if (texture) { | 190 if (texture) { |
207 // By setting image state to UNBOUND instead of COPIED we ensure that | 191 // By setting image state to UNBOUND instead of COPIED we ensure that |
208 // CopyTexImage() is called each time the surface texture is used for | 192 // CopyTexImage() is called each time the surface texture is used for |
209 // drawing. | 193 // drawing. |
210 texture->SetLevelImage(GL_TEXTURE_EXTERNAL_OES, 0, this, | 194 texture->SetLevelStreamTextureImage(GL_TEXTURE_EXTERNAL_OES, 0, this, |
211 gpu::gles2::Texture::UNBOUND); | 195 gpu::gles2::Texture::UNBOUND); |
212 } | 196 } |
213 | 197 |
214 return true; | 198 return true; |
215 } | 199 } |
216 | 200 |
217 void StreamTexture::OnFrameAvailable() { | 201 void StreamTexture::OnFrameAvailable() { |
218 has_pending_frame_ = true; | 202 has_pending_frame_ = true; |
219 if (has_listener_ && owner_stub_) { | 203 if (has_listener_ && owner_stub_) { |
220 owner_stub_->channel()->Send( | 204 owner_stub_->channel()->Send( |
221 new GpuStreamTextureMsg_FrameAvailable(route_id_)); | 205 new GpuStreamTextureMsg_FrameAvailable(route_id_)); |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
263 return false; | 247 return false; |
264 } | 248 } |
265 | 249 |
266 void StreamTexture::ReleaseTexImage(unsigned target) { | 250 void StreamTexture::ReleaseTexImage(unsigned target) { |
267 NOTREACHED(); | 251 NOTREACHED(); |
268 } | 252 } |
269 | 253 |
270 bool StreamTexture::CopyTexSubImage(unsigned target, | 254 bool StreamTexture::CopyTexSubImage(unsigned target, |
271 const gfx::Point& offset, | 255 const gfx::Point& offset, |
272 const gfx::Rect& rect) { | 256 const gfx::Rect& rect) { |
273 if (target != GL_TEXTURE_2D) | 257 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 } | 258 } |
362 | 259 |
363 bool StreamTexture::ScheduleOverlayPlane(gfx::AcceleratedWidget widget, | 260 bool StreamTexture::ScheduleOverlayPlane(gfx::AcceleratedWidget widget, |
364 int z_order, | 261 int z_order, |
365 gfx::OverlayTransform transform, | 262 gfx::OverlayTransform transform, |
366 const gfx::Rect& bounds_rect, | 263 const gfx::Rect& bounds_rect, |
367 const gfx::RectF& crop_rect) { | 264 const gfx::RectF& crop_rect) { |
368 NOTREACHED(); | 265 NOTREACHED(); |
369 return false; | 266 return false; |
370 } | 267 } |
371 | 268 |
372 void StreamTexture::OnMemoryDump(base::trace_event::ProcessMemoryDump* pmd, | 269 void StreamTexture::OnMemoryDump(base::trace_event::ProcessMemoryDump* pmd, |
373 uint64_t process_tracing_id, | 270 uint64_t process_tracing_id, |
374 const std::string& dump_name) { | 271 const std::string& dump_name) { |
375 // TODO(ericrk): Add OnMemoryDump for GLImages. crbug.com/514914 | 272 // TODO(ericrk): Add OnMemoryDump for GLImages. crbug.com/514914 |
376 } | 273 } |
377 | 274 |
378 } // namespace content | 275 } // namespace content |
OLD | NEW |