~emersion/go-drm

ref: c0c2450906d94f52d69c264e485731cbaf1a63f4 go-drm/node.go -rw-r--r-- 10.0 KiB
c0c24509Simon Ser Improve format/modifier pretty-printing 4 months ago
                                                                                
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
package drm

import (
	"fmt"
	"unsafe"
)

type Node struct {
	fd uintptr
}

func NewNode(fd uintptr) *Node {
	return &Node{fd}
}

type Version struct {
	Major, Minor, Patch int32
	Name, Date, Desc    string
}

func (n *Node) Version() (*Version, error) {
	var v versionResp
	if err := version(n.fd, &v); err != nil {
		return nil, err
	}

	name := allocBytes(&v.name, v.nameLen)
	date := allocBytes(&v.date, v.dateLen)
	desc := allocBytes(&v.desc, v.descLen)

	if err := version(n.fd, &v); err != nil {
		return nil, err
	}

	return &Version{
		Major: v.major,
		Minor: v.minor,
		Patch: v.patch,
		Name:  string(name),
		Date:  string(date),
		Desc:  string(desc),
	}, nil
}

type PCIDevice struct {
	Vendor, Device       uint32
	SubVendor, SubDevice uint32
}

func (d *PCIDevice) BusType() BusType {
	return BusPCI
}

type unknownDevice struct {
	busType BusType
}

func (d *unknownDevice) BusType() BusType {
	return d.busType
}

type Device interface {
	BusType() BusType
}

func (n *Node) GetDevice() (Device, error) {
	return n.getDevice()
}

func (n *Node) GetCap(cap Cap) (uint64, error) {
	return getCap(n.fd, uint64(cap))
}

func (n *Node) SetClientCap(cap ClientCap, val uint64) error {
	return setClientCap(n.fd, uint64(cap), val)
}

type ModeCard struct {
	FBs                                      []FBID
	CRTCs                                    []CRTCID
	Connectors                               []ConnectorID
	Encoders                                 []EncoderID
	MinWidth, MaxWidth, MinHeight, MaxHeight uint32
}

func (n *Node) ModeGetResources() (*ModeCard, error) {
	for {
		var r modeCardResp
		if err := modeGetResources(n.fd, &r); err != nil {
			return nil, err
		}
		count := r

		var fbs []FBID
		var crtcs []CRTCID
		var connectors []ConnectorID
		var encoders []EncoderID
		if r.fbsLen > 0 {
			fbs = make([]FBID, r.fbsLen)
			r.fbs = (*uint32)(unsafe.Pointer(&fbs[0]))
		}
		if r.crtcsLen > 0 {
			crtcs = make([]CRTCID, r.crtcsLen)
			r.crtcs = (*uint32)(unsafe.Pointer(&crtcs[0]))
		}
		if r.connectorsLen > 0 {
			connectors = make([]ConnectorID, r.connectorsLen)
			r.connectors = (*uint32)(unsafe.Pointer(&connectors[0]))
		}
		if r.encodersLen > 0 {
			encoders = make([]EncoderID, r.encodersLen)
			r.encoders = (*uint32)(unsafe.Pointer(&encoders[0]))
		}

		if err := modeGetResources(n.fd, &r); err != nil {
			return nil, err
		}

		if r.fbsLen != count.fbsLen || r.crtcsLen != count.crtcsLen || r.connectorsLen != count.connectorsLen || r.encodersLen != count.encodersLen {
			continue
		}

		return &ModeCard{
			FBs:        fbs,
			CRTCs:      crtcs,
			Connectors: connectors,
			Encoders:   encoders,
			MinWidth:   r.minWidth,
			MaxWidth:   r.maxWidth,
			MinHeight:  r.minHeight,
			MaxHeight:  r.maxHeight,
		}, nil
	}
}

func newString(b []byte) string {
	for i := 0; i < len(b); i++ {
		if b[i] == 0 {
			return string(b[:i])
		}
	}
	return string(b)
}

type ModeModeInfo struct {
	Clock                                         uint32
	HDisplay, HSyncStart, HSyncEnd, HTotal, HSkew uint16
	VDisplay, VSyncStart, VSyncEnd, VTotal, VScan uint16

	VRefresh uint32

	Flags uint32
	Type  uint32
	Name  string
}

func newModeModeInfo(info *modeModeInfo) *ModeModeInfo {
	return &ModeModeInfo{
		Clock:      info.clock,
		HDisplay:   info.hDisplay,
		HSyncStart: info.hSyncStart,
		HSyncEnd:   info.hSyncEnd,
		HTotal:     info.hTotal,
		HSkew:      info.hSkew,
		VDisplay:   info.vDisplay,
		VSyncStart: info.vSyncStart,
		VSyncEnd:   info.vSyncEnd,
		VTotal:     info.vTotal,
		VScan:      info.vScan,
		VRefresh:   info.vRefresh,
		Flags:      info.flags,
		Type:       info.typ,
		Name:       newString(info.name[:]),
	}
}

func newModeModeInfoList(infos []modeModeInfo) []ModeModeInfo {
	l := make([]ModeModeInfo, len(infos))
	for i, info := range infos {
		l[i] = *newModeModeInfo(&info)
	}
	return l
}

type ModeCRTC struct {
	ID        CRTCID
	FB        FBID
	X, Y      uint32
	GammaSize uint32
	Mode      *ModeModeInfo
}

func (n *Node) ModeGetCRTC(id CRTCID) (*ModeCRTC, error) {
	r := modeCRTCResp{id: uint32(id)}
	if err := modeGetCRTC(n.fd, &r); err != nil {
		return nil, err
	}

	var mode *ModeModeInfo
	if r.modeValid != 0 {
		mode = newModeModeInfo(&r.mode)
	}

	return &ModeCRTC{
		ID:        CRTCID(r.id),
		FB:        FBID(r.fb),
		X:         r.x,
		Y:         r.y,
		GammaSize: r.gammaSize,
		Mode:      mode,
	}, nil
}

type ModeEncoder struct {
	ID                            EncoderID
	Type                          EncoderType
	CRTC                          CRTCID
	PossibleCRTCs, PossibleClones uint32
}

func (n *Node) ModeGetEncoder(id EncoderID) (*ModeEncoder, error) {
	r := modeEncoderResp{id: uint32(id)}
	if err := modeGetEncoder(n.fd, &r); err != nil {
		return nil, err
	}

	return &ModeEncoder{
		ID:             EncoderID(r.id),
		Type:           EncoderType(r.typ),
		CRTC:           CRTCID(r.crtc),
		PossibleCRTCs:  r.possibleCRTCs,
		PossibleClones: r.possibleClones,
	}, nil
}

type ModeConnector struct {
	PossibleEncoders []EncoderID
	Modes            []ModeModeInfo

	Encoder EncoderID
	ID      ConnectorID
	Type    ConnectorType

	Status              ConnectorStatus
	PhyWidth, PhyHeight uint32 // mm
	Subpixel            Subpixel
}

func (n *Node) ModeGetConnector(id ConnectorID) (*ModeConnector, error) {
	for {
		r := modeConnectorResp{id: uint32(id)}
		if err := modeGetConnector(n.fd, &r); err != nil {
			return nil, err
		}
		count := r

		var encoders []EncoderID
		var modes []modeModeInfo
		if r.modesLen > 0 {
			modes = make([]modeModeInfo, r.modesLen)
			r.modes = (*modeModeInfo)(unsafe.Pointer(&modes[0]))
		}
		if r.encodersLen > 0 {
			encoders = make([]EncoderID, r.encodersLen)
			r.encoders = (*uint32)(unsafe.Pointer(&encoders[0]))
		}

		r.propsLen = 0 // don't retrieve properties

		if err := modeGetConnector(n.fd, &r); err != nil {
			return nil, err
		}

		if r.modesLen != count.modesLen || r.encodersLen != count.encodersLen {
			continue
		}

		return &ModeConnector{
			PossibleEncoders: encoders,
			Modes:            newModeModeInfoList(modes),
			Encoder:          EncoderID(r.encoder),
			ID:               ConnectorID(r.id),
			Type:             ConnectorType(r.typ),
			Status:           ConnectorStatus(r.status),
			PhyWidth:         r.phyWidth,
			PhyHeight:        r.phyHeight,
			Subpixel:         Subpixel(r.subpixel),
		}, nil
	}
}

func (n *Node) ModeGetPlaneResources() ([]PlaneID, error) {
	for {
		var r modePlaneResourcesResp
		if err := modeGetPlaneResources(n.fd, &r); err != nil {
			return nil, err
		}
		count := r

		var planes []PlaneID
		if r.planesLen > 0 {
			planes = make([]PlaneID, r.planesLen)
			r.planes = (*uint32)(unsafe.Pointer(&planes[0]))
		}

		if err := modeGetPlaneResources(n.fd, &r); err != nil {
			return nil, err
		}

		if r.planesLen != count.planesLen {
			continue
		}

		return planes, nil
	}
}

type ModePlane struct {
	ID PlaneID

	CRTC CRTCID
	FB   FBID

	PossibleCRTCs uint32
	GammaSize     uint32

	Formats []Format
}

func (n *Node) ModeGetPlane(id PlaneID) (*ModePlane, error) {
	for {
		r := modePlaneResp{id: uint32(id)}
		if err := modeGetPlane(n.fd, &r); err != nil {
			return nil, err
		}
		count := r

		var formats []Format
		if r.formatsLen > 0 {
			formats = make([]Format, r.formatsLen)
			r.formats = (*uint32)(unsafe.Pointer(&formats[0]))
		}

		if err := modeGetPlane(n.fd, &r); err != nil {
			return nil, err
		}

		if r.formatsLen != count.formatsLen {
			continue
		}

		return &ModePlane{
			ID:            PlaneID(r.id),
			CRTC:          CRTCID(r.crtc),
			FB:            FBID(r.fb),
			PossibleCRTCs: r.possibleCRTCs,
			GammaSize:     r.gammaSize,
			Formats:       formats,
		}, nil
	}
}

func (n *Node) ModeObjectGetProperties(id AnyID) (map[PropertyID]uint64, error) {
	for {
		r := modeObjectGetPropertiesResp{
			id:  uint32(id.Object()),
			typ: uint32(id.Type()),
		}
		if err := modeObjectGetProperties(n.fd, &r); err != nil {
			return nil, err
		}
		count := r

		var propIDs []PropertyID
		var propValues []uint64
		if r.propsLen > 0 {
			propIDs = make([]PropertyID, r.propsLen)
			r.propIDs = (*uint32)(unsafe.Pointer(&propIDs[0]))
			propValues = make([]uint64, r.propsLen)
			r.propValues = (*uint64)(unsafe.Pointer(&propValues[0]))
		}

		if err := modeObjectGetProperties(n.fd, &r); err != nil {
			return nil, err
		}

		if r.propsLen != count.propsLen {
			continue
		}

		m := make(map[PropertyID]uint64, r.propsLen)
		for i := 0; i < int(r.propsLen); i++ {
			m[propIDs[i]] = propValues[i]
		}
		return m, nil
	}
}

func (n *Node) ModeGetProperty(id PropertyID) (*ModeProperty, error) {
	r := modeGetPropertyResp{id: uint32(id)}
	if err := modeGetProperty(n.fd, &r); err != nil {
		return nil, err
	}

	var values []uint64
	if r.valuesLen > 0 {
		values = make([]uint64, r.valuesLen)
		r.values = (uintptr)(unsafe.Pointer(&values[0]))
	}

	var enums []modePropertyEnum
	var blobSizes []uint32
	var blobIDs []BlobID
	switch t := newPropertyType(r.flags); t {
	case PropertyEnum, PropertyBitmask:
		if r.enumBlobsLen > 0 {
			enums = make([]modePropertyEnum, r.enumBlobsLen)
			r.enumBlobs = (uintptr)(unsafe.Pointer(&enums[0]))
		}
	case PropertyBlob:
		if r.valuesLen > 0 {
			panic("drm: modeGetPropertyResp.valuesLen > 0 for blob property")
		}
		if r.enumBlobsLen > 0 {
			blobSizes = make([]uint32, r.enumBlobsLen)
			r.values = (uintptr)(unsafe.Pointer(&blobSizes[0]))
			blobIDs = make([]BlobID, r.enumBlobsLen)
			r.enumBlobs = (uintptr)(unsafe.Pointer(&blobIDs[0]))
		}
	default:
		if r.enumBlobsLen > 0 {
			panic(fmt.Sprintf("drm: enumBlobsLen > 0 for %s property", t))
		}
	}

	if err := modeGetProperty(n.fd, &r); err != nil {
		return nil, err
	}

	return &ModeProperty{
		ID:     PropertyID(r.id),
		Name:   newString(r.name[:]),
		flags:  r.flags,
		values: values,
		enums:  newModePropertyEnumList(enums),
		blobs:  newModePropertyBlobList(blobIDs, blobSizes),
	}, nil
}

func (n *Node) ModeGetBlob(id BlobID) ([]byte, error) {
	r := modeGetBlobResp{id: uint32(id)}
	if err := modeGetBlob(n.fd, &r); err != nil {
		return nil, err
	}

	var data []byte
	if r.size > 0 {
		data = make([]byte, r.size)
		r.data = (*byte)(unsafe.Pointer(&data[0]))
	}

	if err := modeGetBlob(n.fd, &r); err != nil {
		return nil, err
	}

	return data, nil
}