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 
20 import android.compat.annotation.UnsupportedAppUsage;
21 import android.content.res.Resources;
22 import android.util.Log;
23 
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.io.UnsupportedEncodingException;
27 
28 
29 /**
30  * @hide
31  *
32  * Program is a base class for all the objects that modify
33  * various stages of the graphics pipeline
34  *
35  * @deprecated Renderscript has been deprecated in API level 31. Please refer to the <a
36  * href="https://developer.android.com/guide/topics/renderscript/migration-guide">migration
37  * guide</a> for the proposed alternatives.
38  **/
39 @Deprecated
40 public class Program extends BaseObj {
41     static final int MAX_INPUT = 8;
42     static final int MAX_OUTPUT = 8;
43     static final int MAX_CONSTANT = 8;
44     static final int MAX_TEXTURE = 8;
45 
46     /**
47      *
48      * TextureType specifies what textures are attached to Program
49      * objects
50      *
51      **/
52     public enum TextureType {
53         @UnsupportedAppUsage
54         TEXTURE_2D (0),
55         TEXTURE_CUBE (1);
56 
57         int mID;
TextureType(int id)58         TextureType(int id) {
59             mID = id;
60         }
61     }
62 
63     enum ProgramParam {
64         INPUT (0),
65         OUTPUT (1),
66         CONSTANT (2),
67         TEXTURE_TYPE (3);
68 
69         int mID;
ProgramParam(int id)70         ProgramParam(int id) {
71             mID = id;
72         }
73     };
74 
75     Element mInputs[];
76     Element mOutputs[];
77     Type mConstants[];
78     TextureType mTextures[];
79     String mTextureNames[];
80     int mTextureCount;
81     String mShader;
82 
Program(long id, RenderScript rs)83     Program(long id, RenderScript rs) {
84         super(id, rs);
85         guard.open("destroy");
86     }
87 
88     /**
89      * Program object can have zero or more constant allocations
90      * associated with it. This method returns the total count.
91      * @return number of constant input types
92      */
getConstantCount()93     public int getConstantCount() {
94         return mConstants != null ? mConstants.length : 0;
95     }
96 
97     /**
98      * Returns the type of the constant buffer used in the program
99      * object. It could be used to query internal elements or create
100      * an allocation to store constant data.
101      * @param slot index of the constant input type to return
102      * @return constant input type
103      */
getConstant(int slot)104     public Type getConstant(int slot) {
105         if (slot < 0 || slot >= mConstants.length) {
106             throw new IllegalArgumentException("Slot ID out of range.");
107         }
108         return mConstants[slot];
109     }
110 
111     /**
112      * Returns the number of textures used in this program object
113      * @return number of texture inputs
114      */
getTextureCount()115     public int getTextureCount() {
116         return mTextureCount;
117     }
118 
119     /**
120      * Returns the type of texture at a given slot. e.g. 2D or Cube
121      * @param slot index of the texture input
122      * @return texture input type
123      */
getTextureType(int slot)124     public TextureType getTextureType(int slot) {
125         if ((slot < 0) || (slot >= mTextureCount)) {
126             throw new IllegalArgumentException("Slot ID out of range.");
127         }
128         return mTextures[slot];
129     }
130 
131     /**
132      * Returns the name of the texture input at a given slot. e.g.
133      * tex0, diffuse, spec
134      * @param slot index of the texture input
135      * @return texture input name
136      */
getTextureName(int slot)137     public String getTextureName(int slot) {
138         if ((slot < 0) || (slot >= mTextureCount)) {
139             throw new IllegalArgumentException("Slot ID out of range.");
140         }
141         return mTextureNames[slot];
142     }
143 
144     /**
145      * Binds a constant buffer to be used as uniform inputs to the
146      * program
147      *
148      * @param a allocation containing uniform data
149      * @param slot index within the program's list of constant
150      *             buffer allocations
151      */
bindConstants(Allocation a, int slot)152     public void bindConstants(Allocation a, int slot) {
153         if (slot < 0 || slot >= mConstants.length) {
154             throw new IllegalArgumentException("Slot ID out of range.");
155         }
156         if (a != null &&
157             a.getType().getID(mRS) != mConstants[slot].getID(mRS)) {
158             throw new IllegalArgumentException("Allocation type does not match slot type.");
159         }
160         long id = a != null ? a.getID(mRS) : 0;
161         mRS.nProgramBindConstants(getID(mRS), slot, id);
162     }
163 
164     /**
165      * Binds a texture to be used in the program
166      *
167      * @param va allocation containing texture data
168      * @param slot index within the program's list of textures
169      *
170      */
bindTexture(Allocation va, int slot)171     public void bindTexture(Allocation va, int slot)
172         throws IllegalArgumentException {
173         mRS.validate();
174         if ((slot < 0) || (slot >= mTextureCount)) {
175             throw new IllegalArgumentException("Slot ID out of range.");
176         }
177         if (va != null && va.getType().hasFaces() &&
178             mTextures[slot] != TextureType.TEXTURE_CUBE) {
179             throw new IllegalArgumentException("Cannot bind cubemap to 2d texture slot");
180         }
181 
182         long id = va != null ? va.getID(mRS) : 0;
183         mRS.nProgramBindTexture(getID(mRS), slot, id);
184     }
185 
186     /**
187      * Binds an object that describes how a texture at the
188      * corresponding location is sampled
189      *
190      * @param vs sampler for a corresponding texture
191      * @param slot index within the program's list of textures to
192      *             use the sampler on
193      *
194      */
bindSampler(Sampler vs, int slot)195     public void bindSampler(Sampler vs, int slot)
196         throws IllegalArgumentException {
197         mRS.validate();
198         if ((slot < 0) || (slot >= mTextureCount)) {
199             throw new IllegalArgumentException("Slot ID out of range.");
200         }
201 
202         long id = vs != null ? vs.getID(mRS) : 0;
203         mRS.nProgramBindSampler(getID(mRS), slot, id);
204     }
205 
206 
207     public static class BaseProgramBuilder {
208         @UnsupportedAppUsage
209         RenderScript mRS;
210         @UnsupportedAppUsage
211         Element mInputs[];
212         @UnsupportedAppUsage
213         Element mOutputs[];
214         @UnsupportedAppUsage
215         Type mConstants[];
216         Type mTextures[];
217         TextureType mTextureTypes[];
218         String mTextureNames[];
219         @UnsupportedAppUsage
220         int mInputCount;
221         @UnsupportedAppUsage
222         int mOutputCount;
223         @UnsupportedAppUsage
224         int mConstantCount;
225         @UnsupportedAppUsage
226         int mTextureCount;
227         @UnsupportedAppUsage
228         String mShader;
229 
230 
231         @UnsupportedAppUsage
BaseProgramBuilder(RenderScript rs)232         protected BaseProgramBuilder(RenderScript rs) {
233             mRS = rs;
234             mInputs = new Element[MAX_INPUT];
235             mOutputs = new Element[MAX_OUTPUT];
236             mConstants = new Type[MAX_CONSTANT];
237             mInputCount = 0;
238             mOutputCount = 0;
239             mConstantCount = 0;
240             mTextureCount = 0;
241             mTextureTypes = new TextureType[MAX_TEXTURE];
242             mTextureNames = new String[MAX_TEXTURE];
243         }
244 
245         /**
246          * Sets the GLSL shader code to be used in the program
247          *
248          * @param s GLSL shader string
249          * @return  self
250          */
setShader(String s)251         public BaseProgramBuilder setShader(String s) {
252             mShader = s;
253             return this;
254         }
255 
256         /**
257          * Sets the GLSL shader code to be used in the program
258          *
259          * @param resources application resources
260          * @param resourceID id of the file containing GLSL shader code
261          *
262          * @return  self
263          */
setShader(Resources resources, int resourceID)264         public BaseProgramBuilder setShader(Resources resources, int resourceID) {
265             byte[] str;
266             int strLength;
267             InputStream is = resources.openRawResource(resourceID);
268             try {
269                 try {
270                     str = new byte[1024];
271                     strLength = 0;
272                     while(true) {
273                         int bytesLeft = str.length - strLength;
274                         if (bytesLeft == 0) {
275                             byte[] buf2 = new byte[str.length * 2];
276                             System.arraycopy(str, 0, buf2, 0, str.length);
277                             str = buf2;
278                             bytesLeft = str.length - strLength;
279                         }
280                         int bytesRead = is.read(str, strLength, bytesLeft);
281                         if (bytesRead <= 0) {
282                             break;
283                         }
284                         strLength += bytesRead;
285                     }
286                 } finally {
287                     is.close();
288                 }
289             } catch(IOException e) {
290                 throw new Resources.NotFoundException();
291             }
292 
293             try {
294                 mShader = new String(str, 0, strLength, "UTF-8");
295             } catch (UnsupportedEncodingException e) {
296                 Log.e("RenderScript shader creation", "Could not decode shader string");
297             }
298 
299             return this;
300         }
301 
302         /**
303          * Queries the index of the last added constant buffer type
304          *
305          */
getCurrentConstantIndex()306         public int getCurrentConstantIndex() {
307             return mConstantCount - 1;
308         }
309 
310         /**
311          * Queries the index of the last added texture type
312          *
313          */
getCurrentTextureIndex()314         public int getCurrentTextureIndex() {
315             return mTextureCount - 1;
316         }
317 
318         /**
319          * Adds constant (uniform) inputs to the program
320          *
321          * @param t Type that describes the layout of the Allocation
322          *          object to be used as constant inputs to the Program
323          * @return  self
324          */
addConstant(Type t)325         public BaseProgramBuilder addConstant(Type t) throws IllegalStateException {
326             // Should check for consistant and non-conflicting names...
327             if(mConstantCount >= MAX_CONSTANT) {
328                 throw new RSIllegalArgumentException("Max input count exceeded.");
329             }
330             if (t.getElement().isComplex()) {
331                 throw new RSIllegalArgumentException("Complex elements not allowed.");
332             }
333             mConstants[mConstantCount] = t;
334             mConstantCount++;
335             return this;
336         }
337 
338         /**
339          * Adds a texture input to the Program
340          *
341          * @param texType describes that the texture to append it (2D,
342          *                Cubemap, etc.)
343          * @return  self
344          */
addTexture(TextureType texType)345         public BaseProgramBuilder addTexture(TextureType texType) throws IllegalArgumentException {
346             addTexture(texType, "Tex" + mTextureCount);
347             return this;
348         }
349 
350         /**
351          * Adds a texture input to the Program
352          *
353          * @param texType describes that the texture to append it (2D,
354          *                Cubemap, etc.)
355          * @param texName what the texture should be called in the
356          *                shader
357          * @return  self
358          */
addTexture(TextureType texType, String texName)359         public BaseProgramBuilder addTexture(TextureType texType, String texName)
360             throws IllegalArgumentException {
361             if(mTextureCount >= MAX_TEXTURE) {
362                 throw new IllegalArgumentException("Max texture count exceeded.");
363             }
364             mTextureTypes[mTextureCount] = texType;
365             mTextureNames[mTextureCount] = texName;
366             mTextureCount ++;
367             return this;
368         }
369 
initProgram(Program p)370         protected void initProgram(Program p) {
371             p.mInputs = new Element[mInputCount];
372             System.arraycopy(mInputs, 0, p.mInputs, 0, mInputCount);
373             p.mOutputs = new Element[mOutputCount];
374             System.arraycopy(mOutputs, 0, p.mOutputs, 0, mOutputCount);
375             p.mConstants = new Type[mConstantCount];
376             System.arraycopy(mConstants, 0, p.mConstants, 0, mConstantCount);
377             p.mTextureCount = mTextureCount;
378             p.mTextures = new TextureType[mTextureCount];
379             System.arraycopy(mTextureTypes, 0, p.mTextures, 0, mTextureCount);
380             p.mTextureNames = new String[mTextureCount];
381             System.arraycopy(mTextureNames, 0, p.mTextureNames, 0, mTextureCount);
382         }
383     }
384 
385 }
386 
387 
388