1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
|
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/view_manager/gles2/mojo_gpu_memory_buffer.h"
#include "base/logging.h"
#include "base/memory/shared_memory.h"
#include "base/numerics/safe_conversions.h"
namespace gles2 {
namespace {
int NumberOfPlanesForGpuMemoryBufferFormat(gfx::BufferFormat format) {
switch (format) {
case gfx::BufferFormat::ATC:
case gfx::BufferFormat::ATCIA:
case gfx::BufferFormat::DXT1:
case gfx::BufferFormat::DXT5:
case gfx::BufferFormat::ETC1:
case gfx::BufferFormat::R_8:
case gfx::BufferFormat::RGBA_4444:
case gfx::BufferFormat::RGBA_8888:
case gfx::BufferFormat::RGBX_8888:
case gfx::BufferFormat::BGRA_8888:
return 1;
case gfx::BufferFormat::YUV_420:
return 3;
}
NOTREACHED();
return 0;
}
size_t SubsamplingFactor(gfx::BufferFormat format, int plane) {
switch (format) {
case gfx::BufferFormat::ATC:
case gfx::BufferFormat::ATCIA:
case gfx::BufferFormat::DXT1:
case gfx::BufferFormat::DXT5:
case gfx::BufferFormat::ETC1:
case gfx::BufferFormat::R_8:
case gfx::BufferFormat::RGBA_4444:
case gfx::BufferFormat::RGBA_8888:
case gfx::BufferFormat::RGBX_8888:
case gfx::BufferFormat::BGRA_8888:
return 1;
case gfx::BufferFormat::YUV_420: {
static size_t factor[] = {1, 2, 2};
DCHECK_LT(static_cast<size_t>(plane), arraysize(factor));
return factor[plane];
}
}
NOTREACHED();
return 0;
}
size_t StrideInBytes(size_t width, gfx::BufferFormat format, int plane) {
switch (format) {
case gfx::BufferFormat::ATCIA:
case gfx::BufferFormat::DXT5:
DCHECK_EQ(plane, 0);
return width;
case gfx::BufferFormat::ATC:
case gfx::BufferFormat::DXT1:
case gfx::BufferFormat::ETC1:
DCHECK_EQ(plane, 0);
DCHECK_EQ(width % 2, 0u);
return width / 2;
case gfx::BufferFormat::R_8:
return (width + 3) & ~0x3;
case gfx::BufferFormat::RGBA_4444:
DCHECK_EQ(plane, 0);
return width * 2;
case gfx::BufferFormat::RGBA_8888:
case gfx::BufferFormat::RGBX_8888:
case gfx::BufferFormat::BGRA_8888:
DCHECK_EQ(plane, 0);
return width * 4;
case gfx::BufferFormat::YUV_420:
return width / SubsamplingFactor(format, plane);
}
NOTREACHED();
return 0;
}
size_t BufferSizeInBytes(const gfx::Size& size, gfx::BufferFormat format) {
size_t size_in_bytes = 0;
int num_planes = NumberOfPlanesForGpuMemoryBufferFormat(format);
for (int i = 0; i < num_planes; ++i) {
size_in_bytes += StrideInBytes(size.width(), format, i) *
(size.height() / SubsamplingFactor(format, i));
}
return size_in_bytes;
}
} // namespace
MojoGpuMemoryBufferImpl::MojoGpuMemoryBufferImpl(
const gfx::Size& size,
gfx::BufferFormat format,
scoped_ptr<base::SharedMemory> shared_memory)
: size_(size),
format_(format),
shared_memory_(shared_memory.Pass()),
mapped_(false) {}
MojoGpuMemoryBufferImpl::~MojoGpuMemoryBufferImpl() {
}
scoped_ptr<gfx::GpuMemoryBuffer> MojoGpuMemoryBufferImpl::Create(
const gfx::Size& size,
gfx::BufferFormat format,
gfx::BufferUsage usage) {
size_t bytes = BufferSizeInBytes(size, format);
scoped_ptr<base::SharedMemory> shared_memory(new base::SharedMemory);
if (!shared_memory->CreateAnonymous(bytes))
return nullptr;
return make_scoped_ptr<gfx::GpuMemoryBuffer>(
new MojoGpuMemoryBufferImpl(size, format, shared_memory.Pass()));
}
MojoGpuMemoryBufferImpl* MojoGpuMemoryBufferImpl::FromClientBuffer(
ClientBuffer buffer) {
return reinterpret_cast<MojoGpuMemoryBufferImpl*>(buffer);
}
bool MojoGpuMemoryBufferImpl::Map(void** data) {
DCHECK(!mapped_);
if (!shared_memory_->Map(BufferSizeInBytes(size_, format_)))
return false;
mapped_ = true;
size_t offset = 0;
int num_planes = NumberOfPlanesForGpuMemoryBufferFormat(format_);
for (int i = 0; i < num_planes; ++i) {
data[i] = reinterpret_cast<uint8*>(shared_memory_->memory()) + offset;
offset += StrideInBytes(size_.width(), format_, i) *
(size_.height() / SubsamplingFactor(format_, i));
}
return true;
}
void MojoGpuMemoryBufferImpl::Unmap() {
DCHECK(mapped_);
shared_memory_->Unmap();
mapped_ = false;
}
bool MojoGpuMemoryBufferImpl::IsMapped() const {
return mapped_;
}
gfx::BufferFormat MojoGpuMemoryBufferImpl::GetFormat() const {
return format_;
}
void MojoGpuMemoryBufferImpl::GetStride(int* stride) const {
int num_planes = NumberOfPlanesForGpuMemoryBufferFormat(format_);
for (int i = 0; i < num_planes; ++i)
stride[i] =
base::checked_cast<int>(StrideInBytes(size_.width(), format_, i));
}
gfx::GpuMemoryBufferId MojoGpuMemoryBufferImpl::GetId() const {
return 0;
}
gfx::GpuMemoryBufferHandle MojoGpuMemoryBufferImpl::GetHandle() const {
gfx::GpuMemoryBufferHandle handle;
handle.type = gfx::SHARED_MEMORY_BUFFER;
handle.handle = shared_memory_->handle();
return handle;
}
ClientBuffer MojoGpuMemoryBufferImpl::AsClientBuffer() {
return reinterpret_cast<ClientBuffer>(this);
}
} // namespace gles2
|