9484439609bf90c7559c911f4aa5b9c2aba615ec
[org.ibex.core.git] / src / org / xwt / plat / GCJ.java
1 // Copyright 2003 Adam Megacz, see the COPYING file for licensing [GPL]
2 package org.xwt.plat;
3
4 import org.xwt.*;
5 import org.xwt.util.*;
6 import java.io.*;
7
8 /** common superclass for all platforms that use GCJ to compile a native binary */
9 public abstract class GCJ extends Platform {
10
11     // static references to these classes ensure that the linker will include them
12     private static Class c1 = gnu.java.locale.Calendar.class;
13     private static Class c2 = java.util.GregorianCalendar.class;
14     private static Class c3 = gnu.gcj.convert.Input_ASCII.class;
15     private static Class c4 = gnu.gcj.convert.Input_UTF8.class;
16     private static Class c5 = gnu.gcj.convert.Input_8859_1.class;
17     private static Class c6 = gnu.java.locale.LocaleInformation.class;
18     private static Class c7 = gnu.gcj.convert.Output_ASCII.class;
19     private static Class c8 = gnu.java.locale.Calendar_en.class;
20     private static Class c9 = gnu.java.locale.Calendar_en_US.class;
21
22     protected org.xwt.Weak _getWeak(Object o) { return new Java2Weak(o); }
23
24     protected native InputStream _getBuiltinInputStream();
25     protected native InputStream _getFreetypeInputStream();
26
27     private static class Java2Weak extends java.lang.ref.WeakReference implements org.xwt.Weak {
28         public Java2Weak(Object o) { super(o); }
29     }
30         
31     // FIXME
32     protected ImageDecoder _decodeJPEG(InputStream is, String name) {
33         try {
34             return new JPEG(is);
35         } catch (Exception e) {
36             Log.log(this, "Exception while decoding JPEG image " + name);
37             Log.log(this, e);
38             return null;
39         }
40     }
41
42     /** Converts an InputStream carrying a JPEG image into an ARGB int[] */
43     private static class JPEG extends ImageDecoder {
44         int[] data;
45         byte[] buffer;
46         int width, height;
47         InputStream is;
48         
49         public final int[] getData() { return data; }
50         public final int getWidth() { return width; }
51         public final int getHeight() { return height; }
52         private JPEG(InputStream is) { this.is = is; nativeDecompress(); buffer = null; }
53         private native void nativeDecompress();
54     }
55     
56     // FIXME: This could be optimized (a lot) by using a custom hashtable
57     public final static class Retainer {
58         private static Hash table = new Hash();
59         private final static class Key {
60             private Object o;
61             public Key(Object o) { this.o = o; }
62             public int hashCode() { return o == null ? 0 : o.hashCode(); }
63             public boolean equals(Object o2) { return (o2 instanceof Key) && ((Key)o2).o == o; }
64         }
65         private final static class Entry {
66             private int refCount;
67             public Entry() { inc(); }
68             public void inc() { refCount++; }
69             public boolean dec() { return --refCount == 0; }
70         }
71         public static synchronized void retain(Object o) {
72             Key k = new Key(o);
73             Entry r = (Entry) table.get(k);
74             if(r == null) table.put(k,new Entry());
75             else r.inc();
76         }
77         public static synchronized void release(Object o) {
78             Key k = new Key(o);
79             Entry e = (Entry) table.get(k);
80             if(e == null) throw new Error("Retainer::Release on unknown object");
81             if(e.dec()) { table.remove(k); }
82         }
83     }
84 }