summaryrefslogtreecommitdiff
path: root/drmmode.cpp
blob: af077e53c66ad7293ac4948140ae005076af1de4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "drmmode.h"
#include "drmresources.h"

#include <stdint.h>
#include <string>
#include <xf86drm.h>
#include <xf86drmMode.h>

namespace android {

DrmMode::DrmMode(drmModeModeInfoPtr m)
    : id_(0),
      clock_(m->clock),
      h_display_(m->hdisplay),
      h_sync_start_(m->hsync_start),
      h_sync_end_(m->hsync_end),
      h_total_(m->htotal),
      h_skew_(m->hskew),
      v_display_(m->vdisplay),
      v_sync_start_(m->vsync_start),
      v_sync_end_(m->vsync_end),
      v_total_(m->vtotal),
      v_scan_(m->vscan),
      v_refresh_(m->vrefresh),
      flags_(m->flags),
      type_(m->type),
      name_(m->name) {
}

DrmMode::~DrmMode()
{
  if (blob_id_ && fd_) {
    struct drm_mode_destroy_blob destroy_blob;
    memset(&destroy_blob, 0, sizeof(destroy_blob));
    destroy_blob.blob_id = (__u32)blob_id_;
    int ret = drmIoctl(fd_, DRM_IOCTL_MODE_DESTROYPROPBLOB, &destroy_blob);
    if (ret)
      ALOGE("Failed to destroy mode property blob %d, ret=%d", blob_id_, ret);
  }
}

bool DrmMode::operator==(const drmModeModeInfo &m) const {
  return clock_ == m.clock && h_display_ == m.hdisplay &&
         h_sync_start_ == m.hsync_start && h_sync_end_ == m.hsync_end &&
         h_total_ == m.htotal && h_skew_ == m.hskew &&
         v_display_ == m.vdisplay && v_sync_start_ == m.vsync_start &&
         v_sync_end_ == m.vsync_end && v_total_ == m.vtotal &&
         v_scan_ == m.vscan && flags_ == m.flags && type_ == m.type;
}

bool DrmMode::operator==(const DrmMode &m) const {
  return clock_ == m.clock() && h_display_ == m.h_display()&&
         h_sync_start_ == m.h_sync_start() && h_sync_end_ == m.h_sync_end() &&
         h_total_ == m.h_total() && h_skew_ == m.h_skew() &&
         v_display_ == m.v_display() && v_sync_start_ == m.v_sync_start() &&
         v_sync_end_ == m.v_sync_end() && v_total_ == m.v_total() &&
         v_scan_ == m.v_scan() && flags_ == m.flags() && type_ == m.type();
}

bool DrmMode::equal(uint32_t width, uint32_t height, uint32_t vrefresh,
                    bool interlaced) const
{
  int interlaced_ = !!(flags_ & DRM_MODE_FLAG_INTERLACE);

  if (h_display_ == width && v_display_ == height &&
      interlaced_ == interlaced && v_refresh_ == vrefresh)
    return true;
  return false;
}

bool DrmMode::equal(uint32_t width, uint32_t height, uint32_t vrefresh,
                     uint32_t flag, uint32_t clk, bool interlaced) const
{
  int interlaced_ = !!(flags_ & DRM_MODE_FLAG_INTERLACE);
  ALOGV("DrmMode h=%d,v=%d,interlaced=%d,v_refresh_=%d,flags=%d,clk=%d",
         h_display_, v_display_, interlaced_, v_refresh_, flags_,clock_);
  if (h_display_ == width && v_display_ == height &&
      interlaced_ == interlaced && v_refresh_ == vrefresh &&
      flags_ == flag && clock_ == clk)
    return true;
  return false;
}

void DrmMode::ToDrmModeModeInfo(drm_mode_modeinfo *m) const {
  m->clock = clock_;
  m->hdisplay = h_display_;
  m->hsync_start = h_sync_start_;
  m->hsync_end = h_sync_end_;
  m->htotal = h_total_;
  m->hskew = h_skew_;
  m->vdisplay = v_display_;
  m->vsync_start = v_sync_start_;
  m->vsync_end = v_sync_end_;
  m->vtotal = v_total_;
  m->vscan = v_scan_;
  m->vrefresh = v_refresh_;
  m->flags = flags_;
  m->type = type_;
  strncpy(m->name, name_.c_str(), DRM_DISPLAY_MODE_LEN);
}

uint32_t DrmMode::id() const {
  return id_;
}

void DrmMode::set_id(uint32_t id) {
  id_ = id;
}

uint32_t DrmMode::clock() const {
  return clock_;
}

uint32_t DrmMode::h_display() const {
  return h_display_;
}

uint32_t DrmMode::h_sync_start() const {
  return h_sync_start_;
}

uint32_t DrmMode::h_sync_end() const {
  return h_sync_end_;
}

uint32_t DrmMode::h_total() const {
  return h_total_;
}

uint32_t DrmMode::h_skew() const {
  return h_skew_;
}

uint32_t DrmMode::v_display() const {
  return v_display_;
}

uint32_t DrmMode::v_sync_start() const {
  return v_sync_start_;
}

uint32_t DrmMode::v_sync_end() const {
  return v_sync_end_;
}

uint32_t DrmMode::v_total() const {
  return v_total_;
}

uint32_t DrmMode::v_scan() const {
  return v_scan_;
}

float DrmMode::v_refresh() const {
  return v_refresh_ ? v_refresh_ * 1.0f :
                      clock_ / (float)(v_total_ * h_total_) * 1000.0f;
}

uint32_t DrmMode::flags() const {
  return flags_;
}

uint32_t DrmMode::type() const {
  return type_;
}

std::string DrmMode::name() const {
  return name_;
}
}