blob: 5adb682c94db4b2a90b9c3c394740e25bd97c1d2 [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
Shih-wei Liao6b32fab2010-12-10 01:03:59 -080021import android.content.Context;
Jason Sams11c8af92010-10-13 15:31:10 -070022import android.graphics.PixelFormat;
Jason Sams704ff642010-02-09 16:05:07 -080023import android.graphics.Bitmap;
24import android.graphics.BitmapFactory;
25import android.util.Config;
26import android.util.Log;
27import android.view.Surface;
Jason Sams2222aa92010-10-10 17:58:25 -070028import android.view.SurfaceHolder;
29import android.view.SurfaceView;
Jason Sams704ff642010-02-09 16:05:07 -080030
31/**
32 * @hide
33 *
Jason Sams27676fe2010-11-10 17:00:59 -080034 * The Graphics derivitive of RenderScript. Extends the basic context to add a
35 * root script which is the display window for graphical output. When the
36 * system needs to update the display the currently bound root script will be
37 * called. This script is expected to issue the rendering commands to repaint
38 * the screen.
Jason Sams704ff642010-02-09 16:05:07 -080039 **/
40public class RenderScriptGL extends RenderScript {
41 private Surface mSurface;
42 int mWidth;
43 int mHeight;
44
Jason Sams27676fe2010-11-10 17:00:59 -080045 /**
46 * Class which is used to describe a pixel format for a graphical buffer.
47 * This is used to describe the intended format of the display surface.
48 *
Jason Samsbf6ef8d72010-12-06 15:59:59 -080049 * The configuration is described by pairs of minimum and preferred bit
50 * depths for each component within the config and additional structural
51 * information.
Jason Sams27676fe2010-11-10 17:00:59 -080052 */
Jason Sams2222aa92010-10-10 17:58:25 -070053 public static class SurfaceConfig {
54 int mDepthMin = 0;
55 int mDepthPref = 0;
56 int mStencilMin = 0;
57 int mStencilPref = 0;
58 int mColorMin = 8;
59 int mColorPref = 8;
60 int mAlphaMin = 0;
61 int mAlphaPref = 0;
62 int mSamplesMin = 1;
63 int mSamplesPref = 1;
64 float mSamplesQ = 1.f;
Jason Sams704ff642010-02-09 16:05:07 -080065
Jason Sams2222aa92010-10-10 17:58:25 -070066 public SurfaceConfig() {
67 }
68
69 public SurfaceConfig(SurfaceConfig sc) {
70 mDepthMin = sc.mDepthMin;
71 mDepthPref = sc.mDepthPref;
72 mStencilMin = sc.mStencilMin;
73 mStencilPref = sc.mStencilPref;
74 mColorMin = sc.mColorMin;
75 mColorPref = sc.mColorPref;
76 mAlphaMin = sc.mAlphaMin;
77 mAlphaPref = sc.mAlphaPref;
78 mSamplesMin = sc.mSamplesMin;
79 mSamplesPref = sc.mSamplesPref;
80 mSamplesQ = sc.mSamplesQ;
81 }
82
83 private void validateRange(int umin, int upref, int rmin, int rmax) {
84 if (umin < rmin || umin > rmax) {
Jason Samsc1d62102010-11-04 14:32:19 -070085 throw new RSIllegalArgumentException("Minimum value provided out of range.");
Jason Sams2222aa92010-10-10 17:58:25 -070086 }
87 if (upref < umin) {
Jason Samsbf6ef8d72010-12-06 15:59:59 -080088 throw new RSIllegalArgumentException("preferred must be >= Minimum.");
Jason Sams2222aa92010-10-10 17:58:25 -070089 }
90 }
91
Jason Samsbf6ef8d72010-12-06 15:59:59 -080092 /**
93 * Set the per-component bit depth for color (red, green, blue). This
94 * configures the surface for an unsigned integer buffer type.
95 *
96 * @param minimum
97 * @param preferred
98 */
99 public void setColor(int minimum, int preferred) {
100 validateRange(minimum, preferred, 5, 8);
Jason Sams2222aa92010-10-10 17:58:25 -0700101 mColorMin = minimum;
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800102 mColorPref = preferred;
Jason Sams2222aa92010-10-10 17:58:25 -0700103 }
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800104
105 /**
106 * Set the bit depth for alpha. This configures the surface for
107 * an unsigned integer buffer type.
108 *
109 * @param minimum
110 * @param preferred
111 */
112 public void setAlpha(int minimum, int preferred) {
113 validateRange(minimum, preferred, 0, 8);
Jason Sams2222aa92010-10-10 17:58:25 -0700114 mAlphaMin = minimum;
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800115 mAlphaPref = preferred;
Jason Sams2222aa92010-10-10 17:58:25 -0700116 }
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800117
118 /**
119 * Set the bit depth for the depth buffer. This configures the
120 * surface for an unsigned integer buffer type. If a minimum of 0
121 * is specified then its possible no depth buffer will be
122 * allocated.
123 *
124 * @param minimum
125 * @param preferred
126 */
127 public void setDepth(int minimum, int preferred) {
128 validateRange(minimum, preferred, 0, 24);
Jason Sams2222aa92010-10-10 17:58:25 -0700129 mDepthMin = minimum;
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800130 mDepthPref = preferred;
Jason Sams2222aa92010-10-10 17:58:25 -0700131 }
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800132
133 /**
134 * Configure the multisample rendering.
135 *
136 * @param minimum The required number of samples, must be at least 1.
137 * @param preferred The targe number of samples, must be at least
138 * minimum
139 * @param Q The quality of samples, range 0-1. Used to decide between
140 * different formats which have the same number of samples but
141 * different rendering quality.
142 */
143 public void setSamples(int minimum, int preferred, float Q) {
144 validateRange(minimum, preferred, 1, 32);
Jason Sams2222aa92010-10-10 17:58:25 -0700145 if (Q < 0.0f || Q > 1.0f) {
Jason Samsc1d62102010-11-04 14:32:19 -0700146 throw new RSIllegalArgumentException("Quality out of 0-1 range.");
Jason Sams2222aa92010-10-10 17:58:25 -0700147 }
148 mSamplesMin = minimum;
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800149 mSamplesPref = preferred;
Jason Sams2222aa92010-10-10 17:58:25 -0700150 mSamplesQ = Q;
151 }
152 };
153
154 SurfaceConfig mSurfaceConfig;
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800155/*
156 // Keep?
Jason Sams2222aa92010-10-10 17:58:25 -0700157 public void configureSurface(SurfaceHolder sh) {
Jason Sams11c8af92010-10-13 15:31:10 -0700158 if (mSurfaceConfig.mAlphaMin > 1) {
159 sh.setFormat(PixelFormat.RGBA_8888);
160 } else {
161 sh.setFormat(PixelFormat.RGBX_8888);
162 }
Jason Sams2222aa92010-10-10 17:58:25 -0700163 }
164
165 public void checkSurface(SurfaceHolder sh) {
166 }
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800167*/
Jason Sams2222aa92010-10-10 17:58:25 -0700168
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800169 /**
170 * Construct a new RenderScriptGL context.
171 *
Shih-wei Liao6b32fab2010-12-10 01:03:59 -0800172 * @param ctx The context.
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800173 * @param sc The desired format of the primart rendering surface.
174 */
Shih-wei Liao6b32fab2010-12-10 01:03:59 -0800175 public RenderScriptGL(Context ctx, SurfaceConfig sc) {
176 super(ctx);
Jason Sams2222aa92010-10-10 17:58:25 -0700177 mSurfaceConfig = new SurfaceConfig(sc);
178
Jason Sams704ff642010-02-09 16:05:07 -0800179 mSurface = null;
180 mWidth = 0;
181 mHeight = 0;
182 mDev = nDeviceCreate();
Jason Sams11c8af92010-10-13 15:31:10 -0700183 mContext = nContextCreateGL(mDev, 0,
184 mSurfaceConfig.mColorMin, mSurfaceConfig.mColorPref,
185 mSurfaceConfig.mAlphaMin, mSurfaceConfig.mAlphaPref,
186 mSurfaceConfig.mDepthMin, mSurfaceConfig.mDepthPref,
187 mSurfaceConfig.mStencilMin, mSurfaceConfig.mStencilPref,
188 mSurfaceConfig.mSamplesMin, mSurfaceConfig.mSamplesPref,
189 mSurfaceConfig.mSamplesQ);
Jason Samsd5f06302010-11-03 14:27:11 -0700190 if (mContext == 0) {
191 throw new RSDriverException("Failed to create RS context.");
192 }
Jason Sams704ff642010-02-09 16:05:07 -0800193 mMessageThread = new MessageThread(this);
194 mMessageThread.start();
Jason Sams704ff642010-02-09 16:05:07 -0800195 }
196
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800197 /**
198 * Bind an os surface
199 *
200 *
201 * @param w
202 * @param h
203 * @param sur
204 */
205 public void setSurface(SurfaceHolder sur, int w, int h) {
206 validate();
207 if (sur != null) {
208 mSurface = sur.getSurface();
209 } else {
210 mSurface = null;
211 }
Jason Sams704ff642010-02-09 16:05:07 -0800212 mWidth = w;
213 mHeight = h;
Jason Sams704ff642010-02-09 16:05:07 -0800214 nContextSetSurface(w, h, mSurface);
215 }
216
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800217 /**
218 * return the height of the last set surface.
219 *
220 * @return int
221 */
Jason Sams5585e362010-10-29 10:19:21 -0700222 public int getHeight() {
223 return mHeight;
224 }
225
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800226 /**
227 * return the width of the last set surface.
228 *
229 * @return int
230 */
Jason Sams5585e362010-10-29 10:19:21 -0700231 public int getWidth() {
232 return mWidth;
233 }
Jason Sams704ff642010-02-09 16:05:07 -0800234
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800235 /**
236 * Temporarly halt calls to the root rendering script.
237 *
238 */
239 public void pause() {
Jason Sams704ff642010-02-09 16:05:07 -0800240 validate();
241 nContextPause();
242 }
243
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800244 /**
245 * Resume calls to the root rendering script.
246 *
247 */
248 public void resume() {
Jason Sams704ff642010-02-09 16:05:07 -0800249 validate();
250 nContextResume();
251 }
252
253
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800254 /**
255 * Set the script to handle calls to render the primary surface.
256 *
257 * @param s Graphics script to process rendering requests.
258 */
259 public void bindRootScript(Script s) {
Jason Sams704ff642010-02-09 16:05:07 -0800260 validate();
261 nContextBindRootScript(safeID(s));
262 }
263
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800264 /**
265 * Set the default ProgramStore object seen as the parent state by the root
266 * rendering script.
267 *
268 * @param p
269 */
270 public void bindProgramStore(ProgramStore p) {
Jason Sams704ff642010-02-09 16:05:07 -0800271 validate();
Jason Sams54db59c2010-05-13 18:30:11 -0700272 nContextBindProgramStore(safeID(p));
Jason Sams704ff642010-02-09 16:05:07 -0800273 }
274
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800275 /**
276 * Set the default ProgramFragment object seen as the parent state by the
277 * root rendering script.
278 *
279 * @param p
280 */
281 public void bindProgramFragment(ProgramFragment p) {
Jason Sams704ff642010-02-09 16:05:07 -0800282 validate();
283 nContextBindProgramFragment(safeID(p));
284 }
285
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800286 /**
287 * Set the default ProgramRaster object seen as the parent state by the
288 * root rendering script.
289 *
290 * @param p
291 */
292 public void bindProgramRaster(ProgramRaster p) {
Jason Sams704ff642010-02-09 16:05:07 -0800293 validate();
294 nContextBindProgramRaster(safeID(p));
295 }
296
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800297 /**
298 * Set the default ProgramVertex object seen as the parent state by the
299 * root rendering script.
300 *
301 * @param p
302 */
303 public void bindProgramVertex(ProgramVertex p) {
Jason Sams704ff642010-02-09 16:05:07 -0800304 validate();
305 nContextBindProgramVertex(safeID(p));
306 }
307
Jason Sams704ff642010-02-09 16:05:07 -0800308}