blob: 8c0b83d8377d626da8cd8588d57792ace0bd1fb0 [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 Onoratoeb2c02e2009-08-12 21:40:52 -0700217 @AllocationIndex(6) public int flingDuration;
218 @AllocationIndex(7) public int flingEndPos;
Joe Onorato1feb3a82009-08-08 22:32:00 -0700219 }
220
221 public RolloRS() {
222 }
223
224 public void init(Resources res, int width, int height) {
225 mRes = res;
226 mWidth = width;
227 mHeight = height;
228 initGl();
229 initData();
230 initRs();
231 }
232
233 private void initGl() {
Joe Onorato93839052009-08-06 20:34:32 -0700234 Sampler.Builder sb = new Sampler.Builder(mRS);
235 sb.setMin(Sampler.Value.LINEAR);//_MIP_LINEAR);
236 sb.setMag(Sampler.Value.LINEAR);
237 sb.setWrapS(Sampler.Value.CLAMP);
238 sb.setWrapT(Sampler.Value.CLAMP);
239 mSampler = sb.create();
240
241 sb.setMin(Sampler.Value.NEAREST);
242 sb.setMag(Sampler.Value.NEAREST);
243 mSamplerText = sb.create();
244
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700245 ProgramFragment.Builder dbg = new ProgramFragment.Builder(mRS, null, null);
246 mPFDebug = dbg.create();
247 mPFDebug.setName("PFDebug");
Joe Onorato93839052009-08-06 20:34:32 -0700248
249 ProgramFragment.Builder bf = new ProgramFragment.Builder(mRS, null, null);
250 bf.setTexEnable(true, 0);
251 bf.setTexEnvMode(ProgramFragment.EnvMode.MODULATE, 0);
252 mPFImages = bf.create();
253 mPFImages.setName("PF");
254 mPFImages.bindSampler(mSampler, 0);
255
256 bf.setTexEnvMode(ProgramFragment.EnvMode.MODULATE, 0);
257 mPFText = bf.create();
258 mPFText.setName("PFText");
259 mPFText.bindSampler(mSamplerText, 0);
260
261 ProgramStore.Builder bs = new ProgramStore.Builder(mRS, null, null);
262 bs.setDepthFunc(ProgramStore.DepthFunc.LESS);
263 bs.setDitherEnable(false);
264 bs.setDepthMask(true);
265 bs.setBlendFunc(ProgramStore.BlendSrcFunc.SRC_ALPHA,
266 ProgramStore.BlendDstFunc.ONE_MINUS_SRC_ALPHA);
267 mPSBackground = bs.create();
268 mPSBackground.setName("PFS");
269
270 bs.setDepthFunc(ProgramStore.DepthFunc.ALWAYS);
271 bs.setDepthMask(false);
272 bs.setBlendFunc(ProgramStore.BlendSrcFunc.SRC_ALPHA,
273 ProgramStore.BlendDstFunc.ONE_MINUS_SRC_ALPHA);
274 mPSText = bs.create();
275 mPSText.setName("PFSText");
276
277 mPVAlloc = new ProgramVertex.MatrixAllocation(mRS);
278 mPVAlloc.setupProjectionNormalized(mWidth, mHeight);
279
280 ProgramVertex.Builder pvb = new ProgramVertex.Builder(mRS, null, null);
281 mPV = pvb.create();
282 mPV.setName("PV");
283 mPV.bindAllocation(mPVAlloc);
284
285 mPVOrthoAlloc = new ProgramVertex.MatrixAllocation(mRS);
286 mPVOrthoAlloc.setupOrthoWindow(mWidth, mHeight);
287
288 pvb.setTextureMatrixEnable(true);
289 mPVOrtho = pvb.create();
290 mPVOrtho.setName("PVOrtho");
291 mPVOrtho.bindAllocation(mPVOrthoAlloc);
292
293 mRS.contextBindProgramVertex(mPV);
294
295 mAllocScratchBuf = new int[32];
Joe Onoratod769a632009-08-11 17:09:02 -0700296 mAllocScratch = Allocation.createSized(mRS, Element.USER_I32, mAllocScratchBuf.length);
Joe Onorato93839052009-08-06 20:34:32 -0700297 mAllocScratch.data(mAllocScratchBuf);
298
299 Log.e("rs", "Done loading named");
Joe Onoratobf15cb42009-08-07 14:33:40 -0700300 }
301
Joe Onorato1feb3a82009-08-08 22:32:00 -0700302 private void initData() {
Joe Onoratod769a632009-08-11 17:09:02 -0700303 final int count = 100;
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700304 mParams = new Params(mRS);
Joe Onorato1feb3a82009-08-08 22:32:00 -0700305 mState = new State(mRS);
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700306
Joe Onoratobf15cb42009-08-07 14:33:40 -0700307 mIcons = new Allocation[count];
308 mAllocIconIDBuf = new int[count];
309 mAllocIconID = Allocation.createSized(mRS,
310 Element.USER_I32, mAllocIconIDBuf.length);
Joe Onorato93839052009-08-06 20:34:32 -0700311
Joe Onoratobf15cb42009-08-07 14:33:40 -0700312 mLabels = new Allocation[count];
313 mAllocLabelIDBuf = new int[mLabels.length];
314 mAllocLabelID = Allocation.createSized(mRS,
315 Element.USER_I32, mLabels.length);
Joe Onorato93839052009-08-06 20:34:32 -0700316
Joe Onoratobf15cb42009-08-07 14:33:40 -0700317 Element ie8888 = Element.RGBA_8888;
Joe Onorato93839052009-08-06 20:34:32 -0700318
Joe Onoratobf15cb42009-08-07 14:33:40 -0700319 final Utilities.BubbleText bubble = new Utilities.BubbleText(getContext());
Joe Onorato93839052009-08-06 20:34:32 -0700320
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700321 mParams.bubbleWidth = bubble.getBubbleWidth();
322 mParams.bubbleHeight = bubble.getMaxBubbleHeight();
323 mParams.bubbleBitmapWidth = bubble.getBitmapWidth();
324 mParams.bubbleBitmapHeight = bubble.getBitmapHeight();
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700325
Joe Onoratobf15cb42009-08-07 14:33:40 -0700326 for (int i=0; i<count; i++) {
327 mIcons[i] = Allocation.createFromBitmapResource(
328 mRS, mRes, R.raw.maps, ie8888, true);
329 mLabels[i] = makeTextBitmap(bubble, i%9==0 ? "Google Maps" : "Maps");
Joe Onorato93839052009-08-06 20:34:32 -0700330 }
331
Joe Onorato1feb3a82009-08-08 22:32:00 -0700332 for (int i=0; i<count; i++) {
333 mIcons[i].uploadToTexture(0);
334 mLabels[i].uploadToTexture(0);
335 mAllocIconIDBuf[i] = mIcons[i].getID();
336 mAllocLabelIDBuf[i] = mLabels[i].getID();
Joe Onoratobf15cb42009-08-07 14:33:40 -0700337 }
338 mAllocIconID.data(mAllocIconIDBuf);
339 mAllocLabelID.data(mAllocLabelIDBuf);
340
Joe Onorato1feb3a82009-08-08 22:32:00 -0700341 mState.iconCount = count;
342
343 mParams.save();
344 mState.save();
Joe Onorato93839052009-08-06 20:34:32 -0700345 }
346
Joe Onoratobf15cb42009-08-07 14:33:40 -0700347 Allocation makeTextBitmap(Utilities.BubbleText bubble, String label) {
348 Bitmap b = bubble.createTextBitmap(label);
349 Allocation a = Allocation.createFromBitmap(mRS, b, Element.RGBA_8888, true);
350 b.recycle();
351 return a;
Joe Onorato93839052009-08-06 20:34:32 -0700352 }
353
Joe Onorato1feb3a82009-08-08 22:32:00 -0700354 private void initRs() {
Joe Onorato93839052009-08-06 20:34:32 -0700355 ScriptC.Builder sb = new ScriptC.Builder(mRS);
356 sb.setScript(mRes, R.raw.rollo);
Joe Onorato93839052009-08-06 20:34:32 -0700357 sb.setRoot(true);
Joe Onoratod769a632009-08-11 17:09:02 -0700358 sb.addDefines(Defines.class);
Joe Onorato93839052009-08-06 20:34:32 -0700359 mScript = sb.create();
360 mScript.setClearColor(0.0f, 0.0f, 0.0f, 0.0f);
361
Joe Onoratod769a632009-08-11 17:09:02 -0700362 mScript.bindAllocation(mParams.getAllocation(), Defines.ALLOC_PARAMS);
363 mScript.bindAllocation(mState.getAllocation(), Defines.ALLOC_STATE);
364 mScript.bindAllocation(mAllocIconID, Defines.ALLOC_ICON_IDS);
365 mScript.bindAllocation(mAllocScratch, Defines.ALLOC_SCRATCH);
366 mScript.bindAllocation(mAllocLabelID, Defines.ALLOC_LABEL_IDS);
Joe Onorato93839052009-08-06 20:34:32 -0700367
368 mRS.contextBindRootScript(mScript);
369 }
370 }
371
372}
373
374
375