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/. */
|
2013-09-22 20:28:16 -07:00
|
|
|
#include "BorrowedContext.h"
|
2014-06-13 14:03:52 -07:00
|
|
|
#include "DataSurfaceHelpers.h"
|
2011-06-24 10:41:16 -07:00
|
|
|
#include "DrawTargetCG.h"
|
2014-06-13 14:03:52 -07:00
|
|
|
#include "Logging.h"
|
2011-06-24 10:41:16 -07:00
|
|
|
#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>
|
2013-11-07 19:53:54 -08:00
|
|
|
#include "MacIOSurface.h"
|
2013-11-27 03:22:56 -08:00
|
|
|
#include "FilterNodeSoftware.h"
|
2014-02-13 03:11:16 -08:00
|
|
|
#include "mozilla/Assertions.h"
|
2014-06-13 14:03:52 -07:00
|
|
|
#include "mozilla/Types.h" // for decltype
|
2014-02-18 14:18:29 -08:00
|
|
|
#include "mozilla/FloatingPoint.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 {
|
|
|
|
|
2014-09-11 22:18:22 -07:00
|
|
|
template <typename T>
|
|
|
|
static CGRect RectToCGRect(const T& r)
|
2011-06-24 10:41:16 -07:00
|
|
|
{
|
|
|
|
return CGRectMake(r.x, r.y, r.width, r.height);
|
|
|
|
}
|
|
|
|
|
|
|
|
CGBlendMode ToBlendMode(CompositionOp op)
|
|
|
|
{
|
|
|
|
CGBlendMode mode;
|
|
|
|
switch (op) {
|
2014-01-10 11:06:17 -08:00
|
|
|
case CompositionOp::OP_OVER:
|
2011-06-24 10:41:16 -07:00
|
|
|
mode = kCGBlendModeNormal;
|
|
|
|
break;
|
2014-01-10 11:06:17 -08:00
|
|
|
case CompositionOp::OP_ADD:
|
2012-01-17 10:08:38 -08:00
|
|
|
mode = kCGBlendModePlusLighter;
|
2012-01-09 10:54:44 -08:00
|
|
|
break;
|
2014-01-10 11:06:17 -08:00
|
|
|
case CompositionOp::OP_ATOP:
|
2012-01-17 10:08:38 -08:00
|
|
|
mode = kCGBlendModeSourceAtop;
|
2012-01-09 10:54:44 -08:00
|
|
|
break;
|
2014-01-10 11:06:17 -08:00
|
|
|
case CompositionOp::OP_OUT:
|
2012-01-09 10:54:44 -08:00
|
|
|
mode = kCGBlendModeSourceOut;
|
|
|
|
break;
|
2014-01-10 11:06:17 -08:00
|
|
|
case CompositionOp::OP_IN:
|
2012-01-09 10:54:44 -08:00
|
|
|
mode = kCGBlendModeSourceIn;
|
|
|
|
break;
|
2014-01-10 11:06:17 -08:00
|
|
|
case CompositionOp::OP_SOURCE:
|
2012-01-09 10:54:44 -08:00
|
|
|
mode = kCGBlendModeCopy;
|
|
|
|
break;
|
2014-01-10 11:06:17 -08:00
|
|
|
case CompositionOp::OP_DEST_IN:
|
2012-01-09 10:54:44 -08:00
|
|
|
mode = kCGBlendModeDestinationIn;
|
|
|
|
break;
|
2014-01-10 11:06:17 -08:00
|
|
|
case CompositionOp::OP_DEST_OUT:
|
2012-01-09 10:54:44 -08:00
|
|
|
mode = kCGBlendModeDestinationOut;
|
|
|
|
break;
|
2014-01-10 11:06:17 -08:00
|
|
|
case CompositionOp::OP_DEST_OVER:
|
2012-01-09 10:54:44 -08:00
|
|
|
mode = kCGBlendModeDestinationOver;
|
|
|
|
break;
|
2014-01-10 11:06:17 -08:00
|
|
|
case CompositionOp::OP_DEST_ATOP:
|
2012-01-09 10:54:44 -08:00
|
|
|
mode = kCGBlendModeDestinationAtop;
|
|
|
|
break;
|
2014-01-10 11:06:17 -08:00
|
|
|
case CompositionOp::OP_XOR:
|
2012-01-09 10:54:44 -08:00
|
|
|
mode = kCGBlendModeXOR;
|
|
|
|
break;
|
2014-01-10 11:06:17 -08:00
|
|
|
case CompositionOp::OP_MULTIPLY:
|
2012-12-12 12:28:17 -08:00
|
|
|
mode = kCGBlendModeMultiply;
|
|
|
|
break;
|
2014-01-10 11:06:17 -08:00
|
|
|
case CompositionOp::OP_SCREEN:
|
2012-12-12 12:28:17 -08:00
|
|
|
mode = kCGBlendModeScreen;
|
|
|
|
break;
|
2014-01-10 11:06:17 -08:00
|
|
|
case CompositionOp::OP_OVERLAY:
|
2012-12-12 12:28:17 -08:00
|
|
|
mode = kCGBlendModeOverlay;
|
|
|
|
break;
|
2014-01-10 11:06:17 -08:00
|
|
|
case CompositionOp::OP_DARKEN:
|
2012-12-12 12:28:17 -08:00
|
|
|
mode = kCGBlendModeDarken;
|
|
|
|
break;
|
2014-01-10 11:06:17 -08:00
|
|
|
case CompositionOp::OP_LIGHTEN:
|
2012-12-12 12:28:17 -08:00
|
|
|
mode = kCGBlendModeLighten;
|
|
|
|
break;
|
2014-01-10 11:06:17 -08:00
|
|
|
case CompositionOp::OP_COLOR_DODGE:
|
2012-12-12 12:28:17 -08:00
|
|
|
mode = kCGBlendModeColorDodge;
|
|
|
|
break;
|
2014-01-10 11:06:17 -08:00
|
|
|
case CompositionOp::OP_COLOR_BURN:
|
2012-12-12 12:28:17 -08:00
|
|
|
mode = kCGBlendModeColorBurn;
|
|
|
|
break;
|
2014-01-10 11:06:17 -08:00
|
|
|
case CompositionOp::OP_HARD_LIGHT:
|
2012-12-12 12:28:17 -08:00
|
|
|
mode = kCGBlendModeHardLight;
|
|
|
|
break;
|
2014-01-10 11:06:17 -08:00
|
|
|
case CompositionOp::OP_SOFT_LIGHT:
|
2012-12-12 12:28:17 -08:00
|
|
|
mode = kCGBlendModeSoftLight;
|
|
|
|
break;
|
2014-01-10 11:06:17 -08:00
|
|
|
case CompositionOp::OP_DIFFERENCE:
|
2012-12-12 12:28:17 -08:00
|
|
|
mode = kCGBlendModeDifference;
|
|
|
|
break;
|
2014-01-10 11:06:17 -08:00
|
|
|
case CompositionOp::OP_EXCLUSION:
|
2012-12-12 12:28:17 -08:00
|
|
|
mode = kCGBlendModeExclusion;
|
|
|
|
break;
|
2014-01-10 11:06:17 -08:00
|
|
|
case CompositionOp::OP_HUE:
|
2012-12-12 12:28:17 -08:00
|
|
|
mode = kCGBlendModeHue;
|
|
|
|
break;
|
2014-01-10 11:06:17 -08:00
|
|
|
case CompositionOp::OP_SATURATION:
|
2012-12-12 12:28:17 -08:00
|
|
|
mode = kCGBlendModeSaturation;
|
|
|
|
break;
|
2014-01-10 11:06:17 -08:00
|
|
|
case CompositionOp::OP_COLOR:
|
2012-12-12 12:28:17 -08:00
|
|
|
mode = kCGBlendModeColor;
|
|
|
|
break;
|
2014-01-10 11:06:17 -08:00
|
|
|
case CompositionOp::OP_LUMINOSITY:
|
2012-12-12 12:28:17 -08:00
|
|
|
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:
|
2014-01-10 11:06:17 -08:00
|
|
|
case Filter::LINEAR:
|
2013-07-31 11:46:25 -07:00
|
|
|
return kCGInterpolationLow;
|
2014-01-10 11:06:17 -08:00
|
|
|
case Filter::POINT:
|
2013-07-31 11:46:25 -07:00
|
|
|
return kCGInterpolationNone;
|
2014-01-10 11:06:17 -08:00
|
|
|
case Filter::GOOD:
|
2014-09-26 12:05:14 -07:00
|
|
|
return kCGInterpolationLow;
|
2013-07-31 11:46:25 -07:00
|
|
|
}
|
|
|
|
}
|
2011-06-24 10:41:16 -07:00
|
|
|
|
|
|
|
|
2014-07-23 09:12:59 -07:00
|
|
|
DrawTargetCG::DrawTargetCG()
|
|
|
|
: mColorSpace(nullptr)
|
|
|
|
, mCg(nullptr)
|
2014-10-24 09:32:23 -07:00
|
|
|
, mMayContainInvalidPremultipliedData(false)
|
2011-06-24 10:41:16 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
DrawTargetCG::~DrawTargetCG()
|
|
|
|
{
|
2014-10-24 09:32:22 -07:00
|
|
|
if (mSnapshot) {
|
|
|
|
if (mSnapshot->refCount() > 1) {
|
|
|
|
// We only need to worry about snapshots that someone else knows about
|
|
|
|
mSnapshot->DrawTargetWillGoAway();
|
|
|
|
}
|
|
|
|
mSnapshot = nullptr;
|
|
|
|
}
|
2012-01-30 22:46:54 -08:00
|
|
|
|
2014-07-23 09:12:59 -07:00
|
|
|
// Both of these are OK with nullptr arguments, so we do not
|
|
|
|
// need to check (these could be nullptr if Init fails)
|
|
|
|
CGColorSpaceRelease(mColorSpace);
|
|
|
|
CGContextRelease(mCg);
|
2011-06-24 10:41:16 -07:00
|
|
|
}
|
|
|
|
|
2014-06-27 02:17:49 -07:00
|
|
|
DrawTargetType
|
|
|
|
DrawTargetCG::GetType() const
|
|
|
|
{
|
|
|
|
return GetBackendType() == BackendType::COREGRAPHICS_ACCELERATED ?
|
|
|
|
DrawTargetType::HARDWARE_RASTER : DrawTargetType::SOFTWARE_RASTER;
|
|
|
|
}
|
|
|
|
|
2012-07-31 08:17:43 -07:00
|
|
|
BackendType
|
2014-06-19 13:35:33 -07:00
|
|
|
DrawTargetCG::GetBackendType() const
|
2012-07-31 08:17:43 -07:00
|
|
|
{
|
|
|
|
// It may be worth spliting Bitmap and IOSurface DrawTarget
|
|
|
|
// into seperate classes.
|
|
|
|
if (GetContextType(mCg) == CG_CONTEXT_TYPE_IOSURFACE) {
|
2014-01-10 11:06:16 -08:00
|
|
|
return BackendType::COREGRAPHICS_ACCELERATED;
|
2012-07-31 08:17:43 -07:00
|
|
|
} else {
|
2014-01-10 11:06:16 -08:00
|
|
|
return BackendType::COREGRAPHICS;
|
2012-07-31 08:17:43 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
2014-10-24 09:32:23 -07:00
|
|
|
Flush();
|
2014-06-13 09:09:23 -07:00
|
|
|
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();
|
2014-06-19 13:35:33 -07:00
|
|
|
if (newTarget->Init(GetBackendType(), aSize, aFormat)) {
|
2014-06-13 09:09:23 -07:00
|
|
|
return newTarget.forget();
|
2012-01-09 10:54:44 -08:00
|
|
|
}
|
2014-06-13 09:09:23 -07:00
|
|
|
return nullptr;
|
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();
|
|
|
|
|
2014-06-13 09:09:23 -07: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
|
|
|
}
|
|
|
|
|
2014-06-13 09:09:23 -07:00
|
|
|
return newSurf.forget();
|
2011-06-24 10:41:16 -07:00
|
|
|
}
|
|
|
|
|
2014-08-26 15:20:18 -07:00
|
|
|
static void releaseDataSurface(void* info, const void *data, size_t size)
|
|
|
|
{
|
|
|
|
static_cast<DataSourceSurface*>(info)->Release();
|
|
|
|
}
|
|
|
|
|
2013-11-27 03:21:43 -08:00
|
|
|
// This function returns a retained CGImage that needs to be released after
|
|
|
|
// use. The reason for this is that we want to either reuse an existing CGImage
|
|
|
|
// or create a new one.
|
2012-01-30 22:46:54 -08:00
|
|
|
static CGImageRef
|
2013-11-27 03:21:43 -08:00
|
|
|
GetRetainedImageFromSourceSurface(SourceSurface *aSurface)
|
2012-01-30 22:46:54 -08:00
|
|
|
{
|
2014-07-14 10:24:01 -07:00
|
|
|
switch(aSurface->GetType()) {
|
|
|
|
case SurfaceType::COREGRAPHICS_IMAGE:
|
|
|
|
return CGImageRetain(static_cast<SourceSurfaceCG*>(aSurface)->GetImage());
|
2013-11-27 03:21:43 -08:00
|
|
|
|
2014-07-14 10:24:01 -07:00
|
|
|
case SurfaceType::COREGRAPHICS_CGCONTEXT:
|
|
|
|
return CGImageRetain(static_cast<SourceSurfaceCGContext*>(aSurface)->GetImage());
|
2013-11-27 03:21:43 -08:00
|
|
|
|
2014-07-14 10:24:01 -07:00
|
|
|
default:
|
|
|
|
{
|
|
|
|
RefPtr<DataSourceSurface> data = aSurface->GetDataSurface();
|
|
|
|
if (!data) {
|
|
|
|
MOZ_CRASH("unsupported source surface");
|
|
|
|
}
|
2014-08-26 15:20:18 -07:00
|
|
|
data->AddRef();
|
|
|
|
return CreateCGImage(releaseDataSurface, data.get(),
|
|
|
|
data->GetData(), data->GetSize(),
|
2014-07-14 10:24:01 -07:00
|
|
|
data->Stride(), data->GetFormat());
|
|
|
|
}
|
|
|
|
}
|
2012-01-30 22:46:54 -08:00
|
|
|
}
|
|
|
|
|
2011-06-24 10:41:16 -07:00
|
|
|
TemporaryRef<SourceSurface>
|
|
|
|
DrawTargetCG::OptimizeSourceSurface(SourceSurface *aSurface) const
|
|
|
|
{
|
2014-10-02 01:14:46 -07:00
|
|
|
return aSurface;
|
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
|
|
|
|
2014-04-01 01:57:35 -07:00
|
|
|
CGContextRef Check(CGContextRef baseCg, CompositionOp blend, const Rect* maskBounds = nullptr)
|
2012-01-09 10:54:44 -08:00
|
|
|
{
|
|
|
|
if (!IsOperatorBoundByMask(blend)) {
|
|
|
|
mClipBounds = CGContextGetClipBoundingBox(baseCg);
|
2014-04-01 01:57:35 -07:00
|
|
|
// If we're entirely clipped out or if the drawing operation covers the entire clip then
|
|
|
|
// we don't need to create a temporary surface.
|
|
|
|
if (CGRectIsEmpty(mClipBounds) ||
|
|
|
|
(maskBounds && maskBounds->Contains(CGRectToRect(mClipBounds)))) {
|
2014-01-08 01:31:17 -08:00
|
|
|
return baseCg;
|
|
|
|
}
|
|
|
|
|
2012-01-09 10:54:44 -08:00
|
|
|
// 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);
|
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();
|
|
|
|
|
|
|
|
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;
|
2014-04-01 01:57:35 -07:00
|
|
|
CGContextRef cg = fixer.Check(mCg, aDrawOptions.mCompositionOp, &aDest);
|
2012-01-30 22:46:54 -08:00
|
|
|
CGContextSetAlpha(cg, aDrawOptions.mAlpha);
|
2014-01-10 11:06:17 -08:00
|
|
|
CGContextSetShouldAntialias(cg, aDrawOptions.mAntialiasMode != AntialiasMode::NONE);
|
2012-01-09 10:54:44 -08:00
|
|
|
|
2012-01-30 22:46:54 -08:00
|
|
|
CGContextConcatCTM(cg, GfxMatrixToCGAffineTransform(mTransform));
|
2014-03-24 06:10:00 -07:00
|
|
|
|
|
|
|
CGContextSetInterpolationQuality(cg, InterpolationQualityFromFilter(aSurfOptions.mFilter));
|
|
|
|
|
2013-11-27 03:21:43 -08:00
|
|
|
CGImageRef image = GetRetainedImageFromSourceSurface(aSurface);
|
|
|
|
|
2014-03-24 06:10:00 -07:00
|
|
|
if (aSurfOptions.mFilter == Filter::POINT) {
|
|
|
|
CGImageRef subimage = CGImageCreateWithImageInRect(image, RectToCGRect(aSource));
|
|
|
|
CGImageRelease(image);
|
2011-06-24 10:41:16 -07:00
|
|
|
|
2014-03-24 06:10:00 -07:00
|
|
|
CGContextScaleCTM(cg, 1, -1);
|
2012-01-09 10:54:44 -08:00
|
|
|
|
2014-03-24 06:10:00 -07:00
|
|
|
CGRect flippedRect = CGRectMake(aDest.x, -(aDest.y + aDest.height),
|
|
|
|
aDest.width, aDest.height);
|
2012-01-09 10:54:44 -08:00
|
|
|
|
2014-03-24 06:10:00 -07:00
|
|
|
CGContextDrawImage(cg, flippedRect, subimage);
|
|
|
|
CGImageRelease(subimage);
|
|
|
|
} else {
|
|
|
|
CGRect destRect = CGRectMake(aDest.x, aDest.y, aDest.width, aDest.height);
|
|
|
|
CGContextClipToRect(cg, destRect);
|
2012-01-09 10:54:44 -08:00
|
|
|
|
2014-03-24 06:10:00 -07:00
|
|
|
float xScale = aSource.width / aDest.width;
|
|
|
|
float yScale = aSource.height / aDest.height;
|
|
|
|
CGContextTranslateCTM(cg, aDest.x - aSource.x / xScale, aDest.y - aSource.y / yScale);
|
|
|
|
|
|
|
|
CGRect adjustedDestRect = CGRectMake(0, 0, CGImageGetWidth(image) / xScale,
|
|
|
|
CGImageGetHeight(image) / yScale);
|
|
|
|
|
|
|
|
CGContextTranslateCTM(cg, 0, CGRectGetHeight(adjustedDestRect));
|
|
|
|
CGContextScaleCTM(cg, 1, -1);
|
|
|
|
|
|
|
|
CGContextDrawImage(cg, adjustedDestRect, image);
|
|
|
|
CGImageRelease(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
|
|
|
}
|
|
|
|
|
2013-11-27 03:22:56 -08:00
|
|
|
TemporaryRef<FilterNode>
|
|
|
|
DrawTargetCG::CreateFilter(FilterType aType)
|
|
|
|
{
|
|
|
|
return FilterNodeSoftware::Create(aType);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DrawTargetCG::DrawFilter(FilterNode *aNode,
|
|
|
|
const Rect &aSourceRect,
|
|
|
|
const Point &aDestPoint,
|
|
|
|
const DrawOptions &aOptions)
|
|
|
|
{
|
|
|
|
FilterNodeSoftware* filter = static_cast<FilterNodeSoftware*>(aNode);
|
|
|
|
filter->Draw(this, aSourceRect, aDestPoint, aOptions);
|
|
|
|
}
|
|
|
|
|
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:
|
2014-02-24 05:23:37 -08:00
|
|
|
MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(GradientStopsCG)
|
2014-07-23 09:12:59 -07:00
|
|
|
|
|
|
|
GradientStopsCG(CGColorSpaceRef aColorSpace,
|
|
|
|
const std::vector<GradientStop>& aStops,
|
|
|
|
ExtendMode aExtendMode)
|
|
|
|
: mGradient(nullptr)
|
2012-01-09 10:54:44 -08:00
|
|
|
{
|
2014-07-23 09:12:59 -07:00
|
|
|
// This all works fine with empty aStops vector
|
|
|
|
|
2013-07-18 17:08:51 -07:00
|
|
|
mExtend = aExtendMode;
|
2014-01-10 11:06:17 -08:00
|
|
|
if (aExtendMode == ExtendMode::CLAMP) {
|
2014-07-23 09:12:59 -07:00
|
|
|
size_t numStops = aStops.size();
|
|
|
|
|
2013-07-18 17:08:51 -07:00
|
|
|
std::vector<CGFloat> colors;
|
|
|
|
std::vector<CGFloat> offsets;
|
2014-07-23 09:12:59 -07:00
|
|
|
colors.reserve(numStops*4);
|
|
|
|
offsets.reserve(numStops);
|
2013-07-18 17:08:51 -07:00
|
|
|
|
2014-07-23 09:12:59 -07:00
|
|
|
for (size_t i = 0; i < numStops; i++) {
|
2013-07-18 17:08:51 -07:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2014-07-23 09:12:59 -07:00
|
|
|
mGradient = CGGradientCreateWithColorComponents(aColorSpace,
|
2013-07-18 17:08:51 -07:00
|
|
|
&colors.front(),
|
|
|
|
&offsets.front(),
|
2014-07-23 09:12:59 -07:00
|
|
|
offsets.size());
|
2013-07-18 17:08:51 -07:00
|
|
|
} else {
|
2014-07-23 09:12:59 -07:00
|
|
|
mStops = aStops;
|
2012-01-09 10:54:44 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2014-07-23 09:12:59 -07:00
|
|
|
|
2012-01-09 10:54:44 -08:00
|
|
|
virtual ~GradientStopsCG() {
|
2014-07-23 09:12:59 -07:00
|
|
|
// CGGradientRelease is OK with nullptr argument
|
|
|
|
CGGradientRelease(mGradient);
|
2012-01-09 10:54:44 -08:00
|
|
|
}
|
2014-07-23 09:12:59 -07:00
|
|
|
|
2014-01-10 11:06:16 -08:00
|
|
|
// Will always report BackendType::COREGRAPHICS, but it is compatible
|
|
|
|
// with BackendType::COREGRAPHICS_ACCELERATED
|
|
|
|
BackendType GetBackendType() const { return BackendType::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
|
|
|
|
{
|
2014-07-23 09:12:59 -07:00
|
|
|
std::vector<GradientStop> stops(aStops, aStops+aNumStops);
|
|
|
|
return new GradientStopsCG(mColorSpace, stops, aExtendMode);
|
2012-01-09 10:54:44 -08:00
|
|
|
}
|
|
|
|
|
2013-10-11 07:26:40 -07:00
|
|
|
static void
|
|
|
|
UpdateLinearParametersToIncludePoint(double *min_t, double *max_t,
|
|
|
|
CGPoint *start,
|
|
|
|
double dx, double dy,
|
|
|
|
double x, double y)
|
|
|
|
{
|
2014-02-18 14:18:29 -08:00
|
|
|
MOZ_ASSERT(IsFinite(x) && IsFinite(y));
|
|
|
|
|
2013-10-11 07:26:40 -07:00
|
|
|
/**
|
|
|
|
* Compute a parameter t such that a line perpendicular to the (dx,dy)
|
|
|
|
* vector, passing through (start->x + dx*t, start->y + dy*t), also
|
|
|
|
* passes through (x,y).
|
|
|
|
*
|
|
|
|
* Let px = x - start->x, py = y - start->y.
|
|
|
|
* t is given by
|
|
|
|
* (px - dx*t)*dx + (py - dy*t)*dy = 0
|
|
|
|
*
|
|
|
|
* Solving for t we get
|
|
|
|
* numerator = dx*px + dy*py
|
|
|
|
* denominator = dx^2 + dy^2
|
|
|
|
* t = numerator/denominator
|
|
|
|
*
|
|
|
|
* In CalculateRepeatingGradientParams we know the length of (dx,dy)
|
|
|
|
* is not zero. (This is checked in DrawLinearRepeatingGradient.)
|
|
|
|
*/
|
|
|
|
double px = x - start->x;
|
|
|
|
double py = y - start->y;
|
|
|
|
double numerator = dx * px + dy * py;
|
|
|
|
double denominator = dx * dx + dy * dy;
|
|
|
|
double t = numerator / denominator;
|
|
|
|
|
|
|
|
if (*min_t > t) {
|
|
|
|
*min_t = t;
|
|
|
|
}
|
|
|
|
if (*max_t < t) {
|
|
|
|
*max_t = t;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Repeat the gradient line such that lines extended perpendicular to the
|
|
|
|
* gradient line at both start and end would completely enclose the drawing
|
|
|
|
* extents.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
CalculateRepeatingGradientParams(CGPoint *aStart, CGPoint *aEnd,
|
2014-06-10 13:26:32 -07:00
|
|
|
CGRect aExtents, int *aRepeatStartFactor,
|
|
|
|
int *aRepeatEndFactor)
|
2013-10-11 07:26:40 -07:00
|
|
|
{
|
2014-02-13 03:11:16 -08:00
|
|
|
double t_min = INFINITY;
|
|
|
|
double t_max = -INFINITY;
|
2013-10-11 07:26:40 -07:00
|
|
|
double dx = aEnd->x - aStart->x;
|
|
|
|
double dy = aEnd->y - aStart->y;
|
|
|
|
|
|
|
|
double bounds_x1 = aExtents.origin.x;
|
|
|
|
double bounds_y1 = aExtents.origin.y;
|
|
|
|
double bounds_x2 = aExtents.origin.x + aExtents.size.width;
|
|
|
|
double bounds_y2 = aExtents.origin.y + aExtents.size.height;
|
|
|
|
|
|
|
|
UpdateLinearParametersToIncludePoint(&t_min, &t_max, aStart, dx, dy,
|
|
|
|
bounds_x1, bounds_y1);
|
|
|
|
UpdateLinearParametersToIncludePoint(&t_min, &t_max, aStart, dx, dy,
|
|
|
|
bounds_x2, bounds_y1);
|
|
|
|
UpdateLinearParametersToIncludePoint(&t_min, &t_max, aStart, dx, dy,
|
|
|
|
bounds_x2, bounds_y2);
|
|
|
|
UpdateLinearParametersToIncludePoint(&t_min, &t_max, aStart, dx, dy,
|
|
|
|
bounds_x1, bounds_y2);
|
|
|
|
|
2014-02-13 03:11:16 -08:00
|
|
|
MOZ_ASSERT(!isinf(t_min) && !isinf(t_max),
|
|
|
|
"The first call to UpdateLinearParametersToIncludePoint should have made t_min and t_max non-infinite.");
|
|
|
|
|
2013-10-11 07:26:40 -07:00
|
|
|
// Move t_min and t_max to the nearest usable integer to try to avoid
|
|
|
|
// subtle variations due to numerical instability, especially accidentally
|
|
|
|
// cutting off a pixel. Extending the gradient repetitions is always safe.
|
|
|
|
t_min = floor (t_min);
|
|
|
|
t_max = ceil (t_max);
|
|
|
|
aEnd->x = aStart->x + dx * t_max;
|
|
|
|
aEnd->y = aStart->y + dy * t_max;
|
|
|
|
aStart->x = aStart->x + dx * t_min;
|
|
|
|
aStart->y = aStart->y + dy * t_min;
|
|
|
|
|
2014-06-10 13:26:32 -07:00
|
|
|
*aRepeatStartFactor = t_min;
|
|
|
|
*aRepeatEndFactor = t_max;
|
2013-10-11 07:26:40 -07:00
|
|
|
}
|
2013-07-18 17:08:51 -07:00
|
|
|
|
2014-06-10 13:27:45 -07:00
|
|
|
static CGGradientRef
|
2014-07-23 09:12:59 -07:00
|
|
|
CreateRepeatingGradient(CGColorSpaceRef aColorSpace,
|
|
|
|
CGContextRef cg, GradientStopsCG* aStops,
|
2014-06-10 13:27:45 -07:00
|
|
|
int aRepeatStartFactor, int aRepeatEndFactor,
|
|
|
|
bool aReflect)
|
2013-07-18 17:08:51 -07:00
|
|
|
{
|
2014-06-10 13:27:45 -07:00
|
|
|
int repeatCount = aRepeatEndFactor - aRepeatStartFactor;
|
2014-06-10 13:29:03 -07:00
|
|
|
uint32_t stopCount = aStops->mStops.size();
|
2013-07-18 17:08:51 -07:00
|
|
|
double scale = 1./repeatCount;
|
|
|
|
|
|
|
|
std::vector<CGFloat> colors;
|
|
|
|
std::vector<CGFloat> offsets;
|
2014-06-10 13:29:03 -07:00
|
|
|
colors.reserve(stopCount*repeatCount*4);
|
|
|
|
offsets.reserve(stopCount*repeatCount);
|
2014-06-10 13:26:32 -07:00
|
|
|
|
2014-06-10 13:27:45 -07:00
|
|
|
for (int j = aRepeatStartFactor; j < aRepeatEndFactor; j++) {
|
2014-06-10 13:26:32 -07:00
|
|
|
bool isReflected = aReflect && (j % 2) != 0;
|
2014-06-10 13:29:03 -07:00
|
|
|
for (uint32_t i = 0; i < stopCount; i++) {
|
|
|
|
uint32_t stopIndex = isReflected ? stopCount - i - 1 : i;
|
2014-06-10 13:27:45 -07:00
|
|
|
colors.push_back(aStops->mStops[stopIndex].color.r);
|
|
|
|
colors.push_back(aStops->mStops[stopIndex].color.g);
|
|
|
|
colors.push_back(aStops->mStops[stopIndex].color.b);
|
|
|
|
colors.push_back(aStops->mStops[stopIndex].color.a);
|
2014-06-10 13:26:32 -07:00
|
|
|
|
2014-06-10 13:27:45 -07:00
|
|
|
CGFloat offset = aStops->mStops[stopIndex].offset;
|
2014-06-10 13:26:32 -07:00
|
|
|
if (isReflected) {
|
|
|
|
offset = 1 - offset;
|
|
|
|
}
|
2014-06-10 13:27:45 -07:00
|
|
|
offsets.push_back((offset + (j - aRepeatStartFactor)) * scale);
|
2013-07-18 17:08:51 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-23 09:12:59 -07:00
|
|
|
CGGradientRef gradient = CGGradientCreateWithColorComponents(aColorSpace,
|
2013-07-18 17:08:51 -07:00
|
|
|
&colors.front(),
|
|
|
|
&offsets.front(),
|
2014-06-10 13:29:03 -07:00
|
|
|
repeatCount*stopCount);
|
2014-06-10 13:27:45 -07:00
|
|
|
return gradient;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2014-07-23 09:12:59 -07:00
|
|
|
DrawLinearRepeatingGradient(CGColorSpaceRef aColorSpace, CGContextRef cg,
|
|
|
|
const LinearGradientPattern &aPattern,
|
2014-06-10 13:27:45 -07:00
|
|
|
const CGRect &aExtents, bool aReflect)
|
|
|
|
{
|
|
|
|
GradientStopsCG *stops = static_cast<GradientStopsCG*>(aPattern.mStops.get());
|
|
|
|
CGPoint startPoint = { aPattern.mBegin.x, aPattern.mBegin.y };
|
|
|
|
CGPoint endPoint = { aPattern.mEnd.x, aPattern.mEnd.y };
|
|
|
|
|
|
|
|
int repeatStartFactor = 0, repeatEndFactor = 1;
|
|
|
|
// if we don't have a line then we can't extend it
|
|
|
|
if (aPattern.mEnd.x != aPattern.mBegin.x ||
|
|
|
|
aPattern.mEnd.y != aPattern.mBegin.y) {
|
|
|
|
CalculateRepeatingGradientParams(&startPoint, &endPoint, aExtents,
|
|
|
|
&repeatStartFactor, &repeatEndFactor);
|
|
|
|
}
|
|
|
|
|
2014-07-23 09:12:59 -07:00
|
|
|
CGGradientRef gradient = CreateRepeatingGradient(aColorSpace, cg, stops, repeatStartFactor, repeatEndFactor, aReflect);
|
2013-07-18 17:08:51 -07:00
|
|
|
|
|
|
|
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
|
2014-07-23 09:12:59 -07:00
|
|
|
DrawRadialRepeatingGradient(CGColorSpaceRef aColorSpace, CGContextRef cg,
|
|
|
|
const RadialGradientPattern &aPattern,
|
2014-06-10 13:26:58 -07:00
|
|
|
const CGRect &aExtents, bool aReflect)
|
2013-07-18 17:08:51 -07:00
|
|
|
{
|
|
|
|
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;
|
2014-06-10 13:26:58 -07:00
|
|
|
int repeatStartFactor = 0, repeatEndFactor = 1;
|
2013-07-18 17:08:51 -07:00
|
|
|
while (endRadius < minimumEndRadius) {
|
|
|
|
endRadius += length;
|
2014-06-10 13:26:58 -07:00
|
|
|
repeatEndFactor++;
|
2013-07-18 17:08:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
while (startRadius-length >= 0) {
|
|
|
|
startRadius -= length;
|
2014-06-10 13:26:58 -07:00
|
|
|
repeatStartFactor--;
|
2013-07-18 17:08:51 -07:00
|
|
|
}
|
|
|
|
|
2014-07-23 09:12:59 -07:00
|
|
|
CGGradientRef gradient = CreateRepeatingGradient(aColorSpace, cg, stops, repeatStartFactor, repeatEndFactor, aReflect);
|
2013-07-18 17:08:51 -07:00
|
|
|
|
|
|
|
//XXX: are there degenerate radial gradients that we should avoid drawing?
|
|
|
|
CGContextDrawRadialGradient(cg, gradient, startCenter, startRadius, endCenter, endRadius,
|
|
|
|
kCGGradientDrawsBeforeStartLocation | kCGGradientDrawsAfterEndLocation);
|
|
|
|
CGGradientRelease(gradient);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2014-07-23 09:12:59 -07:00
|
|
|
DrawGradient(CGColorSpaceRef aColorSpace,
|
|
|
|
CGContextRef cg, const Pattern &aPattern, const CGRect &aExtents)
|
2012-01-09 10:54:44 -08:00
|
|
|
{
|
2014-02-18 14:18:29 -08:00
|
|
|
if (CGRectIsEmpty(aExtents)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-01-10 11:06:17 -08:00
|
|
|
if (aPattern.GetType() == PatternType::LINEAR_GRADIENT) {
|
2012-01-09 10:54:44 -08:00
|
|
|
const LinearGradientPattern& pat = static_cast<const LinearGradientPattern&>(aPattern);
|
|
|
|
GradientStopsCG *stops = static_cast<GradientStopsCG*>(pat.mStops.get());
|
2014-08-07 04:23:53 -07:00
|
|
|
CGAffineTransform patternMatrix = GfxMatrixToCGAffineTransform(pat.mMatrix);
|
|
|
|
CGContextConcatCTM(cg, patternMatrix);
|
|
|
|
CGRect extents = CGRectApplyAffineTransform(aExtents, CGAffineTransformInvert(patternMatrix));
|
2014-01-10 11:06:17 -08:00
|
|
|
if (stops->mExtend == ExtendMode::CLAMP) {
|
2013-07-18 17:08:51 -07:00
|
|
|
|
|
|
|
// 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);
|
2014-06-10 13:26:32 -07:00
|
|
|
} else if (stops->mExtend == ExtendMode::REPEAT || stops->mExtend == ExtendMode::REFLECT) {
|
2014-08-07 04:23:53 -07:00
|
|
|
DrawLinearRepeatingGradient(aColorSpace, cg, pat, extents, stops->mExtend == ExtendMode::REFLECT);
|
2013-07-18 17:08:51 -07:00
|
|
|
}
|
2014-01-10 11:06:17 -08:00
|
|
|
} else if (aPattern.GetType() == PatternType::RADIAL_GRADIENT) {
|
2012-01-09 10:54:44 -08:00
|
|
|
const RadialGradientPattern& pat = static_cast<const RadialGradientPattern&>(aPattern);
|
2014-08-07 04:23:53 -07:00
|
|
|
CGAffineTransform patternMatrix = GfxMatrixToCGAffineTransform(pat.mMatrix);
|
|
|
|
CGContextConcatCTM(cg, patternMatrix);
|
|
|
|
CGRect extents = CGRectApplyAffineTransform(aExtents, CGAffineTransformInvert(patternMatrix));
|
2012-01-09 10:54:44 -08:00
|
|
|
GradientStopsCG *stops = static_cast<GradientStopsCG*>(pat.mStops.get());
|
2014-01-10 11:06:17 -08:00
|
|
|
if (stops->mExtend == ExtendMode::CLAMP) {
|
2013-07-18 17:08:51 -07:00
|
|
|
|
|
|
|
// 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);
|
2014-06-10 13:26:58 -07:00
|
|
|
} else if (stops->mExtend == ExtendMode::REPEAT || stops->mExtend == ExtendMode::REFLECT) {
|
2014-08-07 04:23:53 -07:00
|
|
|
DrawRadialRepeatingGradient(aColorSpace, cg, pat, extents, stops->mExtend == ExtendMode::REFLECT);
|
2013-07-18 17:08:51 -07:00
|
|
|
}
|
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)
|
|
|
|
{
|
2014-01-10 11:06:17 -08:00
|
|
|
return aPattern.GetType() == PatternType::LINEAR_GRADIENT || aPattern.GetType() == PatternType::RADIAL_GRADIENT;
|
2012-01-09 10:54:44 -08:00
|
|
|
}
|
|
|
|
|
2014-07-23 09:12:59 -07:00
|
|
|
static bool
|
|
|
|
isNonRepeatingSurface(const Pattern& aPattern)
|
|
|
|
{
|
|
|
|
return aPattern.GetType() == PatternType::SURFACE &&
|
|
|
|
static_cast<const SurfacePattern&>(aPattern).mExtendMode != ExtendMode::REPEAT;
|
|
|
|
}
|
|
|
|
|
2012-01-09 10:54:44 -08:00
|
|
|
/* 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?
|
2013-11-27 03:21:43 -08:00
|
|
|
CGImageRef image = GetRetainedImageFromSourceSurface(pat.mSurface.get());
|
2014-09-11 22:18:22 -07:00
|
|
|
Matrix patTransform = pat.mMatrix;
|
|
|
|
if (!pat.mSamplingRect.IsEmpty()) {
|
|
|
|
CGImageRef temp = CGImageCreateWithImageInRect(image, RectToCGRect(pat.mSamplingRect));
|
|
|
|
CGImageRelease(image);
|
|
|
|
image = temp;
|
|
|
|
patTransform.PreTranslate(pat.mSamplingRect.x, pat.mSamplingRect.y);
|
|
|
|
}
|
2012-01-09 10:54:44 -08:00
|
|
|
CGFloat xStep, yStep;
|
|
|
|
switch (pat.mExtendMode) {
|
2014-01-10 11:06:17 -08:00
|
|
|
case ExtendMode::CLAMP:
|
2012-01-09 10:54:44 -08:00
|
|
|
// 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;
|
2014-01-10 11:06:17 -08:00
|
|
|
case ExtendMode::REFLECT:
|
2012-01-09 10:54:44 -08:00
|
|
|
assert(0);
|
2014-01-10 11:06:17 -08:00
|
|
|
case ExtendMode::REPEAT:
|
2012-01-09 10:54:44 -08:00
|
|
|
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),
|
2014-09-11 22:18:22 -07:00
|
|
|
GfxMatrixToCGAffineTransform(patTransform)),
|
2012-11-14 16:52:02 -08:00
|
|
|
aUserSpace);
|
2012-01-09 10:54:44 -08:00
|
|
|
transform = CGAffineTransformTranslate(transform, 0, -static_cast<float>(CGImageGetHeight(image)));
|
2013-11-27 03:21:43 -08:00
|
|
|
return CGPatternCreate(image, bounds, transform, xStep, yStep, kCGPatternTilingConstantSpacing,
|
2012-01-09 10:54:44 -08:00
|
|
|
true, &patternCallbacks);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
SetFillFromPattern(CGContextRef cg, CGColorSpaceRef aColorSpace, const Pattern &aPattern)
|
|
|
|
{
|
|
|
|
assert(!isGradient(aPattern));
|
2014-01-10 11:06:17 -08:00
|
|
|
if (aPattern.GetType() == PatternType::COLOR) {
|
2012-01-09 10:54:44 -08:00
|
|
|
|
|
|
|
const Color& color = static_cast<const ColorPattern&>(aPattern).mColor;
|
|
|
|
//XXX: we should cache colors
|
|
|
|
CGColorRef cgcolor = ColorToCGColor(aColorSpace, color);
|
|
|
|
CGContextSetFillColorWithColor(cg, cgcolor);
|
|
|
|
CGColorRelease(cgcolor);
|
2014-01-10 11:06:17 -08:00
|
|
|
} else if (aPattern.GetType() == PatternType::SURFACE) {
|
2012-01-09 10:54:44 -08:00
|
|
|
|
|
|
|
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));
|
2014-01-10 11:06:17 -08:00
|
|
|
if (aPattern.GetType() == PatternType::COLOR) {
|
2012-01-09 10:54:44 -08:00
|
|
|
const Color& color = static_cast<const ColorPattern&>(aPattern).mColor;
|
|
|
|
//XXX: we should cache colors
|
|
|
|
CGColorRef cgcolor = ColorToCGColor(aColorSpace, color);
|
|
|
|
CGContextSetStrokeColorWithColor(cg, cgcolor);
|
|
|
|
CGColorRelease(cgcolor);
|
2014-01-10 11:06:17 -08:00
|
|
|
} else if (aPattern.GetType() == PatternType::SURFACE) {
|
2012-01-09 10:54:44 -08:00
|
|
|
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();
|
|
|
|
|
|
|
|
CGContextSaveGState(mCg);
|
|
|
|
|
|
|
|
CGContextSetBlendMode(mCg, ToBlendMode(aDrawOptions.mCompositionOp));
|
|
|
|
UnboundnessFixer fixer;
|
|
|
|
CGContextRef cg = fixer.Check(mCg, aDrawOptions.mCompositionOp);
|
|
|
|
CGContextSetAlpha(cg, aDrawOptions.mAlpha);
|
2014-01-10 11:06:17 -08:00
|
|
|
CGContextSetShouldAntialias(cg, aDrawOptions.mAntialiasMode != AntialiasMode::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));
|
2013-11-27 03:21:43 -08:00
|
|
|
CGImageRef image = GetRetainedImageFromSourceSurface(aMask);
|
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
|
|
|
|
|
|
|
// 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.
|
2014-07-23 09:12:59 -07:00
|
|
|
DrawGradient(mColorSpace, 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));
|
|
|
|
}
|
|
|
|
|
2013-11-27 03:21:43 -08:00
|
|
|
CGImageRelease(image);
|
|
|
|
|
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
|
|
|
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,
|
2014-07-23 09:12:59 -07:00
|
|
|
const Pattern &aPattern,
|
|
|
|
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;
|
2014-04-01 01:57:35 -07:00
|
|
|
CGContextRef cg = fixer.Check(mCg, aDrawOptions.mCompositionOp, &aRect);
|
2012-01-09 10:54:44 -08:00
|
|
|
CGContextSetAlpha(mCg, aDrawOptions.mAlpha);
|
2014-01-10 11:06:17 -08:00
|
|
|
CGContextSetShouldAntialias(cg, aDrawOptions.mAntialiasMode != AntialiasMode::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));
|
2014-02-13 03:10:43 -08:00
|
|
|
CGRect clipBounds = CGContextGetClipBoundingBox(cg);
|
2014-07-23 09:12:59 -07:00
|
|
|
DrawGradient(mColorSpace, cg, aPattern, clipBounds);
|
|
|
|
} else if (isNonRepeatingSurface(aPattern)) {
|
|
|
|
// 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 = GetRetainedImageFromSourceSurface(pat.mSurface.get());
|
2014-09-11 22:18:22 -07:00
|
|
|
Matrix transform = pat.mMatrix;
|
|
|
|
if (!pat.mSamplingRect.IsEmpty()) {
|
|
|
|
CGImageRef temp = CGImageCreateWithImageInRect(image, RectToCGRect(pat.mSamplingRect));
|
|
|
|
CGImageRelease(image);
|
|
|
|
image = temp;
|
|
|
|
transform.PreTranslate(pat.mSamplingRect.x, pat.mSamplingRect.y);
|
|
|
|
}
|
2014-07-23 09:12:59 -07:00
|
|
|
CGContextClipToRect(cg, RectToCGRect(aRect));
|
2014-09-11 22:18:22 -07:00
|
|
|
CGContextConcatCTM(cg, GfxMatrixToCGAffineTransform(transform));
|
2014-07-23 09:12:59 -07:00
|
|
|
CGContextTranslateCTM(cg, 0, CGImageGetHeight(image));
|
|
|
|
CGContextScaleCTM(cg, 1, -1);
|
|
|
|
|
|
|
|
CGRect imageRect = CGRectMake(0, 0, CGImageGetWidth(image), CGImageGetHeight(image));
|
|
|
|
|
|
|
|
CGContextSetInterpolationQuality(cg, InterpolationQualityFromFilter(pat.mFilter));
|
|
|
|
|
|
|
|
CGContextDrawImage(cg, imageRect, image);
|
|
|
|
CGImageRelease(image);
|
2012-01-09 10:54:44 -08:00
|
|
|
} else {
|
2014-07-23 09:12:59 -07:00
|
|
|
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)
|
|
|
|
{
|
2013-09-06 12:22:20 -07:00
|
|
|
if (!std::isfinite(p1.x) ||
|
|
|
|
!std::isfinite(p1.y) ||
|
|
|
|
!std::isfinite(p2.x) ||
|
|
|
|
!std::isfinite(p2.y)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
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);
|
2014-01-10 11:06:17 -08:00
|
|
|
CGContextSetShouldAntialias(cg, aDrawOptions.mAntialiasMode != AntialiasMode::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);
|
2014-07-23 09:12:59 -07:00
|
|
|
DrawGradient(mColorSpace, cg, aPattern, extents);
|
2012-01-09 10:54:44 -08:00
|
|
|
} else {
|
|
|
|
SetStrokeFromPattern(cg, mColorSpace, aPattern);
|
|
|
|
CGContextStrokePath(cg);
|
|
|
|
}
|
|
|
|
|
|
|
|
fixer.Fix(mCg);
|
|
|
|
CGContextRestoreGState(mCg);
|
|
|
|
}
|
|
|
|
|
2014-11-11 23:52:57 -08:00
|
|
|
static bool
|
|
|
|
IsInteger(Float aValue)
|
|
|
|
{
|
|
|
|
return floorf(aValue) == aValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
IsPixelAlignedStroke(const Rect& aRect, Float aLineWidth)
|
|
|
|
{
|
|
|
|
Float halfWidth = aLineWidth/2;
|
|
|
|
return IsInteger(aLineWidth) &&
|
|
|
|
IsInteger(aRect.x - halfWidth) && IsInteger(aRect.y - halfWidth) &&
|
|
|
|
IsInteger(aRect.XMost() - halfWidth) && IsInteger(aRect.YMost() - halfWidth);
|
|
|
|
}
|
|
|
|
|
2012-01-09 10:54:44 -08:00
|
|
|
void
|
|
|
|
DrawTargetCG::StrokeRect(const Rect &aRect,
|
|
|
|
const Pattern &aPattern,
|
|
|
|
const StrokeOptions &aStrokeOptions,
|
|
|
|
const DrawOptions &aDrawOptions)
|
|
|
|
{
|
2013-09-06 12:22:20 -07:00
|
|
|
if (!aRect.IsFinite()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
CGContextSetBlendMode(mCg, ToBlendMode(aDrawOptions.mCompositionOp));
|
|
|
|
|
2014-11-11 23:52:57 -08:00
|
|
|
// Work around Quartz bug where antialiasing causes corner pixels to be off by
|
|
|
|
// 1 channel value (e.g. rgb(1,1,1) values appear at the corner of solid
|
|
|
|
// black stroke), by turning off antialiasing when the edges of the stroke
|
|
|
|
// are pixel-aligned. Note that when a transform's components are all
|
|
|
|
// integers, it maps integers coordinates to integer coordinates.
|
|
|
|
bool pixelAlignedStroke = mTransform.IsAllIntegers() &&
|
|
|
|
mTransform.PreservesAxisAlignedRectangles() &&
|
|
|
|
aPattern.GetType() == PatternType::COLOR &&
|
|
|
|
IsPixelAlignedStroke(aRect, aStrokeOptions.mLineWidth);
|
|
|
|
CGContextSetShouldAntialias(cg,
|
|
|
|
aDrawOptions.mAntialiasMode != AntialiasMode::NONE && !pixelAlignedStroke);
|
|
|
|
|
2012-01-09 10:54:44 -08:00
|
|
|
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);
|
2014-07-23 09:12:59 -07:00
|
|
|
DrawGradient(mColorSpace, 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)
|
|
|
|
{
|
2013-09-06 12:22:20 -07:00
|
|
|
if (!aPath->GetBounds().IsFinite()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
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);
|
2014-01-10 11:06:17 -08:00
|
|
|
CGContextSetShouldAntialias(cg, aDrawOptions.mAntialiasMode != AntialiasMode::NONE);
|
2012-01-09 10:54:44 -08:00
|
|
|
CGContextSetBlendMode(mCg, ToBlendMode(aDrawOptions.mCompositionOp));
|
|
|
|
|
|
|
|
CGContextConcatCTM(cg, GfxMatrixToCGAffineTransform(mTransform));
|
|
|
|
|
|
|
|
|
|
|
|
CGContextBeginPath(cg);
|
|
|
|
|
2014-01-10 11:06:16 -08:00
|
|
|
assert(aPath->GetBackendType() == BackendType::COREGRAPHICS);
|
2012-01-09 10:54:44 -08:00
|
|
|
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);
|
2014-07-23 09:12:59 -07:00
|
|
|
DrawGradient(mColorSpace, 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();
|
|
|
|
|
2014-01-10 11:06:16 -08:00
|
|
|
assert(aPath->GetBackendType() == BackendType::COREGRAPHICS);
|
2012-01-09 10:54:44 -08:00
|
|
|
|
|
|
|
CGContextSaveGState(mCg);
|
|
|
|
|
|
|
|
CGContextSetBlendMode(mCg, ToBlendMode(aDrawOptions.mCompositionOp));
|
|
|
|
UnboundnessFixer fixer;
|
|
|
|
CGContextRef cg = fixer.Check(mCg, aDrawOptions.mCompositionOp);
|
|
|
|
CGContextSetAlpha(cg, aDrawOptions.mAlpha);
|
2014-01-10 11:06:17 -08:00
|
|
|
CGContextSetShouldAntialias(cg, aDrawOptions.mAntialiasMode != AntialiasMode::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);
|
2014-01-10 11:06:17 -08:00
|
|
|
if (cgPath->GetFillRule() == FillRule::FILL_EVEN_ODD)
|
2012-02-24 21:35:39 -08:00
|
|
|
CGContextEOClip(mCg);
|
|
|
|
else
|
|
|
|
CGContextClip(mCg);
|
|
|
|
}
|
|
|
|
|
2014-07-23 09:12:59 -07:00
|
|
|
DrawGradient(mColorSpace, cg, aPattern, extents);
|
2012-01-09 10:54:44 -08:00
|
|
|
} else {
|
|
|
|
CGContextAddPath(cg, cgPath->GetPath());
|
|
|
|
|
|
|
|
SetFillFromPattern(cg, mColorSpace, aPattern);
|
|
|
|
|
2014-01-10 11:06:17 -08:00
|
|
|
if (cgPath->GetFillRule() == FillRule::FILL_EVEN_ODD)
|
2012-01-09 10:54:44 -08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-10-24 09:32:23 -07:00
|
|
|
typedef void (*CGContextSetFontSmoothingBackgroundColorFunc) (CGContextRef cgContext, CGColorRef color);
|
|
|
|
|
|
|
|
static CGContextSetFontSmoothingBackgroundColorFunc
|
|
|
|
GetCGContextSetFontSmoothingBackgroundColorFunc()
|
|
|
|
{
|
|
|
|
static CGContextSetFontSmoothingBackgroundColorFunc func = nullptr;
|
|
|
|
static bool lookedUpFunc = false;
|
|
|
|
if (!lookedUpFunc) {
|
|
|
|
func = (CGContextSetFontSmoothingBackgroundColorFunc)dlsym(
|
|
|
|
RTLD_DEFAULT, "CGContextSetFontSmoothingBackgroundColor");
|
|
|
|
lookedUpFunc = true;
|
|
|
|
}
|
|
|
|
return func;
|
|
|
|
}
|
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,
|
2014-10-24 09:32:23 -07:00
|
|
|
const GlyphRenderingOptions *aGlyphRenderingOptions)
|
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);
|
|
|
|
|
2014-10-24 09:32:23 -07:00
|
|
|
if (aGlyphRenderingOptions && aGlyphRenderingOptions->GetType() == FontType::MAC) {
|
|
|
|
Color fontSmoothingBackgroundColor =
|
|
|
|
static_cast<const GlyphRenderingOptionsCG*>(aGlyphRenderingOptions)->FontSmoothingBackgroundColor();
|
|
|
|
if (fontSmoothingBackgroundColor.a > 0) {
|
|
|
|
CGContextSetFontSmoothingBackgroundColorFunc setFontSmoothingBGColorFunc =
|
|
|
|
GetCGContextSetFontSmoothingBackgroundColorFunc();
|
|
|
|
if (setFontSmoothingBGColorFunc) {
|
|
|
|
CGColorRef color = ColorToCGColor(mColorSpace, fontSmoothingBackgroundColor);
|
|
|
|
setFontSmoothingBGColorFunc(mCg, color);
|
|
|
|
CGColorRelease(color);
|
|
|
|
|
|
|
|
// Font rendering with a non-transparent font smoothing background color
|
|
|
|
// can leave pixels in our buffer where the rgb components exceed the alpha
|
|
|
|
// component. When this happens we need to clean up the data afterwards.
|
|
|
|
// The purpose of this is probably the following: Correct compositing of
|
|
|
|
// subpixel anti-aliased fonts on transparent backgrounds requires
|
|
|
|
// different alpha values per RGB component. Usually, premultiplied color
|
|
|
|
// values are derived by multiplying all components with the same per-pixel
|
|
|
|
// alpha value. However, if you multiply each component with a *different*
|
|
|
|
// alpha, and set the alpha component of the pixel to, say, the average
|
|
|
|
// of the alpha values that you used during the premultiplication of the
|
|
|
|
// RGB components, you can trick OVER compositing into doing a simplified
|
|
|
|
// form of component alpha compositing. (You just need to make sure to
|
|
|
|
// clamp the components of the result pixel to [0,255] afterwards.)
|
|
|
|
mMayContainInvalidPremultipliedData = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-09 10:54:44 -08:00
|
|
|
CGContextSetBlendMode(mCg, ToBlendMode(aDrawOptions.mCompositionOp));
|
|
|
|
UnboundnessFixer fixer;
|
|
|
|
CGContextRef cg = fixer.Check(mCg, aDrawOptions.mCompositionOp);
|
|
|
|
CGContextSetAlpha(cg, aDrawOptions.mAlpha);
|
2014-01-10 11:06:17 -08:00
|
|
|
CGContextSetShouldAntialias(cg, aDrawOptions.mAntialiasMode != AntialiasMode::NONE);
|
|
|
|
if (aDrawOptions.mAntialiasMode != AntialiasMode::DEFAULT) {
|
|
|
|
CGContextSetShouldSmoothFonts(cg, aDrawOptions.mAntialiasMode == AntialiasMode::SUBPIXEL);
|
2013-07-29 13:00:53 -07:00
|
|
|
}
|
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
|
2014-02-15 15:21:43 -08:00
|
|
|
CGContextScaleCTM(cg, 1, -1);
|
2012-05-21 07:22:11 -07:00
|
|
|
// 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);
|
2014-11-11 03:02:43 -08:00
|
|
|
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);
|
2014-11-11 03:02:43 -08:00
|
|
|
delete[] bboxes;
|
2012-10-23 02:07:14 -07:00
|
|
|
}
|
2014-02-15 15:21:43 -08:00
|
|
|
CGContextScaleCTM(cg, 1, -1);
|
2014-07-23 09:12:59 -07:00
|
|
|
DrawGradient(mColorSpace, 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();
|
|
|
|
|
2014-01-10 10:55:24 -08:00
|
|
|
if (aSurface->GetType() == SurfaceType::COREGRAPHICS_IMAGE ||
|
2014-03-11 00:24:43 -07:00
|
|
|
aSurface->GetType() == SurfaceType::COREGRAPHICS_CGCONTEXT ||
|
|
|
|
aSurface->GetType() == SurfaceType::DATA) {
|
2013-11-27 03:21:43 -08:00
|
|
|
CGImageRef image = GetRetainedImageFromSourceSurface(aSurface);
|
|
|
|
|
2012-01-09 10:54:44 -08:00
|
|
|
// 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);
|
2014-04-02 20:24:05 -07:00
|
|
|
CGRect destRect = CGRectMake(aDestination.x, aDestination.y,
|
|
|
|
aSourceRect.width, aSourceRect.height);
|
|
|
|
CGContextClipToRect(mCg, destRect);
|
|
|
|
|
2012-01-09 10:54:44 -08:00
|
|
|
CGContextSetBlendMode(mCg, kCGBlendModeCopy);
|
|
|
|
|
|
|
|
CGContextScaleCTM(mCg, 1, -1);
|
|
|
|
|
2014-04-02 20:24:05 -07:00
|
|
|
CGRect flippedRect = CGRectMake(aDestination.x - aSourceRect.x, -(aDestination.y - aSourceRect.y + double(CGImageGetHeight(image))),
|
|
|
|
CGImageGetWidth(image), CGImageGetHeight(image));
|
2012-01-09 10:54:44 -08:00
|
|
|
|
2013-10-15 18:00:30 -07:00
|
|
|
// Quartz seems to copy A8 surfaces incorrectly if we don't initialize them
|
|
|
|
// to transparent first.
|
2014-01-10 11:06:16 -08:00
|
|
|
if (mFormat == SurfaceFormat::A8) {
|
2013-10-15 18:00:30 -07:00
|
|
|
CGContextClearRect(mCg, flippedRect);
|
|
|
|
}
|
2014-04-02 20:24:05 -07:00
|
|
|
CGContextDrawImage(mCg, flippedRect, image);
|
2012-01-09 10:54:44 -08:00
|
|
|
|
|
|
|
CGContextRestoreGState(mCg);
|
2014-05-20 23:43:16 -07:00
|
|
|
CGImageRelease(image);
|
2012-01-09 10:54:44 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
2013-11-27 03:21:43 -08:00
|
|
|
CGImageRef image = GetRetainedImageFromSourceSurface(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
|
|
|
|
2013-11-27 03:21:43 -08:00
|
|
|
CGImageRelease(image);
|
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) {
|
2014-06-13 14:03:52 -07:00
|
|
|
gfxWarning() << "Failed to Init() DrawTargetCG because of bad size.";
|
2012-08-14 11:06:12 -07:00
|
|
|
mColorSpace = nullptr;
|
|
|
|
mCg = 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();
|
|
|
|
|
2014-01-10 11:06:16 -08:00
|
|
|
if (aData == nullptr && aType != BackendType::COREGRAPHICS_ACCELERATED) {
|
2012-07-26 11:06:23 -07:00
|
|
|
// XXX: Currently, Init implicitly clears, that can often be a waste of time
|
2014-06-13 14:03:52 -07:00
|
|
|
size_t bufLen = BufferSizeFromStrideAndHeight(aStride, aSize.height);
|
|
|
|
if (bufLen == 0) {
|
|
|
|
mColorSpace = nullptr;
|
|
|
|
mCg = nullptr;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
static_assert(sizeof(decltype(mData[0])) == 1,
|
|
|
|
"mData.Realloc() takes an object count, so its objects must be 1-byte sized if we use bufLen");
|
2014-09-10 14:54:16 -07:00
|
|
|
mData.Realloc(/* actually an object count */ bufLen, true);
|
2013-09-27 08:20:28 -07:00
|
|
|
aData = static_cast<unsigned char*>(mData);
|
2012-07-26 11:06:23 -07:00
|
|
|
}
|
|
|
|
|
2012-01-09 10:54:44 -08:00
|
|
|
mSize = aSize;
|
|
|
|
|
2014-01-10 11:06:16 -08:00
|
|
|
if (aType == BackendType::COREGRAPHICS_ACCELERATED) {
|
2012-07-31 08:17:43 -07:00
|
|
|
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-07-26 11:06:23 -07:00
|
|
|
|
2014-01-10 11:06:16 -08:00
|
|
|
mFormat = SurfaceFormat::B8G8R8A8;
|
2013-07-29 12:01:29 -07:00
|
|
|
|
2014-01-10 11:06:16 -08:00
|
|
|
if (!mCg || aType == BackendType::COREGRAPHICS) {
|
2012-07-31 08:17:43 -07:00
|
|
|
int bitsPerComponent = 8;
|
2012-07-26 11:06:23 -07:00
|
|
|
|
2012-07-31 08:17:43 -07:00
|
|
|
CGBitmapInfo bitinfo;
|
2014-01-10 11:06:16 -08:00
|
|
|
if (aFormat == SurfaceFormat::A8) {
|
2013-07-29 12:01:29 -07:00
|
|
|
if (mColorSpace)
|
|
|
|
CGColorSpaceRelease(mColorSpace);
|
|
|
|
mColorSpace = nullptr;
|
|
|
|
bitinfo = kCGImageAlphaOnly;
|
2014-01-10 11:06:16 -08:00
|
|
|
mFormat = SurfaceFormat::A8;
|
2013-07-29 12:01:29 -07:00
|
|
|
} else {
|
|
|
|
bitinfo = kCGBitmapByteOrder32Host;
|
2014-01-10 11:06:16 -08:00
|
|
|
if (aFormat == SurfaceFormat::B8G8R8X8) {
|
2013-07-29 12:01:29 -07:00
|
|
|
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
|
|
|
|
|
|
|
|
2014-01-10 11:06:16 -08:00
|
|
|
if (aType == BackendType::COREGRAPHICS_ACCELERATED) {
|
2012-07-31 08:17:43 -07:00
|
|
|
// 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
|
|
|
|
2014-10-24 09:32:23 -07:00
|
|
|
static void
|
|
|
|
EnsureValidPremultipliedData(CGContextRef aContext)
|
|
|
|
{
|
|
|
|
if (CGBitmapContextGetBitsPerPixel(aContext) != 32 ||
|
|
|
|
CGBitmapContextGetAlphaInfo(aContext) != kCGImageAlphaPremultipliedFirst) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t* bitmapData = (uint8_t*)CGBitmapContextGetData(aContext);
|
|
|
|
int w = CGBitmapContextGetWidth(aContext);
|
|
|
|
int h = CGBitmapContextGetHeight(aContext);
|
|
|
|
int stride = CGBitmapContextGetBytesPerRow(aContext);
|
|
|
|
for (int y = 0; y < h; y++) {
|
|
|
|
for (int x = 0; x < w; x++) {
|
|
|
|
int i = y * stride + x * 4;
|
|
|
|
uint8_t a = bitmapData[i + 3];
|
|
|
|
|
|
|
|
// Clamp rgb components to the alpha component.
|
|
|
|
if (bitmapData[i + 0] > a) {
|
|
|
|
bitmapData[i + 0] = a;
|
|
|
|
}
|
|
|
|
if (bitmapData[i + 1] > a) {
|
|
|
|
bitmapData[i + 1] = a;
|
|
|
|
}
|
|
|
|
if (bitmapData[i + 2] > a) {
|
|
|
|
bitmapData[i + 2] = a;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-31 08:17:43 -07:00
|
|
|
void
|
|
|
|
DrawTargetCG::Flush()
|
|
|
|
{
|
2013-10-09 21:44:09 -07:00
|
|
|
if (GetContextType(mCg) == CG_CONTEXT_TYPE_IOSURFACE) {
|
|
|
|
CGContextFlush(mCg);
|
2014-10-24 09:32:23 -07:00
|
|
|
} else if (GetContextType(mCg) == CG_CONTEXT_TYPE_BITMAP &&
|
|
|
|
mMayContainInvalidPremultipliedData) {
|
|
|
|
// We can't guarantee that all our users can handle pixel data where an RGB
|
|
|
|
// component value exceeds the pixel's alpha value. In particular, the
|
|
|
|
// color conversion that CG does when we draw a CGImage snapshot of this
|
|
|
|
// context into a context that has a different color space throws up on
|
|
|
|
// invalid premultiplied data and creates completely wrong colors.
|
|
|
|
// Sanitizing the data means that we lose some of the fake component alpha
|
|
|
|
// behavior that font rendering tries to give us, but the result still
|
|
|
|
// looks good enough to prefer it over grayscale font anti-aliasing.
|
|
|
|
EnsureValidPremultipliedData(mCg);
|
|
|
|
mMayContainInvalidPremultipliedData = false;
|
2013-10-09 21:44:09 -07:00
|
|
|
}
|
2012-07-31 08:17:43 -07:00
|
|
|
}
|
|
|
|
|
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;
|
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-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);
|
|
|
|
|
2014-01-10 11:06:16 -08:00
|
|
|
mFormat = SurfaceFormat::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) {
|
2014-01-10 11:06:16 -08:00
|
|
|
mFormat = SurfaceFormat::A8;
|
2013-07-29 12:01:29 -07:00
|
|
|
} else if ((bitinfo & kCGBitmapAlphaInfoMask) == kCGImageAlphaNoneSkipFirst) {
|
2014-01-10 11:06:16 -08:00
|
|
|
mFormat = SurfaceFormat::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
|
|
|
{
|
2013-09-27 08:20:28 -07:00
|
|
|
int32_t stride = GetAlignedStride<16>(aSize.width * BytesPerPixel(aFormat));
|
2012-07-26 11:06:23 -07:00
|
|
|
|
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
|
|
|
|
{
|
2014-06-13 09:09:23 -07:00
|
|
|
return new PathBuilderCG(aFillRule);
|
2012-01-09 10:54:44 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void*
|
|
|
|
DrawTargetCG::GetNativeSurface(NativeSurfaceType aType)
|
|
|
|
{
|
2014-01-10 11:06:16 -08:00
|
|
|
if ((aType == NativeSurfaceType::CGCONTEXT && GetContextType(mCg) == CG_CONTEXT_TYPE_BITMAP) ||
|
|
|
|
(aType == NativeSurfaceType::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 {
|
2014-01-10 11:06:17 -08:00
|
|
|
if (aMask.GetType() == PatternType::COLOR) {
|
2012-01-09 10:54:44 -08:00
|
|
|
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);
|
2014-01-10 11:06:17 -08:00
|
|
|
} else if (aMask.GetType() == PatternType::SURFACE) {
|
2012-01-09 10:54:44 -08:00
|
|
|
const SurfacePattern& pat = static_cast<const SurfacePattern&>(aMask);
|
2013-11-27 03:21:43 -08:00
|
|
|
CGImageRef mask = GetRetainedImageFromSourceSurface(pat.mSurface.get());
|
2014-09-11 22:18:22 -07:00
|
|
|
MOZ_ASSERT(pat.mSamplingRect.IsEmpty(), "Sampling rect not supported with masks!");
|
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);
|
2013-11-27 03:21:43 -08:00
|
|
|
CGImageRelease(mask);
|
2012-01-09 10:54:44 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2014-01-10 11:06:16 -08:00
|
|
|
assert(aPath->GetBackendType() == BackendType::COREGRAPHICS);
|
2012-01-09 10:54:44 -08:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
2014-01-10 11:06:17 -08:00
|
|
|
if (cgPath->GetFillRule() == FillRule::FILL_EVEN_ODD)
|
2012-01-09 10:54:44 -08:00
|
|
|
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)
|
|
|
|
{
|
2014-08-06 05:40:03 -07:00
|
|
|
if ((aDT->GetBackendType() == BackendType::COREGRAPHICS ||
|
|
|
|
aDT->GetBackendType() == BackendType::COREGRAPHICS_ACCELERATED) &&
|
|
|
|
!aDT->IsTiledDrawTarget() && !aDT->IsDualDrawTarget()) {
|
2013-07-09 15:55:02 -07:00
|
|
|
DrawTargetCG* cgDT = static_cast<DrawTargetCG*>(aDT);
|
2014-10-24 09:32:23 -07:00
|
|
|
cgDT->Flush();
|
2013-07-09 15:55:02 -07:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|