Initializing textures with small patches to avoid use of large arrays

This commit is contained in:
codeanticode
2012-01-27 21:26:51 +00:00
parent 27d03c3376
commit 90302567e2
6 changed files with 40 additions and 9 deletions
@@ -108,6 +108,9 @@ public class PGL {
* vertices to have good room for vertex reuse. */
public static final int MAX_TESS_INDICES = 2 * MAX_TESS_VERTICES;
/** Maximum dimension of a texture used to hold font data. **/
public static final int MAX_FONT_TEX_SIZE = 2048;
public static final int LESS = GL.GL_LESS;
public static final int LESS_OR_EQUAL = GL.GL_LEQUAL;
public static final int COUNTER_CLOCKWISE = GL.GL_CCW;
@@ -3146,7 +3146,8 @@ public class PGraphicsOpenGL extends PGraphics {
protected void textLineImpl(char buffer[], int start, int stop, float x, float y) {
textTex = (PFontTexture)textFont.getCache(pg);
if (textTex == null) {
textTex = new PFontTexture(parent, textFont, maxTextureSize, maxTextureSize);
textTex = new PFontTexture(parent, textFont, PApplet.min(PGL.MAX_FONT_TEX_SIZE, maxTextureSize),
PApplet.min(PGL.MAX_FONT_TEX_SIZE, maxTextureSize));
textFont.setCache(this, textTex);
} else {
if (!pgl.contextIsCurrent(textTex.context)) {
@@ -788,9 +788,18 @@ public class PTexture implements PConstants {
pgl.initTex(glTarget, glFormat, glWidth, glHeight);
// Once OpenGL knows the size of the new texture, we make sure it doesn't
// contain any garbage in the region of interest (0, 0, width, height):
int[] texels = new int[width * height];
setTexels(texels, 0, 0, width, height);
// contain any garbage in the region of interest (0, 0, width, height):
// Doing in patches of 16x16 pixels to avoid creating a (potentially)
// very large transient array which in certain situations (memory-
// constrained android devices) might lead to an out-of-memory error.
int[] texels = new int[16 * 16];
for (int y = 0; y < height + 16; y += 16) {
int h = PApplet.min(16, height - y);
for (int x = 0; x < width + 16; x += 16) {
int w = PApplet.min(16, width - x);
setTexels(texels, x, y, w, h);
}
}
texels = null;
pgl.unbindTexture(glTarget);