applies concurrency fixes
This commit is contained in:
1 parent
42e0d83403
commit
28eb1b673e
56 files changed
+1462
-372
No files matched your search
+85
@@ -0,0 +1,85 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2026 MINRES Technologies GmbH and others.
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* which accompanies this distribution, and is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
*
|
||||
* Contributors:
|
||||
* MINRES Technologies GmbH - initial API and implementation
|
||||
*******************************************************************************/
|
||||
package com.minres.scviewer.database.ui;
|
||||
|
||||
import java.util.IdentityHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.Consumer;
|
||||
import org.eclipse.core.runtime.IStatus;
|
||||
import org.eclipse.core.runtime.Status;
|
||||
import org.eclipse.core.runtime.jobs.Job;
|
||||
import com.minres.scviewer.database.ReadCancellation;
|
||||
import com.minres.scviewer.database.WaveformAccess;
|
||||
import com.minres.scviewer.database.tx.ITx;
|
||||
import com.minres.scviewer.database.tx.ITxAttribute;
|
||||
import com.minres.scviewer.database.tx.ITxRelation;
|
||||
|
||||
/** UI-owned, bounded snapshots. Accessors never call recording code on the UI thread. */
|
||||
public final class AsyncTransactionData implements AutoCloseable {
|
||||
public record Snapshot(List<ITxAttribute> attributes, List<ITxRelation> incoming, List<ITxRelation> outgoing) { }
|
||||
private static final Snapshot EMPTY = new Snapshot(List.of(), List.of(), List.of());
|
||||
private final Map<ITx, Snapshot> values = new IdentityHashMap<>();
|
||||
private final Map<ITx, Job> jobs = new IdentityHashMap<>();
|
||||
private final java.util.ArrayDeque<ITx> order = new java.util.ArrayDeque<>();
|
||||
private final boolean relations;
|
||||
private final Consumer<Runnable> dispatch;
|
||||
private final Consumer<ITx> changed;
|
||||
private volatile boolean closed;
|
||||
public AsyncTransactionData(Consumer<Runnable> dispatch, Consumer<ITx> changed) {
|
||||
this(dispatch, changed, true);
|
||||
}
|
||||
public AsyncTransactionData(Consumer<Runnable> dispatch, Consumer<ITx> changed, boolean relations) {
|
||||
this.relations = relations;
|
||||
this.dispatch = dispatch; this.changed = changed;
|
||||
}
|
||||
public Snapshot get(ITx tx) {
|
||||
if (closed || tx == null) return EMPTY;
|
||||
Snapshot value = values.get(tx);
|
||||
if (value != null) return value;
|
||||
if (!jobs.containsKey(tx)) {
|
||||
Job job = new Job("Load transaction details") {
|
||||
@Override protected IStatus run(org.eclipse.core.runtime.IProgressMonitor monitor) {
|
||||
try {
|
||||
Snapshot loaded = ReadCancellation.with(() -> closed || monitor.isCanceled(),
|
||||
() -> WaveformAccess.read(tx.getStream(), () -> new Snapshot(List.copyOf(tx.getAttributes()),
|
||||
relations ? List.copyOf(tx.getIncomingRelations()) : List.of(),
|
||||
relations ? List.copyOf(tx.getOutgoingRelations()) : List.of())));
|
||||
dispatch.accept(() -> {
|
||||
if (closed || jobs.get(tx) != this) return;
|
||||
jobs.remove(tx);
|
||||
if (values.size() >= 512) values.remove(order.removeFirst());
|
||||
values.put(tx, loaded); order.addLast(tx);
|
||||
changed.accept(tx);
|
||||
});
|
||||
return Status.OK_STATUS;
|
||||
} catch (java.util.concurrent.CancellationException e) {
|
||||
dispatch.accept(() -> { if (!closed && jobs.get(tx) == this) jobs.remove(tx); });
|
||||
return Status.CANCEL_STATUS;
|
||||
} catch (RuntimeException e) {
|
||||
dispatch.accept(() -> {
|
||||
if (!closed && jobs.get(tx) == this) { jobs.remove(tx); values.put(tx, EMPTY); order.addLast(tx); changed.accept(tx); }
|
||||
});
|
||||
return new Status(IStatus.ERROR, "com.minres.scviewer.database.ui.swt", "Cannot load transaction details", e);
|
||||
} finally { monitor.done(); }
|
||||
}
|
||||
};
|
||||
jobs.put(tx, job); job.setSystem(true); job.schedule();
|
||||
}
|
||||
return EMPTY;
|
||||
}
|
||||
public boolean isReady(ITx tx) { return values.containsKey(tx); }
|
||||
@Override public void close() {
|
||||
closed = true;
|
||||
jobs.values().forEach(Job::cancel);
|
||||
jobs.clear(); values.clear(); order.clear();
|
||||
}
|
||||
}
|
||||
+1
-5
@@ -11,9 +11,7 @@
|
||||
package com.minres.scviewer.database.ui;
|
||||
|
||||
import java.util.List;
|
||||
import com.minres.scviewer.database.BitVector;
|
||||
import com.minres.scviewer.database.CombinedBitVectorWaveform.SourceBit;
|
||||
import com.minres.scviewer.database.EventEntry;
|
||||
import com.minres.scviewer.database.WaveformType;
|
||||
|
||||
/** Selection and source normalization shared by the combine action and viewer. */
|
||||
@@ -23,9 +21,7 @@ public final class BitVectorComposition {
|
||||
public static boolean isSingleBit(TrackEntry track) {
|
||||
if (track.waveform == null || track.waveform.getType() != WaveformType.SIGNAL
|
||||
|| track.waveform.getWidth() != 1) return false;
|
||||
EventEntry first = track.waveform.getEvents().firstEntry();
|
||||
return first == null || (first.events != null && first.events.length > 0
|
||||
&& first.events[first.events.length - 1] instanceof BitVector);
|
||||
return track.waveform.isBitVector();
|
||||
}
|
||||
|
||||
public static List<TrackEntry> selectedBits(List<TrackEntry> roots, List<TrackEntry> selection) {
|
||||
|
||||
+2
-1
@@ -39,7 +39,8 @@ public final class BitWaveform extends HierNode implements IWaveform {
|
||||
|
||||
@Override public String getFullName() { return source.getFullName() + getName(); }
|
||||
@Override public String getDisplayName() { return source.getDisplayName() + getName(); }
|
||||
public IWaveform getSource() { return source; }
|
||||
@Override public java.util.List<IWaveform> getReadSources() { return java.util.List.of(source); }
|
||||
public IWaveform getSource() { return source; }
|
||||
public int getBitIndex() { return bit; }
|
||||
@Override public DirectionType getDirection() { return source.getDirection(); }
|
||||
@Override public long getId() { return source.getId(); }
|
||||
|
||||
+3
-7
@@ -16,8 +16,6 @@ import java.util.List;
|
||||
|
||||
import com.minres.scviewer.database.EmptyWaveform;
|
||||
import com.minres.scviewer.database.IWaveform;
|
||||
import com.minres.scviewer.database.BitVector;
|
||||
import com.minres.scviewer.database.EventEntry;
|
||||
import com.minres.scviewer.database.WaveformType;
|
||||
|
||||
public class TrackEntry {
|
||||
@@ -93,11 +91,9 @@ public class TrackEntry {
|
||||
return bitVector;
|
||||
}
|
||||
|
||||
private static boolean isBitVector(IWaveform waveform) {
|
||||
if (waveform == null || waveform.getType() != WaveformType.SIGNAL || waveform.getWidth() <= 1) return false;
|
||||
EventEntry first = waveform.getEvents().firstEntry();
|
||||
return first != null && first.events != null && first.events.length > 0 && first.events[0] instanceof BitVector;
|
||||
}
|
||||
private static boolean isBitVector(IWaveform waveform) {
|
||||
return waveform != null && waveform.getWidth() > 1 && waveform.isBitVector();
|
||||
}
|
||||
|
||||
public boolean isExpandable() {
|
||||
return isGroup() || isBitVector();
|
||||
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2026 MINRES Technologies GmbH and others.
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* which accompanies this distribution, and is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
*
|
||||
* Contributors:
|
||||
* MINRES Technologies GmbH - initial API and implementation
|
||||
*******************************************************************************/
|
||||
package com.minres.scviewer.database.ui;
|
||||
|
||||
import org.eclipse.swt.SWT;
|
||||
import org.eclipse.swt.SWTException;
|
||||
import org.eclipse.swt.widgets.Display;
|
||||
|
||||
/** Enqueue against a captured display, tolerating disposal during submission. */
|
||||
public final class UiDispatch {
|
||||
private UiDispatch() { }
|
||||
public static void async(Display display, Runnable action) {
|
||||
if (display.isDisposed()) return;
|
||||
try { display.asyncExec(() -> { if (!display.isDisposed()) action.run(); }); }
|
||||
catch (SWTException e) { if (e.code != SWT.ERROR_DEVICE_DISPOSED) throw e; }
|
||||
}
|
||||
}
|
||||
+5
-5
@@ -80,7 +80,7 @@ public class ArrowPainter implements IPainter {
|
||||
}
|
||||
|
||||
private int getConcurrencyIndex(ITx tx) {
|
||||
IEvent[] eventList = tx.getStream().getEventsBeforeTime(tx.getBeginTime());
|
||||
IEvent[] eventList = waveCanvas.value(tx.getStream(), tx.getBeginTime());
|
||||
Optional<Integer> res = Arrays.stream(eventList).filter(e -> ((ITxEvent)e).getTransaction()== tx).map(e -> ((ITxEvent)e).getRowIndex()).findFirst();
|
||||
if(res.isPresent() && res.get()>0) {
|
||||
return res.get();
|
||||
@@ -89,10 +89,10 @@ public class ArrowPainter implements IPainter {
|
||||
}
|
||||
|
||||
private Rectangle getBoundingBox(IWaveformPainter painter, ITx itx, IWaveform stream) {
|
||||
int laneHeight = painter.getHeight() / stream.getRowCount();
|
||||
int laneHeight = painter.getHeight() / waveCanvas.rows(stream);
|
||||
//TODO: it needs to take the concurrency index within the current waveform (stream or generator) into account
|
||||
int concIdx=getConcurrencyIndex(itx);
|
||||
int laneOffset = laneHeight * (concIdx>=stream.getRowCount()?stream.getRowCount()-1:concIdx);
|
||||
int laneOffset = laneHeight * (concIdx>=waveCanvas.rows(stream)?waveCanvas.rows(stream)-1:concIdx);
|
||||
int rulerHeight = waveCanvas.rulerHeight;
|
||||
Rectangle bb = new Rectangle((int) (itx.getBeginTime() / scaleFactor),
|
||||
rulerHeight + painter.getVerticalOffset() + laneOffset,
|
||||
@@ -115,8 +115,8 @@ public class ArrowPainter implements IPainter {
|
||||
}
|
||||
Rectangle bb = getBoundingBox(painter, tx, painter instanceof StreamPainter? ((StreamPainter)painter).getStream() : tx.getStream());
|
||||
txRectangle = bb;
|
||||
deriveGeom(tx.getIncomingRelations(), iRect, false);
|
||||
deriveGeom(tx.getOutgoingRelations(), oRect, true);
|
||||
deriveGeom(waveCanvas.transactionData.get(tx).incoming(), iRect, false);
|
||||
deriveGeom(waveCanvas.transactionData.get(tx).outgoing(), oRect, true);
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
+4
-4
@@ -103,18 +103,18 @@ public class SignalPainter extends TrackPainter {
|
||||
long beginTime = beginPos*scaleFactor;
|
||||
long endTime = beginTime + area.width*scaleFactor;
|
||||
|
||||
EventEntry first = signal.getEvents().floorEntry(beginTime);
|
||||
EventEntry first = waveCanvas.events(signal).floorEntry(beginTime);
|
||||
if (first == null)
|
||||
first = signal.getEvents().firstEntry();
|
||||
first = waveCanvas.events(signal).firstEntry();
|
||||
if (first == null || Long.compareUnsigned(first.timestamp, endTime) > 0) return;
|
||||
beginTime = first.timestamp;
|
||||
proj.setForeground(this.waveCanvas.styleProvider.getColor(WaveformColors.LINE));
|
||||
proj.setLineStyle(SWT.LINE_SOLID);
|
||||
proj.setLineWidth(1);
|
||||
IEventList entries = signal.getEvents().subMap(beginTime, true, endTime);
|
||||
IEventList entries = waveCanvas.events(signal).subMap(beginTime, true, endTime);
|
||||
if (entries.isEmpty()) return;
|
||||
SignalChange left = new SignalChange(entries.firstEntry());
|
||||
SignalChange right = new SignalChange(entries.size() > 1 ? entries.higherEntry(left.time) : entries.firstEntry());
|
||||
SignalChange right = new SignalChange(entries.higherEntry(left.time) != null ? entries.higherEntry(left.time) : entries.firstEntry());
|
||||
maxPosX = area.x + area.width;
|
||||
yOffsetT = this.waveCanvas.styleProvider.getTrackHeight() / 5 + area.y;
|
||||
yOffsetM = this.waveCanvas.styleProvider.getTrackHeight() / 2 + area.y;
|
||||
|
||||
+9
-20
@@ -18,7 +18,6 @@ import org.eclipse.swt.graphics.Rectangle;
|
||||
|
||||
import com.minres.scviewer.database.EventKind;
|
||||
import com.minres.scviewer.database.IEvent;
|
||||
import com.minres.scviewer.database.IEventList;
|
||||
import com.minres.scviewer.database.IWaveform;
|
||||
import com.minres.scviewer.database.tx.ITx;
|
||||
import com.minres.scviewer.database.tx.ITxEvent;
|
||||
@@ -35,9 +34,6 @@ public class StreamPainter extends TrackPainter{
|
||||
private IWaveform stream;
|
||||
private int txBase;
|
||||
private int txHeight;
|
||||
private TxIntervalIndex intervalIndex;
|
||||
private IEventList indexedEvents;
|
||||
private int indexedSize = -1;
|
||||
|
||||
public StreamPainter(WaveformCanvas waveCanvas, boolean even, TrackEntry trackEntry) {
|
||||
super(trackEntry, even);
|
||||
@@ -46,8 +42,8 @@ public class StreamPainter extends TrackPainter{
|
||||
}
|
||||
|
||||
public void paintArea(Projection proj, Rectangle area) {
|
||||
int trackHeight=trackEntry.height/stream.getRowCount();
|
||||
if(stream.getEvents().size()==0) {
|
||||
int trackHeight=trackEntry.height/waveCanvas.rows(stream);
|
||||
if(waveCanvas.events(stream).size()==0) {
|
||||
proj.setFillRule(SWT.FILL_EVEN_ODD);
|
||||
proj.setLineStyle(SWT.LINE_SOLID);
|
||||
proj.setLineWidth(1);
|
||||
@@ -79,7 +75,9 @@ public class StreamPainter extends TrackPainter{
|
||||
for( int y1=area.y+trackHeight/2; y1<area.y+trackEntry.height; y1+=trackHeight)
|
||||
proj.drawLine(area.x, y1, area.x+area.width, y1);
|
||||
ITxEvent highlighted = null;
|
||||
for (ITxEvent event : getIntervalIndex().overlapping(beginTime, endTime)) {
|
||||
TxIntervalIndex index = getIntervalIndex();
|
||||
if (index == null) return;
|
||||
for (ITxEvent event : index.overlapping(beginTime, endTime)) {
|
||||
ITx tx = event.getTransaction();
|
||||
drawTx(proj, area, tx, event.getRowIndex(), false);
|
||||
if (waveCanvas.currentSelectedTx != null && waveCanvas.currentSelectedTx.getId() == tx.getId())
|
||||
@@ -91,16 +89,7 @@ public class StreamPainter extends TrackPainter{
|
||||
}
|
||||
}
|
||||
|
||||
private TxIntervalIndex getIntervalIndex() {
|
||||
stream.getRowCount();
|
||||
IEventList events = stream.getEvents();
|
||||
if (intervalIndex == null || indexedEvents != events || indexedSize != events.size()) {
|
||||
intervalIndex = new TxIntervalIndex(events);
|
||||
indexedEvents = events;
|
||||
indexedSize = events.size();
|
||||
}
|
||||
return intervalIndex;
|
||||
}
|
||||
private TxIntervalIndex getIntervalIndex() { return waveCanvas.intervals(stream); }
|
||||
|
||||
protected void drawTx(Projection proj, Rectangle area, ITx tx, int concurrencyIndex, boolean highlighted ) {
|
||||
// compute colors
|
||||
@@ -140,7 +129,9 @@ public class StreamPainter extends TrackPainter{
|
||||
long time = (long) point.x * scale;
|
||||
long low = Math.max(0L, (long) point.x - 5) * scale;
|
||||
long high = ((long) point.x + 5) * scale;
|
||||
for (ITxEvent event : getIntervalIndex().overlapping(low, high)) {
|
||||
TxIntervalIndex index = getIntervalIndex();
|
||||
if (index == null) return null;
|
||||
for (ITxEvent event : index.overlapping(low, high)) {
|
||||
if (event.getRowIndex() != lane) continue;
|
||||
ITx tx = event.getTransaction();
|
||||
if (event.getKind() == EventKind.SINGLE ||
|
||||
@@ -155,8 +146,6 @@ public class StreamPainter extends TrackPainter{
|
||||
|
||||
public void setStream(IWaveform stream) {
|
||||
this.stream = stream;
|
||||
intervalIndex = null;
|
||||
indexedEvents = null;
|
||||
}
|
||||
|
||||
protected ITx getTxFromEntry(int lane, int offset, IEvent[] firstTx) {
|
||||
|
||||
+39
-4
@@ -51,6 +51,32 @@ import com.minres.scviewer.database.ui.swt.Constants;
|
||||
import com.minres.scviewer.database.ui.swt.internal.slider.ZoomBar;
|
||||
|
||||
public class WaveformCanvas extends Canvas implements IWaveformZoom{
|
||||
private final WaveformData data;
|
||||
final org.eclipse.swt.widgets.Display uiDisplay;
|
||||
final com.minres.scviewer.database.ui.AsyncTransactionData transactionData;
|
||||
Runnable dataChanged = () -> redraw();
|
||||
|
||||
com.minres.scviewer.database.IEventList events(IWaveform waveform) {
|
||||
return data.events(waveform, Math.max(0, getMinVisibleTime()), getMaxVisibleTime(), Math.max(1, getScale()));
|
||||
}
|
||||
com.minres.scviewer.database.IEvent[] value(IWaveform waveform, long time) {
|
||||
EventEntry entry = data.events(waveform, time, time, 1).floorEntry(time);
|
||||
return entry == null ? new IEvent[0] : entry.events;
|
||||
}
|
||||
void retainData(List<TrackEntry> tracks) {
|
||||
java.util.Set<IWaveform> retained = java.util.Collections.newSetFromMap(new java.util.IdentityHashMap<>());
|
||||
collectWaveforms(tracks, retained);
|
||||
data.retain(retained);
|
||||
}
|
||||
private void collectWaveforms(List<TrackEntry> tracks, java.util.Set<IWaveform> retained) {
|
||||
for (TrackEntry track : tracks) {
|
||||
if (track.waveform != null) retained.add(track.waveform);
|
||||
collectWaveforms(track.waveforms, retained);
|
||||
}
|
||||
}
|
||||
int rows(IWaveform waveform) { return data.rows(waveform); }
|
||||
com.minres.scviewer.database.tx.TxIntervalIndex intervals(IWaveform waveform) { return data.intervals(waveform); }
|
||||
|
||||
|
||||
public static final long ZOOM_FIT = -2;
|
||||
|
||||
@@ -106,6 +132,14 @@ public class WaveformCanvas extends Canvas implements IWaveformZoom{
|
||||
|
||||
public WaveformCanvas(final Composite parent, int style, IWaveformStyleProvider styleProvider, ZoomBar.IProvider scrollbarProvider) {
|
||||
super(parent, (style & ~SWT.H_SCROLL) | SWT.DOUBLE_BUFFERED | SWT.NO_BACKGROUND | SWT.V_SCROLL );
|
||||
var display = parent.getDisplay();
|
||||
uiDisplay = display;
|
||||
java.util.function.Consumer<Runnable> dispatch = task -> com.minres.scviewer.database.ui.UiDispatch.async(display, task);
|
||||
data = new WaveformData(dispatch, () -> { if (!isDisposed()) dataChanged.run(); });
|
||||
transactionData = new com.minres.scviewer.database.ui.AsyncTransactionData(dispatch, tx -> {
|
||||
if (!isDisposed()) { if (arrowPainter != null) arrowPainter.updateSelection(); redraw(); }
|
||||
});
|
||||
addDisposeListener(event -> { data.close(); transactionData.close(); });
|
||||
this.styleProvider=styleProvider;
|
||||
addControlListener(new ControlAdapter() { /* resize listener. */
|
||||
@Override
|
||||
@@ -321,7 +355,7 @@ public class WaveformCanvas extends Canvas implements IWaveformZoom{
|
||||
setScalingFactor(factor, lowerTime+time_diff/2);
|
||||
} else {
|
||||
origin.x = -(int) (lowerTime/getScale());
|
||||
WaveformCanvas.this.getDisplay().asyncExec(() -> {redraw();});
|
||||
WaveformCanvas.this.getDisplay().asyncExec(() -> { if (!isDisposed()) redraw(); });
|
||||
}
|
||||
lastHorSelection=sel;
|
||||
}
|
||||
@@ -339,7 +373,7 @@ public class WaveformCanvas extends Canvas implements IWaveformZoom{
|
||||
if (!painterList.isEmpty()) {
|
||||
origin.y=-vertical.getSelection();
|
||||
fireSelectionEvent();
|
||||
WaveformCanvas.this.getDisplay().asyncExec(() -> {redraw();});
|
||||
WaveformCanvas.this.getDisplay().asyncExec(() -> { if (!isDisposed()) redraw(); });
|
||||
}
|
||||
}
|
||||
});
|
||||
@@ -475,20 +509,21 @@ public class WaveformCanvas extends Canvas implements IWaveformZoom{
|
||||
setOrigin(size.x - higher, origin.y);
|
||||
}
|
||||
if(currentSelectedTrack != null) {
|
||||
EventEntry entry = currentSelectedTrack.waveform.getEvents().floorEntry(tx.getBeginTime());
|
||||
EventEntry entry = events(currentSelectedTrack.waveform).floorEntry(tx.getBeginTime());
|
||||
IWaveformPainter painter = trackAreaPainter.getPainterFor(currentSelectedTrack);
|
||||
if (painter != null && painter instanceof StreamPainter)
|
||||
reveal(tx, size, painter, entry);
|
||||
} else
|
||||
for (IWaveformPainter painter : wave2painterMap.values()) {
|
||||
if (painter instanceof StreamPainter && ((StreamPainter) painter).getStream() == tx.getStream()) {
|
||||
EventEntry entry = tx.getStream().getEvents().floorEntry(tx.getBeginTime());
|
||||
EventEntry entry = events(tx.getStream()).floorEntry(tx.getBeginTime());
|
||||
reveal(tx, size, painter, entry);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void reveal(ITx tx, Point size, IWaveformPainter painter, EventEntry entry) {
|
||||
if (entry == null) return;
|
||||
Optional<IEvent> res = Arrays.stream(entry.events).filter(e -> ((ITxEvent)e).getTransaction().equals(tx)).findFirst();
|
||||
if(res.isPresent()) {
|
||||
int top = painter.getVerticalOffset() + styleProvider.getTrackHeight() * ((ITxEvent)res.get()).getRowIndex();
|
||||
|
||||
+145
@@ -0,0 +1,145 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2026 MINRES Technologies GmbH and others.
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* which accompanies this distribution, and is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
*
|
||||
* Contributors:
|
||||
* MINRES Technologies GmbH - initial API and implementation
|
||||
*******************************************************************************/
|
||||
package com.minres.scviewer.database.ui.swt.internal;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.function.Consumer;
|
||||
import org.eclipse.core.runtime.IProgressMonitor;
|
||||
import org.eclipse.core.runtime.IStatus;
|
||||
import org.eclipse.core.runtime.Status;
|
||||
import org.eclipse.core.runtime.jobs.Job;
|
||||
import com.minres.scviewer.database.*;
|
||||
import com.minres.scviewer.database.ui.BitWaveform;
|
||||
import com.minres.scviewer.database.tx.TxIntervalIndex;
|
||||
|
||||
/** UI-owned snapshots of recording data. Painting never triggers a lazy read. */
|
||||
final class WaveformData implements AutoCloseable {
|
||||
private record Request(IWaveform waveform, long from, long to, long step) { }
|
||||
private record Snapshot(IEventList events, int rows, TxIntervalIndex intervals) { }
|
||||
private static final IEventList EMPTY = new EventList();
|
||||
private final Map<Request, Snapshot> values = new LinkedHashMap<>(32, .75f, true);
|
||||
private final Map<Request, Job> jobs = new LinkedHashMap<>();
|
||||
private final Consumer<Runnable> dispatch;
|
||||
private final Runnable changed;
|
||||
private volatile boolean closed;
|
||||
WaveformData(Consumer<Runnable> dispatch, Runnable changed) { this.dispatch = dispatch; this.changed = changed; }
|
||||
|
||||
private static boolean derived(IWaveform waveform) {
|
||||
while (waveform instanceof BitWaveform bit) waveform = bit.getSource();
|
||||
return waveform instanceof CombinedBitVectorWaveform;
|
||||
}
|
||||
IEventList events(IWaveform waveform, long from, long to, long step) {
|
||||
Snapshot value = request(waveform, from, to, step);
|
||||
return value == null ? EMPTY : value.events();
|
||||
}
|
||||
int rows(IWaveform waveform) {
|
||||
if (!waveform.getType().isTransaction()) return 1;
|
||||
Snapshot value = request(waveform, 0, -1L, 0);
|
||||
return value == null ? 1 : Math.max(1, value.rows());
|
||||
}
|
||||
TxIntervalIndex intervals(IWaveform waveform) {
|
||||
Snapshot value = request(waveform, 0, -1L, 0);
|
||||
return value == null ? null : value.intervals();
|
||||
}
|
||||
private Snapshot request(IWaveform waveform, long from, long to, long step) {
|
||||
if (closed) return null;
|
||||
boolean sample = derived(waveform);
|
||||
if (!sample) { from = 0; to = -1L; step = 0; }
|
||||
// Metadata callers do not request an unbounded derived timeline.
|
||||
else if (step == 0) return null;
|
||||
Request request = new Request(waveform, from, to, Math.max(0, step));
|
||||
Snapshot value = values.get(request);
|
||||
if (value != null) return value;
|
||||
if (!jobs.containsKey(request)) {
|
||||
// Supersede obsolete ranges and cursor samples independently.
|
||||
if (sample) {
|
||||
var stale = jobs.entrySet().iterator();
|
||||
while (stale.hasNext()) {
|
||||
var entry = stale.next();
|
||||
if (entry.getKey().waveform() == waveform
|
||||
&& (entry.getKey().from() == entry.getKey().to()) == (request.from() == request.to())) {
|
||||
entry.getValue().cancel(); stale.remove();
|
||||
}
|
||||
}
|
||||
}
|
||||
Job job = new Job("Prepare waveform") {
|
||||
@Override protected IStatus run(IProgressMonitor monitor) {
|
||||
try {
|
||||
Snapshot loaded = ReadCancellation.with(() -> closed || monitor.isCanceled(),
|
||||
() -> WaveformAccess.read(waveform, () -> {
|
||||
IEventList events = waveform.getEvents();
|
||||
IEventList ready = sample ? sample(events, request) : events;
|
||||
int rows = waveform.getRowCount();
|
||||
return new Snapshot(ready, rows, waveform.getType().isTransaction() ? new TxIntervalIndex(ready) : null);
|
||||
}));
|
||||
if (closed || monitor.isCanceled()) return Status.CANCEL_STATUS;
|
||||
dispatch.accept(() -> {
|
||||
if (closed || jobs.get(request) != this) return;
|
||||
jobs.remove(request);
|
||||
values.put(request, loaded);
|
||||
if (sample) {
|
||||
long count = values.keySet().stream().filter(key -> key.waveform() == waveform).count();
|
||||
var old = values.keySet().iterator();
|
||||
while (count > 8 && old.hasNext()) {
|
||||
if (old.next().waveform() == waveform) { old.remove(); count--; }
|
||||
}
|
||||
}
|
||||
changed.run();
|
||||
});
|
||||
return Status.OK_STATUS;
|
||||
} catch (java.util.concurrent.CancellationException e) {
|
||||
dispatch.accept(() -> { if (!closed && jobs.get(request) == this) jobs.remove(request); });
|
||||
return Status.CANCEL_STATUS;
|
||||
} catch (RuntimeException e) {
|
||||
dispatch.accept(() -> {
|
||||
if (!closed && jobs.get(request) == this) { jobs.remove(request); values.put(request, new Snapshot(EMPTY, 1, null)); changed.run(); }
|
||||
});
|
||||
return new Status(IStatus.ERROR, "com.minres.scviewer.database.ui.swt", "Cannot prepare waveform", e);
|
||||
} finally { monitor.done(); }
|
||||
}
|
||||
};
|
||||
jobs.put(request, job); job.setSystem(true); job.schedule();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
/** At most a few samples per pixel, preserving boundaries and dense-change markers. */
|
||||
private static IEventList sample(IEventList input, Request request) {
|
||||
EventList result = new EventList();
|
||||
add(result, input.floorEntry(request.from()));
|
||||
for (long time = request.from(); Long.compareUnsigned(time, request.to()) <= 0;) {
|
||||
ReadCancellation.check();
|
||||
add(result, input.floorEntry(time));
|
||||
add(result, input.ceilingEntry(time));
|
||||
long next = time + Math.max(1, request.step());
|
||||
if (Long.compareUnsigned(next, time) <= 0) break;
|
||||
time = next;
|
||||
}
|
||||
add(result, input.floorEntry(request.to()));
|
||||
return result;
|
||||
}
|
||||
private static void add(EventList result, EventEntry entry) {
|
||||
if (entry != null && entry.events != null && !result.containsKey(entry.timestamp))
|
||||
for (IEvent event : entry.events) result.put(entry.timestamp, event);
|
||||
}
|
||||
void retain(java.util.Set<IWaveform> waveforms) {
|
||||
values.keySet().removeIf(request -> !waveforms.contains(request.waveform()));
|
||||
var pending = jobs.entrySet().iterator();
|
||||
while (pending.hasNext()) {
|
||||
var entry = pending.next();
|
||||
if (!waveforms.contains(entry.getKey().waveform())) { entry.getValue().cancel(); pending.remove(); }
|
||||
}
|
||||
}
|
||||
@Override public void close() {
|
||||
closed = true;
|
||||
jobs.values().forEach(Job::cancel); jobs.clear(); values.clear();
|
||||
}
|
||||
}
|
||||
+27
-20
@@ -311,11 +311,11 @@ public class WaveformView implements IWaveformView {
|
||||
EventEntry ceilEntry = null;
|
||||
if (o instanceof TrackEntry) {
|
||||
TrackEntry entry = (TrackEntry) o;
|
||||
IEventList map = entry.waveform.getEvents();
|
||||
IEventList map = waveformCanvas.events(entry.waveform);
|
||||
floorEntry = map.floorEntry(time);
|
||||
ceilEntry = map.ceilingEntry(time);
|
||||
} else if (o instanceof ITx) {
|
||||
IEventList map = ((ITx) o).getStream().getEvents();
|
||||
IEventList map = waveformCanvas.events(((ITx) o).getStream());
|
||||
floorEntry = map.floorEntry(time);
|
||||
ceilEntry = map.ceilingEntry(time);
|
||||
}
|
||||
@@ -512,6 +512,7 @@ public class WaveformView implements IWaveformView {
|
||||
valueFill = createFill(valuePane);
|
||||
|
||||
waveformCanvas.setMaxTime(1);
|
||||
waveformCanvas.dataChanged = this::update;
|
||||
waveformCanvas.addPaintListener(waveformMouseListener);
|
||||
waveformCanvas.addListener(SWT.MouseDown, waveformMouseListener);
|
||||
waveformCanvas.addListener(SWT.MouseUp, waveformMouseListener);
|
||||
@@ -632,6 +633,7 @@ public class WaveformView implements IWaveformView {
|
||||
public void update() {
|
||||
if (waveformCanvas.isDisposed()) return;
|
||||
updateQueued = false;
|
||||
waveformCanvas.retainData(streams);
|
||||
setStreamDropLocation(null);
|
||||
int scrollY = -waveformCanvas.getOrigin().y;
|
||||
layoutInProgress = true;
|
||||
@@ -728,7 +730,7 @@ public class WaveformView implements IWaveformView {
|
||||
streamEntry.vOffset = tracksVerticalHeight;
|
||||
if (streamEntry.waveform.getType().isTransaction()) {
|
||||
streamEntry.currentValue = "";
|
||||
streamEntry.height *= streamEntry.waveform.getRowCount();
|
||||
streamEntry.height *= waveformCanvas.rows(streamEntry.waveform);
|
||||
IWaveformPainter painter = new StreamPainter(waveformCanvas, even, streamEntry);
|
||||
waveformCanvas.addWaveformPainter(painter, false);
|
||||
} else if (streamEntry.waveform.getType() == WaveformType.SIGNAL) {
|
||||
@@ -756,7 +758,7 @@ public class WaveformView implements IWaveformView {
|
||||
final Long time = getCursorTime();
|
||||
TrackEntry.visibleTracks(streams).stream().forEach(entry -> {
|
||||
if (entry.waveform.getType() == WaveformType.SIGNAL) {
|
||||
IEvent[] value = entry.waveform.getEventsBeforeTime(time);
|
||||
IEvent[] value = waveformCanvas.value(entry.waveform, time);
|
||||
if (value == null || value.length == 0) {
|
||||
entry.currentValue = "---";
|
||||
return;
|
||||
@@ -788,8 +790,8 @@ public class WaveformView implements IWaveformView {
|
||||
entry.currentValue = Double.toString(val);
|
||||
}
|
||||
} else if (entry.waveform.getType().isTransaction()) {
|
||||
ITx[] resultsList = new ITx[entry.waveform.getRowCount()];
|
||||
EventEntry firstTx = entry.waveform.getEvents().floorEntry(time);
|
||||
ITx[] resultsList = new ITx[waveformCanvas.rows(entry.waveform)];
|
||||
EventEntry firstTx = waveformCanvas.events(entry.waveform).floorEntry(time);
|
||||
if (firstTx != null) {
|
||||
do {
|
||||
for (IEvent e : firstTx.events) {
|
||||
@@ -802,7 +804,7 @@ public class WaveformView implements IWaveformView {
|
||||
resultsList[evt.getRowIndex()] = evt.getTransaction();
|
||||
}
|
||||
}
|
||||
firstTx = entry.waveform.getEvents().lowerEntry(firstTx.timestamp);
|
||||
firstTx = waveformCanvas.events(entry.waveform).lowerEntry(firstTx.timestamp);
|
||||
} while (firstTx != null && !isArrayFull(resultsList));
|
||||
boolean separator = false;
|
||||
StringBuilder sb = new StringBuilder();
|
||||
@@ -1028,11 +1030,13 @@ public class WaveformView implements IWaveformView {
|
||||
return;
|
||||
TrackEntry selectedWaveform = currentWaveformSelection.size() == 1 ? currentWaveformSelection.get(0)
|
||||
: getEntryFor(currentTxSelection);
|
||||
if (selectedWaveform == null) return;
|
||||
if (selectedWaveform.waveform.getType().isTransaction() && currentTxSelection != null) {
|
||||
if (relationType.equals(IWaveformView.NEXT_PREV_IN_STREAM)) {
|
||||
ITx transaction = null;
|
||||
if (direction == GotoDirection.NEXT) {
|
||||
IEvent[] eventsList = selectedWaveform.waveform.getEvents().get(currentTxSelection.getBeginTime());
|
||||
IEvent[] eventsList = waveformCanvas.events(selectedWaveform.waveform).get(currentTxSelection.getBeginTime());
|
||||
if (eventsList == null) return;
|
||||
boolean meFound = false;
|
||||
for (IEvent evt : eventsList) {
|
||||
if (evt instanceof ITxEvent && evt.getKind() == EventKind.BEGIN
|
||||
@@ -1045,7 +1049,7 @@ public class WaveformView implements IWaveformView {
|
||||
}
|
||||
}
|
||||
if (transaction == null) {
|
||||
EventEntry entry = selectedWaveform.waveform.getEvents()
|
||||
EventEntry entry = waveformCanvas.events(selectedWaveform.waveform)
|
||||
.higherEntry(currentTxSelection.getBeginTime());
|
||||
if (entry != null)
|
||||
do {
|
||||
@@ -1057,11 +1061,12 @@ public class WaveformView implements IWaveformView {
|
||||
}
|
||||
}
|
||||
if (transaction == null)
|
||||
entry = selectedWaveform.waveform.getEvents().higherEntry(entry.timestamp);
|
||||
entry = waveformCanvas.events(selectedWaveform.waveform).higherEntry(entry.timestamp);
|
||||
} while (entry != null && transaction == null);
|
||||
}
|
||||
} else if (direction == GotoDirection.PREV) {
|
||||
IEvent[] eventsList = selectedWaveform.waveform.getEvents().get(currentTxSelection.getBeginTime());
|
||||
IEvent[] eventsList = waveformCanvas.events(selectedWaveform.waveform).get(currentTxSelection.getBeginTime());
|
||||
if (eventsList == null) return;
|
||||
boolean meFound = false;
|
||||
for (IEvent evt : Lists.reverse(Arrays.asList(eventsList))) {
|
||||
if (evt instanceof ITxEvent
|
||||
@@ -1074,7 +1079,7 @@ public class WaveformView implements IWaveformView {
|
||||
}
|
||||
}
|
||||
if (transaction == null) {
|
||||
EventEntry entry = selectedWaveform.waveform.getEvents()
|
||||
EventEntry entry = waveformCanvas.events(selectedWaveform.waveform)
|
||||
.lowerEntry(currentTxSelection.getBeginTime());
|
||||
if (entry != null)
|
||||
do {
|
||||
@@ -1086,7 +1091,7 @@ public class WaveformView implements IWaveformView {
|
||||
}
|
||||
}
|
||||
if (transaction == null)
|
||||
entry = selectedWaveform.waveform.getEvents().lowerEntry(entry.timestamp);
|
||||
entry = waveformCanvas.events(selectedWaveform.waveform).lowerEntry(entry.timestamp);
|
||||
} while (entry != null && transaction == null);
|
||||
}
|
||||
}
|
||||
@@ -1096,12 +1101,12 @@ public class WaveformView implements IWaveformView {
|
||||
} else {
|
||||
ITxRelation tx = null;
|
||||
if (direction == GotoDirection.NEXT) {
|
||||
Collection<ITxRelation> outRel = currentTxSelection.getOutgoingRelations();
|
||||
Collection<ITxRelation> outRel = waveformCanvas.transactionData.get(currentTxSelection).outgoing();
|
||||
tx = selectTxToNavigateTo(outRel, relationType, true);
|
||||
if (tx != null)
|
||||
setSelection(new StructuredSelection(tx.getTarget()), true);
|
||||
} else if (direction == GotoDirection.PREV) {
|
||||
Collection<ITxRelation> inRel = currentTxSelection.getIncomingRelations();
|
||||
Collection<ITxRelation> inRel = waveformCanvas.transactionData.get(currentTxSelection).incoming();
|
||||
tx = selectTxToNavigateTo(inRel, relationType, false);
|
||||
if (tx != null)
|
||||
setSelection(new StructuredSelection(tx.getSource()), true);
|
||||
@@ -1152,7 +1157,7 @@ public class WaveformView implements IWaveformView {
|
||||
long time = getCursorTime();
|
||||
IEventList map = null;
|
||||
if (sel.waveform.getType().isTransaction() || sel.waveform.getType() == WaveformType.SIGNAL) {
|
||||
map = sel.waveform.getEvents();
|
||||
map = waveformCanvas.events(sel.waveform);
|
||||
}
|
||||
if (map != null) {
|
||||
EventEntry entry = direction == GotoDirection.PREV ? map.lowerEntry(time) : map.higherEntry(time);
|
||||
@@ -1242,13 +1247,13 @@ public class WaveformView implements IWaveformView {
|
||||
if (lastKey.equals(firstKey)) {
|
||||
TrackEntry trackEntry = trackVerticalOffset.get(firstKey);
|
||||
IWaveform w = trackEntry.waveform;
|
||||
subArea.height = w.getRowCount() * styleProvider.getTrackHeight();
|
||||
subArea.height = waveformCanvas.rows(w) * styleProvider.getTrackHeight();
|
||||
drawValue(gc, subArea, firstKey, trackEntry.currentValue, trackEntry.selected);
|
||||
} else {
|
||||
for (Entry<Integer, TrackEntry> entry : trackVerticalOffset.subMap(firstKey, true, lastKey, true)
|
||||
.entrySet()) {
|
||||
IWaveform w = entry.getValue().waveform;
|
||||
subArea.height = w.getRowCount() * styleProvider.getTrackHeight();
|
||||
subArea.height = waveformCanvas.rows(w) * styleProvider.getTrackHeight();
|
||||
drawValue(gc, subArea, entry.getKey(), entry.getValue().currentValue,
|
||||
entry.getValue().selected);
|
||||
}
|
||||
@@ -1260,7 +1265,7 @@ public class WaveformView implements IWaveformView {
|
||||
|
||||
protected void drawName(GC gc, Rectangle subArea, Integer firstKey, TrackEntry entry) {
|
||||
IWaveform w = entry.waveform;
|
||||
subArea.height = w.getRowCount() * styleProvider.getTrackHeight();
|
||||
subArea.height = waveformCanvas.rows(w) * styleProvider.getTrackHeight();
|
||||
drawTextFormat(gc, subArea, firstKey, w.getDisplayName(), entry.selected, entry.hierState, nameIndent(entry));
|
||||
}
|
||||
|
||||
@@ -1709,7 +1714,9 @@ public class WaveformView implements IWaveformView {
|
||||
}
|
||||
|
||||
public void asyncUpdate(Widget widget) {
|
||||
widget.getDisplay().asyncExec(() -> {
|
||||
if (widget.isDisposed()) return;
|
||||
com.minres.scviewer.database.ui.UiDispatch.async(waveformCanvas.uiDisplay, () -> {
|
||||
if (widget.isDisposed() || waveformCanvas.isDisposed()) return;
|
||||
waveformCanvas.redraw();
|
||||
updateValueList();
|
||||
});
|
||||
|
||||
+11
-4
@@ -19,6 +19,7 @@ public class ActionTimer implements Runnable {
|
||||
|
||||
private final Display display;
|
||||
private final TimerAction timerAction;
|
||||
private boolean active;
|
||||
|
||||
public interface TimerAction extends Runnable {
|
||||
boolean isEnabled();
|
||||
@@ -30,16 +31,22 @@ public class ActionTimer implements Runnable {
|
||||
}
|
||||
|
||||
public void activate() {
|
||||
if( timerAction.isEnabled() ) {
|
||||
if (!display.isDisposed() && timerAction.isEnabled()) {
|
||||
active = true;
|
||||
display.timerExec( INITIAL_DELAY, this );
|
||||
}
|
||||
}
|
||||
|
||||
public void cancel() {
|
||||
active = false;
|
||||
if (!display.isDisposed()) display.timerExec(-1, this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
if( timerAction.isEnabled() ) {
|
||||
timerAction.run();
|
||||
display.timerExec( FAST_DELAY, this );
|
||||
if (active && !display.isDisposed() && timerAction.isEnabled()) {
|
||||
timerAction.run();
|
||||
if (active && !display.isDisposed() && timerAction.isEnabled()) display.timerExec(FAST_DELAY, this);
|
||||
}
|
||||
}
|
||||
}
|
||||
+4
-1
@@ -46,11 +46,13 @@ public class ImageButton extends Composite
|
||||
}
|
||||
@Override
|
||||
public boolean isEnabled() {
|
||||
return pressed;
|
||||
return !isDisposed() && pressed;
|
||||
}
|
||||
};
|
||||
actionTimer = new ActionTimer(timerAction, this.getDisplay() );
|
||||
addListener(SWT.Dispose, event -> {
|
||||
pressed = false;
|
||||
actionTimer.cancel();
|
||||
if (hoverImage != null) hoverImage.dispose();
|
||||
if (normalImage != null) normalImage.dispose();
|
||||
if (pressedImage != null) pressedImage.dispose();
|
||||
@@ -87,6 +89,7 @@ public class ImageButton extends Composite
|
||||
@Override
|
||||
public void mouseUp(MouseEvent e) {
|
||||
pressed=false;
|
||||
actionTimer.cancel();
|
||||
redraw();
|
||||
}
|
||||
});
|
||||
|
||||
+3
-1
@@ -22,7 +22,9 @@ public class ActionScheduler {
|
||||
this.action = action;
|
||||
}
|
||||
|
||||
public void cancel() { if (!display.isDisposed()) display.timerExec(-1, action); }
|
||||
|
||||
public void schedule( int delay ) {
|
||||
display.timerExec( delay, action );
|
||||
if (!display.isDisposed()) display.timerExec(delay, action);
|
||||
}
|
||||
}
|
||||
+2
-1
@@ -42,6 +42,7 @@ class ClickControl extends ControlAdapter implements ViewComponent, MouseDownAct
|
||||
this.control.addMouseTrackListener( this );
|
||||
this.control.addMouseListener( this );
|
||||
this.control.addControlListener( this );
|
||||
control.addDisposeListener(event -> { buttonClick.disarm(); mouseDownActionTimer.cancel(); });
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -73,7 +74,7 @@ class ClickControl extends ControlAdapter implements ViewComponent, MouseDownAct
|
||||
|
||||
@Override
|
||||
public boolean isEnabled() {
|
||||
return true;
|
||||
return !control.isDisposed() && control.isEnabled();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
+6
-2
@@ -18,6 +18,7 @@ public class MouseDownActionTimer implements Runnable {
|
||||
public static final int FAST_DELAY = 50;
|
||||
|
||||
private final ActionScheduler scheduler;
|
||||
private boolean active;
|
||||
private final TimerAction timerAction;
|
||||
private final ButtonClick mouseClick;
|
||||
|
||||
@@ -33,15 +34,18 @@ public class MouseDownActionTimer implements Runnable {
|
||||
|
||||
public void activate() {
|
||||
if( timerAction.isEnabled() ) {
|
||||
active = true;
|
||||
scheduler.schedule( INITIAL_DELAY );
|
||||
}
|
||||
}
|
||||
|
||||
public void cancel() { active = false; scheduler.cancel(); }
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
if( mouseClick.isArmed() && timerAction.isEnabled() ) {
|
||||
if( active && mouseClick.isArmed() && timerAction.isEnabled() ) {
|
||||
timerAction.run();
|
||||
scheduler.schedule( FAST_DELAY );
|
||||
if (active && timerAction.isEnabled()) scheduler.schedule(FAST_DELAY);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user