Bug 1851372 [wpt PR 41785] - HTML: search setter always uses UTF-8, a=testonly
[gecko.git] / widget / GfxInfoBase.cpp
blob9b53589cf56610ccc7b8eba6be4cbcca8a1743d2
1 /* vim: se cin sw=2 ts=2 et : */
2 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
8 #include "mozilla/ArrayUtils.h"
10 #include "GfxInfoBase.h"
12 #include <mutex> // std::call_once
14 #include "GfxDriverInfo.h"
15 #include "js/Array.h" // JS::GetArrayLength, JS::NewArrayObject
16 #include "js/PropertyAndElement.h" // JS_SetElement, JS_SetProperty
17 #include "nsCOMPtr.h"
18 #include "nsCOMArray.h"
19 #include "nsString.h"
20 #include "nsUnicharUtils.h"
21 #include "nsVersionComparator.h"
22 #include "mozilla/Services.h"
23 #include "mozilla/Observer.h"
24 #include "nsIObserver.h"
25 #include "nsIObserverService.h"
26 #include "nsTArray.h"
27 #include "nsXULAppAPI.h"
28 #include "nsIXULAppInfo.h"
29 #include "mozilla/ClearOnShutdown.h"
30 #include "mozilla/Preferences.h"
31 #include "mozilla/StaticPrefs_gfx.h"
32 #include "mozilla/gfx/2D.h"
33 #include "mozilla/gfx/GPUProcessManager.h"
34 #include "mozilla/gfx/Logging.h"
35 #include "mozilla/gfx/gfxVars.h"
36 #include "mozilla/widget/ScreenManager.h"
37 #include "mozilla/widget/Screen.h"
39 #include "jsapi.h"
41 #include "gfxPlatform.h"
42 #include "gfxConfig.h"
43 #include "DriverCrashGuard.h"
45 using namespace mozilla::widget;
46 using namespace mozilla;
47 using mozilla::MutexAutoLock;
49 nsTArray<GfxDriverInfo>* GfxInfoBase::sDriverInfo;
50 StaticAutoPtr<nsTArray<gfx::GfxInfoFeatureStatus>> GfxInfoBase::sFeatureStatus;
51 bool GfxInfoBase::sDriverInfoObserverInitialized;
52 bool GfxInfoBase::sShutdownOccurred;
54 // Call this when setting sFeatureStatus to a non-null pointer to
55 // ensure destruction even if the GfxInfo component is never instantiated.
56 static void InitFeatureStatus(nsTArray<gfx::GfxInfoFeatureStatus>* aPtr) {
57 static std::once_flag sOnce;
58 std::call_once(sOnce, [] { ClearOnShutdown(&GfxInfoBase::sFeatureStatus); });
59 GfxInfoBase::sFeatureStatus = aPtr;
62 // Observes for shutdown so that the child GfxDriverInfo list is freed.
63 class ShutdownObserver : public nsIObserver {
64 virtual ~ShutdownObserver() = default;
66 public:
67 ShutdownObserver() = default;
69 NS_DECL_ISUPPORTS
71 NS_IMETHOD Observe(nsISupports* subject, const char* aTopic,
72 const char16_t* aData) override {
73 MOZ_ASSERT(strcmp(aTopic, NS_XPCOM_SHUTDOWN_OBSERVER_ID) == 0);
75 delete GfxInfoBase::sDriverInfo;
76 GfxInfoBase::sDriverInfo = nullptr;
78 for (auto& deviceFamily : GfxDriverInfo::sDeviceFamilies) {
79 delete deviceFamily;
80 deviceFamily = nullptr;
83 for (auto& windowProtocol : GfxDriverInfo::sWindowProtocol) {
84 delete windowProtocol;
85 windowProtocol = nullptr;
88 for (auto& deviceVendor : GfxDriverInfo::sDeviceVendors) {
89 delete deviceVendor;
90 deviceVendor = nullptr;
93 for (auto& driverVendor : GfxDriverInfo::sDriverVendors) {
94 delete driverVendor;
95 driverVendor = nullptr;
98 GfxInfoBase::sShutdownOccurred = true;
100 return NS_OK;
104 NS_IMPL_ISUPPORTS(ShutdownObserver, nsIObserver)
106 static void InitGfxDriverInfoShutdownObserver() {
107 if (GfxInfoBase::sDriverInfoObserverInitialized) return;
109 GfxInfoBase::sDriverInfoObserverInitialized = true;
111 nsCOMPtr<nsIObserverService> observerService = services::GetObserverService();
112 if (!observerService) {
113 NS_WARNING("Could not get observer service!");
114 return;
117 ShutdownObserver* obs = new ShutdownObserver();
118 observerService->AddObserver(obs, NS_XPCOM_SHUTDOWN_OBSERVER_ID, false);
121 using namespace mozilla::widget;
122 using namespace mozilla::gfx;
123 using namespace mozilla;
125 NS_IMPL_ISUPPORTS(GfxInfoBase, nsIGfxInfo, nsIObserver,
126 nsISupportsWeakReference)
128 #define BLOCKLIST_PREF_BRANCH "gfx.blacklist."
129 #define SUGGESTED_VERSION_PREF BLOCKLIST_PREF_BRANCH "suggested-driver-version"
131 static const char* GetPrefNameForFeature(int32_t aFeature) {
132 const char* name = nullptr;
133 switch (aFeature) {
134 case nsIGfxInfo::FEATURE_DIRECT2D:
135 name = BLOCKLIST_PREF_BRANCH "direct2d";
136 break;
137 case nsIGfxInfo::FEATURE_DIRECT3D_9_LAYERS:
138 name = BLOCKLIST_PREF_BRANCH "layers.direct3d9";
139 break;
140 case nsIGfxInfo::FEATURE_DIRECT3D_10_LAYERS:
141 name = BLOCKLIST_PREF_BRANCH "layers.direct3d10";
142 break;
143 case nsIGfxInfo::FEATURE_DIRECT3D_10_1_LAYERS:
144 name = BLOCKLIST_PREF_BRANCH "layers.direct3d10-1";
145 break;
146 case nsIGfxInfo::FEATURE_DIRECT3D_11_LAYERS:
147 name = BLOCKLIST_PREF_BRANCH "layers.direct3d11";
148 break;
149 case nsIGfxInfo::FEATURE_DIRECT3D_11_ANGLE:
150 name = BLOCKLIST_PREF_BRANCH "direct3d11angle";
151 break;
152 case nsIGfxInfo::FEATURE_HARDWARE_VIDEO_DECODING:
153 name = BLOCKLIST_PREF_BRANCH "hardwarevideodecoding";
154 break;
155 case nsIGfxInfo::FEATURE_OPENGL_LAYERS:
156 name = BLOCKLIST_PREF_BRANCH "layers.opengl";
157 break;
158 case nsIGfxInfo::FEATURE_WEBGL_OPENGL:
159 name = BLOCKLIST_PREF_BRANCH "webgl.opengl";
160 break;
161 case nsIGfxInfo::FEATURE_WEBGL_ANGLE:
162 name = BLOCKLIST_PREF_BRANCH "webgl.angle";
163 break;
164 case nsIGfxInfo::UNUSED_FEATURE_WEBGL_MSAA:
165 name = BLOCKLIST_PREF_BRANCH "webgl.msaa";
166 break;
167 case nsIGfxInfo::FEATURE_STAGEFRIGHT:
168 name = BLOCKLIST_PREF_BRANCH "stagefright";
169 break;
170 case nsIGfxInfo::FEATURE_WEBRTC_HW_ACCELERATION_H264:
171 name = BLOCKLIST_PREF_BRANCH "webrtc.hw.acceleration.h264";
172 break;
173 case nsIGfxInfo::FEATURE_WEBRTC_HW_ACCELERATION_ENCODE:
174 name = BLOCKLIST_PREF_BRANCH "webrtc.hw.acceleration.encode";
175 break;
176 case nsIGfxInfo::FEATURE_WEBRTC_HW_ACCELERATION_DECODE:
177 name = BLOCKLIST_PREF_BRANCH "webrtc.hw.acceleration.decode";
178 break;
179 case nsIGfxInfo::FEATURE_CANVAS2D_ACCELERATION:
180 name = BLOCKLIST_PREF_BRANCH "canvas2d.acceleration";
181 break;
182 case nsIGfxInfo::FEATURE_DX_INTEROP2:
183 name = BLOCKLIST_PREF_BRANCH "dx.interop2";
184 break;
185 case nsIGfxInfo::FEATURE_GPU_PROCESS:
186 name = BLOCKLIST_PREF_BRANCH "gpu.process";
187 break;
188 case nsIGfxInfo::FEATURE_WEBGL2:
189 name = BLOCKLIST_PREF_BRANCH "webgl2";
190 break;
191 case nsIGfxInfo::FEATURE_D3D11_KEYED_MUTEX:
192 name = BLOCKLIST_PREF_BRANCH "d3d11.keyed.mutex";
193 break;
194 case nsIGfxInfo::FEATURE_WEBRENDER:
195 name = BLOCKLIST_PREF_BRANCH "webrender";
196 break;
197 case nsIGfxInfo::FEATURE_WEBRENDER_COMPOSITOR:
198 name = BLOCKLIST_PREF_BRANCH "webrender.compositor";
199 break;
200 case nsIGfxInfo::FEATURE_DX_NV12:
201 name = BLOCKLIST_PREF_BRANCH "dx.nv12";
202 break;
203 case nsIGfxInfo::FEATURE_DX_P010:
204 name = BLOCKLIST_PREF_BRANCH "dx.p010";
205 break;
206 case nsIGfxInfo::FEATURE_DX_P016:
207 name = BLOCKLIST_PREF_BRANCH "dx.p016";
208 break;
209 case nsIGfxInfo::FEATURE_VP8_HW_DECODE:
210 name = BLOCKLIST_PREF_BRANCH "vp8.hw-decode";
211 break;
212 case nsIGfxInfo::FEATURE_VP9_HW_DECODE:
213 name = BLOCKLIST_PREF_BRANCH "vp9.hw-decode";
214 break;
215 case nsIGfxInfo::FEATURE_GL_SWIZZLE:
216 name = BLOCKLIST_PREF_BRANCH "gl.swizzle";
217 break;
218 case nsIGfxInfo::FEATURE_WEBRENDER_SCISSORED_CACHE_CLEARS:
219 name = BLOCKLIST_PREF_BRANCH "webrender.scissored_cache_clears";
220 break;
221 case nsIGfxInfo::FEATURE_ALLOW_WEBGL_OUT_OF_PROCESS:
222 name = BLOCKLIST_PREF_BRANCH "webgl.allow-oop";
223 break;
224 case nsIGfxInfo::FEATURE_THREADSAFE_GL:
225 name = BLOCKLIST_PREF_BRANCH "gl.threadsafe";
226 break;
227 case nsIGfxInfo::FEATURE_WEBRENDER_OPTIMIZED_SHADERS:
228 name = BLOCKLIST_PREF_BRANCH "webrender.optimized-shaders";
229 break;
230 case nsIGfxInfo::FEATURE_X11_EGL:
231 name = BLOCKLIST_PREF_BRANCH "x11.egl";
232 break;
233 case nsIGfxInfo::FEATURE_DMABUF:
234 name = BLOCKLIST_PREF_BRANCH "dmabuf";
235 break;
236 case nsIGfxInfo::FEATURE_WEBGPU:
237 name = BLOCKLIST_PREF_BRANCH "webgpu";
238 break;
239 case nsIGfxInfo::FEATURE_VIDEO_OVERLAY:
240 name = BLOCKLIST_PREF_BRANCH "video-overlay";
241 break;
242 case nsIGfxInfo::FEATURE_HW_DECODED_VIDEO_ZERO_COPY:
243 name = BLOCKLIST_PREF_BRANCH "hw-video-zero-copy";
244 break;
245 case nsIGfxInfo::FEATURE_WEBRENDER_SHADER_CACHE:
246 name = BLOCKLIST_PREF_BRANCH "webrender.program-binary-disk";
247 break;
248 case nsIGfxInfo::FEATURE_WEBRENDER_PARTIAL_PRESENT:
249 name = BLOCKLIST_PREF_BRANCH "webrender.partial-present";
250 break;
251 case nsIGfxInfo::FEATURE_DMABUF_SURFACE_EXPORT:
252 name = BLOCKLIST_PREF_BRANCH "dmabuf.surface-export";
253 break;
254 case nsIGfxInfo::FEATURE_REUSE_DECODER_DEVICE:
255 name = BLOCKLIST_PREF_BRANCH "reuse-decoder-device";
256 break;
257 case nsIGfxInfo::FEATURE_BACKDROP_FILTER:
258 name = BLOCKLIST_PREF_BRANCH "backdrop.filter";
259 break;
260 case nsIGfxInfo::FEATURE_ACCELERATED_CANVAS2D:
261 name = BLOCKLIST_PREF_BRANCH "accelerated-canvas2d";
262 break;
263 case nsIGfxInfo::FEATURE_H264_HW_DECODE:
264 name = BLOCKLIST_PREF_BRANCH "h264.hw-decode";
265 break;
266 case nsIGfxInfo::FEATURE_AV1_HW_DECODE:
267 name = BLOCKLIST_PREF_BRANCH "av1.hw-decode";
268 break;
269 case nsIGfxInfo::FEATURE_VIDEO_SOFTWARE_OVERLAY:
270 name = BLOCKLIST_PREF_BRANCH "video-software-overlay";
271 break;
272 default:
273 MOZ_ASSERT_UNREACHABLE("Unexpected nsIGfxInfo feature?!");
274 break;
277 return name;
280 // Returns the value of the pref for the relevant feature in aValue.
281 // If the pref doesn't exist, aValue is not touched, and returns false.
282 static bool GetPrefValueForFeature(int32_t aFeature, int32_t& aValue,
283 nsACString& aFailureId) {
284 const char* prefname = GetPrefNameForFeature(aFeature);
285 if (!prefname) return false;
287 aValue = nsIGfxInfo::FEATURE_STATUS_UNKNOWN;
288 if (!NS_SUCCEEDED(Preferences::GetInt(prefname, &aValue))) {
289 return false;
292 if (aValue == nsIGfxInfo::FEATURE_DENIED) {
293 // We should never see the DENIED status with the downloadable blocklist.
294 return false;
297 nsCString failureprefname(prefname);
298 failureprefname += ".failureid";
299 nsAutoCString failureValue;
300 nsresult rv = Preferences::GetCString(failureprefname.get(), failureValue);
301 if (NS_SUCCEEDED(rv)) {
302 aFailureId = failureValue.get();
303 } else {
304 aFailureId = "FEATURE_FAILURE_BLOCKLIST_PREF";
307 return true;
310 static void SetPrefValueForFeature(int32_t aFeature, int32_t aValue,
311 const nsACString& aFailureId) {
312 const char* prefname = GetPrefNameForFeature(aFeature);
313 if (!prefname) return;
314 if (XRE_IsParentProcess()) {
315 GfxInfoBase::sFeatureStatus = nullptr;
318 Preferences::SetInt(prefname, aValue);
319 if (!aFailureId.IsEmpty()) {
320 nsAutoCString failureprefname(prefname);
321 failureprefname += ".failureid";
322 Preferences::SetCString(failureprefname.get(), aFailureId);
326 static void RemovePrefForFeature(int32_t aFeature) {
327 const char* prefname = GetPrefNameForFeature(aFeature);
328 if (!prefname) return;
330 if (XRE_IsParentProcess()) {
331 GfxInfoBase::sFeatureStatus = nullptr;
333 Preferences::ClearUser(prefname);
336 static bool GetPrefValueForDriverVersion(nsCString& aVersion) {
337 return NS_SUCCEEDED(
338 Preferences::GetCString(SUGGESTED_VERSION_PREF, aVersion));
341 static void SetPrefValueForDriverVersion(const nsAString& aVersion) {
342 Preferences::SetString(SUGGESTED_VERSION_PREF, aVersion);
345 static void RemovePrefForDriverVersion() {
346 Preferences::ClearUser(SUGGESTED_VERSION_PREF);
349 static OperatingSystem BlocklistOSToOperatingSystem(const nsAString& os) {
350 if (os.EqualsLiteral("WINNT 6.1")) {
351 return OperatingSystem::Windows7;
353 if (os.EqualsLiteral("WINNT 6.2")) {
354 return OperatingSystem::Windows8;
356 if (os.EqualsLiteral("WINNT 6.3")) {
357 return OperatingSystem::Windows8_1;
359 if (os.EqualsLiteral("WINNT 10.0")) {
360 return OperatingSystem::Windows10;
362 if (os.EqualsLiteral("Linux")) {
363 return OperatingSystem::Linux;
365 if (os.EqualsLiteral("Darwin 9")) {
366 return OperatingSystem::OSX10_5;
368 if (os.EqualsLiteral("Darwin 10")) {
369 return OperatingSystem::OSX10_6;
371 if (os.EqualsLiteral("Darwin 11")) {
372 return OperatingSystem::OSX10_7;
374 if (os.EqualsLiteral("Darwin 12")) {
375 return OperatingSystem::OSX10_8;
377 if (os.EqualsLiteral("Darwin 13")) {
378 return OperatingSystem::OSX10_9;
380 if (os.EqualsLiteral("Darwin 14")) {
381 return OperatingSystem::OSX10_10;
383 if (os.EqualsLiteral("Darwin 15")) {
384 return OperatingSystem::OSX10_11;
386 if (os.EqualsLiteral("Darwin 16")) {
387 return OperatingSystem::OSX10_12;
389 if (os.EqualsLiteral("Darwin 17")) {
390 return OperatingSystem::OSX10_13;
392 if (os.EqualsLiteral("Darwin 18")) {
393 return OperatingSystem::OSX10_14;
395 if (os.EqualsLiteral("Darwin 19")) {
396 return OperatingSystem::OSX10_15;
398 if (os.EqualsLiteral("Darwin 20")) {
399 return OperatingSystem::OSX11_0;
401 if (os.EqualsLiteral("Android")) {
402 return OperatingSystem::Android;
403 // For historical reasons, "All" in blocklist means "All Windows"
405 if (os.EqualsLiteral("All")) {
406 return OperatingSystem::Windows;
408 if (os.EqualsLiteral("Darwin")) {
409 return OperatingSystem::OSX;
412 return OperatingSystem::Unknown;
415 static GfxDeviceFamily* BlocklistDevicesToDeviceFamily(
416 nsTArray<nsCString>& devices) {
417 if (devices.Length() == 0) return nullptr;
419 // For each device, get its device ID, and return a freshly-allocated
420 // GfxDeviceFamily with the contents of that array.
421 GfxDeviceFamily* deviceIds = new GfxDeviceFamily;
423 for (uint32_t i = 0; i < devices.Length(); ++i) {
424 // We make sure we don't add any "empty" device entries to the array, so
425 // we don't need to check if devices[i] is empty.
426 deviceIds->Append(NS_ConvertUTF8toUTF16(devices[i]));
429 return deviceIds;
432 static int32_t BlocklistFeatureToGfxFeature(const nsAString& aFeature) {
433 MOZ_ASSERT(!aFeature.IsEmpty());
434 if (aFeature.EqualsLiteral("DIRECT2D")) {
435 return nsIGfxInfo::FEATURE_DIRECT2D;
437 if (aFeature.EqualsLiteral("DIRECT3D_9_LAYERS")) {
438 return nsIGfxInfo::FEATURE_DIRECT3D_9_LAYERS;
440 if (aFeature.EqualsLiteral("DIRECT3D_10_LAYERS")) {
441 return nsIGfxInfo::FEATURE_DIRECT3D_10_LAYERS;
443 if (aFeature.EqualsLiteral("DIRECT3D_10_1_LAYERS")) {
444 return nsIGfxInfo::FEATURE_DIRECT3D_10_1_LAYERS;
446 if (aFeature.EqualsLiteral("DIRECT3D_11_LAYERS")) {
447 return nsIGfxInfo::FEATURE_DIRECT3D_11_LAYERS;
449 if (aFeature.EqualsLiteral("DIRECT3D_11_ANGLE")) {
450 return nsIGfxInfo::FEATURE_DIRECT3D_11_ANGLE;
452 if (aFeature.EqualsLiteral("HARDWARE_VIDEO_DECODING")) {
453 return nsIGfxInfo::FEATURE_HARDWARE_VIDEO_DECODING;
455 if (aFeature.EqualsLiteral("OPENGL_LAYERS")) {
456 return nsIGfxInfo::FEATURE_OPENGL_LAYERS;
458 if (aFeature.EqualsLiteral("WEBGL_OPENGL")) {
459 return nsIGfxInfo::FEATURE_WEBGL_OPENGL;
461 if (aFeature.EqualsLiteral("WEBGL_ANGLE")) {
462 return nsIGfxInfo::FEATURE_WEBGL_ANGLE;
464 if (aFeature.EqualsLiteral("WEBGL_MSAA")) {
465 return nsIGfxInfo::UNUSED_FEATURE_WEBGL_MSAA;
467 if (aFeature.EqualsLiteral("STAGEFRIGHT")) {
468 return nsIGfxInfo::FEATURE_STAGEFRIGHT;
470 if (aFeature.EqualsLiteral("WEBRTC_HW_ACCELERATION_ENCODE")) {
471 return nsIGfxInfo::FEATURE_WEBRTC_HW_ACCELERATION_ENCODE;
473 if (aFeature.EqualsLiteral("WEBRTC_HW_ACCELERATION_DECODE")) {
474 return nsIGfxInfo::FEATURE_WEBRTC_HW_ACCELERATION_DECODE;
476 if (aFeature.EqualsLiteral("WEBRTC_HW_ACCELERATION_H264")) {
477 return nsIGfxInfo::FEATURE_WEBRTC_HW_ACCELERATION_H264;
479 if (aFeature.EqualsLiteral("CANVAS2D_ACCELERATION")) {
480 return nsIGfxInfo::FEATURE_CANVAS2D_ACCELERATION;
482 if (aFeature.EqualsLiteral("DX_INTEROP2")) {
483 return nsIGfxInfo::FEATURE_DX_INTEROP2;
485 if (aFeature.EqualsLiteral("GPU_PROCESS")) {
486 return nsIGfxInfo::FEATURE_GPU_PROCESS;
488 if (aFeature.EqualsLiteral("WEBGL2")) {
489 return nsIGfxInfo::FEATURE_WEBGL2;
491 if (aFeature.EqualsLiteral("D3D11_KEYED_MUTEX")) {
492 return nsIGfxInfo::FEATURE_D3D11_KEYED_MUTEX;
494 if (aFeature.EqualsLiteral("WEBRENDER")) {
495 return nsIGfxInfo::FEATURE_WEBRENDER;
497 if (aFeature.EqualsLiteral("WEBRENDER_COMPOSITOR")) {
498 return nsIGfxInfo::FEATURE_WEBRENDER_COMPOSITOR;
500 if (aFeature.EqualsLiteral("DX_NV12")) {
501 return nsIGfxInfo::FEATURE_DX_NV12;
503 if (aFeature.EqualsLiteral("VP8_HW_DECODE")) {
504 return nsIGfxInfo::FEATURE_VP8_HW_DECODE;
506 if (aFeature.EqualsLiteral("VP9_HW_DECODE")) {
507 return nsIGfxInfo::FEATURE_VP9_HW_DECODE;
509 if (aFeature.EqualsLiteral("GL_SWIZZLE")) {
510 return nsIGfxInfo::FEATURE_GL_SWIZZLE;
512 if (aFeature.EqualsLiteral("WEBRENDER_SCISSORED_CACHE_CLEARS")) {
513 return nsIGfxInfo::FEATURE_WEBRENDER_SCISSORED_CACHE_CLEARS;
515 if (aFeature.EqualsLiteral("ALLOW_WEBGL_OUT_OF_PROCESS")) {
516 return nsIGfxInfo::FEATURE_ALLOW_WEBGL_OUT_OF_PROCESS;
518 if (aFeature.EqualsLiteral("THREADSAFE_GL")) {
519 return nsIGfxInfo::FEATURE_THREADSAFE_GL;
521 if (aFeature.EqualsLiteral("X11_EGL")) {
522 return nsIGfxInfo::FEATURE_X11_EGL;
524 if (aFeature.EqualsLiteral("DMABUF")) {
525 return nsIGfxInfo::FEATURE_DMABUF;
527 if (aFeature.EqualsLiteral("WEBGPU")) {
528 return nsIGfxInfo::FEATURE_WEBGPU;
530 if (aFeature.EqualsLiteral("VIDEO_OVERLAY")) {
531 return nsIGfxInfo::FEATURE_VIDEO_OVERLAY;
533 if (aFeature.EqualsLiteral("HW_DECODED_VIDEO_ZERO_COPY")) {
534 return nsIGfxInfo::FEATURE_HW_DECODED_VIDEO_ZERO_COPY;
536 if (aFeature.EqualsLiteral("REUSE_DECODER_DEVICE")) {
537 return nsIGfxInfo::FEATURE_REUSE_DECODER_DEVICE;
539 if (aFeature.EqualsLiteral("WEBRENDER_PARTIAL_PRESENT")) {
540 return nsIGfxInfo::FEATURE_WEBRENDER_PARTIAL_PRESENT;
542 if (aFeature.EqualsLiteral("BACKDROP_FILTER")) {
543 return nsIGfxInfo::FEATURE_BACKDROP_FILTER;
545 if (aFeature.EqualsLiteral("ACCELERATED_CANVAS2D")) {
546 return nsIGfxInfo::FEATURE_ACCELERATED_CANVAS2D;
549 // If we don't recognize the feature, it may be new, and something
550 // this version doesn't understand. So, nothing to do. This is
551 // different from feature not being specified at all, in which case
552 // this method should not get called and we should continue with the
553 // "all features" blocklisting.
554 return -1;
557 static int32_t BlocklistFeatureStatusToGfxFeatureStatus(
558 const nsAString& aStatus) {
559 if (aStatus.EqualsLiteral("STATUS_OK")) {
560 return nsIGfxInfo::FEATURE_STATUS_OK;
562 if (aStatus.EqualsLiteral("BLOCKED_DRIVER_VERSION")) {
563 return nsIGfxInfo::FEATURE_BLOCKED_DRIVER_VERSION;
565 if (aStatus.EqualsLiteral("BLOCKED_DEVICE")) {
566 return nsIGfxInfo::FEATURE_BLOCKED_DEVICE;
568 if (aStatus.EqualsLiteral("DISCOURAGED")) {
569 return nsIGfxInfo::FEATURE_DISCOURAGED;
571 if (aStatus.EqualsLiteral("BLOCKED_OS_VERSION")) {
572 return nsIGfxInfo::FEATURE_BLOCKED_OS_VERSION;
574 if (aStatus.EqualsLiteral("DENIED")) {
575 return nsIGfxInfo::FEATURE_DENIED;
577 if (aStatus.EqualsLiteral("ALLOW_QUALIFIED")) {
578 return nsIGfxInfo::FEATURE_ALLOW_QUALIFIED;
580 if (aStatus.EqualsLiteral("ALLOW_ALWAYS")) {
581 return nsIGfxInfo::FEATURE_ALLOW_ALWAYS;
584 // Do not allow it to set STATUS_UNKNOWN. Also, we are not
585 // expecting the "mismatch" status showing up here.
587 return nsIGfxInfo::FEATURE_STATUS_OK;
590 static VersionComparisonOp BlocklistComparatorToComparisonOp(
591 const nsAString& op) {
592 if (op.EqualsLiteral("LESS_THAN")) {
593 return DRIVER_LESS_THAN;
595 if (op.EqualsLiteral("BUILD_ID_LESS_THAN")) {
596 return DRIVER_BUILD_ID_LESS_THAN;
598 if (op.EqualsLiteral("LESS_THAN_OR_EQUAL")) {
599 return DRIVER_LESS_THAN_OR_EQUAL;
601 if (op.EqualsLiteral("BUILD_ID_LESS_THAN_OR_EQUAL")) {
602 return DRIVER_BUILD_ID_LESS_THAN_OR_EQUAL;
604 if (op.EqualsLiteral("GREATER_THAN")) {
605 return DRIVER_GREATER_THAN;
607 if (op.EqualsLiteral("GREATER_THAN_OR_EQUAL")) {
608 return DRIVER_GREATER_THAN_OR_EQUAL;
610 if (op.EqualsLiteral("EQUAL")) {
611 return DRIVER_EQUAL;
613 if (op.EqualsLiteral("NOT_EQUAL")) {
614 return DRIVER_NOT_EQUAL;
616 if (op.EqualsLiteral("BETWEEN_EXCLUSIVE")) {
617 return DRIVER_BETWEEN_EXCLUSIVE;
619 if (op.EqualsLiteral("BETWEEN_INCLUSIVE")) {
620 return DRIVER_BETWEEN_INCLUSIVE;
622 if (op.EqualsLiteral("BETWEEN_INCLUSIVE_START")) {
623 return DRIVER_BETWEEN_INCLUSIVE_START;
626 return DRIVER_COMPARISON_IGNORED;
630 Deserialize Blocklist entries from string.
631 e.g:
632 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
634 static bool BlocklistEntryToDriverInfo(const nsACString& aBlocklistEntry,
635 GfxDriverInfo& aDriverInfo) {
636 // If we get an application version to be zero, something is not working
637 // and we are not going to bother checking the blocklist versions.
638 // See TestGfxWidgets.cpp for how version comparison works.
639 // <versionRange minVersion="42.0a1" maxVersion="45.0"></versionRange>
640 static mozilla::Version zeroV("0");
641 static mozilla::Version appV(GfxInfoBase::GetApplicationVersion().get());
642 if (appV <= zeroV) {
643 gfxCriticalErrorOnce(gfxCriticalError::DefaultOptions(false))
644 << "Invalid application version "
645 << GfxInfoBase::GetApplicationVersion().get();
648 aDriverInfo.mRuleId = "FEATURE_FAILURE_DL_BLOCKLIST_NO_ID"_ns;
650 for (const auto& keyValue : aBlocklistEntry.Split('\t')) {
651 nsTArray<nsCString> splitted;
652 ParseString(keyValue, ':', splitted);
653 if (splitted.Length() != 2) {
654 // If we don't recognize the input data, we do not want to proceed.
655 gfxCriticalErrorOnce(CriticalLog::DefaultOptions(false))
656 << "Unrecognized data " << nsCString(keyValue).get();
657 return false;
659 const nsCString& key = splitted[0];
660 const nsCString& value = splitted[1];
661 NS_ConvertUTF8toUTF16 dataValue(value);
663 if (value.Length() == 0) {
664 // Safety check for empty values.
665 gfxCriticalErrorOnce(CriticalLog::DefaultOptions(false))
666 << "Empty value for " << key.get();
667 return false;
670 if (key.EqualsLiteral("blockID")) {
671 nsCString blockIdStr = "FEATURE_FAILURE_DL_BLOCKLIST_"_ns + value;
672 aDriverInfo.mRuleId = blockIdStr.get();
673 } else if (key.EqualsLiteral("os")) {
674 aDriverInfo.mOperatingSystem = BlocklistOSToOperatingSystem(dataValue);
675 } else if (key.EqualsLiteral("osversion")) {
676 aDriverInfo.mOperatingSystemVersion = strtoul(value.get(), nullptr, 10);
677 } else if (key.EqualsLiteral("windowProtocol")) {
678 aDriverInfo.mWindowProtocol = dataValue;
679 } else if (key.EqualsLiteral("vendor")) {
680 aDriverInfo.mAdapterVendor = dataValue;
681 } else if (key.EqualsLiteral("driverVendor")) {
682 aDriverInfo.mDriverVendor = dataValue;
683 } else if (key.EqualsLiteral("feature")) {
684 aDriverInfo.mFeature = BlocklistFeatureToGfxFeature(dataValue);
685 if (aDriverInfo.mFeature < 0) {
686 // If we don't recognize the feature, we do not want to proceed.
687 gfxWarning() << "Unrecognized feature " << value.get();
688 return false;
690 } else if (key.EqualsLiteral("featureStatus")) {
691 aDriverInfo.mFeatureStatus =
692 BlocklistFeatureStatusToGfxFeatureStatus(dataValue);
693 } else if (key.EqualsLiteral("driverVersion")) {
694 uint64_t version;
695 if (ParseDriverVersion(dataValue, &version))
696 aDriverInfo.mDriverVersion = version;
697 } else if (key.EqualsLiteral("driverVersionMax")) {
698 uint64_t version;
699 if (ParseDriverVersion(dataValue, &version))
700 aDriverInfo.mDriverVersionMax = version;
701 } else if (key.EqualsLiteral("driverVersionComparator")) {
702 aDriverInfo.mComparisonOp = BlocklistComparatorToComparisonOp(dataValue);
703 } else if (key.EqualsLiteral("model")) {
704 aDriverInfo.mModel = dataValue;
705 } else if (key.EqualsLiteral("product")) {
706 aDriverInfo.mProduct = dataValue;
707 } else if (key.EqualsLiteral("manufacturer")) {
708 aDriverInfo.mManufacturer = dataValue;
709 } else if (key.EqualsLiteral("hardware")) {
710 aDriverInfo.mHardware = dataValue;
711 } else if (key.EqualsLiteral("versionRange")) {
712 nsTArray<nsCString> versionRange;
713 ParseString(value, ',', versionRange);
714 if (versionRange.Length() != 2) {
715 gfxCriticalErrorOnce(CriticalLog::DefaultOptions(false))
716 << "Unrecognized versionRange " << value.get();
717 return false;
719 const nsCString& minValue = versionRange[0];
720 const nsCString& maxValue = versionRange[1];
722 mozilla::Version minV(minValue.get());
723 mozilla::Version maxV(maxValue.get());
725 if (minV > zeroV && !(appV >= minV)) {
726 // The version of the application is less than the minimal version
727 // this blocklist entry applies to, so we can just ignore it by
728 // returning false and letting the caller deal with it.
729 return false;
731 if (maxV > zeroV && !(appV <= maxV)) {
732 // The version of the application is more than the maximal version
733 // this blocklist entry applies to, so we can just ignore it by
734 // returning false and letting the caller deal with it.
735 return false;
737 } else if (key.EqualsLiteral("devices")) {
738 nsTArray<nsCString> devices;
739 ParseString(value, ',', devices);
740 GfxDeviceFamily* deviceIds = BlocklistDevicesToDeviceFamily(devices);
741 if (deviceIds) {
742 // Get GfxDriverInfo to adopt the devices array we created.
743 aDriverInfo.mDeleteDevices = true;
744 aDriverInfo.mDevices = deviceIds;
747 // We explicitly ignore unknown elements.
750 return true;
753 NS_IMETHODIMP
754 GfxInfoBase::Observe(nsISupports* aSubject, const char* aTopic,
755 const char16_t* aData) {
756 if (strcmp(aTopic, "blocklist-data-gfxItems") == 0) {
757 nsTArray<GfxDriverInfo> driverInfo;
758 NS_ConvertUTF16toUTF8 utf8Data(aData);
760 for (const auto& blocklistEntry : utf8Data.Split('\n')) {
761 GfxDriverInfo di;
762 if (BlocklistEntryToDriverInfo(blocklistEntry, di)) {
763 // XXX Changing this to driverInfo.AppendElement(di) causes leaks.
764 // Probably some non-standard semantics of the copy/move operations?
765 *driverInfo.AppendElement() = di;
766 // Prevent di falling out of scope from destroying the devices.
767 di.mDeleteDevices = false;
768 } else {
769 driverInfo.AppendElement();
773 EvaluateDownloadedBlocklist(driverInfo);
776 return NS_OK;
779 GfxInfoBase::GfxInfoBase() : mScreenPixels(INT64_MAX), mMutex("GfxInfoBase") {}
781 GfxInfoBase::~GfxInfoBase() = default;
783 nsresult GfxInfoBase::Init() {
784 InitGfxDriverInfoShutdownObserver();
786 nsCOMPtr<nsIObserverService> os = mozilla::services::GetObserverService();
787 if (os) {
788 os->AddObserver(this, "blocklist-data-gfxItems", true);
791 return NS_OK;
794 void GfxInfoBase::GetData() {
795 if (mScreenPixels != INT64_MAX) {
796 // Already initialized.
797 return;
800 ScreenManager::GetSingleton().GetTotalScreenPixels(&mScreenPixels);
803 NS_IMETHODIMP
804 GfxInfoBase::GetFeatureStatus(int32_t aFeature, nsACString& aFailureId,
805 int32_t* aStatus) {
806 // Ignore the gfx.blocklist.all pref on release and beta.
807 #if defined(RELEASE_OR_BETA)
808 int32_t blocklistAll = 0;
809 #else
810 int32_t blocklistAll = StaticPrefs::gfx_blocklist_all_AtStartup();
811 #endif
812 if (blocklistAll > 0) {
813 gfxCriticalErrorOnce(gfxCriticalError::DefaultOptions(false))
814 << "Forcing blocklisting all features";
815 *aStatus = FEATURE_BLOCKED_DEVICE;
816 aFailureId = "FEATURE_FAILURE_BLOCK_ALL";
817 return NS_OK;
820 if (blocklistAll < 0) {
821 gfxCriticalErrorOnce(gfxCriticalError::DefaultOptions(false))
822 << "Ignoring any feature blocklisting.";
823 *aStatus = FEATURE_STATUS_OK;
824 return NS_OK;
827 // This is how we evaluate the downloadable blocklist. If there is no pref,
828 // then we will fallback to checking the static blocklist.
829 if (GetPrefValueForFeature(aFeature, *aStatus, aFailureId)) {
830 return NS_OK;
833 if (XRE_IsContentProcess() || XRE_IsGPUProcess()) {
834 // Use the cached data received from the parent process.
835 MOZ_ASSERT(sFeatureStatus);
836 bool success = false;
837 for (const auto& fs : *sFeatureStatus) {
838 if (fs.feature() == aFeature) {
839 aFailureId = fs.failureId();
840 *aStatus = fs.status();
841 success = true;
842 break;
845 return success ? NS_OK : NS_ERROR_FAILURE;
848 nsString version;
849 nsTArray<GfxDriverInfo> driverInfo;
850 nsresult rv =
851 GetFeatureStatusImpl(aFeature, aStatus, version, driverInfo, aFailureId);
852 return rv;
855 nsTArray<gfx::GfxInfoFeatureStatus> GfxInfoBase::GetAllFeatures() {
856 MOZ_RELEASE_ASSERT(XRE_IsParentProcess());
857 if (!sFeatureStatus) {
858 InitFeatureStatus(new nsTArray<gfx::GfxInfoFeatureStatus>());
859 for (int32_t i = 1; i <= nsIGfxInfo::FEATURE_MAX_VALUE; ++i) {
860 int32_t status = 0;
861 nsAutoCString failureId;
862 GetFeatureStatus(i, failureId, &status);
863 gfx::GfxInfoFeatureStatus gfxFeatureStatus;
864 gfxFeatureStatus.feature() = i;
865 gfxFeatureStatus.status() = status;
866 gfxFeatureStatus.failureId() = failureId;
867 sFeatureStatus->AppendElement(gfxFeatureStatus);
871 nsTArray<gfx::GfxInfoFeatureStatus> features;
872 for (const auto& status : *sFeatureStatus) {
873 gfx::GfxInfoFeatureStatus copy = status;
874 features.AppendElement(copy);
876 return features;
879 inline bool MatchingAllowStatus(int32_t aStatus) {
880 switch (aStatus) {
881 case nsIGfxInfo::FEATURE_ALLOW_ALWAYS:
882 case nsIGfxInfo::FEATURE_ALLOW_QUALIFIED:
883 return true;
884 default:
885 return false;
889 // Matching OS go somewhat beyond the simple equality check because of the
890 // "All Windows" and "All OS X" variations.
892 // aBlockedOS is describing the system(s) we are trying to block.
893 // aSystemOS is describing the system we are running on.
895 // aSystemOS should not be "Windows" or "OSX" - it should be set to
896 // a particular version instead.
897 // However, it is valid for aBlockedOS to be one of those generic values,
898 // as we could be blocking all of the versions.
899 inline bool MatchingOperatingSystems(OperatingSystem aBlockedOS,
900 OperatingSystem aSystemOS,
901 uint32_t aSystemOSBuild) {
902 MOZ_ASSERT(aSystemOS != OperatingSystem::Windows &&
903 aSystemOS != OperatingSystem::OSX);
905 // If the block entry OS is unknown, it doesn't match
906 if (aBlockedOS == OperatingSystem::Unknown) {
907 return false;
910 #if defined(XP_WIN)
911 if (aBlockedOS == OperatingSystem::Windows) {
912 // We do want even "unknown" aSystemOS to fall under "all windows"
913 return true;
916 constexpr uint32_t kMinWin10BuildNumber = 18362;
917 if (aBlockedOS == OperatingSystem::RecentWindows10 &&
918 aSystemOS == OperatingSystem::Windows10) {
919 // For allowlist purposes, we sometimes want to restrict to only recent
920 // versions of Windows 10. This is a bit of a kludge but easier than adding
921 // complicated blocklist infrastructure for build ID comparisons like driver
922 // versions.
923 return aSystemOSBuild >= kMinWin10BuildNumber;
926 if (aBlockedOS == OperatingSystem::NotRecentWindows10) {
927 if (aSystemOS == OperatingSystem::Windows10) {
928 return aSystemOSBuild < kMinWin10BuildNumber;
929 } else {
930 return true;
933 #endif
935 #if defined(XP_MACOSX)
936 if (aBlockedOS == OperatingSystem::OSX) {
937 // We do want even "unknown" aSystemOS to fall under "all OS X"
938 return true;
940 #endif
942 return aSystemOS == aBlockedOS;
945 inline bool MatchingBattery(BatteryStatus aBatteryStatus, bool aHasBattery) {
946 switch (aBatteryStatus) {
947 case BatteryStatus::All:
948 return true;
949 case BatteryStatus::None:
950 return !aHasBattery;
951 case BatteryStatus::Present:
952 return aHasBattery;
955 MOZ_ASSERT_UNREACHABLE("bad battery status");
956 return false;
959 inline bool MatchingScreenSize(ScreenSizeStatus aScreenStatus,
960 int64_t aScreenPixels) {
961 constexpr int64_t kMaxSmallPixels = 2304000; // 1920x1200
962 constexpr int64_t kMaxMediumPixels = 4953600; // 3440x1440
964 switch (aScreenStatus) {
965 case ScreenSizeStatus::All:
966 return true;
967 case ScreenSizeStatus::Small:
968 return aScreenPixels <= kMaxSmallPixels;
969 case ScreenSizeStatus::SmallAndMedium:
970 return aScreenPixels <= kMaxMediumPixels;
971 case ScreenSizeStatus::Medium:
972 return aScreenPixels > kMaxSmallPixels &&
973 aScreenPixels <= kMaxMediumPixels;
974 case ScreenSizeStatus::MediumAndLarge:
975 return aScreenPixels > kMaxSmallPixels;
976 case ScreenSizeStatus::Large:
977 return aScreenPixels > kMaxMediumPixels;
980 MOZ_ASSERT_UNREACHABLE("bad screen status");
981 return false;
984 int32_t GfxInfoBase::FindBlocklistedDeviceInList(
985 const nsTArray<GfxDriverInfo>& info, nsAString& aSuggestedVersion,
986 int32_t aFeature, nsACString& aFailureId, OperatingSystem os,
987 bool aForAllowing) {
988 int32_t status = nsIGfxInfo::FEATURE_STATUS_UNKNOWN;
990 // Some properties are not available on all platforms.
991 nsAutoString windowProtocol;
992 nsresult rv = GetWindowProtocol(windowProtocol);
993 if (NS_FAILED(rv) && rv != NS_ERROR_NOT_IMPLEMENTED) {
994 return 0;
997 bool hasBattery = false;
998 rv = GetHasBattery(&hasBattery);
999 if (NS_FAILED(rv) && rv != NS_ERROR_NOT_IMPLEMENTED) {
1000 return 0;
1003 uint32_t osBuild = OperatingSystemBuild();
1005 // Get the adapters once then reuse below
1006 nsAutoString adapterVendorID[2];
1007 nsAutoString adapterDeviceID[2];
1008 nsAutoString adapterDriverVendor[2];
1009 nsAutoString adapterDriverVersionString[2];
1010 bool adapterInfoFailed[2];
1012 adapterInfoFailed[0] =
1013 (NS_FAILED(GetAdapterVendorID(adapterVendorID[0])) ||
1014 NS_FAILED(GetAdapterDeviceID(adapterDeviceID[0])) ||
1015 NS_FAILED(GetAdapterDriverVendor(adapterDriverVendor[0])) ||
1016 NS_FAILED(GetAdapterDriverVersion(adapterDriverVersionString[0])));
1017 adapterInfoFailed[1] =
1018 (NS_FAILED(GetAdapterVendorID2(adapterVendorID[1])) ||
1019 NS_FAILED(GetAdapterDeviceID2(adapterDeviceID[1])) ||
1020 NS_FAILED(GetAdapterDriverVendor2(adapterDriverVendor[1])) ||
1021 NS_FAILED(GetAdapterDriverVersion2(adapterDriverVersionString[1])));
1022 // No point in going on if we don't have adapter info
1023 if (adapterInfoFailed[0] && adapterInfoFailed[1]) {
1024 return 0;
1027 #if defined(XP_WIN) || defined(ANDROID) || defined(MOZ_WIDGET_GTK)
1028 uint64_t driverVersion[2] = {0, 0};
1029 if (!adapterInfoFailed[0]) {
1030 ParseDriverVersion(adapterDriverVersionString[0], &driverVersion[0]);
1032 if (!adapterInfoFailed[1]) {
1033 ParseDriverVersion(adapterDriverVersionString[1], &driverVersion[1]);
1035 #endif
1037 uint32_t i = 0;
1038 for (; i < info.Length(); i++) {
1039 // If the status is FEATURE_ALLOW_*, then it is for the allowlist, not
1040 // blocklisting. Only consider entries for our search mode.
1041 if (MatchingAllowStatus(info[i].mFeatureStatus) != aForAllowing) {
1042 continue;
1045 // If we don't have the info for this GPU, no need to check further.
1046 // It is unclear that we would ever have a mixture of 1st and 2nd
1047 // GPU, but leaving the code in for that possibility for now.
1048 // (Actually, currently mGpu2 will never be true, so this can
1049 // be optimized out.)
1050 uint32_t infoIndex = info[i].mGpu2 ? 1 : 0;
1051 if (adapterInfoFailed[infoIndex]) {
1052 continue;
1055 // Do the operating system check first, no point in getting the driver
1056 // info if we won't need to use it.
1057 if (!MatchingOperatingSystems(info[i].mOperatingSystem, os, osBuild)) {
1058 continue;
1061 if (info[i].mOperatingSystemVersion &&
1062 info[i].mOperatingSystemVersion != OperatingSystemVersion()) {
1063 continue;
1066 if (!MatchingBattery(info[i].mBattery, hasBattery)) {
1067 continue;
1070 if (!MatchingScreenSize(info[i].mScreen, mScreenPixels)) {
1071 continue;
1074 if (!DoesWindowProtocolMatch(info[i].mWindowProtocol, windowProtocol)) {
1075 continue;
1078 if (!DoesVendorMatch(info[i].mAdapterVendor, adapterVendorID[infoIndex])) {
1079 continue;
1082 if (!DoesDriverVendorMatch(info[i].mDriverVendor,
1083 adapterDriverVendor[infoIndex])) {
1084 continue;
1087 if (info[i].mDevices && !info[i].mDevices->IsEmpty()) {
1088 nsresult rv = info[i].mDevices->Contains(adapterDeviceID[infoIndex]);
1089 if (rv == NS_ERROR_NOT_AVAILABLE) {
1090 // Not found
1091 continue;
1093 if (rv != NS_OK) {
1094 // Failed to search, allowlist should not match, blocklist should match
1095 // for safety reasons
1096 if (aForAllowing) {
1097 continue;
1099 break;
1103 bool match = false;
1105 if (!info[i].mHardware.IsEmpty() && !info[i].mHardware.Equals(Hardware())) {
1106 continue;
1108 if (!info[i].mModel.IsEmpty() && !info[i].mModel.Equals(Model())) {
1109 continue;
1111 if (!info[i].mProduct.IsEmpty() && !info[i].mProduct.Equals(Product())) {
1112 continue;
1114 if (!info[i].mManufacturer.IsEmpty() &&
1115 !info[i].mManufacturer.Equals(Manufacturer())) {
1116 continue;
1119 #if defined(XP_WIN) || defined(ANDROID) || defined(MOZ_WIDGET_GTK)
1120 switch (info[i].mComparisonOp) {
1121 case DRIVER_LESS_THAN:
1122 match = driverVersion[infoIndex] < info[i].mDriverVersion;
1123 break;
1124 case DRIVER_BUILD_ID_LESS_THAN:
1125 match = (driverVersion[infoIndex] & 0xFFFF) < info[i].mDriverVersion;
1126 break;
1127 case DRIVER_LESS_THAN_OR_EQUAL:
1128 match = driverVersion[infoIndex] <= info[i].mDriverVersion;
1129 break;
1130 case DRIVER_BUILD_ID_LESS_THAN_OR_EQUAL:
1131 match = (driverVersion[infoIndex] & 0xFFFF) <= info[i].mDriverVersion;
1132 break;
1133 case DRIVER_GREATER_THAN:
1134 match = driverVersion[infoIndex] > info[i].mDriverVersion;
1135 break;
1136 case DRIVER_GREATER_THAN_OR_EQUAL:
1137 match = driverVersion[infoIndex] >= info[i].mDriverVersion;
1138 break;
1139 case DRIVER_EQUAL:
1140 match = driverVersion[infoIndex] == info[i].mDriverVersion;
1141 break;
1142 case DRIVER_NOT_EQUAL:
1143 match = driverVersion[infoIndex] != info[i].mDriverVersion;
1144 break;
1145 case DRIVER_BETWEEN_EXCLUSIVE:
1146 match = driverVersion[infoIndex] > info[i].mDriverVersion &&
1147 driverVersion[infoIndex] < info[i].mDriverVersionMax;
1148 break;
1149 case DRIVER_BETWEEN_INCLUSIVE:
1150 match = driverVersion[infoIndex] >= info[i].mDriverVersion &&
1151 driverVersion[infoIndex] <= info[i].mDriverVersionMax;
1152 break;
1153 case DRIVER_BETWEEN_INCLUSIVE_START:
1154 match = driverVersion[infoIndex] >= info[i].mDriverVersion &&
1155 driverVersion[infoIndex] < info[i].mDriverVersionMax;
1156 break;
1157 case DRIVER_COMPARISON_IGNORED:
1158 // We don't have a comparison op, so we match everything.
1159 match = true;
1160 break;
1161 default:
1162 NS_WARNING("Bogus op in GfxDriverInfo");
1163 break;
1165 #else
1166 // We don't care what driver version it was. We only check OS version and if
1167 // the device matches.
1168 match = true;
1169 #endif
1171 if (match || info[i].mDriverVersion == GfxDriverInfo::allDriverVersions) {
1172 if (info[i].mFeature == GfxDriverInfo::allFeatures ||
1173 info[i].mFeature == aFeature) {
1174 status = info[i].mFeatureStatus;
1175 if (!info[i].mRuleId.IsEmpty()) {
1176 aFailureId = info[i].mRuleId.get();
1177 } else {
1178 aFailureId = "FEATURE_FAILURE_DL_BLOCKLIST_NO_ID";
1180 break;
1185 #if defined(XP_WIN)
1186 // As a very special case, we block D2D on machines with an NVidia 310M GPU
1187 // as either the primary or secondary adapter. D2D is also blocked when the
1188 // NV 310M is the primary adapter (using the standard blocklisting mechanism).
1189 // If the primary GPU already matched something in the blocklist then we
1190 // ignore this special rule. See bug 1008759.
1191 if (status == nsIGfxInfo::FEATURE_STATUS_UNKNOWN &&
1192 (aFeature == nsIGfxInfo::FEATURE_DIRECT2D)) {
1193 if (!adapterInfoFailed[1]) {
1194 nsAString& nvVendorID =
1195 (nsAString&)GfxDriverInfo::GetDeviceVendor(DeviceVendor::NVIDIA);
1196 const nsString nv310mDeviceId = u"0x0A70"_ns;
1197 if (nvVendorID.Equals(adapterVendorID[1],
1198 nsCaseInsensitiveStringComparator) &&
1199 nv310mDeviceId.Equals(adapterDeviceID[1],
1200 nsCaseInsensitiveStringComparator)) {
1201 status = nsIGfxInfo::FEATURE_BLOCKED_DEVICE;
1202 aFailureId = "FEATURE_FAILURE_D2D_NV310M_BLOCK";
1207 // Depends on Windows driver versioning. We don't pass a GfxDriverInfo object
1208 // back to the Windows handler, so we must handle this here.
1209 if (status == FEATURE_BLOCKED_DRIVER_VERSION) {
1210 if (info[i].mSuggestedVersion) {
1211 aSuggestedVersion.AppendPrintf("%s", info[i].mSuggestedVersion);
1212 } else if (info[i].mComparisonOp == DRIVER_LESS_THAN &&
1213 info[i].mDriverVersion != GfxDriverInfo::allDriverVersions) {
1214 aSuggestedVersion.AppendPrintf(
1215 "%lld.%lld.%lld.%lld",
1216 (info[i].mDriverVersion & 0xffff000000000000) >> 48,
1217 (info[i].mDriverVersion & 0x0000ffff00000000) >> 32,
1218 (info[i].mDriverVersion & 0x00000000ffff0000) >> 16,
1219 (info[i].mDriverVersion & 0x000000000000ffff));
1222 #endif
1224 return status;
1227 void GfxInfoBase::SetFeatureStatus(nsTArray<gfx::GfxInfoFeatureStatus>&& aFS) {
1228 MOZ_ASSERT(!sFeatureStatus);
1229 InitFeatureStatus(new nsTArray<gfx::GfxInfoFeatureStatus>(std::move(aFS)));
1232 bool GfxInfoBase::DoesWindowProtocolMatch(
1233 const nsAString& aBlocklistWindowProtocol,
1234 const nsAString& aWindowProtocol) {
1235 return aBlocklistWindowProtocol.Equals(aWindowProtocol,
1236 nsCaseInsensitiveStringComparator) ||
1237 aBlocklistWindowProtocol.Equals(
1238 GfxDriverInfo::GetWindowProtocol(WindowProtocol::All),
1239 nsCaseInsensitiveStringComparator);
1242 bool GfxInfoBase::DoesVendorMatch(const nsAString& aBlocklistVendor,
1243 const nsAString& aAdapterVendor) {
1244 return aBlocklistVendor.Equals(aAdapterVendor,
1245 nsCaseInsensitiveStringComparator) ||
1246 aBlocklistVendor.Equals(
1247 GfxDriverInfo::GetDeviceVendor(DeviceVendor::All),
1248 nsCaseInsensitiveStringComparator);
1251 bool GfxInfoBase::DoesDriverVendorMatch(const nsAString& aBlocklistVendor,
1252 const nsAString& aDriverVendor) {
1253 return aBlocklistVendor.Equals(aDriverVendor,
1254 nsCaseInsensitiveStringComparator) ||
1255 aBlocklistVendor.Equals(
1256 GfxDriverInfo::GetDriverVendor(DriverVendor::All),
1257 nsCaseInsensitiveStringComparator);
1260 bool GfxInfoBase::IsFeatureAllowlisted(int32_t aFeature) const {
1261 return aFeature == nsIGfxInfo::FEATURE_VIDEO_OVERLAY ||
1262 aFeature == nsIGfxInfo::FEATURE_HW_DECODED_VIDEO_ZERO_COPY;
1265 nsresult GfxInfoBase::GetFeatureStatusImpl(
1266 int32_t aFeature, int32_t* aStatus, nsAString& aSuggestedVersion,
1267 const nsTArray<GfxDriverInfo>& aDriverInfo, nsACString& aFailureId,
1268 OperatingSystem* aOS /* = nullptr */) {
1269 if (aFeature <= 0) {
1270 gfxWarning() << "Invalid feature <= 0";
1271 return NS_OK;
1274 if (*aStatus != nsIGfxInfo::FEATURE_STATUS_UNKNOWN) {
1275 // Terminate now with the status determined by the derived type (OS-specific
1276 // code).
1277 return NS_OK;
1280 if (sShutdownOccurred) {
1281 // This is futile; we've already commenced shutdown and our blocklists have
1282 // been deleted. We may want to look into resurrecting the blocklist instead
1283 // but for now, just don't even go there.
1284 return NS_OK;
1287 // Ensure any additional initialization required is complete.
1288 GetData();
1290 // If an operating system was provided by the derived GetFeatureStatusImpl,
1291 // grab it here. Otherwise, the OS is unknown.
1292 OperatingSystem os = (aOS ? *aOS : OperatingSystem::Unknown);
1294 nsAutoString adapterVendorID;
1295 nsAutoString adapterDeviceID;
1296 nsAutoString adapterDriverVersionString;
1297 if (NS_FAILED(GetAdapterVendorID(adapterVendorID)) ||
1298 NS_FAILED(GetAdapterDeviceID(adapterDeviceID)) ||
1299 NS_FAILED(GetAdapterDriverVersion(adapterDriverVersionString))) {
1300 aFailureId = "FEATURE_FAILURE_CANT_RESOLVE_ADAPTER";
1301 *aStatus = FEATURE_BLOCKED_DEVICE;
1302 return NS_OK;
1305 // We only check either the given blocklist, or the static list, as given.
1306 int32_t status;
1307 if (aDriverInfo.Length()) {
1308 status =
1309 FindBlocklistedDeviceInList(aDriverInfo, aSuggestedVersion, aFeature,
1310 aFailureId, os, /* aForAllowing */ false);
1311 } else {
1312 if (!sDriverInfo) {
1313 sDriverInfo = new nsTArray<GfxDriverInfo>();
1315 status = FindBlocklistedDeviceInList(GetGfxDriverInfo(), aSuggestedVersion,
1316 aFeature, aFailureId, os,
1317 /* aForAllowing */ false);
1320 if (status == nsIGfxInfo::FEATURE_STATUS_UNKNOWN) {
1321 if (IsFeatureAllowlisted(aFeature)) {
1322 // This feature is actually using the allowlist; that means after we pass
1323 // the blocklist to prevent us explicitly from getting the feature, we now
1324 // need to check the allowlist to ensure we are allowed to get it in the
1325 // first place.
1326 if (aDriverInfo.Length()) {
1327 status = FindBlocklistedDeviceInList(aDriverInfo, aSuggestedVersion,
1328 aFeature, aFailureId, os,
1329 /* aForAllowing */ true);
1330 } else {
1331 status = FindBlocklistedDeviceInList(
1332 GetGfxDriverInfo(), aSuggestedVersion, aFeature, aFailureId, os,
1333 /* aForAllowing */ true);
1336 if (status == nsIGfxInfo::FEATURE_STATUS_UNKNOWN) {
1337 status = nsIGfxInfo::FEATURE_DENIED;
1339 } else {
1340 // It's now done being processed. It's safe to set the status to
1341 // STATUS_OK.
1342 status = nsIGfxInfo::FEATURE_STATUS_OK;
1346 *aStatus = status;
1347 return NS_OK;
1350 NS_IMETHODIMP
1351 GfxInfoBase::GetFeatureSuggestedDriverVersion(int32_t aFeature,
1352 nsAString& aVersion) {
1353 nsCString version;
1354 if (GetPrefValueForDriverVersion(version)) {
1355 aVersion = NS_ConvertASCIItoUTF16(version);
1356 return NS_OK;
1359 int32_t status;
1360 nsCString discardFailureId;
1361 nsTArray<GfxDriverInfo> driverInfo;
1362 return GetFeatureStatusImpl(aFeature, &status, aVersion, driverInfo,
1363 discardFailureId);
1366 void GfxInfoBase::EvaluateDownloadedBlocklist(
1367 nsTArray<GfxDriverInfo>& aDriverInfo) {
1368 // If the list is empty, then we don't actually want to call
1369 // GetFeatureStatusImpl since we will use the static list instead. In that
1370 // case, all we want to do is make sure the pref is removed.
1371 if (aDriverInfo.IsEmpty()) {
1372 gfxCriticalNoteOnce << "Evaluate empty downloaded blocklist";
1373 return;
1376 OperatingSystem os = GetOperatingSystem();
1378 // For every feature we know about, we evaluate whether this blocklist has a
1379 // non-STATUS_OK status. If it does, we set the pref we evaluate in
1380 // GetFeatureStatus above, so we don't need to hold on to this blocklist
1381 // anywhere permanent.
1382 for (int feature = 1; feature <= nsIGfxInfo::FEATURE_MAX_VALUE; ++feature) {
1383 int32_t status = nsIGfxInfo::FEATURE_STATUS_UNKNOWN;
1384 nsCString failureId;
1385 nsAutoString suggestedVersion;
1387 // Note that we are careful to call the base class method since we only want
1388 // to evaluate the downloadable blocklist for these prefs.
1389 MOZ_ALWAYS_TRUE(NS_SUCCEEDED(GfxInfoBase::GetFeatureStatusImpl(
1390 feature, &status, suggestedVersion, aDriverInfo, failureId, &os)));
1392 switch (status) {
1393 default:
1394 MOZ_FALLTHROUGH_ASSERT("Unhandled feature status!");
1395 case nsIGfxInfo::FEATURE_STATUS_UNKNOWN:
1396 // This may be returned during shutdown or for invalid features.
1397 case nsIGfxInfo::FEATURE_ALLOW_ALWAYS:
1398 case nsIGfxInfo::FEATURE_ALLOW_QUALIFIED:
1399 case nsIGfxInfo::FEATURE_DENIED:
1400 // We cannot use the downloadable blocklist to control the allowlist.
1401 // If a feature is allowlisted, then we should also ignore DENIED
1402 // statuses from GetFeatureStatusImpl because we don't check the
1403 // static list when and this is an expected value. If we wish to
1404 // override the allowlist, it is as simple as creating a normal
1405 // blocklist rule with a BLOCKED* status code.
1406 case nsIGfxInfo::FEATURE_STATUS_OK:
1407 RemovePrefForFeature(feature);
1408 break;
1410 case nsIGfxInfo::FEATURE_BLOCKED_DRIVER_VERSION:
1411 if (!suggestedVersion.IsEmpty()) {
1412 SetPrefValueForDriverVersion(suggestedVersion);
1413 } else {
1414 RemovePrefForDriverVersion();
1416 [[fallthrough]];
1418 case nsIGfxInfo::FEATURE_BLOCKED_MISMATCHED_VERSION:
1419 case nsIGfxInfo::FEATURE_BLOCKED_DEVICE:
1420 case nsIGfxInfo::FEATURE_DISCOURAGED:
1421 case nsIGfxInfo::FEATURE_BLOCKED_OS_VERSION:
1422 case nsIGfxInfo::FEATURE_BLOCKED_PLATFORM_TEST:
1423 SetPrefValueForFeature(feature, status, failureId);
1424 break;
1429 NS_IMETHODIMP_(void)
1430 GfxInfoBase::LogFailure(const nsACString& failure) {
1431 // gfxCriticalError has a mutex lock of its own, so we may not actually
1432 // need this lock. ::GetFailures() accesses the data but the LogForwarder
1433 // will not return the copy of the logs unless it can get the same lock
1434 // that gfxCriticalError uses. Still, that is so much of an implementation
1435 // detail that it's nicer to just add an extra lock here and in
1436 // ::GetFailures()
1437 MutexAutoLock lock(mMutex);
1439 // By default, gfxCriticalError asserts; make it not assert in this case.
1440 gfxCriticalError(CriticalLog::DefaultOptions(false))
1441 << "(LF) " << failure.BeginReading();
1444 NS_IMETHODIMP GfxInfoBase::GetFailures(nsTArray<int32_t>& indices,
1445 nsTArray<nsCString>& failures) {
1446 MutexAutoLock lock(mMutex);
1448 LogForwarder* logForwarder = Factory::GetLogForwarder();
1449 if (!logForwarder) {
1450 return NS_ERROR_UNEXPECTED;
1453 // There are two string copies in this method, starting with this one. We are
1454 // assuming this is not a big deal, as the size of the array should be small
1455 // and the strings in it should be small as well (the error messages in the
1456 // code.) The second copy happens with the AppendElement() calls.
1457 // Technically, we don't need the mutex lock after the StringVectorCopy()
1458 // call.
1459 LoggingRecord loggedStrings = logForwarder->LoggingRecordCopy();
1460 LoggingRecord::const_iterator it;
1461 for (it = loggedStrings.begin(); it != loggedStrings.end(); ++it) {
1462 failures.AppendElement(nsDependentCSubstring(std::get<1>(*it).c_str(),
1463 std::get<1>(*it).size()));
1464 indices.AppendElement(std::get<0>(*it));
1467 return NS_OK;
1470 nsTArray<GfxInfoCollectorBase*>* sCollectors;
1472 static void InitCollectors() {
1473 if (!sCollectors) sCollectors = new nsTArray<GfxInfoCollectorBase*>;
1476 nsresult GfxInfoBase::GetInfo(JSContext* aCx,
1477 JS::MutableHandle<JS::Value> aResult) {
1478 InitCollectors();
1479 InfoObject obj(aCx);
1481 for (uint32_t i = 0; i < sCollectors->Length(); i++) {
1482 (*sCollectors)[i]->GetInfo(obj);
1485 // Some example property definitions
1486 // obj.DefineProperty("wordCacheSize", gfxTextRunWordCache::Count());
1487 // obj.DefineProperty("renderer", mRendererIDsString);
1488 // obj.DefineProperty("five", 5);
1490 if (!obj.mOk) {
1491 return NS_ERROR_FAILURE;
1494 aResult.setObject(*obj.mObj);
1495 return NS_OK;
1498 nsAutoCString gBaseAppVersion;
1500 const nsCString& GfxInfoBase::GetApplicationVersion() {
1501 static bool versionInitialized = false;
1502 if (!versionInitialized) {
1503 // If we fail to get the version, we will not try again.
1504 versionInitialized = true;
1506 // Get the version from xpcom/system/nsIXULAppInfo.idl
1507 nsCOMPtr<nsIXULAppInfo> app = do_GetService("@mozilla.org/xre/app-info;1");
1508 if (app) {
1509 app->GetVersion(gBaseAppVersion);
1512 return gBaseAppVersion;
1515 void GfxInfoBase::AddCollector(GfxInfoCollectorBase* collector) {
1516 InitCollectors();
1517 sCollectors->AppendElement(collector);
1520 void GfxInfoBase::RemoveCollector(GfxInfoCollectorBase* collector) {
1521 InitCollectors();
1522 for (uint32_t i = 0; i < sCollectors->Length(); i++) {
1523 if ((*sCollectors)[i] == collector) {
1524 sCollectors->RemoveElementAt(i);
1525 break;
1528 if (sCollectors->IsEmpty()) {
1529 delete sCollectors;
1530 sCollectors = nullptr;
1534 static void AppendMonitor(JSContext* aCx, widget::Screen& aScreen,
1535 JS::Handle<JSObject*> aOutArray, int32_t aIndex) {
1536 JS::Rooted<JSObject*> obj(aCx, JS_NewPlainObject(aCx));
1538 auto screenSize = aScreen.GetRect().Size();
1540 JS::Rooted<JS::Value> screenWidth(aCx, JS::Int32Value(screenSize.width));
1541 JS_SetProperty(aCx, obj, "screenWidth", screenWidth);
1543 JS::Rooted<JS::Value> screenHeight(aCx, JS::Int32Value(screenSize.height));
1544 JS_SetProperty(aCx, obj, "screenHeight", screenHeight);
1546 // XXX Just preserving behavior since this is exposed to telemetry, but we
1547 // could consider including this everywhere.
1548 #ifdef XP_MACOSX
1549 JS::Rooted<JS::Value> scale(
1550 aCx, JS::NumberValue(aScreen.GetContentsScaleFactor()));
1551 JS_SetProperty(aCx, obj, "scale", scale);
1552 #endif
1554 #ifdef XP_WIN
1555 JS::Rooted<JS::Value> refreshRate(aCx,
1556 JS::Int32Value(aScreen.GetRefreshRate()));
1557 JS_SetProperty(aCx, obj, "refreshRate", refreshRate);
1559 JS::Rooted<JS::Value> pseudoDisplay(
1560 aCx, JS::BooleanValue(aScreen.GetIsPseudoDisplay()));
1561 JS_SetProperty(aCx, obj, "pseudoDisplay", pseudoDisplay);
1562 #endif
1564 JS::Rooted<JS::Value> element(aCx, JS::ObjectValue(*obj));
1565 JS_SetElement(aCx, aOutArray, aIndex, element);
1568 nsresult GfxInfoBase::FindMonitors(JSContext* aCx,
1569 JS::Handle<JSObject*> aOutArray) {
1570 int32_t index = 0;
1571 auto& sm = ScreenManager::GetSingleton();
1572 for (auto& screen : sm.CurrentScreenList()) {
1573 AppendMonitor(aCx, *screen, aOutArray, index++);
1576 if (index == 0) {
1577 // Ensure we return at least one monitor, this is needed for xpcshell.
1578 RefPtr<Screen> screen = sm.GetPrimaryScreen();
1579 AppendMonitor(aCx, *screen, aOutArray, index++);
1582 return NS_OK;
1585 NS_IMETHODIMP
1586 GfxInfoBase::GetMonitors(JSContext* aCx, JS::MutableHandle<JS::Value> aResult) {
1587 JS::Rooted<JSObject*> array(aCx, JS::NewArrayObject(aCx, 0));
1589 nsresult rv = FindMonitors(aCx, array);
1590 if (NS_FAILED(rv)) {
1591 return rv;
1594 aResult.setObject(*array);
1595 return NS_OK;
1598 static inline bool SetJSPropertyString(JSContext* aCx,
1599 JS::Handle<JSObject*> aObj,
1600 const char* aProp, const char* aString) {
1601 JS::Rooted<JSString*> str(aCx, JS_NewStringCopyZ(aCx, aString));
1602 if (!str) {
1603 return false;
1606 JS::Rooted<JS::Value> val(aCx, JS::StringValue(str));
1607 return JS_SetProperty(aCx, aObj, aProp, val);
1610 template <typename T>
1611 static inline bool AppendJSElement(JSContext* aCx, JS::Handle<JSObject*> aObj,
1612 const T& aValue) {
1613 uint32_t index;
1614 if (!JS::GetArrayLength(aCx, aObj, &index)) {
1615 return false;
1617 return JS_SetElement(aCx, aObj, index, aValue);
1620 nsresult GfxInfoBase::GetFeatures(JSContext* aCx,
1621 JS::MutableHandle<JS::Value> aOut) {
1622 JS::Rooted<JSObject*> obj(aCx, JS_NewPlainObject(aCx));
1623 if (!obj) {
1624 return NS_ERROR_OUT_OF_MEMORY;
1626 aOut.setObject(*obj);
1628 layers::LayersBackend backend =
1629 gfxPlatform::Initialized()
1630 ? gfxPlatform::GetPlatform()->GetCompositorBackend()
1631 : layers::LayersBackend::LAYERS_NONE;
1632 const char* backendName = layers::GetLayersBackendName(backend);
1633 SetJSPropertyString(aCx, obj, "compositor", backendName);
1635 // If graphics isn't initialized yet, just stop now.
1636 if (!gfxPlatform::Initialized()) {
1637 return NS_OK;
1640 DescribeFeatures(aCx, obj);
1641 return NS_OK;
1644 nsresult GfxInfoBase::GetFeatureLog(JSContext* aCx,
1645 JS::MutableHandle<JS::Value> aOut) {
1646 JS::Rooted<JSObject*> containerObj(aCx, JS_NewPlainObject(aCx));
1647 if (!containerObj) {
1648 return NS_ERROR_OUT_OF_MEMORY;
1650 aOut.setObject(*containerObj);
1652 JS::Rooted<JSObject*> featureArray(aCx, JS::NewArrayObject(aCx, 0));
1653 if (!featureArray) {
1654 return NS_ERROR_OUT_OF_MEMORY;
1657 // Collect features.
1658 gfxConfig::ForEachFeature([&](const char* aName, const char* aDescription,
1659 FeatureState& aFeature) -> void {
1660 JS::Rooted<JSObject*> obj(aCx, JS_NewPlainObject(aCx));
1661 if (!obj) {
1662 return;
1664 if (!SetJSPropertyString(aCx, obj, "name", aName) ||
1665 !SetJSPropertyString(aCx, obj, "description", aDescription) ||
1666 !SetJSPropertyString(aCx, obj, "status",
1667 FeatureStatusToString(aFeature.GetValue()))) {
1668 return;
1671 JS::Rooted<JS::Value> log(aCx);
1672 if (!BuildFeatureStateLog(aCx, aFeature, &log)) {
1673 return;
1675 if (!JS_SetProperty(aCx, obj, "log", log)) {
1676 return;
1679 if (!AppendJSElement(aCx, featureArray, obj)) {
1680 return;
1684 JS::Rooted<JSObject*> fallbackArray(aCx, JS::NewArrayObject(aCx, 0));
1685 if (!fallbackArray) {
1686 return NS_ERROR_OUT_OF_MEMORY;
1689 // Collect fallbacks.
1690 gfxConfig::ForEachFallback(
1691 [&](const char* aName, const char* aMessage) -> void {
1692 JS::Rooted<JSObject*> obj(aCx, JS_NewPlainObject(aCx));
1693 if (!obj) {
1694 return;
1697 if (!SetJSPropertyString(aCx, obj, "name", aName) ||
1698 !SetJSPropertyString(aCx, obj, "message", aMessage)) {
1699 return;
1702 if (!AppendJSElement(aCx, fallbackArray, obj)) {
1703 return;
1707 JS::Rooted<JS::Value> val(aCx);
1709 val = JS::ObjectValue(*featureArray);
1710 JS_SetProperty(aCx, containerObj, "features", val);
1712 val = JS::ObjectValue(*fallbackArray);
1713 JS_SetProperty(aCx, containerObj, "fallbacks", val);
1715 return NS_OK;
1718 bool GfxInfoBase::BuildFeatureStateLog(JSContext* aCx,
1719 const FeatureState& aFeature,
1720 JS::MutableHandle<JS::Value> aOut) {
1721 JS::Rooted<JSObject*> log(aCx, JS::NewArrayObject(aCx, 0));
1722 if (!log) {
1723 return false;
1725 aOut.setObject(*log);
1727 aFeature.ForEachStatusChange([&](const char* aType, FeatureStatus aStatus,
1728 const char* aMessage,
1729 const nsCString& aFailureId) -> void {
1730 JS::Rooted<JSObject*> obj(aCx, JS_NewPlainObject(aCx));
1731 if (!obj) {
1732 return;
1735 if (!SetJSPropertyString(aCx, obj, "type", aType) ||
1736 !SetJSPropertyString(aCx, obj, "status",
1737 FeatureStatusToString(aStatus)) ||
1738 (!aFailureId.IsEmpty() &&
1739 !SetJSPropertyString(aCx, obj, "failureId", aFailureId.get())) ||
1740 (aMessage && !SetJSPropertyString(aCx, obj, "message", aMessage))) {
1741 return;
1744 if (!AppendJSElement(aCx, log, obj)) {
1745 return;
1749 return true;
1752 void GfxInfoBase::DescribeFeatures(JSContext* aCx, JS::Handle<JSObject*> aObj) {
1753 JS::Rooted<JSObject*> obj(aCx);
1755 gfx::FeatureState& hwCompositing =
1756 gfxConfig::GetFeature(gfx::Feature::HW_COMPOSITING);
1757 InitFeatureObject(aCx, aObj, "hwCompositing", hwCompositing, &obj);
1759 gfx::FeatureState& gpuProcess =
1760 gfxConfig::GetFeature(gfx::Feature::GPU_PROCESS);
1761 InitFeatureObject(aCx, aObj, "gpuProcess", gpuProcess, &obj);
1763 gfx::FeatureState& webrender = gfxConfig::GetFeature(gfx::Feature::WEBRENDER);
1764 InitFeatureObject(aCx, aObj, "webrender", webrender, &obj);
1766 gfx::FeatureState& wrCompositor =
1767 gfxConfig::GetFeature(gfx::Feature::WEBRENDER_COMPOSITOR);
1768 InitFeatureObject(aCx, aObj, "wrCompositor", wrCompositor, &obj);
1770 gfx::FeatureState& openglCompositing =
1771 gfxConfig::GetFeature(gfx::Feature::OPENGL_COMPOSITING);
1772 InitFeatureObject(aCx, aObj, "openglCompositing", openglCompositing, &obj);
1774 gfx::FeatureState& omtp = gfxConfig::GetFeature(gfx::Feature::OMTP);
1775 InitFeatureObject(aCx, aObj, "omtp", omtp, &obj);
1778 bool GfxInfoBase::InitFeatureObject(JSContext* aCx,
1779 JS::Handle<JSObject*> aContainer,
1780 const char* aName,
1781 mozilla::gfx::FeatureState& aFeatureState,
1782 JS::MutableHandle<JSObject*> aOutObj) {
1783 JS::Rooted<JSObject*> obj(aCx, JS_NewPlainObject(aCx));
1784 if (!obj) {
1785 return false;
1788 nsCString status = aFeatureState.GetStatusAndFailureIdString();
1790 JS::Rooted<JSString*> str(aCx, JS_NewStringCopyZ(aCx, status.get()));
1791 JS::Rooted<JS::Value> val(aCx, JS::StringValue(str));
1792 JS_SetProperty(aCx, obj, "status", val);
1794 // Add the feature object to the container.
1796 JS::Rooted<JS::Value> val(aCx, JS::ObjectValue(*obj));
1797 JS_SetProperty(aCx, aContainer, aName, val);
1800 aOutObj.set(obj);
1801 return true;
1804 nsresult GfxInfoBase::GetActiveCrashGuards(JSContext* aCx,
1805 JS::MutableHandle<JS::Value> aOut) {
1806 JS::Rooted<JSObject*> array(aCx, JS::NewArrayObject(aCx, 0));
1807 if (!array) {
1808 return NS_ERROR_OUT_OF_MEMORY;
1810 aOut.setObject(*array);
1812 DriverCrashGuard::ForEachActiveCrashGuard(
1813 [&](const char* aName, const char* aPrefName) -> void {
1814 JS::Rooted<JSObject*> obj(aCx, JS_NewPlainObject(aCx));
1815 if (!obj) {
1816 return;
1818 if (!SetJSPropertyString(aCx, obj, "type", aName)) {
1819 return;
1821 if (!SetJSPropertyString(aCx, obj, "prefName", aPrefName)) {
1822 return;
1824 if (!AppendJSElement(aCx, array, obj)) {
1825 return;
1829 return NS_OK;
1832 NS_IMETHODIMP
1833 GfxInfoBase::GetTargetFrameRate(uint32_t* aTargetFrameRate) {
1834 *aTargetFrameRate = gfxPlatform::TargetFrameRate();
1835 return NS_OK;
1838 NS_IMETHODIMP
1839 GfxInfoBase::GetCodecSupportInfo(nsACString& aCodecSupportInfo) {
1840 aCodecSupportInfo.Assign(gfx::gfxVars::CodecSupportInfo());
1841 return NS_OK;
1844 NS_IMETHODIMP
1845 GfxInfoBase::GetIsHeadless(bool* aIsHeadless) {
1846 *aIsHeadless = gfxPlatform::IsHeadless();
1847 return NS_OK;
1850 NS_IMETHODIMP
1851 GfxInfoBase::GetContentBackend(nsAString& aContentBackend) {
1852 BackendType backend = gfxPlatform::GetPlatform()->GetDefaultContentBackend();
1853 nsString outStr;
1855 switch (backend) {
1856 case BackendType::DIRECT2D1_1: {
1857 outStr.AppendPrintf("Direct2D 1.1");
1858 break;
1860 case BackendType::SKIA: {
1861 outStr.AppendPrintf("Skia");
1862 break;
1864 case BackendType::CAIRO: {
1865 outStr.AppendPrintf("Cairo");
1866 break;
1868 default:
1869 return NS_ERROR_FAILURE;
1872 aContentBackend.Assign(outStr);
1873 return NS_OK;
1876 NS_IMETHODIMP
1877 GfxInfoBase::GetAzureCanvasBackend(nsAString& aBackend) {
1878 CopyASCIItoUTF16(mozilla::MakeStringSpan(
1879 gfxPlatform::GetPlatform()->GetAzureCanvasBackend()),
1880 aBackend);
1881 return NS_OK;
1884 NS_IMETHODIMP
1885 GfxInfoBase::GetAzureContentBackend(nsAString& aBackend) {
1886 CopyASCIItoUTF16(mozilla::MakeStringSpan(
1887 gfxPlatform::GetPlatform()->GetAzureContentBackend()),
1888 aBackend);
1889 return NS_OK;
1892 NS_IMETHODIMP
1893 GfxInfoBase::GetUsingGPUProcess(bool* aOutValue) {
1894 GPUProcessManager* gpu = GPUProcessManager::Get();
1895 if (!gpu) {
1896 // Not supported in content processes.
1897 return NS_ERROR_FAILURE;
1900 *aOutValue = !!gpu->GetGPUChild();
1901 return NS_OK;
1904 NS_IMETHODIMP_(int32_t)
1905 GfxInfoBase::GetMaxRefreshRate(bool* aMixed) {
1906 if (aMixed) {
1907 *aMixed = false;
1910 int32_t maxRefreshRate = 0;
1911 for (auto& screen : ScreenManager::GetSingleton().CurrentScreenList()) {
1912 int32_t refreshRate = screen->GetRefreshRate();
1913 if (aMixed && maxRefreshRate > 0 && maxRefreshRate != refreshRate) {
1914 *aMixed = true;
1916 maxRefreshRate = std::max(maxRefreshRate, refreshRate);
1919 return maxRefreshRate > 0 ? maxRefreshRate : -1;
1922 NS_IMETHODIMP
1923 GfxInfoBase::ControlGPUProcessForXPCShell(bool aEnable, bool* _retval) {
1924 gfxPlatform::GetPlatform();
1926 GPUProcessManager* gpm = GPUProcessManager::Get();
1927 if (aEnable) {
1928 if (!gfxConfig::IsEnabled(gfx::Feature::GPU_PROCESS)) {
1929 gfxConfig::UserForceEnable(gfx::Feature::GPU_PROCESS, "xpcshell-test");
1931 DebugOnly<nsresult> rv = gpm->EnsureGPUReady();
1932 MOZ_ASSERT(rv != NS_ERROR_ILLEGAL_DURING_SHUTDOWN);
1933 } else {
1934 gfxConfig::UserDisable(gfx::Feature::GPU_PROCESS, "xpcshell-test");
1935 gpm->KillProcess();
1938 *_retval = true;
1939 return NS_OK;
1942 NS_IMETHODIMP GfxInfoBase::KillGPUProcessForTests() {
1943 GPUProcessManager* gpm = GPUProcessManager::Get();
1944 if (!gpm) {
1945 // gfxPlatform has not been initialized.
1946 return NS_ERROR_NOT_INITIALIZED;
1949 gpm->KillProcess();
1950 return NS_OK;
1953 NS_IMETHODIMP GfxInfoBase::CrashGPUProcessForTests() {
1954 GPUProcessManager* gpm = GPUProcessManager::Get();
1955 if (!gpm) {
1956 // gfxPlatform has not been initialized.
1957 return NS_ERROR_NOT_INITIALIZED;
1960 gpm->CrashProcess();
1961 return NS_OK;
1964 GfxInfoCollectorBase::GfxInfoCollectorBase() {
1965 GfxInfoBase::AddCollector(this);
1968 GfxInfoCollectorBase::~GfxInfoCollectorBase() {
1969 GfxInfoBase::RemoveCollector(this);