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