content/media/webaudio/blink/HRTFDatabaseLoader.h

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /*
michael@0 2 * Copyright (C) 2010 Google Inc. All rights reserved.
michael@0 3 *
michael@0 4 * Redistribution and use in source and binary forms, with or without
michael@0 5 * modification, are permitted provided that the following conditions
michael@0 6 * are met:
michael@0 7 *
michael@0 8 * 1. Redistributions of source code must retain the above copyright
michael@0 9 * notice, this list of conditions and the following disclaimer.
michael@0 10 * 2. Redistributions in binary form must reproduce the above copyright
michael@0 11 * notice, this list of conditions and the following disclaimer in the
michael@0 12 * documentation and/or other materials provided with the distribution.
michael@0 13 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
michael@0 14 * its contributors may be used to endorse or promote products derived
michael@0 15 * from this software without specific prior written permission.
michael@0 16 *
michael@0 17 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
michael@0 18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
michael@0 19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
michael@0 20 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
michael@0 21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
michael@0 22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
michael@0 23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
michael@0 24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
michael@0 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
michael@0 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
michael@0 27 */
michael@0 28
michael@0 29 #ifndef HRTFDatabaseLoader_h
michael@0 30 #define HRTFDatabaseLoader_h
michael@0 31
michael@0 32 #include "nsHashKeys.h"
michael@0 33 #include "mozilla/RefPtr.h"
michael@0 34 #include "mozilla/MemoryReporting.h"
michael@0 35 #include "mozilla/Mutex.h"
michael@0 36 #include "HRTFDatabase.h"
michael@0 37
michael@0 38 template <class EntryType> class nsTHashtable;
michael@0 39 template <class T> class nsAutoRef;
michael@0 40
michael@0 41 namespace WebCore {
michael@0 42
michael@0 43 // HRTFDatabaseLoader will asynchronously load the default HRTFDatabase in a new thread.
michael@0 44
michael@0 45 class HRTFDatabaseLoader {
michael@0 46 public:
michael@0 47 // Lazily creates a HRTFDatabaseLoader (if not already created) for the given sample-rate
michael@0 48 // and starts loading asynchronously (when created the first time).
michael@0 49 // Returns the HRTFDatabaseLoader.
michael@0 50 // Must be called from the main thread.
michael@0 51 static mozilla::TemporaryRef<HRTFDatabaseLoader> createAndLoadAsynchronouslyIfNecessary(float sampleRate);
michael@0 52
michael@0 53 // AddRef and Release may be called from any thread.
michael@0 54 void AddRef()
michael@0 55 {
michael@0 56 #if defined(DEBUG) || defined(NS_BUILD_REFCNT_LOGGING)
michael@0 57 int count =
michael@0 58 #endif
michael@0 59 ++m_refCnt;
michael@0 60 MOZ_ASSERT(count > 0, "invalid ref count");
michael@0 61 NS_LOG_ADDREF(this, count, "HRTFDatabaseLoader", sizeof(*this));
michael@0 62 }
michael@0 63
michael@0 64 void Release()
michael@0 65 {
michael@0 66 // The last reference can't be removed on a non-main thread because
michael@0 67 // the object can be accessed on the main thread from the hash
michael@0 68 // table via createAndLoadAsynchronouslyIfNecessary().
michael@0 69 int count = m_refCnt;
michael@0 70 MOZ_ASSERT(count > 0, "extra release");
michael@0 71 // Optimization attempt to possibly skip proxying the release to the
michael@0 72 // main thread.
michael@0 73 if (count != 1 && m_refCnt.compareExchange(count, count - 1)) {
michael@0 74 NS_LOG_RELEASE(this, count - 1, "HRTFDatabaseLoader");
michael@0 75 return;
michael@0 76 }
michael@0 77
michael@0 78 ProxyRelease();
michael@0 79 }
michael@0 80
michael@0 81 // Returns true once the default database has been completely loaded.
michael@0 82 bool isLoaded() const;
michael@0 83
michael@0 84 // waitForLoaderThreadCompletion() may be called more than once,
michael@0 85 // on any thread except m_databaseLoaderThread.
michael@0 86 void waitForLoaderThreadCompletion();
michael@0 87
michael@0 88 HRTFDatabase* database() { return m_hrtfDatabase.get(); }
michael@0 89
michael@0 90 float databaseSampleRate() const { return m_databaseSampleRate; }
michael@0 91
michael@0 92 static void shutdown();
michael@0 93
michael@0 94 // Called in asynchronous loading thread.
michael@0 95 void load();
michael@0 96
michael@0 97 size_t sizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const;
michael@0 98
michael@0 99 private:
michael@0 100 // Both constructor and destructor must be called from the main thread.
michael@0 101 explicit HRTFDatabaseLoader(float sampleRate);
michael@0 102 ~HRTFDatabaseLoader();
michael@0 103
michael@0 104 void ProxyRelease(); // any thread
michael@0 105 void MainThreadRelease(); // main thread only
michael@0 106 class ProxyReleaseEvent;
michael@0 107
michael@0 108 // If it hasn't already been loaded, creates a new thread and initiates asynchronous loading of the default database.
michael@0 109 // This must be called from the main thread.
michael@0 110 void loadAsynchronously();
michael@0 111
michael@0 112 // Map from sample-rate to loader.
michael@0 113 class LoaderByRateEntry : public nsFloatHashKey {
michael@0 114 public:
michael@0 115 LoaderByRateEntry(KeyTypePointer aKey)
michael@0 116 : nsFloatHashKey(aKey)
michael@0 117 , mLoader() // so PutEntry() will zero-initialize
michael@0 118 {
michael@0 119 }
michael@0 120 HRTFDatabaseLoader* mLoader;
michael@0 121 };
michael@0 122
michael@0 123 static PLDHashOperator shutdownEnumFunc(LoaderByRateEntry *entry,
michael@0 124 void* unused);
michael@0 125
michael@0 126 // Keeps track of loaders on a per-sample-rate basis.
michael@0 127 static nsTHashtable<LoaderByRateEntry> *s_loaderMap; // singleton
michael@0 128
michael@0 129 mozilla::Atomic<int> m_refCnt;
michael@0 130
michael@0 131 nsAutoRef<HRTFDatabase> m_hrtfDatabase;
michael@0 132
michael@0 133 // Holding a m_threadLock is required when accessing m_databaseLoaderThread.
michael@0 134 mozilla::Mutex m_threadLock;
michael@0 135 PRThread* m_databaseLoaderThread;
michael@0 136
michael@0 137 float m_databaseSampleRate;
michael@0 138 };
michael@0 139
michael@0 140 } // namespace WebCore
michael@0 141
michael@0 142 #endif // HRTFDatabaseLoader_h

mercurial