Bug 1446931 - P2. Refactor code flow in Benchmark. r=gerald
authorJean-Yves Avenard <jyavenard@mozilla.com>
Fri, 30 Mar 2018 11:00:50 +0200
changeset 411696 8e0de009a63f8e4c05e16926d3dddbb1b63167e0
parent 411695 13b8bb0d5cc6d241af2b2fa64eabf94c4e137973
child 411697 c0a3a4b5e11c30462155b8d5df18edda6ad1f0af
push id101729
push usercsabou@mozilla.com
push dateWed, 04 Apr 2018 18:07:35 +0000
treeherdermozilla-inbound@3c240f56a113 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersgerald
bugs1446931
milestone61.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 1446931 - P2. Refactor code flow in Benchmark. r=gerald The code relied on a flag to be set to simply abort. However, that made the code workflow hard to read. We split each runs so that there's no ambiguity. MozReview-Commit-ID: LI7pL5p69zu
dom/media/Benchmark.cpp
--- a/dom/media/Benchmark.cpp
+++ b/dom/media/Benchmark.cpp
@@ -272,20 +272,18 @@ BenchmarkPlayback::FinalizeShutdown()
     []() { MOZ_CRASH("not reached"); });
 }
 
 void
 BenchmarkPlayback::MainThreadShutdown()
 {
   MOZ_ASSERT(OnThread());
 
-  if (mFinished) {
-    // Nothing more to do.
-    return;
-  }
+  MOZ_ASSERT(!mFinished, "We've already shutdown");
+
   mFinished = true;
 
   if (mDecoder) {
     RefPtr<Benchmark> ref(mMainThreadState);
     mDecoder->Flush()->Then(
       Thread(), __func__,
       [ref, this]() {
         mDecoder->Shutdown()->Then(
@@ -301,27 +299,28 @@ BenchmarkPlayback::MainThreadShutdown()
     FinalizeShutdown();
   }
 }
 
 void
 BenchmarkPlayback::Output(const MediaDataDecoder::DecodedData& aResults)
 {
   MOZ_ASSERT(OnThread());
+  MOZ_ASSERT(!mFinished);
+
   RefPtr<Benchmark> ref(mMainThreadState);
   mFrameCount += aResults.Length();
   if (!mDecodeStartTime && mFrameCount >= ref->mParameters.mStartupFrame) {
     mDecodeStartTime = Some(TimeStamp::Now());
   }
   TimeStamp now = TimeStamp::Now();
   int32_t frames = mFrameCount - ref->mParameters.mStartupFrame;
   TimeDuration elapsedTime = now - mDecodeStartTime.refOr(now);
-  if (!mFinished &&
-      (((frames == ref->mParameters.mFramesToMeasure) && frames > 0) ||
-       elapsedTime >= ref->mParameters.mTimeout || mDrained)) {
+  if (((frames == ref->mParameters.mFramesToMeasure) && frames > 0) ||
+      elapsedTime >= ref->mParameters.mTimeout || mDrained) {
     uint32_t decodeFps = frames / elapsedTime.ToSeconds();
     MainThreadShutdown();
     ref->Dispatch(
       NS_NewRunnableFunction("BenchmarkPlayback::Output", [ref, decodeFps]() {
         ref->ReturnResult(decodeFps);
       }));
   }
 }
@@ -337,36 +336,54 @@ BenchmarkPlayback::Error(const MediaResu
     "BenchmarkPlayback::Error",
     [ref, aError]() { ref->ReturnError(aError); }));
 }
 
 void
 BenchmarkPlayback::InputExhausted()
 {
   MOZ_ASSERT(OnThread());
-  if (mFinished || mSampleIndex >= mSamples.Length()) {
+  MOZ_ASSERT(!mFinished);
+
+  if (mSampleIndex >= mSamples.Length()) {
+    Error(MediaResult(NS_ERROR_FAILURE, "Nothing left to decode"));
     return;
   }
+
+  RefPtr<MediaRawData> sample = mSamples[mSampleIndex];
   RefPtr<Benchmark> ref(mMainThreadState);
-  mDecoder->Decode(mSamples[mSampleIndex])
-    ->Then(Thread(), __func__,
-           [ref, this](const MediaDataDecoder::DecodedData& aResults) {
-             Output(aResults);
-             InputExhausted();
-           },
-           [ref, this](const MediaResult& aError) { Error(aError); });
+  RefPtr<MediaDataDecoder::DecodePromise> p = mDecoder->Decode(sample);
+
   mSampleIndex++;
-  if (mSampleIndex == mSamples.Length()) {
-    if (ref->mParameters.mStopAtFrame) {
+  if (mSampleIndex == mSamples.Length() && !ref->mParameters.mStopAtFrame) {
+    // Complete current frame decode then drain if still necessary.
+    p->Then(Thread(), __func__,
+            [ref, this](const MediaDataDecoder::DecodedData& aResults) {
+              Output(aResults);
+              if (!mFinished) {
+                mDecoder->Drain()->Then(
+                  Thread(), __func__,
+                  [ref, this](const MediaDataDecoder::DecodedData& aResults) {
+                    mDrained = true;
+                    Output(aResults);
+                    MOZ_ASSERT(mFinished, "We must be done now");
+                  },
+                  [ref, this](const MediaResult& aError) { Error(aError); });
+              }
+            },
+            [ref, this](const MediaResult& aError) { Error(aError); });
+  } else {
+    if (mSampleIndex == mSamples.Length() && ref->mParameters.mStopAtFrame) {
       mSampleIndex = 0;
-    } else {
-      mDecoder->Drain()->Then(
-        Thread(), __func__,
-        [ref, this](const MediaDataDecoder::DecodedData& aResults) {
-          mDrained = true;
-          Output(aResults);
-        },
-        [ref, this](const MediaResult& aError) { Error(aError); });
     }
+    // Continue decoding
+    p->Then(Thread(), __func__,
+            [ref, this](const MediaDataDecoder::DecodedData& aResults) {
+              Output(aResults);
+              if (!mFinished) {
+                InputExhausted();
+              }
+            },
+            [ref, this](const MediaResult& aError) { Error(aError); });
   }
 }
 
 } // namespace mozilla