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
60        if (rs.isNative) {
61            RenderScriptThunker rst = (RenderScriptThunker)rs;
62            ScriptCThunker s = new ScriptCThunker(rst, resources, resourceID);
63            mT = s;
64            return;
65        }
66
67        int id = internalCreate(rs, resources, resourceID);
68        if (id == 0) {
69            throw new RSRuntimeException("Loading of ScriptC script failed.");
70        }
71        setID(id);
72    }
73
74
75    private static synchronized int internalCreate(RenderScript rs, Resources resources, int resourceID) {
76        byte[] pgm;
77        int pgmLength;
78        InputStream is = resources.openRawResource(resourceID);
79        try {
80            try {
81                pgm = new byte[1024];
82                pgmLength = 0;
83                while(true) {
84                    int bytesLeft = pgm.length - pgmLength;
85                    if (bytesLeft == 0) {
86                        byte[] buf2 = new byte[pgm.length * 2];
87                        System.arraycopy(pgm, 0, buf2, 0, pgm.length);
88                        pgm = buf2;
89                        bytesLeft = pgm.length - pgmLength;
90                    }
91                    int bytesRead = is.read(pgm, pgmLength, bytesLeft);
92                    if (bytesRead <= 0) {
93                        break;
94                    }
95                    pgmLength += bytesRead;
96                }
97            } finally {
98                is.close();
99            }
100        } catch(IOException e) {
101            throw new Resources.NotFoundException();
102        }
103
104        String resName = resources.getResourceEntryName(resourceID);
105        String cachePath = rs.getApplicationContext().getCacheDir().toString();
106
107        //        Log.v(TAG, "Create script for resource = " + resName + ", " + pgmLength + ", " + pgm);
108        //Log.v(TAG, " path = " + cachePath);
109        return rs.nScriptCCreate(resName, cachePath, pgm, pgmLength);
110    }
111}
112