JNIEXPORT void JNICALL Java_gnu_java_awt_peer_gtk_GdkGraphics2D_gdkDrawDrawable
  (JNIEnv *env, jobject self, jobject other, jint x, jint y)
{
  struct graphics2d *src = NULL, *dst = NULL;
  gint s_height, s_width, d_height, d_width, height, width;
  cairo_matrix_t *matrix;
  cairo_operator_t tmp_op;

  gdk_threads_enter();
  if (peer_is_disposed(env, self)) { gdk_threads_leave(); return; }
  src = (struct graphics2d *)NSA_GET_G2D_PTR (env, other);
  dst = (struct graphics2d *)NSA_GET_G2D_PTR (env, self);
  g_assert (src != NULL);
  g_assert (dst != NULL);  

  if (src->debug) printf ("copying from offscreen drawable\n");

  begin_drawing_operation(dst); 

  gdk_flush();

  gdk_drawable_get_size (src->drawable, &s_width, &s_height);
  gdk_drawable_get_size (dst->drawable, &d_width, &d_height);
  width = min (s_width, d_width);
  height = min (s_height, d_height);

  matrix = cairo_matrix_create ();
  cairo_surface_get_matrix (src->surface, matrix);
  cairo_matrix_translate (matrix, (double)-x, (double)-y);
  cairo_surface_set_matrix (src->surface, matrix);

  tmp_op = cairo_current_operator (dst->cr); 
  cairo_set_operator(dst->cr, CAIRO_OPERATOR_SRC); 
  cairo_show_surface (dst->cr, src->surface, width, height);
  cairo_set_operator(dst->cr, tmp_op);

  cairo_matrix_translate (matrix, (double)x, (double)y);
  cairo_surface_set_matrix (src->surface, matrix);
  cairo_matrix_destroy (matrix);

  gdk_flush();

  end_drawing_operation(dst);

  if (src->debug) printf ("copied %d x %d pixels from offscreen drawable\n", width, height);
  gdk_threads_leave();
}
JNIEXPORT void JNICALL Java_gnu_java_awt_peer_gtk_GdkGraphics2D_drawPixels 
  (JNIEnv *env, jobject obj, jintArray java_pixels, 
   jint w, jint h, jint stride, jdoubleArray java_matrix)
{
  struct graphics2d *gr = NULL;
  jint *native_pixels = NULL;
  jdouble *native_matrix = NULL;

  gr = (struct graphics2d *) NSA_GET_G2D_PTR (env, obj);
  g_assert (gr != NULL);

  if (gr->debug) printf ("drawPixels (%d pixels, %dx%d, stride: %d)\n",
			 (*env)->GetArrayLength (env, java_pixels), w, h, stride);

  native_pixels = (*env)->GetIntArrayElements (env, java_pixels, NULL);
  native_matrix = (*env)->GetDoubleArrayElements (env, java_matrix, NULL);
  g_assert (native_pixels != NULL);
  g_assert (native_matrix != NULL);
  g_assert ((*env)->GetArrayLength (env, java_matrix) == 6);

  begin_drawing_operation (gr);
  
 {
   cairo_matrix_t *mat = NULL;
   cairo_surface_t *surf = cairo_surface_create_for_image ((char *)native_pixels, 
							   CAIRO_FORMAT_ARGB32, 
							   w, h, stride * 4);   
   mat = cairo_matrix_create ();
   cairo_matrix_set_affine (mat, 
			    native_matrix[0], native_matrix[1],
			    native_matrix[2], native_matrix[3],
			    native_matrix[4], native_matrix[5]);
   cairo_surface_set_matrix (surf, mat);
   if (native_matrix[0] != 1.
       || native_matrix[1] != 0.
       || native_matrix[2] != 0.
       || native_matrix[3] != 1.)
     {
       cairo_surface_set_filter (surf, CAIRO_FILTER_BILINEAR);
       cairo_surface_set_filter (gr->surface, CAIRO_FILTER_BILINEAR);
     }
   else
     {
       cairo_surface_set_filter (surf, CAIRO_FILTER_FAST);
       cairo_surface_set_filter (gr->surface, CAIRO_FILTER_FAST);
     }
   cairo_show_surface (gr->cr, surf, w, h);
   cairo_surface_set_filter (gr->surface, CAIRO_FILTER_FAST);
   cairo_matrix_destroy (mat);
   cairo_surface_destroy (surf);
 }
  
 end_drawing_operation (gr);

  (*env)->ReleaseIntArrayElements (env, java_pixels, native_pixels, 0);
  (*env)->ReleaseDoubleArrayElements (env, java_matrix, native_matrix, 0);

}
static void 
update_pattern_transform (struct graphics2d *gr)
{
  double a, b, c, d, tx, ty;
  cairo_matrix_t *mat = NULL;

  g_assert (gr != NULL);
  if (gr->pattern == NULL)
    return;

  return;
  /* temporarily disabled: ambiguous behavior */
  /*   cairo_get_matrix (gr->cr, &a, &b, &c, &d, &tx, &ty); */
  mat = cairo_matrix_create ();
  g_assert (mat != NULL);
  cairo_matrix_set_affine (mat, a, b, c, d, tx, ty);
  cairo_surface_set_matrix (gr->pattern, mat);
  cairo_matrix_destroy (mat);
}
JNIEXPORT void JNICALL Java_gnu_java_awt_peer_gtk_GdkGraphics2D_setGradient 
  (JNIEnv *env, jobject obj, 
   jdouble x1, jdouble y1, 
   jdouble x2, jdouble y2,
   jint r1, jint g1, jint b1, jint a1,
   jint r2, jint g2, jint b2, jint a2,
   jboolean cyclic)
{
  struct graphics2d *gr = NULL;
  cairo_surface_t *surf = NULL;
  cairo_matrix_t *mat = NULL;
  gr = (struct graphics2d *) NSA_GET_G2D_PTR (env, obj);
  g_assert (gr != NULL);

  if (gr->debug) printf ("setGradient (%f,%f) -> (%f,%f); (%d,%d,%d,%d) -> (%d,%d,%d,%d)\n",
			 x1, y1, 
			 x2, y2, 
			 r1, g1, b1, a1, 
			 r2, g2, b2, a2);
  
  cairo_save (gr->cr);
  
  if (cyclic)
    surf = cairo_surface_create_similar (gr->surface, CAIRO_FORMAT_ARGB32, 3, 2);
  else
    surf = cairo_surface_create_similar (gr->surface, CAIRO_FORMAT_ARGB32, 2, 2);      
  g_assert (surf != NULL);

  cairo_set_target_surface (gr->cr, surf);
  
  cairo_identity_matrix (gr->cr);

  cairo_set_rgb_color (gr->cr, r1 / 255.0, g1 / 255.0, b1 / 255.0);
  cairo_set_alpha (gr->cr, a1 / 255.0);
  cairo_rectangle (gr->cr, 0, 0, 1, 2);
  cairo_fill (gr->cr);
    
  cairo_set_rgb_color (gr->cr, r2 / 255.0, g2 / 255.0, b2 / 255.0);
  cairo_set_alpha (gr->cr, a2 / 255.0);
  cairo_rectangle (gr->cr, 1, 0, 1, 2);
  cairo_fill (gr->cr);

  if (cyclic)
    {
      cairo_set_rgb_color (gr->cr, r1 / 255.0, g1 / 255.0, b1 / 255.0);
      cairo_set_alpha (gr->cr, a1 / 255.0);
      cairo_rectangle (gr->cr, 2, 0, 1, 2);
      cairo_fill (gr->cr);
    }

  mat = cairo_matrix_create ();
  g_assert (mat != NULL);

  /* 
     consider the vector [x2 - x1, y2 - y1] = [p,q]

     this is a line in space starting at an 'origin' x1, y1.

     it can also be thought of as a "transformed" unit vector in either the
     x or y directions. we have just *drawn* our gradient as a unit vector
     (well, a 2-3x unit vector) in the x dimension. so what we want to know
     is which transformation turns our existing unit vector into [p,q].

     which means solving for M in 
 
     [p,q] = M[1,0]

     [p,q] = |a b| [1,0]
             |c d|      

     [p,q] = [a,c], with b = d = 0.

     what does this mean? it means that our gradient is 1-dimensional; as
     you move through the x axis of our 2 or 3 pixel gradient from logical
     x positions 0 to 1, the transformation of your x coordinate under the
     matrix M causes you to accumulate both x and y values in fill
     space. the y value of a gradient coordinate is ignored, since the
     gradient is one dimensional. which is correct.

     unfortunately we want the opposite transformation, it seems, because of
     the way cairo is going to use this transformation. I'm a bit confused by
     that, but it seems to work right, so we take reciprocals of values and
     negate offsets. oh well.
     
   */

  double a = (x2 - x1 == 0.) ? 0. : ((cyclic ? 3.0 : 2.0) / (x2 - x1));
  double c = (y2 - y1 == 0.) ? 0. : (1. / (y2 - y1));
  double dx = (x1 == 0.) ? 0. : 1. / x1;
  double dy = (y1 == 0.) ? 0. : 1. / y1;

  cairo_matrix_set_affine (mat,
			   a, 0.,
			   c, 0.,
			   dx, dy);

  cairo_surface_set_matrix (surf, mat);
  cairo_matrix_destroy (mat);
  cairo_surface_set_filter (surf, CAIRO_FILTER_BILINEAR);

  /* FIXME: repeating gradients (not to mention hold gradients) don't seem to work. */
  /*   cairo_surface_set_repeat (surf, cyclic ? 1 : 0); */

  if (gr->pattern)
    cairo_surface_destroy (gr->pattern);

  if (gr->pattern_pixels)
    {
      free (gr->pattern_pixels);
      gr->pattern_pixels = NULL;
    }

  gr->pattern = surf;  

  cairo_restore (gr->cr);    
  cairo_set_pattern (gr->cr, gr->pattern);

}