198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams/*
2ce8b0e674c93035013d1c33aaabc9bb6ceffde0fTim Murray * Copyright (C) 2013 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.File;
2098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Samsimport java.lang.reflect.Field;
217720e5ceb6a4cc9ed330ad7c483f7eac08f554e6Tim Murrayimport java.lang.reflect.Method;
2227250e121155f570d1d0db7a1b09fcbaa1361aa4Tim Murrayimport java.util.concurrent.locks.ReentrantReadWriteLock;
2398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
2498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Samsimport android.content.Context;
2598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Samsimport android.content.pm.ApplicationInfo;
2698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Samsimport android.content.pm.PackageManager;
2798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Samsimport android.content.res.AssetManager;
2898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Samsimport android.graphics.Bitmap;
2998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Samsimport android.graphics.BitmapFactory;
3098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Samsimport android.os.Process;
3198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Samsimport android.util.Log;
3298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Samsimport android.view.Surface;
3398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
3498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams/**
357d435ae5ba100be5710b685653cc351cab159c11Stephen Hines * This class provides access to a RenderScript context, which controls RenderScript
367d435ae5ba100be5710b685653cc351cab159c11Stephen Hines * initialization, resource management, and teardown. An instance of the RenderScript
377d435ae5ba100be5710b685653cc351cab159c11Stephen Hines * class must be created before any other RS objects can be created.
3898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams *
3998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams * <div class="special reference">
4098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams * <h3>Developer Guides</h3>
417d435ae5ba100be5710b685653cc351cab159c11Stephen Hines * <p>For more information about creating an application that uses RenderScript, read the
427d435ae5ba100be5710b685653cc351cab159c11Stephen Hines * <a href="{@docRoot}guide/topics/renderscript/index.html">RenderScript</a> developer guide.</p>
4398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams * </div>
4498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams **/
4598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Samspublic class RenderScript {
4698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    static final String LOG_TAG = "RenderScript_jni";
4798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    static final boolean DEBUG  = false;
4898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    @SuppressWarnings({"UnusedDeclaration", "deprecation"})
4998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    static final boolean LOG_ENABLED = false;
5098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
5198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    private Context mApplicationContext;
52ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams
5398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /*
5498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * We use a class initializer to allow the native code to cache some
5598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * field offsets.
5698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
5798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    @SuppressWarnings({"FieldCanBeLocal", "UnusedDeclaration"})
5898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    static boolean sInitialized;
597720e5ceb6a4cc9ed330ad7c483f7eac08f554e6Tim Murray    static boolean sUseGCHooks;
607720e5ceb6a4cc9ed330ad7c483f7eac08f554e6Tim Murray    static Object sRuntime;
617720e5ceb6a4cc9ed330ad7c483f7eac08f554e6Tim Murray    static Method registerNativeAllocation;
627720e5ceb6a4cc9ed330ad7c483f7eac08f554e6Tim Murray    static Method registerNativeFree;
637720e5ceb6a4cc9ed330ad7c483f7eac08f554e6Tim Murray
64298eec502de84a804f0cbc889def7b7625f9f1e0Tim Murray    static Object lock = new Object();
6598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
6698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    // Non-threadsafe functions.
6798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    native int  nDeviceCreate();
6898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    native void nDeviceDestroy(int dev);
6998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    native void nDeviceSetConfig(int dev, int param, int value);
7098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    native int nContextGetUserMessage(int con, int[] data);
7198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    native String nContextGetErrorMessage(int con);
7298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    native int  nContextPeekMessage(int con, int[] subID);
7398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    native void nContextInitToClient(int con);
7498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    native void nContextDeinitToClient(int con);
7598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
76ce8b0e674c93035013d1c33aaabc9bb6ceffde0fTim Murray    static boolean isNative = false;
77ce8b0e674c93035013d1c33aaabc9bb6ceffde0fTim Murray
782e34e412f4ca0b25cfa67edaa838086dc2758c74Tim Murray    static private int sThunk = -1;
7927250e121155f570d1d0db7a1b09fcbaa1361aa4Tim Murray    static private int sSdkVersion = -1;
8027250e121155f570d1d0db7a1b09fcbaa1361aa4Tim Murray
812e34e412f4ca0b25cfa67edaa838086dc2758c74Tim Murray    static boolean shouldThunk() {
822e34e412f4ca0b25cfa67edaa838086dc2758c74Tim Murray        if (sThunk == -1) {
832e34e412f4ca0b25cfa67edaa838086dc2758c74Tim Murray            throw new RSRuntimeException("Can't use RS classes before setting up a RenderScript context");
842e34e412f4ca0b25cfa67edaa838086dc2758c74Tim Murray        } else if (sThunk == 1) {
852e34e412f4ca0b25cfa67edaa838086dc2758c74Tim Murray            return true;
862e34e412f4ca0b25cfa67edaa838086dc2758c74Tim Murray        }
872e34e412f4ca0b25cfa67edaa838086dc2758c74Tim Murray        return false;
882e34e412f4ca0b25cfa67edaa838086dc2758c74Tim Murray    }
892e34e412f4ca0b25cfa67edaa838086dc2758c74Tim Murray
900f5bae87e2e3e3b0e66803122b5c4c7dd36d43ddStephen Hines    /**
910f5bae87e2e3e3b0e66803122b5c4c7dd36d43ddStephen Hines     * Determines whether or not we should be thunking into the native
920f5bae87e2e3e3b0e66803122b5c4c7dd36d43ddStephen Hines     * RenderScript layer or actually using the compatibility library.
930f5bae87e2e3e3b0e66803122b5c4c7dd36d43ddStephen Hines     */
942e34e412f4ca0b25cfa67edaa838086dc2758c74Tim Murray    static private boolean setupThunk(int sdkVersion, Context ctx) {
952e34e412f4ca0b25cfa67edaa838086dc2758c74Tim Murray        if (sThunk == -1) {
96179158fa990160cf3f309f6a87a039e71ab664e8Tim Murray
97179158fa990160cf3f309f6a87a039e71ab664e8Tim Murray            // get the value of the debug.rs.forcecompat property
98179158fa990160cf3f309f6a87a039e71ab664e8Tim Murray            int forcecompat = 0;
99179158fa990160cf3f309f6a87a039e71ab664e8Tim Murray            try {
100179158fa990160cf3f309f6a87a039e71ab664e8Tim Murray                Class<?> sysprop = Class.forName("android.os.SystemProperties");
101179158fa990160cf3f309f6a87a039e71ab664e8Tim Murray                Class[] signature = {String.class, Integer.TYPE};
102179158fa990160cf3f309f6a87a039e71ab664e8Tim Murray                Method getint = sysprop.getDeclaredMethod("getInt", signature);
103179158fa990160cf3f309f6a87a039e71ab664e8Tim Murray                Object[] args = {"debug.rs.forcecompat", new Integer(0)};
104179158fa990160cf3f309f6a87a039e71ab664e8Tim Murray                forcecompat = ((java.lang.Integer)getint.invoke(null, args)).intValue();
105179158fa990160cf3f309f6a87a039e71ab664e8Tim Murray            } catch (Exception e) {
106179158fa990160cf3f309f6a87a039e71ab664e8Tim Murray
107179158fa990160cf3f309f6a87a039e71ab664e8Tim Murray            }
108179158fa990160cf3f309f6a87a039e71ab664e8Tim Murray
10927250e121155f570d1d0db7a1b09fcbaa1361aa4Tim Murray            // use compat on Jelly Bean MR2 if we're requesting SDK 19+
11027250e121155f570d1d0db7a1b09fcbaa1361aa4Tim Murray            if (android.os.Build.VERSION.SDK_INT == 18 && sdkVersion >= 19) {
1112e34e412f4ca0b25cfa67edaa838086dc2758c74Tim Murray                sThunk = 0;
11227250e121155f570d1d0db7a1b09fcbaa1361aa4Tim Murray            }
11327250e121155f570d1d0db7a1b09fcbaa1361aa4Tim Murray            else if ((android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2)
114179158fa990160cf3f309f6a87a039e71ab664e8Tim Murray                     && forcecompat == 0) {
1152e34e412f4ca0b25cfa67edaa838086dc2758c74Tim Murray                sThunk = 1;
1160f5bae87e2e3e3b0e66803122b5c4c7dd36d43ddStephen Hines            } else {
1172e34e412f4ca0b25cfa67edaa838086dc2758c74Tim Murray                sThunk = 0;
1182e34e412f4ca0b25cfa67edaa838086dc2758c74Tim Murray            }
1192e34e412f4ca0b25cfa67edaa838086dc2758c74Tim Murray
1202e34e412f4ca0b25cfa67edaa838086dc2758c74Tim Murray
1212e34e412f4ca0b25cfa67edaa838086dc2758c74Tim Murray            if (sThunk == 1) {
1222e34e412f4ca0b25cfa67edaa838086dc2758c74Tim Murray                // Workarounds that may disable thunking go here
1232e34e412f4ca0b25cfa67edaa838086dc2758c74Tim Murray                ApplicationInfo info;
1242e34e412f4ca0b25cfa67edaa838086dc2758c74Tim Murray                try {
1252e34e412f4ca0b25cfa67edaa838086dc2758c74Tim Murray                    info = ctx.getPackageManager().getApplicationInfo(ctx.getPackageName(),
1262e34e412f4ca0b25cfa67edaa838086dc2758c74Tim Murray                                                                      PackageManager.GET_META_DATA);
1272e34e412f4ca0b25cfa67edaa838086dc2758c74Tim Murray                } catch (PackageManager.NameNotFoundException e) {
1282e34e412f4ca0b25cfa67edaa838086dc2758c74Tim Murray                    // assume no workarounds needed
1292e34e412f4ca0b25cfa67edaa838086dc2758c74Tim Murray                    return true;
1302e34e412f4ca0b25cfa67edaa838086dc2758c74Tim Murray                }
1312e34e412f4ca0b25cfa67edaa838086dc2758c74Tim Murray                long minorVersion = 0;
1322e34e412f4ca0b25cfa67edaa838086dc2758c74Tim Murray
1332e34e412f4ca0b25cfa67edaa838086dc2758c74Tim Murray                // load minorID from reflection
1342e34e412f4ca0b25cfa67edaa838086dc2758c74Tim Murray                try {
1352e34e412f4ca0b25cfa67edaa838086dc2758c74Tim Murray                    Class<?> javaRS = Class.forName("android.renderscript.RenderScript");
1362e34e412f4ca0b25cfa67edaa838086dc2758c74Tim Murray                    Method getMinorID = javaRS.getDeclaredMethod("getMinorID");
1372e34e412f4ca0b25cfa67edaa838086dc2758c74Tim Murray                    minorVersion = ((java.lang.Long)getMinorID.invoke(null)).longValue();
1382e34e412f4ca0b25cfa67edaa838086dc2758c74Tim Murray                } catch (Exception e) {
1392e34e412f4ca0b25cfa67edaa838086dc2758c74Tim Murray                    // minor version remains 0 on devices with no possible WARs
1402e34e412f4ca0b25cfa67edaa838086dc2758c74Tim Murray                }
1412e34e412f4ca0b25cfa67edaa838086dc2758c74Tim Murray
1422e34e412f4ca0b25cfa67edaa838086dc2758c74Tim Murray                if (info.metaData != null) {
1432e34e412f4ca0b25cfa67edaa838086dc2758c74Tim Murray                    // asynchronous teardown: minor version 1+
1442e34e412f4ca0b25cfa67edaa838086dc2758c74Tim Murray                    if (info.metaData.getBoolean("com.android.support.v8.renderscript.EnableAsyncTeardown") == true) {
1452e34e412f4ca0b25cfa67edaa838086dc2758c74Tim Murray                        if (minorVersion == 0) {
1462e34e412f4ca0b25cfa67edaa838086dc2758c74Tim Murray                            sThunk = 0;
1472e34e412f4ca0b25cfa67edaa838086dc2758c74Tim Murray                        }
1482e34e412f4ca0b25cfa67edaa838086dc2758c74Tim Murray                    }
1492e34e412f4ca0b25cfa67edaa838086dc2758c74Tim Murray
1506318b3424d0036b28343d9a4e7d587ec4760b818Jason Sams                    // blur issues on some drivers with 4.4
1516318b3424d0036b28343d9a4e7d587ec4760b818Jason Sams                    if (info.metaData.getBoolean("com.android.support.v8.renderscript.EnableBlurWorkaround") == true) {
15287c148957ae393873f13767854291c68f525fa2eTim Murray                        if (android.os.Build.VERSION.SDK_INT <= 19) {
1536318b3424d0036b28343d9a4e7d587ec4760b818Jason Sams                            //android.util.Log.e("rs", "war on");
1546318b3424d0036b28343d9a4e7d587ec4760b818Jason Sams                            sThunk = 0;
1556318b3424d0036b28343d9a4e7d587ec4760b818Jason Sams                        }
1566318b3424d0036b28343d9a4e7d587ec4760b818Jason Sams                    }
1572e34e412f4ca0b25cfa67edaa838086dc2758c74Tim Murray                }
1582e34e412f4ca0b25cfa67edaa838086dc2758c74Tim Murray                // end of workarounds
1590f5bae87e2e3e3b0e66803122b5c4c7dd36d43ddStephen Hines            }
1600f5bae87e2e3e3b0e66803122b5c4c7dd36d43ddStephen Hines        }
1612e34e412f4ca0b25cfa67edaa838086dc2758c74Tim Murray
1622e34e412f4ca0b25cfa67edaa838086dc2758c74Tim Murray        if (sThunk == 1) {
1630f5bae87e2e3e3b0e66803122b5c4c7dd36d43ddStephen Hines            return true;
1640f5bae87e2e3e3b0e66803122b5c4c7dd36d43ddStephen Hines        }
1650f5bae87e2e3e3b0e66803122b5c4c7dd36d43ddStephen Hines        return false;
1660f5bae87e2e3e3b0e66803122b5c4c7dd36d43ddStephen Hines    }
1670f5bae87e2e3e3b0e66803122b5c4c7dd36d43ddStephen Hines
16898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
16998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * Name of the file that holds the object cache.
17098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
17198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    private static final String CACHE_PATH = "com.android.renderscript.cache";
17298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    static String mCachePath;
17398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
17498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     /**
17598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * Sets the directory to use as a persistent storage for the
17698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * renderscript object file cache.
17798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
17898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @hide
17998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param cacheDir A directory the current process can write to
18098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
18198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public static void setupDiskCache(File cacheDir) {
18298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        File f = new File(cacheDir, CACHE_PATH);
18398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        mCachePath = f.getAbsolutePath();
18498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        f.mkdirs();
18598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
18698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
1877d435ae5ba100be5710b685653cc351cab159c11Stephen Hines    /**
1887d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * ContextType specifies the specific type of context to be created.
1897d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     *
1907d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     */
191f681be1f0ec328acaa311478887352a456d52be8Jason Sams    public enum ContextType {
1927d435ae5ba100be5710b685653cc351cab159c11Stephen Hines        /**
1937d435ae5ba100be5710b685653cc351cab159c11Stephen Hines         * NORMAL context, this is the default and what shipping apps should
1947d435ae5ba100be5710b685653cc351cab159c11Stephen Hines         * use.
1957d435ae5ba100be5710b685653cc351cab159c11Stephen Hines         */
196f681be1f0ec328acaa311478887352a456d52be8Jason Sams        NORMAL (0),
1977d435ae5ba100be5710b685653cc351cab159c11Stephen Hines
1987d435ae5ba100be5710b685653cc351cab159c11Stephen Hines        /**
1997d435ae5ba100be5710b685653cc351cab159c11Stephen Hines         * DEBUG context, perform extra runtime checks to validate the
2007d435ae5ba100be5710b685653cc351cab159c11Stephen Hines         * kernels and APIs are being used as intended.  Get and SetElementAt
2017d435ae5ba100be5710b685653cc351cab159c11Stephen Hines         * will be bounds checked in this mode.
2027d435ae5ba100be5710b685653cc351cab159c11Stephen Hines         */
203f681be1f0ec328acaa311478887352a456d52be8Jason Sams        DEBUG (1),
2047d435ae5ba100be5710b685653cc351cab159c11Stephen Hines
2057d435ae5ba100be5710b685653cc351cab159c11Stephen Hines        /**
2067d435ae5ba100be5710b685653cc351cab159c11Stephen Hines         * PROFILE context, Intended to be used once the first time an
2077d435ae5ba100be5710b685653cc351cab159c11Stephen Hines         * application is run on a new device.  This mode allows the runtime to
2087d435ae5ba100be5710b685653cc351cab159c11Stephen Hines         * do additional testing and performance tuning.
2097d435ae5ba100be5710b685653cc351cab159c11Stephen Hines         */
210f681be1f0ec328acaa311478887352a456d52be8Jason Sams        PROFILE (2);
211f681be1f0ec328acaa311478887352a456d52be8Jason Sams
212f681be1f0ec328acaa311478887352a456d52be8Jason Sams        int mID;
213f681be1f0ec328acaa311478887352a456d52be8Jason Sams        ContextType(int id) {
214f681be1f0ec328acaa311478887352a456d52be8Jason Sams            mID = id;
215f681be1f0ec328acaa311478887352a456d52be8Jason Sams        }
216f681be1f0ec328acaa311478887352a456d52be8Jason Sams    }
21798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
21898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    // Methods below are wrapped to protect the non-threadsafe
21998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    // lockless fifo.
220f681be1f0ec328acaa311478887352a456d52be8Jason Sams    native int  rsnContextCreate(int dev, int ver, int sdkVer, int contextType);
221f681be1f0ec328acaa311478887352a456d52be8Jason Sams    synchronized int nContextCreate(int dev, int ver, int sdkVer, int contextType) {
222f681be1f0ec328acaa311478887352a456d52be8Jason Sams        return rsnContextCreate(dev, ver, sdkVer, contextType);
22398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
22498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    native void rsnContextDestroy(int con);
22598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    synchronized void nContextDestroy() {
22698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        validate();
22727250e121155f570d1d0db7a1b09fcbaa1361aa4Tim Murray
22827250e121155f570d1d0db7a1b09fcbaa1361aa4Tim Murray        // take teardown lock
22927250e121155f570d1d0db7a1b09fcbaa1361aa4Tim Murray        // teardown lock can only be taken when no objects are being destroyed
23027250e121155f570d1d0db7a1b09fcbaa1361aa4Tim Murray        ReentrantReadWriteLock.WriteLock wlock = mRWLock.writeLock();
23127250e121155f570d1d0db7a1b09fcbaa1361aa4Tim Murray        wlock.lock();
23227250e121155f570d1d0db7a1b09fcbaa1361aa4Tim Murray
23327250e121155f570d1d0db7a1b09fcbaa1361aa4Tim Murray        int curCon = mContext;
23427250e121155f570d1d0db7a1b09fcbaa1361aa4Tim Murray        // context is considered dead as of this point
23527250e121155f570d1d0db7a1b09fcbaa1361aa4Tim Murray        mContext = 0;
23627250e121155f570d1d0db7a1b09fcbaa1361aa4Tim Murray
23727250e121155f570d1d0db7a1b09fcbaa1361aa4Tim Murray        wlock.unlock();
23827250e121155f570d1d0db7a1b09fcbaa1361aa4Tim Murray        rsnContextDestroy(curCon);
23998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
24098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    native void rsnContextSetPriority(int con, int p);
24198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    synchronized void nContextSetPriority(int p) {
24298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        validate();
24398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        rsnContextSetPriority(mContext, p);
24498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
24598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    native void rsnContextDump(int con, int bits);
24698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    synchronized void nContextDump(int bits) {
24798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        validate();
24898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        rsnContextDump(mContext, bits);
24998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
25098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    native void rsnContextFinish(int con);
25198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    synchronized void nContextFinish() {
25298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        validate();
25398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        rsnContextFinish(mContext);
25498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
25598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
2562da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines    native void rsnContextSendMessage(int con, int id, int[] data);
2572da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines    synchronized void nContextSendMessage(int id, int[] data) {
2582da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines        validate();
2592da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines        rsnContextSendMessage(mContext, id, data);
2602da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines    }
2612da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines
26227250e121155f570d1d0db7a1b09fcbaa1361aa4Tim Murray    // nObjDestroy is explicitly _not_ synchronous to prevent crashes in finalizers
26398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    native void rsnObjDestroy(int con, int id);
26427250e121155f570d1d0db7a1b09fcbaa1361aa4Tim Murray    void nObjDestroy(int id) {
26598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        // There is a race condition here.  The calling code may be run
26698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        // by the gc while teardown is occuring.  This protects againts
26798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        // deleting dead objects.
26898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        if (mContext != 0) {
26998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            rsnObjDestroy(mContext, id);
27098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
27198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
27298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
27398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    native int  rsnElementCreate(int con, int type, int kind, boolean norm, int vecSize);
27498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    synchronized int nElementCreate(int type, int kind, boolean norm, int vecSize) {
27598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        validate();
27698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        return rsnElementCreate(mContext, type, kind, norm, vecSize);
27798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
27898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    native int  rsnElementCreate2(int con, int[] elements, String[] names, int[] arraySizes);
27998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    synchronized int nElementCreate2(int[] elements, String[] names, int[] arraySizes) {
28098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        validate();
28198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        return rsnElementCreate2(mContext, elements, names, arraySizes);
28298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
28398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    native void rsnElementGetNativeData(int con, int id, int[] elementData);
28498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    synchronized void nElementGetNativeData(int id, int[] elementData) {
28598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        validate();
28698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        rsnElementGetNativeData(mContext, id, elementData);
28798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
28898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    native void rsnElementGetSubElements(int con, int id,
28998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                         int[] IDs, String[] names, int[] arraySizes);
29098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    synchronized void nElementGetSubElements(int id, int[] IDs, String[] names, int[] arraySizes) {
29198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        validate();
29298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        rsnElementGetSubElements(mContext, id, IDs, names, arraySizes);
29398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
29498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
2959fdd33763a41feb9b0906078c660949fb2c3b930Jason Sams    native int rsnTypeCreate(int con, int eid, int x, int y, int z, boolean mips, boolean faces, int yuv);
2969fdd33763a41feb9b0906078c660949fb2c3b930Jason Sams    synchronized int nTypeCreate(int eid, int x, int y, int z, boolean mips, boolean faces, int yuv) {
29798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        validate();
2989fdd33763a41feb9b0906078c660949fb2c3b930Jason Sams        return rsnTypeCreate(mContext, eid, x, y, z, mips, faces, yuv);
29998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
30098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    native void rsnTypeGetNativeData(int con, int id, int[] typeData);
30198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    synchronized void nTypeGetNativeData(int id, int[] typeData) {
30298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        validate();
30398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        rsnTypeGetNativeData(mContext, id, typeData);
30498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
30598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
30698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    native int  rsnAllocationCreateTyped(int con, int type, int mip, int usage, int pointer);
30798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    synchronized int nAllocationCreateTyped(int type, int mip, int usage, int pointer) {
30898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        validate();
30998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        return rsnAllocationCreateTyped(mContext, type, mip, usage, pointer);
31098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
31198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    native int  rsnAllocationCreateFromBitmap(int con, int type, int mip, Bitmap bmp, int usage);
31298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    synchronized int nAllocationCreateFromBitmap(int type, int mip, Bitmap bmp, int usage) {
31398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        validate();
31498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        return rsnAllocationCreateFromBitmap(mContext, type, mip, bmp, usage);
31598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
3169fdd33763a41feb9b0906078c660949fb2c3b930Jason Sams
317c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray    native int  rsnAllocationCreateBitmapBackedAllocation(int con, int type, int mip, Bitmap bmp, int usage);
318c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray    synchronized int nAllocationCreateBitmapBackedAllocation(int type, int mip, Bitmap bmp, int usage) {
319c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray        validate();
320c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray        return rsnAllocationCreateBitmapBackedAllocation(mContext, type, mip, bmp, usage);
321c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray    }
3229fdd33763a41feb9b0906078c660949fb2c3b930Jason Sams
3239fdd33763a41feb9b0906078c660949fb2c3b930Jason Sams
32498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    native int  rsnAllocationCubeCreateFromBitmap(int con, int type, int mip, Bitmap bmp, int usage);
32598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    synchronized int nAllocationCubeCreateFromBitmap(int type, int mip, Bitmap bmp, int usage) {
32698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        validate();
32798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        return rsnAllocationCubeCreateFromBitmap(mContext, type, mip, bmp, usage);
32898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
32998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    native int  rsnAllocationCreateBitmapRef(int con, int type, Bitmap bmp);
33098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    synchronized int nAllocationCreateBitmapRef(int type, Bitmap bmp) {
33198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        validate();
33298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        return rsnAllocationCreateBitmapRef(mContext, type, bmp);
33398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
33498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    native int  rsnAllocationCreateFromAssetStream(int con, int mips, int assetStream, int usage);
33598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    synchronized int nAllocationCreateFromAssetStream(int mips, int assetStream, int usage) {
33698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        validate();
33798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        return rsnAllocationCreateFromAssetStream(mContext, mips, assetStream, usage);
33898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
33998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
34098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    native void  rsnAllocationCopyToBitmap(int con, int alloc, Bitmap bmp);
34198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    synchronized void nAllocationCopyToBitmap(int alloc, Bitmap bmp) {
34298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        validate();
34398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        rsnAllocationCopyToBitmap(mContext, alloc, bmp);
34498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
34598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
34698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
34798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    native void rsnAllocationSyncAll(int con, int alloc, int src);
34898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    synchronized void nAllocationSyncAll(int alloc, int src) {
34998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        validate();
35098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        rsnAllocationSyncAll(mContext, alloc, src);
35198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
352c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray    native void rsnAllocationIoSend(int con, int alloc);
353c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray    synchronized void nAllocationIoSend(int alloc) {
354c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray        validate();
355c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray        rsnAllocationIoSend(mContext, alloc);
356c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray    }
357c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray    native void rsnAllocationIoReceive(int con, int alloc);
358c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray    synchronized void nAllocationIoReceive(int alloc) {
359c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray        validate();
360c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray        rsnAllocationIoReceive(mContext, alloc);
361c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray    }
36298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
36398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
36498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    native void rsnAllocationGenerateMipmaps(int con, int alloc);
36598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    synchronized void nAllocationGenerateMipmaps(int alloc) {
36698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        validate();
36798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        rsnAllocationGenerateMipmaps(mContext, alloc);
36898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
36998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    native void  rsnAllocationCopyFromBitmap(int con, int alloc, Bitmap bmp);
37098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    synchronized void nAllocationCopyFromBitmap(int alloc, Bitmap bmp) {
37198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        validate();
37298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        rsnAllocationCopyFromBitmap(mContext, alloc, bmp);
37398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
37498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
37598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
37698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    native void rsnAllocationData1D(int con, int id, int off, int mip, int count, int[] d, int sizeBytes);
37798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    synchronized void nAllocationData1D(int id, int off, int mip, int count, int[] d, int sizeBytes) {
37898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        validate();
37998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        rsnAllocationData1D(mContext, id, off, mip, count, d, sizeBytes);
38098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
38198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    native void rsnAllocationData1D(int con, int id, int off, int mip, int count, short[] d, int sizeBytes);
38298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    synchronized void nAllocationData1D(int id, int off, int mip, int count, short[] d, int sizeBytes) {
38398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        validate();
38498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        rsnAllocationData1D(mContext, id, off, mip, count, d, sizeBytes);
38598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
38698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    native void rsnAllocationData1D(int con, int id, int off, int mip, int count, byte[] d, int sizeBytes);
38798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    synchronized void nAllocationData1D(int id, int off, int mip, int count, byte[] d, int sizeBytes) {
38898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        validate();
38998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        rsnAllocationData1D(mContext, id, off, mip, count, d, sizeBytes);
39098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
39198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    native void rsnAllocationData1D(int con, int id, int off, int mip, int count, float[] d, int sizeBytes);
39298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    synchronized void nAllocationData1D(int id, int off, int mip, int count, float[] d, int sizeBytes) {
39398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        validate();
39498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        rsnAllocationData1D(mContext, id, off, mip, count, d, sizeBytes);
39598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
39698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
39798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    native void rsnAllocationElementData1D(int con, int id, int xoff, int mip, int compIdx, byte[] d, int sizeBytes);
39898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    synchronized void nAllocationElementData1D(int id, int xoff, int mip, int compIdx, byte[] d, int sizeBytes) {
39998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        validate();
40098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        rsnAllocationElementData1D(mContext, id, xoff, mip, compIdx, d, sizeBytes);
40198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
40298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
40398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    native void rsnAllocationData2D(int con,
40498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                    int dstAlloc, int dstXoff, int dstYoff,
40598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                    int dstMip, int dstFace,
40698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                    int width, int height,
40798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                    int srcAlloc, int srcXoff, int srcYoff,
40898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                    int srcMip, int srcFace);
40998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    synchronized void nAllocationData2D(int dstAlloc, int dstXoff, int dstYoff,
41098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                        int dstMip, int dstFace,
41198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                        int width, int height,
41298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                        int srcAlloc, int srcXoff, int srcYoff,
41398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                        int srcMip, int srcFace) {
41498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        validate();
41598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        rsnAllocationData2D(mContext,
41698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                            dstAlloc, dstXoff, dstYoff,
41798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                            dstMip, dstFace,
41898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                            width, height,
41998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                            srcAlloc, srcXoff, srcYoff,
42098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                            srcMip, srcFace);
42198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
42298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
42398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    native void rsnAllocationData2D(int con, int id, int xoff, int yoff, int mip, int face, int w, int h, byte[] d, int sizeBytes);
42498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    synchronized void nAllocationData2D(int id, int xoff, int yoff, int mip, int face, int w, int h, byte[] d, int sizeBytes) {
42598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        validate();
42698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        rsnAllocationData2D(mContext, id, xoff, yoff, mip, face, w, h, d, sizeBytes);
42798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
42898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    native void rsnAllocationData2D(int con, int id, int xoff, int yoff, int mip, int face, int w, int h, short[] d, int sizeBytes);
42998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    synchronized void nAllocationData2D(int id, int xoff, int yoff, int mip, int face, int w, int h, short[] d, int sizeBytes) {
43098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        validate();
43198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        rsnAllocationData2D(mContext, id, xoff, yoff, mip, face, w, h, d, sizeBytes);
43298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
43398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    native void rsnAllocationData2D(int con, int id, int xoff, int yoff, int mip, int face, int w, int h, int[] d, int sizeBytes);
43498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    synchronized void nAllocationData2D(int id, int xoff, int yoff, int mip, int face, int w, int h, int[] d, int sizeBytes) {
43598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        validate();
43698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        rsnAllocationData2D(mContext, id, xoff, yoff, mip, face, w, h, d, sizeBytes);
43798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
43898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    native void rsnAllocationData2D(int con, int id, int xoff, int yoff, int mip, int face, int w, int h, float[] d, int sizeBytes);
43998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    synchronized void nAllocationData2D(int id, int xoff, int yoff, int mip, int face, int w, int h, float[] d, int sizeBytes) {
44098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        validate();
44198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        rsnAllocationData2D(mContext, id, xoff, yoff, mip, face, w, h, d, sizeBytes);
44298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
44398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    native void rsnAllocationData2D(int con, int id, int xoff, int yoff, int mip, int face, Bitmap b);
44498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    synchronized void nAllocationData2D(int id, int xoff, int yoff, int mip, int face, Bitmap b) {
44598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        validate();
44698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        rsnAllocationData2D(mContext, id, xoff, yoff, mip, face, b);
44798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
44898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
44937ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams    native void rsnAllocationData3D(int con,
45037ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams                                    int dstAlloc, int dstXoff, int dstYoff, int dstZoff,
45137ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams                                    int dstMip,
45237ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams                                    int width, int height, int depth,
45337ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams                                    int srcAlloc, int srcXoff, int srcYoff, int srcZoff,
45437ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams                                    int srcMip);
45537ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams    synchronized void nAllocationData3D(int dstAlloc, int dstXoff, int dstYoff, int dstZoff,
45637ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams                                        int dstMip,
45737ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams                                        int width, int height, int depth,
45837ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams                                        int srcAlloc, int srcXoff, int srcYoff, int srcZoff,
45937ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams                                        int srcMip) {
46037ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams        validate();
46137ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams        rsnAllocationData3D(mContext,
46237ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams                            dstAlloc, dstXoff, dstYoff, dstZoff,
46337ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams                            dstMip, width, height, depth,
46437ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams                            srcAlloc, srcXoff, srcYoff, srcZoff, srcMip);
46537ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams    }
46637ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams
46737ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams    native void rsnAllocationData3D(int con, int id, int xoff, int yoff, int zoff, int mip, int w, int h, int depth, byte[] d, int sizeBytes);
46837ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams    synchronized void nAllocationData3D(int id, int xoff, int yoff, int zoff, int mip, int w, int h, int depth, byte[] d, int sizeBytes) {
46937ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams        validate();
47037ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams        rsnAllocationData3D(mContext, id, xoff, yoff, zoff, mip, w, h, depth, d, sizeBytes);
47137ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams    }
47237ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams    native void rsnAllocationData3D(int con, int id, int xoff, int yoff, int zoff, int mip, int w, int h, int depth, short[] d, int sizeBytes);
47337ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams    synchronized void nAllocationData3D(int id, int xoff, int yoff, int zoff, int mip, int w, int h, int depth, short[] d, int sizeBytes) {
47437ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams        validate();
47537ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams        rsnAllocationData3D(mContext, id, xoff, yoff, zoff, mip, w, h, depth, d, sizeBytes);
47637ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams    }
47737ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams    native void rsnAllocationData3D(int con, int id, int xoff, int yoff, int zoff, int mip, int w, int h, int depth, int[] d, int sizeBytes);
47837ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams    synchronized void nAllocationData3D(int id, int xoff, int yoff, int zoff, int mip, int w, int h, int depth, int[] d, int sizeBytes) {
47937ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams        validate();
48037ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams        rsnAllocationData3D(mContext, id, xoff, yoff, zoff, mip, w, h, depth, d, sizeBytes);
48137ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams    }
48237ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams    native void rsnAllocationData3D(int con, int id, int xoff, int yoff, int zoff, int mip, int w, int h, int depth, float[] d, int sizeBytes);
48337ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams    synchronized void nAllocationData3D(int id, int xoff, int yoff, int zoff, int mip, int w, int h, int depth, float[] d, int sizeBytes) {
48437ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams        validate();
48537ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams        rsnAllocationData3D(mContext, id, xoff, yoff, zoff, mip, w, h, depth, d, sizeBytes);
48637ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams    }
48737ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams
48837ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams
48998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    native void rsnAllocationRead(int con, int id, byte[] d);
49098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    synchronized void nAllocationRead(int id, byte[] d) {
49198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        validate();
49298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        rsnAllocationRead(mContext, id, d);
49398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
49498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    native void rsnAllocationRead(int con, int id, short[] d);
49598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    synchronized void nAllocationRead(int id, short[] d) {
49698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        validate();
49798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        rsnAllocationRead(mContext, id, d);
49898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
49998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    native void rsnAllocationRead(int con, int id, int[] d);
50098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    synchronized void nAllocationRead(int id, int[] d) {
50198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        validate();
50298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        rsnAllocationRead(mContext, id, d);
50398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
50498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    native void rsnAllocationRead(int con, int id, float[] d);
50598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    synchronized void nAllocationRead(int id, float[] d) {
50698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        validate();
50798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        rsnAllocationRead(mContext, id, d);
50898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
50998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    native int  rsnAllocationGetType(int con, int id);
51098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    synchronized int nAllocationGetType(int id) {
51198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        validate();
51298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        return rsnAllocationGetType(mContext, id);
51398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
51498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
51598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    native void rsnAllocationResize1D(int con, int id, int dimX);
51698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    synchronized void nAllocationResize1D(int id, int dimX) {
51798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        validate();
51898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        rsnAllocationResize1D(mContext, id, dimX);
51998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
52098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    native void rsnAllocationResize2D(int con, int id, int dimX, int dimY);
52198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    synchronized void nAllocationResize2D(int id, int dimX, int dimY) {
52298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        validate();
52398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        rsnAllocationResize2D(mContext, id, dimX, dimY);
52498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
52598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
52698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    native void rsnScriptBindAllocation(int con, int script, int alloc, int slot);
52798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    synchronized void nScriptBindAllocation(int script, int alloc, int slot) {
52898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        validate();
52998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        rsnScriptBindAllocation(mContext, script, alloc, slot);
53098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
53198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    native void rsnScriptSetTimeZone(int con, int script, byte[] timeZone);
53298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    synchronized void nScriptSetTimeZone(int script, byte[] timeZone) {
53398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        validate();
53498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        rsnScriptSetTimeZone(mContext, script, timeZone);
53598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
53698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    native void rsnScriptInvoke(int con, int id, int slot);
53798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    synchronized void nScriptInvoke(int id, int slot) {
53898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        validate();
53998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        rsnScriptInvoke(mContext, id, slot);
54098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
54198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    native void rsnScriptForEach(int con, int id, int slot, int ain, int aout, byte[] params);
54298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    native void rsnScriptForEach(int con, int id, int slot, int ain, int aout);
543be995a8c3431dd87ca8877f56d922689eea35ac1Tim Murray    native void rsnScriptForEachClipped(int con, int id, int slot, int ain, int aout, byte[] params,
544be995a8c3431dd87ca8877f56d922689eea35ac1Tim Murray                                        int xstart, int xend, int ystart, int yend, int zstart, int zend);
5452da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines    native void rsnScriptForEachClipped(int con, int id, int slot, int ain, int aout,
5462da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines                                        int xstart, int xend, int ystart, int yend, int zstart, int zend);
54798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    synchronized void nScriptForEach(int id, int slot, int ain, int aout, byte[] params) {
54898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        validate();
54998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        if (params == null) {
55098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            rsnScriptForEach(mContext, id, slot, ain, aout);
55198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        } else {
55298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            rsnScriptForEach(mContext, id, slot, ain, aout, params);
55398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
55498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
5552da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines
556be995a8c3431dd87ca8877f56d922689eea35ac1Tim Murray    synchronized void nScriptForEachClipped(int id, int slot, int ain, int aout, byte[] params,
5572da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines                                            int xstart, int xend, int ystart, int yend, int zstart, int zend) {
558be995a8c3431dd87ca8877f56d922689eea35ac1Tim Murray        validate();
5592da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines        if (params == null) {
5602da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines            rsnScriptForEachClipped(mContext, id, slot, ain, aout, xstart, xend, ystart, yend, zstart, zend);
5612da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines        } else {
5622da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines            rsnScriptForEachClipped(mContext, id, slot, ain, aout, params, xstart, xend, ystart, yend, zstart, zend);
5632da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines        }
564be995a8c3431dd87ca8877f56d922689eea35ac1Tim Murray    }
565be995a8c3431dd87ca8877f56d922689eea35ac1Tim Murray
56698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    native void rsnScriptInvokeV(int con, int id, int slot, byte[] params);
56798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    synchronized void nScriptInvokeV(int id, int slot, byte[] params) {
56898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        validate();
56998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        rsnScriptInvokeV(mContext, id, slot, params);
57098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
57198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    native void rsnScriptSetVarI(int con, int id, int slot, int val);
57298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    synchronized void nScriptSetVarI(int id, int slot, int val) {
57398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        validate();
57498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        rsnScriptSetVarI(mContext, id, slot, val);
57598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
57698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    native void rsnScriptSetVarJ(int con, int id, int slot, long val);
57798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    synchronized void nScriptSetVarJ(int id, int slot, long val) {
57898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        validate();
57998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        rsnScriptSetVarJ(mContext, id, slot, val);
58098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
58198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    native void rsnScriptSetVarF(int con, int id, int slot, float val);
58298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    synchronized void nScriptSetVarF(int id, int slot, float val) {
58398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        validate();
58498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        rsnScriptSetVarF(mContext, id, slot, val);
58598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
58698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    native void rsnScriptSetVarD(int con, int id, int slot, double val);
58798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    synchronized void nScriptSetVarD(int id, int slot, double val) {
58898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        validate();
58998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        rsnScriptSetVarD(mContext, id, slot, val);
59098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
59198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    native void rsnScriptSetVarV(int con, int id, int slot, byte[] val);
59298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    synchronized void nScriptSetVarV(int id, int slot, byte[] val) {
59398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        validate();
59498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        rsnScriptSetVarV(mContext, id, slot, val);
59598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
59698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    native void rsnScriptSetVarVE(int con, int id, int slot, byte[] val,
59798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                  int e, int[] dims);
59898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    synchronized void nScriptSetVarVE(int id, int slot, byte[] val,
59998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                      int e, int[] dims) {
60098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        validate();
60198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        rsnScriptSetVarVE(mContext, id, slot, val, e, dims);
60298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
60398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    native void rsnScriptSetVarObj(int con, int id, int slot, int val);
60498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    synchronized void nScriptSetVarObj(int id, int slot, int val) {
60598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        validate();
60698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        rsnScriptSetVarObj(mContext, id, slot, val);
60798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
60898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
60998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    native int  rsnScriptCCreate(int con, String resName, String cacheDir,
61098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                 byte[] script, int length);
61198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    synchronized int nScriptCCreate(String resName, String cacheDir, byte[] script, int length) {
61298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        validate();
61398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        return rsnScriptCCreate(mContext, resName, cacheDir, script, length);
61498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
61598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
616ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams    native int  rsnScriptIntrinsicCreate(int con, int id, int eid);
617ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams    synchronized int nScriptIntrinsicCreate(int id, int eid) {
618ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams        validate();
619ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams        return rsnScriptIntrinsicCreate(mContext, id, eid);
620ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams    }
621ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams
622ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams    native int  rsnScriptKernelIDCreate(int con, int sid, int slot, int sig);
623ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams    synchronized int nScriptKernelIDCreate(int sid, int slot, int sig) {
624ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams        validate();
625ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams        return rsnScriptKernelIDCreate(mContext, sid, slot, sig);
626ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams    }
627ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams
628ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams    native int  rsnScriptFieldIDCreate(int con, int sid, int slot);
629ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams    synchronized int nScriptFieldIDCreate(int sid, int slot) {
630ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams        validate();
631ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams        return rsnScriptFieldIDCreate(mContext, sid, slot);
632ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams    }
633ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams
634ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams    native int  rsnScriptGroupCreate(int con, int[] kernels, int[] src, int[] dstk, int[] dstf, int[] types);
635ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams    synchronized int nScriptGroupCreate(int[] kernels, int[] src, int[] dstk, int[] dstf, int[] types) {
636ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams        validate();
637ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams        return rsnScriptGroupCreate(mContext, kernels, src, dstk, dstf, types);
638ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams    }
639ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams
640ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams    native void rsnScriptGroupSetInput(int con, int group, int kernel, int alloc);
641ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams    synchronized void nScriptGroupSetInput(int group, int kernel, int alloc) {
642ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams        validate();
643ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams        rsnScriptGroupSetInput(mContext, group, kernel, alloc);
644ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams    }
645ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams
646ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams    native void rsnScriptGroupSetOutput(int con, int group, int kernel, int alloc);
647ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams    synchronized void nScriptGroupSetOutput(int group, int kernel, int alloc) {
648ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams        validate();
649ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams        rsnScriptGroupSetOutput(mContext, group, kernel, alloc);
650ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams    }
651ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams
652ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams    native void rsnScriptGroupExecute(int con, int group);
653ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams    synchronized void nScriptGroupExecute(int group) {
654ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams        validate();
655ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams        rsnScriptGroupExecute(mContext, group);
656ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams    }
657ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams
65898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    native int  rsnSamplerCreate(int con, int magFilter, int minFilter,
65998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                 int wrapS, int wrapT, int wrapR, float aniso);
66098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    synchronized int nSamplerCreate(int magFilter, int minFilter,
66198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                 int wrapS, int wrapT, int wrapR, float aniso) {
66298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        validate();
66398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        return rsnSamplerCreate(mContext, magFilter, minFilter, wrapS, wrapT, wrapR, aniso);
66498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
66598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
66698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
66798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
66827250e121155f570d1d0db7a1b09fcbaa1361aa4Tim Murray
66998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    int     mDev;
67098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    int     mContext;
67127250e121155f570d1d0db7a1b09fcbaa1361aa4Tim Murray    ReentrantReadWriteLock mRWLock;
67298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    @SuppressWarnings({"FieldCanBeLocal"})
67398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    MessageThread mMessageThread;
67498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
67598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    Element mElement_U8;
67698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    Element mElement_I8;
67798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    Element mElement_U16;
67898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    Element mElement_I16;
67998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    Element mElement_U32;
68098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    Element mElement_I32;
68198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    Element mElement_U64;
68298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    Element mElement_I64;
68398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    Element mElement_F32;
68498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    Element mElement_F64;
68598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    Element mElement_BOOLEAN;
68698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
68798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    Element mElement_ELEMENT;
68898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    Element mElement_TYPE;
68998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    Element mElement_ALLOCATION;
69098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    Element mElement_SAMPLER;
69198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    Element mElement_SCRIPT;
69298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
69398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    Element mElement_A_8;
69498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    Element mElement_RGB_565;
69598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    Element mElement_RGB_888;
69698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    Element mElement_RGBA_5551;
69798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    Element mElement_RGBA_4444;
69898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    Element mElement_RGBA_8888;
69998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
70098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    Element mElement_FLOAT_2;
70198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    Element mElement_FLOAT_3;
70298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    Element mElement_FLOAT_4;
70398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
70498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    Element mElement_DOUBLE_2;
70598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    Element mElement_DOUBLE_3;
70698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    Element mElement_DOUBLE_4;
70798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
70898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    Element mElement_UCHAR_2;
70998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    Element mElement_UCHAR_3;
71098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    Element mElement_UCHAR_4;
71198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
71298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    Element mElement_CHAR_2;
71398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    Element mElement_CHAR_3;
71498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    Element mElement_CHAR_4;
71598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
71698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    Element mElement_USHORT_2;
71798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    Element mElement_USHORT_3;
71898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    Element mElement_USHORT_4;
71998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
72098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    Element mElement_SHORT_2;
72198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    Element mElement_SHORT_3;
72298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    Element mElement_SHORT_4;
72398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
72498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    Element mElement_UINT_2;
72598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    Element mElement_UINT_3;
72698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    Element mElement_UINT_4;
72798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
72898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    Element mElement_INT_2;
72998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    Element mElement_INT_3;
73098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    Element mElement_INT_4;
73198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
73298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    Element mElement_ULONG_2;
73398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    Element mElement_ULONG_3;
73498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    Element mElement_ULONG_4;
73598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
73698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    Element mElement_LONG_2;
73798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    Element mElement_LONG_3;
73898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    Element mElement_LONG_4;
73998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
74098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    Element mElement_MATRIX_4X4;
74198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    Element mElement_MATRIX_3X3;
74298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    Element mElement_MATRIX_2X2;
74398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
74498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    Sampler mSampler_CLAMP_NEAREST;
74598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    Sampler mSampler_CLAMP_LINEAR;
74698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    Sampler mSampler_CLAMP_LINEAR_MIP_LINEAR;
74798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    Sampler mSampler_WRAP_NEAREST;
74898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    Sampler mSampler_WRAP_LINEAR;
74998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    Sampler mSampler_WRAP_LINEAR_MIP_LINEAR;
750626e1c2bd520e735a925ed661538ea2accd7f5f0Tim Murray    Sampler mSampler_MIRRORED_REPEAT_NEAREST;
751626e1c2bd520e735a925ed661538ea2accd7f5f0Tim Murray    Sampler mSampler_MIRRORED_REPEAT_LINEAR;
752626e1c2bd520e735a925ed661538ea2accd7f5f0Tim Murray    Sampler mSampler_MIRRORED_REPEAT_LINEAR_MIP_LINEAR;
753626e1c2bd520e735a925ed661538ea2accd7f5f0Tim Murray
75498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
75598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    ///////////////////////////////////////////////////////////////////////////////////
75698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    //
75798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
75898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
7597d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * The base class from which an application should derive in order
7607d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * to receive RS messages from scripts. When a script calls {@code
7617d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * rsSendToClient}, the data fields will be filled, and the run
7627d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * method will be called on a separate thread.  This will occur
7637d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * some time after {@code rsSendToClient} completes in the script,
7647d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * as {@code rsSendToClient} is asynchronous. Message handlers are
7657d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * not guaranteed to have completed when {@link
76660c5b31f4448410221de043873b94797732afa66Stephen Hines     * android.support.v8.renderscript.RenderScript#finish} returns.
76798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
76898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
76998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public static class RSMessageHandler implements Runnable {
77098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        protected int[] mData;
77198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        protected int mID;
77298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        protected int mLength;
77398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        public void run() {
77498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
77598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
77698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
7777d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * If an application is expecting messages, it should set this
7787d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * field to an instance of {@link RSMessageHandler}.  This
7797d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * instance will receive all the user messages sent from {@code
7807d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * sendToClient} by scripts from this context.
78198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
78298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
78398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    RSMessageHandler mMessageCallback = null;
78498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
78598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public void setMessageHandler(RSMessageHandler msg) {
78698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        mMessageCallback = msg;
787ce8b0e674c93035013d1c33aaabc9bb6ceffde0fTim Murray        if (isNative) {
788ce8b0e674c93035013d1c33aaabc9bb6ceffde0fTim Murray            RenderScriptThunker rst = (RenderScriptThunker) this;
78957314f70efef60f5a858802835ec70247e8f1948Tim Murray            rst.setMessageHandler(msg);
790ce8b0e674c93035013d1c33aaabc9bb6ceffde0fTim Murray        }
79198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
79298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public RSMessageHandler getMessageHandler() {
79398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        return mMessageCallback;
79498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
79598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
79698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
7977d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Place a message into the message queue to be sent back to the message
7987d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * handler once all previous commands have been executed.
7997d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     *
8002da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines     * @hide
8012da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines     *
8022da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines     * @param id
8032da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines     * @param data
8042da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines     */
8052da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines    public void sendMessage(int id, int[] data) {
8062da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines        nContextSendMessage(id, data);
8072da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines    }
8082da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines
8092da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines    /**
8107d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * The runtime error handler base class.  An application should derive from this class
8117d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * if it wishes to install an error handler.  When errors occur at runtime,
8127d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * the fields in this class will be filled, and the run method will be called.
81398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
81498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
81598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public static class RSErrorHandler implements Runnable {
81698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        protected String mErrorMessage;
81798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        protected int mErrorNum;
81898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        public void run() {
81998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
82098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
82198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
82298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
82398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * Application Error handler.  All runtime errors will be dispatched to the
82498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * instance of RSAsyncError set here.  If this field is null a
8257d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * {@link RSRuntimeException} will instead be thrown with details about the error.
82698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * This will cause program termaination.
82798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
82898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
82998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    RSErrorHandler mErrorCallback = null;
83098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
83198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public void setErrorHandler(RSErrorHandler msg) {
83298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        mErrorCallback = msg;
833ce8b0e674c93035013d1c33aaabc9bb6ceffde0fTim Murray        if (isNative) {
834ce8b0e674c93035013d1c33aaabc9bb6ceffde0fTim Murray            RenderScriptThunker rst = (RenderScriptThunker) this;
83557314f70efef60f5a858802835ec70247e8f1948Tim Murray            rst.setErrorHandler(msg);
836ce8b0e674c93035013d1c33aaabc9bb6ceffde0fTim Murray        }
83798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
83898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public RSErrorHandler getErrorHandler() {
83998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        return mErrorCallback;
84098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
84198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
84298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
8437d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * RenderScript worker thread priority enumeration.  The default value is
8447d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * NORMAL.  Applications wishing to do background processing should set
8457d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * their priority to LOW to avoid starving forground processes.
84698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
84798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public enum Priority {
84898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        LOW (Process.THREAD_PRIORITY_BACKGROUND + (5 * Process.THREAD_PRIORITY_LESS_FAVORABLE)),
84998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        NORMAL (Process.THREAD_PRIORITY_DISPLAY);
85098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
85198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        int mID;
85298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        Priority(int id) {
85398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            mID = id;
85498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
85598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
85698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
85798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    void validate() {
85898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        if (mContext == 0) {
85998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            throw new RSInvalidStateException("Calling RS with no Context active.");
86098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
86198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
86298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
86398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
86498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
86598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * Change the priority of the worker threads for this context.
86698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
86798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param p New priority to be set.
86898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
86998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public void setPriority(Priority p) {
87098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        validate();
87198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        nContextSetPriority(p.mID);
87298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
87398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
87498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    static class MessageThread extends Thread {
87598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        RenderScript mRS;
87698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        boolean mRun = true;
87798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        int[] mAuxData = new int[2];
87898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
87998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        static final int RS_MESSAGE_TO_CLIENT_NONE = 0;
88098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        static final int RS_MESSAGE_TO_CLIENT_EXCEPTION = 1;
88198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        static final int RS_MESSAGE_TO_CLIENT_RESIZE = 2;
88298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        static final int RS_MESSAGE_TO_CLIENT_ERROR = 3;
88398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        static final int RS_MESSAGE_TO_CLIENT_USER = 4;
88498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
88598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        static final int RS_ERROR_FATAL_UNKNOWN = 0x1000;
88698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
88798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        MessageThread(RenderScript rs) {
88898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            super("RSMessageThread");
88998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            mRS = rs;
89098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
89198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
89298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
89398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        public void run() {
89498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            // This function is a temporary solution.  The final solution will
89598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            // used typed allocations where the message id is the type indicator.
89698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            int[] rbuf = new int[16];
89798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            mRS.nContextInitToClient(mRS.mContext);
89898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            while(mRun) {
89998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                rbuf[0] = 0;
90098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                int msg = mRS.nContextPeekMessage(mRS.mContext, mAuxData);
90198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                int size = mAuxData[1];
90298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                int subID = mAuxData[0];
90398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
90498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                if (msg == RS_MESSAGE_TO_CLIENT_USER) {
90598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                    if ((size>>2) >= rbuf.length) {
90698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                        rbuf = new int[(size + 3) >> 2];
90798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                    }
90898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                    if (mRS.nContextGetUserMessage(mRS.mContext, rbuf) !=
90998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                        RS_MESSAGE_TO_CLIENT_USER) {
9107d435ae5ba100be5710b685653cc351cab159c11Stephen Hines                        throw new RSDriverException("Error processing message from RenderScript.");
91198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                    }
91298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
91398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                    if(mRS.mMessageCallback != null) {
91498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                        mRS.mMessageCallback.mData = rbuf;
91598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                        mRS.mMessageCallback.mID = subID;
91698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                        mRS.mMessageCallback.mLength = size;
91798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                        mRS.mMessageCallback.run();
91898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                    } else {
91998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                        throw new RSInvalidStateException("Received a message from the script with no message handler installed.");
92098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                    }
92198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                    continue;
92298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                }
92398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
92498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                if (msg == RS_MESSAGE_TO_CLIENT_ERROR) {
92598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                    String e = mRS.nContextGetErrorMessage(mRS.mContext);
92698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
92798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                    if (subID >= RS_ERROR_FATAL_UNKNOWN) {
92898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                        throw new RSRuntimeException("Fatal error " + subID + ", details: " + e);
92998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                    }
93098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
93198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                    if(mRS.mErrorCallback != null) {
93298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                        mRS.mErrorCallback.mErrorMessage = e;
93398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                        mRS.mErrorCallback.mErrorNum = subID;
93498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                        mRS.mErrorCallback.run();
93598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                    } else {
9362da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines                        android.util.Log.e(LOG_TAG, "non fatal RS error, " + e);
93798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                        // Do not throw here. In these cases, we do not have
93898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                        // a fatal error.
93998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                    }
94098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                    continue;
94198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                }
94298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
94398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                // 2: teardown.
94498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                // But we want to avoid starving other threads during
94598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                // teardown by yielding until the next line in the destructor
94698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                // can execute to set mRun = false
94798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                try {
94898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                    sleep(1, 0);
94998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                } catch(InterruptedException e) {
95098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                }
95198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            }
952c2e914f174005f5cb4f996877af7bd83ff5a232aTim Murray            //Log.d(LOG_TAG, "MessageThread exiting.");
95398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
95498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
95598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
95698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    RenderScript(Context ctx) {
95798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        if (ctx != null) {
95898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            mApplicationContext = ctx.getApplicationContext();
95998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
96027250e121155f570d1d0db7a1b09fcbaa1361aa4Tim Murray        mRWLock = new ReentrantReadWriteLock();
96198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
96298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
96398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
96498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * Gets the application context associated with the RenderScript context.
96598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
96698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @return The application context.
96798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
96898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public final Context getApplicationContext() {
96998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        return mApplicationContext;
97098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
97198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
97298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
973f681be1f0ec328acaa311478887352a456d52be8Jason Sams     * @hide
974f681be1f0ec328acaa311478887352a456d52be8Jason Sams     */
975f681be1f0ec328acaa311478887352a456d52be8Jason Sams    public static RenderScript create(Context ctx, int sdkVersion) {
976f681be1f0ec328acaa311478887352a456d52be8Jason Sams        return create(ctx, sdkVersion, ContextType.NORMAL);
977f681be1f0ec328acaa311478887352a456d52be8Jason Sams    }
978f681be1f0ec328acaa311478887352a456d52be8Jason Sams
979f681be1f0ec328acaa311478887352a456d52be8Jason Sams    /**
9807d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Create a RenderScript context.
98198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
98298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @hide
98398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param ctx The context.
98498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @return RenderScript
98598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
986f681be1f0ec328acaa311478887352a456d52be8Jason Sams    public static RenderScript create(Context ctx, int sdkVersion, ContextType ct) {
98798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        RenderScript rs = new RenderScript(ctx);
98898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
98927250e121155f570d1d0db7a1b09fcbaa1361aa4Tim Murray        if (sSdkVersion == -1) {
99027250e121155f570d1d0db7a1b09fcbaa1361aa4Tim Murray            sSdkVersion = sdkVersion;
99127250e121155f570d1d0db7a1b09fcbaa1361aa4Tim Murray        } else if (sSdkVersion != sdkVersion) {
99227250e121155f570d1d0db7a1b09fcbaa1361aa4Tim Murray            throw new RSRuntimeException("Can't have two contexts with different SDK versions in support lib");
99327250e121155f570d1d0db7a1b09fcbaa1361aa4Tim Murray        }
99427250e121155f570d1d0db7a1b09fcbaa1361aa4Tim Murray
9952e34e412f4ca0b25cfa67edaa838086dc2758c74Tim Murray        if (setupThunk(sSdkVersion, ctx)) {
996ce8b0e674c93035013d1c33aaabc9bb6ceffde0fTim Murray            android.util.Log.v(LOG_TAG, "RS native mode");
99727250e121155f570d1d0db7a1b09fcbaa1361aa4Tim Murray            return RenderScriptThunker.create(ctx, sSdkVersion);
998ce8b0e674c93035013d1c33aaabc9bb6ceffde0fTim Murray        }
999298eec502de84a804f0cbc889def7b7625f9f1e0Tim Murray        synchronized(lock) {
1000298eec502de84a804f0cbc889def7b7625f9f1e0Tim Murray            if (sInitialized == false) {
1001298eec502de84a804f0cbc889def7b7625f9f1e0Tim Murray                try {
10027720e5ceb6a4cc9ed330ad7c483f7eac08f554e6Tim Murray                    Class<?> vm_runtime = Class.forName("dalvik.system.VMRuntime");
10037720e5ceb6a4cc9ed330ad7c483f7eac08f554e6Tim Murray                    Method get_runtime = vm_runtime.getDeclaredMethod("getRuntime");
10047720e5ceb6a4cc9ed330ad7c483f7eac08f554e6Tim Murray                    sRuntime = get_runtime.invoke(null);
10057720e5ceb6a4cc9ed330ad7c483f7eac08f554e6Tim Murray                    registerNativeAllocation = vm_runtime.getDeclaredMethod("registerNativeAllocation", Integer.TYPE);
10067720e5ceb6a4cc9ed330ad7c483f7eac08f554e6Tim Murray                    registerNativeFree = vm_runtime.getDeclaredMethod("registerNativeFree", Integer.TYPE);
10077720e5ceb6a4cc9ed330ad7c483f7eac08f554e6Tim Murray                    sUseGCHooks = true;
10087720e5ceb6a4cc9ed330ad7c483f7eac08f554e6Tim Murray                } catch (Exception e) {
10097720e5ceb6a4cc9ed330ad7c483f7eac08f554e6Tim Murray                    Log.e(LOG_TAG, "No GC methods");
10107720e5ceb6a4cc9ed330ad7c483f7eac08f554e6Tim Murray                    sUseGCHooks = false;
10117720e5ceb6a4cc9ed330ad7c483f7eac08f554e6Tim Murray                }
10127720e5ceb6a4cc9ed330ad7c483f7eac08f554e6Tim Murray                try {
1013298eec502de84a804f0cbc889def7b7625f9f1e0Tim Murray                    System.loadLibrary("RSSupport");
1014298eec502de84a804f0cbc889def7b7625f9f1e0Tim Murray                    System.loadLibrary("rsjni");
1015298eec502de84a804f0cbc889def7b7625f9f1e0Tim Murray                    sInitialized = true;
1016298eec502de84a804f0cbc889def7b7625f9f1e0Tim Murray                } catch (UnsatisfiedLinkError e) {
1017298eec502de84a804f0cbc889def7b7625f9f1e0Tim Murray                    Log.e(LOG_TAG, "Error loading RS jni library: " + e);
1018298eec502de84a804f0cbc889def7b7625f9f1e0Tim Murray                    throw new RSRuntimeException("Error loading RS jni library: " + e);
1019298eec502de84a804f0cbc889def7b7625f9f1e0Tim Murray                }
1020298eec502de84a804f0cbc889def7b7625f9f1e0Tim Murray            }
1021298eec502de84a804f0cbc889def7b7625f9f1e0Tim Murray        }
1022ce8b0e674c93035013d1c33aaabc9bb6ceffde0fTim Murray
1023573f6071f8d1a11ad662d1eab2cf44b8b1f18074Jason Sams        android.util.Log.v(LOG_TAG, "RS compat mode");
102498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        rs.mDev = rs.nDeviceCreate();
1025f681be1f0ec328acaa311478887352a456d52be8Jason Sams        rs.mContext = rs.nContextCreate(rs.mDev, 0, sdkVersion, ct.mID);
102698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        if (rs.mContext == 0) {
102798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            throw new RSDriverException("Failed to create RS context.");
102898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
102998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        rs.mMessageThread = new MessageThread(rs);
103098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        rs.mMessageThread.start();
1031573f6071f8d1a11ad662d1eab2cf44b8b1f18074Jason Sams        return rs;
103298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
103398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
103498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
10357d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Create a RenderScript context.
103698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
103798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param ctx The context.
103898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @return RenderScript
103998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
104098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public static RenderScript create(Context ctx) {
1041f681be1f0ec328acaa311478887352a456d52be8Jason Sams        return create(ctx, ContextType.NORMAL);
1042f681be1f0ec328acaa311478887352a456d52be8Jason Sams    }
1043f681be1f0ec328acaa311478887352a456d52be8Jason Sams
1044f681be1f0ec328acaa311478887352a456d52be8Jason Sams    /**
10457d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Create a RenderScript context.
1046f681be1f0ec328acaa311478887352a456d52be8Jason Sams     *
1047f681be1f0ec328acaa311478887352a456d52be8Jason Sams     * @hide
1048f681be1f0ec328acaa311478887352a456d52be8Jason Sams     *
1049f681be1f0ec328acaa311478887352a456d52be8Jason Sams     * @param ctx The context.
10507d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param ct The type of context to be created.
1051f681be1f0ec328acaa311478887352a456d52be8Jason Sams     * @return RenderScript
1052f681be1f0ec328acaa311478887352a456d52be8Jason Sams     */
1053f681be1f0ec328acaa311478887352a456d52be8Jason Sams    public static RenderScript create(Context ctx, ContextType ct) {
105498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        int v = ctx.getApplicationInfo().targetSdkVersion;
1055f681be1f0ec328acaa311478887352a456d52be8Jason Sams        return create(ctx, v, ct);
105698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
105798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
105898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
105998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * Print the currently available debugging information about the state of
106098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * the RS context to the log.
106198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
106298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
106398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public void contextDump() {
106498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        validate();
106598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        nContextDump(0);
106698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
106798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
106898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
10697d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Wait for any pending asynchronous opeations (such as copies to a RS
10707d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * allocation or RS script executions) to complete.
107198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
107298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
107398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public void finish() {
107498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        nContextFinish();
107598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
107698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
107798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
10787d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Destroys this RenderScript context.  Once this function is called,
10797d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * using this context or any objects belonging to this context is
10807d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * illegal.
108198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
108298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
108398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public void destroy() {
108498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        validate();
108527250e121155f570d1d0db7a1b09fcbaa1361aa4Tim Murray        nContextFinish();
108698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        nContextDeinitToClient(mContext);
108798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        mMessageThread.mRun = false;
108898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        try {
108998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            mMessageThread.join();
109098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        } catch(InterruptedException e) {
109198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
109298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
109398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        nContextDestroy();
109498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        nDeviceDestroy(mDev);
109598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        mDev = 0;
109698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
109798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
109898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    boolean isAlive() {
109998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        return mContext != 0;
110098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
110198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
110298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    int safeID(BaseObj o) {
110398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        if(o != null) {
110498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            return o.getID(this);
110598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
110698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        return 0;
110798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
110898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams}
1109