gfx/2d/HelpersCairo.h

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

michael@0 1 /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
michael@0 2 * This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 #ifndef MOZILLA_GFX_HELPERSCAIRO_H_
michael@0 7 #define MOZILLA_GFX_HELPERSCAIRO_H_
michael@0 8
michael@0 9 #include "2D.h"
michael@0 10 #include "cairo.h"
michael@0 11 #include "Logging.h"
michael@0 12
michael@0 13 namespace mozilla {
michael@0 14 namespace gfx {
michael@0 15
michael@0 16 static inline cairo_operator_t
michael@0 17 GfxOpToCairoOp(CompositionOp op)
michael@0 18 {
michael@0 19 switch (op)
michael@0 20 {
michael@0 21 case CompositionOp::OP_OVER:
michael@0 22 return CAIRO_OPERATOR_OVER;
michael@0 23 case CompositionOp::OP_ADD:
michael@0 24 return CAIRO_OPERATOR_ADD;
michael@0 25 case CompositionOp::OP_ATOP:
michael@0 26 return CAIRO_OPERATOR_ATOP;
michael@0 27 case CompositionOp::OP_OUT:
michael@0 28 return CAIRO_OPERATOR_OUT;
michael@0 29 case CompositionOp::OP_IN:
michael@0 30 return CAIRO_OPERATOR_IN;
michael@0 31 case CompositionOp::OP_SOURCE:
michael@0 32 return CAIRO_OPERATOR_SOURCE;
michael@0 33 case CompositionOp::OP_DEST_IN:
michael@0 34 return CAIRO_OPERATOR_DEST_IN;
michael@0 35 case CompositionOp::OP_DEST_OUT:
michael@0 36 return CAIRO_OPERATOR_DEST_OUT;
michael@0 37 case CompositionOp::OP_DEST_OVER:
michael@0 38 return CAIRO_OPERATOR_DEST_OVER;
michael@0 39 case CompositionOp::OP_DEST_ATOP:
michael@0 40 return CAIRO_OPERATOR_DEST_ATOP;
michael@0 41 case CompositionOp::OP_XOR:
michael@0 42 return CAIRO_OPERATOR_XOR;
michael@0 43 case CompositionOp::OP_MULTIPLY:
michael@0 44 return CAIRO_OPERATOR_MULTIPLY;
michael@0 45 case CompositionOp::OP_SCREEN:
michael@0 46 return CAIRO_OPERATOR_SCREEN;
michael@0 47 case CompositionOp::OP_OVERLAY:
michael@0 48 return CAIRO_OPERATOR_OVERLAY;
michael@0 49 case CompositionOp::OP_DARKEN:
michael@0 50 return CAIRO_OPERATOR_DARKEN;
michael@0 51 case CompositionOp::OP_LIGHTEN:
michael@0 52 return CAIRO_OPERATOR_LIGHTEN;
michael@0 53 case CompositionOp::OP_COLOR_DODGE:
michael@0 54 return CAIRO_OPERATOR_COLOR_DODGE;
michael@0 55 case CompositionOp::OP_COLOR_BURN:
michael@0 56 return CAIRO_OPERATOR_COLOR_BURN;
michael@0 57 case CompositionOp::OP_HARD_LIGHT:
michael@0 58 return CAIRO_OPERATOR_HARD_LIGHT;
michael@0 59 case CompositionOp::OP_SOFT_LIGHT:
michael@0 60 return CAIRO_OPERATOR_SOFT_LIGHT;
michael@0 61 case CompositionOp::OP_DIFFERENCE:
michael@0 62 return CAIRO_OPERATOR_DIFFERENCE;
michael@0 63 case CompositionOp::OP_EXCLUSION:
michael@0 64 return CAIRO_OPERATOR_EXCLUSION;
michael@0 65 case CompositionOp::OP_HUE:
michael@0 66 return CAIRO_OPERATOR_HSL_HUE;
michael@0 67 case CompositionOp::OP_SATURATION:
michael@0 68 return CAIRO_OPERATOR_HSL_SATURATION;
michael@0 69 case CompositionOp::OP_COLOR:
michael@0 70 return CAIRO_OPERATOR_HSL_COLOR;
michael@0 71 case CompositionOp::OP_LUMINOSITY:
michael@0 72 return CAIRO_OPERATOR_HSL_LUMINOSITY;
michael@0 73 case CompositionOp::OP_COUNT:
michael@0 74 break;
michael@0 75 }
michael@0 76
michael@0 77 return CAIRO_OPERATOR_OVER;
michael@0 78 }
michael@0 79
michael@0 80 static inline cairo_antialias_t
michael@0 81 GfxAntialiasToCairoAntialias(AntialiasMode antialias)
michael@0 82 {
michael@0 83 switch (antialias)
michael@0 84 {
michael@0 85 case AntialiasMode::NONE:
michael@0 86 return CAIRO_ANTIALIAS_NONE;
michael@0 87 case AntialiasMode::GRAY:
michael@0 88 return CAIRO_ANTIALIAS_GRAY;
michael@0 89 case AntialiasMode::SUBPIXEL:
michael@0 90 return CAIRO_ANTIALIAS_SUBPIXEL;
michael@0 91 case AntialiasMode::DEFAULT:
michael@0 92 return CAIRO_ANTIALIAS_DEFAULT;
michael@0 93 }
michael@0 94 return CAIRO_ANTIALIAS_DEFAULT;
michael@0 95 }
michael@0 96
michael@0 97 static inline cairo_filter_t
michael@0 98 GfxFilterToCairoFilter(Filter filter)
michael@0 99 {
michael@0 100 switch (filter)
michael@0 101 {
michael@0 102 case Filter::GOOD:
michael@0 103 return CAIRO_FILTER_GOOD;
michael@0 104 case Filter::LINEAR:
michael@0 105 return CAIRO_FILTER_BILINEAR;
michael@0 106 case Filter::POINT:
michael@0 107 return CAIRO_FILTER_NEAREST;
michael@0 108 }
michael@0 109
michael@0 110 return CAIRO_FILTER_BILINEAR;
michael@0 111 }
michael@0 112
michael@0 113 static inline cairo_extend_t
michael@0 114 GfxExtendToCairoExtend(ExtendMode extend)
michael@0 115 {
michael@0 116 switch (extend)
michael@0 117 {
michael@0 118 case ExtendMode::CLAMP:
michael@0 119 return CAIRO_EXTEND_PAD;
michael@0 120 case ExtendMode::REPEAT:
michael@0 121 return CAIRO_EXTEND_REPEAT;
michael@0 122 case ExtendMode::REFLECT:
michael@0 123 return CAIRO_EXTEND_REFLECT;
michael@0 124 }
michael@0 125
michael@0 126 return CAIRO_EXTEND_PAD;
michael@0 127 }
michael@0 128
michael@0 129 static inline cairo_format_t
michael@0 130 GfxFormatToCairoFormat(SurfaceFormat format)
michael@0 131 {
michael@0 132 switch (format)
michael@0 133 {
michael@0 134 case SurfaceFormat::B8G8R8A8:
michael@0 135 return CAIRO_FORMAT_ARGB32;
michael@0 136 case SurfaceFormat::B8G8R8X8:
michael@0 137 return CAIRO_FORMAT_RGB24;
michael@0 138 case SurfaceFormat::A8:
michael@0 139 return CAIRO_FORMAT_A8;
michael@0 140 case SurfaceFormat::R5G6B5:
michael@0 141 return CAIRO_FORMAT_RGB16_565;
michael@0 142 default:
michael@0 143 gfxWarning() << "Unknown image format";
michael@0 144 return CAIRO_FORMAT_ARGB32;
michael@0 145 }
michael@0 146 }
michael@0 147
michael@0 148 static inline cairo_content_t
michael@0 149 GfxFormatToCairoContent(SurfaceFormat format)
michael@0 150 {
michael@0 151 switch (format)
michael@0 152 {
michael@0 153 case SurfaceFormat::B8G8R8A8:
michael@0 154 return CAIRO_CONTENT_COLOR_ALPHA;
michael@0 155 case SurfaceFormat::B8G8R8X8:
michael@0 156 case SurfaceFormat::R5G6B5: //fall through
michael@0 157 return CAIRO_CONTENT_COLOR;
michael@0 158 case SurfaceFormat::A8:
michael@0 159 return CAIRO_CONTENT_ALPHA;
michael@0 160 default:
michael@0 161 gfxWarning() << "Unknown image format";
michael@0 162 return CAIRO_CONTENT_COLOR_ALPHA;
michael@0 163 }
michael@0 164 }
michael@0 165
michael@0 166 static inline cairo_line_join_t
michael@0 167 GfxLineJoinToCairoLineJoin(JoinStyle style)
michael@0 168 {
michael@0 169 switch (style)
michael@0 170 {
michael@0 171 case JoinStyle::BEVEL:
michael@0 172 return CAIRO_LINE_JOIN_BEVEL;
michael@0 173 case JoinStyle::ROUND:
michael@0 174 return CAIRO_LINE_JOIN_ROUND;
michael@0 175 case JoinStyle::MITER:
michael@0 176 return CAIRO_LINE_JOIN_MITER;
michael@0 177 case JoinStyle::MITER_OR_BEVEL:
michael@0 178 return CAIRO_LINE_JOIN_MITER;
michael@0 179 }
michael@0 180
michael@0 181 return CAIRO_LINE_JOIN_MITER;
michael@0 182 }
michael@0 183
michael@0 184 static inline cairo_line_cap_t
michael@0 185 GfxLineCapToCairoLineCap(CapStyle style)
michael@0 186 {
michael@0 187 switch (style)
michael@0 188 {
michael@0 189 case CapStyle::BUTT:
michael@0 190 return CAIRO_LINE_CAP_BUTT;
michael@0 191 case CapStyle::ROUND:
michael@0 192 return CAIRO_LINE_CAP_ROUND;
michael@0 193 case CapStyle::SQUARE:
michael@0 194 return CAIRO_LINE_CAP_SQUARE;
michael@0 195 }
michael@0 196
michael@0 197 return CAIRO_LINE_CAP_BUTT;
michael@0 198 }
michael@0 199
michael@0 200 static inline SurfaceFormat
michael@0 201 CairoContentToGfxFormat(cairo_content_t content)
michael@0 202 {
michael@0 203 switch (content)
michael@0 204 {
michael@0 205 case CAIRO_CONTENT_COLOR_ALPHA:
michael@0 206 return SurfaceFormat::B8G8R8A8;
michael@0 207 case CAIRO_CONTENT_COLOR:
michael@0 208 // BEWARE! format may be 565
michael@0 209 return SurfaceFormat::B8G8R8X8;
michael@0 210 case CAIRO_CONTENT_ALPHA:
michael@0 211 return SurfaceFormat::A8;
michael@0 212 }
michael@0 213
michael@0 214 return SurfaceFormat::B8G8R8A8;
michael@0 215 }
michael@0 216
michael@0 217 static inline void
michael@0 218 GfxMatrixToCairoMatrix(const Matrix& mat, cairo_matrix_t& retval)
michael@0 219 {
michael@0 220 cairo_matrix_init(&retval, mat._11, mat._12, mat._21, mat._22, mat._31, mat._32);
michael@0 221 }
michael@0 222
michael@0 223 static inline void
michael@0 224 SetCairoStrokeOptions(cairo_t* aCtx, const StrokeOptions& aStrokeOptions)
michael@0 225 {
michael@0 226 cairo_set_line_width(aCtx, aStrokeOptions.mLineWidth);
michael@0 227
michael@0 228 cairo_set_miter_limit(aCtx, aStrokeOptions.mMiterLimit);
michael@0 229
michael@0 230 if (aStrokeOptions.mDashPattern) {
michael@0 231 // Convert array of floats to array of doubles
michael@0 232 std::vector<double> dashes(aStrokeOptions.mDashLength);
michael@0 233 for (size_t i = 0; i < aStrokeOptions.mDashLength; ++i) {
michael@0 234 dashes[i] = aStrokeOptions.mDashPattern[i];
michael@0 235 }
michael@0 236 cairo_set_dash(aCtx, &dashes[0], aStrokeOptions.mDashLength,
michael@0 237 aStrokeOptions.mDashOffset);
michael@0 238 }
michael@0 239
michael@0 240 cairo_set_line_join(aCtx, GfxLineJoinToCairoLineJoin(aStrokeOptions.mLineJoin));
michael@0 241
michael@0 242 cairo_set_line_cap(aCtx, GfxLineCapToCairoLineCap(aStrokeOptions.mLineCap));
michael@0 243 }
michael@0 244
michael@0 245 static inline cairo_fill_rule_t
michael@0 246 GfxFillRuleToCairoFillRule(FillRule rule)
michael@0 247 {
michael@0 248 switch (rule)
michael@0 249 {
michael@0 250 case FillRule::FILL_WINDING:
michael@0 251 return CAIRO_FILL_RULE_WINDING;
michael@0 252 case FillRule::FILL_EVEN_ODD:
michael@0 253 return CAIRO_FILL_RULE_EVEN_ODD;
michael@0 254 }
michael@0 255
michael@0 256 return CAIRO_FILL_RULE_WINDING;
michael@0 257 }
michael@0 258
michael@0 259 // RAII class for temporarily changing the cairo matrix transform. It will use
michael@0 260 // the given matrix transform while it is in scope. When it goes out of scope
michael@0 261 // it will put the cairo context back the way it was.
michael@0 262
michael@0 263 class CairoTempMatrix
michael@0 264 {
michael@0 265 public:
michael@0 266 CairoTempMatrix(cairo_t* aCtx, const Matrix& aMatrix)
michael@0 267 : mCtx(aCtx)
michael@0 268 {
michael@0 269 cairo_get_matrix(aCtx, &mSaveMatrix);
michael@0 270 cairo_matrix_t matrix;
michael@0 271 GfxMatrixToCairoMatrix(aMatrix, matrix);
michael@0 272 cairo_set_matrix(aCtx, &matrix);
michael@0 273 }
michael@0 274
michael@0 275 ~CairoTempMatrix()
michael@0 276 {
michael@0 277 cairo_set_matrix(mCtx, &mSaveMatrix);
michael@0 278 }
michael@0 279
michael@0 280 private:
michael@0 281 cairo_t* mCtx;
michael@0 282 cairo_matrix_t mSaveMatrix;
michael@0 283 };
michael@0 284
michael@0 285 }
michael@0 286 }
michael@0 287
michael@0 288 #endif /* MOZILLA_GFX_HELPERSCAIRO_H_ */

mercurial