]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - android/external-libkmsxx.git/blob - libkms++/dumbframebuffer.cpp
card: fix has_atomic if libdrm does not have atomic support
[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 <drm_fourcc.h>
8 #include <drm.h>
9 #include <drm_mode.h>
11 #include "kms++.h"
13 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
15 using namespace std;
17 namespace kms
18 {
20 DumbFramebuffer::DumbFramebuffer(Card &card, uint32_t width, uint32_t height, const string& fourcc)
21         :DumbFramebuffer(card, width, height, FourCCToPixelFormat(fourcc))
22 {
23 }
25 DumbFramebuffer::DumbFramebuffer(Card& card, uint32_t width, uint32_t height, PixelFormat format)
26         :Framebuffer(card, width, height), m_format(format)
27 {
28         Create();
29 }
31 DumbFramebuffer::~DumbFramebuffer()
32 {
33         Destroy();
34 }
36 struct FormatPlaneInfo
37 {
38         uint8_t bitspp; /* bits per (macro) pixel */
39         uint8_t xsub;
40         uint8_t ysub;
41 };
43 struct FormatInfo
44 {
45         uint8_t num_planes;
46         struct FormatPlaneInfo planes[4];
47 };
49 static const map<PixelFormat, FormatInfo> format_info_array = {
50         /* YUV packed */
51         { PixelFormat::UYVY, { 1, { { 32, 2, 1 } }, } },
52         { PixelFormat::YUYV, { 1, { { 32, 2, 1 } }, } },
53         { PixelFormat::YVYU, { 1, { { 32, 2, 1 } }, } },
54         { PixelFormat::VYUY, { 1, { { 32, 2, 1 } }, } },
55         /* YUV semi-planar */
56         { PixelFormat::NV12, { 2, { { 8, 1, 1, }, { 16, 2, 2 } }, } },
57         { PixelFormat::NV21, { 2, { { 8, 1, 1, }, { 16, 2, 2 } }, } },
58         /* RGB16 */
59         { PixelFormat::RGB565, { 1, { { 16, 1, 1 } }, } },
60         /* RGB32 */
61         { PixelFormat::XRGB8888, { 1, { { 32, 1, 1 } }, } },
62         { PixelFormat::XBGR8888, { 1, { { 32, 1, 1 } }, } },
63         { PixelFormat::ARGB8888, { 1, { { 32, 1, 1 } }, } },
64         { PixelFormat::ABGR8888, { 1, { { 32, 1, 1 } }, } },
65 };
67 void DumbFramebuffer::Create()
68 {
69         int r;
71         const FormatInfo& format_info = format_info_array.at(m_format);
73         m_num_planes = format_info.num_planes;
75         for (int i = 0; i < format_info.num_planes; ++i) {
76                 const FormatPlaneInfo& pi = format_info.planes[i];
77                 FramebufferPlane& plane = m_planes[i];
79                 /* create dumb buffer */
80                 struct drm_mode_create_dumb creq = drm_mode_create_dumb();
81                 creq.width = width() / pi.xsub;
82                 creq.height = height() / pi.ysub;
83                 creq.bpp = pi.bitspp;
84                 r = drmIoctl(card().fd(), DRM_IOCTL_MODE_CREATE_DUMB, &creq);
85                 if (r)
86                         throw invalid_argument(string("DRM_IOCTL_MODE_CREATE_DUMB failed") + strerror(errno));
88                 plane.handle = creq.handle;
89                 plane.stride = creq.pitch;
90                 plane.size = creq.height * creq.pitch;
91                 plane.offset = 0;
93                 /*
94                 printf("buf %d: %dx%d, bitspp %d, stride %d, size %d\n",
95                         i, creq.width, creq.height, pi->bitspp, plane->stride, plane->size);
96                 */
98                 /* prepare buffer for memory mapping */
99                 struct drm_mode_map_dumb mreq = drm_mode_map_dumb();
100                 mreq.handle = plane.handle;
101                 r = drmIoctl(card().fd(), DRM_IOCTL_MODE_MAP_DUMB, &mreq);
102                 if (r)
103                         throw invalid_argument(string("DRM_IOCTL_MODE_MAP_DUMB failed") + strerror(errno));
105                 /* perform actual memory mapping */
106                 m_planes[i].map = (uint8_t *)mmap(0, plane.size, PROT_READ | PROT_WRITE, MAP_SHARED,
107                                                   card().fd(), mreq.offset);
108                 if (plane.map == MAP_FAILED)
109                         throw invalid_argument(string("mmap failed: ") + strerror(errno));
111                 /* clear the framebuffer to 0 */
112                 memset(plane.map, 0, plane.size);
113         }
115         /* create framebuffer object for the dumb-buffer */
116         uint32_t bo_handles[4] = { m_planes[0].handle, m_planes[1].handle };
117         uint32_t pitches[4] = { m_planes[0].stride, m_planes[1].stride };
118         uint32_t offsets[4] = {  m_planes[0].offset, m_planes[1].offset };
119         uint32_t id;
120         r = drmModeAddFB2(card().fd(), width(), height(), (uint32_t)format(),
121                           bo_handles, pitches, offsets, &id, 0);
122         if (r)
123                 throw invalid_argument(string("drmModeAddFB2 failed: ") + strerror(errno));
125         set_id(id);
128 void DumbFramebuffer::Destroy()
130         /* delete framebuffer */
131         drmModeRmFB(card().fd(), id());
133         for (uint i = 0; i < m_num_planes; ++i) {
134                 FramebufferPlane& plane = m_planes[i];
136                 /* unmap buffer */
137                 munmap(plane.map, plane.size);
139                 /* delete dumb buffer */
140                 struct drm_mode_destroy_dumb dreq = drm_mode_destroy_dumb();
141                 dreq.handle = plane.handle;
142                 drmIoctl(card().fd(), DRM_IOCTL_MODE_DESTROY_DUMB, &dreq);
144         }
147 void DumbFramebuffer::clear()
149         for (unsigned i = 0; i < m_num_planes; ++i)
150                 memset(m_planes[i].map, 0, m_planes[i].size);