1104c45677660586026a7e74ef8c47d396403d50eMichael Jurka/*
2104c45677660586026a7e74ef8c47d396403d50eMichael Jurka * Copyright (C) 2010 The Android Open Source Project
3104c45677660586026a7e74ef8c47d396403d50eMichael Jurka *
4104c45677660586026a7e74ef8c47d396403d50eMichael Jurka * Licensed under the Apache License, Version 2.0 (the "License");
5104c45677660586026a7e74ef8c47d396403d50eMichael Jurka * you may not use this file except in compliance with the License.
6104c45677660586026a7e74ef8c47d396403d50eMichael Jurka * You may obtain a copy of the License at
7104c45677660586026a7e74ef8c47d396403d50eMichael Jurka *
8104c45677660586026a7e74ef8c47d396403d50eMichael Jurka *      http://www.apache.org/licenses/LICENSE-2.0
9104c45677660586026a7e74ef8c47d396403d50eMichael Jurka *
10104c45677660586026a7e74ef8c47d396403d50eMichael Jurka * Unless required by applicable law or agreed to in writing, software
11104c45677660586026a7e74ef8c47d396403d50eMichael Jurka * distributed under the License is distributed on an "AS IS" BASIS,
12104c45677660586026a7e74ef8c47d396403d50eMichael Jurka * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13104c45677660586026a7e74ef8c47d396403d50eMichael Jurka * See the License for the specific language governing permissions and
14104c45677660586026a7e74ef8c47d396403d50eMichael Jurka * limitations under the License.
15104c45677660586026a7e74ef8c47d396403d50eMichael Jurka */
16104c45677660586026a7e74ef8c47d396403d50eMichael Jurka
17104c45677660586026a7e74ef8c47d396403d50eMichael Jurkapackage com.android.gallery3d.glrenderer;
18104c45677660586026a7e74ef8c47d396403d50eMichael Jurka
19104c45677660586026a7e74ef8c47d396403d50eMichael Jurkaimport android.graphics.Bitmap;
20104c45677660586026a7e74ef8c47d396403d50eMichael Jurkaimport android.graphics.Rect;
21104c45677660586026a7e74ef8c47d396403d50eMichael Jurkaimport android.graphics.RectF;
22104c45677660586026a7e74ef8c47d396403d50eMichael Jurka
23104c45677660586026a7e74ef8c47d396403d50eMichael Jurkaimport javax.microedition.khronos.opengles.GL11;
24104c45677660586026a7e74ef8c47d396403d50eMichael Jurka
25104c45677660586026a7e74ef8c47d396403d50eMichael Jurka//
26104c45677660586026a7e74ef8c47d396403d50eMichael Jurka// GLCanvas gives a convenient interface to draw using OpenGL.
27104c45677660586026a7e74ef8c47d396403d50eMichael Jurka//
28104c45677660586026a7e74ef8c47d396403d50eMichael Jurka// When a rectangle is specified in this interface, it means the region
29104c45677660586026a7e74ef8c47d396403d50eMichael Jurka// [x, x+width) * [y, y+height)
30104c45677660586026a7e74ef8c47d396403d50eMichael Jurka//
31104c45677660586026a7e74ef8c47d396403d50eMichael Jurkapublic interface GLCanvas {
32104c45677660586026a7e74ef8c47d396403d50eMichael Jurka
33104c45677660586026a7e74ef8c47d396403d50eMichael Jurka    public GLId getGLId();
34104c45677660586026a7e74ef8c47d396403d50eMichael Jurka
35104c45677660586026a7e74ef8c47d396403d50eMichael Jurka    // Tells GLCanvas the size of the underlying GL surface. This should be
36104c45677660586026a7e74ef8c47d396403d50eMichael Jurka    // called before first drawing and when the size of GL surface is changed.
37104c45677660586026a7e74ef8c47d396403d50eMichael Jurka    // This is called by GLRoot and should not be called by the clients
38104c45677660586026a7e74ef8c47d396403d50eMichael Jurka    // who only want to draw on the GLCanvas. Both width and height must be
39104c45677660586026a7e74ef8c47d396403d50eMichael Jurka    // nonnegative.
40104c45677660586026a7e74ef8c47d396403d50eMichael Jurka    public abstract void setSize(int width, int height);
41104c45677660586026a7e74ef8c47d396403d50eMichael Jurka
42104c45677660586026a7e74ef8c47d396403d50eMichael Jurka    // Clear the drawing buffers. This should only be used by GLRoot.
43104c45677660586026a7e74ef8c47d396403d50eMichael Jurka    public abstract void clearBuffer();
44104c45677660586026a7e74ef8c47d396403d50eMichael Jurka
45104c45677660586026a7e74ef8c47d396403d50eMichael Jurka    public abstract void clearBuffer(float[] argb);
46104c45677660586026a7e74ef8c47d396403d50eMichael Jurka
47104c45677660586026a7e74ef8c47d396403d50eMichael Jurka    // Sets and gets the current alpha, alpha must be in [0, 1].
48104c45677660586026a7e74ef8c47d396403d50eMichael Jurka    public abstract void setAlpha(float alpha);
49104c45677660586026a7e74ef8c47d396403d50eMichael Jurka
50104c45677660586026a7e74ef8c47d396403d50eMichael Jurka    public abstract float getAlpha();
51104c45677660586026a7e74ef8c47d396403d50eMichael Jurka
52104c45677660586026a7e74ef8c47d396403d50eMichael Jurka    // (current alpha) = (current alpha) * alpha
53104c45677660586026a7e74ef8c47d396403d50eMichael Jurka    public abstract void multiplyAlpha(float alpha);
54104c45677660586026a7e74ef8c47d396403d50eMichael Jurka
55104c45677660586026a7e74ef8c47d396403d50eMichael Jurka    // Change the current transform matrix.
56104c45677660586026a7e74ef8c47d396403d50eMichael Jurka    public abstract void translate(float x, float y, float z);
57104c45677660586026a7e74ef8c47d396403d50eMichael Jurka
58104c45677660586026a7e74ef8c47d396403d50eMichael Jurka    public abstract void translate(float x, float y);
59104c45677660586026a7e74ef8c47d396403d50eMichael Jurka
60104c45677660586026a7e74ef8c47d396403d50eMichael Jurka    public abstract void scale(float sx, float sy, float sz);
61104c45677660586026a7e74ef8c47d396403d50eMichael Jurka
62104c45677660586026a7e74ef8c47d396403d50eMichael Jurka    public abstract void rotate(float angle, float x, float y, float z);
63104c45677660586026a7e74ef8c47d396403d50eMichael Jurka
64104c45677660586026a7e74ef8c47d396403d50eMichael Jurka    public abstract void multiplyMatrix(float[] mMatrix, int offset);
65104c45677660586026a7e74ef8c47d396403d50eMichael Jurka
66104c45677660586026a7e74ef8c47d396403d50eMichael Jurka    // Pushes the configuration state (matrix, and alpha) onto
67104c45677660586026a7e74ef8c47d396403d50eMichael Jurka    // a private stack.
68104c45677660586026a7e74ef8c47d396403d50eMichael Jurka    public abstract void save();
69104c45677660586026a7e74ef8c47d396403d50eMichael Jurka
70104c45677660586026a7e74ef8c47d396403d50eMichael Jurka    // Same as save(), but only save those specified in saveFlags.
71104c45677660586026a7e74ef8c47d396403d50eMichael Jurka    public abstract void save(int saveFlags);
72104c45677660586026a7e74ef8c47d396403d50eMichael Jurka
73104c45677660586026a7e74ef8c47d396403d50eMichael Jurka    public static final int SAVE_FLAG_ALL = 0xFFFFFFFF;
74104c45677660586026a7e74ef8c47d396403d50eMichael Jurka    public static final int SAVE_FLAG_ALPHA = 0x01;
75104c45677660586026a7e74ef8c47d396403d50eMichael Jurka    public static final int SAVE_FLAG_MATRIX = 0x02;
76104c45677660586026a7e74ef8c47d396403d50eMichael Jurka
77104c45677660586026a7e74ef8c47d396403d50eMichael Jurka    // Pops from the top of the stack as current configuration state (matrix,
78104c45677660586026a7e74ef8c47d396403d50eMichael Jurka    // alpha, and clip). This call balances a previous call to save(), and is
79104c45677660586026a7e74ef8c47d396403d50eMichael Jurka    // used to remove all modifications to the configuration state since the
80104c45677660586026a7e74ef8c47d396403d50eMichael Jurka    // last save call.
81104c45677660586026a7e74ef8c47d396403d50eMichael Jurka    public abstract void restore();
82104c45677660586026a7e74ef8c47d396403d50eMichael Jurka
83104c45677660586026a7e74ef8c47d396403d50eMichael Jurka    // Draws a line using the specified paint from (x1, y1) to (x2, y2).
84104c45677660586026a7e74ef8c47d396403d50eMichael Jurka    // (Both end points are included).
85104c45677660586026a7e74ef8c47d396403d50eMichael Jurka    public abstract void drawLine(float x1, float y1, float x2, float y2, GLPaint paint);
86104c45677660586026a7e74ef8c47d396403d50eMichael Jurka
87104c45677660586026a7e74ef8c47d396403d50eMichael Jurka    // Draws a rectangle using the specified paint from (x1, y1) to (x2, y2).
88104c45677660586026a7e74ef8c47d396403d50eMichael Jurka    // (Both end points are included).
89104c45677660586026a7e74ef8c47d396403d50eMichael Jurka    public abstract void drawRect(float x1, float y1, float x2, float y2, GLPaint paint);
90104c45677660586026a7e74ef8c47d396403d50eMichael Jurka
91104c45677660586026a7e74ef8c47d396403d50eMichael Jurka    // Fills the specified rectangle with the specified color.
92104c45677660586026a7e74ef8c47d396403d50eMichael Jurka    public abstract void fillRect(float x, float y, float width, float height, int color);
93104c45677660586026a7e74ef8c47d396403d50eMichael Jurka
94104c45677660586026a7e74ef8c47d396403d50eMichael Jurka    // Draws a texture to the specified rectangle.
95104c45677660586026a7e74ef8c47d396403d50eMichael Jurka    public abstract void drawTexture(
96104c45677660586026a7e74ef8c47d396403d50eMichael Jurka            BasicTexture texture, int x, int y, int width, int height);
97104c45677660586026a7e74ef8c47d396403d50eMichael Jurka
98104c45677660586026a7e74ef8c47d396403d50eMichael Jurka    public abstract void drawMesh(BasicTexture tex, int x, int y, int xyBuffer,
99104c45677660586026a7e74ef8c47d396403d50eMichael Jurka            int uvBuffer, int indexBuffer, int indexCount);
100104c45677660586026a7e74ef8c47d396403d50eMichael Jurka
101104c45677660586026a7e74ef8c47d396403d50eMichael Jurka    // Draws the source rectangle part of the texture to the target rectangle.
102104c45677660586026a7e74ef8c47d396403d50eMichael Jurka    public abstract void drawTexture(BasicTexture texture, RectF source, RectF target);
103104c45677660586026a7e74ef8c47d396403d50eMichael Jurka
104104c45677660586026a7e74ef8c47d396403d50eMichael Jurka    // Draw a texture with a specified texture transform.
105104c45677660586026a7e74ef8c47d396403d50eMichael Jurka    public abstract void drawTexture(BasicTexture texture, float[] mTextureTransform,
106104c45677660586026a7e74ef8c47d396403d50eMichael Jurka                int x, int y, int w, int h);
107104c45677660586026a7e74ef8c47d396403d50eMichael Jurka
108104c45677660586026a7e74ef8c47d396403d50eMichael Jurka    // Draw two textures to the specified rectangle. The actual texture used is
109104c45677660586026a7e74ef8c47d396403d50eMichael Jurka    // from * (1 - ratio) + to * ratio
110104c45677660586026a7e74ef8c47d396403d50eMichael Jurka    // The two textures must have the same size.
111104c45677660586026a7e74ef8c47d396403d50eMichael Jurka    public abstract void drawMixed(BasicTexture from, int toColor,
112104c45677660586026a7e74ef8c47d396403d50eMichael Jurka            float ratio, int x, int y, int w, int h);
113104c45677660586026a7e74ef8c47d396403d50eMichael Jurka
114104c45677660586026a7e74ef8c47d396403d50eMichael Jurka    // Draw a region of a texture and a specified color to the specified
115104c45677660586026a7e74ef8c47d396403d50eMichael Jurka    // rectangle. The actual color used is from * (1 - ratio) + to * ratio.
116104c45677660586026a7e74ef8c47d396403d50eMichael Jurka    // The region of the texture is defined by parameter "src". The target
117104c45677660586026a7e74ef8c47d396403d50eMichael Jurka    // rectangle is specified by parameter "target".
118104c45677660586026a7e74ef8c47d396403d50eMichael Jurka    public abstract void drawMixed(BasicTexture from, int toColor,
119104c45677660586026a7e74ef8c47d396403d50eMichael Jurka            float ratio, RectF src, RectF target);
120104c45677660586026a7e74ef8c47d396403d50eMichael Jurka
121104c45677660586026a7e74ef8c47d396403d50eMichael Jurka    // Unloads the specified texture from the canvas. The resource allocated
122104c45677660586026a7e74ef8c47d396403d50eMichael Jurka    // to draw the texture will be released. The specified texture will return
123104c45677660586026a7e74ef8c47d396403d50eMichael Jurka    // to the unloaded state. This function should be called only from
124104c45677660586026a7e74ef8c47d396403d50eMichael Jurka    // BasicTexture or its descendant
125104c45677660586026a7e74ef8c47d396403d50eMichael Jurka    public abstract boolean unloadTexture(BasicTexture texture);
126104c45677660586026a7e74ef8c47d396403d50eMichael Jurka
127104c45677660586026a7e74ef8c47d396403d50eMichael Jurka    // Delete the specified buffer object, similar to unloadTexture.
128104c45677660586026a7e74ef8c47d396403d50eMichael Jurka    public abstract void deleteBuffer(int bufferId);
129104c45677660586026a7e74ef8c47d396403d50eMichael Jurka
130104c45677660586026a7e74ef8c47d396403d50eMichael Jurka    // Delete the textures and buffers in GL side. This function should only be
131104c45677660586026a7e74ef8c47d396403d50eMichael Jurka    // called in the GL thread.
132104c45677660586026a7e74ef8c47d396403d50eMichael Jurka    public abstract void deleteRecycledResources();
133104c45677660586026a7e74ef8c47d396403d50eMichael Jurka
134104c45677660586026a7e74ef8c47d396403d50eMichael Jurka    // Dump statistics information and clear the counters. For debug only.
135104c45677660586026a7e74ef8c47d396403d50eMichael Jurka    public abstract void dumpStatisticsAndClear();
136104c45677660586026a7e74ef8c47d396403d50eMichael Jurka
137104c45677660586026a7e74ef8c47d396403d50eMichael Jurka    public abstract void beginRenderTarget(RawTexture texture);
138104c45677660586026a7e74ef8c47d396403d50eMichael Jurka
139104c45677660586026a7e74ef8c47d396403d50eMichael Jurka    public abstract void endRenderTarget();
140104c45677660586026a7e74ef8c47d396403d50eMichael Jurka
141104c45677660586026a7e74ef8c47d396403d50eMichael Jurka    /**
142104c45677660586026a7e74ef8c47d396403d50eMichael Jurka     * Sets texture parameters to use GL_CLAMP_TO_EDGE for both
143104c45677660586026a7e74ef8c47d396403d50eMichael Jurka     * GL_TEXTURE_WRAP_S and GL_TEXTURE_WRAP_T. Sets texture parameters to be
144104c45677660586026a7e74ef8c47d396403d50eMichael Jurka     * GL_LINEAR for GL_TEXTURE_MIN_FILTER and GL_TEXTURE_MAG_FILTER.
145104c45677660586026a7e74ef8c47d396403d50eMichael Jurka     * bindTexture() must be called prior to this.
146104c45677660586026a7e74ef8c47d396403d50eMichael Jurka     *
147104c45677660586026a7e74ef8c47d396403d50eMichael Jurka     * @param texture The texture to set parameters on.
148104c45677660586026a7e74ef8c47d396403d50eMichael Jurka     */
149104c45677660586026a7e74ef8c47d396403d50eMichael Jurka    public abstract void setTextureParameters(BasicTexture texture);
150104c45677660586026a7e74ef8c47d396403d50eMichael Jurka
151104c45677660586026a7e74ef8c47d396403d50eMichael Jurka    /**
152104c45677660586026a7e74ef8c47d396403d50eMichael Jurka     * Initializes the texture to a size by calling texImage2D on it.
153104c45677660586026a7e74ef8c47d396403d50eMichael Jurka     *
154104c45677660586026a7e74ef8c47d396403d50eMichael Jurka     * @param texture The texture to initialize the size.
155104c45677660586026a7e74ef8c47d396403d50eMichael Jurka     * @param format The texture format (e.g. GL_RGBA)
156104c45677660586026a7e74ef8c47d396403d50eMichael Jurka     * @param type The texture type (e.g. GL_UNSIGNED_BYTE)
157104c45677660586026a7e74ef8c47d396403d50eMichael Jurka     */
158104c45677660586026a7e74ef8c47d396403d50eMichael Jurka    public abstract void initializeTextureSize(BasicTexture texture, int format, int type);
159104c45677660586026a7e74ef8c47d396403d50eMichael Jurka
160104c45677660586026a7e74ef8c47d396403d50eMichael Jurka    /**
161104c45677660586026a7e74ef8c47d396403d50eMichael Jurka     * Initializes the texture to a size by calling texImage2D on it.
162104c45677660586026a7e74ef8c47d396403d50eMichael Jurka     *
163104c45677660586026a7e74ef8c47d396403d50eMichael Jurka     * @param texture The texture to initialize the size.
164104c45677660586026a7e74ef8c47d396403d50eMichael Jurka     * @param bitmap The bitmap to initialize the bitmap with.
165104c45677660586026a7e74ef8c47d396403d50eMichael Jurka     */
166104c45677660586026a7e74ef8c47d396403d50eMichael Jurka    public abstract void initializeTexture(BasicTexture texture, Bitmap bitmap);
167104c45677660586026a7e74ef8c47d396403d50eMichael Jurka
168104c45677660586026a7e74ef8c47d396403d50eMichael Jurka    /**
169104c45677660586026a7e74ef8c47d396403d50eMichael Jurka     * Calls glTexSubImage2D to upload a bitmap to the texture.
170104c45677660586026a7e74ef8c47d396403d50eMichael Jurka     *
171104c45677660586026a7e74ef8c47d396403d50eMichael Jurka     * @param texture The target texture to write to.
172104c45677660586026a7e74ef8c47d396403d50eMichael Jurka     * @param xOffset Specifies a texel offset in the x direction within the
173104c45677660586026a7e74ef8c47d396403d50eMichael Jurka     *            texture array.
174104c45677660586026a7e74ef8c47d396403d50eMichael Jurka     * @param yOffset Specifies a texel offset in the y direction within the
175104c45677660586026a7e74ef8c47d396403d50eMichael Jurka     *            texture array.
176104c45677660586026a7e74ef8c47d396403d50eMichael Jurka     * @param format The texture format (e.g. GL_RGBA)
177104c45677660586026a7e74ef8c47d396403d50eMichael Jurka     * @param type The texture type (e.g. GL_UNSIGNED_BYTE)
178104c45677660586026a7e74ef8c47d396403d50eMichael Jurka     */
179104c45677660586026a7e74ef8c47d396403d50eMichael Jurka    public abstract void texSubImage2D(BasicTexture texture, int xOffset, int yOffset,
180104c45677660586026a7e74ef8c47d396403d50eMichael Jurka            Bitmap bitmap,
181104c45677660586026a7e74ef8c47d396403d50eMichael Jurka            int format, int type);
182104c45677660586026a7e74ef8c47d396403d50eMichael Jurka
183104c45677660586026a7e74ef8c47d396403d50eMichael Jurka    /**
184104c45677660586026a7e74ef8c47d396403d50eMichael Jurka     * Generates buffers and uploads the buffer data.
185104c45677660586026a7e74ef8c47d396403d50eMichael Jurka     *
186104c45677660586026a7e74ef8c47d396403d50eMichael Jurka     * @param buffer The buffer to upload
187104c45677660586026a7e74ef8c47d396403d50eMichael Jurka     * @return The buffer ID that was generated.
188104c45677660586026a7e74ef8c47d396403d50eMichael Jurka     */
189104c45677660586026a7e74ef8c47d396403d50eMichael Jurka    public abstract int uploadBuffer(java.nio.FloatBuffer buffer);
190104c45677660586026a7e74ef8c47d396403d50eMichael Jurka
191104c45677660586026a7e74ef8c47d396403d50eMichael Jurka    /**
192104c45677660586026a7e74ef8c47d396403d50eMichael Jurka     * Generates buffers and uploads the element array buffer data.
193104c45677660586026a7e74ef8c47d396403d50eMichael Jurka     *
194104c45677660586026a7e74ef8c47d396403d50eMichael Jurka     * @param buffer The buffer to upload
195104c45677660586026a7e74ef8c47d396403d50eMichael Jurka     * @return The buffer ID that was generated.
196104c45677660586026a7e74ef8c47d396403d50eMichael Jurka     */
197104c45677660586026a7e74ef8c47d396403d50eMichael Jurka    public abstract int uploadBuffer(java.nio.ByteBuffer buffer);
198104c45677660586026a7e74ef8c47d396403d50eMichael Jurka
199104c45677660586026a7e74ef8c47d396403d50eMichael Jurka    /**
200104c45677660586026a7e74ef8c47d396403d50eMichael Jurka     * After LightCycle makes GL calls, this method is called to restore the GL
201104c45677660586026a7e74ef8c47d396403d50eMichael Jurka     * configuration to the one expected by GLCanvas.
202104c45677660586026a7e74ef8c47d396403d50eMichael Jurka     */
203104c45677660586026a7e74ef8c47d396403d50eMichael Jurka    public abstract void recoverFromLightCycle();
204104c45677660586026a7e74ef8c47d396403d50eMichael Jurka
205104c45677660586026a7e74ef8c47d396403d50eMichael Jurka    /**
206104c45677660586026a7e74ef8c47d396403d50eMichael Jurka     * Gets the bounds given by x, y, width, and height as well as the internal
207104c45677660586026a7e74ef8c47d396403d50eMichael Jurka     * matrix state. There is no special handling for non-90-degree rotations.
208104c45677660586026a7e74ef8c47d396403d50eMichael Jurka     * It only considers the lower-left and upper-right corners as the bounds.
209104c45677660586026a7e74ef8c47d396403d50eMichael Jurka     *
210104c45677660586026a7e74ef8c47d396403d50eMichael Jurka     * @param bounds The output bounds to write to.
211104c45677660586026a7e74ef8c47d396403d50eMichael Jurka     * @param x The left side of the input rectangle.
212104c45677660586026a7e74ef8c47d396403d50eMichael Jurka     * @param y The bottom of the input rectangle.
213104c45677660586026a7e74ef8c47d396403d50eMichael Jurka     * @param width The width of the input rectangle.
214104c45677660586026a7e74ef8c47d396403d50eMichael Jurka     * @param height The height of the input rectangle.
215104c45677660586026a7e74ef8c47d396403d50eMichael Jurka     */
216104c45677660586026a7e74ef8c47d396403d50eMichael Jurka    public abstract void getBounds(Rect bounds, int x, int y, int width, int height);
217104c45677660586026a7e74ef8c47d396403d50eMichael Jurka}
218