Aurimas Liutikas | 93554f2 | 2022-04-19 16:51:35 -0700 | [diff] [blame] | 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 | */ |
| 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 | */ |
| 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 | */ |
| 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 | |
| 93 | private static synchronized long internalCreate(RenderScript rs, Resources resources, int resourceID) { |
| 94 | byte[] pgm; |
| 95 | int pgmLength; |
| 96 | InputStream is = resources.openRawResource(resourceID); |
| 97 | try { |
| 98 | try { |
| 99 | pgm = new byte[1024]; |
| 100 | pgmLength = 0; |
| 101 | while(true) { |
| 102 | int bytesLeft = pgm.length - pgmLength; |
| 103 | if (bytesLeft == 0) { |
| 104 | byte[] buf2 = new byte[pgm.length * 2]; |
| 105 | System.arraycopy(pgm, 0, buf2, 0, pgm.length); |
| 106 | pgm = buf2; |
| 107 | bytesLeft = pgm.length - pgmLength; |
| 108 | } |
| 109 | int bytesRead = is.read(pgm, pgmLength, bytesLeft); |
| 110 | if (bytesRead <= 0) { |
| 111 | break; |
| 112 | } |
| 113 | pgmLength += bytesRead; |
| 114 | } |
| 115 | } finally { |
| 116 | is.close(); |
| 117 | } |
| 118 | } catch(IOException e) { |
| 119 | throw new Resources.NotFoundException(); |
| 120 | } |
| 121 | |
| 122 | String resName = resources.getResourceEntryName(resourceID); |
| 123 | String cachePath = rs.getApplicationContext().getCacheDir().toString(); |
| 124 | |
| 125 | // Log.v(TAG, "Create script for resource = " + resName + ", " + pgmLength + ", " + pgm); |
| 126 | //Log.v(TAG, " path = " + cachePath); |
| 127 | return rs.nScriptCCreate(resName, cachePath, pgm, pgmLength); |
| 128 | } |
| 129 | |
| 130 | private static synchronized long internalStringCreate(RenderScript rs, String resName, byte[] bitcode) { |
| 131 | // Log.v(TAG, "Create script for resource = " + resName); |
| 132 | String cachePath = rs.getApplicationContext().getCacheDir().toString(); |
| 133 | return rs.nScriptCCreate(resName, cachePath, bitcode, bitcode.length); |
| 134 | } |
| 135 | |
| 136 | } |