Skip to content

Commit 862caa9

Browse files
committed
Add grafika's base classes
Signed-off-by: Umair Khan <[email protected]>
1 parent 4f95572 commit 862caa9

File tree

4 files changed

+824
-0
lines changed

4 files changed

+824
-0
lines changed
Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
/*
2+
* Copyright 2014 Google Inc. All rights reserved.
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 in.omerjerk.processing.video.android;
18+
19+
import java.nio.FloatBuffer;
20+
21+
/**
22+
* Base class for stuff we like to draw.
23+
*/
24+
public class Drawable2d {
25+
private static final int SIZEOF_FLOAT = 4;
26+
27+
/**
28+
* Simple equilateral triangle (1.0 per side). Centered on (0,0).
29+
*/
30+
private static final float TRIANGLE_COORDS[] = {
31+
0.0f, 0.577350269f, // 0 top
32+
-0.5f, -0.288675135f, // 1 bottom left
33+
0.5f, -0.288675135f // 2 bottom right
34+
};
35+
private static final float TRIANGLE_TEX_COORDS[] = {
36+
0.5f, 0.0f, // 0 top center
37+
0.0f, 1.0f, // 1 bottom left
38+
1.0f, 1.0f, // 2 bottom right
39+
};
40+
private static final FloatBuffer TRIANGLE_BUF =
41+
GlUtil.createFloatBuffer(TRIANGLE_COORDS);
42+
private static final FloatBuffer TRIANGLE_TEX_BUF =
43+
GlUtil.createFloatBuffer(TRIANGLE_TEX_COORDS);
44+
45+
/**
46+
* Simple square, specified as a triangle strip. The square is centered on (0,0) and has
47+
* a size of 1x1.
48+
* <p>
49+
* Triangles are 0-1-2 and 2-1-3 (counter-clockwise winding).
50+
*/
51+
private static final float RECTANGLE_COORDS[] = {
52+
-0.5f, -0.5f, // 0 bottom left
53+
0.5f, -0.5f, // 1 bottom right
54+
-0.5f, 0.5f, // 2 top left
55+
0.5f, 0.5f, // 3 top right
56+
};
57+
private static final float RECTANGLE_TEX_COORDS[] = {
58+
0.0f, 1.0f, // 0 bottom left
59+
1.0f, 1.0f, // 1 bottom right
60+
0.0f, 0.0f, // 2 top left
61+
1.0f, 0.0f // 3 top right
62+
};
63+
private static final FloatBuffer RECTANGLE_BUF =
64+
GlUtil.createFloatBuffer(RECTANGLE_COORDS);
65+
private static final FloatBuffer RECTANGLE_TEX_BUF =
66+
GlUtil.createFloatBuffer(RECTANGLE_TEX_COORDS);
67+
68+
/**
69+
* A "full" square, extending from -1 to +1 in both dimensions. When the model/view/projection
70+
* matrix is identity, this will exactly cover the viewport.
71+
* <p>
72+
* The texture coordinates are Y-inverted relative to RECTANGLE. (This seems to work out
73+
* right with external textures from SurfaceTexture.)
74+
*/
75+
private static final float FULL_RECTANGLE_COORDS[] = {
76+
-1.0f, -1.0f, // 0 bottom left
77+
1.0f, -1.0f, // 1 bottom right
78+
-1.0f, 1.0f, // 2 top left
79+
1.0f, 1.0f, // 3 top right
80+
};
81+
private static final float FULL_RECTANGLE_TEX_COORDS[] = {
82+
0.0f, 0.0f, // 0 bottom left
83+
1.0f, 0.0f, // 1 bottom right
84+
0.0f, 1.0f, // 2 top left
85+
1.0f, 1.0f // 3 top right
86+
};
87+
private static final FloatBuffer FULL_RECTANGLE_BUF =
88+
GlUtil.createFloatBuffer(FULL_RECTANGLE_COORDS);
89+
private static final FloatBuffer FULL_RECTANGLE_TEX_BUF =
90+
GlUtil.createFloatBuffer(FULL_RECTANGLE_TEX_COORDS);
91+
92+
93+
private FloatBuffer mVertexArray;
94+
private FloatBuffer mTexCoordArray;
95+
private int mVertexCount;
96+
private int mCoordsPerVertex;
97+
private int mVertexStride;
98+
private int mTexCoordStride;
99+
private Prefab mPrefab;
100+
101+
/**
102+
* Enum values for constructor.
103+
*/
104+
public enum Prefab {
105+
TRIANGLE, RECTANGLE, FULL_RECTANGLE
106+
}
107+
108+
/**
109+
* Prepares a drawable from a "pre-fabricated" shape definition.
110+
* <p>
111+
* Does no EGL/GL operations, so this can be done at any time.
112+
*/
113+
public Drawable2d(Prefab shape) {
114+
switch (shape) {
115+
case TRIANGLE:
116+
mVertexArray = TRIANGLE_BUF;
117+
mTexCoordArray = TRIANGLE_TEX_BUF;
118+
mCoordsPerVertex = 2;
119+
mVertexStride = mCoordsPerVertex * SIZEOF_FLOAT;
120+
mVertexCount = TRIANGLE_COORDS.length / mCoordsPerVertex;
121+
break;
122+
case RECTANGLE:
123+
mVertexArray = RECTANGLE_BUF;
124+
mTexCoordArray = RECTANGLE_TEX_BUF;
125+
mCoordsPerVertex = 2;
126+
mVertexStride = mCoordsPerVertex * SIZEOF_FLOAT;
127+
mVertexCount = RECTANGLE_COORDS.length / mCoordsPerVertex;
128+
break;
129+
case FULL_RECTANGLE:
130+
mVertexArray = FULL_RECTANGLE_BUF;
131+
mTexCoordArray = FULL_RECTANGLE_TEX_BUF;
132+
mCoordsPerVertex = 2;
133+
mVertexStride = mCoordsPerVertex * SIZEOF_FLOAT;
134+
mVertexCount = FULL_RECTANGLE_COORDS.length / mCoordsPerVertex;
135+
break;
136+
default:
137+
throw new RuntimeException("Unknown shape " + shape);
138+
}
139+
mTexCoordStride = 2 * SIZEOF_FLOAT;
140+
mPrefab = shape;
141+
}
142+
143+
/**
144+
* Returns the array of vertices.
145+
* <p>
146+
* To avoid allocations, this returns internal state. The caller must not modify it.
147+
*/
148+
public FloatBuffer getVertexArray() {
149+
return mVertexArray;
150+
}
151+
152+
/**
153+
* Returns the array of texture coordinates.
154+
* <p>
155+
* To avoid allocations, this returns internal state. The caller must not modify it.
156+
*/
157+
public FloatBuffer getTexCoordArray() {
158+
return mTexCoordArray;
159+
}
160+
161+
/**
162+
* Returns the number of vertices stored in the vertex array.
163+
*/
164+
public int getVertexCount() {
165+
return mVertexCount;
166+
}
167+
168+
/**
169+
* Returns the width, in bytes, of the data for each vertex.
170+
*/
171+
public int getVertexStride() {
172+
return mVertexStride;
173+
}
174+
175+
/**
176+
* Returns the width, in bytes, of the data for each texture coordinate.
177+
*/
178+
public int getTexCoordStride() {
179+
return mTexCoordStride;
180+
}
181+
182+
/**
183+
* Returns the number of position coordinates per vertex. This will be 2 or 3.
184+
*/
185+
public int getCoordsPerVertex() {
186+
return mCoordsPerVertex;
187+
}
188+
189+
@Override
190+
public String toString() {
191+
if (mPrefab != null) {
192+
return "[Drawable2d: " + mPrefab + "]";
193+
} else {
194+
return "[Drawable2d: ...]";
195+
}
196+
}
197+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
* Copyright 2014 Google Inc. All rights reserved.
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 in.omerjerk.processing.video.android;
18+
19+
/**
20+
* This class essentially represents a viewport-sized sprite that will be rendered with
21+
* a texture, usually from an external source like the camera or video decoder.
22+
*/
23+
public class FullFrameRect {
24+
private final Drawable2d mRectDrawable = new Drawable2d(Drawable2d.Prefab.FULL_RECTANGLE);
25+
private Texture2dProgram mProgram;
26+
27+
/**
28+
* Prepares the object.
29+
*
30+
* @param program The program to use. FullFrameRect takes ownership, and will release
31+
* the program when no longer needed.
32+
*/
33+
public FullFrameRect(Texture2dProgram program) {
34+
mProgram = program;
35+
}
36+
37+
/**
38+
* Releases resources.
39+
* <p>
40+
* This must be called with the appropriate EGL context current (i.e. the one that was
41+
* current when the constructor was called). If we're about to destroy the EGL context,
42+
* there's no value in having the caller make it current just to do this cleanup, so you
43+
* can pass a flag that will tell this function to skip any EGL-context-specific cleanup.
44+
*/
45+
public void release(boolean doEglCleanup) {
46+
if (mProgram != null) {
47+
if (doEglCleanup) {
48+
mProgram.release();
49+
}
50+
mProgram = null;
51+
}
52+
}
53+
54+
/**
55+
* Returns the program currently in use.
56+
*/
57+
public Texture2dProgram getProgram() {
58+
return mProgram;
59+
}
60+
61+
/**
62+
* Changes the program. The previous program will be released.
63+
* <p>
64+
* The appropriate EGL context must be current.
65+
*/
66+
public void changeProgram(Texture2dProgram program) {
67+
mProgram.release();
68+
mProgram = program;
69+
}
70+
71+
/**
72+
* Creates a texture object suitable for use with drawFrame().
73+
*/
74+
public int createTextureObject() {
75+
return mProgram.createTextureObject();
76+
}
77+
78+
/**
79+
* Draws a viewport-filling rect, texturing it with the specified texture object.
80+
*/
81+
public void drawFrame(int textureId, float[] texMatrix) {
82+
// Use the identity matrix for MVP so our 2x2 FULL_RECTANGLE covers the viewport.
83+
mProgram.draw(GlUtil.IDENTITY_MATRIX, mRectDrawable.getVertexArray(), 0,
84+
mRectDrawable.getVertexCount(), mRectDrawable.getCoordsPerVertex(),
85+
mRectDrawable.getVertexStride(),
86+
texMatrix, mRectDrawable.getTexCoordArray(), textureId,
87+
mRectDrawable.getTexCoordStride());
88+
}
89+
}

0 commit comments

Comments
 (0)