Bug 1313381 - Add assertions to nsDeviceContext to assert that it is only initialized once. r=mstange
authorJonathan Watt <jwatt@jwatt.org>
Fri, 21 Oct 2016 20:39:11 +0100
changeset 319879 04ee6f23fb6f13061acad21a0e57b1a0f7664615
parent 319878 da1c0184e9f51860b8ac0b75c78bb30ddeed473c
child 319880 e84c02971f372880904cdd7456bb895f00604ecd
push id20749
push userryanvm@gmail.com
push dateSat, 29 Oct 2016 13:21:21 +0000
treeherderfx-team@1b170b39ed6b [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmstange
bugs1313381
milestone52.0a1
Bug 1313381 - Add assertions to nsDeviceContext to assert that it is only initialized once. r=mstange
gfx/src/nsDeviceContext.cpp
gfx/src/nsDeviceContext.h
--- a/gfx/src/nsDeviceContext.cpp
+++ b/gfx/src/nsDeviceContext.cpp
@@ -189,16 +189,19 @@ nsFontCache::Flush()
     mFontMetrics.Clear();
 }
 
 nsDeviceContext::nsDeviceContext()
     : mWidth(0), mHeight(0), mDepth(0),
       mAppUnitsPerDevPixel(-1), mAppUnitsPerDevPixelAtUnitFullZoom(-1),
       mAppUnitsPerPhysicalInch(-1),
       mFullZoom(1.0f), mPrintingScale(1.0f)
+#ifdef DEBUG
+    , mIsInitialized(false)
+#endif
 {
     MOZ_ASSERT(NS_IsMainThread(), "nsDeviceContext created off main thread");
 }
 
 nsDeviceContext::~nsDeviceContext()
 {
     if (mFontCache) {
         mFontCache->Destroy();
@@ -294,16 +297,23 @@ nsDeviceContext::SetDPI(double* aScale)
 
     mAppUnitsPerPhysicalInch = NS_lround(dpi * mAppUnitsPerDevPixelAtUnitFullZoom);
     UpdateAppUnitsForFullZoom();
 }
 
 nsresult
 nsDeviceContext::Init(nsIWidget *aWidget)
 {
+#ifdef DEBUG
+    // We can't assert |!mIsInitialized| here since EndSwapDocShellsForDocument
+    // re-initializes nsDeviceContext objects.  We can only assert in
+    // InitForPrinting (below).
+    mIsInitialized = true;
+#endif
+
     nsresult rv = NS_OK;
     if (mScreenManager && mWidget == aWidget)
         return rv;
 
     mWidget = aWidget;
     SetDPI();
 
     if (mScreenManager)
@@ -433,23 +443,28 @@ nsDeviceContext::GetClientRect(nsRect &a
     return NS_OK;
 }
 
 nsresult
 nsDeviceContext::InitForPrinting(nsIDeviceContextSpec *aDevice)
 {
     NS_ENSURE_ARG_POINTER(aDevice);
 
-    mDeviceContextSpec = aDevice;
+    MOZ_ASSERT(!mIsInitialized,
+               "Only initialize once, immediately after construction");
+
+    // We don't set mIsInitialized here. The Init() call below does that.
 
     mPrintTarget = aDevice->MakePrintTarget();
     if (!mPrintTarget) {
         return NS_ERROR_FAILURE;
     }
 
+    mDeviceContextSpec = aDevice;
+
     Init(nullptr);
 
     if (!CalcPrintingSize()) {
         return NS_ERROR_FAILURE;
     }
 
     return NS_OK;
 }
--- a/gfx/src/nsDeviceContext.h
+++ b/gfx/src/nsDeviceContext.h
@@ -283,11 +283,14 @@ private:
     RefPtr<nsFontCache>            mFontCache;
     nsCOMPtr<nsIWidget>            mWidget;
     nsCOMPtr<nsIScreenManager>     mScreenManager;
     nsCOMPtr<nsIDeviceContextSpec> mDeviceContextSpec;
     RefPtr<PrintTarget>            mPrintTarget;
 #ifdef XP_MACOSX
     RefPtr<PrintTarget>            mCachedPrintTarget;
 #endif
+#ifdef DEBUG
+    bool mIsInitialized;
+#endif
 };
 
 #endif /* _NS_DEVICECONTEXT_H_ */