Supervised user whitelists: Cleanup
[chromium-blink-merge.git] / content / common / gpu / client / gpu_memory_buffer_impl.cc
blob3cdbe86f6cd1d721b2fe9fb78240b4bf746e573c
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.h"
7 #include "base/logging.h"
8 #include "base/numerics/safe_math.h"
9 #include "content/common/gpu/client/gpu_memory_buffer_impl_shared_memory.h"
10 #include "ui/gl/gl_bindings.h"
12 #if defined(OS_MACOSX)
13 #include "content/common/gpu/client/gpu_memory_buffer_impl_io_surface.h"
14 #endif
16 #if defined(OS_ANDROID)
17 #include "content/common/gpu/client/gpu_memory_buffer_impl_surface_texture.h"
18 #endif
20 #if defined(USE_OZONE)
21 #include "content/common/gpu/client/gpu_memory_buffer_impl_ozone_native_buffer.h"
22 #endif
24 namespace content {
26 GpuMemoryBufferImpl::GpuMemoryBufferImpl(gfx::GpuMemoryBufferId id,
27 const gfx::Size& size,
28 Format format,
29 const DestructionCallback& callback)
30 : id_(id),
31 size_(size),
32 format_(format),
33 callback_(callback),
34 mapped_(false),
35 destruction_sync_point_(0) {
38 GpuMemoryBufferImpl::~GpuMemoryBufferImpl() {
39 callback_.Run(destruction_sync_point_);
42 // static
43 scoped_ptr<GpuMemoryBufferImpl> GpuMemoryBufferImpl::CreateFromHandle(
44 const gfx::GpuMemoryBufferHandle& handle,
45 const gfx::Size& size,
46 Format format,
47 const DestructionCallback& callback) {
48 switch (handle.type) {
49 case gfx::SHARED_MEMORY_BUFFER:
50 return GpuMemoryBufferImplSharedMemory::CreateFromHandle(
51 handle, size, format, callback);
52 #if defined(OS_MACOSX)
53 case gfx::IO_SURFACE_BUFFER:
54 return GpuMemoryBufferImplIOSurface::CreateFromHandle(
55 handle, size, format, callback);
56 #endif
57 #if defined(OS_ANDROID)
58 case gfx::SURFACE_TEXTURE_BUFFER:
59 return GpuMemoryBufferImplSurfaceTexture::CreateFromHandle(
60 handle, size, format, callback);
61 #endif
62 #if defined(USE_OZONE)
63 case gfx::OZONE_NATIVE_BUFFER:
64 return GpuMemoryBufferImplOzoneNativeBuffer::CreateFromHandle(
65 handle, size, format, callback);
66 #endif
67 default:
68 NOTREACHED();
69 return scoped_ptr<GpuMemoryBufferImpl>();
73 // static
74 GpuMemoryBufferImpl* GpuMemoryBufferImpl::FromClientBuffer(
75 ClientBuffer buffer) {
76 return reinterpret_cast<GpuMemoryBufferImpl*>(buffer);
79 // static
80 size_t GpuMemoryBufferImpl::NumberOfPlanesForGpuMemoryBufferFormat(
81 Format format) {
82 switch (format) {
83 case ATC:
84 case ATCIA:
85 case DXT1:
86 case DXT5:
87 case ETC1:
88 case R_8:
89 case RGBA_8888:
90 case RGBX_8888:
91 case BGRA_8888:
92 return 1;
93 case YUV_420:
94 return 3;
96 NOTREACHED();
97 return 0;
100 // static
101 bool GpuMemoryBufferImpl::StrideInBytes(size_t width,
102 Format format,
103 int plane,
104 size_t* stride_in_bytes) {
105 base::CheckedNumeric<size_t> checked_stride = width;
106 switch (format) {
107 case ATCIA:
108 case DXT5:
109 DCHECK_EQ(plane, 0);
110 *stride_in_bytes = width;
111 return true;
112 case ATC:
113 case DXT1:
114 case ETC1:
115 DCHECK_EQ(plane, 0);
116 DCHECK_EQ(width % 2, 0u);
117 *stride_in_bytes = width / 2;
118 return true;
119 case R_8:
120 checked_stride += 3;
121 if (!checked_stride.IsValid())
122 return false;
123 *stride_in_bytes = checked_stride.ValueOrDie() & ~0x3;
124 return true;
125 case RGBX_8888:
126 case RGBA_8888:
127 case BGRA_8888:
128 checked_stride *= 4;
129 if (!checked_stride.IsValid())
130 return false;
131 *stride_in_bytes = checked_stride.ValueOrDie();
132 return true;
133 case YUV_420:
134 DCHECK_EQ(width % 2, 0u);
135 *stride_in_bytes = width / SubsamplingFactor(format, plane);
136 return true;
138 NOTREACHED();
139 return false;
142 // static
143 size_t GpuMemoryBufferImpl::SubsamplingFactor(
144 Format format,
145 int plane) {
146 switch (format) {
147 case ATC:
148 case ATCIA:
149 case DXT1:
150 case DXT5:
151 case ETC1:
152 case R_8:
153 case RGBA_8888:
154 case RGBX_8888:
155 case BGRA_8888:
156 return 1;
157 case YUV_420: {
158 static size_t factor[] = {1, 2, 2};
159 DCHECK_LT(static_cast<size_t>(plane), arraysize(factor));
160 return factor[plane];
163 NOTREACHED();
164 return 0;
167 gfx::GpuMemoryBuffer::Format GpuMemoryBufferImpl::GetFormat() const {
168 return format_;
171 bool GpuMemoryBufferImpl::IsMapped() const {
172 return mapped_;
175 ClientBuffer GpuMemoryBufferImpl::AsClientBuffer() {
176 return reinterpret_cast<ClientBuffer>(this);
179 } // namespace content