OSDN Git Service

Fix hardcoded date/time formats in 3D Gallery.
[android-x86/packages-apps-Gallery2.git] / src / com / cooliris / media / CanvasTexture.java
1 package com.cooliris.media;
2
3 import javax.microedition.khronos.opengles.GL11;
4 import javax.microedition.khronos.opengles.GL11Ext;
5
6 import android.graphics.Bitmap;
7 import android.graphics.Canvas;
8 import android.opengl.GLUtils;
9
10 public abstract class CanvasTexture {
11     private int mWidth;
12     private int mHeight;
13     private int mTextureId;
14     private int mTextureWidth;
15     private int mTextureHeight;
16     private float mNormalizedWidth;
17     private float mNormalizedHeight;
18
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;
24
25     public CanvasTexture(Bitmap.Config bitmapConfig) {
26         mBitmapConfig = bitmapConfig;
27     }
28
29     public final void setNeedsDraw() {
30         mNeedsDraw = true;
31     }
32
33     public final int getWidth() {
34         return mWidth;
35     }
36
37     public final int getHeight() {
38         return mHeight;
39     }
40
41     public final float getNormalizedWidth() {
42         return mNormalizedWidth;
43     }
44
45     public final float getNormalizedHeight() {
46         return mNormalizedHeight;
47     }
48
49     public final void setSize(int width, int height) {
50         mWidth = width;
51         mHeight = height;
52         mNeedsResize = true;
53         mTextureWidth = -1;
54         mTextureHeight = -1;
55         onSizeChanged();
56     }
57
58     public void resetTexture() {
59         // Happens when restoring the scene. Need to manage this more automatically.
60         mTextureId = 0;
61         mNeedsResize = true;
62     }
63
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;
73
74         if (mNeedsResize || mTextureId == 0) {
75             // Clear the resize flag and mark as needing draw.
76             mNeedsDraw = true;
77
78             // Compute the power-of-2 padded size for the texture.
79             int newTextureWidth = Shared.nextPowerOf2(width);
80             int newTextureHeight = Shared.nextPowerOf2(height);
81
82             // Reallocate the bitmap only if the padded size has changed.
83             // TODO: reuse same texture if it is already large enough, just
84             // change clip rect.
85             if (textureWidth != newTextureWidth || textureHeight != newTextureHeight || mTextureId == 0) {
86                 // Allocate a texture if needed.
87                 if (textureId == 0) {
88                     int[] textureIdOut = new int[1];
89                     gl.glGenTextures(1, textureIdOut, 0);
90                     textureId = textureIdOut[0];
91                     mNeedsResize = false;
92                     mTextureId = textureId;
93
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);
100                 }
101
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;
109
110                 // Recycle the existing bitmap and create a new one.
111                 if (bitmap != null)
112                     bitmap.recycle();
113                 bitmap = Bitmap.createBitmap(textureWidth, textureHeight, mBitmapConfig);
114                 canvas.setBitmap(bitmap);
115                 mBitmap = bitmap;
116             }
117         }
118
119         // Bind the texture to the context.
120         if (textureId == 0) {
121             return false;
122         }
123         gl.glBindTexture(GL11.GL_TEXTURE_2D, textureId);
124
125         // Redraw the contents of the texture if needed.
126         if (mNeedsDraw) {
127             mNeedsDraw = false;
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);
132
133         }
134
135         return true;
136     }
137
138     public void draw(RenderView view, GL11 gl, int x, int y) {
139         if (bind(gl)) {
140             view.draw2D(x, y, 0, mWidth, mHeight);
141         }
142     }
143
144     public void drawWithEffect(RenderView view, GL11 gl, float x, float y, float anchorX, float anchorY, float alpha, float scale) {
145         if (bind(gl)) {
146             float width = mWidth;
147             float height = mHeight;
148
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;
153                 width *= scale;
154                 height *= scale;
155                 x = originX - anchorX * width;
156                 y = originY - anchorY * height;
157             }
158
159             // Set alpha if needed.
160             if (alpha != 1f) {  // CR: 1.0f
161                 view.setAlpha(alpha);
162             }
163             view.draw2D(x, y, 0, width, height);
164             if (alpha != 1f) {
165                 view.resetColor();
166             }
167         }
168     }
169
170     protected abstract void onSizeChanged();
171
172     protected abstract void renderCanvas(Canvas canvas, Bitmap backing, int width, int height);
173
174 } // CR: superfluous newline above.