c51d3ec4cc989ac373f610ff3b937e7c339da0b5
[android/external-libkmsxx.git] / tests / testpat.cpp
1 #include <cstdio>
2 #include <algorithm>
3 #include <regex>
4 #include <set>
6 #include "kms++.h"
8 #include "test.h"
9 #include "opts.h"
11 using namespace std;
12 using namespace kms;
14 struct PlaneInfo
15 {
16         Plane* plane;
18         unsigned x;
19         unsigned y;
20         unsigned w;
21         unsigned h;
23         DumbFramebuffer* fb;
24 };
26 struct OutputInfo
27 {
28         Connector* connector;
30         Crtc* crtc;
31         Videomode mode;
32         bool user_set_crtc;
33         DumbFramebuffer* fb;
35         vector<PlaneInfo> planes;
36 };
38 static set<Crtc*> s_used_crtcs;
39 static set<Plane*> s_used_planes;
41 __attribute__ ((unused))
42 static void print_regex_match(smatch sm)
43 {
44         for (unsigned i = 0; i < sm.size(); ++i) {
45                 string str = sm[i].str();
46                 printf("%u: %s\n", i, str.c_str());
47         }
48 }
50 static void get_default_connector(Card& card, OutputInfo& output)
51 {
52         output.connector = card.get_first_connected_connector();
53         output.mode = output.connector->get_default_mode();
54 }
56 static void parse_connector(Card& card, const string& str, OutputInfo& output)
57 {
58         Connector* conn = nullptr;
60         auto connectors = card.get_connectors();
62         if (str[0] == '@') {
63                 char* endptr;
64                 unsigned idx = strtoul(str.c_str() + 1, &endptr, 10);
65                 if (*endptr == 0) {
66                         if (idx >= connectors.size())
67                                 EXIT("Bad connector number '%u'", idx);
69                         conn = connectors[idx];
70                 }
71         } else {
72                 char* endptr;
73                 unsigned id = strtoul(str.c_str(), &endptr, 10);
74                 if (*endptr == 0) {
75                         Connector* c = card.get_connector(id);
76                         if (!c)
77                                 EXIT("Bad connector id '%u'", id);
79                         conn = c;
80                 }
81         }
83         if (!conn) {
84                 auto iter = find_if(connectors.begin(), connectors.end(), [&str](Connector *c) { return c->fullname() == str; });
85                 if (iter != connectors.end())
86                         conn = *iter;
87         }
89         if (!conn)
90                 EXIT("No connector '%s'", str.c_str());
92         if (!conn->connected())
93                 EXIT("Connector '%s' not connected", conn->fullname().c_str());
95         output.connector = conn;
96         output.mode = output.connector->get_default_mode();
97 }
99 static void get_default_crtc(Card& card, OutputInfo& output)
101         Crtc* crtc = output.connector->get_current_crtc();
103         if (crtc && s_used_crtcs.find(crtc) == s_used_crtcs.end()) {
104                 s_used_crtcs.insert(crtc);
105                 output.crtc = crtc;
106                 return;
107         }
109         for (const auto& possible : output.connector->get_possible_crtcs()) {
110                 if (s_used_crtcs.find(possible) == s_used_crtcs.end()) {
111                         s_used_crtcs.insert(possible);
112                         output.crtc = possible;
113                         return;
114                 }
115         }
117         EXIT("Could not find available crtc");
120 static void parse_crtc(Card& card, const string& crtc_str, OutputInfo& output)
122         // @12:1920x1200-60
123         const regex mode_re("(?:(@?)(\\d+):)?(?:(\\d+)x(\\d+))(?:-(\\d+))?");
125         smatch sm;
126         if (!regex_match(crtc_str, sm, mode_re))
127                 EXIT("Failed to parse crtc option '%s'", crtc_str.c_str());
129         if (sm[2].matched) {
130                 bool use_idx = sm[1].length() == 1;
131                 unsigned num = stoul(sm[2].str());
133                 if (use_idx) {
134                         auto crtcs = card.get_crtcs();
136                         if (num >= crtcs.size())
137                                 EXIT("Bad crtc number '%u'", num);
139                         output.crtc = crtcs[num];
140                 } else {
141                         Crtc* c = card.get_crtc(num);
142                         if (!c)
143                                 EXIT("Bad crtc id '%u'", num);
145                         output.crtc = c;
146                 }
147         } else {
148                 output.crtc = output.connector->get_current_crtc();
149         }
151         unsigned w = stoul(sm[3]);
152         unsigned h = stoul(sm[4]);
153         unsigned refresh = 0;
154         if (sm[5].matched)
155                 refresh = stoul(sm[5]);
157         output.mode = output.connector->get_mode(w, h, refresh);
160 static void parse_plane(Card& card, const string& plane_str, const OutputInfo& output, PlaneInfo& pinfo)
162         // 3:400,400-400x400
163         const regex plane_re("(?:(@?)(\\d+):)?(?:(\\d+),(\\d+)-)?(\\d+)x(\\d+)");
165         smatch sm;
166         if (!regex_match(plane_str, sm, plane_re))
167                 EXIT("Failed to parse plane option '%s'", plane_str.c_str());
169         if (sm[2].matched) {
170                 bool use_idx = sm[1].length() == 1;
171                 unsigned num = stoul(sm[2].str());
173                 if (use_idx) {
174                         auto planes = card.get_planes();
176                         if (num >= planes.size())
177                                 EXIT("Bad plane number '%u'", num);
179                         pinfo.plane = planes[num];
180                 } else {
181                         Plane* p = card.get_plane(num);
182                         if (!p)
183                                 EXIT("Bad plane id '%u'", num);
185                         pinfo.plane = p;
186                 }
187         } else {
188                 for (Plane* p : output.crtc->get_possible_planes()) {
189                         if (s_used_planes.find(p) != s_used_planes.end())
190                                 continue;
192                         if (p->plane_type() != PlaneType::Overlay)
193                                 continue;
195                         pinfo.plane = p;
196                 }
198                 if (!pinfo.plane)
199                         EXIT("Failed to find available plane");
200         }
202         s_used_planes.insert(pinfo.plane);
204         pinfo.w = stoul(sm[5]);
205         pinfo.h = stoul(sm[6]);
207         if (sm[3].matched)
208                 pinfo.x = stoul(sm[3]);
209         else
210                 pinfo.x = output.mode.hdisplay / 2 - pinfo.w / 2;
212         if (sm[4].matched)
213                 pinfo.y = stoul(sm[4]);
214         else
215                 pinfo.y = output.mode.vdisplay / 2 - pinfo.h / 2;
218 static DumbFramebuffer* get_default_fb(Card& card, unsigned width, unsigned height)
220         auto fb = new DumbFramebuffer(card, width, height, PixelFormat::XRGB8888);
221         draw_test_pattern(*fb);
222         return fb;
225 static DumbFramebuffer* parse_fb(Card& card, const string& fb_str, unsigned def_w, unsigned def_h)
227         unsigned w = def_w;
228         unsigned h = def_h;
229         PixelFormat format = PixelFormat::XRGB8888;
231         if (!fb_str.empty()) {
232                 // XXX the regexp is not quite correct
233                 // 400x400-NV12
234                 const regex fb_re("(?:(\\d+)x(\\d+))?(?:-)?(\\w\\w\\w\\w)?");
236                 smatch sm;
237                 if (!regex_match(fb_str, sm, fb_re))
238                         EXIT("Failed to parse fb option '%s'", fb_str.c_str());
240                 if (sm[1].matched)
241                         w = stoul(sm[1]);
242                 if (sm[2].matched)
243                         h = stoul(sm[2]);
244                 if (sm[3].matched)
245                         format = FourCCToPixelFormat(sm[3]);
246         }
248         auto fb = new DumbFramebuffer(card, w, h, format);
249         draw_test_pattern(*fb);
250         return fb;
253 static const char* usage_str =
254                 "Usage: testpat [OPTION]...\n\n"
255                 "Show a test pattern on a display or plane\n\n"
256                 "Options:\n"
257                 "      --device=DEVICE       DEVICE is the path to DRM card to open\n"
258                 "  -c, --connector=CONN      CONN is <connector>\n"
259                 "  -r, --crtc=CRTC           CRTC is [<crtc>:]<w>x<h>[@<Hz>]\n"
260                 "  -p, --plane=PLANE         PLANE is [<plane>:][<x>,<y>-]<w>x<h>\n"
261                 "  -f, --fb=FB               FB is [<w>x<h>][-][<4cc>]\n"
262                 "\n"
263                 "<connector>, <crtc> and <plane> can be given by id (<id>) or index (@<idx>).\n"
264                 "<connector> can also be given by name.\n"
265                 "\n"
266                 "Options can be given multiple times to set up multiple displays or planes.\n"
267                 "Options may apply to previous options, e.g. a plane will be set on a crtc set in\n"
268                 "an earlier option.\n"
269                 "If you omit parameters, testpat tries to guess what you mean\n"
270                 "\n"
271                 "Examples:\n"
272                 "\n"
273                 "Set eDP-1 mode to 1920x1080@60, show XR24 framebuffer on the crtc, and a 400x400 XB24 plane:\n"
274                 "    testpat -c eDP-1 -r 1920x1080@60 -f XR24 -p 400x400 -f XB24\n\n"
275                 "XR24 framebuffer on first connected connector in the default mode:\n"
276                 "    testpat -f XR24\n\n"
277                 "XR24 framebuffer on a 400x400 plane on the first connected connector in the default mode:\n"
278                 "    testpat -p 400x400 -f XR24\n\n"
279                 "Test pattern on the second connector with default mode:\n"
280                 "    testpat -c @1\n"
281                 ;
283 static void usage()
285         puts(usage_str);
288 enum class ObjectType
290         Connector,
291         Crtc,
292         Plane,
293         Framebuffer,
294 };
296 struct Arg
298         ObjectType type;
299         string arg;
300 };
302 static string s_device_path = "/dev/dri/card0";
304 static vector<Arg> parse_cmdline(int argc, char **argv)
306         vector<Arg> args;
308         OptionSet optionset = {
309                 Option("|device=",
310                 [&](string s)
311                 {
312                         s_device_path = s;
313                 }),
314                 Option("c|connector=",
315                 [&](string s)
316                 {
317                         args.push_back(Arg { ObjectType::Connector, s });
318                 }),
319                 Option("r|crtc=", [&](string s)
320                 {
321                         args.push_back(Arg { ObjectType::Crtc, s });
322                 }),
323                 Option("p|plane=", [&](string s)
324                 {
325                         args.push_back(Arg { ObjectType::Plane, s });
326                 }),
327                 Option("f|fb=", [&](string s)
328                 {
329                         args.push_back(Arg { ObjectType::Framebuffer, s });
330                 }),
331                 Option("h|help", [&]()
332                 {
333                         usage();
334                         exit(-1);
335                 }),
336         };
338         optionset.parse(argc, argv);
340         if (optionset.params().size() > 0) {
341                 usage();
342                 exit(-1);
343         }
345         return args;
348 static vector<OutputInfo> setups_to_outputs(Card& card, const vector<Arg>& output_args)
350         vector<OutputInfo> outputs;
352         if (output_args.size() == 0) {
353                 // no output args, show a pattern on all screens
354                 for (auto& pipe : card.get_connected_pipelines()) {
355                         OutputInfo output = { };
356                         output.connector = pipe.connector;
357                         output.crtc = pipe.crtc;
358                         output.mode = output.connector->get_default_mode();
360                         output.fb = get_default_fb(card, output.mode.hdisplay, output.mode.vdisplay);
362                         outputs.push_back(output);
363                 }
365                 return outputs;
366         }
368         OutputInfo* current_output = 0;
369         PlaneInfo* current_plane = 0;
371         for (auto& arg : output_args) {
372                 switch (arg.type) {
373                 case ObjectType::Connector:
374                 {
375                         outputs.push_back(OutputInfo { });
376                         current_output = &outputs.back();
378                         parse_connector(card, arg.arg, *current_output);
379                         current_plane = 0;
381                         break;
382                 }
384                 case ObjectType::Crtc:
385                 {
386                         if (!current_output) {
387                                 outputs.push_back(OutputInfo { });
388                                 current_output = &outputs.back();
389                         }
391                         if (!current_output->connector)
392                                 get_default_connector(card, *current_output);
394                         parse_crtc(card, arg.arg, *current_output);
396                         current_output->user_set_crtc = true;
398                         current_plane = 0;
400                         break;
401                 }
403                 case ObjectType::Plane:
404                 {
405                         if (!current_output) {
406                                 outputs.push_back(OutputInfo { });
407                                 current_output = &outputs.back();
408                         }
410                         if (!current_output->connector)
411                                 get_default_connector(card, *current_output);
413                         if (!current_output->crtc)
414                                 get_default_crtc(card, *current_output);
416                         current_output->planes.push_back(PlaneInfo { });
417                         current_plane = &current_output->planes.back();
419                         parse_plane(card, arg.arg, *current_output, *current_plane);
421                         break;
422                 }
424                 case ObjectType::Framebuffer:
425                 {
426                         if (!current_output) {
427                                 outputs.push_back(OutputInfo { });
428                                 current_output = &outputs.back();
429                         }
431                         if (!current_output->connector)
432                                 get_default_connector(card, *current_output);
434                         if (!current_output->crtc)
435                                 get_default_crtc(card, *current_output);
437                         int def_w, def_h;
439                         if (current_plane) {
440                                 def_w = current_plane->w;
441                                 def_h = current_plane->h;
442                         } else {
443                                 def_w = current_output->mode.hdisplay;
444                                 def_h = current_output->mode.vdisplay;
445                         }
447                         auto fb = parse_fb(card, arg.arg, def_w, def_h);
449                         if (current_plane)
450                                 current_plane->fb = fb;
451                         else
452                                 current_output->fb = fb;
454                         break;
455                 }
456                 }
457         }
459         // create default framebuffers if needed
460         for (OutputInfo& o : outputs) {
461                 if (!o.crtc) {
462                         get_default_crtc(card, *current_output);
463                         o.user_set_crtc = true;
464                 }
466                 if (!o.fb && o.user_set_crtc)
467                         o.fb = get_default_fb(card, o.mode.hdisplay, o.mode.vdisplay);
469                 for (PlaneInfo &p : o.planes) {
470                         if (!p.fb)
471                                 p.fb = get_default_fb(card, p.w, p.h);
472                 }
473         }
475         return outputs;
478 static std::string videomode_to_string(const Videomode& mode)
480         unsigned hfp, hsw, hbp;
481         unsigned vfp, vsw, vbp;
483         hfp = mode.hsync_start - mode.hdisplay;
484         hsw = mode.hsync_end - mode.hsync_start;
485         hbp = mode.htotal - mode.hsync_end;
487         vfp = mode.vsync_start - mode.vdisplay;
488         vsw = mode.vsync_end - mode.vsync_start;
489         vbp = mode.vtotal - mode.vsync_end;
491         char buf[256];
493         sprintf(buf, "%.2f MHz %u/%u/%u/%u %u/%u/%u/%u %uHz",
494                 mode.clock / 1000.0,
495                 mode.hdisplay, hfp, hsw, hbp,
496                 mode.vdisplay, vfp, vsw, vbp,
497                 mode.vrefresh);
499         return std::string(buf);
502 static void print_outputs(const vector<OutputInfo>& outputs)
504         for (unsigned i = 0; i < outputs.size(); ++i) {
505                 const OutputInfo& o = outputs[i];
507                 printf("Connector %u/@%u: %s\n", o.connector->id(), o.connector->idx(),
508                        o.connector->fullname().c_str());
509                 printf("  Crtc %u/@%u: %ux%u-%u (%s)\n", o.crtc->id(), o.crtc->idx(),
510                        o.mode.hdisplay, o.mode.vdisplay, o.mode.vrefresh,
511                        videomode_to_string(o.mode).c_str());
512                 if (o.fb)
513                         printf("    Fb %ux%u-%s\n", o.fb->width(), o.fb->height(),
514                                PixelFormatToFourCC(o.fb->format()).c_str());
516                 for (unsigned j = 0; j < o.planes.size(); ++j) {
517                         const PlaneInfo& p = o.planes[j];
518                         printf("  Plane %u/@%u: %u,%u-%ux%u\n", p.plane->id(), p.plane->idx(),
519                                p.x, p.y, p.w, p.h);
520                         printf("    Fb %ux%u-%s\n", p.fb->width(), p.fb->height(),
521                                PixelFormatToFourCC(p.fb->format()).c_str());
522                 }
523         }
526 static void set_crtcs_n_planes(Card& card, const vector<OutputInfo>& outputs)
528         for (const OutputInfo& o : outputs) {
529                 auto conn = o.connector;
530                 auto crtc = o.crtc;
532                 if (o.fb) {
533                         int r = crtc->set_mode(conn, *o.fb, o.mode);
534                         if (r)
535                                 printf("crtc->set_mode() failed for crtc %u: %s\n",
536                                        crtc->id(), strerror(-r));
537                 }
539                 for (const PlaneInfo& p : o.planes) {
540                         int r = crtc->set_plane(p.plane, *p.fb,
541                                                 p.x, p.y, p.w, p.h,
542                                                 0, 0, p.fb->width(), p.fb->height());
543                         if (r)
544                                 printf("crtc->set_plane() failed for plane %u: %s\n",
545                                        p.plane->id(), strerror(-r));
546                 }
547         }
550 int main(int argc, char **argv)
552         vector<Arg> output_args = parse_cmdline(argc, argv);
554         Card card(s_device_path);
556         vector<OutputInfo> outputs = setups_to_outputs(card, output_args);
558         print_outputs(outputs);
560         set_crtcs_n_planes(card, outputs);
562         printf("press enter to exit\n");
564         getchar();