Tue, 06 Jan 2015 21:39:09 +0100
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_HELPERSSKIA_H_ |
michael@0 | 7 | #define MOZILLA_GFX_HELPERSSKIA_H_ |
michael@0 | 8 | |
michael@0 | 9 | #include "2D.h" |
michael@0 | 10 | #include "skia/SkCanvas.h" |
michael@0 | 11 | #include "skia/SkDashPathEffect.h" |
michael@0 | 12 | #include "skia/SkShader.h" |
michael@0 | 13 | #ifdef USE_SKIA_GPU |
michael@0 | 14 | #include "skia/GrTypes.h" |
michael@0 | 15 | #endif |
michael@0 | 16 | #include "mozilla/Assertions.h" |
michael@0 | 17 | #include <vector> |
michael@0 | 18 | |
michael@0 | 19 | namespace mozilla { |
michael@0 | 20 | namespace gfx { |
michael@0 | 21 | |
michael@0 | 22 | static inline SkBitmap::Config |
michael@0 | 23 | GfxFormatToSkiaConfig(SurfaceFormat format) |
michael@0 | 24 | { |
michael@0 | 25 | switch (format) |
michael@0 | 26 | { |
michael@0 | 27 | case SurfaceFormat::B8G8R8A8: |
michael@0 | 28 | return SkBitmap::kARGB_8888_Config; |
michael@0 | 29 | case SurfaceFormat::B8G8R8X8: |
michael@0 | 30 | // We probably need to do something here. |
michael@0 | 31 | return SkBitmap::kARGB_8888_Config; |
michael@0 | 32 | case SurfaceFormat::R5G6B5: |
michael@0 | 33 | return SkBitmap::kRGB_565_Config; |
michael@0 | 34 | case SurfaceFormat::A8: |
michael@0 | 35 | return SkBitmap::kA8_Config; |
michael@0 | 36 | default: |
michael@0 | 37 | return SkBitmap::kARGB_8888_Config; |
michael@0 | 38 | } |
michael@0 | 39 | } |
michael@0 | 40 | |
michael@0 | 41 | static inline SkColorType |
michael@0 | 42 | GfxFormatToSkiaColorType(SurfaceFormat format) |
michael@0 | 43 | { |
michael@0 | 44 | switch (format) |
michael@0 | 45 | { |
michael@0 | 46 | case SurfaceFormat::B8G8R8A8: |
michael@0 | 47 | return kBGRA_8888_SkColorType; |
michael@0 | 48 | case SurfaceFormat::B8G8R8X8: |
michael@0 | 49 | // We probably need to do something here. |
michael@0 | 50 | return kBGRA_8888_SkColorType; |
michael@0 | 51 | case SurfaceFormat::R5G6B5: |
michael@0 | 52 | return kRGB_565_SkColorType; |
michael@0 | 53 | case SurfaceFormat::A8: |
michael@0 | 54 | return kAlpha_8_SkColorType; |
michael@0 | 55 | default: |
michael@0 | 56 | return kRGBA_8888_SkColorType; |
michael@0 | 57 | } |
michael@0 | 58 | } |
michael@0 | 59 | |
michael@0 | 60 | static inline SurfaceFormat |
michael@0 | 61 | SkiaConfigToGfxFormat(SkBitmap::Config config) |
michael@0 | 62 | { |
michael@0 | 63 | switch (config) |
michael@0 | 64 | { |
michael@0 | 65 | case SkBitmap::kARGB_8888_Config: |
michael@0 | 66 | return SurfaceFormat::B8G8R8A8; |
michael@0 | 67 | case SkBitmap::kRGB_565_Config: |
michael@0 | 68 | return SurfaceFormat::R5G6B5; |
michael@0 | 69 | case SkBitmap::kA8_Config: |
michael@0 | 70 | return SurfaceFormat::A8; |
michael@0 | 71 | default: |
michael@0 | 72 | return SurfaceFormat::B8G8R8A8; |
michael@0 | 73 | } |
michael@0 | 74 | } |
michael@0 | 75 | |
michael@0 | 76 | #ifdef USE_SKIA_GPU |
michael@0 | 77 | static inline GrPixelConfig |
michael@0 | 78 | GfxFormatToGrConfig(SurfaceFormat format) |
michael@0 | 79 | { |
michael@0 | 80 | switch (format) |
michael@0 | 81 | { |
michael@0 | 82 | case SurfaceFormat::B8G8R8A8: |
michael@0 | 83 | return kBGRA_8888_GrPixelConfig; |
michael@0 | 84 | case SurfaceFormat::B8G8R8X8: |
michael@0 | 85 | // We probably need to do something here. |
michael@0 | 86 | return kBGRA_8888_GrPixelConfig; |
michael@0 | 87 | case SurfaceFormat::R5G6B5: |
michael@0 | 88 | return kRGB_565_GrPixelConfig; |
michael@0 | 89 | case SurfaceFormat::A8: |
michael@0 | 90 | return kAlpha_8_GrPixelConfig; |
michael@0 | 91 | default: |
michael@0 | 92 | return kRGBA_8888_GrPixelConfig; |
michael@0 | 93 | } |
michael@0 | 94 | |
michael@0 | 95 | } |
michael@0 | 96 | #endif |
michael@0 | 97 | static inline void |
michael@0 | 98 | GfxMatrixToSkiaMatrix(const Matrix& mat, SkMatrix& retval) |
michael@0 | 99 | { |
michael@0 | 100 | retval.setAll(SkFloatToScalar(mat._11), SkFloatToScalar(mat._21), SkFloatToScalar(mat._31), |
michael@0 | 101 | SkFloatToScalar(mat._12), SkFloatToScalar(mat._22), SkFloatToScalar(mat._32), |
michael@0 | 102 | 0, 0, SK_Scalar1); |
michael@0 | 103 | } |
michael@0 | 104 | |
michael@0 | 105 | static inline SkPaint::Cap |
michael@0 | 106 | CapStyleToSkiaCap(CapStyle aCap) |
michael@0 | 107 | { |
michael@0 | 108 | switch (aCap) |
michael@0 | 109 | { |
michael@0 | 110 | case CapStyle::BUTT: |
michael@0 | 111 | return SkPaint::kButt_Cap; |
michael@0 | 112 | case CapStyle::ROUND: |
michael@0 | 113 | return SkPaint::kRound_Cap; |
michael@0 | 114 | case CapStyle::SQUARE: |
michael@0 | 115 | return SkPaint::kSquare_Cap; |
michael@0 | 116 | } |
michael@0 | 117 | return SkPaint::kDefault_Cap; |
michael@0 | 118 | } |
michael@0 | 119 | |
michael@0 | 120 | static inline SkPaint::Join |
michael@0 | 121 | JoinStyleToSkiaJoin(JoinStyle aJoin) |
michael@0 | 122 | { |
michael@0 | 123 | switch (aJoin) |
michael@0 | 124 | { |
michael@0 | 125 | case JoinStyle::BEVEL: |
michael@0 | 126 | return SkPaint::kBevel_Join; |
michael@0 | 127 | case JoinStyle::ROUND: |
michael@0 | 128 | return SkPaint::kRound_Join; |
michael@0 | 129 | case JoinStyle::MITER: |
michael@0 | 130 | case JoinStyle::MITER_OR_BEVEL: |
michael@0 | 131 | return SkPaint::kMiter_Join; |
michael@0 | 132 | } |
michael@0 | 133 | return SkPaint::kDefault_Join; |
michael@0 | 134 | } |
michael@0 | 135 | |
michael@0 | 136 | static inline bool |
michael@0 | 137 | StrokeOptionsToPaint(SkPaint& aPaint, const StrokeOptions &aOptions) |
michael@0 | 138 | { |
michael@0 | 139 | // Skia renders 0 width strokes with a width of 1 (and in black), |
michael@0 | 140 | // so we should just skip the draw call entirely. |
michael@0 | 141 | if (!aOptions.mLineWidth) { |
michael@0 | 142 | return false; |
michael@0 | 143 | } |
michael@0 | 144 | aPaint.setStrokeWidth(SkFloatToScalar(aOptions.mLineWidth)); |
michael@0 | 145 | aPaint.setStrokeMiter(SkFloatToScalar(aOptions.mMiterLimit)); |
michael@0 | 146 | aPaint.setStrokeCap(CapStyleToSkiaCap(aOptions.mLineCap)); |
michael@0 | 147 | aPaint.setStrokeJoin(JoinStyleToSkiaJoin(aOptions.mLineJoin)); |
michael@0 | 148 | |
michael@0 | 149 | if (aOptions.mDashLength > 0) { |
michael@0 | 150 | // Skia only supports dash arrays that are multiples of 2. |
michael@0 | 151 | uint32_t dashCount; |
michael@0 | 152 | |
michael@0 | 153 | if (aOptions.mDashLength % 2 == 0) { |
michael@0 | 154 | dashCount = aOptions.mDashLength; |
michael@0 | 155 | } else { |
michael@0 | 156 | dashCount = aOptions.mDashLength * 2; |
michael@0 | 157 | } |
michael@0 | 158 | |
michael@0 | 159 | std::vector<SkScalar> pattern; |
michael@0 | 160 | pattern.resize(dashCount); |
michael@0 | 161 | |
michael@0 | 162 | for (uint32_t i = 0; i < dashCount; i++) { |
michael@0 | 163 | pattern[i] = SkFloatToScalar(aOptions.mDashPattern[i % aOptions.mDashLength]); |
michael@0 | 164 | } |
michael@0 | 165 | |
michael@0 | 166 | SkDashPathEffect* dash = SkDashPathEffect::Create(&pattern.front(), |
michael@0 | 167 | dashCount, |
michael@0 | 168 | SkFloatToScalar(aOptions.mDashOffset)); |
michael@0 | 169 | SkSafeUnref(aPaint.setPathEffect(dash)); |
michael@0 | 170 | } |
michael@0 | 171 | |
michael@0 | 172 | aPaint.setStyle(SkPaint::kStroke_Style); |
michael@0 | 173 | return true; |
michael@0 | 174 | } |
michael@0 | 175 | |
michael@0 | 176 | static inline SkXfermode::Mode |
michael@0 | 177 | GfxOpToSkiaOp(CompositionOp op) |
michael@0 | 178 | { |
michael@0 | 179 | switch (op) |
michael@0 | 180 | { |
michael@0 | 181 | case CompositionOp::OP_OVER: |
michael@0 | 182 | return SkXfermode::kSrcOver_Mode; |
michael@0 | 183 | case CompositionOp::OP_ADD: |
michael@0 | 184 | return SkXfermode::kPlus_Mode; |
michael@0 | 185 | case CompositionOp::OP_ATOP: |
michael@0 | 186 | return SkXfermode::kSrcATop_Mode; |
michael@0 | 187 | case CompositionOp::OP_OUT: |
michael@0 | 188 | return SkXfermode::kSrcOut_Mode; |
michael@0 | 189 | case CompositionOp::OP_IN: |
michael@0 | 190 | return SkXfermode::kSrcIn_Mode; |
michael@0 | 191 | case CompositionOp::OP_SOURCE: |
michael@0 | 192 | return SkXfermode::kSrc_Mode; |
michael@0 | 193 | case CompositionOp::OP_DEST_IN: |
michael@0 | 194 | return SkXfermode::kDstIn_Mode; |
michael@0 | 195 | case CompositionOp::OP_DEST_OUT: |
michael@0 | 196 | return SkXfermode::kDstOut_Mode; |
michael@0 | 197 | case CompositionOp::OP_DEST_OVER: |
michael@0 | 198 | return SkXfermode::kDstOver_Mode; |
michael@0 | 199 | case CompositionOp::OP_DEST_ATOP: |
michael@0 | 200 | return SkXfermode::kDstATop_Mode; |
michael@0 | 201 | case CompositionOp::OP_XOR: |
michael@0 | 202 | return SkXfermode::kXor_Mode; |
michael@0 | 203 | case CompositionOp::OP_MULTIPLY: |
michael@0 | 204 | return SkXfermode::kMultiply_Mode; |
michael@0 | 205 | case CompositionOp::OP_SCREEN: |
michael@0 | 206 | return SkXfermode::kScreen_Mode; |
michael@0 | 207 | case CompositionOp::OP_OVERLAY: |
michael@0 | 208 | return SkXfermode::kOverlay_Mode; |
michael@0 | 209 | case CompositionOp::OP_DARKEN: |
michael@0 | 210 | return SkXfermode::kDarken_Mode; |
michael@0 | 211 | case CompositionOp::OP_LIGHTEN: |
michael@0 | 212 | return SkXfermode::kLighten_Mode; |
michael@0 | 213 | case CompositionOp::OP_COLOR_DODGE: |
michael@0 | 214 | return SkXfermode::kColorDodge_Mode; |
michael@0 | 215 | case CompositionOp::OP_COLOR_BURN: |
michael@0 | 216 | return SkXfermode::kColorBurn_Mode; |
michael@0 | 217 | case CompositionOp::OP_HARD_LIGHT: |
michael@0 | 218 | return SkXfermode::kHardLight_Mode; |
michael@0 | 219 | case CompositionOp::OP_SOFT_LIGHT: |
michael@0 | 220 | return SkXfermode::kSoftLight_Mode; |
michael@0 | 221 | case CompositionOp::OP_DIFFERENCE: |
michael@0 | 222 | return SkXfermode::kDifference_Mode; |
michael@0 | 223 | case CompositionOp::OP_EXCLUSION: |
michael@0 | 224 | return SkXfermode::kExclusion_Mode; |
michael@0 | 225 | case CompositionOp::OP_HUE: |
michael@0 | 226 | return SkXfermode::kHue_Mode; |
michael@0 | 227 | case CompositionOp::OP_SATURATION: |
michael@0 | 228 | return SkXfermode::kSaturation_Mode; |
michael@0 | 229 | case CompositionOp::OP_COLOR: |
michael@0 | 230 | return SkXfermode::kColor_Mode; |
michael@0 | 231 | case CompositionOp::OP_LUMINOSITY: |
michael@0 | 232 | return SkXfermode::kLuminosity_Mode; |
michael@0 | 233 | default: |
michael@0 | 234 | return SkXfermode::kSrcOver_Mode; |
michael@0 | 235 | } |
michael@0 | 236 | } |
michael@0 | 237 | |
michael@0 | 238 | static inline SkColor ColorToSkColor(const Color &color, Float aAlpha) |
michael@0 | 239 | { |
michael@0 | 240 | //XXX: do a better job converting to int |
michael@0 | 241 | return SkColorSetARGB(U8CPU(color.a*aAlpha*255.0), U8CPU(color.r*255.0), |
michael@0 | 242 | U8CPU(color.g*255.0), U8CPU(color.b*255.0)); |
michael@0 | 243 | } |
michael@0 | 244 | |
michael@0 | 245 | static inline SkRect |
michael@0 | 246 | RectToSkRect(const Rect& aRect) |
michael@0 | 247 | { |
michael@0 | 248 | return SkRect::MakeXYWH(SkFloatToScalar(aRect.x), SkFloatToScalar(aRect.y), |
michael@0 | 249 | SkFloatToScalar(aRect.width), SkFloatToScalar(aRect.height)); |
michael@0 | 250 | } |
michael@0 | 251 | |
michael@0 | 252 | static inline SkRect |
michael@0 | 253 | IntRectToSkRect(const IntRect& aRect) |
michael@0 | 254 | { |
michael@0 | 255 | return SkRect::MakeXYWH(SkIntToScalar(aRect.x), SkIntToScalar(aRect.y), |
michael@0 | 256 | SkIntToScalar(aRect.width), SkIntToScalar(aRect.height)); |
michael@0 | 257 | } |
michael@0 | 258 | |
michael@0 | 259 | static inline SkIRect |
michael@0 | 260 | RectToSkIRect(const Rect& aRect) |
michael@0 | 261 | { |
michael@0 | 262 | return SkIRect::MakeXYWH(int32_t(aRect.x), int32_t(aRect.y), |
michael@0 | 263 | int32_t(aRect.width), int32_t(aRect.height)); |
michael@0 | 264 | } |
michael@0 | 265 | |
michael@0 | 266 | static inline SkIRect |
michael@0 | 267 | IntRectToSkIRect(const IntRect& aRect) |
michael@0 | 268 | { |
michael@0 | 269 | return SkIRect::MakeXYWH(aRect.x, aRect.y, aRect.width, aRect.height); |
michael@0 | 270 | } |
michael@0 | 271 | |
michael@0 | 272 | static inline Point |
michael@0 | 273 | SkPointToPoint(const SkPoint &aPoint) |
michael@0 | 274 | { |
michael@0 | 275 | return Point(SkScalarToFloat(aPoint.x()), SkScalarToFloat(aPoint.y())); |
michael@0 | 276 | } |
michael@0 | 277 | |
michael@0 | 278 | static inline Rect |
michael@0 | 279 | SkRectToRect(const SkRect &aRect) |
michael@0 | 280 | { |
michael@0 | 281 | return Rect(SkScalarToFloat(aRect.x()), SkScalarToFloat(aRect.y()), |
michael@0 | 282 | SkScalarToFloat(aRect.width()), SkScalarToFloat(aRect.height())); |
michael@0 | 283 | } |
michael@0 | 284 | |
michael@0 | 285 | static inline SkShader::TileMode |
michael@0 | 286 | ExtendModeToTileMode(ExtendMode aMode) |
michael@0 | 287 | { |
michael@0 | 288 | switch (aMode) |
michael@0 | 289 | { |
michael@0 | 290 | case ExtendMode::CLAMP: |
michael@0 | 291 | return SkShader::kClamp_TileMode; |
michael@0 | 292 | case ExtendMode::REPEAT: |
michael@0 | 293 | return SkShader::kRepeat_TileMode; |
michael@0 | 294 | case ExtendMode::REFLECT: |
michael@0 | 295 | return SkShader::kMirror_TileMode; |
michael@0 | 296 | } |
michael@0 | 297 | return SkShader::kClamp_TileMode; |
michael@0 | 298 | } |
michael@0 | 299 | |
michael@0 | 300 | } |
michael@0 | 301 | } |
michael@0 | 302 | |
michael@0 | 303 | #endif /* MOZILLA_GFX_HELPERSSKIA_H_ */ |