<commit_before>package com.mycardboarddreams.liquidsurface;

import android.content.Context;
import android.graphics.SurfaceTexture;
import android.opengl.GLSurfaceView;
import android.opengl.GLUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.view.TextureView;

import javax.microedition.khronos.egl.EGL10;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.egl.EGLContext;
import javax.microedition.khronos.egl.EGLDisplay;
import javax.microedition.khronos.egl.EGLSurface;
import javax.microedition.khronos.opengles.GL10;
import javax.microedition.khronos.opengles.GL11;

/**
 * Created on 15-10-18.
 */
public class GLTextureView extends TextureView implements TextureView.SurfaceTextureListener {

    private GLSurfaceView.Renderer mRenderer;

    private static final int TARGET_FRAME_RATE = 55;

    private static final int EGL_OPENGL_ES2_BIT = 4;
    private static final int EGL_CONTEXT_CLIENT_VERSION = 0x3098;
    private static final String TAG = "RenderThread";
    private SurfaceTexture mSurface;
    private EGLDisplay mEglDisplay;
    private EGLSurface mEglSurface;
    private EGLContext mEglContext;
    private EGL10 mEgl;
    private EGLConfig eglConfig;
    private GL10 mGl;

    private int targetFrameDurationMillis;

    private int surfaceHeight;
    private int surfaceWidth;

    public boolean isRunning = false;
    private boolean paused = true;

    private RenderThread thread;

    private int targetFps;

    public GLTextureView(Context context) {
        super(context);
        initialize(context);
    }

    public GLTextureView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initialize(context);
    }

    public GLTextureView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initialize(context);
    }

    public void setRenderer(GLSurfaceView.Renderer renderer){
        mRenderer = renderer;
        initializeRenderer();
    }


    private void initialize(Context context) {
        targetFps = TARGET_FRAME_RATE;

        setSurfaceTextureListener(this);
    }

    @Override
    public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
        startThread(surface, width, height, targetFps);
    }

    public void startThread(SurfaceTexture surface, int width, int height, float targetFramesPerSecond){
        Log.d(TAG, "Starting GLTextureView thread");
        thread = new RenderThread();
        mSurface = surface;
        setDimensions(width, height);
        targetFrameDurationMillis = (int) ((1f/targetFramesPerSecond)*1000);

        thread.start();

    }

    @Override
    public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
        setDimensions(width, height);
        if(mRenderer != null)
            mRenderer.onSurfaceChanged(mGl, width, height);
    }

    public synchronized void setPaused(boolean isPaused){
        Log.d(TAG, String.format("Setting GLTextureView paused to %s", isPaused));
        paused = isPaused;
    }

    public synchronized boolean isPaused(){
        return paused;
    }

    @Override
    public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
        stopThread();
        return false;
    }

    public void stopThread(){
        if(thread != null){
            Log.d(TAG, "Stopping and joining GLTextureView");
            isRunning = false;
            try {
                thread.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            thread = null;
        }

    }

    private boolean shouldSleep(){
        return isPaused() || mRenderer == null;
    }

    private class RenderThread extends Thread {
        @Override
        public void run() {
            isRunning = true;

            initGL();
            checkGlError();

            initializeRenderer();

            long lastFrameTime = System.currentTimeMillis();

            while (isRunning) {
                if (!shouldSleep()) {

                    lastFrameTime = System.currentTimeMillis();

                    drawSingleFrame();
                }

                try {
                    if (shouldSleep())
                        Thread.sleep(100);
                    else {
                        long thisFrameTime = System.currentTimeMillis();
                        long timDiff = thisFrameTime - lastFrameTime;
                        lastFrameTime = thisFrameTime;
                        Thread.sleep(Math.max(10l, targetFrameDurationMillis - timDiff));
                    }
                } catch (InterruptedException e) {
// Ignore
                }
            }
        }

    }

    private void initializeRenderer() {
        if(mRenderer != null && isRunning) {
            mRenderer.onSurfaceCreated(mGl, eglConfig);
            mRenderer.onSurfaceChanged(mGl, surfaceWidth, surfaceHeight);
        }
    }

    private void drawSingleFrame() {
        checkCurrent();

        if(mRenderer != null)
            mRenderer.onDrawFrame(mGl);

        checkGlError();
        if (!mEgl.eglSwapBuffers(mEglDisplay, mEglSurface)) {
            Log.e(TAG, "cannot swap buffers!");
        }
    }

    public void setDimensions(int width, int height){
        surfaceWidth = width;
        surfaceHeight = height;
    }

    private void checkCurrent() {<commit_msg>Allow updating renderer mid stream.<commit_after>package com.mycardboarddreams.liquidsurface;

import android.content.Context;
import android.graphics.SurfaceTexture;
import android.opengl.GLSurfaceView;
import android.opengl.GLUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.view.TextureView;

import javax.microedition.khronos.egl.EGL10;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.egl.EGLContext;
import javax.microedition.khronos.egl.EGLDisplay;
import javax.microedition.khronos.egl.EGLSurface;
import javax.microedition.khronos.opengles.GL10;
import javax.microedition.khronos.opengles.GL11;

/**
 * Created on 15-10-18.
 */
public class GLTextureView extends TextureView implements TextureView.SurfaceTextureListener {

    private GLSurfaceView.Renderer mRenderer;

    private static final int TARGET_FRAME_RATE = 55;

    private static final int EGL_OPENGL_ES2_BIT = 4;
    private static final int EGL_CONTEXT_CLIENT_VERSION = 0x3098;
    private static final String TAG = "RenderThread";
    private SurfaceTexture mSurface;
    private EGLDisplay mEglDisplay;
    private EGLSurface mEglSurface;
    private EGLContext mEglContext;
    private EGL10 mEgl;
    private EGLConfig eglConfig;
    private GL10 mGl;

    private int targetFrameDurationMillis;

    private int surfaceHeight;
    private int surfaceWidth;

    public boolean isRunning = false;
    private boolean paused = true;
    private boolean rendererChanged = false;

    private RenderThread thread;

    private int targetFps;

    public GLTextureView(Context context) {
        super(context);
        initialize(context);
    }

    public GLTextureView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initialize(context);
    }

    public GLTextureView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initialize(context);
    }

    public synchronized void setRenderer(GLSurfaceView.Renderer renderer){
        mRenderer = renderer;
        rendererChanged = true;
    }


    private void initialize(Context context) {
        targetFps = TARGET_FRAME_RATE;

        setSurfaceTextureListener(this);
    }

    @Override
    public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
        startThread(surface, width, height, targetFps);
    }

    public void startThread(SurfaceTexture surface, int width, int height, float targetFramesPerSecond){
        Log.d(TAG, "Starting GLTextureView thread");
        thread = new RenderThread();
        mSurface = surface;
        setDimensions(width, height);
        targetFrameDurationMillis = (int) ((1f/targetFramesPerSecond)*1000);

        thread.start();

    }

    @Override
    public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
        setDimensions(width, height);
        if(mRenderer != null)
            mRenderer.onSurfaceChanged(mGl, width, height);
    }

    public synchronized void setPaused(boolean isPaused){
        Log.d(TAG, String.format("Setting GLTextureView paused to %s", isPaused));
        paused = isPaused;
    }

    public synchronized boolean isPaused(){
        return paused;
    }

    @Override
    public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
        stopThread();
        return false;
    }

    public void stopThread(){
        if(thread != null){
            Log.d(TAG, "Stopping and joining GLTextureView");
            isRunning = false;
            try {
                thread.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            thread = null;
        }

    }

    private boolean shouldSleep(){
        return isPaused() || mRenderer == null;
    }

    private class RenderThread extends Thread {
        @Override
        public void run() {
            isRunning = true;

            initGL();
            checkGlError();

            long lastFrameTime = System.currentTimeMillis();

            while (isRunning) {
                while (mRenderer == null){
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e){
                        // Ignore
                    }

                }

                if(rendererChanged){
                    initializeRenderer(mRenderer);
                    rendererChanged = false;
                }

                if (!shouldSleep()) {

                    lastFrameTime = System.currentTimeMillis();

                    drawSingleFrame();
                }

                try {
                    if (shouldSleep())
                        Thread.sleep(100);
                    else {
                        long thisFrameTime = System.currentTimeMillis();
                        long timDiff = thisFrameTime - lastFrameTime;
                        lastFrameTime = thisFrameTime;
                        Thread.sleep(Math.max(10l, targetFrameDurationMillis - timDiff));
                    }
                } catch (InterruptedException e) {
// Ignore
                }
            }
        }

    }

    private synchronized void initializeRenderer(GLSurfaceView.Renderer renderer) {
        if(renderer != null && isRunning) {
            renderer.onSurfaceCreated(mGl, eglConfig);
            renderer.onSurfaceChanged(mGl, surfaceWidth, surfaceHeight);
        }
    }

    private synchronized void drawSingleFrame() {
        checkCurrent();

        if(mRenderer != null)
            mRenderer.onDrawFrame(mGl);

        checkGlError();
        if (!mEgl.eglSwapBuffers(mEglDisplay, mEglSurface)) {
            Log.e(TAG, "cannot swap buffers!");
        }
    }

    public void setDimensions(int width, int height){
        surfaceWidth = width;
        surfaceHeight = height;
    }

    private void checkCurrent() {
{'fraction_non_alphanumeric': 0.08191275461141709, 'fraction_numerical': 0.006381676719993006, 'mean_word_length': 2.249076966770804, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '6892382', 'n_tokens_mistral': 3147, 'n_tokens_neox': 2958, 'n_words': 711}