198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams/*
21b370e358d16cc3b50b169511d6b387db09f972dJason Sams * Copyright (C) 2008-2012 The Android Open Source Project
398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams *
498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams * Licensed under the Apache License, Version 2.0 (the "License");
598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams * you may not use this file except in compliance with the License.
698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams * You may obtain a copy of the License at
798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams *
898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams *      http://www.apache.org/licenses/LICENSE-2.0
998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams *
1098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams * Unless required by applicable law or agreed to in writing, software
1198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams * distributed under the License is distributed on an "AS IS" BASIS,
1298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams * See the License for the specific language governing permissions and
1498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams * limitations under the License.
1598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams */
1698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
1798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Samspackage android.support.v8.renderscript;
1898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
1998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Samsimport java.io.IOException;
2098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Samsimport java.io.InputStream;
2198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Samsimport android.content.res.Resources;
2298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Samsimport android.content.res.AssetManager;
2398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Samsimport android.graphics.Bitmap;
2498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Samsimport android.graphics.BitmapFactory;
25c5953678decd7963ed0515c2a0b8fcc92b3f3b9fTim Murrayimport android.graphics.Canvas;
26c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murrayimport android.view.Surface;
2798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Samsimport android.util.Log;
2898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Samsimport android.util.TypedValue;
2998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
3098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams/**
317d435ae5ba100be5710b685653cc351cab159c11Stephen Hines * <p> This class provides the primary method through which data is passed to
327d435ae5ba100be5710b685653cc351cab159c11Stephen Hines * and from RenderScript kernels.  An Allocation provides the backing store for
3360c5b31f4448410221de043873b94797732afa66Stephen Hines * a given {@link android.support.v8.renderscript.Type}.  </p>
3498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams *
357d435ae5ba100be5710b685653cc351cab159c11Stephen Hines * <p>An Allocation also contains a set of usage flags that denote how the
367d435ae5ba100be5710b685653cc351cab159c11Stephen Hines * Allocation could be used. For example, an Allocation may have usage flags
377d435ae5ba100be5710b685653cc351cab159c11Stephen Hines * specifying that it can be used from a script as well as input to a {@link
3860c5b31f4448410221de043873b94797732afa66Stephen Hines * android.support.v8.renderscript.Sampler}. A developer must synchronize
3960c5b31f4448410221de043873b94797732afa66Stephen Hines * across these different usages using
4060c5b31f4448410221de043873b94797732afa66Stephen Hines * {@link android.support.v8.renderscript.Allocation#syncAll} in
417d435ae5ba100be5710b685653cc351cab159c11Stephen Hines * order to ensure that different users of the Allocation have a consistent view
427d435ae5ba100be5710b685653cc351cab159c11Stephen Hines * of memory. For example, in the case where an Allocation is used as the output
437d435ae5ba100be5710b685653cc351cab159c11Stephen Hines * of one kernel and as Sampler input in a later kernel, a developer must call
447d435ae5ba100be5710b685653cc351cab159c11Stephen Hines * {@link #syncAll syncAll(Allocation.USAGE_SCRIPT)} prior to launching the
457d435ae5ba100be5710b685653cc351cab159c11Stephen Hines * second kernel to ensure correctness.
4698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams *
477d435ae5ba100be5710b685653cc351cab159c11Stephen Hines * <p>An Allocation can be populated with the {@link #copyFrom} routines. For
487d435ae5ba100be5710b685653cc351cab159c11Stephen Hines * more complex Element types, the {@link #copyFromUnchecked} methods can be
497d435ae5ba100be5710b685653cc351cab159c11Stephen Hines * used to copy from byte arrays or similar constructs.</p>
5098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams *
5198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams * <div class="special reference">
5298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams * <h3>Developer Guides</h3>
5360c5b31f4448410221de043873b94797732afa66Stephen Hines * <p>For more information about creating an application that uses
5460c5b31f4448410221de043873b94797732afa66Stephen Hines * RenderScript, read the
5560c5b31f4448410221de043873b94797732afa66Stephen Hines * <a href="{@docRoot}guide/topics/renderscript/index.html">RenderScript</a>
5660c5b31f4448410221de043873b94797732afa66Stephen Hines * developer guide.</p>
5798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams * </div>
5898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams **/
5998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Samspublic class Allocation extends BaseObj {
6098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    Type mType;
6198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    Bitmap mBitmap;
6298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    int mUsage;
6398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    Allocation mAdaptedAllocation;
647720e5ceb6a4cc9ed330ad7c483f7eac08f554e6Tim Murray    int mSize;
6598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
6698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    boolean mConstrainedLOD;
6798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    boolean mConstrainedFace;
6898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    boolean mConstrainedY;
6998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    boolean mConstrainedZ;
7098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    boolean mReadAllowed = true;
7198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    boolean mWriteAllowed = true;
7298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    int mSelectedY;
7398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    int mSelectedZ;
7498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    int mSelectedLOD;
7598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    Type.CubemapFace mSelectedFace = Type.CubemapFace.POSITIVE_X;
7698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
7798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    int mCurrentDimX;
7898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    int mCurrentDimY;
7998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    int mCurrentDimZ;
8098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    int mCurrentCount;
8198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
8298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
837d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * The usage of the Allocation.  These signal to RenderScript where to place
847d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * the Allocation in memory.
8598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
867d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     */
877d435ae5ba100be5710b685653cc351cab159c11Stephen Hines
887d435ae5ba100be5710b685653cc351cab159c11Stephen Hines    /**
897d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * The Allocation will be bound to and accessed by scripts.
9098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
9198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public static final int USAGE_SCRIPT = 0x0001;
9298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
9398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
947d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * The Allocation will be used as a texture source by one or more graphics
957d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * programs.
9698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
9798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
9898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public static final int USAGE_GRAPHICS_TEXTURE = 0x0002;
9998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
10098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
1017d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * The Allocation will be used as a {@link android.graphics.SurfaceTexture}
1027d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * consumer.  This usage will cause the Allocation to be created as
1037d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * read-only.
104c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray     *
105c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray     */
106c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray    public static final int USAGE_IO_INPUT = 0x0020;
107c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray
108c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray    /**
1097d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * The Allocation will be used as a {@link android.graphics.SurfaceTexture}
1107d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * producer.  The dimensions and format of the {@link
1117d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * android.graphics.SurfaceTexture} will be forced to those of the
1127d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Allocation.
113c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray     *
114c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray     */
115c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray    public static final int USAGE_IO_OUTPUT = 0x0040;
116c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray
117c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray    /**
1187d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * The Allocation's backing store will be inherited from another object
1197d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * (usually a {@link android.graphics.Bitmap}); copying to or from the
1207d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * original source Bitmap will cause a synchronization rather than a full
1217d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * copy.  {@link #syncAll} may also be used to synchronize the Allocation
1227d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * and the source Bitmap.
123c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray     *
1247d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * <p>This is set by default for allocations created with {@link
1257d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * #createFromBitmap} in API version 18 and higher.</p>
126c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray     *
127c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray     */
128c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray    public static final int USAGE_SHARED = 0x0080;
129c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray
130c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray    /**
1317d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Controls mipmap behavior when using the bitmap creation and update
1327d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * functions.
13398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
13498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public enum MipmapControl {
13598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        /**
1367d435ae5ba100be5710b685653cc351cab159c11Stephen Hines         * No mipmaps will be generated and the type generated from the incoming
1377d435ae5ba100be5710b685653cc351cab159c11Stephen Hines         * bitmap will not contain additional LODs.
13898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams         */
13998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        MIPMAP_NONE(0),
14098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
14198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        /**
1427d435ae5ba100be5710b685653cc351cab159c11Stephen Hines         * A full mipmap chain will be created in script memory.  The Type of
1437d435ae5ba100be5710b685653cc351cab159c11Stephen Hines         * the Allocation will contain a full mipmap chain.  On upload, the full
1447d435ae5ba100be5710b685653cc351cab159c11Stephen Hines         * chain will be transferred.
14598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams         */
14698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        MIPMAP_FULL(1),
14798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
14898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        /**
1497d435ae5ba100be5710b685653cc351cab159c11Stephen Hines         * The Type of the Allocation will be the same as MIPMAP_NONE.  It will
1507d435ae5ba100be5710b685653cc351cab159c11Stephen Hines         * not contain mipmaps.  On upload, the allocation data will contain a
1517d435ae5ba100be5710b685653cc351cab159c11Stephen Hines         * full mipmap chain generated from the top level in script memory.
15298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams         */
15398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        MIPMAP_ON_SYNC_TO_TEXTURE(2);
15498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
15598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        int mID;
15698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        MipmapControl(int id) {
15798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            mID = id;
15898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
15998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
16098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
16198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
16298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    private int getIDSafe() {
16398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        if (mAdaptedAllocation != null) {
16498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            return mAdaptedAllocation.getID(mRS);
16598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
16698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        return getID(mRS);
16798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
16898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
16998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
17098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams   /**
17160c5b31f4448410221de043873b94797732afa66Stephen Hines     * Get the {@link android.support.v8.renderscript.Element} of the {@link
17260c5b31f4448410221de043873b94797732afa66Stephen Hines     * android.support.v8.renderscript.Type} of the Allocation.
17398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
1747d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @return Element
17598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
17698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
17798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public Element getElement() {
17898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        return mType.getElement();
17998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
18098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
18198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
18298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * Get the usage flags of the Allocation.
18398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
1847d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @return usage this Allocation's set of the USAGE_* flags OR'd together
18598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
18698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
18798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public int getUsage() {
18898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        return mUsage;
18998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
19098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
19198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
19298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * Get the size of the Allocation in bytes.
19398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
19498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @return size of the Allocation in bytes.
19598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
19698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
19798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public int getBytesSize() {
19898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        return mType.getCount() * mType.getElement().getBytesSize();
19998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
20098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
20198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    private void updateCacheInfo(Type t) {
20298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        mCurrentDimX = t.getX();
20398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        mCurrentDimY = t.getY();
20498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        mCurrentDimZ = t.getZ();
20598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        mCurrentCount = mCurrentDimX;
20698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        if (mCurrentDimY > 1) {
20798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            mCurrentCount *= mCurrentDimY;
20898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
20998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        if (mCurrentDimZ > 1) {
21098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            mCurrentCount *= mCurrentDimZ;
21198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
21298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
21398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
2141b370e358d16cc3b50b169511d6b387db09f972dJason Sams    private void setBitmap(Bitmap b) {
2151b370e358d16cc3b50b169511d6b387db09f972dJason Sams        mBitmap = b;
2161b370e358d16cc3b50b169511d6b387db09f972dJason Sams    }
2171b370e358d16cc3b50b169511d6b387db09f972dJason Sams
21898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    Allocation(int id, RenderScript rs, Type t, int usage) {
21998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        super(id, rs);
220c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray        if ((usage & ~(USAGE_SCRIPT |
221c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray                       USAGE_GRAPHICS_TEXTURE |
222c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray                       USAGE_IO_INPUT |
223c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray                       USAGE_IO_OUTPUT |
224c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray                       USAGE_SHARED)) != 0) {
22598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            throw new RSIllegalArgumentException("Unknown usage specified.");
22698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
22798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
228c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray        if ((usage & USAGE_IO_INPUT) != 0) {
229c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray            mWriteAllowed = false;
230c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray
231c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray            if ((usage & ~(USAGE_IO_INPUT |
232c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray                           USAGE_GRAPHICS_TEXTURE |
233c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray                           USAGE_SCRIPT)) != 0) {
234c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray                throw new RSIllegalArgumentException("Invalid usage combination.");
235c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray            }
236c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray        }
237c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray
23898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        mType = t;
23998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        mUsage = usage;
2407720e5ceb6a4cc9ed330ad7c483f7eac08f554e6Tim Murray        mSize = mType.getCount() * mType.getElement().getBytesSize();
24198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
24298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        if (t != null) {
24398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            updateCacheInfo(t);
24498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
2457720e5ceb6a4cc9ed330ad7c483f7eac08f554e6Tim Murray        if (RenderScript.sUseGCHooks == true) {
2467720e5ceb6a4cc9ed330ad7c483f7eac08f554e6Tim Murray            try {
2477720e5ceb6a4cc9ed330ad7c483f7eac08f554e6Tim Murray                RenderScript.registerNativeAllocation.invoke(RenderScript.sRuntime, mSize);
2487720e5ceb6a4cc9ed330ad7c483f7eac08f554e6Tim Murray            } catch (Exception e) {
2497720e5ceb6a4cc9ed330ad7c483f7eac08f554e6Tim Murray                Log.e(RenderScript.LOG_TAG, "Couldn't invoke registerNativeAllocation:" + e);
2507720e5ceb6a4cc9ed330ad7c483f7eac08f554e6Tim Murray                throw new RSRuntimeException("Couldn't invoke registerNativeAllocation:" + e);
2517720e5ceb6a4cc9ed330ad7c483f7eac08f554e6Tim Murray            }
2527720e5ceb6a4cc9ed330ad7c483f7eac08f554e6Tim Murray        }
2537720e5ceb6a4cc9ed330ad7c483f7eac08f554e6Tim Murray    }
2547720e5ceb6a4cc9ed330ad7c483f7eac08f554e6Tim Murray
2557720e5ceb6a4cc9ed330ad7c483f7eac08f554e6Tim Murray    protected void finalize() throws Throwable {
2567720e5ceb6a4cc9ed330ad7c483f7eac08f554e6Tim Murray        if (RenderScript.sUseGCHooks == true) {
2577720e5ceb6a4cc9ed330ad7c483f7eac08f554e6Tim Murray            RenderScript.registerNativeFree.invoke(RenderScript.sRuntime, mSize);
2587720e5ceb6a4cc9ed330ad7c483f7eac08f554e6Tim Murray        }
2597720e5ceb6a4cc9ed330ad7c483f7eac08f554e6Tim Murray        super.finalize();
26098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
26198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
2627720e5ceb6a4cc9ed330ad7c483f7eac08f554e6Tim Murray
26398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    private void validateIsInt32() {
26498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        if ((mType.mElement.mType == Element.DataType.SIGNED_32) ||
26598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            (mType.mElement.mType == Element.DataType.UNSIGNED_32)) {
26698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            return;
26798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
26898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        throw new RSIllegalArgumentException(
26998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            "32 bit integer source does not match allocation type " + mType.mElement.mType);
27098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
27198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
27298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    private void validateIsInt16() {
27398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        if ((mType.mElement.mType == Element.DataType.SIGNED_16) ||
27498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            (mType.mElement.mType == Element.DataType.UNSIGNED_16)) {
27598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            return;
27698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
27798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        throw new RSIllegalArgumentException(
27898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            "16 bit integer source does not match allocation type " + mType.mElement.mType);
27998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
28098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
28198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    private void validateIsInt8() {
28298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        if ((mType.mElement.mType == Element.DataType.SIGNED_8) ||
28398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            (mType.mElement.mType == Element.DataType.UNSIGNED_8)) {
28498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            return;
28598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
28698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        throw new RSIllegalArgumentException(
28798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            "8 bit integer source does not match allocation type " + mType.mElement.mType);
28898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
28998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
29098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    private void validateIsFloat32() {
29198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        if (mType.mElement.mType == Element.DataType.FLOAT_32) {
29298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            return;
29398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
29498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        throw new RSIllegalArgumentException(
29598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            "32 bit float source does not match allocation type " + mType.mElement.mType);
29698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
29798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
29898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    private void validateIsObject() {
29998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        if ((mType.mElement.mType == Element.DataType.RS_ELEMENT) ||
30098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            (mType.mElement.mType == Element.DataType.RS_TYPE) ||
30198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            (mType.mElement.mType == Element.DataType.RS_ALLOCATION) ||
30298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            (mType.mElement.mType == Element.DataType.RS_SAMPLER) ||
30398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            (mType.mElement.mType == Element.DataType.RS_SCRIPT)) {
30498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            return;
30598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
30698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        throw new RSIllegalArgumentException(
30798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            "Object source does not match allocation type " + mType.mElement.mType);
30898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
30998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
31098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
31160c5b31f4448410221de043873b94797732afa66Stephen Hines     * Get the {@link android.support.v8.renderscript.Type} of the Allocation.
31298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
31398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @return Type
31498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
31598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
31698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public Type getType() {
31798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        return mType;
31898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
31998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
32098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
3217d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Propagate changes from one usage of the Allocation to the
3227d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * other usages of the Allocation.
32398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
32498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
32598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public void syncAll(int srcLocation) {
32698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        switch (srcLocation) {
32798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        case USAGE_SCRIPT:
32898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        case USAGE_GRAPHICS_TEXTURE:
32998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            break;
33098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        default:
33198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            throw new RSIllegalArgumentException("Source must be exactly one usage type.");
33298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
33398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        mRS.validate();
33498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        mRS.nAllocationSyncAll(getIDSafe(), srcLocation);
33598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
33698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
337c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray    /**
3387d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Send a buffer to the output stream.  The contents of the Allocation will
3397d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * be undefined after this operation. This operation is only valid if {@link
3407d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * #USAGE_IO_OUTPUT} is set on the Allocation.
3417d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     *
342c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray     *
343c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray     */
344c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray    public void ioSend() {
345c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray        if ((mUsage & USAGE_IO_OUTPUT) == 0) {
346c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray            throw new RSIllegalArgumentException(
347c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray                "Can only send buffer if IO_OUTPUT usage specified.");
348c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray        }
349c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray        mRS.validate();
350c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray        mRS.nAllocationIoSend(getID(mRS));
351c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray    }
352c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray
353c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray    /**
354c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray     * Delete once code is updated.
355c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray     * @hide
356c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray     */
357c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray    public void ioSendOutput() {
358c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray        ioSend();
359c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray    }
360c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray
361c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray    /**
3627d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Receive the latest input into the Allocation. This operation
3637d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * is only valid if {@link #USAGE_IO_INPUT} is set on the Allocation.
364c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray     *
365c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray     */
366c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray    public void ioReceive() {
367c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray        if ((mUsage & USAGE_IO_INPUT) == 0) {
368c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray            throw new RSIllegalArgumentException(
369c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray                "Can only receive if IO_INPUT usage specified.");
370c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray        }
371c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray        mRS.validate();
372c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray        mRS.nAllocationIoReceive(getID(mRS));
373c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray    }
37498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
37598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
3767d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Copy an array of RS objects to the Allocation.
37798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
37898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param d Source array.
37998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
38098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public void copyFrom(BaseObj[] d) {
38198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        mRS.validate();
38298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        validateIsObject();
38398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        if (d.length != mCurrentCount) {
38498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            throw new RSIllegalArgumentException("Array size mismatch, allocation sizeX = " +
38598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                                 mCurrentCount + ", array length = " + d.length);
38698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
38798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        int i[] = new int[d.length];
38898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        for (int ct=0; ct < d.length; ct++) {
38998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            i[ct] = d[ct].getID(mRS);
39098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
39198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        copy1DRangeFromUnchecked(0, mCurrentCount, i);
39298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
39398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
39498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    private void validateBitmapFormat(Bitmap b) {
39598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        Bitmap.Config bc = b.getConfig();
396c5953678decd7963ed0515c2a0b8fcc92b3f3b9fTim Murray        if (bc == null) {
397c5953678decd7963ed0515c2a0b8fcc92b3f3b9fTim Murray            throw new RSIllegalArgumentException("Bitmap has an unsupported format for this operation");
398c5953678decd7963ed0515c2a0b8fcc92b3f3b9fTim Murray        }
39998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        switch (bc) {
40098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        case ALPHA_8:
40198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            if (mType.getElement().mKind != Element.DataKind.PIXEL_A) {
40298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                throw new RSIllegalArgumentException("Allocation kind is " +
40398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                                     mType.getElement().mKind + ", type " +
40498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                                     mType.getElement().mType +
40598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                                     " of " + mType.getElement().getBytesSize() +
40698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                                     " bytes, passed bitmap was " + bc);
40798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            }
40898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            break;
40998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        case ARGB_8888:
41098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            if ((mType.getElement().mKind != Element.DataKind.PIXEL_RGBA) ||
41198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                (mType.getElement().getBytesSize() != 4)) {
41298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                throw new RSIllegalArgumentException("Allocation kind is " +
41398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                                     mType.getElement().mKind + ", type " +
41498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                                     mType.getElement().mType +
41598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                                     " of " + mType.getElement().getBytesSize() +
41698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                                     " bytes, passed bitmap was " + bc);
41798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            }
41898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            break;
41998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        case RGB_565:
42098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            if ((mType.getElement().mKind != Element.DataKind.PIXEL_RGB) ||
42198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                (mType.getElement().getBytesSize() != 2)) {
42298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                throw new RSIllegalArgumentException("Allocation kind is " +
42398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                                     mType.getElement().mKind + ", type " +
42498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                                     mType.getElement().mType +
42598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                                     " of " + mType.getElement().getBytesSize() +
42698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                                     " bytes, passed bitmap was " + bc);
42798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            }
42898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            break;
42998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        case ARGB_4444:
43098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            if ((mType.getElement().mKind != Element.DataKind.PIXEL_RGBA) ||
43198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                (mType.getElement().getBytesSize() != 2)) {
43298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                throw new RSIllegalArgumentException("Allocation kind is " +
43398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                                     mType.getElement().mKind + ", type " +
43498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                                     mType.getElement().mType +
43598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                                     " of " + mType.getElement().getBytesSize() +
43698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                                     " bytes, passed bitmap was " + bc);
43798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            }
43898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            break;
43937ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams
44098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
44198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
44298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
44398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    private void validateBitmapSize(Bitmap b) {
44498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        if((mCurrentDimX != b.getWidth()) || (mCurrentDimY != b.getHeight())) {
44598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            throw new RSIllegalArgumentException("Cannot update allocation from bitmap, sizes mismatch");
44698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
44798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
44898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
44998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
4507d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Copy into this Allocation from an array. This method does not guarantee
4517d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * that the Allocation is compatible with the input buffer; it copies memory
4527d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * without reinterpretation.
45398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
45498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param d the source data array
45598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
45698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public void copyFromUnchecked(int[] d) {
45798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        mRS.validate();
45837ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams        if (mCurrentDimZ > 0) {
45937ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams            copy3DRangeFromUnchecked(0, 0, 0, mCurrentDimX, mCurrentDimY, mCurrentDimZ, d);
46037ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams        } else if (mCurrentDimY > 0) {
4612da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines            copy2DRangeFromUnchecked(0, 0, mCurrentDimX, mCurrentDimY, d);
4622da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines        } else {
4632da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines            copy1DRangeFromUnchecked(0, mCurrentCount, d);
4642da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines        }
46598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
46698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
4677d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Copy into this Allocation from an array. This method does not guarantee
4687d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * that the Allocation is compatible with the input buffer; it copies memory
4697d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * without reinterpretation.
47098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
47198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param d the source data array
47298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
47398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public void copyFromUnchecked(short[] d) {
47498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        mRS.validate();
47537ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams        if (mCurrentDimZ > 0) {
47637ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams            copy3DRangeFromUnchecked(0, 0, 0, mCurrentDimX, mCurrentDimY, mCurrentDimZ, d);
47737ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams        } else if (mCurrentDimY > 0) {
4782da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines            copy2DRangeFromUnchecked(0, 0, mCurrentDimX, mCurrentDimY, d);
4792da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines        } else {
4802da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines            copy1DRangeFromUnchecked(0, mCurrentCount, d);
4812da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines        }
48298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
48398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
4847d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Copy into this Allocation from an array. This method does not guarantee
4857d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * that the Allocation is compatible with the input buffer; it copies memory
4867d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * without reinterpretation.
48798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
48898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param d the source data array
48998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
49098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public void copyFromUnchecked(byte[] d) {
49198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        mRS.validate();
49237ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams        if (mCurrentDimZ > 0) {
49337ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams            copy3DRangeFromUnchecked(0, 0, 0, mCurrentDimX, mCurrentDimY, mCurrentDimZ, d);
49437ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams        } else if (mCurrentDimY > 0) {
4952da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines            copy2DRangeFromUnchecked(0, 0, mCurrentDimX, mCurrentDimY, d);
4962da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines        } else {
4972da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines            copy1DRangeFromUnchecked(0, mCurrentCount, d);
4982da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines        }
49998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
50098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
5017d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Copy into this Allocation from an array. This method does not guarantee
5027d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * that the Allocation is compatible with the input buffer; it copies memory
5037d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * without reinterpretation.
50498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
50598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param d the source data array
50698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
50798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public void copyFromUnchecked(float[] d) {
50898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        mRS.validate();
50937ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams        if (mCurrentDimZ > 0) {
51037ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams            copy3DRangeFromUnchecked(0, 0, 0, mCurrentDimX, mCurrentDimY, mCurrentDimZ, d);
51137ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams        } else if (mCurrentDimY > 0) {
5122da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines            copy2DRangeFromUnchecked(0, 0, mCurrentDimX, mCurrentDimY, d);
5132da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines        } else {
5142da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines            copy1DRangeFromUnchecked(0, mCurrentCount, d);
5152da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines        }
51698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
51798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
51898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
5197d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Copy into this Allocation from an array.  This variant is type checked
5207d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * and will generate exceptions if the Allocation's {@link
52160c5b31f4448410221de043873b94797732afa66Stephen Hines     * android.support.v8.renderscript.Element} is not a 32 bit integer type.
52298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
52398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param d the source data array
52498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
52598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public void copyFrom(int[] d) {
52698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        mRS.validate();
52737ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams        if (mCurrentDimZ > 0) {
52837ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams            copy3DRangeFrom(0, 0, 0, mCurrentDimX, mCurrentDimY, mCurrentDimZ, d);
52937ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams        } else if (mCurrentDimY > 0) {
5302da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines            copy2DRangeFrom(0, 0, mCurrentDimX, mCurrentDimY, d);
5312da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines        } else {
5322da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines            copy1DRangeFrom(0, mCurrentCount, d);
5332da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines        }
53498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
53598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
53698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
5377d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Copy into this Allocation from an array.  This variant is type checked
5387d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * and will generate exceptions if the Allocation's {@link
53960c5b31f4448410221de043873b94797732afa66Stephen Hines     * android.support.v8.renderscript.Element} is not a 16 bit integer type.
54098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
54198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param d the source data array
54298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
54398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public void copyFrom(short[] d) {
54498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        mRS.validate();
54537ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams        if (mCurrentDimZ > 0) {
54637ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams            copy3DRangeFrom(0, 0, 0, mCurrentDimX, mCurrentDimY, mCurrentDimZ, d);
54737ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams        } else if (mCurrentDimY > 0) {
5482da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines            copy2DRangeFrom(0, 0, mCurrentDimX, mCurrentDimY, d);
5492da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines        } else {
5502da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines            copy1DRangeFrom(0, mCurrentCount, d);
5512da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines        }
55298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
55398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
55498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
5557d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Copy into this Allocation from an array.  This variant is type checked
5567d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * and will generate exceptions if the Allocation's {@link
55760c5b31f4448410221de043873b94797732afa66Stephen Hines     * android.support.v8.renderscript.Element} is not an 8 bit integer type.
55898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
55998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param d the source data array
56098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
56198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public void copyFrom(byte[] d) {
56298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        mRS.validate();
56337ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams        if (mCurrentDimZ > 0) {
56437ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams            copy3DRangeFrom(0, 0, 0, mCurrentDimX, mCurrentDimY, mCurrentDimZ, d);
56537ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams        } else if (mCurrentDimY > 0) {
5662da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines            copy2DRangeFrom(0, 0, mCurrentDimX, mCurrentDimY, d);
5672da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines        } else {
5682da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines            copy1DRangeFrom(0, mCurrentCount, d);
5692da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines        }
57098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
57198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
57298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
5737d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Copy into this Allocation from an array.  This variant is type checked
5747d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * and will generate exceptions if the Allocation's {@link
57560c5b31f4448410221de043873b94797732afa66Stephen Hines     * android.support.v8.renderscript.Element} is not a 32 bit float type.
57698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
57798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param d the source data array
57898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
57998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public void copyFrom(float[] d) {
58098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        mRS.validate();
58137ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams        if (mCurrentDimZ > 0) {
58237ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams            copy3DRangeFrom(0, 0, 0, mCurrentDimX, mCurrentDimY, mCurrentDimZ, d);
58337ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams        } else if (mCurrentDimY > 0) {
5842da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines            copy2DRangeFrom(0, 0, mCurrentDimX, mCurrentDimY, d);
5852da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines        } else {
5862da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines            copy1DRangeFrom(0, mCurrentCount, d);
5872da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines        }
58898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
58998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
59098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
5917d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Copy into an Allocation from a {@link android.graphics.Bitmap}.  The
5927d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * height, width, and format of the bitmap must match the existing
5937d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * allocation.
5947d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     *
5957d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * <p>If the {@link android.graphics.Bitmap} is the same as the {@link
5967d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * android.graphics.Bitmap} used to create the Allocation with {@link
5977d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * #createFromBitmap} and {@link #USAGE_SHARED} is set on the Allocation,
5987d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * this will synchronize the Allocation with the latest data from the {@link
5997d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * android.graphics.Bitmap}, potentially avoiding the actual copy.</p>
60098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
60198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param b the source bitmap
60298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
60398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public void copyFrom(Bitmap b) {
60498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        mRS.validate();
605c5953678decd7963ed0515c2a0b8fcc92b3f3b9fTim Murray        if (b.getConfig() == null) {
606c5953678decd7963ed0515c2a0b8fcc92b3f3b9fTim Murray            Bitmap newBitmap = Bitmap.createBitmap(b.getWidth(), b.getHeight(), Bitmap.Config.ARGB_8888);
607c5953678decd7963ed0515c2a0b8fcc92b3f3b9fTim Murray            Canvas c = new Canvas(newBitmap);
608c5953678decd7963ed0515c2a0b8fcc92b3f3b9fTim Murray            c.drawBitmap(b, 0, 0, null);
609c5953678decd7963ed0515c2a0b8fcc92b3f3b9fTim Murray            copyFrom(newBitmap);
610c5953678decd7963ed0515c2a0b8fcc92b3f3b9fTim Murray            return;
611c5953678decd7963ed0515c2a0b8fcc92b3f3b9fTim Murray        }
61298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        validateBitmapSize(b);
61398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        validateBitmapFormat(b);
61498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        mRS.nAllocationCopyFromBitmap(getID(mRS), b);
61598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
61698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
61798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
6187d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Copy an Allocation from an Allocation.  The types of both allocations
6191b370e358d16cc3b50b169511d6b387db09f972dJason Sams     * must be identical.
6201b370e358d16cc3b50b169511d6b387db09f972dJason Sams     *
6211b370e358d16cc3b50b169511d6b387db09f972dJason Sams     * @param a the source allocation
6221b370e358d16cc3b50b169511d6b387db09f972dJason Sams     */
6231b370e358d16cc3b50b169511d6b387db09f972dJason Sams    public void copyFrom(Allocation a) {
6241b370e358d16cc3b50b169511d6b387db09f972dJason Sams        mRS.validate();
6251b370e358d16cc3b50b169511d6b387db09f972dJason Sams        if (!mType.equals(a.getType())) {
6261b370e358d16cc3b50b169511d6b387db09f972dJason Sams            throw new RSIllegalArgumentException("Types of allocations must match.");
6271b370e358d16cc3b50b169511d6b387db09f972dJason Sams        }
6281b370e358d16cc3b50b169511d6b387db09f972dJason Sams        copy2DRangeFrom(0, 0, mCurrentDimX, mCurrentDimY, a, 0, 0);
6291b370e358d16cc3b50b169511d6b387db09f972dJason Sams    }
6301b370e358d16cc3b50b169511d6b387db09f972dJason Sams
6311b370e358d16cc3b50b169511d6b387db09f972dJason Sams
6321b370e358d16cc3b50b169511d6b387db09f972dJason Sams    /**
6337d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * This is only intended to be used by auto-generated code reflected from
6347d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * the RenderScript script files and should not be used by developers.
63598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
63698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param xoff
63798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param fp
63898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
63998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public void setFromFieldPacker(int xoff, FieldPacker fp) {
64098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        mRS.validate();
64198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        int eSize = mType.mElement.getBytesSize();
64298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        final byte[] data = fp.getData();
64398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
64498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        int count = data.length / eSize;
64598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        if ((eSize * count) != data.length) {
64698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            throw new RSIllegalArgumentException("Field packer length " + data.length +
64798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                               " not divisible by element size " + eSize + ".");
64898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
64998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        copy1DRangeFromUnchecked(xoff, count, data);
65098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
65198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
65298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
6537d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * This is only intended to be used by auto-generated code reflected from
6547d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * the RenderScript script files.
65598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
65698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param xoff
65798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param component_number
65898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param fp
65998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
66098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public void setFromFieldPacker(int xoff, int component_number, FieldPacker fp) {
66198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        mRS.validate();
66298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        if (component_number >= mType.mElement.mElements.length) {
66398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            throw new RSIllegalArgumentException("Component_number " + component_number + " out of range.");
66498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
66598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        if(xoff < 0) {
66698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            throw new RSIllegalArgumentException("Offset must be >= 0.");
66798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
66898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
66998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        final byte[] data = fp.getData();
67098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        int eSize = mType.mElement.mElements[component_number].getBytesSize();
67198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        eSize *= mType.mElement.mArraySizes[component_number];
67298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
67398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        if (data.length != eSize) {
67498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            throw new RSIllegalArgumentException("Field packer sizelength " + data.length +
67598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                               " does not match component size " + eSize + ".");
67698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
67798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
67898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        mRS.nAllocationElementData1D(getIDSafe(), xoff, mSelectedLOD,
67998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                     component_number, data, data.length);
68098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
68198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
68298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    private void data1DChecks(int off, int count, int len, int dataSize) {
68398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        mRS.validate();
68498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        if(off < 0) {
68598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            throw new RSIllegalArgumentException("Offset must be >= 0.");
68698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
68798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        if(count < 1) {
68898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            throw new RSIllegalArgumentException("Count must be >= 1.");
68998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
69098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        if((off + count) > mCurrentCount) {
69198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            throw new RSIllegalArgumentException("Overflow, Available count " + mCurrentCount +
69298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                               ", got " + count + " at offset " + off + ".");
69398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
69498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        if(len < dataSize) {
69598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            throw new RSIllegalArgumentException("Array too small for allocation type.");
69698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
69798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
69898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
69998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
7007d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Generate a mipmap chain. This is only valid if the Type of the Allocation
7017d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * includes mipmaps.
70298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
7037d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * <p>This function will generate a complete set of mipmaps from the top
7047d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * level LOD and place them into the script memory space.</p>
70598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
7067d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * <p>If the Allocation is also using other memory spaces, a call to {@link
7077d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * #syncAll syncAll(Allocation.USAGE_SCRIPT)} is required.</p>
70898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
70998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public void generateMipmaps() {
71098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        mRS.nAllocationGenerateMipmaps(getID(mRS));
71198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
71298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
71398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
7147d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Copy an array into part of this Allocation.  This method does not
7157d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * guarantee that the Allocation is compatible with the input buffer.
71698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
71798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param off The offset of the first element to be copied.
71898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param count The number of elements to be copied.
71998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param d the source data array
72098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
72198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public void copy1DRangeFromUnchecked(int off, int count, int[] d) {
72298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        int dataSize = mType.mElement.getBytesSize() * count;
72398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        data1DChecks(off, count, d.length * 4, dataSize);
72498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        mRS.nAllocationData1D(getIDSafe(), off, mSelectedLOD, count, d, dataSize);
72598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
72698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
7277d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Copy an array into part of this Allocation.  This method does not
7287d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * guarantee that the Allocation is compatible with the input buffer.
72998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
73098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param off The offset of the first element to be copied.
73198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param count The number of elements to be copied.
73298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param d the source data array
73398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
73498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public void copy1DRangeFromUnchecked(int off, int count, short[] d) {
73598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        int dataSize = mType.mElement.getBytesSize() * count;
73698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        data1DChecks(off, count, d.length * 2, dataSize);
73798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        mRS.nAllocationData1D(getIDSafe(), off, mSelectedLOD, count, d, dataSize);
73898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
73998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
7407d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Copy an array into part of this Allocation.  This method does not
7417d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * guarantee that the Allocation is compatible with the input buffer.
74298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
74398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param off The offset of the first element to be copied.
74498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param count The number of elements to be copied.
74598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param d the source data array
74698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
74798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public void copy1DRangeFromUnchecked(int off, int count, byte[] d) {
74898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        int dataSize = mType.mElement.getBytesSize() * count;
74998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        data1DChecks(off, count, d.length, dataSize);
75098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        mRS.nAllocationData1D(getIDSafe(), off, mSelectedLOD, count, d, dataSize);
75198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
75298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
7537d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Copy an array into part of this Allocation.  This method does not
7547d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * guarantee that the Allocation is compatible with the input buffer.
75598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
75698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param off The offset of the first element to be copied.
75798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param count The number of elements to be copied.
75898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param d the source data array
75998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
76098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public void copy1DRangeFromUnchecked(int off, int count, float[] d) {
76198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        int dataSize = mType.mElement.getBytesSize() * count;
76298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        data1DChecks(off, count, d.length * 4, dataSize);
76398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        mRS.nAllocationData1D(getIDSafe(), off, mSelectedLOD, count, d, dataSize);
76498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
76598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
76698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
7677d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Copy an array into part of this Allocation.  This variant is type checked
7687d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * and will generate exceptions if the Allocation type is not a 32 bit
7697d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * integer type.
77098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
77198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param off The offset of the first element to be copied.
77298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param count The number of elements to be copied.
77398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param d the source data array
77498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
77598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public void copy1DRangeFrom(int off, int count, int[] d) {
77698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        validateIsInt32();
77798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        copy1DRangeFromUnchecked(off, count, d);
77898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
77998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
78098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
7817d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Copy an array into part of this Allocation.  This variant is type checked
7827d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * and will generate exceptions if the Allocation type is not a 16 bit
7837d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * integer type.
78498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
78598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param off The offset of the first element to be copied.
78698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param count The number of elements to be copied.
78798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param d the source data array
78898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
78998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public void copy1DRangeFrom(int off, int count, short[] d) {
79098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        validateIsInt16();
79198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        copy1DRangeFromUnchecked(off, count, d);
79298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
79398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
79498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
7957d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Copy an array into part of this Allocation.  This variant is type checked
7967d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * and will generate exceptions if the Allocation type is not an 8 bit
7977d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * integer type.
79898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
79998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param off The offset of the first element to be copied.
80098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param count The number of elements to be copied.
80198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param d the source data array
80298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
80398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public void copy1DRangeFrom(int off, int count, byte[] d) {
80498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        validateIsInt8();
80598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        copy1DRangeFromUnchecked(off, count, d);
80698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
80798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
80898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
8097d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Copy an array into part of this Allocation.  This variant is type checked
8107d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * and will generate exceptions if the Allocation type is not a 32 bit float
8117d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * type.
81298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
81398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param off The offset of the first element to be copied.
81498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param count The number of elements to be copied.
81598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param d the source data array.
81698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
81798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public void copy1DRangeFrom(int off, int count, float[] d) {
81898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        validateIsFloat32();
81998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        copy1DRangeFromUnchecked(off, count, d);
82098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
82198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
82298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     /**
8237d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Copy part of an Allocation into this Allocation.
82498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
82598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param off The offset of the first element to be copied.
82698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param count The number of elements to be copied.
82798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param data the source data allocation.
82898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param dataOff off The offset of the first element in data to
82998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *          be copied.
83098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
83198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public void copy1DRangeFrom(int off, int count, Allocation data, int dataOff) {
83298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        mRS.nAllocationData2D(getIDSafe(), off, 0,
83398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                              mSelectedLOD, mSelectedFace.mID,
83498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                              count, 1, data.getID(mRS), dataOff, 0,
83598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                              data.mSelectedLOD, data.mSelectedFace.mID);
83698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
83798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
83898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    private void validate2DRange(int xoff, int yoff, int w, int h) {
83998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        if (mAdaptedAllocation != null) {
84098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
84198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        } else {
84298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
84398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            if (xoff < 0 || yoff < 0) {
84498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                throw new RSIllegalArgumentException("Offset cannot be negative.");
84598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            }
84698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            if (h < 0 || w < 0) {
84798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                throw new RSIllegalArgumentException("Height or width cannot be negative.");
84898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            }
84998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            if (((xoff + w) > mCurrentDimX) || ((yoff + h) > mCurrentDimY)) {
85098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                throw new RSIllegalArgumentException("Updated region larger than allocation.");
85198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            }
85298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
85398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
85498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
8552da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines    void copy2DRangeFromUnchecked(int xoff, int yoff, int w, int h, byte[] data) {
85698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        mRS.validate();
85798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        validate2DRange(xoff, yoff, w, h);
85898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        mRS.nAllocationData2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID,
85998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                              w, h, data, data.length);
86098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
86198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
8622da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines    void copy2DRangeFromUnchecked(int xoff, int yoff, int w, int h, short[] data) {
86398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        mRS.validate();
86498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        validate2DRange(xoff, yoff, w, h);
86598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        mRS.nAllocationData2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID,
86698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                              w, h, data, data.length * 2);
86798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
86898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
8692da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines    void copy2DRangeFromUnchecked(int xoff, int yoff, int w, int h, int[] data) {
87098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        mRS.validate();
87198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        validate2DRange(xoff, yoff, w, h);
87298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        mRS.nAllocationData2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID,
87398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                              w, h, data, data.length * 4);
87498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
87598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
8762da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines    void copy2DRangeFromUnchecked(int xoff, int yoff, int w, int h, float[] data) {
87798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        mRS.validate();
87898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        validate2DRange(xoff, yoff, w, h);
87998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        mRS.nAllocationData2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID,
88098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                              w, h, data, data.length * 4);
88198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
88298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
8832da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines
8842da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines    /**
8857d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Copy from an array into a rectangular region in this Allocation.  The
8867d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * array is assumed to be tightly packed.
8872da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines     *
8887d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param xoff X offset of the region to update in this Allocation
8897d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param yoff Y offset of the region to update in this Allocation
8907d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param w Width of the region to update
8917d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param h Height of the region to update
8927d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param data to be placed into the Allocation
8932da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines     */
8942da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines    public void copy2DRangeFrom(int xoff, int yoff, int w, int h, byte[] data) {
8952da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines        validateIsInt8();
8962da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines        copy2DRangeFromUnchecked(xoff, yoff, w, h, data);
8972da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines    }
8982da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines
8997d435ae5ba100be5710b685653cc351cab159c11Stephen Hines    /**
9007d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Copy from an array into a rectangular region in this Allocation.  The
9017d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * array is assumed to be tightly packed.
9027d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     *
9037d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param xoff X offset of the region to update in this Allocation
9047d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param yoff Y offset of the region to update in this Allocation
9057d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param w Width of the region to update
9067d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param h Height of the region to update
9077d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param data to be placed into the Allocation
9087d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     */
9092da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines    public void copy2DRangeFrom(int xoff, int yoff, int w, int h, short[] data) {
9102da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines        validateIsInt16();
9112da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines        copy2DRangeFromUnchecked(xoff, yoff, w, h, data);
9122da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines    }
9132da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines
9147d435ae5ba100be5710b685653cc351cab159c11Stephen Hines    /**
9157d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Copy from an array into a rectangular region in this Allocation.  The
9167d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * array is assumed to be tightly packed.
9177d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     *
9187d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param xoff X offset of the region to update in this Allocation
9197d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param yoff Y offset of the region to update in this Allocation
9207d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param w Width of the region to update
9217d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param h Height of the region to update
9227d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param data to be placed into the Allocation
9237d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     */
9242da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines    public void copy2DRangeFrom(int xoff, int yoff, int w, int h, int[] data) {
9252da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines        validateIsInt32();
9262da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines        copy2DRangeFromUnchecked(xoff, yoff, w, h, data);
9272da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines    }
9282da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines
9297d435ae5ba100be5710b685653cc351cab159c11Stephen Hines    /**
9307d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Copy from an array into a rectangular region in this Allocation.  The
9317d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * array is assumed to be tightly packed.
9327d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     *
9337d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param xoff X offset of the region to update in this Allocation
9347d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param yoff Y offset of the region to update in this Allocation
9357d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param w Width of the region to update
9367d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param h Height of the region to update
9377d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param data to be placed into the Allocation
9387d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     */
9392da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines    public void copy2DRangeFrom(int xoff, int yoff, int w, int h, float[] data) {
9402da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines        validateIsFloat32();
9412da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines        copy2DRangeFromUnchecked(xoff, yoff, w, h, data);
9422da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines    }
9432da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines
94498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
9457d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Copy a rectangular region from an Allocation into a rectangular region in
9467d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * this Allocation.
94798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
9487d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param xoff X offset of the region in this Allocation
9497d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param yoff Y offset of the region in this Allocation
9507d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param w Width of the region to update.
9517d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param h Height of the region to update.
9527d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param data source Allocation.
9537d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param dataXoff X offset in source Allocation
9547d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param dataYoff Y offset in source Allocation
95598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
95698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public void copy2DRangeFrom(int xoff, int yoff, int w, int h,
95798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                Allocation data, int dataXoff, int dataYoff) {
95898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        mRS.validate();
95998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        validate2DRange(xoff, yoff, w, h);
96098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        mRS.nAllocationData2D(getIDSafe(), xoff, yoff,
96198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                              mSelectedLOD, mSelectedFace.mID,
96298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                              w, h, data.getID(mRS), dataXoff, dataYoff,
96398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                              data.mSelectedLOD, data.mSelectedFace.mID);
96498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
96598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
96698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
9677d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Copy a {@link android.graphics.Bitmap} into an Allocation.  The height
9687d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * and width of the update will use the height and width of the {@link
9697d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * android.graphics.Bitmap}.
97098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
9717d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param xoff X offset of the region to update in this Allocation
9727d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param yoff Y offset of the region to update in this Allocation
9737d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param data the Bitmap to be copied
97498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
97598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public void copy2DRangeFrom(int xoff, int yoff, Bitmap data) {
97698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        mRS.validate();
977c5953678decd7963ed0515c2a0b8fcc92b3f3b9fTim Murray        if (data.getConfig() == null) {
978c5953678decd7963ed0515c2a0b8fcc92b3f3b9fTim Murray            Bitmap newBitmap = Bitmap.createBitmap(data.getWidth(), data.getHeight(), Bitmap.Config.ARGB_8888);
979c5953678decd7963ed0515c2a0b8fcc92b3f3b9fTim Murray            Canvas c = new Canvas(newBitmap);
980c5953678decd7963ed0515c2a0b8fcc92b3f3b9fTim Murray            c.drawBitmap(data, 0, 0, null);
981c5953678decd7963ed0515c2a0b8fcc92b3f3b9fTim Murray            copy2DRangeFrom(xoff, yoff, newBitmap);
98237ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams            return;
983c5953678decd7963ed0515c2a0b8fcc92b3f3b9fTim Murray        }
98498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        validateBitmapFormat(data);
98598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        validate2DRange(xoff, yoff, data.getWidth(), data.getHeight());
98698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        mRS.nAllocationData2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID, data);
98798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
98898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
98937ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams    private void validate3DRange(int xoff, int yoff, int zoff, int w, int h, int d) {
99037ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams        if (mAdaptedAllocation != null) {
99137ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams
99237ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams        } else {
99337ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams
99437ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams            if (xoff < 0 || yoff < 0 || zoff < 0) {
99537ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams                throw new RSIllegalArgumentException("Offset cannot be negative.");
99637ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams            }
99737ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams            if (h < 0 || w < 0 || d < 0) {
99837ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams                throw new RSIllegalArgumentException("Height or width cannot be negative.");
99937ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams            }
100037ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams            if (((xoff + w) > mCurrentDimX) || ((yoff + h) > mCurrentDimY) || ((zoff + d) > mCurrentDimZ)) {
100137ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams                throw new RSIllegalArgumentException("Updated region larger than allocation.");
100237ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams            }
100337ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams        }
100437ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams    }
100537ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams
100637ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams    /**
100737ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams     * @hide
100837ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams     *
100937ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams     */
101037ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams    void copy3DRangeFromUnchecked(int xoff, int yoff, int zoff, int w, int h, int d, byte[] data) {
101137ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams        mRS.validate();
101237ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams        validate3DRange(xoff, yoff, zoff, w, h, d);
101337ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams        mRS.nAllocationData3D(getIDSafe(), xoff, yoff, zoff, mSelectedLOD,
101437ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams                              w, h, d, data, data.length);
101537ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams    }
101637ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams
101737ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams    /**
101837ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams     * @hide
101937ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams     *
102037ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams     */
102137ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams    void copy3DRangeFromUnchecked(int xoff, int yoff, int zoff, int w, int h, int d, short[] data) {
102237ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams        mRS.validate();
102337ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams        validate3DRange(xoff, yoff, zoff, w, h, d);
102437ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams        mRS.nAllocationData3D(getIDSafe(), xoff, yoff, zoff, mSelectedLOD,
102537ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams                              w, h, d, data, data.length * 2);
102637ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams    }
102737ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams
102837ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams    /**
102937ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams     * @hide
103037ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams     *
103137ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams     */
103237ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams    void copy3DRangeFromUnchecked(int xoff, int yoff, int zoff, int w, int h, int d, int[] data) {
103337ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams        mRS.validate();
103437ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams        validate3DRange(xoff, yoff, zoff, w, h, d);
103537ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams        mRS.nAllocationData3D(getIDSafe(), xoff, yoff, zoff, mSelectedLOD,
103637ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams                              w, h, d, data, data.length * 4);
103737ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams    }
103898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
103998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
104037ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams     * @hide
104137ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams     *
104237ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams     */
104337ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams    void copy3DRangeFromUnchecked(int xoff, int yoff, int zoff, int w, int h, int d, float[] data) {
104437ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams        mRS.validate();
104537ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams        validate3DRange(xoff, yoff, zoff, w, h, d);
104637ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams        mRS.nAllocationData3D(getIDSafe(), xoff, yoff, zoff, mSelectedLOD,
104737ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams                              w, h, d, data, data.length * 4);
104837ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams    }
104937ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams
105037ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams
105137ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams    /**
105237ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams     * @hide
105337ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams     * Copy a rectangular region from the array into the allocation.
10547d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * The array is assumed to be tightly packed.
10557d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     *
10567d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param xoff X offset of the region to update in this Allocation
10577d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param yoff Y offset of the region to update in this Allocation
10587d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param zoff Z offset of the region to update in this Allocation
10597d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param w Width of the region to update
10607d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param h Height of the region to update
10617d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param d Depth of the region to update
106237ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams     * @param data to be placed into the allocation
106337ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams     */
106437ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams    public void copy3DRangeFrom(int xoff, int yoff, int zoff, int w, int h, int d, byte[] data) {
106537ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams        validateIsInt8();
106637ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams        copy3DRangeFromUnchecked(xoff, yoff, zoff, w, h, d, data);
106737ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams    }
106837ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams
106937ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams    /**
107037ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams     * @hide
107137ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams     *
107237ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams     */
107337ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams    public void copy3DRangeFrom(int xoff, int yoff, int zoff, int w, int h, int d, short[] data) {
107437ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams        validateIsInt16();
107537ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams        copy3DRangeFromUnchecked(xoff, yoff, zoff, w, h, d, data);
107637ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams    }
107737ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams
107837ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams    /**
107937ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams     * @hide
108037ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams     *
108137ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams     */
108237ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams    public void copy3DRangeFrom(int xoff, int yoff, int zoff, int w, int h, int d, int[] data) {
108337ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams        validateIsInt32();
108437ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams        copy3DRangeFromUnchecked(xoff, yoff, zoff, w, h, d, data);
108537ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams    }
108637ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams
108737ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams    /**
108837ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams     * @hide
108937ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams     *
109037ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams     */
109137ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams    public void copy3DRangeFrom(int xoff, int yoff, int zoff, int w, int h, int d, float[] data) {
109237ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams        validateIsFloat32();
109337ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams        copy3DRangeFromUnchecked(xoff, yoff, zoff, w, h, d, data);
109437ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams    }
109537ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams
109637ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams    /**
109737ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams     * @hide
109837ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams     * Copy a rectangular region into the allocation from another
109937ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams     * allocation.
110037ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams     *
11017d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param xoff X offset of the region to update in this Allocation
11027d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param yoff Y offset of the region to update in this Allocation
11037d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param zoff Z offset of the region to update in this Allocation
11047d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param w Width of the region to update.
11057d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param h Height of the region to update.
11067d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param d Depth of the region to update.
110737ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams     * @param data source allocation.
11087d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param dataXoff X offset of the region in the source Allocation
11097d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param dataYoff Y offset of the region in the source Allocation
11107d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param dataZoff Z offset of the region in the source Allocation
111137ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams     */
111237ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams    public void copy3DRangeFrom(int xoff, int yoff, int zoff, int w, int h, int d,
111337ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams                                Allocation data, int dataXoff, int dataYoff, int dataZoff) {
111437ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams        mRS.validate();
111537ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams        validate3DRange(xoff, yoff, zoff, w, h, d);
111637ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams        mRS.nAllocationData3D(getIDSafe(), xoff, yoff, zoff, mSelectedLOD,
111737ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams                              w, h, d, data.getID(mRS), dataXoff, dataYoff, dataZoff,
111837ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams                              data.mSelectedLOD);
111937ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams    }
112037ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams
112137ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams
112237ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams    /**
11237d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Copy from the Allocation into a {@link android.graphics.Bitmap}.  The
11247d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * bitmap must match the dimensions of the Allocation.
112598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
112698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param b The bitmap to be set from the Allocation.
112798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
112898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public void copyTo(Bitmap b) {
112998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        mRS.validate();
113098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        validateBitmapFormat(b);
113198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        validateBitmapSize(b);
113298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        mRS.nAllocationCopyToBitmap(getID(mRS), b);
113398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
113498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
113598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
11367d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Copy from the Allocation into a byte array.  The array must be at least
11377d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * as large as the Allocation.  The allocation must be of an 8 bit integer
113860c5b31f4448410221de043873b94797732afa66Stephen Hines     * {@link android.support.v8.renderscript.Element} type.
113998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
114098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param d The array to be set from the Allocation.
114198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
114298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public void copyTo(byte[] d) {
114398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        validateIsInt8();
114498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        mRS.validate();
114598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        mRS.nAllocationRead(getID(mRS), d);
114698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
114798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
114898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
11497d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Copy from the Allocation into a short array.  The array must be at least
11507d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * as large as the Allocation.  The allocation must be of an 16 bit integer
115160c5b31f4448410221de043873b94797732afa66Stephen Hines     * {@link android.support.v8.renderscript.Element} type.
115298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
115398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param d The array to be set from the Allocation.
115498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
115598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public void copyTo(short[] d) {
115698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        validateIsInt16();
115798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        mRS.validate();
115898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        mRS.nAllocationRead(getID(mRS), d);
115998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
116098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
116198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
11627d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Copy from the Allocation into a int array.  The array must be at least as
11637d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * large as the Allocation.  The allocation must be of an 32 bit integer
116460c5b31f4448410221de043873b94797732afa66Stephen Hines     * {@link android.support.v8.renderscript.Element} type.
116598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
116698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param d The array to be set from the Allocation.
116798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
116898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public void copyTo(int[] d) {
116998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        validateIsInt32();
117098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        mRS.validate();
117198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        mRS.nAllocationRead(getID(mRS), d);
117298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
117398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
117498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
11757d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Copy from the Allocation into a float array.  The array must be at least
11767d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * as large as the Allocation.  The allocation must be of an 32 bit float
117760c5b31f4448410221de043873b94797732afa66Stephen Hines     * {@link android.support.v8.renderscript.Element} type.
117898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
117998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param d The array to be set from the Allocation.
118098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
118198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public void copyTo(float[] d) {
118298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        validateIsFloat32();
118398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        mRS.validate();
118498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        mRS.nAllocationRead(getID(mRS), d);
118598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
118698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
118798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    // creation
118898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
118998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    static BitmapFactory.Options mBitmapOptions = new BitmapFactory.Options();
119098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    static {
119198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        mBitmapOptions.inScaled = false;
119298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
119398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
119498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
11957d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Creates a new Allocation with the given {@link
119660c5b31f4448410221de043873b94797732afa66Stephen Hines     * android.support.v8.renderscript.Type}, mipmap flag, and usage flags.
119798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
11987d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param type RenderScript type describing data layout
119998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param mips specifies desired mipmap behaviour for the
120098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *             allocation
12017d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param usage bit field specifying how the Allocation is
120298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *              utilized
120398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
120498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    static public Allocation createTyped(RenderScript rs, Type type, MipmapControl mips, int usage) {
1205ce8b0e674c93035013d1c33aaabc9bb6ceffde0fTim Murray        if (rs.isNative) {
1206ce8b0e674c93035013d1c33aaabc9bb6ceffde0fTim Murray            RenderScriptThunker rst = (RenderScriptThunker)rs;
1207ce8b0e674c93035013d1c33aaabc9bb6ceffde0fTim Murray            return AllocationThunker.createTyped(rst, type, mips, usage);
1208ce8b0e674c93035013d1c33aaabc9bb6ceffde0fTim Murray        }
120998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        rs.validate();
121098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        if (type.getID(rs) == 0) {
121198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            throw new RSInvalidStateException("Bad Type");
121298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
121398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        int id = rs.nAllocationCreateTyped(type.getID(rs), mips.mID, usage, 0);
121498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        if (id == 0) {
121598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            throw new RSRuntimeException("Allocation creation failed.");
121698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
121798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        return new Allocation(id, rs, type, usage);
121898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
121998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
122098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
12217d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Creates an Allocation with the size specified by the type and no mipmaps
12227d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * generated by default
122398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
122498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param rs Context to which the allocation will belong.
122598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param type renderscript type describing data layout
122698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param usage bit field specifying how the allocation is
122798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *              utilized
122898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
122998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @return allocation
123098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
123198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    static public Allocation createTyped(RenderScript rs, Type type, int usage) {
123298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        return createTyped(rs, type, MipmapControl.MIPMAP_NONE, usage);
123398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
123498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
123598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
12367d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Creates an Allocation for use by scripts with a given {@link
123760c5b31f4448410221de043873b94797732afa66Stephen Hines     * android.support.v8.renderscript.Type} and no mipmaps
123898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
12397d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param rs Context to which the Allocation will belong.
12407d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param type RenderScript Type describing data layout
124198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
124298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @return allocation
124398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
124498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    static public Allocation createTyped(RenderScript rs, Type type) {
124598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        return createTyped(rs, type, MipmapControl.MIPMAP_NONE, USAGE_SCRIPT);
124698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
124798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
124898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
12497d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Creates an Allocation with a specified number of given elements
125098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
12517d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param rs Context to which the Allocation will belong.
12527d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param e Element to use in the Allocation
12537d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param count the number of Elements in the Allocation
12547d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param usage bit field specifying how the Allocation is
125598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *              utilized
125698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
125798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @return allocation
125898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
125998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    static public Allocation createSized(RenderScript rs, Element e,
126098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                         int count, int usage) {
1261ce8b0e674c93035013d1c33aaabc9bb6ceffde0fTim Murray        if (rs.isNative) {
1262ce8b0e674c93035013d1c33aaabc9bb6ceffde0fTim Murray            RenderScriptThunker rst = (RenderScriptThunker)rs;
1263ce8b0e674c93035013d1c33aaabc9bb6ceffde0fTim Murray            return AllocationThunker.createSized(rs, e, count, usage);
1264ce8b0e674c93035013d1c33aaabc9bb6ceffde0fTim Murray        }
126598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        rs.validate();
126698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        Type.Builder b = new Type.Builder(rs, e);
126798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        b.setX(count);
126898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        Type t = b.create();
126998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
127098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        int id = rs.nAllocationCreateTyped(t.getID(rs), MipmapControl.MIPMAP_NONE.mID, usage, 0);
127198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        if (id == 0) {
127298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            throw new RSRuntimeException("Allocation creation failed.");
127398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
127498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        return new Allocation(id, rs, t, usage);
127598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
127698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
127798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
12787d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Creates an Allocation with a specified number of given elements
127998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
12807d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param rs Context to which the Allocation will belong.
12817d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param e Element to use in the Allocation
12827d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param count the number of Elements in the Allocation
128398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
128498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @return allocation
128598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
128698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    static public Allocation createSized(RenderScript rs, Element e, int count) {
128798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        return createSized(rs, e, count, USAGE_SCRIPT);
128898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
128998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
129098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    static Element elementFromBitmap(RenderScript rs, Bitmap b) {
129198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        final Bitmap.Config bc = b.getConfig();
129298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        if (bc == Bitmap.Config.ALPHA_8) {
129398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            return Element.A_8(rs);
129498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
129598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        if (bc == Bitmap.Config.ARGB_4444) {
129698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            return Element.RGBA_4444(rs);
129798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
129898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        if (bc == Bitmap.Config.ARGB_8888) {
129998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            return Element.RGBA_8888(rs);
130098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
130198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        if (bc == Bitmap.Config.RGB_565) {
130298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            return Element.RGB_565(rs);
130398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
130498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        throw new RSInvalidStateException("Bad bitmap type: " + bc);
130598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
130698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
130798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    static Type typeFromBitmap(RenderScript rs, Bitmap b,
130898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                       MipmapControl mip) {
130998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        Element e = elementFromBitmap(rs, b);
131098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        Type.Builder tb = new Type.Builder(rs, e);
131198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        tb.setX(b.getWidth());
131298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        tb.setY(b.getHeight());
131398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        tb.setMipmaps(mip == MipmapControl.MIPMAP_FULL);
131498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        return tb.create();
131598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
131698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
131798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
13187d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Creates an Allocation from a {@link android.graphics.Bitmap}.
131998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
132098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param rs Context to which the allocation will belong.
13217d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param b Bitmap source for the allocation data
132298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param mips specifies desired mipmap behaviour for the
132398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *             allocation
132498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param usage bit field specifying how the allocation is
132598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *              utilized
132698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
13277d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @return Allocation containing bitmap data
132898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
132998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
133098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    static public Allocation createFromBitmap(RenderScript rs, Bitmap b,
133198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                              MipmapControl mips,
133298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                              int usage) {
1333ce8b0e674c93035013d1c33aaabc9bb6ceffde0fTim Murray        if (rs.isNative) {
1334ce8b0e674c93035013d1c33aaabc9bb6ceffde0fTim Murray            RenderScriptThunker rst = (RenderScriptThunker)rs;
1335ce8b0e674c93035013d1c33aaabc9bb6ceffde0fTim Murray            return AllocationThunker.createFromBitmap(rst, b, mips, usage);
1336ce8b0e674c93035013d1c33aaabc9bb6ceffde0fTim Murray        }
133798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        rs.validate();
1338c5953678decd7963ed0515c2a0b8fcc92b3f3b9fTim Murray
1339c5953678decd7963ed0515c2a0b8fcc92b3f3b9fTim Murray        // WAR undocumented color formats
1340c5953678decd7963ed0515c2a0b8fcc92b3f3b9fTim Murray        if (b.getConfig() == null) {
1341c5953678decd7963ed0515c2a0b8fcc92b3f3b9fTim Murray            if ((usage & USAGE_SHARED) != 0) {
1342c5953678decd7963ed0515c2a0b8fcc92b3f3b9fTim Murray                throw new RSIllegalArgumentException("USAGE_SHARED cannot be used with a Bitmap that has a null config.");
1343c5953678decd7963ed0515c2a0b8fcc92b3f3b9fTim Murray            }
1344c5953678decd7963ed0515c2a0b8fcc92b3f3b9fTim Murray            Bitmap newBitmap = Bitmap.createBitmap(b.getWidth(), b.getHeight(), Bitmap.Config.ARGB_8888);
1345c5953678decd7963ed0515c2a0b8fcc92b3f3b9fTim Murray            Canvas c = new Canvas(newBitmap);
1346c5953678decd7963ed0515c2a0b8fcc92b3f3b9fTim Murray            c.drawBitmap(b, 0, 0, null);
1347c5953678decd7963ed0515c2a0b8fcc92b3f3b9fTim Murray            return createFromBitmap(rs, newBitmap, mips, usage);
1348c5953678decd7963ed0515c2a0b8fcc92b3f3b9fTim Murray        }
1349c5953678decd7963ed0515c2a0b8fcc92b3f3b9fTim Murray
135098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        Type t = typeFromBitmap(rs, b, mips);
135198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
1352c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray        // enable optimized bitmap path only with no mipmap and script-only usage
1353c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray        if (mips == MipmapControl.MIPMAP_NONE &&
1354c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray            t.getElement().isCompatible(Element.RGBA_8888(rs)) &&
13550f5bae87e2e3e3b0e66803122b5c4c7dd36d43ddStephen Hines            usage == (USAGE_SHARED | USAGE_SCRIPT | USAGE_GRAPHICS_TEXTURE)) {
1356c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray            int id = rs.nAllocationCreateBitmapBackedAllocation(t.getID(rs), mips.mID, b, usage);
1357c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray            if (id == 0) {
1358c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray                throw new RSRuntimeException("Load failed.");
1359c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray            }
1360c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray
1361c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray            // keep a reference to the Bitmap around to prevent GC
1362c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray            Allocation alloc = new Allocation(id, rs, t, usage);
1363c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray            alloc.setBitmap(b);
1364c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray            return alloc;
1365c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray        }
1366c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray
1367c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray
136898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        int id = rs.nAllocationCreateFromBitmap(t.getID(rs), mips.mID, b, usage);
136998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        if (id == 0) {
137098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            throw new RSRuntimeException("Load failed.");
137198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
137298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        return new Allocation(id, rs, t, usage);
137398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
137498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
137598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
13767d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Creates an Allocation from a {@link android.graphics.Bitmap}.
1377c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray     *
13787d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * <p>This Allocation will be created with {@link #USAGE_SHARED}, and
13797d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * {@link #USAGE_SCRIPT}.</p>
138098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
138198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param rs Context to which the allocation will belong.
138298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param b bitmap source for the allocation data
138398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
13847d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @return Allocation containing bitmap data
138598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
138698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
138798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    static public Allocation createFromBitmap(RenderScript rs, Bitmap b) {
138898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        return createFromBitmap(rs, b, MipmapControl.MIPMAP_NONE,
13890f5bae87e2e3e3b0e66803122b5c4c7dd36d43ddStephen Hines                                USAGE_SHARED | USAGE_SCRIPT | USAGE_GRAPHICS_TEXTURE);
139098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
139198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
139298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
13937d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Creates a cubemap Allocation from a {@link android.graphics.Bitmap}
13947d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * containing the horizontal list of cube faces. Each face must be a square,
13957d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * have the same size as all other faces, and have a width that is a power
13967d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * of 2.
139798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
139898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param rs Context to which the allocation will belong.
13997d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param b Bitmap with cubemap faces layed out in the following
140098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *          format: right, left, top, bottom, front, back
140198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param mips specifies desired mipmap behaviour for the cubemap
140298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param usage bit field specifying how the cubemap is utilized
140398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
140498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @return allocation containing cubemap data
140598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
140698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
140798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    static public Allocation createCubemapFromBitmap(RenderScript rs, Bitmap b,
140898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                                     MipmapControl mips,
140998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                                     int usage) {
141098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        rs.validate();
141198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
141298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        int height = b.getHeight();
141398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        int width = b.getWidth();
141498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
141598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        if (width % 6 != 0) {
141698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            throw new RSIllegalArgumentException("Cubemap height must be multiple of 6");
141798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
141898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        if (width / 6 != height) {
141998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            throw new RSIllegalArgumentException("Only square cube map faces supported");
142098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
142198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        boolean isPow2 = (height & (height - 1)) == 0;
142298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        if (!isPow2) {
142398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            throw new RSIllegalArgumentException("Only power of 2 cube faces supported");
142498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
142598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
142698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        Element e = elementFromBitmap(rs, b);
142798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        Type.Builder tb = new Type.Builder(rs, e);
142898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        tb.setX(height);
142998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        tb.setY(height);
143098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        tb.setFaces(true);
143198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        tb.setMipmaps(mips == MipmapControl.MIPMAP_FULL);
143298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        Type t = tb.create();
143398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
143498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        int id = rs.nAllocationCubeCreateFromBitmap(t.getID(rs), mips.mID, b, usage);
143598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        if(id == 0) {
143698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            throw new RSRuntimeException("Load failed for bitmap " + b + " element " + e);
143798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
143898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        return new Allocation(id, rs, t, usage);
143998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
144098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
144198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
14427d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Creates a non-mipmapped cubemap Allocation for use as a graphics texture
14437d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * from a {@link android.graphics.Bitmap} containing the horizontal list of
14447d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * cube faces. Each face must be a square, have the same size as all other
14457d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * faces, and have a width that is a power of 2.
144698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
144798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param rs Context to which the allocation will belong.
144898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param b bitmap with cubemap faces layed out in the following
144998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *          format: right, left, top, bottom, front, back
145098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
145198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @return allocation containing cubemap data
145298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
145398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
145498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    static public Allocation createCubemapFromBitmap(RenderScript rs,
145598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                                     Bitmap b) {
145698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        return createCubemapFromBitmap(rs, b, MipmapControl.MIPMAP_NONE,
145798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                       USAGE_GRAPHICS_TEXTURE);
145898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
145998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
146098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
14617d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Creates a cubemap Allocation from 6 {@link android.graphics.Bitmap}
14627d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * objects containing the cube faces. Each face must be a square, have the
14637d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * same size as all other faces, and have a width that is a power of 2.
146498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
146598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param rs Context to which the allocation will belong.
146698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param xpos cubemap face in the positive x direction
146798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param xneg cubemap face in the negative x direction
146898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param ypos cubemap face in the positive y direction
146998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param yneg cubemap face in the negative y direction
147098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param zpos cubemap face in the positive z direction
147198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param zneg cubemap face in the negative z direction
147298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param mips specifies desired mipmap behaviour for the cubemap
147398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param usage bit field specifying how the cubemap is utilized
147498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
147598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @return allocation containing cubemap data
147698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
147798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
147898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    static public Allocation createCubemapFromCubeFaces(RenderScript rs,
147998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                                        Bitmap xpos,
148098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                                        Bitmap xneg,
148198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                                        Bitmap ypos,
148298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                                        Bitmap yneg,
148398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                                        Bitmap zpos,
148498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                                        Bitmap zneg,
148598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                                        MipmapControl mips,
148698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                                        int usage) {
1487ce8b0e674c93035013d1c33aaabc9bb6ceffde0fTim Murray        /*
148898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        int height = xpos.getHeight();
148998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        if (xpos.getWidth() != height ||
149098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            xneg.getWidth() != height || xneg.getHeight() != height ||
149198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            ypos.getWidth() != height || ypos.getHeight() != height ||
149298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            yneg.getWidth() != height || yneg.getHeight() != height ||
149398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            zpos.getWidth() != height || zpos.getHeight() != height ||
149498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            zneg.getWidth() != height || zneg.getHeight() != height) {
149598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            throw new RSIllegalArgumentException("Only square cube map faces supported");
149698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
149798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        boolean isPow2 = (height & (height - 1)) == 0;
149898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        if (!isPow2) {
149998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            throw new RSIllegalArgumentException("Only power of 2 cube faces supported");
150098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
150198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
150298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        Element e = elementFromBitmap(rs, xpos);
150398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        Type.Builder tb = new Type.Builder(rs, e);
150498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        tb.setX(height);
150598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        tb.setY(height);
150698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        tb.setFaces(true);
150798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        tb.setMipmaps(mips == MipmapControl.MIPMAP_FULL);
150898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        Type t = tb.create();
150998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        Allocation cubemap = Allocation.createTyped(rs, t, mips, usage);
151098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
151198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        AllocationAdapter adapter = AllocationAdapter.create2D(rs, cubemap);
151298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        adapter.setFace(Type.CubemapFace.POSITIVE_X);
151398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        adapter.copyFrom(xpos);
151498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        adapter.setFace(Type.CubemapFace.NEGATIVE_X);
151598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        adapter.copyFrom(xneg);
151698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        adapter.setFace(Type.CubemapFace.POSITIVE_Y);
151798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        adapter.copyFrom(ypos);
151898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        adapter.setFace(Type.CubemapFace.NEGATIVE_Y);
151998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        adapter.copyFrom(yneg);
152098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        adapter.setFace(Type.CubemapFace.POSITIVE_Z);
152198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        adapter.copyFrom(zpos);
152298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        adapter.setFace(Type.CubemapFace.NEGATIVE_Z);
152398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        adapter.copyFrom(zneg);
152498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
152598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        return cubemap;
1526ce8b0e674c93035013d1c33aaabc9bb6ceffde0fTim Murray        */
1527ce8b0e674c93035013d1c33aaabc9bb6ceffde0fTim Murray        return null;
152898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
152998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
153098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
15317d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Creates a non-mipmapped cubemap Allocation for use as a sampler input
15327d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * from 6 {@link android.graphics.Bitmap} objects containing the cube
15337d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * faces. Each face must be a square, have the same size as all other faces,
15347d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * and have a width that is a power of 2.
153598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
153698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param rs Context to which the allocation will belong.
153798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param xpos cubemap face in the positive x direction
153898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param xneg cubemap face in the negative x direction
153998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param ypos cubemap face in the positive y direction
154098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param yneg cubemap face in the negative y direction
154198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param zpos cubemap face in the positive z direction
154298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param zneg cubemap face in the negative z direction
154398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
154498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @return allocation containing cubemap data
154598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
154698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
154798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    static public Allocation createCubemapFromCubeFaces(RenderScript rs,
154898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                                        Bitmap xpos,
154998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                                        Bitmap xneg,
155098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                                        Bitmap ypos,
155198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                                        Bitmap yneg,
155298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                                        Bitmap zpos,
155398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                                        Bitmap zneg) {
155498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        return createCubemapFromCubeFaces(rs, xpos, xneg, ypos, yneg,
155598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                          zpos, zneg, MipmapControl.MIPMAP_NONE,
155698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                          USAGE_GRAPHICS_TEXTURE);
155798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
155898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
1559c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray    /**
15607d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Creates an Allocation from the Bitmap referenced
15617d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * by resource ID.
1562c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray     *
1563c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray     * @param rs Context to which the allocation will belong.
1564c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray     * @param res application resources
1565c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray     * @param id resource id to load the data from
1566c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray     * @param mips specifies desired mipmap behaviour for the
1567c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray     *             allocation
1568c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray     * @param usage bit field specifying how the allocation is
1569c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray     *              utilized
1570c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray     *
15717d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @return Allocation containing resource data
1572c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray     *
1573c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray     */
1574c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray    static public Allocation createFromBitmapResource(RenderScript rs,
1575c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray                                                      Resources res,
1576c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray                                                      int id,
1577c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray                                                      MipmapControl mips,
1578c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray                                                      int usage) {
157998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
1580c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray        rs.validate();
15817d435ae5ba100be5710b685653cc351cab159c11Stephen Hines        if ((usage & (USAGE_SHARED | USAGE_IO_INPUT | USAGE_IO_OUTPUT)) != 0) {
15827d435ae5ba100be5710b685653cc351cab159c11Stephen Hines            throw new RSIllegalArgumentException("Unsupported usage specified.");
15837d435ae5ba100be5710b685653cc351cab159c11Stephen Hines        }
1584c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray        Bitmap b = BitmapFactory.decodeResource(res, id);
1585c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray        Allocation alloc = createFromBitmap(rs, b, mips, usage);
1586c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray        b.recycle();
1587c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray        return alloc;
1588c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray    }
1589c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray
1590c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray    /**
15917d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Creates a non-mipmapped Allocation to use as a graphics texture from the
15927d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * {@link android.graphics.Bitmap} referenced by resource ID.
15937d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     *
15947d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * <p>This allocation will be created with {@link #USAGE_SCRIPT} and
15957d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * {@link #USAGE_GRAPHICS_TEXTURE}.</p>
1596c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray     *
1597c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray     * @param rs Context to which the allocation will belong.
1598c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray     * @param res application resources
1599c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray     * @param id resource id to load the data from
1600c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray     *
16017d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @return Allocation containing resource data
1602c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray     *
1603c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray     */
1604c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray    static public Allocation createFromBitmapResource(RenderScript rs,
1605c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray                                                      Resources res,
1606c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray                                                      int id) {
1607c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray        return createFromBitmapResource(rs, res, id,
1608c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray                                        MipmapControl.MIPMAP_NONE,
16097d435ae5ba100be5710b685653cc351cab159c11Stephen Hines                                        USAGE_SCRIPT | USAGE_GRAPHICS_TEXTURE);
1610c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray    }
1611c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray
1612c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray    /**
16137d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Creates an Allocation containing string data encoded in UTF-8 format.
1614c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray     *
1615c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray     * @param rs Context to which the allocation will belong.
1616c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray     * @param str string to create the allocation from
1617c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray     * @param usage bit field specifying how the allocaiton is
1618c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray     *              utilized
1619c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray     *
1620c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray     */
1621c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray    static public Allocation createFromString(RenderScript rs,
1622c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray                                              String str,
1623c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray                                              int usage) {
1624c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray        rs.validate();
1625c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray        byte[] allocArray = null;
1626c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray        try {
1627c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray            allocArray = str.getBytes("UTF-8");
1628c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray            Allocation alloc = Allocation.createSized(rs, Element.U8(rs), allocArray.length, usage);
1629c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray            alloc.copyFrom(allocArray);
1630c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray            return alloc;
1631c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray        }
1632c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray        catch (Exception e) {
1633c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray            throw new RSRuntimeException("Could not convert string to utf-8.");
1634c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray        }
1635c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray    }
16362da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines}
163737ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams
1638