refactoring os.Open && time convertion bug resolved for benchmark
This commit is contained in:
parent
3065973abc
commit
6d75998b87
12 changed files with 115 additions and 26 deletions
4
Makefile
4
Makefile
|
@ -20,10 +20,10 @@ nuke:
|
|||
cd draw2d && make nuke
|
||||
cd draw2dgl && make nuke
|
||||
cd postscript && make nuke
|
||||
|
||||
command:
|
||||
cd cmd && make
|
||||
#cd wingui && make nuke
|
||||
|
||||
fmt:
|
||||
gofmt -w draw2d postscript wingui cmd
|
||||
gofmt -w .
|
||||
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
|
||||
include $(GOROOT)/src/Make.inc
|
||||
|
||||
TARG=gettingStarted testdraw2d testX11draw testandroid testgopher testimage testpostscript testWalkDraw
|
||||
TARG=gettingStarted testdraw2d testX11draw testandroid testgopher testimage testpostscript testWalkDraw draw2dgl
|
||||
|
||||
OFILES=$(TARG:%=%.$O)
|
||||
|
||||
|
|
63
cmd/draw2dgl.go
Normal file
63
cmd/draw2dgl.go
Normal file
|
@ -0,0 +1,63 @@
|
|||
// Ported from GLUT's samples. Original copyright below applies.
|
||||
|
||||
/* Copyright (c) Mark J. Kilgard, 1996. */
|
||||
|
||||
/* This program is freely distributable without licensing fees
|
||||
and is provided without guarantee or warrantee expressed or
|
||||
implied. This program is -not- in the public domain. */
|
||||
|
||||
/* This program is a response to a question posed by Gil Colgate
|
||||
<gcolgate@sirius.com> about how lengthy a program is required using
|
||||
OpenGL compared to using Direct3D immediate mode to "draw a
|
||||
triangle at screen coordinates 0,0, to 200,200 to 20,200, and I
|
||||
want it to be blue at the top vertex, red at the left vertex, and
|
||||
green at the right vertex". I'm not sure how long the Direct3D
|
||||
program is; Gil has used Direct3D and his guess is "about 3000
|
||||
lines of code". */
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"gl"
|
||||
"glut"
|
||||
)
|
||||
|
||||
type GLPainter struct {
|
||||
|
||||
}
|
||||
|
||||
func reshape(w, h int) {
|
||||
/* Because Gil specified "screen coordinates" (presumably with an
|
||||
upper-left origin), this short bit of code sets up the coordinate
|
||||
system to correspond to actual window coodrinates. This code
|
||||
wouldn't be required if you chose a (more typical in 3D) abstract
|
||||
coordinate system. */
|
||||
|
||||
gl.Viewport(0, 0, w, h) /* Establish viewing area to cover entire window. */
|
||||
gl.MatrixMode(gl.PROJECTION) /* Start modifying the projection matrix. */
|
||||
gl.LoadIdentity() /* Reset project matrix. */
|
||||
gl.Ortho(0, float64(w), 0, float64(h), -1, 1) /* Map abstract coords directly to window coords. */
|
||||
gl.Scalef(1, -1, 1) /* Invert Y axis so increasing Y goes down. */
|
||||
gl.Translatef(0, float32(-h), 0) /* Shift origin up to upper-left corner. */
|
||||
}
|
||||
|
||||
func display() {
|
||||
gl.Clear(gl.COLOR_BUFFER_BIT)
|
||||
gl.Begin(gl.TRIANGLES)
|
||||
gl.Color3f(0.0, 0.0, 1.0) /* blue */
|
||||
gl.Vertex2i(0, 0)
|
||||
gl.Color3f(0.0, 1.0, 0.0) /* green */
|
||||
gl.Vertex2i(200, 200)
|
||||
gl.Color3f(1.0, 0.0, 0.0) /* red */
|
||||
gl.Vertex2i(20, 200)
|
||||
gl.End()
|
||||
gl.Flush() /* Single buffered, so needs a flush. */
|
||||
}
|
||||
|
||||
func main() {
|
||||
glut.Init()
|
||||
glut.CreateWindow("single triangle")
|
||||
glut.DisplayFunc(display)
|
||||
glut.ReshapeFunc(reshape)
|
||||
glut.MainLoop()
|
||||
}
|
|
@ -16,7 +16,7 @@ import (
|
|||
|
||||
|
||||
func saveToPngFile(filePath string, m image.Image) {
|
||||
f, err := os.Open(filePath, os.O_CREAT|os.O_WRONLY, 0600)
|
||||
f, err := os.Create(filePath)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
os.Exit(1)
|
||||
|
|
|
@ -12,6 +12,7 @@ import (
|
|||
"image"
|
||||
"io/ioutil"
|
||||
"strings"
|
||||
"time"
|
||||
"draw2d.googlecode.com/hg/draw2d"
|
||||
"draw2d.googlecode.com/hg/postscript"
|
||||
"draw2d.googlecode.com/hg/wingui"
|
||||
|
@ -72,9 +73,9 @@ func WndProc(hwnd, msg uint32, wparam, lparam int32) uintptr {
|
|||
switch msg {
|
||||
case wingui.WM_CREATE:
|
||||
hdc := wingui.GetDC(hwnd)
|
||||
hdcWndBuffer = wingui.CreateCompatibleDC(hdc)
|
||||
wndBufferHeader = wingui.CreateCompatibleBitmap(hdc, 600, 800)
|
||||
wingui.GetObject(wndBufferHeader, unsafe.Sizeof(wndBuffer), uintptr(unsafe.Pointer(&wndBuffer)))
|
||||
hdcWndBuffer = wingui.CreateCompatibleDC(hdc)
|
||||
wingui.SelectObject(hdcWndBuffer, wndBufferHeader)
|
||||
|
||||
var bmp_header wingui.BITMAPINFOHEADER
|
||||
|
@ -113,19 +114,24 @@ func WndProc(hwnd, msg uint32, wparam, lparam int32) uintptr {
|
|||
}
|
||||
case wingui.WM_PAINT:
|
||||
var ps wingui.PAINTSTRUCT
|
||||
lastTime := time.Nanoseconds()
|
||||
hdc := wingui.BeginPaint(hwnd, &ps)
|
||||
gc := draw2d.NewImageGraphicContext(backBuffer)
|
||||
gc := draw2d.NewGraphicContext(backBuffer)
|
||||
gc.SetFillColor(image.RGBAColor{0xFF, 0xFF, 0xFF, 0xFF})
|
||||
gc.Clear()
|
||||
// gc.Clear()
|
||||
gc.Save()
|
||||
//gc.Translate(0, -380)
|
||||
interpreter := postscript.NewInterpreter(gc)
|
||||
reader := strings.NewReader(postscriptContent)
|
||||
interpreter.Execute(reader)
|
||||
dt := time.Nanoseconds() - lastTime
|
||||
gc.Restore()
|
||||
wingui.BitBlt(hdc, 0, 0, int(wndBuffer.Width), int(wndBuffer.Height), hdcWndBuffer, 0, 0, wingui.SRCCOPY)
|
||||
// back buf in
|
||||
|
||||
wingui.BitBlt(hdc, 0, 0, 100/*int(wndBuffer.Width)*/, 100/*int(wndBuffer.Height)*/, hdcWndBuffer, 0, 0, wingui.SRCCOPY)
|
||||
wingui.EndPaint(hwnd, &ps)
|
||||
rc = wingui.DefWindowProc(hwnd, msg, wparam, lparam)
|
||||
fmt.Printf("Redraw in : %f ms\n", float64(dt)*1e-6)
|
||||
case wingui.WM_CLOSE:
|
||||
wingui.DestroyWindow(hwnd)
|
||||
case wingui.WM_DESTROY:
|
||||
|
@ -165,10 +171,11 @@ func rungui() int {
|
|||
var wc wingui.Wndclassex
|
||||
wc.Size = uint32(unsafe.Sizeof(wc))
|
||||
wc.WndProc = wproc
|
||||
//wc.Style = wingui.CS_HREDRAW | wingui.CS_VREDRAW
|
||||
wc.Instance = mh
|
||||
wc.Icon = myicon
|
||||
wc.Cursor = mycursor
|
||||
wc.Background = wingui.COLOR_BTNFACE + 1
|
||||
wc.Background = 0
|
||||
wc.MenuName = nil
|
||||
wc.ClassName = wcname
|
||||
wc.IconSm = myicon
|
||||
|
@ -215,7 +222,7 @@ func rungui() int {
|
|||
}
|
||||
|
||||
func main() {
|
||||
src, err := os.Open("../resource/postscript/tiger.ps", 0, 0)
|
||||
src, err := os.OpenFile("../resource/postscript/tiger.ps", 0, 0)
|
||||
if err != nil {
|
||||
fmt.Println("can't find postscript file.")
|
||||
return
|
||||
|
|
|
@ -38,9 +38,9 @@ func initGc(w, h int) (image.Image, draw2d.GraphicContext) {
|
|||
|
||||
func saveToPngFile(TestName string, m image.Image) {
|
||||
dt := time.Nanoseconds() - lastTime
|
||||
fmt.Printf("%s during: %f ms\n", TestName, float64(dt)*10e-6)
|
||||
fmt.Printf("%s during: %f ms\n", TestName, float64(dt)*1e-6)
|
||||
filePath := folder + TestName + ".png"
|
||||
f, err := os.Open(filePath, os.O_CREAT|os.O_WRONLY, 0600)
|
||||
f, err := os.Create(filePath)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
os.Exit(1)
|
||||
|
|
|
@ -17,7 +17,7 @@ import (
|
|||
)
|
||||
|
||||
const (
|
||||
w, h = 256, 256
|
||||
w, h = 512, 512
|
||||
)
|
||||
|
||||
var (
|
||||
|
@ -39,10 +39,11 @@ func initGc(w, h int) (image.Image, draw2d.GraphicContext) {
|
|||
}
|
||||
|
||||
func saveToPngFile(TestName string, m image.Image) {
|
||||
dt := time.Nanoseconds() - lastTime
|
||||
fmt.Printf("%s during: %f ms\n", TestName, float64(dt)*10e-6)
|
||||
t := time.Nanoseconds()
|
||||
dt := t - lastTime
|
||||
fmt.Printf("%s during: %f ms\n", TestName, float64(dt)*1e-6)
|
||||
filePath := folder + TestName + ".png"
|
||||
f, err := os.Open(filePath, os.O_CREAT|os.O_WRONLY, 0600)
|
||||
f, err := os.Create(filePath)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
os.Exit(1)
|
||||
|
@ -59,7 +60,8 @@ func saveToPngFile(TestName string, m image.Image) {
|
|||
log.Println(err)
|
||||
os.Exit(1)
|
||||
}
|
||||
fmt.Printf("Wrote %s OK.\n", filePath)
|
||||
dt = time.Nanoseconds() - t
|
||||
fmt.Printf("Wrote %s OK in %f ms.\n", filePath, float64(dt)*1e-6)
|
||||
}
|
||||
|
||||
/*
|
||||
|
@ -496,13 +498,22 @@ func TestFillString() {
|
|||
gc.MoveTo(10, 52)
|
||||
gc.SetFontData(draw2d.FontData{"luxi", draw2d.FontFamilyMono, draw2d.FontStyleBold | draw2d.FontStyleItalic})
|
||||
width := gc.FillString("cou")
|
||||
fmt.Printf("width: %f\n", width)
|
||||
gc.RMoveTo(width+1, 0)
|
||||
gc.FillString("cou")
|
||||
saveToPngFile("TestFillString", i)
|
||||
}
|
||||
|
||||
func TestBigPicture() {
|
||||
i, gc := initGc(w, h)
|
||||
gc.SetLineWidth(10)
|
||||
|
||||
draw2d.Rect(gc, 0, 0, w, h)
|
||||
gc.Fill()
|
||||
saveToPngFile("TestBigPicture", i)
|
||||
}
|
||||
|
||||
func main() {
|
||||
t := time.Nanoseconds()
|
||||
TestPath()
|
||||
TestDrawArc()
|
||||
TestDrawArcNegative()
|
||||
|
@ -520,4 +531,7 @@ func main() {
|
|||
TestTransform()
|
||||
TestPathTransform()
|
||||
TestFillString()
|
||||
TestBigPicture()
|
||||
dt := time.Nanoseconds() - t
|
||||
fmt.Printf("All tests during: %f ms\n", float64(dt)*1e-6)
|
||||
}
|
||||
|
|
|
@ -38,9 +38,9 @@ func initGc(w, h int) (image.Image, draw2d.GraphicContext) {
|
|||
|
||||
func saveToPngFile(TestName string, m image.Image) {
|
||||
dt := time.Nanoseconds() - lastTime
|
||||
fmt.Printf("%s during: %f ms\n", TestName, float64(dt)*10e-6)
|
||||
fmt.Printf("%s during: %f ms\n", TestName, float64(dt)*1e-6)
|
||||
filePath := folder + TestName + ".png"
|
||||
f, err := os.Open(filePath, os.O_CREAT|os.O_WRONLY, 0600)
|
||||
f, err := os.Create(filePath)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
os.Exit(1)
|
||||
|
|
|
@ -15,7 +15,7 @@ import (
|
|||
|
||||
|
||||
func saveToPngFile(filePath string, m image.Image) {
|
||||
f, err := os.Open(filePath, os.O_CREAT|os.O_WRONLY, 0600)
|
||||
f, err := os.Create(filePath)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return
|
||||
|
@ -36,7 +36,7 @@ func saveToPngFile(filePath string, m image.Image) {
|
|||
}
|
||||
|
||||
func loadFromPngFile(filePath string) image.Image {
|
||||
f, err := os.Open(filePath, 0, 0)
|
||||
f, err := os.OpenFile(filePath, 0, 0)
|
||||
if f == nil {
|
||||
log.Printf("can't open file; err=%s\n", err.String())
|
||||
return nil
|
||||
|
|
|
@ -17,7 +17,7 @@ import (
|
|||
|
||||
|
||||
func saveToPngFile(filePath string, m image.Image) {
|
||||
f, err := os.Open(filePath, os.O_CREAT|os.O_WRONLY, 0600)
|
||||
f, err := os.Create(filePath)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
os.Exit(1)
|
||||
|
@ -43,8 +43,7 @@ func main() {
|
|||
gc.Translate(0, 380)
|
||||
gc.Scale(1, -1)
|
||||
gc.Translate(0, -380)
|
||||
lastTime := time.Nanoseconds()
|
||||
src, err := os.Open("../resource/postscript/tiger.ps", 0, 0)
|
||||
src, err := os.OpenFile("../resource/postscript/tiger.ps", 0, 0)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
@ -52,6 +51,7 @@ func main() {
|
|||
bytes, err := ioutil.ReadAll(src)
|
||||
reader := strings.NewReader(string(bytes))
|
||||
interpreter := postscript.NewInterpreter(gc)
|
||||
lastTime := time.Nanoseconds()
|
||||
interpreter.Execute(reader)
|
||||
dt := time.Nanoseconds() - lastTime
|
||||
fmt.Printf("Draw image: %f ms\n", float64(dt)*1e-6)
|
||||
|
|
|
@ -6,6 +6,7 @@ import (
|
|||
"exp/draw"
|
||||
"image"
|
||||
"log"
|
||||
"time"
|
||||
"freetype-go.googlecode.com/hg/freetype"
|
||||
"freetype-go.googlecode.com/hg/freetype/raster"
|
||||
)
|
||||
|
@ -184,7 +185,11 @@ func (gc *ImageGraphicContext) Fill(paths ...*PathStorage) {
|
|||
pathConverter := NewPathConverter(NewVertexMatrixTransform(gc.current.Tr, NewVertexAdder(gc.fillRasterizer)))
|
||||
pathConverter.ApproximationScale = gc.current.Tr.GetMaxAbsScaling()
|
||||
pathConverter.Convert(paths...)
|
||||
|
||||
t := time.Nanoseconds()
|
||||
gc.paint(gc.fillRasterizer, gc.current.FillColor)
|
||||
dt := time.Nanoseconds() - t
|
||||
log.Printf("Paint during %f\n", float64(dt)*1e-6)
|
||||
}
|
||||
|
||||
func (gc *ImageGraphicContext) FillStroke2(paths ...*PathStorage) {
|
||||
|
|
|
@ -59,7 +59,7 @@ func (interpreter *Interpreter) Execute(reader io.Reader) {
|
|||
}
|
||||
|
||||
func (interpreter *Interpreter) ExecuteFile(filePath string) os.Error {
|
||||
src, err := os.Open(filePath, 0, 0)
|
||||
src, err := os.Open(filePath)
|
||||
if src == nil {
|
||||
log.Printf("can't open file; err=%s\n", err.String())
|
||||
return err
|
||||
|
|
Loading…
Reference in a new issue