| 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 "components/view_manager/gles2/mojo_gpu_memory_buffer.h" | 5 #include "components/mus/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/memory/shared_memory.h" |
| 9 #include "base/numerics/safe_conversions.h" | 9 #include "base/numerics/safe_conversions.h" |
| 10 #include "ui/gfx/buffer_format_util.h" | 10 #include "ui/gfx/buffer_format_util.h" |
| 11 | 11 |
| 12 namespace gles2 { | 12 namespace gles2 { |
| 13 | 13 |
| 14 MojoGpuMemoryBufferImpl::MojoGpuMemoryBufferImpl( | 14 MojoGpuMemoryBufferImpl::MojoGpuMemoryBufferImpl( |
| 15 const gfx::Size& size, | 15 const gfx::Size& size, |
| 16 gfx::BufferFormat format, | 16 gfx::BufferFormat format, |
| 17 scoped_ptr<base::SharedMemory> shared_memory) | 17 scoped_ptr<base::SharedMemory> shared_memory) |
| 18 : size_(size), | 18 : size_(size), |
| 19 format_(format), | 19 format_(format), |
| 20 shared_memory_(shared_memory.Pass()), | 20 shared_memory_(shared_memory.Pass()), |
| 21 mapped_(false) {} | 21 mapped_(false) {} |
| 22 | 22 |
| 23 MojoGpuMemoryBufferImpl::~MojoGpuMemoryBufferImpl() { | 23 MojoGpuMemoryBufferImpl::~MojoGpuMemoryBufferImpl() {} |
| 24 } | |
| 25 | 24 |
| 26 scoped_ptr<gfx::GpuMemoryBuffer> MojoGpuMemoryBufferImpl::Create( | 25 scoped_ptr<gfx::GpuMemoryBuffer> MojoGpuMemoryBufferImpl::Create( |
| 27 const gfx::Size& size, | 26 const gfx::Size& size, |
| 28 gfx::BufferFormat format, | 27 gfx::BufferFormat format, |
| 29 gfx::BufferUsage usage) { | 28 gfx::BufferUsage usage) { |
| 30 size_t bytes = gfx::BufferSizeForBufferFormat(size, format); | 29 size_t bytes = gfx::BufferSizeForBufferFormat(size, format); |
| 31 scoped_ptr<base::SharedMemory> shared_memory(new base::SharedMemory); | 30 scoped_ptr<base::SharedMemory> shared_memory(new base::SharedMemory); |
| 32 if (!shared_memory->CreateAnonymous(bytes)) | 31 if (!shared_memory->CreateAnonymous(bytes)) |
| 33 return nullptr; | 32 return nullptr; |
| 34 return make_scoped_ptr<gfx::GpuMemoryBuffer>( | 33 return make_scoped_ptr<gfx::GpuMemoryBuffer>( |
| 35 new MojoGpuMemoryBufferImpl(size, format, shared_memory.Pass())); | 34 new MojoGpuMemoryBufferImpl(size, format, shared_memory.Pass())); |
| 36 } | 35 } |
| 37 | 36 |
| 38 MojoGpuMemoryBufferImpl* MojoGpuMemoryBufferImpl::FromClientBuffer( | 37 MojoGpuMemoryBufferImpl* MojoGpuMemoryBufferImpl::FromClientBuffer( |
| 39 ClientBuffer buffer) { | 38 ClientBuffer buffer) { |
| 40 return reinterpret_cast<MojoGpuMemoryBufferImpl*>(buffer); | 39 return reinterpret_cast<MojoGpuMemoryBufferImpl*>(buffer); |
| 41 } | 40 } |
| 42 | 41 |
| 43 const unsigned char* MojoGpuMemoryBufferImpl::GetMemory() const { | 42 const unsigned char* MojoGpuMemoryBufferImpl::GetMemory() const { |
| 44 return static_cast<const unsigned char*>(shared_memory_->memory()); | 43 return static_cast<const unsigned char*>(shared_memory_->memory()); |
| 45 } | 44 } |
| 46 | 45 |
| 47 bool MojoGpuMemoryBufferImpl::Map(void** data) { | 46 bool MojoGpuMemoryBufferImpl::Map(void** data) { |
| 48 DCHECK(!mapped_); | 47 DCHECK(!mapped_); |
| 49 if (!shared_memory_->Map(gfx::BufferSizeForBufferFormat(size_, format_))) | 48 if (!shared_memory_->Map(gfx::BufferSizeForBufferFormat(size_, format_))) |
| 50 return false; | 49 return false; |
| 51 mapped_ = true; | 50 mapped_ = true; |
| 52 size_t offset = 0; | 51 size_t offset = 0; |
| 53 int num_planes = static_cast<int>( | 52 int num_planes = |
| 54 gfx::NumberOfPlanesForBufferFormat(format_)); | 53 static_cast<int>(gfx::NumberOfPlanesForBufferFormat(format_)); |
| 55 for (int i = 0; i < num_planes; ++i) { | 54 for (int i = 0; i < num_planes; ++i) { |
| 56 data[i] = reinterpret_cast<uint8*>(shared_memory_->memory()) + offset; | 55 data[i] = reinterpret_cast<uint8*>(shared_memory_->memory()) + offset; |
| 57 offset += | 56 offset += |
| 58 gfx::RowSizeForBufferFormat(size_.width(), format_, i) * | 57 gfx::RowSizeForBufferFormat(size_.width(), format_, i) * |
| 59 (size_.height() / gfx::SubsamplingFactorForBufferFormat(format_, i)); | 58 (size_.height() / gfx::SubsamplingFactorForBufferFormat(format_, i)); |
| 60 } | 59 } |
| 61 return true; | 60 return true; |
| 62 } | 61 } |
| 63 | 62 |
| 64 void MojoGpuMemoryBufferImpl::Unmap() { | 63 void MojoGpuMemoryBufferImpl::Unmap() { |
| 65 DCHECK(mapped_); | 64 DCHECK(mapped_); |
| 66 shared_memory_->Unmap(); | 65 shared_memory_->Unmap(); |
| 67 mapped_ = false; | 66 mapped_ = false; |
| 68 } | 67 } |
| 69 | 68 |
| 70 bool MojoGpuMemoryBufferImpl::IsMapped() const { | 69 bool MojoGpuMemoryBufferImpl::IsMapped() const { |
| 71 return mapped_; | 70 return mapped_; |
| 72 } | 71 } |
| 73 | 72 |
| 74 gfx::BufferFormat MojoGpuMemoryBufferImpl::GetFormat() const { | 73 gfx::BufferFormat MojoGpuMemoryBufferImpl::GetFormat() const { |
| 75 return format_; | 74 return format_; |
| 76 } | 75 } |
| 77 | 76 |
| 78 void MojoGpuMemoryBufferImpl::GetStride(int* stride) const { | 77 void MojoGpuMemoryBufferImpl::GetStride(int* stride) const { |
| 79 int num_planes = static_cast<int>( | 78 int num_planes = |
| 80 gfx::NumberOfPlanesForBufferFormat(format_)); | 79 static_cast<int>(gfx::NumberOfPlanesForBufferFormat(format_)); |
| 81 for (int i = 0; i < num_planes; ++i) | 80 for (int i = 0; i < num_planes; ++i) |
| 82 stride[i] = base::checked_cast<int>( | 81 stride[i] = base::checked_cast<int>( |
| 83 gfx::RowSizeForBufferFormat(size_.width(), format_, i)); | 82 gfx::RowSizeForBufferFormat(size_.width(), format_, i)); |
| 84 } | 83 } |
| 85 | 84 |
| 86 gfx::GpuMemoryBufferId MojoGpuMemoryBufferImpl::GetId() const { | 85 gfx::GpuMemoryBufferId MojoGpuMemoryBufferImpl::GetId() const { |
| 87 return gfx::GpuMemoryBufferId(0); | 86 return gfx::GpuMemoryBufferId(0); |
| 88 } | 87 } |
| 89 | 88 |
| 90 gfx::GpuMemoryBufferHandle MojoGpuMemoryBufferImpl::GetHandle() const { | 89 gfx::GpuMemoryBufferHandle MojoGpuMemoryBufferImpl::GetHandle() const { |
| 91 gfx::GpuMemoryBufferHandle handle; | 90 gfx::GpuMemoryBufferHandle handle; |
| 92 handle.type = gfx::SHARED_MEMORY_BUFFER; | 91 handle.type = gfx::SHARED_MEMORY_BUFFER; |
| 93 handle.handle = shared_memory_->handle(); | 92 handle.handle = shared_memory_->handle(); |
| 94 return handle; | 93 return handle; |
| 95 } | 94 } |
| 96 | 95 |
| 97 ClientBuffer MojoGpuMemoryBufferImpl::AsClientBuffer() { | 96 ClientBuffer MojoGpuMemoryBufferImpl::AsClientBuffer() { |
| 98 return reinterpret_cast<ClientBuffer>(this); | 97 return reinterpret_cast<ClientBuffer>(this); |
| 99 } | 98 } |
| 100 | 99 |
| 101 } // namespace gles2 | 100 } // namespace gles2 |
| OLD | NEW |