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/mus/gles2/mojo_gpu_memory_buffer.h" | |
6 | |
7 #include <stdint.h> | |
8 | |
9 #include "base/logging.h" | |
10 #include "base/memory/ptr_util.h" | |
11 #include "base/memory/shared_memory.h" | |
12 #include "base/numerics/safe_conversions.h" | |
13 #include "build/build_config.h" | |
14 #include "ui/gfx/buffer_format_util.h" | |
15 | |
16 namespace mus { | |
17 | |
18 MojoGpuMemoryBufferImpl::MojoGpuMemoryBufferImpl( | |
19 const gfx::Size& size, | |
20 gfx::BufferFormat format, | |
21 std::unique_ptr<base::SharedMemory> shared_memory) | |
22 : GpuMemoryBufferImpl(gfx::GenericSharedMemoryId(0), size, format), | |
23 shared_memory_(std::move(shared_memory)) {} | |
24 | |
25 // TODO(rjkroege): Support running a destructor callback as necessary. | |
26 MojoGpuMemoryBufferImpl::~MojoGpuMemoryBufferImpl() {} | |
27 | |
28 std::unique_ptr<gfx::GpuMemoryBuffer> MojoGpuMemoryBufferImpl::Create( | |
29 const gfx::Size& size, | |
30 gfx::BufferFormat format, | |
31 gfx::BufferUsage usage) { | |
32 size_t bytes = gfx::BufferSizeForBufferFormat(size, format); | |
33 std::unique_ptr<base::SharedMemory> shared_memory(new base::SharedMemory); | |
34 | |
35 if (!shared_memory->CreateAnonymous(bytes)) | |
36 return nullptr; | |
37 | |
38 return base::WrapUnique<gfx::GpuMemoryBuffer>( | |
39 new MojoGpuMemoryBufferImpl(size, format, std::move(shared_memory))); | |
40 } | |
41 | |
42 MojoGpuMemoryBufferImpl* MojoGpuMemoryBufferImpl::FromClientBuffer( | |
43 ClientBuffer buffer) { | |
44 return reinterpret_cast<MojoGpuMemoryBufferImpl*>(buffer); | |
45 } | |
46 | |
47 const unsigned char* MojoGpuMemoryBufferImpl::GetMemory() const { | |
48 return static_cast<const unsigned char*>(shared_memory_->memory()); | |
49 } | |
50 | |
51 bool MojoGpuMemoryBufferImpl::Map() { | |
52 DCHECK(!mapped_); | |
53 if (!shared_memory_->Map(gfx::BufferSizeForBufferFormat(size_, format_))) | |
54 return false; | |
55 mapped_ = true; | |
56 return true; | |
57 } | |
58 | |
59 void* MojoGpuMemoryBufferImpl::memory(size_t plane) { | |
60 DCHECK(mapped_); | |
61 DCHECK_LT(plane, gfx::NumberOfPlanesForBufferFormat(format_)); | |
62 return reinterpret_cast<uint8_t*>(shared_memory_->memory()) + | |
63 gfx::BufferOffsetForBufferFormat(size_, format_, plane); | |
64 } | |
65 | |
66 void MojoGpuMemoryBufferImpl::Unmap() { | |
67 DCHECK(mapped_); | |
68 shared_memory_->Unmap(); | |
69 mapped_ = false; | |
70 } | |
71 | |
72 int MojoGpuMemoryBufferImpl::stride(size_t plane) const { | |
73 DCHECK_LT(plane, gfx::NumberOfPlanesForBufferFormat(format_)); | |
74 return base::checked_cast<int>(gfx::RowSizeForBufferFormat( | |
75 size_.width(), format_, static_cast<int>(plane))); | |
76 } | |
77 | |
78 gfx::GpuMemoryBufferHandle MojoGpuMemoryBufferImpl::GetHandle() const { | |
79 gfx::GpuMemoryBufferHandle handle; | |
80 handle.type = gfx::SHARED_MEMORY_BUFFER; | |
81 handle.handle = shared_memory_->handle(); | |
82 handle.offset = 0; | |
83 handle.stride = static_cast<int32_t>( | |
84 gfx::RowSizeForBufferFormat(size_.width(), format_, 0)); | |
85 | |
86 return handle; | |
87 } | |
88 | |
89 gfx::GpuMemoryBufferType MojoGpuMemoryBufferImpl::GetBufferType() const { | |
90 return gfx::SHARED_MEMORY_BUFFER; | |
91 } | |
92 | |
93 } // namespace mus | |
OLD | NEW |