1cb93a386Sopenharmony_ci/* 2cb93a386Sopenharmony_ci * Copyright 2015 Google Inc. 3cb93a386Sopenharmony_ci * 4cb93a386Sopenharmony_ci * Use of this source code is governed by a BSD-style license that can be 5cb93a386Sopenharmony_ci * found in the LICENSE file. 6cb93a386Sopenharmony_ci */ 7cb93a386Sopenharmony_ci#include <stdio.h> 8cb93a386Sopenharmony_ci 9cb93a386Sopenharmony_ci#include "include/c/sk_canvas.h" 10cb93a386Sopenharmony_ci#include "include/c/sk_data.h" 11cb93a386Sopenharmony_ci#include "include/c/sk_image.h" 12cb93a386Sopenharmony_ci#include "include/c/sk_imageinfo.h" 13cb93a386Sopenharmony_ci#include "include/c/sk_paint.h" 14cb93a386Sopenharmony_ci#include "include/c/sk_path.h" 15cb93a386Sopenharmony_ci#include "include/c/sk_surface.h" 16cb93a386Sopenharmony_ci 17cb93a386Sopenharmony_cistatic sk_surface_t* make_surface(int32_t w, int32_t h) { 18cb93a386Sopenharmony_ci sk_imageinfo_t* info = sk_imageinfo_new(w, h, RGBA_8888_SK_COLORTYPE, 19cb93a386Sopenharmony_ci PREMUL_SK_ALPHATYPE, NULL); 20cb93a386Sopenharmony_ci sk_surface_t* result = sk_surface_new_raster(info, NULL); 21cb93a386Sopenharmony_ci sk_imageinfo_delete(info); 22cb93a386Sopenharmony_ci return result; 23cb93a386Sopenharmony_ci} 24cb93a386Sopenharmony_ci 25cb93a386Sopenharmony_cistatic void emit_png(const char* path, sk_surface_t* surface) { 26cb93a386Sopenharmony_ci sk_image_t* image = sk_surface_new_image_snapshot(surface); 27cb93a386Sopenharmony_ci sk_data_t* data = sk_image_encode(image); 28cb93a386Sopenharmony_ci sk_image_unref(image); 29cb93a386Sopenharmony_ci FILE* f = fopen(path, "wb"); 30cb93a386Sopenharmony_ci fwrite(sk_data_get_data(data), sk_data_get_size(data), 1, f); 31cb93a386Sopenharmony_ci fclose(f); 32cb93a386Sopenharmony_ci sk_data_unref(data); 33cb93a386Sopenharmony_ci} 34cb93a386Sopenharmony_ci 35cb93a386Sopenharmony_civoid draw(sk_canvas_t* canvas) { 36cb93a386Sopenharmony_ci sk_paint_t* fill = sk_paint_new(); 37cb93a386Sopenharmony_ci sk_paint_set_color(fill, sk_color_set_argb(0xFF, 0x00, 0x00, 0xFF)); 38cb93a386Sopenharmony_ci sk_canvas_draw_paint(canvas, fill); 39cb93a386Sopenharmony_ci 40cb93a386Sopenharmony_ci sk_paint_set_color(fill, sk_color_set_argb(0xFF, 0x00, 0xFF, 0xFF)); 41cb93a386Sopenharmony_ci sk_rect_t rect; 42cb93a386Sopenharmony_ci rect.left = 100.0f; 43cb93a386Sopenharmony_ci rect.top = 100.0f; 44cb93a386Sopenharmony_ci rect.right = 540.0f; 45cb93a386Sopenharmony_ci rect.bottom = 380.0f; 46cb93a386Sopenharmony_ci sk_canvas_draw_rect(canvas, &rect, fill); 47cb93a386Sopenharmony_ci 48cb93a386Sopenharmony_ci sk_paint_t* stroke = sk_paint_new(); 49cb93a386Sopenharmony_ci sk_paint_set_color(stroke, sk_color_set_argb(0xFF, 0xFF, 0x00, 0x00)); 50cb93a386Sopenharmony_ci sk_paint_set_antialias(stroke, true); 51cb93a386Sopenharmony_ci sk_paint_set_stroke(stroke, true); 52cb93a386Sopenharmony_ci sk_paint_set_stroke_width(stroke, 5.0f); 53cb93a386Sopenharmony_ci 54cb93a386Sopenharmony_ci sk_pathbuilder_t* path_builder = sk_pathbuilder_new(); 55cb93a386Sopenharmony_ci sk_pathbuilder_move_to(path_builder, 50.0f, 50.0f); 56cb93a386Sopenharmony_ci sk_pathbuilder_line_to(path_builder, 590.0f, 50.0f); 57cb93a386Sopenharmony_ci sk_pathbuilder_cubic_to(path_builder, -490.0f, 50.0f, 1130.0f, 430.0f, 50.0f, 430.0f); 58cb93a386Sopenharmony_ci sk_pathbuilder_line_to(path_builder, 590.0f, 430.0f); 59cb93a386Sopenharmony_ci 60cb93a386Sopenharmony_ci sk_path_t* path = sk_pathbuilder_detach_path(path_builder); 61cb93a386Sopenharmony_ci sk_canvas_draw_path(canvas, path, stroke); 62cb93a386Sopenharmony_ci 63cb93a386Sopenharmony_ci sk_paint_set_color(fill, sk_color_set_argb(0x80, 0x00, 0xFF, 0x00)); 64cb93a386Sopenharmony_ci sk_rect_t rect2; 65cb93a386Sopenharmony_ci rect2.left = 120.0f; 66cb93a386Sopenharmony_ci rect2.top = 120.0f; 67cb93a386Sopenharmony_ci rect2.right = 520.0f; 68cb93a386Sopenharmony_ci rect2.bottom = 360.0f; 69cb93a386Sopenharmony_ci sk_canvas_draw_oval(canvas, &rect2, fill); 70cb93a386Sopenharmony_ci 71cb93a386Sopenharmony_ci sk_pathbuilder_delete(path_builder); 72cb93a386Sopenharmony_ci sk_path_delete(path); 73cb93a386Sopenharmony_ci sk_paint_delete(stroke); 74cb93a386Sopenharmony_ci sk_paint_delete(fill); 75cb93a386Sopenharmony_ci} 76cb93a386Sopenharmony_ci 77cb93a386Sopenharmony_ciint main() { 78cb93a386Sopenharmony_ci sk_surface_t* surface = make_surface(640, 480); 79cb93a386Sopenharmony_ci sk_canvas_t* canvas = sk_surface_get_canvas(surface); 80cb93a386Sopenharmony_ci draw(canvas); 81cb93a386Sopenharmony_ci emit_png("skia-c-example.png", surface); 82cb93a386Sopenharmony_ci sk_surface_unref(surface); 83cb93a386Sopenharmony_ci return 0; 84cb93a386Sopenharmony_ci} 85