blob: 3b6571aaf043e7d69e2b95f92ed9b000562fb27b [file] [log] [blame]
Jason Samsb8c5a842009-07-31 20:40:47 -07001/*
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
17package android.renderscript;
18
19
20import java.io.IOException;
21import java.io.InputStream;
22
23import android.content.res.Resources;
24import android.graphics.Bitmap;
25import android.graphics.BitmapFactory;
26import android.os.Bundle;
27import android.renderscript.Type;
28import android.util.Config;
29import android.util.Log;
30
31/**
32 * @hide
33 *
34 **/
35public class Allocation extends BaseObj {
36 Allocation(int id, RenderScript rs) {
37 super(rs);
38 mID = id;
39 }
40
41 public void uploadToTexture(int baseMipLevel) {
42 mRS.nAllocationUploadToTexture(mID, baseMipLevel);
43 }
44
45 public void destroy() {
46 mRS.nAllocationDestroy(mID);
47 mID = 0;
48 }
49
50 public void data(int[] d) {
51 mRS.nAllocationData(mID, d);
52 }
53
54 public void data(float[] d) {
55 mRS.nAllocationData(mID, d);
56 }
57
58 public void subData1D(int off, int count, int[] d) {
59 mRS.nAllocationSubData1D(mID, off, count, d);
60 }
61
62 public void subData1D(int off, int count, float[] d) {
63 mRS.nAllocationSubData1D(mID, off, count, d);
64 }
65
66 public void subData2D(int xoff, int yoff, int w, int h, int[] d) {
67 mRS.nAllocationSubData2D(mID, xoff, yoff, w, h, d);
68 }
69
70 public void subData2D(int xoff, int yoff, int w, int h, float[] d) {
71 mRS.nAllocationSubData2D(mID, xoff, yoff, w, h, d);
72 }
73
74 public class Adapter1D extends BaseObj {
75 Adapter1D(int id, RenderScript rs) {
76 super(rs);
77 mID = id;
78 }
79
80 public void destroy() {
81 mRS.nAdapter1DDestroy(mID);
82 mID = 0;
83 }
84
85 public void setConstraint(Dimension dim, int value) {
86 mRS.nAdapter1DSetConstraint(mID, dim.mID, value);
87 }
88
89 public void data(int[] d) {
90 mRS.nAdapter1DData(mID, d);
91 }
92
Jason Samsb8c5a842009-07-31 20:40:47 -070093 public void data(float[] d) {
94 mRS.nAdapter1DData(mID, d);
95 }
96
Jason Samsbd1c3ad2009-08-03 16:03:08 -070097 public void subData(int off, int count, int[] d) {
98 mRS.nAdapter1DSubData(mID, off, count, d);
99 }
100
Jason Samsb8c5a842009-07-31 20:40:47 -0700101 public void subData(int off, int count, float[] d) {
102 mRS.nAdapter1DSubData(mID, off, count, d);
103 }
104 }
105
106 public Adapter1D createAdapter1D() {
107 int id = mRS.nAdapter1DCreate();
108 if (id != 0) {
109 mRS.nAdapter1DBindAllocation(id, mID);
110 }
111 return new Adapter1D(id, mRS);
112 }
113
114
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700115 public class Adapter2D extends BaseObj {
116 Adapter2D(int id, RenderScript rs) {
117 super(rs);
118 mID = id;
119 }
120
121 public void destroy() {
122 mRS.nAdapter2DDestroy(mID);
123 mID = 0;
124 }
125
126 public void setConstraint(Dimension dim, int value) {
127 mRS.nAdapter2DSetConstraint(mID, dim.mID, value);
128 }
129
130 public void data(int[] d) {
131 mRS.nAdapter2DData(mID, d);
132 }
133
134 public void data(float[] d) {
135 mRS.nAdapter2DData(mID, d);
136 }
137
138 public void subData(int xoff, int yoff, int w, int h, int[] d) {
139 mRS.nAdapter2DSubData(mID, xoff, yoff, w, h, d);
140 }
141
142 public void subData(int xoff, int yoff, int w, int h, float[] d) {
143 mRS.nAdapter2DSubData(mID, xoff, yoff, w, h, d);
144 }
145 }
146
147 public Adapter2D createAdapter2D() {
148 int id = mRS.nAdapter2DCreate();
149 if (id != 0) {
150 mRS.nAdapter2DBindAllocation(id, mID);
151 }
152 return new Adapter2D(id, mRS);
153 }
154
Jason Samsb8c5a842009-07-31 20:40:47 -0700155
156 // creation
157
158 private static BitmapFactory.Options mBitmapOptions = new BitmapFactory.Options();
159 static {
160 mBitmapOptions.inScaled = false;
161 }
162
163 static public Allocation createTyped(RenderScript rs, Type type) {
164 int id = rs.nAllocationCreateTyped(type.mID);
165 return new Allocation(id, rs);
166 }
167
168 static public Allocation createSized(RenderScript rs, Element e, int count) {
169 int id;
170 if(e.mIsPredefined) {
171 id = rs.nAllocationCreatePredefSized(e.mPredefinedID, count);
172 } else {
173 id = rs.nAllocationCreateSized(e.mID, count);
174 }
175 return new Allocation(id, rs);
176 }
177
178 static public Allocation createFromBitmap(RenderScript rs, Bitmap b, Element dstFmt, boolean genMips)
179 throws IllegalArgumentException {
180 if(!dstFmt.mIsPredefined) {
181 throw new IllegalStateException("Attempting to allocate a bitmap with a non-static element.");
182 }
183
184 int id = rs.nAllocationCreateFromBitmap(dstFmt.mPredefinedID, genMips, b);
185 return new Allocation(id, rs);
186 }
187
188 static public Allocation createFromBitmapBoxed(RenderScript rs, Bitmap b, Element dstFmt, boolean genMips)
189 throws IllegalArgumentException {
190 if(!dstFmt.mIsPredefined) {
191 throw new IllegalStateException("Attempting to allocate a bitmap with a non-static element.");
192 }
193
194 int id = rs.nAllocationCreateFromBitmapBoxed(dstFmt.mPredefinedID, genMips, b);
195 return new Allocation(id, rs);
196 }
197
198 static public Allocation createFromBitmapResource(RenderScript rs, Resources res, int id, Element dstFmt, boolean genMips)
199 throws IllegalArgumentException {
200
201 Bitmap b = BitmapFactory.decodeResource(res, id, mBitmapOptions);
202 return createFromBitmap(rs, b, dstFmt, genMips);
203 }
204
205 static public Allocation createFromBitmapResourceBoxed(RenderScript rs, Resources res, int id, Element dstFmt, boolean genMips)
206 throws IllegalArgumentException {
207
208 Bitmap b = BitmapFactory.decodeResource(res, id, mBitmapOptions);
209 return createFromBitmapBoxed(rs, b, dstFmt, genMips);
210 }
211
212
213}
214
215