Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(606)

Side by Side Diff: content/common/font_warmup_win.cc

Issue 1378353006: Implementation of dwrite font proxy and removal of dwrite font cache (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: More codereview fixes. Jumped the gun on previous patchset. Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « content/common/font_warmup_win.h ('k') | content/common/sandbox_win.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "content/common/font_warmup_win.h" 5 #include "content/common/font_warmup_win.h"
6 6
7 #include <dwrite.h> 7 #include <dwrite.h>
8 #include <map> 8 #include <map>
9 9
10 #include "base/debug/alias.h" 10 #include "base/debug/alias.h"
11 #include "base/files/file_path.h" 11 #include "base/files/file_path.h"
12 #include "base/lazy_instance.h" 12 #include "base/lazy_instance.h"
13 #include "base/logging.h" 13 #include "base/logging.h"
14 #include "base/memory/ref_counted.h" 14 #include "base/memory/ref_counted.h"
15 #include "base/numerics/safe_conversions.h" 15 #include "base/numerics/safe_conversions.h"
16 #include "base/numerics/safe_math.h" 16 #include "base/numerics/safe_math.h"
17 #include "base/strings/utf_string_conversions.h" 17 #include "base/strings/utf_string_conversions.h"
18 #include "base/synchronization/lock.h" 18 #include "base/synchronization/lock.h"
19 #include "base/sys_byteorder.h" 19 #include "base/sys_byteorder.h"
20 #include "base/trace_event/trace_event.h" 20 #include "base/trace_event/trace_event.h"
21 #include "base/win/iat_patch_function.h" 21 #include "base/win/iat_patch_function.h"
22 #include "base/win/windows_version.h" 22 #include "base/win/windows_version.h"
23 #include "content/public/common/dwrite_font_platform_win.h" 23 #include "ppapi/shared_impl/proxy_lock.h"
24 #include "skia/ext/fontmgr_default_win.h" 24 #include "skia/ext/fontmgr_default_win.h"
25 #include "skia/ext/refptr.h" 25 #include "skia/ext/refptr.h"
26 #include "third_party/WebKit/public/web/win/WebFontRendering.h" 26 #include "third_party/WebKit/public/web/win/WebFontRendering.h"
27 #include "third_party/skia/include/core/SkPaint.h" 27 #include "third_party/skia/include/core/SkPaint.h"
28 #include "third_party/skia/include/ports/SkFontMgr.h" 28 #include "third_party/skia/include/ports/SkFontMgr.h"
29 #include "third_party/skia/include/ports/SkTypeface_win.h" 29 #include "third_party/skia/include/ports/SkTypeface_win.h"
30 #include "ui/gfx/hud_font.h" 30 #include "ui/gfx/hud_font.h"
31 31
32 namespace content { 32 namespace content {
33 33
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
89 DWORD flags, 89 DWORD flags,
90 void* server_sid, 90 void* server_sid,
91 void* message, 91 void* message,
92 DWORD* buffer_length, 92 DWORD* buffer_length,
93 void* out_message_attributes, 93 void* out_message_attributes,
94 void* in_message_attributes, 94 void* in_message_attributes,
95 void* time_out) { 95 void* time_out) {
96 return STATUS_ACCESS_DENIED; 96 return STATUS_ACCESS_DENIED;
97 } 97 }
98 98
99 // Directwrite connects to the font cache service to retrieve information about
100 // fonts installed on the system etc. This works well outside the sandbox and
101 // within the sandbox as long as the lpc connection maintained by the current
102 // process with the font cache service remains valid. It appears that there
103 // are cases when this connection is dropped after which directwrite is unable
104 // to connect to the font cache service which causes problems with characters
105 // disappearing.
106 // Directwrite has fallback code to enumerate fonts if it is unable to connect
107 // to the font cache service. We need to intercept the following APIs to
108 // ensure that it does not connect to the font cache service.
109 // NtALpcConnectPort
110 // OpenSCManagerW
111 // OpenServiceW
112 // StartServiceW
113 // CloseServiceHandle.
114 // These are all IAT patched.
115 void PatchServiceManagerCalls() {
116 static bool is_patched = false;
117 if (is_patched)
118 return;
119 const char* service_provider_dll =
120 (base::win::GetVersion() >= base::win::VERSION_WIN8
121 ? "api-ms-win-service-management-l1-1-0.dll"
122 : "advapi32.dll");
123
124 is_patched = true;
125
126 DWORD patched =
127 g_iat_patch_open_sc_manager.Patch(L"dwrite.dll", service_provider_dll,
128 "OpenSCManagerW", OpenSCManagerWPatch);
129 DCHECK(patched == 0);
130
131 patched = g_iat_patch_close_service_handle.Patch(
132 L"dwrite.dll", service_provider_dll, "CloseServiceHandle",
133 CloseServiceHandlePatch);
134 DCHECK(patched == 0);
135
136 patched = g_iat_patch_open_service.Patch(L"dwrite.dll", service_provider_dll,
137 "OpenServiceW", OpenServiceWPatch);
138 DCHECK(patched == 0);
139
140 patched = g_iat_patch_start_service.Patch(
141 L"dwrite.dll", service_provider_dll, "StartServiceW", StartServiceWPatch);
142 DCHECK(patched == 0);
143
144 patched = g_iat_patch_nt_connect_port.Patch(
145 L"dwrite.dll", "ntdll.dll", "NtAlpcConnectPort", NtALpcConnectPortPatch);
146 DCHECK(patched == 0);
147 }
148
149 // Windows-only DirectWrite support. These warm up the DirectWrite paths
150 // before sandbox lock down to allow Skia access to the Font Manager service.
151 void CreateDirectWriteFactory(IDWriteFactory** factory) {
152 typedef decltype(DWriteCreateFactory)* DWriteCreateFactoryProc;
153 HMODULE dwrite_dll = LoadLibraryW(L"dwrite.dll");
154 // TODO(scottmg): Temporary code to track crash in http://crbug.com/387867.
155 if (!dwrite_dll) {
156 DWORD load_library_get_last_error = GetLastError();
157 base::debug::Alias(&dwrite_dll);
158 base::debug::Alias(&load_library_get_last_error);
159 CHECK(false);
160 }
161
162 PatchServiceManagerCalls();
163
164 DWriteCreateFactoryProc dwrite_create_factory_proc =
165 reinterpret_cast<DWriteCreateFactoryProc>(
166 GetProcAddress(dwrite_dll, "DWriteCreateFactory"));
167 // TODO(scottmg): Temporary code to track crash in http://crbug.com/387867.
168 if (!dwrite_create_factory_proc) {
169 DWORD get_proc_address_get_last_error = GetLastError();
170 base::debug::Alias(&dwrite_create_factory_proc);
171 base::debug::Alias(&get_proc_address_get_last_error);
172 CHECK(false);
173 }
174 CHECK(SUCCEEDED(dwrite_create_factory_proc(
175 DWRITE_FACTORY_TYPE_ISOLATED, __uuidof(IDWriteFactory),
176 reinterpret_cast<IUnknown**>(factory))));
177 }
178
179 HRESULT STDMETHODCALLTYPE StubFontCollection(IDWriteFactory* factory,
180 IDWriteFontCollection** col,
181 BOOL checkUpdates) {
182 // We always return pre-created font collection from here.
183 IDWriteFontCollection* custom_collection = GetCustomFontCollection(factory);
184 DCHECK(custom_collection != nullptr);
185 *col = custom_collection;
186 return S_OK;
187 }
188
189 void PatchDWriteFactory(IDWriteFactory* factory) {
190 const unsigned int kGetSystemFontCollectionVTableIndex = 3;
191
192 PROC* vtable = *reinterpret_cast<PROC**>(factory);
193 PROC* function_ptr = &vtable[kGetSystemFontCollectionVTableIndex];
194 void* stub_function = &StubFontCollection;
195 base::win::ModifyCode(function_ptr, &stub_function, sizeof(PROC));
196 }
197
198 // Class to fake out a DC or a Font object. Maintains a reference to a 99 // Class to fake out a DC or a Font object. Maintains a reference to a
199 // SkTypeFace to emulate the simple operation of a DC and Font. 100 // SkTypeFace to emulate the simple operation of a DC and Font.
200 class FakeGdiObject : public base::RefCountedThreadSafe<FakeGdiObject> { 101 class FakeGdiObject : public base::RefCountedThreadSafe<FakeGdiObject> {
201 public: 102 public:
202 FakeGdiObject(uint32_t magic, void* handle) 103 FakeGdiObject(uint32_t magic, void* handle)
203 : handle_(handle), magic_(magic) {} 104 : handle_(handle), magic_(magic) {}
204 105
205 void set_typeface(const skia::RefPtr<SkTypeface>& typeface) { 106 void set_typeface(const skia::RefPtr<SkTypeface>& typeface) {
206 typeface_ = typeface; 107 typeface_ = typeface;
207 } 108 }
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
296 CHECK(g_warmup_fontmgr); 197 CHECK(g_warmup_fontmgr);
297 int weight = log_font->lfWeight; 198 int weight = log_font->lfWeight;
298 if (weight == FW_DONTCARE) 199 if (weight == FW_DONTCARE)
299 weight = SkFontStyle::kNormal_Weight; 200 weight = SkFontStyle::kNormal_Weight;
300 201
301 SkFontStyle style(weight, log_font->lfWidth, 202 SkFontStyle style(weight, log_font->lfWidth,
302 log_font->lfItalic ? SkFontStyle::kItalic_Slant 203 log_font->lfItalic ? SkFontStyle::kItalic_Slant
303 : SkFontStyle::kUpright_Slant); 204 : SkFontStyle::kUpright_Slant);
304 205
305 std::string family_name = base::WideToUTF8(log_font->lfFaceName); 206 std::string family_name = base::WideToUTF8(log_font->lfFaceName);
207 ppapi::ProxyAutoLock lock;
306 return skia::AdoptRef( 208 return skia::AdoptRef(
307 g_warmup_fontmgr->matchFamilyStyle(family_name.c_str(), style)); 209 g_warmup_fontmgr->matchFamilyStyle(family_name.c_str(), style));
308 } 210 }
309 211
310 HDC WINAPI CreateCompatibleDCPatch(HDC dc_handle) { 212 HDC WINAPI CreateCompatibleDCPatch(HDC dc_handle) {
311 scoped_refptr<FakeGdiObject> ret = 213 scoped_refptr<FakeGdiObject> ret =
312 g_fake_gdi_object_factory.Get().Create(kFakeDCMagic); 214 g_fake_gdi_object_factory.Get().Create(kFakeDCMagic);
313 return static_cast<HDC>(ret->handle()); 215 return static_cast<HDC>(ret->handle());
314 } 216 }
315 217
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after
454 DoSingleGdiPatch(create_enum_font_families_patch_, path, 356 DoSingleGdiPatch(create_enum_font_families_patch_, path,
455 "EnumFontFamiliesExW", EnumFontFamiliesExWPatch); 357 "EnumFontFamiliesExW", EnumFontFamiliesExWPatch);
456 DoSingleGdiPatch(create_get_font_data_patch_, path, "GetFontData", 358 DoSingleGdiPatch(create_get_font_data_patch_, path, "GetFontData",
457 GetFontDataPatch); 359 GetFontDataPatch);
458 DoSingleGdiPatch(create_select_object_patch_, path, "SelectObject", 360 DoSingleGdiPatch(create_select_object_patch_, path, "SelectObject",
459 SelectObjectPatch); 361 SelectObjectPatch);
460 } 362 }
461 363
462 } // namespace 364 } // namespace
463 365
464 void DoPreSandboxWarmupForTypeface(SkTypeface* typeface) { 366 // Directwrite connects to the font cache service to retrieve information about
465 SkPaint paint_warmup; 367 // fonts installed on the system etc. This works well outside the sandbox and
466 paint_warmup.setTypeface(typeface); 368 // within the sandbox as long as the lpc connection maintained by the current
467 wchar_t glyph = L'S'; 369 // process with the font cache service remains valid. It appears that there
468 paint_warmup.measureText(&glyph, 2); 370 // are cases when this connection is dropped after which directwrite is unable
469 } 371 // to connect to the font cache service which causes problems with characters
372 // disappearing.
373 // Directwrite has fallback code to enumerate fonts if it is unable to connect
374 // to the font cache service. We need to intercept the following APIs to
375 // ensure that it does not connect to the font cache service.
376 // NtALpcConnectPort
377 // OpenSCManagerW
378 // OpenServiceW
379 // StartServiceW
380 // CloseServiceHandle.
381 // These are all IAT patched.
382 void PatchServiceManagerCalls() {
383 static bool is_patched = false;
384 if (is_patched)
385 return;
386 const char* service_provider_dll =
387 (base::win::GetVersion() >= base::win::VERSION_WIN8
388 ? "api-ms-win-service-management-l1-1-0.dll"
389 : "advapi32.dll");
470 390
471 SkFontMgr* GetPreSandboxWarmupFontMgr() { 391 is_patched = true;
472 if (!g_warmup_fontmgr) {
473 IDWriteFactory* factory;
474 CreateDirectWriteFactory(&factory);
475 392
476 GetCustomFontCollection(factory); 393 DWORD patched =
394 g_iat_patch_open_sc_manager.Patch(L"dwrite.dll", service_provider_dll,
395 "OpenSCManagerW", OpenSCManagerWPatch);
396 DCHECK(patched == 0);
477 397
478 PatchDWriteFactory(factory); 398 patched = g_iat_patch_close_service_handle.Patch(
399 L"dwrite.dll", service_provider_dll, "CloseServiceHandle",
400 CloseServiceHandlePatch);
401 DCHECK(patched == 0);
479 402
480 blink::WebFontRendering::setDirectWriteFactory(factory); 403 patched = g_iat_patch_open_service.Patch(L"dwrite.dll", service_provider_dll,
481 g_warmup_fontmgr = SkFontMgr_New_DirectWrite(factory); 404 "OpenServiceW", OpenServiceWPatch);
482 } 405 DCHECK(patched == 0);
483 return g_warmup_fontmgr; 406
407 patched = g_iat_patch_start_service.Patch(
408 L"dwrite.dll", service_provider_dll, "StartServiceW", StartServiceWPatch);
409 DCHECK(patched == 0);
410
411 patched = g_iat_patch_nt_connect_port.Patch(
412 L"dwrite.dll", "ntdll.dll", "NtAlpcConnectPort", NtALpcConnectPortPatch);
413 DCHECK(patched == 0);
484 } 414 }
485 415
486 GdiFontPatchData* PatchGdiFontEnumeration(const base::FilePath& path) { 416 GdiFontPatchData* PatchGdiFontEnumeration(const base::FilePath& path) {
487 // We assume the fontmgr is already warmed up before calling this. 417 // We assume the fontmgr is already warmed up before calling this.
418 g_warmup_fontmgr = SkFontMgr_New_DirectWrite();
488 DCHECK(g_warmup_fontmgr); 419 DCHECK(g_warmup_fontmgr);
489 return new GdiFontPatchDataImpl(path); 420 return new GdiFontPatchDataImpl(path);
490 } 421 }
491 422
492 size_t GetEmulatedGdiHandleCountForTesting() { 423 size_t GetEmulatedGdiHandleCountForTesting() {
493 return g_fake_gdi_object_factory.Get().GetObjectCount(); 424 return g_fake_gdi_object_factory.Get().GetObjectCount();
494 } 425 }
495 426
496 void ResetEmulatedGdiHandlesForTesting() { 427 void ResetEmulatedGdiHandlesForTesting() {
497 g_fake_gdi_object_factory.Get().ResetObjectHandles(); 428 g_fake_gdi_object_factory.Get().ResetObjectHandles();
498 } 429 }
499 430
500 void SetPreSandboxWarmupFontMgrForTesting(SkFontMgr* fontmgr) {
501 g_warmup_fontmgr = fontmgr;
502 }
503
504 void WarmupDirectWrite() {
505 TRACE_EVENT0("startup", "content::WarmupDirectWrite");
506
507 // The objects used here are intentionally not freed as we want the Skia
508 // code to use these objects after warmup.
509 SetDefaultSkiaFactory(GetPreSandboxWarmupFontMgr());
510
511 // We need to warm up *some* font for DirectWrite. We also need to pass one
512 // down for the CC HUD code, so use the same one here. Note that we don't use
513 // a monospace as would be nice in an attempt to avoid a small startup time
514 // regression, see http://crbug.com/463613.
515 skia::RefPtr<SkTypeface> hud_typeface = skia::AdoptRef(
516 GetPreSandboxWarmupFontMgr()->legacyCreateTypeface("Times New Roman", 0));
517 DoPreSandboxWarmupForTypeface(hud_typeface.get());
518 gfx::SetHudTypeface(hud_typeface);
519 }
520
521 } // namespace content 431 } // namespace content
OLDNEW
« no previous file with comments | « content/common/font_warmup_win.h ('k') | content/common/sandbox_win.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698