1/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.gallery3d.glrenderer;
18
19import android.graphics.Bitmap;
20import android.graphics.Rect;
21import android.graphics.RectF;
22
23//
24// GLCanvas gives a convenient interface to draw using OpenGL.
25//
26// When a rectangle is specified in this interface, it means the region
27// [x, x+width) * [y, y+height)
28//
29public interface GLCanvas {
30
31    public GLId getGLId();
32
33    // Tells GLCanvas the size of the underlying GL surface. This should be
34    // called before first drawing and when the size of GL surface is changed.
35    // This is called by GLRoot and should not be called by the clients
36    // who only want to draw on the GLCanvas. Both width and height must be
37    // nonnegative.
38    public abstract void setSize(int width, int height);
39
40    // Clear the drawing buffers. This should only be used by GLRoot.
41    public abstract void clearBuffer();
42
43    public abstract void clearBuffer(float[] argb);
44
45    // Sets and gets the current alpha, alpha must be in [0, 1].
46    public abstract void setAlpha(float alpha);
47
48    public abstract float getAlpha();
49
50    // (current alpha) = (current alpha) * alpha
51    public abstract void multiplyAlpha(float alpha);
52
53    // Change the current transform matrix.
54    public abstract void translate(float x, float y, float z);
55
56    public abstract void translate(float x, float y);
57
58    public abstract void scale(float sx, float sy, float sz);
59
60    public abstract void rotate(float angle, float x, float y, float z);
61
62    public abstract void multiplyMatrix(float[] mMatrix, int offset);
63
64    // Pushes the configuration state (matrix, and alpha) onto
65    // a private stack.
66    public abstract void save();
67
68    // Same as save(), but only save those specified in saveFlags.
69    public abstract void save(int saveFlags);
70
71    public static final int SAVE_FLAG_ALL = 0xFFFFFFFF;
72    public static final int SAVE_FLAG_ALPHA = 0x01;
73    public static final int SAVE_FLAG_MATRIX = 0x02;
74
75    // Pops from the top of the stack as current configuration state (matrix,
76    // alpha, and clip). This call balances a previous call to save(), and is
77    // used to remove all modifications to the configuration state since the
78    // last save call.
79    public abstract void restore();
80
81    // Draws a line using the specified paint from (x1, y1) to (x2, y2).
82    // (Both end points are included).
83    public abstract void drawLine(float x1, float y1, float x2, float y2, GLPaint paint);
84
85    // Draws a rectangle using the specified paint from (x1, y1) to (x2, y2).
86    // (Both end points are included).
87    public abstract void drawRect(float x1, float y1, float x2, float y2, GLPaint paint);
88
89    // Fills the specified rectangle with the specified color.
90    public abstract void fillRect(float x, float y, float width, float height, int color);
91
92    // Draws a texture to the specified rectangle.
93    public abstract void drawTexture(
94            BasicTexture texture, int x, int y, int width, int height);
95
96    public abstract void drawMesh(BasicTexture tex, int x, int y, int xyBuffer,
97            int uvBuffer, int indexBuffer, int indexCount);
98
99    // Draws the source rectangle part of the texture to the target rectangle.
100    public abstract void drawTexture(BasicTexture texture, RectF source, RectF target);
101
102    // Draw a texture with a specified texture transform.
103    public abstract void drawTexture(BasicTexture texture, float[] mTextureTransform,
104                int x, int y, int w, int h);
105
106    // Draw two textures to the specified rectangle. The actual texture used is
107    // from * (1 - ratio) + to * ratio
108    // The two textures must have the same size.
109    public abstract void drawMixed(BasicTexture from, int toColor,
110            float ratio, int x, int y, int w, int h);
111
112    // Draw a region of a texture and a specified color to the specified
113    // rectangle. The actual color used is from * (1 - ratio) + to * ratio.
114    // The region of the texture is defined by parameter "src". The target
115    // rectangle is specified by parameter "target".
116    public abstract void drawMixed(BasicTexture from, int toColor,
117            float ratio, RectF src, RectF target);
118
119    // Unloads the specified texture from the canvas. The resource allocated
120    // to draw the texture will be released. The specified texture will return
121    // to the unloaded state. This function should be called only from
122    // BasicTexture or its descendant
123    public abstract boolean unloadTexture(BasicTexture texture);
124
125    // Delete the specified buffer object, similar to unloadTexture.
126    public abstract void deleteBuffer(int bufferId);
127
128    // Delete the textures and buffers in GL side. This function should only be
129    // called in the GL thread.
130    public abstract void deleteRecycledResources();
131
132    // Dump statistics information and clear the counters. For debug only.
133    public abstract void dumpStatisticsAndClear();
134
135    public abstract void beginRenderTarget(RawTexture texture);
136
137    public abstract void endRenderTarget();
138
139    /**
140     * Sets texture parameters to use GL_CLAMP_TO_EDGE for both
141     * GL_TEXTURE_WRAP_S and GL_TEXTURE_WRAP_T. Sets texture parameters to be
142     * GL_LINEAR for GL_TEXTURE_MIN_FILTER and GL_TEXTURE_MAG_FILTER.
143     * bindTexture() must be called prior to this.
144     *
145     * @param texture The texture to set parameters on.
146     */
147    public abstract void setTextureParameters(BasicTexture texture);
148
149    /**
150     * Initializes the texture to a size by calling texImage2D on it.
151     *
152     * @param texture The texture to initialize the size.
153     * @param format The texture format (e.g. GL_RGBA)
154     * @param type The texture type (e.g. GL_UNSIGNED_BYTE)
155     */
156    public abstract void initializeTextureSize(BasicTexture texture, int format, int type);
157
158    /**
159     * Initializes the texture to a size by calling texImage2D on it.
160     *
161     * @param texture The texture to initialize the size.
162     * @param bitmap The bitmap to initialize the bitmap with.
163     */
164    public abstract void initializeTexture(BasicTexture texture, Bitmap bitmap);
165
166    /**
167     * Calls glTexSubImage2D to upload a bitmap to the texture.
168     *
169     * @param texture The target texture to write to.
170     * @param xOffset Specifies a texel offset in the x direction within the
171     *            texture array.
172     * @param yOffset Specifies a texel offset in the y direction within the
173     *            texture array.
174     * @param format The texture format (e.g. GL_RGBA)
175     * @param type The texture type (e.g. GL_UNSIGNED_BYTE)
176     */
177    public abstract void texSubImage2D(BasicTexture texture, int xOffset, int yOffset,
178            Bitmap bitmap,
179            int format, int type);
180
181    /**
182     * Generates buffers and uploads the buffer data.
183     *
184     * @param buffer The buffer to upload
185     * @return The buffer ID that was generated.
186     */
187    public abstract int uploadBuffer(java.nio.FloatBuffer buffer);
188
189    /**
190     * Generates buffers and uploads the element array buffer data.
191     *
192     * @param buffer The buffer to upload
193     * @return The buffer ID that was generated.
194     */
195    public abstract int uploadBuffer(java.nio.ByteBuffer buffer);
196
197    /**
198     * After LightCycle makes GL calls, this method is called to restore the GL
199     * configuration to the one expected by GLCanvas.
200     */
201    public abstract void recoverFromLightCycle();
202
203    /**
204     * Gets the bounds given by x, y, width, and height as well as the internal
205     * matrix state. There is no special handling for non-90-degree rotations.
206     * It only considers the lower-left and upper-right corners as the bounds.
207     *
208     * @param bounds The output bounds to write to.
209     * @param x The left side of the input rectangle.
210     * @param y The bottom of the input rectangle.
211     * @param width The width of the input rectangle.
212     * @param height The height of the input rectangle.
213     */
214    public abstract void getBounds(Rect bounds, int x, int y, int width, int height);
215}
216