forked from google/gapid
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcmds.go
265 lines (224 loc) · 9.1 KB
/
cmds.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
// 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 testcmd provides fake commands used for testing.
package testcmd
import (
"context"
"reflect"
"github.com/google/gapid/core/data"
"github.com/google/gapid/core/data/deep"
"github.com/google/gapid/core/data/protoconv"
"github.com/google/gapid/core/image"
"github.com/google/gapid/core/os/device"
"github.com/google/gapid/gapil/constset"
"github.com/google/gapid/gapis/api"
"github.com/google/gapid/gapis/api/testcmd/test_pb"
"github.com/google/gapid/gapis/memory"
"github.com/google/gapid/gapis/replay/builder"
"github.com/google/gapid/gapis/service/box"
)
type A struct {
ID api.CmdID
Flags api.CmdFlags
}
func (a *A) Caller() api.CmdID { return api.CmdNoID }
func (a *A) SetCaller(api.CmdID) {}
func (a *A) Thread() uint64 { return 1 }
func (a *A) SetThread(uint64) {}
func (a *A) CmdName() string { return "A" }
func (a *A) API() api.API { return nil }
func (a *A) CmdFlags(context.Context, api.CmdID, *api.GlobalState) api.CmdFlags { return a.Flags }
func (a *A) Extras() *api.CmdExtras { return nil }
func (a *A) Mutate(context.Context, api.CmdID, *api.GlobalState, *builder.Builder) error {
return nil
}
type B struct {
ID api.CmdID
Bool bool
}
func (*B) Caller() api.CmdID { return api.CmdNoID }
func (*B) SetCaller(api.CmdID) {}
func (*B) Thread() uint64 { return 1 }
func (*B) SetThread(uint64) {}
func (*B) CmdName() string { return "B" }
func (*B) API() api.API { return nil }
func (*B) CmdFlags(context.Context, api.CmdID, *api.GlobalState) api.CmdFlags { return 0 }
func (*B) Extras() *api.CmdExtras { return nil }
func (*B) Mutate(context.Context, api.CmdID, *api.GlobalState, *builder.Builder) error {
return nil
}
type (
Pointer uint64
StringːString struct{ M map[string]string }
IntːStructPtr struct{ M map[int]*Struct }
RawIntːStructPtr map[string]string
Struct struct {
Str string
Ref *Struct
}
)
var _ data.Assignable = &StringːString{}
// Get returns the value of the entry with the given key.
func (m StringːString) Get(k string) string { return m.M[k] }
// Add inserts the key-value pair, replacing any existing entry with the same
// key.
func (m StringːString) Add(k, v string) { m.M[k] = v }
// Lookup searches for the value of the entry with the given key.
func (m StringːString) Lookup(k string) (val string, ok bool) { val, ok = m.M[k]; return }
// Contains returns true if the dictionary contains an entry with the given
// key.
func (m StringːString) Contains(k string) bool { _, ok := m.M[k]; return ok }
// Remove removes the entry with the given key. If no entry with the given
// key exists then this call is a no-op.
func (m StringːString) Remove(k string) { delete(m.M, k) }
// Len returns the number of entries in the dictionary.
func (m StringːString) Len() int { return len(m.M) }
// Keys returns all the entry keys in the map.
func (m StringːString) Keys() []string {
out := make([]string, 0, len(m.M))
for k := range m.M {
out = append(out, k)
}
return out
}
func (m *StringːString) Assign(v interface{}) bool {
m.M = map[string]string{}
return deep.Copy(&m.M, v) == nil
}
var _ data.Assignable = &IntːStructPtr{}
// Get returns the value of the entry with the given key.
func (m IntːStructPtr) Get(k int) *Struct { return m.M[k] }
// Add inserts the key-value pair, replacing any existing entry with the same
// key.
func (m IntːStructPtr) Add(k int, v *Struct) { m.M[k] = v }
// Lookup searches for the value of the entry with the given key.
func (m IntːStructPtr) Lookup(k int) (val *Struct, ok bool) { val, ok = m.M[k]; return }
// Contains returns true if the dictionary contains an entry with the given
// key.
func (m IntːStructPtr) Contains(k int) bool { _, ok := m.M[k]; return ok }
// Remove removes the entry with the given key. If no entry with the given
// key exists then this call is a no-op.
func (m IntːStructPtr) Remove(k int) { delete(m.M, k) }
// Len returns the number of entries in the dictionary.
func (m IntːStructPtr) Len() int { return len(m.M) }
// Keys returns all the entry keys in the map.
func (m IntːStructPtr) Keys() []int {
out := make([]int, 0, len(m.M))
for k := range m.M {
out = append(out, k)
}
return out
}
func (m *IntːStructPtr) Assign(v interface{}) bool {
m.M = map[int]*Struct{}
return deep.Copy(&m.M, v) == nil
}
// Interface compliance checks
var (
_ memory.Pointer = Pointer(0)
_ data.Assignable = (*Pointer)(nil)
)
func (p Pointer) String() string { return memory.PointerToString(p) }
func (p Pointer) IsNullptr() bool { return p == 0 }
func (p Pointer) APointer() { return }
func (p Pointer) Address() uint64 { return uint64(p) }
func (p Pointer) Offset(n uint64) memory.Pointer { panic("not implemented") }
func (p Pointer) ElementSize(m *device.MemoryLayout) uint64 { return 1 }
func (p Pointer) ElementType() reflect.Type { return reflect.TypeOf(byte(0)) }
func (p Pointer) ISlice(start, end uint64, m *device.MemoryLayout) memory.Slice {
panic("not implemented")
}
func (p *Pointer) Assign(o interface{}) bool {
if o, ok := o.(memory.Pointer); ok {
*p = Pointer(o.Address())
return true
}
return false
}
type X struct {
Str string `param:"Str"`
Sli []bool `param:"Sli"`
Ref *Struct `param:"Ref"`
Ptr Pointer `param:"Ptr"`
Map StringːString `param:"Map"`
PMap IntːStructPtr `param:"PMap"`
RMap RawIntːStructPtr `param:"RMap"`
}
func (X) Caller() api.CmdID { return api.CmdNoID }
func (X) SetCaller(api.CmdID) {}
func (X) Thread() uint64 { return 1 }
func (X) SetThread(uint64) {}
func (X) CmdName() string { return "X" }
func (X) API() api.API { return api.Find(APIID) }
func (X) CmdFlags(context.Context, api.CmdID, *api.GlobalState) api.CmdFlags { return 0 }
func (X) Extras() *api.CmdExtras { return nil }
func (X) Mutate(context.Context, api.CmdID, *api.GlobalState, *builder.Builder) error {
return nil
}
type API struct{}
func (API) Name() string { return "foo" }
func (API) ID() api.ID { return APIID }
func (API) Index() uint8 { return 15 }
func (API) ConstantSets() *constset.Pack { return nil }
func (API) GetFramebufferAttachmentInfo(
ctx context.Context,
after []uint64,
state *api.GlobalState,
thread uint64,
attachment api.FramebufferAttachment) (api.FramebufferAttachmentInfo, err error) {
return api.FramebufferAttachmentInfo{}, nil
}
func (API) Context(*api.GlobalState, uint64) api.Context { return nil }
func (API) CreateCmd(name string) api.Cmd {
switch name {
case "X":
return &X{}
default:
return nil
}
}
var (
APIID = api.ID{1, 2, 3}
P = &X{
Str: "aaa",
Sli: []bool{true, false, true},
Ref: &Struct{Str: "ccc", Ref: &Struct{Str: "ddd"}},
Ptr: Pointer(0x123),
Map: StringːString{map[string]string{"cat": "meow", "dog": "woof"}},
PMap: IntːStructPtr{map[int]*Struct{}},
RMap: map[string]string{"eyes": "see", "nose": "smells"},
}
Q = &X{
Str: "xyz",
Sli: []bool{false, true, false},
Ptr: Pointer(0x321),
Map: StringːString{map[string]string{"bird": "tweet", "fox": "?"}},
PMap: IntːStructPtr{map[int]*Struct{
100: &Struct{Str: "baldrick"},
}},
RMap: map[string]string{"ears": "hear", "tongue": "taste"},
}
)
func init() {
api.Register(API{})
protoconv.Register(func(ctx context.Context, a *X) (*test_pb.X, error) {
return &test_pb.X{Data: box.NewValue(*a)}, nil
}, func(ctx context.Context, b *test_pb.X) (*X, error) {
var a X
if err := b.Data.AssignTo(&a); err != nil {
return nil, err
}
return &a, nil
})
}