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