I’m trying to render a XML layout to an OpenGL surface. That works correctly, and I can do 3D effects on the layout. But, when I try to update it and render it again, it does not work. With twiddling and trying different functions, the end result is either a black object, graphic corruption on widgets external to the surface, or the original object without any modification.
My code to transform a view to a bitmap is:
public static Bitmap toBitmap(final View view) {
final Bitmap bitmap = Bitmap.createBitmap(400, 100,
Bitmap.Config.ARGB_8888);
final Canvas canvas = new Canvas(bitmap);
final Paint paint = new Paint();
view.setDrawingCacheEnabled(true);
final int width = MeasureSpec.makeMeasureSpec(canvas.getWidth(),
MeasureSpec.EXACTLY);
final int height = MeasureSpec.makeMeasureSpec(canvas.getHeight(),
MeasureSpec.EXACTLY);
view.measure(width, height);
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
canvas.drawBitmap(view.getDrawingCache(), 0, 0, paint);
return bitmap;
}
Then, in my object’s constructor, I call:
public static int loadTexture(final Bitmap bitmap) {
GLES20.glGenTextures(1, textureHandle, 0);
if (textureHandle[0] != 0) {
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureHandle[0]);
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D,
GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_NEAREST);
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D,
GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_NEAREST);
GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);
bitmap.recycle();
}
if (textureHandle[0] == 0) {
throw new RuntimeException("Error loading texture.");
}
return textureHandle[0];
}
Calling the loadTexture() method again with an updated layout will render it black. I wrote another small method to try different ways:
public static int updateTexture(final Bitmap bitmap) {
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureHandle[0]);
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D,
GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_NEAREST);
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D,
GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_NEAREST);
GLUtils.texSubImage2D(GLES20.GL_TEXTURE_2D, 0, 0, 0, bitmap);
//GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);
// GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, 0);
return textureHandle[0];
}
This leaves the original texture unchanged, and produces an error:
<qgl2DrvAPI_glTexSubImage2D:502>: GL_INVALID_VALUE
glStartTilingQCOM: 0x501
Commenting the texSubImage2D() call and replacing it with texImage2D() does nothing, and produces no error message.
If anyone can shed light on anything I may have done wrong, that would be greatly appreciated.
Thanks!
Are you calling the update method from the OpenGL thread? You don’t need to specify again the texture parameters if you are using the texSubImage function.