blob: 755ba65f4d84e1fbf6b67560333d7f61bc4f107c [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;
Joe Onorato7c312c12009-08-13 21:36:53 -070039import android.database.DataSetObserver;
Joe Onorato93839052009-08-06 20:34:32 -070040import android.graphics.Bitmap;
41import android.graphics.BitmapFactory;
42import android.graphics.Canvas;
43import android.graphics.Paint;
Joe Onorato93839052009-08-06 20:34:32 -070044import android.graphics.drawable.BitmapDrawable;
45import android.graphics.drawable.Drawable;
46import android.os.Handler;
47import android.os.Message;
Joe Onoratod769a632009-08-11 17:09:02 -070048import android.os.SystemClock;
Joe Onorato93839052009-08-06 20:34:32 -070049import android.util.AttributeSet;
50import android.util.Log;
Joe Onoratod769a632009-08-11 17:09:02 -070051import android.view.KeyEvent;
52import android.view.MotionEvent;
Joe Onorato93839052009-08-06 20:34:32 -070053import android.view.Surface;
54import android.view.SurfaceHolder;
55import android.view.SurfaceView;
Joe Onoratod769a632009-08-11 17:09:02 -070056import android.view.VelocityTracker;
57import android.view.ViewConfiguration;
Joe Onorato93839052009-08-06 20:34:32 -070058import android.graphics.PixelFormat;
59
60
61public class AllAppsView extends RSSurfaceView {
Joe Onorato1feb3a82009-08-08 22:32:00 -070062 private RenderScript mRS;
63 private RolloRS mRollo;
64
Joe Onoratod769a632009-08-11 17:09:02 -070065 private ViewConfiguration mConfig;
66 private VelocityTracker mVelocity;
67 private int mLastScrollX;
Joe Onorato1feb3a82009-08-08 22:32:00 -070068 private int mLastMotionX;
Joe Onorato7c312c12009-08-13 21:36:53 -070069 private ApplicationsAdapter mAdapter;
Joe Onorato1feb3a82009-08-08 22:32:00 -070070
Joe Onorato7c312c12009-08-13 21:36:53 -070071
72 public AllAppsView(Context context, AttributeSet attrs) {
73 super(context, attrs);
Joe Onorato93839052009-08-06 20:34:32 -070074 setFocusable(true);
75 getHolder().setFormat(PixelFormat.TRANSLUCENT);
Joe Onoratod769a632009-08-11 17:09:02 -070076 mConfig = ViewConfiguration.get(context);
Joe Onorato93839052009-08-06 20:34:32 -070077 }
78
Joe Onorato7c312c12009-08-13 21:36:53 -070079 public AllAppsView(Context context, AttributeSet attrs, int defStyle) {
80 this(context, attrs);
Joe Onorato93839052009-08-06 20:34:32 -070081 }
82
Joe Onorato7c312c12009-08-13 21:36:53 -070083 void setAdapter(ApplicationsAdapter adapter) {
84 if (mAdapter != null) {
85 mAdapter.unregisterDataSetObserver(mIconObserver);
86 }
87 mAdapter = adapter;
88 if (adapter != null) {
89 adapter.registerDataSetObserver(mIconObserver);
90 }
Joe Onorato93839052009-08-06 20:34:32 -070091 }
92
Joe Onorato1feb3a82009-08-08 22:32:00 -070093 @Override
Joe Onorato93839052009-08-06 20:34:32 -070094 public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
95 super.surfaceChanged(holder, format, w, h);
96
97 mRS = createRenderScript();
Joe Onorato1feb3a82009-08-08 22:32:00 -070098 mRollo = new RolloRS();
99 mRollo.init(getResources(), w, h);
Joe Onorato93839052009-08-06 20:34:32 -0700100 }
101
102 @Override
103 public boolean onKeyDown(int keyCode, KeyEvent event)
104 {
Joe Onorato93839052009-08-06 20:34:32 -0700105 // this method doesn't work when 'extends View' include 'extends ScrollView'.
106 return super.onKeyDown(keyCode, event);
107 }
108
Joe Onorato93839052009-08-06 20:34:32 -0700109 @Override
110 public boolean onTouchEvent(MotionEvent ev)
111 {
Joe Onorato7c312c12009-08-13 21:36:53 -0700112 Log.d(Launcher.LOG_TAG, "onTouchEvent " + ev);
Joe Onorato1feb3a82009-08-08 22:32:00 -0700113 int x = (int)ev.getX();
114 int deltaX;
115 switch (ev.getAction()) {
116 case MotionEvent.ACTION_DOWN:
117 mLastMotionX = x;
Joe Onoratod769a632009-08-11 17:09:02 -0700118 mRollo.mState.read();
119 mRollo.mState.scrollX = mLastScrollX = mRollo.mState.currentScrollX;
120 mRollo.mState.flingVelocityX = 0;
121 mRollo.mState.adjustedDeceleration = 0;
122 mRollo.mState.save();
123 mVelocity = VelocityTracker.obtain();
124 mVelocity.addMovement(ev);
Joe Onorato1feb3a82009-08-08 22:32:00 -0700125 break;
126 case MotionEvent.ACTION_MOVE:
127 case MotionEvent.ACTION_OUTSIDE:
128 deltaX = x - mLastMotionX;
Joe Onoratod769a632009-08-11 17:09:02 -0700129 mVelocity.addMovement(ev);
130 mRollo.mState.currentScrollX = mLastScrollX;
131 mLastScrollX += deltaX;
132 mRollo.mState.scrollX = mLastScrollX;
Joe Onorato1feb3a82009-08-08 22:32:00 -0700133 mRollo.mState.save();
134 mLastMotionX = x;
135 break;
136 case MotionEvent.ACTION_UP:
137 case MotionEvent.ACTION_CANCEL:
Joe Onoratod769a632009-08-11 17:09:02 -0700138 mVelocity.computeCurrentVelocity(1000 /* px/sec */,
139 mConfig.getScaledMaximumFlingVelocity());
140 mRollo.mState.flingTimeMs = (int)SystemClock.uptimeMillis(); // TODO: use long
141 mRollo.mState.flingVelocityX = (int)mVelocity.getXVelocity();
142 mRollo.mState.save();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700143 mLastMotionX = -10000;
Joe Onoratod769a632009-08-11 17:09:02 -0700144 mVelocity.recycle();
145 mVelocity = null;
Joe Onorato1feb3a82009-08-08 22:32:00 -0700146 break;
Joe Onorato93839052009-08-06 20:34:32 -0700147 }
Joe Onorato1feb3a82009-08-08 22:32:00 -0700148 return true;
Joe Onorato93839052009-08-06 20:34:32 -0700149 }
150
151 @Override
152 public boolean onTrackballEvent(MotionEvent ev)
153 {
154 float x = ev.getX();
155 float y = ev.getY();
156 //Float tx = new Float(x);
157 //Float ty = new Float(y);
158 //Log.e("rs", "tbe " + tx.toString() + ", " + ty.toString());
159
160
161 return true;
162 }
163
Joe Onorato7c312c12009-08-13 21:36:53 -0700164 DataSetObserver mIconObserver = new DataSetObserver() {
165 public void onChanged() {
166 Log.d(Launcher.LOG_TAG, "new icons arrived! now have " + mAdapter.getCount());
167 }
168 };
169
Joe Onorato93839052009-08-06 20:34:32 -0700170 public class RolloRS {
Joe Onoratobf15cb42009-08-07 14:33:40 -0700171
Joe Onorato1feb3a82009-08-08 22:32:00 -0700172 // Allocations ======
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700173
Joe Onorato93839052009-08-06 20:34:32 -0700174 private int mWidth;
175 private int mHeight;
176
177 private Resources mRes;
Joe Onorato93839052009-08-06 20:34:32 -0700178 private Script mScript;
179 private Sampler mSampler;
180 private Sampler mSamplerText;
181 private ProgramStore mPSBackground;
182 private ProgramStore mPSText;
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700183 private ProgramFragment mPFDebug;
Joe Onorato93839052009-08-06 20:34:32 -0700184 private ProgramFragment mPFImages;
185 private ProgramFragment mPFText;
186 private ProgramVertex mPV;
187 private ProgramVertex.MatrixAllocation mPVAlloc;
188 private ProgramVertex mPVOrtho;
189 private ProgramVertex.MatrixAllocation mPVOrthoAlloc;
Joe Onorato93839052009-08-06 20:34:32 -0700190
Joe Onoratobf15cb42009-08-07 14:33:40 -0700191 private Allocation[] mIcons;
Joe Onorato93839052009-08-06 20:34:32 -0700192 private int[] mAllocIconIDBuf;
193 private Allocation mAllocIconID;
194
Joe Onoratobf15cb42009-08-07 14:33:40 -0700195 private Allocation[] mLabels;
Joe Onorato93839052009-08-06 20:34:32 -0700196 private int[] mAllocLabelIDBuf;
197 private Allocation mAllocLabelID;
198
199 private int[] mAllocScratchBuf;
200 private Allocation mAllocScratch;
201
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700202 Params mParams;
Joe Onorato1feb3a82009-08-08 22:32:00 -0700203 State mState;
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700204
Joe Onoratod769a632009-08-11 17:09:02 -0700205 class Defines {
206 public static final int ALLOC_PARAMS = 0;
207 public static final int ALLOC_STATE = 1;
208 public static final int ALLOC_SCRATCH = 2;
209 public static final int ALLOC_ICON_IDS = 3;
210 public static final int ALLOC_LABEL_IDS = 4;
211 }
212
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700213 class Params extends IntAllocation {
214 Params(RenderScript rs) {
215 super(rs);
216 }
217 @AllocationIndex(0) public int bubbleWidth;
218 @AllocationIndex(1) public int bubbleHeight;
219 @AllocationIndex(2) public int bubbleBitmapWidth;
220 @AllocationIndex(3) public int bubbleBitmapHeight;
221 }
222
Joe Onorato1feb3a82009-08-08 22:32:00 -0700223 class State extends IntAllocation {
224 State(RenderScript rs) {
225 super(rs);
226 }
227 @AllocationIndex(0) public int iconCount;
228 @AllocationIndex(1) public int scrollX;
Joe Onoratod769a632009-08-11 17:09:02 -0700229 @AllocationIndex(2) public int flingTimeMs;
230 @AllocationIndex(3) public int flingVelocityX;
231 @AllocationIndex(4) public int adjustedDeceleration;
232 @AllocationIndex(5) public int currentScrollX;
Joe Onoratoeb2c02e2009-08-12 21:40:52 -0700233 @AllocationIndex(6) public int flingDuration;
234 @AllocationIndex(7) public int flingEndPos;
Joe Onorato1feb3a82009-08-08 22:32:00 -0700235 }
236
237 public RolloRS() {
238 }
239
240 public void init(Resources res, int width, int height) {
241 mRes = res;
242 mWidth = width;
243 mHeight = height;
244 initGl();
245 initData();
246 initRs();
247 }
248
249 private void initGl() {
Joe Onorato93839052009-08-06 20:34:32 -0700250 Sampler.Builder sb = new Sampler.Builder(mRS);
251 sb.setMin(Sampler.Value.LINEAR);//_MIP_LINEAR);
252 sb.setMag(Sampler.Value.LINEAR);
253 sb.setWrapS(Sampler.Value.CLAMP);
254 sb.setWrapT(Sampler.Value.CLAMP);
255 mSampler = sb.create();
256
257 sb.setMin(Sampler.Value.NEAREST);
258 sb.setMag(Sampler.Value.NEAREST);
259 mSamplerText = sb.create();
260
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700261 ProgramFragment.Builder dbg = new ProgramFragment.Builder(mRS, null, null);
262 mPFDebug = dbg.create();
263 mPFDebug.setName("PFDebug");
Joe Onorato93839052009-08-06 20:34:32 -0700264
265 ProgramFragment.Builder bf = new ProgramFragment.Builder(mRS, null, null);
266 bf.setTexEnable(true, 0);
267 bf.setTexEnvMode(ProgramFragment.EnvMode.MODULATE, 0);
268 mPFImages = bf.create();
269 mPFImages.setName("PF");
270 mPFImages.bindSampler(mSampler, 0);
271
272 bf.setTexEnvMode(ProgramFragment.EnvMode.MODULATE, 0);
273 mPFText = bf.create();
274 mPFText.setName("PFText");
275 mPFText.bindSampler(mSamplerText, 0);
276
277 ProgramStore.Builder bs = new ProgramStore.Builder(mRS, null, null);
278 bs.setDepthFunc(ProgramStore.DepthFunc.LESS);
279 bs.setDitherEnable(false);
280 bs.setDepthMask(true);
281 bs.setBlendFunc(ProgramStore.BlendSrcFunc.SRC_ALPHA,
282 ProgramStore.BlendDstFunc.ONE_MINUS_SRC_ALPHA);
283 mPSBackground = bs.create();
284 mPSBackground.setName("PFS");
285
286 bs.setDepthFunc(ProgramStore.DepthFunc.ALWAYS);
287 bs.setDepthMask(false);
288 bs.setBlendFunc(ProgramStore.BlendSrcFunc.SRC_ALPHA,
289 ProgramStore.BlendDstFunc.ONE_MINUS_SRC_ALPHA);
290 mPSText = bs.create();
291 mPSText.setName("PFSText");
292
293 mPVAlloc = new ProgramVertex.MatrixAllocation(mRS);
294 mPVAlloc.setupProjectionNormalized(mWidth, mHeight);
295
296 ProgramVertex.Builder pvb = new ProgramVertex.Builder(mRS, null, null);
297 mPV = pvb.create();
298 mPV.setName("PV");
299 mPV.bindAllocation(mPVAlloc);
300
301 mPVOrthoAlloc = new ProgramVertex.MatrixAllocation(mRS);
302 mPVOrthoAlloc.setupOrthoWindow(mWidth, mHeight);
303
304 pvb.setTextureMatrixEnable(true);
305 mPVOrtho = pvb.create();
306 mPVOrtho.setName("PVOrtho");
307 mPVOrtho.bindAllocation(mPVOrthoAlloc);
308
309 mRS.contextBindProgramVertex(mPV);
310
311 mAllocScratchBuf = new int[32];
Joe Onoratod769a632009-08-11 17:09:02 -0700312 mAllocScratch = Allocation.createSized(mRS, Element.USER_I32, mAllocScratchBuf.length);
Joe Onorato93839052009-08-06 20:34:32 -0700313 mAllocScratch.data(mAllocScratchBuf);
314
315 Log.e("rs", "Done loading named");
Joe Onoratobf15cb42009-08-07 14:33:40 -0700316 }
317
Joe Onorato1feb3a82009-08-08 22:32:00 -0700318 private void initData() {
Joe Onoratod769a632009-08-11 17:09:02 -0700319 final int count = 100;
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700320 mParams = new Params(mRS);
Joe Onorato1feb3a82009-08-08 22:32:00 -0700321 mState = new State(mRS);
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700322
Joe Onoratobf15cb42009-08-07 14:33:40 -0700323 mIcons = new Allocation[count];
324 mAllocIconIDBuf = new int[count];
325 mAllocIconID = Allocation.createSized(mRS,
326 Element.USER_I32, mAllocIconIDBuf.length);
Joe Onorato93839052009-08-06 20:34:32 -0700327
Joe Onoratobf15cb42009-08-07 14:33:40 -0700328 mLabels = new Allocation[count];
329 mAllocLabelIDBuf = new int[mLabels.length];
330 mAllocLabelID = Allocation.createSized(mRS,
331 Element.USER_I32, mLabels.length);
Joe Onorato93839052009-08-06 20:34:32 -0700332
Joe Onoratobf15cb42009-08-07 14:33:40 -0700333 Element ie8888 = Element.RGBA_8888;
Joe Onorato93839052009-08-06 20:34:32 -0700334
Joe Onoratobf15cb42009-08-07 14:33:40 -0700335 final Utilities.BubbleText bubble = new Utilities.BubbleText(getContext());
Joe Onorato93839052009-08-06 20:34:32 -0700336
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700337 mParams.bubbleWidth = bubble.getBubbleWidth();
338 mParams.bubbleHeight = bubble.getMaxBubbleHeight();
339 mParams.bubbleBitmapWidth = bubble.getBitmapWidth();
340 mParams.bubbleBitmapHeight = bubble.getBitmapHeight();
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700341
Joe Onoratobf15cb42009-08-07 14:33:40 -0700342 for (int i=0; i<count; i++) {
343 mIcons[i] = Allocation.createFromBitmapResource(
344 mRS, mRes, R.raw.maps, ie8888, true);
345 mLabels[i] = makeTextBitmap(bubble, i%9==0 ? "Google Maps" : "Maps");
Joe Onorato93839052009-08-06 20:34:32 -0700346 }
347
Joe Onorato1feb3a82009-08-08 22:32:00 -0700348 for (int i=0; i<count; i++) {
349 mIcons[i].uploadToTexture(0);
350 mLabels[i].uploadToTexture(0);
351 mAllocIconIDBuf[i] = mIcons[i].getID();
352 mAllocLabelIDBuf[i] = mLabels[i].getID();
Joe Onoratobf15cb42009-08-07 14:33:40 -0700353 }
354 mAllocIconID.data(mAllocIconIDBuf);
355 mAllocLabelID.data(mAllocLabelIDBuf);
356
Joe Onorato1feb3a82009-08-08 22:32:00 -0700357 mState.iconCount = count;
358
359 mParams.save();
360 mState.save();
Joe Onorato93839052009-08-06 20:34:32 -0700361 }
362
Joe Onoratobf15cb42009-08-07 14:33:40 -0700363 Allocation makeTextBitmap(Utilities.BubbleText bubble, String label) {
364 Bitmap b = bubble.createTextBitmap(label);
365 Allocation a = Allocation.createFromBitmap(mRS, b, Element.RGBA_8888, true);
366 b.recycle();
367 return a;
Joe Onorato93839052009-08-06 20:34:32 -0700368 }
369
Joe Onorato1feb3a82009-08-08 22:32:00 -0700370 private void initRs() {
Joe Onorato93839052009-08-06 20:34:32 -0700371 ScriptC.Builder sb = new ScriptC.Builder(mRS);
372 sb.setScript(mRes, R.raw.rollo);
Joe Onorato93839052009-08-06 20:34:32 -0700373 sb.setRoot(true);
Joe Onoratod769a632009-08-11 17:09:02 -0700374 sb.addDefines(Defines.class);
Joe Onorato93839052009-08-06 20:34:32 -0700375 mScript = sb.create();
376 mScript.setClearColor(0.0f, 0.0f, 0.0f, 0.0f);
377
Joe Onoratod769a632009-08-11 17:09:02 -0700378 mScript.bindAllocation(mParams.getAllocation(), Defines.ALLOC_PARAMS);
379 mScript.bindAllocation(mState.getAllocation(), Defines.ALLOC_STATE);
380 mScript.bindAllocation(mAllocIconID, Defines.ALLOC_ICON_IDS);
381 mScript.bindAllocation(mAllocScratch, Defines.ALLOC_SCRATCH);
382 mScript.bindAllocation(mAllocLabelID, Defines.ALLOC_LABEL_IDS);
Joe Onorato93839052009-08-06 20:34:32 -0700383
384 mRS.contextBindRootScript(mScript);
385 }
386 }
387
388}
389
390
391