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