| 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/common/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 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. | |
| 28 MojoGpuMemoryBufferImpl::~MojoGpuMemoryBufferImpl() {} | |
| 29 | |
| 30 std::unique_ptr<gfx::GpuMemoryBuffer> MojoGpuMemoryBufferImpl::Create( | |
| 31 const gfx::Size& size, | |
| 32 gfx::BufferFormat format, | |
| 33 gfx::BufferUsage usage) { | |
| 34 size_t bytes = gfx::BufferSizeForBufferFormat(size, format); | |
| 35 | |
| 36 mojo::ScopedSharedBufferHandle handle = | |
| 37 mojo::SharedBufferHandle::Create(bytes); | |
| 38 if (!handle.is_valid()) | |
| 39 return nullptr; | |
| 40 | |
| 41 base::SharedMemoryHandle platform_handle; | |
| 42 size_t shared_memory_size; | |
| 43 bool readonly; | |
| 44 MojoResult result = mojo::UnwrapSharedMemoryHandle( | |
| 45 std::move(handle), &platform_handle, &shared_memory_size, &readonly); | |
| 46 if (result != MOJO_RESULT_OK) | |
| 47 return nullptr; | |
| 48 DCHECK_EQ(shared_memory_size, bytes); | |
| 49 | |
| 50 auto shared_memory = | |
| 51 base::MakeUnique<base::SharedMemory>(platform_handle, readonly); | |
| 52 return base::WrapUnique<gfx::GpuMemoryBuffer>( | |
| 53 new MojoGpuMemoryBufferImpl(size, format, std::move(shared_memory))); | |
| 54 } | |
| 55 | |
| 56 MojoGpuMemoryBufferImpl* MojoGpuMemoryBufferImpl::FromClientBuffer( | |
| 57 ClientBuffer buffer) { | |
| 58 return reinterpret_cast<MojoGpuMemoryBufferImpl*>(buffer); | |
| 59 } | |
| 60 | |
| 61 const unsigned char* MojoGpuMemoryBufferImpl::GetMemory() const { | |
| 62 return static_cast<const unsigned char*>(shared_memory_->memory()); | |
| 63 } | |
| 64 | |
| 65 bool MojoGpuMemoryBufferImpl::Map() { | |
| 66 DCHECK(!mapped_); | |
| 67 if (!shared_memory_->Map(gfx::BufferSizeForBufferFormat(size_, format_))) | |
| 68 return false; | |
| 69 mapped_ = true; | |
| 70 return true; | |
| 71 } | |
| 72 | |
| 73 void* MojoGpuMemoryBufferImpl::memory(size_t plane) { | |
| 74 DCHECK(mapped_); | |
| 75 DCHECK_LT(plane, gfx::NumberOfPlanesForBufferFormat(format_)); | |
| 76 return reinterpret_cast<uint8_t*>(shared_memory_->memory()) + | |
| 77 gfx::BufferOffsetForBufferFormat(size_, format_, plane); | |
| 78 } | |
| 79 | |
| 80 void MojoGpuMemoryBufferImpl::Unmap() { | |
| 81 DCHECK(mapped_); | |
| 82 shared_memory_->Unmap(); | |
| 83 mapped_ = false; | |
| 84 } | |
| 85 | |
| 86 int MojoGpuMemoryBufferImpl::stride(size_t plane) const { | |
| 87 DCHECK_LT(plane, gfx::NumberOfPlanesForBufferFormat(format_)); | |
| 88 return base::checked_cast<int>(gfx::RowSizeForBufferFormat( | |
| 89 size_.width(), format_, static_cast<int>(plane))); | |
| 90 } | |
| 91 | |
| 92 gfx::GpuMemoryBufferHandle MojoGpuMemoryBufferImpl::GetHandle() const { | |
| 93 gfx::GpuMemoryBufferHandle handle; | |
| 94 handle.type = gfx::SHARED_MEMORY_BUFFER; | |
| 95 handle.handle = shared_memory_->handle(); | |
| 96 handle.offset = 0; | |
| 97 handle.stride = static_cast<int32_t>( | |
| 98 gfx::RowSizeForBufferFormat(size_.width(), format_, 0)); | |
| 99 | |
| 100 return handle; | |
| 101 } | |
| 102 | |
| 103 gfx::GpuMemoryBufferType MojoGpuMemoryBufferImpl::GetBufferType() const { | |
| 104 return gfx::SHARED_MEMORY_BUFFER; | |
| 105 } | |
| 106 | |
| 107 } // namespace ui | |
| OLD | NEW |