Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / content / common / gpu / client / gpu_memory_buffer_impl_io_surface.cc
blob7a3f044f8999eaf06dc758b45b4e519f24e11cfd
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"
11 namespace content {
12 namespace {
14 uint32_t LockFlags(gfx::BufferUsage usage) {
15 switch (usage) {
16 case gfx::BufferUsage::MAP:
17 return kIOSurfaceLockAvoidSync;
18 case gfx::BufferUsage::PERSISTENT_MAP:
19 return 0;
20 case gfx::BufferUsage::SCANOUT:
21 return 0;
23 NOTREACHED();
24 return 0;
27 } // namespace
29 GpuMemoryBufferImplIOSurface::GpuMemoryBufferImplIOSurface(
30 gfx::GpuMemoryBufferId id,
31 const gfx::Size& size,
32 gfx::BufferFormat format,
33 const DestructionCallback& callback,
34 IOSurfaceRef io_surface,
35 uint32_t lock_flags)
36 : GpuMemoryBufferImpl(id, size, format, callback),
37 io_surface_(io_surface),
38 lock_flags_(lock_flags) {}
40 GpuMemoryBufferImplIOSurface::~GpuMemoryBufferImplIOSurface() {
43 // static
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));
52 if (!io_surface)
53 return nullptr;
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) {
61 DCHECK(!mapped_);
62 IOReturn status = IOSurfaceLock(io_surface_, lock_flags_, NULL);
63 DCHECK_NE(status, kIOReturnCannotLock);
64 mapped_ = true;
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);
68 return true;
71 void GpuMemoryBufferImplIOSurface::Unmap() {
72 DCHECK(mapped_);
73 IOSurfaceUnlock(io_surface_, lock_flags_, NULL);
74 mapped_ = false;
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;
86 handle.id = id_;
87 return handle;
90 } // namespace content