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