We started redesigning GpuMemoryBuffer interface to handle multiple buffers [0].
[chromium-blink-merge.git] / content / common / gpu / client / gpu_memory_buffer_impl.cc
blobb8284ecb6048510ffb5ea3fb83e22a7d2ebd1ee3
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_buffer.h"
22 #endif
24 namespace content {
26 GpuMemoryBufferImpl::GpuMemoryBufferImpl(gfx::GpuMemoryBufferId id,
27 const gfx::Size& size,
28 Format 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) {
38 GpuMemoryBufferImpl::~GpuMemoryBufferImpl() {
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 Format format,
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);
56 #endif
57 #if defined(OS_ANDROID)
58 case gfx::SURFACE_TEXTURE_BUFFER:
59 return GpuMemoryBufferImplSurfaceTexture::CreateFromHandle(
60 handle, size, format, callback);
61 #endif
62 #if defined(USE_OZONE)
63 case gfx::OZONE_NATIVE_BUFFER:
64 return GpuMemoryBufferImplOzoneNativeBuffer::CreateFromHandle(
65 handle, size, format, callback);
66 #endif
67 default:
68 NOTREACHED();
69 return scoped_ptr<GpuMemoryBufferImpl>();
73 // static
74 GpuMemoryBufferImpl* GpuMemoryBufferImpl::FromClientBuffer(
75 ClientBuffer buffer) {
76 return reinterpret_cast<GpuMemoryBufferImpl*>(buffer);
79 // static
80 size_t GpuMemoryBufferImpl::NumberOfPlanesForGpuMemoryBufferFormat(
81 Format format) {
82 switch (format) {
83 case ATC:
84 case ATCIA:
85 case DXT1:
86 case DXT5:
87 case ETC1:
88 case RGBA_8888:
89 case RGBX_8888:
90 case BGRA_8888:
91 return 1;
92 case YUV_420:
93 return 3;
95 NOTREACHED();
96 return 0;
99 // static
100 bool GpuMemoryBufferImpl::StrideInBytes(size_t width,
101 Format format,
102 int plane,
103 size_t* stride_in_bytes) {
104 switch (format) {
105 case ATCIA:
106 case DXT5:
107 DCHECK_EQ(plane, 0);
108 *stride_in_bytes = width;
109 return true;
110 case ATC:
111 case DXT1:
112 case ETC1:
113 DCHECK_EQ(plane, 0);
114 DCHECK_EQ(width % 2, 0U);
115 *stride_in_bytes = width / 2;
116 return true;
117 case RGBA_8888:
118 case RGBX_8888:
119 case BGRA_8888: {
120 base::CheckedNumeric<size_t> s = width;
121 DCHECK_EQ(plane, 0);
122 s *= 4;
123 if (!s.IsValid())
124 return false;
125 *stride_in_bytes = s.ValueOrDie();
126 return true;
128 case YUV_420: {
129 DCHECK_EQ(width % 2, 0u);
130 *stride_in_bytes = width / SubsamplingFactor(format, plane);
131 return true;
134 NOTREACHED();
135 return false;
138 // static
139 size_t GpuMemoryBufferImpl::SubsamplingFactor(
140 Format format,
141 int plane) {
142 switch (format) {
143 case ATC:
144 case ATCIA:
145 case DXT1:
146 case DXT5:
147 case ETC1:
148 case RGBA_8888:
149 case RGBX_8888:
150 case BGRA_8888:
151 return 1;
152 case YUV_420: {
153 static size_t factor[] = {1, 2, 2};
154 DCHECK_LT(static_cast<size_t>(plane), arraysize(factor));
155 return factor[plane];
158 NOTREACHED();
159 return 0;
162 gfx::GpuMemoryBuffer::Format GpuMemoryBufferImpl::GetFormat() const {
163 return format_;
166 bool GpuMemoryBufferImpl::IsMapped() const {
167 return mapped_;
170 ClientBuffer GpuMemoryBufferImpl::AsClientBuffer() {
171 return reinterpret_cast<ClientBuffer>(this);
174 } // namespace content