add public PixelFormatInfo
[android/external-libkmsxx.git] / libkms++ / dumbframebuffer.cpp
2 #include <cstring>
3 #include <stdexcept>
4 #include <sys/mman.h>
5 #include <xf86drm.h>
6 #include <xf86drmMode.h>
7 #include <fcntl.h>
8 #include <unistd.h>
9 #include <drm_fourcc.h>
10 #include <drm.h>
11 #include <drm_mode.h>
13 #include "kms++.h"
15 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
17 using namespace std;
19 namespace kms
20 {
22 DumbFramebuffer::DumbFramebuffer(Card &card, uint32_t width, uint32_t height, const string& fourcc)
23         :DumbFramebuffer(card, width, height, FourCCToPixelFormat(fourcc))
24 {
25 }
27 DumbFramebuffer::DumbFramebuffer(Card& card, uint32_t width, uint32_t height, PixelFormat format)
28         :Framebuffer(card, width, height), m_format(format)
29 {
30         Create();
31 }
33 DumbFramebuffer::~DumbFramebuffer()
34 {
35         Destroy();
36 }
38 void DumbFramebuffer::Create()
39 {
40         int r;
42         const PixelFormatInfo& format_info = get_pixel_format_info(m_format);
44         m_num_planes = format_info.num_planes;
46         for (int i = 0; i < format_info.num_planes; ++i) {
47                 const PixelFormatPlaneInfo& pi = format_info.planes[i];
48                 FramebufferPlane& plane = m_planes[i];
50                 /* create dumb buffer */
51                 struct drm_mode_create_dumb creq = drm_mode_create_dumb();
52                 creq.width = width();
53                 creq.height = height() / pi.ysub;
54                 creq.bpp = pi.bitspp;
55                 r = drmIoctl(card().fd(), DRM_IOCTL_MODE_CREATE_DUMB, &creq);
56                 if (r)
57                         throw invalid_argument(string("DRM_IOCTL_MODE_CREATE_DUMB failed") + strerror(errno));
59                 plane.handle = creq.handle;
60                 plane.stride = creq.pitch;
61                 plane.size = creq.height * creq.pitch;
62                 plane.offset = 0;
63                 plane.map = 0;
64                 plane.prime_fd = -1;
65         }
67         /* create framebuffer object for the dumb-buffer */
68         uint32_t bo_handles[4] = { m_planes[0].handle, m_planes[1].handle };
69         uint32_t pitches[4] = { m_planes[0].stride, m_planes[1].stride };
70         uint32_t offsets[4] = {  m_planes[0].offset, m_planes[1].offset };
71         uint32_t id;
72         r = drmModeAddFB2(card().fd(), width(), height(), (uint32_t)format(),
73                           bo_handles, pitches, offsets, &id, 0);
74         if (r)
75                 throw invalid_argument(string("drmModeAddFB2 failed: ") + strerror(errno));
77         set_id(id);
78 }
80 void DumbFramebuffer::Destroy()
81 {
82         /* delete framebuffer */
83         drmModeRmFB(card().fd(), id());
85         for (uint i = 0; i < m_num_planes; ++i) {
86                 FramebufferPlane& plane = m_planes[i];
88                 /* unmap buffer */
89                 if (plane.map)
90                         munmap(plane.map, plane.size);
92                 /* delete dumb buffer */
93                 struct drm_mode_destroy_dumb dreq = drm_mode_destroy_dumb();
94                 dreq.handle = plane.handle;
95                 drmIoctl(card().fd(), DRM_IOCTL_MODE_DESTROY_DUMB, &dreq);
96                 if (plane.prime_fd >= 0)
97                         ::close(plane.prime_fd);
98         }
99 }
101 uint8_t* DumbFramebuffer::map(unsigned plane)
103         FramebufferPlane& p = m_planes[plane];
105         if (p.map)
106                 return p.map;
108         /* prepare buffer for memory mapping */
109         struct drm_mode_map_dumb mreq = drm_mode_map_dumb();
110         mreq.handle = p.handle;
111         int r = drmIoctl(card().fd(), DRM_IOCTL_MODE_MAP_DUMB, &mreq);
112         if (r)
113                 throw invalid_argument(string("DRM_IOCTL_MODE_MAP_DUMB failed") + strerror(errno));
115         /* perform actual memory mapping */
116         p.map = (uint8_t *)mmap(0, p.size, PROT_READ | PROT_WRITE, MAP_SHARED,
117                                           card().fd(), mreq.offset);
118         if (p.map == MAP_FAILED)
119                 throw invalid_argument(string("mmap failed: ") + strerror(errno));
121         return p.map;
124 int DumbFramebuffer::prime_fd(unsigned int plane)
126         if (m_planes[plane].prime_fd >= 0)
127                 return m_planes[plane].prime_fd;
129         int r = drmPrimeHandleToFD(card().fd(), m_planes[plane].handle,
130                                    DRM_CLOEXEC, &m_planes[plane].prime_fd);
131         if (r)
132                 throw std::runtime_error("drmPrimeHandleToFD failed\n");
134         return m_planes[plane].prime_fd;