blob: 2dfcc83fc70b0e784c4296976b39f853a1bcd46f [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;
Jason Samsfaa32b32011-06-20 16:58:04 -070025import android.graphics.SurfaceTexture;
Jason Sams704ff642010-02-09 16:05:07 -080026import 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/**
Jason Sams27676fe2010-11-10 17:00:59 -080032 * The Graphics derivitive of RenderScript. Extends the basic context to add a
33 * root script which is the display window for graphical output. When the
34 * system needs to update the display the currently bound root script will be
35 * called. This script is expected to issue the rendering commands to repaint
36 * the screen.
Jason Sams704ff642010-02-09 16:05:07 -080037 **/
38public class RenderScriptGL extends RenderScript {
Jason Sams704ff642010-02-09 16:05:07 -080039 int mWidth;
40 int mHeight;
41
Jason Sams27676fe2010-11-10 17:00:59 -080042 /**
43 * Class which is used to describe a pixel format for a graphical buffer.
44 * This is used to describe the intended format of the display surface.
45 *
Jason Samsbf6ef8d2010-12-06 15:59:59 -080046 * The configuration is described by pairs of minimum and preferred bit
47 * depths for each component within the config and additional structural
48 * information.
Jason Sams27676fe2010-11-10 17:00:59 -080049 */
Jason Sams2222aa92010-10-10 17:58:25 -070050 public static class SurfaceConfig {
51 int mDepthMin = 0;
52 int mDepthPref = 0;
53 int mStencilMin = 0;
54 int mStencilPref = 0;
55 int mColorMin = 8;
56 int mColorPref = 8;
57 int mAlphaMin = 0;
58 int mAlphaPref = 0;
59 int mSamplesMin = 1;
60 int mSamplesPref = 1;
61 float mSamplesQ = 1.f;
Jason Sams704ff642010-02-09 16:05:07 -080062
Jason Sams2222aa92010-10-10 17:58:25 -070063 public SurfaceConfig() {
64 }
65
66 public SurfaceConfig(SurfaceConfig sc) {
67 mDepthMin = sc.mDepthMin;
68 mDepthPref = sc.mDepthPref;
69 mStencilMin = sc.mStencilMin;
70 mStencilPref = sc.mStencilPref;
71 mColorMin = sc.mColorMin;
72 mColorPref = sc.mColorPref;
73 mAlphaMin = sc.mAlphaMin;
74 mAlphaPref = sc.mAlphaPref;
75 mSamplesMin = sc.mSamplesMin;
76 mSamplesPref = sc.mSamplesPref;
77 mSamplesQ = sc.mSamplesQ;
78 }
79
80 private void validateRange(int umin, int upref, int rmin, int rmax) {
81 if (umin < rmin || umin > rmax) {
Jason Samsc1d62102010-11-04 14:32:19 -070082 throw new RSIllegalArgumentException("Minimum value provided out of range.");
Jason Sams2222aa92010-10-10 17:58:25 -070083 }
84 if (upref < umin) {
Jason Samsbf6ef8d2010-12-06 15:59:59 -080085 throw new RSIllegalArgumentException("preferred must be >= Minimum.");
Jason Sams2222aa92010-10-10 17:58:25 -070086 }
87 }
88
Jason Samsbf6ef8d2010-12-06 15:59:59 -080089 /**
90 * Set the per-component bit depth for color (red, green, blue). This
91 * configures the surface for an unsigned integer buffer type.
92 *
93 * @param minimum
94 * @param preferred
95 */
96 public void setColor(int minimum, int preferred) {
97 validateRange(minimum, preferred, 5, 8);
Jason Sams2222aa92010-10-10 17:58:25 -070098 mColorMin = minimum;
Jason Samsbf6ef8d2010-12-06 15:59:59 -080099 mColorPref = preferred;
Jason Sams2222aa92010-10-10 17:58:25 -0700100 }
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800101
102 /**
103 * Set the bit depth for alpha. This configures the surface for
104 * an unsigned integer buffer type.
105 *
106 * @param minimum
107 * @param preferred
108 */
109 public void setAlpha(int minimum, int preferred) {
110 validateRange(minimum, preferred, 0, 8);
Jason Sams2222aa92010-10-10 17:58:25 -0700111 mAlphaMin = minimum;
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800112 mAlphaPref = preferred;
Jason Sams2222aa92010-10-10 17:58:25 -0700113 }
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800114
115 /**
116 * Set the bit depth for the depth buffer. This configures the
117 * surface for an unsigned integer buffer type. If a minimum of 0
118 * is specified then its possible no depth buffer will be
119 * allocated.
120 *
121 * @param minimum
122 * @param preferred
123 */
124 public void setDepth(int minimum, int preferred) {
125 validateRange(minimum, preferred, 0, 24);
Jason Sams2222aa92010-10-10 17:58:25 -0700126 mDepthMin = minimum;
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800127 mDepthPref = preferred;
Jason Sams2222aa92010-10-10 17:58:25 -0700128 }
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800129
130 /**
131 * Configure the multisample rendering.
132 *
133 * @param minimum The required number of samples, must be at least 1.
134 * @param preferred The targe number of samples, must be at least
135 * minimum
136 * @param Q The quality of samples, range 0-1. Used to decide between
137 * different formats which have the same number of samples but
138 * different rendering quality.
139 */
140 public void setSamples(int minimum, int preferred, float Q) {
141 validateRange(minimum, preferred, 1, 32);
Jason Sams2222aa92010-10-10 17:58:25 -0700142 if (Q < 0.0f || Q > 1.0f) {
Jason Samsc1d62102010-11-04 14:32:19 -0700143 throw new RSIllegalArgumentException("Quality out of 0-1 range.");
Jason Sams2222aa92010-10-10 17:58:25 -0700144 }
145 mSamplesMin = minimum;
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800146 mSamplesPref = preferred;
Jason Sams2222aa92010-10-10 17:58:25 -0700147 mSamplesQ = Q;
148 }
149 };
150
151 SurfaceConfig mSurfaceConfig;
Jason Sams2222aa92010-10-10 17:58:25 -0700152
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800153 /**
154 * Construct a new RenderScriptGL context.
155 *
Shih-wei Liao6b32fab2010-12-10 01:03:59 -0800156 * @param ctx The context.
Stephen Hines8cecbb52011-02-28 18:20:34 -0800157 * @param sc The desired format of the primary rendering surface.
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800158 */
Shih-wei Liao6b32fab2010-12-10 01:03:59 -0800159 public RenderScriptGL(Context ctx, SurfaceConfig sc) {
160 super(ctx);
Jason Sams2222aa92010-10-10 17:58:25 -0700161 mSurfaceConfig = new SurfaceConfig(sc);
162
Stephen Hines4382467a2011-08-01 15:02:34 -0700163 int sdkVersion = getTargetSdkVersion(ctx);
164
Jason Sams704ff642010-02-09 16:05:07 -0800165 mWidth = 0;
166 mHeight = 0;
167 mDev = nDeviceCreate();
Alex Sakhartchouk2c74ad92011-03-16 19:28:25 -0700168 int dpi = ctx.getResources().getDisplayMetrics().densityDpi;
Stephen Hines4382467a2011-08-01 15:02:34 -0700169 mContext = nContextCreateGL(mDev, 0, sdkVersion,
Jason Sams11c8af92010-10-13 15:31:10 -0700170 mSurfaceConfig.mColorMin, mSurfaceConfig.mColorPref,
171 mSurfaceConfig.mAlphaMin, mSurfaceConfig.mAlphaPref,
172 mSurfaceConfig.mDepthMin, mSurfaceConfig.mDepthPref,
173 mSurfaceConfig.mStencilMin, mSurfaceConfig.mStencilPref,
174 mSurfaceConfig.mSamplesMin, mSurfaceConfig.mSamplesPref,
Alex Sakhartchouk2c74ad92011-03-16 19:28:25 -0700175 mSurfaceConfig.mSamplesQ, dpi);
Jason Samsd5f06302010-11-03 14:27:11 -0700176 if (mContext == 0) {
177 throw new RSDriverException("Failed to create RS context.");
178 }
Jason Sams704ff642010-02-09 16:05:07 -0800179 mMessageThread = new MessageThread(this);
180 mMessageThread.start();
Jason Sams704ff642010-02-09 16:05:07 -0800181 }
182
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800183 /**
184 * Bind an os surface
185 *
186 *
187 * @param w
188 * @param h
189 * @param sur
190 */
191 public void setSurface(SurfaceHolder sur, int w, int h) {
192 validate();
Jason Samsfaa32b32011-06-20 16:58:04 -0700193 Surface s = null;
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800194 if (sur != null) {
Jason Samsfaa32b32011-06-20 16:58:04 -0700195 s = sur.getSurface();
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800196 }
Jason Sams704ff642010-02-09 16:05:07 -0800197 mWidth = w;
198 mHeight = h;
Jason Samsfaa32b32011-06-20 16:58:04 -0700199 nContextSetSurface(w, h, s);
200 }
201
202 /**
203 * Bind an os surface
204 *
Jason Samsfaa32b32011-06-20 16:58:04 -0700205 * @param w
206 * @param h
207 * @param sur
208 */
209 public void setSurfaceTexture(SurfaceTexture sur, int w, int h) {
210 validate();
211 //android.util.Log.v("rs", "set surface " + sur + " w=" + w + ", h=" + h);
212
213 mWidth = w;
214 mHeight = h;
215 nContextSetSurfaceTexture(w, h, sur);
Jason Sams704ff642010-02-09 16:05:07 -0800216 }
217
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800218 /**
219 * return the height of the last set surface.
220 *
221 * @return int
222 */
Jason Sams5585e362010-10-29 10:19:21 -0700223 public int getHeight() {
224 return mHeight;
225 }
226
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800227 /**
228 * return the width of the last set surface.
229 *
230 * @return int
231 */
Jason Sams5585e362010-10-29 10:19:21 -0700232 public int getWidth() {
233 return mWidth;
234 }
Jason Sams704ff642010-02-09 16:05:07 -0800235
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800236 /**
237 * Temporarly halt calls to the root rendering script.
238 *
239 */
240 public void pause() {
Jason Sams704ff642010-02-09 16:05:07 -0800241 validate();
242 nContextPause();
243 }
244
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800245 /**
246 * Resume calls to the root rendering script.
247 *
248 */
249 public void resume() {
Jason Sams704ff642010-02-09 16:05:07 -0800250 validate();
251 nContextResume();
252 }
253
254
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800255 /**
256 * Set the script to handle calls to render the primary surface.
257 *
258 * @param s Graphics script to process rendering requests.
259 */
260 public void bindRootScript(Script s) {
Jason Sams704ff642010-02-09 16:05:07 -0800261 validate();
262 nContextBindRootScript(safeID(s));
263 }
264
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800265 /**
266 * Set the default ProgramStore object seen as the parent state by the root
267 * rendering script.
268 *
269 * @param p
270 */
271 public void bindProgramStore(ProgramStore p) {
Jason Sams704ff642010-02-09 16:05:07 -0800272 validate();
Jason Sams54db59c2010-05-13 18:30:11 -0700273 nContextBindProgramStore(safeID(p));
Jason Sams704ff642010-02-09 16:05:07 -0800274 }
275
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800276 /**
277 * Set the default ProgramFragment object seen as the parent state by the
278 * root rendering script.
279 *
280 * @param p
281 */
282 public void bindProgramFragment(ProgramFragment p) {
Jason Sams704ff642010-02-09 16:05:07 -0800283 validate();
284 nContextBindProgramFragment(safeID(p));
285 }
286
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800287 /**
288 * Set the default ProgramRaster object seen as the parent state by the
289 * root rendering script.
290 *
291 * @param p
292 */
293 public void bindProgramRaster(ProgramRaster p) {
Jason Sams704ff642010-02-09 16:05:07 -0800294 validate();
295 nContextBindProgramRaster(safeID(p));
296 }
297
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800298 /**
299 * Set the default ProgramVertex object seen as the parent state by the
300 * root rendering script.
301 *
302 * @param p
303 */
304 public void bindProgramVertex(ProgramVertex p) {
Jason Sams704ff642010-02-09 16:05:07 -0800305 validate();
306 nContextBindProgramVertex(safeID(p));
307 }
308
Jason Sams704ff642010-02-09 16:05:07 -0800309}