Re-land: content: Refactor GPU memory buffer framework.
[chromium-blink-merge.git] / base / memory / discardable_memory_android.cc
blobde7112457dea7a17ce0d6483af7dc931168f1261
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/memory/discardable_memory.h"
7 #include "base/basictypes.h"
8 #include "base/compiler_specific.h"
9 #include "base/lazy_instance.h"
10 #include "base/logging.h"
11 #include "base/memory/discardable_memory_ashmem.h"
12 #include "base/memory/discardable_memory_ashmem_allocator.h"
13 #include "base/memory/discardable_memory_emulated.h"
14 #include "base/memory/discardable_memory_shmem.h"
15 #include "base/sys_info.h"
17 namespace base {
18 namespace {
20 const char kAshmemAllocatorName[] = "DiscardableMemoryAshmemAllocator";
22 // For Ashmem, have the DiscardableMemoryManager trigger userspace eviction
23 // when address space usage gets too high (e.g. 512 MBytes).
24 const size_t kAshmemMemoryLimit = 512 * 1024 * 1024;
26 size_t GetOptimalAshmemRegionSizeForAllocator() {
27 // Note that this may do some I/O (without hitting the disk though) so it
28 // should not be called on the critical path.
29 return base::SysInfo::AmountOfPhysicalMemory() / 8;
32 // Holds the shared state used for allocations.
33 struct SharedState {
34 SharedState()
35 : manager(kAshmemMemoryLimit, kAshmemMemoryLimit, TimeDelta::Max()),
36 allocator(kAshmemAllocatorName,
37 GetOptimalAshmemRegionSizeForAllocator()) {}
39 internal::DiscardableMemoryManager manager;
40 internal::DiscardableMemoryAshmemAllocator allocator;
42 LazyInstance<SharedState>::Leaky g_shared_state = LAZY_INSTANCE_INITIALIZER;
44 } // namespace
46 // static
47 void DiscardableMemory::ReleaseFreeMemory() {
48 internal::DiscardableMemoryShmem::ReleaseFreeMemory();
51 // static
52 bool DiscardableMemory::ReduceMemoryUsage() {
53 return internal::DiscardableMemoryEmulated::ReduceMemoryUsage();
56 // static
57 void DiscardableMemory::GetSupportedTypes(
58 std::vector<DiscardableMemoryType>* types) {
59 const DiscardableMemoryType supported_types[] = {
60 DISCARDABLE_MEMORY_TYPE_ASHMEM,
61 DISCARDABLE_MEMORY_TYPE_EMULATED,
62 DISCARDABLE_MEMORY_TYPE_SHMEM
64 types->assign(supported_types, supported_types + arraysize(supported_types));
67 // static
68 scoped_ptr<DiscardableMemory> DiscardableMemory::CreateLockedMemoryWithType(
69 DiscardableMemoryType type, size_t size) {
70 switch (type) {
71 case DISCARDABLE_MEMORY_TYPE_ASHMEM: {
72 SharedState* const shared_state = g_shared_state.Pointer();
73 scoped_ptr<internal::DiscardableMemoryAshmem> memory(
74 new internal::DiscardableMemoryAshmem(
75 size, &shared_state->allocator, &shared_state->manager));
76 if (!memory->Initialize())
77 return nullptr;
79 return memory.Pass();
81 case DISCARDABLE_MEMORY_TYPE_EMULATED: {
82 scoped_ptr<internal::DiscardableMemoryEmulated> memory(
83 new internal::DiscardableMemoryEmulated(size));
84 if (!memory->Initialize())
85 return nullptr;
87 return memory.Pass();
89 case DISCARDABLE_MEMORY_TYPE_SHMEM: {
90 scoped_ptr<internal::DiscardableMemoryShmem> memory(
91 new internal::DiscardableMemoryShmem(size));
92 if (!memory->Initialize())
93 return nullptr;
95 return memory.Pass();
97 case DISCARDABLE_MEMORY_TYPE_NONE:
98 case DISCARDABLE_MEMORY_TYPE_MACH:
99 NOTREACHED();
100 return nullptr;
103 NOTREACHED();
104 return nullptr;
107 // static
108 void DiscardableMemory::PurgeForTesting() {
109 g_shared_state.Pointer()->manager.PurgeAll();
110 internal::DiscardableMemoryEmulated::PurgeForTesting();
111 internal::DiscardableMemoryShmem::PurgeForTesting();
114 } // namespace base