Debian Codegraph @

shiny/widget/glwidget/glwidget.go

raw symbols SCIP
1 // Copyright 2016 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 //go:build !android
6
7 // Package glwidget provides a widget containing a GL ES framebuffer.
8 package glwidget
9
10 import (
11 "fmt"
12 "image"
13 "image/draw"
14
15 "golang.org/x/exp/shiny/driver/gldriver"
16 "golang.org/x/exp/shiny/widget/node"
17 "golang.org/x/mobile/gl"
18 )
19
20 // GL is a widget that maintains an OpenGL ES context.
21 //
22 // The Draw function is responsible for configuring the GL viewport
23 // and for publishing the result to the widget by calling the Publish
24 // method when the frame is complete. A typical draw function:
25 //
26 // func(w *glwidget.GL) {
27 // w.Ctx.Viewport(0, 0, w.Rect.Dx(), w.Rect.Dy())
28 // w.Ctx.ClearColor(0, 0, 0, 1)
29 // w.Ctx.Clear(gl.COLOR_BUFFER_BIT)
30 // // ... draw the frame
31 // w.Publish()
32 // }
33 //
34 // The GL context is separate from the one used by the gldriver to
35 // render the window, and is only used by the glwidget package during
36 // initialization and for the duration of the Publish call. This means
37 // a glwidget user is free to use Ctx as a background GL context
38 // concurrently with the primary UI drawing done by the gldriver.
39 type GL struct {
40 node.LeafEmbed
41
42 Ctx gl.Context
43
44 draw func(*GL)
45 framebuffer gl.Framebuffer
46 tex gl.Texture
47 dst *image.RGBA
48 origin image.Point
49 }
50
51 // NewGL creates a GL widget with a Draw function called when painted.
52 func NewGL(drawFunc func(*GL)) *GL {
53 // TODO: use the size of the monitor as a bound for texture size.
54 const maxWidth, maxHeight = 4096, 3072
55
56 glctx, err := gldriver.NewContext()
57 if err != nil {
58 panic(fmt.Sprintf("glwidget: %v", err)) // TODO: return error?
59 }
60 w := &GL{
61 Ctx: glctx,
62 draw: drawFunc,
63 }
64 w.tex = w.Ctx.CreateTexture()
65 w.Ctx.BindTexture(gl.TEXTURE_2D, w.tex)
66
67 w.Ctx.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA, maxWidth, maxHeight, gl.RGBA, gl.UNSIGNED_BYTE, nil)
68 w.Ctx.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR)
69 w.Ctx.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR)
70 w.Ctx.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE)
71 w.Ctx.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE)
72
73 w.framebuffer = w.Ctx.CreateFramebuffer()
74 w.Ctx.BindFramebuffer(gl.FRAMEBUFFER, w.framebuffer)
75 w.Ctx.FramebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, w.tex, 0)
76
77 // TODO: delete the framebuffer, texture, and gl.Context.
78 // TODO: explicit or finalizer cleanup?
79
80 w.Wrapper = w
81
82 return w
83 }
84
85 func (w *GL) PaintBase(ctx *node.PaintBaseContext, origin image.Point) error {
86 w.Marks.UnmarkNeedsPaintBase()
87 if w.Rect.Empty() {
88 return nil
89 }
90 w.dst = ctx.Dst
91 w.origin = origin
92 w.draw(w)
93 w.dst = nil
94 return nil
95 }
96
97 // Publish renders the default framebuffer of Ctx onto the area of the
98 // window occupied by the widget.
99 func (w *GL) Publish() {
100 if w.dst == nil {
101 panic("glwidget: no destination, Publish called outside of Draw")
102 }
103 // TODO: draw the widget texture directly into the window framebuffer.
104 m := image.NewRGBA(image.Rect(0, 0, w.Rect.Dx(), w.Rect.Dy()))
105 w.Ctx.PixelStorei(gl.PACK_ALIGNMENT, 1)
106 w.Ctx.ReadPixels(m.Pix, 0, 0, w.Rect.Dx(), w.Rect.Dy(), gl.RGBA, gl.UNSIGNED_BYTE)
107 draw.Draw(w.dst, w.Rect.Add(w.origin), m, image.Point{}, draw.Over)
108 }