Add ICU message format support
[chromium-blink-merge.git] / content / common / gpu / client / gpu_memory_buffer_impl.cc
blob928faa9df36578d5cf828c2b6b1a1d9dacf2de37
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 gfx::BufferFormat 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) {}
37 GpuMemoryBufferImpl::~GpuMemoryBufferImpl() {
38 DCHECK(!mapped_);
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 gfx::BufferFormat format,
47 gfx::BufferUsage usage,
48 const DestructionCallback& callback) {
49 switch (handle.type) {
50 case gfx::SHARED_MEMORY_BUFFER:
51 return GpuMemoryBufferImplSharedMemory::CreateFromHandle(
52 handle, size, format, callback);
53 #if defined(OS_MACOSX)
54 case gfx::IO_SURFACE_BUFFER:
55 return GpuMemoryBufferImplIOSurface::CreateFromHandle(
56 handle, size, format, usage, callback);
57 #endif
58 #if defined(OS_ANDROID)
59 case gfx::SURFACE_TEXTURE_BUFFER:
60 return GpuMemoryBufferImplSurfaceTexture::CreateFromHandle(
61 handle, size, format, callback);
62 #endif
63 #if defined(USE_OZONE)
64 case gfx::OZONE_NATIVE_PIXMAP:
65 return GpuMemoryBufferImplOzoneNativePixmap::CreateFromHandle(
66 handle, size, format, usage, callback);
67 #endif
68 default:
69 NOTREACHED();
70 return nullptr;
74 // static
75 GpuMemoryBufferImpl* GpuMemoryBufferImpl::FromClientBuffer(
76 ClientBuffer buffer) {
77 return reinterpret_cast<GpuMemoryBufferImpl*>(buffer);
80 // static
81 size_t GpuMemoryBufferImpl::NumberOfPlanesForGpuMemoryBufferFormat(
82 gfx::BufferFormat format) {
83 switch (format) {
84 case gfx::BufferFormat::ATC:
85 case gfx::BufferFormat::ATCIA:
86 case gfx::BufferFormat::DXT1:
87 case gfx::BufferFormat::DXT5:
88 case gfx::BufferFormat::ETC1:
89 case gfx::BufferFormat::R_8:
90 case gfx::BufferFormat::RGBA_4444:
91 case gfx::BufferFormat::RGBA_8888:
92 case gfx::BufferFormat::RGBX_8888:
93 case gfx::BufferFormat::BGRA_8888:
94 return 1;
95 case gfx::BufferFormat::YUV_420:
96 return 3;
98 NOTREACHED();
99 return 0;
102 // static
103 size_t GpuMemoryBufferImpl::SubsamplingFactor(gfx::BufferFormat format,
104 int plane) {
105 switch (format) {
106 case gfx::BufferFormat::ATC:
107 case gfx::BufferFormat::ATCIA:
108 case gfx::BufferFormat::DXT1:
109 case gfx::BufferFormat::DXT5:
110 case gfx::BufferFormat::ETC1:
111 case gfx::BufferFormat::R_8:
112 case gfx::BufferFormat::RGBA_4444:
113 case gfx::BufferFormat::RGBA_8888:
114 case gfx::BufferFormat::RGBX_8888:
115 case gfx::BufferFormat::BGRA_8888:
116 return 1;
117 case gfx::BufferFormat::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 gfx::BufferFormat format,
130 int plane,
131 size_t* size_in_bytes) {
132 base::CheckedNumeric<size_t> checked_size = width;
133 switch (format) {
134 case gfx::BufferFormat::ATCIA:
135 case gfx::BufferFormat::DXT5:
136 DCHECK_EQ(plane, 0);
137 *size_in_bytes = width;
138 return true;
139 case gfx::BufferFormat::ATC:
140 case gfx::BufferFormat::DXT1:
141 case gfx::BufferFormat::ETC1:
142 DCHECK_EQ(plane, 0);
143 DCHECK_EQ(width % 2, 0u);
144 *size_in_bytes = width / 2;
145 return true;
146 case gfx::BufferFormat::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 gfx::BufferFormat::RGBA_4444:
153 checked_size *= 2;
154 if (!checked_size.IsValid())
155 return false;
156 *size_in_bytes = checked_size.ValueOrDie();
157 case gfx::BufferFormat::RGBX_8888:
158 case gfx::BufferFormat::RGBA_8888:
159 case gfx::BufferFormat::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 gfx::BufferFormat::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 gfx::BufferFormat 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::BufferFormat 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