Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(49)

Side by Side Diff: content/renderer/media/renderer_gpu_video_accelerator_factories.cc

Issue 456823002: Use GLHelper for glReadPixels and format detection (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « content/renderer/media/renderer_gpu_video_accelerator_factories.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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"
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
56 context_provider_ = NULL; 56 context_provider_ = NULL;
57 } 57 }
58 58
59 WebGraphicsContext3DCommandBufferImpl* 59 WebGraphicsContext3DCommandBufferImpl*
60 RendererGpuVideoAcceleratorFactories::GetContext3d() { 60 RendererGpuVideoAcceleratorFactories::GetContext3d() {
61 DCHECK(task_runner_->BelongsToCurrentThread()); 61 DCHECK(task_runner_->BelongsToCurrentThread());
62 if (!context_provider_) 62 if (!context_provider_)
63 return NULL; 63 return NULL;
64 if (context_provider_->IsContextLost()) { 64 if (context_provider_->IsContextLost()) {
65 context_provider_->VerifyContexts(); 65 context_provider_->VerifyContexts();
66 context_provider_ = NULL; 66 context_provider_ = NULL;
piman 2014/08/08 21:02:06 nit: it would be good to reset the gl_helper_ here
robert.bradford 2014/08/08 21:32:31 Acknowledged.
67 return NULL; 67 return NULL;
68 } 68 }
69 return context_provider_->WebContext3D(); 69 return context_provider_->WebContext3D();
70 } 70 }
71 71
72 GLHelper* RendererGpuVideoAcceleratorFactories::GetGLHelper() {
73 if (!GetContext3d())
74 return NULL;
75
76 if (gl_helper_.get() == NULL) {
77 gl_helper_.reset(new GLHelper(GetContext3d()->GetImplementation(),
78 GetContext3d()->GetContextSupport()));
79 }
80
81 return gl_helper_.get();
82 }
83
72 scoped_ptr<media::VideoDecodeAccelerator> 84 scoped_ptr<media::VideoDecodeAccelerator>
73 RendererGpuVideoAcceleratorFactories::CreateVideoDecodeAccelerator() { 85 RendererGpuVideoAcceleratorFactories::CreateVideoDecodeAccelerator() {
74 DCHECK(task_runner_->BelongsToCurrentThread()); 86 DCHECK(task_runner_->BelongsToCurrentThread());
75 87
76 WebGraphicsContext3DCommandBufferImpl* context = GetContext3d(); 88 WebGraphicsContext3DCommandBufferImpl* context = GetContext3d();
77 if (context && context->GetCommandBufferProxy()) { 89 if (context && context->GetCommandBufferProxy()) {
78 return gpu_channel_host_->CreateVideoDecoder( 90 return gpu_channel_host_->CreateVideoDecoder(
79 context->GetCommandBufferProxy()->GetRouteID()); 91 context->GetCommandBufferProxy()->GetRouteID());
80 } 92 }
81 93
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after
171 // flush the command buffers to ensure that. 183 // flush the command buffers to ensure that.
172 gles2->ShallowFlushCHROMIUM(); 184 gles2->ShallowFlushCHROMIUM();
173 } 185 }
174 186
175 void RendererGpuVideoAcceleratorFactories::ReadPixels( 187 void RendererGpuVideoAcceleratorFactories::ReadPixels(
176 uint32 texture_id, 188 uint32 texture_id,
177 const gfx::Rect& visible_rect, 189 const gfx::Rect& visible_rect,
178 const SkBitmap& pixels) { 190 const SkBitmap& pixels) {
179 DCHECK(task_runner_->BelongsToCurrentThread()); 191 DCHECK(task_runner_->BelongsToCurrentThread());
180 192
181 WebGraphicsContext3DCommandBufferImpl* context = GetContext3d(); 193 GLHelper* gl_helper = GetGLHelper();
182 if (!context) 194 if (!gl_helper)
183 return; 195 return;
184 196
185 gpu::gles2::GLES2Implementation* gles2 = context->GetImplementation(); 197 unsigned char* pixel_data =
198 static_cast<unsigned char*>(pixels.pixelRef()->pixels());
186 199
187 GLuint tmp_texture; 200 if (gl_helper->IsReadbackConfigSupported(pixels.colorType())) {
188 gles2->GenTextures(1, &tmp_texture); 201 gl_helper->ReadbackTextureSync(
189 gles2->BindTexture(GL_TEXTURE_2D, tmp_texture); 202 texture_id, visible_rect, pixel_data, pixels.colorType());
190 gles2->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 203 } else {
191 gles2->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 204 gl_helper->ReadbackTextureSync(
192 gles2->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); 205 texture_id, visible_rect, pixel_data, kRGBA_8888_SkColorType);
193 gles2->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
194 context->copyTextureCHROMIUM(
195 GL_TEXTURE_2D, texture_id, tmp_texture, 0, GL_RGBA, GL_UNSIGNED_BYTE);
196 206
197 GLuint fb;
198 gles2->GenFramebuffers(1, &fb);
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
204 #if SK_B32_SHIFT == 0 && SK_G32_SHIFT == 8 && SK_R32_SHIFT == 16 && \
205 SK_A32_SHIFT == 24
206 GLenum skia_format = GL_BGRA_EXT;
207 GLenum read_format = GL_BGRA_EXT;
208 GLint supported_format = 0;
209 GLint supported_type = 0;
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
232 if (skia_format != read_format) {
233 DCHECK(read_format == GL_RGBA);
234 int pixel_count = visible_rect.width() * visible_rect.height(); 207 int pixel_count = visible_rect.width() * visible_rect.height();
235 uint32_t* pixels_ptr = static_cast<uint32_t*>(pixels.pixelRef()->pixels()); 208 uint32_t* pixels_ptr = static_cast<uint32_t*>(pixels.pixelRef()->pixels());
236 for (int i = 0; i < pixel_count; ++i) { 209 for (int i = 0; i < pixel_count; ++i) {
237 uint32_t r = pixels_ptr[i] & 0xFF; 210 uint32_t r = pixels_ptr[i] & 0xFF;
238 uint32_t g = (pixels_ptr[i] >> 8) & 0xFF; 211 uint32_t g = (pixels_ptr[i] >> 8) & 0xFF;
239 uint32_t b = (pixels_ptr[i] >> 16) & 0xFF; 212 uint32_t b = (pixels_ptr[i] >> 16) & 0xFF;
240 uint32_t a = (pixels_ptr[i] >> 24) & 0xFF; 213 uint32_t a = (pixels_ptr[i] >> 24) & 0xFF;
241 pixels_ptr[i] = (r << SK_R32_SHIFT) | 214 pixels_ptr[i] = (b << 0) | (g << 8) | (r << 16) | (a << 24);
piman 2014/08/08 21:02:06 nit: why not keep the SK_*_SHIFT here? That way it
robert.bradford 2014/08/08 21:32:31 Those macros, AFAICT, represent the native represe
piman 2014/08/08 21:57:12 How do we know that? We can check that the format
242 (g << SK_G32_SHIFT) |
243 (b << SK_B32_SHIFT) |
244 (a << SK_A32_SHIFT);
245 } 215 }
246 } 216 }
247
248 DCHECK_EQ(gles2->GetError(), static_cast<GLenum>(GL_NO_ERROR));
249 } 217 }
250 218
251 base::SharedMemory* RendererGpuVideoAcceleratorFactories::CreateSharedMemory( 219 base::SharedMemory* RendererGpuVideoAcceleratorFactories::CreateSharedMemory(
252 size_t size) { 220 size_t size) {
253 DCHECK(task_runner_->BelongsToCurrentThread()); 221 DCHECK(task_runner_->BelongsToCurrentThread());
254 return ChildThread::AllocateSharedMemory(size, thread_safe_sender_.get()); 222 return ChildThread::AllocateSharedMemory(size, thread_safe_sender_.get());
255 } 223 }
256 224
257 scoped_refptr<base::SingleThreadTaskRunner> 225 scoped_refptr<base::SingleThreadTaskRunner>
258 RendererGpuVideoAcceleratorFactories::GetTaskRunner() { 226 RendererGpuVideoAcceleratorFactories::GetTaskRunner() {
259 return task_runner_; 227 return task_runner_;
260 } 228 }
261 229
262 } // namespace content 230 } // namespace content
OLDNEW
« no previous file with comments | « content/renderer/media/renderer_gpu_video_accelerator_factories.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698