blob: 935b75ae4ac214f638b48659f13257bd751604a3 [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 Samsbf6ef8d72010-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 Samsbf6ef8d72010-12-06 15:59:59 -080085 throw new RSIllegalArgumentException("preferred must be >= Minimum.");
Jason Sams2222aa92010-10-10 17:58:25 -070086 }
87 }
88
Jason Samsbf6ef8d72010-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 Samsbf6ef8d72010-12-06 15:59:59 -080099 mColorPref = preferred;
Jason Sams2222aa92010-10-10 17:58:25 -0700100 }
Jason Samsbf6ef8d72010-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 Samsbf6ef8d72010-12-06 15:59:59 -0800112 mAlphaPref = preferred;
Jason Sams2222aa92010-10-10 17:58:25 -0700113 }
Jason Samsbf6ef8d72010-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 Samsbf6ef8d72010-12-06 15:59:59 -0800127 mDepthPref = preferred;
Jason Sams2222aa92010-10-10 17:58:25 -0700128 }
Jason Samsbf6ef8d72010-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 Samsbf6ef8d72010-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 Samsbf6ef8d72010-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 Samsbf6ef8d72010-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
Jason Sams704ff642010-02-09 16:05:07 -0800163 mWidth = 0;
164 mHeight = 0;
165 mDev = nDeviceCreate();
Alex Sakhartchouk2c74ad92011-03-16 19:28:25 -0700166 int dpi = ctx.getResources().getDisplayMetrics().densityDpi;
Jason Sams11c8af92010-10-13 15:31:10 -0700167 mContext = nContextCreateGL(mDev, 0,
168 mSurfaceConfig.mColorMin, mSurfaceConfig.mColorPref,
169 mSurfaceConfig.mAlphaMin, mSurfaceConfig.mAlphaPref,
170 mSurfaceConfig.mDepthMin, mSurfaceConfig.mDepthPref,
171 mSurfaceConfig.mStencilMin, mSurfaceConfig.mStencilPref,
172 mSurfaceConfig.mSamplesMin, mSurfaceConfig.mSamplesPref,
Alex Sakhartchouk2c74ad92011-03-16 19:28:25 -0700173 mSurfaceConfig.mSamplesQ, dpi);
Jason Samsd5f06302010-11-03 14:27:11 -0700174 if (mContext == 0) {
175 throw new RSDriverException("Failed to create RS context.");
176 }
Jason Sams704ff642010-02-09 16:05:07 -0800177 mMessageThread = new MessageThread(this);
178 mMessageThread.start();
Jason Sams704ff642010-02-09 16:05:07 -0800179 }
180
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800181 /**
182 * Bind an os surface
183 *
184 *
185 * @param w
186 * @param h
187 * @param sur
188 */
189 public void setSurface(SurfaceHolder sur, int w, int h) {
190 validate();
Jason Samsfaa32b32011-06-20 16:58:04 -0700191 Surface s = null;
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800192 if (sur != null) {
Jason Samsfaa32b32011-06-20 16:58:04 -0700193 s = sur.getSurface();
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800194 }
Jason Sams704ff642010-02-09 16:05:07 -0800195 mWidth = w;
196 mHeight = h;
Jason Samsfaa32b32011-06-20 16:58:04 -0700197 nContextSetSurface(w, h, s);
198 }
199
200 /**
201 * Bind an os surface
202 *
Jason Samsfaa32b32011-06-20 16:58:04 -0700203 * @param w
204 * @param h
205 * @param sur
206 */
207 public void setSurfaceTexture(SurfaceTexture sur, int w, int h) {
208 validate();
209 //android.util.Log.v("rs", "set surface " + sur + " w=" + w + ", h=" + h);
210
211 mWidth = w;
212 mHeight = h;
213 nContextSetSurfaceTexture(w, h, sur);
Jason Sams704ff642010-02-09 16:05:07 -0800214 }
215
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800216 /**
217 * return the height of the last set surface.
218 *
219 * @return int
220 */
Jason Sams5585e362010-10-29 10:19:21 -0700221 public int getHeight() {
222 return mHeight;
223 }
224
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800225 /**
226 * return the width of the last set surface.
227 *
228 * @return int
229 */
Jason Sams5585e362010-10-29 10:19:21 -0700230 public int getWidth() {
231 return mWidth;
232 }
Jason Sams704ff642010-02-09 16:05:07 -0800233
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800234 /**
235 * Temporarly halt calls to the root rendering script.
236 *
237 */
238 public void pause() {
Jason Sams704ff642010-02-09 16:05:07 -0800239 validate();
240 nContextPause();
241 }
242
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800243 /**
244 * Resume calls to the root rendering script.
245 *
246 */
247 public void resume() {
Jason Sams704ff642010-02-09 16:05:07 -0800248 validate();
249 nContextResume();
250 }
251
252
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800253 /**
254 * Set the script to handle calls to render the primary surface.
255 *
256 * @param s Graphics script to process rendering requests.
257 */
258 public void bindRootScript(Script s) {
Jason Sams704ff642010-02-09 16:05:07 -0800259 validate();
260 nContextBindRootScript(safeID(s));
261 }
262
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800263 /**
264 * Set the default ProgramStore object seen as the parent state by the root
265 * rendering script.
266 *
267 * @param p
268 */
269 public void bindProgramStore(ProgramStore p) {
Jason Sams704ff642010-02-09 16:05:07 -0800270 validate();
Jason Sams54db59c2010-05-13 18:30:11 -0700271 nContextBindProgramStore(safeID(p));
Jason Sams704ff642010-02-09 16:05:07 -0800272 }
273
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800274 /**
275 * Set the default ProgramFragment object seen as the parent state by the
276 * root rendering script.
277 *
278 * @param p
279 */
280 public void bindProgramFragment(ProgramFragment p) {
Jason Sams704ff642010-02-09 16:05:07 -0800281 validate();
282 nContextBindProgramFragment(safeID(p));
283 }
284
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800285 /**
286 * Set the default ProgramRaster object seen as the parent state by the
287 * root rendering script.
288 *
289 * @param p
290 */
291 public void bindProgramRaster(ProgramRaster p) {
Jason Sams704ff642010-02-09 16:05:07 -0800292 validate();
293 nContextBindProgramRaster(safeID(p));
294 }
295
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800296 /**
297 * Set the default ProgramVertex object seen as the parent state by the
298 * root rendering script.
299 *
300 * @param p
301 */
302 public void bindProgramVertex(ProgramVertex p) {
Jason Sams704ff642010-02-09 16:05:07 -0800303 validate();
304 nContextBindProgramVertex(safeID(p));
305 }
306
Jason Sams704ff642010-02-09 16:05:07 -0800307}