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