Bug 1253840 - Remove .ini file as it's no longer necessary by passing on all platform...
[gecko.git] / widget / GfxInfoBase.cpp
blob66dfcaabdd6a0768d6cdea69fcfcc90926b40f72
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 <mutex> // std::call_once
14 #include "GfxDriverInfo.h"
15 #include "js/Array.h" // JS::GetArrayLength, JS::NewArrayObject
16 #include "js/PropertyAndElement.h" // JS_SetElement, JS_SetProperty
17 #include "nsCOMPtr.h"
18 #include "nsCOMArray.h"
19 #include "nsString.h"
20 #include "nsUnicharUtils.h"
21 #include "nsVersionComparator.h"
22 #include "mozilla/Services.h"
23 #include "mozilla/Observer.h"
24 #include "nsIObserver.h"
25 #include "nsIObserverService.h"
26 #include "nsTArray.h"
27 #include "nsXULAppAPI.h"
28 #include "nsIXULAppInfo.h"
29 #include "mozilla/ClearOnShutdown.h"
30 #include "mozilla/Preferences.h"
31 #include "mozilla/StaticPrefs_gfx.h"
32 #include "mozilla/gfx/2D.h"
33 #include "mozilla/gfx/GPUProcessManager.h"
34 #include "mozilla/gfx/Logging.h"
35 #include "mozilla/gfx/gfxVars.h"
36 #include "mozilla/widget/ScreenManager.h"
37 #include "mozilla/widget/Screen.h"
39 #include "jsapi.h"
41 #include "gfxPlatform.h"
42 #include "gfxConfig.h"
43 #include "DriverCrashGuard.h"
45 using namespace mozilla::widget;
46 using namespace mozilla;
47 using mozilla::MutexAutoLock;
49 nsTArray<GfxDriverInfo>* GfxInfoBase::sDriverInfo;
50 StaticAutoPtr<nsTArray<gfx::GfxInfoFeatureStatus>> GfxInfoBase::sFeatureStatus;
51 bool GfxInfoBase::sDriverInfoObserverInitialized;
52 bool GfxInfoBase::sShutdownOccurred;
54 // Call this when setting sFeatureStatus to a non-null pointer to
55 // ensure destruction even if the GfxInfo component is never instantiated.
56 static void InitFeatureStatus(nsTArray<gfx::GfxInfoFeatureStatus>* aPtr) {
57 static std::once_flag sOnce;
58 std::call_once(sOnce, [] { ClearOnShutdown(&GfxInfoBase::sFeatureStatus); });
59 GfxInfoBase::sFeatureStatus = aPtr;
62 // Observes for shutdown so that the child GfxDriverInfo list is freed.
63 class ShutdownObserver : public nsIObserver {
64 virtual ~ShutdownObserver() = default;
66 public:
67 ShutdownObserver() = default;
69 NS_DECL_ISUPPORTS
71 NS_IMETHOD Observe(nsISupports* subject, const char* aTopic,
72 const char16_t* aData) override {
73 MOZ_ASSERT(strcmp(aTopic, NS_XPCOM_SHUTDOWN_OBSERVER_ID) == 0);
75 delete GfxInfoBase::sDriverInfo;
76 GfxInfoBase::sDriverInfo = nullptr;
78 for (auto& deviceFamily : GfxDriverInfo::sDeviceFamilies) {
79 delete deviceFamily;
80 deviceFamily = nullptr;
83 for (auto& windowProtocol : GfxDriverInfo::sWindowProtocol) {
84 delete windowProtocol;
85 windowProtocol = nullptr;
88 for (auto& deviceVendor : GfxDriverInfo::sDeviceVendors) {
89 delete deviceVendor;
90 deviceVendor = nullptr;
93 for (auto& driverVendor : GfxDriverInfo::sDriverVendors) {
94 delete driverVendor;
95 driverVendor = nullptr;
98 GfxInfoBase::sShutdownOccurred = true;
100 return NS_OK;
104 NS_IMPL_ISUPPORTS(ShutdownObserver, nsIObserver)
106 static void InitGfxDriverInfoShutdownObserver() {
107 if (GfxInfoBase::sDriverInfoObserverInitialized) return;
109 GfxInfoBase::sDriverInfoObserverInitialized = true;
111 nsCOMPtr<nsIObserverService> observerService = services::GetObserverService();
112 if (!observerService) {
113 NS_WARNING("Could not get observer service!");
114 return;
117 ShutdownObserver* obs = new ShutdownObserver();
118 observerService->AddObserver(obs, NS_XPCOM_SHUTDOWN_OBSERVER_ID, false);
121 using namespace mozilla::widget;
122 using namespace mozilla::gfx;
123 using namespace mozilla;
125 NS_IMPL_ISUPPORTS(GfxInfoBase, nsIGfxInfo, nsIObserver,
126 nsISupportsWeakReference)
128 #define BLOCKLIST_PREF_BRANCH "gfx.blacklist."
129 #define SUGGESTED_VERSION_PREF BLOCKLIST_PREF_BRANCH "suggested-driver-version"
131 static const char* GetPrefNameForFeature(int32_t aFeature) {
132 const char* name = nullptr;
133 switch (aFeature) {
134 case nsIGfxInfo::FEATURE_DIRECT2D:
135 name = BLOCKLIST_PREF_BRANCH "direct2d";
136 break;
137 case nsIGfxInfo::FEATURE_DIRECT3D_9_LAYERS:
138 name = BLOCKLIST_PREF_BRANCH "layers.direct3d9";
139 break;
140 case nsIGfxInfo::FEATURE_DIRECT3D_10_LAYERS:
141 name = BLOCKLIST_PREF_BRANCH "layers.direct3d10";
142 break;
143 case nsIGfxInfo::FEATURE_DIRECT3D_10_1_LAYERS:
144 name = BLOCKLIST_PREF_BRANCH "layers.direct3d10-1";
145 break;
146 case nsIGfxInfo::FEATURE_DIRECT3D_11_LAYERS:
147 name = BLOCKLIST_PREF_BRANCH "layers.direct3d11";
148 break;
149 case nsIGfxInfo::FEATURE_DIRECT3D_11_ANGLE:
150 name = BLOCKLIST_PREF_BRANCH "direct3d11angle";
151 break;
152 case nsIGfxInfo::FEATURE_HARDWARE_VIDEO_DECODING:
153 name = BLOCKLIST_PREF_BRANCH "hardwarevideodecoding";
154 break;
155 case nsIGfxInfo::FEATURE_OPENGL_LAYERS:
156 name = BLOCKLIST_PREF_BRANCH "layers.opengl";
157 break;
158 case nsIGfxInfo::FEATURE_WEBGL_OPENGL:
159 name = BLOCKLIST_PREF_BRANCH "webgl.opengl";
160 break;
161 case nsIGfxInfo::FEATURE_WEBGL_ANGLE:
162 name = BLOCKLIST_PREF_BRANCH "webgl.angle";
163 break;
164 case nsIGfxInfo::UNUSED_FEATURE_WEBGL_MSAA:
165 name = BLOCKLIST_PREF_BRANCH "webgl.msaa";
166 break;
167 case nsIGfxInfo::FEATURE_STAGEFRIGHT:
168 name = BLOCKLIST_PREF_BRANCH "stagefright";
169 break;
170 case nsIGfxInfo::FEATURE_WEBRTC_HW_ACCELERATION_H264:
171 name = BLOCKLIST_PREF_BRANCH "webrtc.hw.acceleration.h264";
172 break;
173 case nsIGfxInfo::FEATURE_WEBRTC_HW_ACCELERATION_ENCODE:
174 name = BLOCKLIST_PREF_BRANCH "webrtc.hw.acceleration.encode";
175 break;
176 case nsIGfxInfo::FEATURE_WEBRTC_HW_ACCELERATION_DECODE:
177 name = BLOCKLIST_PREF_BRANCH "webrtc.hw.acceleration.decode";
178 break;
179 case nsIGfxInfo::FEATURE_CANVAS2D_ACCELERATION:
180 name = BLOCKLIST_PREF_BRANCH "canvas2d.acceleration";
181 break;
182 case nsIGfxInfo::FEATURE_DX_INTEROP2:
183 name = BLOCKLIST_PREF_BRANCH "dx.interop2";
184 break;
185 case nsIGfxInfo::FEATURE_GPU_PROCESS:
186 name = BLOCKLIST_PREF_BRANCH "gpu.process";
187 break;
188 case nsIGfxInfo::FEATURE_WEBGL2:
189 name = BLOCKLIST_PREF_BRANCH "webgl2";
190 break;
191 case nsIGfxInfo::FEATURE_D3D11_KEYED_MUTEX:
192 name = BLOCKLIST_PREF_BRANCH "d3d11.keyed.mutex";
193 break;
194 case nsIGfxInfo::FEATURE_WEBRENDER:
195 name = BLOCKLIST_PREF_BRANCH "webrender";
196 break;
197 case nsIGfxInfo::FEATURE_WEBRENDER_COMPOSITOR:
198 name = BLOCKLIST_PREF_BRANCH "webrender.compositor";
199 break;
200 case nsIGfxInfo::FEATURE_DX_NV12:
201 name = BLOCKLIST_PREF_BRANCH "dx.nv12";
202 break;
203 case nsIGfxInfo::FEATURE_DX_P010:
204 name = BLOCKLIST_PREF_BRANCH "dx.p010";
205 break;
206 case nsIGfxInfo::FEATURE_DX_P016:
207 name = BLOCKLIST_PREF_BRANCH "dx.p016";
208 break;
209 case nsIGfxInfo::FEATURE_VP8_HW_DECODE:
210 name = BLOCKLIST_PREF_BRANCH "vp8.hw-decode";
211 break;
212 case nsIGfxInfo::FEATURE_VP9_HW_DECODE:
213 name = BLOCKLIST_PREF_BRANCH "vp9.hw-decode";
214 break;
215 case nsIGfxInfo::FEATURE_GL_SWIZZLE:
216 name = BLOCKLIST_PREF_BRANCH "gl.swizzle";
217 break;
218 case nsIGfxInfo::FEATURE_WEBRENDER_SCISSORED_CACHE_CLEARS:
219 name = BLOCKLIST_PREF_BRANCH "webrender.scissored_cache_clears";
220 break;
221 case nsIGfxInfo::FEATURE_ALLOW_WEBGL_OUT_OF_PROCESS:
222 name = BLOCKLIST_PREF_BRANCH "webgl.allow-oop";
223 break;
224 case nsIGfxInfo::FEATURE_THREADSAFE_GL:
225 name = BLOCKLIST_PREF_BRANCH "gl.threadsafe";
226 break;
227 case nsIGfxInfo::FEATURE_WEBRENDER_OPTIMIZED_SHADERS:
228 name = BLOCKLIST_PREF_BRANCH "webrender.optimized-shaders";
229 break;
230 case nsIGfxInfo::FEATURE_X11_EGL:
231 name = BLOCKLIST_PREF_BRANCH "x11.egl";
232 break;
233 case nsIGfxInfo::FEATURE_DMABUF:
234 name = BLOCKLIST_PREF_BRANCH "dmabuf";
235 break;
236 case nsIGfxInfo::FEATURE_WEBGPU:
237 name = BLOCKLIST_PREF_BRANCH "webgpu";
238 break;
239 case nsIGfxInfo::FEATURE_VIDEO_OVERLAY:
240 name = BLOCKLIST_PREF_BRANCH "video-overlay";
241 break;
242 case nsIGfxInfo::FEATURE_HW_DECODED_VIDEO_ZERO_COPY:
243 name = BLOCKLIST_PREF_BRANCH "hw-video-zero-copy";
244 break;
245 case nsIGfxInfo::FEATURE_WEBRENDER_SHADER_CACHE:
246 name = BLOCKLIST_PREF_BRANCH "webrender.program-binary-disk";
247 break;
248 case nsIGfxInfo::FEATURE_WEBRENDER_PARTIAL_PRESENT:
249 name = BLOCKLIST_PREF_BRANCH "webrender.partial-present";
250 break;
251 case nsIGfxInfo::FEATURE_DMABUF_SURFACE_EXPORT:
252 name = BLOCKLIST_PREF_BRANCH "dmabuf.surface-export";
253 break;
254 case nsIGfxInfo::FEATURE_REUSE_DECODER_DEVICE:
255 name = BLOCKLIST_PREF_BRANCH "reuse-decoder-device";
256 break;
257 case nsIGfxInfo::FEATURE_BACKDROP_FILTER:
258 name = BLOCKLIST_PREF_BRANCH "backdrop.filter";
259 break;
260 case nsIGfxInfo::FEATURE_ACCELERATED_CANVAS2D:
261 name = BLOCKLIST_PREF_BRANCH "accelerated-canvas2d";
262 break;
263 case nsIGfxInfo::FEATURE_H264_HW_DECODE:
264 name = BLOCKLIST_PREF_BRANCH "h264.hw-decode";
265 break;
266 case nsIGfxInfo::FEATURE_AV1_HW_DECODE:
267 name = BLOCKLIST_PREF_BRANCH "av1.hw-decode";
268 break;
269 default:
270 MOZ_ASSERT_UNREACHABLE("Unexpected nsIGfxInfo feature?!");
271 break;
274 return name;
277 // Returns the value of the pref for the relevant feature in aValue.
278 // If the pref doesn't exist, aValue is not touched, and returns false.
279 static bool GetPrefValueForFeature(int32_t aFeature, int32_t& aValue,
280 nsACString& aFailureId) {
281 const char* prefname = GetPrefNameForFeature(aFeature);
282 if (!prefname) return false;
284 aValue = nsIGfxInfo::FEATURE_STATUS_UNKNOWN;
285 if (!NS_SUCCEEDED(Preferences::GetInt(prefname, &aValue))) {
286 return false;
289 if (aValue == nsIGfxInfo::FEATURE_DENIED) {
290 // We should never see the DENIED status with the downloadable blocklist.
291 return false;
294 nsCString failureprefname(prefname);
295 failureprefname += ".failureid";
296 nsAutoCString failureValue;
297 nsresult rv = Preferences::GetCString(failureprefname.get(), failureValue);
298 if (NS_SUCCEEDED(rv)) {
299 aFailureId = failureValue.get();
300 } else {
301 aFailureId = "FEATURE_FAILURE_BLOCKLIST_PREF";
304 return true;
307 static void SetPrefValueForFeature(int32_t aFeature, int32_t aValue,
308 const nsACString& aFailureId) {
309 const char* prefname = GetPrefNameForFeature(aFeature);
310 if (!prefname) return;
311 if (XRE_IsParentProcess()) {
312 GfxInfoBase::sFeatureStatus = nullptr;
315 Preferences::SetInt(prefname, aValue);
316 if (!aFailureId.IsEmpty()) {
317 nsAutoCString failureprefname(prefname);
318 failureprefname += ".failureid";
319 Preferences::SetCString(failureprefname.get(), aFailureId);
323 static void RemovePrefForFeature(int32_t aFeature) {
324 const char* prefname = GetPrefNameForFeature(aFeature);
325 if (!prefname) return;
327 if (XRE_IsParentProcess()) {
328 GfxInfoBase::sFeatureStatus = nullptr;
330 Preferences::ClearUser(prefname);
333 static bool GetPrefValueForDriverVersion(nsCString& aVersion) {
334 return NS_SUCCEEDED(
335 Preferences::GetCString(SUGGESTED_VERSION_PREF, aVersion));
338 static void SetPrefValueForDriverVersion(const nsAString& aVersion) {
339 Preferences::SetString(SUGGESTED_VERSION_PREF, aVersion);
342 static void RemovePrefForDriverVersion() {
343 Preferences::ClearUser(SUGGESTED_VERSION_PREF);
346 static OperatingSystem BlocklistOSToOperatingSystem(const nsAString& os) {
347 if (os.EqualsLiteral("WINNT 6.1")) {
348 return OperatingSystem::Windows7;
350 if (os.EqualsLiteral("WINNT 6.2")) {
351 return OperatingSystem::Windows8;
353 if (os.EqualsLiteral("WINNT 6.3")) {
354 return OperatingSystem::Windows8_1;
356 if (os.EqualsLiteral("WINNT 10.0")) {
357 return OperatingSystem::Windows10;
359 if (os.EqualsLiteral("Linux")) {
360 return OperatingSystem::Linux;
362 if (os.EqualsLiteral("Darwin 9")) {
363 return OperatingSystem::OSX10_5;
365 if (os.EqualsLiteral("Darwin 10")) {
366 return OperatingSystem::OSX10_6;
368 if (os.EqualsLiteral("Darwin 11")) {
369 return OperatingSystem::OSX10_7;
371 if (os.EqualsLiteral("Darwin 12")) {
372 return OperatingSystem::OSX10_8;
374 if (os.EqualsLiteral("Darwin 13")) {
375 return OperatingSystem::OSX10_9;
377 if (os.EqualsLiteral("Darwin 14")) {
378 return OperatingSystem::OSX10_10;
380 if (os.EqualsLiteral("Darwin 15")) {
381 return OperatingSystem::OSX10_11;
383 if (os.EqualsLiteral("Darwin 16")) {
384 return OperatingSystem::OSX10_12;
386 if (os.EqualsLiteral("Darwin 17")) {
387 return OperatingSystem::OSX10_13;
389 if (os.EqualsLiteral("Darwin 18")) {
390 return OperatingSystem::OSX10_14;
392 if (os.EqualsLiteral("Darwin 19")) {
393 return OperatingSystem::OSX10_15;
395 if (os.EqualsLiteral("Darwin 20")) {
396 return OperatingSystem::OSX11_0;
398 if (os.EqualsLiteral("Android")) {
399 return OperatingSystem::Android;
400 // For historical reasons, "All" in blocklist means "All Windows"
402 if (os.EqualsLiteral("All")) {
403 return OperatingSystem::Windows;
405 if (os.EqualsLiteral("Darwin")) {
406 return OperatingSystem::OSX;
409 return OperatingSystem::Unknown;
412 static GfxDeviceFamily* BlocklistDevicesToDeviceFamily(
413 nsTArray<nsCString>& devices) {
414 if (devices.Length() == 0) return nullptr;
416 // For each device, get its device ID, and return a freshly-allocated
417 // GfxDeviceFamily with the contents of that array.
418 GfxDeviceFamily* deviceIds = new GfxDeviceFamily;
420 for (uint32_t i = 0; i < devices.Length(); ++i) {
421 // We make sure we don't add any "empty" device entries to the array, so
422 // we don't need to check if devices[i] is empty.
423 deviceIds->Append(NS_ConvertUTF8toUTF16(devices[i]));
426 return deviceIds;
429 static int32_t BlocklistFeatureToGfxFeature(const nsAString& aFeature) {
430 MOZ_ASSERT(!aFeature.IsEmpty());
431 if (aFeature.EqualsLiteral("DIRECT2D")) {
432 return nsIGfxInfo::FEATURE_DIRECT2D;
434 if (aFeature.EqualsLiteral("DIRECT3D_9_LAYERS")) {
435 return nsIGfxInfo::FEATURE_DIRECT3D_9_LAYERS;
437 if (aFeature.EqualsLiteral("DIRECT3D_10_LAYERS")) {
438 return nsIGfxInfo::FEATURE_DIRECT3D_10_LAYERS;
440 if (aFeature.EqualsLiteral("DIRECT3D_10_1_LAYERS")) {
441 return nsIGfxInfo::FEATURE_DIRECT3D_10_1_LAYERS;
443 if (aFeature.EqualsLiteral("DIRECT3D_11_LAYERS")) {
444 return nsIGfxInfo::FEATURE_DIRECT3D_11_LAYERS;
446 if (aFeature.EqualsLiteral("DIRECT3D_11_ANGLE")) {
447 return nsIGfxInfo::FEATURE_DIRECT3D_11_ANGLE;
449 if (aFeature.EqualsLiteral("HARDWARE_VIDEO_DECODING")) {
450 return nsIGfxInfo::FEATURE_HARDWARE_VIDEO_DECODING;
452 if (aFeature.EqualsLiteral("OPENGL_LAYERS")) {
453 return nsIGfxInfo::FEATURE_OPENGL_LAYERS;
455 if (aFeature.EqualsLiteral("WEBGL_OPENGL")) {
456 return nsIGfxInfo::FEATURE_WEBGL_OPENGL;
458 if (aFeature.EqualsLiteral("WEBGL_ANGLE")) {
459 return nsIGfxInfo::FEATURE_WEBGL_ANGLE;
461 if (aFeature.EqualsLiteral("WEBGL_MSAA")) {
462 return nsIGfxInfo::UNUSED_FEATURE_WEBGL_MSAA;
464 if (aFeature.EqualsLiteral("STAGEFRIGHT")) {
465 return nsIGfxInfo::FEATURE_STAGEFRIGHT;
467 if (aFeature.EqualsLiteral("WEBRTC_HW_ACCELERATION_ENCODE")) {
468 return nsIGfxInfo::FEATURE_WEBRTC_HW_ACCELERATION_ENCODE;
470 if (aFeature.EqualsLiteral("WEBRTC_HW_ACCELERATION_DECODE")) {
471 return nsIGfxInfo::FEATURE_WEBRTC_HW_ACCELERATION_DECODE;
473 if (aFeature.EqualsLiteral("WEBRTC_HW_ACCELERATION_H264")) {
474 return nsIGfxInfo::FEATURE_WEBRTC_HW_ACCELERATION_H264;
476 if (aFeature.EqualsLiteral("CANVAS2D_ACCELERATION")) {
477 return nsIGfxInfo::FEATURE_CANVAS2D_ACCELERATION;
479 if (aFeature.EqualsLiteral("DX_INTEROP2")) {
480 return nsIGfxInfo::FEATURE_DX_INTEROP2;
482 if (aFeature.EqualsLiteral("GPU_PROCESS")) {
483 return nsIGfxInfo::FEATURE_GPU_PROCESS;
485 if (aFeature.EqualsLiteral("WEBGL2")) {
486 return nsIGfxInfo::FEATURE_WEBGL2;
488 if (aFeature.EqualsLiteral("D3D11_KEYED_MUTEX")) {
489 return nsIGfxInfo::FEATURE_D3D11_KEYED_MUTEX;
491 if (aFeature.EqualsLiteral("WEBRENDER")) {
492 return nsIGfxInfo::FEATURE_WEBRENDER;
494 if (aFeature.EqualsLiteral("WEBRENDER_COMPOSITOR")) {
495 return nsIGfxInfo::FEATURE_WEBRENDER_COMPOSITOR;
497 if (aFeature.EqualsLiteral("DX_NV12")) {
498 return nsIGfxInfo::FEATURE_DX_NV12;
500 if (aFeature.EqualsLiteral("VP8_HW_DECODE")) {
501 return nsIGfxInfo::FEATURE_VP8_HW_DECODE;
503 if (aFeature.EqualsLiteral("VP9_HW_DECODE")) {
504 return nsIGfxInfo::FEATURE_VP9_HW_DECODE;
506 if (aFeature.EqualsLiteral("GL_SWIZZLE")) {
507 return nsIGfxInfo::FEATURE_GL_SWIZZLE;
509 if (aFeature.EqualsLiteral("WEBRENDER_SCISSORED_CACHE_CLEARS")) {
510 return nsIGfxInfo::FEATURE_WEBRENDER_SCISSORED_CACHE_CLEARS;
512 if (aFeature.EqualsLiteral("ALLOW_WEBGL_OUT_OF_PROCESS")) {
513 return nsIGfxInfo::FEATURE_ALLOW_WEBGL_OUT_OF_PROCESS;
515 if (aFeature.EqualsLiteral("THREADSAFE_GL")) {
516 return nsIGfxInfo::FEATURE_THREADSAFE_GL;
518 if (aFeature.EqualsLiteral("X11_EGL")) {
519 return nsIGfxInfo::FEATURE_X11_EGL;
521 if (aFeature.EqualsLiteral("DMABUF")) {
522 return nsIGfxInfo::FEATURE_DMABUF;
524 if (aFeature.EqualsLiteral("WEBGPU")) {
525 return nsIGfxInfo::FEATURE_WEBGPU;
527 if (aFeature.EqualsLiteral("VIDEO_OVERLAY")) {
528 return nsIGfxInfo::FEATURE_VIDEO_OVERLAY;
530 if (aFeature.EqualsLiteral("HW_DECODED_VIDEO_ZERO_COPY")) {
531 return nsIGfxInfo::FEATURE_HW_DECODED_VIDEO_ZERO_COPY;
533 if (aFeature.EqualsLiteral("REUSE_DECODER_DEVICE")) {
534 return nsIGfxInfo::FEATURE_REUSE_DECODER_DEVICE;
536 if (aFeature.EqualsLiteral("WEBRENDER_PARTIAL_PRESENT")) {
537 return nsIGfxInfo::FEATURE_WEBRENDER_PARTIAL_PRESENT;
539 if (aFeature.EqualsLiteral("BACKDROP_FILTER")) {
540 return nsIGfxInfo::FEATURE_BACKDROP_FILTER;
542 if (aFeature.EqualsLiteral("ACCELERATED_CANVAS2D")) {
543 return nsIGfxInfo::FEATURE_ACCELERATED_CANVAS2D;
546 // If we don't recognize the feature, it may be new, and something
547 // this version doesn't understand. So, nothing to do. This is
548 // different from feature not being specified at all, in which case
549 // this method should not get called and we should continue with the
550 // "all features" blocklisting.
551 return -1;
554 static int32_t BlocklistFeatureStatusToGfxFeatureStatus(
555 const nsAString& aStatus) {
556 if (aStatus.EqualsLiteral("STATUS_OK")) {
557 return nsIGfxInfo::FEATURE_STATUS_OK;
559 if (aStatus.EqualsLiteral("BLOCKED_DRIVER_VERSION")) {
560 return nsIGfxInfo::FEATURE_BLOCKED_DRIVER_VERSION;
562 if (aStatus.EqualsLiteral("BLOCKED_DEVICE")) {
563 return nsIGfxInfo::FEATURE_BLOCKED_DEVICE;
565 if (aStatus.EqualsLiteral("DISCOURAGED")) {
566 return nsIGfxInfo::FEATURE_DISCOURAGED;
568 if (aStatus.EqualsLiteral("BLOCKED_OS_VERSION")) {
569 return nsIGfxInfo::FEATURE_BLOCKED_OS_VERSION;
571 if (aStatus.EqualsLiteral("DENIED")) {
572 return nsIGfxInfo::FEATURE_DENIED;
574 if (aStatus.EqualsLiteral("ALLOW_QUALIFIED")) {
575 return nsIGfxInfo::FEATURE_ALLOW_QUALIFIED;
577 if (aStatus.EqualsLiteral("ALLOW_ALWAYS")) {
578 return nsIGfxInfo::FEATURE_ALLOW_ALWAYS;
581 // Do not allow it to set STATUS_UNKNOWN. Also, we are not
582 // expecting the "mismatch" status showing up here.
584 return nsIGfxInfo::FEATURE_STATUS_OK;
587 static VersionComparisonOp BlocklistComparatorToComparisonOp(
588 const nsAString& op) {
589 if (op.EqualsLiteral("LESS_THAN")) {
590 return DRIVER_LESS_THAN;
592 if (op.EqualsLiteral("BUILD_ID_LESS_THAN")) {
593 return DRIVER_BUILD_ID_LESS_THAN;
595 if (op.EqualsLiteral("LESS_THAN_OR_EQUAL")) {
596 return DRIVER_LESS_THAN_OR_EQUAL;
598 if (op.EqualsLiteral("BUILD_ID_LESS_THAN_OR_EQUAL")) {
599 return DRIVER_BUILD_ID_LESS_THAN_OR_EQUAL;
601 if (op.EqualsLiteral("GREATER_THAN")) {
602 return DRIVER_GREATER_THAN;
604 if (op.EqualsLiteral("GREATER_THAN_OR_EQUAL")) {
605 return DRIVER_GREATER_THAN_OR_EQUAL;
607 if (op.EqualsLiteral("EQUAL")) {
608 return DRIVER_EQUAL;
610 if (op.EqualsLiteral("NOT_EQUAL")) {
611 return DRIVER_NOT_EQUAL;
613 if (op.EqualsLiteral("BETWEEN_EXCLUSIVE")) {
614 return DRIVER_BETWEEN_EXCLUSIVE;
616 if (op.EqualsLiteral("BETWEEN_INCLUSIVE")) {
617 return DRIVER_BETWEEN_INCLUSIVE;
619 if (op.EqualsLiteral("BETWEEN_INCLUSIVE_START")) {
620 return DRIVER_BETWEEN_INCLUSIVE_START;
623 return DRIVER_COMPARISON_IGNORED;
627 Deserialize Blocklist entries from string.
628 e.g:
629 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
631 static bool BlocklistEntryToDriverInfo(const nsACString& aBlocklistEntry,
632 GfxDriverInfo& aDriverInfo) {
633 // If we get an application version to be zero, something is not working
634 // and we are not going to bother checking the blocklist versions.
635 // See TestGfxWidgets.cpp for how version comparison works.
636 // <versionRange minVersion="42.0a1" maxVersion="45.0"></versionRange>
637 static mozilla::Version zeroV("0");
638 static mozilla::Version appV(GfxInfoBase::GetApplicationVersion().get());
639 if (appV <= zeroV) {
640 gfxCriticalErrorOnce(gfxCriticalError::DefaultOptions(false))
641 << "Invalid application version "
642 << GfxInfoBase::GetApplicationVersion().get();
645 aDriverInfo.mRuleId = "FEATURE_FAILURE_DL_BLOCKLIST_NO_ID"_ns;
647 for (const auto& keyValue : aBlocklistEntry.Split('\t')) {
648 nsTArray<nsCString> splitted;
649 ParseString(keyValue, ':', splitted);
650 if (splitted.Length() != 2) {
651 // If we don't recognize the input data, we do not want to proceed.
652 gfxCriticalErrorOnce(CriticalLog::DefaultOptions(false))
653 << "Unrecognized data " << nsCString(keyValue).get();
654 return false;
656 const nsCString& key = splitted[0];
657 const nsCString& value = splitted[1];
658 NS_ConvertUTF8toUTF16 dataValue(value);
660 if (value.Length() == 0) {
661 // Safety check for empty values.
662 gfxCriticalErrorOnce(CriticalLog::DefaultOptions(false))
663 << "Empty value for " << key.get();
664 return false;
667 if (key.EqualsLiteral("blockID")) {
668 nsCString blockIdStr = "FEATURE_FAILURE_DL_BLOCKLIST_"_ns + value;
669 aDriverInfo.mRuleId = blockIdStr.get();
670 } else if (key.EqualsLiteral("os")) {
671 aDriverInfo.mOperatingSystem = BlocklistOSToOperatingSystem(dataValue);
672 } else if (key.EqualsLiteral("osversion")) {
673 aDriverInfo.mOperatingSystemVersion = strtoul(value.get(), nullptr, 10);
674 } else if (key.EqualsLiteral("windowProtocol")) {
675 aDriverInfo.mWindowProtocol = dataValue;
676 } else if (key.EqualsLiteral("vendor")) {
677 aDriverInfo.mAdapterVendor = dataValue;
678 } else if (key.EqualsLiteral("driverVendor")) {
679 aDriverInfo.mDriverVendor = dataValue;
680 } else if (key.EqualsLiteral("feature")) {
681 aDriverInfo.mFeature = BlocklistFeatureToGfxFeature(dataValue);
682 if (aDriverInfo.mFeature < 0) {
683 // If we don't recognize the feature, we do not want to proceed.
684 gfxWarning() << "Unrecognized feature " << value.get();
685 return false;
687 } else if (key.EqualsLiteral("featureStatus")) {
688 aDriverInfo.mFeatureStatus =
689 BlocklistFeatureStatusToGfxFeatureStatus(dataValue);
690 } else if (key.EqualsLiteral("driverVersion")) {
691 uint64_t version;
692 if (ParseDriverVersion(dataValue, &version))
693 aDriverInfo.mDriverVersion = version;
694 } else if (key.EqualsLiteral("driverVersionMax")) {
695 uint64_t version;
696 if (ParseDriverVersion(dataValue, &version))
697 aDriverInfo.mDriverVersionMax = version;
698 } else if (key.EqualsLiteral("driverVersionComparator")) {
699 aDriverInfo.mComparisonOp = BlocklistComparatorToComparisonOp(dataValue);
700 } else if (key.EqualsLiteral("model")) {
701 aDriverInfo.mModel = dataValue;
702 } else if (key.EqualsLiteral("product")) {
703 aDriverInfo.mProduct = dataValue;
704 } else if (key.EqualsLiteral("manufacturer")) {
705 aDriverInfo.mManufacturer = dataValue;
706 } else if (key.EqualsLiteral("hardware")) {
707 aDriverInfo.mHardware = dataValue;
708 } else if (key.EqualsLiteral("versionRange")) {
709 nsTArray<nsCString> versionRange;
710 ParseString(value, ',', versionRange);
711 if (versionRange.Length() != 2) {
712 gfxCriticalErrorOnce(CriticalLog::DefaultOptions(false))
713 << "Unrecognized versionRange " << value.get();
714 return false;
716 const nsCString& minValue = versionRange[0];
717 const nsCString& maxValue = versionRange[1];
719 mozilla::Version minV(minValue.get());
720 mozilla::Version maxV(maxValue.get());
722 if (minV > zeroV && !(appV >= minV)) {
723 // The version of the application is less than the minimal version
724 // this blocklist entry applies to, so we can just ignore it by
725 // returning false and letting the caller deal with it.
726 return false;
728 if (maxV > zeroV && !(appV <= maxV)) {
729 // The version of the application is more than the maximal version
730 // this blocklist entry applies to, so we can just ignore it by
731 // returning false and letting the caller deal with it.
732 return false;
734 } else if (key.EqualsLiteral("devices")) {
735 nsTArray<nsCString> devices;
736 ParseString(value, ',', devices);
737 GfxDeviceFamily* deviceIds = BlocklistDevicesToDeviceFamily(devices);
738 if (deviceIds) {
739 // Get GfxDriverInfo to adopt the devices array we created.
740 aDriverInfo.mDeleteDevices = true;
741 aDriverInfo.mDevices = deviceIds;
744 // We explicitly ignore unknown elements.
747 return true;
750 NS_IMETHODIMP
751 GfxInfoBase::Observe(nsISupports* aSubject, const char* aTopic,
752 const char16_t* aData) {
753 if (strcmp(aTopic, "blocklist-data-gfxItems") == 0) {
754 nsTArray<GfxDriverInfo> driverInfo;
755 NS_ConvertUTF16toUTF8 utf8Data(aData);
757 for (const auto& blocklistEntry : utf8Data.Split('\n')) {
758 GfxDriverInfo di;
759 if (BlocklistEntryToDriverInfo(blocklistEntry, di)) {
760 // XXX Changing this to driverInfo.AppendElement(di) causes leaks.
761 // Probably some non-standard semantics of the copy/move operations?
762 *driverInfo.AppendElement() = di;
763 // Prevent di falling out of scope from destroying the devices.
764 di.mDeleteDevices = false;
765 } else {
766 driverInfo.AppendElement();
770 EvaluateDownloadedBlocklist(driverInfo);
773 return NS_OK;
776 GfxInfoBase::GfxInfoBase() : mScreenPixels(INT64_MAX), mMutex("GfxInfoBase") {}
778 GfxInfoBase::~GfxInfoBase() = default;
780 nsresult GfxInfoBase::Init() {
781 InitGfxDriverInfoShutdownObserver();
783 nsCOMPtr<nsIObserverService> os = mozilla::services::GetObserverService();
784 if (os) {
785 os->AddObserver(this, "blocklist-data-gfxItems", true);
788 return NS_OK;
791 void GfxInfoBase::GetData() {
792 if (mScreenPixels != INT64_MAX) {
793 // Already initialized.
794 return;
797 ScreenManager::GetSingleton().GetTotalScreenPixels(&mScreenPixels);
800 NS_IMETHODIMP
801 GfxInfoBase::GetFeatureStatus(int32_t aFeature, nsACString& aFailureId,
802 int32_t* aStatus) {
803 // Ignore the gfx.blocklist.all pref on release and beta.
804 #if defined(RELEASE_OR_BETA)
805 int32_t blocklistAll = 0;
806 #else
807 int32_t blocklistAll = StaticPrefs::gfx_blocklist_all_AtStartup();
808 #endif
809 if (blocklistAll > 0) {
810 gfxCriticalErrorOnce(gfxCriticalError::DefaultOptions(false))
811 << "Forcing blocklisting all features";
812 *aStatus = FEATURE_BLOCKED_DEVICE;
813 aFailureId = "FEATURE_FAILURE_BLOCK_ALL";
814 return NS_OK;
817 if (blocklistAll < 0) {
818 gfxCriticalErrorOnce(gfxCriticalError::DefaultOptions(false))
819 << "Ignoring any feature blocklisting.";
820 *aStatus = FEATURE_STATUS_OK;
821 return NS_OK;
824 // This is how we evaluate the downloadable blocklist. If there is no pref,
825 // then we will fallback to checking the static blocklist.
826 if (GetPrefValueForFeature(aFeature, *aStatus, aFailureId)) {
827 return NS_OK;
830 if (XRE_IsContentProcess() || XRE_IsGPUProcess()) {
831 // Use the cached data received from the parent process.
832 MOZ_ASSERT(sFeatureStatus);
833 bool success = false;
834 for (const auto& fs : *sFeatureStatus) {
835 if (fs.feature() == aFeature) {
836 aFailureId = fs.failureId();
837 *aStatus = fs.status();
838 success = true;
839 break;
842 return success ? NS_OK : NS_ERROR_FAILURE;
845 nsString version;
846 nsTArray<GfxDriverInfo> driverInfo;
847 nsresult rv =
848 GetFeatureStatusImpl(aFeature, aStatus, version, driverInfo, aFailureId);
849 return rv;
852 nsTArray<gfx::GfxInfoFeatureStatus> GfxInfoBase::GetAllFeatures() {
853 MOZ_RELEASE_ASSERT(XRE_IsParentProcess());
854 if (!sFeatureStatus) {
855 InitFeatureStatus(new nsTArray<gfx::GfxInfoFeatureStatus>());
856 for (int32_t i = 1; i <= nsIGfxInfo::FEATURE_MAX_VALUE; ++i) {
857 int32_t status = 0;
858 nsAutoCString failureId;
859 GetFeatureStatus(i, failureId, &status);
860 gfx::GfxInfoFeatureStatus gfxFeatureStatus;
861 gfxFeatureStatus.feature() = i;
862 gfxFeatureStatus.status() = status;
863 gfxFeatureStatus.failureId() = failureId;
864 sFeatureStatus->AppendElement(gfxFeatureStatus);
868 nsTArray<gfx::GfxInfoFeatureStatus> features;
869 for (const auto& status : *sFeatureStatus) {
870 gfx::GfxInfoFeatureStatus copy = status;
871 features.AppendElement(copy);
873 return features;
876 inline bool MatchingAllowStatus(int32_t aStatus) {
877 switch (aStatus) {
878 case nsIGfxInfo::FEATURE_ALLOW_ALWAYS:
879 case nsIGfxInfo::FEATURE_ALLOW_QUALIFIED:
880 return true;
881 default:
882 return false;
886 // Matching OS go somewhat beyond the simple equality check because of the
887 // "All Windows" and "All OS X" variations.
889 // aBlockedOS is describing the system(s) we are trying to block.
890 // aSystemOS is describing the system we are running on.
892 // aSystemOS should not be "Windows" or "OSX" - it should be set to
893 // a particular version instead.
894 // However, it is valid for aBlockedOS to be one of those generic values,
895 // as we could be blocking all of the versions.
896 inline bool MatchingOperatingSystems(OperatingSystem aBlockedOS,
897 OperatingSystem aSystemOS,
898 uint32_t aSystemOSBuild) {
899 MOZ_ASSERT(aSystemOS != OperatingSystem::Windows &&
900 aSystemOS != OperatingSystem::OSX);
902 // If the block entry OS is unknown, it doesn't match
903 if (aBlockedOS == OperatingSystem::Unknown) {
904 return false;
907 #if defined(XP_WIN)
908 if (aBlockedOS == OperatingSystem::Windows) {
909 // We do want even "unknown" aSystemOS to fall under "all windows"
910 return true;
913 constexpr uint32_t kMinWin10BuildNumber = 18362;
914 if (aBlockedOS == OperatingSystem::RecentWindows10 &&
915 aSystemOS == OperatingSystem::Windows10) {
916 // For allowlist purposes, we sometimes want to restrict to only recent
917 // versions of Windows 10. This is a bit of a kludge but easier than adding
918 // complicated blocklist infrastructure for build ID comparisons like driver
919 // versions.
920 return aSystemOSBuild >= kMinWin10BuildNumber;
923 if (aBlockedOS == OperatingSystem::NotRecentWindows10) {
924 if (aSystemOS == OperatingSystem::Windows10) {
925 return aSystemOSBuild < kMinWin10BuildNumber;
926 } else {
927 return true;
930 #endif
932 #if defined(XP_MACOSX)
933 if (aBlockedOS == OperatingSystem::OSX) {
934 // We do want even "unknown" aSystemOS to fall under "all OS X"
935 return true;
937 #endif
939 return aSystemOS == aBlockedOS;
942 inline bool MatchingBattery(BatteryStatus aBatteryStatus, bool aHasBattery) {
943 switch (aBatteryStatus) {
944 case BatteryStatus::All:
945 return true;
946 case BatteryStatus::None:
947 return !aHasBattery;
948 case BatteryStatus::Present:
949 return aHasBattery;
952 MOZ_ASSERT_UNREACHABLE("bad battery status");
953 return false;
956 inline bool MatchingScreenSize(ScreenSizeStatus aScreenStatus,
957 int64_t aScreenPixels) {
958 constexpr int64_t kMaxSmallPixels = 2304000; // 1920x1200
959 constexpr int64_t kMaxMediumPixels = 4953600; // 3440x1440
961 switch (aScreenStatus) {
962 case ScreenSizeStatus::All:
963 return true;
964 case ScreenSizeStatus::Small:
965 return aScreenPixels <= kMaxSmallPixels;
966 case ScreenSizeStatus::SmallAndMedium:
967 return aScreenPixels <= kMaxMediumPixels;
968 case ScreenSizeStatus::Medium:
969 return aScreenPixels > kMaxSmallPixels &&
970 aScreenPixels <= kMaxMediumPixels;
971 case ScreenSizeStatus::MediumAndLarge:
972 return aScreenPixels > kMaxSmallPixels;
973 case ScreenSizeStatus::Large:
974 return aScreenPixels > kMaxMediumPixels;
977 MOZ_ASSERT_UNREACHABLE("bad screen status");
978 return false;
981 int32_t GfxInfoBase::FindBlocklistedDeviceInList(
982 const nsTArray<GfxDriverInfo>& info, nsAString& aSuggestedVersion,
983 int32_t aFeature, nsACString& aFailureId, OperatingSystem os,
984 bool aForAllowing) {
985 int32_t status = nsIGfxInfo::FEATURE_STATUS_UNKNOWN;
987 // Some properties are not available on all platforms.
988 nsAutoString windowProtocol;
989 nsresult rv = GetWindowProtocol(windowProtocol);
990 if (NS_FAILED(rv) && rv != NS_ERROR_NOT_IMPLEMENTED) {
991 return 0;
994 bool hasBattery = false;
995 rv = GetHasBattery(&hasBattery);
996 if (NS_FAILED(rv) && rv != NS_ERROR_NOT_IMPLEMENTED) {
997 return 0;
1000 uint32_t osBuild = OperatingSystemBuild();
1002 // Get the adapters once then reuse below
1003 nsAutoString adapterVendorID[2];
1004 nsAutoString adapterDeviceID[2];
1005 nsAutoString adapterDriverVendor[2];
1006 nsAutoString adapterDriverVersionString[2];
1007 bool adapterInfoFailed[2];
1009 adapterInfoFailed[0] =
1010 (NS_FAILED(GetAdapterVendorID(adapterVendorID[0])) ||
1011 NS_FAILED(GetAdapterDeviceID(adapterDeviceID[0])) ||
1012 NS_FAILED(GetAdapterDriverVendor(adapterDriverVendor[0])) ||
1013 NS_FAILED(GetAdapterDriverVersion(adapterDriverVersionString[0])));
1014 adapterInfoFailed[1] =
1015 (NS_FAILED(GetAdapterVendorID2(adapterVendorID[1])) ||
1016 NS_FAILED(GetAdapterDeviceID2(adapterDeviceID[1])) ||
1017 NS_FAILED(GetAdapterDriverVendor2(adapterDriverVendor[1])) ||
1018 NS_FAILED(GetAdapterDriverVersion2(adapterDriverVersionString[1])));
1019 // No point in going on if we don't have adapter info
1020 if (adapterInfoFailed[0] && adapterInfoFailed[1]) {
1021 return 0;
1024 #if defined(XP_WIN) || defined(ANDROID) || defined(MOZ_WIDGET_GTK)
1025 uint64_t driverVersion[2] = {0, 0};
1026 if (!adapterInfoFailed[0]) {
1027 ParseDriverVersion(adapterDriverVersionString[0], &driverVersion[0]);
1029 if (!adapterInfoFailed[1]) {
1030 ParseDriverVersion(adapterDriverVersionString[1], &driverVersion[1]);
1032 #endif
1034 uint32_t i = 0;
1035 for (; i < info.Length(); i++) {
1036 // If the status is FEATURE_ALLOW_*, then it is for the allowlist, not
1037 // blocklisting. Only consider entries for our search mode.
1038 if (MatchingAllowStatus(info[i].mFeatureStatus) != aForAllowing) {
1039 continue;
1042 // If we don't have the info for this GPU, no need to check further.
1043 // It is unclear that we would ever have a mixture of 1st and 2nd
1044 // GPU, but leaving the code in for that possibility for now.
1045 // (Actually, currently mGpu2 will never be true, so this can
1046 // be optimized out.)
1047 uint32_t infoIndex = info[i].mGpu2 ? 1 : 0;
1048 if (adapterInfoFailed[infoIndex]) {
1049 continue;
1052 // Do the operating system check first, no point in getting the driver
1053 // info if we won't need to use it.
1054 if (!MatchingOperatingSystems(info[i].mOperatingSystem, os, osBuild)) {
1055 continue;
1058 if (info[i].mOperatingSystemVersion &&
1059 info[i].mOperatingSystemVersion != OperatingSystemVersion()) {
1060 continue;
1063 if (!MatchingBattery(info[i].mBattery, hasBattery)) {
1064 continue;
1067 if (!MatchingScreenSize(info[i].mScreen, mScreenPixels)) {
1068 continue;
1071 if (!DoesWindowProtocolMatch(info[i].mWindowProtocol, windowProtocol)) {
1072 continue;
1075 if (!DoesVendorMatch(info[i].mAdapterVendor, adapterVendorID[infoIndex])) {
1076 continue;
1079 if (!DoesDriverVendorMatch(info[i].mDriverVendor,
1080 adapterDriverVendor[infoIndex])) {
1081 continue;
1084 if (info[i].mDevices && !info[i].mDevices->IsEmpty()) {
1085 nsresult rv = info[i].mDevices->Contains(adapterDeviceID[infoIndex]);
1086 if (rv == NS_ERROR_NOT_AVAILABLE) {
1087 // Not found
1088 continue;
1090 if (rv != NS_OK) {
1091 // Failed to search, allowlist should not match, blocklist should match
1092 // for safety reasons
1093 if (aForAllowing) {
1094 continue;
1096 break;
1100 bool match = false;
1102 if (!info[i].mHardware.IsEmpty() && !info[i].mHardware.Equals(Hardware())) {
1103 continue;
1105 if (!info[i].mModel.IsEmpty() && !info[i].mModel.Equals(Model())) {
1106 continue;
1108 if (!info[i].mProduct.IsEmpty() && !info[i].mProduct.Equals(Product())) {
1109 continue;
1111 if (!info[i].mManufacturer.IsEmpty() &&
1112 !info[i].mManufacturer.Equals(Manufacturer())) {
1113 continue;
1116 #if defined(XP_WIN) || defined(ANDROID) || defined(MOZ_WIDGET_GTK)
1117 switch (info[i].mComparisonOp) {
1118 case DRIVER_LESS_THAN:
1119 match = driverVersion[infoIndex] < info[i].mDriverVersion;
1120 break;
1121 case DRIVER_BUILD_ID_LESS_THAN:
1122 match = (driverVersion[infoIndex] & 0xFFFF) < info[i].mDriverVersion;
1123 break;
1124 case DRIVER_LESS_THAN_OR_EQUAL:
1125 match = driverVersion[infoIndex] <= info[i].mDriverVersion;
1126 break;
1127 case DRIVER_BUILD_ID_LESS_THAN_OR_EQUAL:
1128 match = (driverVersion[infoIndex] & 0xFFFF) <= info[i].mDriverVersion;
1129 break;
1130 case DRIVER_GREATER_THAN:
1131 match = driverVersion[infoIndex] > info[i].mDriverVersion;
1132 break;
1133 case DRIVER_GREATER_THAN_OR_EQUAL:
1134 match = driverVersion[infoIndex] >= info[i].mDriverVersion;
1135 break;
1136 case DRIVER_EQUAL:
1137 match = driverVersion[infoIndex] == info[i].mDriverVersion;
1138 break;
1139 case DRIVER_NOT_EQUAL:
1140 match = driverVersion[infoIndex] != info[i].mDriverVersion;
1141 break;
1142 case DRIVER_BETWEEN_EXCLUSIVE:
1143 match = driverVersion[infoIndex] > info[i].mDriverVersion &&
1144 driverVersion[infoIndex] < info[i].mDriverVersionMax;
1145 break;
1146 case DRIVER_BETWEEN_INCLUSIVE:
1147 match = driverVersion[infoIndex] >= info[i].mDriverVersion &&
1148 driverVersion[infoIndex] <= info[i].mDriverVersionMax;
1149 break;
1150 case DRIVER_BETWEEN_INCLUSIVE_START:
1151 match = driverVersion[infoIndex] >= info[i].mDriverVersion &&
1152 driverVersion[infoIndex] < info[i].mDriverVersionMax;
1153 break;
1154 case DRIVER_COMPARISON_IGNORED:
1155 // We don't have a comparison op, so we match everything.
1156 match = true;
1157 break;
1158 default:
1159 NS_WARNING("Bogus op in GfxDriverInfo");
1160 break;
1162 #else
1163 // We don't care what driver version it was. We only check OS version and if
1164 // the device matches.
1165 match = true;
1166 #endif
1168 if (match || info[i].mDriverVersion == GfxDriverInfo::allDriverVersions) {
1169 if (info[i].mFeature == GfxDriverInfo::allFeatures ||
1170 info[i].mFeature == aFeature) {
1171 status = info[i].mFeatureStatus;
1172 if (!info[i].mRuleId.IsEmpty()) {
1173 aFailureId = info[i].mRuleId.get();
1174 } else {
1175 aFailureId = "FEATURE_FAILURE_DL_BLOCKLIST_NO_ID";
1177 break;
1182 #if defined(XP_WIN)
1183 // As a very special case, we block D2D on machines with an NVidia 310M GPU
1184 // as either the primary or secondary adapter. D2D is also blocked when the
1185 // NV 310M is the primary adapter (using the standard blocklisting mechanism).
1186 // If the primary GPU already matched something in the blocklist then we
1187 // ignore this special rule. See bug 1008759.
1188 if (status == nsIGfxInfo::FEATURE_STATUS_UNKNOWN &&
1189 (aFeature == nsIGfxInfo::FEATURE_DIRECT2D)) {
1190 if (!adapterInfoFailed[1]) {
1191 nsAString& nvVendorID =
1192 (nsAString&)GfxDriverInfo::GetDeviceVendor(DeviceVendor::NVIDIA);
1193 const nsString nv310mDeviceId = u"0x0A70"_ns;
1194 if (nvVendorID.Equals(adapterVendorID[1],
1195 nsCaseInsensitiveStringComparator) &&
1196 nv310mDeviceId.Equals(adapterDeviceID[1],
1197 nsCaseInsensitiveStringComparator)) {
1198 status = nsIGfxInfo::FEATURE_BLOCKED_DEVICE;
1199 aFailureId = "FEATURE_FAILURE_D2D_NV310M_BLOCK";
1204 // Depends on Windows driver versioning. We don't pass a GfxDriverInfo object
1205 // back to the Windows handler, so we must handle this here.
1206 if (status == FEATURE_BLOCKED_DRIVER_VERSION) {
1207 if (info[i].mSuggestedVersion) {
1208 aSuggestedVersion.AppendPrintf("%s", info[i].mSuggestedVersion);
1209 } else if (info[i].mComparisonOp == DRIVER_LESS_THAN &&
1210 info[i].mDriverVersion != GfxDriverInfo::allDriverVersions) {
1211 aSuggestedVersion.AppendPrintf(
1212 "%lld.%lld.%lld.%lld",
1213 (info[i].mDriverVersion & 0xffff000000000000) >> 48,
1214 (info[i].mDriverVersion & 0x0000ffff00000000) >> 32,
1215 (info[i].mDriverVersion & 0x00000000ffff0000) >> 16,
1216 (info[i].mDriverVersion & 0x000000000000ffff));
1219 #endif
1221 return status;
1224 void GfxInfoBase::SetFeatureStatus(nsTArray<gfx::GfxInfoFeatureStatus>&& aFS) {
1225 MOZ_ASSERT(!sFeatureStatus);
1226 InitFeatureStatus(new nsTArray<gfx::GfxInfoFeatureStatus>(std::move(aFS)));
1229 bool GfxInfoBase::DoesWindowProtocolMatch(
1230 const nsAString& aBlocklistWindowProtocol,
1231 const nsAString& aWindowProtocol) {
1232 return aBlocklistWindowProtocol.Equals(aWindowProtocol,
1233 nsCaseInsensitiveStringComparator) ||
1234 aBlocklistWindowProtocol.Equals(
1235 GfxDriverInfo::GetWindowProtocol(WindowProtocol::All),
1236 nsCaseInsensitiveStringComparator);
1239 bool GfxInfoBase::DoesVendorMatch(const nsAString& aBlocklistVendor,
1240 const nsAString& aAdapterVendor) {
1241 return aBlocklistVendor.Equals(aAdapterVendor,
1242 nsCaseInsensitiveStringComparator) ||
1243 aBlocklistVendor.Equals(
1244 GfxDriverInfo::GetDeviceVendor(DeviceVendor::All),
1245 nsCaseInsensitiveStringComparator);
1248 bool GfxInfoBase::DoesDriverVendorMatch(const nsAString& aBlocklistVendor,
1249 const nsAString& aDriverVendor) {
1250 return aBlocklistVendor.Equals(aDriverVendor,
1251 nsCaseInsensitiveStringComparator) ||
1252 aBlocklistVendor.Equals(
1253 GfxDriverInfo::GetDriverVendor(DriverVendor::All),
1254 nsCaseInsensitiveStringComparator);
1257 bool GfxInfoBase::IsFeatureAllowlisted(int32_t aFeature) const {
1258 return aFeature == nsIGfxInfo::FEATURE_VIDEO_OVERLAY ||
1259 aFeature == nsIGfxInfo::FEATURE_HW_DECODED_VIDEO_ZERO_COPY;
1262 nsresult GfxInfoBase::GetFeatureStatusImpl(
1263 int32_t aFeature, int32_t* aStatus, nsAString& aSuggestedVersion,
1264 const nsTArray<GfxDriverInfo>& aDriverInfo, nsACString& aFailureId,
1265 OperatingSystem* aOS /* = nullptr */) {
1266 if (aFeature <= 0) {
1267 gfxWarning() << "Invalid feature <= 0";
1268 return NS_OK;
1271 if (*aStatus != nsIGfxInfo::FEATURE_STATUS_UNKNOWN) {
1272 // Terminate now with the status determined by the derived type (OS-specific
1273 // code).
1274 return NS_OK;
1277 if (sShutdownOccurred) {
1278 // This is futile; we've already commenced shutdown and our blocklists have
1279 // been deleted. We may want to look into resurrecting the blocklist instead
1280 // but for now, just don't even go there.
1281 return NS_OK;
1284 // Ensure any additional initialization required is complete.
1285 GetData();
1287 // If an operating system was provided by the derived GetFeatureStatusImpl,
1288 // grab it here. Otherwise, the OS is unknown.
1289 OperatingSystem os = (aOS ? *aOS : OperatingSystem::Unknown);
1291 nsAutoString adapterVendorID;
1292 nsAutoString adapterDeviceID;
1293 nsAutoString adapterDriverVersionString;
1294 if (NS_FAILED(GetAdapterVendorID(adapterVendorID)) ||
1295 NS_FAILED(GetAdapterDeviceID(adapterDeviceID)) ||
1296 NS_FAILED(GetAdapterDriverVersion(adapterDriverVersionString))) {
1297 aFailureId = "FEATURE_FAILURE_CANT_RESOLVE_ADAPTER";
1298 *aStatus = FEATURE_BLOCKED_DEVICE;
1299 return NS_OK;
1302 // We only check either the given blocklist, or the static list, as given.
1303 int32_t status;
1304 if (aDriverInfo.Length()) {
1305 status =
1306 FindBlocklistedDeviceInList(aDriverInfo, aSuggestedVersion, aFeature,
1307 aFailureId, os, /* aForAllowing */ false);
1308 } else {
1309 if (!sDriverInfo) {
1310 sDriverInfo = new nsTArray<GfxDriverInfo>();
1312 status = FindBlocklistedDeviceInList(GetGfxDriverInfo(), aSuggestedVersion,
1313 aFeature, aFailureId, os,
1314 /* aForAllowing */ false);
1317 if (status == nsIGfxInfo::FEATURE_STATUS_UNKNOWN) {
1318 if (IsFeatureAllowlisted(aFeature)) {
1319 // This feature is actually using the allowlist; that means after we pass
1320 // the blocklist to prevent us explicitly from getting the feature, we now
1321 // need to check the allowlist to ensure we are allowed to get it in the
1322 // first place.
1323 if (aDriverInfo.Length()) {
1324 status = FindBlocklistedDeviceInList(aDriverInfo, aSuggestedVersion,
1325 aFeature, aFailureId, os,
1326 /* aForAllowing */ true);
1327 } else {
1328 status = FindBlocklistedDeviceInList(
1329 GetGfxDriverInfo(), aSuggestedVersion, aFeature, aFailureId, os,
1330 /* aForAllowing */ true);
1333 if (status == nsIGfxInfo::FEATURE_STATUS_UNKNOWN) {
1334 status = nsIGfxInfo::FEATURE_DENIED;
1336 } else {
1337 // It's now done being processed. It's safe to set the status to
1338 // STATUS_OK.
1339 status = nsIGfxInfo::FEATURE_STATUS_OK;
1343 *aStatus = status;
1344 return NS_OK;
1347 NS_IMETHODIMP
1348 GfxInfoBase::GetFeatureSuggestedDriverVersion(int32_t aFeature,
1349 nsAString& aVersion) {
1350 nsCString version;
1351 if (GetPrefValueForDriverVersion(version)) {
1352 aVersion = NS_ConvertASCIItoUTF16(version);
1353 return NS_OK;
1356 int32_t status;
1357 nsCString discardFailureId;
1358 nsTArray<GfxDriverInfo> driverInfo;
1359 return GetFeatureStatusImpl(aFeature, &status, aVersion, driverInfo,
1360 discardFailureId);
1363 void GfxInfoBase::EvaluateDownloadedBlocklist(
1364 nsTArray<GfxDriverInfo>& aDriverInfo) {
1365 // If the list is empty, then we don't actually want to call
1366 // GetFeatureStatusImpl since we will use the static list instead. In that
1367 // case, all we want to do is make sure the pref is removed.
1368 if (aDriverInfo.IsEmpty()) {
1369 gfxCriticalNoteOnce << "Evaluate empty downloaded blocklist";
1370 return;
1373 OperatingSystem os = GetOperatingSystem();
1375 // For every feature we know about, we evaluate whether this blocklist has a
1376 // non-STATUS_OK status. If it does, we set the pref we evaluate in
1377 // GetFeatureStatus above, so we don't need to hold on to this blocklist
1378 // anywhere permanent.
1379 for (int feature = 1; feature <= nsIGfxInfo::FEATURE_MAX_VALUE; ++feature) {
1380 int32_t status = nsIGfxInfo::FEATURE_STATUS_UNKNOWN;
1381 nsCString failureId;
1382 nsAutoString suggestedVersion;
1384 // Note that we are careful to call the base class method since we only want
1385 // to evaluate the downloadable blocklist for these prefs.
1386 MOZ_ALWAYS_TRUE(NS_SUCCEEDED(GfxInfoBase::GetFeatureStatusImpl(
1387 feature, &status, suggestedVersion, aDriverInfo, failureId, &os)));
1389 switch (status) {
1390 default:
1391 MOZ_FALLTHROUGH_ASSERT("Unhandled feature status!");
1392 case nsIGfxInfo::FEATURE_STATUS_UNKNOWN:
1393 // This may be returned during shutdown or for invalid features.
1394 case nsIGfxInfo::FEATURE_ALLOW_ALWAYS:
1395 case nsIGfxInfo::FEATURE_ALLOW_QUALIFIED:
1396 case nsIGfxInfo::FEATURE_DENIED:
1397 // We cannot use the downloadable blocklist to control the allowlist.
1398 // If a feature is allowlisted, then we should also ignore DENIED
1399 // statuses from GetFeatureStatusImpl because we don't check the
1400 // static list when and this is an expected value. If we wish to
1401 // override the allowlist, it is as simple as creating a normal
1402 // blocklist rule with a BLOCKED* status code.
1403 case nsIGfxInfo::FEATURE_STATUS_OK:
1404 RemovePrefForFeature(feature);
1405 break;
1407 case nsIGfxInfo::FEATURE_BLOCKED_DRIVER_VERSION:
1408 if (!suggestedVersion.IsEmpty()) {
1409 SetPrefValueForDriverVersion(suggestedVersion);
1410 } else {
1411 RemovePrefForDriverVersion();
1413 [[fallthrough]];
1415 case nsIGfxInfo::FEATURE_BLOCKED_MISMATCHED_VERSION:
1416 case nsIGfxInfo::FEATURE_BLOCKED_DEVICE:
1417 case nsIGfxInfo::FEATURE_DISCOURAGED:
1418 case nsIGfxInfo::FEATURE_BLOCKED_OS_VERSION:
1419 case nsIGfxInfo::FEATURE_BLOCKED_PLATFORM_TEST:
1420 SetPrefValueForFeature(feature, status, failureId);
1421 break;
1426 NS_IMETHODIMP_(void)
1427 GfxInfoBase::LogFailure(const nsACString& failure) {
1428 // gfxCriticalError has a mutex lock of its own, so we may not actually
1429 // need this lock. ::GetFailures() accesses the data but the LogForwarder
1430 // will not return the copy of the logs unless it can get the same lock
1431 // that gfxCriticalError uses. Still, that is so much of an implementation
1432 // detail that it's nicer to just add an extra lock here and in
1433 // ::GetFailures()
1434 MutexAutoLock lock(mMutex);
1436 // By default, gfxCriticalError asserts; make it not assert in this case.
1437 gfxCriticalError(CriticalLog::DefaultOptions(false))
1438 << "(LF) " << failure.BeginReading();
1441 NS_IMETHODIMP GfxInfoBase::GetFailures(nsTArray<int32_t>& indices,
1442 nsTArray<nsCString>& failures) {
1443 MutexAutoLock lock(mMutex);
1445 LogForwarder* logForwarder = Factory::GetLogForwarder();
1446 if (!logForwarder) {
1447 return NS_ERROR_UNEXPECTED;
1450 // There are two string copies in this method, starting with this one. We are
1451 // assuming this is not a big deal, as the size of the array should be small
1452 // and the strings in it should be small as well (the error messages in the
1453 // code.) The second copy happens with the AppendElement() calls.
1454 // Technically, we don't need the mutex lock after the StringVectorCopy()
1455 // call.
1456 LoggingRecord loggedStrings = logForwarder->LoggingRecordCopy();
1457 LoggingRecord::const_iterator it;
1458 for (it = loggedStrings.begin(); it != loggedStrings.end(); ++it) {
1459 failures.AppendElement(nsDependentCSubstring(std::get<1>(*it).c_str(),
1460 std::get<1>(*it).size()));
1461 indices.AppendElement(std::get<0>(*it));
1464 return NS_OK;
1467 nsTArray<GfxInfoCollectorBase*>* sCollectors;
1469 static void InitCollectors() {
1470 if (!sCollectors) sCollectors = new nsTArray<GfxInfoCollectorBase*>;
1473 nsresult GfxInfoBase::GetInfo(JSContext* aCx,
1474 JS::MutableHandle<JS::Value> aResult) {
1475 InitCollectors();
1476 InfoObject obj(aCx);
1478 for (uint32_t i = 0; i < sCollectors->Length(); i++) {
1479 (*sCollectors)[i]->GetInfo(obj);
1482 // Some example property definitions
1483 // obj.DefineProperty("wordCacheSize", gfxTextRunWordCache::Count());
1484 // obj.DefineProperty("renderer", mRendererIDsString);
1485 // obj.DefineProperty("five", 5);
1487 if (!obj.mOk) {
1488 return NS_ERROR_FAILURE;
1491 aResult.setObject(*obj.mObj);
1492 return NS_OK;
1495 nsAutoCString gBaseAppVersion;
1497 const nsCString& GfxInfoBase::GetApplicationVersion() {
1498 static bool versionInitialized = false;
1499 if (!versionInitialized) {
1500 // If we fail to get the version, we will not try again.
1501 versionInitialized = true;
1503 // Get the version from xpcom/system/nsIXULAppInfo.idl
1504 nsCOMPtr<nsIXULAppInfo> app = do_GetService("@mozilla.org/xre/app-info;1");
1505 if (app) {
1506 app->GetVersion(gBaseAppVersion);
1509 return gBaseAppVersion;
1512 void GfxInfoBase::AddCollector(GfxInfoCollectorBase* collector) {
1513 InitCollectors();
1514 sCollectors->AppendElement(collector);
1517 void GfxInfoBase::RemoveCollector(GfxInfoCollectorBase* collector) {
1518 InitCollectors();
1519 for (uint32_t i = 0; i < sCollectors->Length(); i++) {
1520 if ((*sCollectors)[i] == collector) {
1521 sCollectors->RemoveElementAt(i);
1522 break;
1525 if (sCollectors->IsEmpty()) {
1526 delete sCollectors;
1527 sCollectors = nullptr;
1531 static void AppendMonitor(JSContext* aCx, widget::Screen& aScreen,
1532 JS::Handle<JSObject*> aOutArray, int32_t aIndex) {
1533 JS::Rooted<JSObject*> obj(aCx, JS_NewPlainObject(aCx));
1535 auto screenSize = aScreen.GetRect().Size();
1537 JS::Rooted<JS::Value> screenWidth(aCx, JS::Int32Value(screenSize.width));
1538 JS_SetProperty(aCx, obj, "screenWidth", screenWidth);
1540 JS::Rooted<JS::Value> screenHeight(aCx, JS::Int32Value(screenSize.height));
1541 JS_SetProperty(aCx, obj, "screenHeight", screenHeight);
1543 // XXX Just preserving behavior since this is exposed to telemetry, but we
1544 // could consider including this everywhere.
1545 #ifdef XP_MACOSX
1546 JS::Rooted<JS::Value> scale(
1547 aCx, JS::NumberValue(aScreen.GetContentsScaleFactor()));
1548 JS_SetProperty(aCx, obj, "scale", scale);
1549 #endif
1551 #ifdef XP_WIN
1552 JS::Rooted<JS::Value> refreshRate(aCx,
1553 JS::Int32Value(aScreen.GetRefreshRate()));
1554 JS_SetProperty(aCx, obj, "refreshRate", refreshRate);
1556 JS::Rooted<JS::Value> pseudoDisplay(
1557 aCx, JS::BooleanValue(aScreen.GetIsPseudoDisplay()));
1558 JS_SetProperty(aCx, obj, "pseudoDisplay", pseudoDisplay);
1559 #endif
1561 JS::Rooted<JS::Value> element(aCx, JS::ObjectValue(*obj));
1562 JS_SetElement(aCx, aOutArray, aIndex, element);
1565 nsresult GfxInfoBase::FindMonitors(JSContext* aCx,
1566 JS::Handle<JSObject*> aOutArray) {
1567 int32_t index = 0;
1568 auto& sm = ScreenManager::GetSingleton();
1569 for (auto& screen : sm.CurrentScreenList()) {
1570 AppendMonitor(aCx, *screen, aOutArray, index++);
1573 if (index == 0) {
1574 // Ensure we return at least one monitor, this is needed for xpcshell.
1575 RefPtr<Screen> screen = sm.GetPrimaryScreen();
1576 AppendMonitor(aCx, *screen, aOutArray, index++);
1579 return NS_OK;
1582 NS_IMETHODIMP
1583 GfxInfoBase::GetMonitors(JSContext* aCx, JS::MutableHandle<JS::Value> aResult) {
1584 JS::Rooted<JSObject*> array(aCx, JS::NewArrayObject(aCx, 0));
1586 nsresult rv = FindMonitors(aCx, array);
1587 if (NS_FAILED(rv)) {
1588 return rv;
1591 aResult.setObject(*array);
1592 return NS_OK;
1595 static inline bool SetJSPropertyString(JSContext* aCx,
1596 JS::Handle<JSObject*> aObj,
1597 const char* aProp, const char* aString) {
1598 JS::Rooted<JSString*> str(aCx, JS_NewStringCopyZ(aCx, aString));
1599 if (!str) {
1600 return false;
1603 JS::Rooted<JS::Value> val(aCx, JS::StringValue(str));
1604 return JS_SetProperty(aCx, aObj, aProp, val);
1607 template <typename T>
1608 static inline bool AppendJSElement(JSContext* aCx, JS::Handle<JSObject*> aObj,
1609 const T& aValue) {
1610 uint32_t index;
1611 if (!JS::GetArrayLength(aCx, aObj, &index)) {
1612 return false;
1614 return JS_SetElement(aCx, aObj, index, aValue);
1617 nsresult GfxInfoBase::GetFeatures(JSContext* aCx,
1618 JS::MutableHandle<JS::Value> aOut) {
1619 JS::Rooted<JSObject*> obj(aCx, JS_NewPlainObject(aCx));
1620 if (!obj) {
1621 return NS_ERROR_OUT_OF_MEMORY;
1623 aOut.setObject(*obj);
1625 layers::LayersBackend backend =
1626 gfxPlatform::Initialized()
1627 ? gfxPlatform::GetPlatform()->GetCompositorBackend()
1628 : layers::LayersBackend::LAYERS_NONE;
1629 const char* backendName = layers::GetLayersBackendName(backend);
1630 SetJSPropertyString(aCx, obj, "compositor", backendName);
1632 // If graphics isn't initialized yet, just stop now.
1633 if (!gfxPlatform::Initialized()) {
1634 return NS_OK;
1637 DescribeFeatures(aCx, obj);
1638 return NS_OK;
1641 nsresult GfxInfoBase::GetFeatureLog(JSContext* aCx,
1642 JS::MutableHandle<JS::Value> aOut) {
1643 JS::Rooted<JSObject*> containerObj(aCx, JS_NewPlainObject(aCx));
1644 if (!containerObj) {
1645 return NS_ERROR_OUT_OF_MEMORY;
1647 aOut.setObject(*containerObj);
1649 JS::Rooted<JSObject*> featureArray(aCx, JS::NewArrayObject(aCx, 0));
1650 if (!featureArray) {
1651 return NS_ERROR_OUT_OF_MEMORY;
1654 // Collect features.
1655 gfxConfig::ForEachFeature([&](const char* aName, const char* aDescription,
1656 FeatureState& aFeature) -> void {
1657 JS::Rooted<JSObject*> obj(aCx, JS_NewPlainObject(aCx));
1658 if (!obj) {
1659 return;
1661 if (!SetJSPropertyString(aCx, obj, "name", aName) ||
1662 !SetJSPropertyString(aCx, obj, "description", aDescription) ||
1663 !SetJSPropertyString(aCx, obj, "status",
1664 FeatureStatusToString(aFeature.GetValue()))) {
1665 return;
1668 JS::Rooted<JS::Value> log(aCx);
1669 if (!BuildFeatureStateLog(aCx, aFeature, &log)) {
1670 return;
1672 if (!JS_SetProperty(aCx, obj, "log", log)) {
1673 return;
1676 if (!AppendJSElement(aCx, featureArray, obj)) {
1677 return;
1681 JS::Rooted<JSObject*> fallbackArray(aCx, JS::NewArrayObject(aCx, 0));
1682 if (!fallbackArray) {
1683 return NS_ERROR_OUT_OF_MEMORY;
1686 // Collect fallbacks.
1687 gfxConfig::ForEachFallback(
1688 [&](const char* aName, const char* aMessage) -> void {
1689 JS::Rooted<JSObject*> obj(aCx, JS_NewPlainObject(aCx));
1690 if (!obj) {
1691 return;
1694 if (!SetJSPropertyString(aCx, obj, "name", aName) ||
1695 !SetJSPropertyString(aCx, obj, "message", aMessage)) {
1696 return;
1699 if (!AppendJSElement(aCx, fallbackArray, obj)) {
1700 return;
1704 JS::Rooted<JS::Value> val(aCx);
1706 val = JS::ObjectValue(*featureArray);
1707 JS_SetProperty(aCx, containerObj, "features", val);
1709 val = JS::ObjectValue(*fallbackArray);
1710 JS_SetProperty(aCx, containerObj, "fallbacks", val);
1712 return NS_OK;
1715 bool GfxInfoBase::BuildFeatureStateLog(JSContext* aCx,
1716 const FeatureState& aFeature,
1717 JS::MutableHandle<JS::Value> aOut) {
1718 JS::Rooted<JSObject*> log(aCx, JS::NewArrayObject(aCx, 0));
1719 if (!log) {
1720 return false;
1722 aOut.setObject(*log);
1724 aFeature.ForEachStatusChange([&](const char* aType, FeatureStatus aStatus,
1725 const char* aMessage,
1726 const nsCString& aFailureId) -> void {
1727 JS::Rooted<JSObject*> obj(aCx, JS_NewPlainObject(aCx));
1728 if (!obj) {
1729 return;
1732 if (!SetJSPropertyString(aCx, obj, "type", aType) ||
1733 !SetJSPropertyString(aCx, obj, "status",
1734 FeatureStatusToString(aStatus)) ||
1735 (!aFailureId.IsEmpty() &&
1736 !SetJSPropertyString(aCx, obj, "failureId", aFailureId.get())) ||
1737 (aMessage && !SetJSPropertyString(aCx, obj, "message", aMessage))) {
1738 return;
1741 if (!AppendJSElement(aCx, log, obj)) {
1742 return;
1746 return true;
1749 void GfxInfoBase::DescribeFeatures(JSContext* aCx, JS::Handle<JSObject*> aObj) {
1750 JS::Rooted<JSObject*> obj(aCx);
1752 gfx::FeatureState& hwCompositing =
1753 gfxConfig::GetFeature(gfx::Feature::HW_COMPOSITING);
1754 InitFeatureObject(aCx, aObj, "hwCompositing", hwCompositing, &obj);
1756 gfx::FeatureState& gpuProcess =
1757 gfxConfig::GetFeature(gfx::Feature::GPU_PROCESS);
1758 InitFeatureObject(aCx, aObj, "gpuProcess", gpuProcess, &obj);
1760 gfx::FeatureState& webrender = gfxConfig::GetFeature(gfx::Feature::WEBRENDER);
1761 InitFeatureObject(aCx, aObj, "webrender", webrender, &obj);
1763 gfx::FeatureState& wrCompositor =
1764 gfxConfig::GetFeature(gfx::Feature::WEBRENDER_COMPOSITOR);
1765 InitFeatureObject(aCx, aObj, "wrCompositor", wrCompositor, &obj);
1767 gfx::FeatureState& openglCompositing =
1768 gfxConfig::GetFeature(gfx::Feature::OPENGL_COMPOSITING);
1769 InitFeatureObject(aCx, aObj, "openglCompositing", openglCompositing, &obj);
1771 gfx::FeatureState& omtp = gfxConfig::GetFeature(gfx::Feature::OMTP);
1772 InitFeatureObject(aCx, aObj, "omtp", omtp, &obj);
1775 bool GfxInfoBase::InitFeatureObject(JSContext* aCx,
1776 JS::Handle<JSObject*> aContainer,
1777 const char* aName,
1778 mozilla::gfx::FeatureState& aFeatureState,
1779 JS::MutableHandle<JSObject*> aOutObj) {
1780 JS::Rooted<JSObject*> obj(aCx, JS_NewPlainObject(aCx));
1781 if (!obj) {
1782 return false;
1785 nsCString status = aFeatureState.GetStatusAndFailureIdString();
1787 JS::Rooted<JSString*> str(aCx, JS_NewStringCopyZ(aCx, status.get()));
1788 JS::Rooted<JS::Value> val(aCx, JS::StringValue(str));
1789 JS_SetProperty(aCx, obj, "status", val);
1791 // Add the feature object to the container.
1793 JS::Rooted<JS::Value> val(aCx, JS::ObjectValue(*obj));
1794 JS_SetProperty(aCx, aContainer, aName, val);
1797 aOutObj.set(obj);
1798 return true;
1801 nsresult GfxInfoBase::GetActiveCrashGuards(JSContext* aCx,
1802 JS::MutableHandle<JS::Value> aOut) {
1803 JS::Rooted<JSObject*> array(aCx, JS::NewArrayObject(aCx, 0));
1804 if (!array) {
1805 return NS_ERROR_OUT_OF_MEMORY;
1807 aOut.setObject(*array);
1809 DriverCrashGuard::ForEachActiveCrashGuard(
1810 [&](const char* aName, const char* aPrefName) -> void {
1811 JS::Rooted<JSObject*> obj(aCx, JS_NewPlainObject(aCx));
1812 if (!obj) {
1813 return;
1815 if (!SetJSPropertyString(aCx, obj, "type", aName)) {
1816 return;
1818 if (!SetJSPropertyString(aCx, obj, "prefName", aPrefName)) {
1819 return;
1821 if (!AppendJSElement(aCx, array, obj)) {
1822 return;
1826 return NS_OK;
1829 NS_IMETHODIMP
1830 GfxInfoBase::GetTargetFrameRate(uint32_t* aTargetFrameRate) {
1831 *aTargetFrameRate = gfxPlatform::TargetFrameRate();
1832 return NS_OK;
1835 NS_IMETHODIMP
1836 GfxInfoBase::GetCodecSupportInfo(nsACString& aCodecSupportInfo) {
1837 aCodecSupportInfo.Assign(gfx::gfxVars::CodecSupportInfo());
1838 return NS_OK;
1841 NS_IMETHODIMP
1842 GfxInfoBase::GetIsHeadless(bool* aIsHeadless) {
1843 *aIsHeadless = gfxPlatform::IsHeadless();
1844 return NS_OK;
1847 NS_IMETHODIMP
1848 GfxInfoBase::GetContentBackend(nsAString& aContentBackend) {
1849 BackendType backend = gfxPlatform::GetPlatform()->GetDefaultContentBackend();
1850 nsString outStr;
1852 switch (backend) {
1853 case BackendType::DIRECT2D1_1: {
1854 outStr.AppendPrintf("Direct2D 1.1");
1855 break;
1857 case BackendType::SKIA: {
1858 outStr.AppendPrintf("Skia");
1859 break;
1861 case BackendType::CAIRO: {
1862 outStr.AppendPrintf("Cairo");
1863 break;
1865 default:
1866 return NS_ERROR_FAILURE;
1869 aContentBackend.Assign(outStr);
1870 return NS_OK;
1873 NS_IMETHODIMP
1874 GfxInfoBase::GetAzureCanvasBackend(nsAString& aBackend) {
1875 CopyASCIItoUTF16(mozilla::MakeStringSpan(
1876 gfxPlatform::GetPlatform()->GetAzureCanvasBackend()),
1877 aBackend);
1878 return NS_OK;
1881 NS_IMETHODIMP
1882 GfxInfoBase::GetAzureContentBackend(nsAString& aBackend) {
1883 CopyASCIItoUTF16(mozilla::MakeStringSpan(
1884 gfxPlatform::GetPlatform()->GetAzureContentBackend()),
1885 aBackend);
1886 return NS_OK;
1889 NS_IMETHODIMP
1890 GfxInfoBase::GetUsingGPUProcess(bool* aOutValue) {
1891 GPUProcessManager* gpu = GPUProcessManager::Get();
1892 if (!gpu) {
1893 // Not supported in content processes.
1894 return NS_ERROR_FAILURE;
1897 *aOutValue = !!gpu->GetGPUChild();
1898 return NS_OK;
1901 NS_IMETHODIMP_(int32_t)
1902 GfxInfoBase::GetMaxRefreshRate(bool* aMixed) {
1903 if (aMixed) {
1904 *aMixed = false;
1907 int32_t maxRefreshRate = 0;
1908 for (auto& screen : ScreenManager::GetSingleton().CurrentScreenList()) {
1909 int32_t refreshRate = screen->GetRefreshRate();
1910 if (aMixed && maxRefreshRate > 0 && maxRefreshRate != refreshRate) {
1911 *aMixed = true;
1913 maxRefreshRate = std::max(maxRefreshRate, refreshRate);
1916 return maxRefreshRate > 0 ? maxRefreshRate : -1;
1919 NS_IMETHODIMP
1920 GfxInfoBase::ControlGPUProcessForXPCShell(bool aEnable, bool* _retval) {
1921 gfxPlatform::GetPlatform();
1923 GPUProcessManager* gpm = GPUProcessManager::Get();
1924 if (aEnable) {
1925 if (!gfxConfig::IsEnabled(gfx::Feature::GPU_PROCESS)) {
1926 gfxConfig::UserForceEnable(gfx::Feature::GPU_PROCESS, "xpcshell-test");
1928 DebugOnly<nsresult> rv = gpm->EnsureGPUReady();
1929 MOZ_ASSERT(rv != NS_ERROR_ILLEGAL_DURING_SHUTDOWN);
1930 } else {
1931 gfxConfig::UserDisable(gfx::Feature::GPU_PROCESS, "xpcshell-test");
1932 gpm->KillProcess();
1935 *_retval = true;
1936 return NS_OK;
1939 NS_IMETHODIMP GfxInfoBase::KillGPUProcessForTests() {
1940 GPUProcessManager* gpm = GPUProcessManager::Get();
1941 if (!gpm) {
1942 // gfxPlatform has not been initialized.
1943 return NS_ERROR_NOT_INITIALIZED;
1946 gpm->KillProcess();
1947 return NS_OK;
1950 NS_IMETHODIMP GfxInfoBase::CrashGPUProcessForTests() {
1951 GPUProcessManager* gpm = GPUProcessManager::Get();
1952 if (!gpm) {
1953 // gfxPlatform has not been initialized.
1954 return NS_ERROR_NOT_INITIALIZED;
1957 gpm->CrashProcess();
1958 return NS_OK;
1961 GfxInfoCollectorBase::GfxInfoCollectorBase() {
1962 GfxInfoBase::AddCollector(this);
1965 GfxInfoCollectorBase::~GfxInfoCollectorBase() {
1966 GfxInfoBase::RemoveCollector(this);