Bug 1303094 - make SourceSurfaceSkia allocation fallible by using sk_malloc. r=mchang a=jcristau
authorLee Salzman <lsalzman@mozilla.com>
Fri, 17 Feb 2017 16:41:58 -0500
changeset 376346 4010c154f3c43b1a517ca60bf50c8e99f4064d57
parent 376345 e87493254cbe0c4e91e84ecb57f56d162c187c8c
child 376347 8adddacf61038c048989e2a80fa34629b5d7371b
push id6996
push userjlorenzo@mozilla.com
push dateMon, 06 Mar 2017 20:48:21 +0000
treeherdermozilla-beta@d89512dab048 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmchang, jcristau
bugs1303094
milestone53.0a2
Bug 1303094 - make SourceSurfaceSkia allocation fallible by using sk_malloc. r=mchang a=jcristau MozReview-Commit-ID: 7l3fOG0kkq0
gfx/2d/SourceSurfaceSkia.cpp
--- a/gfx/2d/SourceSurfaceSkia.cpp
+++ b/gfx/2d/SourceSurfaceSkia.cpp
@@ -35,24 +35,46 @@ SourceSurfaceSkia::GetSize() const
 }
 
 SurfaceFormat
 SourceSurfaceSkia::GetFormat() const
 {
   return mFormat;
 }
 
+static sk_sp<SkData>
+MakeSkData(unsigned char* aData, const IntSize& aSize, int32_t aStride)
+{
+  CheckedInt<size_t> size = aStride;
+  size *= aSize.height;
+  if (size.isValid()) {
+    void* mem = sk_malloc_flags(size.value(), 0);
+    if (mem) {
+      if (aData) {
+        memcpy(mem, aData, size.value());
+      }
+      return SkData::MakeFromMalloc(mem, size.value());
+    }
+  }
+  return nullptr;
+}
+
 bool
 SourceSurfaceSkia::InitFromData(unsigned char* aData,
                                 const IntSize &aSize,
                                 int32_t aStride,
                                 SurfaceFormat aFormat)
 {
-  SkPixmap pixmap(MakeSkiaImageInfo(aSize, aFormat), aData, aStride);
-  mImage = SkImage::MakeRasterCopy(pixmap);
+  sk_sp<SkData> data = MakeSkData(aData, aSize, aStride);
+  if (!data) {
+    return false;
+  }
+
+  SkImageInfo info = MakeSkiaImageInfo(aSize, aFormat);
+  mImage = SkImage::MakeRasterData(info, data, aStride);
   if (!mImage) {
     return false;
   }
 
   mSize = aSize;
   mFormat = aFormat;
   mStride = aStride;
   return true;
@@ -101,24 +123,20 @@ SourceSurfaceSkia::InitFromImage(const s
 }
 
 uint8_t*
 SourceSurfaceSkia::GetData()
 {
 #ifdef USE_SKIA_GPU
   if (mImage->isTextureBacked()) {
     sk_sp<SkImage> raster;
-    CheckedInt<size_t> size = mStride;
-    size *= mSize.height;
-    if (size.isValid()) {
-      if (sk_sp<SkData> data = SkData::MakeUninitialized(size.value())) {
-        SkImageInfo info = MakeSkiaImageInfo(mSize, mFormat);
-        if (mImage->readPixels(info, data->writable_data(), mStride, 0, 0, SkImage::kDisallow_CachingHint)) {
-          raster = SkImage::MakeRasterData(info, data, mStride);
-        }
+    if (sk_sp<SkData> data = MakeSkData(nullptr, mSize, mStride)) {
+      SkImageInfo info = MakeSkiaImageInfo(mSize, mFormat);
+      if (mImage->readPixels(info, data->writable_data(), mStride, 0, 0, SkImage::kDisallow_CachingHint)) {
+        raster = SkImage::MakeRasterData(info, data, mStride);
       }
     }
     if (raster) {
       mImage = raster;
     } else {
       gfxCriticalError() << "Failed making Skia raster image for GPU surface";
     }
   }