Add ICU message format support
[chromium-blink-merge.git] / gpu / config / gpu_control_list.cc
blob1fbf0b6f5aa2427243a9150757b800afe5fe79b5
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"
7 #include "base/cpu.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"
17 #include "third_party/re2/re2/re2.h"
19 namespace gpu {
20 namespace {
22 // Break a version string into segments. Return true if each segment is
23 // a valid number, and not all segment is 0.
24 bool ProcessVersionString(const std::string& version_string,
25 char splitter,
26 std::vector<std::string>* version) {
27 DCHECK(version);
28 base::SplitString(version_string, splitter, version);
29 if (version->size() == 0)
30 return false;
31 // If the splitter is '-', we assume it's a date with format "mm-dd-yyyy";
32 // we split it into the order of "yyyy", "mm", "dd".
33 if (splitter == '-') {
34 std::string year = (*version)[version->size() - 1];
35 for (int i = version->size() - 1; i > 0; --i) {
36 (*version)[i] = (*version)[i - 1];
38 (*version)[0] = year;
40 bool all_zero = true;
41 for (size_t i = 0; i < version->size(); ++i) {
42 unsigned num = 0;
43 if (!base::StringToUint((*version)[i], &num))
44 return false;
45 if (num)
46 all_zero = false;
48 return !all_zero;
51 // Compare two number strings using numerical ordering.
52 // Return 0 if number = number_ref,
53 // 1 if number > number_ref,
54 // -1 if number < number_ref.
55 int CompareNumericalNumberStrings(
56 const std::string& number, const std::string& number_ref) {
57 unsigned value1 = 0;
58 unsigned value2 = 0;
59 bool valid = base::StringToUint(number, &value1);
60 DCHECK(valid);
61 valid = base::StringToUint(number_ref, &value2);
62 DCHECK(valid);
63 if (value1 == value2)
64 return 0;
65 if (value1 > value2)
66 return 1;
67 return -1;
70 // Compare two number strings using lexical ordering.
71 // Return 0 if number = number_ref,
72 // 1 if number > number_ref,
73 // -1 if number < number_ref.
74 // We only compare as many digits as number_ref contains.
75 // If number_ref is xxx, it's considered as xxx*
76 // For example: CompareLexicalNumberStrings("121", "12") returns 0,
77 // CompareLexicalNumberStrings("12", "121") returns -1.
78 int CompareLexicalNumberStrings(
79 const std::string& number, const std::string& number_ref) {
80 for (size_t i = 0; i < number_ref.length(); ++i) {
81 unsigned value1 = 0;
82 if (i < number.length())
83 value1 = number[i] - '0';
84 unsigned value2 = number_ref[i] - '0';
85 if (value1 > value2)
86 return 1;
87 if (value1 < value2)
88 return -1;
90 return 0;
93 // A mismatch is identified only if both |input| and |pattern| are not empty.
94 bool StringMismatch(const std::string& input, const std::string& pattern) {
95 if (input.empty() || pattern.empty())
96 return false;
97 return !RE2::FullMatch(input, pattern);
100 const char kMultiGpuStyleStringAMDSwitchable[] = "amd_switchable";
101 const char kMultiGpuStyleStringAMDSwitchableDiscrete[] =
102 "amd_switchable_discrete";
103 const char kMultiGpuStyleStringAMDSwitchableIntegrated[] =
104 "amd_switchable_integrated";
105 const char kMultiGpuStyleStringOptimus[] = "optimus";
107 const char kMultiGpuCategoryStringPrimary[] = "primary";
108 const char kMultiGpuCategoryStringSecondary[] = "secondary";
109 const char kMultiGpuCategoryStringActive[] = "active";
110 const char kMultiGpuCategoryStringAny[] = "any";
112 const char kGLTypeStringGL[] = "gl";
113 const char kGLTypeStringGLES[] = "gles";
114 const char kGLTypeStringANGLE[] = "angle";
116 const char kVersionStyleStringNumerical[] = "numerical";
117 const char kVersionStyleStringLexical[] = "lexical";
119 const char kOp[] = "op";
121 } // namespace anonymous
123 GpuControlList::VersionInfo::VersionInfo(
124 const std::string& version_op,
125 const std::string& version_style,
126 const std::string& version_string,
127 const std::string& version_string2)
128 : version_style_(kVersionStyleNumerical) {
129 op_ = StringToNumericOp(version_op);
130 if (op_ == kUnknown || op_ == kAny)
131 return;
132 version_style_ = StringToVersionStyle(version_style);
133 if (!ProcessVersionString(version_string, '.', &version_)) {
134 op_ = kUnknown;
135 return;
137 if (op_ == kBetween) {
138 if (!ProcessVersionString(version_string2, '.', &version2_))
139 op_ = kUnknown;
143 GpuControlList::VersionInfo::~VersionInfo() {
146 bool GpuControlList::VersionInfo::Contains(
147 const std::string& version_string) const {
148 return Contains(version_string, '.');
151 bool GpuControlList::VersionInfo::Contains(
152 const std::string& version_string, char splitter) const {
153 if (op_ == kUnknown)
154 return false;
155 if (op_ == kAny)
156 return true;
157 std::vector<std::string> version;
158 if (!ProcessVersionString(version_string, splitter, &version))
159 return false;
160 int relation = Compare(version, version_, version_style_);
161 if (op_ == kEQ)
162 return (relation == 0);
163 else if (op_ == kLT)
164 return (relation < 0);
165 else if (op_ == kLE)
166 return (relation <= 0);
167 else if (op_ == kGT)
168 return (relation > 0);
169 else if (op_ == kGE)
170 return (relation >= 0);
171 // op_ == kBetween
172 if (relation < 0)
173 return false;
174 return Compare(version, version2_, version_style_) <= 0;
177 bool GpuControlList::VersionInfo::IsValid() const {
178 return (op_ != kUnknown && version_style_ != kVersionStyleUnknown);
181 bool GpuControlList::VersionInfo::IsLexical() const {
182 return version_style_ == kVersionStyleLexical;
185 // static
186 int GpuControlList::VersionInfo::Compare(
187 const std::vector<std::string>& version,
188 const std::vector<std::string>& version_ref,
189 VersionStyle version_style) {
190 DCHECK(version.size() > 0 && version_ref.size() > 0);
191 DCHECK(version_style != kVersionStyleUnknown);
192 for (size_t i = 0; i < version_ref.size(); ++i) {
193 if (i >= version.size())
194 return 0;
195 int ret = 0;
196 // We assume both versions are checked by ProcessVersionString().
197 if (i > 0 && version_style == kVersionStyleLexical)
198 ret = CompareLexicalNumberStrings(version[i], version_ref[i]);
199 else
200 ret = CompareNumericalNumberStrings(version[i], version_ref[i]);
201 if (ret != 0)
202 return ret;
204 return 0;
207 // static
208 GpuControlList::VersionInfo::VersionStyle
209 GpuControlList::VersionInfo::StringToVersionStyle(
210 const std::string& version_style) {
211 if (version_style.empty() || version_style == kVersionStyleStringNumerical)
212 return kVersionStyleNumerical;
213 if (version_style == kVersionStyleStringLexical)
214 return kVersionStyleLexical;
215 return kVersionStyleUnknown;
218 GpuControlList::OsInfo::OsInfo(const std::string& os,
219 const std::string& version_op,
220 const std::string& version_string,
221 const std::string& version_string2) {
222 type_ = StringToOsType(os);
223 if (type_ != kOsUnknown) {
224 version_info_.reset(new VersionInfo(
225 version_op, std::string(), version_string, version_string2));
229 GpuControlList::OsInfo::~OsInfo() {}
231 bool GpuControlList::OsInfo::Contains(
232 OsType type, const std::string& version) const {
233 if (!IsValid())
234 return false;
235 if (type_ != type && type_ != kOsAny)
236 return false;
237 std::string processed_version;
238 size_t pos = version.find_first_not_of("0123456789.");
239 if (pos != std::string::npos)
240 processed_version = version.substr(0, pos);
241 else
242 processed_version = version;
244 return version_info_->Contains(processed_version);
247 bool GpuControlList::OsInfo::IsValid() const {
248 return type_ != kOsUnknown && version_info_->IsValid();
251 GpuControlList::OsType GpuControlList::OsInfo::type() const {
252 return type_;
255 GpuControlList::OsType GpuControlList::OsInfo::StringToOsType(
256 const std::string& os) {
257 if (os == "win")
258 return kOsWin;
259 else if (os == "macosx")
260 return kOsMacosx;
261 else if (os == "android")
262 return kOsAndroid;
263 else if (os == "linux")
264 return kOsLinux;
265 else if (os == "chromeos")
266 return kOsChromeOS;
267 else if (os == "any")
268 return kOsAny;
269 return kOsUnknown;
272 GpuControlList::FloatInfo::FloatInfo(const std::string& float_op,
273 const std::string& float_value,
274 const std::string& float_value2)
275 : op_(kUnknown),
276 value_(0.f),
277 value2_(0.f) {
278 op_ = StringToNumericOp(float_op);
279 if (op_ == kAny)
280 return;
281 double dvalue = 0;
282 if (!base::StringToDouble(float_value, &dvalue)) {
283 op_ = kUnknown;
284 return;
286 value_ = static_cast<float>(dvalue);
287 if (op_ == kBetween) {
288 if (!base::StringToDouble(float_value2, &dvalue)) {
289 op_ = kUnknown;
290 return;
292 value2_ = static_cast<float>(dvalue);
296 bool GpuControlList::FloatInfo::Contains(float value) const {
297 if (op_ == kUnknown)
298 return false;
299 if (op_ == kAny)
300 return true;
301 if (op_ == kEQ)
302 return (value == value_);
303 if (op_ == kLT)
304 return (value < value_);
305 if (op_ == kLE)
306 return (value <= value_);
307 if (op_ == kGT)
308 return (value > value_);
309 if (op_ == kGE)
310 return (value >= value_);
311 DCHECK(op_ == kBetween);
312 return ((value_ <= value && value <= value2_) ||
313 (value2_ <= value && value <= value_));
316 bool GpuControlList::FloatInfo::IsValid() const {
317 return op_ != kUnknown;
320 GpuControlList::IntInfo::IntInfo(const std::string& int_op,
321 const std::string& int_value,
322 const std::string& int_value2)
323 : op_(kUnknown),
324 value_(0),
325 value2_(0) {
326 op_ = StringToNumericOp(int_op);
327 if (op_ == kAny)
328 return;
329 if (!base::StringToInt(int_value, &value_)) {
330 op_ = kUnknown;
331 return;
333 if (op_ == kBetween &&
334 !base::StringToInt(int_value2, &value2_))
335 op_ = kUnknown;
338 bool GpuControlList::IntInfo::Contains(int value) const {
339 if (op_ == kUnknown)
340 return false;
341 if (op_ == kAny)
342 return true;
343 if (op_ == kEQ)
344 return (value == value_);
345 if (op_ == kLT)
346 return (value < value_);
347 if (op_ == kLE)
348 return (value <= value_);
349 if (op_ == kGT)
350 return (value > value_);
351 if (op_ == kGE)
352 return (value >= value_);
353 DCHECK(op_ == kBetween);
354 return ((value_ <= value && value <= value2_) ||
355 (value2_ <= value && value <= value_));
358 bool GpuControlList::IntInfo::IsValid() const {
359 return op_ != kUnknown;
362 GpuControlList::BoolInfo::BoolInfo(bool value) : value_(value) {}
364 bool GpuControlList::BoolInfo::Contains(bool value) const {
365 return value_ == value;
368 // static
369 GpuControlList::ScopedGpuControlListEntry
370 GpuControlList::GpuControlListEntry::GetEntryFromValue(
371 const base::DictionaryValue* value, bool top_level,
372 const FeatureMap& feature_map,
373 bool supports_feature_type_all) {
374 DCHECK(value);
375 ScopedGpuControlListEntry entry(new GpuControlListEntry());
377 size_t dictionary_entry_count = 0;
379 if (top_level) {
380 uint32 id;
381 if (!value->GetInteger("id", reinterpret_cast<int*>(&id)) ||
382 !entry->SetId(id)) {
383 LOG(WARNING) << "Malformed id entry " << entry->id();
384 return NULL;
386 dictionary_entry_count++;
388 bool disabled;
389 if (value->GetBoolean("disabled", &disabled)) {
390 entry->SetDisabled(disabled);
391 dictionary_entry_count++;
395 std::string description;
396 if (value->GetString("description", &description)) {
397 entry->description_ = description;
398 dictionary_entry_count++;
399 } else {
400 entry->description_ = "The GPU is unavailable for an unexplained reason.";
403 const base::ListValue* cr_bugs;
404 if (value->GetList("cr_bugs", &cr_bugs)) {
405 for (size_t i = 0; i < cr_bugs->GetSize(); ++i) {
406 int bug_id;
407 if (cr_bugs->GetInteger(i, &bug_id)) {
408 entry->cr_bugs_.push_back(bug_id);
409 } else {
410 LOG(WARNING) << "Malformed cr_bugs entry " << entry->id();
411 return NULL;
414 dictionary_entry_count++;
417 const base::ListValue* webkit_bugs;
418 if (value->GetList("webkit_bugs", &webkit_bugs)) {
419 for (size_t i = 0; i < webkit_bugs->GetSize(); ++i) {
420 int bug_id;
421 if (webkit_bugs->GetInteger(i, &bug_id)) {
422 entry->webkit_bugs_.push_back(bug_id);
423 } else {
424 LOG(WARNING) << "Malformed webkit_bugs entry " << entry->id();
425 return NULL;
428 dictionary_entry_count++;
431 const base::ListValue* disabled_extensions;
432 if (value->GetList("disabled_extensions", &disabled_extensions)) {
433 for (size_t i = 0; i < disabled_extensions->GetSize(); ++i) {
434 std::string disabled_extension;
435 if (disabled_extensions->GetString(i, &disabled_extension)) {
436 entry->disabled_extensions_.push_back(disabled_extension);
437 } else {
438 LOG(WARNING) << "Malformed disabled_extensions entry " << entry->id();
439 return NULL;
442 dictionary_entry_count++;
445 const base::DictionaryValue* os_value = NULL;
446 if (value->GetDictionary("os", &os_value)) {
447 std::string os_type;
448 std::string os_version_op = "any";
449 std::string os_version_string;
450 std::string os_version_string2;
451 os_value->GetString("type", &os_type);
452 const base::DictionaryValue* os_version_value = NULL;
453 if (os_value->GetDictionary("version", &os_version_value)) {
454 os_version_value->GetString(kOp, &os_version_op);
455 os_version_value->GetString("value", &os_version_string);
456 os_version_value->GetString("value2", &os_version_string2);
458 if (!entry->SetOsInfo(os_type, os_version_op, os_version_string,
459 os_version_string2)) {
460 LOG(WARNING) << "Malformed os entry " << entry->id();
461 return NULL;
463 dictionary_entry_count++;
466 std::string vendor_id;
467 if (value->GetString("vendor_id", &vendor_id)) {
468 if (!entry->SetVendorId(vendor_id)) {
469 LOG(WARNING) << "Malformed vendor_id entry " << entry->id();
470 return NULL;
472 dictionary_entry_count++;
475 const base::ListValue* device_id_list;
476 if (value->GetList("device_id", &device_id_list)) {
477 for (size_t i = 0; i < device_id_list->GetSize(); ++i) {
478 std::string device_id;
479 if (!device_id_list->GetString(i, &device_id) ||
480 !entry->AddDeviceId(device_id)) {
481 LOG(WARNING) << "Malformed device_id entry " << entry->id();
482 return NULL;
485 dictionary_entry_count++;
488 std::string multi_gpu_style;
489 if (value->GetString("multi_gpu_style", &multi_gpu_style)) {
490 if (!entry->SetMultiGpuStyle(multi_gpu_style)) {
491 LOG(WARNING) << "Malformed multi_gpu_style entry " << entry->id();
492 return NULL;
494 dictionary_entry_count++;
497 std::string multi_gpu_category;
498 if (value->GetString("multi_gpu_category", &multi_gpu_category)) {
499 if (!entry->SetMultiGpuCategory(multi_gpu_category)) {
500 LOG(WARNING) << "Malformed multi_gpu_category entry " << entry->id();
501 return NULL;
503 dictionary_entry_count++;
506 std::string driver_vendor_value;
507 if (value->GetString("driver_vendor", &driver_vendor_value)) {
508 if (!entry->SetDriverVendorInfo(driver_vendor_value)) {
509 LOG(WARNING) << "Malformed driver_vendor entry " << entry->id();
510 return NULL;
512 dictionary_entry_count++;
515 const base::DictionaryValue* driver_version_value = NULL;
516 if (value->GetDictionary("driver_version", &driver_version_value)) {
517 std::string driver_version_op = "any";
518 std::string driver_version_style;
519 std::string driver_version_string;
520 std::string driver_version_string2;
521 driver_version_value->GetString(kOp, &driver_version_op);
522 driver_version_value->GetString("style", &driver_version_style);
523 driver_version_value->GetString("value", &driver_version_string);
524 driver_version_value->GetString("value2", &driver_version_string2);
525 if (!entry->SetDriverVersionInfo(driver_version_op,
526 driver_version_style,
527 driver_version_string,
528 driver_version_string2)) {
529 LOG(WARNING) << "Malformed driver_version entry " << entry->id();
530 return NULL;
532 dictionary_entry_count++;
535 const base::DictionaryValue* driver_date_value = NULL;
536 if (value->GetDictionary("driver_date", &driver_date_value)) {
537 std::string driver_date_op = "any";
538 std::string driver_date_string;
539 std::string driver_date_string2;
540 driver_date_value->GetString(kOp, &driver_date_op);
541 driver_date_value->GetString("value", &driver_date_string);
542 driver_date_value->GetString("value2", &driver_date_string2);
543 if (!entry->SetDriverDateInfo(driver_date_op, driver_date_string,
544 driver_date_string2)) {
545 LOG(WARNING) << "Malformed driver_date entry " << entry->id();
546 return NULL;
548 dictionary_entry_count++;
551 std::string gl_type;
552 if (value->GetString("gl_type", &gl_type)) {
553 if (!entry->SetGLType(gl_type)) {
554 LOG(WARNING) << "Malformed gl_type entry " << entry->id();
555 return NULL;
557 dictionary_entry_count++;
560 const base::DictionaryValue* gl_version_value = NULL;
561 if (value->GetDictionary("gl_version", &gl_version_value)) {
562 std::string version_op = "any";
563 std::string version_string;
564 std::string version_string2;
565 gl_version_value->GetString(kOp, &version_op);
566 gl_version_value->GetString("value", &version_string);
567 gl_version_value->GetString("value2", &version_string2);
568 if (!entry->SetGLVersionInfo(
569 version_op, version_string, version_string2)) {
570 LOG(WARNING) << "Malformed gl_version entry " << entry->id();
571 return NULL;
573 dictionary_entry_count++;
576 std::string gl_vendor_value;
577 if (value->GetString("gl_vendor", &gl_vendor_value)) {
578 if (!entry->SetGLVendorInfo(gl_vendor_value)) {
579 LOG(WARNING) << "Malformed gl_vendor entry " << entry->id();
580 return NULL;
582 dictionary_entry_count++;
585 std::string gl_renderer_value;
586 if (value->GetString("gl_renderer", &gl_renderer_value)) {
587 if (!entry->SetGLRendererInfo(gl_renderer_value)) {
588 LOG(WARNING) << "Malformed gl_renderer entry " << entry->id();
589 return NULL;
591 dictionary_entry_count++;
594 std::string gl_extensions_value;
595 if (value->GetString("gl_extensions", &gl_extensions_value)) {
596 if (!entry->SetGLExtensionsInfo(gl_extensions_value)) {
597 LOG(WARNING) << "Malformed gl_extensions entry " << entry->id();
598 return NULL;
600 dictionary_entry_count++;
603 const base::DictionaryValue* gl_reset_notification_strategy_value = NULL;
604 if (value->GetDictionary("gl_reset_notification_strategy",
605 &gl_reset_notification_strategy_value)) {
606 std::string op;
607 std::string int_value;
608 std::string int_value2;
609 gl_reset_notification_strategy_value->GetString(kOp, &op);
610 gl_reset_notification_strategy_value->GetString("value", &int_value);
611 gl_reset_notification_strategy_value->GetString("value2", &int_value2);
612 if (!entry->SetGLResetNotificationStrategyInfo(
613 op, int_value, int_value2)) {
614 LOG(WARNING) << "Malformed gl_reset_notification_strategy entry "
615 << entry->id();
616 return NULL;
618 dictionary_entry_count++;
621 std::string cpu_brand_value;
622 if (value->GetString("cpu_info", &cpu_brand_value)) {
623 if (!entry->SetCpuBrand(cpu_brand_value)) {
624 LOG(WARNING) << "Malformed cpu_brand entry " << entry->id();
625 return NULL;
627 dictionary_entry_count++;
630 const base::DictionaryValue* perf_graphics_value = NULL;
631 if (value->GetDictionary("perf_graphics", &perf_graphics_value)) {
632 std::string op;
633 std::string float_value;
634 std::string float_value2;
635 perf_graphics_value->GetString(kOp, &op);
636 perf_graphics_value->GetString("value", &float_value);
637 perf_graphics_value->GetString("value2", &float_value2);
638 if (!entry->SetPerfGraphicsInfo(op, float_value, float_value2)) {
639 LOG(WARNING) << "Malformed perf_graphics entry " << entry->id();
640 return NULL;
642 dictionary_entry_count++;
645 const base::DictionaryValue* perf_gaming_value = NULL;
646 if (value->GetDictionary("perf_gaming", &perf_gaming_value)) {
647 std::string op;
648 std::string float_value;
649 std::string float_value2;
650 perf_gaming_value->GetString(kOp, &op);
651 perf_gaming_value->GetString("value", &float_value);
652 perf_gaming_value->GetString("value2", &float_value2);
653 if (!entry->SetPerfGamingInfo(op, float_value, float_value2)) {
654 LOG(WARNING) << "Malformed perf_gaming entry " << entry->id();
655 return NULL;
657 dictionary_entry_count++;
660 const base::DictionaryValue* perf_overall_value = NULL;
661 if (value->GetDictionary("perf_overall", &perf_overall_value)) {
662 std::string op;
663 std::string float_value;
664 std::string float_value2;
665 perf_overall_value->GetString(kOp, &op);
666 perf_overall_value->GetString("value", &float_value);
667 perf_overall_value->GetString("value2", &float_value2);
668 if (!entry->SetPerfOverallInfo(op, float_value, float_value2)) {
669 LOG(WARNING) << "Malformed perf_overall entry " << entry->id();
670 return NULL;
672 dictionary_entry_count++;
675 const base::ListValue* machine_model_name_list;
676 if (value->GetList("machine_model_name", &machine_model_name_list)) {
677 for (size_t i = 0; i < machine_model_name_list->GetSize(); ++i) {
678 std::string model_name;
679 if (!machine_model_name_list->GetString(i, &model_name) ||
680 !entry->AddMachineModelName(model_name)) {
681 LOG(WARNING) << "Malformed machine_model_name entry " << entry->id();
682 return NULL;
685 dictionary_entry_count++;
688 const base::DictionaryValue* machine_model_version_value = NULL;
689 if (value->GetDictionary(
690 "machine_model_version", &machine_model_version_value)) {
691 std::string version_op = "any";
692 std::string version_string;
693 std::string version_string2;
694 machine_model_version_value->GetString(kOp, &version_op);
695 machine_model_version_value->GetString("value", &version_string);
696 machine_model_version_value->GetString("value2", &version_string2);
697 if (!entry->SetMachineModelVersionInfo(
698 version_op, version_string, version_string2)) {
699 LOG(WARNING) << "Malformed machine_model_version entry " << entry->id();
700 return NULL;
702 dictionary_entry_count++;
705 const base::DictionaryValue* gpu_count_value = NULL;
706 if (value->GetDictionary("gpu_count", &gpu_count_value)) {
707 std::string op;
708 std::string int_value;
709 std::string int_value2;
710 gpu_count_value->GetString(kOp, &op);
711 gpu_count_value->GetString("value", &int_value);
712 gpu_count_value->GetString("value2", &int_value2);
713 if (!entry->SetGpuCountInfo(op, int_value, int_value2)) {
714 LOG(WARNING) << "Malformed gpu_count entry " << entry->id();
715 return NULL;
717 dictionary_entry_count++;
720 bool direct_rendering;
721 if (value->GetBoolean("direct_rendering", &direct_rendering)) {
722 entry->SetDirectRenderingInfo(direct_rendering);
723 dictionary_entry_count++;
726 bool in_process_gpu;
727 if (value->GetBoolean("in_process_gpu", &in_process_gpu)) {
728 entry->SetInProcessGPUInfo(in_process_gpu);
729 dictionary_entry_count++;
732 if (top_level) {
733 const base::ListValue* feature_value = NULL;
734 if (value->GetList("features", &feature_value)) {
735 std::vector<std::string> feature_list;
736 for (size_t i = 0; i < feature_value->GetSize(); ++i) {
737 std::string feature;
738 if (feature_value->GetString(i, &feature)) {
739 feature_list.push_back(feature);
740 } else {
741 LOG(WARNING) << "Malformed feature entry " << entry->id();
742 return NULL;
745 if (!entry->SetFeatures(
746 feature_list, feature_map, supports_feature_type_all)) {
747 LOG(WARNING) << "Malformed feature entry " << entry->id();
748 return NULL;
750 dictionary_entry_count++;
754 if (top_level) {
755 const base::ListValue* exception_list_value = NULL;
756 if (value->GetList("exceptions", &exception_list_value)) {
757 for (size_t i = 0; i < exception_list_value->GetSize(); ++i) {
758 const base::DictionaryValue* exception_value = NULL;
759 if (!exception_list_value->GetDictionary(i, &exception_value)) {
760 LOG(WARNING) << "Malformed exceptions entry " << entry->id();
761 return NULL;
763 ScopedGpuControlListEntry exception(GetEntryFromValue(
764 exception_value, false, feature_map, supports_feature_type_all));
765 if (exception.get() == NULL) {
766 LOG(WARNING) << "Malformed exceptions entry " << entry->id();
767 return NULL;
769 // Exception should inherit vendor_id from parent, otherwise if only
770 // device_ids are specified in Exception, the info will be incomplete.
771 if (exception->vendor_id_ == 0 && entry->vendor_id_ != 0)
772 exception->vendor_id_ = entry->vendor_id_;
773 entry->AddException(exception);
775 dictionary_entry_count++;
779 if (value->size() != dictionary_entry_count) {
780 LOG(WARNING) << "Entry with unknown fields " << entry->id();
781 return NULL;
784 // If GL_VERSION is specified, but no info about whether it's GL or GLES,
785 // we use the default for the platform. See GLType enum declaration.
786 if (entry->gl_version_info_.get() != NULL && entry->gl_type_ == kGLTypeNone)
787 entry->gl_type_ = GetDefaultGLType();
789 return entry;
792 GpuControlList::GpuControlListEntry::GpuControlListEntry()
793 : id_(0),
794 disabled_(false),
795 vendor_id_(0),
796 multi_gpu_style_(kMultiGpuStyleNone),
797 multi_gpu_category_(kMultiGpuCategoryPrimary),
798 gl_type_(kGLTypeNone) {
801 GpuControlList::GpuControlListEntry::~GpuControlListEntry() { }
803 bool GpuControlList::GpuControlListEntry::SetId(uint32 id) {
804 if (id != 0) {
805 id_ = id;
806 return true;
808 return false;
811 void GpuControlList::GpuControlListEntry::SetDisabled(bool disabled) {
812 disabled_ = disabled;
815 bool GpuControlList::GpuControlListEntry::SetOsInfo(
816 const std::string& os,
817 const std::string& version_op,
818 const std::string& version_string,
819 const std::string& version_string2) {
820 os_info_.reset(new OsInfo(os, version_op, version_string, version_string2));
821 return os_info_->IsValid();
824 bool GpuControlList::GpuControlListEntry::SetVendorId(
825 const std::string& vendor_id_string) {
826 vendor_id_ = 0;
827 return base::HexStringToUInt(vendor_id_string, &vendor_id_) &&
828 vendor_id_ != 0;
831 bool GpuControlList::GpuControlListEntry::AddDeviceId(
832 const std::string& device_id_string) {
833 uint32 device_id = 0;
834 if (base::HexStringToUInt(device_id_string, &device_id) && device_id != 0) {
835 device_id_list_.push_back(device_id);
836 return true;
838 return false;
841 bool GpuControlList::GpuControlListEntry::SetMultiGpuStyle(
842 const std::string& multi_gpu_style_string) {
843 MultiGpuStyle style = StringToMultiGpuStyle(multi_gpu_style_string);
844 if (style == kMultiGpuStyleNone)
845 return false;
846 multi_gpu_style_ = style;
847 return true;
850 bool GpuControlList::GpuControlListEntry::SetMultiGpuCategory(
851 const std::string& multi_gpu_category_string) {
852 MultiGpuCategory category =
853 StringToMultiGpuCategory(multi_gpu_category_string);
854 if (category == kMultiGpuCategoryNone)
855 return false;
856 multi_gpu_category_ = category;
857 return true;
860 bool GpuControlList::GpuControlListEntry::SetGLType(
861 const std::string& gl_type_string) {
862 GLType gl_type = StringToGLType(gl_type_string);
863 if (gl_type == kGLTypeNone)
864 return false;
865 gl_type_ = gl_type;
866 return true;
869 bool GpuControlList::GpuControlListEntry::SetDriverVendorInfo(
870 const std::string& vendor_value) {
871 driver_vendor_info_ = vendor_value;
872 return !driver_vendor_info_.empty();
875 bool GpuControlList::GpuControlListEntry::SetDriverVersionInfo(
876 const std::string& version_op,
877 const std::string& version_style,
878 const std::string& version_string,
879 const std::string& version_string2) {
880 driver_version_info_.reset(new VersionInfo(
881 version_op, version_style, version_string, version_string2));
882 return driver_version_info_->IsValid();
885 bool GpuControlList::GpuControlListEntry::SetDriverDateInfo(
886 const std::string& date_op,
887 const std::string& date_string,
888 const std::string& date_string2) {
889 driver_date_info_.reset(
890 new VersionInfo(date_op, std::string(), date_string, date_string2));
891 return driver_date_info_->IsValid();
894 bool GpuControlList::GpuControlListEntry::SetGLVersionInfo(
895 const std::string& version_op,
896 const std::string& version_string,
897 const std::string& version_string2) {
898 gl_version_info_.reset(new VersionInfo(
899 version_op, std::string(), version_string, version_string2));
900 return gl_version_info_->IsValid();
903 bool GpuControlList::GpuControlListEntry::SetGLVendorInfo(
904 const std::string& vendor_value) {
905 gl_vendor_info_ = vendor_value;
906 return !gl_vendor_info_.empty();
909 bool GpuControlList::GpuControlListEntry::SetGLRendererInfo(
910 const std::string& renderer_value) {
911 gl_renderer_info_ = renderer_value;
912 return !gl_renderer_info_.empty();
915 bool GpuControlList::GpuControlListEntry::SetGLExtensionsInfo(
916 const std::string& extensions_value) {
917 gl_extensions_info_ = extensions_value;
918 return !gl_extensions_info_.empty();
921 bool GpuControlList::GpuControlListEntry::SetGLResetNotificationStrategyInfo(
922 const std::string& op,
923 const std::string& int_string,
924 const std::string& int_string2) {
925 gl_reset_notification_strategy_info_.reset(
926 new IntInfo(op, int_string, int_string2));
927 return gl_reset_notification_strategy_info_->IsValid();
930 bool GpuControlList::GpuControlListEntry::SetCpuBrand(
931 const std::string& cpu_value) {
932 cpu_brand_ = cpu_value;
933 return !cpu_brand_.empty();
936 bool GpuControlList::GpuControlListEntry::SetPerfGraphicsInfo(
937 const std::string& op,
938 const std::string& float_string,
939 const std::string& float_string2) {
940 perf_graphics_info_.reset(new FloatInfo(op, float_string, float_string2));
941 return perf_graphics_info_->IsValid();
944 bool GpuControlList::GpuControlListEntry::SetPerfGamingInfo(
945 const std::string& op,
946 const std::string& float_string,
947 const std::string& float_string2) {
948 perf_gaming_info_.reset(new FloatInfo(op, float_string, float_string2));
949 return perf_gaming_info_->IsValid();
952 bool GpuControlList::GpuControlListEntry::SetPerfOverallInfo(
953 const std::string& op,
954 const std::string& float_string,
955 const std::string& float_string2) {
956 perf_overall_info_.reset(new FloatInfo(op, float_string, float_string2));
957 return perf_overall_info_->IsValid();
960 bool GpuControlList::GpuControlListEntry::AddMachineModelName(
961 const std::string& model_name) {
962 if (model_name.empty())
963 return false;
964 machine_model_name_list_.push_back(model_name);
965 return true;
968 bool GpuControlList::GpuControlListEntry::SetMachineModelVersionInfo(
969 const std::string& version_op,
970 const std::string& version_string,
971 const std::string& version_string2) {
972 machine_model_version_info_.reset(new VersionInfo(
973 version_op, std::string(), version_string, version_string2));
974 return machine_model_version_info_->IsValid();
977 bool GpuControlList::GpuControlListEntry::SetGpuCountInfo(
978 const std::string& op,
979 const std::string& int_string,
980 const std::string& int_string2) {
981 gpu_count_info_.reset(new IntInfo(op, int_string, int_string2));
982 return gpu_count_info_->IsValid();
985 void GpuControlList::GpuControlListEntry::SetDirectRenderingInfo(bool value) {
986 direct_rendering_info_.reset(new BoolInfo(value));
989 void GpuControlList::GpuControlListEntry::SetInProcessGPUInfo(bool value) {
990 in_process_gpu_info_.reset(new BoolInfo(value));
993 bool GpuControlList::GpuControlListEntry::SetFeatures(
994 const std::vector<std::string>& feature_strings,
995 const FeatureMap& feature_map,
996 bool supports_feature_type_all) {
997 size_t size = feature_strings.size();
998 if (size == 0)
999 return false;
1000 features_.clear();
1001 for (size_t i = 0; i < size; ++i) {
1002 int feature = 0;
1003 if (supports_feature_type_all && feature_strings[i] == "all") {
1004 for (FeatureMap::const_iterator iter = feature_map.begin();
1005 iter != feature_map.end(); ++iter)
1006 features_.insert(iter->second);
1007 continue;
1009 if (!StringToFeature(feature_strings[i], &feature, feature_map)) {
1010 features_.clear();
1011 return false;
1013 features_.insert(feature);
1015 return true;
1018 void GpuControlList::GpuControlListEntry::AddException(
1019 ScopedGpuControlListEntry exception) {
1020 exceptions_.push_back(exception);
1023 bool GpuControlList::GpuControlListEntry::GLVersionInfoMismatch(
1024 const std::string& gl_version) const {
1025 if (gl_version.empty())
1026 return false;
1028 if (gl_version_info_.get() == NULL && gl_type_ == kGLTypeNone)
1029 return false;
1031 std::vector<std::string> segments;
1032 base::SplitString(gl_version, ' ', &segments);
1033 std::string number;
1034 GLType gl_type = kGLTypeNone;
1035 if (segments.size() > 2 &&
1036 segments[0] == "OpenGL" && segments[1] == "ES") {
1037 bool full_match = RE2::FullMatch(segments[2], "([\\d.]+).*", &number);
1038 DCHECK(full_match);
1040 gl_type = kGLTypeGLES;
1041 if (segments.size() > 3 &&
1042 base::StartsWith(segments[3], "(ANGLE",
1043 base::CompareCase::INSENSITIVE_ASCII)) {
1044 gl_type = kGLTypeANGLE;
1046 } else {
1047 number = segments[0];
1048 gl_type = kGLTypeGL;
1051 if (gl_type_ != kGLTypeNone && gl_type_ != gl_type)
1052 return true;
1053 if (gl_version_info_.get() != NULL && !gl_version_info_->Contains(number))
1054 return true;
1055 return false;
1058 // static
1059 GpuControlList::GpuControlListEntry::MultiGpuStyle
1060 GpuControlList::GpuControlListEntry::StringToMultiGpuStyle(
1061 const std::string& style) {
1062 if (style == kMultiGpuStyleStringOptimus)
1063 return kMultiGpuStyleOptimus;
1064 if (style == kMultiGpuStyleStringAMDSwitchable)
1065 return kMultiGpuStyleAMDSwitchable;
1066 if (style == kMultiGpuStyleStringAMDSwitchableIntegrated)
1067 return kMultiGpuStyleAMDSwitchableIntegrated;
1068 if (style == kMultiGpuStyleStringAMDSwitchableDiscrete)
1069 return kMultiGpuStyleAMDSwitchableDiscrete;
1070 return kMultiGpuStyleNone;
1073 // static
1074 GpuControlList::GpuControlListEntry::MultiGpuCategory
1075 GpuControlList::GpuControlListEntry::StringToMultiGpuCategory(
1076 const std::string& category) {
1077 if (category == kMultiGpuCategoryStringPrimary)
1078 return kMultiGpuCategoryPrimary;
1079 if (category == kMultiGpuCategoryStringSecondary)
1080 return kMultiGpuCategorySecondary;
1081 if (category == kMultiGpuCategoryStringActive)
1082 return kMultiGpuCategoryActive;
1083 if (category == kMultiGpuCategoryStringAny)
1084 return kMultiGpuCategoryAny;
1085 return kMultiGpuCategoryNone;
1088 // static
1089 GpuControlList::GpuControlListEntry::GLType
1090 GpuControlList::GpuControlListEntry::StringToGLType(
1091 const std::string& gl_type) {
1092 if (gl_type == kGLTypeStringGL)
1093 return kGLTypeGL;
1094 if (gl_type == kGLTypeStringGLES)
1095 return kGLTypeGLES;
1096 if (gl_type == kGLTypeStringANGLE)
1097 return kGLTypeANGLE;
1098 return kGLTypeNone;
1101 // static
1102 GpuControlList::GpuControlListEntry::GLType
1103 GpuControlList::GpuControlListEntry::GetDefaultGLType() {
1104 #if defined(OS_CHROMEOS)
1105 return kGLTypeGL;
1106 #elif defined(OS_LINUX) || defined(OS_OPENBSD)
1107 return kGLTypeGL;
1108 #elif defined(OS_MACOSX)
1109 return kGLTypeGL;
1110 #elif defined(OS_WIN)
1111 return kGLTypeANGLE;
1112 #elif defined(OS_ANDROID)
1113 return kGLTypeGLES;
1114 #else
1115 return kGLTypeNone;
1116 #endif
1119 void GpuControlList::GpuControlListEntry::LogControlListMatch(
1120 const std::string& control_list_logging_name) const {
1121 static const char kControlListMatchMessage[] =
1122 "Control list match for rule #%u in %s.";
1123 VLOG(1) << base::StringPrintf(kControlListMatchMessage, id_,
1124 control_list_logging_name.c_str());
1127 bool GpuControlList::GpuControlListEntry::Contains(
1128 OsType os_type, const std::string& os_version,
1129 const GPUInfo& gpu_info) const {
1130 DCHECK(os_type != kOsAny);
1131 if (os_info_.get() != NULL && !os_info_->Contains(os_type, os_version))
1132 return false;
1133 if (vendor_id_ != 0) {
1134 std::vector<GPUInfo::GPUDevice> candidates;
1135 switch (multi_gpu_category_) {
1136 case kMultiGpuCategoryPrimary:
1137 candidates.push_back(gpu_info.gpu);
1138 break;
1139 case kMultiGpuCategorySecondary:
1140 candidates = gpu_info.secondary_gpus;
1141 break;
1142 case kMultiGpuCategoryAny:
1143 candidates = gpu_info.secondary_gpus;
1144 candidates.push_back(gpu_info.gpu);
1145 break;
1146 case kMultiGpuCategoryActive:
1147 if (gpu_info.gpu.active)
1148 candidates.push_back(gpu_info.gpu);
1149 for (size_t ii = 0; ii < gpu_info.secondary_gpus.size(); ++ii) {
1150 if (gpu_info.secondary_gpus[ii].active)
1151 candidates.push_back(gpu_info.secondary_gpus[ii]);
1153 default:
1154 break;
1157 GPUInfo::GPUDevice gpu;
1158 gpu.vendor_id = vendor_id_;
1159 bool found = false;
1160 if (device_id_list_.empty()) {
1161 for (size_t ii = 0; ii < candidates.size(); ++ii) {
1162 if (gpu.vendor_id == candidates[ii].vendor_id) {
1163 found = true;
1164 break;
1167 } else {
1168 for (size_t ii = 0; ii < device_id_list_.size(); ++ii) {
1169 gpu.device_id = device_id_list_[ii];
1170 for (size_t jj = 0; jj < candidates.size(); ++jj) {
1171 if (gpu.vendor_id == candidates[jj].vendor_id &&
1172 gpu.device_id == candidates[jj].device_id) {
1173 found = true;
1174 break;
1179 if (!found)
1180 return false;
1182 switch (multi_gpu_style_) {
1183 case kMultiGpuStyleOptimus:
1184 if (!gpu_info.optimus)
1185 return false;
1186 break;
1187 case kMultiGpuStyleAMDSwitchable:
1188 if (!gpu_info.amd_switchable)
1189 return false;
1190 break;
1191 case kMultiGpuStyleAMDSwitchableDiscrete:
1192 if (!gpu_info.amd_switchable)
1193 return false;
1194 // The discrete GPU is always the primary GPU.
1195 // This is guaranteed by GpuInfoCollector.
1196 if (!gpu_info.gpu.active)
1197 return false;
1198 break;
1199 case kMultiGpuStyleAMDSwitchableIntegrated:
1200 if (!gpu_info.amd_switchable)
1201 return false;
1202 // Assume the integrated GPU is the first in the secondary GPU list.
1203 if (gpu_info.secondary_gpus.size() == 0 ||
1204 !gpu_info.secondary_gpus[0].active)
1205 return false;
1206 break;
1207 case kMultiGpuStyleNone:
1208 break;
1210 if (StringMismatch(gpu_info.driver_vendor, driver_vendor_info_))
1211 return false;
1212 if (driver_version_info_.get() != NULL && !gpu_info.driver_version.empty()) {
1213 if (!driver_version_info_->Contains(gpu_info.driver_version))
1214 return false;
1216 if (driver_date_info_.get() != NULL && !gpu_info.driver_date.empty()) {
1217 if (!driver_date_info_->Contains(gpu_info.driver_date, '-'))
1218 return false;
1220 if (GLVersionInfoMismatch(gpu_info.gl_version))
1221 return false;
1222 if (StringMismatch(gpu_info.gl_vendor, gl_vendor_info_))
1223 return false;
1224 if (StringMismatch(gpu_info.gl_renderer, gl_renderer_info_))
1225 return false;
1226 if (StringMismatch(gpu_info.gl_extensions, gl_extensions_info_))
1227 return false;
1228 if (gl_reset_notification_strategy_info_.get() != NULL &&
1229 !gl_reset_notification_strategy_info_->Contains(
1230 gpu_info.gl_reset_notification_strategy))
1231 return false;
1232 if (!machine_model_name_list_.empty()) {
1233 if (gpu_info.machine_model_name.empty())
1234 return false;
1235 bool found_match = false;
1236 for (size_t ii = 0; ii < machine_model_name_list_.size(); ++ii) {
1237 if (RE2::FullMatch(gpu_info.machine_model_name,
1238 machine_model_name_list_[ii])) {
1239 found_match = true;
1240 break;
1243 if (!found_match)
1244 return false;
1246 if (machine_model_version_info_.get() != NULL &&
1247 (gpu_info.machine_model_version.empty() ||
1248 !machine_model_version_info_->Contains(gpu_info.machine_model_version)))
1249 return false;
1250 if (gpu_count_info_.get() != NULL &&
1251 !gpu_count_info_->Contains(gpu_info.secondary_gpus.size() + 1))
1252 return false;
1253 if (direct_rendering_info_.get() != NULL &&
1254 !direct_rendering_info_->Contains(gpu_info.direct_rendering))
1255 return false;
1256 if (in_process_gpu_info_.get() != NULL &&
1257 !in_process_gpu_info_->Contains(gpu_info.in_process_gpu))
1258 return false;
1259 if (!cpu_brand_.empty()) {
1260 base::CPU cpu_info;
1261 if (StringMismatch(cpu_info.cpu_brand(), cpu_brand_))
1262 return false;
1265 for (size_t i = 0; i < exceptions_.size(); ++i) {
1266 if (exceptions_[i]->Contains(os_type, os_version, gpu_info) &&
1267 !exceptions_[i]->NeedsMoreInfo(gpu_info, true))
1268 return false;
1270 return true;
1273 bool GpuControlList::GpuControlListEntry::NeedsMoreInfo(
1274 const GPUInfo& gpu_info,
1275 bool consider_exceptions) const {
1276 // We only check for missing info that might be collected with a gl context.
1277 // If certain info is missing due to some error, say, we fail to collect
1278 // vendor_id/device_id, then even if we launch GPU process and create a gl
1279 // context, we won't gather such missing info, so we still return false.
1280 if (!driver_vendor_info_.empty() && gpu_info.driver_vendor.empty())
1281 return true;
1282 if (driver_version_info_.get() && gpu_info.driver_version.empty())
1283 return true;
1284 if (!gl_vendor_info_.empty() && gpu_info.gl_vendor.empty())
1285 return true;
1286 if (!gl_renderer_info_.empty() && gpu_info.gl_renderer.empty())
1287 return true;
1289 if (consider_exceptions) {
1290 for (size_t i = 0; i < exceptions_.size(); ++i) {
1291 if (exceptions_[i]->NeedsMoreInfo(gpu_info, consider_exceptions))
1292 return true;
1296 return false;
1299 GpuControlList::OsType GpuControlList::GpuControlListEntry::GetOsType() const {
1300 if (os_info_.get() == NULL)
1301 return kOsAny;
1302 return os_info_->type();
1305 uint32 GpuControlList::GpuControlListEntry::id() const {
1306 return id_;
1309 bool GpuControlList::GpuControlListEntry::disabled() const {
1310 return disabled_;
1313 const std::set<int>& GpuControlList::GpuControlListEntry::features() const {
1314 return features_;
1317 void GpuControlList::GpuControlListEntry::GetFeatureNames(
1318 base::ListValue* feature_names,
1319 const FeatureMap& feature_map,
1320 bool supports_feature_type_all) const {
1321 DCHECK(feature_names);
1322 if (supports_feature_type_all && features_.size() == feature_map.size()) {
1323 feature_names->AppendString("all");
1324 return;
1326 for (FeatureMap::const_iterator iter = feature_map.begin();
1327 iter != feature_map.end(); ++iter) {
1328 if (features_.count(iter->second) > 0)
1329 feature_names->AppendString(iter->first);
1333 // static
1334 bool GpuControlList::GpuControlListEntry::StringToFeature(
1335 const std::string& feature_name, int* feature_id,
1336 const FeatureMap& feature_map) {
1337 FeatureMap::const_iterator iter = feature_map.find(feature_name);
1338 if (iter != feature_map.end()) {
1339 *feature_id = iter->second;
1340 return true;
1342 return false;
1345 GpuControlList::GpuControlList()
1346 : max_entry_id_(0),
1347 needs_more_info_(false),
1348 supports_feature_type_all_(false),
1349 control_list_logging_enabled_(false) {
1352 GpuControlList::~GpuControlList() {
1353 Clear();
1356 bool GpuControlList::LoadList(
1357 const std::string& json_context,
1358 GpuControlList::OsFilter os_filter) {
1359 scoped_ptr<base::Value> root = base::JSONReader::Read(json_context);
1360 if (root.get() == NULL || !root->IsType(base::Value::TYPE_DICTIONARY))
1361 return false;
1363 base::DictionaryValue* root_dictionary =
1364 static_cast<base::DictionaryValue*>(root.get());
1365 DCHECK(root_dictionary);
1366 return LoadList(*root_dictionary, os_filter);
1369 bool GpuControlList::LoadList(const base::DictionaryValue& parsed_json,
1370 GpuControlList::OsFilter os_filter) {
1371 std::vector<ScopedGpuControlListEntry> entries;
1373 parsed_json.GetString("version", &version_);
1374 std::vector<std::string> pieces;
1375 if (!ProcessVersionString(version_, '.', &pieces))
1376 return false;
1378 const base::ListValue* list = NULL;
1379 if (!parsed_json.GetList("entries", &list))
1380 return false;
1382 uint32 max_entry_id = 0;
1383 for (size_t i = 0; i < list->GetSize(); ++i) {
1384 const base::DictionaryValue* list_item = NULL;
1385 bool valid = list->GetDictionary(i, &list_item);
1386 if (!valid || list_item == NULL)
1387 return false;
1388 ScopedGpuControlListEntry entry(GpuControlListEntry::GetEntryFromValue(
1389 list_item, true, feature_map_, supports_feature_type_all_));
1390 if (entry.get() == NULL)
1391 return false;
1392 if (entry->id() > max_entry_id)
1393 max_entry_id = entry->id();
1394 entries.push_back(entry);
1397 Clear();
1398 OsType my_os = GetOsType();
1399 for (size_t i = 0; i < entries.size(); ++i) {
1400 OsType entry_os = entries[i]->GetOsType();
1401 if (os_filter == GpuControlList::kAllOs ||
1402 entry_os == kOsAny || entry_os == my_os)
1403 entries_.push_back(entries[i]);
1405 max_entry_id_ = max_entry_id;
1406 return true;
1409 std::set<int> GpuControlList::MakeDecision(
1410 GpuControlList::OsType os,
1411 std::string os_version,
1412 const GPUInfo& gpu_info) {
1413 active_entries_.clear();
1414 std::set<int> features;
1416 needs_more_info_ = false;
1417 // Has all features permanently in the list without any possibility of
1418 // removal in the future (subset of "features" set).
1419 std::set<int> permanent_features;
1420 // Has all features absent from "features" set that could potentially be
1421 // included later with more information.
1422 std::set<int> potential_features;
1424 if (os == kOsAny)
1425 os = GetOsType();
1426 if (os_version.empty())
1427 os_version = base::SysInfo::OperatingSystemVersion();
1429 for (size_t i = 0; i < entries_.size(); ++i) {
1430 ScopedGpuControlListEntry entry = entries_[i];
1431 if (entry->Contains(os, os_version, gpu_info)) {
1432 bool needs_more_info_main = entry->NeedsMoreInfo(gpu_info, false);
1433 bool needs_more_info_exception = entry->NeedsMoreInfo(gpu_info, true);
1435 if (!entry->disabled()) {
1436 if (control_list_logging_enabled_)
1437 entry->LogControlListMatch(control_list_logging_name_);
1438 // Only look at main entry info when deciding what to add to "features"
1439 // set. If we don't have enough info for an exception, it's safer if we
1440 // just ignore the exception and assume the exception doesn't apply.
1441 for (std::set<int>::const_iterator iter = entry->features().begin();
1442 iter != entry->features().end(); ++iter) {
1443 if (needs_more_info_main) {
1444 if (!features.count(*iter))
1445 potential_features.insert(*iter);
1446 } else {
1447 features.insert(*iter);
1448 potential_features.erase(*iter);
1449 if (!needs_more_info_exception)
1450 permanent_features.insert(*iter);
1455 if (!needs_more_info_main)
1456 active_entries_.push_back(entry);
1460 needs_more_info_ = permanent_features.size() < features.size() ||
1461 !potential_features.empty();
1462 return features;
1465 void GpuControlList::GetDecisionEntries(
1466 std::vector<uint32>* entry_ids, bool disabled) const {
1467 DCHECK(entry_ids);
1468 entry_ids->clear();
1469 for (size_t i = 0; i < active_entries_.size(); ++i) {
1470 if (disabled == active_entries_[i]->disabled())
1471 entry_ids->push_back(active_entries_[i]->id());
1475 std::vector<std::string> GpuControlList::GetDisabledExtensions() {
1476 std::set<std::string> disabled_extensions;
1477 for (size_t i = 0; i < active_entries_.size(); ++i) {
1478 GpuControlListEntry* entry = active_entries_[i].get();
1480 if (entry->disabled())
1481 continue;
1483 disabled_extensions.insert(entry->disabled_extensions().begin(),
1484 entry->disabled_extensions().end());
1486 return std::vector<std::string>(disabled_extensions.begin(),
1487 disabled_extensions.end());
1490 void GpuControlList::GetReasons(base::ListValue* problem_list,
1491 const std::string& tag) const {
1492 DCHECK(problem_list);
1493 for (size_t i = 0; i < active_entries_.size(); ++i) {
1494 GpuControlListEntry* entry = active_entries_[i].get();
1495 if (entry->disabled())
1496 continue;
1497 base::DictionaryValue* problem = new base::DictionaryValue();
1499 problem->SetString("description", entry->description());
1501 base::ListValue* cr_bugs = new base::ListValue();
1502 for (size_t j = 0; j < entry->cr_bugs().size(); ++j)
1503 cr_bugs->Append(new base::FundamentalValue(entry->cr_bugs()[j]));
1504 problem->Set("crBugs", cr_bugs);
1506 base::ListValue* webkit_bugs = new base::ListValue();
1507 for (size_t j = 0; j < entry->webkit_bugs().size(); ++j) {
1508 webkit_bugs->Append(new base::FundamentalValue(entry->webkit_bugs()[j]));
1510 problem->Set("webkitBugs", webkit_bugs);
1512 base::ListValue* features = new base::ListValue();
1513 entry->GetFeatureNames(features, feature_map_, supports_feature_type_all_);
1514 problem->Set("affectedGpuSettings", features);
1516 DCHECK(tag == "workarounds" || tag == "disabledFeatures");
1517 problem->SetString("tag", tag);
1519 problem_list->Append(problem);
1523 size_t GpuControlList::num_entries() const {
1524 return entries_.size();
1527 uint32 GpuControlList::max_entry_id() const {
1528 return max_entry_id_;
1531 std::string GpuControlList::version() const {
1532 return version_;
1535 GpuControlList::OsType GpuControlList::GetOsType() {
1536 #if defined(OS_CHROMEOS)
1537 return kOsChromeOS;
1538 #elif defined(OS_WIN)
1539 return kOsWin;
1540 #elif defined(OS_ANDROID)
1541 return kOsAndroid;
1542 #elif defined(OS_LINUX) || defined(OS_OPENBSD)
1543 return kOsLinux;
1544 #elif defined(OS_MACOSX)
1545 return kOsMacosx;
1546 #else
1547 return kOsUnknown;
1548 #endif
1551 void GpuControlList::Clear() {
1552 entries_.clear();
1553 active_entries_.clear();
1554 max_entry_id_ = 0;
1557 // static
1558 GpuControlList::NumericOp GpuControlList::StringToNumericOp(
1559 const std::string& op) {
1560 if (op == "=")
1561 return kEQ;
1562 if (op == "<")
1563 return kLT;
1564 if (op == "<=")
1565 return kLE;
1566 if (op == ">")
1567 return kGT;
1568 if (op == ">=")
1569 return kGE;
1570 if (op == "any")
1571 return kAny;
1572 if (op == "between")
1573 return kBetween;
1574 return kUnknown;
1577 void GpuControlList::AddSupportedFeature(
1578 const std::string& feature_name, int feature_id) {
1579 feature_map_[feature_name] = feature_id;
1582 void GpuControlList::set_supports_feature_type_all(bool supported) {
1583 supports_feature_type_all_ = supported;
1586 } // namespace gpu