AI 148797: Back-port the new version of the Grid.java from Donut.
This new version uses "direct" java.nio.Buffer objects, which avoids a potential crash that may occur when "indirect" java.nio.Buffer objects are used. BUG=1849855 Automated import of CL 148797
This commit is contained in:
committed by
The Android Open Source Project
parent
0355976f62
commit
24a8fe82df
@@ -16,6 +16,8 @@
|
|||||||
|
|
||||||
package com.example.android.apis.graphics.spritetext;
|
package com.example.android.apis.graphics.spritetext;
|
||||||
|
|
||||||
|
import java.nio.ByteBuffer;
|
||||||
|
import java.nio.ByteOrder;
|
||||||
import java.nio.CharBuffer;
|
import java.nio.CharBuffer;
|
||||||
import java.nio.FloatBuffer;
|
import java.nio.FloatBuffer;
|
||||||
|
|
||||||
@@ -27,115 +29,112 @@ import javax.microedition.khronos.opengles.GL10;
|
|||||||
*/
|
*/
|
||||||
class Grid {
|
class Grid {
|
||||||
|
|
||||||
public Grid(int w, int h) {
|
public Grid(int w, int h) {
|
||||||
if (w < 0 || w >= 65536) {
|
if (w < 0 || w >= 65536) {
|
||||||
throw new IllegalArgumentException("w");
|
throw new IllegalArgumentException("w");
|
||||||
}
|
}
|
||||||
if (h < 0 || h >= 65536) {
|
if (h < 0 || h >= 65536) {
|
||||||
throw new IllegalArgumentException("h");
|
throw new IllegalArgumentException("h");
|
||||||
}
|
}
|
||||||
if (w * h >= 65536) {
|
if (w * h >= 65536) {
|
||||||
throw new IllegalArgumentException("w * h >= 65536");
|
throw new IllegalArgumentException("w * h >= 65536");
|
||||||
}
|
}
|
||||||
|
|
||||||
mW = w;
|
mW = w;
|
||||||
mH = h;
|
mH = h;
|
||||||
int size = w * h;
|
int size = w * h;
|
||||||
mVertexArray = new float[size * 3];
|
final int FLOAT_SIZE = 4;
|
||||||
mVertexBuffer = FloatBuffer.wrap(mVertexArray);
|
final int CHAR_SIZE = 2;
|
||||||
|
mVertexBuffer = ByteBuffer.allocateDirect(FLOAT_SIZE * size * 3)
|
||||||
|
.order(ByteOrder.nativeOrder()).asFloatBuffer();
|
||||||
|
mTexCoordBuffer = ByteBuffer.allocateDirect(FLOAT_SIZE * size * 2)
|
||||||
|
.order(ByteOrder.nativeOrder()).asFloatBuffer();
|
||||||
|
|
||||||
mTexCoordArray = new float[size * 2];
|
int quadW = mW - 1;
|
||||||
mTexCoordBuffer = FloatBuffer.wrap(mTexCoordArray);
|
int quadH = mH - 1;
|
||||||
|
int quadCount = quadW * quadH;
|
||||||
|
int indexCount = quadCount * 6;
|
||||||
|
mIndexCount = indexCount;
|
||||||
|
mIndexBuffer = ByteBuffer.allocateDirect(CHAR_SIZE * indexCount)
|
||||||
|
.order(ByteOrder.nativeOrder()).asCharBuffer();
|
||||||
|
|
||||||
int quadW = mW - 1;
|
/*
|
||||||
int quadH = mH - 1;
|
* Initialize triangle list mesh.
|
||||||
int quadCount = quadW * quadH;
|
*
|
||||||
int indexCount = quadCount * 6;
|
* [0]-----[ 1] ...
|
||||||
mIndexCount = indexCount;
|
* | / |
|
||||||
char[] indexArray = new char[indexCount];
|
* | / |
|
||||||
|
* | / |
|
||||||
|
* [w]-----[w+1] ...
|
||||||
|
* | |
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
/*
|
{
|
||||||
* Initialize triangle list mesh.
|
int i = 0;
|
||||||
*
|
for (int y = 0; y < quadH; y++) {
|
||||||
* [0]-----[ 1] ...
|
for (int x = 0; x < quadW; x++) {
|
||||||
* | / |
|
char a = (char) (y * mW + x);
|
||||||
* | / |
|
char b = (char) (y * mW + x + 1);
|
||||||
* | / |
|
char c = (char) ((y + 1) * mW + x);
|
||||||
* [w]-----[w+1] ...
|
char d = (char) ((y + 1) * mW + x + 1);
|
||||||
* | |
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
|
|
||||||
{
|
mIndexBuffer.put(i++, a);
|
||||||
int i = 0;
|
mIndexBuffer.put(i++, b);
|
||||||
for (int y = 0; y < quadH; y++) {
|
mIndexBuffer.put(i++, c);
|
||||||
for (int x = 0; x < quadW; x++) {
|
|
||||||
char a = (char) (y * mW + x);
|
|
||||||
char b = (char) (y * mW + x + 1);
|
|
||||||
char c = (char) ((y + 1) * mW + x);
|
|
||||||
char d = (char) ((y + 1) * mW + x + 1);
|
|
||||||
|
|
||||||
indexArray[i++] = a;
|
mIndexBuffer.put(i++, b);
|
||||||
indexArray[i++] = b;
|
mIndexBuffer.put(i++, c);
|
||||||
indexArray[i++] = c;
|
mIndexBuffer.put(i++, d);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
indexArray[i++] = b;
|
}
|
||||||
indexArray[i++] = c;
|
|
||||||
indexArray[i++] = d;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
mIndexBuffer = CharBuffer.wrap(indexArray);
|
void set(int i, int j, float x, float y, float z, float u, float v) {
|
||||||
}
|
if (i < 0 || i >= mW) {
|
||||||
|
throw new IllegalArgumentException("i");
|
||||||
|
}
|
||||||
|
if (j < 0 || j >= mH) {
|
||||||
|
throw new IllegalArgumentException("j");
|
||||||
|
}
|
||||||
|
|
||||||
void set(int i, int j, float x, float y, float z, float u, float v) {
|
int index = mW * j + i;
|
||||||
if (i < 0 || i >= mW) {
|
|
||||||
throw new IllegalArgumentException("i");
|
|
||||||
}
|
|
||||||
if (j < 0 || j >= mH) {
|
|
||||||
throw new IllegalArgumentException("j");
|
|
||||||
}
|
|
||||||
|
|
||||||
int index = mW * j + i;
|
int posIndex = index * 3;
|
||||||
|
mVertexBuffer.put(posIndex, x);
|
||||||
|
mVertexBuffer.put(posIndex + 1, y);
|
||||||
|
mVertexBuffer.put(posIndex + 2, z);
|
||||||
|
|
||||||
int posIndex = index * 3;
|
int texIndex = index * 2;
|
||||||
mVertexArray[posIndex] = x;
|
mTexCoordBuffer.put(texIndex, u);
|
||||||
mVertexArray[posIndex + 1] = y;
|
mTexCoordBuffer.put(texIndex + 1, v);
|
||||||
mVertexArray[posIndex + 2] = z;
|
}
|
||||||
|
|
||||||
int texIndex = index * 2;
|
public void draw(GL10 gl, boolean useTexture) {
|
||||||
mTexCoordArray[texIndex] = u;
|
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
|
||||||
mTexCoordArray[texIndex + 1] = v;
|
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, mVertexBuffer);
|
||||||
}
|
|
||||||
|
|
||||||
public void draw(GL10 gl, boolean useTexture) {
|
if (useTexture) {
|
||||||
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
|
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
|
||||||
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, mVertexBuffer);
|
gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, mTexCoordBuffer);
|
||||||
|
gl.glEnable(GL10.GL_TEXTURE_2D);
|
||||||
|
} else {
|
||||||
|
gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
|
||||||
|
gl.glDisable(GL10.GL_TEXTURE_2D);
|
||||||
|
}
|
||||||
|
|
||||||
if (useTexture) {
|
gl.glDrawElements(GL10.GL_TRIANGLES, mIndexCount,
|
||||||
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
|
GL10.GL_UNSIGNED_SHORT, mIndexBuffer);
|
||||||
gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, mTexCoordBuffer);
|
|
||||||
gl.glEnable(GL10.GL_TEXTURE_2D);
|
|
||||||
} else {
|
|
||||||
gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
|
|
||||||
gl.glDisable(GL10.GL_TEXTURE_2D);
|
|
||||||
}
|
|
||||||
|
|
||||||
gl.glDrawElements(GL10.GL_TRIANGLES, mIndexCount,
|
|
||||||
GL10.GL_UNSIGNED_SHORT, mIndexBuffer);
|
|
||||||
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
|
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
|
||||||
}
|
}
|
||||||
|
|
||||||
private FloatBuffer mVertexBuffer;
|
private FloatBuffer mVertexBuffer;
|
||||||
private float[] mVertexArray;
|
private FloatBuffer mTexCoordBuffer;
|
||||||
|
private CharBuffer mIndexBuffer;
|
||||||
|
|
||||||
private FloatBuffer mTexCoordBuffer;
|
private int mW;
|
||||||
private float[] mTexCoordArray;
|
private int mH;
|
||||||
|
private int mIndexCount;
|
||||||
private CharBuffer mIndexBuffer;
|
|
||||||
|
|
||||||
private int mW;
|
|
||||||
private int mH;
|
|
||||||
private int mIndexCount;
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user