Merge m-c to inbound.
[gecko.git] / gfx / 2d / HelpersSkia.h
blob3d9e191a4e8277d302c3d610aa96343af2492e0a
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_
9 #include "2D.h"
10 #include "skia/SkCanvas.h"
11 #include "skia/SkDashPathEffect.h"
12 #include "skia/SkShader.h"
13 #ifdef USE_SKIA_GPU
14 #include "skia/GrTypes.h"
15 #endif
16 #include "mozilla/Assertions.h"
17 #include <vector>
19 namespace mozilla {
20 namespace gfx {
22 static inline SkBitmap::Config
23 GfxFormatToSkiaConfig(SurfaceFormat format)
25 switch (format)
27 case FORMAT_B8G8R8A8:
28 return SkBitmap::kARGB_8888_Config;
29 case FORMAT_B8G8R8X8:
30 // We probably need to do something here.
31 return SkBitmap::kARGB_8888_Config;
32 case FORMAT_R5G6B5:
33 return SkBitmap::kRGB_565_Config;
34 case FORMAT_A8:
35 return SkBitmap::kA8_Config;
36 default:
37 return SkBitmap::kARGB_8888_Config;
41 static inline SurfaceFormat
42 SkiaConfigToGfxFormat(SkBitmap::Config config)
44 switch (config)
46 case SkBitmap::kARGB_8888_Config:
47 return FORMAT_B8G8R8A8;
48 case SkBitmap::kRGB_565_Config:
49 return FORMAT_R5G6B5;
50 case SkBitmap::kA8_Config:
51 return FORMAT_A8;
52 default:
53 return FORMAT_B8G8R8A8;
57 #ifdef USE_SKIA_GPU
58 static inline GrPixelConfig
59 GfxFormatToGrConfig(SurfaceFormat format)
61 switch (format)
63 case FORMAT_B8G8R8A8:
64 return kBGRA_8888_GrPixelConfig;
65 case FORMAT_B8G8R8X8:
66 // We probably need to do something here.
67 return kBGRA_8888_GrPixelConfig;
68 case FORMAT_R5G6B5:
69 return kRGB_565_GrPixelConfig;
70 case FORMAT_A8:
71 return kAlpha_8_GrPixelConfig;
72 default:
73 return kRGBA_8888_GrPixelConfig;
77 #endif
78 static inline void
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),
83 0, 0, SK_Scalar1);
86 static inline SkPaint::Cap
87 CapStyleToSkiaCap(CapStyle aCap)
89 switch (aCap)
91 case CAP_BUTT:
92 return SkPaint::kButt_Cap;
93 case CAP_ROUND:
94 return SkPaint::kRound_Cap;
95 case CAP_SQUARE:
96 return SkPaint::kSquare_Cap;
98 return SkPaint::kDefault_Cap;
101 static inline SkPaint::Join
102 JoinStyleToSkiaJoin(JoinStyle aJoin)
104 switch (aJoin)
106 case JOIN_BEVEL:
107 return SkPaint::kBevel_Join;
108 case JOIN_ROUND:
109 return SkPaint::kRound_Join;
110 case JOIN_MITER:
111 case JOIN_MITER_OR_BEVEL:
112 return SkPaint::kMiter_Join;
114 return SkPaint::kDefault_Join;
117 static inline bool
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) {
123 return false;
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.
132 uint32_t dashCount;
134 if (aOptions.mDashLength % 2 == 0) {
135 dashCount = aOptions.mDashLength;
136 } else {
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(),
148 dashCount,
149 SkFloatToScalar(aOptions.mDashOffset));
150 SkSafeUnref(aPaint.setPathEffect(dash));
153 aPaint.setStyle(SkPaint::kStroke_Style);
154 return true;
157 static inline SkXfermode::Mode
158 GfxOpToSkiaOp(CompositionOp op)
160 switch (op)
162 case OP_OVER:
163 return SkXfermode::kSrcOver_Mode;
164 case OP_ADD:
165 return SkXfermode::kPlus_Mode;
166 case OP_ATOP:
167 return SkXfermode::kSrcATop_Mode;
168 case OP_OUT:
169 return SkXfermode::kSrcOut_Mode;
170 case OP_IN:
171 return SkXfermode::kSrcIn_Mode;
172 case OP_SOURCE:
173 return SkXfermode::kSrc_Mode;
174 case OP_DEST_IN:
175 return SkXfermode::kDstIn_Mode;
176 case OP_DEST_OUT:
177 return SkXfermode::kDstOut_Mode;
178 case OP_DEST_OVER:
179 return SkXfermode::kDstOver_Mode;
180 case OP_DEST_ATOP:
181 return SkXfermode::kDstATop_Mode;
182 case OP_XOR:
183 return SkXfermode::kXor_Mode;
184 case OP_MULTIPLY:
185 return SkXfermode::kMultiply_Mode;
186 case OP_SCREEN:
187 return SkXfermode::kScreen_Mode;
188 case OP_OVERLAY:
189 return SkXfermode::kOverlay_Mode;
190 case OP_DARKEN:
191 return SkXfermode::kDarken_Mode;
192 case OP_LIGHTEN:
193 return SkXfermode::kLighten_Mode;
194 case OP_COLOR_DODGE:
195 return SkXfermode::kColorDodge_Mode;
196 case OP_COLOR_BURN:
197 return SkXfermode::kColorBurn_Mode;
198 case OP_HARD_LIGHT:
199 return SkXfermode::kHardLight_Mode;
200 case OP_SOFT_LIGHT:
201 return SkXfermode::kSoftLight_Mode;
202 case OP_DIFFERENCE:
203 return SkXfermode::kDifference_Mode;
204 case OP_EXCLUSION:
205 return SkXfermode::kExclusion_Mode;
206 case OP_HUE:
207 return SkXfermode::kHue_Mode;
208 case OP_SATURATION:
209 return SkXfermode::kSaturation_Mode;
210 case OP_COLOR:
211 return SkXfermode::kColor_Mode;
212 case OP_LUMINOSITY:
213 return SkXfermode::kLuminosity_Mode;
214 default:
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));
226 static inline SkRect
227 RectToSkRect(const Rect& aRect)
229 return SkRect::MakeXYWH(SkFloatToScalar(aRect.x), SkFloatToScalar(aRect.y),
230 SkFloatToScalar(aRect.width), SkFloatToScalar(aRect.height));
233 static inline SkRect
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);
253 static inline Point
254 SkPointToPoint(const SkPoint &aPoint)
256 return Point(SkScalarToFloat(aPoint.x()), SkScalarToFloat(aPoint.y()));
259 static inline SkShader::TileMode
260 ExtendModeToTileMode(ExtendMode aMode)
262 switch (aMode)
264 case EXTEND_CLAMP:
265 return SkShader::kClamp_TileMode;
266 case EXTEND_REPEAT:
267 return SkShader::kRepeat_TileMode;
268 case EXTEND_REFLECT:
269 return SkShader::kMirror_TileMode;
271 return SkShader::kClamp_TileMode;
277 #endif /* MOZILLA_GFX_HELPERSSKIA_H_ */