From b2cb20c0a326900b3b854cf8b516040f5a5771e2 Mon Sep 17 00:00:00 2001 From: Antti Villberg Date: Thu, 11 May 2017 12:26:02 +0300 Subject: [PATCH] Performance enhancements for DiagramToSVG refs #7208 Change-Id: Ieb507e6c9abc287192fff127385cc51f17ebee86 (cherry picked from commit 5655da36b1f5b605b2407b34f1226a9ad60f8ce6) --- .../diagram/svg/export/DiagramToSVG.java | 75 +++++++++++++++++-- 1 file changed, 70 insertions(+), 5 deletions(-) diff --git a/bundles/org.simantics.diagram.svg/src/org/simantics/diagram/svg/export/DiagramToSVG.java b/bundles/org.simantics.diagram.svg/src/org/simantics/diagram/svg/export/DiagramToSVG.java index 3c1109762..555be8be7 100644 --- a/bundles/org.simantics.diagram.svg/src/org/simantics/diagram/svg/export/DiagramToSVG.java +++ b/bundles/org.simantics.diagram.svg/src/org/simantics/diagram/svg/export/DiagramToSVG.java @@ -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 sgProvider = new AtomicReference(); + final DataContainer result = new DataContainer(null); + final DataContainer exception = new DataContainer(null); + + try { + + final ISessionContext sessionContext = Simantics.getSessionContext(); + + Pair modelAndRVI = sessionContext.getSession().syncRequest(new UniqueRead>() { + @Override + public Pair perform(ReadGraph graph) throws DatabaseException { + return new Pair( 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(); + + } + } -- 2.43.2