1 // Copyright (c) 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.
5 #include "gpu/config/gpu_control_list.h"
8 #include "base/json/json_reader.h"
9 #include "base/logging.h"
10 #include "base/strings/string_number_conversions.h"
11 #include "base/strings/string_split.h"
12 #include "base/strings/string_util.h"
13 #include "base/strings/stringprintf.h"
14 #include "base/sys_info.h"
15 #include "gpu/config/gpu_info.h"
16 #include "gpu/config/gpu_util.h"
21 // Break a version string into segments. Return true if each segment is
23 bool ProcessVersionString(const std::string& version_string,
25 std::vector<std::string>* version) {
27 base::SplitString(version_string, splitter, version);
28 if (version->size() == 0)
30 // If the splitter is '-', we assume it's a date with format "mm-dd-yyyy";
31 // we split it into the order of "yyyy", "mm", "dd".
32 if (splitter == '-') {
33 std::string year = (*version)[version->size() - 1];
34 for (int i = version->size() - 1; i > 0; --i) {
35 (*version)[i] = (*version)[i - 1];
39 for (size_t i = 0; i < version->size(); ++i) {
41 if (!base::StringToUint((*version)[i], &num))
47 // Compare two number strings using numerical ordering.
48 // Return 0 if number = number_ref,
49 // 1 if number > number_ref,
50 // -1 if number < number_ref.
51 int CompareNumericalNumberStrings(
52 const std::string& number, const std::string& number_ref) {
55 bool valid = base::StringToUint(number, &value1);
57 valid = base::StringToUint(number_ref, &value2);
66 // Compare two number strings using lexical ordering.
67 // Return 0 if number = number_ref,
68 // 1 if number > number_ref,
69 // -1 if number < number_ref.
70 // We only compare as many digits as number_ref contains.
71 // If number_ref is xxx, it's considered as xxx*
72 // For example: CompareLexicalNumberStrings("121", "12") returns 0,
73 // CompareLexicalNumberStrings("12", "121") returns -1.
74 int CompareLexicalNumberStrings(
75 const std::string& number, const std::string& number_ref) {
76 for (size_t i = 0; i < number_ref.length(); ++i) {
78 if (i < number.length())
79 value1 = number[i] - '0';
80 unsigned value2 = number_ref[i] - '0';
89 const char kMultiGpuStyleStringAMDSwitchable[] = "amd_switchable";
90 const char kMultiGpuStyleStringOptimus[] = "optimus";
92 const char kMultiGpuCategoryStringPrimary[] = "primary";
93 const char kMultiGpuCategoryStringSecondary[] = "secondary";
94 const char kMultiGpuCategoryStringActive[] = "active";
95 const char kMultiGpuCategoryStringAny[] = "any";
97 const char kVersionStyleStringNumerical[] = "numerical";
98 const char kVersionStyleStringLexical[] = "lexical";
100 const char kOp[] = "op";
102 } // namespace anonymous
104 GpuControlList::VersionInfo::VersionInfo(
105 const std::string& version_op,
106 const std::string& version_style,
107 const std::string& version_string,
108 const std::string& version_string2)
109 : version_style_(kVersionStyleNumerical) {
110 op_ = StringToNumericOp(version_op);
111 if (op_ == kUnknown || op_ == kAny)
113 version_style_ = StringToVersionStyle(version_style);
114 if (!ProcessVersionString(version_string, '.', &version_)) {
118 if (op_ == kBetween) {
119 if (!ProcessVersionString(version_string2, '.', &version2_))
124 GpuControlList::VersionInfo::~VersionInfo() {
127 bool GpuControlList::VersionInfo::Contains(
128 const std::string& version_string) const {
129 return Contains(version_string, '.');
132 bool GpuControlList::VersionInfo::Contains(
133 const std::string& version_string, char splitter) const {
138 std::vector<std::string> version;
139 if (!ProcessVersionString(version_string, splitter, &version))
141 int relation = Compare(version, version_, version_style_);
143 return (relation == 0);
145 return (relation < 0);
147 return (relation <= 0);
149 return (relation > 0);
151 return (relation >= 0);
155 return Compare(version, version2_, version_style_) <= 0;
158 bool GpuControlList::VersionInfo::IsValid() const {
159 return (op_ != kUnknown && version_style_ != kVersionStyleUnknown);
162 bool GpuControlList::VersionInfo::IsLexical() const {
163 return version_style_ == kVersionStyleLexical;
167 int GpuControlList::VersionInfo::Compare(
168 const std::vector<std::string>& version,
169 const std::vector<std::string>& version_ref,
170 VersionStyle version_style) {
171 DCHECK(version.size() > 0 && version_ref.size() > 0);
172 DCHECK(version_style != kVersionStyleUnknown);
173 for (size_t i = 0; i < version_ref.size(); ++i) {
174 if (i >= version.size())
177 // We assume both versions are checked by ProcessVersionString().
178 if (i > 0 && version_style == kVersionStyleLexical)
179 ret = CompareLexicalNumberStrings(version[i], version_ref[i]);
181 ret = CompareNumericalNumberStrings(version[i], version_ref[i]);
189 GpuControlList::VersionInfo::VersionStyle
190 GpuControlList::VersionInfo::StringToVersionStyle(
191 const std::string& version_style) {
192 if (version_style.empty() || version_style == kVersionStyleStringNumerical)
193 return kVersionStyleNumerical;
194 if (version_style == kVersionStyleStringLexical)
195 return kVersionStyleLexical;
196 return kVersionStyleUnknown;
199 GpuControlList::OsInfo::OsInfo(const std::string& os,
200 const std::string& version_op,
201 const std::string& version_string,
202 const std::string& version_string2) {
203 type_ = StringToOsType(os);
204 if (type_ != kOsUnknown) {
205 version_info_.reset(new VersionInfo(
206 version_op, std::string(), version_string, version_string2));
210 GpuControlList::OsInfo::~OsInfo() {}
212 bool GpuControlList::OsInfo::Contains(OsType type,
213 const std::string& version) const {
216 if (type_ != type && type_ != kOsAny)
218 return version_info_->Contains(version);
221 bool GpuControlList::OsInfo::IsValid() const {
222 return type_ != kOsUnknown && version_info_->IsValid();
225 GpuControlList::OsType GpuControlList::OsInfo::type() const {
229 GpuControlList::OsType GpuControlList::OsInfo::StringToOsType(
230 const std::string& os) {
233 else if (os == "macosx")
235 else if (os == "android")
237 else if (os == "linux")
239 else if (os == "chromeos")
241 else if (os == "any")
246 GpuControlList::MachineModelInfo::MachineModelInfo(
247 const std::string& name_op,
248 const std::string& name_value,
249 const std::string& version_op,
250 const std::string& version_string,
251 const std::string& version_string2) {
252 name_info_.reset(new StringInfo(name_op, name_value));
253 version_info_.reset(new VersionInfo(
254 version_op, std::string(), version_string, version_string2));
257 GpuControlList::MachineModelInfo::~MachineModelInfo() {}
259 bool GpuControlList::MachineModelInfo::Contains(
260 const std::string& name, const std::string& version) const {
263 if (!name_info_->Contains(name))
265 return version_info_->Contains(version);
268 bool GpuControlList::MachineModelInfo::IsValid() const {
269 return name_info_->IsValid() && version_info_->IsValid();
272 GpuControlList::StringInfo::StringInfo(const std::string& string_op,
273 const std::string& string_value) {
274 op_ = StringToOp(string_op);
275 value_ = StringToLowerASCII(string_value);
278 bool GpuControlList::StringInfo::Contains(const std::string& value) const {
279 std::string my_value = StringToLowerASCII(value);
282 return strstr(my_value.c_str(), value_.c_str()) != NULL;
284 return StartsWithASCII(my_value, value_, false);
286 return EndsWith(my_value, value_, false);
288 return value_ == my_value;
294 bool GpuControlList::StringInfo::IsValid() const {
295 return op_ != kUnknown;
298 GpuControlList::StringInfo::Op GpuControlList::StringInfo::StringToOp(
299 const std::string& string_op) {
300 if (string_op == "=")
302 else if (string_op == "contains")
304 else if (string_op == "beginwith")
306 else if (string_op == "endwith")
311 GpuControlList::FloatInfo::FloatInfo(const std::string& float_op,
312 const std::string& float_value,
313 const std::string& float_value2)
317 op_ = StringToNumericOp(float_op);
321 if (!base::StringToDouble(float_value, &dvalue)) {
325 value_ = static_cast<float>(dvalue);
326 if (op_ == kBetween) {
327 if (!base::StringToDouble(float_value2, &dvalue)) {
331 value2_ = static_cast<float>(dvalue);
335 bool GpuControlList::FloatInfo::Contains(float value) const {
341 return (value == value_);
343 return (value < value_);
345 return (value <= value_);
347 return (value > value_);
349 return (value >= value_);
350 DCHECK(op_ == kBetween);
351 return ((value_ <= value && value <= value2_) ||
352 (value2_ <= value && value <= value_));
355 bool GpuControlList::FloatInfo::IsValid() const {
356 return op_ != kUnknown;
359 GpuControlList::IntInfo::IntInfo(const std::string& int_op,
360 const std::string& int_value,
361 const std::string& int_value2)
365 op_ = StringToNumericOp(int_op);
368 if (!base::StringToInt(int_value, &value_)) {
372 if (op_ == kBetween &&
373 !base::StringToInt(int_value2, &value2_))
377 bool GpuControlList::IntInfo::Contains(int value) const {
383 return (value == value_);
385 return (value < value_);
387 return (value <= value_);
389 return (value > value_);
391 return (value >= value_);
392 DCHECK(op_ == kBetween);
393 return ((value_ <= value && value <= value2_) ||
394 (value2_ <= value && value <= value_));
397 bool GpuControlList::IntInfo::IsValid() const {
398 return op_ != kUnknown;
401 GpuControlList::BoolInfo::BoolInfo(bool value) : value_(value) {}
403 bool GpuControlList::BoolInfo::Contains(bool value) const {
404 return value_ == value;
408 GpuControlList::ScopedGpuControlListEntry
409 GpuControlList::GpuControlListEntry::GetEntryFromValue(
410 const base::DictionaryValue* value, bool top_level,
411 const FeatureMap& feature_map,
412 bool supports_feature_type_all) {
414 ScopedGpuControlListEntry entry(new GpuControlListEntry());
416 size_t dictionary_entry_count = 0;
420 if (!value->GetInteger("id", reinterpret_cast<int*>(&id)) ||
422 LOG(WARNING) << "Malformed id entry " << entry->id();
425 dictionary_entry_count++;
428 if (value->GetBoolean("disabled", &disabled)) {
429 entry->SetDisabled(disabled);
430 dictionary_entry_count++;
434 std::string description;
435 if (value->GetString("description", &description)) {
436 entry->description_ = description;
437 dictionary_entry_count++;
439 entry->description_ = "The GPU is unavailable for an unexplained reason.";
442 const base::ListValue* cr_bugs;
443 if (value->GetList("cr_bugs", &cr_bugs)) {
444 for (size_t i = 0; i < cr_bugs->GetSize(); ++i) {
446 if (cr_bugs->GetInteger(i, &bug_id)) {
447 entry->cr_bugs_.push_back(bug_id);
449 LOG(WARNING) << "Malformed cr_bugs entry " << entry->id();
453 dictionary_entry_count++;
456 const base::ListValue* webkit_bugs;
457 if (value->GetList("webkit_bugs", &webkit_bugs)) {
458 for (size_t i = 0; i < webkit_bugs->GetSize(); ++i) {
460 if (webkit_bugs->GetInteger(i, &bug_id)) {
461 entry->webkit_bugs_.push_back(bug_id);
463 LOG(WARNING) << "Malformed webkit_bugs entry " << entry->id();
467 dictionary_entry_count++;
470 const base::DictionaryValue* os_value = NULL;
471 if (value->GetDictionary("os", &os_value)) {
473 std::string os_version_op = "any";
474 std::string os_version_string;
475 std::string os_version_string2;
476 os_value->GetString("type", &os_type);
477 const base::DictionaryValue* os_version_value = NULL;
478 if (os_value->GetDictionary("version", &os_version_value)) {
479 os_version_value->GetString(kOp, &os_version_op);
480 os_version_value->GetString("value", &os_version_string);
481 os_version_value->GetString("value2", &os_version_string2);
483 if (!entry->SetOsInfo(os_type, os_version_op, os_version_string,
484 os_version_string2)) {
485 LOG(WARNING) << "Malformed os entry " << entry->id();
488 dictionary_entry_count++;
491 std::string vendor_id;
492 if (value->GetString("vendor_id", &vendor_id)) {
493 if (!entry->SetVendorId(vendor_id)) {
494 LOG(WARNING) << "Malformed vendor_id entry " << entry->id();
497 dictionary_entry_count++;
500 const base::ListValue* device_id_list;
501 if (value->GetList("device_id", &device_id_list)) {
502 for (size_t i = 0; i < device_id_list->GetSize(); ++i) {
503 std::string device_id;
504 if (!device_id_list->GetString(i, &device_id) ||
505 !entry->AddDeviceId(device_id)) {
506 LOG(WARNING) << "Malformed device_id entry " << entry->id();
510 dictionary_entry_count++;
513 std::string multi_gpu_style;
514 if (value->GetString("multi_gpu_style", &multi_gpu_style)) {
515 if (!entry->SetMultiGpuStyle(multi_gpu_style)) {
516 LOG(WARNING) << "Malformed multi_gpu_style entry " << entry->id();
519 dictionary_entry_count++;
522 std::string multi_gpu_category;
523 if (value->GetString("multi_gpu_category", &multi_gpu_category)) {
524 if (!entry->SetMultiGpuCategory(multi_gpu_category)) {
525 LOG(WARNING) << "Malformed multi_gpu_category entry " << entry->id();
528 dictionary_entry_count++;
531 const base::DictionaryValue* driver_vendor_value = NULL;
532 if (value->GetDictionary("driver_vendor", &driver_vendor_value)) {
533 std::string vendor_op;
534 std::string vendor_value;
535 driver_vendor_value->GetString(kOp, &vendor_op);
536 driver_vendor_value->GetString("value", &vendor_value);
537 if (!entry->SetDriverVendorInfo(vendor_op, vendor_value)) {
538 LOG(WARNING) << "Malformed driver_vendor entry " << entry->id();
541 dictionary_entry_count++;
544 const base::DictionaryValue* driver_version_value = NULL;
545 if (value->GetDictionary("driver_version", &driver_version_value)) {
546 std::string driver_version_op = "any";
547 std::string driver_version_style;
548 std::string driver_version_string;
549 std::string driver_version_string2;
550 driver_version_value->GetString(kOp, &driver_version_op);
551 driver_version_value->GetString("style", &driver_version_style);
552 driver_version_value->GetString("value", &driver_version_string);
553 driver_version_value->GetString("value2", &driver_version_string2);
554 if (!entry->SetDriverVersionInfo(driver_version_op,
555 driver_version_style,
556 driver_version_string,
557 driver_version_string2)) {
558 LOG(WARNING) << "Malformed driver_version entry " << entry->id();
561 dictionary_entry_count++;
564 const base::DictionaryValue* driver_date_value = NULL;
565 if (value->GetDictionary("driver_date", &driver_date_value)) {
566 std::string driver_date_op = "any";
567 std::string driver_date_string;
568 std::string driver_date_string2;
569 driver_date_value->GetString(kOp, &driver_date_op);
570 driver_date_value->GetString("value", &driver_date_string);
571 driver_date_value->GetString("value2", &driver_date_string2);
572 if (!entry->SetDriverDateInfo(driver_date_op, driver_date_string,
573 driver_date_string2)) {
574 LOG(WARNING) << "Malformed driver_date entry " << entry->id();
577 dictionary_entry_count++;
580 const base::DictionaryValue* gl_vendor_value = NULL;
581 if (value->GetDictionary("gl_vendor", &gl_vendor_value)) {
582 std::string vendor_op;
583 std::string vendor_value;
584 gl_vendor_value->GetString(kOp, &vendor_op);
585 gl_vendor_value->GetString("value", &vendor_value);
586 if (!entry->SetGLVendorInfo(vendor_op, vendor_value)) {
587 LOG(WARNING) << "Malformed gl_vendor entry " << entry->id();
590 dictionary_entry_count++;
593 const base::DictionaryValue* gl_renderer_value = NULL;
594 if (value->GetDictionary("gl_renderer", &gl_renderer_value)) {
595 std::string renderer_op;
596 std::string renderer_value;
597 gl_renderer_value->GetString(kOp, &renderer_op);
598 gl_renderer_value->GetString("value", &renderer_value);
599 if (!entry->SetGLRendererInfo(renderer_op, renderer_value)) {
600 LOG(WARNING) << "Malformed gl_renderer entry " << entry->id();
603 dictionary_entry_count++;
606 const base::DictionaryValue* gl_extensions_value = NULL;
607 if (value->GetDictionary("gl_extensions", &gl_extensions_value)) {
608 std::string extensions_op;
609 std::string extensions_value;
610 gl_extensions_value->GetString(kOp, &extensions_op);
611 gl_extensions_value->GetString("value", &extensions_value);
612 if (!entry->SetGLExtensionsInfo(extensions_op, extensions_value)) {
613 LOG(WARNING) << "Malformed gl_extensions entry " << entry->id();
616 dictionary_entry_count++;
619 const base::DictionaryValue* gl_reset_notification_strategy_value = NULL;
620 if (value->GetDictionary("gl_reset_notification_strategy",
621 &gl_reset_notification_strategy_value)) {
623 std::string int_value;
624 std::string int_value2;
625 gl_reset_notification_strategy_value->GetString(kOp, &op);
626 gl_reset_notification_strategy_value->GetString("value", &int_value);
627 gl_reset_notification_strategy_value->GetString("value2", &int_value2);
628 if (!entry->SetGLResetNotificationStrategyInfo(
629 op, int_value, int_value2)) {
630 LOG(WARNING) << "Malformed gl_reset_notification_strategy entry "
634 dictionary_entry_count++;
637 const base::DictionaryValue* cpu_brand_value = NULL;
638 if (value->GetDictionary("cpu_info", &cpu_brand_value)) {
640 std::string cpu_value;
641 cpu_brand_value->GetString(kOp, &cpu_op);
642 cpu_brand_value->GetString("value", &cpu_value);
643 if (!entry->SetCpuBrand(cpu_op, cpu_value)) {
644 LOG(WARNING) << "Malformed cpu_brand entry " << entry->id();
647 dictionary_entry_count++;
650 const base::DictionaryValue* perf_graphics_value = NULL;
651 if (value->GetDictionary("perf_graphics", &perf_graphics_value)) {
653 std::string float_value;
654 std::string float_value2;
655 perf_graphics_value->GetString(kOp, &op);
656 perf_graphics_value->GetString("value", &float_value);
657 perf_graphics_value->GetString("value2", &float_value2);
658 if (!entry->SetPerfGraphicsInfo(op, float_value, float_value2)) {
659 LOG(WARNING) << "Malformed perf_graphics entry " << entry->id();
662 dictionary_entry_count++;
665 const base::DictionaryValue* perf_gaming_value = NULL;
666 if (value->GetDictionary("perf_gaming", &perf_gaming_value)) {
668 std::string float_value;
669 std::string float_value2;
670 perf_gaming_value->GetString(kOp, &op);
671 perf_gaming_value->GetString("value", &float_value);
672 perf_gaming_value->GetString("value2", &float_value2);
673 if (!entry->SetPerfGamingInfo(op, float_value, float_value2)) {
674 LOG(WARNING) << "Malformed perf_gaming entry " << entry->id();
677 dictionary_entry_count++;
680 const base::DictionaryValue* perf_overall_value = NULL;
681 if (value->GetDictionary("perf_overall", &perf_overall_value)) {
683 std::string float_value;
684 std::string float_value2;
685 perf_overall_value->GetString(kOp, &op);
686 perf_overall_value->GetString("value", &float_value);
687 perf_overall_value->GetString("value2", &float_value2);
688 if (!entry->SetPerfOverallInfo(op, float_value, float_value2)) {
689 LOG(WARNING) << "Malformed perf_overall entry " << entry->id();
692 dictionary_entry_count++;
695 const base::DictionaryValue* machine_model_value = NULL;
696 if (value->GetDictionary("machine_model", &machine_model_value)) {
698 std::string name_value;
699 const base::DictionaryValue* name = NULL;
700 if (machine_model_value->GetDictionary("name", &name)) {
701 name->GetString(kOp, &name_op);
702 name->GetString("value", &name_value);
705 std::string version_op = "any";
706 std::string version_string;
707 std::string version_string2;
708 const base::DictionaryValue* version_value = NULL;
709 if (machine_model_value->GetDictionary("version", &version_value)) {
710 version_value->GetString(kOp, &version_op);
711 version_value->GetString("value", &version_string);
712 version_value->GetString("value2", &version_string2);
714 if (!entry->SetMachineModelInfo(
715 name_op, name_value, version_op, version_string, version_string2)) {
716 LOG(WARNING) << "Malformed machine_model entry " << entry->id();
719 dictionary_entry_count++;
722 const base::DictionaryValue* gpu_count_value = NULL;
723 if (value->GetDictionary("gpu_count", &gpu_count_value)) {
725 std::string int_value;
726 std::string int_value2;
727 gpu_count_value->GetString(kOp, &op);
728 gpu_count_value->GetString("value", &int_value);
729 gpu_count_value->GetString("value2", &int_value2);
730 if (!entry->SetGpuCountInfo(op, int_value, int_value2)) {
731 LOG(WARNING) << "Malformed gpu_count entry " << entry->id();
734 dictionary_entry_count++;
737 bool direct_rendering;
738 if (value->GetBoolean("direct_rendering", &direct_rendering)) {
739 entry->SetDirectRenderingInfo(direct_rendering);
740 dictionary_entry_count++;
744 const base::ListValue* feature_value = NULL;
745 if (value->GetList("features", &feature_value)) {
746 std::vector<std::string> feature_list;
747 for (size_t i = 0; i < feature_value->GetSize(); ++i) {
749 if (feature_value->GetString(i, &feature)) {
750 feature_list.push_back(feature);
752 LOG(WARNING) << "Malformed feature entry " << entry->id();
756 if (!entry->SetFeatures(
757 feature_list, feature_map, supports_feature_type_all)) {
758 LOG(WARNING) << "Malformed feature entry " << entry->id();
761 dictionary_entry_count++;
766 const base::ListValue* exception_list_value = NULL;
767 if (value->GetList("exceptions", &exception_list_value)) {
768 for (size_t i = 0; i < exception_list_value->GetSize(); ++i) {
769 const base::DictionaryValue* exception_value = NULL;
770 if (!exception_list_value->GetDictionary(i, &exception_value)) {
771 LOG(WARNING) << "Malformed exceptions entry " << entry->id();
774 ScopedGpuControlListEntry exception(GetEntryFromValue(
775 exception_value, false, feature_map, supports_feature_type_all));
776 if (exception.get() == NULL) {
777 LOG(WARNING) << "Malformed exceptions entry " << entry->id();
780 // Exception should inherit vendor_id from parent, otherwise if only
781 // device_ids are specified in Exception, the info will be incomplete.
782 if (exception->vendor_id_ == 0 && entry->vendor_id_ != 0)
783 exception->vendor_id_ = entry->vendor_id_;
784 entry->AddException(exception);
786 dictionary_entry_count++;
790 if (value->size() != dictionary_entry_count) {
791 LOG(WARNING) << "Entry with unknown fields " << entry->id();
797 GpuControlList::GpuControlListEntry::GpuControlListEntry()
801 multi_gpu_style_(kMultiGpuStyleNone),
802 multi_gpu_category_(kMultiGpuCategoryPrimary) {
805 GpuControlList::GpuControlListEntry::~GpuControlListEntry() { }
807 bool GpuControlList::GpuControlListEntry::SetId(uint32 id) {
815 void GpuControlList::GpuControlListEntry::SetDisabled(bool disabled) {
816 disabled_ = disabled;
819 bool GpuControlList::GpuControlListEntry::SetOsInfo(
820 const std::string& os,
821 const std::string& version_op,
822 const std::string& version_string,
823 const std::string& version_string2) {
824 os_info_.reset(new OsInfo(os, version_op, version_string, version_string2));
825 return os_info_->IsValid();
828 bool GpuControlList::GpuControlListEntry::SetVendorId(
829 const std::string& vendor_id_string) {
831 return base::HexStringToUInt(vendor_id_string, &vendor_id_) &&
835 bool GpuControlList::GpuControlListEntry::AddDeviceId(
836 const std::string& device_id_string) {
837 uint32 device_id = 0;
838 if (base::HexStringToUInt(device_id_string, &device_id) && device_id != 0) {
839 device_id_list_.push_back(device_id);
845 bool GpuControlList::GpuControlListEntry::SetMultiGpuStyle(
846 const std::string& multi_gpu_style_string) {
847 MultiGpuStyle style = StringToMultiGpuStyle(multi_gpu_style_string);
848 if (style == kMultiGpuStyleNone)
850 multi_gpu_style_ = style;
854 bool GpuControlList::GpuControlListEntry::SetMultiGpuCategory(
855 const std::string& multi_gpu_category_string) {
856 MultiGpuCategory category =
857 StringToMultiGpuCategory(multi_gpu_category_string);
858 if (category == kMultiGpuCategoryNone)
860 multi_gpu_category_ = category;
864 bool GpuControlList::GpuControlListEntry::SetDriverVendorInfo(
865 const std::string& vendor_op,
866 const std::string& vendor_value) {
867 driver_vendor_info_.reset(new StringInfo(vendor_op, vendor_value));
868 return driver_vendor_info_->IsValid();
871 bool GpuControlList::GpuControlListEntry::SetDriverVersionInfo(
872 const std::string& version_op,
873 const std::string& version_style,
874 const std::string& version_string,
875 const std::string& version_string2) {
876 driver_version_info_.reset(new VersionInfo(
877 version_op, version_style, version_string, version_string2));
878 return driver_version_info_->IsValid();
881 bool GpuControlList::GpuControlListEntry::SetDriverDateInfo(
882 const std::string& date_op,
883 const std::string& date_string,
884 const std::string& date_string2) {
885 driver_date_info_.reset(
886 new VersionInfo(date_op, std::string(), date_string, date_string2));
887 return driver_date_info_->IsValid();
890 bool GpuControlList::GpuControlListEntry::SetGLVendorInfo(
891 const std::string& vendor_op,
892 const std::string& vendor_value) {
893 gl_vendor_info_.reset(new StringInfo(vendor_op, vendor_value));
894 return gl_vendor_info_->IsValid();
897 bool GpuControlList::GpuControlListEntry::SetGLRendererInfo(
898 const std::string& renderer_op,
899 const std::string& renderer_value) {
900 gl_renderer_info_.reset(new StringInfo(renderer_op, renderer_value));
901 return gl_renderer_info_->IsValid();
904 bool GpuControlList::GpuControlListEntry::SetGLExtensionsInfo(
905 const std::string& extensions_op,
906 const std::string& extensions_value) {
907 gl_extensions_info_.reset(new StringInfo(extensions_op, extensions_value));
908 return gl_extensions_info_->IsValid();
911 bool GpuControlList::GpuControlListEntry::SetGLResetNotificationStrategyInfo(
912 const std::string& op,
913 const std::string& int_string,
914 const std::string& int_string2) {
915 gl_reset_notification_strategy_info_.reset(
916 new IntInfo(op, int_string, int_string2));
917 return gl_reset_notification_strategy_info_->IsValid();
920 bool GpuControlList::GpuControlListEntry::SetCpuBrand(
921 const std::string& cpu_op,
922 const std::string& cpu_value) {
923 cpu_brand_.reset(new StringInfo(cpu_op, cpu_value));
924 return cpu_brand_->IsValid();
927 bool GpuControlList::GpuControlListEntry::SetPerfGraphicsInfo(
928 const std::string& op,
929 const std::string& float_string,
930 const std::string& float_string2) {
931 perf_graphics_info_.reset(new FloatInfo(op, float_string, float_string2));
932 return perf_graphics_info_->IsValid();
935 bool GpuControlList::GpuControlListEntry::SetPerfGamingInfo(
936 const std::string& op,
937 const std::string& float_string,
938 const std::string& float_string2) {
939 perf_gaming_info_.reset(new FloatInfo(op, float_string, float_string2));
940 return perf_gaming_info_->IsValid();
943 bool GpuControlList::GpuControlListEntry::SetPerfOverallInfo(
944 const std::string& op,
945 const std::string& float_string,
946 const std::string& float_string2) {
947 perf_overall_info_.reset(new FloatInfo(op, float_string, float_string2));
948 return perf_overall_info_->IsValid();
951 bool GpuControlList::GpuControlListEntry::SetMachineModelInfo(
952 const std::string& name_op,
953 const std::string& name_value,
954 const std::string& version_op,
955 const std::string& version_string,
956 const std::string& version_string2) {
957 machine_model_info_.reset(new MachineModelInfo(
958 name_op, name_value, version_op, version_string, version_string2));
959 return machine_model_info_->IsValid();
962 bool GpuControlList::GpuControlListEntry::SetGpuCountInfo(
963 const std::string& op,
964 const std::string& int_string,
965 const std::string& int_string2) {
966 gpu_count_info_.reset(new IntInfo(op, int_string, int_string2));
967 return gpu_count_info_->IsValid();
970 void GpuControlList::GpuControlListEntry::SetDirectRenderingInfo(bool value) {
971 direct_rendering_info_.reset(new BoolInfo(value));
974 bool GpuControlList::GpuControlListEntry::SetFeatures(
975 const std::vector<std::string>& feature_strings,
976 const FeatureMap& feature_map,
977 bool supports_feature_type_all) {
978 size_t size = feature_strings.size();
982 for (size_t i = 0; i < size; ++i) {
984 if (supports_feature_type_all && feature_strings[i] == "all") {
985 for (FeatureMap::const_iterator iter = feature_map.begin();
986 iter != feature_map.end(); ++iter)
987 features_.insert(iter->second);
990 if (!StringToFeature(feature_strings[i], &feature, feature_map)) {
994 features_.insert(feature);
999 void GpuControlList::GpuControlListEntry::AddException(
1000 ScopedGpuControlListEntry exception) {
1001 exceptions_.push_back(exception);
1005 GpuControlList::GpuControlListEntry::MultiGpuStyle
1006 GpuControlList::GpuControlListEntry::StringToMultiGpuStyle(
1007 const std::string& style) {
1008 if (style == kMultiGpuStyleStringOptimus)
1009 return kMultiGpuStyleOptimus;
1010 if (style == kMultiGpuStyleStringAMDSwitchable)
1011 return kMultiGpuStyleAMDSwitchable;
1012 return kMultiGpuStyleNone;
1016 GpuControlList::GpuControlListEntry::MultiGpuCategory
1017 GpuControlList::GpuControlListEntry::StringToMultiGpuCategory(
1018 const std::string& category) {
1019 if (category == kMultiGpuCategoryStringPrimary)
1020 return kMultiGpuCategoryPrimary;
1021 if (category == kMultiGpuCategoryStringSecondary)
1022 return kMultiGpuCategorySecondary;
1023 if (category == kMultiGpuCategoryStringActive)
1024 return kMultiGpuCategoryActive;
1025 if (category == kMultiGpuCategoryStringAny)
1026 return kMultiGpuCategoryAny;
1027 return kMultiGpuCategoryNone;
1030 void GpuControlList::GpuControlListEntry::LogControlListMatch(
1031 const std::string& control_list_logging_name) const {
1032 static const char kControlListMatchMessage[] =
1033 "Control list match for rule #%u in %s.";
1034 VLOG(1) << base::StringPrintf(kControlListMatchMessage, id_,
1035 control_list_logging_name.c_str());
1038 bool GpuControlList::GpuControlListEntry::Contains(
1039 OsType os_type, const std::string& os_version,
1040 const GPUInfo& gpu_info) const {
1041 DCHECK(os_type != kOsAny);
1042 if (os_info_.get() != NULL && !os_info_->Contains(os_type, os_version))
1044 if (vendor_id_ != 0) {
1045 std::vector<GPUInfo::GPUDevice> candidates;
1046 switch (multi_gpu_category_) {
1047 case kMultiGpuCategoryPrimary:
1048 candidates.push_back(gpu_info.gpu);
1050 case kMultiGpuCategorySecondary:
1051 candidates = gpu_info.secondary_gpus;
1053 case kMultiGpuCategoryAny:
1054 candidates = gpu_info.secondary_gpus;
1055 candidates.push_back(gpu_info.gpu);
1057 case kMultiGpuCategoryActive:
1058 if (gpu_info.gpu.active)
1059 candidates.push_back(gpu_info.gpu);
1060 for (size_t ii = 0; ii < gpu_info.secondary_gpus.size(); ++ii) {
1061 if (gpu_info.secondary_gpus[ii].active)
1062 candidates.push_back(gpu_info.secondary_gpus[ii]);
1068 GPUInfo::GPUDevice gpu;
1069 gpu.vendor_id = vendor_id_;
1071 if (device_id_list_.empty()) {
1072 for (size_t ii = 0; ii < candidates.size(); ++ii) {
1073 if (gpu.vendor_id == candidates[ii].vendor_id) {
1079 for (size_t ii = 0; ii < device_id_list_.size(); ++ii) {
1080 gpu.device_id = device_id_list_[ii];
1081 for (size_t jj = 0; jj < candidates.size(); ++jj) {
1082 if (gpu.vendor_id == candidates[jj].vendor_id &&
1083 gpu.device_id == candidates[jj].device_id) {
1093 switch (multi_gpu_style_) {
1094 case kMultiGpuStyleOptimus:
1095 if (!gpu_info.optimus)
1098 case kMultiGpuStyleAMDSwitchable:
1099 if (!gpu_info.amd_switchable)
1102 case kMultiGpuStyleNone:
1105 if (driver_vendor_info_.get() != NULL && !gpu_info.driver_vendor.empty() &&
1106 !driver_vendor_info_->Contains(gpu_info.driver_vendor))
1108 if (driver_version_info_.get() != NULL && !gpu_info.driver_version.empty()) {
1109 if (!driver_version_info_->Contains(gpu_info.driver_version))
1112 if (driver_date_info_.get() != NULL && !gpu_info.driver_date.empty()) {
1113 if (!driver_date_info_->Contains(gpu_info.driver_date, '-'))
1116 if (gl_vendor_info_.get() != NULL && !gpu_info.gl_vendor.empty() &&
1117 !gl_vendor_info_->Contains(gpu_info.gl_vendor))
1119 if (gl_renderer_info_.get() != NULL && !gpu_info.gl_renderer.empty() &&
1120 !gl_renderer_info_->Contains(gpu_info.gl_renderer))
1122 if (gl_extensions_info_.get() != NULL && !gpu_info.gl_extensions.empty() &&
1123 !gl_extensions_info_->Contains(gpu_info.gl_extensions))
1125 if (gl_reset_notification_strategy_info_.get() != NULL &&
1126 !gl_reset_notification_strategy_info_->Contains(
1127 gpu_info.gl_reset_notification_strategy))
1129 if (perf_graphics_info_.get() != NULL &&
1130 (gpu_info.performance_stats.graphics == 0.0 ||
1131 !perf_graphics_info_->Contains(gpu_info.performance_stats.graphics)))
1133 if (perf_gaming_info_.get() != NULL &&
1134 (gpu_info.performance_stats.gaming == 0.0 ||
1135 !perf_gaming_info_->Contains(gpu_info.performance_stats.gaming)))
1137 if (perf_overall_info_.get() != NULL &&
1138 (gpu_info.performance_stats.overall == 0.0 ||
1139 !perf_overall_info_->Contains(gpu_info.performance_stats.overall)))
1141 if (machine_model_info_.get() != NULL) {
1142 std::vector<std::string> name_version;
1143 base::SplitString(gpu_info.machine_model, ' ', &name_version);
1144 if (name_version.size() == 2 &&
1145 !machine_model_info_->Contains(name_version[0], name_version[1]))
1148 if (gpu_count_info_.get() != NULL &&
1149 !gpu_count_info_->Contains(gpu_info.secondary_gpus.size() + 1))
1151 if (direct_rendering_info_.get() != NULL &&
1152 !direct_rendering_info_->Contains(gpu_info.direct_rendering))
1154 if (cpu_brand_.get() != NULL) {
1156 if (!cpu_brand_->Contains(cpu_info.cpu_brand()))
1160 for (size_t i = 0; i < exceptions_.size(); ++i) {
1161 if (exceptions_[i]->Contains(os_type, os_version, gpu_info) &&
1162 !exceptions_[i]->NeedsMoreInfo(gpu_info))
1168 bool GpuControlList::GpuControlListEntry::NeedsMoreInfo(
1169 const GPUInfo& gpu_info) const {
1170 // We only check for missing info that might be collected with a gl context.
1171 // If certain info is missing due to some error, say, we fail to collect
1172 // vendor_id/device_id, then even if we launch GPU process and create a gl
1173 // context, we won't gather such missing info, so we still return false.
1174 if (driver_vendor_info_.get() && gpu_info.driver_vendor.empty())
1176 if (driver_version_info_.get() && gpu_info.driver_version.empty())
1178 if (gl_vendor_info_.get() && gpu_info.gl_vendor.empty())
1180 if (gl_renderer_info_.get() && gpu_info.gl_renderer.empty())
1182 for (size_t i = 0; i < exceptions_.size(); ++i) {
1183 if (exceptions_[i]->NeedsMoreInfo(gpu_info))
1189 GpuControlList::OsType GpuControlList::GpuControlListEntry::GetOsType() const {
1190 if (os_info_.get() == NULL)
1192 return os_info_->type();
1195 uint32 GpuControlList::GpuControlListEntry::id() const {
1199 bool GpuControlList::GpuControlListEntry::disabled() const {
1203 const std::set<int>& GpuControlList::GpuControlListEntry::features() const {
1207 void GpuControlList::GpuControlListEntry::GetFeatureNames(
1208 base::ListValue* feature_names,
1209 const FeatureMap& feature_map,
1210 bool supports_feature_type_all) const {
1211 DCHECK(feature_names);
1212 if (supports_feature_type_all && features_.size() == feature_map.size()) {
1213 feature_names->AppendString("all");
1216 for (FeatureMap::const_iterator iter = feature_map.begin();
1217 iter != feature_map.end(); ++iter) {
1218 if (features_.count(iter->second) > 0)
1219 feature_names->AppendString(iter->first);
1224 bool GpuControlList::GpuControlListEntry::StringToFeature(
1225 const std::string& feature_name, int* feature_id,
1226 const FeatureMap& feature_map) {
1227 FeatureMap::const_iterator iter = feature_map.find(feature_name);
1228 if (iter != feature_map.end()) {
1229 *feature_id = iter->second;
1235 GpuControlList::GpuControlList()
1237 needs_more_info_(false),
1238 supports_feature_type_all_(false),
1239 control_list_logging_enabled_(false) {
1242 GpuControlList::~GpuControlList() {
1246 bool GpuControlList::LoadList(
1247 const std::string& json_context,
1248 GpuControlList::OsFilter os_filter) {
1249 scoped_ptr<base::Value> root;
1250 root.reset(base::JSONReader::Read(json_context));
1251 if (root.get() == NULL || !root->IsType(base::Value::TYPE_DICTIONARY))
1254 base::DictionaryValue* root_dictionary =
1255 static_cast<base::DictionaryValue*>(root.get());
1256 DCHECK(root_dictionary);
1257 return LoadList(*root_dictionary, os_filter);
1260 bool GpuControlList::LoadList(const base::DictionaryValue& parsed_json,
1261 GpuControlList::OsFilter os_filter) {
1262 std::vector<ScopedGpuControlListEntry> entries;
1264 parsed_json.GetString("version", &version_);
1265 std::vector<std::string> pieces;
1266 if (!ProcessVersionString(version_, '.', &pieces))
1269 const base::ListValue* list = NULL;
1270 if (!parsed_json.GetList("entries", &list))
1273 uint32 max_entry_id = 0;
1274 for (size_t i = 0; i < list->GetSize(); ++i) {
1275 const base::DictionaryValue* list_item = NULL;
1276 bool valid = list->GetDictionary(i, &list_item);
1277 if (!valid || list_item == NULL)
1279 ScopedGpuControlListEntry entry(GpuControlListEntry::GetEntryFromValue(
1280 list_item, true, feature_map_, supports_feature_type_all_));
1281 if (entry.get() == NULL)
1283 if (entry->id() > max_entry_id)
1284 max_entry_id = entry->id();
1285 entries.push_back(entry);
1289 OsType my_os = GetOsType();
1290 for (size_t i = 0; i < entries.size(); ++i) {
1291 OsType entry_os = entries[i]->GetOsType();
1292 if (os_filter == GpuControlList::kAllOs ||
1293 entry_os == kOsAny || entry_os == my_os)
1294 entries_.push_back(entries[i]);
1296 max_entry_id_ = max_entry_id;
1300 std::set<int> GpuControlList::MakeDecision(
1301 GpuControlList::OsType os,
1302 std::string os_version,
1303 const GPUInfo& gpu_info) {
1304 active_entries_.clear();
1305 std::set<int> features;
1307 needs_more_info_ = false;
1308 std::set<int> possible_features;
1312 if (os_version.empty()) {
1313 os_version = base::SysInfo::OperatingSystemVersion();
1314 size_t pos = os_version.find_first_not_of("0123456789.");
1315 if (pos != std::string::npos)
1316 os_version = os_version.substr(0, pos);
1318 std::vector<std::string> pieces;
1319 if (!ProcessVersionString(os_version, '.', &pieces))
1322 for (size_t i = 0; i < entries_.size(); ++i) {
1323 if (entries_[i]->Contains(os, os_version, gpu_info)) {
1324 if (!entries_[i]->disabled()) {
1325 if (control_list_logging_enabled_)
1326 entries_[i]->LogControlListMatch(control_list_logging_name_);
1327 MergeFeatureSets(&possible_features, entries_[i]->features());
1328 if (!entries_[i]->NeedsMoreInfo(gpu_info))
1329 MergeFeatureSets(&features, entries_[i]->features());
1331 active_entries_.push_back(entries_[i]);
1335 if (possible_features.size() > features.size())
1336 needs_more_info_ = true;
1341 void GpuControlList::GetDecisionEntries(
1342 std::vector<uint32>* entry_ids, bool disabled) const {
1345 for (size_t i = 0; i < active_entries_.size(); ++i) {
1346 if (disabled == active_entries_[i]->disabled())
1347 entry_ids->push_back(active_entries_[i]->id());
1351 void GpuControlList::GetReasons(base::ListValue* problem_list,
1352 const std::string& tag) const {
1353 DCHECK(problem_list);
1354 for (size_t i = 0; i < active_entries_.size(); ++i) {
1355 GpuControlListEntry* entry = active_entries_[i].get();
1356 if (entry->disabled())
1358 base::DictionaryValue* problem = new base::DictionaryValue();
1360 problem->SetString("description", entry->description());
1362 base::ListValue* cr_bugs = new base::ListValue();
1363 for (size_t j = 0; j < entry->cr_bugs().size(); ++j)
1364 cr_bugs->Append(new base::FundamentalValue(entry->cr_bugs()[j]));
1365 problem->Set("crBugs", cr_bugs);
1367 base::ListValue* webkit_bugs = new base::ListValue();
1368 for (size_t j = 0; j < entry->webkit_bugs().size(); ++j) {
1369 webkit_bugs->Append(new base::FundamentalValue(entry->webkit_bugs()[j]));
1371 problem->Set("webkitBugs", webkit_bugs);
1373 base::ListValue* features = new base::ListValue();
1374 entry->GetFeatureNames(features, feature_map_, supports_feature_type_all_);
1375 problem->Set("affectedGpuSettings", features);
1377 DCHECK(tag == "workarounds" || tag == "disabledFeatures");
1378 problem->SetString("tag", tag);
1380 problem_list->Append(problem);
1384 size_t GpuControlList::num_entries() const {
1385 return entries_.size();
1388 uint32 GpuControlList::max_entry_id() const {
1389 return max_entry_id_;
1392 std::string GpuControlList::version() const {
1396 GpuControlList::OsType GpuControlList::GetOsType() {
1397 #if defined(OS_CHROMEOS)
1399 #elif defined(OS_WIN)
1401 #elif defined(OS_ANDROID)
1403 #elif defined(OS_LINUX) || defined(OS_OPENBSD)
1405 #elif defined(OS_MACOSX)
1412 void GpuControlList::Clear() {
1414 active_entries_.clear();
1419 GpuControlList::NumericOp GpuControlList::StringToNumericOp(
1420 const std::string& op) {
1433 if (op == "between")
1438 void GpuControlList::AddSupportedFeature(
1439 const std::string& feature_name, int feature_id) {
1440 feature_map_[feature_name] = feature_id;
1443 void GpuControlList::set_supports_feature_type_all(bool supported) {
1444 supports_feature_type_all_ = supported;