Chromium Code Reviews| 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 : GpuMemoryBufferImpl(gfx::GenericSharedMemoryId(0), size, format), | 24 : GpuMemoryBufferImpl(gfx::GenericSharedMemoryId(0), size, format), |
| 25 shared_memory_(std::move(shared_memory)) {} | 25 shared_memory_(std::move(shared_memory)) {} |
| 26 | 26 |
| 27 // TODO(rjkroege): Support running a destructor callback as necessary. | 27 // TODO(rjkroege): Support running a destructor callback as necessary. |
| 28 MojoGpuMemoryBufferImpl::~MojoGpuMemoryBufferImpl() {} | 28 MojoGpuMemoryBufferImpl::~MojoGpuMemoryBufferImpl() {} |
| 29 | 29 |
| 30 // static | |
| 30 std::unique_ptr<gfx::GpuMemoryBuffer> MojoGpuMemoryBufferImpl::Create( | 31 std::unique_ptr<gfx::GpuMemoryBuffer> MojoGpuMemoryBufferImpl::Create( |
| 31 const gfx::Size& size, | 32 const gfx::Size& size, |
| 32 gfx::BufferFormat format, | 33 gfx::BufferFormat format, |
| 33 gfx::BufferUsage usage) { | 34 gfx::BufferUsage usage) { |
| 34 size_t bytes = gfx::BufferSizeForBufferFormat(size, format); | 35 size_t bytes = gfx::BufferSizeForBufferFormat(size, format); |
| 35 | 36 |
| 36 mojo::ScopedSharedBufferHandle handle = | 37 mojo::ScopedSharedBufferHandle handle = |
| 37 mojo::SharedBufferHandle::Create(bytes); | 38 mojo::SharedBufferHandle::Create(bytes); |
| 38 if (!handle.is_valid()) | 39 if (!handle.is_valid()) |
| 39 return nullptr; | 40 return nullptr; |
| 40 | 41 |
| 41 base::SharedMemoryHandle platform_handle; | 42 base::SharedMemoryHandle platform_handle; |
| 42 size_t shared_memory_size; | 43 size_t shared_memory_size; |
| 43 bool readonly; | 44 bool readonly; |
| 44 MojoResult result = mojo::UnwrapSharedMemoryHandle( | 45 MojoResult result = mojo::UnwrapSharedMemoryHandle( |
| 45 std::move(handle), &platform_handle, &shared_memory_size, &readonly); | 46 std::move(handle), &platform_handle, &shared_memory_size, &readonly); |
| 46 if (result != MOJO_RESULT_OK) | 47 if (result != MOJO_RESULT_OK) |
| 47 return nullptr; | 48 return nullptr; |
| 48 DCHECK_EQ(shared_memory_size, bytes); | 49 DCHECK_EQ(shared_memory_size, bytes); |
| 49 | 50 |
| 50 auto shared_memory = | 51 auto shared_memory = |
| 51 base::MakeUnique<base::SharedMemory>(platform_handle, readonly); | 52 base::MakeUnique<base::SharedMemory>(platform_handle, readonly); |
| 52 return base::WrapUnique<gfx::GpuMemoryBuffer>( | 53 return base::WrapUnique<gfx::GpuMemoryBuffer>( |
| 53 new MojoGpuMemoryBufferImpl(size, format, std::move(shared_memory))); | 54 new MojoGpuMemoryBufferImpl(size, format, std::move(shared_memory))); |
| 54 } | 55 } |
| 55 | 56 |
| 57 // static | |
| 58 std::unique_ptr<gfx::GpuMemoryBuffer> MojoGpuMemoryBufferImpl::CreateFromHandle( | |
| 59 const gfx::GpuMemoryBufferHandle& handle, | |
| 60 const gfx::Size& size, | |
| 61 gfx::BufferFormat format, | |
| 62 gfx::BufferUsage usage) { | |
| 63 DCHECK_EQ(handle.type, gfx::SHARED_MEMORY_BUFFER); | |
| 64 DCHECK(base::SharedMemory::IsHandleValid(handle.handle)); | |
| 65 const bool readonly = false; | |
| 66 auto shared_memory = | |
| 67 base::MakeUnique<base::SharedMemory>(handle.handle, readonly); | |
| 68 return base::WrapUnique<gfx::GpuMemoryBuffer>( | |
|
dcheng
2016/08/17 22:59:00
Nit: Omit the template parameter here (WrapUnique
Peng
2016/08/17 23:28:17
Done.
| |
| 69 new MojoGpuMemoryBufferImpl(size, format, std::move(shared_memory))); | |
| 70 } | |
| 71 | |
| 56 MojoGpuMemoryBufferImpl* MojoGpuMemoryBufferImpl::FromClientBuffer( | 72 MojoGpuMemoryBufferImpl* MojoGpuMemoryBufferImpl::FromClientBuffer( |
| 57 ClientBuffer buffer) { | 73 ClientBuffer buffer) { |
| 58 return reinterpret_cast<MojoGpuMemoryBufferImpl*>(buffer); | 74 return reinterpret_cast<MojoGpuMemoryBufferImpl*>(buffer); |
| 59 } | 75 } |
| 60 | 76 |
| 61 const unsigned char* MojoGpuMemoryBufferImpl::GetMemory() const { | 77 const unsigned char* MojoGpuMemoryBufferImpl::GetMemory() const { |
| 62 return static_cast<const unsigned char*>(shared_memory_->memory()); | 78 return static_cast<const unsigned char*>(shared_memory_->memory()); |
| 63 } | 79 } |
| 64 | 80 |
| 65 bool MojoGpuMemoryBufferImpl::Map() { | 81 bool MojoGpuMemoryBufferImpl::Map() { |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 98 gfx::RowSizeForBufferFormat(size_.width(), format_, 0)); | 114 gfx::RowSizeForBufferFormat(size_.width(), format_, 0)); |
| 99 | 115 |
| 100 return handle; | 116 return handle; |
| 101 } | 117 } |
| 102 | 118 |
| 103 gfx::GpuMemoryBufferType MojoGpuMemoryBufferImpl::GetBufferType() const { | 119 gfx::GpuMemoryBufferType MojoGpuMemoryBufferImpl::GetBufferType() const { |
| 104 return gfx::SHARED_MEMORY_BUFFER; | 120 return gfx::SHARED_MEMORY_BUFFER; |
| 105 } | 121 } |
| 106 | 122 |
| 107 } // namespace ui | 123 } // namespace ui |
| OLD | NEW |