| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/dwrite_font_proxy/dwrite_font_proxy_init_win.h" | 5 #include "content/child/dwrite_font_proxy/dwrite_font_proxy_init_win.h" |
| 6 | 6 |
| 7 #include <dwrite.h> | 7 #include <dwrite.h> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/callback.h" | 10 #include "base/callback.h" |
| 11 #include "base/debug/alias.h" | 11 #include "base/debug/alias.h" |
| 12 #include "base/win/iat_patch_function.h" | 12 #include "base/win/iat_patch_function.h" |
| 13 #include "base/win/windows_version.h" | 13 #include "base/win/windows_version.h" |
| 14 #include "content/child/dwrite_font_proxy/dwrite_font_proxy_win.h" | 14 #include "content/child/dwrite_font_proxy/dwrite_font_proxy_win.h" |
| 15 #include "content/child/font_warmup_win.h" | 15 #include "content/child/font_warmup_win.h" |
| 16 #include "skia/ext/fontmgr_default_win.h" | 16 #include "skia/ext/fontmgr_default_win.h" |
| 17 #include "skia/ext/refptr.h" | |
| 18 #include "third_party/WebKit/public/web/win/WebFontRendering.h" | 17 #include "third_party/WebKit/public/web/win/WebFontRendering.h" |
| 19 #include "third_party/skia/include/ports/SkFontMgr.h" | |
| 20 #include "third_party/skia/include/ports/SkTypeface_win.h" | 18 #include "third_party/skia/include/ports/SkTypeface_win.h" |
| 21 | 19 |
| 22 namespace mswr = Microsoft::WRL; | 20 namespace mswr = Microsoft::WRL; |
| 23 | 21 |
| 24 namespace content { | 22 namespace content { |
| 25 | 23 |
| 26 namespace { | 24 namespace { |
| 27 | 25 |
| 28 mswr::ComPtr<DWriteFontCollectionProxy> g_font_collection; | 26 mswr::ComPtr<DWriteFontCollectionProxy> g_font_collection; |
| 29 IPC::Sender* g_sender_override = nullptr; | 27 IPC::Sender* g_sender_override = nullptr; |
| (...skipping 23 matching lines...) Expand all Loading... |
| 53 DWORD get_proc_address_get_last_error = GetLastError(); | 51 DWORD get_proc_address_get_last_error = GetLastError(); |
| 54 base::debug::Alias(&dwrite_create_factory_proc); | 52 base::debug::Alias(&dwrite_create_factory_proc); |
| 55 base::debug::Alias(&get_proc_address_get_last_error); | 53 base::debug::Alias(&get_proc_address_get_last_error); |
| 56 CHECK(false); | 54 CHECK(false); |
| 57 } | 55 } |
| 58 CHECK(SUCCEEDED(dwrite_create_factory_proc( | 56 CHECK(SUCCEEDED(dwrite_create_factory_proc( |
| 59 DWRITE_FACTORY_TYPE_ISOLATED, __uuidof(IDWriteFactory), | 57 DWRITE_FACTORY_TYPE_ISOLATED, __uuidof(IDWriteFactory), |
| 60 reinterpret_cast<IUnknown**>(factory)))); | 58 reinterpret_cast<IUnknown**>(factory)))); |
| 61 } | 59 } |
| 62 | 60 |
| 61 HRESULT STDMETHODCALLTYPE StubFontCollection(IDWriteFactory* factory, |
| 62 IDWriteFontCollection** col, |
| 63 BOOL checkUpdates) { |
| 64 DCHECK(g_font_collection); |
| 65 g_font_collection.CopyTo(col); |
| 66 return S_OK; |
| 67 } |
| 68 |
| 69 // Copied from content/common/font_warmup_win.cc |
| 70 void PatchDWriteFactory(IDWriteFactory* factory) { |
| 71 const unsigned int kGetSystemFontCollectionVTableIndex = 3; |
| 72 |
| 73 PROC* vtable = *reinterpret_cast<PROC**>(factory); |
| 74 PROC* function_ptr = &vtable[kGetSystemFontCollectionVTableIndex]; |
| 75 void* stub_function = &StubFontCollection; |
| 76 base::win::ModifyCode(function_ptr, &stub_function, sizeof(PROC)); |
| 77 } |
| 78 |
| 63 // Needed as a function for Bind() | 79 // Needed as a function for Bind() |
| 64 IPC::Sender* GetSenderOverride() { | 80 IPC::Sender* GetSenderOverride() { |
| 65 return g_sender_override; | 81 return g_sender_override; |
| 66 } | 82 } |
| 67 | 83 |
| 68 } // namespace | 84 } // namespace |
| 69 | 85 |
| 70 void InitializeDWriteFontProxy( | 86 void InitializeDWriteFontProxy( |
| 71 const base::Callback<IPC::Sender*(void)>& sender) { | 87 const base::Callback<IPC::Sender*(void)>& sender) { |
| 72 mswr::ComPtr<IDWriteFactory> factory; | 88 mswr::ComPtr<IDWriteFactory> factory; |
| 73 | 89 |
| 74 CreateDirectWriteFactory(&factory); | 90 CreateDirectWriteFactory(&factory); |
| 75 | 91 |
| 76 if (!g_font_collection) { | 92 if (!g_font_collection) { |
| 77 if (g_sender_override) { | 93 if (g_sender_override) { |
| 78 mswr::MakeAndInitialize<DWriteFontCollectionProxy>( | 94 mswr::MakeAndInitialize<DWriteFontCollectionProxy>( |
| 79 &g_font_collection, factory.Get(), base::Bind(&GetSenderOverride)); | 95 &g_font_collection, factory.Get(), base::Bind(&GetSenderOverride)); |
| 80 } else { | 96 } else { |
| 81 mswr::MakeAndInitialize<DWriteFontCollectionProxy>(&g_font_collection, | 97 mswr::MakeAndInitialize<DWriteFontCollectionProxy>(&g_font_collection, |
| 82 factory.Get(), sender); | 98 factory.Get(), sender); |
| 83 } | 99 } |
| 84 } | 100 } |
| 85 | 101 |
| 86 skia::RefPtr<SkFontMgr> skia_font_manager = skia::AdoptRef( | 102 PatchDWriteFactory(factory.Get()); |
| 87 SkFontMgr_New_DirectWrite(factory.Get(), g_font_collection.Get())); | |
| 88 blink::WebFontRendering::setSkiaFontManager(skia_font_manager.get()); | |
| 89 | 103 |
| 90 // Add an extra ref for SetDefaultSkiaFactory, which keeps a ref but doesn't | 104 blink::WebFontRendering::setDirectWriteFactory(factory.Get()); |
| 91 // addref. | 105 SkFontMgr* skia_font_manager = SkFontMgr_New_DirectWrite(factory.Get()); |
| 92 skia_font_manager->ref(); | 106 SetDefaultSkiaFactory(skia_font_manager); |
| 93 SetDefaultSkiaFactory(skia_font_manager.get()); | |
| 94 } | 107 } |
| 95 | 108 |
| 96 void UninitializeDWriteFontProxy() { | 109 void UninitializeDWriteFontProxy() { |
| 97 if (g_font_collection) | 110 if (g_font_collection) |
| 98 g_font_collection->Unregister(); | 111 g_font_collection->Unregister(); |
| 99 } | 112 } |
| 100 | 113 |
| 101 void SetDWriteFontProxySenderForTesting(IPC::Sender* sender) { | 114 void SetDWriteFontProxySenderForTesting(IPC::Sender* sender) { |
| 102 g_sender_override = sender; | 115 g_sender_override = sender; |
| 103 } | 116 } |
| 104 | 117 |
| 105 } // namespace content | 118 } // namespace content |
| OLD | NEW |