1 #include <cassert>
2 #include <stdexcept>
3 
4 #include <xf86drm.h>
5 #include <xf86drmMode.h>
6 
7 #include <kms++/kms++.h>
8 
9 #ifndef DRM_CLIENT_CAP_ATOMIC
10 
11 #define DRM_MODE_ATOMIC_TEST_ONLY 0
12 #define DRM_MODE_ATOMIC_NONBLOCK 0
13 
14 struct _drmModeAtomicReq;
15 typedef struct _drmModeAtomicReq* drmModeAtomicReqPtr;
16 
drmModeAtomicAlloc()17 static inline drmModeAtomicReqPtr drmModeAtomicAlloc()
18 {
19 	return 0;
20 }
drmModeAtomicFree(drmModeAtomicReqPtr)21 static inline void drmModeAtomicFree(drmModeAtomicReqPtr)
22 {
23 }
drmModeAtomicAddProperty(drmModeAtomicReqPtr,uint32_t,uint32_t,uint64_t)24 static inline int drmModeAtomicAddProperty(drmModeAtomicReqPtr, uint32_t, uint32_t, uint64_t)
25 {
26 	return 0;
27 }
drmModeAtomicCommit(int,drmModeAtomicReqPtr,int,void *)28 static inline int drmModeAtomicCommit(int, drmModeAtomicReqPtr, int, void*)
29 {
30 	return 0;
31 }
32 
33 #endif // DRM_CLIENT_CAP_ATOMIC
34 
35 using namespace std;
36 
37 namespace kms
38 {
AtomicReq(Card & card)39 AtomicReq::AtomicReq(Card& card)
40 	: m_card(card)
41 {
42 	assert(card.has_atomic());
43 	m_req = drmModeAtomicAlloc();
44 }
45 
~AtomicReq()46 AtomicReq::~AtomicReq()
47 {
48 	drmModeAtomicFree(m_req);
49 }
50 
add(uint32_t ob_id,uint32_t prop_id,uint64_t value)51 void AtomicReq::add(uint32_t ob_id, uint32_t prop_id, uint64_t value)
52 {
53 	int r = drmModeAtomicAddProperty(m_req, ob_id, prop_id, value);
54 	if (r <= 0)
55 		throw std::invalid_argument("foo");
56 }
57 
add(DrmPropObject * ob,Property * prop,uint64_t value)58 void AtomicReq::add(DrmPropObject* ob, Property* prop, uint64_t value)
59 {
60 	add(ob->id(), prop->id(), value);
61 }
62 
add(kms::DrmPropObject * ob,const string & prop,uint64_t value)63 void AtomicReq::add(kms::DrmPropObject* ob, const string& prop, uint64_t value)
64 {
65 	Property* p = ob->get_prop(prop);
66 
67 	if (!p)
68 		throw runtime_error("Property not found");
69 
70 	add(ob, p, value);
71 }
72 
add(kms::DrmPropObject * ob,const map<string,uint64_t> & values)73 void AtomicReq::add(kms::DrmPropObject* ob, const map<string, uint64_t>& values)
74 {
75 	for (const auto& kvp : values)
76 		add(ob, kvp.first, kvp.second);
77 }
78 
add_display(Connector * conn,Crtc * crtc,Blob * videomode,Plane * primary,Framebuffer * fb)79 void AtomicReq::add_display(Connector* conn, Crtc* crtc, Blob* videomode, Plane* primary, Framebuffer* fb)
80 {
81 	add(conn, {
82 			  { "CRTC_ID", crtc->id() },
83 		  });
84 
85 	add(crtc, {
86 			  { "ACTIVE", 1 },
87 			  { "MODE_ID", videomode->id() },
88 		  });
89 
90 	add(primary, {
91 			     { "FB_ID", fb->id() },
92 			     { "CRTC_ID", crtc->id() },
93 			     { "SRC_X", 0 << 16 },
94 			     { "SRC_Y", 0 << 16 },
95 			     { "SRC_W", fb->width() << 16 },
96 			     { "SRC_H", fb->height() << 16 },
97 			     { "CRTC_X", 0 },
98 			     { "CRTC_Y", 0 },
99 			     { "CRTC_W", fb->width() },
100 			     { "CRTC_H", fb->height() },
101 		     });
102 }
103 
test(bool allow_modeset)104 int AtomicReq::test(bool allow_modeset)
105 {
106 	uint32_t flags = DRM_MODE_ATOMIC_TEST_ONLY;
107 
108 	if (allow_modeset)
109 		flags |= DRM_MODE_ATOMIC_ALLOW_MODESET;
110 
111 	return drmModeAtomicCommit(m_card.fd(), m_req, flags, 0);
112 }
113 
commit(void * data,bool allow_modeset)114 int AtomicReq::commit(void* data, bool allow_modeset)
115 {
116 	uint32_t flags = DRM_MODE_PAGE_FLIP_EVENT | DRM_MODE_ATOMIC_NONBLOCK;
117 
118 	if (allow_modeset)
119 		flags |= DRM_MODE_ATOMIC_ALLOW_MODESET;
120 
121 	return drmModeAtomicCommit(m_card.fd(), m_req, flags, data);
122 }
123 
commit_sync(bool allow_modeset)124 int AtomicReq::commit_sync(bool allow_modeset)
125 {
126 	uint32_t flags = 0;
127 
128 	if (allow_modeset)
129 		flags |= DRM_MODE_ATOMIC_ALLOW_MODESET;
130 
131 	return drmModeAtomicCommit(m_card.fd(), m_req, flags, 0);
132 }
133 } // namespace kms
134