-
Notifications
You must be signed in to change notification settings - Fork 0
/
contexts.go
169 lines (148 loc) · 4.29 KB
/
contexts.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
// 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 resolve
import (
"context"
"fmt"
"reflect"
"sort"
"github.com/google/gapid/core/data/id"
"github.com/google/gapid/gapis/api"
"github.com/google/gapid/gapis/capture"
"github.com/google/gapid/gapis/database"
"github.com/google/gapid/gapis/extensions"
"github.com/google/gapid/gapis/messages"
"github.com/google/gapid/gapis/service"
"github.com/google/gapid/gapis/service/path"
)
// Contexts resolves the list of contexts belonging to a capture.
func Contexts(ctx context.Context, p *path.Contexts, r *path.ResolveConfig) ([]*api.ContextInfo, error) {
obj, err := database.Build(ctx, &ContextListResolvable{Capture: p.Capture, Config: r})
if err != nil {
return nil, err
}
return obj.([]*api.ContextInfo), nil
}
// ContextsByID resolves the list of contexts belonging to a capture.
func ContextsByID(ctx context.Context, p *path.Contexts, r *path.ResolveConfig) (map[api.ContextID]*api.ContextInfo, error) {
ctxs, err := Contexts(ctx, p, r)
if err != nil {
return nil, err
}
out := map[api.ContextID]*api.ContextInfo{}
for _, c := range ctxs {
out[c.ID] = c
}
return out, nil
}
// Context resolves the single context.
func Context(ctx context.Context, p *path.Context, r *path.ResolveConfig) (*api.ContextInfo, error) {
contexts, err := Contexts(ctx, p.Capture.Contexts(), r)
if err != nil {
return nil, err
}
id := api.ContextID(p.ID.ID())
for _, c := range contexts {
if c.ID == id {
return c, nil
}
}
return nil, &service.ErrInvalidPath{
Reason: messages.ErrContextDoesNotExist(p.ID),
Path: p.Path(),
}
}
// Importance is the interface implemeneted by commands that provide an
// "importance score". This value is used to prioritize contexts.
type Importance interface {
Importance() int
}
// Named is the interface implemented by context that have a name.
type Named interface {
Name() string
}
// Resolve implements the database.Resolver interface.
func (r *ContextListResolvable) Resolve(ctx context.Context) (interface{}, error) {
ctx = SetupContext(ctx, r.Capture, r.Config)
c, err := capture.ResolveGraphics(ctx)
if err != nil {
return nil, err
}
type ctxInfo struct {
ctx api.Context
cnts map[reflect.Type]int
pri int
}
seen := map[api.ContextID]int{}
contexts := []*ctxInfo{}
s := c.NewState(ctx)
err = api.ForeachCmd(ctx, c.Commands, true, func(ctx context.Context, i api.CmdID, cmd api.Cmd) error {
if err := cmd.Mutate(ctx, i, s, nil, nil); err != nil {
return fmt.Errorf("Fail to mutate command %v: %v", cmd, err)
}
api := cmd.API()
if api == nil {
return nil
}
context := api.Context(ctx, s, cmd.Thread())
if context == nil {
return nil
}
id := context.ID()
idx, ok := seen[id]
if !ok {
idx = len(contexts)
seen[id] = idx
contexts = append(contexts, &ctxInfo{
ctx: context,
cnts: map[reflect.Type]int{},
})
}
c := contexts[idx]
cmdTy := reflect.TypeOf(cmd)
c.cnts[cmdTy] = c.cnts[cmdTy] + 1
if i, ok := cmd.(Importance); ok {
c.pri += i.Importance()
}
return nil
})
if err != nil {
return nil, err
}
sort.Slice(contexts, func(i, j int) bool {
return contexts[i].pri > contexts[j].pri
})
out := make([]*api.ContextInfo, len(contexts))
for i, c := range contexts {
name := fmt.Sprintf("Context %v", i)
if n, ok := c.ctx.(Named); ok {
name = n.Name()
}
out[i] = &api.ContextInfo{
Path: r.Capture.Context(id.ID(c.ctx.ID())),
ID: c.ctx.ID(),
API: c.ctx.API().ID(),
NumCommandsByType: c.cnts,
Name: name,
Priority: len(contexts) - i,
UserData: map[interface{}]interface{}{},
}
}
for _, e := range extensions.Get() {
if e.AdjustContexts != nil {
e.AdjustContexts(ctx, out)
}
}
return out, nil
}