| Index: ui/gfx/screen_compatible_dc_win.cc
|
| ===================================================================
|
| --- ui/gfx/screen_compatible_dc_win.cc (revision 120986)
|
| +++ ui/gfx/screen_compatible_dc_win.cc (working copy)
|
| @@ -4,16 +4,15 @@
|
|
|
| #include "ui/gfx/screen_compatible_dc_win.h"
|
|
|
| +#include "base/bind.h"
|
| +#include "base/callback.h"
|
| #include "base/logging.h"
|
| #include "base/memory/singleton.h"
|
| #include "base/win/wrapped_window_proc.h"
|
| -#include "ui/base/win/hwnd_util.h"
|
| +#include "ui/base/win/singleton_hwnd.h"
|
|
|
| namespace {
|
|
|
| -// Windows class name to use for the listener window.
|
| -const wchar_t kWindowClassName[] = L"CachedCompatibleDC_Invalidator";
|
| -
|
| class CachedCompatibleDC {
|
| public:
|
| // Singleton getter.
|
| @@ -22,6 +21,9 @@
|
| // Returns the cached screen compatible DC or creates one if needed.
|
| HDC GetOrCreateCompatibleDC();
|
|
|
| + // Listener for WM_DISPLAYCHANGE notifications.
|
| + void OnWndProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam);
|
| +
|
| // Deletes the cached DC, called on WM_DISPLAYCHANGE notifications.
|
| void DeleteCachedCompatibleDC();
|
|
|
| @@ -29,8 +31,8 @@
|
| // Cached device context compatible with the screen.
|
| HDC compatible_dc_;
|
|
|
| - // HWND for listening to WM_DISPLAYCHANGE notifications.
|
| - HWND listener_window_;
|
| + // Indicates whether the listener has been registered with the SingletonHwnd.
|
| + bool listener_registered_;
|
|
|
| CachedCompatibleDC();
|
| ~CachedCompatibleDC();
|
| @@ -40,49 +42,13 @@
|
| DISALLOW_COPY_AND_ASSIGN(CachedCompatibleDC);
|
| };
|
|
|
| -// Windows callback for listening for WM_DISPLAYCHANGE messages.
|
| -LRESULT CALLBACK ListenerWindowProc(HWND hwnd,
|
| - UINT message,
|
| - WPARAM wparam,
|
| - LPARAM lparam) {
|
| - if (message == WM_DISPLAYCHANGE)
|
| - CachedCompatibleDC::GetInstance()->DeleteCachedCompatibleDC();
|
| -
|
| - return ::DefWindowProc(hwnd, message, wparam, lparam);
|
| -}
|
| -
|
| -// Creates a listener window to handle WM_DISPLAYCHANGE messages.
|
| -HWND CreateListenerWindow() {
|
| - HINSTANCE hinst = 0;
|
| - if (!::GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS,
|
| - reinterpret_cast<char*>(&ListenerWindowProc),
|
| - &hinst)) {
|
| - NOTREACHED();
|
| - }
|
| -
|
| - WNDCLASSEX wc = {0};
|
| - wc.cbSize = sizeof(wc);
|
| - wc.lpfnWndProc = base::win::WrappedWindowProc<ListenerWindowProc>;
|
| - wc.hInstance = hinst;
|
| - wc.lpszClassName = kWindowClassName;
|
| - ATOM clazz = ::RegisterClassEx(&wc);
|
| - DCHECK(clazz);
|
| -
|
| - return ::CreateWindow(MAKEINTATOM(clazz), 0, 0, 0, 0, 0, 0, HWND_MESSAGE, 0,
|
| - hinst, 0);
|
| -}
|
| -
|
| CachedCompatibleDC::CachedCompatibleDC()
|
| : compatible_dc_(NULL),
|
| - listener_window_(NULL) {
|
| + listener_registered_(false) {
|
| }
|
|
|
| CachedCompatibleDC::~CachedCompatibleDC() {
|
| DeleteCachedCompatibleDC();
|
| - if (listener_window_) {
|
| - ::DestroyWindow(listener_window_);
|
| - ::UnregisterClass(kWindowClassName, GetModuleHandle(NULL));
|
| - }
|
| }
|
|
|
| // static
|
| @@ -93,14 +59,24 @@
|
| HDC CachedCompatibleDC::GetOrCreateCompatibleDC() {
|
| if (!compatible_dc_) {
|
| compatible_dc_ = ::CreateCompatibleDC(NULL);
|
| - if (!listener_window_) {
|
| - listener_window_ = CreateListenerWindow();
|
| - ui::CheckWindowCreated(listener_window_);
|
| + if (!listener_registered_) {
|
| + ui::WndProcCallback callback =
|
| + base::Bind(&CachedCompatibleDC::OnWndProc, base::Unretained(this));
|
| + ui::SingletonHwnd::GetInstance()->RegisterListener(callback);
|
| + listener_registered_ = true;
|
| }
|
| }
|
| return compatible_dc_;
|
| }
|
|
|
| +void CachedCompatibleDC::OnWndProc(HWND hwnd,
|
| + UINT message,
|
| + WPARAM wparam,
|
| + LPARAM lparam) {
|
| + if (message == WM_DISPLAYCHANGE)
|
| + DeleteCachedCompatibleDC();
|
| +}
|
| +
|
| void CachedCompatibleDC::DeleteCachedCompatibleDC() {
|
| if (compatible_dc_) {
|
| ::DeleteDC(compatible_dc_);
|
|
|