blob: fad883873458e9d7cb4a521bd3bd86d0c27a1c4a [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/**
Tim Murraya9084222013-04-05 22:06:43 +000032 * @hide
Jason Samsd4ca9912012-05-08 19:02:07 -070033 * @deprecated in API 16
Joe Fernandez3aef8e1d2011-12-20 10:38:34 -080034 * The Graphics derivitive of Renderscript. Extends the basic context to add a
Jason Sams27676fe2010-11-10 17:00:59 -080035 * 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.
Joe Fernandez3aef8e1d2011-12-20 10:38:34 -080039 *
40 * <div class="special reference">
41 * <h3>Developer Guides</h3>
42 * <p>For more information about creating an application that uses Renderscript, read the
Scott Mainb47fa162013-02-05 14:23:13 -080043 * <a href="{@docRoot}guide/topics/renderscript/index.html">Renderscript</a> developer guide.</p>
Joe Fernandez3aef8e1d2011-12-20 10:38:34 -080044 * </div>
Jason Sams704ff642010-02-09 16:05:07 -080045 **/
46public class RenderScriptGL extends RenderScript {
Jason Sams704ff642010-02-09 16:05:07 -080047 int mWidth;
48 int mHeight;
49
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070050 /**
Jason Samsd4ca9912012-05-08 19:02:07 -070051 * @deprecated in API 16
Jason Sams27676fe2010-11-10 17:00:59 -080052 * Class which is used to describe a pixel format for a graphical buffer.
53 * This is used to describe the intended format of the display surface.
54 *
Jason Samsbf6ef8d72010-12-06 15:59:59 -080055 * The configuration is described by pairs of minimum and preferred bit
56 * depths for each component within the config and additional structural
57 * information.
Jason Sams27676fe2010-11-10 17:00:59 -080058 */
Jason Sams2222aa92010-10-10 17:58:25 -070059 public static class SurfaceConfig {
60 int mDepthMin = 0;
61 int mDepthPref = 0;
62 int mStencilMin = 0;
63 int mStencilPref = 0;
64 int mColorMin = 8;
65 int mColorPref = 8;
66 int mAlphaMin = 0;
67 int mAlphaPref = 0;
68 int mSamplesMin = 1;
69 int mSamplesPref = 1;
70 float mSamplesQ = 1.f;
Jason Sams704ff642010-02-09 16:05:07 -080071
Jason Samsd4ca9912012-05-08 19:02:07 -070072 /**
73 * @deprecated in API 16
74 */
Jason Sams2222aa92010-10-10 17:58:25 -070075 public SurfaceConfig() {
76 }
77
Jason Samsd4ca9912012-05-08 19:02:07 -070078 /**
79 * @deprecated in API 16
80 */
Jason Sams2222aa92010-10-10 17:58:25 -070081 public SurfaceConfig(SurfaceConfig sc) {
82 mDepthMin = sc.mDepthMin;
83 mDepthPref = sc.mDepthPref;
84 mStencilMin = sc.mStencilMin;
85 mStencilPref = sc.mStencilPref;
86 mColorMin = sc.mColorMin;
87 mColorPref = sc.mColorPref;
88 mAlphaMin = sc.mAlphaMin;
89 mAlphaPref = sc.mAlphaPref;
90 mSamplesMin = sc.mSamplesMin;
91 mSamplesPref = sc.mSamplesPref;
92 mSamplesQ = sc.mSamplesQ;
93 }
94
95 private void validateRange(int umin, int upref, int rmin, int rmax) {
96 if (umin < rmin || umin > rmax) {
Jason Samsc1d62102010-11-04 14:32:19 -070097 throw new RSIllegalArgumentException("Minimum value provided out of range.");
Jason Sams2222aa92010-10-10 17:58:25 -070098 }
99 if (upref < umin) {
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800100 throw new RSIllegalArgumentException("preferred must be >= Minimum.");
Jason Sams2222aa92010-10-10 17:58:25 -0700101 }
102 }
103
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700104 /**
Jason Samsd4ca9912012-05-08 19:02:07 -0700105 * @deprecated in API 16
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800106 * Set the per-component bit depth for color (red, green, blue). This
107 * configures the surface for an unsigned integer buffer type.
108 *
109 * @param minimum
110 * @param preferred
111 */
112 public void setColor(int minimum, int preferred) {
113 validateRange(minimum, preferred, 5, 8);
Jason Sams2222aa92010-10-10 17:58:25 -0700114 mColorMin = minimum;
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800115 mColorPref = preferred;
Jason Sams2222aa92010-10-10 17:58:25 -0700116 }
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800117
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700118 /**
Jason Samsd4ca9912012-05-08 19:02:07 -0700119 * @deprecated in API 16
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800120 * Set the bit depth for alpha. This configures the surface for
121 * an unsigned integer buffer type.
122 *
123 * @param minimum
124 * @param preferred
125 */
126 public void setAlpha(int minimum, int preferred) {
127 validateRange(minimum, preferred, 0, 8);
Jason Sams2222aa92010-10-10 17:58:25 -0700128 mAlphaMin = minimum;
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800129 mAlphaPref = preferred;
Jason Sams2222aa92010-10-10 17:58:25 -0700130 }
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800131
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700132 /**
Jason Samsd4ca9912012-05-08 19:02:07 -0700133 * @deprecated in API 16
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800134 * Set the bit depth for the depth buffer. This configures the
135 * surface for an unsigned integer buffer type. If a minimum of 0
136 * is specified then its possible no depth buffer will be
137 * allocated.
138 *
139 * @param minimum
140 * @param preferred
141 */
142 public void setDepth(int minimum, int preferred) {
143 validateRange(minimum, preferred, 0, 24);
Jason Sams2222aa92010-10-10 17:58:25 -0700144 mDepthMin = minimum;
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800145 mDepthPref = preferred;
Jason Sams2222aa92010-10-10 17:58:25 -0700146 }
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800147
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700148 /**
Jason Samsd4ca9912012-05-08 19:02:07 -0700149 * @deprecated in API 16
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800150 * Configure the multisample rendering.
151 *
152 * @param minimum The required number of samples, must be at least 1.
153 * @param preferred The targe number of samples, must be at least
154 * minimum
155 * @param Q The quality of samples, range 0-1. Used to decide between
156 * different formats which have the same number of samples but
157 * different rendering quality.
158 */
159 public void setSamples(int minimum, int preferred, float Q) {
160 validateRange(minimum, preferred, 1, 32);
Jason Sams2222aa92010-10-10 17:58:25 -0700161 if (Q < 0.0f || Q > 1.0f) {
Jason Samsc1d62102010-11-04 14:32:19 -0700162 throw new RSIllegalArgumentException("Quality out of 0-1 range.");
Jason Sams2222aa92010-10-10 17:58:25 -0700163 }
164 mSamplesMin = minimum;
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800165 mSamplesPref = preferred;
Jason Sams2222aa92010-10-10 17:58:25 -0700166 mSamplesQ = Q;
167 }
168 };
169
170 SurfaceConfig mSurfaceConfig;
Jason Sams2222aa92010-10-10 17:58:25 -0700171
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700172 /**
Jason Samsd4ca9912012-05-08 19:02:07 -0700173 * @deprecated in API 16
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800174 * Construct a new RenderScriptGL context.
175 *
Shih-wei Liao6b32fab2010-12-10 01:03:59 -0800176 * @param ctx The context.
Stephen Hines8cecbb52011-02-28 18:20:34 -0800177 * @param sc The desired format of the primary rendering surface.
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800178 */
Shih-wei Liao6b32fab2010-12-10 01:03:59 -0800179 public RenderScriptGL(Context ctx, SurfaceConfig sc) {
180 super(ctx);
Jason Sams2222aa92010-10-10 17:58:25 -0700181 mSurfaceConfig = new SurfaceConfig(sc);
182
Jason Sams1a4e1f3e2012-02-24 17:51:24 -0800183 int sdkVersion = ctx.getApplicationInfo().targetSdkVersion;
Stephen Hines4382467a2011-08-01 15:02:34 -0700184
Jason Sams704ff642010-02-09 16:05:07 -0800185 mWidth = 0;
186 mHeight = 0;
187 mDev = nDeviceCreate();
Alex Sakhartchouk2c74ad92011-03-16 19:28:25 -0700188 int dpi = ctx.getResources().getDisplayMetrics().densityDpi;
Stephen Hines4382467a2011-08-01 15:02:34 -0700189 mContext = nContextCreateGL(mDev, 0, sdkVersion,
Jason Sams11c8af92010-10-13 15:31:10 -0700190 mSurfaceConfig.mColorMin, mSurfaceConfig.mColorPref,
191 mSurfaceConfig.mAlphaMin, mSurfaceConfig.mAlphaPref,
192 mSurfaceConfig.mDepthMin, mSurfaceConfig.mDepthPref,
193 mSurfaceConfig.mStencilMin, mSurfaceConfig.mStencilPref,
194 mSurfaceConfig.mSamplesMin, mSurfaceConfig.mSamplesPref,
Alex Sakhartchouk2c74ad92011-03-16 19:28:25 -0700195 mSurfaceConfig.mSamplesQ, dpi);
Jason Samsd5f06302010-11-03 14:27:11 -0700196 if (mContext == 0) {
197 throw new RSDriverException("Failed to create RS context.");
198 }
Jason Sams704ff642010-02-09 16:05:07 -0800199 mMessageThread = new MessageThread(this);
200 mMessageThread.start();
Tim Murrayfb329932013-04-10 11:19:54 -0700201 mGCThread = new GCThread(this);
202 mGCThread.start();
203
Jason Sams704ff642010-02-09 16:05:07 -0800204 }
205
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700206 /**
Jason Samsd4ca9912012-05-08 19:02:07 -0700207 * @deprecated in API 16
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800208 * Bind an os surface
209 *
210 *
211 * @param w
212 * @param h
213 * @param sur
214 */
215 public void setSurface(SurfaceHolder sur, int w, int h) {
216 validate();
Jason Samsfaa32b32011-06-20 16:58:04 -0700217 Surface s = null;
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800218 if (sur != null) {
Jason Samsfaa32b32011-06-20 16:58:04 -0700219 s = sur.getSurface();
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800220 }
Jason Sams704ff642010-02-09 16:05:07 -0800221 mWidth = w;
222 mHeight = h;
Jason Samsfaa32b32011-06-20 16:58:04 -0700223 nContextSetSurface(w, h, s);
224 }
225
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700226 /**
Jason Samse619de62012-05-08 18:40:58 -0700227 * @deprecated in API 16
Jason Samsfaa32b32011-06-20 16:58:04 -0700228 * Bind an os surface
229 *
Jason Samsfaa32b32011-06-20 16:58:04 -0700230 * @param w
231 * @param h
232 * @param sur
233 */
234 public void setSurfaceTexture(SurfaceTexture sur, int w, int h) {
235 validate();
236 //android.util.Log.v("rs", "set surface " + sur + " w=" + w + ", h=" + h);
237
238 mWidth = w;
239 mHeight = h;
240 nContextSetSurfaceTexture(w, h, sur);
Jason Sams704ff642010-02-09 16:05:07 -0800241 }
242
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700243 /**
Jason Samsd4ca9912012-05-08 19:02:07 -0700244 * @deprecated in API 16
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800245 * return the height of the last set surface.
246 *
247 * @return int
248 */
Jason Sams5585e362010-10-29 10:19:21 -0700249 public int getHeight() {
250 return mHeight;
251 }
252
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700253 /**
Jason Samsd4ca9912012-05-08 19:02:07 -0700254 * @deprecated in API 16
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800255 * return the width of the last set surface.
256 *
257 * @return int
258 */
Jason Sams5585e362010-10-29 10:19:21 -0700259 public int getWidth() {
260 return mWidth;
261 }
Jason Sams704ff642010-02-09 16:05:07 -0800262
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700263 /**
Jason Samsd4ca9912012-05-08 19:02:07 -0700264 * @deprecated in API 16
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800265 * Temporarly halt calls to the root rendering script.
266 *
267 */
268 public void pause() {
Jason Sams704ff642010-02-09 16:05:07 -0800269 validate();
270 nContextPause();
271 }
272
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700273 /**
Jason Samsd4ca9912012-05-08 19:02:07 -0700274 * @deprecated in API 16
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800275 * Resume calls to the root rendering script.
276 *
277 */
278 public void resume() {
Jason Sams704ff642010-02-09 16:05:07 -0800279 validate();
280 nContextResume();
281 }
282
283
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700284 /**
Jason Samsd4ca9912012-05-08 19:02:07 -0700285 * @deprecated in API 16
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800286 * Set the script to handle calls to render the primary surface.
287 *
288 * @param s Graphics script to process rendering requests.
289 */
290 public void bindRootScript(Script s) {
Jason Sams704ff642010-02-09 16:05:07 -0800291 validate();
292 nContextBindRootScript(safeID(s));
293 }
294
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700295 /**
Jason Samsd4ca9912012-05-08 19:02:07 -0700296 * @deprecated in API 16
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800297 * Set the default ProgramStore object seen as the parent state by the root
298 * rendering script.
299 *
300 * @param p
301 */
302 public void bindProgramStore(ProgramStore p) {
Jason Sams704ff642010-02-09 16:05:07 -0800303 validate();
Jason Sams54db59c2010-05-13 18:30:11 -0700304 nContextBindProgramStore(safeID(p));
Jason Sams704ff642010-02-09 16:05:07 -0800305 }
306
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700307 /**
Jason Samsd4ca9912012-05-08 19:02:07 -0700308 * @deprecated in API 16
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800309 * Set the default ProgramFragment object seen as the parent state by the
310 * root rendering script.
311 *
312 * @param p
313 */
314 public void bindProgramFragment(ProgramFragment p) {
Jason Sams704ff642010-02-09 16:05:07 -0800315 validate();
316 nContextBindProgramFragment(safeID(p));
317 }
318
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700319 /**
Jason Samsd4ca9912012-05-08 19:02:07 -0700320 * @deprecated in API 16
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800321 * Set the default ProgramRaster object seen as the parent state by the
322 * root rendering script.
323 *
324 * @param p
325 */
326 public void bindProgramRaster(ProgramRaster p) {
Jason Sams704ff642010-02-09 16:05:07 -0800327 validate();
328 nContextBindProgramRaster(safeID(p));
329 }
330
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700331 /**
Jason Samsd4ca9912012-05-08 19:02:07 -0700332 * @deprecated in API 16
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800333 * Set the default ProgramVertex object seen as the parent state by the
334 * root rendering script.
335 *
336 * @param p
337 */
338 public void bindProgramVertex(ProgramVertex p) {
Jason Sams704ff642010-02-09 16:05:07 -0800339 validate();
340 nContextBindProgramVertex(safeID(p));
341 }
342
Jason Sams704ff642010-02-09 16:05:07 -0800343}