Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -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 | |
Jason Sams | 94d8e90a | 2009-06-10 16:09:05 -0700 | [diff] [blame] | 17 | package android.renderscript; |
Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 18 | |
Jason Sams | 43ee0685 | 2009-08-12 17:54:11 -0700 | [diff] [blame] | 19 | import java.lang.reflect.Field; |
Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 20 | |
Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 21 | import android.graphics.Bitmap; |
Romain Guy | 650a3eb | 2009-08-31 14:06:43 -0700 | [diff] [blame] | 22 | import android.graphics.BitmapFactory; |
Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 23 | import android.util.Config; |
| 24 | import android.util.Log; |
| 25 | import android.view.Surface; |
Jack Palevich | 43702d8 | 2009-05-28 13:38:16 -0700 | [diff] [blame] | 26 | |
Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 27 | |
Jason Sams | e29d471 | 2009-07-23 15:19:03 -0700 | [diff] [blame] | 28 | /** |
| 29 | * @hide |
| 30 | * |
| 31 | **/ |
Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 32 | public class RenderScript { |
Jason Sams | 3bc47d4 | 2009-11-12 15:10:25 -0800 | [diff] [blame] | 33 | static final String LOG_TAG = "RenderScript_jni"; |
Jason Sams | 704ff64 | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 34 | protected static final boolean DEBUG = false; |
Romain Guy | 650a3eb | 2009-08-31 14:06:43 -0700 | [diff] [blame] | 35 | @SuppressWarnings({"UnusedDeclaration", "deprecation"}) |
Jason Sams | 704ff64 | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 36 | protected static final boolean LOG_ENABLED = DEBUG ? Config.LOGD : Config.LOGV; |
Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 37 | |
| 38 | |
| 39 | |
Jason Sams | 02fb2cb | 2009-05-28 15:37:57 -0700 | [diff] [blame] | 40 | /* |
Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 41 | * We use a class initializer to allow the native code to cache some |
| 42 | * field offsets. |
| 43 | */ |
Romain Guy | 650a3eb | 2009-08-31 14:06:43 -0700 | [diff] [blame] | 44 | @SuppressWarnings({"FieldCanBeLocal", "UnusedDeclaration"}) |
Jason Sams | 704ff64 | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 45 | protected static boolean sInitialized; |
| 46 | native protected static void _nInit(); |
Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 47 | |
Jason Sams | dba3ba5 | 2009-07-30 14:56:12 -0700 | [diff] [blame] | 48 | |
Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 49 | static { |
| 50 | sInitialized = false; |
| 51 | try { |
Jason Sams | e29d471 | 2009-07-23 15:19:03 -0700 | [diff] [blame] | 52 | System.loadLibrary("rs_jni"); |
Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 53 | _nInit(); |
Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 54 | sInitialized = true; |
| 55 | } catch (UnsatisfiedLinkError e) { |
| 56 | Log.d(LOG_TAG, "RenderScript JNI library not found!"); |
| 57 | } |
| 58 | } |
| 59 | |
Jason Sams | ea84a7c | 2009-09-04 14:42:41 -0700 | [diff] [blame] | 60 | native void nInitElements(int a8, int rgba4444, int rgba8888, int rgb565); |
| 61 | |
Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 62 | native int nDeviceCreate(); |
| 63 | native void nDeviceDestroy(int dev); |
Jason Sams | ebfb436 | 2009-09-23 13:57:02 -0700 | [diff] [blame] | 64 | native void nDeviceSetConfig(int dev, int param, int value); |
Jason Sams | 704ff64 | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 65 | native int nContextCreateGL(int dev, int ver, boolean useDepth); |
| 66 | native int nContextCreate(int dev, int ver); |
Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 67 | native void nContextDestroy(int con); |
Jason Sams | 3bc47d4 | 2009-11-12 15:10:25 -0800 | [diff] [blame] | 68 | native void nContextSetSurface(int w, int h, Surface sur); |
Jason Sams | 7d787b4 | 2009-11-15 12:14:26 -0800 | [diff] [blame] | 69 | native void nContextSetPriority(int p); |
Jason Sams | 715333b | 2009-11-17 17:26:46 -0800 | [diff] [blame] | 70 | native void nContextDump(int bits); |
Jason Sams | 96ed4cf | 2010-06-15 12:15:57 -0700 | [diff] [blame] | 71 | native void nContextFinish(); |
Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 72 | |
Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 73 | native void nContextBindRootScript(int script); |
| 74 | native void nContextBindSampler(int sampler, int slot); |
Jason Sams | 54db59c | 2010-05-13 18:30:11 -0700 | [diff] [blame] | 75 | native void nContextBindProgramStore(int pfs); |
Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 76 | native void nContextBindProgramFragment(int pf); |
| 77 | native void nContextBindProgramVertex(int pf); |
Jason Sams | ebfb436 | 2009-09-23 13:57:02 -0700 | [diff] [blame] | 78 | native void nContextBindProgramRaster(int pr); |
Jason Sams | 65e7aa5 | 2009-09-24 17:38:20 -0700 | [diff] [blame] | 79 | native void nContextPause(); |
| 80 | native void nContextResume(); |
Jason Sams | 516c319 | 2009-10-06 13:58:47 -0700 | [diff] [blame] | 81 | native int nContextGetMessage(int[] data, boolean wait); |
| 82 | native void nContextInitToClient(); |
| 83 | native void nContextDeinitToClient(); |
Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 84 | |
Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 85 | native void nAssignName(int obj, byte[] name); |
Jason Sams | 7ce033d | 2009-08-18 14:14:24 -0700 | [diff] [blame] | 86 | native void nObjDestroy(int id); |
Jason Sams | 730ee65 | 2009-08-18 17:07:09 -0700 | [diff] [blame] | 87 | native void nObjDestroyOOB(int id); |
Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 88 | native int nFileOpen(byte[] name); |
Jason Sams | 3eaa338 | 2009-06-10 15:04:38 -0700 | [diff] [blame] | 89 | |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 90 | |
| 91 | native int nElementCreate(int type, int kind, boolean norm, int vecSize); |
| 92 | native int nElementCreate2(int[] elements, String[] names); |
Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 93 | |
Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 94 | native void nTypeBegin(int elementID); |
| 95 | native void nTypeAdd(int dim, int val); |
| 96 | native int nTypeCreate(); |
Jason Sams | 43ee0685 | 2009-08-12 17:54:11 -0700 | [diff] [blame] | 97 | native void nTypeFinalDestroy(Type t); |
| 98 | native void nTypeSetupFields(Type t, int[] types, int[] bits, Field[] IDs); |
Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 99 | |
Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 100 | native int nAllocationCreateTyped(int type); |
Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 101 | native int nAllocationCreateFromBitmap(int dstFmt, boolean genMips, Bitmap bmp); |
Jason Sams | 8a64743 | 2010-03-01 15:31:04 -0800 | [diff] [blame] | 102 | native int nAllocationCreateBitmapRef(int type, Bitmap bmp); |
Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 103 | native int nAllocationCreateFromBitmapBoxed(int dstFmt, boolean genMips, Bitmap bmp); |
Romain Guy | 650a3eb | 2009-08-31 14:06:43 -0700 | [diff] [blame] | 104 | native int nAllocationCreateFromAssetStream(int dstFmt, boolean genMips, int assetStream); |
Jason Sams | fe08d99 | 2009-05-27 14:45:32 -0700 | [diff] [blame] | 105 | |
Jason Sams | c2908e6 | 2010-02-23 17:44:28 -0800 | [diff] [blame] | 106 | native void nAllocationUploadToTexture(int alloc, boolean genMips, int baseMioLevel); |
Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 107 | native void nAllocationUploadToBufferObject(int alloc); |
Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 108 | |
Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 109 | native void nAllocationSubData1D(int id, int off, int count, int[] d, int sizeBytes); |
Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 110 | native void nAllocationSubData1D(int id, int off, int count, short[] d, int sizeBytes); |
| 111 | native void nAllocationSubData1D(int id, int off, int count, byte[] d, int sizeBytes); |
Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 112 | native void nAllocationSubData1D(int id, int off, int count, float[] d, int sizeBytes); |
Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 113 | |
Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 114 | native void nAllocationSubData2D(int id, int xoff, int yoff, int w, int h, int[] d, int sizeBytes); |
| 115 | native void nAllocationSubData2D(int id, int xoff, int yoff, int w, int h, float[] d, int sizeBytes); |
Jason Sams | 40a29e8 | 2009-08-10 14:55:26 -0700 | [diff] [blame] | 116 | native void nAllocationRead(int id, int[] d); |
| 117 | native void nAllocationRead(int id, float[] d); |
Jason Sams | 2525a81 | 2009-09-03 15:43:13 -0700 | [diff] [blame] | 118 | native void nAllocationSubDataFromObject(int id, Type t, int offset, Object o); |
Jason Sams | 5f43fd2 | 2009-09-15 12:39:22 -0700 | [diff] [blame] | 119 | native void nAllocationSubReadFromObject(int id, Type t, int offset, Object o); |
Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 120 | |
Alex Sakhartchouk | aae74ad | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 121 | native int nFileA3DCreateFromAssetStream(int assetStream); |
| 122 | native int nFileA3DGetNumIndexEntries(int fileA3D); |
| 123 | native void nFileA3DGetIndexEntries(int fileA3D, int numEntries, int[] IDs, String[] names); |
| 124 | native int nFileA3DGetEntryByIndex(int fileA3D, int index); |
| 125 | |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 126 | native int nFontCreateFromFile(String fileName, int size, int dpi); |
| 127 | |
Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 128 | native void nAdapter1DBindAllocation(int ad, int alloc); |
| 129 | native void nAdapter1DSetConstraint(int ad, int dim, int value); |
| 130 | native void nAdapter1DData(int ad, int[] d); |
Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 131 | native void nAdapter1DData(int ad, float[] d); |
Jason Sams | bd1c3ad | 2009-08-03 16:03:08 -0700 | [diff] [blame] | 132 | native void nAdapter1DSubData(int ad, int off, int count, int[] d); |
Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 133 | native void nAdapter1DSubData(int ad, int off, int count, float[] d); |
| 134 | native int nAdapter1DCreate(); |
Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 135 | |
Jason Sams | bd1c3ad | 2009-08-03 16:03:08 -0700 | [diff] [blame] | 136 | native void nAdapter2DBindAllocation(int ad, int alloc); |
| 137 | native void nAdapter2DSetConstraint(int ad, int dim, int value); |
| 138 | native void nAdapter2DData(int ad, int[] d); |
| 139 | native void nAdapter2DData(int ad, float[] d); |
| 140 | native void nAdapter2DSubData(int ad, int xoff, int yoff, int w, int h, int[] d); |
| 141 | native void nAdapter2DSubData(int ad, int xoff, int yoff, int w, int h, float[] d); |
| 142 | native int nAdapter2DCreate(); |
| 143 | |
Jason Sams | 2253417 | 2009-08-04 16:58:20 -0700 | [diff] [blame] | 144 | native void nScriptBindAllocation(int script, int alloc, int slot); |
Jason Sams | 2253417 | 2009-08-04 16:58:20 -0700 | [diff] [blame] | 145 | native void nScriptSetTimeZone(int script, byte[] timeZone); |
Jason Sams | be2e841 | 2009-09-16 15:04:38 -0700 | [diff] [blame] | 146 | native void nScriptInvoke(int id, int slot); |
Jason Sams | 4d33993 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 147 | native void nScriptInvokeV(int id, int slot, byte[] params); |
| 148 | native void nScriptSetVarI(int id, int slot, int val); |
| 149 | native void nScriptSetVarF(int id, int slot, float val); |
| 150 | native void nScriptSetVarV(int id, int slot, byte[] val); |
Jason Sams | 2253417 | 2009-08-04 16:58:20 -0700 | [diff] [blame] | 151 | |
Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 152 | native void nScriptCBegin(); |
Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 153 | native void nScriptCSetScript(byte[] script, int offset, int length); |
| 154 | native int nScriptCCreate(); |
Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 155 | |
Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 156 | native void nSamplerBegin(); |
| 157 | native void nSamplerSet(int param, int value); |
| 158 | native int nSamplerCreate(); |
Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 159 | |
Jason Sams | 54db59c | 2010-05-13 18:30:11 -0700 | [diff] [blame] | 160 | native void nProgramStoreBegin(int in, int out); |
| 161 | native void nProgramStoreDepthFunc(int func); |
| 162 | native void nProgramStoreDepthMask(boolean enable); |
| 163 | native void nProgramStoreColorMask(boolean r, boolean g, boolean b, boolean a); |
| 164 | native void nProgramStoreBlendFunc(int src, int dst); |
| 165 | native void nProgramStoreDither(boolean enable); |
| 166 | native int nProgramStoreCreate(); |
Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 167 | |
Jason Sams | ebfb436 | 2009-09-23 13:57:02 -0700 | [diff] [blame] | 168 | native int nProgramRasterCreate(int in, int out, boolean pointSmooth, boolean lineSmooth, boolean pointSprite); |
| 169 | native void nProgramRasterSetLineWidth(int pr, float v); |
| 170 | native void nProgramRasterSetPointSize(int pr, float v); |
| 171 | |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 172 | native void nProgramBindConstants(int pv, int slot, int mID); |
Jason Sams | 68afd01 | 2009-12-17 16:55:08 -0800 | [diff] [blame] | 173 | native void nProgramBindTexture(int vpf, int slot, int a); |
| 174 | native void nProgramBindSampler(int vpf, int slot, int s); |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 175 | |
Jason Sams | 68afd01 | 2009-12-17 16:55:08 -0800 | [diff] [blame] | 176 | native int nProgramFragmentCreate(int[] params); |
Jason Sams | 7e5ab3b | 2009-12-15 13:27:04 -0800 | [diff] [blame] | 177 | native int nProgramFragmentCreate2(String shader, int[] params); |
Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 178 | |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 179 | native int nProgramVertexCreate(boolean texMat); |
| 180 | native int nProgramVertexCreate2(String shader, int[] params); |
Jason Sams | 1fe9b8c | 2009-06-11 14:46:10 -0700 | [diff] [blame] | 181 | |
Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 182 | native void nLightBegin(); |
| 183 | native void nLightSetIsMono(boolean isMono); |
| 184 | native void nLightSetIsLocal(boolean isLocal); |
| 185 | native int nLightCreate(); |
Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 186 | native void nLightSetColor(int l, float r, float g, float b); |
| 187 | native void nLightSetPosition(int l, float x, float y, float z); |
Jason Sams | bba134c | 2009-06-22 15:49:21 -0700 | [diff] [blame] | 188 | |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame^] | 189 | native int nMeshCreate(int vtxCount, int indexCount); |
| 190 | native void nMeshBindVertex(int id, int alloc, int slot); |
| 191 | native void nMeshBindIndex(int id, int alloc, int prim, int slot); |
| 192 | |
Jason Sams | 1bada8c | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 193 | native int nSimpleMeshCreate(int batchID, int idxID, int[] vtxID, int prim); |
| 194 | native void nSimpleMeshBindVertex(int id, int alloc, int slot); |
| 195 | native void nSimpleMeshBindIndex(int id, int alloc); |
| 196 | |
Jason Sams | 40a29e8 | 2009-08-10 14:55:26 -0700 | [diff] [blame] | 197 | native void nAnimationBegin(int attribCount, int keyframeCount); |
| 198 | native void nAnimationAdd(float time, float[] attribs); |
| 199 | native int nAnimationCreate(); |
Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 200 | |
Jason Sams | 704ff64 | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 201 | protected int mDev; |
| 202 | protected int mContext; |
Romain Guy | 650a3eb | 2009-08-31 14:06:43 -0700 | [diff] [blame] | 203 | @SuppressWarnings({"FieldCanBeLocal"}) |
Jason Sams | 704ff64 | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 204 | protected MessageThread mMessageThread; |
Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 205 | |
Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 206 | Element mElement_U8; |
| 207 | Element mElement_I8; |
| 208 | Element mElement_U16; |
| 209 | Element mElement_I16; |
| 210 | Element mElement_U32; |
| 211 | Element mElement_I32; |
| 212 | Element mElement_F32; |
Jason Sams | f110d4b | 2010-06-21 17:42:41 -0700 | [diff] [blame] | 213 | Element mElement_BOOLEAN; |
Jason Sams | 3c0dfba | 2009-09-27 17:50:38 -0700 | [diff] [blame] | 214 | |
Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 215 | Element mElement_ELEMENT; |
| 216 | Element mElement_TYPE; |
| 217 | Element mElement_ALLOCATION; |
| 218 | Element mElement_SAMPLER; |
| 219 | Element mElement_SCRIPT; |
| 220 | Element mElement_MESH; |
| 221 | Element mElement_PROGRAM_FRAGMENT; |
| 222 | Element mElement_PROGRAM_VERTEX; |
| 223 | Element mElement_PROGRAM_RASTER; |
| 224 | Element mElement_PROGRAM_STORE; |
Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 225 | |
Jason Sams | 3c0dfba | 2009-09-27 17:50:38 -0700 | [diff] [blame] | 226 | Element mElement_A_8; |
| 227 | Element mElement_RGB_565; |
| 228 | Element mElement_RGB_888; |
| 229 | Element mElement_RGBA_5551; |
| 230 | Element mElement_RGBA_4444; |
| 231 | Element mElement_RGBA_8888; |
| 232 | |
Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 233 | Element mElement_FLOAT_2; |
| 234 | Element mElement_FLOAT_3; |
| 235 | Element mElement_FLOAT_4; |
| 236 | Element mElement_UCHAR_4; |
Jason Sams | 7d787b4 | 2009-11-15 12:14:26 -0800 | [diff] [blame] | 237 | |
Jason Sams | 4d33993 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 238 | Sampler mSampler_CLAMP_NEAREST; |
| 239 | Sampler mSampler_CLAMP_LINEAR; |
| 240 | Sampler mSampler_CLAMP_LINEAR_MIP_LINEAR; |
| 241 | Sampler mSampler_WRAP_NEAREST; |
| 242 | Sampler mSampler_WRAP_LINEAR; |
| 243 | Sampler mSampler_WRAP_LINEAR_MIP_LINEAR; |
| 244 | |
Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 245 | /////////////////////////////////////////////////////////////////////////////////// |
Jack Palevich | 43702d8 | 2009-05-28 13:38:16 -0700 | [diff] [blame] | 246 | // |
Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 247 | |
Jason Sams | 516c319 | 2009-10-06 13:58:47 -0700 | [diff] [blame] | 248 | public static class RSMessage implements Runnable { |
| 249 | protected int[] mData; |
| 250 | protected int mID; |
| 251 | public void run() { |
| 252 | } |
| 253 | } |
| 254 | public RSMessage mMessageCallback = null; |
| 255 | |
Jason Sams | 7d787b4 | 2009-11-15 12:14:26 -0800 | [diff] [blame] | 256 | public enum Priority { |
| 257 | LOW (5), //ANDROID_PRIORITY_BACKGROUND + 5 |
| 258 | NORMAL (-4); //ANDROID_PRIORITY_DISPLAY |
| 259 | |
| 260 | int mID; |
| 261 | Priority(int id) { |
| 262 | mID = id; |
| 263 | } |
| 264 | } |
| 265 | |
Jason Sams | 771bebb | 2009-12-07 12:40:12 -0800 | [diff] [blame] | 266 | void validate() { |
| 267 | if (mContext == 0) { |
| 268 | throw new IllegalStateException("Calling RS with no Context active."); |
| 269 | } |
| 270 | } |
| 271 | |
Jason Sams | 7d787b4 | 2009-11-15 12:14:26 -0800 | [diff] [blame] | 272 | public void contextSetPriority(Priority p) { |
Jason Sams | 5dbfe93 | 2010-01-27 14:41:43 -0800 | [diff] [blame] | 273 | validate(); |
Jason Sams | 7d787b4 | 2009-11-15 12:14:26 -0800 | [diff] [blame] | 274 | nContextSetPriority(p.mID); |
| 275 | } |
| 276 | |
Jason Sams | 704ff64 | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 277 | protected static class MessageThread extends Thread { |
Jason Sams | 516c319 | 2009-10-06 13:58:47 -0700 | [diff] [blame] | 278 | RenderScript mRS; |
| 279 | boolean mRun = true; |
| 280 | |
| 281 | MessageThread(RenderScript rs) { |
| 282 | super("RSMessageThread"); |
| 283 | mRS = rs; |
| 284 | |
| 285 | } |
| 286 | |
| 287 | public void run() { |
| 288 | // This function is a temporary solution. The final solution will |
| 289 | // used typed allocations where the message id is the type indicator. |
| 290 | int[] rbuf = new int[16]; |
| 291 | mRS.nContextInitToClient(); |
| 292 | while(mRun) { |
| 293 | int msg = mRS.nContextGetMessage(rbuf, true); |
| 294 | if (msg == 0) { |
| 295 | // Should only happen during teardown. |
| 296 | // But we want to avoid starving other threads during |
| 297 | // teardown by yielding until the next line in the destructor |
| 298 | // can execute to set mRun = false |
| 299 | try { |
| 300 | sleep(1, 0); |
| 301 | } catch(InterruptedException e) { |
| 302 | } |
| 303 | } |
| 304 | if(mRS.mMessageCallback != null) { |
| 305 | mRS.mMessageCallback.mData = rbuf; |
| 306 | mRS.mMessageCallback.mID = msg; |
| 307 | mRS.mMessageCallback.run(); |
| 308 | } |
Jason Sams | 516c319 | 2009-10-06 13:58:47 -0700 | [diff] [blame] | 309 | } |
Jason Sams | 3bc47d4 | 2009-11-12 15:10:25 -0800 | [diff] [blame] | 310 | Log.d(LOG_TAG, "MessageThread exiting."); |
Jason Sams | 516c319 | 2009-10-06 13:58:47 -0700 | [diff] [blame] | 311 | } |
| 312 | } |
| 313 | |
Jason Sams | 704ff64 | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 314 | protected RenderScript() { |
Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 315 | } |
| 316 | |
Jason Sams | 704ff64 | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 317 | public static RenderScript create() { |
| 318 | RenderScript rs = new RenderScript(); |
| 319 | |
| 320 | rs.mDev = rs.nDeviceCreate(); |
| 321 | rs.mContext = rs.nContextCreate(rs.mDev, 0); |
| 322 | rs.mMessageThread = new MessageThread(rs); |
| 323 | rs.mMessageThread.start(); |
| 324 | Element.initPredefined(rs); |
| 325 | return rs; |
Jason Sams | efd9b6fb | 2009-11-03 13:58:36 -0800 | [diff] [blame] | 326 | } |
| 327 | |
Jason Sams | 715333b | 2009-11-17 17:26:46 -0800 | [diff] [blame] | 328 | public void contextDump(int bits) { |
Jason Sams | 5dbfe93 | 2010-01-27 14:41:43 -0800 | [diff] [blame] | 329 | validate(); |
Jason Sams | 715333b | 2009-11-17 17:26:46 -0800 | [diff] [blame] | 330 | nContextDump(bits); |
| 331 | } |
| 332 | |
Jason Sams | 96ed4cf | 2010-06-15 12:15:57 -0700 | [diff] [blame] | 333 | public void finish() { |
| 334 | nContextFinish(); |
| 335 | } |
| 336 | |
Jason Sams | f5b4596 | 2009-08-25 14:49:07 -0700 | [diff] [blame] | 337 | public void destroy() { |
Jason Sams | 5dbfe93 | 2010-01-27 14:41:43 -0800 | [diff] [blame] | 338 | validate(); |
Jason Sams | 516c319 | 2009-10-06 13:58:47 -0700 | [diff] [blame] | 339 | nContextDeinitToClient(); |
| 340 | mMessageThread.mRun = false; |
| 341 | |
Jason Sams | f5b4596 | 2009-08-25 14:49:07 -0700 | [diff] [blame] | 342 | nContextDestroy(mContext); |
| 343 | mContext = 0; |
| 344 | |
| 345 | nDeviceDestroy(mDev); |
| 346 | mDev = 0; |
| 347 | } |
Jason Sams | 02fb2cb | 2009-05-28 15:37:57 -0700 | [diff] [blame] | 348 | |
Jason Sams | a9e7a05 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 349 | boolean isAlive() { |
| 350 | return mContext != 0; |
| 351 | } |
| 352 | |
Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 353 | /////////////////////////////////////////////////////////////////////////////////// |
| 354 | // Root state |
| 355 | |
Jason Sams | 704ff64 | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 356 | protected int safeID(BaseObj o) { |
Jason Sams | 6b9dec0 | 2009-09-23 16:38:37 -0700 | [diff] [blame] | 357 | if(o != null) { |
| 358 | return o.mID; |
Jason Sams | d8e4161 | 2009-08-20 17:22:40 -0700 | [diff] [blame] | 359 | } |
Jason Sams | 6b9dec0 | 2009-09-23 16:38:37 -0700 | [diff] [blame] | 360 | return 0; |
Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 361 | } |
Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 362 | } |
| 363 | |
Jason Sams | 36e612a | 2009-07-31 16:26:13 -0700 | [diff] [blame] | 364 | |