1
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>
12
13 #include <kms++/kms++.h>
14 #include <kms++/omap/omapkms++.h>
15
16 extern "C" {
17 #include <omap_drmif.h>
18 }
19
20 #define __round_mask(x, y) ((__typeof__(x))((y)-1))
21 #define round_up(x, y) ((((x)-1) | __round_mask(x, y)) + 1)
22 #define PAGE_SIZE 4096
23
24 using namespace std;
25
26 namespace kms
27 {
OmapFramebuffer(OmapCard & card,uint32_t width,uint32_t height,const string & fourcc,Flags flags)28 OmapFramebuffer::OmapFramebuffer(OmapCard& card, uint32_t width, uint32_t height, const string& fourcc, Flags flags)
29 : OmapFramebuffer(card, width, height, FourCCToPixelFormat(fourcc), flags)
30 {
31 }
32
OmapFramebuffer(OmapCard & card,uint32_t width,uint32_t height,PixelFormat format,Flags flags)33 OmapFramebuffer::OmapFramebuffer(OmapCard& card, uint32_t width, uint32_t height, PixelFormat format, Flags flags)
34 : Framebuffer(card, width, height), m_omap_card(card), m_format(format)
35 {
36 Create(width, height, format, flags);
37 }
38
~OmapFramebuffer()39 OmapFramebuffer::~OmapFramebuffer()
40 {
41 Destroy();
42 }
43
Create(uint32_t width,uint32_t height,PixelFormat format,Flags buffer_flags)44 void OmapFramebuffer::Create(uint32_t width, uint32_t height, PixelFormat format, Flags buffer_flags)
45 {
46 const PixelFormatInfo& format_info = get_pixel_format_info(format);
47
48 m_num_planes = format_info.num_planes;
49
50 for (int i = 0; i < format_info.num_planes; ++i) {
51 const PixelFormatPlaneInfo& pi = format_info.planes[i];
52 FramebufferPlane& plane = m_planes[i];
53
54 uint32_t flags = OMAP_BO_SCANOUT | OMAP_BO_WC;
55
56 #if defined(OMAP_BO_MEM_CONTIG)
57 if (buffer_flags & Flags::MemContig)
58 flags |= OMAP_BO_MEM_CONTIG;
59 if (buffer_flags & Flags::MemTiler)
60 flags |= OMAP_BO_MEM_TILER;
61 if (buffer_flags & Flags::MemPin)
62 flags |= OMAP_BO_MEM_PIN;
63 #endif
64
65 struct omap_bo* bo;
66
67 uint32_t stride;
68
69 if (!(buffer_flags & Flags::Tiled)) {
70 stride = width * pi.bitspp / 8;
71
72 uint32_t size = stride * height / pi.ysub;
73
74 bo = omap_bo_new(m_omap_card.dev(), size, flags);
75 if (!bo)
76 throw invalid_argument(string("omap_bo_new failed: ") + strerror(errno));
77 } else {
78 unsigned bitspertiler;
79
80 switch (format) {
81 case PixelFormat::NV12:
82 bitspertiler = i == 0 ? 8 : 16;
83 break;
84 case PixelFormat::YUYV:
85 case PixelFormat::UYVY:
86 bitspertiler = 32;
87 break;
88 case PixelFormat::ARGB8888:
89 case PixelFormat::XRGB8888:
90 bitspertiler = 32;
91 break;
92 case PixelFormat::RGB565:
93 bitspertiler = 16;
94 break;
95 default:
96 throw invalid_argument("unimplemented format");
97 }
98
99 switch (bitspertiler) {
100 case 8:
101 flags |= OMAP_BO_TILED_8;
102 break;
103 case 16:
104 flags |= OMAP_BO_TILED_16;
105 break;
106 case 32:
107 flags |= OMAP_BO_TILED_32;
108 break;
109 default:
110 throw invalid_argument("bad bitspertiler");
111 }
112
113 uint32_t width_tiler = width * pi.bitspp / bitspertiler;
114
115 bo = omap_bo_new_tiled(m_omap_card.dev(), width_tiler, height, flags);
116 if (!bo)
117 throw invalid_argument(string("omap_bo_new_tiled failed: ") + strerror(errno));
118
119 stride = round_up(width * pi.bitspp / 8, PAGE_SIZE);
120 }
121
122 plane.omap_bo = bo;
123 plane.handle = omap_bo_handle(bo);
124 plane.stride = stride;
125 plane.size = omap_bo_size(bo);
126 plane.offset = 0;
127 plane.map = 0;
128 plane.prime_fd = -1;
129 }
130
131 /* create framebuffer object */
132 uint32_t bo_handles[4] = { m_planes[0].handle, m_planes[1].handle };
133 uint32_t pitches[4] = { m_planes[0].stride, m_planes[1].stride };
134 uint32_t offsets[4] = { m_planes[0].offset, m_planes[1].offset };
135 uint32_t id;
136 int r = drmModeAddFB2(card().fd(), width, height, (uint32_t)format,
137 bo_handles, pitches, offsets, &id, 0);
138 if (r)
139 throw invalid_argument(string("drmModeAddFB2 failed: ") + strerror(errno));
140
141 set_id(id);
142 }
143
Destroy()144 void OmapFramebuffer::Destroy()
145 {
146 /* delete framebuffer */
147 drmModeRmFB(card().fd(), id());
148
149 for (uint i = 0; i < m_num_planes; ++i) {
150 FramebufferPlane& plane = m_planes[i];
151
152 /* unmap buffer */
153 if (plane.map)
154 munmap(plane.map, plane.size);
155
156 omap_bo_del(plane.omap_bo);
157
158 if (plane.prime_fd >= 0)
159 ::close(plane.prime_fd);
160 }
161 }
162
map(unsigned plane)163 uint8_t* OmapFramebuffer::map(unsigned plane)
164 {
165 FramebufferPlane& p = m_planes[plane];
166
167 if (p.map)
168 return p.map;
169
170 p.map = (uint8_t*)omap_bo_map(p.omap_bo);
171 if (p.map == MAP_FAILED)
172 throw invalid_argument(string("mmap failed: ") + strerror(errno));
173
174 return p.map;
175 }
176
prime_fd(unsigned int plane)177 int OmapFramebuffer::prime_fd(unsigned int plane)
178 {
179 FramebufferPlane& p = m_planes[plane];
180
181 if (p.prime_fd >= 0)
182 return p.prime_fd;
183
184 int fd = omap_bo_dmabuf(p.omap_bo);
185 if (fd < 0)
186 throw std::runtime_error("omap_bo_dmabuf failed\n");
187
188 p.prime_fd = fd;
189
190 return fd;
191 }
192
193 } // namespace kms
194