1 // Copyright 2013 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/client/gpu_memory_buffer_impl_io_surface.h"
7 #include "base/atomic_sequence_num.h"
9 #include "base/logging.h"
10 #include "content/common/gpu/client/gpu_memory_buffer_factory_host.h"
11 #include "ui/gl/gl_bindings.h"
16 base::StaticAtomicSequenceNumber g_next_buffer_id;
21 void GpuMemoryBufferCreated(
22 const gfx::Size& size,
23 unsigned internalformat,
24 const GpuMemoryBufferImpl::CreationCallback& callback,
25 const gfx::GpuMemoryBufferHandle& handle) {
26 DCHECK_EQ(gfx::IO_SURFACE_BUFFER, handle.type);
28 callback.Run(GpuMemoryBufferImplIOSurface::CreateFromHandle(
29 handle, size, internalformat, base::Bind(&Noop)));
32 void GpuMemoryBufferCreatedForChildProcess(
33 const GpuMemoryBufferImpl::AllocationCallback& callback,
34 const gfx::GpuMemoryBufferHandle& handle) {
35 DCHECK_EQ(gfx::IO_SURFACE_BUFFER, handle.type);
42 GpuMemoryBufferImplIOSurface::GpuMemoryBufferImplIOSurface(
43 const gfx::Size& size,
44 unsigned internalformat,
45 const DestructionCallback& callback,
46 IOSurfaceRef io_surface)
47 : GpuMemoryBufferImpl(size, internalformat, callback),
48 io_surface_(io_surface) {
51 GpuMemoryBufferImplIOSurface::~GpuMemoryBufferImplIOSurface() {
55 void GpuMemoryBufferImplIOSurface::Create(const gfx::Size& size,
56 unsigned internalformat,
59 const CreationCallback& callback) {
60 gfx::GpuMemoryBufferHandle handle;
61 handle.type = gfx::IO_SURFACE_BUFFER;
62 handle.global_id.primary_id = g_next_buffer_id.GetNext();
63 handle.global_id.secondary_id = client_id;
64 GpuMemoryBufferFactoryHost::GetInstance()->CreateGpuMemoryBuffer(
69 base::Bind(&GpuMemoryBufferCreated, size, internalformat, callback));
73 void GpuMemoryBufferImplIOSurface::AllocateForChildProcess(
74 const gfx::Size& size,
75 unsigned internalformat,
78 const AllocationCallback& callback) {
79 gfx::GpuMemoryBufferHandle handle;
80 handle.type = gfx::IO_SURFACE_BUFFER;
81 handle.global_id.primary_id = g_next_buffer_id.GetNext();
82 handle.global_id.secondary_id = child_client_id;
83 GpuMemoryBufferFactoryHost::GetInstance()->CreateGpuMemoryBuffer(
88 base::Bind(&GpuMemoryBufferCreatedForChildProcess, callback));
92 scoped_ptr<GpuMemoryBufferImpl> GpuMemoryBufferImplIOSurface::CreateFromHandle(
93 const gfx::GpuMemoryBufferHandle& handle,
94 const gfx::Size& size,
95 unsigned internalformat,
96 const DestructionCallback& callback) {
97 DCHECK(IsFormatSupported(internalformat));
99 base::ScopedCFTypeRef<IOSurfaceRef> io_surface(
100 IOSurfaceLookup(handle.io_surface_id));
102 return scoped_ptr<GpuMemoryBufferImpl>();
104 return make_scoped_ptr<GpuMemoryBufferImpl>(new GpuMemoryBufferImplIOSurface(
105 size, internalformat, callback, io_surface.get()));
109 bool GpuMemoryBufferImplIOSurface::IsFormatSupported(unsigned internalformat) {
110 switch (internalformat) {
119 bool GpuMemoryBufferImplIOSurface::IsUsageSupported(unsigned usage) {
121 case GL_IMAGE_MAP_CHROMIUM:
129 bool GpuMemoryBufferImplIOSurface::IsConfigurationSupported(
130 unsigned internalformat,
132 return IsFormatSupported(internalformat) && IsUsageSupported(usage);
136 uint32 GpuMemoryBufferImplIOSurface::PixelFormat(unsigned internalformat) {
137 switch (internalformat) {
146 void* GpuMemoryBufferImplIOSurface::Map() {
148 IOSurfaceLock(io_surface_, 0, NULL);
150 return IOSurfaceGetBaseAddress(io_surface_);
153 void GpuMemoryBufferImplIOSurface::Unmap() {
155 IOSurfaceUnlock(io_surface_, 0, NULL);
159 uint32 GpuMemoryBufferImplIOSurface::GetStride() const {
160 return IOSurfaceGetBytesPerRow(io_surface_);
163 gfx::GpuMemoryBufferHandle GpuMemoryBufferImplIOSurface::GetHandle() const {
164 gfx::GpuMemoryBufferHandle handle;
165 handle.type = gfx::IO_SURFACE_BUFFER;
166 handle.io_surface_id = IOSurfaceGetID(io_surface_);
170 } // namespace content