| Jason Sams | 704ff64 | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package android.renderscript; |
| 18 | |
| 19 | import java.lang.reflect.Field; |
| 20 | |
| Shih-wei Liao | 6b32fab | 2010-12-10 01:03:59 -0800 | [diff] [blame] | 21 | import android.content.Context; |
| Jason Sams | 11c8af9 | 2010-10-13 15:31:10 -0700 | [diff] [blame] | 22 | import android.graphics.PixelFormat; |
| Jason Sams | 704ff64 | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 23 | import android.graphics.Bitmap; |
| 24 | import android.graphics.BitmapFactory; |
| 25 | import android.util.Config; |
| 26 | import android.util.Log; |
| 27 | import android.view.Surface; |
| Jason Sams | 2222aa9 | 2010-10-10 17:58:25 -0700 | [diff] [blame] | 28 | import android.view.SurfaceHolder; |
| 29 | import android.view.SurfaceView; |
| Jason Sams | 704ff64 | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 30 | |
| 31 | /** |
| 32 | * @hide |
| 33 | * |
| Jason Sams | 27676fe | 2010-11-10 17:00:59 -0800 | [diff] [blame] | 34 | * The Graphics derivitive of RenderScript. Extends the basic context to add a |
| 35 | * 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. |
| Jason Sams | 704ff64 | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 39 | **/ |
| 40 | public class RenderScriptGL extends RenderScript { |
| 41 | private Surface mSurface; |
| 42 | int mWidth; |
| 43 | int mHeight; |
| 44 | |
| Jason Sams | 27676fe | 2010-11-10 17:00:59 -0800 | [diff] [blame] | 45 | /** |
| 46 | * Class which is used to describe a pixel format for a graphical buffer. |
| 47 | * This is used to describe the intended format of the display surface. |
| 48 | * |
| Jason Sams | bf6ef8d7 | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 49 | * The configuration is described by pairs of minimum and preferred bit |
| 50 | * depths for each component within the config and additional structural |
| 51 | * information. |
| Jason Sams | 27676fe | 2010-11-10 17:00:59 -0800 | [diff] [blame] | 52 | */ |
| Jason Sams | 2222aa9 | 2010-10-10 17:58:25 -0700 | [diff] [blame] | 53 | public static class SurfaceConfig { |
| 54 | int mDepthMin = 0; |
| 55 | int mDepthPref = 0; |
| 56 | int mStencilMin = 0; |
| 57 | int mStencilPref = 0; |
| 58 | int mColorMin = 8; |
| 59 | int mColorPref = 8; |
| 60 | int mAlphaMin = 0; |
| 61 | int mAlphaPref = 0; |
| 62 | int mSamplesMin = 1; |
| 63 | int mSamplesPref = 1; |
| 64 | float mSamplesQ = 1.f; |
| Jason Sams | 704ff64 | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 65 | |
| Jason Sams | 2222aa9 | 2010-10-10 17:58:25 -0700 | [diff] [blame] | 66 | public SurfaceConfig() { |
| 67 | } |
| 68 | |
| 69 | public SurfaceConfig(SurfaceConfig sc) { |
| 70 | mDepthMin = sc.mDepthMin; |
| 71 | mDepthPref = sc.mDepthPref; |
| 72 | mStencilMin = sc.mStencilMin; |
| 73 | mStencilPref = sc.mStencilPref; |
| 74 | mColorMin = sc.mColorMin; |
| 75 | mColorPref = sc.mColorPref; |
| 76 | mAlphaMin = sc.mAlphaMin; |
| 77 | mAlphaPref = sc.mAlphaPref; |
| 78 | mSamplesMin = sc.mSamplesMin; |
| 79 | mSamplesPref = sc.mSamplesPref; |
| 80 | mSamplesQ = sc.mSamplesQ; |
| 81 | } |
| 82 | |
| 83 | private void validateRange(int umin, int upref, int rmin, int rmax) { |
| 84 | if (umin < rmin || umin > rmax) { |
| Jason Sams | c1d6210 | 2010-11-04 14:32:19 -0700 | [diff] [blame] | 85 | throw new RSIllegalArgumentException("Minimum value provided out of range."); |
| Jason Sams | 2222aa9 | 2010-10-10 17:58:25 -0700 | [diff] [blame] | 86 | } |
| 87 | if (upref < umin) { |
| Jason Sams | bf6ef8d7 | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 88 | throw new RSIllegalArgumentException("preferred must be >= Minimum."); |
| Jason Sams | 2222aa9 | 2010-10-10 17:58:25 -0700 | [diff] [blame] | 89 | } |
| 90 | } |
| 91 | |
| Jason Sams | bf6ef8d7 | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 92 | /** |
| 93 | * Set the per-component bit depth for color (red, green, blue). This |
| 94 | * configures the surface for an unsigned integer buffer type. |
| 95 | * |
| 96 | * @param minimum |
| 97 | * @param preferred |
| 98 | */ |
| 99 | public void setColor(int minimum, int preferred) { |
| 100 | validateRange(minimum, preferred, 5, 8); |
| Jason Sams | 2222aa9 | 2010-10-10 17:58:25 -0700 | [diff] [blame] | 101 | mColorMin = minimum; |
| Jason Sams | bf6ef8d7 | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 102 | mColorPref = preferred; |
| Jason Sams | 2222aa9 | 2010-10-10 17:58:25 -0700 | [diff] [blame] | 103 | } |
| Jason Sams | bf6ef8d7 | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 104 | |
| 105 | /** |
| 106 | * Set the bit depth for alpha. This configures the surface for |
| 107 | * an unsigned integer buffer type. |
| 108 | * |
| 109 | * @param minimum |
| 110 | * @param preferred |
| 111 | */ |
| 112 | public void setAlpha(int minimum, int preferred) { |
| 113 | validateRange(minimum, preferred, 0, 8); |
| Jason Sams | 2222aa9 | 2010-10-10 17:58:25 -0700 | [diff] [blame] | 114 | mAlphaMin = minimum; |
| Jason Sams | bf6ef8d7 | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 115 | mAlphaPref = preferred; |
| Jason Sams | 2222aa9 | 2010-10-10 17:58:25 -0700 | [diff] [blame] | 116 | } |
| Jason Sams | bf6ef8d7 | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 117 | |
| 118 | /** |
| 119 | * Set the bit depth for the depth buffer. This configures the |
| 120 | * surface for an unsigned integer buffer type. If a minimum of 0 |
| 121 | * is specified then its possible no depth buffer will be |
| 122 | * allocated. |
| 123 | * |
| 124 | * @param minimum |
| 125 | * @param preferred |
| 126 | */ |
| 127 | public void setDepth(int minimum, int preferred) { |
| 128 | validateRange(minimum, preferred, 0, 24); |
| Jason Sams | 2222aa9 | 2010-10-10 17:58:25 -0700 | [diff] [blame] | 129 | mDepthMin = minimum; |
| Jason Sams | bf6ef8d7 | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 130 | mDepthPref = preferred; |
| Jason Sams | 2222aa9 | 2010-10-10 17:58:25 -0700 | [diff] [blame] | 131 | } |
| Jason Sams | bf6ef8d7 | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 132 | |
| 133 | /** |
| 134 | * Configure the multisample rendering. |
| 135 | * |
| 136 | * @param minimum The required number of samples, must be at least 1. |
| 137 | * @param preferred The targe number of samples, must be at least |
| 138 | * minimum |
| 139 | * @param Q The quality of samples, range 0-1. Used to decide between |
| 140 | * different formats which have the same number of samples but |
| 141 | * different rendering quality. |
| 142 | */ |
| 143 | public void setSamples(int minimum, int preferred, float Q) { |
| 144 | validateRange(minimum, preferred, 1, 32); |
| Jason Sams | 2222aa9 | 2010-10-10 17:58:25 -0700 | [diff] [blame] | 145 | if (Q < 0.0f || Q > 1.0f) { |
| Jason Sams | c1d6210 | 2010-11-04 14:32:19 -0700 | [diff] [blame] | 146 | throw new RSIllegalArgumentException("Quality out of 0-1 range."); |
| Jason Sams | 2222aa9 | 2010-10-10 17:58:25 -0700 | [diff] [blame] | 147 | } |
| 148 | mSamplesMin = minimum; |
| Jason Sams | bf6ef8d7 | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 149 | mSamplesPref = preferred; |
| Jason Sams | 2222aa9 | 2010-10-10 17:58:25 -0700 | [diff] [blame] | 150 | mSamplesQ = Q; |
| 151 | } |
| 152 | }; |
| 153 | |
| 154 | SurfaceConfig mSurfaceConfig; |
| Jason Sams | bf6ef8d7 | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 155 | /* |
| 156 | // Keep? |
| Jason Sams | 2222aa9 | 2010-10-10 17:58:25 -0700 | [diff] [blame] | 157 | public void configureSurface(SurfaceHolder sh) { |
| Jason Sams | 11c8af9 | 2010-10-13 15:31:10 -0700 | [diff] [blame] | 158 | if (mSurfaceConfig.mAlphaMin > 1) { |
| 159 | sh.setFormat(PixelFormat.RGBA_8888); |
| 160 | } else { |
| 161 | sh.setFormat(PixelFormat.RGBX_8888); |
| 162 | } |
| Jason Sams | 2222aa9 | 2010-10-10 17:58:25 -0700 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | public void checkSurface(SurfaceHolder sh) { |
| 166 | } |
| Jason Sams | bf6ef8d7 | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 167 | */ |
| Jason Sams | 2222aa9 | 2010-10-10 17:58:25 -0700 | [diff] [blame] | 168 | |
| Jason Sams | bf6ef8d7 | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 169 | /** |
| 170 | * Construct a new RenderScriptGL context. |
| 171 | * |
| Shih-wei Liao | 6b32fab | 2010-12-10 01:03:59 -0800 | [diff] [blame] | 172 | * @param ctx The context. |
| Jason Sams | bf6ef8d7 | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 173 | * @param sc The desired format of the primart rendering surface. |
| 174 | */ |
| Shih-wei Liao | 6b32fab | 2010-12-10 01:03:59 -0800 | [diff] [blame] | 175 | public RenderScriptGL(Context ctx, SurfaceConfig sc) { |
| 176 | super(ctx); |
| Jason Sams | 2222aa9 | 2010-10-10 17:58:25 -0700 | [diff] [blame] | 177 | mSurfaceConfig = new SurfaceConfig(sc); |
| 178 | |
| Jason Sams | 704ff64 | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 179 | mSurface = null; |
| 180 | mWidth = 0; |
| 181 | mHeight = 0; |
| 182 | mDev = nDeviceCreate(); |
| Jason Sams | 11c8af9 | 2010-10-13 15:31:10 -0700 | [diff] [blame] | 183 | mContext = nContextCreateGL(mDev, 0, |
| 184 | mSurfaceConfig.mColorMin, mSurfaceConfig.mColorPref, |
| 185 | mSurfaceConfig.mAlphaMin, mSurfaceConfig.mAlphaPref, |
| 186 | mSurfaceConfig.mDepthMin, mSurfaceConfig.mDepthPref, |
| 187 | mSurfaceConfig.mStencilMin, mSurfaceConfig.mStencilPref, |
| 188 | mSurfaceConfig.mSamplesMin, mSurfaceConfig.mSamplesPref, |
| 189 | mSurfaceConfig.mSamplesQ); |
| Jason Sams | d5f0630 | 2010-11-03 14:27:11 -0700 | [diff] [blame] | 190 | if (mContext == 0) { |
| 191 | throw new RSDriverException("Failed to create RS context."); |
| 192 | } |
| Jason Sams | 704ff64 | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 193 | mMessageThread = new MessageThread(this); |
| 194 | mMessageThread.start(); |
| Jason Sams | 704ff64 | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 195 | } |
| 196 | |
| Jason Sams | bf6ef8d7 | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 197 | /** |
| 198 | * Bind an os surface |
| 199 | * |
| 200 | * |
| 201 | * @param w |
| 202 | * @param h |
| 203 | * @param sur |
| 204 | */ |
| 205 | public void setSurface(SurfaceHolder sur, int w, int h) { |
| 206 | validate(); |
| 207 | if (sur != null) { |
| 208 | mSurface = sur.getSurface(); |
| 209 | } else { |
| 210 | mSurface = null; |
| 211 | } |
| Jason Sams | 704ff64 | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 212 | mWidth = w; |
| 213 | mHeight = h; |
| Jason Sams | 704ff64 | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 214 | nContextSetSurface(w, h, mSurface); |
| 215 | } |
| 216 | |
| Jason Sams | bf6ef8d7 | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 217 | /** |
| 218 | * return the height of the last set surface. |
| 219 | * |
| 220 | * @return int |
| 221 | */ |
| Jason Sams | 5585e36 | 2010-10-29 10:19:21 -0700 | [diff] [blame] | 222 | public int getHeight() { |
| 223 | return mHeight; |
| 224 | } |
| 225 | |
| Jason Sams | bf6ef8d7 | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 226 | /** |
| 227 | * return the width of the last set surface. |
| 228 | * |
| 229 | * @return int |
| 230 | */ |
| Jason Sams | 5585e36 | 2010-10-29 10:19:21 -0700 | [diff] [blame] | 231 | public int getWidth() { |
| 232 | return mWidth; |
| 233 | } |
| Jason Sams | 704ff64 | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 234 | |
| Jason Sams | bf6ef8d7 | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 235 | /** |
| 236 | * Temporarly halt calls to the root rendering script. |
| 237 | * |
| 238 | */ |
| 239 | public void pause() { |
| Jason Sams | 704ff64 | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 240 | validate(); |
| 241 | nContextPause(); |
| 242 | } |
| 243 | |
| Jason Sams | bf6ef8d7 | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 244 | /** |
| 245 | * Resume calls to the root rendering script. |
| 246 | * |
| 247 | */ |
| 248 | public void resume() { |
| Jason Sams | 704ff64 | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 249 | validate(); |
| 250 | nContextResume(); |
| 251 | } |
| 252 | |
| 253 | |
| Jason Sams | bf6ef8d7 | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 254 | /** |
| 255 | * Set the script to handle calls to render the primary surface. |
| 256 | * |
| 257 | * @param s Graphics script to process rendering requests. |
| 258 | */ |
| 259 | public void bindRootScript(Script s) { |
| Jason Sams | 704ff64 | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 260 | validate(); |
| 261 | nContextBindRootScript(safeID(s)); |
| 262 | } |
| 263 | |
| Jason Sams | bf6ef8d7 | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 264 | /** |
| 265 | * Set the default ProgramStore object seen as the parent state by the root |
| 266 | * rendering script. |
| 267 | * |
| 268 | * @param p |
| 269 | */ |
| 270 | public void bindProgramStore(ProgramStore p) { |
| Jason Sams | 704ff64 | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 271 | validate(); |
| Jason Sams | 54db59c | 2010-05-13 18:30:11 -0700 | [diff] [blame] | 272 | nContextBindProgramStore(safeID(p)); |
| Jason Sams | 704ff64 | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 273 | } |
| 274 | |
| Jason Sams | bf6ef8d7 | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 275 | /** |
| 276 | * Set the default ProgramFragment object seen as the parent state by the |
| 277 | * root rendering script. |
| 278 | * |
| 279 | * @param p |
| 280 | */ |
| 281 | public void bindProgramFragment(ProgramFragment p) { |
| Jason Sams | 704ff64 | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 282 | validate(); |
| 283 | nContextBindProgramFragment(safeID(p)); |
| 284 | } |
| 285 | |
| Jason Sams | bf6ef8d7 | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 286 | /** |
| 287 | * Set the default ProgramRaster object seen as the parent state by the |
| 288 | * root rendering script. |
| 289 | * |
| 290 | * @param p |
| 291 | */ |
| 292 | public void bindProgramRaster(ProgramRaster p) { |
| Jason Sams | 704ff64 | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 293 | validate(); |
| 294 | nContextBindProgramRaster(safeID(p)); |
| 295 | } |
| 296 | |
| Jason Sams | bf6ef8d7 | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 297 | /** |
| 298 | * Set the default ProgramVertex object seen as the parent state by the |
| 299 | * root rendering script. |
| 300 | * |
| 301 | * @param p |
| 302 | */ |
| 303 | public void bindProgramVertex(ProgramVertex p) { |
| Jason Sams | 704ff64 | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 304 | validate(); |
| 305 | nContextBindProgramVertex(safeID(p)); |
| 306 | } |
| 307 | |
| Jason Sams | 704ff64 | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 308 | } |