<commit_before>package us.guihouse.projector.projection;

import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.awt.image.RescaleOp;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import javax.swing.*;
import us.guihouse.projector.models.WindowConfig;
import us.guihouse.projector.other.GraphicsFinder;
import us.guihouse.projector.services.SettingsService;
import us.guihouse.projector.utils.BlendGenerator;
import us.guihouse.projector.utils.WindowConfigsLoader;

public class WindowManager implements Runnable, CanvasDelegate, WindowConfigsLoader.WindowConfigsObserver {

    private WindowConfigsLoader configLoader;

    private List<WindowConfig> windowConfigs = Collections.emptyList();

    private final PreviewPanel preview;

    private final ProjectionCanvas projectionCanvas;

    private GraphicsDevice defaultDevice;

    private BufferedImage targetRender;
    private BufferedImage bLevelFixed;

    private List<ProjectionWindow> windows = Collections.emptyList();

    private HashMap<Integer, BufferedImage> blendAssets = new HashMap<>();
    private HashMap<String, AffineTransform> transformAssets = new HashMap<>();
    private HashMap<String, RescaleOp> blackLevelRescaleOps = new HashMap<>();

    private Thread drawThread;

    private boolean running = false;
    private boolean fullScreen = false;
    private boolean starting = false;

    private final SettingsService settingsService;

    private final Cursor blankCursor;

    public WindowManager(SettingsService settingsService) {
        this.settingsService = settingsService;

        projectionCanvas = new ProjectionCanvas(this);

        // Transparent 16 x 16 pixel cursor image.
        BufferedImage cursorImg = new BufferedImage(16, 16, BufferedImage.TYPE_INT_ARGB);

        // Create a new blank cursor.
        blankCursor = Toolkit.getDefaultToolkit().createCustomCursor(cursorImg, new Point(0, 0), "blank cursor");

        preview = new PreviewPanel(this);

        this.configLoader = new WindowConfigsLoader(this);
    }

    public ProjectionManager getManager() {
        return projectionCanvas;
    }

    public void setDefaultDevice(GraphicsDevice defaultDevice) {
        this.defaultDevice = defaultDevice;
    }

    private void stopEngine() {
        running = false;

        configLoader.stop();

        if (drawThread != null) {
            drawThread.interrupt();
            drawThread = null;
        }

        windows.forEach(ProjectionWindow::shutdown);

        preview.setProjectionCanvas(null);
    }

    private void startEngine() {
        if (starting) {
            return;
        }

        configLoader.start();

        generateAssets();

        starting = true;

        targetRender = getDefaultDevice().getDefaultConfiguration().createCompatibleImage(getWidth(), getHeight());
        bLevelFixed = getDefaultDevice().getDefaultConfiguration().createCompatibleImage(getWidth(), getHeight());

        windowConfigs.forEach(wc -> {
            windows
                    .stream()
                    .filter(w -> w.getCurrentDevice().getDevice().getIDstring().equals(wc.getDisplayId()))<commit_msg>Aceleration<commit_after>package us.guihouse.projector.projection;

import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.awt.image.RescaleOp;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import javax.swing.*;
import us.guihouse.projector.models.WindowConfig;
import us.guihouse.projector.other.GraphicsFinder;
import us.guihouse.projector.services.SettingsService;
import us.guihouse.projector.utils.BlendGenerator;
import us.guihouse.projector.utils.WindowConfigsLoader;

public class WindowManager implements Runnable, CanvasDelegate, WindowConfigsLoader.WindowConfigsObserver {

    private WindowConfigsLoader configLoader;

    private List<WindowConfig> windowConfigs = Collections.emptyList();

    private final PreviewPanel preview;

    private final ProjectionCanvas projectionCanvas;

    private GraphicsDevice defaultDevice;

    private BufferedImage targetRender;
    private BufferedImage bLevelFixed;

    private List<ProjectionWindow> windows = Collections.emptyList();

    private HashMap<Integer, BufferedImage> blendAssets = new HashMap<>();
    private HashMap<String, AffineTransform> transformAssets = new HashMap<>();
    private HashMap<String, RescaleOp> blackLevelRescaleOps = new HashMap<>();

    private Thread drawThread;

    private boolean running = false;
    private boolean fullScreen = false;
    private boolean starting = false;

    private final SettingsService settingsService;

    private final Cursor blankCursor;

    public WindowManager(SettingsService settingsService) {
        this.settingsService = settingsService;

        projectionCanvas = new ProjectionCanvas(this);

        // Transparent 16 x 16 pixel cursor image.
        BufferedImage cursorImg = new BufferedImage(16, 16, BufferedImage.TYPE_INT_ARGB);

        // Create a new blank cursor.
        blankCursor = Toolkit.getDefaultToolkit().createCustomCursor(cursorImg, new Point(0, 0), "blank cursor");

        preview = new PreviewPanel(this);

        this.configLoader = new WindowConfigsLoader(this);
    }

    public ProjectionManager getManager() {
        return projectionCanvas;
    }

    public void setDefaultDevice(GraphicsDevice defaultDevice) {
        this.defaultDevice = defaultDevice;
    }

    private void stopEngine() {
        running = false;

        configLoader.stop();

        if (drawThread != null) {
            drawThread.interrupt();
            drawThread = null;
        }

        windows.forEach(ProjectionWindow::shutdown);

        preview.setProjectionCanvas(null);
    }

    private void startEngine() {
        if (starting) {
            return;
        }

        configLoader.start();

        generateAssets();

        starting = true;

        targetRender = getDefaultDevice().getDefaultConfiguration().createCompatibleImage(getWidth(), getHeight());
        targetRender.setAccelerationPriority(1.0f);
        bLevelFixed = getDefaultDevice().getDefaultConfiguration().createCompatibleImage(getWidth(), getHeight());
        bLevelFixed.setAccelerationPriority(1.0f);

        windowConfigs.forEach(wc -> {
            windows
                    .stream()
                    .filter(w -> w.getCurrentDevice().getDevice().getIDstring().equals(wc.getDisplayId()))
{'fraction_non_alphanumeric': 0.09058436919559937, 'fraction_numerical': 0.0034290612944706386, 'mean_word_length': 3.5901639344262297, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1463178', 'n_tokens_mistral': 1837, 'n_tokens_neox': 1753, 'n_words': 403}