RenderScriptGL.java revision 11c8af9ded3a319635b4e91a639a616ec97fc7e3
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 **/
34public class RenderScriptGL extends RenderScript {
35    private Surface mSurface;
36    int mWidth;
37    int mHeight;
38
39    public static class SurfaceConfig {
40        int mDepthMin       = 0;
41        int mDepthPref      = 0;
42        int mStencilMin     = 0;
43        int mStencilPref    = 0;
44        int mColorMin       = 8;
45        int mColorPref      = 8;
46        int mAlphaMin       = 0;
47        int mAlphaPref      = 0;
48        int mSamplesMin     = 1;
49        int mSamplesPref    = 1;
50        float mSamplesQ     = 1.f;
51
52        public SurfaceConfig() {
53        }
54
55        public SurfaceConfig(SurfaceConfig sc) {
56            mDepthMin = sc.mDepthMin;
57            mDepthPref = sc.mDepthPref;
58            mStencilMin = sc.mStencilMin;
59            mStencilPref = sc.mStencilPref;
60            mColorMin = sc.mColorMin;
61            mColorPref = sc.mColorPref;
62            mAlphaMin = sc.mAlphaMin;
63            mAlphaPref = sc.mAlphaPref;
64            mSamplesMin = sc.mSamplesMin;
65            mSamplesPref = sc.mSamplesPref;
66            mSamplesQ = sc.mSamplesQ;
67        }
68
69        private void validateRange(int umin, int upref, int rmin, int rmax) {
70            if (umin < rmin || umin > rmax) {
71                throw new IllegalArgumentException("Minimum value provided out of range.");
72            }
73            if (upref < umin) {
74                throw new IllegalArgumentException("Prefered must be >= Minimum.");
75            }
76        }
77
78        public void setColor(int minimum, int prefered) {
79            validateRange(minimum, prefered, 5, 8);
80            mColorMin = minimum;
81            mColorPref = prefered;
82        }
83        public void setAlpha(int minimum, int prefered) {
84            validateRange(minimum, prefered, 0, 8);
85            mAlphaMin = minimum;
86            mAlphaPref = prefered;
87        }
88        public void setDepth(int minimum, int prefered) {
89            validateRange(minimum, prefered, 0, 24);
90            mDepthMin = minimum;
91            mDepthPref = prefered;
92        }
93        public void setSamples(int minimum, int prefered, float Q) {
94            validateRange(minimum, prefered, 0, 24);
95            if (Q < 0.0f || Q > 1.0f) {
96                throw new IllegalArgumentException("Quality out of 0-1 range.");
97            }
98            mSamplesMin = minimum;
99            mSamplesPref = prefered;
100            mSamplesQ = Q;
101        }
102    };
103
104    SurfaceConfig mSurfaceConfig;
105
106    public void configureSurface(SurfaceHolder sh) {
107        if (mSurfaceConfig.mAlphaMin > 1) {
108            sh.setFormat(PixelFormat.RGBA_8888);
109        } else {
110            sh.setFormat(PixelFormat.RGBX_8888);
111        }
112    }
113
114    public void checkSurface(SurfaceHolder sh) {
115    }
116
117    public RenderScriptGL(SurfaceConfig sc) {
118        mSurfaceConfig = new SurfaceConfig(sc);
119
120        mSurface = null;
121        mWidth = 0;
122        mHeight = 0;
123        mDev = nDeviceCreate();
124        mContext = nContextCreateGL(mDev, 0,
125                                    mSurfaceConfig.mColorMin, mSurfaceConfig.mColorPref,
126                                    mSurfaceConfig.mAlphaMin, mSurfaceConfig.mAlphaPref,
127                                    mSurfaceConfig.mDepthMin, mSurfaceConfig.mDepthPref,
128                                    mSurfaceConfig.mStencilMin, mSurfaceConfig.mStencilPref,
129                                    mSurfaceConfig.mSamplesMin, mSurfaceConfig.mSamplesPref,
130                                    mSurfaceConfig.mSamplesQ);
131        mMessageThread = new MessageThread(this);
132        mMessageThread.start();
133        Element.initPredefined(this);
134    }
135
136    public void contextSetSurface(int w, int h, Surface sur) {
137        mSurface = sur;
138        mWidth = w;
139        mHeight = h;
140        validate();
141        nContextSetSurface(w, h, mSurface);
142    }
143
144
145    void pause() {
146        validate();
147        nContextPause();
148    }
149
150    void resume() {
151        validate();
152        nContextResume();
153    }
154
155
156    public void contextBindRootScript(Script s) {
157        validate();
158        nContextBindRootScript(safeID(s));
159    }
160
161    public void contextBindProgramStore(ProgramStore p) {
162        validate();
163        nContextBindProgramStore(safeID(p));
164    }
165
166    public void contextBindProgramFragment(ProgramFragment p) {
167        validate();
168        nContextBindProgramFragment(safeID(p));
169    }
170
171    public void contextBindProgramRaster(ProgramRaster p) {
172        validate();
173        nContextBindProgramRaster(safeID(p));
174    }
175
176    public void contextBindProgramVertex(ProgramVertex p) {
177        validate();
178        nContextBindProgramVertex(safeID(p));
179    }
180
181
182
183
184    //////////////////////////////////////////////////////////////////////////////////
185    // File
186
187    public class File extends BaseObj {
188        File(int id) {
189            super(id, RenderScriptGL.this);
190        }
191    }
192
193    public File fileOpen(String s) throws IllegalStateException, IllegalArgumentException
194    {
195        if(s.length() < 1) {
196            throw new IllegalArgumentException("fileOpen does not accept a zero length string.");
197        }
198
199        try {
200            byte[] bytes = s.getBytes("UTF-8");
201            int id = nFileOpen(bytes);
202            return new File(id);
203        } catch (java.io.UnsupportedEncodingException e) {
204            throw new RuntimeException(e);
205        }
206    }
207
208}
209
210
211