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/logging.h"
8 #include "content/common/mac/io_surface_manager.h"
9 #include "ui/gfx/buffer_format_util.h"
14 uint32_t LockFlags(gfx::BufferUsage usage
) {
16 case gfx::BufferUsage::MAP
:
17 return kIOSurfaceLockAvoidSync
;
18 case gfx::BufferUsage::PERSISTENT_MAP
:
20 case gfx::BufferUsage::SCANOUT
:
29 GpuMemoryBufferImplIOSurface::GpuMemoryBufferImplIOSurface(
30 gfx::GpuMemoryBufferId id
,
31 const gfx::Size
& size
,
32 gfx::BufferFormat format
,
33 const DestructionCallback
& callback
,
34 IOSurfaceRef io_surface
,
36 : GpuMemoryBufferImpl(id
, size
, format
, callback
),
37 io_surface_(io_surface
),
38 lock_flags_(lock_flags
) {}
40 GpuMemoryBufferImplIOSurface::~GpuMemoryBufferImplIOSurface() {
44 scoped_ptr
<GpuMemoryBufferImpl
> GpuMemoryBufferImplIOSurface::CreateFromHandle(
45 const gfx::GpuMemoryBufferHandle
& handle
,
46 const gfx::Size
& size
,
47 gfx::BufferFormat format
,
48 gfx::BufferUsage usage
,
49 const DestructionCallback
& callback
) {
50 base::ScopedCFTypeRef
<IOSurfaceRef
> io_surface(
51 IOSurfaceManager::GetInstance()->AcquireIOSurface(handle
.id
));
55 return make_scoped_ptr
<GpuMemoryBufferImpl
>(
56 new GpuMemoryBufferImplIOSurface(handle
.id
, size
, format
, callback
,
57 io_surface
.release(), LockFlags(usage
)));
60 bool GpuMemoryBufferImplIOSurface::Map(void** data
) {
62 IOReturn status
= IOSurfaceLock(io_surface_
, lock_flags_
, NULL
);
63 DCHECK_NE(status
, kIOReturnCannotLock
);
65 size_t num_planes
= gfx::NumberOfPlanesForBufferFormat(GetFormat());
66 for (size_t plane
= 0; plane
< num_planes
; ++plane
)
67 data
[plane
] = IOSurfaceGetBaseAddressOfPlane(io_surface_
, plane
);
71 void GpuMemoryBufferImplIOSurface::Unmap() {
73 IOSurfaceUnlock(io_surface_
, lock_flags_
, NULL
);
77 void GpuMemoryBufferImplIOSurface::GetStride(int* strides
) const {
78 size_t num_planes
= gfx::NumberOfPlanesForBufferFormat(GetFormat());
79 for (size_t plane
= 0; plane
< num_planes
; ++plane
)
80 strides
[plane
] = IOSurfaceGetBytesPerRowOfPlane(io_surface_
, plane
);
83 gfx::GpuMemoryBufferHandle
GpuMemoryBufferImplIOSurface::GetHandle() const {
84 gfx::GpuMemoryBufferHandle handle
;
85 handle
.type
= gfx::IO_SURFACE_BUFFER
;
90 } // namespace content