Repository navigation
Expand file tree
/
Copy pathdemo.c
More file actions
215 lines (186 loc) · 6.08 KB
/
Copy pathdemo.c
File metadata and controls
215 lines (186 loc) · 6.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
// SPDX-License-Identifier: GPL-3.0+
/*
* libge demo: allocate a DRM dumb buffer, scan it out on the panel, then use
* the GE to draw into it (rectangle fills, lines and a blit). The GE writes
* straight into the scanned-out buffer, so the result shows on the display.
*
* HARDWARE-VERIFY NOTES (things guessed for the QEMU GE model, to confirm on
* real silicon and then align the model to):
* - rectfill fills (x0,y0)..(x1,y1) inclusive of both corners;
* - the fill/line colour is the "start colour" (a/r/g/b, 0..255);
* - line endpoints are inclusive; the HW fractional walk is approximated
* with a plain Bresenham raster in QEMU;
* - bitblt copies a src rect to a dst rect of the same size (no scale).
*/
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <xf86drm.h>
#include <xf86drmMode.h>
#include <drm/drm_fourcc.h>
#include "libge.h"
struct dumbfb {
int fd; /* drm card fd */
uint32_t handle; /* GEM handle */
uint32_t fb_id; /* KMS framebuffer id */
uint32_t w, h, pitch;
uint64_t size;
int prime_fd; /* exported dma-buf for the GE */
uint8_t *map; /* CPU mapping (used to seed the blit source) */
};
/* Create a 32bpp (XRGB8888) dumb buffer and export it as a dma-buf. */
static int dumbfb_create(int fd, uint32_t w, uint32_t h, struct dumbfb *b)
{
struct drm_mode_create_dumb creq = { .width = w, .height = h, .bpp = 32 };
struct drm_mode_map_dumb mreq = { 0 };
uint32_t handles[4] = { 0 }, pitches[4] = { 0 }, offsets[4] = { 0 };
if (drmIoctl(fd, DRM_IOCTL_MODE_CREATE_DUMB, &creq)) {
printf("CREATE_DUMB(%ux%u) failed: %d\n", w, h, errno);
return -errno;
}
b->fd = fd;
b->handle = creq.handle;
b->pitch = creq.pitch;
b->size = creq.size;
b->w = w;
b->h = h;
b->prime_fd = -1;
handles[0] = b->handle;
pitches[0] = b->pitch;
if (drmModeAddFB2(fd, w, h, DRM_FORMAT_ARGB8888, handles, pitches,
offsets, &b->fb_id, 0)) {
printf("AddFB2 failed: %d\n", errno);
return -errno;
}
if (drmPrimeHandleToFD(fd, b->handle, DRM_CLOEXEC, &b->prime_fd)) {
printf("PrimeHandleToFD failed: %d\n", errno);
return -errno;
}
mreq.handle = b->handle;
if (drmIoctl(fd, DRM_IOCTL_MODE_MAP_DUMB, &mreq) == 0)
b->map = mmap(NULL, b->size, PROT_READ | PROT_WRITE, MAP_SHARED,
fd, mreq.offset);
return 0;
}
/* Find a connected connector + a crtc + its preferred mode, and scan out fb. */
static int drm_scanout(int fd, struct dumbfb *b, drmModeModeInfo *mode_out)
{
drmModeRes *res = drmModeGetResources(fd);
drmModeConnector *conn = NULL;
uint32_t crtc_id;
int i, ret = -ENODEV;
if (!res)
return -errno;
for (i = 0; i < res->count_connectors; i++) {
conn = drmModeGetConnector(fd, res->connectors[i]);
if (conn && conn->connection == DRM_MODE_CONNECTED &&
conn->count_modes > 0)
break;
drmModeFreeConnector(conn);
conn = NULL;
}
if (!conn)
goto out;
if (res->count_crtcs < 1)
goto out_conn;
crtc_id = res->crtcs[0];
*mode_out = conn->modes[0];
printf("using %ux%u on connector %u\n", mode_out->hdisplay,
mode_out->vdisplay, conn->connector_id);
ret = drmModeSetCrtc(fd, crtc_id, b->fb_id, 0, 0,
&conn->connector_id, 1, mode_out);
if (ret)
ret = -errno;
out_conn:
drmModeFreeConnector(conn);
out:
drmModeFreeResources(res);
return ret;
}
int main(int argv, char **argc)
{
struct ge_cntx ge;
struct dumbfb dst = { 0 }, src = { 0 };
drmModeModeInfo mode;
struct mstar_ge_opdata ops[8];
unsigned long tag;
int drmfd, ret, n;
uint32_t w, h;
printf("Opening GE..");
ret = libge_open(&ge);
if (ret) {
printf("failed: %d\n", ret);
return ret;
}
printf("done, caps %08x\n", ge.info.caps);
drmfd = open("/dev/dri/card0", O_RDWR | O_CLOEXEC);
if (drmfd < 0) {
printf("open card0 failed: %d\n", errno);
return errno;
}
drmSetMaster(drmfd);
/* size the destination to the panel mode */
{
drmModeRes *res = drmModeGetResources(drmfd);
drmModeConnector *c = res ? drmModeGetConnector(drmfd,
res->connectors[0]) : NULL;
w = (c && c->count_modes) ? c->modes[0].hdisplay : 640;
h = (c && c->count_modes) ? c->modes[0].vdisplay : 480;
drmModeFreeConnector(c);
drmModeFreeResources(res);
}
ret = dumbfb_create(drmfd, w, h, &dst);
if (ret) {
printf("dst dumbfb failed: %d\n", ret);
return -ret;
}
ret = drm_scanout(drmfd, &dst, &mode);
if (ret) {
printf("scanout failed: %d\n", ret);
return -ret;
}
/* a small source for the blit, seeded on the CPU with a chequer box */
ret = dumbfb_create(drmfd, 64, 64, &src);
if (!ret && src.map) {
uint32_t x, y;
for (y = 0; y < 64; y++)
for (x = 0; x < 64; x++)
((uint32_t *)(src.map + y * src.pitch))[x] =
((x ^ y) & 8) ? 0x0000ff00 : 0x00004400;
}
/* ---- Job 1: clear, a couple of filled rects, and a fan of lines ---- */
n = 0;
libge_filljob_fillrect(&ops[n++], 0, 0, w - 1, h - 1, 0xff, 0x10, 0x10, 0x40);
libge_filljob_fillrect(&ops[n++], 40, 40, 240, 160, 0xff, 0xe0, 0x20, 0x20);
libge_filljob_fillrect(&ops[n++], 300, 60, 460, 200, 0xff, 0x20, 0xd0, 0x40);
libge_filljob_line(&ops[n++], 20, h - 20, w - 20, h - 20, 0xff, 0xff, 0xff, 0xff);
libge_filljob_line(&ops[n++], 20, h - 20, w / 2, 220, 0xff, 0xff, 0xa0, 0x00);
libge_filljob_line(&ops[n++], w - 20, h - 20, w / 2, 220, 0xff, 0x00, 0xa0, 0xff);
libge_filljob_line(&ops[n++], 20, 220, w - 20, 220, 0xff, 0xa0, 0xa0, 0xa0);
ret = libge_queue_job(&ge, ops, n,
0, 0, 0, 0, 0, /* no src */
dst.prime_fd, w, h, DRM_FORMAT_ARGB8888, dst.pitch,
&tag);
printf("draw job (%d ops) returned %d\n", n, ret);
/* ---- Job 2: blit the little box onto the screen ---- */
if (src.prime_fd > 0) {
n = 0;
libge_filljob_blit(&ops[n++], 0, 0, 500, 260, 500 + 63, 260 + 63,
false, false, false, MSTAR_GE_ROTATION_0);
ret = libge_queue_job(&ge, ops, n,
src.prime_fd, 64, 64, DRM_FORMAT_ARGB8888, src.pitch,
dst.prime_fd, w, h, DRM_FORMAT_ARGB8888, dst.pitch,
&tag);
printf("blit job returned %d\n", ret);
}
printf("drawn; holding the image (Ctrl-C to quit)\n");
for (;;)
sleep(5);
libge_close(&ge);
return 0;
}