Bug 1167356 - Handle return value of DataSourceSurface::Map wherever possible. r=Bas, a=lizzard
authorAndrew Comminos <acomminos@mozilla.com>
Thu, 11 Jun 2015 13:06:23 -0400
changeset 273531 b43df791dbaf4f3d8e87cccf91159342b85caf1b
parent 273530 1939e93908b290532cc132959f5efa8b53afd113
child 273532 c58332063a0a28ae9eec3d1760cf3510a9100f1a
push id4830
push userjlund@mozilla.com
push dateMon, 29 Jun 2015 20:18:48 +0000
treeherdermozilla-beta@4c2175bb0420 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersBas, lizzard
bugs1167356
milestone40.0a2
Bug 1167356 - Handle return value of DataSourceSurface::Map wherever possible. r=Bas, a=lizzard
dom/base/nsContentUtils.cpp
gfx/2d/DrawTargetTiled.h
gfx/2d/FilterNodeSoftware.cpp
gfx/2d/SourceSurfaceD2D1.cpp
gfx/gl/GLReadTexImageHelper.cpp
gfx/layers/YCbCrImageDataSerializer.cpp
gfx/layers/d3d11/TextureD3D11.cpp
image/src/imgFrame.cpp
widget/gtk/nsImageToPixbuf.cpp
--- a/dom/base/nsContentUtils.cpp
+++ b/dom/base/nsContentUtils.cpp
@@ -7386,17 +7386,19 @@ nsContentUtils::TransferableToIPCTransfe
   }
 }
 
 mozilla::UniquePtr<char[]>
 nsContentUtils::GetSurfaceData(mozilla::gfx::DataSourceSurface* aSurface,
                                size_t* aLength, int32_t* aStride)
 {
   mozilla::gfx::DataSourceSurface::MappedSurface map;
-  aSurface->Map(mozilla::gfx::DataSourceSurface::MapType::READ, &map);
+  if (NS_WARN_IF(!aSurface->Map(mozilla::gfx::DataSourceSurface::MapType::READ, &map))) {
+    return nullptr;
+  }
   mozilla::gfx::IntSize size = aSurface->GetSize();
   mozilla::CheckedInt32 requiredBytes =
     mozilla::CheckedInt32(map.mStride) * mozilla::CheckedInt32(size.height);
   size_t maxBufLen = requiredBytes.isValid() ? requiredBytes.value() : 0;
   mozilla::gfx::SurfaceFormat format = aSurface->GetFormat();
 
   // Surface data handling is totally nuts. This is the magic one needs to
   // know to access the data.
--- a/gfx/2d/DrawTargetTiled.h
+++ b/gfx/2d/DrawTargetTiled.h
@@ -172,17 +172,20 @@ public:
   }
   virtual SurfaceFormat GetFormat() const { return mSnapshots[0]->GetFormat(); }
 
   virtual TemporaryRef<DataSourceSurface> GetDataSurface()
   {
     RefPtr<DataSourceSurface> surf = Factory::CreateDataSourceSurface(GetSize(), GetFormat());
 
     DataSourceSurface::MappedSurface mappedSurf;
-    surf->Map(DataSourceSurface::MapType::WRITE, &mappedSurf);
+    if (!surf->Map(DataSourceSurface::MapType::WRITE, &mappedSurf)) {
+      gfxCriticalError() << "DrawTargetTiled::GetDataSurface failed to map surface";
+      return nullptr;
+    }
 
     {
       RefPtr<DrawTarget> dt =
         Factory::CreateDrawTargetForData(BackendType::CAIRO, mappedSurf.mData,
         GetSize(), mappedSurf.mStride, GetFormat());
 
       if (!dt) {
         gfxWarning() << "DrawTargetTiled::GetDataSurface failed in CreateDrawTargetForData";
--- a/gfx/2d/FilterNodeSoftware.cpp
+++ b/gfx/2d/FilterNodeSoftware.cpp
@@ -1090,17 +1090,20 @@ FilterNodeTransformSoftware::Render(cons
   RefPtr<DataSourceSurface> surf =
     Factory::CreateDataSourceSurface(aRect.Size(), input->GetFormat(), true);
 
   if (!surf) {
     return nullptr;
   }
 
   DataSourceSurface::MappedSurface mapping;
-  surf->Map(DataSourceSurface::MapType::WRITE, &mapping);
+  if (!surf->Map(DataSourceSurface::MapType::WRITE, &mapping)) {
+    gfxCriticalError() << "FilterNodeTransformSoftware::Render failed to map surface";
+    return nullptr;
+  }
 
   RefPtr<DrawTarget> dt =
     Factory::CreateDrawTargetForData(BackendType::CAIRO,
                                      mapping.mData,
                                      surf->GetSize(),
                                      mapping.mStride,
                                      surf->GetFormat());
   if (!dt) {
--- a/gfx/2d/SourceSurfaceD2D1.cpp
+++ b/gfx/2d/SourceSurfaceD2D1.cpp
@@ -177,17 +177,20 @@ DataSourceSurfaceD2D1::Map(MapType aMapT
   D2D1_MAP_OPTIONS options;
   if (aMapType == MapType::READ) {
     options = D2D1_MAP_OPTIONS_READ;
   } else {
     MOZ_CRASH("No support for Write maps on D2D1 DataSourceSurfaces yet!");
   }
 
   D2D1_MAPPED_RECT map;
-  mBitmap->Map(D2D1_MAP_OPTIONS_READ, &map);
+  if (FAILED(mBitmap->Map(D2D1_MAP_OPTIONS_READ, &map))) {
+    gfxCriticalError() << "Failed to map bitmap.";
+    return false;
+  }
   aMappedSurface->mData = map.bits;
   aMappedSurface->mStride = map.pitch;
 
   mIsMapped = !!aMappedSurface->mData;
   return mIsMapped;
 }
 
 void
@@ -210,14 +213,17 @@ DataSourceSurfaceD2D1::Stride()
 void
 DataSourceSurfaceD2D1::EnsureMapped()
 {
   // Do not use GetData() after having used Map!
   MOZ_ASSERT(!mIsMapped);
   if (mMapped) {
     return;
   }
-  mBitmap->Map(D2D1_MAP_OPTIONS_READ, &mMap);
+  if (FAILED(mBitmap->Map(D2D1_MAP_OPTIONS_READ, &mMap))) {
+    gfxCriticalError() << "Failed to map bitmap.";
+    return;
+  }
   mMapped = true;
 }
 
 }
 }
--- a/gfx/gl/GLReadTexImageHelper.cpp
+++ b/gfx/gl/GLReadTexImageHelper.cpp
@@ -212,17 +212,20 @@ GetActualReadFormats(GLContext* gl,
         return true;
     }
 }
 
 static void
 SwapRAndBComponents(DataSourceSurface* surf)
 {
     DataSourceSurface::MappedSurface map;
-    MOZ_ALWAYS_TRUE( surf->Map(DataSourceSurface::MapType::READ_WRITE, &map) );
+    if (!surf->Map(DataSourceSurface::MapType::READ_WRITE, &map)) {
+        MOZ_ASSERT(false, "SwapRAndBComponents: Failed to map surface.");
+        return;
+    }
     MOZ_ASSERT(map.mStride >= 0);
 
     const size_t rowBytes = surf->GetSize().width*4;
     const size_t rowHole = map.mStride - rowBytes;
 
     uint8_t* row = map.mData;
     if (!row) {
         MOZ_ASSERT(false, "SwapRAndBComponents: Failed to get data from"
@@ -283,18 +286,21 @@ CopyDataSourceSurface(DataSourceSurface*
     const bool destHasAlpha = aDest->GetFormat() == SurfaceFormat::R8G8B8A8 ||
                               aDest->GetFormat() == SurfaceFormat::B8G8R8A8;
     const bool needsAlphaMask = !srcHasAlpha && destHasAlpha;
 
     const bool needsConvertTo16Bits = aDest->GetFormat() == SurfaceFormat::R5G6B5;
 
     DataSourceSurface::MappedSurface srcMap;
     DataSourceSurface::MappedSurface destMap;
-    MOZ_ALWAYS_TRUE( aSource->Map(DataSourceSurface::MapType::READ, &srcMap) );
-    MOZ_ALWAYS_TRUE( aDest->Map(DataSourceSurface::MapType::WRITE, &destMap) );
+    if (!aSource->Map(DataSourceSurface::MapType::READ, &srcMap) ||
+        !aDest->Map(DataSourceSurface::MapType::WRITE, &destMap)) {
+        MOZ_ASSERT(false, "CopyDataSourceSurface: Failed to map surface.");
+        return;
+    }
     MOZ_ASSERT(srcMap.mStride >= 0);
     MOZ_ASSERT(destMap.mStride >= 0);
 
     const size_t srcBPP = BytesPerPixel(aSource->GetFormat());
     const size_t srcRowBytes = aSource->GetSize().width * srcBPP;
     const size_t srcRowHole = srcMap.mStride - srcRowBytes;
 
     const size_t destBPP = BytesPerPixel(aDest->GetFormat());
--- a/gfx/layers/YCbCrImageDataSerializer.cpp
+++ b/gfx/layers/YCbCrImageDataSerializer.cpp
@@ -281,17 +281,19 @@ YCbCrImageDataDeserializer::ToDataSource
 {
   RefPtr<DataSourceSurface> result =
     Factory::CreateDataSourceSurface(GetYSize(), gfx::SurfaceFormat::B8G8R8X8);
   if (NS_WARN_IF(!result)) {
     return nullptr;
   }
 
   DataSourceSurface::MappedSurface map;
-  result->Map(DataSourceSurface::MapType::WRITE, &map);
+  if (NS_WARN_IF(!result->Map(DataSourceSurface::MapType::WRITE, &map))) {
+    return nullptr;
+  }
 
   gfx::ConvertYCbCrToRGB32(GetYData(), GetCbData(), GetCrData(),
                            map.mData,
                            0, 0, //pic x and y
                            GetYSize().width, GetYSize().height,
                            GetYStride(), GetCbCrStride(),
                            map.mStride,
                            gfx::YV12);
--- a/gfx/layers/d3d11/TextureD3D11.cpp
+++ b/gfx/layers/d3d11/TextureD3D11.cpp
@@ -841,17 +841,21 @@ DataTextureSourceD3D11::Update(DataSourc
       mIsTiled = false;
       if (FAILED(hr) || !mTexture) {
         Reset();
         return false;
       }
     }
 
     DataSourceSurface::MappedSurface map;
-    aSurface->Map(DataSourceSurface::MapType::READ, &map);
+    if (!aSurface->Map(DataSourceSurface::MapType::READ, &map)) {
+      gfxCriticalError() << "Failed to map surface.";
+      Reset();
+      return false;
+    }
 
     if (aDestRegion) {
       nsIntRegionRectIterator iter(*aDestRegion);
       const IntRect *iterRect;
       while ((iterRect = iter.Next())) {
         D3D11_BOX box;
         box.front = 0;
         box.back = 1;
--- a/image/src/imgFrame.cpp
+++ b/image/src/imgFrame.cpp
@@ -479,19 +479,21 @@ imgFrame::Optimize()
 
     RefPtr<DataSourceSurface> surf =
       CreateLockedSurface(buf, mSize, optFormat);
     if (!surf) {
       return NS_ERROR_OUT_OF_MEMORY;
     }
 
     DataSourceSurface::MappedSurface mapping;
-    DebugOnly<bool> success =
-      surf->Map(DataSourceSurface::MapType::WRITE, &mapping);
-    NS_ASSERTION(success, "Failed to map surface");
+    if (!surf->Map(DataSourceSurface::MapType::WRITE, &mapping)) {
+      gfxCriticalError() << "imgFrame::Optimize failed to map surface";
+      return NS_ERROR_FAILURE;
+    }
+
     RefPtr<DrawTarget> target =
       Factory::CreateDrawTargetForData(BackendType::CAIRO,
                                        mapping.mData,
                                        mSize,
                                        mapping.mStride,
                                        optFormat);
 
     if (!target) {
--- a/widget/gtk/nsImageToPixbuf.cpp
+++ b/widget/gtk/nsImageToPixbuf.cpp
@@ -70,17 +70,19 @@ nsImageToPixbuf::SourceSurfaceToPixbuf(S
     if (!pixbuf)
         return nullptr;
 
     uint32_t destStride = gdk_pixbuf_get_rowstride (pixbuf);
     guchar* destPixels = gdk_pixbuf_get_pixels (pixbuf);
 
     RefPtr<DataSourceSurface> dataSurface = aSurface->GetDataSurface();
     DataSourceSurface::MappedSurface map;
-    dataSurface->Map(DataSourceSurface::MapType::READ, &map);
+    if (!dataSurface->Map(DataSourceSurface::MapType::READ, &map))
+        return nullptr;
+
     uint8_t* srcData = map.mData;
     int32_t srcStride = map.mStride;
 
     SurfaceFormat format = dataSurface->GetFormat();
 
     for (int32_t row = 0; row < aHeight; ++row) {
         for (int32_t col = 0; col < aWidth; ++col) {
             guchar* destPixel = destPixels + row * destStride + 4 * col;