kmscapture: rename
[android/external-libkmsxx.git] / tests / kmscapture.cpp
index d433c44830e081de017cebbb0d42b9016371e01a..8bd1fa9e276d8210b23a162b00db410dd6ef2506 100644 (file)
@@ -18,9 +18,9 @@
 using namespace std;
 using namespace kms;
 
-enum buffer_provider {
-       BUFFER_DRM = 0,
-       BUFFER_V4L,
+enum class BufferProvider {
+       DRM,
+       V4L2,
 };
 
 class Camera
@@ -28,7 +28,7 @@ class Camera
 public:
        Camera(int camera_id, Card& card, Plane* plane, uint32_t x, uint32_t y,
               uint32_t iw, uint32_t ih, PixelFormat pixfmt,
-              enum buffer_provider buffer_type);
+              BufferProvider buffer_provider);
        ~Camera();
 
        Camera(const Camera& other) = delete;
@@ -40,7 +40,7 @@ private:
        ExtFramebuffer* GetExtFrameBuffer(Card& card, int i, PixelFormat pixfmt);
        int m_fd;       /* camera file descriptor */
        Plane* m_plane;
-       enum buffer_provider m_buffer_type;
+       BufferProvider m_buffer_provider;
        vector<DumbFramebuffer*> m_fb; /* framebuffers for DRM buffers */
        vector<ExtFramebuffer*> m_extfb; /* framebuffers for V4L2 buffers */
        int m_prev_fb_index;
@@ -102,7 +102,7 @@ bool inline better_size(struct v4l2_frmsize_discrete* v4ldisc,
 
 Camera::Camera(int camera_id, Card& card, Plane* plane, uint32_t x, uint32_t y,
               uint32_t iw, uint32_t ih, PixelFormat pixfmt,
-              enum buffer_provider buffer_type)
+              BufferProvider buffer_provider)
 {
        char dev_name[20];
        int r, i;
@@ -110,8 +110,8 @@ Camera::Camera(int camera_id, Card& card, Plane* plane, uint32_t x, uint32_t y,
        uint32_t best_h = 240;
        uint32_t v4l_mem;
 
-       m_buffer_type = buffer_type;
-       if (m_buffer_type == BUFFER_V4L)
+       m_buffer_provider = buffer_provider;
+       if (m_buffer_provider == BufferProvider::V4L2)
                v4l_mem = V4L2_MEMORY_MMAP;
        else
                v4l_mem = V4L2_MEMORY_DMABUF;
@@ -121,7 +121,7 @@ Camera::Camera(int camera_id, Card& card, Plane* plane, uint32_t x, uint32_t y,
 
        ASSERT(m_fd >= 0);
 
-       struct v4l2_frmsizeenum v4lfrms = { };
+       struct v4l2_frmsizeenum v4lfrms = { };
        v4lfrms.pixel_format = (uint32_t) pixfmt;
        while (ioctl(m_fd, VIDIOC_ENUM_FRAMESIZES, &v4lfrms) == 0) {
                if (v4lfrms.type == V4L2_FRMSIZE_TYPE_DISCRETE) {
@@ -142,7 +142,7 @@ Camera::Camera(int camera_id, Card& card, Plane* plane, uint32_t x, uint32_t y,
        m_out_x = x + iw / 2 - m_out_width / 2;
        m_out_y = y + ih / 2 - m_out_height / 2;
 
-       struct v4l2_format v4lfmt = { };
+       struct v4l2_format v4lfmt = { };
        v4lfmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
        r = ioctl(m_fd, VIDIOC_G_FMT, &v4lfmt);
        ASSERT(r == 0);
@@ -154,7 +154,7 @@ Camera::Camera(int camera_id, Card& card, Plane* plane, uint32_t x, uint32_t y,
        r = ioctl(m_fd, VIDIOC_S_FMT, &v4lfmt);
        ASSERT(r == 0);
 
-       struct v4l2_requestbuffers v4lreqbuf = { };
+       struct v4l2_requestbuffers v4lreqbuf = { };
        v4lreqbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
        v4lreqbuf.memory = v4l_mem;
        v4lreqbuf.count = CAMERA_BUF_QUEUE_SIZE;
@@ -162,7 +162,7 @@ Camera::Camera(int camera_id, Card& card, Plane* plane, uint32_t x, uint32_t y,
        ASSERT(r == 0);
        ASSERT(v4lreqbuf.count == CAMERA_BUF_QUEUE_SIZE);
 
-       struct v4l2_buffer v4lbuf = { };
+       struct v4l2_buffer v4lbuf = { };
        v4lbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
        v4lbuf.memory = v4l_mem;
 
@@ -170,19 +170,19 @@ Camera::Camera(int camera_id, Card& card, Plane* plane, uint32_t x, uint32_t y,
                DumbFramebuffer *fb = NULL;
                ExtFramebuffer *extfb = NULL;
 
-               if (m_buffer_type == BUFFER_V4L)
+               if (m_buffer_provider == BufferProvider::V4L2)
                        extfb = GetExtFrameBuffer(card, i, pixfmt);
                else
                        fb = new DumbFramebuffer(card, m_in_width,
                                                 m_in_height, pixfmt);
 
                v4lbuf.index = i;
-               if (m_buffer_type == BUFFER_DRM)
+               if (m_buffer_provider == BufferProvider::DRM)
                        v4lbuf.m.fd = fb->prime_fd(0);
                r = ioctl(m_fd, VIDIOC_QBUF, &v4lbuf);
                ASSERT(r == 0);
 
-               if (m_buffer_type == BUFFER_V4L)
+               if (m_buffer_provider == BufferProvider::V4L2)
                        m_extfb.push_back(extfb);
                else
                        m_fb.push_back(fb);
@@ -214,12 +214,12 @@ void Camera::show_next_frame(Crtc* crtc)
        int fb_index;
        uint32_t v4l_mem;
 
-       if (m_buffer_type == BUFFER_V4L)
+       if (m_buffer_provider == BufferProvider::V4L2)
                v4l_mem = V4L2_MEMORY_MMAP;
        else
                v4l_mem = V4L2_MEMORY_DMABUF;
 
-       struct v4l2_buffer v4l2buf = { };
+       struct v4l2_buffer v4l2buf = { };
        v4l2buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
        v4l2buf.memory = v4l_mem;
        r = ioctl(m_fd, VIDIOC_DQBUF, &v4l2buf);
@@ -229,7 +229,7 @@ void Camera::show_next_frame(Crtc* crtc)
        }
 
        fb_index = v4l2buf.index;
-       if (m_buffer_type == BUFFER_V4L)
+       if (m_buffer_provider == BufferProvider::V4L2)
                r = crtc->set_plane(m_plane, *m_extfb[fb_index],
                                    m_out_x, m_out_y, m_out_width, m_out_height,
                                    0, 0, m_in_width, m_in_height);
@@ -245,7 +245,7 @@ void Camera::show_next_frame(Crtc* crtc)
                v4l2buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
                v4l2buf.memory = v4l_mem;
                v4l2buf.index = m_prev_fb_index;
-               if (m_buffer_type == BUFFER_DRM)
+               if (m_buffer_provider == BufferProvider::DRM)
                        v4l2buf.m.fd = m_fb[m_prev_fb_index]->prime_fd(0);
                r = ioctl(m_fd, VIDIOC_QBUF, &v4l2buf);
                ASSERT(r == 0);
@@ -257,7 +257,7 @@ void Camera::show_next_frame(Crtc* crtc)
 
 static bool is_capture_dev(int fd)
 {
-       struct v4l2_capability cap = { };
+       struct v4l2_capability cap = { };
        int r;
 
        r = ioctl(fd, VIDIOC_QUERYCAP, &cap);
@@ -298,7 +298,7 @@ static const char* usage_str =
 int main(int argc, char** argv)
 {
        uint32_t w;
-       enum buffer_provider buffer_type = BUFFER_DRM;
+       BufferProvider buffer_provider = BufferProvider::DRM;
        int i;
 
        auto camera_idx = count_cameras();
@@ -314,7 +314,7 @@ int main(int argc, char** argv)
                Option("|buffer-type=", [&](string s)
                {
                        if (!s.compare("v4l"))
-                               buffer_type = BUFFER_V4L;
+                               buffer_provider = BufferProvider::V4L2;
                        else if (s.compare("drm"))
                                printf("invalid buffer-type: %s\n", s.c_str());
                }),
@@ -340,7 +340,7 @@ int main(int argc, char** argv)
        auto crtc = conn->get_current_crtc();
        printf("Display: %dx%d\n", crtc->width(), crtc->height());
        printf("Buffer provider: %s\n",
-              buffer_type == BUFFER_V4L? "V4L" : "DRM");
+              buffer_provider == BufferProvider::V4L2? "V4L" : "DRM");
 
        w = crtc->width() / nr_cameras;
        vector<Camera*> cameras;
@@ -353,7 +353,7 @@ int main(int argc, char** argv)
                        continue;
 
                auto cam = new Camera(camera_idx[i], card, p, i * w, 0,
-                                     w, crtc->height(), pixfmt, buffer_type);
+                                     w, crtc->height(), pixfmt, buffer_provider);
                cameras.push_back(cam);
                if (++i == nr_cameras)
                        break;
@@ -361,7 +361,8 @@ int main(int argc, char** argv)
 
        FAIL_IF(i < nr_cameras, "available plane not found");
 
-       struct pollfd fds[nr_cameras + 1] = { 0 };
+       vector<pollfd> fds(nr_cameras + 1);
+
        for (i = 0; i < nr_cameras; i++) {
                fds[i].fd = cameras[i]->fd();
                fds[i].events =  POLLIN;
@@ -370,7 +371,7 @@ int main(int argc, char** argv)
        fds[nr_cameras].events =  POLLIN;
 
        while (true) {
-               int r = poll(fds, nr_cameras + 1, -1);
+               int r = poll(fds.data(), nr_cameras + 1, -1);
                ASSERT(r > 0);
 
                if (fds[nr_cameras].revents != 0)