| 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" | |
| 26 #include "ppapi/shared_impl/proxy_lock.h" | 25 #include "ppapi/shared_impl/proxy_lock.h" |
| 27 #include "skia/ext/fontmgr_default_win.h" | 26 #include "skia/ext/fontmgr_default_win.h" |
| 28 #include "skia/ext/refptr.h" | 27 #include "skia/ext/refptr.h" |
| 29 #include "third_party/WebKit/public/web/win/WebFontRendering.h" | |
| 30 #include "third_party/skia/include/core/SkPaint.h" | |
| 31 #include "third_party/skia/include/ports/SkFontMgr.h" | 28 #include "third_party/skia/include/ports/SkFontMgr.h" |
| 32 #include "third_party/skia/include/ports/SkTypeface_win.h" | 29 #include "third_party/skia/include/ports/SkTypeface_win.h" |
| 33 | 30 |
| 34 namespace content { | 31 namespace content { |
| 35 | 32 |
| 36 namespace { | 33 namespace { |
| 37 | 34 |
| 38 // The Skia font manager, used for the life of the process (leaked at the end). | 35 // The Skia font manager, used for the life of the process (leaked at the end). |
| 39 SkFontMgr* g_warmup_fontmgr = nullptr; | 36 SkFontMgr* g_warmup_fontmgr = nullptr; |
| 40 | 37 |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 92 DWORD flags, | 89 DWORD flags, |
| 93 void* server_sid, | 90 void* server_sid, |
| 94 void* message, | 91 void* message, |
| 95 DWORD* buffer_length, | 92 DWORD* buffer_length, |
| 96 void* out_message_attributes, | 93 void* out_message_attributes, |
| 97 void* in_message_attributes, | 94 void* in_message_attributes, |
| 98 void* time_out) { | 95 void* time_out) { |
| 99 return STATUS_ACCESS_DENIED; | 96 return STATUS_ACCESS_DENIED; |
| 100 } | 97 } |
| 101 | 98 |
| 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 | |
| 132 // 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 |
| 133 // SkTypeFace to emulate the simple operation of a DC and Font. | 100 // SkTypeFace to emulate the simple operation of a DC and Font. |
| 134 class FakeGdiObject : public base::RefCountedThreadSafe<FakeGdiObject> { | 101 class FakeGdiObject : public base::RefCountedThreadSafe<FakeGdiObject> { |
| 135 public: | 102 public: |
| 136 FakeGdiObject(uint32_t magic, void* handle) | 103 FakeGdiObject(uint32_t magic, void* handle) |
| 137 : handle_(handle), magic_(magic) {} | 104 : handle_(handle), magic_(magic) {} |
| 138 | 105 |
| 139 void set_typeface(const skia::RefPtr<SkTypeface>& typeface) { | 106 void set_typeface(const skia::RefPtr<SkTypeface>& typeface) { |
| 140 typeface_ = typeface; | 107 typeface_ = typeface; |
| 141 } | 108 } |
| (...skipping 297 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 439 | 406 |
| 440 patched = g_iat_patch_start_service.Patch( | 407 patched = g_iat_patch_start_service.Patch( |
| 441 L"dwrite.dll", service_provider_dll, "StartServiceW", StartServiceWPatch); | 408 L"dwrite.dll", service_provider_dll, "StartServiceW", StartServiceWPatch); |
| 442 DCHECK(patched == 0); | 409 DCHECK(patched == 0); |
| 443 | 410 |
| 444 patched = g_iat_patch_nt_connect_port.Patch( | 411 patched = g_iat_patch_nt_connect_port.Patch( |
| 445 L"dwrite.dll", "ntdll.dll", "NtAlpcConnectPort", NtALpcConnectPortPatch); | 412 L"dwrite.dll", "ntdll.dll", "NtAlpcConnectPort", NtALpcConnectPortPatch); |
| 446 DCHECK(patched == 0); | 413 DCHECK(patched == 0); |
| 447 } | 414 } |
| 448 | 415 |
| 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 | |
| 468 GdiFontPatchData* PatchGdiFontEnumeration(const base::FilePath& path) { | 416 GdiFontPatchData* PatchGdiFontEnumeration(const base::FilePath& path) { |
| 469 if (ShouldUseDirectWriteFontProxyFieldTrial() && !g_warmup_fontmgr) | 417 if (!g_warmup_fontmgr) |
| 470 g_warmup_fontmgr = SkFontMgr_New_DirectWrite(); | 418 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. | |
| 473 DCHECK(g_warmup_fontmgr); | 419 DCHECK(g_warmup_fontmgr); |
| 474 return new GdiFontPatchDataImpl(path); | 420 return new GdiFontPatchDataImpl(path); |
| 475 } | 421 } |
| 476 | 422 |
| 477 size_t GetEmulatedGdiHandleCountForTesting() { | 423 size_t GetEmulatedGdiHandleCountForTesting() { |
| 478 return g_fake_gdi_object_factory.Get().GetObjectCount(); | 424 return g_fake_gdi_object_factory.Get().GetObjectCount(); |
| 479 } | 425 } |
| 480 | 426 |
| 481 void ResetEmulatedGdiHandlesForTesting() { | 427 void ResetEmulatedGdiHandlesForTesting() { |
| 482 g_fake_gdi_object_factory.Get().ResetObjectHandles(); | 428 g_fake_gdi_object_factory.Get().ResetObjectHandles(); |
| 483 } | 429 } |
| 484 | 430 |
| 485 void SetPreSandboxWarmupFontMgrForTesting(SkFontMgr* fontmgr) { | 431 void SetPreSandboxWarmupFontMgrForTesting(SkFontMgr* fontmgr) { |
| 486 g_warmup_fontmgr = fontmgr; | 432 g_warmup_fontmgr = fontmgr; |
| 487 } | 433 } |
| 488 | 434 |
| 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 | |
| 504 } // namespace content | 435 } // namespace content |
| OLD | NEW |