Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -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 | |
| 17 | package android.renderscript; |
| 18 | |
Jason Sams | 43ee0685 | 2009-08-12 17:54:11 -0700 | [diff] [blame^] | 19 | import java.lang.reflect.Field; |
| 20 | |
| 21 | import android.renderscript.Element; |
Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 22 | import android.util.Config; |
| 23 | import android.util.Log; |
| 24 | |
Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 25 | /** |
| 26 | * @hide |
| 27 | * |
| 28 | **/ |
| 29 | public class Type extends BaseObj { |
Jason Sams | 1bada8c | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 30 | Dimension[] mDimensions; |
| 31 | int[] mValues; |
| 32 | Element mElement; |
Jason Sams | 43ee0685 | 2009-08-12 17:54:11 -0700 | [diff] [blame^] | 33 | private int mNativeCache; |
| 34 | Class mJavaClass; |
Jason Sams | 1bada8c | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 35 | |
| 36 | |
Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 37 | Type(int id, RenderScript rs) { |
| 38 | super(rs); |
| 39 | mID = id; |
Jason Sams | 43ee0685 | 2009-08-12 17:54:11 -0700 | [diff] [blame^] | 40 | mNativeCache = 0; |
| 41 | } |
| 42 | |
| 43 | protected void finalize() throws Throwable { |
| 44 | if(mNativeCache) { |
| 45 | mRS.nTypeFinalDestroy(this); |
| 46 | mNativeCache = 0; |
| 47 | } |
| 48 | super.finalize(); |
Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | public void destroy() { |
Jason Sams | 1bada8c | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 52 | if(mDestroyed) { |
| 53 | throw new IllegalStateException("Object already destroyed."); |
| 54 | } |
| 55 | mDestroyed = true; |
Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 56 | mRS.nTypeDestroy(mID); |
Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 57 | } |
| 58 | |
Jason Sams | 43ee0685 | 2009-08-12 17:54:11 -0700 | [diff] [blame^] | 59 | public static Type createFromClass(RenderScript rs, Class c, int size, String scriptName) { |
| 60 | Element e = Element.createFromClass(rs, c); |
| 61 | Builder b = new Builder(rs, e); |
| 62 | b.add(Dimension.X, size); |
| 63 | Type t = b.create(); |
| 64 | e.destroy(); |
| 65 | |
| 66 | // native fields |
| 67 | { |
| 68 | Field[] fields = c.getFields(); |
| 69 | int[] arTypes = new int[fields.length]; |
| 70 | int[] arBits = new int[fields.length]; |
| 71 | |
| 72 | for(int ct=0; ct < fields.length; ct++) { |
| 73 | Field f = fields[ct]; |
| 74 | Class fc = f.getType(); |
| 75 | if(fc == int.class) { |
| 76 | arTypes[ct] = Element.DataType.SIGNED.mID; |
| 77 | arBits[ct] = 32; |
| 78 | } else if(fc == short.class) { |
| 79 | arTypes[ct] = Element.DataType.SIGNED.mID; |
| 80 | arBits[ct] = 16; |
| 81 | } else if(fc == byte.class) { |
| 82 | arTypes[ct] = Element.DataType.SIGNED.mID; |
| 83 | arBits[ct] = 8; |
| 84 | } else if(fc == float.class) { |
| 85 | arTypes[ct] = Element.DataType.FLOAT.mID; |
| 86 | arBits[ct] = 32; |
| 87 | } else { |
| 88 | throw new IllegalArgumentException("Unkown field type"); |
| 89 | } |
| 90 | } |
| 91 | rs.nTypeSetupFields(t, arTypes, arBits, fields); |
| 92 | } |
| 93 | t.mJavaClass = c; |
| 94 | t.setName(scriptName); |
| 95 | return t; |
| 96 | } |
| 97 | |
Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 98 | public static class Builder { |
| 99 | RenderScript mRS; |
Jason Sams | 2253417 | 2009-08-04 16:58:20 -0700 | [diff] [blame] | 100 | Entry[] mEntries; |
| 101 | int mEntryCount; |
| 102 | Element mElement; |
Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 103 | |
Jason Sams | 2253417 | 2009-08-04 16:58:20 -0700 | [diff] [blame] | 104 | class Entry { |
| 105 | Dimension mDim; |
| 106 | int mValue; |
Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 107 | } |
| 108 | |
Jason Sams | 2253417 | 2009-08-04 16:58:20 -0700 | [diff] [blame] | 109 | public Builder(RenderScript rs, Element e) { |
| 110 | mRS = rs; |
| 111 | mEntries = new Entry[4]; |
| 112 | mElement = e; |
Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | public void add(Dimension d, int value) { |
Jason Sams | 2253417 | 2009-08-04 16:58:20 -0700 | [diff] [blame] | 116 | if(mEntries.length >= mEntryCount) { |
| 117 | Entry[] en = new Entry[mEntryCount + 8]; |
| 118 | for(int ct=0; ct < mEntries.length; ct++) { |
| 119 | en[ct] = mEntries[ct]; |
| 120 | } |
| 121 | mEntries = en; |
| 122 | } |
| 123 | mEntries[mEntryCount] = new Entry(); |
| 124 | mEntries[mEntryCount].mDim = d; |
| 125 | mEntries[mEntryCount].mValue = value; |
| 126 | mEntryCount++; |
| 127 | } |
| 128 | |
| 129 | static synchronized Type internalCreate(RenderScript rs, Builder b) { |
| 130 | rs.nTypeBegin(b.mElement.mID); |
| 131 | for (int ct=0; ct < b.mEntryCount; ct++) { |
| 132 | Entry en = b.mEntries[ct]; |
| 133 | rs.nTypeAdd(en.mDim.mID, en.mValue); |
| 134 | } |
| 135 | int id = rs.nTypeCreate(); |
| 136 | return new Type(id, rs); |
Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | public Type create() { |
Jason Sams | 1bada8c | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 140 | Type t = internalCreate(mRS, this); |
| 141 | t.mElement = mElement; |
| 142 | t.mDimensions = new Dimension[mEntryCount]; |
| 143 | t.mValues = new int[mEntryCount]; |
| 144 | for(int ct=0; ct < mEntryCount; ct++) { |
| 145 | t.mDimensions[ct] = mEntries[ct].mDim; |
| 146 | t.mValues[ct] = mEntries[ct].mValue; |
| 147 | } |
| 148 | return t; |
Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 149 | } |
| 150 | } |
| 151 | |
| 152 | } |