ScriptC.java revision 470b3b5b70ec71042d30004747c625ca76c4007a
1/*
2 * Copyright (C) 2012 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 android.content.Context;
20import android.content.res.Resources;
21import android.util.Log;
22
23import java.io.File;
24import java.io.IOException;
25import java.io.InputStream;
26import java.util.Map.Entry;
27import java.util.HashMap;
28
29import java.lang.reflect.Field;
30import java.lang.reflect.Modifier;
31
32/**
33 * The superclass for all user-defined scripts. This is only
34 * intended to be used by the generated derived classes.
35 **/
36public class ScriptC extends Script {
37    private static final String TAG = "ScriptC";
38
39    /**
40     * Only intended for use by the generated derived classes.
41     *
42     * @param id
43     * @param rs
44     */
45    protected ScriptC(int id, RenderScript rs) {
46        super(id, rs);
47    }
48
49    /**
50     * Only intended for use by the generated derived classes.
51     *
52     *
53     * @param rs
54     * @param resources
55     * @param resourceID
56     */
57    protected ScriptC(RenderScript rs, Resources resources, int resourceID) {
58        super(0, rs);
59        int id = internalCreate(rs, resources, resourceID);
60        if (id == 0) {
61            throw new RSRuntimeException("Loading of ScriptC script failed.");
62        }
63        setID(id);
64    }
65
66
67    private static synchronized int internalCreate(RenderScript rs, Resources resources, int resourceID) {
68        byte[] pgm;
69        int pgmLength;
70        InputStream is = resources.openRawResource(resourceID);
71        try {
72            try {
73                pgm = new byte[1024];
74                pgmLength = 0;
75                while(true) {
76                    int bytesLeft = pgm.length - pgmLength;
77                    if (bytesLeft == 0) {
78                        byte[] buf2 = new byte[pgm.length * 2];
79                        System.arraycopy(pgm, 0, buf2, 0, pgm.length);
80                        pgm = buf2;
81                        bytesLeft = pgm.length - pgmLength;
82                    }
83                    int bytesRead = is.read(pgm, pgmLength, bytesLeft);
84                    if (bytesRead <= 0) {
85                        break;
86                    }
87                    pgmLength += bytesRead;
88                }
89            } finally {
90                is.close();
91            }
92        } catch(IOException e) {
93            throw new Resources.NotFoundException();
94        }
95
96        String resName = resources.getResourceEntryName(resourceID);
97        String cachePath = rs.getApplicationContext().getCacheDir().toString();
98
99        //        Log.v(TAG, "Create script for resource = " + resName + ", " + pgmLength + ", " + pgm);
100        //Log.v(TAG, " path = " + cachePath);
101        return rs.nScriptCCreate(resName, cachePath, pgm, pgmLength);
102    }
103}
104