1f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka/*
2f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka * Copyright (C) 2010 The Android Open Source Project
3f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka *
4f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka * Licensed under the Apache License, Version 2.0 (the "License");
5f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka * you may not use this file except in compliance with the License.
6f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka * You may obtain a copy of the License at
7f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka *
8f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka *      http://www.apache.org/licenses/LICENSE-2.0
9f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka *
10f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka * Unless required by applicable law or agreed to in writing, software
11f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka * distributed under the License is distributed on an "AS IS" BASIS,
12f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka * See the License for the specific language governing permissions and
14f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka * limitations under the License.
15f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka */
16f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka
17f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurkapackage com.android.gallery3d.glrenderer;
18f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka
19f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurkaimport android.graphics.Bitmap;
20f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurkaimport android.graphics.RectF;
21f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka
22f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka//
23f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka// GLCanvas gives a convenient interface to draw using OpenGL.
24f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka//
25f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka// When a rectangle is specified in this interface, it means the region
26f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka// [x, x+width) * [y, y+height)
27f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka//
28f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurkapublic interface GLCanvas {
29f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka
30f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka    public GLId getGLId();
31f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka
32f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka    // Tells GLCanvas the size of the underlying GL surface. This should be
33f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka    // called before first drawing and when the size of GL surface is changed.
34f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka    // This is called by GLRoot and should not be called by the clients
35f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka    // who only want to draw on the GLCanvas. Both width and height must be
36f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka    // nonnegative.
37f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka    public abstract void setSize(int width, int height);
38f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka
39f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka    // Clear the drawing buffers. This should only be used by GLRoot.
40f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka    public abstract void clearBuffer();
41f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka
42f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka    public abstract void translate(float x, float y);
43f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka
44f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka    public abstract void rotate(float angle, float x, float y, float z);
45f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka
46f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka    // Same as save(), but only save those specified in saveFlags.
47f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka    public abstract void save(int saveFlags);
48f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka
49f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka    public static final int SAVE_FLAG_ALL = 0xFFFFFFFF;
50f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka    public static final int SAVE_FLAG_MATRIX = 0x02;
51f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka
52f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka    // Pops from the top of the stack as current configuration state (matrix,
53f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka    // alpha, and clip). This call balances a previous call to save(), and is
54f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka    // used to remove all modifications to the configuration state since the
55f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka    // last save call.
56f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka    public abstract void restore();
57f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka
58f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka    // Draws a texture to the specified rectangle.
592c7071f9393741acb281a0e67a6381e1c4905cbcSunny Goyal    public abstract void drawTexture(BasicTexture texture, int x, int y, int width, int height);
60f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka
61f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka    // Draws the source rectangle part of the texture to the target rectangle.
62f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka    public abstract void drawTexture(BasicTexture texture, RectF source, RectF target);
63f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka
64f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka    // Unloads the specified texture from the canvas. The resource allocated
65f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka    // to draw the texture will be released. The specified texture will return
66f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka    // to the unloaded state. This function should be called only from
67f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka    // BasicTexture or its descendant
68f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka    public abstract boolean unloadTexture(BasicTexture texture);
69f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka
70f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka    // Delete the textures and buffers in GL side. This function should only be
71f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka    // called in the GL thread.
72f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka    public abstract void deleteRecycledResources();
73f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka
74f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka    /**
75f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka     * Sets texture parameters to use GL_CLAMP_TO_EDGE for both
76f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka     * GL_TEXTURE_WRAP_S and GL_TEXTURE_WRAP_T. Sets texture parameters to be
77f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka     * GL_LINEAR for GL_TEXTURE_MIN_FILTER and GL_TEXTURE_MAG_FILTER.
78f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka     * bindTexture() must be called prior to this.
79f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka     *
80f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka     * @param texture The texture to set parameters on.
81f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka     */
82f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka    public abstract void setTextureParameters(BasicTexture texture);
83f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka
84f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka    /**
85f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka     * Initializes the texture to a size by calling texImage2D on it.
86f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka     *
87f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka     * @param texture The texture to initialize the size.
88f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka     * @param format The texture format (e.g. GL_RGBA)
89f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka     * @param type The texture type (e.g. GL_UNSIGNED_BYTE)
90f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka     */
91f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka    public abstract void initializeTextureSize(BasicTexture texture, int format, int type);
92f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka
93f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka    /**
94f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka     * Initializes the texture to a size by calling texImage2D on it.
95f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka     *
96f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka     * @param texture The texture to initialize the size.
97f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka     * @param bitmap The bitmap to initialize the bitmap with.
98f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka     */
99f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka    public abstract void initializeTexture(BasicTexture texture, Bitmap bitmap);
100f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka
101f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka    /**
102f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka     * Calls glTexSubImage2D to upload a bitmap to the texture.
103f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka     *
104f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka     * @param texture The target texture to write to.
105f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka     * @param xOffset Specifies a texel offset in the x direction within the
106f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka     *            texture array.
107f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka     * @param yOffset Specifies a texel offset in the y direction within the
108f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka     *            texture array.
109f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka     * @param format The texture format (e.g. GL_RGBA)
110f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka     * @param type The texture type (e.g. GL_UNSIGNED_BYTE)
111f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka     */
112f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka    public abstract void texSubImage2D(BasicTexture texture, int xOffset, int yOffset,
113f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka            Bitmap bitmap,
114f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka            int format, int type);
115f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka
116f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka    /**
117f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka     * Generates buffers and uploads the buffer data.
118f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka     *
119f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka     * @param buffer The buffer to upload
120f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka     * @return The buffer ID that was generated.
121f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka     */
122f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka    public abstract int uploadBuffer(java.nio.FloatBuffer buffer);
123f1b220be2b401f203cfb490f2e78af32700ef4bbMichael Jurka}
124