1/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.support.v8.renderscript;
18
19import java.lang.reflect.Field;
20import java.lang.reflect.Method;
21
22import android.content.Context;
23import android.content.pm.ApplicationInfo;
24import android.content.pm.PackageManager;
25import android.content.res.AssetManager;
26import android.os.Process;
27import android.util.Log;
28import android.view.Surface;
29
30
31
32class RenderScriptThunker extends RenderScript {
33    android.renderscript.RenderScript mN;
34
35    void validate() {
36        if (mN == null) {
37            throw new RSInvalidStateException("Calling RS with no Context active.");
38        }
39    }
40
41    public void setPriority(Priority p) {
42        try {
43            if (p == Priority.LOW) mN.setPriority(android.renderscript.RenderScript.Priority.LOW);
44            if (p == Priority.NORMAL) mN.setPriority(android.renderscript.RenderScript.Priority.NORMAL);
45        } catch (android.renderscript.RSRuntimeException e) {
46            throw ExceptionThunker.convertException(e);
47        }
48    }
49
50    RenderScriptThunker(Context ctx) {
51        super(ctx);
52        isNative = true;
53    }
54
55    public static RenderScript create(Context ctx, int sdkVersion) {
56        try {
57            RenderScriptThunker rs = new RenderScriptThunker(ctx);
58            Class<?> javaRS = Class.forName("android.renderscript.RenderScript");
59            Class[] signature = {Context.class, Integer.TYPE};
60            Object[] args = {ctx, new Integer(sdkVersion)};
61            Method create = javaRS.getDeclaredMethod("create", signature);
62            rs.mN = (android.renderscript.RenderScript)create.invoke(null, args);
63            return rs;
64        }
65        catch(android.renderscript.RSRuntimeException e) {
66            throw ExceptionThunker.convertException(e);
67        } catch (Exception e) {
68            throw new RSRuntimeException("Failure to create platform RenderScript context");
69        }
70    }
71
72    public void contextDump() {
73        try {
74            mN.contextDump();
75        } catch (android.renderscript.RSRuntimeException e) {
76            throw ExceptionThunker.convertException(e);
77        }
78    }
79
80    public void finish() {
81        try {
82            mN.finish();
83        } catch (android.renderscript.RSRuntimeException e) {
84            throw ExceptionThunker.convertException(e);
85        }
86    }
87
88    public void destroy() {
89        try {
90            mN.destroy();
91            mN = null;
92        } catch (android.renderscript.RSRuntimeException e) {
93            throw ExceptionThunker.convertException(e);
94        }
95
96    }
97
98    public void setMessageHandler(RSMessageHandler msg) {
99        mMessageCallback = msg;
100        try {
101            android.renderscript.RenderScript.RSMessageHandler handler =
102                new android.renderscript.RenderScript.RSMessageHandler() {
103                    public void run() {
104                        mMessageCallback.mData = mData;
105                        mMessageCallback.mID = mID;
106                        mMessageCallback.mLength = mLength;
107                        mMessageCallback.run();
108                    }
109                };
110            mN.setMessageHandler(handler);
111        } catch (android.renderscript.RSRuntimeException e) {
112            throw ExceptionThunker.convertException(e);
113        }
114    }
115
116    public void setErrorHandler(RSErrorHandler msg) {
117        mErrorCallback = msg;
118        try {
119            android.renderscript.RenderScript.RSErrorHandler handler =
120                new android.renderscript.RenderScript.RSErrorHandler() {
121                    public void run() {
122                        mErrorCallback.mErrorMessage = mErrorMessage;
123                        mErrorCallback.mErrorNum = mErrorNum;
124                        mErrorCallback.run();
125                    }
126                };
127            mN.setErrorHandler(handler);
128        } catch (android.renderscript.RSRuntimeException e) {
129            throw ExceptionThunker.convertException(e);
130        }
131    }
132
133
134    boolean equals(Object obj1, Object obj2) {
135        if (obj2 instanceof android.support.v8.renderscript.BaseObj) {
136            return ((android.renderscript.BaseObj)obj1).equals(((android.support.v8.renderscript.BaseObj)obj2).getNObj());
137        }
138        return false;
139    }
140}
141