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::MakeUnique<MojoGpuMemoryBufferImpl>( |
53 new MojoGpuMemoryBufferImpl(size, format, std::move(shared_memory))); | 54 size, format, std::move(shared_memory)); |
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::MakeUnique<MojoGpuMemoryBufferImpl>( | |
69 size, format, std::move(shared_memory)); | |
54 } | 70 } |
55 | 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() { |
66 DCHECK(!mapped_); | 82 DCHECK(!mapped_); |
67 if (!shared_memory_->Map(gfx::BufferSizeForBufferFormat(size_, format_))) | 83 if (!shared_memory_->Map(gfx::BufferSizeForBufferFormat(size_, format_))) |
dcheng
2016/08/18 02:46:35
Shouldn't this use the checked variant? As we disc
| |
68 return false; | 84 return false; |
69 mapped_ = true; | 85 mapped_ = true; |
70 return true; | 86 return true; |
71 } | 87 } |
72 | 88 |
73 void* MojoGpuMemoryBufferImpl::memory(size_t plane) { | 89 void* MojoGpuMemoryBufferImpl::memory(size_t plane) { |
74 DCHECK(mapped_); | 90 DCHECK(mapped_); |
75 DCHECK_LT(plane, gfx::NumberOfPlanesForBufferFormat(format_)); | 91 DCHECK_LT(plane, gfx::NumberOfPlanesForBufferFormat(format_)); |
76 return reinterpret_cast<uint8_t*>(shared_memory_->memory()) + | 92 return reinterpret_cast<uint8_t*>(shared_memory_->memory()) + |
77 gfx::BufferOffsetForBufferFormat(size_, format_, plane); | 93 gfx::BufferOffsetForBufferFormat(size_, format_, plane); |
dcheng
2016/08/18 02:46:35
I'm also not sure that it's safe to do this: if si
| |
78 } | 94 } |
79 | 95 |
80 void MojoGpuMemoryBufferImpl::Unmap() { | 96 void MojoGpuMemoryBufferImpl::Unmap() { |
81 DCHECK(mapped_); | 97 DCHECK(mapped_); |
82 shared_memory_->Unmap(); | 98 shared_memory_->Unmap(); |
83 mapped_ = false; | 99 mapped_ = false; |
84 } | 100 } |
85 | 101 |
86 int MojoGpuMemoryBufferImpl::stride(size_t plane) const { | 102 int MojoGpuMemoryBufferImpl::stride(size_t plane) const { |
87 DCHECK_LT(plane, gfx::NumberOfPlanesForBufferFormat(format_)); | 103 DCHECK_LT(plane, gfx::NumberOfPlanesForBufferFormat(format_)); |
88 return base::checked_cast<int>(gfx::RowSizeForBufferFormat( | 104 return base::checked_cast<int>(gfx::RowSizeForBufferFormat( |
dcheng
2016/08/18 02:46:35
Why does this use checked_cast and GetHandle() use
| |
89 size_.width(), format_, static_cast<int>(plane))); | 105 size_.width(), format_, static_cast<int>(plane))); |
90 } | 106 } |
91 | 107 |
92 gfx::GpuMemoryBufferHandle MojoGpuMemoryBufferImpl::GetHandle() const { | 108 gfx::GpuMemoryBufferHandle MojoGpuMemoryBufferImpl::GetHandle() const { |
93 gfx::GpuMemoryBufferHandle handle; | 109 gfx::GpuMemoryBufferHandle handle; |
94 handle.type = gfx::SHARED_MEMORY_BUFFER; | 110 handle.type = gfx::SHARED_MEMORY_BUFFER; |
95 handle.handle = shared_memory_->handle(); | 111 handle.handle = shared_memory_->handle(); |
96 handle.offset = 0; | 112 handle.offset = 0; |
97 handle.stride = static_cast<int32_t>( | 113 handle.stride = static_cast<int32_t>( |
98 gfx::RowSizeForBufferFormat(size_.width(), format_, 0)); | 114 gfx::RowSizeForBufferFormat(size_.width(), format_, 0)); |
dcheng
2016/08/18 02:46:35
Can this overflow an int32_t?
| |
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 |