<commit_before>/*******************************************************************************
 * This file is part of TERMINAL RECALL
 * Copyright (c) 2016 <NAME>
 * Part of the jTRFP.org project
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v3.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/gpl.html
 * 
 * Contributors:
 *     chuck - initial API and implementation
 ******************************************************************************/

package org.jtrfp.trcl.gpu;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.concurrent.Callable;

import com.jogamp.opengl.GL;
import com.jogamp.opengl.GLAutoDrawable;
import com.jogamp.opengl.GLEventListener;
import com.jogamp.opengl.GLRunnable;
import com.jogamp.opengl.awt.GLCanvas;

import org.jtrfp.trcl.core.GLFutureTask;
import org.jtrfp.trcl.core.TRFutureTask;
import org.jtrfp.trcl.gui.GLExecutable;

import com.ochafik.util.listenable.Pair;

public abstract class CanvasBoundGLExecutor<GL_TYPE extends GL> implements GLExecutor<GL_TYPE> {
    private CanvasProvider canvasProvider;
    private GLEventListener glEventListener;
    private final PriorityQueue<Pair<Double,GLExecutable<?,GL_TYPE>>> repeatingTasks = new PriorityQueue<Pair<Double,GLExecutable<?,GL_TYPE>>>(64, new PairKeyComparator());
    private final ArrayList<GLExecutable<?,GL_TYPE>> executeOnDispose = new ArrayList<GLExecutable<?,GL_TYPE>>();
    private final ArrayList<GLExecutable<?,GL_TYPE>> executeOnResize = new ArrayList<GLExecutable<?,GL_TYPE>>();
    private final ArrayList<GLExecutable<?,GL_TYPE>> executeBuffer = new ArrayList<GLExecutable<?,GL_TYPE>>();
    
    public abstract Class<? extends GL> getGLClass();
    
    @Override
    public <T> TRFutureTask<T> submitToGL(Callable<T> c) {
	final GLFutureTask<T> task  = new GLFutureTask<T>(getCanvas(), c);
	task.enqueue();
	return task;
    }//end submitToGL()

    @Override
    public <T> TRFutureTask<T> submitToGL(
	    GLExecutable<T,GL_TYPE> executable) {
	final GLExecutableWrapper<T> wrapper = new GLExecutableWrapper<T>(executable, getCanvas());
	final GLFutureTask<T> result = new GLFutureTask<T>(getCanvas(),wrapper);//TODO: More direct version
	return result;
    }

    @Override
    public void executeOnEachRefresh(GLExecutable<Void, GL_TYPE> executable,
	    double orderPriority) {
	synchronized(repeatingTasks){
	    repeatingTasks.add(new Pair<Double,GLExecutable<?,GL_TYPE>>(orderPriority,executable));
	}
    }

    protected CanvasProvider getCanvasProvider() {
        return canvasProvider;
    }

    protected void setCanvasProvider(CanvasProvider canvasProvider) {
	final GLCanvas              oldCanvas = getCanvas();
	final GLEventListener glEventListener = getGlEventListener();<commit_msg>⟴Improve GL task enqueue result check.<commit_after>/*******************************************************************************
 * This file is part of TERMINAL RECALL
 * Copyright (c) 2016 <NAME>
 * Part of the jTRFP.org project
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v3.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/gpl.html
 * 
 * Contributors:
 *     chuck - initial API and implementation
 ******************************************************************************/

package org.jtrfp.trcl.gpu;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.concurrent.Callable;

import com.jogamp.opengl.GL;
import com.jogamp.opengl.GLAutoDrawable;
import com.jogamp.opengl.GLEventListener;
import com.jogamp.opengl.GLRunnable;
import com.jogamp.opengl.awt.GLCanvas;

import org.jtrfp.trcl.core.GLFutureTask;
import org.jtrfp.trcl.core.TRFutureTask;
import org.jtrfp.trcl.gui.GLExecutable;

import com.ochafik.util.listenable.Pair;

public abstract class CanvasBoundGLExecutor<GL_TYPE extends GL> implements GLExecutor<GL_TYPE> {
    private CanvasProvider canvasProvider;
    private GLEventListener glEventListener;
    private final PriorityQueue<Pair<Double,GLExecutable<?,GL_TYPE>>> repeatingTasks = new PriorityQueue<Pair<Double,GLExecutable<?,GL_TYPE>>>(64, new PairKeyComparator());
    private final ArrayList<GLExecutable<?,GL_TYPE>> executeOnDispose = new ArrayList<GLExecutable<?,GL_TYPE>>();
    private final ArrayList<GLExecutable<?,GL_TYPE>> executeOnResize = new ArrayList<GLExecutable<?,GL_TYPE>>();
    private final ArrayList<GLExecutable<?,GL_TYPE>> executeBuffer = new ArrayList<GLExecutable<?,GL_TYPE>>();
    
    public abstract Class<? extends GL> getGLClass();
    
    @Override
    public <T> TRFutureTask<T> submitToGL(Callable<T> c) {
	final GLFutureTask<T> task  = new GLFutureTask<T>(getCanvas(), c);
	if(!task.enqueue())
	    throw new RuntimeException("GLFutureTask.enqueue() returned false.");
	return task;
    }//end submitToGL()

    @Override
    public <T> TRFutureTask<T> submitToGL(
	    GLExecutable<T,GL_TYPE> executable) {
	final GLExecutableWrapper<T> wrapper = new GLExecutableWrapper<T>(executable, getCanvas());
	final GLFutureTask<T> result = new GLFutureTask<T>(getCanvas(),wrapper);//TODO: More direct version
	return result;
    }

    @Override
    public void executeOnEachRefresh(GLExecutable<Void, GL_TYPE> executable,
	    double orderPriority) {
	synchronized(repeatingTasks){
	    repeatingTasks.add(new Pair<Double,GLExecutable<?,GL_TYPE>>(orderPriority,executable));
	}
    }

    protected CanvasProvider getCanvasProvider() {
        return canvasProvider;
    }

    protected void setCanvasProvider(CanvasProvider canvasProvider) {
	final GLCanvas              oldCanvas = getCanvas();
	final GLEventListener glEventListener = getGlEventListener();
{'fraction_non_alphanumeric': 0.1527683997299122, 'fraction_numerical': 0.0027008777852802163, 'mean_word_length': 5.489594742606791, 'pattern_counts': {'":': 0, '<': 77, '<?xml version=': 0, '>': 77, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '3748253', 'n_tokens_mistral': 1764, 'n_tokens_neox': 1715, 'n_words': 399}