diff options
Diffstat (limited to 'libs/rs')
| -rw-r--r-- | libs/rs/java/Fall/res/raw/fall.c | 164 | ||||
| -rw-r--r-- | libs/rs/java/Fall/src/com/android/fall/rs/FallRS.java | 29 | ||||
| -rw-r--r-- | libs/rs/java/Galaxy/res/raw/galaxy.c | 61 | ||||
| -rw-r--r-- | libs/rs/java/Galaxy/src/com/android/galaxy/rs/GalaxyRS.java | 40 | ||||
| -rw-r--r-- | libs/rs/java/Galaxy/src/com/android/galaxy/rs/GalaxyView.java | 11 | ||||
| -rw-r--r-- | libs/rs/rsContext.cpp | 73 | ||||
| -rw-r--r-- | libs/rs/rsContext.h | 22 | ||||
| -rw-r--r-- | libs/rs/rsThreadIO.cpp | 10 |
8 files changed, 245 insertions, 165 deletions
diff --git a/libs/rs/java/Fall/res/raw/fall.c b/libs/rs/java/Fall/res/raw/fall.c index a67764e..f348a62 100644 --- a/libs/rs/java/Fall/res/raw/fall.c +++ b/libs/rs/java/Fall/res/raw/fall.c @@ -68,17 +68,18 @@ void dropWithStrength(int x, int y, int r, int s) { int* current = loadArrayI32(RSID_RIPPLE_MAP, index * rippleMapSize + origin); int sqr = r * r; + float invs = 1.0f / s; int h = 0; - for ( ; h < r; h++) { + for ( ; h < r; h += 1) { int sqv = h * h; int yn = origin + (y - h) * (width + 2); int yp = origin + (y + h) * (width + 2); int w = 0; - for ( ; w < r; w++) { + for ( ; w < r; w += 1) { int squ = w * w; if (squ + sqv < sqr) { - int v = -sqrtf((sqr - (squ + sqv)) << 16) / s; + int v = -sqrtf((sqr - (squ + sqv)) << 16) * invs; current[yn + x + w] = v; current[yp + x + w] = v; current[yn + x - w] = v; @@ -110,16 +111,16 @@ void updateRipples() { while (h) { int w = width; while (w) { - int droplet = ((current[-b] + current[b] + current[-a] + current[a]) >> 1) - next[0]; + int droplet = ((current[-b] + current[b] + current[-a] + current[a]) >> 1) - *next; droplet -= (droplet >> DAMP); - next[0] = droplet; - current++; - next++; - w--; + *next = droplet; + current += 1; + next += 1; + w -= 1; } current += 2; next += 2; - h--; + h -= 1; } } @@ -149,10 +150,14 @@ void generateRipples() { int *map = loadArrayI32(RSID_REFRACTION_MAP, 0); float *vertices = loadTriangleMeshVerticesF(NAMED_WaterMesh); + float fw = (float) width; + float fh = (float) height; + float fy = (1.0f / 512.0f) * (1.0f / RIPPLE_HEIGHT); + int h = height - 1; while (h >= 0) { int w = width - 1; - int wave = current[0]; + int wave = *current; int offset = h * width; while (w >= 0) { int nextWave = current[1]; @@ -169,40 +174,47 @@ void generateRipples() { v &= ~(v >> 31); if (v >= height) v = height - 1; - vertices[(offset + w) * 8 + 3] = u / (float) width; - vertices[(offset + w) * 8 + 4] = v / (float) height; + int index = (offset + w) << 3; + vertices[index + 3] = u / fw; + vertices[index + 4] = v / fh; // Update Z coordinate of the vertex - vertices[(offset + w) * 8 + 7] = (dy / 512.0f) / RIPPLE_HEIGHT; + vertices[index + 7] = dy * fy; - w--; - current++; + w -= 1; + current += 1; wave = nextWave; } - h--; + h -= 1; current += 2; } // Compute the normals for lighting int y = 0; - for ( ; y < height; y++) { + int w8 = width << 3; + for ( ; y < height; y += 1) { int x = 0; int yOffset = y * width; - for ( ; x < width; x++) { + for ( ; x < width; x += 1) { + int o = (yOffset + x) << 3; + int o1 = o + 8; + int ow = o + w8; + int ow1 = ow + 8; + // V1 - float v1x = vertices[(yOffset + x) * 8 + 5]; - float v1y = vertices[(yOffset + x) * 8 + 6]; - float v1z = vertices[(yOffset + x) * 8 + 7]; + float v1x = vertices[o + 5]; + float v1y = vertices[o + 6]; + float v1z = vertices[o + 7]; // V2 - float v2x = vertices[(yOffset + x + 1) * 8 + 5]; - float v2y = vertices[(yOffset + x + 1) * 8 + 6]; - float v2z = vertices[(yOffset + x + 1) * 8 + 7]; + float v2x = vertices[o1 + 5]; + float v2y = vertices[o1 + 6]; + float v2z = vertices[o1 + 7]; // V3 - float v3x = vertices[(yOffset + width + x) * 8 + 5]; - float v3y = vertices[(yOffset + width + x) * 8 + 6]; - float v3z = vertices[(yOffset + width + x) * 8 + 7]; + float v3x = vertices[ow + 5]; + float v3y = vertices[ow + 6]; + float v3z = vertices[ow + 7]; // N1 float n1x = v2x - v1x; @@ -220,15 +232,15 @@ void generateRipples() { float n3z = n1x * n2y - n1y * n2x; // Normalize - float len = magf3(n3x, n3y, n3z); - n3x /= len; - n3y /= len; - n3z /= len; + float len = 1.0f / magf3(n3x, n3y, n3z); + n3x *= len; + n3y *= len; + n3z *= len; // V2 - v2x = vertices[(yOffset + width + x + 1) * 8 + 5]; - v2y = vertices[(yOffset + width + x + 1) * 8 + 6]; - v2z = vertices[(yOffset + width + x + 1) * 8 + 7]; + v2x = vertices[ow1 + 5]; + v2y = vertices[ow1 + 6]; + v2z = vertices[ow1 + 7]; // N1 n1x = v2x - v1x; @@ -240,23 +252,23 @@ void generateRipples() { n2y = v3y - v1y; n2z = v3z - v1z; - // Avegare of previous normal and N1 x N2 - n3x = n3x / 2.0f + (n1y * n2z - n1z * n2y) / 2.0f; - n3y = n3y / 2.0f + (n1z * n2x - n1x * n2z) / 2.0f; - n3z = n3z / 2.0f + (n1x * n2y - n1y * n2x) / 2.0f; + // Average of previous normal and N1 x N2 + n3x = n3x * 0.5f + (n1y * n2z - n1z * n2y) * 0.5f; + n3y = n3y * 0.5f + (n1z * n2x - n1x * n2z) * 0.5f; + n3z = n3z * 0.5f + (n1x * n2y - n1y * n2x) * 0.5f; // Normalize - len = magf3(n3x, n3y, n3z); - n3x /= len; - n3y /= len; - n3z /= len; - - vertices[(yOffset + x) * 8 + 0] = n3x; - vertices[(yOffset + x) * 8 + 1] = n3y; - vertices[(yOffset + x) * 8 + 2] = -n3z; + len = 1.0f / magf3(n3x, n3y, n3z); + n3x *= len; + n3y *= len; + n3z *= len; + + vertices[o + 0] = n3x; + vertices[o + 1] = n3y; + vertices[o + 2] = -n3z; // reset Z - //vertices[(yOffset + x) * 8 + 7] = 0.0f; + //vertices[(yOffset + x) << 3 + 7] = 0.0f; } } } @@ -264,10 +276,10 @@ void generateRipples() { float averageZ(float x1, float x2, float y1, float y2, float* vertices, int meshWidth, int meshHeight, float glWidth, float glHeight) { - x1 = ((x1 + glWidth / 2.0f) / glWidth) * meshWidth; - x2 = ((x2 + glWidth / 2.0f) / glWidth) * meshWidth; - y1 = ((y1 + glHeight / 2.0f) / glHeight) * meshHeight; - y2 = ((y2 + glHeight / 2.0f) / glHeight) * meshHeight; + x1 = ((x1 + glWidth * 0.5f) / glWidth) * meshWidth; + x2 = ((x2 + glWidth * 0.5f) / glWidth) * meshWidth; + y1 = ((y1 + glHeight * 0.5f) / glHeight) * meshHeight; + y2 = ((y2 + glHeight * 0.5f) / glHeight) * meshHeight; int quadX1 = clamp(x1, 0, meshWidth); int quadX2 = clamp(x2, 0, meshWidth); @@ -278,12 +290,12 @@ float averageZ(float x1, float x2, float y1, float y2, float* vertices, int vertexCount = 0; int y = quadY1; - for ( ; y < quadY2; y++) { + for ( ; y < quadY2; y += 1) { int x = quadX1; int yOffset = y * meshWidth; - for ( ; x < quadX2; x++) { - z += vertices[(yOffset + x) * 8 + 7]; - vertexCount++; + for ( ; x < quadX2; x += 1) { + z += vertices[(yOffset + x) << 3 + 7]; + vertexCount += 1; } } @@ -319,10 +331,10 @@ void drawLeaf(int index, float* vertices, int meshWidth, int meshHeight, if (a > 0.0f) { tz = -a; } else { - z1 = averageZ(x1, x, y1, y, vertices, meshWidth, meshHeight, glWidth, glHeight); - z2 = averageZ(x, x2, y1, y, vertices, meshWidth, meshHeight, glWidth, glHeight); - z3 = averageZ(x, x2, y, y2, vertices, meshWidth, meshHeight, glWidth, glHeight); - z4 = averageZ(x1, x, y, y2, vertices, meshWidth, meshHeight, glWidth, glHeight); +// z1 = averageZ(x1, x, y1, y, vertices, meshWidth, meshHeight, glWidth, glHeight); +// z2 = averageZ(x, x2, y1, y, vertices, meshWidth, meshHeight, glWidth, glHeight); +// z3 = averageZ(x, x2, y, y2, vertices, meshWidth, meshHeight, glWidth, glHeight); +// z4 = averageZ(x1, x, y, y2, vertices, meshWidth, meshHeight, glWidth, glHeight); } x1 -= x; @@ -346,8 +358,8 @@ void drawLeaf(int index, float* vertices, int meshWidth, int meshHeight, if (a <= 0.0f) { float rippled = leafStruct[LEAF_STRUCT_RIPPLED]; if (rippled < 0.0f) { - drop(((x + glWidth / 2.0f) / glWidth) * meshWidth, - meshHeight - ((y + glHeight / 2.0f) / glHeight) * meshHeight, + drop(((x + glWidth * 0.5f) / glWidth) * meshWidth, + meshHeight - ((y + glHeight * 0.5f) / glHeight) * meshHeight, DROP_RADIUS); spin /= 4.0f; leafStruct[LEAF_STRUCT_SPIN] = spin; @@ -454,6 +466,35 @@ void drawLighting() { drawTriangleMesh(NAMED_WaterMesh); } +void drawNormals() { + int width = State_meshWidth; + int height = State_meshHeight; + + float *vertices = loadTriangleMeshVerticesF(NAMED_WaterMesh); + + bindProgramVertex(NAMED_PVSky); + bindProgramFragment(NAMED_PFLighting); + + color(1.0f, 0.0f, 0.0f, 1.0f); + + float scale = 1.0f / 10.0f; + int y = 0; + for ( ; y < height; y += 1) { + int yOffset = y * width; + int x = 0; + for ( ; x < width; x += 1) { + int offset = (yOffset + x) << 3; + float vx = vertices[offset + 5]; + float vy = vertices[offset + 6]; + float vz = vertices[offset + 7]; + float nx = vertices[offset + 0]; + float ny = vertices[offset + 1]; + float nz = vertices[offset + 2]; + drawLine(vx, vy, vz, vx + nx * scale, vy + ny * scale, vz + nz * scale); + } + } +} + int main(int index) { int dropX = Drop_dropX; if (dropX != -1) { @@ -471,6 +512,7 @@ int main(int index) { drawSky(); drawLighting(); drawLeaves(); + //drawNormals(); return 1; } diff --git a/libs/rs/java/Fall/src/com/android/fall/rs/FallRS.java b/libs/rs/java/Fall/src/com/android/fall/rs/FallRS.java index 02d4737..3b13bed 100644 --- a/libs/rs/java/Fall/src/com/android/fall/rs/FallRS.java +++ b/libs/rs/java/Fall/src/com/android/fall/rs/FallRS.java @@ -177,22 +177,37 @@ class FallRS { hResolution += 2; for (int y = 0; y <= hResolution; y++) { + final boolean shift = (y & 0x1) == 0; final float yOffset = y * quadHeight - glHeight / 2.0f - quadHeight; final float t = 1.0f - y / (float) hResolution; for (int x = 0; x <= wResolution; x++) { - rs.triangleMeshAddVertex_XYZ_ST_NORM( - -1.0f + x * quadWidth - quadWidth, yOffset, 0.0f, - x / (float) wResolution, t, - 0.0f, 0.0f, -1.0f); + if (shift) { + rs.triangleMeshAddVertex_XYZ_ST_NORM( + -1.0f + x * quadWidth - quadWidth, yOffset, 0.0f, + x / (float) wResolution, t, + 0.0f, 0.0f, -1.0f); + } else { + rs.triangleMeshAddVertex_XYZ_ST_NORM( + -1.0f + x * quadWidth - quadWidth * 0.5f, yOffset, 0.0f, + x / (float) wResolution, t, + 0.0f, 0.0f, -1.0f); + } } } for (int y = 0; y < hResolution; y++) { + final boolean shift = (y & 0x1) == 0; + final int yOffset = y * (wResolution + 1); for (int x = 0; x < wResolution; x++) { - final int index = y * (wResolution + 1) + x; + final int index = yOffset + x; final int iWR1 = index + wResolution + 1; - rs.triangleMeshAddTriangle(index, index + 1, iWR1); - rs.triangleMeshAddTriangle(index + 1, iWR1, iWR1 + 1); + if (shift) { + rs.triangleMeshAddTriangle(index, index + 1, iWR1); + rs.triangleMeshAddTriangle(index + 1, iWR1 + 1, iWR1); + } else { + rs.triangleMeshAddTriangle(index, iWR1 + 1, iWR1); + rs.triangleMeshAddTriangle(index, index + 1, iWR1 + 1); + } } } diff --git a/libs/rs/java/Galaxy/res/raw/galaxy.c b/libs/rs/java/Galaxy/res/raw/galaxy.c index 59c31a1..9ff449f 100644 --- a/libs/rs/java/Galaxy/res/raw/galaxy.c +++ b/libs/rs/java/Galaxy/res/raw/galaxy.c @@ -19,11 +19,13 @@ #define RSID_PARTICLES 1 -#define PARTICLE_STRUCT_FIELDS_COUNT 4 +#define PARTICLE_STRUCT_FIELDS_COUNT 6 #define PARTICLE_STRUCT_ANGLE 0 #define PARTICLE_STRUCT_DISTANCE 1 #define PARTICLE_STRUCT_SPEED 2 #define PARTICLE_STRUCT_RADIUS 3 +#define PARTICLE_STRUCT_S 4 +#define PARTICLE_STRUCT_T 5 #define RSID_PARTICLES_BUFFER 2 #define PARTICLE_BUFFER_COMPONENTS_COUNT 5 @@ -31,19 +33,14 @@ #define PARTICLES_TEXTURES_COUNT 2 #define ELLIPSE_RATIO 0.892f -#define ELLIPSE_TWIST 0.02333333333f void drawSpace(int width, int height) { bindTexture(NAMED_PFBackground, 0, NAMED_TSpace); drawQuadTexCoords( - 0.0f, 0.0f, 0.0f, - 0.0f, 1.0f, - width, 0.0f, 0.0f, - 2.0f, 1.0f, - width, height, 0.0f, - 2.0f, 0.0f, - 0.0f, height, 0.0f, - 0.0f, 0.0f); + 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, + width, 0.0f, 0.0f, 2.0f, 1.0f, + width, height, 0.0f, 2.0f, 0.0f, + 0.0f, height, 0.0f, 0.0f, 0.0f); } void drawLights(int width, int height) { @@ -61,39 +58,34 @@ void drawLights(int width, int height) { x + 512.0f * 1.1f, y + 512.0f, 0.0f); } -void drawParticle(float *particle, int index, float *particleBuffer, int bufferIndex, - float w, float h) { - - float distance = particle[index + PARTICLE_STRUCT_DISTANCE]; - float angle = particle[index + PARTICLE_STRUCT_ANGLE]; - float speed = particle[index + PARTICLE_STRUCT_SPEED]; - float r = particle[index + PARTICLE_STRUCT_RADIUS]; +void drawParticle(float *particle, float *particleBuffer, float w, float h) { + float distance = particle[PARTICLE_STRUCT_DISTANCE]; + float angle = particle[PARTICLE_STRUCT_ANGLE]; + float speed = particle[PARTICLE_STRUCT_SPEED]; + float r = particle[PARTICLE_STRUCT_RADIUS]; float a = angle + speed; float x = distance * sinf_fast(a); float y = distance * cosf_fast(a) * ELLIPSE_RATIO; - float z = distance * ELLIPSE_TWIST; - float s = cosf_fast(z); - float t = sinf_fast(z); + float s = particle[PARTICLE_STRUCT_S]; + float t = particle[PARTICLE_STRUCT_T]; float sX = t * x + s * y + w; float sY = s * x - t * y + h; // lower left vertex of the particle's triangle - particleBuffer[bufferIndex + 1] = sX - r; // X - particleBuffer[bufferIndex + 2] = sY + r; // Y + particleBuffer[1] = sX - r; // X + particleBuffer[2] = sY + r; // Y // lower right vertex of the particle's triangle - bufferIndex += PARTICLE_BUFFER_COMPONENTS_COUNT; - particleBuffer[bufferIndex + 1] = sX + r; // X - particleBuffer[bufferIndex + 2] = sY + r; // Y + particleBuffer[6] = sX + r; // X + particleBuffer[7] = sY + r; // Y // upper middle vertex of the particle's triangle - bufferIndex += PARTICLE_BUFFER_COMPONENTS_COUNT; - particleBuffer[bufferIndex + 1] = sX; // X - particleBuffer[bufferIndex + 2] = sY - r; // Y + particleBuffer[11] = sX; // X + particleBuffer[12] = sY - r; // Y - particle[index + PARTICLE_STRUCT_ANGLE] = a; + particle[PARTICLE_STRUCT_ANGLE] = a; } void drawParticles(int width, int height) { @@ -103,7 +95,6 @@ void drawParticles(int width, int height) { int radius = State_galaxyRadius; int particlesCount = State_particlesCount; - int count = particlesCount * PARTICLE_STRUCT_FIELDS_COUNT; float *particle = loadArrayF(RSID_PARTICLES, 0); float *particleBuffer = loadArrayF(RSID_PARTICLES_BUFFER, 0); @@ -112,11 +103,11 @@ void drawParticles(int width, int height) { float h = height * 0.5f; int i = 0; - int bufferIndex = 0; - for ( ; i < count; i += PARTICLE_STRUCT_FIELDS_COUNT) { - drawParticle(particle, i, particleBuffer, bufferIndex, w, h); - // each particle is a triangle (3 vertices) of 6 properties (ABGR, X, Y, Z, S, T) - bufferIndex += 3 * PARTICLE_BUFFER_COMPONENTS_COUNT; + for ( ; i < particlesCount; i++) { + drawParticle(particle, particleBuffer, w, h); + particle += PARTICLE_STRUCT_FIELDS_COUNT; + // each particle is a triangle (3 vertices) of 5 properties (ABGR, X, Y, S, T) + particleBuffer += 3 * PARTICLE_BUFFER_COMPONENTS_COUNT; } uploadToBufferObject(NAMED_ParticlesBuffer); diff --git a/libs/rs/java/Galaxy/src/com/android/galaxy/rs/GalaxyRS.java b/libs/rs/java/Galaxy/src/com/android/galaxy/rs/GalaxyRS.java index 717100d..c6f5816 100644 --- a/libs/rs/java/Galaxy/src/com/android/galaxy/rs/GalaxyRS.java +++ b/libs/rs/java/Galaxy/src/com/android/galaxy/rs/GalaxyRS.java @@ -42,9 +42,11 @@ import static android.util.MathUtils.*; import java.util.TimeZone; +@SuppressWarnings({"FieldCanBeLocal"}) class GalaxyRS { private static final int GALAXY_RADIUS = 300; private static final int PARTICLES_COUNT = 12000; + private static final float ELLIPSE_TWIST = 0.023333333f; private static final int RSID_STATE = 0; @@ -54,11 +56,13 @@ class GalaxyRS { private static final int RSID_TEXTURE_FLARES = 2; private static final int RSID_PARTICLES = 1; - private static final int PARTICLE_STRUCT_FIELDS_COUNT = 4; + private static final int PARTICLE_STRUCT_FIELDS_COUNT = 6; private static final int PARTICLE_STRUCT_ANGLE = 0; private static final int PARTICLE_STRUCT_DISTANCE = 1; private static final int PARTICLE_STRUCT_SPEED = 2; private static final int PARTICLE_STRUCT_RADIUS = 3; + private static final int PARTICLE_STRUCT_S = 4; + private static final int PARTICLE_STRUCT_T = 5; private static final int RSID_PARTICLES_BUFFER = 2; @@ -103,35 +107,6 @@ class GalaxyRS { initRS(); } - public void destroy() { - mScript.destroy(); - mSampler.destroy(); - mLightSampler.destroy(); - mPfBackground.destroy(); - mPfsBackground.destroy(); - mPvBackground.destroy(); - mPvOrthoAlloc.mAlloc.destroy(); - for (Allocation a : mTextures) { - a.destroy(); - } - mState.destroy(); - mPfLighting.destroy(); - mParticles.destroy(); - mPfsLights.destroy(); - mParticlesMesh.destroy(); - mParticlesBuffer.destroy(); - mStateType.destroy(); - } - - @Override - protected void finalize() throws Throwable { - try { - destroy(); - } finally { - super.finalize(); - } - } - private void initRS() { createProgramVertex(); createProgramFragmentStore(); @@ -218,13 +193,16 @@ class GalaxyRS { float d = abs(randomGauss()) * GALAXY_RADIUS / 2.0f + random(-4.0f, 4.0f); float z = randomGauss() * 0.5f * 0.8f * ((GALAXY_RADIUS - d) / (float) GALAXY_RADIUS); z += 1.0f; + float p = d * ELLIPSE_TWIST; particles[index + PARTICLE_STRUCT_ANGLE] = random(0.0f, (float) (Math.PI * 2.0)); particles[index + PARTICLE_STRUCT_DISTANCE] = d; particles[index + PARTICLE_STRUCT_SPEED] = random(0.0015f, 0.0025f) * (0.5f + (0.5f * (float) GALAXY_RADIUS / d)) * 0.7f; particles[index + PARTICLE_STRUCT_RADIUS] = z * random(1.2f, 2.1f); - + particles[index + PARTICLE_STRUCT_S] = (float) Math.cos(p); + particles[index + PARTICLE_STRUCT_T] = (float) Math.sin(p); + int red, green, blue; if (d < GALAXY_RADIUS / 3.0f) { red = (int) (220 + (d / (float) GALAXY_RADIUS) * 35); diff --git a/libs/rs/java/Galaxy/src/com/android/galaxy/rs/GalaxyView.java b/libs/rs/java/Galaxy/src/com/android/galaxy/rs/GalaxyView.java index 341293b..4f6d3f0 100644 --- a/libs/rs/java/Galaxy/src/com/android/galaxy/rs/GalaxyView.java +++ b/libs/rs/java/Galaxy/src/com/android/galaxy/rs/GalaxyView.java @@ -22,8 +22,6 @@ import android.renderscript.RenderScript; import android.renderscript.RSSurfaceView; class GalaxyView extends RSSurfaceView { - private GalaxyRS mRender; - public GalaxyView(Context context) { super(context); setFocusable(true); @@ -34,12 +32,7 @@ class GalaxyView extends RSSurfaceView { super.surfaceChanged(holder, format, w, h); RenderScript RS = createRenderScript(); - mRender = new GalaxyRS(w, h); - mRender.init(RS, getResources()); - } - - @Override - public void surfaceDestroyed(SurfaceHolder holder) { - if (mRender != null) mRender.destroy(); + GalaxyRS render = new GalaxyRS(w, h); + render.init(RS, getResources()); } } diff --git a/libs/rs/rsContext.cpp b/libs/rs/rsContext.cpp index 7bfa81e..413caab 100644 --- a/libs/rs/rsContext.cpp +++ b/libs/rs/rsContext.cpp @@ -25,7 +25,6 @@ using namespace android; using namespace android::renderscript; -Context * Context::gCon = NULL; pthread_key_t Context::gThreadTLSKey = 0; void Context::initEGL() @@ -84,6 +83,9 @@ bool Context::runScript(Script *s, uint32_t launchID) bool Context::runRootScript() { +#if RS_LOG_TIMES + timerSet(RS_TIMER_CLEAR_SWAP); +#endif rsAssert(mRootScript->mEnviroment.mIsRoot); //glColor4f(1,1,1,1); @@ -102,22 +104,53 @@ bool Context::runRootScript() glClear(GL_DEPTH_BUFFER_BIT); #if RS_LOG_TIMES - struct timespec startTime; - clock_gettime(CLOCK_MONOTONIC, &startTime); + timerSet(RS_TIMER_SCRIPT); #endif bool ret = runScript(mRootScript.get(), 0); + return ret; +} -#if RS_LOG_TIMES - struct timespec endTime; - clock_gettime(CLOCK_MONOTONIC, &endTime); +uint64_t Context::getTime() const +{ + struct timespec t; + clock_gettime(CLOCK_MONOTONIC, &t); + return t.tv_nsec + ((uint64_t)t.tv_sec * 1000 * 1000 * 1000); +} - uint64_t t1 = endTime.tv_nsec + ((uint64_t)endTime.tv_sec * 1000 * 1000 * 1000); - uint64_t t2 = startTime.tv_nsec + ((uint64_t)startTime.tv_sec * 1000 * 1000 * 1000); - int t3 = (int)((t1 - t2) / 1000 / 1000); - LOGE("times %i", t3); -#endif +void Context::timerReset() +{ + for (int ct=0; ct < _RS_TIMER_TOTAL; ct++) { + mTimers[ct] = 0; + } +} - return ret; +void Context::timerInit() +{ + mTimeLast = getTime(); + mTimerActive = RS_TIMER_INTERNAL; + timerReset(); +} + +void Context::timerSet(Timers tm) +{ + uint64_t last = mTimeLast; + mTimeLast = getTime(); + mTimers[mTimerActive] += mTimeLast - last; + mTimerActive = tm; +} + +void Context::timerPrint() +{ + double total = 0; + for (int ct = 0; ct < _RS_TIMER_TOTAL; ct++) { + total += mTimers[ct]; + } + + LOGV("RS Time Data: Idle %2.1f (%lli), Internal %2.1f (%lli), Script %2.1f (%lli), Clear & Swap %2.1f (%lli)", + 100.0 * mTimers[RS_TIMER_IDLE] / total, mTimers[RS_TIMER_IDLE] / 1000000, + 100.0 * mTimers[RS_TIMER_INTERNAL] / total, mTimers[RS_TIMER_INTERNAL] / 1000000, + 100.0 * mTimers[RS_TIMER_SCRIPT] / total, mTimers[RS_TIMER_SCRIPT] / 1000000, + 100.0 * mTimers[RS_TIMER_CLEAR_SWAP] / total, mTimers[RS_TIMER_CLEAR_SWAP] / 1000000); } void Context::setupCheck() @@ -168,9 +201,19 @@ void * Context::threadProc(void *vrsc) if (mDraw) { mDraw = rsc->runRootScript(); +#if RS_LOG_TIMES + rsc->timerSet(RS_TIMER_CLEAR_SWAP); +#endif eglSwapBuffers(rsc->mDisplay, rsc->mSurface); +#if RS_LOG_TIMES + rsc->timerSet(RS_TIMER_INTERNAL); + rsc->timerPrint(); + rsc->timerReset(); +#endif + } + if (rsc->mObjDestroy.mNeedToEmpty) { + rsc->objDestroyOOBRun(); } - rsc->objDestroyOOBRun(); } glClearColor(0,0,0,0); @@ -188,9 +231,6 @@ Context::Context(Device *dev, Surface *sur) mRunning = false; mExit = false; - // see comment in header - gCon = this; - int status; pthread_attr_t threadAttr; @@ -213,6 +253,7 @@ Context::Context(Device *dev, Surface *sur) mWndSurface = sur; objDestroyOOBInit(); + timerInit(); LOGV("RS Launching thread"); status = pthread_create(&mThreadId, &threadAttr, threadProc, this); diff --git a/libs/rs/rsContext.h b/libs/rs/rsContext.h index 52901b2..ca67e40 100644 --- a/libs/rs/rsContext.h +++ b/libs/rs/rsContext.h @@ -72,8 +72,6 @@ public: ScriptCState mScriptC; - static Context * getContext() {return gCon;} - void swapBuffers(); void setRootScript(Script *); void setVertex(ProgramVertex *); @@ -120,6 +118,20 @@ public: ThreadIO mIO; void objDestroyAdd(ObjectBase *); + // Timers + enum Timers { + RS_TIMER_IDLE, + RS_TIMER_INTERNAL, + RS_TIMER_SCRIPT, + RS_TIMER_CLEAR_SWAP, + _RS_TIMER_TOTAL + }; + uint64_t getTime() const; + void timerInit(); + void timerReset(); + void timerSet(Timers); + void timerPrint(); + protected: Device *mDev; @@ -164,13 +176,15 @@ private: static void * threadProc(void *); - // todo: put in TLS - static Context *gCon; Surface *mWndSurface; Vector<ObjectBase *> mNames; KeyedVector<String8,int> mInt32Defines; KeyedVector<String8,float> mFloatDefines; + + uint64_t mTimers[_RS_TIMER_TOTAL]; + Timers mTimerActive; + uint64_t mTimeLast; }; diff --git a/libs/rs/rsThreadIO.cpp b/libs/rs/rsThreadIO.cpp index 20b0a94..4a1dbbb 100644 --- a/libs/rs/rsThreadIO.cpp +++ b/libs/rs/rsThreadIO.cpp @@ -32,12 +32,18 @@ ThreadIO::~ThreadIO() bool ThreadIO::playCoreCommands(Context *con, bool waitForCommand) { - uint32_t cmdID = 0; - uint32_t cmdSize = 0; bool ret = false; while(!mToCore.isEmpty() || waitForCommand) { + uint32_t cmdID = 0; + uint32_t cmdSize = 0; ret = true; +#if RS_LOG_TIMES + con->timerSet(Context::RS_TIMER_IDLE); +#endif const void * data = mToCore.get(&cmdID, &cmdSize); +#if RS_LOG_TIMES + con->timerSet(Context::RS_TIMER_INTERNAL); +#endif waitForCommand = false; //LOGV("playCoreCommands 3 %i %i", cmdID, cmdSize); |
