2017-10-28 02:10:06 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
2012-05-21 15:12:37 +04:00
|
|
|
* 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"
|
|
|
|
#include "SourceSurfaceSkia.h"
|
2012-01-09 22:54:44 +04:00
|
|
|
#include "ScaledFontBase.h"
|
2013-11-27 15:25:28 +04:00
|
|
|
#include "FilterNodeSoftware.h"
|
2015-09-02 21:12:32 +03:00
|
|
|
#include "HelpersSkia.h"
|
2013-03-14 03:29:47 +04:00
|
|
|
|
2016-09-07 21:42:15 +03:00
|
|
|
#include "mozilla/ArrayUtils.h"
|
2018-03-12 23:37:10 +03:00
|
|
|
#include "mozilla/CheckedInt.h"
|
2018-10-31 05:17:34 +03:00
|
|
|
#include "mozilla/Vector.h"
|
2016-09-07 21:42:15 +03:00
|
|
|
|
2019-03-26 18:06:56 +03:00
|
|
|
#include "skia/include/core/SkFont.h"
|
|
|
|
#include "skia/include/core/SkTextBlob.h"
|
2015-12-18 21:53:25 +03:00
|
|
|
#include "skia/include/core/SkSurface.h"
|
2015-07-29 23:31:40 +03:00
|
|
|
#include "skia/include/core/SkTypeface.h"
|
|
|
|
#include "skia/include/effects/SkGradientShader.h"
|
|
|
|
#include "skia/include/core/SkColorFilter.h"
|
2017-05-15 13:14:38 +03:00
|
|
|
#include "skia/include/core/SkRegion.h"
|
2015-09-04 22:29:11 +03:00
|
|
|
#include "skia/include/effects/SkBlurImageFilter.h"
|
2016-03-26 05:14:50 +03:00
|
|
|
#include "Blur.h"
|
2011-11-02 23:55:03 +04:00
|
|
|
#include "Logging.h"
|
|
|
|
#include "Tools.h"
|
2013-11-03 20:28:30 +04:00
|
|
|
#include "DataSurfaceHelpers.h"
|
2017-02-28 23:47:46 +03:00
|
|
|
#include "PathHelpers.h"
|
2017-10-31 22:02:31 +03:00
|
|
|
#include "SourceSurfaceCapture.h"
|
2017-02-12 17:28:12 +03:00
|
|
|
#include "Swizzle.h"
|
2011-11-02 23:55:03 +04:00
|
|
|
#include <algorithm>
|
|
|
|
|
2016-05-17 21:41:38 +03:00
|
|
|
#ifdef MOZ_WIDGET_COCOA
|
2016-08-03 17:53:12 +03:00
|
|
|
# include "BorrowedContext.h"
|
2016-05-17 21:41:38 +03:00
|
|
|
# include <ApplicationServices/ApplicationServices.h>
|
|
|
|
# include "ScaledFontMac.h"
|
|
|
|
# include "CGTextDrawing.h"
|
|
|
|
#endif
|
|
|
|
|
2016-09-08 19:34:10 +03:00
|
|
|
#ifdef XP_WIN
|
|
|
|
# include "ScaledFontDWrite.h"
|
|
|
|
#endif
|
|
|
|
|
2011-11-02 23:55:03 +04:00
|
|
|
namespace mozilla {
|
|
|
|
namespace gfx {
|
|
|
|
|
|
|
|
class GradientStopsSkia : public GradientStops {
|
|
|
|
public:
|
2017-11-06 06:37:28 +03:00
|
|
|
MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(GradientStopsSkia, override)
|
|
|
|
|
2012-01-19 08:48:33 +04:00
|
|
|
GradientStopsSkia(const std::vector<GradientStop>& aStops, uint32_t aNumStops,
|
|
|
|
ExtendMode aExtendMode)
|
|
|
|
: mCount(aNumStops), 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-06 06:37:28 +03:00
|
|
|
BackendType GetBackendType() const override { 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
|
|
|
/**
|
2016-10-25 05:40:59 +03:00
|
|
|
* When constructing a temporary SkImage via GetSkImageForSurface, we may also
|
2014-01-13 04:29:48 +04:00
|
|
|
* have to construct a temporary DataSourceSurface, which must live as long as
|
2016-10-25 05:40:59 +03:00
|
|
|
* the SkImage. We attach this temporary surface to the image's pixelref, so
|
2016-01-12 21:05:13 +03:00
|
|
|
* that it can be released once the pixelref is freed.
|
2014-01-13 04:29:48 +04:00
|
|
|
*/
|
2016-10-25 05:40:59 +03:00
|
|
|
static void ReleaseTemporarySurface(const void* aPixels, void* aContext) {
|
2016-01-12 21:05:13 +03:00
|
|
|
DataSourceSurface* surf = static_cast<DataSourceSurface*>(aContext);
|
|
|
|
if (surf) {
|
|
|
|
surf->Release();
|
|
|
|
}
|
|
|
|
}
|
2014-01-13 04:29:48 +04:00
|
|
|
|
2016-08-05 20:29:12 +03:00
|
|
|
static void WriteRGBXFormat(uint8_t* aData, const IntSize& aSize,
|
|
|
|
const int32_t aStride, SurfaceFormat aFormat) {
|
|
|
|
if (aFormat != SurfaceFormat::B8G8R8X8 || aSize.IsEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-02-12 17:28:12 +03:00
|
|
|
SwizzleData(aData, aStride, SurfaceFormat::X8R8G8B8_UINT32, aData, aStride,
|
|
|
|
SurfaceFormat::A8R8G8B8_UINT32, aSize);
|
2016-08-05 20:29:12 +03:00
|
|
|
}
|
|
|
|
|
2016-07-26 02:36:35 +03:00
|
|
|
#ifdef DEBUG
|
2016-08-18 16:55:45 +03:00
|
|
|
static IntRect CalculateSurfaceBounds(const IntSize& aSize, const Rect* aBounds,
|
|
|
|
const Matrix* aMatrix) {
|
|
|
|
IntRect surfaceBounds(IntPoint(0, 0), aSize);
|
|
|
|
if (!aBounds) {
|
|
|
|
return surfaceBounds;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(aMatrix);
|
|
|
|
Matrix inverse(*aMatrix);
|
|
|
|
if (!inverse.Invert()) {
|
|
|
|
return surfaceBounds;
|
|
|
|
}
|
|
|
|
|
|
|
|
IntRect bounds;
|
|
|
|
Rect sampledBounds = inverse.TransformBounds(*aBounds);
|
|
|
|
if (!sampledBounds.ToIntRect(&bounds)) {
|
|
|
|
return surfaceBounds;
|
|
|
|
}
|
|
|
|
|
|
|
|
return surfaceBounds.Intersect(bounds);
|
|
|
|
}
|
|
|
|
|
2017-02-12 17:28:12 +03:00
|
|
|
static const int kARGBAlphaOffset =
|
|
|
|
SurfaceFormat::A8R8G8B8_UINT32 == SurfaceFormat::B8G8R8A8 ? 3 : 0;
|
|
|
|
|
2016-07-26 02:36:35 +03:00
|
|
|
static bool VerifyRGBXFormat(uint8_t* aData, const IntSize& aSize,
|
|
|
|
const int32_t aStride, SurfaceFormat aFormat) {
|
|
|
|
if (aFormat != SurfaceFormat::B8G8R8X8 || aSize.IsEmpty()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// We should've initialized the data to be opaque already
|
|
|
|
// On debug builds, verify that this is actually true.
|
|
|
|
int height = aSize.height;
|
2016-08-05 20:26:33 +03:00
|
|
|
int width = aSize.width * 4;
|
2016-07-26 02:36:35 +03:00
|
|
|
|
|
|
|
for (int row = 0; row < height; ++row) {
|
|
|
|
for (int column = 0; column < width; column += 4) {
|
2016-09-07 21:42:15 +03:00
|
|
|
if (aData[column + kARGBAlphaOffset] != 0xFF) {
|
|
|
|
gfxCriticalError() << "RGBX pixel at (" << column << "," << row
|
|
|
|
<< ") in " << width << "x" << height
|
|
|
|
<< " surface is not opaque: " << int(aData[column])
|
|
|
|
<< "," << int(aData[column + 1]) << ","
|
|
|
|
<< int(aData[column + 2]) << ","
|
|
|
|
<< int(aData[column + 3]);
|
|
|
|
}
|
2016-07-26 02:36:35 +03:00
|
|
|
}
|
|
|
|
aData += aStride;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Since checking every pixel is expensive, this only checks the four corners
|
|
|
|
// and center of a surface that their alpha value is 0xFF.
|
2016-08-18 16:55:45 +03:00
|
|
|
static bool VerifyRGBXCorners(uint8_t* aData, const IntSize& aSize,
|
|
|
|
const int32_t aStride, SurfaceFormat aFormat,
|
|
|
|
const Rect* aBounds = nullptr,
|
|
|
|
const Matrix* aMatrix = nullptr) {
|
2016-07-26 02:36:35 +03:00
|
|
|
if (aFormat != SurfaceFormat::B8G8R8X8 || aSize.IsEmpty()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-08-18 16:55:45 +03:00
|
|
|
IntRect bounds = CalculateSurfaceBounds(aSize, aBounds, aMatrix);
|
|
|
|
if (bounds.IsEmpty()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-08-14 15:29:28 +03:00
|
|
|
const int height = bounds.Height();
|
|
|
|
const int width = bounds.Width();
|
2016-07-26 02:36:35 +03:00
|
|
|
const int pixelSize = 4;
|
2016-08-18 16:55:45 +03:00
|
|
|
MOZ_ASSERT(aSize.width * pixelSize <= aStride);
|
2016-07-26 02:36:35 +03:00
|
|
|
|
2017-12-19 23:48:39 +03:00
|
|
|
const int translation = bounds.Y() * aStride + bounds.X() * pixelSize;
|
2016-08-18 16:55:45 +03:00
|
|
|
const int topLeft = translation;
|
|
|
|
const int topRight = topLeft + (width - 1) * pixelSize;
|
|
|
|
const int bottomLeft = translation + (height - 1) * aStride;
|
|
|
|
const int bottomRight = bottomLeft + (width - 1) * pixelSize;
|
2016-07-26 02:36:35 +03:00
|
|
|
|
|
|
|
// Lastly the center pixel
|
2016-08-18 16:55:45 +03:00
|
|
|
const int middleRowHeight = height / 2;
|
|
|
|
const int middleRowWidth = (width / 2) * pixelSize;
|
|
|
|
const int middle = translation + aStride * middleRowHeight + middleRowWidth;
|
2016-09-07 21:42:15 +03:00
|
|
|
|
|
|
|
const int offsets[] = {topLeft, topRight, bottomRight, bottomLeft, middle};
|
2016-11-15 11:55:49 +03:00
|
|
|
for (int offset : offsets) {
|
2016-09-29 02:20:13 +03:00
|
|
|
if (aData[offset + kARGBAlphaOffset] != 0xFF) {
|
2016-09-07 21:42:15 +03:00
|
|
|
int row = offset / aStride;
|
|
|
|
int column = (offset % aStride) / pixelSize;
|
|
|
|
gfxCriticalError() << "RGBX corner pixel at (" << column << "," << row
|
2016-08-18 16:55:45 +03:00
|
|
|
<< ") in " << aSize.width << "x" << aSize.height
|
|
|
|
<< " surface, bounded by "
|
2017-12-19 23:48:39 +03:00
|
|
|
<< "(" << bounds.X() << "," << bounds.Y() << ","
|
|
|
|
<< width << "," << height
|
2016-09-29 02:20:13 +03:00
|
|
|
<< ") is not opaque: " << int(aData[offset]) << ","
|
|
|
|
<< int(aData[offset + 1]) << ","
|
|
|
|
<< int(aData[offset + 2]) << ","
|
|
|
|
<< int(aData[offset + 3]);
|
2016-09-07 21:42:15 +03:00
|
|
|
}
|
|
|
|
}
|
2016-07-26 02:36:35 +03:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2016-08-18 16:55:45 +03:00
|
|
|
static sk_sp<SkImage> GetSkImageForSurface(SourceSurface* aSurface,
|
|
|
|
const Rect* aBounds = nullptr,
|
|
|
|
const Matrix* aMatrix = nullptr) {
|
2016-04-14 23:20:12 +03:00
|
|
|
if (!aSurface) {
|
2016-10-25 05:40:59 +03:00
|
|
|
gfxDebug() << "Creating null Skia image from null SourceSurface";
|
|
|
|
return nullptr;
|
2016-04-14 23:20:12 +03:00
|
|
|
}
|
|
|
|
|
2017-10-31 22:02:31 +03:00
|
|
|
if (aSurface->GetType() == SurfaceType::CAPTURE) {
|
|
|
|
SourceSurfaceCapture* capture =
|
|
|
|
static_cast<SourceSurfaceCapture*>(aSurface);
|
|
|
|
RefPtr<SourceSurface> resolved = capture->Resolve(BackendType::SKIA);
|
|
|
|
if (!resolved) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
MOZ_ASSERT(resolved->GetType() != SurfaceType::CAPTURE);
|
|
|
|
return GetSkImageForSurface(resolved, aBounds, aMatrix);
|
|
|
|
}
|
|
|
|
|
2014-01-13 04:29:48 +04:00
|
|
|
if (aSurface->GetType() == SurfaceType::SKIA) {
|
2016-10-25 05:40:59 +03:00
|
|
|
return static_cast<SourceSurfaceSkia*>(aSurface)->GetImage();
|
2014-01-13 04:29:48 +04:00
|
|
|
}
|
|
|
|
|
2016-01-12 21:05:13 +03:00
|
|
|
DataSourceSurface* surf = aSurface->GetDataSurface().take();
|
2014-01-13 04:29:48 +04:00
|
|
|
if (!surf) {
|
2016-10-25 05:40:59 +03:00
|
|
|
gfxWarning() << "Failed getting DataSourceSurface for Skia image";
|
|
|
|
return nullptr;
|
2013-11-27 15:25:27 +04:00
|
|
|
}
|
2014-01-13 04:29:48 +04:00
|
|
|
|
2016-10-25 05:40:59 +03:00
|
|
|
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";
|
2016-01-12 21:05:13 +03:00
|
|
|
}
|
2014-07-28 04:47:43 +04:00
|
|
|
|
2016-07-26 02:36:35 +03:00
|
|
|
// Skia doesn't support RGBX surfaces so ensure that the alpha value is opaque
|
|
|
|
// white.
|
|
|
|
MOZ_ASSERT(VerifyRGBXCorners(surf->GetData(), surf->GetSize(), surf->Stride(),
|
2016-08-18 16:55:45 +03:00
|
|
|
surf->GetFormat(), aBounds, aMatrix));
|
2016-10-25 05:40:59 +03:00
|
|
|
return image;
|
2013-11-27 15:25:27 +04:00
|
|
|
}
|
|
|
|
|
2011-11-02 23:55:03 +04:00
|
|
|
DrawTargetSkia::DrawTargetSkia()
|
2018-06-16 17:42:33 +03:00
|
|
|
: mCanvas(nullptr), mSnapshot(nullptr), mSnapshotLock {
|
2018-04-13 16:01:28 +03:00
|
|
|
"DrawTargetSkia::mSnapshotLock"
|
|
|
|
}
|
2016-05-17 21:41:38 +03:00
|
|
|
#ifdef MOZ_WIDGET_COCOA
|
2016-05-19 03:09:14 +03:00
|
|
|
, mCG(nullptr), mColorSpace(nullptr), mCanvasData(nullptr), mCGSize(0, 0),
|
2017-05-10 23:21:13 +03:00
|
|
|
mNeedLayer(false)
|
2016-05-17 21:41:38 +03:00
|
|
|
#endif
|
2011-11-02 23:55:03 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
DrawTargetSkia::~DrawTargetSkia() {
|
2017-12-06 06:59:19 +03:00
|
|
|
if (mSnapshot) {
|
|
|
|
MutexAutoLock lock(mSnapshotLock);
|
|
|
|
// We're going to go away, hand our SkSurface to the SourceSurface.
|
|
|
|
mSnapshot->GiveSurface(mSurface);
|
|
|
|
}
|
|
|
|
|
2016-05-17 21:41:38 +03:00
|
|
|
#ifdef MOZ_WIDGET_COCOA
|
|
|
|
if (mCG) {
|
|
|
|
CGContextRelease(mCG);
|
|
|
|
mCG = nullptr;
|
|
|
|
}
|
2016-05-19 03:09:14 +03:00
|
|
|
|
|
|
|
if (mColorSpace) {
|
|
|
|
CGColorSpaceRelease(mColorSpace);
|
|
|
|
mColorSpace = nullptr;
|
|
|
|
}
|
2016-05-17 21:41:38 +03:00
|
|
|
#endif
|
2011-11-02 23:55:03 +04:00
|
|
|
}
|
|
|
|
|
2015-06-17 17:00:52 +03:00
|
|
|
already_AddRefed<SourceSurface> DrawTargetSkia::Snapshot() {
|
2017-11-27 05:12:09 +03:00
|
|
|
// Without this lock, this could cause us to get out a snapshot and race with
|
|
|
|
// Snapshot::~Snapshot() actually destroying itself.
|
2017-12-06 06:59:19 +03:00
|
|
|
MutexAutoLock lock(mSnapshotLock);
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<SourceSurfaceSkia> snapshot = mSnapshot;
|
2016-10-26 20:41:07 +03:00
|
|
|
if (mSurface && !snapshot) {
|
2013-08-02 05:12:16 +04:00
|
|
|
snapshot = new SourceSurfaceSkia();
|
2016-10-25 05:40:59 +03:00
|
|
|
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 {
|
2017-05-10 05:31:07 +03:00
|
|
|
image = mSurface->makeImageSnapshot();
|
2016-10-25 05:40:59 +03:00
|
|
|
}
|
2017-12-06 06:59:19 +03:00
|
|
|
if (!snapshot->InitFromImage(image, mFormat, this)) {
|
2016-10-25 02:45:46 +03:00
|
|
|
return nullptr;
|
2016-10-25 05:40:59 +03:00
|
|
|
}
|
|
|
|
mSnapshot = snapshot;
|
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,
|
2016-01-13 21:11:07 +03:00
|
|
|
SurfaceFormat* aFormat, IntPoint* aOrigin) {
|
2015-12-18 21:53:25 +03:00
|
|
|
SkImageInfo info;
|
|
|
|
size_t rowBytes;
|
2017-05-10 05:31:07 +03:00
|
|
|
SkIPoint origin;
|
|
|
|
void* pixels = mCanvas->accessTopLayerPixels(&info, &rowBytes, &origin);
|
|
|
|
if (!pixels ||
|
|
|
|
// Ensure the layer is at the origin if required.
|
|
|
|
(!aOrigin && !origin.isZero())) {
|
2015-05-01 21:08:04 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
MarkChanged();
|
|
|
|
|
2015-12-18 21:53:25 +03:00
|
|
|
*aData = reinterpret_cast<uint8_t*>(pixels);
|
|
|
|
*aSize = IntSize(info.width(), info.height());
|
|
|
|
*aStride = int32_t(rowBytes);
|
2016-08-04 03:24:39 +03:00
|
|
|
*aFormat = SkiaColorTypeToGfxFormat(info.colorType(), info.alphaType());
|
2016-01-13 21:11:07 +03:00
|
|
|
if (aOrigin) {
|
|
|
|
*aOrigin = IntPoint(origin.x(), origin.y());
|
|
|
|
}
|
2015-05-01 21:08:04 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DrawTargetSkia::ReleaseBits(uint8_t* aData) {}
|
|
|
|
|
2016-10-25 05:40:59 +03:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2018-03-12 23:37:10 +03:00
|
|
|
static void FreeBitmapPixels(void* aBuf, void*) { sk_free(aBuf); }
|
2016-10-25 05:40:59 +03:00
|
|
|
|
2016-11-15 01:16:18 +03:00
|
|
|
static bool ExtractAlphaBitmap(const sk_sp<SkImage>& aImage,
|
|
|
|
SkBitmap* aResultBitmap) {
|
2016-10-25 05:40:59 +03:00
|
|
|
SkImageInfo info = SkImageInfo::MakeA8(aImage->width(), aImage->height());
|
2018-03-12 23:37:10 +03:00
|
|
|
// Skia does not fully allocate the last row according to stride.
|
|
|
|
// Since some of our algorithms (i.e. blur) depend on this, we must allocate
|
|
|
|
// the bitmap pixels manually.
|
|
|
|
size_t stride = SkAlign4(info.minRowBytes());
|
|
|
|
CheckedInt<size_t> size = stride;
|
|
|
|
size *= info.height();
|
|
|
|
if (size.isValid()) {
|
|
|
|
void* buf = sk_malloc_flags(size.value(), 0);
|
|
|
|
if (buf) {
|
|
|
|
SkBitmap bitmap;
|
|
|
|
if (bitmap.installPixels(info, buf, stride, FreeBitmapPixels, nullptr) &&
|
|
|
|
aImage->readPixels(bitmap.info(), bitmap.getPixels(),
|
|
|
|
bitmap.rowBytes(), 0, 0)) {
|
|
|
|
*aResultBitmap = bitmap;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2016-10-25 05:40:59 +03:00
|
|
|
}
|
|
|
|
|
2018-03-12 23:37:10 +03:00
|
|
|
gfxWarning() << "Failed reading alpha pixels for Skia bitmap";
|
|
|
|
return false;
|
2016-10-25 05:40:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static sk_sp<SkImage> ExtractAlphaForSurface(SourceSurface* aSurface) {
|
|
|
|
sk_sp<SkImage> image = GetSkImageForSurface(aSurface);
|
|
|
|
if (!image) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2018-03-12 23:37:10 +03:00
|
|
|
if (image->isAlphaOnly()) {
|
2016-10-25 05:40:59 +03:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2018-03-12 23:37:10 +03:00
|
|
|
static void SetPaintPattern(SkPaint& aPaint, const Pattern& aPattern,
|
|
|
|
Float aAlpha = 1.0,
|
|
|
|
const SkMatrix* aMatrix = nullptr,
|
|
|
|
const Rect* aBounds = nullptr) {
|
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());
|
2019-03-26 18:06:56 +03:00
|
|
|
if (!stops || stops->mCount < 2 || !pat.mBegin.IsFinite() ||
|
|
|
|
!pat.mEnd.IsFinite() || pat.mBegin == pat.mEnd) {
|
2016-06-30 03:01:48 +03:00
|
|
|
aPaint.setColor(SK_ColorTRANSPARENT);
|
|
|
|
} else {
|
2019-10-29 00:42:48 +03:00
|
|
|
SkTileMode mode = ExtendModeToTileMode(stops->mExtendMode, Axis::BOTH);
|
2012-01-19 08:48:33 +04:00
|
|
|
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));
|
|
|
|
|
2016-01-12 21:26:31 +03:00
|
|
|
SkMatrix mat;
|
|
|
|
GfxMatrixToSkiaMatrix(pat.mMatrix, mat);
|
2018-03-12 23:37:10 +03:00
|
|
|
if (aMatrix) {
|
|
|
|
mat.postConcat(*aMatrix);
|
|
|
|
}
|
2016-04-29 05:55:27 +03:00
|
|
|
sk_sp<SkShader> shader = SkGradientShader::MakeLinear(
|
|
|
|
points, &stops->mColors.front(), &stops->mPositions.front(),
|
|
|
|
stops->mCount, mode, 0, &mat);
|
2017-05-10 05:31:07 +03:00
|
|
|
if (shader) {
|
|
|
|
aPaint.setShader(shader);
|
|
|
|
} else {
|
|
|
|
aPaint.setColor(SK_ColorTRANSPARENT);
|
|
|
|
}
|
2012-01-19 08:48:33 +04:00
|
|
|
}
|
|
|
|
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());
|
2019-03-26 18:06:56 +03:00
|
|
|
if (!stops || stops->mCount < 2 || !pat.mCenter1.IsFinite() ||
|
|
|
|
!IsFinite(pat.mRadius1) || !pat.mCenter2.IsFinite() ||
|
|
|
|
!IsFinite(pat.mRadius2) ||
|
|
|
|
(pat.mCenter1 == pat.mCenter2 && pat.mRadius1 == pat.mRadius2)) {
|
2016-06-30 03:01:48 +03:00
|
|
|
aPaint.setColor(SK_ColorTRANSPARENT);
|
|
|
|
} else {
|
2019-10-29 00:42:48 +03:00
|
|
|
SkTileMode mode = ExtendModeToTileMode(stops->mExtendMode, Axis::BOTH);
|
2012-01-19 08:48:33 +04:00
|
|
|
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));
|
|
|
|
|
2016-01-12 21:26:31 +03:00
|
|
|
SkMatrix mat;
|
|
|
|
GfxMatrixToSkiaMatrix(pat.mMatrix, mat);
|
2018-03-12 23:37:10 +03:00
|
|
|
if (aMatrix) {
|
|
|
|
mat.postConcat(*aMatrix);
|
|
|
|
}
|
2016-04-29 05:55:27 +03:00
|
|
|
sk_sp<SkShader> shader = SkGradientShader::MakeTwoPointConical(
|
|
|
|
points[0], SkFloatToScalar(pat.mRadius1), points[1],
|
|
|
|
SkFloatToScalar(pat.mRadius2), &stops->mColors.front(),
|
|
|
|
&stops->mPositions.front(), stops->mCount, mode, 0, &mat);
|
2017-05-10 05:31:07 +03:00
|
|
|
if (shader) {
|
|
|
|
aPaint.setShader(shader);
|
|
|
|
} else {
|
|
|
|
aPaint.setColor(SK_ColorTRANSPARENT);
|
|
|
|
}
|
2012-01-19 08:48:33 +04:00
|
|
|
}
|
|
|
|
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);
|
2016-08-18 16:55:45 +03:00
|
|
|
sk_sp<SkImage> image =
|
|
|
|
GetSkImageForSurface(pat.mSurface, aBounds, &pat.mMatrix);
|
2017-01-06 23:23:17 +03:00
|
|
|
if (!image) {
|
|
|
|
aPaint.setColor(SK_ColorTRANSPARENT);
|
|
|
|
break;
|
|
|
|
}
|
2012-01-19 08:48:33 +04:00
|
|
|
|
|
|
|
SkMatrix mat;
|
|
|
|
GfxMatrixToSkiaMatrix(pat.mMatrix, mat);
|
2018-03-12 23:37:10 +03:00
|
|
|
if (aMatrix) {
|
|
|
|
mat.postConcat(*aMatrix);
|
|
|
|
}
|
2014-09-12 09:18:22 +04:00
|
|
|
|
|
|
|
if (!pat.mSamplingRect.IsEmpty()) {
|
2016-10-25 05:40:59 +03:00
|
|
|
image = ExtractSubset(image, pat.mSamplingRect);
|
2017-12-19 23:48:39 +03:00
|
|
|
mat.preTranslate(pat.mSamplingRect.X(), pat.mSamplingRect.Y());
|
2014-09-12 09:18:22 +04:00
|
|
|
}
|
|
|
|
|
2019-10-29 00:42:48 +03:00
|
|
|
SkTileMode xTile = ExtendModeToTileMode(pat.mExtendMode, Axis::X_AXIS);
|
|
|
|
SkTileMode yTile = ExtendModeToTileMode(pat.mExtendMode, Axis::Y_AXIS);
|
2015-11-23 19:17:35 +03:00
|
|
|
|
2019-10-29 00:42:48 +03:00
|
|
|
aPaint.setShader(image->makeShader(xTile, yTile, &mat));
|
2016-10-25 05:40:59 +03:00
|
|
|
|
2016-05-25 19:01:18 +03:00
|
|
|
if (pat.mSamplingFilter == SamplingFilter::POINT) {
|
2015-12-18 21:53:25 +03:00
|
|
|
aPaint.setFilterQuality(kNone_SkFilterQuality);
|
2012-01-19 08:48:33 +04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-01 12:59:20 +04:00
|
|
|
static inline Rect GetClipBounds(SkCanvas* aCanvas) {
|
2016-08-18 18:25:38 +03:00
|
|
|
// Use a manually transformed getClipDeviceBounds instead of
|
|
|
|
// getClipBounds because getClipBounds inflates the the bounds
|
|
|
|
// by a pixel in each direction to compensate for antialiasing.
|
|
|
|
SkIRect deviceBounds;
|
2017-05-10 05:31:07 +03:00
|
|
|
if (!aCanvas->getDeviceClipBounds(&deviceBounds)) {
|
2016-04-13 23:34:07 +03:00
|
|
|
return Rect();
|
|
|
|
}
|
2016-08-18 18:25:38 +03:00
|
|
|
SkMatrix inverseCTM;
|
|
|
|
if (!aCanvas->getTotalMatrix().invert(&inverseCTM)) {
|
|
|
|
return Rect();
|
|
|
|
}
|
|
|
|
SkRect localBounds;
|
|
|
|
inverseCTM.mapRect(&localBounds, SkRect::Make(deviceBounds));
|
|
|
|
return SkRectToRect(localBounds);
|
2014-04-01 12:59:20 +04:00
|
|
|
}
|
|
|
|
|
2011-11-02 23:55:03 +04:00
|
|
|
struct AutoPaintSetup {
|
2018-03-12 23:37:10 +03:00
|
|
|
AutoPaintSetup(SkCanvas* aCanvas, const DrawOptions& aOptions,
|
|
|
|
const Pattern& aPattern, const Rect* aMaskBounds = nullptr,
|
|
|
|
const SkMatrix* aMatrix = nullptr,
|
|
|
|
const Rect* aSourceBounds = nullptr)
|
2011-11-02 23:55:03 +04:00
|
|
|
: mNeedsRestore(false), mAlpha(1.0) {
|
2016-08-26 07:42:12 +03:00
|
|
|
Init(aCanvas, aOptions, aMaskBounds, false);
|
2018-03-12 23:37:10 +03:00
|
|
|
SetPaintPattern(mPaint, aPattern, mAlpha, aMatrix, aSourceBounds);
|
2011-11-02 23:55:03 +04:00
|
|
|
}
|
|
|
|
|
2016-08-26 07:42:12 +03:00
|
|
|
AutoPaintSetup(SkCanvas* aCanvas, const DrawOptions& aOptions,
|
|
|
|
const Rect* aMaskBounds = nullptr, bool aForceGroup = false)
|
2011-11-02 23:55:03 +04:00
|
|
|
: mNeedsRestore(false), mAlpha(1.0) {
|
2016-08-26 07:42:12 +03:00
|
|
|
Init(aCanvas, aOptions, aMaskBounds, aForceGroup);
|
2011-11-02 23:55:03 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
~AutoPaintSetup() {
|
|
|
|
if (mNeedsRestore) {
|
|
|
|
mCanvas->restore();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-26 07:42:12 +03:00
|
|
|
void Init(SkCanvas* aCanvas, const DrawOptions& aOptions,
|
|
|
|
const Rect* aMaskBounds, bool aForceGroup) {
|
2016-10-25 05:40:59 +03:00
|
|
|
mPaint.setBlendMode(GfxOpToSkiaOp(aOptions.mCompositionOp));
|
2011-11-02 23:55:03 +04:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2016-08-26 07:42:12 +03:00
|
|
|
bool needsGroup =
|
|
|
|
aForceGroup ||
|
|
|
|
(!IsOperatorBoundByMask(aOptions.mCompositionOp) &&
|
|
|
|
(!aMaskBounds || !aMaskBounds->Contains(GetClipBounds(aCanvas))));
|
2014-04-01 12:59:20 +04:00
|
|
|
|
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) {
|
2016-10-25 05:40:59 +03:00
|
|
|
mPaint.setBlendMode(SkBlendMode::kSrcOver);
|
2011-11-02 23:55:03 +04:00
|
|
|
SkPaint temp;
|
2016-10-25 05:40:59 +03:00
|
|
|
temp.setBlendMode(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
|
2019-03-26 18:06:56 +03:00
|
|
|
SkCanvas::SaveLayerRec rec(nullptr, &temp,
|
|
|
|
SkCanvas::kPreserveLCDText_SaveLayerFlag);
|
|
|
|
mCanvas->saveLayer(rec);
|
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;
|
|
|
|
}
|
2015-12-18 21:53:25 +03:00
|
|
|
mPaint.setFilterQuality(kLow_SkFilterQuality);
|
2011-11-02 23:55:03 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Maybe add an operator overload to access this easier?
|
|
|
|
SkPaint mPaint;
|
|
|
|
bool mNeedsRestore;
|
|
|
|
SkCanvas* mCanvas;
|
|
|
|
Float mAlpha;
|
|
|
|
};
|
|
|
|
|
|
|
|
void DrawTargetSkia::Flush() { mCanvas->flush(); }
|
|
|
|
|
|
|
|
void DrawTargetSkia::DrawSurface(SourceSurface* aSurface, const Rect& aDest,
|
|
|
|
const Rect& aSource,
|
|
|
|
const DrawSurfaceOptions& aSurfOptions,
|
|
|
|
const DrawOptions& aOptions) {
|
2016-09-02 04:17:03 +03:00
|
|
|
if (aSource.IsEmpty()) {
|
|
|
|
return;
|
2013-12-03 17:52:06 +04:00
|
|
|
}
|
|
|
|
|
2016-09-16 18:03:48 +03:00
|
|
|
MarkChanged();
|
|
|
|
|
2016-10-25 05:40:59 +03:00
|
|
|
sk_sp<SkImage> image = GetSkImageForSurface(aSurface);
|
|
|
|
if (!image) {
|
2011-11-02 23:55:03 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
SkRect destRect = RectToSkRect(aDest);
|
|
|
|
SkRect sourceRect = RectToSkRect(aSource);
|
2018-03-12 23:37:10 +03:00
|
|
|
bool forceGroup =
|
2016-08-26 07:42:12 +03:00
|
|
|
image->isAlphaOnly() && aOptions.mCompositionOp != CompositionOp::OP_OVER;
|
2014-10-08 08:16:14 +04:00
|
|
|
|
2017-05-10 05:31:07 +03:00
|
|
|
AutoPaintSetup paint(mCanvas, aOptions, &aDest, forceGroup);
|
2016-05-25 19:01:18 +03:00
|
|
|
if (aSurfOptions.mSamplingFilter == SamplingFilter::POINT) {
|
2015-12-18 21:53:25 +03:00
|
|
|
paint.mPaint.setFilterQuality(kNone_SkFilterQuality);
|
2011-11-18 08:00:38 +04:00
|
|
|
}
|
2012-11-16 04:57:40 +04:00
|
|
|
|
2016-10-25 05:40:59 +03:00
|
|
|
mCanvas->drawImageRect(image, sourceRect, destRect, &paint.mPaint);
|
2011-11-02 23:55:03 +04:00
|
|
|
}
|
|
|
|
|
2014-06-27 13:17:49 +04:00
|
|
|
DrawTargetType DrawTargetSkia::GetType() const {
|
|
|
|
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) {
|
2016-09-02 04:17:03 +03:00
|
|
|
if (aSurface->GetSize().IsEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-09-16 18:03:48 +03:00
|
|
|
MarkChanged();
|
|
|
|
|
2016-10-25 05:40:59 +03:00
|
|
|
sk_sp<SkImage> image = GetSkImageForSurface(aSurface);
|
|
|
|
if (!image) {
|
2014-03-07 01:33:39 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-07-28 04:51:09 +04:00
|
|
|
mCanvas->save();
|
2011-11-02 23:55:03 +04:00
|
|
|
mCanvas->resetMatrix();
|
|
|
|
|
|
|
|
SkPaint paint;
|
2016-10-25 05:40:59 +03:00
|
|
|
paint.setBlendMode(GfxOpToSkiaOp(aOperator));
|
2011-11-02 23:55:03 +04:00
|
|
|
|
2015-09-04 22:29:11 +03:00
|
|
|
// bug 1201272
|
|
|
|
// We can't use the SkDropShadowImageFilter here because it applies the xfer
|
|
|
|
// mode first to render the bitmap to a temporary layer, and then implicitly
|
|
|
|
// uses src-over to composite the resulting shadow.
|
|
|
|
// The canvas spec, however, states that the composite op must be used to
|
|
|
|
// composite the resulting shadow, so we must instead use a SkBlurImageFilter
|
|
|
|
// to blur the image ourselves.
|
|
|
|
|
|
|
|
SkPaint shadowPaint;
|
2016-10-25 05:40:59 +03:00
|
|
|
shadowPaint.setBlendMode(GfxOpToSkiaOp(aOperator));
|
2015-09-04 22:29:11 +03:00
|
|
|
|
2016-07-26 17:48:34 +03:00
|
|
|
auto shadowDest = IntPoint::Round(aDest + aOffset);
|
2016-03-26 05:14:50 +03:00
|
|
|
|
|
|
|
SkBitmap blurMask;
|
2019-02-28 17:59:16 +03:00
|
|
|
if (ExtractAlphaBitmap(image, &blurMask)) {
|
|
|
|
// Prefer using our own box blur instead of Skia's. It currently performs
|
|
|
|
// much better than SkBlurImageFilter or SkBlurMaskFilter on the CPU.
|
2016-03-26 05:14:50 +03:00
|
|
|
AlphaBoxBlur blur(Rect(0, 0, blurMask.width(), blurMask.height()),
|
|
|
|
int32_t(blurMask.rowBytes()), aSigma, aSigma);
|
|
|
|
blur.Blur(reinterpret_cast<uint8_t*>(blurMask.getPixels()));
|
2016-10-25 02:45:46 +03:00
|
|
|
blurMask.notifyPixelsChanged();
|
2016-03-26 05:14:50 +03:00
|
|
|
|
|
|
|
shadowPaint.setColor(ColorToSkColor(aColor, 1.0f));
|
|
|
|
|
|
|
|
mCanvas->drawBitmap(blurMask, shadowDest.x, shadowDest.y, &shadowPaint);
|
|
|
|
} else {
|
2016-04-29 05:55:27 +03:00
|
|
|
sk_sp<SkImageFilter> blurFilter(
|
|
|
|
SkBlurImageFilter::Make(aSigma, aSigma, nullptr));
|
2019-10-29 00:42:48 +03:00
|
|
|
sk_sp<SkColorFilter> colorFilter(SkColorFilters::Blend(
|
2016-10-25 05:40:59 +03:00
|
|
|
ColorToSkColor(aColor, 1.0f), SkBlendMode::kSrcIn));
|
2016-03-26 05:14:50 +03:00
|
|
|
|
2016-04-29 05:55:27 +03:00
|
|
|
shadowPaint.setImageFilter(blurFilter);
|
|
|
|
shadowPaint.setColorFilter(colorFilter);
|
2016-03-26 05:14:50 +03:00
|
|
|
|
2016-10-25 05:40:59 +03:00
|
|
|
mCanvas->drawImage(image, shadowDest.x, shadowDest.y, &shadowPaint);
|
2016-03-26 05:14:50 +03:00
|
|
|
}
|
2015-09-04 22:29:11 +03:00
|
|
|
|
2016-11-21 21:17:09 +03:00
|
|
|
if (aSurface->GetFormat() != SurfaceFormat::A8) {
|
|
|
|
// Composite the original image after the shadow
|
|
|
|
auto dest = IntPoint::Round(aDest);
|
|
|
|
mCanvas->drawImage(image, dest.x, dest.y, &paint);
|
|
|
|
}
|
2015-09-04 22:29:11 +03:00
|
|
|
|
2011-11-02 23:55:03 +04:00
|
|
|
mCanvas->restore();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DrawTargetSkia::FillRect(const Rect& aRect, const Pattern& aPattern,
|
|
|
|
const DrawOptions& aOptions) {
|
2016-08-23 20:36:45 +03:00
|
|
|
// The sprite blitting path in Skia can be faster than the shader blitter for
|
|
|
|
// operators other than source (or source-over with opaque surface). So, when
|
|
|
|
// possible/beneficial, route to DrawSurface which will use the sprite
|
|
|
|
// blitter.
|
|
|
|
if (aPattern.GetType() == PatternType::SURFACE &&
|
|
|
|
aOptions.mCompositionOp != CompositionOp::OP_SOURCE) {
|
|
|
|
const SurfacePattern& pat = static_cast<const SurfacePattern&>(aPattern);
|
|
|
|
// Verify there is a valid surface and a pattern matrix without skew.
|
|
|
|
if (pat.mSurface &&
|
|
|
|
(aOptions.mCompositionOp != CompositionOp::OP_OVER ||
|
|
|
|
GfxFormatToSkiaAlphaType(pat.mSurface->GetFormat()) !=
|
|
|
|
kOpaque_SkAlphaType) &&
|
|
|
|
!pat.mMatrix.HasNonAxisAlignedTransform()) {
|
|
|
|
// Bound the sampling to smaller of the bounds or the sampling rect.
|
|
|
|
IntRect srcRect(IntPoint(0, 0), pat.mSurface->GetSize());
|
|
|
|
if (!pat.mSamplingRect.IsEmpty()) {
|
|
|
|
srcRect = srcRect.Intersect(pat.mSamplingRect);
|
|
|
|
}
|
|
|
|
// Transform the destination rectangle by the inverse of the pattern
|
|
|
|
// matrix so that it is in pattern space like the source rectangle.
|
|
|
|
Rect patRect = aRect - pat.mMatrix.GetTranslation();
|
|
|
|
patRect.Scale(1.0f / pat.mMatrix._11, 1.0f / pat.mMatrix._22);
|
|
|
|
// Verify the pattern rectangle will not tile or clamp.
|
|
|
|
if (!patRect.IsEmpty() && srcRect.Contains(RoundedOut(patRect))) {
|
|
|
|
// The pattern is a surface with an axis-aligned source rectangle
|
|
|
|
// fitting entirely in its bounds, so just treat it as a DrawSurface.
|
|
|
|
DrawSurface(pat.mSurface, aRect, patRect,
|
|
|
|
DrawSurfaceOptions(pat.mSamplingFilter), aOptions);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-02 23:55:03 +04:00
|
|
|
MarkChanged();
|
2011-11-02 23:55:03 +04:00
|
|
|
SkRect rect = RectToSkRect(aRect);
|
2018-03-12 23:37:10 +03:00
|
|
|
AutoPaintSetup paint(mCanvas, aOptions, aPattern, &aRect, nullptr, &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);
|
|
|
|
|
2017-05-10 05:31:07 +03:00
|
|
|
AutoPaintSetup paint(mCanvas, aOptions, aPattern);
|
2011-11-18 08:00:38 +04:00
|
|
|
if (!StrokeOptionsToPaint(paint.mPaint, aStrokeOptions)) {
|
|
|
|
return;
|
|
|
|
}
|
2011-11-02 23:55:03 +04:00
|
|
|
|
2015-12-30 21:39:13 +03:00
|
|
|
if (!skiaPath->GetPath().isFinite()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-11-02 23:55:03 +04:00
|
|
|
mCanvas->drawPath(skiaPath->GetPath(), paint.mPaint);
|
|
|
|
}
|
|
|
|
|
2017-02-28 23:47:46 +03:00
|
|
|
static Double DashPeriodLength(const StrokeOptions& aStrokeOptions) {
|
2017-05-23 06:53:04 +03:00
|
|
|
Double length = 0;
|
2017-02-28 23:47:46 +03:00
|
|
|
for (size_t i = 0; i < aStrokeOptions.mDashLength; i++) {
|
|
|
|
length += aStrokeOptions.mDashPattern[i];
|
|
|
|
}
|
|
|
|
if (aStrokeOptions.mDashLength & 1) {
|
|
|
|
// "If an odd number of values is provided, then the list of values is
|
|
|
|
// repeated to yield an even number of values."
|
|
|
|
// Double the length.
|
|
|
|
length += length;
|
|
|
|
}
|
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
2017-05-23 06:53:04 +03:00
|
|
|
static inline Double RoundDownToMultiple(Double aValue, Double aFactor) {
|
|
|
|
return floor(aValue / aFactor) * aFactor;
|
2017-02-28 23:47:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static Rect UserSpaceStrokeClip(const IntRect& aDeviceClip,
|
|
|
|
const Matrix& aTransform,
|
|
|
|
const StrokeOptions& aStrokeOptions) {
|
|
|
|
Matrix inverse = aTransform;
|
|
|
|
if (!inverse.Invert()) {
|
|
|
|
return Rect();
|
|
|
|
}
|
|
|
|
Rect deviceClip(aDeviceClip);
|
|
|
|
deviceClip.Inflate(MaxStrokeExtents(aStrokeOptions, aTransform));
|
|
|
|
return inverse.TransformBounds(deviceClip);
|
|
|
|
}
|
|
|
|
|
|
|
|
static Rect ShrinkClippedStrokedRect(const Rect& aStrokedRect,
|
|
|
|
const IntRect& aDeviceClip,
|
|
|
|
const Matrix& aTransform,
|
|
|
|
const StrokeOptions& aStrokeOptions) {
|
|
|
|
Rect userSpaceStrokeClip =
|
|
|
|
UserSpaceStrokeClip(aDeviceClip, aTransform, aStrokeOptions);
|
2017-12-19 23:48:39 +03:00
|
|
|
RectDouble strokedRectDouble(aStrokedRect.X(), aStrokedRect.Y(),
|
|
|
|
aStrokedRect.Width(), aStrokedRect.Height());
|
2017-05-23 06:53:04 +03:00
|
|
|
RectDouble intersection = strokedRectDouble.Intersect(
|
2017-12-19 23:48:39 +03:00
|
|
|
RectDouble(userSpaceStrokeClip.X(), userSpaceStrokeClip.Y(),
|
2017-08-14 15:29:28 +03:00
|
|
|
userSpaceStrokeClip.Width(), userSpaceStrokeClip.Height()));
|
2017-05-23 06:53:04 +03:00
|
|
|
Double dashPeriodLength = DashPeriodLength(aStrokeOptions);
|
2017-02-28 23:47:46 +03:00
|
|
|
if (intersection.IsEmpty() || dashPeriodLength == 0.0f) {
|
2017-12-19 23:48:39 +03:00
|
|
|
return Rect(intersection.X(), intersection.Y(), intersection.Width(),
|
|
|
|
intersection.Height());
|
2017-02-28 23:47:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Reduce the rectangle side lengths in multiples of the dash period length
|
|
|
|
// so that the visible dashes stay in the same place.
|
2017-05-23 06:53:04 +03:00
|
|
|
MarginDouble insetBy = strokedRectDouble - intersection;
|
2017-02-28 23:47:46 +03:00
|
|
|
insetBy.top = RoundDownToMultiple(insetBy.top, dashPeriodLength);
|
|
|
|
insetBy.right = RoundDownToMultiple(insetBy.right, dashPeriodLength);
|
|
|
|
insetBy.bottom = RoundDownToMultiple(insetBy.bottom, dashPeriodLength);
|
|
|
|
insetBy.left = RoundDownToMultiple(insetBy.left, dashPeriodLength);
|
|
|
|
|
2017-05-23 06:53:04 +03:00
|
|
|
strokedRectDouble.Deflate(insetBy);
|
2017-12-19 23:48:39 +03:00
|
|
|
return Rect(strokedRectDouble.X(), strokedRectDouble.Y(),
|
2017-08-14 15:29:28 +03:00
|
|
|
strokedRectDouble.Width(), strokedRectDouble.Height());
|
2017-02-28 23:47:46 +03:00
|
|
|
}
|
|
|
|
|
2011-11-02 23:55:03 +04:00
|
|
|
void DrawTargetSkia::StrokeRect(const Rect& aRect, const Pattern& aPattern,
|
|
|
|
const StrokeOptions& aStrokeOptions,
|
|
|
|
const DrawOptions& aOptions) {
|
2017-02-28 23:47:46 +03:00
|
|
|
// Stroking large rectangles with dashes is expensive with Skia (fixed
|
|
|
|
// overhead based on the number of dashes, regardless of whether the dashes
|
|
|
|
// are visible), so we try to reduce the size of the stroked rectangle as
|
|
|
|
// much as possible before passing it on to Skia.
|
|
|
|
Rect rect = aRect;
|
|
|
|
if (aStrokeOptions.mDashLength > 0 && !rect.IsEmpty()) {
|
|
|
|
IntRect deviceClip(IntPoint(0, 0), mSize);
|
|
|
|
SkIRect clipBounds;
|
2017-05-10 05:31:07 +03:00
|
|
|
if (mCanvas->getDeviceClipBounds(&clipBounds)) {
|
2017-02-28 23:47:46 +03:00
|
|
|
deviceClip = SkIRectToIntRect(clipBounds);
|
|
|
|
}
|
|
|
|
rect =
|
|
|
|
ShrinkClippedStrokedRect(rect, deviceClip, mTransform, aStrokeOptions);
|
|
|
|
if (rect.IsEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-02 23:55:03 +04:00
|
|
|
MarkChanged();
|
2017-05-10 05:31:07 +03:00
|
|
|
AutoPaintSetup paint(mCanvas, aOptions, aPattern);
|
2011-11-18 08:00:38 +04:00
|
|
|
if (!StrokeOptionsToPaint(paint.mPaint, aStrokeOptions)) {
|
|
|
|
return;
|
|
|
|
}
|
2011-11-02 23:55:03 +04:00
|
|
|
|
2017-02-28 23:47:46 +03:00
|
|
|
mCanvas->drawRect(RectToSkRect(rect), paint.mPaint);
|
2011-11-02 23:55:03 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void 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();
|
2017-05-10 05:31:07 +03:00
|
|
|
AutoPaintSetup paint(mCanvas, 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();
|
2017-01-16 21:21:36 +03:00
|
|
|
if (!aPath || aPath->GetBackendType() != BackendType::SKIA) {
|
2011-11-02 23:55:03 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const PathSkia* skiaPath = static_cast<const PathSkia*>(aPath);
|
|
|
|
|
2017-05-10 05:31:07 +03:00
|
|
|
AutoPaintSetup paint(mCanvas, aOptions, aPattern);
|
2011-11-02 23:55:03 +04:00
|
|
|
|
2015-12-30 21:39:13 +03:00
|
|
|
if (!skiaPath->GetPath().isFinite()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-11-02 23:55:03 +04:00
|
|
|
mCanvas->drawPath(skiaPath->GetPath(), paint.mPaint);
|
|
|
|
}
|
|
|
|
|
2016-05-17 21:41:38 +03:00
|
|
|
#ifdef MOZ_WIDGET_COCOA
|
2016-10-19 06:22:44 +03:00
|
|
|
static inline CGAffineTransform GfxMatrixToCGAffineTransform(const Matrix& m) {
|
|
|
|
CGAffineTransform t;
|
|
|
|
t.a = m._11;
|
|
|
|
t.b = m._12;
|
|
|
|
t.c = m._21;
|
|
|
|
t.d = m._22;
|
|
|
|
t.tx = m._31;
|
|
|
|
t.ty = m._32;
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
2016-05-17 21:41:38 +03:00
|
|
|
/***
|
|
|
|
* We have to do a lot of work to draw glyphs with CG because
|
|
|
|
* CG assumes that the origin of rects are in the bottom left
|
|
|
|
* while every other DrawTarget assumes the top left is the origin.
|
|
|
|
* This means we have to transform the CGContext to have rects
|
|
|
|
* actually be applied in top left fashion. We do this by:
|
|
|
|
*
|
|
|
|
* 1) Translating the context up by the height of the canvas
|
|
|
|
* 2) Flipping the context by the Y axis so it's upside down.
|
|
|
|
*
|
|
|
|
* These two transforms put the origin in the top left.
|
|
|
|
* Transforms are better understood thinking about them from right to left order
|
|
|
|
* (mathematically).
|
|
|
|
*
|
|
|
|
* Consider a point we want to draw at (0, 10) in normal cartesian planes with
|
|
|
|
* a box of (100, 100). in CG terms, this would be at (0, 10).
|
|
|
|
* Positive Y values point up.
|
|
|
|
* In our DrawTarget terms, positive Y values point down, so (0, 10) would be
|
|
|
|
* at (0, 90) in cartesian plane terms. That means our point at (0, 10) in
|
|
|
|
* DrawTarget terms should end up at (0, 90). How does this work with the
|
|
|
|
* current transforms?
|
|
|
|
*
|
|
|
|
* Going right to left with the transforms, a CGPoint of (0, 10) has cartesian
|
|
|
|
* coordinates of (0, 10). The first flip of the Y axis puts the point now at
|
|
|
|
* (0, -10); Next, we translate the context up by the size of the canvas
|
|
|
|
* (Positive Y values go up in CG coordinates but down in our draw target
|
|
|
|
* coordinates). Since our canvas size is (100, 100), the resulting coordinate
|
|
|
|
* becomes (0, 90), which is what we expect from our DrawTarget code. These two
|
|
|
|
* transforms put the CG context equal to what every other DrawTarget expects.
|
|
|
|
*
|
|
|
|
* Next, we need two more transforms for actual text. IF we left the transforms
|
|
|
|
* as is, the text would be drawn upside down, so we need another flip of the Y
|
|
|
|
* axis to draw the text right side up. However, with only the flip, the text
|
|
|
|
* would be drawn in the wrong place. Thus we also have to invert the Y position
|
|
|
|
* of the glyphs to get them in the right place.
|
|
|
|
*
|
|
|
|
* Thus we have the following transforms:
|
|
|
|
* 1) Translation of the context up
|
|
|
|
* 2) Flipping the context around the Y axis
|
|
|
|
* 3) Flipping the context around the Y axis
|
|
|
|
* 4) Inverting the Y position of each glyph
|
|
|
|
*
|
|
|
|
* We cannot cancel out (2) and (3) as we have to apply the clips and transforms
|
|
|
|
* of DrawTargetSkia between (2) and (3).
|
|
|
|
*
|
2018-11-28 03:54:56 +03:00
|
|
|
* Consider the example letter P, drawn at (0, 20) in CG coordinates in a
|
|
|
|
* (100, 100) rect.
|
2016-05-17 21:41:38 +03:00
|
|
|
* Again, going right to left of the transforms. We'd get:
|
|
|
|
*
|
|
|
|
* 1) The letter P drawn at (0, -20) due to the inversion of the Y axis
|
|
|
|
* 2) The letter P upside down (b) at (0, 20) due to the second flip
|
|
|
|
* 3) The letter P right side up at (0, -20) due to the first flip
|
|
|
|
* 4) The letter P right side up at (0, 80) due to the translation
|
|
|
|
*
|
|
|
|
* tl;dr - CGRects assume origin is bottom left, DrawTarget rects assume top
|
|
|
|
* left.
|
|
|
|
*/
|
|
|
|
static bool SetupCGContext(DrawTargetSkia* aDT, CGContextRef aCGContext,
|
2016-12-16 19:02:04 +03:00
|
|
|
SkCanvas* aCanvas, const IntPoint& aOrigin,
|
2017-06-10 04:40:13 +03:00
|
|
|
const IntSize& aSize, bool aClipped) {
|
2016-05-17 21:41:38 +03:00
|
|
|
// 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
|
|
|
|
// the top left. Have to set this before we do anything else.
|
|
|
|
// This is transform (1) up top
|
2016-12-16 19:02:04 +03:00
|
|
|
CGContextTranslateCTM(aCGContext, -aOrigin.x, aOrigin.y + aSize.height);
|
2016-05-17 21:41:38 +03:00
|
|
|
|
|
|
|
// Transform (2) from the comments.
|
|
|
|
CGContextScaleCTM(aCGContext, 1, -1);
|
|
|
|
|
|
|
|
// Want to apply clips BEFORE the transform since the transform
|
|
|
|
// will apply to the clips we apply.
|
2017-06-10 04:40:13 +03:00
|
|
|
if (aClipped) {
|
|
|
|
SkRegion clipRegion;
|
|
|
|
aCanvas->temporary_internal_getRgnClip(&clipRegion);
|
|
|
|
Vector<CGRect, 8> rects;
|
|
|
|
for (SkRegion::Iterator it(clipRegion); !it.done(); it.next()) {
|
|
|
|
const SkIRect& rect = it.rect();
|
|
|
|
if (!rects.append(
|
|
|
|
CGRectMake(rect.x(), rect.y(), rect.width(), rect.height()))) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (rects.length()) {
|
|
|
|
CGContextClipToRects(aCGContext, rects.begin(), rects.length());
|
|
|
|
}
|
2017-05-10 05:31:07 +03:00
|
|
|
}
|
|
|
|
|
2016-05-17 21:41:38 +03:00
|
|
|
CGContextConcatCTM(aCGContext,
|
|
|
|
GfxMatrixToCGAffineTransform(aDT->GetTransform()));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool SetupCGGlyphs(CGContextRef aCGContext, const GlyphBuffer& aBuffer,
|
|
|
|
Vector<CGGlyph, 32>& aGlyphs,
|
|
|
|
Vector<CGPoint, 32>& aPositions) {
|
|
|
|
// Flip again so we draw text in right side up. Transform (3) from the top
|
|
|
|
CGContextScaleCTM(aCGContext, 1, -1);
|
|
|
|
|
|
|
|
if (!aGlyphs.resizeUninitialized(aBuffer.mNumGlyphs) ||
|
|
|
|
!aPositions.resizeUninitialized(aBuffer.mNumGlyphs)) {
|
|
|
|
gfxDevCrash(LogReason::GlyphAllocFailedCG)
|
|
|
|
<< "glyphs/positions allocation failed";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (unsigned int i = 0; i < aBuffer.mNumGlyphs; i++) {
|
|
|
|
aGlyphs[i] = aBuffer.mGlyphs[i].mIndex;
|
|
|
|
|
|
|
|
// Flip the y coordinates so that text ends up in the right spot after the
|
|
|
|
// (3) flip Inversion from (4) in the comments.
|
|
|
|
aPositions[i] = CGPointMake(aBuffer.mGlyphs[i].mPosition.x,
|
|
|
|
-aBuffer.mGlyphs[i].mPosition.y);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// End long comment about transforms. SetupCGContext and SetupCGGlyphs should
|
|
|
|
// stay next to each other.
|
|
|
|
|
|
|
|
// The context returned from this method will have the origin
|
2017-05-10 05:31:07 +03:00
|
|
|
// in the top left and will have applied all the neccessary clips
|
2016-05-17 21:41:38 +03:00
|
|
|
// and transforms to the CGContext. See the comment above
|
|
|
|
// SetupCGContext.
|
|
|
|
CGContextRef DrawTargetSkia::BorrowCGContext(const DrawOptions& aOptions) {
|
2017-05-10 23:21:13 +03:00
|
|
|
// Since we can't replay Skia clips, we have to use a layer if we have a
|
|
|
|
// complex clip. After saving a layer, the SkCanvas queries for needing a
|
|
|
|
// layer change so save if we pushed a layer.
|
|
|
|
mNeedLayer = !mCanvas->isClipEmpty() && !mCanvas->isClipRect();
|
|
|
|
if (mNeedLayer) {
|
2017-05-10 05:31:07 +03:00
|
|
|
SkPaint paint;
|
|
|
|
paint.setBlendMode(SkBlendMode::kSrc);
|
|
|
|
SkCanvas::SaveLayerRec rec(nullptr, &paint,
|
|
|
|
SkCanvas::kInitWithPrevious_SaveLayerFlag);
|
|
|
|
mCanvas->saveLayer(rec);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t* data = nullptr;
|
2016-05-17 21:41:38 +03:00
|
|
|
int32_t stride;
|
|
|
|
SurfaceFormat format;
|
|
|
|
IntSize size;
|
2016-12-16 19:02:04 +03:00
|
|
|
IntPoint origin;
|
2017-05-10 05:31:07 +03:00
|
|
|
if (!LockBits(&data, &size, &stride, &format, &origin)) {
|
2016-05-17 21:41:38 +03:00
|
|
|
NS_WARNING("Could not lock skia bits to wrap CG around");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2017-05-10 23:21:13 +03:00
|
|
|
if (!mNeedLayer && (data == mCanvasData) && mCG && (mCGSize == size)) {
|
2016-05-17 21:41:38 +03:00
|
|
|
// If our canvas data still points to the same data,
|
|
|
|
// we can reuse the CG Context
|
|
|
|
CGContextSetAlpha(mCG, aOptions.mAlpha);
|
2017-06-10 04:40:13 +03:00
|
|
|
CGContextSetShouldAntialias(mCG,
|
|
|
|
aOptions.mAntialiasMode != AntialiasMode::NONE);
|
|
|
|
CGContextSaveGState(mCG);
|
|
|
|
SetupCGContext(this, mCG, mCanvas, origin, size, true);
|
2016-05-17 21:41:38 +03:00
|
|
|
return mCG;
|
|
|
|
}
|
|
|
|
|
2016-05-19 03:09:14 +03:00
|
|
|
if (!mColorSpace) {
|
2016-08-03 17:53:12 +03:00
|
|
|
mColorSpace = (format == SurfaceFormat::A8) ? CGColorSpaceCreateDeviceGray()
|
|
|
|
: CGColorSpaceCreateDeviceRGB();
|
2016-05-19 03:09:14 +03:00
|
|
|
}
|
|
|
|
|
2016-05-17 21:41:38 +03:00
|
|
|
if (mCG) {
|
|
|
|
// Release the old CG context since it's no longer valid.
|
|
|
|
CGContextRelease(mCG);
|
|
|
|
}
|
|
|
|
|
2017-05-10 05:31:07 +03:00
|
|
|
mCanvasData = data;
|
2016-05-17 21:41:38 +03:00
|
|
|
mCGSize = size;
|
|
|
|
|
2016-08-03 17:53:12 +03:00
|
|
|
uint32_t bitmapInfo =
|
|
|
|
(format == SurfaceFormat::A8)
|
|
|
|
? kCGImageAlphaOnly
|
|
|
|
: kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Host;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2016-05-17 21:41:38 +03:00
|
|
|
mCG = CGBitmapContextCreateWithData(
|
|
|
|
mCanvasData, mCGSize.width, mCGSize.height, 8, /* bits per component */
|
|
|
|
stride, mColorSpace, bitmapInfo, NULL, /* Callback when released */
|
|
|
|
NULL);
|
|
|
|
if (!mCG) {
|
2017-05-10 23:21:13 +03:00
|
|
|
if (mNeedLayer) {
|
2017-05-10 05:31:07 +03:00
|
|
|
mCanvas->restore();
|
|
|
|
}
|
2016-05-17 21:41:38 +03:00
|
|
|
ReleaseBits(mCanvasData);
|
|
|
|
NS_WARNING("Could not create bitmap around skia data\n");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
CGContextSetAlpha(mCG, aOptions.mAlpha);
|
|
|
|
CGContextSetShouldAntialias(mCG,
|
|
|
|
aOptions.mAntialiasMode != AntialiasMode::NONE);
|
|
|
|
CGContextSetShouldSmoothFonts(mCG, true);
|
|
|
|
CGContextSetTextDrawingMode(mCG, kCGTextFill);
|
|
|
|
CGContextSaveGState(mCG);
|
2017-06-10 04:40:13 +03:00
|
|
|
SetupCGContext(this, mCG, mCanvas, origin, size, !mNeedLayer);
|
2016-05-17 21:41:38 +03:00
|
|
|
return mCG;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DrawTargetSkia::ReturnCGContext(CGContextRef aCGContext) {
|
|
|
|
MOZ_ASSERT(aCGContext == mCG);
|
|
|
|
ReleaseBits(mCanvasData);
|
|
|
|
CGContextRestoreGState(aCGContext);
|
2017-05-10 05:31:07 +03:00
|
|
|
|
2017-05-10 23:21:13 +03:00
|
|
|
if (mNeedLayer) {
|
2017-05-10 05:31:07 +03:00
|
|
|
// A layer was used for clipping and is about to be popped by the restore.
|
|
|
|
// Make sure the CG context referencing it is released first so the popped
|
|
|
|
// layer doesn't accidentally get used.
|
|
|
|
if (mCG) {
|
|
|
|
CGContextRelease(mCG);
|
|
|
|
mCG = nullptr;
|
|
|
|
}
|
|
|
|
mCanvas->restore();
|
|
|
|
}
|
2016-05-17 21:41:38 +03:00
|
|
|
}
|
|
|
|
|
2016-08-03 17:53:12 +03:00
|
|
|
CGContextRef BorrowedCGContext::BorrowCGContextFromDrawTarget(DrawTarget* aDT) {
|
2016-10-19 06:22:44 +03:00
|
|
|
DrawTargetSkia* skiaDT = static_cast<DrawTargetSkia*>(aDT);
|
|
|
|
return skiaDT->BorrowCGContext(DrawOptions());
|
2016-08-03 17:53:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void BorrowedCGContext::ReturnCGContextToDrawTarget(DrawTarget* aDT,
|
|
|
|
CGContextRef cg) {
|
2016-10-19 06:22:44 +03:00
|
|
|
DrawTargetSkia* skiaDT = static_cast<DrawTargetSkia*>(aDT);
|
|
|
|
skiaDT->ReturnCGContext(cg);
|
|
|
|
return;
|
2016-08-03 17:53:12 +03:00
|
|
|
}
|
|
|
|
|
2016-05-17 21:41:38 +03:00
|
|
|
static void SetFontColor(CGContextRef aCGContext, CGColorSpaceRef aColorSpace,
|
|
|
|
const Pattern& aPattern) {
|
|
|
|
const Color& color = static_cast<const ColorPattern&>(aPattern).mColor;
|
|
|
|
CGColorRef textColor = ColorToCGColor(aColorSpace, color);
|
|
|
|
CGContextSetFillColorWithColor(aCGContext, textColor);
|
|
|
|
CGColorRelease(textColor);
|
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
* We need this to support subpixel AA text on OS X in two cases:
|
|
|
|
* text in DrawTargets that are not opaque and text over vibrant backgrounds.
|
|
|
|
* Skia normally doesn't support subpixel AA text on transparent backgrounds.
|
|
|
|
* To get around this, we have to wrap the Skia bytes with a CGContext and ask
|
|
|
|
* CG to draw the text.
|
|
|
|
* In vibrancy cases, we have to use a private API,
|
|
|
|
* CGContextSetFontSmoothingBackgroundColor, which sets the expected
|
|
|
|
* background color the text will draw onto so that CG can render the text
|
|
|
|
* properly. After that, we have to go back and fixup the pixels
|
|
|
|
* such that their alpha values are correct.
|
|
|
|
*/
|
|
|
|
bool DrawTargetSkia::FillGlyphsWithCG(ScaledFont* aFont,
|
|
|
|
const GlyphBuffer& aBuffer,
|
|
|
|
const Pattern& aPattern,
|
2017-11-03 04:42:56 +03:00
|
|
|
const DrawOptions& aOptions) {
|
2016-05-17 21:41:38 +03:00
|
|
|
MOZ_ASSERT(aFont->GetType() == FontType::MAC);
|
|
|
|
MOZ_ASSERT(aPattern.GetType() == PatternType::COLOR);
|
|
|
|
|
|
|
|
CGContextRef cgContext = BorrowCGContext(aOptions);
|
|
|
|
if (!cgContext) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Vector<CGGlyph, 32> glyphs;
|
|
|
|
Vector<CGPoint, 32> positions;
|
|
|
|
if (!SetupCGGlyphs(cgContext, aBuffer, glyphs, positions)) {
|
2016-05-19 03:09:14 +03:00
|
|
|
ReturnCGContext(cgContext);
|
2016-05-17 21:41:38 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-11-03 04:02:30 +03:00
|
|
|
ScaledFontMac* macFont = static_cast<ScaledFontMac*>(aFont);
|
|
|
|
SetFontSmoothingBackgroundColor(cgContext, mColorSpace,
|
|
|
|
macFont->FontSmoothingBackgroundColor());
|
2016-05-17 21:41:38 +03:00
|
|
|
SetFontColor(cgContext, mColorSpace, aPattern);
|
|
|
|
|
|
|
|
if (ScaledFontMac::CTFontDrawGlyphsPtr != nullptr) {
|
|
|
|
ScaledFontMac::CTFontDrawGlyphsPtr(macFont->mCTFont, glyphs.begin(),
|
|
|
|
positions.begin(), aBuffer.mNumGlyphs,
|
|
|
|
cgContext);
|
|
|
|
} else {
|
|
|
|
CGContextSetFont(cgContext, macFont->mFont);
|
|
|
|
CGContextSetFontSize(cgContext, macFont->mSize);
|
|
|
|
CGContextShowGlyphsAtPositions(cgContext, glyphs.begin(), positions.begin(),
|
|
|
|
aBuffer.mNumGlyphs);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Calculate the area of the text we just drew
|
2016-11-15 12:00:08 +03:00
|
|
|
auto* bboxes = new CGRect[aBuffer.mNumGlyphs];
|
2016-05-17 21:41:38 +03:00
|
|
|
CTFontGetBoundingRectsForGlyphs(macFont->mCTFont, kCTFontDefaultOrientation,
|
|
|
|
glyphs.begin(), bboxes, aBuffer.mNumGlyphs);
|
|
|
|
CGRect extents =
|
|
|
|
ComputeGlyphsExtents(bboxes, positions.begin(), aBuffer.mNumGlyphs, 1.0f);
|
|
|
|
delete[] bboxes;
|
|
|
|
|
|
|
|
CGAffineTransform cgTransform = CGContextGetCTM(cgContext);
|
|
|
|
extents = CGRectApplyAffineTransform(extents, cgTransform);
|
|
|
|
|
|
|
|
// Have to round it out to ensure we fully cover all pixels
|
|
|
|
Rect rect(extents.origin.x, extents.origin.y, extents.size.width,
|
|
|
|
extents.size.height);
|
|
|
|
rect.RoundOut();
|
|
|
|
extents = CGRectMake(rect.x, rect.y, rect.width, rect.height);
|
|
|
|
|
|
|
|
EnsureValidPremultipliedData(cgContext, extents);
|
|
|
|
|
|
|
|
ReturnCGContext(cgContext);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-11-03 04:02:30 +03:00
|
|
|
static bool HasFontSmoothingBackgroundColor(ScaledFont* aFont) {
|
2016-05-17 21:41:38 +03:00
|
|
|
// This should generally only be true if we have a popup context menu
|
2017-11-03 04:02:30 +03:00
|
|
|
if (aFont && aFont->GetType() == FontType::MAC) {
|
2016-05-17 21:41:38 +03:00
|
|
|
Color fontSmoothingBackgroundColor =
|
2017-11-03 04:02:30 +03:00
|
|
|
static_cast<ScaledFontMac*>(aFont)->FontSmoothingBackgroundColor();
|
2016-05-17 21:41:38 +03:00
|
|
|
return fontSmoothingBackgroundColor.a > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-11-03 04:02:30 +03:00
|
|
|
static bool ShouldUseCGToFillGlyphs(ScaledFont* aFont,
|
|
|
|
const Pattern& aPattern) {
|
|
|
|
return HasFontSmoothingBackgroundColor(aFont) &&
|
2016-05-17 21:41:38 +03:00
|
|
|
aPattern.GetType() == PatternType::COLOR;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2016-09-03 01:00:29 +03:00
|
|
|
static bool CanDrawFont(ScaledFont* aFont) {
|
2016-07-20 22:07:11 +03:00
|
|
|
switch (aFont->GetType()) {
|
2018-06-15 02:42:56 +03:00
|
|
|
case FontType::FREETYPE:
|
2016-07-20 22:07:11 +03:00
|
|
|
case FontType::FONTCONFIG:
|
|
|
|
case FontType::MAC:
|
|
|
|
case FontType::GDI:
|
|
|
|
case FontType::DWRITE:
|
2016-09-03 01:00:29 +03:00
|
|
|
return true;
|
2016-07-20 22:07:11 +03:00
|
|
|
default:
|
2016-09-03 01:00:29 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-12 17:24:44 +03:00
|
|
|
void DrawTargetSkia::DrawGlyphs(ScaledFont* aFont, const GlyphBuffer& aBuffer,
|
|
|
|
const Pattern& aPattern,
|
|
|
|
const StrokeOptions* aStrokeOptions,
|
2017-11-03 04:42:56 +03:00
|
|
|
const DrawOptions& aOptions) {
|
2016-09-03 01:00:29 +03:00
|
|
|
if (!CanDrawFont(aFont)) {
|
2011-11-02 23:55:03 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-11-02 23:55:03 +04:00
|
|
|
MarkChanged();
|
|
|
|
|
2016-05-17 21:41:38 +03:00
|
|
|
#ifdef MOZ_WIDGET_COCOA
|
2017-11-03 04:02:30 +03:00
|
|
|
if (!aStrokeOptions && ShouldUseCGToFillGlyphs(aFont, aPattern)) {
|
|
|
|
if (FillGlyphsWithCG(aFont, aBuffer, aPattern, aOptions)) {
|
2016-05-17 21:41:38 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-01-09 22:54:44 +04:00
|
|
|
ScaledFontBase* skiaFont = static_cast<ScaledFontBase*>(aFont);
|
2016-05-20 19:16:29 +03:00
|
|
|
SkTypeface* typeface = skiaFont->GetSkTypeface();
|
|
|
|
if (!typeface) {
|
|
|
|
return;
|
|
|
|
}
|
2011-11-02 23:55:03 +04:00
|
|
|
|
2017-05-10 05:31:07 +03:00
|
|
|
AutoPaintSetup paint(mCanvas, aOptions, aPattern);
|
2017-02-12 17:24:44 +03:00
|
|
|
if (aStrokeOptions && !StrokeOptionsToPaint(paint.mPaint, *aStrokeOptions)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-09-03 01:00:29 +03:00
|
|
|
AntialiasMode aaMode = aFont->GetDefaultAAMode();
|
|
|
|
if (aOptions.mAntialiasMode != AntialiasMode::DEFAULT) {
|
|
|
|
aaMode = aOptions.mAntialiasMode;
|
|
|
|
}
|
|
|
|
bool aaEnabled = aaMode != AntialiasMode::NONE;
|
|
|
|
paint.mPaint.setAntiAlias(aaEnabled);
|
2013-06-05 21:48:59 +04:00
|
|
|
|
2019-03-26 18:06:56 +03:00
|
|
|
SkFont font(sk_ref_sp(typeface), SkFloatToScalar(skiaFont->mSize));
|
2015-05-04 20:23:54 +03:00
|
|
|
|
2019-09-16 20:03:04 +03:00
|
|
|
bool useSubpixelAA =
|
|
|
|
GetPermitSubpixelAA() &&
|
|
|
|
(aaMode == AntialiasMode::DEFAULT || aaMode == AntialiasMode::SUBPIXEL);
|
2019-03-26 18:06:56 +03:00
|
|
|
font.setEdging(useSubpixelAA ? SkFont::Edging::kSubpixelAntiAlias
|
|
|
|
: (aaEnabled ? SkFont::Edging::kAntiAlias
|
|
|
|
: SkFont::Edging::kAlias));
|
2015-09-02 21:12:32 +03:00
|
|
|
|
2019-09-16 20:03:04 +03:00
|
|
|
skiaFont->SetupSkFontDrawOptions(font);
|
2011-11-02 23:55:03 +04:00
|
|
|
|
2019-06-27 05:42:24 +03:00
|
|
|
// Limit the amount of internal batch allocations Skia does.
|
|
|
|
const uint32_t kMaxGlyphBatchSize = 8192;
|
|
|
|
|
|
|
|
for (uint32_t offset = 0; offset < aBuffer.mNumGlyphs;) {
|
|
|
|
uint32_t batchSize =
|
|
|
|
std::min(aBuffer.mNumGlyphs - offset, kMaxGlyphBatchSize);
|
|
|
|
SkTextBlobBuilder builder;
|
|
|
|
auto runBuffer = builder.allocRunPos(font, batchSize);
|
|
|
|
for (uint32_t i = 0; i < batchSize; i++, offset++) {
|
|
|
|
runBuffer.glyphs[i] = aBuffer.mGlyphs[offset].mIndex;
|
|
|
|
runBuffer.points()[i] = PointToSkPoint(aBuffer.mGlyphs[offset].mPosition);
|
|
|
|
}
|
|
|
|
|
|
|
|
sk_sp<SkTextBlob> text = builder.make();
|
|
|
|
mCanvas->drawTextBlob(text, 0, 0, paint.mPaint);
|
|
|
|
}
|
2011-11-02 23:55:03 +04:00
|
|
|
}
|
|
|
|
|
2017-02-12 17:24:44 +03:00
|
|
|
void DrawTargetSkia::FillGlyphs(ScaledFont* aFont, const GlyphBuffer& aBuffer,
|
|
|
|
const Pattern& aPattern,
|
2017-11-03 04:42:56 +03:00
|
|
|
const DrawOptions& aOptions) {
|
|
|
|
DrawGlyphs(aFont, aBuffer, aPattern, nullptr, aOptions);
|
2017-02-12 17:24:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void DrawTargetSkia::StrokeGlyphs(ScaledFont* aFont, const GlyphBuffer& aBuffer,
|
|
|
|
const Pattern& aPattern,
|
|
|
|
const StrokeOptions& aStrokeOptions,
|
2017-11-03 04:42:56 +03:00
|
|
|
const DrawOptions& aOptions) {
|
|
|
|
DrawGlyphs(aFont, aBuffer, aPattern, &aStrokeOptions, aOptions);
|
2017-02-12 17:24:44 +03:00
|
|
|
}
|
|
|
|
|
2012-01-19 08:48:33 +04:00
|
|
|
void DrawTargetSkia::Mask(const Pattern& aSource, const Pattern& aMask,
|
|
|
|
const DrawOptions& aOptions) {
|
2018-03-12 23:37:10 +03:00
|
|
|
SkIRect maskBounds;
|
|
|
|
if (!mCanvas->getDeviceClipBounds(&maskBounds)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
SkPoint maskOrigin;
|
|
|
|
maskOrigin.iset(maskBounds.fLeft, maskBounds.fTop);
|
|
|
|
|
|
|
|
SkMatrix maskMatrix = mCanvas->getTotalMatrix();
|
|
|
|
maskMatrix.postTranslate(-maskOrigin.fX, -maskOrigin.fY);
|
|
|
|
|
2012-01-19 08:48:33 +04:00
|
|
|
MarkChanged();
|
2018-03-12 23:37:10 +03:00
|
|
|
AutoPaintSetup paint(mCanvas, aOptions, aSource, nullptr, &maskMatrix);
|
2012-01-19 08:48:33 +04:00
|
|
|
|
|
|
|
SkPaint maskPaint;
|
2016-01-12 21:05:13 +03:00
|
|
|
SetPaintPattern(maskPaint, aMask);
|
2014-10-08 08:16:14 +04:00
|
|
|
|
2018-03-12 23:37:10 +03:00
|
|
|
SkBitmap maskBitmap;
|
|
|
|
if (!maskBitmap.tryAllocPixelsFlags(
|
|
|
|
SkImageInfo::MakeA8(maskBounds.width(), maskBounds.height()),
|
|
|
|
SkBitmap::kZeroPixels_AllocFlag)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
SkCanvas maskCanvas(maskBitmap);
|
|
|
|
maskCanvas.setMatrix(maskMatrix);
|
|
|
|
maskCanvas.drawPaint(maskPaint);
|
|
|
|
|
|
|
|
mCanvas->save();
|
|
|
|
mCanvas->resetMatrix();
|
|
|
|
|
|
|
|
mCanvas->drawBitmap(maskBitmap, maskOrigin.fX, maskOrigin.fY, &paint.mPaint);
|
2012-01-19 08:48:33 +04:00
|
|
|
|
2018-03-12 23:37:10 +03:00
|
|
|
mCanvas->restore();
|
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();
|
2018-03-12 23:37:10 +03:00
|
|
|
|
|
|
|
SkMatrix invOffset = SkMatrix::MakeTrans(SkFloatToScalar(-aOffset.x),
|
|
|
|
SkFloatToScalar(-aOffset.y));
|
|
|
|
AutoPaintSetup paint(mCanvas, aOptions, aSource, nullptr, &invOffset);
|
2013-11-25 18:20:56 +04:00
|
|
|
|
2016-10-25 05:40:59 +03:00
|
|
|
sk_sp<SkImage> alphaMask = ExtractAlphaForSurface(aMask);
|
|
|
|
if (!alphaMask) {
|
2016-01-12 21:26:31 +03:00
|
|
|
gfxDebug() << *this << ": MaskSurface() failed to extract alpha for mask";
|
|
|
|
return;
|
|
|
|
}
|
2015-11-19 18:25:15 +03:00
|
|
|
|
2019-03-05 06:30:08 +03:00
|
|
|
mCanvas->drawImage(alphaMask, aOffset.x + aMask->GetRect().x,
|
|
|
|
aOffset.y + aMask->GetRect().y, &paint.mPaint);
|
2013-11-25 18:20:56 +04:00
|
|
|
}
|
|
|
|
|
2016-03-23 08:03:10 +03:00
|
|
|
bool DrawTarget::Draw3DTransformedSurface(SourceSurface* aSurface,
|
|
|
|
const Matrix4x4& aMatrix) {
|
|
|
|
// Composite the 3D transform with the DT's transform.
|
|
|
|
Matrix4x4 fullMat = aMatrix * Matrix4x4::From2D(mTransform);
|
|
|
|
if (fullMat.IsSingular()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// Transform the surface bounds and clip to this DT.
|
|
|
|
IntRect xformBounds = RoundedOut(fullMat.TransformAndClipBounds(
|
|
|
|
Rect(Point(0, 0), Size(aSurface->GetSize())),
|
|
|
|
Rect(Point(0, 0), Size(GetSize()))));
|
|
|
|
if (xformBounds.IsEmpty()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// Offset the matrix by the transformed origin.
|
2017-12-19 23:48:39 +03:00
|
|
|
fullMat.PostTranslate(-xformBounds.X(), -xformBounds.Y(), 0);
|
2016-03-23 08:03:10 +03:00
|
|
|
|
|
|
|
// Read in the source data.
|
2016-10-25 05:40:59 +03:00
|
|
|
sk_sp<SkImage> srcImage = GetSkImageForSurface(aSurface);
|
|
|
|
if (!srcImage) {
|
2016-09-02 04:17:03 +03:00
|
|
|
return true;
|
|
|
|
}
|
2016-03-23 08:03:10 +03:00
|
|
|
|
|
|
|
// Set up an intermediate destination surface only the size of the transformed
|
|
|
|
// bounds. Try to pass through the source's format unmodified in both the BGRA
|
|
|
|
// and ARGB cases.
|
|
|
|
RefPtr<DataSourceSurface> dstSurf = Factory::CreateDataSourceSurface(
|
|
|
|
xformBounds.Size(),
|
2016-10-25 05:40:59 +03:00
|
|
|
!srcImage->isOpaque() ? aSurface->GetFormat()
|
2016-03-23 08:03:10 +03:00
|
|
|
: SurfaceFormat::A8R8G8B8_UINT32,
|
|
|
|
true);
|
|
|
|
if (!dstSurf) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-11-09 11:43:29 +03:00
|
|
|
|
|
|
|
DataSourceSurface::ScopedMap map(dstSurf, DataSourceSurface::READ_WRITE);
|
2017-05-10 05:31:07 +03:00
|
|
|
std::unique_ptr<SkCanvas> dstCanvas(SkCanvas::MakeRasterDirect(
|
2017-08-14 15:29:28 +03:00
|
|
|
SkImageInfo::Make(xformBounds.Width(), xformBounds.Height(),
|
2016-10-25 05:40:59 +03:00
|
|
|
GfxFormatToSkiaColorType(dstSurf->GetFormat()),
|
2016-03-23 08:03:10 +03:00
|
|
|
kPremul_SkAlphaType),
|
2017-11-09 11:43:30 +03:00
|
|
|
map.GetData(), map.GetStride()));
|
2016-03-23 08:03:10 +03:00
|
|
|
if (!dstCanvas) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do the transform.
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setAntiAlias(true);
|
|
|
|
paint.setFilterQuality(kLow_SkFilterQuality);
|
2016-10-25 05:40:59 +03:00
|
|
|
paint.setBlendMode(SkBlendMode::kSrc);
|
2016-03-23 08:03:10 +03:00
|
|
|
|
|
|
|
SkMatrix xform;
|
|
|
|
GfxMatrixToSkiaMatrix(fullMat, xform);
|
|
|
|
dstCanvas->setMatrix(xform);
|
|
|
|
|
2016-10-25 05:40:59 +03:00
|
|
|
dstCanvas->drawImage(srcImage, 0, 0, &paint);
|
2016-03-23 08:03:10 +03:00
|
|
|
dstCanvas->flush();
|
|
|
|
|
|
|
|
// Temporarily reset the DT's transform, since it has already been composed
|
|
|
|
// above.
|
|
|
|
Matrix origTransform = mTransform;
|
|
|
|
SetTransform(Matrix());
|
|
|
|
|
|
|
|
// Draw the transformed surface within the transformed bounds.
|
|
|
|
DrawSurface(dstSurf, Rect(xformBounds),
|
|
|
|
Rect(Point(0, 0), Size(xformBounds.Size())));
|
|
|
|
|
|
|
|
SetTransform(origTransform);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DrawTargetSkia::Draw3DTransformedSurface(SourceSurface* aSurface,
|
|
|
|
const Matrix4x4& aMatrix) {
|
|
|
|
if (aMatrix.IsSingular()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-09-16 18:03:48 +03:00
|
|
|
MarkChanged();
|
|
|
|
|
2016-10-25 05:40:59 +03:00
|
|
|
sk_sp<SkImage> image = GetSkImageForSurface(aSurface);
|
|
|
|
if (!image) {
|
2016-09-02 04:17:03 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-03-23 08:03:10 +03:00
|
|
|
mCanvas->save();
|
|
|
|
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setAntiAlias(true);
|
|
|
|
paint.setFilterQuality(kLow_SkFilterQuality);
|
|
|
|
|
|
|
|
SkMatrix xform;
|
|
|
|
GfxMatrixToSkiaMatrix(aMatrix, xform);
|
|
|
|
mCanvas->concat(xform);
|
|
|
|
|
2016-10-25 05:40:59 +03:00
|
|
|
mCanvas->drawImage(image, 0, 0, &paint);
|
2016-03-23 08:03:10 +03:00
|
|
|
|
|
|
|
mCanvas->restore();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-11-02 23:55:03 +04:00
|
|
|
already_AddRefed<SourceSurface> DrawTargetSkia::CreateSourceSurfaceFromData(
|
2013-11-01 17:13:36 +04:00
|
|
|
unsigned char* aData, const IntSize& aSize, int32_t aStride,
|
|
|
|
SurfaceFormat aFormat) const {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<SourceSurfaceSkia> newSurf = new SourceSurfaceSkia();
|
2011-11-02 23:55:03 +04:00
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<DrawTarget> DrawTargetSkia::CreateSimilarDrawTarget(
|
|
|
|
const IntSize& aSize, SurfaceFormat aFormat) const {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<DrawTargetSkia> target = new DrawTargetSkia();
|
2016-10-31 18:05:30 +03:00
|
|
|
#ifdef DEBUG
|
2017-05-10 05:31:07 +03:00
|
|
|
if (!IsBackedByPixels(mCanvas)) {
|
2016-12-22 04:35:22 +03:00
|
|
|
// If our canvas is backed by vector storage such as PDF then we want to
|
|
|
|
// create a new DrawTarget with similar storage to avoid losing fidelity
|
|
|
|
// (fidelity will be lost if the returned DT is Snapshot()'ed and drawn
|
|
|
|
// back onto us since a raster will be drawn instead of vector commands).
|
2016-10-31 18:05:30 +03:00
|
|
|
NS_WARNING("Not backed by pixels - we need to handle PDF backed SkCanvas");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-11-02 23:55:03 +04:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-11-13 13:39:02 +03:00
|
|
|
bool DrawTargetSkia::CanCreateSimilarDrawTarget(const IntSize& aSize,
|
|
|
|
SurfaceFormat aFormat) const {
|
2019-08-30 20:14:44 +03:00
|
|
|
auto minmaxPair = std::minmax(aSize.width, aSize.height);
|
|
|
|
return minmaxPair.first >= 0 &&
|
|
|
|
size_t(minmaxPair.second) < GetMaxSurfaceSize();
|
2018-11-13 13:39:02 +03:00
|
|
|
}
|
|
|
|
|
2019-06-21 12:51:00 +03:00
|
|
|
RefPtr<DrawTarget> DrawTargetSkia::CreateClippedDrawTarget(
|
|
|
|
const Rect& aBounds, SurfaceFormat aFormat) {
|
|
|
|
SkIRect clipBounds;
|
|
|
|
|
|
|
|
RefPtr<DrawTarget> result;
|
|
|
|
// Doing this save()/restore() dance is wasteful
|
|
|
|
mCanvas->save();
|
|
|
|
if (!aBounds.IsEmpty()) {
|
|
|
|
mCanvas->clipRect(RectToSkRect(aBounds), SkClipOp::kIntersect, true);
|
|
|
|
}
|
|
|
|
if (mCanvas->getDeviceClipBounds(&clipBounds)) {
|
|
|
|
RefPtr<DrawTarget> dt = CreateSimilarDrawTarget(
|
|
|
|
IntSize(clipBounds.width(), clipBounds.height()), aFormat);
|
|
|
|
result = gfx::Factory::CreateOffsetDrawTarget(
|
|
|
|
dt, IntPoint(clipBounds.x(), clipBounds.y()));
|
|
|
|
result->SetTransform(mTransform);
|
|
|
|
} else {
|
|
|
|
// Everything is clipped but we still want some kind of surface
|
|
|
|
result = CreateSimilarDrawTarget(IntSize(1, 1), aFormat);
|
|
|
|
}
|
|
|
|
mCanvas->restore();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-08-05 20:29:12 +03:00
|
|
|
already_AddRefed<SourceSurface>
|
|
|
|
DrawTargetSkia::OptimizeSourceSurfaceForUnknownAlpha(
|
|
|
|
SourceSurface* aSurface) const {
|
|
|
|
if (aSurface->GetType() == SurfaceType::SKIA) {
|
|
|
|
RefPtr<SourceSurface> surface(aSurface);
|
2016-02-03 21:49:36 +03:00
|
|
|
return surface.forget();
|
2014-05-23 09:09:34 +04:00
|
|
|
}
|
2016-08-05 20:29:12 +03:00
|
|
|
|
|
|
|
RefPtr<DataSourceSurface> dataSurface = aSurface->GetDataSurface();
|
2017-11-09 11:43:29 +03:00
|
|
|
DataSourceSurface::ScopedMap map(dataSurface, DataSourceSurface::READ_WRITE);
|
2016-08-05 20:29:12 +03:00
|
|
|
|
|
|
|
// For plugins, GDI can sometimes just write 0 to the alpha channel
|
|
|
|
// even for RGBX formats. In this case, we have to manually write
|
|
|
|
// the alpha channel to make Skia happy with RGBX and in case GDI
|
|
|
|
// writes some bad data. Luckily, this only happens on plugins.
|
2017-11-09 11:43:29 +03:00
|
|
|
WriteRGBXFormat(map.GetData(), dataSurface->GetSize(), map.GetStride(),
|
2017-11-09 11:43:30 +03:00
|
|
|
dataSurface->GetFormat());
|
2016-08-05 20:29:12 +03:00
|
|
|
return dataSurface.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<SourceSurface> DrawTargetSkia::OptimizeSourceSurface(
|
|
|
|
SourceSurface* aSurface) const {
|
2016-01-21 17:52:08 +03:00
|
|
|
if (aSurface->GetType() == SurfaceType::SKIA) {
|
|
|
|
RefPtr<SourceSurface> surface(aSurface);
|
|
|
|
return surface.forget();
|
2014-05-23 09:09:34 +04:00
|
|
|
}
|
|
|
|
|
2016-01-21 17:52:08 +03:00
|
|
|
// 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.
|
2016-07-26 02:36:35 +03:00
|
|
|
RefPtr<DataSourceSurface> dataSurface = aSurface->GetDataSurface();
|
2017-11-09 11:43:29 +03:00
|
|
|
#ifdef DEBUG
|
|
|
|
DataSourceSurface::ScopedMap map(dataSurface, DataSourceSurface::READ);
|
|
|
|
MOZ_ASSERT(VerifyRGBXFormat(map.GetData(), dataSurface->GetSize(),
|
2017-11-09 11:43:30 +03:00
|
|
|
map.GetStride(), dataSurface->GetFormat()));
|
2017-11-09 11:43:29 +03:00
|
|
|
#endif
|
2016-07-26 02:36:35 +03:00
|
|
|
return dataSurface.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 {
|
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) {
|
2016-09-16 18:03:48 +03:00
|
|
|
MarkChanged();
|
|
|
|
|
2016-10-25 05:40:59 +03:00
|
|
|
sk_sp<SkImage> image = GetSkImageForSurface(aSurface);
|
|
|
|
if (!image) {
|
2011-11-02 23:55:03 +04:00
|
|
|
return;
|
|
|
|
}
|
2011-11-02 23:55:03 +04:00
|
|
|
|
2011-11-02 23:55:03 +04:00
|
|
|
mCanvas->save();
|
2016-06-21 23:14:28 +03:00
|
|
|
mCanvas->setMatrix(SkMatrix::MakeTrans(SkIntToScalar(aDestination.x),
|
|
|
|
SkIntToScalar(aDestination.y)));
|
2017-08-14 15:29:28 +03:00
|
|
|
mCanvas->clipRect(SkRect::MakeIWH(aSourceRect.Width(), aSourceRect.Height()),
|
|
|
|
SkClipOp::kReplace_deprecated);
|
2012-05-31 22:56:33 +04:00
|
|
|
|
2016-01-13 21:11:07 +03:00
|
|
|
SkPaint paint;
|
2016-10-25 05:40:59 +03:00
|
|
|
if (!image->isOpaque()) {
|
2016-01-13 21:11:07 +03:00
|
|
|
// Keep the xfermode as SOURCE_OVER for opaque bitmaps
|
2012-05-31 22:56:33 +04:00
|
|
|
// http://code.google.com/p/skia/issues/detail?id=628
|
2016-10-25 05:40:59 +03:00
|
|
|
paint.setBlendMode(SkBlendMode::kSrc);
|
2012-05-31 22:56:33 +04:00
|
|
|
}
|
2016-10-25 05:40:59 +03:00
|
|
|
// drawImage with A8 images ends up doing a mask operation
|
2014-06-11 07:42:32 +04:00
|
|
|
// so we need to clear before
|
2018-03-12 23:37:10 +03:00
|
|
|
if (image->isAlphaOnly()) {
|
2016-01-13 21:11:07 +03:00
|
|
|
mCanvas->clear(SK_ColorTRANSPARENT);
|
2014-06-11 07:42:32 +04:00
|
|
|
}
|
2017-12-19 23:48:39 +03:00
|
|
|
mCanvas->drawImage(image, -SkIntToScalar(aSourceRect.X()),
|
|
|
|
-SkIntToScalar(aSourceRect.Y()), &paint);
|
2011-11-02 23:55:03 +04:00
|
|
|
mCanvas->restore();
|
|
|
|
}
|
|
|
|
|
2019-05-25 19:20:59 +03:00
|
|
|
static inline SkPixelGeometry GetSkPixelGeometry() {
|
|
|
|
return Factory::GetBGRSubpixelOrder() ? kBGR_H_SkPixelGeometry
|
|
|
|
: kRGB_H_SkPixelGeometry;
|
|
|
|
}
|
|
|
|
|
2011-11-02 23:55:03 +04:00
|
|
|
bool DrawTargetSkia::Init(const IntSize& aSize, SurfaceFormat aFormat) {
|
2015-11-19 21:35:31 +03:00
|
|
|
if (size_t(std::max(aSize.width, aSize.height)) > GetMaxSurfaceSize()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-10-08 19:16:46 +03:00
|
|
|
// we need to have surfaces that have a stride aligned to 4 for interop with
|
|
|
|
// cairo
|
2016-10-25 05:40:59 +03:00
|
|
|
SkImageInfo info = MakeSkiaImageInfo(aSize, aFormat);
|
|
|
|
size_t stride = SkAlign4(info.minRowBytes());
|
2019-05-25 19:20:59 +03:00
|
|
|
SkSurfaceProps props(0, GetSkPixelGeometry());
|
|
|
|
mSurface = SkSurface::MakeRaster(info, stride, &props);
|
2016-10-25 05:40:59 +03:00
|
|
|
if (!mSurface) {
|
2011-11-02 23:55:03 +04:00
|
|
|
return false;
|
|
|
|
}
|
2013-05-29 20:49:40 +04:00
|
|
|
|
2011-11-02 23:55:03 +04:00
|
|
|
mSize = aSize;
|
2016-10-25 02:45:46 +03:00
|
|
|
mFormat = aFormat;
|
2017-05-10 05:31:07 +03:00
|
|
|
mCanvas = mSurface->getCanvas();
|
2016-12-16 19:02:04 +03:00
|
|
|
SetPermitSubpixelAA(IsOpaque(mFormat));
|
2016-10-25 05:40:59 +03:00
|
|
|
|
|
|
|
if (info.isOpaque()) {
|
|
|
|
mCanvas->clear(SK_ColorBLACK);
|
|
|
|
}
|
2011-11-02 23:55:03 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-10-26 21:23:07 +03:00
|
|
|
bool DrawTargetSkia::Init(SkCanvas* aCanvas) {
|
2017-05-10 05:31:07 +03:00
|
|
|
mCanvas = aCanvas;
|
2016-10-26 21:23:07 +03:00
|
|
|
|
|
|
|
SkImageInfo imageInfo = mCanvas->imageInfo();
|
|
|
|
|
|
|
|
// If the canvas is backed by pixels we clear it to be on the safe side. If
|
|
|
|
// it's not (for example, for PDF output) we don't.
|
2017-05-10 05:31:07 +03:00
|
|
|
if (IsBackedByPixels(mCanvas)) {
|
2016-10-26 21:23:07 +03:00
|
|
|
SkColor clearColor =
|
|
|
|
imageInfo.isOpaque() ? SK_ColorBLACK : SK_ColorTRANSPARENT;
|
|
|
|
mCanvas->clear(clearColor);
|
|
|
|
}
|
|
|
|
|
|
|
|
SkISize size = mCanvas->getBaseLayerSize();
|
|
|
|
mSize.width = size.width();
|
|
|
|
mSize.height = size.height();
|
|
|
|
mFormat =
|
|
|
|
SkiaColorTypeToGfxFormat(imageInfo.colorType(), imageInfo.alphaType());
|
2016-12-16 19:02:04 +03:00
|
|
|
SetPermitSubpixelAA(IsOpaque(mFormat));
|
2016-10-26 21:23:07 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-03-18 23:58:55 +03:00
|
|
|
bool DrawTargetSkia::Init(unsigned char* aData, const IntSize& aSize,
|
|
|
|
int32_t aStride, SurfaceFormat aFormat,
|
|
|
|
bool aUninitialized) {
|
2016-02-17 18:34:37 +03:00
|
|
|
MOZ_ASSERT((aFormat != SurfaceFormat::B8G8R8X8) || aUninitialized ||
|
2016-03-18 23:58:55 +03:00
|
|
|
VerifyRGBXFormat(aData, aSize, aStride, aFormat));
|
2014-03-26 23:58:07 +04:00
|
|
|
|
2019-05-25 19:20:59 +03:00
|
|
|
SkSurfaceProps props(0, GetSkPixelGeometry());
|
2016-10-25 05:40:59 +03:00
|
|
|
mSurface = SkSurface::MakeRasterDirect(MakeSkiaImageInfo(aSize, aFormat),
|
2019-05-25 19:20:59 +03:00
|
|
|
aData, aStride, &props);
|
2016-10-25 05:40:59 +03:00
|
|
|
if (!mSurface) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-05-29 20:49:40 +04:00
|
|
|
|
2012-01-19 08:48:33 +04:00
|
|
|
mSize = aSize;
|
|
|
|
mFormat = aFormat;
|
2017-05-10 05:31:07 +03:00
|
|
|
mCanvas = mSurface->getCanvas();
|
2016-12-16 19:02:04 +03:00
|
|
|
SetPermitSubpixelAA(IsOpaque(mFormat));
|
2016-10-25 05:40:59 +03:00
|
|
|
return true;
|
2012-01-19 08:48:33 +04:00
|
|
|
}
|
|
|
|
|
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-08 08:16:14 +04:00
|
|
|
return nullptr;
|
2014-03-06 01:49:37 +04:00
|
|
|
}
|
|
|
|
|
2011-11-02 23:55:03 +04:00
|
|
|
already_AddRefed<PathBuilder> 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
|
|
|
mCanvas->save();
|
2017-05-10 05:31:07 +03:00
|
|
|
mCanvas->clipRect(RectToSkRect(aRect), SkClipOp::kIntersect, true);
|
2016-07-26 02:36:35 +03:00
|
|
|
SkColor clearColor = (mFormat == SurfaceFormat::B8G8R8X8)
|
|
|
|
? SK_ColorBLACK
|
|
|
|
: SK_ColorTRANSPARENT;
|
|
|
|
mCanvas->clear(clearColor);
|
2011-11-02 23:55:03 +04:00
|
|
|
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();
|
2017-05-10 05:31:07 +03:00
|
|
|
mCanvas->clipPath(skiaPath->GetPath(), SkClipOp::kIntersect, true);
|
2011-11-02 23:55:03 +04:00
|
|
|
}
|
|
|
|
|
2016-09-22 00:03:20 +03:00
|
|
|
void DrawTargetSkia::PushDeviceSpaceClipRects(const IntRect* aRects,
|
|
|
|
uint32_t aCount) {
|
|
|
|
// Build a region by unioning all the rects together.
|
|
|
|
SkRegion region;
|
|
|
|
for (uint32_t i = 0; i < aCount; i++) {
|
|
|
|
region.op(IntRectToSkIRect(aRects[i]), SkRegion::kUnion_Op);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clip with the resulting region. clipRegion does not transform
|
|
|
|
// this region by the current transform, unlike the other SkCanvas
|
|
|
|
// clip methods, so it is just passed through in device-space.
|
|
|
|
mCanvas->save();
|
2017-05-10 05:31:07 +03:00
|
|
|
mCanvas->clipRegion(region, SkClipOp::kIntersect);
|
2016-09-22 00:03:20 +03: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();
|
2017-05-10 05:31:07 +03:00
|
|
|
mCanvas->clipRect(rect, SkClipOp::kIntersect, true);
|
2012-01-19 08:48:33 +04:00
|
|
|
}
|
|
|
|
|
2011-11-02 23:55:03 +04:00
|
|
|
void DrawTargetSkia::PopClip() {
|
|
|
|
mCanvas->restore();
|
2018-02-02 18:22:20 +03:00
|
|
|
SetTransform(GetTransform());
|
2011-11-02 23:55:03 +04:00
|
|
|
}
|
|
|
|
|
2016-01-13 07:30:33 +03:00
|
|
|
void DrawTargetSkia::PushLayer(bool aOpaque, Float aOpacity,
|
|
|
|
SourceSurface* aMask,
|
|
|
|
const Matrix& aMaskTransform,
|
|
|
|
const IntRect& aBounds, bool aCopyBackground) {
|
2018-02-17 20:07:30 +03:00
|
|
|
PushLayerWithBlend(aOpaque, aOpacity, aMask, aMaskTransform, aBounds,
|
|
|
|
aCopyBackground, CompositionOp::OP_OVER);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DrawTargetSkia::PushLayerWithBlend(bool aOpaque, Float aOpacity,
|
|
|
|
SourceSurface* aMask,
|
|
|
|
const Matrix& aMaskTransform,
|
|
|
|
const IntRect& aBounds,
|
|
|
|
bool aCopyBackground,
|
|
|
|
CompositionOp aCompositionOp) {
|
2018-04-05 21:57:11 +03:00
|
|
|
PushedLayer layer(GetPermitSubpixelAA(), aMask);
|
2016-01-13 07:30:33 +03:00
|
|
|
mPushedLayers.push_back(layer);
|
|
|
|
|
|
|
|
SkPaint paint;
|
|
|
|
|
2018-04-05 21:57:11 +03:00
|
|
|
paint.setAlpha(ColorFloatToByte(aOpacity));
|
|
|
|
paint.setBlendMode(GfxOpToSkiaOp(aCompositionOp));
|
2016-01-13 07:30:33 +03:00
|
|
|
|
2016-12-16 19:02:04 +03:00
|
|
|
// aBounds is supplied in device space, but SaveLayerRec wants local space.
|
2016-01-13 07:30:33 +03:00
|
|
|
SkRect bounds = IntRectToSkRect(aBounds);
|
2016-12-16 19:02:04 +03:00
|
|
|
if (!bounds.isEmpty()) {
|
|
|
|
SkMatrix inverseCTM;
|
|
|
|
if (mCanvas->getTotalMatrix().invert(&inverseCTM)) {
|
|
|
|
inverseCTM.mapRect(&bounds);
|
|
|
|
} else {
|
|
|
|
bounds.setEmpty();
|
|
|
|
}
|
|
|
|
}
|
2016-01-13 07:30:33 +03:00
|
|
|
|
2018-04-05 21:57:11 +03:00
|
|
|
sk_sp<SkImage> clipImage = aMask ? GetSkImageForSurface(aMask) : nullptr;
|
|
|
|
SkMatrix clipMatrix;
|
|
|
|
GfxMatrixToSkiaMatrix(aMaskTransform, clipMatrix);
|
2018-11-07 00:04:36 +03:00
|
|
|
if (aMask) {
|
2019-03-05 06:30:06 +03:00
|
|
|
clipMatrix.preTranslate(aMask->GetRect().X(), aMask->GetRect().Y());
|
2018-11-07 00:04:36 +03:00
|
|
|
}
|
|
|
|
|
2016-02-09 21:36:19 +03:00
|
|
|
SkCanvas::SaveLayerRec saveRec(
|
|
|
|
aBounds.IsEmpty() ? nullptr : &bounds, &paint, nullptr, clipImage.get(),
|
2018-04-05 21:57:11 +03:00
|
|
|
&clipMatrix,
|
2016-12-16 19:02:04 +03:00
|
|
|
SkCanvas::kPreserveLCDText_SaveLayerFlag |
|
2017-05-10 05:31:07 +03:00
|
|
|
(aCopyBackground ? SkCanvas::kInitWithPrevious_SaveLayerFlag : 0));
|
2016-02-09 21:36:19 +03:00
|
|
|
|
|
|
|
mCanvas->saveLayer(saveRec);
|
2016-01-13 07:30:33 +03:00
|
|
|
|
|
|
|
SetPermitSubpixelAA(aOpaque);
|
2016-05-17 21:41:38 +03:00
|
|
|
|
|
|
|
#ifdef MOZ_WIDGET_COCOA
|
|
|
|
CGContextRelease(mCG);
|
|
|
|
mCG = nullptr;
|
|
|
|
#endif
|
2016-01-13 07:30:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void DrawTargetSkia::PopLayer() {
|
|
|
|
MarkChanged();
|
|
|
|
|
|
|
|
MOZ_ASSERT(mPushedLayers.size());
|
|
|
|
const PushedLayer& layer = mPushedLayers.back();
|
|
|
|
|
2018-04-05 21:57:11 +03:00
|
|
|
mCanvas->restore();
|
2016-01-13 07:30:33 +03:00
|
|
|
|
2018-02-02 18:22:20 +03:00
|
|
|
SetTransform(GetTransform());
|
2016-01-13 07:30:33 +03:00
|
|
|
SetPermitSubpixelAA(layer.mOldPermitSubpixelAA);
|
|
|
|
|
|
|
|
mPushedLayers.pop_back();
|
2016-05-17 21:41:38 +03:00
|
|
|
|
|
|
|
#ifdef MOZ_WIDGET_COCOA
|
|
|
|
CGContextRelease(mCG);
|
|
|
|
mCG = nullptr;
|
|
|
|
#endif
|
2016-01-13 07:30:33 +03:00
|
|
|
}
|
|
|
|
|
2011-12-28 09:56:11 +04:00
|
|
|
already_AddRefed<GradientStops> 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
|
|
|
}
|
|
|
|
|
2013-11-27 15:25:28 +04:00
|
|
|
already_AddRefed<FilterNode> DrawTargetSkia::CreateFilter(FilterType aType) {
|
|
|
|
return FilterNodeSoftware::Create(aType);
|
|
|
|
}
|
|
|
|
|
2013-08-02 05:12:16 +04:00
|
|
|
void DrawTargetSkia::MarkChanged() {
|
2017-12-06 06:59:19 +03:00
|
|
|
// I'm not entirely certain whether this lock is needed, as multiple threads
|
|
|
|
// should never modify the DrawTarget at the same time anyway, but this seems
|
|
|
|
// like the safest.
|
|
|
|
MutexAutoLock lock(mSnapshotLock);
|
2013-08-02 05:12:16 +04:00
|
|
|
if (mSnapshot) {
|
2017-12-06 06:59:19 +03:00
|
|
|
if (mSnapshot->hasOneRef()) {
|
|
|
|
// No owners outside of this DrawTarget's own reference. Just dump it.
|
|
|
|
mSnapshot = nullptr;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-08-02 05:12:16 +04:00
|
|
|
mSnapshot->DrawTargetWillChange();
|
|
|
|
mSnapshot = nullptr;
|
2016-10-25 05:40:59 +03:00
|
|
|
|
|
|
|
// Handle copying of any image snapshots bound to the surface.
|
2016-10-26 20:41:07 +03:00
|
|
|
if (mSurface) {
|
|
|
|
mSurface->notifyContentWillChange(SkSurface::kRetain_ContentChangeMode);
|
|
|
|
}
|
2011-11-02 23:55:03 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace gfx
|
|
|
|
} // namespace mozilla
|