]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - android/external-libkmsxx.git/blob - libkms++/framebuffer.cpp
Initial version
[android/external-libkmsxx.git] / libkms++ / framebuffer.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"
12 #include "utils/testpat.h"
14 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
16 namespace kms
17 {
19 Framebuffer::Framebuffer(Card &card, uint32_t width, uint32_t height, const char* fourcc)
20         :DrmObject(card, DRM_MODE_OBJECT_FB)
21 {
22         uint32_t a, b, c, d;
23         a = fourcc[0];
24         b = fourcc[1];
25         c = fourcc[2];
26         d = fourcc[3];
28         uint32_t code = ((uint32_t)(a) | ((uint32_t)(b) << 8) | ((uint32_t)(c) << 16) | ((uint32_t)(d) << 24));
30         Create(width, height, code);
31 }
33 Framebuffer::~Framebuffer()
34 {
35         Destroy();
36 }
38 void Framebuffer::print_short() const
39 {
40         printf("Framebuffer %d\n", id());
41 }
43 struct FormatPlaneInfo
44 {
45         uint8_t bitspp; /* bits per (macro) pixel */
46         uint8_t xsub;
47         uint8_t ysub;
48 };
50 struct FormatInfo
51 {
52         uint32_t format;
53         uint8_t num_planes;
54         struct FormatPlaneInfo planes[4];
55 };
57 static const FormatInfo format_info_array[] = {
58         /* YUV packed */
59         { DRM_FORMAT_UYVY, 1, { { 32, 2, 1 } }, },
60         { DRM_FORMAT_YUYV, 1, { { 32, 2, 1 } }, },
61         /* YUV semi-planar */
62         { DRM_FORMAT_NV12, 2, { { 8, 1, 1, }, { 16, 2, 2 } }, },
63         /* RGB16 */
64         { DRM_FORMAT_RGB565, 1, { { 16, 1, 1 } }, },
65         /* RGB32 */
66         { DRM_FORMAT_XRGB8888, 1, { { 32, 1, 1 } }, },
67 };
69 static const FormatInfo& find_format(uint32_t format)
70 {
71         for (uint i = 0; i < ARRAY_SIZE(format_info_array); ++i) {
72                 if (format == format_info_array[i].format)
73                         return format_info_array[i];
74         }
76         throw std::invalid_argument("foo");
77 }
79 void Framebuffer::Create(uint32_t width, uint32_t height, uint32_t format)
80 {
81         int r;
83         m_width = width;
84         m_height = height;
85         m_format = format;
87         const FormatInfo& format_info = find_format(format);
89         m_num_planes = format_info.num_planes;
91         for (int i = 0; i < format_info.num_planes; ++i) {
92                 const FormatPlaneInfo& pi = format_info.planes[i];
93                 FramebufferPlane& plane = m_planes[i];
95                 /* create dumb buffer */
96                 struct drm_mode_create_dumb creq = drm_mode_create_dumb();
97                 creq.width = m_width / pi.xsub;
98                 creq.height = m_height / pi.ysub;
99                 creq.bpp = pi.bitspp;
100                 r = drmIoctl(card().fd(), DRM_IOCTL_MODE_CREATE_DUMB, &creq);
101                 if (r)
102                         throw std::invalid_argument("foo");
104                 plane.handle = creq.handle;
105                 plane.stride = creq.pitch;
106                 plane.size = creq.height * creq.pitch;
108                 /*
109                 printf("buf %d: %dx%d, bitspp %d, stride %d, size %d\n",
110                         i, creq.width, creq.height, pi->bitspp, plane->stride, plane->size);
111                 */
113                 /* prepare buffer for memory mapping */
114                 struct drm_mode_map_dumb mreq = drm_mode_map_dumb();
115                 mreq.handle = plane.handle;
116                 r = drmIoctl(card().fd(), DRM_IOCTL_MODE_MAP_DUMB, &mreq);
117                 if (r)
118                         throw std::invalid_argument("foo");
120                 /* perform actual memory mapping */
121                 m_planes[i].map = (uint8_t *)mmap(0, plane.size, PROT_READ | PROT_WRITE, MAP_SHARED,
122                                                   card().fd(), mreq.offset);
123                 if (plane.map == MAP_FAILED)
124                         throw std::invalid_argument("foo");
126                 /* clear the framebuffer to 0 */
127                 memset(plane.map, 0, plane.size);
128         }
130         /* create framebuffer object for the dumb-buffer */
131         uint32_t bo_handles[4] = { m_planes[0].handle, m_planes[1].handle };
132         uint32_t pitches[4] = { m_planes[0].stride, m_planes[1].stride };
133         uint32_t offsets[4] = { 0 };
134         uint32_t id;
135         r = drmModeAddFB2(card().fd(), m_width, m_height, format,
136                           bo_handles, pitches, offsets, &id, 0);
137         if (r)
138                 throw std::invalid_argument("foo");
140         m_id = id;
143 void Framebuffer::Destroy()
145         /* delete framebuffer */
146         drmModeRmFB(card().fd(), id());
148         for (uint i = 0; i < m_num_planes; ++i) {
149                 FramebufferPlane& plane = m_planes[i];
151                 /* unmap buffer */
152                 munmap(plane.map, plane.size);
154                 /* delete dumb buffer */
155                 struct drm_mode_destroy_dumb dreq = drm_mode_destroy_dumb();
156                 dreq.handle = plane.handle;
157                 drmIoctl(card().fd(), DRM_IOCTL_MODE_DESTROY_DUMB, &dreq);
159         }
162 void Framebuffer::clear()
164         for (unsigned i = 0; i < m_num_planes; ++i)
165                 memset(m_planes[i].map, 0, m_planes[i].size);