1 /* GDK - The GIMP Drawing Kit
2  * Copyright (C) 2005 Red Hat, Inc.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19 
20 #include "gdkcairo.h"
21 #include "gdkdrawable.h"
22 #include "gdkinternals.h"
23 #include "gdkregion-generic.h"
24 #include "gdkalias.h"
25 
26 static void
gdk_ensure_surface_flush(gpointer surface)27 gdk_ensure_surface_flush (gpointer surface)
28 {
29   cairo_surface_flush (surface);
30   cairo_surface_destroy (surface);
31 }
32 
33 /**
34  * gdk_cairo_create:
35  * @drawable: a #GdkDrawable
36  *
37  * Creates a Cairo context for drawing to @drawable.
38  *
39  * <note><para>
40  * Note that due to double-buffering, Cairo contexts created
41  * in a GTK+ expose event handler cannot be cached and reused
42  * between different expose events.
43  * </para></note>
44  *
45  * Return value: A newly created Cairo context. Free with
46  *  cairo_destroy() when you are done drawing.
47  *
48  * Since: 2.8
49  **/
50 cairo_t *
gdk_cairo_create(GdkDrawable * drawable)51 gdk_cairo_create (GdkDrawable *drawable)
52 {
53   static const cairo_user_data_key_t key;
54   cairo_surface_t *surface;
55   cairo_t *cr;
56 
57   g_return_val_if_fail (GDK_IS_DRAWABLE (drawable), NULL);
58 
59   surface = _gdk_drawable_ref_cairo_surface (drawable);
60   cr = cairo_create (surface);
61 
62   if (GDK_DRAWABLE_GET_CLASS (drawable)->set_cairo_clip)
63     GDK_DRAWABLE_GET_CLASS (drawable)->set_cairo_clip (drawable, cr);
64 
65   /* Ugly workaround for GTK not ensuring to flush surfaces before
66    * directly accessing the drawable backed by the surface. Not visible
67    * on X11 (where flushing is a no-op). For details, see
68    * https://bugzilla.gnome.org/show_bug.cgi?id=628291
69    */
70   cairo_set_user_data (cr, &key, surface, gdk_ensure_surface_flush);
71 
72   return cr;
73 }
74 
75 /**
76  * gdk_cairo_reset_clip:
77  * @cr: a #cairo_t
78  * @drawable: a #GdkDrawable
79  *
80  * Resets the clip region for a Cairo context created by gdk_cairo_create().
81  *
82  * This resets the clip region to the "empty" state for the given drawable.
83  * This is required for non-native windows since a direct call to
84  * cairo_reset_clip() would unset the clip region inherited from the
85  * drawable (i.e. the window clip region), and thus let you e.g.
86  * draw outside your window.
87  *
88  * This is rarely needed though, since most code just create a new cairo_t
89  * using gdk_cairo_create() each time they want to draw something.
90  *
91  * Since: 2.18
92  **/
93 void
gdk_cairo_reset_clip(cairo_t * cr,GdkDrawable * drawable)94 gdk_cairo_reset_clip (cairo_t            *cr,
95 		      GdkDrawable        *drawable)
96 {
97   cairo_reset_clip (cr);
98 
99   if (GDK_DRAWABLE_GET_CLASS (drawable)->set_cairo_clip)
100     GDK_DRAWABLE_GET_CLASS (drawable)->set_cairo_clip (drawable, cr);
101 }
102 
103 /**
104  * gdk_cairo_set_source_color:
105  * @cr: a #cairo_t
106  * @color: a #GdkColor
107  *
108  * Sets the specified #GdkColor as the source color of @cr.
109  *
110  * Since: 2.8
111  **/
112 void
gdk_cairo_set_source_color(cairo_t * cr,const GdkColor * color)113 gdk_cairo_set_source_color (cairo_t        *cr,
114 			    const GdkColor *color)
115 {
116   g_return_if_fail (cr != NULL);
117   g_return_if_fail (color != NULL);
118 
119   cairo_set_source_rgb (cr,
120 			color->red / 65535.,
121 			color->green / 65535.,
122 			color->blue / 65535.);
123 }
124 
125 /**
126  * gdk_cairo_rectangle:
127  * @cr: a #cairo_t
128  * @rectangle: a #GdkRectangle
129  *
130  * Adds the given rectangle to the current path of @cr.
131  *
132  * Since: 2.8
133  **/
134 void
gdk_cairo_rectangle(cairo_t * cr,const GdkRectangle * rectangle)135 gdk_cairo_rectangle (cairo_t            *cr,
136 		     const GdkRectangle *rectangle)
137 {
138   g_return_if_fail (cr != NULL);
139   g_return_if_fail (rectangle != NULL);
140 
141   cairo_rectangle (cr,
142 		   rectangle->x,     rectangle->y,
143 		   rectangle->width, rectangle->height);
144 }
145 
146 /**
147  * gdk_cairo_region:
148  * @cr: a #cairo_t
149  * @region: a #GdkRegion
150  *
151  * Adds the given region to the current path of @cr.
152  *
153  * Since: 2.8
154  **/
155 void
gdk_cairo_region(cairo_t * cr,const GdkRegion * region)156 gdk_cairo_region (cairo_t         *cr,
157 		  const GdkRegion *region)
158 {
159   GdkRegionBox *boxes;
160   gint n_boxes, i;
161 
162   g_return_if_fail (cr != NULL);
163   g_return_if_fail (region != NULL);
164 
165   boxes = region->rects;
166   n_boxes = region->numRects;
167 
168   for (i = 0; i < n_boxes; i++)
169     cairo_rectangle (cr,
170 		     boxes[i].x1,
171 		     boxes[i].y1,
172 		     boxes[i].x2 - boxes[i].x1,
173 		     boxes[i].y2 - boxes[i].y1);
174 }
175 
176 /**
177  * gdk_cairo_set_source_pixbuf:
178  * @cr: a #Cairo context
179  * @pixbuf: a #GdkPixbuf
180  * @pixbuf_x: X coordinate of location to place upper left corner of @pixbuf
181  * @pixbuf_y: Y coordinate of location to place upper left corner of @pixbuf
182  *
183  * Sets the given pixbuf as the source pattern for the Cairo context.
184  * The pattern has an extend mode of %CAIRO_EXTEND_NONE and is aligned
185  * so that the origin of @pixbuf is @pixbuf_x, @pixbuf_y
186  *
187  * Since: 2.8
188  **/
189 void
gdk_cairo_set_source_pixbuf(cairo_t * cr,const GdkPixbuf * pixbuf,double pixbuf_x,double pixbuf_y)190 gdk_cairo_set_source_pixbuf (cairo_t         *cr,
191 			     const GdkPixbuf *pixbuf,
192 			     double           pixbuf_x,
193 			     double           pixbuf_y)
194 {
195   gint width = gdk_pixbuf_get_width (pixbuf);
196   gint height = gdk_pixbuf_get_height (pixbuf);
197   guchar *gdk_pixels = gdk_pixbuf_get_pixels (pixbuf);
198   int gdk_rowstride = gdk_pixbuf_get_rowstride (pixbuf);
199   int n_channels = gdk_pixbuf_get_n_channels (pixbuf);
200   int cairo_stride;
201   guchar *cairo_pixels;
202   cairo_format_t format;
203   cairo_surface_t *surface;
204   static const cairo_user_data_key_t key;
205   cairo_status_t status;
206   int j;
207 
208   if (n_channels == 3)
209     format = CAIRO_FORMAT_RGB24;
210   else
211     format = CAIRO_FORMAT_ARGB32;
212 
213   cairo_stride = cairo_format_stride_for_width (format, width);
214   cairo_pixels = g_malloc_n (height, cairo_stride);
215   surface = cairo_image_surface_create_for_data ((unsigned char *)cairo_pixels,
216                                                  format,
217                                                  width, height, cairo_stride);
218 
219   status = cairo_surface_set_user_data (surface, &key,
220                                         cairo_pixels, (cairo_destroy_func_t)g_free);
221   if (status != CAIRO_STATUS_SUCCESS)
222     {
223       g_free (cairo_pixels);
224       goto out;
225     }
226 
227   for (j = height; j; j--)
228     {
229       guchar *p = gdk_pixels;
230       guchar *q = cairo_pixels;
231 
232       if (n_channels == 3)
233 	{
234 	  guchar *end = p + 3 * width;
235 
236 	  while (p < end)
237 	    {
238 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
239 	      q[0] = p[2];
240 	      q[1] = p[1];
241 	      q[2] = p[0];
242 #else
243 	      q[1] = p[0];
244 	      q[2] = p[1];
245 	      q[3] = p[2];
246 #endif
247 	      p += 3;
248 	      q += 4;
249 	    }
250 	}
251       else
252 	{
253 	  guchar *end = p + 4 * width;
254 	  guint t1,t2,t3;
255 
256 #define MULT(d,c,a,t) G_STMT_START { t = c * a + 0x80; d = ((t >> 8) + t) >> 8; } G_STMT_END
257 
258 	  while (p < end)
259 	    {
260 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
261 	      MULT(q[0], p[2], p[3], t1);
262 	      MULT(q[1], p[1], p[3], t2);
263 	      MULT(q[2], p[0], p[3], t3);
264 	      q[3] = p[3];
265 #else
266 	      q[0] = p[3];
267 	      MULT(q[1], p[0], p[3], t1);
268 	      MULT(q[2], p[1], p[3], t2);
269 	      MULT(q[3], p[2], p[3], t3);
270 #endif
271 
272 	      p += 4;
273 	      q += 4;
274 	    }
275 
276 #undef MULT
277 	}
278 
279       gdk_pixels += gdk_rowstride;
280       cairo_pixels += cairo_stride;
281     }
282 
283 out:
284   cairo_set_source_surface (cr, surface, pixbuf_x, pixbuf_y);
285   cairo_surface_destroy (surface);
286 }
287 
288 /**
289  * gdk_cairo_set_source_pixmap:
290  * @cr: a #Cairo context
291  * @pixmap: a #GdkPixmap
292  * @pixmap_x: X coordinate of location to place upper left corner of @pixmap
293  * @pixmap_y: Y coordinate of location to place upper left corner of @pixmap
294  *
295  * Sets the given pixmap as the source pattern for the Cairo context.
296  * The pattern has an extend mode of %CAIRO_EXTEND_NONE and is aligned
297  * so that the origin of @pixmap is @pixmap_x, @pixmap_y
298  *
299  * Since: 2.10
300  *
301  * Deprecated: 2.24: This function is being removed in GTK+ 3 (together
302  *     with #GdkPixmap). Instead, use gdk_cairo_set_source_window() where
303  *     appropriate.
304  **/
305 void
gdk_cairo_set_source_pixmap(cairo_t * cr,GdkPixmap * pixmap,double pixmap_x,double pixmap_y)306 gdk_cairo_set_source_pixmap (cairo_t   *cr,
307 			     GdkPixmap *pixmap,
308 			     double     pixmap_x,
309 			     double     pixmap_y)
310 {
311   cairo_surface_t *surface;
312 
313   surface = _gdk_drawable_ref_cairo_surface (GDK_DRAWABLE (pixmap));
314   cairo_set_source_surface (cr, surface, pixmap_x, pixmap_y);
315   cairo_surface_destroy (surface);
316 }
317 
318 /**
319  * gdk_cairo_set_source_window:
320  * @cr: a #Cairo context
321  * @window: a #GdkWindow
322  * @x: X coordinate of location to place upper left corner of @window
323  * @y: Y coordinate of location to place upper left corner of @window
324  *
325  * Sets the given window as the source pattern for the Cairo context.
326  * The pattern has an extend mode of %CAIRO_EXTEND_NONE and is aligned
327  * so that the origin of @window is @x, @y. The window contains all its
328  * subwindows when rendering.
329  *
330  * Note that the contents of @window are undefined outside of the
331  * visible part of @window, so use this function with care.
332  *
333  * Since: 2.24
334  **/
335 void
gdk_cairo_set_source_window(cairo_t * cr,GdkWindow * window,double x,double y)336 gdk_cairo_set_source_window (cairo_t   *cr,
337                              GdkWindow *window,
338                              double     x,
339                              double     y)
340 {
341   cairo_surface_t *surface;
342 
343   g_return_if_fail (cr != NULL);
344   g_return_if_fail (GDK_IS_WINDOW (window));
345 
346   surface = _gdk_drawable_ref_cairo_surface (GDK_DRAWABLE (window));
347   cairo_set_source_surface (cr, surface, x, y);
348   cairo_surface_destroy (surface);
349 }
350 
351 
352 #define __GDK_CAIRO_C__
353 #include "gdkaliasdef.c"
354