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 bool GpuMemoryBufferImpl::StrideInBytes(size_t width
,
82 size_t* stride_in_bytes
) {
83 base::CheckedNumeric
<size_t> s
= width
;
87 *stride_in_bytes
= width
;
92 DCHECK_EQ(width
% 2, 0U);
97 *stride_in_bytes
= s
.ValueOrDie();
106 *stride_in_bytes
= s
.ValueOrDie();
115 size_t GpuMemoryBufferImpl::NumberOfPlanesForGpuMemoryBufferFormat(
116 gfx::GpuMemoryBuffer::Format format
) {
118 case gfx::GpuMemoryBuffer::Format::ATC
:
119 case gfx::GpuMemoryBuffer::Format::ATCIA
:
120 case gfx::GpuMemoryBuffer::Format::DXT1
:
121 case gfx::GpuMemoryBuffer::Format::DXT5
:
122 case gfx::GpuMemoryBuffer::Format::ETC1
:
123 case gfx::GpuMemoryBuffer::Format::RGBA_8888
:
124 case gfx::GpuMemoryBuffer::Format::RGBX_8888
:
125 case gfx::GpuMemoryBuffer::Format::BGRA_8888
:
133 gfx::GpuMemoryBuffer::Format
GpuMemoryBufferImpl::GetFormat() const {
137 bool GpuMemoryBufferImpl::IsMapped() const {
141 ClientBuffer
GpuMemoryBufferImpl::AsClientBuffer() {
142 return reinterpret_cast<ClientBuffer
>(this);
145 } // namespace content