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; |
Jason Sams | faa32b3 | 2011-06-20 16:58:04 -0700 | [diff] [blame] | 25 | import android.graphics.SurfaceTexture; |
Jason Sams | 704ff64 | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 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 | /** |
Jason Sams | 27676fe | 2010-11-10 17:00:59 -0800 | [diff] [blame] | 32 | * 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 Sams | 704ff64 | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 37 | **/ |
| 38 | public class RenderScriptGL extends RenderScript { |
Jason Sams | 704ff64 | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 39 | int mWidth; |
| 40 | int mHeight; |
| 41 | |
Jason Sams | 27676fe | 2010-11-10 17:00:59 -0800 | [diff] [blame] | 42 | /** |
| 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 Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 46 | * 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 Sams | 27676fe | 2010-11-10 17:00:59 -0800 | [diff] [blame] | 49 | */ |
Jason Sams | 2222aa9 | 2010-10-10 17:58:25 -0700 | [diff] [blame] | 50 | 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 Sams | 704ff64 | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 62 | |
Jason Sams | 2222aa9 | 2010-10-10 17:58:25 -0700 | [diff] [blame] | 63 | 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 Sams | c1d6210 | 2010-11-04 14:32:19 -0700 | [diff] [blame] | 82 | throw new RSIllegalArgumentException("Minimum value provided out of range."); |
Jason Sams | 2222aa9 | 2010-10-10 17:58:25 -0700 | [diff] [blame] | 83 | } |
| 84 | if (upref < umin) { |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 85 | throw new RSIllegalArgumentException("preferred must be >= Minimum."); |
Jason Sams | 2222aa9 | 2010-10-10 17:58:25 -0700 | [diff] [blame] | 86 | } |
| 87 | } |
| 88 | |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 89 | /** |
| 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 Sams | 2222aa9 | 2010-10-10 17:58:25 -0700 | [diff] [blame] | 98 | mColorMin = minimum; |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 99 | mColorPref = preferred; |
Jason Sams | 2222aa9 | 2010-10-10 17:58:25 -0700 | [diff] [blame] | 100 | } |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 101 | |
| 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 Sams | 2222aa9 | 2010-10-10 17:58:25 -0700 | [diff] [blame] | 111 | mAlphaMin = minimum; |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 112 | mAlphaPref = preferred; |
Jason Sams | 2222aa9 | 2010-10-10 17:58:25 -0700 | [diff] [blame] | 113 | } |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 114 | |
| 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 Sams | 2222aa9 | 2010-10-10 17:58:25 -0700 | [diff] [blame] | 126 | mDepthMin = minimum; |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 127 | mDepthPref = preferred; |
Jason Sams | 2222aa9 | 2010-10-10 17:58:25 -0700 | [diff] [blame] | 128 | } |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 129 | |
| 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 Sams | 2222aa9 | 2010-10-10 17:58:25 -0700 | [diff] [blame] | 142 | if (Q < 0.0f || Q > 1.0f) { |
Jason Sams | c1d6210 | 2010-11-04 14:32:19 -0700 | [diff] [blame] | 143 | throw new RSIllegalArgumentException("Quality out of 0-1 range."); |
Jason Sams | 2222aa9 | 2010-10-10 17:58:25 -0700 | [diff] [blame] | 144 | } |
| 145 | mSamplesMin = minimum; |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 146 | mSamplesPref = preferred; |
Jason Sams | 2222aa9 | 2010-10-10 17:58:25 -0700 | [diff] [blame] | 147 | mSamplesQ = Q; |
| 148 | } |
| 149 | }; |
| 150 | |
| 151 | SurfaceConfig mSurfaceConfig; |
Jason Sams | 2222aa9 | 2010-10-10 17:58:25 -0700 | [diff] [blame] | 152 | |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 153 | /** |
| 154 | * Construct a new RenderScriptGL context. |
| 155 | * |
Shih-wei Liao | 6b32fab | 2010-12-10 01:03:59 -0800 | [diff] [blame] | 156 | * @param ctx The context. |
Stephen Hines | 8cecbb5 | 2011-02-28 18:20:34 -0800 | [diff] [blame] | 157 | * @param sc The desired format of the primary rendering surface. |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 158 | */ |
Shih-wei Liao | 6b32fab | 2010-12-10 01:03:59 -0800 | [diff] [blame] | 159 | public RenderScriptGL(Context ctx, SurfaceConfig sc) { |
| 160 | super(ctx); |
Jason Sams | 2222aa9 | 2010-10-10 17:58:25 -0700 | [diff] [blame] | 161 | mSurfaceConfig = new SurfaceConfig(sc); |
| 162 | |
Stephen Hines | 4382467a | 2011-08-01 15:02:34 -0700 | [diff] [blame] | 163 | int sdkVersion = getTargetSdkVersion(ctx); |
| 164 | |
Jason Sams | 704ff64 | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 165 | mWidth = 0; |
| 166 | mHeight = 0; |
| 167 | mDev = nDeviceCreate(); |
Alex Sakhartchouk | 2c74ad9 | 2011-03-16 19:28:25 -0700 | [diff] [blame] | 168 | int dpi = ctx.getResources().getDisplayMetrics().densityDpi; |
Stephen Hines | 4382467a | 2011-08-01 15:02:34 -0700 | [diff] [blame] | 169 | mContext = nContextCreateGL(mDev, 0, sdkVersion, |
Jason Sams | 11c8af9 | 2010-10-13 15:31:10 -0700 | [diff] [blame] | 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 Sakhartchouk | 2c74ad9 | 2011-03-16 19:28:25 -0700 | [diff] [blame] | 175 | mSurfaceConfig.mSamplesQ, dpi); |
Jason Sams | d5f0630 | 2010-11-03 14:27:11 -0700 | [diff] [blame] | 176 | if (mContext == 0) { |
| 177 | throw new RSDriverException("Failed to create RS context."); |
| 178 | } |
Jason Sams | 704ff64 | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 179 | mMessageThread = new MessageThread(this); |
| 180 | mMessageThread.start(); |
Jason Sams | 704ff64 | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 181 | } |
| 182 | |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 183 | /** |
| 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(); |
Jason Sams | faa32b3 | 2011-06-20 16:58:04 -0700 | [diff] [blame] | 193 | Surface s = null; |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 194 | if (sur != null) { |
Jason Sams | faa32b3 | 2011-06-20 16:58:04 -0700 | [diff] [blame] | 195 | s = sur.getSurface(); |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 196 | } |
Jason Sams | 704ff64 | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 197 | mWidth = w; |
| 198 | mHeight = h; |
Jason Sams | faa32b3 | 2011-06-20 16:58:04 -0700 | [diff] [blame] | 199 | nContextSetSurface(w, h, s); |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * Bind an os surface |
| 204 | * |
Jason Sams | faa32b3 | 2011-06-20 16:58:04 -0700 | [diff] [blame] | 205 | * @param w |
| 206 | * @param h |
| 207 | * @param sur |
| 208 | */ |
| 209 | public void setSurfaceTexture(SurfaceTexture sur, int w, int h) { |
| 210 | validate(); |
| 211 | //android.util.Log.v("rs", "set surface " + sur + " w=" + w + ", h=" + h); |
| 212 | |
| 213 | mWidth = w; |
| 214 | mHeight = h; |
| 215 | nContextSetSurfaceTexture(w, h, sur); |
Jason Sams | 704ff64 | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 216 | } |
| 217 | |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 218 | /** |
| 219 | * return the height of the last set surface. |
| 220 | * |
| 221 | * @return int |
| 222 | */ |
Jason Sams | 5585e36 | 2010-10-29 10:19:21 -0700 | [diff] [blame] | 223 | public int getHeight() { |
| 224 | return mHeight; |
| 225 | } |
| 226 | |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 227 | /** |
| 228 | * return the width of the last set surface. |
| 229 | * |
| 230 | * @return int |
| 231 | */ |
Jason Sams | 5585e36 | 2010-10-29 10:19:21 -0700 | [diff] [blame] | 232 | public int getWidth() { |
| 233 | return mWidth; |
| 234 | } |
Jason Sams | 704ff64 | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 235 | |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 236 | /** |
| 237 | * Temporarly halt calls to the root rendering script. |
| 238 | * |
| 239 | */ |
| 240 | public void pause() { |
Jason Sams | 704ff64 | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 241 | validate(); |
| 242 | nContextPause(); |
| 243 | } |
| 244 | |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 245 | /** |
| 246 | * Resume calls to the root rendering script. |
| 247 | * |
| 248 | */ |
| 249 | public void resume() { |
Jason Sams | 704ff64 | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 250 | validate(); |
| 251 | nContextResume(); |
| 252 | } |
| 253 | |
| 254 | |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 255 | /** |
| 256 | * Set the script to handle calls to render the primary surface. |
| 257 | * |
| 258 | * @param s Graphics script to process rendering requests. |
| 259 | */ |
| 260 | public void bindRootScript(Script s) { |
Jason Sams | 704ff64 | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 261 | validate(); |
| 262 | nContextBindRootScript(safeID(s)); |
| 263 | } |
| 264 | |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 265 | /** |
| 266 | * Set the default ProgramStore object seen as the parent state by the root |
| 267 | * rendering script. |
| 268 | * |
| 269 | * @param p |
| 270 | */ |
| 271 | public void bindProgramStore(ProgramStore p) { |
Jason Sams | 704ff64 | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 272 | validate(); |
Jason Sams | 54db59c | 2010-05-13 18:30:11 -0700 | [diff] [blame] | 273 | nContextBindProgramStore(safeID(p)); |
Jason Sams | 704ff64 | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 274 | } |
| 275 | |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 276 | /** |
| 277 | * Set the default ProgramFragment object seen as the parent state by the |
| 278 | * root rendering script. |
| 279 | * |
| 280 | * @param p |
| 281 | */ |
| 282 | public void bindProgramFragment(ProgramFragment p) { |
Jason Sams | 704ff64 | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 283 | validate(); |
| 284 | nContextBindProgramFragment(safeID(p)); |
| 285 | } |
| 286 | |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 287 | /** |
| 288 | * Set the default ProgramRaster object seen as the parent state by the |
| 289 | * root rendering script. |
| 290 | * |
| 291 | * @param p |
| 292 | */ |
| 293 | public void bindProgramRaster(ProgramRaster p) { |
Jason Sams | 704ff64 | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 294 | validate(); |
| 295 | nContextBindProgramRaster(safeID(p)); |
| 296 | } |
| 297 | |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 298 | /** |
| 299 | * Set the default ProgramVertex object seen as the parent state by the |
| 300 | * root rendering script. |
| 301 | * |
| 302 | * @param p |
| 303 | */ |
| 304 | public void bindProgramVertex(ProgramVertex p) { |
Jason Sams | 704ff64 | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 305 | validate(); |
| 306 | nContextBindProgramVertex(safeID(p)); |
| 307 | } |
| 308 | |
Jason Sams | 704ff64 | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 309 | } |