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