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.RectF;
21 
22 //
23 // GLCanvas gives a convenient interface to draw using OpenGL.
24 //
25 // When a rectangle is specified in this interface, it means the region
26 // [x, x+width) * [y, y+height)
27 //
28 public interface GLCanvas {
29 
getGLId()30     public GLId getGLId();
31 
32     // Tells GLCanvas the size of the underlying GL surface. This should be
33     // called before first drawing and when the size of GL surface is changed.
34     // This is called by GLRoot and should not be called by the clients
35     // who only want to draw on the GLCanvas. Both width and height must be
36     // nonnegative.
setSize(int width, int height)37     public abstract void setSize(int width, int height);
38 
39     // Clear the drawing buffers. This should only be used by GLRoot.
clearBuffer()40     public abstract void clearBuffer();
41 
translate(float x, float y)42     public abstract void translate(float x, float y);
43 
rotate(float angle, float x, float y, float z)44     public abstract void rotate(float angle, float x, float y, float z);
45 
46     // Same as save(), but only save those specified in saveFlags.
save(int saveFlags)47     public abstract void save(int saveFlags);
48 
49     public static final int SAVE_FLAG_ALL = 0xFFFFFFFF;
50     public static final int SAVE_FLAG_MATRIX = 0x02;
51 
52     // Pops from the top of the stack as current configuration state (matrix,
53     // alpha, and clip). This call balances a previous call to save(), and is
54     // used to remove all modifications to the configuration state since the
55     // last save call.
restore()56     public abstract void restore();
57 
58     // Draws a texture to the specified rectangle.
drawTexture(BasicTexture texture, int x, int y, int width, int height)59     public abstract void drawTexture(BasicTexture texture, int x, int y, int width, int height);
60 
61     // Draws the source rectangle part of the texture to the target rectangle.
drawTexture(BasicTexture texture, RectF source, RectF target)62     public abstract void drawTexture(BasicTexture texture, RectF source, RectF target);
63 
64     // Unloads the specified texture from the canvas. The resource allocated
65     // to draw the texture will be released. The specified texture will return
66     // to the unloaded state. This function should be called only from
67     // BasicTexture or its descendant
unloadTexture(BasicTexture texture)68     public abstract boolean unloadTexture(BasicTexture texture);
69 
70     // Delete the textures and buffers in GL side. This function should only be
71     // called in the GL thread.
deleteRecycledResources()72     public abstract void deleteRecycledResources();
73 
74     /**
75      * Sets texture parameters to use GL_CLAMP_TO_EDGE for both
76      * GL_TEXTURE_WRAP_S and GL_TEXTURE_WRAP_T. Sets texture parameters to be
77      * GL_LINEAR for GL_TEXTURE_MIN_FILTER and GL_TEXTURE_MAG_FILTER.
78      * bindTexture() must be called prior to this.
79      *
80      * @param texture The texture to set parameters on.
81      */
setTextureParameters(BasicTexture texture)82     public abstract void setTextureParameters(BasicTexture texture);
83 
84     /**
85      * Initializes the texture to a size by calling texImage2D on it.
86      *
87      * @param texture The texture to initialize the size.
88      * @param format The texture format (e.g. GL_RGBA)
89      * @param type The texture type (e.g. GL_UNSIGNED_BYTE)
90      */
initializeTextureSize(BasicTexture texture, int format, int type)91     public abstract void initializeTextureSize(BasicTexture texture, int format, int type);
92 
93     /**
94      * Initializes the texture to a size by calling texImage2D on it.
95      *
96      * @param texture The texture to initialize the size.
97      * @param bitmap The bitmap to initialize the bitmap with.
98      */
initializeTexture(BasicTexture texture, Bitmap bitmap)99     public abstract void initializeTexture(BasicTexture texture, Bitmap bitmap);
100 
101     /**
102      * Calls glTexSubImage2D to upload a bitmap to the texture.
103      *
104      * @param texture The target texture to write to.
105      * @param xOffset Specifies a texel offset in the x direction within the
106      *            texture array.
107      * @param yOffset Specifies a texel offset in the y direction within the
108      *            texture array.
109      * @param format The texture format (e.g. GL_RGBA)
110      * @param type The texture type (e.g. GL_UNSIGNED_BYTE)
111      */
texSubImage2D(BasicTexture texture, int xOffset, int yOffset, Bitmap bitmap, int format, int type)112     public abstract void texSubImage2D(BasicTexture texture, int xOffset, int yOffset,
113             Bitmap bitmap,
114             int format, int type);
115 
116     /**
117      * Generates buffers and uploads the buffer data.
118      *
119      * @param buffer The buffer to upload
120      * @return The buffer ID that was generated.
121      */
uploadBuffer(java.nio.FloatBuffer buffer)122     public abstract int uploadBuffer(java.nio.FloatBuffer buffer);
123 }
124