Bug 1871845 [wpt PR 43787] - Update wpt metadata, a=testonly
[gecko.git] / widget / GfxInfoBase.cpp
blob3924c90eafb6a117c2b9fbb380694d551c0735f7
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/BuildConstants.h"
34 #include "mozilla/gfx/GPUProcessManager.h"
35 #include "mozilla/gfx/Logging.h"
36 #include "mozilla/gfx/gfxVars.h"
37 #include "mozilla/widget/ScreenManager.h"
38 #include "mozilla/widget/Screen.h"
40 #include "jsapi.h"
42 #include "gfxPlatform.h"
43 #include "gfxConfig.h"
44 #include "DriverCrashGuard.h"
46 using namespace mozilla::widget;
47 using namespace mozilla;
48 using mozilla::MutexAutoLock;
50 nsTArray<GfxDriverInfo>* GfxInfoBase::sDriverInfo;
51 StaticAutoPtr<nsTArray<gfx::GfxInfoFeatureStatus>> GfxInfoBase::sFeatureStatus;
52 bool GfxInfoBase::sDriverInfoObserverInitialized;
53 bool GfxInfoBase::sShutdownOccurred;
55 // Call this when setting sFeatureStatus to a non-null pointer to
56 // ensure destruction even if the GfxInfo component is never instantiated.
57 static void InitFeatureStatus(nsTArray<gfx::GfxInfoFeatureStatus>* aPtr) {
58 static std::once_flag sOnce;
59 std::call_once(sOnce, [] { ClearOnShutdown(&GfxInfoBase::sFeatureStatus); });
60 GfxInfoBase::sFeatureStatus = aPtr;
63 // Observes for shutdown so that the child GfxDriverInfo list is freed.
64 class ShutdownObserver : public nsIObserver {
65 virtual ~ShutdownObserver() = default;
67 public:
68 ShutdownObserver() = default;
70 NS_DECL_ISUPPORTS
72 NS_IMETHOD Observe(nsISupports* subject, const char* aTopic,
73 const char16_t* aData) override {
74 MOZ_ASSERT(strcmp(aTopic, NS_XPCOM_SHUTDOWN_OBSERVER_ID) == 0);
76 delete GfxInfoBase::sDriverInfo;
77 GfxInfoBase::sDriverInfo = nullptr;
79 for (auto& deviceFamily : GfxDriverInfo::sDeviceFamilies) {
80 delete deviceFamily;
81 deviceFamily = nullptr;
84 for (auto& windowProtocol : GfxDriverInfo::sWindowProtocol) {
85 delete windowProtocol;
86 windowProtocol = nullptr;
89 for (auto& deviceVendor : GfxDriverInfo::sDeviceVendors) {
90 delete deviceVendor;
91 deviceVendor = nullptr;
94 for (auto& driverVendor : GfxDriverInfo::sDriverVendors) {
95 delete driverVendor;
96 driverVendor = nullptr;
99 GfxInfoBase::sShutdownOccurred = true;
101 return NS_OK;
105 NS_IMPL_ISUPPORTS(ShutdownObserver, nsIObserver)
107 static void InitGfxDriverInfoShutdownObserver() {
108 if (GfxInfoBase::sDriverInfoObserverInitialized) return;
110 GfxInfoBase::sDriverInfoObserverInitialized = true;
112 nsCOMPtr<nsIObserverService> observerService = services::GetObserverService();
113 if (!observerService) {
114 NS_WARNING("Could not get observer service!");
115 return;
118 ShutdownObserver* obs = new ShutdownObserver();
119 observerService->AddObserver(obs, NS_XPCOM_SHUTDOWN_OBSERVER_ID, false);
122 using namespace mozilla::widget;
123 using namespace mozilla::gfx;
124 using namespace mozilla;
126 NS_IMPL_ISUPPORTS(GfxInfoBase, nsIGfxInfo, nsIObserver,
127 nsISupportsWeakReference)
129 #define BLOCKLIST_PREF_BRANCH "gfx.blacklist."
130 #define SUGGESTED_VERSION_PREF BLOCKLIST_PREF_BRANCH "suggested-driver-version"
132 static const char* GetPrefNameForFeature(int32_t aFeature) {
133 const char* name = nullptr;
134 switch (aFeature) {
135 case nsIGfxInfo::FEATURE_DIRECT2D:
136 name = BLOCKLIST_PREF_BRANCH "direct2d";
137 break;
138 case nsIGfxInfo::FEATURE_DIRECT3D_9_LAYERS:
139 name = BLOCKLIST_PREF_BRANCH "layers.direct3d9";
140 break;
141 case nsIGfxInfo::FEATURE_DIRECT3D_10_LAYERS:
142 name = BLOCKLIST_PREF_BRANCH "layers.direct3d10";
143 break;
144 case nsIGfxInfo::FEATURE_DIRECT3D_10_1_LAYERS:
145 name = BLOCKLIST_PREF_BRANCH "layers.direct3d10-1";
146 break;
147 case nsIGfxInfo::FEATURE_DIRECT3D_11_LAYERS:
148 name = BLOCKLIST_PREF_BRANCH "layers.direct3d11";
149 break;
150 case nsIGfxInfo::FEATURE_DIRECT3D_11_ANGLE:
151 name = BLOCKLIST_PREF_BRANCH "direct3d11angle";
152 break;
153 case nsIGfxInfo::FEATURE_HARDWARE_VIDEO_DECODING:
154 name = BLOCKLIST_PREF_BRANCH "hardwarevideodecoding";
155 break;
156 case nsIGfxInfo::FEATURE_OPENGL_LAYERS:
157 name = BLOCKLIST_PREF_BRANCH "layers.opengl";
158 break;
159 case nsIGfxInfo::FEATURE_WEBGL_OPENGL:
160 name = BLOCKLIST_PREF_BRANCH "webgl.opengl";
161 break;
162 case nsIGfxInfo::FEATURE_WEBGL_ANGLE:
163 name = BLOCKLIST_PREF_BRANCH "webgl.angle";
164 break;
165 case nsIGfxInfo::UNUSED_FEATURE_WEBGL_MSAA:
166 name = BLOCKLIST_PREF_BRANCH "webgl.msaa";
167 break;
168 case nsIGfxInfo::FEATURE_STAGEFRIGHT:
169 name = BLOCKLIST_PREF_BRANCH "stagefright";
170 break;
171 case nsIGfxInfo::FEATURE_WEBRTC_HW_ACCELERATION_H264:
172 name = BLOCKLIST_PREF_BRANCH "webrtc.hw.acceleration.h264";
173 break;
174 case nsIGfxInfo::FEATURE_WEBRTC_HW_ACCELERATION_ENCODE:
175 name = BLOCKLIST_PREF_BRANCH "webrtc.hw.acceleration.encode";
176 break;
177 case nsIGfxInfo::FEATURE_WEBRTC_HW_ACCELERATION_DECODE:
178 name = BLOCKLIST_PREF_BRANCH "webrtc.hw.acceleration.decode";
179 break;
180 case nsIGfxInfo::FEATURE_CANVAS2D_ACCELERATION:
181 name = BLOCKLIST_PREF_BRANCH "canvas2d.acceleration";
182 break;
183 case nsIGfxInfo::FEATURE_DX_INTEROP2:
184 name = BLOCKLIST_PREF_BRANCH "dx.interop2";
185 break;
186 case nsIGfxInfo::FEATURE_GPU_PROCESS:
187 name = BLOCKLIST_PREF_BRANCH "gpu.process";
188 break;
189 case nsIGfxInfo::FEATURE_WEBGL2:
190 name = BLOCKLIST_PREF_BRANCH "webgl2";
191 break;
192 case nsIGfxInfo::FEATURE_D3D11_KEYED_MUTEX:
193 name = BLOCKLIST_PREF_BRANCH "d3d11.keyed.mutex";
194 break;
195 case nsIGfxInfo::FEATURE_WEBRENDER:
196 name = BLOCKLIST_PREF_BRANCH "webrender";
197 break;
198 case nsIGfxInfo::FEATURE_WEBRENDER_COMPOSITOR:
199 name = BLOCKLIST_PREF_BRANCH "webrender.compositor";
200 break;
201 case nsIGfxInfo::FEATURE_DX_NV12:
202 name = BLOCKLIST_PREF_BRANCH "dx.nv12";
203 break;
204 case nsIGfxInfo::FEATURE_DX_P010:
205 name = BLOCKLIST_PREF_BRANCH "dx.p010";
206 break;
207 case nsIGfxInfo::FEATURE_DX_P016:
208 name = BLOCKLIST_PREF_BRANCH "dx.p016";
209 break;
210 case nsIGfxInfo::FEATURE_VP8_HW_DECODE:
211 name = BLOCKLIST_PREF_BRANCH "vp8.hw-decode";
212 break;
213 case nsIGfxInfo::FEATURE_VP9_HW_DECODE:
214 name = BLOCKLIST_PREF_BRANCH "vp9.hw-decode";
215 break;
216 case nsIGfxInfo::FEATURE_GL_SWIZZLE:
217 name = BLOCKLIST_PREF_BRANCH "gl.swizzle";
218 break;
219 case nsIGfxInfo::FEATURE_WEBRENDER_SCISSORED_CACHE_CLEARS:
220 name = BLOCKLIST_PREF_BRANCH "webrender.scissored_cache_clears";
221 break;
222 case nsIGfxInfo::FEATURE_ALLOW_WEBGL_OUT_OF_PROCESS:
223 name = BLOCKLIST_PREF_BRANCH "webgl.allow-oop";
224 break;
225 case nsIGfxInfo::FEATURE_THREADSAFE_GL:
226 name = BLOCKLIST_PREF_BRANCH "gl.threadsafe";
227 break;
228 case nsIGfxInfo::FEATURE_WEBRENDER_OPTIMIZED_SHADERS:
229 name = BLOCKLIST_PREF_BRANCH "webrender.optimized-shaders";
230 break;
231 case nsIGfxInfo::FEATURE_X11_EGL:
232 name = BLOCKLIST_PREF_BRANCH "x11.egl";
233 break;
234 case nsIGfxInfo::FEATURE_DMABUF:
235 name = BLOCKLIST_PREF_BRANCH "dmabuf";
236 break;
237 case nsIGfxInfo::FEATURE_WEBGPU:
238 name = BLOCKLIST_PREF_BRANCH "webgpu";
239 break;
240 case nsIGfxInfo::FEATURE_VIDEO_OVERLAY:
241 name = BLOCKLIST_PREF_BRANCH "video-overlay";
242 break;
243 case nsIGfxInfo::FEATURE_HW_DECODED_VIDEO_ZERO_COPY:
244 name = BLOCKLIST_PREF_BRANCH "hw-video-zero-copy";
245 break;
246 case nsIGfxInfo::FEATURE_WEBRENDER_SHADER_CACHE:
247 name = BLOCKLIST_PREF_BRANCH "webrender.program-binary-disk";
248 break;
249 case nsIGfxInfo::FEATURE_WEBRENDER_PARTIAL_PRESENT:
250 name = BLOCKLIST_PREF_BRANCH "webrender.partial-present";
251 break;
252 case nsIGfxInfo::FEATURE_DMABUF_SURFACE_EXPORT:
253 name = BLOCKLIST_PREF_BRANCH "dmabuf.surface-export";
254 break;
255 case nsIGfxInfo::FEATURE_REUSE_DECODER_DEVICE:
256 name = BLOCKLIST_PREF_BRANCH "reuse-decoder-device";
257 break;
258 case nsIGfxInfo::FEATURE_BACKDROP_FILTER:
259 name = BLOCKLIST_PREF_BRANCH "backdrop.filter";
260 break;
261 case nsIGfxInfo::FEATURE_ACCELERATED_CANVAS2D:
262 name = BLOCKLIST_PREF_BRANCH "accelerated-canvas2d";
263 break;
264 case nsIGfxInfo::FEATURE_H264_HW_DECODE:
265 name = BLOCKLIST_PREF_BRANCH "h264.hw-decode";
266 break;
267 case nsIGfxInfo::FEATURE_AV1_HW_DECODE:
268 name = BLOCKLIST_PREF_BRANCH "av1.hw-decode";
269 break;
270 case nsIGfxInfo::FEATURE_VIDEO_SOFTWARE_OVERLAY:
271 name = BLOCKLIST_PREF_BRANCH "video-software-overlay";
272 break;
273 case nsIGfxInfo::FEATURE_WEBGL_USE_HARDWARE:
274 name = BLOCKLIST_PREF_BRANCH "webgl-use-hardware";
275 break;
276 default:
277 MOZ_ASSERT_UNREACHABLE("Unexpected nsIGfxInfo feature?!");
278 break;
281 return name;
284 // Returns the value of the pref for the relevant feature in aValue.
285 // If the pref doesn't exist, aValue is not touched, and returns false.
286 static bool GetPrefValueForFeature(int32_t aFeature, int32_t& aValue,
287 nsACString& aFailureId) {
288 const char* prefname = GetPrefNameForFeature(aFeature);
289 if (!prefname) return false;
291 aValue = nsIGfxInfo::FEATURE_STATUS_UNKNOWN;
292 if (!NS_SUCCEEDED(Preferences::GetInt(prefname, &aValue))) {
293 return false;
296 if (aValue == nsIGfxInfo::FEATURE_DENIED) {
297 // We should never see the DENIED status with the downloadable blocklist.
298 return false;
301 nsCString failureprefname(prefname);
302 failureprefname += ".failureid";
303 nsAutoCString failureValue;
304 nsresult rv = Preferences::GetCString(failureprefname.get(), failureValue);
305 if (NS_SUCCEEDED(rv)) {
306 aFailureId = failureValue.get();
307 } else {
308 aFailureId = "FEATURE_FAILURE_BLOCKLIST_PREF";
311 return true;
314 static void SetPrefValueForFeature(int32_t aFeature, int32_t aValue,
315 const nsACString& aFailureId) {
316 const char* prefname = GetPrefNameForFeature(aFeature);
317 if (!prefname) return;
318 if (XRE_IsParentProcess()) {
319 GfxInfoBase::sFeatureStatus = nullptr;
322 Preferences::SetInt(prefname, aValue);
323 if (!aFailureId.IsEmpty()) {
324 nsAutoCString failureprefname(prefname);
325 failureprefname += ".failureid";
326 Preferences::SetCString(failureprefname.get(), aFailureId);
330 static void RemovePrefForFeature(int32_t aFeature) {
331 const char* prefname = GetPrefNameForFeature(aFeature);
332 if (!prefname) return;
334 if (XRE_IsParentProcess()) {
335 GfxInfoBase::sFeatureStatus = nullptr;
337 Preferences::ClearUser(prefname);
340 static bool GetPrefValueForDriverVersion(nsCString& aVersion) {
341 return NS_SUCCEEDED(
342 Preferences::GetCString(SUGGESTED_VERSION_PREF, aVersion));
345 static void SetPrefValueForDriverVersion(const nsAString& aVersion) {
346 Preferences::SetString(SUGGESTED_VERSION_PREF, aVersion);
349 static void RemovePrefForDriverVersion() {
350 Preferences::ClearUser(SUGGESTED_VERSION_PREF);
353 static OperatingSystem BlocklistOSToOperatingSystem(const nsAString& os) {
354 if (os.EqualsLiteral("WINNT 6.1")) {
355 return OperatingSystem::Windows7;
357 if (os.EqualsLiteral("WINNT 6.2")) {
358 return OperatingSystem::Windows8;
360 if (os.EqualsLiteral("WINNT 6.3")) {
361 return OperatingSystem::Windows8_1;
363 if (os.EqualsLiteral("WINNT 10.0")) {
364 return OperatingSystem::Windows10;
366 if (os.EqualsLiteral("Linux")) {
367 return OperatingSystem::Linux;
369 if (os.EqualsLiteral("Darwin 9")) {
370 return OperatingSystem::OSX10_5;
372 if (os.EqualsLiteral("Darwin 10")) {
373 return OperatingSystem::OSX10_6;
375 if (os.EqualsLiteral("Darwin 11")) {
376 return OperatingSystem::OSX10_7;
378 if (os.EqualsLiteral("Darwin 12")) {
379 return OperatingSystem::OSX10_8;
381 if (os.EqualsLiteral("Darwin 13")) {
382 return OperatingSystem::OSX10_9;
384 if (os.EqualsLiteral("Darwin 14")) {
385 return OperatingSystem::OSX10_10;
387 if (os.EqualsLiteral("Darwin 15")) {
388 return OperatingSystem::OSX10_11;
390 if (os.EqualsLiteral("Darwin 16")) {
391 return OperatingSystem::OSX10_12;
393 if (os.EqualsLiteral("Darwin 17")) {
394 return OperatingSystem::OSX10_13;
396 if (os.EqualsLiteral("Darwin 18")) {
397 return OperatingSystem::OSX10_14;
399 if (os.EqualsLiteral("Darwin 19")) {
400 return OperatingSystem::OSX10_15;
402 if (os.EqualsLiteral("Darwin 20")) {
403 return OperatingSystem::OSX11_0;
405 if (os.EqualsLiteral("Android")) {
406 return OperatingSystem::Android;
407 // For historical reasons, "All" in blocklist means "All Windows"
409 if (os.EqualsLiteral("All")) {
410 return OperatingSystem::Windows;
412 if (os.EqualsLiteral("Darwin")) {
413 return OperatingSystem::OSX;
416 return OperatingSystem::Unknown;
419 static GfxDeviceFamily* BlocklistDevicesToDeviceFamily(
420 nsTArray<nsCString>& devices) {
421 if (devices.Length() == 0) return nullptr;
423 // For each device, get its device ID, and return a freshly-allocated
424 // GfxDeviceFamily with the contents of that array.
425 GfxDeviceFamily* deviceIds = new GfxDeviceFamily;
427 for (uint32_t i = 0; i < devices.Length(); ++i) {
428 // We make sure we don't add any "empty" device entries to the array, so
429 // we don't need to check if devices[i] is empty.
430 deviceIds->Append(NS_ConvertUTF8toUTF16(devices[i]));
433 return deviceIds;
436 static int32_t BlocklistFeatureToGfxFeature(const nsAString& aFeature) {
437 MOZ_ASSERT(!aFeature.IsEmpty());
438 if (aFeature.EqualsLiteral("DIRECT2D")) {
439 return nsIGfxInfo::FEATURE_DIRECT2D;
441 if (aFeature.EqualsLiteral("DIRECT3D_9_LAYERS")) {
442 return nsIGfxInfo::FEATURE_DIRECT3D_9_LAYERS;
444 if (aFeature.EqualsLiteral("DIRECT3D_10_LAYERS")) {
445 return nsIGfxInfo::FEATURE_DIRECT3D_10_LAYERS;
447 if (aFeature.EqualsLiteral("DIRECT3D_10_1_LAYERS")) {
448 return nsIGfxInfo::FEATURE_DIRECT3D_10_1_LAYERS;
450 if (aFeature.EqualsLiteral("DIRECT3D_11_LAYERS")) {
451 return nsIGfxInfo::FEATURE_DIRECT3D_11_LAYERS;
453 if (aFeature.EqualsLiteral("DIRECT3D_11_ANGLE")) {
454 return nsIGfxInfo::FEATURE_DIRECT3D_11_ANGLE;
456 if (aFeature.EqualsLiteral("HARDWARE_VIDEO_DECODING")) {
457 return nsIGfxInfo::FEATURE_HARDWARE_VIDEO_DECODING;
459 if (aFeature.EqualsLiteral("OPENGL_LAYERS")) {
460 return nsIGfxInfo::FEATURE_OPENGL_LAYERS;
462 if (aFeature.EqualsLiteral("WEBGL_OPENGL")) {
463 return nsIGfxInfo::FEATURE_WEBGL_OPENGL;
465 if (aFeature.EqualsLiteral("WEBGL_ANGLE")) {
466 return nsIGfxInfo::FEATURE_WEBGL_ANGLE;
468 if (aFeature.EqualsLiteral("WEBGL_MSAA")) {
469 return nsIGfxInfo::UNUSED_FEATURE_WEBGL_MSAA;
471 if (aFeature.EqualsLiteral("STAGEFRIGHT")) {
472 return nsIGfxInfo::FEATURE_STAGEFRIGHT;
474 if (aFeature.EqualsLiteral("WEBRTC_HW_ACCELERATION_ENCODE")) {
475 return nsIGfxInfo::FEATURE_WEBRTC_HW_ACCELERATION_ENCODE;
477 if (aFeature.EqualsLiteral("WEBRTC_HW_ACCELERATION_DECODE")) {
478 return nsIGfxInfo::FEATURE_WEBRTC_HW_ACCELERATION_DECODE;
480 if (aFeature.EqualsLiteral("WEBRTC_HW_ACCELERATION_H264")) {
481 return nsIGfxInfo::FEATURE_WEBRTC_HW_ACCELERATION_H264;
483 if (aFeature.EqualsLiteral("CANVAS2D_ACCELERATION")) {
484 return nsIGfxInfo::FEATURE_CANVAS2D_ACCELERATION;
486 if (aFeature.EqualsLiteral("DX_INTEROP2")) {
487 return nsIGfxInfo::FEATURE_DX_INTEROP2;
489 if (aFeature.EqualsLiteral("GPU_PROCESS")) {
490 return nsIGfxInfo::FEATURE_GPU_PROCESS;
492 if (aFeature.EqualsLiteral("WEBGL2")) {
493 return nsIGfxInfo::FEATURE_WEBGL2;
495 if (aFeature.EqualsLiteral("D3D11_KEYED_MUTEX")) {
496 return nsIGfxInfo::FEATURE_D3D11_KEYED_MUTEX;
498 if (aFeature.EqualsLiteral("WEBRENDER")) {
499 return nsIGfxInfo::FEATURE_WEBRENDER;
501 if (aFeature.EqualsLiteral("WEBRENDER_COMPOSITOR")) {
502 return nsIGfxInfo::FEATURE_WEBRENDER_COMPOSITOR;
504 if (aFeature.EqualsLiteral("DX_NV12")) {
505 return nsIGfxInfo::FEATURE_DX_NV12;
507 if (aFeature.EqualsLiteral("VP8_HW_DECODE")) {
508 return nsIGfxInfo::FEATURE_VP8_HW_DECODE;
510 if (aFeature.EqualsLiteral("VP9_HW_DECODE")) {
511 return nsIGfxInfo::FEATURE_VP9_HW_DECODE;
513 if (aFeature.EqualsLiteral("GL_SWIZZLE")) {
514 return nsIGfxInfo::FEATURE_GL_SWIZZLE;
516 if (aFeature.EqualsLiteral("WEBRENDER_SCISSORED_CACHE_CLEARS")) {
517 return nsIGfxInfo::FEATURE_WEBRENDER_SCISSORED_CACHE_CLEARS;
519 if (aFeature.EqualsLiteral("ALLOW_WEBGL_OUT_OF_PROCESS")) {
520 return nsIGfxInfo::FEATURE_ALLOW_WEBGL_OUT_OF_PROCESS;
522 if (aFeature.EqualsLiteral("THREADSAFE_GL")) {
523 return nsIGfxInfo::FEATURE_THREADSAFE_GL;
525 if (aFeature.EqualsLiteral("X11_EGL")) {
526 return nsIGfxInfo::FEATURE_X11_EGL;
528 if (aFeature.EqualsLiteral("DMABUF")) {
529 return nsIGfxInfo::FEATURE_DMABUF;
531 if (aFeature.EqualsLiteral("WEBGPU")) {
532 return nsIGfxInfo::FEATURE_WEBGPU;
534 if (aFeature.EqualsLiteral("VIDEO_OVERLAY")) {
535 return nsIGfxInfo::FEATURE_VIDEO_OVERLAY;
537 if (aFeature.EqualsLiteral("HW_DECODED_VIDEO_ZERO_COPY")) {
538 return nsIGfxInfo::FEATURE_HW_DECODED_VIDEO_ZERO_COPY;
540 if (aFeature.EqualsLiteral("REUSE_DECODER_DEVICE")) {
541 return nsIGfxInfo::FEATURE_REUSE_DECODER_DEVICE;
543 if (aFeature.EqualsLiteral("WEBRENDER_PARTIAL_PRESENT")) {
544 return nsIGfxInfo::FEATURE_WEBRENDER_PARTIAL_PRESENT;
546 if (aFeature.EqualsLiteral("BACKDROP_FILTER")) {
547 return nsIGfxInfo::FEATURE_BACKDROP_FILTER;
549 if (aFeature.EqualsLiteral("ACCELERATED_CANVAS2D")) {
550 return nsIGfxInfo::FEATURE_ACCELERATED_CANVAS2D;
553 // If we don't recognize the feature, it may be new, and something
554 // this version doesn't understand. So, nothing to do. This is
555 // different from feature not being specified at all, in which case
556 // this method should not get called and we should continue with the
557 // "all features" blocklisting.
558 return -1;
561 static int32_t BlocklistFeatureStatusToGfxFeatureStatus(
562 const nsAString& aStatus) {
563 if (aStatus.EqualsLiteral("STATUS_OK")) {
564 return nsIGfxInfo::FEATURE_STATUS_OK;
566 if (aStatus.EqualsLiteral("BLOCKED_DRIVER_VERSION")) {
567 return nsIGfxInfo::FEATURE_BLOCKED_DRIVER_VERSION;
569 if (aStatus.EqualsLiteral("BLOCKED_DEVICE")) {
570 return nsIGfxInfo::FEATURE_BLOCKED_DEVICE;
572 if (aStatus.EqualsLiteral("DISCOURAGED")) {
573 return nsIGfxInfo::FEATURE_DISCOURAGED;
575 if (aStatus.EqualsLiteral("BLOCKED_OS_VERSION")) {
576 return nsIGfxInfo::FEATURE_BLOCKED_OS_VERSION;
578 if (aStatus.EqualsLiteral("DENIED")) {
579 return nsIGfxInfo::FEATURE_DENIED;
581 if (aStatus.EqualsLiteral("ALLOW_QUALIFIED")) {
582 return nsIGfxInfo::FEATURE_ALLOW_QUALIFIED;
584 if (aStatus.EqualsLiteral("ALLOW_ALWAYS")) {
585 return nsIGfxInfo::FEATURE_ALLOW_ALWAYS;
588 // Do not allow it to set STATUS_UNKNOWN. Also, we are not
589 // expecting the "mismatch" status showing up here.
591 return nsIGfxInfo::FEATURE_STATUS_OK;
594 static VersionComparisonOp BlocklistComparatorToComparisonOp(
595 const nsAString& op) {
596 if (op.EqualsLiteral("LESS_THAN")) {
597 return DRIVER_LESS_THAN;
599 if (op.EqualsLiteral("BUILD_ID_LESS_THAN")) {
600 return DRIVER_BUILD_ID_LESS_THAN;
602 if (op.EqualsLiteral("LESS_THAN_OR_EQUAL")) {
603 return DRIVER_LESS_THAN_OR_EQUAL;
605 if (op.EqualsLiteral("BUILD_ID_LESS_THAN_OR_EQUAL")) {
606 return DRIVER_BUILD_ID_LESS_THAN_OR_EQUAL;
608 if (op.EqualsLiteral("GREATER_THAN")) {
609 return DRIVER_GREATER_THAN;
611 if (op.EqualsLiteral("GREATER_THAN_OR_EQUAL")) {
612 return DRIVER_GREATER_THAN_OR_EQUAL;
614 if (op.EqualsLiteral("EQUAL")) {
615 return DRIVER_EQUAL;
617 if (op.EqualsLiteral("NOT_EQUAL")) {
618 return DRIVER_NOT_EQUAL;
620 if (op.EqualsLiteral("BETWEEN_EXCLUSIVE")) {
621 return DRIVER_BETWEEN_EXCLUSIVE;
623 if (op.EqualsLiteral("BETWEEN_INCLUSIVE")) {
624 return DRIVER_BETWEEN_INCLUSIVE;
626 if (op.EqualsLiteral("BETWEEN_INCLUSIVE_START")) {
627 return DRIVER_BETWEEN_INCLUSIVE_START;
630 return DRIVER_COMPARISON_IGNORED;
634 Deserialize Blocklist entries from string.
635 e.g:
636 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
638 static bool BlocklistEntryToDriverInfo(const nsACString& aBlocklistEntry,
639 GfxDriverInfo& aDriverInfo) {
640 // If we get an application version to be zero, something is not working
641 // and we are not going to bother checking the blocklist versions.
642 // See TestGfxWidgets.cpp for how version comparison works.
643 // <versionRange minVersion="42.0a1" maxVersion="45.0"></versionRange>
644 static mozilla::Version zeroV("0");
645 static mozilla::Version appV(GfxInfoBase::GetApplicationVersion().get());
646 if (appV <= zeroV) {
647 gfxCriticalErrorOnce(gfxCriticalError::DefaultOptions(false))
648 << "Invalid application version "
649 << GfxInfoBase::GetApplicationVersion().get();
652 aDriverInfo.mRuleId = "FEATURE_FAILURE_DL_BLOCKLIST_NO_ID"_ns;
654 for (const auto& keyValue : aBlocklistEntry.Split('\t')) {
655 nsTArray<nsCString> splitted;
656 ParseString(keyValue, ':', splitted);
657 if (splitted.Length() != 2) {
658 // If we don't recognize the input data, we do not want to proceed.
659 gfxCriticalErrorOnce(CriticalLog::DefaultOptions(false))
660 << "Unrecognized data " << nsCString(keyValue).get();
661 return false;
663 const nsCString& key = splitted[0];
664 const nsCString& value = splitted[1];
665 NS_ConvertUTF8toUTF16 dataValue(value);
667 if (value.Length() == 0) {
668 // Safety check for empty values.
669 gfxCriticalErrorOnce(CriticalLog::DefaultOptions(false))
670 << "Empty value for " << key.get();
671 return false;
674 if (key.EqualsLiteral("blockID")) {
675 nsCString blockIdStr = "FEATURE_FAILURE_DL_BLOCKLIST_"_ns + value;
676 aDriverInfo.mRuleId = blockIdStr.get();
677 } else if (key.EqualsLiteral("os")) {
678 aDriverInfo.mOperatingSystem = BlocklistOSToOperatingSystem(dataValue);
679 } else if (key.EqualsLiteral("osversion")) {
680 aDriverInfo.mOperatingSystemVersion = strtoul(value.get(), nullptr, 10);
681 } else if (key.EqualsLiteral("windowProtocol")) {
682 aDriverInfo.mWindowProtocol = dataValue;
683 } else if (key.EqualsLiteral("vendor")) {
684 aDriverInfo.mAdapterVendor = dataValue;
685 } else if (key.EqualsLiteral("driverVendor")) {
686 aDriverInfo.mDriverVendor = dataValue;
687 } else if (key.EqualsLiteral("feature")) {
688 aDriverInfo.mFeature = BlocklistFeatureToGfxFeature(dataValue);
689 if (aDriverInfo.mFeature < 0) {
690 // If we don't recognize the feature, we do not want to proceed.
691 gfxWarning() << "Unrecognized feature " << value.get();
692 return false;
694 } else if (key.EqualsLiteral("featureStatus")) {
695 aDriverInfo.mFeatureStatus =
696 BlocklistFeatureStatusToGfxFeatureStatus(dataValue);
697 } else if (key.EqualsLiteral("driverVersion")) {
698 uint64_t version;
699 if (ParseDriverVersion(dataValue, &version))
700 aDriverInfo.mDriverVersion = version;
701 } else if (key.EqualsLiteral("driverVersionMax")) {
702 uint64_t version;
703 if (ParseDriverVersion(dataValue, &version))
704 aDriverInfo.mDriverVersionMax = version;
705 } else if (key.EqualsLiteral("driverVersionComparator")) {
706 aDriverInfo.mComparisonOp = BlocklistComparatorToComparisonOp(dataValue);
707 } else if (key.EqualsLiteral("model")) {
708 aDriverInfo.mModel = dataValue;
709 } else if (key.EqualsLiteral("product")) {
710 aDriverInfo.mProduct = dataValue;
711 } else if (key.EqualsLiteral("manufacturer")) {
712 aDriverInfo.mManufacturer = dataValue;
713 } else if (key.EqualsLiteral("hardware")) {
714 aDriverInfo.mHardware = dataValue;
715 } else if (key.EqualsLiteral("versionRange")) {
716 nsTArray<nsCString> versionRange;
717 ParseString(value, ',', versionRange);
718 if (versionRange.Length() != 2) {
719 gfxCriticalErrorOnce(CriticalLog::DefaultOptions(false))
720 << "Unrecognized versionRange " << value.get();
721 return false;
723 const nsCString& minValue = versionRange[0];
724 const nsCString& maxValue = versionRange[1];
726 mozilla::Version minV(minValue.get());
727 mozilla::Version maxV(maxValue.get());
729 if (minV > zeroV && !(appV >= minV)) {
730 // The version of the application is less than the minimal version
731 // this blocklist entry applies to, so we can just ignore it by
732 // returning false and letting the caller deal with it.
733 return false;
735 if (maxV > zeroV && !(appV <= maxV)) {
736 // The version of the application is more than the maximal version
737 // this blocklist entry applies to, so we can just ignore it by
738 // returning false and letting the caller deal with it.
739 return false;
741 } else if (key.EqualsLiteral("devices")) {
742 nsTArray<nsCString> devices;
743 ParseString(value, ',', devices);
744 GfxDeviceFamily* deviceIds = BlocklistDevicesToDeviceFamily(devices);
745 if (deviceIds) {
746 // Get GfxDriverInfo to adopt the devices array we created.
747 aDriverInfo.mDeleteDevices = true;
748 aDriverInfo.mDevices = deviceIds;
751 // We explicitly ignore unknown elements.
754 return true;
757 NS_IMETHODIMP
758 GfxInfoBase::Observe(nsISupports* aSubject, const char* aTopic,
759 const char16_t* aData) {
760 if (strcmp(aTopic, "blocklist-data-gfxItems") == 0) {
761 nsTArray<GfxDriverInfo> driverInfo;
762 NS_ConvertUTF16toUTF8 utf8Data(aData);
764 for (const auto& blocklistEntry : utf8Data.Split('\n')) {
765 GfxDriverInfo di;
766 if (BlocklistEntryToDriverInfo(blocklistEntry, di)) {
767 // XXX Changing this to driverInfo.AppendElement(di) causes leaks.
768 // Probably some non-standard semantics of the copy/move operations?
769 *driverInfo.AppendElement() = di;
770 // Prevent di falling out of scope from destroying the devices.
771 di.mDeleteDevices = false;
772 } else {
773 driverInfo.AppendElement();
777 EvaluateDownloadedBlocklist(driverInfo);
780 return NS_OK;
783 GfxInfoBase::GfxInfoBase() : mScreenPixels(INT64_MAX), mMutex("GfxInfoBase") {}
785 GfxInfoBase::~GfxInfoBase() = default;
787 nsresult GfxInfoBase::Init() {
788 InitGfxDriverInfoShutdownObserver();
790 nsCOMPtr<nsIObserverService> os = mozilla::services::GetObserverService();
791 if (os) {
792 os->AddObserver(this, "blocklist-data-gfxItems", true);
795 return NS_OK;
798 void GfxInfoBase::GetData() {
799 if (mScreenPixels != INT64_MAX) {
800 // Already initialized.
801 return;
804 ScreenManager::GetSingleton().GetTotalScreenPixels(&mScreenPixels);
807 NS_IMETHODIMP
808 GfxInfoBase::GetFeatureStatus(int32_t aFeature, nsACString& aFailureId,
809 int32_t* aStatus) {
810 // Ignore the gfx.blocklist.all pref on release and beta.
811 #if defined(RELEASE_OR_BETA)
812 int32_t blocklistAll = 0;
813 #else
814 int32_t blocklistAll = StaticPrefs::gfx_blocklist_all_AtStartup();
815 #endif
816 if (blocklistAll > 0) {
817 gfxCriticalErrorOnce(gfxCriticalError::DefaultOptions(false))
818 << "Forcing blocklisting all features";
819 *aStatus = FEATURE_BLOCKED_DEVICE;
820 aFailureId = "FEATURE_FAILURE_BLOCK_ALL";
821 return NS_OK;
824 if (blocklistAll < 0) {
825 gfxCriticalErrorOnce(gfxCriticalError::DefaultOptions(false))
826 << "Ignoring any feature blocklisting.";
827 *aStatus = FEATURE_STATUS_OK;
828 return NS_OK;
831 // This is how we evaluate the downloadable blocklist. If there is no pref,
832 // then we will fallback to checking the static blocklist.
833 if (GetPrefValueForFeature(aFeature, *aStatus, aFailureId)) {
834 return NS_OK;
837 if (XRE_IsContentProcess() || XRE_IsGPUProcess()) {
838 // Use the cached data received from the parent process.
839 MOZ_ASSERT(sFeatureStatus);
840 bool success = false;
841 for (const auto& fs : *sFeatureStatus) {
842 if (fs.feature() == aFeature) {
843 aFailureId = fs.failureId();
844 *aStatus = fs.status();
845 success = true;
846 break;
849 return success ? NS_OK : NS_ERROR_FAILURE;
852 nsString version;
853 nsTArray<GfxDriverInfo> driverInfo;
854 nsresult rv =
855 GetFeatureStatusImpl(aFeature, aStatus, version, driverInfo, aFailureId);
856 return rv;
859 nsTArray<gfx::GfxInfoFeatureStatus> GfxInfoBase::GetAllFeatures() {
860 MOZ_RELEASE_ASSERT(XRE_IsParentProcess());
861 if (!sFeatureStatus) {
862 InitFeatureStatus(new nsTArray<gfx::GfxInfoFeatureStatus>());
863 for (int32_t i = 1; i <= nsIGfxInfo::FEATURE_MAX_VALUE; ++i) {
864 int32_t status = 0;
865 nsAutoCString failureId;
866 GetFeatureStatus(i, failureId, &status);
867 gfx::GfxInfoFeatureStatus gfxFeatureStatus;
868 gfxFeatureStatus.feature() = i;
869 gfxFeatureStatus.status() = status;
870 gfxFeatureStatus.failureId() = failureId;
871 sFeatureStatus->AppendElement(gfxFeatureStatus);
875 nsTArray<gfx::GfxInfoFeatureStatus> features;
876 for (const auto& status : *sFeatureStatus) {
877 gfx::GfxInfoFeatureStatus copy = status;
878 features.AppendElement(copy);
880 return features;
883 inline bool MatchingAllowStatus(int32_t aStatus) {
884 switch (aStatus) {
885 case nsIGfxInfo::FEATURE_ALLOW_ALWAYS:
886 case nsIGfxInfo::FEATURE_ALLOW_QUALIFIED:
887 return true;
888 default:
889 return false;
893 // Matching OS go somewhat beyond the simple equality check because of the
894 // "All Windows" and "All OS X" variations.
896 // aBlockedOS is describing the system(s) we are trying to block.
897 // aSystemOS is describing the system we are running on.
899 // aSystemOS should not be "Windows" or "OSX" - it should be set to
900 // a particular version instead.
901 // However, it is valid for aBlockedOS to be one of those generic values,
902 // as we could be blocking all of the versions.
903 inline bool MatchingOperatingSystems(OperatingSystem aBlockedOS,
904 OperatingSystem aSystemOS,
905 uint32_t aSystemOSBuild) {
906 MOZ_ASSERT(aSystemOS != OperatingSystem::Windows &&
907 aSystemOS != OperatingSystem::OSX);
909 // If the block entry OS is unknown, it doesn't match
910 if (aBlockedOS == OperatingSystem::Unknown) {
911 return false;
914 #if defined(XP_WIN)
915 if (aBlockedOS == OperatingSystem::Windows) {
916 // We do want even "unknown" aSystemOS to fall under "all windows"
917 return true;
920 constexpr uint32_t kMinWin10BuildNumber = 18362;
921 if (aBlockedOS == OperatingSystem::RecentWindows10 &&
922 aSystemOS == OperatingSystem::Windows10) {
923 // For allowlist purposes, we sometimes want to restrict to only recent
924 // versions of Windows 10. This is a bit of a kludge but easier than adding
925 // complicated blocklist infrastructure for build ID comparisons like driver
926 // versions.
927 return aSystemOSBuild >= kMinWin10BuildNumber;
930 if (aBlockedOS == OperatingSystem::NotRecentWindows10) {
931 if (aSystemOS == OperatingSystem::Windows10) {
932 return aSystemOSBuild < kMinWin10BuildNumber;
933 } else {
934 return true;
937 #endif
939 #if defined(XP_MACOSX)
940 if (aBlockedOS == OperatingSystem::OSX) {
941 // We do want even "unknown" aSystemOS to fall under "all OS X"
942 return true;
944 #endif
946 return aSystemOS == aBlockedOS;
949 inline bool MatchingBattery(BatteryStatus aBatteryStatus, bool aHasBattery) {
950 switch (aBatteryStatus) {
951 case BatteryStatus::All:
952 return true;
953 case BatteryStatus::None:
954 return !aHasBattery;
955 case BatteryStatus::Present:
956 return aHasBattery;
959 MOZ_ASSERT_UNREACHABLE("bad battery status");
960 return false;
963 inline bool MatchingScreenSize(ScreenSizeStatus aScreenStatus,
964 int64_t aScreenPixels) {
965 constexpr int64_t kMaxSmallPixels = 2304000; // 1920x1200
966 constexpr int64_t kMaxMediumPixels = 4953600; // 3440x1440
968 switch (aScreenStatus) {
969 case ScreenSizeStatus::All:
970 return true;
971 case ScreenSizeStatus::Small:
972 return aScreenPixels <= kMaxSmallPixels;
973 case ScreenSizeStatus::SmallAndMedium:
974 return aScreenPixels <= kMaxMediumPixels;
975 case ScreenSizeStatus::Medium:
976 return aScreenPixels > kMaxSmallPixels &&
977 aScreenPixels <= kMaxMediumPixels;
978 case ScreenSizeStatus::MediumAndLarge:
979 return aScreenPixels > kMaxSmallPixels;
980 case ScreenSizeStatus::Large:
981 return aScreenPixels > kMaxMediumPixels;
984 MOZ_ASSERT_UNREACHABLE("bad screen status");
985 return false;
988 int32_t GfxInfoBase::FindBlocklistedDeviceInList(
989 const nsTArray<GfxDriverInfo>& info, nsAString& aSuggestedVersion,
990 int32_t aFeature, nsACString& aFailureId, OperatingSystem os,
991 bool aForAllowing) {
992 int32_t status = nsIGfxInfo::FEATURE_STATUS_UNKNOWN;
994 // Some properties are not available on all platforms.
995 nsAutoString windowProtocol;
996 nsresult rv = GetWindowProtocol(windowProtocol);
997 if (NS_FAILED(rv) && rv != NS_ERROR_NOT_IMPLEMENTED) {
998 return 0;
1001 bool hasBattery = false;
1002 rv = GetHasBattery(&hasBattery);
1003 if (NS_FAILED(rv) && rv != NS_ERROR_NOT_IMPLEMENTED) {
1004 return 0;
1007 uint32_t osBuild = OperatingSystemBuild();
1009 // Get the adapters once then reuse below
1010 nsAutoString adapterVendorID[2];
1011 nsAutoString adapterDeviceID[2];
1012 nsAutoString adapterDriverVendor[2];
1013 nsAutoString adapterDriverVersionString[2];
1014 bool adapterInfoFailed[2];
1016 adapterInfoFailed[0] =
1017 (NS_FAILED(GetAdapterVendorID(adapterVendorID[0])) ||
1018 NS_FAILED(GetAdapterDeviceID(adapterDeviceID[0])) ||
1019 NS_FAILED(GetAdapterDriverVendor(adapterDriverVendor[0])) ||
1020 NS_FAILED(GetAdapterDriverVersion(adapterDriverVersionString[0])));
1021 adapterInfoFailed[1] =
1022 (NS_FAILED(GetAdapterVendorID2(adapterVendorID[1])) ||
1023 NS_FAILED(GetAdapterDeviceID2(adapterDeviceID[1])) ||
1024 NS_FAILED(GetAdapterDriverVendor2(adapterDriverVendor[1])) ||
1025 NS_FAILED(GetAdapterDriverVersion2(adapterDriverVersionString[1])));
1026 // No point in going on if we don't have adapter info
1027 if (adapterInfoFailed[0] && adapterInfoFailed[1]) {
1028 return 0;
1031 #if defined(XP_WIN) || defined(ANDROID) || defined(MOZ_WIDGET_GTK)
1032 uint64_t driverVersion[2] = {0, 0};
1033 if (!adapterInfoFailed[0]) {
1034 ParseDriverVersion(adapterDriverVersionString[0], &driverVersion[0]);
1036 if (!adapterInfoFailed[1]) {
1037 ParseDriverVersion(adapterDriverVersionString[1], &driverVersion[1]);
1039 #endif
1041 uint32_t i = 0;
1042 for (; i < info.Length(); i++) {
1043 // If the status is FEATURE_ALLOW_*, then it is for the allowlist, not
1044 // blocklisting. Only consider entries for our search mode.
1045 if (MatchingAllowStatus(info[i].mFeatureStatus) != aForAllowing) {
1046 continue;
1049 // If we don't have the info for this GPU, no need to check further.
1050 // It is unclear that we would ever have a mixture of 1st and 2nd
1051 // GPU, but leaving the code in for that possibility for now.
1052 // (Actually, currently mGpu2 will never be true, so this can
1053 // be optimized out.)
1054 uint32_t infoIndex = info[i].mGpu2 ? 1 : 0;
1055 if (adapterInfoFailed[infoIndex]) {
1056 continue;
1059 // Do the operating system check first, no point in getting the driver
1060 // info if we won't need to use it.
1061 if (!MatchingOperatingSystems(info[i].mOperatingSystem, os, osBuild)) {
1062 continue;
1065 if (info[i].mOperatingSystemVersion &&
1066 info[i].mOperatingSystemVersion != OperatingSystemVersion()) {
1067 continue;
1070 if (!MatchingBattery(info[i].mBattery, hasBattery)) {
1071 continue;
1074 if (!MatchingScreenSize(info[i].mScreen, mScreenPixels)) {
1075 continue;
1078 if (!DoesWindowProtocolMatch(info[i].mWindowProtocol, windowProtocol)) {
1079 continue;
1082 if (!DoesVendorMatch(info[i].mAdapterVendor, adapterVendorID[infoIndex])) {
1083 continue;
1086 if (!DoesDriverVendorMatch(info[i].mDriverVendor,
1087 adapterDriverVendor[infoIndex])) {
1088 continue;
1091 if (info[i].mDevices && !info[i].mDevices->IsEmpty()) {
1092 nsresult rv = info[i].mDevices->Contains(adapterDeviceID[infoIndex]);
1093 if (rv == NS_ERROR_NOT_AVAILABLE) {
1094 // Not found
1095 continue;
1097 if (rv != NS_OK) {
1098 // Failed to search, allowlist should not match, blocklist should match
1099 // for safety reasons
1100 if (aForAllowing) {
1101 continue;
1103 break;
1107 bool match = false;
1109 if (!info[i].mHardware.IsEmpty() && !info[i].mHardware.Equals(Hardware())) {
1110 continue;
1112 if (!info[i].mModel.IsEmpty() && !info[i].mModel.Equals(Model())) {
1113 continue;
1115 if (!info[i].mProduct.IsEmpty() && !info[i].mProduct.Equals(Product())) {
1116 continue;
1118 if (!info[i].mManufacturer.IsEmpty() &&
1119 !info[i].mManufacturer.Equals(Manufacturer())) {
1120 continue;
1123 #if defined(XP_WIN) || defined(ANDROID) || defined(MOZ_WIDGET_GTK)
1124 switch (info[i].mComparisonOp) {
1125 case DRIVER_LESS_THAN:
1126 match = driverVersion[infoIndex] < info[i].mDriverVersion;
1127 break;
1128 case DRIVER_BUILD_ID_LESS_THAN:
1129 match = (driverVersion[infoIndex] & 0xFFFF) < info[i].mDriverVersion;
1130 break;
1131 case DRIVER_LESS_THAN_OR_EQUAL:
1132 match = driverVersion[infoIndex] <= info[i].mDriverVersion;
1133 break;
1134 case DRIVER_BUILD_ID_LESS_THAN_OR_EQUAL:
1135 match = (driverVersion[infoIndex] & 0xFFFF) <= info[i].mDriverVersion;
1136 break;
1137 case DRIVER_GREATER_THAN:
1138 match = driverVersion[infoIndex] > info[i].mDriverVersion;
1139 break;
1140 case DRIVER_GREATER_THAN_OR_EQUAL:
1141 match = driverVersion[infoIndex] >= info[i].mDriverVersion;
1142 break;
1143 case DRIVER_EQUAL:
1144 match = driverVersion[infoIndex] == info[i].mDriverVersion;
1145 break;
1146 case DRIVER_NOT_EQUAL:
1147 match = driverVersion[infoIndex] != info[i].mDriverVersion;
1148 break;
1149 case DRIVER_BETWEEN_EXCLUSIVE:
1150 match = driverVersion[infoIndex] > info[i].mDriverVersion &&
1151 driverVersion[infoIndex] < info[i].mDriverVersionMax;
1152 break;
1153 case DRIVER_BETWEEN_INCLUSIVE:
1154 match = driverVersion[infoIndex] >= info[i].mDriverVersion &&
1155 driverVersion[infoIndex] <= info[i].mDriverVersionMax;
1156 break;
1157 case DRIVER_BETWEEN_INCLUSIVE_START:
1158 match = driverVersion[infoIndex] >= info[i].mDriverVersion &&
1159 driverVersion[infoIndex] < info[i].mDriverVersionMax;
1160 break;
1161 case DRIVER_COMPARISON_IGNORED:
1162 // We don't have a comparison op, so we match everything.
1163 match = true;
1164 break;
1165 default:
1166 NS_WARNING("Bogus op in GfxDriverInfo");
1167 break;
1169 #else
1170 // We don't care what driver version it was. We only check OS version and if
1171 // the device matches.
1172 match = true;
1173 #endif
1175 if (match || info[i].mDriverVersion == GfxDriverInfo::allDriverVersions) {
1176 if (info[i].mFeature == GfxDriverInfo::allFeatures ||
1177 info[i].mFeature == aFeature) {
1178 status = info[i].mFeatureStatus;
1179 if (!info[i].mRuleId.IsEmpty()) {
1180 aFailureId = info[i].mRuleId.get();
1181 } else {
1182 aFailureId = "FEATURE_FAILURE_DL_BLOCKLIST_NO_ID";
1184 break;
1189 #if defined(XP_WIN)
1190 // As a very special case, we block D2D on machines with an NVidia 310M GPU
1191 // as either the primary or secondary adapter. D2D is also blocked when the
1192 // NV 310M is the primary adapter (using the standard blocklisting mechanism).
1193 // If the primary GPU already matched something in the blocklist then we
1194 // ignore this special rule. See bug 1008759.
1195 if (status == nsIGfxInfo::FEATURE_STATUS_UNKNOWN &&
1196 (aFeature == nsIGfxInfo::FEATURE_DIRECT2D)) {
1197 if (!adapterInfoFailed[1]) {
1198 nsAString& nvVendorID =
1199 (nsAString&)GfxDriverInfo::GetDeviceVendor(DeviceVendor::NVIDIA);
1200 const nsString nv310mDeviceId = u"0x0A70"_ns;
1201 if (nvVendorID.Equals(adapterVendorID[1],
1202 nsCaseInsensitiveStringComparator) &&
1203 nv310mDeviceId.Equals(adapterDeviceID[1],
1204 nsCaseInsensitiveStringComparator)) {
1205 status = nsIGfxInfo::FEATURE_BLOCKED_DEVICE;
1206 aFailureId = "FEATURE_FAILURE_D2D_NV310M_BLOCK";
1211 // Depends on Windows driver versioning. We don't pass a GfxDriverInfo object
1212 // back to the Windows handler, so we must handle this here.
1213 if (status == FEATURE_BLOCKED_DRIVER_VERSION) {
1214 if (info[i].mSuggestedVersion) {
1215 aSuggestedVersion.AppendPrintf("%s", info[i].mSuggestedVersion);
1216 } else if (info[i].mComparisonOp == DRIVER_LESS_THAN &&
1217 info[i].mDriverVersion != GfxDriverInfo::allDriverVersions) {
1218 aSuggestedVersion.AppendPrintf(
1219 "%lld.%lld.%lld.%lld",
1220 (info[i].mDriverVersion & 0xffff000000000000) >> 48,
1221 (info[i].mDriverVersion & 0x0000ffff00000000) >> 32,
1222 (info[i].mDriverVersion & 0x00000000ffff0000) >> 16,
1223 (info[i].mDriverVersion & 0x000000000000ffff));
1226 #endif
1228 return status;
1231 void GfxInfoBase::SetFeatureStatus(nsTArray<gfx::GfxInfoFeatureStatus>&& aFS) {
1232 MOZ_ASSERT(!sFeatureStatus);
1233 InitFeatureStatus(new nsTArray<gfx::GfxInfoFeatureStatus>(std::move(aFS)));
1236 bool GfxInfoBase::DoesWindowProtocolMatch(
1237 const nsAString& aBlocklistWindowProtocol,
1238 const nsAString& aWindowProtocol) {
1239 return aBlocklistWindowProtocol.Equals(aWindowProtocol,
1240 nsCaseInsensitiveStringComparator) ||
1241 aBlocklistWindowProtocol.Equals(
1242 GfxDriverInfo::GetWindowProtocol(WindowProtocol::All),
1243 nsCaseInsensitiveStringComparator);
1246 bool GfxInfoBase::DoesVendorMatch(const nsAString& aBlocklistVendor,
1247 const nsAString& aAdapterVendor) {
1248 return aBlocklistVendor.Equals(aAdapterVendor,
1249 nsCaseInsensitiveStringComparator) ||
1250 aBlocklistVendor.Equals(
1251 GfxDriverInfo::GetDeviceVendor(DeviceVendor::All),
1252 nsCaseInsensitiveStringComparator);
1255 bool GfxInfoBase::DoesDriverVendorMatch(const nsAString& aBlocklistVendor,
1256 const nsAString& aDriverVendor) {
1257 return aBlocklistVendor.Equals(aDriverVendor,
1258 nsCaseInsensitiveStringComparator) ||
1259 aBlocklistVendor.Equals(
1260 GfxDriverInfo::GetDriverVendor(DriverVendor::All),
1261 nsCaseInsensitiveStringComparator);
1264 bool GfxInfoBase::IsFeatureAllowlisted(int32_t aFeature) const {
1265 return aFeature == nsIGfxInfo::FEATURE_VIDEO_OVERLAY ||
1266 aFeature == nsIGfxInfo::FEATURE_HW_DECODED_VIDEO_ZERO_COPY;
1269 nsresult GfxInfoBase::GetFeatureStatusImpl(
1270 int32_t aFeature, int32_t* aStatus, nsAString& aSuggestedVersion,
1271 const nsTArray<GfxDriverInfo>& aDriverInfo, nsACString& aFailureId,
1272 OperatingSystem* aOS /* = nullptr */) {
1273 if (aFeature <= 0) {
1274 gfxWarning() << "Invalid feature <= 0";
1275 return NS_OK;
1278 if (*aStatus != nsIGfxInfo::FEATURE_STATUS_UNKNOWN) {
1279 // Terminate now with the status determined by the derived type (OS-specific
1280 // code).
1281 return NS_OK;
1284 if (sShutdownOccurred) {
1285 // This is futile; we've already commenced shutdown and our blocklists have
1286 // been deleted. We may want to look into resurrecting the blocklist instead
1287 // but for now, just don't even go there.
1288 return NS_OK;
1291 // Ensure any additional initialization required is complete.
1292 GetData();
1294 // If an operating system was provided by the derived GetFeatureStatusImpl,
1295 // grab it here. Otherwise, the OS is unknown.
1296 OperatingSystem os = (aOS ? *aOS : OperatingSystem::Unknown);
1298 nsAutoString adapterVendorID;
1299 nsAutoString adapterDeviceID;
1300 nsAutoString adapterDriverVersionString;
1301 if (NS_FAILED(GetAdapterVendorID(adapterVendorID)) ||
1302 NS_FAILED(GetAdapterDeviceID(adapterDeviceID)) ||
1303 NS_FAILED(GetAdapterDriverVersion(adapterDriverVersionString))) {
1304 if (OnlyAllowFeatureOnKnownConfig(aFeature)) {
1305 aFailureId = "FEATURE_FAILURE_CANT_RESOLVE_ADAPTER";
1306 *aStatus = nsIGfxInfo::FEATURE_BLOCKED_DEVICE;
1307 } else {
1308 *aStatus = nsIGfxInfo::FEATURE_STATUS_OK;
1310 return NS_OK;
1313 // We only check either the given blocklist, or the static list, as given.
1314 int32_t status;
1315 if (aDriverInfo.Length()) {
1316 status =
1317 FindBlocklistedDeviceInList(aDriverInfo, aSuggestedVersion, aFeature,
1318 aFailureId, os, /* aForAllowing */ false);
1319 } else {
1320 if (!sDriverInfo) {
1321 sDriverInfo = new nsTArray<GfxDriverInfo>();
1323 status = FindBlocklistedDeviceInList(GetGfxDriverInfo(), aSuggestedVersion,
1324 aFeature, aFailureId, os,
1325 /* aForAllowing */ false);
1328 if (status == nsIGfxInfo::FEATURE_STATUS_UNKNOWN) {
1329 if (IsFeatureAllowlisted(aFeature)) {
1330 // This feature is actually using the allowlist; that means after we pass
1331 // the blocklist to prevent us explicitly from getting the feature, we now
1332 // need to check the allowlist to ensure we are allowed to get it in the
1333 // first place.
1334 if (aDriverInfo.Length()) {
1335 status = FindBlocklistedDeviceInList(aDriverInfo, aSuggestedVersion,
1336 aFeature, aFailureId, os,
1337 /* aForAllowing */ true);
1338 } else {
1339 status = FindBlocklistedDeviceInList(
1340 GetGfxDriverInfo(), aSuggestedVersion, aFeature, aFailureId, os,
1341 /* aForAllowing */ true);
1344 if (status == nsIGfxInfo::FEATURE_STATUS_UNKNOWN) {
1345 status = nsIGfxInfo::FEATURE_DENIED;
1347 } else {
1348 // It's now done being processed. It's safe to set the status to
1349 // STATUS_OK.
1350 status = nsIGfxInfo::FEATURE_STATUS_OK;
1354 *aStatus = status;
1355 return NS_OK;
1358 NS_IMETHODIMP
1359 GfxInfoBase::GetFeatureSuggestedDriverVersion(int32_t aFeature,
1360 nsAString& aVersion) {
1361 nsCString version;
1362 if (GetPrefValueForDriverVersion(version)) {
1363 aVersion = NS_ConvertASCIItoUTF16(version);
1364 return NS_OK;
1367 int32_t status;
1368 nsCString discardFailureId;
1369 nsTArray<GfxDriverInfo> driverInfo;
1370 return GetFeatureStatusImpl(aFeature, &status, aVersion, driverInfo,
1371 discardFailureId);
1374 void GfxInfoBase::EvaluateDownloadedBlocklist(
1375 nsTArray<GfxDriverInfo>& aDriverInfo) {
1376 // If the list is empty, then we don't actually want to call
1377 // GetFeatureStatusImpl since we will use the static list instead. In that
1378 // case, all we want to do is make sure the pref is removed.
1379 if (aDriverInfo.IsEmpty()) {
1380 gfxCriticalNoteOnce << "Evaluate empty downloaded blocklist";
1381 return;
1384 OperatingSystem os = GetOperatingSystem();
1386 // For every feature we know about, we evaluate whether this blocklist has a
1387 // non-STATUS_OK status. If it does, we set the pref we evaluate in
1388 // GetFeatureStatus above, so we don't need to hold on to this blocklist
1389 // anywhere permanent.
1390 for (int feature = 1; feature <= nsIGfxInfo::FEATURE_MAX_VALUE; ++feature) {
1391 int32_t status = nsIGfxInfo::FEATURE_STATUS_UNKNOWN;
1392 nsCString failureId;
1393 nsAutoString suggestedVersion;
1395 // Note that we are careful to call the base class method since we only want
1396 // to evaluate the downloadable blocklist for these prefs.
1397 MOZ_ALWAYS_TRUE(NS_SUCCEEDED(GfxInfoBase::GetFeatureStatusImpl(
1398 feature, &status, suggestedVersion, aDriverInfo, failureId, &os)));
1400 switch (status) {
1401 default:
1402 MOZ_FALLTHROUGH_ASSERT("Unhandled feature status!");
1403 case nsIGfxInfo::FEATURE_STATUS_UNKNOWN:
1404 // This may be returned during shutdown or for invalid features.
1405 case nsIGfxInfo::FEATURE_ALLOW_ALWAYS:
1406 case nsIGfxInfo::FEATURE_ALLOW_QUALIFIED:
1407 case nsIGfxInfo::FEATURE_DENIED:
1408 // We cannot use the downloadable blocklist to control the allowlist.
1409 // If a feature is allowlisted, then we should also ignore DENIED
1410 // statuses from GetFeatureStatusImpl because we don't check the
1411 // static list when and this is an expected value. If we wish to
1412 // override the allowlist, it is as simple as creating a normal
1413 // blocklist rule with a BLOCKED* status code.
1414 case nsIGfxInfo::FEATURE_STATUS_OK:
1415 RemovePrefForFeature(feature);
1416 break;
1418 case nsIGfxInfo::FEATURE_BLOCKED_DRIVER_VERSION:
1419 if (!suggestedVersion.IsEmpty()) {
1420 SetPrefValueForDriverVersion(suggestedVersion);
1421 } else {
1422 RemovePrefForDriverVersion();
1424 [[fallthrough]];
1426 case nsIGfxInfo::FEATURE_BLOCKED_MISMATCHED_VERSION:
1427 case nsIGfxInfo::FEATURE_BLOCKED_DEVICE:
1428 case nsIGfxInfo::FEATURE_DISCOURAGED:
1429 case nsIGfxInfo::FEATURE_BLOCKED_OS_VERSION:
1430 case nsIGfxInfo::FEATURE_BLOCKED_PLATFORM_TEST:
1431 SetPrefValueForFeature(feature, status, failureId);
1432 break;
1437 NS_IMETHODIMP_(void)
1438 GfxInfoBase::LogFailure(const nsACString& failure) {
1439 // gfxCriticalError has a mutex lock of its own, so we may not actually
1440 // need this lock. ::GetFailures() accesses the data but the LogForwarder
1441 // will not return the copy of the logs unless it can get the same lock
1442 // that gfxCriticalError uses. Still, that is so much of an implementation
1443 // detail that it's nicer to just add an extra lock here and in
1444 // ::GetFailures()
1445 MutexAutoLock lock(mMutex);
1447 // By default, gfxCriticalError asserts; make it not assert in this case.
1448 gfxCriticalError(CriticalLog::DefaultOptions(false))
1449 << "(LF) " << failure.BeginReading();
1452 NS_IMETHODIMP GfxInfoBase::GetFailures(nsTArray<int32_t>& indices,
1453 nsTArray<nsCString>& failures) {
1454 MutexAutoLock lock(mMutex);
1456 LogForwarder* logForwarder = Factory::GetLogForwarder();
1457 if (!logForwarder) {
1458 return NS_ERROR_UNEXPECTED;
1461 // There are two string copies in this method, starting with this one. We are
1462 // assuming this is not a big deal, as the size of the array should be small
1463 // and the strings in it should be small as well (the error messages in the
1464 // code.) The second copy happens with the AppendElement() calls.
1465 // Technically, we don't need the mutex lock after the StringVectorCopy()
1466 // call.
1467 LoggingRecord loggedStrings = logForwarder->LoggingRecordCopy();
1468 LoggingRecord::const_iterator it;
1469 for (it = loggedStrings.begin(); it != loggedStrings.end(); ++it) {
1470 failures.AppendElement(nsDependentCSubstring(std::get<1>(*it).c_str(),
1471 std::get<1>(*it).size()));
1472 indices.AppendElement(std::get<0>(*it));
1475 return NS_OK;
1478 nsTArray<GfxInfoCollectorBase*>* sCollectors;
1480 static void InitCollectors() {
1481 if (!sCollectors) sCollectors = new nsTArray<GfxInfoCollectorBase*>;
1484 nsresult GfxInfoBase::GetInfo(JSContext* aCx,
1485 JS::MutableHandle<JS::Value> aResult) {
1486 InitCollectors();
1487 InfoObject obj(aCx);
1489 for (uint32_t i = 0; i < sCollectors->Length(); i++) {
1490 (*sCollectors)[i]->GetInfo(obj);
1493 // Some example property definitions
1494 // obj.DefineProperty("wordCacheSize", gfxTextRunWordCache::Count());
1495 // obj.DefineProperty("renderer", mRendererIDsString);
1496 // obj.DefineProperty("five", 5);
1498 if (!obj.mOk) {
1499 return NS_ERROR_FAILURE;
1502 aResult.setObject(*obj.mObj);
1503 return NS_OK;
1506 nsAutoCString gBaseAppVersion;
1508 const nsCString& GfxInfoBase::GetApplicationVersion() {
1509 static bool versionInitialized = false;
1510 if (!versionInitialized) {
1511 // If we fail to get the version, we will not try again.
1512 versionInitialized = true;
1514 // Get the version from xpcom/system/nsIXULAppInfo.idl
1515 nsCOMPtr<nsIXULAppInfo> app = do_GetService("@mozilla.org/xre/app-info;1");
1516 if (app) {
1517 app->GetVersion(gBaseAppVersion);
1520 return gBaseAppVersion;
1523 /* static */ bool GfxInfoBase::OnlyAllowFeatureOnKnownConfig(int32_t aFeature) {
1524 switch (aFeature) {
1525 // The GPU process doesn't need hardware acceleration and can run on
1526 // devices that we normally block from not being on our whitelist.
1527 case nsIGfxInfo::FEATURE_GPU_PROCESS:
1528 return kIsAndroid;
1529 // We can mostly assume that ANGLE will work
1530 case nsIGfxInfo::FEATURE_DIRECT3D_11_ANGLE:
1531 // Remote WebGL is needed for Win32k Lockdown, so it should be enabled
1532 // regardless of HW support or not
1533 case nsIGfxInfo::FEATURE_ALLOW_WEBGL_OUT_OF_PROCESS:
1534 // Backdrop filter should generally work, especially if we fall back to
1535 // Software WebRender because of an unknown vendor.
1536 case nsIGfxInfo::FEATURE_BACKDROP_FILTER:
1537 return false;
1538 default:
1539 return true;
1543 void GfxInfoBase::AddCollector(GfxInfoCollectorBase* collector) {
1544 InitCollectors();
1545 sCollectors->AppendElement(collector);
1548 void GfxInfoBase::RemoveCollector(GfxInfoCollectorBase* collector) {
1549 InitCollectors();
1550 for (uint32_t i = 0; i < sCollectors->Length(); i++) {
1551 if ((*sCollectors)[i] == collector) {
1552 sCollectors->RemoveElementAt(i);
1553 break;
1556 if (sCollectors->IsEmpty()) {
1557 delete sCollectors;
1558 sCollectors = nullptr;
1562 static void AppendMonitor(JSContext* aCx, widget::Screen& aScreen,
1563 JS::Handle<JSObject*> aOutArray, int32_t aIndex) {
1564 JS::Rooted<JSObject*> obj(aCx, JS_NewPlainObject(aCx));
1566 auto screenSize = aScreen.GetRect().Size();
1568 JS::Rooted<JS::Value> screenWidth(aCx, JS::Int32Value(screenSize.width));
1569 JS_SetProperty(aCx, obj, "screenWidth", screenWidth);
1571 JS::Rooted<JS::Value> screenHeight(aCx, JS::Int32Value(screenSize.height));
1572 JS_SetProperty(aCx, obj, "screenHeight", screenHeight);
1574 // XXX Just preserving behavior since this is exposed to telemetry, but we
1575 // could consider including this everywhere.
1576 #ifdef XP_MACOSX
1577 JS::Rooted<JS::Value> scale(
1578 aCx, JS::NumberValue(aScreen.GetContentsScaleFactor()));
1579 JS_SetProperty(aCx, obj, "scale", scale);
1580 #endif
1582 #ifdef XP_WIN
1583 JS::Rooted<JS::Value> refreshRate(aCx,
1584 JS::Int32Value(aScreen.GetRefreshRate()));
1585 JS_SetProperty(aCx, obj, "refreshRate", refreshRate);
1587 JS::Rooted<JS::Value> pseudoDisplay(
1588 aCx, JS::BooleanValue(aScreen.GetIsPseudoDisplay()));
1589 JS_SetProperty(aCx, obj, "pseudoDisplay", pseudoDisplay);
1590 #endif
1592 JS::Rooted<JS::Value> element(aCx, JS::ObjectValue(*obj));
1593 JS_SetElement(aCx, aOutArray, aIndex, element);
1596 nsresult GfxInfoBase::FindMonitors(JSContext* aCx,
1597 JS::Handle<JSObject*> aOutArray) {
1598 int32_t index = 0;
1599 auto& sm = ScreenManager::GetSingleton();
1600 for (auto& screen : sm.CurrentScreenList()) {
1601 AppendMonitor(aCx, *screen, aOutArray, index++);
1604 if (index == 0) {
1605 // Ensure we return at least one monitor, this is needed for xpcshell.
1606 RefPtr<Screen> screen = sm.GetPrimaryScreen();
1607 AppendMonitor(aCx, *screen, aOutArray, index++);
1610 return NS_OK;
1613 NS_IMETHODIMP
1614 GfxInfoBase::GetMonitors(JSContext* aCx, JS::MutableHandle<JS::Value> aResult) {
1615 JS::Rooted<JSObject*> array(aCx, JS::NewArrayObject(aCx, 0));
1617 nsresult rv = FindMonitors(aCx, array);
1618 if (NS_FAILED(rv)) {
1619 return rv;
1622 aResult.setObject(*array);
1623 return NS_OK;
1626 static inline bool SetJSPropertyString(JSContext* aCx,
1627 JS::Handle<JSObject*> aObj,
1628 const char* aProp, const char* aString) {
1629 JS::Rooted<JSString*> str(aCx, JS_NewStringCopyZ(aCx, aString));
1630 if (!str) {
1631 return false;
1634 JS::Rooted<JS::Value> val(aCx, JS::StringValue(str));
1635 return JS_SetProperty(aCx, aObj, aProp, val);
1638 template <typename T>
1639 static inline bool AppendJSElement(JSContext* aCx, JS::Handle<JSObject*> aObj,
1640 const T& aValue) {
1641 uint32_t index;
1642 if (!JS::GetArrayLength(aCx, aObj, &index)) {
1643 return false;
1645 return JS_SetElement(aCx, aObj, index, aValue);
1648 nsresult GfxInfoBase::GetFeatures(JSContext* aCx,
1649 JS::MutableHandle<JS::Value> aOut) {
1650 JS::Rooted<JSObject*> obj(aCx, JS_NewPlainObject(aCx));
1651 if (!obj) {
1652 return NS_ERROR_OUT_OF_MEMORY;
1654 aOut.setObject(*obj);
1656 layers::LayersBackend backend =
1657 gfxPlatform::Initialized()
1658 ? gfxPlatform::GetPlatform()->GetCompositorBackend()
1659 : layers::LayersBackend::LAYERS_NONE;
1660 const char* backendName = layers::GetLayersBackendName(backend);
1661 SetJSPropertyString(aCx, obj, "compositor", backendName);
1663 // If graphics isn't initialized yet, just stop now.
1664 if (!gfxPlatform::Initialized()) {
1665 return NS_OK;
1668 DescribeFeatures(aCx, obj);
1669 return NS_OK;
1672 nsresult GfxInfoBase::GetFeatureLog(JSContext* aCx,
1673 JS::MutableHandle<JS::Value> aOut) {
1674 JS::Rooted<JSObject*> containerObj(aCx, JS_NewPlainObject(aCx));
1675 if (!containerObj) {
1676 return NS_ERROR_OUT_OF_MEMORY;
1678 aOut.setObject(*containerObj);
1680 JS::Rooted<JSObject*> featureArray(aCx, JS::NewArrayObject(aCx, 0));
1681 if (!featureArray) {
1682 return NS_ERROR_OUT_OF_MEMORY;
1685 // Collect features.
1686 gfxConfig::ForEachFeature([&](const char* aName, const char* aDescription,
1687 FeatureState& aFeature) -> void {
1688 JS::Rooted<JSObject*> obj(aCx, JS_NewPlainObject(aCx));
1689 if (!obj) {
1690 return;
1692 if (!SetJSPropertyString(aCx, obj, "name", aName) ||
1693 !SetJSPropertyString(aCx, obj, "description", aDescription) ||
1694 !SetJSPropertyString(aCx, obj, "status",
1695 FeatureStatusToString(aFeature.GetValue()))) {
1696 return;
1699 JS::Rooted<JS::Value> log(aCx);
1700 if (!BuildFeatureStateLog(aCx, aFeature, &log)) {
1701 return;
1703 if (!JS_SetProperty(aCx, obj, "log", log)) {
1704 return;
1707 if (!AppendJSElement(aCx, featureArray, obj)) {
1708 return;
1712 JS::Rooted<JSObject*> fallbackArray(aCx, JS::NewArrayObject(aCx, 0));
1713 if (!fallbackArray) {
1714 return NS_ERROR_OUT_OF_MEMORY;
1717 // Collect fallbacks.
1718 gfxConfig::ForEachFallback(
1719 [&](const char* aName, const char* aMessage) -> void {
1720 JS::Rooted<JSObject*> obj(aCx, JS_NewPlainObject(aCx));
1721 if (!obj) {
1722 return;
1725 if (!SetJSPropertyString(aCx, obj, "name", aName) ||
1726 !SetJSPropertyString(aCx, obj, "message", aMessage)) {
1727 return;
1730 if (!AppendJSElement(aCx, fallbackArray, obj)) {
1731 return;
1735 JS::Rooted<JS::Value> val(aCx);
1737 val = JS::ObjectValue(*featureArray);
1738 JS_SetProperty(aCx, containerObj, "features", val);
1740 val = JS::ObjectValue(*fallbackArray);
1741 JS_SetProperty(aCx, containerObj, "fallbacks", val);
1743 return NS_OK;
1746 bool GfxInfoBase::BuildFeatureStateLog(JSContext* aCx,
1747 const FeatureState& aFeature,
1748 JS::MutableHandle<JS::Value> aOut) {
1749 JS::Rooted<JSObject*> log(aCx, JS::NewArrayObject(aCx, 0));
1750 if (!log) {
1751 return false;
1753 aOut.setObject(*log);
1755 aFeature.ForEachStatusChange([&](const char* aType, FeatureStatus aStatus,
1756 const char* aMessage,
1757 const nsCString& aFailureId) -> void {
1758 JS::Rooted<JSObject*> obj(aCx, JS_NewPlainObject(aCx));
1759 if (!obj) {
1760 return;
1763 if (!SetJSPropertyString(aCx, obj, "type", aType) ||
1764 !SetJSPropertyString(aCx, obj, "status",
1765 FeatureStatusToString(aStatus)) ||
1766 (!aFailureId.IsEmpty() &&
1767 !SetJSPropertyString(aCx, obj, "failureId", aFailureId.get())) ||
1768 (aMessage && !SetJSPropertyString(aCx, obj, "message", aMessage))) {
1769 return;
1772 if (!AppendJSElement(aCx, log, obj)) {
1773 return;
1777 return true;
1780 void GfxInfoBase::DescribeFeatures(JSContext* aCx, JS::Handle<JSObject*> aObj) {
1781 JS::Rooted<JSObject*> obj(aCx);
1783 gfx::FeatureState& hwCompositing =
1784 gfxConfig::GetFeature(gfx::Feature::HW_COMPOSITING);
1785 InitFeatureObject(aCx, aObj, "hwCompositing", hwCompositing, &obj);
1787 gfx::FeatureState& gpuProcess =
1788 gfxConfig::GetFeature(gfx::Feature::GPU_PROCESS);
1789 InitFeatureObject(aCx, aObj, "gpuProcess", gpuProcess, &obj);
1791 gfx::FeatureState& webrender = gfxConfig::GetFeature(gfx::Feature::WEBRENDER);
1792 InitFeatureObject(aCx, aObj, "webrender", webrender, &obj);
1794 gfx::FeatureState& wrCompositor =
1795 gfxConfig::GetFeature(gfx::Feature::WEBRENDER_COMPOSITOR);
1796 InitFeatureObject(aCx, aObj, "wrCompositor", wrCompositor, &obj);
1798 gfx::FeatureState& openglCompositing =
1799 gfxConfig::GetFeature(gfx::Feature::OPENGL_COMPOSITING);
1800 InitFeatureObject(aCx, aObj, "openglCompositing", openglCompositing, &obj);
1802 gfx::FeatureState& omtp = gfxConfig::GetFeature(gfx::Feature::OMTP);
1803 InitFeatureObject(aCx, aObj, "omtp", omtp, &obj);
1806 bool GfxInfoBase::InitFeatureObject(JSContext* aCx,
1807 JS::Handle<JSObject*> aContainer,
1808 const char* aName,
1809 mozilla::gfx::FeatureState& aFeatureState,
1810 JS::MutableHandle<JSObject*> aOutObj) {
1811 JS::Rooted<JSObject*> obj(aCx, JS_NewPlainObject(aCx));
1812 if (!obj) {
1813 return false;
1816 nsCString status = aFeatureState.GetStatusAndFailureIdString();
1818 JS::Rooted<JSString*> str(aCx, JS_NewStringCopyZ(aCx, status.get()));
1819 JS::Rooted<JS::Value> val(aCx, JS::StringValue(str));
1820 JS_SetProperty(aCx, obj, "status", val);
1822 // Add the feature object to the container.
1824 JS::Rooted<JS::Value> val(aCx, JS::ObjectValue(*obj));
1825 JS_SetProperty(aCx, aContainer, aName, val);
1828 aOutObj.set(obj);
1829 return true;
1832 nsresult GfxInfoBase::GetActiveCrashGuards(JSContext* aCx,
1833 JS::MutableHandle<JS::Value> aOut) {
1834 JS::Rooted<JSObject*> array(aCx, JS::NewArrayObject(aCx, 0));
1835 if (!array) {
1836 return NS_ERROR_OUT_OF_MEMORY;
1838 aOut.setObject(*array);
1840 DriverCrashGuard::ForEachActiveCrashGuard(
1841 [&](const char* aName, const char* aPrefName) -> void {
1842 JS::Rooted<JSObject*> obj(aCx, JS_NewPlainObject(aCx));
1843 if (!obj) {
1844 return;
1846 if (!SetJSPropertyString(aCx, obj, "type", aName)) {
1847 return;
1849 if (!SetJSPropertyString(aCx, obj, "prefName", aPrefName)) {
1850 return;
1852 if (!AppendJSElement(aCx, array, obj)) {
1853 return;
1857 return NS_OK;
1860 NS_IMETHODIMP
1861 GfxInfoBase::GetTargetFrameRate(uint32_t* aTargetFrameRate) {
1862 *aTargetFrameRate = gfxPlatform::TargetFrameRate();
1863 return NS_OK;
1866 NS_IMETHODIMP
1867 GfxInfoBase::GetCodecSupportInfo(nsACString& aCodecSupportInfo) {
1868 aCodecSupportInfo.Assign(gfx::gfxVars::CodecSupportInfo());
1869 return NS_OK;
1872 NS_IMETHODIMP
1873 GfxInfoBase::GetIsHeadless(bool* aIsHeadless) {
1874 *aIsHeadless = gfxPlatform::IsHeadless();
1875 return NS_OK;
1878 NS_IMETHODIMP
1879 GfxInfoBase::GetContentBackend(nsAString& aContentBackend) {
1880 BackendType backend = gfxPlatform::GetPlatform()->GetDefaultContentBackend();
1881 nsString outStr;
1883 switch (backend) {
1884 case BackendType::DIRECT2D1_1: {
1885 outStr.AppendPrintf("Direct2D 1.1");
1886 break;
1888 case BackendType::SKIA: {
1889 outStr.AppendPrintf("Skia");
1890 break;
1892 case BackendType::CAIRO: {
1893 outStr.AppendPrintf("Cairo");
1894 break;
1896 default:
1897 return NS_ERROR_FAILURE;
1900 aContentBackend.Assign(outStr);
1901 return NS_OK;
1904 NS_IMETHODIMP
1905 GfxInfoBase::GetAzureCanvasBackend(nsAString& aBackend) {
1906 CopyASCIItoUTF16(mozilla::MakeStringSpan(
1907 gfxPlatform::GetPlatform()->GetAzureCanvasBackend()),
1908 aBackend);
1909 return NS_OK;
1912 NS_IMETHODIMP
1913 GfxInfoBase::GetAzureContentBackend(nsAString& aBackend) {
1914 CopyASCIItoUTF16(mozilla::MakeStringSpan(
1915 gfxPlatform::GetPlatform()->GetAzureContentBackend()),
1916 aBackend);
1917 return NS_OK;
1920 NS_IMETHODIMP
1921 GfxInfoBase::GetUsingGPUProcess(bool* aOutValue) {
1922 GPUProcessManager* gpu = GPUProcessManager::Get();
1923 if (!gpu) {
1924 // Not supported in content processes.
1925 return NS_ERROR_FAILURE;
1928 *aOutValue = !!gpu->GetGPUChild();
1929 return NS_OK;
1932 NS_IMETHODIMP_(int32_t)
1933 GfxInfoBase::GetMaxRefreshRate(bool* aMixed) {
1934 if (aMixed) {
1935 *aMixed = false;
1938 int32_t maxRefreshRate = 0;
1939 for (auto& screen : ScreenManager::GetSingleton().CurrentScreenList()) {
1940 int32_t refreshRate = screen->GetRefreshRate();
1941 if (aMixed && maxRefreshRate > 0 && maxRefreshRate != refreshRate) {
1942 *aMixed = true;
1944 maxRefreshRate = std::max(maxRefreshRate, refreshRate);
1947 return maxRefreshRate > 0 ? maxRefreshRate : -1;
1950 NS_IMETHODIMP
1951 GfxInfoBase::ControlGPUProcessForXPCShell(bool aEnable, bool* _retval) {
1952 gfxPlatform::GetPlatform();
1954 GPUProcessManager* gpm = GPUProcessManager::Get();
1955 if (aEnable) {
1956 if (!gfxConfig::IsEnabled(gfx::Feature::GPU_PROCESS)) {
1957 gfxConfig::UserForceEnable(gfx::Feature::GPU_PROCESS, "xpcshell-test");
1959 DebugOnly<nsresult> rv = gpm->EnsureGPUReady();
1960 MOZ_ASSERT(rv != NS_ERROR_ILLEGAL_DURING_SHUTDOWN);
1961 } else {
1962 gfxConfig::UserDisable(gfx::Feature::GPU_PROCESS, "xpcshell-test");
1963 gpm->KillProcess();
1966 *_retval = true;
1967 return NS_OK;
1970 NS_IMETHODIMP GfxInfoBase::KillGPUProcessForTests() {
1971 GPUProcessManager* gpm = GPUProcessManager::Get();
1972 if (!gpm) {
1973 // gfxPlatform has not been initialized.
1974 return NS_ERROR_NOT_INITIALIZED;
1977 gpm->KillProcess();
1978 return NS_OK;
1981 NS_IMETHODIMP GfxInfoBase::CrashGPUProcessForTests() {
1982 GPUProcessManager* gpm = GPUProcessManager::Get();
1983 if (!gpm) {
1984 // gfxPlatform has not been initialized.
1985 return NS_ERROR_NOT_INITIALIZED;
1988 gpm->CrashProcess();
1989 return NS_OK;
1992 GfxInfoCollectorBase::GfxInfoCollectorBase() {
1993 GfxInfoBase::AddCollector(this);
1996 GfxInfoCollectorBase::~GfxInfoCollectorBase() {
1997 GfxInfoBase::RemoveCollector(this);