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() {
1988379e65d0e9941d6157b8ed86e5d19b4a65c343dTim Murray        if (mType.mDimYuv != 0) {
1998379e65d0e9941d6157b8ed86e5d19b4a65c343dTim Murray            return (int)Math.ceil(mType.getCount() * mType.getElement().getBytesSize() * 1.5);
2008379e65d0e9941d6157b8ed86e5d19b4a65c343dTim Murray        }
20198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        return mType.getCount() * mType.getElement().getBytesSize();
20298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
20398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
20498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    private void updateCacheInfo(Type t) {
20598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        mCurrentDimX = t.getX();
20698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        mCurrentDimY = t.getY();
20798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        mCurrentDimZ = t.getZ();
20898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        mCurrentCount = mCurrentDimX;
20998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        if (mCurrentDimY > 1) {
21098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            mCurrentCount *= mCurrentDimY;
21198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
21298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        if (mCurrentDimZ > 1) {
21398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            mCurrentCount *= mCurrentDimZ;
21498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
21598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
21698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
2171b370e358d16cc3b50b169511d6b387db09f972dJason Sams    private void setBitmap(Bitmap b) {
2181b370e358d16cc3b50b169511d6b387db09f972dJason Sams        mBitmap = b;
2191b370e358d16cc3b50b169511d6b387db09f972dJason Sams    }
2201b370e358d16cc3b50b169511d6b387db09f972dJason Sams
22198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    Allocation(int id, RenderScript rs, Type t, int usage) {
22298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        super(id, rs);
223c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray        if ((usage & ~(USAGE_SCRIPT |
224c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray                       USAGE_GRAPHICS_TEXTURE |
225c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray                       USAGE_IO_INPUT |
226c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray                       USAGE_IO_OUTPUT |
227c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray                       USAGE_SHARED)) != 0) {
22898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            throw new RSIllegalArgumentException("Unknown usage specified.");
22998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
23098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
231c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray        if ((usage & USAGE_IO_INPUT) != 0) {
232c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray            mWriteAllowed = false;
233c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray
234c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray            if ((usage & ~(USAGE_IO_INPUT |
235c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray                           USAGE_GRAPHICS_TEXTURE |
236c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray                           USAGE_SCRIPT)) != 0) {
237c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray                throw new RSIllegalArgumentException("Invalid usage combination.");
238c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray            }
239c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray        }
240c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray
24198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        mType = t;
24298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        mUsage = usage;
2437720e5ceb6a4cc9ed330ad7c483f7eac08f554e6Tim Murray        mSize = mType.getCount() * mType.getElement().getBytesSize();
24498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
24598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        if (t != null) {
24698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            updateCacheInfo(t);
24798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
2487720e5ceb6a4cc9ed330ad7c483f7eac08f554e6Tim Murray        if (RenderScript.sUseGCHooks == true) {
2497720e5ceb6a4cc9ed330ad7c483f7eac08f554e6Tim Murray            try {
2507720e5ceb6a4cc9ed330ad7c483f7eac08f554e6Tim Murray                RenderScript.registerNativeAllocation.invoke(RenderScript.sRuntime, mSize);
2517720e5ceb6a4cc9ed330ad7c483f7eac08f554e6Tim Murray            } catch (Exception e) {
2527720e5ceb6a4cc9ed330ad7c483f7eac08f554e6Tim Murray                Log.e(RenderScript.LOG_TAG, "Couldn't invoke registerNativeAllocation:" + e);
2537720e5ceb6a4cc9ed330ad7c483f7eac08f554e6Tim Murray                throw new RSRuntimeException("Couldn't invoke registerNativeAllocation:" + e);
2547720e5ceb6a4cc9ed330ad7c483f7eac08f554e6Tim Murray            }
2557720e5ceb6a4cc9ed330ad7c483f7eac08f554e6Tim Murray        }
2567720e5ceb6a4cc9ed330ad7c483f7eac08f554e6Tim Murray    }
2577720e5ceb6a4cc9ed330ad7c483f7eac08f554e6Tim Murray
2587720e5ceb6a4cc9ed330ad7c483f7eac08f554e6Tim Murray    protected void finalize() throws Throwable {
2597720e5ceb6a4cc9ed330ad7c483f7eac08f554e6Tim Murray        if (RenderScript.sUseGCHooks == true) {
2607720e5ceb6a4cc9ed330ad7c483f7eac08f554e6Tim Murray            RenderScript.registerNativeFree.invoke(RenderScript.sRuntime, mSize);
2617720e5ceb6a4cc9ed330ad7c483f7eac08f554e6Tim Murray        }
2627720e5ceb6a4cc9ed330ad7c483f7eac08f554e6Tim Murray        super.finalize();
26398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
26498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
2657720e5ceb6a4cc9ed330ad7c483f7eac08f554e6Tim Murray
26698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    private void validateIsInt32() {
26798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        if ((mType.mElement.mType == Element.DataType.SIGNED_32) ||
26898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            (mType.mElement.mType == Element.DataType.UNSIGNED_32)) {
26998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            return;
27098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
27198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        throw new RSIllegalArgumentException(
27298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            "32 bit integer source does not match allocation type " + mType.mElement.mType);
27398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
27498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
27598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    private void validateIsInt16() {
27698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        if ((mType.mElement.mType == Element.DataType.SIGNED_16) ||
27798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            (mType.mElement.mType == Element.DataType.UNSIGNED_16)) {
27898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            return;
27998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
28098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        throw new RSIllegalArgumentException(
28198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            "16 bit integer source does not match allocation type " + mType.mElement.mType);
28298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
28398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
28498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    private void validateIsInt8() {
28598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        if ((mType.mElement.mType == Element.DataType.SIGNED_8) ||
28698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            (mType.mElement.mType == Element.DataType.UNSIGNED_8)) {
28798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            return;
28898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
28998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        throw new RSIllegalArgumentException(
29098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            "8 bit integer source does not match allocation type " + mType.mElement.mType);
29198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
29298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
29398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    private void validateIsFloat32() {
29498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        if (mType.mElement.mType == Element.DataType.FLOAT_32) {
29598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            return;
29698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
29798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        throw new RSIllegalArgumentException(
29898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            "32 bit float source does not match allocation type " + mType.mElement.mType);
29998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
30098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
30198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    private void validateIsObject() {
30298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        if ((mType.mElement.mType == Element.DataType.RS_ELEMENT) ||
30398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            (mType.mElement.mType == Element.DataType.RS_TYPE) ||
30498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            (mType.mElement.mType == Element.DataType.RS_ALLOCATION) ||
30598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            (mType.mElement.mType == Element.DataType.RS_SAMPLER) ||
30698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            (mType.mElement.mType == Element.DataType.RS_SCRIPT)) {
30798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            return;
30898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
30998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        throw new RSIllegalArgumentException(
31098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            "Object source does not match allocation type " + mType.mElement.mType);
31198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
31298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
31398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
31460c5b31f4448410221de043873b94797732afa66Stephen Hines     * Get the {@link android.support.v8.renderscript.Type} of the Allocation.
31598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
31698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @return Type
31798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
31898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
31998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public Type getType() {
32098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        return mType;
32198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
32298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
32398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
3247d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Propagate changes from one usage of the Allocation to the
3257d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * other usages of the Allocation.
32698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
32798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
32898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public void syncAll(int srcLocation) {
32998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        switch (srcLocation) {
33098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        case USAGE_SCRIPT:
33198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        case USAGE_GRAPHICS_TEXTURE:
33298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            break;
33398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        default:
33498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            throw new RSIllegalArgumentException("Source must be exactly one usage type.");
33598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
33698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        mRS.validate();
33798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        mRS.nAllocationSyncAll(getIDSafe(), srcLocation);
33898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
33998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
340c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray    /**
3417d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Send a buffer to the output stream.  The contents of the Allocation will
3427d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * be undefined after this operation. This operation is only valid if {@link
3437d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * #USAGE_IO_OUTPUT} is set on the Allocation.
3447d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     *
345c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray     *
346c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray     */
347c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray    public void ioSend() {
348c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray        if ((mUsage & USAGE_IO_OUTPUT) == 0) {
349c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray            throw new RSIllegalArgumentException(
350c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray                "Can only send buffer if IO_OUTPUT usage specified.");
351c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray        }
352c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray        mRS.validate();
353c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray        mRS.nAllocationIoSend(getID(mRS));
354c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray    }
355c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray
356c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray    /**
357c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray     * Delete once code is updated.
358c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray     * @hide
359c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray     */
360c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray    public void ioSendOutput() {
361c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray        ioSend();
362c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray    }
363c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray
364c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray    /**
3657d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Receive the latest input into the Allocation. This operation
3667d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * is only valid if {@link #USAGE_IO_INPUT} is set on the Allocation.
367c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray     *
368c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray     */
369c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray    public void ioReceive() {
370c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray        if ((mUsage & USAGE_IO_INPUT) == 0) {
371c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray            throw new RSIllegalArgumentException(
372c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray                "Can only receive if IO_INPUT usage specified.");
373c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray        }
374c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray        mRS.validate();
375c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray        mRS.nAllocationIoReceive(getID(mRS));
376c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray    }
37798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
37898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
3797d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Copy an array of RS objects to the Allocation.
38098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
38198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param d Source array.
38298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
38398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public void copyFrom(BaseObj[] d) {
38498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        mRS.validate();
38598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        validateIsObject();
38698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        if (d.length != mCurrentCount) {
38798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            throw new RSIllegalArgumentException("Array size mismatch, allocation sizeX = " +
38898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                                 mCurrentCount + ", array length = " + d.length);
38998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
39098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        int i[] = new int[d.length];
39198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        for (int ct=0; ct < d.length; ct++) {
39298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            i[ct] = d[ct].getID(mRS);
39398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
39498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        copy1DRangeFromUnchecked(0, mCurrentCount, i);
39598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
39698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
39798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    private void validateBitmapFormat(Bitmap b) {
39898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        Bitmap.Config bc = b.getConfig();
399c5953678decd7963ed0515c2a0b8fcc92b3f3b9fTim Murray        if (bc == null) {
400c5953678decd7963ed0515c2a0b8fcc92b3f3b9fTim Murray            throw new RSIllegalArgumentException("Bitmap has an unsupported format for this operation");
401c5953678decd7963ed0515c2a0b8fcc92b3f3b9fTim Murray        }
40298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        switch (bc) {
40398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        case ALPHA_8:
40498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            if (mType.getElement().mKind != Element.DataKind.PIXEL_A) {
40598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                throw new RSIllegalArgumentException("Allocation kind is " +
40698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                                     mType.getElement().mKind + ", type " +
40798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                                     mType.getElement().mType +
40898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                                     " of " + mType.getElement().getBytesSize() +
40998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                                     " bytes, passed bitmap was " + bc);
41098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            }
41198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            break;
41298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        case ARGB_8888:
41398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            if ((mType.getElement().mKind != Element.DataKind.PIXEL_RGBA) ||
41498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                (mType.getElement().getBytesSize() != 4)) {
41598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                throw new RSIllegalArgumentException("Allocation kind is " +
41698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                                     mType.getElement().mKind + ", type " +
41798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                                     mType.getElement().mType +
41898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                                     " of " + mType.getElement().getBytesSize() +
41998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                                     " bytes, passed bitmap was " + bc);
42098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            }
42198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            break;
42298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        case RGB_565:
42398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            if ((mType.getElement().mKind != Element.DataKind.PIXEL_RGB) ||
42498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                (mType.getElement().getBytesSize() != 2)) {
42598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                throw new RSIllegalArgumentException("Allocation kind is " +
42698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                                     mType.getElement().mKind + ", type " +
42798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                                     mType.getElement().mType +
42898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                                     " of " + mType.getElement().getBytesSize() +
42998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                                     " bytes, passed bitmap was " + bc);
43098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            }
43198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            break;
43298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        case ARGB_4444:
43398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            if ((mType.getElement().mKind != Element.DataKind.PIXEL_RGBA) ||
43498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                (mType.getElement().getBytesSize() != 2)) {
43598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                throw new RSIllegalArgumentException("Allocation kind is " +
43698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                                     mType.getElement().mKind + ", type " +
43798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                                     mType.getElement().mType +
43898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                                     " of " + mType.getElement().getBytesSize() +
43998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                                     " bytes, passed bitmap was " + bc);
44098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            }
44198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            break;
44237ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams
44398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
44498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
44598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
44698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    private void validateBitmapSize(Bitmap b) {
44798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        if((mCurrentDimX != b.getWidth()) || (mCurrentDimY != b.getHeight())) {
44898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            throw new RSIllegalArgumentException("Cannot update allocation from bitmap, sizes mismatch");
44998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
45098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
45198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
45298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
4537d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Copy into this Allocation from an array. This method does not guarantee
4547d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * that the Allocation is compatible with the input buffer; it copies memory
4557d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * without reinterpretation.
45698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
45798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param d the source data array
45898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
45998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public void copyFromUnchecked(int[] d) {
46098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        mRS.validate();
46137ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams        if (mCurrentDimZ > 0) {
46237ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams            copy3DRangeFromUnchecked(0, 0, 0, mCurrentDimX, mCurrentDimY, mCurrentDimZ, d);
46337ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams        } else if (mCurrentDimY > 0) {
4642da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines            copy2DRangeFromUnchecked(0, 0, mCurrentDimX, mCurrentDimY, d);
4652da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines        } else {
4662da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines            copy1DRangeFromUnchecked(0, mCurrentCount, d);
4672da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines        }
46898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
46998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
4707d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Copy into this Allocation from an array. This method does not guarantee
4717d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * that the Allocation is compatible with the input buffer; it copies memory
4727d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * without reinterpretation.
47398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
47498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param d the source data array
47598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
47698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public void copyFromUnchecked(short[] d) {
47798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        mRS.validate();
47837ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams        if (mCurrentDimZ > 0) {
47937ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams            copy3DRangeFromUnchecked(0, 0, 0, mCurrentDimX, mCurrentDimY, mCurrentDimZ, d);
48037ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams        } else if (mCurrentDimY > 0) {
4812da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines            copy2DRangeFromUnchecked(0, 0, mCurrentDimX, mCurrentDimY, d);
4822da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines        } else {
4832da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines            copy1DRangeFromUnchecked(0, mCurrentCount, d);
4842da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines        }
48598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
48698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
4877d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Copy into this Allocation from an array. This method does not guarantee
4887d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * that the Allocation is compatible with the input buffer; it copies memory
4897d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * without reinterpretation.
49098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
49198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param d the source data array
49298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
49398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public void copyFromUnchecked(byte[] d) {
49498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        mRS.validate();
49537ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams        if (mCurrentDimZ > 0) {
49637ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams            copy3DRangeFromUnchecked(0, 0, 0, mCurrentDimX, mCurrentDimY, mCurrentDimZ, d);
49737ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams        } else if (mCurrentDimY > 0) {
4982da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines            copy2DRangeFromUnchecked(0, 0, mCurrentDimX, mCurrentDimY, d);
4992da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines        } else {
5002da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines            copy1DRangeFromUnchecked(0, mCurrentCount, d);
5012da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines        }
50298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
50398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
5047d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Copy into this Allocation from an array. This method does not guarantee
5057d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * that the Allocation is compatible with the input buffer; it copies memory
5067d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * without reinterpretation.
50798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
50898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param d the source data array
50998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
51098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public void copyFromUnchecked(float[] d) {
51198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        mRS.validate();
51237ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams        if (mCurrentDimZ > 0) {
51337ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams            copy3DRangeFromUnchecked(0, 0, 0, mCurrentDimX, mCurrentDimY, mCurrentDimZ, d);
51437ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams        } else if (mCurrentDimY > 0) {
5152da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines            copy2DRangeFromUnchecked(0, 0, mCurrentDimX, mCurrentDimY, d);
5162da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines        } else {
5172da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines            copy1DRangeFromUnchecked(0, mCurrentCount, d);
5182da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines        }
51998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
52098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
52198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
5227d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Copy into this Allocation from an array.  This variant is type checked
5237d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * and will generate exceptions if the Allocation's {@link
52460c5b31f4448410221de043873b94797732afa66Stephen Hines     * android.support.v8.renderscript.Element} is not a 32 bit integer type.
52598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
52698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param d the source data array
52798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
52898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public void copyFrom(int[] d) {
52998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        mRS.validate();
53037ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams        if (mCurrentDimZ > 0) {
53137ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams            copy3DRangeFrom(0, 0, 0, mCurrentDimX, mCurrentDimY, mCurrentDimZ, d);
53237ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams        } else if (mCurrentDimY > 0) {
5332da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines            copy2DRangeFrom(0, 0, mCurrentDimX, mCurrentDimY, d);
5342da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines        } else {
5352da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines            copy1DRangeFrom(0, mCurrentCount, d);
5362da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines        }
53798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
53898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
53998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
5407d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Copy into this Allocation from an array.  This variant is type checked
5417d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * and will generate exceptions if the Allocation's {@link
54260c5b31f4448410221de043873b94797732afa66Stephen Hines     * android.support.v8.renderscript.Element} is not a 16 bit integer type.
54398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
54498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param d the source data array
54598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
54698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public void copyFrom(short[] d) {
54798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        mRS.validate();
54837ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams        if (mCurrentDimZ > 0) {
54937ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams            copy3DRangeFrom(0, 0, 0, mCurrentDimX, mCurrentDimY, mCurrentDimZ, d);
55037ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams        } else if (mCurrentDimY > 0) {
5512da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines            copy2DRangeFrom(0, 0, mCurrentDimX, mCurrentDimY, d);
5522da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines        } else {
5532da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines            copy1DRangeFrom(0, mCurrentCount, d);
5542da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines        }
55598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
55698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
55798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
5587d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Copy into this Allocation from an array.  This variant is type checked
5597d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * and will generate exceptions if the Allocation's {@link
56060c5b31f4448410221de043873b94797732afa66Stephen Hines     * android.support.v8.renderscript.Element} is not an 8 bit integer type.
56198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
56298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param d the source data array
56398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
56498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public void copyFrom(byte[] d) {
56598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        mRS.validate();
56637ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams        if (mCurrentDimZ > 0) {
56737ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams            copy3DRangeFrom(0, 0, 0, mCurrentDimX, mCurrentDimY, mCurrentDimZ, d);
56837ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams        } else if (mCurrentDimY > 0) {
5692da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines            copy2DRangeFrom(0, 0, mCurrentDimX, mCurrentDimY, d);
5702da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines        } else {
5712da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines            copy1DRangeFrom(0, mCurrentCount, d);
5722da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines        }
57398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
57498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
57598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
5767d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Copy into this Allocation from an array.  This variant is type checked
5777d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * and will generate exceptions if the Allocation's {@link
57860c5b31f4448410221de043873b94797732afa66Stephen Hines     * android.support.v8.renderscript.Element} is not a 32 bit float type.
57998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
58098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param d the source data array
58198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
58298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public void copyFrom(float[] d) {
58398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        mRS.validate();
58437ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams        if (mCurrentDimZ > 0) {
58537ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams            copy3DRangeFrom(0, 0, 0, mCurrentDimX, mCurrentDimY, mCurrentDimZ, d);
58637ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams        } else if (mCurrentDimY > 0) {
5872da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines            copy2DRangeFrom(0, 0, mCurrentDimX, mCurrentDimY, d);
5882da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines        } else {
5892da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines            copy1DRangeFrom(0, mCurrentCount, d);
5902da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines        }
59198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
59298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
59398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
5947d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Copy into an Allocation from a {@link android.graphics.Bitmap}.  The
5957d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * height, width, and format of the bitmap must match the existing
5967d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * allocation.
5977d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     *
5987d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * <p>If the {@link android.graphics.Bitmap} is the same as the {@link
5997d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * android.graphics.Bitmap} used to create the Allocation with {@link
6007d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * #createFromBitmap} and {@link #USAGE_SHARED} is set on the Allocation,
6017d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * this will synchronize the Allocation with the latest data from the {@link
6027d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * android.graphics.Bitmap}, potentially avoiding the actual copy.</p>
60398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
60498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param b the source bitmap
60598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
60698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public void copyFrom(Bitmap b) {
60798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        mRS.validate();
608c5953678decd7963ed0515c2a0b8fcc92b3f3b9fTim Murray        if (b.getConfig() == null) {
609c5953678decd7963ed0515c2a0b8fcc92b3f3b9fTim Murray            Bitmap newBitmap = Bitmap.createBitmap(b.getWidth(), b.getHeight(), Bitmap.Config.ARGB_8888);
610c5953678decd7963ed0515c2a0b8fcc92b3f3b9fTim Murray            Canvas c = new Canvas(newBitmap);
611c5953678decd7963ed0515c2a0b8fcc92b3f3b9fTim Murray            c.drawBitmap(b, 0, 0, null);
612c5953678decd7963ed0515c2a0b8fcc92b3f3b9fTim Murray            copyFrom(newBitmap);
613c5953678decd7963ed0515c2a0b8fcc92b3f3b9fTim Murray            return;
614c5953678decd7963ed0515c2a0b8fcc92b3f3b9fTim Murray        }
61598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        validateBitmapSize(b);
61698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        validateBitmapFormat(b);
61798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        mRS.nAllocationCopyFromBitmap(getID(mRS), b);
61898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
61998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
62098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
6217d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Copy an Allocation from an Allocation.  The types of both allocations
6221b370e358d16cc3b50b169511d6b387db09f972dJason Sams     * must be identical.
6231b370e358d16cc3b50b169511d6b387db09f972dJason Sams     *
6241b370e358d16cc3b50b169511d6b387db09f972dJason Sams     * @param a the source allocation
6251b370e358d16cc3b50b169511d6b387db09f972dJason Sams     */
6261b370e358d16cc3b50b169511d6b387db09f972dJason Sams    public void copyFrom(Allocation a) {
6271b370e358d16cc3b50b169511d6b387db09f972dJason Sams        mRS.validate();
6281b370e358d16cc3b50b169511d6b387db09f972dJason Sams        if (!mType.equals(a.getType())) {
6291b370e358d16cc3b50b169511d6b387db09f972dJason Sams            throw new RSIllegalArgumentException("Types of allocations must match.");
6301b370e358d16cc3b50b169511d6b387db09f972dJason Sams        }
6311b370e358d16cc3b50b169511d6b387db09f972dJason Sams        copy2DRangeFrom(0, 0, mCurrentDimX, mCurrentDimY, a, 0, 0);
6321b370e358d16cc3b50b169511d6b387db09f972dJason Sams    }
6331b370e358d16cc3b50b169511d6b387db09f972dJason Sams
6341b370e358d16cc3b50b169511d6b387db09f972dJason Sams
6351b370e358d16cc3b50b169511d6b387db09f972dJason Sams    /**
6367d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * This is only intended to be used by auto-generated code reflected from
6377d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * the RenderScript script files and should not be used by developers.
63898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
63998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param xoff
64098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param fp
64198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
64298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public void setFromFieldPacker(int xoff, FieldPacker fp) {
64398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        mRS.validate();
64498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        int eSize = mType.mElement.getBytesSize();
64598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        final byte[] data = fp.getData();
64695730ed847182c0b424f87d14d4ce276496dfa66Stephen Hines        int data_length = fp.getPos();
64798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
64895730ed847182c0b424f87d14d4ce276496dfa66Stephen Hines        int count = data_length / eSize;
64995730ed847182c0b424f87d14d4ce276496dfa66Stephen Hines        if ((eSize * count) != data_length) {
65095730ed847182c0b424f87d14d4ce276496dfa66Stephen Hines            throw new RSIllegalArgumentException("Field packer length " + data_length +
65198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                               " not divisible by element size " + eSize + ".");
65298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
65398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        copy1DRangeFromUnchecked(xoff, count, data);
65498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
65598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
65698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
6577d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * This is only intended to be used by auto-generated code reflected from
6587d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * the RenderScript script files.
65998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
66098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param xoff
66198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param component_number
66298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param fp
66398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
66498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public void setFromFieldPacker(int xoff, int component_number, FieldPacker fp) {
66598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        mRS.validate();
66698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        if (component_number >= mType.mElement.mElements.length) {
66798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            throw new RSIllegalArgumentException("Component_number " + component_number + " out of range.");
66898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
66998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        if(xoff < 0) {
67098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            throw new RSIllegalArgumentException("Offset must be >= 0.");
67198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
67298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
67398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        final byte[] data = fp.getData();
67495730ed847182c0b424f87d14d4ce276496dfa66Stephen Hines        int data_length = fp.getPos();
67598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        int eSize = mType.mElement.mElements[component_number].getBytesSize();
67698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        eSize *= mType.mElement.mArraySizes[component_number];
67798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
67895730ed847182c0b424f87d14d4ce276496dfa66Stephen Hines        if (data_length != eSize) {
67995730ed847182c0b424f87d14d4ce276496dfa66Stephen Hines            throw new RSIllegalArgumentException("Field packer sizelength " + data_length +
68098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                               " does not match component size " + eSize + ".");
68198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
68298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
68398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        mRS.nAllocationElementData1D(getIDSafe(), xoff, mSelectedLOD,
68495730ed847182c0b424f87d14d4ce276496dfa66Stephen Hines                                     component_number, data, data_length);
68598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
68698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
68798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    private void data1DChecks(int off, int count, int len, int dataSize) {
68898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        mRS.validate();
68998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        if(off < 0) {
69098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            throw new RSIllegalArgumentException("Offset must be >= 0.");
69198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
69298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        if(count < 1) {
69398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            throw new RSIllegalArgumentException("Count must be >= 1.");
69498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
69598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        if((off + count) > mCurrentCount) {
69698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            throw new RSIllegalArgumentException("Overflow, Available count " + mCurrentCount +
69798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                               ", got " + count + " at offset " + off + ".");
69898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
69998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        if(len < dataSize) {
70098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            throw new RSIllegalArgumentException("Array too small for allocation type.");
70198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
70298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
70398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
70498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
7057d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Generate a mipmap chain. This is only valid if the Type of the Allocation
7067d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * includes mipmaps.
70798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
7087d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * <p>This function will generate a complete set of mipmaps from the top
7097d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * level LOD and place them into the script memory space.</p>
71098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
7117d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * <p>If the Allocation is also using other memory spaces, a call to {@link
7127d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * #syncAll syncAll(Allocation.USAGE_SCRIPT)} is required.</p>
71398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
71498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public void generateMipmaps() {
71598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        mRS.nAllocationGenerateMipmaps(getID(mRS));
71698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
71798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
71898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
7197d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Copy an array into part of this Allocation.  This method does not
7207d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * guarantee that the Allocation is compatible with the input buffer.
72198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
72298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param off The offset of the first element to be copied.
72398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param count The number of elements to be copied.
72498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param d the source data array
72598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
72698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public void copy1DRangeFromUnchecked(int off, int count, int[] d) {
72798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        int dataSize = mType.mElement.getBytesSize() * count;
72898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        data1DChecks(off, count, d.length * 4, dataSize);
72998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        mRS.nAllocationData1D(getIDSafe(), off, mSelectedLOD, count, d, dataSize);
73098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
73198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
7327d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Copy an array into part of this Allocation.  This method does not
7337d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * guarantee that the Allocation is compatible with the input buffer.
73498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
73598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param off The offset of the first element to be copied.
73698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param count The number of elements to be copied.
73798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param d the source data array
73898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
73998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public void copy1DRangeFromUnchecked(int off, int count, short[] d) {
74098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        int dataSize = mType.mElement.getBytesSize() * count;
74198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        data1DChecks(off, count, d.length * 2, dataSize);
74298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        mRS.nAllocationData1D(getIDSafe(), off, mSelectedLOD, count, d, dataSize);
74398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
74498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
7457d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Copy an array into part of this Allocation.  This method does not
7467d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * guarantee that the Allocation is compatible with the input buffer.
74798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
74898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param off The offset of the first element to be copied.
74998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param count The number of elements to be copied.
75098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param d the source data array
75198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
75298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public void copy1DRangeFromUnchecked(int off, int count, byte[] d) {
75398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        int dataSize = mType.mElement.getBytesSize() * count;
75498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        data1DChecks(off, count, d.length, dataSize);
75598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        mRS.nAllocationData1D(getIDSafe(), off, mSelectedLOD, count, d, dataSize);
75698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
75798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
7587d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Copy an array into part of this Allocation.  This method does not
7597d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * guarantee that the Allocation is compatible with the input buffer.
76098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
76198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param off The offset of the first element to be copied.
76298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param count The number of elements to be copied.
76398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param d the source data array
76498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
76598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public void copy1DRangeFromUnchecked(int off, int count, float[] d) {
76698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        int dataSize = mType.mElement.getBytesSize() * count;
76798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        data1DChecks(off, count, d.length * 4, dataSize);
76898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        mRS.nAllocationData1D(getIDSafe(), off, mSelectedLOD, count, d, dataSize);
76998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
77098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
77198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
7727d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Copy an array into part of this Allocation.  This variant is type checked
7737d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * and will generate exceptions if the Allocation type is not a 32 bit
7747d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * integer type.
77598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
77698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param off The offset of the first element to be copied.
77798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param count The number of elements to be copied.
77898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param d the source data array
77998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
78098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public void copy1DRangeFrom(int off, int count, int[] d) {
78198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        validateIsInt32();
78298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        copy1DRangeFromUnchecked(off, count, d);
78398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
78498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
78598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
7867d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Copy an array into part of this Allocation.  This variant is type checked
7877d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * and will generate exceptions if the Allocation type is not a 16 bit
7887d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * integer type.
78998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
79098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param off The offset of the first element to be copied.
79198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param count The number of elements to be copied.
79298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param d the source data array
79398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
79498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public void copy1DRangeFrom(int off, int count, short[] d) {
79598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        validateIsInt16();
79698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        copy1DRangeFromUnchecked(off, count, d);
79798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
79898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
79998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
8007d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Copy an array into part of this Allocation.  This variant is type checked
8017d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * and will generate exceptions if the Allocation type is not an 8 bit
8027d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * integer type.
80398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
80498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param off The offset of the first element to be copied.
80598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param count The number of elements to be copied.
80698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param d the source data array
80798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
80898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public void copy1DRangeFrom(int off, int count, byte[] d) {
80998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        validateIsInt8();
81098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        copy1DRangeFromUnchecked(off, count, d);
81198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
81298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
81398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
8147d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Copy an array into part of this Allocation.  This variant is type checked
8157d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * and will generate exceptions if the Allocation type is not a 32 bit float
8167d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * type.
81798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
81898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param off The offset of the first element to be copied.
81998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param count The number of elements to be copied.
82098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param d the source data array.
82198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
82298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public void copy1DRangeFrom(int off, int count, float[] d) {
82398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        validateIsFloat32();
82498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        copy1DRangeFromUnchecked(off, count, d);
82598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
82698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
82798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     /**
8287d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Copy part of an Allocation into this Allocation.
82998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
83098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param off The offset of the first element to be copied.
83198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param count The number of elements to be copied.
83298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param data the source data allocation.
83398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param dataOff off The offset of the first element in data to
83498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *          be copied.
83598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
83698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public void copy1DRangeFrom(int off, int count, Allocation data, int dataOff) {
83798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        mRS.nAllocationData2D(getIDSafe(), off, 0,
83898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                              mSelectedLOD, mSelectedFace.mID,
83998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                              count, 1, data.getID(mRS), dataOff, 0,
84098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                              data.mSelectedLOD, data.mSelectedFace.mID);
84198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
84298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
84398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    private void validate2DRange(int xoff, int yoff, int w, int h) {
84498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        if (mAdaptedAllocation != null) {
84598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
84698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        } else {
84798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
84898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            if (xoff < 0 || yoff < 0) {
84998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                throw new RSIllegalArgumentException("Offset cannot be negative.");
85098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            }
85198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            if (h < 0 || w < 0) {
85298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                throw new RSIllegalArgumentException("Height or width cannot be negative.");
85398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            }
85498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            if (((xoff + w) > mCurrentDimX) || ((yoff + h) > mCurrentDimY)) {
85598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                throw new RSIllegalArgumentException("Updated region larger than allocation.");
85698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            }
85798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
85898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
85998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
8602da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines    void copy2DRangeFromUnchecked(int xoff, int yoff, int w, int h, byte[] data) {
86198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        mRS.validate();
86298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        validate2DRange(xoff, yoff, w, h);
86398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        mRS.nAllocationData2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID,
86498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                              w, h, data, data.length);
86598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
86698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
8672da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines    void copy2DRangeFromUnchecked(int xoff, int yoff, int w, int h, short[] data) {
86898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        mRS.validate();
86998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        validate2DRange(xoff, yoff, w, h);
87098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        mRS.nAllocationData2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID,
87198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                              w, h, data, data.length * 2);
87298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
87398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
8742da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines    void copy2DRangeFromUnchecked(int xoff, int yoff, int w, int h, int[] data) {
87598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        mRS.validate();
87698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        validate2DRange(xoff, yoff, w, h);
87798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        mRS.nAllocationData2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID,
87898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                              w, h, data, data.length * 4);
87998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
88098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
8812da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines    void copy2DRangeFromUnchecked(int xoff, int yoff, int w, int h, float[] data) {
88298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        mRS.validate();
88398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        validate2DRange(xoff, yoff, w, h);
88498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        mRS.nAllocationData2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID,
88598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                              w, h, data, data.length * 4);
88698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
88798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
8882da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines
8892da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines    /**
8907d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Copy from an array into a rectangular region in this Allocation.  The
8917d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * array is assumed to be tightly packed.
8922da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines     *
8937d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param xoff X offset of the region to update in this Allocation
8947d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param yoff Y offset of the region to update in this Allocation
8957d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param w Width of the region to update
8967d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param h Height of the region to update
8977d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param data to be placed into the Allocation
8982da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines     */
8992da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines    public void copy2DRangeFrom(int xoff, int yoff, int w, int h, byte[] data) {
9002da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines        validateIsInt8();
9012da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines        copy2DRangeFromUnchecked(xoff, yoff, w, h, data);
9022da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines    }
9032da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines
9047d435ae5ba100be5710b685653cc351cab159c11Stephen Hines    /**
9057d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Copy from an array into a rectangular region in this Allocation.  The
9067d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * array is assumed to be tightly packed.
9077d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     *
9087d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param xoff X offset of the region to update in this Allocation
9097d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param yoff Y offset of the region to update in this Allocation
9107d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param w Width of the region to update
9117d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param h Height of the region to update
9127d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param data to be placed into the Allocation
9137d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     */
9142da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines    public void copy2DRangeFrom(int xoff, int yoff, int w, int h, short[] data) {
9152da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines        validateIsInt16();
9162da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines        copy2DRangeFromUnchecked(xoff, yoff, w, h, data);
9172da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines    }
9182da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines
9197d435ae5ba100be5710b685653cc351cab159c11Stephen Hines    /**
9207d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Copy from an array into a rectangular region in this Allocation.  The
9217d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * array is assumed to be tightly packed.
9227d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     *
9237d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param xoff X offset of the region to update in this Allocation
9247d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param yoff Y offset of the region to update in this Allocation
9257d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param w Width of the region to update
9267d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param h Height of the region to update
9277d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param data to be placed into the Allocation
9287d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     */
9292da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines    public void copy2DRangeFrom(int xoff, int yoff, int w, int h, int[] data) {
9302da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines        validateIsInt32();
9312da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines        copy2DRangeFromUnchecked(xoff, yoff, w, h, data);
9322da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines    }
9332da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines
9347d435ae5ba100be5710b685653cc351cab159c11Stephen Hines    /**
9357d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Copy from an array into a rectangular region in this Allocation.  The
9367d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * array is assumed to be tightly packed.
9377d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     *
9387d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param xoff X offset of the region to update in this Allocation
9397d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param yoff Y offset of the region to update in this Allocation
9407d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param w Width of the region to update
9417d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param h Height of the region to update
9427d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param data to be placed into the Allocation
9437d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     */
9442da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines    public void copy2DRangeFrom(int xoff, int yoff, int w, int h, float[] data) {
9452da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines        validateIsFloat32();
9462da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines        copy2DRangeFromUnchecked(xoff, yoff, w, h, data);
9472da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines    }
9482da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines
94998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
9507d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Copy a rectangular region from an Allocation into a rectangular region in
9517d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * this Allocation.
95298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
9537d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param xoff X offset of the region in this Allocation
9547d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param yoff Y offset of the region in this Allocation
9557d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param w Width of the region to update.
9567d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param h Height of the region to update.
9577d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param data source Allocation.
9587d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param dataXoff X offset in source Allocation
9597d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param dataYoff Y offset in source Allocation
96098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
96198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public void copy2DRangeFrom(int xoff, int yoff, int w, int h,
96298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                Allocation data, int dataXoff, int dataYoff) {
96398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        mRS.validate();
96498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        validate2DRange(xoff, yoff, w, h);
96598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        mRS.nAllocationData2D(getIDSafe(), xoff, yoff,
96698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                              mSelectedLOD, mSelectedFace.mID,
96798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                              w, h, data.getID(mRS), dataXoff, dataYoff,
96898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                              data.mSelectedLOD, data.mSelectedFace.mID);
96998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
97098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
97198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
9727d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Copy a {@link android.graphics.Bitmap} into an Allocation.  The height
9737d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * and width of the update will use the height and width of the {@link
9747d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * android.graphics.Bitmap}.
97598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
9767d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param xoff X offset of the region to update in this Allocation
9777d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param yoff Y offset of the region to update in this Allocation
9787d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param data the Bitmap to be copied
97998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
98098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public void copy2DRangeFrom(int xoff, int yoff, Bitmap data) {
98198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        mRS.validate();
982c5953678decd7963ed0515c2a0b8fcc92b3f3b9fTim Murray        if (data.getConfig() == null) {
983c5953678decd7963ed0515c2a0b8fcc92b3f3b9fTim Murray            Bitmap newBitmap = Bitmap.createBitmap(data.getWidth(), data.getHeight(), Bitmap.Config.ARGB_8888);
984c5953678decd7963ed0515c2a0b8fcc92b3f3b9fTim Murray            Canvas c = new Canvas(newBitmap);
985c5953678decd7963ed0515c2a0b8fcc92b3f3b9fTim Murray            c.drawBitmap(data, 0, 0, null);
986c5953678decd7963ed0515c2a0b8fcc92b3f3b9fTim Murray            copy2DRangeFrom(xoff, yoff, newBitmap);
98737ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams            return;
988c5953678decd7963ed0515c2a0b8fcc92b3f3b9fTim Murray        }
98998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        validateBitmapFormat(data);
99098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        validate2DRange(xoff, yoff, data.getWidth(), data.getHeight());
99198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        mRS.nAllocationData2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID, data);
99298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
99398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
99437ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams    private void validate3DRange(int xoff, int yoff, int zoff, int w, int h, int d) {
99537ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams        if (mAdaptedAllocation != null) {
99637ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams
99737ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams        } else {
99837ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams
99937ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams            if (xoff < 0 || yoff < 0 || zoff < 0) {
100037ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams                throw new RSIllegalArgumentException("Offset cannot be negative.");
100137ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams            }
100237ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams            if (h < 0 || w < 0 || d < 0) {
100337ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams                throw new RSIllegalArgumentException("Height or width cannot be negative.");
100437ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams            }
100537ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams            if (((xoff + w) > mCurrentDimX) || ((yoff + h) > mCurrentDimY) || ((zoff + d) > mCurrentDimZ)) {
100637ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams                throw new RSIllegalArgumentException("Updated region larger than allocation.");
100737ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams            }
100837ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams        }
100937ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams    }
101037ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams
101137ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams    /**
101237ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams     * @hide
101337ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams     *
101437ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams     */
101537ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams    void copy3DRangeFromUnchecked(int xoff, int yoff, int zoff, int w, int h, int d, byte[] data) {
101637ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams        mRS.validate();
101737ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams        validate3DRange(xoff, yoff, zoff, w, h, d);
101837ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams        mRS.nAllocationData3D(getIDSafe(), xoff, yoff, zoff, mSelectedLOD,
101937ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams                              w, h, d, data, data.length);
102037ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams    }
102137ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams
102237ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams    /**
102337ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams     * @hide
102437ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams     *
102537ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams     */
102637ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams    void copy3DRangeFromUnchecked(int xoff, int yoff, int zoff, int w, int h, int d, short[] data) {
102737ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams        mRS.validate();
102837ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams        validate3DRange(xoff, yoff, zoff, w, h, d);
102937ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams        mRS.nAllocationData3D(getIDSafe(), xoff, yoff, zoff, mSelectedLOD,
103037ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams                              w, h, d, data, data.length * 2);
103137ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams    }
103237ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams
103337ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams    /**
103437ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams     * @hide
103537ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams     *
103637ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams     */
103737ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams    void copy3DRangeFromUnchecked(int xoff, int yoff, int zoff, int w, int h, int d, int[] data) {
103837ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams        mRS.validate();
103937ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams        validate3DRange(xoff, yoff, zoff, w, h, d);
104037ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams        mRS.nAllocationData3D(getIDSafe(), xoff, yoff, zoff, mSelectedLOD,
104137ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams                              w, h, d, data, data.length * 4);
104237ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams    }
104398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
104498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
104537ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams     * @hide
104637ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams     *
104737ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams     */
104837ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams    void copy3DRangeFromUnchecked(int xoff, int yoff, int zoff, int w, int h, int d, float[] data) {
104937ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams        mRS.validate();
105037ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams        validate3DRange(xoff, yoff, zoff, w, h, d);
105137ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams        mRS.nAllocationData3D(getIDSafe(), xoff, yoff, zoff, mSelectedLOD,
105237ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams                              w, h, d, data, data.length * 4);
105337ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams    }
105437ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams
105537ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams
105637ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams    /**
105737ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams     * @hide
105837ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams     * Copy a rectangular region from the array into the allocation.
10597d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * The array is assumed to be tightly packed.
10607d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     *
10617d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param xoff X offset of the region to update in this Allocation
10627d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param yoff Y offset of the region to update in this Allocation
10637d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param zoff Z offset of the region to update in this Allocation
10647d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param w Width of the region to update
10657d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param h Height of the region to update
10667d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param d Depth of the region to update
106737ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams     * @param data to be placed into the allocation
106837ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams     */
106937ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams    public void copy3DRangeFrom(int xoff, int yoff, int zoff, int w, int h, int d, byte[] data) {
107037ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams        validateIsInt8();
107137ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams        copy3DRangeFromUnchecked(xoff, yoff, zoff, w, h, d, data);
107237ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams    }
107337ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams
107437ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams    /**
107537ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams     * @hide
107637ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams     *
107737ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams     */
107837ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams    public void copy3DRangeFrom(int xoff, int yoff, int zoff, int w, int h, int d, short[] data) {
107937ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams        validateIsInt16();
108037ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams        copy3DRangeFromUnchecked(xoff, yoff, zoff, w, h, d, data);
108137ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams    }
108237ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams
108337ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams    /**
108437ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams     * @hide
108537ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams     *
108637ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams     */
108737ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams    public void copy3DRangeFrom(int xoff, int yoff, int zoff, int w, int h, int d, int[] data) {
108837ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams        validateIsInt32();
108937ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams        copy3DRangeFromUnchecked(xoff, yoff, zoff, w, h, d, data);
109037ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams    }
109137ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams
109237ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams    /**
109337ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams     * @hide
109437ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams     *
109537ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams     */
109637ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams    public void copy3DRangeFrom(int xoff, int yoff, int zoff, int w, int h, int d, float[] data) {
109737ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams        validateIsFloat32();
109837ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams        copy3DRangeFromUnchecked(xoff, yoff, zoff, w, h, d, data);
109937ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams    }
110037ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams
110137ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams    /**
110237ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams     * @hide
110337ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams     * Copy a rectangular region into the allocation from another
110437ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams     * allocation.
110537ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams     *
11067d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param xoff X offset of the region to update in this Allocation
11077d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param yoff Y offset of the region to update in this Allocation
11087d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param zoff Z offset of the region to update in this Allocation
11097d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param w Width of the region to update.
11107d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param h Height of the region to update.
11117d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param d Depth of the region to update.
111237ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams     * @param data source allocation.
11137d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param dataXoff X offset of the region in the source Allocation
11147d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param dataYoff Y offset of the region in the source Allocation
11157d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param dataZoff Z offset of the region in the source Allocation
111637ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams     */
111737ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams    public void copy3DRangeFrom(int xoff, int yoff, int zoff, int w, int h, int d,
111837ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams                                Allocation data, int dataXoff, int dataYoff, int dataZoff) {
111937ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams        mRS.validate();
112037ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams        validate3DRange(xoff, yoff, zoff, w, h, d);
112137ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams        mRS.nAllocationData3D(getIDSafe(), xoff, yoff, zoff, mSelectedLOD,
112237ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams                              w, h, d, data.getID(mRS), dataXoff, dataYoff, dataZoff,
112337ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams                              data.mSelectedLOD);
112437ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams    }
112537ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams
112637ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams
112737ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams    /**
11287d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Copy from the Allocation into a {@link android.graphics.Bitmap}.  The
11297d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * bitmap must match the dimensions of the Allocation.
113098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
113198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param b The bitmap to be set from the Allocation.
113298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
113398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public void copyTo(Bitmap b) {
113498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        mRS.validate();
113598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        validateBitmapFormat(b);
113698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        validateBitmapSize(b);
113798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        mRS.nAllocationCopyToBitmap(getID(mRS), b);
113898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
113998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
114098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
11417d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Copy from the Allocation into a byte array.  The array must be at least
11427d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * as large as the Allocation.  The allocation must be of an 8 bit integer
114360c5b31f4448410221de043873b94797732afa66Stephen Hines     * {@link android.support.v8.renderscript.Element} type.
114498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
114598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param d The array to be set from the Allocation.
114698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
114798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public void copyTo(byte[] d) {
114898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        validateIsInt8();
114998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        mRS.validate();
115098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        mRS.nAllocationRead(getID(mRS), d);
115198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
115298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
115398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
11547d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Copy from the Allocation into a short array.  The array must be at least
11557d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * as large as the Allocation.  The allocation must be of an 16 bit integer
115660c5b31f4448410221de043873b94797732afa66Stephen Hines     * {@link android.support.v8.renderscript.Element} type.
115798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
115898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param d The array to be set from the Allocation.
115998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
116098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public void copyTo(short[] d) {
116198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        validateIsInt16();
116298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        mRS.validate();
116398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        mRS.nAllocationRead(getID(mRS), d);
116498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
116598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
116698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
11677d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Copy from the Allocation into a int array.  The array must be at least as
11687d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * large as the Allocation.  The allocation must be of an 32 bit integer
116960c5b31f4448410221de043873b94797732afa66Stephen Hines     * {@link android.support.v8.renderscript.Element} type.
117098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
117198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param d The array to be set from the Allocation.
117298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
117398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public void copyTo(int[] d) {
117498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        validateIsInt32();
117598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        mRS.validate();
117698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        mRS.nAllocationRead(getID(mRS), d);
117798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
117898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
117998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
11807d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Copy from the Allocation into a float array.  The array must be at least
11817d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * as large as the Allocation.  The allocation must be of an 32 bit float
118260c5b31f4448410221de043873b94797732afa66Stephen Hines     * {@link android.support.v8.renderscript.Element} type.
118398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
118498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param d The array to be set from the Allocation.
118598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
118698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public void copyTo(float[] d) {
118798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        validateIsFloat32();
118898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        mRS.validate();
118998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        mRS.nAllocationRead(getID(mRS), d);
119098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
119198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
119298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    // creation
119398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
119498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    static BitmapFactory.Options mBitmapOptions = new BitmapFactory.Options();
119598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    static {
119698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        mBitmapOptions.inScaled = false;
119798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
119898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
119998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
12007d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Creates a new Allocation with the given {@link
120160c5b31f4448410221de043873b94797732afa66Stephen Hines     * android.support.v8.renderscript.Type}, mipmap flag, and usage flags.
120298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
12037d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param type RenderScript type describing data layout
120498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param mips specifies desired mipmap behaviour for the
120598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *             allocation
12067d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param usage bit field specifying how the Allocation is
120798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *              utilized
120898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
120998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    static public Allocation createTyped(RenderScript rs, Type type, MipmapControl mips, int usage) {
1210ce8b0e674c93035013d1c33aaabc9bb6ceffde0fTim Murray        if (rs.isNative) {
1211ce8b0e674c93035013d1c33aaabc9bb6ceffde0fTim Murray            RenderScriptThunker rst = (RenderScriptThunker)rs;
1212ce8b0e674c93035013d1c33aaabc9bb6ceffde0fTim Murray            return AllocationThunker.createTyped(rst, type, mips, usage);
1213ce8b0e674c93035013d1c33aaabc9bb6ceffde0fTim Murray        }
121498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        rs.validate();
121598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        if (type.getID(rs) == 0) {
121698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            throw new RSInvalidStateException("Bad Type");
121798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
121898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        int id = rs.nAllocationCreateTyped(type.getID(rs), mips.mID, usage, 0);
121998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        if (id == 0) {
122098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            throw new RSRuntimeException("Allocation creation failed.");
122198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
122298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        return new Allocation(id, rs, type, usage);
122398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
122498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
122598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
12267d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Creates an Allocation with the size specified by the type and no mipmaps
12277d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * generated by default
122898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
122998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param rs Context to which the allocation will belong.
123098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param type renderscript type describing data layout
123198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param usage bit field specifying how the allocation is
123298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *              utilized
123398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
123498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @return allocation
123598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
123698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    static public Allocation createTyped(RenderScript rs, Type type, int usage) {
123798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        return createTyped(rs, type, MipmapControl.MIPMAP_NONE, usage);
123898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
123998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
124098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
12417d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Creates an Allocation for use by scripts with a given {@link
124260c5b31f4448410221de043873b94797732afa66Stephen Hines     * android.support.v8.renderscript.Type} and no mipmaps
124398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
12447d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param rs Context to which the Allocation will belong.
12457d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param type RenderScript Type describing data layout
124698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
124798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @return allocation
124898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
124998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    static public Allocation createTyped(RenderScript rs, Type type) {
125098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        return createTyped(rs, type, MipmapControl.MIPMAP_NONE, USAGE_SCRIPT);
125198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
125298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
125398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
12547d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Creates an Allocation with a specified number of given elements
125598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
12567d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param rs Context to which the Allocation will belong.
12577d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param e Element to use in the Allocation
12587d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param count the number of Elements in the Allocation
12597d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param usage bit field specifying how the Allocation is
126098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *              utilized
126198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
126298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @return allocation
126398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
126498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    static public Allocation createSized(RenderScript rs, Element e,
126598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                         int count, int usage) {
1266ce8b0e674c93035013d1c33aaabc9bb6ceffde0fTim Murray        if (rs.isNative) {
1267ce8b0e674c93035013d1c33aaabc9bb6ceffde0fTim Murray            RenderScriptThunker rst = (RenderScriptThunker)rs;
1268ce8b0e674c93035013d1c33aaabc9bb6ceffde0fTim Murray            return AllocationThunker.createSized(rs, e, count, usage);
1269ce8b0e674c93035013d1c33aaabc9bb6ceffde0fTim Murray        }
127098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        rs.validate();
127198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        Type.Builder b = new Type.Builder(rs, e);
127298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        b.setX(count);
127398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        Type t = b.create();
127498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
127598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        int id = rs.nAllocationCreateTyped(t.getID(rs), MipmapControl.MIPMAP_NONE.mID, usage, 0);
127698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        if (id == 0) {
127798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            throw new RSRuntimeException("Allocation creation failed.");
127898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
127998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        return new Allocation(id, rs, t, usage);
128098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
128198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
128298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
12837d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Creates an Allocation with a specified number of given elements
128498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
12857d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param rs Context to which the Allocation will belong.
12867d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param e Element to use in the Allocation
12877d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param count the number of Elements in the Allocation
128898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
128998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @return allocation
129098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
129198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    static public Allocation createSized(RenderScript rs, Element e, int count) {
129298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        return createSized(rs, e, count, USAGE_SCRIPT);
129398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
129498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
129598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    static Element elementFromBitmap(RenderScript rs, Bitmap b) {
129698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        final Bitmap.Config bc = b.getConfig();
129798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        if (bc == Bitmap.Config.ALPHA_8) {
129898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            return Element.A_8(rs);
129998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
130098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        if (bc == Bitmap.Config.ARGB_4444) {
130198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            return Element.RGBA_4444(rs);
130298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
130398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        if (bc == Bitmap.Config.ARGB_8888) {
130498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            return Element.RGBA_8888(rs);
130598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
130698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        if (bc == Bitmap.Config.RGB_565) {
130798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            return Element.RGB_565(rs);
130898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
130998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        throw new RSInvalidStateException("Bad bitmap type: " + bc);
131098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
131198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
131298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    static Type typeFromBitmap(RenderScript rs, Bitmap b,
131398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                       MipmapControl mip) {
131498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        Element e = elementFromBitmap(rs, b);
131598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        Type.Builder tb = new Type.Builder(rs, e);
131698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        tb.setX(b.getWidth());
131798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        tb.setY(b.getHeight());
131898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        tb.setMipmaps(mip == MipmapControl.MIPMAP_FULL);
131998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        return tb.create();
132098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
132198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
132298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
13237d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Creates an Allocation from a {@link android.graphics.Bitmap}.
132498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
132598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param rs Context to which the allocation will belong.
13267d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param b Bitmap source for the allocation data
132798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param mips specifies desired mipmap behaviour for the
132898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *             allocation
132998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param usage bit field specifying how the allocation is
133098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *              utilized
133198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
13327d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @return Allocation containing bitmap data
133398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
133498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
133598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    static public Allocation createFromBitmap(RenderScript rs, Bitmap b,
133698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                              MipmapControl mips,
133798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                              int usage) {
1338ce8b0e674c93035013d1c33aaabc9bb6ceffde0fTim Murray        if (rs.isNative) {
1339ce8b0e674c93035013d1c33aaabc9bb6ceffde0fTim Murray            RenderScriptThunker rst = (RenderScriptThunker)rs;
1340ce8b0e674c93035013d1c33aaabc9bb6ceffde0fTim Murray            return AllocationThunker.createFromBitmap(rst, b, mips, usage);
1341ce8b0e674c93035013d1c33aaabc9bb6ceffde0fTim Murray        }
134298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        rs.validate();
1343c5953678decd7963ed0515c2a0b8fcc92b3f3b9fTim Murray
1344c5953678decd7963ed0515c2a0b8fcc92b3f3b9fTim Murray        // WAR undocumented color formats
1345c5953678decd7963ed0515c2a0b8fcc92b3f3b9fTim Murray        if (b.getConfig() == null) {
1346c5953678decd7963ed0515c2a0b8fcc92b3f3b9fTim Murray            if ((usage & USAGE_SHARED) != 0) {
1347c5953678decd7963ed0515c2a0b8fcc92b3f3b9fTim Murray                throw new RSIllegalArgumentException("USAGE_SHARED cannot be used with a Bitmap that has a null config.");
1348c5953678decd7963ed0515c2a0b8fcc92b3f3b9fTim Murray            }
1349c5953678decd7963ed0515c2a0b8fcc92b3f3b9fTim Murray            Bitmap newBitmap = Bitmap.createBitmap(b.getWidth(), b.getHeight(), Bitmap.Config.ARGB_8888);
1350c5953678decd7963ed0515c2a0b8fcc92b3f3b9fTim Murray            Canvas c = new Canvas(newBitmap);
1351c5953678decd7963ed0515c2a0b8fcc92b3f3b9fTim Murray            c.drawBitmap(b, 0, 0, null);
1352c5953678decd7963ed0515c2a0b8fcc92b3f3b9fTim Murray            return createFromBitmap(rs, newBitmap, mips, usage);
1353c5953678decd7963ed0515c2a0b8fcc92b3f3b9fTim Murray        }
1354c5953678decd7963ed0515c2a0b8fcc92b3f3b9fTim Murray
135598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        Type t = typeFromBitmap(rs, b, mips);
135698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
1357c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray        // enable optimized bitmap path only with no mipmap and script-only usage
1358c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray        if (mips == MipmapControl.MIPMAP_NONE &&
1359c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray            t.getElement().isCompatible(Element.RGBA_8888(rs)) &&
13600f5bae87e2e3e3b0e66803122b5c4c7dd36d43ddStephen Hines            usage == (USAGE_SHARED | USAGE_SCRIPT | USAGE_GRAPHICS_TEXTURE)) {
1361c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray            int id = rs.nAllocationCreateBitmapBackedAllocation(t.getID(rs), mips.mID, b, usage);
1362c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray            if (id == 0) {
1363c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray                throw new RSRuntimeException("Load failed.");
1364c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray            }
1365c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray
1366c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray            // keep a reference to the Bitmap around to prevent GC
1367c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray            Allocation alloc = new Allocation(id, rs, t, usage);
1368c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray            alloc.setBitmap(b);
1369c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray            return alloc;
1370c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray        }
1371c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray
1372c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray
137398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        int id = rs.nAllocationCreateFromBitmap(t.getID(rs), mips.mID, b, usage);
137498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        if (id == 0) {
137598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            throw new RSRuntimeException("Load failed.");
137698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
137798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        return new Allocation(id, rs, t, usage);
137898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
137998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
138098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
13817d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Creates an Allocation from a {@link android.graphics.Bitmap}.
1382c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray     *
13837d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * <p>This Allocation will be created with {@link #USAGE_SHARED}, and
13847d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * {@link #USAGE_SCRIPT}.</p>
138598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
138698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param rs Context to which the allocation will belong.
138798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param b bitmap source for the allocation data
138898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
13897d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @return Allocation containing bitmap data
139098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
139198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
139298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    static public Allocation createFromBitmap(RenderScript rs, Bitmap b) {
139398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        return createFromBitmap(rs, b, MipmapControl.MIPMAP_NONE,
13940f5bae87e2e3e3b0e66803122b5c4c7dd36d43ddStephen Hines                                USAGE_SHARED | USAGE_SCRIPT | USAGE_GRAPHICS_TEXTURE);
139598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
139698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
139798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
13987d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Creates a cubemap Allocation from a {@link android.graphics.Bitmap}
13997d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * containing the horizontal list of cube faces. Each face must be a square,
14007d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * have the same size as all other faces, and have a width that is a power
14017d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * of 2.
140298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
140398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param rs Context to which the allocation will belong.
14047d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param b Bitmap with cubemap faces layed out in the following
140598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *          format: right, left, top, bottom, front, back
140698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param mips specifies desired mipmap behaviour for the cubemap
140798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param usage bit field specifying how the cubemap is utilized
140898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
140998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @return allocation containing cubemap data
141098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
141198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
141298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    static public Allocation createCubemapFromBitmap(RenderScript rs, Bitmap b,
141398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                                     MipmapControl mips,
141498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                                     int usage) {
141598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        rs.validate();
141698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
141798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        int height = b.getHeight();
141898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        int width = b.getWidth();
141998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
142098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        if (width % 6 != 0) {
142198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            throw new RSIllegalArgumentException("Cubemap height must be multiple of 6");
142298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
142398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        if (width / 6 != height) {
142498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            throw new RSIllegalArgumentException("Only square cube map faces supported");
142598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
142698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        boolean isPow2 = (height & (height - 1)) == 0;
142798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        if (!isPow2) {
142898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            throw new RSIllegalArgumentException("Only power of 2 cube faces supported");
142998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
143098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
143198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        Element e = elementFromBitmap(rs, b);
143298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        Type.Builder tb = new Type.Builder(rs, e);
143398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        tb.setX(height);
143498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        tb.setY(height);
143598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        tb.setFaces(true);
143698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        tb.setMipmaps(mips == MipmapControl.MIPMAP_FULL);
143798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        Type t = tb.create();
143898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
143998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        int id = rs.nAllocationCubeCreateFromBitmap(t.getID(rs), mips.mID, b, usage);
144098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        if(id == 0) {
144198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            throw new RSRuntimeException("Load failed for bitmap " + b + " element " + e);
144298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
144398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        return new Allocation(id, rs, t, usage);
144498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
144598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
144698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
14477d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Creates a non-mipmapped cubemap Allocation for use as a graphics texture
14487d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * from a {@link android.graphics.Bitmap} containing the horizontal list of
14497d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * cube faces. Each face must be a square, have the same size as all other
14507d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * faces, and have a width that is a power of 2.
145198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
145298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param rs Context to which the allocation will belong.
145398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param b bitmap with cubemap faces layed out in the following
145498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *          format: right, left, top, bottom, front, back
145598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
145698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @return allocation containing cubemap data
145798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
145898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
145998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    static public Allocation createCubemapFromBitmap(RenderScript rs,
146098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                                     Bitmap b) {
146198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        return createCubemapFromBitmap(rs, b, MipmapControl.MIPMAP_NONE,
146298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                       USAGE_GRAPHICS_TEXTURE);
146398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
146498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
146598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
14667d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Creates a cubemap Allocation from 6 {@link android.graphics.Bitmap}
14677d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * objects containing the cube faces. Each face must be a square, have the
14687d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * same size as all other faces, and have a width that is a power of 2.
146998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
147098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param rs Context to which the allocation will belong.
147198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param xpos cubemap face in the positive x direction
147298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param xneg cubemap face in the negative x direction
147398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param ypos cubemap face in the positive y direction
147498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param yneg cubemap face in the negative y direction
147598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param zpos cubemap face in the positive z direction
147698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param zneg cubemap face in the negative z direction
147798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param mips specifies desired mipmap behaviour for the cubemap
147898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param usage bit field specifying how the cubemap is utilized
147998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
148098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @return allocation containing cubemap data
148198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
148298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
148398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    static public Allocation createCubemapFromCubeFaces(RenderScript rs,
148498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                                        Bitmap xpos,
148598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                                        Bitmap xneg,
148698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                                        Bitmap ypos,
148798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                                        Bitmap yneg,
148898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                                        Bitmap zpos,
148998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                                        Bitmap zneg,
149098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                                        MipmapControl mips,
149198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                                        int usage) {
1492ce8b0e674c93035013d1c33aaabc9bb6ceffde0fTim Murray        /*
149398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        int height = xpos.getHeight();
149498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        if (xpos.getWidth() != height ||
149598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            xneg.getWidth() != height || xneg.getHeight() != height ||
149698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            ypos.getWidth() != height || ypos.getHeight() != height ||
149798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            yneg.getWidth() != height || yneg.getHeight() != height ||
149898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            zpos.getWidth() != height || zpos.getHeight() != height ||
149998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            zneg.getWidth() != height || zneg.getHeight() != height) {
150098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            throw new RSIllegalArgumentException("Only square cube map faces supported");
150198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
150298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        boolean isPow2 = (height & (height - 1)) == 0;
150398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        if (!isPow2) {
150498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            throw new RSIllegalArgumentException("Only power of 2 cube faces supported");
150598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
150698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
150798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        Element e = elementFromBitmap(rs, xpos);
150898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        Type.Builder tb = new Type.Builder(rs, e);
150998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        tb.setX(height);
151098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        tb.setY(height);
151198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        tb.setFaces(true);
151298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        tb.setMipmaps(mips == MipmapControl.MIPMAP_FULL);
151398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        Type t = tb.create();
151498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        Allocation cubemap = Allocation.createTyped(rs, t, mips, usage);
151598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
151698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        AllocationAdapter adapter = AllocationAdapter.create2D(rs, cubemap);
151798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        adapter.setFace(Type.CubemapFace.POSITIVE_X);
151898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        adapter.copyFrom(xpos);
151998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        adapter.setFace(Type.CubemapFace.NEGATIVE_X);
152098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        adapter.copyFrom(xneg);
152198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        adapter.setFace(Type.CubemapFace.POSITIVE_Y);
152298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        adapter.copyFrom(ypos);
152398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        adapter.setFace(Type.CubemapFace.NEGATIVE_Y);
152498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        adapter.copyFrom(yneg);
152598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        adapter.setFace(Type.CubemapFace.POSITIVE_Z);
152698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        adapter.copyFrom(zpos);
152798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        adapter.setFace(Type.CubemapFace.NEGATIVE_Z);
152898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        adapter.copyFrom(zneg);
152998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
153098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        return cubemap;
1531ce8b0e674c93035013d1c33aaabc9bb6ceffde0fTim Murray        */
1532ce8b0e674c93035013d1c33aaabc9bb6ceffde0fTim Murray        return null;
153398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
153498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
153598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
15367d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Creates a non-mipmapped cubemap Allocation for use as a sampler input
15377d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * from 6 {@link android.graphics.Bitmap} objects containing the cube
15387d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * faces. Each face must be a square, have the same size as all other faces,
15397d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * and have a width that is a power of 2.
154098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
154198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param rs Context to which the allocation will belong.
154298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param xpos cubemap face in the positive x direction
154398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param xneg cubemap face in the negative x direction
154498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param ypos cubemap face in the positive y direction
154598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param yneg cubemap face in the negative y direction
154698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param zpos cubemap face in the positive z direction
154798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param zneg cubemap face in the negative z direction
154898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
154998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @return allocation containing cubemap data
155098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
155198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
155298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    static public Allocation createCubemapFromCubeFaces(RenderScript rs,
155398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                                        Bitmap xpos,
155498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                                        Bitmap xneg,
155598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                                        Bitmap ypos,
155698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                                        Bitmap yneg,
155798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                                        Bitmap zpos,
155898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                                        Bitmap zneg) {
155998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        return createCubemapFromCubeFaces(rs, xpos, xneg, ypos, yneg,
156098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                          zpos, zneg, MipmapControl.MIPMAP_NONE,
156198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                          USAGE_GRAPHICS_TEXTURE);
156298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
156398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
1564c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray    /**
15657d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Creates an Allocation from the Bitmap referenced
15667d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * by resource ID.
1567c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray     *
1568c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray     * @param rs Context to which the allocation will belong.
1569c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray     * @param res application resources
1570c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray     * @param id resource id to load the data from
1571c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray     * @param mips specifies desired mipmap behaviour for the
1572c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray     *             allocation
1573c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray     * @param usage bit field specifying how the allocation is
1574c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray     *              utilized
1575c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray     *
15767d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @return Allocation containing resource data
1577c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray     *
1578c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray     */
1579c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray    static public Allocation createFromBitmapResource(RenderScript rs,
1580c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray                                                      Resources res,
1581c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray                                                      int id,
1582c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray                                                      MipmapControl mips,
1583c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray                                                      int usage) {
158498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
1585c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray        rs.validate();
15867d435ae5ba100be5710b685653cc351cab159c11Stephen Hines        if ((usage & (USAGE_SHARED | USAGE_IO_INPUT | USAGE_IO_OUTPUT)) != 0) {
15877d435ae5ba100be5710b685653cc351cab159c11Stephen Hines            throw new RSIllegalArgumentException("Unsupported usage specified.");
15887d435ae5ba100be5710b685653cc351cab159c11Stephen Hines        }
1589c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray        Bitmap b = BitmapFactory.decodeResource(res, id);
1590c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray        Allocation alloc = createFromBitmap(rs, b, mips, usage);
1591c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray        b.recycle();
1592c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray        return alloc;
1593c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray    }
1594c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray
1595c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray    /**
15967d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Creates a non-mipmapped Allocation to use as a graphics texture from the
15977d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * {@link android.graphics.Bitmap} referenced by resource ID.
15987d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     *
15997d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * <p>This allocation will be created with {@link #USAGE_SCRIPT} and
16007d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * {@link #USAGE_GRAPHICS_TEXTURE}.</p>
1601c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray     *
1602c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray     * @param rs Context to which the allocation will belong.
1603c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray     * @param res application resources
1604c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray     * @param id resource id to load the data from
1605c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray     *
16067d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @return Allocation containing resource data
1607c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray     *
1608c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray     */
1609c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray    static public Allocation createFromBitmapResource(RenderScript rs,
1610c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray                                                      Resources res,
1611c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray                                                      int id) {
1612c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray        return createFromBitmapResource(rs, res, id,
1613c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray                                        MipmapControl.MIPMAP_NONE,
16147d435ae5ba100be5710b685653cc351cab159c11Stephen Hines                                        USAGE_SCRIPT | USAGE_GRAPHICS_TEXTURE);
1615c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray    }
1616c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray
1617c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray    /**
16187d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Creates an Allocation containing string data encoded in UTF-8 format.
1619c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray     *
1620c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray     * @param rs Context to which the allocation will belong.
1621c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray     * @param str string to create the allocation from
1622c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray     * @param usage bit field specifying how the allocaiton is
1623c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray     *              utilized
1624c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray     *
1625c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray     */
1626c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray    static public Allocation createFromString(RenderScript rs,
1627c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray                                              String str,
1628c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray                                              int usage) {
1629c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray        rs.validate();
1630c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray        byte[] allocArray = null;
1631c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray        try {
1632c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray            allocArray = str.getBytes("UTF-8");
1633c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray            Allocation alloc = Allocation.createSized(rs, Element.U8(rs), allocArray.length, usage);
1634c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray            alloc.copyFrom(allocArray);
1635c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray            return alloc;
1636c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray        }
1637c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray        catch (Exception e) {
1638c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray            throw new RSRuntimeException("Could not convert string to utf-8.");
1639c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray        }
1640c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray    }
16412da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines}
164237ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams
1643