ScriptC.java revision 21b4103e42cb0fa004cc4a978f49f63e7668ab0b
1/*
2 * Copyright (C) 2008 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.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 *
34 **/
35public class ScriptC extends Script {
36    private static final String TAG = "ScriptC";
37
38    /**
39     * Only intended for use by the generated derived classes.
40     *
41     * @param id
42     * @param rs
43     */
44    protected ScriptC(int id, RenderScript rs) {
45        super(id, rs);
46    }
47
48    /**
49     * Only intended for use by the generated derived classes.
50     *
51     *
52     * @param rs
53     * @param resources
54     * @param resourceID
55     */
56    protected ScriptC(RenderScript rs, Resources resources, int resourceID) {
57        super(0, rs);
58        int id = internalCreate(rs, resources, resourceID);
59        setID(id);
60    }
61
62
63    private static synchronized int internalCreate(RenderScript rs, Resources resources, int resourceID) {
64        byte[] pgm;
65        int pgmLength;
66        InputStream is = resources.openRawResource(resourceID);
67        try {
68            try {
69                pgm = new byte[1024];
70                pgmLength = 0;
71                while(true) {
72                    int bytesLeft = pgm.length - pgmLength;
73                    if (bytesLeft == 0) {
74                        byte[] buf2 = new byte[pgm.length * 2];
75                        System.arraycopy(pgm, 0, buf2, 0, pgm.length);
76                        pgm = buf2;
77                        bytesLeft = pgm.length - pgmLength;
78                    }
79                    int bytesRead = is.read(pgm, pgmLength, bytesLeft);
80                    if (bytesRead <= 0) {
81                        break;
82                    }
83                    pgmLength += bytesRead;
84                }
85            } finally {
86                is.close();
87            }
88        } catch(IOException e) {
89            throw new Resources.NotFoundException();
90        }
91
92        rs.nScriptCBegin();
93        rs.nScriptCSetScript(pgm, 0, pgmLength);
94
95        // E.g, /system/apps/Fountain.apk
96        String packageName = rs.getApplicationContext().getPackageResourcePath();
97        // For res/raw/fountain.bc, it wil be /com.android.fountain:raw/fountain
98        String resName = resources.getResourceName(resourceID);
99        String cacheDir = rs.getApplicationContext().getCacheDir().toString();
100
101        Log.v(TAG, "Create script for resource = " + resName);
102        return rs.nScriptCCreate(packageName, resName, cacheDir);
103    }
104}
105