Add 'did_proceed' and 'repeat_visit' to ClientMalwareReportRequest to track CTR.
[chromium-blink-merge.git] / components / mus / gles2 / mojo_gpu_memory_buffer.cc
blobfac4629e29e78ae62ddfb3364c6c165ed04f54dd
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"
12 namespace mus {
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() {}
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))
32 return nullptr;
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) {
47 DCHECK(!mapped_);
48 if (!shared_memory_->Map(gfx::BufferSizeForBufferFormat(size_, format_)))
49 return false;
50 mapped_ = true;
51 size_t offset = 0;
52 int num_planes =
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;
56 offset +=
57 gfx::RowSizeForBufferFormat(size_.width(), format_, i) *
58 (size_.height() / gfx::SubsamplingFactorForBufferFormat(format_, i));
60 return true;
63 void MojoGpuMemoryBufferImpl::Unmap() {
64 DCHECK(mapped_);
65 shared_memory_->Unmap();
66 mapped_ = false;
69 bool MojoGpuMemoryBufferImpl::IsMapped() const {
70 return mapped_;
73 gfx::BufferFormat MojoGpuMemoryBufferImpl::GetFormat() const {
74 return format_;
77 void MojoGpuMemoryBufferImpl::GetStride(int* stride) const {
78 int num_planes =
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();
93 return handle;
96 ClientBuffer MojoGpuMemoryBufferImpl::AsClientBuffer() {
97 return reinterpret_cast<ClientBuffer>(this);
100 } // namespace mus