Merge from google/chrome/m55
[platform/upstream/libSkiaSharp.git] / src / c / sk_canvas.cpp
1 /*
2  * Copyright 2016 Xamarin Inc.
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7
8 #include "SkCanvas.h"
9
10 #include "sk_canvas.h"
11
12 #include "sk_types_priv.h"
13
14 void sk_canvas_clear(sk_canvas_t* ccanvas, sk_color_t color) {
15     AsCanvas(ccanvas)->clear(color);
16 }
17
18 void sk_canvas_discard(sk_canvas_t* ccanvas) {
19     AsCanvas(ccanvas)->discard();
20 }
21
22 int sk_canvas_get_save_count(sk_canvas_t* ccanvas) {
23     return AsCanvas(ccanvas)->getSaveCount();
24 }
25
26 void sk_canvas_restore_to_count(sk_canvas_t* ccanvas, int saveCount) {
27     AsCanvas(ccanvas)->restoreToCount(saveCount);
28 }
29
30 void sk_canvas_draw_color(sk_canvas_t* ccanvas, sk_color_t color, sk_xfermode_mode_t cmode) {
31     AsCanvas(ccanvas)->drawColor(color, (SkXfermode::Mode)cmode);
32 }
33
34 void sk_canvas_draw_points(sk_canvas_t* ccanvas, sk_point_mode_t pointMode, size_t count, const sk_point_t points [], const sk_paint_t* cpaint)
35 {
36     AsCanvas(ccanvas)->drawPoints ((SkCanvas::PointMode)pointMode, count, AsPoint(points), *AsPaint(cpaint));
37 }
38
39 void sk_canvas_draw_point(sk_canvas_t* ccanvas, float x, float y, const sk_paint_t* cpaint)
40 {
41     AsCanvas(ccanvas)->drawPoint (x, y, *AsPaint(cpaint));
42 }
43
44 void sk_canvas_draw_point_color(sk_canvas_t* ccanvas, float x, float y, sk_color_t color)
45 {
46     AsCanvas(ccanvas)->drawPoint (x, y, color);
47 }
48
49 void sk_canvas_draw_line(sk_canvas_t* ccanvas, float x0, float y0, float x1, float y1, sk_paint_t* cpaint)
50 {
51     AsCanvas(ccanvas)->drawLine(x0, y0, x1, y1, *AsPaint(cpaint));
52 }
53
54 void sk_canvas_draw_text (sk_canvas_t* ccanvas, const char *text, size_t byteLength, float x, float y, const sk_paint_t* cpaint)
55 {
56     AsCanvas(ccanvas)->drawText(text, byteLength, x, y, *AsPaint(cpaint));
57 }
58
59 void sk_canvas_draw_pos_text (sk_canvas_t* ccanvas, const char *text, size_t byteLength, const sk_point_t pos[], const sk_paint_t* cpaint)
60 {
61     AsCanvas(ccanvas)->drawPosText(text, byteLength, AsPoint(pos), *AsPaint(cpaint));
62 }
63
64 void sk_canvas_draw_text_on_path (sk_canvas_t* ccanvas, const char *text, size_t byteLength, const sk_path_t* path,
65                   float hOffset, float vOffset, const sk_paint_t* cpaint)
66 {
67     AsCanvas(ccanvas)->drawTextOnPathHV(text, byteLength, AsPath(*path), hOffset, vOffset, *AsPaint(cpaint));
68 }
69
70 void sk_canvas_draw_bitmap(sk_canvas_t* ccanvas, const sk_bitmap_t* cbitmap, float x, float y, const sk_paint_t* cpaint)
71 {
72     AsCanvas(ccanvas)->drawBitmap(*AsBitmap(cbitmap), x, y, AsPaint(cpaint));
73 }
74
75 void sk_canvas_draw_bitmap_rect(sk_canvas_t* ccanvas, const sk_bitmap_t* cbitmap, const sk_rect_t* csrcR, const sk_rect_t* cdstR, const sk_paint_t* cpaint)
76 {
77     SkCanvas* canvas = AsCanvas(ccanvas);
78     const SkBitmap& bitmap = *AsBitmap(cbitmap);
79     const SkRect& dst = AsRect(*cdstR);
80     const SkPaint* paint = AsPaint(cpaint);
81
82     if (csrcR) {
83         canvas->drawBitmapRect(bitmap, AsRect(*csrcR), dst, paint);
84     }
85     else {
86         canvas->drawBitmapRect(bitmap, dst, paint);
87     }
88 }
89
90 void sk_canvas_reset_matrix(sk_canvas_t* ccanvas)
91 {
92     AsCanvas(ccanvas)->resetMatrix();
93 }
94
95 void sk_canvas_set_matrix(sk_canvas_t* ccanvas, const sk_matrix_t* cmatrix)
96 {
97     SkMatrix matrix;
98     if (cmatrix) {
99         from_c(cmatrix, &matrix);
100     }
101     AsCanvas(ccanvas)->setMatrix(matrix);
102 }
103
104 void sk_canvas_get_total_matrix(sk_canvas_t* ccanvas, sk_matrix_t* cmatrix)
105 {
106     SkMatrix matrix = AsCanvas(ccanvas)->getTotalMatrix();
107     from_sk(&matrix, cmatrix);
108 }
109
110 void sk_canvas_draw_round_rect(sk_canvas_t* ccanvas, const sk_rect_t* crect, float rx, float ry, const sk_paint_t* cpaint) {
111     AsCanvas(ccanvas)->drawRoundRect(AsRect(*crect), rx, ry, AsPaint(*cpaint));
112 }
113
114 void sk_canvas_clip_rect_with_operation(sk_canvas_t* ccanvas, const sk_rect_t* crect, sk_region_op_t op, bool doAA) {
115     AsCanvas(ccanvas)->clipRect(AsRect(*crect), (SkRegion::Op)op, doAA);
116 }
117
118 void sk_canvas_clip_path_with_operation(sk_canvas_t* ccanvas, const sk_path_t* cpath, sk_region_op_t op, bool doAA) {
119     AsCanvas(ccanvas)->clipPath(AsPath(*cpath), (SkRegion::Op)op, doAA);
120 }
121
122 bool sk_canvas_get_clip_bounds(sk_canvas_t* ccanvas, sk_rect_t* cbounds) {
123     return AsCanvas(ccanvas)->getClipBounds(AsRect(cbounds));
124 }
125
126 bool sk_canvas_get_clip_device_bounds(sk_canvas_t* ccanvas, sk_irect_t* cbounds) {
127     return AsCanvas(ccanvas)->getClipDeviceBounds(AsIRect(cbounds));
128 }
129
130 int sk_canvas_save(sk_canvas_t* ccanvas) {
131     return AsCanvas(ccanvas)->save();
132 }
133
134 int sk_canvas_save_layer(sk_canvas_t* ccanvas, const sk_rect_t* crect, const sk_paint_t* cpaint) {
135     return AsCanvas(ccanvas)->saveLayer(AsRect(crect), AsPaint(cpaint));
136 }
137
138 void sk_canvas_restore(sk_canvas_t* ccanvas) {
139     AsCanvas(ccanvas)->restore();
140 }
141
142 void sk_canvas_translate(sk_canvas_t* ccanvas, float dx, float dy) {
143     AsCanvas(ccanvas)->translate(dx, dy);
144 }
145
146 void sk_canvas_scale(sk_canvas_t* ccanvas, float sx, float sy) {
147     AsCanvas(ccanvas)->scale(sx, sy);
148 }
149
150 void sk_canvas_rotate_degrees(sk_canvas_t* ccanvas, float degrees) {
151     AsCanvas(ccanvas)->rotate(degrees);
152 }
153
154 void sk_canvas_rotate_radians(sk_canvas_t* ccanvas, float radians) {
155     AsCanvas(ccanvas)->rotate(SkRadiansToDegrees(radians));
156 }
157
158 void sk_canvas_skew(sk_canvas_t* ccanvas, float sx, float sy) {
159     AsCanvas(ccanvas)->skew(sx, sy);
160 }
161
162 void sk_canvas_concat(sk_canvas_t* ccanvas, const sk_matrix_t* cmatrix) {
163     SkASSERT(cmatrix);
164     SkMatrix matrix;
165     from_c(cmatrix, &matrix);
166     AsCanvas(ccanvas)->concat(matrix);
167 }
168
169 void sk_canvas_clip_rect(sk_canvas_t* ccanvas, const sk_rect_t* crect) {
170     AsCanvas(ccanvas)->clipRect(AsRect(*crect));
171 }
172
173 void sk_canvas_clip_path(sk_canvas_t* ccanvas, const sk_path_t* cpath) {
174     AsCanvas(ccanvas)->clipPath(AsPath(*cpath));
175 }
176
177 void sk_canvas_draw_paint(sk_canvas_t* ccanvas, const sk_paint_t* cpaint) {
178     AsCanvas(ccanvas)->drawPaint(AsPaint(*cpaint));
179 }
180
181 void sk_canvas_draw_rect(sk_canvas_t* ccanvas, const sk_rect_t* crect, const sk_paint_t* cpaint) {
182     AsCanvas(ccanvas)->drawRect(AsRect(*crect), AsPaint(*cpaint));
183 }
184
185 void sk_canvas_draw_circle(sk_canvas_t* ccanvas, float cx, float cy, float rad,
186                            const sk_paint_t* cpaint) {
187     AsCanvas(ccanvas)->drawCircle(cx, cy, rad, AsPaint(*cpaint));
188 }
189
190 void sk_canvas_draw_oval(sk_canvas_t* ccanvas, const sk_rect_t* crect, const sk_paint_t* cpaint) {
191     AsCanvas(ccanvas)->drawOval(AsRect(*crect), AsPaint(*cpaint));
192 }
193
194 void sk_canvas_draw_path(sk_canvas_t* ccanvas, const sk_path_t* cpath, const sk_paint_t* cpaint) {
195     AsCanvas(ccanvas)->drawPath(AsPath(*cpath), AsPaint(*cpaint));
196 }
197
198 void sk_canvas_draw_image(sk_canvas_t* ccanvas, const sk_image_t* cimage, float x, float y,
199                           const sk_paint_t* cpaint) {
200     AsCanvas(ccanvas)->drawImage(AsImage(cimage), x, y, AsPaint(cpaint));
201 }
202
203 void sk_canvas_draw_image_rect(sk_canvas_t* ccanvas, const sk_image_t* cimage,
204                                const sk_rect_t* csrcR, const sk_rect_t* cdstR,
205                                const sk_paint_t* cpaint) {
206     SkCanvas* canvas = AsCanvas(ccanvas);
207     const SkImage* image = AsImage(cimage);
208     const SkRect& dst = AsRect(*cdstR);
209     const SkPaint* paint = AsPaint(cpaint);
210
211     if (csrcR) {
212         canvas->drawImageRect(image, AsRect(*csrcR), dst, paint);
213     } else {
214         canvas->drawImageRect(image, dst, paint);
215     }
216 }
217
218 void sk_canvas_draw_picture(sk_canvas_t* ccanvas, const sk_picture_t* cpicture,
219                             const sk_matrix_t* cmatrix, const sk_paint_t* cpaint) {
220     const SkMatrix* matrixPtr = NULL;
221     SkMatrix matrix;
222     if (cmatrix) {
223         from_c(cmatrix, &matrix);
224         matrixPtr = &matrix;
225     }
226     AsCanvas(ccanvas)->drawPicture(AsPicture(cpicture), matrixPtr, AsPaint(cpaint));
227 }
228
229 void sk_canvas_flush(sk_canvas_t* ccanvas) {
230     AsCanvas(ccanvas)->flush();
231 }
232
233 sk_canvas_t* sk_canvas_new_from_bitmap(const sk_bitmap_t* bitmap) {
234     return ToCanvas(new SkCanvas(AsBitmap(*bitmap)));
235 }
236
237 void sk_canvas_draw_bitmap_lattice(sk_canvas_t* ccanvas, 
238                                    const sk_bitmap_t* bitmap, 
239                                    const int* xDivs, int xCount, const int* yDivs, int yCount, 
240                                    const sk_rect_t* dst, 
241                                    const sk_paint_t* paint) {
242     SkCanvas::Lattice lattice = { xDivs, xCount, yDivs, yCount };
243     AsCanvas(ccanvas)->drawBitmapLattice(AsBitmap(*bitmap), lattice, AsRect(*dst), AsPaint(paint));
244 }
245
246 void sk_canvas_draw_image_lattice(sk_canvas_t* ccanvas, 
247                                   const sk_image_t* image, 
248                                   const int* xDivs, int xCount, const int* yDivs, int yCount, 
249                                   const sk_rect_t* dst, 
250                                   const sk_paint_t* paint) {
251     SkCanvas::Lattice lattice = { xDivs, xCount, yDivs, yCount };
252     AsCanvas(ccanvas)->drawImageLattice(AsImage(image), lattice, AsRect(*dst), AsPaint(paint));
253 }
254