Index: site/user/api/skia-c-example.c |
diff --git a/site/user/api/skia-c-example.c b/site/user/api/skia-c-example.c |
new file mode 100644 |
index 0000000000000000000000000000000000000000..949e78125d2bf15f6c0fd38762804e3b038e95a6 |
--- /dev/null |
+++ b/site/user/api/skia-c-example.c |
@@ -0,0 +1,92 @@ |
+/* |
+ * Copyright 2015 Google Inc. |
+ * |
+ * Use of this source code is governed by a BSD-style license that can be |
+ * found in the LICENSE file. |
+ */ |
+#include <assert.h> |
+#include <stdio.h> |
+ |
+#include "sk_data.h" |
+#include "sk_image.h" |
+#include "sk_canvas.h" |
+#include "sk_surface.h" |
+#include "sk_paint.h" |
+#include "sk_path.h" |
+ |
+static sk_surface_t* make_surface(int32_t w, int32_t h) { |
+ sk_imageinfo_t info; |
+ info.width = w; |
+ info.height = h; |
+ info.colorType = sk_colortype_get_default_8888(); |
+ info.alphaType = PREMUL_SK_ALPHATYPE; |
+ return sk_surface_new_raster(&info, NULL); |
+} |
+ |
+static void emit_png(const char* path, sk_surface_t* surface) { |
+ assert(surface); |
+ sk_image_t* image = sk_surface_new_image_snapshot(surface); |
+ assert(image); |
+ sk_data_t* data = sk_image_encode(image); |
+ assert(data); |
+ sk_image_unref(image); |
+ FILE* f = fopen(path, "wb"); |
+ assert(f); |
+ int ret = fwrite(sk_data_get_data(data), sk_data_get_size(data), 1, f); |
+ assert(1 == ret); |
+ (void)ret; |
mtklein
2015/09/01 15:48:20
?
hal.canary
2015/09/01 16:13:57
It was for -DNDEBUG=1 -Wunused-variable. Removed
|
+ fclose(f); |
+ sk_data_unref(data); |
+} |
+ |
+void draw(sk_canvas_t* canvas) { |
+ assert(canvas); |
+ sk_paint_t* paint = sk_paint_new(); |
mtklein
2015/09/01 15:48:20
might want to call this 'fill' so it's got better
reed1
2015/09/01 15:50:27
Why do we assert on this allocation? (and not all
hal.canary
2015/09/01 16:13:57
Done.
hal.canary
2015/09/01 16:13:57
Done.
|
+ assert(paint); |
+ sk_paint_set_color(paint, sk_color_set_argb(0xFF, 0x00, 0x00, 0xFF)); |
+ sk_canvas_draw_paint(canvas, paint); |
+ |
+ sk_paint_set_color(paint, sk_color_set_argb(0xFF, 0x00, 0xFF, 0xFF)); |
+ sk_rect_t rect; |
+ rect.left = 100.0f; |
+ rect.top = 100.0f; |
+ rect.right = 540.0f; |
+ rect.bottom = 380.0f; |
+ sk_canvas_draw_rect(canvas, &rect, paint); |
+ |
+ sk_paint_t* stroke = sk_paint_new(); |
+ sk_paint_set_color(stroke, sk_color_set_argb(0xFF, 0xFF, 0x00, 0x00)); |
+ sk_paint_set_antialias(stroke, true); |
+ sk_paint_set_stroke(stroke, true); |
+ sk_paint_set_stroke_width(stroke, 5.0f); |
+ sk_path_t* path = sk_path_new(); |
+ assert(path); |
+ sk_path_move_to(path, 50.0f, 50.0f); |
+ sk_path_line_to(path, 590.0f, 50.0f); |
+ sk_path_cubic_to(path, -490.0f, 50.0f, 1130.0f, 430.0f, 50.0f, 430.0f); |
+ sk_path_line_to(path, 590.0f, 430.0f); |
+ sk_canvas_draw_path(canvas, path, stroke); |
+ |
+ sk_paint_set_color(paint, sk_color_set_argb(0x80, 0x00, 0xFF, 0x00)); |
+ sk_rect_t rect2; |
+ rect2.left = 120.0f; |
+ rect2.top = 120.0f; |
+ rect2.right = 520.0f; |
+ rect2.bottom = 360.0f; |
+ sk_canvas_draw_oval(canvas, &rect2, paint); |
+ |
+ sk_path_delete(path); |
+ sk_paint_delete(stroke); |
+ sk_paint_delete(paint); |
+} |
+ |
+int main() { |
+ sk_surface_t* surface = make_surface(640, 480); |
+ assert(surface); |
+ sk_canvas_t* canvas = sk_surface_get_canvas(surface); |
+ assert(canvas); |
+ draw(canvas); |
+ emit_png("skia-c-example.png", surface); |
+ sk_surface_unref(surface); |
+ return 0; |
+} |