OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/common/gpu/client/gpu_memory_buffer_impl_io_surface.h" | 5 #include "content/common/gpu/client/gpu_memory_buffer_impl_io_surface.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "content/common/gpu/gpu_memory_buffer_factory_io_surface.h" |
8 #include "content/common/mac/io_surface_manager.h" | 9 #include "content/common/mac/io_surface_manager.h" |
9 #include "ui/gfx/buffer_format_util.h" | 10 #include "ui/gfx/buffer_format_util.h" |
10 | 11 |
11 namespace content { | 12 namespace content { |
12 namespace { | 13 namespace { |
13 | 14 |
14 uint32_t LockFlags(gfx::BufferUsage usage) { | 15 uint32_t LockFlags(gfx::BufferUsage usage) { |
15 switch (usage) { | 16 switch (usage) { |
16 case gfx::BufferUsage::MAP: | 17 case gfx::BufferUsage::MAP: |
17 return kIOSurfaceLockAvoidSync; | 18 return kIOSurfaceLockAvoidSync; |
18 case gfx::BufferUsage::PERSISTENT_MAP: | 19 case gfx::BufferUsage::PERSISTENT_MAP: |
19 return 0; | 20 return 0; |
20 case gfx::BufferUsage::SCANOUT: | 21 case gfx::BufferUsage::SCANOUT: |
21 return 0; | 22 return 0; |
22 } | 23 } |
23 NOTREACHED(); | 24 NOTREACHED(); |
24 return 0; | 25 return 0; |
25 } | 26 } |
26 | 27 |
| 28 void FreeIOSurfaceForTesting(gfx::GpuMemoryBufferId id) { |
| 29 IOSurfaceManager::GetInstance()->UnregisterIOSurface(id, 0); |
| 30 } |
| 31 |
27 } // namespace | 32 } // namespace |
28 | 33 |
29 GpuMemoryBufferImplIOSurface::GpuMemoryBufferImplIOSurface( | 34 GpuMemoryBufferImplIOSurface::GpuMemoryBufferImplIOSurface( |
30 gfx::GpuMemoryBufferId id, | 35 gfx::GpuMemoryBufferId id, |
31 const gfx::Size& size, | 36 const gfx::Size& size, |
32 gfx::BufferFormat format, | 37 gfx::BufferFormat format, |
33 const DestructionCallback& callback, | 38 const DestructionCallback& callback, |
34 IOSurfaceRef io_surface, | 39 IOSurfaceRef io_surface, |
35 uint32_t lock_flags) | 40 uint32_t lock_flags) |
36 : GpuMemoryBufferImpl(id, size, format, callback), | 41 : GpuMemoryBufferImpl(id, size, format, callback), |
37 io_surface_(io_surface), | 42 io_surface_(io_surface), |
38 lock_flags_(lock_flags) {} | 43 lock_flags_(lock_flags) {} |
39 | 44 |
40 GpuMemoryBufferImplIOSurface::~GpuMemoryBufferImplIOSurface() { | 45 GpuMemoryBufferImplIOSurface::~GpuMemoryBufferImplIOSurface() { |
41 } | 46 } |
42 | 47 |
43 // static | 48 // static |
44 scoped_ptr<GpuMemoryBufferImpl> GpuMemoryBufferImplIOSurface::CreateFromHandle( | 49 scoped_ptr<GpuMemoryBufferImplIOSurface> |
| 50 GpuMemoryBufferImplIOSurface::CreateFromHandle( |
45 const gfx::GpuMemoryBufferHandle& handle, | 51 const gfx::GpuMemoryBufferHandle& handle, |
46 const gfx::Size& size, | 52 const gfx::Size& size, |
47 gfx::BufferFormat format, | 53 gfx::BufferFormat format, |
48 gfx::BufferUsage usage, | 54 gfx::BufferUsage usage, |
49 const DestructionCallback& callback) { | 55 const DestructionCallback& callback) { |
50 base::ScopedCFTypeRef<IOSurfaceRef> io_surface( | 56 base::ScopedCFTypeRef<IOSurfaceRef> io_surface( |
51 IOSurfaceManager::GetInstance()->AcquireIOSurface(handle.id)); | 57 IOSurfaceManager::GetInstance()->AcquireIOSurface(handle.id)); |
52 if (!io_surface) | 58 if (!io_surface) |
53 return nullptr; | 59 return nullptr; |
54 | 60 |
55 return make_scoped_ptr<GpuMemoryBufferImpl>( | 61 return make_scoped_ptr( |
56 new GpuMemoryBufferImplIOSurface(handle.id, size, format, callback, | 62 new GpuMemoryBufferImplIOSurface(handle.id, size, format, callback, |
57 io_surface.release(), LockFlags(usage))); | 63 io_surface.release(), LockFlags(usage))); |
58 } | 64 } |
59 | 65 |
| 66 // static |
| 67 bool GpuMemoryBufferImplIOSurface::IsConfigurationSupported( |
| 68 gfx::BufferFormat format, |
| 69 gfx::BufferUsage usage) { |
| 70 return GpuMemoryBufferFactoryIOSurface:: |
| 71 IsGpuMemoryBufferConfigurationSupported(format, usage); |
| 72 } |
| 73 |
| 74 // static |
| 75 base::Closure GpuMemoryBufferImplIOSurface::AllocateForTesting( |
| 76 const gfx::Size& size, |
| 77 gfx::BufferFormat format, |
| 78 gfx::BufferUsage usage, |
| 79 gfx::GpuMemoryBufferHandle* handle) { |
| 80 base::ScopedCFTypeRef<IOSurfaceRef> io_surface( |
| 81 GpuMemoryBufferFactoryIOSurface::CreateIOSurface(size, format)); |
| 82 DCHECK(io_surface); |
| 83 gfx::GpuMemoryBufferId kBufferId(1); |
| 84 bool rv = IOSurfaceManager::GetInstance()->RegisterIOSurface(kBufferId, 0, |
| 85 io_surface); |
| 86 DCHECK(rv); |
| 87 handle->type = gfx::IO_SURFACE_BUFFER; |
| 88 handle->id = kBufferId; |
| 89 return base::Bind(&FreeIOSurfaceForTesting, kBufferId); |
| 90 } |
| 91 |
60 bool GpuMemoryBufferImplIOSurface::Map(void** data) { | 92 bool GpuMemoryBufferImplIOSurface::Map(void** data) { |
61 DCHECK(!mapped_); | 93 DCHECK(!mapped_); |
62 IOReturn status = IOSurfaceLock(io_surface_, lock_flags_, NULL); | 94 IOReturn status = IOSurfaceLock(io_surface_, lock_flags_, NULL); |
63 DCHECK_NE(status, kIOReturnCannotLock); | 95 DCHECK_NE(status, kIOReturnCannotLock); |
64 mapped_ = true; | 96 mapped_ = true; |
65 size_t num_planes = gfx::NumberOfPlanesForBufferFormat(GetFormat()); | 97 size_t num_planes = gfx::NumberOfPlanesForBufferFormat(GetFormat()); |
66 for (size_t plane = 0; plane < num_planes; ++plane) | 98 for (size_t plane = 0; plane < num_planes; ++plane) |
67 data[plane] = IOSurfaceGetBaseAddressOfPlane(io_surface_, plane); | 99 data[plane] = IOSurfaceGetBaseAddressOfPlane(io_surface_, plane); |
68 return true; | 100 return true; |
69 } | 101 } |
(...skipping 11 matching lines...) Expand all Loading... |
81 } | 113 } |
82 | 114 |
83 gfx::GpuMemoryBufferHandle GpuMemoryBufferImplIOSurface::GetHandle() const { | 115 gfx::GpuMemoryBufferHandle GpuMemoryBufferImplIOSurface::GetHandle() const { |
84 gfx::GpuMemoryBufferHandle handle; | 116 gfx::GpuMemoryBufferHandle handle; |
85 handle.type = gfx::IO_SURFACE_BUFFER; | 117 handle.type = gfx::IO_SURFACE_BUFFER; |
86 handle.id = id_; | 118 handle.id = id_; |
87 return handle; | 119 return handle; |
88 } | 120 } |
89 | 121 |
90 } // namespace content | 122 } // namespace content |
OLD | NEW |