1 /*
2  * Copyright (C) 2015 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "drmmode.h"
18 #include "drmdevice.h"
19 
20 #include <stdint.h>
21 #include <xf86drmMode.h>
22 #include <string>
23 
24 namespace android {
25 
26 namespace {
HasFlag(const int value,const int & flag)27 inline bool HasFlag(const int value, const int &flag) {
28   return ((value & flag) == flag);
29 }
30 }; // namespace
31 
DrmMode(drmModeModeInfoPtr m)32 DrmMode::DrmMode(drmModeModeInfoPtr m)
33     : id_(0),
34       clock_(m->clock),
35       h_display_(m->hdisplay),
36       h_sync_start_(m->hsync_start),
37       h_sync_end_(m->hsync_end),
38       h_total_(m->htotal),
39       h_skew_(m->hskew),
40       v_display_(m->vdisplay),
41       v_sync_start_(m->vsync_start),
42       v_sync_end_(m->vsync_end),
43       v_total_(m->vtotal),
44       v_scan_(m->vscan),
45       v_refresh_(m->vrefresh),
46       flags_(m->flags),
47       type_(m->type),
48       name_(m->name) {
49 }
50 
operator ==(const drmModeModeInfo & m) const51 bool DrmMode::operator==(const drmModeModeInfo &m) const {
52   return clock_ == m.clock && h_display_ == m.hdisplay &&
53          h_sync_start_ == m.hsync_start && h_sync_end_ == m.hsync_end &&
54          h_total_ == m.htotal && h_skew_ == m.hskew &&
55          v_display_ == m.vdisplay && v_sync_start_ == m.vsync_start &&
56          v_sync_end_ == m.vsync_end && v_total_ == m.vtotal &&
57          v_scan_ == m.vscan && flags_ == m.flags && type_ == m.type;
58 }
59 
ToDrmModeModeInfo(drm_mode_modeinfo * m) const60 void DrmMode::ToDrmModeModeInfo(drm_mode_modeinfo *m) const {
61   m->clock = clock_;
62   m->hdisplay = h_display_;
63   m->hsync_start = h_sync_start_;
64   m->hsync_end = h_sync_end_;
65   m->htotal = h_total_;
66   m->hskew = h_skew_;
67   m->vdisplay = v_display_;
68   m->vsync_start = v_sync_start_;
69   m->vsync_end = v_sync_end_;
70   m->vtotal = v_total_;
71   m->vscan = v_scan_;
72   m->vrefresh = v_refresh_;
73   m->flags = flags_;
74   m->type = type_;
75   strncpy(m->name, name_.c_str(), DRM_DISPLAY_MODE_LEN);
76 }
77 
id() const78 uint32_t DrmMode::id() const {
79   return id_;
80 }
81 
set_id(uint32_t id)82 void DrmMode::set_id(uint32_t id) {
83   id_ = id;
84 }
85 
clock() const86 uint32_t DrmMode::clock() const {
87   return clock_;
88 }
89 
h_display() const90 uint32_t DrmMode::h_display() const {
91   return h_display_;
92 }
93 
h_sync_start() const94 uint32_t DrmMode::h_sync_start() const {
95   return h_sync_start_;
96 }
97 
h_sync_end() const98 uint32_t DrmMode::h_sync_end() const {
99   return h_sync_end_;
100 }
101 
h_total() const102 uint32_t DrmMode::h_total() const {
103   return h_total_;
104 }
105 
h_skew() const106 uint32_t DrmMode::h_skew() const {
107   return h_skew_;
108 }
109 
v_display() const110 uint32_t DrmMode::v_display() const {
111   return v_display_;
112 }
113 
v_sync_start() const114 uint32_t DrmMode::v_sync_start() const {
115   return v_sync_start_;
116 }
117 
v_sync_end() const118 uint32_t DrmMode::v_sync_end() const {
119   return v_sync_end_;
120 }
121 
v_total() const122 uint32_t DrmMode::v_total() const {
123   return v_total_;
124 }
125 
v_scan() const126 uint32_t DrmMode::v_scan() const {
127   return v_scan_;
128 }
129 
v_refresh() const130 float DrmMode::v_refresh() const {
131   // Always recalculate refresh to report correct float rate
132   if (v_total_ == 0 || h_total_ == 0) {
133     return 0.0f;
134   }
135   auto v_refresh = static_cast<float>(clock_) / (float)(v_total_ * h_total_) * 1000.0F;
136   return v_scan_ > 1 ? v_refresh / v_scan_ : v_refresh;
137 }
138 
te_frequency() const139 float DrmMode::te_frequency() const {
140   auto freq = v_refresh();
141   if (type_ & DRM_MODE_TYPE_VRR) {
142     if (HasFlag(flags_, DRM_MODE_FLAG_TE_FREQ_X2)) {
143       freq *= 2;
144     } else if (HasFlag(flags_, DRM_MODE_FLAG_TE_FREQ_X4)) {
145       freq *= 4;
146     } else {
147       if (!HasFlag(flags_, DRM_MODE_FLAG_TE_FREQ_X1)) {
148         return 0.0f;
149       }
150     }
151   } else if (v_scan_ > 1) {
152     freq *= v_scan_;
153   }
154   return freq;
155 }
156 
157 // vertical refresh period.
v_period(int64_t unit) const158 float DrmMode::v_period(int64_t unit) const {
159   auto frequency = v_refresh();
160   if (frequency == 0.0f) {
161     return 0.0f;
162   }
163   return (1.0 / frequency) * unit;
164 }
165 
te_period(int64_t unit) const166 float DrmMode::te_period(int64_t unit) const {
167   auto frequency = te_frequency();
168   if (frequency == 0.0f) {
169     return 0.0f;
170   }
171   return (1.0 / frequency) * unit;
172 }
173 
is_operation_rate_to_bts() const174 bool DrmMode::is_operation_rate_to_bts() const {
175   if (!is_vrr_mode()) {
176     return HasFlag(flags_, DRM_MODE_FLAG_BTS_OP_RATE);
177   }
178   return false;
179 }
180 
is_boost_2x_bts() const181 bool DrmMode::is_boost_2x_bts() const {
182   if (!is_vrr_mode()) {
183     auto vfp = v_sync_start() - v_display();
184     if (vfp > v_display())
185       return true;
186   }
187   return false;
188 }
189 
flags() const190 uint32_t DrmMode::flags() const {
191   return flags_;
192 }
193 
type() const194 uint32_t DrmMode::type() const {
195   return type_;
196 }
197 
name() const198 std::string DrmMode::name() const {
199   return name_;
200 }
201 }  // namespace android
202