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 20 import android.util.Log; 21 22 23 /** 24 * <p>ProgramStore contains a set of parameters that control how 25 * the graphics hardware handles writes to the framebuffer. 26 * It could be used to:</p> 27 * <ul> 28 * <li>enable/disable depth testing</li> 29 * <li>specify wheather depth writes are performed</li> 30 * <li>setup various blending modes for use in effects like 31 * transparency</li> 32 * <li>define write masks for color components written into the 33 * framebuffer</li> 34 * </ul> 35 * 36 **/ 37 public class ProgramStore extends BaseObj { 38 /** 39 * Specifies the function used to determine whether a fragment 40 * will be drawn during the depth testing stage in the rendering 41 * pipeline by comparing its value with that already in the depth 42 * buffer. DepthFunc is only valid when depth buffer is present 43 * and depth testing is enabled 44 */ 45 public enum DepthFunc { 46 47 /** 48 * Always drawn 49 */ 50 ALWAYS (0), 51 /** 52 * Drawn if the incoming depth value is less than that in the 53 * depth buffer 54 */ 55 LESS (1), 56 /** 57 * Drawn if the incoming depth value is less or equal to that in 58 * the depth buffer 59 */ 60 LESS_OR_EQUAL (2), 61 /** 62 * Drawn if the incoming depth value is greater than that in the 63 * depth buffer 64 */ 65 GREATER (3), 66 /** 67 * Drawn if the incoming depth value is greater or equal to that 68 * in the depth buffer 69 */ 70 GREATER_OR_EQUAL (4), 71 /** 72 * Drawn if the incoming depth value is equal to that in the 73 * depth buffer 74 */ 75 EQUAL (5), 76 /** 77 * Drawn if the incoming depth value is not equal to that in the 78 * depth buffer 79 */ 80 NOT_EQUAL (6); 81 82 int mID; 83 DepthFunc(int id) { 84 mID = id; 85 } 86 } 87 88 /** 89 * Specifies the functions used to combine incoming pixels with 90 * those already in the frame buffer. 91 * 92 * BlendSrcFunc describes how the coefficient used to scale the 93 * source pixels during the blending operation is computed 94 * 95 */ 96 public enum BlendSrcFunc { 97 ZERO (0), 98 ONE (1), 99 DST_COLOR (2), 100 ONE_MINUS_DST_COLOR (3), 101 SRC_ALPHA (4), 102 ONE_MINUS_SRC_ALPHA (5), 103 DST_ALPHA (6), 104 ONE_MINUS_DST_ALPHA (7), 105 SRC_ALPHA_SATURATE (8); 106 107 int mID; 108 BlendSrcFunc(int id) { 109 mID = id; 110 } 111 } 112 113 /** 114 * Specifies the functions used to combine incoming pixels with 115 * those already in the frame buffer. 116 * 117 * BlendDstFunc describes how the coefficient used to scale the 118 * pixels already in the framebuffer is computed during the 119 * blending operation 120 * 121 */ 122 public enum BlendDstFunc { 123 ZERO (0), 124 ONE (1), 125 SRC_COLOR (2), 126 ONE_MINUS_SRC_COLOR (3), 127 SRC_ALPHA (4), 128 ONE_MINUS_SRC_ALPHA (5), 129 DST_ALPHA (6), 130 ONE_MINUS_DST_ALPHA (7); 131 132 int mID; 133 BlendDstFunc(int id) { 134 mID = id; 135 } 136 } 137 138 DepthFunc mDepthFunc; 139 boolean mDepthMask; 140 boolean mColorMaskR; 141 boolean mColorMaskG; 142 boolean mColorMaskB; 143 boolean mColorMaskA; 144 BlendSrcFunc mBlendSrc; 145 BlendDstFunc mBlendDst; 146 boolean mDither; 147 148 ProgramStore(int id, RenderScript rs) { 149 super(id, rs); 150 } 151 152 /** 153 * @hide 154 * @return depth function 155 */ 156 public DepthFunc getDepthFunc() { 157 return mDepthFunc; 158 } 159 160 /** 161 * @hide 162 * @return whether depth writes are enabled 163 */ 164 public boolean getDepthMaskEnabled() { 165 return mDepthMask; 166 } 167 168 /** 169 * @hide 170 * @return red color channel mask 171 */ 172 public boolean getColorMaskREnabled() { 173 return mColorMaskR; 174 } 175 176 /** 177 * @hide 178 * @return green color channel mask 179 */ 180 public boolean getColorMaskGEnabled() { 181 return mColorMaskG; 182 } 183 184 /** 185 * @hide 186 * @return blue color channel mask 187 */ 188 public boolean getColorMaskBEnabled() { 189 return mColorMaskB; 190 } 191 192 /** 193 * @hide 194 * @return alpha channel mask 195 */ 196 public boolean getColorMaskAEnabled() { 197 return mColorMaskA; 198 } 199 200 /** 201 * @hide 202 * @return source blend function 203 */ 204 public BlendSrcFunc getBlendSrcFunc() { 205 return mBlendSrc; 206 } 207 208 /** 209 * @hide 210 * @return destination blend function 211 */ 212 public BlendDstFunc getBlendDstFunc() { 213 return mBlendDst; 214 } 215 216 /** 217 * @hide 218 * @return whether dither is enabled 219 */ 220 public boolean getDitherEnabled() { 221 return mDither; 222 } 223 224 /** 225 * Returns a pre-defined program store object with the following 226 * characteristics: 227 * - incoming pixels are drawn if their depth value is less than 228 * the stored value in the depth buffer. If the pixel is 229 * drawn, its value is also stored in the depth buffer 230 * - incoming pixels override the value stored in the color 231 * buffer if it passes the depth test 232 * 233 * @param rs Context to which the program will belong. 234 **/ 235 public static ProgramStore BLEND_NONE_DEPTH_TEST(RenderScript rs) { 236 if(rs.mProgramStore_BLEND_NONE_DEPTH_TEST == null) { 237 ProgramStore.Builder builder = new ProgramStore.Builder(rs); 238 builder.setDepthFunc(ProgramStore.DepthFunc.LESS); 239 builder.setBlendFunc(BlendSrcFunc.ONE, BlendDstFunc.ZERO); 240 builder.setDitherEnabled(false); 241 builder.setDepthMaskEnabled(true); 242 rs.mProgramStore_BLEND_NONE_DEPTH_TEST = builder.create(); 243 } 244 return rs.mProgramStore_BLEND_NONE_DEPTH_TEST; 245 } 246 /** 247 * Returns a pre-defined program store object with the following 248 * characteristics: 249 * - incoming pixels always pass the depth test and their value 250 * is not stored in the depth buffer 251 * - incoming pixels override the value stored in the color 252 * buffer 253 * 254 * @param rs Context to which the program will belong. 255 **/ 256 public static ProgramStore BLEND_NONE_DEPTH_NONE(RenderScript rs) { 257 if(rs.mProgramStore_BLEND_NONE_DEPTH_NO_DEPTH == null) { 258 ProgramStore.Builder builder = new ProgramStore.Builder(rs); 259 builder.setDepthFunc(ProgramStore.DepthFunc.ALWAYS); 260 builder.setBlendFunc(BlendSrcFunc.ONE, BlendDstFunc.ZERO); 261 builder.setDitherEnabled(false); 262 builder.setDepthMaskEnabled(false); 263 rs.mProgramStore_BLEND_NONE_DEPTH_NO_DEPTH = builder.create(); 264 } 265 return rs.mProgramStore_BLEND_NONE_DEPTH_NO_DEPTH; 266 } 267 /** 268 * Returns a pre-defined program store object with the following 269 * characteristics: 270 * - incoming pixels are drawn if their depth value is less than 271 * the stored value in the depth buffer. If the pixel is 272 * drawn, its value is also stored in the depth buffer 273 * - if the incoming (Source) pixel passes depth test, its value 274 * is combined with the stored color (Dest) using the 275 * following formula 276 * Final.RGB = Source.RGB * Source.A + Dest.RGB * (1 - Source.A) 277 * 278 * @param rs Context to which the program will belong. 279 **/ 280 public static ProgramStore BLEND_ALPHA_DEPTH_TEST(RenderScript rs) { 281 if(rs.mProgramStore_BLEND_ALPHA_DEPTH_TEST == null) { 282 ProgramStore.Builder builder = new ProgramStore.Builder(rs); 283 builder.setDepthFunc(ProgramStore.DepthFunc.LESS); 284 builder.setBlendFunc(BlendSrcFunc.SRC_ALPHA, BlendDstFunc.ONE_MINUS_SRC_ALPHA); 285 builder.setDitherEnabled(false); 286 builder.setDepthMaskEnabled(true); 287 rs.mProgramStore_BLEND_ALPHA_DEPTH_TEST = builder.create(); 288 } 289 return rs.mProgramStore_BLEND_ALPHA_DEPTH_TEST; 290 } 291 /** 292 * Returns a pre-defined program store object with the following 293 * characteristics: 294 * - incoming pixels always pass the depth test and their value 295 * is not stored in the depth buffer 296 * - incoming pixel's value is combined with the stored color 297 * (Dest) using the following formula 298 * Final.RGB = Source.RGB * Source.A + Dest.RGB * (1 - Source.A) 299 * 300 * @param rs Context to which the program will belong. 301 **/ 302 public static ProgramStore BLEND_ALPHA_DEPTH_NONE(RenderScript rs) { 303 if(rs.mProgramStore_BLEND_ALPHA_DEPTH_NO_DEPTH == null) { 304 ProgramStore.Builder builder = new ProgramStore.Builder(rs); 305 builder.setDepthFunc(ProgramStore.DepthFunc.ALWAYS); 306 builder.setBlendFunc(BlendSrcFunc.SRC_ALPHA, BlendDstFunc.ONE_MINUS_SRC_ALPHA); 307 builder.setDitherEnabled(false); 308 builder.setDepthMaskEnabled(false); 309 rs.mProgramStore_BLEND_ALPHA_DEPTH_NO_DEPTH = builder.create(); 310 } 311 return rs.mProgramStore_BLEND_ALPHA_DEPTH_NO_DEPTH; 312 } 313 314 /** 315 * Builder class for ProgramStore object. If the builder is left 316 * empty, the equivalent of BLEND_NONE_DEPTH_NONE would be 317 * returned 318 */ 319 public static class Builder { 320 RenderScript mRS; 321 DepthFunc mDepthFunc; 322 boolean mDepthMask; 323 boolean mColorMaskR; 324 boolean mColorMaskG; 325 boolean mColorMaskB; 326 boolean mColorMaskA; 327 BlendSrcFunc mBlendSrc; 328 BlendDstFunc mBlendDst; 329 boolean mDither; 330 331 public Builder(RenderScript rs) { 332 mRS = rs; 333 mDepthFunc = DepthFunc.ALWAYS; 334 mDepthMask = false; 335 mColorMaskR = true; 336 mColorMaskG = true; 337 mColorMaskB = true; 338 mColorMaskA = true; 339 mBlendSrc = BlendSrcFunc.ONE; 340 mBlendDst = BlendDstFunc.ZERO; 341 } 342 343 /** 344 * Specifies the depth testing behavior 345 * 346 * @param func function used for depth testing 347 * 348 * @return this 349 */ 350 public Builder setDepthFunc(DepthFunc func) { 351 mDepthFunc = func; 352 return this; 353 } 354 355 /** 356 * Enables writes into the depth buffer 357 * 358 * @param enable specifies whether depth writes are 359 * enabled or disabled 360 * 361 * @return this 362 */ 363 public Builder setDepthMaskEnabled(boolean enable) { 364 mDepthMask = enable; 365 return this; 366 } 367 368 /** 369 * Enables writes into the color buffer 370 * 371 * @param r specifies whether red channel is written 372 * @param g specifies whether green channel is written 373 * @param b specifies whether blue channel is written 374 * @param a specifies whether alpha channel is written 375 * 376 * @return this 377 */ 378 public Builder setColorMaskEnabled(boolean r, boolean g, boolean b, boolean a) { 379 mColorMaskR = r; 380 mColorMaskG = g; 381 mColorMaskB = b; 382 mColorMaskA = a; 383 return this; 384 } 385 386 /** 387 * Specifies how incoming pixels are combined with the pixels 388 * stored in the framebuffer 389 * 390 * @param src specifies how the source blending factor is 391 * computed 392 * @param dst specifies how the destination blending factor is 393 * computed 394 * 395 * @return this 396 */ 397 public Builder setBlendFunc(BlendSrcFunc src, BlendDstFunc dst) { 398 mBlendSrc = src; 399 mBlendDst = dst; 400 return this; 401 } 402 403 /** 404 * Enables dithering 405 * 406 * @param enable specifies whether dithering is enabled or 407 * disabled 408 * 409 * @return this 410 */ 411 public Builder setDitherEnabled(boolean enable) { 412 mDither = enable; 413 return this; 414 } 415 416 /** 417 * Creates a program store from the current state of the builder 418 */ 419 public ProgramStore create() { 420 mRS.validate(); 421 int id = mRS.nProgramStoreCreate(mColorMaskR, mColorMaskG, mColorMaskB, mColorMaskA, 422 mDepthMask, mDither, 423 mBlendSrc.mID, mBlendDst.mID, mDepthFunc.mID); 424 ProgramStore programStore = new ProgramStore(id, mRS); 425 programStore.mDepthFunc = mDepthFunc; 426 programStore.mDepthMask = mDepthMask; 427 programStore.mColorMaskR = mColorMaskR; 428 programStore.mColorMaskG = mColorMaskG; 429 programStore.mColorMaskB = mColorMaskB; 430 programStore.mColorMaskA = mColorMaskA; 431 programStore.mBlendSrc = mBlendSrc; 432 programStore.mBlendDst = mBlendDst; 433 programStore.mDither = mDither; 434 return programStore; 435 } 436 } 437 438 } 439 440 441 442 443