xref: /aosp_15_r20/external/mesa3d/src/tool/pps/pps_driver.cc (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Copyright © 2019-2020 Collabora, Ltd.
3  * Author: Antonio Caggiano <[email protected]>
4  * Author: Rohan Garg <[email protected]>
5  * Author: Robert Beckett <[email protected]>
6  * Author: Corentin Noël <[email protected]>
7  *
8  * SPDX-License-Identifier: MIT
9  */
10 
11 #include "pps_driver.h"
12 
13 #include <iterator>
14 #include <sstream>
15 
16 #ifdef PPS_FREEDRENO
17 #include "freedreno/ds/fd_pps_driver.h"
18 #endif // PPS_FREEDRENO
19 
20 #ifdef PPS_INTEL
21 #include "intel/ds/intel_pps_driver.h"
22 #endif // PPS_INTEL
23 
24 #ifdef PPS_PANFROST
25 #include "panfrost/ds/pan_pps_driver.h"
26 #endif // PPS_PANFROST
27 
28 #include "pps.h"
29 #include "pps_algorithm.h"
30 
31 namespace pps
32 {
create_supported_drivers()33 std::unordered_map<std::string, std::unique_ptr<Driver>> create_supported_drivers()
34 {
35    std::unordered_map<std::string, std::unique_ptr<Driver>> map;
36 
37 #ifdef PPS_FREEDRENO
38    map.emplace("msm", std::make_unique<FreedrenoDriver>());
39 #endif // PPS_FREEDRENO
40 
41 #ifdef PPS_INTEL
42    map.emplace("i915", std::make_unique<IntelDriver>());
43    map.emplace("xe", std::make_unique<IntelDriver>());
44 #endif // PPS_INTEL
45 
46 #ifdef PPS_PANFROST
47    map.emplace("panfrost", std::make_unique<PanfrostDriver>());
48 #endif // PPS_PANFROST
49 
50    return map;
51 }
52 
get_supported_drivers()53 const std::unordered_map<std::string, std::unique_ptr<Driver>> &Driver::get_supported_drivers()
54 {
55    static auto map = create_supported_drivers();
56    return map;
57 }
58 
supported_device_names()59 const std::vector<std::string> Driver::supported_device_names()
60 {
61    std::vector<std::string> supported_device_names;
62 
63    for (auto &entry : get_supported_drivers()) {
64       supported_device_names.emplace_back(entry.first);
65    }
66 
67    return supported_device_names;
68 }
69 
get_driver(DrmDevice && drm_device)70 Driver *Driver::get_driver(DrmDevice &&drm_device)
71 {
72    auto &supported_drivers = get_supported_drivers();
73    auto it = supported_drivers.find(drm_device.name);
74    if (it == std::end(supported_drivers)) {
75       PERFETTO_FATAL("Failed to find a driver for DRM device %s", drm_device.name.c_str());
76    }
77 
78    Driver *driver = it->second.get();
79    driver->drm_device = std::move(drm_device);
80    return driver;
81 }
82 
default_driver_name()83 std::string Driver::default_driver_name()
84 {
85    auto supported_devices = Driver::supported_device_names();
86    auto devices = DrmDevice::create_all();
87    for (auto &device : devices) {
88       if (CONTAINS(supported_devices, device.name)) {
89          PPS_LOG_IMPORTANT("Driver selected: %s", device.name.c_str());
90          return device.name;
91       }
92    }
93    PPS_LOG_FATAL("Failed to find any driver");
94 }
95 
find_driver_name(const char * requested)96 std::string Driver::find_driver_name(const char *requested)
97 {
98    auto supported_devices = Driver::supported_device_names();
99    auto devices = DrmDevice::create_all();
100    for (auto &device : devices) {
101       if (device.name == requested) {
102          PPS_LOG_IMPORTANT("Driver selected: %s", device.name.c_str());
103          return device.name;
104       }
105    }
106 
107    std::ostringstream drivers_os;
108    std::copy(supported_devices.begin(),
109       supported_devices.end() - 1,
110       std::ostream_iterator<std::string>(drivers_os, ", "));
111    drivers_os << supported_devices.back();
112 
113    PPS_LOG_ERROR(
114       "Device '%s' not found (supported drivers: %s)", requested, drivers_os.str().c_str());
115 
116    return default_driver_name();
117 }
118 
119 } // namespace pps
120