OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "services/ui/public/cpp/mojo_gpu_memory_buffer.h" | 5 #include "services/ui/public/cpp/mojo_gpu_memory_buffer.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
11 #include "base/memory/shared_memory.h" | 11 #include "base/memory/shared_memory.h" |
12 #include "base/numerics/safe_conversions.h" | 12 #include "base/numerics/safe_conversions.h" |
13 #include "build/build_config.h" | 13 #include "build/build_config.h" |
14 #include "mojo/public/cpp/system/buffer.h" | 14 #include "mojo/public/cpp/system/buffer.h" |
15 #include "mojo/public/cpp/system/platform_handle.h" | 15 #include "mojo/public/cpp/system/platform_handle.h" |
16 #include "ui/gfx/buffer_format_util.h" | 16 #include "ui/gfx/buffer_format_util.h" |
17 | 17 |
18 namespace ui { | 18 namespace ui { |
19 | 19 |
20 MojoGpuMemoryBufferImpl::MojoGpuMemoryBufferImpl( | |
21 const gfx::Size& size, | |
22 gfx::BufferFormat format, | |
23 std::unique_ptr<base::SharedMemory> shared_memory) | |
24 : GpuMemoryBufferImpl(gfx::GenericSharedMemoryId(0), size, format), | |
25 shared_memory_(std::move(shared_memory)) {} | |
26 | |
27 // TODO(rjkroege): Support running a destructor callback as necessary. | 20 // TODO(rjkroege): Support running a destructor callback as necessary. |
28 MojoGpuMemoryBufferImpl::~MojoGpuMemoryBufferImpl() {} | 21 MojoGpuMemoryBufferImpl::~MojoGpuMemoryBufferImpl() {} |
29 | 22 |
30 // static | 23 // static |
31 std::unique_ptr<gfx::GpuMemoryBuffer> MojoGpuMemoryBufferImpl::Create( | 24 std::unique_ptr<gfx::GpuMemoryBuffer> MojoGpuMemoryBufferImpl::Create( |
32 const gfx::Size& size, | 25 const gfx::Size& size, |
33 gfx::BufferFormat format, | 26 gfx::BufferFormat format, |
34 gfx::BufferUsage usage) { | 27 gfx::BufferUsage usage) { |
35 size_t bytes = gfx::BufferSizeForBufferFormat(size, format); | 28 size_t bytes = gfx::BufferSizeForBufferFormat(size, format); |
36 | 29 |
37 mojo::ScopedSharedBufferHandle handle = | 30 mojo::ScopedSharedBufferHandle handle = |
38 mojo::SharedBufferHandle::Create(bytes); | 31 mojo::SharedBufferHandle::Create(bytes); |
39 if (!handle.is_valid()) | 32 if (!handle.is_valid()) |
40 return nullptr; | 33 return nullptr; |
41 | 34 |
42 base::SharedMemoryHandle platform_handle; | 35 base::SharedMemoryHandle platform_handle; |
43 size_t shared_memory_size; | 36 size_t shared_memory_size; |
44 bool readonly; | 37 bool readonly; |
45 MojoResult result = mojo::UnwrapSharedMemoryHandle( | 38 MojoResult result = mojo::UnwrapSharedMemoryHandle( |
46 std::move(handle), &platform_handle, &shared_memory_size, &readonly); | 39 std::move(handle), &platform_handle, &shared_memory_size, &readonly); |
47 if (result != MOJO_RESULT_OK) | 40 if (result != MOJO_RESULT_OK) |
48 return nullptr; | 41 return nullptr; |
49 DCHECK_EQ(shared_memory_size, bytes); | 42 DCHECK_EQ(shared_memory_size, bytes); |
50 | 43 |
51 auto shared_memory = | 44 auto shared_memory = |
52 base::MakeUnique<base::SharedMemory>(platform_handle, readonly); | 45 base::MakeUnique<base::SharedMemory>(platform_handle, readonly); |
53 return base::MakeUnique<MojoGpuMemoryBufferImpl>( | 46 const int stride = base::checked_cast<int>( |
54 size, format, std::move(shared_memory)); | 47 gfx::RowSizeForBufferFormat(size.width(), format, 0)); |
| 48 return base::WrapUnique(new MojoGpuMemoryBufferImpl( |
| 49 size, format, std::move(shared_memory), 0, stride)); |
55 } | 50 } |
56 | 51 |
57 // static | 52 // static |
58 std::unique_ptr<gfx::GpuMemoryBuffer> MojoGpuMemoryBufferImpl::CreateFromHandle( | 53 std::unique_ptr<gfx::GpuMemoryBuffer> MojoGpuMemoryBufferImpl::CreateFromHandle( |
59 const gfx::GpuMemoryBufferHandle& handle, | 54 const gfx::GpuMemoryBufferHandle& handle, |
60 const gfx::Size& size, | 55 const gfx::Size& size, |
61 gfx::BufferFormat format, | 56 gfx::BufferFormat format, |
62 gfx::BufferUsage usage) { | 57 gfx::BufferUsage usage) { |
63 DCHECK_EQ(handle.type, gfx::SHARED_MEMORY_BUFFER); | 58 DCHECK_EQ(handle.type, gfx::SHARED_MEMORY_BUFFER); |
64 DCHECK(base::SharedMemory::IsHandleValid(handle.handle)); | 59 DCHECK(base::SharedMemory::IsHandleValid(handle.handle)); |
65 const bool readonly = false; | 60 const bool readonly = false; |
66 auto shared_memory = | 61 auto shared_memory = |
67 base::MakeUnique<base::SharedMemory>(handle.handle, readonly); | 62 base::MakeUnique<base::SharedMemory>(handle.handle, readonly); |
68 return base::MakeUnique<MojoGpuMemoryBufferImpl>( | 63 return base::WrapUnique(new MojoGpuMemoryBufferImpl( |
69 size, format, std::move(shared_memory)); | 64 size, format, std::move(shared_memory), handle.offset, handle.stride)); |
70 } | 65 } |
71 | 66 |
72 MojoGpuMemoryBufferImpl* MojoGpuMemoryBufferImpl::FromClientBuffer( | 67 MojoGpuMemoryBufferImpl* MojoGpuMemoryBufferImpl::FromClientBuffer( |
73 ClientBuffer buffer) { | 68 ClientBuffer buffer) { |
74 return reinterpret_cast<MojoGpuMemoryBufferImpl*>(buffer); | 69 return reinterpret_cast<MojoGpuMemoryBufferImpl*>(buffer); |
75 } | 70 } |
76 | 71 |
77 const unsigned char* MojoGpuMemoryBufferImpl::GetMemory() const { | 72 const unsigned char* MojoGpuMemoryBufferImpl::GetMemory() const { |
78 return static_cast<const unsigned char*>(shared_memory_->memory()); | 73 return static_cast<const unsigned char*>(shared_memory_->memory()); |
79 } | 74 } |
80 | 75 |
81 bool MojoGpuMemoryBufferImpl::Map() { | 76 bool MojoGpuMemoryBufferImpl::Map() { |
82 DCHECK(!mapped_); | 77 DCHECK(!mapped_); |
83 if (!shared_memory_->Map(gfx::BufferSizeForBufferFormat(size_, format_))) | 78 DCHECK_EQ(static_cast<size_t>(stride_), |
| 79 gfx::RowSizeForBufferFormat(size_.width(), format_, 0)); |
| 80 const size_t buffer_size = gfx::BufferSizeForBufferFormat(size_, format_); |
| 81 const size_t map_size = offset_ + buffer_size; |
| 82 if (!shared_memory_->Map(map_size)) |
84 return false; | 83 return false; |
85 mapped_ = true; | 84 mapped_ = true; |
86 return true; | 85 return true; |
87 } | 86 } |
88 | 87 |
89 void* MojoGpuMemoryBufferImpl::memory(size_t plane) { | 88 void* MojoGpuMemoryBufferImpl::memory(size_t plane) { |
90 DCHECK(mapped_); | 89 DCHECK(mapped_); |
91 DCHECK_LT(plane, gfx::NumberOfPlanesForBufferFormat(format_)); | 90 DCHECK_LT(plane, gfx::NumberOfPlanesForBufferFormat(format_)); |
92 return reinterpret_cast<uint8_t*>(shared_memory_->memory()) + | 91 return reinterpret_cast<uint8_t*>(shared_memory_->memory()) + offset_ + |
93 gfx::BufferOffsetForBufferFormat(size_, format_, plane); | 92 gfx::BufferOffsetForBufferFormat(size_, format_, plane); |
94 } | 93 } |
95 | 94 |
96 void MojoGpuMemoryBufferImpl::Unmap() { | 95 void MojoGpuMemoryBufferImpl::Unmap() { |
97 DCHECK(mapped_); | 96 DCHECK(mapped_); |
98 shared_memory_->Unmap(); | 97 shared_memory_->Unmap(); |
99 mapped_ = false; | 98 mapped_ = false; |
100 } | 99 } |
101 | 100 |
102 int MojoGpuMemoryBufferImpl::stride(size_t plane) const { | 101 int MojoGpuMemoryBufferImpl::stride(size_t plane) const { |
103 DCHECK_LT(plane, gfx::NumberOfPlanesForBufferFormat(format_)); | 102 DCHECK_LT(plane, gfx::NumberOfPlanesForBufferFormat(format_)); |
104 return base::checked_cast<int>(gfx::RowSizeForBufferFormat( | 103 return base::checked_cast<int>(gfx::RowSizeForBufferFormat( |
105 size_.width(), format_, static_cast<int>(plane))); | 104 size_.width(), format_, static_cast<int>(plane))); |
106 } | 105 } |
107 | 106 |
108 gfx::GpuMemoryBufferHandle MojoGpuMemoryBufferImpl::GetHandle() const { | 107 gfx::GpuMemoryBufferHandle MojoGpuMemoryBufferImpl::GetHandle() const { |
109 gfx::GpuMemoryBufferHandle handle; | 108 gfx::GpuMemoryBufferHandle handle; |
110 handle.type = gfx::SHARED_MEMORY_BUFFER; | 109 handle.type = gfx::SHARED_MEMORY_BUFFER; |
111 handle.handle = shared_memory_->handle(); | 110 handle.handle = shared_memory_->handle(); |
112 handle.offset = 0; | 111 handle.offset = offset_; |
113 handle.stride = static_cast<int32_t>( | 112 handle.stride = stride_; |
114 gfx::RowSizeForBufferFormat(size_.width(), format_, 0)); | |
115 | 113 |
116 return handle; | 114 return handle; |
117 } | 115 } |
118 | 116 |
119 gfx::GpuMemoryBufferType MojoGpuMemoryBufferImpl::GetBufferType() const { | 117 gfx::GpuMemoryBufferType MojoGpuMemoryBufferImpl::GetBufferType() const { |
120 return gfx::SHARED_MEMORY_BUFFER; | 118 return gfx::SHARED_MEMORY_BUFFER; |
121 } | 119 } |
122 | 120 |
| 121 MojoGpuMemoryBufferImpl::MojoGpuMemoryBufferImpl( |
| 122 const gfx::Size& size, |
| 123 gfx::BufferFormat format, |
| 124 std::unique_ptr<base::SharedMemory> shared_memory, |
| 125 uint32_t offset, |
| 126 int32_t stride) |
| 127 : GpuMemoryBufferImpl(gfx::GenericSharedMemoryId(0), size, format), |
| 128 shared_memory_(std::move(shared_memory)), |
| 129 offset_(offset), |
| 130 stride_(stride) {} |
| 131 |
123 } // namespace ui | 132 } // namespace ui |
OLD | NEW |