OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "ui/gl/gl_image_shared_memory.h" | 5 #include "ui/gl/gl_image_shared_memory.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/memory/shared_memory.h" | 8 #include "base/memory/shared_memory.h" |
9 #include "base/numerics/safe_math.h" | 9 #include "base/numerics/safe_math.h" |
10 #include "base/process/process_handle.h" | 10 #include "base/process/process_handle.h" |
(...skipping 10 matching lines...) Expand all Loading... |
21 : GLImageMemory(size, internalformat) {} | 21 : GLImageMemory(size, internalformat) {} |
22 | 22 |
23 GLImageSharedMemory::~GLImageSharedMemory() { | 23 GLImageSharedMemory::~GLImageSharedMemory() { |
24 DCHECK(!shared_memory_); | 24 DCHECK(!shared_memory_); |
25 } | 25 } |
26 | 26 |
27 bool GLImageSharedMemory::Initialize( | 27 bool GLImageSharedMemory::Initialize( |
28 const base::SharedMemoryHandle& handle, | 28 const base::SharedMemoryHandle& handle, |
29 gfx::GenericSharedMemoryId shared_memory_id, | 29 gfx::GenericSharedMemoryId shared_memory_id, |
30 gfx::BufferFormat format, | 30 gfx::BufferFormat format, |
31 size_t offset, | 31 size_t offset) { |
32 size_t stride) { | 32 size_t size_in_bytes; |
33 if (NumberOfPlanesForBufferFormat(format) != 1) | 33 if (!BufferSizeForBufferFormatChecked(GetSize(), format, &size_in_bytes)) |
34 return false; | |
35 | |
36 base::CheckedNumeric<size_t> checked_size = stride; | |
37 checked_size *= GetSize().height(); | |
38 if (!checked_size.IsValid()) | |
39 return false; | 34 return false; |
40 | 35 |
41 if (!base::SharedMemory::IsHandleValid(handle)) | 36 if (!base::SharedMemory::IsHandleValid(handle)) |
42 return false; | 37 return false; |
43 | 38 |
44 base::SharedMemory shared_memory(handle, true); | 39 base::SharedMemory shared_memory(handle, true); |
45 | 40 |
46 // Duplicate the handle. | 41 // Duplicate the handle. |
47 base::SharedMemoryHandle duped_shared_memory_handle; | 42 base::SharedMemoryHandle duped_shared_memory_handle; |
48 if (!shared_memory.ShareToProcess(base::GetCurrentProcessHandle(), | 43 if (!shared_memory.ShareToProcess(base::GetCurrentProcessHandle(), |
49 &duped_shared_memory_handle)) { | 44 &duped_shared_memory_handle)) { |
50 DVLOG(0) << "Failed to duplicate shared memory handle."; | 45 DVLOG(0) << "Failed to duplicate shared memory handle."; |
51 return false; | 46 return false; |
52 } | 47 } |
53 | 48 |
54 // Minimize the amount of adress space we use but make sure offset is a | 49 // Minimize the amount of adress space we use but make sure offset is a |
55 // multiple of page size as required by MapAt(). | 50 // multiple of page size as required by MapAt(). |
56 size_t memory_offset = offset % base::SysInfo::VMAllocationGranularity(); | 51 size_t memory_offset = offset % base::SysInfo::VMAllocationGranularity(); |
57 size_t map_offset = base::SysInfo::VMAllocationGranularity() * | 52 size_t map_offset = base::SysInfo::VMAllocationGranularity() * |
58 (offset / base::SysInfo::VMAllocationGranularity()); | 53 (offset / base::SysInfo::VMAllocationGranularity()); |
59 | 54 |
60 checked_size += memory_offset; | 55 base::CheckedNumeric<size_t> checked_size_to_map_in_bytes = size_in_bytes; |
61 if (!checked_size.IsValid()) | 56 checked_size_to_map_in_bytes += memory_offset; |
| 57 if (!checked_size_to_map_in_bytes.IsValid()) |
62 return false; | 58 return false; |
63 | 59 |
64 scoped_ptr<base::SharedMemory> duped_shared_memory( | 60 scoped_ptr<base::SharedMemory> duped_shared_memory( |
65 new base::SharedMemory(duped_shared_memory_handle, true)); | 61 new base::SharedMemory(duped_shared_memory_handle, true)); |
66 if (!duped_shared_memory->MapAt(static_cast<off_t>(map_offset), | 62 if (!duped_shared_memory->MapAt(static_cast<off_t>(map_offset), |
67 checked_size.ValueOrDie())) { | 63 checked_size_to_map_in_bytes.ValueOrDie())) { |
68 DVLOG(0) << "Failed to map shared memory."; | 64 DVLOG(0) << "Failed to map shared memory."; |
69 return false; | 65 return false; |
70 } | 66 } |
71 | 67 |
72 if (!GLImageMemory::Initialize( | 68 if (!GLImageMemory::Initialize( |
73 static_cast<uint8_t*>(duped_shared_memory->memory()) + memory_offset, | 69 static_cast<uint8_t*>(duped_shared_memory->memory()) + memory_offset, |
74 format, stride)) { | 70 format)) { |
75 return false; | 71 return false; |
76 } | 72 } |
77 | 73 |
78 DCHECK(!shared_memory_); | 74 DCHECK(!shared_memory_); |
79 shared_memory_ = duped_shared_memory.Pass(); | 75 shared_memory_ = duped_shared_memory.Pass(); |
80 shared_memory_id_ = shared_memory_id; | 76 shared_memory_id_ = shared_memory_id; |
81 return true; | 77 return true; |
82 } | 78 } |
83 | 79 |
84 void GLImageSharedMemory::Destroy(bool have_context) { | 80 void GLImageSharedMemory::Destroy(bool have_context) { |
85 GLImageMemory::Destroy(have_context); | 81 GLImageMemory::Destroy(have_context); |
86 shared_memory_.reset(); | 82 shared_memory_.reset(); |
87 } | 83 } |
88 | 84 |
89 void GLImageSharedMemory::OnMemoryDump( | 85 void GLImageSharedMemory::OnMemoryDump( |
90 base::trace_event::ProcessMemoryDump* pmd, | 86 base::trace_event::ProcessMemoryDump* pmd, |
91 uint64_t process_tracing_id, | 87 uint64_t process_tracing_id, |
92 const std::string& dump_name) { | 88 const std::string& dump_name) { |
93 size_t size_in_bytes = 0; | 89 size_t size_in_bytes = 0; |
94 | 90 |
95 if (shared_memory_) | 91 if (shared_memory_) |
96 size_in_bytes = stride() * GetSize().height(); | 92 size_in_bytes = BufferSizeForBufferFormat(GetSize(), format()); |
97 | 93 |
98 // Dump under "/shared_memory", as the base class may also dump to | 94 // Dump under "/shared_memory", as the base class may also dump to |
99 // "/texture_memory". | 95 // "/texture_memory". |
100 base::trace_event::MemoryAllocatorDump* dump = | 96 base::trace_event::MemoryAllocatorDump* dump = |
101 pmd->CreateAllocatorDump(dump_name + "/shared_memory"); | 97 pmd->CreateAllocatorDump(dump_name + "/shared_memory"); |
102 dump->AddScalar(base::trace_event::MemoryAllocatorDump::kNameSize, | 98 dump->AddScalar(base::trace_event::MemoryAllocatorDump::kNameSize, |
103 base::trace_event::MemoryAllocatorDump::kUnitsBytes, | 99 base::trace_event::MemoryAllocatorDump::kUnitsBytes, |
104 static_cast<uint64_t>(size_in_bytes)); | 100 static_cast<uint64_t>(size_in_bytes)); |
105 | 101 |
106 auto guid = GetGenericSharedMemoryGUIDForTracing(process_tracing_id, | 102 auto guid = GetGenericSharedMemoryGUIDForTracing(process_tracing_id, |
107 shared_memory_id_); | 103 shared_memory_id_); |
108 pmd->CreateSharedGlobalAllocatorDump(guid); | 104 pmd->CreateSharedGlobalAllocatorDump(guid); |
109 pmd->AddOwnershipEdge(dump->guid(), guid); | 105 pmd->AddOwnershipEdge(dump->guid(), guid); |
110 } | 106 } |
111 | 107 |
112 } // namespace gl | 108 } // namespace gl |
OLD | NEW |