Chromium Code Reviews| 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 "ppapi/shared_impl/proxy_lock.h" | 25 #include "ppapi/shared_impl/proxy_lock.h" |
| 26 #include "skia/ext/fontmgr_default_win.h" | 26 #include "skia/ext/fontmgr_default_win.h" |
| 27 #include "skia/ext/refptr.h" | 27 #include "third_party/skia/include/core/SkRefCnt.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 | 30 |
| 31 namespace content { | 31 namespace content { |
| 32 | 32 |
| 33 namespace { | 33 namespace { |
| 34 | 34 |
| 35 // 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). |
| 36 SkFontMgr* g_warmup_fontmgr = nullptr; | 36 SkFontMgr* g_warmup_fontmgr = nullptr; |
| 37 | 37 |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 96 return STATUS_ACCESS_DENIED; | 96 return STATUS_ACCESS_DENIED; |
| 97 } | 97 } |
| 98 | 98 |
| 99 // 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 |
| 100 // SkTypeFace to emulate the simple operation of a DC and Font. | 100 // SkTypeFace to emulate the simple operation of a DC and Font. |
| 101 class FakeGdiObject : public base::RefCountedThreadSafe<FakeGdiObject> { | 101 class FakeGdiObject : public base::RefCountedThreadSafe<FakeGdiObject> { |
| 102 public: | 102 public: |
| 103 FakeGdiObject(uint32_t magic, void* handle) | 103 FakeGdiObject(uint32_t magic, void* handle) |
| 104 : handle_(handle), magic_(magic) {} | 104 : handle_(handle), magic_(magic) {} |
| 105 | 105 |
| 106 void set_typeface(const skia::RefPtr<SkTypeface>& typeface) { | 106 void set_typeface(sk_sp<SkTypeface> typeface) { |
| 107 typeface_ = typeface; | 107 typeface_ = std::move(typeface); |
| 108 } | 108 } |
| 109 | 109 |
| 110 skia::RefPtr<SkTypeface> typeface() { return typeface_; } | 110 sk_sp<SkTypeface> typeface() { return typeface_; } |
| 111 void* handle() { return handle_; } | 111 void* handle() { return handle_; } |
| 112 uint32_t magic() { return magic_; } | 112 uint32_t magic() { return magic_; } |
| 113 | 113 |
| 114 private: | 114 private: |
| 115 friend class base::RefCountedThreadSafe<FakeGdiObject>; | 115 friend class base::RefCountedThreadSafe<FakeGdiObject>; |
| 116 ~FakeGdiObject() {} | 116 ~FakeGdiObject() {} |
| 117 | 117 |
| 118 void* handle_; | 118 void* handle_; |
| 119 uint32_t magic_; | 119 uint32_t magic_; |
| 120 skia::RefPtr<SkTypeface> typeface_; | 120 sk_sp<SkTypeface> typeface_; |
| 121 | 121 |
| 122 DISALLOW_COPY_AND_ASSIGN(FakeGdiObject); | 122 DISALLOW_COPY_AND_ASSIGN(FakeGdiObject); |
| 123 }; | 123 }; |
| 124 | 124 |
| 125 // This class acts as a factory for creating new fake GDI objects. It also maps | 125 // This class acts as a factory for creating new fake GDI objects. It also maps |
| 126 // the new instances of the FakeGdiObject class to an incrementing handle value | 126 // the new instances of the FakeGdiObject class to an incrementing handle value |
| 127 // which is passed to the caller of the emulated GDI function for later | 127 // which is passed to the caller of the emulated GDI function for later |
| 128 // reference. We can't be sure that this won't be used in a multi-threaded | 128 // reference. We can't be sure that this won't be used in a multi-threaded |
| 129 // environment so we need to ensure a lock is taken before accessing the map of | 129 // environment so we need to ensure a lock is taken before accessing the map of |
| 130 // issued objects. | 130 // issued objects. |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 186 DISALLOW_COPY_AND_ASSIGN(FakeGdiObjectFactory); | 186 DISALLOW_COPY_AND_ASSIGN(FakeGdiObjectFactory); |
| 187 }; | 187 }; |
| 188 | 188 |
| 189 base::LazyInstance<FakeGdiObjectFactory>::Leaky g_fake_gdi_object_factory = | 189 base::LazyInstance<FakeGdiObjectFactory>::Leaky g_fake_gdi_object_factory = |
| 190 LAZY_INSTANCE_INITIALIZER; | 190 LAZY_INSTANCE_INITIALIZER; |
| 191 | 191 |
| 192 // Magic values for the fake GDI objects. | 192 // Magic values for the fake GDI objects. |
| 193 const uint32_t kFakeDCMagic = 'fkdc'; | 193 const uint32_t kFakeDCMagic = 'fkdc'; |
| 194 const uint32_t kFakeFontMagic = 'fkft'; | 194 const uint32_t kFakeFontMagic = 'fkft'; |
| 195 | 195 |
| 196 skia::RefPtr<SkTypeface> GetTypefaceFromLOGFONT(const LOGFONTW* log_font) { | 196 sk_sp<SkTypeface> GetTypefaceFromLOGFONT(const LOGFONTW* log_font) { |
| 197 CHECK(g_warmup_fontmgr); | 197 CHECK(g_warmup_fontmgr); |
| 198 int weight = log_font->lfWeight; | 198 int weight = log_font->lfWeight; |
| 199 if (weight == FW_DONTCARE) | 199 if (weight == FW_DONTCARE) |
| 200 weight = SkFontStyle::kNormal_Weight; | 200 weight = SkFontStyle::kNormal_Weight; |
| 201 | 201 |
| 202 SkFontStyle style(weight, log_font->lfWidth, | 202 SkFontStyle style(weight, log_font->lfWidth, |
| 203 log_font->lfItalic ? SkFontStyle::kItalic_Slant | 203 log_font->lfItalic ? SkFontStyle::kItalic_Slant |
| 204 : SkFontStyle::kUpright_Slant); | 204 : SkFontStyle::kUpright_Slant); |
| 205 | 205 |
| 206 std::string family_name = base::WideToUTF8(log_font->lfFaceName); | 206 std::string family_name = base::WideToUTF8(log_font->lfFaceName); |
| 207 ppapi::ProxyAutoLock lock; // Needed for DirectWrite font proxy. | 207 ppapi::ProxyAutoLock lock; // Needed for DirectWrite font proxy. |
| 208 return skia::AdoptRef( | 208 return sk_sp<SkTypeface>( |
| 209 g_warmup_fontmgr->matchFamilyStyle(family_name.c_str(), style)); | 209 g_warmup_fontmgr->matchFamilyStyle(family_name.c_str(), style)); |
| 210 } | 210 } |
| 211 | 211 |
| 212 HDC WINAPI CreateCompatibleDCPatch(HDC dc_handle) { | 212 HDC WINAPI CreateCompatibleDCPatch(HDC dc_handle) { |
| 213 scoped_refptr<FakeGdiObject> ret = | 213 scoped_refptr<FakeGdiObject> ret = |
| 214 g_fake_gdi_object_factory.Get().Create(kFakeDCMagic); | 214 g_fake_gdi_object_factory.Get().Create(kFakeDCMagic); |
| 215 return static_cast<HDC>(ret->handle()); | 215 return static_cast<HDC>(ret->handle()); |
| 216 } | 216 } |
| 217 | 217 |
| 218 HFONT WINAPI CreateFontIndirectWPatch(const LOGFONTW* log_font) { | 218 HFONT WINAPI CreateFontIndirectWPatch(const LOGFONTW* log_font) { |
| 219 if (!log_font) | 219 if (!log_font) |
| 220 return nullptr; | 220 return nullptr; |
| 221 | 221 |
| 222 skia::RefPtr<SkTypeface> typeface = GetTypefaceFromLOGFONT(log_font); | 222 sk_sp<SkTypeface> typeface = GetTypefaceFromLOGFONT(log_font); |
| 223 if (!typeface) | 223 if (!typeface) |
| 224 return nullptr; | 224 return nullptr; |
| 225 | 225 |
| 226 scoped_refptr<FakeGdiObject> ret = | 226 scoped_refptr<FakeGdiObject> ret = |
| 227 g_fake_gdi_object_factory.Get().Create(kFakeFontMagic); | 227 g_fake_gdi_object_factory.Get().Create(kFakeFontMagic); |
| 228 ret->set_typeface(typeface); | 228 ret->set_typeface(typeface); |
|
f(malita)
2016/05/04 17:22:13
std::move(typeface)
tomhudson
2016/05/04 17:47:10
Done.
| |
| 229 | 229 |
| 230 return static_cast<HFONT>(ret->handle()); | 230 return static_cast<HFONT>(ret->handle()); |
| 231 } | 231 } |
| 232 | 232 |
| 233 BOOL WINAPI DeleteDCPatch(HDC dc_handle) { | 233 BOOL WINAPI DeleteDCPatch(HDC dc_handle) { |
| 234 return g_fake_gdi_object_factory.Get().DeleteObject(dc_handle, kFakeDCMagic); | 234 return g_fake_gdi_object_factory.Get().DeleteObject(dc_handle, kFakeDCMagic); |
| 235 } | 235 } |
| 236 | 236 |
| 237 BOOL WINAPI DeleteObjectPatch(HGDIOBJ object_handle) { | 237 BOOL WINAPI DeleteObjectPatch(HGDIOBJ object_handle) { |
| 238 return g_fake_gdi_object_factory.Get().DeleteObject(object_handle, | 238 return g_fake_gdi_object_factory.Get().DeleteObject(object_handle, |
| 239 kFakeFontMagic); | 239 kFakeFontMagic); |
| 240 } | 240 } |
| 241 | 241 |
| 242 int WINAPI EnumFontFamiliesExWPatch(HDC dc_handle, | 242 int WINAPI EnumFontFamiliesExWPatch(HDC dc_handle, |
| 243 LPLOGFONTW log_font, | 243 LPLOGFONTW log_font, |
| 244 FONTENUMPROCW enum_callback, | 244 FONTENUMPROCW enum_callback, |
| 245 LPARAM callback_param, | 245 LPARAM callback_param, |
| 246 DWORD flags) { | 246 DWORD flags) { |
| 247 scoped_refptr<FakeGdiObject> dc_obj = | 247 scoped_refptr<FakeGdiObject> dc_obj = |
| 248 g_fake_gdi_object_factory.Get().Validate(dc_handle, kFakeDCMagic); | 248 g_fake_gdi_object_factory.Get().Validate(dc_handle, kFakeDCMagic); |
| 249 if (!dc_obj) | 249 if (!dc_obj) |
| 250 return 1; | 250 return 1; |
| 251 | 251 |
| 252 if (!log_font || !enum_callback) | 252 if (!log_font || !enum_callback) |
| 253 return 1; | 253 return 1; |
| 254 | 254 |
| 255 skia::RefPtr<SkTypeface> typeface = GetTypefaceFromLOGFONT(log_font); | 255 sk_sp<SkTypeface> typeface = GetTypefaceFromLOGFONT(log_font); |
| 256 if (!typeface) | 256 if (!typeface) |
| 257 return 1; | 257 return 1; |
| 258 | 258 |
| 259 ENUMLOGFONTEXDVW enum_log_font = {}; | 259 ENUMLOGFONTEXDVW enum_log_font = {}; |
| 260 enum_log_font.elfEnumLogfontEx.elfLogFont = *log_font; | 260 enum_log_font.elfEnumLogfontEx.elfLogFont = *log_font; |
| 261 // TODO: Fill in the rest of the text metric structure. Not yet needed for | 261 // TODO: Fill in the rest of the text metric structure. Not yet needed for |
| 262 // Flash support but might be in the future. | 262 // Flash support but might be in the future. |
| 263 NEWTEXTMETRICEXW text_metric = {}; | 263 NEWTEXTMETRICEXW text_metric = {}; |
| 264 text_metric.ntmTm.ntmFlags = NTM_PS_OPENTYPE; | 264 text_metric.ntmTm.ntmFlags = NTM_PS_OPENTYPE; |
| 265 | 265 |
| 266 return enum_callback(&enum_log_font.elfEnumLogfontEx.elfLogFont, | 266 return enum_callback(&enum_log_font.elfEnumLogfontEx.elfLogFont, |
| 267 reinterpret_cast<TEXTMETRIC*>(&text_metric), | 267 reinterpret_cast<TEXTMETRIC*>(&text_metric), |
| 268 TRUETYPE_FONTTYPE, callback_param); | 268 TRUETYPE_FONTTYPE, callback_param); |
| 269 } | 269 } |
| 270 | 270 |
| 271 DWORD WINAPI GetFontDataPatch(HDC dc_handle, | 271 DWORD WINAPI GetFontDataPatch(HDC dc_handle, |
| 272 DWORD table_tag, | 272 DWORD table_tag, |
| 273 DWORD table_offset, | 273 DWORD table_offset, |
| 274 LPVOID buffer, | 274 LPVOID buffer, |
| 275 DWORD buffer_length) { | 275 DWORD buffer_length) { |
| 276 scoped_refptr<FakeGdiObject> dc_obj = | 276 scoped_refptr<FakeGdiObject> dc_obj = |
| 277 g_fake_gdi_object_factory.Get().Validate(dc_handle, kFakeDCMagic); | 277 g_fake_gdi_object_factory.Get().Validate(dc_handle, kFakeDCMagic); |
| 278 if (!dc_obj) | 278 if (!dc_obj) |
| 279 return GDI_ERROR; | 279 return GDI_ERROR; |
| 280 | 280 |
| 281 skia::RefPtr<SkTypeface> typeface = dc_obj->typeface(); | 281 sk_sp<SkTypeface> typeface = dc_obj->typeface(); |
| 282 if (!typeface) | 282 if (!typeface) |
| 283 return GDI_ERROR; | 283 return GDI_ERROR; |
| 284 | 284 |
| 285 // |getTableData| handles |buffer| being nullptr. However if it is nullptr | 285 // |getTableData| handles |buffer| being nullptr. However if it is nullptr |
| 286 // then set the size to INT32_MAX otherwise |getTableData| will return the | 286 // then set the size to INT32_MAX otherwise |getTableData| will return the |
| 287 // minimum value between the table entry size and the size passed in. The | 287 // minimum value between the table entry size and the size passed in. The |
| 288 // common Windows idiom is to pass 0 as |buffer_length| when passing nullptr, | 288 // common Windows idiom is to pass 0 as |buffer_length| when passing nullptr, |
| 289 // which would in this case result in |getTableData| returning 0 which isn't | 289 // which would in this case result in |getTableData| returning 0 which isn't |
| 290 // the correct answer for emulating GDI. |table_tag| must also have its | 290 // the correct answer for emulating GDI. |table_tag| must also have its |
| 291 // byte order swapped to counter the swap which occurs in the called method. | 291 // byte order swapped to counter the swap which occurs in the called method. |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 305 if (!dc_obj) | 305 if (!dc_obj) |
| 306 return nullptr; | 306 return nullptr; |
| 307 | 307 |
| 308 scoped_refptr<FakeGdiObject> font_obj = | 308 scoped_refptr<FakeGdiObject> font_obj = |
| 309 g_fake_gdi_object_factory.Get().Validate(object_handle, kFakeFontMagic); | 309 g_fake_gdi_object_factory.Get().Validate(object_handle, kFakeFontMagic); |
| 310 if (!font_obj) | 310 if (!font_obj) |
| 311 return nullptr; | 311 return nullptr; |
| 312 | 312 |
| 313 // Construct a new fake font object to handle the old font if there's one. | 313 // Construct a new fake font object to handle the old font if there's one. |
| 314 scoped_refptr<FakeGdiObject> new_font_obj; | 314 scoped_refptr<FakeGdiObject> new_font_obj; |
| 315 skia::RefPtr<SkTypeface> old_typeface = dc_obj->typeface(); | 315 sk_sp<SkTypeface> old_typeface = dc_obj->typeface(); |
| 316 if (old_typeface) { | 316 if (old_typeface) { |
| 317 new_font_obj = g_fake_gdi_object_factory.Get().Create(kFakeFontMagic); | 317 new_font_obj = g_fake_gdi_object_factory.Get().Create(kFakeFontMagic); |
| 318 new_font_obj->set_typeface(old_typeface); | 318 new_font_obj->set_typeface(old_typeface); |
|
f(malita)
2016/05/04 17:22:13
std::move(old_typeface)
tomhudson
2016/05/04 17:47:10
Done.
| |
| 319 } | 319 } |
| 320 dc_obj->set_typeface(font_obj->typeface()); | 320 dc_obj->set_typeface(font_obj->typeface()); |
| 321 | 321 |
| 322 if (new_font_obj) | 322 if (new_font_obj) |
| 323 return static_cast<HGDIOBJ>(new_font_obj->handle()); | 323 return static_cast<HGDIOBJ>(new_font_obj->handle()); |
| 324 return nullptr; | 324 return nullptr; |
| 325 } | 325 } |
| 326 | 326 |
| 327 void DoSingleGdiPatch(base::win::IATPatchFunction& patch, | 327 void DoSingleGdiPatch(base::win::IATPatchFunction& patch, |
| 328 const base::FilePath& path, | 328 const base::FilePath& path, |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 426 | 426 |
| 427 void ResetEmulatedGdiHandlesForTesting() { | 427 void ResetEmulatedGdiHandlesForTesting() { |
| 428 g_fake_gdi_object_factory.Get().ResetObjectHandles(); | 428 g_fake_gdi_object_factory.Get().ResetObjectHandles(); |
| 429 } | 429 } |
| 430 | 430 |
| 431 void SetPreSandboxWarmupFontMgrForTesting(SkFontMgr* fontmgr) { | 431 void SetPreSandboxWarmupFontMgrForTesting(SkFontMgr* fontmgr) { |
| 432 g_warmup_fontmgr = fontmgr; | 432 g_warmup_fontmgr = fontmgr; |
| 433 } | 433 } |
| 434 | 434 |
| 435 } // namespace content | 435 } // namespace content |
| OLD | NEW |