/rs/java/android/renderscript/ProgramStore.java

https://github.com/aizuzi/platform_frameworks_base · Java · 445 lines · 193 code · 36 blank · 216 comment · 8 complexity · b53cb6bf465488cde2f06565a23fa3c7 MD5 · raw file

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