Skip to content

Commit b3955f4

Browse files
committed
first round of abstraction
Signed-off-by: Umair Khan <[email protected]>
1 parent 2a589de commit b3955f4

File tree

3 files changed

+206
-168
lines changed

3 files changed

+206
-168
lines changed

src/in/omerjerk/processing/video/android/Capture.java

Lines changed: 6 additions & 167 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
11
package in.omerjerk.processing.video.android;
22

3-
import in.omerjerk.processing.video.android.helpers.FullFrameRect;
4-
import in.omerjerk.processing.video.android.helpers.GlUtil;
5-
import in.omerjerk.processing.video.android.helpers.Texture2dProgram;
6-
73
import java.io.IOException;
84
import java.nio.IntBuffer;
95
import java.util.ArrayList;
106
import java.util.List;
117

12-
import android.app.Activity;
138
import android.graphics.SurfaceTexture;
149
import android.hardware.Camera;
1510
import android.hardware.Camera.Size;
@@ -18,24 +13,11 @@
1813
import android.os.Handler;
1914
import android.os.Message;
2015
import android.util.Log;
21-
import processing.core.PConstants;
2216
import processing.core.PApplet;
23-
import processing.opengl.PGL;
2417
import processing.opengl.PGraphicsOpenGL;
25-
import processing.opengl.Texture;
2618

2719
@SuppressWarnings("deprecation")
28-
public class Capture extends VideoBase implements CameraHandlerCallback,
29-
SurfaceTexture.OnFrameAvailableListener {
30-
31-
private static final boolean DEBUG = true;
32-
33-
private Activity activity;
34-
35-
public static void log(String log) {
36-
if (DEBUG)
37-
System.out.println(log);
38-
}
20+
public class Capture extends VideoBase implements CameraHandlerCallback {
3921

4022
private Camera mCamera;
4123

@@ -46,33 +28,14 @@ public static void log(String log) {
4628

4729
private int selectedCamera = -1;
4830

49-
private GLSurfaceView glView;
50-
private SurfaceTexture mSurfaceTexture;
51-
private FullFrameRect mFullScreen;
52-
private int mTextureId;
53-
private final float[] mSTMatrix = new float[16];
54-
55-
// private Texture customTexture;
56-
private PGraphicsOpenGL pg;
57-
58-
private PGraphicsOpenGL destpg;
59-
6031
private CameraHandler mCameraHandler;
61-
62-
IntBuffer frameBuffers = IntBuffer.allocate(1);
63-
IntBuffer renderBuffers = IntBuffer.allocate(1);
64-
IntBuffer customTexture = IntBuffer.allocate(1);
65-
66-
private IntBuffer pixelBuffer;
67-
68-
private boolean isAvailable = false;
6932

7033
public Capture(PApplet parent) {
7134
this(parent, -1, -1);
7235
}
7336

7437
public Capture(final PApplet parent, int width, int height) {
75-
super();
38+
super(parent);
7639
this.parent = parent;
7740
if (width == -1 || height == -1) {
7841
//TODO: Temp hack. Needs to be handled intelligently.
@@ -81,8 +44,6 @@ public Capture(final PApplet parent, int width, int height) {
8144
}
8245
init(width, height, ARGB);
8346

84-
parent.registerMethod("pause", this);
85-
parent.registerMethod("resume", this);
8647
glView = (GLSurfaceView) parent.getSurfaceView();
8748
pg = (PGraphicsOpenGL)parent.g;
8849
// customTexture = new Texture(pg, width, height);
@@ -122,10 +83,6 @@ public void start() {
12283
CameraHandler.MSG_START_PREVIEW));
12384
}
12485

125-
public boolean available() {
126-
return isAvailable;
127-
}
128-
12986
@Override
13087
public void loadPixels() {
13188
super.loadPixels();
@@ -142,7 +99,8 @@ public void loadPixels() {
14299
pixelBuffer.get(Capture.this.pixels);
143100
}
144101

145-
public void pause() {
102+
@Override
103+
public void onPause() {
146104
log("pause called");
147105
isAvailable = false;
148106
if (mCamera != null) {
@@ -164,7 +122,8 @@ public void run() {
164122
});
165123
}
166124

167-
public void resume() {
125+
@Override
126+
public void onResume() {
168127
log("resume called");
169128
glView.queueEvent(new Runnable() {
170129
@Override
@@ -320,124 +279,4 @@ public void startPreview() {
320279
e.printStackTrace();
321280
}
322281
}
323-
324-
@Override
325-
public void onFrameAvailable(final SurfaceTexture surfaceTexture) {
326-
glView.queueEvent(new Runnable() {
327-
@Override
328-
public void run() {
329-
log("onFrameAvailable");
330-
isAvailable = true;
331-
surfaceTexture.updateTexImage();
332-
333-
GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, frameBuffers.get(0));
334-
GLES20.glViewport(0, 0, width, height);
335-
surfaceTexture.getTransformMatrix(mSTMatrix);
336-
mFullScreen.drawFrame(mTextureId, mSTMatrix);
337-
338-
getImage(false);
339-
340-
/*
341-
pixelBuffer.position(0);
342-
GLES20.glReadPixels(0, 0, width, height, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, pixelBuffer);
343-
pixelBuffer.position(0);
344-
pixelBuffer.get(Capture.this.pixels);
345-
updatePixels(); */
346-
347-
//Fall back to default frame buffer. Not sure if needed.
348-
GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, 0);
349-
}
350-
});
351-
}
352-
353-
private void createSurfaceTexture() {
354-
mFullScreen = new FullFrameRect(
355-
new Texture2dProgram(Texture2dProgram.ProgramType.TEXTURE_EXT));
356-
mTextureId = mFullScreen.createTextureObject();
357-
mSurfaceTexture = new SurfaceTexture(mTextureId);
358-
mSurfaceTexture.setOnFrameAvailableListener(Capture.this);
359-
}
360-
361-
private void prepareFrameBuffers() {
362-
363-
GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
364-
365-
//Generate frame buffer
366-
GLES20.glGenFramebuffers(1, frameBuffers);
367-
GlUtil.checkGlError("glGenFramebuffers");
368-
//Bind frame buffer
369-
GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, frameBuffers.get(0));
370-
GlUtil.checkGlError("glBindFramebuffer");
371-
372-
//Generate render buffers
373-
GLES20.glGenRenderbuffers(1, renderBuffers);
374-
GlUtil.checkGlError("glGenRenderbuffers");
375-
//Bind render buffers
376-
GLES20.glBindRenderbuffer(GLES20.GL_RENDERBUFFER, renderBuffers.get(0));
377-
GlUtil.checkGlError("glBindRenderbuffer");
378-
//Allocate memory to render buffers
379-
GLES20.glRenderbufferStorage(GLES20.GL_RENDERBUFFER, GLES20.GL_DEPTH_COMPONENT16, width, height);
380-
GlUtil.checkGlError("glRenderbufferStorage");
381-
382-
//Attach render buffer to frame buffer
383-
GLES20.glFramebufferRenderbuffer(GLES20.GL_FRAMEBUFFER, GLES20.GL_DEPTH_ATTACHMENT,
384-
GLES20.GL_RENDERBUFFER, renderBuffers.get(0));
385-
GlUtil.checkGlError("glFramebufferRenderbuffer");
386-
387-
GLES20.glGenTextures(1, customTexture);
388-
GlUtil.checkGlError("glGenTextures");
389-
390-
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, customTexture.get(0));
391-
GlUtil.checkGlError("glBindTexture");
392-
393-
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR);
394-
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
395-
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE);
396-
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE);
397-
GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D, 0, GLES20.GL_RGBA, width, height, 0, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, null);
398-
GlUtil.checkGlError("glTexImage2D");
399-
400-
GLES20.glFramebufferTexture2D(GLES20.GL_FRAMEBUFFER, GLES20.GL_COLOR_ATTACHMENT0,
401-
GLES20.GL_TEXTURE_2D, customTexture.get(0), 0);
402-
GlUtil.checkGlError("glFramebufferTexture2D");
403-
404-
/*
405-
//No sure if this is required in opengl es 2. Ignoring as of now.
406-
IntBuffer drawBuffers = IntBuffer.allocate(1);
407-
drawBuffers.put(0, GLES20.GL_COLOR_ATTACHMENT0);
408-
GLES30.glDrawBuffers(1, drawBuffers);
409-
*/
410-
411-
// See if GLES is happy with all this.
412-
int status = GLES20.glCheckFramebufferStatus(GLES20.GL_FRAMEBUFFER);
413-
if (status != GLES20.GL_FRAMEBUFFER_COMPLETE) {
414-
throw new RuntimeException("Framebuffer not complete, status=" + status);
415-
}
416-
}
417-
418-
//The following method has been copied from Syphon library for processing
419-
public void getImage(boolean loadPixels) {
420-
421-
if (destpg == null || destpg.width != width || destpg.height != height) {
422-
destpg = (PGraphicsOpenGL) parent.createGraphics(width, height, PConstants.P2D);
423-
destpg.pgl.setGlThread(Thread.currentThread());
424-
}
425-
426-
destpg.beginDraw();
427-
destpg.background(0, 0);
428-
PGL pgl = destpg.beginPGL();
429-
pgl.drawTexture(PGL.TEXTURE_2D, customTexture.get(0), width, height,
430-
0, 0, width, height);
431-
destpg.endPGL();
432-
destpg.endDraw();
433-
434-
// Uses the PGraphics texture as the cache object for the image
435-
Texture tex = destpg.getTexture();
436-
pg.setCache(this, tex);
437-
if (loadPixels) {
438-
super.loadPixels();
439-
tex.get(this.pixels);
440-
this.setLoaded(false);
441-
}
442-
}
443282
}

src/in/omerjerk/processing/video/android/Movie.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,18 @@ public Movie(PApplet parent) {
99
}
1010

1111
public Movie(PApplet parent, int width, int height) {
12+
super(parent);
1213
if (width == -1 || height == -1) {
1314
width = 720;
1415
height = 1280;
1516
}
1617
init(width, height, ARGB);
1718
}
19+
20+
@Override
21+
public void onPause() {
22+
}
23+
24+
@Override
25+
public void onResume() {}
1826
}

0 commit comments

Comments
 (0)