1 /* 2 * Copyright (C) 2012 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 androidx.renderscript; 18 19 import android.content.Context; 20 import android.content.res.Resources; 21 import android.util.Log; 22 23 import java.io.File; 24 import java.io.IOException; 25 import java.io.InputStream; 26 import java.util.Map.Entry; 27 import java.util.HashMap; 28 29 import java.lang.reflect.Field; 30 import 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 * 36 * @deprecated Renderscript has been deprecated in API level 31. Please refer to the <a 37 * href="https://developer.android.com/guide/topics/renderscript/migration-guide">migration 38 * guide</a> for the proposed alternatives. 39 **/ 40 @Deprecated 41 public class ScriptC extends Script { 42 private static final String TAG = "ScriptC"; 43 44 /** 45 * Only intended for use by the generated derived classes. 46 * 47 * @param id 48 * @param rs 49 */ ScriptC(long id, RenderScript rs)50 protected ScriptC(long id, RenderScript rs) { 51 super(id, rs); 52 } 53 54 /** 55 * Only intended for use by the generated derived classes. 56 * 57 * 58 * @param rs 59 * @param resources 60 * @param resourceID 61 */ ScriptC(RenderScript rs, Resources resources, int resourceID)62 protected ScriptC(RenderScript rs, Resources resources, int resourceID) { 63 super(0, rs); 64 long id = internalCreate(rs, resources, resourceID); 65 if (id == 0) { 66 throw new RSRuntimeException("Loading of ScriptC script failed."); 67 } 68 setID(id); 69 } 70 71 /** 72 * Only intended for use by the generated derived classes. 73 * 74 * @param rs 75 * @param resName 76 * @param bitcode32 77 * @param bitcode64 78 */ ScriptC(RenderScript rs, String resName, byte[] bitcode32, byte[] bitcode64)79 protected ScriptC(RenderScript rs, String resName, byte[] bitcode32, byte[] bitcode64) { 80 super(0, rs); 81 long id = 0; 82 if (RenderScript.sPointerSize == 4) { 83 id = internalStringCreate(rs, resName, bitcode32); 84 } else { 85 id = internalStringCreate(rs, resName, bitcode64); 86 } 87 if (id == 0) { 88 throw new RSRuntimeException("Loading of ScriptC script failed."); 89 } 90 setID(id); 91 } 92 throwExceptionIfSDKTooHigh(RenderScript rs)93 private static void throwExceptionIfSDKTooHigh(RenderScript rs) { 94 String message = 95 "ScriptC scripts are not supported when targeting an API Level > 34. Please refer " 96 + "to https://developer.android.com/guide/topics/renderscript/migration-guide " 97 + "for proposed alternatives."; 98 Log.w(TAG, message); 99 // Inlining of the Build.VERSION_CODES.UPSIDE_DOWN_CAKE constant because it is not available 100 // when building with older SDKs. 101 // https://developer.android.com/reference/android/os/Build.VERSION_CODES#UPSIDE_DOWN_CAKE 102 final int maxSupportedTargetSDKVersion = 34; 103 int targetSdkVersion = rs.getApplicationContext().getApplicationInfo().targetSdkVersion; 104 if (targetSdkVersion > maxSupportedTargetSDKVersion) { 105 throw new UnsupportedOperationException(message); 106 } 107 } 108 internalCreate(RenderScript rs, Resources resources, int resourceID)109 private static synchronized long internalCreate(RenderScript rs, Resources resources, int resourceID) { 110 throwExceptionIfSDKTooHigh(rs); 111 byte[] pgm; 112 int pgmLength; 113 InputStream is = resources.openRawResource(resourceID); 114 try { 115 try { 116 pgm = new byte[1024]; 117 pgmLength = 0; 118 while(true) { 119 int bytesLeft = pgm.length - pgmLength; 120 if (bytesLeft == 0) { 121 byte[] buf2 = new byte[pgm.length * 2]; 122 System.arraycopy(pgm, 0, buf2, 0, pgm.length); 123 pgm = buf2; 124 bytesLeft = pgm.length - pgmLength; 125 } 126 int bytesRead = is.read(pgm, pgmLength, bytesLeft); 127 if (bytesRead <= 0) { 128 break; 129 } 130 pgmLength += bytesRead; 131 } 132 } finally { 133 is.close(); 134 } 135 } catch(IOException e) { 136 throw new Resources.NotFoundException(); 137 } 138 139 String resName = resources.getResourceEntryName(resourceID); 140 String cachePath = rs.getApplicationContext().getCacheDir().toString(); 141 142 // Log.v(TAG, "Create script for resource = " + resName + ", " + pgmLength + ", " + pgm); 143 //Log.v(TAG, " path = " + cachePath); 144 return rs.nScriptCCreate(resName, cachePath, pgm, pgmLength); 145 } 146 internalStringCreate(RenderScript rs, String resName, byte[] bitcode)147 private static synchronized long internalStringCreate(RenderScript rs, String resName, byte[] bitcode) { 148 // Log.v(TAG, "Create script for resource = " + resName); 149 throwExceptionIfSDKTooHigh(rs); 150 String cachePath = rs.getApplicationContext().getCacheDir().toString(); 151 return rs.nScriptCCreate(resName, cachePath, bitcode, bitcode.length); 152 } 153 154 } 155