blob: 3d96c5eeee4d77c1cae0b390472198a303e7bc45 [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;
22
23import java.lang.reflect.Field;
24
25public class IntAllocation {
26 private RenderScript mRS;
27 private int[] mBuffer;
28 private Allocation mAlloc;
29
30 public IntAllocation(RenderScript rs) {
31 mRS = rs;
32 }
33
34 public void save() {
35 Field[] fields = this.getClass().getFields();
36 if (mBuffer == null) {
37 int maxIndex = 0;
38 for (Field f: fields) {
39 AllocationIndex index = f.getAnnotation(AllocationIndex.class);
40 if (index != null) {
41 int value = index.value();
42 if (value > maxIndex) {
43 maxIndex = value;
44 }
45 }
46 }
47 mBuffer = new int[maxIndex+1];
48 mAlloc = Allocation.createSized(mRS, Element.USER_I32, mBuffer.length);
49 }
50 int[] buf = mBuffer;
51 for (Field f: fields) {
52 AllocationIndex index = f.getAnnotation(AllocationIndex.class);
53 if (index != null) {
54 try {
55 buf[index.value()] = f.getInt(this);
56 } catch (IllegalAccessException ex) {
57 throw new RuntimeException(ex);
58 }
59 }
60 }
61 mAlloc.data(buf);
62 }
63
64 Allocation getAllocation() {
65 return mAlloc;
66 }
67}