Actual source code: sdl3ops.c
1: // Defines the operations for the SDL3 PetscDraw implementation.
3: #include <../src/sys/classes/draw/impls/sdl3/sdl3impl.h>
5: // These macros transform from the users coordinates to the window pixel coordinates.
6: #define XTRANS(draw, sdl, x) ((int)(((sdl)->w - 1) * ((draw)->port_xl + ((((x) - (draw)->coor_xl) * ((draw)->port_xr - (draw)->port_xl)) / ((draw)->coor_xr - (draw)->coor_xl)))))
7: #define YTRANS(draw, sdl, y) (((sdl)->h - 1) - (int)(((sdl)->h - 1) * ((draw)->port_yl + ((((y) - (draw)->coor_yl) * ((draw)->port_yr - (draw)->port_yl)) / ((draw)->coor_yr - (draw)->coor_yl)))))
9: #define ITRANS(draw, sdl, i) ((draw)->coor_xl + (((PetscReal)(i)) * ((draw)->coor_xr - (draw)->coor_xl) / ((sdl)->w - 1) - (draw)->port_xl) / ((draw)->port_xr - (draw)->port_xl))
10: #define JTRANS(draw, sdl, j) ((draw)->coor_yl + (((PetscReal)(j)) / ((sdl)->h - 1) + (draw)->port_yl - 1) * ((draw)->coor_yr - (draw)->coor_yl) / ((draw)->port_yl - (draw)->port_yr))
12: static PetscErrorCode PetscDrawSetViewport_SDL3(PetscDraw draw, PetscReal xl, PetscReal yl, PetscReal xr, PetscReal yr)
13: {
14: PetscDraw_SDL3 *sdl = (PetscDraw_SDL3 *)draw->data;
15: int xmax = sdl->w - 1, ymax = sdl->h - 1;
16: int xa = (int)(xl * (PetscReal)xmax), ya = ymax - (int)(yr * (PetscReal)ymax);
17: int xb = (int)(xr * (PetscReal)xmax), yb = ymax - (int)(yl * (PetscReal)ymax);
18: SDL_Rect box = {xa, ya, xb + 1 - xa, yb + 1 - ya};
20: PetscFunctionBegin;
21: (void)SDL_SetRenderClipRect(sdl->renderer, &box);
22: PetscFunctionReturn(PETSC_SUCCESS);
23: }
25: static PetscErrorCode PetscDrawCoordinateToPixel_SDL3(PetscDraw draw, PetscReal x, PetscReal y, int *i, int *j)
26: {
27: PetscDraw_SDL3 *sdl = (PetscDraw_SDL3 *)draw->data;
29: PetscFunctionBegin;
30: *i = XTRANS(draw, sdl, x);
31: *j = YTRANS(draw, sdl, y);
32: PetscFunctionReturn(PETSC_SUCCESS);
33: }
35: static PetscErrorCode PetscDrawPixelToCoordinate_SDL3(PetscDraw draw, int i, int j, PetscReal *x, PetscReal *y)
36: {
37: PetscDraw_SDL3 *sdl = (PetscDraw_SDL3 *)draw->data;
39: PetscFunctionBegin;
40: *x = ITRANS(draw, sdl, i);
41: *y = JTRANS(draw, sdl, j);
42: PetscFunctionReturn(PETSC_SUCCESS);
43: }
45: static PetscErrorCode PetscDrawPoint_SDL3(PetscDraw draw, PetscReal x, PetscReal y, int c)
46: {
47: PetscDraw_SDL3 *sdl = (PetscDraw_SDL3 *)draw->data;
48: int xx = XTRANS(draw, sdl, x), yy = YTRANS(draw, sdl, y);
49: SDL_FRect pix = {(float)(xx - 1), (float)(yy - 1), 3, 3};
51: PetscFunctionBegin;
52: PetscDrawSDL3MarkDrawn(sdl);
53: PetscDrawSDL3SetColor(sdl, c);
54: (void)SDL_RenderFillRect(sdl->renderer, &pix);
55: PetscFunctionReturn(PETSC_SUCCESS);
56: }
58: static PetscErrorCode PetscDrawPointPixel_SDL3(PetscDraw draw, int x, int y, int c)
59: {
60: PetscDraw_SDL3 *sdl = (PetscDraw_SDL3 *)draw->data;
62: PetscFunctionBegin;
63: PetscDrawSDL3MarkDrawn(sdl);
64: PetscDrawSDL3SetColor(sdl, c);
65: (void)SDL_RenderPoint(sdl->renderer, (float)x, (float)y);
66: PetscFunctionReturn(PETSC_SUCCESS);
67: }
69: static PetscErrorCode PetscDrawLine_SDL3(PetscDraw draw, PetscReal xl, PetscReal yl, PetscReal xr, PetscReal yr, int cl)
70: {
71: PetscDraw_SDL3 *sdl = (PetscDraw_SDL3 *)draw->data;
72: int x_1 = XTRANS(draw, sdl, xl), y_1 = YTRANS(draw, sdl, yl), x_2 = XTRANS(draw, sdl, xr), y_2 = YTRANS(draw, sdl, yr);
74: PetscFunctionBegin;
75: PetscDrawSDL3MarkDrawn(sdl);
76: PetscDrawSDL3SetColor(sdl, cl);
77: (void)SDL_RenderLine(sdl->renderer, (float)x_1, (float)y_1, (float)x_2, (float)y_2);
78: PetscFunctionReturn(PETSC_SUCCESS);
79: }
81: static PetscErrorCode PetscDrawArrow_SDL3(PetscDraw draw, PetscReal xl, PetscReal yl, PetscReal xr, PetscReal yr, int cl)
82: {
83: PetscDraw_SDL3 *sdl = (PetscDraw_SDL3 *)draw->data;
84: int x_1 = XTRANS(draw, sdl, xl), y_1 = YTRANS(draw, sdl, yl), x_2 = XTRANS(draw, sdl, xr), y_2 = YTRANS(draw, sdl, yr);
86: PetscFunctionBegin;
87: PetscDrawSDL3MarkDrawn(sdl);
88: PetscDrawSDL3SetColor(sdl, cl);
89: (void)SDL_RenderLine(sdl->renderer, (float)x_1, (float)y_1, (float)x_2, (float)y_2);
90: if (x_1 == x_2 && y_1 == y_2) PetscFunctionReturn(PETSC_SUCCESS);
91: if (x_1 == x_2 && PetscAbs(y_1 - y_2) > 7) {
92: if (y_2 > y_1) {
93: (void)SDL_RenderLine(sdl->renderer, (float)x_2, (float)y_2, (float)(x_2 - 3), (float)(y_2 - 3));
94: (void)SDL_RenderLine(sdl->renderer, (float)x_2, (float)y_2, (float)(x_2 + 3), (float)(y_2 - 3));
95: } else {
96: (void)SDL_RenderLine(sdl->renderer, (float)x_2, (float)y_2, (float)(x_2 - 3), (float)(y_2 + 3));
97: (void)SDL_RenderLine(sdl->renderer, (float)x_2, (float)y_2, (float)(x_2 + 3), (float)(y_2 + 3));
98: }
99: }
100: if (y_1 == y_2 && PetscAbs(x_1 - x_2) > 7) {
101: if (x_2 > x_1) {
102: (void)SDL_RenderLine(sdl->renderer, (float)(x_2 - 3), (float)(y_2 - 3), (float)x_2, (float)y_2);
103: (void)SDL_RenderLine(sdl->renderer, (float)(x_2 - 3), (float)(y_2 + 3), (float)x_2, (float)y_2);
104: } else {
105: (void)SDL_RenderLine(sdl->renderer, (float)x_2, (float)y_2, (float)(x_2 + 3), (float)(y_2 - 3));
106: (void)SDL_RenderLine(sdl->renderer, (float)x_2, (float)y_2, (float)(x_2 + 3), (float)(y_2 + 3));
107: }
108: }
109: PetscFunctionReturn(PETSC_SUCCESS);
110: }
112: static PetscErrorCode PetscDrawRectangle_SDL3(PetscDraw draw, PetscReal xl, PetscReal yl, PetscReal xr, PetscReal yr, int c1, int c2, int c3, int c4)
113: {
114: PetscDraw_SDL3 *sdl = (PetscDraw_SDL3 *)draw->data;
115: int x = XTRANS(draw, sdl, xl), y = YTRANS(draw, sdl, yr), c = (c1 + c2 + c3 + c4) / 4;
116: int w = XTRANS(draw, sdl, xr) + 1 - x, h = YTRANS(draw, sdl, yl) + 1 - y;
117: SDL_FRect rect;
119: PetscFunctionBegin;
120: PetscDrawSDL3MarkDrawn(sdl);
121: PetscDrawSDL3SetColor(sdl, c);
122: if (w <= 0) w = 1;
123: if (h <= 0) h = 1;
124: rect.x = (float)x;
125: rect.y = (float)y;
126: rect.w = (float)w;
127: rect.h = (float)h;
128: (void)SDL_RenderFillRect(sdl->renderer, &rect);
129: PetscFunctionReturn(PETSC_SUCCESS);
130: }
132: static PetscErrorCode PetscDrawEllipse_SDL3(PetscDraw draw, PetscReal x, PetscReal y, PetscReal a, PetscReal b, int c)
133: {
134: PetscDraw_SDL3 *sdl = (PetscDraw_SDL3 *)draw->data;
135: int xc = XTRANS(draw, sdl, x), rx = PetscAbs(XTRANS(draw, sdl, x + a / 2) - XTRANS(draw, sdl, x - a / 2)) / 2;
136: int yc = YTRANS(draw, sdl, y), ry = PetscAbs(YTRANS(draw, sdl, y - b / 2) - YTRANS(draw, sdl, y + b / 2)) / 2;
137: int idx[3 * 64];
138: SDL_Vertex v[66];
140: PetscFunctionBegin;
141: PetscDrawSDL3MarkDrawn(sdl);
142: PetscDrawSDL3ValidColor(sdl, c);
143: // draw a triangle fan approximating the filled ellipse
144: v[0].position.x = (float)xc;
145: v[0].position.y = (float)yc;
146: v[0].color.r = sdl->cpalette[c][0] / 255.0f;
147: v[0].color.g = sdl->cpalette[c][1] / 255.0f;
148: v[0].color.b = sdl->cpalette[c][2] / 255.0f;
149: v[0].color.a = 1.0f;
150: for (int i = 0; i <= 64; i++) {
151: PetscReal theta = 2 * PETSC_PI * (PetscReal)i / 64;
153: v[i + 1].position.x = (float)(xc + rx * PetscCosReal(theta));
154: v[i + 1].position.y = (float)(yc + ry * PetscSinReal(theta));
155: v[i + 1].color = v[0].color;
156: }
157: for (int i = 0; i < 64; i++) {
158: idx[3 * i] = 0;
159: idx[3 * i + 1] = i + 1;
160: idx[3 * i + 2] = i + 2;
161: }
162: (void)SDL_RenderGeometry(sdl->renderer, NULL, v, 66, idx, 3 * 64);
163: PetscFunctionReturn(PETSC_SUCCESS);
164: }
166: static PetscErrorCode PetscDrawTriangle_SDL3(PetscDraw draw, PetscReal X1, PetscReal Y_1, PetscReal X2, PetscReal Y2, PetscReal X3, PetscReal Y3, int c1, int c2, int c3)
167: {
168: PetscDraw_SDL3 *sdl = (PetscDraw_SDL3 *)draw->data;
169: SDL_Vertex v[3];
170: int xv[3] = {XTRANS(draw, sdl, X1), XTRANS(draw, sdl, X2), XTRANS(draw, sdl, X3)};
171: int yv[3] = {YTRANS(draw, sdl, Y_1), YTRANS(draw, sdl, Y2), YTRANS(draw, sdl, Y3)};
172: int cv[3] = {c1, c2, c3};
174: PetscFunctionBegin;
175: PetscDrawSDL3MarkDrawn(sdl);
176: PetscDrawSDL3ValidColor(sdl, c1);
177: PetscDrawSDL3ValidColor(sdl, c2);
178: PetscDrawSDL3ValidColor(sdl, c3);
179: // interpolate the colormap index over the triangle, as PETSC_DRAW_X does, by sampling the palette texture
180: for (int i = 0; i < 3; i++) {
181: v[i].position.x = (float)xv[i];
182: v[i].position.y = (float)yv[i];
183: v[i].color.r = 1.0f;
184: v[i].color.g = 1.0f;
185: v[i].color.b = 1.0f;
186: v[i].color.a = 1.0f;
187: v[i].tex_coord.x = (cv[i] + 0.5f) / PETSC_DRAW_MAXCOLOR;
188: v[i].tex_coord.y = 0.5f;
189: }
190: (void)SDL_RenderGeometry(sdl->renderer, sdl->palette, v, 3, NULL, 0);
191: PetscFunctionReturn(PETSC_SUCCESS);
192: }
194: static PetscErrorCode PetscDrawStringSetSize_SDL3(PetscDraw draw, PetscReal x, PetscReal y)
195: {
196: PetscDraw_SDL3 *sdl = (PetscDraw_SDL3 *)draw->data;
197: int w = (int)(sdl->w * x * (draw->port_xr - draw->port_xl) / (draw->coor_xr - draw->coor_xl));
198: int h = (int)(sdl->h * y * (draw->port_yr - draw->port_yl) / (draw->coor_yr - draw->coor_yl));
200: PetscFunctionBegin;
201: PetscCall(PetscDrawSDL3SetFont(draw, w, h));
202: PetscFunctionReturn(PETSC_SUCCESS);
203: }
205: static PetscErrorCode PetscDrawStringGetSize_SDL3(PetscDraw draw, PetscReal *x, PetscReal *y)
206: {
207: PetscDraw_SDL3 *sdl = (PetscDraw_SDL3 *)draw->data;
208: PetscReal w = sdl->font_w, h = sdl->font_h;
210: PetscFunctionBegin;
211: if (x != NULL) *x = w * (draw->coor_xr - draw->coor_xl) / (sdl->w * (draw->port_xr - draw->port_xl));
212: if (y != NULL) *y = h * (draw->coor_yr - draw->coor_yl) / (sdl->h * (draw->port_yr - draw->port_yl));
213: PetscFunctionReturn(PETSC_SUCCESS);
214: }
216: static PetscErrorCode PetscDrawString_SDL3(PetscDraw draw, PetscReal x, PetscReal y, int c, const char chrs[])
217: {
218: PetscDraw_SDL3 *sdl = (PetscDraw_SDL3 *)draw->data;
219: int xx = XTRANS(draw, sdl, x), yy = YTRANS(draw, sdl, y);
220: size_t len;
221: const char *substr;
222: PetscToken token;
223: TTF_Text *text;
225: PetscFunctionBegin;
226: PetscDrawSDL3MarkDrawn(sdl);
227: PetscCheck(sdl->font != NULL, PETSC_COMM_SELF, PETSC_ERR_LIB, "Unable to open any font with SDL3_ttf; use -draw_sdl3_font filename to specify a font file");
228: PetscCall(PetscTokenCreate(chrs, '\n', &token));
229: PetscCall(PetscTokenFind(token, &substr));
230: while (substr != NULL) {
231: PetscCall(PetscStrlen(substr, &len));
232: if (len) {
233: text = TTF_CreateText(sdl->engine, sdl->font, substr, len);
234: PetscCheck(text != NULL, PETSC_COMM_SELF, PETSC_ERR_LIB, "Unable to create SDL3_ttf text: %s", SDL_GetError());
235: PetscDrawSDL3ValidColor(sdl, c);
236: (void)TTF_SetTextColor(text, sdl->cpalette[c][0], sdl->cpalette[c][1], sdl->cpalette[c][2], SDL_ALPHA_OPAQUE);
237: (void)TTF_DrawRendererText(text, (float)xx, (float)(yy - sdl->font_h));
238: TTF_DestroyText(text);
239: }
240: yy += sdl->font_h;
241: PetscCall(PetscTokenFind(token, &substr));
242: }
243: PetscCall(PetscTokenDestroy(&token));
244: PetscFunctionReturn(PETSC_SUCCESS);
245: }
247: static PetscErrorCode PetscDrawStringVertical_SDL3(PetscDraw draw, PetscReal x, PetscReal y, int c, const char chrs[])
248: {
249: PetscDraw_SDL3 *sdl = (PetscDraw_SDL3 *)draw->data;
250: int xx = XTRANS(draw, sdl, x), yy = YTRANS(draw, sdl, y);
251: char chr[2] = {0, 0};
252: TTF_Text *text;
254: PetscFunctionBegin;
255: PetscDrawSDL3MarkDrawn(sdl);
256: PetscCheck(sdl->font != NULL, PETSC_COMM_SELF, PETSC_ERR_LIB, "Unable to open any font with SDL3_ttf; use -draw_sdl3_font filename to specify a font file");
257: PetscDrawSDL3ValidColor(sdl, c);
258: while ((chr[0] = *chrs++)) {
259: text = TTF_CreateText(sdl->engine, sdl->font, chr, 1);
260: PetscCheck(text != NULL, PETSC_COMM_SELF, PETSC_ERR_LIB, "Unable to create SDL3_ttf text: %s", SDL_GetError());
261: (void)TTF_SetTextColor(text, sdl->cpalette[c][0], sdl->cpalette[c][1], sdl->cpalette[c][2], SDL_ALPHA_OPAQUE);
262: (void)TTF_DrawRendererText(text, (float)xx, (float)yy);
263: TTF_DestroyText(text);
264: yy += sdl->font_h;
265: }
266: PetscFunctionReturn(PETSC_SUCCESS);
267: }
269: // recreate the surface of each process, and the display texture of the window, with a new size
270: static PetscErrorCode PetscDrawSDL3SetSize(PetscDraw draw, int w, int h)
271: {
272: PetscDraw_SDL3 *sdl = (PetscDraw_SDL3 *)draw->data;
274: PetscFunctionBegin;
275: draw->w = w;
276: draw->h = h;
277: PetscCall(PetscDrawSDL3CloseSurface(sdl));
278: PetscCall(PetscDrawSDL3OpenSurface(sdl, w, h));
279: // PetscDrawSDL3Combine() allocates the combined image of the new size
280: PetscCall(PetscFree(sdl->image));
281: if (sdl->win != NULL) PetscCall(PetscDrawSDL3CreateDisplay(sdl));
282: // reset the clipping
283: PetscCall(PetscDrawSetViewport_SDL3(draw, draw->port_xl, draw->port_yl, draw->port_xr, draw->port_yr));
284: PetscFunctionReturn(PETSC_SUCCESS);
285: }
287: // process pending window events on the first process, and give all processes the new window location and size
288: static PetscErrorCode PetscDrawCheckResizedWindow_SDL3(PetscDraw draw)
289: {
290: PetscDraw_SDL3 *sdl = (PetscDraw_SDL3 *)draw->data;
291: int xywh[4] = {0, 0, 0, 0};
292: PetscMPIInt rank;
294: PetscFunctionBegin;
295: if (sdl->isvirtual == PETSC_TRUE || sdl->shared == PETSC_TRUE) PetscFunctionReturn(PETSC_SUCCESS);
296: PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)draw), &rank));
298: if (rank == 0) {
299: SDL_Event event;
301: while (SDL_PollEvent(&event)) {
302: // ignore all events except resizing of this window, queried below
303: }
304: (void)SDL_GetWindowPosition(sdl->win, &xywh[0], &xywh[1]);
305: (void)SDL_GetWindowSize(sdl->win, &xywh[2], &xywh[3]);
306: }
307: PetscCallMPI(MPI_Bcast(xywh, 4, MPI_INT, 0, PetscObjectComm((PetscObject)draw)));
309: // record new window position
310: draw->x = sdl->x = xywh[0];
311: draw->y = sdl->y = xywh[1];
312: // a minimized window may report no size
313: if ((xywh[2] == sdl->w && xywh[3] == sdl->h) || xywh[2] <= 0 || xywh[3] <= 0) PetscFunctionReturn(PETSC_SUCCESS);
314: // the resize loses the drawing, as with PETSC_DRAW_X. The application redraws it.
315: PetscCall(PetscDrawSDL3SetSize(draw, xywh[2], xywh[3]));
316: PetscFunctionReturn(PETSC_SUCCESS);
317: }
319: static PetscErrorCode PetscDrawFlush_SDL3(PetscDraw draw)
320: {
321: PetscDraw_SDL3 *sdl = (PetscDraw_SDL3 *)draw->data;
322: unsigned int *image;
324: PetscFunctionBegin;
325: // keep the window responsive. PetscDrawCheckResizedWindow() handles a resize before the next frame.
326: if (sdl->win != NULL) {
327: SDL_Event event;
329: while (SDL_PollEvent(&event)) { }
330: }
331: // PetscDrawGetImage_SDL3() combines the surfaces of a draw without a window
332: if (sdl->isvirtual == PETSC_TRUE) {
333: (void)SDL_FlushRenderer(sdl->renderer);
334: PetscFunctionReturn(PETSC_SUCCESS);
335: }
336: if (sdl->shared == PETSC_TRUE) {
337: // a singleton displays only the surface of its own process, in the window of its parent if this process owns it
338: (void)SDL_FlushRenderer(sdl->renderer);
339: image = sdl->pixels;
340: } else PetscCall(PetscDrawSDL3Combine(draw, &image));
341: if (sdl->win != NULL) {
342: (void)SDL_UpdateTexture(sdl->display, NULL, image, sdl->w * (int)sizeof(*image));
343: (void)SDL_RenderTexture(sdl->wrenderer, sdl->display, NULL, NULL);
344: (void)SDL_RenderPresent(sdl->wrenderer);
345: }
346: PetscFunctionReturn(PETSC_SUCCESS);
347: }
349: static PetscErrorCode PetscDrawClear_SDL3(PetscDraw draw)
350: {
351: PetscDraw_SDL3 *sdl = (PetscDraw_SDL3 *)draw->data;
352: int xmax = sdl->w - 1, ymax = sdl->h - 1;
353: int xa = (int)(draw->port_xl * xmax), ya = ymax - (int)(draw->port_yr * ymax);
354: int xb = (int)(draw->port_xr * xmax), yb = ymax - (int)(draw->port_yl * ymax);
355: SDL_FRect rect = {(float)xa, (float)ya, (float)(xb + 1 - xa), (float)(yb + 1 - ya)};
357: PetscFunctionBegin;
358: // each process clears its own surface
359: PetscCall(PetscDrawSDL3ClearRect(sdl, &rect));
360: // the surface holds no drawing once all of it is cleared
361: if (xa <= 0 && ya <= 0 && xb >= xmax && yb >= ymax) *sdl->pdrawn = PETSC_FALSE;
362: PetscFunctionReturn(PETSC_SUCCESS);
363: }
365: static PetscErrorCode PetscDrawGetPopup_SDL3(PetscDraw draw, PetscDraw *popup)
366: {
367: PetscDraw_SDL3 *sdl = (PetscDraw_SDL3 *)draw->data;
368: PetscBool flg = PETSC_TRUE;
370: PetscFunctionBegin;
371: PetscCall(PetscOptionsGetBool(((PetscObject)draw)->options, ((PetscObject)draw)->prefix, "-draw_popup", &flg, NULL));
372: if (flg == PETSC_FALSE || sdl->isvirtual == PETSC_TRUE) {
373: *popup = NULL;
374: PetscFunctionReturn(PETSC_SUCCESS);
375: }
377: PetscCall(PetscDrawCreate(PetscObjectComm((PetscObject)draw), NULL, NULL, sdl->x, sdl->y + sdl->h + 10, 220, 220, popup));
378: PetscCall(PetscObjectSetOptionsPrefix((PetscObject)*popup, "popup_"));
379: PetscCall(PetscObjectAppendOptionsPrefix((PetscObject)*popup, ((PetscObject)draw)->prefix));
380: PetscCall(PetscDrawSetType(*popup, PETSC_DRAW_SDL3));
381: draw->popup = *popup;
382: PetscFunctionReturn(PETSC_SUCCESS);
383: }
385: static PetscErrorCode PetscDrawSetTitle_SDL3(PetscDraw draw, const char title[])
386: {
387: PetscDraw_SDL3 *sdl = (PetscDraw_SDL3 *)draw->data;
389: PetscFunctionBegin;
390: if (sdl->win != NULL) (void)SDL_SetWindowTitle(sdl->win, title);
391: PetscFunctionReturn(PETSC_SUCCESS);
392: }
394: static PetscErrorCode PetscDrawResizeWindow_SDL3(PetscDraw draw, int w, int h)
395: {
396: PetscDraw_SDL3 *sdl = (PetscDraw_SDL3 *)draw->data;
397: PetscMPIInt rank;
399: PetscFunctionBegin;
400: if (w == sdl->w && h == sdl->h) PetscFunctionReturn(PETSC_SUCCESS);
401: PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)draw), &rank));
403: if (sdl->isvirtual == PETSC_FALSE) {
404: if (rank == 0) {
405: (void)SDL_SetWindowSize(sdl->win, w, h);
406: // the resize can be asynchronous. Wait until the new size takes effect.
407: (void)SDL_SyncWindow(sdl->win);
408: }
409: // the first process gives the size the window actually has to all processes
410: PetscCall(PetscDrawCheckResizedWindow_SDL3(draw));
411: } else PetscCall(PetscDrawSDL3SetSize(draw, w, h));
412: PetscFunctionReturn(PETSC_SUCCESS);
413: }
415: static PetscErrorCode PetscDrawGetMouseButton_SDL3(PetscDraw draw, PetscDrawButton *button, PetscReal *x_user, PetscReal *y_user, PetscReal *x_phys, PetscReal *y_phys)
416: {
417: PetscDraw_SDL3 *sdl = (PetscDraw_SDL3 *)draw->data;
418: PetscMPIInt rank;
419: PetscReal xx, yy;
420: float px = 0, py = 0;
422: PetscFunctionBegin;
423: *button = PETSC_BUTTON_NONE;
424: // only a singleton on the process that owns the window of its parent receives mouse input
425: if (sdl->isvirtual == PETSC_TRUE || (sdl->shared == PETSC_TRUE && sdl->win == NULL)) PetscFunctionReturn(PETSC_SUCCESS);
426: PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)draw), &rank));
428: if (rank == 0) {
429: SDL_Event event;
430: PetscBool got = PETSC_FALSE;
431: int ww = sdl->w, wh = sdl->h;
433: while (got == PETSC_FALSE) {
434: (void)SDL_WaitEvent(&event);
435: switch (event.type) {
436: case SDL_EVENT_MOUSE_BUTTON_UP:
437: if (event.button.windowID != SDL_GetWindowID(sdl->win)) continue;
438: px = event.button.x;
439: py = event.button.y;
440: switch (event.button.button) {
441: case SDL_BUTTON_LEFT:
442: *button = PETSC_BUTTON_LEFT;
443: break;
444: case SDL_BUTTON_MIDDLE:
445: *button = PETSC_BUTTON_CENTER;
446: break;
447: case SDL_BUTTON_RIGHT:
448: *button = PETSC_BUTTON_RIGHT;
449: break;
450: default:
451: continue;
452: }
453: got = PETSC_TRUE;
454: break;
455: case SDL_EVENT_MOUSE_WHEEL:
456: if (event.wheel.windowID != SDL_GetWindowID(sdl->win) || event.wheel.y == 0) continue;
457: *button = (event.wheel.y > 0) ? PETSC_BUTTON_WHEEL_UP : PETSC_BUTTON_WHEEL_DOWN;
458: px = event.wheel.mouse_x;
459: py = event.wheel.mouse_y;
460: got = PETSC_TRUE;
461: break;
462: case SDL_EVENT_WINDOW_CLOSE_REQUESTED:
463: // the user asked to close the window. Stop waiting for a mouse button.
464: if (event.window.windowID != SDL_GetWindowID(sdl->win)) continue;
465: got = PETSC_TRUE;
466: break;
467: case SDL_EVENT_QUIT:
468: got = PETSC_TRUE;
469: break;
470: default:
471: continue;
472: }
473: }
474: if (SDL_GetModState() & SDL_KMOD_SHIFT) {
475: switch (*button) {
476: case PETSC_BUTTON_LEFT:
477: *button = PETSC_BUTTON_LEFT_SHIFT;
478: break;
479: case PETSC_BUTTON_CENTER:
480: *button = PETSC_BUTTON_CENTER_SHIFT;
481: break;
482: case PETSC_BUTTON_RIGHT:
483: *button = PETSC_BUTTON_RIGHT_SHIFT;
484: break;
485: default:
486: break;
487: }
488: }
489: // the user may resize the window before pressing the mouse button
490: (void)SDL_GetWindowSize(sdl->win, &ww, &wh);
491: xx = ((PetscReal)px) / ww;
492: yy = 1 - ((PetscReal)py) / wh;
493: if (x_user != NULL) *x_user = draw->coor_xl + (xx - draw->port_xl) * (draw->coor_xr - draw->coor_xl) / (draw->port_xr - draw->port_xl);
494: if (y_user != NULL) *y_user = draw->coor_yl + (yy - draw->port_yl) * (draw->coor_yr - draw->coor_yl) / (draw->port_yr - draw->port_yl);
495: if (x_phys != NULL) *x_phys = xx;
496: if (y_phys != NULL) *y_phys = yy;
497: }
498: PetscCall(PetscDrawCheckResizedWindow_SDL3(draw));
499: PetscFunctionReturn(PETSC_SUCCESS);
500: }
502: static PetscErrorCode PetscDrawSetVisible_SDL3(PetscDraw draw, PetscBool visible)
503: {
504: PetscDraw_SDL3 *sdl = (PetscDraw_SDL3 *)draw->data;
506: PetscFunctionBegin;
507: if (sdl->win != NULL) {
508: if (visible == PETSC_TRUE) (void)SDL_ShowWindow(sdl->win);
509: else (void)SDL_HideWindow(sdl->win);
510: }
511: PetscFunctionReturn(PETSC_SUCCESS);
512: }
514: static PetscErrorCode PetscDrawPause_SDL3(PetscDraw draw)
515: {
516: PetscDraw_SDL3 *sdl = (PetscDraw_SDL3 *)draw->data;
518: PetscFunctionBegin;
519: if (sdl->isvirtual == PETSC_TRUE || (sdl->shared == PETSC_TRUE && sdl->win == NULL)) PetscFunctionReturn(PETSC_SUCCESS);
520: if (draw->pause > 0) PetscCall(PetscSleep(draw->pause));
521: else if (draw->pause == -1) {
522: PetscDrawButton button = PETSC_BUTTON_NONE;
524: PetscCall(PetscDrawGetMouseButton(draw, &button, NULL, NULL, NULL, NULL));
525: if (button == PETSC_BUTTON_CENTER || button == PETSC_BUTTON_NONE) draw->pause = 0; // NONE means a close request
526: }
527: PetscFunctionReturn(PETSC_SUCCESS);
528: }
530: static PetscErrorCode PetscDrawDestroy_SDL3(PetscDraw draw)
531: {
532: PetscDraw_SDL3 *sdl = (PetscDraw_SDL3 *)draw->data;
534: PetscFunctionBegin;
535: PetscCall(PetscDrawDestroy(&draw->popup));
536: PetscCall(PetscDrawSDL3Close(sdl));
537: PetscCall(PetscFree(draw->data));
538: PetscFunctionReturn(PETSC_SUCCESS);
539: }
541: static PetscErrorCode PetscDrawGetSingleton_SDL3(PetscDraw, PetscDraw *);
542: static PetscErrorCode PetscDrawRestoreSingleton_SDL3(PetscDraw, PetscDraw *);
544: static struct _PetscDrawOps DvOps = {NULL, PetscDrawFlush_SDL3, PetscDrawLine_SDL3, NULL, NULL, PetscDrawPoint_SDL3, NULL, PetscDrawString_SDL3, PetscDrawStringVertical_SDL3, PetscDrawStringSetSize_SDL3, PetscDrawStringGetSize_SDL3, PetscDrawSetViewport_SDL3, PetscDrawClear_SDL3, PetscDrawRectangle_SDL3, PetscDrawTriangle_SDL3, PetscDrawEllipse_SDL3, PetscDrawGetMouseButton_SDL3, PetscDrawPause_SDL3, NULL, NULL, PetscDrawGetPopup_SDL3, PetscDrawSetTitle_SDL3, PetscDrawCheckResizedWindow_SDL3, PetscDrawResizeWindow_SDL3, PetscDrawDestroy_SDL3, NULL, PetscDrawGetSingleton_SDL3, PetscDrawRestoreSingleton_SDL3, NULL, PetscDrawGetImage_SDL3, NULL, PetscDrawArrow_SDL3, PetscDrawCoordinateToPixel_SDL3, PetscDrawPixelToCoordinate_SDL3, PetscDrawPointPixel_SDL3, NULL, PetscDrawSetVisible_SDL3};
546: static PetscErrorCode PetscDrawGetSingleton_SDL3(PetscDraw draw, PetscDraw *sdraw)
547: {
548: PetscDraw_SDL3 *sdl = (PetscDraw_SDL3 *)draw->data, *ssdl;
550: PetscFunctionBegin;
551: PetscCall(PetscDrawCreate(PETSC_COMM_SELF, NULL, draw->title, draw->x, draw->y, draw->w, draw->h, sdraw));
552: PetscCall(PetscObjectChangeTypeName((PetscObject)*sdraw, PETSC_DRAW_SDL3));
553: (*sdraw)->ops[0] = DvOps;
554: // the size of the surface is owned by the parent
555: (*sdraw)->ops->resizewindow = NULL;
557: if (draw->popup != NULL) PetscCall(PetscDrawGetSingleton(draw->popup, &(*sdraw)->popup));
558: (*sdraw)->pause = draw->pause;
559: (*sdraw)->coor_xl = draw->coor_xl;
560: (*sdraw)->coor_xr = draw->coor_xr;
561: (*sdraw)->coor_yl = draw->coor_yl;
562: (*sdraw)->coor_yr = draw->coor_yr;
563: (*sdraw)->port_xl = draw->port_xl;
564: (*sdraw)->port_xr = draw->port_xr;
565: (*sdraw)->port_yl = draw->port_yl;
566: (*sdraw)->port_yr = draw->port_yr;
568: // draw on the surface of the parent draw on this process, with its renderer, font, and window if this process owns it
569: PetscCall(PetscNew(&ssdl));
570: (*sdraw)->data = (void *)ssdl;
571: PetscCall(PetscMemcpy(ssdl->cpalette, sdl->cpalette, sizeof(ssdl->cpalette)));
572: ssdl->shared = PETSC_TRUE;
573: ssdl->isvirtual = sdl->isvirtual;
574: ssdl->win = sdl->win;
575: ssdl->wrenderer = sdl->wrenderer;
576: ssdl->display = sdl->display;
577: ssdl->renderer = sdl->renderer;
578: ssdl->palette = sdl->palette;
579: ssdl->surface = sdl->surface;
580: ssdl->pixels = sdl->pixels;
581: ssdl->pdrawn = sdl->pdrawn;
582: ssdl->engine = sdl->engine;
583: ssdl->font = sdl->font;
584: ssdl->ownfont = PETSC_FALSE;
585: ssdl->font_w = sdl->font_w;
586: ssdl->font_h = sdl->font_h;
587: ssdl->font_descent = sdl->font_descent;
588: ssdl->x = sdl->x;
589: ssdl->y = sdl->y;
590: ssdl->w = sdl->w;
591: ssdl->h = sdl->h;
592: (*sdraw)->x = ssdl->x;
593: (*sdraw)->y = ssdl->y;
594: (*sdraw)->w = ssdl->w;
595: (*sdraw)->h = ssdl->h;
596: PetscFunctionReturn(PETSC_SUCCESS);
597: }
599: static PetscErrorCode PetscDrawRestoreSingleton_SDL3(PetscDraw draw, PetscDraw *sdraw)
600: {
601: PetscFunctionBegin;
602: if (draw->popup != NULL && (*sdraw)->popup != NULL) {
603: PetscBool issdl3;
605: PetscCall(PetscObjectTypeCompare((PetscObject)draw->popup, PETSC_DRAW_SDL3, &issdl3));
606: if (issdl3 == PETSC_FALSE) goto finally;
607: PetscCall(PetscObjectTypeCompare((PetscObject)(*sdraw)->popup, PETSC_DRAW_SDL3, &issdl3));
608: if (issdl3 == PETSC_FALSE) goto finally;
609: PetscCall(PetscDrawRestoreSingleton(draw->popup, &(*sdraw)->popup));
610: }
611: finally:
612: PetscCall(PetscDrawDestroy(sdraw));
613: // restore the clip rectangle of the renderer that the singleton shared
614: PetscCall(PetscDrawSetViewport_SDL3(draw, draw->port_xl, draw->port_yl, draw->port_xr, draw->port_yr));
615: PetscFunctionReturn(PETSC_SUCCESS);
616: }
618: /*MC
619: PETSC_DRAW_SDL3 - PETSc graphics device that uses SDL3 windows or an offscreen SDL3 surface
621: Options Database Keys:
622: + -draw_size w,h - percentage of screen (either 1, .5, .3, .25), or size in pixels
623: . -geometry x,y,w,h - set location and size in pixels
624: . -draw_virtual (true|false) - do not open a window (draw on an offscreen surface), `-draw_save` will still work
625: - -draw_sdl3_font filename - font file to use for text, searched for on standard font paths if not provided
627: Level: beginner
629: Notes:
630: `SDL3_ttf` must have access to some font otherwise a runtime error is generated. One can use `-draw_sdl3_font filename` in that
631: case to point to some available font.
633: Each process draws on its own offscreen surface. `PetscDrawFlush()` combines the surfaces of all processes with
634: `MPI_Reduce()`, and the first process displays the combined image in its window. Where several processes draw the
635: same pixel, the combined image contains one of their colors. `PetscDrawSave()`, and thus `-draw_save`, also
636: uses the combined image.
638: A draw from `PetscDrawGetSingleton()` draws on the surface of its process. On the first process, `PetscDrawFlush()`
639: of that draw displays only this surface in the window of the parent draw. The next `PetscDrawFlush()` of the parent
640: draw again displays the combined image of all processes.
642: Double buffering is always enabled for `PETSC_DRAW_SDL3` so `-draw_double_buffer 0` is ignored.
644: .seealso: `PetscDraw`, `PetscDrawOpenSDL3()`, `PetscDrawSetFromOptions()`
645: M*/
647: PETSC_EXTERN PetscErrorCode PetscDrawCreate_SDL3(PetscDraw draw)
648: {
649: PetscDraw_SDL3 *sdl;
650: PetscMPIInt rank;
651: int x, y, w, h, sw, sh;
652: static int xmax = 0, ymax = 0;
653: PetscBool dvirtual = PETSC_FALSE, has_display = PETSC_FALSE;
655: PetscFunctionBegin;
656: PetscCall(PetscOptionsGetBool(((PetscObject)draw)->options, ((PetscObject)draw)->prefix, "-draw_virtual", &dvirtual, NULL));
657: PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)draw), &rank));
659: // initialize the display size
660: if (dvirtual == PETSC_FALSE) {
661: if (rank == 0 && xmax) has_display = PETSC_TRUE;
662: else if (rank == 0) {
663: PetscCall(PetscDrawSDL3VideoAcquire(&has_display));
664: if (has_display == PETSC_TRUE) {
665: PetscCall(PetscDrawSDL3GetDisplaySize(&xmax, &ymax));
666: // the window takes its own video reference in PetscDrawSDL3OpenWindow()
667: PetscCall(PetscDrawSDL3VideoRelease());
668: }
669: }
670: PetscCallMPI(MPI_Bcast(&has_display, 1, MPI_C_BOOL, 0, PetscObjectComm((PetscObject)draw)));
671: if (has_display == PETSC_FALSE) {
672: PetscCall((*PetscErrorPrintf)("PETSc unable to use SDL3 windows\nproceeding without graphics\n"));
673: PetscCall(PetscDrawSetType(draw, PETSC_DRAW_NULL));
674: PetscFunctionReturn(PETSC_SUCCESS);
675: }
676: }
677: // all processes use the display size of the first process, because PetscDrawSDL3Combine() needs equal surface sizes
678: sw = xmax;
679: sh = ymax;
680: PetscCallMPI(MPI_Bcast(&sw, 1, MPI_INT, 0, PetscObjectComm((PetscObject)draw)));
681: PetscCallMPI(MPI_Bcast(&sh, 1, MPI_INT, 0, PetscObjectComm((PetscObject)draw)));
682: // an offscreen draw may have no display, so size fractions use the fallback of PetscDrawSDL3GetDisplaySize()
683: if (sw == 0) sw = 1024;
684: if (sh == 0) sh = 768;
686: PetscCall(PetscDrawSetUpWindowGeometry_Private(draw, sw, sh, dvirtual, &x, &y, &w, &h));
688: // every process draws on a surface of the window size
689: PetscCheck(w > 0 && h > 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Negative window width or height");
690: // PetscDrawSDL3Combine() reduces the ARGB8888 pixel values as MPI_UNSIGNED
691: PetscCheck(sizeof(unsigned int) == 4, PETSC_COMM_SELF, PETSC_ERR_SUP, "PETSC_DRAW_SDL3 requires a 4 byte unsigned int");
693: PetscCall(PetscNew(&sdl));
694: draw->ops[0] = DvOps;
695: draw->data = (void *)sdl;
696: draw->x = x;
697: draw->y = y;
698: draw->w = w;
699: draw->h = h;
700: sdl->x = x;
701: sdl->y = y;
702: sdl->w = w;
703: sdl->h = h;
704: sdl->isvirtual = dvirtual;
705: sdl->pdrawn = &sdl->drawn;
707: PetscCall(PetscDrawSDL3SetUpColormap(sdl));
708: // balanced by TTF_Quit() in PetscDrawSDL3Close()
709: PetscCheck(TTF_Init(), PETSC_COMM_SELF, PETSC_ERR_LIB, "Unable to initialize SDL3_ttf: %s", SDL_GetError());
710: PetscCall(PetscDrawSDL3OpenSurface(sdl, w, h));
711: PetscCall(PetscDrawSDL3SetFont(draw, 6, 10));
712: if (rank == 0 && dvirtual == PETSC_FALSE) PetscCall(PetscDrawSDL3OpenWindow(sdl, draw->title, x, y, w, h));
713: PetscFunctionReturn(PETSC_SUCCESS);
714: }