• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <cstdio>
2 #include <iostream>
3 #include <unistd.h>
4 #include <fcntl.h>
5 #include <cassert>
6 
7 #include <kms++/kms++.h>
8 #include "helpers.h"
9 
10 using namespace std;
11 
12 namespace kms
13 {
14 struct CrtcPriv {
15 	drmModeCrtcPtr drm_crtc;
16 };
17 
Crtc(Card & card,uint32_t id,uint32_t idx)18 Crtc::Crtc(Card& card, uint32_t id, uint32_t idx)
19 	: DrmPropObject(card, id, DRM_MODE_OBJECT_CRTC, idx)
20 {
21 	m_priv = new CrtcPriv();
22 	m_priv->drm_crtc = drmModeGetCrtc(this->card().fd(), this->id());
23 	assert(m_priv->drm_crtc);
24 }
25 
~Crtc()26 Crtc::~Crtc()
27 {
28 	drmModeFreeCrtc(m_priv->drm_crtc);
29 	delete m_priv;
30 }
31 
refresh()32 void Crtc::refresh()
33 {
34 	drmModeFreeCrtc(m_priv->drm_crtc);
35 
36 	m_priv->drm_crtc = drmModeGetCrtc(this->card().fd(), this->id());
37 	assert(m_priv->drm_crtc);
38 }
39 
setup()40 void Crtc::setup()
41 {
42 	for (Plane* plane : card().get_planes()) {
43 		if (plane->supports_crtc(this))
44 			m_possible_planes.push_back(plane);
45 	}
46 }
47 
restore_mode(Connector * conn)48 void Crtc::restore_mode(Connector* conn)
49 {
50 	auto c = m_priv->drm_crtc;
51 
52 	uint32_t conns[] = { conn->id() };
53 
54 	drmModeSetCrtc(card().fd(), id(), c->buffer_id,
55 		       c->x, c->y,
56 		       conns, 1, &c->mode);
57 }
58 
set_mode(Connector * conn,const Videomode & mode)59 int Crtc::set_mode(Connector* conn, const Videomode& mode)
60 {
61 	AtomicReq req(card());
62 
63 	unique_ptr<Blob> blob = mode.to_blob(card());
64 
65 	req.add(conn, {
66 			      { "CRTC_ID", this->id() },
67 		      });
68 
69 	req.add(this, {
70 			      { "ACTIVE", 1 },
71 			      { "MODE_ID", blob->id() },
72 		      });
73 
74 	int r = req.commit_sync(true);
75 
76 	refresh();
77 
78 	return r;
79 }
80 
set_mode(Connector * conn,Framebuffer & fb,const Videomode & mode)81 int Crtc::set_mode(Connector* conn, Framebuffer& fb, const Videomode& mode)
82 {
83 	uint32_t conns[] = { conn->id() };
84 	drmModeModeInfo drmmode = video_mode_to_drm_mode(mode);
85 
86 	return drmModeSetCrtc(card().fd(), id(), fb.id(),
87 			      0, 0,
88 			      conns, 1, &drmmode);
89 }
90 
disable_mode()91 int Crtc::disable_mode()
92 {
93 	return drmModeSetCrtc(card().fd(), id(), 0, 0, 0, 0, 0, 0);
94 }
95 
conv(float x)96 static inline uint32_t conv(float x)
97 {
98 	// XXX fix the conversion for fractional part
99 	return ((uint32_t)x) << 16;
100 }
101 
set_plane(Plane * plane,Framebuffer & fb,int32_t dst_x,int32_t dst_y,uint32_t dst_w,uint32_t dst_h,float src_x,float src_y,float src_w,float src_h)102 int Crtc::set_plane(Plane* plane, Framebuffer& fb,
103 		    int32_t dst_x, int32_t dst_y, uint32_t dst_w, uint32_t dst_h,
104 		    float src_x, float src_y, float src_w, float src_h)
105 {
106 	return drmModeSetPlane(card().fd(), plane->id(), id(), fb.id(), 0,
107 			       dst_x, dst_y, dst_w, dst_h,
108 			       conv(src_x), conv(src_y), conv(src_w), conv(src_h));
109 }
110 
disable_plane(Plane * plane)111 int Crtc::disable_plane(Plane* plane)
112 {
113 	return drmModeSetPlane(card().fd(), plane->id(), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
114 }
115 
get_primary_plane()116 Plane* Crtc::get_primary_plane()
117 {
118 	Plane* primary = nullptr;
119 
120 	for (Plane* p : get_possible_planes()) {
121 		if (p->plane_type() != PlaneType::Primary)
122 			continue;
123 
124 		if (p->crtc_id() == id())
125 			return p;
126 
127 		primary = p;
128 	}
129 
130 	if (primary)
131 		return primary;
132 
133 	throw invalid_argument(string("No primary plane for crtc ") + to_string(id()));
134 }
135 
page_flip(Framebuffer & fb,void * data)136 int Crtc::page_flip(Framebuffer& fb, void* data)
137 {
138 	return drmModePageFlip(card().fd(), id(), fb.id(), DRM_MODE_PAGE_FLIP_EVENT, data);
139 }
140 
buffer_id() const141 uint32_t Crtc::buffer_id() const
142 {
143 	return m_priv->drm_crtc->buffer_id;
144 }
145 
x() const146 uint32_t Crtc::x() const
147 {
148 	return m_priv->drm_crtc->x;
149 }
150 
y() const151 uint32_t Crtc::y() const
152 {
153 	return m_priv->drm_crtc->y;
154 }
155 
width() const156 uint32_t Crtc::width() const
157 {
158 	return m_priv->drm_crtc->width;
159 }
160 
height() const161 uint32_t Crtc::height() const
162 {
163 	return m_priv->drm_crtc->height;
164 }
165 
mode_valid() const166 int Crtc::mode_valid() const
167 {
168 	return m_priv->drm_crtc->mode_valid;
169 }
170 
mode() const171 Videomode Crtc::mode() const
172 {
173 	return drm_mode_to_video_mode(m_priv->drm_crtc->mode);
174 }
175 
legacy_gamma_size() const176 int Crtc::legacy_gamma_size() const
177 {
178 	return m_priv->drm_crtc->gamma_size;
179 }
180 
legacy_gamma_set(vector<tuple<uint16_t,uint16_t,uint16_t>> v)181 void Crtc::legacy_gamma_set(vector<tuple<uint16_t, uint16_t, uint16_t>> v)
182 {
183 	uint32_t len = v.size();
184 	uint16_t red[len];
185 	uint16_t green[len];
186 	uint16_t blue[len];
187 
188 	for (uint32_t i = 0; i < len; ++i) {
189 		red[i] = get<0>(v[i]);
190 		green[i] = get<1>(v[i]);
191 		blue[i] = get<2>(v[i]);
192 	}
193 
194 	drmModeCrtcSetGamma(card().fd(), id(), len, red, green, blue);
195 }
196 
197 } // namespace kms
198