michael@0: /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*- michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef GFX_WINDOWS_PLATFORM_H michael@0: #define GFX_WINDOWS_PLATFORM_H michael@0: michael@0: michael@0: /** michael@0: * XXX to get CAIRO_HAS_D2D_SURFACE, CAIRO_HAS_DWRITE_FONT michael@0: * and cairo_win32_scaled_font_select_font michael@0: */ michael@0: #include "cairo-win32.h" michael@0: michael@0: #include "gfxFontUtils.h" michael@0: #include "gfxWindowsSurface.h" michael@0: #include "gfxFont.h" michael@0: #ifdef CAIRO_HAS_DWRITE_FONT michael@0: #include "gfxDWriteFonts.h" michael@0: #endif michael@0: #include "gfxPlatform.h" michael@0: #include "gfxContext.h" michael@0: michael@0: #include "nsTArray.h" michael@0: #include "nsDataHashtable.h" michael@0: michael@0: #include "mozilla/RefPtr.h" michael@0: michael@0: #include michael@0: #include michael@0: michael@0: #ifdef CAIRO_HAS_D2D_SURFACE michael@0: #include michael@0: #endif michael@0: michael@0: // This header is available in the June 2010 SDK and in the Win8 SDK michael@0: #include michael@0: // Win 8.0 SDK types we'll need when building using older sdks. michael@0: #if !defined(D3D_FEATURE_LEVEL_11_1) // defined in the 8.0 SDK only michael@0: #define D3D_FEATURE_LEVEL_11_1 static_cast(0xb100) michael@0: #define D3D_FL9_1_REQ_TEXTURE2D_U_OR_V_DIMENSION 2048 michael@0: #define D3D_FL9_3_REQ_TEXTURE2D_U_OR_V_DIMENSION 4096 michael@0: #endif michael@0: michael@0: namespace mozilla { michael@0: namespace layers { michael@0: class DeviceManagerD3D9; michael@0: } michael@0: } michael@0: class IDirect3DDevice9; michael@0: class ID3D11Device; michael@0: class IDXGIAdapter1; michael@0: michael@0: class nsIMemoryReporter; michael@0: michael@0: // Utility to get a Windows HDC from a thebes context, michael@0: // used by both GDI and Uniscribe font shapers michael@0: struct DCFromContext { michael@0: DCFromContext(gfxContext *aContext) { michael@0: dc = nullptr; michael@0: nsRefPtr aSurface = aContext->CurrentSurface(); michael@0: NS_ASSERTION(aSurface || !aContext->IsCairo(), "DCFromContext: null surface"); michael@0: if (aSurface && michael@0: (aSurface->GetType() == gfxSurfaceType::Win32 || michael@0: aSurface->GetType() == gfxSurfaceType::Win32Printing)) michael@0: { michael@0: dc = static_cast(aSurface.get())->GetDC(); michael@0: needsRelease = false; michael@0: SaveDC(dc); michael@0: cairo_scaled_font_t* scaled = michael@0: cairo_get_scaled_font(aContext->GetCairo()); michael@0: cairo_win32_scaled_font_select_font(scaled, dc); michael@0: } michael@0: if (!dc) { michael@0: dc = GetDC(nullptr); michael@0: SetGraphicsMode(dc, GM_ADVANCED); michael@0: needsRelease = true; michael@0: } michael@0: } michael@0: michael@0: ~DCFromContext() { michael@0: if (needsRelease) { michael@0: ReleaseDC(nullptr, dc); michael@0: } else { michael@0: RestoreDC(dc, -1); michael@0: } michael@0: } michael@0: michael@0: operator HDC () { michael@0: return dc; michael@0: } michael@0: michael@0: HDC dc; michael@0: bool needsRelease; michael@0: }; michael@0: michael@0: // ClearType parameters set by running ClearType tuner michael@0: struct ClearTypeParameterInfo { michael@0: ClearTypeParameterInfo() : michael@0: gamma(-1), pixelStructure(-1), clearTypeLevel(-1), enhancedContrast(-1) michael@0: { } michael@0: michael@0: nsString displayName; // typically just 'DISPLAY1' michael@0: int32_t gamma; michael@0: int32_t pixelStructure; michael@0: int32_t clearTypeLevel; michael@0: int32_t enhancedContrast; michael@0: }; michael@0: michael@0: class gfxWindowsPlatform : public gfxPlatform { michael@0: public: michael@0: enum TextRenderingMode { michael@0: TEXT_RENDERING_NO_CLEARTYPE, michael@0: TEXT_RENDERING_NORMAL, michael@0: TEXT_RENDERING_GDI_CLASSIC, michael@0: TEXT_RENDERING_COUNT michael@0: }; michael@0: michael@0: gfxWindowsPlatform(); michael@0: virtual ~gfxWindowsPlatform(); michael@0: static gfxWindowsPlatform *GetPlatform() { michael@0: return (gfxWindowsPlatform*) gfxPlatform::GetPlatform(); michael@0: } michael@0: michael@0: virtual gfxPlatformFontList* CreatePlatformFontList(); michael@0: michael@0: virtual already_AddRefed michael@0: CreateOffscreenSurface(const IntSize& size, michael@0: gfxContentType contentType) MOZ_OVERRIDE; michael@0: virtual already_AddRefed michael@0: CreateOffscreenImageSurface(const gfxIntSize& aSize, michael@0: gfxContentType aContentType); michael@0: michael@0: virtual mozilla::TemporaryRef michael@0: GetScaledFontForFont(mozilla::gfx::DrawTarget* aTarget, gfxFont *aFont); michael@0: virtual already_AddRefed michael@0: GetThebesSurfaceForDrawTarget(mozilla::gfx::DrawTarget *aTarget); michael@0: michael@0: enum RenderMode { michael@0: /* Use GDI and windows surfaces */ michael@0: RENDER_GDI = 0, michael@0: michael@0: /* Use 32bpp image surfaces and call StretchDIBits */ michael@0: RENDER_IMAGE_STRETCH32, michael@0: michael@0: /* Use 32bpp image surfaces, and do 32->24 conversion before calling StretchDIBits */ michael@0: RENDER_IMAGE_STRETCH24, michael@0: michael@0: /* Use Direct2D rendering */ michael@0: RENDER_DIRECT2D, michael@0: michael@0: /* max */ michael@0: RENDER_MODE_MAX michael@0: }; michael@0: michael@0: int GetScreenDepth() const; michael@0: michael@0: RenderMode GetRenderMode() { return mRenderMode; } michael@0: void SetRenderMode(RenderMode rmode) { mRenderMode = rmode; } michael@0: michael@0: /** michael@0: * Updates render mode with relation to the current preferences and michael@0: * available devices. michael@0: */ michael@0: void UpdateRenderMode(); michael@0: michael@0: /** michael@0: * Verifies a D2D device is present and working, will attempt to create one michael@0: * it is non-functional or non-existant. michael@0: * michael@0: * \param aAttemptForce Attempt to force D2D cairo device creation by using michael@0: * cairo device creation routines. michael@0: */ michael@0: void VerifyD2DDevice(bool aAttemptForce); michael@0: michael@0: #ifdef CAIRO_HAS_D2D_SURFACE michael@0: HRESULT CreateDevice(nsRefPtr &adapter1, int featureLevelIndex); michael@0: #endif michael@0: michael@0: /** michael@0: * Return the resolution scaling factor to convert between "logical" or michael@0: * "screen" pixels as used by Windows (dependent on the DPI scaling option michael@0: * in the Display control panel) and actual device pixels. michael@0: */ michael@0: double GetDPIScale(); michael@0: michael@0: nsresult GetFontList(nsIAtom *aLangGroup, michael@0: const nsACString& aGenericFamily, michael@0: nsTArray& aListOfFonts); michael@0: michael@0: nsresult UpdateFontList(); michael@0: michael@0: virtual void GetCommonFallbackFonts(const uint32_t aCh, michael@0: int32_t aRunScript, michael@0: nsTArray& aFontList); michael@0: michael@0: nsresult ResolveFontName(const nsAString& aFontName, michael@0: FontResolverCallback aCallback, michael@0: void *aClosure, bool& aAborted); michael@0: michael@0: nsresult GetStandardFamilyName(const nsAString& aFontName, nsAString& aFamilyName); michael@0: michael@0: gfxFontGroup *CreateFontGroup(const nsAString &aFamilies, michael@0: const gfxFontStyle *aStyle, michael@0: gfxUserFontSet *aUserFontSet); michael@0: michael@0: /** michael@0: * Look up a local platform font using the full font face name (needed to support @font-face src local() ) michael@0: */ michael@0: virtual gfxFontEntry* LookupLocalFont(const gfxProxyFontEntry *aProxyEntry, michael@0: const nsAString& aFontName); michael@0: michael@0: /** michael@0: * Activate a platform font (needed to support @font-face src url() ) michael@0: */ michael@0: virtual gfxFontEntry* MakePlatformFont(const gfxProxyFontEntry *aProxyEntry, michael@0: const uint8_t *aFontData, michael@0: uint32_t aLength); michael@0: michael@0: /** michael@0: * Check whether format is supported on a platform or not (if unclear, returns true) michael@0: */ michael@0: virtual bool IsFontFormatSupported(nsIURI *aFontURI, uint32_t aFormatFlags); michael@0: michael@0: /* Find a FontFamily/FontEntry object that represents a font on your system given a name */ michael@0: gfxFontFamily *FindFontFamily(const nsAString& aName); michael@0: gfxFontEntry *FindFontEntry(const nsAString& aName, const gfxFontStyle& aFontStyle); michael@0: michael@0: bool GetPrefFontEntries(const nsCString& aLangGroup, nsTArray > *array); michael@0: void SetPrefFontEntries(const nsCString& aLangGroup, nsTArray >& array); michael@0: michael@0: void ClearPrefFonts() { mPrefFonts.Clear(); } michael@0: michael@0: // ClearType is not always enabled even when available (e.g. Windows XP) michael@0: // if either of these prefs are enabled and apply, use ClearType rendering michael@0: bool UseClearTypeForDownloadableFonts(); michael@0: bool UseClearTypeAlways(); michael@0: michael@0: static void GetDLLVersion(char16ptr_t aDLLPath, nsAString& aVersion); michael@0: michael@0: // returns ClearType tuning information for each display michael@0: static void GetCleartypeParams(nsTArray& aParams); michael@0: michael@0: virtual void FontsPrefsChanged(const char *aPref); michael@0: michael@0: void SetupClearTypeParams(); michael@0: michael@0: #ifdef CAIRO_HAS_DWRITE_FONT michael@0: IDWriteFactory *GetDWriteFactory() { return mDWriteFactory; } michael@0: inline bool DWriteEnabled() { return mUseDirectWrite; } michael@0: inline DWRITE_MEASURING_MODE DWriteMeasuringMode() { return mMeasuringMode; } michael@0: IDWriteTextAnalyzer *GetDWriteAnalyzer() { return mDWriteAnalyzer; } michael@0: michael@0: IDWriteRenderingParams *GetRenderingParams(TextRenderingMode aRenderMode) michael@0: { return mRenderingParams[aRenderMode]; } michael@0: #else michael@0: inline bool DWriteEnabled() { return false; } michael@0: #endif michael@0: void OnDeviceManagerDestroy(mozilla::layers::DeviceManagerD3D9* aDeviceManager); michael@0: mozilla::layers::DeviceManagerD3D9* GetD3D9DeviceManager(); michael@0: IDirect3DDevice9* GetD3D9Device(); michael@0: #ifdef CAIRO_HAS_D2D_SURFACE michael@0: cairo_device_t *GetD2DDevice() { return mD2DDevice; } michael@0: ID3D10Device1 *GetD3D10Device() { return mD2DDevice ? cairo_d2d_device_get_device(mD2DDevice) : nullptr; } michael@0: #endif michael@0: ID3D11Device *GetD3D11Device(); michael@0: michael@0: static bool IsOptimus(); michael@0: michael@0: protected: michael@0: RenderMode mRenderMode; michael@0: michael@0: int8_t mUseClearTypeForDownloadableFonts; michael@0: int8_t mUseClearTypeAlways; michael@0: michael@0: private: michael@0: void Init(); michael@0: IDXGIAdapter1 *GetDXGIAdapter(); michael@0: michael@0: bool mUseDirectWrite; michael@0: bool mUsingGDIFonts; michael@0: michael@0: #ifdef CAIRO_HAS_DWRITE_FONT michael@0: nsRefPtr mDWriteFactory; michael@0: nsRefPtr mDWriteAnalyzer; michael@0: nsRefPtr mRenderingParams[TEXT_RENDERING_COUNT]; michael@0: DWRITE_MEASURING_MODE mMeasuringMode; michael@0: #endif michael@0: #ifdef CAIRO_HAS_D2D_SURFACE michael@0: cairo_device_t *mD2DDevice; michael@0: #endif michael@0: mozilla::RefPtr mAdapter; michael@0: nsRefPtr mDeviceManager; michael@0: mozilla::RefPtr mD3D11Device; michael@0: bool mD3D11DeviceInitialized; michael@0: michael@0: virtual void GetPlatformCMSOutputProfile(void* &mem, size_t &size); michael@0: michael@0: // TODO: unify this with mPrefFonts (NB: holds families, not fonts) in gfxPlatformFontList michael@0: nsDataHashtable > > mPrefFonts; michael@0: }; michael@0: michael@0: #endif /* GFX_WINDOWS_PLATFORM_H */