JNIEXPORT void JNICALL Java_gnu_java_awt_peer_gtk_GdkGraphics2D_copyState
  (JNIEnv *env, jobject obj, jobject old)
{
  struct graphics2d *g = NULL, *g_old = NULL;

  g = (struct graphics2d *) malloc (sizeof (struct graphics2d));
  g_assert (g != NULL);
  memset (g, 0, sizeof(struct graphics2d));

  g_old = (struct graphics2d *) NSA_GET_G2D_PTR (env, old);
  g_assert (g_old != NULL);

  if (g_old->debug) printf ("copying state from existing graphics2d\n");

  g->drawable = g_old->drawable;
  g->debug = g_old->debug; 

  gdk_threads_enter ();
  g_object_ref (g->drawable);
  
  g->cr = cairo_create();
  g_assert (g->cr != NULL);

  if (x_server_has_render_extension ())
    init_graphics2d_as_renderable (g);
  else
    init_graphics2d_as_pixbuf (g);

  cairo_surface_set_filter (g->surface, CAIRO_FILTER_FAST);

  gdk_threads_leave ();

  NSA_SET_G2D_PTR (env, obj, g);
}
JNIEXPORT void JNICALL Java_gnu_java_awt_peer_gtk_GdkGraphics2D_initState__II
  (JNIEnv *env, jobject obj, jint width, jint height)
{
  struct graphics2d *gr;

  gdk_threads_enter ();
  
  gr = (struct graphics2d *) malloc (sizeof (struct graphics2d));
  g_assert (gr != NULL);
  memset (gr, 0, sizeof(struct graphics2d));

  check_for_debug (gr);  

  if (gr->debug) printf ("constructing offscreen drawable of size (%d,%d)\n",
			 width, height);
  
  gr->drawable = (GdkDrawable *) gdk_pixmap_new (NULL, width, height, 
						 gdk_rgb_get_visual ()->depth);
  g_assert (gr->drawable != NULL);

  gr->cr = cairo_create();
  g_assert (gr->cr != NULL);

  if (x_server_has_render_extension ())
    init_graphics2d_as_renderable (gr);
  else
    init_graphics2d_as_pixbuf (gr);

  gdk_threads_leave ();
  if (gr->debug) printf ("constructed offscreen drawable of size (%d,%d)\n",
			 width, height);
  NSA_SET_G2D_PTR (env, obj, gr);
}
JNIEXPORT void JNICALL Java_gnu_java_awt_peer_gtk_GdkGraphics2D_initState__Lgnu_java_awt_peer_gtk_GtkComponentPeer_2
  (JNIEnv *env, jobject obj, jobject peer)
{
  struct graphics2d *gr = NULL;
  GtkWidget *widget = NULL;
  void *ptr = NULL;

  gdk_threads_enter();
  if (peer_is_disposed(env, obj)) { gdk_threads_leave(); return; }
  ptr = NSA_GET_PTR (env, peer);
  g_assert (ptr != NULL);

  gr = (struct graphics2d *) malloc (sizeof (struct graphics2d));
  g_assert (gr != NULL);
  memset (gr, 0, sizeof(struct graphics2d));

  check_for_debug (gr);

  gr->cr = cairo_create();
  g_assert (gr->cr != NULL);

  widget = GTK_WIDGET (ptr);
  g_assert (widget != NULL);

  grab_current_drawable (widget, &(gr->drawable), &(gr->win));
  g_assert (gr->drawable != NULL);

  if (x_server_has_render_extension ())
    init_graphics2d_as_renderable (gr);
  else
    init_graphics2d_as_pixbuf (gr);

  NSA_SET_G2D_PTR (env, obj, gr);
  gdk_threads_leave();
}