Bug 1867925 - Mark some storage-access-api tests as intermittent after wpt-sync....
[gecko.git] / widget / GfxInfoBase.cpp
blobbe5f2d056d8985f1e5ec2a0ad49be4ee5c1b9433
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 case nsIGfxInfo::FEATURE_VIDEO_SOFTWARE_OVERLAY:
270 name = BLOCKLIST_PREF_BRANCH "video-software-overlay";
271 break;
272 case nsIGfxInfo::FEATURE_WEBGL_USE_HARDWARE:
273 name = BLOCKLIST_PREF_BRANCH "webgl-use-hardware";
274 break;
275 default:
276 MOZ_ASSERT_UNREACHABLE("Unexpected nsIGfxInfo feature?!");
277 break;
280 return name;
283 // Returns the value of the pref for the relevant feature in aValue.
284 // If the pref doesn't exist, aValue is not touched, and returns false.
285 static bool GetPrefValueForFeature(int32_t aFeature, int32_t& aValue,
286 nsACString& aFailureId) {
287 const char* prefname = GetPrefNameForFeature(aFeature);
288 if (!prefname) return false;
290 aValue = nsIGfxInfo::FEATURE_STATUS_UNKNOWN;
291 if (!NS_SUCCEEDED(Preferences::GetInt(prefname, &aValue))) {
292 return false;
295 if (aValue == nsIGfxInfo::FEATURE_DENIED) {
296 // We should never see the DENIED status with the downloadable blocklist.
297 return false;
300 nsCString failureprefname(prefname);
301 failureprefname += ".failureid";
302 nsAutoCString failureValue;
303 nsresult rv = Preferences::GetCString(failureprefname.get(), failureValue);
304 if (NS_SUCCEEDED(rv)) {
305 aFailureId = failureValue.get();
306 } else {
307 aFailureId = "FEATURE_FAILURE_BLOCKLIST_PREF";
310 return true;
313 static void SetPrefValueForFeature(int32_t aFeature, int32_t aValue,
314 const nsACString& aFailureId) {
315 const char* prefname = GetPrefNameForFeature(aFeature);
316 if (!prefname) return;
317 if (XRE_IsParentProcess()) {
318 GfxInfoBase::sFeatureStatus = nullptr;
321 Preferences::SetInt(prefname, aValue);
322 if (!aFailureId.IsEmpty()) {
323 nsAutoCString failureprefname(prefname);
324 failureprefname += ".failureid";
325 Preferences::SetCString(failureprefname.get(), aFailureId);
329 static void RemovePrefForFeature(int32_t aFeature) {
330 const char* prefname = GetPrefNameForFeature(aFeature);
331 if (!prefname) return;
333 if (XRE_IsParentProcess()) {
334 GfxInfoBase::sFeatureStatus = nullptr;
336 Preferences::ClearUser(prefname);
339 static bool GetPrefValueForDriverVersion(nsCString& aVersion) {
340 return NS_SUCCEEDED(
341 Preferences::GetCString(SUGGESTED_VERSION_PREF, aVersion));
344 static void SetPrefValueForDriverVersion(const nsAString& aVersion) {
345 Preferences::SetString(SUGGESTED_VERSION_PREF, aVersion);
348 static void RemovePrefForDriverVersion() {
349 Preferences::ClearUser(SUGGESTED_VERSION_PREF);
352 static OperatingSystem BlocklistOSToOperatingSystem(const nsAString& os) {
353 if (os.EqualsLiteral("WINNT 6.1")) {
354 return OperatingSystem::Windows7;
356 if (os.EqualsLiteral("WINNT 6.2")) {
357 return OperatingSystem::Windows8;
359 if (os.EqualsLiteral("WINNT 6.3")) {
360 return OperatingSystem::Windows8_1;
362 if (os.EqualsLiteral("WINNT 10.0")) {
363 return OperatingSystem::Windows10;
365 if (os.EqualsLiteral("Linux")) {
366 return OperatingSystem::Linux;
368 if (os.EqualsLiteral("Darwin 9")) {
369 return OperatingSystem::OSX10_5;
371 if (os.EqualsLiteral("Darwin 10")) {
372 return OperatingSystem::OSX10_6;
374 if (os.EqualsLiteral("Darwin 11")) {
375 return OperatingSystem::OSX10_7;
377 if (os.EqualsLiteral("Darwin 12")) {
378 return OperatingSystem::OSX10_8;
380 if (os.EqualsLiteral("Darwin 13")) {
381 return OperatingSystem::OSX10_9;
383 if (os.EqualsLiteral("Darwin 14")) {
384 return OperatingSystem::OSX10_10;
386 if (os.EqualsLiteral("Darwin 15")) {
387 return OperatingSystem::OSX10_11;
389 if (os.EqualsLiteral("Darwin 16")) {
390 return OperatingSystem::OSX10_12;
392 if (os.EqualsLiteral("Darwin 17")) {
393 return OperatingSystem::OSX10_13;
395 if (os.EqualsLiteral("Darwin 18")) {
396 return OperatingSystem::OSX10_14;
398 if (os.EqualsLiteral("Darwin 19")) {
399 return OperatingSystem::OSX10_15;
401 if (os.EqualsLiteral("Darwin 20")) {
402 return OperatingSystem::OSX11_0;
404 if (os.EqualsLiteral("Android")) {
405 return OperatingSystem::Android;
406 // For historical reasons, "All" in blocklist means "All Windows"
408 if (os.EqualsLiteral("All")) {
409 return OperatingSystem::Windows;
411 if (os.EqualsLiteral("Darwin")) {
412 return OperatingSystem::OSX;
415 return OperatingSystem::Unknown;
418 static GfxDeviceFamily* BlocklistDevicesToDeviceFamily(
419 nsTArray<nsCString>& devices) {
420 if (devices.Length() == 0) return nullptr;
422 // For each device, get its device ID, and return a freshly-allocated
423 // GfxDeviceFamily with the contents of that array.
424 GfxDeviceFamily* deviceIds = new GfxDeviceFamily;
426 for (uint32_t i = 0; i < devices.Length(); ++i) {
427 // We make sure we don't add any "empty" device entries to the array, so
428 // we don't need to check if devices[i] is empty.
429 deviceIds->Append(NS_ConvertUTF8toUTF16(devices[i]));
432 return deviceIds;
435 static int32_t BlocklistFeatureToGfxFeature(const nsAString& aFeature) {
436 MOZ_ASSERT(!aFeature.IsEmpty());
437 if (aFeature.EqualsLiteral("DIRECT2D")) {
438 return nsIGfxInfo::FEATURE_DIRECT2D;
440 if (aFeature.EqualsLiteral("DIRECT3D_9_LAYERS")) {
441 return nsIGfxInfo::FEATURE_DIRECT3D_9_LAYERS;
443 if (aFeature.EqualsLiteral("DIRECT3D_10_LAYERS")) {
444 return nsIGfxInfo::FEATURE_DIRECT3D_10_LAYERS;
446 if (aFeature.EqualsLiteral("DIRECT3D_10_1_LAYERS")) {
447 return nsIGfxInfo::FEATURE_DIRECT3D_10_1_LAYERS;
449 if (aFeature.EqualsLiteral("DIRECT3D_11_LAYERS")) {
450 return nsIGfxInfo::FEATURE_DIRECT3D_11_LAYERS;
452 if (aFeature.EqualsLiteral("DIRECT3D_11_ANGLE")) {
453 return nsIGfxInfo::FEATURE_DIRECT3D_11_ANGLE;
455 if (aFeature.EqualsLiteral("HARDWARE_VIDEO_DECODING")) {
456 return nsIGfxInfo::FEATURE_HARDWARE_VIDEO_DECODING;
458 if (aFeature.EqualsLiteral("OPENGL_LAYERS")) {
459 return nsIGfxInfo::FEATURE_OPENGL_LAYERS;
461 if (aFeature.EqualsLiteral("WEBGL_OPENGL")) {
462 return nsIGfxInfo::FEATURE_WEBGL_OPENGL;
464 if (aFeature.EqualsLiteral("WEBGL_ANGLE")) {
465 return nsIGfxInfo::FEATURE_WEBGL_ANGLE;
467 if (aFeature.EqualsLiteral("WEBGL_MSAA")) {
468 return nsIGfxInfo::UNUSED_FEATURE_WEBGL_MSAA;
470 if (aFeature.EqualsLiteral("STAGEFRIGHT")) {
471 return nsIGfxInfo::FEATURE_STAGEFRIGHT;
473 if (aFeature.EqualsLiteral("WEBRTC_HW_ACCELERATION_ENCODE")) {
474 return nsIGfxInfo::FEATURE_WEBRTC_HW_ACCELERATION_ENCODE;
476 if (aFeature.EqualsLiteral("WEBRTC_HW_ACCELERATION_DECODE")) {
477 return nsIGfxInfo::FEATURE_WEBRTC_HW_ACCELERATION_DECODE;
479 if (aFeature.EqualsLiteral("WEBRTC_HW_ACCELERATION_H264")) {
480 return nsIGfxInfo::FEATURE_WEBRTC_HW_ACCELERATION_H264;
482 if (aFeature.EqualsLiteral("CANVAS2D_ACCELERATION")) {
483 return nsIGfxInfo::FEATURE_CANVAS2D_ACCELERATION;
485 if (aFeature.EqualsLiteral("DX_INTEROP2")) {
486 return nsIGfxInfo::FEATURE_DX_INTEROP2;
488 if (aFeature.EqualsLiteral("GPU_PROCESS")) {
489 return nsIGfxInfo::FEATURE_GPU_PROCESS;
491 if (aFeature.EqualsLiteral("WEBGL2")) {
492 return nsIGfxInfo::FEATURE_WEBGL2;
494 if (aFeature.EqualsLiteral("D3D11_KEYED_MUTEX")) {
495 return nsIGfxInfo::FEATURE_D3D11_KEYED_MUTEX;
497 if (aFeature.EqualsLiteral("WEBRENDER")) {
498 return nsIGfxInfo::FEATURE_WEBRENDER;
500 if (aFeature.EqualsLiteral("WEBRENDER_COMPOSITOR")) {
501 return nsIGfxInfo::FEATURE_WEBRENDER_COMPOSITOR;
503 if (aFeature.EqualsLiteral("DX_NV12")) {
504 return nsIGfxInfo::FEATURE_DX_NV12;
506 if (aFeature.EqualsLiteral("VP8_HW_DECODE")) {
507 return nsIGfxInfo::FEATURE_VP8_HW_DECODE;
509 if (aFeature.EqualsLiteral("VP9_HW_DECODE")) {
510 return nsIGfxInfo::FEATURE_VP9_HW_DECODE;
512 if (aFeature.EqualsLiteral("GL_SWIZZLE")) {
513 return nsIGfxInfo::FEATURE_GL_SWIZZLE;
515 if (aFeature.EqualsLiteral("WEBRENDER_SCISSORED_CACHE_CLEARS")) {
516 return nsIGfxInfo::FEATURE_WEBRENDER_SCISSORED_CACHE_CLEARS;
518 if (aFeature.EqualsLiteral("ALLOW_WEBGL_OUT_OF_PROCESS")) {
519 return nsIGfxInfo::FEATURE_ALLOW_WEBGL_OUT_OF_PROCESS;
521 if (aFeature.EqualsLiteral("THREADSAFE_GL")) {
522 return nsIGfxInfo::FEATURE_THREADSAFE_GL;
524 if (aFeature.EqualsLiteral("X11_EGL")) {
525 return nsIGfxInfo::FEATURE_X11_EGL;
527 if (aFeature.EqualsLiteral("DMABUF")) {
528 return nsIGfxInfo::FEATURE_DMABUF;
530 if (aFeature.EqualsLiteral("WEBGPU")) {
531 return nsIGfxInfo::FEATURE_WEBGPU;
533 if (aFeature.EqualsLiteral("VIDEO_OVERLAY")) {
534 return nsIGfxInfo::FEATURE_VIDEO_OVERLAY;
536 if (aFeature.EqualsLiteral("HW_DECODED_VIDEO_ZERO_COPY")) {
537 return nsIGfxInfo::FEATURE_HW_DECODED_VIDEO_ZERO_COPY;
539 if (aFeature.EqualsLiteral("REUSE_DECODER_DEVICE")) {
540 return nsIGfxInfo::FEATURE_REUSE_DECODER_DEVICE;
542 if (aFeature.EqualsLiteral("WEBRENDER_PARTIAL_PRESENT")) {
543 return nsIGfxInfo::FEATURE_WEBRENDER_PARTIAL_PRESENT;
545 if (aFeature.EqualsLiteral("BACKDROP_FILTER")) {
546 return nsIGfxInfo::FEATURE_BACKDROP_FILTER;
548 if (aFeature.EqualsLiteral("ACCELERATED_CANVAS2D")) {
549 return nsIGfxInfo::FEATURE_ACCELERATED_CANVAS2D;
552 // If we don't recognize the feature, it may be new, and something
553 // this version doesn't understand. So, nothing to do. This is
554 // different from feature not being specified at all, in which case
555 // this method should not get called and we should continue with the
556 // "all features" blocklisting.
557 return -1;
560 static int32_t BlocklistFeatureStatusToGfxFeatureStatus(
561 const nsAString& aStatus) {
562 if (aStatus.EqualsLiteral("STATUS_OK")) {
563 return nsIGfxInfo::FEATURE_STATUS_OK;
565 if (aStatus.EqualsLiteral("BLOCKED_DRIVER_VERSION")) {
566 return nsIGfxInfo::FEATURE_BLOCKED_DRIVER_VERSION;
568 if (aStatus.EqualsLiteral("BLOCKED_DEVICE")) {
569 return nsIGfxInfo::FEATURE_BLOCKED_DEVICE;
571 if (aStatus.EqualsLiteral("DISCOURAGED")) {
572 return nsIGfxInfo::FEATURE_DISCOURAGED;
574 if (aStatus.EqualsLiteral("BLOCKED_OS_VERSION")) {
575 return nsIGfxInfo::FEATURE_BLOCKED_OS_VERSION;
577 if (aStatus.EqualsLiteral("DENIED")) {
578 return nsIGfxInfo::FEATURE_DENIED;
580 if (aStatus.EqualsLiteral("ALLOW_QUALIFIED")) {
581 return nsIGfxInfo::FEATURE_ALLOW_QUALIFIED;
583 if (aStatus.EqualsLiteral("ALLOW_ALWAYS")) {
584 return nsIGfxInfo::FEATURE_ALLOW_ALWAYS;
587 // Do not allow it to set STATUS_UNKNOWN. Also, we are not
588 // expecting the "mismatch" status showing up here.
590 return nsIGfxInfo::FEATURE_STATUS_OK;
593 static VersionComparisonOp BlocklistComparatorToComparisonOp(
594 const nsAString& op) {
595 if (op.EqualsLiteral("LESS_THAN")) {
596 return DRIVER_LESS_THAN;
598 if (op.EqualsLiteral("BUILD_ID_LESS_THAN")) {
599 return DRIVER_BUILD_ID_LESS_THAN;
601 if (op.EqualsLiteral("LESS_THAN_OR_EQUAL")) {
602 return DRIVER_LESS_THAN_OR_EQUAL;
604 if (op.EqualsLiteral("BUILD_ID_LESS_THAN_OR_EQUAL")) {
605 return DRIVER_BUILD_ID_LESS_THAN_OR_EQUAL;
607 if (op.EqualsLiteral("GREATER_THAN")) {
608 return DRIVER_GREATER_THAN;
610 if (op.EqualsLiteral("GREATER_THAN_OR_EQUAL")) {
611 return DRIVER_GREATER_THAN_OR_EQUAL;
613 if (op.EqualsLiteral("EQUAL")) {
614 return DRIVER_EQUAL;
616 if (op.EqualsLiteral("NOT_EQUAL")) {
617 return DRIVER_NOT_EQUAL;
619 if (op.EqualsLiteral("BETWEEN_EXCLUSIVE")) {
620 return DRIVER_BETWEEN_EXCLUSIVE;
622 if (op.EqualsLiteral("BETWEEN_INCLUSIVE")) {
623 return DRIVER_BETWEEN_INCLUSIVE;
625 if (op.EqualsLiteral("BETWEEN_INCLUSIVE_START")) {
626 return DRIVER_BETWEEN_INCLUSIVE_START;
629 return DRIVER_COMPARISON_IGNORED;
633 Deserialize Blocklist entries from string.
634 e.g:
635 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
637 static bool BlocklistEntryToDriverInfo(const nsACString& aBlocklistEntry,
638 GfxDriverInfo& aDriverInfo) {
639 // If we get an application version to be zero, something is not working
640 // and we are not going to bother checking the blocklist versions.
641 // See TestGfxWidgets.cpp for how version comparison works.
642 // <versionRange minVersion="42.0a1" maxVersion="45.0"></versionRange>
643 static mozilla::Version zeroV("0");
644 static mozilla::Version appV(GfxInfoBase::GetApplicationVersion().get());
645 if (appV <= zeroV) {
646 gfxCriticalErrorOnce(gfxCriticalError::DefaultOptions(false))
647 << "Invalid application version "
648 << GfxInfoBase::GetApplicationVersion().get();
651 aDriverInfo.mRuleId = "FEATURE_FAILURE_DL_BLOCKLIST_NO_ID"_ns;
653 for (const auto& keyValue : aBlocklistEntry.Split('\t')) {
654 nsTArray<nsCString> splitted;
655 ParseString(keyValue, ':', splitted);
656 if (splitted.Length() != 2) {
657 // If we don't recognize the input data, we do not want to proceed.
658 gfxCriticalErrorOnce(CriticalLog::DefaultOptions(false))
659 << "Unrecognized data " << nsCString(keyValue).get();
660 return false;
662 const nsCString& key = splitted[0];
663 const nsCString& value = splitted[1];
664 NS_ConvertUTF8toUTF16 dataValue(value);
666 if (value.Length() == 0) {
667 // Safety check for empty values.
668 gfxCriticalErrorOnce(CriticalLog::DefaultOptions(false))
669 << "Empty value for " << key.get();
670 return false;
673 if (key.EqualsLiteral("blockID")) {
674 nsCString blockIdStr = "FEATURE_FAILURE_DL_BLOCKLIST_"_ns + value;
675 aDriverInfo.mRuleId = blockIdStr.get();
676 } else if (key.EqualsLiteral("os")) {
677 aDriverInfo.mOperatingSystem = BlocklistOSToOperatingSystem(dataValue);
678 } else if (key.EqualsLiteral("osversion")) {
679 aDriverInfo.mOperatingSystemVersion = strtoul(value.get(), nullptr, 10);
680 } else if (key.EqualsLiteral("windowProtocol")) {
681 aDriverInfo.mWindowProtocol = dataValue;
682 } else if (key.EqualsLiteral("vendor")) {
683 aDriverInfo.mAdapterVendor = dataValue;
684 } else if (key.EqualsLiteral("driverVendor")) {
685 aDriverInfo.mDriverVendor = dataValue;
686 } else if (key.EqualsLiteral("feature")) {
687 aDriverInfo.mFeature = BlocklistFeatureToGfxFeature(dataValue);
688 if (aDriverInfo.mFeature < 0) {
689 // If we don't recognize the feature, we do not want to proceed.
690 gfxWarning() << "Unrecognized feature " << value.get();
691 return false;
693 } else if (key.EqualsLiteral("featureStatus")) {
694 aDriverInfo.mFeatureStatus =
695 BlocklistFeatureStatusToGfxFeatureStatus(dataValue);
696 } else if (key.EqualsLiteral("driverVersion")) {
697 uint64_t version;
698 if (ParseDriverVersion(dataValue, &version))
699 aDriverInfo.mDriverVersion = version;
700 } else if (key.EqualsLiteral("driverVersionMax")) {
701 uint64_t version;
702 if (ParseDriverVersion(dataValue, &version))
703 aDriverInfo.mDriverVersionMax = version;
704 } else if (key.EqualsLiteral("driverVersionComparator")) {
705 aDriverInfo.mComparisonOp = BlocklistComparatorToComparisonOp(dataValue);
706 } else if (key.EqualsLiteral("model")) {
707 aDriverInfo.mModel = dataValue;
708 } else if (key.EqualsLiteral("product")) {
709 aDriverInfo.mProduct = dataValue;
710 } else if (key.EqualsLiteral("manufacturer")) {
711 aDriverInfo.mManufacturer = dataValue;
712 } else if (key.EqualsLiteral("hardware")) {
713 aDriverInfo.mHardware = dataValue;
714 } else if (key.EqualsLiteral("versionRange")) {
715 nsTArray<nsCString> versionRange;
716 ParseString(value, ',', versionRange);
717 if (versionRange.Length() != 2) {
718 gfxCriticalErrorOnce(CriticalLog::DefaultOptions(false))
719 << "Unrecognized versionRange " << value.get();
720 return false;
722 const nsCString& minValue = versionRange[0];
723 const nsCString& maxValue = versionRange[1];
725 mozilla::Version minV(minValue.get());
726 mozilla::Version maxV(maxValue.get());
728 if (minV > zeroV && !(appV >= minV)) {
729 // The version of the application is less than the minimal 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 if (maxV > zeroV && !(appV <= maxV)) {
735 // The version of the application is more than the maximal version
736 // this blocklist entry applies to, so we can just ignore it by
737 // returning false and letting the caller deal with it.
738 return false;
740 } else if (key.EqualsLiteral("devices")) {
741 nsTArray<nsCString> devices;
742 ParseString(value, ',', devices);
743 GfxDeviceFamily* deviceIds = BlocklistDevicesToDeviceFamily(devices);
744 if (deviceIds) {
745 // Get GfxDriverInfo to adopt the devices array we created.
746 aDriverInfo.mDeleteDevices = true;
747 aDriverInfo.mDevices = deviceIds;
750 // We explicitly ignore unknown elements.
753 return true;
756 NS_IMETHODIMP
757 GfxInfoBase::Observe(nsISupports* aSubject, const char* aTopic,
758 const char16_t* aData) {
759 if (strcmp(aTopic, "blocklist-data-gfxItems") == 0) {
760 nsTArray<GfxDriverInfo> driverInfo;
761 NS_ConvertUTF16toUTF8 utf8Data(aData);
763 for (const auto& blocklistEntry : utf8Data.Split('\n')) {
764 GfxDriverInfo di;
765 if (BlocklistEntryToDriverInfo(blocklistEntry, di)) {
766 // XXX Changing this to driverInfo.AppendElement(di) causes leaks.
767 // Probably some non-standard semantics of the copy/move operations?
768 *driverInfo.AppendElement() = di;
769 // Prevent di falling out of scope from destroying the devices.
770 di.mDeleteDevices = false;
771 } else {
772 driverInfo.AppendElement();
776 EvaluateDownloadedBlocklist(driverInfo);
779 return NS_OK;
782 GfxInfoBase::GfxInfoBase() : mScreenPixels(INT64_MAX), mMutex("GfxInfoBase") {}
784 GfxInfoBase::~GfxInfoBase() = default;
786 nsresult GfxInfoBase::Init() {
787 InitGfxDriverInfoShutdownObserver();
789 nsCOMPtr<nsIObserverService> os = mozilla::services::GetObserverService();
790 if (os) {
791 os->AddObserver(this, "blocklist-data-gfxItems", true);
794 return NS_OK;
797 void GfxInfoBase::GetData() {
798 if (mScreenPixels != INT64_MAX) {
799 // Already initialized.
800 return;
803 ScreenManager::GetSingleton().GetTotalScreenPixels(&mScreenPixels);
806 NS_IMETHODIMP
807 GfxInfoBase::GetFeatureStatus(int32_t aFeature, nsACString& aFailureId,
808 int32_t* aStatus) {
809 // Ignore the gfx.blocklist.all pref on release and beta.
810 #if defined(RELEASE_OR_BETA)
811 int32_t blocklistAll = 0;
812 #else
813 int32_t blocklistAll = StaticPrefs::gfx_blocklist_all_AtStartup();
814 #endif
815 if (blocklistAll > 0) {
816 gfxCriticalErrorOnce(gfxCriticalError::DefaultOptions(false))
817 << "Forcing blocklisting all features";
818 *aStatus = FEATURE_BLOCKED_DEVICE;
819 aFailureId = "FEATURE_FAILURE_BLOCK_ALL";
820 return NS_OK;
823 if (blocklistAll < 0) {
824 gfxCriticalErrorOnce(gfxCriticalError::DefaultOptions(false))
825 << "Ignoring any feature blocklisting.";
826 *aStatus = FEATURE_STATUS_OK;
827 return NS_OK;
830 // This is how we evaluate the downloadable blocklist. If there is no pref,
831 // then we will fallback to checking the static blocklist.
832 if (GetPrefValueForFeature(aFeature, *aStatus, aFailureId)) {
833 return NS_OK;
836 if (XRE_IsContentProcess() || XRE_IsGPUProcess()) {
837 // Use the cached data received from the parent process.
838 MOZ_ASSERT(sFeatureStatus);
839 bool success = false;
840 for (const auto& fs : *sFeatureStatus) {
841 if (fs.feature() == aFeature) {
842 aFailureId = fs.failureId();
843 *aStatus = fs.status();
844 success = true;
845 break;
848 return success ? NS_OK : NS_ERROR_FAILURE;
851 nsString version;
852 nsTArray<GfxDriverInfo> driverInfo;
853 nsresult rv =
854 GetFeatureStatusImpl(aFeature, aStatus, version, driverInfo, aFailureId);
855 return rv;
858 nsTArray<gfx::GfxInfoFeatureStatus> GfxInfoBase::GetAllFeatures() {
859 MOZ_RELEASE_ASSERT(XRE_IsParentProcess());
860 if (!sFeatureStatus) {
861 InitFeatureStatus(new nsTArray<gfx::GfxInfoFeatureStatus>());
862 for (int32_t i = 1; i <= nsIGfxInfo::FEATURE_MAX_VALUE; ++i) {
863 int32_t status = 0;
864 nsAutoCString failureId;
865 GetFeatureStatus(i, failureId, &status);
866 gfx::GfxInfoFeatureStatus gfxFeatureStatus;
867 gfxFeatureStatus.feature() = i;
868 gfxFeatureStatus.status() = status;
869 gfxFeatureStatus.failureId() = failureId;
870 sFeatureStatus->AppendElement(gfxFeatureStatus);
874 nsTArray<gfx::GfxInfoFeatureStatus> features;
875 for (const auto& status : *sFeatureStatus) {
876 gfx::GfxInfoFeatureStatus copy = status;
877 features.AppendElement(copy);
879 return features;
882 inline bool MatchingAllowStatus(int32_t aStatus) {
883 switch (aStatus) {
884 case nsIGfxInfo::FEATURE_ALLOW_ALWAYS:
885 case nsIGfxInfo::FEATURE_ALLOW_QUALIFIED:
886 return true;
887 default:
888 return false;
892 // Matching OS go somewhat beyond the simple equality check because of the
893 // "All Windows" and "All OS X" variations.
895 // aBlockedOS is describing the system(s) we are trying to block.
896 // aSystemOS is describing the system we are running on.
898 // aSystemOS should not be "Windows" or "OSX" - it should be set to
899 // a particular version instead.
900 // However, it is valid for aBlockedOS to be one of those generic values,
901 // as we could be blocking all of the versions.
902 inline bool MatchingOperatingSystems(OperatingSystem aBlockedOS,
903 OperatingSystem aSystemOS,
904 uint32_t aSystemOSBuild) {
905 MOZ_ASSERT(aSystemOS != OperatingSystem::Windows &&
906 aSystemOS != OperatingSystem::OSX);
908 // If the block entry OS is unknown, it doesn't match
909 if (aBlockedOS == OperatingSystem::Unknown) {
910 return false;
913 #if defined(XP_WIN)
914 if (aBlockedOS == OperatingSystem::Windows) {
915 // We do want even "unknown" aSystemOS to fall under "all windows"
916 return true;
919 constexpr uint32_t kMinWin10BuildNumber = 18362;
920 if (aBlockedOS == OperatingSystem::RecentWindows10 &&
921 aSystemOS == OperatingSystem::Windows10) {
922 // For allowlist purposes, we sometimes want to restrict to only recent
923 // versions of Windows 10. This is a bit of a kludge but easier than adding
924 // complicated blocklist infrastructure for build ID comparisons like driver
925 // versions.
926 return aSystemOSBuild >= kMinWin10BuildNumber;
929 if (aBlockedOS == OperatingSystem::NotRecentWindows10) {
930 if (aSystemOS == OperatingSystem::Windows10) {
931 return aSystemOSBuild < kMinWin10BuildNumber;
932 } else {
933 return true;
936 #endif
938 #if defined(XP_MACOSX)
939 if (aBlockedOS == OperatingSystem::OSX) {
940 // We do want even "unknown" aSystemOS to fall under "all OS X"
941 return true;
943 #endif
945 return aSystemOS == aBlockedOS;
948 inline bool MatchingBattery(BatteryStatus aBatteryStatus, bool aHasBattery) {
949 switch (aBatteryStatus) {
950 case BatteryStatus::All:
951 return true;
952 case BatteryStatus::None:
953 return !aHasBattery;
954 case BatteryStatus::Present:
955 return aHasBattery;
958 MOZ_ASSERT_UNREACHABLE("bad battery status");
959 return false;
962 inline bool MatchingScreenSize(ScreenSizeStatus aScreenStatus,
963 int64_t aScreenPixels) {
964 constexpr int64_t kMaxSmallPixels = 2304000; // 1920x1200
965 constexpr int64_t kMaxMediumPixels = 4953600; // 3440x1440
967 switch (aScreenStatus) {
968 case ScreenSizeStatus::All:
969 return true;
970 case ScreenSizeStatus::Small:
971 return aScreenPixels <= kMaxSmallPixels;
972 case ScreenSizeStatus::SmallAndMedium:
973 return aScreenPixels <= kMaxMediumPixels;
974 case ScreenSizeStatus::Medium:
975 return aScreenPixels > kMaxSmallPixels &&
976 aScreenPixels <= kMaxMediumPixels;
977 case ScreenSizeStatus::MediumAndLarge:
978 return aScreenPixels > kMaxSmallPixels;
979 case ScreenSizeStatus::Large:
980 return aScreenPixels > kMaxMediumPixels;
983 MOZ_ASSERT_UNREACHABLE("bad screen status");
984 return false;
987 int32_t GfxInfoBase::FindBlocklistedDeviceInList(
988 const nsTArray<GfxDriverInfo>& info, nsAString& aSuggestedVersion,
989 int32_t aFeature, nsACString& aFailureId, OperatingSystem os,
990 bool aForAllowing) {
991 int32_t status = nsIGfxInfo::FEATURE_STATUS_UNKNOWN;
993 // Some properties are not available on all platforms.
994 nsAutoString windowProtocol;
995 nsresult rv = GetWindowProtocol(windowProtocol);
996 if (NS_FAILED(rv) && rv != NS_ERROR_NOT_IMPLEMENTED) {
997 return 0;
1000 bool hasBattery = false;
1001 rv = GetHasBattery(&hasBattery);
1002 if (NS_FAILED(rv) && rv != NS_ERROR_NOT_IMPLEMENTED) {
1003 return 0;
1006 uint32_t osBuild = OperatingSystemBuild();
1008 // Get the adapters once then reuse below
1009 nsAutoString adapterVendorID[2];
1010 nsAutoString adapterDeviceID[2];
1011 nsAutoString adapterDriverVendor[2];
1012 nsAutoString adapterDriverVersionString[2];
1013 bool adapterInfoFailed[2];
1015 adapterInfoFailed[0] =
1016 (NS_FAILED(GetAdapterVendorID(adapterVendorID[0])) ||
1017 NS_FAILED(GetAdapterDeviceID(adapterDeviceID[0])) ||
1018 NS_FAILED(GetAdapterDriverVendor(adapterDriverVendor[0])) ||
1019 NS_FAILED(GetAdapterDriverVersion(adapterDriverVersionString[0])));
1020 adapterInfoFailed[1] =
1021 (NS_FAILED(GetAdapterVendorID2(adapterVendorID[1])) ||
1022 NS_FAILED(GetAdapterDeviceID2(adapterDeviceID[1])) ||
1023 NS_FAILED(GetAdapterDriverVendor2(adapterDriverVendor[1])) ||
1024 NS_FAILED(GetAdapterDriverVersion2(adapterDriverVersionString[1])));
1025 // No point in going on if we don't have adapter info
1026 if (adapterInfoFailed[0] && adapterInfoFailed[1]) {
1027 return 0;
1030 #if defined(XP_WIN) || defined(ANDROID) || defined(MOZ_WIDGET_GTK)
1031 uint64_t driverVersion[2] = {0, 0};
1032 if (!adapterInfoFailed[0]) {
1033 ParseDriverVersion(adapterDriverVersionString[0], &driverVersion[0]);
1035 if (!adapterInfoFailed[1]) {
1036 ParseDriverVersion(adapterDriverVersionString[1], &driverVersion[1]);
1038 #endif
1040 uint32_t i = 0;
1041 for (; i < info.Length(); i++) {
1042 // If the status is FEATURE_ALLOW_*, then it is for the allowlist, not
1043 // blocklisting. Only consider entries for our search mode.
1044 if (MatchingAllowStatus(info[i].mFeatureStatus) != aForAllowing) {
1045 continue;
1048 // If we don't have the info for this GPU, no need to check further.
1049 // It is unclear that we would ever have a mixture of 1st and 2nd
1050 // GPU, but leaving the code in for that possibility for now.
1051 // (Actually, currently mGpu2 will never be true, so this can
1052 // be optimized out.)
1053 uint32_t infoIndex = info[i].mGpu2 ? 1 : 0;
1054 if (adapterInfoFailed[infoIndex]) {
1055 continue;
1058 // Do the operating system check first, no point in getting the driver
1059 // info if we won't need to use it.
1060 if (!MatchingOperatingSystems(info[i].mOperatingSystem, os, osBuild)) {
1061 continue;
1064 if (info[i].mOperatingSystemVersion &&
1065 info[i].mOperatingSystemVersion != OperatingSystemVersion()) {
1066 continue;
1069 if (!MatchingBattery(info[i].mBattery, hasBattery)) {
1070 continue;
1073 if (!MatchingScreenSize(info[i].mScreen, mScreenPixels)) {
1074 continue;
1077 if (!DoesWindowProtocolMatch(info[i].mWindowProtocol, windowProtocol)) {
1078 continue;
1081 if (!DoesVendorMatch(info[i].mAdapterVendor, adapterVendorID[infoIndex])) {
1082 continue;
1085 if (!DoesDriverVendorMatch(info[i].mDriverVendor,
1086 adapterDriverVendor[infoIndex])) {
1087 continue;
1090 if (info[i].mDevices && !info[i].mDevices->IsEmpty()) {
1091 nsresult rv = info[i].mDevices->Contains(adapterDeviceID[infoIndex]);
1092 if (rv == NS_ERROR_NOT_AVAILABLE) {
1093 // Not found
1094 continue;
1096 if (rv != NS_OK) {
1097 // Failed to search, allowlist should not match, blocklist should match
1098 // for safety reasons
1099 if (aForAllowing) {
1100 continue;
1102 break;
1106 bool match = false;
1108 if (!info[i].mHardware.IsEmpty() && !info[i].mHardware.Equals(Hardware())) {
1109 continue;
1111 if (!info[i].mModel.IsEmpty() && !info[i].mModel.Equals(Model())) {
1112 continue;
1114 if (!info[i].mProduct.IsEmpty() && !info[i].mProduct.Equals(Product())) {
1115 continue;
1117 if (!info[i].mManufacturer.IsEmpty() &&
1118 !info[i].mManufacturer.Equals(Manufacturer())) {
1119 continue;
1122 #if defined(XP_WIN) || defined(ANDROID) || defined(MOZ_WIDGET_GTK)
1123 switch (info[i].mComparisonOp) {
1124 case DRIVER_LESS_THAN:
1125 match = driverVersion[infoIndex] < info[i].mDriverVersion;
1126 break;
1127 case DRIVER_BUILD_ID_LESS_THAN:
1128 match = (driverVersion[infoIndex] & 0xFFFF) < info[i].mDriverVersion;
1129 break;
1130 case DRIVER_LESS_THAN_OR_EQUAL:
1131 match = driverVersion[infoIndex] <= info[i].mDriverVersion;
1132 break;
1133 case DRIVER_BUILD_ID_LESS_THAN_OR_EQUAL:
1134 match = (driverVersion[infoIndex] & 0xFFFF) <= info[i].mDriverVersion;
1135 break;
1136 case DRIVER_GREATER_THAN:
1137 match = driverVersion[infoIndex] > info[i].mDriverVersion;
1138 break;
1139 case DRIVER_GREATER_THAN_OR_EQUAL:
1140 match = driverVersion[infoIndex] >= info[i].mDriverVersion;
1141 break;
1142 case DRIVER_EQUAL:
1143 match = driverVersion[infoIndex] == info[i].mDriverVersion;
1144 break;
1145 case DRIVER_NOT_EQUAL:
1146 match = driverVersion[infoIndex] != info[i].mDriverVersion;
1147 break;
1148 case DRIVER_BETWEEN_EXCLUSIVE:
1149 match = driverVersion[infoIndex] > info[i].mDriverVersion &&
1150 driverVersion[infoIndex] < info[i].mDriverVersionMax;
1151 break;
1152 case DRIVER_BETWEEN_INCLUSIVE:
1153 match = driverVersion[infoIndex] >= info[i].mDriverVersion &&
1154 driverVersion[infoIndex] <= info[i].mDriverVersionMax;
1155 break;
1156 case DRIVER_BETWEEN_INCLUSIVE_START:
1157 match = driverVersion[infoIndex] >= info[i].mDriverVersion &&
1158 driverVersion[infoIndex] < info[i].mDriverVersionMax;
1159 break;
1160 case DRIVER_COMPARISON_IGNORED:
1161 // We don't have a comparison op, so we match everything.
1162 match = true;
1163 break;
1164 default:
1165 NS_WARNING("Bogus op in GfxDriverInfo");
1166 break;
1168 #else
1169 // We don't care what driver version it was. We only check OS version and if
1170 // the device matches.
1171 match = true;
1172 #endif
1174 if (match || info[i].mDriverVersion == GfxDriverInfo::allDriverVersions) {
1175 if (info[i].mFeature == GfxDriverInfo::allFeatures ||
1176 info[i].mFeature == aFeature) {
1177 status = info[i].mFeatureStatus;
1178 if (!info[i].mRuleId.IsEmpty()) {
1179 aFailureId = info[i].mRuleId.get();
1180 } else {
1181 aFailureId = "FEATURE_FAILURE_DL_BLOCKLIST_NO_ID";
1183 break;
1188 #if defined(XP_WIN)
1189 // As a very special case, we block D2D on machines with an NVidia 310M GPU
1190 // as either the primary or secondary adapter. D2D is also blocked when the
1191 // NV 310M is the primary adapter (using the standard blocklisting mechanism).
1192 // If the primary GPU already matched something in the blocklist then we
1193 // ignore this special rule. See bug 1008759.
1194 if (status == nsIGfxInfo::FEATURE_STATUS_UNKNOWN &&
1195 (aFeature == nsIGfxInfo::FEATURE_DIRECT2D)) {
1196 if (!adapterInfoFailed[1]) {
1197 nsAString& nvVendorID =
1198 (nsAString&)GfxDriverInfo::GetDeviceVendor(DeviceVendor::NVIDIA);
1199 const nsString nv310mDeviceId = u"0x0A70"_ns;
1200 if (nvVendorID.Equals(adapterVendorID[1],
1201 nsCaseInsensitiveStringComparator) &&
1202 nv310mDeviceId.Equals(adapterDeviceID[1],
1203 nsCaseInsensitiveStringComparator)) {
1204 status = nsIGfxInfo::FEATURE_BLOCKED_DEVICE;
1205 aFailureId = "FEATURE_FAILURE_D2D_NV310M_BLOCK";
1210 // Depends on Windows driver versioning. We don't pass a GfxDriverInfo object
1211 // back to the Windows handler, so we must handle this here.
1212 if (status == FEATURE_BLOCKED_DRIVER_VERSION) {
1213 if (info[i].mSuggestedVersion) {
1214 aSuggestedVersion.AppendPrintf("%s", info[i].mSuggestedVersion);
1215 } else if (info[i].mComparisonOp == DRIVER_LESS_THAN &&
1216 info[i].mDriverVersion != GfxDriverInfo::allDriverVersions) {
1217 aSuggestedVersion.AppendPrintf(
1218 "%lld.%lld.%lld.%lld",
1219 (info[i].mDriverVersion & 0xffff000000000000) >> 48,
1220 (info[i].mDriverVersion & 0x0000ffff00000000) >> 32,
1221 (info[i].mDriverVersion & 0x00000000ffff0000) >> 16,
1222 (info[i].mDriverVersion & 0x000000000000ffff));
1225 #endif
1227 return status;
1230 void GfxInfoBase::SetFeatureStatus(nsTArray<gfx::GfxInfoFeatureStatus>&& aFS) {
1231 MOZ_ASSERT(!sFeatureStatus);
1232 InitFeatureStatus(new nsTArray<gfx::GfxInfoFeatureStatus>(std::move(aFS)));
1235 bool GfxInfoBase::DoesWindowProtocolMatch(
1236 const nsAString& aBlocklistWindowProtocol,
1237 const nsAString& aWindowProtocol) {
1238 return aBlocklistWindowProtocol.Equals(aWindowProtocol,
1239 nsCaseInsensitiveStringComparator) ||
1240 aBlocklistWindowProtocol.Equals(
1241 GfxDriverInfo::GetWindowProtocol(WindowProtocol::All),
1242 nsCaseInsensitiveStringComparator);
1245 bool GfxInfoBase::DoesVendorMatch(const nsAString& aBlocklistVendor,
1246 const nsAString& aAdapterVendor) {
1247 return aBlocklistVendor.Equals(aAdapterVendor,
1248 nsCaseInsensitiveStringComparator) ||
1249 aBlocklistVendor.Equals(
1250 GfxDriverInfo::GetDeviceVendor(DeviceVendor::All),
1251 nsCaseInsensitiveStringComparator);
1254 bool GfxInfoBase::DoesDriverVendorMatch(const nsAString& aBlocklistVendor,
1255 const nsAString& aDriverVendor) {
1256 return aBlocklistVendor.Equals(aDriverVendor,
1257 nsCaseInsensitiveStringComparator) ||
1258 aBlocklistVendor.Equals(
1259 GfxDriverInfo::GetDriverVendor(DriverVendor::All),
1260 nsCaseInsensitiveStringComparator);
1263 bool GfxInfoBase::IsFeatureAllowlisted(int32_t aFeature) const {
1264 return aFeature == nsIGfxInfo::FEATURE_VIDEO_OVERLAY ||
1265 aFeature == nsIGfxInfo::FEATURE_HW_DECODED_VIDEO_ZERO_COPY;
1268 nsresult GfxInfoBase::GetFeatureStatusImpl(
1269 int32_t aFeature, int32_t* aStatus, nsAString& aSuggestedVersion,
1270 const nsTArray<GfxDriverInfo>& aDriverInfo, nsACString& aFailureId,
1271 OperatingSystem* aOS /* = nullptr */) {
1272 if (aFeature <= 0) {
1273 gfxWarning() << "Invalid feature <= 0";
1274 return NS_OK;
1277 if (*aStatus != nsIGfxInfo::FEATURE_STATUS_UNKNOWN) {
1278 // Terminate now with the status determined by the derived type (OS-specific
1279 // code).
1280 return NS_OK;
1283 if (sShutdownOccurred) {
1284 // This is futile; we've already commenced shutdown and our blocklists have
1285 // been deleted. We may want to look into resurrecting the blocklist instead
1286 // but for now, just don't even go there.
1287 return NS_OK;
1290 // Ensure any additional initialization required is complete.
1291 GetData();
1293 // If an operating system was provided by the derived GetFeatureStatusImpl,
1294 // grab it here. Otherwise, the OS is unknown.
1295 OperatingSystem os = (aOS ? *aOS : OperatingSystem::Unknown);
1297 nsAutoString adapterVendorID;
1298 nsAutoString adapterDeviceID;
1299 nsAutoString adapterDriverVersionString;
1300 if (NS_FAILED(GetAdapterVendorID(adapterVendorID)) ||
1301 NS_FAILED(GetAdapterDeviceID(adapterDeviceID)) ||
1302 NS_FAILED(GetAdapterDriverVersion(adapterDriverVersionString))) {
1303 aFailureId = "FEATURE_FAILURE_CANT_RESOLVE_ADAPTER";
1304 *aStatus = FEATURE_BLOCKED_DEVICE;
1305 return NS_OK;
1308 // We only check either the given blocklist, or the static list, as given.
1309 int32_t status;
1310 if (aDriverInfo.Length()) {
1311 status =
1312 FindBlocklistedDeviceInList(aDriverInfo, aSuggestedVersion, aFeature,
1313 aFailureId, os, /* aForAllowing */ false);
1314 } else {
1315 if (!sDriverInfo) {
1316 sDriverInfo = new nsTArray<GfxDriverInfo>();
1318 status = FindBlocklistedDeviceInList(GetGfxDriverInfo(), aSuggestedVersion,
1319 aFeature, aFailureId, os,
1320 /* aForAllowing */ false);
1323 if (status == nsIGfxInfo::FEATURE_STATUS_UNKNOWN) {
1324 if (IsFeatureAllowlisted(aFeature)) {
1325 // This feature is actually using the allowlist; that means after we pass
1326 // the blocklist to prevent us explicitly from getting the feature, we now
1327 // need to check the allowlist to ensure we are allowed to get it in the
1328 // first place.
1329 if (aDriverInfo.Length()) {
1330 status = FindBlocklistedDeviceInList(aDriverInfo, aSuggestedVersion,
1331 aFeature, aFailureId, os,
1332 /* aForAllowing */ true);
1333 } else {
1334 status = FindBlocklistedDeviceInList(
1335 GetGfxDriverInfo(), aSuggestedVersion, aFeature, aFailureId, os,
1336 /* aForAllowing */ true);
1339 if (status == nsIGfxInfo::FEATURE_STATUS_UNKNOWN) {
1340 status = nsIGfxInfo::FEATURE_DENIED;
1342 } else {
1343 // It's now done being processed. It's safe to set the status to
1344 // STATUS_OK.
1345 status = nsIGfxInfo::FEATURE_STATUS_OK;
1349 *aStatus = status;
1350 return NS_OK;
1353 NS_IMETHODIMP
1354 GfxInfoBase::GetFeatureSuggestedDriverVersion(int32_t aFeature,
1355 nsAString& aVersion) {
1356 nsCString version;
1357 if (GetPrefValueForDriverVersion(version)) {
1358 aVersion = NS_ConvertASCIItoUTF16(version);
1359 return NS_OK;
1362 int32_t status;
1363 nsCString discardFailureId;
1364 nsTArray<GfxDriverInfo> driverInfo;
1365 return GetFeatureStatusImpl(aFeature, &status, aVersion, driverInfo,
1366 discardFailureId);
1369 void GfxInfoBase::EvaluateDownloadedBlocklist(
1370 nsTArray<GfxDriverInfo>& aDriverInfo) {
1371 // If the list is empty, then we don't actually want to call
1372 // GetFeatureStatusImpl since we will use the static list instead. In that
1373 // case, all we want to do is make sure the pref is removed.
1374 if (aDriverInfo.IsEmpty()) {
1375 gfxCriticalNoteOnce << "Evaluate empty downloaded blocklist";
1376 return;
1379 OperatingSystem os = GetOperatingSystem();
1381 // For every feature we know about, we evaluate whether this blocklist has a
1382 // non-STATUS_OK status. If it does, we set the pref we evaluate in
1383 // GetFeatureStatus above, so we don't need to hold on to this blocklist
1384 // anywhere permanent.
1385 for (int feature = 1; feature <= nsIGfxInfo::FEATURE_MAX_VALUE; ++feature) {
1386 int32_t status = nsIGfxInfo::FEATURE_STATUS_UNKNOWN;
1387 nsCString failureId;
1388 nsAutoString suggestedVersion;
1390 // Note that we are careful to call the base class method since we only want
1391 // to evaluate the downloadable blocklist for these prefs.
1392 MOZ_ALWAYS_TRUE(NS_SUCCEEDED(GfxInfoBase::GetFeatureStatusImpl(
1393 feature, &status, suggestedVersion, aDriverInfo, failureId, &os)));
1395 switch (status) {
1396 default:
1397 MOZ_FALLTHROUGH_ASSERT("Unhandled feature status!");
1398 case nsIGfxInfo::FEATURE_STATUS_UNKNOWN:
1399 // This may be returned during shutdown or for invalid features.
1400 case nsIGfxInfo::FEATURE_ALLOW_ALWAYS:
1401 case nsIGfxInfo::FEATURE_ALLOW_QUALIFIED:
1402 case nsIGfxInfo::FEATURE_DENIED:
1403 // We cannot use the downloadable blocklist to control the allowlist.
1404 // If a feature is allowlisted, then we should also ignore DENIED
1405 // statuses from GetFeatureStatusImpl because we don't check the
1406 // static list when and this is an expected value. If we wish to
1407 // override the allowlist, it is as simple as creating a normal
1408 // blocklist rule with a BLOCKED* status code.
1409 case nsIGfxInfo::FEATURE_STATUS_OK:
1410 RemovePrefForFeature(feature);
1411 break;
1413 case nsIGfxInfo::FEATURE_BLOCKED_DRIVER_VERSION:
1414 if (!suggestedVersion.IsEmpty()) {
1415 SetPrefValueForDriverVersion(suggestedVersion);
1416 } else {
1417 RemovePrefForDriverVersion();
1419 [[fallthrough]];
1421 case nsIGfxInfo::FEATURE_BLOCKED_MISMATCHED_VERSION:
1422 case nsIGfxInfo::FEATURE_BLOCKED_DEVICE:
1423 case nsIGfxInfo::FEATURE_DISCOURAGED:
1424 case nsIGfxInfo::FEATURE_BLOCKED_OS_VERSION:
1425 case nsIGfxInfo::FEATURE_BLOCKED_PLATFORM_TEST:
1426 SetPrefValueForFeature(feature, status, failureId);
1427 break;
1432 NS_IMETHODIMP_(void)
1433 GfxInfoBase::LogFailure(const nsACString& failure) {
1434 // gfxCriticalError has a mutex lock of its own, so we may not actually
1435 // need this lock. ::GetFailures() accesses the data but the LogForwarder
1436 // will not return the copy of the logs unless it can get the same lock
1437 // that gfxCriticalError uses. Still, that is so much of an implementation
1438 // detail that it's nicer to just add an extra lock here and in
1439 // ::GetFailures()
1440 MutexAutoLock lock(mMutex);
1442 // By default, gfxCriticalError asserts; make it not assert in this case.
1443 gfxCriticalError(CriticalLog::DefaultOptions(false))
1444 << "(LF) " << failure.BeginReading();
1447 NS_IMETHODIMP GfxInfoBase::GetFailures(nsTArray<int32_t>& indices,
1448 nsTArray<nsCString>& failures) {
1449 MutexAutoLock lock(mMutex);
1451 LogForwarder* logForwarder = Factory::GetLogForwarder();
1452 if (!logForwarder) {
1453 return NS_ERROR_UNEXPECTED;
1456 // There are two string copies in this method, starting with this one. We are
1457 // assuming this is not a big deal, as the size of the array should be small
1458 // and the strings in it should be small as well (the error messages in the
1459 // code.) The second copy happens with the AppendElement() calls.
1460 // Technically, we don't need the mutex lock after the StringVectorCopy()
1461 // call.
1462 LoggingRecord loggedStrings = logForwarder->LoggingRecordCopy();
1463 LoggingRecord::const_iterator it;
1464 for (it = loggedStrings.begin(); it != loggedStrings.end(); ++it) {
1465 failures.AppendElement(nsDependentCSubstring(std::get<1>(*it).c_str(),
1466 std::get<1>(*it).size()));
1467 indices.AppendElement(std::get<0>(*it));
1470 return NS_OK;
1473 nsTArray<GfxInfoCollectorBase*>* sCollectors;
1475 static void InitCollectors() {
1476 if (!sCollectors) sCollectors = new nsTArray<GfxInfoCollectorBase*>;
1479 nsresult GfxInfoBase::GetInfo(JSContext* aCx,
1480 JS::MutableHandle<JS::Value> aResult) {
1481 InitCollectors();
1482 InfoObject obj(aCx);
1484 for (uint32_t i = 0; i < sCollectors->Length(); i++) {
1485 (*sCollectors)[i]->GetInfo(obj);
1488 // Some example property definitions
1489 // obj.DefineProperty("wordCacheSize", gfxTextRunWordCache::Count());
1490 // obj.DefineProperty("renderer", mRendererIDsString);
1491 // obj.DefineProperty("five", 5);
1493 if (!obj.mOk) {
1494 return NS_ERROR_FAILURE;
1497 aResult.setObject(*obj.mObj);
1498 return NS_OK;
1501 nsAutoCString gBaseAppVersion;
1503 const nsCString& GfxInfoBase::GetApplicationVersion() {
1504 static bool versionInitialized = false;
1505 if (!versionInitialized) {
1506 // If we fail to get the version, we will not try again.
1507 versionInitialized = true;
1509 // Get the version from xpcom/system/nsIXULAppInfo.idl
1510 nsCOMPtr<nsIXULAppInfo> app = do_GetService("@mozilla.org/xre/app-info;1");
1511 if (app) {
1512 app->GetVersion(gBaseAppVersion);
1515 return gBaseAppVersion;
1518 void GfxInfoBase::AddCollector(GfxInfoCollectorBase* collector) {
1519 InitCollectors();
1520 sCollectors->AppendElement(collector);
1523 void GfxInfoBase::RemoveCollector(GfxInfoCollectorBase* collector) {
1524 InitCollectors();
1525 for (uint32_t i = 0; i < sCollectors->Length(); i++) {
1526 if ((*sCollectors)[i] == collector) {
1527 sCollectors->RemoveElementAt(i);
1528 break;
1531 if (sCollectors->IsEmpty()) {
1532 delete sCollectors;
1533 sCollectors = nullptr;
1537 static void AppendMonitor(JSContext* aCx, widget::Screen& aScreen,
1538 JS::Handle<JSObject*> aOutArray, int32_t aIndex) {
1539 JS::Rooted<JSObject*> obj(aCx, JS_NewPlainObject(aCx));
1541 auto screenSize = aScreen.GetRect().Size();
1543 JS::Rooted<JS::Value> screenWidth(aCx, JS::Int32Value(screenSize.width));
1544 JS_SetProperty(aCx, obj, "screenWidth", screenWidth);
1546 JS::Rooted<JS::Value> screenHeight(aCx, JS::Int32Value(screenSize.height));
1547 JS_SetProperty(aCx, obj, "screenHeight", screenHeight);
1549 // XXX Just preserving behavior since this is exposed to telemetry, but we
1550 // could consider including this everywhere.
1551 #ifdef XP_MACOSX
1552 JS::Rooted<JS::Value> scale(
1553 aCx, JS::NumberValue(aScreen.GetContentsScaleFactor()));
1554 JS_SetProperty(aCx, obj, "scale", scale);
1555 #endif
1557 #ifdef XP_WIN
1558 JS::Rooted<JS::Value> refreshRate(aCx,
1559 JS::Int32Value(aScreen.GetRefreshRate()));
1560 JS_SetProperty(aCx, obj, "refreshRate", refreshRate);
1562 JS::Rooted<JS::Value> pseudoDisplay(
1563 aCx, JS::BooleanValue(aScreen.GetIsPseudoDisplay()));
1564 JS_SetProperty(aCx, obj, "pseudoDisplay", pseudoDisplay);
1565 #endif
1567 JS::Rooted<JS::Value> element(aCx, JS::ObjectValue(*obj));
1568 JS_SetElement(aCx, aOutArray, aIndex, element);
1571 nsresult GfxInfoBase::FindMonitors(JSContext* aCx,
1572 JS::Handle<JSObject*> aOutArray) {
1573 int32_t index = 0;
1574 auto& sm = ScreenManager::GetSingleton();
1575 for (auto& screen : sm.CurrentScreenList()) {
1576 AppendMonitor(aCx, *screen, aOutArray, index++);
1579 if (index == 0) {
1580 // Ensure we return at least one monitor, this is needed for xpcshell.
1581 RefPtr<Screen> screen = sm.GetPrimaryScreen();
1582 AppendMonitor(aCx, *screen, aOutArray, index++);
1585 return NS_OK;
1588 NS_IMETHODIMP
1589 GfxInfoBase::GetMonitors(JSContext* aCx, JS::MutableHandle<JS::Value> aResult) {
1590 JS::Rooted<JSObject*> array(aCx, JS::NewArrayObject(aCx, 0));
1592 nsresult rv = FindMonitors(aCx, array);
1593 if (NS_FAILED(rv)) {
1594 return rv;
1597 aResult.setObject(*array);
1598 return NS_OK;
1601 static inline bool SetJSPropertyString(JSContext* aCx,
1602 JS::Handle<JSObject*> aObj,
1603 const char* aProp, const char* aString) {
1604 JS::Rooted<JSString*> str(aCx, JS_NewStringCopyZ(aCx, aString));
1605 if (!str) {
1606 return false;
1609 JS::Rooted<JS::Value> val(aCx, JS::StringValue(str));
1610 return JS_SetProperty(aCx, aObj, aProp, val);
1613 template <typename T>
1614 static inline bool AppendJSElement(JSContext* aCx, JS::Handle<JSObject*> aObj,
1615 const T& aValue) {
1616 uint32_t index;
1617 if (!JS::GetArrayLength(aCx, aObj, &index)) {
1618 return false;
1620 return JS_SetElement(aCx, aObj, index, aValue);
1623 nsresult GfxInfoBase::GetFeatures(JSContext* aCx,
1624 JS::MutableHandle<JS::Value> aOut) {
1625 JS::Rooted<JSObject*> obj(aCx, JS_NewPlainObject(aCx));
1626 if (!obj) {
1627 return NS_ERROR_OUT_OF_MEMORY;
1629 aOut.setObject(*obj);
1631 layers::LayersBackend backend =
1632 gfxPlatform::Initialized()
1633 ? gfxPlatform::GetPlatform()->GetCompositorBackend()
1634 : layers::LayersBackend::LAYERS_NONE;
1635 const char* backendName = layers::GetLayersBackendName(backend);
1636 SetJSPropertyString(aCx, obj, "compositor", backendName);
1638 // If graphics isn't initialized yet, just stop now.
1639 if (!gfxPlatform::Initialized()) {
1640 return NS_OK;
1643 DescribeFeatures(aCx, obj);
1644 return NS_OK;
1647 nsresult GfxInfoBase::GetFeatureLog(JSContext* aCx,
1648 JS::MutableHandle<JS::Value> aOut) {
1649 JS::Rooted<JSObject*> containerObj(aCx, JS_NewPlainObject(aCx));
1650 if (!containerObj) {
1651 return NS_ERROR_OUT_OF_MEMORY;
1653 aOut.setObject(*containerObj);
1655 JS::Rooted<JSObject*> featureArray(aCx, JS::NewArrayObject(aCx, 0));
1656 if (!featureArray) {
1657 return NS_ERROR_OUT_OF_MEMORY;
1660 // Collect features.
1661 gfxConfig::ForEachFeature([&](const char* aName, const char* aDescription,
1662 FeatureState& aFeature) -> void {
1663 JS::Rooted<JSObject*> obj(aCx, JS_NewPlainObject(aCx));
1664 if (!obj) {
1665 return;
1667 if (!SetJSPropertyString(aCx, obj, "name", aName) ||
1668 !SetJSPropertyString(aCx, obj, "description", aDescription) ||
1669 !SetJSPropertyString(aCx, obj, "status",
1670 FeatureStatusToString(aFeature.GetValue()))) {
1671 return;
1674 JS::Rooted<JS::Value> log(aCx);
1675 if (!BuildFeatureStateLog(aCx, aFeature, &log)) {
1676 return;
1678 if (!JS_SetProperty(aCx, obj, "log", log)) {
1679 return;
1682 if (!AppendJSElement(aCx, featureArray, obj)) {
1683 return;
1687 JS::Rooted<JSObject*> fallbackArray(aCx, JS::NewArrayObject(aCx, 0));
1688 if (!fallbackArray) {
1689 return NS_ERROR_OUT_OF_MEMORY;
1692 // Collect fallbacks.
1693 gfxConfig::ForEachFallback(
1694 [&](const char* aName, const char* aMessage) -> void {
1695 JS::Rooted<JSObject*> obj(aCx, JS_NewPlainObject(aCx));
1696 if (!obj) {
1697 return;
1700 if (!SetJSPropertyString(aCx, obj, "name", aName) ||
1701 !SetJSPropertyString(aCx, obj, "message", aMessage)) {
1702 return;
1705 if (!AppendJSElement(aCx, fallbackArray, obj)) {
1706 return;
1710 JS::Rooted<JS::Value> val(aCx);
1712 val = JS::ObjectValue(*featureArray);
1713 JS_SetProperty(aCx, containerObj, "features", val);
1715 val = JS::ObjectValue(*fallbackArray);
1716 JS_SetProperty(aCx, containerObj, "fallbacks", val);
1718 return NS_OK;
1721 bool GfxInfoBase::BuildFeatureStateLog(JSContext* aCx,
1722 const FeatureState& aFeature,
1723 JS::MutableHandle<JS::Value> aOut) {
1724 JS::Rooted<JSObject*> log(aCx, JS::NewArrayObject(aCx, 0));
1725 if (!log) {
1726 return false;
1728 aOut.setObject(*log);
1730 aFeature.ForEachStatusChange([&](const char* aType, FeatureStatus aStatus,
1731 const char* aMessage,
1732 const nsCString& aFailureId) -> void {
1733 JS::Rooted<JSObject*> obj(aCx, JS_NewPlainObject(aCx));
1734 if (!obj) {
1735 return;
1738 if (!SetJSPropertyString(aCx, obj, "type", aType) ||
1739 !SetJSPropertyString(aCx, obj, "status",
1740 FeatureStatusToString(aStatus)) ||
1741 (!aFailureId.IsEmpty() &&
1742 !SetJSPropertyString(aCx, obj, "failureId", aFailureId.get())) ||
1743 (aMessage && !SetJSPropertyString(aCx, obj, "message", aMessage))) {
1744 return;
1747 if (!AppendJSElement(aCx, log, obj)) {
1748 return;
1752 return true;
1755 void GfxInfoBase::DescribeFeatures(JSContext* aCx, JS::Handle<JSObject*> aObj) {
1756 JS::Rooted<JSObject*> obj(aCx);
1758 gfx::FeatureState& hwCompositing =
1759 gfxConfig::GetFeature(gfx::Feature::HW_COMPOSITING);
1760 InitFeatureObject(aCx, aObj, "hwCompositing", hwCompositing, &obj);
1762 gfx::FeatureState& gpuProcess =
1763 gfxConfig::GetFeature(gfx::Feature::GPU_PROCESS);
1764 InitFeatureObject(aCx, aObj, "gpuProcess", gpuProcess, &obj);
1766 gfx::FeatureState& webrender = gfxConfig::GetFeature(gfx::Feature::WEBRENDER);
1767 InitFeatureObject(aCx, aObj, "webrender", webrender, &obj);
1769 gfx::FeatureState& wrCompositor =
1770 gfxConfig::GetFeature(gfx::Feature::WEBRENDER_COMPOSITOR);
1771 InitFeatureObject(aCx, aObj, "wrCompositor", wrCompositor, &obj);
1773 gfx::FeatureState& openglCompositing =
1774 gfxConfig::GetFeature(gfx::Feature::OPENGL_COMPOSITING);
1775 InitFeatureObject(aCx, aObj, "openglCompositing", openglCompositing, &obj);
1777 gfx::FeatureState& omtp = gfxConfig::GetFeature(gfx::Feature::OMTP);
1778 InitFeatureObject(aCx, aObj, "omtp", omtp, &obj);
1781 bool GfxInfoBase::InitFeatureObject(JSContext* aCx,
1782 JS::Handle<JSObject*> aContainer,
1783 const char* aName,
1784 mozilla::gfx::FeatureState& aFeatureState,
1785 JS::MutableHandle<JSObject*> aOutObj) {
1786 JS::Rooted<JSObject*> obj(aCx, JS_NewPlainObject(aCx));
1787 if (!obj) {
1788 return false;
1791 nsCString status = aFeatureState.GetStatusAndFailureIdString();
1793 JS::Rooted<JSString*> str(aCx, JS_NewStringCopyZ(aCx, status.get()));
1794 JS::Rooted<JS::Value> val(aCx, JS::StringValue(str));
1795 JS_SetProperty(aCx, obj, "status", val);
1797 // Add the feature object to the container.
1799 JS::Rooted<JS::Value> val(aCx, JS::ObjectValue(*obj));
1800 JS_SetProperty(aCx, aContainer, aName, val);
1803 aOutObj.set(obj);
1804 return true;
1807 nsresult GfxInfoBase::GetActiveCrashGuards(JSContext* aCx,
1808 JS::MutableHandle<JS::Value> aOut) {
1809 JS::Rooted<JSObject*> array(aCx, JS::NewArrayObject(aCx, 0));
1810 if (!array) {
1811 return NS_ERROR_OUT_OF_MEMORY;
1813 aOut.setObject(*array);
1815 DriverCrashGuard::ForEachActiveCrashGuard(
1816 [&](const char* aName, const char* aPrefName) -> void {
1817 JS::Rooted<JSObject*> obj(aCx, JS_NewPlainObject(aCx));
1818 if (!obj) {
1819 return;
1821 if (!SetJSPropertyString(aCx, obj, "type", aName)) {
1822 return;
1824 if (!SetJSPropertyString(aCx, obj, "prefName", aPrefName)) {
1825 return;
1827 if (!AppendJSElement(aCx, array, obj)) {
1828 return;
1832 return NS_OK;
1835 NS_IMETHODIMP
1836 GfxInfoBase::GetTargetFrameRate(uint32_t* aTargetFrameRate) {
1837 *aTargetFrameRate = gfxPlatform::TargetFrameRate();
1838 return NS_OK;
1841 NS_IMETHODIMP
1842 GfxInfoBase::GetCodecSupportInfo(nsACString& aCodecSupportInfo) {
1843 aCodecSupportInfo.Assign(gfx::gfxVars::CodecSupportInfo());
1844 return NS_OK;
1847 NS_IMETHODIMP
1848 GfxInfoBase::GetIsHeadless(bool* aIsHeadless) {
1849 *aIsHeadless = gfxPlatform::IsHeadless();
1850 return NS_OK;
1853 NS_IMETHODIMP
1854 GfxInfoBase::GetContentBackend(nsAString& aContentBackend) {
1855 BackendType backend = gfxPlatform::GetPlatform()->GetDefaultContentBackend();
1856 nsString outStr;
1858 switch (backend) {
1859 case BackendType::DIRECT2D1_1: {
1860 outStr.AppendPrintf("Direct2D 1.1");
1861 break;
1863 case BackendType::SKIA: {
1864 outStr.AppendPrintf("Skia");
1865 break;
1867 case BackendType::CAIRO: {
1868 outStr.AppendPrintf("Cairo");
1869 break;
1871 default:
1872 return NS_ERROR_FAILURE;
1875 aContentBackend.Assign(outStr);
1876 return NS_OK;
1879 NS_IMETHODIMP
1880 GfxInfoBase::GetAzureCanvasBackend(nsAString& aBackend) {
1881 CopyASCIItoUTF16(mozilla::MakeStringSpan(
1882 gfxPlatform::GetPlatform()->GetAzureCanvasBackend()),
1883 aBackend);
1884 return NS_OK;
1887 NS_IMETHODIMP
1888 GfxInfoBase::GetAzureContentBackend(nsAString& aBackend) {
1889 CopyASCIItoUTF16(mozilla::MakeStringSpan(
1890 gfxPlatform::GetPlatform()->GetAzureContentBackend()),
1891 aBackend);
1892 return NS_OK;
1895 NS_IMETHODIMP
1896 GfxInfoBase::GetUsingGPUProcess(bool* aOutValue) {
1897 GPUProcessManager* gpu = GPUProcessManager::Get();
1898 if (!gpu) {
1899 // Not supported in content processes.
1900 return NS_ERROR_FAILURE;
1903 *aOutValue = !!gpu->GetGPUChild();
1904 return NS_OK;
1907 NS_IMETHODIMP_(int32_t)
1908 GfxInfoBase::GetMaxRefreshRate(bool* aMixed) {
1909 if (aMixed) {
1910 *aMixed = false;
1913 int32_t maxRefreshRate = 0;
1914 for (auto& screen : ScreenManager::GetSingleton().CurrentScreenList()) {
1915 int32_t refreshRate = screen->GetRefreshRate();
1916 if (aMixed && maxRefreshRate > 0 && maxRefreshRate != refreshRate) {
1917 *aMixed = true;
1919 maxRefreshRate = std::max(maxRefreshRate, refreshRate);
1922 return maxRefreshRate > 0 ? maxRefreshRate : -1;
1925 NS_IMETHODIMP
1926 GfxInfoBase::ControlGPUProcessForXPCShell(bool aEnable, bool* _retval) {
1927 gfxPlatform::GetPlatform();
1929 GPUProcessManager* gpm = GPUProcessManager::Get();
1930 if (aEnable) {
1931 if (!gfxConfig::IsEnabled(gfx::Feature::GPU_PROCESS)) {
1932 gfxConfig::UserForceEnable(gfx::Feature::GPU_PROCESS, "xpcshell-test");
1934 DebugOnly<nsresult> rv = gpm->EnsureGPUReady();
1935 MOZ_ASSERT(rv != NS_ERROR_ILLEGAL_DURING_SHUTDOWN);
1936 } else {
1937 gfxConfig::UserDisable(gfx::Feature::GPU_PROCESS, "xpcshell-test");
1938 gpm->KillProcess();
1941 *_retval = true;
1942 return NS_OK;
1945 NS_IMETHODIMP GfxInfoBase::KillGPUProcessForTests() {
1946 GPUProcessManager* gpm = GPUProcessManager::Get();
1947 if (!gpm) {
1948 // gfxPlatform has not been initialized.
1949 return NS_ERROR_NOT_INITIALIZED;
1952 gpm->KillProcess();
1953 return NS_OK;
1956 NS_IMETHODIMP GfxInfoBase::CrashGPUProcessForTests() {
1957 GPUProcessManager* gpm = GPUProcessManager::Get();
1958 if (!gpm) {
1959 // gfxPlatform has not been initialized.
1960 return NS_ERROR_NOT_INITIALIZED;
1963 gpm->CrashProcess();
1964 return NS_OK;
1967 GfxInfoCollectorBase::GfxInfoCollectorBase() {
1968 GfxInfoBase::AddCollector(this);
1971 GfxInfoCollectorBase::~GfxInfoCollectorBase() {
1972 GfxInfoBase::RemoveCollector(this);