Bug 1423281 - Store the userdata for freeing our memory on the longer living snapshot. r=dvander, a=jcristau
authorBas Schouten <bschouten@mozilla.com>
Wed, 06 Dec 2017 04:59:18 +0100
changeset 445299 06039747cf8690dfb42b90574c3a8743efda6a3e
parent 445298 7e553ba7c247bdc9994a924e31f3d0f9b44b7c22
child 445300 4186bb70043e1c56d2211d83273d47d631963891
push id1618
push userCallek@gmail.com
push dateThu, 11 Jan 2018 17:45:48 +0000
treeherdermozilla-release@882ca853e05a [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersdvander, jcristau
bugs1423281
milestone58.0
Bug 1423281 - Store the userdata for freeing our memory on the longer living snapshot. r=dvander, a=jcristau MozReview-Commit-ID: 91tVpJC7gAe
gfx/2d/SourceSurfaceCapture.cpp
--- a/gfx/2d/SourceSurfaceCapture.cpp
+++ b/gfx/2d/SourceSurfaceCapture.cpp
@@ -66,51 +66,63 @@ SourceSurfaceCapture::Resolve(BackendTyp
   }
   return mResolved;
 }
 
 RefPtr<SourceSurface>
 SourceSurfaceCapture::ResolveImpl(BackendType aBackendType)
 {
   RefPtr<DrawTarget> dt;
+  uint8_t* data = nullptr;
   if (!mSurfaceAllocationSize) {
     if (aBackendType == mRefDT->GetBackendType()) {
       dt = mRefDT->CreateSimilarDrawTarget(mSize, mFormat);
     } else {
       dt = Factory::CreateDrawTarget(aBackendType, mSize, mFormat);
     }
   } else {
-    uint8_t* data = static_cast<uint8_t*>(calloc(1, mSurfaceAllocationSize));
+    data = static_cast<uint8_t*>(calloc(1, mSurfaceAllocationSize));
     if (!data) {
       return nullptr;
     }
     BackendType type = Factory::DoesBackendSupportDataDrawtarget(aBackendType)
                        ? aBackendType
                        : BackendType::SKIA;
     dt = Factory::CreateDrawTargetForData(type, data, mSize, mStride, mFormat);
     if (!dt) {
       free(data);
       return nullptr;
     }
-    dt->AddUserData(reinterpret_cast<UserDataKey*>(dt.get()), data, free);
   }
+
   if (!dt) {
+    // Make sure we haven't allocated and aren't leaking something, the code right
+    // anove here should have guaranteed that.
+    MOZ_ASSERT(!data);
     return nullptr;
   }
 
   // If we're still attached to a DrawTarget, use its command list rather than
   // our own (which will be empty).
   CaptureCommandList& commands = mHasCommandList
                                  ? mCommands
                                  : mOwner->mCommands;
   for (CaptureCommandList::iterator iter(commands); !iter.Done(); iter.Next()) {
     DrawingCommand* cmd = iter.Get();
     cmd->ExecuteOnDT(dt, nullptr);
   }
-  return dt->Snapshot();
+
+  RefPtr<SourceSurface> surf;
+  surf = dt->Snapshot();
+
+  if (data) {
+    surf->AddUserData(reinterpret_cast<UserDataKey*>(dt.get()), data, free);
+  }
+
+  return surf.forget();
 }
 
 already_AddRefed<DataSourceSurface>
 SourceSurfaceCapture::GetDataSurface()
 {
   RefPtr<SourceSurface> surface = Resolve();
   if (!surface) {
     return nullptr;