Here comes a slight optimization for CairoGraphics2D efficiency wrt JNI calls. It adds special methods for 3 common graphics operations (drawLine, drawRect and fillRect), so that unnecessary JNI calls and object creations are avoided.
Here are some numbers, measured with a benchmark (to be added soon) that
draws 1000 of each operations with random sizes (the first number beeing
the old number of ms, the second after the patch).
drawRect: 316 vs 255
drawLine: 364 vs 309
fillRect: 255 vs 95
2006-06-14 Roman Kennke <[EMAIL PROTECTED]>
* gnu/java/awt/peer/gtk/CairoGraphics2D.java
(cairoDrawLine): New native method.
(cairoDrawRect): New native method.
(cairoFillRect): New native method.
(drawLine): Use special native method.
(drawRect): Use special native method.
(fillRect): Use special native method.
* native/jni/gtk-peer/gnu_java_awt_peer_gtk_CairoGraphics2D.c
(cairoDrawLine): New native method.
(cairoDrawRect): New native method.
(cairoFillRect): New native method.
* include/gnu_java_awt_peer_gtk_CairoGraphics2D.h: Regenerated.
/Roman
--
“Improvement makes straight roads, but the crooked roads, without
Improvement, are roads of Genius.” - William Blake
Index: native/jni/gtk-peer/gnu_java_awt_peer_gtk_CairoGraphics2D.c
===================================================================
RCS file: /cvsroot/classpath/classpath/native/jni/gtk-peer/gnu_java_awt_peer_gtk_CairoGraphics2D.c,v
retrieving revision 1.10
diff -u -1 -0 -r1.10 gnu_java_awt_peer_gtk_CairoGraphics2D.c
--- native/jni/gtk-peer/gnu_java_awt_peer_gtk_CairoGraphics2D.c 12 Jun 2006 10:32:44 -0000 1.10
+++ native/jni/gtk-peer/gnu_java_awt_peer_gtk_CairoGraphics2D.c 14 Jun 2006 13:46:36 -0000
@@ -658,20 +658,60 @@
break;
case java_awt_rendering_hints_VALUE_ALPHA_INTERPOLATION_DEFAULT:
cairo_pattern_set_filter (gr->pattern, CAIRO_FILTER_NEAREST);
break;
case java_awt_rendering_hints_VALUE_ALPHA_INTERPOLATION_QUALITY:
cairo_pattern_set_filter (gr->pattern, CAIRO_FILTER_BEST);
break;
}
}
+JNIEXPORT void JNICALL
+Java_gnu_java_awt_peer_gtk_CairoGraphics2D_cairoDrawLine
+(JNIEnv *env __attribute__ ((unused)), jobject obj __attribute__ ((unused)),
+ jlong pointer, jdouble x1, jdouble y1, jdouble x2, jdouble y2)
+{
+ struct cairographics2d *gr = JLONG_TO_PTR(struct cairographics2d, pointer);
+ g_assert (gr != NULL);
+
+ cairo_new_path(gr->cr);
+ cairo_move_to(gr->cr, x1, y1);
+ cairo_line_to(gr->cr, x2, y2);
+ cairo_stroke(gr->cr);
+}
+
+JNIEXPORT void JNICALL
+Java_gnu_java_awt_peer_gtk_CairoGraphics2D_cairoDrawRect
+(JNIEnv *env __attribute__ ((unused)), jobject obj __attribute__ ((unused)),
+ jlong pointer, jdouble x, jdouble y, jdouble w, jdouble h)
+{
+ struct cairographics2d *gr = JLONG_TO_PTR(struct cairographics2d, pointer);
+ g_assert (gr != NULL);
+
+ cairo_new_path(gr->cr);
+ cairo_rectangle(gr->cr, x, y, w, h);
+ cairo_stroke(gr->cr);
+}
+
+JNIEXPORT void JNICALL
+Java_gnu_java_awt_peer_gtk_CairoGraphics2D_cairoFillRect
+(JNIEnv *env __attribute__ ((unused)), jobject obj __attribute__ ((unused)),
+ jlong pointer, jdouble x, jdouble y, jdouble w, jdouble h)
+{
+ struct cairographics2d *gr = JLONG_TO_PTR(struct cairographics2d, pointer);
+ g_assert (gr != NULL);
+
+ cairo_new_path(gr->cr);
+ cairo_rectangle(gr->cr, x, y, w, h);
+ cairo_fill(gr->cr);
+}
+
/************************** FONT STUFF ****************************/
static void
install_font_peer(cairo_t *cr,
struct peerfont *pfont)
{
cairo_font_face_t *ft;
FT_Face face = NULL;
g_assert(cr != NULL);
Index: gnu/java/awt/peer/gtk/CairoGraphics2D.java
===================================================================
RCS file: /cvsroot/classpath/classpath/gnu/java/awt/peer/gtk/CairoGraphics2D.java,v
retrieving revision 1.22
diff -u -1 -0 -r1.22 CairoGraphics2D.java
--- gnu/java/awt/peer/gtk/CairoGraphics2D.java 13 Jun 2006 12:59:22 -0000 1.22
+++ gnu/java/awt/peer/gtk/CairoGraphics2D.java 14 Jun 2006 13:46:37 -0000
@@ -59,21 +59,20 @@
import java.awt.Polygon;
import java.awt.TexturePaint;
import java.awt.Toolkit;
import java.awt.font.FontRenderContext;
import java.awt.font.GlyphVector;
import java.awt.font.TextLayout;
import java.awt.geom.AffineTransform;
import java.awt.geom.Arc2D;
import java.awt.geom.Area;
import java.awt.geom.GeneralPath;
-import java.awt.geom.Line2D;
import java.awt.geom.NoninvertibleTransformException;
import java.awt.geom.PathIterator;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.awt.image.BufferedImageOp;
import java.awt.image.ColorModel;
import java.awt.image.DataBuffer;
@@ -420,20 +419,60 @@
/**
* Save clip
*/
private native void cairoResetClip(long pointer);
/**
* Set interpolation types
*/
private native void cairoSurfaceSetFilter(long pointer, int filter);
+ /**
+ * Draws a line from (x1,y1) to (x2,y2).
+ *
+ * @param pointer the native pointer
+ *
+ * @param x1 the x coordinate of the starting point
+ * @param y1 the y coordinate of the starting point
+ * @param x2 the x coordinate of the end point
+ * @param y2 the y coordinate of the end point
+ */
+ private native void cairoDrawLine(long pointer, double x1, double y1,
+ double x2, double y2);
+
+ /**
+ * Draws a rectangle at starting point (x,y) and with the specified width
+ * and height.
+ *
+ * @param pointer the native pointer
+ * @param x the x coordinate of the upper left corner
+ * @param y the y coordinate of the upper left corner
+ * @param w the width of the rectangle
+ * @param h the height of the rectangle
+ */
+ private native void cairoDrawRect(long pointer, double x, double y, double w,
+ double h);
+
+ /**
+ * Fills a rectangle at starting point (x,y) and with the specified width
+ * and height.
+ *
+ * @param pointer the native pointer
+ * @param x the x coordinate of the upper left corner
+ * @param y the y coordinate of the upper left corner
+ * @param w the width of the rectangle
+ * @param h the height of the rectangle
+ */
+ private native void cairoFillRect(long pointer, double x, double y, double w,
+ double h);
+
+
///////////////////////// TRANSFORMS ///////////////////////////////////
/**
* Set the current transform
*/
public void setTransform(AffineTransform tx)
{
// Transform clip into target space using the old transform.
updateClip(transform);
// Update the native transform.
@@ -933,39 +972,42 @@
public void drawArc(int x, int y, int width, int height, int startAngle,
int arcAngle)
{
draw(new Arc2D.Double((double) x, (double) y, (double) width,
(double) height, (double) startAngle,
(double) arcAngle, Arc2D.OPEN));
}
public void drawLine(int x1, int y1, int x2, int y2)
{
- draw(new Line2D.Double(x1, y1, x2, y2));
+ cairoDrawLine(nativePointer, shifted(x1, shiftDrawCalls),
+ shifted(y1, shiftDrawCalls), shifted(x2, shiftDrawCalls),
+ shifted(y2, shiftDrawCalls));
}
public void drawRect(int x, int y, int width, int height)
{
- draw(new Rectangle(x, y, width, height));
+ cairoDrawRect(nativePointer, shifted(x, shiftDrawCalls),
+ shifted(y, shiftDrawCalls), width, height);
}
public void fillArc(int x, int y, int width, int height, int startAngle,
int arcAngle)
{
fill(new Arc2D.Double((double) x, (double) y, (double) width,
(double) height, (double) startAngle,
(double) arcAngle, Arc2D.OPEN));
}
public void fillRect(int x, int y, int width, int height)
{
- fill(new Rectangle(x, y, width, height));
+ cairoFillRect(nativePointer, x, y, width, height);
}
public void fillPolygon(int[] xPoints, int[] yPoints, int nPoints)
{
fill(new Polygon(xPoints, yPoints, nPoints));
}
public void drawPolygon(int[] xPoints, int[] yPoints, int nPoints)
{
draw(new Polygon(xPoints, yPoints, nPoints));
Index: include/gnu_java_awt_peer_gtk_CairoGraphics2D.h
===================================================================
RCS file: /cvsroot/classpath/classpath/include/gnu_java_awt_peer_gtk_CairoGraphics2D.h,v
retrieving revision 1.6
diff -u -1 -0 -r1.6 gnu_java_awt_peer_gtk_CairoGraphics2D.h
--- include/gnu_java_awt_peer_gtk_CairoGraphics2D.h 12 Jun 2006 10:32:44 -0000 1.6
+++ include/gnu_java_awt_peer_gtk_CairoGraphics2D.h 14 Jun 2006 13:46:37 -0000
@@ -30,16 +30,19 @@
JNIEXPORT void JNICALL Java_gnu_java_awt_peer_gtk_CairoGraphics2D_cairoRelMoveTo (JNIEnv *env, jobject, jlong, jdouble, jdouble);
JNIEXPORT void JNICALL Java_gnu_java_awt_peer_gtk_CairoGraphics2D_cairoLineTo (JNIEnv *env, jobject, jlong, jdouble, jdouble);
JNIEXPORT void JNICALL Java_gnu_java_awt_peer_gtk_CairoGraphics2D_cairoRelLineTo (JNIEnv *env, jobject, jlong, jdouble, jdouble);
JNIEXPORT void JNICALL Java_gnu_java_awt_peer_gtk_CairoGraphics2D_cairoCurveTo (JNIEnv *env, jobject, jlong, jdouble, jdouble, jdouble, jdouble, jdouble, jdouble);
JNIEXPORT void JNICALL Java_gnu_java_awt_peer_gtk_CairoGraphics2D_cairoStroke (JNIEnv *env, jobject, jlong);
JNIEXPORT void JNICALL Java_gnu_java_awt_peer_gtk_CairoGraphics2D_cairoFill (JNIEnv *env, jobject, jlong, jdouble);
JNIEXPORT void JNICALL Java_gnu_java_awt_peer_gtk_CairoGraphics2D_cairoClip (JNIEnv *env, jobject, jlong);
JNIEXPORT void JNICALL Java_gnu_java_awt_peer_gtk_CairoGraphics2D_cairoPreserveClip (JNIEnv *env, jobject, jlong);
JNIEXPORT void JNICALL Java_gnu_java_awt_peer_gtk_CairoGraphics2D_cairoResetClip (JNIEnv *env, jobject, jlong);
JNIEXPORT void JNICALL Java_gnu_java_awt_peer_gtk_CairoGraphics2D_cairoSurfaceSetFilter (JNIEnv *env, jobject, jlong, jint);
+JNIEXPORT void JNICALL Java_gnu_java_awt_peer_gtk_CairoGraphics2D_cairoDrawLine (JNIEnv *env, jobject, jlong, jdouble, jdouble, jdouble, jdouble);
+JNIEXPORT void JNICALL Java_gnu_java_awt_peer_gtk_CairoGraphics2D_cairoDrawRect (JNIEnv *env, jobject, jlong, jdouble, jdouble, jdouble, jdouble);
+JNIEXPORT void JNICALL Java_gnu_java_awt_peer_gtk_CairoGraphics2D_cairoFillRect (JNIEnv *env, jobject, jlong, jdouble, jdouble, jdouble, jdouble);
#ifdef __cplusplus
}
#endif
#endif /* __gnu_java_awt_peer_gtk_CairoGraphics2D__ */
signature.asc
Description: Dies ist ein digital signierter Nachrichtenteil
