blob: 0886db4fed02ec02b310a3a0e1b03a586f5ec218 [file] [log] [blame]
Jason Sams704ff642010-02-09 16:05:07 -08001/*
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
Jason Sams11c8af92010-10-13 15:31:10 -070021import android.graphics.PixelFormat;
Jason Sams704ff642010-02-09 16:05:07 -080022import android.graphics.Bitmap;
23import android.graphics.BitmapFactory;
24import android.util.Config;
25import android.util.Log;
26import android.view.Surface;
Jason Sams2222aa92010-10-10 17:58:25 -070027import android.view.SurfaceHolder;
28import android.view.SurfaceView;
Jason Sams704ff642010-02-09 16:05:07 -080029
30/**
31 * @hide
32 *
Jason Sams27676fe2010-11-10 17:00:59 -080033 * 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.
Jason Sams704ff642010-02-09 16:05:07 -080038 **/
39public class RenderScriptGL extends RenderScript {
40 private Surface mSurface;
41 int mWidth;
42 int mHeight;
43
Jason Sams27676fe2010-11-10 17:00:59 -080044 /**
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 *
Jason Samsbf6ef8d72010-12-06 15:59:59 -080048 * The configuration is described by pairs of minimum and preferred bit
49 * depths for each component within the config and additional structural
50 * information.
Jason Sams27676fe2010-11-10 17:00:59 -080051 */
Jason Sams2222aa92010-10-10 17:58:25 -070052 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;
Jason Sams704ff642010-02-09 16:05:07 -080064
Jason Sams2222aa92010-10-10 17:58:25 -070065 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) {
Jason Samsc1d62102010-11-04 14:32:19 -070084 throw new RSIllegalArgumentException("Minimum value provided out of range.");
Jason Sams2222aa92010-10-10 17:58:25 -070085 }
86 if (upref < umin) {
Jason Samsbf6ef8d72010-12-06 15:59:59 -080087 throw new RSIllegalArgumentException("preferred must be >= Minimum.");
Jason Sams2222aa92010-10-10 17:58:25 -070088 }
89 }
90
Jason Samsbf6ef8d72010-12-06 15:59:59 -080091 /**
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);
Jason Sams2222aa92010-10-10 17:58:25 -0700100 mColorMin = minimum;
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800101 mColorPref = preferred;
Jason Sams2222aa92010-10-10 17:58:25 -0700102 }
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800103
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);
Jason Sams2222aa92010-10-10 17:58:25 -0700113 mAlphaMin = minimum;
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800114 mAlphaPref = preferred;
Jason Sams2222aa92010-10-10 17:58:25 -0700115 }
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800116
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);
Jason Sams2222aa92010-10-10 17:58:25 -0700128 mDepthMin = minimum;
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800129 mDepthPref = preferred;
Jason Sams2222aa92010-10-10 17:58:25 -0700130 }
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800131
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);
Jason Sams2222aa92010-10-10 17:58:25 -0700144 if (Q < 0.0f || Q > 1.0f) {
Jason Samsc1d62102010-11-04 14:32:19 -0700145 throw new RSIllegalArgumentException("Quality out of 0-1 range.");
Jason Sams2222aa92010-10-10 17:58:25 -0700146 }
147 mSamplesMin = minimum;
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800148 mSamplesPref = preferred;
Jason Sams2222aa92010-10-10 17:58:25 -0700149 mSamplesQ = Q;
150 }
151 };
152
153 SurfaceConfig mSurfaceConfig;
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800154/*
155 // Keep?
Jason Sams2222aa92010-10-10 17:58:25 -0700156 public void configureSurface(SurfaceHolder sh) {
Jason Sams11c8af92010-10-13 15:31:10 -0700157 if (mSurfaceConfig.mAlphaMin > 1) {
158 sh.setFormat(PixelFormat.RGBA_8888);
159 } else {
160 sh.setFormat(PixelFormat.RGBX_8888);
161 }
Jason Sams2222aa92010-10-10 17:58:25 -0700162 }
163
164 public void checkSurface(SurfaceHolder sh) {
165 }
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800166*/
Jason Sams2222aa92010-10-10 17:58:25 -0700167
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800168 /**
169 * Construct a new RenderScriptGL context.
170 *
171 *
172 * @param sc The desired format of the primart rendering surface.
173 */
Jason Sams2222aa92010-10-10 17:58:25 -0700174 public RenderScriptGL(SurfaceConfig sc) {
175 mSurfaceConfig = new SurfaceConfig(sc);
176
Jason Sams704ff642010-02-09 16:05:07 -0800177 mSurface = null;
178 mWidth = 0;
179 mHeight = 0;
180 mDev = nDeviceCreate();
Jason Sams11c8af92010-10-13 15:31:10 -0700181 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);
Jason Samsd5f06302010-11-03 14:27:11 -0700188 if (mContext == 0) {
189 throw new RSDriverException("Failed to create RS context.");
190 }
Jason Sams704ff642010-02-09 16:05:07 -0800191 mMessageThread = new MessageThread(this);
192 mMessageThread.start();
Jason Sams704ff642010-02-09 16:05:07 -0800193 }
194
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800195 /**
196 * Bind an os surface
197 *
198 *
199 * @param w
200 * @param h
201 * @param sur
202 */
203 public void setSurface(SurfaceHolder sur, int w, int h) {
204 validate();
205 if (sur != null) {
206 mSurface = sur.getSurface();
207 } else {
208 mSurface = null;
209 }
Jason Sams704ff642010-02-09 16:05:07 -0800210 mWidth = w;
211 mHeight = h;
Jason Sams704ff642010-02-09 16:05:07 -0800212 nContextSetSurface(w, h, mSurface);
213 }
214
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800215 /**
216 * return the height of the last set surface.
217 *
218 * @return int
219 */
Jason Sams5585e362010-10-29 10:19:21 -0700220 public int getHeight() {
221 return mHeight;
222 }
223
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800224 /**
225 * return the width of the last set surface.
226 *
227 * @return int
228 */
Jason Sams5585e362010-10-29 10:19:21 -0700229 public int getWidth() {
230 return mWidth;
231 }
Jason Sams704ff642010-02-09 16:05:07 -0800232
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800233 /**
234 * Temporarly halt calls to the root rendering script.
235 *
236 */
237 public void pause() {
Jason Sams704ff642010-02-09 16:05:07 -0800238 validate();
239 nContextPause();
240 }
241
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800242 /**
243 * Resume calls to the root rendering script.
244 *
245 */
246 public void resume() {
Jason Sams704ff642010-02-09 16:05:07 -0800247 validate();
248 nContextResume();
249 }
250
251
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800252 /**
253 * Set the script to handle calls to render the primary surface.
254 *
255 * @param s Graphics script to process rendering requests.
256 */
257 public void bindRootScript(Script s) {
Jason Sams704ff642010-02-09 16:05:07 -0800258 validate();
259 nContextBindRootScript(safeID(s));
260 }
261
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800262 /**
263 * Set the default ProgramStore object seen as the parent state by the root
264 * rendering script.
265 *
266 * @param p
267 */
268 public void bindProgramStore(ProgramStore p) {
Jason Sams704ff642010-02-09 16:05:07 -0800269 validate();
Jason Sams54db59c2010-05-13 18:30:11 -0700270 nContextBindProgramStore(safeID(p));
Jason Sams704ff642010-02-09 16:05:07 -0800271 }
272
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800273 /**
274 * Set the default ProgramFragment object seen as the parent state by the
275 * root rendering script.
276 *
277 * @param p
278 */
279 public void bindProgramFragment(ProgramFragment p) {
Jason Sams704ff642010-02-09 16:05:07 -0800280 validate();
281 nContextBindProgramFragment(safeID(p));
282 }
283
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800284 /**
285 * Set the default ProgramRaster object seen as the parent state by the
286 * root rendering script.
287 *
288 * @param p
289 */
290 public void bindProgramRaster(ProgramRaster p) {
Jason Sams704ff642010-02-09 16:05:07 -0800291 validate();
292 nContextBindProgramRaster(safeID(p));
293 }
294
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800295 /**
296 * Set the default ProgramVertex object seen as the parent state by the
297 * root rendering script.
298 *
299 * @param p
300 */
301 public void bindProgramVertex(ProgramVertex p) {
Jason Sams704ff642010-02-09 16:05:07 -0800302 validate();
303 nContextBindProgramVertex(safeID(p));
304 }
305
Jason Sams704ff642010-02-09 16:05:07 -0800306}
307
308