PageRenderTime 29ms CodeModel.GetById 17ms app.highlight 9ms RepoModel.GetById 2ms app.codeStats 0ms

/rs/java/android/renderscript/ScriptC.java

https://github.com/aizuzi/platform_frameworks_base
Java | 156 lines | 87 code | 15 blank | 54 comment | 14 complexity | 5cf3bf94a10e10c0ccc2f92d419180be MD5 | raw file
  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 * 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     * Only intended for use by the generated derived classes.
 50     *
 51     * @param id
 52     * @param rs
 53     *
 54     * @hide
 55     */
 56    protected ScriptC(long id, RenderScript rs) {
 57        super(id, rs);
 58    }
 59    /**
 60     * Only intended for use by the generated derived classes.
 61     *
 62     *
 63     * @param rs
 64     * @param resources
 65     * @param resourceID
 66     */
 67    protected ScriptC(RenderScript rs, Resources resources, int resourceID) {
 68        super(0, rs);
 69        long id = internalCreate(rs, resources, resourceID);
 70        if (id == 0) {
 71            throw new RSRuntimeException("Loading of ScriptC script failed.");
 72        }
 73        setID(id);
 74    }
 75
 76    /**
 77     * Only intended for use by the generated derived classes.
 78     *
 79     * @param rs
 80     * @hide
 81     */
 82    protected ScriptC(RenderScript rs, String resName, byte[] bitcode32, byte[] bitcode64) {
 83        super(0, rs);
 84        long id = 0;
 85        if (RenderScript.sPointerSize == 4) {
 86            id = internalStringCreate(rs, resName, bitcode32);
 87        } else {
 88            id = internalStringCreate(rs, resName, bitcode64);
 89        }
 90        if (id == 0) {
 91            throw new RSRuntimeException("Loading of ScriptC script failed.");
 92        }
 93        setID(id);
 94    }
 95
 96    /**
 97     * Name of the file that holds the object cache.
 98     */
 99    private static final String CACHE_PATH = "com.android.renderscript.cache";
100
101    static String mCachePath;
102
103    private static synchronized long internalCreate(RenderScript rs, Resources resources, int resourceID) {
104        byte[] pgm;
105        int pgmLength;
106        InputStream is = resources.openRawResource(resourceID);
107        try {
108            try {
109                pgm = new byte[1024];
110                pgmLength = 0;
111                while(true) {
112                    int bytesLeft = pgm.length - pgmLength;
113                    if (bytesLeft == 0) {
114                        byte[] buf2 = new byte[pgm.length * 2];
115                        System.arraycopy(pgm, 0, buf2, 0, pgm.length);
116                        pgm = buf2;
117                        bytesLeft = pgm.length - pgmLength;
118                    }
119                    int bytesRead = is.read(pgm, pgmLength, bytesLeft);
120                    if (bytesRead <= 0) {
121                        break;
122                    }
123                    pgmLength += bytesRead;
124                }
125            } finally {
126                is.close();
127            }
128        } catch(IOException e) {
129            throw new Resources.NotFoundException();
130        }
131
132        String resName = resources.getResourceEntryName(resourceID);
133
134        // Create the RS cache path if we haven't done so already.
135        if (mCachePath == null) {
136            File f = new File(rs.mCacheDir, CACHE_PATH);
137            mCachePath = f.getAbsolutePath();
138            f.mkdirs();
139        }
140        //        Log.v(TAG, "Create script for resource = " + resName);
141        return rs.nScriptCCreate(resName, mCachePath, pgm, pgmLength);
142    }
143
144    private static synchronized long internalStringCreate(RenderScript rs, String resName, byte[] bitcode) {
145        // Create the RS cache path if we haven't done so already.
146        if (mCachePath == null) {
147            File f = new File(rs.mCacheDir, CACHE_PATH);
148            mCachePath = f.getAbsolutePath();
149            f.mkdirs();
150        }
151        //        Log.v(TAG, "Create script for resource = " + resName);
152        return rs.nScriptCCreate(resName, mCachePath, bitcode, bitcode.length);
153    }
154
155
156}