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::R_8
:
34 case gfx::GpuMemoryBuffer::BGRA_8888
:
36 case gfx::GpuMemoryBuffer::ATC
:
37 case gfx::GpuMemoryBuffer::ATCIA
:
38 case gfx::GpuMemoryBuffer::DXT1
:
39 case gfx::GpuMemoryBuffer::DXT5
:
40 case gfx::GpuMemoryBuffer::ETC1
:
41 case gfx::GpuMemoryBuffer::RGBA_8888
:
42 case gfx::GpuMemoryBuffer::RGBX_8888
:
43 case gfx::GpuMemoryBuffer::YUV_420
:
52 int32
PixelFormat(gfx::GpuMemoryBuffer::Format format
) {
54 case gfx::GpuMemoryBuffer::R_8
:
56 case gfx::GpuMemoryBuffer::BGRA_8888
:
58 case gfx::GpuMemoryBuffer::ATC
:
59 case gfx::GpuMemoryBuffer::ATCIA
:
60 case gfx::GpuMemoryBuffer::DXT1
:
61 case gfx::GpuMemoryBuffer::DXT5
:
62 case gfx::GpuMemoryBuffer::ETC1
:
63 case gfx::GpuMemoryBuffer::RGBA_8888
:
64 case gfx::GpuMemoryBuffer::RGBX_8888
:
65 case gfx::GpuMemoryBuffer::YUV_420
:
74 const GpuMemoryBufferFactory::Configuration kSupportedConfigurations
[] = {
75 {gfx::GpuMemoryBuffer::R_8
, gfx::GpuMemoryBuffer::MAP
},
76 {gfx::GpuMemoryBuffer::BGRA_8888
, gfx::GpuMemoryBuffer::MAP
}};
80 GpuMemoryBufferFactoryIOSurface::GpuMemoryBufferFactoryIOSurface() {
83 GpuMemoryBufferFactoryIOSurface::~GpuMemoryBufferFactoryIOSurface() {
87 bool GpuMemoryBufferFactoryIOSurface::IsGpuMemoryBufferConfigurationSupported(
88 gfx::GpuMemoryBuffer::Format format
,
89 gfx::GpuMemoryBuffer::Usage usage
) {
90 for (auto& configuration
: kSupportedConfigurations
) {
91 if (configuration
.format
== format
&& configuration
.usage
== usage
)
98 void GpuMemoryBufferFactoryIOSurface::GetSupportedGpuMemoryBufferConfigurations(
99 std::vector
<Configuration
>* configurations
) {
100 configurations
->assign(
101 kSupportedConfigurations
,
102 kSupportedConfigurations
+ arraysize(kSupportedConfigurations
));
105 gfx::GpuMemoryBufferHandle
106 GpuMemoryBufferFactoryIOSurface::CreateGpuMemoryBuffer(
107 gfx::GpuMemoryBufferId id
,
108 const gfx::Size
& size
,
109 gfx::GpuMemoryBuffer::Format format
,
110 gfx::GpuMemoryBuffer::Usage usage
,
112 gfx::PluginWindowHandle surface_handle
) {
113 base::ScopedCFTypeRef
<CFMutableDictionaryRef
> properties
;
114 properties
.reset(CFDictionaryCreateMutable(kCFAllocatorDefault
,
116 &kCFTypeDictionaryKeyCallBacks
,
117 &kCFTypeDictionaryValueCallBacks
));
118 AddIntegerValue(properties
, kIOSurfaceWidth
, size
.width());
119 AddIntegerValue(properties
, kIOSurfaceHeight
, size
.height());
120 AddIntegerValue(properties
, kIOSurfaceBytesPerElement
, BytesPerPixel(format
));
121 AddIntegerValue(properties
, kIOSurfacePixelFormat
, PixelFormat(format
));
122 // TODO(reveman): Remove this when using a mach_port_t to transfer
123 // IOSurface to browser and renderer process. crbug.com/323304
124 AddBooleanValue(properties
, kIOSurfaceIsGlobal
, true);
126 base::ScopedCFTypeRef
<IOSurfaceRef
> io_surface(IOSurfaceCreate(properties
));
128 return gfx::GpuMemoryBufferHandle();
131 base::AutoLock
lock(io_surfaces_lock_
);
133 IOSurfaceMapKey
key(id
, client_id
);
134 DCHECK(io_surfaces_
.find(key
) == io_surfaces_
.end());
135 io_surfaces_
[key
] = io_surface
;
138 gfx::GpuMemoryBufferHandle handle
;
139 handle
.type
= gfx::IO_SURFACE_BUFFER
;
141 handle
.io_surface_id
= IOSurfaceGetID(io_surface
);
145 void GpuMemoryBufferFactoryIOSurface::DestroyGpuMemoryBuffer(
146 gfx::GpuMemoryBufferId id
,
148 base::AutoLock
lock(io_surfaces_lock_
);
150 IOSurfaceMapKey
key(id
, client_id
);
151 DCHECK(io_surfaces_
.find(key
) != io_surfaces_
.end());
152 io_surfaces_
.erase(key
);
155 gpu::ImageFactory
* GpuMemoryBufferFactoryIOSurface::AsImageFactory() {
159 scoped_refptr
<gfx::GLImage
>
160 GpuMemoryBufferFactoryIOSurface::CreateImageForGpuMemoryBuffer(
161 const gfx::GpuMemoryBufferHandle
& handle
,
162 const gfx::Size
& size
,
163 gfx::GpuMemoryBuffer::Format format
,
164 unsigned internalformat
,
166 base::AutoLock
lock(io_surfaces_lock_
);
168 DCHECK_EQ(handle
.type
, gfx::IO_SURFACE_BUFFER
);
169 IOSurfaceMapKey
key(handle
.id
, client_id
);
170 IOSurfaceMap::iterator it
= io_surfaces_
.find(key
);
171 if (it
== io_surfaces_
.end())
172 return scoped_refptr
<gfx::GLImage
>();
174 scoped_refptr
<gfx::GLImageIOSurface
> image(
175 new gfx::GLImageIOSurface(size
, internalformat
));
176 if (!image
->Initialize(it
->second
.get(), format
))
177 return scoped_refptr
<gfx::GLImage
>();
182 } // namespace content