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
,
29 const DestructionCallback
& callback
)
35 destruction_sync_point_(0) {
38 GpuMemoryBufferImpl::~GpuMemoryBufferImpl() {
40 callback_
.Run(destruction_sync_point_
);
44 scoped_ptr
<GpuMemoryBufferImpl
> GpuMemoryBufferImpl::CreateFromHandle(
45 const gfx::GpuMemoryBufferHandle
& handle
,
46 const gfx::Size
& size
,
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
);
59 #if defined(OS_ANDROID)
60 case gfx::SURFACE_TEXTURE_BUFFER
:
61 return GpuMemoryBufferImplSurfaceTexture::CreateFromHandle(
62 handle
, size
, format
, callback
);
64 #if defined(USE_OZONE)
65 case gfx::OZONE_NATIVE_PIXMAP
:
66 return GpuMemoryBufferImplOzoneNativePixmap::CreateFromHandle(
67 handle
, size
, format
, usage
, callback
);
76 GpuMemoryBufferImpl
* GpuMemoryBufferImpl::FromClientBuffer(
77 ClientBuffer buffer
) {
78 return reinterpret_cast<GpuMemoryBufferImpl
*>(buffer
);
82 size_t GpuMemoryBufferImpl::NumberOfPlanesForGpuMemoryBufferFormat(
104 size_t GpuMemoryBufferImpl::SubsamplingFactor(Format format
, int plane
) {
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
,
131 size_t* size_in_bytes
) {
132 base::CheckedNumeric
<size_t> checked_size
= width
;
137 *size_in_bytes
= width
;
143 DCHECK_EQ(width
% 2, 0u);
144 *size_in_bytes
= width
/ 2;
148 if (!checked_size
.IsValid())
150 *size_in_bytes
= checked_size
.ValueOrDie() & ~0x3;
154 if (!checked_size
.IsValid())
156 *size_in_bytes
= checked_size
.ValueOrDie();
161 if (!checked_size
.IsValid())
163 *size_in_bytes
= checked_size
.ValueOrDie();
166 DCHECK_EQ(width
% 2, 0u);
167 *size_in_bytes
= width
/ SubsamplingFactor(format
, plane
);
175 bool GpuMemoryBufferImpl::BufferSizeInBytes(const gfx::Size
& size
,
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::GpuMemoryBuffer::Format
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