1 package com.cooliris.media;
3 import javax.microedition.khronos.opengles.GL11;
4 import javax.microedition.khronos.opengles.GL11Ext;
6 import android.graphics.Bitmap;
7 import android.graphics.Canvas;
8 import android.opengl.GLUtils;
10 public abstract class CanvasTexture {
13 private int mTextureId;
14 private int mTextureWidth;
15 private int mTextureHeight;
16 private float mNormalizedWidth;
17 private float mNormalizedHeight;
19 private final Canvas mCanvas = new Canvas();
20 private final Bitmap.Config mBitmapConfig;
21 private Bitmap mBitmap = null;
22 private boolean mNeedsDraw = false;
23 private boolean mNeedsResize = false;
25 public CanvasTexture(Bitmap.Config bitmapConfig) {
26 mBitmapConfig = bitmapConfig;
29 public final void setNeedsDraw() {
33 public final int getWidth() {
37 public final int getHeight() {
41 public final float getNormalizedWidth() {
42 return mNormalizedWidth;
45 public final float getNormalizedHeight() {
46 return mNormalizedHeight;
49 public final void setSize(int width, int height) {
58 public void resetTexture() {
59 // Happens when restoring the scene. Need to manage this more automatically.
64 // This code seems largely a dup of CanvasLayer.
65 public boolean bind(GL11 gl) {
66 int width = (int) mWidth;
67 int height = (int) mHeight;
68 int textureId = mTextureId;
69 int textureWidth = mTextureWidth;
70 int textureHeight = mTextureHeight;
71 Canvas canvas = mCanvas;
72 Bitmap bitmap = mBitmap;
74 if (mNeedsResize || mTextureId == 0) {
75 // Clear the resize flag and mark as needing draw.
78 // Compute the power-of-2 padded size for the texture.
79 int newTextureWidth = Shared.nextPowerOf2(width);
80 int newTextureHeight = Shared.nextPowerOf2(height);
82 // Reallocate the bitmap only if the padded size has changed.
83 // TODO: reuse same texture if it is already large enough, just
85 if (textureWidth != newTextureWidth || textureHeight != newTextureHeight || mTextureId == 0) {
86 // Allocate a texture if needed.
88 int[] textureIdOut = new int[1];
89 gl.glGenTextures(1, textureIdOut, 0);
90 textureId = textureIdOut[0];
92 mTextureId = textureId;
94 // Set texture parameters.
95 gl.glBindTexture(GL11.GL_TEXTURE_2D, textureId);
96 gl.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S, GL11.GL_CLAMP_TO_EDGE);
97 gl.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T, GL11.GL_CLAMP_TO_EDGE);
98 gl.glTexParameterf(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR);
99 gl.glTexParameterf(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR);
102 // Set the new texture width and height.
103 textureWidth = newTextureWidth;
104 textureHeight = newTextureHeight;
105 mTextureWidth = newTextureWidth;
106 mTextureHeight = newTextureHeight;
107 mNormalizedWidth = (float) width / textureWidth;
108 mNormalizedHeight = (float) height / textureHeight;
110 // Recycle the existing bitmap and create a new one.
113 bitmap = Bitmap.createBitmap(textureWidth, textureHeight, mBitmapConfig);
114 canvas.setBitmap(bitmap);
119 // Bind the texture to the context.
120 if (textureId == 0) {
123 gl.glBindTexture(GL11.GL_TEXTURE_2D, textureId);
125 // Redraw the contents of the texture if needed.
128 renderCanvas(canvas, bitmap, width, height);
129 int[] cropRect = { 0, height, width, -height };
130 gl.glTexParameteriv(GL11.GL_TEXTURE_2D, GL11Ext.GL_TEXTURE_CROP_RECT_OES, cropRect, 0);
131 GLUtils.texImage2D(GL11.GL_TEXTURE_2D, 0, bitmap, 0);
138 public void draw(RenderView view, GL11 gl, int x, int y) {
140 view.draw2D(x, y, 0, mWidth, mHeight);
144 public void drawWithEffect(RenderView view, GL11 gl, float x, float y, float anchorX, float anchorY, float alpha, float scale) {
146 float width = mWidth;
147 float height = mHeight;
149 // Apply scale transform if not identity.
150 if (scale != 1) { // CR: 1.0f
151 float originX = x + anchorX * width;
152 float originY = y + anchorY * height;
155 x = originX - anchorX * width;
156 y = originY - anchorY * height;
159 // Set alpha if needed.
160 if (alpha != 1f) { // CR: 1.0f
161 view.setAlpha(alpha);
163 view.draw2D(x, y, 0, width, height);
170 protected abstract void onSizeChanged();
172 protected abstract void renderCanvas(Canvas canvas, Bitmap backing, int width, int height);
174 } // CR: superfluous newline above.