| 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" | |
| 11 #include "content/common/android/surface_texture_peer.h" | 10 #include "content/common/android/surface_texture_peer.h" |
| 12 #include "content/common/gpu/gpu_channel.h" | 11 #include "content/common/gpu/gpu_channel.h" |
| 13 #include "content/common/gpu/gpu_messages.h" | 12 #include "content/common/gpu/gpu_messages.h" |
| 14 #include "gpu/command_buffer/service/context_group.h" | 13 #include "gpu/command_buffer/service/context_group.h" |
| 15 #include "gpu/command_buffer/service/context_state.h" | 14 #include "gpu/command_buffer/service/context_state.h" |
| 16 #include "gpu/command_buffer/service/gles2_cmd_decoder.h" | 15 #include "gpu/command_buffer/service/gles2_cmd_decoder.h" |
| 17 #include "gpu/command_buffer/service/texture_manager.h" | 16 #include "gpu/command_buffer/service/texture_manager.h" |
| 18 #include "ui/gfx/geometry/size.h" | 17 #include "ui/gfx/geometry/size.h" |
| 19 #include "ui/gl/gl_context.h" | 18 #include "ui/gl/gl_context.h" |
| 20 #include "ui/gl/gl_helper.h" | |
| 21 #include "ui/gl/scoped_binders.h" | |
| 22 #include "ui/gl/scoped_make_current.h" | 19 #include "ui/gl/scoped_make_current.h" |
| 23 | 20 |
| 24 namespace content { | 21 namespace content { |
| 25 | 22 |
| 26 using gpu::gles2::ContextGroup; | 23 using gpu::gles2::ContextGroup; |
| 27 using gpu::gles2::GLES2Decoder; | 24 using gpu::gles2::GLES2Decoder; |
| 28 using gpu::gles2::TextureManager; | 25 using gpu::gles2::TextureManager; |
| 29 using gpu::gles2::TextureRef; | 26 using gpu::gles2::TextureRef; |
| 30 | 27 |
| 31 // static | 28 // static |
| (...skipping 30 matching lines...) Expand all Loading... |
| 62 int32_t route_id, | 59 int32_t route_id, |
| 63 uint32_t texture_id) | 60 uint32_t texture_id) |
| 64 : surface_texture_(gfx::SurfaceTexture::Create(texture_id)), | 61 : surface_texture_(gfx::SurfaceTexture::Create(texture_id)), |
| 65 size_(0, 0), | 62 size_(0, 0), |
| 66 has_valid_frame_(false), | 63 has_valid_frame_(false), |
| 67 has_pending_frame_(false), | 64 has_pending_frame_(false), |
| 68 owner_stub_(owner_stub), | 65 owner_stub_(owner_stub), |
| 69 route_id_(route_id), | 66 route_id_(route_id), |
| 70 has_listener_(false), | 67 has_listener_(false), |
| 71 texture_id_(texture_id), | 68 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), | |
| 78 weak_factory_(this) { | 69 weak_factory_(this) { |
| 79 owner_stub->AddDestructionObserver(this); | 70 owner_stub->AddDestructionObserver(this); |
| 80 memset(current_matrix_, 0, sizeof(current_matrix_)); | 71 memset(current_matrix_, 0, sizeof(current_matrix_)); |
| 81 owner_stub->channel()->AddRoute(route_id, this); | 72 owner_stub->channel()->AddRoute(route_id, this); |
| 82 surface_texture_->SetFrameAvailableCallback(base::Bind( | 73 surface_texture_->SetFrameAvailableCallback(base::Bind( |
| 83 &StreamTexture::OnFrameAvailable, weak_factory_.GetWeakPtr())); | 74 &StreamTexture::OnFrameAvailable, weak_factory_.GetWeakPtr())); |
| 84 } | 75 } |
| 85 | 76 |
| 86 StreamTexture::~StreamTexture() { | 77 StreamTexture::~StreamTexture() { |
| 87 if (owner_stub_) { | 78 if (owner_stub_) { |
| 88 owner_stub_->RemoveDestructionObserver(this); | 79 owner_stub_->RemoveDestructionObserver(this); |
| 89 owner_stub_->channel()->RemoveRoute(route_id_); | 80 owner_stub_->channel()->RemoveRoute(route_id_); |
| 90 } | 81 } |
| 91 } | 82 } |
| 92 | 83 |
| 93 void StreamTexture::OnWillDestroyStub() { | 84 void StreamTexture::OnWillDestroyStub() { |
| 94 owner_stub_->RemoveDestructionObserver(this); | 85 owner_stub_->RemoveDestructionObserver(this); |
| 95 owner_stub_->channel()->RemoveRoute(route_id_); | 86 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 | |
| 113 owner_stub_ = NULL; | 87 owner_stub_ = NULL; |
| 114 | 88 |
| 115 // If the owner goes away, there is no need to keep the SurfaceTexture around. | 89 // If the owner goes away, there is no need to keep the SurfaceTexture around. |
| 116 // The GL texture will keep working regardless with the currently bound frame. | 90 // The GL texture will keep working regardless with the currently bound frame. |
| 117 surface_texture_ = NULL; | 91 surface_texture_ = NULL; |
| 118 } | 92 } |
| 119 | 93 |
| 120 void StreamTexture::Destroy(bool have_context) { | 94 void StreamTexture::Destroy(bool have_context) { |
| 121 NOTREACHED(); | 95 NOTREACHED(); |
| 122 } | 96 } |
| 123 | 97 |
| 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(¶ms.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 | |
| 186 bool StreamTexture::CopyTexImage(unsigned target) { | 98 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 | |
| 193 if (target != GL_TEXTURE_EXTERNAL_OES) | 99 if (target != GL_TEXTURE_EXTERNAL_OES) |
| 194 return false; | 100 return false; |
| 195 | 101 |
| 196 if (!owner_stub_ || !surface_texture_.get()) | 102 if (!owner_stub_ || !surface_texture_.get()) |
| 197 return true; | 103 return true; |
| 198 | 104 |
| 199 GLint texture_id; | 105 GLint texture_id; |
| 200 glGetIntegerv(GL_TEXTURE_BINDING_EXTERNAL_OES, &texture_id); | 106 glGetIntegerv(GL_TEXTURE_BINDING_EXTERNAL_OES, &texture_id); |
| 201 DCHECK(texture_id); | 107 DCHECK(texture_id); |
| 202 | 108 |
| 203 // The following code only works if we're being asked to copy into | 109 // The following code only works if we're being asked to copy into |
| 204 // |texture_id_|. Copying into a different texture is not supported. | 110 // |texture_id_|. Copying into a different texture is not supported. |
| 205 if (static_cast<unsigned>(texture_id) != texture_id_) | 111 if (static_cast<unsigned>(texture_id) != texture_id_) |
| 206 return false; | 112 return false; |
| 207 | 113 |
| 208 UpdateTexImage(); | 114 if (has_pending_frame_) { |
| 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 } |
| 209 | 147 |
| 210 TextureManager* texture_manager = | 148 TextureManager* texture_manager = |
| 211 owner_stub_->decoder()->GetContextGroup()->texture_manager(); | 149 owner_stub_->decoder()->GetContextGroup()->texture_manager(); |
| 212 gpu::gles2::Texture* texture = | 150 gpu::gles2::Texture* texture = |
| 213 texture_manager->GetTextureForServiceId(texture_id_); | 151 texture_manager->GetTextureForServiceId(texture_id_); |
| 214 if (texture) { | 152 if (texture) { |
| 215 // By setting image state to UNBOUND instead of COPIED we ensure that | 153 // By setting image state to UNBOUND instead of COPIED we ensure that |
| 216 // CopyTexImage() is called each time the surface texture is used for | 154 // CopyTexImage() is called each time the surface texture is used for |
| 217 // drawing. | 155 // drawing. |
| 218 texture->SetLevelImage(GL_TEXTURE_EXTERNAL_OES, 0, this, | 156 texture->SetLevelImage(GL_TEXTURE_EXTERNAL_OES, 0, this, |
| 219 gpu::gles2::Texture::UNBOUND); | 157 gpu::gles2::Texture::UNBOUND); |
| 220 } | 158 } |
| 221 | 159 |
| 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(¶ms.m00, mtx, sizeof(mtx)); |
| 170 owner_stub_->channel()->Send( |
| 171 new GpuStreamTextureMsg_MatrixChanged(route_id_, params)); |
| 172 } |
| 173 } |
| 174 |
| 222 return true; | 175 return true; |
| 223 } | 176 } |
| 224 | 177 |
| 225 void StreamTexture::OnFrameAvailable() { | 178 void StreamTexture::OnFrameAvailable() { |
| 226 has_pending_frame_ = true; | 179 has_pending_frame_ = true; |
| 227 if (has_listener_ && owner_stub_) { | 180 if (has_listener_ && owner_stub_) { |
| 228 owner_stub_->channel()->Send( | 181 owner_stub_->channel()->Send( |
| 229 new GpuStreamTextureMsg_FrameAvailable(route_id_)); | 182 new GpuStreamTextureMsg_FrameAvailable(route_id_)); |
| 230 } | 183 } |
| 231 } | 184 } |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 271 return false; | 224 return false; |
| 272 } | 225 } |
| 273 | 226 |
| 274 void StreamTexture::ReleaseTexImage(unsigned target) { | 227 void StreamTexture::ReleaseTexImage(unsigned target) { |
| 275 NOTREACHED(); | 228 NOTREACHED(); |
| 276 } | 229 } |
| 277 | 230 |
| 278 bool StreamTexture::CopyTexSubImage(unsigned target, | 231 bool StreamTexture::CopyTexSubImage(unsigned target, |
| 279 const gfx::Point& offset, | 232 const gfx::Point& offset, |
| 280 const gfx::Rect& rect) { | 233 const gfx::Rect& rect) { |
| 281 if (target != GL_TEXTURE_2D) | 234 return false; |
| 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 // clang-format off | |
| 307 const char kVertexShader[] = STRINGIZE( | |
| 308 attribute vec2 a_position; | |
| 309 varying vec2 v_texCoord; | |
| 310 uniform mat4 u_xform; | |
| 311 void main() { | |
| 312 gl_Position = vec4(a_position.x, a_position.y, 0.0, 1.0); | |
| 313 vec2 uv_untransformed = (a_position + vec2(1.0, 1.0)) * 0.5; | |
| 314 v_texCoord = (u_xform * vec4(uv_untransformed, 0.0, 1.0)).xy; | |
| 315 } | |
| 316 ); | |
| 317 const char kFragmentShader[] = | |
| 318 "#extension GL_OES_EGL_image_external : require\n" STRINGIZE( | |
| 319 precision mediump float; | |
| 320 uniform samplerExternalOES a_texture; | |
| 321 varying vec2 v_texCoord; | |
| 322 void main() { | |
| 323 gl_FragColor = texture2D(a_texture, v_texCoord); | |
| 324 } | |
| 325 ); | |
| 326 // clang-format on | |
| 327 | |
| 328 vertex_buffer_ = gfx::GLHelper::SetupQuadVertexBuffer(); | |
| 329 vertex_shader_ = gfx::GLHelper::LoadShader(GL_VERTEX_SHADER, kVertexShader); | |
| 330 fragment_shader_ = | |
| 331 gfx::GLHelper::LoadShader(GL_FRAGMENT_SHADER, kFragmentShader); | |
| 332 program_ = gfx::GLHelper::SetupProgram(vertex_shader_, fragment_shader_); | |
| 333 gfx::ScopedUseProgram use_program(program_); | |
| 334 int sampler_location = glGetUniformLocation(program_, "a_texture"); | |
| 335 DCHECK_NE(-1, sampler_location); | |
| 336 glUniform1i(sampler_location, 0); | |
| 337 u_xform_location_ = glGetUniformLocation(program_, "u_xform"); | |
| 338 DCHECK_NE(-1, u_xform_location_); | |
| 339 } | |
| 340 | |
| 341 gfx::ScopedActiveTexture active_texture(GL_TEXTURE0); | |
| 342 // UpdateTexImage() call below will bind the surface texture to | |
| 343 // TEXTURE_EXTERNAL_OES. This scoped texture binder will restore the current | |
| 344 // binding before this function returns. | |
| 345 gfx::ScopedTextureBinder texture_binder(GL_TEXTURE_EXTERNAL_OES, texture_id_); | |
| 346 | |
| 347 { | |
| 348 gfx::ScopedFrameBufferBinder framebuffer_binder(framebuffer_); | |
| 349 gfx::ScopedViewport viewport(0, 0, size_.width(), size_.height()); | |
| 350 glFramebufferTexture2DEXT(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, | |
| 351 GL_TEXTURE_2D, target_texture, 0); | |
| 352 DCHECK_EQ(static_cast<GLenum>(GL_FRAMEBUFFER_COMPLETE), | |
| 353 glCheckFramebufferStatusEXT(GL_FRAMEBUFFER)); | |
| 354 gfx::ScopedUseProgram use_program(program_); | |
| 355 | |
| 356 glUniformMatrix4fv(u_xform_location_, 1, false, current_matrix_); | |
| 357 gfx::GLHelper::DrawQuad(vertex_buffer_); | |
| 358 | |
| 359 // Detach the output texture from the fbo. | |
| 360 glFramebufferTexture2DEXT(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, | |
| 361 GL_TEXTURE_2D, 0, 0); | |
| 362 } | |
| 363 return true; | |
| 364 } | 235 } |
| 365 | 236 |
| 366 bool StreamTexture::ScheduleOverlayPlane(gfx::AcceleratedWidget widget, | 237 bool StreamTexture::ScheduleOverlayPlane(gfx::AcceleratedWidget widget, |
| 367 int z_order, | 238 int z_order, |
| 368 gfx::OverlayTransform transform, | 239 gfx::OverlayTransform transform, |
| 369 const gfx::Rect& bounds_rect, | 240 const gfx::Rect& bounds_rect, |
| 370 const gfx::RectF& crop_rect) { | 241 const gfx::RectF& crop_rect) { |
| 371 NOTREACHED(); | 242 NOTREACHED(); |
| 372 return false; | 243 return false; |
| 373 } | 244 } |
| 374 | 245 |
| 375 void StreamTexture::OnMemoryDump(base::trace_event::ProcessMemoryDump* pmd, | 246 void StreamTexture::OnMemoryDump(base::trace_event::ProcessMemoryDump* pmd, |
| 376 uint64_t process_tracing_id, | 247 uint64_t process_tracing_id, |
| 377 const std::string& dump_name) { | 248 const std::string& dump_name) { |
| 378 // TODO(ericrk): Add OnMemoryDump for GLImages. crbug.com/514914 | 249 // TODO(ericrk): Add OnMemoryDump for GLImages. crbug.com/514914 |
| 379 } | 250 } |
| 380 | 251 |
| 381 } // namespace content | 252 } // namespace content |
| OLD | NEW |