Commit 479bfb4f authored by Gaëtan Renaudeau's avatar Gaëtan Renaudeau

wip

parent c75cf213
...@@ -4,6 +4,7 @@ import static android.opengl.GLES20.*; ...@@ -4,6 +4,7 @@ import static android.opengl.GLES20.*;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.graphics.PixelFormat; import android.graphics.PixelFormat;
import android.net.Uri;
import android.opengl.GLSurfaceView; import android.opengl.GLSurfaceView;
import android.util.DisplayMetrics; import android.util.DisplayMetrics;
import android.util.Log; import android.util.Log;
...@@ -49,11 +50,10 @@ public class GLCanvas extends GLSurfaceView implements GLSurfaceView.Renderer, R ...@@ -49,11 +50,10 @@ public class GLCanvas extends GLSurfaceView implements GLSurfaceView.Renderer, R
private boolean visibleContent; private boolean visibleContent;
private int captureNextFrameId; private int captureNextFrameId;
private GLData data; private GLData data;
private ReadableArray imagesToPreload; // TODO we need to make a List<Uri> I guess? probably Uri should be resolved in advance. not in GLImage private List<Uri> imagesToPreload;
private List<Uri> preloaded;
List<String> preloaded; // TODO List<Uri> private Map<Uri, GLImage> images = new HashMap<>();
private Map<String, GLImage> images = new HashMap<>();
private List<GLTexture> contentTextures = new ArrayList<>(); private List<GLTexture> contentTextures = new ArrayList<>();
private List<Bitmap> contentBitmaps = new ArrayList<>(); private List<Bitmap> contentBitmaps = new ArrayList<>();
...@@ -69,16 +69,13 @@ public class GLCanvas extends GLSurfaceView implements GLSurfaceView.Renderer, R ...@@ -69,16 +69,13 @@ public class GLCanvas extends GLSurfaceView implements GLSurfaceView.Renderer, R
getHolder().setFormat(PixelFormat.RGBA_8888); getHolder().setFormat(PixelFormat.RGBA_8888);
setRenderer(this); setRenderer(this);
setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY); setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
Log.i("GLCanvas", "created");
} }
@Override @Override
protected void onAttachedToWindow() { protected void onAttachedToWindow() {
Log.i("GLCanvas", "onAttachedToWindow");
super.onAttachedToWindow(); super.onAttachedToWindow();
syncContentBitmaps(); syncContentBitmaps();
requestRender(); requestRender();
preloadingDone = true; // TODO
} }
@Override @Override
...@@ -93,7 +90,6 @@ public class GLCanvas extends GLSurfaceView implements GLSurfaceView.Renderer, R ...@@ -93,7 +90,6 @@ public class GLCanvas extends GLSurfaceView implements GLSurfaceView.Renderer, R
@Override @Override
public void onDrawFrame(GL10 gl) { public void onDrawFrame(GL10 gl) {
Log.i("GLCanvas", "onDrawFrame");
runAll(mRunOnDraw); runAll(mRunOnDraw);
syncEventsThrough(); // FIXME, really need to do this ? syncEventsThrough(); // FIXME, really need to do this ?
...@@ -200,9 +196,12 @@ public class GLCanvas extends GLSurfaceView implements GLSurfaceView.Renderer, R ...@@ -200,9 +196,12 @@ public class GLCanvas extends GLSurfaceView implements GLSurfaceView.Renderer, R
} }
public void setImagesToPreload(ReadableArray imagesToPreload) { public void setImagesToPreload (ReadableArray imagesToPreloadRA) {
// FIXME setImageToPreload, working correctly?
if (preloadingDone) return; if (preloadingDone) return;
List<Uri> imagesToPreload = new ArrayList<>();
for (int i=0; i<imagesToPreloadRA.size(); i++) {
imagesToPreload.add(resolveSrc(imagesToPreloadRA.getString(i)));
}
if (imagesToPreload.size() == 0) { if (imagesToPreload.size() == 0) {
dispatchOnLoad(); dispatchOnLoad();
preloadingDone = true; preloadingDone = true;
...@@ -210,7 +209,6 @@ public class GLCanvas extends GLSurfaceView implements GLSurfaceView.Renderer, R ...@@ -210,7 +209,6 @@ public class GLCanvas extends GLSurfaceView implements GLSurfaceView.Renderer, R
else { else {
preloadingDone = false; preloadingDone = false;
} }
this.imagesToPreload = imagesToPreload; this.imagesToPreload = imagesToPreload;
} }
...@@ -234,7 +232,6 @@ public class GLCanvas extends GLSurfaceView implements GLSurfaceView.Renderer, R ...@@ -234,7 +232,6 @@ public class GLCanvas extends GLSurfaceView implements GLSurfaceView.Renderer, R
public void requestSyncData () { public void requestSyncData () {
runInGLThread(new Runnable() { runInGLThread(new Runnable() {
public void run() { public void run() {
Log.i("GLCanvas", "requestSyncData");
if (ensureCompiledShader(data)) if (ensureCompiledShader(data))
syncData(); syncData();
else else
...@@ -287,15 +284,15 @@ public class GLCanvas extends GLSurfaceView implements GLSurfaceView.Renderer, R ...@@ -287,15 +284,15 @@ public class GLCanvas extends GLSurfaceView implements GLSurfaceView.Renderer, R
private int countPreloaded () { private int countPreloaded () {
int nb = 0; int nb = 0;
for (int i=0; i<imagesToPreload.size(); i++) {/* for (Uri toload: imagesToPreload) {
if ([_preloaded containsObject:srcResource(toload)]) // TODO if (preloaded.contains(toload)) {
nb++; nb++;
*/ }
} }
return nb; return nb;
} }
private void onImageLoad (String loaded) { private void onImageLoad (Uri loaded) {
if (!preloadingDone) { if (!preloadingDone) {
preloaded.add(loaded); preloaded.add(loaded);
int count = countPreloaded(); int count = countPreloaded();
...@@ -314,17 +311,35 @@ public class GLCanvas extends GLSurfaceView implements GLSurfaceView.Renderer, R ...@@ -314,17 +311,35 @@ public class GLCanvas extends GLSurfaceView implements GLSurfaceView.Renderer, R
} }
} }
public Uri resolveSrc (String src) {
Uri uri = null;
if (src != null) {
try {
uri = Uri.parse(src);
// Verify scheme is set, so that relative uri (used by static resources) are not handled.
if (uri.getScheme() == null) {
uri = null;
}
} catch (Exception e) {
// ignore malformed uri, then attempt to extract resource ID.
}
if (uri == null) {
uri = GLImage.getResourceDrawableUri(reactContext, src);
}
}
return uri;
}
public String srcResource (ReadableMap res) { public Uri srcResource (ReadableMap res) {
String src = null; String src = null;
boolean isStatic = res.hasKey("isStatic") && res.getBoolean("isStatic"); boolean isStatic = res.hasKey("isStatic") && res.getBoolean("isStatic");
if (res.hasKey("path")) src = res.getString("path"); if (res.hasKey("path")) src = res.getString("path");
if (src==null || isStatic) src = res.getString("uri"); if (src==null || isStatic) src = res.getString("uri");
return src; return resolveSrc(src);
} }
public GLRenderData recSyncData (GLData data, HashMap<String, GLImage> images) { public GLRenderData recSyncData (GLData data, HashMap<Uri, GLImage> images) {
Map<String, GLImage> prevImages = this.images; Map<Uri, GLImage> prevImages = this.images;
GLShader shader = rnglContext.getShader(data.shader); GLShader shader = rnglContext.getShader(data.shader);
Map<String, Integer> uniformsInteger = new HashMap<>(); Map<String, Integer> uniformsInteger = new HashMap<>();
...@@ -368,6 +383,9 @@ public class GLCanvas extends GLSurfaceView implements GLSurfaceView.Renderer, R ...@@ -368,6 +383,9 @@ public class GLCanvas extends GLSurfaceView implements GLSurfaceView.Renderer, R
String t = value.getString("type"); String t = value.getString("type");
if (t.equals("content")) { if (t.equals("content")) {
int id = value.getInt("id"); int id = value.getInt("id");
if (id >= contentTextures.size()) {
resizeUniformContentTextures(id+1);
}
textures.put(uniformName, contentTextures.get(id)); textures.put(uniformName, contentTextures.get(id));
} }
else if (t.equals("fbo")) { else if (t.equals("fbo")) {
...@@ -376,8 +394,8 @@ public class GLCanvas extends GLSurfaceView implements GLSurfaceView.Renderer, R ...@@ -376,8 +394,8 @@ public class GLCanvas extends GLSurfaceView implements GLSurfaceView.Renderer, R
textures.put(uniformName, fbo.color.get(0)); textures.put(uniformName, fbo.color.get(0));
} }
else if (t.equals("uri")) { else if (t.equals("uri")) {
final String src = srcResource(value); final Uri src = srcResource(value);
if (src==null || src.equals("")) { if (src == null) {
shader.runtimeException("texture uniform '"+uniformName+"': Invalid uri format '"+value+"'"); shader.runtimeException("texture uniform '"+uniformName+"': Invalid uri format '"+value+"'");
} }
...@@ -539,16 +557,13 @@ public class GLCanvas extends GLSurfaceView implements GLSurfaceView.Renderer, R ...@@ -539,16 +557,13 @@ public class GLCanvas extends GLSurfaceView implements GLSurfaceView.Renderer, R
} }
public void syncData () { public void syncData () {
Log.i("GLCanvas", "syncData "+data);
if (data == null) return; if (data == null) return;
HashMap<String, GLImage> images = new HashMap<>(); HashMap<Uri, GLImage> images = new HashMap<>();
renderData = recSyncData(data, images); renderData = recSyncData(data, images);
this.images = images; this.images = images;
this.requestRender(); // FIXME don't do it here since syncData is called in render phase
} }
public void recRender (GLRenderData renderData) { public void recRender (GLRenderData renderData) {
Log.i("GLCanvas", "recRender "+renderData.fboId);
DisplayMetrics dm = reactContext.getResources().getDisplayMetrics(); DisplayMetrics dm = reactContext.getResources().getDisplayMetrics();
int w = Float.valueOf(renderData.width.floatValue() * dm.density).intValue(); int w = Float.valueOf(renderData.width.floatValue() * dm.density).intValue();
...@@ -576,7 +591,6 @@ public class GLCanvas extends GLSurfaceView implements GLSurfaceView.Renderer, R ...@@ -576,7 +591,6 @@ public class GLCanvas extends GLSurfaceView implements GLSurfaceView.Renderer, R
GLTexture texture = renderData.textures.get(uniformName); GLTexture texture = renderData.textures.get(uniformName);
int unit = renderData.uniformsInteger.get(uniformName); int unit = renderData.uniformsInteger.get(uniformName);
texture.bind(unit); texture.bind(unit);
Log.i("GLCanvas", uniformName+" "+unit);
} }
Map<String, Integer> uniformTypes = renderData.shader.getUniformTypes(); Map<String, Integer> uniformTypes = renderData.shader.getUniformTypes();
...@@ -601,8 +615,8 @@ public class GLCanvas extends GLSurfaceView implements GLSurfaceView.Renderer, R ...@@ -601,8 +615,8 @@ public class GLCanvas extends GLSurfaceView implements GLSurfaceView.Renderer, R
public void render () { public void render () {
if (renderData == null) return; if (renderData == null) return;
syncContentTextures();
Log.i("GLCanvas", "render"); Log.i("GLCanvas", "render");
syncContentTextures();
int[] defaultFBOArr = new int[1]; int[] defaultFBOArr = new int[1];
glGetIntegerv(GL_FRAMEBUFFER_BINDING, defaultFBOArr, 0); glGetIntegerv(GL_FRAMEBUFFER_BINDING, defaultFBOArr, 0);
......
package com.projectseptember.RNGL; package com.projectseptember.RNGL;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.util.Log;
import com.facebook.react.bridge.ReadableArray; import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.bridge.ReadableMap; import com.facebook.react.bridge.ReadableMap;
...@@ -65,7 +64,6 @@ public class GLCanvasManager extends SimpleViewManager<GLCanvas> { ...@@ -65,7 +64,6 @@ public class GLCanvasManager extends SimpleViewManager<GLCanvas> {
@Override @Override
public GLCanvas createViewInstance (ThemedReactContext context) { public GLCanvas createViewInstance (ThemedReactContext context) {
Log.i("GLCanvas", "createViewInstance...");
return new GLCanvas(context); return new GLCanvas(context);
} }
} }
package com.projectseptember.RNGL; package com.projectseptember.RNGL;
import android.util.Log;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
...@@ -21,47 +19,23 @@ public class GLFBO { ...@@ -21,47 +19,23 @@ public class GLFBO {
return texture; return texture;
} }
/*
int initRenderBuffer (int width, int height, int component, int attachment)
{
int[] handleArr = new int[1];
glGenRenderbuffers(1, handleArr, 0);
int handle = handleArr[0];
glBindRenderbuffer(GL_RENDERBUFFER, handle);
glRenderbufferStorage(GL_RENDERBUFFER, component, width, height);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, attachment, GL_RENDERBUFFER, handle);
return handle;
}
*/
/*
class FBOState { class FBOState {
private int fbo; private int fbo;
private int rbo;
private int tex;
public FBOState() { public FBOState() {
int[] fbo = new int[1], rbo = new int[1], tex = new int[1]; int[] fbo = new int[1];
glGetIntegerv(GL_FRAMEBUFFER_BINDING, fbo, 0); glGetIntegerv(GL_FRAMEBUFFER_BINDING, fbo, 0);
glGetIntegerv(GL_RENDERBUFFER_BINDING, rbo, 0);
glGetIntegerv(GL_TEXTURE_BINDING_2D, tex, 0);
this.fbo = fbo[0]; this.fbo = fbo[0];
this.rbo = rbo[0];
this.tex = tex[0];
} }
private void restore() { private void restore() {
glBindFramebuffer(GL_FRAMEBUFFER, fbo); glBindFramebuffer(GL_FRAMEBUFFER, fbo);
glBindRenderbuffer(GL_FRAMEBUFFER, rbo);
glBindTexture(GL_FRAMEBUFFER, tex);
} }
} }
*/
public GLFBO() { public GLFBO() {
Log.i("GLFBO", "new"); FBOState state = new FBOState();
//FBOState state = new FBOState();
int[] handleArr = new int[1]; int[] handleArr = new int[1];
glGenFramebuffers(1, handleArr, 0); glGenFramebuffers(1, handleArr, 0);
...@@ -74,12 +48,11 @@ public class GLFBO { ...@@ -74,12 +48,11 @@ public class GLFBO {
for(int i=0; i<numColors; ++i) { for(int i=0; i<numColors; ++i) {
color.add(initTexture(width, height, GL_COLOR_ATTACHMENT0 + i)); color.add(initTexture(width, height, GL_COLOR_ATTACHMENT0 + i));
} }
// state.restore(); state.restore();
} }
@Override @Override
protected void finalize() throws Throwable { protected void finalize() throws Throwable {
Log.i("GLFBO", "finalize");
super.finalize(); super.finalize();
int[] handleArr = new int[] { handle }; int[] handleArr = new int[] { handle };
glDeleteFramebuffers(1, handleArr, 0); glDeleteFramebuffers(1, handleArr, 0);
...@@ -105,13 +78,11 @@ public class GLFBO { ...@@ -105,13 +78,11 @@ public class GLFBO {
} }
public void bind () { public void bind () {
Log.i("GLFBO", "bind");
glBindFramebuffer(GL_FRAMEBUFFER, handle); glBindFramebuffer(GL_FRAMEBUFFER, handle);
glViewport(0, 0, width, height); glViewport(0, 0, width, height);
} }
public void setShape(int w, int h) { public void setShape(int w, int h) {
Log.i("GLFBO", "setShape "+w+" "+h);
if (w == width && h == height) return; if (w == width && h == height) return;
int[] maxFBOSize = new int[1]; int[] maxFBOSize = new int[1];
glGetIntegerv(GL_MAX_RENDERBUFFER_SIZE, maxFBOSize, 0); glGetIntegerv(GL_MAX_RENDERBUFFER_SIZE, maxFBOSize, 0);
...@@ -121,7 +92,7 @@ public class GLFBO { ...@@ -121,7 +92,7 @@ public class GLFBO {
width = w; width = w;
height = h; height = h;
//FBOState state = new FBOState(); FBOState state = new FBOState();
for (GLTexture clr: color) { for (GLTexture clr: color) {
clr.setShape(w, h); clr.setShape(w, h);
...@@ -130,6 +101,6 @@ public class GLFBO { ...@@ -130,6 +101,6 @@ public class GLFBO {
glBindFramebuffer(GL_FRAMEBUFFER, handle); glBindFramebuffer(GL_FRAMEBUFFER, handle);
checkStatus(); checkStatus();
//state.restore(); state.restore();
} }
} }
...@@ -24,11 +24,9 @@ https://github.com/CyberAgent/android-gpuimage/blob/master/library/src/jp/co/cyb ...@@ -24,11 +24,9 @@ https://github.com/CyberAgent/android-gpuimage/blob/master/library/src/jp/co/cyb
*/ */
public class GLImage { public class GLImage {
private final Context context; private final Context context;
private String src; private Uri src;
private GLTexture texture; private GLTexture texture;
private Uri uri;
private boolean isLocalImage;
private boolean isDirty; private boolean isDirty;
private AsyncTask<Void, Void, Bitmap> task; private AsyncTask<Void, Void, Bitmap> task;
private Runnable onload; private Runnable onload;
...@@ -41,31 +39,13 @@ public class GLImage { ...@@ -41,31 +39,13 @@ public class GLImage {
this.texture = new GLTexture(); this.texture = new GLTexture();
} }
public void setSrc(String src) { public void setSrc(Uri src) {
if (this.src == src) return; if (this.src == src) return;
this.src = src; this.src = src;
reloadImage(); reloadImage();
} }
private void reloadImage () { private void reloadImage () {
uri = null;
if (src != null) {
try {
uri = Uri.parse(src);
// Verify scheme is set, so that relative uri (used by static resources) are not handled.
if (uri.getScheme() == null) {
uri = null;
}
} catch (Exception e) {
// ignore malformed uri, then attempt to extract resource ID.
}
if (uri == null) {
uri = getResourceDrawableUri(context, src);
isLocalImage = true;
} else {
isLocalImage = false;
}
}
isDirty = true; isDirty = true;
} }
...@@ -81,13 +61,13 @@ public class GLImage { ...@@ -81,13 +61,13 @@ public class GLImage {
public GLTexture getTexture() { public GLTexture getTexture() {
if (isDirty) { if (isDirty) {
if (task != null) task.cancel(true); if (task != null) task.cancel(true);
task = new LoadImageUriTask(this, uri).execute(); task = new LoadImageUriTask(this, src).execute();
isDirty = false; isDirty = false;
} }
return texture; return texture;
} }
private static @Nullable Uri getResourceDrawableUri(Context context, @Nullable String name) { public static @Nullable Uri getResourceDrawableUri(Context context, @Nullable String name) {
if (name == null || name.isEmpty()) { if (name == null || name.isEmpty()) {
return null; return null;
} }
......
package com.projectseptember.RNGL; package com.projectseptember.RNGL;
import android.opengl.GLES20;
import com.facebook.react.bridge.ReactApplicationContext; import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule; import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod; import com.facebook.react.bridge.ReactMethod;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment