Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / content / common / gpu / client / gpu_memory_buffer_impl_io_surface.cc
blobb2bf0bedd6bd5d687129d53e422c6e35f8c1b83b
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 return 0;
22 NOTREACHED();
23 return 0;
26 } // namespace
28 GpuMemoryBufferImplIOSurface::GpuMemoryBufferImplIOSurface(
29 gfx::GpuMemoryBufferId id,
30 const gfx::Size& size,
31 Format format,
32 const DestructionCallback& callback,
33 IOSurfaceRef io_surface,
34 uint32_t lock_flags)
35 : GpuMemoryBufferImpl(id, size, format, callback),
36 io_surface_(io_surface),
37 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 Format format,
48 Usage 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 *data = IOSurfaceGetBaseAddress(io_surface_);
66 return true;
69 void GpuMemoryBufferImplIOSurface::Unmap() {
70 DCHECK(mapped_);
71 IOSurfaceUnlock(io_surface_, lock_flags_, NULL);
72 mapped_ = false;
75 void GpuMemoryBufferImplIOSurface::GetStride(int* stride) const {
76 *stride = IOSurfaceGetBytesPerRow(io_surface_);
79 gfx::GpuMemoryBufferHandle GpuMemoryBufferImplIOSurface::GetHandle() const {
80 gfx::GpuMemoryBufferHandle handle;
81 handle.type = gfx::IO_SURFACE_BUFFER;
82 handle.id = id_;
83 return handle;
86 } // namespace content