Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 /* | |
| 2 * Copyright 2015 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 #include <assert.h> | |
| 8 #include <stdio.h> | |
| 9 | |
| 10 #include "sk_data.h" | |
| 11 #include "sk_image.h" | |
| 12 #include "sk_canvas.h" | |
| 13 #include "sk_surface.h" | |
| 14 #include "sk_paint.h" | |
| 15 #include "sk_path.h" | |
| 16 | |
| 17 static sk_surface_t* make_surface(int32_t w, int32_t h) { | |
| 18 sk_imageinfo_t info; | |
| 19 info.width = w; | |
| 20 info.height = h; | |
| 21 info.colorType = sk_colortype_get_default_8888(); | |
| 22 info.alphaType = PREMUL_SK_ALPHATYPE; | |
| 23 return sk_surface_new_raster(&info, NULL); | |
| 24 } | |
| 25 | |
| 26 static void emit_png(const char* path, sk_surface_t* surface) { | |
| 27 assert(surface); | |
| 28 sk_image_t* image = sk_surface_new_image_snapshot(surface); | |
| 29 assert(image); | |
| 30 sk_data_t* data = sk_image_encode(image); | |
| 31 assert(data); | |
| 32 sk_image_unref(image); | |
| 33 FILE* f = fopen(path, "wb"); | |
| 34 assert(f); | |
| 35 int ret = fwrite(sk_data_get_data(data), sk_data_get_size(data), 1, f); | |
| 36 assert(1 == ret); | |
| 37 (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
| |
| 38 fclose(f); | |
| 39 sk_data_unref(data); | |
| 40 } | |
| 41 | |
| 42 void draw(sk_canvas_t* canvas) { | |
| 43 assert(canvas); | |
| 44 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.
| |
| 45 assert(paint); | |
| 46 sk_paint_set_color(paint, sk_color_set_argb(0xFF, 0x00, 0x00, 0xFF)); | |
| 47 sk_canvas_draw_paint(canvas, paint); | |
| 48 | |
| 49 sk_paint_set_color(paint, sk_color_set_argb(0xFF, 0x00, 0xFF, 0xFF)); | |
| 50 sk_rect_t rect; | |
| 51 rect.left = 100.0f; | |
| 52 rect.top = 100.0f; | |
| 53 rect.right = 540.0f; | |
| 54 rect.bottom = 380.0f; | |
| 55 sk_canvas_draw_rect(canvas, &rect, paint); | |
| 56 | |
| 57 sk_paint_t* stroke = sk_paint_new(); | |
| 58 sk_paint_set_color(stroke, sk_color_set_argb(0xFF, 0xFF, 0x00, 0x00)); | |
| 59 sk_paint_set_antialias(stroke, true); | |
| 60 sk_paint_set_stroke(stroke, true); | |
| 61 sk_paint_set_stroke_width(stroke, 5.0f); | |
| 62 sk_path_t* path = sk_path_new(); | |
| 63 assert(path); | |
| 64 sk_path_move_to(path, 50.0f, 50.0f); | |
| 65 sk_path_line_to(path, 590.0f, 50.0f); | |
| 66 sk_path_cubic_to(path, -490.0f, 50.0f, 1130.0f, 430.0f, 50.0f, 430.0f); | |
| 67 sk_path_line_to(path, 590.0f, 430.0f); | |
| 68 sk_canvas_draw_path(canvas, path, stroke); | |
| 69 | |
| 70 sk_paint_set_color(paint, sk_color_set_argb(0x80, 0x00, 0xFF, 0x00)); | |
| 71 sk_rect_t rect2; | |
| 72 rect2.left = 120.0f; | |
| 73 rect2.top = 120.0f; | |
| 74 rect2.right = 520.0f; | |
| 75 rect2.bottom = 360.0f; | |
| 76 sk_canvas_draw_oval(canvas, &rect2, paint); | |
| 77 | |
| 78 sk_path_delete(path); | |
| 79 sk_paint_delete(stroke); | |
| 80 sk_paint_delete(paint); | |
| 81 } | |
| 82 | |
| 83 int main() { | |
| 84 sk_surface_t* surface = make_surface(640, 480); | |
| 85 assert(surface); | |
| 86 sk_canvas_t* canvas = sk_surface_get_canvas(surface); | |
| 87 assert(canvas); | |
| 88 draw(canvas); | |
| 89 emit_png("skia-c-example.png", surface); | |
| 90 sk_surface_unref(surface); | |
| 91 return 0; | |
| 92 } | |
| OLD | NEW |