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(); |
| 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 | 177 |
194 // 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 |
195 // |texture_id_|. Copying into a different texture is not supported. | 179 // |texture_id_|. Copying into a different texture is not supported. |
196 // On some devices GL_TEXTURE_BINDING_EXTERNAL_OES is not supported as | 180 // On some devices GL_TEXTURE_BINDING_EXTERNAL_OES is not supported as |
197 // glGetIntegerv() parameter. In this case the value of |texture_id| will be | 181 // glGetIntegerv() parameter. In this case the value of |texture_id| will be |
198 // zero and we assume that it is properly bound to |texture_id_|. | 182 // zero and we assume that it is properly bound to |texture_id_|. |
199 if (texture_id > 0 && static_cast<unsigned>(texture_id) != texture_id_) | 183 if (texture_id > 0 && static_cast<unsigned>(texture_id) != texture_id_) |
200 return false; | 184 return false; |
201 | 185 |
202 UpdateTexImage(); | 186 UpdateTexImage(); |
203 | 187 |
204 TextureManager* texture_manager = | 188 TextureManager* texture_manager = |
205 owner_stub_->decoder()->GetContextGroup()->texture_manager(); | 189 owner_stub_->decoder()->GetContextGroup()->texture_manager(); |
206 gpu::gles2::Texture* texture = | 190 gpu::gles2::Texture* texture = |
207 texture_manager->GetTextureForServiceId(texture_id_); | 191 texture_manager->GetTextureForServiceId(texture_id_); |
208 if (texture) { | 192 if (texture) { |
209 // By setting image state to UNBOUND instead of COPIED we ensure that | 193 // By setting image state to UNBOUND instead of COPIED we ensure that |
210 // CopyTexImage() is called each time the surface texture is used for | 194 // CopyTexImage() is called each time the surface texture is used for |
211 // drawing. | 195 // drawing. |
212 texture->SetLevelImage(GL_TEXTURE_EXTERNAL_OES, 0, this, | 196 texture->SetLevelStreamTextureImage(GL_TEXTURE_EXTERNAL_OES, 0, this, |
213 gpu::gles2::Texture::UNBOUND); | 197 gpu::gles2::Texture::UNBOUND); |
214 } | 198 } |
215 | 199 |
216 return true; | 200 return true; |
217 } | 201 } |
218 | 202 |
219 void StreamTexture::OnFrameAvailable() { | 203 void StreamTexture::OnFrameAvailable() { |
220 has_pending_frame_ = true; | 204 has_pending_frame_ = true; |
221 if (has_listener_ && owner_stub_) { | 205 if (has_listener_ && owner_stub_) { |
222 owner_stub_->channel()->Send( | 206 owner_stub_->channel()->Send( |
223 new GpuStreamTextureMsg_FrameAvailable(route_id_)); | 207 new GpuStreamTextureMsg_FrameAvailable(route_id_)); |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
265 return false; | 249 return false; |
266 } | 250 } |
267 | 251 |
268 void StreamTexture::ReleaseTexImage(unsigned target) { | 252 void StreamTexture::ReleaseTexImage(unsigned target) { |
269 NOTREACHED(); | 253 NOTREACHED(); |
270 } | 254 } |
271 | 255 |
272 bool StreamTexture::CopyTexSubImage(unsigned target, | 256 bool StreamTexture::CopyTexSubImage(unsigned target, |
273 const gfx::Point& offset, | 257 const gfx::Point& offset, |
274 const gfx::Rect& rect) { | 258 const gfx::Rect& rect) { |
275 if (target != GL_TEXTURE_2D) | 259 return false; |
276 return false; | |
277 | |
278 if (!owner_stub_ || !surface_texture_.get()) | |
279 return true; | |
280 | |
281 if (!offset.IsOrigin()) { | |
282 LOG(ERROR) << "Non-origin offset is not supported"; | |
283 return false; | |
284 } | |
285 | |
286 if (rect != gfx::Rect(size_)) { | |
287 LOG(ERROR) << "Sub-rectangle is not supported"; | |
288 return false; | |
289 } | |
290 | |
291 GLint target_texture = 0; | |
292 glGetIntegerv(GL_TEXTURE_BINDING_2D, &target_texture); | |
293 DCHECK(target_texture); | |
294 | |
295 UpdateTexImage(); | |
296 | |
297 if (!framebuffer_) { | |
298 glGenFramebuffersEXT(1, &framebuffer_); | |
299 | |
300 // This vertex shader introduces a y flip before applying the stream | |
301 // texture matrix. This is required because the stream texture matrix | |
302 // Android provides is intended to be used in a y-up coordinate system, | |
303 // whereas Chromium expects y-down. | |
304 | |
305 // clang-format off | |
306 const char kVertexShader[] = STRINGIZE( | |
307 attribute vec2 a_position; | |
308 varying vec2 v_texCoord; | |
309 uniform mat4 u_xform; | |
310 void main() { | |
311 gl_Position = vec4(a_position.x, a_position.y, 0.0, 1.0); | |
312 vec2 uv_untransformed = a_position * vec2(0.5, -0.5) + vec2(0.5, 0.5); | |
313 v_texCoord = (u_xform * vec4(uv_untransformed, 0.0, 1.0)).xy; | |
314 } | |
315 ); | |
316 const char kFragmentShader[] = | |
317 "#extension GL_OES_EGL_image_external : require\n" STRINGIZE( | |
318 precision mediump float; | |
319 uniform samplerExternalOES a_texture; | |
320 varying vec2 v_texCoord; | |
321 void main() { | |
322 gl_FragColor = texture2D(a_texture, v_texCoord); | |
323 } | |
324 ); | |
325 // clang-format on | |
326 | |
327 vertex_buffer_ = gfx::GLHelper::SetupQuadVertexBuffer(); | |
328 vertex_shader_ = gfx::GLHelper::LoadShader(GL_VERTEX_SHADER, kVertexShader); | |
329 fragment_shader_ = | |
330 gfx::GLHelper::LoadShader(GL_FRAGMENT_SHADER, kFragmentShader); | |
331 program_ = gfx::GLHelper::SetupProgram(vertex_shader_, fragment_shader_); | |
332 gfx::ScopedUseProgram use_program(program_); | |
333 int sampler_location = glGetUniformLocation(program_, "a_texture"); | |
334 DCHECK_NE(-1, sampler_location); | |
335 glUniform1i(sampler_location, 0); | |
336 u_xform_location_ = glGetUniformLocation(program_, "u_xform"); | |
337 DCHECK_NE(-1, u_xform_location_); | |
338 } | |
339 | |
340 gfx::ScopedActiveTexture active_texture(GL_TEXTURE0); | |
341 // UpdateTexImage() call below will bind the surface texture to | |
342 // TEXTURE_EXTERNAL_OES. This scoped texture binder will restore the current | |
343 // binding before this function returns. | |
344 gfx::ScopedTextureBinder texture_binder(GL_TEXTURE_EXTERNAL_OES, texture_id_); | |
345 | |
346 { | |
347 gfx::ScopedFrameBufferBinder framebuffer_binder(framebuffer_); | |
348 gfx::ScopedViewport viewport(0, 0, size_.width(), size_.height()); | |
349 glFramebufferTexture2DEXT(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, | |
350 GL_TEXTURE_2D, target_texture, 0); | |
351 DCHECK_EQ(static_cast<GLenum>(GL_FRAMEBUFFER_COMPLETE), | |
352 glCheckFramebufferStatusEXT(GL_FRAMEBUFFER)); | |
353 gfx::ScopedUseProgram use_program(program_); | |
354 | |
355 glUniformMatrix4fv(u_xform_location_, 1, false, current_matrix_); | |
356 gfx::GLHelper::DrawQuad(vertex_buffer_); | |
357 | |
358 // Detach the output texture from the fbo. | |
359 glFramebufferTexture2DEXT(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, | |
360 GL_TEXTURE_2D, 0, 0); | |
361 } | |
362 return true; | |
363 } | 260 } |
364 | 261 |
365 bool StreamTexture::ScheduleOverlayPlane(gfx::AcceleratedWidget widget, | 262 bool StreamTexture::ScheduleOverlayPlane(gfx::AcceleratedWidget widget, |
366 int z_order, | 263 int z_order, |
367 gfx::OverlayTransform transform, | 264 gfx::OverlayTransform transform, |
368 const gfx::Rect& bounds_rect, | 265 const gfx::Rect& bounds_rect, |
369 const gfx::RectF& crop_rect) { | 266 const gfx::RectF& crop_rect) { |
370 NOTREACHED(); | 267 NOTREACHED(); |
371 return false; | 268 return false; |
372 } | 269 } |
373 | 270 |
374 void StreamTexture::OnMemoryDump(base::trace_event::ProcessMemoryDump* pmd, | 271 void StreamTexture::OnMemoryDump(base::trace_event::ProcessMemoryDump* pmd, |
375 uint64_t process_tracing_id, | 272 uint64_t process_tracing_id, |
376 const std::string& dump_name) { | 273 const std::string& dump_name) { |
377 // TODO(ericrk): Add OnMemoryDump for GLImages. crbug.com/514914 | 274 // TODO(ericrk): Add OnMemoryDump for GLImages. crbug.com/514914 |
378 } | 275 } |
379 | 276 |
380 } // namespace content | 277 } // namespace content |
OLD | NEW |