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 | |
Alex Sakhartchouk | dfac814 | 2010-07-15 11:33:03 -0700 | [diff] [blame] | 19 | |
Jason Sams | 43ee0685 | 2009-08-12 17:54:11 -0700 | [diff] [blame] | 20 | import java.lang.reflect.Field; |
Alex Sakhartchouk | dfac814 | 2010-07-15 11:33:03 -0700 | [diff] [blame] | 21 | import android.util.Log; |
Jason Sams | 43ee0685 | 2009-08-12 17:54:11 -0700 | [diff] [blame] | 22 | |
Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 23 | /** |
| 24 | * @hide |
| 25 | * |
| 26 | **/ |
| 27 | public class Type extends BaseObj { |
Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 28 | int mDimX; |
| 29 | int mDimY; |
| 30 | int mDimZ; |
| 31 | boolean mDimLOD; |
| 32 | boolean mDimFaces; |
| 33 | int mElementCount; |
Jason Sams | 1bada8c | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 34 | Element mElement; |
Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 35 | |
Jason Sams | 43ee0685 | 2009-08-12 17:54:11 -0700 | [diff] [blame] | 36 | private int mNativeCache; |
| 37 | Class mJavaClass; |
Jason Sams | 1bada8c | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 38 | |
Jason Sams | e17964e | 2010-01-04 16:52:27 -0800 | [diff] [blame] | 39 | public Element getElement() { |
| 40 | return mElement; |
| 41 | } |
Jason Sams | 1bada8c | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 42 | |
Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 43 | public int getX() { |
| 44 | return mDimX; |
| 45 | } |
| 46 | public int getY() { |
| 47 | return mDimY; |
| 48 | } |
| 49 | public int getZ() { |
| 50 | return mDimZ; |
| 51 | } |
| 52 | public boolean getLOD() { |
| 53 | return mDimLOD; |
| 54 | } |
| 55 | public boolean getFaces() { |
| 56 | return mDimFaces; |
| 57 | } |
| 58 | public int getElementCount() { |
| 59 | return mElementCount; |
| 60 | } |
| 61 | |
| 62 | void calcElementCount() { |
| 63 | boolean hasLod = getLOD(); |
| 64 | int x = getX(); |
| 65 | int y = getY(); |
| 66 | int z = getZ(); |
| 67 | int faces = 1; |
| 68 | if(getFaces()) { |
| 69 | faces = 6; |
| 70 | } |
| 71 | if(x == 0) { |
| 72 | x = 1; |
| 73 | } |
| 74 | if(y == 0) { |
| 75 | y = 1; |
| 76 | } |
| 77 | if(z == 0) { |
| 78 | z = 1; |
| 79 | } |
| 80 | |
| 81 | int count = x * y * z * faces; |
| 82 | if(hasLod && (x > 1) && (y > 1) && (z > 1)) { |
| 83 | if(x > 1) { |
| 84 | x >>= 1; |
| 85 | } |
| 86 | if(y > 1) { |
| 87 | y >>= 1; |
| 88 | } |
| 89 | if(z > 1) { |
| 90 | z >>= 1; |
| 91 | } |
| 92 | |
| 93 | count += x * y * z * faces; |
| 94 | } |
| 95 | mElementCount = count; |
| 96 | } |
| 97 | |
| 98 | |
Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 99 | Type(int id, RenderScript rs) { |
Alex Sakhartchouk | 0de9444 | 2010-08-11 14:41:28 -0700 | [diff] [blame^] | 100 | super(id, rs); |
Jason Sams | 43ee0685 | 2009-08-12 17:54:11 -0700 | [diff] [blame] | 101 | mNativeCache = 0; |
| 102 | } |
| 103 | |
| 104 | protected void finalize() throws Throwable { |
Jason Sams | 1b52aae | 2009-08-12 18:50:44 -0700 | [diff] [blame] | 105 | if(mNativeCache != 0) { |
Jason Sams | 43ee0685 | 2009-08-12 17:54:11 -0700 | [diff] [blame] | 106 | mRS.nTypeFinalDestroy(this); |
| 107 | mNativeCache = 0; |
| 108 | } |
| 109 | super.finalize(); |
Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 110 | } |
| 111 | |
Alex Sakhartchouk | dfac814 | 2010-07-15 11:33:03 -0700 | [diff] [blame] | 112 | @Override |
| 113 | void updateFromNative() { |
| 114 | // We have 6 integer to obtain mDimX; mDimY; mDimZ; |
| 115 | // mDimLOD; mDimFaces; mElement; |
| 116 | int[] dataBuffer = new int[6]; |
| 117 | mRS.nTypeGetNativeData(mID, dataBuffer); |
| 118 | |
| 119 | mDimX = dataBuffer[0]; |
| 120 | mDimY = dataBuffer[1]; |
| 121 | mDimZ = dataBuffer[2]; |
| 122 | mDimLOD = dataBuffer[3] == 1 ? true : false; |
| 123 | mDimFaces = dataBuffer[4] == 1 ? true : false; |
| 124 | |
| 125 | int elementID = dataBuffer[5]; |
| 126 | if(elementID != 0) { |
Alex Sakhartchouk | 0de9444 | 2010-08-11 14:41:28 -0700 | [diff] [blame^] | 127 | mElement = new Element(elementID, mRS); |
Alex Sakhartchouk | dfac814 | 2010-07-15 11:33:03 -0700 | [diff] [blame] | 128 | mElement.updateFromNative(); |
| 129 | } |
| 130 | calcElementCount(); |
| 131 | } |
| 132 | |
Jason Sams | fbf0b9e | 2009-08-13 12:59:04 -0700 | [diff] [blame] | 133 | public static Type createFromClass(RenderScript rs, Class c, int size, String scriptName) { |
Jason Sams | b42315d | 2010-05-14 16:29:20 -0700 | [diff] [blame] | 134 | android.util.Log.e("RenderScript", "Calling depricated createFromClass"); |
| 135 | return null; |
Jason Sams | 43ee0685 | 2009-08-12 17:54:11 -0700 | [diff] [blame] | 136 | } |
| 137 | |
Jason Sams | fbf0b9e | 2009-08-13 12:59:04 -0700 | [diff] [blame] | 138 | |
Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 139 | public static class Builder { |
| 140 | RenderScript mRS; |
Jason Sams | 2253417 | 2009-08-04 16:58:20 -0700 | [diff] [blame] | 141 | Entry[] mEntries; |
| 142 | int mEntryCount; |
| 143 | Element mElement; |
Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 144 | |
Jason Sams | 2253417 | 2009-08-04 16:58:20 -0700 | [diff] [blame] | 145 | class Entry { |
| 146 | Dimension mDim; |
| 147 | int mValue; |
Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 148 | } |
| 149 | |
Jason Sams | 2253417 | 2009-08-04 16:58:20 -0700 | [diff] [blame] | 150 | public Builder(RenderScript rs, Element e) { |
Jason Sams | 3c0dfba | 2009-09-27 17:50:38 -0700 | [diff] [blame] | 151 | if(e.mID == 0) { |
| 152 | throw new IllegalArgumentException("Invalid element."); |
| 153 | } |
| 154 | |
Jason Sams | 2253417 | 2009-08-04 16:58:20 -0700 | [diff] [blame] | 155 | mRS = rs; |
| 156 | mEntries = new Entry[4]; |
| 157 | mElement = e; |
Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | public void add(Dimension d, int value) { |
Jason Sams | 3c0dfba | 2009-09-27 17:50:38 -0700 | [diff] [blame] | 161 | if(value < 1) { |
| 162 | throw new IllegalArgumentException("Values of less than 1 for Dimensions are not valid."); |
| 163 | } |
Jason Sams | 2253417 | 2009-08-04 16:58:20 -0700 | [diff] [blame] | 164 | if(mEntries.length >= mEntryCount) { |
| 165 | Entry[] en = new Entry[mEntryCount + 8]; |
Romain Guy | 484d57f | 2009-08-19 12:10:03 -0700 | [diff] [blame] | 166 | System.arraycopy(mEntries, 0, en, 0, mEntries.length); |
Jason Sams | 2253417 | 2009-08-04 16:58:20 -0700 | [diff] [blame] | 167 | mEntries = en; |
| 168 | } |
| 169 | mEntries[mEntryCount] = new Entry(); |
| 170 | mEntries[mEntryCount].mDim = d; |
| 171 | mEntries[mEntryCount].mValue = value; |
| 172 | mEntryCount++; |
| 173 | } |
| 174 | |
| 175 | static synchronized Type internalCreate(RenderScript rs, Builder b) { |
| 176 | rs.nTypeBegin(b.mElement.mID); |
| 177 | for (int ct=0; ct < b.mEntryCount; ct++) { |
| 178 | Entry en = b.mEntries[ct]; |
| 179 | rs.nTypeAdd(en.mDim.mID, en.mValue); |
| 180 | } |
| 181 | int id = rs.nTypeCreate(); |
| 182 | return new Type(id, rs); |
Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | public Type create() { |
Jason Sams | 1bada8c | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 186 | Type t = internalCreate(mRS, this); |
| 187 | t.mElement = mElement; |
Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 188 | |
Jason Sams | 1bada8c | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 189 | for(int ct=0; ct < mEntryCount; ct++) { |
Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 190 | if(mEntries[ct].mDim == Dimension.X) { |
| 191 | t.mDimX = mEntries[ct].mValue; |
| 192 | } |
| 193 | if(mEntries[ct].mDim == Dimension.Y) { |
| 194 | t.mDimY = mEntries[ct].mValue; |
| 195 | } |
| 196 | if(mEntries[ct].mDim == Dimension.Z) { |
| 197 | t.mDimZ = mEntries[ct].mValue; |
| 198 | } |
| 199 | if(mEntries[ct].mDim == Dimension.LOD) { |
| 200 | t.mDimLOD = mEntries[ct].mValue != 0; |
| 201 | } |
| 202 | if(mEntries[ct].mDim == Dimension.FACE) { |
| 203 | t.mDimFaces = mEntries[ct].mValue != 0; |
| 204 | } |
Jason Sams | 1bada8c | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 205 | } |
Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 206 | t.calcElementCount(); |
Jason Sams | 1bada8c | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 207 | return t; |
Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 208 | } |
| 209 | } |
| 210 | |
| 211 | } |