Bug 1082598 - Part 2: Fix Moz2D Skia usage for Skia update. r=jrmuizel
☠☠ backed out by e6523d21523f ☠ ☠
authorLee Salzman <lsalzman@mozilla.com>
Thu, 17 Dec 2015 14:45:44 -0500
changeset 316185 c239a7bc6cbac5eeb21796a4b389833cf43c531d
parent 316184 aa380501527094672fd84092655f82e62c504482
child 316186 3183d30eb549a7392196f2d4d3c892fe6ee615b3
push id8516
push userjlund@mozilla.com
push dateFri, 18 Dec 2015 00:52:34 +0000
reviewersjrmuizel
bugs1082598
milestone46.0a1
Bug 1082598 - Part 2: Fix Moz2D Skia usage for Skia update. r=jrmuizel
gfx/2d/DrawTargetSkia.cpp
gfx/2d/DrawTargetSkia.h
gfx/2d/ScaledFontCairo.cpp
gfx/2d/SourceSurfaceSkia.cpp
--- a/gfx/2d/DrawTargetSkia.cpp
+++ b/gfx/2d/DrawTargetSkia.cpp
@@ -7,21 +7,17 @@
 #include "SourceSurfaceCairo.h"
 #include "SourceSurfaceSkia.h"
 #include "ScaledFontBase.h"
 #include "ScaledFontCairo.h"
 #include "skia/include/core/SkBitmapDevice.h"
 #include "FilterNodeSoftware.h"
 #include "HelpersSkia.h"
 
-#ifdef USE_SKIA_GPU
-#include "skia/include/gpu/SkGpuDevice.h"
-#include "skia/include/gpu/gl/GrGLInterface.h"
-#endif
-
+#include "skia/include/core/SkSurface.h"
 #include "skia/include/core/SkTypeface.h"
 #include "skia/include/effects/SkGradientShader.h"
 #include "skia/include/core/SkColorFilter.h"
 #include "skia/include/effects/SkBlurImageFilter.h"
 #include "skia/include/effects/SkLayerRasterizer.h"
 #include "Logging.h"
 #include "Tools.h"
 #include "DataSurfaceHelpers.h"
@@ -144,39 +140,42 @@ DrawTargetSkia::Snapshot()
 
   return snapshot.forget();
 }
 
 bool
 DrawTargetSkia::LockBits(uint8_t** aData, IntSize* aSize,
                           int32_t* aStride, SurfaceFormat* aFormat)
 {
-  const SkBitmap &bitmap = mCanvas->getDevice()->accessBitmap(false);
-  if (!bitmap.lockPixelsAreWritable()) {
+  /* Test if the canvas' device has accessible pixels first, as actually
+   * accessing the pixels may trigger side-effects, even if it fails.
+   */
+  if (!mCanvas->peekPixels(nullptr, nullptr)) {
+    return false;
+  }
+
+  SkImageInfo info;
+  size_t rowBytes;
+  void* pixels = mCanvas->accessTopLayerPixels(&info, &rowBytes);
+  if (!pixels) {
     return false;
   }
 
   MarkChanged();
 
-  bitmap.lockPixels();
-  *aData = reinterpret_cast<uint8_t*>(bitmap.getPixels());
-  *aSize = IntSize(bitmap.width(), bitmap.height());
-  *aStride = int32_t(bitmap.rowBytes());
-  *aFormat = SkiaColorTypeToGfxFormat(bitmap.colorType());
+  *aData = reinterpret_cast<uint8_t*>(pixels);
+  *aSize = IntSize(info.width(), info.height());
+  *aStride = int32_t(rowBytes);
+  *aFormat = SkiaColorTypeToGfxFormat(info.colorType());
   return true;
 }
 
 void
 DrawTargetSkia::ReleaseBits(uint8_t* aData)
 {
-  const SkBitmap &bitmap = mCanvas->getDevice()->accessBitmap(false);
-  MOZ_ASSERT(bitmap.lockPixelsAreWritable());
-
-  bitmap.unlockPixels();
-  bitmap.notifyPixelsChanged();
 }
 
 static void
 SetPaintPattern(SkPaint& aPaint, const Pattern& aPattern, TempBitmap& aTmpBitmap,
                 Float aAlpha = 1.0)
 {
   switch (aPattern.GetType()) {
     case PatternType::COLOR: {
@@ -261,17 +260,17 @@ SetPaintPattern(SkPaint& aPaint, const P
       SkShader::TileMode xTileMode = ExtendModeToTileMode(pat.mExtendMode, Axis::X_AXIS);
       SkShader::TileMode yTileMode = ExtendModeToTileMode(pat.mExtendMode, Axis::Y_AXIS);
 
       SkShader* shader = SkShader::CreateBitmapShader(bitmap, xTileMode, yTileMode);
       SkShader* matrixShader = SkShader::CreateLocalMatrixShader(shader, mat);
       SkSafeUnref(shader);
       SkSafeUnref(aPaint.setShader(matrixShader));
       if (pat.mFilter == Filter::POINT) {
-        aPaint.setFilterLevel(SkPaint::kNone_FilterLevel);
+        aPaint.setFilterQuality(kNone_SkFilterQuality);
       }
       break;
     }
   }
 }
 
 static inline Rect
 GetClipBounds(SkCanvas *aCanvas)
@@ -328,17 +327,17 @@ struct AutoPaintSetup {
       temp.setAlpha(ColorFloatToByte(aOptions.mAlpha));
       //TODO: Get a rect here
       mCanvas->saveLayer(nullptr, &temp);
       mNeedsRestore = true;
     } else {
       mPaint.setAlpha(ColorFloatToByte(aOptions.mAlpha));
       mAlpha = aOptions.mAlpha;
     }
-    mPaint.setFilterLevel(SkPaint::kLow_FilterLevel);
+    mPaint.setFilterQuality(kLow_SkFilterQuality);
   }
 
   // TODO: Maybe add an operator overload to access this easier?
   SkPaint mPaint;
   TempBitmap mTmpBitmap;
   bool mNeedsRestore;
   SkCanvas* mCanvas;
   Float mAlpha;
@@ -376,20 +375,20 @@ DrawTargetSkia::DrawSurface(SourceSurfac
 
   SkRect destRect = RectToSkRect(aDest);
   SkRect sourceRect = RectToSkRect(aSource);
 
   TempBitmap bitmap = GetBitmapForSurface(aSurface);
 
   AutoPaintSetup paint(mCanvas.get(), aOptions, &aDest);
   if (aSurfOptions.mFilter == Filter::POINT) {
-    paint.mPaint.setFilterLevel(SkPaint::kNone_FilterLevel);
+    paint.mPaint.setFilterQuality(kNone_SkFilterQuality);
   }
 
-  mCanvas->drawBitmapRectToRect(bitmap.mBitmap, &sourceRect, destRect, &paint.mPaint);
+  mCanvas->drawBitmapRect(bitmap.mBitmap, sourceRect, destRect, &paint.mPaint);
 }
 
 DrawTargetType
 DrawTargetSkia::GetType() const
 {
 #ifdef USE_SKIA_GPU
   if (mGrContext) {
     return DrawTargetType::HARDWARE_RASTER;
@@ -792,19 +791,17 @@ DrawTargetSkia::CopySurface(SourceSurfac
 
   TempBitmap bitmap = GetBitmapForSurface(aSurface);
 
   // This is a fast path that is disabled for now to mimimize risk
   if (false && !bitmap.mBitmap.getTexture() && mCanvas->imageInfo() == bitmap.mBitmap.info()) {
     SkBitmap bm(bitmap.mBitmap);
     bm.lockPixels();
     if (bm.getPixels()) {
-      SkImageInfo info = bm.info();
-      info.fWidth = aSourceRect.width;
-      info.fHeight = aSourceRect.height;
+      SkImageInfo info = bm.info().makeWH(aSourceRect.width, aSourceRect.height);
       uint8_t* pixels = static_cast<uint8_t*>(bm.getPixels());
       // adjust pixels for the source offset
       pixels += aSourceRect.x + aSourceRect.y*bm.rowBytes();
       mCanvas->writePixels(info, pixels, bm.rowBytes(), aDestination.x, aDestination.y);
       return;
     }
   }
 
@@ -826,17 +823,17 @@ DrawTargetSkia::CopySurface(SourceSurfac
   // drawBitmapRect with A8 bitmaps ends up doing a mask operation
   // so we need to clear before
   if (bitmap.mBitmap.colorType() == kAlpha_8_SkColorType) {
     SkPaint clearPaint;
     clearPaint.setColor(SkColorSetARGB(0, 0, 0, 0));
     clearPaint.setXfermodeMode(SkXfermode::kSrc_Mode);
     mCanvas->drawPaint(clearPaint);
   }
-  mCanvas->drawBitmapRect(bitmap.mBitmap, &source, dest, &paint);
+  mCanvas->drawBitmapRect(bitmap.mBitmap, source, dest, &paint);
   mCanvas->restore();
 }
 
 bool
 DrawTargetSkia::Init(const IntSize &aSize, SurfaceFormat aFormat)
 {
   if (size_t(std::max(aSize.width, aSize.height)) > GetMaxSurfaceSize()) {
     return false;
@@ -849,17 +846,17 @@ DrawTargetSkia::Init(const IntSize &aSiz
         aSize.width, aSize.height,
         GfxFormatToSkiaColorType(aFormat),
         alphaType);
   // we need to have surfaces that have a stride aligned to 4 for interop with cairo
   int stride = (BytesPerPixel(aFormat)*aSize.width + (4-1)) & -4;
 
   SkBitmap bitmap;
   bitmap.setInfo(skiInfo, stride);
-  if (!bitmap.allocPixels()) {
+  if (!bitmap.tryAllocPixels()) {
     return false;
   }
 
   bitmap.eraseARGB(0, 0, 0, 0);
 
   mCanvas.adopt(new SkCanvas(bitmap));
   mSize = aSize;
 
@@ -878,34 +875,38 @@ DrawTargetSkia::InitWithGrContext(GrCont
   if (size_t(std::max(aSize.width, aSize.height)) > GetMaxSurfaceSize()) {
     return false;
   }
 
   mGrContext = aGrContext;
   mSize = aSize;
   mFormat = aFormat;
 
-  GrTextureDesc targetDescriptor;
+  GrSurfaceDesc targetDescriptor;
 
-  targetDescriptor.fFlags = kRenderTarget_GrTextureFlagBit;
+  targetDescriptor.fFlags = kRenderTarget_GrSurfaceFlag;
   targetDescriptor.fWidth = mSize.width;
   targetDescriptor.fHeight = mSize.height;
   targetDescriptor.fConfig = GfxFormatToGrConfig(mFormat);
   targetDescriptor.fOrigin = kBottomLeft_GrSurfaceOrigin;
   targetDescriptor.fSampleCnt = 0;
 
-  SkAutoTUnref<GrTexture> skiaTexture(mGrContext->createUncachedTexture(targetDescriptor, NULL, 0));
+  SkAutoTUnref<GrTexture> skiaTexture(mGrContext->textureProvider()->createTexture(targetDescriptor, SkSurface::kNo_Budgeted, nullptr, 0));
   if (!skiaTexture) {
     return false;
   }
 
-  mTexture = (uint32_t)skiaTexture->getTextureHandle();
+  SkAutoTUnref<SkSurface> gpuSurface(SkSurface::NewRenderTargetDirect(skiaTexture->asRenderTarget()));
+  if (!gpuSurface) {
+    return false;
+  }
 
-  SkAutoTUnref<SkBaseDevice> device(new SkGpuDevice(mGrContext.get(), skiaTexture->asRenderTarget()));
-  mCanvas.adopt(new SkCanvas(device.get()));
+  mTexture = reinterpret_cast<GrGLTextureInfo *>(skiaTexture->getTextureHandle())->fID;
+
+  mCanvas = gpuSurface->getCanvas();
 
   return true;
 }
 
 #endif
 
 void
 DrawTargetSkia::Init(unsigned char* aData, const IntSize &aSize, int32_t aStride, SurfaceFormat aFormat)
--- a/gfx/2d/DrawTargetSkia.h
+++ b/gfx/2d/DrawTargetSkia.h
@@ -137,17 +137,17 @@ private:
   bool ShouldLCDRenderText(FontType aFontType, AntialiasMode aAntialiasMode);
 
   SkRect SkRectCoveringWholeSurface() const;
 
   bool UsingSkiaGPU() const;
 
 #ifdef USE_SKIA_GPU
   RefPtrSkia<GrContext> mGrContext;
-  uint32_t mTexture;
+  GrGLuint mTexture;
 #endif
 
   IntSize mSize;
   RefPtrSkia<SkCanvas> mCanvas;
   SourceSurfaceSkia* mSnapshot;
 };
 
 } // namespace gfx
--- a/gfx/2d/ScaledFontCairo.cpp
+++ b/gfx/2d/ScaledFontCairo.cpp
@@ -36,30 +36,30 @@ ScaledFontCairo::ScaledFontCairo(cairo_s
   SetCairoScaledFont(aScaledFont);
 }
 
 #if defined(USE_SKIA) && defined(MOZ_ENABLE_FREETYPE)
 SkTypeface* ScaledFontCairo::GetSkTypeface()
 {
   if (!mTypeface) {
     cairo_font_face_t* fontFace = cairo_scaled_font_get_font_face(mScaledFont);
+    MOZ_ASSERT(cairo_font_face_status(fontFace) == CAIRO_STATUS_SUCCESS);
+
     FT_Face face = cairo_ft_scaled_font_lock_face(mScaledFont);
 
-    int style = SkTypeface::kNormal;
-
-    if (face->style_flags & FT_STYLE_FLAG_ITALIC)
-    style |= SkTypeface::kItalic;
-
-    if (face->style_flags & FT_STYLE_FLAG_BOLD)
-      style |= SkTypeface::kBold;
+    SkFontStyle style(face->style_flags & FT_STYLE_FLAG_BOLD ?
+                        SkFontStyle::kBold_Weight : SkFontStyle::kNormal_Weight,
+                      SkFontStyle::kNormal_Width,
+                      face->style_flags & FT_STYLE_FLAG_ITALIC ?
+                        SkFontStyle::kItalic_Slant : SkFontStyle::kUpright_Slant);
 
     bool isFixedWidth = face->face_flags & FT_FACE_FLAG_FIXED_WIDTH;
     cairo_ft_scaled_font_unlock_face(mScaledFont);
 
-    mTypeface = SkCreateTypefaceFromCairoFont(fontFace, (SkTypeface::Style)style, isFixedWidth);
+    mTypeface = SkCreateTypefaceFromCairoFont(fontFace, style, isFixedWidth);
   }
 
   return mTypeface;
 }
 #endif
 
 } // namespace gfx
 } // namespace mozilla
--- a/gfx/2d/SourceSurfaceSkia.cpp
+++ b/gfx/2d/SourceSurfaceSkia.cpp
@@ -8,16 +8,17 @@
 #include "SourceSurfaceSkia.h"
 #include "skia/include/core/SkBitmap.h"
 #include "skia/include/core/SkDevice.h"
 #include "HelpersSkia.h"
 #include "DrawTargetSkia.h"
 #include "DataSurfaceHelpers.h"
 
 #ifdef USE_SKIA_GPU
+#include "GLDefs.h"
 #include "skia/include/gpu/SkGrPixelRef.h"
 #endif
 
 namespace mozilla {
 namespace gfx {
 
 SourceSurfaceSkia::SourceSurfaceSkia()
   : mDrawTarget(nullptr), mLocked(false)
@@ -45,23 +46,21 @@ SourceSurfaceSkia::GetFormat() const
   return mFormat;
 }
 
 bool
 SourceSurfaceSkia::InitFromCanvas(SkCanvas* aCanvas,
                                   SurfaceFormat aFormat,
                                   DrawTargetSkia* aOwner)
 {
-  SkISize size = aCanvas->getDeviceSize();
-
-  mBitmap = (SkBitmap)aCanvas->getDevice()->accessBitmap(false);
+  mBitmap = aCanvas->getDevice()->accessBitmap(false);
 
   mFormat = aFormat;
 
-  mSize = IntSize(size.fWidth, size.fHeight);
+  mSize = IntSize(mBitmap.width(), mBitmap.height());
   mStride = mBitmap.rowBytes();
   mDrawTarget = aOwner;
 
   return true;
 }
 
 bool 
 SourceSurfaceSkia::InitFromData(unsigned char* aData,
@@ -79,20 +78,16 @@ SourceSurfaceSkia::InitFromData(unsigned
                                        alphaType);
   temp.setInfo(info, aStride);
   temp.setPixels(aData);
 
   if (!temp.copyTo(&mBitmap, GfxFormatToSkiaColorType(aFormat))) {
     return false;
   }
 
-  if (aFormat == SurfaceFormat::B8G8R8X8) {
-    mBitmap.setAlphaType(kIgnore_SkAlphaType);
-  }
-
   mSize = aSize;
   mFormat = aFormat;
   mStride = mBitmap.rowBytes();
   return true;
 }
 
 bool
 SourceSurfaceSkia::InitFromTexture(DrawTargetSkia* aOwner,
@@ -104,21 +99,25 @@ SourceSurfaceSkia::InitFromTexture(DrawT
 #ifdef USE_SKIA_GPU
   GrBackendTextureDesc skiaTexGlue;
   mSize.width = skiaTexGlue.fWidth = aSize.width;
   mSize.height = skiaTexGlue.fHeight = aSize.height;
   skiaTexGlue.fFlags = kNone_GrBackendTextureFlag;
   skiaTexGlue.fOrigin = kTopLeft_GrSurfaceOrigin;
   skiaTexGlue.fConfig = GfxFormatToGrConfig(aFormat);
   skiaTexGlue.fSampleCnt = 0;
-  skiaTexGlue.fTextureHandle = aTexture;
 
-  GrTexture *skiaTexture = aOwner->mGrContext->wrapBackendTexture(skiaTexGlue);
+  GrGLTextureInfo texInfo;
+  texInfo.fTarget = LOCAL_GL_TEXTURE_2D;
+  texInfo.fID = aTexture;
+  skiaTexGlue.fTextureHandle = reinterpret_cast<GrBackendObject>(&texInfo);
+
+  GrTexture *skiaTexture = aOwner->mGrContext->textureProvider()->wrapBackendTexture(skiaTexGlue);
   SkImageInfo imgInfo = SkImageInfo::Make(aSize.width, aSize.height, GfxFormatToSkiaColorType(aFormat), kOpaque_SkAlphaType);
-  SkGrPixelRef *texRef = new SkGrPixelRef(imgInfo, skiaTexture, false);
+  SkGrPixelRef *texRef = new SkGrPixelRef(imgInfo, skiaTexture);
   mBitmap.setInfo(imgInfo);
   mBitmap.setPixelRef(texRef);
   mFormat = aFormat;
   mStride = mBitmap.rowBytes();
 #endif
 
   mDrawTarget = aOwner;
   return true;