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