Revert of [Chromecast] Ignore interfaces not used to connect to internet. (patchset...
[chromium-blink-merge.git] / content / common / gpu / gpu_memory_buffer_factory_io_surface.cc
blob1d4eeedcfbd93d7d87ef6f7a8ce3444c17e199ca
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"
12 namespace content {
13 namespace {
15 void AddBooleanValue(CFMutableDictionaryRef dictionary,
16 const CFStringRef key,
17 bool value) {
18 CFDictionaryAddValue(
19 dictionary, key, value ? kCFBooleanTrue : kCFBooleanFalse);
22 void AddIntegerValue(CFMutableDictionaryRef dictionary,
23 const CFStringRef key,
24 int32 value) {
25 base::ScopedCFTypeRef<CFNumberRef> number(
26 CFNumberCreate(NULL, kCFNumberSInt32Type, &value));
27 CFDictionaryAddValue(dictionary, key, number.get());
30 int32 BytesPerPixel(gfx::GpuMemoryBuffer::Format format) {
31 switch (format) {
32 case gfx::GpuMemoryBuffer::R_8:
33 return 1;
34 case gfx::GpuMemoryBuffer::BGRA_8888:
35 return 4;
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:
44 NOTREACHED();
45 return 0;
48 NOTREACHED();
49 return 0;
52 int32 PixelFormat(gfx::GpuMemoryBuffer::Format format) {
53 switch (format) {
54 case gfx::GpuMemoryBuffer::R_8:
55 return 'L008';
56 case gfx::GpuMemoryBuffer::BGRA_8888:
57 return 'BGRA';
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:
66 NOTREACHED();
67 return 0;
70 NOTREACHED();
71 return 0;
74 const GpuMemoryBufferFactory::Configuration kSupportedConfigurations[] = {
75 {gfx::GpuMemoryBuffer::R_8, gfx::GpuMemoryBuffer::MAP},
76 {gfx::GpuMemoryBuffer::BGRA_8888, gfx::GpuMemoryBuffer::MAP}};
78 } // namespace
80 GpuMemoryBufferFactoryIOSurface::GpuMemoryBufferFactoryIOSurface() {
83 GpuMemoryBufferFactoryIOSurface::~GpuMemoryBufferFactoryIOSurface() {
86 // static
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)
92 return true;
95 return false;
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,
111 int client_id,
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));
127 if (!io_surface)
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;
140 handle.id = id;
141 handle.io_surface_id = IOSurfaceGetID(io_surface);
142 return handle;
145 void GpuMemoryBufferFactoryIOSurface::DestroyGpuMemoryBuffer(
146 gfx::GpuMemoryBufferId id,
147 int client_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() {
156 return this;
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,
165 int client_id) {
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>();
179 return image;
182 } // namespace content