RenderScript.java revision 5dbfe93b3f15f3a837836d024958635fd8f9ad14
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 17package android.renderscript; 18 19import java.lang.reflect.Field; 20 21import android.graphics.Bitmap; 22import android.graphics.BitmapFactory; 23import android.util.Config; 24import android.util.Log; 25import android.view.Surface; 26 27 28/** 29 * @hide 30 * 31 **/ 32public class RenderScript { 33 static final String LOG_TAG = "RenderScript_jni"; 34 private static final boolean DEBUG = false; 35 @SuppressWarnings({"UnusedDeclaration", "deprecation"}) 36 private static final boolean LOG_ENABLED = DEBUG ? Config.LOGD : Config.LOGV; 37 int mWidth; 38 int mHeight; 39 40 41 42 /* 43 * We use a class initializer to allow the native code to cache some 44 * field offsets. 45 */ 46 @SuppressWarnings({"FieldCanBeLocal", "UnusedDeclaration"}) 47 private static boolean sInitialized; 48 native private static void _nInit(); 49 50 51 static { 52 sInitialized = false; 53 try { 54 System.loadLibrary("rs_jni"); 55 _nInit(); 56 sInitialized = true; 57 } catch (UnsatisfiedLinkError e) { 58 Log.d(LOG_TAG, "RenderScript JNI library not found!"); 59 } 60 } 61 62 native void nInitElements(int a8, int rgba4444, int rgba8888, int rgb565); 63 64 native int nDeviceCreate(); 65 native void nDeviceDestroy(int dev); 66 native void nDeviceSetConfig(int dev, int param, int value); 67 native int nContextCreate(int dev, int ver, boolean useDepth); 68 native void nContextDestroy(int con); 69 native void nContextSetSurface(int w, int h, Surface sur); 70 native void nContextSetPriority(int p); 71 native void nContextDump(int bits); 72 73 native void nContextBindRootScript(int script); 74 native void nContextBindSampler(int sampler, int slot); 75 native void nContextBindProgramFragmentStore(int pfs); 76 native void nContextBindProgramFragment(int pf); 77 native void nContextBindProgramVertex(int pf); 78 native void nContextBindProgramRaster(int pr); 79 native void nContextPause(); 80 native void nContextResume(); 81 native int nContextGetMessage(int[] data, boolean wait); 82 native void nContextInitToClient(); 83 native void nContextDeinitToClient(); 84 85 native void nAssignName(int obj, byte[] name); 86 native void nObjDestroy(int id); 87 native void nObjDestroyOOB(int id); 88 native int nFileOpen(byte[] name); 89 90 91 native int nElementCreate(int type, int kind, boolean norm, int vecSize); 92 native int nElementCreate2(int[] elements, String[] names); 93 94 native void nTypeBegin(int elementID); 95 native void nTypeAdd(int dim, int val); 96 native int nTypeCreate(); 97 native void nTypeFinalDestroy(Type t); 98 native void nTypeSetupFields(Type t, int[] types, int[] bits, Field[] IDs); 99 100 native int nAllocationCreateTyped(int type); 101 native int nAllocationCreateFromBitmap(int dstFmt, boolean genMips, Bitmap bmp); 102 native int nAllocationCreateFromBitmapBoxed(int dstFmt, boolean genMips, Bitmap bmp); 103 native int nAllocationCreateFromAssetStream(int dstFmt, boolean genMips, int assetStream); 104 105 native void nAllocationUploadToTexture(int alloc, int baseMioLevel); 106 native void nAllocationUploadToBufferObject(int alloc); 107 108 native void nAllocationSubData1D(int id, int off, int count, int[] d, int sizeBytes); 109 native void nAllocationSubData1D(int id, int off, int count, short[] d, int sizeBytes); 110 native void nAllocationSubData1D(int id, int off, int count, byte[] d, int sizeBytes); 111 native void nAllocationSubData1D(int id, int off, int count, float[] d, int sizeBytes); 112 113 native void nAllocationSubData2D(int id, int xoff, int yoff, int w, int h, int[] d, int sizeBytes); 114 native void nAllocationSubData2D(int id, int xoff, int yoff, int w, int h, float[] d, int sizeBytes); 115 native void nAllocationRead(int id, int[] d); 116 native void nAllocationRead(int id, float[] d); 117 native void nAllocationSubDataFromObject(int id, Type t, int offset, Object o); 118 native void nAllocationSubReadFromObject(int id, Type t, int offset, Object o); 119 120 native void nAdapter1DBindAllocation(int ad, int alloc); 121 native void nAdapter1DSetConstraint(int ad, int dim, int value); 122 native void nAdapter1DData(int ad, int[] d); 123 native void nAdapter1DData(int ad, float[] d); 124 native void nAdapter1DSubData(int ad, int off, int count, int[] d); 125 native void nAdapter1DSubData(int ad, int off, int count, float[] d); 126 native int nAdapter1DCreate(); 127 128 native void nAdapter2DBindAllocation(int ad, int alloc); 129 native void nAdapter2DSetConstraint(int ad, int dim, int value); 130 native void nAdapter2DData(int ad, int[] d); 131 native void nAdapter2DData(int ad, float[] d); 132 native void nAdapter2DSubData(int ad, int xoff, int yoff, int w, int h, int[] d); 133 native void nAdapter2DSubData(int ad, int xoff, int yoff, int w, int h, float[] d); 134 native int nAdapter2DCreate(); 135 136 native void nScriptBindAllocation(int script, int alloc, int slot); 137 native void nScriptSetClearColor(int script, float r, float g, float b, float a); 138 native void nScriptSetClearDepth(int script, float depth); 139 native void nScriptSetClearStencil(int script, int stencil); 140 native void nScriptSetTimeZone(int script, byte[] timeZone); 141 native void nScriptSetType(int type, boolean writable, String name, int slot); 142 native void nScriptSetRoot(boolean isRoot); 143 native void nScriptSetInvokable(String name, int slot); 144 native void nScriptInvoke(int id, int slot); 145 146 native void nScriptCBegin(); 147 native void nScriptCSetScript(byte[] script, int offset, int length); 148 native int nScriptCCreate(); 149 native void nScriptCAddDefineI32(String name, int value); 150 native void nScriptCAddDefineF(String name, float value); 151 152 native void nSamplerBegin(); 153 native void nSamplerSet(int param, int value); 154 native int nSamplerCreate(); 155 156 native void nProgramFragmentStoreBegin(int in, int out); 157 native void nProgramFragmentStoreDepthFunc(int func); 158 native void nProgramFragmentStoreDepthMask(boolean enable); 159 native void nProgramFragmentStoreColorMask(boolean r, boolean g, boolean b, boolean a); 160 native void nProgramFragmentStoreBlendFunc(int src, int dst); 161 native void nProgramFragmentStoreDither(boolean enable); 162 native int nProgramFragmentStoreCreate(); 163 164 native int nProgramRasterCreate(int in, int out, boolean pointSmooth, boolean lineSmooth, boolean pointSprite); 165 native void nProgramRasterSetLineWidth(int pr, float v); 166 native void nProgramRasterSetPointSize(int pr, float v); 167 168 native void nProgramBindConstants(int pv, int slot, int mID); 169 native void nProgramBindTexture(int vpf, int slot, int a); 170 native void nProgramBindSampler(int vpf, int slot, int s); 171 172 native int nProgramFragmentCreate(int[] params); 173 native int nProgramFragmentCreate2(String shader, int[] params); 174 175 native int nProgramVertexCreate(boolean texMat); 176 native int nProgramVertexCreate2(String shader, int[] params); 177 178 native void nLightBegin(); 179 native void nLightSetIsMono(boolean isMono); 180 native void nLightSetIsLocal(boolean isLocal); 181 native int nLightCreate(); 182 native void nLightSetColor(int l, float r, float g, float b); 183 native void nLightSetPosition(int l, float x, float y, float z); 184 185 native int nSimpleMeshCreate(int batchID, int idxID, int[] vtxID, int prim); 186 native void nSimpleMeshBindVertex(int id, int alloc, int slot); 187 native void nSimpleMeshBindIndex(int id, int alloc); 188 189 native void nAnimationBegin(int attribCount, int keyframeCount); 190 native void nAnimationAdd(float time, float[] attribs); 191 native int nAnimationCreate(); 192 193 private int mDev; 194 private int mContext; 195 @SuppressWarnings({"FieldCanBeLocal"}) 196 private Surface mSurface; 197 private MessageThread mMessageThread; 198 199 Element mElement_USER_U8; 200 Element mElement_USER_I8; 201 Element mElement_USER_U16; 202 Element mElement_USER_I16; 203 Element mElement_USER_U32; 204 Element mElement_USER_I32; 205 Element mElement_USER_F32; 206 207 Element mElement_A_8; 208 Element mElement_RGB_565; 209 Element mElement_RGB_888; 210 Element mElement_RGBA_5551; 211 Element mElement_RGBA_4444; 212 Element mElement_RGBA_8888; 213 214 Element mElement_INDEX_16; 215 Element mElement_POSITION_2; 216 Element mElement_POSITION_3; 217 Element mElement_TEXTURE_2; 218 Element mElement_NORMAL_3; 219 Element mElement_COLOR_U8_4; 220 Element mElement_COLOR_F32_4; 221 222 /////////////////////////////////////////////////////////////////////////////////// 223 // 224 225 public static class RSMessage implements Runnable { 226 protected int[] mData; 227 protected int mID; 228 public void run() { 229 } 230 } 231 public RSMessage mMessageCallback = null; 232 233 public enum Priority { 234 LOW (5), //ANDROID_PRIORITY_BACKGROUND + 5 235 NORMAL (-4); //ANDROID_PRIORITY_DISPLAY 236 237 int mID; 238 Priority(int id) { 239 mID = id; 240 } 241 } 242 243 void validate() { 244 if (mContext == 0) { 245 throw new IllegalStateException("Calling RS with no Context active."); 246 } 247 } 248 249 public void contextSetPriority(Priority p) { 250 validate(); 251 nContextSetPriority(p.mID); 252 } 253 254 private static class MessageThread extends Thread { 255 RenderScript mRS; 256 boolean mRun = true; 257 258 MessageThread(RenderScript rs) { 259 super("RSMessageThread"); 260 mRS = rs; 261 262 } 263 264 public void run() { 265 // This function is a temporary solution. The final solution will 266 // used typed allocations where the message id is the type indicator. 267 int[] rbuf = new int[16]; 268 mRS.nContextInitToClient(); 269 while(mRun) { 270 int msg = mRS.nContextGetMessage(rbuf, true); 271 if (msg == 0) { 272 // Should only happen during teardown. 273 // But we want to avoid starving other threads during 274 // teardown by yielding until the next line in the destructor 275 // can execute to set mRun = false 276 try { 277 sleep(1, 0); 278 } catch(InterruptedException e) { 279 } 280 } 281 if(mRS.mMessageCallback != null) { 282 mRS.mMessageCallback.mData = rbuf; 283 mRS.mMessageCallback.mID = msg; 284 mRS.mMessageCallback.run(); 285 } 286 //Log.d(LOG_TAG, "MessageThread msg " + msg + " v1 " + rbuf[0] + " v2 " + rbuf[1] + " v3 " +rbuf[2]); 287 } 288 Log.d(LOG_TAG, "MessageThread exiting."); 289 } 290 } 291 292 public RenderScript(boolean useDepth, boolean forceSW) { 293 mSurface = null; 294 mWidth = 0; 295 mHeight = 0; 296 mDev = nDeviceCreate(); 297 if(forceSW) { 298 nDeviceSetConfig(mDev, 0, 1); 299 } 300 mContext = nContextCreate(mDev, 0, useDepth); 301 mMessageThread = new MessageThread(this); 302 mMessageThread.start(); 303 Element.initPredefined(this); 304 } 305 306 public void contextSetSurface(int w, int h, Surface sur) { 307 mSurface = sur; 308 mWidth = w; 309 mHeight = h; 310 validate(); 311 nContextSetSurface(w, h, mSurface); 312 } 313 314 public void contextDump(int bits) { 315 validate(); 316 nContextDump(bits); 317 } 318 319 public void destroy() { 320 validate(); 321 nContextDeinitToClient(); 322 mMessageThread.mRun = false; 323 324 nContextDestroy(mContext); 325 mContext = 0; 326 327 nDeviceDestroy(mDev); 328 mDev = 0; 329 } 330 331 boolean isAlive() { 332 return mContext != 0; 333 } 334 335 void pause() { 336 validate(); 337 nContextPause(); 338 } 339 340 void resume() { 341 validate(); 342 nContextResume(); 343 } 344 345 ////////////////////////////////////////////////////////////////////////////////// 346 // File 347 348 public class File extends BaseObj { 349 File(int id) { 350 super(RenderScript.this); 351 mID = id; 352 } 353 } 354 355 public File fileOpen(String s) throws IllegalStateException, IllegalArgumentException 356 { 357 if(s.length() < 1) { 358 throw new IllegalArgumentException("fileOpen does not accept a zero length string."); 359 } 360 361 try { 362 byte[] bytes = s.getBytes("UTF-8"); 363 int id = nFileOpen(bytes); 364 return new File(id); 365 } catch (java.io.UnsupportedEncodingException e) { 366 throw new RuntimeException(e); 367 } 368 } 369 370 371 /////////////////////////////////////////////////////////////////////////////////// 372 // Root state 373 374 private int safeID(BaseObj o) { 375 if(o != null) { 376 return o.mID; 377 } 378 return 0; 379 } 380 381 public void contextBindRootScript(Script s) { 382 validate(); 383 nContextBindRootScript(safeID(s)); 384 } 385 386 public void contextBindProgramFragmentStore(ProgramStore p) { 387 validate(); 388 nContextBindProgramFragmentStore(safeID(p)); 389 } 390 391 public void contextBindProgramFragment(ProgramFragment p) { 392 validate(); 393 nContextBindProgramFragment(safeID(p)); 394 } 395 396 public void contextBindProgramRaster(ProgramRaster p) { 397 validate(); 398 nContextBindProgramRaster(safeID(p)); 399 } 400 401 public void contextBindProgramVertex(ProgramVertex p) { 402 validate(); 403 nContextBindProgramVertex(safeID(p)); 404 } 405 406} 407 408 409