| 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) { |
| 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.get()); |
| 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 | 80 |
| 77 // Clear out singleton. | 81 // Remove ourself from the map. |
| 78 ASSERT(this == s_loader); | 82 if (s_loaderMap) |
| 79 s_loader = 0; | 83 s_loaderMap->remove(m_databaseSampleRate); |
| 80 } | 84 } |
| 81 | 85 |
| 82 | |
| 83 // Asynchronously load the database in this thread. | 86 // Asynchronously load the database in this thread. |
| 84 static void databaseLoaderEntry(void* threadData) | 87 static void databaseLoaderEntry(void* threadData) |
| 85 { | 88 { |
| 86 HRTFDatabaseLoader* loader = reinterpret_cast<HRTFDatabaseLoader*>(threadDat
a); | 89 HRTFDatabaseLoader* loader = reinterpret_cast<HRTFDatabaseLoader*>(threadDat
a); |
| 87 ASSERT(loader); | 90 ASSERT(loader); |
| 88 loader->load(); | 91 loader->load(); |
| 89 } | 92 } |
| 90 | 93 |
| 91 void HRTFDatabaseLoader::load() | 94 void HRTFDatabaseLoader::load() |
| 92 { | 95 { |
| (...skipping 24 matching lines...) Expand all Loading... |
| 117 void HRTFDatabaseLoader::waitForLoaderThreadCompletion() | 120 void HRTFDatabaseLoader::waitForLoaderThreadCompletion() |
| 118 { | 121 { |
| 119 MutexLocker locker(m_threadLock); | 122 MutexLocker locker(m_threadLock); |
| 120 | 123 |
| 121 // waitForThreadCompletion() should not be called twice for the same thread. | 124 // waitForThreadCompletion() should not be called twice for the same thread. |
| 122 if (m_databaseLoaderThread) | 125 if (m_databaseLoaderThread) |
| 123 waitForThreadCompletion(m_databaseLoaderThread); | 126 waitForThreadCompletion(m_databaseLoaderThread); |
| 124 m_databaseLoaderThread = 0; | 127 m_databaseLoaderThread = 0; |
| 125 } | 128 } |
| 126 | 129 |
| 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 | 130 void HRTFDatabaseLoader::reportMemoryUsage(MemoryObjectInfo* memoryObjectInfo) c
onst |
| 136 { | 131 { |
| 137 MemoryClassInfo info(memoryObjectInfo, this, PlatformMemoryTypes::AudioShare
dData); | 132 MemoryClassInfo info(memoryObjectInfo, this, PlatformMemoryTypes::AudioShare
dData); |
| 138 info.addMember(m_hrtfDatabase, "hrtfDatabase"); | 133 info.addMember(m_hrtfDatabase, "hrtfDatabase"); |
| 139 } | 134 } |
| 140 | 135 |
| 136 void HRTFDatabaseLoader::LoaderMap::reportMemoryUsage(MemoryObjectInfo* memoryOb
jectInfo) const |
| 137 { |
| 138 ASSERT(isMainThread()); |
| 139 |
| 140 if (s_loaderMap) { |
| 141 for (HRTFDatabaseLoader::LoaderMap::iterator i = s_loaderMap->begin(); i
!= s_loaderMap->end(); ++i) { |
| 142 HRTFDatabaseLoader* loader = i.get()->value; |
| 143 loader->reportMemoryUsage(memoryObjectInfo); |
| 144 } |
| 145 } |
| 146 } |
| 147 |
| 141 } // namespace WebCore | 148 } // namespace WebCore |
| 142 | 149 |
| 143 #endif // ENABLE(WEB_AUDIO) | 150 #endif // ENABLE(WEB_AUDIO) |
| OLD | NEW |