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 "ui/gl/gl_image.h"
9 #include "ui/gl/gl_image_shared_memory.h"
10 #include "ui/ozone/gpu/gpu_memory_buffer_factory_ozone_native_buffer.h"
15 class GpuMemoryBufferFactoryImpl
: public GpuMemoryBufferFactory
{
17 // Overridden from GpuMemoryBufferFactory:
18 virtual gfx::GpuMemoryBufferHandle
CreateGpuMemoryBuffer(
19 const gfx::GpuMemoryBufferHandle
& handle
,
20 const gfx::Size
& size
,
21 unsigned internalformat
,
22 unsigned usage
) OVERRIDE
{
23 switch (handle
.type
) {
24 case gfx::OZONE_NATIVE_BUFFER
:
25 return ozone_buffer_factory
.CreateGpuMemoryBuffer(
26 handle
.global_id
, size
, internalformat
, usage
)
28 : gfx::GpuMemoryBufferHandle();
31 return gfx::GpuMemoryBufferHandle();
34 virtual void DestroyGpuMemoryBuffer(
35 const gfx::GpuMemoryBufferHandle
& handle
) OVERRIDE
{
36 switch (handle
.type
) {
37 case gfx::OZONE_NATIVE_BUFFER
:
38 ozone_buffer_factory
.DestroyGpuMemoryBuffer(handle
.global_id
);
45 virtual scoped_refptr
<gfx::GLImage
> CreateImageForGpuMemoryBuffer(
46 const gfx::GpuMemoryBufferHandle
& handle
,
47 const gfx::Size
& size
,
48 unsigned internalformat
,
49 int client_id
) OVERRIDE
{
50 switch (handle
.type
) {
51 case gfx::SHARED_MEMORY_BUFFER
: {
52 scoped_refptr
<gfx::GLImageSharedMemory
> image(
53 new gfx::GLImageSharedMemory(size
, internalformat
));
54 if (!image
->Initialize(handle
))
59 case gfx::OZONE_NATIVE_BUFFER
:
60 // Verify that client is the owner of the buffer we're about to use.
61 if (handle
.global_id
.secondary_id
!= client_id
)
62 return scoped_refptr
<gfx::GLImage
>();
64 return ozone_buffer_factory
.CreateImageForGpuMemoryBuffer(
65 handle
.global_id
, size
, internalformat
);
68 return scoped_refptr
<gfx::GLImage
>();
73 ui::GpuMemoryBufferFactoryOzoneNativeBuffer ozone_buffer_factory
;
79 scoped_ptr
<GpuMemoryBufferFactory
> GpuMemoryBufferFactory::Create() {
80 return make_scoped_ptr
<GpuMemoryBufferFactory
>(
81 new GpuMemoryBufferFactoryImpl
);
84 } // namespace content