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 "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/strings/stringize_macros.h" | |
8 #include "content/common/android/surface_texture_peer.h" | 9 #include "content/common/android/surface_texture_peer.h" |
9 #include "content/common/gpu/gpu_channel.h" | 10 #include "content/common/gpu/gpu_channel.h" |
10 #include "content/common/gpu/gpu_messages.h" | 11 #include "content/common/gpu/gpu_messages.h" |
11 #include "gpu/command_buffer/service/context_group.h" | 12 #include "gpu/command_buffer/service/context_group.h" |
12 #include "gpu/command_buffer/service/context_state.h" | 13 #include "gpu/command_buffer/service/context_state.h" |
13 #include "gpu/command_buffer/service/gles2_cmd_decoder.h" | 14 #include "gpu/command_buffer/service/gles2_cmd_decoder.h" |
14 #include "gpu/command_buffer/service/texture_manager.h" | 15 #include "gpu/command_buffer/service/texture_manager.h" |
15 #include "ui/gfx/geometry/size.h" | 16 #include "ui/gfx/geometry/size.h" |
16 #include "ui/gl/gl_context.h" | 17 #include "ui/gl/gl_context.h" |
18 #include "ui/gl/gl_helper.h" | |
19 #include "ui/gl/scoped_binders.h" | |
17 #include "ui/gl/scoped_make_current.h" | 20 #include "ui/gl/scoped_make_current.h" |
18 | 21 |
19 namespace content { | 22 namespace content { |
20 | 23 |
21 using gpu::gles2::ContextGroup; | 24 using gpu::gles2::ContextGroup; |
22 using gpu::gles2::GLES2Decoder; | 25 using gpu::gles2::GLES2Decoder; |
23 using gpu::gles2::TextureManager; | 26 using gpu::gles2::TextureManager; |
24 using gpu::gles2::TextureRef; | 27 using gpu::gles2::TextureRef; |
25 | 28 |
26 // static | 29 // static |
(...skipping 30 matching lines...) Expand all Loading... | |
57 StreamTexture::StreamTexture(GpuCommandBufferStub* owner_stub, | 60 StreamTexture::StreamTexture(GpuCommandBufferStub* owner_stub, |
58 int32 route_id, | 61 int32 route_id, |
59 uint32 texture_id) | 62 uint32 texture_id) |
60 : surface_texture_(gfx::SurfaceTexture::Create(texture_id)), | 63 : surface_texture_(gfx::SurfaceTexture::Create(texture_id)), |
61 size_(0, 0), | 64 size_(0, 0), |
62 has_valid_frame_(false), | 65 has_valid_frame_(false), |
63 has_pending_frame_(false), | 66 has_pending_frame_(false), |
64 owner_stub_(owner_stub), | 67 owner_stub_(owner_stub), |
65 route_id_(route_id), | 68 route_id_(route_id), |
66 has_listener_(false), | 69 has_listener_(false), |
70 matrix_modified_(false), | |
67 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_loc_(-1), | |
68 weak_factory_(this) { | 78 weak_factory_(this) { |
69 owner_stub->AddDestructionObserver(this); | 79 owner_stub->AddDestructionObserver(this); |
70 memset(current_matrix_, 0, sizeof(current_matrix_)); | 80 memset(current_matrix_, 0, sizeof(current_matrix_)); |
71 owner_stub->channel()->AddRoute(route_id, this); | 81 owner_stub->channel()->AddRoute(route_id, this); |
72 surface_texture_->SetFrameAvailableCallback(base::Bind( | 82 surface_texture_->SetFrameAvailableCallback(base::Bind( |
73 &StreamTexture::OnFrameAvailable, weak_factory_.GetWeakPtr())); | 83 &StreamTexture::OnFrameAvailable, weak_factory_.GetWeakPtr())); |
74 } | 84 } |
75 | 85 |
76 StreamTexture::~StreamTexture() { | 86 StreamTexture::~StreamTexture() { |
77 if (owner_stub_) { | 87 if (owner_stub_) { |
78 owner_stub_->RemoveDestructionObserver(this); | 88 owner_stub_->RemoveDestructionObserver(this); |
79 owner_stub_->channel()->RemoveRoute(route_id_); | 89 owner_stub_->channel()->RemoveRoute(route_id_); |
80 } | 90 } |
81 } | 91 } |
82 | 92 |
83 void StreamTexture::OnWillDestroyStub() { | 93 void StreamTexture::OnWillDestroyStub() { |
84 owner_stub_->RemoveDestructionObserver(this); | 94 owner_stub_->RemoveDestructionObserver(this); |
85 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_loc_ = -1; | |
111 } | |
112 | |
86 owner_stub_ = NULL; | 113 owner_stub_ = NULL; |
87 | 114 |
88 // 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. |
89 // 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. |
90 surface_texture_ = NULL; | 117 surface_texture_ = NULL; |
91 } | 118 } |
92 | 119 |
93 void StreamTexture::Destroy(bool have_context) { | 120 void StreamTexture::Destroy(bool have_context) { |
94 NOTREACHED(); | 121 NOTREACHED(); |
95 } | 122 } |
96 | 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::DoUpdateTexImage() { | |
144 DCHECK(surface_texture_.get()); | |
145 | |
146 scoped_ptr<ui::ScopedMakeCurrent> scoped_make_current(MakeStubCurrent()); | |
147 | |
148 surface_texture_->UpdateTexImage(); | |
149 has_valid_frame_ = true; | |
150 has_pending_frame_ = false; | |
151 | |
152 if (scoped_make_current.get()) { | |
153 // UpdateTexImage() implies glBindTexture(). | |
154 // The cmd decoder takes care of restoring the binding for this GLImage as | |
155 // far as the current context is concerned, but if we temporarily change | |
156 // it, we have to keep the state intact in *that* context also. | |
157 const gpu::gles2::ContextState* state = | |
158 owner_stub_->decoder()->GetContextState(); | |
159 const gpu::gles2::TextureUnit& active_unit = | |
160 state->texture_units[state->active_texture_unit]; | |
161 glBindTexture(GL_TEXTURE_EXTERNAL_OES, | |
162 active_unit.bound_texture_external_oes.get() | |
163 ? active_unit.bound_texture_external_oes->service_id() | |
164 : 0); | |
165 } | |
166 } | |
167 | |
168 bool StreamTexture::DoCopyTexture(const gfx::Point& offset, | |
169 const gfx::Rect& rect) { | |
170 if (!owner_stub_ || !surface_texture_.get()) | |
171 return true; | |
172 | |
173 if (!offset.IsOrigin()) { | |
174 LOG(ERROR) << "Non-origin offset is not supported"; | |
175 return false; | |
176 } | |
177 | |
178 if (rect != gfx::Rect(size_)) { | |
179 LOG(ERROR) << "Sub-rectangle is not supported"; | |
180 return false; | |
181 } | |
182 GLint target_texture = 0; | |
183 glGetIntegerv(GL_TEXTURE_BINDING_2D, &target_texture); | |
184 DCHECK(target_texture); | |
185 | |
186 DoUpdateTexImage(); | |
187 UpdateTransformMatrix(); | |
188 | |
189 if (!framebuffer_) { | |
190 glGenFramebuffersEXT(1, &framebuffer_); | |
191 | |
192 // clang-format off | |
193 const char kVertexShader[] = STRINGIZE( | |
194 attribute vec2 a_position; | |
195 varying vec2 v_texCoord; | |
196 uniform mat4 u_xform; | |
197 void main() { | |
198 gl_Position = vec4(a_position.x, a_position.y, 0.0, 1.0); | |
199 vec2 uv_untransformed = (a_position + vec2(1.0, 1.0)) * 0.5; | |
200 v_texCoord = (u_xform * vec4(uv_untransformed, 0.0, 1.0)).xy; | |
201 } | |
202 ); | |
203 const char kFragmentShader[] = | |
204 "#extension GL_OES_EGL_image_external : require\n" STRINGIZE( | |
205 precision mediump float; | |
206 uniform samplerExternalOES a_texture; | |
207 varying vec2 v_texCoord; | |
208 void main() { | |
209 gl_FragColor = texture2D(a_texture, v_texCoord); | |
210 } | |
211 ); | |
212 // clang-format on | |
213 | |
214 vertex_buffer_ = gfx::GLHelper::SetupQuadVertexBuffer(); | |
215 vertex_shader_ = gfx::GLHelper::LoadShader(GL_VERTEX_SHADER, kVertexShader); | |
216 fragment_shader_ = | |
217 gfx::GLHelper::LoadShader(GL_FRAGMENT_SHADER, kFragmentShader); | |
218 program_ = gfx::GLHelper::SetupProgram(vertex_shader_, fragment_shader_); | |
219 gfx::ScopedUseProgram use_program(program_); | |
220 int sampler_location = glGetUniformLocation(program_, "a_texture"); | |
221 DCHECK_NE(-1, sampler_location); | |
222 glUniform1i(sampler_location, 0); | |
223 u_xform_loc_ = glGetUniformLocation(program_, "u_xform"); | |
224 DCHECK_NE(-1, u_xform_loc_); | |
225 } | |
226 | |
227 gfx::ScopedActiveTexture active_texture(GL_TEXTURE0); | |
228 // UpdateTexImage() call below will bind the surface texture to | |
229 // TEXTURE_EXTERNAL_OES. This scoped texture binder will restore the current | |
230 // binding before this function returns. | |
231 gfx::ScopedTextureBinder texture_binder(GL_TEXTURE_EXTERNAL_OES, texture_id_); | |
232 | |
233 { | |
234 gfx::ScopedFrameBufferBinder framebuffer_binder(framebuffer_); | |
235 gfx::ScopedViewport viewport(0, 0, size_.width(), size_.height()); | |
236 glFramebufferTexture2DEXT(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, | |
237 GL_TEXTURE_2D, target_texture, 0); | |
238 DCHECK_EQ(static_cast<GLenum>(GL_FRAMEBUFFER_COMPLETE), | |
239 glCheckFramebufferStatusEXT(GL_FRAMEBUFFER)); | |
240 gfx::ScopedUseProgram use_program(program_); | |
241 | |
242 glUniformMatrix4fv(u_xform_loc_, 1, false, current_matrix_); | |
243 gfx::GLHelper::DrawQuad(vertex_buffer_); | |
244 | |
245 // Detach the output texture from the fbo. | |
246 glFramebufferTexture2DEXT(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, | |
247 GL_TEXTURE_2D, 0, 0); | |
248 } | |
249 | |
250 return true; | |
251 } | |
252 | |
97 bool StreamTexture::CopyTexImage(unsigned target) { | 253 bool StreamTexture::CopyTexImage(unsigned target) { |
reveman
2015/12/23 23:11:49
CopyTexImage unlike CopySubTexImage is supposed to
Tobias Sargeant
2015/12/24 01:28:34
Done.
| |
254 if (target == GL_TEXTURE_2D) { | |
255 return DoCopyTexture(gfx::Point(), gfx::Rect(size_)); | |
256 } | |
257 | |
98 if (target != GL_TEXTURE_EXTERNAL_OES) | 258 if (target != GL_TEXTURE_EXTERNAL_OES) |
99 return false; | 259 return false; |
100 | 260 |
101 if (!owner_stub_ || !surface_texture_.get()) | 261 if (!owner_stub_ || !surface_texture_.get()) |
102 return true; | 262 return true; |
103 | 263 |
104 GLint texture_id; | 264 GLint texture_id; |
105 glGetIntegerv(GL_TEXTURE_BINDING_EXTERNAL_OES, &texture_id); | 265 glGetIntegerv(GL_TEXTURE_BINDING_EXTERNAL_OES, &texture_id); |
106 DCHECK(texture_id); | 266 DCHECK(texture_id); |
107 | 267 |
108 // The following code only works if we're being asked to copy into | 268 // The following code only works if we're being asked to copy into |
109 // |texture_id_|. Copying into a different texture is not supported. | 269 // |texture_id_|. Copying into a different texture is not supported. |
110 if (static_cast<unsigned>(texture_id) != texture_id_) | 270 if (static_cast<unsigned>(texture_id) != texture_id_) |
111 return false; | 271 return false; |
112 | 272 |
113 if (has_pending_frame_) { | 273 DoUpdateTexImage(); |
114 scoped_ptr<ui::ScopedMakeCurrent> scoped_make_current; | |
115 bool needs_make_current = | |
116 !owner_stub_->decoder()->GetGLContext()->IsCurrent(NULL); | |
117 // On Android we should not have to perform a real context switch here when | |
118 // using virtual contexts. | |
119 DCHECK(!needs_make_current || !owner_stub_->decoder() | |
120 ->GetContextGroup() | |
121 ->feature_info() | |
122 ->workarounds() | |
123 .use_virtualized_gl_contexts); | |
124 if (needs_make_current) { | |
125 scoped_make_current.reset(new ui::ScopedMakeCurrent( | |
126 owner_stub_->decoder()->GetGLContext(), owner_stub_->surface())); | |
127 } | |
128 surface_texture_->UpdateTexImage(); | |
129 has_valid_frame_ = true; | |
130 has_pending_frame_ = false; | |
131 if (scoped_make_current.get()) { | |
132 // UpdateTexImage() implies glBindTexture(). | |
133 // The cmd decoder takes care of restoring the binding for this GLImage as | |
134 // far as the current context is concerned, but if we temporarily change | |
135 // it, we have to keep the state intact in *that* context also. | |
136 const gpu::gles2::ContextState* state = | |
137 owner_stub_->decoder()->GetContextState(); | |
138 const gpu::gles2::TextureUnit& active_unit = | |
139 state->texture_units[state->active_texture_unit]; | |
140 glBindTexture(GL_TEXTURE_EXTERNAL_OES, | |
141 active_unit.bound_texture_external_oes.get() | |
142 ? active_unit.bound_texture_external_oes->service_id() | |
143 : 0); | |
144 } | |
145 } | |
146 | 274 |
147 TextureManager* texture_manager = | 275 TextureManager* texture_manager = |
148 owner_stub_->decoder()->GetContextGroup()->texture_manager(); | 276 owner_stub_->decoder()->GetContextGroup()->texture_manager(); |
149 gpu::gles2::Texture* texture = | 277 gpu::gles2::Texture* texture = |
150 texture_manager->GetTextureForServiceId(texture_id_); | 278 texture_manager->GetTextureForServiceId(texture_id_); |
151 if (texture) { | 279 if (texture) { |
152 // By setting image state to UNBOUND instead of COPIED we ensure that | 280 // By setting image state to UNBOUND instead of COPIED we ensure that |
153 // CopyTexImage() is called each time the surface texture is used for | 281 // CopyTexImage() is called each time the surface texture is used for |
154 // drawing. | 282 // drawing. |
155 texture->SetLevelImage(GL_TEXTURE_EXTERNAL_OES, 0, this, | 283 texture->SetLevelImage(GL_TEXTURE_EXTERNAL_OES, 0, this, |
156 gpu::gles2::Texture::UNBOUND); | 284 gpu::gles2::Texture::UNBOUND); |
157 } | 285 } |
158 | 286 |
159 if (has_listener_ && has_valid_frame_) { | 287 UpdateTransformMatrix(); |
288 return true; | |
289 } | |
290 | |
291 void StreamTexture::UpdateTransformMatrix() { | |
reveman
2015/12/23 23:11:49
can we move the contents of this function into DoU
Tobias Sargeant
2015/12/24 01:28:34
Done. matrix_modfied_ didn't need to be added in a
reveman
2015/12/24 02:14:43
Right, seems like we should call UpdateTexImage wh
| |
292 if (has_valid_frame_) { | |
160 float mtx[16]; | 293 float mtx[16]; |
161 surface_texture_->GetTransformMatrix(mtx); | 294 surface_texture_->GetTransformMatrix(mtx); |
162 | 295 |
163 // Only query the matrix once we have bound a valid frame. | 296 // Only query the matrix once we have bound a valid frame. |
164 if (memcmp(current_matrix_, mtx, sizeof(mtx)) != 0) { | 297 if (memcmp(current_matrix_, mtx, sizeof(mtx)) != 0) { |
298 matrix_modified_ = true; | |
165 memcpy(current_matrix_, mtx, sizeof(mtx)); | 299 memcpy(current_matrix_, mtx, sizeof(mtx)); |
300 } | |
166 | 301 |
302 if (has_listener_ && owner_stub_) { | |
167 GpuStreamTextureMsg_MatrixChanged_Params params; | 303 GpuStreamTextureMsg_MatrixChanged_Params params; |
168 memcpy(¶ms.m00, mtx, sizeof(mtx)); | 304 memcpy(¶ms.m00, mtx, sizeof(mtx)); |
169 owner_stub_->channel()->Send( | 305 owner_stub_->channel()->Send( |
170 new GpuStreamTextureMsg_MatrixChanged(route_id_, params)); | 306 new GpuStreamTextureMsg_MatrixChanged(route_id_, params)); |
307 matrix_modified_ = false; | |
171 } | 308 } |
172 } | 309 } |
173 | |
174 return true; | |
175 } | 310 } |
176 | 311 |
177 void StreamTexture::OnFrameAvailable() { | 312 void StreamTexture::OnFrameAvailable() { |
178 has_pending_frame_ = true; | 313 has_pending_frame_ = true; |
179 if (has_listener_ && owner_stub_) { | 314 if (has_listener_ && owner_stub_) { |
180 owner_stub_->channel()->Send( | 315 owner_stub_->channel()->Send( |
181 new GpuStreamTextureMsg_FrameAvailable(route_id_)); | 316 new GpuStreamTextureMsg_FrameAvailable(route_id_)); |
182 } | 317 } |
183 } | 318 } |
184 | 319 |
(...skipping 14 matching lines...) Expand all Loading... | |
199 IPC_MESSAGE_UNHANDLED(handled = false) | 334 IPC_MESSAGE_UNHANDLED(handled = false) |
200 IPC_END_MESSAGE_MAP() | 335 IPC_END_MESSAGE_MAP() |
201 | 336 |
202 DCHECK(handled); | 337 DCHECK(handled); |
203 return handled; | 338 return handled; |
204 } | 339 } |
205 | 340 |
206 void StreamTexture::OnStartListening() { | 341 void StreamTexture::OnStartListening() { |
207 DCHECK(!has_listener_); | 342 DCHECK(!has_listener_); |
208 has_listener_ = true; | 343 has_listener_ = true; |
344 matrix_modified_ = true; | |
209 } | 345 } |
210 | 346 |
211 void StreamTexture::OnEstablishPeer(int32 primary_id, int32 secondary_id) { | 347 void StreamTexture::OnEstablishPeer(int32 primary_id, int32 secondary_id) { |
212 if (!owner_stub_) | 348 if (!owner_stub_) |
213 return; | 349 return; |
214 | 350 |
215 base::ProcessHandle process = owner_stub_->channel()->GetClientPID(); | 351 base::ProcessHandle process = owner_stub_->channel()->GetClientPID(); |
216 | 352 |
217 SurfaceTexturePeer::GetInstance()->EstablishSurfaceTexturePeer( | 353 SurfaceTexturePeer::GetInstance()->EstablishSurfaceTexturePeer( |
218 process, surface_texture_, primary_id, secondary_id); | 354 process, surface_texture_, primary_id, secondary_id); |
219 } | 355 } |
220 | 356 |
221 bool StreamTexture::BindTexImage(unsigned target) { | 357 bool StreamTexture::BindTexImage(unsigned target) { |
222 NOTREACHED(); | 358 NOTREACHED(); |
223 return false; | 359 return false; |
224 } | 360 } |
225 | 361 |
226 void StreamTexture::ReleaseTexImage(unsigned target) { | 362 void StreamTexture::ReleaseTexImage(unsigned target) { |
227 NOTREACHED(); | 363 NOTREACHED(); |
228 } | 364 } |
229 | 365 |
230 bool StreamTexture::CopyTexSubImage(unsigned target, | 366 bool StreamTexture::CopyTexSubImage(unsigned target, |
231 const gfx::Point& offset, | 367 const gfx::Point& offset, |
232 const gfx::Rect& rect) { | 368 const gfx::Rect& rect) { |
369 if (target == GL_TEXTURE_2D) { | |
370 return DoCopyTexture(offset, rect); | |
371 } | |
372 | |
233 return false; | 373 return false; |
234 } | 374 } |
235 | 375 |
236 bool StreamTexture::ScheduleOverlayPlane(gfx::AcceleratedWidget widget, | 376 bool StreamTexture::ScheduleOverlayPlane(gfx::AcceleratedWidget widget, |
237 int z_order, | 377 int z_order, |
238 gfx::OverlayTransform transform, | 378 gfx::OverlayTransform transform, |
239 const gfx::Rect& bounds_rect, | 379 const gfx::Rect& bounds_rect, |
240 const gfx::RectF& crop_rect) { | 380 const gfx::RectF& crop_rect) { |
241 NOTREACHED(); | 381 NOTREACHED(); |
242 return false; | 382 return false; |
243 } | 383 } |
244 | 384 |
245 void StreamTexture::OnMemoryDump(base::trace_event::ProcessMemoryDump* pmd, | 385 void StreamTexture::OnMemoryDump(base::trace_event::ProcessMemoryDump* pmd, |
246 uint64_t process_tracing_id, | 386 uint64_t process_tracing_id, |
247 const std::string& dump_name) { | 387 const std::string& dump_name) { |
248 // TODO(ericrk): Add OnMemoryDump for GLImages. crbug.com/514914 | 388 // TODO(ericrk): Add OnMemoryDump for GLImages. crbug.com/514914 |
249 } | 389 } |
250 | 390 |
251 } // namespace content | 391 } // namespace content |
OLD | NEW |