diff --git a/android/core/src/processing/core/PApplet.java b/android/core/src/processing/core/PApplet.java index 20c9d1b4b..2bce094e7 100644 --- a/android/core/src/processing/core/PApplet.java +++ b/android/core/src/processing/core/PApplet.java @@ -727,6 +727,7 @@ public class PApplet extends Activity implements PConstants, Runnable { // null. This is required because PApplet.onResume events (which call // this.onResume() and thus require a valid renderer) are triggered // before surfaceChanged() is ever called. + println("Creating A3D"); g3 = new PGraphicsAndroid3D(); // Set semi-arbitrary size; will be set properly when surfaceChanged() called g3.setSize(wide, high); @@ -797,7 +798,17 @@ public class PApplet extends Activity implements PConstants, Runnable { System.out.println("surfaceDestroyed()"); } - //g3.dispose(); + /* + // TODO: Check how to make sure of calling g3.dispose() when this call to + // surfaceDestoryed corresponds to the sketch being shut down instead of just + // taken to the background. + + // For instance, something like this would be ok? + // The sketch is being stopped, so we dispose the resources. + if (!paused) { + g3.dispose(); + } + */ } diff --git a/android/core/src/processing/core/PFramebuffer.java b/android/core/src/processing/core/PFramebuffer.java index 5e1de3c05..8a81daac6 100644 --- a/android/core/src/processing/core/PFramebuffer.java +++ b/android/core/src/processing/core/PFramebuffer.java @@ -90,8 +90,7 @@ public class PFramebuffer implements PConstants { createFramebuffer(w, h); - pixelBuffer = IntBuffer.allocate(width * height); - pixelBuffer.rewind(); + pixelBuffer = null; if (!screenFb && !FboMode) { // When FBOs are not available, rendering to texture is implemented by saving a portion of @@ -269,6 +268,7 @@ public class PFramebuffer implements PConstants { // Saves content of the screen into the backup texture. public void backupScreen() { + if (pixelBuffer == null) allocatePixelBuffer(); gl.glReadPixels(0, 0, width, height, GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, pixelBuffer); copyToTexture(pixelBuffer, backupTexture.getGLID(), backupTexture.getGLTarget()); } @@ -280,6 +280,7 @@ public class PFramebuffer implements PConstants { // Copies current content of screen to color buffers. public void copyToColorBuffers() { + if (pixelBuffer == null) allocatePixelBuffer(); gl.glReadPixels(0, 0, width, height, GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, pixelBuffer); for (int i = 0; i < numColorBuffers; i++) { copyToTexture(pixelBuffer, glColorBufferIDs[i], glColorBufferTargets[i]); @@ -287,12 +288,15 @@ public class PFramebuffer implements PConstants { } public void readPixels() { + if (pixelBuffer == null) allocatePixelBuffer(); gl.glReadPixels(0, 0, width, height, GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, pixelBuffer); } public void getPixels(int[] pixels) { - pixelBuffer.get(pixels); - pixelBuffer.rewind(); + if (pixelBuffer != null) { + pixelBuffer.get(pixels); + pixelBuffer.rewind(); + } } public IntBuffer getPixelBuffer() { @@ -307,6 +311,11 @@ public class PFramebuffer implements PConstants { gl.glDisable(gltarget); } + protected void allocatePixelBuffer() { + pixelBuffer = IntBuffer.allocate(width * height); + pixelBuffer.rewind(); + } + protected void createFramebuffer(int w, int h) { deleteFramebuffer(); // Just in the case this object is being re-initialized. diff --git a/android/core/src/processing/core/PGraphicsAndroid3D.java b/android/core/src/processing/core/PGraphicsAndroid3D.java index f5e92b671..f75981718 100644 --- a/android/core/src/processing/core/PGraphicsAndroid3D.java +++ b/android/core/src/processing/core/PGraphicsAndroid3D.java @@ -164,7 +164,7 @@ public class PGraphicsAndroid3D extends PGraphics { /** Default ambient light for the entire scene **/ public float[] baseLight = { 0.05f, 0.05f, 0.05f, 1.0f }; - boolean lightsAllocated = false; + protected boolean lightsAllocated = false; // ........................................................ @@ -189,26 +189,26 @@ public class PGraphicsAndroid3D extends PGraphics { protected PImage[] multitextureImages = new PImage[MAX_TEXTURES]; // Used to detect changes in the current texture images. - protected PImage multitextureImages0[] = new PImage[MAX_TEXTURES]; + protected PImage[] multitextureImages0 = new PImage[MAX_TEXTURES]; // Current multitexture UV coordinates. protected float[] multitextureU = new float[MAX_TEXTURES]; protected float[] multitextureV = new float[MAX_TEXTURES]; // Multitexture UV coordinates for all vertices. - float vertexU[][] = new float[DEFAULT_VERTICES][1]; - float vertexV[][] = new float[DEFAULT_VERTICES][1]; + protected float[][] vertexU = new float[DEFAULT_VERTICES][1]; + protected float[][] vertexV = new float[DEFAULT_VERTICES][1]; // Texture images assigned to each vertex. - PImage vertexTex[][] = new PImage[DEFAULT_VERTICES][1]; + protected PImage[][] vertexTex = new PImage[DEFAULT_VERTICES][1]; // UV arrays used in renderTriangles(). - float renderUa[] = new float[MAX_TEXTURES]; - float renderVa[] = new float[MAX_TEXTURES]; - float renderUb[] = new float[MAX_TEXTURES]; - float renderVb[] = new float[MAX_TEXTURES]; - float renderUc[] = new float[MAX_TEXTURES]; - float renderVc[] = new float[MAX_TEXTURES]; + protected float[] renderUa = new float[MAX_TEXTURES]; + protected float[] renderVa = new float[MAX_TEXTURES]; + protected float[] renderUb = new float[MAX_TEXTURES]; + protected float[] renderVb = new float[MAX_TEXTURES]; + protected float[] renderUc = new float[MAX_TEXTURES]; + protected float[] renderVc = new float[MAX_TEXTURES]; // ........................................................ @@ -223,22 +223,22 @@ public class PGraphicsAndroid3D extends PGraphics { static protected final int TRIANGLE_FIELD_COUNT = 3; // Points - static final int DEFAULT_POINTS = 512; + public static final int DEFAULT_POINTS = 512; protected int pointCount; protected int[][] points = new int[DEFAULT_POINTS][POINT_FIELD_COUNT]; // Lines. - static final int DEFAULT_LINES = 512; + public static final int DEFAULT_LINES = 512; protected int lineCount; protected int[][] lines = new int[DEFAULT_LINES][LINE_FIELD_COUNT]; // Triangles. - static final int DEFAULT_TRIANGLES = 256; + public static final int DEFAULT_TRIANGLES = 256; protected int triangleCount; // total number of triangles protected int[][] triangles = new int[DEFAULT_TRIANGLES][TRIANGLE_FIELD_COUNT]; // Vertex, color, texture coordinate and normal buffers. - static final int DEFAULT_BUFFER_SIZE = 512; + public static final int DEFAULT_BUFFER_SIZE = 512; private IntBuffer vertexBuffer; private IntBuffer colorBuffer; private IntBuffer normalBuffer; @@ -282,23 +282,27 @@ public class PGraphicsAndroid3D extends PGraphics { // ........................................................ + public static final int DEFAULT_PATHS = 64; + // This is done to keep track of start/stop information for lines in the // line array, so that lines can be shown as a single path, rather than just // individual segments. protected int pathCount; - protected int[] pathOffset = new int[64]; - protected int[] pathLength = new int[64]; + protected int[] pathOffset = new int[DEFAULT_PATHS]; + protected int[] pathLength = new int[DEFAULT_PATHS]; // ........................................................ + public static final int DEFAULT_FACES = 64; + // And this is done to keep track of start/stop information for textured // triangles in the triangle array, so that a range of triangles with the // same texture applied to them are correctly textured during the // rendering stage. protected int faceCount; - protected int[] faceOffset = new int[64]; - protected int[] faceLength = new int[64]; - protected PImage[][] faceTextures = new PImage[64][MAX_TEXTURES]; + protected int[] faceOffset = new int[DEFAULT_FACES]; + protected int[] faceLength = new int[DEFAULT_FACES]; + protected PImage[][] faceTextures = new PImage[DEFAULT_FACES][MAX_TEXTURES]; // ........................................................ @@ -317,10 +321,10 @@ public class PGraphicsAndroid3D extends PGraphics { static protected final int GL_FRAME_BUFFER = 2; static protected final int GL_RENDER_BUFFER = 3; - static protected Set glTextureObjects; - static protected Set glVertexBuffers; - static protected Set glFrameBuffers; - static protected Set glRenderBuffers; + static protected Set glTextureObjects = new HashSet(); + static protected Set glVertexBuffers = new HashSet(); + static protected Set glFrameBuffers = new HashSet(); + static protected Set glRenderBuffers = new HashSet(); // ........................................................ @@ -352,17 +356,17 @@ public class PGraphicsAndroid3D extends PGraphics { // ....................................................... static protected boolean usingModelviewStack; - static A3DMatrixStack modelviewStack; + static protected A3DMatrixStack modelviewStack; // ........................................................ // Used to save a copy of the last drawn frame in order to repaint on the // backbuffer when using no clear mode. - public int[] screenTexCrop; + protected int[] screenTexCrop; // This variable controls clearing of the color buffer. - boolean clearColorBuffer; - boolean clearColorBuffer0; + protected boolean clearColorBuffer; + protected boolean clearColorBuffer0; // ........................................................ @@ -406,18 +410,11 @@ public class PGraphicsAndroid3D extends PGraphics { // Size of a float (in bytes). protected static final int SIZEOF_FLOAT = Float.SIZE / 8; - // //////////////////////////////////////////////////////////// + public PGraphicsAndroid3D() { renderer = new A3DRenderer(); - - if (glTextureObjects == null) { - glTextureObjects = new HashSet(); - glVertexBuffers = new HashSet(); - glFrameBuffers = new HashSet(); - glRenderBuffers = new HashSet(); - } } // public void setParent(PApplet parent) @@ -426,7 +423,7 @@ public class PGraphicsAndroid3D extends PGraphics { super.setPrimary(primary); // argh, a semi-transparent opengl surface? Yes! - format = ARGB; + format = ARGB; } // public void setPath(String path) @@ -519,11 +516,19 @@ public class PGraphicsAndroid3D extends PGraphics { numTexBuffers = 1; buffersAllocated = true; - } + } } + public void delete() { + super.delete(); + deleteAllGLResources(); + } + + public void dispose() { + super.dispose(); + deleteAllGLResources(); } @@ -902,7 +907,7 @@ public class PGraphicsAndroid3D extends PGraphics { // changed accordingly. noLights(); } - } + } if (!settingsInited) { defaultSettings(); @@ -992,9 +997,7 @@ public class PGraphicsAndroid3D extends PGraphics { // Creating framebuffer object or draw texture for the primary surface, depending on the // availability of FBOs. if (fboSupported) { - if (offscreenFramebuffer == null) { - createOffscreenFramebuffer(); - } + createOffscreenFramebuffer(); } else { if (gl11 == null || gl11x == null) { throw new RuntimeException("A3D: no clear mode with no FBOs requires OpenGL ES 1.1"); @@ -1011,7 +1014,7 @@ public class PGraphicsAndroid3D extends PGraphics { gl.glClearColor(0, 0, 0, 0); gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT); } else { - // We need to save the color buffer after finishing with the rendering of the this frame, + // We need to save the color buffer after finishing with the rendering of this frame, // to use is as the background for the next frame (I call this "incremental rendering"). if (fboSupported) { @@ -1072,7 +1075,7 @@ public class PGraphicsAndroid3D extends PGraphics { // buffer might be bound, but should popped to the screen buffer for correct // continuation of onscreen rendering. clearColorBuffer0 = clearColorBuffer; - + report("bot beginDraw()"); } @@ -1281,7 +1284,7 @@ public class PGraphicsAndroid3D extends PGraphics { numRecordedTextures = 0; - recordedGroups = new ArrayList(64); + recordedGroups = new ArrayList(PApplet.max(DEFAULT_PATHS, DEFAULT_FACES)); } public void beginShape(int kind) { @@ -5422,7 +5425,7 @@ public class PGraphicsAndroid3D extends PGraphics { // LOAD/UPDATE TEXTURE - public void loadTexture() { + public void loadTexture() { if (texture == null) { loadTexture(NEAREST); texture.setFlippedY(true); @@ -5434,8 +5437,7 @@ public class PGraphicsAndroid3D extends PGraphics { pixelBuffer.rewind(); } - gl.glReadPixels(0, 0, width, height, GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, - pixelBuffer); + gl.glReadPixels(0, 0, width, height, GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, pixelBuffer); copyToScreenTexture(pixelBuffer); pixelBuffer.rewind(); @@ -6044,7 +6046,7 @@ public class PGraphicsAndroid3D extends PGraphics { // setup() method is triggered) then this seems to be the best // location to release resources. Also, at this point we are // guaranteed to have all the gl objects non-null. - deleteAllGLResources(); + //deleteAllGLResources(); gl = null; gl11 = null; diff --git a/android/core/src/processing/core/PTexture.java b/android/core/src/processing/core/PTexture.java index 87ff1f8ca..cc9e4ab4c 100644 --- a/android/core/src/processing/core/PTexture.java +++ b/android/core/src/processing/core/PTexture.java @@ -80,7 +80,7 @@ public class PTexture implements PConstants { * @param parent PApplet * @param width int * @param height int - */ + */ public PTexture(PApplet parent, int width, int height) { this(parent, width, height, new Parameters()); } @@ -92,8 +92,8 @@ public class PTexture implements PConstants { * @param parent PApplet * @param width int * @param height int - * @param params Parameters - */ + * @param params Parameters + */ public PTexture(PApplet parent, int width, int height, Parameters params) { this.parent = parent; this.width = width; @@ -106,43 +106,48 @@ public class PTexture implements PConstants { setParameters(params); createTexture(width, height); - } - + } + /** * Creates an instance of PTexture using image file filename as source. * @param parent PApplet * @param filename String - */ + */ public PTexture(PApplet parent, String filename) { this(parent, filename, new Parameters()); } - + /** * Creates an instance of PTexture using image file filename as source and the specified texture parameters. * @param parent PApplet - * @param filename String + * @param filename String * @param params Parameters - */ + */ public PTexture(PApplet parent, String filename, Parameters params) { this.parent = parent; - + a3d = (PGraphicsAndroid3D)parent.g; - gl = a3d.gl; + gl = a3d.gl; glID = 0; PImage img = parent.loadImage(filename); setParameters(params); set(img); - } + } public void delete() { deleteTexture(); } + /* + protected void finalize() { + deleteTexture(); + } + */ //////////////////////////////////////////////////////////// @@ -187,11 +192,6 @@ public class PTexture implements PConstants { // Now, overwriting "this" with tex. copyObject(tex); - // Zeroing the texture id of tex, so the texture is not - // deleted by OpenGL when the object is finalized by the GC. - // "This" texture now wraps the one created in tex. - tex.glID = 0; - // Nullifying some utility objects so they are recreated with the appropriate // size when needed. tmpPixels = null; @@ -406,7 +406,7 @@ public class PTexture implements PConstants { /** * Provides the ID of the OpenGL texture object. * @return int - */ + */ protected int getGLID() { return glID; } @@ -415,7 +415,7 @@ public class PTexture implements PConstants { /** * Returns the texture target. * @return int - */ + */ protected int getGLTarget() { return glTarget; } @@ -424,7 +424,7 @@ public class PTexture implements PConstants { /** * Returns the texture internal format. * @return int - */ + */ protected int getGLFormat() { return glFormat; } @@ -433,7 +433,7 @@ public class PTexture implements PConstants { /** * Returns the texture minimization filter. * @return int - */ + */ protected int getGLMinFilter() { return glMinFilter; } @@ -442,11 +442,11 @@ public class PTexture implements PConstants { /** * Returns the texture magnification filter. * @return int - */ + */ protected int getGLMagFilter() { return glMagFilter; } - + /** * Returns the texture wrapping mode for the S coordinate. * @return int @@ -466,34 +466,34 @@ public class PTexture implements PConstants { /** * Returns true or false whether or not the texture is using mipmaps. * @return boolean - */ + */ protected boolean usingMipmaps() { return usingMipmaps; } - + /** * Returns the maximum possible value for the texture coordinate U (horizontal). * @return float - */ + */ protected float getMaxTexCoordU() { return maxTexCoordU; } - + /** * Returns the maximum possible value for the texture coordinate V (vertical). * @return float - */ + */ protected float getMaxTexCoordV() { return maxTexCoordV; } - + /** * Returns true if the texture is flipped along the horizontal direction. * @return boolean; - */ + */ protected boolean isFlippedX() { return flippedX; } @@ -502,16 +502,16 @@ public class PTexture implements PConstants { /** * Sets the texture as flipped or not flipped on the horizontal direction. * @param v boolean; - */ + */ protected void setFlippedX(boolean v) { flippedX = v; - } - + } + /** * Returns true if the texture is flipped along the vertical direction. * @return boolean; - */ + */ protected boolean isFlippedY() { return flippedY; } @@ -520,12 +520,12 @@ public class PTexture implements PConstants { /** * Sets the texture as flipped or not flipped on the vertical direction. * @param v boolean; - */ + */ protected void setFlippedY(boolean v) { flippedY = v; } - + //////////////////////////////////////////////////////////// // Utilities @@ -588,7 +588,7 @@ public class PTexture implements PConstants { } yindex -= mult * width * 2; } - } + } /** @@ -597,7 +597,7 @@ public class PTexture implements PConstants { * are used in the YUV420 to RBGBA conversion. * @param intArray int[] * @param tIntArray int[] - * @param arrayFormat int + * @param arrayFormat int * @param w int * @param h int */ @@ -605,7 +605,7 @@ public class PTexture implements PConstants { if (PGraphicsAndroid3D.BIG_ENDIAN) { switch (arrayFormat) { case ALPHA: - + // Converting from xxxA into RGBA. RGB is set to white // (0xFFFFFF, i.e.: (255, 255, 255)) for (int i = 0; i< intArray.length; i++) { @@ -614,7 +614,7 @@ public class PTexture implements PConstants { break; case RGB: - + // Converting xRGB into RGBA. A is set to 0xFF (255, full opacity). for (int i = 0; i< intArray.length; i++) { int pixel = intArray[i]; @@ -672,7 +672,7 @@ public class PTexture implements PConstants { switch (arrayFormat) { case ALPHA: - + // Converting xxxA into ARGB, with RGB set to white. for (int i = 0; i< intArray.length; i++) { tIntArray[i] = (intArray[i] << 24) | 0x00FFFFFF; @@ -693,7 +693,7 @@ public class PTexture implements PConstants { break; case ARGB: - + // We need to convert ARGB into ABGR, // so R and B must be swapped, A and G just brought back in. for (int i = 0; i< intArray.length; i++) { @@ -743,7 +743,7 @@ public class PTexture implements PConstants { * Reorders a pixel array in a given format into ARGB. The input array must be * of size width * height, while the output array must be of glWidth * glHeight. * @param intArray int[] - * @param intArray int[] + * @param intArray int[] * @param arrayFormat int */ protected void convertToARGB(int[] intArray, int[] tIntArray, int arrayFormat) { @@ -870,7 +870,7 @@ public class PTexture implements PConstants { /** * Creates the opengl texture object. * @param w int - * @param h int + * @param h int */ protected void createTexture(int w, int h) { deleteTexture(); // Just in the case this object is being re-initialized. @@ -1052,16 +1052,16 @@ public class PTexture implements PConstants { /** * Sets texture target and internal format according to the target and type specified. - * @param target int + * @param target int * @param params GLTextureParameters - */ + */ protected void setParameters(Parameters params) { - if (params.target == TEXTURE2D) { + if (params.target == TEXTURE2D) { glTarget = GL10.GL_TEXTURE_2D; } else { - throw new RuntimeException("GTexture: Unknown texture target"); - } - + throw new RuntimeException("GTexture: Unknown texture target"); + } + if (params.format == RGB) { glFormat = GL10.GL_RGB; } else if (params.format == ARGB) { @@ -1076,13 +1076,13 @@ public class PTexture implements PConstants { glMinFilter = GL10.GL_NEAREST; } else if (params.minFilter == LINEAR) { glMinFilter = GL10.GL_LINEAR; - } else if (params.minFilter == NEAREST_MIPMAP_NEAREST) { + } else if (params.minFilter == NEAREST_MIPMAP_NEAREST) { glMinFilter = GL10.GL_NEAREST_MIPMAP_NEAREST; - } else if (params.minFilter == LINEAR_MIPMAP_NEAREST) { + } else if (params.minFilter == LINEAR_MIPMAP_NEAREST) { glMinFilter = GL10.GL_LINEAR_MIPMAP_NEAREST; - } else if (params.minFilter == NEAREST_MIPMAP_LINEAR) { + } else if (params.minFilter == NEAREST_MIPMAP_LINEAR) { glMinFilter = GL10.GL_NEAREST_MIPMAP_LINEAR; - } else if (params.minFilter == LINEAR_MIPMAP_LINEAR) { + } else if (params.minFilter == LINEAR_MIPMAP_LINEAR) { glMinFilter = GL10.GL_LINEAR_MIPMAP_LINEAR; } else { throw new RuntimeException("GTexture: Unknown minimization filter"); @@ -1111,7 +1111,7 @@ public class PTexture implements PConstants { } else { throw new RuntimeException("GTexture: Unknown wrapping mode"); } - } + } ///////////////////////////////////////////////////////////////////////////