~emersion/drmdb

ref: 5822da6f76f7839c8466e19d3bccbdb950302467 drmdb/drmtree/drmtree.go -rw-r--r-- 7.8 KiB
5822da6fSimon Ser Let tables grow 2 years 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
// Package drmtree exposes DRM data as a tree structure.
package drmtree

import (
	"encoding/json"
	"fmt"

	"git.sr.ht/~emersion/go-drm"
)

type DriverVersion struct {
	Major int32  `json:"major"`
	Minor int32  `json:"minor"`
	Patch int32  `json:"patch"`
	Date  string `json:"date"`
}

func (ver *DriverVersion) String() string {
	return fmt.Sprintf("%v.%v.%v (%v)", ver.Major, ver.Minor, ver.Patch, ver.Date)
}

func (ver *DriverVersion) Less(other *DriverVersion) bool {
	return ver.Major < other.Major || ver.Minor < other.Minor || ver.Patch < other.Patch || ver.Date < other.Date
}

type Kernel struct {
	SysName string `json:"sysname"`
	Release string `json:"release"`
	Version string `json:"version"`
}

type Driver struct {
	Name       string             `json:"name"`
	Desc       string             `json:"desc"`
	Version    DriverVersion      `json:"version"`
	Kernel     Kernel             `json:"kernel"`
	Caps       map[string]*uint64 `json:"caps"`
	ClientCaps map[string]bool    `json:"client_caps"`
}

type DevicePCI struct {
	Vendor    uint32 `json:"vendor"`
	Device    uint32 `json:"device"`
	SubVendor uint32 `json:"subsystem_vendor"`
	SubDevice uint32 `json:"subsystem_device"`
}

type DevicePlatform struct {
	Compatible []string `json:"compatible"`
}

type Device struct {
	BusType    drm.BusType `json:"bus_type"`
	DeviceData interface{} `json:"device_data,omitempty"`
}

func (dev *Device) UnmarshalJSON(b []byte) error {
	type device Device

	var deviceData json.RawMessage
	rawDev := device{DeviceData: &deviceData}
	if err := json.Unmarshal(b, &rawDev); err != nil {
		return err
	}

	switch rawDev.BusType {
	case drm.BusPCI:
		var devPCI DevicePCI
		if err := json.Unmarshal(deviceData, &devPCI); err != nil {
			return err
		}
		rawDev.DeviceData = &devPCI
	case drm.BusPlatform:
		var devPlatform DevicePlatform
		if err := json.Unmarshal(deviceData, &devPlatform); err != nil {
			return err
		}
		rawDev.DeviceData = &devPlatform
	default:
		rawDev.DeviceData = nil
	}

	*dev = Device(rawDev)
	return nil
}

type Mode struct {
	Clock      uint32 `json:"clock"`
	HDisplay   uint16 `json:"hdisplay"`
	HSyncStart uint16 `json:"hsync_start"`
	HSyncEnd   uint16 `json:"hsync_end"`
	HTotal     uint16 `json:"htotal"`
	HSkew      uint16 `json:"hskew"`
	VDisplay   uint16 `json:"vdisplay"`
	VSyncStart uint16 `json:"vsync_total"`
	VSyncEnd   uint16 `json:"vsync_end"`
	VTotal     uint16 `json:"vtotal"`
	VScan      uint16 `json:"vscan"`
	VRefresh   uint32 `json:"vrefresh"`
	Flags      uint32 `json:"flags"`
	Type       uint32 `json:"type"`
	Name       string `json:"name"`
}

func (mode *Mode) String() string {
	// TODO: refresh and flags
	return mode.Name
}

type PlaneInFormatsModifier struct {
	Modifier drm.Modifier `json:"modifier"`
	Formats  []drm.Format `json:"formats"`
}

type PropertySpecEnum struct {
	Name  string `json:"name"`
	Value uint64 `json:"value"`
}

type PropertySpecRange struct {
	Min uint64 `json:"min"`
	Max uint64 `json:"max`
}

type PropertySpecSignedRange struct {
	Min int64 `json:"min"`
	Max int64 `json:"max`
}

type Property struct {
	ID        drm.PropertyID   `json:"id"`
	Type      drm.PropertyType `json:"type"`
	Immutable bool             `json:"immutable"`
	Atomic    bool             `json:"atomic"`
	Spec      interface{}      `json:"spec"`
	RawValue  uint64           `json:"raw_value"`
	// Value interpreted with the property type
	Value interface{} `json:"value"`
	// Value interpreted with the property name, optional
	Data interface{} `json:"data"`

	// For Data unmarshaling, populated by PropertyMap.UnmarshalJSON
	name string
}

func (prop *Property) UnmarshalJSON(b []byte) error {
	type property Property

	var rawSpec, rawValue, rawData json.RawMessage
	rawProp := property{
		Spec:  &rawSpec,
		Value: &rawValue,
		Data:  &rawData,
	}
	if err := json.Unmarshal(b, &rawProp); err != nil {
		return err
	}

	rawProp.Spec = nil
	rawProp.Value = nil
	rawProp.Data = nil

	if rawSpec != nil {
		var err error
		switch rawProp.Type {
		case drm.PropertyRange:
			var spec PropertySpecRange
			err = json.Unmarshal(rawSpec, &spec)
			rawProp.Spec = &spec
		case drm.PropertyEnum, drm.PropertyBitmask:
			var spec []PropertySpecEnum
			err = json.Unmarshal(rawSpec, &spec)
			rawProp.Spec = spec
		case drm.PropertyObject:
			spec := drm.ObjectType(0)
			err = json.Unmarshal(rawSpec, &spec)
			rawProp.Spec = spec
		case drm.PropertySignedRange:
			spec := PropertySpecSignedRange{}
			err = json.Unmarshal(rawSpec, &spec)
			rawProp.Spec = &spec
		}
		if err != nil {
			return err
		}
	}

	if rawValue != nil {
		var err error
		switch rawProp.Type {
		case drm.PropertyRange, drm.PropertyEnum, drm.PropertyBitmask:
			var value uint64
			err = json.Unmarshal(rawValue, &value)
			rawProp.Value = value
		case drm.PropertyBlob:
			var value []byte
			err = json.Unmarshal(rawValue, &value)
			rawProp.Value = value
		case drm.PropertyObject:
			var value drm.ObjectID
			err = json.Unmarshal(rawValue, &value)
			rawProp.Value = value
		case drm.PropertySignedRange:
			var value int64
			err = json.Unmarshal(rawValue, &value)
			rawProp.Value = value
		}
		if err != nil {
			return err
		}
	}

	if rawData != nil {
		var err error
		switch prop.name {
		case "SRC_X", "SRC_Y", "SRC_W", "SRC_H":
			var data uint64
			err = json.Unmarshal(rawData, &data)
			rawProp.Data = data
		case "IN_FORMATS":
			var data []PlaneInFormatsModifier
			err = json.Unmarshal(rawData, &data)
			rawProp.Data = data
		case "MODE_ID":
			var data Mode
			err = json.Unmarshal(rawData, &data)
			rawProp.Data = &data
		case "WRITEBACK_PIXEL_FORMATS":
			var data []drm.Format
			err = json.Unmarshal(rawData, &data)
			rawProp.Data = data
		case "PATH":
			var data string
			err = json.Unmarshal(rawData, &data)
			rawProp.Data = data
		}
		if err != nil {
			return err
		}
	}

	*prop = Property(rawProp)
	return nil
}

type PropertyMap map[string]Property

func (m *PropertyMap) UnmarshalJSON(b []byte) error {
	var rawMap map[string]json.RawMessage
	if err := json.Unmarshal(b, &rawMap); err != nil {
		return err
	}

	*m = make(PropertyMap)

	for name, rawProp := range rawMap {
		prop := Property{name: name}
		if err := json.Unmarshal(rawProp, &prop); err != nil {
			return err
		}

		(*m)[name] = prop
	}

	return nil
}

type Connector struct {
	ID         drm.ConnectorID     `json:"id"`
	Type       drm.ConnectorType   `json:"type"`
	Status     drm.ConnectorStatus `json:"status"`
	PhyWidth   uint32              `json:"phy_width"`
	PhyHeight  uint32              `json:"phy_height"`
	Subpixel   drm.Subpixel        `json:"subpixel"`
	Encoders   []drm.EncoderID     `json:"encoders"`
	Modes      []Mode              `json:"modes"`
	Properties PropertyMap         `json:"properties"`
}

type Encoder struct {
	ID             drm.EncoderID   `json:"id"`
	Type           drm.EncoderType `json:"type"`
	CRTC           drm.CRTCID      `json:"crtc"`
	PossibleCRTCs  uint32          `json:"possible_crtcs"`
	PossibleClones uint32          `json:"possible_clones"`
}

type CRTC struct {
	ID         drm.CRTCID  `json:"id"`
	FB         drm.FBID    `json:"fb"`
	X          uint32      `json:"x"`
	Y          uint32      `json:"y"`
	GammaSize  uint32      `json:"gamma_size"`
	Mode       *Mode       `json:"mode"`
	Properties PropertyMap `json:"properties"`
}

type Plane struct {
	ID            drm.PlaneID  `json:"id"`
	CRTC          drm.CRTCID   `json:"crtc"`
	FB            drm.FBID     `json:"fb"`
	PossibleCRTCs uint32       `json:"possible_crtcs"`
	GammaSize     uint32       `json:"gamma_size"`
	Formats       []drm.Format `json:"formats"`
	Properties    PropertyMap  `json:"properties"`
}

type Node struct {
	Driver     *Driver     `json:"driver"`
	Device     *Device     `json:"device"`
	Connectors []Connector `json:"connectors"`
	Encoders   []Encoder   `json:"encoders"`
	CRTCs      []CRTC      `json:"crtcs"`
	Planes     []Plane     `json:"planes"`
}

type NodeMap map[string]*Node