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 return clock_ / (float)(v_total_ * h_total_) * 1000.0f;
136 }
137
te_frequency() const138 float DrmMode::te_frequency() const {
139 auto freq = v_refresh();
140 if (is_vrr_mode()) {
141 if (HasFlag(flags_, DRM_MODE_FLAG_TE_FREQ_X2)) {
142 freq *= 2;
143 } else if (HasFlag(flags_, DRM_MODE_FLAG_TE_FREQ_X4)) {
144 freq *= 4;
145 } else {
146 if (!HasFlag(flags_, DRM_MODE_FLAG_TE_FREQ_X1)) {
147 return 0.0f;
148 }
149 }
150 }
151 return freq;
152 }
153
154 // vertical refresh period.
v_period(int64_t unit) const155 float DrmMode::v_period(int64_t unit) const {
156 auto frequency = v_refresh();
157 if (frequency == 0.0f) {
158 return 0.0f;
159 }
160 return (1.0 / frequency) * unit;
161 }
162
te_period(int64_t unit) const163 float DrmMode::te_period(int64_t unit) const {
164 auto frequency = te_frequency();
165 if (frequency == 0.0f) {
166 return 0.0f;
167 }
168 return (1.0 / frequency) * unit;
169 }
170
is_operation_rate_to_bts() const171 bool DrmMode::is_operation_rate_to_bts() const {
172 if (!is_vrr_mode()) {
173 return HasFlag(flags_, DRM_MODE_FLAG_BTS_OP_RATE);
174 }
175 return false;
176 }
177
flags() const178 uint32_t DrmMode::flags() const {
179 return flags_;
180 }
181
type() const182 uint32_t DrmMode::type() const {
183 return type_;
184 }
185
name() const186 std::string DrmMode::name() const {
187 return name_;
188 }
189 } // namespace android
190