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 "ui/gl/gl_bindings.h" 9 10 namespace content { 11 GpuMemoryBufferImplIOSurface(const gfx::Size & size,unsigned internalformat)12GpuMemoryBufferImplIOSurface::GpuMemoryBufferImplIOSurface( 13 const gfx::Size& size, 14 unsigned internalformat) 15 : GpuMemoryBufferImpl(size, internalformat) {} 16 ~GpuMemoryBufferImplIOSurface()17GpuMemoryBufferImplIOSurface::~GpuMemoryBufferImplIOSurface() {} 18 19 // static IsFormatSupported(unsigned internalformat)20bool GpuMemoryBufferImplIOSurface::IsFormatSupported(unsigned internalformat) { 21 switch (internalformat) { 22 case GL_BGRA8_EXT: 23 return true; 24 default: 25 return false; 26 } 27 } 28 29 // static IsUsageSupported(unsigned usage)30bool GpuMemoryBufferImplIOSurface::IsUsageSupported(unsigned usage) { 31 switch (usage) { 32 case GL_IMAGE_MAP_CHROMIUM: 33 return true; 34 default: 35 return false; 36 } 37 } 38 39 // static IsConfigurationSupported(unsigned internalformat,unsigned usage)40bool GpuMemoryBufferImplIOSurface::IsConfigurationSupported( 41 unsigned internalformat, 42 unsigned usage) { 43 return IsFormatSupported(internalformat) && IsUsageSupported(usage); 44 } 45 46 // static PixelFormat(unsigned internalformat)47uint32 GpuMemoryBufferImplIOSurface::PixelFormat(unsigned internalformat) { 48 switch (internalformat) { 49 case GL_BGRA8_EXT: 50 return 'BGRA'; 51 default: 52 NOTREACHED(); 53 return 0; 54 } 55 } 56 InitializeFromHandle(gfx::GpuMemoryBufferHandle handle)57bool GpuMemoryBufferImplIOSurface::InitializeFromHandle( 58 gfx::GpuMemoryBufferHandle handle) { 59 DCHECK(IsFormatSupported(internalformat_)); 60 io_surface_.reset(IOSurfaceLookup(handle.io_surface_id)); 61 if (!io_surface_) { 62 VLOG(1) << "IOSurface lookup failed"; 63 return false; 64 } 65 66 return true; 67 } 68 Map()69void* GpuMemoryBufferImplIOSurface::Map() { 70 DCHECK(!mapped_); 71 IOSurfaceLock(io_surface_, 0, NULL); 72 mapped_ = true; 73 return IOSurfaceGetBaseAddress(io_surface_); 74 } 75 Unmap()76void GpuMemoryBufferImplIOSurface::Unmap() { 77 DCHECK(mapped_); 78 IOSurfaceUnlock(io_surface_, 0, NULL); 79 mapped_ = false; 80 } 81 GetStride() const82uint32 GpuMemoryBufferImplIOSurface::GetStride() const { 83 return IOSurfaceGetBytesPerRow(io_surface_); 84 } 85 GetHandle() const86gfx::GpuMemoryBufferHandle GpuMemoryBufferImplIOSurface::GetHandle() const { 87 gfx::GpuMemoryBufferHandle handle; 88 handle.type = gfx::IO_SURFACE_BUFFER; 89 handle.io_surface_id = IOSurfaceGetID(io_surface_); 90 return handle; 91 } 92 93 } // namespace content 94