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" |
| 11 #include "base/sys_info.h" |
11 #include "base/trace_event/memory_allocator_dump.h" | 12 #include "base/trace_event/memory_allocator_dump.h" |
12 #include "base/trace_event/memory_dump_manager.h" | 13 #include "base/trace_event/memory_dump_manager.h" |
13 #include "base/trace_event/process_memory_dump.h" | 14 #include "base/trace_event/process_memory_dump.h" |
14 #include "ui/gfx/buffer_format_util.h" | 15 #include "ui/gfx/buffer_format_util.h" |
15 | 16 |
16 namespace gfx { | 17 namespace gfx { |
17 | 18 |
18 GLImageSharedMemory::GLImageSharedMemory(const Size& size, | 19 GLImageSharedMemory::GLImageSharedMemory(const Size& size, |
19 unsigned internalformat) | 20 unsigned internalformat) |
20 : GLImageMemory(size, internalformat) {} | 21 : GLImageMemory(size, internalformat) {} |
21 | 22 |
22 GLImageSharedMemory::~GLImageSharedMemory() { | 23 GLImageSharedMemory::~GLImageSharedMemory() { |
23 DCHECK(!shared_memory_); | 24 DCHECK(!shared_memory_); |
24 } | 25 } |
25 | 26 |
26 bool GLImageSharedMemory::Initialize(const base::SharedMemoryHandle& handle, | 27 bool GLImageSharedMemory::Initialize(const base::SharedMemoryHandle& handle, |
27 GenericSharedMemoryId shared_memory_id, | 28 GenericSharedMemoryId shared_memory_id, |
28 BufferFormat format) { | 29 BufferFormat format, |
| 30 size_t offset) { |
29 size_t size_in_bytes; | 31 size_t size_in_bytes; |
30 if (!BufferSizeForBufferFormatChecked(GetSize(), format, &size_in_bytes)) | 32 if (!BufferSizeForBufferFormatChecked(GetSize(), format, &size_in_bytes)) |
31 return false; | 33 return false; |
32 | 34 |
33 if (!base::SharedMemory::IsHandleValid(handle)) | 35 if (!base::SharedMemory::IsHandleValid(handle)) |
34 return false; | 36 return false; |
35 | 37 |
36 base::SharedMemory shared_memory(handle, true); | 38 base::SharedMemory shared_memory(handle, true); |
37 | 39 |
38 // Duplicate the handle. | 40 // Duplicate the handle. |
39 base::SharedMemoryHandle duped_shared_memory_handle; | 41 base::SharedMemoryHandle duped_shared_memory_handle; |
40 if (!shared_memory.ShareToProcess(base::GetCurrentProcessHandle(), | 42 if (!shared_memory.ShareToProcess(base::GetCurrentProcessHandle(), |
41 &duped_shared_memory_handle)) { | 43 &duped_shared_memory_handle)) { |
42 DVLOG(0) << "Failed to duplicate shared memory handle."; | 44 DVLOG(0) << "Failed to duplicate shared memory handle."; |
43 return false; | 45 return false; |
44 } | 46 } |
45 | 47 |
| 48 // Minimize the amount of adress space we use but make sure offset is a |
| 49 // multiple of page size as required by MapAt(). |
| 50 size_t memory_offset = offset % base::SysInfo::VMAllocationGranularity(); |
| 51 size_t map_offset = base::SysInfo::VMAllocationGranularity() * |
| 52 (offset / base::SysInfo::VMAllocationGranularity()); |
| 53 |
| 54 base::CheckedNumeric<size_t> checked_size_to_map_in_bytes = size_in_bytes; |
| 55 checked_size_to_map_in_bytes += memory_offset; |
| 56 if (!checked_size_to_map_in_bytes.IsValid()) |
| 57 return false; |
| 58 |
46 scoped_ptr<base::SharedMemory> duped_shared_memory( | 59 scoped_ptr<base::SharedMemory> duped_shared_memory( |
47 new base::SharedMemory(duped_shared_memory_handle, true)); | 60 new base::SharedMemory(duped_shared_memory_handle, true)); |
48 if (!duped_shared_memory->Map(size_in_bytes)) { | 61 if (!duped_shared_memory->MapAt(static_cast<off_t>(map_offset), |
| 62 checked_size_to_map_in_bytes.ValueOrDie())) { |
49 DVLOG(0) << "Failed to map shared memory."; | 63 DVLOG(0) << "Failed to map shared memory."; |
50 return false; | 64 return false; |
51 } | 65 } |
52 | 66 |
53 if (!GLImageMemory::Initialize( | 67 if (!GLImageMemory::Initialize( |
54 static_cast<unsigned char*>(duped_shared_memory->memory()), format)) { | 68 static_cast<uint8_t*>(duped_shared_memory->memory()) + memory_offset, |
| 69 format)) { |
55 return false; | 70 return false; |
56 } | 71 } |
57 | 72 |
58 DCHECK(!shared_memory_); | 73 DCHECK(!shared_memory_); |
59 shared_memory_ = duped_shared_memory.Pass(); | 74 shared_memory_ = duped_shared_memory.Pass(); |
60 shared_memory_id_ = shared_memory_id; | 75 shared_memory_id_ = shared_memory_id; |
61 return true; | 76 return true; |
62 } | 77 } |
63 | 78 |
64 void GLImageSharedMemory::Destroy(bool have_context) { | 79 void GLImageSharedMemory::Destroy(bool have_context) { |
(...skipping 18 matching lines...) Expand all Loading... |
83 base::trace_event::MemoryAllocatorDump::kUnitsBytes, | 98 base::trace_event::MemoryAllocatorDump::kUnitsBytes, |
84 static_cast<uint64_t>(size_in_bytes)); | 99 static_cast<uint64_t>(size_in_bytes)); |
85 | 100 |
86 auto guid = GetGenericSharedMemoryGUIDForTracing(process_tracing_id, | 101 auto guid = GetGenericSharedMemoryGUIDForTracing(process_tracing_id, |
87 shared_memory_id_); | 102 shared_memory_id_); |
88 pmd->CreateSharedGlobalAllocatorDump(guid); | 103 pmd->CreateSharedGlobalAllocatorDump(guid); |
89 pmd->AddOwnershipEdge(dump->guid(), guid); | 104 pmd->AddOwnershipEdge(dump->guid(), guid); |
90 } | 105 } |
91 | 106 |
92 } // namespace gfx | 107 } // namespace gfx |
OLD | NEW |