gfx/thebes/gfxWindowsPlatform.h

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

michael@0 1 /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*-
michael@0 2 * This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 #ifndef GFX_WINDOWS_PLATFORM_H
michael@0 7 #define GFX_WINDOWS_PLATFORM_H
michael@0 8
michael@0 9
michael@0 10 /**
michael@0 11 * XXX to get CAIRO_HAS_D2D_SURFACE, CAIRO_HAS_DWRITE_FONT
michael@0 12 * and cairo_win32_scaled_font_select_font
michael@0 13 */
michael@0 14 #include "cairo-win32.h"
michael@0 15
michael@0 16 #include "gfxFontUtils.h"
michael@0 17 #include "gfxWindowsSurface.h"
michael@0 18 #include "gfxFont.h"
michael@0 19 #ifdef CAIRO_HAS_DWRITE_FONT
michael@0 20 #include "gfxDWriteFonts.h"
michael@0 21 #endif
michael@0 22 #include "gfxPlatform.h"
michael@0 23 #include "gfxContext.h"
michael@0 24
michael@0 25 #include "nsTArray.h"
michael@0 26 #include "nsDataHashtable.h"
michael@0 27
michael@0 28 #include "mozilla/RefPtr.h"
michael@0 29
michael@0 30 #include <windows.h>
michael@0 31 #include <objbase.h>
michael@0 32
michael@0 33 #ifdef CAIRO_HAS_D2D_SURFACE
michael@0 34 #include <dxgi.h>
michael@0 35 #endif
michael@0 36
michael@0 37 // This header is available in the June 2010 SDK and in the Win8 SDK
michael@0 38 #include <d3dcommon.h>
michael@0 39 // Win 8.0 SDK types we'll need when building using older sdks.
michael@0 40 #if !defined(D3D_FEATURE_LEVEL_11_1) // defined in the 8.0 SDK only
michael@0 41 #define D3D_FEATURE_LEVEL_11_1 static_cast<D3D_FEATURE_LEVEL>(0xb100)
michael@0 42 #define D3D_FL9_1_REQ_TEXTURE2D_U_OR_V_DIMENSION 2048
michael@0 43 #define D3D_FL9_3_REQ_TEXTURE2D_U_OR_V_DIMENSION 4096
michael@0 44 #endif
michael@0 45
michael@0 46 namespace mozilla {
michael@0 47 namespace layers {
michael@0 48 class DeviceManagerD3D9;
michael@0 49 }
michael@0 50 }
michael@0 51 class IDirect3DDevice9;
michael@0 52 class ID3D11Device;
michael@0 53 class IDXGIAdapter1;
michael@0 54
michael@0 55 class nsIMemoryReporter;
michael@0 56
michael@0 57 // Utility to get a Windows HDC from a thebes context,
michael@0 58 // used by both GDI and Uniscribe font shapers
michael@0 59 struct DCFromContext {
michael@0 60 DCFromContext(gfxContext *aContext) {
michael@0 61 dc = nullptr;
michael@0 62 nsRefPtr<gfxASurface> aSurface = aContext->CurrentSurface();
michael@0 63 NS_ASSERTION(aSurface || !aContext->IsCairo(), "DCFromContext: null surface");
michael@0 64 if (aSurface &&
michael@0 65 (aSurface->GetType() == gfxSurfaceType::Win32 ||
michael@0 66 aSurface->GetType() == gfxSurfaceType::Win32Printing))
michael@0 67 {
michael@0 68 dc = static_cast<gfxWindowsSurface*>(aSurface.get())->GetDC();
michael@0 69 needsRelease = false;
michael@0 70 SaveDC(dc);
michael@0 71 cairo_scaled_font_t* scaled =
michael@0 72 cairo_get_scaled_font(aContext->GetCairo());
michael@0 73 cairo_win32_scaled_font_select_font(scaled, dc);
michael@0 74 }
michael@0 75 if (!dc) {
michael@0 76 dc = GetDC(nullptr);
michael@0 77 SetGraphicsMode(dc, GM_ADVANCED);
michael@0 78 needsRelease = true;
michael@0 79 }
michael@0 80 }
michael@0 81
michael@0 82 ~DCFromContext() {
michael@0 83 if (needsRelease) {
michael@0 84 ReleaseDC(nullptr, dc);
michael@0 85 } else {
michael@0 86 RestoreDC(dc, -1);
michael@0 87 }
michael@0 88 }
michael@0 89
michael@0 90 operator HDC () {
michael@0 91 return dc;
michael@0 92 }
michael@0 93
michael@0 94 HDC dc;
michael@0 95 bool needsRelease;
michael@0 96 };
michael@0 97
michael@0 98 // ClearType parameters set by running ClearType tuner
michael@0 99 struct ClearTypeParameterInfo {
michael@0 100 ClearTypeParameterInfo() :
michael@0 101 gamma(-1), pixelStructure(-1), clearTypeLevel(-1), enhancedContrast(-1)
michael@0 102 { }
michael@0 103
michael@0 104 nsString displayName; // typically just 'DISPLAY1'
michael@0 105 int32_t gamma;
michael@0 106 int32_t pixelStructure;
michael@0 107 int32_t clearTypeLevel;
michael@0 108 int32_t enhancedContrast;
michael@0 109 };
michael@0 110
michael@0 111 class gfxWindowsPlatform : public gfxPlatform {
michael@0 112 public:
michael@0 113 enum TextRenderingMode {
michael@0 114 TEXT_RENDERING_NO_CLEARTYPE,
michael@0 115 TEXT_RENDERING_NORMAL,
michael@0 116 TEXT_RENDERING_GDI_CLASSIC,
michael@0 117 TEXT_RENDERING_COUNT
michael@0 118 };
michael@0 119
michael@0 120 gfxWindowsPlatform();
michael@0 121 virtual ~gfxWindowsPlatform();
michael@0 122 static gfxWindowsPlatform *GetPlatform() {
michael@0 123 return (gfxWindowsPlatform*) gfxPlatform::GetPlatform();
michael@0 124 }
michael@0 125
michael@0 126 virtual gfxPlatformFontList* CreatePlatformFontList();
michael@0 127
michael@0 128 virtual already_AddRefed<gfxASurface>
michael@0 129 CreateOffscreenSurface(const IntSize& size,
michael@0 130 gfxContentType contentType) MOZ_OVERRIDE;
michael@0 131 virtual already_AddRefed<gfxASurface>
michael@0 132 CreateOffscreenImageSurface(const gfxIntSize& aSize,
michael@0 133 gfxContentType aContentType);
michael@0 134
michael@0 135 virtual mozilla::TemporaryRef<mozilla::gfx::ScaledFont>
michael@0 136 GetScaledFontForFont(mozilla::gfx::DrawTarget* aTarget, gfxFont *aFont);
michael@0 137 virtual already_AddRefed<gfxASurface>
michael@0 138 GetThebesSurfaceForDrawTarget(mozilla::gfx::DrawTarget *aTarget);
michael@0 139
michael@0 140 enum RenderMode {
michael@0 141 /* Use GDI and windows surfaces */
michael@0 142 RENDER_GDI = 0,
michael@0 143
michael@0 144 /* Use 32bpp image surfaces and call StretchDIBits */
michael@0 145 RENDER_IMAGE_STRETCH32,
michael@0 146
michael@0 147 /* Use 32bpp image surfaces, and do 32->24 conversion before calling StretchDIBits */
michael@0 148 RENDER_IMAGE_STRETCH24,
michael@0 149
michael@0 150 /* Use Direct2D rendering */
michael@0 151 RENDER_DIRECT2D,
michael@0 152
michael@0 153 /* max */
michael@0 154 RENDER_MODE_MAX
michael@0 155 };
michael@0 156
michael@0 157 int GetScreenDepth() const;
michael@0 158
michael@0 159 RenderMode GetRenderMode() { return mRenderMode; }
michael@0 160 void SetRenderMode(RenderMode rmode) { mRenderMode = rmode; }
michael@0 161
michael@0 162 /**
michael@0 163 * Updates render mode with relation to the current preferences and
michael@0 164 * available devices.
michael@0 165 */
michael@0 166 void UpdateRenderMode();
michael@0 167
michael@0 168 /**
michael@0 169 * Verifies a D2D device is present and working, will attempt to create one
michael@0 170 * it is non-functional or non-existant.
michael@0 171 *
michael@0 172 * \param aAttemptForce Attempt to force D2D cairo device creation by using
michael@0 173 * cairo device creation routines.
michael@0 174 */
michael@0 175 void VerifyD2DDevice(bool aAttemptForce);
michael@0 176
michael@0 177 #ifdef CAIRO_HAS_D2D_SURFACE
michael@0 178 HRESULT CreateDevice(nsRefPtr<IDXGIAdapter1> &adapter1, int featureLevelIndex);
michael@0 179 #endif
michael@0 180
michael@0 181 /**
michael@0 182 * Return the resolution scaling factor to convert between "logical" or
michael@0 183 * "screen" pixels as used by Windows (dependent on the DPI scaling option
michael@0 184 * in the Display control panel) and actual device pixels.
michael@0 185 */
michael@0 186 double GetDPIScale();
michael@0 187
michael@0 188 nsresult GetFontList(nsIAtom *aLangGroup,
michael@0 189 const nsACString& aGenericFamily,
michael@0 190 nsTArray<nsString>& aListOfFonts);
michael@0 191
michael@0 192 nsresult UpdateFontList();
michael@0 193
michael@0 194 virtual void GetCommonFallbackFonts(const uint32_t aCh,
michael@0 195 int32_t aRunScript,
michael@0 196 nsTArray<const char*>& aFontList);
michael@0 197
michael@0 198 nsresult ResolveFontName(const nsAString& aFontName,
michael@0 199 FontResolverCallback aCallback,
michael@0 200 void *aClosure, bool& aAborted);
michael@0 201
michael@0 202 nsresult GetStandardFamilyName(const nsAString& aFontName, nsAString& aFamilyName);
michael@0 203
michael@0 204 gfxFontGroup *CreateFontGroup(const nsAString &aFamilies,
michael@0 205 const gfxFontStyle *aStyle,
michael@0 206 gfxUserFontSet *aUserFontSet);
michael@0 207
michael@0 208 /**
michael@0 209 * Look up a local platform font using the full font face name (needed to support @font-face src local() )
michael@0 210 */
michael@0 211 virtual gfxFontEntry* LookupLocalFont(const gfxProxyFontEntry *aProxyEntry,
michael@0 212 const nsAString& aFontName);
michael@0 213
michael@0 214 /**
michael@0 215 * Activate a platform font (needed to support @font-face src url() )
michael@0 216 */
michael@0 217 virtual gfxFontEntry* MakePlatformFont(const gfxProxyFontEntry *aProxyEntry,
michael@0 218 const uint8_t *aFontData,
michael@0 219 uint32_t aLength);
michael@0 220
michael@0 221 /**
michael@0 222 * Check whether format is supported on a platform or not (if unclear, returns true)
michael@0 223 */
michael@0 224 virtual bool IsFontFormatSupported(nsIURI *aFontURI, uint32_t aFormatFlags);
michael@0 225
michael@0 226 /* Find a FontFamily/FontEntry object that represents a font on your system given a name */
michael@0 227 gfxFontFamily *FindFontFamily(const nsAString& aName);
michael@0 228 gfxFontEntry *FindFontEntry(const nsAString& aName, const gfxFontStyle& aFontStyle);
michael@0 229
michael@0 230 bool GetPrefFontEntries(const nsCString& aLangGroup, nsTArray<nsRefPtr<gfxFontEntry> > *array);
michael@0 231 void SetPrefFontEntries(const nsCString& aLangGroup, nsTArray<nsRefPtr<gfxFontEntry> >& array);
michael@0 232
michael@0 233 void ClearPrefFonts() { mPrefFonts.Clear(); }
michael@0 234
michael@0 235 // ClearType is not always enabled even when available (e.g. Windows XP)
michael@0 236 // if either of these prefs are enabled and apply, use ClearType rendering
michael@0 237 bool UseClearTypeForDownloadableFonts();
michael@0 238 bool UseClearTypeAlways();
michael@0 239
michael@0 240 static void GetDLLVersion(char16ptr_t aDLLPath, nsAString& aVersion);
michael@0 241
michael@0 242 // returns ClearType tuning information for each display
michael@0 243 static void GetCleartypeParams(nsTArray<ClearTypeParameterInfo>& aParams);
michael@0 244
michael@0 245 virtual void FontsPrefsChanged(const char *aPref);
michael@0 246
michael@0 247 void SetupClearTypeParams();
michael@0 248
michael@0 249 #ifdef CAIRO_HAS_DWRITE_FONT
michael@0 250 IDWriteFactory *GetDWriteFactory() { return mDWriteFactory; }
michael@0 251 inline bool DWriteEnabled() { return mUseDirectWrite; }
michael@0 252 inline DWRITE_MEASURING_MODE DWriteMeasuringMode() { return mMeasuringMode; }
michael@0 253 IDWriteTextAnalyzer *GetDWriteAnalyzer() { return mDWriteAnalyzer; }
michael@0 254
michael@0 255 IDWriteRenderingParams *GetRenderingParams(TextRenderingMode aRenderMode)
michael@0 256 { return mRenderingParams[aRenderMode]; }
michael@0 257 #else
michael@0 258 inline bool DWriteEnabled() { return false; }
michael@0 259 #endif
michael@0 260 void OnDeviceManagerDestroy(mozilla::layers::DeviceManagerD3D9* aDeviceManager);
michael@0 261 mozilla::layers::DeviceManagerD3D9* GetD3D9DeviceManager();
michael@0 262 IDirect3DDevice9* GetD3D9Device();
michael@0 263 #ifdef CAIRO_HAS_D2D_SURFACE
michael@0 264 cairo_device_t *GetD2DDevice() { return mD2DDevice; }
michael@0 265 ID3D10Device1 *GetD3D10Device() { return mD2DDevice ? cairo_d2d_device_get_device(mD2DDevice) : nullptr; }
michael@0 266 #endif
michael@0 267 ID3D11Device *GetD3D11Device();
michael@0 268
michael@0 269 static bool IsOptimus();
michael@0 270
michael@0 271 protected:
michael@0 272 RenderMode mRenderMode;
michael@0 273
michael@0 274 int8_t mUseClearTypeForDownloadableFonts;
michael@0 275 int8_t mUseClearTypeAlways;
michael@0 276
michael@0 277 private:
michael@0 278 void Init();
michael@0 279 IDXGIAdapter1 *GetDXGIAdapter();
michael@0 280
michael@0 281 bool mUseDirectWrite;
michael@0 282 bool mUsingGDIFonts;
michael@0 283
michael@0 284 #ifdef CAIRO_HAS_DWRITE_FONT
michael@0 285 nsRefPtr<IDWriteFactory> mDWriteFactory;
michael@0 286 nsRefPtr<IDWriteTextAnalyzer> mDWriteAnalyzer;
michael@0 287 nsRefPtr<IDWriteRenderingParams> mRenderingParams[TEXT_RENDERING_COUNT];
michael@0 288 DWRITE_MEASURING_MODE mMeasuringMode;
michael@0 289 #endif
michael@0 290 #ifdef CAIRO_HAS_D2D_SURFACE
michael@0 291 cairo_device_t *mD2DDevice;
michael@0 292 #endif
michael@0 293 mozilla::RefPtr<IDXGIAdapter1> mAdapter;
michael@0 294 nsRefPtr<mozilla::layers::DeviceManagerD3D9> mDeviceManager;
michael@0 295 mozilla::RefPtr<ID3D11Device> mD3D11Device;
michael@0 296 bool mD3D11DeviceInitialized;
michael@0 297
michael@0 298 virtual void GetPlatformCMSOutputProfile(void* &mem, size_t &size);
michael@0 299
michael@0 300 // TODO: unify this with mPrefFonts (NB: holds families, not fonts) in gfxPlatformFontList
michael@0 301 nsDataHashtable<nsCStringHashKey, nsTArray<nsRefPtr<gfxFontEntry> > > mPrefFonts;
michael@0 302 };
michael@0 303
michael@0 304 #endif /* GFX_WINDOWS_PLATFORM_H */

mercurial