Joe Onorato | 43e7bcf | 2009-08-08 18:53:53 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 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 com.android.launcher2; |
| 18 | |
| 19 | import android.renderscript.Allocation; |
| 20 | import android.renderscript.Element; |
| 21 | import android.renderscript.RenderScript; |
Joe Onorato | 1feb3a8 | 2009-08-08 22:32:00 -0700 | [diff] [blame] | 22 | import android.util.Log; |
Joe Onorato | 43e7bcf | 2009-08-08 18:53:53 -0700 | [diff] [blame] | 23 | |
| 24 | import java.lang.reflect.Field; |
| 25 | |
| 26 | public class IntAllocation { |
| 27 | private RenderScript mRS; |
| 28 | private int[] mBuffer; |
| 29 | private Allocation mAlloc; |
| 30 | |
| 31 | public IntAllocation(RenderScript rs) { |
| 32 | mRS = rs; |
| 33 | } |
| 34 | |
| 35 | public void save() { |
| 36 | Field[] fields = this.getClass().getFields(); |
| 37 | if (mBuffer == null) { |
| 38 | int maxIndex = 0; |
| 39 | for (Field f: fields) { |
| 40 | AllocationIndex index = f.getAnnotation(AllocationIndex.class); |
| 41 | if (index != null) { |
| 42 | int value = index.value(); |
| 43 | if (value > maxIndex) { |
| 44 | maxIndex = value; |
| 45 | } |
| 46 | } |
| 47 | } |
| 48 | mBuffer = new int[maxIndex+1]; |
Joe Onorato | 1feb3a8 | 2009-08-08 22:32:00 -0700 | [diff] [blame] | 49 | if (true) { |
| 50 | // helpful debugging check |
| 51 | for (Field f: fields) { |
| 52 | AllocationIndex index = f.getAnnotation(AllocationIndex.class); |
| 53 | if (index != null) { |
| 54 | int i = index.value(); |
| 55 | if (mBuffer[i] != 0) { |
| 56 | throw new RuntimeException("@AllocationIndex on field in class " |
| 57 | + this.getClass().getName() + " with duplicate value " |
| 58 | + i + " for field " + f.getName() + ". The other field is " |
| 59 | + fields[mBuffer[i]-1].getName() + '.'); |
| 60 | } |
| 61 | mBuffer[i] = i+1; |
| 62 | } |
| 63 | } |
| 64 | for (int i=0; i<mBuffer.length; i++) { |
| 65 | mBuffer[i] = 0; |
| 66 | } |
| 67 | } |
Joe Onorato | 43e7bcf | 2009-08-08 18:53:53 -0700 | [diff] [blame] | 68 | mAlloc = Allocation.createSized(mRS, Element.USER_I32, mBuffer.length); |
| 69 | } |
| 70 | int[] buf = mBuffer; |
| 71 | for (Field f: fields) { |
| 72 | AllocationIndex index = f.getAnnotation(AllocationIndex.class); |
| 73 | if (index != null) { |
| 74 | try { |
| 75 | buf[index.value()] = f.getInt(this); |
| 76 | } catch (IllegalAccessException ex) { |
| 77 | throw new RuntimeException(ex); |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | mAlloc.data(buf); |
| 82 | } |
| 83 | |
Joe Onorato | d769a63 | 2009-08-11 17:09:02 -0700 | [diff] [blame] | 84 | public void read() { |
| 85 | int[] buf = mBuffer; |
| 86 | if (buf != null) { |
| 87 | mAlloc.readData(buf); |
| 88 | Field[] fields = this.getClass().getFields(); |
| 89 | for (Field f: fields) { |
| 90 | AllocationIndex index = f.getAnnotation(AllocationIndex.class); |
| 91 | if (index != null) { |
| 92 | try { |
| 93 | f.setInt(this, buf[index.value()]); |
| 94 | } catch (IllegalAccessException ex) { |
| 95 | throw new RuntimeException(ex); |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | |
Joe Onorato | 43e7bcf | 2009-08-08 18:53:53 -0700 | [diff] [blame] | 102 | Allocation getAllocation() { |
| 103 | return mAlloc; |
| 104 | } |
| 105 | } |