-
Notifications
You must be signed in to change notification settings - Fork 0
/
devices.go
117 lines (103 loc) · 3.24 KB
/
devices.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
// 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 devices contains functions for gathering devices that can replay a
// capture.
package devices
import (
"context"
"fmt"
"sort"
"github.com/google/gapid/core/log"
"github.com/google/gapid/core/os/device"
"github.com/google/gapid/core/os/device/bind"
"github.com/google/gapid/gapis/api"
"github.com/google/gapid/gapis/capture"
"github.com/google/gapid/gapis/replay"
"github.com/google/gapid/gapis/service/path"
)
// ForReplay returns a priority-sorted path list of devices that are capable of
// replaying the capture c.
func ForReplay(ctx context.Context, p *path.Capture) ([]*path.Device, error) {
c, err := capture.ResolveGraphicsFromPath(ctx, p)
if err != nil {
return nil, err
}
apis := make([]replay.Support, 0, len(c.APIs))
for _, i := range c.APIs {
api := api.Find(api.ID(i.ID()))
if f, ok := api.(replay.Support); ok {
apis = append(apis, f)
}
}
all := Sorted(ctx)
filtered := make([]prioritizedDevice, 0, len(all))
for _, device := range all {
instance := device.Instance()
p := uint32(1)
for _, api := range apis {
// TODO: Check if device is a LAD, and if so filter by supportsLAD.
ctx := log.V{
"api": fmt.Sprintf("%T", api),
"device": instance.Name,
}.Bind(ctx)
priority := api.GetReplayPriority(ctx, instance, c.Header)
p = p * priority
if priority != 0 {
log.D(ctx, "Compatible %d", priority)
} else {
log.D(ctx, "Incompatible")
}
}
if p > 0 {
ctx := log.V{
"device": instance,
}.Bind(ctx)
log.D(ctx, "Priority %d", p)
filtered = append(filtered, prioritizedDevice{device, p})
}
}
sort.Sort(prioritizedDevices(filtered))
paths := make([]*path.Device, len(filtered))
for i, d := range filtered {
paths[i] = path.NewDevice(d.device.Instance().ID.ID())
}
return paths, nil
}
// Sorted returns all devices, sorted by Android first, and then Host.
func Sorted(ctx context.Context) []bind.Device {
all := bind.GetRegistry(ctx).Devices()
androidDevices := make([]bind.Device, 0, len(all))
nonAndroidDevices := make([]bind.Device, 0, len(all))
for _, dev := range all {
instance := dev.Instance()
if instance.GetConfiguration().GetOS().GetKind() == device.Android {
androidDevices = append(androidDevices, dev)
} else {
nonAndroidDevices = append(nonAndroidDevices, dev)
}
}
return append(androidDevices, nonAndroidDevices...)
}
type prioritizedDevice struct {
device bind.Device
priority uint32
}
type prioritizedDevices []prioritizedDevice
func (p prioritizedDevices) Len() int {
return len(p)
}
func (p prioritizedDevices) Less(i, j int) bool {
return p[i].priority < p[j].priority
}
func (p prioritizedDevices) Swap(i, j int) { p[i], p[j] = p[j], p[i] }