📄 gnu_java_awt_peer_gtk_gdkgraphics2d.c
字号:
/* gnu_java_awt_peer_gtk_GdkGraphics2d.c Copyright (C) 2003, 2005 Free Software Foundation, Inc. This file is part of GNU Classpath. GNU Classpath is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. GNU Classpath 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 General Public License for more details. You should have received a copy of the GNU General Public License along with GNU Classpath; see the file COPYING. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. Linking this library statically or dynamically with other modules is making a combined work based on this library. Thus, the terms and conditions of the GNU General Public License cover the whole combination. As a special exception, the copyright holders of this library give you permission to link this library with independent modules to produce an executable, regardless of the license terms of these independent modules, and to copy and distribute the resulting executable under terms of your choice, provided that you also meet, for each linked independent module, the terms and conditions of the license of that module. An independent module is a module which is not derived from or based on this library. If you modify this library, you may extend this exception to your version of the library, but you are not obligated to do so. If you do not wish to do so, delete this exception statement from your version. */#include "gtkcairopeer.h"#include "gdkfont.h"#include "gnu_java_awt_peer_gtk_GdkGraphics2D.h"#include <gdk/gdktypes.h>#include <gdk/gdkprivate.h>#include <gdk/gdkx.h>#include <X11/extensions/Xrender.h>#include <gdk-pixbuf/gdk-pixbuf.h>#include <gdk-pixbuf/gdk-pixdata.h>#include <cairo-ft.h>#include <cairo-xlib.h>#include <stdio.h>#include <stdlib.h>static jmethodID initComponentGraphics2DUnlockedID;voidcp_gtk_graphics2d_init_jni (void){ jclass gdkgraphics2d; JNIEnv *env = cp_gtk_gdk_env(); gdkgraphics2d = (*env)->FindClass (env, "gnu/java/awt/peer/gtk/GdkGraphics2D"); if ((*env)->ExceptionOccurred(env)) return; initComponentGraphics2DUnlockedID = (*cp_gtk_gdk_env())->GetMethodID (cp_gtk_gdk_env(), gdkgraphics2d, "initComponentGraphics2DUnlocked", "()V");}static struct state_table *native_graphics2d_state_table;#define NSA_G2D_INIT(env, clazz) \ native_graphics2d_state_table = cp_gtk_init_state_table (env, clazz)#define NSA_GET_G2D_PTR(env, obj) \ cp_gtk_get_state (env, obj, native_graphics2d_state_table)#define NSA_SET_G2D_PTR(env, obj, ptr) \ cp_gtk_set_state (env, obj, native_graphics2d_state_table, (void *)ptr)#define NSA_DEL_G2D_PTR(env, obj) \ cp_gtk_remove_state_slot (env, obj, native_graphics2d_state_table)JNIEXPORT void JNICALLJava_gnu_java_awt_peer_gtk_GdkGraphics2D_initStaticState (JNIEnv *env, jclass clazz){ gdk_threads_enter(); NSA_G2D_INIT (env, clazz); gdk_threads_leave();}/* these public final constants are part of the java2d public API, so we write them explicitly here to save fetching them from the constant pool all the time. */#ifndef min#define min(x,y) ((x) < (y) ? (x) : (y))#endifenum java_awt_alpha_composite_rule { java_awt_alpha_composite_CLEAR = 1, java_awt_alpha_composite_SRC = 2, java_awt_alpha_composite_SRC_OVER = 3, java_awt_alpha_composite_DST_OVER = 4, java_awt_alpha_composite_SRC_IN = 5, java_awt_alpha_composite_DST_IN = 6, java_awt_alpha_composite_SRC_OUT = 7, java_awt_alpha_composite_DST_OUT = 8, java_awt_alpha_composite_DST = 9, java_awt_alpha_composite_SRC_ATOP = 10, java_awt_alpha_composite_DST_ATOP = 11, java_awt_alpha_composite_XOR = 12 };enum java_awt_basic_stroke_join_rule { java_awt_basic_stroke_JOIN_MITER = 0, java_awt_basic_stroke_JOIN_ROUND = 1, java_awt_basic_stroke_JOIN_BEVEL = 2 };enum java_awt_basic_stroke_cap_rule { java_awt_basic_stroke_CAP_BUTT = 0, java_awt_basic_stroke_CAP_ROUND = 1, java_awt_basic_stroke_CAP_SQUARE = 2 };enum java_awt_geom_path_iterator_winding_rule { java_awt_geom_path_iterator_WIND_EVEN_ODD = 0, java_awt_geom_path_iterator_WIND_NON_ZERO = 1 };enum java_awt_rendering_hints_filter { java_awt_rendering_hints_VALUE_INTERPOLATION_NEAREST_NEIGHBOR = 0, java_awt_rendering_hints_VALUE_INTERPOLATION_BILINEAR = 1, java_awt_rendering_hints_VALUE_ALPHA_INTERPOLATION_SPEED = 2, java_awt_rendering_hints_VALUE_ALPHA_INTERPOLATION_QUALITY = 3, java_awt_rendering_hints_VALUE_ALPHA_INTERPOLATION_DEFAULT = 4 };static intpeer_is_disposed(JNIEnv *env, jobject obj){ static jfieldID fid = NULL; jclass cls; jobject peer; return 0; if (fid == NULL) { cls = (*env)->GetObjectClass(env, obj); fid = (*env)->GetFieldID(env, cls, "component", "Lgnu/java/awt/peer/gtk/GtkComponentPeer;"); } g_assert(fid != NULL); peer = (*env)->GetObjectField(env, obj, fid); if (peer == NULL || NSA_GET_PTR (env, peer) != NULL) return 0; else { return 1; }}static void grab_current_drawable (GtkWidget *widget, GdkDrawable **draw, GdkWindow **win){ g_assert (widget != NULL); g_assert (draw != NULL); g_assert (win != NULL); *win = widget->window; *draw = *win; gdk_window_get_internal_paint_info (*win, draw, 0, 0); g_object_ref (*draw);}static intx_server_has_render_extension (void){ int ev = 0, err = 0; return (int) XRenderQueryExtension (GDK_DISPLAY (), &ev, &err);}static voidinit_graphics2d_as_pixbuf (struct graphics2d *gr){ gint width, height; gint bits_per_sample = 8; gint total_channels = 4; gboolean has_alpha = TRUE; g_assert (gr != NULL); g_assert (gr->drawable != NULL); if (gr->debug) printf ("initializing graphics2d as pixbuf\n"); gdk_drawable_get_size (gr->drawable, &width, &height); gr->drawbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, has_alpha, bits_per_sample, width, height); g_assert (gr->drawbuf != NULL); g_assert (gdk_pixbuf_get_bits_per_sample (gr->drawbuf) == bits_per_sample); g_assert (gdk_pixbuf_get_n_channels (gr->drawbuf) == total_channels); gr->surface = cairo_image_surface_create_for_data (gdk_pixbuf_get_pixels (gr->drawbuf), CAIRO_FORMAT_ARGB32, gdk_pixbuf_get_width (gr->drawbuf), gdk_pixbuf_get_height (gr->drawbuf), gdk_pixbuf_get_rowstride (gr->drawbuf)); g_assert (gr->surface != NULL); gr->mode = MODE_DRAWABLE_NO_RENDER; if (gr->cr != NULL) cairo_destroy (gr->cr); gr->cr = cairo_create (gr->surface);}static voidinit_graphics2d_as_renderable (struct graphics2d *gr){ Drawable draw; Display * dpy; Visual * vis; g_assert (gr != NULL); g_assert (gr->drawable != NULL); gr->drawbuf = NULL; if (gr->debug) printf ("initializing graphics2d as renderable\n"); draw = gdk_x11_drawable_get_xid (gr->drawable); dpy = gdk_x11_drawable_get_xdisplay (gr->drawable); g_assert (dpy != NULL); vis = gdk_x11_visual_get_xvisual (gdk_drawable_get_visual (gr->drawable)); g_assert (vis != NULL); gr->surface = cairo_xlib_surface_create (dpy, draw, vis, gr->width, gr->height); g_assert (gr->surface != NULL); gr->mode = MODE_DRAWABLE_WITH_RENDER; if (gr->cr != NULL) cairo_destroy (gr->cr); gr->cr = cairo_create (gr->surface);}static voidbegin_drawing_operation (JNIEnv *env, struct graphics2d * gr){ g_assert(cairo_status (gr->cr) == CAIRO_STATUS_SUCCESS); switch (gr->mode) { case MODE_DRAWABLE_WITH_RENDER: break; case MODE_DRAWABLE_NO_RENDER: { gint drawable_width, drawable_height; gint pixbuf_width, pixbuf_height; gint width, height; gdk_drawable_get_size (gr->drawable, &drawable_width, &drawable_height); pixbuf_width = gdk_pixbuf_get_width (gr->drawbuf); pixbuf_height = gdk_pixbuf_get_height (gr->drawbuf); width = min (drawable_width, pixbuf_width); height = min (drawable_height, pixbuf_height); gdk_pixbuf_get_from_drawable (gr->drawbuf, /* destination pixbuf */ gr->drawable, NULL, /* colormap */ 0, 0, 0, 0, width, height); if (gr->debug) printf ("copied (%d, %d) pixels from GDK drawable to pixbuf\n", width, height); } break; case MODE_JAVA_ARRAY: { jboolean isCopy; gr->javabuf = (*env)->GetPrimitiveArrayCritical (env, gr->jarray, &isCopy); gr->isCopy |= isCopy; if (gr->isCopy) { /* Make sure that the pixel buffer copy is already initalized, i.e. we already failed to get direct access in initState. */ g_assert (gr->javabuf_copy != NULL); memcpy (gr->javabuf_copy, gr->javabuf, gr->width * gr->height * 4); } } break; }}static voidend_drawing_operation (JNIEnv *env, struct graphics2d * gr){ g_assert(cairo_status (gr->cr) == CAIRO_STATUS_SUCCESS); switch (gr->mode) { case MODE_DRAWABLE_WITH_RENDER: break; case MODE_DRAWABLE_NO_RENDER: { gint drawable_width, drawable_height; gint pixbuf_width, pixbuf_height; gint width, height; gdk_drawable_get_size (gr->drawable, &drawable_width, &drawable_height); pixbuf_width = gdk_pixbuf_get_width (gr->drawbuf); pixbuf_height = gdk_pixbuf_get_height (gr->drawbuf); width = min (drawable_width, pixbuf_width); height = min (drawable_height, pixbuf_height); gdk_draw_pixbuf (gr->drawable, NULL, gr->drawbuf, 0, 0, 0, 0, width, height, GDK_RGB_DITHER_NORMAL, 0, 0); if (gr->debug) printf ("copied (%d, %d) pixels from pixbuf to GDK drawable\n", width, height); } break; case MODE_JAVA_ARRAY: if (gr->isCopy) memcpy (gr->javabuf, gr->javabuf_copy, gr->width * gr->height * 4); (*env)->ReleasePrimitiveArrayCritical (env, gr->jarray, gr->javabuf, JNI_COMMIT); }}static void update_pattern_transform (struct graphics2d *gr){ cairo_matrix_t mat; g_assert (gr != NULL); if (gr->pattern == NULL) return; cairo_get_matrix (gr->cr, &mat); cairo_pattern_set_matrix (gr->pattern, &mat);}static voidcheck_for_debug (struct graphics2d *gr){ gr->debug = (gboolean)(getenv("DEBUGJ2D") != NULL);}static voidrealize_cb (GtkWidget *widget __attribute__ ((unused)), jobject peer){ (*cp_gtk_gdk_env())->CallVoidMethod (cp_gtk_gdk_env(), peer, initComponentGraphics2DUnlockedID);}JNIEXPORT void JNICALLJava_gnu_java_awt_peer_gtk_GdkGraphics2D_copyState (JNIEnv *env, jobject obj, jobject old){ struct graphics2d *g = NULL, *g_old = NULL; gdk_threads_enter(); g = (struct graphics2d *) g_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->debug = g_old->debug; g->mode = g_old->mode; g->width = g_old->width; g->height = g_old->height; if (g_old->mode == MODE_JAVA_ARRAY) { jint size = g->width * g->height * 4; g->jarray = (*env)->NewGlobalRef (env, g_old->jarray); g->javabuf = (*env)->GetIntArrayElements (env, g->jarray, &g->isCopy); g->isCopy = JNI_TRUE; g->javabuf_copy = (jint *) g_malloc (size); memcpy (g->javabuf_copy, g->javabuf, size); g->surface = cairo_image_surface_create_for_data ((unsigned char *) g->javabuf, CAIRO_FORMAT_ARGB32, g->width, g->height, g->width * 4); g_assert (g->surface != NULL); g->cr = cairo_create (g->surface); g_assert (g->cr != NULL); (*env)->ReleaseIntArrayElements (env, g->jarray, g->javabuf, JNI_ABORT); } else { g->drawable = g_old->drawable; g_object_ref (g->drawable); if (x_server_has_render_extension ()) init_graphics2d_as_renderable (g); else init_graphics2d_as_pixbuf (g); } if (g->pattern) cairo_pattern_set_filter (g->pattern, CAIRO_FILTER_FAST); NSA_SET_G2D_PTR (env, obj, g); gdk_threads_leave();}JNIEXPORT void JNICALLJava_gnu_java_awt_peer_gtk_GdkGraphics2D_initState___3III(JNIEnv *env, jobject obj, jintArray jarr, jint width, jint height){ struct graphics2d *gr = NULL; jint *cairobuf = NULL; gdk_threads_enter(); gr = (struct graphics2d *) g_malloc (sizeof (struct graphics2d)); g_assert (gr != NULL); memset (gr, 0, sizeof(struct graphics2d)); check_for_debug (gr); if (gr->debug) printf ("constructing java-backed image of size (%d,%d)\n", width, height); gr->width = width; gr->height = height; gr->jarray = (*env)->NewGlobalRef(env, jarr); gr->javabuf = (*env)->GetPrimitiveArrayCritical (env, gr->jarray, &gr->isCopy); if (gr->isCopy) { /* We didn't get direct access to the pixel buffer, so we'll have to maintain a separate copy for Cairo. */ jint size = gr->width * gr->height * 4; gr->javabuf_copy = (jint *) g_malloc (size); memcpy (gr->javabuf_copy, gr->javabuf, size); cairobuf = gr->javabuf_copy; } else { /* Have Cairo write directly to the Java array. */ cairobuf = gr->javabuf; } gr->surface = cairo_image_surface_create_for_data ((unsigned char *) cairobuf, CAIRO_FORMAT_ARGB32, gr->width, gr->height, gr->width * 4); g_assert (gr->surface != NULL); gr->cr = cairo_create (gr->surface); g_assert (gr->cr != NULL); (*env)->ReleasePrimitiveArrayCritical (env, gr->jarray, gr->javabuf, JNI_COMMIT); gr->mode = MODE_JAVA_ARRAY; if (gr->debug) printf ("constructed java-backed image of size (%d,%d)\n", width, height); NSA_SET_G2D_PTR (env, obj, gr); gdk_threads_leave(); }JNIEXPORT void JNICALLJava_gnu_java_awt_peer_gtk_GdkGraphics2D_initState__II
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -