RenderScriptGL.java revision 4ef6650bd05a39a09958ea1db92f120ea4949cb1
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 17package android.renderscript; 18 19import java.lang.reflect.Field; 20 21import android.graphics.PixelFormat; 22import android.graphics.Bitmap; 23import android.graphics.BitmapFactory; 24import android.util.Config; 25import android.util.Log; 26import android.view.Surface; 27import android.view.SurfaceHolder; 28import android.view.SurfaceView; 29 30/** 31 * @hide 32 * 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. 38 **/ 39public class RenderScriptGL extends RenderScript { 40 private Surface mSurface; 41 int mWidth; 42 int mHeight; 43 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 * 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. 51 */ 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; 64 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) { 84 throw new RSIllegalArgumentException("Minimum value provided out of range."); 85 } 86 if (upref < umin) { 87 throw new RSIllegalArgumentException("preferred must be >= Minimum."); 88 } 89 } 90 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); 100 mColorMin = minimum; 101 mColorPref = preferred; 102 } 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); 113 mAlphaMin = minimum; 114 mAlphaPref = preferred; 115 } 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); 128 mDepthMin = minimum; 129 mDepthPref = preferred; 130 } 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); 144 if (Q < 0.0f || Q > 1.0f) { 145 throw new RSIllegalArgumentException("Quality out of 0-1 range."); 146 } 147 mSamplesMin = minimum; 148 mSamplesPref = preferred; 149 mSamplesQ = Q; 150 } 151 }; 152 153 SurfaceConfig mSurfaceConfig; 154/* 155 // Keep? 156 public void configureSurface(SurfaceHolder sh) { 157 if (mSurfaceConfig.mAlphaMin > 1) { 158 sh.setFormat(PixelFormat.RGBA_8888); 159 } else { 160 sh.setFormat(PixelFormat.RGBX_8888); 161 } 162 } 163 164 public void checkSurface(SurfaceHolder sh) { 165 } 166*/ 167 168 /** 169 * Construct a new RenderScriptGL context. 170 * 171 * 172 * @param sc The desired format of the primart rendering surface. 173 */ 174 public RenderScriptGL(SurfaceConfig sc) { 175 mSurfaceConfig = new SurfaceConfig(sc); 176 177 mSurface = null; 178 mWidth = 0; 179 mHeight = 0; 180 mDev = nDeviceCreate(); 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); 188 if (mContext == 0) { 189 throw new RSDriverException("Failed to create RS context."); 190 } 191 mMessageThread = new MessageThread(this); 192 mMessageThread.start(); 193 } 194 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 } 210 mWidth = w; 211 mHeight = h; 212 nContextSetSurface(w, h, mSurface); 213 } 214 215 /** 216 * return the height of the last set surface. 217 * 218 * @return int 219 */ 220 public int getHeight() { 221 return mHeight; 222 } 223 224 /** 225 * return the width of the last set surface. 226 * 227 * @return int 228 */ 229 public int getWidth() { 230 return mWidth; 231 } 232 233 /** 234 * Temporarly halt calls to the root rendering script. 235 * 236 */ 237 public void pause() { 238 validate(); 239 nContextPause(); 240 } 241 242 /** 243 * Resume calls to the root rendering script. 244 * 245 */ 246 public void resume() { 247 validate(); 248 nContextResume(); 249 } 250 251 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) { 258 validate(); 259 nContextBindRootScript(safeID(s)); 260 } 261 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) { 269 validate(); 270 nContextBindProgramStore(safeID(p)); 271 } 272 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) { 280 validate(); 281 nContextBindProgramFragment(safeID(p)); 282 } 283 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) { 291 validate(); 292 nContextBindProgramRaster(safeID(p)); 293 } 294 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) { 302 validate(); 303 nContextBindProgramVertex(safeID(p)); 304 } 305 306} 307 308 309