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
3 // found in the LICENSE file.
5 #include "content/renderer/media/renderer_gpu_video_accelerator_factories.h"
8 #include <GLES2/gl2ext.h>
10 #include "base/bind.h"
11 #include "content/child/child_gpu_memory_buffer_manager.h"
12 #include "content/child/child_thread_impl.h"
13 #include "content/common/gpu/client/context_provider_command_buffer.h"
14 #include "content/common/gpu/client/gl_helper.h"
15 #include "content/common/gpu/client/gpu_channel_host.h"
16 #include "content/common/gpu/client/webgraphicscontext3d_command_buffer_impl.h"
17 #include "content/common/gpu/media/gpu_video_accelerator_util.h"
18 #include "content/renderer/render_thread_impl.h"
19 #include "gpu/command_buffer/client/gles2_implementation.h"
20 #include "gpu/command_buffer/client/gpu_memory_buffer_manager.h"
21 #include "media/video/video_decode_accelerator.h"
22 #include "media/video/video_encode_accelerator.h"
27 scoped_refptr
<RendererGpuVideoAcceleratorFactories
>
28 RendererGpuVideoAcceleratorFactories::Create(
29 GpuChannelHost
* gpu_channel_host
,
30 const scoped_refptr
<base::SingleThreadTaskRunner
>& task_runner
,
31 const scoped_refptr
<ContextProviderCommandBuffer
>& context_provider
,
32 unsigned image_texture_target
,
33 bool enable_video_accelerator
) {
34 scoped_refptr
<RendererGpuVideoAcceleratorFactories
> factories
=
35 new RendererGpuVideoAcceleratorFactories(gpu_channel_host
, task_runner
,
38 enable_video_accelerator
);
39 // Post task from outside constructor, since AddRef()/Release() is unsafe from
41 task_runner
->PostTask(
43 base::Bind(&RendererGpuVideoAcceleratorFactories::BindContext
,
48 RendererGpuVideoAcceleratorFactories::RendererGpuVideoAcceleratorFactories(
49 GpuChannelHost
* gpu_channel_host
,
50 const scoped_refptr
<base::SingleThreadTaskRunner
>& task_runner
,
51 const scoped_refptr
<ContextProviderCommandBuffer
>& context_provider
,
52 unsigned image_texture_target
,
53 bool enable_video_accelerator
)
54 : task_runner_(task_runner
),
55 gpu_channel_host_(gpu_channel_host
),
56 context_provider_(context_provider
),
57 image_texture_target_(image_texture_target
),
58 video_accelerator_enabled_(enable_video_accelerator
),
59 gpu_memory_buffer_manager_(
60 ChildThreadImpl::current()->gpu_memory_buffer_manager()),
61 thread_safe_sender_(ChildThreadImpl::current()->thread_safe_sender()) {
62 DCHECK(gpu_channel_host_
.get());
65 RendererGpuVideoAcceleratorFactories::~RendererGpuVideoAcceleratorFactories() {}
67 void RendererGpuVideoAcceleratorFactories::BindContext() {
68 DCHECK(task_runner_
->BelongsToCurrentThread());
69 if (!context_provider_
->BindToCurrentThread())
70 context_provider_
= NULL
;
73 WebGraphicsContext3DCommandBufferImpl
*
74 RendererGpuVideoAcceleratorFactories::GetContext3d() {
75 DCHECK(task_runner_
->BelongsToCurrentThread());
76 if (!context_provider_
.get())
78 if (context_provider_
->ContextGL()->GetGraphicsResetStatusKHR() !=
80 context_provider_
->VerifyContexts();
81 context_provider_
= NULL
;
82 gl_helper_
.reset(NULL
);
85 return context_provider_
->WebContext3D();
88 GLHelper
* RendererGpuVideoAcceleratorFactories::GetGLHelper() {
92 if (gl_helper_
.get() == NULL
) {
93 gl_helper_
.reset(new GLHelper(GetContext3d()->GetImplementation(),
94 GetContext3d()->GetContextSupport()));
97 return gl_helper_
.get();
100 bool RendererGpuVideoAcceleratorFactories::IsGpuVideoAcceleratorEnabled() {
101 return video_accelerator_enabled_
;
104 scoped_ptr
<media::VideoDecodeAccelerator
>
105 RendererGpuVideoAcceleratorFactories::CreateVideoDecodeAccelerator() {
106 DCHECK(video_accelerator_enabled_
);
107 DCHECK(task_runner_
->BelongsToCurrentThread());
109 WebGraphicsContext3DCommandBufferImpl
* context
= GetContext3d();
110 if (context
&& context
->GetCommandBufferProxy()) {
111 return gpu_channel_host_
->CreateVideoDecoder(
112 context
->GetCommandBufferProxy()->GetRouteID());
115 return scoped_ptr
<media::VideoDecodeAccelerator
>();
118 scoped_ptr
<media::VideoEncodeAccelerator
>
119 RendererGpuVideoAcceleratorFactories::CreateVideoEncodeAccelerator() {
120 DCHECK(video_accelerator_enabled_
);
121 DCHECK(task_runner_
->BelongsToCurrentThread());
123 WebGraphicsContext3DCommandBufferImpl
* context
= GetContext3d();
124 if (context
&& context
->GetCommandBufferProxy()) {
125 return gpu_channel_host_
->CreateVideoEncoder(
126 context
->GetCommandBufferProxy()->GetRouteID());
129 return scoped_ptr
<media::VideoEncodeAccelerator
>();
132 bool RendererGpuVideoAcceleratorFactories::CreateTextures(
134 const gfx::Size
& size
,
135 std::vector
<uint32
>* texture_ids
,
136 std::vector
<gpu::Mailbox
>* texture_mailboxes
,
137 uint32 texture_target
) {
138 DCHECK(task_runner_
->BelongsToCurrentThread());
139 DCHECK(texture_target
);
141 WebGraphicsContext3DCommandBufferImpl
* context
= GetContext3d();
145 gpu::gles2::GLES2Implementation
* gles2
= context
->GetImplementation();
146 texture_ids
->resize(count
);
147 texture_mailboxes
->resize(count
);
148 gles2
->GenTextures(count
, &texture_ids
->at(0));
149 for (int i
= 0; i
< count
; ++i
) {
150 gles2
->ActiveTexture(GL_TEXTURE0
);
151 uint32 texture_id
= texture_ids
->at(i
);
152 gles2
->BindTexture(texture_target
, texture_id
);
153 gles2
->TexParameteri(texture_target
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR
);
154 gles2
->TexParameteri(texture_target
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
155 gles2
->TexParameteri(texture_target
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
156 gles2
->TexParameteri(texture_target
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
157 if (texture_target
== GL_TEXTURE_2D
) {
158 gles2
->TexImage2D(texture_target
,
168 gles2
->GenMailboxCHROMIUM(texture_mailboxes
->at(i
).name
);
169 gles2
->ProduceTextureCHROMIUM(texture_target
,
170 texture_mailboxes
->at(i
).name
);
173 // We need ShallowFlushCHROMIUM() here to order the command buffer commands
174 // with respect to IPC to the GPU process, to guarantee that the decoder in
175 // the GPU process can use these textures as soon as it receives IPC
176 // notification of them.
177 gles2
->ShallowFlushCHROMIUM();
178 DCHECK_EQ(gles2
->GetError(), static_cast<GLenum
>(GL_NO_ERROR
));
182 void RendererGpuVideoAcceleratorFactories::DeleteTexture(uint32 texture_id
) {
183 DCHECK(task_runner_
->BelongsToCurrentThread());
185 WebGraphicsContext3DCommandBufferImpl
* context
= GetContext3d();
189 gpu::gles2::GLES2Implementation
* gles2
= context
->GetImplementation();
190 gles2
->DeleteTextures(1, &texture_id
);
191 DCHECK_EQ(gles2
->GetError(), static_cast<GLenum
>(GL_NO_ERROR
));
194 void RendererGpuVideoAcceleratorFactories::WaitSyncPoint(uint32 sync_point
) {
195 DCHECK(task_runner_
->BelongsToCurrentThread());
197 WebGraphicsContext3DCommandBufferImpl
* context
= GetContext3d();
201 gpu::gles2::GLES2Implementation
* gles2
= context
->GetImplementation();
202 gles2
->WaitSyncPointCHROMIUM(sync_point
);
204 // Callers expect the WaitSyncPoint to affect the next IPCs. Make sure to
205 // flush the command buffers to ensure that.
206 gles2
->ShallowFlushCHROMIUM();
209 scoped_ptr
<gfx::GpuMemoryBuffer
>
210 RendererGpuVideoAcceleratorFactories::AllocateGpuMemoryBuffer(
211 const gfx::Size
& size
,
212 gfx::GpuMemoryBuffer::Format format
,
213 gfx::GpuMemoryBuffer::Usage usage
) {
214 DCHECK(task_runner_
->BelongsToCurrentThread());
215 scoped_ptr
<gfx::GpuMemoryBuffer
> buffer
=
216 gpu_memory_buffer_manager_
->AllocateGpuMemoryBuffer(size
, format
, usage
);
217 return buffer
.Pass();
220 bool RendererGpuVideoAcceleratorFactories::IsTextureRGSupported() {
221 DCHECK(task_runner_
->BelongsToCurrentThread());
223 WebGraphicsContext3DCommandBufferImpl
* context
= GetContext3d();
226 return context
->GetImplementation()->capabilities().texture_rg
;
229 unsigned RendererGpuVideoAcceleratorFactories::ImageTextureTarget() {
230 return image_texture_target_
;
233 gpu::gles2::GLES2Interface
*
234 RendererGpuVideoAcceleratorFactories::GetGLES2Interface() {
235 DCHECK(task_runner_
->BelongsToCurrentThread());
237 WebGraphicsContext3DCommandBufferImpl
* context
= GetContext3d();
240 gpu::gles2::GLES2Implementation
* gles2
= context
->GetImplementation();
244 scoped_ptr
<base::SharedMemory
>
245 RendererGpuVideoAcceleratorFactories::CreateSharedMemory(size_t size
) {
246 DCHECK(task_runner_
->BelongsToCurrentThread());
247 scoped_ptr
<base::SharedMemory
> mem(
248 ChildThreadImpl::AllocateSharedMemory(size
, thread_safe_sender_
.get()));
249 if (mem
&& !mem
->Map(size
))
254 scoped_refptr
<base::SingleThreadTaskRunner
>
255 RendererGpuVideoAcceleratorFactories::GetTaskRunner() {
259 media::VideoDecodeAccelerator::SupportedProfiles
260 RendererGpuVideoAcceleratorFactories::
261 GetVideoDecodeAcceleratorSupportedProfiles() {
262 return GpuVideoAcceleratorUtil::ConvertGpuToMediaDecodeProfiles(
263 gpu_channel_host_
->gpu_info()
264 .video_decode_accelerator_supported_profiles
);
267 media::VideoEncodeAccelerator::SupportedProfiles
268 RendererGpuVideoAcceleratorFactories::
269 GetVideoEncodeAcceleratorSupportedProfiles() {
270 return GpuVideoAcceleratorUtil::ConvertGpuToMediaEncodeProfiles(
271 gpu_channel_host_
->gpu_info()
272 .video_encode_accelerator_supported_profiles
);
275 } // namespace content