Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.launcher2; |
| 18 | |
| 19 | import java.io.Writer; |
| 20 | import java.util.ArrayList; |
| 21 | import java.util.concurrent.Semaphore; |
| 22 | import java.lang.Float; |
| 23 | |
| 24 | import android.renderscript.RSSurfaceView; |
| 25 | import android.renderscript.RenderScript; |
| 26 | |
| 27 | import android.renderscript.RenderScript; |
| 28 | import android.renderscript.ProgramVertex; |
| 29 | import android.renderscript.Element; |
| 30 | import android.renderscript.Allocation; |
| 31 | import android.renderscript.Script; |
| 32 | import android.renderscript.ScriptC; |
| 33 | import android.renderscript.ProgramFragment; |
| 34 | import android.renderscript.ProgramStore; |
| 35 | import android.renderscript.Sampler; |
| 36 | |
| 37 | import android.content.Context; |
| 38 | import android.content.res.Resources; |
| 39 | import android.graphics.Bitmap; |
| 40 | import android.graphics.BitmapFactory; |
| 41 | import android.graphics.Canvas; |
| 42 | import android.graphics.Paint; |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 43 | import android.graphics.drawable.BitmapDrawable; |
| 44 | import android.graphics.drawable.Drawable; |
| 45 | import android.os.Handler; |
| 46 | import android.os.Message; |
| 47 | import android.util.AttributeSet; |
| 48 | import android.util.Log; |
| 49 | import android.view.Surface; |
| 50 | import android.view.SurfaceHolder; |
| 51 | import android.view.SurfaceView; |
| 52 | import android.view.KeyEvent; |
| 53 | import android.view.MotionEvent; |
| 54 | import android.graphics.PixelFormat; |
| 55 | |
| 56 | |
| 57 | public 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 Onorato | bf15cb4 | 2009-08-07 14:33:40 -0700 | [diff] [blame^] | 234 | |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 235 | //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 Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 246 | 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 Onorato | bf15cb4 | 2009-08-07 14:33:40 -0700 | [diff] [blame^] | 255 | initIcons(29); |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 256 | 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 Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 300 | |
| 301 | private int[] mAllocStateBuf; |
| 302 | private Allocation mAllocState; |
| 303 | |
Joe Onorato | bf15cb4 | 2009-08-07 14:33:40 -0700 | [diff] [blame^] | 304 | private Allocation[] mIcons; |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 305 | private int[] mAllocIconIDBuf; |
| 306 | private Allocation mAllocIconID; |
| 307 | |
Joe Onorato | bf15cb4 | 2009-08-07 14:33:40 -0700 | [diff] [blame^] | 308 | private Allocation[] mLabels; |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 309 | 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 Onorato | bf15cb4 | 2009-08-07 14:33:40 -0700 | [diff] [blame^] | 380 | } |
| 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 Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 387 | |
Joe Onorato | bf15cb4 | 2009-08-07 14:33:40 -0700 | [diff] [blame^] | 388 | mLabels = new Allocation[count]; |
| 389 | mAllocLabelIDBuf = new int[mLabels.length]; |
| 390 | mAllocLabelID = Allocation.createSized(mRS, |
| 391 | Element.USER_I32, mLabels.length); |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 392 | |
Joe Onorato | bf15cb4 | 2009-08-07 14:33:40 -0700 | [diff] [blame^] | 393 | Element ie8888 = Element.RGBA_8888; |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 394 | |
Joe Onorato | bf15cb4 | 2009-08-07 14:33:40 -0700 | [diff] [blame^] | 395 | final Utilities.BubbleText bubble = new Utilities.BubbleText(getContext()); |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 396 | |
Joe Onorato | bf15cb4 | 2009-08-07 14:33:40 -0700 | [diff] [blame^] | 397 | 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 Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 401 | } |
| 402 | |
Joe Onorato | bf15cb4 | 2009-08-07 14:33:40 -0700 | [diff] [blame^] | 403 | 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 Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 413 | } |
| 414 | |
Joe Onorato | bf15cb4 | 2009-08-07 14:33:40 -0700 | [diff] [blame^] | 415 | 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 Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 420 | } |
| 421 | |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 422 | 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 | |