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 
19 import android.content.res.Resources;
20 
21 import java.io.File;
22 import java.io.IOException;
23 import java.io.InputStream;
24 
25 /**
26  * The superclass for all user-defined scripts. This is only
27  * intended to be used by the generated derived classes.
28  *
29  * @deprecated Renderscript has been deprecated in API level 31. Please refer to the <a
30  * href="https://developer.android.com/guide/topics/renderscript/migration-guide">migration
31  * guide</a> for the proposed alternatives.
32  **/
33 @Deprecated
34 public class ScriptC extends Script {
35     private static final String TAG = "ScriptC";
36 
37     /**
38      * Only intended for use by the generated derived classes.
39      *
40      * @param id
41      * @param rs
42      */
ScriptC(int id, RenderScript rs)43     protected ScriptC(int id, RenderScript rs) {
44         super(id, rs);
45     }
46     /**
47      * Only intended for use by the generated derived classes.
48      *
49      * @param id
50      * @param rs
51      *
52      */
ScriptC(long id, RenderScript rs)53     protected ScriptC(long id, RenderScript rs) {
54         super(id, rs);
55     }
56     /**
57      * Only intended for use by the generated derived classes.
58      *
59      *
60      * @param rs
61      * @param resources
62      * @param resourceID
63      */
ScriptC(RenderScript rs, Resources resources, int resourceID)64     protected ScriptC(RenderScript rs, Resources resources, int resourceID) {
65         super(0, rs);
66         long id = internalCreate(rs, resources, resourceID);
67         if (id == 0) {
68             throw new RSRuntimeException("Loading of ScriptC script failed.");
69         }
70         setID(id);
71     }
72 
73     /**
74      * Only intended for use by the generated derived classes.
75      *
76      * @param rs
77      */
ScriptC(RenderScript rs, String resName, byte[] bitcode32, byte[] bitcode64)78     protected ScriptC(RenderScript rs, String resName, byte[] bitcode32, byte[] bitcode64) {
79         super(0, rs);
80         long id = 0;
81         if (RenderScript.sPointerSize == 4) {
82             id = internalStringCreate(rs, resName, bitcode32);
83         } else {
84             id = internalStringCreate(rs, resName, bitcode64);
85         }
86         if (id == 0) {
87             throw new RSRuntimeException("Loading of ScriptC script failed.");
88         }
89         setID(id);
90     }
91 
internalCreate(RenderScript rs, Resources resources, int resourceID)92     private static synchronized long internalCreate(RenderScript rs, Resources resources, int resourceID) {
93         byte[] pgm;
94         int pgmLength;
95         InputStream is = resources.openRawResource(resourceID);
96         try {
97             try {
98                 pgm = new byte[1024];
99                 pgmLength = 0;
100                 while(true) {
101                     int bytesLeft = pgm.length - pgmLength;
102                     if (bytesLeft == 0) {
103                         byte[] buf2 = new byte[pgm.length * 2];
104                         System.arraycopy(pgm, 0, buf2, 0, pgm.length);
105                         pgm = buf2;
106                         bytesLeft = pgm.length - pgmLength;
107                     }
108                     int bytesRead = is.read(pgm, pgmLength, bytesLeft);
109                     if (bytesRead <= 0) {
110                         break;
111                     }
112                     pgmLength += bytesRead;
113                 }
114             } finally {
115                 is.close();
116             }
117         } catch(IOException e) {
118             throw new Resources.NotFoundException();
119         }
120 
121         String resName = resources.getResourceEntryName(resourceID);
122 
123         //        Log.v(TAG, "Create script for resource = " + resName);
124         return rs.nScriptCCreate(resName, RenderScript.getCachePath(), pgm, pgmLength);
125     }
126 
internalStringCreate(RenderScript rs, String resName, byte[] bitcode)127     private static synchronized long internalStringCreate(RenderScript rs, String resName, byte[] bitcode) {
128         //        Log.v(TAG, "Create script for resource = " + resName);
129         return rs.nScriptCCreate(resName, RenderScript.getCachePath(), bitcode, bitcode.length);
130     }
131 }
132