| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | // Copyright 2019 Juan Palacios <[email protected]> |
| 3 | |
| 4 | #include "gpuxmlparser.h" |
| 5 | |
| 6 | #include "core/profilepartxmlparserprovider.h" |
| 7 | #include "igpu.h" |
| 8 | #include <utility> |
| 9 | |
| 10 | class GPUXMLParser::Factory final |
| 11 | : public ProfilePartXMLParser::Factory |
| 12 | , public IGPUProfilePart::Exporter |
| 13 | { |
| 14 | public: |
| 15 | Factory(IProfilePartXMLParserProvider const &profilePartParserProvider, |
| 16 | GPUXMLParser &parser) noexcept |
| 17 | : ProfilePartXMLParser::Factory(profilePartParserProvider) |
| 18 | , outer_(parser) |
| 19 | { |
| 20 | } |
| 21 | |
| 22 | void takePartParser(Item const &i, |
| 23 | std::unique_ptr<IProfilePartXMLParser> &&part) override; |
| 24 | |
| 25 | std::optional<std::reference_wrapper<Exportable::Exporter>> |
| 26 | provideExporter(Item const &i) override; |
| 27 | |
| 28 | void takeActive(bool) override |
| 29 | { |
| 30 | } |
| 31 | |
| 32 | void takeIndex(int) override |
| 33 | { |
| 34 | } |
| 35 | |
| 36 | void takeDeviceID(std::string const &) override |
| 37 | { |
| 38 | } |
| 39 | |
| 40 | void takeRevision(std::string const &) override |
| 41 | { |
| 42 | } |
| 43 | |
| 44 | void takeUniqueID(std::optional<std::string>) override |
| 45 | { |
| 46 | } |
| 47 | |
| 48 | private: |
| 49 | GPUXMLParser &outer_; |
| 50 | }; |
| 51 | |
| 52 | void GPUXMLParser::Factory::takePartParser( |
| 53 | Item const &i, std::unique_ptr<IProfilePartXMLParser> &&part) |
| 54 | { |
| 55 | outer_.parsers_.emplace(i.ID(), std::move(part)); |
| 56 | } |
| 57 | |
| 58 | std::optional<std::reference_wrapper<Exportable::Exporter>> |
| 59 | GPUXMLParser::Factory::provideExporter(Item const &i) |
| 60 | { |
| 61 | if (i.ID() == IGPU::ItemID) |
| 62 | return *this; |
| 63 | else |
| 64 | return factory(i); |
| 65 | } |
| 66 | |
| 67 | class GPUXMLParser::Initializer final : public IGPUProfilePart::Exporter |
| 68 | { |
| 69 | public: |
| 70 | Initializer(GPUXMLParser &outer) noexcept |
| 71 | : outer_(outer) |
| 72 | { |
| 73 | } |
| 74 | |
| 75 | std::optional<std::reference_wrapper<Exportable::Exporter>> |
| 76 | provideExporter(Item const &i) override; |
| 77 | |
| 78 | void takeActive(bool active) override; |
| 79 | void takeIndex(int index) override; |
| 80 | void takeDeviceID(std::string const &deviceID) override; |
| 81 | void takeRevision(std::string const &revision) override; |
| 82 | void takeUniqueID(std::optional<std::string> uniqueID) override; |
| 83 | |
| 84 | private: |
| 85 | GPUXMLParser &outer_; |
| 86 | std::unordered_map<std::string, std::unique_ptr<Exportable::Exporter>> initializers_; |
| 87 | }; |
| 88 | |
| 89 | std::optional<std::reference_wrapper<Exportable::Exporter>> |
| 90 | GPUXMLParser::Initializer::provideExporter(Item const &i) |
| 91 | { |
| 92 | auto &id = i.ID(); |
| 93 | if (outer_.parsers_.count(id) > 0) { |
| 94 | if (initializers_.count(id) > 0) |
| 95 | return *initializers_.at(id); |
| 96 | else { |
| 97 | auto initializer = outer_.parsers_.at(id)->initializer(); |
| 98 | if (initializer != nullptr) { |
| 99 | initializers_.emplace(id, std::move(initializer)); |
| 100 | return *initializers_.at(id); |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | return {}; |
| 106 | } |
| 107 | |
| 108 | void GPUXMLParser::Initializer::takeActive(bool active) |
| 109 | { |
| 110 | outer_.active_ = outer_.activeDefault_ = active; |
| 111 | } |
| 112 | |
| 113 | void GPUXMLParser::Initializer::takeIndex(int index) |
| 114 | { |
| 115 | outer_.index_ = outer_.indexDefault_ = index; |
| 116 | } |
| 117 | |
| 118 | void GPUXMLParser::Initializer::takeDeviceID(std::string const &deviceID) |
| 119 | { |
| 120 | outer_.deviceID_ = outer_.deviceIDDefault_ = deviceID; |
| 121 | } |
| 122 | |
| 123 | void GPUXMLParser::Initializer::takeRevision(std::string const &revision) |
| 124 | { |
| 125 | outer_.revision_ = outer_.revisionDefault_ = revision; |
| 126 | } |
| 127 | |
| 128 | void GPUXMLParser::Initializer::takeUniqueID(std::optional<std::string> uniqueID) |
| 129 | { |
| 130 | outer_.uniqueID_ = outer_.uniqueIDDefault_ = uniqueID; |
| 131 | } |
| 132 | |
| 133 | GPUXMLParser::GPUXMLParser() noexcept |
| 134 | : ProfilePartXMLParser(IGPU::ItemID, *this, *this) |
| 135 | { |
| 136 | } |
| 137 | |
| 138 | std::unique_ptr<Exportable::Exporter> GPUXMLParser::factory( |
| 139 | IProfilePartXMLParserProvider const &profilePartParserProvider) |
| 140 | { |
| 141 | return std::make_unique<GPUXMLParser::Factory>(profilePartParserProvider, |
| 142 | *this); |
| 143 | } |
| 144 | |
| 145 | std::unique_ptr<Exportable::Exporter> GPUXMLParser::initializer() |
| 146 | { |
| 147 | return std::make_unique<GPUXMLParser::Initializer>(*this); |
| 148 | } |
| 149 | |
| 150 | std::optional<std::reference_wrapper<Exportable::Exporter>> |
| 151 | GPUXMLParser::provideExporter(Item const &i) |
| 152 | { |
| 153 | auto parserIt = parsers_.find(i.ID()); |
| 154 | if (parserIt != parsers_.cend()) |
| 155 | return parserIt->second->profilePartExporter(); |
| 156 | |
| 157 | return {}; |
| 158 | } |
| 159 | |
| 160 | std::optional<std::reference_wrapper<Importable::Importer>> |
| 161 | GPUXMLParser::provideImporter(Item const &i) |
| 162 | { |
| 163 | auto parserIt = parsers_.find(i.ID()); |
| 164 | if (parserIt != parsers_.cend()) |
| 165 | return parserIt->second->profilePartImporter(); |
| 166 | |
| 167 | return {}; |
| 168 | } |
| 169 | |
| 170 | void GPUXMLParser::takeActive(bool active) |
| 171 | { |
| 172 | active_ = active; |
| 173 | } |
| 174 | |
| 175 | bool GPUXMLParser::provideActive() const |
| 176 | { |
| 177 | return active_; |
| 178 | } |
| 179 | |
| 180 | void GPUXMLParser::takeIndex(int index) |
| 181 | { |
| 182 | index_ = index; |
| 183 | } |
| 184 | |
| 185 | int GPUXMLParser::provideIndex() const |
| 186 | { |
| 187 | return index_; |
| 188 | } |
| 189 | |
| 190 | void GPUXMLParser::takeDeviceID(std::string const &deviceID) |
| 191 | { |
| 192 | deviceID_ = deviceID; |
| 193 | } |
| 194 | |
| 195 | std::string const &GPUXMLParser::provideDeviceID() const |
| 196 | { |
| 197 | return deviceID_; |
| 198 | } |
| 199 | |
| 200 | void GPUXMLParser::takeRevision(std::string const &revision) |
| 201 | { |
| 202 | revision_ = revision; |
| 203 | } |
| 204 | |
| 205 | std::string const &GPUXMLParser::provideRevision() const |
| 206 | { |
| 207 | return revision_; |
| 208 | } |
| 209 | |
| 210 | void GPUXMLParser::takeUniqueID(std::optional<std::string> uniqueID) |
| 211 | { |
| 212 | uniqueID_ = uniqueID; |
| 213 | } |
| 214 | |
| 215 | std::optional<std::string> GPUXMLParser::provideUniqueID() const |
| 216 | { |
| 217 | return uniqueID_; |
| 218 | } |
| 219 | |
| 220 | void GPUXMLParser::appendTo(pugi::xml_node &parentNode) |
| 221 | { |
| 222 | auto gpuNode = parentNode.append_child(ID().c_str()); |
| 223 | gpuNode.append_attribute("active") = active_; |
| 224 | gpuNode.append_attribute("index") = index_; |
| 225 | gpuNode.append_attribute("deviceid") = deviceID_.c_str(); |
| 226 | gpuNode.append_attribute("revision") = revision_.c_str(); |
| 227 | |
| 228 | if (uniqueID_.has_value()) |
| 229 | gpuNode.append_attribute("uniqueid") = uniqueID_->c_str(); |
| 230 | |
| 231 | for (auto &[key, component] : parsers_) |
| 232 | component->appendTo(gpuNode); |
| 233 | } |
| 234 | |
| 235 | void GPUXMLParser::resetAttributes() |
| 236 | { |
| 237 | active_ = activeDefault_; |
| 238 | index_ = indexDefault_; |
| 239 | deviceID_ = deviceIDDefault_; |
| 240 | revision_ = revisionDefault_; |
| 241 | uniqueID_ = uniqueIDDefault_; |
| 242 | } |
| 243 | |
| 244 | void GPUXMLParser::loadPartFrom(pugi::xml_node const &parentNode) |
| 245 | { |
| 246 | auto gpuNode = parentNode.find_child([&](pugi::xml_node const &node) { |
| 247 | // match gpu node |
| 248 | if (node.name() != ID()) |
| 249 | return false; |
| 250 | |
| 251 | // try to match specific gpu by uniqueid attribute |
| 252 | auto uniqueID = node.attribute("uniqueid").as_string(); |
| 253 | if (*uniqueID != 0 && uniqueID_.has_value()) |
| 254 | return uniqueID_ == uniqueID; |
| 255 | |
| 256 | // match specific gpu by index, deviceid and revision attributes |
| 257 | return node.attribute("index").as_int(-1) == index_ && |
| 258 | node.attribute("deviceid").as_string() == deviceID_ && |
| 259 | node.attribute("revision").as_string() == revision_; |
| 260 | }); |
| 261 | |
| 262 | active_ = gpuNode.attribute("active").as_bool(activeDefault_); |
| 263 | |
| 264 | for (auto &[key, component] : parsers_) |
| 265 | component->loadFrom(gpuNode); |
| 266 | } |
| 267 | |
| 268 | bool const GPUXMLParser::registered_ = |
| 269 | ProfilePartXMLParserProvider::registerProvider( |
| 270 | IGPU::ItemID, []() { return std::make_unique<GPUXMLParser>(); }); |