blob: 53b6e287ceefe9ebf7ac1fc523e0b2260c17a57c [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 Sams704ff642010-02-09 16:05:07 -080025import android.util.Log;
26import android.view.Surface;
Jason Sams2222aa92010-10-10 17:58:25 -070027import android.view.SurfaceHolder;
28import android.view.SurfaceView;
Jason Sams704ff642010-02-09 16:05:07 -080029
30/**
Jason Sams27676fe2010-11-10 17:00:59 -080031 * The Graphics derivitive of RenderScript. Extends the basic context to add a
32 * root script which is the display window for graphical output. When the
33 * system needs to update the display the currently bound root script will be
34 * called. This script is expected to issue the rendering commands to repaint
35 * the screen.
Jason Sams704ff642010-02-09 16:05:07 -080036 **/
37public class RenderScriptGL extends RenderScript {
38 private Surface mSurface;
39 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 mSurface = null;
164 mWidth = 0;
165 mHeight = 0;
166 mDev = nDeviceCreate();
Alex Sakhartchouk2c74ad92011-03-16 19:28:25 -0700167 int dpi = ctx.getResources().getDisplayMetrics().densityDpi;
Jason Sams11c8af92010-10-13 15:31:10 -0700168 mContext = nContextCreateGL(mDev, 0,
169 mSurfaceConfig.mColorMin, mSurfaceConfig.mColorPref,
170 mSurfaceConfig.mAlphaMin, mSurfaceConfig.mAlphaPref,
171 mSurfaceConfig.mDepthMin, mSurfaceConfig.mDepthPref,
172 mSurfaceConfig.mStencilMin, mSurfaceConfig.mStencilPref,
173 mSurfaceConfig.mSamplesMin, mSurfaceConfig.mSamplesPref,
Alex Sakhartchouk2c74ad92011-03-16 19:28:25 -0700174 mSurfaceConfig.mSamplesQ, dpi);
Jason Samsd5f06302010-11-03 14:27:11 -0700175 if (mContext == 0) {
176 throw new RSDriverException("Failed to create RS context.");
177 }
Jason Sams704ff642010-02-09 16:05:07 -0800178 mMessageThread = new MessageThread(this);
179 mMessageThread.start();
Jason Sams704ff642010-02-09 16:05:07 -0800180 }
181
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800182 /**
183 * Bind an os surface
184 *
185 *
186 * @param w
187 * @param h
188 * @param sur
189 */
190 public void setSurface(SurfaceHolder sur, int w, int h) {
191 validate();
192 if (sur != null) {
193 mSurface = sur.getSurface();
194 } else {
195 mSurface = null;
196 }
Jason Sams704ff642010-02-09 16:05:07 -0800197 mWidth = w;
198 mHeight = h;
Jason Sams704ff642010-02-09 16:05:07 -0800199 nContextSetSurface(w, h, mSurface);
200 }
201
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800202 /**
203 * return the height of the last set surface.
204 *
205 * @return int
206 */
Jason Sams5585e362010-10-29 10:19:21 -0700207 public int getHeight() {
208 return mHeight;
209 }
210
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800211 /**
212 * return the width of the last set surface.
213 *
214 * @return int
215 */
Jason Sams5585e362010-10-29 10:19:21 -0700216 public int getWidth() {
217 return mWidth;
218 }
Jason Sams704ff642010-02-09 16:05:07 -0800219
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800220 /**
221 * Temporarly halt calls to the root rendering script.
222 *
223 */
224 public void pause() {
Jason Sams704ff642010-02-09 16:05:07 -0800225 validate();
226 nContextPause();
227 }
228
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800229 /**
230 * Resume calls to the root rendering script.
231 *
232 */
233 public void resume() {
Jason Sams704ff642010-02-09 16:05:07 -0800234 validate();
235 nContextResume();
236 }
237
238
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800239 /**
240 * Set the script to handle calls to render the primary surface.
241 *
242 * @param s Graphics script to process rendering requests.
243 */
244 public void bindRootScript(Script s) {
Jason Sams704ff642010-02-09 16:05:07 -0800245 validate();
246 nContextBindRootScript(safeID(s));
247 }
248
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800249 /**
250 * Set the default ProgramStore object seen as the parent state by the root
251 * rendering script.
252 *
253 * @param p
254 */
255 public void bindProgramStore(ProgramStore p) {
Jason Sams704ff642010-02-09 16:05:07 -0800256 validate();
Jason Sams54db59c2010-05-13 18:30:11 -0700257 nContextBindProgramStore(safeID(p));
Jason Sams704ff642010-02-09 16:05:07 -0800258 }
259
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800260 /**
261 * Set the default ProgramFragment object seen as the parent state by the
262 * root rendering script.
263 *
264 * @param p
265 */
266 public void bindProgramFragment(ProgramFragment p) {
Jason Sams704ff642010-02-09 16:05:07 -0800267 validate();
268 nContextBindProgramFragment(safeID(p));
269 }
270
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800271 /**
272 * Set the default ProgramRaster object seen as the parent state by the
273 * root rendering script.
274 *
275 * @param p
276 */
277 public void bindProgramRaster(ProgramRaster p) {
Jason Sams704ff642010-02-09 16:05:07 -0800278 validate();
279 nContextBindProgramRaster(safeID(p));
280 }
281
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800282 /**
283 * Set the default ProgramVertex object seen as the parent state by the
284 * root rendering script.
285 *
286 * @param p
287 */
288 public void bindProgramVertex(ProgramVertex p) {
Jason Sams704ff642010-02-09 16:05:07 -0800289 validate();
290 nContextBindProgramVertex(safeID(p));
291 }
292
Jason Sams704ff642010-02-09 16:05:07 -0800293}