1 // Copyright 2014 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/common/gpu/gpu_memory_buffer_factory.h"
7 #include "base/logging.h"
8 #include "gpu/command_buffer/service/image_factory.h"
9 #include "ui/gl/gl_image.h"
10 #include "ui/gl/gl_image_shared_memory.h"
11 #include "ui/ozone/gpu/gpu_memory_buffer_factory_ozone_native_buffer.h"
16 class GpuMemoryBufferFactoryImpl
: public GpuMemoryBufferFactory
,
17 public gpu::ImageFactory
{
19 // Overridden from GpuMemoryBufferFactory:
20 virtual gfx::GpuMemoryBufferHandle
CreateGpuMemoryBuffer(
21 gfx::GpuMemoryBufferType type
,
22 gfx::GpuMemoryBufferId id
,
23 const gfx::Size
& size
,
24 gfx::GpuMemoryBuffer::Format format
,
25 gfx::GpuMemoryBuffer::Usage usage
,
26 int client_id
) override
{
28 case gfx::OZONE_NATIVE_BUFFER
: {
29 if (!ozone_buffer_factory_
.CreateGpuMemoryBuffer(
30 id
, size
, format
, usage
, client_id
)) {
31 return gfx::GpuMemoryBufferHandle();
33 gfx::GpuMemoryBufferHandle handle
;
34 handle
.type
= gfx::OZONE_NATIVE_BUFFER
;
40 return gfx::GpuMemoryBufferHandle();
43 virtual void DestroyGpuMemoryBuffer(gfx::GpuMemoryBufferType type
,
44 gfx::GpuMemoryBufferId id
,
45 int client_id
) override
{
47 case gfx::OZONE_NATIVE_BUFFER
:
48 ozone_buffer_factory_
.DestroyGpuMemoryBuffer(id
, client_id
);
55 virtual gpu::ImageFactory
* AsImageFactory() override
{ return this; }
57 // Overridden from gpu::ImageFactory:
58 virtual scoped_refptr
<gfx::GLImage
> CreateImageForGpuMemoryBuffer(
59 const gfx::GpuMemoryBufferHandle
& handle
,
60 const gfx::Size
& size
,
61 gfx::GpuMemoryBuffer::Format format
,
62 unsigned internalformat
,
63 int client_id
) override
{
64 switch (handle
.type
) {
65 case gfx::SHARED_MEMORY_BUFFER
: {
66 scoped_refptr
<gfx::GLImageSharedMemory
> image(
67 new gfx::GLImageSharedMemory(size
, internalformat
));
68 if (!image
->Initialize(handle
, format
))
73 case gfx::OZONE_NATIVE_BUFFER
:
74 return ozone_buffer_factory_
.CreateImageForGpuMemoryBuffer(
75 handle
.id
, size
, format
, internalformat
, client_id
);
78 return scoped_refptr
<gfx::GLImage
>();
83 ui::GpuMemoryBufferFactoryOzoneNativeBuffer ozone_buffer_factory_
;
89 scoped_ptr
<GpuMemoryBufferFactory
> GpuMemoryBufferFactory::Create() {
90 return make_scoped_ptr
<GpuMemoryBufferFactory
>(
91 new GpuMemoryBufferFactoryImpl
);
94 } // namespace content