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()) | |
reveman
2015/12/23 16:05:36
when does this happen and why is it correct to ret
Tobias Sargeant
2015/12/23 20:06:14
This could happen when the command buffer is in th
reveman
2015/12/23 23:11:48
Ok, maybe fine then but I'm somewhat worried that
| |
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 LOG(WARNING) << "XXX " << "first copy init"; | |
reveman
2015/12/23 16:05:36
I assume this is not supposed to land.
Tobias Sargeant
2015/12/23 20:06:14
You're right. Sorry I missed it when I went throug
| |
191 | |
192 glGenFramebuffersEXT(1, &framebuffer_); | |
193 | |
194 // clang-format off | |
195 const char kVertexShader[] = STRINGIZE( | |
196 attribute vec2 a_position; | |
197 varying vec2 v_texCoord; | |
198 uniform mat4 u_xform; | |
199 void main() { | |
200 gl_Position = vec4(a_position.x, a_position.y, 0.0, 1.0); | |
201 vec2 uv_untransformed = (a_position + vec2(1.0, 1.0)) * 0.5; | |
202 v_texCoord = (u_xform * vec4(uv_untransformed, 0.0, 1.0)).xy; | |
203 } | |
204 ); | |
205 const char kFragmentShader[] = | |
206 "#extension GL_OES_EGL_image_external : require\n" STRINGIZE( | |
207 precision mediump float; | |
208 uniform samplerExternalOES a_texture; | |
209 varying vec2 v_texCoord; | |
210 void main() { | |
211 gl_FragColor = texture2D(a_texture, v_texCoord); | |
212 } | |
213 ); | |
214 // clang-format on | |
215 | |
216 vertex_buffer_ = gfx::GLHelper::SetupQuadVertexBuffer(); | |
217 vertex_shader_ = gfx::GLHelper::LoadShader(GL_VERTEX_SHADER, kVertexShader); | |
218 fragment_shader_ = | |
219 gfx::GLHelper::LoadShader(GL_FRAGMENT_SHADER, kFragmentShader); | |
220 program_ = gfx::GLHelper::SetupProgram(vertex_shader_, fragment_shader_); | |
221 gfx::ScopedUseProgram use_program(program_); | |
222 int sampler_location = glGetUniformLocation(program_, "a_texture"); | |
223 DCHECK_NE(-1, sampler_location); | |
224 glUniform1i(sampler_location, 0); | |
225 u_xform_loc_ = glGetUniformLocation(program_, "u_xform"); | |
226 DCHECK_NE(-1, u_xform_loc_); | |
227 } | |
228 | |
229 gfx::ScopedActiveTexture active_texture(GL_TEXTURE0); | |
230 // UpdateTexImage() call below will bind the surface texture to | |
231 // TEXTURE_EXTERNAL_OES. This scoped texture binder will restore the current | |
232 // binding before this function returns. | |
233 gfx::ScopedTextureBinder texture_binder(GL_TEXTURE_EXTERNAL_OES, texture_id_); | |
234 | |
235 { | |
236 gfx::ScopedFrameBufferBinder framebuffer_binder(framebuffer_); | |
237 gfx::ScopedViewport viewport(0, 0, size_.width(), size_.height()); | |
238 glFramebufferTexture2DEXT(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, | |
239 GL_TEXTURE_2D, target_texture, 0); | |
240 DCHECK_EQ(static_cast<GLenum>(GL_FRAMEBUFFER_COMPLETE), | |
241 glCheckFramebufferStatusEXT(GL_FRAMEBUFFER)); | |
242 gfx::ScopedUseProgram use_program(program_); | |
243 | |
244 glUniformMatrix4fv(u_xform_loc_, 1, false, current_matrix_); | |
245 gfx::GLHelper::DrawQuad(vertex_buffer_); | |
246 | |
247 // Detach the output texture from the fbo. | |
248 glFramebufferTexture2DEXT(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, | |
249 GL_TEXTURE_2D, 0, 0); | |
250 } | |
251 | |
252 return true; | |
253 } | |
254 | |
97 bool StreamTexture::CopyTexImage(unsigned target) { | 255 bool StreamTexture::CopyTexImage(unsigned target) { |
256 if (target == GL_TEXTURE_2D) { | |
257 return DoCopyTexture(gfx::Point(), gfx::Rect(size_)); | |
258 } | |
259 | |
98 if (target != GL_TEXTURE_EXTERNAL_OES) | 260 if (target != GL_TEXTURE_EXTERNAL_OES) |
reveman
2015/12/23 16:05:36
do we still need this non-TEXTURE_2D code?
Tobias Sargeant
2015/12/23 20:06:14
Do you mean the return false for !2D, !EXTERNAL_OE
reveman
2015/12/23 23:11:49
Acknowledged.
| |
99 return false; | 261 return false; |
100 | 262 |
101 if (!owner_stub_ || !surface_texture_.get()) | 263 if (!owner_stub_ || !surface_texture_.get()) |
102 return true; | 264 return true; |
103 | 265 |
104 GLint texture_id; | 266 GLint texture_id; |
105 glGetIntegerv(GL_TEXTURE_BINDING_EXTERNAL_OES, &texture_id); | 267 glGetIntegerv(GL_TEXTURE_BINDING_EXTERNAL_OES, &texture_id); |
106 DCHECK(texture_id); | 268 DCHECK(texture_id); |
107 | 269 |
108 // The following code only works if we're being asked to copy into | 270 // The following code only works if we're being asked to copy into |
109 // |texture_id_|. Copying into a different texture is not supported. | 271 // |texture_id_|. Copying into a different texture is not supported. |
110 if (static_cast<unsigned>(texture_id) != texture_id_) | 272 if (static_cast<unsigned>(texture_id) != texture_id_) |
111 return false; | 273 return false; |
112 | 274 |
113 if (has_pending_frame_) { | 275 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 | 276 |
147 TextureManager* texture_manager = | 277 TextureManager* texture_manager = |
148 owner_stub_->decoder()->GetContextGroup()->texture_manager(); | 278 owner_stub_->decoder()->GetContextGroup()->texture_manager(); |
149 gpu::gles2::Texture* texture = | 279 gpu::gles2::Texture* texture = |
150 texture_manager->GetTextureForServiceId(texture_id_); | 280 texture_manager->GetTextureForServiceId(texture_id_); |
151 if (texture) { | 281 if (texture) { |
152 // By setting image state to UNBOUND instead of COPIED we ensure that | 282 // By setting image state to UNBOUND instead of COPIED we ensure that |
153 // CopyTexImage() is called each time the surface texture is used for | 283 // CopyTexImage() is called each time the surface texture is used for |
154 // drawing. | 284 // drawing. |
155 texture->SetLevelImage(GL_TEXTURE_EXTERNAL_OES, 0, this, | 285 texture->SetLevelImage(GL_TEXTURE_EXTERNAL_OES, 0, this, |
156 gpu::gles2::Texture::UNBOUND); | 286 gpu::gles2::Texture::UNBOUND); |
157 } | 287 } |
158 | 288 |
159 if (has_listener_ && has_valid_frame_) { | 289 UpdateTransformMatrix(); |
290 return true; | |
291 } | |
292 | |
293 void StreamTexture::UpdateTransformMatrix() { | |
294 if (has_valid_frame_) { | |
160 float mtx[16]; | 295 float mtx[16]; |
161 surface_texture_->GetTransformMatrix(mtx); | 296 surface_texture_->GetTransformMatrix(mtx); |
162 | 297 |
163 // Only query the matrix once we have bound a valid frame. | 298 // Only query the matrix once we have bound a valid frame. |
164 if (memcmp(current_matrix_, mtx, sizeof(mtx)) != 0) { | 299 if (memcmp(current_matrix_, mtx, sizeof(mtx)) != 0) { |
300 matrix_modified_ = true; | |
165 memcpy(current_matrix_, mtx, sizeof(mtx)); | 301 memcpy(current_matrix_, mtx, sizeof(mtx)); |
302 } | |
166 | 303 |
304 if (has_listener_ && owner_stub_) { | |
167 GpuStreamTextureMsg_MatrixChanged_Params params; | 305 GpuStreamTextureMsg_MatrixChanged_Params params; |
168 memcpy(¶ms.m00, mtx, sizeof(mtx)); | 306 memcpy(¶ms.m00, mtx, sizeof(mtx)); |
169 owner_stub_->channel()->Send( | 307 owner_stub_->channel()->Send( |
170 new GpuStreamTextureMsg_MatrixChanged(route_id_, params)); | 308 new GpuStreamTextureMsg_MatrixChanged(route_id_, params)); |
309 matrix_modified_ = false; | |
171 } | 310 } |
172 } | 311 } |
173 | |
174 return true; | |
175 } | 312 } |
176 | 313 |
177 void StreamTexture::OnFrameAvailable() { | 314 void StreamTexture::OnFrameAvailable() { |
178 has_pending_frame_ = true; | 315 has_pending_frame_ = true; |
179 if (has_listener_ && owner_stub_) { | 316 if (has_listener_ && owner_stub_) { |
180 owner_stub_->channel()->Send( | 317 owner_stub_->channel()->Send( |
181 new GpuStreamTextureMsg_FrameAvailable(route_id_)); | 318 new GpuStreamTextureMsg_FrameAvailable(route_id_)); |
182 } | 319 } |
183 } | 320 } |
184 | 321 |
(...skipping 14 matching lines...) Expand all Loading... | |
199 IPC_MESSAGE_UNHANDLED(handled = false) | 336 IPC_MESSAGE_UNHANDLED(handled = false) |
200 IPC_END_MESSAGE_MAP() | 337 IPC_END_MESSAGE_MAP() |
201 | 338 |
202 DCHECK(handled); | 339 DCHECK(handled); |
203 return handled; | 340 return handled; |
204 } | 341 } |
205 | 342 |
206 void StreamTexture::OnStartListening() { | 343 void StreamTexture::OnStartListening() { |
207 DCHECK(!has_listener_); | 344 DCHECK(!has_listener_); |
208 has_listener_ = true; | 345 has_listener_ = true; |
346 matrix_modified_ = true; | |
209 } | 347 } |
210 | 348 |
211 void StreamTexture::OnEstablishPeer(int32 primary_id, int32 secondary_id) { | 349 void StreamTexture::OnEstablishPeer(int32 primary_id, int32 secondary_id) { |
212 if (!owner_stub_) | 350 if (!owner_stub_) |
213 return; | 351 return; |
214 | 352 |
215 base::ProcessHandle process = owner_stub_->channel()->GetClientPID(); | 353 base::ProcessHandle process = owner_stub_->channel()->GetClientPID(); |
216 | 354 |
217 SurfaceTexturePeer::GetInstance()->EstablishSurfaceTexturePeer( | 355 SurfaceTexturePeer::GetInstance()->EstablishSurfaceTexturePeer( |
218 process, surface_texture_, primary_id, secondary_id); | 356 process, surface_texture_, primary_id, secondary_id); |
219 } | 357 } |
220 | 358 |
221 bool StreamTexture::BindTexImage(unsigned target) { | 359 bool StreamTexture::BindTexImage(unsigned target) { |
222 NOTREACHED(); | 360 NOTREACHED(); |
223 return false; | 361 return false; |
224 } | 362 } |
225 | 363 |
226 void StreamTexture::ReleaseTexImage(unsigned target) { | 364 void StreamTexture::ReleaseTexImage(unsigned target) { |
227 NOTREACHED(); | 365 NOTREACHED(); |
228 } | 366 } |
229 | 367 |
230 bool StreamTexture::CopyTexSubImage(unsigned target, | 368 bool StreamTexture::CopyTexSubImage(unsigned target, |
231 const gfx::Point& offset, | 369 const gfx::Point& offset, |
232 const gfx::Rect& rect) { | 370 const gfx::Rect& rect) { |
371 if (target == GL_TEXTURE_2D) { | |
372 return DoCopyTexture(offset, rect); | |
373 } | |
374 | |
233 return false; | 375 return false; |
234 } | 376 } |
235 | 377 |
236 bool StreamTexture::ScheduleOverlayPlane(gfx::AcceleratedWidget widget, | 378 bool StreamTexture::ScheduleOverlayPlane(gfx::AcceleratedWidget widget, |
237 int z_order, | 379 int z_order, |
238 gfx::OverlayTransform transform, | 380 gfx::OverlayTransform transform, |
239 const gfx::Rect& bounds_rect, | 381 const gfx::Rect& bounds_rect, |
240 const gfx::RectF& crop_rect) { | 382 const gfx::RectF& crop_rect) { |
241 NOTREACHED(); | 383 NOTREACHED(); |
242 return false; | 384 return false; |
243 } | 385 } |
244 | 386 |
245 void StreamTexture::OnMemoryDump(base::trace_event::ProcessMemoryDump* pmd, | 387 void StreamTexture::OnMemoryDump(base::trace_event::ProcessMemoryDump* pmd, |
246 uint64_t process_tracing_id, | 388 uint64_t process_tracing_id, |
247 const std::string& dump_name) { | 389 const std::string& dump_name) { |
248 // TODO(ericrk): Add OnMemoryDump for GLImages. crbug.com/514914 | 390 // TODO(ericrk): Add OnMemoryDump for GLImages. crbug.com/514914 |
249 } | 391 } |
250 | 392 |
251 } // namespace content | 393 } // namespace content |
OLD | NEW |