[Android WebView] Fix webview perf bot switchover to use org.chromium.webview_shell...
[chromium-blink-merge.git] / content / common / gpu / gpu_memory_buffer_factory_io_surface.cc
blobb73ef8389b803dc6f35ad0f6808896882cab3a02
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 <vector>
11 #include "base/logging.h"
12 #include "content/common/mac/io_surface_manager.h"
13 #include "ui/gl/gl_image_io_surface.h"
15 namespace content {
16 namespace {
18 void AddIntegerValue(CFMutableDictionaryRef dictionary,
19 const CFStringRef key,
20 int32 value) {
21 base::ScopedCFTypeRef<CFNumberRef> number(
22 CFNumberCreate(NULL, kCFNumberSInt32Type, &value));
23 CFDictionaryAddValue(dictionary, key, number.get());
26 int32 BytesPerPixel(gfx::GpuMemoryBuffer::Format format) {
27 switch (format) {
28 case gfx::GpuMemoryBuffer::R_8:
29 return 1;
30 case gfx::GpuMemoryBuffer::BGRA_8888:
31 return 4;
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:
41 NOTREACHED();
42 return 0;
45 NOTREACHED();
46 return 0;
49 int32 PixelFormat(gfx::GpuMemoryBuffer::Format format) {
50 switch (format) {
51 case gfx::GpuMemoryBuffer::R_8:
52 return 'L008';
53 case gfx::GpuMemoryBuffer::BGRA_8888:
54 return 'BGRA';
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:
64 NOTREACHED();
65 return 0;
68 NOTREACHED();
69 return 0;
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}};
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));
123 base::ScopedCFTypeRef<IOSurfaceRef> io_surface(IOSurfaceCreate(properties));
124 if (!io_surface)
125 return gfx::GpuMemoryBufferHandle();
127 if (!IOSurfaceManager::GetInstance()->RegisterIOSurface(id, client_id,
128 io_surface)) {
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;
142 handle.id = id;
143 return handle;
146 void GpuMemoryBufferFactoryIOSurface::DestroyGpuMemoryBuffer(
147 gfx::GpuMemoryBufferId id,
148 int client_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() {
161 return this;
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,
170 int client_id) {
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>();
184 return image;
187 } // namespace content