OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "ui/gfx/screen_compatible_dc_win.h" | 5 #include "ui/gfx/screen_compatible_dc_win.h" |
6 | 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/callback.h" |
7 #include "base/logging.h" | 9 #include "base/logging.h" |
8 #include "base/memory/singleton.h" | 10 #include "base/memory/singleton.h" |
9 #include "base/win/wrapped_window_proc.h" | 11 #include "base/win/wrapped_window_proc.h" |
10 #include "ui/base/win/hwnd_util.h" | 12 #include "ui/base/win/singleton_hwnd.h" |
11 | 13 |
12 namespace { | 14 namespace { |
13 | 15 |
14 // Windows class name to use for the listener window. | |
15 const wchar_t kWindowClassName[] = L"CachedCompatibleDC_Invalidator"; | |
16 | |
17 class CachedCompatibleDC { | 16 class CachedCompatibleDC { |
18 public: | 17 public: |
19 // Singleton getter. | 18 // Singleton getter. |
20 static CachedCompatibleDC* GetInstance(); | 19 static CachedCompatibleDC* GetInstance(); |
21 | 20 |
22 // Returns the cached screen compatible DC or creates one if needed. | 21 // Returns the cached screen compatible DC or creates one if needed. |
23 HDC GetOrCreateCompatibleDC(); | 22 HDC GetOrCreateCompatibleDC(); |
24 | 23 |
| 24 // Listener for WM_DISPLAYCHANGE notifications. |
| 25 void OnWndProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam); |
| 26 |
25 // Deletes the cached DC, called on WM_DISPLAYCHANGE notifications. | 27 // Deletes the cached DC, called on WM_DISPLAYCHANGE notifications. |
26 void DeleteCachedCompatibleDC(); | 28 void DeleteCachedCompatibleDC(); |
27 | 29 |
28 private: | 30 private: |
| 31 CachedCompatibleDC(); |
| 32 ~CachedCompatibleDC(); |
| 33 |
29 // Cached device context compatible with the screen. | 34 // Cached device context compatible with the screen. |
30 HDC compatible_dc_; | 35 HDC compatible_dc_; |
31 | 36 |
32 // HWND for listening to WM_DISPLAYCHANGE notifications. | 37 // Indicates whether the listener has been registered with the SingletonHwnd. |
33 HWND listener_window_; | 38 bool listener_registered_; |
34 | |
35 CachedCompatibleDC(); | |
36 ~CachedCompatibleDC(); | |
37 | 39 |
38 friend struct DefaultSingletonTraits<CachedCompatibleDC>; | 40 friend struct DefaultSingletonTraits<CachedCompatibleDC>; |
39 | 41 |
40 DISALLOW_COPY_AND_ASSIGN(CachedCompatibleDC); | 42 DISALLOW_COPY_AND_ASSIGN(CachedCompatibleDC); |
41 }; | 43 }; |
42 | 44 |
43 // Windows callback for listening for WM_DISPLAYCHANGE messages. | |
44 LRESULT CALLBACK ListenerWindowProc(HWND hwnd, | |
45 UINT message, | |
46 WPARAM wparam, | |
47 LPARAM lparam) { | |
48 if (message == WM_DISPLAYCHANGE) | |
49 CachedCompatibleDC::GetInstance()->DeleteCachedCompatibleDC(); | |
50 | |
51 return ::DefWindowProc(hwnd, message, wparam, lparam); | |
52 } | |
53 | |
54 // Creates a listener window to handle WM_DISPLAYCHANGE messages. | |
55 HWND CreateListenerWindow() { | |
56 HINSTANCE hinst = 0; | |
57 if (!::GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, | |
58 reinterpret_cast<char*>(&ListenerWindowProc), | |
59 &hinst)) { | |
60 NOTREACHED(); | |
61 } | |
62 | |
63 WNDCLASSEX wc = {0}; | |
64 wc.cbSize = sizeof(wc); | |
65 wc.lpfnWndProc = base::win::WrappedWindowProc<ListenerWindowProc>; | |
66 wc.hInstance = hinst; | |
67 wc.lpszClassName = kWindowClassName; | |
68 ATOM clazz = ::RegisterClassEx(&wc); | |
69 DCHECK(clazz); | |
70 | |
71 return ::CreateWindow(MAKEINTATOM(clazz), 0, 0, 0, 0, 0, 0, HWND_MESSAGE, 0, | |
72 hinst, 0); | |
73 } | |
74 | |
75 CachedCompatibleDC::CachedCompatibleDC() | 45 CachedCompatibleDC::CachedCompatibleDC() |
76 : compatible_dc_(NULL), | 46 : compatible_dc_(NULL), |
77 listener_window_(NULL) { | 47 listener_registered_(false) { |
78 } | 48 } |
79 | 49 |
80 CachedCompatibleDC::~CachedCompatibleDC() { | 50 CachedCompatibleDC::~CachedCompatibleDC() { |
81 DeleteCachedCompatibleDC(); | 51 DeleteCachedCompatibleDC(); |
82 if (listener_window_) { | |
83 ::DestroyWindow(listener_window_); | |
84 ::UnregisterClass(kWindowClassName, GetModuleHandle(NULL)); | |
85 } | |
86 } | 52 } |
87 | 53 |
88 // static | 54 // static |
89 CachedCompatibleDC* CachedCompatibleDC::GetInstance() { | 55 CachedCompatibleDC* CachedCompatibleDC::GetInstance() { |
90 return Singleton<CachedCompatibleDC>::get(); | 56 return Singleton<CachedCompatibleDC>::get(); |
91 } | 57 } |
92 | 58 |
93 HDC CachedCompatibleDC::GetOrCreateCompatibleDC() { | 59 HDC CachedCompatibleDC::GetOrCreateCompatibleDC() { |
94 if (!compatible_dc_) { | 60 if (!compatible_dc_) { |
95 compatible_dc_ = ::CreateCompatibleDC(NULL); | 61 compatible_dc_ = ::CreateCompatibleDC(NULL); |
96 if (!listener_window_) { | 62 if (!listener_registered_) { |
97 listener_window_ = CreateListenerWindow(); | 63 ui::WndProcCallback callback = |
98 ui::CheckWindowCreated(listener_window_); | 64 base::Bind(&CachedCompatibleDC::OnWndProc, base::Unretained(this)); |
| 65 ui::SingletonHwnd::GetInstance()->RegisterListener(callback); |
| 66 listener_registered_ = true; |
99 } | 67 } |
100 } | 68 } |
101 return compatible_dc_; | 69 return compatible_dc_; |
102 } | 70 } |
103 | 71 |
| 72 void CachedCompatibleDC::OnWndProc(HWND hwnd, |
| 73 UINT message, |
| 74 WPARAM wparam, |
| 75 LPARAM lparam) { |
| 76 if (message == WM_DISPLAYCHANGE) |
| 77 DeleteCachedCompatibleDC(); |
| 78 } |
| 79 |
104 void CachedCompatibleDC::DeleteCachedCompatibleDC() { | 80 void CachedCompatibleDC::DeleteCachedCompatibleDC() { |
105 if (compatible_dc_) { | 81 if (compatible_dc_) { |
106 ::DeleteDC(compatible_dc_); | 82 ::DeleteDC(compatible_dc_); |
107 compatible_dc_ = NULL; | 83 compatible_dc_ = NULL; |
108 } | 84 } |
109 } | 85 } |
110 | 86 |
111 } // namespace | 87 } // namespace |
112 | 88 |
113 namespace gfx { | 89 namespace gfx { |
114 | 90 |
115 ScopedTemporaryScreenCompatibleDC::ScopedTemporaryScreenCompatibleDC() | 91 ScopedTemporaryScreenCompatibleDC::ScopedTemporaryScreenCompatibleDC() |
116 : hdc_(CachedCompatibleDC::GetInstance()->GetOrCreateCompatibleDC()) { | 92 : hdc_(CachedCompatibleDC::GetInstance()->GetOrCreateCompatibleDC()) { |
117 } | 93 } |
118 | 94 |
119 ScopedTemporaryScreenCompatibleDC::~ScopedTemporaryScreenCompatibleDC() { | 95 ScopedTemporaryScreenCompatibleDC::~ScopedTemporaryScreenCompatibleDC() { |
120 } | 96 } |
121 | 97 |
122 } // namespace gfx | 98 } // namespace gfx |
OLD | NEW |