Skip to content

Commit 2c03257

Browse files
committed
Revise ViewerPanel synchronization
1 parent f39423a commit 2c03257

File tree

1 file changed

+32
-33
lines changed

1 file changed

+32
-33
lines changed

src/main/java/bdv/viewer/ViewerPanel.java

Lines changed: 32 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
import java.util.concurrent.ForkJoinPool;
4949
import java.util.concurrent.ThreadFactory;
5050
import java.util.concurrent.atomic.AtomicInteger;
51+
import java.util.concurrent.atomic.AtomicReference;
5152
import java.util.stream.Collectors;
5253

5354
import javax.swing.DefaultBoundedRangeModel;
@@ -191,7 +192,7 @@ public class ViewerPanel extends AbstractViewerPanel implements OverlayRenderer,
191192
* to make smooth transitions when {@link #align(AlignPlane) aligning to
192193
* orthogonal planes}.
193194
*/
194-
private AbstractTransformAnimator currentAnimator = null;
195+
private AtomicReference<AbstractTransformAnimator> currentAnimator = new AtomicReference<>();
195196

196197
/**
197198
* A list of currently incomplete (see {@link OverlayAnimator#isComplete()})
@@ -365,10 +366,7 @@ public void addSources( final Collection< SourceAndConverter< ? > > sourceAndCon
365366
@Deprecated
366367
public void removeSource( final Source< ? > source )
367368
{
368-
synchronized ( state )
369-
{
370-
state.removeSource( soc( source ) );
371-
}
369+
state.removeSource( soc( source ) );
372370
}
373371

374372
/**
@@ -377,10 +375,7 @@ public void removeSource( final Source< ? > source )
377375
@Deprecated
378376
public void removeSources( final Collection< Source< ? > > sources )
379377
{
380-
synchronized ( state )
381-
{
382-
state.removeSources( sources.stream().map( this::soc ).collect( Collectors.toList() ) );
383-
}
378+
state.removeSources( sources.stream().map( this::soc ).collect( Collectors.toList() ) );
384379
}
385380

386381
/**
@@ -465,17 +460,15 @@ public void paint()
465460

466461
display.repaint();
467462

468-
synchronized ( this )
463+
final AbstractTransformAnimator animator = currentAnimator.get();
464+
if ( animator != null )
469465
{
470-
if ( currentAnimator != null )
471-
{
472-
final AffineTransform3D transform = currentAnimator.getCurrent( System.currentTimeMillis() );
473-
state.setViewerTransform( transform );
474-
if ( currentAnimator.isComplete() )
475-
currentAnimator = null;
476-
else
477-
requestRepaint();
478-
}
466+
final AffineTransform3D transform = animator.getCurrent( System.currentTimeMillis() );
467+
state.setViewerTransform( transform );
468+
if ( animator.isComplete() )
469+
currentAnimator.compareAndSet( animator, null );
470+
else
471+
requestRepaint();
479472
}
480473
}
481474

@@ -628,10 +621,10 @@ else if ( numTimepoints == 1 && sliderVisible )
628621
}
629622

630623
@Override
631-
public synchronized void setTransformAnimator( final AbstractTransformAnimator animator )
624+
public void setTransformAnimator( final AbstractTransformAnimator animator )
632625
{
633-
currentAnimator = animator;
634-
currentAnimator.setTime( System.currentTimeMillis() );
626+
animator.setTime( System.currentTimeMillis() );
627+
currentAnimator.set( animator );
635628
requestRepaint();
636629
}
637630

@@ -640,7 +633,7 @@ public synchronized void setTransformAnimator( final AbstractTransformAnimator a
640633
* interpolation modes: nearest-neighbor and N-linear.)
641634
*/
642635
// TODO: Deprecate or leave as convenience?
643-
public synchronized void toggleInterpolation()
636+
public void toggleInterpolation()
644637
{
645638
NavigationActions.toggleInterpolation( state );
646639
}
@@ -649,7 +642,7 @@ public synchronized void toggleInterpolation()
649642
* Set the {@link Interpolation} mode.
650643
*/
651644
// TODO: Deprecate or leave as convenience?
652-
public synchronized void setInterpolation( final Interpolation mode )
645+
public void setInterpolation( final Interpolation mode )
653646
{
654647
state.setInterpolation( mode );
655648
}
@@ -658,7 +651,7 @@ public synchronized void setInterpolation( final Interpolation mode )
658651
* Set the {@link DisplayMode}.
659652
*/
660653
// TODO: Deprecate or leave as convenience?
661-
public synchronized void setDisplayMode( final DisplayMode displayMode )
654+
public void setDisplayMode( final DisplayMode displayMode )
662655
{
663656
state.setDisplayMode( displayMode );
664657
}
@@ -679,7 +672,7 @@ public void setCurrentViewerTransform( final AffineTransform3D viewerTransform )
679672
* time-point index.
680673
*/
681674
// TODO: Deprecate or leave as convenience?
682-
public synchronized void setTimepoint( final int timepoint )
675+
public void setTimepoint( final int timepoint )
683676
{
684677
state.setCurrentTimepoint( timepoint );
685678
}
@@ -688,7 +681,7 @@ public synchronized void setTimepoint( final int timepoint )
688681
* Show the next time-point.
689682
*/
690683
// TODO: Deprecate or leave as convenience?
691-
public synchronized void nextTimePoint()
684+
public void nextTimePoint()
692685
{
693686
NavigationActions.nextTimePoint( state );
694687
}
@@ -697,7 +690,7 @@ public synchronized void nextTimePoint()
697690
* Show the previous time-point.
698691
*/
699692
// TODO: Deprecate or leave as convenience?
700-
public synchronized void previousTimePoint()
693+
public void previousTimePoint()
701694
{
702695
NavigationActions.previousTimePoint( state );
703696
}
@@ -934,15 +927,21 @@ public void removeTimePointListener( final TimePointListener listener )
934927
timePointListeners().remove( listener );
935928
}
936929

937-
public synchronized Element stateToXml()
930+
public Element stateToXml()
938931
{
939-
return new XmlIoViewerState().toXml( deprecatedState );
932+
synchronized ( state )
933+
{
934+
return new XmlIoViewerState().toXml( deprecatedState );
935+
}
940936
}
941937

942-
public synchronized void stateFromXml( final Element parent )
938+
public void stateFromXml( final Element parent )
943939
{
944-
final XmlIoViewerState io = new XmlIoViewerState();
945-
io.restoreFromXml( parent.getChild( io.getTagName() ), deprecatedState );
940+
synchronized ( state )
941+
{
942+
final XmlIoViewerState io = new XmlIoViewerState();
943+
io.restoreFromXml( parent.getChild( io.getTagName() ), deprecatedState );
944+
}
946945
}
947946

948947
/**

0 commit comments

Comments
 (0)