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