OLD | NEW |
1 // Copyright 2014 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 "cc/test/test_gpu_memory_buffer_manager.h" | 5 #include "components/view_manager/gles2/mojo_gpu_memory_buffer.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/memory/shared_memory.h" |
8 #include "base/numerics/safe_conversions.h" | 9 #include "base/numerics/safe_conversions.h" |
9 #include "ui/gfx/gpu_memory_buffer.h" | |
10 | 10 |
11 namespace cc { | 11 namespace gles2 { |
| 12 |
12 namespace { | 13 namespace { |
13 | 14 |
14 int NumberOfPlanesForGpuMemoryBufferFormat( | 15 int NumberOfPlanesForGpuMemoryBufferFormat( |
15 gfx::GpuMemoryBuffer::Format format) { | 16 gfx::GpuMemoryBuffer::Format format) { |
16 switch (format) { | 17 switch (format) { |
17 case gfx::GpuMemoryBuffer::ATC: | 18 case gfx::GpuMemoryBuffer::ATC: |
18 case gfx::GpuMemoryBuffer::ATCIA: | 19 case gfx::GpuMemoryBuffer::ATCIA: |
19 case gfx::GpuMemoryBuffer::DXT1: | 20 case gfx::GpuMemoryBuffer::DXT1: |
20 case gfx::GpuMemoryBuffer::DXT5: | 21 case gfx::GpuMemoryBuffer::DXT5: |
21 case gfx::GpuMemoryBuffer::ETC1: | 22 case gfx::GpuMemoryBuffer::ETC1: |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
90 gfx::GpuMemoryBuffer::Format format) { | 91 gfx::GpuMemoryBuffer::Format format) { |
91 size_t size_in_bytes = 0; | 92 size_t size_in_bytes = 0; |
92 int num_planes = NumberOfPlanesForGpuMemoryBufferFormat(format); | 93 int num_planes = NumberOfPlanesForGpuMemoryBufferFormat(format); |
93 for (int i = 0; i < num_planes; ++i) { | 94 for (int i = 0; i < num_planes; ++i) { |
94 size_in_bytes += StrideInBytes(size.width(), format, i) * | 95 size_in_bytes += StrideInBytes(size.width(), format, i) * |
95 (size.height() / SubsamplingFactor(format, i)); | 96 (size.height() / SubsamplingFactor(format, i)); |
96 } | 97 } |
97 return size_in_bytes; | 98 return size_in_bytes; |
98 } | 99 } |
99 | 100 |
100 class GpuMemoryBufferImpl : public gfx::GpuMemoryBuffer { | |
101 public: | |
102 GpuMemoryBufferImpl(const gfx::Size& size, | |
103 Format format, | |
104 scoped_ptr<base::SharedMemory> shared_memory) | |
105 : size_(size), | |
106 format_(format), | |
107 shared_memory_(shared_memory.Pass()), | |
108 mapped_(false) {} | |
109 | |
110 // Overridden from gfx::GpuMemoryBuffer: | |
111 bool Map(void** data) override { | |
112 DCHECK(!mapped_); | |
113 if (!shared_memory_->Map(BufferSizeInBytes(size_, format_))) | |
114 return false; | |
115 mapped_ = true; | |
116 size_t offset = 0; | |
117 int num_planes = NumberOfPlanesForGpuMemoryBufferFormat(format_); | |
118 for (int i = 0; i < num_planes; ++i) { | |
119 data[i] = reinterpret_cast<uint8*>(shared_memory_->memory()) + offset; | |
120 offset += StrideInBytes(size_.width(), format_, i) * | |
121 (size_.height() / SubsamplingFactor(format_, i)); | |
122 } | |
123 return true; | |
124 } | |
125 void Unmap() override { | |
126 DCHECK(mapped_); | |
127 shared_memory_->Unmap(); | |
128 mapped_ = false; | |
129 } | |
130 bool IsMapped() const override { return mapped_; } | |
131 Format GetFormat() const override { return format_; } | |
132 void GetStride(int* stride) const override { | |
133 int num_planes = NumberOfPlanesForGpuMemoryBufferFormat(format_); | |
134 for (int i = 0; i < num_planes; ++i) | |
135 stride[i] = | |
136 base::checked_cast<int>(StrideInBytes(size_.width(), format_, i)); | |
137 } | |
138 gfx::GpuMemoryBufferHandle GetHandle() const override { | |
139 gfx::GpuMemoryBufferHandle handle; | |
140 handle.type = gfx::SHARED_MEMORY_BUFFER; | |
141 handle.handle = shared_memory_->handle(); | |
142 return handle; | |
143 } | |
144 ClientBuffer AsClientBuffer() override { | |
145 return reinterpret_cast<ClientBuffer>(this); | |
146 } | |
147 | |
148 private: | |
149 const gfx::Size size_; | |
150 gfx::GpuMemoryBuffer::Format format_; | |
151 scoped_ptr<base::SharedMemory> shared_memory_; | |
152 bool mapped_; | |
153 }; | |
154 | |
155 } // namespace | 101 } // namespace |
156 | 102 |
157 TestGpuMemoryBufferManager::TestGpuMemoryBufferManager() { | 103 MojoGpuMemoryBufferImpl::MojoGpuMemoryBufferImpl( |
| 104 const gfx::Size& size, |
| 105 Format format, |
| 106 scoped_ptr<base::SharedMemory> shared_memory) |
| 107 : size_(size), |
| 108 format_(format), |
| 109 shared_memory_(shared_memory.Pass()), |
| 110 mapped_(false) { |
158 } | 111 } |
159 | 112 |
160 TestGpuMemoryBufferManager::~TestGpuMemoryBufferManager() { | 113 MojoGpuMemoryBufferImpl::~MojoGpuMemoryBufferImpl() { |
161 } | 114 } |
162 | 115 |
163 scoped_ptr<gfx::GpuMemoryBuffer> | 116 scoped_ptr<gfx::GpuMemoryBuffer> MojoGpuMemoryBufferImpl::Create( |
164 TestGpuMemoryBufferManager::AllocateGpuMemoryBuffer( | |
165 const gfx::Size& size, | 117 const gfx::Size& size, |
166 gfx::GpuMemoryBuffer::Format format, | 118 Format format, |
167 gfx::GpuMemoryBuffer::Usage usage) { | 119 Usage usage) { |
| 120 size_t bytes = BufferSizeInBytes(size, format); |
168 scoped_ptr<base::SharedMemory> shared_memory(new base::SharedMemory); | 121 scoped_ptr<base::SharedMemory> shared_memory(new base::SharedMemory); |
169 if (!shared_memory->CreateAnonymous(BufferSizeInBytes(size, format))) | 122 if (!shared_memory->CreateAnonymous(bytes)) |
170 return nullptr; | 123 return nullptr; |
171 return make_scoped_ptr<gfx::GpuMemoryBuffer>( | 124 return make_scoped_ptr<gfx::GpuMemoryBuffer>( |
172 new GpuMemoryBufferImpl(size, format, shared_memory.Pass())); | 125 new MojoGpuMemoryBufferImpl(size, format, shared_memory.Pass())); |
173 } | 126 } |
174 | 127 |
175 gfx::GpuMemoryBuffer* | 128 MojoGpuMemoryBufferImpl* MojoGpuMemoryBufferImpl::FromClientBuffer( |
176 TestGpuMemoryBufferManager::GpuMemoryBufferFromClientBuffer( | |
177 ClientBuffer buffer) { | 129 ClientBuffer buffer) { |
178 return reinterpret_cast<gfx::GpuMemoryBuffer*>(buffer); | 130 return reinterpret_cast<MojoGpuMemoryBufferImpl*>(buffer); |
179 } | 131 } |
180 | 132 |
181 void TestGpuMemoryBufferManager::SetDestructionSyncPoint( | 133 bool MojoGpuMemoryBufferImpl::Map(void** data) { |
182 gfx::GpuMemoryBuffer* buffer, | 134 DCHECK(!mapped_); |
183 uint32 sync_point) { | 135 if (!shared_memory_->Map(BufferSizeInBytes(size_, format_))) |
| 136 return false; |
| 137 mapped_ = true; |
| 138 size_t offset = 0; |
| 139 int num_planes = NumberOfPlanesForGpuMemoryBufferFormat(format_); |
| 140 for (int i = 0; i < num_planes; ++i) { |
| 141 data[i] = reinterpret_cast<uint8*>(shared_memory_->memory()) + offset; |
| 142 offset += StrideInBytes(size_.width(), format_, i) * |
| 143 (size_.height() / SubsamplingFactor(format_, i)); |
| 144 } |
| 145 return true; |
184 } | 146 } |
185 | 147 |
186 } // namespace cc | 148 void MojoGpuMemoryBufferImpl::Unmap() { |
| 149 DCHECK(mapped_); |
| 150 shared_memory_->Unmap(); |
| 151 mapped_ = false; |
| 152 } |
| 153 |
| 154 bool MojoGpuMemoryBufferImpl::IsMapped() const { |
| 155 return mapped_; |
| 156 } |
| 157 |
| 158 gfx::GpuMemoryBuffer::Format MojoGpuMemoryBufferImpl::GetFormat() const { |
| 159 return format_; |
| 160 } |
| 161 |
| 162 void MojoGpuMemoryBufferImpl::GetStride(int* stride) const { |
| 163 int num_planes = NumberOfPlanesForGpuMemoryBufferFormat(format_); |
| 164 for (int i = 0; i < num_planes; ++i) |
| 165 stride[i] = |
| 166 base::checked_cast<int>(StrideInBytes(size_.width(), format_, i)); |
| 167 } |
| 168 |
| 169 gfx::GpuMemoryBufferHandle MojoGpuMemoryBufferImpl::GetHandle() const { |
| 170 gfx::GpuMemoryBufferHandle handle; |
| 171 handle.type = gfx::SHARED_MEMORY_BUFFER; |
| 172 handle.handle = shared_memory_->handle(); |
| 173 return handle; |
| 174 } |
| 175 |
| 176 ClientBuffer MojoGpuMemoryBufferImpl::AsClientBuffer() { |
| 177 return reinterpret_cast<ClientBuffer>(this); |
| 178 } |
| 179 |
| 180 } // namespace gles2 |
OLD | NEW |