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"
8 #include "base/logging.h"
9 #include "content/common/gpu/client/gpu_memory_buffer_factory_host.h"
10 #include "ui/gl/gl_bindings.h"
15 void GpuMemoryBufferDeleted(gfx::GpuMemoryBufferId id
,
18 GpuMemoryBufferFactoryHost::GetInstance()->DestroyGpuMemoryBuffer(
19 gfx::IO_SURFACE_BUFFER
, id
, client_id
, sync_point
);
22 void GpuMemoryBufferCreated(
23 const gfx::Size
& size
,
24 gfx::GpuMemoryBuffer::Format format
,
26 const GpuMemoryBufferImpl::CreationCallback
& callback
,
27 const gfx::GpuMemoryBufferHandle
& handle
) {
28 if (handle
.is_null()) {
29 callback
.Run(scoped_ptr
<GpuMemoryBufferImpl
>());
33 DCHECK_EQ(gfx::IO_SURFACE_BUFFER
, handle
.type
);
34 callback
.Run(GpuMemoryBufferImplIOSurface::CreateFromHandle(
38 base::Bind(&GpuMemoryBufferDeleted
, handle
.id
, client_id
)));
41 void GpuMemoryBufferCreatedForChildProcess(
42 const GpuMemoryBufferImpl::AllocationCallback
& callback
,
43 const gfx::GpuMemoryBufferHandle
& handle
) {
44 DCHECK_IMPLIES(!handle
.is_null(), gfx::IO_SURFACE_BUFFER
== handle
.type
);
51 GpuMemoryBufferImplIOSurface::GpuMemoryBufferImplIOSurface(
52 gfx::GpuMemoryBufferId id
,
53 const gfx::Size
& size
,
55 const DestructionCallback
& callback
,
56 IOSurfaceRef io_surface
)
57 : GpuMemoryBufferImpl(id
, size
, format
, callback
), io_surface_(io_surface
) {
60 GpuMemoryBufferImplIOSurface::~GpuMemoryBufferImplIOSurface() {
64 void GpuMemoryBufferImplIOSurface::Create(gfx::GpuMemoryBufferId id
,
65 const gfx::Size
& size
,
68 const CreationCallback
& callback
) {
69 GpuMemoryBufferFactoryHost::GetInstance()->CreateGpuMemoryBuffer(
70 gfx::IO_SURFACE_BUFFER
,
76 base::Bind(&GpuMemoryBufferCreated
, size
, format
, client_id
, callback
));
80 void GpuMemoryBufferImplIOSurface::AllocateForChildProcess(
81 gfx::GpuMemoryBufferId id
,
82 const gfx::Size
& size
,
85 const AllocationCallback
& callback
) {
86 GpuMemoryBufferFactoryHost::GetInstance()->CreateGpuMemoryBuffer(
87 gfx::IO_SURFACE_BUFFER
,
93 base::Bind(&GpuMemoryBufferCreatedForChildProcess
, callback
));
97 scoped_ptr
<GpuMemoryBufferImpl
> GpuMemoryBufferImplIOSurface::CreateFromHandle(
98 const gfx::GpuMemoryBufferHandle
& handle
,
99 const gfx::Size
& size
,
101 const DestructionCallback
& callback
) {
102 DCHECK(IsFormatSupported(format
));
104 base::ScopedCFTypeRef
<IOSurfaceRef
> io_surface(
105 IOSurfaceLookup(handle
.io_surface_id
));
107 return scoped_ptr
<GpuMemoryBufferImpl
>();
109 return make_scoped_ptr
<GpuMemoryBufferImpl
>(new GpuMemoryBufferImplIOSurface(
110 handle
.id
, size
, format
, callback
, io_surface
.get()));
114 void GpuMemoryBufferImplIOSurface::DeletedByChildProcess(
115 gfx::GpuMemoryBufferId id
,
117 uint32_t sync_point
) {
118 GpuMemoryBufferFactoryHost::GetInstance()->DestroyGpuMemoryBuffer(
119 gfx::IO_SURFACE_BUFFER
, id
, child_client_id
, sync_point
);
123 bool GpuMemoryBufferImplIOSurface::IsFormatSupported(Format format
) {
137 bool GpuMemoryBufferImplIOSurface::IsUsageSupported(Usage usage
) {
150 bool GpuMemoryBufferImplIOSurface::IsConfigurationSupported(Format format
,
152 return IsFormatSupported(format
) && IsUsageSupported(usage
);
156 uint32
GpuMemoryBufferImplIOSurface::PixelFormat(Format format
) {
170 void* GpuMemoryBufferImplIOSurface::Map() {
172 IOSurfaceLock(io_surface_
, 0, NULL
);
174 return IOSurfaceGetBaseAddress(io_surface_
);
177 void GpuMemoryBufferImplIOSurface::Unmap() {
179 IOSurfaceUnlock(io_surface_
, 0, NULL
);
183 uint32
GpuMemoryBufferImplIOSurface::GetStride() const {
184 return IOSurfaceGetBytesPerRow(io_surface_
);
187 gfx::GpuMemoryBufferHandle
GpuMemoryBufferImplIOSurface::GetHandle() const {
188 gfx::GpuMemoryBufferHandle handle
;
189 handle
.type
= gfx::IO_SURFACE_BUFFER
;
191 handle
.io_surface_id
= IOSurfaceGetID(io_surface_
);
195 } // namespace content