GPU workaround to simulate Out of Memory errors with large textures
[chromium-blink-merge.git] / content / common / gpu / client / gpu_memory_buffer_impl.cc
blob208261dd0831e940f8f858b63a4169888c706db8
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 bool GpuMemoryBufferImpl::StrideInBytes(size_t width,
81 Format format,
82 size_t* stride_in_bytes) {
83 base::CheckedNumeric<size_t> s = width;
84 switch (format) {
85 case ATCIA:
86 case DXT5:
87 *stride_in_bytes = width;
88 return true;
89 case ATC:
90 case DXT1:
91 case ETC1:
92 DCHECK_EQ(width % 2, 0U);
93 s /= 2;
94 if (!s.IsValid())
95 return false;
97 *stride_in_bytes = s.ValueOrDie();
98 return true;
99 case RGBA_8888:
100 case RGBX_8888:
101 case BGRA_8888:
102 s *= 4;
103 if (!s.IsValid())
104 return false;
106 *stride_in_bytes = s.ValueOrDie();
107 return true;
110 NOTREACHED();
111 return false;
114 // static
115 size_t GpuMemoryBufferImpl::NumberOfPlanesForGpuMemoryBufferFormat(
116 gfx::GpuMemoryBuffer::Format format) {
117 switch (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:
126 return 1;
127 default:
128 NOTREACHED();
129 return 0;
133 gfx::GpuMemoryBuffer::Format GpuMemoryBufferImpl::GetFormat() const {
134 return format_;
137 bool GpuMemoryBufferImpl::IsMapped() const {
138 return mapped_;
141 ClientBuffer GpuMemoryBufferImpl::AsClientBuffer() {
142 return reinterpret_cast<ClientBuffer>(this);
145 } // namespace content