]> Pileus Git - ~andy/gtk/blobdiff - gdk/gdkdraw.c
Bug 553803 – eventually call XCloseDevice on XOpenDevice results
[~andy/gtk] / gdk / gdkdraw.c
index 97905bf5662b782cb39f27b274b857174dca122c..0876695a606fb492c2b753a95f60f13117f498c8 100644 (file)
  * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
  */
 
+#include "config.h"
+#include <math.h>
+#include <pango/pangocairo.h>
+#include "gdkcairo.h"
 #include "gdkdrawable.h"
 #include "gdkinternals.h"
 #include "gdkwindow.h"
 #include "gdkscreen.h"
 #include "gdk-pixbuf-private.h"
 #include "gdkpixbuf.h"
+#include "gdkalias.h"
 
 static GdkImage*    gdk_drawable_real_get_image (GdkDrawable     *drawable,
                                                 gint             x,
@@ -56,37 +61,9 @@ static void         gdk_drawable_real_draw_pixbuf            (GdkDrawable  *draw
                                                              GdkRgbDither  dither,
                                                              gint          x_dither,
                                                              gint          y_dither);
+     
 
-static void gdk_drawable_class_init (GdkDrawableClass *klass);
-
-GType
-gdk_drawable_get_type (void)
-{
-  static GType object_type = 0;
-
-  if (!object_type)
-    {
-      static const GTypeInfo object_info =
-      {
-        sizeof (GdkDrawableClass),
-        (GBaseInitFunc) NULL,
-        (GBaseFinalizeFunc) NULL,
-        (GClassInitFunc) gdk_drawable_class_init,
-        NULL,           /* class_finalize */
-        NULL,           /* class_data */
-        sizeof (GdkDrawable),
-        0,              /* n_preallocs */
-        (GInstanceInitFunc) NULL,
-      };
-      
-      object_type = g_type_register_static (G_TYPE_OBJECT,
-                                            "GdkDrawable",
-                                            &object_info, 
-                                           G_TYPE_FLAG_ABSTRACT);
-    }  
-
-  return object_type;
-}
+G_DEFINE_ABSTRACT_TYPE (GdkDrawable, gdk_drawable, G_TYPE_OBJECT)
 
 static void
 gdk_drawable_class_init (GdkDrawableClass *klass)
@@ -99,6 +76,11 @@ gdk_drawable_class_init (GdkDrawableClass *klass)
   klass->draw_pixbuf = gdk_drawable_real_draw_pixbuf;
 }
 
+static void
+gdk_drawable_init (GdkDrawable *drawable)
+{
+}
+
 /* Manipulation of drawables
  */
 
@@ -485,22 +467,23 @@ gdk_draw_arc (GdkDrawable *drawable,
  *     necessary.
  * @points: an array of #GdkPoint structures specifying the points making 
  *     up the polygon.
- * @npoints: the number of points.
+ * @n_points: the number of points.
  * 
  * Draws an outlined or filled polygon.
  **/
 void
-gdk_draw_polygon (GdkDrawable *drawable,
-                 GdkGC       *gc,
-                 gboolean     filled,
-                 GdkPoint    *points,
-                 gint         npoints)
+gdk_draw_polygon (GdkDrawable    *drawable,
+                 GdkGC          *gc,
+                 gboolean        filled,
+                 const GdkPoint *points,
+                 gint            n_points)
 {
   g_return_if_fail (GDK_IS_DRAWABLE (drawable));
   g_return_if_fail (GDK_IS_GC (gc));
 
   GDK_DRAWABLE_GET_CLASS (drawable)->draw_polygon (drawable, gc, filled,
-                                                   points, npoints);
+                                                   (GdkPoint *) points,
+                                                   n_points);
 }
 
 /* gdk_draw_string
@@ -607,7 +590,7 @@ gdk_draw_text_wc (GdkDrawable        *drawable,
  * gdk_draw_drawable:
  * @drawable: a #GdkDrawable
  * @gc: a #GdkGC sharing the drawable's visual and colormap
- * @src: another #GdkDrawable
+ * @src: the source #GdkDrawable, which may be the same as @drawable
  * @xsrc: X position in @src of rectangle to draw
  * @ysrc: Y position in @src of rectangle to draw
  * @xdest: X position in @drawable where the rectangle should be drawn
@@ -627,9 +610,9 @@ gdk_draw_text_wc (GdkDrawable        *drawable,
  * colormap, or errors will result. (On X11, failure to match
  * visual/colormap results in a BadMatch error from the X server.)
  * A common cause of this problem is an attempt to draw a bitmap to
- * a color drawable. The way to draw a bitmap is to set the
- * bitmap as a clip mask on your #GdkGC, then use gdk_draw_rectangle()
- * to draw a rectangle clipped to the bitmap.
+ * a color drawable. The way to draw a bitmap is to set the bitmap as 
+ * the stipple on the #GdkGC, set the fill mode to %GDK_STIPPLED, and 
+ * then draw the rectangle.
  **/
 void
 gdk_draw_drawable (GdkDrawable *drawable,
@@ -748,34 +731,45 @@ gdk_draw_image (GdkDrawable *drawable,
  *
  * The clip mask of @gc is ignored, but clip rectangles and clip regions work
  * fine.
- * 
+ *
+ * If GDK is built with the Sun mediaLib library, the gdk_draw_pixbuf
+ * function is accelerated using mediaLib, which provides hardware
+ * acceleration on Intel, AMD, and Sparc chipsets.  If desired, mediaLib
+ * support can be turned off by setting the GDK_DISABLE_MEDIALIB environment
+ * variable.
+ *
  * Since: 2.2
  **/
 void
 gdk_draw_pixbuf (GdkDrawable     *drawable,
-                 GdkGC           *gc,
-                 GdkPixbuf       *pixbuf,
-                 gint             src_x,
-                 gint             src_y,
-                 gint             dest_x,
-                 gint             dest_y,
-                 gint             width,
-                 gint             height,
-                 GdkRgbDither     dither,
-                 gint             x_dither,
-                 gint             y_dither)
+                 GdkGC           *gc,
+                 const GdkPixbuf *pixbuf,
+                 gint             src_x,
+                 gint             src_y,
+                 gint             dest_x,
+                 gint             dest_y,
+                 gint             width,
+                 gint             height,
+                 GdkRgbDither     dither,
+                 gint             x_dither,
+                 gint             y_dither)
 {
   g_return_if_fail (GDK_IS_DRAWABLE (drawable));
   g_return_if_fail (gc == NULL || GDK_IS_GC (gc));
   g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
 
+  if (width == 0 || height == 0)
+    return;
+
   if (width == -1)
     width = gdk_pixbuf_get_width (pixbuf);
   if (height == -1)
     height = gdk_pixbuf_get_height (pixbuf);
 
-  GDK_DRAWABLE_GET_CLASS (drawable)->draw_pixbuf (drawable, gc, pixbuf,
-                                                 src_x, src_y, dest_x, dest_y, width, height,
+  GDK_DRAWABLE_GET_CLASS (drawable)->draw_pixbuf (drawable, gc,
+                                                  (GdkPixbuf *) pixbuf,
+                                                 src_x, src_y, dest_x, dest_y,
+                                                  width, height,
                                                  dither, x_dither, y_dither);
 }
 
@@ -784,26 +778,27 @@ gdk_draw_pixbuf (GdkDrawable     *drawable,
  * @drawable: a #GdkDrawable (a #GdkWindow or a #GdkPixmap).
  * @gc: a #GdkGC.
  * @points: an array of #GdkPoint structures.
- * @npoints: the number of points to be drawn.
+ * @n_points: the number of points to be drawn.
  * 
  * Draws a number of points, using the foreground color and other 
  * attributes of the #GdkGC.
  **/
 void
-gdk_draw_points (GdkDrawable *drawable,
-                GdkGC       *gc,
-                GdkPoint    *points,
-                gint         npoints)
+gdk_draw_points (GdkDrawable    *drawable,
+                GdkGC          *gc,
+                const GdkPoint *points,
+                gint            n_points)
 {
   g_return_if_fail (GDK_IS_DRAWABLE (drawable));
-  g_return_if_fail ((points != NULL) && (npoints > 0));
+  g_return_if_fail ((points != NULL) && (n_points > 0));
   g_return_if_fail (GDK_IS_GC (gc));
-  g_return_if_fail (npoints >= 0);
+  g_return_if_fail (n_points >= 0);
 
-  if (npoints == 0)
+  if (n_points == 0)
     return;
 
-  GDK_DRAWABLE_GET_CLASS (drawable)->draw_points (drawable, gc, points, npoints);
+  GDK_DRAWABLE_GET_CLASS (drawable)->draw_points (drawable, gc,
+                                                  (GdkPoint *) points, n_points);
 }
 
 /**
@@ -812,27 +807,28 @@ gdk_draw_points (GdkDrawable *drawable,
  * @gc: a #GdkGC.
  * @segs: an array of #GdkSegment structures specifying the start and 
  *   end points of the lines to be drawn.
- * @nsegs: the number of line segments to draw, i.e. the size of the 
+ * @n_segs: the number of line segments to draw, i.e. the size of the 
  *   @segs array.
  * 
  * Draws a number of unconnected lines.
  **/
 void
-gdk_draw_segments (GdkDrawable *drawable,
-                  GdkGC       *gc,
-                  GdkSegment  *segs,
-                  gint         nsegs)
+gdk_draw_segments (GdkDrawable      *drawable,
+                  GdkGC            *gc,
+                  const GdkSegment *segs,
+                  gint              n_segs)
 {
   g_return_if_fail (GDK_IS_DRAWABLE (drawable));
 
-  if (nsegs == 0)
+  if (n_segs == 0)
     return;
 
   g_return_if_fail (segs != NULL);
   g_return_if_fail (GDK_IS_GC (gc));
-  g_return_if_fail (nsegs >= 0);
+  g_return_if_fail (n_segs >= 0);
 
-  GDK_DRAWABLE_GET_CLASS (drawable)->draw_segments (drawable, gc, segs, nsegs);
+  GDK_DRAWABLE_GET_CLASS (drawable)->draw_segments (drawable, gc,
+                                                    (GdkSegment *) segs, n_segs);
 }
 
 /**
@@ -840,7 +836,7 @@ gdk_draw_segments (GdkDrawable *drawable,
  * @drawable: a #GdkDrawable (a #GdkWindow or a #GdkPixmap).
  * @gc: a #GdkGC.
  * @points: an array of #GdkPoint structures specifying the endpoints of the
- * @npoints: the size of the @points array.
+ * @n_points: the size of the @points array.
  * 
  * Draws a series of lines connecting the given points.
  * The way in which joins between lines are draw is determined by the
@@ -848,20 +844,55 @@ gdk_draw_segments (GdkDrawable *drawable,
  * gdk_gc_set_line_attributes().
  **/
 void
-gdk_draw_lines (GdkDrawable *drawable,
-               GdkGC       *gc,
-               GdkPoint    *points,
-               gint         npoints)
+gdk_draw_lines (GdkDrawable    *drawable,
+               GdkGC          *gc,
+               const GdkPoint *points,
+               gint            n_points)
 {
   g_return_if_fail (GDK_IS_DRAWABLE (drawable));
   g_return_if_fail (points != NULL);
   g_return_if_fail (GDK_IS_GC (gc));
-  g_return_if_fail (npoints >= 0);
+  g_return_if_fail (n_points >= 0);
 
-  if (npoints == 0)
+  if (n_points == 0)
     return;
 
-  GDK_DRAWABLE_GET_CLASS (drawable)->draw_lines (drawable, gc, points, npoints);
+  GDK_DRAWABLE_GET_CLASS (drawable)->draw_lines (drawable, gc,
+                                                 (GdkPoint *) points, n_points);
+}
+
+static void
+real_draw_glyphs (GdkDrawable       *drawable,
+                 GdkGC             *gc,
+                 const PangoMatrix *matrix,
+                 PangoFont         *font,
+                 gdouble            x,
+                 gdouble            y,
+                 PangoGlyphString  *glyphs)
+{
+  cairo_t *cr;
+
+  cr = gdk_cairo_create (drawable);
+  _gdk_gc_update_context (gc, cr, NULL, NULL, TRUE);
+
+  if (matrix)
+    {
+      cairo_matrix_t cairo_matrix;
+
+      cairo_matrix.xx = matrix->xx;
+      cairo_matrix.yx = matrix->yx;
+      cairo_matrix.xy = matrix->xy;
+      cairo_matrix.yy = matrix->yy;
+      cairo_matrix.x0 = matrix->x0;
+      cairo_matrix.y0 = matrix->y0;
+      
+      cairo_set_matrix (cr, &cairo_matrix);
+    }
+
+  cairo_move_to (cr, x, y);
+  pango_cairo_show_glyph_string (cr, font, glyphs);
+
+  cairo_destroy (cr);
 }
 
 /**
@@ -871,12 +902,12 @@ gdk_draw_lines (GdkDrawable *drawable,
  * @font: font to be used
  * @x: X coordinate of baseline origin
  * @y: Y coordinate of baseline origin
- * @glyphs: glyphs to render
+ * @glyphs: the glyph string to draw
  *
  * This is a low-level function; 99% of text rendering should be done
  * using gdk_draw_layout() instead.
  *
- * A glyph is a character in a font. This function draws a sequence of
+ * A glyph is a single image in a font. This function draws a sequence of
  * glyphs.  To obtain a sequence of glyphs you have to understand a
  * lot about internationalized text handling, which you don't want to
  * understand; thus, use gdk_draw_layout() instead of this function,
@@ -893,11 +924,94 @@ gdk_draw_glyphs (GdkDrawable      *drawable,
 {
   g_return_if_fail (GDK_IS_DRAWABLE (drawable));
   g_return_if_fail (GDK_IS_GC (gc));
+  
+  real_draw_glyphs (drawable, gc, NULL, font,
+                   x, y, glyphs);
+}
 
+/**
+ * gdk_draw_glyphs_transformed:
+ * @drawable: a #GdkDrawable
+ * @gc: a #GdkGC
+ * @matrix: a #PangoMatrix, or %NULL to use an identity transformation
+ * @font: the font in which to draw the string
+ * @x:       the x position of the start of the string (in Pango
+ *           units in user space coordinates)
+ * @y:       the y position of the baseline (in Pango units
+ *           in user space coordinates)
+ * @glyphs:  the glyph string to draw
+ * 
+ * Renders a #PangoGlyphString onto a drawable, possibly
+ * transforming the layed-out coordinates through a transformation
+ * matrix. Note that the transformation matrix for @font is not
+ * changed, so to produce correct rendering results, the @font
+ * must have been loaded using a #PangoContext with an identical
+ * transformation matrix to that passed in to this function.
+ *
+ * See also gdk_draw_glyphs(), gdk_draw_layout().
+ *
+ * Since: 2.6
+ **/
+void
+gdk_draw_glyphs_transformed (GdkDrawable       *drawable,
+                            GdkGC             *gc,
+                            const PangoMatrix *matrix,
+                            PangoFont         *font,
+                            gint               x,
+                            gint               y,
+                            PangoGlyphString  *glyphs)
+{
+  g_return_if_fail (GDK_IS_DRAWABLE (drawable));
+  g_return_if_fail (GDK_IS_GC (gc));
 
-  GDK_DRAWABLE_GET_CLASS (drawable)->draw_glyphs (drawable, gc, font, x, y, glyphs);
+  real_draw_glyphs (drawable, gc, matrix, font,
+                   x / PANGO_SCALE, y / PANGO_SCALE, glyphs);
 }
 
+/**
+ * gdk_draw_trapezoids:
+ * @drawable: a #GdkDrawable
+ * @gc: a #GdkGC
+ * @trapezoids: an array of #GdkTrapezoid structures
+ * @n_trapezoids: the number of trapezoids to draw
+ * 
+ * Draws a set of anti-aliased trapezoids. The trapezoids are
+ * combined using saturation addition, then drawn over the background
+ * as a set. This is low level functionality used internally to implement
+ * rotated underlines and backgrouds when rendering a PangoLayout and is
+ * likely not useful for applications.
+ *
+ * Since: 2.6
+ **/
+void
+gdk_draw_trapezoids (GdkDrawable        *drawable,
+                    GdkGC              *gc,
+                    const GdkTrapezoid *trapezoids,
+                    gint                n_trapezoids)
+{
+  cairo_t *cr;
+  int i;
+
+  g_return_if_fail (GDK_IS_DRAWABLE (drawable));
+  g_return_if_fail (GDK_IS_GC (gc));
+  g_return_if_fail (n_trapezoids == 0 || trapezoids != NULL);
+
+  cr = gdk_cairo_create (drawable);
+  _gdk_gc_update_context (gc, cr, NULL, NULL, TRUE);
+  
+  for (i = 0; i < n_trapezoids; i++)
+    {
+      cairo_move_to (cr, trapezoids[i].x11, trapezoids[i].y1);
+      cairo_line_to (cr, trapezoids[i].x21, trapezoids[i].y1);
+      cairo_line_to (cr, trapezoids[i].x22, trapezoids[i].y2);
+      cairo_line_to (cr, trapezoids[i].x12, trapezoids[i].y2);
+      cairo_close_path (cr);
+    }
+
+  cairo_fill (cr);
+
+  cairo_destroy (cr);
+}
 
 /**
  * gdk_drawable_copy_to_image:
@@ -1101,8 +1215,8 @@ gdk_drawable_real_get_composite_drawable (GdkDrawable *drawable,
  * but no area outside of this region will be affected by drawing
  * primitives.
  * 
- * Return value: a #GdkRegion. This must be freed with gdk_region_destroy()
- *               when you are done.
+ * Returns: a #GdkRegion. This must be freed with gdk_region_destroy()
+ *          when you are done.
  **/
 GdkRegion *
 gdk_drawable_get_clip_region (GdkDrawable *drawable)
@@ -1114,15 +1228,15 @@ gdk_drawable_get_clip_region (GdkDrawable *drawable)
 
 /**
  * gdk_drawable_get_visible_region:
- * @drawable: 
+ * @drawable: a #GdkDrawable
  * 
  * Computes the region of a drawable that is potentially visible.
  * This does not necessarily take into account if the window is
  * obscured by other windows, but no area outside of this region
  * is visible.
  * 
- * Return value: a #GdkRegion. This must be freed with gdk_region_destroy()
- *               when you are done.
+ * Returns: a #GdkRegion. This must be freed with gdk_region_destroy()
+ *          when you are done.
  **/
 GdkRegion *
 gdk_drawable_get_visible_region (GdkDrawable *drawable)
@@ -1145,6 +1259,25 @@ gdk_drawable_real_get_visible_region (GdkDrawable *drawable)
   return gdk_region_rectangle (&rect);
 }
 
+/**
+ * _gdk_drawable_ref_cairo_surface:
+ * @drawable: a #GdkDrawable
+ * 
+ * Obtains a #cairo_surface_t for the given drawable. If a
+ * #cairo_surface_t for the drawable already exists, it will be
+ * referenced, otherwise a new surface will be created.
+ * 
+ * Return value: a newly referenced #cairo_surface_t that points
+ *  to @drawable. Unref with cairo_surface_destroy()
+ **/
+cairo_surface_t *
+_gdk_drawable_ref_cairo_surface (GdkDrawable *drawable)
+{
+  g_return_val_if_fail (GDK_IS_DRAWABLE (drawable), NULL);
+
+  return GDK_DRAWABLE_GET_CLASS (drawable)->ref_cairo_surface (drawable);
+}
+
 static void
 composite (guchar *src_buf,
           gint    src_rowstride,
@@ -1239,6 +1372,44 @@ composite_0888 (guchar      *src_buf,
     }
 }
 
+#ifdef USE_MEDIALIB
+static void
+composite_0888_medialib (guchar      *src_buf,
+                        gint         src_rowstride,
+                        guchar      *dest_buf,
+                        gint         dest_rowstride,
+                        GdkByteOrder dest_byte_order,
+                        gint         width,
+                        gint         height)
+{
+  guchar *src  = src_buf;
+  guchar *dest = dest_buf;
+
+  mlib_image img_src, img_dst;
+
+  mlib_ImageSetStruct (&img_dst,
+                       MLIB_BYTE,
+                       4,
+                       width,
+                       height,
+                       dest_rowstride,
+                       dest_buf);
+
+  mlib_ImageSetStruct (&img_src,
+                       MLIB_BYTE,
+                       4,
+                       width,
+                       height,
+                       src_rowstride,
+                       src_buf);
+
+  if (dest_byte_order == GDK_LSB_FIRST)
+      mlib_ImageBlendRGBA2BGRA (&img_dst, &img_src);
+  else
+      mlib_ImageBlendRGBA2ARGB (&img_dst, &img_src);
+}
+#endif
+
 static void
 composite_565 (guchar      *src_buf,
               gint         src_rowstride,
@@ -1329,7 +1500,6 @@ gdk_drawable_real_draw_pixbuf (GdkDrawable  *drawable,
                               gint          x_dither,
                               gint          y_dither)
 {
-  gboolean free_gc = FALSE;
   GdkPixbuf *composited = NULL;
   gint dwidth, dheight;
   GdkRegion *clip;
@@ -1405,12 +1575,8 @@ gdk_drawable_real_draw_pixbuf (GdkDrawable  *drawable,
     return;
   
   /* Actually draw */
-
   if (!gc)
-    {
-      gc = gdk_gc_new (drawable);
-      free_gc = TRUE;
-    }
+    gc = _gdk_drawable_get_scratch_gc (drawable, FALSE);
   
   if (pixbuf->has_alpha)
     {
@@ -1441,7 +1607,16 @@ gdk_drawable_real_draw_pixbuf (GdkDrawable  *drawable,
                   visual->red_mask   == 0xff0000 &&
                   visual->green_mask == 0x00ff00 &&
                   visual->blue_mask  == 0x0000ff)
-           composite_func = composite_0888;
+           {
+#ifdef USE_MEDIALIB
+             if (_gdk_use_medialib ())
+               composite_func = composite_0888_medialib;
+             else
+               composite_func = composite_0888;
+#else
+             composite_func = composite_0888;
+#endif
+           }
        }
 
       /* We can't use our composite func if we are required to dither
@@ -1535,7 +1710,68 @@ gdk_drawable_real_draw_pixbuf (GdkDrawable  *drawable,
  out:
   if (composited)
     g_object_unref (composited);
+}
 
-  if (free_gc)
-    g_object_unref (gc);
+/************************************************************************/
+
+/**
+ * _gdk_drawable_get_scratch_gc:
+ * @drawable: A #GdkDrawable
+ * @graphics_exposures: Whether the returned #GdkGC should generate graphics exposures 
+ * 
+ * Returns a #GdkGC suitable for drawing on @drawable. The #GdkGC has
+ * the standard values for @drawable, except for the graphics_exposures
+ * field which is determined by the @graphics_exposures parameter.
+ *
+ * The foreground color of the returned #GdkGC is undefined. The #GdkGC
+ * must not be altered in any way, except to change its foreground color.
+ * 
+ * Return value: A #GdkGC suitable for drawing on @drawable
+ * 
+ * Since: 2.4
+ **/
+GdkGC *
+_gdk_drawable_get_scratch_gc (GdkDrawable *drawable,
+                             gboolean     graphics_exposures)
+{
+  GdkScreen *screen;
+  gint depth;
+
+  g_return_val_if_fail (GDK_IS_DRAWABLE (drawable), NULL);
+
+  screen = gdk_drawable_get_screen (drawable);
+
+  g_return_val_if_fail (!screen->closed, NULL);
+
+  depth = gdk_drawable_get_depth (drawable) - 1;
+
+  if (graphics_exposures)
+    {
+      if (!screen->exposure_gcs[depth])
+       {
+         GdkGCValues values;
+         GdkGCValuesMask mask;
+
+         values.graphics_exposures = TRUE;
+         mask = GDK_GC_EXPOSURES;  
+
+         screen->exposure_gcs[depth] =
+           gdk_gc_new_with_values (drawable, &values, mask);
+       }
+
+      return screen->exposure_gcs[depth];
+    }
+  else
+    {
+      if (!screen->normal_gcs[depth])
+       {
+         screen->normal_gcs[depth] =
+           gdk_gc_new (drawable);
+       }
+
+      return screen->normal_gcs[depth];
+    }
 }
+
+#define __GDK_DRAW_C__
+#include "gdkaliasdef.c"