base: Change DCHECK_IS_ON to a macro DCHECK_IS_ON().
[chromium-blink-merge.git] / content / common / gpu / gpu_memory_buffer_factory_io_surface.cc
blobba00ea2553a2600002baee58b12b5ee9cdb9a4c0
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::BGRA_8888:
33 return 4;
34 case gfx::GpuMemoryBuffer::RGBA_8888:
35 case gfx::GpuMemoryBuffer::RGBX_8888:
36 NOTREACHED();
37 return 0;
40 NOTREACHED();
41 return 0;
44 int32 PixelFormat(gfx::GpuMemoryBuffer::Format format) {
45 switch (format) {
46 case gfx::GpuMemoryBuffer::BGRA_8888:
47 return 'BGRA';
48 case gfx::GpuMemoryBuffer::RGBA_8888:
49 case gfx::GpuMemoryBuffer::RGBX_8888:
50 NOTREACHED();
51 return 0;
54 NOTREACHED();
55 return 0;
58 const GpuMemoryBufferFactory::Configuration kSupportedConfigurations[] = {
59 { gfx::GpuMemoryBuffer::BGRA_8888, gfx::GpuMemoryBuffer::MAP }
62 } // namespace
64 GpuMemoryBufferFactoryIOSurface::GpuMemoryBufferFactoryIOSurface() {
67 GpuMemoryBufferFactoryIOSurface::~GpuMemoryBufferFactoryIOSurface() {
70 // static
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)
76 return true;
79 return false;
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,
95 int client_id,
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));
111 if (!io_surface)
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;
124 handle.id = id;
125 handle.io_surface_id = IOSurfaceGetID(io_surface);
126 return handle;
129 void GpuMemoryBufferFactoryIOSurface::DestroyGpuMemoryBuffer(
130 gfx::GpuMemoryBufferId id,
131 int client_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() {
141 return this;
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,
150 int client_id) {
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>();
163 return image;
166 } // namespace content