| // Copyright (C) 2016 The Android Open Source Project |
| // |
| // 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 gapir |
| |
| import ( |
| "sync" |
| |
| "android.googlesource.com/platform/tools/gpu/framework/id" |
| "android.googlesource.com/platform/tools/gpu/framework/log" |
| ) |
| |
| // Discovery is used to find replay devices on the local machine and connected |
| // Android devices. |
| type Discovery struct { |
| sync.Mutex |
| devices []Device |
| } |
| |
| // Device looks up the device with the specified identifier. |
| // If no device with the specified identifier was registered with the Discovery |
| // then nil is returned. |
| func (d *Discovery) Device(id id.ID) Device { |
| d.Lock() |
| defer d.Unlock() |
| for _, d := range d.devices { |
| if d.ID() == id { |
| return d |
| } |
| } |
| return nil |
| } |
| |
| // Devices returns the list of devices registered with the Discovery. |
| func (d *Discovery) Devices() []Device { |
| d.Lock() |
| defer d.Unlock() |
| |
| return append([]Device{}, d.devices...) |
| } |
| |
| // DefaultDevice returns the first device registered with the Discovery. |
| func (d *Discovery) DefaultDevice() Device { |
| d.Lock() |
| defer d.Unlock() |
| if len(d.devices) == 0 { |
| return nil |
| } |
| return d.devices[0] |
| } |
| |
| // AddDevice registers the device dev with the Discovery. |
| func (d *Discovery) AddDevice(ctx log.Context, dev Device) { |
| if dev != nil { |
| d.Lock() |
| defer d.Unlock() |
| for _, t := range d.devices { |
| if t == dev { |
| return // already added |
| } |
| } |
| ctx.Info().Log("Adding new device") |
| d.devices = append(d.devices, dev) |
| } |
| } |
| |
| // RemoveDevice unregisters the device dev with the Discovery. |
| func (d *Discovery) RemoveDevice(ctx log.Context, dev Device) { |
| if dev != nil { |
| d.Lock() |
| defer d.Unlock() |
| for i, t := range d.devices { |
| if t == dev { |
| ctx.Info().Log("Removing existing device") |
| copy(d.devices[i:], d.devices[i+1:]) |
| d.devices = d.devices[:len(d.devices)-1] |
| } |
| } |
| } |
| } |