-
Notifications
You must be signed in to change notification settings - Fork 3
/
implremote.go
118 lines (109 loc) · 3.4 KB
/
implremote.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package ui
import (
"github.com/Yeicor/sdfx-ui/internal"
"github.com/barkimedes/go-deepcopy"
"github.com/deadsy/sdfx/sdf"
"github.com/deadsy/sdfx/vec/v2i"
"log"
"net/rpc"
"time"
)
// rendererClient implements DevRendererImpl by calling a remote implementation (using Go's net/rpc)
type rendererClient struct {
cl *rpc.Client
cachedReflectTree *internal.ReflectTree // Avoids sending the whole metadata tree over the network more than once
}
// newDevRendererClient see rendererClient
func newDevRendererClient(client *rpc.Client) internal.DevRendererImpl {
return &rendererClient{cl: client}
}
func (d *rendererClient) Dimensions() int {
var out int
err := d.cl.Call("RendererService.Dimensions", &out, &out)
if err != nil {
log.Println("[DevRenderer] Error on remote call (RendererService.Dimensions):", err)
}
return out
}
func (d *rendererClient) BoundingBox() sdf.Box3 {
var out sdf.Box3
err := d.cl.Call("RendererService.BoundingBox", &out, &out)
if err != nil {
log.Println("[DevRenderer] Error on remote call (RendererService.BoundingBox):", err)
}
return out
}
func (d *rendererClient) ReflectTree() *internal.ReflectTree {
if d.cachedReflectTree != nil {
return d.cachedReflectTree
}
var in sdf.Box3
var out internal.ReflectTree
err := d.cl.Call("RendererService.BoundingBox", &in, &out)
if err != nil {
log.Println("[DevRenderer] Error on remote call (RendererService.BoundingBox):", err)
}
return &out
}
func (d *rendererClient) ColorModes() int {
var out int
err := d.cl.Call("RendererService.ColorModes", &out, &out)
if err != nil {
log.Println("[DevRenderer] Error on remote call (RendererService.ColorModes):", err)
}
return out
}
func (d *rendererClient) Render(args *internal.RenderArgs) error {
fullRenderSize := args.FullRender.Bounds().Size()
args.StateLock.RLock() // Clone the state to avoid locking while the rendering is happening
argsRemote := &internal.RemoteRenderArgs{
RenderSize: v2i.Vec{X: fullRenderSize.X, Y: fullRenderSize.Y},
State: deepcopy.MustAnything(args.State).(*internal.RendererState),
}
argsRemote.State.ReflectTree = nil // HACK: Avoids sending the whole metadata tree over the network more than once
args.StateLock.RUnlock()
var ignoreMe int
err := d.cl.Call("RendererService.RenderStart", argsRemote, &ignoreMe)
if err != nil {
return err
}
for {
var res internal.RemoteRenderResults
err = d.cl.Call("RendererService.RenderGet", ignoreMe, &res)
if err != nil {
return err
}
select {
case <-args.Ctx.Done(): // Cancel remote renderer also
err = d.cl.Call("RendererService.RenderCancel", ignoreMe, &ignoreMe)
if err != nil {
log.Println("[DevRenderer] Error on remote call (RendererService.RenderCancel):", err)
}
return args.Ctx.Err()
default:
}
if res.NewState != nil {
args.StateLock.Lock() // Clone back the new state to avoid locking while the rendering is happening
*args.State = *res.NewState
args.StateLock.Unlock()
}
if res.IsPartial {
if args.PartialRenders != nil {
args.PartialRenders <- res.RenderedImg
}
} else { // Final render
if args.PartialRenders != nil {
close(args.PartialRenders)
}
args.CachedRenderLock.Lock()
*args.FullRender = *res.RenderedImg
args.CachedRenderLock.Unlock()
break
}
}
return err
}
func (d *rendererClient) Shutdown(timeout time.Duration) error {
var out int
return d.cl.Call("RendererService.Shutdown", &timeout, &out)
}