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 // The surface texture matrix provided by the android framework is intended to | |
95 // be used in a y-up coordinate system, whereas Chromium expects y-down. | |
96 // Therefore we need to invert that transformation here. | |
97 surface_texture_->GetTransformMatrix(xform); | |
98 // This is equivalent to post-multiplication by | |
99 // [ 1 0 0 0 ] | |
100 // [ 0 1 0 -1 ] | |
101 // [ 0 0 1 0 ] | |
102 // [ 0 0 0 1 ] | |
103 xform[13] += xform[5]; | |
liberato (no reviews please)
2016/02/29 22:33:32
i'm not sure how this works for both AVDA and Stre
Tobias Sargeant
2016/02/29 23:57:50
I tested both paths, corresponding to producing an
| |
104 xform[5] = -xform[5]; | |
105 } | |
106 | |
93 void StreamTexture::OnWillDestroyStub() { | 107 void StreamTexture::OnWillDestroyStub() { |
94 owner_stub_->RemoveDestructionObserver(this); | 108 owner_stub_->RemoveDestructionObserver(this); |
95 owner_stub_->channel()->RemoveRoute(route_id_); | 109 owner_stub_->channel()->RemoveRoute(route_id_); |
96 | 110 |
97 if (framebuffer_) { | 111 if (framebuffer_) { |
98 scoped_ptr<ui::ScopedMakeCurrent> scoped_make_current(MakeStubCurrent()); | 112 scoped_ptr<ui::ScopedMakeCurrent> scoped_make_current(MakeStubCurrent()); |
99 | 113 |
100 glDeleteProgram(program_); | 114 glDeleteProgram(program_); |
101 glDeleteShader(vertex_shader_); | 115 glDeleteShader(vertex_shader_); |
102 glDeleteShader(fragment_shader_); | 116 glDeleteShader(fragment_shader_); |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
135 void StreamTexture::UpdateTexImage() { | 149 void StreamTexture::UpdateTexImage() { |
136 DCHECK(surface_texture_.get()); | 150 DCHECK(surface_texture_.get()); |
137 DCHECK(owner_stub_); | 151 DCHECK(owner_stub_); |
138 | 152 |
139 if (!has_pending_frame_) return; | 153 if (!has_pending_frame_) return; |
140 | 154 |
141 scoped_ptr<ui::ScopedMakeCurrent> scoped_make_current(MakeStubCurrent()); | 155 scoped_ptr<ui::ScopedMakeCurrent> scoped_make_current(MakeStubCurrent()); |
142 | 156 |
143 surface_texture_->UpdateTexImage(); | 157 surface_texture_->UpdateTexImage(); |
144 | 158 |
145 has_valid_frame_ = true; | |
146 has_pending_frame_ = false; | 159 has_pending_frame_ = false; |
147 | 160 |
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()) { | 161 if (scoped_make_current.get()) { |
163 // UpdateTexImage() implies glBindTexture(). | 162 // UpdateTexImage() implies glBindTexture(). |
164 // The cmd decoder takes care of restoring the binding for this GLImage as | 163 // 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 | 164 // 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. | 165 // it, we have to keep the state intact in *that* context also. |
167 const gpu::gles2::ContextState* state = | 166 const gpu::gles2::ContextState* state = |
168 owner_stub_->decoder()->GetContextState(); | 167 owner_stub_->decoder()->GetContextState(); |
169 const gpu::gles2::TextureUnit& active_unit = | 168 const gpu::gles2::TextureUnit& active_unit = |
170 state->texture_units[state->active_texture_unit]; | 169 state->texture_units[state->active_texture_unit]; |
171 glBindTexture(GL_TEXTURE_EXTERNAL_OES, | 170 glBindTexture(GL_TEXTURE_EXTERNAL_OES, |
172 active_unit.bound_texture_external_oes.get() | 171 active_unit.bound_texture_external_oes.get() |
173 ? active_unit.bound_texture_external_oes->service_id() | 172 ? active_unit.bound_texture_external_oes->service_id() |
174 : 0); | 173 : 0); |
175 } | 174 } |
176 } | 175 } |
177 | 176 |
178 bool StreamTexture::CopyTexImage(unsigned target) { | 177 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) | 178 if (target != GL_TEXTURE_EXTERNAL_OES) |
186 return false; | 179 return false; |
187 | 180 |
188 if (!owner_stub_ || !surface_texture_.get()) | 181 if (!owner_stub_ || !surface_texture_.get()) |
189 return true; | 182 return true; |
190 | 183 |
191 GLint texture_id; | 184 GLint texture_id; |
192 glGetIntegerv(GL_TEXTURE_BINDING_EXTERNAL_OES, &texture_id); | 185 glGetIntegerv(GL_TEXTURE_BINDING_EXTERNAL_OES, &texture_id); |
193 DCHECK(texture_id); | 186 DCHECK(texture_id); |
194 | 187 |
195 // The following code only works if we're being asked to copy into | 188 // The following code only works if we're being asked to copy into |
196 // |texture_id_|. Copying into a different texture is not supported. | 189 // |texture_id_|. Copying into a different texture is not supported. |
197 if (static_cast<unsigned>(texture_id) != texture_id_) | 190 if (static_cast<unsigned>(texture_id) != texture_id_) |
198 return false; | 191 return false; |
199 | 192 |
200 UpdateTexImage(); | 193 UpdateTexImage(); |
201 | 194 |
202 TextureManager* texture_manager = | 195 TextureManager* texture_manager = |
203 owner_stub_->decoder()->GetContextGroup()->texture_manager(); | 196 owner_stub_->decoder()->GetContextGroup()->texture_manager(); |
204 gpu::gles2::Texture* texture = | 197 gpu::gles2::Texture* texture = |
205 texture_manager->GetTextureForServiceId(texture_id_); | 198 texture_manager->GetTextureForServiceId(texture_id_); |
206 if (texture) { | 199 if (texture) { |
207 // By setting image state to UNBOUND instead of COPIED we ensure that | 200 // By setting image state to UNBOUND instead of COPIED we ensure that |
208 // CopyTexImage() is called each time the surface texture is used for | 201 // CopyTexImage() is called each time the surface texture is used for |
209 // drawing. | 202 // drawing. |
210 texture->SetLevelImage(GL_TEXTURE_EXTERNAL_OES, 0, this, | 203 texture->SetLevelStreamTextureImage(GL_TEXTURE_EXTERNAL_OES, 0, this, |
211 gpu::gles2::Texture::UNBOUND); | 204 gpu::gles2::Texture::UNBOUND); |
212 } | 205 } |
213 | 206 |
214 return true; | 207 return true; |
215 } | 208 } |
216 | 209 |
217 void StreamTexture::OnFrameAvailable() { | 210 void StreamTexture::OnFrameAvailable() { |
218 has_pending_frame_ = true; | 211 has_pending_frame_ = true; |
219 if (has_listener_ && owner_stub_) { | 212 if (has_listener_ && owner_stub_) { |
220 owner_stub_->channel()->Send( | 213 owner_stub_->channel()->Send( |
221 new GpuStreamTextureMsg_FrameAvailable(route_id_)); | 214 new GpuStreamTextureMsg_FrameAvailable(route_id_)); |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
263 return false; | 256 return false; |
264 } | 257 } |
265 | 258 |
266 void StreamTexture::ReleaseTexImage(unsigned target) { | 259 void StreamTexture::ReleaseTexImage(unsigned target) { |
267 NOTREACHED(); | 260 NOTREACHED(); |
268 } | 261 } |
269 | 262 |
270 bool StreamTexture::CopyTexSubImage(unsigned target, | 263 bool StreamTexture::CopyTexSubImage(unsigned target, |
271 const gfx::Point& offset, | 264 const gfx::Point& offset, |
272 const gfx::Rect& rect) { | 265 const gfx::Rect& rect) { |
273 if (target != GL_TEXTURE_2D) | 266 NOTREACHED(); |
274 return false; | 267 return false; |
275 | |
276 if (!owner_stub_ || !surface_texture_.get()) | |
277 return true; | |
278 | |
279 if (!offset.IsOrigin()) { | |
280 LOG(ERROR) << "Non-origin offset is not supported"; | |
281 return false; | |
282 } | |
283 | |
284 if (rect != gfx::Rect(size_)) { | |
285 LOG(ERROR) << "Sub-rectangle is not supported"; | |
286 return false; | |
287 } | |
288 | |
289 GLint target_texture = 0; | |
290 glGetIntegerv(GL_TEXTURE_BINDING_2D, &target_texture); | |
291 DCHECK(target_texture); | |
292 | |
293 UpdateTexImage(); | |
294 | |
295 if (!framebuffer_) { | |
296 glGenFramebuffersEXT(1, &framebuffer_); | |
297 | |
298 // This vertex shader introduces a y flip before applying the stream | |
299 // texture matrix. This is required because the stream texture matrix | |
300 // Android provides is intended to be used in a y-up coordinate system, | |
301 // whereas Chromium expects y-down. | |
302 | |
303 // clang-format off | |
304 const char kVertexShader[] = STRINGIZE( | |
305 attribute vec2 a_position; | |
306 varying vec2 v_texCoord; | |
307 uniform mat4 u_xform; | |
308 void main() { | |
309 gl_Position = vec4(a_position.x, a_position.y, 0.0, 1.0); | |
310 vec2 uv_untransformed = a_position * vec2(0.5, -0.5) + vec2(0.5, 0.5); | |
311 v_texCoord = (u_xform * vec4(uv_untransformed, 0.0, 1.0)).xy; | |
312 } | |
313 ); | |
314 const char kFragmentShader[] = | |
315 "#extension GL_OES_EGL_image_external : require\n" STRINGIZE( | |
316 precision mediump float; | |
317 uniform samplerExternalOES a_texture; | |
318 varying vec2 v_texCoord; | |
319 void main() { | |
320 gl_FragColor = texture2D(a_texture, v_texCoord); | |
321 } | |
322 ); | |
323 // clang-format on | |
324 | |
325 vertex_buffer_ = gfx::GLHelper::SetupQuadVertexBuffer(); | |
326 vertex_shader_ = gfx::GLHelper::LoadShader(GL_VERTEX_SHADER, kVertexShader); | |
327 fragment_shader_ = | |
328 gfx::GLHelper::LoadShader(GL_FRAGMENT_SHADER, kFragmentShader); | |
329 program_ = gfx::GLHelper::SetupProgram(vertex_shader_, fragment_shader_); | |
330 gfx::ScopedUseProgram use_program(program_); | |
331 int sampler_location = glGetUniformLocation(program_, "a_texture"); | |
332 DCHECK_NE(-1, sampler_location); | |
333 glUniform1i(sampler_location, 0); | |
334 u_xform_location_ = glGetUniformLocation(program_, "u_xform"); | |
335 DCHECK_NE(-1, u_xform_location_); | |
336 } | |
337 | |
338 gfx::ScopedActiveTexture active_texture(GL_TEXTURE0); | |
339 // UpdateTexImage() call below will bind the surface texture to | |
340 // TEXTURE_EXTERNAL_OES. This scoped texture binder will restore the current | |
341 // binding before this function returns. | |
342 gfx::ScopedTextureBinder texture_binder(GL_TEXTURE_EXTERNAL_OES, texture_id_); | |
343 | |
344 { | |
345 gfx::ScopedFrameBufferBinder framebuffer_binder(framebuffer_); | |
346 gfx::ScopedViewport viewport(0, 0, size_.width(), size_.height()); | |
347 glFramebufferTexture2DEXT(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, | |
348 GL_TEXTURE_2D, target_texture, 0); | |
349 DCHECK_EQ(static_cast<GLenum>(GL_FRAMEBUFFER_COMPLETE), | |
350 glCheckFramebufferStatusEXT(GL_FRAMEBUFFER)); | |
351 gfx::ScopedUseProgram use_program(program_); | |
352 | |
353 glUniformMatrix4fv(u_xform_location_, 1, false, current_matrix_); | |
354 gfx::GLHelper::DrawQuad(vertex_buffer_); | |
355 | |
356 // Detach the output texture from the fbo. | |
357 glFramebufferTexture2DEXT(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, | |
358 GL_TEXTURE_2D, 0, 0); | |
359 } | |
360 return true; | |
361 } | 268 } |
362 | 269 |
363 bool StreamTexture::ScheduleOverlayPlane(gfx::AcceleratedWidget widget, | 270 bool StreamTexture::ScheduleOverlayPlane(gfx::AcceleratedWidget widget, |
364 int z_order, | 271 int z_order, |
365 gfx::OverlayTransform transform, | 272 gfx::OverlayTransform transform, |
366 const gfx::Rect& bounds_rect, | 273 const gfx::Rect& bounds_rect, |
367 const gfx::RectF& crop_rect) { | 274 const gfx::RectF& crop_rect) { |
368 NOTREACHED(); | 275 NOTREACHED(); |
369 return false; | 276 return false; |
370 } | 277 } |
371 | 278 |
372 void StreamTexture::OnMemoryDump(base::trace_event::ProcessMemoryDump* pmd, | 279 void StreamTexture::OnMemoryDump(base::trace_event::ProcessMemoryDump* pmd, |
373 uint64_t process_tracing_id, | 280 uint64_t process_tracing_id, |
374 const std::string& dump_name) { | 281 const std::string& dump_name) { |
375 // TODO(ericrk): Add OnMemoryDump for GLImages. crbug.com/514914 | 282 // TODO(ericrk): Add OnMemoryDump for GLImages. crbug.com/514914 |
376 } | 283 } |
377 | 284 |
378 } // namespace content | 285 } // namespace content |
OLD | NEW |