cygprofile: increase timeouts to allow showing web contents
[chromium-blink-merge.git] / components / view_manager / gles2 / mojo_gpu_memory_buffer.cc
blob300b1c4663021bbe4bd92a3b656389e622d3ddb0
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"
12 namespace gles2 {
14 MojoGpuMemoryBufferImpl::MojoGpuMemoryBufferImpl(
15 const gfx::Size& size,
16 gfx::BufferFormat format,
17 scoped_ptr<base::SharedMemory> shared_memory)
18 : size_(size),
19 format_(format),
20 shared_memory_(shared_memory.Pass()),
21 mapped_(false) {}
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))
33 return nullptr;
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) {
48 DCHECK(!mapped_);
49 if (!shared_memory_->Map(gfx::BufferSizeForBufferFormat(size_, format_)))
50 return false;
51 mapped_ = true;
52 size_t offset = 0;
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;
57 offset +=
58 gfx::RowSizeForBufferFormat(size_.width(), format_, i) *
59 (size_.height() / gfx::SubsamplingFactorForBufferFormat(format_, i));
61 return true;
64 void MojoGpuMemoryBufferImpl::Unmap() {
65 DCHECK(mapped_);
66 shared_memory_->Unmap();
67 mapped_ = false;
70 bool MojoGpuMemoryBufferImpl::IsMapped() const {
71 return mapped_;
74 gfx::BufferFormat MojoGpuMemoryBufferImpl::GetFormat() const {
75 return format_;
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();
94 return handle;
97 ClientBuffer MojoGpuMemoryBufferImpl::AsClientBuffer() {
98 return reinterpret_cast<ClientBuffer>(this);
101 } // namespace gles2