[Cronet] Delay StartNetLog and StopNetLog until native request context is initialized
[chromium-blink-merge.git] / ui / gfx / font_render_params_win.cc
blobf7a014809e88c4b56b6c59d0035430860ef56ccb
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
3 // found in the LICENSE file.
5 #include "base/files/file_path.h"
6 #include "base/memory/scoped_ptr.h"
7 #include "base/memory/singleton.h"
8 #include "base/win/registry.h"
9 #include "ui/gfx/font_render_params.h"
10 #include "ui/gfx/win/direct_write.h"
11 #include "ui/gfx/win/singleton_hwnd.h"
13 namespace gfx {
15 namespace {
17 FontRenderParams::SubpixelRendering GetSubpixelRenderingGeometry() {
18 DISPLAY_DEVICE display_device = {sizeof(DISPLAY_DEVICE), 0};
19 for (int i = 0; EnumDisplayDevices(nullptr, i, &display_device, 0); ++i) {
20 // TODO(scottmg): We only support the primary device currently.
21 if (display_device.StateFlags & DISPLAY_DEVICE_PRIMARY_DEVICE) {
22 base::FilePath trimmed =
23 base::FilePath(display_device.DeviceName).BaseName();
24 base::win::RegKey key(
25 HKEY_LOCAL_MACHINE,
26 (L"SOFTWARE\\Microsoft\\Avalon.Graphics\\" + trimmed.value()).c_str(),
27 KEY_READ);
28 DWORD pixel_structure;
29 if (key.ReadValueDW(L"PixelStructure", &pixel_structure) ==
30 ERROR_SUCCESS) {
31 if (pixel_structure == 1)
32 return FontRenderParams::SUBPIXEL_RENDERING_RGB;
33 if (pixel_structure == 2)
34 return FontRenderParams::SUBPIXEL_RENDERING_BGR;
36 break;
40 // No explicit ClearType settings, default to RGB.
41 return FontRenderParams::SUBPIXEL_RENDERING_RGB;
44 // Caches font render params and updates them on system notifications.
45 class CachedFontRenderParams : public gfx::SingletonHwnd::Observer {
46 public:
47 static CachedFontRenderParams* GetInstance() {
48 return Singleton<CachedFontRenderParams>::get();
51 const FontRenderParams& GetParams() {
52 if (params_)
53 return *params_;
55 params_.reset(new FontRenderParams());
56 params_->antialiasing = false;
57 params_->subpixel_positioning = false;
58 params_->autohinter = false;
59 params_->use_bitmaps = false;
60 params_->hinting = FontRenderParams::HINTING_MEDIUM;
61 params_->subpixel_rendering = FontRenderParams::SUBPIXEL_RENDERING_NONE;
63 BOOL enabled = false;
64 if (SystemParametersInfo(SPI_GETFONTSMOOTHING, 0, &enabled, 0) && enabled) {
65 params_->antialiasing = true;
66 // GDI does not support subpixel positioning.
67 params_->subpixel_positioning = win::IsDirectWriteEnabled();
69 UINT type = 0;
70 if (SystemParametersInfo(SPI_GETFONTSMOOTHINGTYPE, 0, &type, 0) &&
71 type == FE_FONTSMOOTHINGCLEARTYPE) {
72 params_->subpixel_rendering = GetSubpixelRenderingGeometry();
75 gfx::SingletonHwnd::GetInstance()->AddObserver(this);
76 return *params_;
79 private:
80 friend struct DefaultSingletonTraits<CachedFontRenderParams>;
82 CachedFontRenderParams() {}
83 virtual ~CachedFontRenderParams() {
84 // Can't remove the SingletonHwnd observer here since SingletonHwnd may have
85 // been destroyed already (both singletons).
88 virtual void OnWndProc(HWND hwnd,
89 UINT message,
90 WPARAM wparam,
91 LPARAM lparam) override {
92 if (message == WM_SETTINGCHANGE) {
93 params_.reset();
94 gfx::SingletonHwnd::GetInstance()->RemoveObserver(this);
98 scoped_ptr<FontRenderParams> params_;
100 DISALLOW_COPY_AND_ASSIGN(CachedFontRenderParams);
103 } // namespace
105 FontRenderParams GetFontRenderParams(const FontRenderParamsQuery& query,
106 std::string* family_out) {
107 if (family_out)
108 NOTIMPLEMENTED();
109 // Customized font rendering settings are not supported, only defaults.
110 return CachedFontRenderParams::GetInstance()->GetParams();
113 } // namespace gfx