]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - android/external-libkmsxx.git/blob - utils/kmstest.cpp
kmstest: remove get_default_connector()
[android/external-libkmsxx.git] / utils / kmstest.cpp
1 #include <cstdio>
2 #include <cstring>
3 #include <algorithm>
4 #include <regex>
5 #include <set>
6 #include <chrono>
8 #include <sys/select.h>
10 #include <kms++/kms++.h>
11 #include <kms++/modedb.h>
12 #include <kms++/mode_cvt.h>
14 #include <kms++util/kms++util.h>
16 using namespace std;
17 using namespace kms;
19 struct PlaneInfo
20 {
21         Plane* plane;
23         unsigned x;
24         unsigned y;
25         unsigned w;
26         unsigned h;
28         unsigned view_x;
29         unsigned view_y;
30         unsigned view_w;
31         unsigned view_h;
33         vector<MappedFramebuffer*> fbs;
34 };
36 struct OutputInfo
37 {
38         Connector* connector;
40         Crtc* crtc;
41         Plane* primary_plane;
42         Videomode mode;
43         bool user_set_crtc;
44         vector<MappedFramebuffer*> fbs;
46         vector<PlaneInfo> planes;
47 };
49 static bool s_use_dmt;
50 static bool s_use_cea;
51 static unsigned s_num_buffers = 1;
52 static bool s_flip_mode;
53 static bool s_flip_sync;
54 static bool s_cvt;
55 static bool s_cvt_v2;
56 static bool s_cvt_vid_opt;
58 static set<Crtc*> s_used_crtcs;
59 static set<Plane*> s_used_planes;
61 __attribute__ ((unused))
62 static void print_regex_match(smatch sm)
63 {
64         for (unsigned i = 0; i < sm.size(); ++i) {
65                 string str = sm[i].str();
66                 printf("%u: %s\n", i, str.c_str());
67         }
68 }
70 static void get_connector(ResourceManager& resman, OutputInfo& output, const string& str = "")
71 {
72         Connector* conn = resman.reserve_connector(str);
74         if (!conn)
75                 EXIT("No connector '%s'", str.c_str());
77         if (!conn->connected())
78                 EXIT("Connector '%s' not connected", conn->fullname().c_str());
80         output.connector = conn;
81         output.mode = output.connector->get_default_mode();
82 }
84 static void get_default_crtc(Card& card, OutputInfo& output)
85 {
86         Crtc* crtc = output.connector->get_current_crtc();
88         if (crtc && s_used_crtcs.find(crtc) == s_used_crtcs.end()) {
89                 s_used_crtcs.insert(crtc);
90                 output.crtc = crtc;
91                 return;
92         }
94         for (const auto& possible : output.connector->get_possible_crtcs()) {
95                 if (s_used_crtcs.find(possible) == s_used_crtcs.end()) {
96                         s_used_crtcs.insert(possible);
97                         output.crtc = possible;
98                         return;
99                 }
100         }
102         EXIT("Could not find available crtc");
105 static void parse_crtc(Card& card, const string& crtc_str, OutputInfo& output)
107         // @12:1920x1200i@60
108         // @12:33000000,800/210/30/16/-,480/22/13/10/-,i
110         const regex modename_re("(?:(@?)(\\d+):)?"      // @12:
111                                 "(?:(\\d+)x(\\d+)(i)?)" // 1920x1200i
112                                 "(?:@([\\d\\.]+))?");   // @60
114         const regex modeline_re("(?:(@?)(\\d+):)?"                      // @12:
115                                 "(\\d+),"                               // 33000000,
116                                 "(\\d+)/(\\d+)/(\\d+)/(\\d+)/([+-]),"   // 800/210/30/16/-,
117                                 "(\\d+)/(\\d+)/(\\d+)/(\\d+)/([+-])"    // 480/22/13/10/-
118                                 "(?:,([i]+))?"                          // ,i
119                                 );
121         smatch sm;
122         if (regex_match(crtc_str, sm, modename_re)) {
123                 if (sm[2].matched) {
124                         bool use_id = sm[1].length() == 1;
125                         unsigned num = stoul(sm[2].str());
127                         if (use_id) {
128                                 Crtc* c = card.get_crtc(num);
129                                 if (!c)
130                                         EXIT("Bad crtc id '%u'", num);
132                                 output.crtc = c;
133                         } else {
134                                 auto crtcs = card.get_crtcs();
136                                 if (num >= crtcs.size())
137                                         EXIT("Bad crtc number '%u'", num);
139                                 output.crtc = crtcs[num];
140                         }
141                 } else {
142                         output.crtc = output.connector->get_current_crtc();
143                 }
145                 unsigned w = stoul(sm[3]);
146                 unsigned h = stoul(sm[4]);
147                 bool ilace = sm[5].matched ? true : false;
148                 float refresh = sm[6].matched ? stof(sm[6]) : 0;
150                 if (s_cvt) {
151                         output.mode = videomode_from_cvt(w, h, refresh, ilace, s_cvt_v2, s_cvt_vid_opt);
152                 } else if (s_use_dmt) {
153                         try {
154                                 output.mode = find_dmt(w, h, refresh, ilace);
155                         } catch (exception& e) {
156                                 EXIT("Mode not found from DMT tables\n");
157                         }
158                 } else if (s_use_cea) {
159                         try {
160                                 output.mode = find_cea(w, h, refresh, ilace);
161                         } catch (exception& e) {
162                                 EXIT("Mode not found from CEA tables\n");
163                         }
164                 } else {
165                         try {
166                                 output.mode = output.connector->get_mode(w, h, refresh, ilace);
167                         } catch (exception& e) {
168                                 EXIT("Mode not found from the connector\n");
169                         }
170                 }
171         } else if (regex_match(crtc_str, sm, modeline_re)) {
172                 if (sm[2].matched) {
173                         bool use_id = sm[1].length() == 1;
174                         unsigned num = stoul(sm[2].str());
176                         if (use_id) {
177                                 Crtc* c = card.get_crtc(num);
178                                 if (!c)
179                                         EXIT("Bad crtc id '%u'", num);
181                                 output.crtc = c;
182                         } else {
183                                 auto crtcs = card.get_crtcs();
185                                 if (num >= crtcs.size())
186                                         EXIT("Bad crtc number '%u'", num);
188                                 output.crtc = crtcs[num];
189                         }
190                 } else {
191                         output.crtc = output.connector->get_current_crtc();
192                 }
194                 unsigned clock = stoul(sm[3]);
196                 unsigned hact = stoul(sm[4]);
197                 unsigned hfp = stoul(sm[5]);
198                 unsigned hsw = stoul(sm[6]);
199                 unsigned hbp = stoul(sm[7]);
200                 bool h_pos_sync = sm[8] == "+" ? true : false;
202                 unsigned vact = stoul(sm[9]);
203                 unsigned vfp = stoul(sm[10]);
204                 unsigned vsw = stoul(sm[11]);
205                 unsigned vbp = stoul(sm[12]);
206                 bool v_pos_sync = sm[13] == "+" ? true : false;
208                 output.mode = videomode_from_timings(clock / 1000, hact, hfp, hsw, hbp, vact, vfp, vsw, vbp);
209                 output.mode.set_hsync(h_pos_sync ? SyncPolarity::Positive : SyncPolarity::Negative);
210                 output.mode.set_vsync(v_pos_sync ? SyncPolarity::Positive : SyncPolarity::Negative);
212                 if (sm[14].matched) {
213                         for (int i = 0; i < sm[14].length(); ++i) {
214                                 char f = string(sm[14])[i];
216                                 switch (f) {
217                                 case 'i':
218                                         output.mode.set_interlace(true);
219                                         break;
220                                 default:
221                                         EXIT("Bad mode flag %c", f);
222                                 }
223                         }
224                 }
225         } else {
226                 EXIT("Failed to parse crtc option '%s'", crtc_str.c_str());
227         }
230 static void parse_plane(Card& card, const string& plane_str, const OutputInfo& output, PlaneInfo& pinfo)
232         // 3:400,400-400x400
233         const regex plane_re("(?:(@?)(\\d+):)?"         // 3:
234                              "(?:(\\d+),(\\d+)-)?"      // 400,400-
235                              "(\\d+)x(\\d+)");          // 400x400
237         smatch sm;
238         if (!regex_match(plane_str, sm, plane_re))
239                 EXIT("Failed to parse plane option '%s'", plane_str.c_str());
241         if (sm[2].matched) {
242                 bool use_id = sm[1].length() == 1;
243                 unsigned num = stoul(sm[2].str());
245                 if (use_id) {
246                         Plane* p = card.get_plane(num);
247                         if (!p)
248                                 EXIT("Bad plane id '%u'", num);
250                         pinfo.plane = p;
251                 } else {
252                         auto planes = card.get_planes();
254                         if (num >= planes.size())
255                                 EXIT("Bad plane number '%u'", num);
257                         pinfo.plane = planes[num];
258                 }
259         } else {
260                 for (Plane* p : output.crtc->get_possible_planes()) {
261                         if (s_used_planes.find(p) != s_used_planes.end())
262                                 continue;
264                         if (p->plane_type() != PlaneType::Overlay)
265                                 continue;
267                         pinfo.plane = p;
268                 }
270                 if (!pinfo.plane)
271                         EXIT("Failed to find available plane");
272         }
274         s_used_planes.insert(pinfo.plane);
276         pinfo.w = stoul(sm[5]);
277         pinfo.h = stoul(sm[6]);
279         if (sm[3].matched)
280                 pinfo.x = stoul(sm[3]);
281         else
282                 pinfo.x = output.mode.hdisplay / 2 - pinfo.w / 2;
284         if (sm[4].matched)
285                 pinfo.y = stoul(sm[4]);
286         else
287                 pinfo.y = output.mode.vdisplay / 2 - pinfo.h / 2;
290 static vector<MappedFramebuffer*> get_default_fb(Card& card, unsigned width, unsigned height)
292         vector<MappedFramebuffer*> v;
294         for (unsigned i = 0; i < s_num_buffers; ++i)
295                 v.push_back(new DumbFramebuffer(card, width, height, PixelFormat::XRGB8888));
297         return v;
300 static vector<MappedFramebuffer*> parse_fb(Card& card, const string& fb_str, unsigned def_w, unsigned def_h)
302         unsigned w = def_w;
303         unsigned h = def_h;
304         PixelFormat format = PixelFormat::XRGB8888;
306         if (!fb_str.empty()) {
307                 // XXX the regexp is not quite correct
308                 // 400x400-NV12
309                 const regex fb_re("(?:(\\d+)x(\\d+))?"          // 400x400
310                                   "(?:-)?"                      // -
311                                   "(\\w\\w\\w\\w)?");           // NV12
313                 smatch sm;
314                 if (!regex_match(fb_str, sm, fb_re))
315                         EXIT("Failed to parse fb option '%s'", fb_str.c_str());
317                 if (sm[1].matched)
318                         w = stoul(sm[1]);
319                 if (sm[2].matched)
320                         h = stoul(sm[2]);
321                 if (sm[3].matched)
322                         format = FourCCToPixelFormat(sm[3]);
323         }
325         vector<MappedFramebuffer*> v;
327         for (unsigned i = 0; i < s_num_buffers; ++i)
328                 v.push_back(new DumbFramebuffer(card, w, h, format));
330         return v;
333 static void parse_view(const string& view_str, PlaneInfo& pinfo)
335         const regex view_re("(\\d+),(\\d+)-(\\d+)x(\\d+)");             // 400,400-400x400
337         smatch sm;
338         if (!regex_match(view_str, sm, view_re))
339                 EXIT("Failed to parse view option '%s'", view_str.c_str());
341         pinfo.view_x = stoul(sm[1]);
342         pinfo.view_y = stoul(sm[2]);
343         pinfo.view_w = stoul(sm[3]);
344         pinfo.view_h = stoul(sm[4]);
347 static const char* usage_str =
348                 "Usage: kmstest [OPTION]...\n\n"
349                 "Show a test pattern on a display or plane\n\n"
350                 "Options:\n"
351                 "      --device=DEVICE       DEVICE is the path to DRM card to open\n"
352                 "  -c, --connector=CONN      CONN is <connector>\n"
353                 "  -r, --crtc=CRTC           CRTC is [<crtc>:]<w>x<h>[@<Hz>]\n"
354                 "                            or\n"
355                 "                            [<crtc>:]<pclk>,<hact>/<hfp>/<hsw>/<hbp>/<hsp>,<vact>/<vfp>/<vsw>/<vbp>/<vsp>[,i]\n"
356                 "  -p, --plane=PLANE         PLANE is [<plane>:][<x>,<y>-]<w>x<h>\n"
357                 "  -f, --fb=FB               FB is [<w>x<h>][-][<4cc>]\n"
358                 "  -v, --view=VIEW           VIEW is <x>,<y>-<w>x<h>\n"
359                 "      --dmt                 Search for the given mode from DMT tables\n"
360                 "      --cea                 Search for the given mode from CEA tables\n"
361                 "      --cvt=CVT             Create videomode with CVT. CVT is 'v1', 'v2' or 'v2o'\n"
362                 "      --flip                Do page flipping for each output\n"
363                 "      --sync                Synchronize page flipping\n"
364                 "\n"
365                 "<connector>, <crtc> and <plane> can be given by index (<idx>) or id (<id>).\n"
366                 "<connector> can also be given by name.\n"
367                 "\n"
368                 "Options can be given multiple times to set up multiple displays or planes.\n"
369                 "Options may apply to previous options, e.g. a plane will be set on a crtc set in\n"
370                 "an earlier option.\n"
371                 "If you omit parameters, kmstest tries to guess what you mean\n"
372                 "\n"
373                 "Examples:\n"
374                 "\n"
375                 "Set eDP-1 mode to 1920x1080@60, show XR24 framebuffer on the crtc, and a 400x400 XB24 plane:\n"
376                 "    kmstest -c eDP-1 -r 1920x1080@60 -f XR24 -p 400x400 -f XB24\n\n"
377                 "XR24 framebuffer on first connected connector in the default mode:\n"
378                 "    kmstest -f XR24\n\n"
379                 "XR24 framebuffer on a 400x400 plane on the first connected connector in the default mode:\n"
380                 "    kmstest -p 400x400 -f XR24\n\n"
381                 "Test pattern on the second connector with default mode:\n"
382                 "    kmstest -c 1\n"
383                 ;
385 static void usage()
387         puts(usage_str);
390 enum class ObjectType
392         Connector,
393         Crtc,
394         Plane,
395         Framebuffer,
396         View,
397 };
399 struct Arg
401         ObjectType type;
402         string arg;
403 };
405 static string s_device_path = "/dev/dri/card0";
407 static vector<Arg> parse_cmdline(int argc, char **argv)
409         vector<Arg> args;
411         OptionSet optionset = {
412                 Option("|device=",
413                 [&](string s)
414                 {
415                         s_device_path = s;
416                 }),
417                 Option("c|connector=",
418                 [&](string s)
419                 {
420                         args.push_back(Arg { ObjectType::Connector, s });
421                 }),
422                 Option("r|crtc=", [&](string s)
423                 {
424                         args.push_back(Arg { ObjectType::Crtc, s });
425                 }),
426                 Option("p|plane=", [&](string s)
427                 {
428                         args.push_back(Arg { ObjectType::Plane, s });
429                 }),
430                 Option("f|fb=", [&](string s)
431                 {
432                         args.push_back(Arg { ObjectType::Framebuffer, s });
433                 }),
434                 Option("v|view=", [&](string s)
435                 {
436                         args.push_back(Arg { ObjectType::View, s });
437                 }),
438                 Option("|dmt", []()
439                 {
440                         s_use_dmt = true;
441                 }),
442                 Option("|cea", []()
443                 {
444                         s_use_cea = true;
445                 }),
446                 Option("|flip", []()
447                 {
448                         s_flip_mode = true;
449                         s_num_buffers = 2;
450                 }),
451                 Option("|sync", []()
452                 {
453                         s_flip_sync = true;
454                 }),
455                 Option("|cvt=", [&](string s)
456                 {
457                         if (s == "v1")
458                                 s_cvt = true;
459                         else if (s == "v2")
460                                 s_cvt = s_cvt_v2 = true;
461                         else if (s == "v2o")
462                                 s_cvt = s_cvt_v2 = s_cvt_vid_opt = true;
463                         else {
464                                 usage();
465                                 exit(-1);
466                         }
467                 }),
468                 Option("h|help", [&]()
469                 {
470                         usage();
471                         exit(-1);
472                 }),
473         };
475         optionset.parse(argc, argv);
477         if (optionset.params().size() > 0) {
478                 usage();
479                 exit(-1);
480         }
482         return args;
485 static vector<OutputInfo> setups_to_outputs(Card& card, ResourceManager& resman, const vector<Arg>& output_args)
487         vector<OutputInfo> outputs;
489         if (output_args.size() == 0) {
490                 // no output args, show a pattern on all screens
491                 for (Connector* conn : card.get_connectors()) {
492                         if (!conn->connected())
493                                 continue;
495                         OutputInfo output = { };
496                         output.connector = resman.reserve_connector(conn);
497                         output.crtc = resman.reserve_crtc(conn);
498                         output.mode = output.connector->get_default_mode();
500                         output.fbs = get_default_fb(card, output.mode.hdisplay, output.mode.vdisplay);
502                         outputs.push_back(output);
503                 }
505                 return outputs;
506         }
508         OutputInfo* current_output = 0;
509         PlaneInfo* current_plane = 0;
511         for (auto& arg : output_args) {
512                 switch (arg.type) {
513                 case ObjectType::Connector:
514                 {
515                         outputs.push_back(OutputInfo { });
516                         current_output = &outputs.back();
518                         get_connector(resman, *current_output, arg.arg);
519                         current_plane = 0;
521                         break;
522                 }
524                 case ObjectType::Crtc:
525                 {
526                         if (!current_output) {
527                                 outputs.push_back(OutputInfo { });
528                                 current_output = &outputs.back();
529                         }
531                         if (!current_output->connector)
532                                 get_connector(resman, *current_output);
534                         parse_crtc(card, arg.arg, *current_output);
536                         current_output->user_set_crtc = true;
538                         current_plane = 0;
540                         break;
541                 }
543                 case ObjectType::Plane:
544                 {
545                         if (!current_output) {
546                                 outputs.push_back(OutputInfo { });
547                                 current_output = &outputs.back();
548                         }
550                         if (!current_output->connector)
551                                 get_connector(resman, *current_output);
553                         if (!current_output->crtc)
554                                 get_default_crtc(card, *current_output);
556                         current_output->planes.push_back(PlaneInfo { });
557                         current_plane = &current_output->planes.back();
559                         parse_plane(card, arg.arg, *current_output, *current_plane);
561                         break;
562                 }
564                 case ObjectType::Framebuffer:
565                 {
566                         if (!current_output) {
567                                 outputs.push_back(OutputInfo { });
568                                 current_output = &outputs.back();
569                         }
571                         if (!current_output->connector)
572                                 get_connector(resman, *current_output);
574                         if (!current_output->crtc)
575                                 get_default_crtc(card, *current_output);
577                         int def_w, def_h;
579                         if (current_plane) {
580                                 def_w = current_plane->w;
581                                 def_h = current_plane->h;
582                         } else {
583                                 def_w = current_output->mode.hdisplay;
584                                 def_h = current_output->mode.vdisplay;
585                         }
587                         auto fbs = parse_fb(card, arg.arg, def_w, def_h);
589                         if (current_plane)
590                                 current_plane->fbs = fbs;
591                         else
592                                 current_output->fbs = fbs;
594                         break;
595                 }
597                 case ObjectType::View:
598                 {
599                         if (!current_plane || current_plane->fbs.empty())
600                                 EXIT("'view' parameter requires a plane and a fb");
602                         parse_view(arg.arg, *current_plane);
603                         break;
604                 }
605                 }
606         }
608         // create default framebuffers if needed
609         for (OutputInfo& o : outputs) {
610                 if (!o.crtc) {
611                         get_default_crtc(card, o);
612                         o.user_set_crtc = true;
613                 }
615                 if (o.fbs.empty() && o.user_set_crtc)
616                         o.fbs = get_default_fb(card, o.mode.hdisplay, o.mode.vdisplay);
618                 for (PlaneInfo &p : o.planes) {
619                         if (p.fbs.empty())
620                                 p.fbs = get_default_fb(card, p.w, p.h);
621                 }
622         }
624         return outputs;
627 static std::string videomode_to_string(const Videomode& m)
629         string h = sformat("%u/%u/%u/%u", m.hdisplay, m.hfp(), m.hsw(), m.hbp());
630         string v = sformat("%u/%u/%u/%u", m.vdisplay, m.vfp(), m.vsw(), m.vbp());
632         return sformat("%s %.3f %s %s %u (%.2f) %#x %#x",
633                        m.name.c_str(),
634                        m.clock / 1000.0,
635                        h.c_str(), v.c_str(),
636                        m.vrefresh, m.calculated_vrefresh(),
637                        m.flags,
638                        m.type);
641 static void print_outputs(const vector<OutputInfo>& outputs)
643         for (unsigned i = 0; i < outputs.size(); ++i) {
644                 const OutputInfo& o = outputs[i];
646                 printf("Connector %u/@%u: %s\n", o.connector->idx(), o.connector->id(),
647                        o.connector->fullname().c_str());
648                 printf("  Crtc %u/@%u", o.crtc->idx(), o.crtc->id());
649                 if (o.primary_plane)
650                         printf(" (plane %u/@%u)", o.primary_plane->idx(), o.primary_plane->id());
651                 printf(": %s\n", videomode_to_string(o.mode).c_str());
652                 if (!o.fbs.empty()) {
653                         auto fb = o.fbs[0];
654                         printf("    Fb %u %ux%u-%s\n", fb->id(), fb->width(), fb->height(),
655                                PixelFormatToFourCC(fb->format()).c_str());
656                 }
658                 for (unsigned j = 0; j < o.planes.size(); ++j) {
659                         const PlaneInfo& p = o.planes[j];
660                         auto fb = p.fbs[0];
661                         printf("  Plane %u/@%u: %u,%u-%ux%u\n", p.plane->idx(), p.plane->id(),
662                                p.x, p.y, p.w, p.h);
663                         printf("    Fb %u %ux%u-%s\n", fb->id(), fb->width(), fb->height(),
664                                PixelFormatToFourCC(fb->format()).c_str());
665                 }
666         }
669 static void draw_test_patterns(const vector<OutputInfo>& outputs)
671         for (const OutputInfo& o : outputs) {
672                 for (auto fb : o.fbs)
673                         draw_test_pattern(*fb);
675                 for (const PlaneInfo& p : o.planes)
676                         for (auto fb : p.fbs)
677                                 draw_test_pattern(*fb);
678         }
681 static void set_crtcs_n_planes_legacy(Card& card, const vector<OutputInfo>& outputs)
683         // Disable unused crtcs
684         for (Crtc* crtc : card.get_crtcs()) {
685                 if (find_if(outputs.begin(), outputs.end(), [crtc](const OutputInfo& o) { return o.crtc == crtc; }) != outputs.end())
686                         continue;
688                 crtc->disable_mode();
689         }
691         for (const OutputInfo& o : outputs) {
692                 auto conn = o.connector;
693                 auto crtc = o.crtc;
695                 if (!o.fbs.empty()) {
696                         auto fb = o.fbs[0];
697                         int r = crtc->set_mode(conn, *fb, o.mode);
698                         if (r)
699                                 printf("crtc->set_mode() failed for crtc %u: %s\n",
700                                        crtc->id(), strerror(-r));
701                 }
703                 for (const PlaneInfo& p : o.planes) {
704                         auto fb = p.fbs[0];
705                         int r = crtc->set_plane(p.plane, *fb,
706                                                 p.x, p.y, p.w, p.h,
707                                                 0, 0, fb->width(), fb->height());
708                         if (r)
709                                 printf("crtc->set_plane() failed for plane %u: %s\n",
710                                        p.plane->id(), strerror(-r));
711                 }
712         }
715 static void set_crtcs_n_planes_atomic(Card& card, const vector<OutputInfo>& outputs)
717         int r;
719         // XXX DRM framework doesn't allow moving an active plane from one crtc to another.
720         // See drm_atomic.c::plane_switching_crtc().
721         // For the time being, disable all crtcs and planes here.
723         AtomicReq disable_req(card);
725         // Disable unused crtcs
726         for (Crtc* crtc : card.get_crtcs()) {
727                 //if (find_if(outputs.begin(), outputs.end(), [crtc](const OutputInfo& o) { return o.crtc == crtc; }) != outputs.end())
728                 //      continue;
730                 disable_req.add(crtc, {
731                                 { "ACTIVE", 0 },
732                         });
733         }
735         // Disable unused planes
736         for (Plane* plane : card.get_planes()) {
737                 //if (find_if(outputs.begin(), outputs.end(), [plane](const OutputInfo& o) { return o.primary_plane == plane; }) != outputs.end())
738                 //      continue;
740                 disable_req.add(plane, {
741                                 { "FB_ID", 0 },
742                                 { "CRTC_ID", 0 },
743                         });
744         }
746         r = disable_req.commit_sync(true);
747         if (r)
748                 EXIT("Atomic commit failed when disabling: %d\n", r);
751         // Keep blobs here so that we keep ref to them until we have committed the req
752         vector<unique_ptr<Blob>> blobs;
754         AtomicReq req(card);
756         for (const OutputInfo& o : outputs) {
757                 auto conn = o.connector;
758                 auto crtc = o.crtc;
760                 blobs.emplace_back(o.mode.to_blob(card));
761                 Blob* mode_blob = blobs.back().get();
763                 req.add(conn, {
764                                 { "CRTC_ID", crtc->id() },
765                         });
767                 req.add(crtc, {
768                                 { "ACTIVE", 1 },
769                                 { "MODE_ID", mode_blob->id() },
770                         });
772                 if (!o.fbs.empty()) {
773                         auto fb = o.fbs[0];
775                         req.add(o.primary_plane, {
776                                         { "FB_ID", fb->id() },
777                                         { "CRTC_ID", crtc->id() },
778                                         { "SRC_X", 0 << 16 },
779                                         { "SRC_Y", 0 << 16 },
780                                         { "SRC_W", fb->width() << 16 },
781                                         { "SRC_H", fb->height() << 16 },
782                                         { "CRTC_X", 0 },
783                                         { "CRTC_Y", 0 },
784                                         { "CRTC_W", fb->width() },
785                                         { "CRTC_H", fb->height() },
786                                 });
787                 }
789                 for (const PlaneInfo& p : o.planes) {
790                         auto fb = p.fbs[0];
792                         req.add(p.plane, {
793                                         { "FB_ID", fb->id() },
794                                         { "CRTC_ID", crtc->id() },
795                                         { "SRC_X", (p.view_x ?: 0) << 16 },
796                                         { "SRC_Y", (p.view_y ?: 0) << 16 },
797                                         { "SRC_W", (p.view_w ?: fb->width()) << 16 },
798                                         { "SRC_H", (p.view_h ?: fb->height()) << 16 },
799                                         { "CRTC_X", p.x },
800                                         { "CRTC_Y", p.y },
801                                         { "CRTC_W", p.w },
802                                         { "CRTC_H", p.h },
803                                 });
804                 }
805         }
807         r = req.test(true);
808         if (r)
809                 EXIT("Atomic test failed: %d\n", r);
811         r = req.commit_sync(true);
812         if (r)
813                 EXIT("Atomic commit failed: %d\n", r);
816 static void set_crtcs_n_planes(Card& card, const vector<OutputInfo>& outputs)
818         if (card.has_atomic())
819                 set_crtcs_n_planes_atomic(card, outputs);
820         else
821                 set_crtcs_n_planes_legacy(card, outputs);
824 class FlipState : private PageFlipHandlerBase
826 public:
827         FlipState(Card& card, const string& name, vector<const OutputInfo*> outputs)
828                 : m_card(card), m_name(name), m_outputs(outputs)
829         {
830         }
832         void start_flipping()
833         {
834                 m_prev_frame = m_prev_print = std::chrono::steady_clock::now();
835                 m_slowest_frame = std::chrono::duration<float>::min();
836                 m_frame_num = 0;
837                 queue_next();
838         }
840 private:
841         void handle_page_flip(uint32_t frame, double time)
842         {
843                 m_frame_num++;
845                 auto now = std::chrono::steady_clock::now();
847                 std::chrono::duration<float> diff = now - m_prev_frame;
848                 if (diff > m_slowest_frame)
849                         m_slowest_frame = diff;
851                 if (m_frame_num  % 100 == 0) {
852                         std::chrono::duration<float> fsec = now - m_prev_print;
853                         printf("Connector %s: fps %f, slowest %.2f ms\n",
854                                m_name.c_str(),
855                                100.0 / fsec.count(),
856                                m_slowest_frame.count() * 1000);
857                         m_prev_print = now;
858                         m_slowest_frame = std::chrono::duration<float>::min();
859                 }
861                 m_prev_frame = now;
863                 queue_next();
864         }
866         static unsigned get_bar_pos(MappedFramebuffer* fb, unsigned frame_num)
867         {
868                 return (frame_num * bar_speed) % (fb->width() - bar_width + 1);
869         }
871         static void draw_bar(MappedFramebuffer* fb, unsigned frame_num)
872         {
873                 int old_xpos = frame_num < s_num_buffers ? -1 : get_bar_pos(fb, frame_num - s_num_buffers);
874                 int new_xpos = get_bar_pos(fb, frame_num);
876                 draw_color_bar(*fb, old_xpos, new_xpos, bar_width);
877                 draw_text(*fb, fb->width() / 2, 0, to_string(frame_num), RGB(255, 255, 255));
878         }
880         static void do_flip_output(AtomicReq& req, unsigned frame_num, const OutputInfo& o)
881         {
882                 unsigned cur = frame_num % s_num_buffers;
884                 if (!o.fbs.empty()) {
885                         auto fb = o.fbs[cur];
887                         draw_bar(fb, frame_num);
889                         req.add(o.primary_plane, {
890                                         { "FB_ID", fb->id() },
891                                 });
892                 }
894                 for (const PlaneInfo& p : o.planes) {
895                         auto fb = p.fbs[cur];
897                         draw_bar(fb, frame_num);
899                         req.add(p.plane, {
900                                         { "FB_ID", fb->id() },
901                                 });
902                 }
903         }
905         void do_flip_output_legacy(unsigned frame_num, const OutputInfo& o)
906         {
907                 unsigned cur = frame_num % s_num_buffers;
909                 if (!o.fbs.empty()) {
910                         auto fb = o.fbs[cur];
912                         draw_bar(fb, frame_num);
914                         int r = o.crtc->page_flip(*fb, this);
915                         ASSERT(r == 0);
916                 }
918                 for (const PlaneInfo& p : o.planes) {
919                         auto fb = p.fbs[cur];
921                         draw_bar(fb, frame_num);
923                         int r = o.crtc->set_plane(p.plane, *fb,
924                                                   p.x, p.y, p.w, p.h,
925                                                   0, 0, fb->width(), fb->height());
926                         ASSERT(r == 0);
927                 }
928         }
930         void queue_next()
931         {
932                 if (m_card.has_atomic()) {
933                         AtomicReq req(m_card);
935                         for (auto o : m_outputs)
936                                 do_flip_output(req, m_frame_num, *o);
938                         int r = req.commit(this);
939                         if (r)
940                                 EXIT("Flip commit failed: %d\n", r);
941                 } else {
942                         ASSERT(m_outputs.size() == 1);
943                         do_flip_output_legacy(m_frame_num, *m_outputs[0]);
944                 }
945         }
947         Card& m_card;
948         string m_name;
949         vector<const OutputInfo*> m_outputs;
950         unsigned m_frame_num;
952         chrono::steady_clock::time_point m_prev_print;
953         chrono::steady_clock::time_point m_prev_frame;
954         chrono::duration<float> m_slowest_frame;
956         static const unsigned bar_width = 20;
957         static const unsigned bar_speed = 8;
958 };
960 static void main_flip(Card& card, const vector<OutputInfo>& outputs)
962         fd_set fds;
964         FD_ZERO(&fds);
966         int fd = card.fd();
968         vector<unique_ptr<FlipState>> flipstates;
970         if (!s_flip_sync) {
971                 for (const OutputInfo& o : outputs) {
972                         auto fs = unique_ptr<FlipState>(new FlipState(card, to_string(o.connector->idx()), { &o }));
973                         flipstates.push_back(move(fs));
974                 }
975         } else {
976                 vector<const OutputInfo*> ois;
978                 string name;
979                 for (const OutputInfo& o : outputs) {
980                         name += to_string(o.connector->idx()) + ",";
981                         ois.push_back(&o);
982                 }
984                 auto fs = unique_ptr<FlipState>(new FlipState(card, name, ois));
985                 flipstates.push_back(move(fs));
986         }
988         for (unique_ptr<FlipState>& fs : flipstates)
989                 fs->start_flipping();
991         while (true) {
992                 int r;
994                 FD_SET(0, &fds);
995                 FD_SET(fd, &fds);
997                 r = select(fd + 1, &fds, NULL, NULL, NULL);
998                 if (r < 0) {
999                         fprintf(stderr, "select() failed with %d: %m\n", errno);
1000                         break;
1001                 } else if (FD_ISSET(0, &fds)) {
1002                         fprintf(stderr, "Exit due to user-input\n");
1003                         break;
1004                 } else if (FD_ISSET(fd, &fds)) {
1005                         card.call_page_flip_handlers();
1006                 }
1007         }
1010 int main(int argc, char **argv)
1012         vector<Arg> output_args = parse_cmdline(argc, argv);
1014         Card card(s_device_path);
1016         if (!card.has_atomic() && s_flip_sync)
1017                 EXIT("Synchronized flipping requires atomic modesetting");
1019         ResourceManager resman(card);
1021         vector<OutputInfo> outputs = setups_to_outputs(card, resman, output_args);
1023         if (card.has_atomic()) {
1024                 for (OutputInfo& o : outputs) {
1025                         if (o.fbs.empty())
1026                                 continue;
1028                         o.primary_plane = resman.reserve_primary_plane(o.crtc);
1030                         if (!o.primary_plane)
1031                                 EXIT("Could not get primary plane for crtc '%u'", o.crtc->id());
1032                 }
1033         }
1035         if (!s_flip_mode)
1036                 draw_test_patterns(outputs);
1038         print_outputs(outputs);
1040         set_crtcs_n_planes(card, outputs);
1042         printf("press enter to exit\n");
1044         if (s_flip_mode)
1045                 main_flip(card, outputs);
1046         else
1047                 getchar();