Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(88)

Side by Side Diff: ui/gfx/font_render_params_win.cc

Issue 1092183005: Fix Up SingletonHwnd Observer Lifetime Issues (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Convert to scoped_ptr Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 "base/bind.h"
6 #include "base/bind_helpers.h"
5 #include "base/files/file_path.h" 7 #include "base/files/file_path.h"
6 #include "base/memory/scoped_ptr.h" 8 #include "base/memory/scoped_ptr.h"
7 #include "base/memory/singleton.h" 9 #include "base/memory/singleton.h"
8 #include "base/win/registry.h" 10 #include "base/win/registry.h"
9 #include "ui/gfx/font_render_params.h" 11 #include "ui/gfx/font_render_params.h"
10 #include "ui/gfx/win/direct_write.h" 12 #include "ui/gfx/win/direct_write.h"
11 #include "ui/gfx/win/singleton_hwnd.h" 13 #include "ui/gfx/win/singleton_hwnd_observer.h"
12 14
13 namespace gfx { 15 namespace gfx {
14 16
15 namespace { 17 namespace {
16 18
17 FontRenderParams::SubpixelRendering GetSubpixelRenderingGeometry() { 19 FontRenderParams::SubpixelRendering GetSubpixelRenderingGeometry() {
18 DISPLAY_DEVICE display_device = {sizeof(DISPLAY_DEVICE), 0}; 20 DISPLAY_DEVICE display_device = {sizeof(DISPLAY_DEVICE), 0};
19 for (int i = 0; EnumDisplayDevices(nullptr, i, &display_device, 0); ++i) { 21 for (int i = 0; EnumDisplayDevices(nullptr, i, &display_device, 0); ++i) {
20 // TODO(scottmg): We only support the primary device currently. 22 // TODO(scottmg): We only support the primary device currently.
21 if (display_device.StateFlags & DISPLAY_DEVICE_PRIMARY_DEVICE) { 23 if (display_device.StateFlags & DISPLAY_DEVICE_PRIMARY_DEVICE) {
(...skipping 13 matching lines...) Expand all
35 } 37 }
36 break; 38 break;
37 } 39 }
38 } 40 }
39 41
40 // No explicit ClearType settings, default to RGB. 42 // No explicit ClearType settings, default to RGB.
41 return FontRenderParams::SUBPIXEL_RENDERING_RGB; 43 return FontRenderParams::SUBPIXEL_RENDERING_RGB;
42 } 44 }
43 45
44 // Caches font render params and updates them on system notifications. 46 // Caches font render params and updates them on system notifications.
45 class CachedFontRenderParams : public gfx::SingletonHwnd::Observer { 47 class CachedFontRenderParams {
46 public: 48 public:
47 static CachedFontRenderParams* GetInstance() { 49 static CachedFontRenderParams* GetInstance() {
48 return Singleton<CachedFontRenderParams>::get(); 50 return Singleton<CachedFontRenderParams>::get();
49 } 51 }
50 52
51 const FontRenderParams& GetParams() { 53 const FontRenderParams& GetParams() {
52 if (params_) 54 if (params_)
53 return *params_; 55 return *params_;
54 56
55 params_.reset(new FontRenderParams()); 57 params_.reset(new FontRenderParams());
56 params_->antialiasing = false; 58 params_->antialiasing = false;
57 params_->subpixel_positioning = false; 59 params_->subpixel_positioning = false;
58 params_->autohinter = false; 60 params_->autohinter = false;
59 params_->use_bitmaps = false; 61 params_->use_bitmaps = false;
60 params_->hinting = FontRenderParams::HINTING_MEDIUM; 62 params_->hinting = FontRenderParams::HINTING_MEDIUM;
61 params_->subpixel_rendering = FontRenderParams::SUBPIXEL_RENDERING_NONE; 63 params_->subpixel_rendering = FontRenderParams::SUBPIXEL_RENDERING_NONE;
62 64
63 BOOL enabled = false; 65 BOOL enabled = false;
64 if (SystemParametersInfo(SPI_GETFONTSMOOTHING, 0, &enabled, 0) && enabled) { 66 if (SystemParametersInfo(SPI_GETFONTSMOOTHING, 0, &enabled, 0) && enabled) {
65 params_->antialiasing = true; 67 params_->antialiasing = true;
66 // GDI does not support subpixel positioning. 68 // GDI does not support subpixel positioning.
67 params_->subpixel_positioning = win::IsDirectWriteEnabled(); 69 params_->subpixel_positioning = win::IsDirectWriteEnabled();
68 70
69 UINT type = 0; 71 UINT type = 0;
70 if (SystemParametersInfo(SPI_GETFONTSMOOTHINGTYPE, 0, &type, 0) && 72 if (SystemParametersInfo(SPI_GETFONTSMOOTHINGTYPE, 0, &type, 0) &&
71 type == FE_FONTSMOOTHINGCLEARTYPE) { 73 type == FE_FONTSMOOTHINGCLEARTYPE) {
72 params_->subpixel_rendering = GetSubpixelRenderingGeometry(); 74 params_->subpixel_rendering = GetSubpixelRenderingGeometry();
73 } 75 }
74 } 76 }
75 gfx::SingletonHwnd::GetInstance()->AddObserver(this); 77 singleton_hwnd_observer_.reset(new SingletonHwndObserver(
78 base::Bind(&CachedFontRenderParams::OnSettingsChange,
79 base::Unretained(this))));
76 return *params_; 80 return *params_;
77 } 81 }
78 82
79 private: 83 private:
80 friend struct DefaultSingletonTraits<CachedFontRenderParams>; 84 friend struct DefaultSingletonTraits<CachedFontRenderParams>;
81 85
82 CachedFontRenderParams() {} 86 CachedFontRenderParams() {}
83 virtual ~CachedFontRenderParams() { 87 virtual ~CachedFontRenderParams() {
sky 2015/04/28 13:20:34 This no longer needs to be virtual.
robliao 2015/04/28 14:11:55 Done.
84 // Can't remove the SingletonHwnd observer here since SingletonHwnd may have 88 // Can't remove the SingletonHwnd observer here since SingletonHwnd may have
sky 2015/04/28 13:20:34 And this comment doesn't apply anymore.
robliao 2015/04/28 14:11:55 Done.
85 // been destroyed already (both singletons). 89 // been destroyed already (both singletons).
86 } 90 }
87 91
88 void OnWndProc(HWND hwnd, 92 void OnSettingsChange(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam) {
89 UINT message,
90 WPARAM wparam,
91 LPARAM lparam) override {
92 if (message == WM_SETTINGCHANGE) { 93 if (message == WM_SETTINGCHANGE) {
93 params_.reset(); 94 params_.reset();
94 gfx::SingletonHwnd::GetInstance()->RemoveObserver(this); 95 singleton_hwnd_observer_.reset(nullptr);
95 } 96 }
96 } 97 }
97 98
98 scoped_ptr<FontRenderParams> params_; 99 scoped_ptr<FontRenderParams> params_;
100 scoped_ptr<SingletonHwndObserver> singleton_hwnd_observer_;
99 101
100 DISALLOW_COPY_AND_ASSIGN(CachedFontRenderParams); 102 DISALLOW_COPY_AND_ASSIGN(CachedFontRenderParams);
101 }; 103 };
102 104
103 } // namespace 105 } // namespace
104 106
105 FontRenderParams GetFontRenderParams(const FontRenderParamsQuery& query, 107 FontRenderParams GetFontRenderParams(const FontRenderParamsQuery& query,
106 std::string* family_out) { 108 std::string* family_out) {
107 if (family_out) 109 if (family_out)
108 NOTIMPLEMENTED(); 110 NOTIMPLEMENTED();
109 // Customized font rendering settings are not supported, only defaults. 111 // Customized font rendering settings are not supported, only defaults.
110 return CachedFontRenderParams::GetInstance()->GetParams(); 112 return CachedFontRenderParams::GetInstance()->GetParams();
111 } 113 }
112 114
113 } // namespace gfx 115 } // namespace gfx
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698