]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - android/external-libkmsxx.git/blob - libkms++/dumbframebuffer.cpp
README: add notes about test tools
[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 struct FormatPlaneInfo
39 {
40         uint8_t bitspp; /* bits per (macro) pixel */
41         uint8_t xsub;
42         uint8_t ysub;
43 };
45 struct FormatInfo
46 {
47         uint8_t num_planes;
48         struct FormatPlaneInfo planes[4];
49 };
51 static const map<PixelFormat, FormatInfo> format_info_array = {
52         /* YUV packed */
53         { PixelFormat::UYVY, { 1, { { 32, 2, 1 } }, } },
54         { PixelFormat::YUYV, { 1, { { 32, 2, 1 } }, } },
55         { PixelFormat::YVYU, { 1, { { 32, 2, 1 } }, } },
56         { PixelFormat::VYUY, { 1, { { 32, 2, 1 } }, } },
57         /* YUV semi-planar */
58         { PixelFormat::NV12, { 2, { { 8, 1, 1, }, { 16, 2, 2 } }, } },
59         { PixelFormat::NV21, { 2, { { 8, 1, 1, }, { 16, 2, 2 } }, } },
60         /* RGB16 */
61         { PixelFormat::RGB565, { 1, { { 16, 1, 1 } }, } },
62         /* RGB32 */
63         { PixelFormat::XRGB8888, { 1, { { 32, 1, 1 } }, } },
64         { PixelFormat::XBGR8888, { 1, { { 32, 1, 1 } }, } },
65         { PixelFormat::ARGB8888, { 1, { { 32, 1, 1 } }, } },
66         { PixelFormat::ABGR8888, { 1, { { 32, 1, 1 } }, } },
67 };
69 void DumbFramebuffer::Create()
70 {
71         int r;
73         const FormatInfo& format_info = format_info_array.at(m_format);
75         m_num_planes = format_info.num_planes;
77         for (int i = 0; i < format_info.num_planes; ++i) {
78                 const FormatPlaneInfo& pi = format_info.planes[i];
79                 FramebufferPlane& plane = m_planes[i];
81                 /* create dumb buffer */
82                 struct drm_mode_create_dumb creq = drm_mode_create_dumb();
83                 creq.width = width() / pi.xsub;
84                 creq.height = height() / pi.ysub;
85                 creq.bpp = pi.bitspp;
86                 r = drmIoctl(card().fd(), DRM_IOCTL_MODE_CREATE_DUMB, &creq);
87                 if (r)
88                         throw invalid_argument(string("DRM_IOCTL_MODE_CREATE_DUMB failed") + strerror(errno));
90                 plane.handle = creq.handle;
91                 plane.stride = creq.pitch;
92                 plane.size = creq.height * creq.pitch;
93                 plane.offset = 0;
94                 plane.map = 0;
95                 plane.prime_fd = -1;
96         }
98         /* create framebuffer object for the dumb-buffer */
99         uint32_t bo_handles[4] = { m_planes[0].handle, m_planes[1].handle };
100         uint32_t pitches[4] = { m_planes[0].stride, m_planes[1].stride };
101         uint32_t offsets[4] = {  m_planes[0].offset, m_planes[1].offset };
102         uint32_t id;
103         r = drmModeAddFB2(card().fd(), width(), height(), (uint32_t)format(),
104                           bo_handles, pitches, offsets, &id, 0);
105         if (r)
106                 throw invalid_argument(string("drmModeAddFB2 failed: ") + strerror(errno));
108         set_id(id);
111 void DumbFramebuffer::Destroy()
113         /* delete framebuffer */
114         drmModeRmFB(card().fd(), id());
116         for (uint i = 0; i < m_num_planes; ++i) {
117                 FramebufferPlane& plane = m_planes[i];
119                 /* unmap buffer */
120                 if (plane.map)
121                         munmap(plane.map, plane.size);
123                 /* delete dumb buffer */
124                 struct drm_mode_destroy_dumb dreq = drm_mode_destroy_dumb();
125                 dreq.handle = plane.handle;
126                 drmIoctl(card().fd(), DRM_IOCTL_MODE_DESTROY_DUMB, &dreq);
127                 if (plane.prime_fd >= 0)
128                         ::close(plane.prime_fd);
129         }
132 uint8_t* DumbFramebuffer::map(unsigned plane)
134         FramebufferPlane& p = m_planes[plane];
136         if (p.map)
137                 return p.map;
139         /* prepare buffer for memory mapping */
140         struct drm_mode_map_dumb mreq = drm_mode_map_dumb();
141         mreq.handle = p.handle;
142         int r = drmIoctl(card().fd(), DRM_IOCTL_MODE_MAP_DUMB, &mreq);
143         if (r)
144                 throw invalid_argument(string("DRM_IOCTL_MODE_MAP_DUMB failed") + strerror(errno));
146         /* perform actual memory mapping */
147         p.map = (uint8_t *)mmap(0, p.size, PROT_READ | PROT_WRITE, MAP_SHARED,
148                                           card().fd(), mreq.offset);
149         if (p.map == MAP_FAILED)
150                 throw invalid_argument(string("mmap failed: ") + strerror(errno));
152         return p.map;
155 int DumbFramebuffer::prime_fd(unsigned int plane)
157         if (m_planes[plane].prime_fd >= 0)
158                 return m_planes[plane].prime_fd;
160         int r = drmPrimeHandleToFD(card().fd(), m_planes[plane].handle,
161                                    DRM_CLOEXEC, &m_planes[plane].prime_fd);
162         if (r)
163                 throw std::runtime_error("drmPrimeHandleToFD failed\n");
165         return m_planes[plane].prime_fd;