blob: 78e3f1a22c1044a3ed4524dbac98930f67add1b5 [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 {
Joe Onorato1feb3a82009-08-08 22:32:00 -070058 private RenderScript mRS;
59 private RolloRS mRollo;
60
61 private int mLastMotionX;
62
Joe Onorato93839052009-08-06 20:34:32 -070063 public AllAppsView(Context context) {
64 super(context);
65 setFocusable(true);
66 getHolder().setFormat(PixelFormat.TRANSLUCENT);
67 }
68
69 public AllAppsView(Context context, AttributeSet attrs) {
70 this(context);
71 }
72
73 public AllAppsView(Context context, AttributeSet attrs, int defStyle) {
74 this(context);
75 }
76
Joe Onorato1feb3a82009-08-08 22:32:00 -070077 @Override
Joe Onorato93839052009-08-06 20:34:32 -070078 public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
79 super.surfaceChanged(holder, format, w, h);
80
81 mRS = createRenderScript();
Joe Onorato1feb3a82009-08-08 22:32:00 -070082 mRollo = new RolloRS();
83 mRollo.init(getResources(), w, h);
Joe Onorato93839052009-08-06 20:34:32 -070084 }
85
86 @Override
87 public boolean onKeyDown(int keyCode, KeyEvent event)
88 {
Joe Onorato93839052009-08-06 20:34:32 -070089 // this method doesn't work when 'extends View' include 'extends ScrollView'.
90 return super.onKeyDown(keyCode, event);
91 }
92
Joe Onorato93839052009-08-06 20:34:32 -070093 @Override
94 public boolean onTouchEvent(MotionEvent ev)
95 {
Joe Onorato1feb3a82009-08-08 22:32:00 -070096 int x = (int)ev.getX();
97 int deltaX;
98 switch (ev.getAction()) {
99 case MotionEvent.ACTION_DOWN:
100 mLastMotionX = x;
101 break;
102 case MotionEvent.ACTION_MOVE:
103 case MotionEvent.ACTION_OUTSIDE:
104 deltaX = x - mLastMotionX;
105 mRollo.mState.scrollX += deltaX;
106 Log.d(Launcher.LOG_TAG, "updated scrollX=" + mRollo.mState.scrollX);
107 mRollo.mState.save();
108 mLastMotionX = x;
109 break;
110 case MotionEvent.ACTION_UP:
111 case MotionEvent.ACTION_CANCEL:
112 mLastMotionX = -10000;
113 break;
Joe Onorato93839052009-08-06 20:34:32 -0700114 }
Joe Onorato1feb3a82009-08-08 22:32:00 -0700115 return true;
Joe Onorato93839052009-08-06 20:34:32 -0700116 }
117
118 @Override
119 public boolean onTrackballEvent(MotionEvent ev)
120 {
121 float x = ev.getX();
122 float y = ev.getY();
123 //Float tx = new Float(x);
124 //Float ty = new Float(y);
125 //Log.e("rs", "tbe " + tx.toString() + ", " + ty.toString());
126
127
128 return true;
129 }
130
131 public class RolloRS {
Joe Onoratobf15cb42009-08-07 14:33:40 -0700132
Joe Onorato1feb3a82009-08-08 22:32:00 -0700133 // Allocations ======
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700134 static final int ALLOC_PARAMS = 0;
135 static final int ALLOC_STATE = 1;
136 static final int ALLOC_SCRATCH = 2;
137 static final int ALLOC_ICON_IDS = 3;
138 static final int ALLOC_LABEL_IDS = 4;
139
Joe Onorato93839052009-08-06 20:34:32 -0700140 private int mWidth;
141 private int mHeight;
142
143 private Resources mRes;
Joe Onorato93839052009-08-06 20:34:32 -0700144 private Script mScript;
145 private Sampler mSampler;
146 private Sampler mSamplerText;
147 private ProgramStore mPSBackground;
148 private ProgramStore mPSText;
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700149 private ProgramFragment mPFDebug;
Joe Onorato93839052009-08-06 20:34:32 -0700150 private ProgramFragment mPFImages;
151 private ProgramFragment mPFText;
152 private ProgramVertex mPV;
153 private ProgramVertex.MatrixAllocation mPVAlloc;
154 private ProgramVertex mPVOrtho;
155 private ProgramVertex.MatrixAllocation mPVOrthoAlloc;
Joe Onorato93839052009-08-06 20:34:32 -0700156
Joe Onoratobf15cb42009-08-07 14:33:40 -0700157 private Allocation[] mIcons;
Joe Onorato93839052009-08-06 20:34:32 -0700158 private int[] mAllocIconIDBuf;
159 private Allocation mAllocIconID;
160
Joe Onoratobf15cb42009-08-07 14:33:40 -0700161 private Allocation[] mLabels;
Joe Onorato93839052009-08-06 20:34:32 -0700162 private int[] mAllocLabelIDBuf;
163 private Allocation mAllocLabelID;
164
165 private int[] mAllocScratchBuf;
166 private Allocation mAllocScratch;
167
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700168 Params mParams;
Joe Onorato1feb3a82009-08-08 22:32:00 -0700169 State mState;
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700170
171 class Params extends IntAllocation {
172 Params(RenderScript rs) {
173 super(rs);
174 }
175 @AllocationIndex(0) public int bubbleWidth;
176 @AllocationIndex(1) public int bubbleHeight;
177 @AllocationIndex(2) public int bubbleBitmapWidth;
178 @AllocationIndex(3) public int bubbleBitmapHeight;
179 }
180
Joe Onorato1feb3a82009-08-08 22:32:00 -0700181 class State extends IntAllocation {
182 State(RenderScript rs) {
183 super(rs);
184 }
185 @AllocationIndex(0) public int iconCount;
186 @AllocationIndex(1) public int scrollX;
187 }
188
189 public RolloRS() {
190 }
191
192 public void init(Resources res, int width, int height) {
193 mRes = res;
194 mWidth = width;
195 mHeight = height;
196 initGl();
197 initData();
198 initRs();
199 }
200
201 private void initGl() {
Joe Onorato93839052009-08-06 20:34:32 -0700202 Sampler.Builder sb = new Sampler.Builder(mRS);
203 sb.setMin(Sampler.Value.LINEAR);//_MIP_LINEAR);
204 sb.setMag(Sampler.Value.LINEAR);
205 sb.setWrapS(Sampler.Value.CLAMP);
206 sb.setWrapT(Sampler.Value.CLAMP);
207 mSampler = sb.create();
208
209 sb.setMin(Sampler.Value.NEAREST);
210 sb.setMag(Sampler.Value.NEAREST);
211 mSamplerText = sb.create();
212
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700213 ProgramFragment.Builder dbg = new ProgramFragment.Builder(mRS, null, null);
214 mPFDebug = dbg.create();
215 mPFDebug.setName("PFDebug");
Joe Onorato93839052009-08-06 20:34:32 -0700216
217 ProgramFragment.Builder bf = new ProgramFragment.Builder(mRS, null, null);
218 bf.setTexEnable(true, 0);
219 bf.setTexEnvMode(ProgramFragment.EnvMode.MODULATE, 0);
220 mPFImages = bf.create();
221 mPFImages.setName("PF");
222 mPFImages.bindSampler(mSampler, 0);
223
224 bf.setTexEnvMode(ProgramFragment.EnvMode.MODULATE, 0);
225 mPFText = bf.create();
226 mPFText.setName("PFText");
227 mPFText.bindSampler(mSamplerText, 0);
228
229 ProgramStore.Builder bs = new ProgramStore.Builder(mRS, null, null);
230 bs.setDepthFunc(ProgramStore.DepthFunc.LESS);
231 bs.setDitherEnable(false);
232 bs.setDepthMask(true);
233 bs.setBlendFunc(ProgramStore.BlendSrcFunc.SRC_ALPHA,
234 ProgramStore.BlendDstFunc.ONE_MINUS_SRC_ALPHA);
235 mPSBackground = bs.create();
236 mPSBackground.setName("PFS");
237
238 bs.setDepthFunc(ProgramStore.DepthFunc.ALWAYS);
239 bs.setDepthMask(false);
240 bs.setBlendFunc(ProgramStore.BlendSrcFunc.SRC_ALPHA,
241 ProgramStore.BlendDstFunc.ONE_MINUS_SRC_ALPHA);
242 mPSText = bs.create();
243 mPSText.setName("PFSText");
244
245 mPVAlloc = new ProgramVertex.MatrixAllocation(mRS);
246 mPVAlloc.setupProjectionNormalized(mWidth, mHeight);
247
248 ProgramVertex.Builder pvb = new ProgramVertex.Builder(mRS, null, null);
249 mPV = pvb.create();
250 mPV.setName("PV");
251 mPV.bindAllocation(mPVAlloc);
252
253 mPVOrthoAlloc = new ProgramVertex.MatrixAllocation(mRS);
254 mPVOrthoAlloc.setupOrthoWindow(mWidth, mHeight);
255
256 pvb.setTextureMatrixEnable(true);
257 mPVOrtho = pvb.create();
258 mPVOrtho.setName("PVOrtho");
259 mPVOrtho.bindAllocation(mPVOrthoAlloc);
260
261 mRS.contextBindProgramVertex(mPV);
262
263 mAllocScratchBuf = new int[32];
264 mAllocScratch = Allocation.createSized(mRS,
265 Element.USER_I32, mAllocScratchBuf.length);
266 mAllocScratch.data(mAllocScratchBuf);
267
268 Log.e("rs", "Done loading named");
Joe Onoratobf15cb42009-08-07 14:33:40 -0700269 }
270
Joe Onorato1feb3a82009-08-08 22:32:00 -0700271 private void initData() {
272 final int count = 29;
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700273 mParams = new Params(mRS);
Joe Onorato1feb3a82009-08-08 22:32:00 -0700274 mState = new State(mRS);
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700275
Joe Onoratobf15cb42009-08-07 14:33:40 -0700276 mIcons = new Allocation[count];
277 mAllocIconIDBuf = new int[count];
278 mAllocIconID = Allocation.createSized(mRS,
279 Element.USER_I32, mAllocIconIDBuf.length);
Joe Onorato93839052009-08-06 20:34:32 -0700280
Joe Onoratobf15cb42009-08-07 14:33:40 -0700281 mLabels = new Allocation[count];
282 mAllocLabelIDBuf = new int[mLabels.length];
283 mAllocLabelID = Allocation.createSized(mRS,
284 Element.USER_I32, mLabels.length);
Joe Onorato93839052009-08-06 20:34:32 -0700285
Joe Onoratobf15cb42009-08-07 14:33:40 -0700286 Element ie8888 = Element.RGBA_8888;
Joe Onorato93839052009-08-06 20:34:32 -0700287
Joe Onoratobf15cb42009-08-07 14:33:40 -0700288 final Utilities.BubbleText bubble = new Utilities.BubbleText(getContext());
Joe Onorato93839052009-08-06 20:34:32 -0700289
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700290 mParams.bubbleWidth = bubble.getBubbleWidth();
291 mParams.bubbleHeight = bubble.getMaxBubbleHeight();
292 mParams.bubbleBitmapWidth = bubble.getBitmapWidth();
293 mParams.bubbleBitmapHeight = bubble.getBitmapHeight();
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700294
Joe Onoratobf15cb42009-08-07 14:33:40 -0700295 for (int i=0; i<count; i++) {
296 mIcons[i] = Allocation.createFromBitmapResource(
297 mRS, mRes, R.raw.maps, ie8888, true);
298 mLabels[i] = makeTextBitmap(bubble, i%9==0 ? "Google Maps" : "Maps");
Joe Onorato93839052009-08-06 20:34:32 -0700299 }
300
Joe Onorato1feb3a82009-08-08 22:32:00 -0700301 for (int i=0; i<count; i++) {
302 mIcons[i].uploadToTexture(0);
303 mLabels[i].uploadToTexture(0);
304 mAllocIconIDBuf[i] = mIcons[i].getID();
305 mAllocLabelIDBuf[i] = mLabels[i].getID();
Joe Onoratobf15cb42009-08-07 14:33:40 -0700306 }
307 mAllocIconID.data(mAllocIconIDBuf);
308 mAllocLabelID.data(mAllocLabelIDBuf);
309
Joe Onorato1feb3a82009-08-08 22:32:00 -0700310 mState.iconCount = count;
311
312 mParams.save();
313 mState.save();
Joe Onorato93839052009-08-06 20:34:32 -0700314 }
315
Joe Onoratobf15cb42009-08-07 14:33:40 -0700316 Allocation makeTextBitmap(Utilities.BubbleText bubble, String label) {
317 Bitmap b = bubble.createTextBitmap(label);
318 Allocation a = Allocation.createFromBitmap(mRS, b, Element.RGBA_8888, true);
319 b.recycle();
320 return a;
Joe Onorato93839052009-08-06 20:34:32 -0700321 }
322
Joe Onorato1feb3a82009-08-08 22:32:00 -0700323 private void initRs() {
Joe Onorato93839052009-08-06 20:34:32 -0700324 ScriptC.Builder sb = new ScriptC.Builder(mRS);
325 sb.setScript(mRes, R.raw.rollo);
Joe Onorato93839052009-08-06 20:34:32 -0700326 sb.setRoot(true);
327 mScript = sb.create();
328 mScript.setClearColor(0.0f, 0.0f, 0.0f, 0.0f);
329
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700330 mScript.bindAllocation(mParams.getAllocation(), ALLOC_PARAMS);
Joe Onorato1feb3a82009-08-08 22:32:00 -0700331 mScript.bindAllocation(mState.getAllocation(), ALLOC_STATE);
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700332 mScript.bindAllocation(mAllocIconID, ALLOC_ICON_IDS);
333 mScript.bindAllocation(mAllocScratch, ALLOC_SCRATCH);
334 mScript.bindAllocation(mAllocLabelID, ALLOC_LABEL_IDS);
Joe Onorato93839052009-08-06 20:34:32 -0700335
336 mRS.contextBindRootScript(mScript);
337 }
338 }
339
340}
341
342
343