Bug 777614 - Handle BGRX SourceSurfaces in Skia by converting them to BGRA r=nrc
authorGeorge Wright <gw@gwright.org.uk>
Wed, 12 Sep 2012 19:07:53 -0400
changeset 107524 70bf3735aa3debaaa32aba0317317519c6f8293c
parent 107523 73adb13034a8494c0cff7ac391221f460654fb8d
child 107525 2e3a9cb6edb9565a8c4b0e1719f4c06bb8e398ba
push id82
push usershu@rfrn.org
push dateFri, 05 Oct 2012 13:20:22 +0000
reviewersnrc
bugs777614
milestone18.0a1
Bug 777614 - Handle BGRX SourceSurfaces in Skia by converting them to BGRA r=nrc
gfx/2d/DrawTargetSkia.cpp
gfx/2d/HelpersSkia.h
gfx/2d/SourceSurfaceSkia.cpp
--- a/gfx/2d/DrawTargetSkia.cpp
+++ b/gfx/2d/DrawTargetSkia.cpp
@@ -671,16 +671,22 @@ DrawTargetSkia::Init(const IntSize &aSiz
   mCanvas = canvas.get();
   mFormat = aFormat;
   return true;
 }
 
 void
 DrawTargetSkia::Init(unsigned char* aData, const IntSize &aSize, int32_t aStride, SurfaceFormat aFormat)
 {
+  if (aFormat == FORMAT_B8G8R8X8) {
+    // We have to manually set the A channel to be 255 as Skia doesn't understand BGRX
+    ConvertBGRXToBGRA(aData, aSize, aStride);
+    mBitmap.setIsOpaque(true);
+  }
+
   mBitmap.setConfig(GfxFormatToSkiaConfig(aFormat), aSize.width, aSize.height, aStride);
   mBitmap.setPixels(aData);
   
   SkAutoTUnref<SkDevice> device(new SkDevice(mBitmap));
   SkAutoTUnref<SkCanvas> canvas(new SkCanvas(device.get()));
   mSize = aSize;
 
   mDevice = device.get();
--- a/gfx/2d/HelpersSkia.h
+++ b/gfx/2d/HelpersSkia.h
@@ -108,12 +108,25 @@ StrokeOptionsToPaint(SkPaint& aPaint, co
                                                   SkFloatToScalar(aOptions.mDashOffset));
     SkSafeUnref(aPaint.setPathEffect(dash));
   }
 
   aPaint.setStyle(SkPaint::kStroke_Style);
   return true;
 }
 
+static inline void
+ConvertBGRXToBGRA(unsigned char* aData, const IntSize &aSize, int32_t aStride)
+{
+    uint32_t* pixel = reinterpret_cast<uint32_t*>(aData);
+
+    for (int row = 0; row < aSize.height; ++row) {
+        for (int column = 0; column < aSize.width; ++column) {
+            pixel[column] |= 0xFF000000;
+        }
+        pixel += (aStride/4);
+    }
+}
+
 }
 }
 
 #endif /* MOZILLA_GFX_HELPERSSKIA_H_ */
--- a/gfx/2d/SourceSurfaceSkia.cpp
+++ b/gfx/2d/SourceSurfaceSkia.cpp
@@ -44,17 +44,26 @@ SourceSurfaceSkia::InitFromData(unsigned
 {
   SkBitmap temp;
   temp.setConfig(GfxFormatToSkiaConfig(aFormat), aSize.width, aSize.height, aStride);
   temp.setPixels(aData);
 
   if (!temp.copyTo(&mBitmap, GfxFormatToSkiaConfig(aFormat))) {
     return false;
   }
-  
+
+  if (aFormat == FORMAT_B8G8R8X8) {
+    mBitmap.lockPixels();
+    // We have to manually set the A channel to be 255 as Skia doesn't understand BGRX
+    ConvertBGRXToBGRA(reinterpret_cast<unsigned char*>(mBitmap.getPixels()), aSize, aStride);
+    mBitmap.unlockPixels();
+    mBitmap.notifyPixelsChanged();
+    mBitmap.setIsOpaque(true);
+  }
+
   mSize = aSize;
   mFormat = aFormat;
   mStride = aStride;
   return true;
 }
 
 bool
 SourceSurfaceSkia::InitWithBitmap(const SkBitmap& aBitmap,