libkmsxx: Add include of <cerrno.h> to various files
[android/external-libkmsxx.git] / kms++ / src / dumbframebuffer.cpp
2 #include <cstring>
3 #include <cerrno>
4 #include <stdexcept>
5 #include <sys/mman.h>
6 #include <xf86drm.h>
7 #include <xf86drmMode.h>
8 #include <fcntl.h>
9 #include <unistd.h>
10 #include <drm_fourcc.h>
11 #include <drm.h>
12 #include <drm_mode.h>
14 #include <kms++/kms++.h>
16 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
18 using namespace std;
20 namespace kms
21 {
23 DumbFramebuffer::DumbFramebuffer(Card &card, uint32_t width, uint32_t height, const string& fourcc)
24         :DumbFramebuffer(card, width, height, FourCCToPixelFormat(fourcc))
25 {
26 }
28 DumbFramebuffer::DumbFramebuffer(Card& card, uint32_t width, uint32_t height, PixelFormat format)
29         :Framebuffer(card, width, height), m_format(format)
30 {
31         Create();
32 }
34 DumbFramebuffer::~DumbFramebuffer()
35 {
36         Destroy();
37 }
39 void DumbFramebuffer::Create()
40 {
41         int r;
43         const PixelFormatInfo& format_info = get_pixel_format_info(m_format);
45         m_num_planes = format_info.num_planes;
47         for (int i = 0; i < format_info.num_planes; ++i) {
48                 const PixelFormatPlaneInfo& pi = format_info.planes[i];
49                 FramebufferPlane& plane = m_planes[i];
51                 /* create dumb buffer */
52                 struct drm_mode_create_dumb creq = drm_mode_create_dumb();
53                 creq.width = width();
54                 creq.height = height() / pi.ysub;
55                 creq.bpp = pi.bitspp;
56                 r = drmIoctl(card().fd(), DRM_IOCTL_MODE_CREATE_DUMB, &creq);
57                 if (r)
58                         throw invalid_argument(string("DRM_IOCTL_MODE_CREATE_DUMB failed: ") + strerror(errno));
60                 plane.handle = creq.handle;
61                 plane.stride = creq.pitch;
62                 plane.size = creq.height * creq.pitch;
63                 plane.offset = 0;
64                 plane.map = 0;
65                 plane.prime_fd = -1;
66         }
68         /* create framebuffer object for the dumb-buffer */
69         uint32_t bo_handles[4] = { m_planes[0].handle, m_planes[1].handle };
70         uint32_t pitches[4] = { m_planes[0].stride, m_planes[1].stride };
71         uint32_t offsets[4] = {  m_planes[0].offset, m_planes[1].offset };
72         uint32_t id;
73         r = drmModeAddFB2(card().fd(), width(), height(), (uint32_t)format(),
74                           bo_handles, pitches, offsets, &id, 0);
75         if (r)
76                 throw invalid_argument(string("drmModeAddFB2 failed: ") + strerror(errno));
78         set_id(id);
79 }
81 void DumbFramebuffer::Destroy()
82 {
83         /* delete framebuffer */
84         drmModeRmFB(card().fd(), id());
86         for (uint i = 0; i < m_num_planes; ++i) {
87                 FramebufferPlane& plane = m_planes[i];
89                 /* unmap buffer */
90                 if (plane.map)
91                         munmap(plane.map, plane.size);
93                 /* delete dumb buffer */
94                 struct drm_mode_destroy_dumb dreq = drm_mode_destroy_dumb();
95                 dreq.handle = plane.handle;
96                 drmIoctl(card().fd(), DRM_IOCTL_MODE_DESTROY_DUMB, &dreq);
97                 if (plane.prime_fd >= 0)
98                         ::close(plane.prime_fd);
99         }
102 uint8_t* DumbFramebuffer::map(unsigned plane)
104         FramebufferPlane& p = m_planes[plane];
106         if (p.map)
107                 return p.map;
109         /* prepare buffer for memory mapping */
110         struct drm_mode_map_dumb mreq = drm_mode_map_dumb();
111         mreq.handle = p.handle;
112         int r = drmIoctl(card().fd(), DRM_IOCTL_MODE_MAP_DUMB, &mreq);
113         if (r)
114                 throw invalid_argument(string("DRM_IOCTL_MODE_MAP_DUMB failed: ") + strerror(errno));
116         /* perform actual memory mapping */
117         p.map = (uint8_t *)mmap(0, p.size, PROT_READ | PROT_WRITE, MAP_SHARED,
118                                           card().fd(), mreq.offset);
119         if (p.map == MAP_FAILED)
120                 throw invalid_argument(string("mmap failed: ") + strerror(errno));
122         return p.map;
125 int DumbFramebuffer::prime_fd(unsigned int plane)
127         if (m_planes[plane].prime_fd >= 0)
128                 return m_planes[plane].prime_fd;
130         int r = drmPrimeHandleToFD(card().fd(), m_planes[plane].handle,
131                                    DRM_CLOEXEC | O_RDWR, &m_planes[plane].prime_fd);
132         if (r)
133                 throw std::runtime_error("drmPrimeHandleToFD failed");
135         return m_planes[plane].prime_fd;