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"
15 void AddBooleanValue(CFMutableDictionaryRef dictionary
,
16 const CFStringRef key
,
19 dictionary
, key
, value
? kCFBooleanTrue
: kCFBooleanFalse
);
22 void AddIntegerValue(CFMutableDictionaryRef dictionary
,
23 const CFStringRef key
,
25 base::ScopedCFTypeRef
<CFNumberRef
> number(
26 CFNumberCreate(NULL
, kCFNumberSInt32Type
, &value
));
27 CFDictionaryAddValue(dictionary
, key
, number
.get());
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
,
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
));
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
;
72 handle
.io_surface_id
= IOSurfaceGetID(io_surface
);
76 void GpuMemoryBufferFactoryIOSurface::DestroyGpuMemoryBuffer(
77 gfx::GpuMemoryBufferId 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
,
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
>();
103 } // namespace content