]> gerrit.simantics Code Review - simantics/platform.git/commitdiff
Performance enhancements for DiagramToSVG 05/505/1
authorAntti Villberg <antti.villberg@semantum.fi>
Thu, 11 May 2017 09:26:02 +0000 (12:26 +0300)
committerTuukka Lehtonen <tuukka.lehtonen@semantum.fi>
Thu, 11 May 2017 10:57:27 +0000 (13:57 +0300)
refs #7208

Change-Id: Ieb507e6c9abc287192fff127385cc51f17ebee86
(cherry picked from commit 5655da36b1f5b605b2407b34f1226a9ad60f8ce6)

bundles/org.simantics.diagram.svg/src/org/simantics/diagram/svg/export/DiagramToSVG.java

index 3c110976294a86bf1aa2414ea50eda567881365b..555be8be7d1cf68224a7e38c0e4bc78043567f55 100644 (file)
@@ -29,6 +29,7 @@ import org.simantics.structural2.StructuralVariables;
 import org.simantics.utils.DataContainer;
 import org.simantics.utils.datastructures.Pair;
 import org.simantics.utils.page.MarginUtils.Margins;
+import org.simantics.utils.threads.IThreadWorkQueue;
 import org.simantics.utils.threads.ThreadUtils;
 import org.simantics.utils.threads.WorkerThread;
 
@@ -127,9 +128,6 @@ public class DiagramToSVG {
                if (diagram == null)
                        throw new DatabaseException("Input " + input + " cannot be resolved as diagram");
                
-               
-       
-               
                final WorkerThread thread = new WorkerThread("Diagram Image Painter");
                thread.start();
                
@@ -203,8 +201,6 @@ public class DiagramToSVG {
         return model;
     }
 
-
-
     private static String resolveRVI(ReadGraph graph, Resource diagram) throws DatabaseException {
        ModelingResources mod = ModelingResources.getInstance(graph);
         Resource composite = graph.getSingleObject(diagram, mod.DiagramToComposite);
@@ -213,4 +209,73 @@ public class DiagramToSVG {
         return StructuralVariables.getRVI(graph, variablePath);
     }
 
+       public static String renderWithLoader(final Resource input, final ImagePrinter.ImageExportPlan exportPlan,
+                       Margins margins, SVGGraphics2D svgExporter,
+                       IThreadWorkQueue loaderThread,
+                       IThreadWorkQueue painterThread) throws Exception {
+
+               if(!painterThread.currentThreadAccess()) throw new IllegalStateException("The callable should be called from the contextThread");
+
+               final CanvasContext ctx = new CanvasContext(loaderThread);
+               final AtomicReference<ICanvasSceneGraphProvider> sgProvider = new AtomicReference<ICanvasSceneGraphProvider>();
+               final DataContainer<String> result = new DataContainer<String>(null);
+               final DataContainer<Exception> exception = new DataContainer<Exception>(null);
+
+               try {
+                       
+                       final ISessionContext sessionContext = Simantics.getSessionContext();
+
+                       Pair<Resource, String> modelAndRVI = sessionContext.getSession().syncRequest(new UniqueRead<Pair<Resource, String>>() {
+                               @Override
+                               public Pair<Resource, String> perform(ReadGraph graph) throws DatabaseException {
+                                       return new Pair<Resource, String>( resolveModel(graph, exportPlan.diagram ), resolveRVI(graph, exportPlan.diagram) );
+                               }
+                       });
+
+                       ICanvasSceneGraphProvider provider = DiagramNodeUtil.loadSceneGraphProvider(ctx, modelAndRVI.first, exportPlan.diagram, modelAndRVI.second);
+                       sgProvider.set( provider );
+
+                       final Semaphore done = new Semaphore(0);
+
+                       ThreadUtils.asyncExec(loaderThread, new Runnable() {
+                               @Override
+                               public void run() {
+                                       try {
+                                               SVGBuilder chassis = margins != null ?
+                                                               new SVGBuilder(exportPlan.dpi,exportPlan.size,margins) :
+                                                                       new SVGBuilder(exportPlan.dpi,exportPlan.size,exportPlan.margin);
+                                                               result.set(chassis.paint(ctx, svgExporter));
+                                       } catch (DatabaseException e) {
+                                               exception.set(e);
+                                               done.release();
+                                       } catch (Throwable e) {
+                                               exception.set(new DatabaseException(e));
+                                               done.release();
+                                       } finally {
+                                               done.release();
+                                       }
+                               }
+                       });
+
+                       done.acquire();
+
+               } catch (DatabaseException e) {
+                       exception.set(e);
+               } catch (Throwable e) {
+                       exception.set(new DatabaseException(e));
+               } finally {
+
+                       if (sgProvider.get() != null)
+                               sgProvider.get().dispose();
+                       ctx.dispose();
+
+               }
+
+               if(exception.get() != null) 
+                       throw exception.get();
+
+               return result.get();
+               
+       }
+       
 }