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 { |
Joe Onorato | 1feb3a8 | 2009-08-08 22:32:00 -0700 | [diff] [blame^] | 58 | private RenderScript mRS; |
| 59 | private RolloRS mRollo; |
| 60 | |
| 61 | private int mLastMotionX; |
| 62 | |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 63 | 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 Onorato | 1feb3a8 | 2009-08-08 22:32:00 -0700 | [diff] [blame^] | 77 | @Override |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 78 | public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) { |
| 79 | super.surfaceChanged(holder, format, w, h); |
| 80 | |
| 81 | mRS = createRenderScript(); |
Joe Onorato | 1feb3a8 | 2009-08-08 22:32:00 -0700 | [diff] [blame^] | 82 | mRollo = new RolloRS(); |
| 83 | mRollo.init(getResources(), w, h); |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | @Override |
| 87 | public boolean onKeyDown(int keyCode, KeyEvent event) |
| 88 | { |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 89 | // this method doesn't work when 'extends View' include 'extends ScrollView'. |
| 90 | return super.onKeyDown(keyCode, event); |
| 91 | } |
| 92 | |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 93 | @Override |
| 94 | public boolean onTouchEvent(MotionEvent ev) |
| 95 | { |
Joe Onorato | 1feb3a8 | 2009-08-08 22:32:00 -0700 | [diff] [blame^] | 96 | 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 Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 114 | } |
Joe Onorato | 1feb3a8 | 2009-08-08 22:32:00 -0700 | [diff] [blame^] | 115 | return true; |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 116 | } |
| 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 Onorato | bf15cb4 | 2009-08-07 14:33:40 -0700 | [diff] [blame] | 132 | |
Joe Onorato | 1feb3a8 | 2009-08-08 22:32:00 -0700 | [diff] [blame^] | 133 | // Allocations ====== |
Joe Onorato | 43e7bcf | 2009-08-08 18:53:53 -0700 | [diff] [blame] | 134 | 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 Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 140 | private int mWidth; |
| 141 | private int mHeight; |
| 142 | |
| 143 | private Resources mRes; |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 144 | private Script mScript; |
| 145 | private Sampler mSampler; |
| 146 | private Sampler mSamplerText; |
| 147 | private ProgramStore mPSBackground; |
| 148 | private ProgramStore mPSText; |
Joe Onorato | 43e7bcf | 2009-08-08 18:53:53 -0700 | [diff] [blame] | 149 | private ProgramFragment mPFDebug; |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 150 | 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 Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 156 | |
Joe Onorato | bf15cb4 | 2009-08-07 14:33:40 -0700 | [diff] [blame] | 157 | private Allocation[] mIcons; |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 158 | private int[] mAllocIconIDBuf; |
| 159 | private Allocation mAllocIconID; |
| 160 | |
Joe Onorato | bf15cb4 | 2009-08-07 14:33:40 -0700 | [diff] [blame] | 161 | private Allocation[] mLabels; |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 162 | private int[] mAllocLabelIDBuf; |
| 163 | private Allocation mAllocLabelID; |
| 164 | |
| 165 | private int[] mAllocScratchBuf; |
| 166 | private Allocation mAllocScratch; |
| 167 | |
Joe Onorato | 43e7bcf | 2009-08-08 18:53:53 -0700 | [diff] [blame] | 168 | Params mParams; |
Joe Onorato | 1feb3a8 | 2009-08-08 22:32:00 -0700 | [diff] [blame^] | 169 | State mState; |
Joe Onorato | 43e7bcf | 2009-08-08 18:53:53 -0700 | [diff] [blame] | 170 | |
| 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 Onorato | 1feb3a8 | 2009-08-08 22:32:00 -0700 | [diff] [blame^] | 181 | 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 Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 202 | 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 Onorato | 43e7bcf | 2009-08-08 18:53:53 -0700 | [diff] [blame] | 213 | ProgramFragment.Builder dbg = new ProgramFragment.Builder(mRS, null, null); |
| 214 | mPFDebug = dbg.create(); |
| 215 | mPFDebug.setName("PFDebug"); |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 216 | |
| 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 Onorato | bf15cb4 | 2009-08-07 14:33:40 -0700 | [diff] [blame] | 269 | } |
| 270 | |
Joe Onorato | 1feb3a8 | 2009-08-08 22:32:00 -0700 | [diff] [blame^] | 271 | private void initData() { |
| 272 | final int count = 29; |
Joe Onorato | 43e7bcf | 2009-08-08 18:53:53 -0700 | [diff] [blame] | 273 | mParams = new Params(mRS); |
Joe Onorato | 1feb3a8 | 2009-08-08 22:32:00 -0700 | [diff] [blame^] | 274 | mState = new State(mRS); |
Joe Onorato | 43e7bcf | 2009-08-08 18:53:53 -0700 | [diff] [blame] | 275 | |
Joe Onorato | bf15cb4 | 2009-08-07 14:33:40 -0700 | [diff] [blame] | 276 | mIcons = new Allocation[count]; |
| 277 | mAllocIconIDBuf = new int[count]; |
| 278 | mAllocIconID = Allocation.createSized(mRS, |
| 279 | Element.USER_I32, mAllocIconIDBuf.length); |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 280 | |
Joe Onorato | bf15cb4 | 2009-08-07 14:33:40 -0700 | [diff] [blame] | 281 | mLabels = new Allocation[count]; |
| 282 | mAllocLabelIDBuf = new int[mLabels.length]; |
| 283 | mAllocLabelID = Allocation.createSized(mRS, |
| 284 | Element.USER_I32, mLabels.length); |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 285 | |
Joe Onorato | bf15cb4 | 2009-08-07 14:33:40 -0700 | [diff] [blame] | 286 | Element ie8888 = Element.RGBA_8888; |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 287 | |
Joe Onorato | bf15cb4 | 2009-08-07 14:33:40 -0700 | [diff] [blame] | 288 | final Utilities.BubbleText bubble = new Utilities.BubbleText(getContext()); |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 289 | |
Joe Onorato | 43e7bcf | 2009-08-08 18:53:53 -0700 | [diff] [blame] | 290 | mParams.bubbleWidth = bubble.getBubbleWidth(); |
| 291 | mParams.bubbleHeight = bubble.getMaxBubbleHeight(); |
| 292 | mParams.bubbleBitmapWidth = bubble.getBitmapWidth(); |
| 293 | mParams.bubbleBitmapHeight = bubble.getBitmapHeight(); |
Joe Onorato | 43e7bcf | 2009-08-08 18:53:53 -0700 | [diff] [blame] | 294 | |
Joe Onorato | bf15cb4 | 2009-08-07 14:33:40 -0700 | [diff] [blame] | 295 | 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 Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 299 | } |
| 300 | |
Joe Onorato | 1feb3a8 | 2009-08-08 22:32:00 -0700 | [diff] [blame^] | 301 | 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 Onorato | bf15cb4 | 2009-08-07 14:33:40 -0700 | [diff] [blame] | 306 | } |
| 307 | mAllocIconID.data(mAllocIconIDBuf); |
| 308 | mAllocLabelID.data(mAllocLabelIDBuf); |
| 309 | |
Joe Onorato | 1feb3a8 | 2009-08-08 22:32:00 -0700 | [diff] [blame^] | 310 | mState.iconCount = count; |
| 311 | |
| 312 | mParams.save(); |
| 313 | mState.save(); |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 314 | } |
| 315 | |
Joe Onorato | bf15cb4 | 2009-08-07 14:33:40 -0700 | [diff] [blame] | 316 | 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 Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 321 | } |
| 322 | |
Joe Onorato | 1feb3a8 | 2009-08-08 22:32:00 -0700 | [diff] [blame^] | 323 | private void initRs() { |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 324 | ScriptC.Builder sb = new ScriptC.Builder(mRS); |
| 325 | sb.setScript(mRes, R.raw.rollo); |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 326 | sb.setRoot(true); |
| 327 | mScript = sb.create(); |
| 328 | mScript.setClearColor(0.0f, 0.0f, 0.0f, 0.0f); |
| 329 | |
Joe Onorato | 43e7bcf | 2009-08-08 18:53:53 -0700 | [diff] [blame] | 330 | mScript.bindAllocation(mParams.getAllocation(), ALLOC_PARAMS); |
Joe Onorato | 1feb3a8 | 2009-08-08 22:32:00 -0700 | [diff] [blame^] | 331 | mScript.bindAllocation(mState.getAllocation(), ALLOC_STATE); |
Joe Onorato | 43e7bcf | 2009-08-08 18:53:53 -0700 | [diff] [blame] | 332 | mScript.bindAllocation(mAllocIconID, ALLOC_ICON_IDS); |
| 333 | mScript.bindAllocation(mAllocScratch, ALLOC_SCRATCH); |
| 334 | mScript.bindAllocation(mAllocLabelID, ALLOC_LABEL_IDS); |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 335 | |
| 336 | mRS.contextBindRootScript(mScript); |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | } |
| 341 | |
| 342 | |
| 343 | |