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.cc
blob5d2350af8f6d46625289f5bc18faf52fdfe8bf35
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_pixmap.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 DCHECK(!mapped_);
40 callback_.Run(destruction_sync_point_);
43 // static
44 scoped_ptr<GpuMemoryBufferImpl> GpuMemoryBufferImpl::CreateFromHandle(
45 const gfx::GpuMemoryBufferHandle& handle,
46 const gfx::Size& size,
47 Format format,
48 Usage usage,
49 const DestructionCallback& callback) {
50 switch (handle.type) {
51 case gfx::SHARED_MEMORY_BUFFER:
52 return GpuMemoryBufferImplSharedMemory::CreateFromHandle(
53 handle, size, format, callback);
54 #if defined(OS_MACOSX)
55 case gfx::IO_SURFACE_BUFFER:
56 return GpuMemoryBufferImplIOSurface::CreateFromHandle(
57 handle, size, format, usage, callback);
58 #endif
59 #if defined(OS_ANDROID)
60 case gfx::SURFACE_TEXTURE_BUFFER:
61 return GpuMemoryBufferImplSurfaceTexture::CreateFromHandle(
62 handle, size, format, callback);
63 #endif
64 #if defined(USE_OZONE)
65 case gfx::OZONE_NATIVE_PIXMAP:
66 return GpuMemoryBufferImplOzoneNativePixmap::CreateFromHandle(
67 handle, size, format, usage, callback);
68 #endif
69 default:
70 NOTREACHED();
71 return nullptr;
75 // static
76 GpuMemoryBufferImpl* GpuMemoryBufferImpl::FromClientBuffer(
77 ClientBuffer buffer) {
78 return reinterpret_cast<GpuMemoryBufferImpl*>(buffer);
81 // static
82 size_t GpuMemoryBufferImpl::NumberOfPlanesForGpuMemoryBufferFormat(
83 Format format) {
84 switch (format) {
85 case ATC:
86 case ATCIA:
87 case DXT1:
88 case DXT5:
89 case ETC1:
90 case R_8:
91 case RGBA_4444:
92 case RGBA_8888:
93 case RGBX_8888:
94 case BGRA_8888:
95 return 1;
96 case YUV_420:
97 return 3;
99 NOTREACHED();
100 return 0;
103 // static
104 size_t GpuMemoryBufferImpl::SubsamplingFactor(Format format, int plane) {
105 switch (format) {
106 case ATC:
107 case ATCIA:
108 case DXT1:
109 case DXT5:
110 case ETC1:
111 case R_8:
112 case RGBA_4444:
113 case RGBA_8888:
114 case RGBX_8888:
115 case BGRA_8888:
116 return 1;
117 case YUV_420: {
118 static size_t factor[] = {1, 2, 2};
119 DCHECK_LT(static_cast<size_t>(plane), arraysize(factor));
120 return factor[plane];
123 NOTREACHED();
124 return 0;
127 // static
128 bool GpuMemoryBufferImpl::RowSizeInBytes(size_t width,
129 Format format,
130 int plane,
131 size_t* size_in_bytes) {
132 base::CheckedNumeric<size_t> checked_size = width;
133 switch (format) {
134 case ATCIA:
135 case DXT5:
136 DCHECK_EQ(plane, 0);
137 *size_in_bytes = width;
138 return true;
139 case ATC:
140 case DXT1:
141 case ETC1:
142 DCHECK_EQ(plane, 0);
143 DCHECK_EQ(width % 2, 0u);
144 *size_in_bytes = width / 2;
145 return true;
146 case R_8:
147 checked_size += 3;
148 if (!checked_size.IsValid())
149 return false;
150 *size_in_bytes = checked_size.ValueOrDie() & ~0x3;
151 return true;
152 case RGBA_4444:
153 checked_size *= 2;
154 if (!checked_size.IsValid())
155 return false;
156 *size_in_bytes = checked_size.ValueOrDie();
157 case RGBX_8888:
158 case RGBA_8888:
159 case BGRA_8888:
160 checked_size *= 4;
161 if (!checked_size.IsValid())
162 return false;
163 *size_in_bytes = checked_size.ValueOrDie();
164 return true;
165 case YUV_420:
166 DCHECK_EQ(width % 2, 0u);
167 *size_in_bytes = width / SubsamplingFactor(format, plane);
168 return true;
170 NOTREACHED();
171 return false;
174 // static
175 bool GpuMemoryBufferImpl::BufferSizeInBytes(const gfx::Size& size,
176 Format format,
177 size_t* size_in_bytes) {
178 base::CheckedNumeric<size_t> checked_size = 0;
179 size_t num_planes = NumberOfPlanesForGpuMemoryBufferFormat(format);
180 for (size_t i = 0; i < num_planes; ++i) {
181 size_t row_size_in_bytes = 0;
182 if (!RowSizeInBytes(size.width(), format, i, &row_size_in_bytes))
183 return false;
184 base::CheckedNumeric<size_t> checked_plane_size = row_size_in_bytes;
185 checked_plane_size *= size.height() / SubsamplingFactor(format, i);
186 if (!checked_plane_size.IsValid())
187 return false;
188 checked_size += checked_plane_size.ValueOrDie();
189 if (!checked_size.IsValid())
190 return false;
192 *size_in_bytes = checked_size.ValueOrDie();
193 return true;
196 gfx::GpuMemoryBuffer::Format GpuMemoryBufferImpl::GetFormat() const {
197 return format_;
200 bool GpuMemoryBufferImpl::IsMapped() const {
201 return mapped_;
204 gfx::GpuMemoryBufferId GpuMemoryBufferImpl::GetId() const {
205 return id_;
208 ClientBuffer GpuMemoryBufferImpl::AsClientBuffer() {
209 return reinterpret_cast<ClientBuffer>(this);
212 } // namespace content