Bug 1521774: Ensure a DC is available when using the generic dc on the main thread. r=rhunt a=lizzard
authorBas Schouten <bschouten@mozilla.com>
Fri, 08 Feb 2019 14:52:05 +0100
changeset 515953 1bbd5e11e62bfd7489fc929ab46cde79bbadab3e
parent 515952 e320050c6f958301665225d0e63d09e6df73eb91
child 515954 6a3f0a95ae10e277860a294f560f3a66f04452dc
push id1953
push userffxbld-merge
push dateMon, 11 Mar 2019 12:10:20 +0000
treeherdermozilla-release@9c35dcbaa899 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersrhunt, lizzard
bugs1521774
milestone66.0
Bug 1521774: Ensure a DC is available when using the generic dc on the main thread. r=rhunt a=lizzard Differential Revision: https://phabricator.services.mozilla.com/D19146
gfx/2d/DrawTargetD2D1.cpp
--- a/gfx/2d/DrawTargetD2D1.cpp
+++ b/gfx/2d/DrawTargetD2D1.cpp
@@ -1043,48 +1043,56 @@ void DrawTargetD2D1::PopLayer() {
   mDC->PopLayer();
 }
 
 already_AddRefed<SourceSurface> DrawTargetD2D1::CreateSourceSurfaceFromData(
     unsigned char *aData, const IntSize &aSize, int32_t aStride,
     SurfaceFormat aFormat) const {
   RefPtr<ID2D1Bitmap1> bitmap;
 
-  HRESULT hr = Factory::GetD2DDeviceContext()->CreateBitmap(
-      D2DIntSize(aSize), aData, aStride,
-      D2D1::BitmapProperties1(D2D1_BITMAP_OPTIONS_NONE,
-                              D2DPixelFormat(aFormat)),
-      getter_AddRefs(bitmap));
+  RefPtr<ID2D1DeviceContext> dc = Factory::GetD2DDeviceContext();
+  if (!dc) {
+    return nullptr;
+  }
+
+  HRESULT hr =
+      dc->CreateBitmap(D2DIntSize(aSize), aData, aStride,
+                       D2D1::BitmapProperties1(D2D1_BITMAP_OPTIONS_NONE,
+                                               D2DPixelFormat(aFormat)),
+                       getter_AddRefs(bitmap));
 
   if (FAILED(hr) || !bitmap) {
     gfxCriticalError(
         CriticalLog::DefaultOptions(Factory::ReasonableSurfaceSize(aSize)))
         << "[D2D1.1] 1CreateBitmap failure " << aSize << " Code: " << hexa(hr)
         << " format " << (int)aFormat;
     return nullptr;
   }
 
-  return MakeAndAddRef<SourceSurfaceD2D1>(
-      bitmap.get(), Factory::GetD2DDeviceContext().get(), aFormat, aSize);
+  return MakeAndAddRef<SourceSurfaceD2D1>(bitmap.get(), dc.get(), aFormat,
+                                          aSize);
 }
 
 already_AddRefed<DrawTarget> DrawTargetD2D1::CreateSimilarDrawTarget(
     const IntSize &aSize, SurfaceFormat aFormat) const {
   RefPtr<DrawTargetD2D1> dt = new DrawTargetD2D1();
 
   if (!dt->Init(aSize, aFormat)) {
     return nullptr;
   }
 
   return dt.forget();
 }
 
 bool DrawTargetD2D1::CanCreateSimilarDrawTarget(const IntSize &aSize,
                                                 SurfaceFormat aFormat) const {
   RefPtr<ID2D1DeviceContext> dc = Factory::GetD2DDeviceContext();
+  if (!dc) {
+    return false;
+  }
   return (dc->GetMaximumBitmapSize() >= UINT32(aSize.width) &&
           dc->GetMaximumBitmapSize() >= UINT32(aSize.height));
 }
 
 already_AddRefed<PathBuilder> DrawTargetD2D1::CreatePathBuilder(
     FillRule aFillRule) const {
   RefPtr<ID2D1PathGeometry> path;
   HRESULT hr = factory()->CreatePathGeometry(getter_AddRefs(path));
@@ -2157,16 +2165,21 @@ already_AddRefed<ID2D1Image> DrawTargetD
 
 already_AddRefed<SourceSurface> DrawTargetD2D1::OptimizeSourceSurface(
     SourceSurface *aSurface) const {
   if (aSurface->GetType() == SurfaceType::D2D1_1_IMAGE) {
     RefPtr<SourceSurface> surface(aSurface);
     return surface.forget();
   }
 
+  RefPtr<ID2D1DeviceContext> dc = Factory::GetD2DDeviceContext();
+  if (!dc) {
+    return nullptr;
+  }
+
   // Special case captures so we don't resolve them to a data surface.
   if (aSurface->GetType() == SurfaceType::CAPTURE) {
     SourceSurfaceCapture *capture =
         static_cast<SourceSurfaceCapture *>(aSurface);
     RefPtr<SourceSurface> resolved = capture->Resolve(GetBackendType());
     if (!resolved) {
       return nullptr;
     }
@@ -2178,17 +2191,17 @@ already_AddRefed<SourceSurface> DrawTarg
 
   RefPtr<ID2D1Bitmap1> bitmap;
   {
     DataSourceSurface::ScopedMap map(data, DataSourceSurface::READ);
     if (MOZ2D_WARN_IF(!map.IsMapped())) {
       return nullptr;
     }
 
-    HRESULT hr = Factory::GetD2DDeviceContext()->CreateBitmap(
+    HRESULT hr = dc->CreateBitmap(
         D2DIntSize(data->GetSize()), map.GetData(), map.GetStride(),
         D2D1::BitmapProperties1(D2D1_BITMAP_OPTIONS_NONE,
                                 D2DPixelFormat(data->GetFormat())),
         getter_AddRefs(bitmap));
 
     if (FAILED(hr)) {
       gfxCriticalError(CriticalLog::DefaultOptions(
           Factory::ReasonableSurfaceSize(data->GetSize())))
@@ -2196,18 +2209,17 @@ already_AddRefed<SourceSurface> DrawTarg
           << " Code: " << hexa(hr) << " format " << (int)data->GetFormat();
     }
   }
 
   if (!bitmap) {
     return data.forget();
   }
 
-  return MakeAndAddRef<SourceSurfaceD2D1>(bitmap.get(),
-                                          Factory::GetD2DDeviceContext().get(),
+  return MakeAndAddRef<SourceSurfaceD2D1>(bitmap.get(), dc.get(),
                                           data->GetFormat(), data->GetSize());
 }
 
 void DrawTargetD2D1::PushD2DLayer(ID2D1DeviceContext *aDC,
                                   ID2D1Geometry *aGeometry,
                                   const D2D1_MATRIX_3X2_F &aTransform,
                                   bool aPixelAligned, bool aForceIgnoreAlpha,
                                   const D2D1_RECT_F &aMaxRect) {