OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "content/common/gpu/client/gpu_memory_buffer_impl_io_surface.h" | |
6 | |
7 #include "base/logging.h" | |
8 #include "gpu/ipc/common/gpu_memory_buffer_support.h" | |
9 #include "ui/gfx/buffer_format_util.h" | |
10 #include "ui/gfx/mac/io_surface.h" | |
11 | |
12 namespace content { | |
13 namespace { | |
14 | |
15 uint32_t LockFlags(gfx::BufferUsage usage) { | |
16 switch (usage) { | |
17 case gfx::BufferUsage::GPU_READ_CPU_READ_WRITE: | |
18 return kIOSurfaceLockAvoidSync; | |
19 case gfx::BufferUsage::GPU_READ: | |
20 case gfx::BufferUsage::SCANOUT: | |
21 case gfx::BufferUsage::GPU_READ_CPU_READ_WRITE_PERSISTENT: | |
22 return 0; | |
23 } | |
24 NOTREACHED(); | |
25 return 0; | |
26 } | |
27 | |
28 void NoOp() { | |
29 } | |
30 | |
31 } // namespace | |
32 | |
33 GpuMemoryBufferImplIOSurface::GpuMemoryBufferImplIOSurface( | |
34 gfx::GpuMemoryBufferId id, | |
35 const gfx::Size& size, | |
36 gfx::BufferFormat format, | |
37 const DestructionCallback& callback, | |
38 IOSurfaceRef io_surface, | |
39 uint32_t lock_flags) | |
40 : GpuMemoryBufferImpl(id, size, format, callback), | |
41 io_surface_(io_surface), | |
42 lock_flags_(lock_flags) {} | |
43 | |
44 GpuMemoryBufferImplIOSurface::~GpuMemoryBufferImplIOSurface() { | |
45 } | |
46 | |
47 // static | |
48 scoped_ptr<GpuMemoryBufferImplIOSurface> | |
49 GpuMemoryBufferImplIOSurface::CreateFromHandle( | |
50 const gfx::GpuMemoryBufferHandle& handle, | |
51 const gfx::Size& size, | |
52 gfx::BufferFormat format, | |
53 gfx::BufferUsage usage, | |
54 const DestructionCallback& callback) { | |
55 base::ScopedCFTypeRef<IOSurfaceRef> io_surface( | |
56 IOSurfaceLookupFromMachPort(handle.mach_port.get())); | |
57 if (!io_surface) | |
58 return nullptr; | |
59 | |
60 return make_scoped_ptr( | |
61 new GpuMemoryBufferImplIOSurface(handle.id, size, format, callback, | |
62 io_surface.release(), LockFlags(usage))); | |
63 } | |
64 | |
65 // static | |
66 bool GpuMemoryBufferImplIOSurface::IsConfigurationSupported( | |
67 gfx::BufferFormat format, | |
68 gfx::BufferUsage usage) { | |
69 return gpu::IsNativeGpuMemoryBufferConfigurationSupported(format, usage); | |
70 } | |
71 | |
72 // static | |
73 base::Closure GpuMemoryBufferImplIOSurface::AllocateForTesting( | |
74 const gfx::Size& size, | |
75 gfx::BufferFormat format, | |
76 gfx::BufferUsage usage, | |
77 gfx::GpuMemoryBufferHandle* handle) { | |
78 base::ScopedCFTypeRef<IOSurfaceRef> io_surface( | |
79 gfx::CreateIOSurface(size, format)); | |
80 DCHECK(io_surface); | |
81 gfx::GpuMemoryBufferId kBufferId(1); | |
82 handle->type = gfx::IO_SURFACE_BUFFER; | |
83 handle->id = kBufferId; | |
84 handle->mach_port.reset(IOSurfaceCreateMachPort(io_surface)); | |
85 return base::Bind(&NoOp); | |
86 } | |
87 | |
88 bool GpuMemoryBufferImplIOSurface::Map() { | |
89 DCHECK(!mapped_); | |
90 IOReturn status = IOSurfaceLock(io_surface_, lock_flags_, NULL); | |
91 DCHECK_NE(status, kIOReturnCannotLock); | |
92 mapped_ = true; | |
93 return true; | |
94 } | |
95 | |
96 void* GpuMemoryBufferImplIOSurface::memory(size_t plane) { | |
97 DCHECK(mapped_); | |
98 DCHECK_LT(plane, gfx::NumberOfPlanesForBufferFormat(format_)); | |
99 return IOSurfaceGetBaseAddressOfPlane(io_surface_, plane); | |
100 } | |
101 | |
102 void GpuMemoryBufferImplIOSurface::Unmap() { | |
103 DCHECK(mapped_); | |
104 IOSurfaceUnlock(io_surface_, lock_flags_, NULL); | |
105 mapped_ = false; | |
106 } | |
107 | |
108 bool GpuMemoryBufferImplIOSurface::IsInUseByMacOSWindowServer() const { | |
109 return IOSurfaceIsInUse(io_surface_); | |
110 } | |
111 | |
112 int GpuMemoryBufferImplIOSurface::stride(size_t plane) const { | |
113 DCHECK_LT(plane, gfx::NumberOfPlanesForBufferFormat(format_)); | |
114 return IOSurfaceGetBytesPerRowOfPlane(io_surface_, plane); | |
115 } | |
116 | |
117 gfx::GpuMemoryBufferHandle GpuMemoryBufferImplIOSurface::GetHandle() const { | |
118 gfx::GpuMemoryBufferHandle handle; | |
119 handle.type = gfx::IO_SURFACE_BUFFER; | |
120 handle.id = id_; | |
121 return handle; | |
122 } | |
123 | |
124 } // namespace content | |
OLD | NEW |