| 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/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 Loading... |
| 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 // Windows-only DirectWrite support. These warm up the DirectWrite paths | |
| 100 // before sandbox lock down to allow Skia access to the Font Manager service. | |
| 101 void CreateDirectWriteFactory(IDWriteFactory** factory) { | |
| 102 typedef decltype(DWriteCreateFactory)* DWriteCreateFactoryProc; | |
| 103 HMODULE dwrite_dll = LoadLibraryW(L"dwrite.dll"); | |
| 104 // TODO(scottmg): Temporary code to track crash in http://crbug.com/387867. | |
| 105 if (!dwrite_dll) { | |
| 106 DWORD load_library_get_last_error = GetLastError(); | |
| 107 base::debug::Alias(&dwrite_dll); | |
| 108 base::debug::Alias(&load_library_get_last_error); | |
| 109 CHECK(false); | |
| 110 } | |
| 111 | |
| 112 PatchServiceManagerCalls(); | |
| 113 | |
| 114 DWriteCreateFactoryProc dwrite_create_factory_proc = | |
| 115 reinterpret_cast<DWriteCreateFactoryProc>( | |
| 116 GetProcAddress(dwrite_dll, "DWriteCreateFactory")); | |
| 117 // TODO(scottmg): Temporary code to track crash in http://crbug.com/387867. | |
| 118 if (!dwrite_create_factory_proc) { | |
| 119 DWORD get_proc_address_get_last_error = GetLastError(); | |
| 120 base::debug::Alias(&dwrite_create_factory_proc); | |
| 121 base::debug::Alias(&get_proc_address_get_last_error); | |
| 122 CHECK(false); | |
| 123 } | |
| 124 CHECK(SUCCEEDED(dwrite_create_factory_proc( | |
| 125 DWRITE_FACTORY_TYPE_ISOLATED, __uuidof(IDWriteFactory), | |
| 126 reinterpret_cast<IUnknown**>(factory)))); | |
| 127 } | |
| 128 | |
| 129 HRESULT STDMETHODCALLTYPE StubFontCollection(IDWriteFactory* factory, | |
| 130 IDWriteFontCollection** col, | |
| 131 BOOL checkUpdates) { | |
| 132 // We always return pre-created font collection from here. | |
| 133 IDWriteFontCollection* custom_collection = GetCustomFontCollection(factory); | |
| 134 DCHECK(custom_collection != nullptr); | |
| 135 *col = custom_collection; | |
| 136 return S_OK; | |
| 137 } | |
| 138 | |
| 139 void PatchDWriteFactory(IDWriteFactory* factory) { | |
| 140 const unsigned int kGetSystemFontCollectionVTableIndex = 3; | |
| 141 | |
| 142 PROC* vtable = *reinterpret_cast<PROC**>(factory); | |
| 143 PROC* function_ptr = &vtable[kGetSystemFontCollectionVTableIndex]; | |
| 144 void* stub_function = &StubFontCollection; | |
| 145 base::win::ModifyCode(function_ptr, &stub_function, sizeof(PROC)); | |
| 146 } | |
| 147 | |
| 148 // 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 |
| 149 // SkTypeFace to emulate the simple operation of a DC and Font. | 100 // SkTypeFace to emulate the simple operation of a DC and Font. |
| 150 class FakeGdiObject : public base::RefCountedThreadSafe<FakeGdiObject> { | 101 class FakeGdiObject : public base::RefCountedThreadSafe<FakeGdiObject> { |
| 151 public: | 102 public: |
| 152 FakeGdiObject(uint32_t magic, void* handle) | 103 FakeGdiObject(uint32_t magic, void* handle) |
| 153 : handle_(handle), magic_(magic) {} | 104 : handle_(handle), magic_(magic) {} |
| 154 | 105 |
| 155 void set_typeface(const skia::RefPtr<SkTypeface>& typeface) { | 106 void set_typeface(const skia::RefPtr<SkTypeface>& typeface) { |
| 156 typeface_ = typeface; | 107 typeface_ = typeface; |
| 157 } | 108 } |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 246 CHECK(g_warmup_fontmgr); | 197 CHECK(g_warmup_fontmgr); |
| 247 int weight = log_font->lfWeight; | 198 int weight = log_font->lfWeight; |
| 248 if (weight == FW_DONTCARE) | 199 if (weight == FW_DONTCARE) |
| 249 weight = SkFontStyle::kNormal_Weight; | 200 weight = SkFontStyle::kNormal_Weight; |
| 250 | 201 |
| 251 SkFontStyle style(weight, log_font->lfWidth, | 202 SkFontStyle style(weight, log_font->lfWidth, |
| 252 log_font->lfItalic ? SkFontStyle::kItalic_Slant | 203 log_font->lfItalic ? SkFontStyle::kItalic_Slant |
| 253 : SkFontStyle::kUpright_Slant); | 204 : SkFontStyle::kUpright_Slant); |
| 254 | 205 |
| 255 std::string family_name = base::WideToUTF8(log_font->lfFaceName); | 206 std::string family_name = base::WideToUTF8(log_font->lfFaceName); |
| 207 ppapi::ProxyAutoLock lock; |
| 256 return skia::AdoptRef( | 208 return skia::AdoptRef( |
| 257 g_warmup_fontmgr->matchFamilyStyle(family_name.c_str(), style)); | 209 g_warmup_fontmgr->matchFamilyStyle(family_name.c_str(), style)); |
| 258 } | 210 } |
| 259 | 211 |
| 260 HDC WINAPI CreateCompatibleDCPatch(HDC dc_handle) { | 212 HDC WINAPI CreateCompatibleDCPatch(HDC dc_handle) { |
| 261 scoped_refptr<FakeGdiObject> ret = | 213 scoped_refptr<FakeGdiObject> ret = |
| 262 g_fake_gdi_object_factory.Get().Create(kFakeDCMagic); | 214 g_fake_gdi_object_factory.Get().Create(kFakeDCMagic); |
| 263 return static_cast<HDC>(ret->handle()); | 215 return static_cast<HDC>(ret->handle()); |
| 264 } | 216 } |
| 265 | 217 |
| (...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 454 | 406 |
| 455 patched = g_iat_patch_start_service.Patch( | 407 patched = g_iat_patch_start_service.Patch( |
| 456 L"dwrite.dll", service_provider_dll, "StartServiceW", StartServiceWPatch); | 408 L"dwrite.dll", service_provider_dll, "StartServiceW", StartServiceWPatch); |
| 457 DCHECK(patched == 0); | 409 DCHECK(patched == 0); |
| 458 | 410 |
| 459 patched = g_iat_patch_nt_connect_port.Patch( | 411 patched = g_iat_patch_nt_connect_port.Patch( |
| 460 L"dwrite.dll", "ntdll.dll", "NtAlpcConnectPort", NtALpcConnectPortPatch); | 412 L"dwrite.dll", "ntdll.dll", "NtAlpcConnectPort", NtALpcConnectPortPatch); |
| 461 DCHECK(patched == 0); | 413 DCHECK(patched == 0); |
| 462 } | 414 } |
| 463 | 415 |
| 464 void DoPreSandboxWarmupForTypeface(SkTypeface* typeface) { | |
| 465 SkPaint paint_warmup; | |
| 466 paint_warmup.setTypeface(typeface); | |
| 467 wchar_t glyph = L'S'; | |
| 468 paint_warmup.measureText(&glyph, 2); | |
| 469 } | |
| 470 | |
| 471 SkFontMgr* GetPreSandboxWarmupFontMgr() { | |
| 472 if (!g_warmup_fontmgr) { | |
| 473 IDWriteFactory* factory; | |
| 474 CreateDirectWriteFactory(&factory); | |
| 475 | |
| 476 GetCustomFontCollection(factory); | |
| 477 | |
| 478 PatchDWriteFactory(factory); | |
| 479 | |
| 480 blink::WebFontRendering::setDirectWriteFactory(factory); | |
| 481 g_warmup_fontmgr = SkFontMgr_New_DirectWrite(factory); | |
| 482 } | |
| 483 return g_warmup_fontmgr; | |
| 484 } | |
| 485 | |
| 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 |
| OLD | NEW |