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_io_surface.h"
7 #include <CoreFoundation/CoreFoundation.h>
9 #include "base/logging.h"
10 #include "ui/gl/gl_image_io_surface.h"
15 void AddBooleanValue(CFMutableDictionaryRef dictionary
,
16 const CFStringRef key
,
19 dictionary
, key
, value
? kCFBooleanTrue
: kCFBooleanFalse
);
22 void AddIntegerValue(CFMutableDictionaryRef dictionary
,
23 const CFStringRef key
,
25 base::ScopedCFTypeRef
<CFNumberRef
> number(
26 CFNumberCreate(NULL
, kCFNumberSInt32Type
, &value
));
27 CFDictionaryAddValue(dictionary
, key
, number
.get());
30 int32
BytesPerPixel(gfx::GpuMemoryBuffer::Format format
) {
32 case gfx::GpuMemoryBuffer::BGRA_8888
:
34 case gfx::GpuMemoryBuffer::RGBA_8888
:
35 case gfx::GpuMemoryBuffer::RGBX_8888
:
44 int32
PixelFormat(gfx::GpuMemoryBuffer::Format format
) {
46 case gfx::GpuMemoryBuffer::BGRA_8888
:
48 case gfx::GpuMemoryBuffer::RGBA_8888
:
49 case gfx::GpuMemoryBuffer::RGBX_8888
:
58 const GpuMemoryBufferFactory::Configuration kSupportedConfigurations
[] = {
59 { gfx::GpuMemoryBuffer::BGRA_8888
, gfx::GpuMemoryBuffer::MAP
}
64 GpuMemoryBufferFactoryIOSurface::GpuMemoryBufferFactoryIOSurface() {
67 GpuMemoryBufferFactoryIOSurface::~GpuMemoryBufferFactoryIOSurface() {
71 bool GpuMemoryBufferFactoryIOSurface::IsGpuMemoryBufferConfigurationSupported(
72 gfx::GpuMemoryBuffer::Format format
,
73 gfx::GpuMemoryBuffer::Usage usage
) {
74 for (auto& configuration
: kSupportedConfigurations
) {
75 if (configuration
.format
== format
&& configuration
.usage
== usage
)
82 void GpuMemoryBufferFactoryIOSurface::GetSupportedGpuMemoryBufferConfigurations(
83 std::vector
<Configuration
>* configurations
) {
84 configurations
->assign(
85 kSupportedConfigurations
,
86 kSupportedConfigurations
+ arraysize(kSupportedConfigurations
));
89 gfx::GpuMemoryBufferHandle
90 GpuMemoryBufferFactoryIOSurface::CreateGpuMemoryBuffer(
91 gfx::GpuMemoryBufferId id
,
92 const gfx::Size
& size
,
93 gfx::GpuMemoryBuffer::Format format
,
94 gfx::GpuMemoryBuffer::Usage usage
,
96 gfx::PluginWindowHandle surface_handle
) {
97 base::ScopedCFTypeRef
<CFMutableDictionaryRef
> properties
;
98 properties
.reset(CFDictionaryCreateMutable(kCFAllocatorDefault
,
100 &kCFTypeDictionaryKeyCallBacks
,
101 &kCFTypeDictionaryValueCallBacks
));
102 AddIntegerValue(properties
, kIOSurfaceWidth
, size
.width());
103 AddIntegerValue(properties
, kIOSurfaceHeight
, size
.height());
104 AddIntegerValue(properties
, kIOSurfaceBytesPerElement
, BytesPerPixel(format
));
105 AddIntegerValue(properties
, kIOSurfacePixelFormat
, PixelFormat(format
));
106 // TODO(reveman): Remove this when using a mach_port_t to transfer
107 // IOSurface to browser and renderer process. crbug.com/323304
108 AddBooleanValue(properties
, kIOSurfaceIsGlobal
, true);
110 base::ScopedCFTypeRef
<IOSurfaceRef
> io_surface(IOSurfaceCreate(properties
));
112 return gfx::GpuMemoryBufferHandle();
115 base::AutoLock
lock(io_surfaces_lock_
);
117 IOSurfaceMapKey
key(id
, client_id
);
118 DCHECK(io_surfaces_
.find(key
) == io_surfaces_
.end());
119 io_surfaces_
[key
] = io_surface
;
122 gfx::GpuMemoryBufferHandle handle
;
123 handle
.type
= gfx::IO_SURFACE_BUFFER
;
125 handle
.io_surface_id
= IOSurfaceGetID(io_surface
);
129 void GpuMemoryBufferFactoryIOSurface::DestroyGpuMemoryBuffer(
130 gfx::GpuMemoryBufferId id
,
132 base::AutoLock
lock(io_surfaces_lock_
);
134 IOSurfaceMapKey
key(id
, client_id
);
135 IOSurfaceMap::iterator it
= io_surfaces_
.find(key
);
136 if (it
!= io_surfaces_
.end())
137 io_surfaces_
.erase(it
);
140 gpu::ImageFactory
* GpuMemoryBufferFactoryIOSurface::AsImageFactory() {
144 scoped_refptr
<gfx::GLImage
>
145 GpuMemoryBufferFactoryIOSurface::CreateImageForGpuMemoryBuffer(
146 const gfx::GpuMemoryBufferHandle
& handle
,
147 const gfx::Size
& size
,
148 gfx::GpuMemoryBuffer::Format format
,
149 unsigned internalformat
,
151 base::AutoLock
lock(io_surfaces_lock_
);
153 DCHECK_EQ(handle
.type
, gfx::IO_SURFACE_BUFFER
);
154 IOSurfaceMapKey
key(handle
.id
, client_id
);
155 IOSurfaceMap::iterator it
= io_surfaces_
.find(key
);
156 if (it
== io_surfaces_
.end())
157 return scoped_refptr
<gfx::GLImage
>();
159 scoped_refptr
<gfx::GLImageIOSurface
> image(new gfx::GLImageIOSurface(size
));
160 if (!image
->Initialize(it
->second
.get()))
161 return scoped_refptr
<gfx::GLImage
>();
166 } // namespace content