OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2014 Google Inc. | 2 * Copyright 2014 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkDWrite.h" | 8 #include "SkDWrite.h" |
9 #include "SkHRESULT.h" | 9 #include "SkHRESULT.h" |
10 #include "SkOnce.h" | 10 #include "SkOnce.h" |
11 #include "SkString.h" | 11 #include "SkString.h" |
12 | 12 |
13 #include <dwrite.h> | 13 #include <dwrite.h> |
14 | 14 |
15 static IDWriteFactory* gDWriteFactory = NULL; | 15 static IDWriteFactory* gDWriteFactory = NULL; |
16 | 16 |
| 17 static void release_dwrite_factory() { |
| 18 if (gDWriteFactory) { |
| 19 gDWriteFactory->Release(); |
| 20 } |
| 21 } |
| 22 |
17 static void create_dwrite_factory(IDWriteFactory** factory) { | 23 static void create_dwrite_factory(IDWriteFactory** factory) { |
18 typedef decltype(DWriteCreateFactory)* DWriteCreateFactoryProc; | 24 typedef decltype(DWriteCreateFactory)* DWriteCreateFactoryProc; |
19 DWriteCreateFactoryProc dWriteCreateFactoryProc = reinterpret_cast<DWriteCre
ateFactoryProc>( | 25 DWriteCreateFactoryProc dWriteCreateFactoryProc = reinterpret_cast<DWriteCre
ateFactoryProc>( |
20 GetProcAddress(LoadLibraryW(L"dwrite.dll"), "DWriteCreateFactory")); | 26 GetProcAddress(LoadLibraryW(L"dwrite.dll"), "DWriteCreateFactory")); |
21 | 27 |
22 if (!dWriteCreateFactoryProc) { | 28 if (!dWriteCreateFactoryProc) { |
23 HRESULT hr = HRESULT_FROM_WIN32(GetLastError()); | 29 HRESULT hr = HRESULT_FROM_WIN32(GetLastError()); |
24 if (!IS_ERROR(hr)) { | 30 if (!IS_ERROR(hr)) { |
25 hr = ERROR_PROC_NOT_FOUND; | 31 hr = ERROR_PROC_NOT_FOUND; |
26 } | 32 } |
27 HRVM(hr, "Could not get DWriteCreateFactory proc."); | 33 HRVM(hr, "Could not get DWriteCreateFactory proc."); |
28 } | 34 } |
29 | 35 |
30 HRVM(dWriteCreateFactoryProc(DWRITE_FACTORY_TYPE_SHARED, | 36 HRVM(dWriteCreateFactoryProc(DWRITE_FACTORY_TYPE_SHARED, |
31 __uuidof(IDWriteFactory), | 37 __uuidof(IDWriteFactory), |
32 reinterpret_cast<IUnknown**>(factory)), | 38 reinterpret_cast<IUnknown**>(factory)), |
33 "Could not create DirectWrite factory."); | 39 "Could not create DirectWrite factory."); |
| 40 atexit(release_dwrite_factory); |
34 } | 41 } |
35 | 42 |
36 static void release_dwrite_factory() { | |
37 if (gDWriteFactory) { | |
38 gDWriteFactory->Release(); | |
39 } | |
40 } | |
41 | 43 |
42 IDWriteFactory* sk_get_dwrite_factory() { | 44 IDWriteFactory* sk_get_dwrite_factory() { |
43 SK_DECLARE_STATIC_ONCE(once); | 45 SK_DECLARE_STATIC_ONCE(once); |
44 SkOnce(&once, create_dwrite_factory, &gDWriteFactory, release_dwrite_factory
); | 46 SkOnce(&once, create_dwrite_factory, &gDWriteFactory); |
45 | 47 |
46 return gDWriteFactory; | 48 return gDWriteFactory; |
47 } | 49 } |
48 | 50 |
49 //////////////////////////////////////////////////////////////////////////////// | 51 //////////////////////////////////////////////////////////////////////////////// |
50 // String conversion | 52 // String conversion |
51 | 53 |
52 /** Converts a utf8 string to a WCHAR string. */ | 54 /** Converts a utf8 string to a WCHAR string. */ |
53 HRESULT sk_cstring_to_wchar(const char* skname, SkSMallocWCHAR* name) { | 55 HRESULT sk_cstring_to_wchar(const char* skname, SkSMallocWCHAR* name) { |
54 int wlen = MultiByteToWideChar(CP_UTF8, 0, skname, -1, NULL, 0); | 56 int wlen = MultiByteToWideChar(CP_UTF8, 0, skname, -1, NULL, 0); |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
119 ); | 121 ); |
120 if (!*proc) { | 122 if (!*proc) { |
121 HRESULT hr = HRESULT_FROM_WIN32(GetLastError()); | 123 HRESULT hr = HRESULT_FROM_WIN32(GetLastError()); |
122 if (!IS_ERROR(hr)) { | 124 if (!IS_ERROR(hr)) { |
123 hr = ERROR_PROC_NOT_FOUND; | 125 hr = ERROR_PROC_NOT_FOUND; |
124 } | 126 } |
125 return hr; | 127 return hr; |
126 } | 128 } |
127 return S_OK; | 129 return S_OK; |
128 } | 130 } |
OLD | NEW |