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.compat.annotation.UnsupportedAppUsage;
20 import android.content.Context;
21 import android.graphics.SurfaceTexture;
22 import android.view.Surface;
23 import android.view.SurfaceHolder;
24 
25 /**
26  * @hide
27  * @deprecated in API 16
28  * The Graphics derivitive of RenderScript.  Extends the basic context to add a
29  * root script which is the display window for graphical output.  When the
30  * system needs to update the display the currently bound root script will be
31  * called.  This script is expected to issue the rendering commands to repaint
32  * the screen.
33  *
34  * <div class="special reference">
35  * <h3>Developer Guides</h3>
36  * <p>For more information about creating an application that uses RenderScript, read the
37  * <a href="{@docRoot}guide/topics/renderscript/index.html">RenderScript</a> developer guide.</p>
38  * </div>
39  **/
40 public class RenderScriptGL extends RenderScript {
41     int mWidth;
42     int mHeight;
43 
44     /**
45      * @deprecated in API 16
46      * Class which is used to describe a pixel format for a graphical buffer.
47      * This is used to describe the intended format of the display surface.
48      *
49      * The configuration is described by pairs of minimum and preferred bit
50      * depths for each component within the config and additional structural
51      * information.
52      */
53     public static class SurfaceConfig {
54         int mDepthMin       = 0;
55         int mDepthPref      = 0;
56         int mStencilMin     = 0;
57         int mStencilPref    = 0;
58         int mColorMin       = 8;
59         int mColorPref      = 8;
60         int mAlphaMin       = 0;
61         int mAlphaPref      = 0;
62         int mSamplesMin     = 1;
63         int mSamplesPref    = 1;
64         float mSamplesQ     = 1.f;
65 
66         /**
67          * @deprecated in API 16
68          */
69         @UnsupportedAppUsage
SurfaceConfig()70         public SurfaceConfig() {
71         }
72 
73         /**
74          * @deprecated in API 16
75          */
SurfaceConfig(SurfaceConfig sc)76         public SurfaceConfig(SurfaceConfig sc) {
77             mDepthMin = sc.mDepthMin;
78             mDepthPref = sc.mDepthPref;
79             mStencilMin = sc.mStencilMin;
80             mStencilPref = sc.mStencilPref;
81             mColorMin = sc.mColorMin;
82             mColorPref = sc.mColorPref;
83             mAlphaMin = sc.mAlphaMin;
84             mAlphaPref = sc.mAlphaPref;
85             mSamplesMin = sc.mSamplesMin;
86             mSamplesPref = sc.mSamplesPref;
87             mSamplesQ = sc.mSamplesQ;
88         }
89 
validateRange(int umin, int upref, int rmin, int rmax)90         private void validateRange(int umin, int upref, int rmin, int rmax) {
91             if (umin < rmin || umin > rmax) {
92                 throw new RSIllegalArgumentException("Minimum value provided out of range.");
93             }
94             if (upref < umin) {
95                 throw new RSIllegalArgumentException("preferred must be >= Minimum.");
96             }
97         }
98 
99         /**
100          * @deprecated in API 16
101          * Set the per-component bit depth for color (red, green, blue).  This
102          * configures the surface for an unsigned integer buffer type.
103          *
104          * @param minimum
105          * @param preferred
106          */
setColor(int minimum, int preferred)107         public void setColor(int minimum, int preferred) {
108             validateRange(minimum, preferred, 5, 8);
109             mColorMin = minimum;
110             mColorPref = preferred;
111         }
112 
113         /**
114          * @deprecated in API 16
115          * Set the bit depth for alpha. This configures the surface for
116          * an unsigned integer buffer type.
117          *
118          * @param minimum
119          * @param preferred
120          */
setAlpha(int minimum, int preferred)121         public void setAlpha(int minimum, int preferred) {
122             validateRange(minimum, preferred, 0, 8);
123             mAlphaMin = minimum;
124             mAlphaPref = preferred;
125         }
126 
127          /**
128          * @deprecated in API 16
129          * Set the bit depth for the depth buffer. This configures the
130          * surface for an unsigned integer buffer type.  If a minimum of 0
131          * is specified then its possible no depth buffer will be
132          * allocated.
133          *
134          * @param minimum
135          * @param preferred
136          */
137         @UnsupportedAppUsage
setDepth(int minimum, int preferred)138         public void setDepth(int minimum, int preferred) {
139             validateRange(minimum, preferred, 0, 24);
140             mDepthMin = minimum;
141             mDepthPref = preferred;
142         }
143 
144         /**
145          * @deprecated in API 16
146          * Configure the multisample rendering.
147          *
148          * @param minimum The required number of samples, must be at least 1.
149          * @param preferred The targe number of samples, must be at least
150          *                  minimum
151          * @param Q  The quality of samples, range 0-1.  Used to decide between
152          *           different formats which have the same number of samples but
153          *           different rendering quality.
154          */
setSamples(int minimum, int preferred, float Q)155         public void setSamples(int minimum, int preferred, float Q) {
156             validateRange(minimum, preferred, 1, 32);
157             if (Q < 0.0f || Q > 1.0f) {
158                 throw new RSIllegalArgumentException("Quality out of 0-1 range.");
159             }
160             mSamplesMin = minimum;
161             mSamplesPref = preferred;
162             mSamplesQ = Q;
163         }
164     };
165 
166     SurfaceConfig mSurfaceConfig;
167 
168     /**
169      * @deprecated in API 16
170      * Construct a new RenderScriptGL context.
171      *
172      * @param ctx The context.
173      * @param sc The desired format of the primary rendering surface.
174      */
175     @UnsupportedAppUsage
RenderScriptGL(Context ctx, SurfaceConfig sc)176     public RenderScriptGL(Context ctx, SurfaceConfig sc) {
177         super(ctx);
178         mSurfaceConfig = new SurfaceConfig(sc);
179 
180         int sdkVersion = ctx.getApplicationInfo().targetSdkVersion;
181 
182         mWidth = 0;
183         mHeight = 0;
184         long device = nDeviceCreate();
185         int dpi = ctx.getResources().getDisplayMetrics().densityDpi;
186         mContext = nContextCreateGL(device, 0, sdkVersion,
187                                     mSurfaceConfig.mColorMin, mSurfaceConfig.mColorPref,
188                                     mSurfaceConfig.mAlphaMin, mSurfaceConfig.mAlphaPref,
189                                     mSurfaceConfig.mDepthMin, mSurfaceConfig.mDepthPref,
190                                     mSurfaceConfig.mStencilMin, mSurfaceConfig.mStencilPref,
191                                     mSurfaceConfig.mSamplesMin, mSurfaceConfig.mSamplesPref,
192                                     mSurfaceConfig.mSamplesQ, dpi);
193         if (mContext == 0) {
194             throw new RSDriverException("Failed to create RS context.");
195         }
196         mMessageThread = new MessageThread(this);
197         mMessageThread.start();
198     }
199 
200     /**
201      * @deprecated in API 16
202      * Bind an os surface
203      *
204      *
205      * @param w
206      * @param h
207      * @param sur
208      */
209     @UnsupportedAppUsage
setSurface(SurfaceHolder sur, int w, int h)210     public void setSurface(SurfaceHolder sur, int w, int h) {
211         validate();
212         Surface s = null;
213         if (sur != null) {
214             s = sur.getSurface();
215         }
216         mWidth = w;
217         mHeight = h;
218         nContextSetSurface(w, h, s);
219     }
220 
221     /**
222      * @deprecated in API 16
223      * Bind an os surface
224      *
225      * @param w
226      * @param h
227      * @param sur
228      */
setSurfaceTexture(SurfaceTexture sur, int w, int h)229     public void setSurfaceTexture(SurfaceTexture sur, int w, int h) {
230         validate();
231         //android.util.Log.v("rs", "set surface " + sur + " w=" + w + ", h=" + h);
232 
233         Surface s = null;
234         if (sur != null) {
235             s = new Surface(sur);
236         }
237         mWidth = w;
238         mHeight = h;
239         nContextSetSurface(w, h, s);
240     }
241 
242     /**
243      * @deprecated in API 16
244      * return the height of the last set surface.
245      *
246      * @return int
247      */
getHeight()248     public int getHeight() {
249         return mHeight;
250     }
251 
252     /**
253      * @deprecated in API 16
254      * return the width of the last set surface.
255      *
256      * @return int
257      */
getWidth()258     public int getWidth() {
259         return mWidth;
260     }
261 
262     /**
263      * @deprecated in API 16
264      * Temporarly halt calls to the root rendering script.
265      *
266      */
pause()267     public void pause() {
268         validate();
269         nContextPause();
270     }
271 
272     /**
273      * @deprecated in API 16
274      * Resume calls to the root rendering script.
275      *
276      */
resume()277     public void resume() {
278         validate();
279         nContextResume();
280     }
281 
282 
283     /**
284      * @deprecated in API 16
285      * Set the script to handle calls to render the primary surface.
286      *
287      * @param s Graphics script to process rendering requests.
288      */
289     @UnsupportedAppUsage
bindRootScript(Script s)290     public void bindRootScript(Script s) {
291         validate();
292         nContextBindRootScript((int)safeID(s));
293     }
294 
295     /**
296      * @deprecated in API 16
297      * Set the default ProgramStore object seen as the parent state by the root
298      * rendering script.
299      *
300      * @param p
301      */
302     @UnsupportedAppUsage
bindProgramStore(ProgramStore p)303     public void bindProgramStore(ProgramStore p) {
304         validate();
305         nContextBindProgramStore((int)safeID(p));
306     }
307 
308     /**
309      * @deprecated in API 16
310      * Set the default ProgramFragment object seen as the parent state by the
311      * root rendering script.
312      *
313      * @param p
314      */
bindProgramFragment(ProgramFragment p)315     public void bindProgramFragment(ProgramFragment p) {
316         validate();
317         nContextBindProgramFragment((int)safeID(p));
318     }
319 
320     /**
321      * @deprecated in API 16
322      * Set the default ProgramRaster object seen as the parent state by the
323      * root rendering script.
324      *
325      * @param p
326      */
327     @UnsupportedAppUsage
bindProgramRaster(ProgramRaster p)328     public void bindProgramRaster(ProgramRaster p) {
329         validate();
330         nContextBindProgramRaster((int)safeID(p));
331     }
332 
333     /**
334      * @deprecated in API 16
335      * Set the default ProgramVertex object seen as the parent state by the
336      * root rendering script.
337      *
338      * @param p
339      */
340     @UnsupportedAppUsage
bindProgramVertex(ProgramVertex p)341     public void bindProgramVertex(ProgramVertex p) {
342         validate();
343         nContextBindProgramVertex((int)safeID(p));
344     }
345 
346 }
347