gdkdrawable-x11.c 35.8 KB
Newer Older
1
/* GIMP Drawing Kit
2 3 4
 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
 *
 * This library is free software; you can redistribute it and/or
5
 * modify it under the terms of the GNU Lesser General Public
6 7 8 9 10 11
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * 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 16 17 18 19 20
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

/*
21
 * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
22 23 24 25 26
 * file for a list of people on the GTK+ Team.  See the ChangeLog
 * files for a list of changes.  These files are distributed with
 * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
 */

27
#include "gdkx.h"
28 29
#include "gdkregion-generic.h"

30
#include <pango/pangox.h>
31
#include <config.h>
32

33 34 35 36
#if HAVE_XFT
#include <pango/pangoxft.h>
#endif

37
#include <stdlib.h>
38
#include <string.h>		/* for memcpy() */
39

40 41 42
#if defined (HAVE_IPC_H) && defined (HAVE_SHM_H) && defined (HAVE_XSHM_H)
#define USE_SHM
#endif
43

44 45 46 47 48 49 50
#ifdef USE_SHM
#include <X11/extensions/XShm.h>
#endif /* USE_SHM */

#include "gdkprivate-x11.h"
#include "gdkdrawable-x11.h"
#include "gdkpixmap-x11.h"
51 52
#include "gdkscreen-x11.h"
#include "gdkdisplay-x11.h"
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

static void gdk_x11_draw_rectangle (GdkDrawable    *drawable,
				    GdkGC          *gc,
				    gint            filled,
				    gint            x,
				    gint            y,
				    gint            width,
				    gint            height);
static void gdk_x11_draw_arc       (GdkDrawable    *drawable,
				    GdkGC          *gc,
				    gint            filled,
				    gint            x,
				    gint            y,
				    gint            width,
				    gint            height,
				    gint            angle1,
				    gint            angle2);
static void gdk_x11_draw_polygon   (GdkDrawable    *drawable,
				    GdkGC          *gc,
				    gint            filled,
				    GdkPoint       *points,
				    gint            npoints);
static void gdk_x11_draw_text      (GdkDrawable    *drawable,
				    GdkFont        *font,
				    GdkGC          *gc,
				    gint            x,
				    gint            y,
				    const gchar    *text,
				    gint            text_length);
static void gdk_x11_draw_text_wc   (GdkDrawable    *drawable,
				    GdkFont        *font,
				    GdkGC          *gc,
				    gint            x,
				    gint            y,
				    const GdkWChar *text,
				    gint            text_length);
static void gdk_x11_draw_drawable  (GdkDrawable    *drawable,
				    GdkGC          *gc,
				    GdkPixmap      *src,
				    gint            xsrc,
				    gint            ysrc,
				    gint            xdest,
				    gint            ydest,
				    gint            width,
				    gint            height);
static void gdk_x11_draw_points    (GdkDrawable    *drawable,
				    GdkGC          *gc,
				    GdkPoint       *points,
				    gint            npoints);
static void gdk_x11_draw_segments  (GdkDrawable    *drawable,
				    GdkGC          *gc,
				    GdkSegment     *segs,
				    gint            nsegs);
static void gdk_x11_draw_lines     (GdkDrawable    *drawable,
				    GdkGC          *gc,
				    GdkPoint       *points,
				    gint            npoints);
static void gdk_x11_draw_glyphs    (GdkDrawable      *drawable,
                                    GdkGC            *gc,
                                    PangoFont        *font,
                                    gint              x,
                                    gint              y,
                                    PangoGlyphString *glyphs);
static void gdk_x11_draw_image     (GdkDrawable     *drawable,
                                    GdkGC           *gc,
                                    GdkImage        *image,
                                    gint             xsrc,
                                    gint             ysrc,
                                    gint             xdest,
                                    gint             ydest,
                                    gint             width,
                                    gint             height);
125 126 127 128 129 130 131 132 133 134 135 136 137 138
#ifdef HAVE_XFT
static void gdk_x11_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);
#endif /* HAVE_XFT */
139 140 141 142 143 144

static void gdk_x11_set_colormap   (GdkDrawable    *drawable,
                                    GdkColormap    *colormap);

static GdkColormap* gdk_x11_get_colormap   (GdkDrawable    *drawable);
static gint         gdk_x11_get_depth      (GdkDrawable    *drawable);
145
static GdkScreen *  gdk_x11_get_screen	   (GdkDrawable    *drawable);
Havoc Pennington's avatar
Havoc Pennington committed
146 147
static GdkVisual*   gdk_x11_get_visual     (GdkDrawable    *drawable);

148 149
static void gdk_drawable_impl_x11_class_init (GdkDrawableImplX11Class *klass);

150 151
static void gdk_drawable_impl_x11_finalize   (GObject *object);

152 153 154
static gpointer parent_class = NULL;

GType
155
_gdk_drawable_impl_x11_get_type (void)
156
{
157
  static GType object_type = 0;
158

159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
  if (!object_type)
    {
      static const GTypeInfo object_info =
      {
        sizeof (GdkDrawableImplX11Class),
        (GBaseInitFunc) NULL,
        (GBaseFinalizeFunc) NULL,
        (GClassInitFunc) gdk_drawable_impl_x11_class_init,
        NULL,           /* class_finalize */
        NULL,           /* class_data */
        sizeof (GdkDrawableImplX11),
        0,              /* n_preallocs */
        (GInstanceInitFunc) NULL,
      };
      
      object_type = g_type_register_static (GDK_TYPE_DRAWABLE,
                                            "GdkDrawableImplX11",
176
                                            &object_info, 0);
177 178
    }
  
179
  return object_type;
180 181
}

182 183
static void
gdk_drawable_impl_x11_class_init (GdkDrawableImplX11Class *klass)
184
{
185
  GdkDrawableClass *drawable_class = GDK_DRAWABLE_CLASS (klass);
186 187
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
  
188 189
  parent_class = g_type_class_peek_parent (klass);

190 191
  object_class->finalize = gdk_drawable_impl_x11_finalize;
  
192 193 194 195 196 197 198 199 200 201 202 203
  drawable_class->create_gc = _gdk_x11_gc_new;
  drawable_class->draw_rectangle = gdk_x11_draw_rectangle;
  drawable_class->draw_arc = gdk_x11_draw_arc;
  drawable_class->draw_polygon = gdk_x11_draw_polygon;
  drawable_class->draw_text = gdk_x11_draw_text;
  drawable_class->draw_text_wc = gdk_x11_draw_text_wc;
  drawable_class->draw_drawable = gdk_x11_draw_drawable;
  drawable_class->draw_points = gdk_x11_draw_points;
  drawable_class->draw_segments = gdk_x11_draw_segments;
  drawable_class->draw_lines = gdk_x11_draw_lines;
  drawable_class->draw_glyphs = gdk_x11_draw_glyphs;
  drawable_class->draw_image = gdk_x11_draw_image;
204
#ifdef HAVE_XFT  
205
  drawable_class->draw_pixbuf = gdk_x11_draw_pixbuf;
206
#endif /* HAVE_XFT */
207
  
208 209
  drawable_class->set_colormap = gdk_x11_set_colormap;
  drawable_class->get_colormap = gdk_x11_get_colormap;
210

211
  drawable_class->get_depth = gdk_x11_get_depth;
212
  drawable_class->get_screen = gdk_x11_get_screen;
Havoc Pennington's avatar
Havoc Pennington committed
213 214
  drawable_class->get_visual = gdk_x11_get_visual;
  
215
  drawable_class->_copy_to_image = _gdk_x11_copy_to_image;
216
}
217

218 219 220 221 222 223 224 225
static void
gdk_drawable_impl_x11_finalize (GObject *object)
{
  gdk_drawable_set_colormap (GDK_DRAWABLE (object), NULL);

  G_OBJECT_CLASS (parent_class)->finalize (object);
}

226
#ifdef HAVE_XFT
227
gboolean
228
_gdk_x11_have_render (GdkDisplay *display)
229 230 231 232 233 234
{
  /* This check is cheap, but if we have to do version checks, we will
   * need to cache the result since version checks are round-trip
   */
  int event_base, error_base;

235 236
  return XRenderQueryExtension (GDK_DISPLAY_XDISPLAY (display), 
				&event_base, &error_base);
237 238
}

239 240
static XftDraw *
gdk_x11_drawable_get_xft_draw (GdkDrawable *drawable)
241 242 243
{
  GdkDrawableImplX11 *impl = GDK_DRAWABLE_IMPL_X11 (drawable);

244
   if (impl->xft_draw == NULL)
245
    {
246 247
      GdkColormap *colormap = gdk_drawable_get_colormap (drawable);
      GdkVisual *visual;
248

249
      if (!colormap)
250 251 252 253 254 255
	{
	  g_warning ("Using Xft rendering requires the drawable argument to\n"
		     "have a specified colormap. All windows have a colormap,\n"
		     "however, pixmaps only have colormap by default if they\n"
		     "were created with a non-NULL window argument. Otherwise\n"
		     "a colormap must be set on them with gdk_drawable_set_colormap");
256
 	  return NULL;
257 258
	}

259 260 261 262
      visual = gdk_colormap_get_visual (colormap);
      
      impl->xft_draw = XftDrawCreate (GDK_SCREEN_XDISPLAY (impl->screen), impl->xid,
 				      GDK_VISUAL_XVISUAL (visual), GDK_COLORMAP_XCOLORMAP (colormap));
263 264
    }

265
   return impl->xft_draw;
266 267
}

268 269 270 271 272 273 274 275
static Picture
gdk_x11_drawable_get_picture (GdkDrawable *drawable)
{
  XftDraw *draw = gdk_x11_drawable_get_xft_draw (drawable);

  return draw ? XftDrawPicture (draw) : None;
}
  
276
static void
277 278
gdk_x11_drawable_update_xft_clip (GdkDrawable *drawable,
				  GdkGC       *gc)
279
{
280
  GdkGCX11 *gc_private = gc ? GDK_GC_X11 (gc) : NULL;
281
  XftDraw *xft_draw = gdk_x11_drawable_get_xft_draw (drawable);
282

283
  if (gc && gc_private->clip_region)
284 285 286
    {
      GdkRegionBox *boxes = gc_private->clip_region->rects;
      gint n_boxes = gc_private->clip_region->numRects;
287
#if 0				/* Until XftDrawSetClipRectangles is there */
288 289 290 291 292 293 294 295 296 297
      XRectangle *rects = g_new (XRectangle, n_boxes);
      int i;

      for (i=0; i < n_boxes; i++)
	{
	  rects[i].x = CLAMP (boxes[i].x1 + gc->clip_x_origin, G_MINSHORT, G_MAXSHORT);
	  rects[i].y = CLAMP (boxes[i].y1 + gc->clip_y_origin, G_MINSHORT, G_MAXSHORT);
	  rects[i].width = CLAMP (boxes[i].x2 + gc->clip_x_origin, G_MINSHORT, G_MAXSHORT) - rects[i].x;
	  rects[i].height = CLAMP (boxes[i].y2 + gc->clip_y_origin, G_MINSHORT, G_MAXSHORT) - rects[i].y;
	}
298
      XftDrawSetClipRectangles (xft_draw, 0, 0, rects, n_boxes);
299 300

      g_free (rects);
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
#else
      Region xregion = XCreateRegion ();
      int i;
 
      for (i=0; i < n_boxes; i++)
 	{
 	  XRectangle rect;
 	  
 	  rect.x = CLAMP (boxes[i].x1 + gc->clip_x_origin, G_MINSHORT, G_MAXSHORT);
 	  rect.y = CLAMP (boxes[i].y1 + gc->clip_y_origin, G_MINSHORT, G_MAXSHORT);
 	  rect.width = CLAMP (boxes[i].x2 + gc->clip_x_origin, G_MINSHORT, G_MAXSHORT) - rect.x;
 	  rect.height = CLAMP (boxes[i].y2 + gc->clip_y_origin, G_MINSHORT, G_MAXSHORT) - rect.y;
	  
 	  XUnionRectWithRegion (&rect, xregion, xregion);
 	}
      
      XftDrawSetClip (xft_draw, xregion);
      XDestroyRegion (xregion);
#endif      
320 321 322
    }
  else
    {
323
      XftDrawSetClip (xft_draw, NULL);
324 325 326 327
    }
}
#endif  

328 329 330
/*****************************************************
 * X11 specific implementations of generic functions *
 *****************************************************/
331

332 333 334 335 336 337 338 339
static GdkColormap*
gdk_x11_get_colormap (GdkDrawable *drawable)
{
  GdkDrawableImplX11 *impl;

  impl = GDK_DRAWABLE_IMPL_X11 (drawable);

  return impl->colormap;
340 341
}

342 343 344
static void
gdk_x11_set_colormap (GdkDrawable *drawable,
                      GdkColormap *colormap)
345
{
346 347 348 349 350 351
  GdkDrawableImplX11 *impl;

  impl = GDK_DRAWABLE_IMPL_X11 (drawable);

  if (impl->colormap == colormap)
    return;
352
  
353
  if (impl->colormap)
354
    g_object_unref (impl->colormap);
355 356
  impl->colormap = colormap;
  if (impl->colormap)
357
    g_object_ref (impl->colormap);
358 359
}

360 361 362
/* Drawing
 */

363 364 365 366 367 368 369 370 371
static void
gdk_x11_draw_rectangle (GdkDrawable *drawable,
			GdkGC       *gc,
			gint         filled,
			gint         x,
			gint         y,
			gint         width,
			gint         height)
{
372 373 374 375
  GdkDrawableImplX11 *impl;

  impl = GDK_DRAWABLE_IMPL_X11 (drawable);
  
376
  if (filled)
377
    XFillRectangle (GDK_SCREEN_XDISPLAY (impl->screen), impl->xid,
378
		    GDK_GC_GET_XGC (gc), x, y, width, height);
379
  else
380
    XDrawRectangle (GDK_SCREEN_XDISPLAY (impl->screen), impl->xid,
381
		    GDK_GC_GET_XGC (gc), x, y, width, height);
382 383 384 385 386 387 388 389 390 391 392 393 394
}

static void
gdk_x11_draw_arc (GdkDrawable *drawable,
		  GdkGC       *gc,
		  gint         filled,
		  gint         x,
		  gint         y,
		  gint         width,
		  gint         height,
		  gint         angle1,
		  gint         angle2)
{
395 396 397 398 399
  GdkDrawableImplX11 *impl;

  impl = GDK_DRAWABLE_IMPL_X11 (drawable);

  
400
  if (filled)
401
    XFillArc (GDK_SCREEN_XDISPLAY (impl->screen), impl->xid,
402
	      GDK_GC_GET_XGC (gc), x, y, width, height, angle1, angle2);
403
  else
404
    XDrawArc (GDK_SCREEN_XDISPLAY (impl->screen), impl->xid,
405
	      GDK_GC_GET_XGC (gc), x, y, width, height, angle1, angle2);
406 407 408 409 410 411 412 413 414
}

static void
gdk_x11_draw_polygon (GdkDrawable *drawable,
		      GdkGC       *gc,
		      gint         filled,
		      GdkPoint    *points,
		      gint         npoints)
{
415 416
  XPoint *tmp_points;
  gint tmp_npoints, i;
417
  GdkDrawableImplX11 *impl;
418

419 420 421
  impl = GDK_DRAWABLE_IMPL_X11 (drawable);

  
422 423
  if (!filled &&
      (points[0].x != points[npoints-1].x || points[0].y != points[npoints-1].y))
424
    {
425 426 427 428
      tmp_npoints = npoints + 1;
      tmp_points = g_new (XPoint, tmp_npoints);
      tmp_points[npoints].x = points[0].x;
      tmp_points[npoints].y = points[0].y;
429 430 431
    }
  else
    {
432 433 434 435 436 437 438 439
      tmp_npoints = npoints;
      tmp_points = g_new (XPoint, tmp_npoints);
    }

  for (i=0; i<npoints; i++)
    {
      tmp_points[i].x = points[i].x;
      tmp_points[i].y = points[i].y;
440
    }
441 442
  
  if (filled)
443
    XFillPolygon (GDK_SCREEN_XDISPLAY (impl->screen), impl->xid,
444 445
		  GDK_GC_GET_XGC (gc), tmp_points, tmp_npoints, Complex, CoordModeOrigin);
  else
446
    XDrawLines (GDK_SCREEN_XDISPLAY (impl->screen), impl->xid,
447 448 449
		GDK_GC_GET_XGC (gc), tmp_points, tmp_npoints, CoordModeOrigin);

  g_free (tmp_points);
450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466
}

/* gdk_x11_draw_text
 *
 * Modified by Li-Da Lho to draw 16 bits and Multibyte strings
 *
 * Interface changed: add "GdkFont *font" to specify font or fontset explicitely
 */
static void
gdk_x11_draw_text (GdkDrawable *drawable,
		   GdkFont     *font,
		   GdkGC       *gc,
		   gint         x,
		   gint         y,
		   const gchar *text,
		   gint         text_length)
{
467
  GdkDrawableImplX11 *impl;
468
  Display *xdisplay;
469 470

  impl = GDK_DRAWABLE_IMPL_X11 (drawable);
471
  xdisplay = GDK_SCREEN_XDISPLAY (impl->screen);
472
  
473 474 475
  if (font->type == GDK_FONT_FONT)
    {
      XFontStruct *xfont = (XFontStruct *) GDK_FONT_XFONT (font);
476
      XSetFont(xdisplay, GDK_GC_GET_XGC (gc), xfont->fid);
477 478
      if ((xfont->min_byte1 == 0) && (xfont->max_byte1 == 0))
	{
479
	  XDrawString (xdisplay, impl->xid,
480
		       GDK_GC_GET_XGC (gc), x, y, text, text_length);
481 482 483
	}
      else
	{
484
	  XDrawString16 (xdisplay, impl->xid,
485
			 GDK_GC_GET_XGC (gc), x, y, (XChar2b *) text, text_length / 2);
486 487 488 489 490
	}
    }
  else if (font->type == GDK_FONT_FONTSET)
    {
      XFontSet fontset = (XFontSet) GDK_FONT_XFONT (font);
491
      XmbDrawString (xdisplay, impl->xid,
492
		     fontset, GDK_GC_GET_XGC (gc), x, y, text, text_length);
493 494 495 496 497 498 499 500 501 502 503 504 505 506
    }
  else
    g_error("undefined font type\n");
}

static void
gdk_x11_draw_text_wc (GdkDrawable    *drawable,
		      GdkFont	     *font,
		      GdkGC	     *gc,
		      gint	      x,
		      gint	      y,
		      const GdkWChar *text,
		      gint	      text_length)
{
507
  GdkDrawableImplX11 *impl;
508
  Display *xdisplay;
509 510

  impl = GDK_DRAWABLE_IMPL_X11 (drawable);
511
  xdisplay = GDK_SCREEN_XDISPLAY (impl->screen);
512
  
513 514 515 516 517
  if (font->type == GDK_FONT_FONT)
    {
      XFontStruct *xfont = (XFontStruct *) GDK_FONT_XFONT (font);
      gchar *text_8bit;
      gint i;
518
      XSetFont(xdisplay, GDK_GC_GET_XGC (gc), xfont->fid);
519 520
      text_8bit = g_new (gchar, text_length);
      for (i=0; i<text_length; i++) text_8bit[i] = text[i];
521
      XDrawString (xdisplay, impl->xid,
522
                   GDK_GC_GET_XGC (gc), x, y, text_8bit, text_length);
523 524 525 526 527 528
      g_free (text_8bit);
    }
  else if (font->type == GDK_FONT_FONTSET)
    {
      if (sizeof(GdkWChar) == sizeof(wchar_t))
	{
529
	  XwcDrawString (xdisplay, impl->xid,
530
			 (XFontSet) GDK_FONT_XFONT (font),
531
			 GDK_GC_GET_XGC (gc), x, y, (wchar_t *)text, text_length);
532 533 534 535 536 537 538
	}
      else
	{
	  wchar_t *text_wchar;
	  gint i;
	  text_wchar = g_new (wchar_t, text_length);
	  for (i=0; i<text_length; i++) text_wchar[i] = text[i];
539
	  XwcDrawString (xdisplay, impl->xid,
540
			 (XFontSet) GDK_FONT_XFONT (font),
541
			 GDK_GC_GET_XGC (gc), x, y, text_wchar, text_length);
542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559
	  g_free (text_wchar);
	}
    }
  else
    g_error("undefined font type\n");
}

static void
gdk_x11_draw_drawable (GdkDrawable *drawable,
		       GdkGC       *gc,
		       GdkPixmap   *src,
		       gint         xsrc,
		       gint         ysrc,
		       gint         xdest,
		       gint         ydest,
		       gint         width,
		       gint         height)
{
560 561
  int src_depth = gdk_drawable_get_depth (src);
  int dest_depth = gdk_drawable_get_depth (drawable);
562
  GdkDrawableImplX11 *impl;
Havoc Pennington's avatar
Havoc Pennington committed
563 564
  GdkDrawableImplX11 *src_impl;
  
565
  impl = GDK_DRAWABLE_IMPL_X11 (drawable);
Havoc Pennington's avatar
Havoc Pennington committed
566 567 568 569 570

  if (GDK_IS_DRAWABLE_IMPL_X11 (src))
    src_impl = GDK_DRAWABLE_IMPL_X11 (src);
  else
    src_impl = NULL;
571
  
572
  if (src_depth == 1)
573
    {
574
      XCopyArea (GDK_SCREEN_XDISPLAY (impl->screen),
Havoc Pennington's avatar
Havoc Pennington committed
575
                 src_impl ? src_impl->xid : GDK_DRAWABLE_XID (src),
576
		 impl->xid,
577
		 GDK_GC_GET_XGC (gc),
578 579 580 581
		 xsrc, ysrc,
		 width, height,
		 xdest, ydest);
    }
582
  else if (dest_depth != 0 && src_depth == dest_depth)
583
    {
584
      XCopyArea (GDK_SCREEN_XDISPLAY (impl->screen),
Havoc Pennington's avatar
Havoc Pennington committed
585
                 src_impl ? src_impl->xid : GDK_DRAWABLE_XID (src),
586
		 impl->xid,
587
		 GDK_GC_GET_XGC (gc),
588 589 590 591
		 xsrc, ysrc,
		 width, height,
		 xdest, ydest);
    }
592
  else
Havoc Pennington's avatar
Havoc Pennington committed
593 594
    g_warning ("Attempt to draw a drawable with depth %d to a drawable with depth %d",
               src_depth, dest_depth);
595 596 597 598 599 600 601 602
}

static void
gdk_x11_draw_points (GdkDrawable *drawable,
		     GdkGC       *gc,
		     GdkPoint    *points,
		     gint         npoints)
{
603 604 605 606 607
  GdkDrawableImplX11 *impl;

  impl = GDK_DRAWABLE_IMPL_X11 (drawable);

  
608 609 610 611 612
  /* We special-case npoints == 1, because X will merge multiple
   * consecutive XDrawPoint requests into a PolyPoint request
   */
  if (npoints == 1)
    {
613
      XDrawPoint (GDK_SCREEN_XDISPLAY (impl->screen),
614
		  impl->xid,
615
		  GDK_GC_GET_XGC (gc),
616 617 618 619
		  points[0].x, points[0].y);
    }
  else
    {
620 621 622 623 624 625 626 627 628
      gint i;
      XPoint *tmp_points = g_new (XPoint, npoints);

      for (i=0; i<npoints; i++)
	{
	  tmp_points[i].x = points[i].x;
	  tmp_points[i].y = points[i].y;
	}
      
629
      XDrawPoints (GDK_SCREEN_XDISPLAY (impl->screen),
630
		   impl->xid,
631 632
		   GDK_GC_GET_XGC (gc),
		   tmp_points,
633 634
		   npoints,
		   CoordModeOrigin);
635 636

      g_free (tmp_points);
637 638 639 640 641 642 643 644 645
    }
}

static void
gdk_x11_draw_segments (GdkDrawable *drawable,
		       GdkGC       *gc,
		       GdkSegment  *segs,
		       gint         nsegs)
{
646 647 648 649 650
  GdkDrawableImplX11 *impl;

  impl = GDK_DRAWABLE_IMPL_X11 (drawable);

  
651 652 653 654 655
  /* We special-case nsegs == 1, because X will merge multiple
   * consecutive XDrawLine requests into a PolySegment request
   */
  if (nsegs == 1)
    {
656
      XDrawLine (GDK_SCREEN_XDISPLAY (impl->screen), impl->xid,
657
		 GDK_GC_GET_XGC (gc), segs[0].x1, segs[0].y1,
658 659 660 661
		 segs[0].x2, segs[0].y2);
    }
  else
    {
662 663 664 665 666 667 668 669 670 671 672
      gint i;
      XSegment *tmp_segs = g_new (XSegment, nsegs);

      for (i=0; i<nsegs; i++)
	{
	  tmp_segs[i].x1 = segs[i].x1;
	  tmp_segs[i].x2 = segs[i].x2;
	  tmp_segs[i].y1 = segs[i].y1;
	  tmp_segs[i].y2 = segs[i].y2;
	}
      
673
      XDrawSegments (GDK_SCREEN_XDISPLAY (impl->screen),
674
		     impl->xid,
675 676 677 678
		     GDK_GC_GET_XGC (gc),
		     tmp_segs, nsegs);

      g_free (tmp_segs);
679 680 681 682 683 684 685 686 687
    }
}

static void
gdk_x11_draw_lines (GdkDrawable *drawable,
		    GdkGC       *gc,
		    GdkPoint    *points,
		    gint         npoints)
{
688 689
  gint i;
  XPoint *tmp_points = g_new (XPoint, npoints);
690 691 692
  GdkDrawableImplX11 *impl;

  impl = GDK_DRAWABLE_IMPL_X11 (drawable);
693

694
  
695 696 697 698 699 700
  for (i=0; i<npoints; i++)
    {
      tmp_points[i].x = points[i].x;
      tmp_points[i].y = points[i].y;
    }
      
701
  XDrawLines (GDK_SCREEN_XDISPLAY (impl->screen),
702
	      impl->xid,
703 704
	      GDK_GC_GET_XGC (gc),
	      tmp_points, npoints,
705
	      CoordModeOrigin);
706 707

  g_free (tmp_points);
708
}
709 710 711 712 713 714 715 716 717

static void
gdk_x11_draw_glyphs (GdkDrawable      *drawable,
		     GdkGC            *gc,
		     PangoFont        *font,
		     gint              x,
		     gint              y,
		     PangoGlyphString *glyphs)
{
718 719 720 721
  GdkDrawableImplX11 *impl;

  impl = GDK_DRAWABLE_IMPL_X11 (drawable);

722 723 724
#if HAVE_XFT
  if (PANGO_XFT_IS_FONT (font))
    {
725 726 727 728 729 730 731
      XftColor color;
      XftDraw *draw;
 
      _gdk_gc_x11_get_fg_xft_color (gc, &color);
       
      gdk_x11_drawable_update_xft_clip (drawable, gc);
      draw = gdk_x11_drawable_get_xft_draw (drawable);
732
      
733
      pango_xft_render (draw, &color, font, glyphs, x, y);
734 735 736
    }
  else
#endif  /* !HAVE_XFT */
737
    pango_x_render (GDK_SCREEN_XDISPLAY (impl->screen),
738 739 740
		    impl->xid,
		    GDK_GC_GET_XGC (gc),
		    font, glyphs, x, y);
741
}
742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758

static void
gdk_x11_draw_image     (GdkDrawable     *drawable,
                        GdkGC           *gc,
                        GdkImage        *image,
                        gint             xsrc,
                        gint             ysrc,
                        gint             xdest,
                        gint             ydest,
                        gint             width,
                        gint             height)
{
  GdkDrawableImplX11 *impl;
  
  impl = GDK_DRAWABLE_IMPL_X11 (drawable);

  if (image->type == GDK_IMAGE_SHARED)
759
    XShmPutImage (GDK_SCREEN_XDISPLAY (impl->screen), impl->xid,
760 761 762
                  GDK_GC_GET_XGC (gc), GDK_IMAGE_XIMAGE (image),
                  xsrc, ysrc, xdest, ydest, width, height, False);
  else
763
    XPutImage (GDK_SCREEN_XDISPLAY (impl->screen), impl->xid,
764 765 766 767 768 769 770 771 772 773 774 775
               GDK_GC_GET_XGC (gc), GDK_IMAGE_XIMAGE (image),
               xsrc, ysrc, xdest, ydest, width, height);
}

static gint
gdk_x11_get_depth (GdkDrawable *drawable)
{
  /* This is a bit bogus but I'm not sure the other way is better */

  return gdk_drawable_get_depth (GDK_DRAWABLE_IMPL_X11 (drawable)->wrapper);
}

776

777
static GdkDrawable * get_impl_drawable (GdkDrawable *drawable)
778 779 780 781 782 783 784 785 786 787
{
  GdkDrawable *impl;
  
  if (GDK_IS_WINDOW (drawable))
    impl = ((GdkPixmapObject *)drawable)->impl;
  else if (GDK_IS_PIXMAP (drawable))
    impl = ((GdkPixmapObject *)drawable)->impl;
  else
    {
      g_warning (G_STRLOC " drawable is not a pixmap or window");
788
      return None;
789
    }
790 791 792 793 794 795 796 797 798 799 800 801
  return impl;
}


static GdkScreen*
gdk_x11_get_screen (GdkDrawable *drawable)
{
  if (GDK_IS_DRAWABLE_IMPL_X11 (drawable))
    return GDK_DRAWABLE_IMPL_X11 (drawable)->screen;
  else
    return GDK_DRAWABLE_IMPL_X11 (get_impl_drawable (drawable))->screen;
}
802

803 804 805 806 807 808 809 810 811 812 813 814 815
static GdkVisual*
gdk_x11_get_visual (GdkDrawable    *drawable)
{
  return gdk_drawable_get_visual (GDK_DRAWABLE_IMPL_X11 (drawable)->wrapper);
}

Display *
gdk_x11_drawable_get_xdisplay (GdkDrawable *drawable)
{
  if (GDK_IS_DRAWABLE_IMPL_X11 (drawable))
    return GDK_SCREEN_XDISPLAY (GDK_DRAWABLE_IMPL_X11 (drawable)->screen);
  else
    return GDK_SCREEN_XDISPLAY (GDK_DRAWABLE_IMPL_X11 (get_impl_drawable (drawable))->screen);
816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834
}

XID
gdk_x11_drawable_get_xid (GdkDrawable *drawable)
{
  GdkDrawable *impl;
  
  if (GDK_IS_WINDOW (drawable))
    impl = ((GdkPixmapObject *)drawable)->impl;
  else if (GDK_IS_PIXMAP (drawable))
    impl = ((GdkPixmapObject *)drawable)->impl;
  else
    {
      g_warning (G_STRLOC " drawable is not a pixmap or window");
      return None;
    }

  return ((GdkDrawableImplX11 *)impl)->xid;
}
835 836 837 838 839 840 841 842 843 844 845 846 847 848 849

/* Code for accelerated alpha compositing using the RENDER extension.
 * It's a bit long because there are lots of possibilities for
 * what's the fastest depending on the available picture formats,
 * whether we can used shared pixmaps, etc.
 */
#ifdef HAVE_XFT
typedef enum {
  FORMAT_NONE,
  FORMAT_EXACT_MASK,
  FORMAT_ARGB_MASK,
  FORMAT_ARGB
} FormatType;

static FormatType
850
select_format (GdkDisplay         *display,
851 852 853
	       XRenderPictFormat **format,
	       XRenderPictFormat **mask)
{
854
  Display *xdisplay = GDK_DISPLAY_XDISPLAY (display);
855 856
  XRenderPictFormat pf;

857
  if (!_gdk_x11_have_render (display))
858 859 860 861 862 863
    return FORMAT_NONE;
  
  /* Look for a 32-bit xRGB and Axxx formats that exactly match the
   * in memory data format. We can use them as pixmap and mask
   * to deal with non-premultiplied data.
   */
864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083

  pf.type = PictTypeDirect;
  pf.depth = 32;
  pf.direct.redMask = 0xff;
  pf.direct.greenMask = 0xff;
  pf.direct.blueMask = 0xff;
  
  pf.direct.alphaMask = 0;
  if (ImageByteOrder (xdisplay) == LSBFirst)
    {
      /* ABGR */
      pf.direct.red = 0;
      pf.direct.green = 8;
      pf.direct.blue = 16;
    }
  else
    {
      /* RGBA */
      pf.direct.red = 24;
      pf.direct.green = 16;
      pf.direct.blue = 8;
    }
  
  *format = XRenderFindFormat (xdisplay,
			       (PictFormatType | PictFormatDepth |
				PictFormatRedMask | PictFormatRed |
				PictFormatGreenMask | PictFormatGreen |
				PictFormatBlueMask | PictFormatBlue |
				PictFormatAlphaMask),
			       &pf,
			       0);

  pf.direct.alphaMask = 0xff;
  if (ImageByteOrder (xdisplay) == LSBFirst)
    {
      /* ABGR */
      pf.direct.alpha = 24;
    }
  else
    {
      pf.direct.alpha = 0;
    }
  
  *mask = XRenderFindFormat (xdisplay,
			     (PictFormatType | PictFormatDepth |
			      PictFormatAlphaMask | PictFormatAlpha),
			     &pf,
			     0);

  if (*format && *mask)
    return FORMAT_EXACT_MASK;

  /* OK, that failed, now look for xRGB and Axxx formats in
   * RENDER's preferred order
   */
  pf.direct.alphaMask = 0;
  /* ARGB */
  pf.direct.red = 16;
  pf.direct.green = 8;
  pf.direct.blue = 0;
  
  *format = XRenderFindFormat (xdisplay,
			       (PictFormatType | PictFormatDepth |
				PictFormatRedMask | PictFormatRed |
				PictFormatGreenMask | PictFormatGreen |
				PictFormatBlueMask | PictFormatBlue |
				PictFormatAlphaMask),
			       &pf,
			       0);

  pf.direct.alphaMask = 0xff;
  pf.direct.alpha = 24;
  
  *mask = XRenderFindFormat (xdisplay,
			     (PictFormatType | PictFormatDepth |
			      PictFormatAlphaMask | PictFormatAlpha),
			     &pf,
			     0);

  if (*format && *mask)
    return FORMAT_ARGB_MASK;

  /* Finally, if neither of the above worked, fall back to
   * looking for combined ARGB -- we'll premultiply ourselves.
   */

  pf.type = PictTypeDirect;
  pf.depth = 32;
  pf.direct.red = 16;
  pf.direct.green = 8;
  pf.direct.blue = 0;
  pf.direct.alphaMask = 0xff;
  pf.direct.alpha = 24;

  *format = XRenderFindFormat (xdisplay,
			       (PictFormatType | PictFormatDepth |
				PictFormatRedMask | PictFormatRed |
				PictFormatGreenMask | PictFormatGreen |
				PictFormatBlueMask | PictFormatBlue |
				PictFormatAlphaMask | PictFormatAlpha),
			       &pf,
			       0);
  *mask = NULL;

  if (*format)
    return FORMAT_ARGB;

  return FORMAT_NONE;
}

#if 0
static void
list_formats (XRenderPictFormat *pf)
{
  gint i;
  
  for (i=0 ;; i++)
    {
      XRenderPictFormat *pf = XRenderFindFormat (impl->xdisplay, 0, NULL, i);
      if (pf)
	{
	  g_print ("%2d R-%#06x/%#06x G-%#06x/%#06x B-%#06x/%#06x A-%#06x/%#06x\n",
		   pf->depth,
		   pf->direct.red,
		   pf->direct.redMask,
		   pf->direct.green,
		   pf->direct.greenMask,
		   pf->direct.blue,
		   pf->direct.blueMask,
		   pf->direct.alpha,
		   pf->direct.alphaMask);
	}
      else
	break;
    }
}
#endif  

static void
convert_to_format (guchar        *src_buf,
		   gint           src_rowstride,
		   guchar        *dest_buf,
		   gint           dest_rowstride,
		   FormatType     dest_format,
		   GdkByteOrder   dest_byteorder,
		   gint           width,
		   gint           height)
{
  gint i;

  if (dest_format == FORMAT_EXACT_MASK &&
      src_rowstride == dest_rowstride)
    {
      memcpy (dest_buf, src_buf, height * src_rowstride);
      return;
    }
  
  for (i=0; i < height; i++)
    {
      switch (dest_format)
	{
	case FORMAT_EXACT_MASK:
	  {
	    memcpy (dest_buf + i * dest_rowstride,
		    src_buf + i * src_rowstride,
		    width * 4);
	    break;
	  }
	case FORMAT_ARGB_MASK:
	  {
	    guint *p = (guint *)(src_buf + i * src_rowstride);
	    guint *q = (guint *)(dest_buf + i * dest_rowstride);
	    guint *end = p + width;

#if G_BYTE_ORDER == G_LITTLE_ENDIAN	    
	    if (dest_byteorder == GDK_LSB_FIRST)
	      {
		/* ABGR => ARGB */
		
		while (p < end)
		  {
		    *q = ( (*p & 0xff00ff00) |
			  ((*p & 0x000000ff) << 16) |
			  ((*p & 0x00ff0000) >> 16));
		    q++;
		    p++;
		  }
	      }
	    else
	      {
		/* ABGR => BGRA */
		
		while (p < end)
		  {
		    *q = (((*p & 0xff000000) >> 24) |
			  ((*p & 0x00ffffff) << 8));
		    q++;
		    p++;
		  }
	      }
#else /* G_BYTE_ORDER == G_BIG_ENDIAN */
	    if (dest_byteorder == GDK_LSB_FIRST)
	      {
		/* RGBA => BGRA */
		
		while (p < end)
		  {
		    *q = ( (*p & 0x00ff00ff) |
			  ((*p & 0x0000ff00) << 16) |
			  ((*p & 0xff000000) >> 16));
		    q++;
		    p++;
		  }
	      }
	    else
	      {
		/* RGBA => ARGB */
		
		while (p < end)
		  {
1084 1085
		    *q = (((*p & 0xffffff00) >> 8) |
			  ((*p & 0x000000ff) << 24));
1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097
		    q++;
		    p++;
		  }
	      }
#endif /* G_BYTE_ORDER*/	    
	    break;
	  }
	case FORMAT_ARGB:
	  {
	    guchar *p = (src_buf + i * src_rowstride);
	    guchar *q = (dest_buf + i * dest_rowstride);
	    guchar *end = p + 4 * width;
1098
	    guint t1,t2,t3;
1099
	    
1100
#define MULT(d,c,a,t) G_STMT_START { t = c * a; d = ((t >> 8) + t) >> 8; } G_STMT_END
1101 1102 1103 1104 1105
	    
	    if (dest_byteorder == GDK_LSB_FIRST)
	      {
		while (p < end)
		  {
1106 1107 1108 1109
		    MULT(q[0], p[2], p[3], t1);
		    MULT(q[1], p[1], p[3], t2);
		    MULT(q[2], p[0], p[3], t3);
		    q[3] = p[3];
1110 1111 1112 1113 1114 1115 1116 1117
		    p += 4;
		    q += 4;
		  }
	      }
	    else
	      {
		while (p < end)
		  {
1118 1119 1120 1121
		    q[0] = p[3];
		    MULT(q[1], p[0], p[3], t1);
		    MULT(q[2], p[1], p[3], t2);
		    MULT(q[3], p[2], p[3], t3);
1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148
		    p += 4;
		    q += 4;
		  }
	      }
#undef MULT
	    break;
	  }
	case FORMAT_NONE:
	  g_assert_not_reached ();
	  break;
	}
    }
}

static void
draw_with_images (GdkDrawable       *drawable,
		  GdkGC             *gc,
		  FormatType         format_type,
		  XRenderPictFormat *format,
		  XRenderPictFormat *mask_format,
		  guchar            *src_rgb,
		  gint               src_rowstride,
		  gint               dest_x,
		  gint               dest_y,
		  gint               width,
		  gint               height)
{
1149 1150
  GdkScreen *screen = GDK_DRAWABLE_IMPL_X11 (drawable)->screen;
  Display *xdisplay = GDK_SCREEN_XDISPLAY (screen);
1151 1152 1153 1154 1155 1156 1157 1158
  GdkImage *image;
  GdkPixmap *pix;
  GdkGC *pix_gc;
  Picture pict;
  Picture dest_pict;
  Picture mask = None;
  gint x0, y0;

1159 1160
  pix = gdk_pixmap_new (gdk_screen_get_root_window (screen), width, height, 32);
						  
1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181
  pict = XRenderCreatePicture (xdisplay, 
			       GDK_PIXMAP_XID (pix),
			       format, 0, NULL);
  if (mask_format)
    mask = XRenderCreatePicture (xdisplay, 
				 GDK_PIXMAP_XID (pix),
				 mask_format, 0, NULL);

  dest_pict = gdk_x11_drawable_get_picture (drawable);
  
  pix_gc = gdk_gc_new (pix);

  for (y0 = 0; y0 < height; y0 += GDK_SCRATCH_IMAGE_HEIGHT)
    {
      gint height1 = MIN (height - y0, GDK_SCRATCH_IMAGE_HEIGHT);
      for (x0 = 0; x0 < width; x0 += GDK_SCRATCH_IMAGE_WIDTH)
	{
	  gint xs0, ys0;
	  
	  gint width1 = MIN (width - x0, GDK_SCRATCH_IMAGE_WIDTH);
	  
1182
	  image = _gdk_image_get_scratch (screen, width1, height1, 32, &xs0, &ys0);
1183 1184
	  
	  convert_to_format (src_rgb + y0 * src_rowstride + 4 * x0, src_rowstride,
1185
			     image->mem + ys0 * image->bpl + xs0 * image->bpp, image->bpl,
1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248