]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.diagram.svg/src/org/simantics/diagram/svg/export/DiagramToSVG.java
Performance enhancements for DiagramToSVG
[simantics/platform.git] / bundles / org.simantics.diagram.svg / src / org / simantics / diagram / svg / export / DiagramToSVG.java
1 package org.simantics.diagram.svg.export;
2
3
4 import java.awt.Point;
5 import java.util.concurrent.Semaphore;
6 import java.util.concurrent.atomic.AtomicReference;
7
8 import org.apache.batik.svggen.SVGGraphics2D;
9 import org.simantics.Simantics;
10 import org.simantics.db.ReadGraph;
11 import org.simantics.db.Resource;
12 import org.simantics.db.common.ResourceArray;
13 import org.simantics.db.common.request.PossibleIndexRoot;
14 import org.simantics.db.common.request.UniqueRead;
15 import org.simantics.db.common.utils.NameUtils;
16 import org.simantics.db.exception.DatabaseException;
17 import org.simantics.db.exception.ValidationException;
18 import org.simantics.db.management.ISessionContext;
19 import org.simantics.db.request.Read;
20 import org.simantics.diagram.elements.DiagramNodeUtil;
21 import org.simantics.diagram.export.ImagePrinter;
22 import org.simantics.diagram.stubs.DiagramResource;
23 import org.simantics.g2d.canvas.impl.CanvasContext;
24 import org.simantics.g2d.scenegraph.ICanvasSceneGraphProvider;
25 import org.simantics.layer0.Layer0;
26 import org.simantics.modeling.ModelingResources;
27 import org.simantics.structural.stubs.StructuralResource2;
28 import org.simantics.structural2.StructuralVariables;
29 import org.simantics.utils.DataContainer;
30 import org.simantics.utils.datastructures.Pair;
31 import org.simantics.utils.page.MarginUtils.Margins;
32 import org.simantics.utils.threads.IThreadWorkQueue;
33 import org.simantics.utils.threads.ThreadUtils;
34 import org.simantics.utils.threads.WorkerThread;
35
36
37 public class DiagramToSVG {
38         
39         
40         public static String diagramToSVG(Resource diagram) {
41
42                 try {
43                         ImagePrinter.ImageExportPlan exportPlan = new ImagePrinter.ImageExportPlan();
44                         exportPlan.margin = 0.05;
45                         exportPlan.dpi = 96.0;
46                         exportPlan.diagram = diagram;
47                         return render(diagram,exportPlan);
48                 } catch (Exception e) {
49                         e.printStackTrace();
50                         return "<b>error</b> "+ e.getMessage();
51                 }
52  
53                 
54         }
55         
56         public static String diagramToSVG(Resource diagram, int width, int height) {
57
58                 try {
59                         ImagePrinter.ImageExportPlan exportPlan = new ImagePrinter.ImageExportPlan();
60                         exportPlan.margin = 0.05;
61                         exportPlan.size = new Point(width, height);
62                         exportPlan.diagram = diagram;
63                         return render(diagram,exportPlan);
64                 } catch (Exception e) {
65                         e.printStackTrace();
66                         return "<b>error</b> "+ e.getMessage();
67                 }
68  
69                 
70         }
71         
72         public static String diagramToSVG(Resource diagram, double dpi) {
73
74                 try {
75                         ImagePrinter.ImageExportPlan exportPlan = new ImagePrinter.ImageExportPlan();
76                         exportPlan.margin = 0.05;
77                         exportPlan.dpi = dpi;
78                         exportPlan.diagram = diagram;
79                         return render(diagram,exportPlan);
80                 } catch (Exception e) {
81                         e.printStackTrace();
82                         return "<b>error</b> "+ e.getMessage();
83                 }
84  
85                 
86         }
87         
88         public static String diagramToSVG(Resource diagram, ImagePrinter.ImageExportPlan exportPlan, Margins margins, SVGGraphics2D svgGenerator) {
89
90                 try {
91                         return render(diagram,exportPlan,margins,svgGenerator);
92                 } catch (Exception e) {
93                         e.printStackTrace();
94                         return "<b>error</b> "+ e.getMessage();
95                 }
96                 
97         }
98         
99         private static String render(final Resource input, final ImagePrinter.ImageExportPlan exportPlan) throws Exception {
100                 return render(input, exportPlan, null, SVGBuilder.defaultSVGGenerator()); 
101         }
102         
103         private static String render(final Resource input, final ImagePrinter.ImageExportPlan exportPlan, Margins margins, SVGGraphics2D svgExporter) throws Exception {
104                 Resource diagram = Simantics.getSession().syncRequest(new Read<Resource>() {
105                         @Override
106                         public Resource perform(ReadGraph graph) throws DatabaseException {
107                                 DiagramResource DIA = DiagramResource.getInstance(graph);
108                                 if (graph.isInstanceOf(input, DIA.Diagram))
109                                         return input;
110                                 StructuralResource2 SR = StructuralResource2.getInstance(graph);
111                                 ModelingResources MOD = ModelingResources.getInstance(graph);
112                                 Layer0 L0 = Layer0.getInstance(graph);
113                                 if (graph.isInstanceOf(input, SR.Composite)) {
114                                         Resource possibleDiagram = graph.getPossibleObject(input, MOD.CompositeToDiagram);
115                                         if (possibleDiagram != null)
116                                                 return possibleDiagram;
117                                         for (Resource r : graph.getObjects(input, L0.ConsistsOf)) {
118                                                 if (graph.isInstanceOf(r, SR.Composite)) {
119                                                         possibleDiagram = graph.getPossibleObject(input, MOD.CompositeToDiagram);
120                                                         if (possibleDiagram != null)
121                                                                 return possibleDiagram;
122                                                 }
123                                         }
124                                 }
125                                 return null;
126                         }
127                 });
128                 if (diagram == null)
129                         throw new DatabaseException("Input " + input + " cannot be resolved as diagram");
130                 
131                 final WorkerThread thread = new WorkerThread("Diagram Image Painter");
132                 thread.start();
133                 
134         final CanvasContext ctx = new CanvasContext(thread);
135         final AtomicReference<ICanvasSceneGraphProvider> sgProvider = new AtomicReference<ICanvasSceneGraphProvider>();
136                 final ISessionContext sessionContext = Simantics.getSessionContext();
137                 final DataContainer<String> result = new DataContainer<String>(null);
138                 final DataContainer<Exception> exception = new DataContainer<Exception>(null);
139         try {
140             final Semaphore done = new Semaphore(0);
141             // IMPORTANT: Load diagram in a different thread than the canvas context thread!
142             ThreadUtils.getBlockingWorkExecutor().execute(new Runnable() {
143                 @Override
144                 public void run() {
145                     try {
146                         Pair<Resource, String> modelAndRVI = sessionContext.getSession().syncRequest(new UniqueRead<Pair<Resource, String>>() {
147                             @Override
148                             public Pair<Resource, String> perform(ReadGraph graph) throws DatabaseException {
149                                 return new Pair<Resource, String>( resolveModel(graph, exportPlan.diagram ), resolveRVI(graph, exportPlan.diagram) );
150                             }
151                         });
152
153                         ICanvasSceneGraphProvider provider = DiagramNodeUtil.loadSceneGraphProvider(ctx, modelAndRVI.first, exportPlan.diagram, modelAndRVI.second);
154                         sgProvider.set( provider );
155                         
156                         ThreadUtils.asyncExec(thread, new Runnable() {
157                             @Override
158                             public void run() {
159                                 try {
160                                     SVGBuilder chassis = margins != null ?
161                                                 new SVGBuilder(exportPlan.dpi,exportPlan.size,margins) :
162                                                 new SVGBuilder(exportPlan.dpi,exportPlan.size,exportPlan.margin);
163                                     
164                                     result.set(chassis.paint(ctx, svgExporter));
165                                 } catch (Exception e) {
166                                         exception.set(e);
167                                 } finally {
168                                     done.release();
169                                 }
170                             }
171                         });
172                     } catch (DatabaseException e) {
173                         exception.set(e);
174                         done.release();
175                     } catch (Throwable e) {
176                         exception.set(new DatabaseException(e));
177                         done.release();
178                     } finally {
179                         done.release();
180                     }
181                 }
182             });
183
184             done.acquire(2);
185             if (exception.get() != null)
186                 throw exception.get();
187             return result.get();
188         } finally {
189             if (sgProvider.get() != null)
190                 sgProvider.get().dispose();
191             ctx.dispose();
192         }
193         }
194         
195         private static Resource resolveModel(ReadGraph graph, Resource diagram) throws DatabaseException {
196         ModelingResources mod = ModelingResources.getInstance(graph);
197         Resource composite = graph.getSingleObject(diagram, mod.DiagramToComposite);
198         Resource model = graph.syncRequest(new PossibleIndexRoot(composite));
199         if (model == null)
200             throw new ValidationException("no model found for composite " + NameUtils.getSafeName(graph, composite));
201         return model;
202     }
203
204     private static String resolveRVI(ReadGraph graph, Resource diagram) throws DatabaseException {
205         ModelingResources mod = ModelingResources.getInstance(graph);
206         Resource composite = graph.getSingleObject(diagram, mod.DiagramToComposite);
207         final ResourceArray compositePath = StructuralVariables.getCompositeArray(graph, composite);
208         final ResourceArray variablePath = compositePath.removeFromBeginning(1);
209         return StructuralVariables.getRVI(graph, variablePath);
210     }
211
212         public static String renderWithLoader(final Resource input, final ImagePrinter.ImageExportPlan exportPlan,
213                         Margins margins, SVGGraphics2D svgExporter,
214                         IThreadWorkQueue loaderThread,
215                         IThreadWorkQueue painterThread) throws Exception {
216
217                 if(!painterThread.currentThreadAccess()) throw new IllegalStateException("The callable should be called from the contextThread");
218
219                 final CanvasContext ctx = new CanvasContext(loaderThread);
220                 final AtomicReference<ICanvasSceneGraphProvider> sgProvider = new AtomicReference<ICanvasSceneGraphProvider>();
221                 final DataContainer<String> result = new DataContainer<String>(null);
222                 final DataContainer<Exception> exception = new DataContainer<Exception>(null);
223
224                 try {
225                         
226                         final ISessionContext sessionContext = Simantics.getSessionContext();
227
228                         Pair<Resource, String> modelAndRVI = sessionContext.getSession().syncRequest(new UniqueRead<Pair<Resource, String>>() {
229                                 @Override
230                                 public Pair<Resource, String> perform(ReadGraph graph) throws DatabaseException {
231                                         return new Pair<Resource, String>( resolveModel(graph, exportPlan.diagram ), resolveRVI(graph, exportPlan.diagram) );
232                                 }
233                         });
234
235                         ICanvasSceneGraphProvider provider = DiagramNodeUtil.loadSceneGraphProvider(ctx, modelAndRVI.first, exportPlan.diagram, modelAndRVI.second);
236                         sgProvider.set( provider );
237
238                         final Semaphore done = new Semaphore(0);
239
240                         ThreadUtils.asyncExec(loaderThread, new Runnable() {
241                                 @Override
242                                 public void run() {
243                                         try {
244                                                 SVGBuilder chassis = margins != null ?
245                                                                 new SVGBuilder(exportPlan.dpi,exportPlan.size,margins) :
246                                                                         new SVGBuilder(exportPlan.dpi,exportPlan.size,exportPlan.margin);
247                                                                 result.set(chassis.paint(ctx, svgExporter));
248                                         } catch (DatabaseException e) {
249                                                 exception.set(e);
250                                                 done.release();
251                                         } catch (Throwable e) {
252                                                 exception.set(new DatabaseException(e));
253                                                 done.release();
254                                         } finally {
255                                                 done.release();
256                                         }
257                                 }
258                         });
259
260                         done.acquire();
261
262                 } catch (DatabaseException e) {
263                         exception.set(e);
264                 } catch (Throwable e) {
265                         exception.set(new DatabaseException(e));
266                 } finally {
267
268                         if (sgProvider.get() != null)
269                                 sgProvider.get().dispose();
270                         ctx.dispose();
271
272                 }
273
274                 if(exception.get() != null) 
275                         throw exception.get();
276
277                 return result.get();
278                 
279         }
280         
281 }