4a7846cb5ce47d1bbe0b6a645190830fe5f50ccf
1 #include <cstdio>
2 #include <algorithm>
3 #include <chrono>
5 #include <xf86drm.h>
6 #include <xf86drmMode.h>
7 #include <drm_fourcc.h>
9 #include "kms++.h"
11 #include "test.h"
13 using namespace std;
14 using namespace kms;
16 static void main_loop(Card& card);
18 class Flipper
19 {
20 public:
21 Flipper(Card& card, unsigned width, unsigned height)
22 : m_current(0), m_bar_xpos(0)
23 {
24 auto format = PixelFormat::XRGB8888;
25 m_fbs[0] = new DumbFramebuffer(card, width, height, format);
26 m_fbs[1] = new DumbFramebuffer(card, width, height, format);
27 }
29 ~Flipper()
30 {
31 delete m_fbs[0];
32 delete m_fbs[1];
33 }
35 Framebuffer* get_next()
36 {
37 m_current ^= 1;
39 const int bar_width = 20;
40 const int bar_speed = 8;
42 auto fb = m_fbs[m_current];
44 int current_xpos = m_bar_xpos;
45 int old_xpos = (current_xpos + (fb->width() - bar_width - bar_speed)) % (fb->width() - bar_width);
46 int new_xpos = (current_xpos + bar_speed) % (fb->width() - bar_width);
48 draw_color_bar(*fb, old_xpos, new_xpos, bar_width);
50 m_bar_xpos = new_xpos;
52 return fb;
53 }
55 private:
56 DumbFramebuffer* m_fbs[2];
58 int m_current;
59 int m_bar_xpos;
60 };
62 class OutputFlipHandler : private PageFlipHandlerBase
63 {
64 public:
65 OutputFlipHandler(Connector* conn, Crtc* crtc, const Videomode& mode)
66 : m_connector(conn), m_crtc(crtc), m_mode(mode),
67 m_flipper(conn->card(), mode.hdisplay, mode.vdisplay),
68 m_plane(0), m_plane_flipper(0)
69 {
70 }
72 OutputFlipHandler(Connector* conn, Crtc* crtc, const Videomode& mode,
73 Plane* plane, unsigned pwidth, unsigned pheight)
74 : m_connector(conn), m_crtc(crtc), m_mode(mode),
75 m_flipper(conn->card(), mode.hdisplay, mode.vdisplay),
76 m_plane(plane)
77 {
78 m_plane_flipper = new Flipper(conn->card(), pwidth, pheight);
79 }
81 ~OutputFlipHandler()
82 {
83 if (m_plane_flipper)
84 delete m_plane_flipper;
85 }
87 OutputFlipHandler(const OutputFlipHandler& other) = delete;
88 OutputFlipHandler& operator=(const OutputFlipHandler& other) = delete;
90 void set_mode()
91 {
92 auto mode = m_connector->get_default_mode();
93 auto fb = m_flipper.get_next();
94 int r = m_crtc->set_mode(m_connector, *fb, mode);
95 ASSERT(r == 0);
97 if (m_plane) {
98 auto planefb = m_plane_flipper->get_next();
99 r = m_crtc->set_plane(m_plane, *planefb,
100 0, 0, planefb->width(), planefb->height(),
101 0, 0, planefb->width(), planefb->height());
102 ASSERT(r == 0);
103 }
104 }
106 void start_flipping()
107 {
108 m_time_last = m_t1 = std::chrono::steady_clock::now();
109 m_slowest_frame = std::chrono::duration<float>::min();
110 m_frame_num = 0;
111 queue_next();
112 }
114 private:
115 void handle_page_flip(uint32_t frame, double time)
116 {
117 ++m_frame_num;
119 auto now = std::chrono::steady_clock::now();
121 std::chrono::duration<float> diff = now - m_time_last;
122 if (diff > m_slowest_frame)
123 m_slowest_frame = diff;
125 if (m_frame_num % 100 == 0) {
126 std::chrono::duration<float> fsec = now - m_t1;
127 printf("Output %d: fps %f, slowest %.2f ms\n",
128 m_connector->idx(), 100.0 / fsec.count(),
129 m_slowest_frame.count() * 1000);
130 m_t1 = now;
131 m_slowest_frame = std::chrono::duration<float>::min();
132 }
134 m_time_last = now;
136 queue_next();
137 }
139 void queue_next()
140 {
141 auto crtc = m_crtc;
142 auto& card = crtc->card();
144 auto fb = m_flipper.get_next();
145 Framebuffer* planefb = m_plane ? m_plane_flipper->get_next() : 0;
147 if (card.has_atomic()) {
148 int r;
150 AtomicReq req(card);
152 req.add(m_crtc, "FB_ID", fb->id());
153 if (m_plane)
154 req.add(m_plane, "FB_ID", planefb->id());
156 r = req.test();
157 ASSERT(r == 0);
159 r = req.commit(this);
160 ASSERT(r == 0);
161 } else {
162 int r = crtc->page_flip(*fb, this);
163 ASSERT(r == 0);
165 if (m_plane) {
166 r = m_crtc->set_plane(m_plane, *planefb,
167 0, 0, planefb->width(), planefb->height(),
168 0, 0, planefb->width(), planefb->height());
169 ASSERT(r == 0);
170 }
171 }
172 }
174 private:
175 Connector* m_connector;
176 Crtc* m_crtc;
177 Videomode m_mode;
179 int m_frame_num;
180 chrono::steady_clock::time_point m_t1;
181 chrono::steady_clock::time_point m_time_last;
182 chrono::duration<float> m_slowest_frame;
184 Flipper m_flipper;
186 Plane* m_plane;
187 Flipper* m_plane_flipper;
188 };
190 int main()
191 {
192 Card card;
194 if (card.master() == false)
195 printf("Not DRM master, modeset may fail\n");
197 vector<OutputFlipHandler*> outputs;
199 for (auto pipe : card.get_connected_pipelines())
200 {
201 auto conn = pipe.connector;
202 auto crtc = pipe.crtc;
203 auto mode = conn->get_default_mode();
206 Plane* plane = 0;
207 #if 0 // disable the plane for now
208 for (Plane* p : crtc->get_possible_planes()) {
209 if (p->plane_type() == PlaneType::Overlay) {
210 plane = p;
211 break;
212 }
213 }
214 #endif
215 OutputFlipHandler* output;
216 if (plane)
217 output = new OutputFlipHandler(conn, crtc, mode, plane, 500, 400);
218 else
219 output = new OutputFlipHandler(conn, crtc, mode);
220 outputs.push_back(output);
221 }
223 for(auto out : outputs)
224 out->set_mode();
226 for(auto out : outputs)
227 out->start_flipping();
229 main_loop(card);
231 for(auto out : outputs)
232 delete out;
233 }
235 static void main_loop(Card& card)
236 {
237 fd_set fds;
239 FD_ZERO(&fds);
241 int fd = card.fd();
243 printf("press enter to exit\n");
245 while (true) {
246 int r;
248 FD_SET(0, &fds);
249 FD_SET(fd, &fds);
251 r = select(fd + 1, &fds, NULL, NULL, NULL);
252 if (r < 0) {
253 fprintf(stderr, "select() failed with %d: %m\n", errno);
254 break;
255 } else if (FD_ISSET(0, &fds)) {
256 fprintf(stderr, "exit due to user-input\n");
257 break;
258 } else if (FD_ISSET(fd, &fds)) {
259 card.call_page_flip_handlers();
260 }
261 }
262 }