File/dir renames
[android/external-libkmsxx.git] / utils / db.cpp
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 fb = m_flipper.get_next();
93                 int r = m_crtc->set_mode(m_connector, *fb, m_mode);
94                 ASSERT(r == 0);
96                 if (m_crtc->card().has_atomic())
97                         m_root_plane = m_crtc->get_primary_plane();
99                 if (m_plane) {
100                         auto planefb = m_plane_flipper->get_next();
101                         r = m_crtc->set_plane(m_plane, *planefb,
102                                               0, 0, planefb->width(), planefb->height(),
103                                               0, 0, planefb->width(), planefb->height());
104                         ASSERT(r == 0);
105                 }
106         }
108         void start_flipping()
109         {
110                 m_time_last = m_t1 = std::chrono::steady_clock::now();
111                 m_slowest_frame = std::chrono::duration<float>::min();
112                 m_frame_num = 0;
113                 queue_next();
114         }
116 private:
117         void handle_page_flip(uint32_t frame, double time)
118         {
119                 ++m_frame_num;
121                 auto now = std::chrono::steady_clock::now();
123                 std::chrono::duration<float> diff = now - m_time_last;
124                 if (diff > m_slowest_frame)
125                         m_slowest_frame = diff;
127                 if (m_frame_num  % 100 == 0) {
128                         std::chrono::duration<float> fsec = now - m_t1;
129                         printf("Output %d: fps %f, slowest %.2f ms\n",
130                                m_connector->idx(), 100.0 / fsec.count(),
131                                m_slowest_frame.count() * 1000);
132                         m_t1 = now;
133                         m_slowest_frame = std::chrono::duration<float>::min();
134                 }
136                 m_time_last = now;
138                 queue_next();
139         }
141         void queue_next()
142         {
143                 auto crtc = m_crtc;
144                 auto& card = crtc->card();
146                 auto fb = m_flipper.get_next();
147                 Framebuffer* planefb = m_plane ? m_plane_flipper->get_next() : 0;
149                 if (card.has_atomic()) {
150                         int r;
152                         AtomicReq req(card);
154                         req.add(m_root_plane, "FB_ID", fb->id());
155                         if (m_plane)
156                                 req.add(m_plane, "FB_ID", planefb->id());
158                         r = req.test();
159                         ASSERT(r == 0);
161                         r = req.commit(this);
162                         ASSERT(r == 0);
163                 } else {
164                         int r = crtc->page_flip(*fb, this);
165                         ASSERT(r == 0);
167                         if (m_plane) {
168                                 r = m_crtc->set_plane(m_plane, *planefb,
169                                                       0, 0, planefb->width(), planefb->height(),
170                                                       0, 0, planefb->width(), planefb->height());
171                                 ASSERT(r == 0);
172                         }
173                 }
174         }
176 private:
177         Connector* m_connector;
178         Crtc* m_crtc;
179         Videomode m_mode;
180         Plane* m_root_plane;
182         int m_frame_num;
183         chrono::steady_clock::time_point m_t1;
184         chrono::steady_clock::time_point m_time_last;
185         chrono::duration<float> m_slowest_frame;
187         Flipper m_flipper;
189         Plane* m_plane;
190         Flipper* m_plane_flipper;
191 };
193 int main()
195         Card card;
197         if (card.master() == false)
198                 printf("Not DRM master, modeset may fail\n");
200         vector<OutputFlipHandler*> outputs;
202         for (auto pipe : card.get_connected_pipelines())
203         {
204                 auto conn = pipe.connector;
205                 auto crtc = pipe.crtc;
206                 auto mode = conn->get_default_mode();
209                 Plane* plane = 0;
210 #if 0 // disable the plane for now
211                 for (Plane* p : crtc->get_possible_planes()) {
212                         if (p->plane_type() == PlaneType::Overlay) {
213                                 plane = p;
214                                 break;
215                         }
216                 }
217 #endif
218                 OutputFlipHandler* output;
219                 if (plane)
220                         output = new OutputFlipHandler(conn, crtc, mode, plane, 500, 400);
221                 else
222                         output = new OutputFlipHandler(conn, crtc, mode);
223                 outputs.push_back(output);
224         }
226         for(auto out : outputs)
227                 out->set_mode();
229         for(auto out : outputs)
230                 out->start_flipping();
232         main_loop(card);
234         for(auto out : outputs)
235                 delete out;
238 static void main_loop(Card& card)
240         fd_set fds;
242         FD_ZERO(&fds);
244         int fd = card.fd();
246         printf("press enter to exit\n");
248         while (true) {
249                 int r;
251                 FD_SET(0, &fds);
252                 FD_SET(fd, &fds);
254                 r = select(fd + 1, &fds, NULL, NULL, NULL);
255                 if (r < 0) {
256                         fprintf(stderr, "select() failed with %d: %m\n", errno);
257                         break;
258                 } else if (FD_ISSET(0, &fds)) {
259                         fprintf(stderr, "exit due to user-input\n");
260                         break;
261                 } else if (FD_ISSET(fd, &fds)) {
262                         card.call_page_flip_handlers();
263                 }
264         }