2011-11-02 23:55:03 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
2012-05-21 15:12:37 +04:00
|
|
|
* 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/. */
|
2011-11-02 23:55:03 +04:00
|
|
|
|
|
|
|
#include "DrawTargetSkia.h"
|
2014-07-04 01:06:48 +04:00
|
|
|
#include "SourceSurfaceCairo.h"
|
2011-11-02 23:55:03 +04:00
|
|
|
#include "SourceSurfaceSkia.h"
|
2012-01-09 22:54:44 +04:00
|
|
|
#include "ScaledFontBase.h"
|
2013-06-05 21:48:59 +04:00
|
|
|
#include "ScaledFontCairo.h"
|
2013-12-06 08:01:53 +04:00
|
|
|
#include "skia/SkGpuDevice.h"
|
|
|
|
#include "skia/SkBitmapDevice.h"
|
2013-11-27 15:25:28 +04:00
|
|
|
#include "FilterNodeSoftware.h"
|
2013-03-14 03:29:47 +04:00
|
|
|
|
|
|
|
#ifdef USE_SKIA_GPU
|
2012-12-01 03:57:38 +04:00
|
|
|
#include "skia/SkGpuDevice.h"
|
2013-06-17 06:07:18 +04:00
|
|
|
#include "skia/GrGLInterface.h"
|
2013-03-14 03:29:47 +04:00
|
|
|
#endif
|
|
|
|
|
2011-11-02 23:55:03 +04:00
|
|
|
#include "skia/SkTypeface.h"
|
|
|
|
#include "skia/SkGradientShader.h"
|
|
|
|
#include "skia/SkBlurDrawLooper.h"
|
|
|
|
#include "skia/SkBlurMaskFilter.h"
|
|
|
|
#include "skia/SkColorFilter.h"
|
2014-03-07 01:33:39 +04:00
|
|
|
#include "skia/SkDropShadowImageFilter.h"
|
2011-11-02 23:55:03 +04:00
|
|
|
#include "skia/SkLayerRasterizer.h"
|
|
|
|
#include "skia/SkLayerDrawLooper.h"
|
|
|
|
#include "skia/SkDashPathEffect.h"
|
|
|
|
#include "Logging.h"
|
|
|
|
#include "Tools.h"
|
2013-11-03 20:28:30 +04:00
|
|
|
#include "DataSurfaceHelpers.h"
|
2011-11-02 23:55:03 +04:00
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace gfx {
|
|
|
|
|
|
|
|
class GradientStopsSkia : public GradientStops
|
|
|
|
{
|
|
|
|
public:
|
2014-02-24 17:23:37 +04:00
|
|
|
MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(GradientStopsSkia)
|
2012-01-19 08:48:33 +04:00
|
|
|
GradientStopsSkia(const std::vector<GradientStop>& aStops, uint32_t aNumStops, ExtendMode aExtendMode)
|
2011-11-02 23:55:03 +04:00
|
|
|
: mCount(aNumStops)
|
2012-01-19 08:48:33 +04:00
|
|
|
, mExtendMode(aExtendMode)
|
2011-11-02 23:55:03 +04:00
|
|
|
{
|
|
|
|
if (mCount == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Skia gradients always require a stop at 0.0 and 1.0, insert these if
|
|
|
|
// we don't have them.
|
|
|
|
uint32_t shift = 0;
|
|
|
|
if (aStops[0].offset != 0) {
|
|
|
|
mCount++;
|
|
|
|
shift = 1;
|
|
|
|
}
|
|
|
|
if (aStops[aNumStops-1].offset != 1) {
|
|
|
|
mCount++;
|
|
|
|
}
|
|
|
|
mColors.resize(mCount);
|
|
|
|
mPositions.resize(mCount);
|
|
|
|
if (aStops[0].offset != 0) {
|
|
|
|
mColors[0] = ColorToSkColor(aStops[0].color, 1.0);
|
|
|
|
mPositions[0] = 0;
|
|
|
|
}
|
|
|
|
for (uint32_t i = 0; i < aNumStops; i++) {
|
|
|
|
mColors[i + shift] = ColorToSkColor(aStops[i].color, 1.0);
|
|
|
|
mPositions[i + shift] = SkFloatToScalar(aStops[i].offset);
|
|
|
|
}
|
|
|
|
if (aStops[aNumStops-1].offset != 1) {
|
|
|
|
mColors[mCount-1] = ColorToSkColor(aStops[aNumStops-1].color, 1.0);
|
|
|
|
mPositions[mCount-1] = SK_Scalar1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-10 23:06:16 +04:00
|
|
|
BackendType GetBackendType() const { return BackendType::SKIA; }
|
2011-11-02 23:55:03 +04:00
|
|
|
|
|
|
|
std::vector<SkColor> mColors;
|
|
|
|
std::vector<SkScalar> mPositions;
|
|
|
|
int mCount;
|
2012-01-19 08:48:33 +04:00
|
|
|
ExtendMode mExtendMode;
|
2011-11-02 23:55:03 +04:00
|
|
|
};
|
|
|
|
|
2014-01-13 04:29:48 +04:00
|
|
|
/**
|
|
|
|
* When constructing a temporary SkBitmap via GetBitmapForSurface, we may also
|
|
|
|
* have to construct a temporary DataSourceSurface, which must live as long as
|
|
|
|
* the SkBitmap. So we return a pair of the SkBitmap and the (optional)
|
|
|
|
* temporary surface.
|
|
|
|
*/
|
|
|
|
struct TempBitmap
|
|
|
|
{
|
|
|
|
SkBitmap mBitmap;
|
|
|
|
RefPtr<SourceSurface> mTmpSurface;
|
|
|
|
};
|
|
|
|
|
|
|
|
static TempBitmap
|
|
|
|
GetBitmapForSurface(SourceSurface* aSurface)
|
|
|
|
{
|
|
|
|
TempBitmap result;
|
|
|
|
|
|
|
|
if (aSurface->GetType() == SurfaceType::SKIA) {
|
|
|
|
result.mBitmap = static_cast<SourceSurfaceSkia*>(aSurface)->GetBitmap();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<DataSourceSurface> surf = aSurface->GetDataSurface();
|
|
|
|
if (!surf) {
|
2013-11-27 15:25:27 +04:00
|
|
|
MOZ_CRASH("Non-skia SourceSurfaces need to be DataSourceSurfaces");
|
|
|
|
}
|
2014-01-13 04:29:48 +04:00
|
|
|
|
2014-07-28 04:47:43 +04:00
|
|
|
SkAlphaType alphaType = (surf->GetFormat() == SurfaceFormat::B8G8R8X8) ?
|
|
|
|
kOpaque_SkAlphaType : kPremul_SkAlphaType;
|
|
|
|
|
|
|
|
SkImageInfo info = SkImageInfo::Make(surf->GetSize().width,
|
|
|
|
surf->GetSize().height,
|
|
|
|
GfxFormatToSkiaColorType(surf->GetFormat()),
|
|
|
|
alphaType);
|
|
|
|
result.mBitmap.setInfo(info, surf->Stride());
|
|
|
|
|
2014-01-13 04:29:48 +04:00
|
|
|
result.mBitmap.setPixels(surf->GetData());
|
|
|
|
result.mTmpSurface = surf.forget();
|
|
|
|
return result;
|
2013-11-27 15:25:27 +04:00
|
|
|
}
|
|
|
|
|
2011-11-02 23:55:03 +04:00
|
|
|
DrawTargetSkia::DrawTargetSkia()
|
2014-10-15 07:55:37 +04:00
|
|
|
:
|
|
|
|
#ifdef USE_SKIA_GPU
|
|
|
|
mTexture(0),
|
|
|
|
#endif
|
|
|
|
mSnapshot(nullptr)
|
2011-11-02 23:55:03 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
DrawTargetSkia::~DrawTargetSkia()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-06-17 17:00:52 +03:00
|
|
|
already_AddRefed<SourceSurface>
|
2011-11-02 23:55:03 +04:00
|
|
|
DrawTargetSkia::Snapshot()
|
|
|
|
{
|
2013-08-02 05:12:16 +04:00
|
|
|
RefPtr<SourceSurfaceSkia> snapshot = mSnapshot;
|
|
|
|
if (!snapshot) {
|
|
|
|
snapshot = new SourceSurfaceSkia();
|
2015-05-26 06:10:25 +03:00
|
|
|
mSnapshot = snapshot;
|
2015-05-26 10:00:19 +03:00
|
|
|
if (!snapshot->InitFromCanvas(mCanvas.get(), mFormat, this))
|
|
|
|
return nullptr;
|
2013-08-02 05:12:16 +04:00
|
|
|
}
|
2013-05-29 20:49:40 +04:00
|
|
|
|
2014-06-13 20:09:23 +04:00
|
|
|
return snapshot.forget();
|
2011-11-02 23:55:03 +04:00
|
|
|
}
|
|
|
|
|
2015-05-01 21:08:04 +03:00
|
|
|
bool
|
|
|
|
DrawTargetSkia::LockBits(uint8_t** aData, IntSize* aSize,
|
|
|
|
int32_t* aStride, SurfaceFormat* aFormat)
|
|
|
|
{
|
|
|
|
const SkBitmap &bitmap = mCanvas->getDevice()->accessBitmap(false);
|
|
|
|
if (!bitmap.lockPixelsAreWritable()) {
|
|
|
|
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());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DrawTargetSkia::ReleaseBits(uint8_t* aData)
|
|
|
|
{
|
|
|
|
const SkBitmap &bitmap = mCanvas->getDevice()->accessBitmap(false);
|
|
|
|
MOZ_ASSERT(bitmap.lockPixelsAreWritable());
|
|
|
|
|
|
|
|
bitmap.unlockPixels();
|
|
|
|
bitmap.notifyPixelsChanged();
|
|
|
|
}
|
|
|
|
|
2014-01-13 04:29:48 +04:00
|
|
|
static void
|
|
|
|
SetPaintPattern(SkPaint& aPaint, const Pattern& aPattern, TempBitmap& aTmpBitmap,
|
|
|
|
Float aAlpha = 1.0)
|
2012-01-19 08:48:33 +04:00
|
|
|
{
|
|
|
|
switch (aPattern.GetType()) {
|
2014-01-10 23:06:17 +04:00
|
|
|
case PatternType::COLOR: {
|
2012-01-19 08:48:33 +04:00
|
|
|
Color color = static_cast<const ColorPattern&>(aPattern).mColor;
|
|
|
|
aPaint.setColor(ColorToSkColor(color, aAlpha));
|
|
|
|
break;
|
|
|
|
}
|
2014-01-10 23:06:17 +04:00
|
|
|
case PatternType::LINEAR_GRADIENT: {
|
2012-01-19 08:48:33 +04:00
|
|
|
const LinearGradientPattern& pat = static_cast<const LinearGradientPattern&>(aPattern);
|
|
|
|
GradientStopsSkia *stops = static_cast<GradientStopsSkia*>(pat.mStops.get());
|
|
|
|
SkShader::TileMode mode = ExtendModeToTileMode(stops->mExtendMode);
|
|
|
|
|
|
|
|
if (stops->mCount >= 2) {
|
|
|
|
SkPoint points[2];
|
|
|
|
points[0] = SkPoint::Make(SkFloatToScalar(pat.mBegin.x), SkFloatToScalar(pat.mBegin.y));
|
|
|
|
points[1] = SkPoint::Make(SkFloatToScalar(pat.mEnd.x), SkFloatToScalar(pat.mEnd.y));
|
|
|
|
|
2014-10-08 08:16:14 +04:00
|
|
|
SkShader* shader = SkGradientShader::CreateLinear(points,
|
|
|
|
&stops->mColors.front(),
|
|
|
|
&stops->mPositions.front(),
|
|
|
|
stops->mCount,
|
2012-01-19 08:48:33 +04:00
|
|
|
mode);
|
2012-07-26 04:11:17 +04:00
|
|
|
|
|
|
|
if (shader) {
|
|
|
|
SkMatrix mat;
|
|
|
|
GfxMatrixToSkiaMatrix(pat.mMatrix, mat);
|
2014-07-28 04:47:43 +04:00
|
|
|
SkShader* matrixShader = SkShader::CreateLocalMatrixShader(shader, mat);
|
|
|
|
SkSafeUnref(shader);
|
|
|
|
SkSafeUnref(aPaint.setShader(matrixShader));
|
2012-07-26 04:11:17 +04:00
|
|
|
}
|
|
|
|
|
2012-01-19 08:48:33 +04:00
|
|
|
} else {
|
|
|
|
aPaint.setColor(SkColorSetARGB(0, 0, 0, 0));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2014-01-10 23:06:17 +04:00
|
|
|
case PatternType::RADIAL_GRADIENT: {
|
2012-01-19 08:48:33 +04:00
|
|
|
const RadialGradientPattern& pat = static_cast<const RadialGradientPattern&>(aPattern);
|
|
|
|
GradientStopsSkia *stops = static_cast<GradientStopsSkia*>(pat.mStops.get());
|
|
|
|
SkShader::TileMode mode = ExtendModeToTileMode(stops->mExtendMode);
|
|
|
|
|
|
|
|
if (stops->mCount >= 2) {
|
|
|
|
SkPoint points[2];
|
|
|
|
points[0] = SkPoint::Make(SkFloatToScalar(pat.mCenter1.x), SkFloatToScalar(pat.mCenter1.y));
|
|
|
|
points[1] = SkPoint::Make(SkFloatToScalar(pat.mCenter2.x), SkFloatToScalar(pat.mCenter2.y));
|
|
|
|
|
2014-10-08 08:16:14 +04:00
|
|
|
SkShader* shader = SkGradientShader::CreateTwoPointConical(points[0],
|
2012-09-12 20:37:34 +04:00
|
|
|
SkFloatToScalar(pat.mRadius1),
|
2014-10-08 08:16:14 +04:00
|
|
|
points[1],
|
2012-09-12 20:37:34 +04:00
|
|
|
SkFloatToScalar(pat.mRadius2),
|
2014-10-08 08:16:14 +04:00
|
|
|
&stops->mColors.front(),
|
|
|
|
&stops->mPositions.front(),
|
|
|
|
stops->mCount,
|
2012-09-12 20:37:34 +04:00
|
|
|
mode);
|
2012-07-26 04:11:17 +04:00
|
|
|
if (shader) {
|
|
|
|
SkMatrix mat;
|
|
|
|
GfxMatrixToSkiaMatrix(pat.mMatrix, mat);
|
2014-07-28 04:47:43 +04:00
|
|
|
SkShader* matrixShader = SkShader::CreateLocalMatrixShader(shader, mat);
|
|
|
|
SkSafeUnref(shader);
|
|
|
|
SkSafeUnref(aPaint.setShader(matrixShader));
|
2012-07-26 04:11:17 +04:00
|
|
|
}
|
|
|
|
|
2012-01-19 08:48:33 +04:00
|
|
|
} else {
|
|
|
|
aPaint.setColor(SkColorSetARGB(0, 0, 0, 0));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2014-01-10 23:06:17 +04:00
|
|
|
case PatternType::SURFACE: {
|
2012-01-19 08:48:33 +04:00
|
|
|
const SurfacePattern& pat = static_cast<const SurfacePattern&>(aPattern);
|
2014-01-13 04:29:48 +04:00
|
|
|
aTmpBitmap = GetBitmapForSurface(pat.mSurface);
|
2014-09-12 09:18:22 +04:00
|
|
|
SkBitmap& bitmap = aTmpBitmap.mBitmap;
|
2012-01-19 08:48:33 +04:00
|
|
|
|
|
|
|
SkMatrix mat;
|
|
|
|
GfxMatrixToSkiaMatrix(pat.mMatrix, mat);
|
2014-09-12 09:18:22 +04:00
|
|
|
|
|
|
|
if (!pat.mSamplingRect.IsEmpty()) {
|
|
|
|
SkIRect rect = IntRectToSkIRect(pat.mSamplingRect);
|
|
|
|
bitmap.extractSubset(&bitmap, rect);
|
|
|
|
mat.preTranslate(rect.x(), rect.y());
|
|
|
|
}
|
|
|
|
|
|
|
|
SkShader::TileMode mode = ExtendModeToTileMode(pat.mExtendMode);
|
|
|
|
SkShader* shader = SkShader::CreateBitmapShader(bitmap, mode, mode);
|
2014-07-28 04:47:43 +04:00
|
|
|
SkShader* matrixShader = SkShader::CreateLocalMatrixShader(shader, mat);
|
|
|
|
SkSafeUnref(shader);
|
|
|
|
SkSafeUnref(aPaint.setShader(matrixShader));
|
2014-01-10 23:06:17 +04:00
|
|
|
if (pat.mFilter == Filter::POINT) {
|
2014-03-20 22:07:36 +04:00
|
|
|
aPaint.setFilterLevel(SkPaint::kNone_FilterLevel);
|
2012-01-19 08:48:33 +04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-01 12:59:20 +04:00
|
|
|
static inline Rect
|
|
|
|
GetClipBounds(SkCanvas *aCanvas)
|
|
|
|
{
|
|
|
|
SkRect clipBounds;
|
|
|
|
aCanvas->getClipBounds(&clipBounds);
|
|
|
|
return SkRectToRect(clipBounds);
|
|
|
|
}
|
|
|
|
|
2011-11-02 23:55:03 +04:00
|
|
|
struct AutoPaintSetup {
|
2014-04-01 12:59:20 +04:00
|
|
|
AutoPaintSetup(SkCanvas *aCanvas, const DrawOptions& aOptions, const Pattern& aPattern, const Rect* aMaskBounds = nullptr)
|
2011-11-02 23:55:03 +04:00
|
|
|
: mNeedsRestore(false), mAlpha(1.0)
|
|
|
|
{
|
2014-04-01 12:59:20 +04:00
|
|
|
Init(aCanvas, aOptions, aMaskBounds);
|
2014-01-13 04:29:48 +04:00
|
|
|
SetPaintPattern(mPaint, aPattern, mTmpBitmap, mAlpha);
|
2011-11-02 23:55:03 +04:00
|
|
|
}
|
|
|
|
|
2014-04-01 12:59:20 +04:00
|
|
|
AutoPaintSetup(SkCanvas *aCanvas, const DrawOptions& aOptions, const Rect* aMaskBounds = nullptr)
|
2011-11-02 23:55:03 +04:00
|
|
|
: mNeedsRestore(false), mAlpha(1.0)
|
|
|
|
{
|
2014-04-01 12:59:20 +04:00
|
|
|
Init(aCanvas, aOptions, aMaskBounds);
|
2011-11-02 23:55:03 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
~AutoPaintSetup()
|
|
|
|
{
|
|
|
|
if (mNeedsRestore) {
|
|
|
|
mCanvas->restore();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-01 12:59:20 +04:00
|
|
|
void Init(SkCanvas *aCanvas, const DrawOptions& aOptions, const Rect* aMaskBounds)
|
2011-11-02 23:55:03 +04:00
|
|
|
{
|
|
|
|
mPaint.setXfermodeMode(GfxOpToSkiaOp(aOptions.mCompositionOp));
|
|
|
|
mCanvas = aCanvas;
|
|
|
|
|
|
|
|
//TODO: Can we set greyscale somehow?
|
2014-01-10 23:06:17 +04:00
|
|
|
if (aOptions.mAntialiasMode != AntialiasMode::NONE) {
|
2011-11-02 23:55:03 +04:00
|
|
|
mPaint.setAntiAlias(true);
|
|
|
|
} else {
|
|
|
|
mPaint.setAntiAlias(false);
|
|
|
|
}
|
|
|
|
|
2014-04-01 12:59:20 +04:00
|
|
|
Rect clipBounds = GetClipBounds(aCanvas);
|
|
|
|
bool needsGroup = !IsOperatorBoundByMask(aOptions.mCompositionOp) &&
|
|
|
|
(!aMaskBounds || !aMaskBounds->Contains(clipBounds));
|
|
|
|
|
2011-11-02 23:55:03 +04:00
|
|
|
// TODO: We could skip the temporary for operator_source and just
|
|
|
|
// clear the clip rect. The other operators would be harder
|
|
|
|
// but could be worth it to skip pushing a group.
|
2014-04-01 12:59:20 +04:00
|
|
|
if (needsGroup) {
|
2011-11-02 23:55:03 +04:00
|
|
|
mPaint.setXfermodeMode(SkXfermode::kSrcOver_Mode);
|
|
|
|
SkPaint temp;
|
|
|
|
temp.setXfermodeMode(GfxOpToSkiaOp(aOptions.mCompositionOp));
|
2014-06-18 18:16:30 +04:00
|
|
|
temp.setAlpha(ColorFloatToByte(aOptions.mAlpha));
|
2011-11-02 23:55:03 +04:00
|
|
|
//TODO: Get a rect here
|
2012-08-14 22:06:12 +04:00
|
|
|
mCanvas->saveLayer(nullptr, &temp);
|
2011-11-02 23:55:03 +04:00
|
|
|
mNeedsRestore = true;
|
|
|
|
} else {
|
2014-06-18 18:16:30 +04:00
|
|
|
mPaint.setAlpha(ColorFloatToByte(aOptions.mAlpha));
|
2011-11-02 23:55:03 +04:00
|
|
|
mAlpha = aOptions.mAlpha;
|
|
|
|
}
|
2014-03-20 22:07:36 +04:00
|
|
|
mPaint.setFilterLevel(SkPaint::kLow_FilterLevel);
|
2011-11-02 23:55:03 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Maybe add an operator overload to access this easier?
|
|
|
|
SkPaint mPaint;
|
2014-01-13 04:29:48 +04:00
|
|
|
TempBitmap mTmpBitmap;
|
2011-11-02 23:55:03 +04:00
|
|
|
bool mNeedsRestore;
|
|
|
|
SkCanvas* mCanvas;
|
|
|
|
Float mAlpha;
|
|
|
|
};
|
|
|
|
|
|
|
|
void
|
|
|
|
DrawTargetSkia::Flush()
|
|
|
|
{
|
2012-12-01 03:57:38 +04:00
|
|
|
mCanvas->flush();
|
2011-11-02 23:55:03 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DrawTargetSkia::DrawSurface(SourceSurface *aSurface,
|
|
|
|
const Rect &aDest,
|
|
|
|
const Rect &aSource,
|
|
|
|
const DrawSurfaceOptions &aSurfOptions,
|
|
|
|
const DrawOptions &aOptions)
|
|
|
|
{
|
2014-04-15 16:55:48 +04:00
|
|
|
RefPtr<SourceSurface> dataSurface;
|
|
|
|
|
2014-01-10 22:55:24 +04:00
|
|
|
if (!(aSurface->GetType() == SurfaceType::SKIA || aSurface->GetType() == SurfaceType::DATA)) {
|
2014-04-15 16:55:48 +04:00
|
|
|
dataSurface = aSurface->GetDataSurface();
|
|
|
|
if (!dataSurface) {
|
|
|
|
gfxDebug() << *this << ": DrawSurface() can't draw surface";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
aSurface = dataSurface.get();
|
2013-12-03 17:52:06 +04:00
|
|
|
}
|
|
|
|
|
2011-11-02 23:55:03 +04:00
|
|
|
if (aSource.IsEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-11-02 23:55:03 +04:00
|
|
|
MarkChanged();
|
|
|
|
|
2011-11-02 23:55:03 +04:00
|
|
|
SkRect destRect = RectToSkRect(aDest);
|
|
|
|
SkRect sourceRect = RectToSkRect(aSource);
|
|
|
|
|
2014-01-13 04:29:48 +04:00
|
|
|
TempBitmap bitmap = GetBitmapForSurface(aSurface);
|
2014-10-08 08:16:14 +04:00
|
|
|
|
2014-04-01 12:59:20 +04:00
|
|
|
AutoPaintSetup paint(mCanvas.get(), aOptions, &aDest);
|
2014-01-10 23:06:17 +04:00
|
|
|
if (aSurfOptions.mFilter == Filter::POINT) {
|
2014-03-20 22:07:36 +04:00
|
|
|
paint.mPaint.setFilterLevel(SkPaint::kNone_FilterLevel);
|
2011-11-18 08:00:38 +04:00
|
|
|
}
|
2012-11-16 04:57:40 +04:00
|
|
|
|
2014-01-15 11:16:39 +04:00
|
|
|
mCanvas->drawBitmapRectToRect(bitmap.mBitmap, &sourceRect, destRect, &paint.mPaint);
|
2011-11-02 23:55:03 +04:00
|
|
|
}
|
|
|
|
|
2014-06-27 13:17:49 +04:00
|
|
|
DrawTargetType
|
|
|
|
DrawTargetSkia::GetType() const
|
|
|
|
{
|
|
|
|
#ifdef USE_SKIA_GPU
|
|
|
|
if (mGrContext) {
|
|
|
|
return DrawTargetType::HARDWARE_RASTER;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return DrawTargetType::SOFTWARE_RASTER;
|
|
|
|
}
|
|
|
|
|
2013-11-27 15:25:28 +04:00
|
|
|
void
|
|
|
|
DrawTargetSkia::DrawFilter(FilterNode *aNode,
|
|
|
|
const Rect &aSourceRect,
|
|
|
|
const Point &aDestPoint,
|
|
|
|
const DrawOptions &aOptions)
|
|
|
|
{
|
|
|
|
FilterNodeSoftware* filter = static_cast<FilterNodeSoftware*>(aNode);
|
|
|
|
filter->Draw(this, aSourceRect, aDestPoint, aOptions);
|
|
|
|
}
|
|
|
|
|
2011-11-02 23:55:03 +04:00
|
|
|
void
|
|
|
|
DrawTargetSkia::DrawSurfaceWithShadow(SourceSurface *aSurface,
|
|
|
|
const Point &aDest,
|
|
|
|
const Color &aColor,
|
|
|
|
const Point &aOffset,
|
|
|
|
Float aSigma,
|
|
|
|
CompositionOp aOperator)
|
|
|
|
{
|
2014-03-07 01:33:39 +04:00
|
|
|
if (!(aSurface->GetType() == SurfaceType::SKIA || aSurface->GetType() == SurfaceType::DATA)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-11-02 23:55:03 +04:00
|
|
|
MarkChanged();
|
2014-03-07 01:33:39 +04:00
|
|
|
|
2014-07-28 04:51:09 +04:00
|
|
|
mCanvas->save();
|
2011-11-02 23:55:03 +04:00
|
|
|
mCanvas->resetMatrix();
|
|
|
|
|
2014-01-13 04:29:48 +04:00
|
|
|
TempBitmap bitmap = GetBitmapForSurface(aSurface);
|
2014-03-07 01:33:39 +04:00
|
|
|
|
2011-11-02 23:55:03 +04:00
|
|
|
SkPaint paint;
|
2014-03-07 01:33:39 +04:00
|
|
|
|
2014-03-20 22:07:36 +04:00
|
|
|
SkImageFilter* filter = SkDropShadowImageFilter::Create(aOffset.x, aOffset.y,
|
2014-07-28 04:51:32 +04:00
|
|
|
aSigma, aSigma,
|
|
|
|
ColorToSkColor(aColor, 1.0));
|
2014-03-07 01:33:39 +04:00
|
|
|
|
|
|
|
paint.setImageFilter(filter);
|
2011-11-02 23:55:03 +04:00
|
|
|
paint.setXfermodeMode(GfxOpToSkiaOp(aOperator));
|
|
|
|
|
2014-03-07 01:33:39 +04:00
|
|
|
mCanvas->drawBitmap(bitmap.mBitmap, aDest.x, aDest.y, &paint);
|
2011-11-02 23:55:03 +04:00
|
|
|
mCanvas->restore();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DrawTargetSkia::FillRect(const Rect &aRect,
|
|
|
|
const Pattern &aPattern,
|
|
|
|
const DrawOptions &aOptions)
|
|
|
|
{
|
2011-11-02 23:55:03 +04:00
|
|
|
MarkChanged();
|
2011-11-02 23:55:03 +04:00
|
|
|
SkRect rect = RectToSkRect(aRect);
|
2014-04-01 12:59:20 +04:00
|
|
|
AutoPaintSetup paint(mCanvas.get(), aOptions, aPattern, &aRect);
|
2011-11-02 23:55:03 +04:00
|
|
|
|
|
|
|
mCanvas->drawRect(rect, paint.mPaint);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DrawTargetSkia::Stroke(const Path *aPath,
|
|
|
|
const Pattern &aPattern,
|
|
|
|
const StrokeOptions &aStrokeOptions,
|
|
|
|
const DrawOptions &aOptions)
|
|
|
|
{
|
2011-11-02 23:55:03 +04:00
|
|
|
MarkChanged();
|
2012-04-26 02:04:35 +04:00
|
|
|
MOZ_ASSERT(aPath, "Null path");
|
2014-01-10 23:06:16 +04:00
|
|
|
if (aPath->GetBackendType() != BackendType::SKIA) {
|
2011-11-02 23:55:03 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const PathSkia *skiaPath = static_cast<const PathSkia*>(aPath);
|
|
|
|
|
|
|
|
|
|
|
|
AutoPaintSetup paint(mCanvas.get(), aOptions, aPattern);
|
2011-11-18 08:00:38 +04:00
|
|
|
if (!StrokeOptionsToPaint(paint.mPaint, aStrokeOptions)) {
|
|
|
|
return;
|
|
|
|
}
|
2011-11-02 23:55:03 +04:00
|
|
|
|
|
|
|
mCanvas->drawPath(skiaPath->GetPath(), paint.mPaint);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DrawTargetSkia::StrokeRect(const Rect &aRect,
|
|
|
|
const Pattern &aPattern,
|
|
|
|
const StrokeOptions &aStrokeOptions,
|
|
|
|
const DrawOptions &aOptions)
|
|
|
|
{
|
2011-11-02 23:55:03 +04:00
|
|
|
MarkChanged();
|
2011-11-02 23:55:03 +04:00
|
|
|
AutoPaintSetup paint(mCanvas.get(), aOptions, aPattern);
|
2011-11-18 08:00:38 +04:00
|
|
|
if (!StrokeOptionsToPaint(paint.mPaint, aStrokeOptions)) {
|
|
|
|
return;
|
|
|
|
}
|
2011-11-02 23:55:03 +04:00
|
|
|
|
|
|
|
mCanvas->drawRect(RectToSkRect(aRect), paint.mPaint);
|
|
|
|
}
|
|
|
|
|
2014-10-08 08:16:14 +04:00
|
|
|
void
|
2011-11-02 23:55:03 +04:00
|
|
|
DrawTargetSkia::StrokeLine(const Point &aStart,
|
|
|
|
const Point &aEnd,
|
|
|
|
const Pattern &aPattern,
|
|
|
|
const StrokeOptions &aStrokeOptions,
|
|
|
|
const DrawOptions &aOptions)
|
|
|
|
{
|
2011-11-02 23:55:03 +04:00
|
|
|
MarkChanged();
|
2011-11-02 23:55:03 +04:00
|
|
|
AutoPaintSetup paint(mCanvas.get(), aOptions, aPattern);
|
2011-11-18 08:00:38 +04:00
|
|
|
if (!StrokeOptionsToPaint(paint.mPaint, aStrokeOptions)) {
|
|
|
|
return;
|
|
|
|
}
|
2011-11-02 23:55:03 +04:00
|
|
|
|
2014-10-08 08:16:14 +04:00
|
|
|
mCanvas->drawLine(SkFloatToScalar(aStart.x), SkFloatToScalar(aStart.y),
|
|
|
|
SkFloatToScalar(aEnd.x), SkFloatToScalar(aEnd.y),
|
2011-11-02 23:55:03 +04:00
|
|
|
paint.mPaint);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DrawTargetSkia::Fill(const Path *aPath,
|
|
|
|
const Pattern &aPattern,
|
|
|
|
const DrawOptions &aOptions)
|
|
|
|
{
|
2011-11-02 23:55:03 +04:00
|
|
|
MarkChanged();
|
2014-01-10 23:06:16 +04:00
|
|
|
if (aPath->GetBackendType() != BackendType::SKIA) {
|
2011-11-02 23:55:03 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const PathSkia *skiaPath = static_cast<const PathSkia*>(aPath);
|
|
|
|
|
|
|
|
AutoPaintSetup paint(mCanvas.get(), aOptions, aPattern);
|
|
|
|
|
|
|
|
mCanvas->drawPath(skiaPath->GetPath(), paint.mPaint);
|
|
|
|
}
|
|
|
|
|
2015-05-04 20:23:54 +03:00
|
|
|
bool
|
|
|
|
DrawTargetSkia::ShouldLCDRenderText(FontType aFontType, AntialiasMode aAntialiasMode)
|
|
|
|
{
|
|
|
|
// For non-opaque surfaces, only allow subpixel AA if explicitly permitted.
|
|
|
|
if (!IsOpaque(mFormat) && !mPermitSubpixelAA) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aAntialiasMode == AntialiasMode::DEFAULT) {
|
|
|
|
switch (aFontType) {
|
|
|
|
case FontType::MAC:
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
// TODO: Figure out what to do for the other platforms.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (aAntialiasMode == AntialiasMode::SUBPIXEL);
|
|
|
|
}
|
|
|
|
|
2011-11-02 23:55:03 +04:00
|
|
|
void
|
|
|
|
DrawTargetSkia::FillGlyphs(ScaledFont *aFont,
|
|
|
|
const GlyphBuffer &aBuffer,
|
|
|
|
const Pattern &aPattern,
|
2012-03-19 23:20:17 +04:00
|
|
|
const DrawOptions &aOptions,
|
2013-06-05 21:48:59 +04:00
|
|
|
const GlyphRenderingOptions *aRenderingOptions)
|
2011-11-02 23:55:03 +04:00
|
|
|
{
|
2014-01-10 23:06:16 +04:00
|
|
|
if (aFont->GetType() != FontType::MAC &&
|
|
|
|
aFont->GetType() != FontType::SKIA &&
|
|
|
|
aFont->GetType() != FontType::GDI) {
|
2011-11-02 23:55:03 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-11-02 23:55:03 +04:00
|
|
|
MarkChanged();
|
|
|
|
|
2012-01-09 22:54:44 +04:00
|
|
|
ScaledFontBase* skiaFont = static_cast<ScaledFontBase*>(aFont);
|
2011-11-02 23:55:03 +04:00
|
|
|
|
|
|
|
AutoPaintSetup paint(mCanvas.get(), aOptions, aPattern);
|
2012-01-09 22:54:44 +04:00
|
|
|
paint.mPaint.setTypeface(skiaFont->GetSkTypeface());
|
2011-11-02 23:55:03 +04:00
|
|
|
paint.mPaint.setTextSize(SkFloatToScalar(skiaFont->mSize));
|
|
|
|
paint.mPaint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
|
2013-06-05 21:48:59 +04:00
|
|
|
|
2015-05-04 20:23:54 +03:00
|
|
|
bool shouldLCDRenderText = ShouldLCDRenderText(aFont->GetType(), aOptions.mAntialiasMode);
|
|
|
|
paint.mPaint.setLCDRenderText(shouldLCDRenderText);
|
|
|
|
|
2014-01-10 23:06:16 +04:00
|
|
|
if (aRenderingOptions && aRenderingOptions->GetType() == FontType::CAIRO) {
|
2013-06-05 21:48:59 +04:00
|
|
|
switch (static_cast<const GlyphRenderingOptionsCairo*>(aRenderingOptions)->GetHinting()) {
|
2014-01-10 23:06:16 +04:00
|
|
|
case FontHinting::NONE:
|
2013-06-05 21:48:59 +04:00
|
|
|
paint.mPaint.setHinting(SkPaint::kNo_Hinting);
|
|
|
|
break;
|
2014-01-10 23:06:16 +04:00
|
|
|
case FontHinting::LIGHT:
|
2013-06-05 21:48:59 +04:00
|
|
|
paint.mPaint.setHinting(SkPaint::kSlight_Hinting);
|
|
|
|
break;
|
2014-01-10 23:06:16 +04:00
|
|
|
case FontHinting::NORMAL:
|
2013-06-05 21:48:59 +04:00
|
|
|
paint.mPaint.setHinting(SkPaint::kNormal_Hinting);
|
|
|
|
break;
|
2014-01-10 23:06:16 +04:00
|
|
|
case FontHinting::FULL:
|
2013-06-05 21:48:59 +04:00
|
|
|
paint.mPaint.setHinting(SkPaint::kFull_Hinting);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (static_cast<const GlyphRenderingOptionsCairo*>(aRenderingOptions)->GetAutoHinting()) {
|
|
|
|
paint.mPaint.setAutohinted(true);
|
|
|
|
}
|
2015-05-04 20:23:54 +03:00
|
|
|
} else if (aFont->GetType() == FontType::MAC && shouldLCDRenderText) {
|
|
|
|
// SkFontHost_mac only supports subpixel antialiasing when hinting is turned off.
|
|
|
|
paint.mPaint.setHinting(SkPaint::kNo_Hinting);
|
2013-06-05 21:48:59 +04:00
|
|
|
} else {
|
|
|
|
paint.mPaint.setHinting(SkPaint::kNormal_Hinting);
|
|
|
|
}
|
|
|
|
|
2011-11-02 23:55:03 +04:00
|
|
|
std::vector<uint16_t> indices;
|
|
|
|
std::vector<SkPoint> offsets;
|
|
|
|
indices.resize(aBuffer.mNumGlyphs);
|
|
|
|
offsets.resize(aBuffer.mNumGlyphs);
|
|
|
|
|
|
|
|
for (unsigned int i = 0; i < aBuffer.mNumGlyphs; i++) {
|
|
|
|
indices[i] = aBuffer.mGlyphs[i].mIndex;
|
|
|
|
offsets[i].fX = SkFloatToScalar(aBuffer.mGlyphs[i].mPosition.x);
|
|
|
|
offsets[i].fY = SkFloatToScalar(aBuffer.mGlyphs[i].mPosition.y);
|
|
|
|
}
|
|
|
|
|
|
|
|
mCanvas->drawPosText(&indices.front(), aBuffer.mNumGlyphs*2, &offsets.front(), paint.mPaint);
|
|
|
|
}
|
|
|
|
|
2012-01-19 08:48:33 +04:00
|
|
|
void
|
|
|
|
DrawTargetSkia::Mask(const Pattern &aSource,
|
|
|
|
const Pattern &aMask,
|
|
|
|
const DrawOptions &aOptions)
|
|
|
|
{
|
|
|
|
MarkChanged();
|
|
|
|
AutoPaintSetup paint(mCanvas.get(), aOptions, aSource);
|
|
|
|
|
|
|
|
SkPaint maskPaint;
|
2014-01-13 04:29:48 +04:00
|
|
|
TempBitmap tmpBitmap;
|
|
|
|
SetPaintPattern(maskPaint, aMask, tmpBitmap);
|
2014-10-08 08:16:14 +04:00
|
|
|
|
2014-06-20 22:47:27 +04:00
|
|
|
SkLayerRasterizer::Builder builder;
|
|
|
|
builder.addLayer(maskPaint);
|
|
|
|
SkAutoTUnref<SkRasterizer> raster(builder.detachRasterizer());
|
|
|
|
paint.mPaint.setRasterizer(raster.get());
|
2012-01-19 08:48:33 +04:00
|
|
|
|
2013-11-25 18:22:33 +04:00
|
|
|
mCanvas->drawRect(SkRectCoveringWholeSurface(), paint.mPaint);
|
2012-01-19 08:48:33 +04:00
|
|
|
}
|
|
|
|
|
2013-11-25 18:20:56 +04:00
|
|
|
void
|
|
|
|
DrawTargetSkia::MaskSurface(const Pattern &aSource,
|
|
|
|
SourceSurface *aMask,
|
|
|
|
Point aOffset,
|
|
|
|
const DrawOptions &aOptions)
|
|
|
|
{
|
|
|
|
MarkChanged();
|
|
|
|
AutoPaintSetup paint(mCanvas.get(), aOptions, aSource);
|
|
|
|
|
2014-05-17 05:31:43 +04:00
|
|
|
TempBitmap bitmap = GetBitmapForSurface(aMask);
|
|
|
|
if (bitmap.mBitmap.colorType() == kAlpha_8_SkColorType) {
|
|
|
|
mCanvas->drawBitmap(bitmap.mBitmap, aOffset.x, aOffset.y, &paint.mPaint);
|
|
|
|
} else {
|
|
|
|
SkPaint maskPaint;
|
|
|
|
TempBitmap tmpBitmap;
|
|
|
|
SetPaintPattern(maskPaint, SurfacePattern(aMask, ExtendMode::CLAMP), tmpBitmap);
|
2013-11-25 18:20:56 +04:00
|
|
|
|
2014-05-17 05:31:43 +04:00
|
|
|
SkMatrix transform = maskPaint.getShader()->getLocalMatrix();
|
|
|
|
transform.postTranslate(SkFloatToScalar(aOffset.x), SkFloatToScalar(aOffset.y));
|
2014-07-28 04:47:43 +04:00
|
|
|
SkShader* matrixShader = SkShader::CreateLocalMatrixShader(maskPaint.getShader(), transform);
|
2014-09-02 23:18:15 +04:00
|
|
|
SkSafeUnref(maskPaint.setShader(matrixShader));
|
2013-11-25 18:20:56 +04:00
|
|
|
|
2014-06-20 22:47:27 +04:00
|
|
|
SkLayerRasterizer::Builder builder;
|
|
|
|
builder.addLayer(maskPaint);
|
|
|
|
SkAutoTUnref<SkRasterizer> raster(builder.detachRasterizer());
|
|
|
|
paint.mPaint.setRasterizer(raster.get());
|
2013-11-25 18:20:56 +04:00
|
|
|
|
2014-05-17 05:31:43 +04:00
|
|
|
IntSize size = aMask->GetSize();
|
|
|
|
Rect rect = Rect(aOffset.x, aOffset.y, size.width, size.height);
|
|
|
|
mCanvas->drawRect(RectToSkRect(rect), paint.mPaint);
|
|
|
|
}
|
2013-11-25 18:20:56 +04:00
|
|
|
}
|
|
|
|
|
2015-06-17 17:00:52 +03:00
|
|
|
already_AddRefed<SourceSurface>
|
2011-11-02 23:55:03 +04:00
|
|
|
DrawTargetSkia::CreateSourceSurfaceFromData(unsigned char *aData,
|
2013-11-01 17:13:36 +04:00
|
|
|
const IntSize &aSize,
|
|
|
|
int32_t aStride,
|
|
|
|
SurfaceFormat aFormat) const
|
2011-11-02 23:55:03 +04:00
|
|
|
{
|
|
|
|
RefPtr<SourceSurfaceSkia> newSurf = new SourceSurfaceSkia();
|
|
|
|
|
|
|
|
if (!newSurf->InitFromData(aData, aSize, aStride, aFormat)) {
|
|
|
|
gfxDebug() << *this << ": Failure to create source surface from data. Size: " << aSize;
|
2012-08-14 22:06:12 +04:00
|
|
|
return nullptr;
|
2011-11-02 23:55:03 +04:00
|
|
|
}
|
2014-10-08 08:16:14 +04:00
|
|
|
|
2014-06-13 20:09:23 +04:00
|
|
|
return newSurf.forget();
|
2011-11-02 23:55:03 +04:00
|
|
|
}
|
|
|
|
|
2015-06-17 17:00:52 +03:00
|
|
|
already_AddRefed<DrawTarget>
|
2011-11-02 23:55:03 +04:00
|
|
|
DrawTargetSkia::CreateSimilarDrawTarget(const IntSize &aSize, SurfaceFormat aFormat) const
|
|
|
|
{
|
|
|
|
RefPtr<DrawTargetSkia> target = new DrawTargetSkia();
|
|
|
|
if (!target->Init(aSize, aFormat)) {
|
2012-08-14 22:06:12 +04:00
|
|
|
return nullptr;
|
2011-11-02 23:55:03 +04:00
|
|
|
}
|
2014-06-13 20:09:23 +04:00
|
|
|
return target.forget();
|
2011-11-02 23:55:03 +04:00
|
|
|
}
|
|
|
|
|
2014-05-23 09:09:34 +04:00
|
|
|
bool
|
|
|
|
DrawTargetSkia::UsingSkiaGPU() const
|
|
|
|
{
|
|
|
|
#ifdef USE_SKIA_GPU
|
|
|
|
return !!mTexture;
|
|
|
|
#else
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2015-06-17 17:00:52 +03:00
|
|
|
already_AddRefed<SourceSurface>
|
2011-11-02 23:55:03 +04:00
|
|
|
DrawTargetSkia::OptimizeSourceSurface(SourceSurface *aSurface) const
|
|
|
|
{
|
2014-01-10 22:55:24 +04:00
|
|
|
if (aSurface->GetType() == SurfaceType::SKIA) {
|
2015-04-30 22:20:30 +03:00
|
|
|
RefPtr<SourceSurface> surface(aSurface);
|
|
|
|
return surface.forget();
|
2013-11-01 17:13:36 +04:00
|
|
|
}
|
|
|
|
|
2014-05-23 09:09:34 +04:00
|
|
|
if (!UsingSkiaGPU()) {
|
|
|
|
// If we're not using skia-gl then drawing doesn't require any
|
|
|
|
// uploading, so any data surface is fine. Call GetDataSurface
|
|
|
|
// to trigger any required readback so that it only happens
|
|
|
|
// once.
|
|
|
|
return aSurface->GetDataSurface();
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we are using skia-gl then we want to copy into a surface that
|
|
|
|
// will cache the uploaded gl texture.
|
|
|
|
RefPtr<DataSourceSurface> dataSurf = aSurface->GetDataSurface();
|
|
|
|
DataSourceSurface::MappedSurface map;
|
|
|
|
if (!dataSurf->Map(DataSourceSurface::READ, &map)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<SourceSurface> result = CreateSourceSurfaceFromData(map.mData,
|
|
|
|
dataSurf->GetSize(),
|
|
|
|
map.mStride,
|
|
|
|
dataSurf->GetFormat());
|
|
|
|
dataSurf->Unmap();
|
2014-06-13 20:09:23 +04:00
|
|
|
return result.forget();
|
2011-11-02 23:55:03 +04:00
|
|
|
}
|
|
|
|
|
2015-06-17 17:00:52 +03:00
|
|
|
already_AddRefed<SourceSurface>
|
2011-11-02 23:55:03 +04:00
|
|
|
DrawTargetSkia::CreateSourceSurfaceFromNativeSurface(const NativeSurface &aSurface) const
|
|
|
|
{
|
2014-07-04 01:06:48 +04:00
|
|
|
if (aSurface.mType == NativeSurfaceType::CAIRO_SURFACE) {
|
|
|
|
if (aSurface.mSize.width <= 0 ||
|
|
|
|
aSurface.mSize.height <= 0) {
|
|
|
|
gfxWarning() << "Can't create a SourceSurface without a valid size";
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
cairo_surface_t* surf = static_cast<cairo_surface_t*>(aSurface.mSurface);
|
2015-04-30 22:20:30 +03:00
|
|
|
return MakeAndAddRef<SourceSurfaceCairo>(surf, aSurface.mSize, aSurface.mFormat);
|
2014-11-11 23:14:00 +03:00
|
|
|
#if USE_SKIA_GPU
|
2015-05-01 21:08:04 +03:00
|
|
|
} else if (aSurface.mType == NativeSurfaceType::OPENGL_TEXTURE && UsingSkiaGPU()) {
|
2014-11-11 23:14:00 +03:00
|
|
|
RefPtr<SourceSurfaceSkia> newSurf = new SourceSurfaceSkia();
|
|
|
|
unsigned int texture = (unsigned int)((uintptr_t)aSurface.mSurface);
|
2015-05-01 21:08:04 +03:00
|
|
|
if (newSurf->InitFromTexture((DrawTargetSkia*)this, texture, aSurface.mSize, aSurface.mFormat)) {
|
2015-04-30 22:20:30 +03:00
|
|
|
return newSurf.forget();
|
2014-11-11 23:14:00 +03:00
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
#endif
|
2014-07-04 01:06:48 +04:00
|
|
|
}
|
|
|
|
|
2012-08-14 22:06:12 +04:00
|
|
|
return nullptr;
|
2011-11-02 23:55:03 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DrawTargetSkia::CopySurface(SourceSurface *aSurface,
|
|
|
|
const IntRect& aSourceRect,
|
|
|
|
const IntPoint &aDestination)
|
|
|
|
{
|
|
|
|
//TODO: We could just use writePixels() here if the sourceRect is the entire source
|
2013-11-01 17:13:36 +04:00
|
|
|
|
2014-06-11 19:38:41 +04:00
|
|
|
if (aSurface->GetType() != SurfaceType::SKIA && aSurface->GetType() != SurfaceType::DATA) {
|
2011-11-02 23:55:03 +04:00
|
|
|
return;
|
|
|
|
}
|
2011-11-02 23:55:03 +04:00
|
|
|
|
|
|
|
MarkChanged();
|
2013-11-01 17:13:36 +04:00
|
|
|
|
2014-01-13 04:29:48 +04:00
|
|
|
TempBitmap bitmap = GetBitmapForSurface(aSurface);
|
2011-11-02 23:55:03 +04:00
|
|
|
|
2014-06-11 07:42:32 +04:00
|
|
|
// This is a fast path that is disabled for now to mimimize risk
|
2014-07-28 04:47:43 +04:00
|
|
|
if (false && !bitmap.mBitmap.getTexture() && mCanvas->imageInfo() == bitmap.mBitmap.info()) {
|
2014-10-09 00:04:19 +04:00
|
|
|
SkBitmap bm(bitmap.mBitmap);
|
|
|
|
bm.lockPixels();
|
|
|
|
if (bm.getPixels()) {
|
|
|
|
SkImageInfo info = bm.info();
|
|
|
|
info.fWidth = aSourceRect.width;
|
|
|
|
info.fHeight = 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;
|
|
|
|
}
|
2014-06-11 07:42:32 +04:00
|
|
|
}
|
|
|
|
|
2011-11-02 23:55:03 +04:00
|
|
|
mCanvas->save();
|
|
|
|
mCanvas->resetMatrix();
|
2014-10-08 08:16:14 +04:00
|
|
|
SkRect dest = IntRectToSkRect(IntRect(aDestination.x, aDestination.y, aSourceRect.width, aSourceRect.height));
|
2011-11-02 23:55:03 +04:00
|
|
|
SkIRect source = IntRectToSkIRect(aSourceRect);
|
|
|
|
mCanvas->clipRect(dest, SkRegion::kReplace_Op);
|
|
|
|
SkPaint paint;
|
2012-05-31 22:56:33 +04:00
|
|
|
|
2014-07-28 04:47:43 +04:00
|
|
|
if (mCanvas->imageInfo().colorType() == kRGB_565_SkColorType) {
|
2012-05-31 22:56:33 +04:00
|
|
|
// Set the xfermode to SOURCE_OVER to workaround
|
|
|
|
// http://code.google.com/p/skia/issues/detail?id=628
|
|
|
|
// RGB565 is opaque so they're equivalent anyway
|
|
|
|
paint.setXfermodeMode(SkXfermode::kSrcOver_Mode);
|
|
|
|
} else {
|
|
|
|
paint.setXfermodeMode(SkXfermode::kSrc_Mode);
|
|
|
|
}
|
2014-06-11 07:42:32 +04:00
|
|
|
// drawBitmapRect with A8 bitmaps ends up doing a mask operation
|
|
|
|
// so we need to clear before
|
2014-07-28 04:47:43 +04:00
|
|
|
if (bitmap.mBitmap.colorType() == kAlpha_8_SkColorType) {
|
2014-06-11 07:42:32 +04:00
|
|
|
SkPaint clearPaint;
|
|
|
|
clearPaint.setColor(SkColorSetARGB(0, 0, 0, 0));
|
|
|
|
clearPaint.setXfermodeMode(SkXfermode::kSrc_Mode);
|
|
|
|
mCanvas->drawPaint(clearPaint);
|
|
|
|
}
|
2014-01-13 04:29:48 +04:00
|
|
|
mCanvas->drawBitmapRect(bitmap.mBitmap, &source, dest, &paint);
|
2011-11-02 23:55:03 +04:00
|
|
|
mCanvas->restore();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
DrawTargetSkia::Init(const IntSize &aSize, SurfaceFormat aFormat)
|
|
|
|
{
|
2014-06-25 02:35:07 +04:00
|
|
|
SkAlphaType alphaType = (aFormat == SurfaceFormat::B8G8R8X8) ?
|
|
|
|
kOpaque_SkAlphaType : kPremul_SkAlphaType;
|
|
|
|
|
|
|
|
SkImageInfo skiInfo = SkImageInfo::Make(
|
|
|
|
aSize.width, aSize.height,
|
|
|
|
GfxFormatToSkiaColorType(aFormat),
|
|
|
|
alphaType);
|
|
|
|
|
|
|
|
SkAutoTUnref<SkBaseDevice> device(SkBitmapDevice::Create(skiInfo));
|
|
|
|
if (!device) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-05-29 20:49:40 +04:00
|
|
|
|
|
|
|
SkBitmap bitmap = device->accessBitmap(true);
|
|
|
|
if (!bitmap.allocPixels()) {
|
2011-11-02 23:55:03 +04:00
|
|
|
return false;
|
|
|
|
}
|
2013-05-29 20:49:40 +04:00
|
|
|
|
|
|
|
bitmap.eraseARGB(0, 0, 0, 0);
|
|
|
|
|
2014-08-06 20:01:44 +04:00
|
|
|
mCanvas.adopt(new SkCanvas(device.get()));
|
2011-11-02 23:55:03 +04:00
|
|
|
mSize = aSize;
|
|
|
|
|
|
|
|
mFormat = aFormat;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-03-14 03:29:47 +04:00
|
|
|
#ifdef USE_SKIA_GPU
|
2014-03-26 22:21:50 +04:00
|
|
|
bool
|
2014-03-06 01:49:37 +04:00
|
|
|
DrawTargetSkia::InitWithGrContext(GrContext* aGrContext,
|
|
|
|
const IntSize &aSize,
|
|
|
|
SurfaceFormat aFormat)
|
2012-12-01 03:57:38 +04:00
|
|
|
{
|
2014-03-24 18:13:55 +04:00
|
|
|
MOZ_ASSERT(aGrContext, "null GrContext");
|
|
|
|
|
2014-03-06 01:49:37 +04:00
|
|
|
mGrContext = aGrContext;
|
2013-06-17 06:07:18 +04:00
|
|
|
mSize = aSize;
|
|
|
|
mFormat = aFormat;
|
2013-06-17 21:45:04 +04:00
|
|
|
|
2014-03-06 01:49:37 +04:00
|
|
|
GrTextureDesc targetDescriptor;
|
2012-12-01 03:57:38 +04:00
|
|
|
|
2014-03-06 01:49:37 +04:00
|
|
|
targetDescriptor.fFlags = kRenderTarget_GrTextureFlagBit;
|
2013-06-17 06:07:18 +04:00
|
|
|
targetDescriptor.fWidth = mSize.width;
|
|
|
|
targetDescriptor.fHeight = mSize.height;
|
|
|
|
targetDescriptor.fConfig = GfxFormatToGrConfig(mFormat);
|
2013-04-09 23:57:22 +04:00
|
|
|
targetDescriptor.fOrigin = kBottomLeft_GrSurfaceOrigin;
|
2012-12-01 03:57:38 +04:00
|
|
|
targetDescriptor.fSampleCnt = 0;
|
|
|
|
|
2014-03-06 01:49:37 +04:00
|
|
|
SkAutoTUnref<GrTexture> skiaTexture(mGrContext->createUncachedTexture(targetDescriptor, NULL, 0));
|
2014-03-26 22:21:50 +04:00
|
|
|
if (!skiaTexture) {
|
|
|
|
return false;
|
|
|
|
}
|
2014-03-06 01:49:37 +04:00
|
|
|
|
|
|
|
mTexture = (uint32_t)skiaTexture->getTextureHandle();
|
|
|
|
|
|
|
|
SkAutoTUnref<SkBaseDevice> device(new SkGpuDevice(mGrContext.get(), skiaTexture->asRenderTarget()));
|
2014-08-06 20:01:44 +04:00
|
|
|
mCanvas.adopt(new SkCanvas(device.get()));
|
2014-03-26 22:21:50 +04:00
|
|
|
|
|
|
|
return true;
|
2013-08-28 18:08:10 +04:00
|
|
|
}
|
|
|
|
|
2013-03-14 03:29:47 +04:00
|
|
|
#endif
|
2012-12-01 03:57:38 +04:00
|
|
|
|
2012-01-19 08:48:33 +04:00
|
|
|
void
|
|
|
|
DrawTargetSkia::Init(unsigned char* aData, const IntSize &aSize, int32_t aStride, SurfaceFormat aFormat)
|
|
|
|
{
|
2014-03-26 23:58:07 +04:00
|
|
|
SkAlphaType alphaType = kPremul_SkAlphaType;
|
|
|
|
if (aFormat == SurfaceFormat::B8G8R8X8) {
|
|
|
|
// We have to manually set the A channel to be 255 as Skia doesn't understand BGRX
|
|
|
|
ConvertBGRXToBGRA(aData, aSize, aStride);
|
|
|
|
alphaType = kOpaque_SkAlphaType;
|
|
|
|
}
|
|
|
|
|
2013-11-01 06:48:46 +04:00
|
|
|
SkBitmap bitmap;
|
2014-07-28 04:47:43 +04:00
|
|
|
|
|
|
|
SkImageInfo info = SkImageInfo::Make(aSize.width,
|
|
|
|
aSize.height,
|
|
|
|
GfxFormatToSkiaColorType(aFormat),
|
|
|
|
alphaType);
|
|
|
|
bitmap.setInfo(info, aStride);
|
2013-11-01 06:48:46 +04:00
|
|
|
bitmap.setPixels(aData);
|
2014-11-19 01:22:01 +03:00
|
|
|
mCanvas.adopt(new SkCanvas(bitmap));
|
2013-05-29 20:49:40 +04:00
|
|
|
|
2012-01-19 08:48:33 +04:00
|
|
|
mSize = aSize;
|
|
|
|
mFormat = aFormat;
|
|
|
|
}
|
|
|
|
|
2011-11-02 23:55:03 +04:00
|
|
|
void
|
|
|
|
DrawTargetSkia::SetTransform(const Matrix& aTransform)
|
|
|
|
{
|
|
|
|
SkMatrix mat;
|
|
|
|
GfxMatrixToSkiaMatrix(aTransform, mat);
|
|
|
|
mCanvas->setMatrix(mat);
|
|
|
|
mTransform = aTransform;
|
|
|
|
}
|
|
|
|
|
2014-03-06 01:49:37 +04:00
|
|
|
void*
|
|
|
|
DrawTargetSkia::GetNativeSurface(NativeSurfaceType aType)
|
|
|
|
{
|
2014-10-15 07:55:37 +04:00
|
|
|
#ifdef USE_SKIA_GPU
|
2014-03-06 01:49:37 +04:00
|
|
|
if (aType == NativeSurfaceType::OPENGL_TEXTURE) {
|
|
|
|
return (void*)((uintptr_t)mTexture);
|
|
|
|
}
|
2014-10-15 07:55:37 +04:00
|
|
|
#endif
|
2014-10-08 08:16:14 +04:00
|
|
|
return nullptr;
|
2014-03-06 01:49:37 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-06-17 17:00:52 +03:00
|
|
|
already_AddRefed<PathBuilder>
|
2011-11-02 23:55:03 +04:00
|
|
|
DrawTargetSkia::CreatePathBuilder(FillRule aFillRule) const
|
|
|
|
{
|
2015-04-30 22:20:30 +03:00
|
|
|
return MakeAndAddRef<PathBuilderSkia>(aFillRule);
|
2011-11-02 23:55:03 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DrawTargetSkia::ClearRect(const Rect &aRect)
|
|
|
|
{
|
2011-11-02 23:55:03 +04:00
|
|
|
MarkChanged();
|
2011-11-02 23:55:03 +04:00
|
|
|
SkPaint paint;
|
|
|
|
mCanvas->save();
|
2013-10-08 23:05:57 +04:00
|
|
|
mCanvas->clipRect(RectToSkRect(aRect), SkRegion::kIntersect_Op, true);
|
2011-11-02 23:55:03 +04:00
|
|
|
paint.setColor(SkColorSetARGB(0, 0, 0, 0));
|
|
|
|
paint.setXfermodeMode(SkXfermode::kSrc_Mode);
|
|
|
|
mCanvas->drawPaint(paint);
|
|
|
|
mCanvas->restore();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DrawTargetSkia::PushClip(const Path *aPath)
|
|
|
|
{
|
2014-01-10 23:06:16 +04:00
|
|
|
if (aPath->GetBackendType() != BackendType::SKIA) {
|
2011-11-02 23:55:03 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const PathSkia *skiaPath = static_cast<const PathSkia*>(aPath);
|
2014-07-28 04:51:09 +04:00
|
|
|
mCanvas->save();
|
2013-10-08 23:05:57 +04:00
|
|
|
mCanvas->clipPath(skiaPath->GetPath(), SkRegion::kIntersect_Op, true);
|
2011-11-02 23:55:03 +04:00
|
|
|
}
|
|
|
|
|
2012-01-19 08:48:33 +04:00
|
|
|
void
|
|
|
|
DrawTargetSkia::PushClipRect(const Rect& aRect)
|
|
|
|
{
|
|
|
|
SkRect rect = RectToSkRect(aRect);
|
|
|
|
|
2014-07-28 04:51:09 +04:00
|
|
|
mCanvas->save();
|
2013-10-08 23:05:57 +04:00
|
|
|
mCanvas->clipRect(rect, SkRegion::kIntersect_Op, true);
|
2012-01-19 08:48:33 +04:00
|
|
|
}
|
|
|
|
|
2011-11-02 23:55:03 +04:00
|
|
|
void
|
|
|
|
DrawTargetSkia::PopClip()
|
|
|
|
{
|
|
|
|
mCanvas->restore();
|
|
|
|
}
|
|
|
|
|
2015-06-17 17:00:52 +03:00
|
|
|
already_AddRefed<GradientStops>
|
2011-12-28 09:56:11 +04:00
|
|
|
DrawTargetSkia::CreateGradientStops(GradientStop *aStops, uint32_t aNumStops, ExtendMode aExtendMode) const
|
2011-11-02 23:55:03 +04:00
|
|
|
{
|
|
|
|
std::vector<GradientStop> stops;
|
|
|
|
stops.resize(aNumStops);
|
|
|
|
for (uint32_t i = 0; i < aNumStops; i++) {
|
|
|
|
stops[i] = aStops[i];
|
|
|
|
}
|
|
|
|
std::stable_sort(stops.begin(), stops.end());
|
2014-10-08 08:16:14 +04:00
|
|
|
|
2015-04-30 22:20:30 +03:00
|
|
|
return MakeAndAddRef<GradientStopsSkia>(stops, aNumStops, aExtendMode);
|
2011-11-02 23:55:03 +04:00
|
|
|
}
|
|
|
|
|
2015-06-17 17:00:52 +03:00
|
|
|
already_AddRefed<FilterNode>
|
2013-11-27 15:25:28 +04:00
|
|
|
DrawTargetSkia::CreateFilter(FilterType aType)
|
|
|
|
{
|
|
|
|
return FilterNodeSoftware::Create(aType);
|
|
|
|
}
|
|
|
|
|
2011-11-02 23:55:03 +04:00
|
|
|
void
|
2013-08-02 05:12:16 +04:00
|
|
|
DrawTargetSkia::MarkChanged()
|
2013-08-03 07:35:39 +04:00
|
|
|
{
|
2013-08-02 05:12:16 +04:00
|
|
|
if (mSnapshot) {
|
|
|
|
mSnapshot->DrawTargetWillChange();
|
|
|
|
mSnapshot = nullptr;
|
2011-11-02 23:55:03 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-25 18:22:33 +04:00
|
|
|
// Return a rect (in user space) that covers the entire surface by applying
|
|
|
|
// the inverse of GetTransform() to (0, 0, mSize.width, mSize.height).
|
|
|
|
SkRect
|
|
|
|
DrawTargetSkia::SkRectCoveringWholeSurface() const
|
|
|
|
{
|
|
|
|
return RectToSkRect(mTransform.TransformBounds(Rect(0, 0, mSize.width, mSize.height)));
|
|
|
|
}
|
|
|
|
|
2015-05-26 10:00:19 +03:00
|
|
|
void
|
|
|
|
DrawTargetSkia::SnapshotDestroyed()
|
|
|
|
{
|
|
|
|
mSnapshot = nullptr;
|
|
|
|
}
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace gfx
|
|
|
|
} // namespace mozilla
|