OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "content/common/gpu/stream_texture_android.h" | |
6 | |
7 #include <string.h> | |
8 | |
9 #include "base/bind.h" | |
10 #include "base/strings/stringize_macros.h" | |
11 #include "content/common/gpu/gpu_channel.h" | |
12 #include "gpu/command_buffer/service/context_group.h" | |
13 #include "gpu/command_buffer/service/context_state.h" | |
14 #include "gpu/command_buffer/service/gles2_cmd_decoder.h" | |
15 #include "gpu/command_buffer/service/texture_manager.h" | |
16 #include "gpu/ipc/common/android/surface_texture_peer.h" | |
17 #include "gpu/ipc/common/gpu_messages.h" | |
18 #include "ui/gfx/geometry/size.h" | |
19 #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" | |
23 | |
24 namespace content { | |
25 | |
26 using gpu::gles2::ContextGroup; | |
27 using gpu::gles2::GLES2Decoder; | |
28 using gpu::gles2::TextureManager; | |
29 using gpu::gles2::TextureRef; | |
30 | |
31 // static | |
32 bool StreamTexture::Create(GpuCommandBufferStub* owner_stub, | |
33 uint32_t client_texture_id, | |
34 int stream_id) { | |
35 GLES2Decoder* decoder = owner_stub->decoder(); | |
36 TextureManager* texture_manager = | |
37 decoder->GetContextGroup()->texture_manager(); | |
38 TextureRef* texture = texture_manager->GetTexture(client_texture_id); | |
39 | |
40 if (texture && (!texture->texture()->target() || | |
41 texture->texture()->target() == GL_TEXTURE_EXTERNAL_OES)) { | |
42 | |
43 // TODO: Ideally a valid image id was returned to the client so that | |
44 // it could then call glBindTexImage2D() for doing the following. | |
45 scoped_refptr<gl::GLImage> gl_image( | |
46 new StreamTexture(owner_stub, stream_id, texture->service_id())); | |
47 gfx::Size size = gl_image->GetSize(); | |
48 texture_manager->SetTarget(texture, GL_TEXTURE_EXTERNAL_OES); | |
49 texture_manager->SetLevelInfo(texture, GL_TEXTURE_EXTERNAL_OES, 0, GL_RGBA, | |
50 size.width(), size.height(), 1, 0, GL_RGBA, | |
51 GL_UNSIGNED_BYTE, gfx::Rect(size)); | |
52 texture_manager->SetLevelImage(texture, GL_TEXTURE_EXTERNAL_OES, 0, | |
53 gl_image.get(), | |
54 gpu::gles2::Texture::UNBOUND); | |
55 return true; | |
56 } | |
57 | |
58 return false; | |
59 } | |
60 | |
61 StreamTexture::StreamTexture(GpuCommandBufferStub* owner_stub, | |
62 int32_t route_id, | |
63 uint32_t texture_id) | |
64 : surface_texture_(gfx::SurfaceTexture::Create(texture_id)), | |
65 size_(0, 0), | |
66 has_valid_frame_(false), | |
67 has_pending_frame_(false), | |
68 owner_stub_(owner_stub), | |
69 route_id_(route_id), | |
70 has_listener_(false), | |
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_location_(-1), | |
78 weak_factory_(this) { | |
79 owner_stub->AddDestructionObserver(this); | |
80 memset(current_matrix_, 0, sizeof(current_matrix_)); | |
81 owner_stub->channel()->AddRoute(route_id, owner_stub->stream_id(), this); | |
82 surface_texture_->SetFrameAvailableCallback(base::Bind( | |
83 &StreamTexture::OnFrameAvailable, weak_factory_.GetWeakPtr())); | |
84 } | |
85 | |
86 StreamTexture::~StreamTexture() { | |
87 if (owner_stub_) { | |
88 owner_stub_->RemoveDestructionObserver(this); | |
89 owner_stub_->channel()->RemoveRoute(route_id_); | |
90 } | |
91 } | |
92 | |
93 void StreamTexture::OnWillDestroyStub() { | |
94 owner_stub_->RemoveDestructionObserver(this); | |
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_location_ = -1; | |
111 } | |
112 | |
113 owner_stub_ = NULL; | |
114 | |
115 // 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. | |
117 surface_texture_ = NULL; | |
118 } | |
119 | |
120 void StreamTexture::Destroy(bool have_context) { | |
121 NOTREACHED(); | |
122 } | |
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 if (needs_make_current) { | |
129 scoped_make_current.reset(new ui::ScopedMakeCurrent( | |
130 owner_stub_->decoder()->GetGLContext(), owner_stub_->surface())); | |
131 } | |
132 return scoped_make_current; | |
133 } | |
134 | |
135 void StreamTexture::UpdateTexImage() { | |
136 DCHECK(surface_texture_.get()); | |
137 DCHECK(owner_stub_); | |
138 | |
139 if (!has_pending_frame_) return; | |
140 | |
141 scoped_ptr<ui::ScopedMakeCurrent> scoped_make_current(MakeStubCurrent()); | |
142 | |
143 surface_texture_->UpdateTexImage(); | |
144 | |
145 has_valid_frame_ = true; | |
146 has_pending_frame_ = false; | |
147 | |
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()) { | |
163 // UpdateTexImage() implies glBindTexture(). | |
164 // 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 | |
166 // it, we have to keep the state intact in *that* context also. | |
167 const gpu::gles2::ContextState* state = | |
168 owner_stub_->decoder()->GetContextState(); | |
169 const gpu::gles2::TextureUnit& active_unit = | |
170 state->texture_units[state->active_texture_unit]; | |
171 glBindTexture(GL_TEXTURE_EXTERNAL_OES, | |
172 active_unit.bound_texture_external_oes.get() | |
173 ? active_unit.bound_texture_external_oes->service_id() | |
174 : 0); | |
175 } | |
176 } | |
177 | |
178 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) | |
186 return false; | |
187 | |
188 if (!owner_stub_ || !surface_texture_.get()) | |
189 return true; | |
190 | |
191 GLint texture_id; | |
192 glGetIntegerv(GL_TEXTURE_BINDING_EXTERNAL_OES, &texture_id); | |
193 | |
194 // The following code only works if we're being asked to copy into | |
195 // |texture_id_|. Copying into a different texture is not supported. | |
196 // 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 | |
198 // zero and we assume that it is properly bound to |texture_id_|. | |
199 if (texture_id > 0 && static_cast<unsigned>(texture_id) != texture_id_) | |
200 return false; | |
201 | |
202 UpdateTexImage(); | |
203 | |
204 TextureManager* texture_manager = | |
205 owner_stub_->decoder()->GetContextGroup()->texture_manager(); | |
206 gpu::gles2::Texture* texture = | |
207 texture_manager->GetTextureForServiceId(texture_id_); | |
208 if (texture) { | |
209 // By setting image state to UNBOUND instead of COPIED we ensure that | |
210 // CopyTexImage() is called each time the surface texture is used for | |
211 // drawing. | |
212 texture->SetLevelImage(GL_TEXTURE_EXTERNAL_OES, 0, this, | |
213 gpu::gles2::Texture::UNBOUND); | |
214 } | |
215 | |
216 return true; | |
217 } | |
218 | |
219 void StreamTexture::OnFrameAvailable() { | |
220 has_pending_frame_ = true; | |
221 if (has_listener_ && owner_stub_) { | |
222 owner_stub_->channel()->Send( | |
223 new GpuStreamTextureMsg_FrameAvailable(route_id_)); | |
224 } | |
225 } | |
226 | |
227 gfx::Size StreamTexture::GetSize() { | |
228 return size_; | |
229 } | |
230 | |
231 unsigned StreamTexture::GetInternalFormat() { | |
232 return GL_RGBA; | |
233 } | |
234 | |
235 bool StreamTexture::OnMessageReceived(const IPC::Message& message) { | |
236 bool handled = true; | |
237 IPC_BEGIN_MESSAGE_MAP(StreamTexture, message) | |
238 IPC_MESSAGE_HANDLER(GpuStreamTextureMsg_StartListening, OnStartListening) | |
239 IPC_MESSAGE_HANDLER(GpuStreamTextureMsg_EstablishPeer, OnEstablishPeer) | |
240 IPC_MESSAGE_HANDLER(GpuStreamTextureMsg_SetSize, OnSetSize) | |
241 IPC_MESSAGE_UNHANDLED(handled = false) | |
242 IPC_END_MESSAGE_MAP() | |
243 | |
244 DCHECK(handled); | |
245 return handled; | |
246 } | |
247 | |
248 void StreamTexture::OnStartListening() { | |
249 DCHECK(!has_listener_); | |
250 has_listener_ = true; | |
251 } | |
252 | |
253 void StreamTexture::OnEstablishPeer(int32_t primary_id, int32_t secondary_id) { | |
254 if (!owner_stub_) | |
255 return; | |
256 | |
257 base::ProcessHandle process = owner_stub_->channel()->GetClientPID(); | |
258 | |
259 gpu::SurfaceTexturePeer::GetInstance()->EstablishSurfaceTexturePeer( | |
260 process, surface_texture_, primary_id, secondary_id); | |
261 } | |
262 | |
263 bool StreamTexture::BindTexImage(unsigned target) { | |
264 NOTREACHED(); | |
265 return false; | |
266 } | |
267 | |
268 void StreamTexture::ReleaseTexImage(unsigned target) { | |
269 NOTREACHED(); | |
270 } | |
271 | |
272 bool StreamTexture::CopyTexSubImage(unsigned target, | |
273 const gfx::Point& offset, | |
274 const gfx::Rect& rect) { | |
275 if (target != GL_TEXTURE_2D) | |
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 } | |
364 | |
365 bool StreamTexture::ScheduleOverlayPlane(gfx::AcceleratedWidget widget, | |
366 int z_order, | |
367 gfx::OverlayTransform transform, | |
368 const gfx::Rect& bounds_rect, | |
369 const gfx::RectF& crop_rect) { | |
370 NOTREACHED(); | |
371 return false; | |
372 } | |
373 | |
374 void StreamTexture::OnMemoryDump(base::trace_event::ProcessMemoryDump* pmd, | |
375 uint64_t process_tracing_id, | |
376 const std::string& dump_name) { | |
377 // TODO(ericrk): Add OnMemoryDump for GLImages. crbug.com/514914 | |
378 } | |
379 | |
380 } // namespace content | |
OLD | NEW |