OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright (C) 2010 Google Inc. All rights reserved. | 2 * Copyright (C) 2010 Google Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions | 5 * modification, are permitted provided that the following conditions |
6 * are met: | 6 * are met: |
7 * | 7 * |
8 * 1. Redistributions of source code must retain the above copyright | 8 * 1. Redistributions of source code must retain the above copyright |
9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
10 * 2. Redistributions in binary form must reproduce the above copyright | 10 * 2. Redistributions in binary form must reproduce the above copyright |
(...skipping 21 matching lines...) Expand all Loading... | |
32 | 32 |
33 #include "core/platform/audio/HRTFDatabaseLoader.h" | 33 #include "core/platform/audio/HRTFDatabaseLoader.h" |
34 | 34 |
35 #include "core/platform/PlatformMemoryInstrumentation.h" | 35 #include "core/platform/PlatformMemoryInstrumentation.h" |
36 #include "core/platform/audio/HRTFDatabase.h" | 36 #include "core/platform/audio/HRTFDatabase.h" |
37 #include <wtf/MainThread.h> | 37 #include <wtf/MainThread.h> |
38 | 38 |
39 namespace WebCore { | 39 namespace WebCore { |
40 | 40 |
41 // Singleton | 41 // Singleton |
42 HRTFDatabaseLoader* HRTFDatabaseLoader::s_loader = 0; | 42 HRTFDatabaseLoader::LoaderMap* HRTFDatabaseLoader::s_loaderMap = 0; |
43 | 43 |
44 PassRefPtr<HRTFDatabaseLoader> HRTFDatabaseLoader::createAndLoadAsynchronouslyIf Necessary(float sampleRate) | 44 PassRefPtr<HRTFDatabaseLoader> HRTFDatabaseLoader::createAndLoadAsynchronouslyIf Necessary(float sampleRate) |
45 { | 45 { |
46 ASSERT(isMainThread()); | 46 ASSERT(isMainThread()); |
47 | 47 |
48 RefPtr<HRTFDatabaseLoader> loader; | 48 RefPtr<HRTFDatabaseLoader> loader; |
49 | 49 |
50 if (!s_loader) { | 50 if (!s_loaderMap) |
51 // Lazily create and load. | 51 s_loaderMap = adoptPtr(new LoaderMap()).leakPtr(); |
52 loader = adoptRef(new HRTFDatabaseLoader(sampleRate)); | 52 |
53 s_loader = loader.get(); | 53 loader = s_loaderMap->get(sampleRate); |
54 loader->loadAsynchronously(); | 54 if (loader.get()) { |
Ken Russell (switch to Gerrit)
2013/05/11 01:35:16
RefPtr has a conversion operator to bool, so you s
Chris Rogers
2013/05/13 20:05:22
Done.
| |
55 } else { | |
56 loader = s_loader; | |
57 ASSERT(sampleRate == loader->databaseSampleRate()); | 55 ASSERT(sampleRate == loader->databaseSampleRate()); |
56 return loader; | |
58 } | 57 } |
59 | 58 |
59 loader = adoptRef(new HRTFDatabaseLoader(sampleRate)); | |
60 s_loaderMap->add(sampleRate, loader); | |
61 | |
62 loader->loadAsynchronously(); | |
63 | |
60 return loader; | 64 return loader; |
61 } | 65 } |
62 | 66 |
63 HRTFDatabaseLoader::HRTFDatabaseLoader(float sampleRate) | 67 HRTFDatabaseLoader::HRTFDatabaseLoader(float sampleRate) |
64 : m_databaseLoaderThread(0) | 68 : m_databaseLoaderThread(0) |
65 , m_databaseSampleRate(sampleRate) | 69 , m_databaseSampleRate(sampleRate) |
66 { | 70 { |
67 ASSERT(isMainThread()); | 71 ASSERT(isMainThread()); |
68 } | 72 } |
69 | 73 |
70 HRTFDatabaseLoader::~HRTFDatabaseLoader() | 74 HRTFDatabaseLoader::~HRTFDatabaseLoader() |
71 { | 75 { |
72 ASSERT(isMainThread()); | 76 ASSERT(isMainThread()); |
73 | 77 |
74 waitForLoaderThreadCompletion(); | 78 waitForLoaderThreadCompletion(); |
75 m_hrtfDatabase.clear(); | 79 m_hrtfDatabase.clear(); |
76 | |
77 // Clear out singleton. | |
78 ASSERT(this == s_loader); | |
79 s_loader = 0; | |
80 } | 80 } |
81 | 81 |
82 | |
83 // Asynchronously load the database in this thread. | 82 // Asynchronously load the database in this thread. |
84 static void databaseLoaderEntry(void* threadData) | 83 static void databaseLoaderEntry(void* threadData) |
85 { | 84 { |
86 HRTFDatabaseLoader* loader = reinterpret_cast<HRTFDatabaseLoader*>(threadDat a); | 85 HRTFDatabaseLoader* loader = reinterpret_cast<HRTFDatabaseLoader*>(threadDat a); |
87 ASSERT(loader); | 86 ASSERT(loader); |
88 loader->load(); | 87 loader->load(); |
89 } | 88 } |
90 | 89 |
91 void HRTFDatabaseLoader::load() | 90 void HRTFDatabaseLoader::load() |
92 { | 91 { |
(...skipping 24 matching lines...) Expand all Loading... | |
117 void HRTFDatabaseLoader::waitForLoaderThreadCompletion() | 116 void HRTFDatabaseLoader::waitForLoaderThreadCompletion() |
118 { | 117 { |
119 MutexLocker locker(m_threadLock); | 118 MutexLocker locker(m_threadLock); |
120 | 119 |
121 // waitForThreadCompletion() should not be called twice for the same thread. | 120 // waitForThreadCompletion() should not be called twice for the same thread. |
122 if (m_databaseLoaderThread) | 121 if (m_databaseLoaderThread) |
123 waitForThreadCompletion(m_databaseLoaderThread); | 122 waitForThreadCompletion(m_databaseLoaderThread); |
124 m_databaseLoaderThread = 0; | 123 m_databaseLoaderThread = 0; |
125 } | 124 } |
126 | 125 |
127 HRTFDatabase* HRTFDatabaseLoader::defaultHRTFDatabase() | |
128 { | |
129 if (!s_loader) | |
130 return 0; | |
131 | |
132 return s_loader->database(); | |
133 } | |
134 | |
135 void HRTFDatabaseLoader::reportMemoryUsage(MemoryObjectInfo* memoryObjectInfo) c onst | 126 void HRTFDatabaseLoader::reportMemoryUsage(MemoryObjectInfo* memoryObjectInfo) c onst |
136 { | 127 { |
137 MemoryClassInfo info(memoryObjectInfo, this, PlatformMemoryTypes::AudioShare dData); | 128 MemoryClassInfo info(memoryObjectInfo, this, PlatformMemoryTypes::AudioShare dData); |
138 info.addMember(m_hrtfDatabase, "hrtfDatabase"); | 129 info.addMember(m_hrtfDatabase, "hrtfDatabase"); |
139 } | 130 } |
140 | 131 |
132 void HRTFDatabaseLoader::LoaderMap::reportMemoryUsage(MemoryObjectInfo* memoryOb jectInfo) const | |
133 { | |
134 if (s_loaderMap) { | |
135 for (HRTFDatabaseLoader::LoaderMap::iterator i = s_loaderMap->begin(); i != s_loaderMap->end(); ++i) { | |
136 HRTFDatabaseLoader* loader = i.get()->value.get(); | |
137 loader->reportMemoryUsage(memoryObjectInfo); | |
138 } | |
139 } | |
140 } | |
141 | |
141 } // namespace WebCore | 142 } // namespace WebCore |
142 | 143 |
143 #endif // ENABLE(WEB_AUDIO) | 144 #endif // ENABLE(WEB_AUDIO) |
OLD | NEW |