|
1 /* |
|
2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 /** |
|
18 * \file |
|
19 * \brief Target platform dependent Java module for Symbian. |
|
20 * |
|
21 */ |
|
22 |
|
23 package com.nokia.microedition.m2g; |
|
24 import javax.microedition.lcdui.Graphics; |
|
25 import javax.microedition.lcdui.Image; |
|
26 import org.eclipse.swt.widgets.Display; |
|
27 |
|
28 import com.nokia.mj.impl.nokialcdui.LCDUIInvoker; |
|
29 import org.eclipse.swt.internal.extension.DisplayExtension; |
|
30 |
|
31 /** |
|
32 * Implements platform-dependent functionality. At the moment, this |
|
33 * includes native finalization and some helper methods for |
|
34 * synchronizing 2D and 3D rendering. |
|
35 */ |
|
36 class Platform |
|
37 { |
|
38 /** |
|
39 * eSWT display for ui thread access |
|
40 */ |
|
41 private static Display display = null; |
|
42 private static boolean libraryLoaded = false; |
|
43 |
|
44 //------------------------------------------------------------------ |
|
45 // Package private methods |
|
46 //------------------------------------------------------------------ |
|
47 |
|
48 /** |
|
49 * Executes given runnable in UI thread if caller thread is not UI thread |
|
50 */ |
|
51 static void executeInUIThread(M2GRunnableQt obj) |
|
52 { |
|
53 if (display == null) |
|
54 { |
|
55 if (!uiThreadAvailable()) |
|
56 { |
|
57 throw new Error("Ui thread not available"); |
|
58 } |
|
59 } |
|
60 if (display.isDisposed()) |
|
61 { |
|
62 throw new Error("Display already disposed"); |
|
63 } |
|
64 |
|
65 if (obj != null) |
|
66 { |
|
67 // If we are not in UI thread use display |
|
68 if (Thread.currentThread() != display.getThread()) |
|
69 { |
|
70 display.syncExec(obj); |
|
71 } |
|
72 else |
|
73 { |
|
74 // In this case we are in UI thread so just execute directly |
|
75 obj.run(); |
|
76 } |
|
77 // Check if any exceptions occured in execution |
|
78 // and throw forward in caller thread |
|
79 obj.checkAndThrow(); |
|
80 } |
|
81 } |
|
82 |
|
83 /** |
|
84 * Check the UI thread / toolkit init status and store display if it is available |
|
85 * @return true if either lcdui or eswt toolkit is initialized and ui thread is accessible |
|
86 * otherwise false |
|
87 */ |
|
88 static boolean uiThreadAvailable() |
|
89 { |
|
90 if (display != null) |
|
91 { |
|
92 return true; |
|
93 } |
|
94 else |
|
95 { |
|
96 display = DisplayExtension.getDisplayInstance(); |
|
97 System.out.println("P-UI Thread not available Case" ); |
|
98 if (display == null) |
|
99 { |
|
100 return false; |
|
101 } |
|
102 else |
|
103 { |
|
104 System.out.println("P-UI Thread available Case" ); |
|
105 // UI thread is available, so load native library if not already loaded |
|
106 if (!libraryLoaded) |
|
107 { |
|
108 System.out.println("P-UI Thread not available Case Javam2g loaded" ); |
|
109 com.nokia.mj.impl.rt.support.Jvm.loadSystemLibrary("javam2g"); |
|
110 libraryLoaded = true; |
|
111 } |
|
112 return true; |
|
113 } |
|
114 } |
|
115 } |
|
116 |
|
117 /** |
|
118 * Registers an Object3D in the global handle-to-object map. The |
|
119 * handle of the object must already be set at this point! |
|
120 */ |
|
121 // static final void registerFinalizer(Object3D obj) |
|
122 // { |
|
123 // //heuristicGC(); |
|
124 // } |
|
125 // |
|
126 // /** |
|
127 // * Registers a Graphics3D object (not derived from Object3D) for |
|
128 // * finalization. |
|
129 // */ |
|
130 // static final void registerFinalizer(Graphics3D g3d) |
|
131 // { |
|
132 // //heuristicGC(); |
|
133 // } |
|
134 // |
|
135 // /** |
|
136 // * Registers an Interface object for finalization |
|
137 // */ |
|
138 // static final void registerFinalizer(Interface m2G) |
|
139 // { |
|
140 // } |
|
141 // |
|
142 // /** |
|
143 // * Registers a Loader object for finalization |
|
144 // */ |
|
145 // static final void registerFinalizer(Loader loader) |
|
146 // { |
|
147 // } |
|
148 // |
|
149 // /** |
|
150 // * Flushes all pending rendering to a Graphics context and blocks |
|
151 // * until finished |
|
152 // */ |
|
153 // static final void sync(Graphics g) |
|
154 // { |
|
155 // //ToolkitInvoker invoker = ToolkitInvoker.getToolkitInvoker(); |
|
156 // //invoker.toolkitSync(invoker.getToolkit()); |
|
157 // } |
|
158 // |
|
159 // /** |
|
160 // * Flushes all pending rendering to an Image object |
|
161 // */ |
|
162 // static final void sync(Image img) |
|
163 // { |
|
164 // //ToolkitInvoker invoker = ToolkitInvoker.getToolkitInvoker(); |
|
165 // //invoker.toolkitSync(invoker.getToolkit()); |
|
166 // } |
|
167 // |
|
168 // /** |
|
169 // * Finalizes the native peer of an interface |
|
170 // */ |
|
171 // static final native void finalizeInterface(int handle); |
|
172 // |
|
173 // /** |
|
174 // * Finalizes the native peer of an object |
|
175 // * JCF: added this wrapper method so we could pass the toolkit handle to the native method. |
|
176 // */ |
|
177 static final void finalizeObject(int handle) |
|
178 { |
|
179 try |
|
180 { |
|
181 final int finalHandle = handle; |
|
182 executeInUIThread( |
|
183 new M2GRunnableQt() |
|
184 { |
|
185 void doRun() |
|
186 { |
|
187 _finalizeObject(finalHandle); |
|
188 } |
|
189 }); |
|
190 } |
|
191 catch (Exception e) |
|
192 { |
|
193 // do nothing |
|
194 } |
|
195 } |
|
196 |
|
197 /** |
|
198 * Finalizes the native peer of an object associated with |
|
199 * given Interface instance |
|
200 */ |
|
201 // static final void finalizeObject(int handle, Interface aInterface) |
|
202 // { |
|
203 // try |
|
204 // { |
|
205 // final int finalHandle = handle; |
|
206 // executeInUIThread( |
|
207 // new M2GRunnable() |
|
208 // { |
|
209 // public void doRun() |
|
210 // { |
|
211 // _finalizeObject(finalHandle); |
|
212 // } |
|
213 // }); |
|
214 // } |
|
215 // catch (Exception e) |
|
216 // { |
|
217 // // do nothing |
|
218 // } |
|
219 // } |
|
220 |
|
221 |
|
222 //------------------------------------------------------------------ |
|
223 // Private methods |
|
224 //------------------------------------------------------------------ |
|
225 |
|
226 /** |
|
227 * Trigger GC if minimum free memory limit has been exceeded in the native side |
|
228 */ |
|
229 static final void heuristicGC() |
|
230 { |
|
231 } |
|
232 private static final native void _finalizeObject(int handle); |
|
233 } |
|
234 |