436cdeeba584fdc512c39ea43f7f266946d02f47
[android/external-libkmsxx.git] / libkms++ / crtc.cpp
1 #include <stdio.h>
2 #include <iostream>
3 #include <unistd.h>
4 #include <fcntl.h>
5 #include <cassert>
7 #include "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 static inline uint32_t conv(float x)
64 {
65         // XXX fix the conversion for fractional part
66         return ((uint32_t)x) << 16;
67 }
69 int Crtc::set_plane(Plane* plane, Framebuffer& fb,
70                     int32_t dst_x, int32_t dst_y, uint32_t dst_w, uint32_t dst_h,
71                     float src_x, float src_y, float src_w, float src_h)
72 {
73         return drmModeSetPlane(card().fd(), plane->id(), id(), fb.id(), 0,
74                                dst_x, dst_y, dst_w, dst_h,
75                                conv(src_x), conv(src_y), conv(src_w), conv(src_h));
76 }
78 int Crtc::disable_plane(Plane* plane)
79 {
80         return drmModeSetPlane(card().fd(), plane->id(), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
81 }
83 Plane* Crtc::get_primary_plane()
84 {
85         for (Plane* p : get_possible_planes()) {
86                 if (p->plane_type() != PlaneType::Primary)
87                         continue;
89                 if (p->crtc_id() != id())
90                         continue;
92                 return p;
93         }
95         throw invalid_argument(string("No primary plane for crtc ") + to_string(id()));
96 }
98 int Crtc::page_flip(Framebuffer& fb, void *data)
99 {
100         return drmModePageFlip(card().fd(), id(), fb.id(), DRM_MODE_PAGE_FLIP_EVENT, data);
103 uint32_t Crtc::buffer_id() const
105         return m_priv->drm_crtc->buffer_id;
108 uint32_t Crtc::x() const
110         return m_priv->drm_crtc->x;
113 uint32_t Crtc::y() const
115         return m_priv->drm_crtc->y;
118 uint32_t Crtc::width() const
120         return m_priv->drm_crtc->width;
123 uint32_t Crtc::height() const
125         return m_priv->drm_crtc->height;
128 int Crtc::mode_valid() const
130         return m_priv->drm_crtc->mode_valid;
133 Videomode Crtc::mode() const
135         return drm_mode_to_video_mode(m_priv->drm_crtc->mode);
138 int Crtc::gamma_size() const
140         return m_priv->drm_crtc->gamma_size;