no bug - Bumping Firefox l10n changesets r=release a=l10n-bump DONTBUILD CLOSED TREE
[gecko.git] / gfx / thebes / gfxPlatform.cpp
blobccc76def7edcb16509abbe44bfea0ceddc25eca6
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/glean/GleanMetrics.h"
41 #include "mozilla/TimeStamp.h"
42 #include "mozilla/Unused.h"
43 #include "mozilla/IntegerPrintfMacros.h"
44 #include "mozilla/Base64.h"
45 #include "mozilla/VsyncDispatcher.h"
47 #include "mozilla/Logging.h"
48 #include "mozilla/Components.h"
49 #include "nsAppRunner.h"
50 #include "nsAppDirectoryServiceDefs.h"
51 #include "nsCSSProps.h"
52 #include "nsContentUtils.h"
54 #include "gfxCrashReporterUtils.h"
55 #include "gfxPlatform.h"
56 #include "gfxPlatformWorker.h"
58 #include "gfxBlur.h"
59 #include "gfxEnv.h"
60 #include "gfxTextRun.h"
61 #include "gfxUserFontSet.h"
62 #include "gfxConfig.h"
63 #include "GfxDriverInfo.h"
64 #include "VRProcessManager.h"
65 #include "VRThread.h"
67 #ifdef XP_WIN
68 # include <process.h>
69 # define getpid _getpid
70 #else
71 # include <unistd.h>
72 #endif
74 #include "nsXULAppAPI.h"
75 #include "nsIXULAppInfo.h"
76 #include "nsDirectoryServiceUtils.h"
77 #include "nsDirectoryServiceDefs.h"
79 #if defined(XP_WIN)
80 # include "gfxWindowsPlatform.h"
81 # include "mozilla/widget/WinWindowOcclusionTracker.h"
82 #elif defined(XP_MACOSX)
83 # include "gfxPlatformMac.h"
84 # include "gfxQuartzSurface.h"
85 #elif defined(MOZ_WIDGET_GTK)
86 # include "gfxPlatformGtk.h"
87 #elif defined(ANDROID)
88 # include "gfxAndroidPlatform.h"
89 #endif
90 #if defined(MOZ_WIDGET_ANDROID)
91 # include "mozilla/jni/Utils.h" // for IsFennec
92 #endif
94 #ifdef XP_WIN
95 # include "mozilla/WindowsVersion.h"
96 # include "WinUtils.h"
97 #endif
99 #include "nsGkAtoms.h"
100 #include "gfxPlatformFontList.h"
101 #include "gfxContext.h"
102 #include "gfxImageSurface.h"
103 #include "nsUnicodeProperties.h"
104 #include "harfbuzz/hb.h"
105 #include "gfxGraphiteShaper.h"
106 #include "gfx2DGlue.h"
107 #include "gfxGradientCache.h"
108 #include "gfxUtils.h" // for NextPowerOfTwo
109 #include "gfxFontMissingGlyphs.h"
111 #include "nsExceptionHandler.h"
112 #include "nsServiceManagerUtils.h"
113 #include "nsTArray.h"
114 #include "nsIObserverService.h"
115 #include "mozilla/widget/Screen.h"
116 #include "mozilla/widget/ScreenManager.h"
117 #include "MainThreadUtils.h"
119 #include "nsWeakReference.h"
121 #include "cairo.h"
122 #include "qcms.h"
124 #include "imgITools.h"
126 #include "nsCRT.h"
127 #include "GLContext.h"
128 #include "GLContextProvider.h"
129 #include "mozilla/gfx/Logging.h"
131 #ifdef __GNUC__
132 # pragma GCC diagnostic push
133 # pragma GCC diagnostic ignored "-Wshadow"
134 #endif
135 #include "skia/include/core/SkGraphics.h"
136 #ifdef MOZ_ENABLE_FREETYPE
137 # include "skia/include/ports/SkTypeface_cairo.h"
138 #endif
139 #include "mozilla/gfx/SkMemoryReporter.h"
140 #ifdef __GNUC__
141 # pragma GCC diagnostic pop // -Wshadow
142 #endif
143 static const uint32_t kDefaultGlyphCacheSize = -1;
145 #include "mozilla/Preferences.h"
146 #include "mozilla/Assertions.h"
147 #include "mozilla/Atomics.h"
148 #include "mozilla/Attributes.h"
149 #include "mozilla/Mutex.h"
151 #include "nsAlgorithm.h"
152 #include "nsIGfxInfo.h"
153 #include "nsIXULRuntime.h"
154 #include "VsyncSource.h"
155 #include "SoftwareVsyncSource.h"
156 #include "nscore.h" // for NS_FREE_PERMANENT_DATA
157 #include "mozilla/dom/ContentChild.h"
158 #include "mozilla/dom/ContentParent.h"
159 #include "mozilla/dom/TouchEvent.h"
160 #include "gfxVR.h"
161 #include "VRManager.h"
162 #include "VRManagerChild.h"
163 #include "mozilla/gfx/GPUParent.h"
164 #include "prsystem.h"
166 #include "mozilla/gfx/2D.h"
167 #include "mozilla/gfx/SourceSurfaceCairo.h"
169 using namespace mozilla;
170 using namespace mozilla::layers;
171 using namespace mozilla::gl;
172 using namespace mozilla::gfx;
174 gfxPlatform* gPlatform = nullptr;
175 static bool gEverInitialized = false;
177 const ContentDeviceData* gContentDeviceInitData = nullptr;
178 Maybe<nsTArray<uint8_t>> gCMSOutputProfileData;
180 Atomic<bool, MemoryOrdering::ReleaseAcquire> gfxPlatform::gCMSInitialized;
181 CMSMode gfxPlatform::gCMSMode = CMSMode::Off;
183 // These two may point to the same profile
184 qcms_profile* gfxPlatform::gCMSOutputProfile = nullptr;
185 qcms_profile* gfxPlatform::gCMSsRGBProfile = nullptr;
187 qcms_transform* gfxPlatform::gCMSRGBTransform = nullptr;
188 qcms_transform* gfxPlatform::gCMSInverseRGBTransform = nullptr;
189 qcms_transform* gfxPlatform::gCMSRGBATransform = nullptr;
190 qcms_transform* gfxPlatform::gCMSBGRATransform = nullptr;
192 /// This override of the LogForwarder, initially used for the critical graphics
193 /// errors, is sending the log to the crash annotations as well, but only
194 /// if the capacity set with the method below is >= 2. We always retain the
195 /// very first critical message, and the latest capacity-1 messages are
196 /// rotated through. Note that we don't expect the total number of times
197 /// this gets called to be large - it is meant for critical errors only.
199 class CrashStatsLogForwarder : public mozilla::gfx::LogForwarder {
200 public:
201 explicit CrashStatsLogForwarder(CrashReporter::Annotation aKey);
202 void Log(const std::string& aString) override;
203 void CrashAction(LogReason aReason) override;
204 bool UpdateStringsVector(const std::string& aString) override;
206 LoggingRecord LoggingRecordCopy() override;
208 void SetCircularBufferSize(uint32_t aCapacity);
210 private:
211 // Helper for the Log()
212 void UpdateCrashReport();
214 private:
215 LoggingRecord mBuffer;
216 CrashReporter::Annotation mCrashCriticalKey;
217 uint32_t mMaxCapacity;
218 int32_t mIndex;
219 Mutex mMutex MOZ_UNANNOTATED;
222 CrashStatsLogForwarder::CrashStatsLogForwarder(CrashReporter::Annotation aKey)
223 : mCrashCriticalKey(aKey),
224 mMaxCapacity(0),
225 mIndex(-1),
226 mMutex("CrashStatsLogForwarder") {}
228 void CrashStatsLogForwarder::SetCircularBufferSize(uint32_t aCapacity) {
229 MutexAutoLock lock(mMutex);
231 mMaxCapacity = aCapacity;
232 mBuffer.reserve(static_cast<size_t>(aCapacity));
235 LoggingRecord CrashStatsLogForwarder::LoggingRecordCopy() {
236 MutexAutoLock lock(mMutex);
237 return mBuffer;
240 bool CrashStatsLogForwarder::UpdateStringsVector(const std::string& aString) {
241 // We want at least the first one and the last one. Otherwise, no point.
242 if (mMaxCapacity < 2) {
243 return false;
246 mIndex += 1;
247 MOZ_ASSERT(mIndex >= 0);
249 // index will count 0, 1, 2, ..., max-1, 1, 2, ..., max-1, 1, 2, ...
250 int32_t index = mIndex ? (mIndex - 1) % (mMaxCapacity - 1) + 1 : 0;
251 MOZ_ASSERT(index >= 0 && index < (int32_t)mMaxCapacity);
252 MOZ_ASSERT(index <= mIndex && index <= (int32_t)mBuffer.size());
254 double tStamp = (TimeStamp::NowLoRes() - TimeStamp::ProcessCreation())
255 .ToSecondsSigDigits();
257 // Checking for index >= mBuffer.size(), rather than index == mBuffer.size()
258 // just out of paranoia, but we know index <= mBuffer.size().
259 LoggingRecordEntry newEntry(mIndex, aString, tStamp);
260 if (index >= static_cast<int32_t>(mBuffer.size())) {
261 mBuffer.push_back(newEntry);
262 } else {
263 mBuffer[index] = newEntry;
265 return true;
268 void CrashStatsLogForwarder::UpdateCrashReport() {
269 std::stringstream message;
270 std::string logAnnotation;
272 switch (XRE_GetProcessType()) {
273 case GeckoProcessType_Default:
274 logAnnotation = "|[";
275 break;
276 case GeckoProcessType_Content:
277 logAnnotation = "|[C";
278 break;
279 case GeckoProcessType_GPU:
280 logAnnotation = "|[G";
281 break;
282 default:
283 logAnnotation = "|[X";
284 break;
287 for (auto& it : mBuffer) {
288 message << logAnnotation << std::get<0>(it) << "]" << std::get<1>(it)
289 << " (t=" << std::get<2>(it) << ") ";
292 nsCString reportString(message.str().c_str());
293 nsresult annotated =
294 CrashReporter::AnnotateCrashReport(mCrashCriticalKey, reportString);
296 if (annotated != NS_OK) {
297 printf("Crash Annotation %s: %s",
298 CrashReporter::AnnotationToString(mCrashCriticalKey),
299 message.str().c_str());
303 class LogForwarderEvent : public Runnable {
304 virtual ~LogForwarderEvent() = default;
306 public:
307 NS_INLINE_DECL_REFCOUNTING_INHERITED(LogForwarderEvent, Runnable)
309 explicit LogForwarderEvent(const nsCString& aMessage)
310 : mozilla::Runnable("LogForwarderEvent"), mMessage(aMessage) {}
312 NS_IMETHOD Run() override {
313 MOZ_ASSERT(NS_IsMainThread() &&
314 (XRE_IsContentProcess() || XRE_IsGPUProcess()));
316 if (XRE_IsContentProcess()) {
317 dom::ContentChild* cc = dom::ContentChild::GetSingleton();
318 Unused << cc->SendGraphicsError(mMessage);
319 } else if (XRE_IsGPUProcess()) {
320 GPUParent* gp = GPUParent::GetSingleton();
321 Unused << gp->SendGraphicsError(mMessage);
324 return NS_OK;
327 protected:
328 nsCString mMessage;
331 void CrashStatsLogForwarder::Log(const std::string& aString) {
332 MutexAutoLock lock(mMutex);
334 if (UpdateStringsVector(aString)) {
335 UpdateCrashReport();
338 // Add it to the parent strings
339 if (!XRE_IsParentProcess()) {
340 nsCString stringToSend(aString.c_str());
341 if (NS_IsMainThread()) {
342 if (XRE_IsContentProcess()) {
343 dom::ContentChild* cc = dom::ContentChild::GetSingleton();
344 Unused << cc->SendGraphicsError(stringToSend);
345 } else if (XRE_IsGPUProcess()) {
346 GPUParent* gp = GPUParent::GetSingleton();
347 Unused << gp->SendGraphicsError(stringToSend);
349 } else {
350 nsCOMPtr<nsIRunnable> r1 = new LogForwarderEvent(stringToSend);
351 NS_DispatchToMainThread(r1);
356 class CrashTelemetryEvent : public Runnable {
357 virtual ~CrashTelemetryEvent() = default;
359 public:
360 NS_INLINE_DECL_REFCOUNTING_INHERITED(CrashTelemetryEvent, Runnable)
362 explicit CrashTelemetryEvent(uint32_t aReason)
363 : mozilla::Runnable("CrashTelemetryEvent"), mReason(aReason) {}
365 NS_IMETHOD Run() override {
366 MOZ_ASSERT(NS_IsMainThread());
367 Telemetry::Accumulate(Telemetry::GFX_CRASH, mReason);
368 return NS_OK;
371 protected:
372 uint32_t mReason;
375 void CrashStatsLogForwarder::CrashAction(LogReason aReason) {
376 #ifndef RELEASE_OR_BETA
377 // Non-release builds crash by default, but will use telemetry
378 // if this environment variable is present.
379 static bool useTelemetry = gfxEnv::MOZ_GFX_CRASH_TELEMETRY();
380 #else
381 // Release builds use telemetry by default, but will crash instead
382 // if this environment variable is present.
383 static bool useTelemetry = !gfxEnv::MOZ_GFX_CRASH_MOZ_CRASH();
384 #endif
386 if (useTelemetry) {
387 // The callers need to assure that aReason is in the range
388 // that the telemetry call below supports.
389 if (NS_IsMainThread()) {
390 Telemetry::Accumulate(Telemetry::GFX_CRASH, (uint32_t)aReason);
391 } else {
392 nsCOMPtr<nsIRunnable> r1 = new CrashTelemetryEvent((uint32_t)aReason);
393 NS_DispatchToMainThread(r1);
395 } else {
396 // ignoring aReason, we can get the information we need from the stack
397 MOZ_CRASH("GFX_CRASH");
401 #define GFX_DOWNLOADABLE_FONTS_ENABLED "gfx.downloadable_fonts.enabled"
403 #define GFX_PREF_FALLBACK_USE_CMAPS \
404 "gfx.font_rendering.fallback.always_use_cmaps"
406 #define GFX_PREF_OPENTYPE_SVG "gfx.font_rendering.opentype_svg.enabled"
408 #define GFX_PREF_WORD_CACHE_CHARLIMIT "gfx.font_rendering.wordcache.charlimit"
409 #define GFX_PREF_WORD_CACHE_MAXENTRIES "gfx.font_rendering.wordcache.maxentries"
411 #define GFX_PREF_GRAPHITE_SHAPING "gfx.font_rendering.graphite.enabled"
412 #if defined(XP_MACOSX)
413 # define GFX_PREF_CORETEXT_SHAPING "gfx.font_rendering.coretext.enabled"
414 #endif
416 #define FONT_VARIATIONS_PREF "layout.css.font-variations.enabled"
418 static const char* kObservedPrefs[] = {"gfx.downloadable_fonts.",
419 "gfx.font_rendering.", nullptr};
421 static void FontPrefChanged(const char* aPref, void* aData) {
422 MOZ_ASSERT(aPref);
423 NS_ASSERTION(gfxPlatform::GetPlatform(), "the singleton instance has gone");
424 gfxPlatform::GetPlatform()->FontsPrefsChanged(aPref);
427 void gfxPlatform::OnMemoryPressure(layers::MemoryPressureReason aWhy) {
428 Factory::PurgeAllCaches();
429 gfxGradientCache::PurgeAllCaches();
430 gfxFontMissingGlyphs::Purge();
431 PurgeSkiaFontCache();
432 if (XRE_IsParentProcess()) {
433 layers::CompositorManagerChild* manager =
434 CompositorManagerChild::GetInstance();
435 if (manager) {
436 manager->SendNotifyMemoryPressure();
441 gfxPlatform::gfxPlatform()
442 : mAzureCanvasBackendCollector(this, &gfxPlatform::GetAzureBackendInfo),
443 mApzSupportCollector(this, &gfxPlatform::GetApzSupportInfo),
444 mFrameStatsCollector(this, &gfxPlatform::GetFrameStats),
445 mCMSInfoCollector(this, &gfxPlatform::GetCMSSupportInfo),
446 mDisplayInfoCollector(this, &gfxPlatform::GetDisplayInfo),
447 mOverlayInfoCollector(this, &gfxPlatform::GetOverlayInfo),
448 mSwapChainInfoCollector(this, &gfxPlatform::GetSwapChainInfo),
449 mCompositorBackend(layers::LayersBackend::LAYERS_NONE),
450 mScreenDepth(0) {
451 mAllowDownloadableFonts = UNINITIALIZED_VALUE;
453 InitBackendPrefs(GetBackendPrefs());
454 VRManager::ManagerInit();
457 gfxPlatform* gfxPlatform::GetPlatform() {
458 if (!gPlatform) {
459 MOZ_RELEASE_ASSERT(!XRE_IsContentProcess(),
460 "Content Process should have called InitChild() before "
461 "first GetPlatform()");
462 Init();
464 return gPlatform;
467 bool gfxPlatform::Initialized() { return !!gPlatform; }
469 /* static */
470 void gfxPlatform::InitChild(const ContentDeviceData& aData) {
471 MOZ_ASSERT(XRE_IsContentProcess());
472 MOZ_ASSERT(!gPlatform,
473 "InitChild() should be called before first GetPlatform()");
474 // Make the provided initial ContentDeviceData available to the init
475 // routines.
476 gContentDeviceInitData = &aData;
477 Init();
478 gContentDeviceInitData = nullptr;
481 #define WR_DEBUG_PREF "gfx.webrender.debug"
483 static void SwapIntervalPrefChangeCallback(const char* aPrefName, void*) {
484 bool egl = Preferences::GetBool("gfx.swap-interval.egl", false);
485 bool glx = Preferences::GetBool("gfx.swap-interval.glx", false);
486 gfxVars::SetSwapIntervalEGL(egl);
487 gfxVars::SetSwapIntervalGLX(glx);
490 static void WebRendeProfilerUIPrefChangeCallback(const char* aPrefName, void*) {
491 nsCString uiString;
492 if (NS_SUCCEEDED(Preferences::GetCString("gfx.webrender.debug.profiler-ui",
493 uiString))) {
494 gfxVars::SetWebRenderProfilerUI(uiString);
498 // List of boolean dynamic parameter for WebRender.
500 // The parameters in this list are:
501 // - The pref name.
502 // - The BoolParameter enum variant (see webrender_api/src/lib.rs)
503 // - A default value.
504 #define WR_BOOL_PARAMETER_LIST(_) \
505 _("gfx.webrender.batched-texture-uploads", \
506 wr::BoolParameter::BatchedUploads, true) \
507 _("gfx.webrender.draw-calls-for-texture-copy", \
508 wr::BoolParameter::DrawCallsForTextureCopy, true) \
509 _("gfx.webrender.pbo-uploads", wr::BoolParameter::PboUploads, true) \
510 _("gfx.webrender.multithreading", wr::BoolParameter::Multithreading, true)
512 static void WebRenderBoolParameterChangeCallback(const char*, void*) {
513 uint32_t bits = 0;
515 #define WR_BOOL_PARAMETER(name, key, default_val) \
516 if (Preferences::GetBool(name, default_val)) { \
517 bits |= 1 << (uint32_t)key; \
520 WR_BOOL_PARAMETER_LIST(WR_BOOL_PARAMETER)
521 #undef WR_BOOL_PARAMETER
523 gfx::gfxVars::SetWebRenderBoolParameters(bits);
526 static void RegisterWebRenderBoolParamCallback() {
527 #define WR_BOOL_PARAMETER(name, _key, _default_val) \
528 Preferences::RegisterCallback(WebRenderBoolParameterChangeCallback, name);
530 WR_BOOL_PARAMETER_LIST(WR_BOOL_PARAMETER)
531 #undef WR_BOOL_PARAMETER
533 WebRenderBoolParameterChangeCallback(nullptr, nullptr);
536 static void WebRenderDebugPrefChangeCallback(const char* aPrefName, void*) {
537 wr::DebugFlags flags{0};
538 #define GFX_WEBRENDER_DEBUG(suffix, bit) \
539 if (Preferences::GetBool(WR_DEBUG_PREF suffix, false)) { \
540 flags |= (bit); \
543 GFX_WEBRENDER_DEBUG(".profiler", wr::DebugFlags::PROFILER_DBG)
544 GFX_WEBRENDER_DEBUG(".render-targets", wr::DebugFlags::RENDER_TARGET_DBG)
545 GFX_WEBRENDER_DEBUG(".texture-cache", wr::DebugFlags::TEXTURE_CACHE_DBG)
546 GFX_WEBRENDER_DEBUG(".gpu-time-queries", wr::DebugFlags::GPU_TIME_QUERIES)
547 GFX_WEBRENDER_DEBUG(".gpu-sample-queries", wr::DebugFlags::GPU_SAMPLE_QUERIES)
548 GFX_WEBRENDER_DEBUG(".disable-batching", wr::DebugFlags::DISABLE_BATCHING)
549 GFX_WEBRENDER_DEBUG(".epochs", wr::DebugFlags::EPOCHS)
550 GFX_WEBRENDER_DEBUG(".smart-profiler", wr::DebugFlags::SMART_PROFILER)
551 GFX_WEBRENDER_DEBUG(".echo-driver-messages",
552 wr::DebugFlags::ECHO_DRIVER_MESSAGES)
553 GFX_WEBRENDER_DEBUG(".show-overdraw", wr::DebugFlags::SHOW_OVERDRAW)
554 GFX_WEBRENDER_DEBUG(".gpu-cache", wr::DebugFlags::GPU_CACHE_DBG)
555 GFX_WEBRENDER_DEBUG(".texture-cache.clear-evicted",
556 wr::DebugFlags::TEXTURE_CACHE_DBG_CLEAR_EVICTED)
557 GFX_WEBRENDER_DEBUG(".picture-caching", wr::DebugFlags::PICTURE_CACHING_DBG)
558 GFX_WEBRENDER_DEBUG(".force-picture-invalidation",
559 wr::DebugFlags::FORCE_PICTURE_INVALIDATION)
560 GFX_WEBRENDER_DEBUG(".primitives", wr::DebugFlags::PRIMITIVE_DBG)
561 // Bit 18 is for the zoom display, which requires the mouse position and thus
562 // currently only works in wrench.
563 GFX_WEBRENDER_DEBUG(".small-screen", wr::DebugFlags::SMALL_SCREEN)
564 GFX_WEBRENDER_DEBUG(".disable-opaque-pass",
565 wr::DebugFlags::DISABLE_OPAQUE_PASS)
566 GFX_WEBRENDER_DEBUG(".disable-alpha-pass", wr::DebugFlags::DISABLE_ALPHA_PASS)
567 GFX_WEBRENDER_DEBUG(".disable-clip-masks", wr::DebugFlags::DISABLE_CLIP_MASKS)
568 GFX_WEBRENDER_DEBUG(".disable-text-prims", wr::DebugFlags::DISABLE_TEXT_PRIMS)
569 GFX_WEBRENDER_DEBUG(".disable-gradient-prims",
570 wr::DebugFlags::DISABLE_GRADIENT_PRIMS)
571 GFX_WEBRENDER_DEBUG(".obscure-images", wr::DebugFlags::OBSCURE_IMAGES)
572 GFX_WEBRENDER_DEBUG(".glyph-flashing", wr::DebugFlags::GLYPH_FLASHING)
573 GFX_WEBRENDER_DEBUG(".capture-profiler", wr::DebugFlags::PROFILER_CAPTURE)
574 GFX_WEBRENDER_DEBUG(".window-visibility",
575 wr::DebugFlags::WINDOW_VISIBILITY_DBG)
576 GFX_WEBRENDER_DEBUG(".restrict-blob-size", wr::DebugFlags::RESTRICT_BLOB_SIZE)
577 #undef GFX_WEBRENDER_DEBUG
578 gfx::gfxVars::SetWebRenderDebugFlags(flags._0);
581 static void WebRenderQualityPrefChangeCallback(const char* aPref, void*) {
582 gfxPlatform::GetPlatform()->UpdateForceSubpixelAAWherePossible();
585 static void WebRenderBatchingPrefChangeCallback(const char* aPrefName, void*) {
586 uint32_t count = Preferences::GetUint(
587 StaticPrefs::GetPrefName_gfx_webrender_batching_lookback(), 10);
589 gfx::gfxVars::SetWebRenderBatchingLookback(count);
592 static void WebRenderBlobTileSizePrefChangeCallback(const char* aPrefName,
593 void*) {
594 uint32_t tileSize = Preferences::GetUint(
595 StaticPrefs::GetPrefName_gfx_webrender_blob_tile_size(), 256);
596 gfx::gfxVars::SetWebRenderBlobTileSize(tileSize);
599 static void WebRenderUploadThresholdPrefChangeCallback(const char* aPrefName,
600 void*) {
601 int value = Preferences::GetInt(
602 StaticPrefs::GetPrefName_gfx_webrender_batched_upload_threshold(),
603 512 * 512);
605 gfxVars::SetWebRenderBatchedUploadThreshold(value);
608 static uint32_t GetSkiaGlyphCacheSize() {
609 // Only increase font cache size on non-android to save memory.
610 #if !defined(MOZ_WIDGET_ANDROID)
611 // 10mb as the default pref cache size on desktop due to talos perf tweaking.
612 // Chromium uses 20mb and skia default uses 2mb.
613 // We don't need to change the font cache count since we usually
614 // cache thrash due to asian character sets in talos.
615 // Only increase memory on the content process
616 uint32_t cacheSize =
617 StaticPrefs::gfx_content_skia_font_cache_size_AtStartup() * 1024 * 1024;
618 if (mozilla::BrowserTabsRemoteAutostart()) {
619 return XRE_IsContentProcess() ? cacheSize : kDefaultGlyphCacheSize;
622 return cacheSize;
623 #else
624 return kDefaultGlyphCacheSize;
625 #endif // MOZ_WIDGET_ANDROID
628 class WebRenderMemoryReporter final : public nsIMemoryReporter {
629 public:
630 NS_DECL_ISUPPORTS
631 NS_DECL_NSIMEMORYREPORTER
633 private:
634 ~WebRenderMemoryReporter() = default;
637 // Memory reporter for WebRender.
639 // The reporting within WebRender is manual and incomplete. We could do a much
640 // more thorough job by depending on the malloc_size_of crate, but integrating
641 // that into WebRender is tricky [1].
643 // So the idea is to start with manual reporting for the large allocations
644 // detected by DMD, and see how much that can cover in practice (which may
645 // require a few rounds of iteration). If that approach turns out to be
646 // fundamentally insufficient, we can either duplicate more of the
647 // malloc_size_of functionality in WebRender, or deal with the complexity of a
648 // gecko-only crate dependency.
650 // [1] See https://bugzilla.mozilla.org/show_bug.cgi?id=1480293#c1
651 struct WebRenderMemoryReporterHelper {
652 WebRenderMemoryReporterHelper(nsIHandleReportCallback* aCallback,
653 nsISupports* aData)
654 : mCallback(aCallback), mData(aData) {}
655 nsCOMPtr<nsIHandleReportCallback> mCallback;
656 nsCOMPtr<nsISupports> mData;
658 void Report(size_t aBytes, const char* aName) const {
659 nsPrintfCString path("explicit/gfx/webrender/%s", aName);
660 nsCString desc("CPU heap memory used by WebRender"_ns);
661 ReportInternal(aBytes, path, desc, nsIMemoryReporter::KIND_HEAP);
664 void ReportTexture(size_t aBytes, const char* aName) const {
665 nsPrintfCString path("gfx/webrender/textures/%s", aName);
666 nsCString desc("GPU texture memory used by WebRender"_ns);
667 ReportInternal(aBytes, path, desc, nsIMemoryReporter::KIND_OTHER);
670 void ReportTotalGPUBytes(size_t aBytes) const {
671 nsCString path("gfx/webrender/total-gpu-bytes"_ns);
672 nsCString desc(nsLiteralCString(
673 "Total GPU bytes used by WebRender (should match textures/ sum)"));
674 ReportInternal(aBytes, path, desc, nsIMemoryReporter::KIND_OTHER);
677 void ReportInternal(size_t aBytes, nsACString& aPath, nsACString& aDesc,
678 int32_t aKind) const {
679 // Generally, memory reporters pass the empty string as the process name to
680 // indicate "current process". However, if we're using a GPU process, the
681 // measurements will actually take place in that process, and it's easier to
682 // just note that here rather than trying to invoke the memory reporter in
683 // the GPU process.
684 nsAutoCString processName;
685 if (gfxConfig::IsEnabled(Feature::GPU_PROCESS)) {
686 GPUParent::GetGPUProcessName(processName);
689 mCallback->Callback(processName, aPath, aKind,
690 nsIMemoryReporter::UNITS_BYTES, aBytes, aDesc, mData);
694 static void FinishAsyncMemoryReport() {
695 nsCOMPtr<nsIMemoryReporterManager> imgr =
696 do_GetService("@mozilla.org/memory-reporter-manager;1");
697 if (imgr) {
698 imgr->EndReport();
702 // clang-format off
703 // (For some reason, clang-format gets the second macro right, but totally mangles the first).
704 #define REPORT_INTERNER(id) \
705 helper.Report(aReport.interning.interners.id, \
706 "interning/" #id "/interners");
707 // clang-format on
709 #define REPORT_DATA_STORE(id) \
710 helper.Report(aReport.interning.data_stores.id, \
711 "interning/" #id "/data-stores");
713 NS_IMPL_ISUPPORTS(WebRenderMemoryReporter, nsIMemoryReporter)
715 NS_IMETHODIMP
716 WebRenderMemoryReporter::CollectReports(nsIHandleReportCallback* aHandleReport,
717 nsISupports* aData, bool aAnonymize) {
718 MOZ_ASSERT(XRE_IsParentProcess());
719 MOZ_ASSERT(NS_IsMainThread());
720 layers::CompositorManagerChild* manager =
721 CompositorManagerChild::GetInstance();
722 if (!manager) {
723 FinishAsyncMemoryReport();
724 return NS_OK;
727 WebRenderMemoryReporterHelper helper(aHandleReport, aData);
728 manager->SendReportMemory(
729 [=](wr::MemoryReport aReport) {
730 // CPU Memory.
731 helper.Report(aReport.clip_stores, "clip-stores");
732 helper.Report(aReport.gpu_cache_metadata, "gpu-cache/metadata");
733 helper.Report(aReport.gpu_cache_cpu_mirror, "gpu-cache/cpu-mirror");
734 helper.Report(aReport.render_tasks, "render-tasks");
735 helper.Report(aReport.hit_testers, "hit-testers");
736 helper.Report(aReport.fonts, "resource-cache/fonts");
737 helper.Report(aReport.weak_fonts, "resource-cache/weak-fonts");
738 helper.Report(aReport.images, "resource-cache/images");
739 helper.Report(aReport.rasterized_blobs,
740 "resource-cache/rasterized-blobs");
741 helper.Report(aReport.texture_cache_structures,
742 "texture-cache/structures");
743 helper.Report(aReport.shader_cache, "shader-cache");
744 helper.Report(aReport.display_list, "display-list");
745 helper.Report(aReport.swgl, "swgl");
746 helper.Report(aReport.upload_staging_memory, "upload-stagin-memory");
748 WEBRENDER_FOR_EACH_INTERNER(REPORT_INTERNER);
749 WEBRENDER_FOR_EACH_INTERNER(REPORT_DATA_STORE);
751 // GPU Memory.
752 helper.ReportTexture(aReport.gpu_cache_textures, "gpu-cache");
753 helper.ReportTexture(aReport.vertex_data_textures, "vertex-data");
754 helper.ReportTexture(aReport.render_target_textures, "render-targets");
755 helper.ReportTexture(aReport.depth_target_textures, "depth-targets");
756 helper.ReportTexture(aReport.picture_tile_textures, "picture-tiles");
757 helper.ReportTexture(aReport.atlas_textures, "texture-cache/atlas");
758 helper.ReportTexture(aReport.standalone_textures,
759 "texture-cache/standalone");
760 helper.ReportTexture(aReport.texture_upload_pbos,
761 "texture-upload-pbos");
762 helper.ReportTexture(aReport.swap_chain, "swap-chains");
763 helper.ReportTexture(aReport.render_texture_hosts,
764 "render-texture-hosts");
765 helper.ReportTexture(aReport.upload_staging_textures,
766 "upload-staging-textures");
768 FinishAsyncMemoryReport();
770 [](mozilla::ipc::ResponseRejectReason&& aReason) {
771 FinishAsyncMemoryReport();
774 return NS_OK;
777 #undef REPORT_INTERNER
778 #undef REPORT_DATA_STORE
780 std::atomic<int8_t> gfxPlatform::sHasVariationFontSupport = -1;
782 bool gfxPlatform::HasVariationFontSupport() {
783 // We record the status here: 0 for not supported, 1 for supported.
784 if (sHasVariationFontSupport < 0) {
785 // It doesn't actually matter if we race with another thread setting this,
786 // as any thread will set it to the same value.
787 #if defined(XP_WIN)
788 sHasVariationFontSupport = gfxWindowsPlatform::CheckVariationFontSupport();
789 #elif defined(XP_MACOSX)
790 sHasVariationFontSupport = gfxPlatformMac::CheckVariationFontSupport();
791 #elif defined(MOZ_WIDGET_GTK)
792 sHasVariationFontSupport = gfxPlatformGtk::CheckVariationFontSupport();
793 #elif defined(ANDROID)
794 sHasVariationFontSupport = gfxAndroidPlatform::CheckVariationFontSupport();
795 #else
796 # error "No gfxPlatform implementation available"
797 #endif
799 return sHasVariationFontSupport > 0;
802 void gfxPlatform::Init() {
803 MOZ_RELEASE_ASSERT(!XRE_IsGPUProcess(), "GFX: Not allowed in GPU process.");
804 MOZ_RELEASE_ASSERT(!XRE_IsRDDProcess(), "GFX: Not allowed in RDD process.");
805 MOZ_RELEASE_ASSERT(NS_IsMainThread(), "GFX: Not in main thread.");
807 if (gEverInitialized) {
808 MOZ_CRASH("Already started???");
810 gEverInitialized = true;
812 gfxVars::Initialize();
814 gfxConfig::Init();
816 if (XRE_IsParentProcess()) {
817 GPUProcessManager::Initialize();
818 RDDProcessManager::Initialize();
820 nsCOMPtr<nsIFile> file;
821 nsresult rv = NS_GetSpecialDirectory(NS_GRE_DIR, getter_AddRefs(file));
822 if (NS_FAILED(rv)) {
823 gfxVars::SetGREDirectory(nsString());
824 } else {
825 nsAutoString path;
826 file->GetPath(path);
827 gfxVars::SetGREDirectory(nsString(path));
831 if (XRE_IsParentProcess()) {
832 nsCOMPtr<nsIFile> profDir;
833 nsresult rv = NS_GetSpecialDirectory(NS_APP_PROFILE_DIR_STARTUP,
834 getter_AddRefs(profDir));
835 if (NS_FAILED(rv)) {
836 gfxVars::SetProfDirectory(nsString());
837 } else {
838 nsAutoString path;
839 profDir->GetPath(path);
840 gfxVars::SetProfDirectory(nsString(path));
843 nsAutoCString path;
844 Preferences::GetCString("layers.windowrecording.path", path);
845 gfxVars::SetLayersWindowRecordingPath(path);
847 if (gFxREmbedded) {
848 gfxVars::SetFxREmbedded(true);
852 // Drop a note in the crash report if we end up forcing an option that could
853 // destabilize things. New items should be appended at the end (of an
854 // existing or in a new section), so that we don't have to know the version to
855 // interpret these cryptic strings.
857 nsAutoCString forcedPrefs;
858 // D2D prefs
859 forcedPrefs.AppendPrintf(
860 "FP(D%d%d", StaticPrefs::gfx_direct2d_disabled_AtStartup(),
861 StaticPrefs::gfx_direct2d_force_enabled_AtStartup());
862 // Layers prefs
863 forcedPrefs.AppendPrintf(
864 "-L%d%d%d%d",
865 StaticPrefs::layers_amd_switchable_gfx_enabled_AtStartup(),
866 StaticPrefs::layers_acceleration_disabled_AtStartup_DoNotUseDirectly(),
867 StaticPrefs::
868 layers_acceleration_force_enabled_AtStartup_DoNotUseDirectly(),
869 StaticPrefs::layers_d3d11_force_warp_AtStartup());
870 // WebGL prefs
871 forcedPrefs.AppendPrintf(
872 "-W%d%d%d%d%d%d%d", StaticPrefs::webgl_angle_force_d3d11(),
873 StaticPrefs::webgl_angle_force_warp(), StaticPrefs::webgl_disabled(),
874 StaticPrefs::webgl_disable_angle(), StaticPrefs::webgl_dxgl_enabled(),
875 StaticPrefs::webgl_force_enabled(), StaticPrefs::webgl_msaa_force());
876 // Prefs that don't fit into any of the other sections
877 forcedPrefs.AppendPrintf("-T%d%d%d) ",
878 StaticPrefs::gfx_android_rgb16_force_AtStartup(),
879 StaticPrefs::gfx_canvas_accelerated(),
880 StaticPrefs::layers_force_shmem_tiles_AtStartup());
881 ScopedGfxFeatureReporter::AppNote(forcedPrefs);
884 InitMoz2DLogging();
886 /* Initialize the GfxInfo service.
887 * Note: we can't call functions on GfxInfo that depend
888 * on gPlatform until after it has been initialized
889 * below. GfxInfo initialization annotates our
890 * crash reports so we want to do it before
891 * we try to load any drivers and do device detection
892 * incase that code crashes. See bug #591561. */
893 nsCOMPtr<nsIGfxInfo> gfxInfo;
894 /* this currently will only succeed on Windows */
895 gfxInfo = components::GfxInfo::Service();
897 if (XRE_IsParentProcess()) {
898 // Some gfxVars must be initialized prior gPlatform for coherent results.
899 gfxVars::SetDXInterop2Blocked(IsDXInterop2Blocked());
900 gfxVars::SetDXNV12Blocked(IsDXNV12Blocked());
901 gfxVars::SetDXP010Blocked(IsDXP010Blocked());
902 gfxVars::SetDXP016Blocked(IsDXP016Blocked());
905 #if defined(XP_WIN)
906 gPlatform = new gfxWindowsPlatform;
907 #elif defined(XP_MACOSX)
908 gPlatform = new gfxPlatformMac;
909 #elif defined(MOZ_WIDGET_GTK)
910 gPlatform = new gfxPlatformGtk;
911 #elif defined(ANDROID)
912 gPlatform = new gfxAndroidPlatform;
913 #else
914 # error "No gfxPlatform implementation available"
915 #endif
916 gPlatform->PopulateScreenInfo();
917 gPlatform->InitAcceleration();
918 gPlatform->InitWebRenderConfig();
920 gPlatform->InitHardwareVideoConfig();
921 gPlatform->InitWebGLConfig();
922 gPlatform->InitWebGPUConfig();
923 gPlatform->InitWindowOcclusionConfig();
924 gPlatform->InitBackdropFilterConfig();
925 gPlatform->InitAcceleratedCanvas2DConfig();
927 #if defined(XP_WIN)
928 // When using WebRender, we defer initialization of the D3D11 devices until
929 // the (rare) cases where they're used. Note that the GPU process where
930 // WebRender runs doesn't initialize gfxPlatform and performs explicit
931 // initialization of the bits it needs.
932 if (XRE_IsParentProcess() && !gfxConfig::IsEnabled(Feature::GPU_PROCESS) &&
933 StaticPrefs::
934 gfx_webrender_enabled_no_gpu_process_with_angle_win_AtStartup()) {
935 gPlatform->EnsureDevicesInitialized();
937 #endif
939 if (XRE_IsParentProcess()) {
940 mozilla::glean::gpu_process::feature_status.Set(
941 gfxConfig::GetFeature(Feature::GPU_PROCESS)
942 .GetStatusAndFailureIdString());
945 if (gfxConfig::IsEnabled(Feature::GPU_PROCESS)) {
946 GPUProcessManager* gpu = GPUProcessManager::Get();
947 Unused << gpu->LaunchGPUProcess();
950 if (XRE_IsParentProcess()) {
951 // Create the global vsync source and dispatcher.
952 RefPtr<VsyncSource> vsyncSource =
953 gfxPlatform::ForceSoftwareVsync()
954 ? gPlatform->GetSoftwareVsyncSource()
955 : gPlatform->GetGlobalHardwareVsyncSource();
956 gPlatform->mVsyncDispatcher = new VsyncDispatcher(vsyncSource);
958 // Listen for layout.frame_rate pref changes.
959 Preferences::RegisterCallback(
960 gfxPlatform::ReInitFrameRate,
961 nsDependentCString(StaticPrefs::GetPrefName_layout_frame_rate()));
962 Preferences::RegisterCallback(
963 gfxPlatform::ReInitFrameRate,
964 nsDependentCString(
965 StaticPrefs::GetPrefName_privacy_resistFingerprinting()));
968 // Create the sRGB to output display profile transforms. They can be accessed
969 // off the main thread so we want to avoid a race condition.
970 InitializeCMS();
972 SkGraphics::Init();
973 #ifdef MOZ_ENABLE_FREETYPE
974 SkInitCairoFT(gPlatform->FontHintingEnabled());
975 #endif
976 gfxGradientCache::Init();
978 InitLayersIPC();
980 // This *create* the platform font list instance, but may not *initialize* it
981 // yet if the gfx.font-list.lazy-init.enabled pref is set. The first *use*
982 // of the list will ensure it is initialized.
983 if (!gPlatform->CreatePlatformFontList()) {
984 MOZ_CRASH("Could not initialize gfxPlatformFontList");
987 gPlatform->mScreenReferenceDrawTarget =
988 gPlatform->CreateOffscreenContentDrawTarget(IntSize(1, 1),
989 SurfaceFormat::B8G8R8A8);
990 if (!gPlatform->mScreenReferenceDrawTarget ||
991 !gPlatform->mScreenReferenceDrawTarget->IsValid()) {
992 // If TDR is detected, create a draw target with software backend
993 // and it should be replaced later when the process gets the device
994 // reset notification.
995 if (!gPlatform->DidRenderingDeviceReset()) {
996 gfxCriticalError() << "Could not initialize mScreenReferenceDrawTarget";
1000 if (NS_FAILED(gfxFontCache::Init())) {
1001 MOZ_CRASH("Could not initialize gfxFontCache");
1004 Preferences::RegisterPrefixCallbacks(FontPrefChanged, kObservedPrefs);
1006 GLContext::PlatformStartup();
1008 // Listen to memory pressure event so we can purge DrawTarget caches
1009 gPlatform->mMemoryPressureObserver =
1010 layers::MemoryPressureObserver::Create(gPlatform);
1012 // Request the imgITools service, implicitly initializing ImageLib.
1013 nsCOMPtr<imgITools> imgTools = do_GetService("@mozilla.org/image/tools;1");
1014 if (!imgTools) {
1015 MOZ_CRASH("Could not initialize ImageLib");
1018 RegisterStrongMemoryReporter(new GfxMemoryImageReporter());
1019 if (XRE_IsParentProcess()) {
1020 RegisterStrongAsyncMemoryReporter(new WebRenderMemoryReporter());
1023 RegisterStrongMemoryReporter(new SkMemoryReporter());
1025 uint32_t skiaCacheSize = GetSkiaGlyphCacheSize();
1026 if (skiaCacheSize != kDefaultGlyphCacheSize) {
1027 SkGraphics::SetFontCacheLimit(skiaCacheSize);
1030 InitNullMetadata();
1031 InitOpenGLConfig();
1033 if (XRE_IsParentProcess()) {
1034 Preferences::Unlock(FONT_VARIATIONS_PREF);
1035 if (!gfxPlatform::HasVariationFontSupport()) {
1036 // Ensure variation fonts are disabled and the pref is locked.
1037 Preferences::SetBool(FONT_VARIATIONS_PREF, false, PrefValueKind::Default);
1038 Preferences::SetBool(FONT_VARIATIONS_PREF, false);
1039 Preferences::Lock(FONT_VARIATIONS_PREF);
1043 if (XRE_IsParentProcess()) {
1044 ReportTelemetry();
1047 nsCOMPtr<nsIObserverService> obs = services::GetObserverService();
1048 if (obs) {
1049 obs->NotifyObservers(nullptr, "gfx-features-ready", nullptr);
1053 void gfxPlatform::ReportTelemetry() {
1054 MOZ_RELEASE_ASSERT(XRE_IsParentProcess(),
1055 "GFX: Only allowed to be called from parent process.");
1057 nsCOMPtr<nsIGfxInfo> gfxInfo = components::GfxInfo::Service();
1060 auto& screenManager = widget::ScreenManager::GetSingleton();
1061 const uint32_t screenCount = screenManager.CurrentScreenList().Length();
1062 RefPtr<widget::Screen> primaryScreen = screenManager.GetPrimaryScreen();
1063 const LayoutDeviceIntRect rect = primaryScreen->GetRect();
1065 mozilla::glean::gfx_display::count.Set(screenCount);
1066 mozilla::glean::gfx_display::primary_height.Set(rect.Height());
1067 mozilla::glean::gfx_display::primary_width.Set(rect.Width());
1070 nsString adapterDesc;
1071 gfxInfo->GetAdapterDescription(adapterDesc);
1072 mozilla::glean::gfx_adapter_primary::description.Set(
1073 NS_ConvertUTF16toUTF8(adapterDesc));
1075 nsString adapterVendorId;
1076 gfxInfo->GetAdapterVendorID(adapterVendorId);
1077 mozilla::glean::gfx_adapter_primary::vendor_id.Set(
1078 NS_ConvertUTF16toUTF8(adapterVendorId));
1080 nsString adapterDeviceId;
1081 gfxInfo->GetAdapterDeviceID(adapterDeviceId);
1082 mozilla::glean::gfx_adapter_primary::device_id.Set(
1083 NS_ConvertUTF16toUTF8(adapterDeviceId));
1085 nsString adapterSubsystemId;
1086 gfxInfo->GetAdapterSubsysID(adapterSubsystemId);
1087 mozilla::glean::gfx_adapter_primary::subsystem_id.Set(
1088 NS_ConvertUTF16toUTF8(adapterSubsystemId));
1090 uint32_t adapterRam = 0;
1091 gfxInfo->GetAdapterRAM(&adapterRam);
1092 mozilla::glean::gfx_adapter_primary::ram.Set(adapterRam);
1094 nsString adapterDriver;
1095 gfxInfo->GetAdapterDriver(adapterDriver);
1096 mozilla::glean::gfx_adapter_primary::driver_files.Set(
1097 NS_ConvertUTF16toUTF8(adapterDriver));
1099 nsString adapterDriverVendor;
1100 gfxInfo->GetAdapterDriverVendor(adapterDriverVendor);
1101 mozilla::glean::gfx_adapter_primary::driver_vendor.Set(
1102 NS_ConvertUTF16toUTF8(adapterDriverVendor));
1104 nsString adapterDriverVersion;
1105 gfxInfo->GetAdapterDriverVersion(adapterDriverVersion);
1106 mozilla::glean::gfx_adapter_primary::driver_version.Set(
1107 NS_ConvertUTF16toUTF8(adapterDriverVersion));
1109 nsString adapterDriverDate;
1110 gfxInfo->GetAdapterDriverDate(adapterDriverDate);
1111 mozilla::glean::gfx_adapter_primary::driver_date.Set(
1112 NS_ConvertUTF16toUTF8(adapterDriverDate));
1114 mozilla::glean::gfx_status::headless.Set(IsHeadless());
1116 MOZ_ASSERT(gPlatform, "Need gPlatform to generate some telemetry.");
1117 Telemetry::ScalarSet(Telemetry::ScalarID::GFX_SUPPORTS_HDR,
1118 gPlatform->SupportsHDR());
1121 static bool IsFeatureSupported(long aFeature, bool aDefault) {
1122 nsCOMPtr<nsIGfxInfo> gfxInfo = components::GfxInfo::Service();
1123 nsCString blockId;
1124 int32_t status;
1125 if (!NS_SUCCEEDED(gfxInfo->GetFeatureStatus(aFeature, blockId, &status))) {
1126 return aDefault;
1128 return status == nsIGfxInfo::FEATURE_STATUS_OK;
1131 /* static*/
1132 bool gfxPlatform::IsDXInterop2Blocked() {
1133 return !IsFeatureSupported(nsIGfxInfo::FEATURE_DX_INTEROP2, false);
1136 /* static*/
1137 bool gfxPlatform::IsDXNV12Blocked() {
1138 return !IsFeatureSupported(nsIGfxInfo::FEATURE_DX_NV12, false);
1141 /* static*/
1142 bool gfxPlatform::IsDXP010Blocked() {
1143 return !IsFeatureSupported(nsIGfxInfo::FEATURE_DX_P010, false);
1146 /* static*/
1147 bool gfxPlatform::IsDXP016Blocked() {
1148 return !IsFeatureSupported(nsIGfxInfo::FEATURE_DX_P016, false);
1151 /* static */
1152 int32_t gfxPlatform::MaxTextureSize() {
1153 // Make sure we don't completely break rendering because of a typo in the
1154 // pref or whatnot.
1155 const int32_t kMinSizePref = 2048;
1156 return std::max(
1157 kMinSizePref,
1158 StaticPrefs::gfx_max_texture_size_AtStartup_DoNotUseDirectly());
1161 /* static */
1162 int32_t gfxPlatform::MaxAllocSize() {
1163 // Make sure we don't completely break rendering because of a typo in the
1164 // pref or whatnot.
1165 const int32_t kMinAllocPref = 10000000;
1166 return std::max(kMinAllocPref,
1167 StaticPrefs::gfx_max_alloc_size_AtStartup_DoNotUseDirectly());
1170 /* static */
1171 void gfxPlatform::InitMoz2DLogging() {
1172 auto fwd = new CrashStatsLogForwarder(
1173 CrashReporter::Annotation::GraphicsCriticalError);
1174 fwd->SetCircularBufferSize(StaticPrefs::gfx_logging_crash_length_AtStartup());
1176 mozilla::gfx::Config cfg;
1177 cfg.mLogForwarder = fwd;
1178 cfg.mMaxTextureSize = gfxPlatform::MaxTextureSize();
1179 cfg.mMaxAllocSize = gfxPlatform::MaxAllocSize();
1181 gfx::Factory::Init(cfg);
1184 /* static */
1185 bool gfxPlatform::IsHeadless() {
1186 static bool initialized = false;
1187 static bool headless = false;
1188 if (!initialized) {
1189 initialized = true;
1190 headless = PR_GetEnv("MOZ_HEADLESS");
1192 return headless;
1195 /* static */
1196 bool gfxPlatform::UseRemoteCanvas() {
1197 return XRE_IsContentProcess() && (gfx::gfxVars::RemoteCanvasEnabled() ||
1198 gfx::gfxVars::UseAcceleratedCanvas2D());
1201 /* static */
1202 bool gfxPlatform::IsBackendAccelerated(
1203 const mozilla::gfx::BackendType aBackendType) {
1204 return aBackendType == BackendType::DIRECT2D ||
1205 aBackendType == BackendType::DIRECT2D1_1;
1208 /* static */
1209 bool gfxPlatform::CanMigrateMacGPUs() {
1210 int32_t pMigration = StaticPrefs::gfx_compositor_gpu_migration();
1212 bool forceDisable = pMigration == 0;
1213 bool forceEnable = pMigration == 2;
1215 return forceEnable || !forceDisable;
1218 static bool sLayersIPCIsUp = false;
1220 /* static */
1221 void gfxPlatform::InitNullMetadata() {
1222 ScrollMetadata::sNullMetadata = new ScrollMetadata();
1223 ClearOnShutdown(&ScrollMetadata::sNullMetadata);
1226 void gfxPlatform::Shutdown() {
1227 // In some cases, gPlatform may not be created but Shutdown() called,
1228 // e.g., during xpcshell tests.
1229 if (!gPlatform) {
1230 return;
1233 MOZ_ASSERT(!sLayersIPCIsUp);
1235 // These may be called before the corresponding subsystems have actually
1236 // started up. That's OK, they can handle it.
1237 gfxFontCache::Shutdown();
1238 gfxGradientCache::Shutdown();
1239 gfxAlphaBoxBlur::ShutdownBlurCache();
1240 gfxGraphiteShaper::Shutdown();
1241 gfxPlatformFontList::Shutdown();
1242 gfxFontMissingGlyphs::Shutdown();
1244 // Free the various non-null transforms and loaded profiles
1245 ShutdownCMS();
1247 Preferences::UnregisterPrefixCallbacks(FontPrefChanged, kObservedPrefs);
1249 NS_ASSERTION(gPlatform->mMemoryPressureObserver,
1250 "mMemoryPressureObserver has already gone");
1251 if (gPlatform->mMemoryPressureObserver) {
1252 gPlatform->mMemoryPressureObserver->Unregister();
1253 gPlatform->mMemoryPressureObserver = nullptr;
1256 if (XRE_IsParentProcess()) {
1257 if (gPlatform->mGlobalHardwareVsyncSource) {
1258 gPlatform->mGlobalHardwareVsyncSource->Shutdown();
1260 if (gPlatform->mSoftwareVsyncSource &&
1261 gPlatform->mSoftwareVsyncSource !=
1262 gPlatform->mGlobalHardwareVsyncSource) {
1263 gPlatform->mSoftwareVsyncSource->Shutdown();
1267 gPlatform->mGlobalHardwareVsyncSource = nullptr;
1268 gPlatform->mSoftwareVsyncSource = nullptr;
1269 gPlatform->mVsyncDispatcher = nullptr;
1271 // Shut down the default GL context provider.
1272 GLContextProvider::Shutdown();
1274 #if defined(XP_WIN)
1275 // The above shutdown calls operate on the available context providers on
1276 // most platforms. Windows is a "special snowflake", though, and has three
1277 // context providers available, so we have to shut all of them down.
1278 // We should only support the default GL provider on Windows; then, this
1279 // could go away. Unfortunately, we currently support WGL (the default) for
1280 // WebGL on Optimus.
1281 GLContextProviderEGL::Shutdown();
1282 #endif
1284 if (XRE_IsParentProcess()) {
1285 GPUProcessManager::Shutdown();
1286 VRProcessManager::Shutdown();
1287 RDDProcessManager::Shutdown();
1290 gfx::Factory::ShutDown();
1291 gfxVars::Shutdown();
1292 gfxFont::DestroySingletons();
1294 gfxConfig::Shutdown();
1296 gPlatform->WillShutdown();
1298 delete gPlatform;
1299 gPlatform = nullptr;
1302 /* static */
1303 void gfxPlatform::InitLayersIPC() {
1304 if (sLayersIPCIsUp) {
1305 return;
1307 sLayersIPCIsUp = true;
1309 if (XRE_IsParentProcess()) {
1310 #if defined(XP_WIN)
1311 if (gfxConfig::IsEnabled(gfx::Feature::WINDOW_OCCLUSION)) {
1312 widget::WinWindowOcclusionTracker::Ensure();
1314 #endif
1315 if (!gfxConfig::IsEnabled(Feature::GPU_PROCESS)) {
1316 RemoteTextureMap::Init();
1317 wr::RenderThread::Start(GPUProcessManager::Get()->AllocateNamespace());
1318 image::ImageMemoryReporter::InitForWebRender();
1321 layers::CompositorThreadHolder::Start();
1323 if (!gfxConfig::IsEnabled(Feature::GPU_PROCESS)) {
1324 gfx::CanvasRenderThread::Start();
1329 /* static */
1330 void gfxPlatform::ShutdownLayersIPC() {
1331 if (!sLayersIPCIsUp) {
1332 return;
1334 sLayersIPCIsUp = false;
1336 if (XRE_IsContentProcess()) {
1337 gfx::VRManagerChild::ShutDown();
1338 gfx::CanvasManagerChild::Shutdown();
1339 // cf bug 1215265.
1340 if (StaticPrefs::layers_child_process_shutdown()) {
1341 layers::CompositorManagerChild::Shutdown();
1342 layers::ImageBridgeChild::ShutDown();
1345 } else if (XRE_IsParentProcess()) {
1346 VideoBridgeParent::Shutdown();
1347 RDDProcessManager::RDDProcessShutdown();
1348 gfx::VRManagerChild::ShutDown();
1349 gfx::CanvasManagerChild::Shutdown();
1350 layers::CompositorManagerChild::Shutdown();
1351 layers::ImageBridgeChild::ShutDown();
1352 // This could be running on either the Compositor thread, the Renderer
1353 // thread, or the dedicated CanvasRender thread, so we need to shutdown
1354 // before the former two.
1355 gfx::CanvasRenderThread::Shutdown();
1356 // This has to happen after shutting down the child protocols.
1357 layers::CompositorThreadHolder::Shutdown();
1358 RemoteTextureMap::Shutdown();
1359 image::ImageMemoryReporter::ShutdownForWebRender();
1360 // There is a case that RenderThread exists when UseWebRender() is
1361 // false. This could happen when WebRender was fallbacked to compositor.
1362 if (wr::RenderThread::Get()) {
1363 wr::RenderThread::ShutDown();
1365 Preferences::UnregisterCallback(WebRenderDebugPrefChangeCallback,
1366 WR_DEBUG_PREF);
1367 Preferences::UnregisterCallback(WebRendeProfilerUIPrefChangeCallback,
1368 "gfx.webrender.debug.profiler-ui");
1369 Preferences::UnregisterCallback(
1370 WebRenderBlobTileSizePrefChangeCallback,
1371 nsDependentCString(
1372 StaticPrefs::GetPrefName_gfx_webrender_blob_tile_size()));
1374 #if defined(XP_WIN)
1375 widget::WinWindowOcclusionTracker::ShutDown();
1376 #endif
1377 } else {
1378 // TODO: There are other kind of processes and we should make sure gfx
1379 // stuff is either not created there or shut down properly.
1383 void gfxPlatform::WillShutdown() {
1384 // Destoy these first in case they depend on backend-specific resources.
1385 // Otherwise, the backend's destructor would be called before the
1386 // base gfxPlatform destructor.
1387 mScreenReferenceSurface = nullptr;
1388 mScreenReferenceDrawTarget = nullptr;
1390 // Always clear out the Skia font cache here, in case it is referencing any
1391 // SharedFTFaces that would otherwise outlive destruction of the FT_Library
1392 // that owns them.
1393 SkGraphics::PurgeFontCache();
1395 // The cairo folks think we should only clean up in debug builds,
1396 // but we're generally in the habit of trying to shut down as
1397 // cleanly as possible even in production code, so call this
1398 // cairo_debug_* function unconditionally.
1400 // because cairo can assert and thus crash on shutdown, don't do this in
1401 // release builds
1402 #ifdef NS_FREE_PERMANENT_DATA
1403 cairo_debug_reset_static_data();
1404 #endif
1407 gfxPlatform::~gfxPlatform() = default;
1409 /* static */
1410 already_AddRefed<DrawTarget> gfxPlatform::CreateDrawTargetForSurface(
1411 gfxASurface* aSurface, const IntSize& aSize) {
1412 SurfaceFormat format = aSurface->GetSurfaceFormat();
1413 RefPtr<DrawTarget> drawTarget = Factory::CreateDrawTargetForCairoSurface(
1414 aSurface->CairoSurface(), aSize, &format);
1415 if (!drawTarget) {
1416 gfxWarning() << "gfxPlatform::CreateDrawTargetForSurface failed in "
1417 "CreateDrawTargetForCairoSurface";
1418 return nullptr;
1420 return drawTarget.forget();
1423 cairo_user_data_key_t kSourceSurface;
1426 * Record the backend that was used to construct the SourceSurface.
1427 * When getting the cached SourceSurface for a gfxASurface/DrawTarget pair,
1428 * we check to make sure the DrawTarget's backend matches the backend
1429 * for the cached SourceSurface, and only use it if they match. This
1430 * can avoid expensive and unnecessary readbacks.
1432 struct SourceSurfaceUserData {
1433 RefPtr<SourceSurface> mSrcSurface;
1434 BackendType mBackendType;
1437 static void SourceBufferDestroy(void* srcSurfUD) {
1438 delete static_cast<SourceSurfaceUserData*>(srcSurfUD);
1441 UserDataKey kThebesSurface;
1443 struct DependentSourceSurfaceUserData {
1444 RefPtr<gfxASurface> mSurface;
1447 static void SourceSurfaceDestroyed(void* aData) {
1448 delete static_cast<DependentSourceSurfaceUserData*>(aData);
1451 void gfxPlatform::ClearSourceSurfaceForSurface(gfxASurface* aSurface) {
1452 aSurface->SetData(&kSourceSurface, nullptr, nullptr);
1455 /* static */
1456 already_AddRefed<SourceSurface> gfxPlatform::GetSourceSurfaceForSurface(
1457 RefPtr<DrawTarget> aTarget, gfxASurface* aSurface, bool aIsPlugin) {
1458 if (!aSurface->CairoSurface() || aSurface->CairoStatus()) {
1459 return nullptr;
1462 if (!aTarget) {
1463 aTarget = gfxPlatform::GetPlatform()->ScreenReferenceDrawTarget();
1466 void* userData = aSurface->GetData(&kSourceSurface);
1468 if (userData) {
1469 SourceSurfaceUserData* surf = static_cast<SourceSurfaceUserData*>(userData);
1471 if (surf->mSrcSurface->IsValid() &&
1472 surf->mBackendType == aTarget->GetBackendType()) {
1473 RefPtr<SourceSurface> srcSurface(surf->mSrcSurface);
1474 return srcSurface.forget();
1476 // We can just continue here as when setting new user data the destroy
1477 // function will be called for the old user data.
1480 SurfaceFormat format = aSurface->GetSurfaceFormat();
1482 if (aTarget->GetBackendType() == BackendType::CAIRO) {
1483 // If we're going to be used with a CAIRO DrawTarget, then just create a
1484 // SourceSurfaceCairo since we don't know the underlying type of the CAIRO
1485 // DrawTarget and can't pick a better surface type. Doing this also avoids
1486 // readback of aSurface's surface into memory if, for example, aSurface
1487 // wraps an xlib cairo surface (which can be important to avoid a major
1488 // slowdown).
1490 // We return here regardless of whether CreateSourceSurfaceFromNativeSurface
1491 // succeeds or not since we don't expect to be able to do any better below
1492 // if it fails.
1494 // Note that the returned SourceSurfaceCairo holds a strong reference to
1495 // the cairo_surface_t* that it wraps, which essencially means it holds a
1496 // strong reference to aSurface since aSurface shares its
1497 // cairo_surface_t*'s reference count variable. As a result we can't cache
1498 // srcBuffer on aSurface (see below) since aSurface would then hold a
1499 // strong reference back to srcBuffer, creating a reference loop and a
1500 // memory leak. Not caching is fine since wrapping is cheap enough (no
1501 // copying) so we can just wrap again next time we're called.
1502 return Factory::CreateSourceSurfaceForCairoSurface(
1503 aSurface->CairoSurface(), aSurface->GetSize(), format);
1506 RefPtr<SourceSurface> srcBuffer;
1508 // Currently no other DrawTarget types implement
1509 // CreateSourceSurfaceFromNativeSurface
1511 if (!srcBuffer) {
1512 // If aSurface wraps data, we can create a SourceSurfaceRawData that wraps
1513 // the same data, then optimize it for aTarget:
1514 RefPtr<DataSourceSurface> surf = GetWrappedDataSourceSurface(aSurface);
1515 if (surf) {
1516 srcBuffer = aIsPlugin
1517 ? aTarget->OptimizeSourceSurfaceForUnknownAlpha(surf)
1518 : aTarget->OptimizeSourceSurface(surf);
1520 if (srcBuffer == surf) {
1521 // GetWrappedDataSourceSurface returns a SourceSurface that holds a
1522 // strong reference to aSurface since it wraps aSurface's data and
1523 // needs it to stay alive. As a result we can't cache srcBuffer on
1524 // aSurface (below) since aSurface would then hold a strong reference
1525 // back to srcBuffer, creating a reference loop and a memory leak. Not
1526 // caching is fine since wrapping is cheap enough (no copying) so we
1527 // can just wrap again next time we're called.
1529 // Note that the check below doesn't catch this since srcBuffer will be
1530 // a SourceSurfaceRawData object (even if aSurface is not a
1531 // gfxImageSurface object), which is why we need this separate check.
1532 return srcBuffer.forget();
1537 if (!srcBuffer) {
1538 MOZ_ASSERT(aTarget->GetBackendType() != BackendType::CAIRO,
1539 "We already tried CreateSourceSurfaceFromNativeSurface with a "
1540 "DrawTargetCairo above");
1541 // We've run out of performant options. We now try creating a SourceSurface
1542 // using a temporary DrawTargetCairo and then optimizing it to aTarget's
1543 // actual type. The CreateSourceSurfaceFromNativeSurface() call will
1544 // likely create a DataSourceSurface (possibly involving copying and/or
1545 // readback), and the OptimizeSourceSurface may well copy again and upload
1546 // to the GPU. So, while this code path is rarely hit, hitting it may be
1547 // very slow.
1548 srcBuffer = Factory::CreateSourceSurfaceForCairoSurface(
1549 aSurface->CairoSurface(), aSurface->GetSize(), format);
1550 if (srcBuffer) {
1551 srcBuffer = aTarget->OptimizeSourceSurface(srcBuffer);
1555 if (!srcBuffer) {
1556 return nullptr;
1559 if ((srcBuffer->GetType() == SurfaceType::CAIRO &&
1560 static_cast<SourceSurfaceCairo*>(srcBuffer.get())->GetSurface() ==
1561 aSurface->CairoSurface()) ||
1562 (srcBuffer->GetType() == SurfaceType::CAIRO_IMAGE &&
1563 static_cast<DataSourceSurfaceCairo*>(srcBuffer.get())->GetSurface() ==
1564 aSurface->CairoSurface())) {
1565 // See the "Note that the returned SourceSurfaceCairo..." comment above.
1566 return srcBuffer.forget();
1569 // Add user data to aSurface so we can cache lookups in the future.
1570 auto* srcSurfUD = new SourceSurfaceUserData;
1571 srcSurfUD->mBackendType = aTarget->GetBackendType();
1572 srcSurfUD->mSrcSurface = srcBuffer;
1573 aSurface->SetData(&kSourceSurface, srcSurfUD, SourceBufferDestroy);
1575 return srcBuffer.forget();
1578 already_AddRefed<DataSourceSurface> gfxPlatform::GetWrappedDataSourceSurface(
1579 gfxASurface* aSurface) {
1580 RefPtr<gfxImageSurface> image = aSurface->GetAsImageSurface();
1581 if (!image) {
1582 return nullptr;
1584 RefPtr<DataSourceSurface> result = Factory::CreateWrappingDataSourceSurface(
1585 image->Data(), image->Stride(), image->GetSize(),
1586 ImageFormatToSurfaceFormat(image->Format()));
1588 if (!result) {
1589 return nullptr;
1592 // If we wrapped the underlying data of aSurface, then we need to add user
1593 // data to make sure aSurface stays alive until we are done with the data.
1594 auto* srcSurfUD = new DependentSourceSurfaceUserData;
1595 srcSurfUD->mSurface = aSurface;
1596 result->AddUserData(&kThebesSurface, srcSurfUD, SourceSurfaceDestroyed);
1598 return result.forget();
1601 void gfxPlatform::PopulateScreenInfo() {
1602 nsCOMPtr<nsIScreenManager> manager =
1603 do_GetService("@mozilla.org/gfx/screenmanager;1");
1604 MOZ_ASSERT(manager, "failed to get nsIScreenManager");
1606 nsCOMPtr<nsIScreen> screen;
1607 manager->GetPrimaryScreen(getter_AddRefs(screen));
1608 if (!screen) {
1609 // This can happen in xpcshell, for instance
1610 return;
1613 screen->GetColorDepth(&mScreenDepth);
1614 if (XRE_IsParentProcess()) {
1615 gfxVars::SetScreenDepth(mScreenDepth);
1618 int left, top;
1619 screen->GetRect(&left, &top, &mScreenSize.width, &mScreenSize.height);
1622 bool gfxPlatform::SupportsAzureContentForDrawTarget(DrawTarget* aTarget) {
1623 if (!aTarget || !aTarget->IsValid()) {
1624 return false;
1627 return SupportsAzureContentForType(aTarget->GetBackendType());
1630 void gfxPlatform::PurgeSkiaFontCache() {
1631 if (gfxPlatform::GetPlatform()->GetDefaultContentBackend() ==
1632 BackendType::SKIA) {
1633 SkGraphics::PurgeFontCache();
1637 already_AddRefed<DrawTarget> gfxPlatform::CreateDrawTargetForBackend(
1638 BackendType aBackend, const IntSize& aSize, SurfaceFormat aFormat) {
1639 // There is a bunch of knowledge in the gfxPlatform heirarchy about how to
1640 // create the best offscreen surface for the current system and situation. We
1641 // can easily take advantage of this for the Cairo backend, so that's what we
1642 // do.
1643 // mozilla::gfx::Factory can get away without having all this knowledge for
1644 // now, but this might need to change in the future (using
1645 // CreateOffscreenSurface() and CreateDrawTargetForSurface() for all
1646 // backends).
1647 if (aBackend == BackendType::CAIRO) {
1648 RefPtr<gfxASurface> surf =
1649 CreateOffscreenSurface(aSize, SurfaceFormatToImageFormat(aFormat));
1650 if (!surf || surf->CairoStatus()) {
1651 return nullptr;
1653 return CreateDrawTargetForSurface(surf, aSize);
1655 return Factory::CreateDrawTarget(aBackend, aSize, aFormat);
1658 already_AddRefed<DrawTarget> gfxPlatform::CreateOffscreenCanvasDrawTarget(
1659 const IntSize& aSize, SurfaceFormat aFormat) {
1660 NS_ASSERTION(mPreferredCanvasBackend != BackendType::NONE, "No backend.");
1662 // If we are using remote canvas we don't want to use acceleration in
1663 // canvas DrawTargets we are not remoting, so we always use the fallback
1664 // software one.
1665 if (!gfxPlatform::UseRemoteCanvas() ||
1666 !gfxPlatform::IsBackendAccelerated(mPreferredCanvasBackend)) {
1667 RefPtr<DrawTarget> target =
1668 CreateDrawTargetForBackend(mPreferredCanvasBackend, aSize, aFormat);
1669 if (target || mFallbackCanvasBackend == BackendType::NONE) {
1670 return target.forget();
1674 #ifdef XP_WIN
1675 // On Windows, the fallback backend (Cairo) should use its image backend.
1676 return Factory::CreateDrawTarget(mFallbackCanvasBackend, aSize, aFormat);
1677 #else
1678 return CreateDrawTargetForBackend(mFallbackCanvasBackend, aSize, aFormat);
1679 #endif
1682 already_AddRefed<DrawTarget> gfxPlatform::CreateOffscreenContentDrawTarget(
1683 const IntSize& aSize, SurfaceFormat aFormat, bool aFallback) {
1684 BackendType backend = (aFallback) ? mSoftwareBackend : mContentBackend;
1685 NS_ASSERTION(backend != BackendType::NONE, "No backend.");
1686 RefPtr<DrawTarget> dt = CreateDrawTargetForBackend(backend, aSize, aFormat);
1688 if (!dt) {
1689 return nullptr;
1692 // We'd prefer this to take proper care and return a CaptureDT, but for the
1693 // moment since we can't and this means we're going to be drawing on the main
1694 // thread force it's initialization. See bug 1526045 and bug 1521368.
1695 dt->ClearRect(gfx::Rect());
1696 if (!dt->IsValid()) {
1697 return nullptr;
1699 return dt.forget();
1702 already_AddRefed<DrawTarget> gfxPlatform::CreateSimilarSoftwareDrawTarget(
1703 DrawTarget* aDT, const IntSize& aSize, SurfaceFormat aFormat) {
1704 RefPtr<DrawTarget> dt;
1706 if (Factory::DoesBackendSupportDataDrawtarget(aDT->GetBackendType())) {
1707 dt = aDT->CreateSimilarDrawTarget(aSize, aFormat);
1708 } else {
1709 BackendType backendType = BackendType::SKIA;
1710 dt = Factory::CreateDrawTarget(backendType, aSize, aFormat);
1713 return dt.forget();
1716 /* static */
1717 already_AddRefed<DrawTarget> gfxPlatform::CreateDrawTargetForData(
1718 unsigned char* aData, const IntSize& aSize, int32_t aStride,
1719 SurfaceFormat aFormat, bool aUninitialized) {
1720 BackendType backendType = gfxVars::ContentBackend();
1721 NS_ASSERTION(backendType != BackendType::NONE, "No backend.");
1723 if (!Factory::DoesBackendSupportDataDrawtarget(backendType)) {
1724 backendType = BackendType::SKIA;
1727 RefPtr<DrawTarget> dt = Factory::CreateDrawTargetForData(
1728 backendType, aData, aSize, aStride, aFormat, aUninitialized);
1730 return dt.forget();
1733 /* static */
1734 BackendType gfxPlatform::BackendTypeForName(const nsCString& aName) {
1735 if (aName.EqualsLiteral("cairo")) return BackendType::CAIRO;
1736 if (aName.EqualsLiteral("skia")) return BackendType::SKIA;
1737 if (aName.EqualsLiteral("direct2d")) return BackendType::DIRECT2D;
1738 if (aName.EqualsLiteral("direct2d1.1")) return BackendType::DIRECT2D1_1;
1739 return BackendType::NONE;
1742 nsresult gfxPlatform::GetFontList(nsAtom* aLangGroup,
1743 const nsACString& aGenericFamily,
1744 nsTArray<nsString>& aListOfFonts) {
1745 gfxPlatformFontList::PlatformFontList()->GetFontList(
1746 aLangGroup, aGenericFamily, aListOfFonts);
1747 return NS_OK;
1750 nsresult gfxPlatform::UpdateFontList(bool aFullRebuild) {
1751 gfxPlatformFontList::PlatformFontList()->UpdateFontList(aFullRebuild);
1752 return NS_OK;
1755 void gfxPlatform::GetStandardFamilyName(const nsCString& aFontName,
1756 nsACString& aFamilyName) {
1757 gfxPlatformFontList::PlatformFontList()->GetStandardFamilyName(aFontName,
1758 aFamilyName);
1761 nsAutoCString gfxPlatform::GetDefaultFontName(
1762 const nsACString& aLangGroup, const nsACString& aGenericFamily) {
1763 // To benefit from Return Value Optimization, all paths here must return
1764 // this one variable:
1765 nsAutoCString result;
1767 auto* pfl = gfxPlatformFontList::PlatformFontList();
1768 FamilyAndGeneric fam = pfl->GetDefaultFontFamily(aLangGroup, aGenericFamily);
1769 if (!pfl->GetLocalizedFamilyName(fam.mFamily, result)) {
1770 NS_WARNING("missing default font-family name");
1773 return result;
1776 bool gfxPlatform::DownloadableFontsEnabled() {
1777 if (mAllowDownloadableFonts == UNINITIALIZED_VALUE) {
1778 mAllowDownloadableFonts =
1779 Preferences::GetBool(GFX_DOWNLOADABLE_FONTS_ENABLED, false);
1782 return mAllowDownloadableFonts;
1785 bool gfxPlatform::UseCmapsDuringSystemFallback() {
1786 return StaticPrefs::gfx_font_rendering_fallback_always_use_cmaps();
1789 bool gfxPlatform::OpenTypeSVGEnabled() {
1790 return StaticPrefs::gfx_font_rendering_opentype_svg_enabled();
1793 uint32_t gfxPlatform::WordCacheCharLimit() {
1794 return StaticPrefs::gfx_font_rendering_wordcache_charlimit();
1797 uint32_t gfxPlatform::WordCacheMaxEntries() {
1798 return StaticPrefs::gfx_font_rendering_wordcache_maxentries();
1801 bool gfxPlatform::UseGraphiteShaping() {
1802 return StaticPrefs::gfx_font_rendering_graphite_enabled();
1805 bool gfxPlatform::IsFontFormatSupported(
1806 StyleFontFaceSourceFormatKeyword aFormatHint,
1807 StyleFontFaceSourceTechFlags aTechFlags) {
1808 // By default, font resources are assumed to be supported; but if the format
1809 // hint or technology flags explicitly indicate something we don't support,
1810 // then return false.
1811 switch (aFormatHint) {
1812 case StyleFontFaceSourceFormatKeyword::None:
1813 break;
1814 case StyleFontFaceSourceFormatKeyword::Collection:
1815 return false;
1816 case StyleFontFaceSourceFormatKeyword::Opentype:
1817 case StyleFontFaceSourceFormatKeyword::Truetype:
1818 break;
1819 case StyleFontFaceSourceFormatKeyword::EmbeddedOpentype:
1820 return false;
1821 case StyleFontFaceSourceFormatKeyword::Svg:
1822 return false;
1823 case StyleFontFaceSourceFormatKeyword::Woff:
1824 break;
1825 case StyleFontFaceSourceFormatKeyword::Woff2:
1826 break;
1827 case StyleFontFaceSourceFormatKeyword::Unknown:
1828 return false;
1829 default:
1830 MOZ_ASSERT_UNREACHABLE("bad format hint!");
1831 return false;
1833 StyleFontFaceSourceTechFlags unsupportedTechnologies =
1834 StyleFontFaceSourceTechFlags::INCREMENTAL |
1835 StyleFontFaceSourceTechFlags::COLOR_SBIX;
1836 if (!StaticPrefs::gfx_downloadable_fonts_keep_color_bitmaps()) {
1837 unsupportedTechnologies |= StyleFontFaceSourceTechFlags::COLOR_CBDT;
1839 if (!StaticPrefs::gfx_font_rendering_colr_v1_enabled()) {
1840 unsupportedTechnologies |= StyleFontFaceSourceTechFlags::COLOR_COLRV1;
1842 if (!StaticPrefs::layout_css_font_palette_enabled()) {
1843 unsupportedTechnologies |= StyleFontFaceSourceTechFlags::PALETTES;
1845 if (!StaticPrefs::layout_css_font_variations_enabled()) {
1846 unsupportedTechnologies |= StyleFontFaceSourceTechFlags::VARIATIONS;
1848 if (aTechFlags & unsupportedTechnologies) {
1849 return false;
1851 return true;
1854 bool gfxPlatform::IsKnownIconFontFamily(const nsAtom* aFamilyName) const {
1855 return gfxPlatformFontList::PlatformFontList()->IsKnownIconFontFamily(
1856 aFamilyName);
1859 gfxFontEntry* gfxPlatform::LookupLocalFont(nsPresContext* aPresContext,
1860 const nsACString& aFontName,
1861 WeightRange aWeightForEntry,
1862 StretchRange aStretchForEntry,
1863 SlantStyleRange aStyleForEntry) {
1864 return gfxPlatformFontList::PlatformFontList()->LookupLocalFont(
1865 aPresContext, aFontName, aWeightForEntry, aStretchForEntry,
1866 aStyleForEntry);
1869 gfxFontEntry* gfxPlatform::MakePlatformFont(const nsACString& aFontName,
1870 WeightRange aWeightForEntry,
1871 StretchRange aStretchForEntry,
1872 SlantStyleRange aStyleForEntry,
1873 const uint8_t* aFontData,
1874 uint32_t aLength) {
1875 return gfxPlatformFontList::PlatformFontList()->MakePlatformFont(
1876 aFontName, aWeightForEntry, aStretchForEntry, aStyleForEntry, aFontData,
1877 aLength);
1880 BackendPrefsData gfxPlatform::GetBackendPrefs() const {
1881 BackendPrefsData data;
1883 data.mCanvasBitmask = BackendTypeBit(BackendType::SKIA);
1884 data.mContentBitmask = BackendTypeBit(BackendType::SKIA);
1886 #ifdef MOZ_WIDGET_GTK
1887 data.mCanvasBitmask |= BackendTypeBit(BackendType::CAIRO);
1888 data.mContentBitmask |= BackendTypeBit(BackendType::CAIRO);
1889 #endif
1891 data.mCanvasDefault = BackendType::SKIA;
1892 data.mContentDefault = BackendType::SKIA;
1894 return data;
1897 void gfxPlatform::InitBackendPrefs(BackendPrefsData&& aPrefsData) {
1898 mPreferredCanvasBackend = GetCanvasBackendPref(aPrefsData.mCanvasBitmask);
1899 if (mPreferredCanvasBackend == BackendType::NONE) {
1900 mPreferredCanvasBackend = aPrefsData.mCanvasDefault;
1903 if (mPreferredCanvasBackend == BackendType::DIRECT2D1_1) {
1904 // Falling back to D2D 1.0 won't help us here. When D2D 1.1 DT creation
1905 // fails it means the surface was too big or there's something wrong with
1906 // the device. D2D 1.0 will encounter a similar situation.
1907 mFallbackCanvasBackend = GetCanvasBackendPref(
1908 aPrefsData.mCanvasBitmask & ~(BackendTypeBit(mPreferredCanvasBackend) |
1909 BackendTypeBit(BackendType::DIRECT2D)));
1910 } else {
1911 mFallbackCanvasBackend = GetCanvasBackendPref(
1912 aPrefsData.mCanvasBitmask & ~BackendTypeBit(mPreferredCanvasBackend));
1915 mContentBackendBitmask = aPrefsData.mContentBitmask;
1916 mContentBackend = GetContentBackendPref(mContentBackendBitmask);
1917 if (mContentBackend == BackendType::NONE) {
1918 mContentBackend = aPrefsData.mContentDefault;
1919 // mContentBackendBitmask is our canonical reference for supported
1920 // backends so we need to add the default if we are using it and
1921 // overriding the prefs.
1922 mContentBackendBitmask |= BackendTypeBit(aPrefsData.mContentDefault);
1925 uint32_t swBackendBits = BackendTypeBit(BackendType::SKIA);
1926 #ifdef MOZ_WIDGET_GTK
1927 swBackendBits |= BackendTypeBit(BackendType::CAIRO);
1928 #endif
1929 mSoftwareBackend = GetContentBackendPref(swBackendBits);
1930 if (mSoftwareBackend == BackendType::NONE) {
1931 mSoftwareBackend = BackendType::SKIA;
1934 // If we don't have a fallback canvas backend then use the same software
1935 // fallback as content.
1936 if (mFallbackCanvasBackend == BackendType::NONE) {
1937 mFallbackCanvasBackend = mSoftwareBackend;
1940 if (XRE_IsParentProcess()) {
1941 gfxVars::SetContentBackend(mContentBackend);
1942 gfxVars::SetSoftwareBackend(mSoftwareBackend);
1946 /* static */
1947 BackendType gfxPlatform::GetCanvasBackendPref(uint32_t aBackendBitmask) {
1948 return GetBackendPref("gfx.canvas.azure.backends", aBackendBitmask);
1951 /* static */
1952 BackendType gfxPlatform::GetContentBackendPref(uint32_t& aBackendBitmask) {
1953 return GetBackendPref("gfx.content.azure.backends", aBackendBitmask);
1956 /* static */
1957 BackendType gfxPlatform::GetBackendPref(const char* aBackendPrefName,
1958 uint32_t& aBackendBitmask) {
1959 nsTArray<nsCString> backendList;
1960 nsAutoCString prefString;
1961 if (NS_SUCCEEDED(Preferences::GetCString(aBackendPrefName, prefString))) {
1962 ParseString(prefString, ',', backendList);
1965 uint32_t allowedBackends = 0;
1966 BackendType result = BackendType::NONE;
1967 for (uint32_t i = 0; i < backendList.Length(); ++i) {
1968 BackendType type = BackendTypeForName(backendList[i]);
1969 if (BackendTypeBit(type) & aBackendBitmask) {
1970 allowedBackends |= BackendTypeBit(type);
1971 if (result == BackendType::NONE) {
1972 result = type;
1977 aBackendBitmask = allowedBackends;
1978 return result;
1981 bool gfxPlatform::InSafeMode() {
1982 static bool sSafeModeInitialized = false;
1983 static bool sInSafeMode = false;
1985 if (!sSafeModeInitialized) {
1986 sSafeModeInitialized = true;
1987 nsCOMPtr<nsIXULRuntime> xr = do_GetService("@mozilla.org/xre/runtime;1");
1988 if (xr) {
1989 xr->GetInSafeMode(&sInSafeMode);
1992 return sInSafeMode;
1995 bool gfxPlatform::OffMainThreadCompositingEnabled() {
1996 return UsesOffMainThreadCompositing();
1999 void gfxPlatform::SetCMSModeOverride(CMSMode aMode) {
2000 MOZ_ASSERT(gCMSInitialized);
2001 gCMSMode = aMode;
2004 int gfxPlatform::GetRenderingIntent() {
2005 // StaticPrefList.yaml is using 0 as the default for the rendering
2006 // intent preference, based on that being the value for
2007 // QCMS_INTENT_DEFAULT. Assert here to catch if that ever
2008 // changes and we can then figure out what to do about it.
2009 MOZ_ASSERT(QCMS_INTENT_DEFAULT == 0);
2011 /* Try to query the pref system for a rendering intent. */
2012 int32_t pIntent = StaticPrefs::gfx_color_management_rendering_intent();
2013 if ((pIntent < QCMS_INTENT_MIN) || (pIntent > QCMS_INTENT_MAX)) {
2014 /* If the pref is out of range, use embedded profile. */
2015 pIntent = -1;
2017 return pIntent;
2020 DeviceColor gfxPlatform::TransformPixel(const sRGBColor& in,
2021 qcms_transform* transform) {
2022 if (transform) {
2023 /* we want the bytes in RGB order */
2024 #ifdef IS_LITTLE_ENDIAN
2025 /* ABGR puts the bytes in |RGBA| order on little endian */
2026 uint32_t packed = in.ToABGR();
2027 qcms_transform_data(transform, (uint8_t*)&packed, (uint8_t*)&packed, 1);
2028 auto out = DeviceColor::FromABGR(packed);
2029 #else
2030 /* ARGB puts the bytes in |ARGB| order on big endian */
2031 uint32_t packed = in.UnusualToARGB();
2032 /* add one to move past the alpha byte */
2033 qcms_transform_data(transform, (uint8_t*)&packed + 1, (uint8_t*)&packed + 1,
2035 auto out = DeviceColor::UnusualFromARGB(packed);
2036 #endif
2037 out.a = in.a;
2038 return out;
2040 return DeviceColor(in.r, in.g, in.b, in.a);
2043 nsTArray<uint8_t> gfxPlatform::GetPrefCMSOutputProfileData() {
2044 const auto mirror = StaticPrefs::gfx_color_management_display_profile();
2045 const auto fname = *mirror;
2046 if (fname == "") {
2047 return nsTArray<uint8_t>();
2050 void* mem = nullptr;
2051 size_t size = 0;
2052 qcms_data_from_path(fname.get(), &mem, &size);
2054 nsTArray<uint8_t> result;
2056 if (mem) {
2057 result.AppendElements(static_cast<uint8_t*>(mem), size);
2058 free(mem);
2061 return result;
2064 const mozilla::gfx::ContentDeviceData* gfxPlatform::GetInitContentDeviceData() {
2065 return gContentDeviceInitData;
2068 Maybe<nsTArray<uint8_t>>& gfxPlatform::GetCMSOutputProfileData() {
2069 return gCMSOutputProfileData;
2072 CMSMode GfxColorManagementMode() {
2073 const auto mode = StaticPrefs::gfx_color_management_mode();
2074 if (mode >= 0 && mode < UnderlyingValue(CMSMode::AllCount)) {
2075 return CMSMode(mode);
2077 return CMSMode::Off;
2080 void gfxPlatform::InitializeCMS() {
2081 if (gCMSInitialized) {
2082 return;
2085 if (XRE_IsGPUProcess()) {
2086 // Colors in the GPU process should already be managed, so we don't need to
2087 // perform color management there.
2088 gCMSInitialized = true;
2089 return;
2092 MOZ_DIAGNOSTIC_ASSERT(NS_IsMainThread(),
2093 "CMS should be initialized on the main thread");
2094 if (MOZ_UNLIKELY(!NS_IsMainThread())) {
2095 return;
2098 gCMSMode = GfxColorManagementMode();
2100 gCMSsRGBProfile = qcms_profile_sRGB();
2102 /* Determine if we're using the internal override to force sRGB as
2103 an output profile for reftests. See Bug 452125.
2105 Note that we don't normally (outside of tests) set a default value
2106 of this preference, which means nsIPrefBranch::GetBoolPref will
2107 typically throw (and leave its out-param untouched).
2109 if (StaticPrefs::gfx_color_management_force_srgb() ||
2110 StaticPrefs::gfx_color_management_native_srgb()) {
2111 gCMSOutputProfile = gCMSsRGBProfile;
2114 if (!gCMSOutputProfile) {
2115 nsTArray<uint8_t> outputProfileData =
2116 gfxPlatform::GetPlatform()->GetPlatformCMSOutputProfileData();
2117 if (!outputProfileData.IsEmpty()) {
2118 gCMSOutputProfile = qcms_profile_from_memory_curves_only(
2119 outputProfileData.Elements(), outputProfileData.Length());
2123 /* Determine if the profile looks bogus. If so, close the profile
2124 * and use sRGB instead. See bug 460629, */
2125 if (gCMSOutputProfile && qcms_profile_is_bogus(gCMSOutputProfile)) {
2126 NS_ASSERTION(gCMSOutputProfile != gCMSsRGBProfile,
2127 "Builtin sRGB profile tagged as bogus!!!");
2128 qcms_profile_release(gCMSOutputProfile);
2129 gCMSOutputProfile = nullptr;
2132 if (!gCMSOutputProfile) {
2133 gCMSOutputProfile = gCMSsRGBProfile;
2136 /* Precache the LUT16 Interpolations for the output profile. See
2137 bug 444661 for details. */
2138 qcms_profile_precache_output_transform(gCMSOutputProfile);
2140 // Create the RGB transform.
2141 gCMSRGBTransform =
2142 qcms_transform_create(gCMSsRGBProfile, QCMS_DATA_RGB_8, gCMSOutputProfile,
2143 QCMS_DATA_RGB_8, QCMS_INTENT_PERCEPTUAL);
2145 // And the inverse.
2146 gCMSInverseRGBTransform =
2147 qcms_transform_create(gCMSOutputProfile, QCMS_DATA_RGB_8, gCMSsRGBProfile,
2148 QCMS_DATA_RGB_8, QCMS_INTENT_PERCEPTUAL);
2150 // The RGBA transform.
2151 gCMSRGBATransform = qcms_transform_create(gCMSsRGBProfile, QCMS_DATA_RGBA_8,
2152 gCMSOutputProfile, QCMS_DATA_RGBA_8,
2153 QCMS_INTENT_PERCEPTUAL);
2155 // And the BGRA one.
2156 gCMSBGRATransform = qcms_transform_create(gCMSsRGBProfile, QCMS_DATA_BGRA_8,
2157 gCMSOutputProfile, QCMS_DATA_BGRA_8,
2158 QCMS_INTENT_PERCEPTUAL);
2160 // FIXME: We only enable iccv4 after we create the platform profile, to
2161 // wallpaper over bug 1697787.
2163 // This should happen ideally right after setting gCMSMode.
2164 if (StaticPrefs::gfx_color_management_enablev4()) {
2165 qcms_enable_iccv4();
2168 gCMSInitialized = true;
2171 qcms_transform* gfxPlatform::GetCMSOSRGBATransform() {
2172 switch (SurfaceFormat::OS_RGBA) {
2173 case SurfaceFormat::B8G8R8A8:
2174 return GetCMSBGRATransform();
2175 case SurfaceFormat::R8G8B8A8:
2176 return GetCMSRGBATransform();
2177 default:
2178 // We do not support color management with big endian.
2179 return nullptr;
2183 qcms_data_type gfxPlatform::GetCMSOSRGBAType() {
2184 switch (SurfaceFormat::OS_RGBA) {
2185 case SurfaceFormat::B8G8R8A8:
2186 return QCMS_DATA_BGRA_8;
2187 case SurfaceFormat::R8G8B8A8:
2188 return QCMS_DATA_RGBA_8;
2189 default:
2190 // We do not support color management with big endian.
2191 return QCMS_DATA_RGBA_8;
2195 /* Shuts down various transforms and profiles for CMS. */
2196 void gfxPlatform::ShutdownCMS() {
2197 if (gCMSRGBTransform) {
2198 qcms_transform_release(gCMSRGBTransform);
2199 gCMSRGBTransform = nullptr;
2201 if (gCMSInverseRGBTransform) {
2202 qcms_transform_release(gCMSInverseRGBTransform);
2203 gCMSInverseRGBTransform = nullptr;
2205 if (gCMSRGBATransform) {
2206 qcms_transform_release(gCMSRGBATransform);
2207 gCMSRGBATransform = nullptr;
2209 if (gCMSBGRATransform) {
2210 qcms_transform_release(gCMSBGRATransform);
2211 gCMSBGRATransform = nullptr;
2213 if (gCMSOutputProfile) {
2214 qcms_profile_release(gCMSOutputProfile);
2216 // handle the aliased case
2217 if (gCMSsRGBProfile == gCMSOutputProfile) {
2218 gCMSsRGBProfile = nullptr;
2220 gCMSOutputProfile = nullptr;
2222 if (gCMSsRGBProfile) {
2223 qcms_profile_release(gCMSsRGBProfile);
2224 gCMSsRGBProfile = nullptr;
2227 // Reset the state variables
2228 gCMSMode = CMSMode::Off;
2229 gCMSInitialized = false;
2232 uint32_t gfxPlatform::GetBidiNumeralOption() {
2233 return StaticPrefs::bidi_numeral();
2236 /* static */
2237 void gfxPlatform::FlushFontAndWordCaches() {
2238 gfxFontCache* fontCache = gfxFontCache::GetCache();
2239 if (fontCache) {
2240 fontCache->Flush();
2243 gfxPlatform::PurgeSkiaFontCache();
2246 /* static */
2247 void gfxPlatform::ForceGlobalReflow(NeedsReframe aNeedsReframe,
2248 BroadcastToChildren aBroadcastToChildren) {
2249 MOZ_ASSERT(NS_IsMainThread());
2250 const bool reframe = aNeedsReframe == NeedsReframe::Yes;
2251 // Send a notification that will be observed by PresShells in this process
2252 // only.
2253 if (nsCOMPtr<nsIObserverService> obs = services::GetObserverService()) {
2254 char16_t needsReframe[] = {char16_t(reframe), 0};
2255 obs->NotifyObservers(nullptr, "font-info-updated", needsReframe);
2257 if (XRE_IsParentProcess() &&
2258 aBroadcastToChildren == BroadcastToChildren::Yes) {
2259 // Propagate the change to child processes.
2260 for (auto* process :
2261 dom::ContentParent::AllProcesses(dom::ContentParent::eLive)) {
2262 Unused << process->SendForceGlobalReflow(reframe);
2267 void gfxPlatform::FontsPrefsChanged(const char* aPref) {
2268 NS_ASSERTION(aPref != nullptr, "null preference");
2269 if (!strcmp(GFX_DOWNLOADABLE_FONTS_ENABLED, aPref)) {
2270 mAllowDownloadableFonts = UNINITIALIZED_VALUE;
2271 } else if (!strcmp(GFX_PREF_WORD_CACHE_CHARLIMIT, aPref) ||
2272 !strcmp(GFX_PREF_WORD_CACHE_MAXENTRIES, aPref) ||
2273 !strcmp(GFX_PREF_GRAPHITE_SHAPING, aPref)) {
2274 FlushFontAndWordCaches();
2275 } else if (
2276 #if defined(XP_MACOSX)
2277 !strcmp(GFX_PREF_CORETEXT_SHAPING, aPref) ||
2278 #endif
2279 !strcmp("gfx.font_rendering.ahem_antialias_none", aPref)) {
2280 FlushFontAndWordCaches();
2281 } else if (!strcmp(GFX_PREF_OPENTYPE_SVG, aPref)) {
2282 gfxFontCache::GetCache()->Flush();
2283 gfxFontCache::GetCache()->NotifyGlyphsChanged();
2287 mozilla::LogModule* gfxPlatform::GetLog(eGfxLog aWhichLog) {
2288 // logs shared across gfx
2289 static LazyLogModule sFontlistLog("fontlist");
2290 static LazyLogModule sFontInitLog("fontinit");
2291 static LazyLogModule sTextrunLog("textrun");
2292 static LazyLogModule sTextrunuiLog("textrunui");
2293 static LazyLogModule sCmapDataLog("cmapdata");
2294 static LazyLogModule sTextPerfLog("textperf");
2296 switch (aWhichLog) {
2297 case eGfxLog_fontlist:
2298 return sFontlistLog;
2299 case eGfxLog_fontinit:
2300 return sFontInitLog;
2301 case eGfxLog_textrun:
2302 return sTextrunLog;
2303 case eGfxLog_textrunui:
2304 return sTextrunuiLog;
2305 case eGfxLog_cmapdata:
2306 return sCmapDataLog;
2307 case eGfxLog_textperf:
2308 return sTextPerfLog;
2311 MOZ_ASSERT_UNREACHABLE("Unexpected log type");
2312 return nullptr;
2315 RefPtr<mozilla::gfx::DrawTarget> gfxPlatform::ScreenReferenceDrawTarget() {
2316 MOZ_ASSERT_IF(XRE_IsContentProcess(), NS_IsMainThread());
2317 return (mScreenReferenceDrawTarget)
2318 ? mScreenReferenceDrawTarget
2319 : gPlatform->CreateOffscreenContentDrawTarget(
2320 IntSize(1, 1), SurfaceFormat::B8G8R8A8, true);
2323 /* static */ RefPtr<mozilla::gfx::DrawTarget>
2324 gfxPlatform::ThreadLocalScreenReferenceDrawTarget() {
2325 if (NS_IsMainThread() && gPlatform) {
2326 return gPlatform->ScreenReferenceDrawTarget();
2329 gfxPlatformWorker* platformWorker = gfxPlatformWorker::Get();
2330 if (platformWorker) {
2331 return platformWorker->ScreenReferenceDrawTarget();
2334 return Factory::CreateDrawTarget(BackendType::SKIA, IntSize(1, 1),
2335 SurfaceFormat::B8G8R8A8);
2338 mozilla::gfx::SurfaceFormat gfxPlatform::Optimal2DFormatForContent(
2339 gfxContentType aContent) {
2340 switch (aContent) {
2341 case gfxContentType::COLOR:
2342 switch (GetOffscreenFormat()) {
2343 case SurfaceFormat::A8R8G8B8_UINT32:
2344 return mozilla::gfx::SurfaceFormat::B8G8R8A8;
2345 case SurfaceFormat::X8R8G8B8_UINT32:
2346 return mozilla::gfx::SurfaceFormat::B8G8R8X8;
2347 case SurfaceFormat::R5G6B5_UINT16:
2348 return mozilla::gfx::SurfaceFormat::R5G6B5_UINT16;
2349 default:
2350 MOZ_ASSERT_UNREACHABLE(
2351 "unknown gfxImageFormat for "
2352 "gfxContentType::COLOR");
2353 return mozilla::gfx::SurfaceFormat::B8G8R8A8;
2355 case gfxContentType::ALPHA:
2356 return mozilla::gfx::SurfaceFormat::A8;
2357 case gfxContentType::COLOR_ALPHA:
2358 return mozilla::gfx::SurfaceFormat::B8G8R8A8;
2359 default:
2360 MOZ_ASSERT_UNREACHABLE("unknown gfxContentType");
2361 return mozilla::gfx::SurfaceFormat::B8G8R8A8;
2365 gfxImageFormat gfxPlatform::OptimalFormatForContent(gfxContentType aContent) {
2366 switch (aContent) {
2367 case gfxContentType::COLOR:
2368 return GetOffscreenFormat();
2369 case gfxContentType::ALPHA:
2370 return SurfaceFormat::A8;
2371 case gfxContentType::COLOR_ALPHA:
2372 return SurfaceFormat::A8R8G8B8_UINT32;
2373 default:
2374 MOZ_ASSERT_UNREACHABLE("unknown gfxContentType");
2375 return SurfaceFormat::A8R8G8B8_UINT32;
2380 * There are a number of layers acceleration (or layers in general) preferences
2381 * that should be consistent for the lifetime of the application (bug 840967).
2382 * As such, we will evaluate them all as soon as one of them is evaluated
2383 * and remember the values. Changing these preferences during the run will
2384 * not have any effect until we restart.
2386 static mozilla::Atomic<bool> sLayersSupportsHardwareVideoDecoding(false);
2387 static bool sLayersHardwareVideoDecodingFailed = false;
2389 static mozilla::Atomic<bool> sLayersAccelerationPrefsInitialized(false);
2391 static void VideoDecodingFailedChangedCallback(const char* aPref, void*) {
2392 sLayersHardwareVideoDecodingFailed = Preferences::GetBool(aPref, false);
2393 gfxPlatform::GetPlatform()->UpdateCanUseHardwareVideoDecoding();
2396 void gfxPlatform::UpdateCanUseHardwareVideoDecoding() {
2397 if (XRE_IsParentProcess()) {
2398 gfxVars::SetCanUseHardwareVideoDecoding(CanUseHardwareVideoDecoding());
2402 void gfxPlatform::UpdateForceSubpixelAAWherePossible() {
2403 bool forceSubpixelAAWherePossible =
2404 StaticPrefs::gfx_webrender_quality_force_subpixel_aa_where_possible();
2405 gfxVars::SetForceSubpixelAAWherePossible(forceSubpixelAAWherePossible);
2408 void gfxPlatform::InitAcceleration() {
2409 if (sLayersAccelerationPrefsInitialized) {
2410 return;
2413 InitCompositorAccelerationPrefs();
2415 // If this is called for the first time on a non-main thread, we're screwed.
2416 // At the moment there's no explicit guarantee that the main thread calls
2417 // this before the compositor thread, but let's at least make the assumption
2418 // explicit.
2419 MOZ_ASSERT(NS_IsMainThread(), "can only initialize prefs on the main thread");
2421 #ifndef MOZ_WIDGET_GTK
2422 nsCOMPtr<nsIGfxInfo> gfxInfo = components::GfxInfo::Service();
2423 nsCString discardFailureId;
2424 int32_t status;
2425 #endif
2427 if (XRE_IsParentProcess()) {
2428 gfxVars::SetBrowserTabsRemoteAutostart(BrowserTabsRemoteAutostart());
2429 gfxVars::SetOffscreenFormat(GetOffscreenFormat());
2430 gfxVars::SetRequiresAcceleratedGLContextForCompositorOGL(
2431 RequiresAcceleratedGLContextForCompositorOGL());
2432 #ifdef XP_WIN
2433 if (NS_SUCCEEDED(
2434 gfxInfo->GetFeatureStatus(nsIGfxInfo::FEATURE_D3D11_KEYED_MUTEX,
2435 discardFailureId, &status))) {
2436 gfxVars::SetAllowD3D11KeyedMutex(status == nsIGfxInfo::FEATURE_STATUS_OK);
2437 } else {
2438 // If we couldn't properly evaluate the status, err on the side
2439 // of caution and give this functionality to the user.
2440 gfxCriticalNote << "Cannot evaluate keyed mutex feature status";
2441 gfxVars::SetAllowD3D11KeyedMutex(true);
2443 if (StaticPrefs::gfx_direct3d11_use_double_buffering()) {
2444 gfxVars::SetUseDoubleBufferingWithCompositor(true);
2446 #endif
2449 if (StaticPrefs::media_hardware_video_decoding_enabled_AtStartup()) {
2450 #ifdef MOZ_WIDGET_GTK
2451 sLayersSupportsHardwareVideoDecoding =
2452 gfxPlatformGtk::GetPlatform()->InitVAAPIConfig(
2453 StaticPrefs::
2454 media_hardware_video_decoding_force_enabled_AtStartup() ||
2455 StaticPrefs::media_ffmpeg_vaapi_enabled_AtStartup());
2456 #else
2457 if (
2458 # ifdef XP_WIN
2459 Preferences::GetBool("media.wmf.dxva.enabled", true) &&
2460 # endif
2461 NS_SUCCEEDED(gfxInfo->GetFeatureStatus(
2462 nsIGfxInfo::FEATURE_HARDWARE_VIDEO_DECODING, discardFailureId,
2463 &status))) {
2464 if (status == nsIGfxInfo::FEATURE_STATUS_OK ||
2465 StaticPrefs::
2466 media_hardware_video_decoding_force_enabled_AtStartup()) {
2467 sLayersSupportsHardwareVideoDecoding = true;
2470 #endif
2471 } else if (XRE_IsParentProcess()) {
2472 FeatureState& feature =
2473 gfxConfig::GetFeature(Feature::HARDWARE_VIDEO_DECODING);
2474 feature.EnableByDefault();
2475 feature.UserDisable("User disabled via pref",
2476 "FEATURE_HARDWARE_VIDEO_DECODING_PREF_DISABLED"_ns);
2479 sLayersAccelerationPrefsInitialized = true;
2481 if (XRE_IsParentProcess()) {
2482 Preferences::RegisterCallbackAndCall(
2483 VideoDecodingFailedChangedCallback,
2484 "media.hardware-video-decoding.failed");
2485 InitGPUProcessPrefs();
2487 FeatureState& feature = gfxConfig::GetFeature(Feature::REMOTE_CANVAS);
2488 feature.SetDefault(StaticPrefs::gfx_canvas_remote_AtStartup(),
2489 FeatureStatus::Disabled, "Disabled via pref");
2491 if (!gfxConfig::IsEnabled(Feature::GPU_PROCESS) &&
2492 !StaticPrefs::gfx_canvas_remote_allow_in_parent_AtStartup()) {
2493 feature.Disable(FeatureStatus::UnavailableNoGpuProcess,
2494 "Disabled without GPU process",
2495 "FEATURE_REMOTE_CANVAS_NO_GPU_PROCESS"_ns);
2498 #ifndef XP_WIN
2499 gfxConfig::ForceDisable(Feature::REMOTE_CANVAS, FeatureStatus::Blocked,
2500 "Platform not supported",
2501 "FEATURE_REMOTE_CANVAS_NOT_WINDOWS"_ns);
2502 #endif
2504 gfxVars::SetRemoteCanvasEnabled(feature.IsEnabled());
2508 void gfxPlatform::InitGPUProcessPrefs() {
2509 // We want to hide this from about:support, so only set a default if the
2510 // pref is known to be true.
2511 if (!StaticPrefs::layers_gpu_process_enabled_AtStartup() &&
2512 !StaticPrefs::layers_gpu_process_force_enabled_AtStartup()) {
2513 return;
2516 FeatureState& gpuProc = gfxConfig::GetFeature(Feature::GPU_PROCESS);
2518 // We require E10S - otherwise, there is very little benefit to the GPU
2519 // process, since the UI process must still use acceleration for
2520 // performance.
2521 if (!BrowserTabsRemoteAutostart()) {
2522 gpuProc.DisableByDefault(FeatureStatus::Unavailable,
2523 "Multi-process mode is not enabled",
2524 "FEATURE_FAILURE_NO_E10S"_ns);
2525 } else {
2526 gpuProc.SetDefaultFromPref(
2527 StaticPrefs::GetPrefName_layers_gpu_process_enabled(), true,
2528 StaticPrefs::GetPrefDefault_layers_gpu_process_enabled());
2531 if (StaticPrefs::layers_gpu_process_force_enabled_AtStartup()) {
2532 gpuProc.UserForceEnable("User force-enabled via pref");
2535 nsCString message;
2536 nsCString failureId;
2537 if (!gfxPlatform::IsGfxInfoStatusOkay(nsIGfxInfo::FEATURE_GPU_PROCESS,
2538 &message, failureId)) {
2539 gpuProc.Disable(FeatureStatus::Blocklisted, message.get(), failureId);
2540 return;
2543 if (IsHeadless()) {
2544 gpuProc.ForceDisable(FeatureStatus::Blocked, "Headless mode is enabled",
2545 "FEATURE_FAILURE_HEADLESS_MODE"_ns);
2546 return;
2549 InitPlatformGPUProcessPrefs();
2552 void gfxPlatform::InitCompositorAccelerationPrefs() {
2553 const char* acceleratedEnv = PR_GetEnv("MOZ_ACCELERATED");
2555 FeatureState& feature = gfxConfig::GetFeature(Feature::HW_COMPOSITING);
2557 // Base value - does the platform allow acceleration?
2558 if (feature.SetDefault(AccelerateLayersByDefault(), FeatureStatus::Blocked,
2559 "Acceleration blocked by platform")) {
2560 if (StaticPrefs::
2561 layers_acceleration_disabled_AtStartup_DoNotUseDirectly()) {
2562 feature.UserDisable("Disabled by layers.acceleration.disabled=true",
2563 "FEATURE_FAILURE_COMP_PREF"_ns);
2564 } else if (acceleratedEnv && *acceleratedEnv == '0') {
2565 feature.UserDisable("Disabled by envvar", "FEATURE_FAILURE_COMP_ENV"_ns);
2567 } else {
2568 if (acceleratedEnv && *acceleratedEnv == '1') {
2569 feature.UserEnable("Enabled by envvar");
2573 // This has specific meaning elsewhere, so we always record it.
2574 if (StaticPrefs::
2575 layers_acceleration_force_enabled_AtStartup_DoNotUseDirectly()) {
2576 feature.UserForceEnable("Force-enabled by pref");
2579 // Safe, headless, and record/replay modes override everything.
2580 if (InSafeMode()) {
2581 feature.ForceDisable(FeatureStatus::Blocked,
2582 "Acceleration blocked by safe-mode",
2583 "FEATURE_FAILURE_COMP_SAFEMODE"_ns);
2585 if (IsHeadless()) {
2586 feature.ForceDisable(FeatureStatus::Blocked,
2587 "Acceleration blocked by headless mode",
2588 "FEATURE_FAILURE_COMP_HEADLESSMODE"_ns);
2592 /*static*/
2593 bool gfxPlatform::WebRenderPrefEnabled() {
2594 return StaticPrefs::gfx_webrender_all_AtStartup();
2597 /*static*/
2598 bool gfxPlatform::WebRenderEnvvarEnabled() {
2599 const char* env = PR_GetEnv("MOZ_WEBRENDER");
2600 return (env && *env == '1');
2603 /* static */ const char* gfxPlatform::WebRenderResourcePathOverride() {
2604 const char* resourcePath = PR_GetEnv("WR_RESOURCE_PATH");
2605 if (!resourcePath || resourcePath[0] == '\0') {
2606 return nullptr;
2608 return resourcePath;
2611 void gfxPlatform::InitWebRenderConfig() {
2612 bool prefEnabled = WebRenderPrefEnabled();
2613 bool envvarEnabled = WebRenderEnvvarEnabled();
2615 // WR? WR+ => means WR was enabled on qualified hardware
2616 // WR! WR+ => means WR was enabled via gfx.webrender.{all,enabled} or
2617 // envvar, possibly on unqualified hardware
2618 // In all cases WR- means WR was not enabled, for one of many possible
2619 // reasons. Prior to bug 1523788 landing the gfx.webrender.{all,enabled}
2620 // prefs only worked on Nightly so keep that in mind when looking at older
2621 // crash reports.
2622 ScopedGfxFeatureReporter reporter("WR", prefEnabled || envvarEnabled);
2623 if (!XRE_IsParentProcess()) {
2624 // The parent process runs through all the real decision-making code
2625 // later in this function. For other processes we still want to report
2626 // the state of the feature for crash reports.
2627 reporter.SetSuccessful();
2628 return;
2631 // Update the gfxConfig feature states.
2632 gfxConfigManager manager;
2633 manager.Init();
2634 manager.ConfigureWebRender();
2636 bool hasHardware = gfxConfig::IsEnabled(Feature::WEBRENDER);
2638 #ifdef MOZ_WIDGET_GTK
2639 // We require a hardware driver to back the GL context unless the user forced
2640 // on WebRender.
2641 if (!gfxConfig::IsForcedOnByUser(Feature::WEBRENDER) &&
2642 StaticPrefs::gfx_webrender_reject_software_driver_AtStartup()) {
2643 gfxVars::SetWebRenderRequiresHardwareDriver(true);
2645 #endif
2647 #ifdef XP_WIN
2648 if (gfxConfig::IsEnabled(Feature::WEBRENDER_ANGLE)) {
2649 gfxVars::SetUseWebRenderANGLE(true);
2651 #endif
2653 if (gfxConfig::IsEnabled(Feature::WEBRENDER_SHADER_CACHE)) {
2654 gfxVars::SetUseWebRenderProgramBinaryDisk(true);
2657 gfxVars::SetUseWebRenderOptimizedShaders(
2658 gfxConfig::IsEnabled(Feature::WEBRENDER_OPTIMIZED_SHADERS));
2660 gfxVars::SetUseSoftwareWebRender(!hasHardware);
2662 Preferences::RegisterPrefixCallbackAndCall(SwapIntervalPrefChangeCallback,
2663 "gfx.swap-interval");
2665 reporter.SetSuccessful();
2667 Preferences::RegisterPrefixCallbackAndCall(WebRenderDebugPrefChangeCallback,
2668 WR_DEBUG_PREF);
2670 RegisterWebRenderBoolParamCallback();
2672 Preferences::RegisterPrefixCallbackAndCall(
2673 WebRendeProfilerUIPrefChangeCallback, "gfx.webrender.debug.profiler-ui");
2674 Preferences::RegisterCallback(
2675 WebRenderQualityPrefChangeCallback,
2676 nsDependentCString(
2677 StaticPrefs::
2678 GetPrefName_gfx_webrender_quality_force_subpixel_aa_where_possible()));
2680 Preferences::RegisterCallback(
2681 WebRenderBatchingPrefChangeCallback,
2682 nsDependentCString(
2683 StaticPrefs::GetPrefName_gfx_webrender_batching_lookback()));
2685 Preferences::RegisterCallbackAndCall(
2686 WebRenderBlobTileSizePrefChangeCallback,
2687 nsDependentCString(
2688 StaticPrefs::GetPrefName_gfx_webrender_blob_tile_size()));
2690 Preferences::RegisterCallbackAndCall(
2691 WebRenderUploadThresholdPrefChangeCallback,
2692 nsDependentCString(
2693 StaticPrefs::GetPrefName_gfx_webrender_batched_upload_threshold()));
2695 if (WebRenderResourcePathOverride()) {
2696 CrashReporter::AnnotateCrashReport(
2697 CrashReporter::Annotation::IsWebRenderResourcePathOverridden, true);
2700 UpdateForceSubpixelAAWherePossible();
2702 #if defined(MOZ_WIDGET_ANDROID) || defined(MOZ_WIDGET_GTK)
2703 if (StaticPrefs::gfx_webrender_software_opengl_AtStartup()) {
2704 gfxVars::SetAllowSoftwareWebRenderOGL(true);
2706 #endif
2708 #ifdef XP_WIN
2709 if (gfxConfig::IsEnabled(Feature::WEBRENDER_DCOMP_PRESENT)) {
2710 gfxVars::SetUseWebRenderDCompWin(true);
2712 if (StaticPrefs::gfx_webrender_software_d3d11_AtStartup()) {
2713 gfxVars::SetAllowSoftwareWebRenderD3D11(true);
2716 const bool overlaySupported =
2717 IsWin10AnniversaryUpdateOrLater() &&
2718 gfxConfig::IsEnabled(Feature::WEBRENDER_COMPOSITOR);
2719 MOZ_ASSERT_IF(overlaySupported,
2720 gfxConfig::IsEnabled(Feature::WEBRENDER_DCOMP_PRESENT));
2722 bool useVideoHwOverlay = false;
2723 if (StaticPrefs::gfx_webrender_dcomp_video_hw_overlay_win_AtStartup()) {
2724 if (overlaySupported) {
2725 useVideoHwOverlay = true;
2728 if (useVideoHwOverlay &&
2729 !StaticPrefs::
2730 gfx_webrender_dcomp_video_hw_overlay_win_force_enabled_AtStartup()) {
2731 nsCString failureId;
2732 int32_t status;
2733 const nsCOMPtr<nsIGfxInfo> gfxInfo = components::GfxInfo::Service();
2734 if (NS_FAILED(gfxInfo->GetFeatureStatus(nsIGfxInfo::FEATURE_VIDEO_OVERLAY,
2735 failureId, &status))) {
2736 FeatureState& feature =
2737 gfxConfig::GetFeature(Feature::VIDEO_HARDWARE_OVERLAY);
2738 feature.DisableByDefault(FeatureStatus::BlockedNoGfxInfo,
2739 "gfxInfo is broken",
2740 "FEATURE_FAILURE_WR_NO_GFX_INFO"_ns);
2741 useVideoHwOverlay = false;
2742 } else {
2743 if (status != nsIGfxInfo::FEATURE_ALLOW_ALWAYS) {
2744 FeatureState& feature =
2745 gfxConfig::GetFeature(Feature::VIDEO_HARDWARE_OVERLAY);
2746 feature.DisableByDefault(FeatureStatus::Blocked,
2747 "Blocklisted by gfxInfo", failureId);
2748 useVideoHwOverlay = false;
2752 } else if (overlaySupported) {
2753 FeatureState& feature =
2754 gfxConfig::GetFeature(Feature::VIDEO_HARDWARE_OVERLAY);
2755 feature.DisableByDefault(FeatureStatus::Blocked, "Disabled by pref",
2756 "FEATURE_FAILURE_DISABLED_BY_PREF"_ns);
2759 if (useVideoHwOverlay) {
2760 FeatureState& feature =
2761 gfxConfig::GetFeature(Feature::VIDEO_HARDWARE_OVERLAY);
2762 feature.EnableByDefault();
2763 gfxVars::SetUseWebRenderDCompVideoHwOverlayWin(true);
2766 bool useVideoSwOverlay = false;
2767 if (overlaySupported &&
2768 StaticPrefs::gfx_webrender_dcomp_video_sw_overlay_win_AtStartup()) {
2769 useVideoSwOverlay = true;
2771 if (useVideoSwOverlay &&
2772 !StaticPrefs::
2773 gfx_webrender_dcomp_video_sw_overlay_win_force_enabled_AtStartup()) {
2774 nsCString failureId;
2775 int32_t status;
2776 const nsCOMPtr<nsIGfxInfo> gfxInfo = components::GfxInfo::Service();
2777 if (NS_FAILED(gfxInfo->GetFeatureStatus(
2778 nsIGfxInfo::FEATURE_VIDEO_SOFTWARE_OVERLAY, failureId,
2779 &status))) {
2780 FeatureState& feature =
2781 gfxConfig::GetFeature(Feature::VIDEO_SOFTWARE_OVERLAY);
2782 feature.DisableByDefault(FeatureStatus::BlockedNoGfxInfo,
2783 "gfxInfo is broken",
2784 "FEATURE_FAILURE_WR_NO_GFX_INFO"_ns);
2785 useVideoSwOverlay = false;
2786 } else {
2787 if (status != nsIGfxInfo::FEATURE_STATUS_OK) {
2788 FeatureState& feature =
2789 gfxConfig::GetFeature(Feature::VIDEO_SOFTWARE_OVERLAY);
2790 feature.DisableByDefault(FeatureStatus::Blocked,
2791 "Blocklisted by gfxInfo", failureId);
2792 useVideoSwOverlay = false;
2796 } else if (overlaySupported) {
2797 FeatureState& feature =
2798 gfxConfig::GetFeature(Feature::VIDEO_SOFTWARE_OVERLAY);
2799 feature.DisableByDefault(FeatureStatus::Blocked, "Disabled by pref",
2800 "FEATURE_FAILURE_DISABLED_BY_PREF"_ns);
2803 if (useVideoSwOverlay) {
2804 FeatureState& feature =
2805 gfxConfig::GetFeature(Feature::VIDEO_SOFTWARE_OVERLAY);
2806 feature.EnableByDefault();
2807 gfxVars::SetUseWebRenderDCompVideoSwOverlayWin(true);
2810 bool useHwVideoZeroCopy = false;
2811 if (StaticPrefs::media_wmf_zero_copy_nv12_textures_AtStartup()) {
2812 if (hasHardware) {
2813 useHwVideoZeroCopy = true;
2816 if (useHwVideoZeroCopy &&
2817 !StaticPrefs::
2818 media_wmf_zero_copy_nv12_textures_force_enabled_AtStartup()) {
2819 nsCString failureId;
2820 int32_t status;
2821 const nsCOMPtr<nsIGfxInfo> gfxInfo = components::GfxInfo::Service();
2822 if (NS_FAILED(gfxInfo->GetFeatureStatus(
2823 nsIGfxInfo::FEATURE_HW_DECODED_VIDEO_ZERO_COPY, failureId,
2824 &status))) {
2825 FeatureState& feature =
2826 gfxConfig::GetFeature(Feature::HW_DECODED_VIDEO_ZERO_COPY);
2827 feature.DisableByDefault(FeatureStatus::BlockedNoGfxInfo,
2828 "gfxInfo is broken",
2829 "FEATURE_FAILURE_WR_NO_GFX_INFO"_ns);
2830 useHwVideoZeroCopy = false;
2831 } else {
2832 if (status != nsIGfxInfo::FEATURE_ALLOW_ALWAYS) {
2833 FeatureState& feature =
2834 gfxConfig::GetFeature(Feature::HW_DECODED_VIDEO_ZERO_COPY);
2835 feature.DisableByDefault(FeatureStatus::Blocked,
2836 "Blocklisted by gfxInfo", failureId);
2837 useHwVideoZeroCopy = false;
2843 if (useHwVideoZeroCopy) {
2844 FeatureState& feature =
2845 gfxConfig::GetFeature(Feature::HW_DECODED_VIDEO_ZERO_COPY);
2846 feature.EnableByDefault();
2847 gfxVars::SetHwDecodedVideoZeroCopy(true);
2850 bool reuseDecoderDevice = false;
2851 if (StaticPrefs::gfx_direct3d11_reuse_decoder_device_AtStartup()) {
2852 reuseDecoderDevice = true;
2854 if (reuseDecoderDevice &&
2855 !StaticPrefs::
2856 gfx_direct3d11_reuse_decoder_device_force_enabled_AtStartup()) {
2857 nsCString failureId;
2858 int32_t status;
2859 const nsCOMPtr<nsIGfxInfo> gfxInfo = components::GfxInfo::Service();
2860 if (NS_FAILED(gfxInfo->GetFeatureStatus(
2861 nsIGfxInfo::FEATURE_REUSE_DECODER_DEVICE, failureId, &status))) {
2862 FeatureState& feature =
2863 gfxConfig::GetFeature(Feature::REUSE_DECODER_DEVICE);
2864 feature.DisableByDefault(FeatureStatus::BlockedNoGfxInfo,
2865 "gfxInfo is broken",
2866 "FEATURE_FAILURE_WR_NO_GFX_INFO"_ns);
2867 reuseDecoderDevice = false;
2868 } else {
2869 if (status != nsIGfxInfo::FEATURE_STATUS_OK) {
2870 FeatureState& feature =
2871 gfxConfig::GetFeature(Feature::REUSE_DECODER_DEVICE);
2872 feature.DisableByDefault(FeatureStatus::Blocked,
2873 "Blocklisted by gfxInfo", failureId);
2874 reuseDecoderDevice = false;
2880 if (reuseDecoderDevice) {
2881 FeatureState& feature =
2882 gfxConfig::GetFeature(Feature::REUSE_DECODER_DEVICE);
2883 feature.EnableByDefault();
2884 gfxVars::SetReuseDecoderDevice(true);
2887 if (Preferences::GetBool("gfx.webrender.flip-sequential", false)) {
2888 if (gfxVars::UseWebRenderANGLE()) {
2889 gfxVars::SetUseWebRenderFlipSequentialWin(true);
2892 if (Preferences::GetBool("gfx.webrender.triple-buffering.enabled", false)) {
2893 if (gfxVars::UseWebRenderDCompWin() ||
2894 gfxVars::UseWebRenderFlipSequentialWin()) {
2895 gfxVars::SetUseWebRenderTripleBufferingWin(true);
2898 #endif
2900 if (gfxConfig::IsEnabled(Feature::WEBRENDER_COMPOSITOR)) {
2901 gfxVars::SetUseWebRenderCompositor(true);
2904 Telemetry::ScalarSet(
2905 Telemetry::ScalarID::GFX_OS_COMPOSITOR,
2906 gfx::gfxConfig::IsEnabled(gfx::Feature::WEBRENDER_COMPOSITOR));
2908 if (gfxConfig::IsEnabled(Feature::WEBRENDER_PARTIAL)) {
2909 gfxVars::SetWebRenderMaxPartialPresentRects(
2910 StaticPrefs::gfx_webrender_max_partial_present_rects_AtStartup());
2913 // Set features that affect WR's RendererOptions
2914 gfxVars::SetUseGLSwizzle(
2915 IsFeatureSupported(nsIGfxInfo::FEATURE_GL_SWIZZLE, true));
2916 gfxVars::SetUseWebRenderScissoredCacheClears(gfx::gfxConfig::IsEnabled(
2917 gfx::Feature::WEBRENDER_SCISSORED_CACHE_CLEARS));
2919 // The RemoveShaderCacheFromDiskIfNecessary() needs to be called after
2920 // WebRenderConfig initialization.
2921 gfxUtils::RemoveShaderCacheFromDiskIfNecessary();
2924 void gfxPlatform::InitHardwareVideoConfig() {
2925 if (!XRE_IsParentProcess()) {
2926 return;
2929 #ifdef MOZ_WIDGET_GTK
2930 // We don't want to expose codec info if whole HW decoding is disabled.
2931 if (!sLayersSupportsHardwareVideoDecoding) {
2932 return;
2934 #endif
2936 nsCString message;
2937 nsCString failureId;
2939 FeatureState& featureVP8 = gfxConfig::GetFeature(Feature::VP8_HW_DECODE);
2940 featureVP8.EnableByDefault();
2942 if (!IsGfxInfoStatusOkay(nsIGfxInfo::FEATURE_VP8_HW_DECODE, &message,
2943 failureId)) {
2944 featureVP8.Disable(FeatureStatus::Blocklisted, message.get(), failureId);
2946 gfxVars::SetUseVP8HwDecode(featureVP8.IsEnabled());
2948 FeatureState& featureVP9 = gfxConfig::GetFeature(Feature::VP9_HW_DECODE);
2949 featureVP9.EnableByDefault();
2951 if (!IsGfxInfoStatusOkay(nsIGfxInfo::FEATURE_VP9_HW_DECODE, &message,
2952 failureId)) {
2953 featureVP9.Disable(FeatureStatus::Blocklisted, message.get(), failureId);
2955 gfxVars::SetUseVP9HwDecode(featureVP9.IsEnabled());
2957 // H264_HW_DECODE/AV1_HW_DECODE is used on Linux only right now.
2958 #ifdef MOZ_WIDGET_GTK
2959 FeatureState& featureH264 = gfxConfig::GetFeature(Feature::H264_HW_DECODE);
2960 featureH264.EnableByDefault();
2962 if (!IsGfxInfoStatusOkay(nsIGfxInfo::FEATURE_H264_HW_DECODE, &message,
2963 failureId)) {
2964 featureH264.Disable(FeatureStatus::Blocklisted, message.get(), failureId);
2966 gfxVars::SetUseH264HwDecode(featureH264.IsEnabled());
2968 FeatureState& featureAV1 = gfxConfig::GetFeature(Feature::AV1_HW_DECODE);
2969 featureAV1.EnableByDefault();
2971 if (!IsGfxInfoStatusOkay(nsIGfxInfo::FEATURE_AV1_HW_DECODE, &message,
2972 failureId)) {
2973 featureAV1.Disable(FeatureStatus::Blocklisted, message.get(), failureId);
2975 gfxVars::SetUseAV1HwDecode(featureAV1.IsEnabled());
2976 #endif
2979 void gfxPlatform::InitWebGLConfig() {
2980 if (!XRE_IsParentProcess()) return;
2982 const nsCOMPtr<nsIGfxInfo> gfxInfo = components::GfxInfo::Service();
2984 const auto IsFeatureOk = [&](const int32_t feature) {
2985 nsCString discardFailureId;
2986 int32_t status;
2987 MOZ_RELEASE_ASSERT(NS_SUCCEEDED(
2988 gfxInfo->GetFeatureStatus(feature, discardFailureId, &status)));
2989 return (status == nsIGfxInfo::FEATURE_STATUS_OK);
2992 gfxVars::SetAllowWebgl2(IsFeatureOk(nsIGfxInfo::FEATURE_WEBGL2));
2993 gfxVars::SetWebglAllowWindowsNativeGl(
2994 IsFeatureOk(nsIGfxInfo::FEATURE_WEBGL_OPENGL));
2995 gfxVars::SetAllowWebglAccelAngle(
2996 IsFeatureOk(nsIGfxInfo::FEATURE_WEBGL_ANGLE));
2997 gfxVars::SetWebglUseHardware(
2998 IsFeatureOk(nsIGfxInfo::FEATURE_WEBGL_USE_HARDWARE));
3000 if (kIsMacOS) {
3001 // Avoid crash for Intel HD Graphics 3000 on OSX. (Bug 1413269)
3002 nsString vendorID, deviceID;
3003 gfxInfo->GetAdapterVendorID(vendorID);
3004 gfxInfo->GetAdapterDeviceID(deviceID);
3005 if (vendorID.EqualsLiteral("0x8086") &&
3006 (deviceID.EqualsLiteral("0x0116") ||
3007 deviceID.EqualsLiteral("0x0126"))) {
3008 gfxVars::SetWebglAllowCoreProfile(false);
3012 bool allowWebGLOop =
3013 IsFeatureOk(nsIGfxInfo::FEATURE_ALLOW_WEBGL_OUT_OF_PROCESS);
3014 if (!kIsAndroid) {
3015 gfxVars::SetAllowWebglOop(allowWebGLOop);
3016 } else {
3017 // On android, enable out-of-process WebGL only when GPU process exists.
3018 gfxVars::SetAllowWebglOop(allowWebGLOop &&
3019 gfxConfig::IsEnabled(Feature::GPU_PROCESS));
3020 // Enable gl::SharedSurface of AndroidHardwareBuffer when API version is 26+
3021 // and out-of-process WebGL is enabled.
3022 #ifdef MOZ_WIDGET_ANDROID
3023 if (gfxVars::AllowWebglOop() && jni::GetAPIVersion() >= 26 &&
3024 StaticPrefs::webgl_out_of_process_enable_ahardwarebuffer_AtStartup()) {
3025 gfxVars::SetUseAHardwareBufferSharedSurfaceWebglOop(true);
3027 #endif
3030 bool threadsafeGL = IsFeatureOk(nsIGfxInfo::FEATURE_THREADSAFE_GL);
3031 threadsafeGL |= StaticPrefs::webgl_threadsafe_gl_force_enabled_AtStartup();
3032 threadsafeGL &= !StaticPrefs::webgl_threadsafe_gl_force_disabled_AtStartup();
3033 gfxVars::SetSupportsThreadsafeGL(threadsafeGL);
3035 FeatureState& feature =
3036 gfxConfig::GetFeature(Feature::CANVAS_RENDERER_THREAD);
3037 if (!threadsafeGL) {
3038 feature.DisableByDefault(FeatureStatus::Blocked, "Thread unsafe GL",
3039 "FEATURE_FAILURE_THREAD_UNSAFE_GL"_ns);
3040 } else if (!StaticPrefs::webgl_use_canvas_render_thread_AtStartup()) {
3041 feature.DisableByDefault(FeatureStatus::Blocked, "Disabled by pref",
3042 "FEATURE_FAILURE_DISABLED_BY_PREF"_ns);
3043 } else {
3044 feature.EnableByDefault();
3046 gfxVars::SetUseCanvasRenderThread(feature.IsEnabled());
3048 bool webglOopAsyncPresentForceSync =
3049 (threadsafeGL && !gfxVars::UseCanvasRenderThread()) ||
3050 StaticPrefs::webgl_out_of_process_async_present_force_sync();
3051 gfxVars::SetWebglOopAsyncPresentForceSync(webglOopAsyncPresentForceSync);
3053 if (kIsAndroid) {
3054 // Don't enable robust buffer access on Adreno 620 and 630 devices.
3055 // It causes the linking of some shaders to fail. See bug 1485441 and
3056 // bug 1810693.
3057 nsAutoString renderer;
3058 gfxInfo->GetAdapterDeviceID(renderer);
3059 if ((renderer.Find(u"Adreno (TM) 620") != -1) ||
3060 (renderer.Find(u"Adreno (TM) 630") != -1)) {
3061 gfxVars::SetAllowEglRbab(false);
3065 if (kIsLinux) {
3066 nsCString discardFailureId;
3067 int32_t status;
3068 FeatureState& feature =
3069 gfxConfig::GetFeature(Feature::DMABUF_SURFACE_EXPORT);
3070 if (NS_FAILED(
3071 gfxInfo->GetFeatureStatus(nsIGfxInfo::FEATURE_DMABUF_SURFACE_EXPORT,
3072 discardFailureId, &status)) ||
3073 status != nsIGfxInfo::FEATURE_STATUS_OK) {
3074 feature.DisableByDefault(FeatureStatus::Blocked, "Blocklisted by gfxInfo",
3075 discardFailureId);
3076 gfxVars::SetUseDMABufSurfaceExport(false);
3077 } else {
3078 feature.EnableByDefault();
3083 void gfxPlatform::InitWebGPUConfig() {
3084 if (!XRE_IsParentProcess()) {
3085 return;
3088 FeatureState& feature = gfxConfig::GetFeature(Feature::WEBGPU);
3089 feature.EnableByDefault();
3091 nsCString message;
3092 nsCString failureId;
3093 if (!IsGfxInfoStatusOkay(nsIGfxInfo::FEATURE_WEBGPU, &message, failureId)) {
3094 if (StaticPrefs::gfx_webgpu_ignore_blocklist_AtStartup()) {
3095 feature.UserForceEnable(
3096 "Ignoring blocklist entry because gfx.webgpu.ignore-blocklist is "
3097 "true.");
3100 feature.Disable(FeatureStatus::Blocklisted, message.get(), failureId);
3103 #ifdef RELEASE_OR_BETA
3104 feature.ForceDisable(FeatureStatus::Blocked,
3105 "WebGPU cannot be enabled in release or beta",
3106 "WEBGPU_DISABLE_RELEASE_OR_BETA"_ns);
3107 #endif
3109 gfxVars::SetAllowWebGPU(feature.IsEnabled());
3112 #ifdef XP_WIN
3113 static void WindowOcclusionPrefChangeCallback(const char* aPref, void*) {
3114 const char* env = PR_GetEnv("MOZ_WINDOW_OCCLUSION");
3115 if (env) {
3116 // env has a higher priority than pref.
3117 return;
3120 FeatureState& feature = gfxConfig::GetFeature(Feature::WINDOW_OCCLUSION);
3121 bool enabled =
3122 StaticPrefs::widget_windows_window_occlusion_tracking_enabled();
3124 printf_stderr("Dynamically enable window occlusion %d\n", enabled);
3126 // Update feature before calling WinUtils::EnableWindowOcclusion()
3127 if (enabled) {
3128 feature.UserEnable("User enabled by pref");
3129 } else {
3130 feature.UserDisable("User disabled via pref",
3131 "FEATURE_FAILURE_PREF_DISABLED"_ns);
3133 widget::WinUtils::EnableWindowOcclusion(enabled);
3135 #endif
3137 void gfxPlatform::InitWindowOcclusionConfig() {
3138 if (!XRE_IsParentProcess()) {
3139 return;
3141 #ifdef XP_WIN
3142 FeatureState& feature = gfxConfig::GetFeature(Feature::WINDOW_OCCLUSION);
3143 feature.SetDefaultFromPref(
3144 StaticPrefs::
3145 GetPrefName_widget_windows_window_occlusion_tracking_enabled(),
3146 true,
3147 StaticPrefs::
3148 GetPrefDefault_widget_windows_window_occlusion_tracking_enabled());
3150 const char* env = PR_GetEnv("MOZ_WINDOW_OCCLUSION");
3151 if (env) {
3152 if (*env == '1') {
3153 feature.UserForceEnable("Force enabled by envvar");
3154 } else {
3155 feature.UserDisable("Force disabled by envvar",
3156 "FEATURE_FAILURE_OCCL_ENV"_ns);
3160 Preferences::RegisterCallback(
3161 WindowOcclusionPrefChangeCallback,
3162 nsDependentCString(
3163 StaticPrefs::
3164 GetPrefName_widget_windows_window_occlusion_tracking_enabled()));
3165 #endif
3168 static void BackdropFilterPrefChangeCallback(const char*, void*) {
3169 FeatureState& feature = gfxConfig::GetFeature(Feature::BACKDROP_FILTER);
3171 // We need to reset because the user status needs to be set before the
3172 // environment status, but the environment status comes from the blocklist,
3173 // and the user status can be updated after the fact.
3174 feature.Reset();
3175 feature.EnableByDefault();
3177 if (StaticPrefs::layout_css_backdrop_filter_force_enabled()) {
3178 feature.UserForceEnable("Force enabled by pref");
3181 nsCString message;
3182 nsCString failureId;
3183 if (!gfxPlatform::IsGfxInfoStatusOkay(nsIGfxInfo::FEATURE_BACKDROP_FILTER,
3184 &message, failureId)) {
3185 feature.Disable(FeatureStatus::Blocklisted, message.get(), failureId);
3188 // This may still be gated by the layout.css.backdrop-filter.enabled pref but
3189 // the test infrastructure is very sensitive to how changes to that pref
3190 // propagate, so we don't include them in the gfxVars/gfxFeature.
3191 gfxVars::SetAllowBackdropFilter(feature.IsEnabled());
3194 void gfxPlatform::InitBackdropFilterConfig() {
3195 // This would ideally be in the nsCSSProps code
3196 // but nsCSSProps is initialized before gfxPlatform
3197 // so it has to be done here.
3198 gfxVars::AddReceiver(&nsCSSProps::GfxVarReceiver());
3200 if (!XRE_IsParentProcess()) {
3201 // gfxVars doesn't notify receivers when initialized on content processes
3202 // we need to explicitly recompute backdrop-filter's enabled state here.
3203 nsCSSProps::RecomputeEnabledState(
3204 StaticPrefs::GetPrefName_layout_css_backdrop_filter_enabled());
3205 return;
3208 BackdropFilterPrefChangeCallback(nullptr, nullptr);
3210 Preferences::RegisterCallback(
3211 BackdropFilterPrefChangeCallback,
3212 nsDependentCString(
3213 StaticPrefs::GetPrefName_layout_css_backdrop_filter_force_enabled()));
3216 static void AcceleratedCanvas2DPrefChangeCallback(const char*, void*) {
3217 FeatureState& feature = gfxConfig::GetFeature(Feature::ACCELERATED_CANVAS2D);
3219 // Reset to track toggling prefs and ensure force-enable does not happen
3220 // after blocklist.
3221 feature.Reset();
3223 // gfx.canvas.accelerated pref controls whether platform enables the feature,
3224 // but it still allows blocklisting to override it later.
3225 feature.SetDefaultFromPref(
3226 StaticPrefs::GetPrefName_gfx_canvas_accelerated(), true,
3227 StaticPrefs::GetPrefDefault_gfx_canvas_accelerated());
3229 // gfx.canvas.accelerated.force-enabled overrides the blocklist.
3230 if (StaticPrefs::gfx_canvas_accelerated_force_enabled()) {
3231 feature.UserForceEnable("Force-enabled by pref");
3234 if (kIsAndroid && !gfxConfig::IsEnabled(Feature::GPU_PROCESS)) {
3235 feature.Disable(FeatureStatus::Blocked, "Disabled by GPU Process disabled",
3236 "FEATURE_FAILURE_DISABLED_BY_GPU_PROCESS_DISABLED"_ns);
3237 } else if (!gfxConfig::IsEnabled(Feature::WEBRENDER)) {
3238 // There isn't much benefit to accelerating Canvas2D if we can't accelerate
3239 // WebRender itself.
3240 feature.Disable(FeatureStatus::Blocked, "Disabled by Software WebRender",
3241 "FEATURE_FAILURE_DISABLED_BY_SOFTWARE_WEBRENDER"_ns);
3244 // Check if blocklisted despite the default pref.
3245 nsCString message;
3246 nsCString failureId;
3247 if (!gfxPlatform::IsGfxInfoStatusOkay(
3248 nsIGfxInfo::FEATURE_ACCELERATED_CANVAS2D, &message, failureId)) {
3249 feature.Disable(FeatureStatus::Blocklisted, message.get(), failureId);
3252 if (StaticPrefs::gfx_canvas_remote_worker_threads_AtStartup() != 0) {
3253 feature.ForceDisable(FeatureStatus::Failed,
3254 "Disabled with non-zero canvas worker threads",
3255 "FEATURE_FAILURE_DISABLE_BY_CANVAS_WORKER_THREADS"_ns);
3258 gfxVars::SetUseAcceleratedCanvas2D(feature.IsEnabled());
3261 void gfxPlatform::InitAcceleratedCanvas2DConfig() {
3262 if (!XRE_IsParentProcess()) {
3263 return;
3266 // Decide during pref changes whether or not to enable acceleration. This
3267 // allows easily toggling acceleration on and off to test performance.
3268 AcceleratedCanvas2DPrefChangeCallback(nullptr, nullptr);
3270 Preferences::RegisterCallback(
3271 AcceleratedCanvas2DPrefChangeCallback,
3272 nsDependentCString(StaticPrefs::GetPrefName_gfx_canvas_accelerated()));
3273 Preferences::RegisterCallback(
3274 AcceleratedCanvas2DPrefChangeCallback,
3275 nsDependentCString(
3276 StaticPrefs::GetPrefName_gfx_canvas_accelerated_force_enabled()));
3279 bool gfxPlatform::CanUseHardwareVideoDecoding() {
3280 // this function is called from the compositor thread, so it is not
3281 // safe to init the prefs etc. from here.
3282 MOZ_ASSERT(sLayersAccelerationPrefsInitialized);
3283 return sLayersSupportsHardwareVideoDecoding &&
3284 !sLayersHardwareVideoDecodingFailed;
3287 bool gfxPlatform::AccelerateLayersByDefault() {
3288 #if defined(MOZ_GL_PROVIDER) || defined(MOZ_WIDGET_UIKIT)
3289 return true;
3290 #else
3291 return false;
3292 #endif
3295 /* static */
3296 bool gfxPlatform::UsesOffMainThreadCompositing() {
3297 if (XRE_GetProcessType() == GeckoProcessType_GPU) {
3298 return true;
3301 static bool firstTime = true;
3302 static bool result = false;
3304 if (firstTime) {
3305 MOZ_ASSERT(sLayersAccelerationPrefsInitialized);
3306 result = gfxVars::BrowserTabsRemoteAutostart() ||
3307 !StaticPrefs::
3308 layers_offmainthreadcomposition_force_disabled_AtStartup();
3309 #if defined(MOZ_WIDGET_GTK)
3310 // Linux users who chose OpenGL are being included in OMTC
3311 result |= StaticPrefs::
3312 layers_acceleration_force_enabled_AtStartup_DoNotUseDirectly();
3314 #endif
3315 firstTime = false;
3318 return result;
3321 RefPtr<mozilla::VsyncDispatcher> gfxPlatform::GetGlobalVsyncDispatcher() {
3322 MOZ_ASSERT(mVsyncDispatcher,
3323 "mVsyncDispatcher should have been initialized by ReInitFrameRate "
3324 "during gfxPlatform init");
3325 MOZ_ASSERT(XRE_IsParentProcess());
3326 return mVsyncDispatcher;
3329 already_AddRefed<mozilla::gfx::VsyncSource>
3330 gfxPlatform::GetGlobalHardwareVsyncSource() {
3331 if (!mGlobalHardwareVsyncSource) {
3332 mGlobalHardwareVsyncSource = CreateGlobalHardwareVsyncSource();
3334 return do_AddRef(mGlobalHardwareVsyncSource);
3337 /***
3338 * The preference "layout.frame_rate" has 3 meanings depending on the value:
3340 * -1 = Auto (default), use hardware vsync or software vsync @ 60 hz if hw
3341 * vsync fails.
3342 * 0 = ASAP mode - used during talos testing.
3343 * X = Software vsync at a rate of X times per second.
3345 already_AddRefed<mozilla::gfx::VsyncSource>
3346 gfxPlatform::GetSoftwareVsyncSource() {
3347 if (!mSoftwareVsyncSource) {
3348 double rateInMS = 1000.0 / (double)gfxPlatform::GetSoftwareVsyncRate();
3349 mSoftwareVsyncSource = new mozilla::gfx::SoftwareVsyncSource(
3350 TimeDuration::FromMilliseconds(rateInMS));
3352 return do_AddRef(mSoftwareVsyncSource);
3355 /* static */
3356 bool gfxPlatform::IsInLayoutAsapMode() {
3357 // There are 2 modes of ASAP mode.
3358 // 1 is that the refresh driver and compositor are in lock step
3359 // the second is that the compositor goes ASAP and the refresh driver
3360 // goes at whatever the configurated rate is. This only checks the version
3361 // talos uses, which is the refresh driver and compositor are in lockstep.
3362 // Ignore privacy_resistFingerprinting to preserve ASAP mode there.
3363 return StaticPrefs::layout_frame_rate() == 0;
3366 static int LayoutFrameRateFromPrefs() {
3367 auto val = StaticPrefs::layout_frame_rate();
3368 if (nsContentUtils::ShouldResistFingerprinting(
3369 "The frame rate is a global property.", RFPTarget::FrameRate)) {
3370 val = 60;
3372 return val;
3375 /* static */
3376 bool gfxPlatform::ForceSoftwareVsync() {
3377 return LayoutFrameRateFromPrefs() > 0;
3380 /* static */
3381 int gfxPlatform::GetSoftwareVsyncRate() {
3382 int preferenceRate = LayoutFrameRateFromPrefs();
3383 if (preferenceRate <= 0) {
3384 return gfxPlatform::GetDefaultFrameRate();
3386 return preferenceRate;
3389 /* static */
3390 int gfxPlatform::GetDefaultFrameRate() { return 60; }
3392 /* static */
3393 void gfxPlatform::ReInitFrameRate(const char* aPrefIgnored,
3394 void* aDataIgnored) {
3395 MOZ_RELEASE_ASSERT(XRE_IsParentProcess());
3397 if (gPlatform->mSoftwareVsyncSource) {
3398 // Update the rate of the existing software vsync source.
3399 double rateInMS = 1000.0 / (double)gfxPlatform::GetSoftwareVsyncRate();
3400 gPlatform->mSoftwareVsyncSource->SetVsyncRate(
3401 TimeDuration::FromMilliseconds(rateInMS));
3404 // Swap out the dispatcher's underlying source.
3405 RefPtr<VsyncSource> vsyncSource =
3406 gfxPlatform::ForceSoftwareVsync()
3407 ? gPlatform->GetSoftwareVsyncSource()
3408 : gPlatform->GetGlobalHardwareVsyncSource();
3409 gPlatform->mVsyncDispatcher->SetVsyncSource(vsyncSource);
3412 const char* gfxPlatform::GetAzureCanvasBackend() const {
3413 BackendType backend{};
3415 if (gfxConfig::IsEnabled(Feature::GPU_PROCESS)) {
3416 // Assume content process' backend prefs.
3417 BackendPrefsData data = GetBackendPrefs();
3418 backend = GetCanvasBackendPref(data.mCanvasBitmask);
3419 if (backend == BackendType::NONE) {
3420 backend = data.mCanvasDefault;
3422 } else {
3423 backend = mPreferredCanvasBackend;
3426 return GetBackendName(backend);
3429 const char* gfxPlatform::GetAzureContentBackend() const {
3430 BackendType backend{};
3432 if (gfxConfig::IsEnabled(Feature::GPU_PROCESS)) {
3433 // Assume content process' backend prefs.
3434 BackendPrefsData data = GetBackendPrefs();
3435 backend = GetContentBackendPref(data.mContentBitmask);
3436 if (backend == BackendType::NONE) {
3437 backend = data.mContentDefault;
3439 } else {
3440 backend = mContentBackend;
3443 return GetBackendName(backend);
3446 void gfxPlatform::GetAzureBackendInfo(mozilla::widget::InfoObject& aObj) {
3447 if (gfxConfig::IsEnabled(Feature::GPU_PROCESS)) {
3448 aObj.DefineProperty("AzureCanvasBackend (UI Process)",
3449 GetBackendName(mPreferredCanvasBackend));
3450 aObj.DefineProperty("AzureFallbackCanvasBackend (UI Process)",
3451 GetBackendName(mFallbackCanvasBackend));
3452 aObj.DefineProperty("AzureContentBackend (UI Process)",
3453 GetBackendName(mContentBackend));
3454 } else {
3455 aObj.DefineProperty("AzureFallbackCanvasBackend",
3456 GetBackendName(mFallbackCanvasBackend));
3459 aObj.DefineProperty("AzureCanvasBackend", GetAzureCanvasBackend());
3460 aObj.DefineProperty("AzureContentBackend", GetAzureContentBackend());
3463 void gfxPlatform::GetApzSupportInfo(mozilla::widget::InfoObject& aObj) {
3464 if (!gfxPlatform::AsyncPanZoomEnabled()) {
3465 return;
3468 if (SupportsApzWheelInput()) {
3469 aObj.DefineProperty("ApzWheelInput", 1);
3472 if (SupportsApzTouchInput()) {
3473 aObj.DefineProperty("ApzTouchInput", 1);
3476 if (SupportsApzDragInput()) {
3477 aObj.DefineProperty("ApzDragInput", 1);
3480 if (SupportsApzKeyboardInput() &&
3481 !StaticPrefs::accessibility_browsewithcaret()) {
3482 aObj.DefineProperty("ApzKeyboardInput", 1);
3485 if (SupportsApzAutoscrolling()) {
3486 aObj.DefineProperty("ApzAutoscrollInput", 1);
3489 if (SupportsApzZooming()) {
3490 aObj.DefineProperty("ApzZoomingInput", 1);
3494 void gfxPlatform::GetFrameStats(mozilla::widget::InfoObject& aObj) {
3495 uint32_t i = 0;
3496 for (FrameStats& f : mFrameStats) {
3497 nsPrintfCString name("Slow Frame #%02u", ++i);
3499 nsPrintfCString value(
3500 "Frame %" PRIu64
3501 "(%s) CONTENT_FRAME_TIME %d - Transaction start %f, main-thread time "
3502 "%f, full paint time %f, Skipped composites %u, Composite start %f, "
3503 "Resource upload time %f, GPU cache upload time %f, Render time %f, "
3504 "Composite time %f",
3505 f.id().mId, f.url().get(), f.contentFrameTime(),
3506 (f.transactionStart() - f.refreshStart()).ToMilliseconds(),
3507 (f.fwdTime() - f.transactionStart()).ToMilliseconds(),
3508 f.sceneBuiltTime()
3509 ? (f.sceneBuiltTime() - f.transactionStart()).ToMilliseconds()
3510 : 0.0,
3511 f.skippedComposites(),
3512 (f.compositeStart() - f.refreshStart()).ToMilliseconds(),
3513 f.resourceUploadTime(), f.gpuCacheUploadTime(),
3514 (f.compositeEnd() - f.renderStart()).ToMilliseconds(),
3515 (f.compositeEnd() - f.compositeStart()).ToMilliseconds());
3516 aObj.DefineProperty(name.get(), value.get());
3520 void gfxPlatform::GetCMSSupportInfo(mozilla::widget::InfoObject& aObj) {
3521 nsTArray<uint8_t> outputProfileData =
3522 gfxPlatform::GetPlatform()->GetPlatformCMSOutputProfileData();
3523 if (outputProfileData.IsEmpty()) {
3524 nsPrintfCString msg("Empty profile data");
3525 aObj.DefineProperty("CMSOutputProfile", msg.get());
3526 return;
3529 // Some profiles can be quite large. We don't want to include giant profiles
3530 // by default in about:support. For now, we only accept less than 8kiB.
3531 const size_t kMaxProfileSize = 8192;
3532 if (outputProfileData.Length() >= kMaxProfileSize) {
3533 nsPrintfCString msg("%zu bytes, too large", outputProfileData.Length());
3534 aObj.DefineProperty("CMSOutputProfile", msg.get());
3535 return;
3538 nsString encodedProfile;
3539 nsresult rv =
3540 Base64Encode(reinterpret_cast<const char*>(outputProfileData.Elements()),
3541 outputProfileData.Length(), encodedProfile);
3542 if (!NS_SUCCEEDED(rv)) {
3543 nsPrintfCString msg("base64 encode failed 0x%08x",
3544 static_cast<uint32_t>(rv));
3545 aObj.DefineProperty("CMSOutputProfile", msg.get());
3546 return;
3549 aObj.DefineProperty("CMSOutputProfile", encodedProfile);
3552 void gfxPlatform::GetDisplayInfo(mozilla::widget::InfoObject& aObj) {
3553 auto& screens = widget::ScreenManager::GetSingleton().CurrentScreenList();
3554 aObj.DefineProperty("DisplayCount", screens.Length());
3556 size_t i = 0;
3557 for (auto& screen : screens) {
3558 const LayoutDeviceIntRect rect = screen->GetRect();
3559 nsPrintfCString value("%dx%d@%dHz scales:%f|%f", rect.width, rect.height,
3560 screen->GetRefreshRate(),
3561 screen->GetContentsScaleFactor(),
3562 screen->GetDefaultCSSScaleFactor());
3564 aObj.DefineProperty(nsPrintfCString("Display%zu", i++).get(),
3565 NS_ConvertUTF8toUTF16(value));
3568 // Platform display info is only currently used for about:support and getting
3569 // it might fail in a child process anyway.
3570 if (XRE_IsParentProcess()) {
3571 GetPlatformDisplayInfo(aObj);
3575 void gfxPlatform::GetOverlayInfo(mozilla::widget::InfoObject& aObj) {
3576 if (mOverlayInfo.isNothing()) {
3577 return;
3580 auto toString = [](mozilla::layers::OverlaySupportType aType) -> const char* {
3581 switch (aType) {
3582 case mozilla::layers::OverlaySupportType::None:
3583 return "None";
3584 case mozilla::layers::OverlaySupportType::Software:
3585 return "Software";
3586 case mozilla::layers::OverlaySupportType::Direct:
3587 return "Direct";
3588 case mozilla::layers::OverlaySupportType::Scaling:
3589 return "Scaling";
3590 default:
3591 MOZ_ASSERT_UNREACHABLE("Unexpected to be called");
3593 MOZ_CRASH("Incomplete switch");
3596 nsPrintfCString value("NV12=%s YUV2=%s BGRA8=%s RGB10A2=%s",
3597 toString(mOverlayInfo.ref().mNv12Overlay),
3598 toString(mOverlayInfo.ref().mYuy2Overlay),
3599 toString(mOverlayInfo.ref().mBgra8Overlay),
3600 toString(mOverlayInfo.ref().mRgb10a2Overlay));
3602 aObj.DefineProperty("OverlaySupport", NS_ConvertUTF8toUTF16(value));
3605 void gfxPlatform::GetSwapChainInfo(mozilla::widget::InfoObject& aObj) {
3606 if (mSwapChainInfo.isNothing()) {
3607 return;
3610 auto toString = [](bool aTearingSupported) -> const char* {
3611 if (aTearingSupported) {
3612 return "Supported";
3614 return "Not Supported";
3617 nsPrintfCString value("%s", toString(mSwapChainInfo.ref().mTearingSupported));
3619 aObj.DefineProperty("SwapChainTearingSupport", NS_ConvertUTF8toUTF16(value));
3622 class FrameStatsComparator {
3623 public:
3624 bool Equals(const FrameStats& aA, const FrameStats& aB) const {
3625 return aA.contentFrameTime() == aB.contentFrameTime();
3627 // Reverse the condition here since we want the array sorted largest to
3628 // smallest.
3629 bool LessThan(const FrameStats& aA, const FrameStats& aB) const {
3630 return aA.contentFrameTime() > aB.contentFrameTime();
3634 void gfxPlatform::NotifyFrameStats(nsTArray<FrameStats>&& aFrameStats) {
3635 if (!StaticPrefs::gfx_logging_slow_frames_enabled_AtStartup()) {
3636 return;
3639 FrameStatsComparator comp;
3640 for (FrameStats& f : aFrameStats) {
3641 mFrameStats.InsertElementSorted(f, comp);
3643 if (mFrameStats.Length() > 10) {
3644 mFrameStats.SetLength(10);
3648 /*static*/
3649 uint32_t gfxPlatform::TargetFrameRate() {
3650 if (gPlatform && gPlatform->mVsyncDispatcher) {
3651 return round(1000.0 /
3652 gPlatform->mVsyncDispatcher->GetVsyncRate().ToMilliseconds());
3654 return 0;
3657 /* static */
3658 bool gfxPlatform::UseDesktopZoomingScrollbars() {
3659 return StaticPrefs::apz_allow_zooming() &&
3660 !StaticPrefs::apz_force_disable_desktop_zooming_scrollbars();
3663 /*static*/
3664 bool gfxPlatform::AsyncPanZoomEnabled() {
3665 #if !defined(MOZ_WIDGET_ANDROID) && !defined(MOZ_WIDGET_UIKIT)
3666 // For XUL applications (everything but Firefox on Android)
3667 // we only want to use APZ when E10S is enabled. If
3668 // we ever get input events off the main thread we can consider relaxing
3669 // this requirement.
3670 if (!BrowserTabsRemoteAutostart()) {
3671 return false;
3673 #endif
3674 #ifdef MOZ_WIDGET_ANDROID
3675 return true;
3676 #else
3677 // If Fission is enabled, OOP iframes require APZ for hittest. So, we
3678 // need to forcibly enable APZ in that case for avoiding users confused.
3679 if (FissionAutostart()) {
3680 return true;
3682 return StaticPrefs::
3683 layers_async_pan_zoom_enabled_AtStartup_DoNotUseDirectly();
3684 #endif
3687 /*static*/
3688 bool gfxPlatform::PerfWarnings() {
3689 return StaticPrefs::gfx_perf_warnings_enabled();
3692 void gfxPlatform::NotifyCompositorCreated(LayersBackend aBackend) {
3693 if (mCompositorBackend == aBackend) {
3694 return;
3697 if (mCompositorBackend != LayersBackend::LAYERS_NONE) {
3698 gfxCriticalNote << "Compositors might be mixed (" << int(mCompositorBackend)
3699 << "," << int(aBackend) << ")";
3702 // Set the backend before we notify so it's available immediately.
3703 mCompositorBackend = aBackend;
3705 if (XRE_IsParentProcess()) {
3706 nsDependentCString compositor(GetLayersBackendName(mCompositorBackend));
3707 mozilla::glean::gfx_status::compositor.Set(compositor);
3709 nsCString geckoVersion;
3710 nsCOMPtr<nsIXULAppInfo> app = do_GetService("@mozilla.org/xre/app-info;1");
3711 if (app) {
3712 app->GetVersion(geckoVersion);
3714 mozilla::glean::gfx_status::last_compositor_gecko_version.Set(geckoVersion);
3716 mozilla::glean::gfx_feature::webrender.Set(
3717 gfxConfig::GetFeature(gfx::Feature::WEBRENDER)
3718 .GetStatusAndFailureIdString());
3721 // Notify that we created a compositor, so telemetry can update.
3722 NS_DispatchToMainThread(
3723 NS_NewRunnableFunction("gfxPlatform::NotifyCompositorCreated", [] {
3724 if (nsCOMPtr<nsIObserverService> obsvc =
3725 services::GetObserverService()) {
3726 obsvc->NotifyObservers(nullptr, "compositor:created", nullptr);
3728 }));
3731 /* static */
3732 bool gfxPlatform::FallbackFromAcceleration(FeatureStatus aStatus,
3733 const char* aMessage,
3734 const nsACString& aFailureId,
3735 bool aCrashAfterFinalFallback) {
3736 // We always want to ensure (Hardware) WebRender is disabled.
3737 if (gfxConfig::IsEnabled(Feature::WEBRENDER)) {
3738 gfxConfig::GetFeature(Feature::WEBRENDER)
3739 .ForceDisable(aStatus, aMessage, aFailureId);
3742 // Determine whether or not we are allowed to use Software WebRender in
3743 // fallback without the GPU process. Either the pref is false, or the feature
3744 // is enabled and we are currently still using it.
3745 bool swglFallbackAllowed =
3746 !StaticPrefs::
3747 gfx_webrender_fallback_software_requires_gpu_process_AtStartup() ||
3748 gfxConfig::IsEnabled(Feature::GPU_PROCESS);
3750 #ifdef XP_WIN
3751 // Before we disable D3D11 and HW_COMPOSITING, we should check if we can
3752 // fallback from WebRender to Software WebRender + D3D11 compositing.
3753 if (swglFallbackAllowed && gfxVars::AllowSoftwareWebRenderD3D11() &&
3754 gfxConfig::IsEnabled(Feature::D3D11_COMPOSITING) &&
3755 !gfxVars::UseSoftwareWebRender()) {
3756 // Fallback to Software WebRender + D3D11 compositing.
3757 gfxCriticalNote << "Fallback WR to SW-WR + D3D11";
3758 gfxVars::SetUseSoftwareWebRender(true);
3759 return true;
3762 if (swglFallbackAllowed && gfxVars::AllowSoftwareWebRenderD3D11() &&
3763 gfxVars::UseSoftwareWebRender()) {
3764 // Fallback from Software WebRender + D3D11 to Software WebRender.
3765 gfxCriticalNote << "Fallback SW-WR + D3D11 to SW-WR";
3766 gfxVars::SetAllowSoftwareWebRenderD3D11(false);
3767 return true;
3770 // We aren't using Software WebRender + D3D11 compositing, so turn off the
3771 // D3D11 and D2D.
3772 if (gfxConfig::IsEnabled(Feature::DIRECT2D)) {
3773 gfxConfig::GetFeature(Feature::DIRECT2D)
3774 .ForceDisable(aStatus, aMessage, aFailureId);
3776 if (gfxConfig::IsEnabled(Feature::D3D11_COMPOSITING)) {
3777 gfxConfig::GetFeature(Feature::D3D11_COMPOSITING)
3778 .ForceDisable(aStatus, aMessage, aFailureId);
3780 #endif
3782 #if defined(MOZ_WIDGET_ANDROID) || defined(MOZ_WIDGET_GTK)
3783 // Before we disable OpenGL and HW_COMPOSITING, we should check if we can
3784 // fallback from WebRender to Software WebRender + OpenGL compositing.
3785 if (swglFallbackAllowed && gfxVars::AllowSoftwareWebRenderOGL() &&
3786 gfxConfig::IsEnabled(Feature::OPENGL_COMPOSITING) &&
3787 !gfxVars::UseSoftwareWebRender()) {
3788 // Fallback to Software WebRender + OpenGL compositing.
3789 gfxCriticalNote << "Fallback WR to SW-WR + OpenGL";
3790 gfxVars::SetUseSoftwareWebRender(true);
3791 return true;
3793 #endif
3794 // Android does not want to fallback to SW-WR.
3795 #ifdef MOZ_WIDGET_GTK
3796 if (swglFallbackAllowed && gfxVars::AllowSoftwareWebRenderOGL() &&
3797 gfxVars::UseSoftwareWebRender()) {
3798 // Fallback from Software WebRender + OpenGL to Software WebRender.
3799 gfxCriticalNote << "Fallback SW-WR + OpenGL to SW-WR";
3800 gfxVars::SetAllowSoftwareWebRenderOGL(false);
3801 return true;
3803 #endif
3805 #ifndef MOZ_WIDGET_ANDROID
3806 // Non-Android wants to fallback to Software WebRender or Basic. Android wants
3807 // to fallback to OpenGL.
3808 if (gfxConfig::IsEnabled(Feature::HW_COMPOSITING)) {
3809 gfxConfig::GetFeature(Feature::HW_COMPOSITING)
3810 .ForceDisable(aStatus, aMessage, aFailureId);
3812 #endif
3814 if (StaticPrefs::gfx_webrender_fallback_software_AtStartup() &&
3815 swglFallbackAllowed && !gfxVars::UseSoftwareWebRender()) {
3816 // Fallback from WebRender to Software WebRender.
3817 gfxCriticalNote << "Fallback WR to SW-WR";
3818 gfxVars::SetUseSoftwareWebRender(true);
3819 return true;
3822 if (!gfxVars::UseSoftwareWebRender()) {
3823 // Software WebRender may be disabled due to a startup issue with the
3824 // blocklist, despite it being our only fallback option based on the prefs.
3825 // If WebRender is unable to be initialized, this means that user would
3826 // otherwise get stuck with WebRender. As such, force a switch to Software
3827 // WebRender in this case.
3828 gfxCriticalNoteOnce << "Fallback WR to SW-WR, forced";
3829 gfxVars::SetUseSoftwareWebRender(true);
3830 return true;
3833 if (aCrashAfterFinalFallback) {
3834 MOZ_CRASH("Fallback configurations exhausted");
3837 // Continue using Software WebRender (disabled fallback to Basic).
3838 gfxCriticalNoteOnce << "Fallback remains SW-WR";
3839 return false;
3842 /* static */
3843 void gfxPlatform::DisableGPUProcess() {
3844 if (gfxVars::RemoteCanvasEnabled() &&
3845 !StaticPrefs::gfx_canvas_remote_allow_in_parent_AtStartup()) {
3846 gfxConfig::Disable(
3847 Feature::REMOTE_CANVAS, FeatureStatus::UnavailableNoGpuProcess,
3848 "Disabled by GPU process disabled",
3849 "FEATURE_REMOTE_CANVAS_DISABLED_BY_GPU_PROCESS_DISABLED"_ns);
3850 gfxVars::SetRemoteCanvasEnabled(false);
3853 if (kIsAndroid) {
3854 // On android, enable out-of-process WebGL only when GPU process exists.
3855 gfxVars::SetAllowWebglOop(false);
3856 // On android, enable accelerated canvas only when GPU process exists.
3857 gfxVars::SetUseAcceleratedCanvas2D(false);
3858 gfxConfig::Disable(Feature::ACCELERATED_CANVAS2D, FeatureStatus::Blocked,
3859 "Disabled by GPU Process disabled",
3860 "FEATURE_FAILURE_DISABLED_BY_GPU_PROCESS_DISABLED"_ns);
3863 RemoteTextureMap::Init();
3864 // We need to initialize the parent process to prepare for WebRender if we
3865 // did not end up disabling it, despite losing the GPU process.
3866 wr::RenderThread::Start(GPUProcessManager::Get()->AllocateNamespace());
3867 gfx::CanvasRenderThread::Start();
3868 image::ImageMemoryReporter::InitForWebRender();
3871 /* static */ void gfxPlatform::DisableRemoteCanvas() {
3872 if (gfxVars::RemoteCanvasEnabled()) {
3873 gfxConfig::ForceDisable(Feature::REMOTE_CANVAS, FeatureStatus::Failed,
3874 "Disabled by runtime error",
3875 "FEATURE_REMOTE_CANVAS_RUNTIME_ERROR"_ns);
3876 gfxVars::SetRemoteCanvasEnabled(false);
3878 if (gfxVars::UseAcceleratedCanvas2D()) {
3879 gfxConfig::ForceDisable(Feature::ACCELERATED_CANVAS2D,
3880 FeatureStatus::Failed, "Disabled by runtime error",
3881 "FEATURE_ACCELERATED_CANVAS2D_RUNTIME_ERROR"_ns);
3882 gfxVars::SetUseAcceleratedCanvas2D(false);
3886 void gfxPlatform::ImportCachedContentDeviceData() {
3887 MOZ_ASSERT(XRE_IsContentProcess());
3889 // Import the content device data if we've got some waiting.
3890 if (!gContentDeviceInitData) {
3891 return;
3894 ImportContentDeviceData(*gContentDeviceInitData);
3895 gContentDeviceInitData = nullptr;
3898 void gfxPlatform::ImportContentDeviceData(
3899 const mozilla::gfx::ContentDeviceData& aData) {
3900 MOZ_ASSERT(XRE_IsContentProcess());
3902 const DevicePrefs& prefs = aData.prefs();
3903 gfxConfig::Inherit(Feature::HW_COMPOSITING, prefs.hwCompositing());
3905 // We don't inherit Feature::OPENGL_COMPOSITING here, because platforms
3906 // will handle that (without imported data from the parent) in
3907 // InitOpenGLConfig.
3909 gCMSOutputProfileData = Some(aData.cmsOutputProfileData().Clone());
3912 void gfxPlatform::BuildContentDeviceData(
3913 mozilla::gfx::ContentDeviceData* aOut) {
3914 MOZ_ASSERT(XRE_IsParentProcess());
3916 // Make sure our settings are synchronized from the GPU process.
3917 DebugOnly<nsresult> rv = GPUProcessManager::Get()->EnsureGPUReady();
3918 MOZ_ASSERT(rv != NS_ERROR_ILLEGAL_DURING_SHUTDOWN);
3920 aOut->prefs().hwCompositing() = gfxConfig::GetValue(Feature::HW_COMPOSITING);
3921 aOut->prefs().oglCompositing() =
3922 gfxConfig::GetValue(Feature::OPENGL_COMPOSITING);
3925 void gfxPlatform::ImportGPUDeviceData(
3926 const mozilla::gfx::GPUDeviceData& aData) {
3927 MOZ_ASSERT(XRE_IsParentProcess());
3929 gfxConfig::ImportChange(Feature::OPENGL_COMPOSITING, aData.oglCompositing());
3932 bool gfxPlatform::SupportsApzTouchInput() const {
3933 return dom::TouchEvent::PrefEnabled(nullptr);
3936 bool gfxPlatform::SupportsApzDragInput() const {
3937 return StaticPrefs::apz_drag_enabled();
3940 bool gfxPlatform::SupportsApzKeyboardInput() const {
3941 return StaticPrefs::apz_keyboard_enabled_AtStartup();
3944 bool gfxPlatform::SupportsApzAutoscrolling() const {
3945 return StaticPrefs::apz_autoscroll_enabled();
3948 bool gfxPlatform::SupportsApzZooming() const {
3949 return StaticPrefs::apz_allow_zooming();
3952 void gfxPlatform::InitOpenGLConfig() {
3953 #ifdef XP_WIN
3954 // Don't enable by default on Windows, since it could show up in about:support
3955 // even though it'll never get used. Only attempt if user enables the pref
3956 if (!Preferences::GetBool("layers.prefer-opengl")) {
3957 return;
3959 #endif
3961 FeatureState& openGLFeature =
3962 gfxConfig::GetFeature(Feature::OPENGL_COMPOSITING);
3964 // Check to see hw comp supported
3965 if (!gfxConfig::IsEnabled(Feature::HW_COMPOSITING)) {
3966 openGLFeature.DisableByDefault(FeatureStatus::Unavailable,
3967 "Hardware compositing is disabled",
3968 "FEATURE_FAILURE_OPENGL_NEED_HWCOMP"_ns);
3969 return;
3972 #ifdef XP_WIN
3973 openGLFeature.SetDefaultFromPref(
3974 StaticPrefs::GetPrefName_layers_prefer_opengl(), true,
3975 StaticPrefs::GetPrefDefault_layers_prefer_opengl());
3976 #else
3977 openGLFeature.EnableByDefault();
3978 #endif
3980 // When layers acceleration is force-enabled, enable it even for blocklisted
3981 // devices.
3982 if (StaticPrefs::
3983 layers_acceleration_force_enabled_AtStartup_DoNotUseDirectly()) {
3984 openGLFeature.UserForceEnable("Force-enabled by pref");
3985 return;
3988 nsCString message;
3989 nsCString failureId;
3990 if (!IsGfxInfoStatusOkay(nsIGfxInfo::FEATURE_OPENGL_LAYERS, &message,
3991 failureId)) {
3992 openGLFeature.Disable(FeatureStatus::Blocklisted, message.get(), failureId);
3996 bool gfxPlatform::IsGfxInfoStatusOkay(int32_t aFeature, nsCString* aOutMessage,
3997 nsCString& aFailureId) {
3998 nsCOMPtr<nsIGfxInfo> gfxInfo = components::GfxInfo::Service();
3999 if (!gfxInfo) {
4000 return true;
4003 int32_t status;
4004 if (NS_SUCCEEDED(gfxInfo->GetFeatureStatus(aFeature, aFailureId, &status)) &&
4005 status != nsIGfxInfo::FEATURE_STATUS_OK) {
4006 aOutMessage->AssignLiteral("#BLOCKLIST_");
4007 aOutMessage->AppendASCII(aFailureId.get());
4008 return false;
4011 return true;