changeset 493:1b6228092a57 Android_Skia

Java code for MadButterfly JNI. This is Java code that access MadButterfly JNI interface.
author Thinker K.F. Li <thinker@branda.to>
date Thu, 26 Nov 2009 15:02:51 +0800
parents e95598916dfb
children ebc431f2af70
files Android/java/Android.mk Android/java/org/madbutterfly/InvalidStateException.java Android/java/org/madbutterfly/MBView.java Android/java/org/madbutterfly/_jni.java Android/java/org/madbutterfly/coord.java Android/java/org/madbutterfly/paint.java Android/java/org/madbutterfly/redraw_man.java Android/java/org/madbutterfly/shape.java
diffstat 8 files changed, 377 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Android/java/Android.mk	Thu Nov 26 15:02:51 2009 +0800
@@ -0,0 +1,16 @@
+LOCAL_PATH:= $(call my-dir)
+include $(CLEAR_VARS)
+
+LOCAL_MODULE := mbfly-java
+LOCAL_MODULE_CLASS := JAVA_LIBRARIES
+
+LOCAL_SRC_FILES := \
+	org/madbutterfly/_jni.java \
+	org/madbutterfly/MBView.java \
+	org/madbutterfly/redraw_man.java \
+	org/madbutterfly/coord.java \
+	org/madbutterfly/shape.java \
+	org/madbutterfly/paint.java \
+	org/madbutterfly/InvalidStateException.java
+
+include $(BUILD_JAVA_LIBRARY)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Android/java/org/madbutterfly/InvalidStateException.java	Thu Nov 26 15:02:51 2009 +0800
@@ -0,0 +1,12 @@
+package org.madbutterfly;
+
+import java.lang.Exception;
+
+class InvalidStateException extends Exception {
+    public InvalidStateException() {
+	super();
+    }
+    public InvalidStateException(String msg) {
+	super(msg);
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Android/java/org/madbutterfly/MBView.java	Thu Nov 26 15:02:51 2009 +0800
@@ -0,0 +1,68 @@
+package org.madbutterfly;
+
+import android.view.SurfaceView;
+import android.view.SurfaceHolder;
+import android.content.Context;
+import android.graphics.Canvas;
+import android.graphics.Bitmap;
+import android.graphics.Paint;
+import android.graphics.Xfermode;
+import android.graphics.PorterDuff;
+import android.graphics.PorterDuffXfermode;
+
+class MBView extends SurfaceView {
+    redraw_man rdman;
+    Canvas cr, backend;
+    Bitmap cr_bmap, backend_bmap;
+    Paint copy_pnt;
+    int w, h;
+    
+    public MBView(Context context) {
+	super(context);
+	Paint paint;
+	Xfermode mode;
+	
+	rdman = null;
+	cr = null;
+	backend = null;
+	
+	mode = new PorterDuffXfermode(PorterDuff.Mode.SRC);
+	copy_pnt = new Paint();
+	copy_pnt.setXfermode(mode);
+    }
+    
+    public redraw_man get_rdman() {
+	if(rdman != null)
+	    return rdman;
+
+	w = getWidth();
+	h = getHeight();
+
+	cr_bmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
+	cr = new Canvas(cr_bmap);
+	backend_bmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
+	backend = new Canvas(backend_bmap);
+	
+	rdman = new redraw_man(cr, backend, this);
+
+	return rdman;
+    }
+
+    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
+	this.w = w;
+	this.h = h;
+	cr_bmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
+	cr.setBitmap(cr_bmap);
+	backend_bmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
+	backend.setBitmap(backend_bmap);
+    }
+
+    public void redraw() {
+	SurfaceHolder holder;
+	Canvas canvas;
+
+	holder = getHolder();
+	canvas = holder.lockCanvas();
+	canvas.drawBitmap(backend_bmap, 0, 0, copy_pnt);
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Android/java/org/madbutterfly/_jni.java	Thu Nov 26 15:02:51 2009 +0800
@@ -0,0 +1,47 @@
+package org.madbutterfly;
+import android.graphics.Canvas;
+
+class _jni {
+    native static int redraw_man_new(Canvas cr, Canvas backend);
+    native static void redraw_man_free(int rdman);
+    native static int rdman_add_shape(int rdman, int shape, int coord);
+    native static int rdman_get_root(int rdman);
+    native static int rdman_redraw_all(int rdman);
+    native static int rdman_redraw_changed(int rdman);
+    native static int rdman_redraw_area(int rdman, float x, float y,
+					float w, float h);
+    native static void rdman_paint_fill(int rdman, int paint, int shape);
+    native static void rdman_paint_stroke(int rdman, int paint, int shape);
+    
+    /* coord_t */
+    native static int rdman_coord_new(int rdman, int parent);
+    native static void rdman_coord_free(int rdman, int coord);
+    native static void rdman_coord_subtree_free(int rdman, int coord);
+    native static void rdman_coord_changed(int rdman, int coord);
+    
+    /* shape_t */
+    native static void rdman_shape_changed(int rdman, int shape);
+    native static void rdman_shape_free(int rdman, int shape);
+    native static int rdman_shape_path_new(int rdman, String data);
+
+    /* paint_t */
+    native static int rdman_paint_color_new(int rdman, float r, float g,
+					    float b, float a);
+    native static int rdman_paint_free(int rdman, int paint);
+    native static void paint_color_set(int paint, float r, float g,
+				      float b, float a);
+    native static float[] paint_color_get(int paint);
+    native static int rdman_paint_linear_new(int rdman,
+					     float x1, float y1,
+					     float x2, float y2);
+    native static int paint_linear_stops(int paint, int n_stops, int stops);
+    native static int rdman_paint_radial_new(int rdman, float cx, float cy,
+					     float r);
+    native static int paint_radial_stops(int paint, int n_stops, int stops);
+    native static int paint_create_stops(float stops[][]);
+    native static int paint_free_stops(int stops);
+    
+    static {
+	System.loadLibrary("mbfly-jni");
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Android/java/org/madbutterfly/coord.java	Thu Nov 26 15:02:51 2009 +0800
@@ -0,0 +1,32 @@
+package org.madbutterfly;
+
+import java.util.ArrayList;
+
+class coord {
+    protected int addr;
+    redraw_man rdman;
+    public ArrayList children;
+    public ArrayList members;
+
+    public coord(redraw_man rdman, int addr) {
+	this.addr = addr;
+	this.rdman = rdman;
+	children = new ArrayList();
+	members = new ArrayList();
+    }
+
+    protected void invalid() {
+	addr = 0;
+    }
+
+    protected void finalize() {
+	if(addr != 0)
+	    _jni.rdman_coord_free(rdman._rdman_addr, addr);
+    }
+
+    public void add_shape(shape member) {
+	_jni.rdman_add_shape(rdman._rdman_addr, member.addr, addr);
+	members.add(member);
+	member.parent = this;
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Android/java/org/madbutterfly/paint.java	Thu Nov 26 15:02:51 2009 +0800
@@ -0,0 +1,70 @@
+package org.madbutterfly;
+
+import java.util.ArrayList;
+
+class paint {
+    protected int addr;
+    redraw_man rdman;
+    ArrayList members;
+
+    public paint(redraw_man rdman, int addr) {
+	this.addr = addr;
+	this.rdman = rdman;
+	members = new ArrayList();
+    }
+
+    protected void invalid() {
+	addr = 0;
+    }
+
+    protected void finalize() {
+	if(addr != 0)
+	    _jni.rdman_paint_free(rdman._rdman_addr, addr);
+    }
+
+    public void stroke(shape sh) {
+	no_stroke(sh);
+	
+	if(sh.fill != this)
+	    members.add(sh);
+
+	sh.stroke = this;
+	
+	_jni.rdman_paint_stroke(rdman._rdman_addr, addr, sh.addr);
+    }
+    
+    public void fill(shape sh) {
+	no_fill(sh);
+	
+	if(sh.stroke != this)
+	    members.add(sh);
+
+	sh.fill = this;
+	
+	_jni.rdman_paint_fill(rdman._rdman_addr, addr, sh.addr);
+    }
+
+    public boolean is_empty() {
+	return members.size() == 0;
+    }
+
+    public static void no_stroke(shape sh) {
+	int i;
+	
+	if(sh.stroke != null && sh.stroke != sh.fill) {
+	    i = sh.stroke.members.indexOf(sh);
+	    sh.stroke.members.remove(i);
+	}
+	sh.stroke = null;
+    }
+
+    public static void no_fill(shape sh) {
+	int i;
+	
+	if(sh.fill != null && sh.stroke != sh.fill) {
+	    i = sh.stroke.members.indexOf(sh);
+	    sh.stroke.members.remove(i);
+	}
+	sh.fill = null;
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Android/java/org/madbutterfly/redraw_man.java	Thu Nov 26 15:02:51 2009 +0800
@@ -0,0 +1,108 @@
+package org.madbutterfly;
+
+import android.graphics.Canvas;
+import android.view.SurfaceHolder;
+import java.util.ArrayList;
+
+class redraw_man {
+    Canvas _cr, _backend;
+    int _rdman_addr;		// address of redraw_man_t
+    coord root;
+    ArrayList paints;
+    MBView _view;
+    
+    public redraw_man(Canvas cr, Canvas backend, MBView view) {
+	int addr;
+	
+	_view = view;
+	_cr = cr;
+	_backend = backend;
+	
+	_rdman_addr = _jni.redraw_man_new(cr, backend);
+	addr = _jni.rdman_get_root(_rdman_addr);
+	root = new coord(this, addr);
+	
+	paints = new ArrayList();
+    }
+
+    static void _invalid_subtree(coord subtree) {
+	int i, sz;
+	coord child;
+	shape member;
+
+	subtree.invalid();
+	
+	sz = subtree.members.size();
+	for(i = 0; i < sz; i ++) {
+	    member = (shape)subtree.members.get(i);
+	    member.invalid();
+	}
+
+	sz = subtree.children.size();
+	for(i = 0; i < sz; i++) {
+	    child = (coord)subtree.children.get(i);
+	    _invalid_subtree(child);
+	}
+    }
+
+    protected void finalize() {
+	int i, sz;
+	paint pnt;
+	
+	_invalid_subtree(root);
+	
+	/* invalid paints */
+	sz = paints.size();
+	for(i = 0; i < sz; i++) {
+	    pnt = (paint)paints.get(i);
+	    pnt.invalid();
+	}
+	
+	_jni.redraw_man_free(_rdman_addr);
+    }
+
+    public coord get_root() {
+	return root;
+    }
+
+    public coord coord_new(coord parent) {
+	int addr;
+	coord child;
+	
+	addr = _jni.rdman_coord_new(_rdman_addr, parent.addr);
+	child = new coord(this, addr);
+	parent.children.add(child);
+	
+	return child;
+    }
+
+    public void coord_free(coord obj) {
+	obj.finalize();
+	obj.invalid();
+    }
+
+    public shape shape_path_new(String pathdata) {
+	int addr;
+	shape new_shape;
+
+	addr = _jni.rdman_shape_path_new(_rdman_addr, pathdata);
+	new_shape = new shape(this, addr);
+
+	return new_shape;
+    }
+
+    public paint paint_color_new(float r, float g, float b, float a) {
+	int addr;
+	paint pnt;
+
+	addr = _jni.rdman_paint_color_new(_rdman_addr, r, g, b, a);
+	pnt = new paint(this, addr);
+	
+	return pnt;
+    }
+
+    public void redraw() {
+	_jni.rdman_redraw_all(_rdman_addr);
+	_view.redraw();
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Android/java/org/madbutterfly/shape.java	Thu Nov 26 15:02:51 2009 +0800
@@ -0,0 +1,24 @@
+package org.madbutterfly;
+
+class shape {
+    protected int addr;
+    redraw_man rdman;
+    protected coord parent;
+    protected paint stroke, fill;
+
+    public shape(redraw_man rdman, int addr) {
+	this.addr = addr;
+	this.rdman = rdman;
+	this.parent = null;
+	stroke = fill = null;
+    }
+    
+    protected void invalid() {
+	addr = 0;
+    }
+    
+    protected void finalize() {
+	if(addr != 0)
+	    _jni.rdman_shape_free(rdman._rdman_addr, addr);
+    }
+}