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