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