blob: 2cfeb176c14615bdacc0e280dea1df443858227b [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/**
Joe Fernandez3aef8e1d2011-12-20 10:38:34 -080032 * The Graphics derivitive of Renderscript. Extends the basic context to add a
Jason Sams27676fe2010-11-10 17:00:59 -080033 * 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.
Joe Fernandez3aef8e1d2011-12-20 10:38:34 -080037 *
38 * <div class="special reference">
39 * <h3>Developer Guides</h3>
40 * <p>For more information about creating an application that uses Renderscript, read the
41 * <a href="{@docRoot}guide/topics/graphics/renderscript.html">Renderscript</a> developer guide.</p>
42 * </div>
Jason Sams704ff642010-02-09 16:05:07 -080043 **/
44public class RenderScriptGL extends RenderScript {
Jason Sams704ff642010-02-09 16:05:07 -080045 int mWidth;
46 int mHeight;
47
Jason Sams27676fe2010-11-10 17:00:59 -080048 /**
49 * Class which is used to describe a pixel format for a graphical buffer.
50 * This is used to describe the intended format of the display surface.
51 *
Jason Samsbf6ef8d2010-12-06 15:59:59 -080052 * The configuration is described by pairs of minimum and preferred bit
53 * depths for each component within the config and additional structural
54 * information.
Jason Sams27676fe2010-11-10 17:00:59 -080055 */
Jason Sams2222aa92010-10-10 17:58:25 -070056 public static class SurfaceConfig {
57 int mDepthMin = 0;
58 int mDepthPref = 0;
59 int mStencilMin = 0;
60 int mStencilPref = 0;
61 int mColorMin = 8;
62 int mColorPref = 8;
63 int mAlphaMin = 0;
64 int mAlphaPref = 0;
65 int mSamplesMin = 1;
66 int mSamplesPref = 1;
67 float mSamplesQ = 1.f;
Jason Sams704ff642010-02-09 16:05:07 -080068
Jason Sams2222aa92010-10-10 17:58:25 -070069 public SurfaceConfig() {
70 }
71
72 public SurfaceConfig(SurfaceConfig sc) {
73 mDepthMin = sc.mDepthMin;
74 mDepthPref = sc.mDepthPref;
75 mStencilMin = sc.mStencilMin;
76 mStencilPref = sc.mStencilPref;
77 mColorMin = sc.mColorMin;
78 mColorPref = sc.mColorPref;
79 mAlphaMin = sc.mAlphaMin;
80 mAlphaPref = sc.mAlphaPref;
81 mSamplesMin = sc.mSamplesMin;
82 mSamplesPref = sc.mSamplesPref;
83 mSamplesQ = sc.mSamplesQ;
84 }
85
86 private void validateRange(int umin, int upref, int rmin, int rmax) {
87 if (umin < rmin || umin > rmax) {
Jason Samsc1d62102010-11-04 14:32:19 -070088 throw new RSIllegalArgumentException("Minimum value provided out of range.");
Jason Sams2222aa92010-10-10 17:58:25 -070089 }
90 if (upref < umin) {
Jason Samsbf6ef8d2010-12-06 15:59:59 -080091 throw new RSIllegalArgumentException("preferred must be >= Minimum.");
Jason Sams2222aa92010-10-10 17:58:25 -070092 }
93 }
94
Jason Samsbf6ef8d2010-12-06 15:59:59 -080095 /**
96 * Set the per-component bit depth for color (red, green, blue). This
97 * configures the surface for an unsigned integer buffer type.
98 *
99 * @param minimum
100 * @param preferred
101 */
102 public void setColor(int minimum, int preferred) {
103 validateRange(minimum, preferred, 5, 8);
Jason Sams2222aa92010-10-10 17:58:25 -0700104 mColorMin = minimum;
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800105 mColorPref = preferred;
Jason Sams2222aa92010-10-10 17:58:25 -0700106 }
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800107
108 /**
109 * Set the bit depth for alpha. This configures the surface for
110 * an unsigned integer buffer type.
111 *
112 * @param minimum
113 * @param preferred
114 */
115 public void setAlpha(int minimum, int preferred) {
116 validateRange(minimum, preferred, 0, 8);
Jason Sams2222aa92010-10-10 17:58:25 -0700117 mAlphaMin = minimum;
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800118 mAlphaPref = preferred;
Jason Sams2222aa92010-10-10 17:58:25 -0700119 }
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800120
121 /**
122 * Set the bit depth for the depth buffer. This configures the
123 * surface for an unsigned integer buffer type. If a minimum of 0
124 * is specified then its possible no depth buffer will be
125 * allocated.
126 *
127 * @param minimum
128 * @param preferred
129 */
130 public void setDepth(int minimum, int preferred) {
131 validateRange(minimum, preferred, 0, 24);
Jason Sams2222aa92010-10-10 17:58:25 -0700132 mDepthMin = minimum;
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800133 mDepthPref = preferred;
Jason Sams2222aa92010-10-10 17:58:25 -0700134 }
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800135
136 /**
137 * Configure the multisample rendering.
138 *
139 * @param minimum The required number of samples, must be at least 1.
140 * @param preferred The targe number of samples, must be at least
141 * minimum
142 * @param Q The quality of samples, range 0-1. Used to decide between
143 * different formats which have the same number of samples but
144 * different rendering quality.
145 */
146 public void setSamples(int minimum, int preferred, float Q) {
147 validateRange(minimum, preferred, 1, 32);
Jason Sams2222aa92010-10-10 17:58:25 -0700148 if (Q < 0.0f || Q > 1.0f) {
Jason Samsc1d62102010-11-04 14:32:19 -0700149 throw new RSIllegalArgumentException("Quality out of 0-1 range.");
Jason Sams2222aa92010-10-10 17:58:25 -0700150 }
151 mSamplesMin = minimum;
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800152 mSamplesPref = preferred;
Jason Sams2222aa92010-10-10 17:58:25 -0700153 mSamplesQ = Q;
154 }
155 };
156
157 SurfaceConfig mSurfaceConfig;
Jason Sams2222aa92010-10-10 17:58:25 -0700158
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800159 /**
160 * Construct a new RenderScriptGL context.
161 *
Shih-wei Liao6b32fab2010-12-10 01:03:59 -0800162 * @param ctx The context.
Stephen Hines8cecbb52011-02-28 18:20:34 -0800163 * @param sc The desired format of the primary rendering surface.
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800164 */
Shih-wei Liao6b32fab2010-12-10 01:03:59 -0800165 public RenderScriptGL(Context ctx, SurfaceConfig sc) {
166 super(ctx);
Jason Sams2222aa92010-10-10 17:58:25 -0700167 mSurfaceConfig = new SurfaceConfig(sc);
168
Stephen Hines4382467a2011-08-01 15:02:34 -0700169 int sdkVersion = getTargetSdkVersion(ctx);
170
Jason Sams704ff642010-02-09 16:05:07 -0800171 mWidth = 0;
172 mHeight = 0;
173 mDev = nDeviceCreate();
Alex Sakhartchouk2c74ad92011-03-16 19:28:25 -0700174 int dpi = ctx.getResources().getDisplayMetrics().densityDpi;
Stephen Hines4382467a2011-08-01 15:02:34 -0700175 mContext = nContextCreateGL(mDev, 0, sdkVersion,
Jason Sams11c8af92010-10-13 15:31:10 -0700176 mSurfaceConfig.mColorMin, mSurfaceConfig.mColorPref,
177 mSurfaceConfig.mAlphaMin, mSurfaceConfig.mAlphaPref,
178 mSurfaceConfig.mDepthMin, mSurfaceConfig.mDepthPref,
179 mSurfaceConfig.mStencilMin, mSurfaceConfig.mStencilPref,
180 mSurfaceConfig.mSamplesMin, mSurfaceConfig.mSamplesPref,
Alex Sakhartchouk2c74ad92011-03-16 19:28:25 -0700181 mSurfaceConfig.mSamplesQ, dpi);
Jason Samsd5f06302010-11-03 14:27:11 -0700182 if (mContext == 0) {
183 throw new RSDriverException("Failed to create RS context.");
184 }
Jason Sams704ff642010-02-09 16:05:07 -0800185 mMessageThread = new MessageThread(this);
186 mMessageThread.start();
Jason Sams704ff642010-02-09 16:05:07 -0800187 }
188
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800189 /**
190 * Bind an os surface
191 *
192 *
193 * @param w
194 * @param h
195 * @param sur
196 */
197 public void setSurface(SurfaceHolder sur, int w, int h) {
198 validate();
Jason Samsfaa32b32011-06-20 16:58:04 -0700199 Surface s = null;
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800200 if (sur != null) {
Jason Samsfaa32b32011-06-20 16:58:04 -0700201 s = sur.getSurface();
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800202 }
Jason Sams704ff642010-02-09 16:05:07 -0800203 mWidth = w;
204 mHeight = h;
Jason Samsfaa32b32011-06-20 16:58:04 -0700205 nContextSetSurface(w, h, s);
206 }
207
208 /**
209 * Bind an os surface
210 *
Jason Samsfaa32b32011-06-20 16:58:04 -0700211 * @param w
212 * @param h
213 * @param sur
214 */
215 public void setSurfaceTexture(SurfaceTexture sur, int w, int h) {
216 validate();
217 //android.util.Log.v("rs", "set surface " + sur + " w=" + w + ", h=" + h);
218
219 mWidth = w;
220 mHeight = h;
221 nContextSetSurfaceTexture(w, h, sur);
Jason Sams704ff642010-02-09 16:05:07 -0800222 }
223
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800224 /**
225 * return the height of the last set surface.
226 *
227 * @return int
228 */
Jason Sams5585e362010-10-29 10:19:21 -0700229 public int getHeight() {
230 return mHeight;
231 }
232
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800233 /**
234 * return the width of the last set surface.
235 *
236 * @return int
237 */
Jason Sams5585e362010-10-29 10:19:21 -0700238 public int getWidth() {
239 return mWidth;
240 }
Jason Sams704ff642010-02-09 16:05:07 -0800241
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800242 /**
243 * Temporarly halt calls to the root rendering script.
244 *
245 */
246 public void pause() {
Jason Sams704ff642010-02-09 16:05:07 -0800247 validate();
248 nContextPause();
249 }
250
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800251 /**
252 * Resume calls to the root rendering script.
253 *
254 */
255 public void resume() {
Jason Sams704ff642010-02-09 16:05:07 -0800256 validate();
257 nContextResume();
258 }
259
260
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800261 /**
262 * Set the script to handle calls to render the primary surface.
263 *
264 * @param s Graphics script to process rendering requests.
265 */
266 public void bindRootScript(Script s) {
Jason Sams704ff642010-02-09 16:05:07 -0800267 validate();
268 nContextBindRootScript(safeID(s));
269 }
270
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800271 /**
272 * Set the default ProgramStore object seen as the parent state by the root
273 * rendering script.
274 *
275 * @param p
276 */
277 public void bindProgramStore(ProgramStore p) {
Jason Sams704ff642010-02-09 16:05:07 -0800278 validate();
Jason Sams54db59c2010-05-13 18:30:11 -0700279 nContextBindProgramStore(safeID(p));
Jason Sams704ff642010-02-09 16:05:07 -0800280 }
281
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800282 /**
283 * Set the default ProgramFragment object seen as the parent state by the
284 * root rendering script.
285 *
286 * @param p
287 */
288 public void bindProgramFragment(ProgramFragment p) {
Jason Sams704ff642010-02-09 16:05:07 -0800289 validate();
290 nContextBindProgramFragment(safeID(p));
291 }
292
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800293 /**
294 * Set the default ProgramRaster object seen as the parent state by the
295 * root rendering script.
296 *
297 * @param p
298 */
299 public void bindProgramRaster(ProgramRaster p) {
Jason Sams704ff642010-02-09 16:05:07 -0800300 validate();
301 nContextBindProgramRaster(safeID(p));
302 }
303
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800304 /**
305 * Set the default ProgramVertex object seen as the parent state by the
306 * root rendering script.
307 *
308 * @param p
309 */
310 public void bindProgramVertex(ProgramVertex p) {
Jason Sams704ff642010-02-09 16:05:07 -0800311 validate();
312 nContextBindProgramVertex(safeID(p));
313 }
314
Jason Sams704ff642010-02-09 16:05:07 -0800315}