Jason Sams | 69f0d31 | 2009-08-03 18:11:17 -0700 | [diff] [blame] | 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 | |
| 17 | package android.renderscript; |
| 18 | |
Joe Onorato | d7b3774 | 2009-08-09 22:57:44 -0700 | [diff] [blame] | 19 | import android.content.res.Resources; |
Joe Onorato | d7b3774 | 2009-08-09 22:57:44 -0700 | [diff] [blame] | 20 | |
Shih-wei Liao | 6b32fab | 2010-12-10 01:03:59 -0800 | [diff] [blame] | 21 | import java.io.File; |
Jason Sams | 69f0d31 | 2009-08-03 18:11:17 -0700 | [diff] [blame] | 22 | import java.io.IOException; |
| 23 | import java.io.InputStream; |
Joe Onorato | f415cf2 | 2009-08-10 15:15:52 -0700 | [diff] [blame] | 24 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 25 | /** |
Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 26 | * The superclass for all user-defined scripts. This is only |
| 27 | * intended to be used by the generated derived classes. |
Jason Sams | 69f0d31 | 2009-08-03 18:11:17 -0700 | [diff] [blame] | 28 | **/ |
| 29 | public class ScriptC extends Script { |
Joe Onorato | f415cf2 | 2009-08-10 15:15:52 -0700 | [diff] [blame] | 30 | private static final String TAG = "ScriptC"; |
| 31 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 32 | /** |
Jason Sams | 67e3d20 | 2011-01-09 13:49:01 -0800 | [diff] [blame] | 33 | * Only intended for use by the generated derived classes. |
| 34 | * |
| 35 | * @param id |
| 36 | * @param rs |
| 37 | */ |
Jason Sams | 3ba02b3 | 2010-11-03 23:01:38 -0700 | [diff] [blame] | 38 | protected ScriptC(int id, RenderScript rs) { |
Jason Sams | 69f0d31 | 2009-08-03 18:11:17 -0700 | [diff] [blame] | 39 | super(id, rs); |
| 40 | } |
Ashok Bhat | 0e0c088 | 2014-02-04 14:57:58 +0000 | [diff] [blame] | 41 | /** |
| 42 | * Only intended for use by the generated derived classes. |
| 43 | * |
| 44 | * @param id |
| 45 | * @param rs |
| 46 | * |
Ashok Bhat | 0e0c088 | 2014-02-04 14:57:58 +0000 | [diff] [blame] | 47 | */ |
| 48 | protected ScriptC(long id, RenderScript rs) { |
| 49 | super(id, rs); |
| 50 | } |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 51 | /** |
Jason Sams | 67e3d20 | 2011-01-09 13:49:01 -0800 | [diff] [blame] | 52 | * Only intended for use by the generated derived classes. |
| 53 | * |
| 54 | * |
| 55 | * @param rs |
| 56 | * @param resources |
| 57 | * @param resourceID |
| 58 | */ |
Jason Sams | 3ba02b3 | 2010-11-03 23:01:38 -0700 | [diff] [blame] | 59 | protected ScriptC(RenderScript rs, Resources resources, int resourceID) { |
Jason Sams | 2d71bc7 | 2010-03-26 16:06:43 -0700 | [diff] [blame] | 60 | super(0, rs); |
Ashok Bhat | 0e0c088 | 2014-02-04 14:57:58 +0000 | [diff] [blame] | 61 | long id = internalCreate(rs, resources, resourceID); |
Jason Sams | fdc54a9 | 2011-01-19 16:14:21 -0800 | [diff] [blame] | 62 | if (id == 0) { |
| 63 | throw new RSRuntimeException("Loading of ScriptC script failed."); |
| 64 | } |
Jason Sams | 06d69de | 2010-11-09 17:11:40 -0800 | [diff] [blame] | 65 | setID(id); |
Jason Sams | 2d71bc7 | 2010-03-26 16:06:43 -0700 | [diff] [blame] | 66 | } |
| 67 | |
Stephen Hines | 7d25a82 | 2013-04-09 23:51:56 -0700 | [diff] [blame] | 68 | /** |
Tim Murray | 56f9e6f | 2014-05-16 11:47:26 -0700 | [diff] [blame] | 69 | * Only intended for use by the generated derived classes. |
| 70 | * |
| 71 | * @param rs |
Tim Murray | 56f9e6f | 2014-05-16 11:47:26 -0700 | [diff] [blame] | 72 | */ |
| 73 | protected ScriptC(RenderScript rs, String resName, byte[] bitcode32, byte[] bitcode64) { |
| 74 | super(0, rs); |
| 75 | long id = 0; |
| 76 | if (RenderScript.sPointerSize == 4) { |
| 77 | id = internalStringCreate(rs, resName, bitcode32); |
| 78 | } else { |
| 79 | id = internalStringCreate(rs, resName, bitcode64); |
| 80 | } |
| 81 | if (id == 0) { |
| 82 | throw new RSRuntimeException("Loading of ScriptC script failed."); |
| 83 | } |
| 84 | setID(id); |
| 85 | } |
| 86 | |
| 87 | /** |
Stephen Hines | 7d25a82 | 2013-04-09 23:51:56 -0700 | [diff] [blame] | 88 | * Name of the file that holds the object cache. |
| 89 | */ |
| 90 | private static final String CACHE_PATH = "com.android.renderscript.cache"; |
| 91 | |
| 92 | static String mCachePath; |
Jason Sams | 2d71bc7 | 2010-03-26 16:06:43 -0700 | [diff] [blame] | 93 | |
Ashok Bhat | 0e0c088 | 2014-02-04 14:57:58 +0000 | [diff] [blame] | 94 | private static synchronized long internalCreate(RenderScript rs, Resources resources, int resourceID) { |
Jason Sams | 2d71bc7 | 2010-03-26 16:06:43 -0700 | [diff] [blame] | 95 | byte[] pgm; |
| 96 | int pgmLength; |
| 97 | InputStream is = resources.openRawResource(resourceID); |
| 98 | try { |
| 99 | try { |
| 100 | pgm = new byte[1024]; |
| 101 | pgmLength = 0; |
| 102 | while(true) { |
| 103 | int bytesLeft = pgm.length - pgmLength; |
| 104 | if (bytesLeft == 0) { |
| 105 | byte[] buf2 = new byte[pgm.length * 2]; |
| 106 | System.arraycopy(pgm, 0, buf2, 0, pgm.length); |
| 107 | pgm = buf2; |
| 108 | bytesLeft = pgm.length - pgmLength; |
| 109 | } |
| 110 | int bytesRead = is.read(pgm, pgmLength, bytesLeft); |
| 111 | if (bytesRead <= 0) { |
| 112 | break; |
| 113 | } |
| 114 | pgmLength += bytesRead; |
| 115 | } |
| 116 | } finally { |
| 117 | is.close(); |
| 118 | } |
| 119 | } catch(IOException e) { |
| 120 | throw new Resources.NotFoundException(); |
| 121 | } |
| 122 | |
Logan Chien | ef72ff2 | 2011-07-11 15:32:24 +0800 | [diff] [blame] | 123 | String resName = resources.getResourceEntryName(resourceID); |
Shih-wei Liao | eeca435 | 2010-12-20 20:45:56 +0800 | [diff] [blame] | 124 | |
Stephen Hines | 7d25a82 | 2013-04-09 23:51:56 -0700 | [diff] [blame] | 125 | // Create the RS cache path if we haven't done so already. |
| 126 | if (mCachePath == null) { |
| 127 | File f = new File(rs.mCacheDir, CACHE_PATH); |
| 128 | mCachePath = f.getAbsolutePath(); |
| 129 | f.mkdirs(); |
| 130 | } |
Tim Murray | da67deb | 2013-05-09 12:02:50 -0700 | [diff] [blame] | 131 | // Log.v(TAG, "Create script for resource = " + resName); |
Stephen Hines | 7d25a82 | 2013-04-09 23:51:56 -0700 | [diff] [blame] | 132 | return rs.nScriptCCreate(resName, mCachePath, pgm, pgmLength); |
Jason Sams | 2d71bc7 | 2010-03-26 16:06:43 -0700 | [diff] [blame] | 133 | } |
Tim Murray | 56f9e6f | 2014-05-16 11:47:26 -0700 | [diff] [blame] | 134 | |
| 135 | private static synchronized long internalStringCreate(RenderScript rs, String resName, byte[] bitcode) { |
| 136 | // Create the RS cache path if we haven't done so already. |
| 137 | if (mCachePath == null) { |
| 138 | File f = new File(rs.mCacheDir, CACHE_PATH); |
| 139 | mCachePath = f.getAbsolutePath(); |
| 140 | f.mkdirs(); |
| 141 | } |
| 142 | // Log.v(TAG, "Create script for resource = " + resName); |
| 143 | return rs.nScriptCCreate(resName, mCachePath, bitcode, bitcode.length); |
| 144 | } |
| 145 | |
| 146 | |
Jason Sams | 69f0d31 | 2009-08-03 18:11:17 -0700 | [diff] [blame] | 147 | } |