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 | /** |
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 { |
| 39 | private Surface mSurface; |
| 40 | int mWidth; |
| 41 | int mHeight; |
| 42 | |
Jason Sams | 27676fe | 2010-11-10 17:00:59 -0800 | [diff] [blame] | 43 | /** |
| 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 Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 47 | * 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 Sams | 27676fe | 2010-11-10 17:00:59 -0800 | [diff] [blame] | 50 | */ |
Jason Sams | 2222aa9 | 2010-10-10 17:58:25 -0700 | [diff] [blame] | 51 | 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 Sams | 704ff64 | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 63 | |
Jason Sams | 2222aa9 | 2010-10-10 17:58:25 -0700 | [diff] [blame] | 64 | 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 Sams | c1d6210 | 2010-11-04 14:32:19 -0700 | [diff] [blame] | 83 | throw new RSIllegalArgumentException("Minimum value provided out of range."); |
Jason Sams | 2222aa9 | 2010-10-10 17:58:25 -0700 | [diff] [blame] | 84 | } |
| 85 | if (upref < umin) { |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 86 | throw new RSIllegalArgumentException("preferred must be >= Minimum."); |
Jason Sams | 2222aa9 | 2010-10-10 17:58:25 -0700 | [diff] [blame] | 87 | } |
| 88 | } |
| 89 | |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 90 | /** |
| 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 Sams | 2222aa9 | 2010-10-10 17:58:25 -0700 | [diff] [blame] | 99 | mColorMin = minimum; |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 100 | mColorPref = preferred; |
Jason Sams | 2222aa9 | 2010-10-10 17:58:25 -0700 | [diff] [blame] | 101 | } |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 102 | |
| 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 Sams | 2222aa9 | 2010-10-10 17:58:25 -0700 | [diff] [blame] | 112 | mAlphaMin = minimum; |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 113 | mAlphaPref = preferred; |
Jason Sams | 2222aa9 | 2010-10-10 17:58:25 -0700 | [diff] [blame] | 114 | } |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 115 | |
| 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 Sams | 2222aa9 | 2010-10-10 17:58:25 -0700 | [diff] [blame] | 127 | mDepthMin = minimum; |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 128 | mDepthPref = preferred; |
Jason Sams | 2222aa9 | 2010-10-10 17:58:25 -0700 | [diff] [blame] | 129 | } |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 130 | |
| 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 Sams | 2222aa9 | 2010-10-10 17:58:25 -0700 | [diff] [blame] | 143 | if (Q < 0.0f || Q > 1.0f) { |
Jason Sams | c1d6210 | 2010-11-04 14:32:19 -0700 | [diff] [blame] | 144 | throw new RSIllegalArgumentException("Quality out of 0-1 range."); |
Jason Sams | 2222aa9 | 2010-10-10 17:58:25 -0700 | [diff] [blame] | 145 | } |
| 146 | mSamplesMin = minimum; |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 147 | mSamplesPref = preferred; |
Jason Sams | 2222aa9 | 2010-10-10 17:58:25 -0700 | [diff] [blame] | 148 | mSamplesQ = Q; |
| 149 | } |
| 150 | }; |
| 151 | |
| 152 | SurfaceConfig mSurfaceConfig; |
Jason Sams | 2222aa9 | 2010-10-10 17:58:25 -0700 | [diff] [blame] | 153 | |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 154 | /** |
| 155 | * Construct a new RenderScriptGL context. |
| 156 | * |
Shih-wei Liao | 6b32fab | 2010-12-10 01:03:59 -0800 | [diff] [blame] | 157 | * @param ctx The context. |
Stephen Hines | 8cecbb5 | 2011-02-28 18:20:34 -0800 | [diff] [blame] | 158 | * @param sc The desired format of the primary rendering surface. |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 159 | */ |
Shih-wei Liao | 6b32fab | 2010-12-10 01:03:59 -0800 | [diff] [blame] | 160 | public RenderScriptGL(Context ctx, SurfaceConfig sc) { |
| 161 | super(ctx); |
Jason Sams | 2222aa9 | 2010-10-10 17:58:25 -0700 | [diff] [blame] | 162 | mSurfaceConfig = new SurfaceConfig(sc); |
| 163 | |
Jason Sams | 704ff64 | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 164 | mSurface = null; |
| 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; |
Jason Sams | 11c8af9 | 2010-10-13 15:31:10 -0700 | [diff] [blame] | 169 | 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 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(); |
| 193 | if (sur != null) { |
| 194 | mSurface = sur.getSurface(); |
| 195 | } else { |
| 196 | mSurface = null; |
| 197 | } |
Jason Sams | 704ff64 | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 198 | mWidth = w; |
| 199 | mHeight = h; |
Jason Sams | 704ff64 | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 200 | nContextSetSurface(w, h, mSurface); |
| 201 | } |
| 202 | |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 203 | /** |
| 204 | * return the height of the last set surface. |
| 205 | * |
| 206 | * @return int |
| 207 | */ |
Jason Sams | 5585e36 | 2010-10-29 10:19:21 -0700 | [diff] [blame] | 208 | public int getHeight() { |
| 209 | return mHeight; |
| 210 | } |
| 211 | |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 212 | /** |
| 213 | * return the width of the last set surface. |
| 214 | * |
| 215 | * @return int |
| 216 | */ |
Jason Sams | 5585e36 | 2010-10-29 10:19:21 -0700 | [diff] [blame] | 217 | public int getWidth() { |
| 218 | return mWidth; |
| 219 | } |
Jason Sams | 704ff64 | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 220 | |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 221 | /** |
| 222 | * Temporarly halt calls to the root rendering script. |
| 223 | * |
| 224 | */ |
| 225 | public void pause() { |
Jason Sams | 704ff64 | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 226 | validate(); |
| 227 | nContextPause(); |
| 228 | } |
| 229 | |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 230 | /** |
| 231 | * Resume calls to the root rendering script. |
| 232 | * |
| 233 | */ |
| 234 | public void resume() { |
Jason Sams | 704ff64 | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 235 | validate(); |
| 236 | nContextResume(); |
| 237 | } |
| 238 | |
| 239 | |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 240 | /** |
| 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 Sams | 704ff64 | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 246 | validate(); |
| 247 | nContextBindRootScript(safeID(s)); |
| 248 | } |
| 249 | |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 250 | /** |
| 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 Sams | 704ff64 | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 257 | validate(); |
Jason Sams | 54db59c | 2010-05-13 18:30:11 -0700 | [diff] [blame] | 258 | nContextBindProgramStore(safeID(p)); |
Jason Sams | 704ff64 | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 259 | } |
| 260 | |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 261 | /** |
| 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 Sams | 704ff64 | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 268 | validate(); |
| 269 | nContextBindProgramFragment(safeID(p)); |
| 270 | } |
| 271 | |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 272 | /** |
| 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 Sams | 704ff64 | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 279 | validate(); |
| 280 | nContextBindProgramRaster(safeID(p)); |
| 281 | } |
| 282 | |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 283 | /** |
| 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 Sams | 704ff64 | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 290 | validate(); |
| 291 | nContextBindProgramVertex(safeID(p)); |
| 292 | } |
| 293 | |
Jason Sams | 704ff64 | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 294 | } |