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 context
->GetCommandBufferProxy()->CreateVideoDecoder();
116 scoped_ptr
<media::VideoEncodeAccelerator
>
117 RendererGpuVideoAcceleratorFactories::CreateVideoEncodeAccelerator() {
118 DCHECK(video_accelerator_enabled_
);
119 DCHECK(task_runner_
->BelongsToCurrentThread());
121 WebGraphicsContext3DCommandBufferImpl
* context
= GetContext3d();
122 if (context
&& context
->GetCommandBufferProxy())
123 return context
->GetCommandBufferProxy()->CreateVideoEncoder();
128 bool RendererGpuVideoAcceleratorFactories::CreateTextures(
130 const gfx::Size
& size
,
131 std::vector
<uint32
>* texture_ids
,
132 std::vector
<gpu::Mailbox
>* texture_mailboxes
,
133 uint32 texture_target
) {
134 DCHECK(task_runner_
->BelongsToCurrentThread());
135 DCHECK(texture_target
);
137 WebGraphicsContext3DCommandBufferImpl
* context
= GetContext3d();
141 gpu::gles2::GLES2Implementation
* gles2
= context
->GetImplementation();
142 texture_ids
->resize(count
);
143 texture_mailboxes
->resize(count
);
144 gles2
->GenTextures(count
, &texture_ids
->at(0));
145 for (int i
= 0; i
< count
; ++i
) {
146 gles2
->ActiveTexture(GL_TEXTURE0
);
147 uint32 texture_id
= texture_ids
->at(i
);
148 gles2
->BindTexture(texture_target
, texture_id
);
149 gles2
->TexParameteri(texture_target
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR
);
150 gles2
->TexParameteri(texture_target
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
151 gles2
->TexParameteri(texture_target
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
152 gles2
->TexParameteri(texture_target
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
153 if (texture_target
== GL_TEXTURE_2D
) {
154 gles2
->TexImage2D(texture_target
,
164 gles2
->GenMailboxCHROMIUM(texture_mailboxes
->at(i
).name
);
165 gles2
->ProduceTextureCHROMIUM(texture_target
,
166 texture_mailboxes
->at(i
).name
);
169 // We need ShallowFlushCHROMIUM() here to order the command buffer commands
170 // with respect to IPC to the GPU process, to guarantee that the decoder in
171 // the GPU process can use these textures as soon as it receives IPC
172 // notification of them.
173 gles2
->ShallowFlushCHROMIUM();
174 DCHECK_EQ(gles2
->GetError(), static_cast<GLenum
>(GL_NO_ERROR
));
178 void RendererGpuVideoAcceleratorFactories::DeleteTexture(uint32 texture_id
) {
179 DCHECK(task_runner_
->BelongsToCurrentThread());
181 WebGraphicsContext3DCommandBufferImpl
* context
= GetContext3d();
185 gpu::gles2::GLES2Implementation
* gles2
= context
->GetImplementation();
186 gles2
->DeleteTextures(1, &texture_id
);
187 DCHECK_EQ(gles2
->GetError(), static_cast<GLenum
>(GL_NO_ERROR
));
190 void RendererGpuVideoAcceleratorFactories::WaitSyncPoint(uint32 sync_point
) {
191 DCHECK(task_runner_
->BelongsToCurrentThread());
193 WebGraphicsContext3DCommandBufferImpl
* context
= GetContext3d();
197 gpu::gles2::GLES2Implementation
* gles2
= context
->GetImplementation();
198 gles2
->WaitSyncPointCHROMIUM(sync_point
);
200 // Callers expect the WaitSyncPoint to affect the next IPCs. Make sure to
201 // flush the command buffers to ensure that.
202 gles2
->ShallowFlushCHROMIUM();
205 scoped_ptr
<gfx::GpuMemoryBuffer
>
206 RendererGpuVideoAcceleratorFactories::AllocateGpuMemoryBuffer(
207 const gfx::Size
& size
,
208 gfx::BufferFormat format
,
209 gfx::BufferUsage usage
) {
210 DCHECK(task_runner_
->BelongsToCurrentThread());
211 scoped_ptr
<gfx::GpuMemoryBuffer
> buffer
=
212 gpu_memory_buffer_manager_
->AllocateGpuMemoryBuffer(size
, format
, usage
);
213 return buffer
.Pass();
216 bool RendererGpuVideoAcceleratorFactories::IsTextureRGSupported() {
217 DCHECK(task_runner_
->BelongsToCurrentThread());
219 WebGraphicsContext3DCommandBufferImpl
* context
= GetContext3d();
222 return context
->GetImplementation()->capabilities().texture_rg
;
225 unsigned RendererGpuVideoAcceleratorFactories::ImageTextureTarget() {
226 return image_texture_target_
;
229 gpu::gles2::GLES2Interface
*
230 RendererGpuVideoAcceleratorFactories::GetGLES2Interface() {
231 DCHECK(task_runner_
->BelongsToCurrentThread());
233 WebGraphicsContext3DCommandBufferImpl
* context
= GetContext3d();
236 gpu::gles2::GLES2Implementation
* gles2
= context
->GetImplementation();
240 scoped_ptr
<base::SharedMemory
>
241 RendererGpuVideoAcceleratorFactories::CreateSharedMemory(size_t size
) {
242 DCHECK(task_runner_
->BelongsToCurrentThread());
243 scoped_ptr
<base::SharedMemory
> mem(
244 ChildThreadImpl::AllocateSharedMemory(size
, thread_safe_sender_
.get()));
245 if (mem
&& !mem
->Map(size
))
250 scoped_refptr
<base::SingleThreadTaskRunner
>
251 RendererGpuVideoAcceleratorFactories::GetTaskRunner() {
255 media::VideoDecodeAccelerator::SupportedProfiles
256 RendererGpuVideoAcceleratorFactories::
257 GetVideoDecodeAcceleratorSupportedProfiles() {
258 return GpuVideoAcceleratorUtil::ConvertGpuToMediaDecodeProfiles(
259 gpu_channel_host_
->gpu_info()
260 .video_decode_accelerator_supported_profiles
);
263 media::VideoEncodeAccelerator::SupportedProfiles
264 RendererGpuVideoAcceleratorFactories::
265 GetVideoEncodeAcceleratorSupportedProfiles() {
266 return GpuVideoAcceleratorUtil::ConvertGpuToMediaEncodeProfiles(
267 gpu_channel_host_
->gpu_info()
268 .video_encode_accelerator_supported_profiles
);
271 } // namespace content