OSDN Git Service

Provide better support for orientation change. Very much work in progress still.
authorRomain Guy <romainguy@android.com>
Tue, 25 Aug 2009 01:40:21 +0000 (18:40 -0700)
committerRomain Guy <romainguy@android.com>
Tue, 25 Aug 2009 01:40:21 +0000 (18:40 -0700)
Change-Id: I73cdd43d25ccbb200d0fd91f70202a80d0e90d77

res/raw/galaxy.rs [moved from res/raw/galaxy.c with 95% similarity]
res/raw/grass.rs [moved from res/raw/grass.c with 100% similarity]
src/com/android/wallpaper/galaxy/GalaxyRS.java
src/com/android/wallpaper/galaxy/GalaxyWallpaper.java
src/com/android/wallpaper/grass/GrassRS.java
src/com/android/wallpaper/grass/GrassWallpaper.java

similarity index 95%
rename from res/raw/galaxy.c
rename to res/raw/galaxy.rs
index 55d822e..2aec4de 100644 (file)
 void drawSpace(float xOffset, int width, int height) {
     bindTexture(NAMED_PFBackground, 0, NAMED_TSpace);
     drawQuadTexCoords(
-            xOffset, 0.0f, 0.0f, 0.0f, 1.0f,
-            xOffset + width, 0.0f, 0.0f, 2.0f, 1.0f,
-            xOffset + width, height, 0.0f, 2.0f, 0.0f,
-            xOffset, 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(float xOffset, int width, int height) {
similarity index 100%
rename from res/raw/grass.c
rename to res/raw/grass.rs
index 9b53514..93d9143 100644 (file)
@@ -72,8 +72,8 @@ class GalaxyRS {
 
     private final BitmapFactory.Options mOptionsARGB = new BitmapFactory.Options();
 
-    private final int mWidth;
-    private final int mHeight;
+    private int mWidth;
+    private int mHeight;
 
     @SuppressWarnings({"FieldCanBeLocal"})
     private ScriptC mScript;
@@ -120,6 +120,14 @@ class GalaxyRS {
         initRS();
     }
 
+    void stop() {
+        mRS.contextBindRootScript(null);
+    }
+
+    void start() {
+        mRS.contextBindRootScript(mScript);
+    }
+
     private void initRS() {
         createProgramVertex();
         createProgramFragmentStore();
@@ -133,14 +141,12 @@ class GalaxyRS {
         sb.setRoot(true);
 
         mScript = sb.create();
-        mScript.setClearColor(0.0f, 0.0f, 0.0f, 1.0f);
+        mScript.setClearColor(1.0f, 0.0f, 0.0f, 1.0f);
         mScript.setTimeZone(TimeZone.getDefault().getID());
 
         mScript.bindAllocation(mState, RSID_STATE);
         mScript.bindAllocation(mParticles, RSID_PARTICLES);
         mScript.bindAllocation(mParticlesBuffer, RSID_PARTICLES_BUFFER);
-
-        mRS.contextBindRootScript(mScript);
     }
 
     private void createScriptStructures() {
@@ -170,7 +176,18 @@ class GalaxyRS {
     void setOffsetX(float xOffset) {
         mGalaxyState.xOffset = xOffset;
         mState.data(mGalaxyState);
-    }    
+    }
+
+    void resize(int width, int height) {
+        mWidth = width;
+        mHeight = height;
+
+        mGalaxyState.width = width;
+        mGalaxyState.height = height;
+        mState.data(mGalaxyState);
+
+        mPvOrthoAlloc.setupOrthoWindow(mWidth, mHeight);
+    }
 
     static class GalaxyState {
         public int width;
@@ -344,8 +361,7 @@ class GalaxyRS {
 
     private void createProgramVertex() {
         mPvOrthoAlloc = new ProgramVertex.MatrixAllocation(mRS);
-        //mPvOrthoAlloc.setupProjectionNormalized(mWidth, mHeight);
-        mPvOrthoAlloc.setupOrthoWindow(mWidth, mHeight);        
+        mPvOrthoAlloc.setupOrthoWindow(mWidth, mHeight);
 
         ProgramVertex.Builder builder = new ProgramVertex.Builder(mRS, null, null);
         mPvBackground = builder.create();
index 55f9940..ded2305 100644 (file)
@@ -39,18 +39,36 @@ public class GalaxyWallpaper extends WallpaperService {
         @Override
         public void onDestroy() {
             super.onDestroy();
+            destroyRenderer();
+        }
+
+        private void destroyRenderer() {
+            if (mRenderer != null) {
+                mRenderer.stop();
+                mRenderer = null;
+            }
         }
 
         @Override
         public void onVisibilityChanged(boolean visible) {
             super.onVisibilityChanged(visible);
+            if (visible) {
+                mRenderer.start();
+            } else {
+                mRenderer.stop();
+            }
         }
 
         @Override
         public void onSurfaceChanged(SurfaceHolder holder, int format, int width, int height) {
             super.onSurfaceChanged(holder, format, width, height);
-            mRenderer = new GalaxyRS(width, height);
-            mRenderer.init(mRs, getResources());
+            if (mRenderer == null) {
+                mRenderer = new GalaxyRS(width, height);
+                mRenderer.init(mRs, getResources());
+                mRenderer.start();
+            } else {
+                mRenderer.resize(width, height);
+            }
         }
 
         @Override
@@ -72,6 +90,7 @@ public class GalaxyWallpaper extends WallpaperService {
         @Override
         public void onSurfaceDestroyed(SurfaceHolder holder) {
             super.onSurfaceDestroyed(holder);
+            destroyRenderer();
         }
     }
 }
\ No newline at end of file
index c473f0c..f16bd31 100644 (file)
@@ -69,8 +69,8 @@ class GrassRS {
     private Resources mResources;
     private RenderScript mRS;
 
-    private final int mWidth;
-    private final int mHeight;
+    private int mWidth;
+    private int mHeight;
 
     @SuppressWarnings({ "FieldCanBeLocal" })
     private ScriptC mScript;
@@ -98,6 +98,7 @@ class GrassRS {
 
     private int mTriangles;
     private final float[] mFloatData5 = new float[5];
+    private WorldState mWorldState;
 
     public GrassRS(int width, int height) {
         mWidth = width;
@@ -109,7 +110,28 @@ class GrassRS {
         mResources = res;
         initRS();
     }
+    
+    void stop() {
+        mRS.contextBindRootScript(null);
+    }
+
+    void start() {
+        mRS.contextBindRootScript(mScript);
+    }
+
+    void resize(int width, int height) {
+        mWidth = width;
+        mHeight = height;
 
+        mWorldState.width = width;
+        mWorldState.height = height;
+        mState.data(mWorldState);
+
+        mPvOrthoAlloc.setupOrthoWindow(mWidth, mHeight);
+
+        // TODO: REPOSITION BLADES
+    }
+    
     private void initRS() {
         createProgramVertex();
         createProgramFragmentStore();
@@ -129,8 +151,6 @@ class GrassRS {
         mScript.bindAllocation(mState, RSID_STATE);
         mScript.bindAllocation(mBlades, RSID_BLADES);
         mScript.bindAllocation(mBladesBuffer, RSID_BLADES_BUFFER);
-
-        mRS.contextBindRootScript(mScript);
     }
 
     private void createScriptStructures() {
@@ -147,15 +167,15 @@ class GrassRS {
     }
 
     private void createState() {
-        WorldState state = new WorldState();
-        state.width = mWidth;
-        state.height = mHeight;
-        state.bladesCount = BLADES_COUNT;
-        state.trianglesCount = mTriangles;
+        mWorldState = new WorldState();
+        mWorldState.width = mWidth;
+        mWorldState.height = mHeight;
+        mWorldState.bladesCount = BLADES_COUNT;
+        mWorldState.trianglesCount = mTriangles;
 
         mStateType = Type.createFromClass(mRS, WorldState.class, 1, "WorldState");
         mState = Allocation.createTyped(mRS, mStateType);
-        mState.data(state);
+        mState.data(mWorldState);
     }
 
     private void createBlades() {
index 68118a9..f32db51 100644 (file)
@@ -38,18 +38,36 @@ public class GrassWallpaper extends WallpaperService {
         @Override
         public void onDestroy() {
             super.onDestroy();
+            destroyRenderer();
+        }
+
+        private void destroyRenderer() {
+            if (mRenderer != null) {
+                mRenderer.stop();
+                mRenderer = null;
+            }
         }
 
         @Override
         public void onVisibilityChanged(boolean visible) {
             super.onVisibilityChanged(visible);
+            if (visible) {
+                mRenderer.start();                
+            } else {
+                mRenderer.stop();
+            }
         }
 
         @Override
         public void onSurfaceChanged(SurfaceHolder holder, int format, int width, int height) {
             super.onSurfaceChanged(holder, format, width, height);
-            mRenderer = new GrassRS(width, height);
-            mRenderer.init(mRs, getResources());
+            if (mRenderer == null) {
+                mRenderer = new GrassRS(width, height);
+                mRenderer.init(mRs, getResources());
+                mRenderer.start();
+            } else {
+                mRenderer.resize(width, height);
+            }
         }
 
         @Override
@@ -66,6 +84,7 @@ public class GrassWallpaper extends WallpaperService {
         @Override
         public void onSurfaceDestroyed(SurfaceHolder holder) {
             super.onSurfaceDestroyed(holder);
+            destroyRenderer();
         }
     }
 }