| 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( | 20 MojoGpuMemoryBufferImpl::MojoGpuMemoryBufferImpl( |
| 21 const gfx::Size& size, | 21 const gfx::Size& size, |
| 22 gfx::BufferFormat format, | 22 gfx::BufferFormat format, |
| 23 std::unique_ptr<base::SharedMemory> shared_memory) | 23 std::unique_ptr<base::SharedMemory> shared_memory, |
| 24 size_t offset, |
| 25 int stride) |
| 24 : GpuMemoryBufferImpl(gfx::GenericSharedMemoryId(0), size, format), | 26 : GpuMemoryBufferImpl(gfx::GenericSharedMemoryId(0), size, format), |
| 25 shared_memory_(std::move(shared_memory)) {} | 27 shared_memory_(std::move(shared_memory)), |
| 28 offset_(offset), |
| 29 stride_(stride) {} |
| 26 | 30 |
| 27 // TODO(rjkroege): Support running a destructor callback as necessary. | 31 // TODO(rjkroege): Support running a destructor callback as necessary. |
| 28 MojoGpuMemoryBufferImpl::~MojoGpuMemoryBufferImpl() {} | 32 MojoGpuMemoryBufferImpl::~MojoGpuMemoryBufferImpl() {} |
| 29 | 33 |
| 30 // static | 34 // static |
| 31 std::unique_ptr<gfx::GpuMemoryBuffer> MojoGpuMemoryBufferImpl::Create( | 35 std::unique_ptr<gfx::GpuMemoryBuffer> MojoGpuMemoryBufferImpl::Create( |
| 32 const gfx::Size& size, | 36 const gfx::Size& size, |
| 33 gfx::BufferFormat format, | 37 gfx::BufferFormat format, |
| 34 gfx::BufferUsage usage) { | 38 gfx::BufferUsage usage) { |
| 35 size_t bytes = gfx::BufferSizeForBufferFormat(size, format); | 39 size_t bytes = gfx::BufferSizeForBufferFormat(size, format); |
| 36 | 40 |
| 37 mojo::ScopedSharedBufferHandle handle = | 41 mojo::ScopedSharedBufferHandle handle = |
| 38 mojo::SharedBufferHandle::Create(bytes); | 42 mojo::SharedBufferHandle::Create(bytes); |
| 39 if (!handle.is_valid()) | 43 if (!handle.is_valid()) |
| 40 return nullptr; | 44 return nullptr; |
| 41 | 45 |
| 42 base::SharedMemoryHandle platform_handle; | 46 base::SharedMemoryHandle platform_handle; |
| 43 size_t shared_memory_size; | 47 size_t shared_memory_size; |
| 44 bool readonly; | 48 bool readonly; |
| 45 MojoResult result = mojo::UnwrapSharedMemoryHandle( | 49 MojoResult result = mojo::UnwrapSharedMemoryHandle( |
| 46 std::move(handle), &platform_handle, &shared_memory_size, &readonly); | 50 std::move(handle), &platform_handle, &shared_memory_size, &readonly); |
| 47 if (result != MOJO_RESULT_OK) | 51 if (result != MOJO_RESULT_OK) |
| 48 return nullptr; | 52 return nullptr; |
| 49 DCHECK_EQ(shared_memory_size, bytes); | 53 DCHECK_EQ(shared_memory_size, bytes); |
| 50 | 54 |
| 51 auto shared_memory = | 55 auto shared_memory = |
| 52 base::MakeUnique<base::SharedMemory>(platform_handle, readonly); | 56 base::MakeUnique<base::SharedMemory>(platform_handle, readonly); |
| 53 return base::MakeUnique<MojoGpuMemoryBufferImpl>( | 57 const int stride = base::checked_cast<int>( |
| 54 size, format, std::move(shared_memory)); | 58 gfx::RowSizeForBufferFormat(size.width(), format, 0)); |
| 59 return base::WrapUnique(new MojoGpuMemoryBufferImpl( |
| 60 size, format, std::move(shared_memory), 0, stride)); |
| 55 } | 61 } |
| 56 | 62 |
| 57 // static | 63 // static |
| 58 std::unique_ptr<gfx::GpuMemoryBuffer> MojoGpuMemoryBufferImpl::CreateFromHandle( | 64 std::unique_ptr<gfx::GpuMemoryBuffer> MojoGpuMemoryBufferImpl::CreateFromHandle( |
| 59 const gfx::GpuMemoryBufferHandle& handle, | 65 const gfx::GpuMemoryBufferHandle& handle, |
| 60 const gfx::Size& size, | 66 const gfx::Size& size, |
| 61 gfx::BufferFormat format, | 67 gfx::BufferFormat format, |
| 62 gfx::BufferUsage usage) { | 68 gfx::BufferUsage usage) { |
| 63 DCHECK_EQ(handle.type, gfx::SHARED_MEMORY_BUFFER); | 69 DCHECK_EQ(handle.type, gfx::SHARED_MEMORY_BUFFER); |
| 64 DCHECK(base::SharedMemory::IsHandleValid(handle.handle)); | 70 DCHECK(base::SharedMemory::IsHandleValid(handle.handle)); |
| 65 const bool readonly = false; | 71 const bool readonly = false; |
| 66 auto shared_memory = | 72 auto shared_memory = |
| 67 base::MakeUnique<base::SharedMemory>(handle.handle, readonly); | 73 base::MakeUnique<base::SharedMemory>(handle.handle, readonly); |
| 68 return base::MakeUnique<MojoGpuMemoryBufferImpl>( | 74 return base::WrapUnique(new MojoGpuMemoryBufferImpl( |
| 69 size, format, std::move(shared_memory)); | 75 size, format, std::move(shared_memory), handle.offset, handle.stride)); |
| 70 } | 76 } |
| 71 | 77 |
| 72 MojoGpuMemoryBufferImpl* MojoGpuMemoryBufferImpl::FromClientBuffer( | 78 MojoGpuMemoryBufferImpl* MojoGpuMemoryBufferImpl::FromClientBuffer( |
| 73 ClientBuffer buffer) { | 79 ClientBuffer buffer) { |
| 74 return reinterpret_cast<MojoGpuMemoryBufferImpl*>(buffer); | 80 return reinterpret_cast<MojoGpuMemoryBufferImpl*>(buffer); |
| 75 } | 81 } |
| 76 | 82 |
| 77 const unsigned char* MojoGpuMemoryBufferImpl::GetMemory() const { | 83 const unsigned char* MojoGpuMemoryBufferImpl::GetMemory() const { |
| 78 return static_cast<const unsigned char*>(shared_memory_->memory()); | 84 return static_cast<const unsigned char*>(shared_memory_->memory()); |
| 79 } | 85 } |
| 80 | 86 |
| 81 bool MojoGpuMemoryBufferImpl::Map() { | 87 bool MojoGpuMemoryBufferImpl::Map() { |
| 82 DCHECK(!mapped_); | 88 DCHECK(!mapped_); |
| 83 if (!shared_memory_->Map(gfx::BufferSizeForBufferFormat(size_, format_))) | 89 DCHECK_EQ(static_cast<size_t>(stride_), |
| 90 gfx::RowSizeForBufferFormat(size_.width(), format_, 0)); |
| 91 const size_t buffer_size = gfx::BufferSizeForBufferFormat(size_, format_); |
| 92 const size_t map_size = offset_ + buffer_size; |
| 93 if (!shared_memory_->Map(map_size)) |
| 84 return false; | 94 return false; |
| 85 mapped_ = true; | 95 mapped_ = true; |
| 86 return true; | 96 return true; |
| 87 } | 97 } |
| 88 | 98 |
| 89 void* MojoGpuMemoryBufferImpl::memory(size_t plane) { | 99 void* MojoGpuMemoryBufferImpl::memory(size_t plane) { |
| 90 DCHECK(mapped_); | 100 DCHECK(mapped_); |
| 91 DCHECK_LT(plane, gfx::NumberOfPlanesForBufferFormat(format_)); | 101 DCHECK_LT(plane, gfx::NumberOfPlanesForBufferFormat(format_)); |
| 92 return reinterpret_cast<uint8_t*>(shared_memory_->memory()) + | 102 return reinterpret_cast<uint8_t*>(shared_memory_->memory()) + offset_ + |
| 93 gfx::BufferOffsetForBufferFormat(size_, format_, plane); | 103 gfx::BufferOffsetForBufferFormat(size_, format_, plane); |
| 94 } | 104 } |
| 95 | 105 |
| 96 void MojoGpuMemoryBufferImpl::Unmap() { | 106 void MojoGpuMemoryBufferImpl::Unmap() { |
| 97 DCHECK(mapped_); | 107 DCHECK(mapped_); |
| 98 shared_memory_->Unmap(); | 108 shared_memory_->Unmap(); |
| 99 mapped_ = false; | 109 mapped_ = false; |
| 100 } | 110 } |
| 101 | 111 |
| 102 int MojoGpuMemoryBufferImpl::stride(size_t plane) const { | 112 int MojoGpuMemoryBufferImpl::stride(size_t plane) const { |
| 103 DCHECK_LT(plane, gfx::NumberOfPlanesForBufferFormat(format_)); | 113 DCHECK_LT(plane, gfx::NumberOfPlanesForBufferFormat(format_)); |
| 104 return base::checked_cast<int>(gfx::RowSizeForBufferFormat( | 114 return base::checked_cast<int>(gfx::RowSizeForBufferFormat( |
| 105 size_.width(), format_, static_cast<int>(plane))); | 115 size_.width(), format_, static_cast<int>(plane))); |
| 106 } | 116 } |
| 107 | 117 |
| 108 gfx::GpuMemoryBufferHandle MojoGpuMemoryBufferImpl::GetHandle() const { | 118 gfx::GpuMemoryBufferHandle MojoGpuMemoryBufferImpl::GetHandle() const { |
| 109 gfx::GpuMemoryBufferHandle handle; | 119 gfx::GpuMemoryBufferHandle handle; |
| 110 handle.type = gfx::SHARED_MEMORY_BUFFER; | 120 handle.type = gfx::SHARED_MEMORY_BUFFER; |
| 111 handle.handle = shared_memory_->handle(); | 121 handle.handle = shared_memory_->handle(); |
| 112 handle.offset = 0; | 122 handle.offset = offset_; |
| 113 handle.stride = static_cast<int32_t>( | 123 handle.stride = stride_; |
| 114 gfx::RowSizeForBufferFormat(size_.width(), format_, 0)); | |
| 115 | 124 |
| 116 return handle; | 125 return handle; |
| 117 } | 126 } |
| 118 | 127 |
| 119 gfx::GpuMemoryBufferType MojoGpuMemoryBufferImpl::GetBufferType() const { | 128 gfx::GpuMemoryBufferType MojoGpuMemoryBufferImpl::GetBufferType() const { |
| 120 return gfx::SHARED_MEMORY_BUFFER; | 129 return gfx::SHARED_MEMORY_BUFFER; |
| 121 } | 130 } |
| 122 | 131 |
| 123 } // namespace ui | 132 } // namespace ui |
| OLD | NEW |