blob: d4b5434ae3a7843a06f76ac15b979b494f174f97 [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;
25import android.util.Config;
26import 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 {
39 private Surface mSurface;
40 int mWidth;
41 int mHeight;
42
Jason Sams27676fe2010-11-10 17:00:59 -080043 /**
44 * Class which is used to describe a pixel format for a graphical buffer.
45 * This is used to describe the intended format of the display surface.
46 *
Jason Samsbf6ef8d2010-12-06 15:59:59 -080047 * The configuration is described by pairs of minimum and preferred bit
48 * depths for each component within the config and additional structural
49 * information.
Jason Sams27676fe2010-11-10 17:00:59 -080050 */
Jason Sams2222aa92010-10-10 17:58:25 -070051 public static class SurfaceConfig {
52 int mDepthMin = 0;
53 int mDepthPref = 0;
54 int mStencilMin = 0;
55 int mStencilPref = 0;
56 int mColorMin = 8;
57 int mColorPref = 8;
58 int mAlphaMin = 0;
59 int mAlphaPref = 0;
60 int mSamplesMin = 1;
61 int mSamplesPref = 1;
62 float mSamplesQ = 1.f;
Jason Sams704ff642010-02-09 16:05:07 -080063
Jason Sams2222aa92010-10-10 17:58:25 -070064 public SurfaceConfig() {
65 }
66
67 public SurfaceConfig(SurfaceConfig sc) {
68 mDepthMin = sc.mDepthMin;
69 mDepthPref = sc.mDepthPref;
70 mStencilMin = sc.mStencilMin;
71 mStencilPref = sc.mStencilPref;
72 mColorMin = sc.mColorMin;
73 mColorPref = sc.mColorPref;
74 mAlphaMin = sc.mAlphaMin;
75 mAlphaPref = sc.mAlphaPref;
76 mSamplesMin = sc.mSamplesMin;
77 mSamplesPref = sc.mSamplesPref;
78 mSamplesQ = sc.mSamplesQ;
79 }
80
81 private void validateRange(int umin, int upref, int rmin, int rmax) {
82 if (umin < rmin || umin > rmax) {
Jason Samsc1d62102010-11-04 14:32:19 -070083 throw new RSIllegalArgumentException("Minimum value provided out of range.");
Jason Sams2222aa92010-10-10 17:58:25 -070084 }
85 if (upref < umin) {
Jason Samsbf6ef8d2010-12-06 15:59:59 -080086 throw new RSIllegalArgumentException("preferred must be >= Minimum.");
Jason Sams2222aa92010-10-10 17:58:25 -070087 }
88 }
89
Jason Samsbf6ef8d2010-12-06 15:59:59 -080090 /**
91 * Set the per-component bit depth for color (red, green, blue). This
92 * configures the surface for an unsigned integer buffer type.
93 *
94 * @param minimum
95 * @param preferred
96 */
97 public void setColor(int minimum, int preferred) {
98 validateRange(minimum, preferred, 5, 8);
Jason Sams2222aa92010-10-10 17:58:25 -070099 mColorMin = minimum;
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800100 mColorPref = preferred;
Jason Sams2222aa92010-10-10 17:58:25 -0700101 }
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800102
103 /**
104 * Set the bit depth for alpha. This configures the surface for
105 * an unsigned integer buffer type.
106 *
107 * @param minimum
108 * @param preferred
109 */
110 public void setAlpha(int minimum, int preferred) {
111 validateRange(minimum, preferred, 0, 8);
Jason Sams2222aa92010-10-10 17:58:25 -0700112 mAlphaMin = minimum;
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800113 mAlphaPref = preferred;
Jason Sams2222aa92010-10-10 17:58:25 -0700114 }
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800115
116 /**
117 * Set the bit depth for the depth buffer. This configures the
118 * surface for an unsigned integer buffer type. If a minimum of 0
119 * is specified then its possible no depth buffer will be
120 * allocated.
121 *
122 * @param minimum
123 * @param preferred
124 */
125 public void setDepth(int minimum, int preferred) {
126 validateRange(minimum, preferred, 0, 24);
Jason Sams2222aa92010-10-10 17:58:25 -0700127 mDepthMin = minimum;
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800128 mDepthPref = preferred;
Jason Sams2222aa92010-10-10 17:58:25 -0700129 }
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800130
131 /**
132 * Configure the multisample rendering.
133 *
134 * @param minimum The required number of samples, must be at least 1.
135 * @param preferred The targe number of samples, must be at least
136 * minimum
137 * @param Q The quality of samples, range 0-1. Used to decide between
138 * different formats which have the same number of samples but
139 * different rendering quality.
140 */
141 public void setSamples(int minimum, int preferred, float Q) {
142 validateRange(minimum, preferred, 1, 32);
Jason Sams2222aa92010-10-10 17:58:25 -0700143 if (Q < 0.0f || Q > 1.0f) {
Jason Samsc1d62102010-11-04 14:32:19 -0700144 throw new RSIllegalArgumentException("Quality out of 0-1 range.");
Jason Sams2222aa92010-10-10 17:58:25 -0700145 }
146 mSamplesMin = minimum;
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800147 mSamplesPref = preferred;
Jason Sams2222aa92010-10-10 17:58:25 -0700148 mSamplesQ = Q;
149 }
150 };
151
152 SurfaceConfig mSurfaceConfig;
Jason Sams2222aa92010-10-10 17:58:25 -0700153
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800154 /**
155 * Construct a new RenderScriptGL context.
156 *
Shih-wei Liao6b32fab2010-12-10 01:03:59 -0800157 * @param ctx The context.
Stephen Hines8cecbb52011-02-28 18:20:34 -0800158 * @param sc The desired format of the primary rendering surface.
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800159 */
Shih-wei Liao6b32fab2010-12-10 01:03:59 -0800160 public RenderScriptGL(Context ctx, SurfaceConfig sc) {
161 super(ctx);
Jason Sams2222aa92010-10-10 17:58:25 -0700162 mSurfaceConfig = new SurfaceConfig(sc);
163
Jason Sams704ff642010-02-09 16:05:07 -0800164 mSurface = null;
165 mWidth = 0;
166 mHeight = 0;
167 mDev = nDeviceCreate();
Alex Sakhartchouk2c74ad92011-03-16 19:28:25 -0700168 int dpi = ctx.getResources().getDisplayMetrics().densityDpi;
Jason Sams11c8af92010-10-13 15:31:10 -0700169 mContext = nContextCreateGL(mDev, 0,
170 mSurfaceConfig.mColorMin, mSurfaceConfig.mColorPref,
171 mSurfaceConfig.mAlphaMin, mSurfaceConfig.mAlphaPref,
172 mSurfaceConfig.mDepthMin, mSurfaceConfig.mDepthPref,
173 mSurfaceConfig.mStencilMin, mSurfaceConfig.mStencilPref,
174 mSurfaceConfig.mSamplesMin, mSurfaceConfig.mSamplesPref,
Alex Sakhartchouk2c74ad92011-03-16 19:28:25 -0700175 mSurfaceConfig.mSamplesQ, dpi);
Jason Samsd5f06302010-11-03 14:27:11 -0700176 if (mContext == 0) {
177 throw new RSDriverException("Failed to create RS context.");
178 }
Jason Sams704ff642010-02-09 16:05:07 -0800179 mMessageThread = new MessageThread(this);
180 mMessageThread.start();
Jason Sams704ff642010-02-09 16:05:07 -0800181 }
182
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800183 /**
184 * Bind an os surface
185 *
186 *
187 * @param w
188 * @param h
189 * @param sur
190 */
191 public void setSurface(SurfaceHolder sur, int w, int h) {
192 validate();
193 if (sur != null) {
194 mSurface = sur.getSurface();
195 } else {
196 mSurface = null;
197 }
Jason Sams704ff642010-02-09 16:05:07 -0800198 mWidth = w;
199 mHeight = h;
Jason Sams704ff642010-02-09 16:05:07 -0800200 nContextSetSurface(w, h, mSurface);
201 }
202
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800203 /**
204 * return the height of the last set surface.
205 *
206 * @return int
207 */
Jason Sams5585e362010-10-29 10:19:21 -0700208 public int getHeight() {
209 return mHeight;
210 }
211
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800212 /**
213 * return the width of the last set surface.
214 *
215 * @return int
216 */
Jason Sams5585e362010-10-29 10:19:21 -0700217 public int getWidth() {
218 return mWidth;
219 }
Jason Sams704ff642010-02-09 16:05:07 -0800220
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800221 /**
222 * Temporarly halt calls to the root rendering script.
223 *
224 */
225 public void pause() {
Jason Sams704ff642010-02-09 16:05:07 -0800226 validate();
227 nContextPause();
228 }
229
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800230 /**
231 * Resume calls to the root rendering script.
232 *
233 */
234 public void resume() {
Jason Sams704ff642010-02-09 16:05:07 -0800235 validate();
236 nContextResume();
237 }
238
239
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800240 /**
241 * Set the script to handle calls to render the primary surface.
242 *
243 * @param s Graphics script to process rendering requests.
244 */
245 public void bindRootScript(Script s) {
Jason Sams704ff642010-02-09 16:05:07 -0800246 validate();
247 nContextBindRootScript(safeID(s));
248 }
249
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800250 /**
251 * Set the default ProgramStore object seen as the parent state by the root
252 * rendering script.
253 *
254 * @param p
255 */
256 public void bindProgramStore(ProgramStore p) {
Jason Sams704ff642010-02-09 16:05:07 -0800257 validate();
Jason Sams54db59c2010-05-13 18:30:11 -0700258 nContextBindProgramStore(safeID(p));
Jason Sams704ff642010-02-09 16:05:07 -0800259 }
260
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800261 /**
262 * Set the default ProgramFragment object seen as the parent state by the
263 * root rendering script.
264 *
265 * @param p
266 */
267 public void bindProgramFragment(ProgramFragment p) {
Jason Sams704ff642010-02-09 16:05:07 -0800268 validate();
269 nContextBindProgramFragment(safeID(p));
270 }
271
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800272 /**
273 * Set the default ProgramRaster object seen as the parent state by the
274 * root rendering script.
275 *
276 * @param p
277 */
278 public void bindProgramRaster(ProgramRaster p) {
Jason Sams704ff642010-02-09 16:05:07 -0800279 validate();
280 nContextBindProgramRaster(safeID(p));
281 }
282
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800283 /**
284 * Set the default ProgramVertex object seen as the parent state by the
285 * root rendering script.
286 *
287 * @param p
288 */
289 public void bindProgramVertex(ProgramVertex p) {
Jason Sams704ff642010-02-09 16:05:07 -0800290 validate();
291 nContextBindProgramVertex(safeID(p));
292 }
293
Jason Sams704ff642010-02-09 16:05:07 -0800294}