-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathclient.go
More file actions
335 lines (287 loc) · 8.69 KB
/
Copy pathclient.go
File metadata and controls
335 lines (287 loc) · 8.69 KB
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
package compose
import (
"fmt"
"image"
"math"
"sync"
"sync/atomic"
"time"
"github.com/gogpu/compose/internal/codec"
"github.com/gogpu/compose/internal/protocol"
"github.com/gogpu/compose/internal/transport/socket"
)
// saturateUint16 converts v to uint16, clamping to math.MaxUint16 on overflow.
func saturateUint16(v int) uint16 {
return uint16(min(max(v, 0), math.MaxUint16))
}
// saturateUint16from32 converts v to uint16, clamping to math.MaxUint16 on overflow.
func saturateUint16from32(v uint32) uint16 {
return uint16(min(v, math.MaxUint16))
}
// saturateUint32 converts v to uint32, clamping to math.MaxUint32 on overflow.
func saturateUint32(v int) uint32 {
if v <= 0 {
return 0
}
if uint64(v) > uint64(math.MaxUint32) {
return math.MaxUint32
}
return uint32(v)
}
// Client is the module-side endpoint that connects to a compositor and
// publishes frames. All methods are safe for concurrent use.
type Client struct {
conn *socket.Conn
moduleID uint64
name string
codec codec.Codec
mu sync.RWMutex
onFrameRequest func()
closed atomic.Bool
done chan struct{}
wg sync.WaitGroup
seq atomic.Uint64
}
// Dial creates a Client that connects to a compositor at the given Unix
// domain socket address. Dial performs the handshake immediately: it sends
// a HelloMsg and reads the WelcomeMsg. If the compositor rejects the
// connection, ErrNotAccepted is returned.
//
// Use ClientOption functions to configure the module:
//
// client, err := compose.Dial("/tmp/compose.sock",
// compose.WithName("clock"),
// compose.WithFrameSize(400, 120),
// compose.WithFPS(1),
// )
func Dial(addr string, opts ...ClientOption) (*Client, error) {
cfg := defaultClientConfig()
for _, o := range opts {
o(&cfg)
}
dialer := socket.NewDialer(addr)
sc, err := dialer.Dial()
if err != nil {
return nil, fmt.Errorf("compose: dial: %w", err)
}
// Build and send HelloMsg.
hello := &protocol.HelloMsg{
Magic: protocol.Magic,
Version: protocol.ProtocolVersion,
Width: saturateUint16from32(cfg.width),
Height: saturateUint16from32(cfg.height),
PreferredFPS: cfg.fps,
Transport: protocol.TransportSocket,
}
protocol.SetName(hello, cfg.name)
if err := sc.WriteHandshakeHello(hello); err != nil {
_ = sc.Close()
return nil, fmt.Errorf("compose: send hello: %w", err)
}
// Read WelcomeMsg.
welcome, err := sc.ReadHandshakeWelcome()
if err != nil {
_ = sc.Close()
return nil, fmt.Errorf("compose: read welcome: %w", err)
}
if welcome.Accepted == 0 {
_ = sc.Close()
return nil, ErrNotAccepted
}
c := &Client{
conn: sc,
moduleID: welcome.ModuleID,
name: cfg.name,
codec: codec.Raw(), // client always sends raw; server handles decompression
done: make(chan struct{}),
}
// Start reader goroutine for FrameRequest messages.
c.wg.Add(1)
go c.readLoop()
return c, nil
}
// PublishFrame sends a frame to the compositor.
// The frame's ModuleID is automatically set to this client's assigned ID.
//
// Returns ErrClosed if the client has been shut down.
func (c *Client) PublishFrame(f Frame) error {
if c.closed.Load() {
return ErrClosed
}
seq := c.seq.Add(1)
pixels := f.Pixels
uncompressedSize := saturateUint32(len(pixels))
// Snapshot the codec while holding the client lock, then release it before
// doing the potentially expensive encode. Keeping the snapshot local makes
// the compression flags and payload use the same codec even if
// SetCompression runs concurrently.
c.mu.RLock()
frameCodec := c.codec
c.mu.RUnlock()
// Compress if codec is not raw.
var flags protocol.Flag
compressionID := protocol.CompressionNone
codecID := frameCodec.ID()
if codecID != codec.IDRaw {
maxSize := frameCodec.MaxEncodedSize(len(pixels))
dst := make([]byte, maxSize)
compressed, err := frameCodec.Encode(dst, pixels)
if err != nil {
return fmt.Errorf("compose: compress frame: %w", err)
}
pixels = compressed
flags = flags.Set(protocol.FlagCompressed)
compressionID = protocol.Compression(codecID)
}
// Set dirty rect flags.
if !f.DirtyRect.Empty() {
flags = flags.Set(protocol.FlagDirtyValid)
} else {
flags = flags.Set(protocol.FlagKeyframe)
}
hdr := &protocol.Header{
Magic: protocol.Magic,
Version: protocol.ProtocolVersion,
MsgType: protocol.MsgFrame,
Flags: flags,
ModuleID: c.moduleID,
Sequence: seq,
TimestampNs: f.Timestamp,
Width: saturateUint16from32(f.Width),
Height: saturateUint16from32(f.Height),
Stride: f.Width * 4,
PixelFormat: protocol.PixelRGBA8,
Compression: compressionID,
PayloadSize: saturateUint32(len(pixels)),
UncompressedSize: uncompressedSize,
}
// Set dirty rect fields if valid.
if flags.Has(protocol.FlagDirtyValid) {
hdr.DirtyX = saturateUint16(f.DirtyRect.Min.X)
hdr.DirtyY = saturateUint16(f.DirtyRect.Min.Y)
hdr.DirtyW = saturateUint16(f.DirtyRect.Dx())
hdr.DirtyH = saturateUint16(f.DirtyRect.Dy())
}
// Use monotonic timestamp if caller did not set one.
if hdr.TimestampNs == 0 {
hdr.TimestampNs = time.Now().UnixNano()
}
return c.conn.WriteFrame(hdr, pixels)
}
// OnFrameRequest registers a callback invoked when the compositor requests
// a frame. This enables pull-based rendering: the module renders only when
// asked. The callback is called on an internal goroutine; it must not block
// for extended periods. Only one callback can be active; subsequent calls
// replace the previous one. Pass nil to remove.
func (c *Client) OnFrameRequest(fn func()) {
c.mu.Lock()
defer c.mu.Unlock()
c.onFrameRequest = fn
}
// Close disconnects from the compositor and releases resources.
// After Close returns, no more callbacks will be invoked.
func (c *Client) Close() error {
if !c.closed.CompareAndSwap(false, true) {
return ErrClosed
}
close(c.done)
// Send graceful disconnect message (best-effort).
hdr := &protocol.Header{
Magic: protocol.Magic,
Version: protocol.ProtocolVersion,
MsgType: protocol.MsgDisconnect,
ModuleID: c.moduleID,
}
_ = c.conn.WriteFrame(hdr, nil)
err := c.conn.Close()
c.wg.Wait()
if err != nil {
return fmt.Errorf("compose: close client: %w", err)
}
return nil
}
// ModuleID returns the compositor-assigned module identifier.
// This is valid after Dial returns successfully.
func (c *Client) ModuleID() uint64 {
return c.moduleID
}
// readLoop runs in a goroutine, reading control messages from the
// compositor. Currently handles FrameRequest messages.
func (c *Client) readLoop() {
defer c.wg.Done()
for {
select {
case <-c.done:
return
default:
}
hdr, _, err := c.conn.ReadFrame()
if err != nil {
// Connection closed or error — stop reading.
return
}
switch hdr.MsgType {
case protocol.MsgFrameRequest:
c.mu.RLock()
cb := c.onFrameRequest
c.mu.RUnlock()
if cb != nil {
cb()
}
case protocol.MsgDisconnect:
return
default:
// Ignore unknown message types for forward compatibility.
}
}
}
// SetCompression sets the codec used for frame payload compression.
// This allows the compositor to negotiate compression during or after
// handshake. Supported values: "lz4". Any other value uses raw.
func (c *Client) SetCompression(algo string) {
c.mu.Lock()
defer c.mu.Unlock()
c.codec = resolveCodec(algo)
}
// frameToHeader converts a public Frame to a protocol.Header.
// Used in PublishFrame; extracted here for testing.
func frameToHeader(f Frame, moduleID uint64, seq uint64, c codec.Codec) protocol.Header {
var flags protocol.Flag
if !f.DirtyRect.Empty() {
flags = flags.Set(protocol.FlagDirtyValid)
} else {
flags = flags.Set(protocol.FlagKeyframe)
}
if c.ID() != codec.IDRaw {
flags = flags.Set(protocol.FlagCompressed)
}
hdr := protocol.Header{
Magic: protocol.Magic,
Version: protocol.ProtocolVersion,
MsgType: protocol.MsgFrame,
Flags: flags,
ModuleID: moduleID,
Sequence: seq,
TimestampNs: f.Timestamp,
Width: saturateUint16from32(f.Width),
Height: saturateUint16from32(f.Height),
Stride: f.Width * 4,
PixelFormat: protocol.PixelRGBA8,
Compression: protocol.Compression(c.ID()),
PayloadSize: saturateUint32(len(f.Pixels)),
UncompressedSize: saturateUint32(len(f.Pixels)),
}
if flags.Has(protocol.FlagDirtyValid) {
dr := f.DirtyRect.Canon()
hdr.DirtyX = saturateUint16(dr.Min.X)
hdr.DirtyY = saturateUint16(dr.Min.Y)
hdr.DirtyW = saturateUint16(dr.Dx())
hdr.DirtyH = saturateUint16(dr.Dy())
}
return hdr
}
// isDirtyRectValid reports whether the dirty rect represents a valid
// sub-region (non-empty).
func isDirtyRectValid(r image.Rectangle) bool {
return !r.Empty()
}