Bug 1610630 [wpt PR 21320] - Add meta:timeout=long to FileSystemBaseHandle-postMessag...
[gecko.git] / widget / GfxInfoBase.cpp
blob1976b6fd1e54950c1f5fa6c56b71b3589f828f24
1 /* vim: se cin sw=2 ts=2 et : */
2 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
8 #include "mozilla/ArrayUtils.h"
10 #include "GfxInfoBase.h"
12 #include "GfxDriverInfo.h"
13 #include "js/Array.h" // JS::GetArrayLength, JS::NewArrayObject
14 #include "nsCOMPtr.h"
15 #include "nsCOMArray.h"
16 #include "nsString.h"
17 #include "nsUnicharUtils.h"
18 #include "nsVersionComparator.h"
19 #include "mozilla/Services.h"
20 #include "mozilla/Observer.h"
21 #include "nsIObserver.h"
22 #include "nsIObserverService.h"
23 #include "nsTArray.h"
24 #include "nsXULAppAPI.h"
25 #include "nsIXULAppInfo.h"
26 #include "mozilla/Preferences.h"
27 #include "mozilla/StaticPrefs_gfx.h"
28 #include "mozilla/gfx/2D.h"
29 #include "mozilla/gfx/GPUProcessManager.h"
30 #include "mozilla/gfx/Logging.h"
31 #include "mozilla/gfx/gfxVars.h"
32 #include "mozilla/layers/PaintThread.h"
34 #include "gfxPlatform.h"
35 #include "gfxConfig.h"
36 #include "DriverCrashGuard.h"
38 using namespace mozilla::widget;
39 using namespace mozilla;
40 using mozilla::MutexAutoLock;
42 nsTArray<GfxDriverInfo>* GfxInfoBase::sDriverInfo;
43 nsTArray<dom::GfxInfoFeatureStatus>* GfxInfoBase::sFeatureStatus;
44 bool GfxInfoBase::sDriverInfoObserverInitialized;
45 bool GfxInfoBase::sShutdownOccurred;
47 // Observes for shutdown so that the child GfxDriverInfo list is freed.
48 class ShutdownObserver : public nsIObserver {
49 virtual ~ShutdownObserver() {}
51 public:
52 ShutdownObserver() {}
54 NS_DECL_ISUPPORTS
56 NS_IMETHOD Observe(nsISupports* subject, const char* aTopic,
57 const char16_t* aData) override {
58 MOZ_ASSERT(strcmp(aTopic, NS_XPCOM_SHUTDOWN_OBSERVER_ID) == 0);
60 delete GfxInfoBase::sDriverInfo;
61 GfxInfoBase::sDriverInfo = nullptr;
63 delete GfxInfoBase::sFeatureStatus;
64 GfxInfoBase::sFeatureStatus = nullptr;
66 for (uint32_t i = 0; i < DeviceFamilyMax; i++) {
67 delete GfxDriverInfo::sDeviceFamilies[i];
68 GfxDriverInfo::sDeviceFamilies[i] = nullptr;
71 for (uint32_t i = 0; i < DeviceVendorMax; i++) {
72 delete GfxDriverInfo::sDeviceVendors[i];
73 GfxDriverInfo::sDeviceVendors[i] = nullptr;
76 for (uint32_t i = 0; i < DriverVendorMax; i++) {
77 delete GfxDriverInfo::sDriverVendors[i];
78 GfxDriverInfo::sDriverVendors[i] = nullptr;
81 GfxInfoBase::sShutdownOccurred = true;
83 return NS_OK;
87 NS_IMPL_ISUPPORTS(ShutdownObserver, nsIObserver)
89 static void InitGfxDriverInfoShutdownObserver() {
90 if (GfxInfoBase::sDriverInfoObserverInitialized) return;
92 GfxInfoBase::sDriverInfoObserverInitialized = true;
94 nsCOMPtr<nsIObserverService> observerService = services::GetObserverService();
95 if (!observerService) {
96 NS_WARNING("Could not get observer service!");
97 return;
100 ShutdownObserver* obs = new ShutdownObserver();
101 observerService->AddObserver(obs, NS_XPCOM_SHUTDOWN_OBSERVER_ID, false);
104 using namespace mozilla::widget;
105 using namespace mozilla::gfx;
106 using namespace mozilla;
108 NS_IMPL_ISUPPORTS(GfxInfoBase, nsIGfxInfo, nsIObserver,
109 nsISupportsWeakReference)
111 #define BLACKLIST_PREF_BRANCH "gfx.blacklist."
112 #define SUGGESTED_VERSION_PREF BLACKLIST_PREF_BRANCH "suggested-driver-version"
113 #define BLACKLIST_ENTRY_TAG_NAME "gfxBlacklistEntry"
115 static const char* GetPrefNameForFeature(int32_t aFeature) {
116 const char* name = nullptr;
117 switch (aFeature) {
118 case nsIGfxInfo::FEATURE_DIRECT2D:
119 name = BLACKLIST_PREF_BRANCH "direct2d";
120 break;
121 case nsIGfxInfo::FEATURE_DIRECT3D_9_LAYERS:
122 name = BLACKLIST_PREF_BRANCH "layers.direct3d9";
123 break;
124 case nsIGfxInfo::FEATURE_DIRECT3D_10_LAYERS:
125 name = BLACKLIST_PREF_BRANCH "layers.direct3d10";
126 break;
127 case nsIGfxInfo::FEATURE_DIRECT3D_10_1_LAYERS:
128 name = BLACKLIST_PREF_BRANCH "layers.direct3d10-1";
129 break;
130 case nsIGfxInfo::FEATURE_DIRECT3D_11_LAYERS:
131 name = BLACKLIST_PREF_BRANCH "layers.direct3d11";
132 break;
133 case nsIGfxInfo::FEATURE_DIRECT3D_11_ANGLE:
134 name = BLACKLIST_PREF_BRANCH "direct3d11angle";
135 break;
136 case nsIGfxInfo::FEATURE_HARDWARE_VIDEO_DECODING:
137 name = BLACKLIST_PREF_BRANCH "hardwarevideodecoding";
138 break;
139 case nsIGfxInfo::FEATURE_OPENGL_LAYERS:
140 name = BLACKLIST_PREF_BRANCH "layers.opengl";
141 break;
142 case nsIGfxInfo::FEATURE_WEBGL_OPENGL:
143 name = BLACKLIST_PREF_BRANCH "webgl.opengl";
144 break;
145 case nsIGfxInfo::FEATURE_WEBGL_ANGLE:
146 name = BLACKLIST_PREF_BRANCH "webgl.angle";
147 break;
148 case nsIGfxInfo::FEATURE_WEBGL_MSAA:
149 name = BLACKLIST_PREF_BRANCH "webgl.msaa";
150 break;
151 case nsIGfxInfo::FEATURE_STAGEFRIGHT:
152 name = BLACKLIST_PREF_BRANCH "stagefright";
153 break;
154 case nsIGfxInfo::FEATURE_WEBRTC_HW_ACCELERATION_H264:
155 name = BLACKLIST_PREF_BRANCH "webrtc.hw.acceleration.h264";
156 break;
157 case nsIGfxInfo::FEATURE_WEBRTC_HW_ACCELERATION_ENCODE:
158 name = BLACKLIST_PREF_BRANCH "webrtc.hw.acceleration.encode";
159 break;
160 case nsIGfxInfo::FEATURE_WEBRTC_HW_ACCELERATION_DECODE:
161 name = BLACKLIST_PREF_BRANCH "webrtc.hw.acceleration.decode";
162 break;
163 case nsIGfxInfo::FEATURE_CANVAS2D_ACCELERATION:
164 name = BLACKLIST_PREF_BRANCH "canvas2d.acceleration";
165 break;
166 case nsIGfxInfo::FEATURE_DX_INTEROP2:
167 name = BLACKLIST_PREF_BRANCH "dx.interop2";
168 break;
169 case nsIGfxInfo::FEATURE_GPU_PROCESS:
170 name = BLACKLIST_PREF_BRANCH "gpu.process";
171 break;
172 case nsIGfxInfo::FEATURE_WEBGL2:
173 name = BLACKLIST_PREF_BRANCH "webgl2";
174 break;
175 case nsIGfxInfo::FEATURE_ADVANCED_LAYERS:
176 name = BLACKLIST_PREF_BRANCH "layers.advanced";
177 break;
178 case nsIGfxInfo::FEATURE_D3D11_KEYED_MUTEX:
179 name = BLACKLIST_PREF_BRANCH "d3d11.keyed.mutex";
180 break;
181 case nsIGfxInfo::FEATURE_WEBRENDER:
182 name = BLACKLIST_PREF_BRANCH "webrender";
183 break;
184 case nsIGfxInfo::FEATURE_WEBRENDER_COMPOSITOR:
185 name = BLACKLIST_PREF_BRANCH "webrender.compositor";
186 break;
187 case nsIGfxInfo::FEATURE_DX_NV12:
188 name = BLACKLIST_PREF_BRANCH "dx.nv12";
189 break;
190 case nsIGfxInfo::FEATURE_DX_P010:
191 name = BLACKLIST_PREF_BRANCH "dx.p010";
192 break;
193 case nsIGfxInfo::FEATURE_DX_P016:
194 name = BLACKLIST_PREF_BRANCH "dx.p016";
195 break;
196 case nsIGfxInfo::FEATURE_VP8_HW_DECODE:
197 case nsIGfxInfo::FEATURE_VP9_HW_DECODE:
198 // We don't provide prefs for these features as these are
199 // not handling downloadable blocklist.
200 break;
201 case nsIGfxInfo::FEATURE_GL_SWIZZLE:
202 name = BLACKLIST_PREF_BRANCH "gl.swizzle";
203 break;
204 default:
205 MOZ_ASSERT_UNREACHABLE("Unexpected nsIGfxInfo feature?!");
206 break;
209 return name;
212 // Returns the value of the pref for the relevant feature in aValue.
213 // If the pref doesn't exist, aValue is not touched, and returns false.
214 static bool GetPrefValueForFeature(int32_t aFeature, int32_t& aValue,
215 nsACString& aFailureId) {
216 const char* prefname = GetPrefNameForFeature(aFeature);
217 if (!prefname) return false;
219 aValue = nsIGfxInfo::FEATURE_STATUS_UNKNOWN;
220 if (!NS_SUCCEEDED(Preferences::GetInt(prefname, &aValue))) {
221 return false;
224 nsCString failureprefname(prefname);
225 failureprefname += ".failureid";
226 nsAutoCString failureValue;
227 nsresult rv = Preferences::GetCString(failureprefname.get(), failureValue);
228 if (NS_SUCCEEDED(rv)) {
229 aFailureId = failureValue.get();
230 } else {
231 aFailureId = "FEATURE_FAILURE_BLACKLIST_PREF";
234 return true;
237 static void SetPrefValueForFeature(int32_t aFeature, int32_t aValue,
238 const nsACString& aFailureId) {
239 const char* prefname = GetPrefNameForFeature(aFeature);
240 if (!prefname) return;
241 if (XRE_IsParentProcess()) {
242 delete GfxInfoBase::sFeatureStatus;
243 GfxInfoBase::sFeatureStatus = nullptr;
246 Preferences::SetInt(prefname, aValue);
247 if (!aFailureId.IsEmpty()) {
248 nsCString failureprefname(prefname);
249 failureprefname += ".failureid";
250 Preferences::SetCString(failureprefname.get(), aFailureId);
254 static void RemovePrefForFeature(int32_t aFeature) {
255 const char* prefname = GetPrefNameForFeature(aFeature);
256 if (!prefname) return;
258 if (XRE_IsParentProcess()) {
259 delete GfxInfoBase::sFeatureStatus;
260 GfxInfoBase::sFeatureStatus = nullptr;
262 Preferences::ClearUser(prefname);
265 static bool GetPrefValueForDriverVersion(nsCString& aVersion) {
266 return NS_SUCCEEDED(
267 Preferences::GetCString(SUGGESTED_VERSION_PREF, aVersion));
270 static void SetPrefValueForDriverVersion(const nsAString& aVersion) {
271 Preferences::SetString(SUGGESTED_VERSION_PREF, aVersion);
274 static void RemovePrefForDriverVersion() {
275 Preferences::ClearUser(SUGGESTED_VERSION_PREF);
278 static OperatingSystem BlacklistOSToOperatingSystem(const nsAString& os) {
279 if (os.EqualsLiteral("WINNT 6.1"))
280 return OperatingSystem::Windows7;
281 else if (os.EqualsLiteral("WINNT 6.2"))
282 return OperatingSystem::Windows8;
283 else if (os.EqualsLiteral("WINNT 6.3"))
284 return OperatingSystem::Windows8_1;
285 else if (os.EqualsLiteral("WINNT 10.0"))
286 return OperatingSystem::Windows10;
287 else if (os.EqualsLiteral("Linux"))
288 return OperatingSystem::Linux;
289 else if (os.EqualsLiteral("Darwin 9"))
290 return OperatingSystem::OSX10_5;
291 else if (os.EqualsLiteral("Darwin 10"))
292 return OperatingSystem::OSX10_6;
293 else if (os.EqualsLiteral("Darwin 11"))
294 return OperatingSystem::OSX10_7;
295 else if (os.EqualsLiteral("Darwin 12"))
296 return OperatingSystem::OSX10_8;
297 else if (os.EqualsLiteral("Darwin 13"))
298 return OperatingSystem::OSX10_9;
299 else if (os.EqualsLiteral("Darwin 14"))
300 return OperatingSystem::OSX10_10;
301 else if (os.EqualsLiteral("Darwin 15"))
302 return OperatingSystem::OSX10_11;
303 else if (os.EqualsLiteral("Darwin 16"))
304 return OperatingSystem::OSX10_12;
305 else if (os.EqualsLiteral("Darwin 17"))
306 return OperatingSystem::OSX10_13;
307 else if (os.EqualsLiteral("Darwin 18"))
308 return OperatingSystem::OSX10_14;
309 else if (os.EqualsLiteral("Darwin 19"))
310 return OperatingSystem::OSX10_15;
311 else if (os.EqualsLiteral("Android"))
312 return OperatingSystem::Android;
313 // For historical reasons, "All" in blocklist means "All Windows"
314 else if (os.EqualsLiteral("All"))
315 return OperatingSystem::Windows;
317 return OperatingSystem::Unknown;
320 static GfxDeviceFamily* BlacklistDevicesToDeviceFamily(
321 nsTArray<nsCString>& devices) {
322 if (devices.Length() == 0) return nullptr;
324 // For each device, get its device ID, and return a freshly-allocated
325 // GfxDeviceFamily with the contents of that array.
326 GfxDeviceFamily* deviceIds = new GfxDeviceFamily;
328 for (uint32_t i = 0; i < devices.Length(); ++i) {
329 // We make sure we don't add any "empty" device entries to the array, so
330 // we don't need to check if devices[i] is empty.
331 deviceIds->AppendElement(NS_ConvertUTF8toUTF16(devices[i]));
334 return deviceIds;
337 static int32_t BlacklistFeatureToGfxFeature(const nsAString& aFeature) {
338 MOZ_ASSERT(!aFeature.IsEmpty());
339 if (aFeature.EqualsLiteral("DIRECT2D"))
340 return nsIGfxInfo::FEATURE_DIRECT2D;
341 else if (aFeature.EqualsLiteral("DIRECT3D_9_LAYERS"))
342 return nsIGfxInfo::FEATURE_DIRECT3D_9_LAYERS;
343 else if (aFeature.EqualsLiteral("DIRECT3D_10_LAYERS"))
344 return nsIGfxInfo::FEATURE_DIRECT3D_10_LAYERS;
345 else if (aFeature.EqualsLiteral("DIRECT3D_10_1_LAYERS"))
346 return nsIGfxInfo::FEATURE_DIRECT3D_10_1_LAYERS;
347 else if (aFeature.EqualsLiteral("DIRECT3D_11_LAYERS"))
348 return nsIGfxInfo::FEATURE_DIRECT3D_11_LAYERS;
349 else if (aFeature.EqualsLiteral("DIRECT3D_11_ANGLE"))
350 return nsIGfxInfo::FEATURE_DIRECT3D_11_ANGLE;
351 else if (aFeature.EqualsLiteral("HARDWARE_VIDEO_DECODING"))
352 return nsIGfxInfo::FEATURE_HARDWARE_VIDEO_DECODING;
353 else if (aFeature.EqualsLiteral("OPENGL_LAYERS"))
354 return nsIGfxInfo::FEATURE_OPENGL_LAYERS;
355 else if (aFeature.EqualsLiteral("WEBGL_OPENGL"))
356 return nsIGfxInfo::FEATURE_WEBGL_OPENGL;
357 else if (aFeature.EqualsLiteral("WEBGL_ANGLE"))
358 return nsIGfxInfo::FEATURE_WEBGL_ANGLE;
359 else if (aFeature.EqualsLiteral("WEBGL_MSAA"))
360 return nsIGfxInfo::FEATURE_WEBGL_MSAA;
361 else if (aFeature.EqualsLiteral("STAGEFRIGHT"))
362 return nsIGfxInfo::FEATURE_STAGEFRIGHT;
363 else if (aFeature.EqualsLiteral("WEBRTC_HW_ACCELERATION_ENCODE"))
364 return nsIGfxInfo::FEATURE_WEBRTC_HW_ACCELERATION_ENCODE;
365 else if (aFeature.EqualsLiteral("WEBRTC_HW_ACCELERATION_DECODE"))
366 return nsIGfxInfo::FEATURE_WEBRTC_HW_ACCELERATION_DECODE;
367 else if (aFeature.EqualsLiteral("WEBRTC_HW_ACCELERATION_H264"))
368 return nsIGfxInfo::FEATURE_WEBRTC_HW_ACCELERATION_H264;
369 else if (aFeature.EqualsLiteral("CANVAS2D_ACCELERATION"))
370 return nsIGfxInfo::FEATURE_CANVAS2D_ACCELERATION;
371 else if (aFeature.EqualsLiteral("DX_INTEROP2"))
372 return nsIGfxInfo::FEATURE_DX_INTEROP2;
373 else if (aFeature.EqualsLiteral("GPU_PROCESS"))
374 return nsIGfxInfo::FEATURE_GPU_PROCESS;
375 else if (aFeature.EqualsLiteral("WEBGL2"))
376 return nsIGfxInfo::FEATURE_WEBGL2;
377 else if (aFeature.EqualsLiteral("ADVANCED_LAYERS"))
378 return nsIGfxInfo::FEATURE_ADVANCED_LAYERS;
379 else if (aFeature.EqualsLiteral("D3D11_KEYED_MUTEX"))
380 return nsIGfxInfo::FEATURE_D3D11_KEYED_MUTEX;
381 else if (aFeature.EqualsLiteral("WEBRENDER"))
382 return nsIGfxInfo::FEATURE_WEBRENDER;
383 else if (aFeature.EqualsLiteral("WEBRENDER_COMPOSITOR"))
384 return nsIGfxInfo::FEATURE_WEBRENDER_COMPOSITOR;
385 else if (aFeature.EqualsLiteral("DX_NV12"))
386 return nsIGfxInfo::FEATURE_DX_NV12;
387 // We do not support FEATURE_VP8_HW_DECODE and FEATURE_VP9_HW_DECODE
388 // in downloadable blocklist.
389 else if (aFeature.EqualsLiteral("GL_SWIZZLE"))
390 return nsIGfxInfo::FEATURE_GL_SWIZZLE;
392 // If we don't recognize the feature, it may be new, and something
393 // this version doesn't understand. So, nothing to do. This is
394 // different from feature not being specified at all, in which case
395 // this method should not get called and we should continue with the
396 // "all features" blocklisting.
397 return -1;
400 static int32_t BlacklistFeatureStatusToGfxFeatureStatus(
401 const nsAString& aStatus) {
402 if (aStatus.EqualsLiteral("STATUS_OK"))
403 return nsIGfxInfo::FEATURE_STATUS_OK;
404 else if (aStatus.EqualsLiteral("BLOCKED_DRIVER_VERSION"))
405 return nsIGfxInfo::FEATURE_BLOCKED_DRIVER_VERSION;
406 else if (aStatus.EqualsLiteral("BLOCKED_DEVICE"))
407 return nsIGfxInfo::FEATURE_BLOCKED_DEVICE;
408 else if (aStatus.EqualsLiteral("DISCOURAGED"))
409 return nsIGfxInfo::FEATURE_DISCOURAGED;
410 else if (aStatus.EqualsLiteral("BLOCKED_OS_VERSION"))
411 return nsIGfxInfo::FEATURE_BLOCKED_OS_VERSION;
413 // Do not allow it to set STATUS_UNKNOWN. Also, we are not
414 // expecting the "mismatch" status showing up here.
416 return nsIGfxInfo::FEATURE_STATUS_OK;
419 static VersionComparisonOp BlacklistComparatorToComparisonOp(
420 const nsAString& op) {
421 if (op.EqualsLiteral("LESS_THAN"))
422 return DRIVER_LESS_THAN;
423 else if (op.EqualsLiteral("BUILD_ID_LESS_THAN"))
424 return DRIVER_BUILD_ID_LESS_THAN;
425 else if (op.EqualsLiteral("LESS_THAN_OR_EQUAL"))
426 return DRIVER_LESS_THAN_OR_EQUAL;
427 else if (op.EqualsLiteral("BUILD_ID_LESS_THAN_OR_EQUAL"))
428 return DRIVER_BUILD_ID_LESS_THAN_OR_EQUAL;
429 else if (op.EqualsLiteral("GREATER_THAN"))
430 return DRIVER_GREATER_THAN;
431 else if (op.EqualsLiteral("GREATER_THAN_OR_EQUAL"))
432 return DRIVER_GREATER_THAN_OR_EQUAL;
433 else if (op.EqualsLiteral("EQUAL"))
434 return DRIVER_EQUAL;
435 else if (op.EqualsLiteral("NOT_EQUAL"))
436 return DRIVER_NOT_EQUAL;
437 else if (op.EqualsLiteral("BETWEEN_EXCLUSIVE"))
438 return DRIVER_BETWEEN_EXCLUSIVE;
439 else if (op.EqualsLiteral("BETWEEN_INCLUSIVE"))
440 return DRIVER_BETWEEN_INCLUSIVE;
441 else if (op.EqualsLiteral("BETWEEN_INCLUSIVE_START"))
442 return DRIVER_BETWEEN_INCLUSIVE_START;
444 return DRIVER_COMPARISON_IGNORED;
448 Deserialize Blacklist entries from string.
449 e.g:
450 os:WINNT 6.0\tvendor:0x8086\tdevices:0x2582,0x2782\tfeature:DIRECT3D_10_LAYERS\tfeatureStatus:BLOCKED_DRIVER_VERSION\tdriverVersion:8.52.322.2202\tdriverVersionComparator:LESS_THAN_OR_EQUAL
452 static bool BlacklistEntryToDriverInfo(nsCString& aBlacklistEntry,
453 GfxDriverInfo& aDriverInfo) {
454 // If we get an application version to be zero, something is not working
455 // and we are not going to bother checking the blocklist versions.
456 // See TestGfxWidgets.cpp for how version comparison works.
457 // <versionRange minVersion="42.0a1" maxVersion="45.0"></versionRange>
458 static mozilla::Version zeroV("0");
459 static mozilla::Version appV(GfxInfoBase::GetApplicationVersion().get());
460 if (appV <= zeroV) {
461 gfxCriticalErrorOnce(gfxCriticalError::DefaultOptions(false))
462 << "Invalid application version "
463 << GfxInfoBase::GetApplicationVersion().get();
466 nsTArray<nsCString> keyValues;
467 ParseString(aBlacklistEntry, '\t', keyValues);
469 aDriverInfo.mRuleId =
470 NS_LITERAL_CSTRING("FEATURE_FAILURE_DL_BLACKLIST_NO_ID");
472 for (uint32_t i = 0; i < keyValues.Length(); ++i) {
473 nsCString keyValue = keyValues[i];
474 nsTArray<nsCString> splitted;
475 ParseString(keyValue, ':', splitted);
476 if (splitted.Length() != 2) {
477 // If we don't recognize the input data, we do not want to proceed.
478 gfxCriticalErrorOnce(CriticalLog::DefaultOptions(false))
479 << "Unrecognized data " << keyValue.get();
480 return false;
482 nsCString key = splitted[0];
483 nsCString value = splitted[1];
484 NS_ConvertUTF8toUTF16 dataValue(value);
486 if (value.Length() == 0) {
487 // Safety check for empty values.
488 gfxCriticalErrorOnce(CriticalLog::DefaultOptions(false))
489 << "Empty value for " << key.get();
490 return false;
493 if (key.EqualsLiteral("blockID")) {
494 nsCString blockIdStr =
495 NS_LITERAL_CSTRING("FEATURE_FAILURE_DL_BLACKLIST_") + value;
496 aDriverInfo.mRuleId = blockIdStr.get();
497 } else if (key.EqualsLiteral("os")) {
498 aDriverInfo.mOperatingSystem = BlacklistOSToOperatingSystem(dataValue);
499 } else if (key.EqualsLiteral("osversion")) {
500 aDriverInfo.mOperatingSystemVersion = strtoul(value.get(), nullptr, 10);
501 } else if (key.EqualsLiteral("vendor")) {
502 aDriverInfo.mAdapterVendor = dataValue;
503 } else if (key.EqualsLiteral("driverVendor")) {
504 aDriverInfo.mDriverVendor = dataValue;
505 } else if (key.EqualsLiteral("feature")) {
506 aDriverInfo.mFeature = BlacklistFeatureToGfxFeature(dataValue);
507 if (aDriverInfo.mFeature < 0) {
508 // If we don't recognize the feature, we do not want to proceed.
509 gfxCriticalErrorOnce(CriticalLog::DefaultOptions(false))
510 << "Unrecognized feature " << value.get();
511 return false;
513 } else if (key.EqualsLiteral("featureStatus")) {
514 aDriverInfo.mFeatureStatus =
515 BlacklistFeatureStatusToGfxFeatureStatus(dataValue);
516 } else if (key.EqualsLiteral("driverVersion")) {
517 uint64_t version;
518 if (ParseDriverVersion(dataValue, &version))
519 aDriverInfo.mDriverVersion = version;
520 } else if (key.EqualsLiteral("driverVersionMax")) {
521 uint64_t version;
522 if (ParseDriverVersion(dataValue, &version))
523 aDriverInfo.mDriverVersionMax = version;
524 } else if (key.EqualsLiteral("driverVersionComparator")) {
525 aDriverInfo.mComparisonOp = BlacklistComparatorToComparisonOp(dataValue);
526 } else if (key.EqualsLiteral("model")) {
527 aDriverInfo.mModel = dataValue;
528 } else if (key.EqualsLiteral("product")) {
529 aDriverInfo.mProduct = dataValue;
530 } else if (key.EqualsLiteral("manufacturer")) {
531 aDriverInfo.mManufacturer = dataValue;
532 } else if (key.EqualsLiteral("hardware")) {
533 aDriverInfo.mHardware = dataValue;
534 } else if (key.EqualsLiteral("versionRange")) {
535 nsTArray<nsCString> versionRange;
536 ParseString(value, ',', versionRange);
537 if (versionRange.Length() != 2) {
538 gfxCriticalErrorOnce(CriticalLog::DefaultOptions(false))
539 << "Unrecognized versionRange " << value.get();
540 return false;
542 nsCString minValue = versionRange[0];
543 nsCString maxValue = versionRange[1];
545 mozilla::Version minV(minValue.get());
546 mozilla::Version maxV(maxValue.get());
548 if (minV > zeroV && !(appV >= minV)) {
549 // The version of the application is less than the minimal version
550 // this blocklist entry applies to, so we can just ignore it by
551 // returning false and letting the caller deal with it.
552 return false;
554 if (maxV > zeroV && !(appV <= maxV)) {
555 // The version of the application is more than the maximal version
556 // this blocklist entry applies to, so we can just ignore it by
557 // returning false and letting the caller deal with it.
558 return false;
560 } else if (key.EqualsLiteral("devices")) {
561 nsTArray<nsCString> devices;
562 ParseString(value, ',', devices);
563 GfxDeviceFamily* deviceIds = BlacklistDevicesToDeviceFamily(devices);
564 if (deviceIds) {
565 // Get GfxDriverInfo to adopt the devices array we created.
566 aDriverInfo.mDeleteDevices = true;
567 aDriverInfo.mDevices = deviceIds;
570 // We explicitly ignore unknown elements.
573 return true;
576 static void BlacklistEntriesToDriverInfo(nsTArray<nsCString>& aBlacklistEntries,
577 nsTArray<GfxDriverInfo>& aDriverInfo) {
578 aDriverInfo.Clear();
579 aDriverInfo.SetLength(aBlacklistEntries.Length());
581 for (uint32_t i = 0; i < aBlacklistEntries.Length(); ++i) {
582 nsCString blacklistEntry = aBlacklistEntries[i];
583 GfxDriverInfo di;
584 if (BlacklistEntryToDriverInfo(blacklistEntry, di)) {
585 aDriverInfo[i] = di;
586 // Prevent di falling out of scope from destroying the devices.
587 di.mDeleteDevices = false;
592 NS_IMETHODIMP
593 GfxInfoBase::Observe(nsISupports* aSubject, const char* aTopic,
594 const char16_t* aData) {
595 if (strcmp(aTopic, "blocklist-data-gfxItems") == 0) {
596 nsTArray<GfxDriverInfo> driverInfo;
597 nsTArray<nsCString> blacklistEntries;
598 nsCString utf8Data = NS_ConvertUTF16toUTF8(aData);
599 if (utf8Data.Length() > 0) {
600 ParseString(utf8Data, '\n', blacklistEntries);
602 BlacklistEntriesToDriverInfo(blacklistEntries, driverInfo);
603 EvaluateDownloadedBlacklist(driverInfo);
606 return NS_OK;
609 GfxInfoBase::GfxInfoBase() : mMutex("GfxInfoBase") {}
611 GfxInfoBase::~GfxInfoBase() {}
613 nsresult GfxInfoBase::Init() {
614 InitGfxDriverInfoShutdownObserver();
616 nsCOMPtr<nsIObserverService> os = mozilla::services::GetObserverService();
617 if (os) {
618 os->AddObserver(this, "blocklist-data-gfxItems", true);
621 return NS_OK;
624 NS_IMETHODIMP
625 GfxInfoBase::GetFeatureStatus(int32_t aFeature, nsACString& aFailureId,
626 int32_t* aStatus) {
627 // Ignore the gfx.blocklist.all pref on release and beta.
628 #if defined(RELEASE_OR_BETA)
629 int32_t blocklistAll = 0;
630 #else
631 int32_t blocklistAll = StaticPrefs::gfx_blocklist_all_AtStartup();
632 #endif
633 if (blocklistAll > 0) {
634 gfxCriticalErrorOnce(gfxCriticalError::DefaultOptions(false))
635 << "Forcing blocklisting all features";
636 *aStatus = FEATURE_BLOCKED_DEVICE;
637 aFailureId = "FEATURE_FAILURE_BLOCK_ALL";
638 return NS_OK;
639 } else if (blocklistAll < 0) {
640 gfxCriticalErrorOnce(gfxCriticalError::DefaultOptions(false))
641 << "Ignoring any feature blocklisting.";
642 *aStatus = FEATURE_STATUS_OK;
643 return NS_OK;
646 if (GetPrefValueForFeature(aFeature, *aStatus, aFailureId)) {
647 return NS_OK;
650 if (XRE_IsContentProcess()) {
651 // Use the cached data received from the parent process.
652 MOZ_ASSERT(sFeatureStatus);
653 bool success = false;
654 for (const auto& fs : *sFeatureStatus) {
655 if (fs.feature() == aFeature) {
656 aFailureId = fs.failureId();
657 *aStatus = fs.status();
658 success = true;
659 break;
662 return success ? NS_OK : NS_ERROR_FAILURE;
665 nsString version;
666 nsTArray<GfxDriverInfo> driverInfo;
667 nsresult rv =
668 GetFeatureStatusImpl(aFeature, aStatus, version, driverInfo, aFailureId);
669 return rv;
672 void GfxInfoBase::GetAllFeatures(dom::XPCOMInitData& xpcomInit) {
673 MOZ_RELEASE_ASSERT(XRE_IsParentProcess());
674 if (!sFeatureStatus) {
675 sFeatureStatus = new nsTArray<dom::GfxInfoFeatureStatus>();
676 for (int32_t i = 1; i <= nsIGfxInfo::FEATURE_MAX_VALUE; ++i) {
677 int32_t status = 0;
678 nsAutoCString failureId;
679 GetFeatureStatus(i, failureId, &status);
680 dom::GfxInfoFeatureStatus gfxFeatureStatus;
681 gfxFeatureStatus.feature() = i;
682 gfxFeatureStatus.status() = status;
683 gfxFeatureStatus.failureId() = failureId;
684 sFeatureStatus->AppendElement(gfxFeatureStatus);
687 for (const auto& status : *sFeatureStatus) {
688 dom::GfxInfoFeatureStatus copy = status;
689 xpcomInit.gfxFeatureStatus().AppendElement(copy);
693 // Matching OS go somewhat beyond the simple equality check because of the
694 // "All Windows" and "All OS X" variations.
696 // aBlockedOS is describing the system(s) we are trying to block.
697 // aSystemOS is describing the system we are running on.
699 // aSystemOS should not be "Windows" or "OSX" - it should be set to
700 // a particular version instead.
701 // However, it is valid for aBlockedOS to be one of those generic values,
702 // as we could be blocking all of the versions.
703 inline bool MatchingOperatingSystems(OperatingSystem aBlockedOS,
704 OperatingSystem aSystemOS) {
705 MOZ_ASSERT(aSystemOS != OperatingSystem::Windows &&
706 aSystemOS != OperatingSystem::OSX);
708 // If the block entry OS is unknown, it doesn't match
709 if (aBlockedOS == OperatingSystem::Unknown) {
710 return false;
713 #if defined(XP_WIN)
714 if (aBlockedOS == OperatingSystem::Windows) {
715 // We do want even "unknown" aSystemOS to fall under "all windows"
716 return true;
718 #endif
720 #if defined(XP_MACOSX)
721 if (aBlockedOS == OperatingSystem::OSX) {
722 // We do want even "unknown" aSystemOS to fall under "all OS X"
723 return true;
725 #endif
727 return aSystemOS == aBlockedOS;
730 int32_t GfxInfoBase::FindBlocklistedDeviceInList(
731 const nsTArray<GfxDriverInfo>& info, nsAString& aSuggestedVersion,
732 int32_t aFeature, nsACString& aFailureId, OperatingSystem os) {
733 int32_t status = nsIGfxInfo::FEATURE_STATUS_UNKNOWN;
735 // Get the adapters once then reuse below
736 nsAutoString adapterVendorID[2];
737 nsAutoString adapterDeviceID[2];
738 nsAutoString adapterDriverVendor[2];
739 nsAutoString adapterDriverVersionString[2];
740 bool adapterInfoFailed[2];
742 adapterInfoFailed[0] =
743 (NS_FAILED(GetAdapterVendorID(adapterVendorID[0])) ||
744 NS_FAILED(GetAdapterDeviceID(adapterDeviceID[0])) ||
745 NS_FAILED(GetAdapterDriverVendor(adapterDriverVendor[0])) ||
746 NS_FAILED(GetAdapterDriverVersion(adapterDriverVersionString[0])));
747 adapterInfoFailed[1] =
748 (NS_FAILED(GetAdapterVendorID2(adapterVendorID[1])) ||
749 NS_FAILED(GetAdapterDeviceID2(adapterDeviceID[1])) ||
750 NS_FAILED(GetAdapterDriverVendor2(adapterDriverVendor[1])) ||
751 NS_FAILED(GetAdapterDriverVersion2(adapterDriverVersionString[1])));
752 // No point in going on if we don't have adapter info
753 if (adapterInfoFailed[0] && adapterInfoFailed[1]) {
754 return 0;
757 #if defined(XP_WIN) || defined(ANDROID) || defined(MOZ_X11)
758 uint64_t driverVersion[2] = {0, 0};
759 if (!adapterInfoFailed[0]) {
760 ParseDriverVersion(adapterDriverVersionString[0], &driverVersion[0]);
762 if (!adapterInfoFailed[1]) {
763 ParseDriverVersion(adapterDriverVersionString[1], &driverVersion[1]);
765 #endif
767 uint32_t i = 0;
768 for (; i < info.Length(); i++) {
769 // If we don't have the info for this GPU, no need to check further.
770 // It is unclear that we would ever have a mixture of 1st and 2nd
771 // GPU, but leaving the code in for that possibility for now.
772 // (Actually, currently mGpu2 will never be true, so this can
773 // be optimized out.)
774 uint32_t infoIndex = info[i].mGpu2 ? 1 : 0;
775 if (adapterInfoFailed[infoIndex]) {
776 continue;
779 // Do the operating system check first, no point in getting the driver
780 // info if we won't need to use it.
781 if (!MatchingOperatingSystems(info[i].mOperatingSystem, os)) {
782 continue;
785 if (info[i].mOperatingSystemVersion &&
786 info[i].mOperatingSystemVersion != OperatingSystemVersion()) {
787 continue;
790 if (!DoesVendorMatch(info[i].mAdapterVendor, adapterVendorID[infoIndex])) {
791 continue;
794 if (!DoesDriverVendorMatch(info[i].mDriverVendor,
795 adapterDriverVendor[infoIndex])) {
796 continue;
799 if (info[i].mDevices != GfxDriverInfo::allDevices &&
800 info[i].mDevices->Length()) {
801 bool deviceMatches = false;
802 for (uint32_t j = 0; j < info[i].mDevices->Length(); j++) {
803 if ((*info[i].mDevices)[j].Equals(
804 adapterDeviceID[infoIndex],
805 nsCaseInsensitiveStringComparator())) {
806 deviceMatches = true;
807 break;
811 if (!deviceMatches) {
812 continue;
816 bool match = false;
818 if (!info[i].mHardware.IsEmpty() && !info[i].mHardware.Equals(Hardware())) {
819 continue;
821 if (!info[i].mModel.IsEmpty() && !info[i].mModel.Equals(Model())) {
822 continue;
824 if (!info[i].mProduct.IsEmpty() && !info[i].mProduct.Equals(Product())) {
825 continue;
827 if (!info[i].mManufacturer.IsEmpty() &&
828 !info[i].mManufacturer.Equals(Manufacturer())) {
829 continue;
832 #if defined(XP_WIN) || defined(ANDROID) || defined(MOZ_X11)
833 switch (info[i].mComparisonOp) {
834 case DRIVER_LESS_THAN:
835 match = driverVersion[infoIndex] < info[i].mDriverVersion;
836 break;
837 case DRIVER_BUILD_ID_LESS_THAN:
838 match = (driverVersion[infoIndex] & 0xFFFF) < info[i].mDriverVersion;
839 break;
840 case DRIVER_LESS_THAN_OR_EQUAL:
841 match = driverVersion[infoIndex] <= info[i].mDriverVersion;
842 break;
843 case DRIVER_BUILD_ID_LESS_THAN_OR_EQUAL:
844 match = (driverVersion[infoIndex] & 0xFFFF) <= info[i].mDriverVersion;
845 break;
846 case DRIVER_GREATER_THAN:
847 match = driverVersion[infoIndex] > info[i].mDriverVersion;
848 break;
849 case DRIVER_GREATER_THAN_OR_EQUAL:
850 match = driverVersion[infoIndex] >= info[i].mDriverVersion;
851 break;
852 case DRIVER_EQUAL:
853 match = driverVersion[infoIndex] == info[i].mDriverVersion;
854 break;
855 case DRIVER_NOT_EQUAL:
856 match = driverVersion[infoIndex] != info[i].mDriverVersion;
857 break;
858 case DRIVER_BETWEEN_EXCLUSIVE:
859 match = driverVersion[infoIndex] > info[i].mDriverVersion &&
860 driverVersion[infoIndex] < info[i].mDriverVersionMax;
861 break;
862 case DRIVER_BETWEEN_INCLUSIVE:
863 match = driverVersion[infoIndex] >= info[i].mDriverVersion &&
864 driverVersion[infoIndex] <= info[i].mDriverVersionMax;
865 break;
866 case DRIVER_BETWEEN_INCLUSIVE_START:
867 match = driverVersion[infoIndex] >= info[i].mDriverVersion &&
868 driverVersion[infoIndex] < info[i].mDriverVersionMax;
869 break;
870 case DRIVER_COMPARISON_IGNORED:
871 // We don't have a comparison op, so we match everything.
872 match = true;
873 break;
874 default:
875 NS_WARNING("Bogus op in GfxDriverInfo");
876 break;
878 #else
879 // We don't care what driver version it was. We only check OS version and if
880 // the device matches.
881 match = true;
882 #endif
884 if (match || info[i].mDriverVersion == GfxDriverInfo::allDriverVersions) {
885 if (info[i].mFeature == GfxDriverInfo::allFeatures ||
886 info[i].mFeature == aFeature) {
887 status = info[i].mFeatureStatus;
888 if (!info[i].mRuleId.IsEmpty()) {
889 aFailureId = info[i].mRuleId.get();
890 } else {
891 aFailureId = "FEATURE_FAILURE_DL_BLACKLIST_NO_ID";
893 break;
898 #if defined(XP_WIN)
899 // As a very special case, we block D2D on machines with an NVidia 310M GPU
900 // as either the primary or secondary adapter. D2D is also blocked when the
901 // NV 310M is the primary adapter (using the standard blocklisting mechanism).
902 // If the primary GPU already matched something in the blocklist then we
903 // ignore this special rule. See bug 1008759.
904 if (status == nsIGfxInfo::FEATURE_STATUS_UNKNOWN &&
905 (aFeature == nsIGfxInfo::FEATURE_DIRECT2D)) {
906 if (!adapterInfoFailed[1]) {
907 nsAString& nvVendorID =
908 (nsAString&)GfxDriverInfo::GetDeviceVendor(VendorNVIDIA);
909 const nsString nv310mDeviceId = NS_LITERAL_STRING("0x0A70");
910 if (nvVendorID.Equals(adapterVendorID[1],
911 nsCaseInsensitiveStringComparator()) &&
912 nv310mDeviceId.Equals(adapterDeviceID[1],
913 nsCaseInsensitiveStringComparator())) {
914 status = nsIGfxInfo::FEATURE_BLOCKED_DEVICE;
915 aFailureId = "FEATURE_FAILURE_D2D_NV310M_BLOCK";
920 // Depends on Windows driver versioning. We don't pass a GfxDriverInfo object
921 // back to the Windows handler, so we must handle this here.
922 if (status == FEATURE_BLOCKED_DRIVER_VERSION) {
923 if (info[i].mSuggestedVersion) {
924 aSuggestedVersion.AppendPrintf("%s", info[i].mSuggestedVersion);
925 } else if (info[i].mComparisonOp == DRIVER_LESS_THAN &&
926 info[i].mDriverVersion != GfxDriverInfo::allDriverVersions) {
927 aSuggestedVersion.AppendPrintf(
928 "%lld.%lld.%lld.%lld",
929 (info[i].mDriverVersion & 0xffff000000000000) >> 48,
930 (info[i].mDriverVersion & 0x0000ffff00000000) >> 32,
931 (info[i].mDriverVersion & 0x00000000ffff0000) >> 16,
932 (info[i].mDriverVersion & 0x000000000000ffff));
935 #endif
937 return status;
940 void GfxInfoBase::SetFeatureStatus(
941 const nsTArray<dom::GfxInfoFeatureStatus>& aFS) {
942 MOZ_ASSERT(!sFeatureStatus);
943 sFeatureStatus = new nsTArray<dom::GfxInfoFeatureStatus>(aFS);
946 bool GfxInfoBase::DoesVendorMatch(const nsAString& aBlocklistVendor,
947 const nsAString& aAdapterVendor) {
948 return aBlocklistVendor.Equals(aAdapterVendor,
949 nsCaseInsensitiveStringComparator()) ||
950 aBlocklistVendor.Equals(GfxDriverInfo::GetDeviceVendor(VendorAll),
951 nsCaseInsensitiveStringComparator());
954 bool GfxInfoBase::DoesDriverVendorMatch(const nsAString& aBlocklistVendor,
955 const nsAString& aDriverVendor) {
956 return aBlocklistVendor.Equals(aDriverVendor,
957 nsCaseInsensitiveStringComparator()) ||
958 aBlocklistVendor.Equals(
959 GfxDriverInfo::GetDriverVendor(DriverVendorAll),
960 nsCaseInsensitiveStringComparator());
963 nsresult GfxInfoBase::GetFeatureStatusImpl(
964 int32_t aFeature, int32_t* aStatus, nsAString& aSuggestedVersion,
965 const nsTArray<GfxDriverInfo>& aDriverInfo, nsACString& aFailureId,
966 OperatingSystem* aOS /* = nullptr */) {
967 if (aFeature <= 0) {
968 gfxWarning() << "Invalid feature <= 0";
969 return NS_OK;
972 if (*aStatus != nsIGfxInfo::FEATURE_STATUS_UNKNOWN) {
973 // Terminate now with the status determined by the derived type (OS-specific
974 // code).
975 return NS_OK;
978 if (sShutdownOccurred) {
979 // This is futile; we've already commenced shutdown and our blocklists have
980 // been deleted. We may want to look into resurrecting the blocklist instead
981 // but for now, just don't even go there.
982 return NS_OK;
985 // If an operating system was provided by the derived GetFeatureStatusImpl,
986 // grab it here. Otherwise, the OS is unknown.
987 OperatingSystem os = (aOS ? *aOS : OperatingSystem::Unknown);
989 nsAutoString adapterVendorID;
990 nsAutoString adapterDeviceID;
991 nsAutoString adapterDriverVersionString;
992 if (NS_FAILED(GetAdapterVendorID(adapterVendorID)) ||
993 NS_FAILED(GetAdapterDeviceID(adapterDeviceID)) ||
994 NS_FAILED(GetAdapterDriverVersion(adapterDriverVersionString))) {
995 aFailureId = "FEATURE_FAILURE_CANT_RESOLVE_ADAPTER";
996 *aStatus = FEATURE_BLOCKED_DEVICE;
997 return NS_OK;
1000 // Check if the device is blocked from the downloaded blocklist. If not, check
1001 // the static list after that. This order is used so that we can later escape
1002 // out of static blocks (i.e. if we were wrong or something was patched, we
1003 // can back out our static block without doing a release).
1004 int32_t status;
1005 if (aDriverInfo.Length()) {
1006 status = FindBlocklistedDeviceInList(aDriverInfo, aSuggestedVersion,
1007 aFeature, aFailureId, os);
1008 } else {
1009 if (!sDriverInfo) {
1010 sDriverInfo = new nsTArray<GfxDriverInfo>();
1012 status = FindBlocklistedDeviceInList(GetGfxDriverInfo(), aSuggestedVersion,
1013 aFeature, aFailureId, os);
1016 // It's now done being processed. It's safe to set the status to STATUS_OK.
1017 if (status == nsIGfxInfo::FEATURE_STATUS_UNKNOWN) {
1018 *aStatus = nsIGfxInfo::FEATURE_STATUS_OK;
1019 } else {
1020 *aStatus = status;
1023 return NS_OK;
1026 NS_IMETHODIMP
1027 GfxInfoBase::GetFeatureSuggestedDriverVersion(int32_t aFeature,
1028 nsAString& aVersion) {
1029 nsCString version;
1030 if (GetPrefValueForDriverVersion(version)) {
1031 aVersion = NS_ConvertASCIItoUTF16(version);
1032 return NS_OK;
1035 int32_t status;
1036 nsCString discardFailureId;
1037 nsTArray<GfxDriverInfo> driverInfo;
1038 return GetFeatureStatusImpl(aFeature, &status, aVersion, driverInfo,
1039 discardFailureId);
1042 void GfxInfoBase::EvaluateDownloadedBlacklist(
1043 nsTArray<GfxDriverInfo>& aDriverInfo) {
1044 int32_t features[] = {nsIGfxInfo::FEATURE_DIRECT2D,
1045 nsIGfxInfo::FEATURE_DIRECT3D_9_LAYERS,
1046 nsIGfxInfo::FEATURE_DIRECT3D_10_LAYERS,
1047 nsIGfxInfo::FEATURE_DIRECT3D_10_1_LAYERS,
1048 nsIGfxInfo::FEATURE_DIRECT3D_11_LAYERS,
1049 nsIGfxInfo::FEATURE_DIRECT3D_11_ANGLE,
1050 nsIGfxInfo::FEATURE_HARDWARE_VIDEO_DECODING,
1051 nsIGfxInfo::FEATURE_OPENGL_LAYERS,
1052 nsIGfxInfo::FEATURE_WEBGL_OPENGL,
1053 nsIGfxInfo::FEATURE_WEBGL_ANGLE,
1054 nsIGfxInfo::FEATURE_WEBRTC_HW_ACCELERATION_ENCODE,
1055 nsIGfxInfo::FEATURE_WEBRTC_HW_ACCELERATION_DECODE,
1056 nsIGfxInfo::FEATURE_WEBGL_MSAA,
1057 nsIGfxInfo::FEATURE_STAGEFRIGHT,
1058 nsIGfxInfo::FEATURE_WEBRTC_HW_ACCELERATION_H264,
1059 nsIGfxInfo::FEATURE_CANVAS2D_ACCELERATION,
1060 nsIGfxInfo::FEATURE_VP8_HW_DECODE,
1061 nsIGfxInfo::FEATURE_VP9_HW_DECODE,
1062 nsIGfxInfo::FEATURE_DX_INTEROP2,
1063 nsIGfxInfo::FEATURE_GPU_PROCESS,
1064 nsIGfxInfo::FEATURE_WEBGL2,
1065 nsIGfxInfo::FEATURE_ADVANCED_LAYERS,
1066 nsIGfxInfo::FEATURE_D3D11_KEYED_MUTEX,
1067 nsIGfxInfo::FEATURE_WEBRENDER,
1068 nsIGfxInfo::FEATURE_WEBRENDER_COMPOSITOR,
1069 nsIGfxInfo::FEATURE_DX_NV12,
1070 nsIGfxInfo::FEATURE_DX_P010,
1071 nsIGfxInfo::FEATURE_DX_P016,
1072 nsIGfxInfo::FEATURE_GL_SWIZZLE,
1075 // For every feature we know about, we evaluate whether this blacklist has a
1076 // non-STATUS_OK status. If it does, we set the pref we evaluate in
1077 // GetFeatureStatus above, so we don't need to hold on to this blacklist
1078 // anywhere permanent.
1079 int i = 0;
1080 while (features[i]) {
1081 int32_t status;
1082 nsCString failureId;
1083 nsAutoString suggestedVersion;
1084 if (NS_SUCCEEDED(GetFeatureStatusImpl(
1085 features[i], &status, suggestedVersion, aDriverInfo, failureId))) {
1086 switch (status) {
1087 default:
1088 case nsIGfxInfo::FEATURE_STATUS_OK:
1089 RemovePrefForFeature(features[i]);
1090 break;
1092 case nsIGfxInfo::FEATURE_BLOCKED_DRIVER_VERSION:
1093 if (!suggestedVersion.IsEmpty()) {
1094 SetPrefValueForDriverVersion(suggestedVersion);
1095 } else {
1096 RemovePrefForDriverVersion();
1098 [[fallthrough]];
1100 case nsIGfxInfo::FEATURE_BLOCKED_MISMATCHED_VERSION:
1101 case nsIGfxInfo::FEATURE_BLOCKED_DEVICE:
1102 case nsIGfxInfo::FEATURE_DISCOURAGED:
1103 case nsIGfxInfo::FEATURE_BLOCKED_OS_VERSION:
1104 SetPrefValueForFeature(features[i], status, failureId);
1105 break;
1109 ++i;
1113 NS_IMETHODIMP_(void)
1114 GfxInfoBase::LogFailure(const nsACString& failure) {
1115 // gfxCriticalError has a mutex lock of its own, so we may not actually
1116 // need this lock. ::GetFailures() accesses the data but the LogForwarder
1117 // will not return the copy of the logs unless it can get the same lock
1118 // that gfxCriticalError uses. Still, that is so much of an implementation
1119 // detail that it's nicer to just add an extra lock here and in
1120 // ::GetFailures()
1121 MutexAutoLock lock(mMutex);
1123 // By default, gfxCriticalError asserts; make it not assert in this case.
1124 gfxCriticalError(CriticalLog::DefaultOptions(false))
1125 << "(LF) " << failure.BeginReading();
1128 NS_IMETHODIMP GfxInfoBase::GetFailures(nsTArray<int32_t>& indices,
1129 nsTArray<nsCString>& failures) {
1130 MutexAutoLock lock(mMutex);
1132 LogForwarder* logForwarder = Factory::GetLogForwarder();
1133 if (!logForwarder) {
1134 return NS_ERROR_UNEXPECTED;
1137 // There are two string copies in this method, starting with this one. We are
1138 // assuming this is not a big deal, as the size of the array should be small
1139 // and the strings in it should be small as well (the error messages in the
1140 // code.) The second copy happens with the AppendElement() calls.
1141 // Technically, we don't need the mutex lock after the StringVectorCopy()
1142 // call.
1143 LoggingRecord loggedStrings = logForwarder->LoggingRecordCopy();
1144 LoggingRecord::const_iterator it;
1145 for (it = loggedStrings.begin(); it != loggedStrings.end(); ++it) {
1146 failures.AppendElement(
1147 nsDependentCSubstring(Get<1>(*it).c_str(), Get<1>(*it).size()));
1148 indices.AppendElement(Get<0>(*it));
1151 return NS_OK;
1154 nsTArray<GfxInfoCollectorBase*>* sCollectors;
1156 static void InitCollectors() {
1157 if (!sCollectors) sCollectors = new nsTArray<GfxInfoCollectorBase*>;
1160 nsresult GfxInfoBase::GetInfo(JSContext* aCx,
1161 JS::MutableHandle<JS::Value> aResult) {
1162 InitCollectors();
1163 InfoObject obj(aCx);
1165 for (uint32_t i = 0; i < sCollectors->Length(); i++) {
1166 (*sCollectors)[i]->GetInfo(obj);
1169 // Some example property definitions
1170 // obj.DefineProperty("wordCacheSize", gfxTextRunWordCache::Count());
1171 // obj.DefineProperty("renderer", mRendererIDsString);
1172 // obj.DefineProperty("five", 5);
1174 if (!obj.mOk) {
1175 return NS_ERROR_FAILURE;
1178 aResult.setObject(*obj.mObj);
1179 return NS_OK;
1182 nsAutoCString gBaseAppVersion;
1184 const nsCString& GfxInfoBase::GetApplicationVersion() {
1185 static bool versionInitialized = false;
1186 if (!versionInitialized) {
1187 // If we fail to get the version, we will not try again.
1188 versionInitialized = true;
1190 // Get the version from xpcom/system/nsIXULAppInfo.idl
1191 nsCOMPtr<nsIXULAppInfo> app = do_GetService("@mozilla.org/xre/app-info;1");
1192 if (app) {
1193 app->GetVersion(gBaseAppVersion);
1196 return gBaseAppVersion;
1199 void GfxInfoBase::AddCollector(GfxInfoCollectorBase* collector) {
1200 InitCollectors();
1201 sCollectors->AppendElement(collector);
1204 void GfxInfoBase::RemoveCollector(GfxInfoCollectorBase* collector) {
1205 InitCollectors();
1206 for (uint32_t i = 0; i < sCollectors->Length(); i++) {
1207 if ((*sCollectors)[i] == collector) {
1208 sCollectors->RemoveElementAt(i);
1209 break;
1212 if (sCollectors->IsEmpty()) {
1213 delete sCollectors;
1214 sCollectors = nullptr;
1218 nsresult GfxInfoBase::FindMonitors(JSContext* aCx, JS::HandleObject aOutArray) {
1219 // If we have no platform specific implementation for detecting monitors, we
1220 // can just get the screen size from gfxPlatform as the best guess.
1221 if (!gfxPlatform::Initialized()) {
1222 return NS_OK;
1225 // If the screen size is empty, we are probably in xpcshell.
1226 gfx::IntSize screenSize = gfxPlatform::GetPlatform()->GetScreenSize();
1228 JS::Rooted<JSObject*> obj(aCx, JS_NewPlainObject(aCx));
1230 JS::Rooted<JS::Value> screenWidth(aCx, JS::Int32Value(screenSize.width));
1231 JS_SetProperty(aCx, obj, "screenWidth", screenWidth);
1233 JS::Rooted<JS::Value> screenHeight(aCx, JS::Int32Value(screenSize.height));
1234 JS_SetProperty(aCx, obj, "screenHeight", screenHeight);
1236 JS::Rooted<JS::Value> element(aCx, JS::ObjectValue(*obj));
1237 JS_SetElement(aCx, aOutArray, 0, element);
1239 return NS_OK;
1242 NS_IMETHODIMP
1243 GfxInfoBase::GetMonitors(JSContext* aCx, JS::MutableHandleValue aResult) {
1244 JS::Rooted<JSObject*> array(aCx, JS::NewArrayObject(aCx, 0));
1246 nsresult rv = FindMonitors(aCx, array);
1247 if (NS_FAILED(rv)) {
1248 return rv;
1251 aResult.setObject(*array);
1252 return NS_OK;
1255 static inline bool SetJSPropertyString(JSContext* aCx,
1256 JS::Handle<JSObject*> aObj,
1257 const char* aProp, const char* aString) {
1258 JS::Rooted<JSString*> str(aCx, JS_NewStringCopyZ(aCx, aString));
1259 if (!str) {
1260 return false;
1263 JS::Rooted<JS::Value> val(aCx, JS::StringValue(str));
1264 return JS_SetProperty(aCx, aObj, aProp, val);
1267 template <typename T>
1268 static inline bool AppendJSElement(JSContext* aCx, JS::Handle<JSObject*> aObj,
1269 const T& aValue) {
1270 uint32_t index;
1271 if (!JS::GetArrayLength(aCx, aObj, &index)) {
1272 return false;
1274 return JS_SetElement(aCx, aObj, index, aValue);
1277 nsresult GfxInfoBase::GetFeatures(JSContext* aCx,
1278 JS::MutableHandle<JS::Value> aOut) {
1279 JS::Rooted<JSObject*> obj(aCx, JS_NewPlainObject(aCx));
1280 if (!obj) {
1281 return NS_ERROR_OUT_OF_MEMORY;
1283 aOut.setObject(*obj);
1285 layers::LayersBackend backend =
1286 gfxPlatform::Initialized()
1287 ? gfxPlatform::GetPlatform()->GetCompositorBackend()
1288 : layers::LayersBackend::LAYERS_NONE;
1289 const char* backendName = layers::GetLayersBackendName(backend);
1290 SetJSPropertyString(aCx, obj, "compositor", backendName);
1292 // If graphics isn't initialized yet, just stop now.
1293 if (!gfxPlatform::Initialized()) {
1294 return NS_OK;
1297 DescribeFeatures(aCx, obj);
1298 return NS_OK;
1301 nsresult GfxInfoBase::GetFeatureLog(JSContext* aCx,
1302 JS::MutableHandle<JS::Value> aOut) {
1303 JS::Rooted<JSObject*> containerObj(aCx, JS_NewPlainObject(aCx));
1304 if (!containerObj) {
1305 return NS_ERROR_OUT_OF_MEMORY;
1307 aOut.setObject(*containerObj);
1309 JS::Rooted<JSObject*> featureArray(aCx, JS::NewArrayObject(aCx, 0));
1310 if (!featureArray) {
1311 return NS_ERROR_OUT_OF_MEMORY;
1314 // Collect features.
1315 gfxConfig::ForEachFeature([&](const char* aName, const char* aDescription,
1316 FeatureState& aFeature) -> void {
1317 JS::Rooted<JSObject*> obj(aCx, JS_NewPlainObject(aCx));
1318 if (!obj) {
1319 return;
1321 if (!SetJSPropertyString(aCx, obj, "name", aName) ||
1322 !SetJSPropertyString(aCx, obj, "description", aDescription) ||
1323 !SetJSPropertyString(aCx, obj, "status",
1324 FeatureStatusToString(aFeature.GetValue()))) {
1325 return;
1328 JS::Rooted<JS::Value> log(aCx);
1329 if (!BuildFeatureStateLog(aCx, aFeature, &log)) {
1330 return;
1332 if (!JS_SetProperty(aCx, obj, "log", log)) {
1333 return;
1336 if (!AppendJSElement(aCx, featureArray, obj)) {
1337 return;
1341 JS::Rooted<JSObject*> fallbackArray(aCx, JS::NewArrayObject(aCx, 0));
1342 if (!fallbackArray) {
1343 return NS_ERROR_OUT_OF_MEMORY;
1346 // Collect fallbacks.
1347 gfxConfig::ForEachFallback(
1348 [&](const char* aName, const char* aMessage) -> void {
1349 JS::Rooted<JSObject*> obj(aCx, JS_NewPlainObject(aCx));
1350 if (!obj) {
1351 return;
1354 if (!SetJSPropertyString(aCx, obj, "name", aName) ||
1355 !SetJSPropertyString(aCx, obj, "message", aMessage)) {
1356 return;
1359 if (!AppendJSElement(aCx, fallbackArray, obj)) {
1360 return;
1364 JS::Rooted<JS::Value> val(aCx);
1366 val = JS::ObjectValue(*featureArray);
1367 JS_SetProperty(aCx, containerObj, "features", val);
1369 val = JS::ObjectValue(*fallbackArray);
1370 JS_SetProperty(aCx, containerObj, "fallbacks", val);
1372 return NS_OK;
1375 bool GfxInfoBase::BuildFeatureStateLog(JSContext* aCx,
1376 const FeatureState& aFeature,
1377 JS::MutableHandle<JS::Value> aOut) {
1378 JS::Rooted<JSObject*> log(aCx, JS::NewArrayObject(aCx, 0));
1379 if (!log) {
1380 return false;
1382 aOut.setObject(*log);
1384 aFeature.ForEachStatusChange([&](const char* aType, FeatureStatus aStatus,
1385 const char* aMessage) -> void {
1386 JS::Rooted<JSObject*> obj(aCx, JS_NewPlainObject(aCx));
1387 if (!obj) {
1388 return;
1391 if (!SetJSPropertyString(aCx, obj, "type", aType) ||
1392 !SetJSPropertyString(aCx, obj, "status",
1393 FeatureStatusToString(aStatus)) ||
1394 (aMessage && !SetJSPropertyString(aCx, obj, "message", aMessage))) {
1395 return;
1398 if (!AppendJSElement(aCx, log, obj)) {
1399 return;
1403 return true;
1406 void GfxInfoBase::DescribeFeatures(JSContext* aCx, JS::Handle<JSObject*> aObj) {
1407 JS::Rooted<JSObject*> obj(aCx);
1409 gfx::FeatureStatus gpuProcess =
1410 gfxConfig::GetValue(gfx::Feature::GPU_PROCESS);
1411 InitFeatureObject(aCx, aObj, "gpuProcess", gpuProcess, &obj);
1413 gfx::FeatureStatus wrQualified =
1414 gfxConfig::GetValue(gfx::Feature::WEBRENDER_QUALIFIED);
1415 InitFeatureObject(aCx, aObj, "wrQualified", wrQualified, &obj);
1417 gfx::FeatureStatus webrender = gfxConfig::GetValue(gfx::Feature::WEBRENDER);
1418 InitFeatureObject(aCx, aObj, "webrender", webrender, &obj);
1420 // Only include AL if the platform attempted to use it.
1421 gfx::FeatureStatus advancedLayers =
1422 gfxConfig::GetValue(gfx::Feature::ADVANCED_LAYERS);
1423 if (advancedLayers != FeatureStatus::Unused) {
1424 InitFeatureObject(aCx, aObj, "advancedLayers", advancedLayers, &obj);
1426 if (gfxConfig::UseFallback(Fallback::NO_CONSTANT_BUFFER_OFFSETTING)) {
1427 JS::Rooted<JS::Value> trueVal(aCx, JS::BooleanValue(true));
1428 JS_SetProperty(aCx, obj, "noConstantBufferOffsetting", trueVal);
1433 bool GfxInfoBase::InitFeatureObject(JSContext* aCx,
1434 JS::Handle<JSObject*> aContainer,
1435 const char* aName,
1436 mozilla::gfx::FeatureStatus& aFeatureStatus,
1437 JS::MutableHandle<JSObject*> aOutObj) {
1438 JS::Rooted<JSObject*> obj(aCx, JS_NewPlainObject(aCx));
1439 if (!obj) {
1440 return false;
1443 // Set "status".
1444 const char* status = FeatureStatusToString(aFeatureStatus);
1446 JS::Rooted<JSString*> str(aCx, JS_NewStringCopyZ(aCx, status));
1447 JS::Rooted<JS::Value> val(aCx, JS::StringValue(str));
1448 JS_SetProperty(aCx, obj, "status", val);
1450 // Add the feature object to the container.
1452 JS::Rooted<JS::Value> val(aCx, JS::ObjectValue(*obj));
1453 JS_SetProperty(aCx, aContainer, aName, val);
1456 aOutObj.set(obj);
1457 return true;
1460 nsresult GfxInfoBase::GetActiveCrashGuards(JSContext* aCx,
1461 JS::MutableHandle<JS::Value> aOut) {
1462 JS::Rooted<JSObject*> array(aCx, JS::NewArrayObject(aCx, 0));
1463 if (!array) {
1464 return NS_ERROR_OUT_OF_MEMORY;
1466 aOut.setObject(*array);
1468 DriverCrashGuard::ForEachActiveCrashGuard(
1469 [&](const char* aName, const char* aPrefName) -> void {
1470 JS::Rooted<JSObject*> obj(aCx, JS_NewPlainObject(aCx));
1471 if (!obj) {
1472 return;
1474 if (!SetJSPropertyString(aCx, obj, "type", aName)) {
1475 return;
1477 if (!SetJSPropertyString(aCx, obj, "prefName", aPrefName)) {
1478 return;
1480 if (!AppendJSElement(aCx, array, obj)) {
1481 return;
1485 return NS_OK;
1488 NS_IMETHODIMP
1489 GfxInfoBase::GetWebRenderEnabled(bool* aWebRenderEnabled) {
1490 *aWebRenderEnabled = gfxVars::UseWebRender();
1491 return NS_OK;
1494 NS_IMETHODIMP
1495 GfxInfoBase::GetUsesTiling(bool* aUsesTiling) {
1496 *aUsesTiling = gfxPlatform::GetPlatform()->UsesTiling();
1497 return NS_OK;
1500 NS_IMETHODIMP
1501 GfxInfoBase::GetContentUsesTiling(bool* aUsesTiling) {
1502 *aUsesTiling = gfxPlatform::GetPlatform()->ContentUsesTiling();
1503 return NS_OK;
1506 NS_IMETHODIMP
1507 GfxInfoBase::GetOffMainThreadPaintEnabled(bool* aOffMainThreadPaintEnabled) {
1508 *aOffMainThreadPaintEnabled = gfxConfig::IsEnabled(gfx::Feature::OMTP);
1509 return NS_OK;
1512 NS_IMETHODIMP
1513 GfxInfoBase::GetOffMainThreadPaintWorkerCount(
1514 int32_t* aOffMainThreadPaintWorkerCount) {
1515 if (gfxConfig::IsEnabled(gfx::Feature::OMTP)) {
1516 *aOffMainThreadPaintWorkerCount =
1517 layers::PaintThread::CalculatePaintWorkerCount();
1518 } else {
1519 *aOffMainThreadPaintWorkerCount = 0;
1521 return NS_OK;
1524 NS_IMETHODIMP
1525 GfxInfoBase::GetTargetFrameRate(uint32_t* aTargetFrameRate) {
1526 *aTargetFrameRate = gfxPlatform::TargetFrameRate();
1527 return NS_OK;
1530 NS_IMETHODIMP
1531 GfxInfoBase::GetIsHeadless(bool* aIsHeadless) {
1532 *aIsHeadless = gfxPlatform::IsHeadless();
1533 return NS_OK;
1536 NS_IMETHODIMP
1537 GfxInfoBase::GetContentBackend(nsAString& aContentBackend) {
1538 BackendType backend = gfxPlatform::GetPlatform()->GetDefaultContentBackend();
1539 nsString outStr;
1541 switch (backend) {
1542 case BackendType::DIRECT2D1_1: {
1543 outStr.AppendPrintf("Direct2D 1.1");
1544 break;
1546 case BackendType::SKIA: {
1547 outStr.AppendPrintf("Skia");
1548 break;
1550 case BackendType::CAIRO: {
1551 outStr.AppendPrintf("Cairo");
1552 break;
1554 default:
1555 return NS_ERROR_FAILURE;
1558 aContentBackend.Assign(outStr);
1559 return NS_OK;
1562 NS_IMETHODIMP
1563 GfxInfoBase::GetUsingGPUProcess(bool* aOutValue) {
1564 GPUProcessManager* gpu = GPUProcessManager::Get();
1565 if (!gpu) {
1566 // Not supported in content processes.
1567 return NS_ERROR_FAILURE;
1570 *aOutValue = !!gpu->GetGPUChild();
1571 return NS_OK;
1574 NS_IMETHODIMP
1575 GfxInfoBase::ControlGPUProcessForXPCShell(bool aEnable, bool* _retval) {
1576 gfxPlatform::GetPlatform();
1578 GPUProcessManager* gpm = GPUProcessManager::Get();
1579 if (aEnable) {
1580 if (!gfxConfig::IsEnabled(gfx::Feature::GPU_PROCESS)) {
1581 gfxConfig::UserForceEnable(gfx::Feature::GPU_PROCESS, "xpcshell-test");
1583 gpm->LaunchGPUProcess();
1584 gpm->EnsureGPUReady();
1585 } else {
1586 gfxConfig::UserDisable(gfx::Feature::GPU_PROCESS, "xpcshell-test");
1587 gpm->KillProcess();
1590 *_retval = true;
1591 return NS_OK;
1594 GfxInfoCollectorBase::GfxInfoCollectorBase() {
1595 GfxInfoBase::AddCollector(this);
1598 GfxInfoCollectorBase::~GfxInfoCollectorBase() {
1599 GfxInfoBase::RemoveCollector(this);