Bug 1856663 - Add more chunks for Android mochitest-plain. r=jmaher,taskgraph-reviewe...
[gecko.git] / gfx / thebes / gfxPlatform.cpp
blobd7c03b967f8921fd19bfefef2777c08357a3a68b
1 /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include "mozilla/FontPropertyTypes.h"
7 #include "mozilla/RDDProcessManager.h"
8 #include "mozilla/image/ImageMemoryReporter.h"
9 #include "mozilla/layers/CompositorManagerChild.h"
10 #include "mozilla/layers/CompositorThread.h"
11 #include "mozilla/layers/ImageBridgeChild.h"
12 #include "mozilla/layers/ISurfaceAllocator.h" // for GfxMemoryImageReporter
13 #include "mozilla/layers/CompositorBridgeChild.h"
14 #include "mozilla/layers/RemoteTextureMap.h"
15 #include "mozilla/layers/VideoBridgeParent.h"
16 #include "mozilla/webrender/RenderThread.h"
17 #include "mozilla/webrender/WebRenderAPI.h"
18 #include "mozilla/webrender/webrender_ffi.h"
19 #include "mozilla/gfx/BuildConstants.h"
20 #include "mozilla/gfx/gfxConfigManager.h"
21 #include "mozilla/gfx/gfxVars.h"
22 #include "mozilla/gfx/GPUProcessManager.h"
23 #include "mozilla/gfx/GraphicsMessages.h"
24 #include "mozilla/gfx/CanvasManagerChild.h"
25 #include "mozilla/gfx/CanvasRenderThread.h"
26 #include "mozilla/ClearOnShutdown.h"
27 #include "mozilla/DebugOnly.h"
28 #include "mozilla/EnumTypeTraits.h"
29 #include "mozilla/StaticPrefs_accessibility.h"
30 #include "mozilla/StaticPrefs_apz.h"
31 #include "mozilla/StaticPrefs_bidi.h"
32 #include "mozilla/StaticPrefs_gfx.h"
33 #include "mozilla/StaticPrefs_layout.h"
34 #include "mozilla/StaticPrefs_layers.h"
35 #include "mozilla/StaticPrefs_media.h"
36 #include "mozilla/StaticPrefs_privacy.h"
37 #include "mozilla/StaticPrefs_webgl.h"
38 #include "mozilla/StaticPrefs_widget.h"
39 #include "mozilla/Telemetry.h"
40 #include "mozilla/TimeStamp.h"
41 #include "mozilla/Unused.h"
42 #include "mozilla/IntegerPrintfMacros.h"
43 #include "mozilla/Base64.h"
44 #include "mozilla/VsyncDispatcher.h"
46 #include "mozilla/Logging.h"
47 #include "mozilla/Components.h"
48 #include "nsAppRunner.h"
49 #include "nsAppDirectoryServiceDefs.h"
50 #include "nsCSSProps.h"
51 #include "nsContentUtils.h"
53 #include "gfxCrashReporterUtils.h"
54 #include "gfxPlatform.h"
55 #include "gfxPlatformWorker.h"
57 #include "gfxBlur.h"
58 #include "gfxEnv.h"
59 #include "gfxTextRun.h"
60 #include "gfxUserFontSet.h"
61 #include "gfxConfig.h"
62 #include "GfxDriverInfo.h"
63 #include "VRProcessManager.h"
64 #include "VRThread.h"
66 #ifdef XP_WIN
67 # include <process.h>
68 # define getpid _getpid
69 #else
70 # include <unistd.h>
71 #endif
73 #include "nsXULAppAPI.h"
74 #include "nsIXULAppInfo.h"
75 #include "nsDirectoryServiceUtils.h"
76 #include "nsDirectoryServiceDefs.h"
78 #if defined(XP_WIN)
79 # include "gfxWindowsPlatform.h"
80 # include "mozilla/widget/WinWindowOcclusionTracker.h"
81 #elif defined(XP_MACOSX)
82 # include "gfxPlatformMac.h"
83 # include "gfxQuartzSurface.h"
84 #elif defined(MOZ_WIDGET_GTK)
85 # include "gfxPlatformGtk.h"
86 #elif defined(ANDROID)
87 # include "gfxAndroidPlatform.h"
88 #endif
89 #if defined(MOZ_WIDGET_ANDROID)
90 # include "mozilla/jni/Utils.h" // for IsFennec
91 #endif
93 #ifdef XP_WIN
94 # include "mozilla/WindowsVersion.h"
95 # include "WinUtils.h"
96 #endif
98 #include "nsGkAtoms.h"
99 #include "gfxPlatformFontList.h"
100 #include "gfxContext.h"
101 #include "gfxImageSurface.h"
102 #include "nsUnicodeProperties.h"
103 #include "harfbuzz/hb.h"
104 #include "gfxGraphiteShaper.h"
105 #include "gfx2DGlue.h"
106 #include "gfxGradientCache.h"
107 #include "gfxUtils.h" // for NextPowerOfTwo
108 #include "gfxFontMissingGlyphs.h"
110 #include "nsExceptionHandler.h"
111 #include "nsServiceManagerUtils.h"
112 #include "nsTArray.h"
113 #include "nsIObserverService.h"
114 #include "mozilla/widget/Screen.h"
115 #include "mozilla/widget/ScreenManager.h"
116 #include "MainThreadUtils.h"
118 #include "nsWeakReference.h"
120 #include "cairo.h"
121 #include "qcms.h"
123 #include "imgITools.h"
125 #include "nsCRT.h"
126 #include "GLContext.h"
127 #include "GLContextProvider.h"
128 #include "mozilla/gfx/Logging.h"
130 #ifdef __GNUC__
131 # pragma GCC diagnostic push
132 # pragma GCC diagnostic ignored "-Wshadow"
133 #endif
134 #include "skia/include/core/SkGraphics.h"
135 #ifdef MOZ_ENABLE_FREETYPE
136 # include "skia/include/ports/SkTypeface_cairo.h"
137 #endif
138 #include "mozilla/gfx/SkMemoryReporter.h"
139 #ifdef __GNUC__
140 # pragma GCC diagnostic pop // -Wshadow
141 #endif
142 static const uint32_t kDefaultGlyphCacheSize = -1;
144 #include "mozilla/Preferences.h"
145 #include "mozilla/Assertions.h"
146 #include "mozilla/Atomics.h"
147 #include "mozilla/Attributes.h"
148 #include "mozilla/Mutex.h"
150 #include "nsAlgorithm.h"
151 #include "nsIGfxInfo.h"
152 #include "nsIXULRuntime.h"
153 #include "VsyncSource.h"
154 #include "SoftwareVsyncSource.h"
155 #include "nscore.h" // for NS_FREE_PERMANENT_DATA
156 #include "mozilla/dom/ContentChild.h"
157 #include "mozilla/dom/ContentParent.h"
158 #include "mozilla/dom/TouchEvent.h"
159 #include "gfxVR.h"
160 #include "VRManager.h"
161 #include "VRManagerChild.h"
162 #include "mozilla/gfx/GPUParent.h"
163 #include "prsystem.h"
165 #include "mozilla/gfx/2D.h"
166 #include "mozilla/gfx/SourceSurfaceCairo.h"
168 using namespace mozilla;
169 using namespace mozilla::layers;
170 using namespace mozilla::gl;
171 using namespace mozilla::gfx;
173 gfxPlatform* gPlatform = nullptr;
174 static bool gEverInitialized = false;
176 const ContentDeviceData* gContentDeviceInitData = nullptr;
178 Atomic<bool, MemoryOrdering::ReleaseAcquire> gfxPlatform::gCMSInitialized;
179 CMSMode gfxPlatform::gCMSMode = CMSMode::Off;
181 // These two may point to the same profile
182 qcms_profile* gfxPlatform::gCMSOutputProfile = nullptr;
183 qcms_profile* gfxPlatform::gCMSsRGBProfile = nullptr;
185 qcms_transform* gfxPlatform::gCMSRGBTransform = nullptr;
186 qcms_transform* gfxPlatform::gCMSInverseRGBTransform = nullptr;
187 qcms_transform* gfxPlatform::gCMSRGBATransform = nullptr;
188 qcms_transform* gfxPlatform::gCMSBGRATransform = nullptr;
190 /// This override of the LogForwarder, initially used for the critical graphics
191 /// errors, is sending the log to the crash annotations as well, but only
192 /// if the capacity set with the method below is >= 2. We always retain the
193 /// very first critical message, and the latest capacity-1 messages are
194 /// rotated through. Note that we don't expect the total number of times
195 /// this gets called to be large - it is meant for critical errors only.
197 class CrashStatsLogForwarder : public mozilla::gfx::LogForwarder {
198 public:
199 explicit CrashStatsLogForwarder(CrashReporter::Annotation aKey);
200 void Log(const std::string& aString) override;
201 void CrashAction(LogReason aReason) override;
202 bool UpdateStringsVector(const std::string& aString) override;
204 LoggingRecord LoggingRecordCopy() override;
206 void SetCircularBufferSize(uint32_t aCapacity);
208 private:
209 // Helper for the Log()
210 void UpdateCrashReport();
212 private:
213 LoggingRecord mBuffer;
214 CrashReporter::Annotation mCrashCriticalKey;
215 uint32_t mMaxCapacity;
216 int32_t mIndex;
217 Mutex mMutex MOZ_UNANNOTATED;
220 CrashStatsLogForwarder::CrashStatsLogForwarder(CrashReporter::Annotation aKey)
221 : mBuffer(),
222 mCrashCriticalKey(aKey),
223 mMaxCapacity(0),
224 mIndex(-1),
225 mMutex("CrashStatsLogForwarder") {}
227 void CrashStatsLogForwarder::SetCircularBufferSize(uint32_t aCapacity) {
228 MutexAutoLock lock(mMutex);
230 mMaxCapacity = aCapacity;
231 mBuffer.reserve(static_cast<size_t>(aCapacity));
234 LoggingRecord CrashStatsLogForwarder::LoggingRecordCopy() {
235 MutexAutoLock lock(mMutex);
236 return mBuffer;
239 bool CrashStatsLogForwarder::UpdateStringsVector(const std::string& aString) {
240 // We want at least the first one and the last one. Otherwise, no point.
241 if (mMaxCapacity < 2) {
242 return false;
245 mIndex += 1;
246 MOZ_ASSERT(mIndex >= 0);
248 // index will count 0, 1, 2, ..., max-1, 1, 2, ..., max-1, 1, 2, ...
249 int32_t index = mIndex ? (mIndex - 1) % (mMaxCapacity - 1) + 1 : 0;
250 MOZ_ASSERT(index >= 0 && index < (int32_t)mMaxCapacity);
251 MOZ_ASSERT(index <= mIndex && index <= (int32_t)mBuffer.size());
253 double tStamp = (TimeStamp::NowLoRes() - TimeStamp::ProcessCreation())
254 .ToSecondsSigDigits();
256 // Checking for index >= mBuffer.size(), rather than index == mBuffer.size()
257 // just out of paranoia, but we know index <= mBuffer.size().
258 LoggingRecordEntry newEntry(mIndex, aString, tStamp);
259 if (index >= static_cast<int32_t>(mBuffer.size())) {
260 mBuffer.push_back(newEntry);
261 } else {
262 mBuffer[index] = newEntry;
264 return true;
267 void CrashStatsLogForwarder::UpdateCrashReport() {
268 std::stringstream message;
269 std::string logAnnotation;
271 switch (XRE_GetProcessType()) {
272 case GeckoProcessType_Default:
273 logAnnotation = "|[";
274 break;
275 case GeckoProcessType_Content:
276 logAnnotation = "|[C";
277 break;
278 case GeckoProcessType_GPU:
279 logAnnotation = "|[G";
280 break;
281 default:
282 logAnnotation = "|[X";
283 break;
286 for (auto& it : mBuffer) {
287 message << logAnnotation << std::get<0>(it) << "]" << std::get<1>(it)
288 << " (t=" << std::get<2>(it) << ") ";
291 nsCString reportString(message.str().c_str());
292 nsresult annotated =
293 CrashReporter::AnnotateCrashReport(mCrashCriticalKey, reportString);
295 if (annotated != NS_OK) {
296 printf("Crash Annotation %s: %s",
297 CrashReporter::AnnotationToString(mCrashCriticalKey),
298 message.str().c_str());
302 class LogForwarderEvent : public Runnable {
303 virtual ~LogForwarderEvent() = default;
305 public:
306 NS_INLINE_DECL_REFCOUNTING_INHERITED(LogForwarderEvent, Runnable)
308 explicit LogForwarderEvent(const nsCString& aMessage)
309 : mozilla::Runnable("LogForwarderEvent"), mMessage(aMessage) {}
311 NS_IMETHOD Run() override {
312 MOZ_ASSERT(NS_IsMainThread() &&
313 (XRE_IsContentProcess() || XRE_IsGPUProcess()));
315 if (XRE_IsContentProcess()) {
316 dom::ContentChild* cc = dom::ContentChild::GetSingleton();
317 Unused << cc->SendGraphicsError(mMessage);
318 } else if (XRE_IsGPUProcess()) {
319 GPUParent* gp = GPUParent::GetSingleton();
320 Unused << gp->SendGraphicsError(mMessage);
323 return NS_OK;
326 protected:
327 nsCString mMessage;
330 void CrashStatsLogForwarder::Log(const std::string& aString) {
331 MutexAutoLock lock(mMutex);
333 if (UpdateStringsVector(aString)) {
334 UpdateCrashReport();
337 // Add it to the parent strings
338 if (!XRE_IsParentProcess()) {
339 nsCString stringToSend(aString.c_str());
340 if (NS_IsMainThread()) {
341 if (XRE_IsContentProcess()) {
342 dom::ContentChild* cc = dom::ContentChild::GetSingleton();
343 Unused << cc->SendGraphicsError(stringToSend);
344 } else if (XRE_IsGPUProcess()) {
345 GPUParent* gp = GPUParent::GetSingleton();
346 Unused << gp->SendGraphicsError(stringToSend);
348 } else {
349 nsCOMPtr<nsIRunnable> r1 = new LogForwarderEvent(stringToSend);
350 NS_DispatchToMainThread(r1);
355 class CrashTelemetryEvent : public Runnable {
356 virtual ~CrashTelemetryEvent() = default;
358 public:
359 NS_INLINE_DECL_REFCOUNTING_INHERITED(CrashTelemetryEvent, Runnable)
361 explicit CrashTelemetryEvent(uint32_t aReason)
362 : mozilla::Runnable("CrashTelemetryEvent"), mReason(aReason) {}
364 NS_IMETHOD Run() override {
365 MOZ_ASSERT(NS_IsMainThread());
366 Telemetry::Accumulate(Telemetry::GFX_CRASH, mReason);
367 return NS_OK;
370 protected:
371 uint32_t mReason;
374 void CrashStatsLogForwarder::CrashAction(LogReason aReason) {
375 #ifndef RELEASE_OR_BETA
376 // Non-release builds crash by default, but will use telemetry
377 // if this environment variable is present.
378 static bool useTelemetry = gfxEnv::MOZ_GFX_CRASH_TELEMETRY();
379 #else
380 // Release builds use telemetry by default, but will crash instead
381 // if this environment variable is present.
382 static bool useTelemetry = !gfxEnv::MOZ_GFX_CRASH_MOZ_CRASH();
383 #endif
385 if (useTelemetry) {
386 // The callers need to assure that aReason is in the range
387 // that the telemetry call below supports.
388 if (NS_IsMainThread()) {
389 Telemetry::Accumulate(Telemetry::GFX_CRASH, (uint32_t)aReason);
390 } else {
391 nsCOMPtr<nsIRunnable> r1 = new CrashTelemetryEvent((uint32_t)aReason);
392 NS_DispatchToMainThread(r1);
394 } else {
395 // ignoring aReason, we can get the information we need from the stack
396 MOZ_CRASH("GFX_CRASH");
400 #define GFX_DOWNLOADABLE_FONTS_ENABLED "gfx.downloadable_fonts.enabled"
402 #define GFX_PREF_FALLBACK_USE_CMAPS \
403 "gfx.font_rendering.fallback.always_use_cmaps"
405 #define GFX_PREF_OPENTYPE_SVG "gfx.font_rendering.opentype_svg.enabled"
407 #define GFX_PREF_WORD_CACHE_CHARLIMIT "gfx.font_rendering.wordcache.charlimit"
408 #define GFX_PREF_WORD_CACHE_MAXENTRIES "gfx.font_rendering.wordcache.maxentries"
410 #define GFX_PREF_GRAPHITE_SHAPING "gfx.font_rendering.graphite.enabled"
411 #if defined(XP_MACOSX)
412 # define GFX_PREF_CORETEXT_SHAPING "gfx.font_rendering.coretext.enabled"
413 #endif
415 #define FONT_VARIATIONS_PREF "layout.css.font-variations.enabled"
417 static const char* kObservedPrefs[] = {"gfx.downloadable_fonts.",
418 "gfx.font_rendering.", nullptr};
420 static void FontPrefChanged(const char* aPref, void* aData) {
421 MOZ_ASSERT(aPref);
422 NS_ASSERTION(gfxPlatform::GetPlatform(), "the singleton instance has gone");
423 gfxPlatform::GetPlatform()->FontsPrefsChanged(aPref);
426 void gfxPlatform::OnMemoryPressure(layers::MemoryPressureReason aWhy) {
427 Factory::PurgeAllCaches();
428 gfxGradientCache::PurgeAllCaches();
429 gfxFontMissingGlyphs::Purge();
430 PurgeSkiaFontCache();
431 if (XRE_IsParentProcess()) {
432 layers::CompositorManagerChild* manager =
433 CompositorManagerChild::GetInstance();
434 if (manager) {
435 manager->SendNotifyMemoryPressure();
440 gfxPlatform::gfxPlatform()
441 : mAzureCanvasBackendCollector(this, &gfxPlatform::GetAzureBackendInfo),
442 mApzSupportCollector(this, &gfxPlatform::GetApzSupportInfo),
443 mFrameStatsCollector(this, &gfxPlatform::GetFrameStats),
444 mCMSInfoCollector(this, &gfxPlatform::GetCMSSupportInfo),
445 mDisplayInfoCollector(this, &gfxPlatform::GetDisplayInfo),
446 mOverlayInfoCollector(this, &gfxPlatform::GetOverlayInfo),
447 mSwapChainInfoCollector(this, &gfxPlatform::GetSwapChainInfo),
448 mCompositorBackend(layers::LayersBackend::LAYERS_NONE),
449 mScreenDepth(0) {
450 mAllowDownloadableFonts = UNINITIALIZED_VALUE;
452 InitBackendPrefs(GetBackendPrefs());
453 VRManager::ManagerInit();
456 gfxPlatform* gfxPlatform::GetPlatform() {
457 if (!gPlatform) {
458 MOZ_RELEASE_ASSERT(!XRE_IsContentProcess(),
459 "Content Process should have called InitChild() before "
460 "first GetPlatform()");
461 Init();
463 return gPlatform;
466 bool gfxPlatform::Initialized() { return !!gPlatform; }
468 /* static */
469 void gfxPlatform::InitChild(const ContentDeviceData& aData) {
470 MOZ_ASSERT(XRE_IsContentProcess());
471 MOZ_RELEASE_ASSERT(!gPlatform,
472 "InitChild() should be called before first GetPlatform()");
473 // Make the provided initial ContentDeviceData available to the init
474 // routines, so they don't have to do a sync request from the parent.
475 gContentDeviceInitData = &aData;
476 Init();
477 gContentDeviceInitData = nullptr;
480 #define WR_DEBUG_PREF "gfx.webrender.debug"
482 static void SwapIntervalPrefChangeCallback(const char* aPrefName, void*) {
483 bool egl = Preferences::GetBool("gfx.swap-interval.egl", false);
484 bool glx = Preferences::GetBool("gfx.swap-interval.glx", false);
485 gfxVars::SetSwapIntervalEGL(egl);
486 gfxVars::SetSwapIntervalGLX(glx);
489 static void WebRendeProfilerUIPrefChangeCallback(const char* aPrefName, void*) {
490 nsCString uiString;
491 if (NS_SUCCEEDED(Preferences::GetCString("gfx.webrender.debug.profiler-ui",
492 uiString))) {
493 gfxVars::SetWebRenderProfilerUI(uiString);
497 // List of boolean dynamic parameter for WebRender.
499 // The parameters in this list are:
500 // - The pref name.
501 // - The BoolParameter enum variant (see webrender_api/src/lib.rs)
502 // - A default value.
503 #define WR_BOOL_PARAMETER_LIST(_) \
504 _("gfx.webrender.batched-texture-uploads", \
505 wr::BoolParameter::BatchedUploads, true) \
506 _("gfx.webrender.draw-calls-for-texture-copy", \
507 wr::BoolParameter::DrawCallsForTextureCopy, true) \
508 _("gfx.webrender.pbo-uploads", wr::BoolParameter::PboUploads, true) \
509 _("gfx.webrender.multithreading", wr::BoolParameter::Multithreading, true)
511 static void WebRenderBoolParameterChangeCallback(const char*, void*) {
512 uint32_t bits = 0;
514 #define WR_BOOL_PARAMETER(name, key, default_val) \
515 if (Preferences::GetBool(name, default_val)) { \
516 bits |= 1 << (uint32_t)key; \
519 WR_BOOL_PARAMETER_LIST(WR_BOOL_PARAMETER)
520 #undef WR_BOOL_PARAMETER
522 gfx::gfxVars::SetWebRenderBoolParameters(bits);
525 static void RegisterWebRenderBoolParamCallback() {
526 #define WR_BOOL_PARAMETER(name, _key, _default_val) \
527 Preferences::RegisterCallback(WebRenderBoolParameterChangeCallback, name);
529 WR_BOOL_PARAMETER_LIST(WR_BOOL_PARAMETER)
530 #undef WR_BOOL_PARAMETER
532 WebRenderBoolParameterChangeCallback(nullptr, nullptr);
535 static void WebRenderDebugPrefChangeCallback(const char* aPrefName, void*) {
536 wr::DebugFlags flags{0};
537 #define GFX_WEBRENDER_DEBUG(suffix, bit) \
538 if (Preferences::GetBool(WR_DEBUG_PREF suffix, false)) { \
539 flags |= (bit); \
542 GFX_WEBRENDER_DEBUG(".profiler", wr::DebugFlags::PROFILER_DBG)
543 GFX_WEBRENDER_DEBUG(".render-targets", wr::DebugFlags::RENDER_TARGET_DBG)
544 GFX_WEBRENDER_DEBUG(".texture-cache", wr::DebugFlags::TEXTURE_CACHE_DBG)
545 GFX_WEBRENDER_DEBUG(".gpu-time-queries", wr::DebugFlags::GPU_TIME_QUERIES)
546 GFX_WEBRENDER_DEBUG(".gpu-sample-queries", wr::DebugFlags::GPU_SAMPLE_QUERIES)
547 GFX_WEBRENDER_DEBUG(".disable-batching", wr::DebugFlags::DISABLE_BATCHING)
548 GFX_WEBRENDER_DEBUG(".epochs", wr::DebugFlags::EPOCHS)
549 GFX_WEBRENDER_DEBUG(".smart-profiler", wr::DebugFlags::SMART_PROFILER)
550 GFX_WEBRENDER_DEBUG(".echo-driver-messages",
551 wr::DebugFlags::ECHO_DRIVER_MESSAGES)
552 GFX_WEBRENDER_DEBUG(".show-overdraw", wr::DebugFlags::SHOW_OVERDRAW)
553 GFX_WEBRENDER_DEBUG(".gpu-cache", wr::DebugFlags::GPU_CACHE_DBG)
554 GFX_WEBRENDER_DEBUG(".texture-cache.clear-evicted",
555 wr::DebugFlags::TEXTURE_CACHE_DBG_CLEAR_EVICTED)
556 GFX_WEBRENDER_DEBUG(".picture-caching", wr::DebugFlags::PICTURE_CACHING_DBG)
557 GFX_WEBRENDER_DEBUG(".force-picture-invalidation",
558 wr::DebugFlags::FORCE_PICTURE_INVALIDATION)
559 GFX_WEBRENDER_DEBUG(".primitives", wr::DebugFlags::PRIMITIVE_DBG)
560 // Bit 18 is for the zoom display, which requires the mouse position and thus
561 // currently only works in wrench.
562 GFX_WEBRENDER_DEBUG(".small-screen", wr::DebugFlags::SMALL_SCREEN)
563 GFX_WEBRENDER_DEBUG(".disable-opaque-pass",
564 wr::DebugFlags::DISABLE_OPAQUE_PASS)
565 GFX_WEBRENDER_DEBUG(".disable-alpha-pass", wr::DebugFlags::DISABLE_ALPHA_PASS)
566 GFX_WEBRENDER_DEBUG(".disable-clip-masks", wr::DebugFlags::DISABLE_CLIP_MASKS)
567 GFX_WEBRENDER_DEBUG(".disable-text-prims", wr::DebugFlags::DISABLE_TEXT_PRIMS)
568 GFX_WEBRENDER_DEBUG(".disable-gradient-prims",
569 wr::DebugFlags::DISABLE_GRADIENT_PRIMS)
570 GFX_WEBRENDER_DEBUG(".obscure-images", wr::DebugFlags::OBSCURE_IMAGES)
571 GFX_WEBRENDER_DEBUG(".glyph-flashing", wr::DebugFlags::GLYPH_FLASHING)
572 GFX_WEBRENDER_DEBUG(".capture-profiler", wr::DebugFlags::PROFILER_CAPTURE)
573 GFX_WEBRENDER_DEBUG(".window-visibility",
574 wr::DebugFlags::WINDOW_VISIBILITY_DBG)
575 GFX_WEBRENDER_DEBUG(".restrict-blob-size", wr::DebugFlags::RESTRICT_BLOB_SIZE)
576 #undef GFX_WEBRENDER_DEBUG
577 gfx::gfxVars::SetWebRenderDebugFlags(flags._0);
580 static void WebRenderQualityPrefChangeCallback(const char* aPref, void*) {
581 gfxPlatform::GetPlatform()->UpdateForceSubpixelAAWherePossible();
584 static void WebRenderBatchingPrefChangeCallback(const char* aPrefName, void*) {
585 uint32_t count = Preferences::GetUint(
586 StaticPrefs::GetPrefName_gfx_webrender_batching_lookback(), 10);
588 gfx::gfxVars::SetWebRenderBatchingLookback(count);
591 static void WebRenderBlobTileSizePrefChangeCallback(const char* aPrefName,
592 void*) {
593 uint32_t tileSize = Preferences::GetUint(
594 StaticPrefs::GetPrefName_gfx_webrender_blob_tile_size(), 256);
595 gfx::gfxVars::SetWebRenderBlobTileSize(tileSize);
598 static void WebRenderUploadThresholdPrefChangeCallback(const char* aPrefName,
599 void*) {
600 int value = Preferences::GetInt(
601 StaticPrefs::GetPrefName_gfx_webrender_batched_upload_threshold(),
602 512 * 512);
604 gfxVars::SetWebRenderBatchedUploadThreshold(value);
607 static uint32_t GetSkiaGlyphCacheSize() {
608 // Only increase font cache size on non-android to save memory.
609 #if !defined(MOZ_WIDGET_ANDROID)
610 // 10mb as the default pref cache size on desktop due to talos perf tweaking.
611 // Chromium uses 20mb and skia default uses 2mb.
612 // We don't need to change the font cache count since we usually
613 // cache thrash due to asian character sets in talos.
614 // Only increase memory on the content process
615 uint32_t cacheSize =
616 StaticPrefs::gfx_content_skia_font_cache_size_AtStartup() * 1024 * 1024;
617 if (mozilla::BrowserTabsRemoteAutostart()) {
618 return XRE_IsContentProcess() ? cacheSize : kDefaultGlyphCacheSize;
621 return cacheSize;
622 #else
623 return kDefaultGlyphCacheSize;
624 #endif // MOZ_WIDGET_ANDROID
627 class WebRenderMemoryReporter final : public nsIMemoryReporter {
628 public:
629 NS_DECL_ISUPPORTS
630 NS_DECL_NSIMEMORYREPORTER
632 private:
633 ~WebRenderMemoryReporter() = default;
636 // Memory reporter for WebRender.
638 // The reporting within WebRender is manual and incomplete. We could do a much
639 // more thorough job by depending on the malloc_size_of crate, but integrating
640 // that into WebRender is tricky [1].
642 // So the idea is to start with manual reporting for the large allocations
643 // detected by DMD, and see how much that can cover in practice (which may
644 // require a few rounds of iteration). If that approach turns out to be
645 // fundamentally insufficient, we can either duplicate more of the
646 // malloc_size_of functionality in WebRender, or deal with the complexity of a
647 // gecko-only crate dependency.
649 // [1] See https://bugzilla.mozilla.org/show_bug.cgi?id=1480293#c1
650 struct WebRenderMemoryReporterHelper {
651 WebRenderMemoryReporterHelper(nsIHandleReportCallback* aCallback,
652 nsISupports* aData)
653 : mCallback(aCallback), mData(aData) {}
654 nsCOMPtr<nsIHandleReportCallback> mCallback;
655 nsCOMPtr<nsISupports> mData;
657 void Report(size_t aBytes, const char* aName) const {
658 nsPrintfCString path("explicit/gfx/webrender/%s", aName);
659 nsCString desc("CPU heap memory used by WebRender"_ns);
660 ReportInternal(aBytes, path, desc, nsIMemoryReporter::KIND_HEAP);
663 void ReportTexture(size_t aBytes, const char* aName) const {
664 nsPrintfCString path("gfx/webrender/textures/%s", aName);
665 nsCString desc("GPU texture memory used by WebRender"_ns);
666 ReportInternal(aBytes, path, desc, nsIMemoryReporter::KIND_OTHER);
669 void ReportTotalGPUBytes(size_t aBytes) const {
670 nsCString path("gfx/webrender/total-gpu-bytes"_ns);
671 nsCString desc(nsLiteralCString(
672 "Total GPU bytes used by WebRender (should match textures/ sum)"));
673 ReportInternal(aBytes, path, desc, nsIMemoryReporter::KIND_OTHER);
676 void ReportInternal(size_t aBytes, nsACString& aPath, nsACString& aDesc,
677 int32_t aKind) const {
678 // Generally, memory reporters pass the empty string as the process name to
679 // indicate "current process". However, if we're using a GPU process, the
680 // measurements will actually take place in that process, and it's easier to
681 // just note that here rather than trying to invoke the memory reporter in
682 // the GPU process.
683 nsAutoCString processName;
684 if (gfxConfig::IsEnabled(Feature::GPU_PROCESS)) {
685 GPUParent::GetGPUProcessName(processName);
688 mCallback->Callback(processName, aPath, aKind,
689 nsIMemoryReporter::UNITS_BYTES, aBytes, aDesc, mData);
693 static void FinishAsyncMemoryReport() {
694 nsCOMPtr<nsIMemoryReporterManager> imgr =
695 do_GetService("@mozilla.org/memory-reporter-manager;1");
696 if (imgr) {
697 imgr->EndReport();
701 // clang-format off
702 // (For some reason, clang-format gets the second macro right, but totally mangles the first).
703 #define REPORT_INTERNER(id) \
704 helper.Report(aReport.interning.interners.id, \
705 "interning/" #id "/interners");
706 // clang-format on
708 #define REPORT_DATA_STORE(id) \
709 helper.Report(aReport.interning.data_stores.id, \
710 "interning/" #id "/data-stores");
712 NS_IMPL_ISUPPORTS(WebRenderMemoryReporter, nsIMemoryReporter)
714 NS_IMETHODIMP
715 WebRenderMemoryReporter::CollectReports(nsIHandleReportCallback* aHandleReport,
716 nsISupports* aData, bool aAnonymize) {
717 MOZ_ASSERT(XRE_IsParentProcess());
718 MOZ_ASSERT(NS_IsMainThread());
719 layers::CompositorManagerChild* manager =
720 CompositorManagerChild::GetInstance();
721 if (!manager) {
722 FinishAsyncMemoryReport();
723 return NS_OK;
726 WebRenderMemoryReporterHelper helper(aHandleReport, aData);
727 manager->SendReportMemory(
728 [=](wr::MemoryReport aReport) {
729 // CPU Memory.
730 helper.Report(aReport.clip_stores, "clip-stores");
731 helper.Report(aReport.gpu_cache_metadata, "gpu-cache/metadata");
732 helper.Report(aReport.gpu_cache_cpu_mirror, "gpu-cache/cpu-mirror");
733 helper.Report(aReport.render_tasks, "render-tasks");
734 helper.Report(aReport.hit_testers, "hit-testers");
735 helper.Report(aReport.fonts, "resource-cache/fonts");
736 helper.Report(aReport.weak_fonts, "resource-cache/weak-fonts");
737 helper.Report(aReport.images, "resource-cache/images");
738 helper.Report(aReport.rasterized_blobs,
739 "resource-cache/rasterized-blobs");
740 helper.Report(aReport.texture_cache_structures,
741 "texture-cache/structures");
742 helper.Report(aReport.shader_cache, "shader-cache");
743 helper.Report(aReport.display_list, "display-list");
744 helper.Report(aReport.swgl, "swgl");
745 helper.Report(aReport.upload_staging_memory, "upload-stagin-memory");
747 WEBRENDER_FOR_EACH_INTERNER(REPORT_INTERNER);
748 WEBRENDER_FOR_EACH_INTERNER(REPORT_DATA_STORE);
750 // GPU Memory.
751 helper.ReportTexture(aReport.gpu_cache_textures, "gpu-cache");
752 helper.ReportTexture(aReport.vertex_data_textures, "vertex-data");
753 helper.ReportTexture(aReport.render_target_textures, "render-targets");
754 helper.ReportTexture(aReport.depth_target_textures, "depth-targets");
755 helper.ReportTexture(aReport.picture_tile_textures, "picture-tiles");
756 helper.ReportTexture(aReport.atlas_textures, "texture-cache/atlas");
757 helper.ReportTexture(aReport.standalone_textures,
758 "texture-cache/standalone");
759 helper.ReportTexture(aReport.texture_upload_pbos,
760 "texture-upload-pbos");
761 helper.ReportTexture(aReport.swap_chain, "swap-chains");
762 helper.ReportTexture(aReport.render_texture_hosts,
763 "render-texture-hosts");
764 helper.ReportTexture(aReport.upload_staging_textures,
765 "upload-staging-textures");
767 FinishAsyncMemoryReport();
769 [](mozilla::ipc::ResponseRejectReason&& aReason) {
770 FinishAsyncMemoryReport();
773 return NS_OK;
776 #undef REPORT_INTERNER
777 #undef REPORT_DATA_STORE
779 std::atomic<int8_t> gfxPlatform::sHasVariationFontSupport = -1;
781 bool gfxPlatform::HasVariationFontSupport() {
782 // We record the status here: 0 for not supported, 1 for supported.
783 if (sHasVariationFontSupport < 0) {
784 // It doesn't actually matter if we race with another thread setting this,
785 // as any thread will set it to the same value.
786 #if defined(XP_WIN)
787 sHasVariationFontSupport = gfxWindowsPlatform::CheckVariationFontSupport();
788 #elif defined(XP_MACOSX)
789 sHasVariationFontSupport = gfxPlatformMac::CheckVariationFontSupport();
790 #elif defined(MOZ_WIDGET_GTK)
791 sHasVariationFontSupport = gfxPlatformGtk::CheckVariationFontSupport();
792 #elif defined(ANDROID)
793 sHasVariationFontSupport = gfxAndroidPlatform::CheckVariationFontSupport();
794 #else
795 # error "No gfxPlatform implementation available"
796 #endif
798 return sHasVariationFontSupport > 0;
801 void gfxPlatform::Init() {
802 MOZ_RELEASE_ASSERT(!XRE_IsGPUProcess(), "GFX: Not allowed in GPU process.");
803 MOZ_RELEASE_ASSERT(!XRE_IsRDDProcess(), "GFX: Not allowed in RDD process.");
804 MOZ_RELEASE_ASSERT(NS_IsMainThread(), "GFX: Not in main thread.");
806 if (gEverInitialized) {
807 MOZ_CRASH("Already started???");
809 gEverInitialized = true;
811 gfxVars::Initialize();
813 gfxConfig::Init();
815 if (XRE_IsParentProcess()) {
816 GPUProcessManager::Initialize();
817 RDDProcessManager::Initialize();
819 nsCOMPtr<nsIFile> file;
820 nsresult rv = NS_GetSpecialDirectory(NS_GRE_DIR, getter_AddRefs(file));
821 if (NS_FAILED(rv)) {
822 gfxVars::SetGREDirectory(nsString());
823 } else {
824 nsAutoString path;
825 file->GetPath(path);
826 gfxVars::SetGREDirectory(nsString(path));
830 if (XRE_IsParentProcess()) {
831 nsCOMPtr<nsIFile> profDir;
832 nsresult rv = NS_GetSpecialDirectory(NS_APP_PROFILE_DIR_STARTUP,
833 getter_AddRefs(profDir));
834 if (NS_FAILED(rv)) {
835 gfxVars::SetProfDirectory(nsString());
836 } else {
837 nsAutoString path;
838 profDir->GetPath(path);
839 gfxVars::SetProfDirectory(nsString(path));
842 nsAutoCString path;
843 Preferences::GetCString("layers.windowrecording.path", path);
844 gfxVars::SetLayersWindowRecordingPath(path);
846 if (gFxREmbedded) {
847 gfxVars::SetFxREmbedded(true);
851 // Drop a note in the crash report if we end up forcing an option that could
852 // destabilize things. New items should be appended at the end (of an
853 // existing or in a new section), so that we don't have to know the version to
854 // interpret these cryptic strings.
856 nsAutoCString forcedPrefs;
857 // D2D prefs
858 forcedPrefs.AppendPrintf(
859 "FP(D%d%d", StaticPrefs::gfx_direct2d_disabled_AtStartup(),
860 StaticPrefs::gfx_direct2d_force_enabled_AtStartup());
861 // Layers prefs
862 forcedPrefs.AppendPrintf(
863 "-L%d%d%d%d",
864 StaticPrefs::layers_amd_switchable_gfx_enabled_AtStartup(),
865 StaticPrefs::layers_acceleration_disabled_AtStartup_DoNotUseDirectly(),
866 StaticPrefs::
867 layers_acceleration_force_enabled_AtStartup_DoNotUseDirectly(),
868 StaticPrefs::layers_d3d11_force_warp_AtStartup());
869 // WebGL prefs
870 forcedPrefs.AppendPrintf(
871 "-W%d%d%d%d%d%d%d", StaticPrefs::webgl_angle_force_d3d11(),
872 StaticPrefs::webgl_angle_force_warp(), StaticPrefs::webgl_disabled(),
873 StaticPrefs::webgl_disable_angle(), StaticPrefs::webgl_dxgl_enabled(),
874 StaticPrefs::webgl_force_enabled(), StaticPrefs::webgl_msaa_force());
875 // Prefs that don't fit into any of the other sections
876 forcedPrefs.AppendPrintf("-T%d%d%d) ",
877 StaticPrefs::gfx_android_rgb16_force_AtStartup(),
878 StaticPrefs::gfx_canvas_accelerated(),
879 StaticPrefs::layers_force_shmem_tiles_AtStartup());
880 ScopedGfxFeatureReporter::AppNote(forcedPrefs);
883 InitMoz2DLogging();
885 /* Initialize the GfxInfo service.
886 * Note: we can't call functions on GfxInfo that depend
887 * on gPlatform until after it has been initialized
888 * below. GfxInfo initialization annotates our
889 * crash reports so we want to do it before
890 * we try to load any drivers and do device detection
891 * incase that code crashes. See bug #591561. */
892 nsCOMPtr<nsIGfxInfo> gfxInfo;
893 /* this currently will only succeed on Windows */
894 gfxInfo = components::GfxInfo::Service();
896 if (XRE_IsParentProcess()) {
897 // Some gfxVars must be initialized prior gPlatform for coherent results.
898 gfxVars::SetDXInterop2Blocked(IsDXInterop2Blocked());
899 gfxVars::SetDXNV12Blocked(IsDXNV12Blocked());
900 gfxVars::SetDXP010Blocked(IsDXP010Blocked());
901 gfxVars::SetDXP016Blocked(IsDXP016Blocked());
904 #if defined(XP_WIN)
905 gPlatform = new gfxWindowsPlatform;
906 #elif defined(XP_MACOSX)
907 gPlatform = new gfxPlatformMac;
908 #elif defined(MOZ_WIDGET_GTK)
909 gPlatform = new gfxPlatformGtk;
910 #elif defined(ANDROID)
911 gPlatform = new gfxAndroidPlatform;
912 #else
913 # error "No gfxPlatform implementation available"
914 #endif
915 gPlatform->PopulateScreenInfo();
916 gPlatform->InitAcceleration();
917 gPlatform->InitWebRenderConfig();
919 gPlatform->InitHardwareVideoConfig();
920 gPlatform->InitWebGLConfig();
921 gPlatform->InitWebGPUConfig();
922 gPlatform->InitWindowOcclusionConfig();
923 gPlatform->InitBackdropFilterConfig();
924 gPlatform->InitAcceleratedCanvas2DConfig();
926 #if defined(XP_WIN)
927 // When using WebRender, we defer initialization of the D3D11 devices until
928 // the (rare) cases where they're used. Note that the GPU process where
929 // WebRender runs doesn't initialize gfxPlatform and performs explicit
930 // initialization of the bits it needs.
931 if (XRE_IsParentProcess() && !gfxConfig::IsEnabled(Feature::GPU_PROCESS) &&
932 StaticPrefs::
933 gfx_webrender_enabled_no_gpu_process_with_angle_win_AtStartup()) {
934 gPlatform->EnsureDevicesInitialized();
936 #endif
938 if (gfxConfig::IsEnabled(Feature::GPU_PROCESS)) {
939 GPUProcessManager* gpu = GPUProcessManager::Get();
940 Unused << gpu->LaunchGPUProcess();
943 if (XRE_IsParentProcess()) {
944 // Create the global vsync source and dispatcher.
945 RefPtr<VsyncSource> vsyncSource =
946 gfxPlatform::ForceSoftwareVsync()
947 ? gPlatform->GetSoftwareVsyncSource()
948 : gPlatform->GetGlobalHardwareVsyncSource();
949 gPlatform->mVsyncDispatcher = new VsyncDispatcher(vsyncSource);
951 // Listen for layout.frame_rate pref changes.
952 Preferences::RegisterCallback(
953 gfxPlatform::ReInitFrameRate,
954 nsDependentCString(StaticPrefs::GetPrefName_layout_frame_rate()));
955 Preferences::RegisterCallback(
956 gfxPlatform::ReInitFrameRate,
957 nsDependentCString(
958 StaticPrefs::GetPrefName_privacy_resistFingerprinting()));
961 // Create the sRGB to output display profile transforms. They can be accessed
962 // off the main thread so we want to avoid a race condition.
963 InitializeCMS();
965 SkGraphics::Init();
966 #ifdef MOZ_ENABLE_FREETYPE
967 SkInitCairoFT(gPlatform->FontHintingEnabled());
968 #endif
969 gfxGradientCache::Init();
971 InitLayersIPC();
973 // This *create* the platform font list instance, but may not *initialize* it
974 // yet if the gfx.font-list.lazy-init.enabled pref is set. The first *use*
975 // of the list will ensure it is initialized.
976 if (!gPlatform->CreatePlatformFontList()) {
977 MOZ_CRASH("Could not initialize gfxPlatformFontList");
980 gPlatform->mScreenReferenceDrawTarget =
981 gPlatform->CreateOffscreenContentDrawTarget(IntSize(1, 1),
982 SurfaceFormat::B8G8R8A8);
983 if (!gPlatform->mScreenReferenceDrawTarget ||
984 !gPlatform->mScreenReferenceDrawTarget->IsValid()) {
985 // If TDR is detected, create a draw target with software backend
986 // and it should be replaced later when the process gets the device
987 // reset notification.
988 if (!gPlatform->DidRenderingDeviceReset()) {
989 gfxCriticalError() << "Could not initialize mScreenReferenceDrawTarget";
993 if (NS_FAILED(gfxFontCache::Init())) {
994 MOZ_CRASH("Could not initialize gfxFontCache");
997 Preferences::RegisterPrefixCallbacks(FontPrefChanged, kObservedPrefs);
999 GLContext::PlatformStartup();
1001 // Listen to memory pressure event so we can purge DrawTarget caches
1002 gPlatform->mMemoryPressureObserver =
1003 layers::MemoryPressureObserver::Create(gPlatform);
1005 // Request the imgITools service, implicitly initializing ImageLib.
1006 nsCOMPtr<imgITools> imgTools = do_GetService("@mozilla.org/image/tools;1");
1007 if (!imgTools) {
1008 MOZ_CRASH("Could not initialize ImageLib");
1011 RegisterStrongMemoryReporter(new GfxMemoryImageReporter());
1012 if (XRE_IsParentProcess()) {
1013 RegisterStrongAsyncMemoryReporter(new WebRenderMemoryReporter());
1016 RegisterStrongMemoryReporter(new SkMemoryReporter());
1018 uint32_t skiaCacheSize = GetSkiaGlyphCacheSize();
1019 if (skiaCacheSize != kDefaultGlyphCacheSize) {
1020 SkGraphics::SetFontCacheLimit(skiaCacheSize);
1023 InitNullMetadata();
1024 InitOpenGLConfig();
1026 if (XRE_IsParentProcess()) {
1027 Preferences::Unlock(FONT_VARIATIONS_PREF);
1028 if (!gfxPlatform::HasVariationFontSupport()) {
1029 // Ensure variation fonts are disabled and the pref is locked.
1030 Preferences::SetBool(FONT_VARIATIONS_PREF, false, PrefValueKind::Default);
1031 Preferences::SetBool(FONT_VARIATIONS_PREF, false);
1032 Preferences::Lock(FONT_VARIATIONS_PREF);
1036 if (XRE_IsParentProcess()) {
1037 ReportTelemetry();
1040 nsCOMPtr<nsIObserverService> obs = services::GetObserverService();
1041 if (obs) {
1042 obs->NotifyObservers(nullptr, "gfx-features-ready", nullptr);
1046 void gfxPlatform::ReportTelemetry() {
1047 MOZ_RELEASE_ASSERT(XRE_IsParentProcess(),
1048 "GFX: Only allowed to be called from parent process.");
1050 nsCOMPtr<nsIGfxInfo> gfxInfo = components::GfxInfo::Service();
1053 auto& screenManager = widget::ScreenManager::GetSingleton();
1054 const uint32_t screenCount = screenManager.CurrentScreenList().Length();
1055 RefPtr<widget::Screen> primaryScreen = screenManager.GetPrimaryScreen();
1056 const LayoutDeviceIntRect rect = primaryScreen->GetRect();
1058 Telemetry::ScalarSet(Telemetry::ScalarID::GFX_DISPLAY_COUNT, screenCount);
1059 Telemetry::ScalarSet(Telemetry::ScalarID::GFX_DISPLAY_PRIMARY_HEIGHT,
1060 uint32_t(rect.Height()));
1061 Telemetry::ScalarSet(Telemetry::ScalarID::GFX_DISPLAY_PRIMARY_WIDTH,
1062 uint32_t(rect.Width()));
1065 nsString adapterDesc;
1066 gfxInfo->GetAdapterDescription(adapterDesc);
1067 Telemetry::ScalarSet(Telemetry::ScalarID::GFX_ADAPTER_DESCRIPTION,
1068 adapterDesc);
1070 nsString adapterVendorId;
1071 gfxInfo->GetAdapterVendorID(adapterVendorId);
1072 Telemetry::ScalarSet(Telemetry::ScalarID::GFX_ADAPTER_VENDOR_ID,
1073 adapterVendorId);
1075 nsString adapterDeviceId;
1076 gfxInfo->GetAdapterDeviceID(adapterDeviceId);
1077 Telemetry::ScalarSet(Telemetry::ScalarID::GFX_ADAPTER_DEVICE_ID,
1078 adapterDeviceId);
1080 nsString adapterSubsystemId;
1081 gfxInfo->GetAdapterSubsysID(adapterSubsystemId);
1082 Telemetry::ScalarSet(Telemetry::ScalarID::GFX_ADAPTER_SUBSYSTEM_ID,
1083 adapterSubsystemId);
1085 uint32_t adapterRam = 0;
1086 gfxInfo->GetAdapterRAM(&adapterRam);
1087 Telemetry::ScalarSet(Telemetry::ScalarID::GFX_ADAPTER_RAM, adapterRam);
1089 nsString adapterDriver;
1090 gfxInfo->GetAdapterDriver(adapterDriver);
1091 Telemetry::ScalarSet(Telemetry::ScalarID::GFX_ADAPTER_DRIVER_FILES,
1092 adapterDriver);
1094 nsString adapterDriverVendor;
1095 gfxInfo->GetAdapterDriverVendor(adapterDriverVendor);
1096 Telemetry::ScalarSet(Telemetry::ScalarID::GFX_ADAPTER_DRIVER_VENDOR,
1097 adapterDriverVendor);
1099 nsString adapterDriverVersion;
1100 gfxInfo->GetAdapterDriverVersion(adapterDriverVersion);
1101 Telemetry::ScalarSet(Telemetry::ScalarID::GFX_ADAPTER_DRIVER_VERSION,
1102 adapterDriverVersion);
1104 nsString adapterDriverDate;
1105 gfxInfo->GetAdapterDriverDate(adapterDriverDate);
1106 Telemetry::ScalarSet(Telemetry::ScalarID::GFX_ADAPTER_DRIVER_DATE,
1107 adapterDriverDate);
1109 Telemetry::ScalarSet(Telemetry::ScalarID::GFX_HEADLESS, IsHeadless());
1111 MOZ_ASSERT(gPlatform, "Need gPlatform to generate some telemetry.");
1112 Telemetry::ScalarSet(Telemetry::ScalarID::GFX_SUPPORTS_HDR,
1113 gPlatform->SupportsHDR());
1116 static bool IsFeatureSupported(long aFeature, bool aDefault) {
1117 nsCOMPtr<nsIGfxInfo> gfxInfo = components::GfxInfo::Service();
1118 nsCString blockId;
1119 int32_t status;
1120 if (!NS_SUCCEEDED(gfxInfo->GetFeatureStatus(aFeature, blockId, &status))) {
1121 return aDefault;
1123 return status == nsIGfxInfo::FEATURE_STATUS_OK;
1126 /* static*/
1127 bool gfxPlatform::IsDXInterop2Blocked() {
1128 return !IsFeatureSupported(nsIGfxInfo::FEATURE_DX_INTEROP2, false);
1131 /* static*/
1132 bool gfxPlatform::IsDXNV12Blocked() {
1133 return !IsFeatureSupported(nsIGfxInfo::FEATURE_DX_NV12, false);
1136 /* static*/
1137 bool gfxPlatform::IsDXP010Blocked() {
1138 return !IsFeatureSupported(nsIGfxInfo::FEATURE_DX_P010, false);
1141 /* static*/
1142 bool gfxPlatform::IsDXP016Blocked() {
1143 return !IsFeatureSupported(nsIGfxInfo::FEATURE_DX_P016, false);
1146 /* static */
1147 int32_t gfxPlatform::MaxTextureSize() {
1148 // Make sure we don't completely break rendering because of a typo in the
1149 // pref or whatnot.
1150 const int32_t kMinSizePref = 2048;
1151 return std::max(
1152 kMinSizePref,
1153 StaticPrefs::gfx_max_texture_size_AtStartup_DoNotUseDirectly());
1156 /* static */
1157 int32_t gfxPlatform::MaxAllocSize() {
1158 // Make sure we don't completely break rendering because of a typo in the
1159 // pref or whatnot.
1160 const int32_t kMinAllocPref = 10000000;
1161 return std::max(kMinAllocPref,
1162 StaticPrefs::gfx_max_alloc_size_AtStartup_DoNotUseDirectly());
1165 /* static */
1166 void gfxPlatform::InitMoz2DLogging() {
1167 auto fwd = new CrashStatsLogForwarder(
1168 CrashReporter::Annotation::GraphicsCriticalError);
1169 fwd->SetCircularBufferSize(StaticPrefs::gfx_logging_crash_length_AtStartup());
1171 mozilla::gfx::Config cfg;
1172 cfg.mLogForwarder = fwd;
1173 cfg.mMaxTextureSize = gfxPlatform::MaxTextureSize();
1174 cfg.mMaxAllocSize = gfxPlatform::MaxAllocSize();
1176 gfx::Factory::Init(cfg);
1179 /* static */
1180 bool gfxPlatform::IsHeadless() {
1181 static bool initialized = false;
1182 static bool headless = false;
1183 if (!initialized) {
1184 initialized = true;
1185 headless = PR_GetEnv("MOZ_HEADLESS");
1187 return headless;
1190 /* static */
1191 bool gfxPlatform::UseRemoteCanvas() {
1192 return XRE_IsContentProcess() && gfx::gfxVars::RemoteCanvasEnabled();
1195 /* static */
1196 bool gfxPlatform::IsBackendAccelerated(
1197 const mozilla::gfx::BackendType aBackendType) {
1198 return aBackendType == BackendType::DIRECT2D ||
1199 aBackendType == BackendType::DIRECT2D1_1;
1202 /* static */
1203 bool gfxPlatform::CanMigrateMacGPUs() {
1204 int32_t pMigration = StaticPrefs::gfx_compositor_gpu_migration();
1206 bool forceDisable = pMigration == 0;
1207 bool forceEnable = pMigration == 2;
1209 return forceEnable || !forceDisable;
1212 static bool sLayersIPCIsUp = false;
1214 /* static */
1215 void gfxPlatform::InitNullMetadata() {
1216 ScrollMetadata::sNullMetadata = new ScrollMetadata();
1217 ClearOnShutdown(&ScrollMetadata::sNullMetadata);
1220 void gfxPlatform::Shutdown() {
1221 // In some cases, gPlatform may not be created but Shutdown() called,
1222 // e.g., during xpcshell tests.
1223 if (!gPlatform) {
1224 return;
1227 MOZ_ASSERT(!sLayersIPCIsUp);
1229 // These may be called before the corresponding subsystems have actually
1230 // started up. That's OK, they can handle it.
1231 gfxFontCache::Shutdown();
1232 gfxGradientCache::Shutdown();
1233 gfxAlphaBoxBlur::ShutdownBlurCache();
1234 gfxGraphiteShaper::Shutdown();
1235 gfxPlatformFontList::Shutdown();
1236 gfxFontMissingGlyphs::Shutdown();
1238 // Free the various non-null transforms and loaded profiles
1239 ShutdownCMS();
1241 Preferences::UnregisterPrefixCallbacks(FontPrefChanged, kObservedPrefs);
1243 NS_ASSERTION(gPlatform->mMemoryPressureObserver,
1244 "mMemoryPressureObserver has already gone");
1245 if (gPlatform->mMemoryPressureObserver) {
1246 gPlatform->mMemoryPressureObserver->Unregister();
1247 gPlatform->mMemoryPressureObserver = nullptr;
1250 if (XRE_IsParentProcess()) {
1251 if (gPlatform->mGlobalHardwareVsyncSource) {
1252 gPlatform->mGlobalHardwareVsyncSource->Shutdown();
1254 if (gPlatform->mSoftwareVsyncSource &&
1255 gPlatform->mSoftwareVsyncSource !=
1256 gPlatform->mGlobalHardwareVsyncSource) {
1257 gPlatform->mSoftwareVsyncSource->Shutdown();
1261 gPlatform->mGlobalHardwareVsyncSource = nullptr;
1262 gPlatform->mSoftwareVsyncSource = nullptr;
1263 gPlatform->mVsyncDispatcher = nullptr;
1265 // Shut down the default GL context provider.
1266 GLContextProvider::Shutdown();
1268 #if defined(XP_WIN)
1269 // The above shutdown calls operate on the available context providers on
1270 // most platforms. Windows is a "special snowflake", though, and has three
1271 // context providers available, so we have to shut all of them down.
1272 // We should only support the default GL provider on Windows; then, this
1273 // could go away. Unfortunately, we currently support WGL (the default) for
1274 // WebGL on Optimus.
1275 GLContextProviderEGL::Shutdown();
1276 #endif
1278 if (XRE_IsParentProcess()) {
1279 GPUProcessManager::Shutdown();
1280 VRProcessManager::Shutdown();
1281 RDDProcessManager::Shutdown();
1284 gfx::Factory::ShutDown();
1285 gfxVars::Shutdown();
1286 gfxFont::DestroySingletons();
1288 gfxConfig::Shutdown();
1290 gPlatform->WillShutdown();
1292 delete gPlatform;
1293 gPlatform = nullptr;
1296 /* static */
1297 void gfxPlatform::InitLayersIPC() {
1298 if (sLayersIPCIsUp) {
1299 return;
1301 sLayersIPCIsUp = true;
1303 if (XRE_IsParentProcess()) {
1304 #if defined(XP_WIN)
1305 if (gfxConfig::IsEnabled(gfx::Feature::WINDOW_OCCLUSION)) {
1306 widget::WinWindowOcclusionTracker::Ensure();
1308 #endif
1309 if (!gfxConfig::IsEnabled(Feature::GPU_PROCESS)) {
1310 RemoteTextureMap::Init();
1311 wr::RenderThread::Start(GPUProcessManager::Get()->AllocateNamespace());
1312 image::ImageMemoryReporter::InitForWebRender();
1315 layers::CompositorThreadHolder::Start();
1317 if (!gfxConfig::IsEnabled(Feature::GPU_PROCESS)) {
1318 gfx::CanvasRenderThread::Start();
1323 /* static */
1324 void gfxPlatform::ShutdownLayersIPC() {
1325 if (!sLayersIPCIsUp) {
1326 return;
1328 sLayersIPCIsUp = false;
1330 if (XRE_IsContentProcess()) {
1331 gfx::VRManagerChild::ShutDown();
1332 gfx::CanvasManagerChild::Shutdown();
1333 // cf bug 1215265.
1334 if (StaticPrefs::layers_child_process_shutdown()) {
1335 layers::CompositorManagerChild::Shutdown();
1336 layers::ImageBridgeChild::ShutDown();
1339 } else if (XRE_IsParentProcess()) {
1340 VideoBridgeParent::Shutdown();
1341 RDDProcessManager::RDDProcessShutdown();
1342 gfx::VRManagerChild::ShutDown();
1343 gfx::CanvasManagerChild::Shutdown();
1344 layers::CompositorManagerChild::Shutdown();
1345 layers::ImageBridgeChild::ShutDown();
1346 // This could be running on either the Compositor thread, the Renderer
1347 // thread, or the dedicated CanvasRender thread, so we need to shutdown
1348 // before the former two.
1349 gfx::CanvasRenderThread::Shutdown();
1350 // This has to happen after shutting down the child protocols.
1351 layers::CompositorThreadHolder::Shutdown();
1352 RemoteTextureMap::Shutdown();
1353 image::ImageMemoryReporter::ShutdownForWebRender();
1354 // There is a case that RenderThread exists when UseWebRender() is
1355 // false. This could happen when WebRender was fallbacked to compositor.
1356 if (wr::RenderThread::Get()) {
1357 wr::RenderThread::ShutDown();
1359 Preferences::UnregisterCallback(WebRenderDebugPrefChangeCallback,
1360 WR_DEBUG_PREF);
1361 Preferences::UnregisterCallback(WebRendeProfilerUIPrefChangeCallback,
1362 "gfx.webrender.debug.profiler-ui");
1363 Preferences::UnregisterCallback(
1364 WebRenderBlobTileSizePrefChangeCallback,
1365 nsDependentCString(
1366 StaticPrefs::GetPrefName_gfx_webrender_blob_tile_size()));
1368 #if defined(XP_WIN)
1369 widget::WinWindowOcclusionTracker::ShutDown();
1370 #endif
1371 } else {
1372 // TODO: There are other kind of processes and we should make sure gfx
1373 // stuff is either not created there or shut down properly.
1377 void gfxPlatform::WillShutdown() {
1378 // Destoy these first in case they depend on backend-specific resources.
1379 // Otherwise, the backend's destructor would be called before the
1380 // base gfxPlatform destructor.
1381 mScreenReferenceSurface = nullptr;
1382 mScreenReferenceDrawTarget = nullptr;
1384 // Always clear out the Skia font cache here, in case it is referencing any
1385 // SharedFTFaces that would otherwise outlive destruction of the FT_Library
1386 // that owns them.
1387 SkGraphics::PurgeFontCache();
1389 // The cairo folks think we should only clean up in debug builds,
1390 // but we're generally in the habit of trying to shut down as
1391 // cleanly as possible even in production code, so call this
1392 // cairo_debug_* function unconditionally.
1394 // because cairo can assert and thus crash on shutdown, don't do this in
1395 // release builds
1396 #ifdef NS_FREE_PERMANENT_DATA
1397 cairo_debug_reset_static_data();
1398 #endif
1401 gfxPlatform::~gfxPlatform() = default;
1403 /* static */
1404 already_AddRefed<DrawTarget> gfxPlatform::CreateDrawTargetForSurface(
1405 gfxASurface* aSurface, const IntSize& aSize) {
1406 SurfaceFormat format = aSurface->GetSurfaceFormat();
1407 RefPtr<DrawTarget> drawTarget = Factory::CreateDrawTargetForCairoSurface(
1408 aSurface->CairoSurface(), aSize, &format);
1409 if (!drawTarget) {
1410 gfxWarning() << "gfxPlatform::CreateDrawTargetForSurface failed in "
1411 "CreateDrawTargetForCairoSurface";
1412 return nullptr;
1414 return drawTarget.forget();
1417 cairo_user_data_key_t kSourceSurface;
1420 * Record the backend that was used to construct the SourceSurface.
1421 * When getting the cached SourceSurface for a gfxASurface/DrawTarget pair,
1422 * we check to make sure the DrawTarget's backend matches the backend
1423 * for the cached SourceSurface, and only use it if they match. This
1424 * can avoid expensive and unnecessary readbacks.
1426 struct SourceSurfaceUserData {
1427 RefPtr<SourceSurface> mSrcSurface;
1428 BackendType mBackendType;
1431 static void SourceBufferDestroy(void* srcSurfUD) {
1432 delete static_cast<SourceSurfaceUserData*>(srcSurfUD);
1435 UserDataKey kThebesSurface;
1437 struct DependentSourceSurfaceUserData {
1438 RefPtr<gfxASurface> mSurface;
1441 static void SourceSurfaceDestroyed(void* aData) {
1442 delete static_cast<DependentSourceSurfaceUserData*>(aData);
1445 void gfxPlatform::ClearSourceSurfaceForSurface(gfxASurface* aSurface) {
1446 aSurface->SetData(&kSourceSurface, nullptr, nullptr);
1449 /* static */
1450 already_AddRefed<SourceSurface> gfxPlatform::GetSourceSurfaceForSurface(
1451 RefPtr<DrawTarget> aTarget, gfxASurface* aSurface, bool aIsPlugin) {
1452 if (!aSurface->CairoSurface() || aSurface->CairoStatus()) {
1453 return nullptr;
1456 if (!aTarget) {
1457 aTarget = gfxPlatform::GetPlatform()->ScreenReferenceDrawTarget();
1460 void* userData = aSurface->GetData(&kSourceSurface);
1462 if (userData) {
1463 SourceSurfaceUserData* surf = static_cast<SourceSurfaceUserData*>(userData);
1465 if (surf->mSrcSurface->IsValid() &&
1466 surf->mBackendType == aTarget->GetBackendType()) {
1467 RefPtr<SourceSurface> srcSurface(surf->mSrcSurface);
1468 return srcSurface.forget();
1470 // We can just continue here as when setting new user data the destroy
1471 // function will be called for the old user data.
1474 SurfaceFormat format = aSurface->GetSurfaceFormat();
1476 if (aTarget->GetBackendType() == BackendType::CAIRO) {
1477 // If we're going to be used with a CAIRO DrawTarget, then just create a
1478 // SourceSurfaceCairo since we don't know the underlying type of the CAIRO
1479 // DrawTarget and can't pick a better surface type. Doing this also avoids
1480 // readback of aSurface's surface into memory if, for example, aSurface
1481 // wraps an xlib cairo surface (which can be important to avoid a major
1482 // slowdown).
1484 // We return here regardless of whether CreateSourceSurfaceFromNativeSurface
1485 // succeeds or not since we don't expect to be able to do any better below
1486 // if it fails.
1488 // Note that the returned SourceSurfaceCairo holds a strong reference to
1489 // the cairo_surface_t* that it wraps, which essencially means it holds a
1490 // strong reference to aSurface since aSurface shares its
1491 // cairo_surface_t*'s reference count variable. As a result we can't cache
1492 // srcBuffer on aSurface (see below) since aSurface would then hold a
1493 // strong reference back to srcBuffer, creating a reference loop and a
1494 // memory leak. Not caching is fine since wrapping is cheap enough (no
1495 // copying) so we can just wrap again next time we're called.
1496 return Factory::CreateSourceSurfaceForCairoSurface(
1497 aSurface->CairoSurface(), aSurface->GetSize(), format);
1500 RefPtr<SourceSurface> srcBuffer;
1502 // Currently no other DrawTarget types implement
1503 // CreateSourceSurfaceFromNativeSurface
1505 if (!srcBuffer) {
1506 // If aSurface wraps data, we can create a SourceSurfaceRawData that wraps
1507 // the same data, then optimize it for aTarget:
1508 RefPtr<DataSourceSurface> surf = GetWrappedDataSourceSurface(aSurface);
1509 if (surf) {
1510 srcBuffer = aIsPlugin
1511 ? aTarget->OptimizeSourceSurfaceForUnknownAlpha(surf)
1512 : aTarget->OptimizeSourceSurface(surf);
1514 if (srcBuffer == surf) {
1515 // GetWrappedDataSourceSurface returns a SourceSurface that holds a
1516 // strong reference to aSurface since it wraps aSurface's data and
1517 // needs it to stay alive. As a result we can't cache srcBuffer on
1518 // aSurface (below) since aSurface would then hold a strong reference
1519 // back to srcBuffer, creating a reference loop and a memory leak. Not
1520 // caching is fine since wrapping is cheap enough (no copying) so we
1521 // can just wrap again next time we're called.
1523 // Note that the check below doesn't catch this since srcBuffer will be
1524 // a SourceSurfaceRawData object (even if aSurface is not a
1525 // gfxImageSurface object), which is why we need this separate check.
1526 return srcBuffer.forget();
1531 if (!srcBuffer) {
1532 MOZ_ASSERT(aTarget->GetBackendType() != BackendType::CAIRO,
1533 "We already tried CreateSourceSurfaceFromNativeSurface with a "
1534 "DrawTargetCairo above");
1535 // We've run out of performant options. We now try creating a SourceSurface
1536 // using a temporary DrawTargetCairo and then optimizing it to aTarget's
1537 // actual type. The CreateSourceSurfaceFromNativeSurface() call will
1538 // likely create a DataSourceSurface (possibly involving copying and/or
1539 // readback), and the OptimizeSourceSurface may well copy again and upload
1540 // to the GPU. So, while this code path is rarely hit, hitting it may be
1541 // very slow.
1542 srcBuffer = Factory::CreateSourceSurfaceForCairoSurface(
1543 aSurface->CairoSurface(), aSurface->GetSize(), format);
1544 if (srcBuffer) {
1545 srcBuffer = aTarget->OptimizeSourceSurface(srcBuffer);
1549 if (!srcBuffer) {
1550 return nullptr;
1553 if ((srcBuffer->GetType() == SurfaceType::CAIRO &&
1554 static_cast<SourceSurfaceCairo*>(srcBuffer.get())->GetSurface() ==
1555 aSurface->CairoSurface()) ||
1556 (srcBuffer->GetType() == SurfaceType::CAIRO_IMAGE &&
1557 static_cast<DataSourceSurfaceCairo*>(srcBuffer.get())->GetSurface() ==
1558 aSurface->CairoSurface())) {
1559 // See the "Note that the returned SourceSurfaceCairo..." comment above.
1560 return srcBuffer.forget();
1563 // Add user data to aSurface so we can cache lookups in the future.
1564 auto* srcSurfUD = new SourceSurfaceUserData;
1565 srcSurfUD->mBackendType = aTarget->GetBackendType();
1566 srcSurfUD->mSrcSurface = srcBuffer;
1567 aSurface->SetData(&kSourceSurface, srcSurfUD, SourceBufferDestroy);
1569 return srcBuffer.forget();
1572 already_AddRefed<DataSourceSurface> gfxPlatform::GetWrappedDataSourceSurface(
1573 gfxASurface* aSurface) {
1574 RefPtr<gfxImageSurface> image = aSurface->GetAsImageSurface();
1575 if (!image) {
1576 return nullptr;
1578 RefPtr<DataSourceSurface> result = Factory::CreateWrappingDataSourceSurface(
1579 image->Data(), image->Stride(), image->GetSize(),
1580 ImageFormatToSurfaceFormat(image->Format()));
1582 if (!result) {
1583 return nullptr;
1586 // If we wrapped the underlying data of aSurface, then we need to add user
1587 // data to make sure aSurface stays alive until we are done with the data.
1588 auto* srcSurfUD = new DependentSourceSurfaceUserData;
1589 srcSurfUD->mSurface = aSurface;
1590 result->AddUserData(&kThebesSurface, srcSurfUD, SourceSurfaceDestroyed);
1592 return result.forget();
1595 void gfxPlatform::PopulateScreenInfo() {
1596 nsCOMPtr<nsIScreenManager> manager =
1597 do_GetService("@mozilla.org/gfx/screenmanager;1");
1598 MOZ_ASSERT(manager, "failed to get nsIScreenManager");
1600 nsCOMPtr<nsIScreen> screen;
1601 manager->GetPrimaryScreen(getter_AddRefs(screen));
1602 if (!screen) {
1603 // This can happen in xpcshell, for instance
1604 return;
1607 screen->GetColorDepth(&mScreenDepth);
1608 if (XRE_IsParentProcess()) {
1609 gfxVars::SetScreenDepth(mScreenDepth);
1612 int left, top;
1613 screen->GetRect(&left, &top, &mScreenSize.width, &mScreenSize.height);
1616 bool gfxPlatform::SupportsAzureContentForDrawTarget(DrawTarget* aTarget) {
1617 if (!aTarget || !aTarget->IsValid()) {
1618 return false;
1621 return SupportsAzureContentForType(aTarget->GetBackendType());
1624 void gfxPlatform::PurgeSkiaFontCache() {
1625 if (gfxPlatform::GetPlatform()->GetDefaultContentBackend() ==
1626 BackendType::SKIA) {
1627 SkGraphics::PurgeFontCache();
1631 already_AddRefed<DrawTarget> gfxPlatform::CreateDrawTargetForBackend(
1632 BackendType aBackend, const IntSize& aSize, SurfaceFormat aFormat) {
1633 // There is a bunch of knowledge in the gfxPlatform heirarchy about how to
1634 // create the best offscreen surface for the current system and situation. We
1635 // can easily take advantage of this for the Cairo backend, so that's what we
1636 // do.
1637 // mozilla::gfx::Factory can get away without having all this knowledge for
1638 // now, but this might need to change in the future (using
1639 // CreateOffscreenSurface() and CreateDrawTargetForSurface() for all
1640 // backends).
1641 if (aBackend == BackendType::CAIRO) {
1642 RefPtr<gfxASurface> surf =
1643 CreateOffscreenSurface(aSize, SurfaceFormatToImageFormat(aFormat));
1644 if (!surf || surf->CairoStatus()) {
1645 return nullptr;
1647 return CreateDrawTargetForSurface(surf, aSize);
1649 return Factory::CreateDrawTarget(aBackend, aSize, aFormat);
1652 already_AddRefed<DrawTarget> gfxPlatform::CreateOffscreenCanvasDrawTarget(
1653 const IntSize& aSize, SurfaceFormat aFormat) {
1654 NS_ASSERTION(mPreferredCanvasBackend != BackendType::NONE, "No backend.");
1656 // If we are using remote canvas we don't want to use acceleration in
1657 // canvas DrawTargets we are not remoting, so we always use the fallback
1658 // software one.
1659 if (!gfxPlatform::UseRemoteCanvas() ||
1660 !gfxPlatform::IsBackendAccelerated(mPreferredCanvasBackend)) {
1661 RefPtr<DrawTarget> target =
1662 CreateDrawTargetForBackend(mPreferredCanvasBackend, aSize, aFormat);
1663 if (target || mFallbackCanvasBackend == BackendType::NONE) {
1664 return target.forget();
1668 #ifdef XP_WIN
1669 // On Windows, the fallback backend (Cairo) should use its image backend.
1670 return Factory::CreateDrawTarget(mFallbackCanvasBackend, aSize, aFormat);
1671 #else
1672 return CreateDrawTargetForBackend(mFallbackCanvasBackend, aSize, aFormat);
1673 #endif
1676 already_AddRefed<DrawTarget> gfxPlatform::CreateOffscreenContentDrawTarget(
1677 const IntSize& aSize, SurfaceFormat aFormat, bool aFallback) {
1678 BackendType backend = (aFallback) ? mSoftwareBackend : mContentBackend;
1679 NS_ASSERTION(backend != BackendType::NONE, "No backend.");
1680 RefPtr<DrawTarget> dt = CreateDrawTargetForBackend(backend, aSize, aFormat);
1682 if (!dt) {
1683 return nullptr;
1686 // We'd prefer this to take proper care and return a CaptureDT, but for the
1687 // moment since we can't and this means we're going to be drawing on the main
1688 // thread force it's initialization. See bug 1526045 and bug 1521368.
1689 dt->ClearRect(gfx::Rect());
1690 if (!dt->IsValid()) {
1691 return nullptr;
1693 return dt.forget();
1696 already_AddRefed<DrawTarget> gfxPlatform::CreateSimilarSoftwareDrawTarget(
1697 DrawTarget* aDT, const IntSize& aSize, SurfaceFormat aFormat) {
1698 RefPtr<DrawTarget> dt;
1700 if (Factory::DoesBackendSupportDataDrawtarget(aDT->GetBackendType())) {
1701 dt = aDT->CreateSimilarDrawTarget(aSize, aFormat);
1702 } else {
1703 BackendType backendType = BackendType::SKIA;
1704 dt = Factory::CreateDrawTarget(backendType, aSize, aFormat);
1707 return dt.forget();
1710 /* static */
1711 already_AddRefed<DrawTarget> gfxPlatform::CreateDrawTargetForData(
1712 unsigned char* aData, const IntSize& aSize, int32_t aStride,
1713 SurfaceFormat aFormat, bool aUninitialized) {
1714 BackendType backendType = gfxVars::ContentBackend();
1715 NS_ASSERTION(backendType != BackendType::NONE, "No backend.");
1717 if (!Factory::DoesBackendSupportDataDrawtarget(backendType)) {
1718 backendType = BackendType::SKIA;
1721 RefPtr<DrawTarget> dt = Factory::CreateDrawTargetForData(
1722 backendType, aData, aSize, aStride, aFormat, aUninitialized);
1724 return dt.forget();
1727 /* static */
1728 BackendType gfxPlatform::BackendTypeForName(const nsCString& aName) {
1729 if (aName.EqualsLiteral("cairo")) return BackendType::CAIRO;
1730 if (aName.EqualsLiteral("skia")) return BackendType::SKIA;
1731 if (aName.EqualsLiteral("direct2d")) return BackendType::DIRECT2D;
1732 if (aName.EqualsLiteral("direct2d1.1")) return BackendType::DIRECT2D1_1;
1733 return BackendType::NONE;
1736 nsresult gfxPlatform::GetFontList(nsAtom* aLangGroup,
1737 const nsACString& aGenericFamily,
1738 nsTArray<nsString>& aListOfFonts) {
1739 gfxPlatformFontList::PlatformFontList()->GetFontList(
1740 aLangGroup, aGenericFamily, aListOfFonts);
1741 return NS_OK;
1744 nsresult gfxPlatform::UpdateFontList(bool aFullRebuild) {
1745 gfxPlatformFontList::PlatformFontList()->UpdateFontList(aFullRebuild);
1746 return NS_OK;
1749 void gfxPlatform::GetStandardFamilyName(const nsCString& aFontName,
1750 nsACString& aFamilyName) {
1751 gfxPlatformFontList::PlatformFontList()->GetStandardFamilyName(aFontName,
1752 aFamilyName);
1755 nsAutoCString gfxPlatform::GetDefaultFontName(
1756 const nsACString& aLangGroup, const nsACString& aGenericFamily) {
1757 // To benefit from Return Value Optimization, all paths here must return
1758 // this one variable:
1759 nsAutoCString result;
1761 auto* pfl = gfxPlatformFontList::PlatformFontList();
1762 FamilyAndGeneric fam = pfl->GetDefaultFontFamily(aLangGroup, aGenericFamily);
1763 if (!pfl->GetLocalizedFamilyName(fam.mFamily, result)) {
1764 NS_WARNING("missing default font-family name");
1767 return result;
1770 bool gfxPlatform::DownloadableFontsEnabled() {
1771 if (mAllowDownloadableFonts == UNINITIALIZED_VALUE) {
1772 mAllowDownloadableFonts =
1773 Preferences::GetBool(GFX_DOWNLOADABLE_FONTS_ENABLED, false);
1776 return mAllowDownloadableFonts;
1779 bool gfxPlatform::UseCmapsDuringSystemFallback() {
1780 return StaticPrefs::gfx_font_rendering_fallback_always_use_cmaps();
1783 bool gfxPlatform::OpenTypeSVGEnabled() {
1784 return StaticPrefs::gfx_font_rendering_opentype_svg_enabled();
1787 uint32_t gfxPlatform::WordCacheCharLimit() {
1788 return StaticPrefs::gfx_font_rendering_wordcache_charlimit();
1791 uint32_t gfxPlatform::WordCacheMaxEntries() {
1792 return StaticPrefs::gfx_font_rendering_wordcache_maxentries();
1795 bool gfxPlatform::UseGraphiteShaping() {
1796 return StaticPrefs::gfx_font_rendering_graphite_enabled();
1799 bool gfxPlatform::IsFontFormatSupported(
1800 StyleFontFaceSourceFormatKeyword aFormatHint,
1801 StyleFontFaceSourceTechFlags aTechFlags) {
1802 // By default, font resources are assumed to be supported; but if the format
1803 // hint or technology flags explicitly indicate something we don't support,
1804 // then return false.
1805 switch (aFormatHint) {
1806 case StyleFontFaceSourceFormatKeyword::None:
1807 break;
1808 case StyleFontFaceSourceFormatKeyword::Collection:
1809 return false;
1810 case StyleFontFaceSourceFormatKeyword::Opentype:
1811 case StyleFontFaceSourceFormatKeyword::Truetype:
1812 break;
1813 case StyleFontFaceSourceFormatKeyword::EmbeddedOpentype:
1814 return false;
1815 case StyleFontFaceSourceFormatKeyword::Svg:
1816 return false;
1817 case StyleFontFaceSourceFormatKeyword::Woff:
1818 break;
1819 case StyleFontFaceSourceFormatKeyword::Woff2:
1820 break;
1821 case StyleFontFaceSourceFormatKeyword::Unknown:
1822 return false;
1823 default:
1824 MOZ_ASSERT_UNREACHABLE("bad format hint!");
1825 return false;
1827 StyleFontFaceSourceTechFlags unsupportedTechnologies =
1828 StyleFontFaceSourceTechFlags::INCREMENTAL |
1829 StyleFontFaceSourceTechFlags::COLOR_SBIX;
1830 if (!StaticPrefs::gfx_downloadable_fonts_keep_color_bitmaps()) {
1831 unsupportedTechnologies |= StyleFontFaceSourceTechFlags::COLOR_CBDT;
1833 if (!StaticPrefs::gfx_font_rendering_colr_v1_enabled()) {
1834 unsupportedTechnologies |= StyleFontFaceSourceTechFlags::COLOR_COLRV1;
1836 if (!StaticPrefs::layout_css_font_palette_enabled()) {
1837 unsupportedTechnologies |= StyleFontFaceSourceTechFlags::PALETTES;
1839 if (!StaticPrefs::layout_css_font_variations_enabled()) {
1840 unsupportedTechnologies |= StyleFontFaceSourceTechFlags::VARIATIONS;
1842 if (aTechFlags & unsupportedTechnologies) {
1843 return false;
1845 return true;
1848 bool gfxPlatform::IsKnownIconFontFamily(const nsAtom* aFamilyName) const {
1849 return gfxPlatformFontList::PlatformFontList()->IsKnownIconFontFamily(
1850 aFamilyName);
1853 gfxFontEntry* gfxPlatform::LookupLocalFont(nsPresContext* aPresContext,
1854 const nsACString& aFontName,
1855 WeightRange aWeightForEntry,
1856 StretchRange aStretchForEntry,
1857 SlantStyleRange aStyleForEntry) {
1858 return gfxPlatformFontList::PlatformFontList()->LookupLocalFont(
1859 aPresContext, aFontName, aWeightForEntry, aStretchForEntry,
1860 aStyleForEntry);
1863 gfxFontEntry* gfxPlatform::MakePlatformFont(const nsACString& aFontName,
1864 WeightRange aWeightForEntry,
1865 StretchRange aStretchForEntry,
1866 SlantStyleRange aStyleForEntry,
1867 const uint8_t* aFontData,
1868 uint32_t aLength) {
1869 return gfxPlatformFontList::PlatformFontList()->MakePlatformFont(
1870 aFontName, aWeightForEntry, aStretchForEntry, aStyleForEntry, aFontData,
1871 aLength);
1874 BackendPrefsData gfxPlatform::GetBackendPrefs() const {
1875 BackendPrefsData data;
1877 data.mCanvasBitmask = BackendTypeBit(BackendType::SKIA);
1878 data.mContentBitmask = BackendTypeBit(BackendType::SKIA);
1880 #ifdef MOZ_WIDGET_GTK
1881 data.mCanvasBitmask |= BackendTypeBit(BackendType::CAIRO);
1882 data.mContentBitmask |= BackendTypeBit(BackendType::CAIRO);
1883 #endif
1885 data.mCanvasDefault = BackendType::SKIA;
1886 data.mContentDefault = BackendType::SKIA;
1888 return data;
1891 void gfxPlatform::InitBackendPrefs(BackendPrefsData&& aPrefsData) {
1892 mPreferredCanvasBackend = GetCanvasBackendPref(aPrefsData.mCanvasBitmask);
1893 if (mPreferredCanvasBackend == BackendType::NONE) {
1894 mPreferredCanvasBackend = aPrefsData.mCanvasDefault;
1897 if (mPreferredCanvasBackend == BackendType::DIRECT2D1_1) {
1898 // Falling back to D2D 1.0 won't help us here. When D2D 1.1 DT creation
1899 // fails it means the surface was too big or there's something wrong with
1900 // the device. D2D 1.0 will encounter a similar situation.
1901 mFallbackCanvasBackend = GetCanvasBackendPref(
1902 aPrefsData.mCanvasBitmask & ~(BackendTypeBit(mPreferredCanvasBackend) |
1903 BackendTypeBit(BackendType::DIRECT2D)));
1904 } else {
1905 mFallbackCanvasBackend = GetCanvasBackendPref(
1906 aPrefsData.mCanvasBitmask & ~BackendTypeBit(mPreferredCanvasBackend));
1909 mContentBackendBitmask = aPrefsData.mContentBitmask;
1910 mContentBackend = GetContentBackendPref(mContentBackendBitmask);
1911 if (mContentBackend == BackendType::NONE) {
1912 mContentBackend = aPrefsData.mContentDefault;
1913 // mContentBackendBitmask is our canonical reference for supported
1914 // backends so we need to add the default if we are using it and
1915 // overriding the prefs.
1916 mContentBackendBitmask |= BackendTypeBit(aPrefsData.mContentDefault);
1919 uint32_t swBackendBits = BackendTypeBit(BackendType::SKIA);
1920 #ifdef MOZ_WIDGET_GTK
1921 swBackendBits |= BackendTypeBit(BackendType::CAIRO);
1922 #endif
1923 mSoftwareBackend = GetContentBackendPref(swBackendBits);
1924 if (mSoftwareBackend == BackendType::NONE) {
1925 mSoftwareBackend = BackendType::SKIA;
1928 // If we don't have a fallback canvas backend then use the same software
1929 // fallback as content.
1930 if (mFallbackCanvasBackend == BackendType::NONE) {
1931 mFallbackCanvasBackend = mSoftwareBackend;
1934 if (XRE_IsParentProcess()) {
1935 gfxVars::SetContentBackend(mContentBackend);
1936 gfxVars::SetSoftwareBackend(mSoftwareBackend);
1940 /* static */
1941 BackendType gfxPlatform::GetCanvasBackendPref(uint32_t aBackendBitmask) {
1942 return GetBackendPref("gfx.canvas.azure.backends", aBackendBitmask);
1945 /* static */
1946 BackendType gfxPlatform::GetContentBackendPref(uint32_t& aBackendBitmask) {
1947 return GetBackendPref("gfx.content.azure.backends", aBackendBitmask);
1950 /* static */
1951 BackendType gfxPlatform::GetBackendPref(const char* aBackendPrefName,
1952 uint32_t& aBackendBitmask) {
1953 nsTArray<nsCString> backendList;
1954 nsAutoCString prefString;
1955 if (NS_SUCCEEDED(Preferences::GetCString(aBackendPrefName, prefString))) {
1956 ParseString(prefString, ',', backendList);
1959 uint32_t allowedBackends = 0;
1960 BackendType result = BackendType::NONE;
1961 for (uint32_t i = 0; i < backendList.Length(); ++i) {
1962 BackendType type = BackendTypeForName(backendList[i]);
1963 if (BackendTypeBit(type) & aBackendBitmask) {
1964 allowedBackends |= BackendTypeBit(type);
1965 if (result == BackendType::NONE) {
1966 result = type;
1971 aBackendBitmask = allowedBackends;
1972 return result;
1975 bool gfxPlatform::InSafeMode() {
1976 static bool sSafeModeInitialized = false;
1977 static bool sInSafeMode = false;
1979 if (!sSafeModeInitialized) {
1980 sSafeModeInitialized = true;
1981 nsCOMPtr<nsIXULRuntime> xr = do_GetService("@mozilla.org/xre/runtime;1");
1982 if (xr) {
1983 xr->GetInSafeMode(&sInSafeMode);
1986 return sInSafeMode;
1989 bool gfxPlatform::OffMainThreadCompositingEnabled() {
1990 return UsesOffMainThreadCompositing();
1993 void gfxPlatform::SetCMSModeOverride(CMSMode aMode) {
1994 MOZ_ASSERT(gCMSInitialized);
1995 gCMSMode = aMode;
1998 int gfxPlatform::GetRenderingIntent() {
1999 // StaticPrefList.yaml is using 0 as the default for the rendering
2000 // intent preference, based on that being the value for
2001 // QCMS_INTENT_DEFAULT. Assert here to catch if that ever
2002 // changes and we can then figure out what to do about it.
2003 MOZ_ASSERT(QCMS_INTENT_DEFAULT == 0);
2005 /* Try to query the pref system for a rendering intent. */
2006 int32_t pIntent = StaticPrefs::gfx_color_management_rendering_intent();
2007 if ((pIntent < QCMS_INTENT_MIN) || (pIntent > QCMS_INTENT_MAX)) {
2008 /* If the pref is out of range, use embedded profile. */
2009 pIntent = -1;
2011 return pIntent;
2014 DeviceColor gfxPlatform::TransformPixel(const sRGBColor& in,
2015 qcms_transform* transform) {
2016 if (transform) {
2017 /* we want the bytes in RGB order */
2018 #ifdef IS_LITTLE_ENDIAN
2019 /* ABGR puts the bytes in |RGBA| order on little endian */
2020 uint32_t packed = in.ToABGR();
2021 qcms_transform_data(transform, (uint8_t*)&packed, (uint8_t*)&packed, 1);
2022 auto out = DeviceColor::FromABGR(packed);
2023 #else
2024 /* ARGB puts the bytes in |ARGB| order on big endian */
2025 uint32_t packed = in.UnusualToARGB();
2026 /* add one to move past the alpha byte */
2027 qcms_transform_data(transform, (uint8_t*)&packed + 1, (uint8_t*)&packed + 1,
2029 auto out = DeviceColor::UnusualFromARGB(packed);
2030 #endif
2031 out.a = in.a;
2032 return out;
2034 return DeviceColor(in.r, in.g, in.b, in.a);
2037 nsTArray<uint8_t> gfxPlatform::GetPrefCMSOutputProfileData() {
2038 const auto mirror = StaticPrefs::gfx_color_management_display_profile();
2039 const auto fname = *mirror;
2040 if (fname == "") {
2041 return nsTArray<uint8_t>();
2044 void* mem = nullptr;
2045 size_t size = 0;
2046 qcms_data_from_path(fname.get(), &mem, &size);
2048 nsTArray<uint8_t> result;
2050 if (mem) {
2051 result.AppendElements(static_cast<uint8_t*>(mem), size);
2052 free(mem);
2055 return result;
2058 const mozilla::gfx::ContentDeviceData* gfxPlatform::GetInitContentDeviceData() {
2059 return gContentDeviceInitData;
2062 CMSMode GfxColorManagementMode() {
2063 const auto mode = StaticPrefs::gfx_color_management_mode();
2064 if (mode >= 0 && mode < UnderlyingValue(CMSMode::AllCount)) {
2065 return CMSMode(mode);
2067 return CMSMode::Off;
2070 void gfxPlatform::InitializeCMS() {
2071 if (gCMSInitialized) {
2072 return;
2075 if (XRE_IsGPUProcess()) {
2076 // Colors in the GPU process should already be managed, so we don't need to
2077 // perform color management there.
2078 gCMSInitialized = true;
2079 return;
2082 MOZ_DIAGNOSTIC_ASSERT(NS_IsMainThread(),
2083 "CMS should be initialized on the main thread");
2084 if (MOZ_UNLIKELY(!NS_IsMainThread())) {
2085 return;
2088 gCMSMode = GfxColorManagementMode();
2090 gCMSsRGBProfile = qcms_profile_sRGB();
2092 /* Determine if we're using the internal override to force sRGB as
2093 an output profile for reftests. See Bug 452125.
2095 Note that we don't normally (outside of tests) set a default value
2096 of this preference, which means nsIPrefBranch::GetBoolPref will
2097 typically throw (and leave its out-param untouched).
2099 if (StaticPrefs::gfx_color_management_force_srgb() ||
2100 StaticPrefs::gfx_color_management_native_srgb()) {
2101 gCMSOutputProfile = gCMSsRGBProfile;
2104 if (!gCMSOutputProfile) {
2105 nsTArray<uint8_t> outputProfileData =
2106 gfxPlatform::GetPlatform()->GetPlatformCMSOutputProfileData();
2107 if (!outputProfileData.IsEmpty()) {
2108 gCMSOutputProfile = qcms_profile_from_memory_curves_only(
2109 outputProfileData.Elements(), outputProfileData.Length());
2113 /* Determine if the profile looks bogus. If so, close the profile
2114 * and use sRGB instead. See bug 460629, */
2115 if (gCMSOutputProfile && qcms_profile_is_bogus(gCMSOutputProfile)) {
2116 NS_ASSERTION(gCMSOutputProfile != gCMSsRGBProfile,
2117 "Builtin sRGB profile tagged as bogus!!!");
2118 qcms_profile_release(gCMSOutputProfile);
2119 gCMSOutputProfile = nullptr;
2122 if (!gCMSOutputProfile) {
2123 gCMSOutputProfile = gCMSsRGBProfile;
2126 /* Precache the LUT16 Interpolations for the output profile. See
2127 bug 444661 for details. */
2128 qcms_profile_precache_output_transform(gCMSOutputProfile);
2130 // Create the RGB transform.
2131 gCMSRGBTransform =
2132 qcms_transform_create(gCMSsRGBProfile, QCMS_DATA_RGB_8, gCMSOutputProfile,
2133 QCMS_DATA_RGB_8, QCMS_INTENT_PERCEPTUAL);
2135 // And the inverse.
2136 gCMSInverseRGBTransform =
2137 qcms_transform_create(gCMSOutputProfile, QCMS_DATA_RGB_8, gCMSsRGBProfile,
2138 QCMS_DATA_RGB_8, QCMS_INTENT_PERCEPTUAL);
2140 // The RGBA transform.
2141 gCMSRGBATransform = qcms_transform_create(gCMSsRGBProfile, QCMS_DATA_RGBA_8,
2142 gCMSOutputProfile, QCMS_DATA_RGBA_8,
2143 QCMS_INTENT_PERCEPTUAL);
2145 // And the BGRA one.
2146 gCMSBGRATransform = qcms_transform_create(gCMSsRGBProfile, QCMS_DATA_BGRA_8,
2147 gCMSOutputProfile, QCMS_DATA_BGRA_8,
2148 QCMS_INTENT_PERCEPTUAL);
2150 // FIXME: We only enable iccv4 after we create the platform profile, to
2151 // wallpaper over bug 1697787.
2153 // This should happen ideally right after setting gCMSMode.
2154 if (StaticPrefs::gfx_color_management_enablev4()) {
2155 qcms_enable_iccv4();
2158 gCMSInitialized = true;
2161 qcms_transform* gfxPlatform::GetCMSOSRGBATransform() {
2162 switch (SurfaceFormat::OS_RGBA) {
2163 case SurfaceFormat::B8G8R8A8:
2164 return GetCMSBGRATransform();
2165 case SurfaceFormat::R8G8B8A8:
2166 return GetCMSRGBATransform();
2167 default:
2168 // We do not support color management with big endian.
2169 return nullptr;
2173 qcms_data_type gfxPlatform::GetCMSOSRGBAType() {
2174 switch (SurfaceFormat::OS_RGBA) {
2175 case SurfaceFormat::B8G8R8A8:
2176 return QCMS_DATA_BGRA_8;
2177 case SurfaceFormat::R8G8B8A8:
2178 return QCMS_DATA_RGBA_8;
2179 default:
2180 // We do not support color management with big endian.
2181 return QCMS_DATA_RGBA_8;
2185 /* Shuts down various transforms and profiles for CMS. */
2186 void gfxPlatform::ShutdownCMS() {
2187 if (gCMSRGBTransform) {
2188 qcms_transform_release(gCMSRGBTransform);
2189 gCMSRGBTransform = nullptr;
2191 if (gCMSInverseRGBTransform) {
2192 qcms_transform_release(gCMSInverseRGBTransform);
2193 gCMSInverseRGBTransform = nullptr;
2195 if (gCMSRGBATransform) {
2196 qcms_transform_release(gCMSRGBATransform);
2197 gCMSRGBATransform = nullptr;
2199 if (gCMSBGRATransform) {
2200 qcms_transform_release(gCMSBGRATransform);
2201 gCMSBGRATransform = nullptr;
2203 if (gCMSOutputProfile) {
2204 qcms_profile_release(gCMSOutputProfile);
2206 // handle the aliased case
2207 if (gCMSsRGBProfile == gCMSOutputProfile) {
2208 gCMSsRGBProfile = nullptr;
2210 gCMSOutputProfile = nullptr;
2212 if (gCMSsRGBProfile) {
2213 qcms_profile_release(gCMSsRGBProfile);
2214 gCMSsRGBProfile = nullptr;
2217 // Reset the state variables
2218 gCMSMode = CMSMode::Off;
2219 gCMSInitialized = false;
2222 uint32_t gfxPlatform::GetBidiNumeralOption() {
2223 return StaticPrefs::bidi_numeral();
2226 /* static */
2227 void gfxPlatform::FlushFontAndWordCaches() {
2228 gfxFontCache* fontCache = gfxFontCache::GetCache();
2229 if (fontCache) {
2230 fontCache->Flush();
2233 gfxPlatform::PurgeSkiaFontCache();
2236 /* static */
2237 void gfxPlatform::ForceGlobalReflow(NeedsReframe aNeedsReframe,
2238 BroadcastToChildren aBroadcastToChildren) {
2239 MOZ_ASSERT(NS_IsMainThread());
2240 const bool reframe = aNeedsReframe == NeedsReframe::Yes;
2241 // Send a notification that will be observed by PresShells in this process
2242 // only.
2243 if (nsCOMPtr<nsIObserverService> obs = services::GetObserverService()) {
2244 char16_t needsReframe[] = {char16_t(reframe), 0};
2245 obs->NotifyObservers(nullptr, "font-info-updated", needsReframe);
2247 if (XRE_IsParentProcess() &&
2248 aBroadcastToChildren == BroadcastToChildren::Yes) {
2249 // Propagate the change to child processes.
2250 for (auto* process :
2251 dom::ContentParent::AllProcesses(dom::ContentParent::eLive)) {
2252 Unused << process->SendForceGlobalReflow(reframe);
2257 void gfxPlatform::FontsPrefsChanged(const char* aPref) {
2258 NS_ASSERTION(aPref != nullptr, "null preference");
2259 if (!strcmp(GFX_DOWNLOADABLE_FONTS_ENABLED, aPref)) {
2260 mAllowDownloadableFonts = UNINITIALIZED_VALUE;
2261 } else if (!strcmp(GFX_PREF_WORD_CACHE_CHARLIMIT, aPref) ||
2262 !strcmp(GFX_PREF_WORD_CACHE_MAXENTRIES, aPref) ||
2263 !strcmp(GFX_PREF_GRAPHITE_SHAPING, aPref)) {
2264 FlushFontAndWordCaches();
2265 } else if (
2266 #if defined(XP_MACOSX)
2267 !strcmp(GFX_PREF_CORETEXT_SHAPING, aPref) ||
2268 #endif
2269 !strcmp("gfx.font_rendering.ahem_antialias_none", aPref)) {
2270 FlushFontAndWordCaches();
2271 } else if (!strcmp(GFX_PREF_OPENTYPE_SVG, aPref)) {
2272 gfxFontCache::GetCache()->Flush();
2273 gfxFontCache::GetCache()->NotifyGlyphsChanged();
2277 mozilla::LogModule* gfxPlatform::GetLog(eGfxLog aWhichLog) {
2278 // logs shared across gfx
2279 static LazyLogModule sFontlistLog("fontlist");
2280 static LazyLogModule sFontInitLog("fontinit");
2281 static LazyLogModule sTextrunLog("textrun");
2282 static LazyLogModule sTextrunuiLog("textrunui");
2283 static LazyLogModule sCmapDataLog("cmapdata");
2284 static LazyLogModule sTextPerfLog("textperf");
2286 switch (aWhichLog) {
2287 case eGfxLog_fontlist:
2288 return sFontlistLog;
2289 case eGfxLog_fontinit:
2290 return sFontInitLog;
2291 case eGfxLog_textrun:
2292 return sTextrunLog;
2293 case eGfxLog_textrunui:
2294 return sTextrunuiLog;
2295 case eGfxLog_cmapdata:
2296 return sCmapDataLog;
2297 case eGfxLog_textperf:
2298 return sTextPerfLog;
2301 MOZ_ASSERT_UNREACHABLE("Unexpected log type");
2302 return nullptr;
2305 RefPtr<mozilla::gfx::DrawTarget> gfxPlatform::ScreenReferenceDrawTarget() {
2306 MOZ_ASSERT_IF(XRE_IsContentProcess(), NS_IsMainThread());
2307 return (mScreenReferenceDrawTarget)
2308 ? mScreenReferenceDrawTarget
2309 : gPlatform->CreateOffscreenContentDrawTarget(
2310 IntSize(1, 1), SurfaceFormat::B8G8R8A8, true);
2313 /* static */ RefPtr<mozilla::gfx::DrawTarget>
2314 gfxPlatform::ThreadLocalScreenReferenceDrawTarget() {
2315 if (NS_IsMainThread() && gPlatform) {
2316 return gPlatform->ScreenReferenceDrawTarget();
2319 gfxPlatformWorker* platformWorker = gfxPlatformWorker::Get();
2320 if (platformWorker) {
2321 return platformWorker->ScreenReferenceDrawTarget();
2324 return Factory::CreateDrawTarget(BackendType::SKIA, IntSize(1, 1),
2325 SurfaceFormat::B8G8R8A8);
2328 mozilla::gfx::SurfaceFormat gfxPlatform::Optimal2DFormatForContent(
2329 gfxContentType aContent) {
2330 switch (aContent) {
2331 case gfxContentType::COLOR:
2332 switch (GetOffscreenFormat()) {
2333 case SurfaceFormat::A8R8G8B8_UINT32:
2334 return mozilla::gfx::SurfaceFormat::B8G8R8A8;
2335 case SurfaceFormat::X8R8G8B8_UINT32:
2336 return mozilla::gfx::SurfaceFormat::B8G8R8X8;
2337 case SurfaceFormat::R5G6B5_UINT16:
2338 return mozilla::gfx::SurfaceFormat::R5G6B5_UINT16;
2339 default:
2340 MOZ_ASSERT_UNREACHABLE(
2341 "unknown gfxImageFormat for "
2342 "gfxContentType::COLOR");
2343 return mozilla::gfx::SurfaceFormat::B8G8R8A8;
2345 case gfxContentType::ALPHA:
2346 return mozilla::gfx::SurfaceFormat::A8;
2347 case gfxContentType::COLOR_ALPHA:
2348 return mozilla::gfx::SurfaceFormat::B8G8R8A8;
2349 default:
2350 MOZ_ASSERT_UNREACHABLE("unknown gfxContentType");
2351 return mozilla::gfx::SurfaceFormat::B8G8R8A8;
2355 gfxImageFormat gfxPlatform::OptimalFormatForContent(gfxContentType aContent) {
2356 switch (aContent) {
2357 case gfxContentType::COLOR:
2358 return GetOffscreenFormat();
2359 case gfxContentType::ALPHA:
2360 return SurfaceFormat::A8;
2361 case gfxContentType::COLOR_ALPHA:
2362 return SurfaceFormat::A8R8G8B8_UINT32;
2363 default:
2364 MOZ_ASSERT_UNREACHABLE("unknown gfxContentType");
2365 return SurfaceFormat::A8R8G8B8_UINT32;
2370 * There are a number of layers acceleration (or layers in general) preferences
2371 * that should be consistent for the lifetime of the application (bug 840967).
2372 * As such, we will evaluate them all as soon as one of them is evaluated
2373 * and remember the values. Changing these preferences during the run will
2374 * not have any effect until we restart.
2376 static mozilla::Atomic<bool> sLayersSupportsHardwareVideoDecoding(false);
2377 static bool sLayersHardwareVideoDecodingFailed = false;
2379 static mozilla::Atomic<bool> sLayersAccelerationPrefsInitialized(false);
2381 static void VideoDecodingFailedChangedCallback(const char* aPref, void*) {
2382 sLayersHardwareVideoDecodingFailed = Preferences::GetBool(aPref, false);
2383 gfxPlatform::GetPlatform()->UpdateCanUseHardwareVideoDecoding();
2386 void gfxPlatform::UpdateCanUseHardwareVideoDecoding() {
2387 if (XRE_IsParentProcess()) {
2388 gfxVars::SetCanUseHardwareVideoDecoding(CanUseHardwareVideoDecoding());
2392 void gfxPlatform::UpdateForceSubpixelAAWherePossible() {
2393 bool forceSubpixelAAWherePossible =
2394 StaticPrefs::gfx_webrender_quality_force_subpixel_aa_where_possible();
2395 gfxVars::SetForceSubpixelAAWherePossible(forceSubpixelAAWherePossible);
2398 void gfxPlatform::InitAcceleration() {
2399 if (sLayersAccelerationPrefsInitialized) {
2400 return;
2403 InitCompositorAccelerationPrefs();
2405 // If this is called for the first time on a non-main thread, we're screwed.
2406 // At the moment there's no explicit guarantee that the main thread calls
2407 // this before the compositor thread, but let's at least make the assumption
2408 // explicit.
2409 MOZ_ASSERT(NS_IsMainThread(), "can only initialize prefs on the main thread");
2411 #ifndef MOZ_WIDGET_GTK
2412 nsCOMPtr<nsIGfxInfo> gfxInfo = components::GfxInfo::Service();
2413 nsCString discardFailureId;
2414 int32_t status;
2415 #endif
2417 if (XRE_IsParentProcess()) {
2418 gfxVars::SetBrowserTabsRemoteAutostart(BrowserTabsRemoteAutostart());
2419 gfxVars::SetOffscreenFormat(GetOffscreenFormat());
2420 gfxVars::SetRequiresAcceleratedGLContextForCompositorOGL(
2421 RequiresAcceleratedGLContextForCompositorOGL());
2422 #ifdef XP_WIN
2423 if (NS_SUCCEEDED(
2424 gfxInfo->GetFeatureStatus(nsIGfxInfo::FEATURE_D3D11_KEYED_MUTEX,
2425 discardFailureId, &status))) {
2426 gfxVars::SetAllowD3D11KeyedMutex(status == nsIGfxInfo::FEATURE_STATUS_OK);
2427 } else {
2428 // If we couldn't properly evaluate the status, err on the side
2429 // of caution and give this functionality to the user.
2430 gfxCriticalNote << "Cannot evaluate keyed mutex feature status";
2431 gfxVars::SetAllowD3D11KeyedMutex(true);
2433 if (StaticPrefs::gfx_direct3d11_use_double_buffering()) {
2434 gfxVars::SetUseDoubleBufferingWithCompositor(true);
2436 #endif
2439 if (StaticPrefs::media_hardware_video_decoding_enabled_AtStartup()) {
2440 #ifdef MOZ_WIDGET_GTK
2441 sLayersSupportsHardwareVideoDecoding =
2442 gfxPlatformGtk::GetPlatform()->InitVAAPIConfig(
2443 StaticPrefs::
2444 media_hardware_video_decoding_force_enabled_AtStartup() ||
2445 StaticPrefs::media_ffmpeg_vaapi_enabled_AtStartup());
2446 #else
2447 if (
2448 # ifdef XP_WIN
2449 Preferences::GetBool("media.wmf.dxva.enabled", true) &&
2450 # endif
2451 NS_SUCCEEDED(gfxInfo->GetFeatureStatus(
2452 nsIGfxInfo::FEATURE_HARDWARE_VIDEO_DECODING, discardFailureId,
2453 &status))) {
2454 if (status == nsIGfxInfo::FEATURE_STATUS_OK ||
2455 StaticPrefs::
2456 media_hardware_video_decoding_force_enabled_AtStartup()) {
2457 sLayersSupportsHardwareVideoDecoding = true;
2460 #endif
2461 } else if (XRE_IsParentProcess()) {
2462 FeatureState& feature =
2463 gfxConfig::GetFeature(Feature::HARDWARE_VIDEO_DECODING);
2464 feature.EnableByDefault();
2465 feature.UserDisable("User disabled via pref",
2466 "FEATURE_HARDWARE_VIDEO_DECODING_PREF_DISABLED"_ns);
2469 sLayersAccelerationPrefsInitialized = true;
2471 if (XRE_IsParentProcess()) {
2472 Preferences::RegisterCallbackAndCall(
2473 VideoDecodingFailedChangedCallback,
2474 "media.hardware-video-decoding.failed");
2475 InitGPUProcessPrefs();
2477 FeatureState& feature = gfxConfig::GetFeature(Feature::REMOTE_CANVAS);
2478 feature.SetDefault(StaticPrefs::gfx_canvas_remote_AtStartup(),
2479 FeatureStatus::Disabled, "Disabled via pref");
2481 if (!gfxConfig::IsEnabled(Feature::GPU_PROCESS) &&
2482 !StaticPrefs::gfx_canvas_remote_allow_in_parent_AtStartup()) {
2483 feature.Disable(FeatureStatus::UnavailableNoGpuProcess,
2484 "Disabled without GPU process",
2485 "FEATURE_REMOTE_CANVAS_NO_GPU_PROCESS"_ns);
2488 #ifndef XP_WIN
2489 gfxConfig::ForceDisable(Feature::REMOTE_CANVAS, FeatureStatus::Blocked,
2490 "Platform not supported",
2491 "FEATURE_REMOTE_CANVAS_NOT_WINDOWS"_ns);
2492 #endif
2494 gfxVars::SetRemoteCanvasEnabled(feature.IsEnabled());
2498 void gfxPlatform::InitGPUProcessPrefs() {
2499 // We want to hide this from about:support, so only set a default if the
2500 // pref is known to be true.
2501 if (!StaticPrefs::layers_gpu_process_enabled_AtStartup() &&
2502 !StaticPrefs::layers_gpu_process_force_enabled_AtStartup()) {
2503 return;
2506 FeatureState& gpuProc = gfxConfig::GetFeature(Feature::GPU_PROCESS);
2508 // We require E10S - otherwise, there is very little benefit to the GPU
2509 // process, since the UI process must still use acceleration for
2510 // performance.
2511 if (!BrowserTabsRemoteAutostart()) {
2512 gpuProc.DisableByDefault(FeatureStatus::Unavailable,
2513 "Multi-process mode is not enabled",
2514 "FEATURE_FAILURE_NO_E10S"_ns);
2515 } else {
2516 gpuProc.SetDefaultFromPref(
2517 StaticPrefs::GetPrefName_layers_gpu_process_enabled(), true,
2518 StaticPrefs::GetPrefDefault_layers_gpu_process_enabled());
2521 if (StaticPrefs::layers_gpu_process_force_enabled_AtStartup()) {
2522 gpuProc.UserForceEnable("User force-enabled via pref");
2525 nsCString message;
2526 nsCString failureId;
2527 if (!gfxPlatform::IsGfxInfoStatusOkay(nsIGfxInfo::FEATURE_GPU_PROCESS,
2528 &message, failureId)) {
2529 gpuProc.Disable(FeatureStatus::Blocklisted, message.get(), failureId);
2530 return;
2533 if (IsHeadless()) {
2534 gpuProc.ForceDisable(FeatureStatus::Blocked, "Headless mode is enabled",
2535 "FEATURE_FAILURE_HEADLESS_MODE"_ns);
2536 return;
2538 if (InSafeMode()) {
2539 gpuProc.ForceDisable(FeatureStatus::Blocked, "Safe-mode is enabled",
2540 "FEATURE_FAILURE_SAFE_MODE"_ns);
2541 return;
2544 InitPlatformGPUProcessPrefs();
2547 void gfxPlatform::InitCompositorAccelerationPrefs() {
2548 const char* acceleratedEnv = PR_GetEnv("MOZ_ACCELERATED");
2550 FeatureState& feature = gfxConfig::GetFeature(Feature::HW_COMPOSITING);
2552 // Base value - does the platform allow acceleration?
2553 if (feature.SetDefault(AccelerateLayersByDefault(), FeatureStatus::Blocked,
2554 "Acceleration blocked by platform")) {
2555 if (StaticPrefs::
2556 layers_acceleration_disabled_AtStartup_DoNotUseDirectly()) {
2557 feature.UserDisable("Disabled by layers.acceleration.disabled=true",
2558 "FEATURE_FAILURE_COMP_PREF"_ns);
2559 } else if (acceleratedEnv && *acceleratedEnv == '0') {
2560 feature.UserDisable("Disabled by envvar", "FEATURE_FAILURE_COMP_ENV"_ns);
2562 } else {
2563 if (acceleratedEnv && *acceleratedEnv == '1') {
2564 feature.UserEnable("Enabled by envvar");
2568 // This has specific meaning elsewhere, so we always record it.
2569 if (StaticPrefs::
2570 layers_acceleration_force_enabled_AtStartup_DoNotUseDirectly()) {
2571 feature.UserForceEnable("Force-enabled by pref");
2574 // Safe, headless, and record/replay modes override everything.
2575 if (InSafeMode()) {
2576 feature.ForceDisable(FeatureStatus::Blocked,
2577 "Acceleration blocked by safe-mode",
2578 "FEATURE_FAILURE_COMP_SAFEMODE"_ns);
2580 if (IsHeadless()) {
2581 feature.ForceDisable(FeatureStatus::Blocked,
2582 "Acceleration blocked by headless mode",
2583 "FEATURE_FAILURE_COMP_HEADLESSMODE"_ns);
2587 /*static*/
2588 bool gfxPlatform::WebRenderPrefEnabled() {
2589 return StaticPrefs::gfx_webrender_all_AtStartup();
2592 /*static*/
2593 bool gfxPlatform::WebRenderEnvvarEnabled() {
2594 const char* env = PR_GetEnv("MOZ_WEBRENDER");
2595 return (env && *env == '1');
2598 /* static */ const char* gfxPlatform::WebRenderResourcePathOverride() {
2599 const char* resourcePath = PR_GetEnv("WR_RESOURCE_PATH");
2600 if (!resourcePath || resourcePath[0] == '\0') {
2601 return nullptr;
2603 return resourcePath;
2606 void gfxPlatform::InitWebRenderConfig() {
2607 bool prefEnabled = WebRenderPrefEnabled();
2608 bool envvarEnabled = WebRenderEnvvarEnabled();
2610 // WR? WR+ => means WR was enabled on qualified hardware
2611 // WR! WR+ => means WR was enabled via gfx.webrender.{all,enabled} or
2612 // envvar, possibly on unqualified hardware
2613 // In all cases WR- means WR was not enabled, for one of many possible
2614 // reasons. Prior to bug 1523788 landing the gfx.webrender.{all,enabled}
2615 // prefs only worked on Nightly so keep that in mind when looking at older
2616 // crash reports.
2617 ScopedGfxFeatureReporter reporter("WR", prefEnabled || envvarEnabled);
2618 if (!XRE_IsParentProcess()) {
2619 // The parent process runs through all the real decision-making code
2620 // later in this function. For other processes we still want to report
2621 // the state of the feature for crash reports.
2622 reporter.SetSuccessful();
2623 return;
2626 // Update the gfxConfig feature states.
2627 gfxConfigManager manager;
2628 manager.Init();
2629 manager.ConfigureWebRender();
2631 bool hasHardware = gfxConfig::IsEnabled(Feature::WEBRENDER);
2633 #ifdef MOZ_WIDGET_GTK
2634 // We require a hardware driver to back the GL context unless the user forced
2635 // on WebRender.
2636 if (!gfxConfig::IsForcedOnByUser(Feature::WEBRENDER) &&
2637 StaticPrefs::gfx_webrender_reject_software_driver_AtStartup()) {
2638 gfxVars::SetWebRenderRequiresHardwareDriver(true);
2640 #endif
2642 #ifdef XP_WIN
2643 if (gfxConfig::IsEnabled(Feature::WEBRENDER_ANGLE)) {
2644 gfxVars::SetUseWebRenderANGLE(true);
2646 #endif
2648 if (gfxConfig::IsEnabled(Feature::WEBRENDER_SHADER_CACHE)) {
2649 gfxVars::SetUseWebRenderProgramBinaryDisk(true);
2652 gfxVars::SetUseWebRenderOptimizedShaders(
2653 gfxConfig::IsEnabled(Feature::WEBRENDER_OPTIMIZED_SHADERS));
2655 gfxVars::SetUseSoftwareWebRender(!hasHardware);
2657 Preferences::RegisterPrefixCallbackAndCall(SwapIntervalPrefChangeCallback,
2658 "gfx.swap-interval");
2660 reporter.SetSuccessful();
2662 Preferences::RegisterPrefixCallbackAndCall(WebRenderDebugPrefChangeCallback,
2663 WR_DEBUG_PREF);
2665 RegisterWebRenderBoolParamCallback();
2667 Preferences::RegisterPrefixCallbackAndCall(
2668 WebRendeProfilerUIPrefChangeCallback, "gfx.webrender.debug.profiler-ui");
2669 Preferences::RegisterCallback(
2670 WebRenderQualityPrefChangeCallback,
2671 nsDependentCString(
2672 StaticPrefs::
2673 GetPrefName_gfx_webrender_quality_force_subpixel_aa_where_possible()));
2675 Preferences::RegisterCallback(
2676 WebRenderBatchingPrefChangeCallback,
2677 nsDependentCString(
2678 StaticPrefs::GetPrefName_gfx_webrender_batching_lookback()));
2680 Preferences::RegisterCallbackAndCall(
2681 WebRenderBlobTileSizePrefChangeCallback,
2682 nsDependentCString(
2683 StaticPrefs::GetPrefName_gfx_webrender_blob_tile_size()));
2685 Preferences::RegisterCallbackAndCall(
2686 WebRenderUploadThresholdPrefChangeCallback,
2687 nsDependentCString(
2688 StaticPrefs::GetPrefName_gfx_webrender_batched_upload_threshold()));
2690 if (WebRenderResourcePathOverride()) {
2691 CrashReporter::AnnotateCrashReport(
2692 CrashReporter::Annotation::IsWebRenderResourcePathOverridden, true);
2695 UpdateForceSubpixelAAWherePossible();
2697 #if defined(MOZ_WIDGET_ANDROID) || defined(MOZ_WIDGET_GTK)
2698 if (StaticPrefs::gfx_webrender_software_opengl_AtStartup()) {
2699 gfxVars::SetAllowSoftwareWebRenderOGL(true);
2701 #endif
2703 #ifdef XP_WIN
2704 if (gfxConfig::IsEnabled(Feature::WEBRENDER_DCOMP_PRESENT)) {
2705 gfxVars::SetUseWebRenderDCompWin(true);
2707 if (StaticPrefs::gfx_webrender_software_d3d11_AtStartup()) {
2708 gfxVars::SetAllowSoftwareWebRenderD3D11(true);
2711 const bool overlaySupported =
2712 IsWin10AnniversaryUpdateOrLater() &&
2713 gfxConfig::IsEnabled(Feature::WEBRENDER_COMPOSITOR);
2714 MOZ_ASSERT_IF(overlaySupported,
2715 gfxConfig::IsEnabled(Feature::WEBRENDER_DCOMP_PRESENT));
2717 bool useVideoHwOverlay = false;
2718 if (StaticPrefs::gfx_webrender_dcomp_video_hw_overlay_win_AtStartup()) {
2719 if (overlaySupported) {
2720 useVideoHwOverlay = true;
2723 if (useVideoHwOverlay &&
2724 !StaticPrefs::
2725 gfx_webrender_dcomp_video_hw_overlay_win_force_enabled_AtStartup()) {
2726 nsCString failureId;
2727 int32_t status;
2728 const nsCOMPtr<nsIGfxInfo> gfxInfo = components::GfxInfo::Service();
2729 if (NS_FAILED(gfxInfo->GetFeatureStatus(nsIGfxInfo::FEATURE_VIDEO_OVERLAY,
2730 failureId, &status))) {
2731 FeatureState& feature =
2732 gfxConfig::GetFeature(Feature::VIDEO_HARDWARE_OVERLAY);
2733 feature.DisableByDefault(FeatureStatus::BlockedNoGfxInfo,
2734 "gfxInfo is broken",
2735 "FEATURE_FAILURE_WR_NO_GFX_INFO"_ns);
2736 useVideoHwOverlay = false;
2737 } else {
2738 if (status != nsIGfxInfo::FEATURE_ALLOW_ALWAYS) {
2739 FeatureState& feature =
2740 gfxConfig::GetFeature(Feature::VIDEO_HARDWARE_OVERLAY);
2741 feature.DisableByDefault(FeatureStatus::Blocked,
2742 "Blocklisted by gfxInfo", failureId);
2743 useVideoHwOverlay = false;
2747 } else if (overlaySupported) {
2748 FeatureState& feature =
2749 gfxConfig::GetFeature(Feature::VIDEO_HARDWARE_OVERLAY);
2750 feature.DisableByDefault(FeatureStatus::Blocked, "Disabled by pref",
2751 "FEATURE_FAILURE_DISABLED_BY_PREF"_ns);
2754 if (useVideoHwOverlay) {
2755 FeatureState& feature =
2756 gfxConfig::GetFeature(Feature::VIDEO_HARDWARE_OVERLAY);
2757 feature.EnableByDefault();
2758 gfxVars::SetUseWebRenderDCompVideoHwOverlayWin(true);
2761 bool useVideoSwOverlay = false;
2762 if (overlaySupported &&
2763 StaticPrefs::gfx_webrender_dcomp_video_sw_overlay_win_AtStartup()) {
2764 useVideoSwOverlay = true;
2766 if (useVideoSwOverlay &&
2767 !StaticPrefs::
2768 gfx_webrender_dcomp_video_sw_overlay_win_force_enabled_AtStartup()) {
2769 nsCString failureId;
2770 int32_t status;
2771 const nsCOMPtr<nsIGfxInfo> gfxInfo = components::GfxInfo::Service();
2772 if (NS_FAILED(gfxInfo->GetFeatureStatus(
2773 nsIGfxInfo::FEATURE_VIDEO_SOFTWARE_OVERLAY, failureId,
2774 &status))) {
2775 FeatureState& feature =
2776 gfxConfig::GetFeature(Feature::VIDEO_SOFTWARE_OVERLAY);
2777 feature.DisableByDefault(FeatureStatus::BlockedNoGfxInfo,
2778 "gfxInfo is broken",
2779 "FEATURE_FAILURE_WR_NO_GFX_INFO"_ns);
2780 useVideoSwOverlay = false;
2781 } else {
2782 if (status != nsIGfxInfo::FEATURE_STATUS_OK) {
2783 FeatureState& feature =
2784 gfxConfig::GetFeature(Feature::VIDEO_SOFTWARE_OVERLAY);
2785 feature.DisableByDefault(FeatureStatus::Blocked,
2786 "Blocklisted by gfxInfo", failureId);
2787 useVideoSwOverlay = false;
2791 } else if (overlaySupported) {
2792 FeatureState& feature =
2793 gfxConfig::GetFeature(Feature::VIDEO_SOFTWARE_OVERLAY);
2794 feature.DisableByDefault(FeatureStatus::Blocked, "Disabled by pref",
2795 "FEATURE_FAILURE_DISABLED_BY_PREF"_ns);
2798 if (useVideoSwOverlay) {
2799 FeatureState& feature =
2800 gfxConfig::GetFeature(Feature::VIDEO_SOFTWARE_OVERLAY);
2801 feature.EnableByDefault();
2802 gfxVars::SetUseWebRenderDCompVideoSwOverlayWin(true);
2805 bool useHwVideoZeroCopy = false;
2806 if (StaticPrefs::media_wmf_zero_copy_nv12_textures_AtStartup()) {
2807 if (hasHardware) {
2808 useHwVideoZeroCopy = true;
2811 if (useHwVideoZeroCopy &&
2812 !StaticPrefs::
2813 media_wmf_zero_copy_nv12_textures_force_enabled_AtStartup()) {
2814 nsCString failureId;
2815 int32_t status;
2816 const nsCOMPtr<nsIGfxInfo> gfxInfo = components::GfxInfo::Service();
2817 if (NS_FAILED(gfxInfo->GetFeatureStatus(
2818 nsIGfxInfo::FEATURE_HW_DECODED_VIDEO_ZERO_COPY, failureId,
2819 &status))) {
2820 FeatureState& feature =
2821 gfxConfig::GetFeature(Feature::HW_DECODED_VIDEO_ZERO_COPY);
2822 feature.DisableByDefault(FeatureStatus::BlockedNoGfxInfo,
2823 "gfxInfo is broken",
2824 "FEATURE_FAILURE_WR_NO_GFX_INFO"_ns);
2825 useHwVideoZeroCopy = false;
2826 } else {
2827 if (status != nsIGfxInfo::FEATURE_ALLOW_ALWAYS) {
2828 FeatureState& feature =
2829 gfxConfig::GetFeature(Feature::HW_DECODED_VIDEO_ZERO_COPY);
2830 feature.DisableByDefault(FeatureStatus::Blocked,
2831 "Blocklisted by gfxInfo", failureId);
2832 useHwVideoZeroCopy = false;
2838 if (useHwVideoZeroCopy) {
2839 FeatureState& feature =
2840 gfxConfig::GetFeature(Feature::HW_DECODED_VIDEO_ZERO_COPY);
2841 feature.EnableByDefault();
2842 gfxVars::SetHwDecodedVideoZeroCopy(true);
2845 bool reuseDecoderDevice = false;
2846 if (StaticPrefs::gfx_direct3d11_reuse_decoder_device_AtStartup()) {
2847 reuseDecoderDevice = true;
2849 if (reuseDecoderDevice &&
2850 !StaticPrefs::
2851 gfx_direct3d11_reuse_decoder_device_force_enabled_AtStartup()) {
2852 nsCString failureId;
2853 int32_t status;
2854 const nsCOMPtr<nsIGfxInfo> gfxInfo = components::GfxInfo::Service();
2855 if (NS_FAILED(gfxInfo->GetFeatureStatus(
2856 nsIGfxInfo::FEATURE_REUSE_DECODER_DEVICE, failureId, &status))) {
2857 FeatureState& feature =
2858 gfxConfig::GetFeature(Feature::REUSE_DECODER_DEVICE);
2859 feature.DisableByDefault(FeatureStatus::BlockedNoGfxInfo,
2860 "gfxInfo is broken",
2861 "FEATURE_FAILURE_WR_NO_GFX_INFO"_ns);
2862 reuseDecoderDevice = false;
2863 } else {
2864 if (status != nsIGfxInfo::FEATURE_STATUS_OK) {
2865 FeatureState& feature =
2866 gfxConfig::GetFeature(Feature::REUSE_DECODER_DEVICE);
2867 feature.DisableByDefault(FeatureStatus::Blocked,
2868 "Blocklisted by gfxInfo", failureId);
2869 reuseDecoderDevice = false;
2875 if (reuseDecoderDevice) {
2876 FeatureState& feature =
2877 gfxConfig::GetFeature(Feature::REUSE_DECODER_DEVICE);
2878 feature.EnableByDefault();
2879 gfxVars::SetReuseDecoderDevice(true);
2882 if (Preferences::GetBool("gfx.webrender.flip-sequential", false)) {
2883 if (gfxVars::UseWebRenderANGLE()) {
2884 gfxVars::SetUseWebRenderFlipSequentialWin(true);
2887 if (Preferences::GetBool("gfx.webrender.triple-buffering.enabled", false)) {
2888 if (gfxVars::UseWebRenderDCompWin() ||
2889 gfxVars::UseWebRenderFlipSequentialWin()) {
2890 gfxVars::SetUseWebRenderTripleBufferingWin(true);
2893 #endif
2895 if (gfxConfig::IsEnabled(Feature::WEBRENDER_COMPOSITOR)) {
2896 gfxVars::SetUseWebRenderCompositor(true);
2899 Telemetry::ScalarSet(
2900 Telemetry::ScalarID::GFX_OS_COMPOSITOR,
2901 gfx::gfxConfig::IsEnabled(gfx::Feature::WEBRENDER_COMPOSITOR));
2903 if (gfxConfig::IsEnabled(Feature::WEBRENDER_PARTIAL)) {
2904 gfxVars::SetWebRenderMaxPartialPresentRects(
2905 StaticPrefs::gfx_webrender_max_partial_present_rects_AtStartup());
2908 // Set features that affect WR's RendererOptions
2909 gfxVars::SetUseGLSwizzle(
2910 IsFeatureSupported(nsIGfxInfo::FEATURE_GL_SWIZZLE, true));
2911 gfxVars::SetUseWebRenderScissoredCacheClears(gfx::gfxConfig::IsEnabled(
2912 gfx::Feature::WEBRENDER_SCISSORED_CACHE_CLEARS));
2914 // The RemoveShaderCacheFromDiskIfNecessary() needs to be called after
2915 // WebRenderConfig initialization.
2916 gfxUtils::RemoveShaderCacheFromDiskIfNecessary();
2919 void gfxPlatform::InitHardwareVideoConfig() {
2920 if (!XRE_IsParentProcess()) {
2921 return;
2924 #ifdef MOZ_WIDGET_GTK
2925 // We don't want to expose codec info if whole HW decoding is disabled.
2926 if (!sLayersSupportsHardwareVideoDecoding) {
2927 return;
2929 #endif
2931 nsCString message;
2932 nsCString failureId;
2934 FeatureState& featureVP8 = gfxConfig::GetFeature(Feature::VP8_HW_DECODE);
2935 featureVP8.EnableByDefault();
2937 if (!IsGfxInfoStatusOkay(nsIGfxInfo::FEATURE_VP8_HW_DECODE, &message,
2938 failureId)) {
2939 featureVP8.Disable(FeatureStatus::Blocklisted, message.get(), failureId);
2941 gfxVars::SetUseVP8HwDecode(featureVP8.IsEnabled());
2943 FeatureState& featureVP9 = gfxConfig::GetFeature(Feature::VP9_HW_DECODE);
2944 featureVP9.EnableByDefault();
2946 if (!IsGfxInfoStatusOkay(nsIGfxInfo::FEATURE_VP9_HW_DECODE, &message,
2947 failureId)) {
2948 featureVP9.Disable(FeatureStatus::Blocklisted, message.get(), failureId);
2950 gfxVars::SetUseVP9HwDecode(featureVP9.IsEnabled());
2952 // H264_HW_DECODE/AV1_HW_DECODE is used on Linux only right now.
2953 #ifdef MOZ_WIDGET_GTK
2954 FeatureState& featureH264 = gfxConfig::GetFeature(Feature::H264_HW_DECODE);
2955 featureH264.EnableByDefault();
2957 if (!IsGfxInfoStatusOkay(nsIGfxInfo::FEATURE_H264_HW_DECODE, &message,
2958 failureId)) {
2959 featureH264.Disable(FeatureStatus::Blocklisted, message.get(), failureId);
2961 gfxVars::SetUseH264HwDecode(featureH264.IsEnabled());
2963 FeatureState& featureAV1 = gfxConfig::GetFeature(Feature::AV1_HW_DECODE);
2964 featureAV1.EnableByDefault();
2966 if (!IsGfxInfoStatusOkay(nsIGfxInfo::FEATURE_AV1_HW_DECODE, &message,
2967 failureId)) {
2968 featureAV1.Disable(FeatureStatus::Blocklisted, message.get(), failureId);
2970 gfxVars::SetUseAV1HwDecode(featureAV1.IsEnabled());
2971 #endif
2974 void gfxPlatform::InitWebGLConfig() {
2975 if (!XRE_IsParentProcess()) return;
2977 const nsCOMPtr<nsIGfxInfo> gfxInfo = components::GfxInfo::Service();
2979 const auto IsFeatureOk = [&](const int32_t feature) {
2980 nsCString discardFailureId;
2981 int32_t status;
2982 MOZ_RELEASE_ASSERT(NS_SUCCEEDED(
2983 gfxInfo->GetFeatureStatus(feature, discardFailureId, &status)));
2984 return (status == nsIGfxInfo::FEATURE_STATUS_OK);
2987 gfxVars::SetAllowWebgl2(IsFeatureOk(nsIGfxInfo::FEATURE_WEBGL2));
2988 gfxVars::SetWebglAllowWindowsNativeGl(
2989 IsFeatureOk(nsIGfxInfo::FEATURE_WEBGL_OPENGL));
2990 gfxVars::SetAllowWebglAccelAngle(
2991 IsFeatureOk(nsIGfxInfo::FEATURE_WEBGL_ANGLE));
2993 if (kIsMacOS) {
2994 // Avoid crash for Intel HD Graphics 3000 on OSX. (Bug 1413269)
2995 nsString vendorID, deviceID;
2996 gfxInfo->GetAdapterVendorID(vendorID);
2997 gfxInfo->GetAdapterDeviceID(deviceID);
2998 if (vendorID.EqualsLiteral("0x8086") &&
2999 (deviceID.EqualsLiteral("0x0116") ||
3000 deviceID.EqualsLiteral("0x0126"))) {
3001 gfxVars::SetWebglAllowCoreProfile(false);
3005 bool allowWebGLOop =
3006 IsFeatureOk(nsIGfxInfo::FEATURE_ALLOW_WEBGL_OUT_OF_PROCESS);
3007 if (!kIsAndroid) {
3008 gfxVars::SetAllowWebglOop(allowWebGLOop);
3009 } else {
3010 // On android, enable out-of-process WebGL only when GPU process exists.
3011 gfxVars::SetAllowWebglOop(allowWebGLOop &&
3012 gfxConfig::IsEnabled(Feature::GPU_PROCESS));
3013 // Enable gl::SharedSurface of AndroidHardwareBuffer when API version is 26+
3014 // and out-of-process WebGL is enabled.
3015 #ifdef MOZ_WIDGET_ANDROID
3016 if (gfxVars::AllowWebglOop() && jni::GetAPIVersion() >= 26 &&
3017 StaticPrefs::webgl_out_of_process_enable_ahardwarebuffer_AtStartup()) {
3018 gfxVars::SetUseAHardwareBufferSharedSurfaceWebglOop(true);
3020 #endif
3023 bool threadsafeGL = IsFeatureOk(nsIGfxInfo::FEATURE_THREADSAFE_GL);
3024 threadsafeGL |= StaticPrefs::webgl_threadsafe_gl_force_enabled_AtStartup();
3025 threadsafeGL &= !StaticPrefs::webgl_threadsafe_gl_force_disabled_AtStartup();
3026 gfxVars::SetSupportsThreadsafeGL(threadsafeGL);
3028 FeatureState& feature =
3029 gfxConfig::GetFeature(Feature::CANVAS_RENDERER_THREAD);
3030 if (!threadsafeGL) {
3031 feature.DisableByDefault(FeatureStatus::Blocked, "Thread unsafe GL",
3032 "FEATURE_FAILURE_THREAD_UNSAFE_GL"_ns);
3033 } else if (!StaticPrefs::webgl_use_canvas_render_thread_AtStartup()) {
3034 feature.DisableByDefault(FeatureStatus::Blocked, "Disabled by pref",
3035 "FEATURE_FAILURE_DISABLED_BY_PREF"_ns);
3036 } else {
3037 feature.EnableByDefault();
3039 gfxVars::SetUseCanvasRenderThread(feature.IsEnabled());
3041 bool webglOopAsyncPresentForceSync =
3042 !gfxVars::UseCanvasRenderThread() ||
3043 StaticPrefs::webgl_out_of_process_async_present_force_sync();
3044 gfxVars::SetWebglOopAsyncPresentForceSync(webglOopAsyncPresentForceSync);
3046 if (kIsAndroid) {
3047 // Don't enable robust buffer access on Adreno 620 and 630 devices.
3048 // It causes the linking of some shaders to fail. See bug 1485441 and
3049 // bug 1810693.
3050 nsAutoString renderer;
3051 gfxInfo->GetAdapterDeviceID(renderer);
3052 if ((renderer.Find(u"Adreno (TM) 620") != -1) ||
3053 (renderer.Find(u"Adreno (TM) 630") != -1)) {
3054 gfxVars::SetAllowEglRbab(false);
3058 if (kIsLinux) {
3059 nsCString discardFailureId;
3060 int32_t status;
3061 FeatureState& feature =
3062 gfxConfig::GetFeature(Feature::DMABUF_SURFACE_EXPORT);
3063 if (NS_FAILED(
3064 gfxInfo->GetFeatureStatus(nsIGfxInfo::FEATURE_DMABUF_SURFACE_EXPORT,
3065 discardFailureId, &status)) ||
3066 status != nsIGfxInfo::FEATURE_STATUS_OK) {
3067 feature.DisableByDefault(FeatureStatus::Blocked, "Blocklisted by gfxInfo",
3068 discardFailureId);
3069 gfxVars::SetUseDMABufSurfaceExport(false);
3070 } else {
3071 feature.EnableByDefault();
3076 void gfxPlatform::InitWebGPUConfig() {
3077 if (!XRE_IsParentProcess()) {
3078 return;
3081 FeatureState& feature = gfxConfig::GetFeature(Feature::WEBGPU);
3082 feature.EnableByDefault();
3084 nsCString message;
3085 nsCString failureId;
3086 if (!IsGfxInfoStatusOkay(nsIGfxInfo::FEATURE_WEBGPU, &message, failureId)) {
3087 if (StaticPrefs::gfx_webgpu_ignore_blocklist_AtStartup()) {
3088 feature.UserForceEnable(
3089 "Ignoring blocklist entry because of gfx.webgpu.force-enabled:true.");
3092 feature.Disable(FeatureStatus::Blocklisted, message.get(), failureId);
3095 #ifdef RELEASE_OR_BETA
3096 feature.ForceDisable(FeatureStatus::Blocked,
3097 "WebGPU cannot be enabled in release or beta",
3098 "WEBGPU_DISABLE_RELEASE_OR_BETA"_ns);
3099 #endif
3101 gfxVars::SetAllowWebGPU(feature.IsEnabled());
3104 #ifdef XP_WIN
3105 static void WindowOcclusionPrefChangeCallback(const char* aPref, void*) {
3106 const char* env = PR_GetEnv("MOZ_WINDOW_OCCLUSION");
3107 if (env) {
3108 // env has a higher priority than pref.
3109 return;
3112 FeatureState& feature = gfxConfig::GetFeature(Feature::WINDOW_OCCLUSION);
3113 bool enabled =
3114 StaticPrefs::widget_windows_window_occlusion_tracking_enabled();
3116 printf_stderr("Dynamically enable window occlusion %d\n", enabled);
3118 // Update feature before calling WinUtils::EnableWindowOcclusion()
3119 if (enabled) {
3120 feature.UserEnable("User enabled by pref");
3121 } else {
3122 feature.UserDisable("User disabled via pref",
3123 "FEATURE_FAILURE_PREF_DISABLED"_ns);
3125 widget::WinUtils::EnableWindowOcclusion(enabled);
3127 #endif
3129 void gfxPlatform::InitWindowOcclusionConfig() {
3130 if (!XRE_IsParentProcess()) {
3131 return;
3133 #ifdef XP_WIN
3134 FeatureState& feature = gfxConfig::GetFeature(Feature::WINDOW_OCCLUSION);
3135 feature.SetDefaultFromPref(
3136 StaticPrefs::
3137 GetPrefName_widget_windows_window_occlusion_tracking_enabled(),
3138 true,
3139 StaticPrefs::
3140 GetPrefDefault_widget_windows_window_occlusion_tracking_enabled());
3142 const char* env = PR_GetEnv("MOZ_WINDOW_OCCLUSION");
3143 if (env) {
3144 if (*env == '1') {
3145 feature.UserForceEnable("Force enabled by envvar");
3146 } else {
3147 feature.UserDisable("Force disabled by envvar",
3148 "FEATURE_FAILURE_OCCL_ENV"_ns);
3152 Preferences::RegisterCallback(
3153 WindowOcclusionPrefChangeCallback,
3154 nsDependentCString(
3155 StaticPrefs::
3156 GetPrefName_widget_windows_window_occlusion_tracking_enabled()));
3157 #endif
3160 static void BackdropFilterPrefChangeCallback(const char*, void*) {
3161 FeatureState& feature = gfxConfig::GetFeature(Feature::BACKDROP_FILTER);
3163 // We need to reset because the user status needs to be set before the
3164 // environment status, but the environment status comes from the blocklist,
3165 // and the user status can be updated after the fact.
3166 feature.Reset();
3167 feature.EnableByDefault();
3169 if (StaticPrefs::layout_css_backdrop_filter_force_enabled()) {
3170 feature.UserForceEnable("Force enabled by pref");
3173 nsCString message;
3174 nsCString failureId;
3175 if (!gfxPlatform::IsGfxInfoStatusOkay(nsIGfxInfo::FEATURE_BACKDROP_FILTER,
3176 &message, failureId)) {
3177 feature.Disable(FeatureStatus::Blocklisted, message.get(), failureId);
3180 // This may still be gated by the layout.css.backdrop-filter.enabled pref but
3181 // the test infrastructure is very sensitive to how changes to that pref
3182 // propagate, so we don't include them in the gfxVars/gfxFeature.
3183 gfxVars::SetAllowBackdropFilter(feature.IsEnabled());
3186 void gfxPlatform::InitBackdropFilterConfig() {
3187 // This would ideally be in the nsCSSProps code
3188 // but nsCSSProps is initialized before gfxPlatform
3189 // so it has to be done here.
3190 gfxVars::AddReceiver(&nsCSSProps::GfxVarReceiver());
3192 if (!XRE_IsParentProcess()) {
3193 // gfxVars doesn't notify receivers when initialized on content processes
3194 // we need to explicitly recompute backdrop-filter's enabled state here.
3195 nsCSSProps::RecomputeEnabledState(
3196 StaticPrefs::GetPrefName_layout_css_backdrop_filter_enabled());
3197 return;
3200 BackdropFilterPrefChangeCallback(nullptr, nullptr);
3202 Preferences::RegisterCallback(
3203 BackdropFilterPrefChangeCallback,
3204 nsDependentCString(
3205 StaticPrefs::GetPrefName_layout_css_backdrop_filter_force_enabled()));
3208 static void AcceleratedCanvas2DPrefChangeCallback(const char*, void*) {
3209 FeatureState& feature = gfxConfig::GetFeature(Feature::ACCELERATED_CANVAS2D);
3211 // Reset to track toggling prefs and ensure force-enable does not happen
3212 // after blocklist.
3213 feature.Reset();
3215 // gfx.canvas.accelerated pref controls whether platform enables the feature,
3216 // but it still allows blocklisting to override it later.
3217 feature.SetDefaultFromPref(
3218 StaticPrefs::GetPrefName_gfx_canvas_accelerated(), true,
3219 StaticPrefs::GetPrefDefault_gfx_canvas_accelerated());
3221 // gfx.canvas.accelerated.force-enabled overrides the blocklist.
3222 if (StaticPrefs::gfx_canvas_accelerated_force_enabled()) {
3223 feature.UserForceEnable("Force-enabled by pref");
3226 if (kIsAndroid && !gfxConfig::IsEnabled(Feature::GPU_PROCESS)) {
3227 feature.Disable(FeatureStatus::Blocked, "Disabled by GPU Process disabled",
3228 "FEATURE_FAILURE_DISABLED_BY_GPU_PROCESS_DISABLED"_ns);
3229 } else if (!gfxConfig::IsEnabled(Feature::WEBRENDER)) {
3230 // There isn't much benefit to accelerating Canvas2D if we can't accelerate
3231 // WebRender itself.
3232 feature.Disable(FeatureStatus::Blocked, "Disabled by Software WebRender",
3233 "FEATURE_FAILURE_DISABLED_BY_SOFTWARE_WEBRENDER"_ns);
3236 // Check if blocklisted despite the default pref.
3237 nsCString message;
3238 nsCString failureId;
3239 if (!gfxPlatform::IsGfxInfoStatusOkay(
3240 nsIGfxInfo::FEATURE_ACCELERATED_CANVAS2D, &message, failureId)) {
3241 feature.Disable(FeatureStatus::Blocklisted, message.get(), failureId);
3244 gfxVars::SetUseAcceleratedCanvas2D(feature.IsEnabled());
3247 void gfxPlatform::InitAcceleratedCanvas2DConfig() {
3248 if (!XRE_IsParentProcess()) {
3249 return;
3252 // Decide during pref changes whether or not to enable acceleration. This
3253 // allows easily toggling acceleration on and off to test performance.
3254 AcceleratedCanvas2DPrefChangeCallback(nullptr, nullptr);
3256 Preferences::RegisterCallback(
3257 AcceleratedCanvas2DPrefChangeCallback,
3258 nsDependentCString(StaticPrefs::GetPrefName_gfx_canvas_accelerated()));
3259 Preferences::RegisterCallback(
3260 AcceleratedCanvas2DPrefChangeCallback,
3261 nsDependentCString(
3262 StaticPrefs::GetPrefName_gfx_canvas_accelerated_force_enabled()));
3265 bool gfxPlatform::CanUseHardwareVideoDecoding() {
3266 // this function is called from the compositor thread, so it is not
3267 // safe to init the prefs etc. from here.
3268 MOZ_ASSERT(sLayersAccelerationPrefsInitialized);
3269 return sLayersSupportsHardwareVideoDecoding &&
3270 !sLayersHardwareVideoDecodingFailed;
3273 bool gfxPlatform::AccelerateLayersByDefault() {
3274 #if defined(MOZ_GL_PROVIDER) || defined(MOZ_WIDGET_UIKIT)
3275 return true;
3276 #else
3277 return false;
3278 #endif
3281 /* static */
3282 bool gfxPlatform::UsesOffMainThreadCompositing() {
3283 if (XRE_GetProcessType() == GeckoProcessType_GPU) {
3284 return true;
3287 static bool firstTime = true;
3288 static bool result = false;
3290 if (firstTime) {
3291 MOZ_ASSERT(sLayersAccelerationPrefsInitialized);
3292 result = gfxVars::BrowserTabsRemoteAutostart() ||
3293 !StaticPrefs::
3294 layers_offmainthreadcomposition_force_disabled_AtStartup();
3295 #if defined(MOZ_WIDGET_GTK)
3296 // Linux users who chose OpenGL are being included in OMTC
3297 result |= StaticPrefs::
3298 layers_acceleration_force_enabled_AtStartup_DoNotUseDirectly();
3300 #endif
3301 firstTime = false;
3304 return result;
3307 RefPtr<mozilla::VsyncDispatcher> gfxPlatform::GetGlobalVsyncDispatcher() {
3308 MOZ_ASSERT(mVsyncDispatcher,
3309 "mVsyncDispatcher should have been initialized by ReInitFrameRate "
3310 "during gfxPlatform init");
3311 MOZ_ASSERT(XRE_IsParentProcess());
3312 return mVsyncDispatcher;
3315 already_AddRefed<mozilla::gfx::VsyncSource>
3316 gfxPlatform::GetGlobalHardwareVsyncSource() {
3317 if (!mGlobalHardwareVsyncSource) {
3318 mGlobalHardwareVsyncSource = CreateGlobalHardwareVsyncSource();
3320 return do_AddRef(mGlobalHardwareVsyncSource);
3323 /***
3324 * The preference "layout.frame_rate" has 3 meanings depending on the value:
3326 * -1 = Auto (default), use hardware vsync or software vsync @ 60 hz if hw
3327 * vsync fails.
3328 * 0 = ASAP mode - used during talos testing.
3329 * X = Software vsync at a rate of X times per second.
3331 already_AddRefed<mozilla::gfx::VsyncSource>
3332 gfxPlatform::GetSoftwareVsyncSource() {
3333 if (!mSoftwareVsyncSource) {
3334 double rateInMS = 1000.0 / (double)gfxPlatform::GetSoftwareVsyncRate();
3335 mSoftwareVsyncSource = new mozilla::gfx::SoftwareVsyncSource(
3336 TimeDuration::FromMilliseconds(rateInMS));
3338 return do_AddRef(mSoftwareVsyncSource);
3341 /* static */
3342 bool gfxPlatform::IsInLayoutAsapMode() {
3343 // There are 2 modes of ASAP mode.
3344 // 1 is that the refresh driver and compositor are in lock step
3345 // the second is that the compositor goes ASAP and the refresh driver
3346 // goes at whatever the configurated rate is. This only checks the version
3347 // talos uses, which is the refresh driver and compositor are in lockstep.
3348 // Ignore privacy_resistFingerprinting to preserve ASAP mode there.
3349 return StaticPrefs::layout_frame_rate() == 0;
3352 static int LayoutFrameRateFromPrefs() {
3353 auto val = StaticPrefs::layout_frame_rate();
3354 if (nsContentUtils::ShouldResistFingerprinting(
3355 "The frame rate is a global property.", RFPTarget::FrameRate)) {
3356 val = 60;
3358 return val;
3361 /* static */
3362 bool gfxPlatform::ForceSoftwareVsync() {
3363 return LayoutFrameRateFromPrefs() > 0;
3366 /* static */
3367 int gfxPlatform::GetSoftwareVsyncRate() {
3368 int preferenceRate = LayoutFrameRateFromPrefs();
3369 if (preferenceRate <= 0) {
3370 return gfxPlatform::GetDefaultFrameRate();
3372 return preferenceRate;
3375 /* static */
3376 int gfxPlatform::GetDefaultFrameRate() { return 60; }
3378 /* static */
3379 void gfxPlatform::ReInitFrameRate(const char* aPrefIgnored,
3380 void* aDataIgnored) {
3381 MOZ_RELEASE_ASSERT(XRE_IsParentProcess());
3383 if (gPlatform->mSoftwareVsyncSource) {
3384 // Update the rate of the existing software vsync source.
3385 double rateInMS = 1000.0 / (double)gfxPlatform::GetSoftwareVsyncRate();
3386 gPlatform->mSoftwareVsyncSource->SetVsyncRate(
3387 TimeDuration::FromMilliseconds(rateInMS));
3390 // Swap out the dispatcher's underlying source.
3391 RefPtr<VsyncSource> vsyncSource =
3392 gfxPlatform::ForceSoftwareVsync()
3393 ? gPlatform->GetSoftwareVsyncSource()
3394 : gPlatform->GetGlobalHardwareVsyncSource();
3395 gPlatform->mVsyncDispatcher->SetVsyncSource(vsyncSource);
3398 const char* gfxPlatform::GetAzureCanvasBackend() const {
3399 BackendType backend{};
3401 if (gfxConfig::IsEnabled(Feature::GPU_PROCESS)) {
3402 // Assume content process' backend prefs.
3403 BackendPrefsData data = GetBackendPrefs();
3404 backend = GetCanvasBackendPref(data.mCanvasBitmask);
3405 if (backend == BackendType::NONE) {
3406 backend = data.mCanvasDefault;
3408 } else {
3409 backend = mPreferredCanvasBackend;
3412 return GetBackendName(backend);
3415 const char* gfxPlatform::GetAzureContentBackend() const {
3416 BackendType backend{};
3418 if (gfxConfig::IsEnabled(Feature::GPU_PROCESS)) {
3419 // Assume content process' backend prefs.
3420 BackendPrefsData data = GetBackendPrefs();
3421 backend = GetContentBackendPref(data.mContentBitmask);
3422 if (backend == BackendType::NONE) {
3423 backend = data.mContentDefault;
3425 } else {
3426 backend = mContentBackend;
3429 return GetBackendName(backend);
3432 void gfxPlatform::GetAzureBackendInfo(mozilla::widget::InfoObject& aObj) {
3433 if (gfxConfig::IsEnabled(Feature::GPU_PROCESS)) {
3434 aObj.DefineProperty("AzureCanvasBackend (UI Process)",
3435 GetBackendName(mPreferredCanvasBackend));
3436 aObj.DefineProperty("AzureFallbackCanvasBackend (UI Process)",
3437 GetBackendName(mFallbackCanvasBackend));
3438 aObj.DefineProperty("AzureContentBackend (UI Process)",
3439 GetBackendName(mContentBackend));
3440 } else {
3441 aObj.DefineProperty("AzureFallbackCanvasBackend",
3442 GetBackendName(mFallbackCanvasBackend));
3445 aObj.DefineProperty("AzureCanvasBackend", GetAzureCanvasBackend());
3446 aObj.DefineProperty("AzureContentBackend", GetAzureContentBackend());
3449 void gfxPlatform::GetApzSupportInfo(mozilla::widget::InfoObject& aObj) {
3450 if (!gfxPlatform::AsyncPanZoomEnabled()) {
3451 return;
3454 if (SupportsApzWheelInput()) {
3455 aObj.DefineProperty("ApzWheelInput", 1);
3458 if (SupportsApzTouchInput()) {
3459 aObj.DefineProperty("ApzTouchInput", 1);
3462 if (SupportsApzDragInput()) {
3463 aObj.DefineProperty("ApzDragInput", 1);
3466 if (SupportsApzKeyboardInput() &&
3467 !StaticPrefs::accessibility_browsewithcaret()) {
3468 aObj.DefineProperty("ApzKeyboardInput", 1);
3471 if (SupportsApzAutoscrolling()) {
3472 aObj.DefineProperty("ApzAutoscrollInput", 1);
3475 if (SupportsApzZooming()) {
3476 aObj.DefineProperty("ApzZoomingInput", 1);
3480 void gfxPlatform::GetFrameStats(mozilla::widget::InfoObject& aObj) {
3481 uint32_t i = 0;
3482 for (FrameStats& f : mFrameStats) {
3483 nsPrintfCString name("Slow Frame #%02u", ++i);
3485 nsPrintfCString value(
3486 "Frame %" PRIu64
3487 "(%s) CONTENT_FRAME_TIME %d - Transaction start %f, main-thread time "
3488 "%f, full paint time %f, Skipped composites %u, Composite start %f, "
3489 "Resource upload time %f, GPU cache upload time %f, Render time %f, "
3490 "Composite time %f",
3491 f.id().mId, f.url().get(), f.contentFrameTime(),
3492 (f.transactionStart() - f.refreshStart()).ToMilliseconds(),
3493 (f.fwdTime() - f.transactionStart()).ToMilliseconds(),
3494 f.sceneBuiltTime()
3495 ? (f.sceneBuiltTime() - f.transactionStart()).ToMilliseconds()
3496 : 0.0,
3497 f.skippedComposites(),
3498 (f.compositeStart() - f.refreshStart()).ToMilliseconds(),
3499 f.resourceUploadTime(), f.gpuCacheUploadTime(),
3500 (f.compositeEnd() - f.renderStart()).ToMilliseconds(),
3501 (f.compositeEnd() - f.compositeStart()).ToMilliseconds());
3502 aObj.DefineProperty(name.get(), value.get());
3506 void gfxPlatform::GetCMSSupportInfo(mozilla::widget::InfoObject& aObj) {
3507 nsTArray<uint8_t> outputProfileData =
3508 gfxPlatform::GetPlatform()->GetPlatformCMSOutputProfileData();
3509 if (outputProfileData.IsEmpty()) {
3510 nsPrintfCString msg("Empty profile data");
3511 aObj.DefineProperty("CMSOutputProfile", msg.get());
3512 return;
3515 // Some profiles can be quite large. We don't want to include giant profiles
3516 // by default in about:support. For now, we only accept less than 8kiB.
3517 const size_t kMaxProfileSize = 8192;
3518 if (outputProfileData.Length() >= kMaxProfileSize) {
3519 nsPrintfCString msg("%zu bytes, too large", outputProfileData.Length());
3520 aObj.DefineProperty("CMSOutputProfile", msg.get());
3521 return;
3524 nsString encodedProfile;
3525 nsresult rv =
3526 Base64Encode(reinterpret_cast<const char*>(outputProfileData.Elements()),
3527 outputProfileData.Length(), encodedProfile);
3528 if (!NS_SUCCEEDED(rv)) {
3529 nsPrintfCString msg("base64 encode failed 0x%08x",
3530 static_cast<uint32_t>(rv));
3531 aObj.DefineProperty("CMSOutputProfile", msg.get());
3532 return;
3535 aObj.DefineProperty("CMSOutputProfile", encodedProfile);
3538 void gfxPlatform::GetDisplayInfo(mozilla::widget::InfoObject& aObj) {
3539 auto& screens = widget::ScreenManager::GetSingleton().CurrentScreenList();
3540 aObj.DefineProperty("DisplayCount", screens.Length());
3542 size_t i = 0;
3543 for (auto& screen : screens) {
3544 const LayoutDeviceIntRect rect = screen->GetRect();
3545 nsPrintfCString value("%dx%d@%dHz scales:%f|%f", rect.width, rect.height,
3546 screen->GetRefreshRate(),
3547 screen->GetContentsScaleFactor(),
3548 screen->GetDefaultCSSScaleFactor());
3550 aObj.DefineProperty(nsPrintfCString("Display%zu", i++).get(),
3551 NS_ConvertUTF8toUTF16(value));
3554 // Platform display info is only currently used for about:support and getting
3555 // it might fail in a child process anyway.
3556 if (XRE_IsParentProcess()) {
3557 GetPlatformDisplayInfo(aObj);
3561 void gfxPlatform::GetOverlayInfo(mozilla::widget::InfoObject& aObj) {
3562 if (mOverlayInfo.isNothing()) {
3563 return;
3566 auto toString = [](mozilla::layers::OverlaySupportType aType) -> const char* {
3567 switch (aType) {
3568 case mozilla::layers::OverlaySupportType::None:
3569 return "None";
3570 case mozilla::layers::OverlaySupportType::Software:
3571 return "Software";
3572 case mozilla::layers::OverlaySupportType::Direct:
3573 return "Direct";
3574 case mozilla::layers::OverlaySupportType::Scaling:
3575 return "Scaling";
3576 default:
3577 MOZ_ASSERT_UNREACHABLE("Unexpected to be called");
3579 MOZ_CRASH("Incomplete switch");
3582 nsPrintfCString value("NV12=%s YUV2=%s BGRA8=%s RGB10A2=%s",
3583 toString(mOverlayInfo.ref().mNv12Overlay),
3584 toString(mOverlayInfo.ref().mYuy2Overlay),
3585 toString(mOverlayInfo.ref().mBgra8Overlay),
3586 toString(mOverlayInfo.ref().mRgb10a2Overlay));
3588 aObj.DefineProperty("OverlaySupport", NS_ConvertUTF8toUTF16(value));
3591 void gfxPlatform::GetSwapChainInfo(mozilla::widget::InfoObject& aObj) {
3592 if (mSwapChainInfo.isNothing()) {
3593 return;
3596 auto toString = [](bool aTearingSupported) -> const char* {
3597 if (aTearingSupported) {
3598 return "Supported";
3600 return "Not Supported";
3603 nsPrintfCString value("%s", toString(mSwapChainInfo.ref().mTearingSupported));
3605 aObj.DefineProperty("SwapChainTearingSupport", NS_ConvertUTF8toUTF16(value));
3608 class FrameStatsComparator {
3609 public:
3610 bool Equals(const FrameStats& aA, const FrameStats& aB) const {
3611 return aA.contentFrameTime() == aB.contentFrameTime();
3613 // Reverse the condition here since we want the array sorted largest to
3614 // smallest.
3615 bool LessThan(const FrameStats& aA, const FrameStats& aB) const {
3616 return aA.contentFrameTime() > aB.contentFrameTime();
3620 void gfxPlatform::NotifyFrameStats(nsTArray<FrameStats>&& aFrameStats) {
3621 if (!StaticPrefs::gfx_logging_slow_frames_enabled_AtStartup()) {
3622 return;
3625 FrameStatsComparator comp;
3626 for (FrameStats& f : aFrameStats) {
3627 mFrameStats.InsertElementSorted(f, comp);
3629 if (mFrameStats.Length() > 10) {
3630 mFrameStats.SetLength(10);
3634 /*static*/
3635 uint32_t gfxPlatform::TargetFrameRate() {
3636 if (gPlatform && gPlatform->mVsyncDispatcher) {
3637 return round(1000.0 /
3638 gPlatform->mVsyncDispatcher->GetVsyncRate().ToMilliseconds());
3640 return 0;
3643 /* static */
3644 bool gfxPlatform::UseDesktopZoomingScrollbars() {
3645 return StaticPrefs::apz_allow_zooming() &&
3646 !StaticPrefs::apz_force_disable_desktop_zooming_scrollbars();
3649 /*static*/
3650 bool gfxPlatform::AsyncPanZoomEnabled() {
3651 #if !defined(MOZ_WIDGET_ANDROID) && !defined(MOZ_WIDGET_UIKIT)
3652 // For XUL applications (everything but Firefox on Android)
3653 // we only want to use APZ when E10S is enabled. If
3654 // we ever get input events off the main thread we can consider relaxing
3655 // this requirement.
3656 if (!BrowserTabsRemoteAutostart()) {
3657 return false;
3659 #endif
3660 #ifdef MOZ_WIDGET_ANDROID
3661 return true;
3662 #else
3663 // If Fission is enabled, OOP iframes require APZ for hittest. So, we
3664 // need to forcibly enable APZ in that case for avoiding users confused.
3665 if (FissionAutostart()) {
3666 return true;
3668 return StaticPrefs::
3669 layers_async_pan_zoom_enabled_AtStartup_DoNotUseDirectly();
3670 #endif
3673 /*static*/
3674 bool gfxPlatform::PerfWarnings() {
3675 return StaticPrefs::gfx_perf_warnings_enabled();
3678 void gfxPlatform::NotifyCompositorCreated(LayersBackend aBackend) {
3679 if (mCompositorBackend == aBackend) {
3680 return;
3683 if (mCompositorBackend != LayersBackend::LAYERS_NONE) {
3684 gfxCriticalNote << "Compositors might be mixed (" << int(mCompositorBackend)
3685 << "," << int(aBackend) << ")";
3688 // Set the backend before we notify so it's available immediately.
3689 mCompositorBackend = aBackend;
3691 if (XRE_IsParentProcess()) {
3692 Telemetry::ScalarSet(
3693 Telemetry::ScalarID::GFX_COMPOSITOR,
3694 NS_ConvertUTF8toUTF16(GetLayersBackendName(mCompositorBackend)));
3696 nsCString geckoVersion;
3697 nsCOMPtr<nsIXULAppInfo> app = do_GetService("@mozilla.org/xre/app-info;1");
3698 if (app) {
3699 app->GetVersion(geckoVersion);
3701 Telemetry::ScalarSet(Telemetry::ScalarID::GFX_LAST_COMPOSITOR_GECKO_VERSION,
3702 NS_ConvertASCIItoUTF16(geckoVersion));
3704 Telemetry::ScalarSet(
3705 Telemetry::ScalarID::GFX_FEATURE_WEBRENDER,
3706 NS_ConvertUTF8toUTF16(gfxConfig::GetFeature(gfx::Feature::WEBRENDER)
3707 .GetStatusAndFailureIdString()));
3710 // Notify that we created a compositor, so telemetry can update.
3711 NS_DispatchToMainThread(
3712 NS_NewRunnableFunction("gfxPlatform::NotifyCompositorCreated", [] {
3713 if (nsCOMPtr<nsIObserverService> obsvc =
3714 services::GetObserverService()) {
3715 obsvc->NotifyObservers(nullptr, "compositor:created", nullptr);
3717 }));
3720 /* static */
3721 bool gfxPlatform::FallbackFromAcceleration(FeatureStatus aStatus,
3722 const char* aMessage,
3723 const nsACString& aFailureId,
3724 bool aCrashAfterFinalFallback) {
3725 // We always want to ensure (Hardware) WebRender is disabled.
3726 if (gfxConfig::IsEnabled(Feature::WEBRENDER)) {
3727 gfxConfig::GetFeature(Feature::WEBRENDER)
3728 .ForceDisable(aStatus, aMessage, aFailureId);
3731 // Determine whether or not we are allowed to use Software WebRender in
3732 // fallback without the GPU process. Either the pref is false, or the feature
3733 // is enabled and we are currently still using it.
3734 bool swglFallbackAllowed =
3735 !StaticPrefs::
3736 gfx_webrender_fallback_software_requires_gpu_process_AtStartup() ||
3737 gfxConfig::IsEnabled(Feature::GPU_PROCESS);
3739 #ifdef XP_WIN
3740 // Before we disable D3D11 and HW_COMPOSITING, we should check if we can
3741 // fallback from WebRender to Software WebRender + D3D11 compositing.
3742 if (swglFallbackAllowed && gfxVars::AllowSoftwareWebRenderD3D11() &&
3743 gfxConfig::IsEnabled(Feature::D3D11_COMPOSITING) &&
3744 !gfxVars::UseSoftwareWebRender()) {
3745 // Fallback to Software WebRender + D3D11 compositing.
3746 gfxCriticalNote << "Fallback WR to SW-WR + D3D11";
3747 gfxVars::SetUseSoftwareWebRender(true);
3748 return true;
3751 if (swglFallbackAllowed && gfxVars::AllowSoftwareWebRenderD3D11() &&
3752 gfxVars::UseSoftwareWebRender()) {
3753 // Fallback from Software WebRender + D3D11 to Software WebRender.
3754 gfxCriticalNote << "Fallback SW-WR + D3D11 to SW-WR";
3755 gfxVars::SetAllowSoftwareWebRenderD3D11(false);
3756 return true;
3759 // We aren't using Software WebRender + D3D11 compositing, so turn off the
3760 // D3D11 and D2D.
3761 if (gfxConfig::IsEnabled(Feature::DIRECT2D)) {
3762 gfxConfig::GetFeature(Feature::DIRECT2D)
3763 .ForceDisable(aStatus, aMessage, aFailureId);
3765 if (gfxConfig::IsEnabled(Feature::D3D11_COMPOSITING)) {
3766 gfxConfig::GetFeature(Feature::D3D11_COMPOSITING)
3767 .ForceDisable(aStatus, aMessage, aFailureId);
3769 #endif
3771 #if defined(MOZ_WIDGET_ANDROID) || defined(MOZ_WIDGET_GTK)
3772 // Before we disable OpenGL and HW_COMPOSITING, we should check if we can
3773 // fallback from WebRender to Software WebRender + OpenGL compositing.
3774 if (swglFallbackAllowed && gfxVars::AllowSoftwareWebRenderOGL() &&
3775 gfxConfig::IsEnabled(Feature::OPENGL_COMPOSITING) &&
3776 !gfxVars::UseSoftwareWebRender()) {
3777 // Fallback to Software WebRender + OpenGL compositing.
3778 gfxCriticalNote << "Fallback WR to SW-WR + OpenGL";
3779 gfxVars::SetUseSoftwareWebRender(true);
3780 return true;
3782 #endif
3783 // Android does not want to fallback to SW-WR.
3784 #ifdef MOZ_WIDGET_GTK
3785 if (swglFallbackAllowed && gfxVars::AllowSoftwareWebRenderOGL() &&
3786 gfxVars::UseSoftwareWebRender()) {
3787 // Fallback from Software WebRender + OpenGL to Software WebRender.
3788 gfxCriticalNote << "Fallback SW-WR + OpenGL to SW-WR";
3789 gfxVars::SetAllowSoftwareWebRenderOGL(false);
3790 return true;
3792 #endif
3794 #ifndef MOZ_WIDGET_ANDROID
3795 // Non-Android wants to fallback to Software WebRender or Basic. Android wants
3796 // to fallback to OpenGL.
3797 if (gfxConfig::IsEnabled(Feature::HW_COMPOSITING)) {
3798 gfxConfig::GetFeature(Feature::HW_COMPOSITING)
3799 .ForceDisable(aStatus, aMessage, aFailureId);
3801 #endif
3803 if (StaticPrefs::gfx_webrender_fallback_software_AtStartup() &&
3804 swglFallbackAllowed && !gfxVars::UseSoftwareWebRender()) {
3805 // Fallback from WebRender to Software WebRender.
3806 gfxCriticalNote << "Fallback WR to SW-WR";
3807 gfxVars::SetUseSoftwareWebRender(true);
3808 return true;
3811 if (!gfxVars::UseSoftwareWebRender()) {
3812 // Software WebRender may be disabled due to a startup issue with the
3813 // blocklist, despite it being our only fallback option based on the prefs.
3814 // If WebRender is unable to be initialized, this means that user would
3815 // otherwise get stuck with WebRender. As such, force a switch to Software
3816 // WebRender in this case.
3817 gfxCriticalNoteOnce << "Fallback WR to SW-WR, forced";
3818 gfxVars::SetUseSoftwareWebRender(true);
3819 return true;
3822 if (aCrashAfterFinalFallback) {
3823 MOZ_CRASH("Fallback configurations exhausted");
3826 // Continue using Software WebRender (disabled fallback to Basic).
3827 gfxCriticalNoteOnce << "Fallback remains SW-WR";
3828 return false;
3831 /* static */
3832 void gfxPlatform::DisableGPUProcess() {
3833 if (gfxVars::RemoteCanvasEnabled() &&
3834 !StaticPrefs::gfx_canvas_remote_allow_in_parent_AtStartup()) {
3835 gfxConfig::Disable(
3836 Feature::REMOTE_CANVAS, FeatureStatus::UnavailableNoGpuProcess,
3837 "Disabled by GPU process disabled",
3838 "FEATURE_REMOTE_CANVAS_DISABLED_BY_GPU_PROCESS_DISABLED"_ns);
3839 gfxVars::SetRemoteCanvasEnabled(false);
3842 if (kIsAndroid) {
3843 // On android, enable out-of-process WebGL only when GPU process exists.
3844 gfxVars::SetAllowWebglOop(false);
3845 // On android, enable accelerated canvas only when GPU process exists.
3846 gfxVars::SetUseAcceleratedCanvas2D(false);
3847 gfxConfig::Disable(Feature::ACCELERATED_CANVAS2D, FeatureStatus::Blocked,
3848 "Disabled by GPU Process disabled",
3849 "FEATURE_FAILURE_DISABLED_BY_GPU_PROCESS_DISABLED"_ns);
3852 RemoteTextureMap::Init();
3853 // We need to initialize the parent process to prepare for WebRender if we
3854 // did not end up disabling it, despite losing the GPU process.
3855 wr::RenderThread::Start(GPUProcessManager::Get()->AllocateNamespace());
3856 gfx::CanvasRenderThread::Start();
3857 image::ImageMemoryReporter::InitForWebRender();
3860 /* static */ void gfxPlatform::DisableRemoteCanvas() {
3861 if (!gfxVars::RemoteCanvasEnabled()) {
3862 return;
3864 gfxConfig::ForceDisable(Feature::REMOTE_CANVAS, FeatureStatus::Failed,
3865 "Disabled by runtime error",
3866 "FEATURE_REMOTE_CANVAS_RUNTIME_ERROR"_ns);
3867 gfxVars::SetRemoteCanvasEnabled(false);
3870 void gfxPlatform::FetchAndImportContentDeviceData() {
3871 MOZ_ASSERT(XRE_IsContentProcess());
3873 if (gContentDeviceInitData) {
3874 ImportContentDeviceData(*gContentDeviceInitData);
3875 return;
3878 mozilla::dom::ContentChild* cc = mozilla::dom::ContentChild::GetSingleton();
3880 mozilla::gfx::ContentDeviceData data;
3881 cc->SendGetGraphicsDeviceInitData(&data);
3883 ImportContentDeviceData(data);
3886 void gfxPlatform::ImportContentDeviceData(
3887 const mozilla::gfx::ContentDeviceData& aData) {
3888 MOZ_ASSERT(XRE_IsContentProcess());
3890 const DevicePrefs& prefs = aData.prefs();
3891 gfxConfig::Inherit(Feature::HW_COMPOSITING, prefs.hwCompositing());
3892 gfxConfig::Inherit(Feature::OPENGL_COMPOSITING, prefs.oglCompositing());
3895 void gfxPlatform::BuildContentDeviceData(
3896 mozilla::gfx::ContentDeviceData* aOut) {
3897 MOZ_ASSERT(XRE_IsParentProcess());
3899 // Make sure our settings are synchronized from the GPU process.
3900 DebugOnly<nsresult> rv = GPUProcessManager::Get()->EnsureGPUReady();
3901 MOZ_ASSERT(rv != NS_ERROR_ILLEGAL_DURING_SHUTDOWN);
3903 aOut->prefs().hwCompositing() = gfxConfig::GetValue(Feature::HW_COMPOSITING);
3904 aOut->prefs().oglCompositing() =
3905 gfxConfig::GetValue(Feature::OPENGL_COMPOSITING);
3908 void gfxPlatform::ImportGPUDeviceData(
3909 const mozilla::gfx::GPUDeviceData& aData) {
3910 MOZ_ASSERT(XRE_IsParentProcess());
3912 gfxConfig::ImportChange(Feature::OPENGL_COMPOSITING, aData.oglCompositing());
3915 bool gfxPlatform::SupportsApzTouchInput() const {
3916 return dom::TouchEvent::PrefEnabled(nullptr);
3919 bool gfxPlatform::SupportsApzDragInput() const {
3920 return StaticPrefs::apz_drag_enabled();
3923 bool gfxPlatform::SupportsApzKeyboardInput() const {
3924 return StaticPrefs::apz_keyboard_enabled_AtStartup();
3927 bool gfxPlatform::SupportsApzAutoscrolling() const {
3928 return StaticPrefs::apz_autoscroll_enabled();
3931 bool gfxPlatform::SupportsApzZooming() const {
3932 return StaticPrefs::apz_allow_zooming();
3935 void gfxPlatform::InitOpenGLConfig() {
3936 #ifdef XP_WIN
3937 // Don't enable by default on Windows, since it could show up in about:support
3938 // even though it'll never get used. Only attempt if user enables the pref
3939 if (!Preferences::GetBool("layers.prefer-opengl")) {
3940 return;
3942 #endif
3944 FeatureState& openGLFeature =
3945 gfxConfig::GetFeature(Feature::OPENGL_COMPOSITING);
3947 // Check to see hw comp supported
3948 if (!gfxConfig::IsEnabled(Feature::HW_COMPOSITING)) {
3949 openGLFeature.DisableByDefault(FeatureStatus::Unavailable,
3950 "Hardware compositing is disabled",
3951 "FEATURE_FAILURE_OPENGL_NEED_HWCOMP"_ns);
3952 return;
3955 #ifdef XP_WIN
3956 openGLFeature.SetDefaultFromPref(
3957 StaticPrefs::GetPrefName_layers_prefer_opengl(), true,
3958 StaticPrefs::GetPrefDefault_layers_prefer_opengl());
3959 #else
3960 openGLFeature.EnableByDefault();
3961 #endif
3963 // When layers acceleration is force-enabled, enable it even for blocklisted
3964 // devices.
3965 if (StaticPrefs::
3966 layers_acceleration_force_enabled_AtStartup_DoNotUseDirectly()) {
3967 openGLFeature.UserForceEnable("Force-enabled by pref");
3968 return;
3971 nsCString message;
3972 nsCString failureId;
3973 if (!IsGfxInfoStatusOkay(nsIGfxInfo::FEATURE_OPENGL_LAYERS, &message,
3974 failureId)) {
3975 openGLFeature.Disable(FeatureStatus::Blocklisted, message.get(), failureId);
3979 bool gfxPlatform::IsGfxInfoStatusOkay(int32_t aFeature, nsCString* aOutMessage,
3980 nsCString& aFailureId) {
3981 nsCOMPtr<nsIGfxInfo> gfxInfo = components::GfxInfo::Service();
3982 if (!gfxInfo) {
3983 return true;
3986 int32_t status;
3987 if (NS_SUCCEEDED(gfxInfo->GetFeatureStatus(aFeature, aFailureId, &status)) &&
3988 status != nsIGfxInfo::FEATURE_STATUS_OK) {
3989 aOutMessage->AssignLiteral("#BLOCKLIST_");
3990 aOutMessage->AppendASCII(aFailureId.get());
3991 return false;
3994 return true;