| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "gpu/command_buffer/client/mapped_memory.h" | 5 #include "gpu/command_buffer/client/mapped_memory.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <algorithm> | 10 #include <algorithm> |
| 11 #include <functional> | 11 #include <functional> |
| 12 | 12 |
| 13 #include "base/atomic_sequence_num.h" | 13 #include "base/atomic_sequence_num.h" |
| 14 #include "base/logging.h" | 14 #include "base/logging.h" |
| 15 #include "base/memory/ptr_util.h" | 15 #include "base/memory/ptr_util.h" |
| 16 #include "base/strings/stringprintf.h" | 16 #include "base/strings/stringprintf.h" |
| 17 #include "base/thread_task_runner_handle.h" | 17 #include "base/thread_task_runner_handle.h" |
| 18 #include "base/trace_event/memory_dump_manager.h" | 18 #include "base/trace_event/memory_dump_manager.h" |
| 19 #include "base/trace_event/trace_event.h" | 19 #include "base/trace_event/trace_event.h" |
| 20 #include "gpu/command_buffer/client/cmd_buffer_helper.h" | 20 #include "gpu/command_buffer/client/cmd_buffer_helper.h" |
| 21 #include "gpu/command_buffer/client/shared_memory_limits.h" |
| 21 #include "gpu/command_buffer/common/buffer.h" | 22 #include "gpu/command_buffer/common/buffer.h" |
| 22 | 23 |
| 23 namespace gpu { | 24 namespace gpu { |
| 24 namespace { | 25 namespace { |
| 25 | 26 |
| 26 // Generates process-unique IDs to use for tracing a MappedMemoryManager's | 27 // Generates process-unique IDs to use for tracing a MappedMemoryManager's |
| 27 // chunks. | 28 // chunks. |
| 28 base::StaticAtomicSequenceNumber g_next_mapped_memory_manager_tracing_id; | 29 base::StaticAtomicSequenceNumber g_next_mapped_memory_manager_tracing_id; |
| 29 | 30 |
| 30 } // namespace | 31 } // namespace |
| 31 | 32 |
| 32 MemoryChunk::MemoryChunk(int32_t shm_id, | 33 MemoryChunk::MemoryChunk(int32_t shm_id, |
| 33 scoped_refptr<gpu::Buffer> shm, | 34 scoped_refptr<gpu::Buffer> shm, |
| 34 CommandBufferHelper* helper) | 35 CommandBufferHelper* helper) |
| 35 : shm_id_(shm_id), | 36 : shm_id_(shm_id), |
| 36 shm_(shm), | 37 shm_(shm), |
| 37 allocator_(shm->size(), helper, shm->memory()) {} | 38 allocator_(shm->size(), helper, shm->memory()) {} |
| 38 | 39 |
| 39 MemoryChunk::~MemoryChunk() {} | 40 MemoryChunk::~MemoryChunk() {} |
| 40 | 41 |
| 41 MappedMemoryManager::MappedMemoryManager(CommandBufferHelper* helper, | 42 MappedMemoryManager::MappedMemoryManager(CommandBufferHelper* helper, |
| 42 size_t unused_memory_reclaim_limit) | 43 size_t unused_memory_reclaim_limit) |
| 43 : chunk_size_multiple_(FencedAllocator::kAllocAlignment), | 44 : chunk_size_multiple_(FencedAllocator::kAllocAlignment), |
| 44 helper_(helper), | 45 helper_(helper), |
| 45 allocated_memory_(0), | 46 allocated_memory_(0), |
| 46 max_free_bytes_(unused_memory_reclaim_limit), | 47 max_free_bytes_(unused_memory_reclaim_limit), |
| 47 max_allocated_bytes_(kNoLimit), | 48 max_allocated_bytes_(SharedMemoryLimits::kNoLimit), |
| 48 tracing_id_(g_next_mapped_memory_manager_tracing_id.GetNext()) { | 49 tracing_id_(g_next_mapped_memory_manager_tracing_id.GetNext()) { |
| 49 // In certain cases, ThreadTaskRunnerHandle isn't set (Android Webview). | 50 // In certain cases, ThreadTaskRunnerHandle isn't set (Android Webview). |
| 50 // Don't register a dump provider in these cases. | 51 // Don't register a dump provider in these cases. |
| 51 // TODO(ericrk): Get this working in Android Webview. crbug.com/517156 | 52 // TODO(ericrk): Get this working in Android Webview. crbug.com/517156 |
| 52 if (base::ThreadTaskRunnerHandle::IsSet()) { | 53 if (base::ThreadTaskRunnerHandle::IsSet()) { |
| 53 base::trace_event::MemoryDumpManager::GetInstance()->RegisterDumpProvider( | 54 base::trace_event::MemoryDumpManager::GetInstance()->RegisterDumpProvider( |
| 54 this, "gpu::MappedMemoryManager", base::ThreadTaskRunnerHandle::Get()); | 55 this, "gpu::MappedMemoryManager", base::ThreadTaskRunnerHandle::Get()); |
| 55 } | 56 } |
| 56 } | 57 } |
| 57 | 58 |
| (...skipping 23 matching lines...) Expand all Loading... |
| 81 DCHECK(mem); | 82 DCHECK(mem); |
| 82 *shm_id = chunk->shm_id(); | 83 *shm_id = chunk->shm_id(); |
| 83 *shm_offset = chunk->GetOffset(mem); | 84 *shm_offset = chunk->GetOffset(mem); |
| 84 return mem; | 85 return mem; |
| 85 } | 86 } |
| 86 } | 87 } |
| 87 | 88 |
| 88 // If there is a memory limit being enforced and total free | 89 // If there is a memory limit being enforced and total free |
| 89 // memory (allocated_memory_ - total_bytes_in_use) is larger than | 90 // memory (allocated_memory_ - total_bytes_in_use) is larger than |
| 90 // the limit try waiting. | 91 // the limit try waiting. |
| 91 if (max_free_bytes_ != kNoLimit && | 92 if (max_free_bytes_ != SharedMemoryLimits::kNoLimit && |
| 92 (allocated_memory_ - total_bytes_in_use) >= max_free_bytes_) { | 93 (allocated_memory_ - total_bytes_in_use) >= max_free_bytes_) { |
| 93 TRACE_EVENT0("gpu", "MappedMemoryManager::Alloc::wait"); | 94 TRACE_EVENT0("gpu", "MappedMemoryManager::Alloc::wait"); |
| 94 for (auto& chunk : chunks_) { | 95 for (auto& chunk : chunks_) { |
| 95 if (chunk->GetLargestFreeSizeWithWaiting() >= size) { | 96 if (chunk->GetLargestFreeSizeWithWaiting() >= size) { |
| 96 void* mem = chunk->Alloc(size); | 97 void* mem = chunk->Alloc(size); |
| 97 DCHECK(mem); | 98 DCHECK(mem); |
| 98 *shm_id = chunk->shm_id(); | 99 *shm_id = chunk->shm_id(); |
| 99 *shm_offset = chunk->GetOffset(mem); | 100 *shm_offset = chunk->GetOffset(mem); |
| 100 return mem; | 101 return mem; |
| 101 } | 102 } |
| 102 } | 103 } |
| 103 } | 104 } |
| 104 } | 105 } |
| 105 | 106 |
| 106 if (max_allocated_bytes_ != kNoLimit && | 107 if (max_allocated_bytes_ != SharedMemoryLimits::kNoLimit && |
| 107 (allocated_memory_ + size) > max_allocated_bytes_) { | 108 (allocated_memory_ + size) > max_allocated_bytes_) { |
| 108 return nullptr; | 109 return nullptr; |
| 109 } | 110 } |
| 110 | 111 |
| 111 // Make a new chunk to satisfy the request. | 112 // Make a new chunk to satisfy the request. |
| 112 CommandBuffer* cmd_buf = helper_->command_buffer(); | 113 CommandBuffer* cmd_buf = helper_->command_buffer(); |
| 113 unsigned int chunk_size = | 114 unsigned int chunk_size = |
| 114 ((size + chunk_size_multiple_ - 1) / chunk_size_multiple_) * | 115 ((size + chunk_size_multiple_ - 1) / chunk_size_multiple_) * |
| 115 chunk_size_multiple_; | 116 chunk_size_multiple_; |
| 116 int32_t id = -1; | 117 int32_t id = -1; |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 211 void ScopedMappedMemoryPtr::Reset(uint32_t new_size) { | 212 void ScopedMappedMemoryPtr::Reset(uint32_t new_size) { |
| 212 Release(); | 213 Release(); |
| 213 | 214 |
| 214 if (new_size) { | 215 if (new_size) { |
| 215 buffer_ = mapped_memory_manager_->Alloc(new_size, &shm_id_, &shm_offset_); | 216 buffer_ = mapped_memory_manager_->Alloc(new_size, &shm_id_, &shm_offset_); |
| 216 size_ = buffer_ ? new_size : 0; | 217 size_ = buffer_ ? new_size : 0; |
| 217 } | 218 } |
| 218 } | 219 } |
| 219 | 220 |
| 220 } // namespace gpu | 221 } // namespace gpu |
| OLD | NEW |