-
Notifications
You must be signed in to change notification settings - Fork 328
/
path.go
796 lines (693 loc) · 28.2 KB
/
path.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
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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
// Copyright (C) 2017 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package path
import (
"fmt"
"math"
"reflect"
"strings"
"github.com/golang/protobuf/proto"
"github.com/google/gapid/core/data/id"
"github.com/google/gapid/core/data/protoutil"
"github.com/google/gapid/core/data/slice"
"github.com/google/gapid/core/image"
"github.com/google/gapid/gapis/service/box"
"github.com/google/gapid/gapis/vertex"
)
// Node is the interface for types that represent a reference to a capture,
// command list, single command, memory, state or sub-object. A path can be
// passed between client and server using RPCs in order to describe some data
// in a capture.
type Node interface {
// Parent returns the path that this path derives from.
// If this path is a root, then Base returns nil.
Parent() Node
// SetParent sets the path that this derives from.
SetParent(Node)
// Path returns this path node as a path.
Path() *Any
// Validate checks the path for correctness, returning an error if any
// issues are found.
Validate() error
}
func printIndices(index []uint64) string {
parts := make([]string, len(index))
for i, v := range index {
parts[i] = fmt.Sprint(v)
}
return strings.Join(parts, ".")
}
func (n *API) Path() *Any { return &Any{Path: &Any_API{n}} }
func (n *ArrayIndex) Path() *Any { return &Any{Path: &Any_ArrayIndex{n}} }
func (n *As) Path() *Any { return &Any{Path: &Any_As{n}} }
func (n *Blob) Path() *Any { return &Any{Path: &Any_Blob{n}} }
func (n *Capture) Path() *Any { return &Any{Path: &Any_Capture{n}} }
func (n *ConstantSet) Path() *Any { return &Any{Path: &Any_ConstantSet{n}} }
func (n *Command) Path() *Any { return &Any{Path: &Any_Command{n}} }
func (n *Commands) Path() *Any { return &Any{Path: &Any_Commands{n}} }
func (n *CommandTree) Path() *Any { return &Any{Path: &Any_CommandTree{n}} }
func (n *CommandTreeNode) Path() *Any { return &Any{Path: &Any_CommandTreeNode{n}} }
func (n *CommandTreeNodeForCommand) Path() *Any { return &Any{Path: &Any_CommandTreeNodeForCommand{n}} }
func (n *Context) Path() *Any { return &Any{Path: &Any_Context{n}} }
func (n *Contexts) Path() *Any { return &Any{Path: &Any_Contexts{n}} }
func (n *Device) Path() *Any { return &Any{Path: &Any_Device{n}} }
func (n *DeviceTraceConfiguration) Path() *Any { return &Any{Path: &Any_TraceConfig{n}} }
func (n *Events) Path() *Any { return &Any{Path: &Any_Events{n}} }
func (n *FramebufferObservation) Path() *Any { return &Any{Path: &Any_FBO{n}} }
func (n *Field) Path() *Any { return &Any{Path: &Any_Field{n}} }
func (n *GlobalState) Path() *Any { return &Any{Path: &Any_GlobalState{n}} }
func (n *ImageInfo) Path() *Any { return &Any{Path: &Any_ImageInfo{n}} }
func (n *MapIndex) Path() *Any { return &Any{Path: &Any_MapIndex{n}} }
func (n *Memory) Path() *Any { return &Any{Path: &Any_Memory{n}} }
func (n *Mesh) Path() *Any { return &Any{Path: &Any_Mesh{n}} }
func (n *Metrics) Path() *Any { return &Any{Path: &Any_Metrics{n}} }
func (n *Parameter) Path() *Any { return &Any{Path: &Any_Parameter{n}} }
func (n *Report) Path() *Any { return &Any{Path: &Any_Report{n}} }
func (n *ResourceData) Path() *Any { return &Any{Path: &Any_ResourceData{n}} }
func (n *Messages) Path() *Any { return &Any{Path: &Any_Messages{n}} }
func (n *MultiResourceData) Path() *Any { return &Any{Path: &Any_MultiResourceData{n}} }
func (n *Resources) Path() *Any { return &Any{Path: &Any_Resources{n}} }
func (n *Result) Path() *Any { return &Any{Path: &Any_Result{n}} }
func (n *Slice) Path() *Any { return &Any{Path: &Any_Slice{n}} }
func (n *State) Path() *Any { return &Any{Path: &Any_State{n}} }
func (n *StateTree) Path() *Any { return &Any{Path: &Any_StateTree{n}} }
func (n *StateTreeNode) Path() *Any { return &Any{Path: &Any_StateTreeNode{n}} }
func (n *StateTreeNodeForPath) Path() *Any { return &Any{Path: &Any_StateTreeNodeForPath{n}} }
func (n *Stats) Path() *Any { return &Any{Path: &Any_Stats{n}} }
func (n *Thumbnail) Path() *Any { return &Any{Path: &Any_Thumbnail{n}} }
func (n API) Parent() Node { return nil }
func (n ArrayIndex) Parent() Node { return oneOfNode(n.Array) }
func (n As) Parent() Node { return oneOfNode(n.From) }
func (n Blob) Parent() Node { return nil }
func (n Capture) Parent() Node { return nil }
func (n ConstantSet) Parent() Node { return n.API }
func (n Command) Parent() Node { return n.Capture }
func (n Commands) Parent() Node { return n.Capture }
func (n CommandTree) Parent() Node { return n.Capture }
func (n CommandTreeNode) Parent() Node { return nil }
func (n CommandTreeNodeForCommand) Parent() Node { return n.Command }
func (n Context) Parent() Node { return n.Capture }
func (n Contexts) Parent() Node { return n.Capture }
func (n Device) Parent() Node { return nil }
func (n DeviceTraceConfiguration) Parent() Node { return n.Device }
func (n Events) Parent() Node { return n.Capture }
func (n FramebufferObservation) Parent() Node { return n.Command }
func (n Field) Parent() Node { return oneOfNode(n.Struct) }
func (n GlobalState) Parent() Node { return n.After }
func (n ImageInfo) Parent() Node { return nil }
func (n MapIndex) Parent() Node { return oneOfNode(n.Map) }
func (n Memory) Parent() Node { return n.After }
func (n Mesh) Parent() Node { return oneOfNode(n.Object) }
func (n Metrics) Parent() Node { return n.Command }
func (n Messages) Parent() Node { return n.Capture }
func (n Parameter) Parent() Node { return n.Command }
func (n Report) Parent() Node { return n.Capture }
func (n ResourceData) Parent() Node { return n.After }
func (n MultiResourceData) Parent() Node { return n.After }
func (n Resources) Parent() Node { return n.Capture }
func (n Result) Parent() Node { return n.Command }
func (n Slice) Parent() Node { return oneOfNode(n.Array) }
func (n State) Parent() Node { return n.After }
func (n StateTree) Parent() Node { return n.State }
func (n StateTreeNode) Parent() Node { return nil }
func (n StateTreeNodeForPath) Parent() Node { return nil }
func (n Stats) Parent() Node { return n.Capture }
func (n Thumbnail) Parent() Node { return oneOfNode(n.Object) }
func (n *API) SetParent(p Node) {}
func (n *Blob) SetParent(p Node) {}
func (n *Capture) SetParent(p Node) {}
func (n *ConstantSet) SetParent(p Node) { n.API, _ = p.(*API) }
func (n *Command) SetParent(p Node) { n.Capture, _ = p.(*Capture) }
func (n *Commands) SetParent(p Node) { n.Capture, _ = p.(*Capture) }
func (n *CommandTree) SetParent(p Node) { n.Capture, _ = p.(*Capture) }
func (n *CommandTreeNode) SetParent(p Node) {}
func (n *CommandTreeNodeForCommand) SetParent(p Node) { n.Command, _ = p.(*Command) }
func (n *Context) SetParent(p Node) { n.Capture, _ = p.(*Capture) }
func (n *Contexts) SetParent(p Node) { n.Capture, _ = p.(*Capture) }
func (n *Device) SetParent(p Node) {}
func (n *DeviceTraceConfiguration) SetParent(p Node) { n.Device, _ = p.(*Device) }
func (n *Events) SetParent(p Node) { n.Capture, _ = p.(*Capture) }
func (n *FramebufferObservation) SetParent(p Node) { n.Command, _ = p.(*Command) }
func (n *GlobalState) SetParent(p Node) { n.After, _ = p.(*Command) }
func (n *ImageInfo) SetParent(p Node) {}
func (n *Memory) SetParent(p Node) { n.After, _ = p.(*Command) }
func (n *Metrics) SetParent(p Node) { n.Command, _ = p.(*Command) }
func (n *Messages) SetParent(p Node) { n.Capture, _ = p.(*Capture) }
func (n *Parameter) SetParent(p Node) { n.Command, _ = p.(*Command) }
func (n *Report) SetParent(p Node) { n.Capture, _ = p.(*Capture) }
func (n *ResourceData) SetParent(p Node) { n.After, _ = p.(*Command) }
func (n *MultiResourceData) SetParent(p Node) { n.After, _ = p.(*Command) }
func (n *Resources) SetParent(p Node) { n.Capture, _ = p.(*Capture) }
func (n *Result) SetParent(p Node) { n.Command, _ = p.(*Command) }
func (n *State) SetParent(p Node) { n.After, _ = p.(*Command) }
func (n *StateTree) SetParent(p Node) { n.State, _ = p.(*State) }
func (n *StateTreeNode) SetParent(p Node) {}
func (n *StateTreeNodeForPath) SetParent(p Node) {}
func (n *Stats) SetParent(p Node) { n.Capture, _ = p.(*Capture) }
// Format implements fmt.Formatter to print the path.
func (n ArrayIndex) Format(f fmt.State, c rune) {
fmt.Fprintf(f, "%v[%v]", n.Parent(), n.Index)
}
// Format implements fmt.Formatter to print the path.
func (n API) Format(f fmt.State, c rune) { fmt.Fprintf(f, "api<%v>", n.ID) }
// Format implements fmt.Formatter to print the path.
func (n As) Format(f fmt.State, c rune) {
fmt.Fprintf(f, "%v.as<%v>", n.Parent(), protoutil.OneOf(n.To))
}
// Format implements fmt.Formatter to print the path.
func (n Blob) Format(f fmt.State, c rune) { fmt.Fprintf(f, "blob<%x>", n.ID) }
// Format implements fmt.Formatter to print the path.
func (n Capture) Format(f fmt.State, c rune) { fmt.Fprintf(f, "capture<%x>", n.ID) }
// Format implements fmt.Formatter to print the path.
func (n ConstantSet) Format(f fmt.State, c rune) {
fmt.Fprintf(f, "%v.constant-set<%v>", n.Parent(), n.Index)
}
// Format implements fmt.Formatter to print the path.
func (n Command) Format(f fmt.State, c rune) {
fmt.Fprintf(f, "%v.commands[%v]", n.Parent(), printIndices(n.Indices))
}
// Format implements fmt.Formatter to print the path.
func (n Commands) Format(f fmt.State, c rune) {
fmt.Fprintf(f, "%v.commands[%v-%v]", n.Parent(), printIndices(n.From), printIndices(n.To))
}
// Format implements fmt.Formatter to print the path.
func (n CommandTree) Format(f fmt.State, c rune) { fmt.Fprintf(f, "%v.command-tree", n.Capture) }
// Format implements fmt.Formatter to print the path.
func (n CommandTreeNode) Format(f fmt.State, c rune) {
fmt.Fprintf(f, "command-tree<%v>[%v]", n.Tree, printIndices(n.Indices))
}
// Format implements fmt.Formatter to print the path.
func (n CommandTreeNodeForCommand) Format(f fmt.State, c rune) {
fmt.Fprintf(f, "%v.command-tree-node<%v>", n.Command, n.Tree)
}
// Format implements fmt.Formatter to print the path.
func (n Context) Format(f fmt.State, c rune) { fmt.Fprintf(f, "%v.[%x]", n.Parent(), n.ID) }
// Format implements fmt.Formatter to print the path.
func (n Contexts) Format(f fmt.State, c rune) { fmt.Fprintf(f, "%v.contexts", n.Parent()) }
// Format implements fmt.Formatter to print the path.
func (n Device) Format(f fmt.State, c rune) { fmt.Fprintf(f, "device<%x>", n.ID) }
// Format implements fmt.Formatter to print the path.
func (n Events) Format(f fmt.State, c rune) { fmt.Fprintf(f, "%v.events", n.Parent()) }
// Format implements fmt.Formatter to print the path.
func (n Field) Format(f fmt.State, c rune) { fmt.Fprintf(f, "%v.%v", n.Parent(), n.Name) }
// Format implements fmt.Formatter to print the path.
func (n GlobalState) Format(f fmt.State, c rune) { fmt.Fprintf(f, "%v.global-state", n.Parent()) }
// Format implements fmt.Formatter to print the path.
func (n ImageInfo) Format(f fmt.State, c rune) { fmt.Fprintf(f, "image-info<%x>", n.ID) }
// Format implements fmt.Formatter to print the path.
func (n MapIndex) Format(f fmt.State, c rune) { fmt.Fprintf(f, "%v[%x]", n.Parent(), n.Key) }
// Format implements fmt.Formatter to print the path.
func (n Memory) Format(f fmt.State, c rune) { fmt.Fprintf(f, "%v.memory-after", n.Parent()) }
// Format implements fmt.Formatter to print the message path.
func (n Messages) Format(f fmt.State, c rune) { fmt.Fprintf(f, "%v.messages", n.Parent()) }
// Format implements fmt.Formatter to print the path.
func (n Mesh) Format(f fmt.State, c rune) { fmt.Fprintf(f, "%v.mesh", n.Parent()) }
// Format implements fmt.Formatter to print the path.
func (n Parameter) Format(f fmt.State, c rune) { fmt.Fprintf(f, "%v.%v", n.Parent(), n.Name) }
// Format implements fmt.Formatter to print the path.
func (n Report) Format(f fmt.State, c rune) { fmt.Fprintf(f, "%v.report", n.Parent()) }
// Format implements fmt.Formatter to print the path.
func (n ResourceData) Format(f fmt.State, c rune) {
fmt.Fprintf(f, "%v.resource-data<%x>", n.Parent(), n.ID)
}
// Format implements fmt.Formatter to print the path.
func (n MultiResourceData) Format(f fmt.State, c rune) {
fmt.Fprintf(f, "%v.resource-data<%x>", n.Parent(), n.IDs)
}
// Format implements fmt.Formatter to print the path.
func (n Resources) Format(f fmt.State, c rune) { fmt.Fprintf(f, "%v.resources", n.Parent()) }
// Format implements fmt.Formatter to print the path.
func (n Result) Format(f fmt.State, c rune) { fmt.Fprintf(f, "%v.result", n.Parent()) }
// Format implements fmt.Formatter to print the path.
func (n Slice) Format(f fmt.State, c rune) {
fmt.Fprintf(f, "%v[%v:%v]", n.Parent(), n.Start, n.End)
}
// Format implements fmt.Formatter to print the path.
func (n State) Format(f fmt.State, c rune) {
fmt.Fprintf(f, "%v.state<context: %v>", n.Parent(), n.Context)
}
// Format implements fmt.Formatter to print the path.
func (n StateTree) Format(f fmt.State, c rune) { fmt.Fprintf(f, "%v.tree", n.State) }
// Format implements fmt.Formatter to print the path.
func (n StateTreeNode) Format(f fmt.State, c rune) {
fmt.Fprintf(f, "state-tree<%v>[%v]", n.Tree, printIndices(n.Indices))
}
// Format implements fmt.Formatter to print the path.
func (n StateTreeNodeForPath) Format(f fmt.State, c rune) {
fmt.Fprintf(f, "state-tree-for<%v, %v>", n.Tree, n.Member)
}
// Format implements fmt.Formatter to print the path.
func (n Stats) Format(f fmt.State, c rune) { fmt.Fprintf(f, "%v.stats", n.Parent()) }
// Format implements fmt.Formatter to print the path.
func (n Thumbnail) Format(f fmt.State, c rune) { fmt.Fprintf(f, "%v.thumbnail", n.Parent()) }
func (n *As) SetParent(p Node) {
switch p := p.(type) {
case nil:
n.From = nil
case *Field:
n.From = &As_Field{p}
case *Slice:
n.From = &As_Slice{p}
case *ArrayIndex:
n.From = &As_ArrayIndex{p}
case *MapIndex:
n.From = &As_MapIndex{p}
case *ImageInfo:
n.From = &As_ImageInfo{p}
case *ResourceData:
n.From = &As_ResourceData{p}
case *Mesh:
n.From = &As_Mesh{p}
default:
panic(fmt.Errorf("Cannot set As.From to %T", p))
}
}
func (n *ArrayIndex) SetParent(p Node) {
switch p := p.(type) {
case nil:
n.Array = nil
case *Field:
n.Array = &ArrayIndex_Field{p}
case *Slice:
n.Array = &ArrayIndex_Slice{p}
case *ArrayIndex:
n.Array = &ArrayIndex_ArrayIndex{p}
case *MapIndex:
n.Array = &ArrayIndex_MapIndex{p}
case *Parameter:
n.Array = &ArrayIndex_Parameter{p}
case *Report:
n.Array = &ArrayIndex_Report{p}
default:
panic(fmt.Errorf("Cannot set ArrayIndex.Array to %T", p))
}
}
func (n *Field) SetParent(p Node) {
switch p := p.(type) {
case nil:
n.Struct = nil
case *Field:
n.Struct = &Field_Field{p}
case *GlobalState:
n.Struct = &Field_GlobalState{p}
case *Slice:
n.Struct = &Field_Slice{p}
case *ArrayIndex:
n.Struct = &Field_ArrayIndex{p}
case *MapIndex:
n.Struct = &Field_MapIndex{p}
case *Parameter:
n.Struct = &Field_Parameter{p}
case *State:
n.Struct = &Field_State{p}
default:
panic(fmt.Errorf("Cannot set Field.Struct to %T", p))
}
}
func (n *MapIndex) SetParent(p Node) {
switch p := p.(type) {
case nil:
n.Map = nil
case *Field:
n.Map = &MapIndex_Field{p}
case *Slice:
n.Map = &MapIndex_Slice{p}
case *ArrayIndex:
n.Map = &MapIndex_ArrayIndex{p}
case *MapIndex:
n.Map = &MapIndex_MapIndex{p}
case *Parameter:
n.Map = &MapIndex_Parameter{p}
case *State:
n.Map = &MapIndex_State{p}
default:
panic(fmt.Errorf("Cannot set MapIndex.Map to %T", p))
}
}
func (n *Mesh) SetParent(p Node) {
switch p := p.(type) {
case nil:
n.Object = nil
case *Command:
n.Object = &Mesh_Command{p}
case *CommandTreeNode:
n.Object = &Mesh_CommandTreeNode{p}
default:
panic(fmt.Errorf("Cannot set Mesh.Object to %T", p))
}
}
func (n *Slice) SetParent(p Node) {
switch p := p.(type) {
case nil:
n.Array = nil
case *Field:
n.Array = &Slice_Field{p}
case *Slice:
n.Array = &Slice_Slice{p}
case *ArrayIndex:
n.Array = &Slice_ArrayIndex{p}
case *MapIndex:
n.Array = &Slice_MapIndex{p}
case *Parameter:
n.Array = &Slice_Parameter{p}
default:
panic(fmt.Errorf("Cannot set Slice.Array to %T", p))
}
}
func (n *Thumbnail) SetParent(p Node) {
switch p := p.(type) {
case nil:
n.Object = nil
case *ResourceData:
n.Object = &Thumbnail_Resource{p}
case *Command:
n.Object = &Thumbnail_Command{p}
case *CommandTreeNode:
n.Object = &Thumbnail_CommandTreeNode{p}
default:
panic(fmt.Errorf("Cannot set Thumbnail.Object to %T", p))
}
}
func oneOfNode(v interface{}) Node {
return protoutil.OneOf(v).(Node)
}
// Node returns the path node for p.
func (p *Any) Node() Node { return oneOfNode(p.Path) }
// Format implements fmt.Formatter to print the path.
func (p *Any) Format(f fmt.State, c rune) {
fmt.Fprint(f, p.Node())
}
// FindCommand traverses the path nodes looking for a Command path node.
// If no Command path node was found then nil is returned.
func FindCommand(n Node) *Command {
for n != nil {
if c, ok := n.(*Command); ok {
return c
}
n = n.Parent()
}
return nil
}
// FindCapture traverses the path nodes looking for a Capture path node.
// If no Capture path node was found then nil is returned.
func FindCapture(n Node) *Capture {
for n != nil {
if c, ok := n.(*Capture); ok {
return c
}
n = n.Parent()
}
return nil
}
// NewAPI returns a new API path node with the given ID.
func NewAPI(id id.ID) *API {
return &API{ID: NewID(id)}
}
// NewCapture returns a new Capture path node with the given ID.
func NewCapture(id id.ID) *Capture {
return &Capture{ID: NewID(id)}
}
// NewDevice returns a new Device path node with the given ID.
func NewDevice(id id.ID) *Device {
return &Device{ID: NewID(id)}
}
// NewBlob returns a new Blob path node with the given ID.
func NewBlob(id id.ID) *Blob {
return &Blob{ID: NewID(id)}
}
// NewImageInfo returns a new ImageInfo path node with the given ID.
func NewImageInfo(id id.ID) *ImageInfo {
return &ImageInfo{ID: image.NewID(id)}
}
// NewField returns a new Field path.
func NewField(name string, s Node) *Field {
out := &Field{Name: name}
out.SetParent(s)
return out
}
// NewArrayIndex returns a new ArrayIndex path.
func NewArrayIndex(idx uint64, a Node) *ArrayIndex {
out := &ArrayIndex{Index: idx}
out.SetParent(a)
return out
}
// NewMapIndex returns a new MapIndex path.
func NewMapIndex(k interface{}, m Node) *MapIndex {
if v := box.NewValue(k); v != nil {
out := &MapIndex{Key: &MapIndex_Box{v}}
out.SetParent(m)
return out
}
panic(fmt.Errorf("Cannot set MapIndex.Key to %T", k))
}
// NewSlice returns a new Slice path.
func NewSlice(s, e uint64, n Node) *Slice {
out := &Slice{Start: s, End: e}
out.SetParent(n)
return out
}
// ConstantSet returns a path to the API's i'th ConstantSet.
func (n *API) ConstantSet(i int) *ConstantSet {
return &ConstantSet{
API: n,
Index: uint32(i),
}
}
// Resources returns the path node to the capture's resources.
func (n *Capture) Resources() *Resources {
return &Resources{Capture: n}
}
// Report returns the path node to the capture's report.
func (n *Capture) Report(d *Device, f *CommandFilter, display bool) *Report {
return &Report{Capture: n, Device: d, Filter: f, DisplayToSurface: display}
}
// Contexts returns the path node to the capture's contexts.
func (n *Capture) Contexts() *Contexts {
return &Contexts{Capture: n}
}
// Messages returns the path node to the capture's messages.
func (n *Capture) Messages() *Messages {
return &Messages{Capture: n}
}
// Commands returns the path node to the capture's commands.
func (n *Capture) Commands() *Commands {
return &Commands{
Capture: n,
From: []uint64{0},
To: []uint64{math.MaxUint64},
}
}
// CommandRange returns the path node to a range of the capture's commands.
func (n *Capture) CommandRange(from, to uint64) *Commands {
return &Commands{
Capture: n,
From: []uint64{from},
To: []uint64{to},
}
}
// SubCommandRange returns the path node to a range of the capture's subcommands
func (n *Capture) SubCommandRange(from, to []uint64) *Commands {
return &Commands{
Capture: n,
From: append([]uint64{}, from...),
To: append([]uint64{}, to...),
}
}
// CommandTree returns the path to the root node of a capture's command tree
// optionally filtered by f.
func (n *Capture) CommandTree(f *CommandFilter) *CommandTree {
return &CommandTree{Capture: n, Filter: f}
}
// Child returns the path to the i'th child of the CommandTreeNode.
func (n *CommandTreeNode) Child(i uint64) *CommandTreeNode {
newIndices := make([]uint64, len(n.Indices)+1)
copy(newIndices, n.Indices)
newIndices[len(n.Indices)] = i
return &CommandTreeNode{Tree: n.Tree, Indices: newIndices}
}
// Command returns the path node to a single command in the capture.
func (n *Capture) Command(i uint64, subidx ...uint64) *Command {
indices := append([]uint64{i}, subidx...)
return &Command{Capture: n, Indices: indices}
}
// Context returns the path node to the a context with the given ID.
func (n *Capture) Context(id id.ID) *Context {
return &Context{Capture: n, ID: NewID(id)}
}
// Thread returns the path node to the thread with the given ID.
func (n *Capture) Thread(id uint64) *Thread {
return &Thread{Capture: n, ID: id}
}
// MemoryAfter returns the path node to the memory after this command.
func (n *Command) MemoryAfter(pool uint32, addr, size uint64) *Memory {
return &Memory{Address: addr, Size: size, Pool: pool, After: n}
}
// ResourceAfter returns the path node to the resource with the given identifier
// after this command.
func (n *Command) ResourceAfter(id *ID) *ResourceData {
return &ResourceData{
ID: id,
After: n,
}
}
// ResourcesAfter returns the path node to the resources with the given
// identifiers after this command.
func (n *Command) ResourcesAfter(ids []*ID) *MultiResourceData {
return &MultiResourceData{
IDs: ids,
After: n,
}
}
// FramebufferObservation returns the path node to framebuffer observation
// after this command.
func (n *Command) FramebufferObservation() *FramebufferObservation {
return &FramebufferObservation{Command: n}
}
// Mesh returns the path node to the mesh of this command.
func (n *Command) Mesh(options *MeshOptions) *Mesh {
return &Mesh{
Options: options,
Object: &Mesh_Command{n},
}
}
// NewMeshOptions returns a new MeshOptions object.
func NewMeshOptions(faceted bool) *MeshOptions {
return &MeshOptions{
Faceted: faceted,
}
}
// Hints returns the vertex semantics hints from the mesh options.
func (o *MeshOptions) Hints() map[string]vertex.Semantic_Type {
m := map[string]vertex.Semantic_Type{}
if o == nil {
return m
}
for _, hint := range o.VertexSemantics {
m[hint.Name] = hint.Type
}
return m
}
// GlobalStateAfter returns the path node to the state after this command.
func (n *Command) GlobalStateAfter() *GlobalState {
return &GlobalState{After: n}
}
// StateAfter returns the path node to the state after this command.
func (n *Command) StateAfter() *State {
return &State{After: n}
}
// First returns the path to the first command.
func (n *Commands) First() *Command {
return &Command{Capture: n.Capture, Indices: n.From}
}
// Last returns the path to the last command.
func (n *Commands) Last() *Command {
return &Command{Capture: n.Capture, Indices: n.To}
}
// Index returns the path to the i'th child of the StateTreeNode.
func (n *StateTreeNode) Index(i ...uint64) *StateTreeNode {
newIndices := make([]uint64, len(n.Indices)+len(i))
copy(newIndices, n.Indices)
copy(newIndices[len(n.Indices):], i)
return &StateTreeNode{Tree: n.Tree, Indices: newIndices}
}
// Parameter returns the path node to the parameter with the given name.
func (n *Command) Parameter(name string) *Parameter {
return &Parameter{Name: name, Command: n}
}
// Result returns the path node to the command's result.
func (n *Command) Result() *Result {
return &Result{Command: n}
}
// Tree returns the path node to the state tree for this state.
func (n *State) Tree() *StateTree {
return &StateTree{State: n}
}
func (n *GlobalState) Field(name string) *Field { return NewField(name, n) }
func (n *State) Field(name string) *Field { return NewField(name, n) }
func (n *Parameter) ArrayIndex(i uint64) *ArrayIndex { return NewArrayIndex(i, n) }
func (n *Parameter) Field(name string) *Field { return NewField(name, n) }
func (n *Parameter) MapIndex(k interface{}) *MapIndex { return NewMapIndex(k, n) }
func (n *Parameter) Slice(s, e uint64) *Slice { return NewSlice(s, e, n) }
func (n *Field) ArrayIndex(i uint64) *ArrayIndex { return NewArrayIndex(i, n) }
func (n *Field) Field(name string) *Field { return NewField(name, n) }
func (n *Field) MapIndex(k interface{}) *MapIndex { return NewMapIndex(k, n) }
func (n *Field) Slice(s, e uint64) *Slice { return NewSlice(s, e, n) }
func (n *MapIndex) ArrayIndex(i uint64) *ArrayIndex { return NewArrayIndex(i, n) }
func (n *MapIndex) Field(name string) *Field { return NewField(name, n) }
func (n *MapIndex) MapIndex(k interface{}) *MapIndex { return NewMapIndex(k, n) }
func (n *MapIndex) Slice(s, e uint64) *Slice { return NewSlice(s, e, n) }
func (n *Slice) ArrayIndex(i uint64) *ArrayIndex { return NewArrayIndex(i, n) }
func (n *Slice) Slice(s, e uint64) *Slice { return NewSlice(s, e, n) }
func (n *MapIndex) KeyValue() interface{} {
switch k := protoutil.OneOf(n.Key).(type) {
case nil:
return nil
case *box.Value:
return k.Get()
default:
panic(fmt.Errorf("Unsupport MapIndex key type %T", k))
}
}
// As requests the ImageInfo converted to the specified format.
func (n *ImageInfo) As(f *image.Format) *As {
return &As{
To: &As_ImageFormat{f},
From: &As_ImageInfo{n},
}
}
// ToList unchains the parents of each node, returning them as a list, starting
// with the root node.
func ToList(n Node) []Node {
out := []Node{}
for ; n != nil; n = n.Parent() {
out = append(out, n)
}
slice.Reverse(out)
return out
}
// HasRoot returns true iff p starts with root, using equal as the node
// comparision function.
func HasRoot(p, root Node) (res bool) {
a, b := ToList(p), ToList(root)
if len(b) > len(a) {
return false
}
for i, n := range b {
if !ShallowEqual(n, a[i]) {
return false
}
}
return true
}
// ShallowEqual returns true if paths a and b are equal (ignoring parents).
func ShallowEqual(a, b Node) bool {
a, b = proto.Clone(a.(proto.Message)).(Node), proto.Clone(b.(proto.Message)).(Node)
a.SetParent(nil)
b.SetParent(nil)
return reflect.DeepEqual(a, b)
}
func (i *ID) SameAs(o *ID) bool {
if i == nil || o == nil {
return false
}
if len(i.Data) != len(o.Data) {
return false
}
for ii := range i.Data {
if i.Data[ii] != o.Data[ii] {
return false
}
}
return true
}