blob: 662a9dcbcca078667e470837886fa0b249da9d03 [file] [log] [blame]
Joe Onorato93839052009-08-06 20:34:32 -07001/*
2 * Copyright (C) 2008 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
17package com.android.launcher2;
18
19import java.io.Writer;
20import java.util.ArrayList;
21import java.util.concurrent.Semaphore;
22import java.lang.Float;
23
24import android.renderscript.RSSurfaceView;
25import android.renderscript.RenderScript;
26
27import android.renderscript.RenderScript;
28import android.renderscript.ProgramVertex;
29import android.renderscript.Element;
30import android.renderscript.Allocation;
31import android.renderscript.Script;
32import android.renderscript.ScriptC;
33import android.renderscript.ProgramFragment;
34import android.renderscript.ProgramStore;
35import android.renderscript.Sampler;
36
37import android.content.Context;
38import android.content.res.Resources;
39import android.graphics.Bitmap;
40import android.graphics.BitmapFactory;
41import android.graphics.Canvas;
42import android.graphics.Paint;
Joe Onorato93839052009-08-06 20:34:32 -070043import android.graphics.drawable.BitmapDrawable;
44import android.graphics.drawable.Drawable;
45import android.os.Handler;
46import android.os.Message;
Joe Onoratod769a632009-08-11 17:09:02 -070047import android.os.SystemClock;
Joe Onorato93839052009-08-06 20:34:32 -070048import android.util.AttributeSet;
49import android.util.Log;
Joe Onoratod769a632009-08-11 17:09:02 -070050import android.view.KeyEvent;
51import android.view.MotionEvent;
Joe Onorato93839052009-08-06 20:34:32 -070052import android.view.Surface;
53import android.view.SurfaceHolder;
54import android.view.SurfaceView;
Joe Onoratod769a632009-08-11 17:09:02 -070055import android.view.VelocityTracker;
56import android.view.ViewConfiguration;
Joe Onorato93839052009-08-06 20:34:32 -070057import android.graphics.PixelFormat;
58
59
60public class AllAppsView extends RSSurfaceView {
Joe Onorato1feb3a82009-08-08 22:32:00 -070061 private RenderScript mRS;
62 private RolloRS mRollo;
63
Joe Onoratod769a632009-08-11 17:09:02 -070064 private ViewConfiguration mConfig;
65 private VelocityTracker mVelocity;
66 private int mLastScrollX;
Joe Onorato1feb3a82009-08-08 22:32:00 -070067 private int mLastMotionX;
68
Joe Onorato93839052009-08-06 20:34:32 -070069 public AllAppsView(Context context) {
70 super(context);
71 setFocusable(true);
72 getHolder().setFormat(PixelFormat.TRANSLUCENT);
Joe Onoratod769a632009-08-11 17:09:02 -070073 mConfig = ViewConfiguration.get(context);
Joe Onorato93839052009-08-06 20:34:32 -070074 }
75
76 public AllAppsView(Context context, AttributeSet attrs) {
77 this(context);
78 }
79
80 public AllAppsView(Context context, AttributeSet attrs, int defStyle) {
81 this(context);
82 }
83
Joe Onorato1feb3a82009-08-08 22:32:00 -070084 @Override
Joe Onorato93839052009-08-06 20:34:32 -070085 public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
86 super.surfaceChanged(holder, format, w, h);
87
88 mRS = createRenderScript();
Joe Onorato1feb3a82009-08-08 22:32:00 -070089 mRollo = new RolloRS();
90 mRollo.init(getResources(), w, h);
Joe Onorato93839052009-08-06 20:34:32 -070091 }
92
93 @Override
94 public boolean onKeyDown(int keyCode, KeyEvent event)
95 {
Joe Onorato93839052009-08-06 20:34:32 -070096 // this method doesn't work when 'extends View' include 'extends ScrollView'.
97 return super.onKeyDown(keyCode, event);
98 }
99
Joe Onorato93839052009-08-06 20:34:32 -0700100 @Override
101 public boolean onTouchEvent(MotionEvent ev)
102 {
Joe Onorato1feb3a82009-08-08 22:32:00 -0700103 int x = (int)ev.getX();
104 int deltaX;
105 switch (ev.getAction()) {
106 case MotionEvent.ACTION_DOWN:
107 mLastMotionX = x;
Joe Onoratod769a632009-08-11 17:09:02 -0700108 mRollo.mState.read();
109 mRollo.mState.scrollX = mLastScrollX = mRollo.mState.currentScrollX;
110 mRollo.mState.flingVelocityX = 0;
111 mRollo.mState.adjustedDeceleration = 0;
112 mRollo.mState.save();
113 mVelocity = VelocityTracker.obtain();
114 mVelocity.addMovement(ev);
Joe Onorato1feb3a82009-08-08 22:32:00 -0700115 break;
116 case MotionEvent.ACTION_MOVE:
117 case MotionEvent.ACTION_OUTSIDE:
118 deltaX = x - mLastMotionX;
Joe Onoratod769a632009-08-11 17:09:02 -0700119 mVelocity.addMovement(ev);
120 mRollo.mState.currentScrollX = mLastScrollX;
121 mLastScrollX += deltaX;
122 mRollo.mState.scrollX = mLastScrollX;
Joe Onorato1feb3a82009-08-08 22:32:00 -0700123 mRollo.mState.save();
124 mLastMotionX = x;
125 break;
126 case MotionEvent.ACTION_UP:
127 case MotionEvent.ACTION_CANCEL:
Joe Onoratod769a632009-08-11 17:09:02 -0700128 mVelocity.computeCurrentVelocity(1000 /* px/sec */,
129 mConfig.getScaledMaximumFlingVelocity());
130 mRollo.mState.flingTimeMs = (int)SystemClock.uptimeMillis(); // TODO: use long
131 mRollo.mState.flingVelocityX = (int)mVelocity.getXVelocity();
132 mRollo.mState.save();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700133 mLastMotionX = -10000;
Joe Onoratod769a632009-08-11 17:09:02 -0700134 mVelocity.recycle();
135 mVelocity = null;
Joe Onorato1feb3a82009-08-08 22:32:00 -0700136 break;
Joe Onorato93839052009-08-06 20:34:32 -0700137 }
Joe Onorato1feb3a82009-08-08 22:32:00 -0700138 return true;
Joe Onorato93839052009-08-06 20:34:32 -0700139 }
140
141 @Override
142 public boolean onTrackballEvent(MotionEvent ev)
143 {
144 float x = ev.getX();
145 float y = ev.getY();
146 //Float tx = new Float(x);
147 //Float ty = new Float(y);
148 //Log.e("rs", "tbe " + tx.toString() + ", " + ty.toString());
149
150
151 return true;
152 }
153
154 public class RolloRS {
Joe Onoratobf15cb42009-08-07 14:33:40 -0700155
Joe Onorato1feb3a82009-08-08 22:32:00 -0700156 // Allocations ======
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700157
Joe Onorato93839052009-08-06 20:34:32 -0700158 private int mWidth;
159 private int mHeight;
160
161 private Resources mRes;
Joe Onorato93839052009-08-06 20:34:32 -0700162 private Script mScript;
163 private Sampler mSampler;
164 private Sampler mSamplerText;
165 private ProgramStore mPSBackground;
166 private ProgramStore mPSText;
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700167 private ProgramFragment mPFDebug;
Joe Onorato93839052009-08-06 20:34:32 -0700168 private ProgramFragment mPFImages;
169 private ProgramFragment mPFText;
170 private ProgramVertex mPV;
171 private ProgramVertex.MatrixAllocation mPVAlloc;
172 private ProgramVertex mPVOrtho;
173 private ProgramVertex.MatrixAllocation mPVOrthoAlloc;
Joe Onorato93839052009-08-06 20:34:32 -0700174
Joe Onoratobf15cb42009-08-07 14:33:40 -0700175 private Allocation[] mIcons;
Joe Onorato93839052009-08-06 20:34:32 -0700176 private int[] mAllocIconIDBuf;
177 private Allocation mAllocIconID;
178
Joe Onoratobf15cb42009-08-07 14:33:40 -0700179 private Allocation[] mLabels;
Joe Onorato93839052009-08-06 20:34:32 -0700180 private int[] mAllocLabelIDBuf;
181 private Allocation mAllocLabelID;
182
183 private int[] mAllocScratchBuf;
184 private Allocation mAllocScratch;
185
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700186 Params mParams;
Joe Onorato1feb3a82009-08-08 22:32:00 -0700187 State mState;
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700188
Joe Onoratod769a632009-08-11 17:09:02 -0700189 class Defines {
190 public static final int ALLOC_PARAMS = 0;
191 public static final int ALLOC_STATE = 1;
192 public static final int ALLOC_SCRATCH = 2;
193 public static final int ALLOC_ICON_IDS = 3;
194 public static final int ALLOC_LABEL_IDS = 4;
195 }
196
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700197 class Params extends IntAllocation {
198 Params(RenderScript rs) {
199 super(rs);
200 }
201 @AllocationIndex(0) public int bubbleWidth;
202 @AllocationIndex(1) public int bubbleHeight;
203 @AllocationIndex(2) public int bubbleBitmapWidth;
204 @AllocationIndex(3) public int bubbleBitmapHeight;
205 }
206
Joe Onorato1feb3a82009-08-08 22:32:00 -0700207 class State extends IntAllocation {
208 State(RenderScript rs) {
209 super(rs);
210 }
211 @AllocationIndex(0) public int iconCount;
212 @AllocationIndex(1) public int scrollX;
Joe Onoratod769a632009-08-11 17:09:02 -0700213 @AllocationIndex(2) public int flingTimeMs;
214 @AllocationIndex(3) public int flingVelocityX;
215 @AllocationIndex(4) public int adjustedDeceleration;
216 @AllocationIndex(5) public int currentScrollX;
Joe Onorato1feb3a82009-08-08 22:32:00 -0700217 }
218
219 public RolloRS() {
220 }
221
222 public void init(Resources res, int width, int height) {
223 mRes = res;
224 mWidth = width;
225 mHeight = height;
226 initGl();
227 initData();
228 initRs();
229 }
230
231 private void initGl() {
Joe Onorato93839052009-08-06 20:34:32 -0700232 Sampler.Builder sb = new Sampler.Builder(mRS);
233 sb.setMin(Sampler.Value.LINEAR);//_MIP_LINEAR);
234 sb.setMag(Sampler.Value.LINEAR);
235 sb.setWrapS(Sampler.Value.CLAMP);
236 sb.setWrapT(Sampler.Value.CLAMP);
237 mSampler = sb.create();
238
239 sb.setMin(Sampler.Value.NEAREST);
240 sb.setMag(Sampler.Value.NEAREST);
241 mSamplerText = sb.create();
242
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700243 ProgramFragment.Builder dbg = new ProgramFragment.Builder(mRS, null, null);
244 mPFDebug = dbg.create();
245 mPFDebug.setName("PFDebug");
Joe Onorato93839052009-08-06 20:34:32 -0700246
247 ProgramFragment.Builder bf = new ProgramFragment.Builder(mRS, null, null);
248 bf.setTexEnable(true, 0);
249 bf.setTexEnvMode(ProgramFragment.EnvMode.MODULATE, 0);
250 mPFImages = bf.create();
251 mPFImages.setName("PF");
252 mPFImages.bindSampler(mSampler, 0);
253
254 bf.setTexEnvMode(ProgramFragment.EnvMode.MODULATE, 0);
255 mPFText = bf.create();
256 mPFText.setName("PFText");
257 mPFText.bindSampler(mSamplerText, 0);
258
259 ProgramStore.Builder bs = new ProgramStore.Builder(mRS, null, null);
260 bs.setDepthFunc(ProgramStore.DepthFunc.LESS);
261 bs.setDitherEnable(false);
262 bs.setDepthMask(true);
263 bs.setBlendFunc(ProgramStore.BlendSrcFunc.SRC_ALPHA,
264 ProgramStore.BlendDstFunc.ONE_MINUS_SRC_ALPHA);
265 mPSBackground = bs.create();
266 mPSBackground.setName("PFS");
267
268 bs.setDepthFunc(ProgramStore.DepthFunc.ALWAYS);
269 bs.setDepthMask(false);
270 bs.setBlendFunc(ProgramStore.BlendSrcFunc.SRC_ALPHA,
271 ProgramStore.BlendDstFunc.ONE_MINUS_SRC_ALPHA);
272 mPSText = bs.create();
273 mPSText.setName("PFSText");
274
275 mPVAlloc = new ProgramVertex.MatrixAllocation(mRS);
276 mPVAlloc.setupProjectionNormalized(mWidth, mHeight);
277
278 ProgramVertex.Builder pvb = new ProgramVertex.Builder(mRS, null, null);
279 mPV = pvb.create();
280 mPV.setName("PV");
281 mPV.bindAllocation(mPVAlloc);
282
283 mPVOrthoAlloc = new ProgramVertex.MatrixAllocation(mRS);
284 mPVOrthoAlloc.setupOrthoWindow(mWidth, mHeight);
285
286 pvb.setTextureMatrixEnable(true);
287 mPVOrtho = pvb.create();
288 mPVOrtho.setName("PVOrtho");
289 mPVOrtho.bindAllocation(mPVOrthoAlloc);
290
291 mRS.contextBindProgramVertex(mPV);
292
293 mAllocScratchBuf = new int[32];
Joe Onoratod769a632009-08-11 17:09:02 -0700294 mAllocScratch = Allocation.createSized(mRS, Element.USER_I32, mAllocScratchBuf.length);
Joe Onorato93839052009-08-06 20:34:32 -0700295 mAllocScratch.data(mAllocScratchBuf);
296
297 Log.e("rs", "Done loading named");
Joe Onoratobf15cb42009-08-07 14:33:40 -0700298 }
299
Joe Onorato1feb3a82009-08-08 22:32:00 -0700300 private void initData() {
Joe Onoratod769a632009-08-11 17:09:02 -0700301 final int count = 100;
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700302 mParams = new Params(mRS);
Joe Onorato1feb3a82009-08-08 22:32:00 -0700303 mState = new State(mRS);
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700304
Joe Onoratobf15cb42009-08-07 14:33:40 -0700305 mIcons = new Allocation[count];
306 mAllocIconIDBuf = new int[count];
307 mAllocIconID = Allocation.createSized(mRS,
308 Element.USER_I32, mAllocIconIDBuf.length);
Joe Onorato93839052009-08-06 20:34:32 -0700309
Joe Onoratobf15cb42009-08-07 14:33:40 -0700310 mLabels = new Allocation[count];
311 mAllocLabelIDBuf = new int[mLabels.length];
312 mAllocLabelID = Allocation.createSized(mRS,
313 Element.USER_I32, mLabels.length);
Joe Onorato93839052009-08-06 20:34:32 -0700314
Joe Onoratobf15cb42009-08-07 14:33:40 -0700315 Element ie8888 = Element.RGBA_8888;
Joe Onorato93839052009-08-06 20:34:32 -0700316
Joe Onoratobf15cb42009-08-07 14:33:40 -0700317 final Utilities.BubbleText bubble = new Utilities.BubbleText(getContext());
Joe Onorato93839052009-08-06 20:34:32 -0700318
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700319 mParams.bubbleWidth = bubble.getBubbleWidth();
320 mParams.bubbleHeight = bubble.getMaxBubbleHeight();
321 mParams.bubbleBitmapWidth = bubble.getBitmapWidth();
322 mParams.bubbleBitmapHeight = bubble.getBitmapHeight();
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700323
Joe Onoratobf15cb42009-08-07 14:33:40 -0700324 for (int i=0; i<count; i++) {
325 mIcons[i] = Allocation.createFromBitmapResource(
326 mRS, mRes, R.raw.maps, ie8888, true);
327 mLabels[i] = makeTextBitmap(bubble, i%9==0 ? "Google Maps" : "Maps");
Joe Onorato93839052009-08-06 20:34:32 -0700328 }
329
Joe Onorato1feb3a82009-08-08 22:32:00 -0700330 for (int i=0; i<count; i++) {
331 mIcons[i].uploadToTexture(0);
332 mLabels[i].uploadToTexture(0);
333 mAllocIconIDBuf[i] = mIcons[i].getID();
334 mAllocLabelIDBuf[i] = mLabels[i].getID();
Joe Onoratobf15cb42009-08-07 14:33:40 -0700335 }
336 mAllocIconID.data(mAllocIconIDBuf);
337 mAllocLabelID.data(mAllocLabelIDBuf);
338
Joe Onorato1feb3a82009-08-08 22:32:00 -0700339 mState.iconCount = count;
340
341 mParams.save();
342 mState.save();
Joe Onorato93839052009-08-06 20:34:32 -0700343 }
344
Joe Onoratobf15cb42009-08-07 14:33:40 -0700345 Allocation makeTextBitmap(Utilities.BubbleText bubble, String label) {
346 Bitmap b = bubble.createTextBitmap(label);
347 Allocation a = Allocation.createFromBitmap(mRS, b, Element.RGBA_8888, true);
348 b.recycle();
349 return a;
Joe Onorato93839052009-08-06 20:34:32 -0700350 }
351
Joe Onorato1feb3a82009-08-08 22:32:00 -0700352 private void initRs() {
Joe Onorato93839052009-08-06 20:34:32 -0700353 ScriptC.Builder sb = new ScriptC.Builder(mRS);
354 sb.setScript(mRes, R.raw.rollo);
Joe Onorato93839052009-08-06 20:34:32 -0700355 sb.setRoot(true);
Joe Onoratod769a632009-08-11 17:09:02 -0700356 sb.addDefines(Defines.class);
Joe Onorato93839052009-08-06 20:34:32 -0700357 mScript = sb.create();
358 mScript.setClearColor(0.0f, 0.0f, 0.0f, 0.0f);
359
Joe Onoratod769a632009-08-11 17:09:02 -0700360 mScript.bindAllocation(mParams.getAllocation(), Defines.ALLOC_PARAMS);
361 mScript.bindAllocation(mState.getAllocation(), Defines.ALLOC_STATE);
362 mScript.bindAllocation(mAllocIconID, Defines.ALLOC_ICON_IDS);
363 mScript.bindAllocation(mAllocScratch, Defines.ALLOC_SCRATCH);
364 mScript.bindAllocation(mAllocLabelID, Defines.ALLOC_LABEL_IDS);
Joe Onorato93839052009-08-06 20:34:32 -0700365
366 mRS.contextBindRootScript(mScript);
367 }
368 }
369
370}
371
372
373