1 /*
2   Copyright 2012-2020 David Robillard <d@drobilla.net>
3 
4   Permission to use, copy, modify, and/or distribute this software for any
5   purpose with or without fee is hereby granted, provided that the above
6   copyright notice and this permission notice appear in all copies.
7 
8   THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9   WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10   MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11   ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12   WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13   ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14   OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16 
17 #include "types.h"
18 #include "x11.h"
19 
20 #include "pugl/cairo.h"
21 #include "pugl/pugl.h"
22 
23 #include <X11/Xutil.h>
24 #include <cairo/cairo-xlib.h>
25 #include <cairo/cairo.h>
26 
27 #include <stdlib.h>
28 
29 typedef struct {
30   cairo_surface_t* back;
31   cairo_surface_t* front;
32   cairo_t*         cr;
33 } PuglX11CairoSurface;
34 
35 static void
puglX11CairoClose(PuglView * view)36 puglX11CairoClose(PuglView* view)
37 {
38   PuglInternals* const       impl    = view->impl;
39   PuglX11CairoSurface* const surface = (PuglX11CairoSurface*)impl->surface;
40 
41   cairo_surface_destroy(surface->front);
42   cairo_surface_destroy(surface->back);
43   surface->front = surface->back = NULL;
44 }
45 
46 static PuglStatus
puglX11CairoOpen(PuglView * view)47 puglX11CairoOpen(PuglView* view)
48 {
49   PuglInternals* const       impl    = view->impl;
50   PuglX11CairoSurface* const surface = (PuglX11CairoSurface*)impl->surface;
51   surface->back = cairo_xlib_surface_create(impl->display,
52                                             impl->win,
53                                             impl->vi->visual,
54                                             (int)view->frame.width,
55                                             (int)view->frame.height);
56 
57   surface->front =
58     cairo_surface_create_similar(surface->back,
59                                  cairo_surface_get_content(surface->back),
60                                  (int)view->frame.width,
61                                  (int)view->frame.height);
62 
63   if (cairo_surface_status(surface->back) ||
64       cairo_surface_status(surface->front)) {
65     puglX11CairoClose(view);
66     return PUGL_CREATE_CONTEXT_FAILED;
67   }
68 
69   return PUGL_SUCCESS;
70 }
71 
72 static PuglStatus
puglX11CairoCreate(PuglView * view)73 puglX11CairoCreate(PuglView* view)
74 {
75   PuglInternals* const impl = view->impl;
76 
77   impl->surface = (cairo_surface_t*)calloc(1, sizeof(PuglX11CairoSurface));
78   return PUGL_SUCCESS;
79 }
80 
81 static PuglStatus
puglX11CairoDestroy(PuglView * view)82 puglX11CairoDestroy(PuglView* view)
83 {
84   PuglInternals* const       impl    = view->impl;
85   PuglX11CairoSurface* const surface = (PuglX11CairoSurface*)impl->surface;
86 
87   puglX11CairoClose(view);
88   free(surface);
89 
90   return PUGL_SUCCESS;
91 }
92 
93 static PuglStatus
puglX11CairoEnter(PuglView * view,const PuglEventExpose * expose)94 puglX11CairoEnter(PuglView* view, const PuglEventExpose* expose)
95 {
96   PuglInternals* const       impl    = view->impl;
97   PuglX11CairoSurface* const surface = (PuglX11CairoSurface*)impl->surface;
98   PuglStatus                 st      = PUGL_SUCCESS;
99 
100   if (expose && !(st = puglX11CairoOpen(view))) {
101     surface->cr = cairo_create(surface->front);
102 
103     if (cairo_status(surface->cr)) {
104       st = PUGL_CREATE_CONTEXT_FAILED;
105     }
106   }
107   return st;
108 }
109 
110 static PuglStatus
puglX11CairoLeave(PuglView * view,const PuglEventExpose * expose)111 puglX11CairoLeave(PuglView* view, const PuglEventExpose* expose)
112 {
113   PuglInternals* const       impl    = view->impl;
114   PuglX11CairoSurface* const surface = (PuglX11CairoSurface*)impl->surface;
115 
116   if (expose) {
117     // Destroy front context and create a new one for drawing to the back
118     cairo_destroy(surface->cr);
119     surface->cr = cairo_create(surface->back);
120 
121     // Clip to expose region
122     cairo_rectangle(
123       surface->cr, expose->x, expose->y, expose->width, expose->height);
124     cairo_clip(surface->cr);
125 
126     // Paint front onto back
127     cairo_set_source_surface(surface->cr, surface->front, 0, 0);
128     cairo_paint(surface->cr);
129 
130     // Flush to X and close everything
131     cairo_destroy(surface->cr);
132     cairo_surface_flush(surface->back);
133     puglX11CairoClose(view);
134     surface->cr = NULL;
135   }
136   return PUGL_SUCCESS;
137 }
138 
139 static void*
puglX11CairoGetContext(PuglView * view)140 puglX11CairoGetContext(PuglView* view)
141 {
142   PuglInternals* const       impl    = view->impl;
143   PuglX11CairoSurface* const surface = (PuglX11CairoSurface*)impl->surface;
144   return surface->cr;
145 }
146 
147 const PuglBackend*
puglCairoBackend(void)148 puglCairoBackend(void)
149 {
150   static const PuglBackend backend = {puglX11StubConfigure,
151                                       puglX11CairoCreate,
152                                       puglX11CairoDestroy,
153                                       puglX11CairoEnter,
154                                       puglX11CairoLeave,
155                                       puglX11CairoGetContext};
156 
157   return &backend;
158 }
159