OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 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/renderer/media/renderer_gpu_video_accelerator_factories.h" | 5 #include "content/renderer/media/renderer_gpu_video_accelerator_factories.h" |
6 | 6 |
7 #include <GLES2/gl2.h> | 7 #include <GLES2/gl2.h> |
8 #include <GLES2/gl2ext.h> | 8 #include <GLES2/gl2ext.h> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "content/child/child_thread.h" | 11 #include "content/child/child_thread.h" |
12 #include "content/common/gpu/client/context_provider_command_buffer.h" | 12 #include "content/common/gpu/client/context_provider_command_buffer.h" |
| 13 #include "content/common/gpu/client/gl_helper.h" |
13 #include "content/common/gpu/client/gpu_channel_host.h" | 14 #include "content/common/gpu/client/gpu_channel_host.h" |
14 #include "content/common/gpu/client/webgraphicscontext3d_command_buffer_impl.h" | 15 #include "content/common/gpu/client/webgraphicscontext3d_command_buffer_impl.h" |
15 #include "content/renderer/render_thread_impl.h" | 16 #include "content/renderer/render_thread_impl.h" |
16 #include "gpu/command_buffer/client/gles2_implementation.h" | 17 #include "gpu/command_buffer/client/gles2_implementation.h" |
17 #include "media/video/video_decode_accelerator.h" | 18 #include "media/video/video_decode_accelerator.h" |
18 #include "media/video/video_encode_accelerator.h" | 19 #include "media/video/video_encode_accelerator.h" |
19 #include "third_party/skia/include/core/SkBitmap.h" | 20 #include "third_party/skia/include/core/SkBitmap.h" |
20 #include "third_party/skia/include/core/SkPixelRef.h" | 21 #include "third_party/skia/include/core/SkPixelRef.h" |
21 | 22 |
22 namespace content { | 23 namespace content { |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
57 } | 58 } |
58 | 59 |
59 WebGraphicsContext3DCommandBufferImpl* | 60 WebGraphicsContext3DCommandBufferImpl* |
60 RendererGpuVideoAcceleratorFactories::GetContext3d() { | 61 RendererGpuVideoAcceleratorFactories::GetContext3d() { |
61 DCHECK(task_runner_->BelongsToCurrentThread()); | 62 DCHECK(task_runner_->BelongsToCurrentThread()); |
62 if (!context_provider_.get()) | 63 if (!context_provider_.get()) |
63 return NULL; | 64 return NULL; |
64 if (context_provider_->IsContextLost()) { | 65 if (context_provider_->IsContextLost()) { |
65 context_provider_->VerifyContexts(); | 66 context_provider_->VerifyContexts(); |
66 context_provider_ = NULL; | 67 context_provider_ = NULL; |
| 68 gl_helper_.reset(NULL); |
67 return NULL; | 69 return NULL; |
68 } | 70 } |
69 return context_provider_->WebContext3D(); | 71 return context_provider_->WebContext3D(); |
70 } | 72 } |
71 | 73 |
| 74 GLHelper* RendererGpuVideoAcceleratorFactories::GetGLHelper() { |
| 75 if (!GetContext3d()) |
| 76 return NULL; |
| 77 |
| 78 if (gl_helper_.get() == NULL) { |
| 79 gl_helper_.reset(new GLHelper(GetContext3d()->GetImplementation(), |
| 80 GetContext3d()->GetContextSupport())); |
| 81 } |
| 82 |
| 83 return gl_helper_.get(); |
| 84 } |
| 85 |
72 scoped_ptr<media::VideoDecodeAccelerator> | 86 scoped_ptr<media::VideoDecodeAccelerator> |
73 RendererGpuVideoAcceleratorFactories::CreateVideoDecodeAccelerator() { | 87 RendererGpuVideoAcceleratorFactories::CreateVideoDecodeAccelerator() { |
74 DCHECK(task_runner_->BelongsToCurrentThread()); | 88 DCHECK(task_runner_->BelongsToCurrentThread()); |
75 | 89 |
76 WebGraphicsContext3DCommandBufferImpl* context = GetContext3d(); | 90 WebGraphicsContext3DCommandBufferImpl* context = GetContext3d(); |
77 if (context && context->GetCommandBufferProxy()) { | 91 if (context && context->GetCommandBufferProxy()) { |
78 return gpu_channel_host_->CreateVideoDecoder( | 92 return gpu_channel_host_->CreateVideoDecoder( |
79 context->GetCommandBufferProxy()->GetRouteID()); | 93 context->GetCommandBufferProxy()->GetRouteID()); |
80 } | 94 } |
81 | 95 |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
171 // flush the command buffers to ensure that. | 185 // flush the command buffers to ensure that. |
172 gles2->ShallowFlushCHROMIUM(); | 186 gles2->ShallowFlushCHROMIUM(); |
173 } | 187 } |
174 | 188 |
175 void RendererGpuVideoAcceleratorFactories::ReadPixels( | 189 void RendererGpuVideoAcceleratorFactories::ReadPixels( |
176 uint32 texture_id, | 190 uint32 texture_id, |
177 const gfx::Rect& visible_rect, | 191 const gfx::Rect& visible_rect, |
178 const SkBitmap& pixels) { | 192 const SkBitmap& pixels) { |
179 DCHECK(task_runner_->BelongsToCurrentThread()); | 193 DCHECK(task_runner_->BelongsToCurrentThread()); |
180 | 194 |
| 195 GLHelper* gl_helper = GetGLHelper(); |
181 WebGraphicsContext3DCommandBufferImpl* context = GetContext3d(); | 196 WebGraphicsContext3DCommandBufferImpl* context = GetContext3d(); |
182 if (!context) | 197 |
| 198 if (!gl_helper || !context) |
183 return; | 199 return; |
184 | 200 |
185 gpu::gles2::GLES2Implementation* gles2 = context->GetImplementation(); | 201 // Copy texture from texture_id to tmp_texture as texture might be external |
186 | 202 // (GL_TEXTURE_EXTERNAL_OES) |
187 GLuint tmp_texture; | 203 GLuint tmp_texture; |
188 gles2->GenTextures(1, &tmp_texture); | 204 tmp_texture = gl_helper->CreateTexture(); |
189 gles2->BindTexture(GL_TEXTURE_2D, tmp_texture); | |
190 gles2->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); | |
191 gles2->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); | |
192 gles2->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); | |
193 gles2->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); | |
194 context->copyTextureCHROMIUM( | 205 context->copyTextureCHROMIUM( |
195 GL_TEXTURE_2D, texture_id, tmp_texture, 0, GL_RGBA, GL_UNSIGNED_BYTE); | 206 GL_TEXTURE_2D, texture_id, tmp_texture, 0, GL_RGBA, GL_UNSIGNED_BYTE); |
196 | 207 |
197 GLuint fb; | 208 unsigned char* pixel_data = |
198 gles2->GenFramebuffers(1, &fb); | 209 static_cast<unsigned char*>(pixels.pixelRef()->pixels()); |
199 gles2->BindFramebuffer(GL_FRAMEBUFFER, fb); | |
200 gles2->FramebufferTexture2D( | |
201 GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, tmp_texture, 0); | |
202 gles2->PixelStorei(GL_PACK_ALIGNMENT, 4); | |
203 | 210 |
204 #if SK_B32_SHIFT == 0 && SK_G32_SHIFT == 8 && SK_R32_SHIFT == 16 && \ | 211 if (gl_helper->IsReadbackConfigSupported(pixels.colorType())) { |
205 SK_A32_SHIFT == 24 | 212 gl_helper->ReadbackTextureSync( |
206 GLenum skia_format = GL_BGRA_EXT; | 213 tmp_texture, visible_rect, pixel_data, pixels.colorType()); |
207 GLenum read_format = GL_BGRA_EXT; | 214 } else if (pixels.colorType() == kN32_SkColorType) { |
208 GLint supported_format = 0; | 215 gl_helper->ReadbackTextureSync( |
209 GLint supported_type = 0; | 216 tmp_texture, visible_rect, pixel_data, kRGBA_8888_SkColorType); |
210 gles2->GetIntegerv(GL_IMPLEMENTATION_COLOR_READ_FORMAT, &supported_format); | |
211 gles2->GetIntegerv(GL_IMPLEMENTATION_COLOR_READ_TYPE, &supported_type); | |
212 if (supported_format != GL_BGRA_EXT || supported_type != GL_UNSIGNED_BYTE) { | |
213 read_format = GL_RGBA; | |
214 } | |
215 #elif SK_R32_SHIFT == 0 && SK_G32_SHIFT == 8 && SK_B32_SHIFT == 16 && \ | |
216 SK_A32_SHIFT == 24 | |
217 GLenum skia_format = GL_RGBA; | |
218 GLenum read_format = GL_RGBA; | |
219 #else | |
220 #error Unexpected Skia ARGB_8888 layout! | |
221 #endif | |
222 gles2->ReadPixels(visible_rect.x(), | |
223 visible_rect.y(), | |
224 visible_rect.width(), | |
225 visible_rect.height(), | |
226 read_format, | |
227 GL_UNSIGNED_BYTE, | |
228 pixels.pixelRef()->pixels()); | |
229 gles2->DeleteFramebuffers(1, &fb); | |
230 gles2->DeleteTextures(1, &tmp_texture); | |
231 | 217 |
232 if (skia_format != read_format) { | |
233 DCHECK(read_format == GL_RGBA); | |
234 int pixel_count = visible_rect.width() * visible_rect.height(); | 218 int pixel_count = visible_rect.width() * visible_rect.height(); |
235 uint32_t* pixels_ptr = static_cast<uint32_t*>(pixels.pixelRef()->pixels()); | 219 uint32_t* pixels_ptr = static_cast<uint32_t*>(pixels.pixelRef()->pixels()); |
236 for (int i = 0; i < pixel_count; ++i) { | 220 for (int i = 0; i < pixel_count; ++i) { |
237 uint32_t r = pixels_ptr[i] & 0xFF; | 221 uint32_t r = pixels_ptr[i] & 0xFF; |
238 uint32_t g = (pixels_ptr[i] >> 8) & 0xFF; | 222 uint32_t g = (pixels_ptr[i] >> 8) & 0xFF; |
239 uint32_t b = (pixels_ptr[i] >> 16) & 0xFF; | 223 uint32_t b = (pixels_ptr[i] >> 16) & 0xFF; |
240 uint32_t a = (pixels_ptr[i] >> 24) & 0xFF; | 224 uint32_t a = (pixels_ptr[i] >> 24) & 0xFF; |
241 pixels_ptr[i] = (r << SK_R32_SHIFT) | | 225 pixels_ptr[i] = (r << SK_R32_SHIFT) | |
242 (g << SK_G32_SHIFT) | | 226 (g << SK_G32_SHIFT) | |
243 (b << SK_B32_SHIFT) | | 227 (b << SK_B32_SHIFT) | |
244 (a << SK_A32_SHIFT); | 228 (a << SK_A32_SHIFT); |
245 } | 229 } |
| 230 } else { |
| 231 NOTREACHED(); |
246 } | 232 } |
247 | 233 |
248 DCHECK_EQ(gles2->GetError(), static_cast<GLenum>(GL_NO_ERROR)); | 234 gl_helper->DeleteTexture(tmp_texture); |
249 } | 235 } |
250 | 236 |
251 base::SharedMemory* RendererGpuVideoAcceleratorFactories::CreateSharedMemory( | 237 base::SharedMemory* RendererGpuVideoAcceleratorFactories::CreateSharedMemory( |
252 size_t size) { | 238 size_t size) { |
253 DCHECK(task_runner_->BelongsToCurrentThread()); | 239 DCHECK(task_runner_->BelongsToCurrentThread()); |
254 return ChildThread::AllocateSharedMemory(size, thread_safe_sender_.get()); | 240 return ChildThread::AllocateSharedMemory(size, thread_safe_sender_.get()); |
255 } | 241 } |
256 | 242 |
257 scoped_refptr<base::SingleThreadTaskRunner> | 243 scoped_refptr<base::SingleThreadTaskRunner> |
258 RendererGpuVideoAcceleratorFactories::GetTaskRunner() { | 244 RendererGpuVideoAcceleratorFactories::GetTaskRunner() { |
259 return task_runner_; | 245 return task_runner_; |
260 } | 246 } |
261 | 247 |
262 } // namespace content | 248 } // namespace content |
OLD | NEW |