Allocation.java revision d176abf6992ea9b34a01ba1e8b232ac4ac08db31
145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams/*
28fafeb11cf19fdada0add886e99ab0af5d3ddabeJason Sams * Copyright (C) 2008-2012 The Android Open Source Project
345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams *
445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams * Licensed under the Apache License, Version 2.0 (the "License");
545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams * you may not use this file except in compliance with the License.
645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams * You may obtain a copy of the License at
745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams *
845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams *      http://www.apache.org/licenses/LICENSE-2.0
945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams *
1045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams * Unless required by applicable law or agreed to in writing, software
1145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams * distributed under the License is distributed on an "AS IS" BASIS,
1245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams * See the License for the specific language governing permissions and
1445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams * limitations under the License.
1545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams */
1645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
1745d443665f5ce7efa934706a89883f0cc87f3513Jason Samspackage android.support.v8.renderscript;
1845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
1945d443665f5ce7efa934706a89883f0cc87f3513Jason Samsimport java.io.IOException;
2045d443665f5ce7efa934706a89883f0cc87f3513Jason Samsimport java.io.InputStream;
2145d443665f5ce7efa934706a89883f0cc87f3513Jason Samsimport android.content.res.Resources;
2245d443665f5ce7efa934706a89883f0cc87f3513Jason Samsimport android.content.res.AssetManager;
2345d443665f5ce7efa934706a89883f0cc87f3513Jason Samsimport android.graphics.Bitmap;
2445d443665f5ce7efa934706a89883f0cc87f3513Jason Samsimport android.graphics.BitmapFactory;
25c806aeedb20cb1a725092657ce3358eb4008222bTim Murrayimport android.graphics.Canvas;
269c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murrayimport android.view.Surface;
2745d443665f5ce7efa934706a89883f0cc87f3513Jason Samsimport android.util.Log;
2845d443665f5ce7efa934706a89883f0cc87f3513Jason Samsimport android.util.TypedValue;
2945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
3045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams/**
31032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines * <p> This class provides the primary method through which data is passed to
32032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines * and from RenderScript kernels.  An Allocation provides the backing store for
332192d04dde96e02b9c908d3cc846c8c881ff54a1Stephen Hines * a given {@link android.support.v8.renderscript.Type}.  </p>
3445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams *
35032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines * <p>An Allocation also contains a set of usage flags that denote how the
36032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines * Allocation could be used. For example, an Allocation may have usage flags
37032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines * specifying that it can be used from a script as well as input to a {@link
382192d04dde96e02b9c908d3cc846c8c881ff54a1Stephen Hines * android.support.v8.renderscript.Sampler}. A developer must synchronize
392192d04dde96e02b9c908d3cc846c8c881ff54a1Stephen Hines * across these different usages using
402192d04dde96e02b9c908d3cc846c8c881ff54a1Stephen Hines * {@link android.support.v8.renderscript.Allocation#syncAll} in
41032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines * order to ensure that different users of the Allocation have a consistent view
42032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines * of memory. For example, in the case where an Allocation is used as the output
43032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines * of one kernel and as Sampler input in a later kernel, a developer must call
44032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines * {@link #syncAll syncAll(Allocation.USAGE_SCRIPT)} prior to launching the
45032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines * second kernel to ensure correctness.
4645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams *
47032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines * <p>An Allocation can be populated with the {@link #copyFrom} routines. For
48032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines * more complex Element types, the {@link #copyFromUnchecked} methods can be
49032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines * used to copy from byte arrays or similar constructs.</p>
5045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams *
5145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams * <div class="special reference">
5245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams * <h3>Developer Guides</h3>
532192d04dde96e02b9c908d3cc846c8c881ff54a1Stephen Hines * <p>For more information about creating an application that uses
542192d04dde96e02b9c908d3cc846c8c881ff54a1Stephen Hines * RenderScript, read the
552192d04dde96e02b9c908d3cc846c8c881ff54a1Stephen Hines * <a href="{@docRoot}guide/topics/renderscript/index.html">RenderScript</a>
562192d04dde96e02b9c908d3cc846c8c881ff54a1Stephen Hines * developer guide.</p>
5745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams * </div>
5845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams **/
5945d443665f5ce7efa934706a89883f0cc87f3513Jason Samspublic class Allocation extends BaseObj {
6045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    Type mType;
6145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    Bitmap mBitmap;
6245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    int mUsage;
6345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    Allocation mAdaptedAllocation;
64d176abf6992ea9b34a01ba1e8b232ac4ac08db31Tim Murray    int mSize;
6545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
6645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    boolean mConstrainedLOD;
6745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    boolean mConstrainedFace;
6845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    boolean mConstrainedY;
6945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    boolean mConstrainedZ;
7045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    boolean mReadAllowed = true;
7145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    boolean mWriteAllowed = true;
7245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    int mSelectedY;
7345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    int mSelectedZ;
7445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    int mSelectedLOD;
7545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    Type.CubemapFace mSelectedFace = Type.CubemapFace.POSITIVE_X;
7645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
7745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    int mCurrentDimX;
7845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    int mCurrentDimY;
7945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    int mCurrentDimZ;
8045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    int mCurrentCount;
8145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
8245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    /**
83032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * The usage of the Allocation.  These signal to RenderScript where to place
84032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * the Allocation in memory.
8545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
86032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     */
87032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines
88032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines    /**
89032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * The Allocation will be bound to and accessed by scripts.
9045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     */
9145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    public static final int USAGE_SCRIPT = 0x0001;
9245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
9345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    /**
94032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * The Allocation will be used as a texture source by one or more graphics
95032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * programs.
9645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
9745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     */
9845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    public static final int USAGE_GRAPHICS_TEXTURE = 0x0002;
9945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
10045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    /**
101032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * The Allocation will be used as a {@link android.graphics.SurfaceTexture}
102032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * consumer.  This usage will cause the Allocation to be created as
103032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * read-only.
1049c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray     *
1059c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray     */
1069c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray    public static final int USAGE_IO_INPUT = 0x0020;
1079c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray
1089c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray    /**
109032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * The Allocation will be used as a {@link android.graphics.SurfaceTexture}
110032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * producer.  The dimensions and format of the {@link
111032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * android.graphics.SurfaceTexture} will be forced to those of the
112032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * Allocation.
1139c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray     *
1149c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray     */
1159c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray    public static final int USAGE_IO_OUTPUT = 0x0040;
1169c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray
1179c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray    /**
118032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * The Allocation's backing store will be inherited from another object
119032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * (usually a {@link android.graphics.Bitmap}); copying to or from the
120032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * original source Bitmap will cause a synchronization rather than a full
121032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * copy.  {@link #syncAll} may also be used to synchronize the Allocation
122032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * and the source Bitmap.
1239c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray     *
124032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * <p>This is set by default for allocations created with {@link
125032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * #createFromBitmap} in API version 18 and higher.</p>
1269c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray     *
1279c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray     */
1289c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray    public static final int USAGE_SHARED = 0x0080;
1299c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray
1309c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray    /**
131032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * Controls mipmap behavior when using the bitmap creation and update
132032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * functions.
13345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     */
13445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    public enum MipmapControl {
13545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        /**
136032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines         * No mipmaps will be generated and the type generated from the incoming
137032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines         * bitmap will not contain additional LODs.
13845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams         */
13945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        MIPMAP_NONE(0),
14045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
14145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        /**
142032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines         * A full mipmap chain will be created in script memory.  The Type of
143032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines         * the Allocation will contain a full mipmap chain.  On upload, the full
144032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines         * chain will be transferred.
14545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams         */
14645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        MIPMAP_FULL(1),
14745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
14845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        /**
149032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines         * The Type of the Allocation will be the same as MIPMAP_NONE.  It will
150032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines         * not contain mipmaps.  On upload, the allocation data will contain a
151032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines         * full mipmap chain generated from the top level in script memory.
15245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams         */
15345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        MIPMAP_ON_SYNC_TO_TEXTURE(2);
15445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
15545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        int mID;
15645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        MipmapControl(int id) {
15745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            mID = id;
15845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        }
15945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
16045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
16145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
16245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    private int getIDSafe() {
16345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        if (mAdaptedAllocation != null) {
16445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            return mAdaptedAllocation.getID(mRS);
16545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        }
16645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        return getID(mRS);
16745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
16845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
16945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
17045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams   /**
1712192d04dde96e02b9c908d3cc846c8c881ff54a1Stephen Hines     * Get the {@link android.support.v8.renderscript.Element} of the {@link
1722192d04dde96e02b9c908d3cc846c8c881ff54a1Stephen Hines     * android.support.v8.renderscript.Type} of the Allocation.
17345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
174032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @return Element
17545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
17645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     */
17745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    public Element getElement() {
17845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        return mType.getElement();
17945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
18045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
18145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    /**
18245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * Get the usage flags of the Allocation.
18345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
184032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @return usage this Allocation's set of the USAGE_* flags OR'd together
18545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
18645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     */
18745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    public int getUsage() {
18845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        return mUsage;
18945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
19045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
19145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    /**
19245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * Get the size of the Allocation in bytes.
19345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
19445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @return size of the Allocation in bytes.
19545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
19645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     */
19745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    public int getBytesSize() {
19845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        return mType.getCount() * mType.getElement().getBytesSize();
19945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
20045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
20145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    private void updateCacheInfo(Type t) {
20245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        mCurrentDimX = t.getX();
20345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        mCurrentDimY = t.getY();
20445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        mCurrentDimZ = t.getZ();
20545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        mCurrentCount = mCurrentDimX;
20645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        if (mCurrentDimY > 1) {
20745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            mCurrentCount *= mCurrentDimY;
20845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        }
20945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        if (mCurrentDimZ > 1) {
21045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            mCurrentCount *= mCurrentDimZ;
21145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        }
21245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
21345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
2148fafeb11cf19fdada0add886e99ab0af5d3ddabeJason Sams    private void setBitmap(Bitmap b) {
2158fafeb11cf19fdada0add886e99ab0af5d3ddabeJason Sams        mBitmap = b;
2168fafeb11cf19fdada0add886e99ab0af5d3ddabeJason Sams    }
2178fafeb11cf19fdada0add886e99ab0af5d3ddabeJason Sams
21845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    Allocation(int id, RenderScript rs, Type t, int usage) {
21945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        super(id, rs);
2209c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray        if ((usage & ~(USAGE_SCRIPT |
2219c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray                       USAGE_GRAPHICS_TEXTURE |
2229c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray                       USAGE_IO_INPUT |
2239c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray                       USAGE_IO_OUTPUT |
2249c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray                       USAGE_SHARED)) != 0) {
22545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            throw new RSIllegalArgumentException("Unknown usage specified.");
22645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        }
22745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
2289c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray        if ((usage & USAGE_IO_INPUT) != 0) {
2299c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray            mWriteAllowed = false;
2309c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray
2319c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray            if ((usage & ~(USAGE_IO_INPUT |
2329c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray                           USAGE_GRAPHICS_TEXTURE |
2339c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray                           USAGE_SCRIPT)) != 0) {
2349c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray                throw new RSIllegalArgumentException("Invalid usage combination.");
2359c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray            }
2369c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray        }
2379c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray
23845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        mType = t;
23945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        mUsage = usage;
240d176abf6992ea9b34a01ba1e8b232ac4ac08db31Tim Murray        mSize = mType.getCount() * mType.getElement().getBytesSize();
24145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
24245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        if (t != null) {
24345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            updateCacheInfo(t);
24445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        }
245d176abf6992ea9b34a01ba1e8b232ac4ac08db31Tim Murray        if (RenderScript.sUseGCHooks == true) {
246d176abf6992ea9b34a01ba1e8b232ac4ac08db31Tim Murray            try {
247d176abf6992ea9b34a01ba1e8b232ac4ac08db31Tim Murray                RenderScript.registerNativeAllocation.invoke(RenderScript.sRuntime, mSize);
248d176abf6992ea9b34a01ba1e8b232ac4ac08db31Tim Murray            } catch (Exception e) {
249d176abf6992ea9b34a01ba1e8b232ac4ac08db31Tim Murray                Log.e(RenderScript.LOG_TAG, "Couldn't invoke registerNativeAllocation:" + e);
250d176abf6992ea9b34a01ba1e8b232ac4ac08db31Tim Murray                throw new RSRuntimeException("Couldn't invoke registerNativeAllocation:" + e);
251d176abf6992ea9b34a01ba1e8b232ac4ac08db31Tim Murray            }
252d176abf6992ea9b34a01ba1e8b232ac4ac08db31Tim Murray        }
253d176abf6992ea9b34a01ba1e8b232ac4ac08db31Tim Murray    }
254d176abf6992ea9b34a01ba1e8b232ac4ac08db31Tim Murray
255d176abf6992ea9b34a01ba1e8b232ac4ac08db31Tim Murray    protected void finalize() throws Throwable {
256d176abf6992ea9b34a01ba1e8b232ac4ac08db31Tim Murray        if (RenderScript.sUseGCHooks == true) {
257d176abf6992ea9b34a01ba1e8b232ac4ac08db31Tim Murray            RenderScript.registerNativeFree.invoke(RenderScript.sRuntime, mSize);
258d176abf6992ea9b34a01ba1e8b232ac4ac08db31Tim Murray        }
259d176abf6992ea9b34a01ba1e8b232ac4ac08db31Tim Murray        super.finalize();
26045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
26145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
262d176abf6992ea9b34a01ba1e8b232ac4ac08db31Tim Murray
26345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    private void validateIsInt32() {
26445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        if ((mType.mElement.mType == Element.DataType.SIGNED_32) ||
26545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            (mType.mElement.mType == Element.DataType.UNSIGNED_32)) {
26645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            return;
26745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        }
26845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        throw new RSIllegalArgumentException(
26945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            "32 bit integer source does not match allocation type " + mType.mElement.mType);
27045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
27145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
27245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    private void validateIsInt16() {
27345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        if ((mType.mElement.mType == Element.DataType.SIGNED_16) ||
27445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            (mType.mElement.mType == Element.DataType.UNSIGNED_16)) {
27545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            return;
27645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        }
27745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        throw new RSIllegalArgumentException(
27845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            "16 bit integer source does not match allocation type " + mType.mElement.mType);
27945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
28045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
28145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    private void validateIsInt8() {
28245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        if ((mType.mElement.mType == Element.DataType.SIGNED_8) ||
28345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            (mType.mElement.mType == Element.DataType.UNSIGNED_8)) {
28445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            return;
28545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        }
28645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        throw new RSIllegalArgumentException(
28745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            "8 bit integer source does not match allocation type " + mType.mElement.mType);
28845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
28945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
29045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    private void validateIsFloat32() {
29145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        if (mType.mElement.mType == Element.DataType.FLOAT_32) {
29245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            return;
29345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        }
29445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        throw new RSIllegalArgumentException(
29545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            "32 bit float source does not match allocation type " + mType.mElement.mType);
29645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
29745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
29845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    private void validateIsObject() {
29945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        if ((mType.mElement.mType == Element.DataType.RS_ELEMENT) ||
30045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            (mType.mElement.mType == Element.DataType.RS_TYPE) ||
30145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            (mType.mElement.mType == Element.DataType.RS_ALLOCATION) ||
30245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            (mType.mElement.mType == Element.DataType.RS_SAMPLER) ||
30345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            (mType.mElement.mType == Element.DataType.RS_SCRIPT)) {
30445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            return;
30545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        }
30645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        throw new RSIllegalArgumentException(
30745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            "Object source does not match allocation type " + mType.mElement.mType);
30845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
30945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
31045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    /**
3112192d04dde96e02b9c908d3cc846c8c881ff54a1Stephen Hines     * Get the {@link android.support.v8.renderscript.Type} of the Allocation.
31245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
31345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @return Type
31445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
31545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     */
31645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    public Type getType() {
31745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        return mType;
31845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
31945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
32045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    /**
321032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * Propagate changes from one usage of the Allocation to the
322032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * other usages of the Allocation.
32345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
32445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     */
32545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    public void syncAll(int srcLocation) {
32645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        switch (srcLocation) {
32745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        case USAGE_SCRIPT:
32845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        case USAGE_GRAPHICS_TEXTURE:
32945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            break;
33045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        default:
33145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            throw new RSIllegalArgumentException("Source must be exactly one usage type.");
33245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        }
33345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        mRS.validate();
33445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        mRS.nAllocationSyncAll(getIDSafe(), srcLocation);
33545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
33645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
3379c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray    /**
338032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * Send a buffer to the output stream.  The contents of the Allocation will
339032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * be undefined after this operation. This operation is only valid if {@link
340032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * #USAGE_IO_OUTPUT} is set on the Allocation.
341032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     *
3429c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray     *
3439c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray     */
3449c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray    public void ioSend() {
3459c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray        if ((mUsage & USAGE_IO_OUTPUT) == 0) {
3469c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray            throw new RSIllegalArgumentException(
3479c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray                "Can only send buffer if IO_OUTPUT usage specified.");
3489c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray        }
3499c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray        mRS.validate();
3509c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray        mRS.nAllocationIoSend(getID(mRS));
3519c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray    }
3529c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray
3539c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray    /**
3549c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray     * Delete once code is updated.
3559c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray     * @hide
3569c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray     */
3579c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray    public void ioSendOutput() {
3589c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray        ioSend();
3599c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray    }
3609c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray
3619c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray    /**
362032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * Receive the latest input into the Allocation. This operation
363032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * is only valid if {@link #USAGE_IO_INPUT} is set on the Allocation.
3649c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray     *
3659c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray     */
3669c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray    public void ioReceive() {
3679c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray        if ((mUsage & USAGE_IO_INPUT) == 0) {
3689c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray            throw new RSIllegalArgumentException(
3699c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray                "Can only receive if IO_INPUT usage specified.");
3709c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray        }
3719c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray        mRS.validate();
3729c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray        mRS.nAllocationIoReceive(getID(mRS));
3739c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray    }
37445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
37545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    /**
376032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * Copy an array of RS objects to the Allocation.
37745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
37845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param d Source array.
37945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     */
38045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    public void copyFrom(BaseObj[] d) {
38145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        mRS.validate();
38245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        validateIsObject();
38345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        if (d.length != mCurrentCount) {
38445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            throw new RSIllegalArgumentException("Array size mismatch, allocation sizeX = " +
38545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                                                 mCurrentCount + ", array length = " + d.length);
38645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        }
38745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        int i[] = new int[d.length];
38845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        for (int ct=0; ct < d.length; ct++) {
38945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            i[ct] = d[ct].getID(mRS);
39045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        }
39145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        copy1DRangeFromUnchecked(0, mCurrentCount, i);
39245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
39345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
39445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    private void validateBitmapFormat(Bitmap b) {
39545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        Bitmap.Config bc = b.getConfig();
396c806aeedb20cb1a725092657ce3358eb4008222bTim Murray        if (bc == null) {
397c806aeedb20cb1a725092657ce3358eb4008222bTim Murray            throw new RSIllegalArgumentException("Bitmap has an unsupported format for this operation");
398c806aeedb20cb1a725092657ce3358eb4008222bTim Murray        }
39945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        switch (bc) {
40045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        case ALPHA_8:
40145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            if (mType.getElement().mKind != Element.DataKind.PIXEL_A) {
40245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                throw new RSIllegalArgumentException("Allocation kind is " +
40345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                                                     mType.getElement().mKind + ", type " +
40445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                                                     mType.getElement().mType +
40545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                                                     " of " + mType.getElement().getBytesSize() +
40645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                                                     " bytes, passed bitmap was " + bc);
40745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            }
40845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            break;
40945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        case ARGB_8888:
41045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            if ((mType.getElement().mKind != Element.DataKind.PIXEL_RGBA) ||
41145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                (mType.getElement().getBytesSize() != 4)) {
41245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                throw new RSIllegalArgumentException("Allocation kind is " +
41345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                                                     mType.getElement().mKind + ", type " +
41445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                                                     mType.getElement().mType +
41545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                                                     " of " + mType.getElement().getBytesSize() +
41645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                                                     " bytes, passed bitmap was " + bc);
41745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            }
41845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            break;
41945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        case RGB_565:
42045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            if ((mType.getElement().mKind != Element.DataKind.PIXEL_RGB) ||
42145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                (mType.getElement().getBytesSize() != 2)) {
42245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                throw new RSIllegalArgumentException("Allocation kind is " +
42345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                                                     mType.getElement().mKind + ", type " +
42445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                                                     mType.getElement().mType +
42545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                                                     " of " + mType.getElement().getBytesSize() +
42645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                                                     " bytes, passed bitmap was " + bc);
42745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            }
42845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            break;
42945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        case ARGB_4444:
43045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            if ((mType.getElement().mKind != Element.DataKind.PIXEL_RGBA) ||
43145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                (mType.getElement().getBytesSize() != 2)) {
43245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                throw new RSIllegalArgumentException("Allocation kind is " +
43345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                                                     mType.getElement().mKind + ", type " +
43445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                                                     mType.getElement().mType +
43545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                                                     " of " + mType.getElement().getBytesSize() +
43645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                                                     " bytes, passed bitmap was " + bc);
43745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            }
43845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            break;
439302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams
44045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        }
44145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
44245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
44345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    private void validateBitmapSize(Bitmap b) {
44445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        if((mCurrentDimX != b.getWidth()) || (mCurrentDimY != b.getHeight())) {
44545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            throw new RSIllegalArgumentException("Cannot update allocation from bitmap, sizes mismatch");
44645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        }
44745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
44845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
44945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    /**
450032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * Copy into this Allocation from an array. This method does not guarantee
451032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * that the Allocation is compatible with the input buffer; it copies memory
452032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * without reinterpretation.
45345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
45445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param d the source data array
45545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     */
45645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    public void copyFromUnchecked(int[] d) {
45745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        mRS.validate();
458302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams        if (mCurrentDimZ > 0) {
459302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams            copy3DRangeFromUnchecked(0, 0, 0, mCurrentDimX, mCurrentDimY, mCurrentDimZ, d);
460302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams        } else if (mCurrentDimY > 0) {
461dfe51945d97812921083af3693deaa4bde6ab11fStephen Hines            copy2DRangeFromUnchecked(0, 0, mCurrentDimX, mCurrentDimY, d);
462dfe51945d97812921083af3693deaa4bde6ab11fStephen Hines        } else {
463dfe51945d97812921083af3693deaa4bde6ab11fStephen Hines            copy1DRangeFromUnchecked(0, mCurrentCount, d);
464dfe51945d97812921083af3693deaa4bde6ab11fStephen Hines        }
46545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
46645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    /**
467032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * Copy into this Allocation from an array. This method does not guarantee
468032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * that the Allocation is compatible with the input buffer; it copies memory
469032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * without reinterpretation.
47045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
47145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param d the source data array
47245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     */
47345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    public void copyFromUnchecked(short[] d) {
47445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        mRS.validate();
475302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams        if (mCurrentDimZ > 0) {
476302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams            copy3DRangeFromUnchecked(0, 0, 0, mCurrentDimX, mCurrentDimY, mCurrentDimZ, d);
477302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams        } else if (mCurrentDimY > 0) {
478dfe51945d97812921083af3693deaa4bde6ab11fStephen Hines            copy2DRangeFromUnchecked(0, 0, mCurrentDimX, mCurrentDimY, d);
479dfe51945d97812921083af3693deaa4bde6ab11fStephen Hines        } else {
480dfe51945d97812921083af3693deaa4bde6ab11fStephen Hines            copy1DRangeFromUnchecked(0, mCurrentCount, d);
481dfe51945d97812921083af3693deaa4bde6ab11fStephen Hines        }
48245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
48345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    /**
484032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * Copy into this Allocation from an array. This method does not guarantee
485032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * that the Allocation is compatible with the input buffer; it copies memory
486032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * without reinterpretation.
48745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
48845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param d the source data array
48945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     */
49045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    public void copyFromUnchecked(byte[] d) {
49145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        mRS.validate();
492302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams        if (mCurrentDimZ > 0) {
493302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams            copy3DRangeFromUnchecked(0, 0, 0, mCurrentDimX, mCurrentDimY, mCurrentDimZ, d);
494302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams        } else if (mCurrentDimY > 0) {
495dfe51945d97812921083af3693deaa4bde6ab11fStephen Hines            copy2DRangeFromUnchecked(0, 0, mCurrentDimX, mCurrentDimY, d);
496dfe51945d97812921083af3693deaa4bde6ab11fStephen Hines        } else {
497dfe51945d97812921083af3693deaa4bde6ab11fStephen Hines            copy1DRangeFromUnchecked(0, mCurrentCount, d);
498dfe51945d97812921083af3693deaa4bde6ab11fStephen Hines        }
49945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
50045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    /**
501032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * Copy into this Allocation from an array. This method does not guarantee
502032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * that the Allocation is compatible with the input buffer; it copies memory
503032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * without reinterpretation.
50445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
50545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param d the source data array
50645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     */
50745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    public void copyFromUnchecked(float[] d) {
50845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        mRS.validate();
509302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams        if (mCurrentDimZ > 0) {
510302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams            copy3DRangeFromUnchecked(0, 0, 0, mCurrentDimX, mCurrentDimY, mCurrentDimZ, d);
511302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams        } else if (mCurrentDimY > 0) {
512dfe51945d97812921083af3693deaa4bde6ab11fStephen Hines            copy2DRangeFromUnchecked(0, 0, mCurrentDimX, mCurrentDimY, d);
513dfe51945d97812921083af3693deaa4bde6ab11fStephen Hines        } else {
514dfe51945d97812921083af3693deaa4bde6ab11fStephen Hines            copy1DRangeFromUnchecked(0, mCurrentCount, d);
515dfe51945d97812921083af3693deaa4bde6ab11fStephen Hines        }
51645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
51745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
51845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    /**
519032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * Copy into this Allocation from an array.  This variant is type checked
520032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * and will generate exceptions if the Allocation's {@link
5212192d04dde96e02b9c908d3cc846c8c881ff54a1Stephen Hines     * android.support.v8.renderscript.Element} is not a 32 bit integer type.
52245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
52345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param d the source data array
52445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     */
52545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    public void copyFrom(int[] d) {
52645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        mRS.validate();
527302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams        if (mCurrentDimZ > 0) {
528302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams            copy3DRangeFrom(0, 0, 0, mCurrentDimX, mCurrentDimY, mCurrentDimZ, d);
529302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams        } else if (mCurrentDimY > 0) {
530dfe51945d97812921083af3693deaa4bde6ab11fStephen Hines            copy2DRangeFrom(0, 0, mCurrentDimX, mCurrentDimY, d);
531dfe51945d97812921083af3693deaa4bde6ab11fStephen Hines        } else {
532dfe51945d97812921083af3693deaa4bde6ab11fStephen Hines            copy1DRangeFrom(0, mCurrentCount, d);
533dfe51945d97812921083af3693deaa4bde6ab11fStephen Hines        }
53445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
53545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
53645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    /**
537032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * Copy into this Allocation from an array.  This variant is type checked
538032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * and will generate exceptions if the Allocation's {@link
5392192d04dde96e02b9c908d3cc846c8c881ff54a1Stephen Hines     * android.support.v8.renderscript.Element} is not a 16 bit integer type.
54045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
54145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param d the source data array
54245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     */
54345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    public void copyFrom(short[] d) {
54445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        mRS.validate();
545302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams        if (mCurrentDimZ > 0) {
546302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams            copy3DRangeFrom(0, 0, 0, mCurrentDimX, mCurrentDimY, mCurrentDimZ, d);
547302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams        } else if (mCurrentDimY > 0) {
548dfe51945d97812921083af3693deaa4bde6ab11fStephen Hines            copy2DRangeFrom(0, 0, mCurrentDimX, mCurrentDimY, d);
549dfe51945d97812921083af3693deaa4bde6ab11fStephen Hines        } else {
550dfe51945d97812921083af3693deaa4bde6ab11fStephen Hines            copy1DRangeFrom(0, mCurrentCount, d);
551dfe51945d97812921083af3693deaa4bde6ab11fStephen Hines        }
55245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
55345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
55445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    /**
555032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * Copy into this Allocation from an array.  This variant is type checked
556032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * and will generate exceptions if the Allocation's {@link
5572192d04dde96e02b9c908d3cc846c8c881ff54a1Stephen Hines     * android.support.v8.renderscript.Element} is not an 8 bit integer type.
55845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
55945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param d the source data array
56045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     */
56145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    public void copyFrom(byte[] d) {
56245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        mRS.validate();
563302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams        if (mCurrentDimZ > 0) {
564302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams            copy3DRangeFrom(0, 0, 0, mCurrentDimX, mCurrentDimY, mCurrentDimZ, d);
565302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams        } else if (mCurrentDimY > 0) {
566dfe51945d97812921083af3693deaa4bde6ab11fStephen Hines            copy2DRangeFrom(0, 0, mCurrentDimX, mCurrentDimY, d);
567dfe51945d97812921083af3693deaa4bde6ab11fStephen Hines        } else {
568dfe51945d97812921083af3693deaa4bde6ab11fStephen Hines            copy1DRangeFrom(0, mCurrentCount, d);
569dfe51945d97812921083af3693deaa4bde6ab11fStephen Hines        }
57045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
57145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
57245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    /**
573032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * Copy into this Allocation from an array.  This variant is type checked
574032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * and will generate exceptions if the Allocation's {@link
5752192d04dde96e02b9c908d3cc846c8c881ff54a1Stephen Hines     * android.support.v8.renderscript.Element} is not a 32 bit float type.
57645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
57745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param d the source data array
57845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     */
57945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    public void copyFrom(float[] d) {
58045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        mRS.validate();
581302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams        if (mCurrentDimZ > 0) {
582302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams            copy3DRangeFrom(0, 0, 0, mCurrentDimX, mCurrentDimY, mCurrentDimZ, d);
583302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams        } else if (mCurrentDimY > 0) {
584dfe51945d97812921083af3693deaa4bde6ab11fStephen Hines            copy2DRangeFrom(0, 0, mCurrentDimX, mCurrentDimY, d);
585dfe51945d97812921083af3693deaa4bde6ab11fStephen Hines        } else {
586dfe51945d97812921083af3693deaa4bde6ab11fStephen Hines            copy1DRangeFrom(0, mCurrentCount, d);
587dfe51945d97812921083af3693deaa4bde6ab11fStephen Hines        }
58845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
58945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
59045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    /**
591032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * Copy into an Allocation from a {@link android.graphics.Bitmap}.  The
592032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * height, width, and format of the bitmap must match the existing
593032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * allocation.
594032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     *
595032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * <p>If the {@link android.graphics.Bitmap} is the same as the {@link
596032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * android.graphics.Bitmap} used to create the Allocation with {@link
597032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * #createFromBitmap} and {@link #USAGE_SHARED} is set on the Allocation,
598032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * this will synchronize the Allocation with the latest data from the {@link
599032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * android.graphics.Bitmap}, potentially avoiding the actual copy.</p>
60045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
60145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param b the source bitmap
60245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     */
60345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    public void copyFrom(Bitmap b) {
60445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        mRS.validate();
605c806aeedb20cb1a725092657ce3358eb4008222bTim Murray        if (b.getConfig() == null) {
606c806aeedb20cb1a725092657ce3358eb4008222bTim Murray            Bitmap newBitmap = Bitmap.createBitmap(b.getWidth(), b.getHeight(), Bitmap.Config.ARGB_8888);
607c806aeedb20cb1a725092657ce3358eb4008222bTim Murray            Canvas c = new Canvas(newBitmap);
608c806aeedb20cb1a725092657ce3358eb4008222bTim Murray            c.drawBitmap(b, 0, 0, null);
609c806aeedb20cb1a725092657ce3358eb4008222bTim Murray            copyFrom(newBitmap);
610c806aeedb20cb1a725092657ce3358eb4008222bTim Murray            return;
611c806aeedb20cb1a725092657ce3358eb4008222bTim Murray        }
61245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        validateBitmapSize(b);
61345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        validateBitmapFormat(b);
61445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        mRS.nAllocationCopyFromBitmap(getID(mRS), b);
61545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
61645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
61745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    /**
618032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * Copy an Allocation from an Allocation.  The types of both allocations
6198fafeb11cf19fdada0add886e99ab0af5d3ddabeJason Sams     * must be identical.
6208fafeb11cf19fdada0add886e99ab0af5d3ddabeJason Sams     *
6218fafeb11cf19fdada0add886e99ab0af5d3ddabeJason Sams     * @param a the source allocation
6228fafeb11cf19fdada0add886e99ab0af5d3ddabeJason Sams     */
6238fafeb11cf19fdada0add886e99ab0af5d3ddabeJason Sams    public void copyFrom(Allocation a) {
6248fafeb11cf19fdada0add886e99ab0af5d3ddabeJason Sams        mRS.validate();
6258fafeb11cf19fdada0add886e99ab0af5d3ddabeJason Sams        if (!mType.equals(a.getType())) {
6268fafeb11cf19fdada0add886e99ab0af5d3ddabeJason Sams            throw new RSIllegalArgumentException("Types of allocations must match.");
6278fafeb11cf19fdada0add886e99ab0af5d3ddabeJason Sams        }
6288fafeb11cf19fdada0add886e99ab0af5d3ddabeJason Sams        copy2DRangeFrom(0, 0, mCurrentDimX, mCurrentDimY, a, 0, 0);
6298fafeb11cf19fdada0add886e99ab0af5d3ddabeJason Sams    }
6308fafeb11cf19fdada0add886e99ab0af5d3ddabeJason Sams
6318fafeb11cf19fdada0add886e99ab0af5d3ddabeJason Sams
6328fafeb11cf19fdada0add886e99ab0af5d3ddabeJason Sams    /**
633032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * This is only intended to be used by auto-generated code reflected from
634032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * the RenderScript script files and should not be used by developers.
63545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
63645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param xoff
63745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param fp
63845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     */
63945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    public void setFromFieldPacker(int xoff, FieldPacker fp) {
64045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        mRS.validate();
64145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        int eSize = mType.mElement.getBytesSize();
64245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        final byte[] data = fp.getData();
64345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
64445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        int count = data.length / eSize;
64545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        if ((eSize * count) != data.length) {
64645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            throw new RSIllegalArgumentException("Field packer length " + data.length +
64745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                                               " not divisible by element size " + eSize + ".");
64845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        }
64945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        copy1DRangeFromUnchecked(xoff, count, data);
65045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
65145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
65245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    /**
653032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * This is only intended to be used by auto-generated code reflected from
654032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * the RenderScript script files.
65545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
65645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param xoff
65745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param component_number
65845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param fp
65945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     */
66045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    public void setFromFieldPacker(int xoff, int component_number, FieldPacker fp) {
66145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        mRS.validate();
66245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        if (component_number >= mType.mElement.mElements.length) {
66345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            throw new RSIllegalArgumentException("Component_number " + component_number + " out of range.");
66445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        }
66545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        if(xoff < 0) {
66645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            throw new RSIllegalArgumentException("Offset must be >= 0.");
66745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        }
66845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
66945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        final byte[] data = fp.getData();
67045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        int eSize = mType.mElement.mElements[component_number].getBytesSize();
67145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        eSize *= mType.mElement.mArraySizes[component_number];
67245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
67345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        if (data.length != eSize) {
67445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            throw new RSIllegalArgumentException("Field packer sizelength " + data.length +
67545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                                               " does not match component size " + eSize + ".");
67645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        }
67745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
67845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        mRS.nAllocationElementData1D(getIDSafe(), xoff, mSelectedLOD,
67945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                                     component_number, data, data.length);
68045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
68145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
68245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    private void data1DChecks(int off, int count, int len, int dataSize) {
68345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        mRS.validate();
68445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        if(off < 0) {
68545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            throw new RSIllegalArgumentException("Offset must be >= 0.");
68645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        }
68745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        if(count < 1) {
68845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            throw new RSIllegalArgumentException("Count must be >= 1.");
68945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        }
69045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        if((off + count) > mCurrentCount) {
69145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            throw new RSIllegalArgumentException("Overflow, Available count " + mCurrentCount +
69245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                                               ", got " + count + " at offset " + off + ".");
69345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        }
69445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        if(len < dataSize) {
69545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            throw new RSIllegalArgumentException("Array too small for allocation type.");
69645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        }
69745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
69845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
69945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    /**
700032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * Generate a mipmap chain. This is only valid if the Type of the Allocation
701032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * includes mipmaps.
70245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
703032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * <p>This function will generate a complete set of mipmaps from the top
704032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * level LOD and place them into the script memory space.</p>
70545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
706032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * <p>If the Allocation is also using other memory spaces, a call to {@link
707032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * #syncAll syncAll(Allocation.USAGE_SCRIPT)} is required.</p>
70845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     */
70945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    public void generateMipmaps() {
71045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        mRS.nAllocationGenerateMipmaps(getID(mRS));
71145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
71245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
71345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    /**
714032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * Copy an array into part of this Allocation.  This method does not
715032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * guarantee that the Allocation is compatible with the input buffer.
71645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
71745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param off The offset of the first element to be copied.
71845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param count The number of elements to be copied.
71945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param d the source data array
72045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     */
72145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    public void copy1DRangeFromUnchecked(int off, int count, int[] d) {
72245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        int dataSize = mType.mElement.getBytesSize() * count;
72345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        data1DChecks(off, count, d.length * 4, dataSize);
72445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        mRS.nAllocationData1D(getIDSafe(), off, mSelectedLOD, count, d, dataSize);
72545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
72645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    /**
727032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * Copy an array into part of this Allocation.  This method does not
728032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * guarantee that the Allocation is compatible with the input buffer.
72945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
73045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param off The offset of the first element to be copied.
73145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param count The number of elements to be copied.
73245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param d the source data array
73345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     */
73445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    public void copy1DRangeFromUnchecked(int off, int count, short[] d) {
73545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        int dataSize = mType.mElement.getBytesSize() * count;
73645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        data1DChecks(off, count, d.length * 2, dataSize);
73745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        mRS.nAllocationData1D(getIDSafe(), off, mSelectedLOD, count, d, dataSize);
73845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
73945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    /**
740032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * Copy an array into part of this Allocation.  This method does not
741032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * guarantee that the Allocation is compatible with the input buffer.
74245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
74345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param off The offset of the first element to be copied.
74445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param count The number of elements to be copied.
74545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param d the source data array
74645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     */
74745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    public void copy1DRangeFromUnchecked(int off, int count, byte[] d) {
74845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        int dataSize = mType.mElement.getBytesSize() * count;
74945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        data1DChecks(off, count, d.length, dataSize);
75045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        mRS.nAllocationData1D(getIDSafe(), off, mSelectedLOD, count, d, dataSize);
75145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
75245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    /**
753032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * Copy an array into part of this Allocation.  This method does not
754032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * guarantee that the Allocation is compatible with the input buffer.
75545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
75645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param off The offset of the first element to be copied.
75745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param count The number of elements to be copied.
75845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param d the source data array
75945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     */
76045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    public void copy1DRangeFromUnchecked(int off, int count, float[] d) {
76145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        int dataSize = mType.mElement.getBytesSize() * count;
76245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        data1DChecks(off, count, d.length * 4, dataSize);
76345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        mRS.nAllocationData1D(getIDSafe(), off, mSelectedLOD, count, d, dataSize);
76445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
76545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
76645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    /**
767032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * Copy an array into part of this Allocation.  This variant is type checked
768032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * and will generate exceptions if the Allocation type is not a 32 bit
769032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * integer type.
77045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
77145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param off The offset of the first element to be copied.
77245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param count The number of elements to be copied.
77345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param d the source data array
77445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     */
77545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    public void copy1DRangeFrom(int off, int count, int[] d) {
77645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        validateIsInt32();
77745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        copy1DRangeFromUnchecked(off, count, d);
77845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
77945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
78045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    /**
781032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * Copy an array into part of this Allocation.  This variant is type checked
782032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * and will generate exceptions if the Allocation type is not a 16 bit
783032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * integer type.
78445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
78545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param off The offset of the first element to be copied.
78645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param count The number of elements to be copied.
78745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param d the source data array
78845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     */
78945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    public void copy1DRangeFrom(int off, int count, short[] d) {
79045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        validateIsInt16();
79145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        copy1DRangeFromUnchecked(off, count, d);
79245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
79345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
79445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    /**
795032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * Copy an array into part of this Allocation.  This variant is type checked
796032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * and will generate exceptions if the Allocation type is not an 8 bit
797032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * integer type.
79845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
79945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param off The offset of the first element to be copied.
80045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param count The number of elements to be copied.
80145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param d the source data array
80245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     */
80345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    public void copy1DRangeFrom(int off, int count, byte[] d) {
80445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        validateIsInt8();
80545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        copy1DRangeFromUnchecked(off, count, d);
80645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
80745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
80845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    /**
809032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * Copy an array into part of this Allocation.  This variant is type checked
810032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * and will generate exceptions if the Allocation type is not a 32 bit float
811032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * type.
81245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
81345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param off The offset of the first element to be copied.
81445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param count The number of elements to be copied.
81545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param d the source data array.
81645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     */
81745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    public void copy1DRangeFrom(int off, int count, float[] d) {
81845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        validateIsFloat32();
81945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        copy1DRangeFromUnchecked(off, count, d);
82045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
82145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
82245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     /**
823032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * Copy part of an Allocation into this Allocation.
82445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
82545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param off The offset of the first element to be copied.
82645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param count The number of elements to be copied.
82745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param data the source data allocation.
82845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param dataOff off The offset of the first element in data to
82945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *          be copied.
83045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     */
83145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    public void copy1DRangeFrom(int off, int count, Allocation data, int dataOff) {
83245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        mRS.nAllocationData2D(getIDSafe(), off, 0,
83345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                              mSelectedLOD, mSelectedFace.mID,
83445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                              count, 1, data.getID(mRS), dataOff, 0,
83545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                              data.mSelectedLOD, data.mSelectedFace.mID);
83645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
83745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
83845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    private void validate2DRange(int xoff, int yoff, int w, int h) {
83945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        if (mAdaptedAllocation != null) {
84045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
84145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        } else {
84245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
84345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            if (xoff < 0 || yoff < 0) {
84445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                throw new RSIllegalArgumentException("Offset cannot be negative.");
84545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            }
84645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            if (h < 0 || w < 0) {
84745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                throw new RSIllegalArgumentException("Height or width cannot be negative.");
84845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            }
84945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            if (((xoff + w) > mCurrentDimX) || ((yoff + h) > mCurrentDimY)) {
85045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                throw new RSIllegalArgumentException("Updated region larger than allocation.");
85145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            }
85245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        }
85345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
85445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
855dfe51945d97812921083af3693deaa4bde6ab11fStephen Hines    void copy2DRangeFromUnchecked(int xoff, int yoff, int w, int h, byte[] data) {
85645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        mRS.validate();
85745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        validate2DRange(xoff, yoff, w, h);
85845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        mRS.nAllocationData2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID,
85945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                              w, h, data, data.length);
86045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
86145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
862dfe51945d97812921083af3693deaa4bde6ab11fStephen Hines    void copy2DRangeFromUnchecked(int xoff, int yoff, int w, int h, short[] data) {
86345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        mRS.validate();
86445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        validate2DRange(xoff, yoff, w, h);
86545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        mRS.nAllocationData2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID,
86645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                              w, h, data, data.length * 2);
86745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
86845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
869dfe51945d97812921083af3693deaa4bde6ab11fStephen Hines    void copy2DRangeFromUnchecked(int xoff, int yoff, int w, int h, int[] data) {
87045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        mRS.validate();
87145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        validate2DRange(xoff, yoff, w, h);
87245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        mRS.nAllocationData2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID,
87345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                              w, h, data, data.length * 4);
87445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
87545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
876dfe51945d97812921083af3693deaa4bde6ab11fStephen Hines    void copy2DRangeFromUnchecked(int xoff, int yoff, int w, int h, float[] data) {
87745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        mRS.validate();
87845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        validate2DRange(xoff, yoff, w, h);
87945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        mRS.nAllocationData2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID,
88045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                              w, h, data, data.length * 4);
88145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
88245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
883dfe51945d97812921083af3693deaa4bde6ab11fStephen Hines
884dfe51945d97812921083af3693deaa4bde6ab11fStephen Hines    /**
885032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * Copy from an array into a rectangular region in this Allocation.  The
886032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * array is assumed to be tightly packed.
887dfe51945d97812921083af3693deaa4bde6ab11fStephen Hines     *
888032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param xoff X offset of the region to update in this Allocation
889032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param yoff Y offset of the region to update in this Allocation
890032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param w Width of the region to update
891032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param h Height of the region to update
892032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param data to be placed into the Allocation
893dfe51945d97812921083af3693deaa4bde6ab11fStephen Hines     */
894dfe51945d97812921083af3693deaa4bde6ab11fStephen Hines    public void copy2DRangeFrom(int xoff, int yoff, int w, int h, byte[] data) {
895dfe51945d97812921083af3693deaa4bde6ab11fStephen Hines        validateIsInt8();
896dfe51945d97812921083af3693deaa4bde6ab11fStephen Hines        copy2DRangeFromUnchecked(xoff, yoff, w, h, data);
897dfe51945d97812921083af3693deaa4bde6ab11fStephen Hines    }
898dfe51945d97812921083af3693deaa4bde6ab11fStephen Hines
899032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines    /**
900032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * Copy from an array into a rectangular region in this Allocation.  The
901032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * array is assumed to be tightly packed.
902032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     *
903032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param xoff X offset of the region to update in this Allocation
904032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param yoff Y offset of the region to update in this Allocation
905032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param w Width of the region to update
906032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param h Height of the region to update
907032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param data to be placed into the Allocation
908032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     */
909dfe51945d97812921083af3693deaa4bde6ab11fStephen Hines    public void copy2DRangeFrom(int xoff, int yoff, int w, int h, short[] data) {
910dfe51945d97812921083af3693deaa4bde6ab11fStephen Hines        validateIsInt16();
911dfe51945d97812921083af3693deaa4bde6ab11fStephen Hines        copy2DRangeFromUnchecked(xoff, yoff, w, h, data);
912dfe51945d97812921083af3693deaa4bde6ab11fStephen Hines    }
913dfe51945d97812921083af3693deaa4bde6ab11fStephen Hines
914032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines    /**
915032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * Copy from an array into a rectangular region in this Allocation.  The
916032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * array is assumed to be tightly packed.
917032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     *
918032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param xoff X offset of the region to update in this Allocation
919032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param yoff Y offset of the region to update in this Allocation
920032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param w Width of the region to update
921032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param h Height of the region to update
922032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param data to be placed into the Allocation
923032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     */
924dfe51945d97812921083af3693deaa4bde6ab11fStephen Hines    public void copy2DRangeFrom(int xoff, int yoff, int w, int h, int[] data) {
925dfe51945d97812921083af3693deaa4bde6ab11fStephen Hines        validateIsInt32();
926dfe51945d97812921083af3693deaa4bde6ab11fStephen Hines        copy2DRangeFromUnchecked(xoff, yoff, w, h, data);
927dfe51945d97812921083af3693deaa4bde6ab11fStephen Hines    }
928dfe51945d97812921083af3693deaa4bde6ab11fStephen Hines
929032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines    /**
930032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * Copy from an array into a rectangular region in this Allocation.  The
931032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * array is assumed to be tightly packed.
932032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     *
933032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param xoff X offset of the region to update in this Allocation
934032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param yoff Y offset of the region to update in this Allocation
935032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param w Width of the region to update
936032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param h Height of the region to update
937032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param data to be placed into the Allocation
938032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     */
939dfe51945d97812921083af3693deaa4bde6ab11fStephen Hines    public void copy2DRangeFrom(int xoff, int yoff, int w, int h, float[] data) {
940dfe51945d97812921083af3693deaa4bde6ab11fStephen Hines        validateIsFloat32();
941dfe51945d97812921083af3693deaa4bde6ab11fStephen Hines        copy2DRangeFromUnchecked(xoff, yoff, w, h, data);
942dfe51945d97812921083af3693deaa4bde6ab11fStephen Hines    }
943dfe51945d97812921083af3693deaa4bde6ab11fStephen Hines
94445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    /**
945032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * Copy a rectangular region from an Allocation into a rectangular region in
946032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * this Allocation.
94745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
948032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param xoff X offset of the region in this Allocation
949032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param yoff Y offset of the region in this Allocation
950032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param w Width of the region to update.
951032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param h Height of the region to update.
952032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param data source Allocation.
953032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param dataXoff X offset in source Allocation
954032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param dataYoff Y offset in source Allocation
95545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     */
95645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    public void copy2DRangeFrom(int xoff, int yoff, int w, int h,
95745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                                Allocation data, int dataXoff, int dataYoff) {
95845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        mRS.validate();
95945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        validate2DRange(xoff, yoff, w, h);
96045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        mRS.nAllocationData2D(getIDSafe(), xoff, yoff,
96145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                              mSelectedLOD, mSelectedFace.mID,
96245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                              w, h, data.getID(mRS), dataXoff, dataYoff,
96345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                              data.mSelectedLOD, data.mSelectedFace.mID);
96445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
96545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
96645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    /**
967032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * Copy a {@link android.graphics.Bitmap} into an Allocation.  The height
968032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * and width of the update will use the height and width of the {@link
969032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * android.graphics.Bitmap}.
97045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
971032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param xoff X offset of the region to update in this Allocation
972032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param yoff Y offset of the region to update in this Allocation
973032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param data the Bitmap to be copied
97445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     */
97545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    public void copy2DRangeFrom(int xoff, int yoff, Bitmap data) {
97645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        mRS.validate();
977c806aeedb20cb1a725092657ce3358eb4008222bTim Murray        if (data.getConfig() == null) {
978c806aeedb20cb1a725092657ce3358eb4008222bTim Murray            Bitmap newBitmap = Bitmap.createBitmap(data.getWidth(), data.getHeight(), Bitmap.Config.ARGB_8888);
979c806aeedb20cb1a725092657ce3358eb4008222bTim Murray            Canvas c = new Canvas(newBitmap);
980c806aeedb20cb1a725092657ce3358eb4008222bTim Murray            c.drawBitmap(data, 0, 0, null);
981c806aeedb20cb1a725092657ce3358eb4008222bTim Murray            copy2DRangeFrom(xoff, yoff, newBitmap);
982302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams            return;
983c806aeedb20cb1a725092657ce3358eb4008222bTim Murray        }
98445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        validateBitmapFormat(data);
98545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        validate2DRange(xoff, yoff, data.getWidth(), data.getHeight());
98645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        mRS.nAllocationData2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID, data);
98745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
98845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
989302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams    private void validate3DRange(int xoff, int yoff, int zoff, int w, int h, int d) {
990302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams        if (mAdaptedAllocation != null) {
991302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams
992302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams        } else {
993302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams
994302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams            if (xoff < 0 || yoff < 0 || zoff < 0) {
995302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams                throw new RSIllegalArgumentException("Offset cannot be negative.");
996302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams            }
997302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams            if (h < 0 || w < 0 || d < 0) {
998302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams                throw new RSIllegalArgumentException("Height or width cannot be negative.");
999302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams            }
1000302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams            if (((xoff + w) > mCurrentDimX) || ((yoff + h) > mCurrentDimY) || ((zoff + d) > mCurrentDimZ)) {
1001302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams                throw new RSIllegalArgumentException("Updated region larger than allocation.");
1002302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams            }
1003302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams        }
1004302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams    }
1005302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams
1006302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams    /**
1007302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams     * @hide
1008302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams     *
1009302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams     */
1010302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams    void copy3DRangeFromUnchecked(int xoff, int yoff, int zoff, int w, int h, int d, byte[] data) {
1011302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams        mRS.validate();
1012302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams        validate3DRange(xoff, yoff, zoff, w, h, d);
1013302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams        mRS.nAllocationData3D(getIDSafe(), xoff, yoff, zoff, mSelectedLOD,
1014302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams                              w, h, d, data, data.length);
1015302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams    }
1016302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams
1017302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams    /**
1018302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams     * @hide
1019302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams     *
1020302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams     */
1021302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams    void copy3DRangeFromUnchecked(int xoff, int yoff, int zoff, int w, int h, int d, short[] data) {
1022302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams        mRS.validate();
1023302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams        validate3DRange(xoff, yoff, zoff, w, h, d);
1024302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams        mRS.nAllocationData3D(getIDSafe(), xoff, yoff, zoff, mSelectedLOD,
1025302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams                              w, h, d, data, data.length * 2);
1026302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams    }
1027302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams
1028302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams    /**
1029302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams     * @hide
1030302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams     *
1031302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams     */
1032302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams    void copy3DRangeFromUnchecked(int xoff, int yoff, int zoff, int w, int h, int d, int[] data) {
1033302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams        mRS.validate();
1034302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams        validate3DRange(xoff, yoff, zoff, w, h, d);
1035302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams        mRS.nAllocationData3D(getIDSafe(), xoff, yoff, zoff, mSelectedLOD,
1036302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams                              w, h, d, data, data.length * 4);
1037302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams    }
103845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
103945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    /**
1040302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams     * @hide
1041302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams     *
1042302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams     */
1043302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams    void copy3DRangeFromUnchecked(int xoff, int yoff, int zoff, int w, int h, int d, float[] data) {
1044302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams        mRS.validate();
1045302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams        validate3DRange(xoff, yoff, zoff, w, h, d);
1046302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams        mRS.nAllocationData3D(getIDSafe(), xoff, yoff, zoff, mSelectedLOD,
1047302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams                              w, h, d, data, data.length * 4);
1048302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams    }
1049302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams
1050302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams
1051302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams    /**
1052302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams     * @hide
1053302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams     * Copy a rectangular region from the array into the allocation.
1054032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * The array is assumed to be tightly packed.
1055032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     *
1056032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param xoff X offset of the region to update in this Allocation
1057032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param yoff Y offset of the region to update in this Allocation
1058032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param zoff Z offset of the region to update in this Allocation
1059032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param w Width of the region to update
1060032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param h Height of the region to update
1061032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param d Depth of the region to update
1062302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams     * @param data to be placed into the allocation
1063302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams     */
1064302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams    public void copy3DRangeFrom(int xoff, int yoff, int zoff, int w, int h, int d, byte[] data) {
1065302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams        validateIsInt8();
1066302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams        copy3DRangeFromUnchecked(xoff, yoff, zoff, w, h, d, data);
1067302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams    }
1068302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams
1069302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams    /**
1070302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams     * @hide
1071302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams     *
1072302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams     */
1073302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams    public void copy3DRangeFrom(int xoff, int yoff, int zoff, int w, int h, int d, short[] data) {
1074302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams        validateIsInt16();
1075302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams        copy3DRangeFromUnchecked(xoff, yoff, zoff, w, h, d, data);
1076302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams    }
1077302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams
1078302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams    /**
1079302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams     * @hide
1080302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams     *
1081302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams     */
1082302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams    public void copy3DRangeFrom(int xoff, int yoff, int zoff, int w, int h, int d, int[] data) {
1083302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams        validateIsInt32();
1084302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams        copy3DRangeFromUnchecked(xoff, yoff, zoff, w, h, d, data);
1085302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams    }
1086302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams
1087302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams    /**
1088302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams     * @hide
1089302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams     *
1090302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams     */
1091302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams    public void copy3DRangeFrom(int xoff, int yoff, int zoff, int w, int h, int d, float[] data) {
1092302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams        validateIsFloat32();
1093302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams        copy3DRangeFromUnchecked(xoff, yoff, zoff, w, h, d, data);
1094302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams    }
1095302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams
1096302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams    /**
1097302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams     * @hide
1098302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams     * Copy a rectangular region into the allocation from another
1099302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams     * allocation.
1100302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams     *
1101032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param xoff X offset of the region to update in this Allocation
1102032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param yoff Y offset of the region to update in this Allocation
1103032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param zoff Z offset of the region to update in this Allocation
1104032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param w Width of the region to update.
1105032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param h Height of the region to update.
1106032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param d Depth of the region to update.
1107302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams     * @param data source allocation.
1108032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param dataXoff X offset of the region in the source Allocation
1109032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param dataYoff Y offset of the region in the source Allocation
1110032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param dataZoff Z offset of the region in the source Allocation
1111302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams     */
1112302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams    public void copy3DRangeFrom(int xoff, int yoff, int zoff, int w, int h, int d,
1113302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams                                Allocation data, int dataXoff, int dataYoff, int dataZoff) {
1114302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams        mRS.validate();
1115302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams        validate3DRange(xoff, yoff, zoff, w, h, d);
1116302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams        mRS.nAllocationData3D(getIDSafe(), xoff, yoff, zoff, mSelectedLOD,
1117302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams                              w, h, d, data.getID(mRS), dataXoff, dataYoff, dataZoff,
1118302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams                              data.mSelectedLOD);
1119302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams    }
1120302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams
1121302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams
1122302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams    /**
1123032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * Copy from the Allocation into a {@link android.graphics.Bitmap}.  The
1124032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * bitmap must match the dimensions of the Allocation.
112545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
112645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param b The bitmap to be set from the Allocation.
112745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     */
112845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    public void copyTo(Bitmap b) {
112945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        mRS.validate();
113045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        validateBitmapFormat(b);
113145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        validateBitmapSize(b);
113245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        mRS.nAllocationCopyToBitmap(getID(mRS), b);
113345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
113445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
113545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    /**
1136032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * Copy from the Allocation into a byte array.  The array must be at least
1137032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * as large as the Allocation.  The allocation must be of an 8 bit integer
11382192d04dde96e02b9c908d3cc846c8c881ff54a1Stephen Hines     * {@link android.support.v8.renderscript.Element} type.
113945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
114045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param d The array to be set from the Allocation.
114145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     */
114245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    public void copyTo(byte[] d) {
114345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        validateIsInt8();
114445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        mRS.validate();
114545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        mRS.nAllocationRead(getID(mRS), d);
114645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
114745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
114845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    /**
1149032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * Copy from the Allocation into a short array.  The array must be at least
1150032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * as large as the Allocation.  The allocation must be of an 16 bit integer
11512192d04dde96e02b9c908d3cc846c8c881ff54a1Stephen Hines     * {@link android.support.v8.renderscript.Element} type.
115245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
115345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param d The array to be set from the Allocation.
115445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     */
115545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    public void copyTo(short[] d) {
115645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        validateIsInt16();
115745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        mRS.validate();
115845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        mRS.nAllocationRead(getID(mRS), d);
115945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
116045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
116145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    /**
1162032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * Copy from the Allocation into a int array.  The array must be at least as
1163032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * large as the Allocation.  The allocation must be of an 32 bit integer
11642192d04dde96e02b9c908d3cc846c8c881ff54a1Stephen Hines     * {@link android.support.v8.renderscript.Element} type.
116545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
116645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param d The array to be set from the Allocation.
116745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     */
116845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    public void copyTo(int[] d) {
116945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        validateIsInt32();
117045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        mRS.validate();
117145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        mRS.nAllocationRead(getID(mRS), d);
117245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
117345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
117445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    /**
1175032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * Copy from the Allocation into a float array.  The array must be at least
1176032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * as large as the Allocation.  The allocation must be of an 32 bit float
11772192d04dde96e02b9c908d3cc846c8c881ff54a1Stephen Hines     * {@link android.support.v8.renderscript.Element} type.
117845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
117945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param d The array to be set from the Allocation.
118045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     */
118145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    public void copyTo(float[] d) {
118245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        validateIsFloat32();
118345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        mRS.validate();
118445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        mRS.nAllocationRead(getID(mRS), d);
118545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
118645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
118745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    // creation
118845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
118945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    static BitmapFactory.Options mBitmapOptions = new BitmapFactory.Options();
119045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    static {
119145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        mBitmapOptions.inScaled = false;
119245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
119345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
119445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    /**
1195032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * Creates a new Allocation with the given {@link
11962192d04dde96e02b9c908d3cc846c8c881ff54a1Stephen Hines     * android.support.v8.renderscript.Type}, mipmap flag, and usage flags.
119745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
1198032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param type RenderScript type describing data layout
119945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param mips specifies desired mipmap behaviour for the
120045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *             allocation
1201032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param usage bit field specifying how the Allocation is
120245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *              utilized
120345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     */
120445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    static public Allocation createTyped(RenderScript rs, Type type, MipmapControl mips, int usage) {
1205baf4b4a16ff2ab85653438b93db9af03d4375f2dTim Murray        if (rs.isNative) {
1206baf4b4a16ff2ab85653438b93db9af03d4375f2dTim Murray            RenderScriptThunker rst = (RenderScriptThunker)rs;
1207baf4b4a16ff2ab85653438b93db9af03d4375f2dTim Murray            return AllocationThunker.createTyped(rst, type, mips, usage);
1208baf4b4a16ff2ab85653438b93db9af03d4375f2dTim Murray        }
120945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        rs.validate();
121045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        if (type.getID(rs) == 0) {
121145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            throw new RSInvalidStateException("Bad Type");
121245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        }
121345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        int id = rs.nAllocationCreateTyped(type.getID(rs), mips.mID, usage, 0);
121445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        if (id == 0) {
121545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            throw new RSRuntimeException("Allocation creation failed.");
121645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        }
121745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        return new Allocation(id, rs, type, usage);
121845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
121945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
122045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    /**
1221032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * Creates an Allocation with the size specified by the type and no mipmaps
1222032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * generated by default
122345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
122445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param rs Context to which the allocation will belong.
122545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param type renderscript type describing data layout
122645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param usage bit field specifying how the allocation is
122745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *              utilized
122845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
122945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @return allocation
123045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     */
123145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    static public Allocation createTyped(RenderScript rs, Type type, int usage) {
123245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        return createTyped(rs, type, MipmapControl.MIPMAP_NONE, usage);
123345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
123445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
123545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    /**
1236032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * Creates an Allocation for use by scripts with a given {@link
12372192d04dde96e02b9c908d3cc846c8c881ff54a1Stephen Hines     * android.support.v8.renderscript.Type} and no mipmaps
123845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
1239032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param rs Context to which the Allocation will belong.
1240032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param type RenderScript Type describing data layout
124145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
124245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @return allocation
124345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     */
124445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    static public Allocation createTyped(RenderScript rs, Type type) {
124545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        return createTyped(rs, type, MipmapControl.MIPMAP_NONE, USAGE_SCRIPT);
124645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
124745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
124845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    /**
1249032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * Creates an Allocation with a specified number of given elements
125045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
1251032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param rs Context to which the Allocation will belong.
1252032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param e Element to use in the Allocation
1253032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param count the number of Elements in the Allocation
1254032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param usage bit field specifying how the Allocation is
125545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *              utilized
125645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
125745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @return allocation
125845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     */
125945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    static public Allocation createSized(RenderScript rs, Element e,
126045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                                         int count, int usage) {
1261baf4b4a16ff2ab85653438b93db9af03d4375f2dTim Murray        if (rs.isNative) {
1262baf4b4a16ff2ab85653438b93db9af03d4375f2dTim Murray            RenderScriptThunker rst = (RenderScriptThunker)rs;
1263baf4b4a16ff2ab85653438b93db9af03d4375f2dTim Murray            return AllocationThunker.createSized(rs, e, count, usage);
1264baf4b4a16ff2ab85653438b93db9af03d4375f2dTim Murray        }
126545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        rs.validate();
126645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        Type.Builder b = new Type.Builder(rs, e);
126745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        b.setX(count);
126845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        Type t = b.create();
126945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
127045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        int id = rs.nAllocationCreateTyped(t.getID(rs), MipmapControl.MIPMAP_NONE.mID, usage, 0);
127145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        if (id == 0) {
127245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            throw new RSRuntimeException("Allocation creation failed.");
127345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        }
127445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        return new Allocation(id, rs, t, usage);
127545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
127645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
127745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    /**
1278032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * Creates an Allocation with a specified number of given elements
127945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
1280032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param rs Context to which the Allocation will belong.
1281032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param e Element to use in the Allocation
1282032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param count the number of Elements in the Allocation
128345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
128445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @return allocation
128545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     */
128645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    static public Allocation createSized(RenderScript rs, Element e, int count) {
128745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        return createSized(rs, e, count, USAGE_SCRIPT);
128845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
128945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
129045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    static Element elementFromBitmap(RenderScript rs, Bitmap b) {
129145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        final Bitmap.Config bc = b.getConfig();
129245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        if (bc == Bitmap.Config.ALPHA_8) {
129345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            return Element.A_8(rs);
129445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        }
129545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        if (bc == Bitmap.Config.ARGB_4444) {
129645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            return Element.RGBA_4444(rs);
129745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        }
129845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        if (bc == Bitmap.Config.ARGB_8888) {
129945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            return Element.RGBA_8888(rs);
130045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        }
130145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        if (bc == Bitmap.Config.RGB_565) {
130245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            return Element.RGB_565(rs);
130345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        }
130445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        throw new RSInvalidStateException("Bad bitmap type: " + bc);
130545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
130645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
130745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    static Type typeFromBitmap(RenderScript rs, Bitmap b,
130845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                                       MipmapControl mip) {
130945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        Element e = elementFromBitmap(rs, b);
131045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        Type.Builder tb = new Type.Builder(rs, e);
131145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        tb.setX(b.getWidth());
131245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        tb.setY(b.getHeight());
131345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        tb.setMipmaps(mip == MipmapControl.MIPMAP_FULL);
131445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        return tb.create();
131545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
131645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
131745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    /**
1318032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * Creates an Allocation from a {@link android.graphics.Bitmap}.
131945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
132045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param rs Context to which the allocation will belong.
1321032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param b Bitmap source for the allocation data
132245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param mips specifies desired mipmap behaviour for the
132345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *             allocation
132445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param usage bit field specifying how the allocation is
132545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *              utilized
132645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
1327032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @return Allocation containing bitmap data
132845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
132945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     */
133045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    static public Allocation createFromBitmap(RenderScript rs, Bitmap b,
133145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                                              MipmapControl mips,
133245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                                              int usage) {
1333baf4b4a16ff2ab85653438b93db9af03d4375f2dTim Murray        if (rs.isNative) {
1334baf4b4a16ff2ab85653438b93db9af03d4375f2dTim Murray            RenderScriptThunker rst = (RenderScriptThunker)rs;
1335baf4b4a16ff2ab85653438b93db9af03d4375f2dTim Murray            return AllocationThunker.createFromBitmap(rst, b, mips, usage);
1336baf4b4a16ff2ab85653438b93db9af03d4375f2dTim Murray        }
133745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        rs.validate();
1338c806aeedb20cb1a725092657ce3358eb4008222bTim Murray
1339c806aeedb20cb1a725092657ce3358eb4008222bTim Murray        // WAR undocumented color formats
1340c806aeedb20cb1a725092657ce3358eb4008222bTim Murray        if (b.getConfig() == null) {
1341c806aeedb20cb1a725092657ce3358eb4008222bTim Murray            if ((usage & USAGE_SHARED) != 0) {
1342c806aeedb20cb1a725092657ce3358eb4008222bTim Murray                throw new RSIllegalArgumentException("USAGE_SHARED cannot be used with a Bitmap that has a null config.");
1343c806aeedb20cb1a725092657ce3358eb4008222bTim Murray            }
1344c806aeedb20cb1a725092657ce3358eb4008222bTim Murray            Bitmap newBitmap = Bitmap.createBitmap(b.getWidth(), b.getHeight(), Bitmap.Config.ARGB_8888);
1345c806aeedb20cb1a725092657ce3358eb4008222bTim Murray            Canvas c = new Canvas(newBitmap);
1346c806aeedb20cb1a725092657ce3358eb4008222bTim Murray            c.drawBitmap(b, 0, 0, null);
1347c806aeedb20cb1a725092657ce3358eb4008222bTim Murray            return createFromBitmap(rs, newBitmap, mips, usage);
1348c806aeedb20cb1a725092657ce3358eb4008222bTim Murray        }
1349c806aeedb20cb1a725092657ce3358eb4008222bTim Murray
135045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        Type t = typeFromBitmap(rs, b, mips);
135145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
13529c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray        // enable optimized bitmap path only with no mipmap and script-only usage
13539c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray        if (mips == MipmapControl.MIPMAP_NONE &&
13549c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray            t.getElement().isCompatible(Element.RGBA_8888(rs)) &&
1355257cecb5e24c9af71ba4d621887e88e46b0ac411Stephen Hines            usage == (USAGE_SHARED | USAGE_SCRIPT | USAGE_GRAPHICS_TEXTURE)) {
13569c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray            int id = rs.nAllocationCreateBitmapBackedAllocation(t.getID(rs), mips.mID, b, usage);
13579c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray            if (id == 0) {
13589c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray                throw new RSRuntimeException("Load failed.");
13599c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray            }
13609c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray
13619c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray            // keep a reference to the Bitmap around to prevent GC
13629c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray            Allocation alloc = new Allocation(id, rs, t, usage);
13639c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray            alloc.setBitmap(b);
13649c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray            return alloc;
13659c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray        }
13669c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray
13679c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray
136845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        int id = rs.nAllocationCreateFromBitmap(t.getID(rs), mips.mID, b, usage);
136945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        if (id == 0) {
137045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            throw new RSRuntimeException("Load failed.");
137145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        }
137245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        return new Allocation(id, rs, t, usage);
137345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
137445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
137545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    /**
1376032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * Creates an Allocation from a {@link android.graphics.Bitmap}.
13779c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray     *
1378032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * <p>This Allocation will be created with {@link #USAGE_SHARED}, and
1379032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * {@link #USAGE_SCRIPT}.</p>
138045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
138145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param rs Context to which the allocation will belong.
138245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param b bitmap source for the allocation data
138345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
1384032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @return Allocation containing bitmap data
138545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
138645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     */
138745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    static public Allocation createFromBitmap(RenderScript rs, Bitmap b) {
138845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        return createFromBitmap(rs, b, MipmapControl.MIPMAP_NONE,
1389257cecb5e24c9af71ba4d621887e88e46b0ac411Stephen Hines                                USAGE_SHARED | USAGE_SCRIPT | USAGE_GRAPHICS_TEXTURE);
139045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
139145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
139245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    /**
1393032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * Creates a cubemap Allocation from a {@link android.graphics.Bitmap}
1394032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * containing the horizontal list of cube faces. Each face must be a square,
1395032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * have the same size as all other faces, and have a width that is a power
1396032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * of 2.
139745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
139845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param rs Context to which the allocation will belong.
1399032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param b Bitmap with cubemap faces layed out in the following
140045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *          format: right, left, top, bottom, front, back
140145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param mips specifies desired mipmap behaviour for the cubemap
140245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param usage bit field specifying how the cubemap is utilized
140345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
140445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @return allocation containing cubemap data
140545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
140645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     */
140745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    static public Allocation createCubemapFromBitmap(RenderScript rs, Bitmap b,
140845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                                                     MipmapControl mips,
140945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                                                     int usage) {
141045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        rs.validate();
141145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
141245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        int height = b.getHeight();
141345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        int width = b.getWidth();
141445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
141545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        if (width % 6 != 0) {
141645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            throw new RSIllegalArgumentException("Cubemap height must be multiple of 6");
141745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        }
141845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        if (width / 6 != height) {
141945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            throw new RSIllegalArgumentException("Only square cube map faces supported");
142045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        }
142145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        boolean isPow2 = (height & (height - 1)) == 0;
142245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        if (!isPow2) {
142345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            throw new RSIllegalArgumentException("Only power of 2 cube faces supported");
142445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        }
142545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
142645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        Element e = elementFromBitmap(rs, b);
142745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        Type.Builder tb = new Type.Builder(rs, e);
142845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        tb.setX(height);
142945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        tb.setY(height);
143045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        tb.setFaces(true);
143145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        tb.setMipmaps(mips == MipmapControl.MIPMAP_FULL);
143245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        Type t = tb.create();
143345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
143445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        int id = rs.nAllocationCubeCreateFromBitmap(t.getID(rs), mips.mID, b, usage);
143545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        if(id == 0) {
143645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            throw new RSRuntimeException("Load failed for bitmap " + b + " element " + e);
143745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        }
143845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        return new Allocation(id, rs, t, usage);
143945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
144045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
144145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    /**
1442032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * Creates a non-mipmapped cubemap Allocation for use as a graphics texture
1443032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * from a {@link android.graphics.Bitmap} containing the horizontal list of
1444032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * cube faces. Each face must be a square, have the same size as all other
1445032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * faces, and have a width that is a power of 2.
144645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
144745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param rs Context to which the allocation will belong.
144845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param b bitmap with cubemap faces layed out in the following
144945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *          format: right, left, top, bottom, front, back
145045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
145145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @return allocation containing cubemap data
145245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
145345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     */
145445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    static public Allocation createCubemapFromBitmap(RenderScript rs,
145545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                                                     Bitmap b) {
145645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        return createCubemapFromBitmap(rs, b, MipmapControl.MIPMAP_NONE,
145745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                                       USAGE_GRAPHICS_TEXTURE);
145845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
145945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
146045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    /**
1461032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * Creates a cubemap Allocation from 6 {@link android.graphics.Bitmap}
1462032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * objects containing the cube faces. Each face must be a square, have the
1463032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * same size as all other faces, and have a width that is a power of 2.
146445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
146545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param rs Context to which the allocation will belong.
146645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param xpos cubemap face in the positive x direction
146745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param xneg cubemap face in the negative x direction
146845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param ypos cubemap face in the positive y direction
146945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param yneg cubemap face in the negative y direction
147045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param zpos cubemap face in the positive z direction
147145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param zneg cubemap face in the negative z direction
147245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param mips specifies desired mipmap behaviour for the cubemap
147345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param usage bit field specifying how the cubemap is utilized
147445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
147545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @return allocation containing cubemap data
147645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
147745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     */
147845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    static public Allocation createCubemapFromCubeFaces(RenderScript rs,
147945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                                                        Bitmap xpos,
148045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                                                        Bitmap xneg,
148145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                                                        Bitmap ypos,
148245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                                                        Bitmap yneg,
148345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                                                        Bitmap zpos,
148445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                                                        Bitmap zneg,
148545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                                                        MipmapControl mips,
148645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                                                        int usage) {
1487baf4b4a16ff2ab85653438b93db9af03d4375f2dTim Murray        /*
148845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        int height = xpos.getHeight();
148945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        if (xpos.getWidth() != height ||
149045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            xneg.getWidth() != height || xneg.getHeight() != height ||
149145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            ypos.getWidth() != height || ypos.getHeight() != height ||
149245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            yneg.getWidth() != height || yneg.getHeight() != height ||
149345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            zpos.getWidth() != height || zpos.getHeight() != height ||
149445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            zneg.getWidth() != height || zneg.getHeight() != height) {
149545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            throw new RSIllegalArgumentException("Only square cube map faces supported");
149645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        }
149745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        boolean isPow2 = (height & (height - 1)) == 0;
149845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        if (!isPow2) {
149945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            throw new RSIllegalArgumentException("Only power of 2 cube faces supported");
150045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        }
150145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
150245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        Element e = elementFromBitmap(rs, xpos);
150345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        Type.Builder tb = new Type.Builder(rs, e);
150445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        tb.setX(height);
150545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        tb.setY(height);
150645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        tb.setFaces(true);
150745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        tb.setMipmaps(mips == MipmapControl.MIPMAP_FULL);
150845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        Type t = tb.create();
150945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        Allocation cubemap = Allocation.createTyped(rs, t, mips, usage);
151045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
151145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        AllocationAdapter adapter = AllocationAdapter.create2D(rs, cubemap);
151245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        adapter.setFace(Type.CubemapFace.POSITIVE_X);
151345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        adapter.copyFrom(xpos);
151445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        adapter.setFace(Type.CubemapFace.NEGATIVE_X);
151545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        adapter.copyFrom(xneg);
151645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        adapter.setFace(Type.CubemapFace.POSITIVE_Y);
151745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        adapter.copyFrom(ypos);
151845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        adapter.setFace(Type.CubemapFace.NEGATIVE_Y);
151945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        adapter.copyFrom(yneg);
152045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        adapter.setFace(Type.CubemapFace.POSITIVE_Z);
152145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        adapter.copyFrom(zpos);
152245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        adapter.setFace(Type.CubemapFace.NEGATIVE_Z);
152345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        adapter.copyFrom(zneg);
152445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
152545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        return cubemap;
1526baf4b4a16ff2ab85653438b93db9af03d4375f2dTim Murray        */
1527baf4b4a16ff2ab85653438b93db9af03d4375f2dTim Murray        return null;
152845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
152945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
153045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    /**
1531032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * Creates a non-mipmapped cubemap Allocation for use as a sampler input
1532032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * from 6 {@link android.graphics.Bitmap} objects containing the cube
1533032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * faces. Each face must be a square, have the same size as all other faces,
1534032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * and have a width that is a power of 2.
153545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
153645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param rs Context to which the allocation will belong.
153745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param xpos cubemap face in the positive x direction
153845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param xneg cubemap face in the negative x direction
153945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param ypos cubemap face in the positive y direction
154045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param yneg cubemap face in the negative y direction
154145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param zpos cubemap face in the positive z direction
154245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param zneg cubemap face in the negative z direction
154345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
154445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @return allocation containing cubemap data
154545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
154645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     */
154745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    static public Allocation createCubemapFromCubeFaces(RenderScript rs,
154845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                                                        Bitmap xpos,
154945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                                                        Bitmap xneg,
155045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                                                        Bitmap ypos,
155145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                                                        Bitmap yneg,
155245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                                                        Bitmap zpos,
155345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                                                        Bitmap zneg) {
155445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        return createCubemapFromCubeFaces(rs, xpos, xneg, ypos, yneg,
155545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                                          zpos, zneg, MipmapControl.MIPMAP_NONE,
155645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                                          USAGE_GRAPHICS_TEXTURE);
155745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
155845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
15599c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray    /**
1560032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * Creates an Allocation from the Bitmap referenced
1561032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * by resource ID.
15629c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray     *
15639c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray     * @param rs Context to which the allocation will belong.
15649c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray     * @param res application resources
15659c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray     * @param id resource id to load the data from
15669c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray     * @param mips specifies desired mipmap behaviour for the
15679c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray     *             allocation
15689c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray     * @param usage bit field specifying how the allocation is
15699c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray     *              utilized
15709c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray     *
1571032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @return Allocation containing resource data
15729c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray     *
15739c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray     */
15749c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray    static public Allocation createFromBitmapResource(RenderScript rs,
15759c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray                                                      Resources res,
15769c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray                                                      int id,
15779c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray                                                      MipmapControl mips,
15789c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray                                                      int usage) {
157945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
15809c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray        rs.validate();
1581032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines        if ((usage & (USAGE_SHARED | USAGE_IO_INPUT | USAGE_IO_OUTPUT)) != 0) {
1582032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines            throw new RSIllegalArgumentException("Unsupported usage specified.");
1583032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines        }
15849c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray        Bitmap b = BitmapFactory.decodeResource(res, id);
15859c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray        Allocation alloc = createFromBitmap(rs, b, mips, usage);
15869c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray        b.recycle();
15879c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray        return alloc;
15889c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray    }
15899c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray
15909c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray    /**
1591032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * Creates a non-mipmapped Allocation to use as a graphics texture from the
1592032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * {@link android.graphics.Bitmap} referenced by resource ID.
1593032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     *
1594032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * <p>This allocation will be created with {@link #USAGE_SCRIPT} and
1595032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * {@link #USAGE_GRAPHICS_TEXTURE}.</p>
15969c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray     *
15979c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray     * @param rs Context to which the allocation will belong.
15989c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray     * @param res application resources
15999c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray     * @param id resource id to load the data from
16009c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray     *
1601032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @return Allocation containing resource data
16029c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray     *
16039c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray     */
16049c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray    static public Allocation createFromBitmapResource(RenderScript rs,
16059c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray                                                      Resources res,
16069c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray                                                      int id) {
16079c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray        return createFromBitmapResource(rs, res, id,
16089c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray                                        MipmapControl.MIPMAP_NONE,
1609032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines                                        USAGE_SCRIPT | USAGE_GRAPHICS_TEXTURE);
16109c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray    }
16119c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray
16129c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray    /**
1613032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * Creates an Allocation containing string data encoded in UTF-8 format.
16149c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray     *
16159c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray     * @param rs Context to which the allocation will belong.
16169c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray     * @param str string to create the allocation from
16179c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray     * @param usage bit field specifying how the allocaiton is
16189c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray     *              utilized
16199c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray     *
16209c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray     */
16219c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray    static public Allocation createFromString(RenderScript rs,
16229c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray                                              String str,
16239c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray                                              int usage) {
16249c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray        rs.validate();
16259c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray        byte[] allocArray = null;
16269c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray        try {
16279c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray            allocArray = str.getBytes("UTF-8");
16289c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray            Allocation alloc = Allocation.createSized(rs, Element.U8(rs), allocArray.length, usage);
16299c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray            alloc.copyFrom(allocArray);
16309c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray            return alloc;
16319c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray        }
16329c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray        catch (Exception e) {
16339c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray            throw new RSRuntimeException("Could not convert string to utf-8.");
16349c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray        }
16359c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray    }
1636dfe51945d97812921083af3693deaa4bde6ab11fStephen Hines}
1637302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams
1638