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