RNGLContext.java 1.78 KB
Newer Older
1 2 3 4 5 6 7
package com.projectseptember.RNGL;

import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.ReadableMap;

Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
8 9 10 11 12
import java.util.HashMap;
import java.util.Map;

import javax.microedition.khronos.egl.EGLContext;

13 14
public class RNGLContext extends ReactContextBaseJavaModule {

Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
    // Share GL Context ?
    // http://developer.android.com/training/graphics/opengl/environment.html
    // http://stackoverflow.com/questions/8845491/sharing-the-egl2-0-context-between-2-glsurfaceviews-caused-egl-bad-access-on-and
    // http://stackoverflow.com/questions/5675355/sharing-the-gles20-context-and-textures-between-different-glsurfaceviews

    private static String STATIC_VERT =
    "attribute vec2 position;"+
    "varying vec2 uv;"+
    "void main() {"+
        "gl_Position = vec4(position,0.0,1.0);"+
        "uv = vec2(0.5, 0.5) * (position+vec2(1.0, 1.0));"+
    "}";

    private Map<Integer, GLShader> shaders = new HashMap<>();
    private Map<Integer, GLFBO> fbos = new HashMap<>();

31 32 33 34 35 36 37 38 39
    public RNGLContext (ReactApplicationContext reactContext) {
        super(reactContext);
    }

    @Override
    public String getName() {
        return "RNGLContext";
    }

Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
40 41 42 43 44 45 46 47 48 49 50
    public GLShader getShader (Integer id) {
        return shaders.get(id);
    }

    public GLFBO getFBO (Integer id) {
        if (!fbos.containsKey(id)) {
            fbos.put(id, new GLFBO());
        }
        return fbos.get(id);
    }

51
    @ReactMethod
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
52 53 54 55
    public void addShader (final Integer id, final ReadableMap config) {
        final String frag = config.getString("frag");
        final String name = config.getString("name");
        shaders.put(id, new GLShader(name, STATIC_VERT, frag));
56 57
    }
}