package com.fgodt.mixplayer;



import android.content.Context;
import android.opengl.GLES20;
import android.opengl.GLSurfaceView;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

public class VideoRender extends GLSurfaceView implements GLSurfaceView.Renderer {

    //lib mixplayer hander
    private long  hander = 0;
    private int mScreenWidth, mScreenHeight;
    private native void step(long handler);
    private native void init(long handler,int w, int h);
    private native void nativeChangeSize(long handler, int w, int h);

    final Object textLocker = new Object();

    boolean ispause = false;
    float volume = 1.0f;


    private boolean createDone=false;

    public VideoRender(Context context){
        super(context);
        this.setEGLContextClientVersion(2);
        this.setRenderer(this);
    }
    public int getScreenWidth(){
        return mScreenWidth;
    }
    public int getScreenHeight(){
        return mScreenHeight;
    }


    boolean inited = false;
    boolean hasrender = false;
    void nativeInit(int w, int h){
        hasrender = true;
    }
    void nativeStep(){
        if(hasrender && !inited){
            init(hander,getScreenWidth(),getScreenHeight());
            inited = true;
        }
        if(hander>0 && inited){
            step(hander);
        }
    }

    void close(){
        hander = 0;
        inited = false;
        hasrender = false;
    }


    public void setMixHander(long hander){
        this.hander = hander;
    }

    @Override
    public void onSurfaceCreated(GL10 gl, EGLConfig config) {
        return;
    }

    @Override
    public void onSurfaceChanged(GL10 gl, int width, int height) {
        mScreenWidth =width;
        mScreenHeight = height;
        if(this.hander>0){
            nativeChangeSize(this.hander,width,height);
        }
        return;
    }


    @Override
    public void onDrawFrame(GL10 gl) {
        if(hander == 0){
            GLES20.glClearColor(0,0,0,1);
            GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
        }
        nativeStep();
        return;
    }
}
