OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "components/view_manager/gles2/mojo_gpu_memory_buffer.h" | |
6 | |
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" | |
11 | |
12 namespace gles2 { | |
13 | |
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) {} | |
22 | |
23 MojoGpuMemoryBufferImpl::~MojoGpuMemoryBufferImpl() { | |
24 } | |
25 | |
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())); | |
36 } | |
37 | |
38 MojoGpuMemoryBufferImpl* MojoGpuMemoryBufferImpl::FromClientBuffer( | |
39 ClientBuffer buffer) { | |
40 return reinterpret_cast<MojoGpuMemoryBufferImpl*>(buffer); | |
41 } | |
42 | |
43 const unsigned char* MojoGpuMemoryBufferImpl::GetMemory() const { | |
44 return static_cast<const unsigned char*>(shared_memory_->memory()); | |
45 } | |
46 | |
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)); | |
60 } | |
61 return true; | |
62 } | |
63 | |
64 void MojoGpuMemoryBufferImpl::Unmap() { | |
65 DCHECK(mapped_); | |
66 shared_memory_->Unmap(); | |
67 mapped_ = false; | |
68 } | |
69 | |
70 bool MojoGpuMemoryBufferImpl::IsMapped() const { | |
71 return mapped_; | |
72 } | |
73 | |
74 gfx::BufferFormat MojoGpuMemoryBufferImpl::GetFormat() const { | |
75 return format_; | |
76 } | |
77 | |
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)); | |
84 } | |
85 | |
86 gfx::GpuMemoryBufferId MojoGpuMemoryBufferImpl::GetId() const { | |
87 return gfx::GpuMemoryBufferId(0); | |
88 } | |
89 | |
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; | |
95 } | |
96 | |
97 ClientBuffer MojoGpuMemoryBufferImpl::AsClientBuffer() { | |
98 return reinterpret_cast<ClientBuffer>(this); | |
99 } | |
100 | |
101 } // namespace gles2 | |
OLD | NEW |