blob: 12c81026a59aafa80f0b97186615467d164b8669 [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
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070031/**
Jason Samsd4ca9912012-05-08 19:02:07 -070032 * @deprecated in API 16
Joe Fernandez3aef8e1d2011-12-20 10:38:34 -080033 * The Graphics derivitive of Renderscript. Extends the basic context to add a
Jason Sams27676fe2010-11-10 17:00:59 -080034 * 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.
Joe Fernandez3aef8e1d2011-12-20 10:38:34 -080038 *
39 * <div class="special reference">
40 * <h3>Developer Guides</h3>
41 * <p>For more information about creating an application that uses Renderscript, read the
42 * <a href="{@docRoot}guide/topics/graphics/renderscript.html">Renderscript</a> developer guide.</p>
43 * </div>
Jason Sams704ff642010-02-09 16:05:07 -080044 **/
45public class RenderScriptGL extends RenderScript {
Jason Sams704ff642010-02-09 16:05:07 -080046 int mWidth;
47 int mHeight;
48
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070049 /**
Jason Samsd4ca9912012-05-08 19:02:07 -070050 * @deprecated in API 16
Jason Sams27676fe2010-11-10 17:00:59 -080051 * Class which is used to describe a pixel format for a graphical buffer.
52 * This is used to describe the intended format of the display surface.
53 *
Jason Samsbf6ef8d72010-12-06 15:59:59 -080054 * The configuration is described by pairs of minimum and preferred bit
55 * depths for each component within the config and additional structural
56 * information.
Jason Sams27676fe2010-11-10 17:00:59 -080057 */
Jason Sams2222aa92010-10-10 17:58:25 -070058 public static class SurfaceConfig {
59 int mDepthMin = 0;
60 int mDepthPref = 0;
61 int mStencilMin = 0;
62 int mStencilPref = 0;
63 int mColorMin = 8;
64 int mColorPref = 8;
65 int mAlphaMin = 0;
66 int mAlphaPref = 0;
67 int mSamplesMin = 1;
68 int mSamplesPref = 1;
69 float mSamplesQ = 1.f;
Jason Sams704ff642010-02-09 16:05:07 -080070
Jason Samsd4ca9912012-05-08 19:02:07 -070071 /**
72 * @deprecated in API 16
73 */
Jason Sams2222aa92010-10-10 17:58:25 -070074 public SurfaceConfig() {
75 }
76
Jason Samsd4ca9912012-05-08 19:02:07 -070077 /**
78 * @deprecated in API 16
79 */
Jason Sams2222aa92010-10-10 17:58:25 -070080 public SurfaceConfig(SurfaceConfig sc) {
81 mDepthMin = sc.mDepthMin;
82 mDepthPref = sc.mDepthPref;
83 mStencilMin = sc.mStencilMin;
84 mStencilPref = sc.mStencilPref;
85 mColorMin = sc.mColorMin;
86 mColorPref = sc.mColorPref;
87 mAlphaMin = sc.mAlphaMin;
88 mAlphaPref = sc.mAlphaPref;
89 mSamplesMin = sc.mSamplesMin;
90 mSamplesPref = sc.mSamplesPref;
91 mSamplesQ = sc.mSamplesQ;
92 }
93
94 private void validateRange(int umin, int upref, int rmin, int rmax) {
95 if (umin < rmin || umin > rmax) {
Jason Samsc1d62102010-11-04 14:32:19 -070096 throw new RSIllegalArgumentException("Minimum value provided out of range.");
Jason Sams2222aa92010-10-10 17:58:25 -070097 }
98 if (upref < umin) {
Jason Samsbf6ef8d72010-12-06 15:59:59 -080099 throw new RSIllegalArgumentException("preferred must be >= Minimum.");
Jason Sams2222aa92010-10-10 17:58:25 -0700100 }
101 }
102
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700103 /**
Jason Samsd4ca9912012-05-08 19:02:07 -0700104 * @deprecated in API 16
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800105 * Set the per-component bit depth for color (red, green, blue). This
106 * configures the surface for an unsigned integer buffer type.
107 *
108 * @param minimum
109 * @param preferred
110 */
111 public void setColor(int minimum, int preferred) {
112 validateRange(minimum, preferred, 5, 8);
Jason Sams2222aa92010-10-10 17:58:25 -0700113 mColorMin = minimum;
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800114 mColorPref = preferred;
Jason Sams2222aa92010-10-10 17:58:25 -0700115 }
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800116
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700117 /**
Jason Samsd4ca9912012-05-08 19:02:07 -0700118 * @deprecated in API 16
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800119 * Set the bit depth for alpha. This configures the surface for
120 * an unsigned integer buffer type.
121 *
122 * @param minimum
123 * @param preferred
124 */
125 public void setAlpha(int minimum, int preferred) {
126 validateRange(minimum, preferred, 0, 8);
Jason Sams2222aa92010-10-10 17:58:25 -0700127 mAlphaMin = minimum;
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800128 mAlphaPref = preferred;
Jason Sams2222aa92010-10-10 17:58:25 -0700129 }
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800130
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700131 /**
Jason Samsd4ca9912012-05-08 19:02:07 -0700132 * @deprecated in API 16
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800133 * Set the bit depth for the depth buffer. This configures the
134 * surface for an unsigned integer buffer type. If a minimum of 0
135 * is specified then its possible no depth buffer will be
136 * allocated.
137 *
138 * @param minimum
139 * @param preferred
140 */
141 public void setDepth(int minimum, int preferred) {
142 validateRange(minimum, preferred, 0, 24);
Jason Sams2222aa92010-10-10 17:58:25 -0700143 mDepthMin = minimum;
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800144 mDepthPref = preferred;
Jason Sams2222aa92010-10-10 17:58:25 -0700145 }
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800146
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700147 /**
Jason Samsd4ca9912012-05-08 19:02:07 -0700148 * @deprecated in API 16
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800149 * Configure the multisample rendering.
150 *
151 * @param minimum The required number of samples, must be at least 1.
152 * @param preferred The targe number of samples, must be at least
153 * minimum
154 * @param Q The quality of samples, range 0-1. Used to decide between
155 * different formats which have the same number of samples but
156 * different rendering quality.
157 */
158 public void setSamples(int minimum, int preferred, float Q) {
159 validateRange(minimum, preferred, 1, 32);
Jason Sams2222aa92010-10-10 17:58:25 -0700160 if (Q < 0.0f || Q > 1.0f) {
Jason Samsc1d62102010-11-04 14:32:19 -0700161 throw new RSIllegalArgumentException("Quality out of 0-1 range.");
Jason Sams2222aa92010-10-10 17:58:25 -0700162 }
163 mSamplesMin = minimum;
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800164 mSamplesPref = preferred;
Jason Sams2222aa92010-10-10 17:58:25 -0700165 mSamplesQ = Q;
166 }
167 };
168
169 SurfaceConfig mSurfaceConfig;
Jason Sams2222aa92010-10-10 17:58:25 -0700170
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700171 /**
Jason Samsd4ca9912012-05-08 19:02:07 -0700172 * @deprecated in API 16
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800173 * Construct a new RenderScriptGL context.
174 *
Shih-wei Liao6b32fab2010-12-10 01:03:59 -0800175 * @param ctx The context.
Stephen Hines8cecbb52011-02-28 18:20:34 -0800176 * @param sc The desired format of the primary rendering surface.
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800177 */
Shih-wei Liao6b32fab2010-12-10 01:03:59 -0800178 public RenderScriptGL(Context ctx, SurfaceConfig sc) {
179 super(ctx);
Jason Sams2222aa92010-10-10 17:58:25 -0700180 mSurfaceConfig = new SurfaceConfig(sc);
181
Jason Sams1a4e1f3e2012-02-24 17:51:24 -0800182 int sdkVersion = ctx.getApplicationInfo().targetSdkVersion;
Stephen Hines4382467a2011-08-01 15:02:34 -0700183
Jason Sams704ff642010-02-09 16:05:07 -0800184 mWidth = 0;
185 mHeight = 0;
186 mDev = nDeviceCreate();
Alex Sakhartchouk2c74ad92011-03-16 19:28:25 -0700187 int dpi = ctx.getResources().getDisplayMetrics().densityDpi;
Stephen Hines4382467a2011-08-01 15:02:34 -0700188 mContext = nContextCreateGL(mDev, 0, sdkVersion,
Jason Sams11c8af92010-10-13 15:31:10 -0700189 mSurfaceConfig.mColorMin, mSurfaceConfig.mColorPref,
190 mSurfaceConfig.mAlphaMin, mSurfaceConfig.mAlphaPref,
191 mSurfaceConfig.mDepthMin, mSurfaceConfig.mDepthPref,
192 mSurfaceConfig.mStencilMin, mSurfaceConfig.mStencilPref,
193 mSurfaceConfig.mSamplesMin, mSurfaceConfig.mSamplesPref,
Alex Sakhartchouk2c74ad92011-03-16 19:28:25 -0700194 mSurfaceConfig.mSamplesQ, dpi);
Jason Samsd5f06302010-11-03 14:27:11 -0700195 if (mContext == 0) {
196 throw new RSDriverException("Failed to create RS context.");
197 }
Jason Sams704ff642010-02-09 16:05:07 -0800198 mMessageThread = new MessageThread(this);
199 mMessageThread.start();
Jason Sams704ff642010-02-09 16:05:07 -0800200 }
201
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700202 /**
Jason Samsd4ca9912012-05-08 19:02:07 -0700203 * @deprecated in API 16
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800204 * Bind an os surface
205 *
206 *
207 * @param w
208 * @param h
209 * @param sur
210 */
211 public void setSurface(SurfaceHolder sur, int w, int h) {
212 validate();
Jason Samsfaa32b32011-06-20 16:58:04 -0700213 Surface s = null;
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800214 if (sur != null) {
Jason Samsfaa32b32011-06-20 16:58:04 -0700215 s = sur.getSurface();
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800216 }
Jason Sams704ff642010-02-09 16:05:07 -0800217 mWidth = w;
218 mHeight = h;
Jason Samsfaa32b32011-06-20 16:58:04 -0700219 nContextSetSurface(w, h, s);
220 }
221
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700222 /**
Jason Samse619de62012-05-08 18:40:58 -0700223 * @deprecated in API 16
Jason Samsfaa32b32011-06-20 16:58:04 -0700224 * Bind an os surface
225 *
Jason Samsfaa32b32011-06-20 16:58:04 -0700226 * @param w
227 * @param h
228 * @param sur
229 */
230 public void setSurfaceTexture(SurfaceTexture sur, int w, int h) {
231 validate();
232 //android.util.Log.v("rs", "set surface " + sur + " w=" + w + ", h=" + h);
233
234 mWidth = w;
235 mHeight = h;
236 nContextSetSurfaceTexture(w, h, sur);
Jason Sams704ff642010-02-09 16:05:07 -0800237 }
238
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700239 /**
Jason Samsd4ca9912012-05-08 19:02:07 -0700240 * @deprecated in API 16
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800241 * return the height of the last set surface.
242 *
243 * @return int
244 */
Jason Sams5585e362010-10-29 10:19:21 -0700245 public int getHeight() {
246 return mHeight;
247 }
248
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700249 /**
Jason Samsd4ca9912012-05-08 19:02:07 -0700250 * @deprecated in API 16
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800251 * return the width of the last set surface.
252 *
253 * @return int
254 */
Jason Sams5585e362010-10-29 10:19:21 -0700255 public int getWidth() {
256 return mWidth;
257 }
Jason Sams704ff642010-02-09 16:05:07 -0800258
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700259 /**
Jason Samsd4ca9912012-05-08 19:02:07 -0700260 * @deprecated in API 16
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800261 * Temporarly halt calls to the root rendering script.
262 *
263 */
264 public void pause() {
Jason Sams704ff642010-02-09 16:05:07 -0800265 validate();
266 nContextPause();
267 }
268
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700269 /**
Jason Samsd4ca9912012-05-08 19:02:07 -0700270 * @deprecated in API 16
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800271 * Resume calls to the root rendering script.
272 *
273 */
274 public void resume() {
Jason Sams704ff642010-02-09 16:05:07 -0800275 validate();
276 nContextResume();
277 }
278
279
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700280 /**
Jason Samsd4ca9912012-05-08 19:02:07 -0700281 * @deprecated in API 16
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800282 * Set the script to handle calls to render the primary surface.
283 *
284 * @param s Graphics script to process rendering requests.
285 */
286 public void bindRootScript(Script s) {
Jason Sams704ff642010-02-09 16:05:07 -0800287 validate();
288 nContextBindRootScript(safeID(s));
289 }
290
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700291 /**
Jason Samsd4ca9912012-05-08 19:02:07 -0700292 * @deprecated in API 16
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800293 * Set the default ProgramStore object seen as the parent state by the root
294 * rendering script.
295 *
296 * @param p
297 */
298 public void bindProgramStore(ProgramStore p) {
Jason Sams704ff642010-02-09 16:05:07 -0800299 validate();
Jason Sams54db59c2010-05-13 18:30:11 -0700300 nContextBindProgramStore(safeID(p));
Jason Sams704ff642010-02-09 16:05:07 -0800301 }
302
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700303 /**
Jason Samsd4ca9912012-05-08 19:02:07 -0700304 * @deprecated in API 16
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800305 * Set the default ProgramFragment object seen as the parent state by the
306 * root rendering script.
307 *
308 * @param p
309 */
310 public void bindProgramFragment(ProgramFragment p) {
Jason Sams704ff642010-02-09 16:05:07 -0800311 validate();
312 nContextBindProgramFragment(safeID(p));
313 }
314
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700315 /**
Jason Samsd4ca9912012-05-08 19:02:07 -0700316 * @deprecated in API 16
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800317 * Set the default ProgramRaster object seen as the parent state by the
318 * root rendering script.
319 *
320 * @param p
321 */
322 public void bindProgramRaster(ProgramRaster p) {
Jason Sams704ff642010-02-09 16:05:07 -0800323 validate();
324 nContextBindProgramRaster(safeID(p));
325 }
326
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700327 /**
Jason Samsd4ca9912012-05-08 19:02:07 -0700328 * @deprecated in API 16
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800329 * Set the default ProgramVertex object seen as the parent state by the
330 * root rendering script.
331 *
332 * @param p
333 */
334 public void bindProgramVertex(ProgramVertex p) {
Jason Sams704ff642010-02-09 16:05:07 -0800335 validate();
336 nContextBindProgramVertex(safeID(p));
337 }
338
Jason Sams704ff642010-02-09 16:05:07 -0800339}