Bug 831193 (part 16) - Don't use NS_MEMORY_REPORTER_IMPLEMENT in media/. r=kinetik.
authorNicholas Nethercote <nnethercote@mozilla.com>
Thu, 17 Jan 2013 21:43:20 -0800
changeset 144655 15a219d9119ee956a2216ddc289fe593ba337000
parent 144654 95e4bc825071bb6dd6026ad20022acd3ae76926c
child 144656 24b954d5230c8f6df5c1a021d0a3bd4629b21ca0
push id33045
push usernnethercote@mozilla.com
push dateWed, 28 Aug 2013 08:59:06 +0000
treeherdermozilla-inbound@eead1e72622d [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerskinetik
bugs831193
milestone26.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 831193 (part 16) - Don't use NS_MEMORY_REPORTER_IMPLEMENT in media/. r=kinetik.
content/media/MediaDecoder.cpp
--- a/content/media/MediaDecoder.cpp
+++ b/content/media/MediaDecoder.cpp
@@ -1,10 +1,10 @@
-/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
-/* vim:set ts=2 sw=2 sts=2 et cindent: */
+/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* vim: set ts=8 sts=2 et sw=2 tw=80: */
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #include "MediaDecoder.h"
 #include "mozilla/FloatingPoint.h"
 #include "mozilla/MathAlgorithms.h"
 #include <limits>
@@ -49,72 +49,68 @@ static const int64_t CAN_PLAY_THROUGH_MA
 
 #ifdef PR_LOGGING
 PRLogModuleInfo* gMediaDecoderLog;
 #define LOG(type, msg) PR_LOG(gMediaDecoderLog, type, msg)
 #else
 #define LOG(type, msg)
 #endif
 
-class MediaMemoryReporter
+class MediaMemoryTracker
 {
-  MediaMemoryReporter();
-  ~MediaMemoryReporter();
-  static MediaMemoryReporter* sUniqueInstance;
+  MediaMemoryTracker();
+  ~MediaMemoryTracker();
+  static MediaMemoryTracker* sUniqueInstance;
 
-  static MediaMemoryReporter* UniqueInstance() {
+  static MediaMemoryTracker* UniqueInstance() {
     if (!sUniqueInstance) {
-      sUniqueInstance = new MediaMemoryReporter;
+      sUniqueInstance = new MediaMemoryTracker();
     }
     return sUniqueInstance;
   }
 
   typedef nsTArray<MediaDecoder*> DecodersArray;
   static DecodersArray& Decoders() {
     return UniqueInstance()->mDecoders;
   }
 
   DecodersArray mDecoders;
 
-  nsCOMPtr<nsIMemoryReporter> mMediaDecodedVideoMemory;
-  nsCOMPtr<nsIMemoryReporter> mMediaDecodedAudioMemory;
+  nsCOMPtr<nsIMemoryMultiReporter> mReporter;
 
 public:
-  static void AddMediaDecoder(MediaDecoder* aDecoder) {
+  static void AddMediaDecoder(MediaDecoder* aDecoder)
+  {
     Decoders().AppendElement(aDecoder);
   }
 
-  static void RemoveMediaDecoder(MediaDecoder* aDecoder) {
+  static void RemoveMediaDecoder(MediaDecoder* aDecoder)
+  {
     DecodersArray& decoders = Decoders();
     decoders.RemoveElement(aDecoder);
     if (decoders.IsEmpty()) {
       delete sUniqueInstance;
       sUniqueInstance = nullptr;
     }
   }
 
-  static int64_t GetDecodedVideoMemory() {
+  static void GetAmounts(int64_t* aVideo, int64_t* aAudio)
+  {
+    *aVideo = 0;
+    *aAudio = 0;
     DecodersArray& decoders = Decoders();
-    int64_t result = 0;
     for (size_t i = 0; i < decoders.Length(); ++i) {
-      result += decoders[i]->VideoQueueMemoryInUse();
+      *aVideo += decoders[i]->VideoQueueMemoryInUse();
+      *aAudio += decoders[i]->AudioQueueMemoryInUse();
     }
-    return result;
-  }
-
-  static int64_t GetDecodedAudioMemory() {
-    DecodersArray& decoders = Decoders();
-    int64_t result = 0;
-    for (size_t i = 0; i < decoders.Length(); ++i) {
-      result += decoders[i]->AudioQueueMemoryInUse();
-    }
-    return result;
   }
 };
 
+MediaMemoryTracker* MediaMemoryTracker::sUniqueInstance = nullptr;
+
 NS_IMPL_ISUPPORTS1(MediaDecoder, nsIObserver)
 
 void MediaDecoder::SetDormantIfNecessary(bool aDormant)
 {
   MOZ_ASSERT(NS_IsMainThread());
   ReentrantMonitorAutoEnter mon(GetReentrantMonitor());
 
   if (!mDecoderStateMachine || !mDecoderStateMachine->IsDormantNeeded() || (mPlayState == PLAY_STATE_SHUTDOWN)) {
@@ -384,17 +380,17 @@ MediaDecoder::MediaDecoder() :
   mFrameBufferLength(0),
   mPinnedForSeek(false),
   mShuttingDown(false),
   mPausedForPlaybackRateNull(false),
   mAudioChannelType(AUDIO_CHANNEL_NORMAL)
 {
   MOZ_COUNT_CTOR(MediaDecoder);
   MOZ_ASSERT(NS_IsMainThread());
-  MediaMemoryReporter::AddMediaDecoder(this);
+  MediaMemoryTracker::AddMediaDecoder(this);
 #ifdef PR_LOGGING
   if (!gMediaDecoderLog) {
     gMediaDecoderLog = PR_NewLogModule("MediaDecoder");
   }
 #endif
 }
 
 bool MediaDecoder::Init(MediaDecoderOwner* aOwner)
@@ -439,17 +435,17 @@ void MediaDecoder::Shutdown()
   mOwner = nullptr;
 
   nsContentUtils::UnregisterShutdownObserver(this);
 }
 
 MediaDecoder::~MediaDecoder()
 {
   MOZ_ASSERT(NS_IsMainThread());
-  MediaMemoryReporter::RemoveMediaDecoder(this);
+  MediaMemoryTracker::RemoveMediaDecoder(this);
   UnpinForSeek();
   MOZ_COUNT_DTOR(MediaDecoder);
 }
 
 nsresult MediaDecoder::OpenResource(nsIStreamListener** aStreamListener)
 {
   MOZ_ASSERT(NS_IsMainThread());
   if (aStreamListener) {
@@ -1726,47 +1722,67 @@ MediaDecoder::IsDASHEnabled()
 #ifdef MOZ_WMF
 bool
 MediaDecoder::IsWMFEnabled()
 {
   return WMFDecoder::IsEnabled();
 }
 #endif
 
+class MediaReporter MOZ_FINAL : public nsIMemoryMultiReporter
+{
+public:
+  NS_DECL_ISUPPORTS
+
+  NS_IMETHOD GetName(nsACString& aName)
+  {
+    aName.AssignLiteral("media");
+    return NS_OK;
+  }
+
+  NS_IMETHOD CollectReports(nsIMemoryMultiReporterCallback* aCb,
+                            nsISupports* aClosure)
+  {
+    int64_t video, audio;
+    MediaMemoryTracker::GetAmounts(&video, &audio);
+
+  #define REPORT(_path, _amount, _desc)                                       \
+    do {                                                                      \
+        nsresult rv;                                                          \
+        rv = aCb->Callback(EmptyCString(), NS_LITERAL_CSTRING(_path),         \
+                           nsIMemoryReporter::KIND_HEAP,                      \
+                           nsIMemoryReporter::UNITS_BYTES, _amount,           \
+                           NS_LITERAL_CSTRING(_desc), aClosure);              \
+        NS_ENSURE_SUCCESS(rv, rv);                                            \
+    } while (0)
+
+    REPORT("explicit/media/decoded-video", video,
+           "Memory used by decoded video frames.");
+
+    REPORT("explicit/media/decoded-audio", audio,
+           "Memory used by decoded audio chunks.");
+
+    return NS_OK;
+  }
+};
+
+NS_IMPL_ISUPPORTS1(MediaReporter, nsIMemoryMultiReporter)
+
 MediaDecoderOwner*
 MediaDecoder::GetOwner()
 {
   MOZ_ASSERT(NS_IsMainThread());
   return mOwner;
 }
 
-MediaMemoryReporter* MediaMemoryReporter::sUniqueInstance;
-
-NS_MEMORY_REPORTER_IMPLEMENT(MediaDecodedVideoMemory,
-  "explicit/media/decoded-video",
-  KIND_HEAP,
-  UNITS_BYTES,
-  MediaMemoryReporter::GetDecodedVideoMemory,
-  "Memory used by decoded video frames.")
-
-NS_MEMORY_REPORTER_IMPLEMENT(MediaDecodedAudioMemory,
-  "explicit/media/decoded-audio",
-  KIND_HEAP,
-  UNITS_BYTES,
-  MediaMemoryReporter::GetDecodedAudioMemory,
-  "Memory used by decoded audio chunks.")
-
-MediaMemoryReporter::MediaMemoryReporter()
-  : mMediaDecodedVideoMemory(new NS_MEMORY_REPORTER_NAME(MediaDecodedVideoMemory))
-  , mMediaDecodedAudioMemory(new NS_MEMORY_REPORTER_NAME(MediaDecodedAudioMemory))
+MediaMemoryTracker::MediaMemoryTracker()
+  : mReporter(new MediaReporter())
 {
-  NS_RegisterMemoryReporter(mMediaDecodedVideoMemory);
-  NS_RegisterMemoryReporter(mMediaDecodedAudioMemory);
+  NS_RegisterMemoryMultiReporter(mReporter);
 }
 
-MediaMemoryReporter::~MediaMemoryReporter()
+MediaMemoryTracker::~MediaMemoryTracker()
 {
-  NS_UnregisterMemoryReporter(mMediaDecodedVideoMemory);
-  NS_UnregisterMemoryReporter(mMediaDecodedAudioMemory);
+  NS_UnregisterMemoryMultiReporter(mReporter);
 }
 
 } // namespace mozilla