blob: e9f6031e5048669dedd7ccea3dcfb00ef63d7285 [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
Jean-Françoisf4dd7432023-10-18 17:27:16 +020019import android.app.compat.CompatChanges;
20import android.compat.annotation.ChangeId;
21import android.compat.annotation.EnabledAfter;
Joe Onoratod7b37742009-08-09 22:57:44 -070022import android.content.res.Resources;
Jean-Françoisf4dd7432023-10-18 17:27:16 +020023import android.util.Slog;
Joe Onoratod7b37742009-08-09 22:57:44 -070024
Jason Sams69f0d312009-08-03 18:11:17 -070025import java.io.IOException;
26import java.io.InputStream;
Joe Onoratof415cf22009-08-10 15:15:52 -070027
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070028/**
Tim Murrayc11e25c42013-04-09 11:01:01 -070029 * The superclass for all user-defined scripts. This is only
30 * intended to be used by the generated derived classes.
Xusong Wang1f8dc652021-01-05 10:09:52 -080031 *
32 * @deprecated Renderscript has been deprecated in API level 31. Please refer to the <a
33 * href="https://developer.android.com/guide/topics/renderscript/migration-guide">migration
34 * guide</a> for the proposed alternatives.
Jason Sams69f0d312009-08-03 18:11:17 -070035 **/
Xusong Wang1f8dc652021-01-05 10:09:52 -080036@Deprecated
Jason Sams69f0d312009-08-03 18:11:17 -070037public class ScriptC extends Script {
Joe Onoratof415cf22009-08-10 15:15:52 -070038 private static final String TAG = "ScriptC";
39
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070040 /**
Jean-Françoisa8319392024-02-19 15:33:52 +010041 * In targetSdkVersion 36 and above, Renderscript's ScriptC stops being supported
Jean-Françoisf4dd7432023-10-18 17:27:16 +020042 * and an exception is thrown when the class is instantiated.
Jean-Françoisa8319392024-02-19 15:33:52 +010043 * In targetSdkVersion 35 and below, Renderscript's ScriptC still works.
Jean-Françoisf4dd7432023-10-18 17:27:16 +020044 */
45 @ChangeId
Jean-Françoisa8319392024-02-19 15:33:52 +010046 @EnabledAfter(targetSdkVersion = 36)
Jean-Françoisf4dd7432023-10-18 17:27:16 +020047 private static final long RENDERSCRIPT_SCRIPTC_DEPRECATION_CHANGE_ID = 297019750L;
48
49 /**
Jason Sams67e3d202011-01-09 13:49:01 -080050 * Only intended for use by the generated derived classes.
51 *
52 * @param id
53 * @param rs
54 */
Jason Sams3ba02b32010-11-03 23:01:38 -070055 protected ScriptC(int id, RenderScript rs) {
Jason Sams69f0d312009-08-03 18:11:17 -070056 super(id, rs);
57 }
Ashok Bhat0e0c0882014-02-04 14:57:58 +000058 /**
59 * Only intended for use by the generated derived classes.
60 *
61 * @param id
62 * @param rs
63 *
Ashok Bhat0e0c0882014-02-04 14:57:58 +000064 */
65 protected ScriptC(long id, RenderScript rs) {
66 super(id, rs);
67 }
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070068 /**
Jason Sams67e3d202011-01-09 13:49:01 -080069 * Only intended for use by the generated derived classes.
70 *
71 *
72 * @param rs
73 * @param resources
74 * @param resourceID
75 */
Jason Sams3ba02b32010-11-03 23:01:38 -070076 protected ScriptC(RenderScript rs, Resources resources, int resourceID) {
Jason Sams2d71bc72010-03-26 16:06:43 -070077 super(0, rs);
Ashok Bhat0e0c0882014-02-04 14:57:58 +000078 long id = internalCreate(rs, resources, resourceID);
Jason Samsfdc54a92011-01-19 16:14:21 -080079 if (id == 0) {
80 throw new RSRuntimeException("Loading of ScriptC script failed.");
81 }
Jason Sams06d69de2010-11-09 17:11:40 -080082 setID(id);
Jason Sams2d71bc72010-03-26 16:06:43 -070083 }
84
Stephen Hines7d25a822013-04-09 23:51:56 -070085 /**
Tim Murray56f9e6f2014-05-16 11:47:26 -070086 * Only intended for use by the generated derived classes.
87 *
88 * @param rs
Tim Murray56f9e6f2014-05-16 11:47:26 -070089 */
90 protected ScriptC(RenderScript rs, String resName, byte[] bitcode32, byte[] bitcode64) {
91 super(0, rs);
92 long id = 0;
93 if (RenderScript.sPointerSize == 4) {
94 id = internalStringCreate(rs, resName, bitcode32);
95 } else {
96 id = internalStringCreate(rs, resName, bitcode64);
97 }
98 if (id == 0) {
99 throw new RSRuntimeException("Loading of ScriptC script failed.");
100 }
101 setID(id);
102 }
103
Jean-François4f585f72024-02-12 15:22:15 +0100104 private static void throwExceptionIfScriptCUnsupported() {
105 // Checks that this device actually does have an ABI that supports ScriptC.
106 //
107 // For an explanation as to why `System.loadLibrary` is used, see discussion at
108 // https://android-review.googlesource.com/c/platform/frameworks/base/+/2957974/comment/2f908b80_a05292ee
109 try {
110 System.loadLibrary("RS");
111 } catch (UnsatisfiedLinkError e) {
112 String s = "This device does not have an ABI that supports ScriptC.";
113 throw new UnsupportedOperationException(s);
114 }
115
Jean-Françoisa8319392024-02-19 15:33:52 +0100116 // Throw an exception if the target API is 36 or above
Jean-Françoisf4dd7432023-10-18 17:27:16 +0200117 String message =
Jean-Françoisa8319392024-02-19 15:33:52 +0100118 "ScriptC scripts are not supported when targeting an API Level >= 36. Please refer "
Jean-Françoisf4dd7432023-10-18 17:27:16 +0200119 + "to https://developer.android.com/guide/topics/renderscript/migration-guide "
120 + "for proposed alternatives.";
121 Slog.w(TAG, message);
122 if (CompatChanges.isChangeEnabled(RENDERSCRIPT_SCRIPTC_DEPRECATION_CHANGE_ID)) {
123 throw new UnsupportedOperationException(message);
124 }
125 }
126
Jean-François Geyelinfb8f4d82023-10-03 17:25:24 +0000127 private static synchronized long internalCreate(RenderScript rs, Resources resources, int resourceID) {
Jean-François4f585f72024-02-12 15:22:15 +0100128 throwExceptionIfScriptCUnsupported();
Jason Sams2d71bc72010-03-26 16:06:43 -0700129 byte[] pgm;
130 int pgmLength;
131 InputStream is = resources.openRawResource(resourceID);
132 try {
133 try {
134 pgm = new byte[1024];
135 pgmLength = 0;
136 while(true) {
137 int bytesLeft = pgm.length - pgmLength;
138 if (bytesLeft == 0) {
139 byte[] buf2 = new byte[pgm.length * 2];
140 System.arraycopy(pgm, 0, buf2, 0, pgm.length);
141 pgm = buf2;
142 bytesLeft = pgm.length - pgmLength;
143 }
144 int bytesRead = is.read(pgm, pgmLength, bytesLeft);
145 if (bytesRead <= 0) {
146 break;
147 }
148 pgmLength += bytesRead;
149 }
150 } finally {
151 is.close();
152 }
153 } catch(IOException e) {
154 throw new Resources.NotFoundException();
155 }
156
Logan Chienef72ff22011-07-11 15:32:24 +0800157 String resName = resources.getResourceEntryName(resourceID);
Shih-wei Liaoeeca4352010-12-20 20:45:56 +0800158
Tim Murrayda67deb2013-05-09 12:02:50 -0700159 // Log.v(TAG, "Create script for resource = " + resName);
Yang Ni689f6372016-03-10 16:12:31 -0800160 return rs.nScriptCCreate(resName, RenderScript.getCachePath(), pgm, pgmLength);
Jason Sams2d71bc72010-03-26 16:06:43 -0700161 }
Tim Murray56f9e6f2014-05-16 11:47:26 -0700162
163 private static synchronized long internalStringCreate(RenderScript rs, String resName, byte[] bitcode) {
Tim Murray56f9e6f2014-05-16 11:47:26 -0700164 // Log.v(TAG, "Create script for resource = " + resName);
Jean-François4f585f72024-02-12 15:22:15 +0100165 throwExceptionIfScriptCUnsupported();
Yang Ni689f6372016-03-10 16:12:31 -0800166 return rs.nScriptCCreate(resName, RenderScript.getCachePath(), bitcode, bitcode.length);
Tim Murray56f9e6f2014-05-16 11:47:26 -0700167 }
Jason Sams69f0d312009-08-03 18:11:17 -0700168}