gpu: Associate all GpuMemoryBuffers with unique IDs.
[chromium-blink-merge.git] / content / common / gpu / gpu_memory_buffer_factory_io_surface.cc
blob182f82655d9ef37e856bc479ae273656ae3a4692
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 "content/common/gpu/client/gpu_memory_buffer_impl_io_surface.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 } // namespace
32 GpuMemoryBufferFactoryIOSurface::GpuMemoryBufferFactoryIOSurface() {
35 GpuMemoryBufferFactoryIOSurface::~GpuMemoryBufferFactoryIOSurface() {
38 gfx::GpuMemoryBufferHandle
39 GpuMemoryBufferFactoryIOSurface::CreateGpuMemoryBuffer(
40 gfx::GpuMemoryBufferId id,
41 const gfx::Size& size,
42 gfx::GpuMemoryBuffer::Format format,
43 int client_id) {
44 base::ScopedCFTypeRef<CFMutableDictionaryRef> properties;
45 properties.reset(CFDictionaryCreateMutable(kCFAllocatorDefault,
47 &kCFTypeDictionaryKeyCallBacks,
48 &kCFTypeDictionaryValueCallBacks));
49 AddIntegerValue(properties, kIOSurfaceWidth, size.width());
50 AddIntegerValue(properties, kIOSurfaceHeight, size.height());
51 AddIntegerValue(properties,
52 kIOSurfaceBytesPerElement,
53 GpuMemoryBufferImpl::BytesPerPixel(format));
54 AddIntegerValue(properties,
55 kIOSurfacePixelFormat,
56 GpuMemoryBufferImplIOSurface::PixelFormat(format));
57 // TODO(reveman): Remove this when using a mach_port_t to transfer
58 // IOSurface to browser and renderer process. crbug.com/323304
59 AddBooleanValue(properties, kIOSurfaceIsGlobal, true);
61 base::ScopedCFTypeRef<IOSurfaceRef> io_surface(IOSurfaceCreate(properties));
62 if (!io_surface)
63 return gfx::GpuMemoryBufferHandle();
65 IOSurfaceMapKey key(id, client_id);
66 DCHECK(io_surfaces_.find(key) == io_surfaces_.end());
67 io_surfaces_[key] = io_surface;
69 gfx::GpuMemoryBufferHandle handle;
70 handle.type = gfx::IO_SURFACE_BUFFER;
71 handle.id = id;
72 handle.io_surface_id = IOSurfaceGetID(io_surface);
73 return handle;
76 void GpuMemoryBufferFactoryIOSurface::DestroyGpuMemoryBuffer(
77 gfx::GpuMemoryBufferId id,
78 int client_id) {
79 IOSurfaceMapKey key(id, client_id);
80 IOSurfaceMap::iterator it = io_surfaces_.find(key);
81 if (it != io_surfaces_.end())
82 io_surfaces_.erase(it);
85 scoped_refptr<gfx::GLImage>
86 GpuMemoryBufferFactoryIOSurface::CreateImageForGpuMemoryBuffer(
87 gfx::GpuMemoryBufferId id,
88 const gfx::Size& size,
89 gfx::GpuMemoryBuffer::Format format,
90 int client_id) {
91 IOSurfaceMapKey key(id, client_id);
92 IOSurfaceMap::iterator it = io_surfaces_.find(key);
93 if (it == io_surfaces_.end())
94 return scoped_refptr<gfx::GLImage>();
96 scoped_refptr<gfx::GLImageIOSurface> image(new gfx::GLImageIOSurface(size));
97 if (!image->Initialize(it->second.get()))
98 return scoped_refptr<gfx::GLImage>();
100 return image;
103 } // namespace content