8d522c070aff2b635a7c6a8364c249ff9acb4709
[platform/framework/web/chromium-efl.git] / tizen_src / chromium_impl / ui / display / device_display_info_efl.cc
1 // Copyright 2015 Samsung Electronics. 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.
4
5 #include "ui/display/device_display_info_efl.h"
6
7 #include "base/logging.h"
8 #include "tizen/system_info.h"
9
10 namespace display {
11
12 namespace {
13
14 const double kBaselineDPIDensity = 160.0;
15 const int kInvalidRotationDegrees = -1;
16
17 int GetDensityRange(int dpi) {
18   if (IsMobileProfile() || IsWearableProfile()) {
19     // Copied from Android platform and extended to support UHD displays,
20     // (http://developer.android.com/reference/android/util/DisplayMetrics.html).
21     const int density_range[] = {
22       0,    // NODPI, ANYDPI
23       120,  // LOW
24       160,  // MEDIUM
25       240,  // HIGH
26       280,  // DPI_280
27       320,  // XHIGH
28       400,  // DPI_400
29       480,  // XXHIGH
30       560,  // DPI_560
31       640,  // XXXHIGH
32       720,  // DPI_720
33       800,  // ULTRAHIGH
34       880,  // DPI_880
35       960,  // ULTRAXHIGH
36       1040, // DPI_1040
37       1120, // ULTRAXXHIGH
38       1200, // DPI_1200
39       1280  // ULTRAXXXHIGH
40     };
41
42     const int range_size = sizeof(density_range) / sizeof(*density_range);
43     int upper_bound = density_range[range_size - 1];
44     for (int i = range_size - 2; i >= 0 && dpi <= density_range[i]; --i) {
45       upper_bound = density_range[i];
46     }
47
48     return upper_bound;
49   } else {
50     // For platforms other than mobile, the user agents expect 1.0 as DIP scale
51     // for the contents to be delivered.
52     return 1.0;
53   }
54 }
55
56 }  // namespace
57
58 // DeviceDisplayInfoEflImpl
59 class DISPLAY_EXPORT DeviceDisplayInfoEflImpl {
60  public:
61   static DeviceDisplayInfoEflImpl* GetInstance();
62
63   void Update(int display_width, int display_height, double dip_scale,
64               int rotation_degrees);
65   void SetRotationDegrees(int rotation_degrees);
66   int GetDisplayWidth() const;
67   int GetDisplayHeight() const;
68   double GetDIPScale() const;
69   int GetRotationDegrees() const;
70
71  private:
72   DeviceDisplayInfoEflImpl();
73
74   friend struct base::DefaultSingletonTraits<DeviceDisplayInfoEflImpl>;
75
76   mutable base::Lock display_info_accessor_;
77   mutable base::Lock rotation_accessor_;
78
79   int display_width_;
80   int display_height_;
81   double dip_scale_;
82   int rotation_degrees_;
83
84   DISALLOW_COPY_AND_ASSIGN(DeviceDisplayInfoEflImpl);
85 };
86
87 // static
88 DeviceDisplayInfoEflImpl* DeviceDisplayInfoEflImpl::GetInstance() {
89   return base::Singleton<DeviceDisplayInfoEflImpl>::get();
90 }
91
92 void DeviceDisplayInfoEflImpl::Update(int display_width, int display_height,
93                                       double dip_scale, int rotation_degrees) {
94   {
95     base::AutoLock autolock(display_info_accessor_);
96     display_width_ = display_width;
97     display_height_ = display_height;
98     dip_scale_ = dip_scale;
99
100     DCHECK_NE(0, display_width_);
101     DCHECK_NE(0, display_height_);
102     DCHECK_NE(0, dip_scale_);
103   }
104
105   {
106     base::AutoLock autolock(rotation_accessor_);
107     rotation_degrees_ = rotation_degrees;
108     DCHECK_NE(kInvalidRotationDegrees, rotation_degrees_);
109   }
110 }
111
112 void DeviceDisplayInfoEflImpl::SetRotationDegrees(int rotation_degrees) {
113   base::AutoLock autolock(rotation_accessor_);
114   rotation_degrees_ = rotation_degrees;
115 }
116
117 int DeviceDisplayInfoEflImpl::GetDisplayWidth()  const {
118   base::AutoLock autolock(display_info_accessor_);
119   DCHECK_NE(0, display_width_);
120   return display_width_;
121 }
122
123 int DeviceDisplayInfoEflImpl::GetDisplayHeight() const {
124   base::AutoLock autolock(display_info_accessor_);
125   DCHECK_NE(0, display_height_);
126   return display_height_;
127 }
128
129 double DeviceDisplayInfoEflImpl::GetDIPScale() const {
130   base::AutoLock autolock(display_info_accessor_);
131   DCHECK_NE(0, dip_scale_);
132   return dip_scale_;
133 }
134
135 int DeviceDisplayInfoEflImpl::GetRotationDegrees() const {
136   base::AutoLock autolock(rotation_accessor_);
137   DCHECK_NE(kInvalidRotationDegrees, rotation_degrees_);
138   return rotation_degrees_;
139 }
140
141 DeviceDisplayInfoEflImpl::DeviceDisplayInfoEflImpl()
142   : display_width_(0),
143     display_height_(0),
144     dip_scale_(0),
145     rotation_degrees_(kInvalidRotationDegrees) {
146 }
147
148 // DeviceDisplayInfoEfl
149 DeviceDisplayInfoEfl::DeviceDisplayInfoEfl() {
150   DCHECK(DeviceDisplayInfoEflImpl::GetInstance());
151 }
152
153 void DeviceDisplayInfoEfl::Update(int display_width, int display_height,
154                                   double dip_scale, int rotation_degrees) {
155   DeviceDisplayInfoEflImpl::GetInstance()->Update(
156       display_width, display_height, dip_scale, rotation_degrees);
157 }
158
159 void DeviceDisplayInfoEfl::SetRotationDegrees(int rotation_degrees) {
160   DeviceDisplayInfoEflImpl::GetInstance()->SetRotationDegrees(rotation_degrees);
161 }
162
163 int DeviceDisplayInfoEfl::GetDisplayWidth() const {
164   return DeviceDisplayInfoEflImpl::GetInstance()->GetDisplayWidth();
165 }
166
167 int DeviceDisplayInfoEfl::GetDisplayHeight() const {
168   return DeviceDisplayInfoEflImpl::GetInstance()->GetDisplayHeight();
169 }
170
171 double DeviceDisplayInfoEfl::ComputeDIPScale(int dpi) const {
172   if (IsMobileProfile()) {
173     double dip_scale = static_cast<double>(GetDensityRange(dpi));
174     DCHECK(dip_scale);
175     dip_scale /= kBaselineDPIDensity;
176     return dip_scale;
177   } else {
178     // For platforms other than mobile, the user agents expect 1.0 as DIP scale
179     // for the contents to be delivered.
180     return 1.0;
181   }
182 }
183
184 double DeviceDisplayInfoEfl::GetDIPScale() const {
185   return DeviceDisplayInfoEflImpl::GetInstance()->GetDIPScale();
186 }
187
188 int DeviceDisplayInfoEfl::GetRotationDegrees() const {
189   return DeviceDisplayInfoEflImpl::GetInstance()->GetRotationDegrees();
190 }
191
192 }  // namespace display
193