Bug 1873042 - Part 3: Optimise substring(0, 1) pattern. r=jandem
[gecko.git] / widget / GfxInfoBase.cpp
bloba21e5cafa54b1c8ba182b308325694093893ee0a
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 (info[i].mFeature == GfxDriverInfo::optionalFeatures &&
1179 OnlyAllowFeatureOnKnownConfig(aFeature))) {
1180 status = info[i].mFeatureStatus;
1181 if (!info[i].mRuleId.IsEmpty()) {
1182 aFailureId = info[i].mRuleId.get();
1183 } else {
1184 aFailureId = "FEATURE_FAILURE_DL_BLOCKLIST_NO_ID";
1186 break;
1191 #if defined(XP_WIN)
1192 // As a very special case, we block D2D on machines with an NVidia 310M GPU
1193 // as either the primary or secondary adapter. D2D is also blocked when the
1194 // NV 310M is the primary adapter (using the standard blocklisting mechanism).
1195 // If the primary GPU already matched something in the blocklist then we
1196 // ignore this special rule. See bug 1008759.
1197 if (status == nsIGfxInfo::FEATURE_STATUS_UNKNOWN &&
1198 (aFeature == nsIGfxInfo::FEATURE_DIRECT2D)) {
1199 if (!adapterInfoFailed[1]) {
1200 nsAString& nvVendorID =
1201 (nsAString&)GfxDriverInfo::GetDeviceVendor(DeviceVendor::NVIDIA);
1202 const nsString nv310mDeviceId = u"0x0A70"_ns;
1203 if (nvVendorID.Equals(adapterVendorID[1],
1204 nsCaseInsensitiveStringComparator) &&
1205 nv310mDeviceId.Equals(adapterDeviceID[1],
1206 nsCaseInsensitiveStringComparator)) {
1207 status = nsIGfxInfo::FEATURE_BLOCKED_DEVICE;
1208 aFailureId = "FEATURE_FAILURE_D2D_NV310M_BLOCK";
1213 // Depends on Windows driver versioning. We don't pass a GfxDriverInfo object
1214 // back to the Windows handler, so we must handle this here.
1215 if (status == FEATURE_BLOCKED_DRIVER_VERSION) {
1216 if (info[i].mSuggestedVersion) {
1217 aSuggestedVersion.AppendPrintf("%s", info[i].mSuggestedVersion);
1218 } else if (info[i].mComparisonOp == DRIVER_LESS_THAN &&
1219 info[i].mDriverVersion != GfxDriverInfo::allDriverVersions) {
1220 aSuggestedVersion.AppendPrintf(
1221 "%lld.%lld.%lld.%lld",
1222 (info[i].mDriverVersion & 0xffff000000000000) >> 48,
1223 (info[i].mDriverVersion & 0x0000ffff00000000) >> 32,
1224 (info[i].mDriverVersion & 0x00000000ffff0000) >> 16,
1225 (info[i].mDriverVersion & 0x000000000000ffff));
1228 #endif
1230 return status;
1233 void GfxInfoBase::SetFeatureStatus(nsTArray<gfx::GfxInfoFeatureStatus>&& aFS) {
1234 MOZ_ASSERT(!sFeatureStatus);
1235 InitFeatureStatus(new nsTArray<gfx::GfxInfoFeatureStatus>(std::move(aFS)));
1238 bool GfxInfoBase::DoesWindowProtocolMatch(
1239 const nsAString& aBlocklistWindowProtocol,
1240 const nsAString& aWindowProtocol) {
1241 return aBlocklistWindowProtocol.Equals(aWindowProtocol,
1242 nsCaseInsensitiveStringComparator) ||
1243 aBlocklistWindowProtocol.Equals(
1244 GfxDriverInfo::GetWindowProtocol(WindowProtocol::All),
1245 nsCaseInsensitiveStringComparator);
1248 bool GfxInfoBase::DoesVendorMatch(const nsAString& aBlocklistVendor,
1249 const nsAString& aAdapterVendor) {
1250 return aBlocklistVendor.Equals(aAdapterVendor,
1251 nsCaseInsensitiveStringComparator) ||
1252 aBlocklistVendor.Equals(
1253 GfxDriverInfo::GetDeviceVendor(DeviceVendor::All),
1254 nsCaseInsensitiveStringComparator);
1257 bool GfxInfoBase::DoesDriverVendorMatch(const nsAString& aBlocklistVendor,
1258 const nsAString& aDriverVendor) {
1259 return aBlocklistVendor.Equals(aDriverVendor,
1260 nsCaseInsensitiveStringComparator) ||
1261 aBlocklistVendor.Equals(
1262 GfxDriverInfo::GetDriverVendor(DriverVendor::All),
1263 nsCaseInsensitiveStringComparator);
1266 bool GfxInfoBase::IsFeatureAllowlisted(int32_t aFeature) const {
1267 return aFeature == nsIGfxInfo::FEATURE_VIDEO_OVERLAY ||
1268 aFeature == nsIGfxInfo::FEATURE_HW_DECODED_VIDEO_ZERO_COPY;
1271 nsresult GfxInfoBase::GetFeatureStatusImpl(
1272 int32_t aFeature, int32_t* aStatus, nsAString& aSuggestedVersion,
1273 const nsTArray<GfxDriverInfo>& aDriverInfo, nsACString& aFailureId,
1274 OperatingSystem* aOS /* = nullptr */) {
1275 if (aFeature <= 0) {
1276 gfxWarning() << "Invalid feature <= 0";
1277 return NS_OK;
1280 if (*aStatus != nsIGfxInfo::FEATURE_STATUS_UNKNOWN) {
1281 // Terminate now with the status determined by the derived type (OS-specific
1282 // code).
1283 return NS_OK;
1286 if (sShutdownOccurred) {
1287 // This is futile; we've already commenced shutdown and our blocklists have
1288 // been deleted. We may want to look into resurrecting the blocklist instead
1289 // but for now, just don't even go there.
1290 return NS_OK;
1293 // Ensure any additional initialization required is complete.
1294 GetData();
1296 // If an operating system was provided by the derived GetFeatureStatusImpl,
1297 // grab it here. Otherwise, the OS is unknown.
1298 OperatingSystem os = (aOS ? *aOS : OperatingSystem::Unknown);
1300 nsAutoString adapterVendorID;
1301 nsAutoString adapterDeviceID;
1302 nsAutoString adapterDriverVersionString;
1303 if (NS_FAILED(GetAdapterVendorID(adapterVendorID)) ||
1304 NS_FAILED(GetAdapterDeviceID(adapterDeviceID)) ||
1305 NS_FAILED(GetAdapterDriverVersion(adapterDriverVersionString))) {
1306 if (OnlyAllowFeatureOnKnownConfig(aFeature)) {
1307 aFailureId = "FEATURE_FAILURE_CANT_RESOLVE_ADAPTER";
1308 *aStatus = nsIGfxInfo::FEATURE_BLOCKED_DEVICE;
1309 } else {
1310 *aStatus = nsIGfxInfo::FEATURE_STATUS_OK;
1312 return NS_OK;
1315 // We only check either the given blocklist, or the static list, as given.
1316 int32_t status;
1317 if (aDriverInfo.Length()) {
1318 status =
1319 FindBlocklistedDeviceInList(aDriverInfo, aSuggestedVersion, aFeature,
1320 aFailureId, os, /* aForAllowing */ false);
1321 } else {
1322 if (!sDriverInfo) {
1323 sDriverInfo = new nsTArray<GfxDriverInfo>();
1325 status = FindBlocklistedDeviceInList(GetGfxDriverInfo(), aSuggestedVersion,
1326 aFeature, aFailureId, os,
1327 /* aForAllowing */ false);
1330 if (status == nsIGfxInfo::FEATURE_STATUS_UNKNOWN) {
1331 if (IsFeatureAllowlisted(aFeature)) {
1332 // This feature is actually using the allowlist; that means after we pass
1333 // the blocklist to prevent us explicitly from getting the feature, we now
1334 // need to check the allowlist to ensure we are allowed to get it in the
1335 // first place.
1336 if (aDriverInfo.Length()) {
1337 status = FindBlocklistedDeviceInList(aDriverInfo, aSuggestedVersion,
1338 aFeature, aFailureId, os,
1339 /* aForAllowing */ true);
1340 } else {
1341 status = FindBlocklistedDeviceInList(
1342 GetGfxDriverInfo(), aSuggestedVersion, aFeature, aFailureId, os,
1343 /* aForAllowing */ true);
1346 if (status == nsIGfxInfo::FEATURE_STATUS_UNKNOWN) {
1347 status = nsIGfxInfo::FEATURE_DENIED;
1349 } else {
1350 // It's now done being processed. It's safe to set the status to
1351 // STATUS_OK.
1352 status = nsIGfxInfo::FEATURE_STATUS_OK;
1356 *aStatus = status;
1357 return NS_OK;
1360 NS_IMETHODIMP
1361 GfxInfoBase::GetFeatureSuggestedDriverVersion(int32_t aFeature,
1362 nsAString& aVersion) {
1363 nsCString version;
1364 if (GetPrefValueForDriverVersion(version)) {
1365 aVersion = NS_ConvertASCIItoUTF16(version);
1366 return NS_OK;
1369 int32_t status;
1370 nsCString discardFailureId;
1371 nsTArray<GfxDriverInfo> driverInfo;
1372 return GetFeatureStatusImpl(aFeature, &status, aVersion, driverInfo,
1373 discardFailureId);
1376 void GfxInfoBase::EvaluateDownloadedBlocklist(
1377 nsTArray<GfxDriverInfo>& aDriverInfo) {
1378 // If the list is empty, then we don't actually want to call
1379 // GetFeatureStatusImpl since we will use the static list instead. In that
1380 // case, all we want to do is make sure the pref is removed.
1381 if (aDriverInfo.IsEmpty()) {
1382 gfxCriticalNoteOnce << "Evaluate empty downloaded blocklist";
1383 return;
1386 OperatingSystem os = GetOperatingSystem();
1388 // For every feature we know about, we evaluate whether this blocklist has a
1389 // non-STATUS_OK status. If it does, we set the pref we evaluate in
1390 // GetFeatureStatus above, so we don't need to hold on to this blocklist
1391 // anywhere permanent.
1392 for (int feature = 1; feature <= nsIGfxInfo::FEATURE_MAX_VALUE; ++feature) {
1393 int32_t status = nsIGfxInfo::FEATURE_STATUS_UNKNOWN;
1394 nsCString failureId;
1395 nsAutoString suggestedVersion;
1397 // Note that we are careful to call the base class method since we only want
1398 // to evaluate the downloadable blocklist for these prefs.
1399 MOZ_ALWAYS_TRUE(NS_SUCCEEDED(GfxInfoBase::GetFeatureStatusImpl(
1400 feature, &status, suggestedVersion, aDriverInfo, failureId, &os)));
1402 switch (status) {
1403 default:
1404 MOZ_FALLTHROUGH_ASSERT("Unhandled feature status!");
1405 case nsIGfxInfo::FEATURE_STATUS_UNKNOWN:
1406 // This may be returned during shutdown or for invalid features.
1407 case nsIGfxInfo::FEATURE_ALLOW_ALWAYS:
1408 case nsIGfxInfo::FEATURE_ALLOW_QUALIFIED:
1409 case nsIGfxInfo::FEATURE_DENIED:
1410 // We cannot use the downloadable blocklist to control the allowlist.
1411 // If a feature is allowlisted, then we should also ignore DENIED
1412 // statuses from GetFeatureStatusImpl because we don't check the
1413 // static list when and this is an expected value. If we wish to
1414 // override the allowlist, it is as simple as creating a normal
1415 // blocklist rule with a BLOCKED* status code.
1416 case nsIGfxInfo::FEATURE_STATUS_OK:
1417 RemovePrefForFeature(feature);
1418 break;
1420 case nsIGfxInfo::FEATURE_BLOCKED_DRIVER_VERSION:
1421 if (!suggestedVersion.IsEmpty()) {
1422 SetPrefValueForDriverVersion(suggestedVersion);
1423 } else {
1424 RemovePrefForDriverVersion();
1426 [[fallthrough]];
1428 case nsIGfxInfo::FEATURE_BLOCKED_MISMATCHED_VERSION:
1429 case nsIGfxInfo::FEATURE_BLOCKED_DEVICE:
1430 case nsIGfxInfo::FEATURE_DISCOURAGED:
1431 case nsIGfxInfo::FEATURE_BLOCKED_OS_VERSION:
1432 case nsIGfxInfo::FEATURE_BLOCKED_PLATFORM_TEST:
1433 SetPrefValueForFeature(feature, status, failureId);
1434 break;
1439 NS_IMETHODIMP_(void)
1440 GfxInfoBase::LogFailure(const nsACString& failure) {
1441 // gfxCriticalError has a mutex lock of its own, so we may not actually
1442 // need this lock. ::GetFailures() accesses the data but the LogForwarder
1443 // will not return the copy of the logs unless it can get the same lock
1444 // that gfxCriticalError uses. Still, that is so much of an implementation
1445 // detail that it's nicer to just add an extra lock here and in
1446 // ::GetFailures()
1447 MutexAutoLock lock(mMutex);
1449 // By default, gfxCriticalError asserts; make it not assert in this case.
1450 gfxCriticalError(CriticalLog::DefaultOptions(false))
1451 << "(LF) " << failure.BeginReading();
1454 NS_IMETHODIMP GfxInfoBase::GetFailures(nsTArray<int32_t>& indices,
1455 nsTArray<nsCString>& failures) {
1456 MutexAutoLock lock(mMutex);
1458 LogForwarder* logForwarder = Factory::GetLogForwarder();
1459 if (!logForwarder) {
1460 return NS_ERROR_UNEXPECTED;
1463 // There are two string copies in this method, starting with this one. We are
1464 // assuming this is not a big deal, as the size of the array should be small
1465 // and the strings in it should be small as well (the error messages in the
1466 // code.) The second copy happens with the AppendElement() calls.
1467 // Technically, we don't need the mutex lock after the StringVectorCopy()
1468 // call.
1469 LoggingRecord loggedStrings = logForwarder->LoggingRecordCopy();
1470 LoggingRecord::const_iterator it;
1471 for (it = loggedStrings.begin(); it != loggedStrings.end(); ++it) {
1472 failures.AppendElement(nsDependentCSubstring(std::get<1>(*it).c_str(),
1473 std::get<1>(*it).size()));
1474 indices.AppendElement(std::get<0>(*it));
1477 return NS_OK;
1480 nsTArray<GfxInfoCollectorBase*>* sCollectors;
1482 static void InitCollectors() {
1483 if (!sCollectors) sCollectors = new nsTArray<GfxInfoCollectorBase*>;
1486 nsresult GfxInfoBase::GetInfo(JSContext* aCx,
1487 JS::MutableHandle<JS::Value> aResult) {
1488 InitCollectors();
1489 InfoObject obj(aCx);
1491 for (uint32_t i = 0; i < sCollectors->Length(); i++) {
1492 (*sCollectors)[i]->GetInfo(obj);
1495 // Some example property definitions
1496 // obj.DefineProperty("wordCacheSize", gfxTextRunWordCache::Count());
1497 // obj.DefineProperty("renderer", mRendererIDsString);
1498 // obj.DefineProperty("five", 5);
1500 if (!obj.mOk) {
1501 return NS_ERROR_FAILURE;
1504 aResult.setObject(*obj.mObj);
1505 return NS_OK;
1508 nsAutoCString gBaseAppVersion;
1510 const nsCString& GfxInfoBase::GetApplicationVersion() {
1511 static bool versionInitialized = false;
1512 if (!versionInitialized) {
1513 // If we fail to get the version, we will not try again.
1514 versionInitialized = true;
1516 // Get the version from xpcom/system/nsIXULAppInfo.idl
1517 nsCOMPtr<nsIXULAppInfo> app = do_GetService("@mozilla.org/xre/app-info;1");
1518 if (app) {
1519 app->GetVersion(gBaseAppVersion);
1522 return gBaseAppVersion;
1525 /* static */ bool GfxInfoBase::OnlyAllowFeatureOnKnownConfig(int32_t aFeature) {
1526 switch (aFeature) {
1527 // The GPU process doesn't need hardware acceleration and can run on
1528 // devices that we normally block from not being on our whitelist.
1529 case nsIGfxInfo::FEATURE_GPU_PROCESS:
1530 return kIsAndroid;
1531 // We can mostly assume that ANGLE will work
1532 case nsIGfxInfo::FEATURE_DIRECT3D_11_ANGLE:
1533 // Remote WebGL is needed for Win32k Lockdown, so it should be enabled
1534 // regardless of HW support or not
1535 case nsIGfxInfo::FEATURE_ALLOW_WEBGL_OUT_OF_PROCESS:
1536 // Backdrop filter should generally work, especially if we fall back to
1537 // Software WebRender because of an unknown vendor.
1538 case nsIGfxInfo::FEATURE_BACKDROP_FILTER:
1539 return false;
1540 default:
1541 return true;
1545 void GfxInfoBase::AddCollector(GfxInfoCollectorBase* collector) {
1546 InitCollectors();
1547 sCollectors->AppendElement(collector);
1550 void GfxInfoBase::RemoveCollector(GfxInfoCollectorBase* collector) {
1551 InitCollectors();
1552 for (uint32_t i = 0; i < sCollectors->Length(); i++) {
1553 if ((*sCollectors)[i] == collector) {
1554 sCollectors->RemoveElementAt(i);
1555 break;
1558 if (sCollectors->IsEmpty()) {
1559 delete sCollectors;
1560 sCollectors = nullptr;
1564 static void AppendMonitor(JSContext* aCx, widget::Screen& aScreen,
1565 JS::Handle<JSObject*> aOutArray, int32_t aIndex) {
1566 JS::Rooted<JSObject*> obj(aCx, JS_NewPlainObject(aCx));
1568 auto screenSize = aScreen.GetRect().Size();
1570 JS::Rooted<JS::Value> screenWidth(aCx, JS::Int32Value(screenSize.width));
1571 JS_SetProperty(aCx, obj, "screenWidth", screenWidth);
1573 JS::Rooted<JS::Value> screenHeight(aCx, JS::Int32Value(screenSize.height));
1574 JS_SetProperty(aCx, obj, "screenHeight", screenHeight);
1576 // XXX Just preserving behavior since this is exposed to telemetry, but we
1577 // could consider including this everywhere.
1578 #ifdef XP_MACOSX
1579 JS::Rooted<JS::Value> scale(
1580 aCx, JS::NumberValue(aScreen.GetContentsScaleFactor()));
1581 JS_SetProperty(aCx, obj, "scale", scale);
1582 #endif
1584 #ifdef XP_WIN
1585 JS::Rooted<JS::Value> refreshRate(aCx,
1586 JS::Int32Value(aScreen.GetRefreshRate()));
1587 JS_SetProperty(aCx, obj, "refreshRate", refreshRate);
1589 JS::Rooted<JS::Value> pseudoDisplay(
1590 aCx, JS::BooleanValue(aScreen.GetIsPseudoDisplay()));
1591 JS_SetProperty(aCx, obj, "pseudoDisplay", pseudoDisplay);
1592 #endif
1594 JS::Rooted<JS::Value> element(aCx, JS::ObjectValue(*obj));
1595 JS_SetElement(aCx, aOutArray, aIndex, element);
1598 nsresult GfxInfoBase::FindMonitors(JSContext* aCx,
1599 JS::Handle<JSObject*> aOutArray) {
1600 int32_t index = 0;
1601 auto& sm = ScreenManager::GetSingleton();
1602 for (auto& screen : sm.CurrentScreenList()) {
1603 AppendMonitor(aCx, *screen, aOutArray, index++);
1606 if (index == 0) {
1607 // Ensure we return at least one monitor, this is needed for xpcshell.
1608 RefPtr<Screen> screen = sm.GetPrimaryScreen();
1609 AppendMonitor(aCx, *screen, aOutArray, index++);
1612 return NS_OK;
1615 NS_IMETHODIMP
1616 GfxInfoBase::GetMonitors(JSContext* aCx, JS::MutableHandle<JS::Value> aResult) {
1617 JS::Rooted<JSObject*> array(aCx, JS::NewArrayObject(aCx, 0));
1619 nsresult rv = FindMonitors(aCx, array);
1620 if (NS_FAILED(rv)) {
1621 return rv;
1624 aResult.setObject(*array);
1625 return NS_OK;
1628 static inline bool SetJSPropertyString(JSContext* aCx,
1629 JS::Handle<JSObject*> aObj,
1630 const char* aProp, const char* aString) {
1631 JS::Rooted<JSString*> str(aCx, JS_NewStringCopyZ(aCx, aString));
1632 if (!str) {
1633 return false;
1636 JS::Rooted<JS::Value> val(aCx, JS::StringValue(str));
1637 return JS_SetProperty(aCx, aObj, aProp, val);
1640 template <typename T>
1641 static inline bool AppendJSElement(JSContext* aCx, JS::Handle<JSObject*> aObj,
1642 const T& aValue) {
1643 uint32_t index;
1644 if (!JS::GetArrayLength(aCx, aObj, &index)) {
1645 return false;
1647 return JS_SetElement(aCx, aObj, index, aValue);
1650 nsresult GfxInfoBase::GetFeatures(JSContext* aCx,
1651 JS::MutableHandle<JS::Value> aOut) {
1652 JS::Rooted<JSObject*> obj(aCx, JS_NewPlainObject(aCx));
1653 if (!obj) {
1654 return NS_ERROR_OUT_OF_MEMORY;
1656 aOut.setObject(*obj);
1658 layers::LayersBackend backend =
1659 gfxPlatform::Initialized()
1660 ? gfxPlatform::GetPlatform()->GetCompositorBackend()
1661 : layers::LayersBackend::LAYERS_NONE;
1662 const char* backendName = layers::GetLayersBackendName(backend);
1663 SetJSPropertyString(aCx, obj, "compositor", backendName);
1665 // If graphics isn't initialized yet, just stop now.
1666 if (!gfxPlatform::Initialized()) {
1667 return NS_OK;
1670 DescribeFeatures(aCx, obj);
1671 return NS_OK;
1674 nsresult GfxInfoBase::GetFeatureLog(JSContext* aCx,
1675 JS::MutableHandle<JS::Value> aOut) {
1676 JS::Rooted<JSObject*> containerObj(aCx, JS_NewPlainObject(aCx));
1677 if (!containerObj) {
1678 return NS_ERROR_OUT_OF_MEMORY;
1680 aOut.setObject(*containerObj);
1682 JS::Rooted<JSObject*> featureArray(aCx, JS::NewArrayObject(aCx, 0));
1683 if (!featureArray) {
1684 return NS_ERROR_OUT_OF_MEMORY;
1687 // Collect features.
1688 gfxConfig::ForEachFeature([&](const char* aName, const char* aDescription,
1689 FeatureState& aFeature) -> void {
1690 JS::Rooted<JSObject*> obj(aCx, JS_NewPlainObject(aCx));
1691 if (!obj) {
1692 return;
1694 if (!SetJSPropertyString(aCx, obj, "name", aName) ||
1695 !SetJSPropertyString(aCx, obj, "description", aDescription) ||
1696 !SetJSPropertyString(aCx, obj, "status",
1697 FeatureStatusToString(aFeature.GetValue()))) {
1698 return;
1701 JS::Rooted<JS::Value> log(aCx);
1702 if (!BuildFeatureStateLog(aCx, aFeature, &log)) {
1703 return;
1705 if (!JS_SetProperty(aCx, obj, "log", log)) {
1706 return;
1709 if (!AppendJSElement(aCx, featureArray, obj)) {
1710 return;
1714 JS::Rooted<JSObject*> fallbackArray(aCx, JS::NewArrayObject(aCx, 0));
1715 if (!fallbackArray) {
1716 return NS_ERROR_OUT_OF_MEMORY;
1719 // Collect fallbacks.
1720 gfxConfig::ForEachFallback(
1721 [&](const char* aName, const char* aMessage) -> void {
1722 JS::Rooted<JSObject*> obj(aCx, JS_NewPlainObject(aCx));
1723 if (!obj) {
1724 return;
1727 if (!SetJSPropertyString(aCx, obj, "name", aName) ||
1728 !SetJSPropertyString(aCx, obj, "message", aMessage)) {
1729 return;
1732 if (!AppendJSElement(aCx, fallbackArray, obj)) {
1733 return;
1737 JS::Rooted<JS::Value> val(aCx);
1739 val = JS::ObjectValue(*featureArray);
1740 JS_SetProperty(aCx, containerObj, "features", val);
1742 val = JS::ObjectValue(*fallbackArray);
1743 JS_SetProperty(aCx, containerObj, "fallbacks", val);
1745 return NS_OK;
1748 bool GfxInfoBase::BuildFeatureStateLog(JSContext* aCx,
1749 const FeatureState& aFeature,
1750 JS::MutableHandle<JS::Value> aOut) {
1751 JS::Rooted<JSObject*> log(aCx, JS::NewArrayObject(aCx, 0));
1752 if (!log) {
1753 return false;
1755 aOut.setObject(*log);
1757 aFeature.ForEachStatusChange([&](const char* aType, FeatureStatus aStatus,
1758 const char* aMessage,
1759 const nsCString& aFailureId) -> void {
1760 JS::Rooted<JSObject*> obj(aCx, JS_NewPlainObject(aCx));
1761 if (!obj) {
1762 return;
1765 if (!SetJSPropertyString(aCx, obj, "type", aType) ||
1766 !SetJSPropertyString(aCx, obj, "status",
1767 FeatureStatusToString(aStatus)) ||
1768 (!aFailureId.IsEmpty() &&
1769 !SetJSPropertyString(aCx, obj, "failureId", aFailureId.get())) ||
1770 (aMessage && !SetJSPropertyString(aCx, obj, "message", aMessage))) {
1771 return;
1774 if (!AppendJSElement(aCx, log, obj)) {
1775 return;
1779 return true;
1782 void GfxInfoBase::DescribeFeatures(JSContext* aCx, JS::Handle<JSObject*> aObj) {
1783 JS::Rooted<JSObject*> obj(aCx);
1785 gfx::FeatureState& hwCompositing =
1786 gfxConfig::GetFeature(gfx::Feature::HW_COMPOSITING);
1787 InitFeatureObject(aCx, aObj, "hwCompositing", hwCompositing, &obj);
1789 gfx::FeatureState& gpuProcess =
1790 gfxConfig::GetFeature(gfx::Feature::GPU_PROCESS);
1791 InitFeatureObject(aCx, aObj, "gpuProcess", gpuProcess, &obj);
1793 gfx::FeatureState& webrender = gfxConfig::GetFeature(gfx::Feature::WEBRENDER);
1794 InitFeatureObject(aCx, aObj, "webrender", webrender, &obj);
1796 gfx::FeatureState& wrCompositor =
1797 gfxConfig::GetFeature(gfx::Feature::WEBRENDER_COMPOSITOR);
1798 InitFeatureObject(aCx, aObj, "wrCompositor", wrCompositor, &obj);
1800 gfx::FeatureState& openglCompositing =
1801 gfxConfig::GetFeature(gfx::Feature::OPENGL_COMPOSITING);
1802 InitFeatureObject(aCx, aObj, "openglCompositing", openglCompositing, &obj);
1804 gfx::FeatureState& omtp = gfxConfig::GetFeature(gfx::Feature::OMTP);
1805 InitFeatureObject(aCx, aObj, "omtp", omtp, &obj);
1808 bool GfxInfoBase::InitFeatureObject(JSContext* aCx,
1809 JS::Handle<JSObject*> aContainer,
1810 const char* aName,
1811 mozilla::gfx::FeatureState& aFeatureState,
1812 JS::MutableHandle<JSObject*> aOutObj) {
1813 JS::Rooted<JSObject*> obj(aCx, JS_NewPlainObject(aCx));
1814 if (!obj) {
1815 return false;
1818 nsCString status = aFeatureState.GetStatusAndFailureIdString();
1820 JS::Rooted<JSString*> str(aCx, JS_NewStringCopyZ(aCx, status.get()));
1821 JS::Rooted<JS::Value> val(aCx, JS::StringValue(str));
1822 JS_SetProperty(aCx, obj, "status", val);
1824 // Add the feature object to the container.
1826 JS::Rooted<JS::Value> val(aCx, JS::ObjectValue(*obj));
1827 JS_SetProperty(aCx, aContainer, aName, val);
1830 aOutObj.set(obj);
1831 return true;
1834 nsresult GfxInfoBase::GetActiveCrashGuards(JSContext* aCx,
1835 JS::MutableHandle<JS::Value> aOut) {
1836 JS::Rooted<JSObject*> array(aCx, JS::NewArrayObject(aCx, 0));
1837 if (!array) {
1838 return NS_ERROR_OUT_OF_MEMORY;
1840 aOut.setObject(*array);
1842 DriverCrashGuard::ForEachActiveCrashGuard(
1843 [&](const char* aName, const char* aPrefName) -> void {
1844 JS::Rooted<JSObject*> obj(aCx, JS_NewPlainObject(aCx));
1845 if (!obj) {
1846 return;
1848 if (!SetJSPropertyString(aCx, obj, "type", aName)) {
1849 return;
1851 if (!SetJSPropertyString(aCx, obj, "prefName", aPrefName)) {
1852 return;
1854 if (!AppendJSElement(aCx, array, obj)) {
1855 return;
1859 return NS_OK;
1862 NS_IMETHODIMP
1863 GfxInfoBase::GetTargetFrameRate(uint32_t* aTargetFrameRate) {
1864 *aTargetFrameRate = gfxPlatform::TargetFrameRate();
1865 return NS_OK;
1868 NS_IMETHODIMP
1869 GfxInfoBase::GetCodecSupportInfo(nsACString& aCodecSupportInfo) {
1870 aCodecSupportInfo.Assign(gfx::gfxVars::CodecSupportInfo());
1871 return NS_OK;
1874 NS_IMETHODIMP
1875 GfxInfoBase::GetIsHeadless(bool* aIsHeadless) {
1876 *aIsHeadless = gfxPlatform::IsHeadless();
1877 return NS_OK;
1880 NS_IMETHODIMP
1881 GfxInfoBase::GetContentBackend(nsAString& aContentBackend) {
1882 BackendType backend = gfxPlatform::GetPlatform()->GetDefaultContentBackend();
1883 nsString outStr;
1885 switch (backend) {
1886 case BackendType::DIRECT2D1_1: {
1887 outStr.AppendPrintf("Direct2D 1.1");
1888 break;
1890 case BackendType::SKIA: {
1891 outStr.AppendPrintf("Skia");
1892 break;
1894 case BackendType::CAIRO: {
1895 outStr.AppendPrintf("Cairo");
1896 break;
1898 default:
1899 return NS_ERROR_FAILURE;
1902 aContentBackend.Assign(outStr);
1903 return NS_OK;
1906 NS_IMETHODIMP
1907 GfxInfoBase::GetAzureCanvasBackend(nsAString& aBackend) {
1908 CopyASCIItoUTF16(mozilla::MakeStringSpan(
1909 gfxPlatform::GetPlatform()->GetAzureCanvasBackend()),
1910 aBackend);
1911 return NS_OK;
1914 NS_IMETHODIMP
1915 GfxInfoBase::GetAzureContentBackend(nsAString& aBackend) {
1916 CopyASCIItoUTF16(mozilla::MakeStringSpan(
1917 gfxPlatform::GetPlatform()->GetAzureContentBackend()),
1918 aBackend);
1919 return NS_OK;
1922 NS_IMETHODIMP
1923 GfxInfoBase::GetUsingGPUProcess(bool* aOutValue) {
1924 GPUProcessManager* gpu = GPUProcessManager::Get();
1925 if (!gpu) {
1926 // Not supported in content processes.
1927 return NS_ERROR_FAILURE;
1930 *aOutValue = !!gpu->GetGPUChild();
1931 return NS_OK;
1934 NS_IMETHODIMP_(int32_t)
1935 GfxInfoBase::GetMaxRefreshRate(bool* aMixed) {
1936 if (aMixed) {
1937 *aMixed = false;
1940 int32_t maxRefreshRate = 0;
1941 for (auto& screen : ScreenManager::GetSingleton().CurrentScreenList()) {
1942 int32_t refreshRate = screen->GetRefreshRate();
1943 if (aMixed && maxRefreshRate > 0 && maxRefreshRate != refreshRate) {
1944 *aMixed = true;
1946 maxRefreshRate = std::max(maxRefreshRate, refreshRate);
1949 return maxRefreshRate > 0 ? maxRefreshRate : -1;
1952 NS_IMETHODIMP
1953 GfxInfoBase::ControlGPUProcessForXPCShell(bool aEnable, bool* _retval) {
1954 gfxPlatform::GetPlatform();
1956 GPUProcessManager* gpm = GPUProcessManager::Get();
1957 if (aEnable) {
1958 if (!gfxConfig::IsEnabled(gfx::Feature::GPU_PROCESS)) {
1959 gfxConfig::UserForceEnable(gfx::Feature::GPU_PROCESS, "xpcshell-test");
1961 DebugOnly<nsresult> rv = gpm->EnsureGPUReady();
1962 MOZ_ASSERT(rv != NS_ERROR_ILLEGAL_DURING_SHUTDOWN);
1963 } else {
1964 gfxConfig::UserDisable(gfx::Feature::GPU_PROCESS, "xpcshell-test");
1965 gpm->KillProcess();
1968 *_retval = true;
1969 return NS_OK;
1972 NS_IMETHODIMP GfxInfoBase::KillGPUProcessForTests() {
1973 GPUProcessManager* gpm = GPUProcessManager::Get();
1974 if (!gpm) {
1975 // gfxPlatform has not been initialized.
1976 return NS_ERROR_NOT_INITIALIZED;
1979 gpm->KillProcess();
1980 return NS_OK;
1983 NS_IMETHODIMP GfxInfoBase::CrashGPUProcessForTests() {
1984 GPUProcessManager* gpm = GPUProcessManager::Get();
1985 if (!gpm) {
1986 // gfxPlatform has not been initialized.
1987 return NS_ERROR_NOT_INITIALIZED;
1990 gpm->CrashProcess();
1991 return NS_OK;
1994 GfxInfoCollectorBase::GfxInfoCollectorBase() {
1995 GfxInfoBase::AddCollector(this);
1998 GfxInfoCollectorBase::~GfxInfoCollectorBase() {
1999 GfxInfoBase::RemoveCollector(this);