From 6d75998b872aa7cd8a8288ae38dc5689b2be7d3d Mon Sep 17 00:00:00 2001 From: Laurent Le Goff Date: Wed, 20 Apr 2011 18:30:48 +0200 Subject: [PATCH] refactoring os.Open && time convertion bug resolved for benchmark --- Makefile | 4 +-- cmd/Makefile | 2 +- cmd/draw2dgl.go | 63 +++++++++++++++++++++++++++++++++++++++ cmd/gettingStarted.go | 2 +- cmd/testWalkDraw.go | 19 ++++++++---- cmd/testandroid.go | 4 +-- cmd/testdraw2d.go | 26 ++++++++++++---- cmd/testgopher.go | 4 +-- cmd/testimage.go | 4 +-- cmd/testpostscript.go | 6 ++-- draw2d/image.go | 5 ++++ postscript/interpreter.go | 2 +- 12 files changed, 115 insertions(+), 26 deletions(-) create mode 100644 cmd/draw2dgl.go diff --git a/Makefile b/Makefile index 1ef1059..6e40c37 100644 --- a/Makefile +++ b/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 . diff --git a/cmd/Makefile b/cmd/Makefile index 1d2f034..bccfd08 100644 --- a/cmd/Makefile +++ b/cmd/Makefile @@ -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) diff --git a/cmd/draw2dgl.go b/cmd/draw2dgl.go new file mode 100644 index 0000000..cce25e8 --- /dev/null +++ b/cmd/draw2dgl.go @@ -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 + 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() +} diff --git a/cmd/gettingStarted.go b/cmd/gettingStarted.go index 000c23f..6ae8429 100644 --- a/cmd/gettingStarted.go +++ b/cmd/gettingStarted.go @@ -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) diff --git a/cmd/testWalkDraw.go b/cmd/testWalkDraw.go index b012952..8bb4626 100644 --- a/cmd/testWalkDraw.go +++ b/cmd/testWalkDraw.go @@ -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 diff --git a/cmd/testandroid.go b/cmd/testandroid.go index a1be290..6ebf6ed 100644 --- a/cmd/testandroid.go +++ b/cmd/testandroid.go @@ -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) diff --git a/cmd/testdraw2d.go b/cmd/testdraw2d.go index 3ba4d56..6425e11 100644 --- a/cmd/testdraw2d.go +++ b/cmd/testdraw2d.go @@ -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) } diff --git a/cmd/testgopher.go b/cmd/testgopher.go index ff8c25a..58a161e 100644 --- a/cmd/testgopher.go +++ b/cmd/testgopher.go @@ -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) diff --git a/cmd/testimage.go b/cmd/testimage.go index 4bd5a84..c4bfcf9 100644 --- a/cmd/testimage.go +++ b/cmd/testimage.go @@ -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 diff --git a/cmd/testpostscript.go b/cmd/testpostscript.go index 27c8744..039ae00 100644 --- a/cmd/testpostscript.go +++ b/cmd/testpostscript.go @@ -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) diff --git a/draw2d/image.go b/draw2d/image.go index 3f99518..3e1bb69 100644 --- a/draw2d/image.go +++ b/draw2d/image.go @@ -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) { diff --git a/postscript/interpreter.go b/postscript/interpreter.go index f2a0c19..ea391a9 100644 --- a/postscript/interpreter.go +++ b/postscript/interpreter.go @@ -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