1 /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #ifndef MOZILLA_GFX_HELPERSSKIA_H_
7 #define MOZILLA_GFX_HELPERSSKIA_H_
10 #include "skia/SkCanvas.h"
11 #include "skia/SkDashPathEffect.h"
12 #include "skia/SkShader.h"
14 #include "skia/GrTypes.h"
16 #include "mozilla/Assertions.h"
22 static inline SkBitmap::Config
23 GfxFormatToSkiaConfig(SurfaceFormat format
)
28 return SkBitmap::kARGB_8888_Config
;
30 // We probably need to do something here.
31 return SkBitmap::kARGB_8888_Config
;
33 return SkBitmap::kRGB_565_Config
;
35 return SkBitmap::kA8_Config
;
37 return SkBitmap::kARGB_8888_Config
;
41 static inline SurfaceFormat
42 SkiaConfigToGfxFormat(SkBitmap::Config config
)
46 case SkBitmap::kARGB_8888_Config
:
47 return FORMAT_B8G8R8A8
;
48 case SkBitmap::kRGB_565_Config
:
50 case SkBitmap::kA8_Config
:
53 return FORMAT_B8G8R8A8
;
58 static inline GrPixelConfig
59 GfxFormatToGrConfig(SurfaceFormat format
)
64 return kBGRA_8888_GrPixelConfig
;
66 // We probably need to do something here.
67 return kBGRA_8888_GrPixelConfig
;
69 return kRGB_565_GrPixelConfig
;
71 return kAlpha_8_GrPixelConfig
;
73 return kRGBA_8888_GrPixelConfig
;
79 GfxMatrixToSkiaMatrix(const Matrix
& mat
, SkMatrix
& retval
)
81 retval
.setAll(SkFloatToScalar(mat
._11
), SkFloatToScalar(mat
._21
), SkFloatToScalar(mat
._31
),
82 SkFloatToScalar(mat
._12
), SkFloatToScalar(mat
._22
), SkFloatToScalar(mat
._32
),
86 static inline SkPaint::Cap
87 CapStyleToSkiaCap(CapStyle aCap
)
92 return SkPaint::kButt_Cap
;
94 return SkPaint::kRound_Cap
;
96 return SkPaint::kSquare_Cap
;
98 return SkPaint::kDefault_Cap
;
101 static inline SkPaint::Join
102 JoinStyleToSkiaJoin(JoinStyle aJoin
)
107 return SkPaint::kBevel_Join
;
109 return SkPaint::kRound_Join
;
111 case JOIN_MITER_OR_BEVEL
:
112 return SkPaint::kMiter_Join
;
114 return SkPaint::kDefault_Join
;
118 StrokeOptionsToPaint(SkPaint
& aPaint
, const StrokeOptions
&aOptions
)
120 // Skia renders 0 width strokes with a width of 1 (and in black),
121 // so we should just skip the draw call entirely.
122 if (!aOptions
.mLineWidth
) {
125 aPaint
.setStrokeWidth(SkFloatToScalar(aOptions
.mLineWidth
));
126 aPaint
.setStrokeMiter(SkFloatToScalar(aOptions
.mMiterLimit
));
127 aPaint
.setStrokeCap(CapStyleToSkiaCap(aOptions
.mLineCap
));
128 aPaint
.setStrokeJoin(JoinStyleToSkiaJoin(aOptions
.mLineJoin
));
130 if (aOptions
.mDashLength
> 0) {
131 // Skia only supports dash arrays that are multiples of 2.
134 if (aOptions
.mDashLength
% 2 == 0) {
135 dashCount
= aOptions
.mDashLength
;
137 dashCount
= aOptions
.mDashLength
* 2;
140 std::vector
<SkScalar
> pattern
;
141 pattern
.resize(dashCount
);
143 for (uint32_t i
= 0; i
< dashCount
; i
++) {
144 pattern
[i
] = SkFloatToScalar(aOptions
.mDashPattern
[i
% aOptions
.mDashLength
]);
147 SkDashPathEffect
* dash
= new SkDashPathEffect(&pattern
.front(),
149 SkFloatToScalar(aOptions
.mDashOffset
));
150 SkSafeUnref(aPaint
.setPathEffect(dash
));
153 aPaint
.setStyle(SkPaint::kStroke_Style
);
157 static inline SkXfermode::Mode
158 GfxOpToSkiaOp(CompositionOp op
)
163 return SkXfermode::kSrcOver_Mode
;
165 return SkXfermode::kPlus_Mode
;
167 return SkXfermode::kSrcATop_Mode
;
169 return SkXfermode::kSrcOut_Mode
;
171 return SkXfermode::kSrcIn_Mode
;
173 return SkXfermode::kSrc_Mode
;
175 return SkXfermode::kDstIn_Mode
;
177 return SkXfermode::kDstOut_Mode
;
179 return SkXfermode::kDstOver_Mode
;
181 return SkXfermode::kDstATop_Mode
;
183 return SkXfermode::kXor_Mode
;
185 return SkXfermode::kMultiply_Mode
;
187 return SkXfermode::kScreen_Mode
;
189 return SkXfermode::kOverlay_Mode
;
191 return SkXfermode::kDarken_Mode
;
193 return SkXfermode::kLighten_Mode
;
195 return SkXfermode::kColorDodge_Mode
;
197 return SkXfermode::kColorBurn_Mode
;
199 return SkXfermode::kHardLight_Mode
;
201 return SkXfermode::kSoftLight_Mode
;
203 return SkXfermode::kDifference_Mode
;
205 return SkXfermode::kExclusion_Mode
;
207 return SkXfermode::kHue_Mode
;
209 return SkXfermode::kSaturation_Mode
;
211 return SkXfermode::kColor_Mode
;
213 return SkXfermode::kLuminosity_Mode
;
215 return SkXfermode::kSrcOver_Mode
;
219 static inline SkColor
ColorToSkColor(const Color
&color
, Float aAlpha
)
221 //XXX: do a better job converting to int
222 return SkColorSetARGB(U8CPU(color
.a
*aAlpha
*255.0), U8CPU(color
.r
*255.0),
223 U8CPU(color
.g
*255.0), U8CPU(color
.b
*255.0));
227 RectToSkRect(const Rect
& aRect
)
229 return SkRect::MakeXYWH(SkFloatToScalar(aRect
.x
), SkFloatToScalar(aRect
.y
),
230 SkFloatToScalar(aRect
.width
), SkFloatToScalar(aRect
.height
));
234 IntRectToSkRect(const IntRect
& aRect
)
236 return SkRect::MakeXYWH(SkIntToScalar(aRect
.x
), SkIntToScalar(aRect
.y
),
237 SkIntToScalar(aRect
.width
), SkIntToScalar(aRect
.height
));
240 static inline SkIRect
241 RectToSkIRect(const Rect
& aRect
)
243 return SkIRect::MakeXYWH(int32_t(aRect
.x
), int32_t(aRect
.y
),
244 int32_t(aRect
.width
), int32_t(aRect
.height
));
247 static inline SkIRect
248 IntRectToSkIRect(const IntRect
& aRect
)
250 return SkIRect::MakeXYWH(aRect
.x
, aRect
.y
, aRect
.width
, aRect
.height
);
254 SkPointToPoint(const SkPoint
&aPoint
)
256 return Point(SkScalarToFloat(aPoint
.x()), SkScalarToFloat(aPoint
.y()));
259 static inline SkShader::TileMode
260 ExtendModeToTileMode(ExtendMode aMode
)
265 return SkShader::kClamp_TileMode
;
267 return SkShader::kRepeat_TileMode
;
269 return SkShader::kMirror_TileMode
;
271 return SkShader::kClamp_TileMode
;
277 #endif /* MOZILLA_GFX_HELPERSSKIA_H_ */