]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - android/external-libkmsxx.git/blob - utils/kmsview.cpp
b503f0a0ecfc6fe91ae9533712993fda07952502
[android/external-libkmsxx.git] / utils / kmsview.cpp
1 #include <cstdio>
2 #include <fstream>
3 #include <unistd.h>
5 #include <kms++/kms++.h>
6 #include <kms++util/kms++util.h>
8 using namespace std;
9 using namespace kms;
11 static void read_frame(ifstream& is, DumbFramebuffer* fb, Crtc* crtc, Plane* plane)
12 {
13         for (unsigned i = 0; i < fb->num_planes(); ++i)
14                 is.read((char*)fb->map(i), fb->size(i));
16         unsigned w = min(crtc->width(), fb->width());
17         unsigned h = min(crtc->height(), fb->height());
19         int r = crtc->set_plane(plane, *fb,
20                                 0, 0, w, h,
21                                 0, 0, fb->width(), fb->height());
23         ASSERT(r == 0);
24 }
26 static const char* usage_str =
27                 "Usage: kmsview [-t <ms>] <file> <width> <height> <fourcc>\n\n"
28                 "Options:\n"
29                 "  -t, --time        Milliseconds to sleep between frames\n"
30                 ;
32 static void usage()
33 {
34         puts(usage_str);
35 }
37 int main(int argc, char** argv)
38 {
39         uint32_t time = 0;
40         string dev_path = "/dev/dri/card0";
42         OptionSet optionset = {
43                 Option("|device=", [&dev_path](string s)
44                 {
45                         dev_path = s;
46                 }),
47                 Option("t|time=", [&time](const string& str)
48                 {
49                         time = stoul(str);
50                 }),
51                 Option("h|help", []()
52                 {
53                         usage();
54                         exit(-1);
55                 }),
56         };
58         optionset.parse(argc, argv);
60         vector<string> params = optionset.params();
62         if (params.size() != 4) {
63                 usage();
64                 exit(-1);
65         }
67         string filename = params[0];
68         uint32_t w = stoi(params[1]);
69         uint32_t h = stoi(params[2]);
70         string modestr = params[3];
72         auto pixfmt = FourCCToPixelFormat(modestr);
74         ifstream is(filename, ifstream::binary);
76         is.seekg(0, std::ios::end);
77         unsigned fsize = is.tellg();
78         is.seekg(0);
81         Card card(dev_path);
83         auto conn = card.get_first_connected_connector();
84         auto crtc = conn->get_current_crtc();
86         auto fb = new DumbFramebuffer(card, w, h, pixfmt);
88         Plane* plane = 0;
90         for (Plane* p : crtc->get_possible_planes()) {
91                 if (p->plane_type() != PlaneType::Overlay)
92                         continue;
94                 if (!p->supports_format(pixfmt))
95                         continue;
97                 plane = p;
98                 break;
99         }
101         FAIL_IF(!plane, "available plane not found");
104         unsigned frame_size = 0;
105         for (unsigned i = 0; i < fb->num_planes(); ++i)
106                 frame_size += fb->size(i);
108         unsigned num_frames = fsize / frame_size;
109         printf("file size %u, frame size %u, frames %u\n", fsize, frame_size, num_frames);
111         for (unsigned i = 0; i < num_frames; ++i) {
112                 printf("frame %d", i); fflush(stdout);
113                 read_frame(is, fb, crtc, plane);
114                 if (!time) {
115                         getchar();
116                 } else {
117                         usleep(time * 1000);
118                         printf("\n");
119                 }
120         }
122         is.close();
124         if (time) {
125                 printf("press enter to exit\n");
126                 getchar();
127         }
129         delete fb;