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/mus/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() {}
25 scoped_ptr
<gfx::GpuMemoryBuffer
> MojoGpuMemoryBufferImpl::Create(
26 const gfx::Size
& size
,
27 gfx::BufferFormat format
,
28 gfx::BufferUsage usage
) {
29 size_t bytes
= gfx::BufferSizeForBufferFormat(size
, format
);
30 scoped_ptr
<base::SharedMemory
> shared_memory(new base::SharedMemory
);
31 if (!shared_memory
->CreateAnonymous(bytes
))
33 return make_scoped_ptr
<gfx::GpuMemoryBuffer
>(
34 new MojoGpuMemoryBufferImpl(size
, format
, shared_memory
.Pass()));
37 MojoGpuMemoryBufferImpl
* MojoGpuMemoryBufferImpl::FromClientBuffer(
38 ClientBuffer buffer
) {
39 return reinterpret_cast<MojoGpuMemoryBufferImpl
*>(buffer
);
42 const unsigned char* MojoGpuMemoryBufferImpl::GetMemory() const {
43 return static_cast<const unsigned char*>(shared_memory_
->memory());
46 bool MojoGpuMemoryBufferImpl::Map(void** data
) {
48 if (!shared_memory_
->Map(gfx::BufferSizeForBufferFormat(size_
, format_
)))
53 static_cast<int>(gfx::NumberOfPlanesForBufferFormat(format_
));
54 for (int i
= 0; i
< num_planes
; ++i
) {
55 data
[i
] = reinterpret_cast<uint8
*>(shared_memory_
->memory()) + offset
;
57 gfx::RowSizeForBufferFormat(size_
.width(), format_
, i
) *
58 (size_
.height() / gfx::SubsamplingFactorForBufferFormat(format_
, i
));
63 void MojoGpuMemoryBufferImpl::Unmap() {
65 shared_memory_
->Unmap();
69 bool MojoGpuMemoryBufferImpl::IsMapped() const {
73 gfx::BufferFormat
MojoGpuMemoryBufferImpl::GetFormat() const {
77 void MojoGpuMemoryBufferImpl::GetStride(int* stride
) const {
79 static_cast<int>(gfx::NumberOfPlanesForBufferFormat(format_
));
80 for (int i
= 0; i
< num_planes
; ++i
)
81 stride
[i
] = base::checked_cast
<int>(
82 gfx::RowSizeForBufferFormat(size_
.width(), format_
, i
));
85 gfx::GpuMemoryBufferId
MojoGpuMemoryBufferImpl::GetId() const {
86 return gfx::GpuMemoryBufferId(0);
89 gfx::GpuMemoryBufferHandle
MojoGpuMemoryBufferImpl::GetHandle() const {
90 gfx::GpuMemoryBufferHandle handle
;
91 handle
.type
= gfx::SHARED_MEMORY_BUFFER
;
92 handle
.handle
= shared_memory_
->handle();
96 ClientBuffer
MojoGpuMemoryBufferImpl::AsClientBuffer() {
97 return reinterpret_cast<ClientBuffer
>(this);