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