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_buffer.h"
26 GpuMemoryBufferImpl::GpuMemoryBufferImpl(gfx::GpuMemoryBufferId id
,
27 const gfx::Size
& size
,
29 const DestructionCallback
& callback
)
35 destruction_sync_point_(0) {
38 GpuMemoryBufferImpl::~GpuMemoryBufferImpl() {
39 callback_
.Run(destruction_sync_point_
);
43 scoped_ptr
<GpuMemoryBufferImpl
> GpuMemoryBufferImpl::CreateFromHandle(
44 const gfx::GpuMemoryBufferHandle
& handle
,
45 const gfx::Size
& size
,
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
);
57 #if defined(OS_ANDROID)
58 case gfx::SURFACE_TEXTURE_BUFFER
:
59 return GpuMemoryBufferImplSurfaceTexture::CreateFromHandle(
60 handle
, size
, format
, callback
);
62 #if defined(USE_OZONE)
63 case gfx::OZONE_NATIVE_BUFFER
:
64 return GpuMemoryBufferImplOzoneNativeBuffer::CreateFromHandle(
65 handle
, size
, format
, callback
);
69 return scoped_ptr
<GpuMemoryBufferImpl
>();
74 GpuMemoryBufferImpl
* GpuMemoryBufferImpl::FromClientBuffer(
75 ClientBuffer buffer
) {
76 return reinterpret_cast<GpuMemoryBufferImpl
*>(buffer
);
80 size_t GpuMemoryBufferImpl::NumberOfPlanesForGpuMemoryBufferFormat(
100 bool GpuMemoryBufferImpl::StrideInBytes(size_t width
,
103 size_t* stride_in_bytes
) {
108 *stride_in_bytes
= width
;
114 DCHECK_EQ(width
% 2, 0U);
115 *stride_in_bytes
= width
/ 2;
120 base::CheckedNumeric
<size_t> s
= width
;
125 *stride_in_bytes
= s
.ValueOrDie();
129 DCHECK_EQ(width
% 2, 0u);
130 *stride_in_bytes
= width
/ SubsamplingFactor(format
, plane
);
139 size_t GpuMemoryBufferImpl::SubsamplingFactor(
153 static size_t factor
[] = {1, 2, 2};
154 DCHECK_LT(static_cast<size_t>(plane
), arraysize(factor
));
155 return factor
[plane
];
162 gfx::GpuMemoryBuffer::Format
GpuMemoryBufferImpl::GetFormat() const {
166 bool GpuMemoryBufferImpl::IsMapped() const {
170 ClientBuffer
GpuMemoryBufferImpl::AsClientBuffer() {
171 return reinterpret_cast<ClientBuffer
>(this);
174 } // namespace content