blob: ae37c464b2aba1e135b9f7170578b6c4779c1ec4 [file] [log] [blame]
reed8e474782014-10-06 11:00:51 -07001/*
2 * Copyright 2014 Google 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
reed938dfba2014-10-06 06:08:16 -07008#include "sk_surface.h"
9
10#include "SkCanvas.h"
11#include "SkImage.h"
12#include "SkMatrix.h"
13#include "SkPaint.h"
14#include "SkPath.h"
15#include "SkSurface.h"
16
reed8e474782014-10-06 11:00:51 -070017static SkImageInfo make(const sk_imageinfo_t& cinfo) {
reed938dfba2014-10-06 06:08:16 -070018 return SkImageInfo::Make(cinfo.width, cinfo.height,
19 (SkColorType)cinfo.colorType, (SkAlphaType)cinfo.alphaType);
20}
21
reed8e474782014-10-06 11:00:51 -070022static const SkRect& AsRect(const sk_rect_t& crect) {
23 return reinterpret_cast<const SkRect&>(crect);
24}
25
26static const SkPath& AsPath(const sk_path_t& cpath) {
27 return reinterpret_cast<const SkPath&>(cpath);
28}
29
robertphillipsa624e872014-10-08 06:04:35 -070030static SkPath* as_path(sk_path_t* cpath) {
31 return reinterpret_cast<SkPath*>(cpath);
32}
33
reed8e474782014-10-06 11:00:51 -070034static const SkImage* AsImage(const sk_image_t* cimage) {
35 return reinterpret_cast<const SkImage*>(cimage);
36}
reed938dfba2014-10-06 06:08:16 -070037
38static const SkPaint& AsPaint(const sk_paint_t& cpaint) {
reed8e474782014-10-06 11:00:51 -070039 return reinterpret_cast<const SkPaint&>(cpaint);
reed938dfba2014-10-06 06:08:16 -070040}
41
42static const SkPaint* AsPaint(const sk_paint_t* cpaint) {
reed8e474782014-10-06 11:00:51 -070043 return reinterpret_cast<const SkPaint*>(cpaint);
reed938dfba2014-10-06 06:08:16 -070044}
45
reed8e474782014-10-06 11:00:51 -070046static SkPaint* AsPaint(sk_paint_t* cpaint) {
47 return reinterpret_cast<SkPaint*>(cpaint);
48}
49
50static SkCanvas* AsCanvas(sk_canvas_t* ccanvas) {
51 return reinterpret_cast<SkCanvas*>(ccanvas);
52}
53
54///////////////////////////////////////////////////////////////////////////////////////////
55
56sk_image_t* sk_image_new_raster_copy(const sk_imageinfo_t* cinfo, const void* pixels,
57 size_t rowBytes) {
58 return (sk_image_t*)SkImage::NewRasterCopy(make(*cinfo), pixels, rowBytes);
59}
60
61void sk_image_ref(const sk_image_t* cimage) {
62 AsImage(cimage)->ref();
63}
64
65void sk_image_unref(const sk_image_t* cimage) {
66 AsImage(cimage)->unref();
67}
68
69int sk_image_get_width(const sk_image_t* cimage) {
70 return AsImage(cimage)->width();
71}
72
73int sk_image_get_height(const sk_image_t* cimage) {
74 return AsImage(cimage)->height();
75}
76
77uint32_t sk_image_get_unique_id(const sk_image_t* cimage) {
78 return AsImage(cimage)->uniqueID();
79}
80
81///////////////////////////////////////////////////////////////////////////////////////////
82
83sk_paint_t* sk_paint_new() {
84 return (sk_paint_t*)SkNEW(SkPaint);
85}
86
87void sk_paint_delete(sk_paint_t* cpaint) {
88 SkDELETE(AsPaint(cpaint));
89}
90
91bool sk_paint_is_antialias(const sk_paint_t* cpaint) {
92 return AsPaint(*cpaint).isAntiAlias();
93}
94
95void sk_paint_set_antialias(sk_paint_t* cpaint, bool aa) {
96 AsPaint(cpaint)->setAntiAlias(aa);
97}
98
99sk_color_t sk_paint_get_color(const sk_paint_t* cpaint) {
100 return AsPaint(*cpaint).getColor();
101}
102
103void sk_paint_set_color(sk_paint_t* cpaint, sk_color_t c) {
104 AsPaint(cpaint)->setColor(c);
105}
reed938dfba2014-10-06 06:08:16 -0700106
107///////////////////////////////////////////////////////////////////////////////////////////
108
robertphillipsa624e872014-10-08 06:04:35 -0700109sk_path_t* sk_path_new() {
110 return (sk_path_t*)SkNEW(SkPath);
111}
112
113void sk_path_delete(sk_path_t* cpath) {
114 SkDELETE(as_path(cpath));
115}
116
117void sk_path_move_to(sk_path_t* cpath, float x, float y) {
118 as_path(cpath)->moveTo(x, y);
119}
120
121void sk_path_line_to(sk_path_t* cpath, float x, float y) {
122 as_path(cpath)->lineTo(x, y);
123}
124
125void sk_path_quad_to(sk_path_t* cpath, float x0, float y0, float x1, float y1) {
126 as_path(cpath)->quadTo(x0, y0, x1, y1);
127}
128
129void sk_path_close(sk_path_t* cpath) {
130 as_path(cpath)->close();
131}
132
133///////////////////////////////////////////////////////////////////////////////////////////
134
reed938dfba2014-10-06 06:08:16 -0700135void sk_canvas_save(sk_canvas_t* ccanvas) {
136 AsCanvas(ccanvas)->save();
137}
138
139void sk_canvas_save_layer(sk_canvas_t* ccanvas, const sk_rect_t* crect, const sk_paint_t* cpaint) {
140 AsCanvas(ccanvas)->drawRect(AsRect(*crect), AsPaint(*cpaint));
141}
142
143void sk_canvas_restore(sk_canvas_t* ccanvas) {
144 AsCanvas(ccanvas)->restore();
145}
146
147void sk_canvas_translate(sk_canvas_t* ccanvas, float dx, float dy) {
148 AsCanvas(ccanvas)->translate(dx, dy);
149}
150
151void sk_canvas_scale(sk_canvas_t* ccanvas, float sx, float sy) {
reed8e474782014-10-06 11:00:51 -0700152 AsCanvas(ccanvas)->scale(sx, sy);
reed938dfba2014-10-06 06:08:16 -0700153}
154
155void sk_canvas_draw_paint(sk_canvas_t* ccanvas, const sk_paint_t* cpaint) {
156 AsCanvas(ccanvas)->drawPaint(AsPaint(*cpaint));
157}
158
159void sk_canvas_draw_rect(sk_canvas_t* ccanvas, const sk_rect_t* crect, const sk_paint_t* cpaint) {
160 AsCanvas(ccanvas)->drawRect(AsRect(*crect), AsPaint(*cpaint));
161}
162
163void sk_canvas_draw_oval(sk_canvas_t* ccanvas, const sk_rect_t* crect, const sk_paint_t* cpaint) {
164 AsCanvas(ccanvas)->drawOval(AsRect(*crect), AsPaint(*cpaint));
165}
166
167void sk_canvas_draw_path(sk_canvas_t* ccanvas, const sk_path_t* cpath, const sk_paint_t* cpaint) {
168 AsCanvas(ccanvas)->drawPath(AsPath(*cpath), AsPaint(*cpaint));
169}
170
171void sk_canvas_draw_image(sk_canvas_t* ccanvas, const sk_image_t* cimage, float x, float y,
172 const sk_paint_t* cpaint) {
173 AsCanvas(ccanvas)->drawImage(AsImage(cimage), x, y, AsPaint(cpaint));
174}
175
176///////////////////////////////////////////////////////////////////////////////////////////
177
reed8e474782014-10-06 11:00:51 -0700178sk_surface_t* sk_surface_new_raster(const sk_imageinfo_t* cinfo) {
reed938dfba2014-10-06 06:08:16 -0700179 return (sk_surface_t*)SkSurface::NewRaster(make(*cinfo));
180}
181
reed8e474782014-10-06 11:00:51 -0700182sk_surface_t* sk_surface_new_raster_direct(const sk_imageinfo_t* cinfo, void* pixels,
reed938dfba2014-10-06 06:08:16 -0700183 size_t rowBytes) {
184 return (sk_surface_t*)SkSurface::NewRasterDirect(make(*cinfo), pixels, rowBytes);
185}
186
187void sk_surface_delete(sk_surface_t* csurf) {
188 SkSurface* surf = (SkSurface*)csurf;
189 SkSafeUnref(surf);
190}
191
192sk_canvas_t* sk_surface_get_canvas(sk_surface_t* csurf) {
193 SkSurface* surf = (SkSurface*)csurf;
reed8e474782014-10-06 11:00:51 -0700194 return (sk_canvas_t*)surf->getCanvas();
reed938dfba2014-10-06 06:08:16 -0700195}
196
197sk_image_t* sk_surface_new_image_snapshot(sk_surface_t* csurf) {
198 SkSurface* surf = (SkSurface*)csurf;
reed8e474782014-10-06 11:00:51 -0700199 return (sk_image_t*)surf->newImageSnapshot();
reed938dfba2014-10-06 06:08:16 -0700200}
201
202
reed8e474782014-10-06 11:00:51 -0700203///////////////////
204
205void sk_test_capi(SkCanvas* canvas) {
206 sk_imageinfo_t cinfo;
207 cinfo.width = 100;
208 cinfo.height = 100;
209 cinfo.colorType = (sk_colortype_t)kN32_SkColorType;
210 cinfo.alphaType = (sk_alphatype_t)kPremul_SkAlphaType;
211
212 sk_surface_t* csurface = sk_surface_new_raster(&cinfo);
213 sk_canvas_t* ccanvas = sk_surface_get_canvas(csurface);
214
215 sk_paint_t* cpaint = sk_paint_new();
216 sk_paint_set_antialias(cpaint, true);
217 sk_paint_set_color(cpaint, 0xFFFF0000);
218
219 sk_rect_t cr = { 5, 5, 95, 95 };
220 sk_canvas_draw_oval(ccanvas, &cr, cpaint);
221
222 cr.left += 25;
223 cr.top += 25;
224 cr.right -= 25;
225 cr.bottom -= 25;
226 sk_paint_set_color(cpaint, 0xFF00FF00);
227 sk_canvas_draw_rect(ccanvas, &cr, cpaint);
228
robertphillipsa624e872014-10-08 06:04:35 -0700229 sk_path_t* cpath = sk_path_new();
230 sk_path_move_to(cpath, 50, 50);
231 sk_path_line_to(cpath, 100, 100);
232 sk_path_line_to(cpath, 50, 100);
233 sk_path_close(cpath);
234
235 sk_canvas_draw_path(ccanvas, cpath, cpaint);
236
reed8e474782014-10-06 11:00:51 -0700237 sk_image_t* cimage = sk_surface_new_image_snapshot(csurface);
238
239 // HERE WE CROSS THE C..C++ boundary
240 canvas->drawImage((const SkImage*)cimage, 20, 20, NULL);
241
robertphillipsa624e872014-10-08 06:04:35 -0700242 sk_path_delete(cpath);
reed8e474782014-10-06 11:00:51 -0700243 sk_paint_delete(cpaint);
244 sk_image_unref(cimage);
245 sk_surface_delete(csurface);
246}