Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(609)

Side by Side Diff: site/user/api/c.md

Issue 1307183006: C API: Add SK_API, also documentation of an example. (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: 2015-09-01 (Tuesday) 11:41:42 EDT Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 Skia's Stable C API
mtklein 2015/09/01 15:48:20 Let's move this out of site/
hal.canary 2015/09/01 16:13:57 Done. Moved to experimantal/c-example
2 ===================
3
4 <div style="text-align:center">
5 <strong>EXPERIMENTAL EXPERIMENTAL EXPERIMENTAL EXPERIMENTAL<br>
6 DO NOT USE &mdash; FOR INTERNAL TESTING ONLY</strong>
7 </div>
8
9 Several issues hinder the development of a stable ABI (application
10 binary interface) for Skia:
11
12 1. Skia's C++ API changes a lot from version to version. Skia's two
13 largest clients, Android and Chrome, are kept up to date by the
14 Skia team, but that can not happen for every client.
15 2. Skia's headers will only match the compiled skia libraries if
16 configured identically.
17
18 To mitigate these two issues, Skia is experimenting with the
19 introduction of a C API. This will change more slowly than the C++
20 interface and, once API version 1.0.0 is announced,
21 backwards-incompatable changes will be avoided whenever possible.
22
23 Here is an example program that uses the C api. To try it out, get the file
24 [`skia-c-example.c`](./skia-c-example.c).
25
26 <!--?prettify lang=c?-->
27
28 #include <assert.h>
29 #include <stdio.h>
30
31 #include "sk_data.h"
32 #include "sk_image.h"
33 #include "sk_canvas.h"
34 #include "sk_surface.h"
35 #include "sk_paint.h"
36 #include "sk_path.h"
37
38 static sk_surface_t* make_surface(int32_t w, int32_t h) {
39 sk_imageinfo_t info;
40 info.width = w;
41 info.height = h;
42 info.colorType = sk_colortype_get_default_8888();
43 info.alphaType = PREMUL_SK_ALPHATYPE;
44 return sk_surface_new_raster(&info, NULL);
45 }
46
47 static void emit_png(const char* path, sk_surface_t* surface) {
48 assert(surface);
49 sk_image_t* image = sk_surface_new_image_snapshot(surface);
50 assert(image);
51 sk_data_t* data = sk_image_encode(image);
52 assert(data);
53 sk_image_unref(image);
54 FILE* f = fopen(path, "wb");
55 assert(f);
56 int ret = fwrite(sk_data_get_data(data), sk_data_get_size(data), 1, f);
57 assert(1 == ret);
58 (void)ret;
59 fclose(f);
60 sk_data_unref(data);
61 }
62
63 void draw(sk_canvas_t* canvas) {
64 assert(canvas);
65 sk_paint_t* paint = sk_paint_new();
66 assert(paint);
67 sk_paint_set_color(paint, sk_color_set_argb(0xFF, 0x00, 0x00, 0xFF));
68 sk_canvas_draw_paint(canvas, paint);
69
70 sk_paint_set_color(paint, sk_color_set_argb(0xFF, 0x00, 0xFF, 0xFF));
71 sk_rect_t rect;
72 rect.left = 100.0f;
73 rect.top = 100.0f;
74 rect.right = 540.0f;
75 rect.bottom = 380.0f;
76 sk_canvas_draw_rect(canvas, &rect, paint);
77
78 sk_paint_t* stroke = sk_paint_new();
79 sk_paint_set_color(stroke, sk_color_set_argb(0xFF, 0xFF, 0x00, 0x00));
80 sk_paint_set_antialias(stroke, true);
81 sk_paint_set_stroke(stroke, true);
82 sk_paint_set_stroke_width(stroke, 5.0f);
83 sk_path_t* path = sk_path_new();
84 assert(path);
85 sk_path_move_to(path, 50.0f, 50.0f);
86 sk_path_line_to(path, 590.0f, 50.0f);
87 sk_path_cubic_to(path, -490.0f, 50.0f, 1130.0f, 430.0f, 50.0f, 430.0f);
88 sk_path_line_to(path, 590.0f, 430.0f);
89 sk_canvas_draw_path(canvas, path, stroke);
90
91 sk_paint_set_color(paint, sk_color_set_argb(0x80, 0x00, 0xFF, 0x00));
92 sk_rect_t rect2;
93 rect2.left = 120.0f;
94 rect2.top = 120.0f;
95 rect2.right = 520.0f;
96 rect2.bottom = 360.0f;
97 sk_canvas_draw_oval(canvas, &rect2, paint);
98
99 sk_path_delete(path);
100 sk_paint_delete(stroke);
101 sk_paint_delete(paint);
102 }
103
104 int main() {
105 sk_surface_t* surface = make_surface(640, 480);
106 assert(surface);
107 sk_canvas_t* canvas = sk_surface_get_canvas(surface);
108 assert(canvas);
109 draw(canvas);
110 emit_png("skia-c-example.png", surface);
111 sk_surface_unref(surface);
112 return 0;
113 }
114
115 <a href="https://fiddle.skia.org/c/6c6c01438d9c3d80e9c22e606359432e"><img src="h ttps://fiddle.skia.org/i/6c6c01438d9c3d8
116 0e9c22e606359432e_raster.png" alt=""></a>
117
118 Gyp+Linux example
119 -----------------
120
121 The following proof-of-concept workflow currently works on Ubuntu 14.04:
122
123 1. Aquire Skia and install dependencies (you may have already done this):
124
125 <!--?prettify lang=sh?-->
126
127 git clone 'https://chromium.googlesource.com/chromium/tools/depot_tools. git'
128 export PATH="$PWD/depot_tools:$PATH"
129 git clone 'https://skia.googlesource.com/skia'
130 skia/tools/install_dependencies.sh
131 SKIA_DIR="$PWD/skia"
132
133 2. Compile Skia as a shared library:
134
135 <!--?prettify lang=sh?-->
136
137 GYP_DEFINES=skia_shared_lib=1 "$SKIA_DIR"/bin/sync-and-gyp
138 ninja -C "$SKIA_DIR/out/Release" skia_lib
139
140 3. Compile, link, and run the example program:
141
142 <!--?prettify lang=sh?-->
143
144 cd [location_of_skia-c-example.c]/
145 cc -c -I "$SKIA_DIR/include/c" skia-c-example.c -o skia-c-example.o
146 cc skia-c-example.o -L "$SKIA_DIR/out/Release/lib" -lskia -o skia-c-exam ple
147 LD_LIBRARY_PATH="$SKIA_DIR/out/Release/lib" ./skia-c-example
148 xdg-open skia-c-example.png
149
150 Cmake+MacOS example
151 -------------------
152
153 The following proof-of-concept workflow currently works on MacOS
154
155 1. Aquire Skia and install dependencies (you may have already done this):
156
157 <!--?prettify lang=sh?-->
158
159 cd [Wherever you want skia src code]
160 git clone 'https://skia.googlesource.com/skia'
161 SKIA_DIR="$PWD/skia"
162
163 2. Compile Skia as a shared library:
164
165 <!--?prettify lang=sh?-->
166
167 cd [Wherever you want skia build files]
168 mkdir build_skia
169 cd build_skia
170 SKIA_BUILD="$PWD"
171 cmake "$SKIA_DIR/cmake" -G Ninja && ninja
172
173 3. Compile, link, and run the example program:
174
175 <!--?prettify lang=sh?-->
176
177 cd [Wherever you want the example]
178 cp "$SKIA_DIR/site/user/api/skia-c-example.c" .
179 cc -c -I "$SKIA_DIR/include/c" skia-c-example.c -o skia-c-example.o
180 c++ skia-c-example.o \
181 "$SKIA_BUILD"/libskia.* -Wl,-rpath -Wl,"$SKIA_BUILD" \
182 -o skia-c-example
183 ./skia-c-example
184 open skia-c-example.png
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698