Bumping manifests a=b2g-bump
[gecko.git] / gfx / thebes / gfxPlatform.h
blob92fb5b4b85e6425e84a18ad4505962d45c594435
1 /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*-
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 #ifndef GFX_PLATFORM_H
7 #define GFX_PLATFORM_H
9 #include "prlog.h"
10 #include "nsTArray.h"
11 #include "nsString.h"
12 #include "nsCOMPtr.h"
13 #include "nsAutoPtr.h"
15 #include "gfxPrefs.h"
16 #include "gfxTypes.h"
17 #include "gfxFontFamilyList.h"
18 #include "gfxBlur.h"
19 #include "nsRect.h"
21 #include "qcms.h"
23 #include "mozilla/RefPtr.h"
24 #include "GfxInfoCollector.h"
26 #include "mozilla/layers/CompositorTypes.h"
28 class gfxASurface;
29 class gfxFont;
30 class gfxFontGroup;
31 struct gfxFontStyle;
32 class gfxUserFontSet;
33 class gfxFontEntry;
34 class gfxProxyFontEntry;
35 class gfxPlatformFontList;
36 class gfxTextRun;
37 class nsIURI;
38 class nsIAtom;
39 class nsIObserver;
40 struct gfxRGBA;
42 namespace mozilla {
43 namespace gl {
44 class GLContext;
45 class SkiaGLGlue;
47 namespace gfx {
48 class DrawTarget;
49 class SourceSurface;
50 class DataSourceSurface;
51 class ScaledFont;
52 class DrawEventRecorder;
54 inline uint32_t
55 BackendTypeBit(BackendType b)
57 return 1 << uint8_t(b);
62 #define MOZ_PERFORMANCE_WARNING(module, ...) \
63 do { \
64 if (gfxPrefs::PerfWarnings()) { \
65 printf_stderr("[" module "] " __VA_ARGS__); \
66 } \
67 } while (0)
69 extern cairo_user_data_key_t kDrawTarget;
71 // pref lang id's for font prefs
72 // !!! needs to match the list of pref font.default.xx entries listed in all.js !!!
73 // !!! don't use as bit mask, this may grow larger !!!
75 enum eFontPrefLang {
76 eFontPrefLang_Western = 0,
77 eFontPrefLang_Japanese = 1,
78 eFontPrefLang_ChineseTW = 2,
79 eFontPrefLang_ChineseCN = 3,
80 eFontPrefLang_ChineseHK = 4,
81 eFontPrefLang_Korean = 5,
82 eFontPrefLang_Cyrillic = 6,
83 eFontPrefLang_Greek = 7,
84 eFontPrefLang_Thai = 8,
85 eFontPrefLang_Hebrew = 9,
86 eFontPrefLang_Arabic = 10,
87 eFontPrefLang_Devanagari = 11,
88 eFontPrefLang_Tamil = 12,
89 eFontPrefLang_Armenian = 13,
90 eFontPrefLang_Bengali = 14,
91 eFontPrefLang_Canadian = 15,
92 eFontPrefLang_Ethiopic = 16,
93 eFontPrefLang_Georgian = 17,
94 eFontPrefLang_Gujarati = 18,
95 eFontPrefLang_Gurmukhi = 19,
96 eFontPrefLang_Khmer = 20,
97 eFontPrefLang_Malayalam = 21,
98 eFontPrefLang_Oriya = 22,
99 eFontPrefLang_Telugu = 23,
100 eFontPrefLang_Kannada = 24,
101 eFontPrefLang_Sinhala = 25,
102 eFontPrefLang_Tibetan = 26,
104 eFontPrefLang_Others = 27, // x-unicode
106 eFontPrefLang_CJKSet = 28 // special code for CJK set
109 enum eCMSMode {
110 eCMSMode_Off = 0, // No color management
111 eCMSMode_All = 1, // Color manage everything
112 eCMSMode_TaggedOnly = 2, // Color manage tagged Images Only
113 eCMSMode_AllCount = 3
116 enum eGfxLog {
117 // all font enumerations, localized names, fullname/psnames, cmap loads
118 eGfxLog_fontlist = 0,
119 // timing info on font initialization
120 eGfxLog_fontinit = 1,
121 // dump text runs, font matching, system fallback for content
122 eGfxLog_textrun = 2,
123 // dump text runs, font matching, system fallback for chrome
124 eGfxLog_textrunui = 3,
125 // dump cmap coverage data as they are loaded
126 eGfxLog_cmapdata = 4,
127 // text perf data
128 eGfxLog_textperf = 5
131 // when searching through pref langs, max number of pref langs
132 const uint32_t kMaxLenPrefLangList = 32;
134 #define UNINITIALIZED_VALUE (-1)
136 inline const char*
137 GetBackendName(mozilla::gfx::BackendType aBackend)
139 switch (aBackend) {
140 case mozilla::gfx::BackendType::DIRECT2D:
141 return "direct2d";
142 case mozilla::gfx::BackendType::COREGRAPHICS_ACCELERATED:
143 return "quartz accelerated";
144 case mozilla::gfx::BackendType::COREGRAPHICS:
145 return "quartz";
146 case mozilla::gfx::BackendType::CAIRO:
147 return "cairo";
148 case mozilla::gfx::BackendType::SKIA:
149 return "skia";
150 case mozilla::gfx::BackendType::RECORDING:
151 return "recording";
152 case mozilla::gfx::BackendType::DIRECT2D1_1:
153 return "direct2d 1.1";
154 case mozilla::gfx::BackendType::NONE:
155 return "none";
157 MOZ_CRASH("Incomplete switch");
160 class gfxPlatform {
161 public:
162 typedef mozilla::gfx::DataSourceSurface DataSourceSurface;
163 typedef mozilla::gfx::DrawTarget DrawTarget;
164 typedef mozilla::gfx::IntSize IntSize;
165 typedef mozilla::gfx::SourceSurface SourceSurface;
168 * Return a pointer to the current active platform.
169 * This is a singleton; it contains mostly convenience
170 * functions to obtain platform-specific objects.
172 static gfxPlatform *GetPlatform();
176 * Shut down Thebes.
177 * Init() arranges for this to be called at an appropriate time.
179 static void Shutdown();
181 static void InitLayersIPC();
182 static void ShutdownLayersIPC();
185 * Create an offscreen surface of the given dimensions
186 * and image format.
188 virtual already_AddRefed<gfxASurface>
189 CreateOffscreenSurface(const IntSize& size,
190 gfxContentType contentType) = 0;
193 * Beware that these methods may return DrawTargets which are not fully supported
194 * on the current platform and might fail silently in subtle ways. This is a massive
195 * potential footgun. You should only use these methods for canvas drawing really.
196 * Use extreme caution if you use them for content where you are not 100% sure we
197 * support the DrawTarget we get back.
198 * See SupportsAzureContentForDrawTarget.
200 virtual mozilla::TemporaryRef<DrawTarget>
201 CreateDrawTargetForSurface(gfxASurface *aSurface, const mozilla::gfx::IntSize& aSize);
203 virtual mozilla::TemporaryRef<DrawTarget>
204 CreateDrawTargetForUpdateSurface(gfxASurface *aSurface, const mozilla::gfx::IntSize& aSize);
207 * Creates a SourceSurface for a gfxASurface. This function does no caching,
208 * so the caller should cache the gfxASurface if it will be used frequently.
209 * The returned surface keeps a reference to aTarget, so it is OK to keep the
210 * surface, even if aTarget changes.
211 * aTarget should not keep a reference to the returned surface because that
212 * will cause a cycle.
214 * This function is static so that it can be accessed from
215 * PluginInstanceChild (where we can't call gfxPlatform::GetPlatform()
216 * because the prefs service can only be accessed from the main process).
218 static mozilla::TemporaryRef<SourceSurface>
219 GetSourceSurfaceForSurface(mozilla::gfx::DrawTarget *aTarget, gfxASurface *aSurface);
221 static void ClearSourceSurfaceForSurface(gfxASurface *aSurface);
223 static mozilla::TemporaryRef<DataSourceSurface>
224 GetWrappedDataSourceSurface(gfxASurface *aSurface);
226 virtual mozilla::TemporaryRef<mozilla::gfx::ScaledFont>
227 GetScaledFontForFont(mozilla::gfx::DrawTarget* aTarget, gfxFont *aFont);
229 mozilla::TemporaryRef<DrawTarget>
230 CreateOffscreenContentDrawTarget(const mozilla::gfx::IntSize& aSize, mozilla::gfx::SurfaceFormat aFormat);
232 mozilla::TemporaryRef<DrawTarget>
233 CreateOffscreenCanvasDrawTarget(const mozilla::gfx::IntSize& aSize, mozilla::gfx::SurfaceFormat aFormat);
235 virtual mozilla::TemporaryRef<DrawTarget>
236 CreateDrawTargetForData(unsigned char* aData, const mozilla::gfx::IntSize& aSize,
237 int32_t aStride, mozilla::gfx::SurfaceFormat aFormat);
240 * Returns true if we should use Azure to render content with aTarget. For
241 * example, it is possible that we are using Direct2D for rendering and thus
242 * using Azure. But we want to render to a CairoDrawTarget, in which case
243 * SupportsAzureContent will return true but SupportsAzureContentForDrawTarget
244 * will return false.
246 bool SupportsAzureContentForDrawTarget(mozilla::gfx::DrawTarget* aTarget);
248 bool SupportsAzureContentForType(mozilla::gfx::BackendType aType) {
249 return BackendTypeBit(aType) & mContentBackendBitmask;
252 virtual bool UseAcceleratedSkiaCanvas();
253 virtual void InitializeSkiaCacheLimits();
255 void GetAzureBackendInfo(mozilla::widget::InfoObject &aObj) {
256 aObj.DefineProperty("AzureCanvasBackend", GetBackendName(mPreferredCanvasBackend));
257 aObj.DefineProperty("AzureSkiaAccelerated", UseAcceleratedSkiaCanvas());
258 aObj.DefineProperty("AzureFallbackCanvasBackend", GetBackendName(mFallbackCanvasBackend));
259 aObj.DefineProperty("AzureContentBackend", GetBackendName(mContentBackend));
262 mozilla::gfx::BackendType GetContentBackend() {
263 return mContentBackend;
266 mozilla::gfx::BackendType GetPreferredCanvasBackend() {
267 return mPreferredCanvasBackend;
271 * Font bits
274 virtual void SetupClusterBoundaries(gfxTextRun *aTextRun, const char16_t *aString);
277 * Fill aListOfFonts with the results of querying the list of font names
278 * that correspond to the given language group or generic font family
279 * (or both, or neither).
281 virtual nsresult GetFontList(nsIAtom *aLangGroup,
282 const nsACString& aGenericFamily,
283 nsTArray<nsString>& aListOfFonts);
286 * Rebuilds the any cached system font lists
288 virtual nsresult UpdateFontList();
291 * Create the platform font-list object (gfxPlatformFontList concrete subclass).
292 * This function is responsible to create the appropriate subclass of
293 * gfxPlatformFontList *and* to call its InitFontList() method.
295 virtual gfxPlatformFontList *CreatePlatformFontList() {
296 NS_NOTREACHED("oops, this platform doesn't have a gfxPlatformFontList implementation");
297 return nullptr;
301 * Resolving a font name to family name. The result MUST be in the result of GetFontList().
302 * If the name doesn't in the system, aFamilyName will be empty string, but not failed.
304 virtual nsresult GetStandardFamilyName(const nsAString& aFontName, nsAString& aFamilyName) = 0;
307 * Create the appropriate platform font group
309 virtual gfxFontGroup
310 *CreateFontGroup(const mozilla::FontFamilyList& aFontFamilyList,
311 const gfxFontStyle *aStyle,
312 gfxUserFontSet *aUserFontSet) = 0;
316 * Look up a local platform font using the full font face name.
317 * (Needed to support @font-face src local().)
318 * Ownership of the returned gfxFontEntry is passed to the caller,
319 * who must either AddRef() or delete.
321 virtual gfxFontEntry* LookupLocalFont(const gfxProxyFontEntry *aProxyEntry,
322 const nsAString& aFontName)
323 { return nullptr; }
326 * Activate a platform font. (Needed to support @font-face src url().)
327 * aFontData is a NS_Malloc'ed block that must be freed by this function
328 * (or responsibility passed on) when it is no longer needed; the caller
329 * will NOT free it.
330 * Ownership of the returned gfxFontEntry is passed to the caller,
331 * who must either AddRef() or delete.
333 virtual gfxFontEntry* MakePlatformFont(const gfxProxyFontEntry *aProxyEntry,
334 const uint8_t *aFontData,
335 uint32_t aLength);
338 * Whether to allow downloadable fonts via @font-face rules
340 bool DownloadableFontsEnabled();
343 * True when hinting should be enabled. This setting shouldn't
344 * change per gecko process, while the process is live. If so the
345 * results are not defined.
347 * NB: this bit is only honored by the FT2 backend, currently.
349 virtual bool FontHintingEnabled() { return true; }
352 * True when zooming should not require reflow, so glyph metrics and
353 * positioning should not be adjusted for device pixels.
354 * If this is TRUE, then FontHintingEnabled() should be FALSE,
355 * but the converse is not necessarily required; in particular,
356 * B2G always has FontHintingEnabled FALSE, but RequiresLinearZoom
357 * is only true for the browser process, not Gaia or other apps.
359 * Like FontHintingEnabled (above), this setting shouldn't
360 * change per gecko process, while the process is live. If so the
361 * results are not defined.
363 * NB: this bit is only honored by the FT2 backend, currently.
365 virtual bool RequiresLinearZoom() { return false; }
368 * Whether to check all font cmaps during system font fallback
370 bool UseCmapsDuringSystemFallback();
373 * Whether to render SVG glyphs within an OpenType font wrapper
375 bool OpenTypeSVGEnabled();
378 * Max character length of words in the word cache
380 uint32_t WordCacheCharLimit();
383 * Max number of entries in word cache
385 uint32_t WordCacheMaxEntries();
388 * Whether to use the SIL Graphite rendering engine
389 * (for fonts that include Graphite tables)
391 bool UseGraphiteShaping();
393 // check whether format is supported on a platform or not (if unclear, returns true)
394 virtual bool IsFontFormatSupported(nsIURI *aFontURI, uint32_t aFormatFlags) { return false; }
396 virtual bool DidRenderingDeviceReset() { return false; }
398 void GetPrefFonts(nsIAtom *aLanguage, nsString& array, bool aAppendUnicode = true);
400 // in some situations, need to make decisions about ambiguous characters, may need to look at multiple pref langs
401 void GetLangPrefs(eFontPrefLang aPrefLangs[], uint32_t &aLen, eFontPrefLang aCharLang, eFontPrefLang aPageLang);
404 * Iterate over pref fonts given a list of lang groups. For a single lang
405 * group, multiple pref fonts are possible. If error occurs, returns false,
406 * true otherwise. Callback returns false to abort process.
408 typedef bool (*PrefFontCallback) (eFontPrefLang aLang, const nsAString& aName,
409 void *aClosure);
410 static bool ForEachPrefFont(eFontPrefLang aLangArray[], uint32_t aLangArrayLen,
411 PrefFontCallback aCallback,
412 void *aClosure);
414 // convert a lang group to enum constant (i.e. "zh-TW" ==> eFontPrefLang_ChineseTW)
415 static eFontPrefLang GetFontPrefLangFor(const char* aLang);
417 // convert a lang group atom to enum constant
418 static eFontPrefLang GetFontPrefLangFor(nsIAtom *aLang);
420 // convert a enum constant to lang group string (i.e. eFontPrefLang_ChineseTW ==> "zh-TW")
421 static const char* GetPrefLangName(eFontPrefLang aLang);
423 // map a Unicode range (based on char code) to a font language for Preferences
424 static eFontPrefLang GetFontPrefLangFor(uint8_t aUnicodeRange);
426 // returns true if a pref lang is CJK
427 static bool IsLangCJK(eFontPrefLang aLang);
429 // helper method to add a pref lang to an array, if not already in array
430 static void AppendPrefLang(eFontPrefLang aPrefLangs[], uint32_t& aLen, eFontPrefLang aAddLang);
432 // returns a list of commonly used fonts for a given character
433 // these are *possible* matches, no cmap-checking is done at this level
434 virtual void GetCommonFallbackFonts(const uint32_t /*aCh*/,
435 int32_t /*aRunScript*/,
436 nsTArray<const char*>& /*aFontList*/)
438 // platform-specific override, by default do nothing
441 static bool OffMainThreadCompositingEnabled();
443 static bool CanUseDirect3D9();
444 static bool CanUseDirect3D11();
447 * Is it possible to use buffer rotation. Note that these
448 * check the preference, but also allow for the override to
449 * disable it using DisableBufferRotation.
451 static bool BufferRotationEnabled();
452 static void DisableBufferRotation();
455 * Are we going to try color management?
457 static eCMSMode GetCMSMode();
460 * Determines the rendering intent for color management.
462 * If the value in the pref gfx.color_management.rendering_intent is a
463 * valid rendering intent as defined in gfx/qcms/qcms.h, that
464 * value is returned. Otherwise, -1 is returned and the embedded intent
465 * should be used.
467 * See bug 444014 for details.
469 static int GetRenderingIntent();
472 * Convert a pixel using a cms transform in an endian-aware manner.
474 * Sets 'out' to 'in' if transform is nullptr.
476 static void TransformPixel(const gfxRGBA& in, gfxRGBA& out, qcms_transform *transform);
479 * Return the output device ICC profile.
481 static qcms_profile* GetCMSOutputProfile();
484 * Return the sRGB ICC profile.
486 static qcms_profile* GetCMSsRGBProfile();
489 * Return sRGB -> output device transform.
491 static qcms_transform* GetCMSRGBTransform();
494 * Return output -> sRGB device transform.
496 static qcms_transform* GetCMSInverseRGBTransform();
499 * Return sRGBA -> output device transform.
501 static qcms_transform* GetCMSRGBATransform();
503 virtual void FontsPrefsChanged(const char *aPref);
505 int32_t GetBidiNumeralOption();
508 * Returns a 1x1 surface that can be used to create graphics contexts
509 * for measuring text etc as if they will be rendered to the screen
511 gfxASurface* ScreenReferenceSurface() { return mScreenReferenceSurface; }
512 mozilla::gfx::DrawTarget* ScreenReferenceDrawTarget() { return mScreenReferenceDrawTarget; }
514 virtual mozilla::gfx::SurfaceFormat Optimal2DFormatForContent(gfxContentType aContent);
516 virtual gfxImageFormat OptimalFormatForContent(gfxContentType aContent);
518 virtual gfxImageFormat GetOffscreenFormat()
519 { return gfxImageFormat::RGB24; }
522 * Returns a logger if one is available and logging is enabled
524 static PRLogModuleInfo* GetLog(eGfxLog aWhichLog);
526 virtual int GetScreenDepth() const;
529 * Return the layer debugging options to use browser-wide.
531 mozilla::layers::DiagnosticTypes GetLayerDiagnosticTypes();
533 static nsIntRect FrameCounterBounds() {
534 int bits = 16;
535 int sizeOfBit = 3;
536 return nsIntRect(0, 0, bits * sizeOfBit, sizeOfBit);
539 mozilla::gl::SkiaGLGlue* GetSkiaGLGlue();
540 void PurgeSkiaCache();
542 virtual bool IsInGonkEmulator() const { return false; }
544 static bool UsesOffMainThreadCompositing();
546 bool HasEnoughTotalSystemMemoryForSkiaGL();
547 protected:
548 gfxPlatform();
549 virtual ~gfxPlatform();
551 void AppendCJKPrefLangs(eFontPrefLang aPrefLangs[], uint32_t &aLen,
552 eFontPrefLang aCharLang, eFontPrefLang aPageLang);
555 * Helper method, creates a draw target for a specific Azure backend.
556 * Used by CreateOffscreenDrawTarget.
558 mozilla::TemporaryRef<DrawTarget>
559 CreateDrawTargetForBackend(mozilla::gfx::BackendType aBackend,
560 const mozilla::gfx::IntSize& aSize,
561 mozilla::gfx::SurfaceFormat aFormat);
564 * Initialise the preferred and fallback canvas backends
565 * aBackendBitmask specifies the backends which are acceptable to the caller.
566 * The backend used is determined by aBackendBitmask and the order specified
567 * by the gfx.canvas.azure.backends pref.
569 void InitBackendPrefs(uint32_t aCanvasBitmask, mozilla::gfx::BackendType aCanvasDefault,
570 uint32_t aContentBitmask, mozilla::gfx::BackendType aContentDefault);
573 * returns the first backend named in the pref gfx.canvas.azure.backends
574 * which is a component of aBackendBitmask, a bitmask of backend types
576 static mozilla::gfx::BackendType GetCanvasBackendPref(uint32_t aBackendBitmask);
579 * returns the first backend named in the pref gfx.content.azure.backend
580 * which is a component of aBackendBitmask, a bitmask of backend types
582 static mozilla::gfx::BackendType GetContentBackendPref(uint32_t &aBackendBitmask);
585 * Will return the first backend named in aBackendPrefName
586 * allowed by aBackendBitmask, a bitmask of backend types.
587 * It also modifies aBackendBitmask to only include backends that are
588 * allowed given the prefs.
590 static mozilla::gfx::BackendType GetBackendPref(const char* aBackendPrefName,
591 uint32_t &aBackendBitmask);
593 * Decode the backend enumberation from a string.
595 static mozilla::gfx::BackendType BackendTypeForName(const nsCString& aName);
597 static mozilla::TemporaryRef<mozilla::gfx::ScaledFont>
598 GetScaledFontForFontWithCairoSkia(mozilla::gfx::DrawTarget* aTarget, gfxFont* aFont);
600 int8_t mAllowDownloadableFonts;
601 int8_t mGraphiteShapingEnabled;
602 int8_t mOpenTypeSVGEnabled;
604 int8_t mBidiNumeralOption;
606 // whether to always search font cmaps globally
607 // when doing system font fallback
608 int8_t mFallbackUsesCmaps;
610 // max character limit for words in word cache
611 int32_t mWordCacheCharLimit;
613 // max number of entries in word cache
614 int32_t mWordCacheMaxEntries;
616 uint32_t mTotalSystemMemory;
618 private:
620 * Start up Thebes.
622 static void Init();
624 static void CreateCMSOutputProfile();
626 static void GetCMSOutputProfileData(void *&mem, size_t &size);
628 friend void RecordingPrefChanged(const char *aPrefName, void *aClosure);
630 virtual void GetPlatformCMSOutputProfile(void *&mem, size_t &size);
632 nsRefPtr<gfxASurface> mScreenReferenceSurface;
633 mozilla::RefPtr<mozilla::gfx::DrawTarget> mScreenReferenceDrawTarget;
634 nsTArray<uint32_t> mCJKPrefLangs;
635 nsCOMPtr<nsIObserver> mSRGBOverrideObserver;
636 nsCOMPtr<nsIObserver> mFontPrefsObserver;
637 nsCOMPtr<nsIObserver> mMemoryPressureObserver;
639 // The preferred draw target backend to use for canvas
640 mozilla::gfx::BackendType mPreferredCanvasBackend;
641 // The fallback draw target backend to use for canvas, if the preferred backend fails
642 mozilla::gfx::BackendType mFallbackCanvasBackend;
643 // The backend to use for content
644 mozilla::gfx::BackendType mContentBackend;
645 // Bitmask of backend types we can use to render content
646 uint32_t mContentBackendBitmask;
648 mozilla::widget::GfxInfoCollector<gfxPlatform> mAzureCanvasBackendCollector;
650 mozilla::RefPtr<mozilla::gfx::DrawEventRecorder> mRecorder;
651 mozilla::RefPtr<mozilla::gl::SkiaGLGlue> mSkiaGlue;
654 #endif /* GFX_PLATFORM_H */