blob: 7bf0c7c6b68ab8a9cd33f7b3b74efca27b944d83 [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 Onorato93839052009-08-06 20:34:32 -0700246 public RolloRS() {
247 }
248
249 public void init(RenderScript rs, Resources res, int width, int height) {
250 mRS = rs;
251 mRes = res;
252 mWidth = width;
253 mHeight = height;
254 initNamed();
Joe Onoratobf15cb42009-08-07 14:33:40 -0700255 initIcons(29);
Joe Onorato93839052009-08-06 20:34:32 -0700256 initRS();
257 }
258
259 public void setPosition(float column) {
260 mAllocStateBuf[STATE_FIRST_VISIBLE] = (int)(column * (-20));
261 mAllocState.data(mAllocStateBuf);
262 }
263
264 public void setTouch(boolean touch) {
265 mAllocStateBuf[STATE_TOUCH] = touch ? 1 : 0;
266 mAllocState.data(mAllocStateBuf);
267 }
268
269 public void setZoom(float z) {
270 //Log.e("rs", "zoom " + Float.toString(z));
271
272 mAllocStateBuf[STATE_ZOOM] = (int)(z * 1000.f);
273 mAllocState.data(mAllocStateBuf);
274 }
275
276 public void setSelected(int index) {
277 //Log.e("rs", "setSelected " + Integer.toString(index));
278
279 mAllocStateBuf[STATE_SELECTION] = index;
280 mAllocStateBuf[STATE_DONE] = 1;
281 mAllocState.data(mAllocStateBuf);
282 }
283
284 private int mWidth;
285 private int mHeight;
286
287 private Resources mRes;
288 private RenderScript mRS;
289 private Script mScript;
290 private Sampler mSampler;
291 private Sampler mSamplerText;
292 private ProgramStore mPSBackground;
293 private ProgramStore mPSText;
294 private ProgramFragment mPFImages;
295 private ProgramFragment mPFText;
296 private ProgramVertex mPV;
297 private ProgramVertex.MatrixAllocation mPVAlloc;
298 private ProgramVertex mPVOrtho;
299 private ProgramVertex.MatrixAllocation mPVOrthoAlloc;
Joe Onorato93839052009-08-06 20:34:32 -0700300
301 private int[] mAllocStateBuf;
302 private Allocation mAllocState;
303
Joe Onoratobf15cb42009-08-07 14:33:40 -0700304 private Allocation[] mIcons;
Joe Onorato93839052009-08-06 20:34:32 -0700305 private int[] mAllocIconIDBuf;
306 private Allocation mAllocIconID;
307
Joe Onoratobf15cb42009-08-07 14:33:40 -0700308 private Allocation[] mLabels;
Joe Onorato93839052009-08-06 20:34:32 -0700309 private int[] mAllocLabelIDBuf;
310 private Allocation mAllocLabelID;
311
312 private int[] mAllocScratchBuf;
313 private Allocation mAllocScratch;
314
315 private void initNamed() {
316 Sampler.Builder sb = new Sampler.Builder(mRS);
317 sb.setMin(Sampler.Value.LINEAR);//_MIP_LINEAR);
318 sb.setMag(Sampler.Value.LINEAR);
319 sb.setWrapS(Sampler.Value.CLAMP);
320 sb.setWrapT(Sampler.Value.CLAMP);
321 mSampler = sb.create();
322
323 sb.setMin(Sampler.Value.NEAREST);
324 sb.setMag(Sampler.Value.NEAREST);
325 mSamplerText = sb.create();
326
327
328 ProgramFragment.Builder bf = new ProgramFragment.Builder(mRS, null, null);
329 bf.setTexEnable(true, 0);
330 bf.setTexEnvMode(ProgramFragment.EnvMode.MODULATE, 0);
331 mPFImages = bf.create();
332 mPFImages.setName("PF");
333 mPFImages.bindSampler(mSampler, 0);
334
335 bf.setTexEnvMode(ProgramFragment.EnvMode.MODULATE, 0);
336 mPFText = bf.create();
337 mPFText.setName("PFText");
338 mPFText.bindSampler(mSamplerText, 0);
339
340 ProgramStore.Builder bs = new ProgramStore.Builder(mRS, null, null);
341 bs.setDepthFunc(ProgramStore.DepthFunc.LESS);
342 bs.setDitherEnable(false);
343 bs.setDepthMask(true);
344 bs.setBlendFunc(ProgramStore.BlendSrcFunc.SRC_ALPHA,
345 ProgramStore.BlendDstFunc.ONE_MINUS_SRC_ALPHA);
346 mPSBackground = bs.create();
347 mPSBackground.setName("PFS");
348
349 bs.setDepthFunc(ProgramStore.DepthFunc.ALWAYS);
350 bs.setDepthMask(false);
351 bs.setBlendFunc(ProgramStore.BlendSrcFunc.SRC_ALPHA,
352 ProgramStore.BlendDstFunc.ONE_MINUS_SRC_ALPHA);
353 mPSText = bs.create();
354 mPSText.setName("PFSText");
355
356 mPVAlloc = new ProgramVertex.MatrixAllocation(mRS);
357 mPVAlloc.setupProjectionNormalized(mWidth, mHeight);
358
359 ProgramVertex.Builder pvb = new ProgramVertex.Builder(mRS, null, null);
360 mPV = pvb.create();
361 mPV.setName("PV");
362 mPV.bindAllocation(mPVAlloc);
363
364 mPVOrthoAlloc = new ProgramVertex.MatrixAllocation(mRS);
365 mPVOrthoAlloc.setupOrthoWindow(mWidth, mHeight);
366
367 pvb.setTextureMatrixEnable(true);
368 mPVOrtho = pvb.create();
369 mPVOrtho.setName("PVOrtho");
370 mPVOrtho.bindAllocation(mPVOrthoAlloc);
371
372 mRS.contextBindProgramVertex(mPV);
373
374 mAllocScratchBuf = new int[32];
375 mAllocScratch = Allocation.createSized(mRS,
376 Element.USER_I32, mAllocScratchBuf.length);
377 mAllocScratch.data(mAllocScratchBuf);
378
379 Log.e("rs", "Done loading named");
Joe Onoratobf15cb42009-08-07 14:33:40 -0700380 }
381
382 private void initIcons(int count) {
383 mIcons = new Allocation[count];
384 mAllocIconIDBuf = new int[count];
385 mAllocIconID = Allocation.createSized(mRS,
386 Element.USER_I32, mAllocIconIDBuf.length);
Joe Onorato93839052009-08-06 20:34:32 -0700387
Joe Onoratobf15cb42009-08-07 14:33:40 -0700388 mLabels = new Allocation[count];
389 mAllocLabelIDBuf = new int[mLabels.length];
390 mAllocLabelID = Allocation.createSized(mRS,
391 Element.USER_I32, mLabels.length);
Joe Onorato93839052009-08-06 20:34:32 -0700392
Joe Onoratobf15cb42009-08-07 14:33:40 -0700393 Element ie8888 = Element.RGBA_8888;
Joe Onorato93839052009-08-06 20:34:32 -0700394
Joe Onoratobf15cb42009-08-07 14:33:40 -0700395 final Utilities.BubbleText bubble = new Utilities.BubbleText(getContext());
Joe Onorato93839052009-08-06 20:34:32 -0700396
Joe Onoratobf15cb42009-08-07 14:33:40 -0700397 for (int i=0; i<count; i++) {
398 mIcons[i] = Allocation.createFromBitmapResource(
399 mRS, mRes, R.raw.maps, ie8888, true);
400 mLabels[i] = makeTextBitmap(bubble, i%9==0 ? "Google Maps" : "Maps");
Joe Onorato93839052009-08-06 20:34:32 -0700401 }
402
Joe Onoratobf15cb42009-08-07 14:33:40 -0700403 for(int ct=0; ct < count; ct++) {
404 mIcons[ct].uploadToTexture(0);
405 mLabels[ct].uploadToTexture(0);
406 mAllocIconIDBuf[ct] = mIcons[ct].getID();
407 mAllocLabelIDBuf[ct] = mLabels[ct].getID();
408 }
409 mAllocIconID.data(mAllocIconIDBuf);
410 mAllocLabelID.data(mAllocLabelIDBuf);
411
412 Log.e("rs", "Done loading icons");
Joe Onorato93839052009-08-06 20:34:32 -0700413 }
414
Joe Onoratobf15cb42009-08-07 14:33:40 -0700415 Allocation makeTextBitmap(Utilities.BubbleText bubble, String label) {
416 Bitmap b = bubble.createTextBitmap(label);
417 Allocation a = Allocation.createFromBitmap(mRS, b, Element.RGBA_8888, true);
418 b.recycle();
419 return a;
Joe Onorato93839052009-08-06 20:34:32 -0700420 }
421
Joe Onorato93839052009-08-06 20:34:32 -0700422 private void initRS() {
423 ScriptC.Builder sb = new ScriptC.Builder(mRS);
424 sb.setScript(mRes, R.raw.rollo);
425 //sb.setScript(mRes, R.raw.rollo2);
426 sb.setRoot(true);
427 mScript = sb.create();
428 mScript.setClearColor(0.0f, 0.0f, 0.0f, 0.0f);
429
430 mAllocStateBuf = new int[] {0, 0, 0, 8, 0, 0, -1, 0, mAllocIconIDBuf.length, 0, 0};
431 mAllocState = Allocation.createSized(mRS,
432 Element.USER_I32, mAllocStateBuf.length);
433 mScript.bindAllocation(mAllocState, 0);
434 mScript.bindAllocation(mAllocIconID, 1);
435 mScript.bindAllocation(mAllocScratch, 2);
436 mScript.bindAllocation(mAllocLabelID, 3);
437 setPosition(0);
438 setZoom(1);
439
440 //RenderScript.File f = mRS.fileOpen("/sdcard/test.a3d");
441
442 mRS.contextBindRootScript(mScript);
443 }
444 }
445
446}
447
448
449