Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / ui / gfx / win / direct_write.cc
blob38c926a9bd11da661c3e66b614c177427a6bf6ae
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ui/gfx/win/direct_write.h"
7 #include <dwrite.h>
9 #include "base/basictypes.h"
10 #include "base/command_line.h"
11 #include "base/metrics/field_trial.h"
12 #include "base/win/registry.h"
13 #include "base/win/scoped_comptr.h"
14 #include "base/win/windows_version.h"
15 #include "skia/ext/fontmgr_default_win.h"
16 #include "third_party/skia/include/ports/SkTypeface_win.h"
17 #include "ui/gfx/platform_font_win.h"
18 #include "ui/gfx/switches.h"
19 #include "ui/gfx/win/dpi.h"
21 namespace gfx {
22 namespace win {
24 namespace {
26 static bool dwrite_enabled = false;
30 bool ShouldUseDirectWrite() {
31 // If the flag is currently on, and we're on Win7 or above, we enable
32 // DirectWrite. Skia does not require the additions to DirectWrite in QFE
33 // 2670838, but a simple 'better than XP' check is not enough.
34 if (base::win::GetVersion() < base::win::VERSION_WIN7)
35 return false;
37 base::win::OSInfo::VersionNumber os_version =
38 base::win::OSInfo::GetInstance()->version_number();
39 if ((os_version.major == 6) && (os_version.minor == 1)) {
40 // We can't use DirectWrite for pre-release versions of Windows 7.
41 if (os_version.build < 7600)
42 return false;
44 // If forced off, don't use it.
45 const base::CommandLine& command_line =
46 *base::CommandLine::ForCurrentProcess();
47 if (command_line.HasSwitch(switches::kDisableDirectWrite))
48 return false;
50 // Can't use GDI on HiDPI.
51 if (gfx::GetDPIScale() > 1.0f)
52 return true;
54 // Otherwise, check the field trial.
55 const std::string group_name =
56 base::FieldTrialList::FindFullName("DirectWrite");
57 return group_name != "Disabled";
60 void MaybeInitializeDirectWrite() {
61 static bool tried_dwrite_initialize = false;
62 if (tried_dwrite_initialize)
63 return;
64 tried_dwrite_initialize = true;
66 if (!ShouldUseDirectWrite() ||
67 base::CommandLine::ForCurrentProcess()->HasSwitch(
68 switches::kDisableDirectWriteForUI)) {
69 return;
72 using DWriteCreateFactoryProc = decltype(DWriteCreateFactory)*;
73 HMODULE dwrite_dll = LoadLibraryW(L"dwrite.dll");
74 if (!dwrite_dll)
75 return;
77 DWriteCreateFactoryProc dwrite_create_factory_proc =
78 reinterpret_cast<DWriteCreateFactoryProc>(
79 GetProcAddress(dwrite_dll, "DWriteCreateFactory"));
80 // Not finding the DWriteCreateFactory function indicates a corrupt dll.
81 if (!dwrite_create_factory_proc)
82 return;
84 base::win::ScopedComPtr<IDWriteFactory> factory;
86 // Failure to create the DirectWrite factory indicates a corrupt dll.
87 if (FAILED(dwrite_create_factory_proc(
88 DWRITE_FACTORY_TYPE_SHARED,
89 __uuidof(IDWriteFactory),
90 reinterpret_cast<IUnknown**>(factory.Receive()))))
91 return;
93 // The skia call to create a new DirectWrite font manager instance can fail
94 // if we are unable to get the system font collection from the DirectWrite
95 // factory. The GetSystemFontCollection method in the IDWriteFactory
96 // interface fails with E_INVALIDARG on certain Windows 7 gold versions
97 // (6.1.7600.*). We should just use GDI in these cases.
98 SkFontMgr* direct_write_font_mgr = SkFontMgr_New_DirectWrite(factory.get());
99 if (!direct_write_font_mgr)
100 return;
101 dwrite_enabled = true;
102 SetDefaultSkiaFactory(direct_write_font_mgr);
103 gfx::PlatformFontWin::SetDirectWriteFactory(factory.get());
106 bool IsDirectWriteEnabled() {
107 return dwrite_enabled;
110 } // namespace win
111 } // namespace gfx