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