InspectionFrame.java

package libsidutils.debug;

import libsidplay.components.mos656x.IPALEmulation;
import libsidplay.components.mos656x.IPALEmulation.FrameBuffer;
import libsidplay.components.mos656x.VIC;
import libsidplay.components.mos656x.VICInspection;

import java.nio.ByteBuffer;
import java.util.Base64;

/**
 * One detached copy of pixels already committed by the renderer. Never advances the machine or records history.
 */
public record InspectionFrame(int width, int height, int firstRaster, int beamX, int beamY,
                              long startTime, String status, int[] pixels) {
    public static InspectionFrame capture(VIC vic, long time) {
        int width = vic.getBorderWidth(), height = vic.getBorderHeight();
        int first = VICInspection.firstDisplayedRaster(vic);
        int lines = vic.getCyclesPerFrame() / vic.getCyclesPerLine();
        int row = (VICInspection.raster(vic) - first + lines) % lines;
        int cycle = VICInspection.cycle(vic), lineCycles = vic.getCyclesPerLine();
        int x = cycle >= 23 ? (cycle - 22) * 8 : cycle < 10 ? (lineCycles - 22 + cycle) * 8 : -1;
        if (x > width) x = -1;
        long start = time - (long) row * lineCycles - (cycle + lineCycles - 10) % lineCycles;
        IPALEmulation renderer = vic.getPalEmulation();
        FrameBuffer buffer = renderer == null ? null : renderer.getFrameBuffer();
        String status = renderer == null ? "DISABLED" : buffer == null ? "UNSUPPORTED"
                : !buffer.current() ? "SKIPPED" : "EMPTY";
        int count = buffer == null || !buffer.current() ? 0 : Math.min(width * height, buffer.renderedPixels());
        int[] pixels = new int[Math.max(0, count)];
        if (count > 0) {
            ByteBuffer bytes = buffer.pixels();
            for (int i = 0; i < count; i++) {
                int offset = buffer.offset() + i * 4;
                int a = bytes.get(offset) & 255, g = bytes.get(offset + 1) & 255, b = bytes.get(offset + 2) & 255;
                pixels[i] = 0xff000000 | (buffer.rgba() ? a << 16 | g << 8 | b : b << 16 | g << 8 | a);
            }
            status = count == width * height ? "COMPLETE" : "PARTIAL";
        }
        return new InspectionFrame(width, height, first, x, row < height ? row : -1, start, status, pixels);
    }

    public String toJson() {
        // RGB24 keeps a full border-inclusive capture below half a megabyte; missing pixels are never exported.
        byte[] rgb = new byte[pixels.length * 3];
        for (int i = 0; i < pixels.length; i++) {
            rgb[i * 3] = (byte) (pixels[i] >>> 16);
            rgb[i * 3 + 1] = (byte) (pixels[i] >>> 8);
            rgb[i * 3 + 2] = (byte) pixels[i];
        }
        return "{\"width\":" + width + ",\"height\":" + height + ",\"firstRaster\":" + firstRaster
                + ",\"beamX\":" + beamX + ",\"beamY\":" + beamY + ",\"startTime\":" + startTime
                + ",\"status\":\"" + status + "\",\"renderedPixels\":" + pixels.length
                + ",\"encoding\":\"RGB24_BASE64\",\"pixels\":\"" + Base64.getEncoder().encodeToString(rgb) + "\"}";
    }
}