2012-01-10 02:15:10 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
2012-05-21 15:12:37 +04:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2012-01-10 02:15:10 +04:00
|
|
|
|
|
|
|
#ifndef MOZILLA_GFX_HELPERSCAIRO_H_
|
|
|
|
#define MOZILLA_GFX_HELPERSCAIRO_H_
|
|
|
|
|
|
|
|
#include "2D.h"
|
|
|
|
#include "cairo.h"
|
2012-08-09 00:17:04 +04:00
|
|
|
#include "Logging.h"
|
2012-01-10 02:15:10 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace gfx {
|
|
|
|
|
|
|
|
static inline cairo_operator_t
|
|
|
|
GfxOpToCairoOp(CompositionOp op)
|
|
|
|
{
|
|
|
|
switch (op)
|
|
|
|
{
|
2014-01-10 23:06:17 +04:00
|
|
|
case CompositionOp::OP_OVER:
|
2012-01-10 02:15:10 +04:00
|
|
|
return CAIRO_OPERATOR_OVER;
|
2014-01-10 23:06:17 +04:00
|
|
|
case CompositionOp::OP_ADD:
|
2012-01-10 02:15:10 +04:00
|
|
|
return CAIRO_OPERATOR_ADD;
|
2014-01-10 23:06:17 +04:00
|
|
|
case CompositionOp::OP_ATOP:
|
2012-01-10 02:15:10 +04:00
|
|
|
return CAIRO_OPERATOR_ATOP;
|
2014-01-10 23:06:17 +04:00
|
|
|
case CompositionOp::OP_OUT:
|
2012-01-10 02:15:10 +04:00
|
|
|
return CAIRO_OPERATOR_OUT;
|
2014-01-10 23:06:17 +04:00
|
|
|
case CompositionOp::OP_IN:
|
2012-01-10 02:15:10 +04:00
|
|
|
return CAIRO_OPERATOR_IN;
|
2014-01-10 23:06:17 +04:00
|
|
|
case CompositionOp::OP_SOURCE:
|
2012-01-10 02:15:10 +04:00
|
|
|
return CAIRO_OPERATOR_SOURCE;
|
2014-01-10 23:06:17 +04:00
|
|
|
case CompositionOp::OP_DEST_IN:
|
2012-01-10 02:15:10 +04:00
|
|
|
return CAIRO_OPERATOR_DEST_IN;
|
2014-01-10 23:06:17 +04:00
|
|
|
case CompositionOp::OP_DEST_OUT:
|
2012-01-10 02:15:10 +04:00
|
|
|
return CAIRO_OPERATOR_DEST_OUT;
|
2014-01-10 23:06:17 +04:00
|
|
|
case CompositionOp::OP_DEST_OVER:
|
2012-01-10 02:15:10 +04:00
|
|
|
return CAIRO_OPERATOR_DEST_OVER;
|
2014-01-10 23:06:17 +04:00
|
|
|
case CompositionOp::OP_DEST_ATOP:
|
2012-01-10 02:15:10 +04:00
|
|
|
return CAIRO_OPERATOR_DEST_ATOP;
|
2014-01-10 23:06:17 +04:00
|
|
|
case CompositionOp::OP_XOR:
|
2012-01-10 02:15:10 +04:00
|
|
|
return CAIRO_OPERATOR_XOR;
|
2014-01-10 23:06:17 +04:00
|
|
|
case CompositionOp::OP_MULTIPLY:
|
2012-12-13 00:28:17 +04:00
|
|
|
return CAIRO_OPERATOR_MULTIPLY;
|
2014-01-10 23:06:17 +04:00
|
|
|
case CompositionOp::OP_SCREEN:
|
2012-12-13 00:28:17 +04:00
|
|
|
return CAIRO_OPERATOR_SCREEN;
|
2014-01-10 23:06:17 +04:00
|
|
|
case CompositionOp::OP_OVERLAY:
|
2012-12-13 00:28:17 +04:00
|
|
|
return CAIRO_OPERATOR_OVERLAY;
|
2014-01-10 23:06:17 +04:00
|
|
|
case CompositionOp::OP_DARKEN:
|
2012-12-13 00:28:17 +04:00
|
|
|
return CAIRO_OPERATOR_DARKEN;
|
2014-01-10 23:06:17 +04:00
|
|
|
case CompositionOp::OP_LIGHTEN:
|
2012-12-13 00:28:17 +04:00
|
|
|
return CAIRO_OPERATOR_LIGHTEN;
|
2014-01-10 23:06:17 +04:00
|
|
|
case CompositionOp::OP_COLOR_DODGE:
|
2012-12-13 00:28:17 +04:00
|
|
|
return CAIRO_OPERATOR_COLOR_DODGE;
|
2014-01-10 23:06:17 +04:00
|
|
|
case CompositionOp::OP_COLOR_BURN:
|
2012-12-13 00:28:17 +04:00
|
|
|
return CAIRO_OPERATOR_COLOR_BURN;
|
2014-01-10 23:06:17 +04:00
|
|
|
case CompositionOp::OP_HARD_LIGHT:
|
2012-12-13 00:28:17 +04:00
|
|
|
return CAIRO_OPERATOR_HARD_LIGHT;
|
2014-01-10 23:06:17 +04:00
|
|
|
case CompositionOp::OP_SOFT_LIGHT:
|
2012-12-13 00:28:17 +04:00
|
|
|
return CAIRO_OPERATOR_SOFT_LIGHT;
|
2014-01-10 23:06:17 +04:00
|
|
|
case CompositionOp::OP_DIFFERENCE:
|
2012-12-13 00:28:17 +04:00
|
|
|
return CAIRO_OPERATOR_DIFFERENCE;
|
2014-01-10 23:06:17 +04:00
|
|
|
case CompositionOp::OP_EXCLUSION:
|
2012-12-13 00:28:17 +04:00
|
|
|
return CAIRO_OPERATOR_EXCLUSION;
|
2014-01-10 23:06:17 +04:00
|
|
|
case CompositionOp::OP_HUE:
|
2012-12-13 00:28:17 +04:00
|
|
|
return CAIRO_OPERATOR_HSL_HUE;
|
2014-01-10 23:06:17 +04:00
|
|
|
case CompositionOp::OP_SATURATION:
|
2012-12-13 00:28:17 +04:00
|
|
|
return CAIRO_OPERATOR_HSL_SATURATION;
|
2014-01-10 23:06:17 +04:00
|
|
|
case CompositionOp::OP_COLOR:
|
2012-12-13 00:28:17 +04:00
|
|
|
return CAIRO_OPERATOR_HSL_COLOR;
|
2014-01-10 23:06:17 +04:00
|
|
|
case CompositionOp::OP_LUMINOSITY:
|
2012-12-13 00:28:17 +04:00
|
|
|
return CAIRO_OPERATOR_HSL_LUMINOSITY;
|
2014-01-10 23:06:17 +04:00
|
|
|
case CompositionOp::OP_COUNT:
|
2012-01-10 02:15:10 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return CAIRO_OPERATOR_OVER;
|
|
|
|
}
|
|
|
|
|
2013-08-21 03:56:27 +04:00
|
|
|
static inline cairo_antialias_t
|
|
|
|
GfxAntialiasToCairoAntialias(AntialiasMode antialias)
|
|
|
|
{
|
|
|
|
switch (antialias)
|
|
|
|
{
|
2014-01-10 23:06:17 +04:00
|
|
|
case AntialiasMode::NONE:
|
2013-08-21 03:56:27 +04:00
|
|
|
return CAIRO_ANTIALIAS_NONE;
|
2014-01-10 23:06:17 +04:00
|
|
|
case AntialiasMode::GRAY:
|
2013-08-21 03:56:27 +04:00
|
|
|
return CAIRO_ANTIALIAS_GRAY;
|
2014-01-10 23:06:17 +04:00
|
|
|
case AntialiasMode::SUBPIXEL:
|
2013-08-21 03:56:27 +04:00
|
|
|
return CAIRO_ANTIALIAS_SUBPIXEL;
|
2014-01-10 23:06:17 +04:00
|
|
|
case AntialiasMode::DEFAULT:
|
2013-08-21 03:56:27 +04:00
|
|
|
return CAIRO_ANTIALIAS_DEFAULT;
|
|
|
|
}
|
|
|
|
return CAIRO_ANTIALIAS_DEFAULT;
|
|
|
|
}
|
|
|
|
|
2012-01-10 02:15:10 +04:00
|
|
|
static inline cairo_filter_t
|
|
|
|
GfxFilterToCairoFilter(Filter filter)
|
|
|
|
{
|
|
|
|
switch (filter)
|
|
|
|
{
|
2014-01-10 23:06:17 +04:00
|
|
|
case Filter::GOOD:
|
2013-07-31 22:46:25 +04:00
|
|
|
return CAIRO_FILTER_GOOD;
|
2014-01-10 23:06:17 +04:00
|
|
|
case Filter::LINEAR:
|
2012-01-10 02:15:10 +04:00
|
|
|
return CAIRO_FILTER_BILINEAR;
|
2014-01-10 23:06:17 +04:00
|
|
|
case Filter::POINT:
|
2012-01-10 02:15:10 +04:00
|
|
|
return CAIRO_FILTER_NEAREST;
|
|
|
|
}
|
|
|
|
|
|
|
|
return CAIRO_FILTER_BILINEAR;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline cairo_extend_t
|
|
|
|
GfxExtendToCairoExtend(ExtendMode extend)
|
|
|
|
{
|
|
|
|
switch (extend)
|
|
|
|
{
|
2014-01-10 23:06:17 +04:00
|
|
|
case ExtendMode::CLAMP:
|
2012-01-10 02:15:10 +04:00
|
|
|
return CAIRO_EXTEND_PAD;
|
2014-01-10 23:06:17 +04:00
|
|
|
case ExtendMode::REPEAT:
|
2012-01-10 02:15:10 +04:00
|
|
|
return CAIRO_EXTEND_REPEAT;
|
2014-01-10 23:06:17 +04:00
|
|
|
case ExtendMode::REFLECT:
|
2012-01-10 02:15:10 +04:00
|
|
|
return CAIRO_EXTEND_REFLECT;
|
|
|
|
}
|
|
|
|
|
|
|
|
return CAIRO_EXTEND_PAD;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline cairo_format_t
|
|
|
|
GfxFormatToCairoFormat(SurfaceFormat format)
|
|
|
|
{
|
|
|
|
switch (format)
|
|
|
|
{
|
2014-01-10 23:06:16 +04:00
|
|
|
case SurfaceFormat::B8G8R8A8:
|
2012-01-10 02:15:10 +04:00
|
|
|
return CAIRO_FORMAT_ARGB32;
|
2014-01-10 23:06:16 +04:00
|
|
|
case SurfaceFormat::B8G8R8X8:
|
2012-01-10 02:15:10 +04:00
|
|
|
return CAIRO_FORMAT_RGB24;
|
2014-01-10 23:06:16 +04:00
|
|
|
case SurfaceFormat::A8:
|
2012-01-10 02:15:10 +04:00
|
|
|
return CAIRO_FORMAT_A8;
|
2014-01-10 23:06:16 +04:00
|
|
|
case SurfaceFormat::R5G6B5:
|
2012-08-09 00:17:04 +04:00
|
|
|
return CAIRO_FORMAT_RGB16_565;
|
2012-02-04 16:22:47 +04:00
|
|
|
default:
|
2012-08-09 00:17:04 +04:00
|
|
|
gfxWarning() << "Unknown image format";
|
2014-10-08 08:01:51 +04:00
|
|
|
MOZ_ASSERT(false, "Unknown image format");
|
2012-02-04 16:22:47 +04:00
|
|
|
return CAIRO_FORMAT_ARGB32;
|
2012-01-10 02:15:10 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline cairo_content_t
|
|
|
|
GfxFormatToCairoContent(SurfaceFormat format)
|
|
|
|
{
|
|
|
|
switch (format)
|
|
|
|
{
|
2014-01-10 23:06:16 +04:00
|
|
|
case SurfaceFormat::B8G8R8A8:
|
2012-01-10 02:15:10 +04:00
|
|
|
return CAIRO_CONTENT_COLOR_ALPHA;
|
2014-01-10 23:06:16 +04:00
|
|
|
case SurfaceFormat::B8G8R8X8:
|
|
|
|
case SurfaceFormat::R5G6B5: //fall through
|
2012-01-10 02:15:10 +04:00
|
|
|
return CAIRO_CONTENT_COLOR;
|
2014-01-10 23:06:16 +04:00
|
|
|
case SurfaceFormat::A8:
|
2012-01-10 02:15:10 +04:00
|
|
|
return CAIRO_CONTENT_ALPHA;
|
2012-02-04 16:22:47 +04:00
|
|
|
default:
|
2012-08-09 00:17:04 +04:00
|
|
|
gfxWarning() << "Unknown image format";
|
2014-10-08 08:01:51 +04:00
|
|
|
MOZ_ASSERT(false, "Unknown image format");
|
2012-02-04 16:22:47 +04:00
|
|
|
return CAIRO_CONTENT_COLOR_ALPHA;
|
2012-01-10 02:15:10 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline cairo_line_join_t
|
|
|
|
GfxLineJoinToCairoLineJoin(JoinStyle style)
|
|
|
|
{
|
|
|
|
switch (style)
|
|
|
|
{
|
2014-01-10 23:06:17 +04:00
|
|
|
case JoinStyle::BEVEL:
|
2012-01-10 02:15:10 +04:00
|
|
|
return CAIRO_LINE_JOIN_BEVEL;
|
2014-01-10 23:06:17 +04:00
|
|
|
case JoinStyle::ROUND:
|
2012-01-10 02:15:10 +04:00
|
|
|
return CAIRO_LINE_JOIN_ROUND;
|
2014-01-10 23:06:17 +04:00
|
|
|
case JoinStyle::MITER:
|
2012-01-10 02:15:10 +04:00
|
|
|
return CAIRO_LINE_JOIN_MITER;
|
2014-01-10 23:06:17 +04:00
|
|
|
case JoinStyle::MITER_OR_BEVEL:
|
2012-01-10 02:15:10 +04:00
|
|
|
return CAIRO_LINE_JOIN_MITER;
|
|
|
|
}
|
|
|
|
|
|
|
|
return CAIRO_LINE_JOIN_MITER;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline cairo_line_cap_t
|
|
|
|
GfxLineCapToCairoLineCap(CapStyle style)
|
|
|
|
{
|
|
|
|
switch (style)
|
|
|
|
{
|
2014-01-10 23:06:17 +04:00
|
|
|
case CapStyle::BUTT:
|
2012-01-10 02:15:10 +04:00
|
|
|
return CAIRO_LINE_CAP_BUTT;
|
2014-01-10 23:06:17 +04:00
|
|
|
case CapStyle::ROUND:
|
2012-01-10 02:15:10 +04:00
|
|
|
return CAIRO_LINE_CAP_ROUND;
|
2014-01-10 23:06:17 +04:00
|
|
|
case CapStyle::SQUARE:
|
2012-01-10 02:15:10 +04:00
|
|
|
return CAIRO_LINE_CAP_SQUARE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return CAIRO_LINE_CAP_BUTT;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline SurfaceFormat
|
|
|
|
CairoContentToGfxFormat(cairo_content_t content)
|
|
|
|
{
|
|
|
|
switch (content)
|
|
|
|
{
|
|
|
|
case CAIRO_CONTENT_COLOR_ALPHA:
|
2014-01-10 23:06:16 +04:00
|
|
|
return SurfaceFormat::B8G8R8A8;
|
2012-01-10 02:15:10 +04:00
|
|
|
case CAIRO_CONTENT_COLOR:
|
2012-08-09 00:17:04 +04:00
|
|
|
// BEWARE! format may be 565
|
2014-01-10 23:06:16 +04:00
|
|
|
return SurfaceFormat::B8G8R8X8;
|
2012-01-10 02:15:10 +04:00
|
|
|
case CAIRO_CONTENT_ALPHA:
|
2014-01-10 23:06:16 +04:00
|
|
|
return SurfaceFormat::A8;
|
2012-01-10 02:15:10 +04:00
|
|
|
}
|
|
|
|
|
2014-01-10 23:06:16 +04:00
|
|
|
return SurfaceFormat::B8G8R8A8;
|
2012-01-10 02:15:10 +04:00
|
|
|
}
|
|
|
|
|
2014-12-18 00:49:57 +03:00
|
|
|
static inline SurfaceFormat
|
|
|
|
CairoFormatToGfxFormat(cairo_format_t format)
|
|
|
|
{
|
|
|
|
switch (format) {
|
|
|
|
case CAIRO_FORMAT_ARGB32:
|
|
|
|
return SurfaceFormat::B8G8R8A8;
|
|
|
|
case CAIRO_FORMAT_RGB24:
|
|
|
|
return SurfaceFormat::B8G8R8X8;
|
|
|
|
case CAIRO_FORMAT_A8:
|
|
|
|
return SurfaceFormat::A8;
|
|
|
|
case CAIRO_FORMAT_RGB16_565:
|
|
|
|
return SurfaceFormat::R5G6B5;
|
|
|
|
default:
|
2015-03-09 16:34:45 +03:00
|
|
|
gfxCriticalError() << "Unknown cairo format " << format;
|
2014-12-18 00:49:57 +03:00
|
|
|
return SurfaceFormat::UNKNOWN;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline SurfaceFormat
|
|
|
|
GfxFormatForCairoSurface(cairo_surface_t* surface)
|
|
|
|
{
|
|
|
|
if (cairo_surface_get_type(surface) == CAIRO_SURFACE_TYPE_IMAGE) {
|
|
|
|
return CairoFormatToGfxFormat(cairo_image_surface_get_format(surface));
|
|
|
|
}
|
|
|
|
|
|
|
|
return CairoContentToGfxFormat(cairo_surface_get_content(surface));
|
|
|
|
}
|
|
|
|
|
2012-01-10 02:15:10 +04:00
|
|
|
static inline void
|
|
|
|
GfxMatrixToCairoMatrix(const Matrix& mat, cairo_matrix_t& retval)
|
|
|
|
{
|
|
|
|
cairo_matrix_init(&retval, mat._11, mat._12, mat._21, mat._22, mat._31, mat._32);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
SetCairoStrokeOptions(cairo_t* aCtx, const StrokeOptions& aStrokeOptions)
|
|
|
|
{
|
|
|
|
cairo_set_line_width(aCtx, aStrokeOptions.mLineWidth);
|
|
|
|
|
|
|
|
cairo_set_miter_limit(aCtx, aStrokeOptions.mMiterLimit);
|
|
|
|
|
|
|
|
if (aStrokeOptions.mDashPattern) {
|
|
|
|
// Convert array of floats to array of doubles
|
|
|
|
std::vector<double> dashes(aStrokeOptions.mDashLength);
|
|
|
|
for (size_t i = 0; i < aStrokeOptions.mDashLength; ++i) {
|
|
|
|
dashes[i] = aStrokeOptions.mDashPattern[i];
|
|
|
|
}
|
|
|
|
cairo_set_dash(aCtx, &dashes[0], aStrokeOptions.mDashLength,
|
|
|
|
aStrokeOptions.mDashOffset);
|
|
|
|
}
|
|
|
|
|
|
|
|
cairo_set_line_join(aCtx, GfxLineJoinToCairoLineJoin(aStrokeOptions.mLineJoin));
|
|
|
|
|
|
|
|
cairo_set_line_cap(aCtx, GfxLineCapToCairoLineCap(aStrokeOptions.mLineCap));
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline cairo_fill_rule_t
|
|
|
|
GfxFillRuleToCairoFillRule(FillRule rule)
|
|
|
|
{
|
|
|
|
switch (rule)
|
|
|
|
{
|
2014-01-10 23:06:17 +04:00
|
|
|
case FillRule::FILL_WINDING:
|
2012-01-10 02:15:10 +04:00
|
|
|
return CAIRO_FILL_RULE_WINDING;
|
2014-01-10 23:06:17 +04:00
|
|
|
case FillRule::FILL_EVEN_ODD:
|
2012-01-10 02:15:10 +04:00
|
|
|
return CAIRO_FILL_RULE_EVEN_ODD;
|
|
|
|
}
|
|
|
|
|
|
|
|
return CAIRO_FILL_RULE_WINDING;
|
|
|
|
}
|
|
|
|
|
2012-09-04 01:47:09 +04:00
|
|
|
// RAII class for temporarily changing the cairo matrix transform. It will use
|
|
|
|
// the given matrix transform while it is in scope. When it goes out of scope
|
|
|
|
// it will put the cairo context back the way it was.
|
|
|
|
|
|
|
|
class CairoTempMatrix
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
CairoTempMatrix(cairo_t* aCtx, const Matrix& aMatrix)
|
|
|
|
: mCtx(aCtx)
|
|
|
|
{
|
|
|
|
cairo_get_matrix(aCtx, &mSaveMatrix);
|
|
|
|
cairo_matrix_t matrix;
|
|
|
|
GfxMatrixToCairoMatrix(aMatrix, matrix);
|
|
|
|
cairo_set_matrix(aCtx, &matrix);
|
|
|
|
}
|
|
|
|
|
|
|
|
~CairoTempMatrix()
|
|
|
|
{
|
2012-10-02 06:12:13 +04:00
|
|
|
cairo_set_matrix(mCtx, &mSaveMatrix);
|
2012-09-04 01:47:09 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
cairo_t* mCtx;
|
|
|
|
cairo_matrix_t mSaveMatrix;
|
|
|
|
};
|
|
|
|
|
2012-01-10 02:15:10 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* MOZILLA_GFX_HELPERSCAIRO_H_ */
|