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"
16 #if defined(OS_ANDROID)
17 #include "content/common/gpu/client/gpu_memory_buffer_impl_surface_texture.h"
20 #if defined(USE_OZONE)
21 #include "content/common/gpu/client/gpu_memory_buffer_impl_ozone_native_pixmap.h"
26 GpuMemoryBufferImpl::GpuMemoryBufferImpl(gfx::GpuMemoryBufferId id
,
27 const gfx::Size
& size
,
28 gfx::BufferFormat format
,
29 const DestructionCallback
& callback
)
35 destruction_sync_point_(0) {}
37 GpuMemoryBufferImpl::~GpuMemoryBufferImpl() {
39 callback_
.Run(destruction_sync_point_
);
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
);
58 #if defined(OS_ANDROID)
59 case gfx::SURFACE_TEXTURE_BUFFER
:
60 return GpuMemoryBufferImplSurfaceTexture::CreateFromHandle(
61 handle
, size
, format
, callback
);
63 #if defined(USE_OZONE)
64 case gfx::OZONE_NATIVE_PIXMAP
:
65 return GpuMemoryBufferImplOzoneNativePixmap::CreateFromHandle(
66 handle
, size
, format
, usage
, callback
);
75 GpuMemoryBufferImpl
* GpuMemoryBufferImpl::FromClientBuffer(
76 ClientBuffer buffer
) {
77 return reinterpret_cast<GpuMemoryBufferImpl
*>(buffer
);
81 size_t GpuMemoryBufferImpl::NumberOfPlanesForGpuMemoryBufferFormat(
82 gfx::BufferFormat 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
:
95 case gfx::BufferFormat::YUV_420
:
103 size_t GpuMemoryBufferImpl::SubsamplingFactor(gfx::BufferFormat 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
:
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
];
128 bool GpuMemoryBufferImpl::RowSizeInBytes(size_t width
,
129 gfx::BufferFormat format
,
131 size_t* size_in_bytes
) {
132 base::CheckedNumeric
<size_t> checked_size
= width
;
134 case gfx::BufferFormat::ATCIA
:
135 case gfx::BufferFormat::DXT5
:
137 *size_in_bytes
= width
;
139 case gfx::BufferFormat::ATC
:
140 case gfx::BufferFormat::DXT1
:
141 case gfx::BufferFormat::ETC1
:
143 DCHECK_EQ(width
% 2, 0u);
144 *size_in_bytes
= width
/ 2;
146 case gfx::BufferFormat::R_8
:
148 if (!checked_size
.IsValid())
150 *size_in_bytes
= checked_size
.ValueOrDie() & ~0x3;
152 case gfx::BufferFormat::RGBA_4444
:
154 if (!checked_size
.IsValid())
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
:
161 if (!checked_size
.IsValid())
163 *size_in_bytes
= checked_size
.ValueOrDie();
165 case gfx::BufferFormat::YUV_420
:
166 DCHECK_EQ(width
% 2, 0u);
167 *size_in_bytes
= width
/ SubsamplingFactor(format
, plane
);
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
))
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())
188 checked_size
+= checked_plane_size
.ValueOrDie();
189 if (!checked_size
.IsValid())
192 *size_in_bytes
= checked_size
.ValueOrDie();
196 gfx::BufferFormat
GpuMemoryBufferImpl::GetFormat() const {
200 bool GpuMemoryBufferImpl::IsMapped() const {
204 gfx::GpuMemoryBufferId
GpuMemoryBufferImpl::GetId() const {
208 ClientBuffer
GpuMemoryBufferImpl::AsClientBuffer() {
209 return reinterpret_cast<ClientBuffer
>(this);
212 } // namespace content