blob: ab9d9155d1b886dad78f23f43277fd674e571583 [file] [log] [blame]
Joe Onorato43e7bcf2009-08-08 18:53:53 -07001/*
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
17package com.android.launcher2;
18
19import android.renderscript.Allocation;
20import android.renderscript.Element;
21import android.renderscript.RenderScript;
Joe Onorato1feb3a82009-08-08 22:32:00 -070022import android.util.Log;
Joe Onorato43e7bcf2009-08-08 18:53:53 -070023
24import java.lang.reflect.Field;
25
26public 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 Onorato1feb3a82009-08-08 22:32:00 -070049 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 Onorato43e7bcf2009-08-08 18:53:53 -070068 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
84 Allocation getAllocation() {
85 return mAlloc;
86 }
87}