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