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>
11 #include "base/logging.h"
12 #include "content/common/mac/io_surface_manager.h"
13 #include "ui/gl/gl_image_io_surface.h"
18 void AddIntegerValue(CFMutableDictionaryRef dictionary
,
19 const CFStringRef key
,
21 base::ScopedCFTypeRef
<CFNumberRef
> number(
22 CFNumberCreate(NULL
, kCFNumberSInt32Type
, &value
));
23 CFDictionaryAddValue(dictionary
, key
, number
.get());
26 int32
BytesPerPixel(gfx::GpuMemoryBuffer::Format format
) {
28 case gfx::GpuMemoryBuffer::R_8
:
30 case gfx::GpuMemoryBuffer::BGRA_8888
:
32 case gfx::GpuMemoryBuffer::ATC
:
33 case gfx::GpuMemoryBuffer::ATCIA
:
34 case gfx::GpuMemoryBuffer::DXT1
:
35 case gfx::GpuMemoryBuffer::DXT5
:
36 case gfx::GpuMemoryBuffer::ETC1
:
37 case gfx::GpuMemoryBuffer::RGBA_4444
:
38 case gfx::GpuMemoryBuffer::RGBA_8888
:
39 case gfx::GpuMemoryBuffer::RGBX_8888
:
40 case gfx::GpuMemoryBuffer::YUV_420
:
49 int32
PixelFormat(gfx::GpuMemoryBuffer::Format format
) {
51 case gfx::GpuMemoryBuffer::R_8
:
53 case gfx::GpuMemoryBuffer::BGRA_8888
:
55 case gfx::GpuMemoryBuffer::ATC
:
56 case gfx::GpuMemoryBuffer::ATCIA
:
57 case gfx::GpuMemoryBuffer::DXT1
:
58 case gfx::GpuMemoryBuffer::DXT5
:
59 case gfx::GpuMemoryBuffer::ETC1
:
60 case gfx::GpuMemoryBuffer::RGBA_4444
:
61 case gfx::GpuMemoryBuffer::RGBA_8888
:
62 case gfx::GpuMemoryBuffer::RGBX_8888
:
63 case gfx::GpuMemoryBuffer::YUV_420
:
72 const GpuMemoryBufferFactory::Configuration kSupportedConfigurations
[] = {
73 {gfx::GpuMemoryBuffer::R_8
, gfx::GpuMemoryBuffer::PERSISTENT_MAP
},
74 {gfx::GpuMemoryBuffer::R_8
, gfx::GpuMemoryBuffer::MAP
},
75 {gfx::GpuMemoryBuffer::BGRA_8888
, gfx::GpuMemoryBuffer::PERSISTENT_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
));
123 base::ScopedCFTypeRef
<IOSurfaceRef
> io_surface(IOSurfaceCreate(properties
));
125 return gfx::GpuMemoryBufferHandle();
127 if (!IOSurfaceManager::GetInstance()->RegisterIOSurface(id
, client_id
,
129 return gfx::GpuMemoryBufferHandle();
133 base::AutoLock
lock(io_surfaces_lock_
);
135 IOSurfaceMapKey
key(id
, client_id
);
136 DCHECK(io_surfaces_
.find(key
) == io_surfaces_
.end());
137 io_surfaces_
[key
] = io_surface
;
140 gfx::GpuMemoryBufferHandle handle
;
141 handle
.type
= gfx::IO_SURFACE_BUFFER
;
146 void GpuMemoryBufferFactoryIOSurface::DestroyGpuMemoryBuffer(
147 gfx::GpuMemoryBufferId id
,
150 base::AutoLock
lock(io_surfaces_lock_
);
152 IOSurfaceMapKey
key(id
, client_id
);
153 DCHECK(io_surfaces_
.find(key
) != io_surfaces_
.end());
154 io_surfaces_
.erase(key
);
157 IOSurfaceManager::GetInstance()->UnregisterIOSurface(id
, client_id
);
160 gpu::ImageFactory
* GpuMemoryBufferFactoryIOSurface::AsImageFactory() {
164 scoped_refptr
<gfx::GLImage
>
165 GpuMemoryBufferFactoryIOSurface::CreateImageForGpuMemoryBuffer(
166 const gfx::GpuMemoryBufferHandle
& handle
,
167 const gfx::Size
& size
,
168 gfx::GpuMemoryBuffer::Format format
,
169 unsigned internalformat
,
171 base::AutoLock
lock(io_surfaces_lock_
);
173 DCHECK_EQ(handle
.type
, gfx::IO_SURFACE_BUFFER
);
174 IOSurfaceMapKey
key(handle
.id
, client_id
);
175 IOSurfaceMap::iterator it
= io_surfaces_
.find(key
);
176 if (it
== io_surfaces_
.end())
177 return scoped_refptr
<gfx::GLImage
>();
179 scoped_refptr
<gfx::GLImageIOSurface
> image(
180 new gfx::GLImageIOSurface(size
, internalformat
));
181 if (!image
->Initialize(it
->second
.get(), format
))
182 return scoped_refptr
<gfx::GLImage
>();
187 } // namespace content