blob: 9e76f5293bd065af92bf439f73f2e6232c1d8471 [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
Joe Onoratod7b37742009-08-09 22:57:44 -070019import android.content.res.Resources;
Joe Onoratod7b37742009-08-09 22:57:44 -070020
Shih-wei Liao6b32fab2010-12-10 01:03:59 -080021import java.io.File;
Jason Sams69f0d312009-08-03 18:11:17 -070022import java.io.IOException;
23import java.io.InputStream;
Joe Onoratof415cf22009-08-10 15:15:52 -070024
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070025/**
Tim Murrayc11e25c2013-04-09 11:01:01 -070026 * The superclass for all user-defined scripts. This is only
27 * intended to be used by the generated derived classes.
Jason Sams69f0d312009-08-03 18:11:17 -070028 **/
29public class ScriptC extends Script {
Joe Onoratof415cf22009-08-10 15:15:52 -070030 private static final String TAG = "ScriptC";
31
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070032 /**
Jason Sams67e3d202011-01-09 13:49:01 -080033 * Only intended for use by the generated derived classes.
34 *
35 * @param id
36 * @param rs
37 */
Jason Sams3ba02b32010-11-03 23:01:38 -070038 protected ScriptC(int id, RenderScript rs) {
Jason Sams69f0d312009-08-03 18:11:17 -070039 super(id, rs);
40 }
Ashok Bhat0e0c0882014-02-04 14:57:58 +000041 /**
42 * Only intended for use by the generated derived classes.
43 *
44 * @param id
45 * @param rs
46 *
47 * @hide
48 */
49 protected ScriptC(long id, RenderScript rs) {
50 super(id, rs);
51 }
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070052 /**
Jason Sams67e3d202011-01-09 13:49:01 -080053 * Only intended for use by the generated derived classes.
54 *
55 *
56 * @param rs
57 * @param resources
58 * @param resourceID
59 */
Jason Sams3ba02b32010-11-03 23:01:38 -070060 protected ScriptC(RenderScript rs, Resources resources, int resourceID) {
Jason Sams2d71bc72010-03-26 16:06:43 -070061 super(0, rs);
Ashok Bhat0e0c0882014-02-04 14:57:58 +000062 long id = internalCreate(rs, resources, resourceID);
Jason Samsfdc54a92011-01-19 16:14:21 -080063 if (id == 0) {
64 throw new RSRuntimeException("Loading of ScriptC script failed.");
65 }
Jason Sams06d69de2010-11-09 17:11:40 -080066 setID(id);
Jason Sams2d71bc72010-03-26 16:06:43 -070067 }
68
Stephen Hines7d25a822013-04-09 23:51:56 -070069 /**
70 * Name of the file that holds the object cache.
71 */
72 private static final String CACHE_PATH = "com.android.renderscript.cache";
73
74 static String mCachePath;
Jason Sams2d71bc72010-03-26 16:06:43 -070075
Ashok Bhat0e0c0882014-02-04 14:57:58 +000076 private static synchronized long internalCreate(RenderScript rs, Resources resources, int resourceID) {
Jason Sams2d71bc72010-03-26 16:06:43 -070077 byte[] pgm;
78 int pgmLength;
79 InputStream is = resources.openRawResource(resourceID);
80 try {
81 try {
82 pgm = new byte[1024];
83 pgmLength = 0;
84 while(true) {
85 int bytesLeft = pgm.length - pgmLength;
86 if (bytesLeft == 0) {
87 byte[] buf2 = new byte[pgm.length * 2];
88 System.arraycopy(pgm, 0, buf2, 0, pgm.length);
89 pgm = buf2;
90 bytesLeft = pgm.length - pgmLength;
91 }
92 int bytesRead = is.read(pgm, pgmLength, bytesLeft);
93 if (bytesRead <= 0) {
94 break;
95 }
96 pgmLength += bytesRead;
97 }
98 } finally {
99 is.close();
100 }
101 } catch(IOException e) {
102 throw new Resources.NotFoundException();
103 }
104
Logan Chienef72ff22011-07-11 15:32:24 +0800105 String resName = resources.getResourceEntryName(resourceID);
Shih-wei Liaoeeca4352010-12-20 20:45:56 +0800106
Stephen Hines7d25a822013-04-09 23:51:56 -0700107 // Create the RS cache path if we haven't done so already.
108 if (mCachePath == null) {
109 File f = new File(rs.mCacheDir, CACHE_PATH);
110 mCachePath = f.getAbsolutePath();
111 f.mkdirs();
112 }
Tim Murrayda67deb2013-05-09 12:02:50 -0700113 // Log.v(TAG, "Create script for resource = " + resName);
Stephen Hines7d25a822013-04-09 23:51:56 -0700114 return rs.nScriptCCreate(resName, mCachePath, pgm, pgmLength);
Jason Sams2d71bc72010-03-26 16:06:43 -0700115 }
Jason Sams69f0d312009-08-03 18:11:17 -0700116}