Add P2PDatagramSocket and P2PStreamSocket interfaces.
[chromium-blink-merge.git] / content / common / gpu / client / gpu_memory_buffer_impl_io_surface.cc
blob45292cf7cc9cd1794962d8ab01359273eb4b7bf1
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"
10 namespace content {
11 namespace {
13 uint32_t LockFlags(gfx::GpuMemoryBuffer::Usage usage) {
14 switch (usage) {
15 case gfx::GpuMemoryBuffer::MAP:
16 return kIOSurfaceLockAvoidSync;
17 case gfx::GpuMemoryBuffer::PERSISTENT_MAP:
18 return 0;
19 case gfx::GpuMemoryBuffer::SCANOUT:
20 NOTREACHED();
21 return 0;
23 NOTREACHED();
24 return 0;
27 } // namespace
29 GpuMemoryBufferImplIOSurface::GpuMemoryBufferImplIOSurface(
30 gfx::GpuMemoryBufferId id,
31 const gfx::Size& size,
32 Format 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) {
41 GpuMemoryBufferImplIOSurface::~GpuMemoryBufferImplIOSurface() {
44 // static
45 scoped_ptr<GpuMemoryBufferImpl> GpuMemoryBufferImplIOSurface::CreateFromHandle(
46 const gfx::GpuMemoryBufferHandle& handle,
47 const gfx::Size& size,
48 Format format,
49 Usage usage,
50 const DestructionCallback& callback) {
51 base::ScopedCFTypeRef<IOSurfaceRef> io_surface(
52 IOSurfaceManager::GetInstance()->AcquireIOSurface(handle.id));
53 if (!io_surface)
54 return nullptr;
56 return make_scoped_ptr<GpuMemoryBufferImpl>(
57 new GpuMemoryBufferImplIOSurface(handle.id, size, format, callback,
58 io_surface.release(), LockFlags(usage)));
61 bool GpuMemoryBufferImplIOSurface::Map(void** data) {
62 DCHECK(!mapped_);
63 IOReturn status = IOSurfaceLock(io_surface_, lock_flags_, NULL);
64 DCHECK_NE(status, kIOReturnCannotLock);
65 mapped_ = true;
66 *data = IOSurfaceGetBaseAddress(io_surface_);
67 return true;
70 void GpuMemoryBufferImplIOSurface::Unmap() {
71 DCHECK(mapped_);
72 IOSurfaceUnlock(io_surface_, lock_flags_, NULL);
73 mapped_ = false;
76 void GpuMemoryBufferImplIOSurface::GetStride(int* stride) const {
77 *stride = IOSurfaceGetBytesPerRow(io_surface_);
80 gfx::GpuMemoryBufferHandle GpuMemoryBufferImplIOSurface::GetHandle() const {
81 gfx::GpuMemoryBufferHandle handle;
82 handle.type = gfx::IO_SURFACE_BUFFER;
83 handle.id = id_;
84 return handle;
87 } // namespace content