Bug 1299435 - part 2 - fix Moz2d for Skia m55 update. r=mchang

MozReview-Commit-ID: 8jRzOaS5vqy
This commit is contained in:
Lee Salzman 2016-10-24 15:22:20 -04:00
Родитель d3d6a89296
Коммит ab9cf08d5a
15 изменённых файлов: 389 добавлений и 386 удалений

Просмотреть файл

@ -19,6 +19,7 @@
#include "skia/include/core/SkColorFilter.h"
#include "skia/include/effects/SkBlurImageFilter.h"
#include "skia/include/effects/SkLayerRasterizer.h"
#include "skia/src/core/SkSpecialImage.h"
#include "Blur.h"
#include "Logging.h"
#include "Tools.h"
@ -29,7 +30,9 @@
#include "GLDefs.h"
#include "skia/include/gpu/SkGr.h"
#include "skia/include/gpu/GrContext.h"
#include "skia/include/gpu/GrDrawContext.h"
#include "skia/include/gpu/gl/GrGLInterface.h"
#include "skia/src/image/SkImage_Gpu.h"
#endif
#ifdef MOZ_WIDGET_COCOA
@ -94,13 +97,13 @@ public:
};
/**
* When constructing a temporary SkBitmap via GetBitmapForSurface, we may also
* When constructing a temporary SkImage via GetSkImageForSurface, we may also
* have to construct a temporary DataSourceSurface, which must live as long as
* the SkBitmap. We attach this temporary surface to the bitmap's pixelref, so
* the SkImage. We attach this temporary surface to the image's pixelref, so
* that it can be released once the pixelref is freed.
*/
static void
ReleaseTemporarySurface(void* aPixels, void* aContext)
ReleaseTemporarySurface(const void* aPixels, void* aContext)
{
DataSourceSurface* surf = static_cast<DataSourceSurface*>(aContext);
if (surf) {
@ -208,37 +211,36 @@ VerifyRGBXCorners(uint8_t* aData, const IntSize &aSize, const int32_t aStride, S
}
#endif
static SkBitmap
GetBitmapForSurface(SourceSurface* aSurface)
static sk_sp<SkImage>
GetSkImageForSurface(SourceSurface* aSurface)
{
SkBitmap bitmap;
if (!aSurface) {
gfxDebug() << "Creating empty Skia bitmap from null SourceSurface";
return bitmap;
gfxDebug() << "Creating null Skia image from null SourceSurface";
return nullptr;
}
if (aSurface->GetType() == SurfaceType::SKIA) {
bitmap = static_cast<SourceSurfaceSkia*>(aSurface)->GetBitmap();
return bitmap;
return static_cast<SourceSurfaceSkia*>(aSurface)->GetImage();
}
DataSourceSurface* surf = aSurface->GetDataSurface().take();
if (!surf) {
gfxWarning() << "Failed getting DataSourceSurface for Skia bitmap";
return bitmap;
gfxWarning() << "Failed getting DataSourceSurface for Skia image";
return nullptr;
}
if (!bitmap.installPixels(MakeSkiaImageInfo(surf->GetSize(), surf->GetFormat()),
surf->GetData(), surf->Stride(), nullptr,
ReleaseTemporarySurface, surf)) {
gfxDebug() << "Failed installing pixels on Skia bitmap for temporary surface";
SkPixmap pixmap(MakeSkiaImageInfo(surf->GetSize(), surf->GetFormat()),
surf->GetData(), surf->Stride());
sk_sp<SkImage> image = SkImage::MakeFromRaster(pixmap, ReleaseTemporarySurface, surf);
if (!image) {
ReleaseTemporarySurface(nullptr, surf);
gfxDebug() << "Failed making Skia raster image for temporary surface";
}
// Skia doesn't support RGBX surfaces so ensure that the alpha value is opaque white.
MOZ_ASSERT(VerifyRGBXCorners(surf->GetData(), surf->GetSize(),
surf->Stride(), surf->GetFormat()));
return bitmap;
return image;
}
DrawTargetSkia::DrawTargetSkia()
@ -273,9 +275,19 @@ DrawTargetSkia::Snapshot()
RefPtr<SourceSurfaceSkia> snapshot = mSnapshot;
if (!snapshot) {
snapshot = new SourceSurfaceSkia();
mSnapshot = snapshot;
if (!snapshot->InitFromCanvas(mCanvas.get(), mFormat, this))
sk_sp<SkImage> image;
// If the surface is raster, making a snapshot may trigger a pixel copy.
// Instead, try to directly make a raster image referencing the surface pixels.
SkPixmap pixmap;
if (mSurface->peekPixels(&pixmap)) {
image = SkImage::MakeFromRaster(pixmap, nullptr, nullptr);
} else {
image = mSurface->makeImageSnapshot(SkBudgeted::kNo);
}
if (!snapshot->InitFromImage(image, mFormat, this)) {
return nullptr;
}
mSnapshot = snapshot;
}
return snapshot.forget();
@ -323,6 +335,81 @@ DrawTargetSkia::ReleaseBits(uint8_t* aData)
{
}
static void
ReleaseImage(const void* aPixels, void* aContext)
{
SkImage* image = static_cast<SkImage*>(aContext);
SkSafeUnref(image);
}
static sk_sp<SkImage>
ExtractSubset(sk_sp<SkImage> aImage, const IntRect& aRect)
{
SkIRect subsetRect = IntRectToSkIRect(aRect);
if (aImage->bounds() == subsetRect) {
return aImage;
}
// makeSubset is slow, so prefer to use SkPixmap::extractSubset where possible.
SkPixmap pixmap, subsetPixmap;
if (aImage->peekPixels(&pixmap) &&
pixmap.extractSubset(&subsetPixmap, subsetRect)) {
// Release the original image reference so only the subset image keeps it alive.
return SkImage::MakeFromRaster(subsetPixmap, ReleaseImage, aImage.release());
}
return aImage->makeSubset(subsetRect);
}
static inline bool
SkImageIsMask(const sk_sp<SkImage>& aImage)
{
SkPixmap pixmap;
if (aImage->peekPixels(&pixmap)) {
return pixmap.colorType() == kAlpha_8_SkColorType;
#ifdef USE_SKIA_GPU
} else if (GrTexture* tex = aImage->getTexture()) {
return GrPixelConfigIsAlphaOnly(tex->config());
#endif
} else {
return false;
}
}
static bool
ExtractAlphaBitmap(sk_sp<SkImage> aImage, SkBitmap* aResultBitmap)
{
SkImageInfo info = SkImageInfo::MakeA8(aImage->width(), aImage->height());
SkBitmap bitmap;
if (!bitmap.tryAllocPixels(info, SkAlign4(info.minRowBytes())) ||
!aImage->readPixels(bitmap.info(), bitmap.getPixels(), bitmap.rowBytes(), 0, 0)) {
gfxWarning() << "Failed reading alpha pixels for Skia bitmap";
return false;
}
*aResultBitmap = bitmap;
return true;
}
static sk_sp<SkImage>
ExtractAlphaForSurface(SourceSurface* aSurface)
{
sk_sp<SkImage> image = GetSkImageForSurface(aSurface);
if (!image) {
return nullptr;
}
if (SkImageIsMask(image)) {
return image;
}
SkBitmap bitmap;
if (!ExtractAlphaBitmap(image, &bitmap)) {
return nullptr;
}
// Mark the bitmap immutable so that it will be shared rather than copied.
bitmap.setImmutable();
return SkImage::MakeFromBitmap(bitmap);
}
static void
SetPaintPattern(SkPaint& aPaint, const Pattern& aPattern, Float aAlpha = 1.0, Point aOffset = Point(0, 0))
{
@ -386,23 +473,26 @@ SetPaintPattern(SkPaint& aPaint, const Pattern& aPattern, Float aAlpha = 1.0, Po
}
case PatternType::SURFACE: {
const SurfacePattern& pat = static_cast<const SurfacePattern&>(aPattern);
SkBitmap bitmap = GetBitmapForSurface(pat.mSurface);
sk_sp<SkImage> image = GetSkImageForSurface(pat.mSurface);
SkMatrix mat;
GfxMatrixToSkiaMatrix(pat.mMatrix, mat);
mat.postTranslate(SkFloatToScalar(aOffset.x), SkFloatToScalar(aOffset.y));
if (!pat.mSamplingRect.IsEmpty()) {
SkIRect rect = IntRectToSkIRect(pat.mSamplingRect);
bitmap.extractSubset(&bitmap, rect);
mat.preTranslate(rect.x(), rect.y());
image = ExtractSubset(image, pat.mSamplingRect);
mat.preTranslate(pat.mSamplingRect.x, pat.mSamplingRect.y);
}
SkShader::TileMode xTileMode = ExtendModeToTileMode(pat.mExtendMode, Axis::X_AXIS);
SkShader::TileMode yTileMode = ExtendModeToTileMode(pat.mExtendMode, Axis::Y_AXIS);
sk_sp<SkShader> shader = SkShader::MakeBitmapShader(bitmap, xTileMode, yTileMode, &mat);
aPaint.setShader(shader);
if (image) {
aPaint.setShader(image->makeShader(xTileMode, yTileMode, &mat));
} else {
aPaint.setColor(SK_ColorTRANSPARENT);
}
if (pat.mSamplingFilter == SamplingFilter::POINT) {
aPaint.setFilterQuality(kNone_SkFilterQuality);
}
@ -453,7 +543,7 @@ struct AutoPaintSetup {
void Init(SkCanvas *aCanvas, const DrawOptions& aOptions, const Rect* aMaskBounds, bool aForceGroup)
{
mPaint.setXfermodeMode(GfxOpToSkiaOp(aOptions.mCompositionOp));
mPaint.setBlendMode(GfxOpToSkiaOp(aOptions.mCompositionOp));
mCanvas = aCanvas;
//TODO: Can we set greyscale somehow?
@ -471,9 +561,9 @@ struct AutoPaintSetup {
// clear the clip rect. The other operators would be harder
// but could be worth it to skip pushing a group.
if (needsGroup) {
mPaint.setXfermodeMode(SkXfermode::kSrcOver_Mode);
mPaint.setBlendMode(SkBlendMode::kSrcOver);
SkPaint temp;
temp.setXfermodeMode(GfxOpToSkiaOp(aOptions.mCompositionOp));
temp.setBlendMode(GfxOpToSkiaOp(aOptions.mCompositionOp));
temp.setAlpha(ColorFloatToByte(aOptions.mAlpha));
//TODO: Get a rect here
mCanvas->saveLayer(nullptr, &temp);
@ -511,14 +601,14 @@ DrawTargetSkia::DrawSurface(SourceSurface *aSurface,
MarkChanged();
SkBitmap bitmap = GetBitmapForSurface(aSurface);
if (bitmap.empty()) {
sk_sp<SkImage> image = GetSkImageForSurface(aSurface);
if (!image) {
return;
}
SkRect destRect = RectToSkRect(aDest);
SkRect sourceRect = RectToSkRect(aSource);
bool forceGroup = bitmap.colorType() == kAlpha_8_SkColorType &&
bool forceGroup = SkImageIsMask(image) &&
aOptions.mCompositionOp != CompositionOp::OP_OVER;
AutoPaintSetup paint(mCanvas.get(), aOptions, &aDest, forceGroup);
@ -526,7 +616,7 @@ DrawTargetSkia::DrawSurface(SourceSurface *aSurface,
paint.mPaint.setFilterQuality(kNone_SkFilterQuality);
}
mCanvas->drawBitmapRect(bitmap, sourceRect, destRect, &paint.mPaint);
mCanvas->drawImageRect(image, sourceRect, destRect, &paint.mPaint);
}
DrawTargetType
@ -564,8 +654,8 @@ DrawTargetSkia::DrawSurfaceWithShadow(SourceSurface *aSurface,
MarkChanged();
SkBitmap bitmap = GetBitmapForSurface(aSurface);
if (bitmap.empty()) {
sk_sp<SkImage> image = GetSkImageForSurface(aSurface);
if (!image) {
return;
}
@ -573,7 +663,7 @@ DrawTargetSkia::DrawSurfaceWithShadow(SourceSurface *aSurface,
mCanvas->resetMatrix();
SkPaint paint;
paint.setXfermodeMode(GfxOpToSkiaOp(aOperator));
paint.setBlendMode(GfxOpToSkiaOp(aOperator));
// bug 1201272
// We can't use the SkDropShadowImageFilter here because it applies the xfer
@ -584,23 +674,20 @@ DrawTargetSkia::DrawSurfaceWithShadow(SourceSurface *aSurface,
// to blur the image ourselves.
SkPaint shadowPaint;
shadowPaint.setXfermodeMode(GfxOpToSkiaOp(aOperator));
shadowPaint.setBlendMode(GfxOpToSkiaOp(aOperator));
auto shadowDest = IntPoint::Round(aDest + aOffset);
SkBitmap blurMask;
if (!UsingSkiaGPU() &&
bitmap.extractAlpha(&blurMask)) {
ExtractAlphaBitmap(image, &blurMask)) {
// Prefer using our own box blur instead of Skia's when we're
// not using the GPU. It currently performs much better than
// SkBlurImageFilter or SkBlurMaskFilter on the CPU.
AlphaBoxBlur blur(Rect(0, 0, blurMask.width(), blurMask.height()),
int32_t(blurMask.rowBytes()),
aSigma, aSigma);
blurMask.lockPixels();
blur.Blur(reinterpret_cast<uint8_t*>(blurMask.getPixels()));
blurMask.unlockPixels();
blurMask.notifyPixelsChanged();
shadowPaint.setColor(ColorToSkColor(aColor, 1.0f));
@ -608,17 +695,17 @@ DrawTargetSkia::DrawSurfaceWithShadow(SourceSurface *aSurface,
} else {
sk_sp<SkImageFilter> blurFilter(SkBlurImageFilter::Make(aSigma, aSigma, nullptr));
sk_sp<SkColorFilter> colorFilter(
SkColorFilter::MakeModeFilter(ColorToSkColor(aColor, 1.0f), SkXfermode::kSrcIn_Mode));
SkColorFilter::MakeModeFilter(ColorToSkColor(aColor, 1.0f), SkBlendMode::kSrcIn));
shadowPaint.setImageFilter(blurFilter);
shadowPaint.setColorFilter(colorFilter);
mCanvas->drawBitmap(bitmap, shadowDest.x, shadowDest.y, &shadowPaint);
mCanvas->drawImage(image, shadowDest.x, shadowDest.y, &shadowPaint);
}
// Composite the original image after the shadow
auto dest = IntPoint::Round(aDest);
mCanvas->drawBitmap(bitmap, dest.x, dest.y, &paint);
mCanvas->drawImage(image, dest.x, dest.y, &paint);
mCanvas->restore();
}
@ -776,18 +863,18 @@ class CGClipApply : public SkCanvas::ClipVisitor {
public:
explicit CGClipApply(CGContextRef aCGContext)
: mCG(aCGContext) {}
void clipRect(const SkRect& aRect, SkRegion::Op op, bool antialias) override {
void clipRect(const SkRect& aRect, SkCanvas::ClipOp op, bool antialias) override {
CGRect rect = CGRectMake(aRect.x(), aRect.y(), aRect.width(), aRect.height());
CGContextClipToRect(mCG, rect);
}
void clipRRect(const SkRRect& rrect, SkRegion::Op op, bool antialias) override {
void clipRRect(const SkRRect& rrect, SkCanvas::ClipOp op, bool antialias) override {
SkPath path;
path.addRRect(rrect);
clipPath(path, op, antialias);
}
void clipPath(const SkPath& aPath, SkRegion::Op, bool antialias) override {
void clipPath(const SkPath& aPath, SkCanvas::ClipOp, bool antialias) override {
SkPath::Iter iter(aPath, true);
SkPoint source[4];
SkPath::Verb verb;
@ -936,7 +1023,7 @@ GfxMatrixToCGAffineTransform(const Matrix &m)
static bool
SetupCGContext(DrawTargetSkia* aDT,
CGContextRef aCGContext,
RefPtrSkia<SkCanvas> aCanvas)
sk_sp<SkCanvas> aCanvas)
{
// DrawTarget expects the origin to be at the top left, but CG
// expects it to be at the bottom left. Transform to set the origin to
@ -1229,7 +1316,7 @@ DrawTargetSkia::FillGlyphs(ScaledFont *aFont,
bool aaEnabled = aaMode != AntialiasMode::NONE;
paint.mPaint.setAntiAlias(aaEnabled);
paint.mPaint.setTypeface(typeface);
paint.mPaint.setTypeface(sk_ref_sp(typeface));
paint.mPaint.setTextSize(SkFloatToScalar(skiaFont->mSize));
paint.mPaint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
@ -1337,14 +1424,13 @@ DrawTargetSkia::MaskSurface(const Pattern &aSource,
MarkChanged();
AutoPaintSetup paint(mCanvas.get(), aOptions, aSource, nullptr, -aOffset);
SkBitmap bitmap = GetBitmapForSurface(aMask);
if (bitmap.colorType() != kAlpha_8_SkColorType &&
!bitmap.extractAlpha(&bitmap)) {
sk_sp<SkImage> alphaMask = ExtractAlphaForSurface(aMask);
if (!alphaMask) {
gfxDebug() << *this << ": MaskSurface() failed to extract alpha for mask";
return;
}
mCanvas->drawBitmap(bitmap, aOffset.x, aOffset.y, &paint.mPaint);
mCanvas->drawImage(alphaMask, aOffset.x, aOffset.y, &paint.mPaint);
}
bool
@ -1367,8 +1453,8 @@ DrawTarget::Draw3DTransformedSurface(SourceSurface* aSurface, const Matrix4x4& a
fullMat.PostTranslate(-xformBounds.x, -xformBounds.y, 0);
// Read in the source data.
SkBitmap srcBitmap = GetBitmapForSurface(aSurface);
if (srcBitmap.empty()) {
sk_sp<SkImage> srcImage = GetSkImageForSurface(aSurface);
if (!srcImage) {
return true;
}
@ -1376,17 +1462,16 @@ DrawTarget::Draw3DTransformedSurface(SourceSurface* aSurface, const Matrix4x4& a
// Try to pass through the source's format unmodified in both the BGRA and ARGB cases.
RefPtr<DataSourceSurface> dstSurf =
Factory::CreateDataSourceSurface(xformBounds.Size(),
srcBitmap.alphaType() == kPremul_SkAlphaType ?
!srcImage->isOpaque() ?
aSurface->GetFormat() : SurfaceFormat::A8R8G8B8_UINT32,
true);
if (!dstSurf) {
return false;
}
SkAutoTUnref<SkCanvas> dstCanvas(
sk_sp<SkCanvas> dstCanvas(
SkCanvas::NewRasterDirect(
SkImageInfo::Make(xformBounds.width, xformBounds.height,
srcBitmap.alphaType() == kPremul_SkAlphaType ?
srcBitmap.colorType() : kBGRA_8888_SkColorType,
GfxFormatToSkiaColorType(dstSurf->GetFormat()),
kPremul_SkAlphaType),
dstSurf->GetData(), dstSurf->Stride()));
if (!dstCanvas) {
@ -1397,13 +1482,13 @@ DrawTarget::Draw3DTransformedSurface(SourceSurface* aSurface, const Matrix4x4& a
SkPaint paint;
paint.setAntiAlias(true);
paint.setFilterQuality(kLow_SkFilterQuality);
paint.setXfermodeMode(SkXfermode::kSrc_Mode);
paint.setBlendMode(SkBlendMode::kSrc);
SkMatrix xform;
GfxMatrixToSkiaMatrix(fullMat, xform);
dstCanvas->setMatrix(xform);
dstCanvas->drawBitmap(srcBitmap, 0, 0, &paint);
dstCanvas->drawImage(srcImage, 0, 0, &paint);
dstCanvas->flush();
// Temporarily reset the DT's transform, since it has already been composed above.
@ -1427,8 +1512,8 @@ DrawTargetSkia::Draw3DTransformedSurface(SourceSurface* aSurface, const Matrix4x
MarkChanged();
SkBitmap bitmap = GetBitmapForSurface(aSurface);
if (bitmap.empty()) {
sk_sp<SkImage> image = GetSkImageForSurface(aSurface);
if (!image) {
return true;
}
@ -1442,7 +1527,7 @@ DrawTargetSkia::Draw3DTransformedSurface(SourceSurface* aSurface, const Matrix4x
GfxMatrixToSkiaMatrix(aMatrix, xform);
mCanvas->concat(xform);
mCanvas->drawBitmap(bitmap, 0, 0, &paint);
mCanvas->drawImage(image, 0, 0, &paint);
mCanvas->restore();
@ -1499,23 +1584,19 @@ DrawTargetSkia::UsingSkiaGPU() const
already_AddRefed<SourceSurface>
DrawTargetSkia::OptimizeGPUSourceSurface(SourceSurface *aSurface) const
{
// Check if the underlying SkBitmap already has an associated GrTexture.
if (aSurface->GetType() == SurfaceType::SKIA &&
static_cast<SourceSurfaceSkia*>(aSurface)->GetBitmap().getTexture()) {
// Check if the underlying SkImage already has an associated GrTexture.
sk_sp<SkImage> image = GetSkImageForSurface(aSurface);
if (!image || image->isTextureBacked()) {
RefPtr<SourceSurface> surface(aSurface);
return surface.forget();
}
SkBitmap bitmap = GetBitmapForSurface(aSurface);
// Upload the SkBitmap to a GrTexture otherwise.
SkAutoTUnref<GrTexture> texture(
GrRefCachedBitmapTexture(mGrContext.get(), bitmap, GrTextureParams::ClampBilerp()));
// Upload the SkImage to a GrTexture otherwise.
sk_sp<SkImage> texture = image->makeTextureImage(mGrContext.get());
if (texture) {
// Create a new SourceSurfaceSkia whose SkBitmap contains the GrTexture.
// Create a new SourceSurfaceSkia whose SkImage contains the GrTexture.
RefPtr<SourceSurfaceSkia> surface = new SourceSurfaceSkia();
if (surface->InitFromGrTexture(texture, aSurface->GetSize(), aSurface->GetFormat())) {
if (surface->InitFromImage(texture, aSurface->GetFormat())) {
return surface.forget();
}
}
@ -1529,7 +1610,7 @@ DrawTargetSkia::OptimizeGPUSourceSurface(SourceSurface *aSurface) const
// Wrap it in a Skia source surface so that can do tiled uploads on-demand.
RefPtr<SourceSurfaceSkia> surface = new SourceSurfaceSkia();
surface->InitFromBitmap(bitmap);
surface->InitFromImage(image);
return surface.forget();
}
#endif
@ -1600,10 +1681,11 @@ DrawTargetSkia::CreateSourceSurfaceFromNativeSurface(const NativeSurface &aSurfa
texInfo.fID = (GrGLuint)(uintptr_t)aSurface.mSurface;
texDesc.fTextureHandle = reinterpret_cast<GrBackendObject>(&texInfo);
SkAutoTUnref<GrTexture> texture(mGrContext->textureProvider()->wrapBackendTexture(texDesc, kAdopt_GrWrapOwnership));
sk_sp<SkImage> texture =
SkImage::MakeFromAdoptedTexture(mGrContext.get(), texDesc,
GfxFormatToSkiaAlphaType(aSurface.mFormat));
RefPtr<SourceSurfaceSkia> newSurf = new SourceSurfaceSkia();
if (newSurf->InitFromGrTexture(texture, aSurface.mSize, aSurface.mFormat)) {
if (texture && newSurf->InitFromImage(texture, aSurface.mFormat)) {
return newSurf.forget();
}
return nullptr;
@ -1620,27 +1702,27 @@ DrawTargetSkia::CopySurface(SourceSurface *aSurface,
{
MarkChanged();
SkBitmap bitmap = GetBitmapForSurface(aSurface);
if (bitmap.empty()) {
sk_sp<SkImage> image = GetSkImageForSurface(aSurface);
if (!image) {
return;
}
mCanvas->save();
mCanvas->setMatrix(SkMatrix::MakeTrans(SkIntToScalar(aDestination.x), SkIntToScalar(aDestination.y)));
mCanvas->clipRect(SkRect::MakeIWH(aSourceRect.width, aSourceRect.height), SkRegion::kReplace_Op);
mCanvas->clipRect(SkRect::MakeIWH(aSourceRect.width, aSourceRect.height), kReplace_SkClipOp);
SkPaint paint;
if (!bitmap.isOpaque()) {
if (!image->isOpaque()) {
// Keep the xfermode as SOURCE_OVER for opaque bitmaps
// http://code.google.com/p/skia/issues/detail?id=628
paint.setXfermodeMode(SkXfermode::kSrc_Mode);
paint.setBlendMode(SkBlendMode::kSrc);
}
// drawBitmap with A8 bitmaps ends up doing a mask operation
// drawImage with A8 images ends up doing a mask operation
// so we need to clear before
if (bitmap.colorType() == kAlpha_8_SkColorType) {
if (SkImageIsMask(image)) {
mCanvas->clear(SK_ColorTRANSPARENT);
}
mCanvas->drawBitmap(bitmap, -SkIntToScalar(aSourceRect.x), -SkIntToScalar(aSourceRect.y), &paint);
mCanvas->drawImage(image, -SkIntToScalar(aSourceRect.x), -SkIntToScalar(aSourceRect.y), &paint);
mCanvas->restore();
}
@ -1652,21 +1734,20 @@ DrawTargetSkia::Init(const IntSize &aSize, SurfaceFormat aFormat)
}
// 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(MakeSkiaImageInfo(aSize, aFormat), stride);
if (!bitmap.tryAllocPixels()) {
SkImageInfo info = MakeSkiaImageInfo(aSize, aFormat);
size_t stride = SkAlign4(info.minRowBytes());
mSurface = SkSurface::MakeRaster(info, stride, nullptr);
if (!mSurface) {
return false;
}
SkColor clearColor = (aFormat == SurfaceFormat::B8G8R8X8) ? SK_ColorBLACK : SK_ColorTRANSPARENT;
bitmap.eraseColor(clearColor);
mCanvas.adopt(new SkCanvas(bitmap));
mSize = aSize;
mFormat = aFormat;
mCanvas = sk_ref_sp(mSurface->getCanvas());
if (info.isOpaque()) {
mCanvas->clear(SK_ColorBLACK);
}
return true;
}
@ -1704,39 +1785,38 @@ DrawTargetSkia::InitWithGrContext(GrContext* aGrContext,
// Create a GPU rendertarget/texture using the supplied GrContext.
// NewRenderTarget also implicitly clears the underlying texture on creation.
sk_sp<SkSurface> gpuSurface =
mSurface =
SkSurface::MakeRenderTarget(aGrContext,
SkBudgeted(aCached),
MakeSkiaImageInfo(aSize, aFormat));
if (!gpuSurface) {
if (!mSurface) {
return false;
}
mGrContext = aGrContext;
mGrContext = sk_ref_sp(aGrContext);
mSize = aSize;
mFormat = aFormat;
mCanvas = gpuSurface->getCanvas();
mCanvas = sk_ref_sp(mSurface->getCanvas());
return true;
}
#endif
void
bool
DrawTargetSkia::Init(unsigned char* aData, const IntSize &aSize, int32_t aStride, SurfaceFormat aFormat, bool aUninitialized)
{
MOZ_ASSERT((aFormat != SurfaceFormat::B8G8R8X8) ||
aUninitialized || VerifyRGBXFormat(aData, aSize, aStride, aFormat));
SkBitmap bitmap;
bitmap.setInfo(MakeSkiaImageInfo(aSize, aFormat), aStride);
bitmap.setPixels(aData);
mCanvas.adopt(new SkCanvas(bitmap));
mSurface = SkSurface::MakeRasterDirect(MakeSkiaImageInfo(aSize, aFormat), aData, aStride);
if (!mSurface) {
return false;
}
mSize = aSize;
mFormat = aFormat;
mCanvas = sk_ref_sp(mSurface->getCanvas());
return true;
}
void
@ -1753,14 +1833,9 @@ DrawTargetSkia::GetNativeSurface(NativeSurfaceType aType)
{
#ifdef USE_SKIA_GPU
if (aType == NativeSurfaceType::OPENGL_TEXTURE) {
// Get the current texture backing the GPU device.
// Beware - this texture is only guaranteed to valid after a draw target flush.
GrRenderTarget* rt = mCanvas->getDevice()->accessRenderTarget();
if (rt) {
GrTexture* tex = rt->asTexture();
if (tex) {
return (void*)(uintptr_t)reinterpret_cast<GrGLTextureInfo *>(tex->getTextureHandle())->fID;
}
GrBackendObject handle = mSurface->getTextureHandle(SkSurface::kFlushRead_BackendHandleAccess);
if (handle) {
return (void*)(uintptr_t)reinterpret_cast<GrGLTextureInfo *>(handle)->fID;
}
}
#endif
@ -1779,7 +1854,7 @@ DrawTargetSkia::ClearRect(const Rect &aRect)
{
MarkChanged();
mCanvas->save();
mCanvas->clipRect(RectToSkRect(aRect), SkRegion::kIntersect_Op, true);
mCanvas->clipRect(RectToSkRect(aRect), kIntersect_SkClipOp, true);
SkColor clearColor = (mFormat == SurfaceFormat::B8G8R8X8) ? SK_ColorBLACK : SK_ColorTRANSPARENT;
mCanvas->clear(clearColor);
mCanvas->restore();
@ -1794,7 +1869,7 @@ DrawTargetSkia::PushClip(const Path *aPath)
const PathSkia *skiaPath = static_cast<const PathSkia*>(aPath);
mCanvas->save();
mCanvas->clipPath(skiaPath->GetPath(), SkRegion::kIntersect_Op, true);
mCanvas->clipPath(skiaPath->GetPath(), kIntersect_SkClipOp, true);
}
void
@ -1810,7 +1885,7 @@ DrawTargetSkia::PushDeviceSpaceClipRects(const IntRect* aRects, uint32_t aCount)
// this region by the current transform, unlike the other SkCanvas
// clip methods, so it is just passed through in device-space.
mCanvas->save();
mCanvas->clipRegion(region, SkRegion::kIntersect_Op);
mCanvas->clipRegion(region, kIntersect_SkClipOp);
}
void
@ -1819,7 +1894,7 @@ DrawTargetSkia::PushClipRect(const Rect& aRect)
SkRect rect = RectToSkRect(aRect);
mCanvas->save();
mCanvas->clipRect(rect, SkRegion::kIntersect_Op, true);
mCanvas->clipRect(rect, kIntersect_SkClipOp, true);
}
void
@ -1833,14 +1908,14 @@ class CopyLayerImageFilter : public SkImageFilter
{
public:
CopyLayerImageFilter()
: SkImageFilter(0, nullptr)
: SkImageFilter(nullptr, 0, nullptr)
{}
virtual bool onFilterImageDeprecated(Proxy*, const SkBitmap& src, const Context&,
SkBitmap* result, SkIPoint* offset) const override {
*result = src;
virtual sk_sp<SkSpecialImage> onFilterImage(SkSpecialImage* source,
const Context& ctx,
SkIPoint* offset) const override {
offset->set(0, 0);
return true;
return sk_ref_sp(source);
}
SK_TO_STRING_OVERRIDE()
@ -1878,7 +1953,7 @@ DrawTargetSkia::PushLayer(bool aOpaque, Float aOpacity, SourceSurface* aMask,
SkRect bounds = IntRectToSkRect(aBounds);
SkAutoTUnref<SkImageFilter> backdrop(aCopyBackground ? new CopyLayerImageFilter : nullptr);
sk_sp<SkImageFilter> backdrop(aCopyBackground ? new CopyLayerImageFilter : nullptr);
SkCanvas::SaveLayerRec saveRec(aBounds.IsEmpty() ? nullptr : &bounds,
&paint,
@ -1904,12 +1979,26 @@ DrawTargetSkia::PopLayer()
const PushedLayer& layer = mPushedLayers.back();
if (layer.mMask) {
// If we have a mask, take a reference to the layer's bitmap device so that
// If we have a mask, take a reference to the top layer's device so that
// we can mask it ourselves. This assumes we forced SkCanvas::restore to
// skip implicitly drawing the layer.
SkAutoTUnref<SkBaseDevice> layerDevice(SkSafeRef(mCanvas->getTopDevice()));
sk_sp<SkBaseDevice> layerDevice = sk_ref_sp(mCanvas->getTopDevice());
SkIRect layerBounds = layerDevice->getGlobalBounds();
SkBitmap layerBitmap = layerDevice->accessBitmap(false);
sk_sp<SkImage> layerImage;
SkPixmap layerPixmap;
if (layerDevice->peekPixels(&layerPixmap)) {
layerImage = SkImage::MakeFromRaster(layerPixmap, nullptr, nullptr);
#ifdef USE_SKIA_GPU
} else if (GrDrawContext* drawCtx = mCanvas->internal_private_accessTopLayerDrawContext()) {
drawCtx->prepareForExternalIO();
if (GrTexture* tex = drawCtx->accessRenderTarget()->asTexture()) {
layerImage = sk_make_sp<SkImage_Gpu>(layerBounds.width(), layerBounds.height(),
kNeedNewImageUniqueID,
layerDevice->imageInfo().alphaType(),
tex, nullptr, SkBudgeted::kNo);
}
#endif
}
// Restore the background with the layer's device left alive.
mCanvas->restore();
@ -1929,15 +2018,15 @@ DrawTargetSkia::PopLayer()
// even though the mask is. So first we use the inverse of the transform
// affecting the mask, then add back on the layer's origin.
layerMat.preTranslate(layerBounds.x(), layerBounds.y());
sk_sp<SkShader> shader = SkShader::MakeBitmapShader(layerBitmap,
SkShader::kClamp_TileMode,
SkShader::kClamp_TileMode,
&layerMat);
paint.setShader(shader);
SkBitmap mask = GetBitmapForSurface(layer.mMask);
if (mask.colorType() != kAlpha_8_SkColorType &&
!mask.extractAlpha(&mask)) {
if (layerImage) {
paint.setShader(layerImage->makeShader(SkShader::kClamp_TileMode, SkShader::kClamp_TileMode, &layerMat));
} else {
paint.setColor(SK_ColorTRANSPARENT);
}
sk_sp<SkImage> alphaMask = ExtractAlphaForSurface(layer.mMask);
if (!alphaMask) {
gfxDebug() << *this << ": PopLayer() failed to extract alpha for mask";
} else {
mCanvas->save();
@ -1948,7 +2037,7 @@ DrawTargetSkia::PopLayer()
mCanvas->clipRect(SkRect::Make(layerBounds));
mCanvas->setMatrix(maskMat);
mCanvas->drawBitmap(mask, 0, 0, &paint);
mCanvas->drawImage(alphaMask, 0, 0, &paint);
mCanvas->restore();
}
@ -1992,6 +2081,9 @@ DrawTargetSkia::MarkChanged()
if (mSnapshot) {
mSnapshot->DrawTargetWillChange();
mSnapshot = nullptr;
// Handle copying of any image snapshots bound to the surface.
mSurface->notifyContentWillChange(SkSurface::kRetain_ContentChangeMode);
}
}

Просмотреть файл

@ -7,6 +7,7 @@
#define _MOZILLA_GFX_SOURCESURFACESKIA_H
#include "skia/include/core/SkCanvas.h"
#include "skia/include/core/SkSurface.h"
#include "2D.h"
#include "HelpersSkia.h"
@ -130,7 +131,7 @@ public:
virtual void DetachAllSnapshots() override { MarkChanged(); }
bool Init(const IntSize &aSize, SurfaceFormat aFormat);
void Init(unsigned char* aData, const IntSize &aSize, int32_t aStride, SurfaceFormat aFormat, bool aUninitialized = false);
bool Init(unsigned char* aData, const IntSize &aSize, int32_t aStride, SurfaceFormat aFormat, bool aUninitialized = false);
#ifdef USE_SKIA_GPU
bool InitWithGrContext(GrContext* aGrContext,
@ -190,11 +191,12 @@ private:
std::vector<PushedLayer> mPushedLayers;
#ifdef USE_SKIA_GPU
RefPtrSkia<GrContext> mGrContext;
sk_sp<GrContext> mGrContext;
#endif
IntSize mSize;
RefPtrSkia<SkCanvas> mCanvas;
sk_sp<SkSurface> mSurface;
sk_sp<SkCanvas> mCanvas;
SourceSurfaceSkia* mSnapshot;
#ifdef MOZ_WIDGET_COCOA

Просмотреть файл

@ -382,8 +382,9 @@ Factory::CreateDrawTargetForData(BackendType aBackend,
{
RefPtr<DrawTargetSkia> newTarget;
newTarget = new DrawTargetSkia();
newTarget->Init(aData, aSize, aStride, aFormat, aUninitialized);
retVal = newTarget;
if (newTarget->Init(aData, aSize, aStride, aFormat, aUninitialized)) {
retVal = newTarget;
}
break;
}
#endif

Просмотреть файл

@ -15,7 +15,6 @@
#endif
#include "mozilla/Assertions.h"
#include <vector>
#include "RefPtrSkia.h"
#include "nsDebug.h"
namespace mozilla {
@ -187,65 +186,65 @@ StrokeOptionsToPaint(SkPaint& aPaint, const StrokeOptions &aOptions)
return true;
}
static inline SkXfermode::Mode
static inline SkBlendMode
GfxOpToSkiaOp(CompositionOp op)
{
switch (op)
{
case CompositionOp::OP_OVER:
return SkXfermode::kSrcOver_Mode;
return SkBlendMode::kSrcOver;
case CompositionOp::OP_ADD:
return SkXfermode::kPlus_Mode;
return SkBlendMode::kPlus;
case CompositionOp::OP_ATOP:
return SkXfermode::kSrcATop_Mode;
return SkBlendMode::kSrcATop;
case CompositionOp::OP_OUT:
return SkXfermode::kSrcOut_Mode;
return SkBlendMode::kSrcOut;
case CompositionOp::OP_IN:
return SkXfermode::kSrcIn_Mode;
return SkBlendMode::kSrcIn;
case CompositionOp::OP_SOURCE:
return SkXfermode::kSrc_Mode;
return SkBlendMode::kSrc;
case CompositionOp::OP_DEST_IN:
return SkXfermode::kDstIn_Mode;
return SkBlendMode::kDstIn;
case CompositionOp::OP_DEST_OUT:
return SkXfermode::kDstOut_Mode;
return SkBlendMode::kDstOut;
case CompositionOp::OP_DEST_OVER:
return SkXfermode::kDstOver_Mode;
return SkBlendMode::kDstOver;
case CompositionOp::OP_DEST_ATOP:
return SkXfermode::kDstATop_Mode;
return SkBlendMode::kDstATop;
case CompositionOp::OP_XOR:
return SkXfermode::kXor_Mode;
return SkBlendMode::kXor;
case CompositionOp::OP_MULTIPLY:
return SkXfermode::kMultiply_Mode;
return SkBlendMode::kMultiply;
case CompositionOp::OP_SCREEN:
return SkXfermode::kScreen_Mode;
return SkBlendMode::kScreen;
case CompositionOp::OP_OVERLAY:
return SkXfermode::kOverlay_Mode;
return SkBlendMode::kOverlay;
case CompositionOp::OP_DARKEN:
return SkXfermode::kDarken_Mode;
return SkBlendMode::kDarken;
case CompositionOp::OP_LIGHTEN:
return SkXfermode::kLighten_Mode;
return SkBlendMode::kLighten;
case CompositionOp::OP_COLOR_DODGE:
return SkXfermode::kColorDodge_Mode;
return SkBlendMode::kColorDodge;
case CompositionOp::OP_COLOR_BURN:
return SkXfermode::kColorBurn_Mode;
return SkBlendMode::kColorBurn;
case CompositionOp::OP_HARD_LIGHT:
return SkXfermode::kHardLight_Mode;
return SkBlendMode::kHardLight;
case CompositionOp::OP_SOFT_LIGHT:
return SkXfermode::kSoftLight_Mode;
return SkBlendMode::kSoftLight;
case CompositionOp::OP_DIFFERENCE:
return SkXfermode::kDifference_Mode;
return SkBlendMode::kDifference;
case CompositionOp::OP_EXCLUSION:
return SkXfermode::kExclusion_Mode;
return SkBlendMode::kExclusion;
case CompositionOp::OP_HUE:
return SkXfermode::kHue_Mode;
return SkBlendMode::kHue;
case CompositionOp::OP_SATURATION:
return SkXfermode::kSaturation_Mode;
return SkBlendMode::kSaturation;
case CompositionOp::OP_COLOR:
return SkXfermode::kColor_Mode;
return SkBlendMode::kColor;
case CompositionOp::OP_LUMINOSITY:
return SkXfermode::kLuminosity_Mode;
return SkBlendMode::kLuminosity;
default:
return SkXfermode::kSrcOver_Mode;
return SkBlendMode::kSrcOver;
}
}

Просмотреть файл

@ -1,83 +0,0 @@
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifndef MOZILLA_GFX_REFPTRSKIA_H_
#define MOZILLA_GFX_REFPTRSKIA_H_
namespace mozilla {
namespace gfx {
// The following class was imported from Skia, which is under the
// following licence:
//
// Copyright (c) 2011 Google Inc. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
template <typename T> class RefPtrSkia {
public:
RefPtrSkia() : fObj(NULL) {}
explicit RefPtrSkia(T* obj) : fObj(obj) { SkSafeRef(fObj); }
RefPtrSkia(const RefPtrSkia& o) : fObj(o.fObj) { SkSafeRef(fObj); }
~RefPtrSkia() { SkSafeUnref(fObj); }
RefPtrSkia& operator=(const RefPtrSkia& rp) {
SkRefCnt_SafeAssign(fObj, rp.fObj);
return *this;
}
RefPtrSkia& operator=(T* obj) {
SkRefCnt_SafeAssign(fObj, obj);
return *this;
}
T* get() const { return fObj; }
T& operator*() const { return *fObj; }
T* operator->() const { return fObj; }
RefPtrSkia& adopt(T* obj) {
SkSafeUnref(fObj);
fObj = obj;
return *this;
}
typedef T* RefPtrSkia::*unspecified_bool_type;
operator unspecified_bool_type() const {
return fObj ? &RefPtrSkia::fObj : NULL;
}
private:
T* fObj;
};
// End of code imported from Skia.
} // namespace gfx
} // namespace mozilla
#endif /* MOZILLA_GFX_REFPTRSKIA_H_ */

Просмотреть файл

@ -80,7 +80,7 @@ ScaledFontBase::GetSkiaPathForGlyphs(const GlyphBuffer &aBuffer)
MOZ_ASSERT(typeFace);
SkPaint paint;
paint.setTypeface(typeFace);
paint.setTypeface(sk_ref_sp(typeFace));
paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
paint.setTextSize(SkFloatToScalar(mSize));

Просмотреть файл

@ -7,6 +7,7 @@
#define MOZILLA_GFX_SCALEDFONTWIN_H_
#include "ScaledFontBase.h"
#include <windows.h>
namespace mozilla {
namespace gfx {

Просмотреть файл

@ -6,27 +6,22 @@
#include "Logging.h"
#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 "skia/include/gpu/SkGrPixelRef.h"
#endif
#include "skia/include/core/SkData.h"
#include "mozilla/CheckedInt.h"
namespace mozilla {
namespace gfx {
SourceSurfaceSkia::SourceSurfaceSkia()
: mDrawTarget(nullptr), mLocked(false)
: mDrawTarget(nullptr)
{
}
SourceSurfaceSkia::~SourceSurfaceSkia()
{
MaybeUnlock();
if (mDrawTarget) {
mDrawTarget->SnapshotDestroyed();
mDrawTarget = nullptr;
@ -45,109 +40,112 @@ SourceSurfaceSkia::GetFormat() const
return mFormat;
}
bool
SourceSurfaceSkia::InitFromCanvas(SkCanvas* aCanvas,
SurfaceFormat aFormat,
DrawTargetSkia* aOwner)
{
mBitmap = aCanvas->getDevice()->accessBitmap(false);
mFormat = aFormat;
mSize = IntSize(mBitmap.width(), mBitmap.height());
mStride = mBitmap.rowBytes();
mDrawTarget = aOwner;
return true;
}
bool
SourceSurfaceSkia::InitFromData(unsigned char* aData,
const IntSize &aSize,
int32_t aStride,
SurfaceFormat aFormat)
{
SkBitmap temp;
temp.setInfo(MakeSkiaImageInfo(aSize, aFormat), aStride);
temp.setPixels(aData);
if (!temp.copyTo(&mBitmap)) {
SkPixmap pixmap(MakeSkiaImageInfo(aSize, aFormat), aData, aStride);
mImage = SkImage::MakeRasterCopy(pixmap);
if (!mImage) {
return false;
}
mSize = aSize;
mFormat = aFormat;
mStride = mBitmap.rowBytes();
mStride = aStride;
return true;
}
void
SourceSurfaceSkia::InitFromBitmap(const SkBitmap& aBitmap)
{
mBitmap = aBitmap;
mSize = IntSize(mBitmap.width(), mBitmap.height());
mFormat = SkiaColorTypeToGfxFormat(mBitmap.colorType(), mBitmap.alphaType());
mStride = mBitmap.rowBytes();
}
#ifdef USE_SKIA_GPU
bool
SourceSurfaceSkia::InitFromGrTexture(GrTexture* aTexture,
const IntSize &aSize,
SurfaceFormat aFormat)
SourceSurfaceSkia::InitFromImage(sk_sp<SkImage> aImage,
SurfaceFormat aFormat,
DrawTargetSkia* aOwner)
{
if (!aTexture) {
if (!aImage) {
return false;
}
// Create a GPU pixelref wrapping the texture.
SkImageInfo imgInfo = MakeSkiaImageInfo(aSize, aFormat);
mBitmap.setInfo(imgInfo);
mBitmap.setPixelRef(new SkGrPixelRef(imgInfo, aTexture))->unref();
mSize = IntSize(aImage->width(), aImage->height());
// For the raster image case, we want to use the format and stride
// information that the underlying raster image is using, which is
// reliable.
// For the GPU case (for which peekPixels is false), we can't easily
// figure this information out. It is better to report the originally
// intended format and stride that we will convert to if this GPU
// image is ever read back into a raster image.
SkPixmap pixmap;
if (aImage->peekPixels(&pixmap)) {
mFormat =
aFormat != SurfaceFormat::UNKNOWN ?
aFormat :
SkiaColorTypeToGfxFormat(pixmap.colorType(), pixmap.alphaType());
mStride = pixmap.rowBytes();
} else if (aFormat != SurfaceFormat::UNKNOWN) {
mFormat = aFormat;
SkImageInfo info = MakeSkiaImageInfo(mSize, mFormat);
mStride = SkAlign4(info.minRowBytes());
} else {
return false;
}
mImage = aImage;
if (aOwner) {
mDrawTarget = aOwner;
}
mSize = aSize;
mFormat = aFormat;
mStride = mBitmap.rowBytes();
return true;
}
#endif
unsigned char*
uint8_t*
SourceSurfaceSkia::GetData()
{
if (!mLocked) {
mBitmap.lockPixels();
mLocked = true;
#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 (!raster) {
gfxCriticalError() << "Failed making Skia raster image for GPU surface";
}
mImage = raster;
}
unsigned char *pixels = (unsigned char *)mBitmap.getPixels();
return pixels;
#endif
SkPixmap pixmap;
if (!mImage->peekPixels(&pixmap)) {
gfxCriticalError() << "Failed accessing pixels for Skia raster image";
}
return reinterpret_cast<uint8_t*>(pixmap.writable_addr());
}
void
SourceSurfaceSkia::DrawTargetWillChange()
{
if (mDrawTarget) {
MaybeUnlock();
mDrawTarget = nullptr;
// First try a deep copy to avoid a readback from the GPU.
// If that fails, try the CPU copy.
if (!mBitmap.deepCopyTo(&mBitmap) &&
!mBitmap.copyTo(&mBitmap)) {
mBitmap.reset();
// Raster snapshots do not use Skia's internal copy-on-write mechanism,
// so we need to do an explicit copy here.
// GPU snapshots, for which peekPixels is false, will already be dealt
// with automatically via the internal copy-on-write mechanism, so we
// don't need to do anything for them here.
SkPixmap pixmap;
if (mImage->peekPixels(&pixmap)) {
mImage = SkImage::MakeRasterCopy(pixmap);
if (!mImage) {
gfxCriticalError() << "Failed copying Skia raster snapshot";
}
}
}
}
void
SourceSurfaceSkia::MaybeUnlock()
{
if (mLocked) {
mBitmap.unlockPixels();
mLocked = false;
mDrawTarget = nullptr;
}
}

Просмотреть файл

@ -9,7 +9,7 @@
#include "2D.h"
#include <vector>
#include "skia/include/core/SkCanvas.h"
#include "skia/include/core/SkBitmap.h"
#include "skia/include/core/SkImage.h"
namespace mozilla {
@ -28,30 +28,18 @@ public:
virtual IntSize GetSize() const;
virtual SurfaceFormat GetFormat() const;
SkBitmap& GetBitmap() { return mBitmap; }
sk_sp<SkImage>& GetImage() { return mImage; }
bool InitFromData(unsigned char* aData,
const IntSize &aSize,
int32_t aStride,
SurfaceFormat aFormat);
bool InitFromCanvas(SkCanvas* aCanvas,
SurfaceFormat aFormat,
DrawTargetSkia* aOwner);
bool InitFromImage(sk_sp<SkImage> aImage,
SurfaceFormat aFormat = SurfaceFormat::UNKNOWN,
DrawTargetSkia* aOwner = nullptr);
void InitFromBitmap(const SkBitmap& aBitmap);
#ifdef USE_SKIA_GPU
/**
* NOTE: While wrapping a Texture for SkiaGL, the texture *must* be created
* with the same GLcontext of DrawTargetSkia
*/
bool InitFromGrTexture(GrTexture* aTexture,
const IntSize &aSize,
SurfaceFormat aFormat);
#endif
virtual unsigned char *GetData();
virtual uint8_t* GetData();
virtual int32_t Stride() { return mStride; }
@ -59,14 +47,12 @@ private:
friend class DrawTargetSkia;
void DrawTargetWillChange();
void MaybeUnlock();
SkBitmap mBitmap;
sk_sp<SkImage> mImage;
SurfaceFormat mFormat;
IntSize mSize;
int32_t mStride;
RefPtr<DrawTargetSkia> mDrawTarget;
bool mLocked;
};
} // namespace gfx

Просмотреть файл

@ -39,7 +39,7 @@
#include "skia/include/core/SkColorPriv.h"
#include "skia/include/core/SkBitmap.h"
#include "skia/include/core/SkRect.h"
#include "skia/include/core/SkFontHost.h"
#include "skia/include/core/SkFontLCDConfig.h"
namespace skia {
@ -205,21 +205,21 @@ SkBitmap ImageOperations::ResizeSubpixel(const SkBitmap& source,
int dest_width, int dest_height,
const SkIRect& dest_subset) {
// Currently only works on Linux/BSD because these are the only platforms
// where SkFontHost::GetSubpixelOrder is defined.
// where SkFontLCDConfig::GetSubpixelOrder is defined.
#if defined(XP_UNIX)
// Understand the display.
const SkFontHost::LCDOrder order = SkFontHost::GetSubpixelOrder();
const SkFontHost::LCDOrientation orientation =
SkFontHost::GetSubpixelOrientation();
const SkFontLCDConfig::LCDOrder order = SkFontLCDConfig::GetSubpixelOrder();
const SkFontLCDConfig::LCDOrientation orientation =
SkFontLCDConfig::GetSubpixelOrientation();
// Decide on which dimension, if any, to deploy subpixel rendering.
int w = 1;
int h = 1;
switch (orientation) {
case SkFontHost::kHorizontal_LCDOrientation:
case SkFontLCDConfig::kHorizontal_LCDOrientation:
w = dest_width < source.width() ? 3 : 1;
break;
case SkFontHost::kVertical_LCDOrientation:
case SkFontLCDConfig::kVertical_LCDOrientation:
h = dest_height < source.height() ? 3 : 1;
break;
}
@ -260,15 +260,15 @@ SkBitmap ImageOperations::ResizeSubpixel(const SkBitmap& source,
for (int x = 0; x < dest_subset.width(); x++, src += w, dst++) {
uint8_t r = 0, g = 0, b = 0, a = 0;
switch (order) {
case SkFontHost::kRGB_LCDOrder:
case SkFontLCDConfig::kRGB_LCDOrder:
switch (orientation) {
case SkFontHost::kHorizontal_LCDOrientation:
case SkFontLCDConfig::kHorizontal_LCDOrientation:
r = SkGetPackedR32(src[0]);
g = SkGetPackedG32(src[1]);
b = SkGetPackedB32(src[2]);
a = SkGetPackedA32(src[1]);
break;
case SkFontHost::kVertical_LCDOrientation:
case SkFontLCDConfig::kVertical_LCDOrientation:
r = SkGetPackedR32(src[0 * row_words]);
g = SkGetPackedG32(src[1 * row_words]);
b = SkGetPackedB32(src[2 * row_words]);
@ -276,15 +276,15 @@ SkBitmap ImageOperations::ResizeSubpixel(const SkBitmap& source,
break;
}
break;
case SkFontHost::kBGR_LCDOrder:
case SkFontLCDConfig::kBGR_LCDOrder:
switch (orientation) {
case SkFontHost::kHorizontal_LCDOrientation:
case SkFontLCDConfig::kHorizontal_LCDOrientation:
b = SkGetPackedB32(src[0]);
g = SkGetPackedG32(src[1]);
r = SkGetPackedR32(src[2]);
a = SkGetPackedA32(src[1]);
break;
case SkFontHost::kVertical_LCDOrientation:
case SkFontLCDConfig::kVertical_LCDOrientation:
b = SkGetPackedB32(src[0 * row_words]);
g = SkGetPackedG32(src[1 * row_words]);
r = SkGetPackedR32(src[2 * row_words]);
@ -292,7 +292,7 @@ SkBitmap ImageOperations::ResizeSubpixel(const SkBitmap& source,
break;
}
break;
case SkFontHost::kNONE_LCDOrder:
case SkFontLCDConfig::kNONE_LCDOrder:
break;
}
// Premultiplied alpha is very fragile.

Просмотреть файл

@ -111,7 +111,6 @@ if CONFIG['MOZ_ENABLE_SKIA']:
SOURCES['SourceSurfaceSkia.cpp'].flags += ['-Wno-implicit-fallthrough']
EXPORTS.mozilla.gfx += [
'HelpersSkia.h',
'RefPtrSkia.h',
]
# Are we targeting x86 or x64? If so, build SSE2 files.
@ -217,3 +216,15 @@ if CONFIG['MOZ_WIDGET_TOOLKIT'] in ('android', 'gtk2', 'gtk3', 'gonk'):
CXXFLAGS += CONFIG['CAIRO_FT_CFLAGS']
LOCAL_INCLUDES += CONFIG['SKIA_INCLUDES']
if CONFIG['MOZ_ENABLE_SKIA']:
LOCAL_INCLUDES += [
'/gfx/skia/skia/include/private',
'/gfx/skia/skia/src/core',
'/gfx/skia/skia/src/image',
]
if CONFIG['MOZ_ENABLE_SKIA_GPU']:
LOCAL_INCLUDES += [
'/gfx/skia/skia/src/gpu',
]

Просмотреть файл

@ -196,6 +196,7 @@ static GrGLInterface* CreateGrGLInterfaceFromGLContext(GLContext* context)
i->fFunctions.fDisableVertexAttribArray = WrapGL(context, &GLContext::fDisableVertexAttribArray);
i->fFunctions.fDrawArrays = WrapGL(context, &GLContext::fDrawArrays);
i->fFunctions.fDrawElements = WrapGL(context, &GLContext::fDrawElements);
i->fFunctions.fDrawRangeElements = WrapGL(context, &GLContext::fDrawRangeElements);
i->fFunctions.fEnable = WrapGL(context, &GLContext::fEnable);
i->fFunctions.fEnableVertexAttribArray = WrapGL(context, &GLContext::fEnableVertexAttribArray);
i->fFunctions.fFinish = WrapGL(context, &GLContext::fFinish);
@ -305,8 +306,8 @@ static GrGLInterface* CreateGrGLInterfaceFromGLContext(GLContext* context)
SkiaGLGlue::SkiaGLGlue(GLContext* context)
: mGLContext(context)
{
mGrGLInterface.adopt(CreateGrGLInterfaceFromGLContext(mGLContext));
mGrContext.adopt(GrContext::Create(kOpenGL_GrBackend, (GrBackendContext)mGrGLInterface.get()));
mGrGLInterface.reset(CreateGrGLInterfaceFromGLContext(mGLContext));
mGrContext.reset(GrContext::Create(kOpenGL_GrBackend, (GrBackendContext)mGrGLInterface.get()));
}
SkiaGLGlue::~SkiaGLGlue()

Просмотреть файл

@ -8,7 +8,7 @@
#ifdef USE_SKIA_GPU
#include "mozilla/gfx/RefPtrSkia.h"
#include "skia/include/core/SkRefCnt.h"
#include "mozilla/RefPtr.h"
struct GrGLInterface;
@ -32,8 +32,8 @@ protected:
private:
RefPtr<GLContext> mGLContext;
gfx::RefPtrSkia<GrGLInterface> mGrGLInterface;
gfx::RefPtrSkia<GrContext> mGrContext;
sk_sp<GrGLInterface> mGrGLInterface;
sk_sp<GrContext> mGrContext;
};
} // namespace gl

Просмотреть файл

@ -12,9 +12,7 @@
#include "mozilla/TimeStamp.h" // for TimeStamp, TimeDuration
#include "mozilla/Sprintf.h"
#include "SkColor.h"
#include "mozilla/gfx/HelpersSkia.h"
#include "skia/include/core/SkBitmapDevice.h"
#include "PaintCounter.h"
namespace mozilla {
@ -31,12 +29,10 @@ PaintCounter::PaintCounter()
mSurface = Factory::CreateDataSourceSurface(mRect.Size(), mFormat);
mStride = mSurface->Stride();
SkBitmap bitmap;
bitmap.setInfo(MakeSkiaImageInfo(mRect.Size(), mFormat), mStride);
bitmap.setPixels(mSurface->GetData());
bitmap.eraseColor(SK_ColorWHITE);
mCanvas.adopt(new SkCanvas(bitmap));
mCanvas.reset(
SkCanvas::NewRasterDirect(MakeSkiaImageInfo(mRect.Size(), mFormat),
mSurface->GetData(), mStride));
mCanvas->clear(SK_ColorWHITE);
}
PaintCounter::~PaintCounter()

Просмотреть файл

@ -10,7 +10,6 @@
#include "mozilla/RefPtr.h" // for already_AddRefed, RefCounted
#include "mozilla/TimeStamp.h" // for TimeStamp, TimeDuration
#include "skia/include/core/SkCanvas.h"
#include "mozilla/gfx/HelpersSkia.h"
namespace mozilla {
namespace layers {
@ -34,7 +33,7 @@ private:
virtual ~PaintCounter();
SurfaceFormat mFormat;
RefPtrSkia<SkCanvas> mCanvas;
sk_sp<SkCanvas> mCanvas;
IntSize mSize;
int mStride;