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