Bug 1799258 - [qcms] Add query for profile data and lut tables. r=jrmuizel
[gecko.git] / widget / GfxInfoBase.cpp
bloba95b7bb70f0ddd7edb9f85cf6ffe302b7f595498
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 "gfxPlatform.h"
40 #include "gfxConfig.h"
41 #include "DriverCrashGuard.h"
43 using namespace mozilla::widget;
44 using namespace mozilla;
45 using mozilla::MutexAutoLock;
47 nsTArray<GfxDriverInfo>* GfxInfoBase::sDriverInfo;
48 StaticAutoPtr<nsTArray<gfx::GfxInfoFeatureStatus>> GfxInfoBase::sFeatureStatus;
49 bool GfxInfoBase::sDriverInfoObserverInitialized;
50 bool GfxInfoBase::sShutdownOccurred;
52 // Call this when setting sFeatureStatus to a non-null pointer to
53 // ensure destruction even if the GfxInfo component is never instantiated.
54 static void InitFeatureStatus(nsTArray<gfx::GfxInfoFeatureStatus>* aPtr) {
55 static std::once_flag sOnce;
56 std::call_once(sOnce, [] { ClearOnShutdown(&GfxInfoBase::sFeatureStatus); });
57 GfxInfoBase::sFeatureStatus = aPtr;
60 // Observes for shutdown so that the child GfxDriverInfo list is freed.
61 class ShutdownObserver : public nsIObserver {
62 virtual ~ShutdownObserver() = default;
64 public:
65 ShutdownObserver() = default;
67 NS_DECL_ISUPPORTS
69 NS_IMETHOD Observe(nsISupports* subject, const char* aTopic,
70 const char16_t* aData) override {
71 MOZ_ASSERT(strcmp(aTopic, NS_XPCOM_SHUTDOWN_OBSERVER_ID) == 0);
73 delete GfxInfoBase::sDriverInfo;
74 GfxInfoBase::sDriverInfo = nullptr;
76 for (auto& deviceFamily : GfxDriverInfo::sDeviceFamilies) {
77 delete deviceFamily;
78 deviceFamily = nullptr;
81 for (auto& windowProtocol : GfxDriverInfo::sWindowProtocol) {
82 delete windowProtocol;
83 windowProtocol = nullptr;
86 for (auto& deviceVendor : GfxDriverInfo::sDeviceVendors) {
87 delete deviceVendor;
88 deviceVendor = nullptr;
91 for (auto& driverVendor : GfxDriverInfo::sDriverVendors) {
92 delete driverVendor;
93 driverVendor = nullptr;
96 GfxInfoBase::sShutdownOccurred = true;
98 return NS_OK;
102 NS_IMPL_ISUPPORTS(ShutdownObserver, nsIObserver)
104 static void InitGfxDriverInfoShutdownObserver() {
105 if (GfxInfoBase::sDriverInfoObserverInitialized) return;
107 GfxInfoBase::sDriverInfoObserverInitialized = true;
109 nsCOMPtr<nsIObserverService> observerService = services::GetObserverService();
110 if (!observerService) {
111 NS_WARNING("Could not get observer service!");
112 return;
115 ShutdownObserver* obs = new ShutdownObserver();
116 observerService->AddObserver(obs, NS_XPCOM_SHUTDOWN_OBSERVER_ID, false);
119 using namespace mozilla::widget;
120 using namespace mozilla::gfx;
121 using namespace mozilla;
123 NS_IMPL_ISUPPORTS(GfxInfoBase, nsIGfxInfo, nsIObserver,
124 nsISupportsWeakReference)
126 #define BLOCKLIST_PREF_BRANCH "gfx.blacklist."
127 #define SUGGESTED_VERSION_PREF BLOCKLIST_PREF_BRANCH "suggested-driver-version"
129 static const char* GetPrefNameForFeature(int32_t aFeature) {
130 const char* name = nullptr;
131 switch (aFeature) {
132 case nsIGfxInfo::FEATURE_DIRECT2D:
133 name = BLOCKLIST_PREF_BRANCH "direct2d";
134 break;
135 case nsIGfxInfo::FEATURE_DIRECT3D_9_LAYERS:
136 name = BLOCKLIST_PREF_BRANCH "layers.direct3d9";
137 break;
138 case nsIGfxInfo::FEATURE_DIRECT3D_10_LAYERS:
139 name = BLOCKLIST_PREF_BRANCH "layers.direct3d10";
140 break;
141 case nsIGfxInfo::FEATURE_DIRECT3D_10_1_LAYERS:
142 name = BLOCKLIST_PREF_BRANCH "layers.direct3d10-1";
143 break;
144 case nsIGfxInfo::FEATURE_DIRECT3D_11_LAYERS:
145 name = BLOCKLIST_PREF_BRANCH "layers.direct3d11";
146 break;
147 case nsIGfxInfo::FEATURE_DIRECT3D_11_ANGLE:
148 name = BLOCKLIST_PREF_BRANCH "direct3d11angle";
149 break;
150 case nsIGfxInfo::FEATURE_HARDWARE_VIDEO_DECODING:
151 name = BLOCKLIST_PREF_BRANCH "hardwarevideodecoding";
152 break;
153 case nsIGfxInfo::FEATURE_OPENGL_LAYERS:
154 name = BLOCKLIST_PREF_BRANCH "layers.opengl";
155 break;
156 case nsIGfxInfo::FEATURE_WEBGL_OPENGL:
157 name = BLOCKLIST_PREF_BRANCH "webgl.opengl";
158 break;
159 case nsIGfxInfo::FEATURE_WEBGL_ANGLE:
160 name = BLOCKLIST_PREF_BRANCH "webgl.angle";
161 break;
162 case nsIGfxInfo::UNUSED_FEATURE_WEBGL_MSAA:
163 name = BLOCKLIST_PREF_BRANCH "webgl.msaa";
164 break;
165 case nsIGfxInfo::FEATURE_STAGEFRIGHT:
166 name = BLOCKLIST_PREF_BRANCH "stagefright";
167 break;
168 case nsIGfxInfo::FEATURE_WEBRTC_HW_ACCELERATION_H264:
169 name = BLOCKLIST_PREF_BRANCH "webrtc.hw.acceleration.h264";
170 break;
171 case nsIGfxInfo::FEATURE_WEBRTC_HW_ACCELERATION_ENCODE:
172 name = BLOCKLIST_PREF_BRANCH "webrtc.hw.acceleration.encode";
173 break;
174 case nsIGfxInfo::FEATURE_WEBRTC_HW_ACCELERATION_DECODE:
175 name = BLOCKLIST_PREF_BRANCH "webrtc.hw.acceleration.decode";
176 break;
177 case nsIGfxInfo::FEATURE_CANVAS2D_ACCELERATION:
178 name = BLOCKLIST_PREF_BRANCH "canvas2d.acceleration";
179 break;
180 case nsIGfxInfo::FEATURE_DX_INTEROP2:
181 name = BLOCKLIST_PREF_BRANCH "dx.interop2";
182 break;
183 case nsIGfxInfo::FEATURE_GPU_PROCESS:
184 name = BLOCKLIST_PREF_BRANCH "gpu.process";
185 break;
186 case nsIGfxInfo::FEATURE_WEBGL2:
187 name = BLOCKLIST_PREF_BRANCH "webgl2";
188 break;
189 case nsIGfxInfo::FEATURE_D3D11_KEYED_MUTEX:
190 name = BLOCKLIST_PREF_BRANCH "d3d11.keyed.mutex";
191 break;
192 case nsIGfxInfo::FEATURE_WEBRENDER:
193 name = BLOCKLIST_PREF_BRANCH "webrender";
194 break;
195 case nsIGfxInfo::FEATURE_WEBRENDER_COMPOSITOR:
196 name = BLOCKLIST_PREF_BRANCH "webrender.compositor";
197 break;
198 case nsIGfxInfo::FEATURE_DX_NV12:
199 name = BLOCKLIST_PREF_BRANCH "dx.nv12";
200 break;
201 case nsIGfxInfo::FEATURE_DX_P010:
202 name = BLOCKLIST_PREF_BRANCH "dx.p010";
203 break;
204 case nsIGfxInfo::FEATURE_DX_P016:
205 name = BLOCKLIST_PREF_BRANCH "dx.p016";
206 break;
207 case nsIGfxInfo::FEATURE_VP8_HW_DECODE:
208 name = BLOCKLIST_PREF_BRANCH "vp8.hw-decode";
209 break;
210 case nsIGfxInfo::FEATURE_VP9_HW_DECODE:
211 name = BLOCKLIST_PREF_BRANCH "vp9.hw-decode";
212 break;
213 case nsIGfxInfo::FEATURE_GL_SWIZZLE:
214 name = BLOCKLIST_PREF_BRANCH "gl.swizzle";
215 break;
216 case nsIGfxInfo::FEATURE_WEBRENDER_SCISSORED_CACHE_CLEARS:
217 name = BLOCKLIST_PREF_BRANCH "webrender.scissored_cache_clears";
218 break;
219 case nsIGfxInfo::FEATURE_ALLOW_WEBGL_OUT_OF_PROCESS:
220 name = BLOCKLIST_PREF_BRANCH "webgl.allow-oop";
221 break;
222 case nsIGfxInfo::FEATURE_THREADSAFE_GL:
223 name = BLOCKLIST_PREF_BRANCH "gl.threadsafe";
224 break;
225 case nsIGfxInfo::FEATURE_WEBRENDER_OPTIMIZED_SHADERS:
226 name = BLOCKLIST_PREF_BRANCH "webrender.optimized-shaders";
227 break;
228 case nsIGfxInfo::FEATURE_X11_EGL:
229 name = BLOCKLIST_PREF_BRANCH "x11.egl";
230 break;
231 case nsIGfxInfo::FEATURE_DMABUF:
232 name = BLOCKLIST_PREF_BRANCH "dmabuf";
233 break;
234 case nsIGfxInfo::FEATURE_WEBGPU:
235 name = BLOCKLIST_PREF_BRANCH "webgpu";
236 break;
237 case nsIGfxInfo::FEATURE_VIDEO_OVERLAY:
238 name = BLOCKLIST_PREF_BRANCH "video-overlay";
239 break;
240 case nsIGfxInfo::FEATURE_HW_DECODED_VIDEO_ZERO_COPY:
241 name = BLOCKLIST_PREF_BRANCH "hw-video-zero-copy";
242 break;
243 case nsIGfxInfo::FEATURE_WEBRENDER_SHADER_CACHE:
244 name = BLOCKLIST_PREF_BRANCH "webrender.program-binary-disk";
245 break;
246 case nsIGfxInfo::FEATURE_WEBRENDER_PARTIAL_PRESENT:
247 name = BLOCKLIST_PREF_BRANCH "webrender.partial-present";
248 break;
249 case nsIGfxInfo::FEATURE_DMABUF_SURFACE_EXPORT:
250 name = BLOCKLIST_PREF_BRANCH "dmabuf.surface-export";
251 break;
252 case nsIGfxInfo::FEATURE_REUSE_DECODER_DEVICE:
253 name = BLOCKLIST_PREF_BRANCH "reuse-decoder-device";
254 break;
255 case nsIGfxInfo::FEATURE_BACKDROP_FILTER:
256 name = BLOCKLIST_PREF_BRANCH "backdrop.filter";
257 break;
258 case nsIGfxInfo::FEATURE_ACCELERATED_CANVAS2D:
259 name = BLOCKLIST_PREF_BRANCH "accelerated-canvas2d";
260 break;
261 default:
262 MOZ_ASSERT_UNREACHABLE("Unexpected nsIGfxInfo feature?!");
263 break;
266 return name;
269 // Returns the value of the pref for the relevant feature in aValue.
270 // If the pref doesn't exist, aValue is not touched, and returns false.
271 static bool GetPrefValueForFeature(int32_t aFeature, int32_t& aValue,
272 nsACString& aFailureId) {
273 const char* prefname = GetPrefNameForFeature(aFeature);
274 if (!prefname) return false;
276 aValue = nsIGfxInfo::FEATURE_STATUS_UNKNOWN;
277 if (!NS_SUCCEEDED(Preferences::GetInt(prefname, &aValue))) {
278 return false;
281 if (aValue == nsIGfxInfo::FEATURE_DENIED) {
282 // We should never see the DENIED status with the downloadable blocklist.
283 return false;
286 nsCString failureprefname(prefname);
287 failureprefname += ".failureid";
288 nsAutoCString failureValue;
289 nsresult rv = Preferences::GetCString(failureprefname.get(), failureValue);
290 if (NS_SUCCEEDED(rv)) {
291 aFailureId = failureValue.get();
292 } else {
293 aFailureId = "FEATURE_FAILURE_BLOCKLIST_PREF";
296 return true;
299 static void SetPrefValueForFeature(int32_t aFeature, int32_t aValue,
300 const nsACString& aFailureId) {
301 const char* prefname = GetPrefNameForFeature(aFeature);
302 if (!prefname) return;
303 if (XRE_IsParentProcess()) {
304 GfxInfoBase::sFeatureStatus = nullptr;
307 Preferences::SetInt(prefname, aValue);
308 if (!aFailureId.IsEmpty()) {
309 nsAutoCString failureprefname(prefname);
310 failureprefname += ".failureid";
311 Preferences::SetCString(failureprefname.get(), aFailureId);
315 static void RemovePrefForFeature(int32_t aFeature) {
316 const char* prefname = GetPrefNameForFeature(aFeature);
317 if (!prefname) return;
319 if (XRE_IsParentProcess()) {
320 GfxInfoBase::sFeatureStatus = nullptr;
322 Preferences::ClearUser(prefname);
325 static bool GetPrefValueForDriverVersion(nsCString& aVersion) {
326 return NS_SUCCEEDED(
327 Preferences::GetCString(SUGGESTED_VERSION_PREF, aVersion));
330 static void SetPrefValueForDriverVersion(const nsAString& aVersion) {
331 Preferences::SetString(SUGGESTED_VERSION_PREF, aVersion);
334 static void RemovePrefForDriverVersion() {
335 Preferences::ClearUser(SUGGESTED_VERSION_PREF);
338 static OperatingSystem BlocklistOSToOperatingSystem(const nsAString& os) {
339 if (os.EqualsLiteral("WINNT 6.1")) {
340 return OperatingSystem::Windows7;
342 if (os.EqualsLiteral("WINNT 6.2")) {
343 return OperatingSystem::Windows8;
345 if (os.EqualsLiteral("WINNT 6.3")) {
346 return OperatingSystem::Windows8_1;
348 if (os.EqualsLiteral("WINNT 10.0")) {
349 return OperatingSystem::Windows10;
351 if (os.EqualsLiteral("Linux")) {
352 return OperatingSystem::Linux;
354 if (os.EqualsLiteral("Darwin 9")) {
355 return OperatingSystem::OSX10_5;
357 if (os.EqualsLiteral("Darwin 10")) {
358 return OperatingSystem::OSX10_6;
360 if (os.EqualsLiteral("Darwin 11")) {
361 return OperatingSystem::OSX10_7;
363 if (os.EqualsLiteral("Darwin 12")) {
364 return OperatingSystem::OSX10_8;
366 if (os.EqualsLiteral("Darwin 13")) {
367 return OperatingSystem::OSX10_9;
369 if (os.EqualsLiteral("Darwin 14")) {
370 return OperatingSystem::OSX10_10;
372 if (os.EqualsLiteral("Darwin 15")) {
373 return OperatingSystem::OSX10_11;
375 if (os.EqualsLiteral("Darwin 16")) {
376 return OperatingSystem::OSX10_12;
378 if (os.EqualsLiteral("Darwin 17")) {
379 return OperatingSystem::OSX10_13;
381 if (os.EqualsLiteral("Darwin 18")) {
382 return OperatingSystem::OSX10_14;
384 if (os.EqualsLiteral("Darwin 19")) {
385 return OperatingSystem::OSX10_15;
387 if (os.EqualsLiteral("Darwin 20")) {
388 return OperatingSystem::OSX11_0;
390 if (os.EqualsLiteral("Android")) {
391 return OperatingSystem::Android;
392 // For historical reasons, "All" in blocklist means "All Windows"
394 if (os.EqualsLiteral("All")) {
395 return OperatingSystem::Windows;
397 if (os.EqualsLiteral("Darwin")) {
398 return OperatingSystem::OSX;
401 return OperatingSystem::Unknown;
404 static GfxDeviceFamily* BlocklistDevicesToDeviceFamily(
405 nsTArray<nsCString>& devices) {
406 if (devices.Length() == 0) return nullptr;
408 // For each device, get its device ID, and return a freshly-allocated
409 // GfxDeviceFamily with the contents of that array.
410 GfxDeviceFamily* deviceIds = new GfxDeviceFamily;
412 for (uint32_t i = 0; i < devices.Length(); ++i) {
413 // We make sure we don't add any "empty" device entries to the array, so
414 // we don't need to check if devices[i] is empty.
415 deviceIds->Append(NS_ConvertUTF8toUTF16(devices[i]));
418 return deviceIds;
421 static int32_t BlocklistFeatureToGfxFeature(const nsAString& aFeature) {
422 MOZ_ASSERT(!aFeature.IsEmpty());
423 if (aFeature.EqualsLiteral("DIRECT2D")) {
424 return nsIGfxInfo::FEATURE_DIRECT2D;
426 if (aFeature.EqualsLiteral("DIRECT3D_9_LAYERS")) {
427 return nsIGfxInfo::FEATURE_DIRECT3D_9_LAYERS;
429 if (aFeature.EqualsLiteral("DIRECT3D_10_LAYERS")) {
430 return nsIGfxInfo::FEATURE_DIRECT3D_10_LAYERS;
432 if (aFeature.EqualsLiteral("DIRECT3D_10_1_LAYERS")) {
433 return nsIGfxInfo::FEATURE_DIRECT3D_10_1_LAYERS;
435 if (aFeature.EqualsLiteral("DIRECT3D_11_LAYERS")) {
436 return nsIGfxInfo::FEATURE_DIRECT3D_11_LAYERS;
438 if (aFeature.EqualsLiteral("DIRECT3D_11_ANGLE")) {
439 return nsIGfxInfo::FEATURE_DIRECT3D_11_ANGLE;
441 if (aFeature.EqualsLiteral("HARDWARE_VIDEO_DECODING")) {
442 return nsIGfxInfo::FEATURE_HARDWARE_VIDEO_DECODING;
444 if (aFeature.EqualsLiteral("OPENGL_LAYERS")) {
445 return nsIGfxInfo::FEATURE_OPENGL_LAYERS;
447 if (aFeature.EqualsLiteral("WEBGL_OPENGL")) {
448 return nsIGfxInfo::FEATURE_WEBGL_OPENGL;
450 if (aFeature.EqualsLiteral("WEBGL_ANGLE")) {
451 return nsIGfxInfo::FEATURE_WEBGL_ANGLE;
453 if (aFeature.EqualsLiteral("WEBGL_MSAA")) {
454 return nsIGfxInfo::UNUSED_FEATURE_WEBGL_MSAA;
456 if (aFeature.EqualsLiteral("STAGEFRIGHT")) {
457 return nsIGfxInfo::FEATURE_STAGEFRIGHT;
459 if (aFeature.EqualsLiteral("WEBRTC_HW_ACCELERATION_ENCODE")) {
460 return nsIGfxInfo::FEATURE_WEBRTC_HW_ACCELERATION_ENCODE;
462 if (aFeature.EqualsLiteral("WEBRTC_HW_ACCELERATION_DECODE")) {
463 return nsIGfxInfo::FEATURE_WEBRTC_HW_ACCELERATION_DECODE;
465 if (aFeature.EqualsLiteral("WEBRTC_HW_ACCELERATION_H264")) {
466 return nsIGfxInfo::FEATURE_WEBRTC_HW_ACCELERATION_H264;
468 if (aFeature.EqualsLiteral("CANVAS2D_ACCELERATION")) {
469 return nsIGfxInfo::FEATURE_CANVAS2D_ACCELERATION;
471 if (aFeature.EqualsLiteral("DX_INTEROP2")) {
472 return nsIGfxInfo::FEATURE_DX_INTEROP2;
474 if (aFeature.EqualsLiteral("GPU_PROCESS")) {
475 return nsIGfxInfo::FEATURE_GPU_PROCESS;
477 if (aFeature.EqualsLiteral("WEBGL2")) {
478 return nsIGfxInfo::FEATURE_WEBGL2;
480 if (aFeature.EqualsLiteral("D3D11_KEYED_MUTEX")) {
481 return nsIGfxInfo::FEATURE_D3D11_KEYED_MUTEX;
483 if (aFeature.EqualsLiteral("WEBRENDER")) {
484 return nsIGfxInfo::FEATURE_WEBRENDER;
486 if (aFeature.EqualsLiteral("WEBRENDER_COMPOSITOR")) {
487 return nsIGfxInfo::FEATURE_WEBRENDER_COMPOSITOR;
489 if (aFeature.EqualsLiteral("DX_NV12")) {
490 return nsIGfxInfo::FEATURE_DX_NV12;
492 if (aFeature.EqualsLiteral("VP8_HW_DECODE")) {
493 return nsIGfxInfo::FEATURE_VP8_HW_DECODE;
495 if (aFeature.EqualsLiteral("VP9_HW_DECODE")) {
496 return nsIGfxInfo::FEATURE_VP9_HW_DECODE;
498 if (aFeature.EqualsLiteral("GL_SWIZZLE")) {
499 return nsIGfxInfo::FEATURE_GL_SWIZZLE;
501 if (aFeature.EqualsLiteral("WEBRENDER_SCISSORED_CACHE_CLEARS")) {
502 return nsIGfxInfo::FEATURE_WEBRENDER_SCISSORED_CACHE_CLEARS;
504 if (aFeature.EqualsLiteral("ALLOW_WEBGL_OUT_OF_PROCESS")) {
505 return nsIGfxInfo::FEATURE_ALLOW_WEBGL_OUT_OF_PROCESS;
507 if (aFeature.EqualsLiteral("THREADSAFE_GL")) {
508 return nsIGfxInfo::FEATURE_THREADSAFE_GL;
510 if (aFeature.EqualsLiteral("X11_EGL")) {
511 return nsIGfxInfo::FEATURE_X11_EGL;
513 if (aFeature.EqualsLiteral("DMABUF")) {
514 return nsIGfxInfo::FEATURE_DMABUF;
516 if (aFeature.EqualsLiteral("WEBGPU")) {
517 return nsIGfxInfo::FEATURE_WEBGPU;
519 if (aFeature.EqualsLiteral("VIDEO_OVERLAY")) {
520 return nsIGfxInfo::FEATURE_VIDEO_OVERLAY;
522 if (aFeature.EqualsLiteral("HW_DECODED_VIDEO_ZERO_COPY")) {
523 return nsIGfxInfo::FEATURE_HW_DECODED_VIDEO_ZERO_COPY;
525 if (aFeature.EqualsLiteral("REUSE_DECODER_DEVICE")) {
526 return nsIGfxInfo::FEATURE_REUSE_DECODER_DEVICE;
528 if (aFeature.EqualsLiteral("WEBRENDER_PARTIAL_PRESENT")) {
529 return nsIGfxInfo::FEATURE_WEBRENDER_PARTIAL_PRESENT;
531 if (aFeature.EqualsLiteral("BACKDROP_FILTER")) {
532 return nsIGfxInfo::FEATURE_BACKDROP_FILTER;
534 if (aFeature.EqualsLiteral("ACCELERATED_CANVAS2D")) {
535 return nsIGfxInfo::FEATURE_ACCELERATED_CANVAS2D;
538 // If we don't recognize the feature, it may be new, and something
539 // this version doesn't understand. So, nothing to do. This is
540 // different from feature not being specified at all, in which case
541 // this method should not get called and we should continue with the
542 // "all features" blocklisting.
543 return -1;
546 static int32_t BlocklistFeatureStatusToGfxFeatureStatus(
547 const nsAString& aStatus) {
548 if (aStatus.EqualsLiteral("STATUS_OK")) {
549 return nsIGfxInfo::FEATURE_STATUS_OK;
551 if (aStatus.EqualsLiteral("BLOCKED_DRIVER_VERSION")) {
552 return nsIGfxInfo::FEATURE_BLOCKED_DRIVER_VERSION;
554 if (aStatus.EqualsLiteral("BLOCKED_DEVICE")) {
555 return nsIGfxInfo::FEATURE_BLOCKED_DEVICE;
557 if (aStatus.EqualsLiteral("DISCOURAGED")) {
558 return nsIGfxInfo::FEATURE_DISCOURAGED;
560 if (aStatus.EqualsLiteral("BLOCKED_OS_VERSION")) {
561 return nsIGfxInfo::FEATURE_BLOCKED_OS_VERSION;
563 if (aStatus.EqualsLiteral("DENIED")) {
564 return nsIGfxInfo::FEATURE_DENIED;
566 if (aStatus.EqualsLiteral("ALLOW_QUALIFIED")) {
567 return nsIGfxInfo::FEATURE_ALLOW_QUALIFIED;
569 if (aStatus.EqualsLiteral("ALLOW_ALWAYS")) {
570 return nsIGfxInfo::FEATURE_ALLOW_ALWAYS;
573 // Do not allow it to set STATUS_UNKNOWN. Also, we are not
574 // expecting the "mismatch" status showing up here.
576 return nsIGfxInfo::FEATURE_STATUS_OK;
579 static VersionComparisonOp BlocklistComparatorToComparisonOp(
580 const nsAString& op) {
581 if (op.EqualsLiteral("LESS_THAN")) {
582 return DRIVER_LESS_THAN;
584 if (op.EqualsLiteral("BUILD_ID_LESS_THAN")) {
585 return DRIVER_BUILD_ID_LESS_THAN;
587 if (op.EqualsLiteral("LESS_THAN_OR_EQUAL")) {
588 return DRIVER_LESS_THAN_OR_EQUAL;
590 if (op.EqualsLiteral("BUILD_ID_LESS_THAN_OR_EQUAL")) {
591 return DRIVER_BUILD_ID_LESS_THAN_OR_EQUAL;
593 if (op.EqualsLiteral("GREATER_THAN")) {
594 return DRIVER_GREATER_THAN;
596 if (op.EqualsLiteral("GREATER_THAN_OR_EQUAL")) {
597 return DRIVER_GREATER_THAN_OR_EQUAL;
599 if (op.EqualsLiteral("EQUAL")) {
600 return DRIVER_EQUAL;
602 if (op.EqualsLiteral("NOT_EQUAL")) {
603 return DRIVER_NOT_EQUAL;
605 if (op.EqualsLiteral("BETWEEN_EXCLUSIVE")) {
606 return DRIVER_BETWEEN_EXCLUSIVE;
608 if (op.EqualsLiteral("BETWEEN_INCLUSIVE")) {
609 return DRIVER_BETWEEN_INCLUSIVE;
611 if (op.EqualsLiteral("BETWEEN_INCLUSIVE_START")) {
612 return DRIVER_BETWEEN_INCLUSIVE_START;
615 return DRIVER_COMPARISON_IGNORED;
619 Deserialize Blocklist entries from string.
620 e.g:
621 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
623 static bool BlocklistEntryToDriverInfo(const nsACString& aBlocklistEntry,
624 GfxDriverInfo& aDriverInfo) {
625 // If we get an application version to be zero, something is not working
626 // and we are not going to bother checking the blocklist versions.
627 // See TestGfxWidgets.cpp for how version comparison works.
628 // <versionRange minVersion="42.0a1" maxVersion="45.0"></versionRange>
629 static mozilla::Version zeroV("0");
630 static mozilla::Version appV(GfxInfoBase::GetApplicationVersion().get());
631 if (appV <= zeroV) {
632 gfxCriticalErrorOnce(gfxCriticalError::DefaultOptions(false))
633 << "Invalid application version "
634 << GfxInfoBase::GetApplicationVersion().get();
637 aDriverInfo.mRuleId = "FEATURE_FAILURE_DL_BLOCKLIST_NO_ID"_ns;
639 for (const auto& keyValue : aBlocklistEntry.Split('\t')) {
640 nsTArray<nsCString> splitted;
641 ParseString(keyValue, ':', splitted);
642 if (splitted.Length() != 2) {
643 // If we don't recognize the input data, we do not want to proceed.
644 gfxCriticalErrorOnce(CriticalLog::DefaultOptions(false))
645 << "Unrecognized data " << nsCString(keyValue).get();
646 return false;
648 const nsCString& key = splitted[0];
649 const nsCString& value = splitted[1];
650 NS_ConvertUTF8toUTF16 dataValue(value);
652 if (value.Length() == 0) {
653 // Safety check for empty values.
654 gfxCriticalErrorOnce(CriticalLog::DefaultOptions(false))
655 << "Empty value for " << key.get();
656 return false;
659 if (key.EqualsLiteral("blockID")) {
660 nsCString blockIdStr = "FEATURE_FAILURE_DL_BLOCKLIST_"_ns + value;
661 aDriverInfo.mRuleId = blockIdStr.get();
662 } else if (key.EqualsLiteral("os")) {
663 aDriverInfo.mOperatingSystem = BlocklistOSToOperatingSystem(dataValue);
664 } else if (key.EqualsLiteral("osversion")) {
665 aDriverInfo.mOperatingSystemVersion = strtoul(value.get(), nullptr, 10);
666 } else if (key.EqualsLiteral("windowProtocol")) {
667 aDriverInfo.mWindowProtocol = dataValue;
668 } else if (key.EqualsLiteral("vendor")) {
669 aDriverInfo.mAdapterVendor = dataValue;
670 } else if (key.EqualsLiteral("driverVendor")) {
671 aDriverInfo.mDriverVendor = dataValue;
672 } else if (key.EqualsLiteral("feature")) {
673 aDriverInfo.mFeature = BlocklistFeatureToGfxFeature(dataValue);
674 if (aDriverInfo.mFeature < 0) {
675 // If we don't recognize the feature, we do not want to proceed.
676 gfxWarning() << "Unrecognized feature " << value.get();
677 return false;
679 } else if (key.EqualsLiteral("featureStatus")) {
680 aDriverInfo.mFeatureStatus =
681 BlocklistFeatureStatusToGfxFeatureStatus(dataValue);
682 } else if (key.EqualsLiteral("driverVersion")) {
683 uint64_t version;
684 if (ParseDriverVersion(dataValue, &version))
685 aDriverInfo.mDriverVersion = version;
686 } else if (key.EqualsLiteral("driverVersionMax")) {
687 uint64_t version;
688 if (ParseDriverVersion(dataValue, &version))
689 aDriverInfo.mDriverVersionMax = version;
690 } else if (key.EqualsLiteral("driverVersionComparator")) {
691 aDriverInfo.mComparisonOp = BlocklistComparatorToComparisonOp(dataValue);
692 } else if (key.EqualsLiteral("model")) {
693 aDriverInfo.mModel = dataValue;
694 } else if (key.EqualsLiteral("product")) {
695 aDriverInfo.mProduct = dataValue;
696 } else if (key.EqualsLiteral("manufacturer")) {
697 aDriverInfo.mManufacturer = dataValue;
698 } else if (key.EqualsLiteral("hardware")) {
699 aDriverInfo.mHardware = dataValue;
700 } else if (key.EqualsLiteral("versionRange")) {
701 nsTArray<nsCString> versionRange;
702 ParseString(value, ',', versionRange);
703 if (versionRange.Length() != 2) {
704 gfxCriticalErrorOnce(CriticalLog::DefaultOptions(false))
705 << "Unrecognized versionRange " << value.get();
706 return false;
708 const nsCString& minValue = versionRange[0];
709 const nsCString& maxValue = versionRange[1];
711 mozilla::Version minV(minValue.get());
712 mozilla::Version maxV(maxValue.get());
714 if (minV > zeroV && !(appV >= minV)) {
715 // The version of the application is less than the minimal version
716 // this blocklist entry applies to, so we can just ignore it by
717 // returning false and letting the caller deal with it.
718 return false;
720 if (maxV > zeroV && !(appV <= maxV)) {
721 // The version of the application is more than the maximal version
722 // this blocklist entry applies to, so we can just ignore it by
723 // returning false and letting the caller deal with it.
724 return false;
726 } else if (key.EqualsLiteral("devices")) {
727 nsTArray<nsCString> devices;
728 ParseString(value, ',', devices);
729 GfxDeviceFamily* deviceIds = BlocklistDevicesToDeviceFamily(devices);
730 if (deviceIds) {
731 // Get GfxDriverInfo to adopt the devices array we created.
732 aDriverInfo.mDeleteDevices = true;
733 aDriverInfo.mDevices = deviceIds;
736 // We explicitly ignore unknown elements.
739 return true;
742 NS_IMETHODIMP
743 GfxInfoBase::Observe(nsISupports* aSubject, const char* aTopic,
744 const char16_t* aData) {
745 if (strcmp(aTopic, "blocklist-data-gfxItems") == 0) {
746 nsTArray<GfxDriverInfo> driverInfo;
747 NS_ConvertUTF16toUTF8 utf8Data(aData);
749 for (const auto& blocklistEntry : utf8Data.Split('\n')) {
750 GfxDriverInfo di;
751 if (BlocklistEntryToDriverInfo(blocklistEntry, di)) {
752 // XXX Changing this to driverInfo.AppendElement(di) causes leaks.
753 // Probably some non-standard semantics of the copy/move operations?
754 *driverInfo.AppendElement() = di;
755 // Prevent di falling out of scope from destroying the devices.
756 di.mDeleteDevices = false;
757 } else {
758 driverInfo.AppendElement();
762 EvaluateDownloadedBlocklist(driverInfo);
765 return NS_OK;
768 GfxInfoBase::GfxInfoBase() : mScreenPixels(INT64_MAX), mMutex("GfxInfoBase") {}
770 GfxInfoBase::~GfxInfoBase() = default;
772 nsresult GfxInfoBase::Init() {
773 InitGfxDriverInfoShutdownObserver();
775 nsCOMPtr<nsIObserverService> os = mozilla::services::GetObserverService();
776 if (os) {
777 os->AddObserver(this, "blocklist-data-gfxItems", true);
780 return NS_OK;
783 void GfxInfoBase::GetData() {
784 if (mScreenPixels != INT64_MAX) {
785 // Already initialized.
786 return;
789 ScreenManager::GetSingleton().GetTotalScreenPixels(&mScreenPixels);
792 NS_IMETHODIMP
793 GfxInfoBase::GetFeatureStatus(int32_t aFeature, nsACString& aFailureId,
794 int32_t* aStatus) {
795 // Ignore the gfx.blocklist.all pref on release and beta.
796 #if defined(RELEASE_OR_BETA)
797 int32_t blocklistAll = 0;
798 #else
799 int32_t blocklistAll = StaticPrefs::gfx_blocklist_all_AtStartup();
800 #endif
801 if (blocklistAll > 0) {
802 gfxCriticalErrorOnce(gfxCriticalError::DefaultOptions(false))
803 << "Forcing blocklisting all features";
804 *aStatus = FEATURE_BLOCKED_DEVICE;
805 aFailureId = "FEATURE_FAILURE_BLOCK_ALL";
806 return NS_OK;
809 if (blocklistAll < 0) {
810 gfxCriticalErrorOnce(gfxCriticalError::DefaultOptions(false))
811 << "Ignoring any feature blocklisting.";
812 *aStatus = FEATURE_STATUS_OK;
813 return NS_OK;
816 // This is how we evaluate the downloadable blocklist. If there is no pref,
817 // then we will fallback to checking the static blocklist.
818 if (GetPrefValueForFeature(aFeature, *aStatus, aFailureId)) {
819 return NS_OK;
822 if (XRE_IsContentProcess() || XRE_IsGPUProcess()) {
823 // Use the cached data received from the parent process.
824 MOZ_ASSERT(sFeatureStatus);
825 bool success = false;
826 for (const auto& fs : *sFeatureStatus) {
827 if (fs.feature() == aFeature) {
828 aFailureId = fs.failureId();
829 *aStatus = fs.status();
830 success = true;
831 break;
834 return success ? NS_OK : NS_ERROR_FAILURE;
837 nsString version;
838 nsTArray<GfxDriverInfo> driverInfo;
839 nsresult rv =
840 GetFeatureStatusImpl(aFeature, aStatus, version, driverInfo, aFailureId);
841 return rv;
844 nsTArray<gfx::GfxInfoFeatureStatus> GfxInfoBase::GetAllFeatures() {
845 MOZ_RELEASE_ASSERT(XRE_IsParentProcess());
846 if (!sFeatureStatus) {
847 InitFeatureStatus(new nsTArray<gfx::GfxInfoFeatureStatus>());
848 for (int32_t i = 1; i <= nsIGfxInfo::FEATURE_MAX_VALUE; ++i) {
849 int32_t status = 0;
850 nsAutoCString failureId;
851 GetFeatureStatus(i, failureId, &status);
852 gfx::GfxInfoFeatureStatus gfxFeatureStatus;
853 gfxFeatureStatus.feature() = i;
854 gfxFeatureStatus.status() = status;
855 gfxFeatureStatus.failureId() = failureId;
856 sFeatureStatus->AppendElement(gfxFeatureStatus);
860 nsTArray<gfx::GfxInfoFeatureStatus> features;
861 for (const auto& status : *sFeatureStatus) {
862 gfx::GfxInfoFeatureStatus copy = status;
863 features.AppendElement(copy);
865 return features;
868 inline bool MatchingAllowStatus(int32_t aStatus) {
869 switch (aStatus) {
870 case nsIGfxInfo::FEATURE_ALLOW_ALWAYS:
871 case nsIGfxInfo::FEATURE_ALLOW_QUALIFIED:
872 return true;
873 default:
874 return false;
878 // Matching OS go somewhat beyond the simple equality check because of the
879 // "All Windows" and "All OS X" variations.
881 // aBlockedOS is describing the system(s) we are trying to block.
882 // aSystemOS is describing the system we are running on.
884 // aSystemOS should not be "Windows" or "OSX" - it should be set to
885 // a particular version instead.
886 // However, it is valid for aBlockedOS to be one of those generic values,
887 // as we could be blocking all of the versions.
888 inline bool MatchingOperatingSystems(OperatingSystem aBlockedOS,
889 OperatingSystem aSystemOS,
890 uint32_t aSystemOSBuild) {
891 MOZ_ASSERT(aSystemOS != OperatingSystem::Windows &&
892 aSystemOS != OperatingSystem::OSX);
894 // If the block entry OS is unknown, it doesn't match
895 if (aBlockedOS == OperatingSystem::Unknown) {
896 return false;
899 #if defined(XP_WIN)
900 if (aBlockedOS == OperatingSystem::Windows) {
901 // We do want even "unknown" aSystemOS to fall under "all windows"
902 return true;
905 constexpr uint32_t kMinWin10BuildNumber = 18362;
906 if (aBlockedOS == OperatingSystem::RecentWindows10 &&
907 aSystemOS == OperatingSystem::Windows10) {
908 // For allowlist purposes, we sometimes want to restrict to only recent
909 // versions of Windows 10. This is a bit of a kludge but easier than adding
910 // complicated blocklist infrastructure for build ID comparisons like driver
911 // versions.
912 return aSystemOSBuild >= kMinWin10BuildNumber;
915 if (aBlockedOS == OperatingSystem::NotRecentWindows10) {
916 if (aSystemOS == OperatingSystem::Windows10) {
917 return aSystemOSBuild < kMinWin10BuildNumber;
918 } else {
919 return true;
922 #endif
924 #if defined(XP_MACOSX)
925 if (aBlockedOS == OperatingSystem::OSX) {
926 // We do want even "unknown" aSystemOS to fall under "all OS X"
927 return true;
929 #endif
931 return aSystemOS == aBlockedOS;
934 inline bool MatchingBattery(BatteryStatus aBatteryStatus, bool aHasBattery) {
935 switch (aBatteryStatus) {
936 case BatteryStatus::All:
937 return true;
938 case BatteryStatus::None:
939 return !aHasBattery;
940 case BatteryStatus::Present:
941 return aHasBattery;
944 MOZ_ASSERT_UNREACHABLE("bad battery status");
945 return false;
948 inline bool MatchingScreenSize(ScreenSizeStatus aScreenStatus,
949 int64_t aScreenPixels) {
950 constexpr int64_t kMaxSmallPixels = 2304000; // 1920x1200
951 constexpr int64_t kMaxMediumPixels = 4953600; // 3440x1440
953 switch (aScreenStatus) {
954 case ScreenSizeStatus::All:
955 return true;
956 case ScreenSizeStatus::Small:
957 return aScreenPixels <= kMaxSmallPixels;
958 case ScreenSizeStatus::SmallAndMedium:
959 return aScreenPixels <= kMaxMediumPixels;
960 case ScreenSizeStatus::Medium:
961 return aScreenPixels > kMaxSmallPixels &&
962 aScreenPixels <= kMaxMediumPixels;
963 case ScreenSizeStatus::MediumAndLarge:
964 return aScreenPixels > kMaxSmallPixels;
965 case ScreenSizeStatus::Large:
966 return aScreenPixels > kMaxMediumPixels;
969 MOZ_ASSERT_UNREACHABLE("bad screen status");
970 return false;
973 int32_t GfxInfoBase::FindBlocklistedDeviceInList(
974 const nsTArray<GfxDriverInfo>& info, nsAString& aSuggestedVersion,
975 int32_t aFeature, nsACString& aFailureId, OperatingSystem os,
976 bool aForAllowing) {
977 int32_t status = nsIGfxInfo::FEATURE_STATUS_UNKNOWN;
979 // Some properties are not available on all platforms.
980 nsAutoString windowProtocol;
981 nsresult rv = GetWindowProtocol(windowProtocol);
982 if (NS_FAILED(rv) && rv != NS_ERROR_NOT_IMPLEMENTED) {
983 return 0;
986 bool hasBattery = false;
987 rv = GetHasBattery(&hasBattery);
988 if (NS_FAILED(rv) && rv != NS_ERROR_NOT_IMPLEMENTED) {
989 return 0;
992 uint32_t osBuild = OperatingSystemBuild();
994 // Get the adapters once then reuse below
995 nsAutoString adapterVendorID[2];
996 nsAutoString adapterDeviceID[2];
997 nsAutoString adapterDriverVendor[2];
998 nsAutoString adapterDriverVersionString[2];
999 bool adapterInfoFailed[2];
1001 adapterInfoFailed[0] =
1002 (NS_FAILED(GetAdapterVendorID(adapterVendorID[0])) ||
1003 NS_FAILED(GetAdapterDeviceID(adapterDeviceID[0])) ||
1004 NS_FAILED(GetAdapterDriverVendor(adapterDriverVendor[0])) ||
1005 NS_FAILED(GetAdapterDriverVersion(adapterDriverVersionString[0])));
1006 adapterInfoFailed[1] =
1007 (NS_FAILED(GetAdapterVendorID2(adapterVendorID[1])) ||
1008 NS_FAILED(GetAdapterDeviceID2(adapterDeviceID[1])) ||
1009 NS_FAILED(GetAdapterDriverVendor2(adapterDriverVendor[1])) ||
1010 NS_FAILED(GetAdapterDriverVersion2(adapterDriverVersionString[1])));
1011 // No point in going on if we don't have adapter info
1012 if (adapterInfoFailed[0] && adapterInfoFailed[1]) {
1013 return 0;
1016 #if defined(XP_WIN) || defined(ANDROID) || defined(MOZ_WIDGET_GTK)
1017 uint64_t driverVersion[2] = {0, 0};
1018 if (!adapterInfoFailed[0]) {
1019 ParseDriverVersion(adapterDriverVersionString[0], &driverVersion[0]);
1021 if (!adapterInfoFailed[1]) {
1022 ParseDriverVersion(adapterDriverVersionString[1], &driverVersion[1]);
1024 #endif
1026 uint32_t i = 0;
1027 for (; i < info.Length(); i++) {
1028 // If the status is FEATURE_ALLOW_*, then it is for the allowlist, not
1029 // blocklisting. Only consider entries for our search mode.
1030 if (MatchingAllowStatus(info[i].mFeatureStatus) != aForAllowing) {
1031 continue;
1034 // If we don't have the info for this GPU, no need to check further.
1035 // It is unclear that we would ever have a mixture of 1st and 2nd
1036 // GPU, but leaving the code in for that possibility for now.
1037 // (Actually, currently mGpu2 will never be true, so this can
1038 // be optimized out.)
1039 uint32_t infoIndex = info[i].mGpu2 ? 1 : 0;
1040 if (adapterInfoFailed[infoIndex]) {
1041 continue;
1044 // Do the operating system check first, no point in getting the driver
1045 // info if we won't need to use it.
1046 if (!MatchingOperatingSystems(info[i].mOperatingSystem, os, osBuild)) {
1047 continue;
1050 if (info[i].mOperatingSystemVersion &&
1051 info[i].mOperatingSystemVersion != OperatingSystemVersion()) {
1052 continue;
1055 if (!MatchingBattery(info[i].mBattery, hasBattery)) {
1056 continue;
1059 if (!MatchingScreenSize(info[i].mScreen, mScreenPixels)) {
1060 continue;
1063 if (!DoesWindowProtocolMatch(info[i].mWindowProtocol, windowProtocol)) {
1064 continue;
1067 if (!DoesVendorMatch(info[i].mAdapterVendor, adapterVendorID[infoIndex])) {
1068 continue;
1071 if (!DoesDriverVendorMatch(info[i].mDriverVendor,
1072 adapterDriverVendor[infoIndex])) {
1073 continue;
1076 if (info[i].mDevices && !info[i].mDevices->IsEmpty()) {
1077 nsresult rv = info[i].mDevices->Contains(adapterDeviceID[infoIndex]);
1078 if (rv == NS_ERROR_NOT_AVAILABLE) {
1079 // Not found
1080 continue;
1082 if (rv != NS_OK) {
1083 // Failed to search, allowlist should not match, blocklist should match
1084 // for safety reasons
1085 if (aForAllowing) {
1086 continue;
1088 break;
1092 bool match = false;
1094 if (!info[i].mHardware.IsEmpty() && !info[i].mHardware.Equals(Hardware())) {
1095 continue;
1097 if (!info[i].mModel.IsEmpty() && !info[i].mModel.Equals(Model())) {
1098 continue;
1100 if (!info[i].mProduct.IsEmpty() && !info[i].mProduct.Equals(Product())) {
1101 continue;
1103 if (!info[i].mManufacturer.IsEmpty() &&
1104 !info[i].mManufacturer.Equals(Manufacturer())) {
1105 continue;
1108 #if defined(XP_WIN) || defined(ANDROID) || defined(MOZ_WIDGET_GTK)
1109 switch (info[i].mComparisonOp) {
1110 case DRIVER_LESS_THAN:
1111 match = driverVersion[infoIndex] < info[i].mDriverVersion;
1112 break;
1113 case DRIVER_BUILD_ID_LESS_THAN:
1114 match = (driverVersion[infoIndex] & 0xFFFF) < info[i].mDriverVersion;
1115 break;
1116 case DRIVER_LESS_THAN_OR_EQUAL:
1117 match = driverVersion[infoIndex] <= info[i].mDriverVersion;
1118 break;
1119 case DRIVER_BUILD_ID_LESS_THAN_OR_EQUAL:
1120 match = (driverVersion[infoIndex] & 0xFFFF) <= info[i].mDriverVersion;
1121 break;
1122 case DRIVER_GREATER_THAN:
1123 match = driverVersion[infoIndex] > info[i].mDriverVersion;
1124 break;
1125 case DRIVER_GREATER_THAN_OR_EQUAL:
1126 match = driverVersion[infoIndex] >= info[i].mDriverVersion;
1127 break;
1128 case DRIVER_EQUAL:
1129 match = driverVersion[infoIndex] == info[i].mDriverVersion;
1130 break;
1131 case DRIVER_NOT_EQUAL:
1132 match = driverVersion[infoIndex] != info[i].mDriverVersion;
1133 break;
1134 case DRIVER_BETWEEN_EXCLUSIVE:
1135 match = driverVersion[infoIndex] > info[i].mDriverVersion &&
1136 driverVersion[infoIndex] < info[i].mDriverVersionMax;
1137 break;
1138 case DRIVER_BETWEEN_INCLUSIVE:
1139 match = driverVersion[infoIndex] >= info[i].mDriverVersion &&
1140 driverVersion[infoIndex] <= info[i].mDriverVersionMax;
1141 break;
1142 case DRIVER_BETWEEN_INCLUSIVE_START:
1143 match = driverVersion[infoIndex] >= info[i].mDriverVersion &&
1144 driverVersion[infoIndex] < info[i].mDriverVersionMax;
1145 break;
1146 case DRIVER_COMPARISON_IGNORED:
1147 // We don't have a comparison op, so we match everything.
1148 match = true;
1149 break;
1150 default:
1151 NS_WARNING("Bogus op in GfxDriverInfo");
1152 break;
1154 #else
1155 // We don't care what driver version it was. We only check OS version and if
1156 // the device matches.
1157 match = true;
1158 #endif
1160 if (match || info[i].mDriverVersion == GfxDriverInfo::allDriverVersions) {
1161 if (info[i].mFeature == GfxDriverInfo::allFeatures ||
1162 info[i].mFeature == aFeature) {
1163 status = info[i].mFeatureStatus;
1164 if (!info[i].mRuleId.IsEmpty()) {
1165 aFailureId = info[i].mRuleId.get();
1166 } else {
1167 aFailureId = "FEATURE_FAILURE_DL_BLOCKLIST_NO_ID";
1169 break;
1174 #if defined(XP_WIN)
1175 // As a very special case, we block D2D on machines with an NVidia 310M GPU
1176 // as either the primary or secondary adapter. D2D is also blocked when the
1177 // NV 310M is the primary adapter (using the standard blocklisting mechanism).
1178 // If the primary GPU already matched something in the blocklist then we
1179 // ignore this special rule. See bug 1008759.
1180 if (status == nsIGfxInfo::FEATURE_STATUS_UNKNOWN &&
1181 (aFeature == nsIGfxInfo::FEATURE_DIRECT2D)) {
1182 if (!adapterInfoFailed[1]) {
1183 nsAString& nvVendorID =
1184 (nsAString&)GfxDriverInfo::GetDeviceVendor(DeviceVendor::NVIDIA);
1185 const nsString nv310mDeviceId = u"0x0A70"_ns;
1186 if (nvVendorID.Equals(adapterVendorID[1],
1187 nsCaseInsensitiveStringComparator) &&
1188 nv310mDeviceId.Equals(adapterDeviceID[1],
1189 nsCaseInsensitiveStringComparator)) {
1190 status = nsIGfxInfo::FEATURE_BLOCKED_DEVICE;
1191 aFailureId = "FEATURE_FAILURE_D2D_NV310M_BLOCK";
1196 // Depends on Windows driver versioning. We don't pass a GfxDriverInfo object
1197 // back to the Windows handler, so we must handle this here.
1198 if (status == FEATURE_BLOCKED_DRIVER_VERSION) {
1199 if (info[i].mSuggestedVersion) {
1200 aSuggestedVersion.AppendPrintf("%s", info[i].mSuggestedVersion);
1201 } else if (info[i].mComparisonOp == DRIVER_LESS_THAN &&
1202 info[i].mDriverVersion != GfxDriverInfo::allDriverVersions) {
1203 aSuggestedVersion.AppendPrintf(
1204 "%lld.%lld.%lld.%lld",
1205 (info[i].mDriverVersion & 0xffff000000000000) >> 48,
1206 (info[i].mDriverVersion & 0x0000ffff00000000) >> 32,
1207 (info[i].mDriverVersion & 0x00000000ffff0000) >> 16,
1208 (info[i].mDriverVersion & 0x000000000000ffff));
1211 #endif
1213 return status;
1216 void GfxInfoBase::SetFeatureStatus(nsTArray<gfx::GfxInfoFeatureStatus>&& aFS) {
1217 MOZ_ASSERT(!sFeatureStatus);
1218 InitFeatureStatus(new nsTArray<gfx::GfxInfoFeatureStatus>(std::move(aFS)));
1221 bool GfxInfoBase::DoesWindowProtocolMatch(
1222 const nsAString& aBlocklistWindowProtocol,
1223 const nsAString& aWindowProtocol) {
1224 return aBlocklistWindowProtocol.Equals(aWindowProtocol,
1225 nsCaseInsensitiveStringComparator) ||
1226 aBlocklistWindowProtocol.Equals(
1227 GfxDriverInfo::GetWindowProtocol(WindowProtocol::All),
1228 nsCaseInsensitiveStringComparator);
1231 bool GfxInfoBase::DoesVendorMatch(const nsAString& aBlocklistVendor,
1232 const nsAString& aAdapterVendor) {
1233 return aBlocklistVendor.Equals(aAdapterVendor,
1234 nsCaseInsensitiveStringComparator) ||
1235 aBlocklistVendor.Equals(
1236 GfxDriverInfo::GetDeviceVendor(DeviceVendor::All),
1237 nsCaseInsensitiveStringComparator);
1240 bool GfxInfoBase::DoesDriverVendorMatch(const nsAString& aBlocklistVendor,
1241 const nsAString& aDriverVendor) {
1242 return aBlocklistVendor.Equals(aDriverVendor,
1243 nsCaseInsensitiveStringComparator) ||
1244 aBlocklistVendor.Equals(
1245 GfxDriverInfo::GetDriverVendor(DriverVendor::All),
1246 nsCaseInsensitiveStringComparator);
1249 bool GfxInfoBase::IsFeatureAllowlisted(int32_t aFeature) const {
1250 return aFeature == nsIGfxInfo::FEATURE_VIDEO_OVERLAY ||
1251 aFeature == nsIGfxInfo::FEATURE_HW_DECODED_VIDEO_ZERO_COPY;
1254 nsresult GfxInfoBase::GetFeatureStatusImpl(
1255 int32_t aFeature, int32_t* aStatus, nsAString& aSuggestedVersion,
1256 const nsTArray<GfxDriverInfo>& aDriverInfo, nsACString& aFailureId,
1257 OperatingSystem* aOS /* = nullptr */) {
1258 if (aFeature <= 0) {
1259 gfxWarning() << "Invalid feature <= 0";
1260 return NS_OK;
1263 if (*aStatus != nsIGfxInfo::FEATURE_STATUS_UNKNOWN) {
1264 // Terminate now with the status determined by the derived type (OS-specific
1265 // code).
1266 return NS_OK;
1269 if (sShutdownOccurred) {
1270 // This is futile; we've already commenced shutdown and our blocklists have
1271 // been deleted. We may want to look into resurrecting the blocklist instead
1272 // but for now, just don't even go there.
1273 return NS_OK;
1276 // Ensure any additional initialization required is complete.
1277 GetData();
1279 // If an operating system was provided by the derived GetFeatureStatusImpl,
1280 // grab it here. Otherwise, the OS is unknown.
1281 OperatingSystem os = (aOS ? *aOS : OperatingSystem::Unknown);
1283 nsAutoString adapterVendorID;
1284 nsAutoString adapterDeviceID;
1285 nsAutoString adapterDriverVersionString;
1286 if (NS_FAILED(GetAdapterVendorID(adapterVendorID)) ||
1287 NS_FAILED(GetAdapterDeviceID(adapterDeviceID)) ||
1288 NS_FAILED(GetAdapterDriverVersion(adapterDriverVersionString))) {
1289 aFailureId = "FEATURE_FAILURE_CANT_RESOLVE_ADAPTER";
1290 *aStatus = FEATURE_BLOCKED_DEVICE;
1291 return NS_OK;
1294 // We only check either the given blocklist, or the static list, as given.
1295 int32_t status;
1296 if (aDriverInfo.Length()) {
1297 status =
1298 FindBlocklistedDeviceInList(aDriverInfo, aSuggestedVersion, aFeature,
1299 aFailureId, os, /* aForAllowing */ false);
1300 } else {
1301 if (!sDriverInfo) {
1302 sDriverInfo = new nsTArray<GfxDriverInfo>();
1304 status = FindBlocklistedDeviceInList(GetGfxDriverInfo(), aSuggestedVersion,
1305 aFeature, aFailureId, os,
1306 /* aForAllowing */ false);
1309 if (status == nsIGfxInfo::FEATURE_STATUS_UNKNOWN) {
1310 if (IsFeatureAllowlisted(aFeature)) {
1311 // This feature is actually using the allowlist; that means after we pass
1312 // the blocklist to prevent us explicitly from getting the feature, we now
1313 // need to check the allowlist to ensure we are allowed to get it in the
1314 // first place.
1315 if (aDriverInfo.Length()) {
1316 status = FindBlocklistedDeviceInList(aDriverInfo, aSuggestedVersion,
1317 aFeature, aFailureId, os,
1318 /* aForAllowing */ true);
1319 } else {
1320 status = FindBlocklistedDeviceInList(
1321 GetGfxDriverInfo(), aSuggestedVersion, aFeature, aFailureId, os,
1322 /* aForAllowing */ true);
1325 if (status == nsIGfxInfo::FEATURE_STATUS_UNKNOWN) {
1326 status = nsIGfxInfo::FEATURE_DENIED;
1328 } else {
1329 // It's now done being processed. It's safe to set the status to
1330 // STATUS_OK.
1331 status = nsIGfxInfo::FEATURE_STATUS_OK;
1335 *aStatus = status;
1336 return NS_OK;
1339 NS_IMETHODIMP
1340 GfxInfoBase::GetFeatureSuggestedDriverVersion(int32_t aFeature,
1341 nsAString& aVersion) {
1342 nsCString version;
1343 if (GetPrefValueForDriverVersion(version)) {
1344 aVersion = NS_ConvertASCIItoUTF16(version);
1345 return NS_OK;
1348 int32_t status;
1349 nsCString discardFailureId;
1350 nsTArray<GfxDriverInfo> driverInfo;
1351 return GetFeatureStatusImpl(aFeature, &status, aVersion, driverInfo,
1352 discardFailureId);
1355 void GfxInfoBase::EvaluateDownloadedBlocklist(
1356 nsTArray<GfxDriverInfo>& aDriverInfo) {
1357 // If the list is empty, then we don't actually want to call
1358 // GetFeatureStatusImpl since we will use the static list instead. In that
1359 // case, all we want to do is make sure the pref is removed.
1360 if (aDriverInfo.IsEmpty()) {
1361 gfxCriticalNoteOnce << "Evaluate empty downloaded blocklist";
1362 return;
1365 OperatingSystem os = GetOperatingSystem();
1367 // For every feature we know about, we evaluate whether this blocklist has a
1368 // non-STATUS_OK status. If it does, we set the pref we evaluate in
1369 // GetFeatureStatus above, so we don't need to hold on to this blocklist
1370 // anywhere permanent.
1371 for (int feature = 1; feature <= nsIGfxInfo::FEATURE_MAX_VALUE; ++feature) {
1372 int32_t status = nsIGfxInfo::FEATURE_STATUS_UNKNOWN;
1373 nsCString failureId;
1374 nsAutoString suggestedVersion;
1376 // Note that we are careful to call the base class method since we only want
1377 // to evaluate the downloadable blocklist for these prefs.
1378 MOZ_ALWAYS_TRUE(NS_SUCCEEDED(GfxInfoBase::GetFeatureStatusImpl(
1379 feature, &status, suggestedVersion, aDriverInfo, failureId, &os)));
1381 switch (status) {
1382 default:
1383 MOZ_FALLTHROUGH_ASSERT("Unhandled feature status!");
1384 case nsIGfxInfo::FEATURE_STATUS_UNKNOWN:
1385 // This may be returned during shutdown or for invalid features.
1386 case nsIGfxInfo::FEATURE_ALLOW_ALWAYS:
1387 case nsIGfxInfo::FEATURE_ALLOW_QUALIFIED:
1388 case nsIGfxInfo::FEATURE_DENIED:
1389 // We cannot use the downloadable blocklist to control the allowlist.
1390 // If a feature is allowlisted, then we should also ignore DENIED
1391 // statuses from GetFeatureStatusImpl because we don't check the
1392 // static list when and this is an expected value. If we wish to
1393 // override the allowlist, it is as simple as creating a normal
1394 // blocklist rule with a BLOCKED* status code.
1395 case nsIGfxInfo::FEATURE_STATUS_OK:
1396 RemovePrefForFeature(feature);
1397 break;
1399 case nsIGfxInfo::FEATURE_BLOCKED_DRIVER_VERSION:
1400 if (!suggestedVersion.IsEmpty()) {
1401 SetPrefValueForDriverVersion(suggestedVersion);
1402 } else {
1403 RemovePrefForDriverVersion();
1405 [[fallthrough]];
1407 case nsIGfxInfo::FEATURE_BLOCKED_MISMATCHED_VERSION:
1408 case nsIGfxInfo::FEATURE_BLOCKED_DEVICE:
1409 case nsIGfxInfo::FEATURE_DISCOURAGED:
1410 case nsIGfxInfo::FEATURE_BLOCKED_OS_VERSION:
1411 case nsIGfxInfo::FEATURE_BLOCKED_PLATFORM_TEST:
1412 SetPrefValueForFeature(feature, status, failureId);
1413 break;
1418 NS_IMETHODIMP_(void)
1419 GfxInfoBase::LogFailure(const nsACString& failure) {
1420 // gfxCriticalError has a mutex lock of its own, so we may not actually
1421 // need this lock. ::GetFailures() accesses the data but the LogForwarder
1422 // will not return the copy of the logs unless it can get the same lock
1423 // that gfxCriticalError uses. Still, that is so much of an implementation
1424 // detail that it's nicer to just add an extra lock here and in
1425 // ::GetFailures()
1426 MutexAutoLock lock(mMutex);
1428 // By default, gfxCriticalError asserts; make it not assert in this case.
1429 gfxCriticalError(CriticalLog::DefaultOptions(false))
1430 << "(LF) " << failure.BeginReading();
1433 NS_IMETHODIMP GfxInfoBase::GetFailures(nsTArray<int32_t>& indices,
1434 nsTArray<nsCString>& failures) {
1435 MutexAutoLock lock(mMutex);
1437 LogForwarder* logForwarder = Factory::GetLogForwarder();
1438 if (!logForwarder) {
1439 return NS_ERROR_UNEXPECTED;
1442 // There are two string copies in this method, starting with this one. We are
1443 // assuming this is not a big deal, as the size of the array should be small
1444 // and the strings in it should be small as well (the error messages in the
1445 // code.) The second copy happens with the AppendElement() calls.
1446 // Technically, we don't need the mutex lock after the StringVectorCopy()
1447 // call.
1448 LoggingRecord loggedStrings = logForwarder->LoggingRecordCopy();
1449 LoggingRecord::const_iterator it;
1450 for (it = loggedStrings.begin(); it != loggedStrings.end(); ++it) {
1451 failures.AppendElement(
1452 nsDependentCSubstring(Get<1>(*it).c_str(), Get<1>(*it).size()));
1453 indices.AppendElement(Get<0>(*it));
1456 return NS_OK;
1459 nsTArray<GfxInfoCollectorBase*>* sCollectors;
1461 static void InitCollectors() {
1462 if (!sCollectors) sCollectors = new nsTArray<GfxInfoCollectorBase*>;
1465 nsresult GfxInfoBase::GetInfo(JSContext* aCx,
1466 JS::MutableHandle<JS::Value> aResult) {
1467 InitCollectors();
1468 InfoObject obj(aCx);
1470 for (uint32_t i = 0; i < sCollectors->Length(); i++) {
1471 (*sCollectors)[i]->GetInfo(obj);
1474 // Some example property definitions
1475 // obj.DefineProperty("wordCacheSize", gfxTextRunWordCache::Count());
1476 // obj.DefineProperty("renderer", mRendererIDsString);
1477 // obj.DefineProperty("five", 5);
1479 if (!obj.mOk) {
1480 return NS_ERROR_FAILURE;
1483 aResult.setObject(*obj.mObj);
1484 return NS_OK;
1487 nsAutoCString gBaseAppVersion;
1489 const nsCString& GfxInfoBase::GetApplicationVersion() {
1490 static bool versionInitialized = false;
1491 if (!versionInitialized) {
1492 // If we fail to get the version, we will not try again.
1493 versionInitialized = true;
1495 // Get the version from xpcom/system/nsIXULAppInfo.idl
1496 nsCOMPtr<nsIXULAppInfo> app = do_GetService("@mozilla.org/xre/app-info;1");
1497 if (app) {
1498 app->GetVersion(gBaseAppVersion);
1501 return gBaseAppVersion;
1504 void GfxInfoBase::AddCollector(GfxInfoCollectorBase* collector) {
1505 InitCollectors();
1506 sCollectors->AppendElement(collector);
1509 void GfxInfoBase::RemoveCollector(GfxInfoCollectorBase* collector) {
1510 InitCollectors();
1511 for (uint32_t i = 0; i < sCollectors->Length(); i++) {
1512 if ((*sCollectors)[i] == collector) {
1513 sCollectors->RemoveElementAt(i);
1514 break;
1517 if (sCollectors->IsEmpty()) {
1518 delete sCollectors;
1519 sCollectors = nullptr;
1523 static void AppendMonitor(JSContext* aCx, widget::Screen& aScreen,
1524 JS::Handle<JSObject*> aOutArray, int32_t aIndex) {
1525 JS::Rooted<JSObject*> obj(aCx, JS_NewPlainObject(aCx));
1527 auto screenSize = aScreen.GetRect().Size();
1529 JS::Rooted<JS::Value> screenWidth(aCx, JS::Int32Value(screenSize.width));
1530 JS_SetProperty(aCx, obj, "screenWidth", screenWidth);
1532 JS::Rooted<JS::Value> screenHeight(aCx, JS::Int32Value(screenSize.height));
1533 JS_SetProperty(aCx, obj, "screenHeight", screenHeight);
1535 // XXX Just preserving behavior since this is exposed to telemetry, but we
1536 // could consider including this everywhere.
1537 #ifdef XP_MACOSX
1538 JS::Rooted<JS::Value> scale(
1539 aCx, JS::NumberValue(aScreen.GetContentsScaleFactor()));
1540 JS_SetProperty(aCx, obj, "scale", scale);
1541 #endif
1543 #ifdef XP_WIN
1544 JS::Rooted<JS::Value> refreshRate(aCx,
1545 JS::Int32Value(aScreen.GetRefreshRate()));
1546 JS_SetProperty(aCx, obj, "refreshRate", refreshRate);
1548 JS::Rooted<JS::Value> pseudoDisplay(
1549 aCx, JS::BooleanValue(aScreen.GetIsPseudoDisplay()));
1550 JS_SetProperty(aCx, obj, "pseudoDisplay", pseudoDisplay);
1551 #endif
1553 JS::Rooted<JS::Value> element(aCx, JS::ObjectValue(*obj));
1554 JS_SetElement(aCx, aOutArray, aIndex, element);
1557 nsresult GfxInfoBase::FindMonitors(JSContext* aCx,
1558 JS::Handle<JSObject*> aOutArray) {
1559 int32_t index = 0;
1560 auto& sm = ScreenManager::GetSingleton();
1561 for (auto& screen : sm.CurrentScreenList()) {
1562 AppendMonitor(aCx, *screen, aOutArray, index++);
1565 if (index == 0) {
1566 // Ensure we return at least one monitor, this is needed for xpcshell.
1567 RefPtr<Screen> screen = sm.GetPrimaryScreen();
1568 AppendMonitor(aCx, *screen, aOutArray, index++);
1571 return NS_OK;
1574 NS_IMETHODIMP
1575 GfxInfoBase::GetMonitors(JSContext* aCx, JS::MutableHandle<JS::Value> aResult) {
1576 JS::Rooted<JSObject*> array(aCx, JS::NewArrayObject(aCx, 0));
1578 nsresult rv = FindMonitors(aCx, array);
1579 if (NS_FAILED(rv)) {
1580 return rv;
1583 aResult.setObject(*array);
1584 return NS_OK;
1587 static inline bool SetJSPropertyString(JSContext* aCx,
1588 JS::Handle<JSObject*> aObj,
1589 const char* aProp, const char* aString) {
1590 JS::Rooted<JSString*> str(aCx, JS_NewStringCopyZ(aCx, aString));
1591 if (!str) {
1592 return false;
1595 JS::Rooted<JS::Value> val(aCx, JS::StringValue(str));
1596 return JS_SetProperty(aCx, aObj, aProp, val);
1599 template <typename T>
1600 static inline bool AppendJSElement(JSContext* aCx, JS::Handle<JSObject*> aObj,
1601 const T& aValue) {
1602 uint32_t index;
1603 if (!JS::GetArrayLength(aCx, aObj, &index)) {
1604 return false;
1606 return JS_SetElement(aCx, aObj, index, aValue);
1609 nsresult GfxInfoBase::GetFeatures(JSContext* aCx,
1610 JS::MutableHandle<JS::Value> aOut) {
1611 JS::Rooted<JSObject*> obj(aCx, JS_NewPlainObject(aCx));
1612 if (!obj) {
1613 return NS_ERROR_OUT_OF_MEMORY;
1615 aOut.setObject(*obj);
1617 layers::LayersBackend backend =
1618 gfxPlatform::Initialized()
1619 ? gfxPlatform::GetPlatform()->GetCompositorBackend()
1620 : layers::LayersBackend::LAYERS_NONE;
1621 const char* backendName = layers::GetLayersBackendName(backend);
1622 SetJSPropertyString(aCx, obj, "compositor", backendName);
1624 // If graphics isn't initialized yet, just stop now.
1625 if (!gfxPlatform::Initialized()) {
1626 return NS_OK;
1629 DescribeFeatures(aCx, obj);
1630 return NS_OK;
1633 nsresult GfxInfoBase::GetFeatureLog(JSContext* aCx,
1634 JS::MutableHandle<JS::Value> aOut) {
1635 JS::Rooted<JSObject*> containerObj(aCx, JS_NewPlainObject(aCx));
1636 if (!containerObj) {
1637 return NS_ERROR_OUT_OF_MEMORY;
1639 aOut.setObject(*containerObj);
1641 JS::Rooted<JSObject*> featureArray(aCx, JS::NewArrayObject(aCx, 0));
1642 if (!featureArray) {
1643 return NS_ERROR_OUT_OF_MEMORY;
1646 // Collect features.
1647 gfxConfig::ForEachFeature([&](const char* aName, const char* aDescription,
1648 FeatureState& aFeature) -> void {
1649 JS::Rooted<JSObject*> obj(aCx, JS_NewPlainObject(aCx));
1650 if (!obj) {
1651 return;
1653 if (!SetJSPropertyString(aCx, obj, "name", aName) ||
1654 !SetJSPropertyString(aCx, obj, "description", aDescription) ||
1655 !SetJSPropertyString(aCx, obj, "status",
1656 FeatureStatusToString(aFeature.GetValue()))) {
1657 return;
1660 JS::Rooted<JS::Value> log(aCx);
1661 if (!BuildFeatureStateLog(aCx, aFeature, &log)) {
1662 return;
1664 if (!JS_SetProperty(aCx, obj, "log", log)) {
1665 return;
1668 if (!AppendJSElement(aCx, featureArray, obj)) {
1669 return;
1673 JS::Rooted<JSObject*> fallbackArray(aCx, JS::NewArrayObject(aCx, 0));
1674 if (!fallbackArray) {
1675 return NS_ERROR_OUT_OF_MEMORY;
1678 // Collect fallbacks.
1679 gfxConfig::ForEachFallback(
1680 [&](const char* aName, const char* aMessage) -> void {
1681 JS::Rooted<JSObject*> obj(aCx, JS_NewPlainObject(aCx));
1682 if (!obj) {
1683 return;
1686 if (!SetJSPropertyString(aCx, obj, "name", aName) ||
1687 !SetJSPropertyString(aCx, obj, "message", aMessage)) {
1688 return;
1691 if (!AppendJSElement(aCx, fallbackArray, obj)) {
1692 return;
1696 JS::Rooted<JS::Value> val(aCx);
1698 val = JS::ObjectValue(*featureArray);
1699 JS_SetProperty(aCx, containerObj, "features", val);
1701 val = JS::ObjectValue(*fallbackArray);
1702 JS_SetProperty(aCx, containerObj, "fallbacks", val);
1704 return NS_OK;
1707 bool GfxInfoBase::BuildFeatureStateLog(JSContext* aCx,
1708 const FeatureState& aFeature,
1709 JS::MutableHandle<JS::Value> aOut) {
1710 JS::Rooted<JSObject*> log(aCx, JS::NewArrayObject(aCx, 0));
1711 if (!log) {
1712 return false;
1714 aOut.setObject(*log);
1716 aFeature.ForEachStatusChange([&](const char* aType, FeatureStatus aStatus,
1717 const char* aMessage,
1718 const nsCString& aFailureId) -> void {
1719 JS::Rooted<JSObject*> obj(aCx, JS_NewPlainObject(aCx));
1720 if (!obj) {
1721 return;
1724 if (!SetJSPropertyString(aCx, obj, "type", aType) ||
1725 !SetJSPropertyString(aCx, obj, "status",
1726 FeatureStatusToString(aStatus)) ||
1727 (!aFailureId.IsEmpty() &&
1728 !SetJSPropertyString(aCx, obj, "failureId", aFailureId.get())) ||
1729 (aMessage && !SetJSPropertyString(aCx, obj, "message", aMessage))) {
1730 return;
1733 if (!AppendJSElement(aCx, log, obj)) {
1734 return;
1738 return true;
1741 void GfxInfoBase::DescribeFeatures(JSContext* aCx, JS::Handle<JSObject*> aObj) {
1742 JS::Rooted<JSObject*> obj(aCx);
1744 gfx::FeatureState& hwCompositing =
1745 gfxConfig::GetFeature(gfx::Feature::HW_COMPOSITING);
1746 InitFeatureObject(aCx, aObj, "hwCompositing", hwCompositing, &obj);
1748 gfx::FeatureState& gpuProcess =
1749 gfxConfig::GetFeature(gfx::Feature::GPU_PROCESS);
1750 InitFeatureObject(aCx, aObj, "gpuProcess", gpuProcess, &obj);
1752 gfx::FeatureState& webrender = gfxConfig::GetFeature(gfx::Feature::WEBRENDER);
1753 InitFeatureObject(aCx, aObj, "webrender", webrender, &obj);
1755 gfx::FeatureState& wrCompositor =
1756 gfxConfig::GetFeature(gfx::Feature::WEBRENDER_COMPOSITOR);
1757 InitFeatureObject(aCx, aObj, "wrCompositor", wrCompositor, &obj);
1759 gfx::FeatureState& openglCompositing =
1760 gfxConfig::GetFeature(gfx::Feature::OPENGL_COMPOSITING);
1761 InitFeatureObject(aCx, aObj, "openglCompositing", openglCompositing, &obj);
1763 gfx::FeatureState& omtp = gfxConfig::GetFeature(gfx::Feature::OMTP);
1764 InitFeatureObject(aCx, aObj, "omtp", omtp, &obj);
1767 bool GfxInfoBase::InitFeatureObject(JSContext* aCx,
1768 JS::Handle<JSObject*> aContainer,
1769 const char* aName,
1770 mozilla::gfx::FeatureState& aFeatureState,
1771 JS::MutableHandle<JSObject*> aOutObj) {
1772 JS::Rooted<JSObject*> obj(aCx, JS_NewPlainObject(aCx));
1773 if (!obj) {
1774 return false;
1777 nsCString status = aFeatureState.GetStatusAndFailureIdString();
1779 JS::Rooted<JSString*> str(aCx, JS_NewStringCopyZ(aCx, status.get()));
1780 JS::Rooted<JS::Value> val(aCx, JS::StringValue(str));
1781 JS_SetProperty(aCx, obj, "status", val);
1783 // Add the feature object to the container.
1785 JS::Rooted<JS::Value> val(aCx, JS::ObjectValue(*obj));
1786 JS_SetProperty(aCx, aContainer, aName, val);
1789 aOutObj.set(obj);
1790 return true;
1793 nsresult GfxInfoBase::GetActiveCrashGuards(JSContext* aCx,
1794 JS::MutableHandle<JS::Value> aOut) {
1795 JS::Rooted<JSObject*> array(aCx, JS::NewArrayObject(aCx, 0));
1796 if (!array) {
1797 return NS_ERROR_OUT_OF_MEMORY;
1799 aOut.setObject(*array);
1801 DriverCrashGuard::ForEachActiveCrashGuard(
1802 [&](const char* aName, const char* aPrefName) -> void {
1803 JS::Rooted<JSObject*> obj(aCx, JS_NewPlainObject(aCx));
1804 if (!obj) {
1805 return;
1807 if (!SetJSPropertyString(aCx, obj, "type", aName)) {
1808 return;
1810 if (!SetJSPropertyString(aCx, obj, "prefName", aPrefName)) {
1811 return;
1813 if (!AppendJSElement(aCx, array, obj)) {
1814 return;
1818 return NS_OK;
1821 NS_IMETHODIMP
1822 GfxInfoBase::GetTargetFrameRate(uint32_t* aTargetFrameRate) {
1823 *aTargetFrameRate = gfxPlatform::TargetFrameRate();
1824 return NS_OK;
1827 NS_IMETHODIMP
1828 GfxInfoBase::GetCodecSupportInfo(nsACString& aCodecSupportInfo) {
1829 aCodecSupportInfo.Assign(gfx::gfxVars::CodecSupportInfo());
1830 return NS_OK;
1833 NS_IMETHODIMP
1834 GfxInfoBase::GetIsHeadless(bool* aIsHeadless) {
1835 *aIsHeadless = gfxPlatform::IsHeadless();
1836 return NS_OK;
1839 NS_IMETHODIMP
1840 GfxInfoBase::GetContentBackend(nsAString& aContentBackend) {
1841 BackendType backend = gfxPlatform::GetPlatform()->GetDefaultContentBackend();
1842 nsString outStr;
1844 switch (backend) {
1845 case BackendType::DIRECT2D1_1: {
1846 outStr.AppendPrintf("Direct2D 1.1");
1847 break;
1849 case BackendType::SKIA: {
1850 outStr.AppendPrintf("Skia");
1851 break;
1853 case BackendType::CAIRO: {
1854 outStr.AppendPrintf("Cairo");
1855 break;
1857 default:
1858 return NS_ERROR_FAILURE;
1861 aContentBackend.Assign(outStr);
1862 return NS_OK;
1865 NS_IMETHODIMP
1866 GfxInfoBase::GetAzureCanvasBackend(nsAString& aBackend) {
1867 CopyASCIItoUTF16(mozilla::MakeStringSpan(
1868 gfxPlatform::GetPlatform()->GetAzureCanvasBackend()),
1869 aBackend);
1870 return NS_OK;
1873 NS_IMETHODIMP
1874 GfxInfoBase::GetAzureContentBackend(nsAString& aBackend) {
1875 CopyASCIItoUTF16(mozilla::MakeStringSpan(
1876 gfxPlatform::GetPlatform()->GetAzureContentBackend()),
1877 aBackend);
1878 return NS_OK;
1881 NS_IMETHODIMP
1882 GfxInfoBase::GetUsingGPUProcess(bool* aOutValue) {
1883 GPUProcessManager* gpu = GPUProcessManager::Get();
1884 if (!gpu) {
1885 // Not supported in content processes.
1886 return NS_ERROR_FAILURE;
1889 *aOutValue = !!gpu->GetGPUChild();
1890 return NS_OK;
1893 NS_IMETHODIMP_(int32_t)
1894 GfxInfoBase::GetMaxRefreshRate(bool* aMixed) {
1895 if (aMixed) {
1896 *aMixed = false;
1899 int32_t maxRefreshRate = 0;
1900 for (auto& screen : ScreenManager::GetSingleton().CurrentScreenList()) {
1901 int32_t refreshRate = screen->GetRefreshRate();
1902 if (aMixed && maxRefreshRate > 0 && maxRefreshRate != refreshRate) {
1903 *aMixed = true;
1905 maxRefreshRate = std::max(maxRefreshRate, refreshRate);
1908 return maxRefreshRate > 0 ? maxRefreshRate : -1;
1911 NS_IMETHODIMP
1912 GfxInfoBase::ControlGPUProcessForXPCShell(bool aEnable, bool* _retval) {
1913 gfxPlatform::GetPlatform();
1915 GPUProcessManager* gpm = GPUProcessManager::Get();
1916 if (aEnable) {
1917 if (!gfxConfig::IsEnabled(gfx::Feature::GPU_PROCESS)) {
1918 gfxConfig::UserForceEnable(gfx::Feature::GPU_PROCESS, "xpcshell-test");
1920 gpm->EnsureGPUReady();
1921 } else {
1922 gfxConfig::UserDisable(gfx::Feature::GPU_PROCESS, "xpcshell-test");
1923 gpm->KillProcess();
1926 *_retval = true;
1927 return NS_OK;
1930 NS_IMETHODIMP GfxInfoBase::KillGPUProcessForTests() {
1931 GPUProcessManager* gpm = GPUProcessManager::Get();
1932 if (!gpm) {
1933 // gfxPlatform has not been initialized.
1934 return NS_ERROR_NOT_INITIALIZED;
1937 gpm->KillProcess();
1938 return NS_OK;
1941 NS_IMETHODIMP GfxInfoBase::CrashGPUProcessForTests() {
1942 GPUProcessManager* gpm = GPUProcessManager::Get();
1943 if (!gpm) {
1944 // gfxPlatform has not been initialized.
1945 return NS_ERROR_NOT_INITIALIZED;
1948 gpm->CrashProcess();
1949 return NS_OK;
1952 GfxInfoCollectorBase::GfxInfoCollectorBase() {
1953 GfxInfoBase::AddCollector(this);
1956 GfxInfoCollectorBase::~GfxInfoCollectorBase() {
1957 GfxInfoBase::RemoveCollector(this);