blob: cdb2b0811c68e6cbe6e2354ecb7623615a3a43fd [file] [log] [blame]
Jason Sams69f0d312009-08-03 18:11:17 -07001/*
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
Shih-wei Liao6b32fab2010-12-10 01:03:59 -080019import android.content.Context;
Joe Onoratod7b37742009-08-09 22:57:44 -070020import android.content.res.Resources;
21import android.util.Log;
22
Shih-wei Liao6b32fab2010-12-10 01:03:59 -080023import java.io.File;
Jason Sams69f0d312009-08-03 18:11:17 -070024import java.io.IOException;
25import java.io.InputStream;
Joe Onoratod7b37742009-08-09 22:57:44 -070026import java.util.Map.Entry;
27import java.util.HashMap;
Jason Sams69f0d312009-08-03 18:11:17 -070028
Joe Onoratof415cf22009-08-10 15:15:52 -070029import java.lang.reflect.Field;
30import java.lang.reflect.Modifier;
31
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070032/**
Tim Murrayc11e25c2013-04-09 11:01:01 -070033 * The superclass for all user-defined scripts. This is only
34 * intended to be used by the generated derived classes.
Jason Sams69f0d312009-08-03 18:11:17 -070035 **/
36public class ScriptC extends Script {
Joe Onoratof415cf22009-08-10 15:15:52 -070037 private static final String TAG = "ScriptC";
38
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070039 /**
Jason Sams67e3d202011-01-09 13:49:01 -080040 * Only intended for use by the generated derived classes.
41 *
42 * @param id
43 * @param rs
44 */
Jason Sams3ba02b32010-11-03 23:01:38 -070045 protected ScriptC(int id, RenderScript rs) {
Jason Sams69f0d312009-08-03 18:11:17 -070046 super(id, rs);
47 }
Ashok Bhat0e0c0882014-02-04 14:57:58 +000048 /**
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 }
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070059 /**
Jason Sams67e3d202011-01-09 13:49:01 -080060 * Only intended for use by the generated derived classes.
61 *
62 *
63 * @param rs
64 * @param resources
65 * @param resourceID
66 */
Jason Sams3ba02b32010-11-03 23:01:38 -070067 protected ScriptC(RenderScript rs, Resources resources, int resourceID) {
Jason Sams2d71bc72010-03-26 16:06:43 -070068 super(0, rs);
Ashok Bhat0e0c0882014-02-04 14:57:58 +000069 long id = internalCreate(rs, resources, resourceID);
Jason Samsfdc54a92011-01-19 16:14:21 -080070 if (id == 0) {
71 throw new RSRuntimeException("Loading of ScriptC script failed.");
72 }
Jason Sams06d69de2010-11-09 17:11:40 -080073 setID(id);
Jason Sams2d71bc72010-03-26 16:06:43 -070074 }
75
Stephen Hines7d25a822013-04-09 23:51:56 -070076 /**
77 * Name of the file that holds the object cache.
78 */
79 private static final String CACHE_PATH = "com.android.renderscript.cache";
80
81 static String mCachePath;
Jason Sams2d71bc72010-03-26 16:06:43 -070082
Ashok Bhat0e0c0882014-02-04 14:57:58 +000083 private static synchronized long internalCreate(RenderScript rs, Resources resources, int resourceID) {
Jason Sams2d71bc72010-03-26 16:06:43 -070084 byte[] pgm;
85 int pgmLength;
86 InputStream is = resources.openRawResource(resourceID);
87 try {
88 try {
89 pgm = new byte[1024];
90 pgmLength = 0;
91 while(true) {
92 int bytesLeft = pgm.length - pgmLength;
93 if (bytesLeft == 0) {
94 byte[] buf2 = new byte[pgm.length * 2];
95 System.arraycopy(pgm, 0, buf2, 0, pgm.length);
96 pgm = buf2;
97 bytesLeft = pgm.length - pgmLength;
98 }
99 int bytesRead = is.read(pgm, pgmLength, bytesLeft);
100 if (bytesRead <= 0) {
101 break;
102 }
103 pgmLength += bytesRead;
104 }
105 } finally {
106 is.close();
107 }
108 } catch(IOException e) {
109 throw new Resources.NotFoundException();
110 }
111
Logan Chienef72ff22011-07-11 15:32:24 +0800112 String resName = resources.getResourceEntryName(resourceID);
Shih-wei Liaoeeca4352010-12-20 20:45:56 +0800113
Stephen Hines7d25a822013-04-09 23:51:56 -0700114 // Create the RS cache path if we haven't done so already.
115 if (mCachePath == null) {
116 File f = new File(rs.mCacheDir, CACHE_PATH);
117 mCachePath = f.getAbsolutePath();
118 f.mkdirs();
119 }
Tim Murrayda67deb2013-05-09 12:02:50 -0700120 // Log.v(TAG, "Create script for resource = " + resName);
Stephen Hines7d25a822013-04-09 23:51:56 -0700121 return rs.nScriptCCreate(resName, mCachePath, pgm, pgmLength);
Jason Sams2d71bc72010-03-26 16:06:43 -0700122 }
Jason Sams69f0d312009-08-03 18:11:17 -0700123}