1 // Copyright 2015 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 "components/view_manager/gles2/mojo_gpu_memory_buffer.h"
7 #include "base/logging.h"
8 #include "base/memory/shared_memory.h"
9 #include "base/numerics/safe_conversions.h"
10 #include "ui/gfx/buffer_format_util.h"
14 MojoGpuMemoryBufferImpl::MojoGpuMemoryBufferImpl(
15 const gfx::Size
& size
,
16 gfx::BufferFormat format
,
17 scoped_ptr
<base::SharedMemory
> shared_memory
)
20 shared_memory_(shared_memory
.Pass()),
23 MojoGpuMemoryBufferImpl::~MojoGpuMemoryBufferImpl() {
26 scoped_ptr
<gfx::GpuMemoryBuffer
> MojoGpuMemoryBufferImpl::Create(
27 const gfx::Size
& size
,
28 gfx::BufferFormat format
,
29 gfx::BufferUsage usage
) {
30 size_t bytes
= gfx::BufferSizeForBufferFormat(size
, format
);
31 scoped_ptr
<base::SharedMemory
> shared_memory(new base::SharedMemory
);
32 if (!shared_memory
->CreateAnonymous(bytes
))
34 return make_scoped_ptr
<gfx::GpuMemoryBuffer
>(
35 new MojoGpuMemoryBufferImpl(size
, format
, shared_memory
.Pass()));
38 MojoGpuMemoryBufferImpl
* MojoGpuMemoryBufferImpl::FromClientBuffer(
39 ClientBuffer buffer
) {
40 return reinterpret_cast<MojoGpuMemoryBufferImpl
*>(buffer
);
43 const unsigned char* MojoGpuMemoryBufferImpl::GetMemory() const {
44 return static_cast<const unsigned char*>(shared_memory_
->memory());
47 bool MojoGpuMemoryBufferImpl::Map(void** data
) {
49 if (!shared_memory_
->Map(gfx::BufferSizeForBufferFormat(size_
, format_
)))
53 int num_planes
= static_cast<int>(
54 gfx::NumberOfPlanesForBufferFormat(format_
));
55 for (int i
= 0; i
< num_planes
; ++i
) {
56 data
[i
] = reinterpret_cast<uint8
*>(shared_memory_
->memory()) + offset
;
58 gfx::RowSizeForBufferFormat(size_
.width(), format_
, i
) *
59 (size_
.height() / gfx::SubsamplingFactorForBufferFormat(format_
, i
));
64 void MojoGpuMemoryBufferImpl::Unmap() {
66 shared_memory_
->Unmap();
70 bool MojoGpuMemoryBufferImpl::IsMapped() const {
74 gfx::BufferFormat
MojoGpuMemoryBufferImpl::GetFormat() const {
78 void MojoGpuMemoryBufferImpl::GetStride(int* stride
) const {
79 int num_planes
= static_cast<int>(
80 gfx::NumberOfPlanesForBufferFormat(format_
));
81 for (int i
= 0; i
< num_planes
; ++i
)
82 stride
[i
] = base::checked_cast
<int>(
83 gfx::RowSizeForBufferFormat(size_
.width(), format_
, i
));
86 gfx::GpuMemoryBufferId
MojoGpuMemoryBufferImpl::GetId() const {
87 return gfx::GpuMemoryBufferId(0);
90 gfx::GpuMemoryBufferHandle
MojoGpuMemoryBufferImpl::GetHandle() const {
91 gfx::GpuMemoryBufferHandle handle
;
92 handle
.type
= gfx::SHARED_MEMORY_BUFFER
;
93 handle
.handle
= shared_memory_
->handle();
97 ClientBuffer
MojoGpuMemoryBufferImpl::AsClientBuffer() {
98 return reinterpret_cast<ClientBuffer
>(this);