OLD | NEW |
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/child/font_warmup_win.h" | 5 #include "content/child/font_warmup_win.h" |
6 | 6 |
7 #include <dwrite.h> | 7 #include <dwrite.h> |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 #include <map> | 9 #include <map> |
10 | 10 |
11 #include "base/debug/alias.h" | 11 #include "base/debug/alias.h" |
12 #include "base/files/file_path.h" | 12 #include "base/files/file_path.h" |
13 #include "base/lazy_instance.h" | 13 #include "base/lazy_instance.h" |
14 #include "base/logging.h" | 14 #include "base/logging.h" |
15 #include "base/macros.h" | 15 #include "base/macros.h" |
16 #include "base/memory/ref_counted.h" | 16 #include "base/memory/ref_counted.h" |
17 #include "base/numerics/safe_conversions.h" | 17 #include "base/numerics/safe_conversions.h" |
18 #include "base/numerics/safe_math.h" | 18 #include "base/numerics/safe_math.h" |
19 #include "base/strings/utf_string_conversions.h" | 19 #include "base/strings/utf_string_conversions.h" |
20 #include "base/synchronization/lock.h" | 20 #include "base/synchronization/lock.h" |
21 #include "base/sys_byteorder.h" | 21 #include "base/sys_byteorder.h" |
22 #include "base/trace_event/trace_event.h" | 22 #include "base/trace_event/trace_event.h" |
23 #include "base/win/iat_patch_function.h" | 23 #include "base/win/iat_patch_function.h" |
24 #include "base/win/windows_version.h" | 24 #include "base/win/windows_version.h" |
| 25 #include "content/public/common/dwrite_font_platform_win.h" |
25 #include "ppapi/shared_impl/proxy_lock.h" | 26 #include "ppapi/shared_impl/proxy_lock.h" |
26 #include "skia/ext/fontmgr_default_win.h" | 27 #include "skia/ext/fontmgr_default_win.h" |
27 #include "skia/ext/refptr.h" | 28 #include "skia/ext/refptr.h" |
| 29 #include "third_party/WebKit/public/web/win/WebFontRendering.h" |
| 30 #include "third_party/skia/include/core/SkPaint.h" |
28 #include "third_party/skia/include/ports/SkFontMgr.h" | 31 #include "third_party/skia/include/ports/SkFontMgr.h" |
29 #include "third_party/skia/include/ports/SkTypeface_win.h" | 32 #include "third_party/skia/include/ports/SkTypeface_win.h" |
30 | 33 |
31 namespace content { | 34 namespace content { |
32 | 35 |
33 namespace { | 36 namespace { |
34 | 37 |
35 // The Skia font manager, used for the life of the process (leaked at the end). | 38 // The Skia font manager, used for the life of the process (leaked at the end). |
36 SkFontMgr* g_warmup_fontmgr = nullptr; | 39 SkFontMgr* g_warmup_fontmgr = nullptr; |
37 | 40 |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
89 DWORD flags, | 92 DWORD flags, |
90 void* server_sid, | 93 void* server_sid, |
91 void* message, | 94 void* message, |
92 DWORD* buffer_length, | 95 DWORD* buffer_length, |
93 void* out_message_attributes, | 96 void* out_message_attributes, |
94 void* in_message_attributes, | 97 void* in_message_attributes, |
95 void* time_out) { | 98 void* time_out) { |
96 return STATUS_ACCESS_DENIED; | 99 return STATUS_ACCESS_DENIED; |
97 } | 100 } |
98 | 101 |
| 102 // Windows-only DirectWrite support. These warm up the DirectWrite paths |
| 103 // before sandbox lock down to allow Skia access to the Font Manager service. |
| 104 void CreateDirectWriteFactory(IDWriteFactory** factory) { |
| 105 typedef decltype(DWriteCreateFactory)* DWriteCreateFactoryProc; |
| 106 HMODULE dwrite_dll = LoadLibraryW(L"dwrite.dll"); |
| 107 // TODO(scottmg): Temporary code to track crash in http://crbug.com/387867. |
| 108 if (!dwrite_dll) { |
| 109 DWORD load_library_get_last_error = GetLastError(); |
| 110 base::debug::Alias(&dwrite_dll); |
| 111 base::debug::Alias(&load_library_get_last_error); |
| 112 CHECK(false); |
| 113 } |
| 114 |
| 115 PatchServiceManagerCalls(); |
| 116 |
| 117 DWriteCreateFactoryProc dwrite_create_factory_proc = |
| 118 reinterpret_cast<DWriteCreateFactoryProc>( |
| 119 GetProcAddress(dwrite_dll, "DWriteCreateFactory")); |
| 120 // TODO(scottmg): Temporary code to track crash in http://crbug.com/387867. |
| 121 if (!dwrite_create_factory_proc) { |
| 122 DWORD get_proc_address_get_last_error = GetLastError(); |
| 123 base::debug::Alias(&dwrite_create_factory_proc); |
| 124 base::debug::Alias(&get_proc_address_get_last_error); |
| 125 CHECK(false); |
| 126 } |
| 127 CHECK(SUCCEEDED(dwrite_create_factory_proc( |
| 128 DWRITE_FACTORY_TYPE_ISOLATED, __uuidof(IDWriteFactory), |
| 129 reinterpret_cast<IUnknown**>(factory)))); |
| 130 } |
| 131 |
99 // Class to fake out a DC or a Font object. Maintains a reference to a | 132 // Class to fake out a DC or a Font object. Maintains a reference to a |
100 // SkTypeFace to emulate the simple operation of a DC and Font. | 133 // SkTypeFace to emulate the simple operation of a DC and Font. |
101 class FakeGdiObject : public base::RefCountedThreadSafe<FakeGdiObject> { | 134 class FakeGdiObject : public base::RefCountedThreadSafe<FakeGdiObject> { |
102 public: | 135 public: |
103 FakeGdiObject(uint32_t magic, void* handle) | 136 FakeGdiObject(uint32_t magic, void* handle) |
104 : handle_(handle), magic_(magic) {} | 137 : handle_(handle), magic_(magic) {} |
105 | 138 |
106 void set_typeface(const skia::RefPtr<SkTypeface>& typeface) { | 139 void set_typeface(const skia::RefPtr<SkTypeface>& typeface) { |
107 typeface_ = typeface; | 140 typeface_ = typeface; |
108 } | 141 } |
(...skipping 297 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
406 | 439 |
407 patched = g_iat_patch_start_service.Patch( | 440 patched = g_iat_patch_start_service.Patch( |
408 L"dwrite.dll", service_provider_dll, "StartServiceW", StartServiceWPatch); | 441 L"dwrite.dll", service_provider_dll, "StartServiceW", StartServiceWPatch); |
409 DCHECK(patched == 0); | 442 DCHECK(patched == 0); |
410 | 443 |
411 patched = g_iat_patch_nt_connect_port.Patch( | 444 patched = g_iat_patch_nt_connect_port.Patch( |
412 L"dwrite.dll", "ntdll.dll", "NtAlpcConnectPort", NtALpcConnectPortPatch); | 445 L"dwrite.dll", "ntdll.dll", "NtAlpcConnectPort", NtALpcConnectPortPatch); |
413 DCHECK(patched == 0); | 446 DCHECK(patched == 0); |
414 } | 447 } |
415 | 448 |
| 449 void DoPreSandboxWarmupForTypeface(SkTypeface* typeface) { |
| 450 SkPaint paint_warmup; |
| 451 paint_warmup.setTypeface(typeface); |
| 452 wchar_t glyph = L'S'; |
| 453 paint_warmup.measureText(&glyph, 2); |
| 454 } |
| 455 |
| 456 SkFontMgr* GetPreSandboxWarmupFontMgr() { |
| 457 if (!g_warmup_fontmgr) { |
| 458 IDWriteFactory* factory; |
| 459 CreateDirectWriteFactory(&factory); |
| 460 |
| 461 g_warmup_fontmgr = |
| 462 SkFontMgr_New_DirectWrite(factory, GetCustomFontCollection(factory)); |
| 463 blink::WebFontRendering::setSkiaFontManager(g_warmup_fontmgr); |
| 464 } |
| 465 return g_warmup_fontmgr; |
| 466 } |
| 467 |
416 GdiFontPatchData* PatchGdiFontEnumeration(const base::FilePath& path) { | 468 GdiFontPatchData* PatchGdiFontEnumeration(const base::FilePath& path) { |
417 if (!g_warmup_fontmgr) | 469 if (ShouldUseDirectWriteFontProxyFieldTrial() && !g_warmup_fontmgr) |
418 g_warmup_fontmgr = SkFontMgr_New_DirectWrite(); | 470 g_warmup_fontmgr = SkFontMgr_New_DirectWrite(); |
| 471 // If not using the font proxy, we assume |g_warmup_fontmgr| is already |
| 472 // initialized before this function is called. |
419 DCHECK(g_warmup_fontmgr); | 473 DCHECK(g_warmup_fontmgr); |
420 return new GdiFontPatchDataImpl(path); | 474 return new GdiFontPatchDataImpl(path); |
421 } | 475 } |
422 | 476 |
423 size_t GetEmulatedGdiHandleCountForTesting() { | 477 size_t GetEmulatedGdiHandleCountForTesting() { |
424 return g_fake_gdi_object_factory.Get().GetObjectCount(); | 478 return g_fake_gdi_object_factory.Get().GetObjectCount(); |
425 } | 479 } |
426 | 480 |
427 void ResetEmulatedGdiHandlesForTesting() { | 481 void ResetEmulatedGdiHandlesForTesting() { |
428 g_fake_gdi_object_factory.Get().ResetObjectHandles(); | 482 g_fake_gdi_object_factory.Get().ResetObjectHandles(); |
429 } | 483 } |
430 | 484 |
431 void SetPreSandboxWarmupFontMgrForTesting(SkFontMgr* fontmgr) { | 485 void SetPreSandboxWarmupFontMgrForTesting(SkFontMgr* fontmgr) { |
432 g_warmup_fontmgr = fontmgr; | 486 g_warmup_fontmgr = fontmgr; |
433 } | 487 } |
434 | 488 |
| 489 void WarmupDirectWrite() { |
| 490 TRACE_EVENT0("startup", "content::WarmupDirectWrite"); |
| 491 |
| 492 // The objects used here are intentionally not freed as we want the Skia |
| 493 // code to use these objects after warmup. |
| 494 SetDefaultSkiaFactory(GetPreSandboxWarmupFontMgr()); |
| 495 |
| 496 // We need to warm up *some* font for DirectWrite. Note that we don't use |
| 497 // a monospace as would be nice in an attempt to avoid a small startup time |
| 498 // regression, see http://crbug.com/463613. |
| 499 skia::RefPtr<SkTypeface> hud_typeface = skia::AdoptRef( |
| 500 GetPreSandboxWarmupFontMgr()->legacyCreateTypeface("Times New Roman", 0)); |
| 501 DoPreSandboxWarmupForTypeface(hud_typeface.get()); |
| 502 } |
| 503 |
435 } // namespace content | 504 } // namespace content |
OLD | NEW |