packages/wallpapers/Basic
修订版 | 44da17844aa73a047a1f1ff67b4794ea330be3b5 (tree) |
---|---|
时间 | 2009-08-29 03:03:21 |
作者 | Romain Guy <romainguy@andr...> |
Commiter | Romain Guy |
Refactor RenderScript wallpapers to share more code between the implementations.
Change-Id: I5995f68438531396cfae80c27e5d7781effaaa31
@@ -53,11 +53,12 @@ | ||
53 | 53 | |
54 | 54 | #define REAL_TIME 0 |
55 | 55 | |
56 | -float time(int frameCount) { | |
56 | +float time() { | |
57 | 57 | if (REAL_TIME) { |
58 | 58 | return (hour() * 3600.0f + minute() * 60.0f + second()) / SECONDS_IN_DAY; |
59 | 59 | } |
60 | - return (frameCount % 180) / 180.0f; | |
60 | + float t = uptimeMillis() / 20000.0f; | |
61 | + return t - (int) t; | |
61 | 62 | } |
62 | 63 | |
63 | 64 | void alpha(float a) { |
@@ -92,14 +93,14 @@ void drawSunset(int width, int height) { | ||
92 | 93 | drawRect(0.0f, 0.0f, width, height, 0.0f); |
93 | 94 | } |
94 | 95 | |
95 | -int drawBlade(float *bladeStruct, float *bladeBuffer, int *bladeColor, float now, int frameCount) { | |
96 | +int drawBlade(float *bladeStruct, float *bladeBuffer, int *bladeColor, float now, float xOffset) { | |
96 | 97 | float offset = bladeStruct[BLADE_STRUCT_OFFSET]; |
97 | 98 | float scale = bladeStruct[BLADE_STRUCT_SCALE]; |
98 | 99 | float angle = bladeStruct[BLADE_STRUCT_ANGLE]; |
99 | 100 | float hardness = bladeStruct[BLADE_STRUCT_HARDNESS]; |
100 | 101 | float turbulenceX = bladeStruct[BLADE_STRUCT_TURBULENCEX]; |
101 | 102 | |
102 | - float xpos = bladeStruct[BLADE_STRUCT_XPOS]; | |
103 | + float xpos = bladeStruct[BLADE_STRUCT_XPOS] + xOffset; | |
103 | 104 | float ypos = bladeStruct[BLADE_STRUCT_YPOS]; |
104 | 105 | |
105 | 106 | float lengthX = bladeStruct[BLADE_STRUCT_LENGTHX]; |
@@ -126,7 +127,7 @@ int drawBlade(float *bladeStruct, float *bladeBuffer, int *bladeColor, float now | ||
126 | 127 | |
127 | 128 | int color = hsbToAbgr(h, s, lerpf(0, b, newB), 1.0f); |
128 | 129 | |
129 | - float newAngle = turbulencef2(turbulenceX, frameCount * 0.006f, 4.0f) - 0.5f; | |
130 | + float newAngle = turbulencef2(turbulenceX, uptimeMillis() * 0.00004f, 4.0f) - 0.5f; | |
130 | 131 | newAngle *= 0.5f; |
131 | 132 | angle = clampf(angle + (newAngle + offset - angle) * 0.15f, -MAX_BEND, MAX_BEND); |
132 | 133 |
@@ -202,7 +203,7 @@ int drawBlade(float *bladeStruct, float *bladeBuffer, int *bladeColor, float now | ||
202 | 203 | return triangles * 15; |
203 | 204 | } |
204 | 205 | |
205 | -void drawBlades(float now, int frameCount) { | |
206 | +void drawBlades(float now, float xOffset) { | |
206 | 207 | // For anti-aliasing |
207 | 208 | bindTexture(NAMED_PFBackground, 0, NAMED_TAa); |
208 | 209 |
@@ -215,7 +216,7 @@ void drawBlades(float now, int frameCount) { | ||
215 | 216 | int *bladeColor = loadArrayI32(RSID_BLADES_BUFFER, 0); |
216 | 217 | |
217 | 218 | for ( ; i < bladesCount; i += 1) { |
218 | - int offset = drawBlade(bladeStruct, bladeBuffer, bladeColor, now, frameCount); | |
219 | + int offset = drawBlade(bladeStruct, bladeBuffer, bladeColor, now, xOffset); | |
219 | 220 | bladeBuffer += offset; |
220 | 221 | bladeColor += offset; |
221 | 222 | bladeStruct += BLADE_STRUCT_FIELDS_COUNT; |
@@ -229,8 +230,9 @@ int main(int launchID) { | ||
229 | 230 | int width = State_width; |
230 | 231 | int height = State_height; |
231 | 232 | |
232 | - int frameCount = State_frameCount; | |
233 | - float now = time(frameCount); | |
233 | + float x = lerpf(width, 0, State_xOffset); | |
234 | + | |
235 | + float now = time(); | |
234 | 236 | alpha(1.0f); |
235 | 237 | |
236 | 238 | if (now >= MIDNIGHT && now < MORNING) { |
@@ -251,10 +253,7 @@ int main(int launchID) { | ||
251 | 253 | drawSunset(width, height); |
252 | 254 | } |
253 | 255 | |
254 | - drawBlades(now, frameCount); | |
255 | - | |
256 | - frameCount++; | |
257 | - storeI32(RSID_STATE, OFFSETOF_WorldState_frameCount, frameCount); | |
256 | + drawBlades(now, x); | |
258 | 257 | |
259 | 258 | return 1; |
260 | 259 | } |
@@ -0,0 +1,80 @@ | ||
1 | +/* | |
2 | + * Copyright (C) 2009 The Android Open Source Project | |
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 | + | |
18 | +package com.android.wallpaper; | |
19 | + | |
20 | +import android.content.res.Resources; | |
21 | +import android.renderscript.RenderScript; | |
22 | +import android.renderscript.ScriptC; | |
23 | + | |
24 | +public abstract class RenderScriptScene { | |
25 | + protected int mWidth; | |
26 | + protected int mHeight; | |
27 | + protected Resources mResources; | |
28 | + protected RenderScript mRS; | |
29 | + protected ScriptC mScript; | |
30 | + | |
31 | + public RenderScriptScene(int width, int height) { | |
32 | + mWidth = width; | |
33 | + mHeight = height; | |
34 | + } | |
35 | + | |
36 | + public void init(RenderScript rs, Resources res) { | |
37 | + mRS = rs; | |
38 | + mResources = res; | |
39 | + mScript = createScript(); | |
40 | + } | |
41 | + | |
42 | + public int getWidth() { | |
43 | + return mWidth; | |
44 | + } | |
45 | + | |
46 | + public int getHeight() { | |
47 | + return mHeight; | |
48 | + } | |
49 | + | |
50 | + public Resources getResources() { | |
51 | + return mResources; | |
52 | + } | |
53 | + | |
54 | + public RenderScript getRS() { | |
55 | + return mRS; | |
56 | + } | |
57 | + | |
58 | + public ScriptC getScript() { | |
59 | + return mScript; | |
60 | + } | |
61 | + | |
62 | + protected abstract ScriptC createScript(); | |
63 | + | |
64 | + public void stop() { | |
65 | + mRS.contextBindRootScript(null); | |
66 | + } | |
67 | + | |
68 | + public void start() { | |
69 | + mRS.contextBindRootScript(mScript); | |
70 | + } | |
71 | + | |
72 | + public void resize(int width, int height) { | |
73 | + mWidth = width; | |
74 | + mHeight = height; | |
75 | + } | |
76 | + | |
77 | + @SuppressWarnings({"UnusedDeclaration"}) | |
78 | + public void setOffset(float xOffset, float yOffset, int xPixels, int yPixels) { | |
79 | + } | |
80 | +} |
@@ -0,0 +1,104 @@ | ||
1 | +/* | |
2 | + * Copyright (C) 2009 The Android Open Source Project | |
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 | + | |
18 | +package com.android.wallpaper; | |
19 | + | |
20 | +import android.service.wallpaper.WallpaperService; | |
21 | +import android.renderscript.RenderScript; | |
22 | +import android.view.SurfaceHolder; | |
23 | +import android.view.Surface; | |
24 | + | |
25 | +public abstract class RenderScriptWallpaper<T extends RenderScriptScene> extends WallpaperService { | |
26 | + public Engine onCreateEngine() { | |
27 | + return new RenderScriptEngine(); | |
28 | + } | |
29 | + | |
30 | + protected abstract T createScene(int width, int height); | |
31 | + | |
32 | + private class RenderScriptEngine extends Engine { | |
33 | + private RenderScript mRs; | |
34 | + private T mRenderer; | |
35 | + | |
36 | + @Override | |
37 | + public void onCreate(SurfaceHolder surfaceHolder) { | |
38 | + super.onCreate(surfaceHolder); | |
39 | + setTouchEventsEnabled(false); | |
40 | + surfaceHolder.setSizeFromLayout(); | |
41 | + } | |
42 | + | |
43 | + @Override | |
44 | + public void onDestroy() { | |
45 | + super.onDestroy(); | |
46 | + destroyRenderer(); | |
47 | + } | |
48 | + | |
49 | + private void destroyRenderer() { | |
50 | + if (mRenderer != null) { | |
51 | + mRenderer.stop(); | |
52 | + mRenderer = null; | |
53 | + } | |
54 | + if (mRs != null) { | |
55 | + mRs.destroy(); | |
56 | + mRs = null; | |
57 | + } | |
58 | + } | |
59 | + | |
60 | + @Override | |
61 | + public void onVisibilityChanged(boolean visible) { | |
62 | + super.onVisibilityChanged(visible); | |
63 | + if (visible) { | |
64 | + mRenderer.start(); | |
65 | + } else { | |
66 | + mRenderer.stop(); | |
67 | + } | |
68 | + } | |
69 | + | |
70 | + @Override | |
71 | + public void onSurfaceChanged(SurfaceHolder holder, int format, int width, int height) { | |
72 | + super.onSurfaceChanged(holder, format, width, height); | |
73 | + if (mRenderer == null) { | |
74 | + mRenderer = createScene(width, height); | |
75 | + mRenderer.init(mRs, getResources()); | |
76 | + mRenderer.start(); | |
77 | + } else { | |
78 | + mRenderer.resize(width, height); | |
79 | + } | |
80 | + } | |
81 | + | |
82 | + @Override | |
83 | + public void onOffsetsChanged(float xOffset, float yOffset, int xPixels, int yPixels) { | |
84 | + mRenderer.setOffset(xOffset, yOffset, xPixels, yPixels); | |
85 | + } | |
86 | + | |
87 | + @Override | |
88 | + public void onSurfaceCreated(SurfaceHolder holder) { | |
89 | + super.onSurfaceCreated(holder); | |
90 | + | |
91 | + Surface surface = null; | |
92 | + while (surface == null) { | |
93 | + surface = holder.getSurface(); | |
94 | + } | |
95 | + mRs = new RenderScript(surface, false); | |
96 | + } | |
97 | + | |
98 | + @Override | |
99 | + public void onSurfaceDestroyed(SurfaceHolder holder) { | |
100 | + super.onSurfaceDestroyed(holder); | |
101 | + destroyRenderer(); | |
102 | + } | |
103 | + } | |
104 | +} |
@@ -16,8 +16,6 @@ | ||
16 | 16 | |
17 | 17 | package com.android.wallpaper.galaxy; |
18 | 18 | |
19 | -import android.content.res.Resources; | |
20 | -import android.renderscript.RenderScript; | |
21 | 19 | import android.renderscript.ScriptC; |
22 | 20 | import android.renderscript.ProgramFragment; |
23 | 21 | import android.renderscript.ProgramStore; |
@@ -43,8 +41,9 @@ import static android.util.MathUtils.*; | ||
43 | 41 | import java.util.TimeZone; |
44 | 42 | |
45 | 43 | import com.android.wallpaper.R; |
44 | +import com.android.wallpaper.RenderScriptScene; | |
46 | 45 | |
47 | -class GalaxyRS { | |
46 | +class GalaxyRS extends RenderScriptScene { | |
48 | 47 | private static final int GALAXY_RADIUS = 300; |
49 | 48 | private static final int PARTICLES_COUNT = 12000; |
50 | 49 | private static final float ELLIPSE_TWIST = 0.023333333f; |
@@ -67,16 +66,8 @@ class GalaxyRS { | ||
67 | 66 | |
68 | 67 | private static final int RSID_PARTICLES_BUFFER = 2; |
69 | 68 | |
70 | - private Resources mResources; | |
71 | - private RenderScript mRS; | |
72 | - | |
73 | 69 | private final BitmapFactory.Options mOptionsARGB = new BitmapFactory.Options(); |
74 | 70 | |
75 | - private int mWidth; | |
76 | - private int mHeight; | |
77 | - | |
78 | - @SuppressWarnings({"FieldCanBeLocal"}) | |
79 | - private ScriptC mScript; | |
80 | 71 | @SuppressWarnings({"FieldCanBeLocal"}) |
81 | 72 | private ProgramFragment mPfBackground; |
82 | 73 | @SuppressWarnings({"FieldCanBeLocal"}) |
@@ -107,28 +98,14 @@ class GalaxyRS { | ||
107 | 98 | |
108 | 99 | private final float[] mFloatData5 = new float[5]; |
109 | 100 | |
110 | - public GalaxyRS(int width, int height) { | |
111 | - mWidth = width; | |
112 | - mHeight = height; | |
101 | + GalaxyRS(int width, int height) { | |
102 | + super(width, height); | |
113 | 103 | mOptionsARGB.inScaled = false; |
114 | 104 | mOptionsARGB.inPreferredConfig = Bitmap.Config.ARGB_8888; |
115 | 105 | } |
116 | 106 | |
117 | - public void init(RenderScript rs, Resources res) { | |
118 | - mRS = rs; | |
119 | - mResources = res; | |
120 | - initRS(); | |
121 | - } | |
122 | - | |
123 | - void stop() { | |
124 | - mRS.contextBindRootScript(null); | |
125 | - } | |
126 | - | |
127 | - void start() { | |
128 | - mRS.contextBindRootScript(mScript); | |
129 | - } | |
130 | - | |
131 | - private void initRS() { | |
107 | + @Override | |
108 | + protected ScriptC createScript() { | |
132 | 109 | createProgramVertex(); |
133 | 110 | createProgramFragmentStore(); |
134 | 111 | createProgramFragment(); |
@@ -140,13 +117,15 @@ class GalaxyRS { | ||
140 | 117 | sb.setScript(mResources, R.raw.galaxy); |
141 | 118 | sb.setRoot(true); |
142 | 119 | |
143 | - mScript = sb.create(); | |
144 | - mScript.setClearColor(1.0f, 0.0f, 0.0f, 1.0f); | |
145 | - mScript.setTimeZone(TimeZone.getDefault().getID()); | |
120 | + ScriptC script = sb.create(); | |
121 | + script.setClearColor(1.0f, 0.0f, 0.0f, 1.0f); | |
122 | + script.setTimeZone(TimeZone.getDefault().getID()); | |
146 | 123 | |
147 | - mScript.bindAllocation(mState, RSID_STATE); | |
148 | - mScript.bindAllocation(mParticles, RSID_PARTICLES); | |
149 | - mScript.bindAllocation(mParticlesBuffer, RSID_PARTICLES_BUFFER); | |
124 | + script.bindAllocation(mState, RSID_STATE); | |
125 | + script.bindAllocation(mParticles, RSID_PARTICLES); | |
126 | + script.bindAllocation(mParticlesBuffer, RSID_PARTICLES_BUFFER); | |
127 | + | |
128 | + return script; | |
150 | 129 | } |
151 | 130 | |
152 | 131 | private void createScriptStructures() { |
@@ -173,14 +152,15 @@ class GalaxyRS { | ||
173 | 152 | mParticlesMesh.bindVertexAllocation(mParticlesBuffer, 0); |
174 | 153 | } |
175 | 154 | |
176 | - void setOffsetX(float xOffset) { | |
155 | + @Override | |
156 | + public void setOffset(float xOffset, float yOffset, int xPixels, int yPixels) { | |
177 | 157 | mGalaxyState.xOffset = xOffset; |
178 | 158 | mState.data(mGalaxyState); |
179 | 159 | } |
180 | 160 | |
181 | - void resize(int width, int height) { | |
182 | - mWidth = width; | |
183 | - mHeight = height; | |
161 | + @Override | |
162 | + public void resize(int width, int height) { | |
163 | + super.resize(width, height); | |
184 | 164 | |
185 | 165 | mGalaxyState.width = width; |
186 | 166 | mGalaxyState.height = height; |
@@ -16,85 +16,10 @@ | ||
16 | 16 | |
17 | 17 | package com.android.wallpaper.galaxy; |
18 | 18 | |
19 | -import android.service.wallpaper.WallpaperService; | |
20 | -import android.view.SurfaceHolder; | |
21 | -import android.view.Surface; | |
22 | -import android.renderscript.RenderScript; | |
19 | +import com.android.wallpaper.RenderScriptWallpaper; | |
23 | 20 | |
24 | -public class GalaxyWallpaper extends WallpaperService { | |
25 | - public Engine onCreateEngine() { | |
26 | - return new RenderScriptEngine(); | |
21 | +public class GalaxyWallpaper extends RenderScriptWallpaper<GalaxyRS> { | |
22 | + protected GalaxyRS createScene(int width, int height) { | |
23 | + return new GalaxyRS(width, height); | |
27 | 24 | } |
28 | - | |
29 | - private class RenderScriptEngine extends Engine { | |
30 | - private RenderScript mRs; | |
31 | - private GalaxyRS mRenderer; | |
32 | - | |
33 | - @Override | |
34 | - public void onCreate(SurfaceHolder surfaceHolder) { | |
35 | - super.onCreate(surfaceHolder); | |
36 | - surfaceHolder.setSizeFromLayout(); | |
37 | - } | |
38 | - | |
39 | - @Override | |
40 | - public void onDestroy() { | |
41 | - super.onDestroy(); | |
42 | - destroyRenderer(); | |
43 | - } | |
44 | - | |
45 | - private void destroyRenderer() { | |
46 | - if (mRenderer != null) { | |
47 | - mRenderer.stop(); | |
48 | - mRenderer = null; | |
49 | - } | |
50 | - if (mRs != null) { | |
51 | - mRs.destroy(); | |
52 | - mRs = null; | |
53 | - } | |
54 | - } | |
55 | - | |
56 | - @Override | |
57 | - public void onVisibilityChanged(boolean visible) { | |
58 | - super.onVisibilityChanged(visible); | |
59 | - if (visible) { | |
60 | - mRenderer.start(); | |
61 | - } else { | |
62 | - mRenderer.stop(); | |
63 | - } | |
64 | - } | |
65 | - | |
66 | - @Override | |
67 | - public void onSurfaceChanged(SurfaceHolder holder, int format, int width, int height) { | |
68 | - super.onSurfaceChanged(holder, format, width, height); | |
69 | - if (mRenderer == null) { | |
70 | - mRenderer = new GalaxyRS(width, height); | |
71 | - mRenderer.init(mRs, getResources()); | |
72 | - mRenderer.start(); | |
73 | - } else { | |
74 | - mRenderer.resize(width, height); | |
75 | - } | |
76 | - } | |
77 | - | |
78 | - @Override | |
79 | - public void onOffsetsChanged(float xOffset, float yOffset, int xPixels, int yPixels) { | |
80 | - mRenderer.setOffsetX(xOffset); | |
81 | - } | |
82 | - | |
83 | - @Override | |
84 | - public void onSurfaceCreated(SurfaceHolder holder) { | |
85 | - super.onSurfaceCreated(holder); | |
86 | - | |
87 | - Surface surface = null; | |
88 | - while (surface == null) { | |
89 | - surface = holder.getSurface(); | |
90 | - } | |
91 | - mRs = new RenderScript(surface, false); | |
92 | - } | |
93 | - | |
94 | - @Override | |
95 | - public void onSurfaceDestroyed(SurfaceHolder holder) { | |
96 | - super.onSurfaceDestroyed(holder); | |
97 | - destroyRenderer(); | |
98 | - } | |
99 | - } | |
100 | -} | |
\ No newline at end of file | ||
25 | +} |
@@ -16,13 +16,11 @@ | ||
16 | 16 | |
17 | 17 | package com.android.wallpaper.grass; |
18 | 18 | |
19 | -import android.content.res.Resources; | |
20 | 19 | import android.renderscript.Sampler; |
21 | 20 | import static android.renderscript.ProgramFragment.EnvMode.*; |
22 | 21 | import static android.renderscript.ProgramStore.DepthFunc.*; |
23 | 22 | import static android.renderscript.ProgramStore.BlendSrcFunc; |
24 | 23 | import static android.renderscript.ProgramStore.BlendDstFunc; |
25 | -import android.renderscript.RenderScript; | |
26 | 24 | import android.renderscript.ProgramFragment; |
27 | 25 | import android.renderscript.ProgramStore; |
28 | 26 | import android.renderscript.Allocation; |
@@ -37,10 +35,11 @@ import android.renderscript.SimpleMesh; | ||
37 | 35 | import android.renderscript.Primitive; |
38 | 36 | import static android.renderscript.Sampler.Value.*; |
39 | 37 | import com.android.wallpaper.R; |
38 | +import com.android.wallpaper.RenderScriptScene; | |
40 | 39 | |
41 | 40 | import java.util.TimeZone; |
42 | 41 | |
43 | -class GrassRS { | |
42 | +class GrassRS extends RenderScriptScene { | |
44 | 43 | private static final float TESSELATION = 0.5f; |
45 | 44 | |
46 | 45 | private static final int RSID_STATE = 0; |
@@ -66,14 +65,6 @@ class GrassRS { | ||
66 | 65 | |
67 | 66 | private static final int RSID_BLADES_BUFFER = 2; |
68 | 67 | |
69 | - private Resources mResources; | |
70 | - private RenderScript mRS; | |
71 | - | |
72 | - private int mWidth; | |
73 | - private int mHeight; | |
74 | - | |
75 | - @SuppressWarnings({ "FieldCanBeLocal" }) | |
76 | - private ScriptC mScript; | |
77 | 68 | @SuppressWarnings({ "FieldCanBeLocal" }) |
78 | 69 | private ProgramFragment mPfBackground; |
79 | 70 | @SuppressWarnings({ "FieldCanBeLocal" }) |
@@ -100,39 +91,25 @@ class GrassRS { | ||
100 | 91 | private final float[] mFloatData5 = new float[5]; |
101 | 92 | private WorldState mWorldState; |
102 | 93 | |
103 | - public GrassRS(int width, int height) { | |
104 | - mWidth = width; | |
105 | - mHeight = height; | |
106 | - } | |
107 | - | |
108 | - public void init(RenderScript rs, Resources res) { | |
109 | - mRS = rs; | |
110 | - mResources = res; | |
111 | - initRS(); | |
112 | - } | |
113 | - | |
114 | - void stop() { | |
115 | - mRS.contextBindRootScript(null); | |
116 | - } | |
117 | - | |
118 | - void start() { | |
119 | - mRS.contextBindRootScript(mScript); | |
94 | + GrassRS(int width, int height) { | |
95 | + super(width, height); | |
120 | 96 | } |
121 | 97 | |
122 | - void resize(int width, int height) { | |
123 | - mWidth = width; | |
124 | - mHeight = height; | |
125 | - | |
98 | + @Override | |
99 | + public void resize(int width, int height) { | |
100 | + super.resize(width, height); | |
101 | + | |
126 | 102 | mWorldState.width = width; |
127 | 103 | mWorldState.height = height; |
128 | 104 | mState.data(mWorldState); |
129 | 105 | |
130 | 106 | mPvOrthoAlloc.setupOrthoWindow(mWidth, mHeight); |
131 | 107 | |
132 | - // TODO: REPOSITION BLADES | |
108 | + // TODO: REPOSITION BLADES | |
133 | 109 | } |
134 | - | |
135 | - private void initRS() { | |
110 | + | |
111 | + @Override | |
112 | + protected ScriptC createScript() { | |
136 | 113 | createProgramVertex(); |
137 | 114 | createProgramFragmentStore(); |
138 | 115 | createProgramFragment(); |
@@ -144,13 +121,15 @@ class GrassRS { | ||
144 | 121 | sb.setScript(mResources, R.raw.grass); |
145 | 122 | sb.setRoot(true); |
146 | 123 | |
147 | - mScript = sb.create(); | |
148 | - mScript.setClearColor(0.0f, 0.0f, 0.0f, 1.0f); | |
149 | - mScript.setTimeZone(TimeZone.getDefault().getID()); | |
124 | + ScriptC script = sb.create(); | |
125 | + script.setClearColor(0.0f, 0.0f, 0.0f, 1.0f); | |
126 | + script.setTimeZone(TimeZone.getDefault().getID()); | |
150 | 127 | |
151 | - mScript.bindAllocation(mState, RSID_STATE); | |
152 | - mScript.bindAllocation(mBlades, RSID_BLADES); | |
153 | - mScript.bindAllocation(mBladesBuffer, RSID_BLADES_BUFFER); | |
128 | + script.bindAllocation(mState, RSID_STATE); | |
129 | + script.bindAllocation(mBlades, RSID_BLADES); | |
130 | + script.bindAllocation(mBladesBuffer, RSID_BLADES_BUFFER); | |
131 | + | |
132 | + return script; | |
154 | 133 | } |
155 | 134 | |
156 | 135 | private void createScriptStructures() { |
@@ -158,12 +137,18 @@ class GrassRS { | ||
158 | 137 | createState(); |
159 | 138 | } |
160 | 139 | |
140 | + @Override | |
141 | + public void setOffset(float xOffset, float yOffset, int xPixels, int yPixels) { | |
142 | + mWorldState.xOffset = xOffset; | |
143 | + mState.data(mWorldState); | |
144 | + } | |
145 | + | |
161 | 146 | static class WorldState { |
162 | - public int frameCount; | |
163 | 147 | public int bladesCount; |
164 | 148 | public int trianglesCount; |
165 | 149 | public int width; |
166 | 150 | public int height; |
151 | + public float xOffset; | |
167 | 152 | } |
168 | 153 | |
169 | 154 | private void createState() { |
@@ -251,7 +236,7 @@ class GrassRS { | ||
251 | 236 | |
252 | 237 | private int createBlade(float[] blades, int index) { |
253 | 238 | final float size = random(4.0f) + 4.0f; |
254 | - final int xpos = random(mWidth); | |
239 | + final int xpos = random(-mWidth, mWidth); | |
255 | 240 | |
256 | 241 | //noinspection PointlessArithmeticExpression |
257 | 242 | blades[index + BLADE_STRUCT_ANGLE] = 0.0f; |
@@ -16,80 +16,12 @@ | ||
16 | 16 | |
17 | 17 | package com.android.wallpaper.grass; |
18 | 18 | |
19 | -import android.service.wallpaper.WallpaperService; | |
20 | -import android.view.SurfaceHolder; | |
21 | -import android.view.Surface; | |
22 | -import android.renderscript.RenderScript; | |
19 | +import com.android.wallpaper.RenderScriptWallpaper; | |
20 | +import com.android.wallpaper.RenderScriptScene; | |
23 | 21 | |
24 | -public class GrassWallpaper extends WallpaperService { | |
25 | - public Engine onCreateEngine() { | |
26 | - return new RenderScriptEngine(); | |
27 | - } | |
28 | - | |
29 | - private class RenderScriptEngine extends Engine { | |
30 | - private RenderScript mRs; | |
31 | - private GrassRS mRenderer; | |
32 | - | |
33 | - @Override | |
34 | - public void onCreate(SurfaceHolder surfaceHolder) { | |
35 | - super.onCreate(surfaceHolder); | |
36 | - } | |
37 | - | |
38 | - @Override | |
39 | - public void onDestroy() { | |
40 | - super.onDestroy(); | |
41 | - destroyRenderer(); | |
42 | - } | |
43 | - | |
44 | - private void destroyRenderer() { | |
45 | - if (mRenderer != null) { | |
46 | - mRenderer.stop(); | |
47 | - mRenderer = null; | |
48 | - } | |
49 | - if (mRs != null) { | |
50 | - mRs.destroy(); | |
51 | - mRs = null; | |
52 | - } | |
53 | - } | |
54 | - | |
55 | - @Override | |
56 | - public void onVisibilityChanged(boolean visible) { | |
57 | - super.onVisibilityChanged(visible); | |
58 | - if (visible) { | |
59 | - mRenderer.start(); | |
60 | - } else { | |
61 | - mRenderer.stop(); | |
62 | - } | |
63 | - } | |
64 | - | |
65 | - @Override | |
66 | - public void onSurfaceChanged(SurfaceHolder holder, int format, int width, int height) { | |
67 | - super.onSurfaceChanged(holder, format, width, height); | |
68 | - if (mRenderer == null) { | |
69 | - mRenderer = new GrassRS(width, height); | |
70 | - mRenderer.init(mRs, getResources()); | |
71 | - mRenderer.start(); | |
72 | - } else { | |
73 | - mRenderer.resize(width, height); | |
74 | - } | |
75 | - } | |
76 | - | |
77 | - @Override | |
78 | - public void onSurfaceCreated(SurfaceHolder holder) { | |
79 | - super.onSurfaceCreated(holder); | |
80 | - | |
81 | - Surface surface = null; | |
82 | - while (surface == null) { | |
83 | - surface = holder.getSurface(); | |
84 | - } | |
85 | - mRs = new RenderScript(surface, false); | |
86 | - } | |
87 | - | |
88 | - @Override | |
89 | - public void onSurfaceDestroyed(SurfaceHolder holder) { | |
90 | - super.onSurfaceDestroyed(holder); | |
91 | - destroyRenderer(); | |
92 | - } | |
22 | +public class GrassWallpaper extends RenderScriptWallpaper { | |
23 | + protected RenderScriptScene createScene(int width, int height) { | |
24 | + return new GrassRS(width, height); | |
93 | 25 | } |
94 | 26 | } |
95 | 27 |