Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 1 | /* |
Stephen Hines | 9069ee8 | 2012-02-13 18:25:54 -0800 | [diff] [blame] | 2 | * Copyright (C) 2008-2012 The Android Open Source Project |
Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 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 | |
Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 19 | import java.io.IOException; |
| 20 | import java.io.InputStream; |
Jason Sams | 739c826 | 2013-04-11 18:07:52 -0700 | [diff] [blame] | 21 | import java.util.HashMap; |
Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 22 | import android.content.res.Resources; |
Romain Guy | 650a3eb | 2009-08-31 14:06:43 -0700 | [diff] [blame] | 23 | import android.content.res.AssetManager; |
Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 24 | import android.graphics.Bitmap; |
| 25 | import android.graphics.BitmapFactory; |
Jason Sams | fb9aa9f | 2012-03-28 15:30:07 -0700 | [diff] [blame] | 26 | import android.view.Surface; |
Jason Sams | 615e7ce | 2012-01-13 14:01:20 -0800 | [diff] [blame] | 27 | import android.graphics.SurfaceTexture; |
Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 28 | import android.util.Log; |
Romain Guy | 650a3eb | 2009-08-31 14:06:43 -0700 | [diff] [blame] | 29 | import android.util.TypedValue; |
Tim Murray | abd5db9 | 2013-02-28 11:45:22 -0800 | [diff] [blame] | 30 | import android.graphics.Canvas; |
Tim Murray | 6d7a53c | 2013-05-23 16:59:23 -0700 | [diff] [blame] | 31 | import android.os.Trace; |
Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 32 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 33 | /** |
Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 34 | * <p> This class provides the primary method through which data is passed to |
| 35 | * and from RenderScript kernels. An Allocation provides the backing store for |
| 36 | * a given {@link android.renderscript.Type}. </p> |
Jason Sams | a23d4e7 | 2011-01-04 18:59:12 -0800 | [diff] [blame] | 37 | * |
Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 38 | * <p>An Allocation also contains a set of usage flags that denote how the |
| 39 | * Allocation could be used. For example, an Allocation may have usage flags |
| 40 | * specifying that it can be used from a script as well as input to a {@link |
| 41 | * android.renderscript.Sampler}. A developer must synchronize across these |
| 42 | * different usages using {@link android.renderscript.Allocation#syncAll} in |
| 43 | * order to ensure that different users of the Allocation have a consistent view |
| 44 | * of memory. For example, in the case where an Allocation is used as the output |
| 45 | * of one kernel and as Sampler input in a later kernel, a developer must call |
| 46 | * {@link #syncAll syncAll(Allocation.USAGE_SCRIPT)} prior to launching the |
| 47 | * second kernel to ensure correctness. |
Jason Sams | a23d4e7 | 2011-01-04 18:59:12 -0800 | [diff] [blame] | 48 | * |
Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 49 | * <p>An Allocation can be populated with the {@link #copyFrom} routines. For |
| 50 | * more complex Element types, the {@link #copyFromUnchecked} methods can be |
| 51 | * used to copy from byte arrays or similar constructs.</p> |
Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 52 | * |
Joe Fernandez | 3aef8e1d | 2011-12-20 10:38:34 -0800 | [diff] [blame] | 53 | * <div class="special reference"> |
| 54 | * <h3>Developer Guides</h3> |
Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 55 | * <p>For more information about creating an application that uses RenderScript, read the |
| 56 | * <a href="{@docRoot}guide/topics/renderscript/index.html">RenderScript</a> developer guide.</p> |
Joe Fernandez | 3aef8e1d | 2011-12-20 10:38:34 -0800 | [diff] [blame] | 57 | * </div> |
Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 58 | **/ |
| 59 | public class Allocation extends BaseObj { |
Jason Sams | 43ee0685 | 2009-08-12 17:54:11 -0700 | [diff] [blame] | 60 | Type mType; |
Jason Sams | 8a64743 | 2010-03-01 15:31:04 -0800 | [diff] [blame] | 61 | Bitmap mBitmap; |
Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 62 | int mUsage; |
Jason Sams | ba862d1 | 2011-07-07 15:24:42 -0700 | [diff] [blame] | 63 | Allocation mAdaptedAllocation; |
| 64 | |
| 65 | boolean mConstrainedLOD; |
| 66 | boolean mConstrainedFace; |
| 67 | boolean mConstrainedY; |
| 68 | boolean mConstrainedZ; |
Jason Sams | 615e7ce | 2012-01-13 14:01:20 -0800 | [diff] [blame] | 69 | boolean mReadAllowed = true; |
| 70 | boolean mWriteAllowed = true; |
Jason Sams | ba862d1 | 2011-07-07 15:24:42 -0700 | [diff] [blame] | 71 | int mSelectedY; |
| 72 | int mSelectedZ; |
| 73 | int mSelectedLOD; |
| 74 | Type.CubemapFace mSelectedFace = Type.CubemapFace.POSITIVE_X; |
| 75 | |
| 76 | int mCurrentDimX; |
| 77 | int mCurrentDimY; |
| 78 | int mCurrentDimZ; |
| 79 | int mCurrentCount; |
Jason Sams | 739c826 | 2013-04-11 18:07:52 -0700 | [diff] [blame] | 80 | static HashMap<Integer, Allocation> mAllocationMap = |
| 81 | new HashMap<Integer, Allocation>(); |
| 82 | IoInputNotifier mBufferNotifier; |
Jason Sams | ba862d1 | 2011-07-07 15:24:42 -0700 | [diff] [blame] | 83 | |
Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 84 | /** |
| 85 | * The usage of the Allocation. These signal to RenderScript where to place |
| 86 | * the Allocation in memory. |
| 87 | * |
| 88 | */ |
Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 89 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 90 | /** |
Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 91 | * The Allocation will be bound to and accessed by scripts. |
Jason Sams | f708609 | 2011-01-12 13:28:37 -0800 | [diff] [blame] | 92 | */ |
Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 93 | public static final int USAGE_SCRIPT = 0x0001; |
Jason Sams | f708609 | 2011-01-12 13:28:37 -0800 | [diff] [blame] | 94 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 95 | /** |
Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 96 | * The Allocation will be used as a texture source by one or more graphics |
| 97 | * programs. |
Jason Sams | f708609 | 2011-01-12 13:28:37 -0800 | [diff] [blame] | 98 | * |
| 99 | */ |
Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 100 | public static final int USAGE_GRAPHICS_TEXTURE = 0x0002; |
Jason Sams | f708609 | 2011-01-12 13:28:37 -0800 | [diff] [blame] | 101 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 102 | /** |
Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 103 | * The Allocation will be used as a graphics mesh. |
| 104 | * |
| 105 | * This was deprecated in API level 16. |
Jason Sams | f708609 | 2011-01-12 13:28:37 -0800 | [diff] [blame] | 106 | * |
| 107 | */ |
Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 108 | public static final int USAGE_GRAPHICS_VERTEX = 0x0004; |
Jason Sams | f708609 | 2011-01-12 13:28:37 -0800 | [diff] [blame] | 109 | |
| 110 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 111 | /** |
Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 112 | * The Allocation will be used as the source of shader constants by one or |
| 113 | * more programs. |
| 114 | * |
| 115 | * This was deprecated in API level 16. |
Jason Sams | f708609 | 2011-01-12 13:28:37 -0800 | [diff] [blame] | 116 | * |
| 117 | */ |
Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 118 | public static final int USAGE_GRAPHICS_CONSTANTS = 0x0008; |
| 119 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 120 | /** |
Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 121 | * The Allocation will be used as a target for offscreen rendering |
| 122 | * |
| 123 | * This was deprecated in API level 16. |
Alex Sakhartchouk | 8e90f2b | 2011-04-01 14:19:01 -0700 | [diff] [blame] | 124 | * |
| 125 | */ |
| 126 | public static final int USAGE_GRAPHICS_RENDER_TARGET = 0x0010; |
| 127 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 128 | /** |
Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 129 | * The Allocation will be used as a {@link android.graphics.SurfaceTexture} |
| 130 | * consumer. This usage will cause the Allocation to be created as |
| 131 | * read-only. |
Jason Sams | 615e7ce | 2012-01-13 14:01:20 -0800 | [diff] [blame] | 132 | * |
Jason Sams | 615e7ce | 2012-01-13 14:01:20 -0800 | [diff] [blame] | 133 | */ |
Jason Sams | fe1d5ff | 2012-03-23 11:47:26 -0700 | [diff] [blame] | 134 | public static final int USAGE_IO_INPUT = 0x0020; |
Stephen Hines | 9069ee8 | 2012-02-13 18:25:54 -0800 | [diff] [blame] | 135 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 136 | /** |
Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 137 | * The Allocation will be used as a {@link android.graphics.SurfaceTexture} |
| 138 | * producer. The dimensions and format of the {@link |
| 139 | * android.graphics.SurfaceTexture} will be forced to those of the |
| 140 | * Allocation. |
Jason Sams | 615e7ce | 2012-01-13 14:01:20 -0800 | [diff] [blame] | 141 | * |
Jason Sams | 615e7ce | 2012-01-13 14:01:20 -0800 | [diff] [blame] | 142 | */ |
Jason Sams | fe1d5ff | 2012-03-23 11:47:26 -0700 | [diff] [blame] | 143 | public static final int USAGE_IO_OUTPUT = 0x0040; |
Jason Sams | 43ee0685 | 2009-08-12 17:54:11 -0700 | [diff] [blame] | 144 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 145 | /** |
Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 146 | * The Allocation's backing store will be inherited from another object |
| 147 | * (usually a {@link android.graphics.Bitmap}); copying to or from the |
| 148 | * original source Bitmap will cause a synchronization rather than a full |
| 149 | * copy. {@link #syncAll} may also be used to synchronize the Allocation |
| 150 | * and the source Bitmap. |
Tim Murray | 00bb454 | 2012-12-17 16:35:06 -0800 | [diff] [blame] | 151 | * |
Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 152 | * <p>This is set by default for allocations created with {@link |
| 153 | * #createFromBitmap} in API version 18 and higher.</p> |
Tim Murray | 00bb454 | 2012-12-17 16:35:06 -0800 | [diff] [blame] | 154 | * |
| 155 | */ |
| 156 | public static final int USAGE_SHARED = 0x0080; |
| 157 | |
| 158 | /** |
Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 159 | * Controls mipmap behavior when using the bitmap creation and update |
| 160 | * functions. |
Jason Sams | f708609 | 2011-01-12 13:28:37 -0800 | [diff] [blame] | 161 | */ |
Jason Sams | 4ef6650 | 2010-12-10 16:03:15 -0800 | [diff] [blame] | 162 | public enum MipmapControl { |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 163 | /** |
Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 164 | * No mipmaps will be generated and the type generated from the incoming |
| 165 | * bitmap will not contain additional LODs. |
Jason Sams | f708609 | 2011-01-12 13:28:37 -0800 | [diff] [blame] | 166 | */ |
Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 167 | MIPMAP_NONE(0), |
Jason Sams | f708609 | 2011-01-12 13:28:37 -0800 | [diff] [blame] | 168 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 169 | /** |
Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 170 | * A full mipmap chain will be created in script memory. The Type of |
| 171 | * the Allocation will contain a full mipmap chain. On upload, the full |
| 172 | * chain will be transferred. |
Jason Sams | f708609 | 2011-01-12 13:28:37 -0800 | [diff] [blame] | 173 | */ |
Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 174 | MIPMAP_FULL(1), |
Jason Sams | f708609 | 2011-01-12 13:28:37 -0800 | [diff] [blame] | 175 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 176 | /** |
Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 177 | * The Type of the Allocation will be the same as MIPMAP_NONE. It will |
| 178 | * not contain mipmaps. On upload, the allocation data will contain a |
| 179 | * full mipmap chain generated from the top level in script memory. |
Jason Sams | f708609 | 2011-01-12 13:28:37 -0800 | [diff] [blame] | 180 | */ |
Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 181 | MIPMAP_ON_SYNC_TO_TEXTURE(2); |
| 182 | |
| 183 | int mID; |
Jason Sams | 4ef6650 | 2010-12-10 16:03:15 -0800 | [diff] [blame] | 184 | MipmapControl(int id) { |
Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 185 | mID = id; |
| 186 | } |
Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 187 | } |
| 188 | |
Jason Sams | 48fe534 | 2011-07-08 13:52:30 -0700 | [diff] [blame] | 189 | |
| 190 | private int getIDSafe() { |
| 191 | if (mAdaptedAllocation != null) { |
Jason Sams | e07694b | 2012-04-03 15:36:36 -0700 | [diff] [blame] | 192 | return mAdaptedAllocation.getID(mRS); |
Jason Sams | 48fe534 | 2011-07-08 13:52:30 -0700 | [diff] [blame] | 193 | } |
Jason Sams | e07694b | 2012-04-03 15:36:36 -0700 | [diff] [blame] | 194 | return getID(mRS); |
Jason Sams | 48fe534 | 2011-07-08 13:52:30 -0700 | [diff] [blame] | 195 | } |
| 196 | |
Jason Sams | 03d2d00 | 2012-03-23 13:51:56 -0700 | [diff] [blame] | 197 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 198 | /** |
Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 199 | * Get the {@link android.renderscript.Element} of the {@link |
| 200 | * android.renderscript.Type} of the Allocation. |
Jason Sams | 03d2d00 | 2012-03-23 13:51:56 -0700 | [diff] [blame] | 201 | * |
Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 202 | * @return Element |
Jason Sams | 03d2d00 | 2012-03-23 13:51:56 -0700 | [diff] [blame] | 203 | * |
| 204 | */ |
| 205 | public Element getElement() { |
| 206 | return mType.getElement(); |
| 207 | } |
| 208 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 209 | /** |
Jason Sams | 03d2d00 | 2012-03-23 13:51:56 -0700 | [diff] [blame] | 210 | * Get the usage flags of the Allocation. |
| 211 | * |
Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 212 | * @return usage this Allocation's set of the USAGE_* flags OR'd together |
Jason Sams | 03d2d00 | 2012-03-23 13:51:56 -0700 | [diff] [blame] | 213 | * |
| 214 | */ |
| 215 | public int getUsage() { |
| 216 | return mUsage; |
| 217 | } |
| 218 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 219 | /** |
Jason Sams | 36c0f64 | 2012-03-23 15:48:37 -0700 | [diff] [blame] | 220 | * Get the size of the Allocation in bytes. |
| 221 | * |
Alex Sakhartchouk | 918e840 | 2012-04-11 14:04:23 -0700 | [diff] [blame] | 222 | * @return size of the Allocation in bytes. |
Jason Sams | 36c0f64 | 2012-03-23 15:48:37 -0700 | [diff] [blame] | 223 | * |
| 224 | */ |
Alex Sakhartchouk | 918e840 | 2012-04-11 14:04:23 -0700 | [diff] [blame] | 225 | public int getBytesSize() { |
| 226 | return mType.getCount() * mType.getElement().getBytesSize(); |
Jason Sams | 36c0f64 | 2012-03-23 15:48:37 -0700 | [diff] [blame] | 227 | } |
| 228 | |
Jason Sams | 452a766 | 2011-07-07 16:05:18 -0700 | [diff] [blame] | 229 | private void updateCacheInfo(Type t) { |
| 230 | mCurrentDimX = t.getX(); |
| 231 | mCurrentDimY = t.getY(); |
| 232 | mCurrentDimZ = t.getZ(); |
| 233 | mCurrentCount = mCurrentDimX; |
| 234 | if (mCurrentDimY > 1) { |
| 235 | mCurrentCount *= mCurrentDimY; |
| 236 | } |
| 237 | if (mCurrentDimZ > 1) { |
| 238 | mCurrentCount *= mCurrentDimZ; |
| 239 | } |
| 240 | } |
Jason Sams | ba862d1 | 2011-07-07 15:24:42 -0700 | [diff] [blame] | 241 | |
Tim Murray | a314551 | 2012-12-04 17:59:29 -0800 | [diff] [blame] | 242 | private void setBitmap(Bitmap b) { |
| 243 | mBitmap = b; |
| 244 | } |
| 245 | |
Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 246 | Allocation(int id, RenderScript rs, Type t, int usage) { |
Alex Sakhartchouk | 0de9444 | 2010-08-11 14:41:28 -0700 | [diff] [blame] | 247 | super(id, rs); |
Jason Sams | 49a05d7 | 2010-12-29 14:31:29 -0800 | [diff] [blame] | 248 | if ((usage & ~(USAGE_SCRIPT | |
| 249 | USAGE_GRAPHICS_TEXTURE | |
| 250 | USAGE_GRAPHICS_VERTEX | |
Alex Sakhartchouk | 8e90f2b | 2011-04-01 14:19:01 -0700 | [diff] [blame] | 251 | USAGE_GRAPHICS_CONSTANTS | |
Jason Sams | 615e7ce | 2012-01-13 14:01:20 -0800 | [diff] [blame] | 252 | USAGE_GRAPHICS_RENDER_TARGET | |
Jason Sams | 615e7ce | 2012-01-13 14:01:20 -0800 | [diff] [blame] | 253 | USAGE_IO_INPUT | |
Tim Murray | 00bb454 | 2012-12-17 16:35:06 -0800 | [diff] [blame] | 254 | USAGE_IO_OUTPUT | |
| 255 | USAGE_SHARED)) != 0) { |
Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 256 | throw new RSIllegalArgumentException("Unknown usage specified."); |
| 257 | } |
Jason Sams | 615e7ce | 2012-01-13 14:01:20 -0800 | [diff] [blame] | 258 | |
Jason Sams | fe1d5ff | 2012-03-23 11:47:26 -0700 | [diff] [blame] | 259 | if ((usage & USAGE_IO_INPUT) != 0) { |
Jason Sams | 615e7ce | 2012-01-13 14:01:20 -0800 | [diff] [blame] | 260 | mWriteAllowed = false; |
| 261 | |
Jason Sams | fe1d5ff | 2012-03-23 11:47:26 -0700 | [diff] [blame] | 262 | if ((usage & ~(USAGE_IO_INPUT | |
Jason Sams | 615e7ce | 2012-01-13 14:01:20 -0800 | [diff] [blame] | 263 | USAGE_GRAPHICS_TEXTURE | |
| 264 | USAGE_SCRIPT)) != 0) { |
| 265 | throw new RSIllegalArgumentException("Invalid usage combination."); |
| 266 | } |
| 267 | } |
Jason Sams | 9bf1892 | 2013-04-13 19:48:36 -0700 | [diff] [blame] | 268 | |
Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 269 | mType = t; |
Jason Sams | 615e7ce | 2012-01-13 14:01:20 -0800 | [diff] [blame] | 270 | mUsage = usage; |
Jason Sams | ba862d1 | 2011-07-07 15:24:42 -0700 | [diff] [blame] | 271 | |
Jason Sams | 452a766 | 2011-07-07 16:05:18 -0700 | [diff] [blame] | 272 | if (t != null) { |
| 273 | updateCacheInfo(t); |
Jason Sams | ba862d1 | 2011-07-07 15:24:42 -0700 | [diff] [blame] | 274 | } |
Alex Sakhartchouk | 80a4c2c | 2010-07-12 15:50:32 -0700 | [diff] [blame] | 275 | } |
| 276 | |
Jason Sams | b97b251 | 2011-01-16 15:04:08 -0800 | [diff] [blame] | 277 | private void validateIsInt32() { |
| 278 | if ((mType.mElement.mType == Element.DataType.SIGNED_32) || |
| 279 | (mType.mElement.mType == Element.DataType.UNSIGNED_32)) { |
| 280 | return; |
| 281 | } |
| 282 | throw new RSIllegalArgumentException( |
| 283 | "32 bit integer source does not match allocation type " + mType.mElement.mType); |
| 284 | } |
| 285 | |
| 286 | private void validateIsInt16() { |
| 287 | if ((mType.mElement.mType == Element.DataType.SIGNED_16) || |
| 288 | (mType.mElement.mType == Element.DataType.UNSIGNED_16)) { |
| 289 | return; |
| 290 | } |
| 291 | throw new RSIllegalArgumentException( |
| 292 | "16 bit integer source does not match allocation type " + mType.mElement.mType); |
| 293 | } |
| 294 | |
| 295 | private void validateIsInt8() { |
| 296 | if ((mType.mElement.mType == Element.DataType.SIGNED_8) || |
| 297 | (mType.mElement.mType == Element.DataType.UNSIGNED_8)) { |
| 298 | return; |
| 299 | } |
| 300 | throw new RSIllegalArgumentException( |
| 301 | "8 bit integer source does not match allocation type " + mType.mElement.mType); |
| 302 | } |
| 303 | |
| 304 | private void validateIsFloat32() { |
| 305 | if (mType.mElement.mType == Element.DataType.FLOAT_32) { |
| 306 | return; |
| 307 | } |
| 308 | throw new RSIllegalArgumentException( |
| 309 | "32 bit float source does not match allocation type " + mType.mElement.mType); |
| 310 | } |
| 311 | |
| 312 | private void validateIsObject() { |
| 313 | if ((mType.mElement.mType == Element.DataType.RS_ELEMENT) || |
| 314 | (mType.mElement.mType == Element.DataType.RS_TYPE) || |
| 315 | (mType.mElement.mType == Element.DataType.RS_ALLOCATION) || |
| 316 | (mType.mElement.mType == Element.DataType.RS_SAMPLER) || |
| 317 | (mType.mElement.mType == Element.DataType.RS_SCRIPT) || |
| 318 | (mType.mElement.mType == Element.DataType.RS_MESH) || |
| 319 | (mType.mElement.mType == Element.DataType.RS_PROGRAM_FRAGMENT) || |
| 320 | (mType.mElement.mType == Element.DataType.RS_PROGRAM_VERTEX) || |
| 321 | (mType.mElement.mType == Element.DataType.RS_PROGRAM_RASTER) || |
| 322 | (mType.mElement.mType == Element.DataType.RS_PROGRAM_STORE)) { |
| 323 | return; |
| 324 | } |
| 325 | throw new RSIllegalArgumentException( |
| 326 | "Object source does not match allocation type " + mType.mElement.mType); |
| 327 | } |
| 328 | |
Alex Sakhartchouk | dfac814 | 2010-07-15 11:33:03 -0700 | [diff] [blame] | 329 | @Override |
| 330 | void updateFromNative() { |
Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 331 | super.updateFromNative(); |
Jason Sams | e07694b | 2012-04-03 15:36:36 -0700 | [diff] [blame] | 332 | int typeID = mRS.nAllocationGetType(getID(mRS)); |
Alex Sakhartchouk | dfac814 | 2010-07-15 11:33:03 -0700 | [diff] [blame] | 333 | if(typeID != 0) { |
| 334 | mType = new Type(typeID, mRS); |
| 335 | mType.updateFromNative(); |
Jason Sams | ad37cb2 | 2011-07-07 16:17:36 -0700 | [diff] [blame] | 336 | updateCacheInfo(mType); |
Alex Sakhartchouk | dfac814 | 2010-07-15 11:33:03 -0700 | [diff] [blame] | 337 | } |
| 338 | } |
| 339 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 340 | /** |
Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 341 | * Get the {@link android.renderscript.Type} of the Allocation. |
Jason Sams | 03d2d00 | 2012-03-23 13:51:56 -0700 | [diff] [blame] | 342 | * |
| 343 | * @return Type |
| 344 | * |
| 345 | */ |
Jason Sams | ea87e96 | 2010-01-12 12:12:28 -0800 | [diff] [blame] | 346 | public Type getType() { |
| 347 | return mType; |
| 348 | } |
| 349 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 350 | /** |
Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 351 | * Propagate changes from one usage of the Allocation to the |
| 352 | * other usages of the Allocation. |
Jason Sams | 03d2d00 | 2012-03-23 13:51:56 -0700 | [diff] [blame] | 353 | * |
| 354 | */ |
Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 355 | public void syncAll(int srcLocation) { |
Tim Murray | 6d7a53c | 2013-05-23 16:59:23 -0700 | [diff] [blame] | 356 | Trace.traceBegin(RenderScript.TRACE_TAG, "syncAll"); |
Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 357 | switch (srcLocation) { |
Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 358 | case USAGE_GRAPHICS_TEXTURE: |
Tim Murray | 78e6494 | 2013-04-09 17:28:56 -0700 | [diff] [blame] | 359 | case USAGE_SCRIPT: |
| 360 | if ((mUsage & USAGE_SHARED) != 0) { |
| 361 | copyFrom(mBitmap); |
| 362 | } |
| 363 | break; |
| 364 | case USAGE_GRAPHICS_CONSTANTS: |
Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 365 | case USAGE_GRAPHICS_VERTEX: |
| 366 | break; |
Tim Murray | 78e6494 | 2013-04-09 17:28:56 -0700 | [diff] [blame] | 367 | case USAGE_SHARED: |
| 368 | if ((mUsage & USAGE_SHARED) != 0) { |
| 369 | copyTo(mBitmap); |
| 370 | } |
| 371 | break; |
Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 372 | default: |
| 373 | throw new RSIllegalArgumentException("Source must be exactly one usage type."); |
| 374 | } |
| 375 | mRS.validate(); |
Jason Sams | 48fe534 | 2011-07-08 13:52:30 -0700 | [diff] [blame] | 376 | mRS.nAllocationSyncAll(getIDSafe(), srcLocation); |
Tim Murray | 6d7a53c | 2013-05-23 16:59:23 -0700 | [diff] [blame] | 377 | Trace.traceEnd(RenderScript.TRACE_TAG); |
Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 378 | } |
| 379 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 380 | /** |
Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 381 | * Send a buffer to the output stream. The contents of the Allocation will |
| 382 | * be undefined after this operation. This operation is only valid if {@link |
| 383 | * #USAGE_IO_OUTPUT} is set on the Allocation. |
| 384 | * |
Jason Sams | 163766c | 2012-02-15 12:04:24 -0800 | [diff] [blame] | 385 | * |
Jason Sams | 163766c | 2012-02-15 12:04:24 -0800 | [diff] [blame] | 386 | */ |
Jason Sams | c5f519c | 2012-03-29 17:58:15 -0700 | [diff] [blame] | 387 | public void ioSend() { |
Tim Murray | 6d7a53c | 2013-05-23 16:59:23 -0700 | [diff] [blame] | 388 | Trace.traceBegin(RenderScript.TRACE_TAG, "ioSend"); |
Jason Sams | 163766c | 2012-02-15 12:04:24 -0800 | [diff] [blame] | 389 | if ((mUsage & USAGE_IO_OUTPUT) == 0) { |
| 390 | throw new RSIllegalArgumentException( |
| 391 | "Can only send buffer if IO_OUTPUT usage specified."); |
| 392 | } |
| 393 | mRS.validate(); |
Jason Sams | e07694b | 2012-04-03 15:36:36 -0700 | [diff] [blame] | 394 | mRS.nAllocationIoSend(getID(mRS)); |
Tim Murray | 6d7a53c | 2013-05-23 16:59:23 -0700 | [diff] [blame] | 395 | Trace.traceEnd(RenderScript.TRACE_TAG); |
Jason Sams | 163766c | 2012-02-15 12:04:24 -0800 | [diff] [blame] | 396 | } |
| 397 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 398 | /** |
Jason Sams | c5f519c | 2012-03-29 17:58:15 -0700 | [diff] [blame] | 399 | * Delete once code is updated. |
| 400 | * @hide |
| 401 | */ |
| 402 | public void ioSendOutput() { |
| 403 | ioSend(); |
| 404 | } |
| 405 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 406 | /** |
Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 407 | * Receive the latest input into the Allocation. This operation |
| 408 | * is only valid if {@link #USAGE_IO_INPUT} is set on the Allocation. |
Jason Sams | 163766c | 2012-02-15 12:04:24 -0800 | [diff] [blame] | 409 | * |
Jason Sams | 163766c | 2012-02-15 12:04:24 -0800 | [diff] [blame] | 410 | */ |
Jason Sams | c5f519c | 2012-03-29 17:58:15 -0700 | [diff] [blame] | 411 | public void ioReceive() { |
Tim Murray | 6d7a53c | 2013-05-23 16:59:23 -0700 | [diff] [blame] | 412 | Trace.traceBegin(RenderScript.TRACE_TAG, "ioReceive"); |
Jason Sams | 163766c | 2012-02-15 12:04:24 -0800 | [diff] [blame] | 413 | if ((mUsage & USAGE_IO_INPUT) == 0) { |
| 414 | throw new RSIllegalArgumentException( |
Jason Sams | fe1d5ff | 2012-03-23 11:47:26 -0700 | [diff] [blame] | 415 | "Can only receive if IO_INPUT usage specified."); |
Jason Sams | 163766c | 2012-02-15 12:04:24 -0800 | [diff] [blame] | 416 | } |
| 417 | mRS.validate(); |
Jason Sams | e07694b | 2012-04-03 15:36:36 -0700 | [diff] [blame] | 418 | mRS.nAllocationIoReceive(getID(mRS)); |
Tim Murray | 6d7a53c | 2013-05-23 16:59:23 -0700 | [diff] [blame] | 419 | Trace.traceEnd(RenderScript.TRACE_TAG); |
Jason Sams | 163766c | 2012-02-15 12:04:24 -0800 | [diff] [blame] | 420 | } |
| 421 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 422 | /** |
Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 423 | * Copy an array of RS objects to the Allocation. |
Jason Sams | 03d2d00 | 2012-03-23 13:51:56 -0700 | [diff] [blame] | 424 | * |
| 425 | * @param d Source array. |
| 426 | */ |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 427 | public void copyFrom(BaseObj[] d) { |
Tim Murray | 6d7a53c | 2013-05-23 16:59:23 -0700 | [diff] [blame] | 428 | Trace.traceBegin(RenderScript.TRACE_TAG, "copyFrom"); |
Jason Sams | 771bebb | 2009-12-07 12:40:12 -0800 | [diff] [blame] | 429 | mRS.validate(); |
Jason Sams | b97b251 | 2011-01-16 15:04:08 -0800 | [diff] [blame] | 430 | validateIsObject(); |
Jason Sams | ba862d1 | 2011-07-07 15:24:42 -0700 | [diff] [blame] | 431 | if (d.length != mCurrentCount) { |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 432 | throw new RSIllegalArgumentException("Array size mismatch, allocation sizeX = " + |
Jason Sams | ba862d1 | 2011-07-07 15:24:42 -0700 | [diff] [blame] | 433 | mCurrentCount + ", array length = " + d.length); |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 434 | } |
| 435 | int i[] = new int[d.length]; |
| 436 | for (int ct=0; ct < d.length; ct++) { |
Jason Sams | e07694b | 2012-04-03 15:36:36 -0700 | [diff] [blame] | 437 | i[ct] = d[ct].getID(mRS); |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 438 | } |
Jason Sams | ba862d1 | 2011-07-07 15:24:42 -0700 | [diff] [blame] | 439 | copy1DRangeFromUnchecked(0, mCurrentCount, i); |
Tim Murray | 6d7a53c | 2013-05-23 16:59:23 -0700 | [diff] [blame] | 440 | Trace.traceEnd(RenderScript.TRACE_TAG); |
Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 441 | } |
| 442 | |
Jason Sams | fb9f82c | 2011-01-12 14:53:25 -0800 | [diff] [blame] | 443 | private void validateBitmapFormat(Bitmap b) { |
Jason Sams | 252c078 | 2011-01-11 17:42:52 -0800 | [diff] [blame] | 444 | Bitmap.Config bc = b.getConfig(); |
Tim Murray | abd5db9 | 2013-02-28 11:45:22 -0800 | [diff] [blame] | 445 | if (bc == null) { |
| 446 | throw new RSIllegalArgumentException("Bitmap has an unsupported format for this operation"); |
| 447 | } |
Jason Sams | 252c078 | 2011-01-11 17:42:52 -0800 | [diff] [blame] | 448 | switch (bc) { |
| 449 | case ALPHA_8: |
| 450 | if (mType.getElement().mKind != Element.DataKind.PIXEL_A) { |
| 451 | throw new RSIllegalArgumentException("Allocation kind is " + |
| 452 | mType.getElement().mKind + ", type " + |
| 453 | mType.getElement().mType + |
Alex Sakhartchouk | 918e840 | 2012-04-11 14:04:23 -0700 | [diff] [blame] | 454 | " of " + mType.getElement().getBytesSize() + |
Jason Sams | 252c078 | 2011-01-11 17:42:52 -0800 | [diff] [blame] | 455 | " bytes, passed bitmap was " + bc); |
| 456 | } |
| 457 | break; |
| 458 | case ARGB_8888: |
| 459 | if ((mType.getElement().mKind != Element.DataKind.PIXEL_RGBA) || |
Alex Sakhartchouk | 918e840 | 2012-04-11 14:04:23 -0700 | [diff] [blame] | 460 | (mType.getElement().getBytesSize() != 4)) { |
Jason Sams | 252c078 | 2011-01-11 17:42:52 -0800 | [diff] [blame] | 461 | throw new RSIllegalArgumentException("Allocation kind is " + |
| 462 | mType.getElement().mKind + ", type " + |
| 463 | mType.getElement().mType + |
Alex Sakhartchouk | 918e840 | 2012-04-11 14:04:23 -0700 | [diff] [blame] | 464 | " of " + mType.getElement().getBytesSize() + |
Jason Sams | 252c078 | 2011-01-11 17:42:52 -0800 | [diff] [blame] | 465 | " bytes, passed bitmap was " + bc); |
| 466 | } |
| 467 | break; |
| 468 | case RGB_565: |
| 469 | if ((mType.getElement().mKind != Element.DataKind.PIXEL_RGB) || |
Alex Sakhartchouk | 918e840 | 2012-04-11 14:04:23 -0700 | [diff] [blame] | 470 | (mType.getElement().getBytesSize() != 2)) { |
Jason Sams | 252c078 | 2011-01-11 17:42:52 -0800 | [diff] [blame] | 471 | throw new RSIllegalArgumentException("Allocation kind is " + |
| 472 | mType.getElement().mKind + ", type " + |
| 473 | mType.getElement().mType + |
Alex Sakhartchouk | 918e840 | 2012-04-11 14:04:23 -0700 | [diff] [blame] | 474 | " of " + mType.getElement().getBytesSize() + |
Jason Sams | 252c078 | 2011-01-11 17:42:52 -0800 | [diff] [blame] | 475 | " bytes, passed bitmap was " + bc); |
| 476 | } |
| 477 | break; |
| 478 | case ARGB_4444: |
| 479 | if ((mType.getElement().mKind != Element.DataKind.PIXEL_RGBA) || |
Alex Sakhartchouk | 918e840 | 2012-04-11 14:04:23 -0700 | [diff] [blame] | 480 | (mType.getElement().getBytesSize() != 2)) { |
Jason Sams | 252c078 | 2011-01-11 17:42:52 -0800 | [diff] [blame] | 481 | throw new RSIllegalArgumentException("Allocation kind is " + |
| 482 | mType.getElement().mKind + ", type " + |
| 483 | mType.getElement().mType + |
Alex Sakhartchouk | 918e840 | 2012-04-11 14:04:23 -0700 | [diff] [blame] | 484 | " of " + mType.getElement().getBytesSize() + |
Jason Sams | 252c078 | 2011-01-11 17:42:52 -0800 | [diff] [blame] | 485 | " bytes, passed bitmap was " + bc); |
| 486 | } |
| 487 | break; |
| 488 | |
| 489 | } |
Jason Sams | 4ef6650 | 2010-12-10 16:03:15 -0800 | [diff] [blame] | 490 | } |
| 491 | |
Jason Sams | fb9f82c | 2011-01-12 14:53:25 -0800 | [diff] [blame] | 492 | private void validateBitmapSize(Bitmap b) { |
Jason Sams | ba862d1 | 2011-07-07 15:24:42 -0700 | [diff] [blame] | 493 | if((mCurrentDimX != b.getWidth()) || (mCurrentDimY != b.getHeight())) { |
Jason Sams | fb9f82c | 2011-01-12 14:53:25 -0800 | [diff] [blame] | 494 | throw new RSIllegalArgumentException("Cannot update allocation from bitmap, sizes mismatch"); |
| 495 | } |
| 496 | } |
| 497 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 498 | /** |
Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 499 | * Copy into this Allocation from an array. This method does not guarantee |
| 500 | * that the Allocation is compatible with the input buffer; it copies memory |
| 501 | * without reinterpretation. |
Jason Sams | 4fa3eed | 2011-01-19 15:44:38 -0800 | [diff] [blame] | 502 | * |
| 503 | * @param d the source data array |
| 504 | */ |
| 505 | public void copyFromUnchecked(int[] d) { |
Tim Murray | 6d7a53c | 2013-05-23 16:59:23 -0700 | [diff] [blame] | 506 | Trace.traceBegin(RenderScript.TRACE_TAG, "copyFromUnchecked"); |
Jason Sams | 4fa3eed | 2011-01-19 15:44:38 -0800 | [diff] [blame] | 507 | mRS.validate(); |
Jason Sams | b05d689 | 2013-04-09 15:59:24 -0700 | [diff] [blame] | 508 | if (mCurrentDimZ > 0) { |
| 509 | copy3DRangeFromUnchecked(0, 0, 0, mCurrentDimX, mCurrentDimY, mCurrentDimZ, d); |
| 510 | } else if (mCurrentDimY > 0) { |
Stephen Hines | a9a7b37 | 2013-02-08 17:11:31 -0800 | [diff] [blame] | 511 | copy2DRangeFromUnchecked(0, 0, mCurrentDimX, mCurrentDimY, d); |
| 512 | } else { |
| 513 | copy1DRangeFromUnchecked(0, mCurrentCount, d); |
| 514 | } |
Tim Murray | 6d7a53c | 2013-05-23 16:59:23 -0700 | [diff] [blame] | 515 | Trace.traceEnd(RenderScript.TRACE_TAG); |
Jason Sams | 4fa3eed | 2011-01-19 15:44:38 -0800 | [diff] [blame] | 516 | } |
Tim Murray | 6d7a53c | 2013-05-23 16:59:23 -0700 | [diff] [blame] | 517 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 518 | /** |
Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 519 | * Copy into this Allocation from an array. This method does not guarantee |
| 520 | * that the Allocation is compatible with the input buffer; it copies memory |
| 521 | * without reinterpretation. |
Jason Sams | 4fa3eed | 2011-01-19 15:44:38 -0800 | [diff] [blame] | 522 | * |
| 523 | * @param d the source data array |
| 524 | */ |
| 525 | public void copyFromUnchecked(short[] d) { |
Tim Murray | 6d7a53c | 2013-05-23 16:59:23 -0700 | [diff] [blame] | 526 | Trace.traceBegin(RenderScript.TRACE_TAG, "copyFromUnchecked"); |
Jason Sams | 4fa3eed | 2011-01-19 15:44:38 -0800 | [diff] [blame] | 527 | mRS.validate(); |
Jason Sams | b05d689 | 2013-04-09 15:59:24 -0700 | [diff] [blame] | 528 | if (mCurrentDimZ > 0) { |
| 529 | copy3DRangeFromUnchecked(0, 0, 0, mCurrentDimX, mCurrentDimY, mCurrentDimZ, d); |
| 530 | } else if (mCurrentDimY > 0) { |
Stephen Hines | a9a7b37 | 2013-02-08 17:11:31 -0800 | [diff] [blame] | 531 | copy2DRangeFromUnchecked(0, 0, mCurrentDimX, mCurrentDimY, d); |
| 532 | } else { |
| 533 | copy1DRangeFromUnchecked(0, mCurrentCount, d); |
| 534 | } |
Tim Murray | 6d7a53c | 2013-05-23 16:59:23 -0700 | [diff] [blame] | 535 | Trace.traceEnd(RenderScript.TRACE_TAG); |
Jason Sams | 4fa3eed | 2011-01-19 15:44:38 -0800 | [diff] [blame] | 536 | } |
Tim Murray | 6d7a53c | 2013-05-23 16:59:23 -0700 | [diff] [blame] | 537 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 538 | /** |
Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 539 | * Copy into this Allocation from an array. This method does not guarantee |
| 540 | * that the Allocation is compatible with the input buffer; it copies memory |
| 541 | * without reinterpretation. |
Jason Sams | 4fa3eed | 2011-01-19 15:44:38 -0800 | [diff] [blame] | 542 | * |
| 543 | * @param d the source data array |
| 544 | */ |
| 545 | public void copyFromUnchecked(byte[] d) { |
Tim Murray | 6d7a53c | 2013-05-23 16:59:23 -0700 | [diff] [blame] | 546 | Trace.traceBegin(RenderScript.TRACE_TAG, "copyFromUnchecked"); |
Jason Sams | 4fa3eed | 2011-01-19 15:44:38 -0800 | [diff] [blame] | 547 | mRS.validate(); |
Jason Sams | b05d689 | 2013-04-09 15:59:24 -0700 | [diff] [blame] | 548 | if (mCurrentDimZ > 0) { |
| 549 | copy3DRangeFromUnchecked(0, 0, 0, mCurrentDimX, mCurrentDimY, mCurrentDimZ, d); |
| 550 | } else if (mCurrentDimY > 0) { |
Stephen Hines | a9a7b37 | 2013-02-08 17:11:31 -0800 | [diff] [blame] | 551 | copy2DRangeFromUnchecked(0, 0, mCurrentDimX, mCurrentDimY, d); |
| 552 | } else { |
| 553 | copy1DRangeFromUnchecked(0, mCurrentCount, d); |
| 554 | } |
Tim Murray | 6d7a53c | 2013-05-23 16:59:23 -0700 | [diff] [blame] | 555 | Trace.traceEnd(RenderScript.TRACE_TAG); |
Jason Sams | 4fa3eed | 2011-01-19 15:44:38 -0800 | [diff] [blame] | 556 | } |
Tim Murray | 6d7a53c | 2013-05-23 16:59:23 -0700 | [diff] [blame] | 557 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 558 | /** |
Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 559 | * Copy into this Allocation from an array. This method does not guarantee |
| 560 | * that the Allocation is compatible with the input buffer; it copies memory |
| 561 | * without reinterpretation. |
Jason Sams | 4fa3eed | 2011-01-19 15:44:38 -0800 | [diff] [blame] | 562 | * |
| 563 | * @param d the source data array |
| 564 | */ |
| 565 | public void copyFromUnchecked(float[] d) { |
Tim Murray | 6d7a53c | 2013-05-23 16:59:23 -0700 | [diff] [blame] | 566 | Trace.traceBegin(RenderScript.TRACE_TAG, "copyFromUnchecked"); |
Jason Sams | 4fa3eed | 2011-01-19 15:44:38 -0800 | [diff] [blame] | 567 | mRS.validate(); |
Jason Sams | b05d689 | 2013-04-09 15:59:24 -0700 | [diff] [blame] | 568 | if (mCurrentDimZ > 0) { |
| 569 | copy3DRangeFromUnchecked(0, 0, 0, mCurrentDimX, mCurrentDimY, mCurrentDimZ, d); |
| 570 | } else if (mCurrentDimY > 0) { |
Stephen Hines | a9a7b37 | 2013-02-08 17:11:31 -0800 | [diff] [blame] | 571 | copy2DRangeFromUnchecked(0, 0, mCurrentDimX, mCurrentDimY, d); |
| 572 | } else { |
| 573 | copy1DRangeFromUnchecked(0, mCurrentCount, d); |
| 574 | } |
Tim Murray | 6d7a53c | 2013-05-23 16:59:23 -0700 | [diff] [blame] | 575 | Trace.traceEnd(RenderScript.TRACE_TAG); |
Jason Sams | 4fa3eed | 2011-01-19 15:44:38 -0800 | [diff] [blame] | 576 | } |
| 577 | |
Tim Murray | 6d7a53c | 2013-05-23 16:59:23 -0700 | [diff] [blame] | 578 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 579 | /** |
Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 580 | * Copy into this Allocation from an array. This variant is type checked |
| 581 | * and will generate exceptions if the Allocation's {@link |
| 582 | * android.renderscript.Element} is not a 32 bit integer type. |
Jason Sams | 4fa3eed | 2011-01-19 15:44:38 -0800 | [diff] [blame] | 583 | * |
| 584 | * @param d the source data array |
| 585 | */ |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 586 | public void copyFrom(int[] d) { |
Tim Murray | 6d7a53c | 2013-05-23 16:59:23 -0700 | [diff] [blame] | 587 | Trace.traceBegin(RenderScript.TRACE_TAG, "copyFrom"); |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 588 | mRS.validate(); |
Jason Sams | b05d689 | 2013-04-09 15:59:24 -0700 | [diff] [blame] | 589 | if (mCurrentDimZ > 0) { |
| 590 | copy3DRangeFrom(0, 0, 0, mCurrentDimX, mCurrentDimY, mCurrentDimZ, d); |
| 591 | } else if (mCurrentDimY > 0) { |
Stephen Hines | a9a7b37 | 2013-02-08 17:11:31 -0800 | [diff] [blame] | 592 | copy2DRangeFrom(0, 0, mCurrentDimX, mCurrentDimY, d); |
| 593 | } else { |
| 594 | copy1DRangeFrom(0, mCurrentCount, d); |
| 595 | } |
Tim Murray | 6d7a53c | 2013-05-23 16:59:23 -0700 | [diff] [blame] | 596 | Trace.traceEnd(RenderScript.TRACE_TAG); |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 597 | } |
Jason Sams | 4fa3eed | 2011-01-19 15:44:38 -0800 | [diff] [blame] | 598 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 599 | /** |
Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 600 | * Copy into this Allocation from an array. This variant is type checked |
| 601 | * and will generate exceptions if the Allocation's {@link |
| 602 | * android.renderscript.Element} is not a 16 bit integer type. |
Jason Sams | 4fa3eed | 2011-01-19 15:44:38 -0800 | [diff] [blame] | 603 | * |
| 604 | * @param d the source data array |
| 605 | */ |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 606 | public void copyFrom(short[] d) { |
Tim Murray | 6d7a53c | 2013-05-23 16:59:23 -0700 | [diff] [blame] | 607 | Trace.traceBegin(RenderScript.TRACE_TAG, "copyFrom"); |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 608 | mRS.validate(); |
Jason Sams | b05d689 | 2013-04-09 15:59:24 -0700 | [diff] [blame] | 609 | if (mCurrentDimZ > 0) { |
| 610 | copy3DRangeFrom(0, 0, 0, mCurrentDimX, mCurrentDimY, mCurrentDimZ, d); |
| 611 | } else if (mCurrentDimY > 0) { |
Stephen Hines | a9a7b37 | 2013-02-08 17:11:31 -0800 | [diff] [blame] | 612 | copy2DRangeFrom(0, 0, mCurrentDimX, mCurrentDimY, d); |
| 613 | } else { |
| 614 | copy1DRangeFrom(0, mCurrentCount, d); |
| 615 | } |
Tim Murray | 6d7a53c | 2013-05-23 16:59:23 -0700 | [diff] [blame] | 616 | Trace.traceEnd(RenderScript.TRACE_TAG); |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 617 | } |
Jason Sams | 4fa3eed | 2011-01-19 15:44:38 -0800 | [diff] [blame] | 618 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 619 | /** |
Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 620 | * Copy into this Allocation from an array. This variant is type checked |
| 621 | * and will generate exceptions if the Allocation's {@link |
| 622 | * android.renderscript.Element} is not an 8 bit integer type. |
Jason Sams | 4fa3eed | 2011-01-19 15:44:38 -0800 | [diff] [blame] | 623 | * |
| 624 | * @param d the source data array |
| 625 | */ |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 626 | public void copyFrom(byte[] d) { |
Tim Murray | 6d7a53c | 2013-05-23 16:59:23 -0700 | [diff] [blame] | 627 | Trace.traceBegin(RenderScript.TRACE_TAG, "copyFrom"); |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 628 | mRS.validate(); |
Jason Sams | b05d689 | 2013-04-09 15:59:24 -0700 | [diff] [blame] | 629 | if (mCurrentDimZ > 0) { |
| 630 | copy3DRangeFrom(0, 0, 0, mCurrentDimX, mCurrentDimY, mCurrentDimZ, d); |
| 631 | } else if (mCurrentDimY > 0) { |
Stephen Hines | a9a7b37 | 2013-02-08 17:11:31 -0800 | [diff] [blame] | 632 | copy2DRangeFrom(0, 0, mCurrentDimX, mCurrentDimY, d); |
| 633 | } else { |
| 634 | copy1DRangeFrom(0, mCurrentCount, d); |
| 635 | } |
Tim Murray | 6d7a53c | 2013-05-23 16:59:23 -0700 | [diff] [blame] | 636 | Trace.traceEnd(RenderScript.TRACE_TAG); |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 637 | } |
Jason Sams | 4fa3eed | 2011-01-19 15:44:38 -0800 | [diff] [blame] | 638 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 639 | /** |
Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 640 | * Copy into this Allocation from an array. This variant is type checked |
| 641 | * and will generate exceptions if the Allocation's {@link |
| 642 | * android.renderscript.Element} is not a 32 bit float type. |
Jason Sams | 4fa3eed | 2011-01-19 15:44:38 -0800 | [diff] [blame] | 643 | * |
| 644 | * @param d the source data array |
| 645 | */ |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 646 | public void copyFrom(float[] d) { |
Tim Murray | 6d7a53c | 2013-05-23 16:59:23 -0700 | [diff] [blame] | 647 | Trace.traceBegin(RenderScript.TRACE_TAG, "copyFrom"); |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 648 | mRS.validate(); |
Jason Sams | b05d689 | 2013-04-09 15:59:24 -0700 | [diff] [blame] | 649 | if (mCurrentDimZ > 0) { |
| 650 | copy3DRangeFrom(0, 0, 0, mCurrentDimX, mCurrentDimY, mCurrentDimZ, d); |
| 651 | } else if (mCurrentDimY > 0) { |
Stephen Hines | a9a7b37 | 2013-02-08 17:11:31 -0800 | [diff] [blame] | 652 | copy2DRangeFrom(0, 0, mCurrentDimX, mCurrentDimY, d); |
| 653 | } else { |
| 654 | copy1DRangeFrom(0, mCurrentCount, d); |
| 655 | } |
Tim Murray | 6d7a53c | 2013-05-23 16:59:23 -0700 | [diff] [blame] | 656 | Trace.traceEnd(RenderScript.TRACE_TAG); |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 657 | } |
Jason Sams | 4fa3eed | 2011-01-19 15:44:38 -0800 | [diff] [blame] | 658 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 659 | /** |
Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 660 | * Copy into an Allocation from a {@link android.graphics.Bitmap}. The |
| 661 | * height, width, and format of the bitmap must match the existing |
| 662 | * allocation. |
| 663 | * |
| 664 | * <p>If the {@link android.graphics.Bitmap} is the same as the {@link |
| 665 | * android.graphics.Bitmap} used to create the Allocation with {@link |
| 666 | * #createFromBitmap} and {@link #USAGE_SHARED} is set on the Allocation, |
| 667 | * this will synchronize the Allocation with the latest data from the {@link |
| 668 | * android.graphics.Bitmap}, potentially avoiding the actual copy.</p> |
Jason Sams | 4fa3eed | 2011-01-19 15:44:38 -0800 | [diff] [blame] | 669 | * |
| 670 | * @param b the source bitmap |
| 671 | */ |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 672 | public void copyFrom(Bitmap b) { |
Tim Murray | 6d7a53c | 2013-05-23 16:59:23 -0700 | [diff] [blame] | 673 | Trace.traceBegin(RenderScript.TRACE_TAG, "copyFrom"); |
Jason Sams | fb9f82c | 2011-01-12 14:53:25 -0800 | [diff] [blame] | 674 | mRS.validate(); |
Tim Murray | abd5db9 | 2013-02-28 11:45:22 -0800 | [diff] [blame] | 675 | if (b.getConfig() == null) { |
| 676 | Bitmap newBitmap = Bitmap.createBitmap(b.getWidth(), b.getHeight(), Bitmap.Config.ARGB_8888); |
| 677 | Canvas c = new Canvas(newBitmap); |
| 678 | c.drawBitmap(b, 0, 0, null); |
| 679 | copyFrom(newBitmap); |
| 680 | return; |
| 681 | } |
Jason Sams | fb9f82c | 2011-01-12 14:53:25 -0800 | [diff] [blame] | 682 | validateBitmapSize(b); |
| 683 | validateBitmapFormat(b); |
Jason Sams | e07694b | 2012-04-03 15:36:36 -0700 | [diff] [blame] | 684 | mRS.nAllocationCopyFromBitmap(getID(mRS), b); |
Tim Murray | 6d7a53c | 2013-05-23 16:59:23 -0700 | [diff] [blame] | 685 | Trace.traceEnd(RenderScript.TRACE_TAG); |
Alex Sakhartchouk | 26ae390 | 2010-10-11 12:35:15 -0700 | [diff] [blame] | 686 | } |
| 687 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 688 | /** |
Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 689 | * Copy an Allocation from an Allocation. The types of both allocations |
Tim Murray | f671fb0 | 2012-10-03 13:50:05 -0700 | [diff] [blame] | 690 | * must be identical. |
| 691 | * |
| 692 | * @param a the source allocation |
| 693 | */ |
| 694 | public void copyFrom(Allocation a) { |
Tim Murray | 6d7a53c | 2013-05-23 16:59:23 -0700 | [diff] [blame] | 695 | Trace.traceBegin(RenderScript.TRACE_TAG, "copyFrom"); |
Tim Murray | f671fb0 | 2012-10-03 13:50:05 -0700 | [diff] [blame] | 696 | mRS.validate(); |
| 697 | if (!mType.equals(a.getType())) { |
| 698 | throw new RSIllegalArgumentException("Types of allocations must match."); |
| 699 | } |
| 700 | copy2DRangeFrom(0, 0, mCurrentDimX, mCurrentDimY, a, 0, 0); |
Tim Murray | 6d7a53c | 2013-05-23 16:59:23 -0700 | [diff] [blame] | 701 | Trace.traceEnd(RenderScript.TRACE_TAG); |
Tim Murray | f671fb0 | 2012-10-03 13:50:05 -0700 | [diff] [blame] | 702 | } |
| 703 | |
Tim Murray | f671fb0 | 2012-10-03 13:50:05 -0700 | [diff] [blame] | 704 | /** |
Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 705 | * This is only intended to be used by auto-generated code reflected from |
| 706 | * the RenderScript script files and should not be used by developers. |
Jason Sams | fa445b9 | 2011-01-07 17:00:07 -0800 | [diff] [blame] | 707 | * |
| 708 | * @param xoff |
| 709 | * @param fp |
| 710 | */ |
Jason Sams | 21b4103 | 2011-01-16 15:05:41 -0800 | [diff] [blame] | 711 | public void setFromFieldPacker(int xoff, FieldPacker fp) { |
Jason Sams | f70b0fc | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 712 | mRS.validate(); |
Alex Sakhartchouk | 918e840 | 2012-04-11 14:04:23 -0700 | [diff] [blame] | 713 | int eSize = mType.mElement.getBytesSize(); |
Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 714 | final byte[] data = fp.getData(); |
| 715 | |
| 716 | int count = data.length / eSize; |
| 717 | if ((eSize * count) != data.length) { |
Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 718 | throw new RSIllegalArgumentException("Field packer length " + data.length + |
Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 719 | " not divisible by element size " + eSize + "."); |
| 720 | } |
Jason Sams | ba862d1 | 2011-07-07 15:24:42 -0700 | [diff] [blame] | 721 | copy1DRangeFromUnchecked(xoff, count, data); |
Jason Sams | 49bdaf0 | 2010-08-31 13:50:42 -0700 | [diff] [blame] | 722 | } |
| 723 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 724 | /** |
Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 725 | * This is only intended to be used by auto-generated code reflected from |
| 726 | * the RenderScript script files. |
Jason Sams | fa445b9 | 2011-01-07 17:00:07 -0800 | [diff] [blame] | 727 | * |
| 728 | * @param xoff |
| 729 | * @param component_number |
| 730 | * @param fp |
| 731 | */ |
Jason Sams | 21b4103 | 2011-01-16 15:05:41 -0800 | [diff] [blame] | 732 | public void setFromFieldPacker(int xoff, int component_number, FieldPacker fp) { |
Jason Sams | f70b0fc | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 733 | mRS.validate(); |
Jason Sams | 49bdaf0 | 2010-08-31 13:50:42 -0700 | [diff] [blame] | 734 | if (component_number >= mType.mElement.mElements.length) { |
Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 735 | throw new RSIllegalArgumentException("Component_number " + component_number + " out of range."); |
Jason Sams | 49bdaf0 | 2010-08-31 13:50:42 -0700 | [diff] [blame] | 736 | } |
| 737 | if(xoff < 0) { |
Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 738 | throw new RSIllegalArgumentException("Offset must be >= 0."); |
Jason Sams | 49bdaf0 | 2010-08-31 13:50:42 -0700 | [diff] [blame] | 739 | } |
| 740 | |
| 741 | final byte[] data = fp.getData(); |
Alex Sakhartchouk | 918e840 | 2012-04-11 14:04:23 -0700 | [diff] [blame] | 742 | int eSize = mType.mElement.mElements[component_number].getBytesSize(); |
Alex Sakhartchouk | bf3c3f2 | 2012-02-02 09:47:26 -0800 | [diff] [blame] | 743 | eSize *= mType.mElement.mArraySizes[component_number]; |
Jason Sams | 49bdaf0 | 2010-08-31 13:50:42 -0700 | [diff] [blame] | 744 | |
| 745 | if (data.length != eSize) { |
Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 746 | throw new RSIllegalArgumentException("Field packer sizelength " + data.length + |
Jason Sams | 49bdaf0 | 2010-08-31 13:50:42 -0700 | [diff] [blame] | 747 | " does not match component size " + eSize + "."); |
| 748 | } |
| 749 | |
Jason Sams | 48fe534 | 2011-07-08 13:52:30 -0700 | [diff] [blame] | 750 | mRS.nAllocationElementData1D(getIDSafe(), xoff, mSelectedLOD, |
Jason Sams | ba862d1 | 2011-07-07 15:24:42 -0700 | [diff] [blame] | 751 | component_number, data, data.length); |
Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 752 | } |
| 753 | |
Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 754 | private void data1DChecks(int off, int count, int len, int dataSize) { |
Jason Sams | 771bebb | 2009-12-07 12:40:12 -0800 | [diff] [blame] | 755 | mRS.validate(); |
Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 756 | if(off < 0) { |
Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 757 | throw new RSIllegalArgumentException("Offset must be >= 0."); |
Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 758 | } |
| 759 | if(count < 1) { |
Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 760 | throw new RSIllegalArgumentException("Count must be >= 1."); |
Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 761 | } |
Jason Sams | ba862d1 | 2011-07-07 15:24:42 -0700 | [diff] [blame] | 762 | if((off + count) > mCurrentCount) { |
| 763 | throw new RSIllegalArgumentException("Overflow, Available count " + mCurrentCount + |
Jason Sams | a70f416 | 2010-03-26 15:33:42 -0700 | [diff] [blame] | 764 | ", got " + count + " at offset " + off + "."); |
Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 765 | } |
Jason Sams | ba862d1 | 2011-07-07 15:24:42 -0700 | [diff] [blame] | 766 | if(len < dataSize) { |
Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 767 | throw new RSIllegalArgumentException("Array too small for allocation type."); |
Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 768 | } |
Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 769 | } |
| 770 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 771 | /** |
Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 772 | * Generate a mipmap chain. This is only valid if the Type of the Allocation |
| 773 | * includes mipmaps. |
Jason Sams | f708609 | 2011-01-12 13:28:37 -0800 | [diff] [blame] | 774 | * |
Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 775 | * <p>This function will generate a complete set of mipmaps from the top |
| 776 | * level LOD and place them into the script memory space.</p> |
Jason Sams | f708609 | 2011-01-12 13:28:37 -0800 | [diff] [blame] | 777 | * |
Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 778 | * <p>If the Allocation is also using other memory spaces, a call to {@link |
| 779 | * #syncAll syncAll(Allocation.USAGE_SCRIPT)} is required.</p> |
Jason Sams | f708609 | 2011-01-12 13:28:37 -0800 | [diff] [blame] | 780 | */ |
| 781 | public void generateMipmaps() { |
Jason Sams | e07694b | 2012-04-03 15:36:36 -0700 | [diff] [blame] | 782 | mRS.nAllocationGenerateMipmaps(getID(mRS)); |
Jason Sams | f708609 | 2011-01-12 13:28:37 -0800 | [diff] [blame] | 783 | } |
| 784 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 785 | /** |
Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 786 | * Copy an array into part of this Allocation. This method does not |
| 787 | * guarantee that the Allocation is compatible with the input buffer. |
Jason Sams | 4fa3eed | 2011-01-19 15:44:38 -0800 | [diff] [blame] | 788 | * |
| 789 | * @param off The offset of the first element to be copied. |
| 790 | * @param count The number of elements to be copied. |
| 791 | * @param d the source data array |
| 792 | */ |
| 793 | public void copy1DRangeFromUnchecked(int off, int count, int[] d) { |
Tim Murray | 6d7a53c | 2013-05-23 16:59:23 -0700 | [diff] [blame] | 794 | Trace.traceBegin(RenderScript.TRACE_TAG, "copy1DRangeFromUnchecked"); |
Alex Sakhartchouk | 918e840 | 2012-04-11 14:04:23 -0700 | [diff] [blame] | 795 | int dataSize = mType.mElement.getBytesSize() * count; |
Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 796 | data1DChecks(off, count, d.length * 4, dataSize); |
Jason Sams | 48fe534 | 2011-07-08 13:52:30 -0700 | [diff] [blame] | 797 | mRS.nAllocationData1D(getIDSafe(), off, mSelectedLOD, count, d, dataSize); |
Tim Murray | 6d7a53c | 2013-05-23 16:59:23 -0700 | [diff] [blame] | 798 | Trace.traceEnd(RenderScript.TRACE_TAG); |
Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 799 | } |
Tim Murray | 6d7a53c | 2013-05-23 16:59:23 -0700 | [diff] [blame] | 800 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 801 | /** |
Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 802 | * Copy an array into part of this Allocation. This method does not |
| 803 | * guarantee that the Allocation is compatible with the input buffer. |
Jason Sams | 4fa3eed | 2011-01-19 15:44:38 -0800 | [diff] [blame] | 804 | * |
| 805 | * @param off The offset of the first element to be copied. |
| 806 | * @param count The number of elements to be copied. |
| 807 | * @param d the source data array |
| 808 | */ |
| 809 | public void copy1DRangeFromUnchecked(int off, int count, short[] d) { |
Tim Murray | 6d7a53c | 2013-05-23 16:59:23 -0700 | [diff] [blame] | 810 | Trace.traceBegin(RenderScript.TRACE_TAG, "copy1DRangeFromUnchecked"); |
Alex Sakhartchouk | 918e840 | 2012-04-11 14:04:23 -0700 | [diff] [blame] | 811 | int dataSize = mType.mElement.getBytesSize() * count; |
Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 812 | data1DChecks(off, count, d.length * 2, dataSize); |
Jason Sams | 48fe534 | 2011-07-08 13:52:30 -0700 | [diff] [blame] | 813 | mRS.nAllocationData1D(getIDSafe(), off, mSelectedLOD, count, d, dataSize); |
Tim Murray | 6d7a53c | 2013-05-23 16:59:23 -0700 | [diff] [blame] | 814 | Trace.traceEnd(RenderScript.TRACE_TAG); |
Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 815 | } |
Tim Murray | 6d7a53c | 2013-05-23 16:59:23 -0700 | [diff] [blame] | 816 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 817 | /** |
Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 818 | * Copy an array into part of this Allocation. This method does not |
| 819 | * guarantee that the Allocation is compatible with the input buffer. |
Jason Sams | 4fa3eed | 2011-01-19 15:44:38 -0800 | [diff] [blame] | 820 | * |
| 821 | * @param off The offset of the first element to be copied. |
| 822 | * @param count The number of elements to be copied. |
| 823 | * @param d the source data array |
| 824 | */ |
| 825 | public void copy1DRangeFromUnchecked(int off, int count, byte[] d) { |
Tim Murray | 6d7a53c | 2013-05-23 16:59:23 -0700 | [diff] [blame] | 826 | Trace.traceBegin(RenderScript.TRACE_TAG, "copy1DRangeFromUnchecked"); |
Alex Sakhartchouk | 918e840 | 2012-04-11 14:04:23 -0700 | [diff] [blame] | 827 | int dataSize = mType.mElement.getBytesSize() * count; |
Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 828 | data1DChecks(off, count, d.length, dataSize); |
Jason Sams | 48fe534 | 2011-07-08 13:52:30 -0700 | [diff] [blame] | 829 | mRS.nAllocationData1D(getIDSafe(), off, mSelectedLOD, count, d, dataSize); |
Tim Murray | 6d7a53c | 2013-05-23 16:59:23 -0700 | [diff] [blame] | 830 | Trace.traceEnd(RenderScript.TRACE_TAG); |
Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 831 | } |
Tim Murray | 6d7a53c | 2013-05-23 16:59:23 -0700 | [diff] [blame] | 832 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 833 | /** |
Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 834 | * Copy an array into part of this Allocation. This method does not |
| 835 | * guarantee that the Allocation is compatible with the input buffer. |
Jason Sams | 4fa3eed | 2011-01-19 15:44:38 -0800 | [diff] [blame] | 836 | * |
| 837 | * @param off The offset of the first element to be copied. |
| 838 | * @param count The number of elements to be copied. |
| 839 | * @param d the source data array |
| 840 | */ |
| 841 | public void copy1DRangeFromUnchecked(int off, int count, float[] d) { |
Tim Murray | 6d7a53c | 2013-05-23 16:59:23 -0700 | [diff] [blame] | 842 | Trace.traceBegin(RenderScript.TRACE_TAG, "copy1DRangeFromUnchecked"); |
Alex Sakhartchouk | 918e840 | 2012-04-11 14:04:23 -0700 | [diff] [blame] | 843 | int dataSize = mType.mElement.getBytesSize() * count; |
Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 844 | data1DChecks(off, count, d.length * 4, dataSize); |
Jason Sams | 48fe534 | 2011-07-08 13:52:30 -0700 | [diff] [blame] | 845 | mRS.nAllocationData1D(getIDSafe(), off, mSelectedLOD, count, d, dataSize); |
Tim Murray | 6d7a53c | 2013-05-23 16:59:23 -0700 | [diff] [blame] | 846 | Trace.traceEnd(RenderScript.TRACE_TAG); |
Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 847 | } |
| 848 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 849 | /** |
Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 850 | * Copy an array into part of this Allocation. This variant is type checked |
| 851 | * and will generate exceptions if the Allocation type is not a 32 bit |
| 852 | * integer type. |
Jason Sams | 4fa3eed | 2011-01-19 15:44:38 -0800 | [diff] [blame] | 853 | * |
| 854 | * @param off The offset of the first element to be copied. |
| 855 | * @param count The number of elements to be copied. |
| 856 | * @param d the source data array |
| 857 | */ |
Jason Sams | b97b251 | 2011-01-16 15:04:08 -0800 | [diff] [blame] | 858 | public void copy1DRangeFrom(int off, int count, int[] d) { |
Tim Murray | 6d7a53c | 2013-05-23 16:59:23 -0700 | [diff] [blame] | 859 | Trace.traceBegin(RenderScript.TRACE_TAG, "copy1DRangeFrom"); |
Jason Sams | b97b251 | 2011-01-16 15:04:08 -0800 | [diff] [blame] | 860 | validateIsInt32(); |
| 861 | copy1DRangeFromUnchecked(off, count, d); |
Tim Murray | 6d7a53c | 2013-05-23 16:59:23 -0700 | [diff] [blame] | 862 | Trace.traceEnd(RenderScript.TRACE_TAG); |
Jason Sams | b97b251 | 2011-01-16 15:04:08 -0800 | [diff] [blame] | 863 | } |
Jason Sams | 4fa3eed | 2011-01-19 15:44:38 -0800 | [diff] [blame] | 864 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 865 | /** |
Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 866 | * Copy an array into part of this Allocation. This variant is type checked |
| 867 | * and will generate exceptions if the Allocation type is not a 16 bit |
| 868 | * integer type. |
Jason Sams | 4fa3eed | 2011-01-19 15:44:38 -0800 | [diff] [blame] | 869 | * |
| 870 | * @param off The offset of the first element to be copied. |
| 871 | * @param count The number of elements to be copied. |
| 872 | * @param d the source data array |
| 873 | */ |
Jason Sams | b97b251 | 2011-01-16 15:04:08 -0800 | [diff] [blame] | 874 | public void copy1DRangeFrom(int off, int count, short[] d) { |
Tim Murray | 6d7a53c | 2013-05-23 16:59:23 -0700 | [diff] [blame] | 875 | Trace.traceBegin(RenderScript.TRACE_TAG, "copy1DRangeFrom"); |
Jason Sams | b97b251 | 2011-01-16 15:04:08 -0800 | [diff] [blame] | 876 | validateIsInt16(); |
| 877 | copy1DRangeFromUnchecked(off, count, d); |
Tim Murray | 6d7a53c | 2013-05-23 16:59:23 -0700 | [diff] [blame] | 878 | Trace.traceEnd(RenderScript.TRACE_TAG); |
Jason Sams | b97b251 | 2011-01-16 15:04:08 -0800 | [diff] [blame] | 879 | } |
Jason Sams | 4fa3eed | 2011-01-19 15:44:38 -0800 | [diff] [blame] | 880 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 881 | /** |
Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 882 | * Copy an array into part of this Allocation. This variant is type checked |
| 883 | * and will generate exceptions if the Allocation type is not an 8 bit |
| 884 | * integer type. |
Jason Sams | 4fa3eed | 2011-01-19 15:44:38 -0800 | [diff] [blame] | 885 | * |
| 886 | * @param off The offset of the first element to be copied. |
| 887 | * @param count The number of elements to be copied. |
| 888 | * @param d the source data array |
| 889 | */ |
Jason Sams | b97b251 | 2011-01-16 15:04:08 -0800 | [diff] [blame] | 890 | public void copy1DRangeFrom(int off, int count, byte[] d) { |
Tim Murray | 6d7a53c | 2013-05-23 16:59:23 -0700 | [diff] [blame] | 891 | Trace.traceBegin(RenderScript.TRACE_TAG, "copy1DRangeFrom"); |
Jason Sams | b97b251 | 2011-01-16 15:04:08 -0800 | [diff] [blame] | 892 | validateIsInt8(); |
| 893 | copy1DRangeFromUnchecked(off, count, d); |
Tim Murray | 6d7a53c | 2013-05-23 16:59:23 -0700 | [diff] [blame] | 894 | Trace.traceEnd(RenderScript.TRACE_TAG); |
Jason Sams | b97b251 | 2011-01-16 15:04:08 -0800 | [diff] [blame] | 895 | } |
Jason Sams | 4fa3eed | 2011-01-19 15:44:38 -0800 | [diff] [blame] | 896 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 897 | /** |
Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 898 | * Copy an array into part of this Allocation. This variant is type checked |
| 899 | * and will generate exceptions if the Allocation type is not a 32 bit float |
| 900 | * type. |
Jason Sams | 4fa3eed | 2011-01-19 15:44:38 -0800 | [diff] [blame] | 901 | * |
| 902 | * @param off The offset of the first element to be copied. |
| 903 | * @param count The number of elements to be copied. |
Alex Sakhartchouk | 304b1f5 | 2011-06-14 11:13:19 -0700 | [diff] [blame] | 904 | * @param d the source data array. |
Jason Sams | 4fa3eed | 2011-01-19 15:44:38 -0800 | [diff] [blame] | 905 | */ |
Jason Sams | b97b251 | 2011-01-16 15:04:08 -0800 | [diff] [blame] | 906 | public void copy1DRangeFrom(int off, int count, float[] d) { |
Tim Murray | 6d7a53c | 2013-05-23 16:59:23 -0700 | [diff] [blame] | 907 | Trace.traceBegin(RenderScript.TRACE_TAG, "copy1DRangeFrom"); |
Jason Sams | b97b251 | 2011-01-16 15:04:08 -0800 | [diff] [blame] | 908 | validateIsFloat32(); |
| 909 | copy1DRangeFromUnchecked(off, count, d); |
Tim Murray | 6d7a53c | 2013-05-23 16:59:23 -0700 | [diff] [blame] | 910 | Trace.traceEnd(RenderScript.TRACE_TAG); |
Jason Sams | b97b251 | 2011-01-16 15:04:08 -0800 | [diff] [blame] | 911 | } |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 912 | /** |
Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 913 | * Copy part of an Allocation into this Allocation. |
Alex Sakhartchouk | 304b1f5 | 2011-06-14 11:13:19 -0700 | [diff] [blame] | 914 | * |
| 915 | * @param off The offset of the first element to be copied. |
| 916 | * @param count The number of elements to be copied. |
| 917 | * @param data the source data allocation. |
| 918 | * @param dataOff off The offset of the first element in data to |
| 919 | * be copied. |
| 920 | */ |
| 921 | public void copy1DRangeFrom(int off, int count, Allocation data, int dataOff) { |
Tim Murray | 6d7a53c | 2013-05-23 16:59:23 -0700 | [diff] [blame] | 922 | Trace.traceBegin(RenderScript.TRACE_TAG, "copy1DRangeFrom"); |
Jason Sams | 48fe534 | 2011-07-08 13:52:30 -0700 | [diff] [blame] | 923 | mRS.nAllocationData2D(getIDSafe(), off, 0, |
Jason Sams | ba862d1 | 2011-07-07 15:24:42 -0700 | [diff] [blame] | 924 | mSelectedLOD, mSelectedFace.mID, |
Jason Sams | e07694b | 2012-04-03 15:36:36 -0700 | [diff] [blame] | 925 | count, 1, data.getID(mRS), dataOff, 0, |
Jason Sams | ba862d1 | 2011-07-07 15:24:42 -0700 | [diff] [blame] | 926 | data.mSelectedLOD, data.mSelectedFace.mID); |
Alex Sakhartchouk | 304b1f5 | 2011-06-14 11:13:19 -0700 | [diff] [blame] | 927 | } |
| 928 | |
Jason Sams | fb9f82c | 2011-01-12 14:53:25 -0800 | [diff] [blame] | 929 | private void validate2DRange(int xoff, int yoff, int w, int h) { |
Jason Sams | ba862d1 | 2011-07-07 15:24:42 -0700 | [diff] [blame] | 930 | if (mAdaptedAllocation != null) { |
| 931 | |
| 932 | } else { |
| 933 | |
| 934 | if (xoff < 0 || yoff < 0) { |
| 935 | throw new RSIllegalArgumentException("Offset cannot be negative."); |
| 936 | } |
| 937 | if (h < 0 || w < 0) { |
| 938 | throw new RSIllegalArgumentException("Height or width cannot be negative."); |
| 939 | } |
| 940 | if (((xoff + w) > mCurrentDimX) || ((yoff + h) > mCurrentDimY)) { |
| 941 | throw new RSIllegalArgumentException("Updated region larger than allocation."); |
| 942 | } |
Jason Sams | fb9f82c | 2011-01-12 14:53:25 -0800 | [diff] [blame] | 943 | } |
| 944 | } |
Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 945 | |
Stephen Hines | a9a7b37 | 2013-02-08 17:11:31 -0800 | [diff] [blame] | 946 | void copy2DRangeFromUnchecked(int xoff, int yoff, int w, int h, byte[] data) { |
Tim Murray | 6d7a53c | 2013-05-23 16:59:23 -0700 | [diff] [blame] | 947 | Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeFromUnchecked"); |
Stephen Hines | a9a7b37 | 2013-02-08 17:11:31 -0800 | [diff] [blame] | 948 | mRS.validate(); |
| 949 | validate2DRange(xoff, yoff, w, h); |
| 950 | mRS.nAllocationData2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID, |
| 951 | w, h, data, data.length); |
Tim Murray | 6d7a53c | 2013-05-23 16:59:23 -0700 | [diff] [blame] | 952 | Trace.traceEnd(RenderScript.TRACE_TAG); |
Stephen Hines | a9a7b37 | 2013-02-08 17:11:31 -0800 | [diff] [blame] | 953 | } |
| 954 | |
| 955 | void copy2DRangeFromUnchecked(int xoff, int yoff, int w, int h, short[] data) { |
Tim Murray | 6d7a53c | 2013-05-23 16:59:23 -0700 | [diff] [blame] | 956 | Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeFromUnchecked"); |
Stephen Hines | a9a7b37 | 2013-02-08 17:11:31 -0800 | [diff] [blame] | 957 | mRS.validate(); |
| 958 | validate2DRange(xoff, yoff, w, h); |
| 959 | mRS.nAllocationData2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID, |
| 960 | w, h, data, data.length * 2); |
Tim Murray | 6d7a53c | 2013-05-23 16:59:23 -0700 | [diff] [blame] | 961 | Trace.traceEnd(RenderScript.TRACE_TAG); |
Stephen Hines | a9a7b37 | 2013-02-08 17:11:31 -0800 | [diff] [blame] | 962 | } |
| 963 | |
| 964 | void copy2DRangeFromUnchecked(int xoff, int yoff, int w, int h, int[] data) { |
Tim Murray | 6d7a53c | 2013-05-23 16:59:23 -0700 | [diff] [blame] | 965 | Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeFromUnchecked"); |
Stephen Hines | a9a7b37 | 2013-02-08 17:11:31 -0800 | [diff] [blame] | 966 | mRS.validate(); |
| 967 | validate2DRange(xoff, yoff, w, h); |
| 968 | mRS.nAllocationData2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID, |
| 969 | w, h, data, data.length * 4); |
Tim Murray | 6d7a53c | 2013-05-23 16:59:23 -0700 | [diff] [blame] | 970 | Trace.traceEnd(RenderScript.TRACE_TAG); |
Stephen Hines | a9a7b37 | 2013-02-08 17:11:31 -0800 | [diff] [blame] | 971 | } |
| 972 | |
| 973 | void copy2DRangeFromUnchecked(int xoff, int yoff, int w, int h, float[] data) { |
Tim Murray | 6d7a53c | 2013-05-23 16:59:23 -0700 | [diff] [blame] | 974 | Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeFromUnchecked"); |
Stephen Hines | a9a7b37 | 2013-02-08 17:11:31 -0800 | [diff] [blame] | 975 | mRS.validate(); |
| 976 | validate2DRange(xoff, yoff, w, h); |
| 977 | mRS.nAllocationData2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID, |
| 978 | w, h, data, data.length * 4); |
Tim Murray | 6d7a53c | 2013-05-23 16:59:23 -0700 | [diff] [blame] | 979 | Trace.traceEnd(RenderScript.TRACE_TAG); |
Stephen Hines | a9a7b37 | 2013-02-08 17:11:31 -0800 | [diff] [blame] | 980 | } |
| 981 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 982 | /** |
Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 983 | * Copy from an array into a rectangular region in this Allocation. The |
| 984 | * array is assumed to be tightly packed. |
Jason Sams | f708609 | 2011-01-12 13:28:37 -0800 | [diff] [blame] | 985 | * |
Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 986 | * @param xoff X offset of the region to update in this Allocation |
| 987 | * @param yoff Y offset of the region to update in this Allocation |
| 988 | * @param w Width of the region to update |
| 989 | * @param h Height of the region to update |
| 990 | * @param data to be placed into the Allocation |
Jason Sams | f708609 | 2011-01-12 13:28:37 -0800 | [diff] [blame] | 991 | */ |
| 992 | public void copy2DRangeFrom(int xoff, int yoff, int w, int h, byte[] data) { |
Tim Murray | 6d7a53c | 2013-05-23 16:59:23 -0700 | [diff] [blame] | 993 | Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeFrom"); |
Stephen Hines | 5f528be | 2013-02-08 21:03:51 -0800 | [diff] [blame] | 994 | validateIsInt8(); |
Stephen Hines | a9a7b37 | 2013-02-08 17:11:31 -0800 | [diff] [blame] | 995 | copy2DRangeFromUnchecked(xoff, yoff, w, h, data); |
Tim Murray | 6d7a53c | 2013-05-23 16:59:23 -0700 | [diff] [blame] | 996 | Trace.traceEnd(RenderScript.TRACE_TAG); |
Jason Sams | fa445b9 | 2011-01-07 17:00:07 -0800 | [diff] [blame] | 997 | } |
| 998 | |
Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 999 | /** |
| 1000 | * Copy from an array into a rectangular region in this Allocation. The |
| 1001 | * array is assumed to be tightly packed. |
| 1002 | * |
| 1003 | * @param xoff X offset of the region to update in this Allocation |
| 1004 | * @param yoff Y offset of the region to update in this Allocation |
| 1005 | * @param w Width of the region to update |
| 1006 | * @param h Height of the region to update |
| 1007 | * @param data to be placed into the Allocation |
| 1008 | */ |
Jason Sams | f708609 | 2011-01-12 13:28:37 -0800 | [diff] [blame] | 1009 | public void copy2DRangeFrom(int xoff, int yoff, int w, int h, short[] data) { |
Tim Murray | 6d7a53c | 2013-05-23 16:59:23 -0700 | [diff] [blame] | 1010 | Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeFrom"); |
Stephen Hines | 5f528be | 2013-02-08 21:03:51 -0800 | [diff] [blame] | 1011 | validateIsInt16(); |
Stephen Hines | a9a7b37 | 2013-02-08 17:11:31 -0800 | [diff] [blame] | 1012 | copy2DRangeFromUnchecked(xoff, yoff, w, h, data); |
Tim Murray | 6d7a53c | 2013-05-23 16:59:23 -0700 | [diff] [blame] | 1013 | Trace.traceEnd(RenderScript.TRACE_TAG); |
Jason Sams | fa445b9 | 2011-01-07 17:00:07 -0800 | [diff] [blame] | 1014 | } |
| 1015 | |
Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 1016 | /** |
| 1017 | * Copy from an array into a rectangular region in this Allocation. The |
| 1018 | * array is assumed to be tightly packed. |
| 1019 | * |
| 1020 | * @param xoff X offset of the region to update in this Allocation |
| 1021 | * @param yoff Y offset of the region to update in this Allocation |
| 1022 | * @param w Width of the region to update |
| 1023 | * @param h Height of the region to update |
| 1024 | * @param data to be placed into the Allocation |
| 1025 | */ |
Jason Sams | f708609 | 2011-01-12 13:28:37 -0800 | [diff] [blame] | 1026 | public void copy2DRangeFrom(int xoff, int yoff, int w, int h, int[] data) { |
Tim Murray | 6d7a53c | 2013-05-23 16:59:23 -0700 | [diff] [blame] | 1027 | Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeFrom"); |
Stephen Hines | 5f528be | 2013-02-08 21:03:51 -0800 | [diff] [blame] | 1028 | validateIsInt32(); |
Stephen Hines | a9a7b37 | 2013-02-08 17:11:31 -0800 | [diff] [blame] | 1029 | copy2DRangeFromUnchecked(xoff, yoff, w, h, data); |
Tim Murray | 6d7a53c | 2013-05-23 16:59:23 -0700 | [diff] [blame] | 1030 | Trace.traceEnd(RenderScript.TRACE_TAG); |
Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 1031 | } |
| 1032 | |
Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 1033 | /** |
| 1034 | * Copy from an array into a rectangular region in this Allocation. The |
| 1035 | * array is assumed to be tightly packed. |
| 1036 | * |
| 1037 | * @param xoff X offset of the region to update in this Allocation |
| 1038 | * @param yoff Y offset of the region to update in this Allocation |
| 1039 | * @param w Width of the region to update |
| 1040 | * @param h Height of the region to update |
| 1041 | * @param data to be placed into the Allocation |
| 1042 | */ |
Jason Sams | f708609 | 2011-01-12 13:28:37 -0800 | [diff] [blame] | 1043 | public void copy2DRangeFrom(int xoff, int yoff, int w, int h, float[] data) { |
Tim Murray | 6d7a53c | 2013-05-23 16:59:23 -0700 | [diff] [blame] | 1044 | Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeFrom"); |
Stephen Hines | 5f528be | 2013-02-08 21:03:51 -0800 | [diff] [blame] | 1045 | validateIsFloat32(); |
Stephen Hines | a9a7b37 | 2013-02-08 17:11:31 -0800 | [diff] [blame] | 1046 | copy2DRangeFromUnchecked(xoff, yoff, w, h, data); |
Tim Murray | 6d7a53c | 2013-05-23 16:59:23 -0700 | [diff] [blame] | 1047 | Trace.traceEnd(RenderScript.TRACE_TAG); |
Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 1048 | } |
| 1049 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 1050 | /** |
Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 1051 | * Copy a rectangular region from an Allocation into a rectangular region in |
| 1052 | * this Allocation. |
Alex Sakhartchouk | 304b1f5 | 2011-06-14 11:13:19 -0700 | [diff] [blame] | 1053 | * |
Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 1054 | * @param xoff X offset of the region in this Allocation |
| 1055 | * @param yoff Y offset of the region in this Allocation |
| 1056 | * @param w Width of the region to update. |
| 1057 | * @param h Height of the region to update. |
| 1058 | * @param data source Allocation. |
| 1059 | * @param dataXoff X offset in source Allocation |
| 1060 | * @param dataYoff Y offset in source Allocation |
Alex Sakhartchouk | 304b1f5 | 2011-06-14 11:13:19 -0700 | [diff] [blame] | 1061 | */ |
| 1062 | public void copy2DRangeFrom(int xoff, int yoff, int w, int h, |
| 1063 | Allocation data, int dataXoff, int dataYoff) { |
Tim Murray | 6d7a53c | 2013-05-23 16:59:23 -0700 | [diff] [blame] | 1064 | Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeFrom"); |
Alex Sakhartchouk | 304b1f5 | 2011-06-14 11:13:19 -0700 | [diff] [blame] | 1065 | mRS.validate(); |
| 1066 | validate2DRange(xoff, yoff, w, h); |
Jason Sams | 48fe534 | 2011-07-08 13:52:30 -0700 | [diff] [blame] | 1067 | mRS.nAllocationData2D(getIDSafe(), xoff, yoff, |
Jason Sams | ba862d1 | 2011-07-07 15:24:42 -0700 | [diff] [blame] | 1068 | mSelectedLOD, mSelectedFace.mID, |
Jason Sams | e07694b | 2012-04-03 15:36:36 -0700 | [diff] [blame] | 1069 | w, h, data.getID(mRS), dataXoff, dataYoff, |
Jason Sams | ba862d1 | 2011-07-07 15:24:42 -0700 | [diff] [blame] | 1070 | data.mSelectedLOD, data.mSelectedFace.mID); |
Tim Murray | 6d7a53c | 2013-05-23 16:59:23 -0700 | [diff] [blame] | 1071 | Trace.traceEnd(RenderScript.TRACE_TAG); |
Alex Sakhartchouk | 304b1f5 | 2011-06-14 11:13:19 -0700 | [diff] [blame] | 1072 | } |
| 1073 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 1074 | /** |
Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 1075 | * Copy a {@link android.graphics.Bitmap} into an Allocation. The height |
| 1076 | * and width of the update will use the height and width of the {@link |
| 1077 | * android.graphics.Bitmap}. |
Jason Sams | f708609 | 2011-01-12 13:28:37 -0800 | [diff] [blame] | 1078 | * |
Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 1079 | * @param xoff X offset of the region to update in this Allocation |
| 1080 | * @param yoff Y offset of the region to update in this Allocation |
| 1081 | * @param data the Bitmap to be copied |
Jason Sams | f708609 | 2011-01-12 13:28:37 -0800 | [diff] [blame] | 1082 | */ |
| 1083 | public void copy2DRangeFrom(int xoff, int yoff, Bitmap data) { |
Tim Murray | 6d7a53c | 2013-05-23 16:59:23 -0700 | [diff] [blame] | 1084 | Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeFrom"); |
Jason Sams | fa445b9 | 2011-01-07 17:00:07 -0800 | [diff] [blame] | 1085 | mRS.validate(); |
Tim Murray | abd5db9 | 2013-02-28 11:45:22 -0800 | [diff] [blame] | 1086 | if (data.getConfig() == null) { |
| 1087 | Bitmap newBitmap = Bitmap.createBitmap(data.getWidth(), data.getHeight(), Bitmap.Config.ARGB_8888); |
| 1088 | Canvas c = new Canvas(newBitmap); |
| 1089 | c.drawBitmap(data, 0, 0, null); |
| 1090 | copy2DRangeFrom(xoff, yoff, newBitmap); |
Jason Sams | b05d689 | 2013-04-09 15:59:24 -0700 | [diff] [blame] | 1091 | return; |
Tim Murray | abd5db9 | 2013-02-28 11:45:22 -0800 | [diff] [blame] | 1092 | } |
Jason Sams | fb9f82c | 2011-01-12 14:53:25 -0800 | [diff] [blame] | 1093 | validateBitmapFormat(data); |
| 1094 | validate2DRange(xoff, yoff, data.getWidth(), data.getHeight()); |
Jason Sams | 48fe534 | 2011-07-08 13:52:30 -0700 | [diff] [blame] | 1095 | mRS.nAllocationData2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID, data); |
Tim Murray | 6d7a53c | 2013-05-23 16:59:23 -0700 | [diff] [blame] | 1096 | Trace.traceEnd(RenderScript.TRACE_TAG); |
Jason Sams | fa445b9 | 2011-01-07 17:00:07 -0800 | [diff] [blame] | 1097 | } |
| 1098 | |
Jason Sams | b05d689 | 2013-04-09 15:59:24 -0700 | [diff] [blame] | 1099 | private void validate3DRange(int xoff, int yoff, int zoff, int w, int h, int d) { |
| 1100 | if (mAdaptedAllocation != null) { |
| 1101 | |
| 1102 | } else { |
| 1103 | |
| 1104 | if (xoff < 0 || yoff < 0 || zoff < 0) { |
| 1105 | throw new RSIllegalArgumentException("Offset cannot be negative."); |
| 1106 | } |
| 1107 | if (h < 0 || w < 0 || d < 0) { |
| 1108 | throw new RSIllegalArgumentException("Height or width cannot be negative."); |
| 1109 | } |
| 1110 | if (((xoff + w) > mCurrentDimX) || ((yoff + h) > mCurrentDimY) || ((zoff + d) > mCurrentDimZ)) { |
| 1111 | throw new RSIllegalArgumentException("Updated region larger than allocation."); |
| 1112 | } |
| 1113 | } |
| 1114 | } |
| 1115 | |
| 1116 | /** |
| 1117 | * @hide |
| 1118 | * |
| 1119 | */ |
| 1120 | void copy3DRangeFromUnchecked(int xoff, int yoff, int zoff, int w, int h, int d, byte[] data) { |
| 1121 | mRS.validate(); |
| 1122 | validate3DRange(xoff, yoff, zoff, w, h, d); |
| 1123 | mRS.nAllocationData3D(getIDSafe(), xoff, yoff, zoff, mSelectedLOD, |
| 1124 | w, h, d, data, data.length); |
| 1125 | } |
| 1126 | |
| 1127 | /** |
| 1128 | * @hide |
| 1129 | * |
| 1130 | */ |
| 1131 | void copy3DRangeFromUnchecked(int xoff, int yoff, int zoff, int w, int h, int d, short[] data) { |
| 1132 | mRS.validate(); |
| 1133 | validate3DRange(xoff, yoff, zoff, w, h, d); |
| 1134 | mRS.nAllocationData3D(getIDSafe(), xoff, yoff, zoff, mSelectedLOD, |
| 1135 | w, h, d, data, data.length * 2); |
| 1136 | } |
| 1137 | |
| 1138 | /** |
| 1139 | * @hide |
| 1140 | * |
| 1141 | */ |
| 1142 | void copy3DRangeFromUnchecked(int xoff, int yoff, int zoff, int w, int h, int d, int[] data) { |
| 1143 | mRS.validate(); |
| 1144 | validate3DRange(xoff, yoff, zoff, w, h, d); |
| 1145 | mRS.nAllocationData3D(getIDSafe(), xoff, yoff, zoff, mSelectedLOD, |
| 1146 | w, h, d, data, data.length * 4); |
| 1147 | } |
| 1148 | |
| 1149 | /** |
| 1150 | * @hide |
| 1151 | * |
| 1152 | */ |
| 1153 | void copy3DRangeFromUnchecked(int xoff, int yoff, int zoff, int w, int h, int d, float[] data) { |
| 1154 | mRS.validate(); |
| 1155 | validate3DRange(xoff, yoff, zoff, w, h, d); |
| 1156 | mRS.nAllocationData3D(getIDSafe(), xoff, yoff, zoff, mSelectedLOD, |
| 1157 | w, h, d, data, data.length * 4); |
| 1158 | } |
| 1159 | |
| 1160 | |
| 1161 | /** |
| 1162 | * @hide |
| 1163 | * Copy a rectangular region from the array into the allocation. |
Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 1164 | * The array is assumed to be tightly packed. |
Jason Sams | b05d689 | 2013-04-09 15:59:24 -0700 | [diff] [blame] | 1165 | * |
Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 1166 | * @param xoff X offset of the region to update in this Allocation |
| 1167 | * @param yoff Y offset of the region to update in this Allocation |
| 1168 | * @param zoff Z offset of the region to update in this Allocation |
| 1169 | * @param w Width of the region to update |
| 1170 | * @param h Height of the region to update |
| 1171 | * @param d Depth of the region to update |
Jason Sams | b05d689 | 2013-04-09 15:59:24 -0700 | [diff] [blame] | 1172 | * @param data to be placed into the allocation |
| 1173 | */ |
| 1174 | public void copy3DRangeFrom(int xoff, int yoff, int zoff, int w, int h, int d, byte[] data) { |
| 1175 | validateIsInt8(); |
| 1176 | copy3DRangeFromUnchecked(xoff, yoff, zoff, w, h, d, data); |
| 1177 | } |
| 1178 | |
| 1179 | /** |
| 1180 | * @hide |
| 1181 | * |
| 1182 | */ |
| 1183 | public void copy3DRangeFrom(int xoff, int yoff, int zoff, int w, int h, int d, short[] data) { |
| 1184 | validateIsInt16(); |
| 1185 | copy3DRangeFromUnchecked(xoff, yoff, zoff, w, h, d, data); |
| 1186 | } |
| 1187 | |
| 1188 | /** |
| 1189 | * @hide |
| 1190 | * |
| 1191 | */ |
| 1192 | public void copy3DRangeFrom(int xoff, int yoff, int zoff, int w, int h, int d, int[] data) { |
| 1193 | validateIsInt32(); |
| 1194 | copy3DRangeFromUnchecked(xoff, yoff, zoff, w, h, d, data); |
| 1195 | } |
| 1196 | |
| 1197 | /** |
| 1198 | * @hide |
| 1199 | * |
| 1200 | */ |
| 1201 | public void copy3DRangeFrom(int xoff, int yoff, int zoff, int w, int h, int d, float[] data) { |
| 1202 | validateIsFloat32(); |
| 1203 | copy3DRangeFromUnchecked(xoff, yoff, zoff, w, h, d, data); |
| 1204 | } |
| 1205 | |
| 1206 | /** |
| 1207 | * @hide |
| 1208 | * Copy a rectangular region into the allocation from another |
| 1209 | * allocation. |
| 1210 | * |
Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 1211 | * @param xoff X offset of the region to update in this Allocation |
| 1212 | * @param yoff Y offset of the region to update in this Allocation |
| 1213 | * @param zoff Z offset of the region to update in this Allocation |
| 1214 | * @param w Width of the region to update. |
| 1215 | * @param h Height of the region to update. |
| 1216 | * @param d Depth of the region to update. |
Jason Sams | b05d689 | 2013-04-09 15:59:24 -0700 | [diff] [blame] | 1217 | * @param data source allocation. |
Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 1218 | * @param dataXoff X offset of the region in the source Allocation |
| 1219 | * @param dataYoff Y offset of the region in the source Allocation |
| 1220 | * @param dataZoff Z offset of the region in the source Allocation |
Jason Sams | b05d689 | 2013-04-09 15:59:24 -0700 | [diff] [blame] | 1221 | */ |
| 1222 | public void copy3DRangeFrom(int xoff, int yoff, int zoff, int w, int h, int d, |
| 1223 | Allocation data, int dataXoff, int dataYoff, int dataZoff) { |
| 1224 | mRS.validate(); |
| 1225 | validate3DRange(xoff, yoff, zoff, w, h, d); |
| 1226 | mRS.nAllocationData3D(getIDSafe(), xoff, yoff, zoff, mSelectedLOD, |
| 1227 | w, h, d, data.getID(mRS), dataXoff, dataYoff, dataZoff, |
| 1228 | data.mSelectedLOD); |
| 1229 | } |
| 1230 | |
Jason Sams | fa445b9 | 2011-01-07 17:00:07 -0800 | [diff] [blame] | 1231 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 1232 | /** |
Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 1233 | * Copy from the Allocation into a {@link android.graphics.Bitmap}. The |
| 1234 | * bitmap must match the dimensions of the Allocation. |
Jason Sams | 48fe534 | 2011-07-08 13:52:30 -0700 | [diff] [blame] | 1235 | * |
| 1236 | * @param b The bitmap to be set from the Allocation. |
| 1237 | */ |
Jason Sams | fa445b9 | 2011-01-07 17:00:07 -0800 | [diff] [blame] | 1238 | public void copyTo(Bitmap b) { |
Tim Murray | 6d7a53c | 2013-05-23 16:59:23 -0700 | [diff] [blame] | 1239 | Trace.traceBegin(RenderScript.TRACE_TAG, "copyTo"); |
Jason Sams | fb9f82c | 2011-01-12 14:53:25 -0800 | [diff] [blame] | 1240 | mRS.validate(); |
| 1241 | validateBitmapFormat(b); |
| 1242 | validateBitmapSize(b); |
Jason Sams | e07694b | 2012-04-03 15:36:36 -0700 | [diff] [blame] | 1243 | mRS.nAllocationCopyToBitmap(getID(mRS), b); |
Tim Murray | 6d7a53c | 2013-05-23 16:59:23 -0700 | [diff] [blame] | 1244 | Trace.traceEnd(RenderScript.TRACE_TAG); |
Jason Sams | fa445b9 | 2011-01-07 17:00:07 -0800 | [diff] [blame] | 1245 | } |
| 1246 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 1247 | /** |
Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 1248 | * Copy from the Allocation into a byte array. The array must be at least |
| 1249 | * as large as the Allocation. The allocation must be of an 8 bit integer |
| 1250 | * {@link android.renderscript.Element} type. |
Jason Sams | 48fe534 | 2011-07-08 13:52:30 -0700 | [diff] [blame] | 1251 | * |
| 1252 | * @param d The array to be set from the Allocation. |
| 1253 | */ |
Jason Sams | fa445b9 | 2011-01-07 17:00:07 -0800 | [diff] [blame] | 1254 | public void copyTo(byte[] d) { |
Tim Murray | 6d7a53c | 2013-05-23 16:59:23 -0700 | [diff] [blame] | 1255 | Trace.traceBegin(RenderScript.TRACE_TAG, "copyTo"); |
Jason Sams | b97b251 | 2011-01-16 15:04:08 -0800 | [diff] [blame] | 1256 | validateIsInt8(); |
Jason Sams | 771bebb | 2009-12-07 12:40:12 -0800 | [diff] [blame] | 1257 | mRS.validate(); |
Jason Sams | e07694b | 2012-04-03 15:36:36 -0700 | [diff] [blame] | 1258 | mRS.nAllocationRead(getID(mRS), d); |
Tim Murray | 6d7a53c | 2013-05-23 16:59:23 -0700 | [diff] [blame] | 1259 | Trace.traceEnd(RenderScript.TRACE_TAG); |
Jason Sams | 40a29e8 | 2009-08-10 14:55:26 -0700 | [diff] [blame] | 1260 | } |
| 1261 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 1262 | /** |
Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 1263 | * Copy from the Allocation into a short array. The array must be at least |
| 1264 | * as large as the Allocation. The allocation must be of an 16 bit integer |
| 1265 | * {@link android.renderscript.Element} type. |
Jason Sams | 48fe534 | 2011-07-08 13:52:30 -0700 | [diff] [blame] | 1266 | * |
| 1267 | * @param d The array to be set from the Allocation. |
| 1268 | */ |
Jason Sams | fa445b9 | 2011-01-07 17:00:07 -0800 | [diff] [blame] | 1269 | public void copyTo(short[] d) { |
Tim Murray | 6d7a53c | 2013-05-23 16:59:23 -0700 | [diff] [blame] | 1270 | Trace.traceBegin(RenderScript.TRACE_TAG, "copyTo"); |
Jason Sams | b97b251 | 2011-01-16 15:04:08 -0800 | [diff] [blame] | 1271 | validateIsInt16(); |
Jason Sams | fa445b9 | 2011-01-07 17:00:07 -0800 | [diff] [blame] | 1272 | mRS.validate(); |
Jason Sams | e07694b | 2012-04-03 15:36:36 -0700 | [diff] [blame] | 1273 | mRS.nAllocationRead(getID(mRS), d); |
Tim Murray | 6d7a53c | 2013-05-23 16:59:23 -0700 | [diff] [blame] | 1274 | Trace.traceEnd(RenderScript.TRACE_TAG); |
Jason Sams | fa445b9 | 2011-01-07 17:00:07 -0800 | [diff] [blame] | 1275 | } |
| 1276 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 1277 | /** |
Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 1278 | * Copy from the Allocation into a int array. The array must be at least as |
| 1279 | * large as the Allocation. The allocation must be of an 32 bit integer |
| 1280 | * {@link android.renderscript.Element} type. |
Jason Sams | 48fe534 | 2011-07-08 13:52:30 -0700 | [diff] [blame] | 1281 | * |
| 1282 | * @param d The array to be set from the Allocation. |
| 1283 | */ |
Jason Sams | fa445b9 | 2011-01-07 17:00:07 -0800 | [diff] [blame] | 1284 | public void copyTo(int[] d) { |
Tim Murray | 6d7a53c | 2013-05-23 16:59:23 -0700 | [diff] [blame] | 1285 | Trace.traceBegin(RenderScript.TRACE_TAG, "copyTo"); |
Jason Sams | b97b251 | 2011-01-16 15:04:08 -0800 | [diff] [blame] | 1286 | validateIsInt32(); |
Jason Sams | fa445b9 | 2011-01-07 17:00:07 -0800 | [diff] [blame] | 1287 | mRS.validate(); |
Jason Sams | e07694b | 2012-04-03 15:36:36 -0700 | [diff] [blame] | 1288 | mRS.nAllocationRead(getID(mRS), d); |
Tim Murray | 6d7a53c | 2013-05-23 16:59:23 -0700 | [diff] [blame] | 1289 | Trace.traceEnd(RenderScript.TRACE_TAG); |
Jason Sams | fa445b9 | 2011-01-07 17:00:07 -0800 | [diff] [blame] | 1290 | } |
| 1291 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 1292 | /** |
Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 1293 | * Copy from the Allocation into a float array. The array must be at least |
| 1294 | * as large as the Allocation. The allocation must be of an 32 bit float |
| 1295 | * {@link android.renderscript.Element} type. |
Jason Sams | 48fe534 | 2011-07-08 13:52:30 -0700 | [diff] [blame] | 1296 | * |
| 1297 | * @param d The array to be set from the Allocation. |
| 1298 | */ |
Jason Sams | fa445b9 | 2011-01-07 17:00:07 -0800 | [diff] [blame] | 1299 | public void copyTo(float[] d) { |
Tim Murray | 6d7a53c | 2013-05-23 16:59:23 -0700 | [diff] [blame] | 1300 | Trace.traceBegin(RenderScript.TRACE_TAG, "copyTo"); |
Jason Sams | b97b251 | 2011-01-16 15:04:08 -0800 | [diff] [blame] | 1301 | validateIsFloat32(); |
Jason Sams | 771bebb | 2009-12-07 12:40:12 -0800 | [diff] [blame] | 1302 | mRS.validate(); |
Jason Sams | e07694b | 2012-04-03 15:36:36 -0700 | [diff] [blame] | 1303 | mRS.nAllocationRead(getID(mRS), d); |
Tim Murray | 6d7a53c | 2013-05-23 16:59:23 -0700 | [diff] [blame] | 1304 | Trace.traceEnd(RenderScript.TRACE_TAG); |
Jason Sams | 40a29e8 | 2009-08-10 14:55:26 -0700 | [diff] [blame] | 1305 | } |
| 1306 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 1307 | /** |
Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 1308 | * Resize a 1D allocation. The contents of the allocation are preserved. |
| 1309 | * If new elements are allocated objects are created with null contents and |
| 1310 | * the new region is otherwise undefined. |
Jason Sams | f708609 | 2011-01-12 13:28:37 -0800 | [diff] [blame] | 1311 | * |
Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 1312 | * <p>If the new region is smaller the references of any objects outside the |
| 1313 | * new region will be released.</p> |
Jason Sams | f708609 | 2011-01-12 13:28:37 -0800 | [diff] [blame] | 1314 | * |
Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 1315 | * <p>A new type will be created with the new dimension.</p> |
Jason Sams | f708609 | 2011-01-12 13:28:37 -0800 | [diff] [blame] | 1316 | * |
| 1317 | * @param dimX The new size of the allocation. |
Jason Sams | b05d689 | 2013-04-09 15:59:24 -0700 | [diff] [blame] | 1318 | * |
Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 1319 | * @deprecated RenderScript objects should be immutable once created. The |
| 1320 | * replacement is to create a new allocation and copy the contents. |
Jason Sams | f708609 | 2011-01-12 13:28:37 -0800 | [diff] [blame] | 1321 | */ |
Jason Sams | 31a7e42 | 2010-10-26 13:09:17 -0700 | [diff] [blame] | 1322 | public synchronized void resize(int dimX) { |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 1323 | if ((mType.getY() > 0)|| (mType.getZ() > 0) || mType.hasFaces() || mType.hasMipmaps()) { |
Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 1324 | throw new RSInvalidStateException("Resize only support for 1D allocations at this time."); |
Jason Sams | 5edc608 | 2010-10-05 13:32:49 -0700 | [diff] [blame] | 1325 | } |
Jason Sams | e07694b | 2012-04-03 15:36:36 -0700 | [diff] [blame] | 1326 | mRS.nAllocationResize1D(getID(mRS), dimX); |
Jason Sams | d26297f | 2010-11-01 16:08:59 -0700 | [diff] [blame] | 1327 | mRS.finish(); // Necessary because resize is fifoed and update is async. |
Jason Sams | 31a7e42 | 2010-10-26 13:09:17 -0700 | [diff] [blame] | 1328 | |
Jason Sams | e07694b | 2012-04-03 15:36:36 -0700 | [diff] [blame] | 1329 | int typeID = mRS.nAllocationGetType(getID(mRS)); |
Jason Sams | 31a7e42 | 2010-10-26 13:09:17 -0700 | [diff] [blame] | 1330 | mType = new Type(typeID, mRS); |
| 1331 | mType.updateFromNative(); |
Jason Sams | 452a766 | 2011-07-07 16:05:18 -0700 | [diff] [blame] | 1332 | updateCacheInfo(mType); |
Jason Sams | 5edc608 | 2010-10-05 13:32:49 -0700 | [diff] [blame] | 1333 | } |
| 1334 | |
Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 1335 | |
| 1336 | // creation |
| 1337 | |
Jason Sams | 49a05d7 | 2010-12-29 14:31:29 -0800 | [diff] [blame] | 1338 | static BitmapFactory.Options mBitmapOptions = new BitmapFactory.Options(); |
Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 1339 | static { |
| 1340 | mBitmapOptions.inScaled = false; |
| 1341 | } |
| 1342 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 1343 | /** |
Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 1344 | * Creates a new Allocation with the given {@link |
| 1345 | * android.renderscript.Type}, mipmap flag, and usage flags. |
Alex Sakhartchouk | 623c54d | 2011-01-12 17:32:36 -0800 | [diff] [blame] | 1346 | * |
Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 1347 | * @param type RenderScript type describing data layout |
Alex Sakhartchouk | 623c54d | 2011-01-12 17:32:36 -0800 | [diff] [blame] | 1348 | * @param mips specifies desired mipmap behaviour for the |
| 1349 | * allocation |
Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 1350 | * @param usage bit field specifying how the Allocation is |
Alex Sakhartchouk | 623c54d | 2011-01-12 17:32:36 -0800 | [diff] [blame] | 1351 | * utilized |
| 1352 | */ |
| 1353 | static public Allocation createTyped(RenderScript rs, Type type, MipmapControl mips, int usage) { |
Tim Murray | 6d7a53c | 2013-05-23 16:59:23 -0700 | [diff] [blame] | 1354 | Trace.traceBegin(RenderScript.TRACE_TAG, "createTyped"); |
Jason Sams | 771bebb | 2009-12-07 12:40:12 -0800 | [diff] [blame] | 1355 | rs.validate(); |
Jason Sams | e07694b | 2012-04-03 15:36:36 -0700 | [diff] [blame] | 1356 | if (type.getID(rs) == 0) { |
Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 1357 | throw new RSInvalidStateException("Bad Type"); |
Jason Sams | 1bada8c | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 1358 | } |
Jason Sams | e07694b | 2012-04-03 15:36:36 -0700 | [diff] [blame] | 1359 | int id = rs.nAllocationCreateTyped(type.getID(rs), mips.mID, usage, 0); |
Jason Sams | 857d0c7 | 2011-11-23 15:02:15 -0800 | [diff] [blame] | 1360 | if (id == 0) { |
| 1361 | throw new RSRuntimeException("Allocation creation failed."); |
| 1362 | } |
Tim Murray | 6d7a53c | 2013-05-23 16:59:23 -0700 | [diff] [blame] | 1363 | Trace.traceEnd(RenderScript.TRACE_TAG); |
Jason Sams | 857d0c7 | 2011-11-23 15:02:15 -0800 | [diff] [blame] | 1364 | return new Allocation(id, rs, type, usage); |
| 1365 | } |
| 1366 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 1367 | /** |
Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 1368 | * Creates an Allocation with the size specified by the type and no mipmaps |
| 1369 | * generated by default |
Alex Sakhartchouk | 623c54d | 2011-01-12 17:32:36 -0800 | [diff] [blame] | 1370 | * |
Alex Sakhartchouk | f5c876e | 2011-01-13 14:53:43 -0800 | [diff] [blame] | 1371 | * @param rs Context to which the allocation will belong. |
Alex Sakhartchouk | 623c54d | 2011-01-12 17:32:36 -0800 | [diff] [blame] | 1372 | * @param type renderscript type describing data layout |
| 1373 | * @param usage bit field specifying how the allocation is |
| 1374 | * utilized |
| 1375 | * |
| 1376 | * @return allocation |
| 1377 | */ |
Jason Sams | e5d3712 | 2010-12-16 00:33:33 -0800 | [diff] [blame] | 1378 | static public Allocation createTyped(RenderScript rs, Type type, int usage) { |
| 1379 | return createTyped(rs, type, MipmapControl.MIPMAP_NONE, usage); |
| 1380 | } |
| 1381 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 1382 | /** |
Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 1383 | * Creates an Allocation for use by scripts with a given {@link |
| 1384 | * android.renderscript.Type} and no mipmaps |
Alex Sakhartchouk | 623c54d | 2011-01-12 17:32:36 -0800 | [diff] [blame] | 1385 | * |
Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 1386 | * @param rs Context to which the Allocation will belong. |
| 1387 | * @param type RenderScript Type describing data layout |
Alex Sakhartchouk | 623c54d | 2011-01-12 17:32:36 -0800 | [diff] [blame] | 1388 | * |
| 1389 | * @return allocation |
| 1390 | */ |
Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 1391 | static public Allocation createTyped(RenderScript rs, Type type) { |
Jason Sams | d4b23b5 | 2010-12-13 15:32:35 -0800 | [diff] [blame] | 1392 | return createTyped(rs, type, MipmapControl.MIPMAP_NONE, USAGE_SCRIPT); |
Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 1393 | } |
Jason Sams | 1bada8c | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 1394 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 1395 | /** |
Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 1396 | * Creates an Allocation with a specified number of given elements |
Alex Sakhartchouk | 623c54d | 2011-01-12 17:32:36 -0800 | [diff] [blame] | 1397 | * |
Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 1398 | * @param rs Context to which the Allocation will belong. |
| 1399 | * @param e Element to use in the Allocation |
| 1400 | * @param count the number of Elements in the Allocation |
| 1401 | * @param usage bit field specifying how the Allocation is |
Alex Sakhartchouk | 623c54d | 2011-01-12 17:32:36 -0800 | [diff] [blame] | 1402 | * utilized |
| 1403 | * |
| 1404 | * @return allocation |
| 1405 | */ |
Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 1406 | static public Allocation createSized(RenderScript rs, Element e, |
| 1407 | int count, int usage) { |
Tim Murray | 6d7a53c | 2013-05-23 16:59:23 -0700 | [diff] [blame] | 1408 | Trace.traceBegin(RenderScript.TRACE_TAG, "createSized"); |
Jason Sams | 771bebb | 2009-12-07 12:40:12 -0800 | [diff] [blame] | 1409 | rs.validate(); |
Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 1410 | Type.Builder b = new Type.Builder(rs, e); |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 1411 | b.setX(count); |
Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 1412 | Type t = b.create(); |
| 1413 | |
Jason Sams | e07694b | 2012-04-03 15:36:36 -0700 | [diff] [blame] | 1414 | int id = rs.nAllocationCreateTyped(t.getID(rs), MipmapControl.MIPMAP_NONE.mID, usage, 0); |
Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 1415 | if (id == 0) { |
Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 1416 | throw new RSRuntimeException("Allocation creation failed."); |
Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 1417 | } |
Tim Murray | 6d7a53c | 2013-05-23 16:59:23 -0700 | [diff] [blame] | 1418 | Trace.traceEnd(RenderScript.TRACE_TAG); |
Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 1419 | return new Allocation(id, rs, t, usage); |
| 1420 | } |
| 1421 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 1422 | /** |
Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 1423 | * Creates an Allocation with a specified number of given elements |
Alex Sakhartchouk | 623c54d | 2011-01-12 17:32:36 -0800 | [diff] [blame] | 1424 | * |
Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 1425 | * @param rs Context to which the Allocation will belong. |
| 1426 | * @param e Element to use in the Allocation |
| 1427 | * @param count the number of Elements in the Allocation |
Alex Sakhartchouk | 623c54d | 2011-01-12 17:32:36 -0800 | [diff] [blame] | 1428 | * |
| 1429 | * @return allocation |
| 1430 | */ |
Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 1431 | static public Allocation createSized(RenderScript rs, Element e, int count) { |
Jason Sams | d4b23b5 | 2010-12-13 15:32:35 -0800 | [diff] [blame] | 1432 | return createSized(rs, e, count, USAGE_SCRIPT); |
Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 1433 | } |
| 1434 | |
Jason Sams | 49a05d7 | 2010-12-29 14:31:29 -0800 | [diff] [blame] | 1435 | static Element elementFromBitmap(RenderScript rs, Bitmap b) { |
Jason Sams | 8a64743 | 2010-03-01 15:31:04 -0800 | [diff] [blame] | 1436 | final Bitmap.Config bc = b.getConfig(); |
| 1437 | if (bc == Bitmap.Config.ALPHA_8) { |
| 1438 | return Element.A_8(rs); |
| 1439 | } |
| 1440 | if (bc == Bitmap.Config.ARGB_4444) { |
| 1441 | return Element.RGBA_4444(rs); |
| 1442 | } |
| 1443 | if (bc == Bitmap.Config.ARGB_8888) { |
| 1444 | return Element.RGBA_8888(rs); |
| 1445 | } |
| 1446 | if (bc == Bitmap.Config.RGB_565) { |
| 1447 | return Element.RGB_565(rs); |
| 1448 | } |
Jeff Sharkey | 4bd1a3d | 2010-11-16 13:46:34 -0800 | [diff] [blame] | 1449 | throw new RSInvalidStateException("Bad bitmap type: " + bc); |
Jason Sams | 8a64743 | 2010-03-01 15:31:04 -0800 | [diff] [blame] | 1450 | } |
| 1451 | |
Jason Sams | 49a05d7 | 2010-12-29 14:31:29 -0800 | [diff] [blame] | 1452 | static Type typeFromBitmap(RenderScript rs, Bitmap b, |
Jason Sams | 4ef6650 | 2010-12-10 16:03:15 -0800 | [diff] [blame] | 1453 | MipmapControl mip) { |
Jason Sams | 8a64743 | 2010-03-01 15:31:04 -0800 | [diff] [blame] | 1454 | Element e = elementFromBitmap(rs, b); |
| 1455 | Type.Builder tb = new Type.Builder(rs, e); |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 1456 | tb.setX(b.getWidth()); |
| 1457 | tb.setY(b.getHeight()); |
Jason Sams | 4ef6650 | 2010-12-10 16:03:15 -0800 | [diff] [blame] | 1458 | tb.setMipmaps(mip == MipmapControl.MIPMAP_FULL); |
Jason Sams | 8a64743 | 2010-03-01 15:31:04 -0800 | [diff] [blame] | 1459 | return tb.create(); |
| 1460 | } |
| 1461 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 1462 | /** |
Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 1463 | * Creates an Allocation from a {@link android.graphics.Bitmap}. |
Alex Sakhartchouk | 623c54d | 2011-01-12 17:32:36 -0800 | [diff] [blame] | 1464 | * |
Alex Sakhartchouk | f5c876e | 2011-01-13 14:53:43 -0800 | [diff] [blame] | 1465 | * @param rs Context to which the allocation will belong. |
Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 1466 | * @param b Bitmap source for the allocation data |
Alex Sakhartchouk | 623c54d | 2011-01-12 17:32:36 -0800 | [diff] [blame] | 1467 | * @param mips specifies desired mipmap behaviour for the |
| 1468 | * allocation |
| 1469 | * @param usage bit field specifying how the allocation is |
| 1470 | * utilized |
| 1471 | * |
Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 1472 | * @return Allocation containing bitmap data |
Alex Sakhartchouk | 623c54d | 2011-01-12 17:32:36 -0800 | [diff] [blame] | 1473 | * |
| 1474 | */ |
Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 1475 | static public Allocation createFromBitmap(RenderScript rs, Bitmap b, |
Jason Sams | 4ef6650 | 2010-12-10 16:03:15 -0800 | [diff] [blame] | 1476 | MipmapControl mips, |
Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 1477 | int usage) { |
Tim Murray | 6d7a53c | 2013-05-23 16:59:23 -0700 | [diff] [blame] | 1478 | Trace.traceBegin(RenderScript.TRACE_TAG, "createFromBitmap"); |
Jason Sams | 771bebb | 2009-12-07 12:40:12 -0800 | [diff] [blame] | 1479 | rs.validate(); |
Tim Murray | abd5db9 | 2013-02-28 11:45:22 -0800 | [diff] [blame] | 1480 | |
| 1481 | // WAR undocumented color formats |
| 1482 | if (b.getConfig() == null) { |
| 1483 | if ((usage & USAGE_SHARED) != 0) { |
| 1484 | throw new RSIllegalArgumentException("USAGE_SHARED cannot be used with a Bitmap that has a null config."); |
| 1485 | } |
| 1486 | Bitmap newBitmap = Bitmap.createBitmap(b.getWidth(), b.getHeight(), Bitmap.Config.ARGB_8888); |
| 1487 | Canvas c = new Canvas(newBitmap); |
| 1488 | c.drawBitmap(b, 0, 0, null); |
| 1489 | return createFromBitmap(rs, newBitmap, mips, usage); |
| 1490 | } |
| 1491 | |
Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 1492 | Type t = typeFromBitmap(rs, b, mips); |
Jason Sams | 8a64743 | 2010-03-01 15:31:04 -0800 | [diff] [blame] | 1493 | |
Tim Murray | a314551 | 2012-12-04 17:59:29 -0800 | [diff] [blame] | 1494 | // enable optimized bitmap path only with no mipmap and script-only usage |
| 1495 | if (mips == MipmapControl.MIPMAP_NONE && |
| 1496 | t.getElement().isCompatible(Element.RGBA_8888(rs)) && |
Tim Murray | 78e6494 | 2013-04-09 17:28:56 -0700 | [diff] [blame] | 1497 | usage == (USAGE_SHARED | USAGE_SCRIPT | USAGE_GRAPHICS_TEXTURE)) { |
Tim Murray | a314551 | 2012-12-04 17:59:29 -0800 | [diff] [blame] | 1498 | int id = rs.nAllocationCreateBitmapBackedAllocation(t.getID(rs), mips.mID, b, usage); |
| 1499 | if (id == 0) { |
| 1500 | throw new RSRuntimeException("Load failed."); |
| 1501 | } |
| 1502 | |
| 1503 | // keep a reference to the Bitmap around to prevent GC |
| 1504 | Allocation alloc = new Allocation(id, rs, t, usage); |
| 1505 | alloc.setBitmap(b); |
| 1506 | return alloc; |
| 1507 | } |
| 1508 | |
Jason Sams | 9bf1892 | 2013-04-13 19:48:36 -0700 | [diff] [blame] | 1509 | |
Jason Sams | e07694b | 2012-04-03 15:36:36 -0700 | [diff] [blame] | 1510 | int id = rs.nAllocationCreateFromBitmap(t.getID(rs), mips.mID, b, usage); |
Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 1511 | if (id == 0) { |
Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 1512 | throw new RSRuntimeException("Load failed."); |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 1513 | } |
Tim Murray | 6d7a53c | 2013-05-23 16:59:23 -0700 | [diff] [blame] | 1514 | Trace.traceEnd(RenderScript.TRACE_TAG); |
Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 1515 | return new Allocation(id, rs, t, usage); |
| 1516 | } |
| 1517 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 1518 | /** |
Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 1519 | * Returns the handle to a raw buffer that is being managed by the screen |
| 1520 | * compositor. This operation is only valid for Allocations with {@link |
| 1521 | * #USAGE_IO_INPUT}. |
Jason Sams | fb9aa9f | 2012-03-28 15:30:07 -0700 | [diff] [blame] | 1522 | * |
Alex Sakhartchouk | 918e840 | 2012-04-11 14:04:23 -0700 | [diff] [blame] | 1523 | * @return Surface object associated with allocation |
Jason Sams | fb9aa9f | 2012-03-28 15:30:07 -0700 | [diff] [blame] | 1524 | * |
| 1525 | */ |
| 1526 | public Surface getSurface() { |
Jason Sams | 72226e0 | 2013-02-22 12:45:54 -0800 | [diff] [blame] | 1527 | if ((mUsage & USAGE_IO_INPUT) == 0) { |
| 1528 | throw new RSInvalidStateException("Allocation is not a surface texture."); |
| 1529 | } |
| 1530 | return mRS.nAllocationGetSurface(getID(mRS)); |
Jason Sams | fb9aa9f | 2012-03-28 15:30:07 -0700 | [diff] [blame] | 1531 | } |
| 1532 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 1533 | /** |
Jason Sams | c089c2f | 2013-02-22 13:57:36 -0800 | [diff] [blame] | 1534 | * @hide |
| 1535 | */ |
| 1536 | public void setSurfaceTexture(SurfaceTexture st) { |
| 1537 | setSurface(new Surface(st)); |
| 1538 | } |
| 1539 | |
| 1540 | /** |
Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 1541 | * Associate a {@link android.view.Surface} with this Allocation. This |
| 1542 | * operation is only valid for Allocations with {@link #USAGE_IO_OUTPUT}. |
Alex Sakhartchouk | 918e840 | 2012-04-11 14:04:23 -0700 | [diff] [blame] | 1543 | * |
| 1544 | * @param sur Surface to associate with allocation |
Jason Sams | 163766c | 2012-02-15 12:04:24 -0800 | [diff] [blame] | 1545 | */ |
Jason Sams | fb9aa9f | 2012-03-28 15:30:07 -0700 | [diff] [blame] | 1546 | public void setSurface(Surface sur) { |
| 1547 | mRS.validate(); |
Jason Sams | 163766c | 2012-02-15 12:04:24 -0800 | [diff] [blame] | 1548 | if ((mUsage & USAGE_IO_OUTPUT) == 0) { |
| 1549 | throw new RSInvalidStateException("Allocation is not USAGE_IO_OUTPUT."); |
| 1550 | } |
| 1551 | |
Jason Sams | e07694b | 2012-04-03 15:36:36 -0700 | [diff] [blame] | 1552 | mRS.nAllocationSetSurface(getID(mRS), sur); |
Jason Sams | 163766c | 2012-02-15 12:04:24 -0800 | [diff] [blame] | 1553 | } |
| 1554 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 1555 | /** |
Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 1556 | * Creates an Allocation from a {@link android.graphics.Bitmap}. |
Tim Murray | 00bb454 | 2012-12-17 16:35:06 -0800 | [diff] [blame] | 1557 | * |
Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 1558 | * <p>With target API version 18 or greater, this Allocation will be created |
| 1559 | * with {@link #USAGE_SHARED}, {@link #USAGE_SCRIPT}, and {@link |
| 1560 | * #USAGE_GRAPHICS_TEXTURE}. With target API version 17 or lower, this |
| 1561 | * Allocation will be created with {@link #USAGE_GRAPHICS_TEXTURE}.</p> |
Alex Sakhartchouk | 623c54d | 2011-01-12 17:32:36 -0800 | [diff] [blame] | 1562 | * |
Alex Sakhartchouk | f5c876e | 2011-01-13 14:53:43 -0800 | [diff] [blame] | 1563 | * @param rs Context to which the allocation will belong. |
Alex Sakhartchouk | 623c54d | 2011-01-12 17:32:36 -0800 | [diff] [blame] | 1564 | * @param b bitmap source for the allocation data |
| 1565 | * |
Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 1566 | * @return Allocation containing bitmap data |
Alex Sakhartchouk | 623c54d | 2011-01-12 17:32:36 -0800 | [diff] [blame] | 1567 | * |
| 1568 | */ |
Jason Sams | 6d8eb26 | 2010-12-15 01:41:00 -0800 | [diff] [blame] | 1569 | static public Allocation createFromBitmap(RenderScript rs, Bitmap b) { |
Tim Murray | 00bb454 | 2012-12-17 16:35:06 -0800 | [diff] [blame] | 1570 | if (rs.getApplicationContext().getApplicationInfo().targetSdkVersion >= 18) { |
| 1571 | return createFromBitmap(rs, b, MipmapControl.MIPMAP_NONE, |
Tim Murray | 78e6494 | 2013-04-09 17:28:56 -0700 | [diff] [blame] | 1572 | USAGE_SHARED | USAGE_SCRIPT | USAGE_GRAPHICS_TEXTURE); |
Tim Murray | 00bb454 | 2012-12-17 16:35:06 -0800 | [diff] [blame] | 1573 | } |
Jason Sams | 6d8eb26 | 2010-12-15 01:41:00 -0800 | [diff] [blame] | 1574 | return createFromBitmap(rs, b, MipmapControl.MIPMAP_NONE, |
| 1575 | USAGE_GRAPHICS_TEXTURE); |
Jason Sams | 8a64743 | 2010-03-01 15:31:04 -0800 | [diff] [blame] | 1576 | } |
| 1577 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 1578 | /** |
Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 1579 | * Creates a cubemap Allocation from a {@link android.graphics.Bitmap} |
| 1580 | * containing the horizontal list of cube faces. Each face must be a square, |
| 1581 | * have the same size as all other faces, and have a width that is a power |
| 1582 | * of 2. |
Alex Sakhartchouk | 623c54d | 2011-01-12 17:32:36 -0800 | [diff] [blame] | 1583 | * |
Alex Sakhartchouk | f5c876e | 2011-01-13 14:53:43 -0800 | [diff] [blame] | 1584 | * @param rs Context to which the allocation will belong. |
Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 1585 | * @param b Bitmap with cubemap faces layed out in the following |
Alex Sakhartchouk | 623c54d | 2011-01-12 17:32:36 -0800 | [diff] [blame] | 1586 | * format: right, left, top, bottom, front, back |
| 1587 | * @param mips specifies desired mipmap behaviour for the cubemap |
| 1588 | * @param usage bit field specifying how the cubemap is utilized |
| 1589 | * |
| 1590 | * @return allocation containing cubemap data |
| 1591 | * |
| 1592 | */ |
Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 1593 | static public Allocation createCubemapFromBitmap(RenderScript rs, Bitmap b, |
Jason Sams | 4ef6650 | 2010-12-10 16:03:15 -0800 | [diff] [blame] | 1594 | MipmapControl mips, |
Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 1595 | int usage) { |
Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 1596 | rs.validate(); |
Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 1597 | |
Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 1598 | int height = b.getHeight(); |
| 1599 | int width = b.getWidth(); |
| 1600 | |
Alex Sakhartchouk | fe852e2 | 2011-01-10 15:57:57 -0800 | [diff] [blame] | 1601 | if (width % 6 != 0) { |
Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 1602 | throw new RSIllegalArgumentException("Cubemap height must be multiple of 6"); |
| 1603 | } |
Alex Sakhartchouk | fe852e2 | 2011-01-10 15:57:57 -0800 | [diff] [blame] | 1604 | if (width / 6 != height) { |
Alex Sakhartchouk | dcc2319 | 2011-01-11 14:47:44 -0800 | [diff] [blame] | 1605 | throw new RSIllegalArgumentException("Only square cube map faces supported"); |
Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 1606 | } |
Alex Sakhartchouk | fe852e2 | 2011-01-10 15:57:57 -0800 | [diff] [blame] | 1607 | boolean isPow2 = (height & (height - 1)) == 0; |
Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 1608 | if (!isPow2) { |
| 1609 | throw new RSIllegalArgumentException("Only power of 2 cube faces supported"); |
| 1610 | } |
| 1611 | |
| 1612 | Element e = elementFromBitmap(rs, b); |
| 1613 | Type.Builder tb = new Type.Builder(rs, e); |
Alex Sakhartchouk | fe852e2 | 2011-01-10 15:57:57 -0800 | [diff] [blame] | 1614 | tb.setX(height); |
| 1615 | tb.setY(height); |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 1616 | tb.setFaces(true); |
Jason Sams | 4ef6650 | 2010-12-10 16:03:15 -0800 | [diff] [blame] | 1617 | tb.setMipmaps(mips == MipmapControl.MIPMAP_FULL); |
Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 1618 | Type t = tb.create(); |
| 1619 | |
Jason Sams | e07694b | 2012-04-03 15:36:36 -0700 | [diff] [blame] | 1620 | int id = rs.nAllocationCubeCreateFromBitmap(t.getID(rs), mips.mID, b, usage); |
Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 1621 | if(id == 0) { |
| 1622 | throw new RSRuntimeException("Load failed for bitmap " + b + " element " + e); |
| 1623 | } |
Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 1624 | return new Allocation(id, rs, t, usage); |
Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 1625 | } |
| 1626 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 1627 | /** |
Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 1628 | * Creates a non-mipmapped cubemap Allocation for use as a graphics texture |
| 1629 | * from a {@link android.graphics.Bitmap} containing the horizontal list of |
| 1630 | * cube faces. Each face must be a square, have the same size as all other |
| 1631 | * faces, and have a width that is a power of 2. |
Alex Sakhartchouk | 623c54d | 2011-01-12 17:32:36 -0800 | [diff] [blame] | 1632 | * |
Alex Sakhartchouk | f5c876e | 2011-01-13 14:53:43 -0800 | [diff] [blame] | 1633 | * @param rs Context to which the allocation will belong. |
Alex Sakhartchouk | 623c54d | 2011-01-12 17:32:36 -0800 | [diff] [blame] | 1634 | * @param b bitmap with cubemap faces layed out in the following |
| 1635 | * format: right, left, top, bottom, front, back |
| 1636 | * |
| 1637 | * @return allocation containing cubemap data |
| 1638 | * |
| 1639 | */ |
Alex Sakhartchouk | dcc2319 | 2011-01-11 14:47:44 -0800 | [diff] [blame] | 1640 | static public Allocation createCubemapFromBitmap(RenderScript rs, |
| 1641 | Bitmap b) { |
Jason Sams | 6d8eb26 | 2010-12-15 01:41:00 -0800 | [diff] [blame] | 1642 | return createCubemapFromBitmap(rs, b, MipmapControl.MIPMAP_NONE, |
Alex Sakhartchouk | fe852e2 | 2011-01-10 15:57:57 -0800 | [diff] [blame] | 1643 | USAGE_GRAPHICS_TEXTURE); |
Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 1644 | } |
| 1645 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 1646 | /** |
Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 1647 | * Creates a cubemap Allocation from 6 {@link android.graphics.Bitmap} |
| 1648 | * objects containing the cube faces. Each face must be a square, have the |
| 1649 | * same size as all other faces, and have a width that is a power of 2. |
Alex Sakhartchouk | 623c54d | 2011-01-12 17:32:36 -0800 | [diff] [blame] | 1650 | * |
Alex Sakhartchouk | f5c876e | 2011-01-13 14:53:43 -0800 | [diff] [blame] | 1651 | * @param rs Context to which the allocation will belong. |
Alex Sakhartchouk | 623c54d | 2011-01-12 17:32:36 -0800 | [diff] [blame] | 1652 | * @param xpos cubemap face in the positive x direction |
| 1653 | * @param xneg cubemap face in the negative x direction |
| 1654 | * @param ypos cubemap face in the positive y direction |
| 1655 | * @param yneg cubemap face in the negative y direction |
| 1656 | * @param zpos cubemap face in the positive z direction |
| 1657 | * @param zneg cubemap face in the negative z direction |
| 1658 | * @param mips specifies desired mipmap behaviour for the cubemap |
| 1659 | * @param usage bit field specifying how the cubemap is utilized |
| 1660 | * |
| 1661 | * @return allocation containing cubemap data |
| 1662 | * |
| 1663 | */ |
Alex Sakhartchouk | dcc2319 | 2011-01-11 14:47:44 -0800 | [diff] [blame] | 1664 | static public Allocation createCubemapFromCubeFaces(RenderScript rs, |
| 1665 | Bitmap xpos, |
| 1666 | Bitmap xneg, |
| 1667 | Bitmap ypos, |
| 1668 | Bitmap yneg, |
| 1669 | Bitmap zpos, |
| 1670 | Bitmap zneg, |
| 1671 | MipmapControl mips, |
| 1672 | int usage) { |
| 1673 | int height = xpos.getHeight(); |
| 1674 | if (xpos.getWidth() != height || |
| 1675 | xneg.getWidth() != height || xneg.getHeight() != height || |
| 1676 | ypos.getWidth() != height || ypos.getHeight() != height || |
| 1677 | yneg.getWidth() != height || yneg.getHeight() != height || |
| 1678 | zpos.getWidth() != height || zpos.getHeight() != height || |
| 1679 | zneg.getWidth() != height || zneg.getHeight() != height) { |
| 1680 | throw new RSIllegalArgumentException("Only square cube map faces supported"); |
| 1681 | } |
| 1682 | boolean isPow2 = (height & (height - 1)) == 0; |
| 1683 | if (!isPow2) { |
| 1684 | throw new RSIllegalArgumentException("Only power of 2 cube faces supported"); |
| 1685 | } |
| 1686 | |
| 1687 | Element e = elementFromBitmap(rs, xpos); |
| 1688 | Type.Builder tb = new Type.Builder(rs, e); |
| 1689 | tb.setX(height); |
| 1690 | tb.setY(height); |
| 1691 | tb.setFaces(true); |
| 1692 | tb.setMipmaps(mips == MipmapControl.MIPMAP_FULL); |
| 1693 | Type t = tb.create(); |
| 1694 | Allocation cubemap = Allocation.createTyped(rs, t, mips, usage); |
| 1695 | |
| 1696 | AllocationAdapter adapter = AllocationAdapter.create2D(rs, cubemap); |
Stephen Hines | 20fbd01 | 2011-06-16 17:44:53 -0700 | [diff] [blame] | 1697 | adapter.setFace(Type.CubemapFace.POSITIVE_X); |
Alex Sakhartchouk | dcc2319 | 2011-01-11 14:47:44 -0800 | [diff] [blame] | 1698 | adapter.copyFrom(xpos); |
| 1699 | adapter.setFace(Type.CubemapFace.NEGATIVE_X); |
| 1700 | adapter.copyFrom(xneg); |
Stephen Hines | 20fbd01 | 2011-06-16 17:44:53 -0700 | [diff] [blame] | 1701 | adapter.setFace(Type.CubemapFace.POSITIVE_Y); |
Alex Sakhartchouk | dcc2319 | 2011-01-11 14:47:44 -0800 | [diff] [blame] | 1702 | adapter.copyFrom(ypos); |
| 1703 | adapter.setFace(Type.CubemapFace.NEGATIVE_Y); |
| 1704 | adapter.copyFrom(yneg); |
Stephen Hines | 20fbd01 | 2011-06-16 17:44:53 -0700 | [diff] [blame] | 1705 | adapter.setFace(Type.CubemapFace.POSITIVE_Z); |
Alex Sakhartchouk | dcc2319 | 2011-01-11 14:47:44 -0800 | [diff] [blame] | 1706 | adapter.copyFrom(zpos); |
| 1707 | adapter.setFace(Type.CubemapFace.NEGATIVE_Z); |
| 1708 | adapter.copyFrom(zneg); |
| 1709 | |
| 1710 | return cubemap; |
| 1711 | } |
| 1712 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 1713 | /** |
Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 1714 | * Creates a non-mipmapped cubemap Allocation for use as a sampler input |
| 1715 | * from 6 {@link android.graphics.Bitmap} objects containing the cube |
| 1716 | * faces. Each face must be a square, have the same size as all other faces, |
| 1717 | * and have a width that is a power of 2. |
Alex Sakhartchouk | 623c54d | 2011-01-12 17:32:36 -0800 | [diff] [blame] | 1718 | * |
Alex Sakhartchouk | f5c876e | 2011-01-13 14:53:43 -0800 | [diff] [blame] | 1719 | * @param rs Context to which the allocation will belong. |
Alex Sakhartchouk | 623c54d | 2011-01-12 17:32:36 -0800 | [diff] [blame] | 1720 | * @param xpos cubemap face in the positive x direction |
| 1721 | * @param xneg cubemap face in the negative x direction |
| 1722 | * @param ypos cubemap face in the positive y direction |
| 1723 | * @param yneg cubemap face in the negative y direction |
| 1724 | * @param zpos cubemap face in the positive z direction |
| 1725 | * @param zneg cubemap face in the negative z direction |
| 1726 | * |
| 1727 | * @return allocation containing cubemap data |
| 1728 | * |
| 1729 | */ |
Alex Sakhartchouk | dcc2319 | 2011-01-11 14:47:44 -0800 | [diff] [blame] | 1730 | static public Allocation createCubemapFromCubeFaces(RenderScript rs, |
| 1731 | Bitmap xpos, |
| 1732 | Bitmap xneg, |
| 1733 | Bitmap ypos, |
| 1734 | Bitmap yneg, |
| 1735 | Bitmap zpos, |
| 1736 | Bitmap zneg) { |
| 1737 | return createCubemapFromCubeFaces(rs, xpos, xneg, ypos, yneg, |
| 1738 | zpos, zneg, MipmapControl.MIPMAP_NONE, |
| 1739 | USAGE_GRAPHICS_TEXTURE); |
| 1740 | } |
| 1741 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 1742 | /** |
Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 1743 | * Creates an Allocation from the Bitmap referenced |
| 1744 | * by resource ID. |
Alex Sakhartchouk | 623c54d | 2011-01-12 17:32:36 -0800 | [diff] [blame] | 1745 | * |
Alex Sakhartchouk | f5c876e | 2011-01-13 14:53:43 -0800 | [diff] [blame] | 1746 | * @param rs Context to which the allocation will belong. |
Alex Sakhartchouk | 623c54d | 2011-01-12 17:32:36 -0800 | [diff] [blame] | 1747 | * @param res application resources |
| 1748 | * @param id resource id to load the data from |
| 1749 | * @param mips specifies desired mipmap behaviour for the |
| 1750 | * allocation |
| 1751 | * @param usage bit field specifying how the allocation is |
| 1752 | * utilized |
| 1753 | * |
Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 1754 | * @return Allocation containing resource data |
Alex Sakhartchouk | 623c54d | 2011-01-12 17:32:36 -0800 | [diff] [blame] | 1755 | * |
| 1756 | */ |
Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 1757 | static public Allocation createFromBitmapResource(RenderScript rs, |
| 1758 | Resources res, |
| 1759 | int id, |
Jason Sams | 4ef6650 | 2010-12-10 16:03:15 -0800 | [diff] [blame] | 1760 | MipmapControl mips, |
Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 1761 | int usage) { |
Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 1762 | |
Jason Sams | 771bebb | 2009-12-07 12:40:12 -0800 | [diff] [blame] | 1763 | rs.validate(); |
Jason Sams | 3ece2f3 | 2013-05-31 14:00:46 -0700 | [diff] [blame] | 1764 | if ((usage & (USAGE_SHARED | USAGE_IO_INPUT | USAGE_IO_OUTPUT)) != 0) { |
| 1765 | throw new RSIllegalArgumentException("Unsupported usage specified."); |
| 1766 | } |
Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 1767 | Bitmap b = BitmapFactory.decodeResource(res, id); |
| 1768 | Allocation alloc = createFromBitmap(rs, b, mips, usage); |
| 1769 | b.recycle(); |
| 1770 | return alloc; |
Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 1771 | } |
| 1772 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 1773 | /** |
Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 1774 | * Creates a non-mipmapped Allocation to use as a graphics texture from the |
| 1775 | * {@link android.graphics.Bitmap} referenced by resource ID. |
Alex Sakhartchouk | 623c54d | 2011-01-12 17:32:36 -0800 | [diff] [blame] | 1776 | * |
Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 1777 | * <p>With target API version 18 or greater, this allocation will be created |
| 1778 | * with {@link #USAGE_SCRIPT} and {@link #USAGE_GRAPHICS_TEXTURE}. With |
| 1779 | * target API version 17 or lower, this allocation will be created with |
| 1780 | * {@link #USAGE_GRAPHICS_TEXTURE}.</p> |
Jason Sams | 455d644 | 2013-02-05 19:20:18 -0800 | [diff] [blame] | 1781 | * |
Alex Sakhartchouk | f5c876e | 2011-01-13 14:53:43 -0800 | [diff] [blame] | 1782 | * @param rs Context to which the allocation will belong. |
Alex Sakhartchouk | 623c54d | 2011-01-12 17:32:36 -0800 | [diff] [blame] | 1783 | * @param res application resources |
| 1784 | * @param id resource id to load the data from |
| 1785 | * |
Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 1786 | * @return Allocation containing resource data |
Alex Sakhartchouk | 623c54d | 2011-01-12 17:32:36 -0800 | [diff] [blame] | 1787 | * |
| 1788 | */ |
Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 1789 | static public Allocation createFromBitmapResource(RenderScript rs, |
| 1790 | Resources res, |
Jason Sams | 6d8eb26 | 2010-12-15 01:41:00 -0800 | [diff] [blame] | 1791 | int id) { |
Jason Sams | 455d644 | 2013-02-05 19:20:18 -0800 | [diff] [blame] | 1792 | if (rs.getApplicationContext().getApplicationInfo().targetSdkVersion >= 18) { |
| 1793 | return createFromBitmapResource(rs, res, id, |
| 1794 | MipmapControl.MIPMAP_NONE, |
Jason Sams | 3ece2f3 | 2013-05-31 14:00:46 -0700 | [diff] [blame] | 1795 | USAGE_SCRIPT | USAGE_GRAPHICS_TEXTURE); |
Jason Sams | 455d644 | 2013-02-05 19:20:18 -0800 | [diff] [blame] | 1796 | } |
Jason Sams | 6d8eb26 | 2010-12-15 01:41:00 -0800 | [diff] [blame] | 1797 | return createFromBitmapResource(rs, res, id, |
| 1798 | MipmapControl.MIPMAP_NONE, |
| 1799 | USAGE_GRAPHICS_TEXTURE); |
| 1800 | } |
| 1801 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 1802 | /** |
Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 1803 | * Creates an Allocation containing string data encoded in UTF-8 format. |
Alex Sakhartchouk | 623c54d | 2011-01-12 17:32:36 -0800 | [diff] [blame] | 1804 | * |
Alex Sakhartchouk | f5c876e | 2011-01-13 14:53:43 -0800 | [diff] [blame] | 1805 | * @param rs Context to which the allocation will belong. |
Alex Sakhartchouk | 623c54d | 2011-01-12 17:32:36 -0800 | [diff] [blame] | 1806 | * @param str string to create the allocation from |
| 1807 | * @param usage bit field specifying how the allocaiton is |
| 1808 | * utilized |
| 1809 | * |
| 1810 | */ |
Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 1811 | static public Allocation createFromString(RenderScript rs, |
| 1812 | String str, |
| 1813 | int usage) { |
| 1814 | rs.validate(); |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 1815 | byte[] allocArray = null; |
| 1816 | try { |
| 1817 | allocArray = str.getBytes("UTF-8"); |
Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 1818 | Allocation alloc = Allocation.createSized(rs, Element.U8(rs), allocArray.length, usage); |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 1819 | alloc.copyFrom(allocArray); |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 1820 | return alloc; |
| 1821 | } |
| 1822 | catch (Exception e) { |
Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 1823 | throw new RSRuntimeException("Could not convert string to utf-8."); |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 1824 | } |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 1825 | } |
Jason Sams | 739c826 | 2013-04-11 18:07:52 -0700 | [diff] [blame] | 1826 | |
| 1827 | /** |
Jason Sams | f64cca9 | 2013-04-19 12:56:37 -0700 | [diff] [blame] | 1828 | * @hide |
| 1829 | * |
Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 1830 | * Interface to handle notification when new buffers are available via |
| 1831 | * {@link #USAGE_IO_INPUT}. An application will receive one notification |
| 1832 | * when a buffer is available. Additional buffers will not trigger new |
| 1833 | * notifications until a buffer is processed. |
Jason Sams | 739c826 | 2013-04-11 18:07:52 -0700 | [diff] [blame] | 1834 | */ |
| 1835 | public interface IoInputNotifier { |
| 1836 | public void onBufferAvailable(Allocation a); |
| 1837 | } |
| 1838 | |
| 1839 | /** |
Jason Sams | f64cca9 | 2013-04-19 12:56:37 -0700 | [diff] [blame] | 1840 | * @hide |
| 1841 | * |
Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 1842 | * Set a notification handler for {@link #USAGE_IO_INPUT}. |
Jason Sams | 739c826 | 2013-04-11 18:07:52 -0700 | [diff] [blame] | 1843 | * |
Jason Sams | c876cc4 | 2013-04-11 20:22:31 -0700 | [diff] [blame] | 1844 | * @param callback instance of the IoInputNotifier class to be called |
Jason Sams | 739c826 | 2013-04-11 18:07:52 -0700 | [diff] [blame] | 1845 | * when buffer arrive. |
| 1846 | */ |
| 1847 | public void setIoInputNotificationHandler(IoInputNotifier callback) { |
| 1848 | synchronized(mAllocationMap) { |
| 1849 | mAllocationMap.put(new Integer(getID(mRS)), this); |
| 1850 | mBufferNotifier = callback; |
| 1851 | } |
| 1852 | } |
| 1853 | |
| 1854 | static void sendBufferNotification(int id) { |
| 1855 | synchronized(mAllocationMap) { |
| 1856 | Allocation a = mAllocationMap.get(new Integer(id)); |
| 1857 | |
| 1858 | if ((a != null) && (a.mBufferNotifier != null)) { |
| 1859 | a.mBufferNotifier.onBufferAvailable(a); |
| 1860 | } |
| 1861 | } |
| 1862 | } |
| 1863 | |
Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 1864 | } |
| 1865 | |