blob: 0a522ae5ff87151906d33fac68ab18b42ffec486 [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;
47import android.util.AttributeSet;
48import android.util.Log;
49import android.view.Surface;
50import android.view.SurfaceHolder;
51import android.view.SurfaceView;
52import android.view.KeyEvent;
53import android.view.MotionEvent;
54import android.graphics.PixelFormat;
55
56
57public class AllAppsView extends RSSurfaceView {
58 public AllAppsView(Context context) {
59 super(context);
60 setFocusable(true);
61 getHolder().setFormat(PixelFormat.TRANSLUCENT);
62 }
63
64 public AllAppsView(Context context, AttributeSet attrs) {
65 this(context);
66 }
67
68 public AllAppsView(Context context, AttributeSet attrs, int defStyle) {
69 this(context);
70 }
71
72 private RenderScript mRS;
73 private RolloRS mRender;
74
75 public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
76 super.surfaceChanged(holder, format, w, h);
77
78 mRS = createRenderScript();
79 mRender = new RolloRS();
80 mRender.init(mRS, getResources(), w, h);
81 }
82
83 @Override
84 public boolean onKeyDown(int keyCode, KeyEvent event)
85 {
86 // break point at here
87 // this method doesn't work when 'extends View' include 'extends ScrollView'.
88 return super.onKeyDown(keyCode, event);
89 }
90
91 boolean mControlMode = false;
92 boolean mZoomMode = false;
93 boolean mFlingMode = false;
94 float mFlingX = 0;
95 float mFlingY = 0;
96 float mColumn = -1;
97 float mOldColumn;
98 float mZoom = 1;
99
100 int mIconCount = 29;
101 int mRows = 4;
102 int mColumns = (mIconCount + mRows - 1) / mRows;
103
104 float mMaxZoom = ((float)mColumns) / 3.f;
105
106
107 void setColumn(boolean clamp)
108 {
109 //Log.e("rs", " col = " + Float.toString(mColumn));
110 float c = mColumn;
111 if(c > (mColumns -2)) {
112 c = (mColumns -2);
113 }
114 if(c < 0) {
115 c = 0;
116 }
117 mRender.setPosition(c);
118 if(clamp) {
119 mColumn = c;
120 }
121 }
122
123 void computeSelection(float x, float y)
124 {
125 float col = mColumn + (x - 0.5f) * 4 + 1.25f;
126 int iCol = (int)(col + 0.25f);
127
128 float row = (y / 0.8f) * mRows;
129 int iRow = (int)(row - 0.5f);
130
131 mRender.setSelected(iCol * mRows + iRow);
132 }
133
134 @Override
135 public boolean onTouchEvent(MotionEvent ev)
136 {
137 boolean ret = true;
138 int act = ev.getAction();
139 if (act == ev.ACTION_UP) {
140 ret = false;
141 }
142
143 float nx = ev.getX() / getWidth();
144 float ny = ev.getY() / getHeight();
145
146 //Log.e("rs", "width=" + Float.toString(getWidth()));
147 //Log.e("rs", "height=" + Float.toString(getHeight()));
148
149 mRender.setTouch(ret);
150
151 if((ny > 0.85f) || mControlMode) {
152 mFlingMode = false;
153
154 // Projector control
155 if((nx > 0.2f) && (nx < 0.8f) || mControlMode) {
156 if(act != ev.ACTION_UP) {
157 float zoom = mMaxZoom;
158 if(mControlMode) {
159 if(!mZoomMode) {
160 zoom = 1.f;
161 }
162 float dx = nx - mFlingX;
163
164 if((ny < 0.9) && mZoomMode) {
165 zoom = mMaxZoom - ((0.9f - ny) * 10.f);
166 if(zoom < 1) {
167 zoom = 1;
168 mZoomMode = false;
169 }
170 mOldColumn = mColumn;
171 }
172 mColumn += dx * 4;// * zoom;
173 if(zoom > 1.01f) {
174 mColumn += (mZoom - zoom) * (nx - 0.5f) * 4 * zoom;
175 }
176 } else {
177 mOldColumn = mColumn;
178 mColumn = ((float)mColumns) / 2;
179 mControlMode = true;
180 mZoomMode = true;
181 }
182 mZoom = zoom;
183 mFlingX = nx;
184 mRender.setZoom(zoom);
185 if(mZoom < 1.01f) {
186 computeSelection(nx, ny);
187 }
188 } else {
189 mControlMode = false;
190 mColumn = mOldColumn;
191 mRender.setZoom(1.f);
192 mRender.setSelected(-1);
193 }
194 } else {
195 // Do something with corners here....
196 }
197 setColumn(true);
198
199 } else {
200 // icon control
201 if(act != ev.ACTION_UP) {
202 if(mFlingMode) {
203 mColumn += (mFlingX - nx) * 4;
204 setColumn(true);
205 }
206 mFlingMode = true;
207 mFlingX = nx;
208 mFlingY = ny;
209 } else {
210 mFlingMode = false;
211 mColumn = (float)(java.lang.Math.floor(mColumn * 0.25f + 0.3f) * 4.f) + 1.f;
212 setColumn(true);
213 }
214 }
215
216
217 return ret;
218 }
219
220 @Override
221 public boolean onTrackballEvent(MotionEvent ev)
222 {
223 float x = ev.getX();
224 float y = ev.getY();
225 //Float tx = new Float(x);
226 //Float ty = new Float(y);
227 //Log.e("rs", "tbe " + tx.toString() + ", " + ty.toString());
228
229
230 return true;
231 }
232
233 public class RolloRS {
Joe Onoratobf15cb42009-08-07 14:33:40 -0700234
Joe Onorato93839052009-08-06 20:34:32 -0700235 //public static final int STATE_SELECTED_ID = 0;
236 public static final int STATE_DONE = 1;
237 //public static final int STATE_PRESSURE = 2;
238 public static final int STATE_ZOOM = 3;
239 //public static final int STATE_WARP = 4;
240 public static final int STATE_ORIENTATION = 5;
241 public static final int STATE_SELECTION = 6;
242 public static final int STATE_FIRST_VISIBLE = 7;
243 public static final int STATE_COUNT = 8;
244 public static final int STATE_TOUCH = 9;
245
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700246 static final int ALLOC_PARAMS = 0;
247 static final int ALLOC_STATE = 1;
248 static final int ALLOC_SCRATCH = 2;
249 static final int ALLOC_ICON_IDS = 3;
250 static final int ALLOC_LABEL_IDS = 4;
251
Joe Onorato93839052009-08-06 20:34:32 -0700252 public RolloRS() {
253 }
254
255 public void init(RenderScript rs, Resources res, int width, int height) {
256 mRS = rs;
257 mRes = res;
258 mWidth = width;
259 mHeight = height;
260 initNamed();
Joe Onoratobf15cb42009-08-07 14:33:40 -0700261 initIcons(29);
Joe Onorato93839052009-08-06 20:34:32 -0700262 initRS();
263 }
264
265 public void setPosition(float column) {
266 mAllocStateBuf[STATE_FIRST_VISIBLE] = (int)(column * (-20));
267 mAllocState.data(mAllocStateBuf);
268 }
269
270 public void setTouch(boolean touch) {
271 mAllocStateBuf[STATE_TOUCH] = touch ? 1 : 0;
272 mAllocState.data(mAllocStateBuf);
273 }
274
275 public void setZoom(float z) {
276 //Log.e("rs", "zoom " + Float.toString(z));
277
278 mAllocStateBuf[STATE_ZOOM] = (int)(z * 1000.f);
279 mAllocState.data(mAllocStateBuf);
280 }
281
282 public void setSelected(int index) {
283 //Log.e("rs", "setSelected " + Integer.toString(index));
284
285 mAllocStateBuf[STATE_SELECTION] = index;
286 mAllocStateBuf[STATE_DONE] = 1;
287 mAllocState.data(mAllocStateBuf);
288 }
289
290 private int mWidth;
291 private int mHeight;
292
293 private Resources mRes;
294 private RenderScript mRS;
295 private Script mScript;
296 private Sampler mSampler;
297 private Sampler mSamplerText;
298 private ProgramStore mPSBackground;
299 private ProgramStore mPSText;
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700300 private ProgramFragment mPFDebug;
Joe Onorato93839052009-08-06 20:34:32 -0700301 private ProgramFragment mPFImages;
302 private ProgramFragment mPFText;
303 private ProgramVertex mPV;
304 private ProgramVertex.MatrixAllocation mPVAlloc;
305 private ProgramVertex mPVOrtho;
306 private ProgramVertex.MatrixAllocation mPVOrthoAlloc;
Joe Onorato93839052009-08-06 20:34:32 -0700307
308 private int[] mAllocStateBuf;
309 private Allocation mAllocState;
310
Joe Onoratobf15cb42009-08-07 14:33:40 -0700311 private Allocation[] mIcons;
Joe Onorato93839052009-08-06 20:34:32 -0700312 private int[] mAllocIconIDBuf;
313 private Allocation mAllocIconID;
314
Joe Onoratobf15cb42009-08-07 14:33:40 -0700315 private Allocation[] mLabels;
Joe Onorato93839052009-08-06 20:34:32 -0700316 private int[] mAllocLabelIDBuf;
317 private Allocation mAllocLabelID;
318
319 private int[] mAllocScratchBuf;
320 private Allocation mAllocScratch;
321
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700322 Params mParams;
323
324 class Params extends IntAllocation {
325 Params(RenderScript rs) {
326 super(rs);
327 }
328 @AllocationIndex(0) public int bubbleWidth;
329 @AllocationIndex(1) public int bubbleHeight;
330 @AllocationIndex(2) public int bubbleBitmapWidth;
331 @AllocationIndex(3) public int bubbleBitmapHeight;
332 }
333
Joe Onorato93839052009-08-06 20:34:32 -0700334 private void initNamed() {
335 Sampler.Builder sb = new Sampler.Builder(mRS);
336 sb.setMin(Sampler.Value.LINEAR);//_MIP_LINEAR);
337 sb.setMag(Sampler.Value.LINEAR);
338 sb.setWrapS(Sampler.Value.CLAMP);
339 sb.setWrapT(Sampler.Value.CLAMP);
340 mSampler = sb.create();
341
342 sb.setMin(Sampler.Value.NEAREST);
343 sb.setMag(Sampler.Value.NEAREST);
344 mSamplerText = sb.create();
345
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700346 ProgramFragment.Builder dbg = new ProgramFragment.Builder(mRS, null, null);
347 mPFDebug = dbg.create();
348 mPFDebug.setName("PFDebug");
Joe Onorato93839052009-08-06 20:34:32 -0700349
350 ProgramFragment.Builder bf = new ProgramFragment.Builder(mRS, null, null);
351 bf.setTexEnable(true, 0);
352 bf.setTexEnvMode(ProgramFragment.EnvMode.MODULATE, 0);
353 mPFImages = bf.create();
354 mPFImages.setName("PF");
355 mPFImages.bindSampler(mSampler, 0);
356
357 bf.setTexEnvMode(ProgramFragment.EnvMode.MODULATE, 0);
358 mPFText = bf.create();
359 mPFText.setName("PFText");
360 mPFText.bindSampler(mSamplerText, 0);
361
362 ProgramStore.Builder bs = new ProgramStore.Builder(mRS, null, null);
363 bs.setDepthFunc(ProgramStore.DepthFunc.LESS);
364 bs.setDitherEnable(false);
365 bs.setDepthMask(true);
366 bs.setBlendFunc(ProgramStore.BlendSrcFunc.SRC_ALPHA,
367 ProgramStore.BlendDstFunc.ONE_MINUS_SRC_ALPHA);
368 mPSBackground = bs.create();
369 mPSBackground.setName("PFS");
370
371 bs.setDepthFunc(ProgramStore.DepthFunc.ALWAYS);
372 bs.setDepthMask(false);
373 bs.setBlendFunc(ProgramStore.BlendSrcFunc.SRC_ALPHA,
374 ProgramStore.BlendDstFunc.ONE_MINUS_SRC_ALPHA);
375 mPSText = bs.create();
376 mPSText.setName("PFSText");
377
378 mPVAlloc = new ProgramVertex.MatrixAllocation(mRS);
379 mPVAlloc.setupProjectionNormalized(mWidth, mHeight);
380
381 ProgramVertex.Builder pvb = new ProgramVertex.Builder(mRS, null, null);
382 mPV = pvb.create();
383 mPV.setName("PV");
384 mPV.bindAllocation(mPVAlloc);
385
386 mPVOrthoAlloc = new ProgramVertex.MatrixAllocation(mRS);
387 mPVOrthoAlloc.setupOrthoWindow(mWidth, mHeight);
388
389 pvb.setTextureMatrixEnable(true);
390 mPVOrtho = pvb.create();
391 mPVOrtho.setName("PVOrtho");
392 mPVOrtho.bindAllocation(mPVOrthoAlloc);
393
394 mRS.contextBindProgramVertex(mPV);
395
396 mAllocScratchBuf = new int[32];
397 mAllocScratch = Allocation.createSized(mRS,
398 Element.USER_I32, mAllocScratchBuf.length);
399 mAllocScratch.data(mAllocScratchBuf);
400
401 Log.e("rs", "Done loading named");
Joe Onoratobf15cb42009-08-07 14:33:40 -0700402 }
403
404 private void initIcons(int count) {
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700405 mParams = new Params(mRS);
406
Joe Onoratobf15cb42009-08-07 14:33:40 -0700407 mIcons = new Allocation[count];
408 mAllocIconIDBuf = new int[count];
409 mAllocIconID = Allocation.createSized(mRS,
410 Element.USER_I32, mAllocIconIDBuf.length);
Joe Onorato93839052009-08-06 20:34:32 -0700411
Joe Onoratobf15cb42009-08-07 14:33:40 -0700412 mLabels = new Allocation[count];
413 mAllocLabelIDBuf = new int[mLabels.length];
414 mAllocLabelID = Allocation.createSized(mRS,
415 Element.USER_I32, mLabels.length);
Joe Onorato93839052009-08-06 20:34:32 -0700416
Joe Onoratobf15cb42009-08-07 14:33:40 -0700417 Element ie8888 = Element.RGBA_8888;
Joe Onorato93839052009-08-06 20:34:32 -0700418
Joe Onoratobf15cb42009-08-07 14:33:40 -0700419 final Utilities.BubbleText bubble = new Utilities.BubbleText(getContext());
Joe Onorato93839052009-08-06 20:34:32 -0700420
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700421 mParams.bubbleWidth = bubble.getBubbleWidth();
422 mParams.bubbleHeight = bubble.getMaxBubbleHeight();
423 mParams.bubbleBitmapWidth = bubble.getBitmapWidth();
424 mParams.bubbleBitmapHeight = bubble.getBitmapHeight();
425 mParams.save();
426
Joe Onoratobf15cb42009-08-07 14:33:40 -0700427 for (int i=0; i<count; i++) {
428 mIcons[i] = Allocation.createFromBitmapResource(
429 mRS, mRes, R.raw.maps, ie8888, true);
430 mLabels[i] = makeTextBitmap(bubble, i%9==0 ? "Google Maps" : "Maps");
Joe Onorato93839052009-08-06 20:34:32 -0700431 }
432
Joe Onoratobf15cb42009-08-07 14:33:40 -0700433 for(int ct=0; ct < count; ct++) {
434 mIcons[ct].uploadToTexture(0);
435 mLabels[ct].uploadToTexture(0);
436 mAllocIconIDBuf[ct] = mIcons[ct].getID();
437 mAllocLabelIDBuf[ct] = mLabels[ct].getID();
438 }
439 mAllocIconID.data(mAllocIconIDBuf);
440 mAllocLabelID.data(mAllocLabelIDBuf);
441
442 Log.e("rs", "Done loading icons");
Joe Onorato93839052009-08-06 20:34:32 -0700443 }
444
Joe Onoratobf15cb42009-08-07 14:33:40 -0700445 Allocation makeTextBitmap(Utilities.BubbleText bubble, String label) {
446 Bitmap b = bubble.createTextBitmap(label);
447 Allocation a = Allocation.createFromBitmap(mRS, b, Element.RGBA_8888, true);
448 b.recycle();
449 return a;
Joe Onorato93839052009-08-06 20:34:32 -0700450 }
451
Joe Onorato93839052009-08-06 20:34:32 -0700452 private void initRS() {
453 ScriptC.Builder sb = new ScriptC.Builder(mRS);
454 sb.setScript(mRes, R.raw.rollo);
455 //sb.setScript(mRes, R.raw.rollo2);
456 sb.setRoot(true);
457 mScript = sb.create();
458 mScript.setClearColor(0.0f, 0.0f, 0.0f, 0.0f);
459
460 mAllocStateBuf = new int[] {0, 0, 0, 8, 0, 0, -1, 0, mAllocIconIDBuf.length, 0, 0};
461 mAllocState = Allocation.createSized(mRS,
462 Element.USER_I32, mAllocStateBuf.length);
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700463
464 mScript.bindAllocation(mParams.getAllocation(), ALLOC_PARAMS);
465 mScript.bindAllocation(mAllocState, ALLOC_STATE);
466 mScript.bindAllocation(mAllocIconID, ALLOC_ICON_IDS);
467 mScript.bindAllocation(mAllocScratch, ALLOC_SCRATCH);
468 mScript.bindAllocation(mAllocLabelID, ALLOC_LABEL_IDS);
Joe Onorato93839052009-08-06 20:34:32 -0700469 setPosition(0);
470 setZoom(1);
471
472 //RenderScript.File f = mRS.fileOpen("/sdcard/test.a3d");
473
474 mRS.contextBindRootScript(mScript);
475 }
476 }
477
478}
479
480
481