blob: c9cbe3e8bf6ecf5063fbe3ee5590f662dc97bb4d [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
Tim Murrayc11e25c2013-04-09 11:01:01 -070034 * 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>
Tim Murrayc11e25c2013-04-09 11:01:01 -070042 * <p>For more information about creating an application that uses RenderScript, read the
43 * <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 Samsbf6ef8d2010-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 Samsbf6ef8d2010-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 Samsbf6ef8d2010-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 Samsbf6ef8d2010-12-06 15:59:59 -0800115 mColorPref = preferred;
Jason Sams2222aa92010-10-10 17:58:25 -0700116 }
Jason Samsbf6ef8d2010-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 Samsbf6ef8d2010-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 Samsbf6ef8d2010-12-06 15:59:59 -0800129 mAlphaPref = preferred;
Jason Sams2222aa92010-10-10 17:58:25 -0700130 }
Jason Samsbf6ef8d2010-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 Samsbf6ef8d2010-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 Samsbf6ef8d2010-12-06 15:59:59 -0800145 mDepthPref = preferred;
Jason Sams2222aa92010-10-10 17:58:25 -0700146 }
Jason Samsbf6ef8d2010-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 Samsbf6ef8d2010-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 Samsbf6ef8d2010-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 Samsbf6ef8d2010-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 Samsbf6ef8d2010-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 Sams1a4e1f32012-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();
Jason Sams704ff642010-02-09 16:05:07 -0800201 }
202
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700203 /**
Jason Samsd4ca9912012-05-08 19:02:07 -0700204 * @deprecated in API 16
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800205 * Bind an os surface
206 *
207 *
208 * @param w
209 * @param h
210 * @param sur
211 */
212 public void setSurface(SurfaceHolder sur, int w, int h) {
213 validate();
Jason Samsfaa32b32011-06-20 16:58:04 -0700214 Surface s = null;
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800215 if (sur != null) {
Jason Samsfaa32b32011-06-20 16:58:04 -0700216 s = sur.getSurface();
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800217 }
Jason Sams704ff642010-02-09 16:05:07 -0800218 mWidth = w;
219 mHeight = h;
Jason Samsfaa32b32011-06-20 16:58:04 -0700220 nContextSetSurface(w, h, s);
221 }
222
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700223 /**
Jason Samse619de62012-05-08 18:40:58 -0700224 * @deprecated in API 16
Jason Samsfaa32b32011-06-20 16:58:04 -0700225 * Bind an os surface
226 *
Jason Samsfaa32b32011-06-20 16:58:04 -0700227 * @param w
228 * @param h
229 * @param sur
230 */
231 public void setSurfaceTexture(SurfaceTexture sur, int w, int h) {
232 validate();
233 //android.util.Log.v("rs", "set surface " + sur + " w=" + w + ", h=" + h);
234
235 mWidth = w;
236 mHeight = h;
237 nContextSetSurfaceTexture(w, h, sur);
Jason Sams704ff642010-02-09 16:05:07 -0800238 }
239
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700240 /**
Jason Samsd4ca9912012-05-08 19:02:07 -0700241 * @deprecated in API 16
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800242 * return the height of the last set surface.
243 *
244 * @return int
245 */
Jason Sams5585e362010-10-29 10:19:21 -0700246 public int getHeight() {
247 return mHeight;
248 }
249
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700250 /**
Jason Samsd4ca9912012-05-08 19:02:07 -0700251 * @deprecated in API 16
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800252 * return the width of the last set surface.
253 *
254 * @return int
255 */
Jason Sams5585e362010-10-29 10:19:21 -0700256 public int getWidth() {
257 return mWidth;
258 }
Jason Sams704ff642010-02-09 16:05:07 -0800259
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700260 /**
Jason Samsd4ca9912012-05-08 19:02:07 -0700261 * @deprecated in API 16
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800262 * Temporarly halt calls to the root rendering script.
263 *
264 */
265 public void pause() {
Jason Sams704ff642010-02-09 16:05:07 -0800266 validate();
267 nContextPause();
268 }
269
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700270 /**
Jason Samsd4ca9912012-05-08 19:02:07 -0700271 * @deprecated in API 16
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800272 * Resume calls to the root rendering script.
273 *
274 */
275 public void resume() {
Jason Sams704ff642010-02-09 16:05:07 -0800276 validate();
277 nContextResume();
278 }
279
280
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700281 /**
Jason Samsd4ca9912012-05-08 19:02:07 -0700282 * @deprecated in API 16
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800283 * Set the script to handle calls to render the primary surface.
284 *
285 * @param s Graphics script to process rendering requests.
286 */
287 public void bindRootScript(Script s) {
Jason Sams704ff642010-02-09 16:05:07 -0800288 validate();
Tim Murray7a629fa2013-11-19 12:45:54 -0800289 nContextBindRootScript((int)safeID(s));
Jason Sams704ff642010-02-09 16:05:07 -0800290 }
291
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700292 /**
Jason Samsd4ca9912012-05-08 19:02:07 -0700293 * @deprecated in API 16
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800294 * Set the default ProgramStore object seen as the parent state by the root
295 * rendering script.
296 *
297 * @param p
298 */
299 public void bindProgramStore(ProgramStore p) {
Jason Sams704ff642010-02-09 16:05:07 -0800300 validate();
Tim Murray7a629fa2013-11-19 12:45:54 -0800301 nContextBindProgramStore((int)safeID(p));
Jason Sams704ff642010-02-09 16:05:07 -0800302 }
303
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700304 /**
Jason Samsd4ca9912012-05-08 19:02:07 -0700305 * @deprecated in API 16
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800306 * Set the default ProgramFragment object seen as the parent state by the
307 * root rendering script.
308 *
309 * @param p
310 */
311 public void bindProgramFragment(ProgramFragment p) {
Jason Sams704ff642010-02-09 16:05:07 -0800312 validate();
Tim Murray7a629fa2013-11-19 12:45:54 -0800313 nContextBindProgramFragment((int)safeID(p));
Jason Sams704ff642010-02-09 16:05:07 -0800314 }
315
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700316 /**
Jason Samsd4ca9912012-05-08 19:02:07 -0700317 * @deprecated in API 16
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800318 * Set the default ProgramRaster object seen as the parent state by the
319 * root rendering script.
320 *
321 * @param p
322 */
323 public void bindProgramRaster(ProgramRaster p) {
Jason Sams704ff642010-02-09 16:05:07 -0800324 validate();
Tim Murray7a629fa2013-11-19 12:45:54 -0800325 nContextBindProgramRaster((int)safeID(p));
Jason Sams704ff642010-02-09 16:05:07 -0800326 }
327
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700328 /**
Jason Samsd4ca9912012-05-08 19:02:07 -0700329 * @deprecated in API 16
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800330 * Set the default ProgramVertex object seen as the parent state by the
331 * root rendering script.
332 *
333 * @param p
334 */
335 public void bindProgramVertex(ProgramVertex p) {
Jason Sams704ff642010-02-09 16:05:07 -0800336 validate();
Tim Murray7a629fa2013-11-19 12:45:54 -0800337 nContextBindProgramVertex((int)safeID(p));
Jason Sams704ff642010-02-09 16:05:07 -0800338 }
339
Jason Sams704ff642010-02-09 16:05:07 -0800340}