1# Copyright 2013 The Chromium Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5class GPUDevice(object):
6  """Provides information about an individual GPU device.
7
8     On platforms which support them, the vendor_id and device_id are
9     PCI IDs. On other platforms, the vendor_string and device_string
10     are platform-dependent strings.
11  """
12
13  _VENDOR_ID_MAP = {
14    0x1002: 'ATI',
15    0x8086: 'Intel',
16    0x10de: 'Nvidia',
17    }
18
19  def __init__(self, vendor_id, device_id, vendor_string, device_string):
20    self._vendor_id = vendor_id
21    self._device_id = device_id
22    self._vendor_string = vendor_string
23    self._device_string = device_string
24
25  def __str__(self):
26    vendor = 'VENDOR = 0x%x' % self._vendor_id
27    vendor_string = self._vendor_string
28    if not vendor_string and self._vendor_id in self._VENDOR_ID_MAP:
29      vendor_string = self._VENDOR_ID_MAP[self._vendor_id]
30    if vendor_string:
31      vendor += ' (%s)' % vendor_string
32    device = 'DEVICE = 0x%x' % self._device_id
33    if self._device_string:
34      device += ' (%s)' % self._device_string
35    return '%s, %s' % (vendor, device)
36
37  @classmethod
38  def FromDict(cls, attrs):
39    """Constructs a GPUDevice from a dictionary. Requires the
40       following attributes to be present in the dictionary:
41
42         vendor_id
43         device_id
44         vendor_string
45         device_string
46
47       Raises an exception if any attributes are missing.
48    """
49    return cls(attrs['vendor_id'], attrs['device_id'],
50               attrs['vendor_string'], attrs['device_string'])
51
52  @property
53  def vendor_id(self):
54    """The GPU vendor's PCI ID as a number, or 0 if not available.
55
56       Most desktop machines supply this information rather than the
57       vendor and device strings."""
58    return self._vendor_id
59
60  @property
61  def device_id(self):
62    """The GPU device's PCI ID as a number, or 0 if not available.
63
64       Most desktop machines supply this information rather than the
65       vendor and device strings."""
66    return self._device_id
67
68  @property
69  def vendor_string(self):
70    """The GPU vendor's name as a string, or the empty string if not
71       available.
72
73       Most mobile devices supply this information rather than the PCI
74       IDs."""
75    return self._vendor_string
76
77  @property
78  def device_string(self):
79    """The GPU device's name as a string, or the empty string if not
80       available.
81
82       Most mobile devices supply this information rather than the PCI
83       IDs."""
84    return self._device_string
85