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"
13 uint32_t LockFlags(gfx::BufferUsage usage
) {
15 case gfx::BufferUsage::MAP
:
16 return kIOSurfaceLockAvoidSync
;
17 case gfx::BufferUsage::PERSISTENT_MAP
:
19 case gfx::BufferUsage::SCANOUT
:
28 GpuMemoryBufferImplIOSurface::GpuMemoryBufferImplIOSurface(
29 gfx::GpuMemoryBufferId id
,
30 const gfx::Size
& size
,
31 gfx::BufferFormat format
,
32 const DestructionCallback
& callback
,
33 IOSurfaceRef io_surface
,
35 : GpuMemoryBufferImpl(id
, size
, format
, callback
),
36 io_surface_(io_surface
),
37 lock_flags_(lock_flags
) {}
39 GpuMemoryBufferImplIOSurface::~GpuMemoryBufferImplIOSurface() {
43 scoped_ptr
<GpuMemoryBufferImpl
> GpuMemoryBufferImplIOSurface::CreateFromHandle(
44 const gfx::GpuMemoryBufferHandle
& handle
,
45 const gfx::Size
& size
,
46 gfx::BufferFormat format
,
47 gfx::BufferUsage usage
,
48 const DestructionCallback
& callback
) {
49 base::ScopedCFTypeRef
<IOSurfaceRef
> io_surface(
50 IOSurfaceManager::GetInstance()->AcquireIOSurface(handle
.id
));
54 return make_scoped_ptr
<GpuMemoryBufferImpl
>(
55 new GpuMemoryBufferImplIOSurface(handle
.id
, size
, format
, callback
,
56 io_surface
.release(), LockFlags(usage
)));
59 bool GpuMemoryBufferImplIOSurface::Map(void** data
) {
61 IOReturn status
= IOSurfaceLock(io_surface_
, lock_flags_
, NULL
);
62 DCHECK_NE(status
, kIOReturnCannotLock
);
64 *data
= IOSurfaceGetBaseAddress(io_surface_
);
68 void GpuMemoryBufferImplIOSurface::Unmap() {
70 IOSurfaceUnlock(io_surface_
, lock_flags_
, NULL
);
74 void GpuMemoryBufferImplIOSurface::GetStride(int* stride
) const {
75 *stride
= IOSurfaceGetBytesPerRow(io_surface_
);
78 gfx::GpuMemoryBufferHandle
GpuMemoryBufferImplIOSurface::GetHandle() const {
79 gfx::GpuMemoryBufferHandle handle
;
80 handle
.type
= gfx::IO_SURFACE_BUFFER
;
85 } // namespace content