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