RenderScript.java revision 516c31911578db8ce53529483c3ded918ac7dc6b
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 = "libRS_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 38 39 40 /* 41 * We use a class initializer to allow the native code to cache some 42 * field offsets. 43 */ 44 @SuppressWarnings({"FieldCanBeLocal", "UnusedDeclaration"}) 45 private static boolean sInitialized; 46 native private static void _nInit(); 47 48 49 static { 50 sInitialized = false; 51 try { 52 System.loadLibrary("rs_jni"); 53 _nInit(); 54 sInitialized = true; 55 } catch (UnsatisfiedLinkError e) { 56 Log.d(LOG_TAG, "RenderScript JNI library not found!"); 57 } 58 } 59 60 native void nInitElements(int a8, int rgba4444, int rgba8888, int rgb565); 61 62 native int nDeviceCreate(); 63 native void nDeviceDestroy(int dev); 64 native void nDeviceSetConfig(int dev, int param, int value); 65 native int nContextCreate(int dev, Surface sur, int ver, boolean useDepth); 66 native void nContextDestroy(int con); 67 68 native void nContextBindRootScript(int script); 69 native void nContextBindSampler(int sampler, int slot); 70 native void nContextBindProgramFragmentStore(int pfs); 71 native void nContextBindProgramFragment(int pf); 72 native void nContextBindProgramVertex(int pf); 73 native void nContextBindProgramRaster(int pr); 74 native void nContextAddDefineI32(String name, int value); 75 native void nContextAddDefineF(String name, float value); 76 native void nContextPause(); 77 native void nContextResume(); 78 native int nContextGetMessage(int[] data, boolean wait); 79 native void nContextInitToClient(); 80 native void nContextDeinitToClient(); 81 82 native void nAssignName(int obj, byte[] name); 83 native void nObjDestroy(int id); 84 native void nObjDestroyOOB(int id); 85 native int nFileOpen(byte[] name); 86 87 native void nElementBegin(); 88 native void nElementAdd(int kind, int type, boolean norm, int bits, String s); 89 native int nElementCreate(); 90 91 native void nTypeBegin(int elementID); 92 native void nTypeAdd(int dim, int val); 93 native int nTypeCreate(); 94 native void nTypeFinalDestroy(Type t); 95 native void nTypeSetupFields(Type t, int[] types, int[] bits, Field[] IDs); 96 97 native int nAllocationCreateTyped(int type); 98 native int nAllocationCreateFromBitmap(int dstFmt, boolean genMips, Bitmap bmp); 99 native int nAllocationCreateFromBitmapBoxed(int dstFmt, boolean genMips, Bitmap bmp); 100 native int nAllocationCreateFromAssetStream(int dstFmt, boolean genMips, int assetStream); 101 102 native void nAllocationUploadToTexture(int alloc, int baseMioLevel); 103 native void nAllocationUploadToBufferObject(int alloc); 104 105 native void nAllocationSubData1D(int id, int off, int count, int[] d, int sizeBytes); 106 native void nAllocationSubData1D(int id, int off, int count, short[] d, int sizeBytes); 107 native void nAllocationSubData1D(int id, int off, int count, byte[] d, int sizeBytes); 108 native void nAllocationSubData1D(int id, int off, int count, float[] d, int sizeBytes); 109 110 native void nAllocationSubData2D(int id, int xoff, int yoff, int w, int h, int[] d, int sizeBytes); 111 native void nAllocationSubData2D(int id, int xoff, int yoff, int w, int h, float[] d, int sizeBytes); 112 native void nAllocationRead(int id, int[] d); 113 native void nAllocationRead(int id, float[] d); 114 native void nAllocationSubDataFromObject(int id, Type t, int offset, Object o); 115 native void nAllocationSubReadFromObject(int id, Type t, int offset, Object o); 116 117 native void nAdapter1DBindAllocation(int ad, int alloc); 118 native void nAdapter1DSetConstraint(int ad, int dim, int value); 119 native void nAdapter1DData(int ad, int[] d); 120 native void nAdapter1DData(int ad, float[] d); 121 native void nAdapter1DSubData(int ad, int off, int count, int[] d); 122 native void nAdapter1DSubData(int ad, int off, int count, float[] d); 123 native int nAdapter1DCreate(); 124 125 native void nAdapter2DBindAllocation(int ad, int alloc); 126 native void nAdapter2DSetConstraint(int ad, int dim, int value); 127 native void nAdapter2DData(int ad, int[] d); 128 native void nAdapter2DData(int ad, float[] d); 129 native void nAdapter2DSubData(int ad, int xoff, int yoff, int w, int h, int[] d); 130 native void nAdapter2DSubData(int ad, int xoff, int yoff, int w, int h, float[] d); 131 native int nAdapter2DCreate(); 132 133 native void nScriptBindAllocation(int script, int alloc, int slot); 134 native void nScriptSetClearColor(int script, float r, float g, float b, float a); 135 native void nScriptSetClearDepth(int script, float depth); 136 native void nScriptSetClearStencil(int script, int stencil); 137 native void nScriptSetTimeZone(int script, byte[] timeZone); 138 native void nScriptSetType(int type, boolean writable, String name, int slot); 139 native void nScriptSetRoot(boolean isRoot); 140 native void nScriptSetInvokable(String name, int slot); 141 native void nScriptInvoke(int id, int slot); 142 143 native void nScriptCBegin(); 144 native void nScriptCSetScript(byte[] script, int offset, int length); 145 native int nScriptCCreate(); 146 native void nScriptCAddDefineI32(String name, int value); 147 native void nScriptCAddDefineF(String name, float value); 148 149 native void nSamplerBegin(); 150 native void nSamplerSet(int param, int value); 151 native int nSamplerCreate(); 152 153 native void nProgramFragmentStoreBegin(int in, int out); 154 native void nProgramFragmentStoreDepthFunc(int func); 155 native void nProgramFragmentStoreDepthMask(boolean enable); 156 native void nProgramFragmentStoreColorMask(boolean r, boolean g, boolean b, boolean a); 157 native void nProgramFragmentStoreBlendFunc(int src, int dst); 158 native void nProgramFragmentStoreDither(boolean enable); 159 native int nProgramFragmentStoreCreate(); 160 161 native int nProgramRasterCreate(int in, int out, boolean pointSmooth, boolean lineSmooth, boolean pointSprite); 162 native void nProgramRasterSetLineWidth(int pr, float v); 163 native void nProgramRasterSetPointSize(int pr, float v); 164 165 native void nProgramFragmentBegin(int in, int out, boolean pointSpriteEnable); 166 native void nProgramFragmentBindTexture(int vpf, int slot, int a); 167 native void nProgramFragmentBindSampler(int vpf, int slot, int s); 168 native void nProgramFragmentSetSlot(int slot, boolean enable, int env, int vt); 169 native int nProgramFragmentCreate(); 170 171 native void nProgramVertexBindAllocation(int pv, int mID); 172 native void nProgramVertexBegin(int inID, int outID); 173 native void nProgramVertexSetTextureMatrixEnable(boolean enable); 174 native void nProgramVertexAddLight(int id); 175 native int nProgramVertexCreate(); 176 177 native void nLightBegin(); 178 native void nLightSetIsMono(boolean isMono); 179 native void nLightSetIsLocal(boolean isLocal); 180 native int nLightCreate(); 181 native void nLightSetColor(int l, float r, float g, float b); 182 native void nLightSetPosition(int l, float x, float y, float z); 183 184 native int nSimpleMeshCreate(int batchID, int idxID, int[] vtxID, int prim); 185 native void nSimpleMeshBindVertex(int id, int alloc, int slot); 186 native void nSimpleMeshBindIndex(int id, int alloc); 187 188 native void nAnimationBegin(int attribCount, int keyframeCount); 189 native void nAnimationAdd(float time, float[] attribs); 190 native int nAnimationCreate(); 191 192 private int mDev; 193 private int mContext; 194 @SuppressWarnings({"FieldCanBeLocal"}) 195 private Surface mSurface; 196 private MessageThread mMessageThread; 197 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_FLOAT; 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_XY_F32; 216 Element mElement_XYZ_F32; 217 218 /////////////////////////////////////////////////////////////////////////////////// 219 // 220 221 public static class RSMessage implements Runnable { 222 protected int[] mData; 223 protected int mID; 224 public void run() { 225 } 226 } 227 public RSMessage mMessageCallback = null; 228 229 private static class MessageThread extends Thread { 230 RenderScript mRS; 231 boolean mRun = true; 232 233 MessageThread(RenderScript rs) { 234 super("RSMessageThread"); 235 mRS = rs; 236 237 } 238 239 public void run() { 240 // This function is a temporary solution. The final solution will 241 // used typed allocations where the message id is the type indicator. 242 int[] rbuf = new int[16]; 243 mRS.nContextInitToClient(); 244 while(mRun) { 245 int msg = mRS.nContextGetMessage(rbuf, true); 246 if (msg == 0) { 247 // Should only happen during teardown. 248 // But we want to avoid starving other threads during 249 // teardown by yielding until the next line in the destructor 250 // can execute to set mRun = false 251 try { 252 sleep(1, 0); 253 } catch(InterruptedException e) { 254 } 255 } 256 if(mRS.mMessageCallback != null) { 257 mRS.mMessageCallback.mData = rbuf; 258 mRS.mMessageCallback.mID = msg; 259 mRS.mMessageCallback.run(); 260 } 261 //Log.d("rs", "MessageThread msg " + msg + " v1 " + rbuf[0] + " v2 " + rbuf[1] + " v3 " +rbuf[2]); 262 } 263 Log.d("rs", "MessageThread exiting."); 264 } 265 } 266 267 public RenderScript(Surface sur, boolean useDepth, boolean forceSW) { 268 mSurface = sur; 269 mDev = nDeviceCreate(); 270 if(forceSW) { 271 nDeviceSetConfig(mDev, 0, 1); 272 } 273 mContext = nContextCreate(mDev, mSurface, 0, useDepth); 274 Element.initPredefined(this); 275 mMessageThread = new MessageThread(this); 276 mMessageThread.start(); 277 } 278 279 public void destroy() { 280 nContextDeinitToClient(); 281 mMessageThread.mRun = false; 282 283 nContextDestroy(mContext); 284 mContext = 0; 285 286 nDeviceDestroy(mDev); 287 mDev = 0; 288 } 289 290 boolean isAlive() { 291 return mContext != 0; 292 } 293 294 void pause() { 295 nContextPause(); 296 } 297 298 void resume() { 299 nContextResume(); 300 } 301 302 ////////////////////////////////////////////////////////////////////////////////// 303 // File 304 305 public class File extends BaseObj { 306 File(int id) { 307 super(RenderScript.this); 308 mID = id; 309 } 310 } 311 312 public File fileOpen(String s) throws IllegalStateException, IllegalArgumentException 313 { 314 if(s.length() < 1) { 315 throw new IllegalArgumentException("fileOpen does not accept a zero length string."); 316 } 317 318 try { 319 byte[] bytes = s.getBytes("UTF-8"); 320 int id = nFileOpen(bytes); 321 return new File(id); 322 } catch (java.io.UnsupportedEncodingException e) { 323 throw new RuntimeException(e); 324 } 325 } 326 327 328 /////////////////////////////////////////////////////////////////////////////////// 329 // Root state 330 331 private int safeID(BaseObj o) { 332 if(o != null) { 333 return o.mID; 334 } 335 return 0; 336 } 337 338 public void contextBindRootScript(Script s) { 339 nContextBindRootScript(safeID(s)); 340 } 341 342 public void contextBindProgramFragmentStore(ProgramStore p) { 343 nContextBindProgramFragmentStore(safeID(p)); 344 } 345 346 public void contextBindProgramFragment(ProgramFragment p) { 347 nContextBindProgramFragment(safeID(p)); 348 } 349 350 public void contextBindProgramRaster(ProgramRaster p) { 351 nContextBindProgramRaster(safeID(p)); 352 } 353 354 public void contextBindProgramVertex(ProgramVertex p) { 355 nContextBindProgramVertex(safeID(p)); 356 } 357 358} 359 360 361