4c2f5ce7a2623a8595238f6ccad1fe01f16570dd
[android/external-libkmsxx.git] / kms++ / src / crtc.cpp
1 #include <stdio.h>
2 #include <iostream>
3 #include <unistd.h>
4 #include <fcntl.h>
5 #include <cassert>
7 #include <kms++/kms++.h>
8 #include "helpers.h"
10 using namespace std;
12 namespace kms
13 {
15 struct CrtcPriv
16 {
17         drmModeCrtcPtr drm_crtc;
18 };
20 Crtc::Crtc(Card &card, uint32_t id, uint32_t idx)
21         :DrmPropObject(card, id, DRM_MODE_OBJECT_CRTC, idx)
22 {
23         m_priv = new CrtcPriv();
24         m_priv->drm_crtc = drmModeGetCrtc(this->card().fd(), this->id());
25         assert(m_priv->drm_crtc);
26 }
28 Crtc::~Crtc()
29 {
30         drmModeFreeCrtc(m_priv->drm_crtc);
31         delete m_priv;
32 }
34 void Crtc::setup()
35 {
36         for (Plane* plane : card().get_planes()) {
37                 if (plane->supports_crtc(this))
38                         m_possible_planes.push_back(plane);
39         }
40 }
42 void Crtc::restore_mode(Connector* conn)
43 {
44         auto c = m_priv->drm_crtc;
46         uint32_t conns[] = { conn->id() };
48         drmModeSetCrtc(card().fd(), id(), c->buffer_id,
49                        c->x, c->y,
50                        conns, 1, &c->mode);
51 }
53 int Crtc::set_mode(Connector* conn, Framebuffer& fb, const Videomode& mode)
54 {
55         uint32_t conns[] = { conn->id() };
56         drmModeModeInfo drmmode = video_mode_to_drm_mode(mode);
58         return drmModeSetCrtc(card().fd(), id(), fb.id(),
59                               0, 0,
60                               conns, 1, &drmmode);
61 }
63 int Crtc::disable_mode()
64 {
65         return drmModeSetCrtc(card().fd(), id(), 0, 0, 0, 0, 0, 0);
66 }
68 static inline uint32_t conv(float x)
69 {
70         // XXX fix the conversion for fractional part
71         return ((uint32_t)x) << 16;
72 }
74 int Crtc::set_plane(Plane* plane, Framebuffer& fb,
75                     int32_t dst_x, int32_t dst_y, uint32_t dst_w, uint32_t dst_h,
76                     float src_x, float src_y, float src_w, float src_h)
77 {
78         return drmModeSetPlane(card().fd(), plane->id(), id(), fb.id(), 0,
79                                dst_x, dst_y, dst_w, dst_h,
80                                conv(src_x), conv(src_y), conv(src_w), conv(src_h));
81 }
83 int Crtc::disable_plane(Plane* plane)
84 {
85         return drmModeSetPlane(card().fd(), plane->id(), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
86 }
88 Plane* Crtc::get_primary_plane()
89 {
90         for (Plane* p : get_possible_planes()) {
91                 if (p->plane_type() != PlaneType::Primary)
92                         continue;
94                 return p;
95         }
97         throw invalid_argument(string("No primary plane for crtc ") + to_string(id()));
98 }
100 int Crtc::page_flip(Framebuffer& fb, void *data)
102         return drmModePageFlip(card().fd(), id(), fb.id(), DRM_MODE_PAGE_FLIP_EVENT, data);
105 uint32_t Crtc::buffer_id() const
107         return m_priv->drm_crtc->buffer_id;
110 uint32_t Crtc::x() const
112         return m_priv->drm_crtc->x;
115 uint32_t Crtc::y() const
117         return m_priv->drm_crtc->y;
120 uint32_t Crtc::width() const
122         return m_priv->drm_crtc->width;
125 uint32_t Crtc::height() const
127         return m_priv->drm_crtc->height;
130 int Crtc::mode_valid() const
132         return m_priv->drm_crtc->mode_valid;
135 Videomode Crtc::mode() const
137         return drm_mode_to_video_mode(m_priv->drm_crtc->mode);
140 int Crtc::gamma_size() const
142         return m_priv->drm_crtc->gamma_size;