2011-06-24 10:41:16 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
2012-05-21 04:12:37 -07: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-06-24 10:41:16 -07:00
|
|
|
#include "DrawTargetCG.h"
|
|
|
|
#include "SourceSurfaceCG.h"
|
|
|
|
#include "Rect.h"
|
2012-01-09 10:54:44 -08:00
|
|
|
#include "ScaledFontMac.h"
|
|
|
|
#include "Tools.h"
|
|
|
|
#include <vector>
|
2013-07-18 17:08:51 -07:00
|
|
|
#include <algorithm>
|
2012-07-31 08:17:43 -07:00
|
|
|
#include "QuartzSupport.h"
|
2011-06-24 10:41:16 -07:00
|
|
|
|
2013-07-18 17:08:51 -07:00
|
|
|
using namespace std;
|
|
|
|
|
2011-06-24 10:41:16 -07:00
|
|
|
//CG_EXTERN void CGContextSetCompositeOperation (CGContextRef, PrivateCGCompositeMode);
|
|
|
|
|
2012-01-24 12:14:50 -08:00
|
|
|
// A private API that Cairo has been using for a long time
|
|
|
|
CG_EXTERN void CGContextSetCTM(CGContextRef, CGAffineTransform);
|
|
|
|
|
2011-06-24 10:41:16 -07:00
|
|
|
namespace mozilla {
|
|
|
|
namespace gfx {
|
|
|
|
|
|
|
|
static CGRect RectToCGRect(Rect r)
|
|
|
|
{
|
|
|
|
return CGRectMake(r.x, r.y, r.width, r.height);
|
|
|
|
}
|
|
|
|
|
2012-01-09 10:54:44 -08:00
|
|
|
static CGRect IntRectToCGRect(IntRect r)
|
|
|
|
{
|
|
|
|
return CGRectMake(r.x, r.y, r.width, r.height);
|
|
|
|
}
|
|
|
|
|
2011-06-24 10:41:16 -07:00
|
|
|
CGBlendMode ToBlendMode(CompositionOp op)
|
|
|
|
{
|
|
|
|
CGBlendMode mode;
|
|
|
|
switch (op) {
|
|
|
|
case OP_OVER:
|
|
|
|
mode = kCGBlendModeNormal;
|
|
|
|
break;
|
2012-01-17 10:08:38 -08:00
|
|
|
case OP_ADD:
|
|
|
|
mode = kCGBlendModePlusLighter;
|
2012-01-09 10:54:44 -08:00
|
|
|
break;
|
2012-01-17 10:08:38 -08:00
|
|
|
case OP_ATOP:
|
|
|
|
mode = kCGBlendModeSourceAtop;
|
2012-01-09 10:54:44 -08:00
|
|
|
break;
|
2012-01-09 10:54:44 -08:00
|
|
|
case OP_OUT:
|
|
|
|
mode = kCGBlendModeSourceOut;
|
|
|
|
break;
|
|
|
|
case OP_IN:
|
|
|
|
mode = kCGBlendModeSourceIn;
|
|
|
|
break;
|
|
|
|
case OP_SOURCE:
|
|
|
|
mode = kCGBlendModeCopy;
|
|
|
|
break;
|
|
|
|
case OP_DEST_IN:
|
|
|
|
mode = kCGBlendModeDestinationIn;
|
|
|
|
break;
|
|
|
|
case OP_DEST_OUT:
|
|
|
|
mode = kCGBlendModeDestinationOut;
|
|
|
|
break;
|
|
|
|
case OP_DEST_OVER:
|
|
|
|
mode = kCGBlendModeDestinationOver;
|
|
|
|
break;
|
|
|
|
case OP_DEST_ATOP:
|
|
|
|
mode = kCGBlendModeDestinationAtop;
|
|
|
|
break;
|
|
|
|
case OP_XOR:
|
|
|
|
mode = kCGBlendModeXOR;
|
|
|
|
break;
|
2012-12-12 12:28:17 -08:00
|
|
|
case OP_MULTIPLY:
|
|
|
|
mode = kCGBlendModeMultiply;
|
|
|
|
break;
|
|
|
|
case OP_SCREEN:
|
|
|
|
mode = kCGBlendModeScreen;
|
|
|
|
break;
|
|
|
|
case OP_OVERLAY:
|
|
|
|
mode = kCGBlendModeOverlay;
|
|
|
|
break;
|
|
|
|
case OP_DARKEN:
|
|
|
|
mode = kCGBlendModeDarken;
|
|
|
|
break;
|
|
|
|
case OP_LIGHTEN:
|
|
|
|
mode = kCGBlendModeLighten;
|
|
|
|
break;
|
|
|
|
case OP_COLOR_DODGE:
|
|
|
|
mode = kCGBlendModeColorDodge;
|
|
|
|
break;
|
|
|
|
case OP_COLOR_BURN:
|
|
|
|
mode = kCGBlendModeColorBurn;
|
|
|
|
break;
|
|
|
|
case OP_HARD_LIGHT:
|
|
|
|
mode = kCGBlendModeHardLight;
|
|
|
|
break;
|
|
|
|
case OP_SOFT_LIGHT:
|
|
|
|
mode = kCGBlendModeSoftLight;
|
|
|
|
break;
|
|
|
|
case OP_DIFFERENCE:
|
|
|
|
mode = kCGBlendModeDifference;
|
|
|
|
break;
|
|
|
|
case OP_EXCLUSION:
|
|
|
|
mode = kCGBlendModeExclusion;
|
|
|
|
break;
|
|
|
|
case OP_HUE:
|
|
|
|
mode = kCGBlendModeHue;
|
|
|
|
break;
|
|
|
|
case OP_SATURATION:
|
|
|
|
mode = kCGBlendModeSaturation;
|
|
|
|
break;
|
|
|
|
case OP_COLOR:
|
|
|
|
mode = kCGBlendModeColor;
|
|
|
|
break;
|
|
|
|
case OP_LUMINOSITY:
|
|
|
|
mode = kCGBlendModeLuminosity;
|
|
|
|
break;
|
2012-01-09 10:54:44 -08:00
|
|
|
/*
|
|
|
|
case OP_CLEAR:
|
|
|
|
mode = kCGBlendModeClear;
|
|
|
|
break;*/
|
2011-06-24 10:41:16 -07:00
|
|
|
default:
|
|
|
|
mode = kCGBlendModeNormal;
|
|
|
|
}
|
|
|
|
return mode;
|
|
|
|
}
|
|
|
|
|
2013-07-31 11:46:25 -07:00
|
|
|
static CGInterpolationQuality
|
|
|
|
InterpolationQualityFromFilter(Filter aFilter)
|
|
|
|
{
|
|
|
|
switch (aFilter) {
|
|
|
|
default:
|
|
|
|
case FILTER_LINEAR:
|
|
|
|
return kCGInterpolationLow;
|
|
|
|
case FILTER_POINT:
|
|
|
|
return kCGInterpolationNone;
|
|
|
|
case FILTER_GOOD:
|
|
|
|
return kCGInterpolationDefault;
|
|
|
|
}
|
|
|
|
}
|
2011-06-24 10:41:16 -07:00
|
|
|
|
|
|
|
|
2013-02-12 20:00:54 -08:00
|
|
|
DrawTargetCG::DrawTargetCG() : mCg(nullptr), mSnapshot(nullptr)
|
2011-06-24 10:41:16 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
DrawTargetCG::~DrawTargetCG()
|
|
|
|
{
|
2012-01-30 22:46:54 -08:00
|
|
|
MarkChanged();
|
|
|
|
|
2012-01-09 10:54:44 -08:00
|
|
|
// We need to conditionally release these because Init can fail without initializing these.
|
|
|
|
if (mColorSpace)
|
|
|
|
CGColorSpaceRelease(mColorSpace);
|
|
|
|
if (mCg)
|
|
|
|
CGContextRelease(mCg);
|
2012-01-19 07:33:35 -08:00
|
|
|
free(mData);
|
2011-06-24 10:41:16 -07:00
|
|
|
}
|
|
|
|
|
2012-07-31 08:17:43 -07:00
|
|
|
BackendType
|
|
|
|
DrawTargetCG::GetType() const
|
|
|
|
{
|
|
|
|
// It may be worth spliting Bitmap and IOSurface DrawTarget
|
|
|
|
// into seperate classes.
|
|
|
|
if (GetContextType(mCg) == CG_CONTEXT_TYPE_IOSURFACE) {
|
|
|
|
return BACKEND_COREGRAPHICS_ACCELERATED;
|
|
|
|
} else {
|
|
|
|
return BACKEND_COREGRAPHICS;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-24 10:41:16 -07:00
|
|
|
TemporaryRef<SourceSurface>
|
|
|
|
DrawTargetCG::Snapshot()
|
|
|
|
{
|
2012-01-30 22:46:54 -08:00
|
|
|
if (!mSnapshot) {
|
2012-07-31 08:17:43 -07:00
|
|
|
if (GetContextType(mCg) == CG_CONTEXT_TYPE_IOSURFACE) {
|
|
|
|
return new SourceSurfaceCGIOSurfaceContext(this);
|
|
|
|
} else {
|
|
|
|
mSnapshot = new SourceSurfaceCGBitmapContext(this);
|
|
|
|
}
|
2012-01-30 22:46:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
return mSnapshot;
|
2012-01-09 10:54:44 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
TemporaryRef<DrawTarget>
|
|
|
|
DrawTargetCG::CreateSimilarDrawTarget(const IntSize &aSize, SurfaceFormat aFormat) const
|
|
|
|
{
|
|
|
|
// XXX: in thebes we use CGLayers to do this kind of thing. It probably makes sense
|
|
|
|
// to add that in somehow, but at a higher level
|
|
|
|
RefPtr<DrawTargetCG> newTarget = new DrawTargetCG();
|
2012-07-31 08:17:43 -07:00
|
|
|
if (newTarget->Init(GetType(), aSize, aFormat)) {
|
2012-01-09 10:54:44 -08:00
|
|
|
return newTarget;
|
|
|
|
} else {
|
2012-08-14 11:06:12 -07:00
|
|
|
return nullptr;
|
2012-01-09 10:54:44 -08:00
|
|
|
}
|
2011-06-24 10:41:16 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
TemporaryRef<SourceSurface>
|
|
|
|
DrawTargetCG::CreateSourceSurfaceFromData(unsigned char *aData,
|
|
|
|
const IntSize &aSize,
|
|
|
|
int32_t aStride,
|
|
|
|
SurfaceFormat aFormat) const
|
|
|
|
{
|
|
|
|
RefPtr<SourceSurfaceCG> newSurf = new SourceSurfaceCG();
|
|
|
|
|
2012-01-09 10:54:44 -08:00
|
|
|
if (!newSurf->InitFromData(aData, aSize, aStride, aFormat)) {
|
2012-08-14 11:06:12 -07:00
|
|
|
return nullptr;
|
2011-06-24 10:41:16 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return newSurf;
|
|
|
|
}
|
|
|
|
|
2012-01-30 22:46:54 -08:00
|
|
|
static CGImageRef
|
|
|
|
GetImageFromSourceSurface(SourceSurface *aSurface)
|
|
|
|
{
|
|
|
|
if (aSurface->GetType() == SURFACE_COREGRAPHICS_IMAGE)
|
|
|
|
return static_cast<SourceSurfaceCG*>(aSurface)->GetImage();
|
|
|
|
else if (aSurface->GetType() == SURFACE_COREGRAPHICS_CGCONTEXT)
|
2012-07-31 08:17:43 -07:00
|
|
|
return static_cast<SourceSurfaceCGContext*>(aSurface)->GetImage();
|
2012-01-30 22:46:54 -08:00
|
|
|
else if (aSurface->GetType() == SURFACE_DATA)
|
|
|
|
return static_cast<DataSourceSurfaceCG*>(aSurface)->GetImage();
|
2012-02-02 01:02:06 -08:00
|
|
|
abort();
|
2012-01-30 22:46:54 -08:00
|
|
|
}
|
|
|
|
|
2011-06-24 10:41:16 -07:00
|
|
|
TemporaryRef<SourceSurface>
|
|
|
|
DrawTargetCG::OptimizeSourceSurface(SourceSurface *aSurface) const
|
|
|
|
{
|
2012-08-14 11:06:12 -07:00
|
|
|
return nullptr;
|
2011-06-24 10:41:16 -07:00
|
|
|
}
|
|
|
|
|
2012-01-09 10:54:44 -08:00
|
|
|
class UnboundnessFixer
|
|
|
|
{
|
|
|
|
CGRect mClipBounds;
|
|
|
|
CGLayerRef mLayer;
|
|
|
|
CGContextRef mCg;
|
|
|
|
public:
|
2012-08-14 11:06:12 -07:00
|
|
|
UnboundnessFixer() : mCg(nullptr) {}
|
2012-01-09 10:54:44 -08:00
|
|
|
|
|
|
|
CGContextRef Check(CGContextRef baseCg, CompositionOp blend)
|
|
|
|
{
|
|
|
|
if (!IsOperatorBoundByMask(blend)) {
|
|
|
|
mClipBounds = CGContextGetClipBoundingBox(baseCg);
|
|
|
|
// TransparencyLayers aren't blended using the blend mode so
|
|
|
|
// we are forced to use CGLayers
|
|
|
|
|
|
|
|
//XXX: The size here is in default user space units, of the layer relative to the graphics context.
|
|
|
|
// is the clip bounds still correct if, for example, we have a scale applied to the context?
|
2012-08-14 11:06:12 -07:00
|
|
|
mLayer = CGLayerCreateWithContext(baseCg, mClipBounds.size, nullptr);
|
|
|
|
//XXX: if the size is 0x0 we get a nullptr CGContext back from GetContext
|
2012-01-09 10:54:44 -08:00
|
|
|
mCg = CGLayerGetContext(mLayer);
|
|
|
|
// CGContext's default to have the origin at the bottom left
|
|
|
|
// so flip it to the top left and adjust for the origin
|
|
|
|
// of the layer
|
|
|
|
CGContextTranslateCTM(mCg, -mClipBounds.origin.x, mClipBounds.origin.y + mClipBounds.size.height);
|
|
|
|
CGContextScaleCTM(mCg, 1, -1);
|
|
|
|
|
|
|
|
return mCg;
|
|
|
|
} else {
|
|
|
|
return baseCg;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Fix(CGContextRef baseCg)
|
|
|
|
{
|
|
|
|
if (mCg) {
|
|
|
|
CGContextTranslateCTM(baseCg, 0, mClipBounds.size.height);
|
|
|
|
CGContextScaleCTM(baseCg, 1, -1);
|
|
|
|
mClipBounds.origin.y *= -1;
|
|
|
|
CGContextDrawLayerAtPoint(baseCg, mClipBounds.origin, mLayer);
|
|
|
|
CGContextRelease(mCg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-06-24 10:41:16 -07:00
|
|
|
void
|
|
|
|
DrawTargetCG::DrawSurface(SourceSurface *aSurface,
|
|
|
|
const Rect &aDest,
|
|
|
|
const Rect &aSource,
|
2012-01-09 10:54:44 -08:00
|
|
|
const DrawSurfaceOptions &aSurfOptions,
|
|
|
|
const DrawOptions &aDrawOptions)
|
2011-06-24 10:41:16 -07:00
|
|
|
{
|
2012-01-30 22:46:54 -08:00
|
|
|
MarkChanged();
|
|
|
|
|
2011-06-24 10:41:16 -07:00
|
|
|
CGImageRef image;
|
2012-08-14 11:06:12 -07:00
|
|
|
CGImageRef subimage = nullptr;
|
2012-01-30 22:46:54 -08:00
|
|
|
CGContextSaveGState(mCg);
|
2012-01-09 10:54:44 -08:00
|
|
|
|
2012-01-30 22:46:54 -08:00
|
|
|
CGContextSetBlendMode(mCg, ToBlendMode(aDrawOptions.mCompositionOp));
|
|
|
|
UnboundnessFixer fixer;
|
|
|
|
CGContextRef cg = fixer.Check(mCg, aDrawOptions.mCompositionOp);
|
|
|
|
CGContextSetAlpha(cg, aDrawOptions.mAlpha);
|
2013-07-19 05:18:18 -07:00
|
|
|
CGContextSetShouldAntialias(cg, aDrawOptions.mAntialiasMode != AA_NONE);
|
2012-01-09 10:54:44 -08:00
|
|
|
|
2012-01-30 22:46:54 -08:00
|
|
|
CGContextConcatCTM(cg, GfxMatrixToCGAffineTransform(mTransform));
|
|
|
|
image = GetImageFromSourceSurface(aSurface);
|
|
|
|
/* we have two options here:
|
|
|
|
* - create a subimage -- this is slower
|
|
|
|
* - fancy things with clip and different dest rects */
|
|
|
|
{
|
|
|
|
subimage = CGImageCreateWithImageInRect(image, RectToCGRect(aSource));
|
|
|
|
image = subimage;
|
|
|
|
}
|
2011-06-24 10:41:16 -07:00
|
|
|
|
2012-01-30 22:46:54 -08:00
|
|
|
CGContextScaleCTM(cg, 1, -1);
|
2012-01-09 10:54:44 -08:00
|
|
|
|
2012-01-30 22:46:54 -08:00
|
|
|
CGRect flippedRect = CGRectMake(aDest.x, -(aDest.y + aDest.height),
|
|
|
|
aDest.width, aDest.height);
|
2012-01-09 10:54:44 -08:00
|
|
|
|
2013-07-31 11:46:25 -07:00
|
|
|
CGContextSetInterpolationQuality(cg, InterpolationQualityFromFilter(aSurfOptions.mFilter));
|
2012-01-09 10:54:44 -08:00
|
|
|
|
2012-01-30 22:46:54 -08:00
|
|
|
CGContextDrawImage(cg, flippedRect, image);
|
2012-01-09 10:54:44 -08:00
|
|
|
|
2012-01-30 22:46:54 -08:00
|
|
|
fixer.Fix(mCg);
|
2012-01-09 10:54:44 -08:00
|
|
|
|
2012-01-30 22:46:54 -08:00
|
|
|
CGContextRestoreGState(mCg);
|
2011-06-24 10:41:16 -07:00
|
|
|
|
2012-01-30 22:46:54 -08:00
|
|
|
CGImageRelease(subimage);
|
2011-06-24 10:41:16 -07:00
|
|
|
}
|
|
|
|
|
2012-01-09 10:54:44 -08:00
|
|
|
static CGColorRef ColorToCGColor(CGColorSpaceRef aColorSpace, const Color& aColor)
|
|
|
|
{
|
|
|
|
CGFloat components[4] = {aColor.r, aColor.g, aColor.b, aColor.a};
|
|
|
|
return CGColorCreate(aColorSpace, components);
|
|
|
|
}
|
|
|
|
|
|
|
|
class GradientStopsCG : public GradientStops
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
//XXX: The skia backend uses a vector and passes in aNumStops. It should do better
|
|
|
|
GradientStopsCG(GradientStop* aStops, uint32_t aNumStops, ExtendMode aExtendMode)
|
|
|
|
{
|
2013-07-18 17:08:51 -07:00
|
|
|
mExtend = aExtendMode;
|
|
|
|
if (aExtendMode == EXTEND_CLAMP) {
|
|
|
|
//XXX: do the stops need to be in any particular order?
|
|
|
|
// what should we do about the color space here? we certainly shouldn't be
|
|
|
|
// recreating it all the time
|
|
|
|
std::vector<CGFloat> colors;
|
|
|
|
std::vector<CGFloat> offsets;
|
|
|
|
colors.reserve(aNumStops*4);
|
|
|
|
offsets.reserve(aNumStops);
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < aNumStops; i++) {
|
|
|
|
colors.push_back(aStops[i].color.r);
|
|
|
|
colors.push_back(aStops[i].color.g);
|
|
|
|
colors.push_back(aStops[i].color.b);
|
|
|
|
colors.push_back(aStops[i].color.a);
|
|
|
|
|
|
|
|
offsets.push_back(aStops[i].offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
|
|
|
|
mGradient = CGGradientCreateWithColorComponents(colorSpace,
|
|
|
|
&colors.front(),
|
|
|
|
&offsets.front(),
|
|
|
|
aNumStops);
|
|
|
|
CGColorSpaceRelease(colorSpace);
|
|
|
|
} else {
|
|
|
|
mGradient = nullptr;
|
|
|
|
mStops.reserve(aNumStops);
|
|
|
|
for (uint32_t i = 0; i < aNumStops; i++) {
|
|
|
|
mStops.push_back(aStops[i]);
|
|
|
|
}
|
2012-01-09 10:54:44 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
virtual ~GradientStopsCG() {
|
2013-07-18 17:08:51 -07:00
|
|
|
if (mGradient)
|
|
|
|
CGGradientRelease(mGradient);
|
2012-01-09 10:54:44 -08:00
|
|
|
}
|
2012-07-31 08:17:43 -07:00
|
|
|
// Will always report BACKEND_COREGRAPHICS, but it is compatible
|
|
|
|
// with BACKEND_COREGRAPHICS_ACCELERATED
|
2012-01-09 10:54:44 -08:00
|
|
|
BackendType GetBackendType() const { return BACKEND_COREGRAPHICS; }
|
2013-07-18 17:08:51 -07:00
|
|
|
// XXX this should be a union
|
2012-01-09 10:54:44 -08:00
|
|
|
CGGradientRef mGradient;
|
2013-07-18 17:08:51 -07:00
|
|
|
std::vector<GradientStop> mStops;
|
|
|
|
ExtendMode mExtend;
|
2012-01-09 10:54:44 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
TemporaryRef<GradientStops>
|
|
|
|
DrawTargetCG::CreateGradientStops(GradientStop *aStops, uint32_t aNumStops,
|
|
|
|
ExtendMode aExtendMode) const
|
|
|
|
{
|
|
|
|
return new GradientStopsCG(aStops, aNumStops, aExtendMode);
|
|
|
|
}
|
|
|
|
|
2013-07-18 17:08:51 -07:00
|
|
|
|
|
|
|
static void
|
|
|
|
DrawLinearRepeatingGradient(CGContextRef cg, const LinearGradientPattern &aPattern, const CGRect &aExtents)
|
|
|
|
{
|
|
|
|
GradientStopsCG *stops = static_cast<GradientStopsCG*>(aPattern.mStops.get());
|
|
|
|
CGPoint startPoint = { aPattern.mBegin.x, aPattern.mBegin.y };
|
|
|
|
CGPoint endPoint = { aPattern.mEnd.x, aPattern.mEnd.y };
|
|
|
|
|
|
|
|
// extend the gradient line in multiples of the existing length in both
|
|
|
|
// directions until it crosses an edge of the extents box.
|
|
|
|
double xDiff = aPattern.mEnd.x - aPattern.mBegin.x;
|
|
|
|
double yDiff = aPattern.mEnd.y - aPattern.mBegin.y;
|
|
|
|
|
|
|
|
int repeatCount = 1;
|
|
|
|
// if we don't have a line then we can't extend it
|
|
|
|
if (xDiff || yDiff) {
|
|
|
|
while (startPoint.x > aExtents.origin.x
|
|
|
|
&& startPoint.y > aExtents.origin.y
|
|
|
|
&& startPoint.x < (aExtents.origin.x+aExtents.size.width)
|
|
|
|
&& startPoint.y < (aExtents.origin.y+aExtents.size.height))
|
|
|
|
{
|
|
|
|
startPoint.x -= xDiff;
|
|
|
|
startPoint.y -= yDiff;
|
|
|
|
repeatCount++;
|
|
|
|
}
|
|
|
|
while (endPoint.x > aExtents.origin.x
|
|
|
|
&& endPoint.y > aExtents.origin.y
|
|
|
|
&& endPoint.x < (aExtents.origin.x+aExtents.size.width)
|
|
|
|
&& endPoint.y < (aExtents.origin.y+aExtents.size.height))
|
|
|
|
{
|
|
|
|
endPoint.x += xDiff;
|
|
|
|
endPoint.y += yDiff;
|
|
|
|
repeatCount++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
double scale = 1./repeatCount;
|
|
|
|
|
|
|
|
std::vector<CGFloat> colors;
|
|
|
|
std::vector<CGFloat> offsets;
|
|
|
|
colors.reserve(stops->mStops.size()*repeatCount*4);
|
|
|
|
offsets.reserve(stops->mStops.size()*repeatCount);
|
|
|
|
|
|
|
|
for (int j = 0; j < repeatCount; j++) {
|
|
|
|
for (uint32_t i = 0; i < stops->mStops.size(); i++) {
|
|
|
|
colors.push_back(stops->mStops[i].color.r);
|
|
|
|
colors.push_back(stops->mStops[i].color.g);
|
|
|
|
colors.push_back(stops->mStops[i].color.b);
|
|
|
|
colors.push_back(stops->mStops[i].color.a);
|
|
|
|
|
|
|
|
offsets.push_back((stops->mStops[i].offset + j)*scale);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
|
|
|
|
CGGradientRef gradient = CGGradientCreateWithColorComponents(colorSpace,
|
|
|
|
&colors.front(),
|
|
|
|
&offsets.front(),
|
|
|
|
repeatCount*stops->mStops.size());
|
|
|
|
CGColorSpaceRelease(colorSpace);
|
|
|
|
|
|
|
|
CGContextDrawLinearGradient(cg, gradient, startPoint, endPoint,
|
|
|
|
kCGGradientDrawsBeforeStartLocation | kCGGradientDrawsAfterEndLocation);
|
|
|
|
CGGradientRelease(gradient);
|
|
|
|
}
|
|
|
|
|
|
|
|
static CGPoint CGRectTopLeft(CGRect a)
|
|
|
|
{ return a.origin; }
|
|
|
|
static CGPoint CGRectBottomLeft(CGRect a)
|
|
|
|
{ return CGPointMake(a.origin.x, a.origin.y + a.size.height); }
|
|
|
|
static CGPoint CGRectTopRight(CGRect a)
|
|
|
|
{ return CGPointMake(a.origin.x + a.size.width, a.origin.y); }
|
|
|
|
static CGPoint CGRectBottomRight(CGRect a)
|
|
|
|
{ return CGPointMake(a.origin.x + a.size.width, a.origin.y + a.size.height); }
|
|
|
|
|
|
|
|
static CGFloat
|
|
|
|
CGPointDistance(CGPoint a, CGPoint b)
|
|
|
|
{
|
|
|
|
return hypot(a.x-b.x, a.y-b.y);
|
|
|
|
}
|
|
|
|
|
2012-01-09 10:54:44 -08:00
|
|
|
static void
|
2013-07-18 17:08:51 -07:00
|
|
|
DrawRadialRepeatingGradient(CGContextRef cg, const RadialGradientPattern &aPattern, const CGRect &aExtents)
|
|
|
|
{
|
|
|
|
GradientStopsCG *stops = static_cast<GradientStopsCG*>(aPattern.mStops.get());
|
|
|
|
CGPoint startCenter = { aPattern.mCenter1.x, aPattern.mCenter1.y };
|
|
|
|
CGFloat startRadius = aPattern.mRadius1;
|
|
|
|
CGPoint endCenter = { aPattern.mCenter2.x, aPattern.mCenter2.y };
|
|
|
|
CGFloat endRadius = aPattern.mRadius2;
|
|
|
|
|
|
|
|
// find the maximum distance from endCenter to a corner of aExtents
|
|
|
|
CGFloat minimumEndRadius = endRadius;
|
|
|
|
minimumEndRadius = max(minimumEndRadius, CGPointDistance(endCenter, CGRectTopLeft(aExtents)));
|
|
|
|
minimumEndRadius = max(minimumEndRadius, CGPointDistance(endCenter, CGRectBottomLeft(aExtents)));
|
|
|
|
minimumEndRadius = max(minimumEndRadius, CGPointDistance(endCenter, CGRectTopRight(aExtents)));
|
|
|
|
minimumEndRadius = max(minimumEndRadius, CGPointDistance(endCenter, CGRectBottomRight(aExtents)));
|
|
|
|
|
|
|
|
CGFloat length = endRadius - startRadius;
|
|
|
|
int repeatCount = 1;
|
|
|
|
while (endRadius < minimumEndRadius) {
|
|
|
|
endRadius += length;
|
|
|
|
repeatCount++;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (startRadius-length >= 0) {
|
|
|
|
startRadius -= length;
|
|
|
|
repeatCount++;
|
|
|
|
}
|
|
|
|
|
|
|
|
double scale = 1./repeatCount;
|
|
|
|
|
|
|
|
std::vector<CGFloat> colors;
|
|
|
|
std::vector<CGFloat> offsets;
|
|
|
|
colors.reserve(stops->mStops.size()*repeatCount*4);
|
|
|
|
offsets.reserve(stops->mStops.size()*repeatCount);
|
|
|
|
for (int j = 0; j < repeatCount; j++) {
|
|
|
|
for (uint32_t i = 0; i < stops->mStops.size(); i++) {
|
|
|
|
colors.push_back(stops->mStops[i].color.r);
|
|
|
|
colors.push_back(stops->mStops[i].color.g);
|
|
|
|
colors.push_back(stops->mStops[i].color.b);
|
|
|
|
colors.push_back(stops->mStops[i].color.a);
|
|
|
|
|
|
|
|
offsets.push_back((stops->mStops[i].offset + j)*scale);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
|
|
|
|
CGGradientRef gradient = CGGradientCreateWithColorComponents(colorSpace,
|
|
|
|
&colors.front(),
|
|
|
|
&offsets.front(),
|
|
|
|
repeatCount*stops->mStops.size());
|
|
|
|
CGColorSpaceRelease(colorSpace);
|
|
|
|
|
|
|
|
//XXX: are there degenerate radial gradients that we should avoid drawing?
|
|
|
|
CGContextDrawRadialGradient(cg, gradient, startCenter, startRadius, endCenter, endRadius,
|
|
|
|
kCGGradientDrawsBeforeStartLocation | kCGGradientDrawsAfterEndLocation);
|
|
|
|
CGGradientRelease(gradient);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
DrawGradient(CGContextRef cg, const Pattern &aPattern, const CGRect &aExtents)
|
2012-01-09 10:54:44 -08:00
|
|
|
{
|
|
|
|
if (aPattern.GetType() == PATTERN_LINEAR_GRADIENT) {
|
|
|
|
const LinearGradientPattern& pat = static_cast<const LinearGradientPattern&>(aPattern);
|
|
|
|
GradientStopsCG *stops = static_cast<GradientStopsCG*>(pat.mStops.get());
|
2013-07-16 08:27:12 -07:00
|
|
|
CGContextConcatCTM(cg, GfxMatrixToCGAffineTransform(pat.mMatrix));
|
2013-07-18 17:08:51 -07:00
|
|
|
if (stops->mExtend == EXTEND_CLAMP) {
|
|
|
|
|
|
|
|
// XXX: we should take the m out of the properties of LinearGradientPatterns
|
|
|
|
CGPoint startPoint = { pat.mBegin.x, pat.mBegin.y };
|
|
|
|
CGPoint endPoint = { pat.mEnd.x, pat.mEnd.y };
|
2012-01-09 10:54:44 -08:00
|
|
|
|
2013-07-18 17:08:51 -07:00
|
|
|
// Canvas spec states that we should avoid drawing degenerate gradients (XXX: should this be in common code?)
|
|
|
|
//if (startPoint.x == endPoint.x && startPoint.y == endPoint.y)
|
|
|
|
// return;
|
2012-01-09 10:54:44 -08:00
|
|
|
|
2013-07-18 17:08:51 -07:00
|
|
|
CGContextDrawLinearGradient(cg, stops->mGradient, startPoint, endPoint,
|
|
|
|
kCGGradientDrawsBeforeStartLocation | kCGGradientDrawsAfterEndLocation);
|
|
|
|
} else if (stops->mExtend == EXTEND_REPEAT) {
|
|
|
|
DrawLinearRepeatingGradient(cg, pat, aExtents);
|
|
|
|
}
|
2012-01-09 10:54:44 -08:00
|
|
|
} else if (aPattern.GetType() == PATTERN_RADIAL_GRADIENT) {
|
|
|
|
const RadialGradientPattern& pat = static_cast<const RadialGradientPattern&>(aPattern);
|
2013-07-16 08:27:12 -07:00
|
|
|
CGContextConcatCTM(cg, GfxMatrixToCGAffineTransform(pat.mMatrix));
|
2012-01-09 10:54:44 -08:00
|
|
|
GradientStopsCG *stops = static_cast<GradientStopsCG*>(pat.mStops.get());
|
2013-07-18 17:08:51 -07:00
|
|
|
if (stops->mExtend == EXTEND_CLAMP) {
|
|
|
|
|
|
|
|
// XXX: we should take the m out of the properties of RadialGradientPatterns
|
|
|
|
CGPoint startCenter = { pat.mCenter1.x, pat.mCenter1.y };
|
|
|
|
CGFloat startRadius = pat.mRadius1;
|
|
|
|
CGPoint endCenter = { pat.mCenter2.x, pat.mCenter2.y };
|
|
|
|
CGFloat endRadius = pat.mRadius2;
|
|
|
|
|
|
|
|
//XXX: are there degenerate radial gradients that we should avoid drawing?
|
|
|
|
CGContextDrawRadialGradient(cg, stops->mGradient, startCenter, startRadius, endCenter, endRadius,
|
|
|
|
kCGGradientDrawsBeforeStartLocation | kCGGradientDrawsAfterEndLocation);
|
|
|
|
} else if (stops->mExtend == EXTEND_REPEAT) {
|
|
|
|
DrawRadialRepeatingGradient(cg, pat, aExtents);
|
|
|
|
}
|
2012-01-09 10:54:44 -08:00
|
|
|
} else {
|
|
|
|
assert(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
drawPattern(void *info, CGContextRef context)
|
|
|
|
{
|
|
|
|
CGImageRef image = static_cast<CGImageRef>(info);
|
|
|
|
CGRect rect = {{0, 0},
|
|
|
|
{static_cast<CGFloat>(CGImageGetWidth(image)),
|
|
|
|
static_cast<CGFloat>(CGImageGetHeight(image))}};
|
|
|
|
CGContextDrawImage(context, rect, image);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
releaseInfo(void *info)
|
|
|
|
{
|
|
|
|
CGImageRef image = static_cast<CGImageRef>(info);
|
|
|
|
CGImageRelease(image);
|
|
|
|
}
|
|
|
|
|
|
|
|
CGPatternCallbacks patternCallbacks = {
|
|
|
|
0,
|
|
|
|
drawPattern,
|
|
|
|
releaseInfo
|
|
|
|
};
|
|
|
|
|
|
|
|
static bool
|
|
|
|
isGradient(const Pattern &aPattern)
|
|
|
|
{
|
|
|
|
return aPattern.GetType() == PATTERN_LINEAR_GRADIENT || aPattern.GetType() == PATTERN_RADIAL_GRADIENT;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* CoreGraphics patterns ignore the userspace transform so
|
|
|
|
* we need to multiply it in */
|
|
|
|
static CGPatternRef
|
|
|
|
CreateCGPattern(const Pattern &aPattern, CGAffineTransform aUserSpace)
|
|
|
|
{
|
|
|
|
const SurfacePattern& pat = static_cast<const SurfacePattern&>(aPattern);
|
|
|
|
// XXX: is .get correct here?
|
2012-01-30 22:46:54 -08:00
|
|
|
CGImageRef image = GetImageFromSourceSurface(pat.mSurface.get());
|
2012-01-09 10:54:44 -08:00
|
|
|
CGFloat xStep, yStep;
|
|
|
|
switch (pat.mExtendMode) {
|
|
|
|
case EXTEND_CLAMP:
|
|
|
|
// The 1 << 22 comes from Webkit see Pattern::createPlatformPattern() in PatternCG.cpp for more info
|
|
|
|
xStep = static_cast<CGFloat>(1 << 22);
|
|
|
|
yStep = static_cast<CGFloat>(1 << 22);
|
|
|
|
break;
|
|
|
|
case EXTEND_REFLECT:
|
|
|
|
assert(0);
|
|
|
|
case EXTEND_REPEAT:
|
|
|
|
xStep = static_cast<CGFloat>(CGImageGetWidth(image));
|
|
|
|
yStep = static_cast<CGFloat>(CGImageGetHeight(image));
|
|
|
|
// webkit uses wkCGPatternCreateWithImageAndTransform a wrapper around CGPatternCreateWithImage2
|
|
|
|
// this is done to avoid pixel-cracking along pattern boundaries
|
|
|
|
// (see https://bugs.webkit.org/show_bug.cgi?id=53055)
|
|
|
|
// typedef enum {
|
|
|
|
// wkPatternTilingNoDistortion,
|
|
|
|
// wkPatternTilingConstantSpacingMinimalDistortion,
|
|
|
|
// wkPatternTilingConstantSpacing
|
|
|
|
// } wkPatternTiling;
|
|
|
|
// extern CGPatternRef (*wkCGPatternCreateWithImageAndTransform)(CGImageRef, CGAffineTransform, int);
|
|
|
|
}
|
|
|
|
|
|
|
|
//XXX: We should be using CGContextDrawTiledImage when we can. Even though it
|
|
|
|
// creates a pattern, it seems to go down a faster path than using a delegate
|
|
|
|
// like we do below
|
|
|
|
CGRect bounds = {
|
|
|
|
{0, 0,},
|
|
|
|
{static_cast<CGFloat>(CGImageGetWidth(image)), static_cast<CGFloat>(CGImageGetHeight(image))}
|
|
|
|
};
|
2012-11-14 16:52:02 -08:00
|
|
|
CGAffineTransform transform =
|
|
|
|
CGAffineTransformConcat(CGAffineTransformConcat(CGAffineTransformMakeScale(1,
|
|
|
|
-1),
|
|
|
|
GfxMatrixToCGAffineTransform(pat.mMatrix)),
|
|
|
|
aUserSpace);
|
2012-01-09 10:54:44 -08:00
|
|
|
transform = CGAffineTransformTranslate(transform, 0, -static_cast<float>(CGImageGetHeight(image)));
|
|
|
|
return CGPatternCreate(CGImageRetain(image), bounds, transform, xStep, yStep, kCGPatternTilingConstantSpacing,
|
|
|
|
true, &patternCallbacks);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
SetFillFromPattern(CGContextRef cg, CGColorSpaceRef aColorSpace, const Pattern &aPattern)
|
|
|
|
{
|
|
|
|
assert(!isGradient(aPattern));
|
|
|
|
if (aPattern.GetType() == PATTERN_COLOR) {
|
|
|
|
|
|
|
|
const Color& color = static_cast<const ColorPattern&>(aPattern).mColor;
|
|
|
|
//XXX: we should cache colors
|
|
|
|
CGColorRef cgcolor = ColorToCGColor(aColorSpace, color);
|
|
|
|
CGContextSetFillColorWithColor(cg, cgcolor);
|
|
|
|
CGColorRelease(cgcolor);
|
|
|
|
} else if (aPattern.GetType() == PATTERN_SURFACE) {
|
|
|
|
|
|
|
|
CGColorSpaceRef patternSpace;
|
2012-08-14 11:06:12 -07:00
|
|
|
patternSpace = CGColorSpaceCreatePattern (nullptr);
|
2012-01-09 10:54:44 -08:00
|
|
|
CGContextSetFillColorSpace(cg, patternSpace);
|
|
|
|
CGColorSpaceRelease(patternSpace);
|
|
|
|
|
|
|
|
CGPatternRef pattern = CreateCGPattern(aPattern, CGContextGetCTM(cg));
|
2013-07-12 07:01:58 -07:00
|
|
|
const SurfacePattern& pat = static_cast<const SurfacePattern&>(aPattern);
|
2013-07-31 11:46:25 -07:00
|
|
|
CGContextSetInterpolationQuality(cg, InterpolationQualityFromFilter(pat.mFilter));
|
2012-01-09 10:54:44 -08:00
|
|
|
CGFloat alpha = 1.;
|
|
|
|
CGContextSetFillPattern(cg, pattern, &alpha);
|
|
|
|
CGPatternRelease(pattern);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
SetStrokeFromPattern(CGContextRef cg, CGColorSpaceRef aColorSpace, const Pattern &aPattern)
|
|
|
|
{
|
|
|
|
assert(!isGradient(aPattern));
|
|
|
|
if (aPattern.GetType() == PATTERN_COLOR) {
|
|
|
|
const Color& color = static_cast<const ColorPattern&>(aPattern).mColor;
|
|
|
|
//XXX: we should cache colors
|
|
|
|
CGColorRef cgcolor = ColorToCGColor(aColorSpace, color);
|
|
|
|
CGContextSetStrokeColorWithColor(cg, cgcolor);
|
|
|
|
CGColorRelease(cgcolor);
|
|
|
|
} else if (aPattern.GetType() == PATTERN_SURFACE) {
|
|
|
|
CGColorSpaceRef patternSpace;
|
2012-08-14 11:06:12 -07:00
|
|
|
patternSpace = CGColorSpaceCreatePattern (nullptr);
|
2012-01-09 10:54:44 -08:00
|
|
|
CGContextSetStrokeColorSpace(cg, patternSpace);
|
|
|
|
CGColorSpaceRelease(patternSpace);
|
|
|
|
|
|
|
|
CGPatternRef pattern = CreateCGPattern(aPattern, CGContextGetCTM(cg));
|
2013-07-12 07:01:58 -07:00
|
|
|
const SurfacePattern& pat = static_cast<const SurfacePattern&>(aPattern);
|
2013-07-31 11:46:25 -07:00
|
|
|
CGContextSetInterpolationQuality(cg, InterpolationQualityFromFilter(pat.mFilter));
|
2012-01-09 10:54:44 -08:00
|
|
|
CGFloat alpha = 1.;
|
|
|
|
CGContextSetStrokePattern(cg, pattern, &alpha);
|
|
|
|
CGPatternRelease(pattern);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
Bug 892964. Rewrite DrawTargetCG::MaskSurface to avoid using FillRect(). r=mattwoodrow
With FillRect we were setting the transform twice.
The patch looks really bad because I needed to move the implementation of
MaskSurface so that it was below the helper methods.
The actual change is:
- FillRect(Rect(aOffset.x, aOffset.y, size.width, size.height), aSource, aDrawOptions);
-
+ if (isGradient(aSource)) {
+ // we shouldn't need to clip to an additional rectangle
+ // as the cliping to the mask should be sufficient.
+ DrawGradient(cg, aSource);
+ } else {
+ SetFillFromPattern(cg, mColorSpace, aSource);
+ CGContextFillRect(cg, CGRectMake(aOffset.x, aOffset.y, size.width, size.height));
+ }
This also avoids setting up other state like alpha and the unboundness fixer twice.
--HG--
extra : rebase_source : 762f2d94ddaff129d584aab1e1f429ddfea99277
2013-07-08 14:35:56 -07:00
|
|
|
void
|
|
|
|
DrawTargetCG::MaskSurface(const Pattern &aSource,
|
|
|
|
SourceSurface *aMask,
|
|
|
|
Point aOffset,
|
|
|
|
const DrawOptions &aDrawOptions)
|
|
|
|
{
|
|
|
|
MarkChanged();
|
|
|
|
|
|
|
|
CGImageRef image;
|
|
|
|
CGContextSaveGState(mCg);
|
|
|
|
|
|
|
|
CGContextSetBlendMode(mCg, ToBlendMode(aDrawOptions.mCompositionOp));
|
|
|
|
UnboundnessFixer fixer;
|
|
|
|
CGContextRef cg = fixer.Check(mCg, aDrawOptions.mCompositionOp);
|
|
|
|
CGContextSetAlpha(cg, aDrawOptions.mAlpha);
|
2013-07-19 05:18:18 -07:00
|
|
|
CGContextSetShouldAntialias(cg, aDrawOptions.mAntialiasMode != AA_NONE);
|
Bug 892964. Rewrite DrawTargetCG::MaskSurface to avoid using FillRect(). r=mattwoodrow
With FillRect we were setting the transform twice.
The patch looks really bad because I needed to move the implementation of
MaskSurface so that it was below the helper methods.
The actual change is:
- FillRect(Rect(aOffset.x, aOffset.y, size.width, size.height), aSource, aDrawOptions);
-
+ if (isGradient(aSource)) {
+ // we shouldn't need to clip to an additional rectangle
+ // as the cliping to the mask should be sufficient.
+ DrawGradient(cg, aSource);
+ } else {
+ SetFillFromPattern(cg, mColorSpace, aSource);
+ CGContextFillRect(cg, CGRectMake(aOffset.x, aOffset.y, size.width, size.height));
+ }
This also avoids setting up other state like alpha and the unboundness fixer twice.
--HG--
extra : rebase_source : 762f2d94ddaff129d584aab1e1f429ddfea99277
2013-07-08 14:35:56 -07:00
|
|
|
|
|
|
|
CGContextConcatCTM(cg, GfxMatrixToCGAffineTransform(mTransform));
|
|
|
|
image = GetImageFromSourceSurface(aMask);
|
|
|
|
|
|
|
|
// use a negative-y so that the mask image draws right ways up
|
|
|
|
CGContextScaleCTM(cg, 1, -1);
|
|
|
|
|
|
|
|
IntSize size = aMask->GetSize();
|
|
|
|
|
|
|
|
CGContextClipToMask(cg, CGRectMake(aOffset.x, -(aOffset.y + size.height), size.width, size.height), image);
|
|
|
|
|
|
|
|
CGContextScaleCTM(cg, 1, -1);
|
|
|
|
if (isGradient(aSource)) {
|
|
|
|
// we shouldn't need to clip to an additional rectangle
|
|
|
|
// as the cliping to the mask should be sufficient.
|
2013-07-18 17:08:51 -07:00
|
|
|
DrawGradient(cg, aSource, CGRectMake(aOffset.x, aOffset.y, size.width, size.height));
|
Bug 892964. Rewrite DrawTargetCG::MaskSurface to avoid using FillRect(). r=mattwoodrow
With FillRect we were setting the transform twice.
The patch looks really bad because I needed to move the implementation of
MaskSurface so that it was below the helper methods.
The actual change is:
- FillRect(Rect(aOffset.x, aOffset.y, size.width, size.height), aSource, aDrawOptions);
-
+ if (isGradient(aSource)) {
+ // we shouldn't need to clip to an additional rectangle
+ // as the cliping to the mask should be sufficient.
+ DrawGradient(cg, aSource);
+ } else {
+ SetFillFromPattern(cg, mColorSpace, aSource);
+ CGContextFillRect(cg, CGRectMake(aOffset.x, aOffset.y, size.width, size.height));
+ }
This also avoids setting up other state like alpha and the unboundness fixer twice.
--HG--
extra : rebase_source : 762f2d94ddaff129d584aab1e1f429ddfea99277
2013-07-08 14:35:56 -07:00
|
|
|
} else {
|
|
|
|
SetFillFromPattern(cg, mColorSpace, aSource);
|
|
|
|
CGContextFillRect(cg, CGRectMake(aOffset.x, aOffset.y, size.width, size.height));
|
|
|
|
}
|
|
|
|
|
|
|
|
fixer.Fix(mCg);
|
|
|
|
|
|
|
|
CGContextRestoreGState(mCg);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-01-09 10:54:44 -08:00
|
|
|
|
2011-06-24 10:41:16 -07:00
|
|
|
void
|
|
|
|
DrawTargetCG::FillRect(const Rect &aRect,
|
|
|
|
const Pattern &aPattern,
|
2012-01-09 10:54:44 -08:00
|
|
|
const DrawOptions &aDrawOptions)
|
2011-06-24 10:41:16 -07:00
|
|
|
{
|
2012-01-30 22:46:54 -08:00
|
|
|
MarkChanged();
|
|
|
|
|
2012-01-09 10:54:44 -08:00
|
|
|
CGContextSaveGState(mCg);
|
|
|
|
|
|
|
|
UnboundnessFixer fixer;
|
|
|
|
CGContextRef cg = fixer.Check(mCg, aDrawOptions.mCompositionOp);
|
|
|
|
CGContextSetAlpha(mCg, aDrawOptions.mAlpha);
|
2013-07-19 05:18:18 -07:00
|
|
|
CGContextSetShouldAntialias(cg, aDrawOptions.mAntialiasMode != AA_NONE);
|
2012-01-09 10:54:44 -08:00
|
|
|
CGContextSetBlendMode(mCg, ToBlendMode(aDrawOptions.mCompositionOp));
|
|
|
|
|
|
|
|
CGContextConcatCTM(cg, GfxMatrixToCGAffineTransform(mTransform));
|
|
|
|
|
|
|
|
if (isGradient(aPattern)) {
|
|
|
|
CGContextClipToRect(cg, RectToCGRect(aRect));
|
2013-07-18 17:08:51 -07:00
|
|
|
DrawGradient(cg, aPattern, RectToCGRect(aRect));
|
2012-01-09 10:54:44 -08:00
|
|
|
} else {
|
2013-07-19 17:39:31 -07:00
|
|
|
if (aPattern.GetType() == PATTERN_SURFACE && static_cast<const SurfacePattern&>(aPattern).mExtendMode != EXTEND_REPEAT) {
|
|
|
|
// SetFillFromPattern can handle this case but using CGContextDrawImage
|
|
|
|
// should give us better performance, better output, smaller PDF and
|
|
|
|
// matches what cairo does.
|
|
|
|
const SurfacePattern& pat = static_cast<const SurfacePattern&>(aPattern);
|
|
|
|
CGImageRef image = GetImageFromSourceSurface(pat.mSurface.get());
|
|
|
|
CGContextClipToRect(cg, RectToCGRect(aRect));
|
|
|
|
CGContextConcatCTM(cg, GfxMatrixToCGAffineTransform(pat.mMatrix));
|
|
|
|
CGContextTranslateCTM(cg, 0, CGImageGetHeight(image));
|
|
|
|
CGContextScaleCTM(cg, 1, -1);
|
|
|
|
|
|
|
|
CGRect imageRect = CGRectMake(0, 0, CGImageGetWidth(image), CGImageGetHeight(image));
|
|
|
|
|
2013-07-31 11:46:25 -07:00
|
|
|
CGContextSetInterpolationQuality(cg, InterpolationQualityFromFilter(pat.mFilter));
|
2013-07-19 17:39:31 -07:00
|
|
|
|
|
|
|
CGContextDrawImage(cg, imageRect, image);
|
|
|
|
} else {
|
|
|
|
SetFillFromPattern(cg, mColorSpace, aPattern);
|
|
|
|
CGContextFillRect(cg, RectToCGRect(aRect));
|
|
|
|
}
|
2011-06-24 10:41:16 -07:00
|
|
|
}
|
|
|
|
|
2012-01-09 10:54:44 -08:00
|
|
|
fixer.Fix(mCg);
|
|
|
|
CGContextRestoreGState(mCg);
|
2011-06-24 10:41:16 -07:00
|
|
|
}
|
|
|
|
|
2012-01-09 10:54:44 -08:00
|
|
|
void
|
|
|
|
DrawTargetCG::StrokeLine(const Point &p1, const Point &p2, const Pattern &aPattern, const StrokeOptions &aStrokeOptions, const DrawOptions &aDrawOptions)
|
|
|
|
{
|
2012-01-30 22:46:54 -08:00
|
|
|
MarkChanged();
|
|
|
|
|
2012-01-09 10:54:44 -08:00
|
|
|
CGContextSaveGState(mCg);
|
|
|
|
|
|
|
|
UnboundnessFixer fixer;
|
|
|
|
CGContextRef cg = fixer.Check(mCg, aDrawOptions.mCompositionOp);
|
|
|
|
CGContextSetAlpha(mCg, aDrawOptions.mAlpha);
|
2013-07-19 05:18:18 -07:00
|
|
|
CGContextSetShouldAntialias(cg, aDrawOptions.mAntialiasMode != AA_NONE);
|
2012-01-09 10:54:44 -08:00
|
|
|
CGContextSetBlendMode(mCg, ToBlendMode(aDrawOptions.mCompositionOp));
|
|
|
|
|
|
|
|
CGContextConcatCTM(cg, GfxMatrixToCGAffineTransform(mTransform));
|
|
|
|
|
|
|
|
CGContextBeginPath(cg);
|
|
|
|
CGContextMoveToPoint(cg, p1.x, p1.y);
|
|
|
|
CGContextAddLineToPoint(cg, p2.x, p2.y);
|
|
|
|
|
|
|
|
SetStrokeOptions(cg, aStrokeOptions);
|
|
|
|
|
|
|
|
if (isGradient(aPattern)) {
|
|
|
|
CGContextReplacePathWithStrokedPath(cg);
|
2013-07-18 17:08:51 -07:00
|
|
|
CGRect extents = CGContextGetPathBoundingBox(cg);
|
2012-01-09 10:54:44 -08:00
|
|
|
//XXX: should we use EO clip here?
|
|
|
|
CGContextClip(cg);
|
2013-07-18 17:08:51 -07:00
|
|
|
DrawGradient(cg, aPattern, extents);
|
2012-01-09 10:54:44 -08:00
|
|
|
} else {
|
|
|
|
SetStrokeFromPattern(cg, mColorSpace, aPattern);
|
|
|
|
CGContextStrokePath(cg);
|
|
|
|
}
|
|
|
|
|
|
|
|
fixer.Fix(mCg);
|
|
|
|
CGContextRestoreGState(mCg);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DrawTargetCG::StrokeRect(const Rect &aRect,
|
|
|
|
const Pattern &aPattern,
|
|
|
|
const StrokeOptions &aStrokeOptions,
|
|
|
|
const DrawOptions &aDrawOptions)
|
|
|
|
{
|
2012-01-30 22:46:54 -08:00
|
|
|
MarkChanged();
|
|
|
|
|
2012-01-09 10:54:44 -08:00
|
|
|
CGContextSaveGState(mCg);
|
|
|
|
|
|
|
|
UnboundnessFixer fixer;
|
|
|
|
CGContextRef cg = fixer.Check(mCg, aDrawOptions.mCompositionOp);
|
|
|
|
CGContextSetAlpha(mCg, aDrawOptions.mAlpha);
|
2013-07-19 05:18:18 -07:00
|
|
|
CGContextSetShouldAntialias(cg, aDrawOptions.mAntialiasMode != AA_NONE);
|
2012-01-09 10:54:44 -08:00
|
|
|
CGContextSetBlendMode(mCg, ToBlendMode(aDrawOptions.mCompositionOp));
|
|
|
|
|
|
|
|
CGContextConcatCTM(cg, GfxMatrixToCGAffineTransform(mTransform));
|
|
|
|
|
2013-07-12 14:19:28 -07:00
|
|
|
SetStrokeOptions(cg, aStrokeOptions);
|
2012-01-09 10:54:44 -08:00
|
|
|
|
|
|
|
if (isGradient(aPattern)) {
|
|
|
|
// There's no CGContextClipStrokeRect so we do it by hand
|
|
|
|
CGContextBeginPath(cg);
|
|
|
|
CGContextAddRect(cg, RectToCGRect(aRect));
|
|
|
|
CGContextReplacePathWithStrokedPath(cg);
|
2013-07-18 17:08:51 -07:00
|
|
|
CGRect extents = CGContextGetPathBoundingBox(cg);
|
2012-01-09 10:54:44 -08:00
|
|
|
//XXX: should we use EO clip here?
|
|
|
|
CGContextClip(cg);
|
2013-07-18 17:08:51 -07:00
|
|
|
DrawGradient(cg, aPattern, extents);
|
2012-01-09 10:54:44 -08:00
|
|
|
} else {
|
|
|
|
SetStrokeFromPattern(cg, mColorSpace, aPattern);
|
|
|
|
CGContextStrokeRect(cg, RectToCGRect(aRect));
|
|
|
|
}
|
|
|
|
|
|
|
|
fixer.Fix(mCg);
|
|
|
|
CGContextRestoreGState(mCg);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
DrawTargetCG::ClearRect(const Rect &aRect)
|
|
|
|
{
|
2012-01-30 22:46:54 -08:00
|
|
|
MarkChanged();
|
|
|
|
|
2012-01-09 10:54:44 -08:00
|
|
|
CGContextSaveGState(mCg);
|
|
|
|
CGContextConcatCTM(mCg, GfxMatrixToCGAffineTransform(mTransform));
|
|
|
|
|
|
|
|
CGContextClearRect(mCg, RectToCGRect(aRect));
|
|
|
|
|
|
|
|
CGContextRestoreGState(mCg);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DrawTargetCG::Stroke(const Path *aPath, const Pattern &aPattern, const StrokeOptions &aStrokeOptions, const DrawOptions &aDrawOptions)
|
|
|
|
{
|
2012-01-30 22:46:54 -08:00
|
|
|
MarkChanged();
|
|
|
|
|
2012-01-09 10:54:44 -08:00
|
|
|
CGContextSaveGState(mCg);
|
|
|
|
|
|
|
|
UnboundnessFixer fixer;
|
|
|
|
CGContextRef cg = fixer.Check(mCg, aDrawOptions.mCompositionOp);
|
|
|
|
CGContextSetAlpha(mCg, aDrawOptions.mAlpha);
|
2013-07-19 05:18:18 -07:00
|
|
|
CGContextSetShouldAntialias(cg, aDrawOptions.mAntialiasMode != AA_NONE);
|
2012-01-09 10:54:44 -08:00
|
|
|
CGContextSetBlendMode(mCg, ToBlendMode(aDrawOptions.mCompositionOp));
|
|
|
|
|
|
|
|
CGContextConcatCTM(cg, GfxMatrixToCGAffineTransform(mTransform));
|
|
|
|
|
|
|
|
|
|
|
|
CGContextBeginPath(cg);
|
|
|
|
|
|
|
|
assert(aPath->GetBackendType() == BACKEND_COREGRAPHICS);
|
|
|
|
const PathCG *cgPath = static_cast<const PathCG*>(aPath);
|
|
|
|
CGContextAddPath(cg, cgPath->GetPath());
|
|
|
|
|
|
|
|
SetStrokeOptions(cg, aStrokeOptions);
|
|
|
|
|
|
|
|
if (isGradient(aPattern)) {
|
|
|
|
CGContextReplacePathWithStrokedPath(cg);
|
2013-07-18 17:08:51 -07:00
|
|
|
CGRect extents = CGContextGetPathBoundingBox(cg);
|
2012-01-09 10:54:44 -08:00
|
|
|
//XXX: should we use EO clip here?
|
|
|
|
CGContextClip(cg);
|
2013-07-18 17:08:51 -07:00
|
|
|
DrawGradient(cg, aPattern, extents);
|
2012-01-09 10:54:44 -08:00
|
|
|
} else {
|
|
|
|
// XXX: we could put fill mode into the path fill rule if we wanted
|
|
|
|
|
|
|
|
SetStrokeFromPattern(cg, mColorSpace, aPattern);
|
|
|
|
CGContextStrokePath(cg);
|
|
|
|
}
|
|
|
|
|
|
|
|
fixer.Fix(mCg);
|
|
|
|
CGContextRestoreGState(mCg);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DrawTargetCG::Fill(const Path *aPath, const Pattern &aPattern, const DrawOptions &aDrawOptions)
|
|
|
|
{
|
2012-01-30 22:46:54 -08:00
|
|
|
MarkChanged();
|
|
|
|
|
2012-01-09 10:54:44 -08:00
|
|
|
assert(aPath->GetBackendType() == BACKEND_COREGRAPHICS);
|
|
|
|
|
|
|
|
CGContextSaveGState(mCg);
|
|
|
|
|
|
|
|
CGContextSetBlendMode(mCg, ToBlendMode(aDrawOptions.mCompositionOp));
|
|
|
|
UnboundnessFixer fixer;
|
|
|
|
CGContextRef cg = fixer.Check(mCg, aDrawOptions.mCompositionOp);
|
|
|
|
CGContextSetAlpha(cg, aDrawOptions.mAlpha);
|
2013-07-19 05:18:18 -07:00
|
|
|
CGContextSetShouldAntialias(cg, aDrawOptions.mAntialiasMode != AA_NONE);
|
2012-01-09 10:54:44 -08:00
|
|
|
|
|
|
|
CGContextConcatCTM(cg, GfxMatrixToCGAffineTransform(mTransform));
|
|
|
|
|
2012-02-24 21:35:39 -08:00
|
|
|
CGContextBeginPath(cg);
|
|
|
|
// XXX: we could put fill mode into the path fill rule if we wanted
|
|
|
|
const PathCG *cgPath = static_cast<const PathCG*>(aPath);
|
|
|
|
|
2012-01-09 10:54:44 -08:00
|
|
|
if (isGradient(aPattern)) {
|
2012-02-24 21:35:39 -08:00
|
|
|
// setup a clip to draw the gradient through
|
2013-07-18 17:08:51 -07:00
|
|
|
CGRect extents;
|
2012-02-24 21:35:39 -08:00
|
|
|
if (CGPathIsEmpty(cgPath->GetPath())) {
|
|
|
|
// Adding an empty path will cause us not to clip
|
|
|
|
// so clip everything explicitly
|
|
|
|
CGContextClipToRect(mCg, CGRectZero);
|
2013-07-18 17:08:51 -07:00
|
|
|
extents = CGRectZero;
|
2012-02-24 21:35:39 -08:00
|
|
|
} else {
|
|
|
|
CGContextAddPath(cg, cgPath->GetPath());
|
2013-07-18 17:08:51 -07:00
|
|
|
extents = CGContextGetPathBoundingBox(cg);
|
2012-02-24 21:35:39 -08:00
|
|
|
if (cgPath->GetFillRule() == FILL_EVEN_ODD)
|
|
|
|
CGContextEOClip(mCg);
|
|
|
|
else
|
|
|
|
CGContextClip(mCg);
|
|
|
|
}
|
|
|
|
|
2013-07-18 17:08:51 -07:00
|
|
|
DrawGradient(cg, aPattern, extents);
|
2012-01-09 10:54:44 -08:00
|
|
|
} else {
|
|
|
|
CGContextAddPath(cg, cgPath->GetPath());
|
|
|
|
|
|
|
|
SetFillFromPattern(cg, mColorSpace, aPattern);
|
|
|
|
|
|
|
|
if (cgPath->GetFillRule() == FILL_EVEN_ODD)
|
|
|
|
CGContextEOFillPath(cg);
|
|
|
|
else
|
|
|
|
CGContextFillPath(cg);
|
|
|
|
}
|
|
|
|
|
|
|
|
fixer.Fix(mCg);
|
|
|
|
CGContextRestoreGState(mCg);
|
|
|
|
}
|
|
|
|
|
2013-07-18 17:08:51 -07:00
|
|
|
CGRect ComputeGlyphsExtents(CGRect *bboxes, CGPoint *positions, CFIndex count, float scale)
|
|
|
|
{
|
|
|
|
CGFloat x1, x2, y1, y2;
|
|
|
|
if (count < 1)
|
|
|
|
return CGRectZero;
|
|
|
|
|
|
|
|
x1 = bboxes[0].origin.x + positions[0].x;
|
|
|
|
x2 = bboxes[0].origin.x + positions[0].x + scale*bboxes[0].size.width;
|
|
|
|
y1 = bboxes[0].origin.y + positions[0].y;
|
|
|
|
y2 = bboxes[0].origin.y + positions[0].y + scale*bboxes[0].size.height;
|
|
|
|
|
|
|
|
// accumulate max and minimum coordinates
|
|
|
|
for (int i = 1; i < count; i++) {
|
|
|
|
x1 = min(x1, bboxes[i].origin.x + positions[i].x);
|
|
|
|
y1 = min(y1, bboxes[i].origin.y + positions[i].y);
|
|
|
|
x2 = max(x2, bboxes[i].origin.x + positions[i].x + scale*bboxes[i].size.width);
|
|
|
|
y2 = max(y2, bboxes[i].origin.y + positions[i].y + scale*bboxes[i].size.height);
|
|
|
|
}
|
|
|
|
|
|
|
|
CGRect extents = {{x1, y1}, {x2-x1, y2-y1}};
|
|
|
|
return extents;
|
|
|
|
}
|
|
|
|
|
2012-01-09 10:54:44 -08:00
|
|
|
|
|
|
|
void
|
2012-03-19 12:20:17 -07:00
|
|
|
DrawTargetCG::FillGlyphs(ScaledFont *aFont, const GlyphBuffer &aBuffer, const Pattern &aPattern, const DrawOptions &aDrawOptions,
|
|
|
|
const GlyphRenderingOptions*)
|
2012-01-09 10:54:44 -08:00
|
|
|
{
|
2012-01-30 22:46:54 -08:00
|
|
|
MarkChanged();
|
|
|
|
|
2012-01-09 10:54:44 -08:00
|
|
|
assert(aBuffer.mNumGlyphs);
|
|
|
|
CGContextSaveGState(mCg);
|
|
|
|
|
|
|
|
CGContextSetBlendMode(mCg, ToBlendMode(aDrawOptions.mCompositionOp));
|
|
|
|
UnboundnessFixer fixer;
|
|
|
|
CGContextRef cg = fixer.Check(mCg, aDrawOptions.mCompositionOp);
|
|
|
|
CGContextSetAlpha(cg, aDrawOptions.mAlpha);
|
2013-07-19 05:18:18 -07:00
|
|
|
CGContextSetShouldAntialias(cg, aDrawOptions.mAntialiasMode != AA_NONE);
|
2013-07-29 13:00:53 -07:00
|
|
|
if (aDrawOptions.mAntialiasMode != AA_DEFAULT) {
|
|
|
|
CGContextSetShouldSmoothFonts(cg, aDrawOptions.mAntialiasMode == AA_SUBPIXEL);
|
|
|
|
}
|
2012-01-09 10:54:44 -08:00
|
|
|
|
|
|
|
CGContextConcatCTM(cg, GfxMatrixToCGAffineTransform(mTransform));
|
|
|
|
|
2012-10-23 02:07:14 -07:00
|
|
|
ScaledFontMac* macFont = static_cast<ScaledFontMac*>(aFont);
|
2012-01-09 10:54:44 -08:00
|
|
|
|
|
|
|
//XXX: we should use a stack vector here when we have a class like that
|
|
|
|
std::vector<CGGlyph> glyphs;
|
|
|
|
std::vector<CGPoint> positions;
|
|
|
|
glyphs.resize(aBuffer.mNumGlyphs);
|
|
|
|
positions.resize(aBuffer.mNumGlyphs);
|
|
|
|
|
|
|
|
// Handle the flip
|
2012-05-21 07:22:11 -07:00
|
|
|
CGAffineTransform matrix = CGAffineTransformMakeScale(1, -1);
|
|
|
|
CGContextConcatCTM(cg, matrix);
|
|
|
|
// CGContextSetTextMatrix works differently with kCGTextClip && kCGTextFill
|
|
|
|
// It seems that it transforms the positions with TextFill and not with TextClip
|
|
|
|
// Therefore we'll avoid it. See also:
|
|
|
|
// http://cgit.freedesktop.org/cairo/commit/?id=9c0d761bfcdd28d52c83d74f46dd3c709ae0fa69
|
2012-01-09 10:54:44 -08:00
|
|
|
|
|
|
|
for (unsigned int i = 0; i < aBuffer.mNumGlyphs; i++) {
|
|
|
|
glyphs[i] = aBuffer.mGlyphs[i].mIndex;
|
|
|
|
// XXX: CGPointMake might not be inlined
|
|
|
|
positions[i] = CGPointMake(aBuffer.mGlyphs[i].mPosition.x,
|
|
|
|
-aBuffer.mGlyphs[i].mPosition.y);
|
|
|
|
}
|
|
|
|
|
|
|
|
//XXX: CGContextShowGlyphsAtPositions is 10.5+ for older versions use CGContextShowGlyphsWithAdvances
|
|
|
|
if (isGradient(aPattern)) {
|
|
|
|
CGContextSetTextDrawingMode(cg, kCGTextClip);
|
2013-07-18 17:08:51 -07:00
|
|
|
CGRect extents;
|
2012-10-23 02:07:14 -07:00
|
|
|
if (ScaledFontMac::CTFontDrawGlyphsPtr != nullptr) {
|
2013-07-18 17:08:51 -07:00
|
|
|
CGRect *bboxes = new CGRect[aBuffer.mNumGlyphs];
|
|
|
|
CTFontGetBoundingRectsForGlyphs(macFont->mCTFont, kCTFontDefaultOrientation,
|
|
|
|
&glyphs.front(), bboxes, aBuffer.mNumGlyphs);
|
|
|
|
extents = ComputeGlyphsExtents(bboxes, &positions.front(), aBuffer.mNumGlyphs, 1.0f);
|
2012-10-23 02:07:14 -07:00
|
|
|
ScaledFontMac::CTFontDrawGlyphsPtr(macFont->mCTFont, &glyphs.front(),
|
2013-07-18 17:08:51 -07:00
|
|
|
&positions.front(), aBuffer.mNumGlyphs, cg);
|
|
|
|
delete bboxes;
|
2012-10-23 02:07:14 -07:00
|
|
|
} else {
|
2013-07-18 17:08:51 -07:00
|
|
|
CGRect *bboxes = new CGRect[aBuffer.mNumGlyphs];
|
|
|
|
CGFontGetGlyphBBoxes(macFont->mFont, &glyphs.front(), aBuffer.mNumGlyphs, bboxes);
|
|
|
|
extents = ComputeGlyphsExtents(bboxes, &positions.front(), aBuffer.mNumGlyphs, macFont->mSize);
|
|
|
|
|
2012-10-23 02:07:14 -07:00
|
|
|
CGContextSetFont(cg, macFont->mFont);
|
|
|
|
CGContextSetFontSize(cg, macFont->mSize);
|
|
|
|
CGContextShowGlyphsAtPositions(cg, &glyphs.front(), &positions.front(),
|
|
|
|
aBuffer.mNumGlyphs);
|
2013-07-18 17:08:51 -07:00
|
|
|
delete bboxes;
|
2012-10-23 02:07:14 -07:00
|
|
|
}
|
2013-07-18 17:08:51 -07:00
|
|
|
DrawGradient(cg, aPattern, extents);
|
2012-01-09 10:54:44 -08:00
|
|
|
} else {
|
|
|
|
//XXX: with CoreGraphics we can stroke text directly instead of going
|
|
|
|
// through GetPath. It would be nice to add support for using that
|
|
|
|
CGContextSetTextDrawingMode(cg, kCGTextFill);
|
|
|
|
SetFillFromPattern(cg, mColorSpace, aPattern);
|
2012-10-23 02:07:14 -07:00
|
|
|
if (ScaledFontMac::CTFontDrawGlyphsPtr != nullptr) {
|
|
|
|
ScaledFontMac::CTFontDrawGlyphsPtr(macFont->mCTFont, &glyphs.front(),
|
|
|
|
&positions.front(),
|
|
|
|
aBuffer.mNumGlyphs, cg);
|
|
|
|
} else {
|
|
|
|
CGContextSetFont(cg, macFont->mFont);
|
|
|
|
CGContextSetFontSize(cg, macFont->mSize);
|
|
|
|
CGContextShowGlyphsAtPositions(cg, &glyphs.front(), &positions.front(),
|
|
|
|
aBuffer.mNumGlyphs);
|
|
|
|
}
|
2012-01-09 10:54:44 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
fixer.Fix(mCg);
|
|
|
|
CGContextRestoreGState(cg);
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
void
|
|
|
|
CGContextResetClip(CGContextRef);
|
|
|
|
};
|
|
|
|
|
|
|
|
void
|
|
|
|
DrawTargetCG::CopySurface(SourceSurface *aSurface,
|
|
|
|
const IntRect& aSourceRect,
|
|
|
|
const IntPoint &aDestination)
|
|
|
|
{
|
2012-01-30 22:46:54 -08:00
|
|
|
MarkChanged();
|
|
|
|
|
2012-01-09 10:54:44 -08:00
|
|
|
CGImageRef image;
|
2012-08-14 11:06:12 -07:00
|
|
|
CGImageRef subimage = nullptr;
|
2012-01-09 10:54:44 -08:00
|
|
|
if (aSurface->GetType() == SURFACE_COREGRAPHICS_IMAGE) {
|
2012-01-30 22:46:54 -08:00
|
|
|
image = GetImageFromSourceSurface(aSurface);
|
2012-01-09 10:54:44 -08:00
|
|
|
/* we have two options here:
|
|
|
|
* - create a subimage -- this is slower
|
|
|
|
* - fancy things with clip and different dest rects */
|
|
|
|
{
|
|
|
|
subimage = CGImageCreateWithImageInRect(image, IntRectToCGRect(aSourceRect));
|
|
|
|
image = subimage;
|
|
|
|
}
|
|
|
|
// XXX: it might be more efficient for us to do the copy directly if we have access to the bits
|
|
|
|
|
|
|
|
CGContextSaveGState(mCg);
|
|
|
|
|
|
|
|
// CopySurface ignores the clip, so we need to use private API to temporarily reset it
|
|
|
|
CGContextResetClip(mCg);
|
|
|
|
CGContextSetBlendMode(mCg, kCGBlendModeCopy);
|
|
|
|
|
|
|
|
CGContextScaleCTM(mCg, 1, -1);
|
|
|
|
|
|
|
|
CGRect flippedRect = CGRectMake(aDestination.x, -(aDestination.y + aSourceRect.height),
|
|
|
|
aSourceRect.width, aSourceRect.height);
|
|
|
|
|
|
|
|
CGContextDrawImage(mCg, flippedRect, image);
|
|
|
|
|
|
|
|
CGContextRestoreGState(mCg);
|
|
|
|
|
|
|
|
CGImageRelease(subimage);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DrawTargetCG::DrawSurfaceWithShadow(SourceSurface *aSurface, const Point &aDest, const Color &aColor, const Point &aOffset, Float aSigma, CompositionOp aOperator)
|
|
|
|
{
|
2012-01-30 22:46:54 -08:00
|
|
|
MarkChanged();
|
|
|
|
|
2012-01-09 10:54:44 -08:00
|
|
|
CGImageRef image;
|
2012-01-30 22:46:54 -08:00
|
|
|
image = GetImageFromSourceSurface(aSurface);
|
2012-01-09 10:54:44 -08:00
|
|
|
|
2012-01-30 22:46:54 -08:00
|
|
|
IntSize size = aSurface->GetSize();
|
|
|
|
CGContextSaveGState(mCg);
|
|
|
|
//XXX do we need to do the fixup here?
|
|
|
|
CGContextSetBlendMode(mCg, ToBlendMode(aOperator));
|
2012-01-09 10:54:44 -08:00
|
|
|
|
2012-01-30 22:46:54 -08:00
|
|
|
CGContextScaleCTM(mCg, 1, -1);
|
2012-01-09 10:54:44 -08:00
|
|
|
|
2012-01-30 22:46:54 -08:00
|
|
|
CGRect flippedRect = CGRectMake(aDest.x, -(aDest.y + size.height),
|
|
|
|
size.width, size.height);
|
2012-01-09 10:54:44 -08:00
|
|
|
|
2012-01-30 22:46:54 -08:00
|
|
|
CGColorRef color = ColorToCGColor(mColorSpace, aColor);
|
|
|
|
CGSize offset = {aOffset.x, -aOffset.y};
|
|
|
|
// CoreGraphics needs twice sigma as it's amount of blur
|
|
|
|
CGContextSetShadowWithColor(mCg, offset, 2*aSigma, color);
|
|
|
|
CGColorRelease(color);
|
2012-01-09 10:54:44 -08:00
|
|
|
|
2012-01-30 22:46:54 -08:00
|
|
|
CGContextDrawImage(mCg, flippedRect, image);
|
2012-01-09 10:54:44 -08:00
|
|
|
|
2012-01-30 22:46:54 -08:00
|
|
|
CGContextRestoreGState(mCg);
|
2012-01-09 10:54:44 -08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2012-07-31 08:17:43 -07:00
|
|
|
DrawTargetCG::Init(BackendType aType,
|
|
|
|
unsigned char* aData,
|
2012-07-26 11:06:23 -07:00
|
|
|
const IntSize &aSize,
|
|
|
|
int32_t aStride,
|
|
|
|
SurfaceFormat aFormat)
|
2012-01-09 10:54:44 -08:00
|
|
|
{
|
|
|
|
// XXX: we should come up with some consistent semantics for dealing
|
|
|
|
// with zero area drawtargets
|
2012-07-26 11:06:23 -07:00
|
|
|
if (aSize.width <= 0 || aSize.height <= 0 ||
|
|
|
|
// 32767 is the maximum size supported by cairo
|
|
|
|
// we clamp to that to make it easier to interoperate
|
|
|
|
aSize.width > 32767 || aSize.height > 32767) {
|
2012-08-14 11:06:12 -07:00
|
|
|
mColorSpace = nullptr;
|
|
|
|
mCg = nullptr;
|
|
|
|
mData = nullptr;
|
2012-01-09 10:54:44 -08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
//XXX: handle SurfaceFormat
|
|
|
|
|
|
|
|
//XXX: we'd be better off reusing the Colorspace across draw targets
|
|
|
|
mColorSpace = CGColorSpaceCreateDeviceRGB();
|
|
|
|
|
2012-08-14 11:06:12 -07:00
|
|
|
if (aData == nullptr && aType != BACKEND_COREGRAPHICS_ACCELERATED) {
|
2012-07-26 11:06:23 -07:00
|
|
|
// XXX: Currently, Init implicitly clears, that can often be a waste of time
|
|
|
|
mData = calloc(aSize.height * aStride, 1);
|
|
|
|
aData = static_cast<unsigned char*>(mData);
|
|
|
|
} else {
|
2012-08-14 11:06:12 -07:00
|
|
|
// mData == nullptr means DrawTargetCG doesn't own the image data and will not
|
2012-07-26 11:06:23 -07:00
|
|
|
// delete it in the destructor
|
2012-08-14 11:06:12 -07:00
|
|
|
mData = nullptr;
|
2012-07-26 11:06:23 -07:00
|
|
|
}
|
|
|
|
|
2012-01-09 10:54:44 -08:00
|
|
|
mSize = aSize;
|
|
|
|
|
2012-07-31 08:17:43 -07:00
|
|
|
if (aType == BACKEND_COREGRAPHICS_ACCELERATED) {
|
|
|
|
RefPtr<MacIOSurface> ioSurface = MacIOSurface::CreateIOSurface(aSize.width, aSize.height);
|
|
|
|
mCg = ioSurface->CreateIOSurfaceContext();
|
|
|
|
// If we don't have the symbol for 'CreateIOSurfaceContext' mCg will be null
|
|
|
|
// and we will fallback to software below
|
2012-08-14 11:06:12 -07:00
|
|
|
mData = nullptr;
|
2012-07-31 08:17:43 -07:00
|
|
|
}
|
2012-07-26 11:06:23 -07:00
|
|
|
|
2013-07-29 12:01:29 -07:00
|
|
|
mFormat = FORMAT_B8G8R8A8;
|
|
|
|
|
2012-07-31 08:17:43 -07:00
|
|
|
if (!mCg || aType == BACKEND_COREGRAPHICS) {
|
|
|
|
int bitsPerComponent = 8;
|
2012-07-26 11:06:23 -07:00
|
|
|
|
2012-07-31 08:17:43 -07:00
|
|
|
CGBitmapInfo bitinfo;
|
2013-07-29 12:01:29 -07:00
|
|
|
if (aFormat == FORMAT_A8) {
|
|
|
|
if (mColorSpace)
|
|
|
|
CGColorSpaceRelease(mColorSpace);
|
|
|
|
mColorSpace = nullptr;
|
|
|
|
bitinfo = kCGImageAlphaOnly;
|
|
|
|
mFormat = FORMAT_A8;
|
|
|
|
} else {
|
|
|
|
bitinfo = kCGBitmapByteOrder32Host;
|
|
|
|
if (aFormat == FORMAT_B8G8R8X8) {
|
|
|
|
bitinfo |= kCGImageAlphaNoneSkipFirst;
|
|
|
|
mFormat = aFormat;
|
|
|
|
} else {
|
|
|
|
bitinfo |= kCGImageAlphaPremultipliedFirst;
|
|
|
|
}
|
|
|
|
}
|
2012-07-31 08:17:43 -07:00
|
|
|
// XXX: what should we do if this fails?
|
|
|
|
mCg = CGBitmapContextCreate (aData,
|
|
|
|
mSize.width,
|
|
|
|
mSize.height,
|
|
|
|
bitsPerComponent,
|
|
|
|
aStride,
|
|
|
|
mColorSpace,
|
|
|
|
bitinfo);
|
|
|
|
}
|
2012-01-09 10:54:44 -08:00
|
|
|
|
|
|
|
assert(mCg);
|
|
|
|
// CGContext's default to have the origin at the bottom left
|
|
|
|
// so flip it to the top left
|
|
|
|
CGContextTranslateCTM(mCg, 0, mSize.height);
|
|
|
|
CGContextScaleCTM(mCg, 1, -1);
|
2012-07-26 11:06:23 -07:00
|
|
|
// See Bug 722164 for performance details
|
|
|
|
// Medium or higher quality lead to expensive interpolation
|
|
|
|
// for canvas we want to use low quality interpolation
|
|
|
|
// to have competitive performance with other canvas
|
|
|
|
// implementation.
|
|
|
|
// XXX: Create input parameter to control interpolation and
|
|
|
|
// use the default for content.
|
|
|
|
CGContextSetInterpolationQuality(mCg, kCGInterpolationLow);
|
2012-01-09 10:54:44 -08:00
|
|
|
|
|
|
|
|
2012-07-31 08:17:43 -07:00
|
|
|
if (aType == BACKEND_COREGRAPHICS_ACCELERATED) {
|
|
|
|
// The bitmap backend uses callac to clear, we can't do that without
|
|
|
|
// reading back the surface. This should trigger something equivilent
|
|
|
|
// to glClear.
|
|
|
|
ClearRect(Rect(0, 0, mSize.width, mSize.height));
|
|
|
|
}
|
|
|
|
|
2012-01-09 10:54:44 -08:00
|
|
|
return true;
|
|
|
|
}
|
2011-06-24 10:41:16 -07:00
|
|
|
|
2012-07-31 08:17:43 -07:00
|
|
|
void
|
|
|
|
DrawTargetCG::Flush()
|
|
|
|
{
|
|
|
|
CGContextFlush(mCg);
|
|
|
|
}
|
|
|
|
|
2011-06-24 10:41:16 -07:00
|
|
|
bool
|
2012-07-26 11:06:23 -07:00
|
|
|
DrawTargetCG::Init(CGContextRef cgContext, const IntSize &aSize)
|
2011-06-24 10:41:16 -07:00
|
|
|
{
|
2012-01-09 10:54:44 -08:00
|
|
|
// XXX: we should come up with some consistent semantics for dealing
|
|
|
|
// with zero area drawtargets
|
2012-07-26 11:06:23 -07:00
|
|
|
if (aSize.width == 0 || aSize.height == 0) {
|
2012-08-14 11:06:12 -07:00
|
|
|
mColorSpace = nullptr;
|
|
|
|
mCg = nullptr;
|
|
|
|
mData = nullptr;
|
2012-01-09 10:54:44 -08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
//XXX: handle SurfaceFormat
|
|
|
|
|
|
|
|
//XXX: we'd be better off reusing the Colorspace across draw targets
|
|
|
|
mColorSpace = CGColorSpaceCreateDeviceRGB();
|
2011-06-24 10:41:16 -07:00
|
|
|
|
|
|
|
mSize = aSize;
|
|
|
|
|
2012-07-26 11:06:23 -07:00
|
|
|
mCg = cgContext;
|
2013-07-12 14:19:27 -07:00
|
|
|
CGContextRetain(mCg);
|
2011-06-24 10:41:16 -07:00
|
|
|
|
2012-08-14 11:06:12 -07:00
|
|
|
mData = nullptr;
|
2012-01-09 10:54:44 -08:00
|
|
|
|
|
|
|
assert(mCg);
|
|
|
|
|
2013-07-09 18:02:41 -07:00
|
|
|
// CGContext's default to have the origin at the bottom left.
|
|
|
|
// However, currently the only use of this function is to construct a
|
|
|
|
// DrawTargetCG around a CGContextRef from a cairo quartz surface which
|
|
|
|
// already has it's origin adjusted.
|
|
|
|
//
|
|
|
|
// CGContextTranslateCTM(mCg, 0, mSize.height);
|
|
|
|
// CGContextScaleCTM(mCg, 1, -1);
|
|
|
|
|
2012-01-09 10:54:44 -08:00
|
|
|
mFormat = FORMAT_B8G8R8A8;
|
2013-07-09 18:02:41 -07:00
|
|
|
if (GetContextType(mCg) == CG_CONTEXT_TYPE_BITMAP) {
|
|
|
|
CGColorSpaceRef colorspace;
|
2013-07-29 12:01:29 -07:00
|
|
|
CGBitmapInfo bitinfo = CGBitmapContextGetBitmapInfo(mCg);
|
2013-07-09 18:02:41 -07:00
|
|
|
colorspace = CGBitmapContextGetColorSpace (mCg);
|
|
|
|
if (CGColorSpaceGetNumberOfComponents(colorspace) == 1) {
|
2013-07-29 12:01:29 -07:00
|
|
|
mFormat = FORMAT_A8;
|
|
|
|
} else if ((bitinfo & kCGBitmapAlphaInfoMask) == kCGImageAlphaNoneSkipFirst) {
|
|
|
|
mFormat = FORMAT_B8G8R8X8;
|
2013-07-09 18:02:41 -07:00
|
|
|
}
|
|
|
|
}
|
2011-06-24 10:41:16 -07:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2012-01-09 10:54:44 -08:00
|
|
|
|
2012-07-26 11:06:23 -07:00
|
|
|
bool
|
2012-07-31 08:17:43 -07:00
|
|
|
DrawTargetCG::Init(BackendType aType, const IntSize &aSize, SurfaceFormat &aFormat)
|
2012-07-26 11:06:23 -07:00
|
|
|
{
|
|
|
|
int stride = aSize.width*4;
|
|
|
|
|
2012-08-14 11:06:12 -07:00
|
|
|
// Calling Init with aData == nullptr will allocate.
|
|
|
|
return Init(aType, nullptr, aSize, stride, aFormat);
|
2012-07-26 11:06:23 -07:00
|
|
|
}
|
|
|
|
|
2012-01-09 10:54:44 -08:00
|
|
|
TemporaryRef<PathBuilder>
|
|
|
|
DrawTargetCG::CreatePathBuilder(FillRule aFillRule) const
|
|
|
|
{
|
|
|
|
RefPtr<PathBuilderCG> pb = new PathBuilderCG(aFillRule);
|
|
|
|
return pb;
|
|
|
|
}
|
|
|
|
|
|
|
|
void*
|
|
|
|
DrawTargetCG::GetNativeSurface(NativeSurfaceType aType)
|
|
|
|
{
|
2013-02-12 20:00:54 -08:00
|
|
|
if ((aType == NATIVE_SURFACE_CGCONTEXT && GetContextType(mCg) == CG_CONTEXT_TYPE_BITMAP) ||
|
|
|
|
(aType == NATIVE_SURFACE_CGCONTEXT_ACCELERATED && GetContextType(mCg) == CG_CONTEXT_TYPE_IOSURFACE)) {
|
2012-01-09 10:54:44 -08:00
|
|
|
return mCg;
|
|
|
|
} else {
|
2012-08-14 11:06:12 -07:00
|
|
|
return nullptr;
|
2012-01-09 10:54:44 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DrawTargetCG::Mask(const Pattern &aSource,
|
|
|
|
const Pattern &aMask,
|
|
|
|
const DrawOptions &aDrawOptions)
|
|
|
|
{
|
2012-01-30 22:46:54 -08:00
|
|
|
MarkChanged();
|
2012-01-09 10:54:44 -08:00
|
|
|
|
|
|
|
CGContextSaveGState(mCg);
|
|
|
|
|
|
|
|
if (isGradient(aMask)) {
|
|
|
|
assert(0);
|
|
|
|
} else {
|
|
|
|
if (aMask.GetType() == PATTERN_COLOR) {
|
|
|
|
DrawOptions drawOptions(aDrawOptions);
|
|
|
|
const Color& color = static_cast<const ColorPattern&>(aMask).mColor;
|
|
|
|
drawOptions.mAlpha *= color.a;
|
|
|
|
assert(0);
|
|
|
|
// XXX: we need to get a rect that when transformed covers the entire surface
|
|
|
|
//Rect
|
|
|
|
//FillRect(rect, aSource, drawOptions);
|
|
|
|
} else if (aMask.GetType() == PATTERN_SURFACE) {
|
|
|
|
const SurfacePattern& pat = static_cast<const SurfacePattern&>(aMask);
|
2012-01-30 22:46:54 -08:00
|
|
|
CGImageRef mask = GetImageFromSourceSurface(pat.mSurface.get());
|
2012-01-09 10:54:44 -08:00
|
|
|
Rect rect(0,0, CGImageGetWidth(mask), CGImageGetHeight(mask));
|
|
|
|
// XXX: probably we need to do some flipping of the image or something
|
|
|
|
CGContextClipToMask(mCg, RectToCGRect(rect), mask);
|
|
|
|
FillRect(rect, aSource, aDrawOptions);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CGContextRestoreGState(mCg);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DrawTargetCG::PushClipRect(const Rect &aRect)
|
|
|
|
{
|
|
|
|
CGContextSaveGState(mCg);
|
|
|
|
|
2012-01-24 12:14:50 -08:00
|
|
|
/* We go through a bit of trouble to temporarilly set the transform
|
|
|
|
* while we add the path */
|
|
|
|
CGAffineTransform previousTransform = CGContextGetCTM(mCg);
|
|
|
|
CGContextConcatCTM(mCg, GfxMatrixToCGAffineTransform(mTransform));
|
2012-01-09 10:54:44 -08:00
|
|
|
CGContextClipToRect(mCg, RectToCGRect(aRect));
|
2012-01-24 12:14:50 -08:00
|
|
|
CGContextSetCTM(mCg, previousTransform);
|
2012-01-09 10:54:44 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
DrawTargetCG::PushClip(const Path *aPath)
|
|
|
|
{
|
|
|
|
CGContextSaveGState(mCg);
|
|
|
|
|
|
|
|
CGContextBeginPath(mCg);
|
|
|
|
assert(aPath->GetBackendType() == BACKEND_COREGRAPHICS);
|
|
|
|
|
|
|
|
const PathCG *cgPath = static_cast<const PathCG*>(aPath);
|
|
|
|
|
|
|
|
// Weirdly, CoreGraphics clips empty paths as all shown
|
|
|
|
// but emtpy rects as all clipped. We detect this situation and
|
|
|
|
// workaround it appropriately
|
|
|
|
if (CGPathIsEmpty(cgPath->GetPath())) {
|
|
|
|
// XXX: should we return here?
|
|
|
|
CGContextClipToRect(mCg, CGRectZero);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-01-24 12:14:50 -08:00
|
|
|
/* We go through a bit of trouble to temporarilly set the transform
|
|
|
|
* while we add the path. XXX: this could be improved if we keep
|
|
|
|
* the CTM as resident state on the DrawTarget. */
|
|
|
|
CGContextSaveGState(mCg);
|
|
|
|
CGContextConcatCTM(mCg, GfxMatrixToCGAffineTransform(mTransform));
|
2012-01-09 10:54:44 -08:00
|
|
|
CGContextAddPath(mCg, cgPath->GetPath());
|
2012-01-24 12:14:50 -08:00
|
|
|
CGContextRestoreGState(mCg);
|
|
|
|
|
2012-01-09 10:54:44 -08:00
|
|
|
if (cgPath->GetFillRule() == FILL_EVEN_ODD)
|
|
|
|
CGContextEOClip(mCg);
|
|
|
|
else
|
|
|
|
CGContextClip(mCg);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DrawTargetCG::PopClip()
|
|
|
|
{
|
|
|
|
CGContextRestoreGState(mCg);
|
|
|
|
}
|
|
|
|
|
2012-01-30 22:46:54 -08:00
|
|
|
void
|
|
|
|
DrawTargetCG::MarkChanged()
|
|
|
|
{
|
|
|
|
if (mSnapshot) {
|
|
|
|
if (mSnapshot->refCount() > 1) {
|
|
|
|
// We only need to worry about snapshots that someone else knows about
|
|
|
|
mSnapshot->DrawTargetWillChange();
|
|
|
|
}
|
2012-08-14 11:06:12 -07:00
|
|
|
mSnapshot = nullptr;
|
2012-01-30 22:46:54 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-09 15:55:02 -07:00
|
|
|
CGContextRef
|
|
|
|
BorrowedCGContext::BorrowCGContextFromDrawTarget(DrawTarget *aDT)
|
|
|
|
{
|
|
|
|
if (aDT->GetType() == BACKEND_COREGRAPHICS || aDT->GetType() == BACKEND_COREGRAPHICS_ACCELERATED) {
|
|
|
|
DrawTargetCG* cgDT = static_cast<DrawTargetCG*>(aDT);
|
|
|
|
cgDT->MarkChanged();
|
|
|
|
|
|
|
|
// swap out the context
|
|
|
|
CGContextRef cg = cgDT->mCg;
|
|
|
|
cgDT->mCg = nullptr;
|
|
|
|
|
|
|
|
// save the state to make it easier for callers to avoid mucking with things
|
|
|
|
CGContextSaveGState(cg);
|
|
|
|
|
2013-07-12 14:19:29 -07:00
|
|
|
CGContextConcatCTM(cg, GfxMatrixToCGAffineTransform(cgDT->mTransform));
|
|
|
|
|
2013-07-09 15:55:02 -07:00
|
|
|
return cg;
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
BorrowedCGContext::ReturnCGContextToDrawTarget(DrawTarget *aDT, CGContextRef cg)
|
|
|
|
{
|
|
|
|
DrawTargetCG* cgDT = static_cast<DrawTargetCG*>(aDT);
|
|
|
|
|
|
|
|
CGContextRestoreGState(cg);
|
|
|
|
cgDT->mCg = cg;
|
|
|
|
}
|
2012-01-09 10:54:44 -08:00
|
|
|
|
|
|
|
|
2011-06-24 10:41:16 -07:00
|
|
|
}
|
|
|
|
}
|