diff options
author | Stephan Altmueller <stephana@google.com> | 2017-05-09 15:56:06 -0400 |
---|---|---|
committer | Skia Commit-Bot <skia-commit-bot@chromium.org> | 2017-05-10 15:32:19 +0000 |
commit | efa48d599de15d960d7335a4a93a6bbeb97d3c41 (patch) | |
tree | 6b95700087faeaaec0ea030d0fa5e17b515cebc1 /experimental/go-demo | |
parent | 05fd8cf08970763832d6690ca1823168dd6f071f (diff) |
Experimental Go bindings for Skia
This CL implements Go bindings for a subset of the functions in the C API.
It implements a Go version of the C demo program in
experimental/c-api-example/skia-c-example.c and the output is identical.
(Checked by hand).
The main purpose is to establish a pattern of calling the Skia C API that
is memory safe and provides a idiomatic Go interface to Skia.
Follow up CLs will cover the entire C API, add documentation and establish
a pattern to distribute the bindings more easily.
BUG=
Change-Id: I96ff7c3715164c533202ce300ab0312b1b07f884
Change-Id: I96ff7c3715164c533202ce300ab0312b1b07f884
Reviewed-on: https://skia-review.googlesource.com/10032
Reviewed-by: Mike Klein <mtklein@chromium.org>
Commit-Queue: Stephan Altmueller <stephana@google.com>
Diffstat (limited to 'experimental/go-demo')
-rw-r--r-- | experimental/go-demo/main.go | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/experimental/go-demo/main.go b/experimental/go-demo/main.go new file mode 100644 index 0000000000..7df64d6e40 --- /dev/null +++ b/experimental/go-demo/main.go @@ -0,0 +1,76 @@ +/* + * Copyright 2015 Google Inc. + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ +package main + +/* + This is a translation of the example code in + "experimental/c-api-example/skia-c-example.c" to test the + go-skia package. +*/ + +import ( + "log" + "os" + + skia "skia.googlesource.com/skia/experimental/go-skia" +) + +func main() { + imgInfo := &skia.ImageInfo{ + Width: 640, + Height: 480, + ColorType: skia.GetDefaultColortype(), + AlphaType: skia.PREMUL_ALPHATYPE, + } + + surface, err := skia.NewRasterSurface(imgInfo) + if err != nil { + log.Fatalln(err) + } + + canvas := surface.Canvas() + + fillPaint := skia.NewPaint() + fillPaint.SetColor(0xFF0000FF) + canvas.DrawPaint(fillPaint) + fillPaint.SetColor(0xFF00FFFF) + + rect := skia.NewRect(100, 100, 540, 380) + canvas.DrawRect(rect, fillPaint) + + strokePaint := skia.NewPaint() + strokePaint.SetColor(0xFFFF0000) + strokePaint.SetAntiAlias(true) + strokePaint.SetStroke(true) + strokePaint.SetStrokeWidth(5.0) + + path := skia.NewPath() + path.MoveTo(50, 50) + path.LineTo(590, 50) + path.CubicTo(-490, 50, 1130, 430, 50, 430) + path.LineTo(590, 430) + canvas.DrawPath(path, strokePaint) + + fillPaint.SetColor(0x8000FF00) + canvas.DrawOval(skia.NewRect(120, 120, 520, 360), fillPaint) + + // // Get a skia image from the surface. + skImg := surface.Image() + + // Write new image to file if we have one. + if skImg != nil { + out, err := os.Create("testimage.png") + if err != nil { + log.Fatal(err) + } + defer out.Close() + + if err := skImg.WritePNG(out); err != nil { + log.Fatalf("Unable to write png: %s", err) + } + } +} |