Bug 839383 - part 2 - factor out CGImageRef creation code in SourceSurfaceCG.cpp; r=jrmuizel
authorNathan Froyd <froydnj@mozilla.com>
Tue, 12 Mar 2013 16:59:26 -0400
changeset 127121 c2715e049b3c
parent 127120 3b354780c5ac
child 127122 b06fa2317215
push id117
push usertomi.aarnio@nokia.com
push dateWed, 03 Apr 2013 12:07:07 +0000
reviewersjrmuizel
bugs839383
milestone23.0a1
Bug 839383 - part 2 - factor out CGImageRef creation code in SourceSurfaceCG.cpp; r=jrmuizel
gfx/2d/SourceSurfaceCG.cpp
--- a/gfx/2d/SourceSurfaceCG.cpp
+++ b/gfx/2d/SourceSurfaceCG.cpp
@@ -74,69 +74,79 @@ AssignSurfaceParametersFromFormat(Surfac
       aBitsPerPixel = 8;
       break;
 
     default:
       MOZ_CRASH();
   }
 }
 
+static CGImageRef
+CreateCGImage(void *aInfo,
+              const void *aData,
+              const IntSize &aSize,
+              int32_t aStride,
+              SurfaceFormat aFormat)
+{
+  //XXX: we should avoid creating this colorspace everytime
+  CGColorSpaceRef colorSpace = nullptr;
+  CGBitmapInfo bitinfo = 0;
+  int bitsPerComponent = 0;
+  int bitsPerPixel = 0;
+
+  AssignSurfaceParametersFromFormat(aFormat, colorSpace, bitinfo,
+                                    bitsPerComponent, bitsPerPixel);
+
+  CGDataProviderRef dataProvider = CGDataProviderCreateWithData(aInfo,
+                                                                aData,
+                                                                aSize.height * aStride,
+                                                                releaseCallback);
+
+  CGImageRef image;
+  if (aFormat == FORMAT_A8) {
+    CGFloat decode[] = {1.0, 0.0};
+    image = CGImageMaskCreate (aSize.width, aSize.height,
+                               bitsPerComponent,
+                               bitsPerPixel,
+                               aStride,
+                               dataProvider,
+                               decode,
+                               true);
+  } else {
+    image = CGImageCreate (aSize.width, aSize.height,
+                           bitsPerComponent,
+                           bitsPerPixel,
+                           aStride,
+                           colorSpace,
+                           bitinfo,
+                           dataProvider,
+                           nullptr,
+                           true,
+                           kCGRenderingIntentDefault);
+  }
+
+  CGDataProviderRelease(dataProvider);
+  CGColorSpaceRelease(colorSpace);
+
+  return image;
+}
+
 bool
 SourceSurfaceCG::InitFromData(unsigned char *aData,
                                const IntSize &aSize,
                                int32_t aStride,
                                SurfaceFormat aFormat)
 {
-  //XXX: we should avoid creating this colorspace everytime
-  CGColorSpaceRef colorSpace = nullptr;
-  CGBitmapInfo bitinfo = 0;
-  CGDataProviderRef dataProvider = nullptr;
-  int bitsPerComponent = 0;
-  int bitsPerPixel = 0;
-
   assert(aSize.width >= 0 && aSize.height >= 0);
 
-  AssignSurfaceParametersFromFormat(aFormat, colorSpace, bitinfo,
-                                    bitsPerComponent, bitsPerPixel);
-
   void *data = malloc(aStride * aSize.height);
   memcpy(data, aData, aStride * aSize.height);
 
   mFormat = aFormat;
-
-  dataProvider = CGDataProviderCreateWithData (data,
-                                               data,
-					       aSize.height * aStride,
-					       releaseCallback);
-
-  if (aFormat == FORMAT_A8) {
-    CGFloat decode[] = {1.0, 0.0};
-    mImage = CGImageMaskCreate (aSize.width, aSize.height,
-				bitsPerComponent,
-				bitsPerPixel,
-				aStride,
-				dataProvider,
-				decode,
-				true);
-
-  } else {
-    mImage = CGImageCreate (aSize.width, aSize.height,
-			    bitsPerComponent,
-			    bitsPerPixel,
-			    aStride,
-			    colorSpace,
-			    bitinfo,
-			    dataProvider,
-			    nullptr,
-			    true,
-			    kCGRenderingIntentDefault);
-  }
-
-  CGDataProviderRelease(dataProvider);
-  CGColorSpaceRelease (colorSpace);
+  mImage = CreateCGImage(data, data, aSize, aStride, aFormat);
 
   return mImage != nullptr;
 }
 
 DataSourceSurfaceCG::~DataSourceSurfaceCG()
 {
   CGImageRelease(mImage);
   free(CGBitmapContextGetData(mCg));
@@ -153,61 +163,20 @@ DataSourceSurfaceCG::GetSize() const
 }
 
 bool
 DataSourceSurfaceCG::InitFromData(unsigned char *aData,
                                const IntSize &aSize,
                                int32_t aStride,
                                SurfaceFormat aFormat)
 {
-  //XXX: we should avoid creating this colorspace everytime
-  CGColorSpaceRef colorSpace = nullptr;
-  CGBitmapInfo bitinfo = 0;
-  CGDataProviderRef dataProvider = nullptr;
-  int bitsPerComponent = 0;
-  int bitsPerPixel = 0;
-
-  AssignSurfaceParametersFromFormat(aFormat, colorSpace, bitinfo,
-                                    bitsPerComponent, bitsPerPixel);
-
   void *data = malloc(aStride * aSize.height);
   memcpy(data, aData, aStride * aSize.height);
 
-  //mFormat = aFormat;
-
-  dataProvider = CGDataProviderCreateWithData (data,
-                                               data,
-					       aSize.height * aStride,
-					       releaseCallback);
-
-  if (aFormat == FORMAT_A8) {
-    CGFloat decode[] = {1.0, 0.0};
-    mImage = CGImageMaskCreate (aSize.width, aSize.height,
-				bitsPerComponent,
-				bitsPerPixel,
-				aStride,
-				dataProvider,
-				decode,
-				true);
-
-  } else {
-    mImage = CGImageCreate (aSize.width, aSize.height,
-			    bitsPerComponent,
-			    bitsPerPixel,
-			    aStride,
-			    colorSpace,
-			    bitinfo,
-			    dataProvider,
-			    nullptr,
-			    true,
-			    kCGRenderingIntentDefault);
-  }
-
-  CGDataProviderRelease(dataProvider);
-  CGColorSpaceRelease (colorSpace);
+  mImage = CreateCGImage(data, data, aSize, aStride, aFormat);
 
   return mImage;
 }
 
 CGContextRef CreateBitmapContextForImage(CGImageRef image)
 {
   CGColorSpaceRef colorSpace;
 
@@ -300,58 +269,31 @@ void SourceSurfaceCGBitmapContext::Ensur
   // Instead of using CGBitmapContextCreateImage we create
   // a CGImage around the data associated with the CGBitmapContext
   // we do this to avoid the vm_copy that CGBitmapContextCreateImage.
   // vm_copy tends to cause all sorts of unexpected performance problems
   // because of the mm tricks that vm_copy does. Using a regular
   // memcpy when the bitmap context is modified gives us more predictable
   // performance characteristics.
   if (!mImage) {
-      //XXX: we should avoid creating this colorspace everytime
-      CGColorSpaceRef colorSpace = nullptr;
-      CGBitmapInfo bitinfo = 0;
-      CGDataProviderRef dataProvider = nullptr;
-      int bitsPerComponent = 8;
-      int bitsPerPixel = 32;
-
-      colorSpace = CGColorSpaceCreateDeviceRGB();
-      bitinfo = kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Host;
+    void *info;
+    if (mCg) {
+      // if we have an mCg than it owns the data
+      // and we don't want to tranfer ownership
+      // to the CGDataProviderCreateWithData
+      info = nullptr;
+    } else {
+      // otherwise we transfer ownership to
+      // the dataProvider
+      info = mData;
+    }
 
-      void *info;
-      if (mCg) {
-          // if we have an mCg than it owns the data
-          // and we don't want to tranfer ownership
-          // to the CGDataProviderCreateWithData
-          info = nullptr;
-      } else {
-          // otherwise we transfer ownership to
-          // the dataProvider
-          info = mData;
-      }
-
-      if (!mData) abort();
+    if (!mData) abort();
 
-      dataProvider = CGDataProviderCreateWithData (info,
-                                                   mData,
-                                                   mSize.height * mStride,
-                                                   releaseCallback);
-
-      mImage = CGImageCreate (mSize.width, mSize.height,
-                              bitsPerComponent,
-                              bitsPerPixel,
-                              mStride,
-                              colorSpace,
-                              bitinfo,
-                              dataProvider,
-                              nullptr,
-                              true,
-                              kCGRenderingIntentDefault);
-
-      CGDataProviderRelease(dataProvider);
-      CGColorSpaceRelease (colorSpace);
+    mImage = CreateCGImage(info, mData, mSize, mStride, FORMAT_B8G8R8A8);
   }
 }
 
 IntSize
 SourceSurfaceCGBitmapContext::GetSize() const
 {
   return mSize;
 }
@@ -425,46 +367,17 @@ void SourceSurfaceCGIOSurfaceContext::En
   // Instead of using CGBitmapContextCreateImage we create
   // a CGImage around the data associated with the CGBitmapContext
   // we do this to avoid the vm_copy that CGBitmapContextCreateImage.
   // vm_copy tends to cause all sorts of unexpected performance problems
   // because of the mm tricks that vm_copy does. Using a regular
   // memcpy when the bitmap context is modified gives us more predictable
   // performance characteristics.
   if (!mImage) {
-      //XXX: we should avoid creating this colorspace everytime
-      CGColorSpaceRef colorSpace = nullptr;
-      CGBitmapInfo bitinfo = 0;
-      CGDataProviderRef dataProvider = nullptr;
-      int bitsPerComponent = 8;
-      int bitsPerPixel = 32;
-
-      colorSpace = CGColorSpaceCreateDeviceRGB();
-      bitinfo = kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Host;
-
-      void *info = mData;
-
-      dataProvider = CGDataProviderCreateWithData (info,
-                                                   mData,
-                                                   mSize.height * mStride,
-                                                   releaseCallback);
-
-      mImage = CGImageCreate (mSize.width, mSize.height,
-                              bitsPerComponent,
-                              bitsPerPixel,
-                              mStride,
-                              colorSpace,
-                              bitinfo,
-                              dataProvider,
-                              nullptr,
-                              true,
-                              kCGRenderingIntentDefault);
-
-      CGDataProviderRelease(dataProvider);
-      CGColorSpaceRelease (colorSpace);
+    mImage = CreateCGImage(mData, mData, mSize, mStride, FORMAT_B8G8R8A8);
   }
 
 }
 
 IntSize
 SourceSurfaceCGIOSurfaceContext::GetSize() const
 {
   return mSize;